1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBNETLINK_H__
3 #define __LIBNETLINK_H__ 1
8 #include <linux/netlink.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/if_link.h>
11 #include <linux/if_addr.h>
12 #include <linux/neighbour.h>
13 #include <linux/netconf.h>
14 #include <arpa/inet.h>
18 struct sockaddr_nl local
;
19 struct sockaddr_nl peer
;
24 #define RTNL_HANDLE_F_LISTEN_ALL_NSID 0x01
25 #define RTNL_HANDLE_F_SUPPRESS_NLERR 0x02
26 #define RTNL_HANDLE_F_STRICT_CHK 0x04
31 struct nlmsg_list
*next
;
36 struct nlmsg_list
*head
;
37 struct nlmsg_list
*tail
;
42 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
43 __attribute__((warn_unused_result
));
45 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
47 __attribute__((warn_unused_result
));
49 void rtnl_close(struct rtnl_handle
*rth
);
50 void rtnl_set_strict_dump(struct rtnl_handle
*rth
);
52 typedef int (*req_filter_fn_t
)(struct nlmsghdr
*nlh
, int reqlen
);
54 int rtnl_addrdump_req(struct rtnl_handle
*rth
, int family
,
55 req_filter_fn_t filter_fn
)
56 __attribute__((warn_unused_result
));
57 int rtnl_addrlbldump_req(struct rtnl_handle
*rth
, int family
)
58 __attribute__((warn_unused_result
));
59 int rtnl_routedump_req(struct rtnl_handle
*rth
, int family
,
60 req_filter_fn_t filter_fn
)
61 __attribute__((warn_unused_result
));
62 int rtnl_ruledump_req(struct rtnl_handle
*rth
, int family
)
63 __attribute__((warn_unused_result
));
64 int rtnl_neighdump_req(struct rtnl_handle
*rth
, int family
,
65 req_filter_fn_t filter_fn
)
66 __attribute__((warn_unused_result
));
67 int rtnl_neightbldump_req(struct rtnl_handle
*rth
, int family
)
68 __attribute__((warn_unused_result
));
69 int rtnl_mdbdump_req(struct rtnl_handle
*rth
, int family
)
70 __attribute__((warn_unused_result
));
71 int rtnl_netconfdump_req(struct rtnl_handle
*rth
, int family
)
72 __attribute__((warn_unused_result
));
73 int rtnl_nsiddump_req(struct rtnl_handle
*rth
, int family
)
74 __attribute__((warn_unused_result
));
76 int rtnl_linkdump_req(struct rtnl_handle
*rth
, int fam
)
77 __attribute__((warn_unused_result
));
78 int rtnl_linkdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
79 __attribute__((warn_unused_result
));
81 int rtnl_linkdump_req_filter_fn(struct rtnl_handle
*rth
, int fam
,
83 __attribute__((warn_unused_result
));
84 int rtnl_statsdump_req_filter(struct rtnl_handle
*rth
, int fam
, __u32 filt_mask
)
85 __attribute__((warn_unused_result
));
86 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
,
88 __attribute__((warn_unused_result
));
89 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
90 __attribute__((warn_unused_result
));
92 struct rtnl_ctrl_data
{
96 typedef int (*rtnl_filter_t
)(struct nlmsghdr
*n
, void *);
98 typedef int (*rtnl_listen_filter_t
)(struct rtnl_ctrl_data
*,
99 struct nlmsghdr
*n
, void *);
101 typedef int (*nl_ext_ack_fn_t
)(const char *errmsg
, uint32_t off
,
102 const struct nlmsghdr
*inner_nlh
);
104 struct rtnl_dump_filter_arg
{
105 rtnl_filter_t filter
;
110 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
111 rtnl_filter_t filter
,
112 void *arg
, __u16 nc_flags
);
113 #define rtnl_dump_filter(rth, filter, arg) \
114 rtnl_dump_filter_nc(rth, filter, arg, 0)
115 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
116 struct nlmsghdr
**answer
)
117 __attribute__((warn_unused_result
));
118 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
119 struct nlmsghdr
**answer
)
120 __attribute__((warn_unused_result
));
121 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
122 struct nlmsghdr
**answer
)
123 __attribute__((warn_unused_result
));
124 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int)
125 __attribute__((warn_unused_result
));
126 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int)
127 __attribute__((warn_unused_result
));
128 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
);
130 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
);
131 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
);
132 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
);
133 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
);
134 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
);
135 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *data
);
137 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
,
138 const void *data
, int alen
);
139 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
);
140 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
);
141 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
);
142 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
143 const void *data
, int len
);
144 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*nest
);
145 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
);
146 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
);
147 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
);
148 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
);
149 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
150 const void *data
, int alen
);
152 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
);
153 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
154 int len
, unsigned short flags
);
155 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
);
156 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
);
158 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
);
159 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
);
161 #define RTA_TAIL(rta) \
162 ((struct rtattr *) (((void *) (rta)) + \
163 RTA_ALIGN((rta)->rta_len)))
165 #define parse_rtattr_nested(tb, max, rta) \
166 (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta)))
168 #define parse_rtattr_one_nested(type, rta) \
169 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
171 #define parse_rtattr_nested_compat(tb, max, rta, data, len) \
172 ({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
173 __parse_rtattr_nested_compat(tb, max, rta, len); })
175 static inline __u8
rta_getattr_u8(const struct rtattr
*rta
)
177 return *(__u8
*)RTA_DATA(rta
);
179 static inline __u16
rta_getattr_u16(const struct rtattr
*rta
)
181 return *(__u16
*)RTA_DATA(rta
);
183 static inline __be16
rta_getattr_be16(const struct rtattr
*rta
)
185 return ntohs(rta_getattr_u16(rta
));
187 static inline __u32
rta_getattr_u32(const struct rtattr
*rta
)
189 return *(__u32
*)RTA_DATA(rta
);
191 static inline __be32
rta_getattr_be32(const struct rtattr
*rta
)
193 return ntohl(rta_getattr_u32(rta
));
195 static inline __u64
rta_getattr_u64(const struct rtattr
*rta
)
199 memcpy(&tmp
, RTA_DATA(rta
), sizeof(__u64
));
202 static inline __s32
rta_getattr_s32(const struct rtattr
*rta
)
204 return *(__s32
*)RTA_DATA(rta
);
206 static inline __s64
rta_getattr_s64(const struct rtattr
*rta
)
210 memcpy(&tmp
, RTA_DATA(rta
), sizeof(tmp
));
213 static inline const char *rta_getattr_str(const struct rtattr
*rta
)
215 return (const char *)RTA_DATA(rta
);
218 int rtnl_listen_all_nsid(struct rtnl_handle
*);
219 int rtnl_listen(struct rtnl_handle
*, rtnl_listen_filter_t handler
,
221 int rtnl_from_file(FILE *, rtnl_listen_filter_t handler
,
224 #define NLMSG_TAIL(nmsg) \
225 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
229 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
232 #define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg))
236 #define IFLA_RTA(r) \
237 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
240 #define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg))
245 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
248 #define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndmsg))
252 #define NDTA_RTA(r) \
253 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
256 #define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg))
260 #define NETNS_RTA(r) \
261 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
263 #ifndef NETNS_PAYLOAD
264 #define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg))
267 #ifndef IFLA_STATS_RTA
268 #define IFLA_STATS_RTA(r) \
269 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg))))
272 /* User defined nlmsg_type which is used mostly for logging netlink
273 * messages from dump file */
274 #define NLMSG_TSTAMP 15
276 #endif /* __LIBNETLINK_H__ */