1
2#ifndef __LIBNETLINK_H__
3#define __LIBNETLINK_H__ 1
4
5#include <stdio.h>
6#include <string.h>
7#include <asm/types.h>
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>
15
16struct rtnl_handle {
17 int fd;
18 struct sockaddr_nl local;
19 struct sockaddr_nl peer;
20 __u32 seq;
21 __u32 dump;
22 int proto;
23 FILE *dump_fp;
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
27 int flags;
28};
29
30struct nlmsg_list {
31 struct nlmsg_list *next;
32 struct nlmsghdr h;
33};
34
35struct nlmsg_chain {
36 struct nlmsg_list *head;
37 struct nlmsg_list *tail;
38};
39
40struct ipstats_req {
41 struct nlmsghdr nlh;
42 struct if_stats_msg ifsm;
43 char buf[128];
44};
45
46extern int rcvbuf;
47
48int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
49 __attribute__((warn_unused_result));
50
51int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
52 int protocol)
53 __attribute__((warn_unused_result));
54int rtnl_add_nl_group(struct rtnl_handle *rth, unsigned int group)
55 __attribute__((warn_unused_result));
56void rtnl_close(struct rtnl_handle *rth);
57void rtnl_set_strict_dump(struct rtnl_handle *rth);
58
59typedef int (*req_filter_fn_t)(struct nlmsghdr *nlh, int reqlen);
60
61int rtnl_addrdump_req(struct rtnl_handle *rth, int family,
62 req_filter_fn_t filter_fn)
63 __attribute__((warn_unused_result));
64int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family)
65 __attribute__((warn_unused_result));
66int rtnl_routedump_req(struct rtnl_handle *rth, int family,
67 req_filter_fn_t filter_fn)
68 __attribute__((warn_unused_result));
69int rtnl_ruledump_req(struct rtnl_handle *rth, int family)
70 __attribute__((warn_unused_result));
71int rtnl_neighdump_req(struct rtnl_handle *rth, int family,
72 req_filter_fn_t filter_fn)
73 __attribute__((warn_unused_result));
74int rtnl_neightbldump_req(struct rtnl_handle *rth, int family)
75 __attribute__((warn_unused_result));
76int rtnl_mdbdump_req(struct rtnl_handle *rth, int family)
77 __attribute__((warn_unused_result));
78int rtnl_brvlandump_req(struct rtnl_handle *rth, int family, __u32 dump_flags)
79 __attribute__((warn_unused_result));
80int rtnl_netconfdump_req(struct rtnl_handle *rth, int family)
81 __attribute__((warn_unused_result));
82
83int rtnl_linkdump_req(struct rtnl_handle *rth, int fam)
84 __attribute__((warn_unused_result));
85int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask)
86 __attribute__((warn_unused_result));
87
88int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int fam,
89 req_filter_fn_t fn)
90 __attribute__((warn_unused_result));
91int rtnl_fdb_linkdump_req_filter_fn(struct rtnl_handle *rth,
92 req_filter_fn_t filter_fn)
93 __attribute__((warn_unused_result));
94int rtnl_nsiddump_req_filter_fn(struct rtnl_handle *rth, int family,
95 req_filter_fn_t filter_fn)
96 __attribute__((warn_unused_result));
97int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask,
98 int (*filter_fn)(struct ipstats_req *req,
99 void *data),
100 void *filter_data)
101 __attribute__((warn_unused_result));
102int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
103 int len)
104 __attribute__((warn_unused_result));
105int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n)
106 __attribute__((warn_unused_result));
107
108int rtnl_nexthopdump_req(struct rtnl_handle *rth, int family,
109 req_filter_fn_t filter_fn)
110 __attribute__((warn_unused_result));
111int rtnl_nexthop_bucket_dump_req(struct rtnl_handle *rth, int family,
112 req_filter_fn_t filter_fn)
113 __attribute__((warn_unused_result));
114
115int rtnl_tunneldump_req(struct rtnl_handle *rth, int family, int ifindex,
116 __u8 flags)
117 __attribute__((warn_unused_result));
118
119struct rtnl_ctrl_data {
120 int nsid;
121};
122
123typedef int (*rtnl_filter_t)(struct nlmsghdr *n, void *);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140#define RTNL_LET_NLERR 0x01
141#define RTNL_SUPPRESS_NLMSG_DONE_NLERR 0x02
142#define RTNL_SUPPRESS_NLMSG_ERROR_NLERR 0x04
143#define RTNL_SUPPRESS_NLERR 0x06
144typedef int (*rtnl_err_hndlr_t)(struct nlmsghdr *n, void *);
145
146typedef int (*rtnl_listen_filter_t)(struct rtnl_ctrl_data *,
147 struct nlmsghdr *n, void *);
148
149typedef int (*nl_ext_ack_fn_t)(const char *errmsg, uint32_t off,
150 const struct nlmsghdr *inner_nlh);
151
152struct rtnl_dump_filter_arg {
153 rtnl_filter_t filter;
154 void *arg1;
155 rtnl_err_hndlr_t errhndlr;
156 void *arg2;
157 __u16 nc_flags;
158};
159
160int rtnl_dump_filter_nc(struct rtnl_handle *rth,
161 rtnl_filter_t filter,
162 void *arg, __u16 nc_flags);
163#define rtnl_dump_filter(rth, filter, arg) \
164 rtnl_dump_filter_nc(rth, filter, arg, 0)
165int rtnl_dump_filter_errhndlr_nc(struct rtnl_handle *rth,
166 rtnl_filter_t filter,
167 void *arg1,
168 rtnl_err_hndlr_t errhndlr,
169 void *arg2,
170 __u16 nc_flags);
171#define rtnl_dump_filter_errhndlr(rth, filter, farg, errhndlr, earg) \
172 rtnl_dump_filter_errhndlr_nc(rth, filter, farg, errhndlr, earg, 0)
173
174int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
175 struct nlmsghdr **answer)
176 __attribute__((warn_unused_result));
177int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen,
178 struct nlmsghdr **answer)
179 __attribute__((warn_unused_result));
180int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n,
181 struct nlmsghdr **answer)
182 __attribute__((warn_unused_result));
183int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
184 __attribute__((warn_unused_result));
185int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
186 __attribute__((warn_unused_result));
187int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn);
188int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, int error);
189
190int addattr(struct nlmsghdr *n, int maxlen, int type);
191int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
192int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data);
193int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data);
194int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data);
195int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *data);
196
197int addattr_l(struct nlmsghdr *n, int maxlen, int type,
198 const void *data, int alen);
199int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len);
200struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type);
201int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
202struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
203 const void *data, int len);
204int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *nest);
205int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data);
206int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data);
207int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data);
208int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data);
209int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
210 const void *data, int alen);
211
212int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len);
213int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
214 int len, unsigned short flags);
215struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len);
216int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, int len);
217
218struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type);
219int rta_nest_end(struct rtattr *rta, struct rtattr *nest);
220
221#define RTA_TAIL(rta) \
222 ((struct rtattr *) (((void *) (rta)) + \
223 RTA_ALIGN((rta)->rta_len)))
224
225#define parse_rtattr_nested(tb, max, rta) \
226 (parse_rtattr_flags((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta), \
227 NLA_F_NESTED))
228
229#define parse_rtattr_one_nested(type, rta) \
230 (parse_rtattr_one(type, RTA_DATA(rta), RTA_PAYLOAD(rta)))
231
232#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
233 ({ data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
234 __parse_rtattr_nested_compat(tb, max, rta, len); })
235
236static inline __u8 rta_getattr_u8(const struct rtattr *rta)
237{
238 return *(__u8 *)RTA_DATA(rta);
239}
240static inline __u16 rta_getattr_u16(const struct rtattr *rta)
241{
242 return *(__u16 *)RTA_DATA(rta);
243}
244static inline __be16 rta_getattr_be16(const struct rtattr *rta)
245{
246 return ntohs(rta_getattr_u16(rta));
247}
248static inline __u32 rta_getattr_u32(const struct rtattr *rta)
249{
250 return *(__u32 *)RTA_DATA(rta);
251}
252static inline __be32 rta_getattr_be32(const struct rtattr *rta)
253{
254 return ntohl(rta_getattr_u32(rta));
255}
256static inline __u64 rta_getattr_u64(const struct rtattr *rta)
257{
258 __u64 tmp;
259
260 memcpy(&tmp, RTA_DATA(rta), sizeof(__u64));
261 return tmp;
262}
263static inline __s32 rta_getattr_s32(const struct rtattr *rta)
264{
265 return *(__s32 *)RTA_DATA(rta);
266}
267static inline __s64 rta_getattr_s64(const struct rtattr *rta)
268{
269 __s64 tmp;
270
271 memcpy(&tmp, RTA_DATA(rta), sizeof(tmp));
272 return tmp;
273}
274static inline const char *rta_getattr_str(const struct rtattr *rta)
275{
276 return (const char *)RTA_DATA(rta);
277}
278
279int rtnl_listen_all_nsid(struct rtnl_handle *);
280int rtnl_listen(struct rtnl_handle *, rtnl_listen_filter_t handler,
281 void *jarg);
282int rtnl_from_file(FILE *, rtnl_listen_filter_t handler,
283 void *jarg);
284
285#define NLMSG_TAIL(nmsg) \
286 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
287
288#ifndef IFA_RTA
289#define IFA_RTA(r) \
290 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
291#endif
292#ifndef IFA_PAYLOAD
293#define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg))
294#endif
295
296#ifndef IFLA_RTA
297#define IFLA_RTA(r) \
298 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))))
299#endif
300#ifndef IFLA_PAYLOAD
301#define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg))
302#endif
303
304#ifndef NDA_RTA
305#define NDA_RTA(r) \
306 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
307#endif
308#ifndef NDA_PAYLOAD
309#define NDA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndmsg))
310#endif
311
312#ifndef NDTA_RTA
313#define NDTA_RTA(r) \
314 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndtmsg))))
315#endif
316#ifndef NDTA_PAYLOAD
317#define NDTA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ndtmsg))
318#endif
319
320#ifndef NETNS_RTA
321#define NETNS_RTA(r) \
322 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
323#endif
324#ifndef NETNS_PAYLOAD
325#define NETNS_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtgenmsg))
326#endif
327
328#ifndef IFLA_STATS_RTA
329#define IFLA_STATS_RTA(r) \
330 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct if_stats_msg))))
331#endif
332
333#ifndef BRVLAN_RTA
334#define BRVLAN_RTA(r) \
335 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct br_vlan_msg))))
336#endif
337
338#ifndef TUNNEL_RTA
339#define TUNNEL_RTA(r) \
340 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct tunnel_msg))))
341#endif
342
343
344
345#define NLMSG_TSTAMP 15
346
347#define rtattr_for_each_nested(attr, nest) \
348 for ((attr) = (void *)RTA_DATA(nest); \
349 RTA_OK(attr, RTA_PAYLOAD(nest) - ((char *)(attr) - (char *)RTA_DATA((nest)))); \
350 (attr) = RTA_TAIL((attr)))
351
352void nl_print_policy(const struct rtattr *attr, FILE *fp);
353
354#endif
355