]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * libnetlink.c RTnetlink service routines. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
463d9efa | 15 | #include <stdbool.h> |
aba5acdf | 16 | #include <unistd.h> |
aba5acdf SH |
17 | #include <fcntl.h> |
18 | #include <net/if_arp.h> | |
19 | #include <sys/socket.h> | |
20 | #include <netinet/in.h> | |
21 | #include <string.h> | |
22 | #include <errno.h> | |
23 | #include <time.h> | |
24 | #include <sys/uio.h> | |
b05d9a3d | 25 | #include <linux/fib_rules.h> |
39360023 | 26 | #include <linux/if_addrlabel.h> |
9dbe6df4 | 27 | #include <linux/if_bridge.h> |
aba5acdf SH |
28 | |
29 | #include "libnetlink.h" | |
30 | ||
92e03242 DA |
31 | #define __aligned(x) __attribute__((aligned(x))) |
32 | ||
449b824a ND |
33 | #ifndef SOL_NETLINK |
34 | #define SOL_NETLINK 270 | |
35 | #endif | |
36 | ||
c079e121 SH |
37 | #ifndef MIN |
38 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
39 | #endif | |
40 | ||
7f03191f PM |
41 | int rcvbuf = 1024 * 1024; |
42 | ||
b6432e68 SH |
43 | #ifdef HAVE_LIBMNL |
44 | #include <libmnl/libmnl.h> | |
45 | ||
46 | static const enum mnl_attr_data_type extack_policy[NLMSGERR_ATTR_MAX + 1] = { | |
47 | [NLMSGERR_ATTR_MSG] = MNL_TYPE_NUL_STRING, | |
48 | [NLMSGERR_ATTR_OFFS] = MNL_TYPE_U32, | |
49 | }; | |
50 | ||
51 | static int err_attr_cb(const struct nlattr *attr, void *data) | |
52 | { | |
53 | const struct nlattr **tb = data; | |
54 | uint16_t type; | |
55 | ||
e5fa0e6f DA |
56 | if (mnl_attr_type_valid(attr, NLMSGERR_ATTR_MAX) < 0) { |
57 | fprintf(stderr, "Invalid extack attribute\n"); | |
b6432e68 | 58 | return MNL_CB_ERROR; |
e5fa0e6f | 59 | } |
b6432e68 SH |
60 | |
61 | type = mnl_attr_get_type(attr); | |
e5fa0e6f DA |
62 | if (mnl_attr_validate(attr, extack_policy[type]) < 0) { |
63 | fprintf(stderr, "extack attribute %d failed validation\n", | |
64 | type); | |
b6432e68 | 65 | return MNL_CB_ERROR; |
e5fa0e6f | 66 | } |
b6432e68 SH |
67 | |
68 | tb[type] = attr; | |
69 | return MNL_CB_OK; | |
70 | } | |
71 | ||
2750252d DA |
72 | static void print_ext_ack_msg(bool is_err, const char *msg) |
73 | { | |
74 | fprintf(stderr, "%s: %s", is_err ? "Error" : "Warning", msg); | |
75 | if (msg[strlen(msg) - 1] != '.') | |
76 | fprintf(stderr, "."); | |
77 | fprintf(stderr, "\n"); | |
78 | } | |
79 | ||
b6432e68 | 80 | /* dump netlink extended ack error message */ |
049c5853 | 81 | int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn) |
b6432e68 | 82 | { |
e5fa0e6f | 83 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {}; |
b6432e68 SH |
84 | const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh); |
85 | const struct nlmsghdr *err_nlh = NULL; | |
86 | unsigned int hlen = sizeof(*err); | |
844c37b4 | 87 | const char *msg = NULL; |
b6432e68 SH |
88 | uint32_t off = 0; |
89 | ||
b6432e68 SH |
90 | /* no TLVs, nothing to do here */ |
91 | if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS)) | |
92 | return 0; | |
93 | ||
94 | /* if NLM_F_CAPPED is set then the inner err msg was capped */ | |
95 | if (!(nlh->nlmsg_flags & NLM_F_CAPPED)) | |
96 | hlen += mnl_nlmsg_get_payload_len(&err->msg); | |
97 | ||
e5fa0e6f DA |
98 | if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK) |
99 | return 0; | |
b6432e68 SH |
100 | |
101 | if (tb[NLMSGERR_ATTR_MSG]) | |
844c37b4 | 102 | msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]); |
b6432e68 SH |
103 | |
104 | if (tb[NLMSGERR_ATTR_OFFS]) { | |
105 | off = mnl_attr_get_u32(tb[NLMSGERR_ATTR_OFFS]); | |
106 | ||
107 | if (off > nlh->nlmsg_len) { | |
108 | fprintf(stderr, | |
109 | "Invalid offset for NLMSGERR_ATTR_OFFS\n"); | |
110 | off = 0; | |
111 | } else if (!(nlh->nlmsg_flags & NLM_F_CAPPED)) | |
112 | err_nlh = &err->msg; | |
113 | } | |
114 | ||
fb6cb307 | 115 | if (errfn) |
844c37b4 | 116 | return errfn(msg, off, err_nlh); |
fb6cb307 | 117 | |
844c37b4 DA |
118 | if (msg && *msg != '\0') { |
119 | bool is_err = !!err->error; | |
120 | ||
2750252d DA |
121 | print_ext_ack_msg(is_err, msg); |
122 | return is_err ? 1 : 0; | |
123 | } | |
fb6cb307 | 124 | |
2750252d DA |
125 | return 0; |
126 | } | |
127 | ||
128 | static int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, int error) | |
129 | { | |
130 | struct nlattr *tb[NLMSGERR_ATTR_MAX + 1] = {}; | |
131 | unsigned int hlen = sizeof(int); | |
132 | const char *msg = NULL; | |
133 | ||
134 | if (mnl_attr_parse(nlh, hlen, err_attr_cb, tb) != MNL_CB_OK) | |
135 | return 0; | |
136 | ||
137 | if (tb[NLMSGERR_ATTR_MSG]) | |
138 | msg = mnl_attr_get_str(tb[NLMSGERR_ATTR_MSG]); | |
139 | ||
140 | if (msg && *msg != '\0') { | |
141 | bool is_err = !!error; | |
142 | ||
143 | print_ext_ack_msg(is_err, msg); | |
844c37b4 | 144 | return is_err ? 1 : 0; |
fb6cb307 DA |
145 | } |
146 | ||
147 | return 0; | |
b6432e68 SH |
148 | } |
149 | #else | |
7d23fa55 SH |
150 | #warning "libmnl required for error support" |
151 | ||
b6432e68 | 152 | /* No extended error ack without libmnl */ |
049c5853 | 153 | int nl_dump_ext_ack(const struct nlmsghdr *nlh, nl_ext_ack_fn_t errfn) |
b6432e68 SH |
154 | { |
155 | return 0; | |
156 | } | |
2750252d DA |
157 | |
158 | static int nl_dump_ext_ack_done(const struct nlmsghdr *nlh, int error) | |
159 | { | |
160 | return 0; | |
161 | } | |
b6432e68 SH |
162 | #endif |
163 | ||
aea41afc DA |
164 | /* Older kernels may not support strict dump and filtering */ |
165 | void rtnl_set_strict_dump(struct rtnl_handle *rth) | |
166 | { | |
167 | int one = 1; | |
168 | ||
285033bf DA |
169 | if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK, |
170 | &one, sizeof(one)) < 0) | |
171 | return; | |
172 | ||
173 | rth->flags |= RTNL_HANDLE_F_STRICT_CHK; | |
aea41afc DA |
174 | } |
175 | ||
aba5acdf SH |
176 | void rtnl_close(struct rtnl_handle *rth) |
177 | { | |
3bfa73ff SH |
178 | if (rth->fd >= 0) { |
179 | close(rth->fd); | |
180 | rth->fd = -1; | |
181 | } | |
aba5acdf SH |
182 | } |
183 | ||
2c500a4d | 184 | int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions, |
8ed63ab1 | 185 | int protocol) |
aba5acdf | 186 | { |
f332d169 | 187 | socklen_t addr_len; |
007d3a3e | 188 | int sndbuf = 32768; |
b6432e68 | 189 | int one = 1; |
aba5acdf | 190 | |
b16621ca | 191 | memset(rth, 0, sizeof(*rth)); |
aba5acdf | 192 | |
8a4025f6 | 193 | rth->proto = protocol; |
bcb9d403 | 194 | rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol); |
aba5acdf SH |
195 | if (rth->fd < 0) { |
196 | perror("Cannot open netlink socket"); | |
197 | return -1; | |
198 | } | |
199 | ||
2c500a4d SH |
200 | if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, |
201 | &sndbuf, sizeof(sndbuf)) < 0) { | |
007d3a3e SH |
202 | perror("SO_SNDBUF"); |
203 | return -1; | |
204 | } | |
205 | ||
2c500a4d SH |
206 | if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF, |
207 | &rcvbuf, sizeof(rcvbuf)) < 0) { | |
007d3a3e SH |
208 | perror("SO_RCVBUF"); |
209 | return -1; | |
210 | } | |
211 | ||
b6432e68 SH |
212 | /* Older kernels may no support extended ACK reporting */ |
213 | setsockopt(rth->fd, SOL_NETLINK, NETLINK_EXT_ACK, | |
214 | &one, sizeof(one)); | |
215 | ||
aba5acdf SH |
216 | memset(&rth->local, 0, sizeof(rth->local)); |
217 | rth->local.nl_family = AF_NETLINK; | |
218 | rth->local.nl_groups = subscriptions; | |
219 | ||
2c500a4d SH |
220 | if (bind(rth->fd, (struct sockaddr *)&rth->local, |
221 | sizeof(rth->local)) < 0) { | |
aba5acdf SH |
222 | perror("Cannot bind netlink socket"); |
223 | return -1; | |
224 | } | |
225 | addr_len = sizeof(rth->local); | |
2c500a4d SH |
226 | if (getsockname(rth->fd, (struct sockaddr *)&rth->local, |
227 | &addr_len) < 0) { | |
aba5acdf SH |
228 | perror("Cannot getsockname"); |
229 | return -1; | |
230 | } | |
231 | if (addr_len != sizeof(rth->local)) { | |
232 | fprintf(stderr, "Wrong address length %d\n", addr_len); | |
233 | return -1; | |
234 | } | |
235 | if (rth->local.nl_family != AF_NETLINK) { | |
2c500a4d SH |
236 | fprintf(stderr, "Wrong address family %d\n", |
237 | rth->local.nl_family); | |
aba5acdf SH |
238 | return -1; |
239 | } | |
240 | rth->seq = time(NULL); | |
241 | return 0; | |
242 | } | |
243 | ||
2c500a4d | 244 | int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions) |
c7699875 | 245 | { |
246 | return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE); | |
247 | } | |
248 | ||
88470978 DA |
249 | int rtnl_addrdump_req(struct rtnl_handle *rth, int family, |
250 | req_filter_fn_t filter_fn) | |
46917d08 DA |
251 | { |
252 | struct { | |
253 | struct nlmsghdr nlh; | |
254 | struct ifaddrmsg ifm; | |
88470978 | 255 | char buf[128]; |
46917d08 | 256 | } req = { |
92e03242 | 257 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), |
46917d08 DA |
258 | .nlh.nlmsg_type = RTM_GETADDR, |
259 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
260 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
261 | .ifm.ifa_family = family, | |
262 | }; | |
263 | ||
88470978 DA |
264 | if (filter_fn) { |
265 | int err; | |
266 | ||
267 | err = filter_fn(&req.nlh, sizeof(req)); | |
268 | if (err) | |
269 | return err; | |
270 | } | |
271 | ||
46917d08 DA |
272 | return send(rth->fd, &req, sizeof(req), 0); |
273 | } | |
274 | ||
39360023 DA |
275 | int rtnl_addrlbldump_req(struct rtnl_handle *rth, int family) |
276 | { | |
277 | struct { | |
278 | struct nlmsghdr nlh; | |
279 | struct ifaddrlblmsg ifal; | |
280 | } req = { | |
92e03242 | 281 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)), |
39360023 DA |
282 | .nlh.nlmsg_type = RTM_GETADDRLABEL, |
283 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
284 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
285 | .ifal.ifal_family = family, | |
286 | }; | |
287 | ||
288 | return send(rth->fd, &req, sizeof(req), 0); | |
289 | } | |
290 | ||
43fd93ae DA |
291 | int rtnl_routedump_req(struct rtnl_handle *rth, int family, |
292 | req_filter_fn_t filter_fn) | |
bfb27dfa DA |
293 | { |
294 | struct { | |
295 | struct nlmsghdr nlh; | |
296 | struct rtmsg rtm; | |
43fd93ae | 297 | char buf[128]; |
bfb27dfa | 298 | } req = { |
92e03242 | 299 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)), |
bfb27dfa DA |
300 | .nlh.nlmsg_type = RTM_GETROUTE, |
301 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
302 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
303 | .rtm.rtm_family = family, | |
304 | }; | |
305 | ||
43fd93ae DA |
306 | if (filter_fn) { |
307 | int err; | |
308 | ||
309 | err = filter_fn(&req.nlh, sizeof(req)); | |
310 | if (err) | |
311 | return err; | |
312 | } | |
313 | ||
bfb27dfa DA |
314 | return send(rth->fd, &req, sizeof(req), 0); |
315 | } | |
316 | ||
b05d9a3d DA |
317 | int rtnl_ruledump_req(struct rtnl_handle *rth, int family) |
318 | { | |
319 | struct { | |
320 | struct nlmsghdr nlh; | |
321 | struct fib_rule_hdr frh; | |
322 | } req = { | |
92e03242 | 323 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), |
b05d9a3d DA |
324 | .nlh.nlmsg_type = RTM_GETRULE, |
325 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
326 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
327 | .frh.family = family | |
328 | }; | |
329 | ||
330 | return send(rth->fd, &req, sizeof(req), 0); | |
331 | } | |
332 | ||
f255ab12 DA |
333 | int rtnl_neighdump_req(struct rtnl_handle *rth, int family, |
334 | req_filter_fn_t filter_fn) | |
9e0ab19c DA |
335 | { |
336 | struct { | |
337 | struct nlmsghdr nlh; | |
338 | struct ndmsg ndm; | |
f255ab12 | 339 | char buf[256]; |
9e0ab19c | 340 | } req = { |
92e03242 | 341 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)), |
9e0ab19c DA |
342 | .nlh.nlmsg_type = RTM_GETNEIGH, |
343 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
344 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
345 | .ndm.ndm_family = family, | |
346 | }; | |
347 | ||
f255ab12 DA |
348 | if (filter_fn) { |
349 | int err; | |
350 | ||
351 | err = filter_fn(&req.nlh, sizeof(req)); | |
352 | if (err) | |
353 | return err; | |
354 | } | |
355 | ||
9e0ab19c DA |
356 | return send(rth->fd, &req, sizeof(req), 0); |
357 | } | |
358 | ||
ff41db8a DA |
359 | int rtnl_neightbldump_req(struct rtnl_handle *rth, int family) |
360 | { | |
361 | struct { | |
362 | struct nlmsghdr nlh; | |
363 | struct ndtmsg ndtmsg; | |
364 | } req = { | |
92e03242 | 365 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg)), |
ff41db8a DA |
366 | .nlh.nlmsg_type = RTM_GETNEIGHTBL, |
367 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
368 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
369 | .ndtmsg.ndtm_family = family, | |
370 | }; | |
371 | ||
372 | return send(rth->fd, &req, sizeof(req), 0); | |
373 | } | |
374 | ||
9dbe6df4 DA |
375 | int rtnl_mdbdump_req(struct rtnl_handle *rth, int family) |
376 | { | |
377 | struct { | |
378 | struct nlmsghdr nlh; | |
379 | struct br_port_msg bpm; | |
380 | } req = { | |
92e03242 | 381 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_port_msg)), |
9dbe6df4 DA |
382 | .nlh.nlmsg_type = RTM_GETMDB, |
383 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
384 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
385 | .bpm.family = family, | |
386 | }; | |
387 | ||
388 | return send(rth->fd, &req, sizeof(req), 0); | |
389 | } | |
390 | ||
ddee16bc DA |
391 | int rtnl_netconfdump_req(struct rtnl_handle *rth, int family) |
392 | { | |
393 | struct { | |
394 | struct nlmsghdr nlh; | |
395 | struct netconfmsg ncm; | |
92e03242 | 396 | char buf[0] __aligned(NLMSG_ALIGNTO); |
ddee16bc | 397 | } req = { |
92e03242 | 398 | .nlh.nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct netconfmsg))), |
ddee16bc DA |
399 | .nlh.nlmsg_type = RTM_GETNETCONF, |
400 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
401 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
402 | .ncm.ncm_family = family, | |
403 | }; | |
404 | ||
405 | return send(rth->fd, &req, sizeof(req), 0); | |
406 | } | |
407 | ||
efb0b383 DA |
408 | int rtnl_nsiddump_req(struct rtnl_handle *rth, int family) |
409 | { | |
410 | struct { | |
411 | struct nlmsghdr nlh; | |
412 | struct rtgenmsg rtm; | |
92e03242 | 413 | char buf[0] __aligned(NLMSG_ALIGNTO); |
efb0b383 | 414 | } req = { |
92e03242 | 415 | .nlh.nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct rtgenmsg))), |
efb0b383 DA |
416 | .nlh.nlmsg_type = RTM_GETNSID, |
417 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
418 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
419 | .rtm.rtgen_family = family, | |
420 | }; | |
421 | ||
422 | return send(rth->fd, &req, sizeof(req), 0); | |
423 | } | |
424 | ||
d97b16b2 | 425 | static int __rtnl_linkdump_req(struct rtnl_handle *rth, int family) |
aba5acdf SH |
426 | { |
427 | struct { | |
428 | struct nlmsghdr nlh; | |
63338dca | 429 | struct ifinfomsg ifm; |
d17b136f | 430 | } req = { |
d97b16b2 | 431 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
31ae2912 | 432 | .nlh.nlmsg_type = RTM_GETLINK, |
d17b136f PS |
433 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
434 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
435 | .ifm.ifi_family = family, | |
d17b136f | 436 | }; |
bd886ebb | 437 | |
2c500a4d | 438 | return send(rth->fd, &req, sizeof(req), 0); |
aba5acdf SH |
439 | } |
440 | ||
d97b16b2 | 441 | int rtnl_linkdump_req(struct rtnl_handle *rth, int family) |
b0a4ce62 | 442 | { |
d97b16b2 DA |
443 | if (family == AF_UNSPEC) |
444 | return rtnl_linkdump_req_filter(rth, family, RTEXT_FILTER_VF); | |
445 | ||
446 | return __rtnl_linkdump_req(rth, family); | |
447 | } | |
448 | ||
449 | int rtnl_linkdump_req_filter(struct rtnl_handle *rth, int family, | |
450 | __u32 filt_mask) | |
451 | { | |
452 | if (family == AF_UNSPEC) { | |
453 | struct { | |
454 | struct nlmsghdr nlh; | |
455 | struct ifinfomsg ifm; | |
456 | /* attribute has to be NLMSG aligned */ | |
457 | struct rtattr ext_req __aligned(NLMSG_ALIGNTO); | |
458 | __u32 ext_filter_mask; | |
459 | } req = { | |
460 | .nlh.nlmsg_len = sizeof(req), | |
461 | .nlh.nlmsg_type = RTM_GETLINK, | |
462 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
463 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
464 | .ifm.ifi_family = family, | |
465 | .ext_req.rta_type = IFLA_EXT_MASK, | |
466 | .ext_req.rta_len = RTA_LENGTH(sizeof(__u32)), | |
467 | .ext_filter_mask = filt_mask, | |
468 | }; | |
469 | ||
470 | return send(rth->fd, &req, sizeof(req), 0); | |
471 | } | |
b0a4ce62 | 472 | |
d97b16b2 DA |
473 | return __rtnl_linkdump_req(rth, family); |
474 | } | |
b0a4ce62 | 475 | |
d97b16b2 DA |
476 | int rtnl_linkdump_req_filter_fn(struct rtnl_handle *rth, int family, |
477 | req_filter_fn_t filter_fn) | |
478 | { | |
479 | if (family == AF_UNSPEC) { | |
480 | struct { | |
481 | struct nlmsghdr nlh; | |
482 | struct ifinfomsg ifm; | |
483 | char buf[1024]; | |
484 | } req = { | |
485 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
486 | .nlh.nlmsg_type = RTM_GETLINK, | |
487 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
488 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
489 | .ifm.ifi_family = family, | |
490 | }; | |
491 | int err; | |
492 | ||
493 | if (!filter_fn) | |
494 | return -EINVAL; | |
495 | ||
496 | err = filter_fn(&req.nlh, sizeof(req)); | |
497 | if (err) | |
498 | return err; | |
499 | ||
500 | return send(rth->fd, &req, req.nlh.nlmsg_len, 0); | |
501 | } | |
b0a4ce62 | 502 | |
d97b16b2 | 503 | return __rtnl_linkdump_req(rth, family); |
b0a4ce62 DA |
504 | } |
505 | ||
56eeeda9 | 506 | int rtnl_statsdump_req_filter(struct rtnl_handle *rth, int fam, __u32 filt_mask) |
7abf5de6 NA |
507 | { |
508 | struct { | |
509 | struct nlmsghdr nlh; | |
510 | struct if_stats_msg ifsm; | |
511 | } req; | |
512 | ||
513 | memset(&req, 0, sizeof(req)); | |
514 | req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct if_stats_msg)); | |
56eeeda9 | 515 | req.nlh.nlmsg_type = RTM_GETSTATS; |
7abf5de6 NA |
516 | req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST; |
517 | req.nlh.nlmsg_pid = 0; | |
518 | req.nlh.nlmsg_seq = rth->dump = ++rth->seq; | |
519 | req.ifsm.family = fam; | |
520 | req.ifsm.filter_mask = filt_mask; | |
521 | ||
2c500a4d | 522 | return send(rth->fd, &req, sizeof(req), 0); |
7abf5de6 NA |
523 | } |
524 | ||
6cf8398f | 525 | int rtnl_send(struct rtnl_handle *rth, const void *buf, int len) |
aba5acdf | 526 | { |
f31a37f7 SH |
527 | return send(rth->fd, buf, len, 0); |
528 | } | |
529 | ||
6cf8398f | 530 | int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len) |
f31a37f7 | 531 | { |
54bb35c6 SH |
532 | struct nlmsghdr *h; |
533 | int status; | |
534 | char resp[1024]; | |
aba5acdf | 535 | |
f31a37f7 | 536 | status = send(rth->fd, buf, len, 0); |
54bb35c6 SH |
537 | if (status < 0) |
538 | return status; | |
539 | ||
2d8240f8 SH |
540 | /* Check for immediate errors */ |
541 | status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK); | |
54bb35c6 SH |
542 | if (status < 0) { |
543 | if (errno == EAGAIN) | |
544 | return 0; | |
545 | return -1; | |
546 | } | |
547 | ||
548 | for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status); | |
549 | h = NLMSG_NEXT(h, status)) { | |
550 | if (h->nlmsg_type == NLMSG_ERROR) { | |
2c500a4d SH |
551 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); |
552 | ||
54bb35c6 SH |
553 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) |
554 | fprintf(stderr, "ERROR truncated\n"); | |
e9e9365b | 555 | else |
54bb35c6 | 556 | errno = -err->error; |
24f38182 | 557 | return -1; |
54bb35c6 | 558 | } |
54bb35c6 SH |
559 | } |
560 | ||
561 | return 0; | |
aba5acdf SH |
562 | } |
563 | ||
564 | int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len) | |
565 | { | |
d17b136f PS |
566 | struct nlmsghdr nlh = { |
567 | .nlmsg_len = NLMSG_LENGTH(len), | |
568 | .nlmsg_type = type, | |
569 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
570 | .nlmsg_seq = rth->dump = ++rth->seq, | |
571 | }; | |
6cf8398f | 572 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
8ed63ab1 SH |
573 | struct iovec iov[2] = { |
574 | { .iov_base = &nlh, .iov_len = sizeof(nlh) }, | |
575 | { .iov_base = req, .iov_len = len } | |
576 | }; | |
aba5acdf | 577 | struct msghdr msg = { |
8ed63ab1 | 578 | .msg_name = &nladdr, |
d17b136f | 579 | .msg_namelen = sizeof(nladdr), |
8ed63ab1 SH |
580 | .msg_iov = iov, |
581 | .msg_iovlen = 2, | |
aba5acdf SH |
582 | }; |
583 | ||
aba5acdf SH |
584 | return sendmsg(rth->fd, &msg, 0); |
585 | } | |
586 | ||
0d238ca2 DA |
587 | int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n) |
588 | { | |
589 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; | |
590 | struct iovec iov = { | |
2c500a4d | 591 | .iov_base = n, |
0d238ca2 DA |
592 | .iov_len = n->nlmsg_len |
593 | }; | |
594 | struct msghdr msg = { | |
595 | .msg_name = &nladdr, | |
596 | .msg_namelen = sizeof(nladdr), | |
597 | .msg_iov = &iov, | |
598 | .msg_iovlen = 1, | |
599 | }; | |
600 | ||
601 | n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST; | |
602 | n->nlmsg_pid = 0; | |
603 | n->nlmsg_seq = rth->dump = ++rth->seq; | |
604 | ||
605 | return sendmsg(rth->fd, &msg, 0); | |
606 | } | |
607 | ||
0efa6257 | 608 | static int rtnl_dump_done(struct nlmsghdr *h) |
892a25e2 SH |
609 | { |
610 | int len = *(int *)NLMSG_DATA(h); | |
611 | ||
05a14fc1 DA |
612 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(int))) { |
613 | fprintf(stderr, "DONE truncated\n"); | |
614 | return -1; | |
615 | } | |
892a25e2 | 616 | |
05a14fc1 | 617 | if (len < 0) { |
2750252d DA |
618 | /* check for any messages returned from kernel */ |
619 | if (nl_dump_ext_ack_done(h, len)) | |
620 | return len; | |
621 | ||
05a14fc1 DA |
622 | errno = -len; |
623 | switch (errno) { | |
624 | case ENOENT: | |
625 | case EOPNOTSUPP: | |
626 | return -1; | |
627 | case EMSGSIZE: | |
628 | fprintf(stderr, | |
629 | "Error: Buffer too small for object.\n"); | |
630 | break; | |
631 | default: | |
892a25e2 | 632 | perror("RTNETLINK answers"); |
892a25e2 | 633 | } |
05a14fc1 | 634 | return len; |
892a25e2 | 635 | } |
05a14fc1 | 636 | |
844c37b4 DA |
637 | /* check for any messages returned from kernel */ |
638 | nl_dump_ext_ack(h, NULL); | |
639 | ||
892a25e2 SH |
640 | return 0; |
641 | } | |
642 | ||
643 | static void rtnl_dump_error(const struct rtnl_handle *rth, | |
644 | struct nlmsghdr *h) | |
645 | { | |
646 | ||
647 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
648 | fprintf(stderr, "ERROR truncated\n"); | |
649 | } else { | |
650 | const struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); | |
651 | ||
652 | errno = -err->error; | |
653 | if (rth->proto == NETLINK_SOCK_DIAG && | |
654 | (errno == ENOENT || | |
655 | errno == EOPNOTSUPP)) | |
656 | return; | |
657 | ||
3ad6d176 DA |
658 | if (!(rth->flags & RTNL_HANDLE_F_SUPPRESS_NLERR)) |
659 | perror("RTNETLINK answers"); | |
892a25e2 SH |
660 | } |
661 | } | |
662 | ||
2d34851c HL |
663 | static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags) |
664 | { | |
665 | int len; | |
666 | ||
667 | do { | |
668 | len = recvmsg(fd, msg, flags); | |
669 | } while (len < 0 && (errno == EINTR || errno == EAGAIN)); | |
670 | ||
671 | if (len < 0) { | |
672 | fprintf(stderr, "netlink receive error %s (%d)\n", | |
673 | strerror(errno), errno); | |
674 | return -errno; | |
675 | } | |
676 | ||
677 | if (len == 0) { | |
678 | fprintf(stderr, "EOF on netlink\n"); | |
679 | return -ENODATA; | |
680 | } | |
681 | ||
682 | return len; | |
683 | } | |
684 | ||
685 | static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer) | |
686 | { | |
687 | struct iovec *iov = msg->msg_iov; | |
688 | char *buf; | |
689 | int len; | |
690 | ||
691 | iov->iov_base = NULL; | |
692 | iov->iov_len = 0; | |
693 | ||
694 | len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC); | |
695 | if (len < 0) | |
696 | return len; | |
697 | ||
698 | buf = malloc(len); | |
699 | if (!buf) { | |
700 | fprintf(stderr, "malloc error: not enough buffer\n"); | |
701 | return -ENOMEM; | |
702 | } | |
703 | ||
704 | iov->iov_base = buf; | |
705 | iov->iov_len = len; | |
706 | ||
707 | len = __rtnl_recvmsg(fd, msg, 0); | |
708 | if (len < 0) { | |
709 | free(buf); | |
710 | return len; | |
711 | } | |
712 | ||
713 | if (answer) | |
714 | *answer = buf; | |
715 | else | |
716 | free(buf); | |
717 | ||
718 | return len; | |
719 | } | |
720 | ||
1d2fac41 SH |
721 | static int rtnl_dump_filter_l(struct rtnl_handle *rth, |
722 | const struct rtnl_dump_filter_arg *arg) | |
aba5acdf | 723 | { |
aba5acdf | 724 | struct sockaddr_nl nladdr; |
8ed63ab1 SH |
725 | struct iovec iov; |
726 | struct msghdr msg = { | |
727 | .msg_name = &nladdr, | |
728 | .msg_namelen = sizeof(nladdr), | |
729 | .msg_iov = &iov, | |
730 | .msg_iovlen = 1, | |
731 | }; | |
2d34851c | 732 | char *buf; |
16f02e14 | 733 | int dump_intr = 0; |
aba5acdf SH |
734 | |
735 | while (1) { | |
736 | int status; | |
b49240ec | 737 | const struct rtnl_dump_filter_arg *a; |
3bc1c4f2 BG |
738 | int found_done = 0; |
739 | int msglen = 0; | |
aba5acdf | 740 | |
2d34851c HL |
741 | status = rtnl_recvmsg(rth->fd, &msg, &buf); |
742 | if (status < 0) | |
743 | return status; | |
aba5acdf | 744 | |
486ccd99 VK |
745 | if (rth->dump_fp) |
746 | fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp); | |
747 | ||
b49240ec | 748 | for (a = arg; a->filter; a++) { |
2c500a4d SH |
749 | struct nlmsghdr *h = (struct nlmsghdr *)buf; |
750 | ||
3bc1c4f2 | 751 | msglen = status; |
b49240ec | 752 | |
3bc1c4f2 | 753 | while (NLMSG_OK(h, msglen)) { |
486ccd99 | 754 | int err = 0; |
b49240ec | 755 | |
8e72880f PS |
756 | h->nlmsg_flags &= ~a->nc_flags; |
757 | ||
b49240ec SH |
758 | if (nladdr.nl_pid != 0 || |
759 | h->nlmsg_pid != rth->local.nl_pid || | |
cd70f3f5 | 760 | h->nlmsg_seq != rth->dump) |
b49240ec | 761 | goto skip_it; |
aba5acdf | 762 | |
16f02e14 ND |
763 | if (h->nlmsg_flags & NLM_F_DUMP_INTR) |
764 | dump_intr = 1; | |
765 | ||
3bc1c4f2 | 766 | if (h->nlmsg_type == NLMSG_DONE) { |
0efa6257 | 767 | err = rtnl_dump_done(h); |
2d34851c HL |
768 | if (err < 0) { |
769 | free(buf); | |
892a25e2 | 770 | return -1; |
2d34851c | 771 | } |
892a25e2 | 772 | |
3bc1c4f2 BG |
773 | found_done = 1; |
774 | break; /* process next filter */ | |
775 | } | |
892a25e2 | 776 | |
b49240ec | 777 | if (h->nlmsg_type == NLMSG_ERROR) { |
892a25e2 | 778 | rtnl_dump_error(rth, h); |
2d34851c | 779 | free(buf); |
b49240ec | 780 | return -1; |
aba5acdf | 781 | } |
486ccd99 VK |
782 | |
783 | if (!rth->dump_fp) { | |
cd554f2c | 784 | err = a->filter(h, a->arg1); |
2d34851c HL |
785 | if (err < 0) { |
786 | free(buf); | |
486ccd99 | 787 | return err; |
2d34851c | 788 | } |
486ccd99 | 789 | } |
aba5acdf SH |
790 | |
791 | skip_it: | |
3bc1c4f2 | 792 | h = NLMSG_NEXT(h, msglen); |
b49240ec | 793 | } |
3bc1c4f2 | 794 | } |
2d34851c | 795 | free(buf); |
3bc1c4f2 | 796 | |
16f02e14 ND |
797 | if (found_done) { |
798 | if (dump_intr) | |
799 | fprintf(stderr, | |
800 | "Dump was interrupted and may be inconsistent.\n"); | |
3bc1c4f2 | 801 | return 0; |
16f02e14 | 802 | } |
3bc1c4f2 | 803 | |
aba5acdf SH |
804 | if (msg.msg_flags & MSG_TRUNC) { |
805 | fprintf(stderr, "Message truncated\n"); | |
806 | continue; | |
807 | } | |
3bc1c4f2 BG |
808 | if (msglen) { |
809 | fprintf(stderr, "!!!Remnant of size %d\n", msglen); | |
aba5acdf SH |
810 | exit(1); |
811 | } | |
812 | } | |
813 | } | |
814 | ||
8e72880f | 815 | int rtnl_dump_filter_nc(struct rtnl_handle *rth, |
b49240ec | 816 | rtnl_filter_t filter, |
8e72880f | 817 | void *arg1, __u16 nc_flags) |
b49240ec SH |
818 | { |
819 | const struct rtnl_dump_filter_arg a[2] = { | |
8e72880f PS |
820 | { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, }, |
821 | { .filter = NULL, .arg1 = NULL, .nc_flags = 0, }, | |
b49240ec SH |
822 | }; |
823 | ||
824 | return rtnl_dump_filter_l(rth, a); | |
825 | } | |
826 | ||
b6432e68 SH |
827 | static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err, |
828 | nl_ext_ack_fn_t errfn) | |
829 | { | |
844c37b4 | 830 | if (nl_dump_ext_ack(h, errfn)) |
b6432e68 SH |
831 | return; |
832 | ||
833 | fprintf(stderr, "RTNETLINK answers: %s\n", | |
834 | strerror(-err->error)); | |
835 | } | |
836 | ||
72a2ff39 CM |
837 | |
838 | static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov, | |
839 | size_t iovlen, struct nlmsghdr **answer, | |
840 | bool show_rtnl_err, nl_ext_ack_fn_t errfn) | |
aba5acdf | 841 | { |
d17b136f | 842 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
72a2ff39 | 843 | struct iovec riov; |
aba5acdf | 844 | struct msghdr msg = { |
8ed63ab1 SH |
845 | .msg_name = &nladdr, |
846 | .msg_namelen = sizeof(nladdr), | |
72a2ff39 CM |
847 | .msg_iov = iov, |
848 | .msg_iovlen = iovlen, | |
aba5acdf | 849 | }; |
72a2ff39 CM |
850 | unsigned int seq = 0; |
851 | struct nlmsghdr *h; | |
852 | int i, status; | |
2d34851c | 853 | char *buf; |
aba5acdf | 854 | |
72a2ff39 CM |
855 | for (i = 0; i < iovlen; i++) { |
856 | h = iov[i].iov_base; | |
857 | h->nlmsg_seq = seq = ++rtnl->seq; | |
858 | if (answer == NULL) | |
859 | h->nlmsg_flags |= NLM_F_ACK; | |
860 | } | |
aba5acdf SH |
861 | |
862 | status = sendmsg(rtnl->fd, &msg, 0); | |
aba5acdf SH |
863 | if (status < 0) { |
864 | perror("Cannot talk to rtnetlink"); | |
865 | return -1; | |
866 | } | |
867 | ||
72a2ff39 CM |
868 | /* change msg to use the response iov */ |
869 | msg.msg_iov = &riov; | |
870 | msg.msg_iovlen = 1; | |
871 | i = 0; | |
aba5acdf | 872 | while (1) { |
0951cbcd | 873 | next: |
2d34851c | 874 | status = rtnl_recvmsg(rtnl->fd, &msg, &buf); |
72a2ff39 | 875 | ++i; |
2d34851c HL |
876 | |
877 | if (status < 0) | |
878 | return status; | |
aba5acdf | 879 | |
aba5acdf | 880 | if (msg.msg_namelen != sizeof(nladdr)) { |
2c500a4d SH |
881 | fprintf(stderr, |
882 | "sender address length == %d\n", | |
883 | msg.msg_namelen); | |
aba5acdf SH |
884 | exit(1); |
885 | } | |
2c500a4d | 886 | for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) { |
aba5acdf SH |
887 | int len = h->nlmsg_len; |
888 | int l = len - sizeof(*h); | |
889 | ||
2c500a4d | 890 | if (l < 0 || len > status) { |
aba5acdf SH |
891 | if (msg.msg_flags & MSG_TRUNC) { |
892 | fprintf(stderr, "Truncated message\n"); | |
2d34851c | 893 | free(buf); |
aba5acdf SH |
894 | return -1; |
895 | } | |
2c500a4d SH |
896 | fprintf(stderr, |
897 | "!!!malformed message: len=%d\n", | |
898 | len); | |
aba5acdf SH |
899 | exit(1); |
900 | } | |
901 | ||
c079e121 | 902 | if (nladdr.nl_pid != 0 || |
10f57ef1 | 903 | h->nlmsg_pid != rtnl->local.nl_pid || |
72a2ff39 | 904 | h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) { |
4cca16f2 SH |
905 | /* Don't forget to skip that message. */ |
906 | status -= NLMSG_ALIGN(len); | |
2c500a4d | 907 | h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); |
aba5acdf SH |
908 | continue; |
909 | } | |
910 | ||
911 | if (h->nlmsg_type == NLMSG_ERROR) { | |
2c500a4d | 912 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); |
8c50b728 | 913 | int error = err->error; |
2c500a4d | 914 | |
aba5acdf SH |
915 | if (l < sizeof(struct nlmsgerr)) { |
916 | fprintf(stderr, "ERROR truncated\n"); | |
c60389e4 SH |
917 | free(buf); |
918 | return -1; | |
919 | } | |
920 | ||
bfb3bf18 | 921 | if (!error) { |
844c37b4 DA |
922 | /* check messages from kernel */ |
923 | nl_dump_ext_ack(h, errfn); | |
bfb3bf18 SH |
924 | } else { |
925 | errno = -error; | |
844c37b4 | 926 | |
b45e3000 SH |
927 | if (rtnl->proto != NETLINK_SOCK_DIAG && |
928 | show_rtnl_err) | |
929 | rtnl_talk_error(h, err, errfn); | |
930 | } | |
b6432e68 | 931 | |
c60389e4 SH |
932 | if (answer) |
933 | *answer = (struct nlmsghdr *)buf; | |
934 | else | |
935 | free(buf); | |
b45e3000 | 936 | |
0951cbcd PM |
937 | if (i < iovlen) |
938 | goto next; | |
8c50b728 | 939 | return error ? -i : 0; |
aba5acdf | 940 | } |
c079e121 | 941 | |
aba5acdf | 942 | if (answer) { |
86bf43c7 | 943 | *answer = (struct nlmsghdr *)buf; |
aba5acdf SH |
944 | return 0; |
945 | } | |
946 | ||
947 | fprintf(stderr, "Unexpected reply!!!\n"); | |
948 | ||
949 | status -= NLMSG_ALIGN(len); | |
2c500a4d | 950 | h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); |
aba5acdf | 951 | } |
2d34851c | 952 | free(buf); |
c079e121 | 953 | |
aba5acdf SH |
954 | if (msg.msg_flags & MSG_TRUNC) { |
955 | fprintf(stderr, "Message truncated\n"); | |
956 | continue; | |
957 | } | |
c079e121 | 958 | |
aba5acdf SH |
959 | if (status) { |
960 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
961 | exit(1); | |
962 | } | |
963 | } | |
964 | } | |
965 | ||
72a2ff39 CM |
966 | static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, |
967 | struct nlmsghdr **answer, | |
968 | bool show_rtnl_err, nl_ext_ack_fn_t errfn) | |
969 | { | |
970 | struct iovec iov = { | |
971 | .iov_base = n, | |
972 | .iov_len = n->nlmsg_len | |
973 | }; | |
974 | ||
975 | return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn); | |
976 | } | |
977 | ||
463d9efa | 978 | int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, |
86bf43c7 | 979 | struct nlmsghdr **answer) |
463d9efa | 980 | { |
86bf43c7 | 981 | return __rtnl_talk(rtnl, n, answer, true, NULL); |
b6432e68 SH |
982 | } |
983 | ||
72a2ff39 CM |
984 | int rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iovec, size_t iovlen, |
985 | struct nlmsghdr **answer) | |
986 | { | |
987 | return __rtnl_talk_iov(rtnl, iovec, iovlen, answer, true, NULL); | |
988 | } | |
989 | ||
463d9efa | 990 | int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle *rtnl, struct nlmsghdr *n, |
86bf43c7 | 991 | struct nlmsghdr **answer) |
463d9efa | 992 | { |
86bf43c7 | 993 | return __rtnl_talk(rtnl, n, answer, false, NULL); |
463d9efa DA |
994 | } |
995 | ||
449b824a ND |
996 | int rtnl_listen_all_nsid(struct rtnl_handle *rth) |
997 | { | |
998 | unsigned int on = 1; | |
999 | ||
1000 | if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on, | |
1001 | sizeof(on)) < 0) { | |
1002 | perror("NETLINK_LISTEN_ALL_NSID"); | |
1003 | return -1; | |
1004 | } | |
1005 | rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID; | |
1006 | return 0; | |
1007 | } | |
1008 | ||
8ed63ab1 | 1009 | int rtnl_listen(struct rtnl_handle *rtnl, |
0628cddd | 1010 | rtnl_listen_filter_t handler, |
6dc9f016 | 1011 | void *jarg) |
aba5acdf SH |
1012 | { |
1013 | int status; | |
1014 | struct nlmsghdr *h; | |
d17b136f | 1015 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
aba5acdf | 1016 | struct iovec iov; |
aba5acdf | 1017 | struct msghdr msg = { |
8ed63ab1 SH |
1018 | .msg_name = &nladdr, |
1019 | .msg_namelen = sizeof(nladdr), | |
1020 | .msg_iov = &iov, | |
1021 | .msg_iovlen = 1, | |
aba5acdf | 1022 | }; |
e557212e | 1023 | char buf[16384]; |
449b824a ND |
1024 | char cmsgbuf[BUFSIZ]; |
1025 | ||
1026 | if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) { | |
1027 | msg.msg_control = &cmsgbuf; | |
1028 | msg.msg_controllen = sizeof(cmsgbuf); | |
1029 | } | |
aba5acdf | 1030 | |
aba5acdf | 1031 | iov.iov_base = buf; |
aba5acdf | 1032 | while (1) { |
449b824a ND |
1033 | struct rtnl_ctrl_data ctrl; |
1034 | struct cmsghdr *cmsg; | |
1035 | ||
aba5acdf SH |
1036 | iov.iov_len = sizeof(buf); |
1037 | status = recvmsg(rtnl->fd, &msg, 0); | |
1038 | ||
1039 | if (status < 0) { | |
aa8032e6 | 1040 | if (errno == EINTR || errno == EAGAIN) |
aba5acdf | 1041 | continue; |
aa8032e6 SH |
1042 | fprintf(stderr, "netlink receive error %s (%d)\n", |
1043 | strerror(errno), errno); | |
7f03191f PM |
1044 | if (errno == ENOBUFS) |
1045 | continue; | |
aa8032e6 | 1046 | return -1; |
aba5acdf SH |
1047 | } |
1048 | if (status == 0) { | |
1049 | fprintf(stderr, "EOF on netlink\n"); | |
1050 | return -1; | |
1051 | } | |
1052 | if (msg.msg_namelen != sizeof(nladdr)) { | |
2c500a4d SH |
1053 | fprintf(stderr, |
1054 | "Sender address length == %d\n", | |
1055 | msg.msg_namelen); | |
aba5acdf SH |
1056 | exit(1); |
1057 | } | |
449b824a ND |
1058 | |
1059 | if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) { | |
1060 | memset(&ctrl, 0, sizeof(ctrl)); | |
1061 | ctrl.nsid = -1; | |
1062 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; | |
1063 | cmsg = CMSG_NXTHDR(&msg, cmsg)) | |
1064 | if (cmsg->cmsg_level == SOL_NETLINK && | |
1065 | cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID && | |
1066 | cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { | |
1067 | int *data = (int *)CMSG_DATA(cmsg); | |
1068 | ||
1069 | ctrl.nsid = *data; | |
1070 | } | |
1071 | } | |
1072 | ||
2c500a4d | 1073 | for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) { |
aba5acdf SH |
1074 | int err; |
1075 | int len = h->nlmsg_len; | |
1076 | int l = len - sizeof(*h); | |
1077 | ||
2c500a4d | 1078 | if (l < 0 || len > status) { |
aba5acdf SH |
1079 | if (msg.msg_flags & MSG_TRUNC) { |
1080 | fprintf(stderr, "Truncated message\n"); | |
1081 | return -1; | |
1082 | } | |
2c500a4d SH |
1083 | fprintf(stderr, |
1084 | "!!!malformed message: len=%d\n", | |
1085 | len); | |
aba5acdf SH |
1086 | exit(1); |
1087 | } | |
1088 | ||
cd554f2c | 1089 | err = handler(&ctrl, h, jarg); |
aba5acdf SH |
1090 | if (err < 0) |
1091 | return err; | |
1092 | ||
1093 | status -= NLMSG_ALIGN(len); | |
2c500a4d | 1094 | h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); |
aba5acdf SH |
1095 | } |
1096 | if (msg.msg_flags & MSG_TRUNC) { | |
1097 | fprintf(stderr, "Message truncated\n"); | |
1098 | continue; | |
1099 | } | |
1100 | if (status) { | |
1101 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
1102 | exit(1); | |
1103 | } | |
1104 | } | |
1105 | } | |
1106 | ||
0628cddd | 1107 | int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler, |
6dc9f016 | 1108 | void *jarg) |
aba5acdf SH |
1109 | { |
1110 | int status; | |
2c500a4d SH |
1111 | char buf[16384]; |
1112 | struct nlmsghdr *h = (struct nlmsghdr *)buf; | |
aba5acdf | 1113 | |
aba5acdf | 1114 | while (1) { |
2dd9f8e0 | 1115 | int err, len; |
aba5acdf SH |
1116 | int l; |
1117 | ||
1118 | status = fread(&buf, 1, sizeof(*h), rtnl); | |
1119 | ||
1120 | if (status < 0) { | |
1121 | if (errno == EINTR) | |
1122 | continue; | |
1123 | perror("rtnl_from_file: fread"); | |
1124 | return -1; | |
1125 | } | |
1126 | if (status == 0) | |
1127 | return 0; | |
1128 | ||
1129 | len = h->nlmsg_len; | |
aba5acdf SH |
1130 | l = len - sizeof(*h); |
1131 | ||
2c500a4d | 1132 | if (l < 0 || len > sizeof(buf)) { |
aba5acdf SH |
1133 | fprintf(stderr, "!!!malformed message: len=%d @%lu\n", |
1134 | len, ftell(rtnl)); | |
1135 | return -1; | |
1136 | } | |
1137 | ||
1138 | status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl); | |
1139 | ||
1140 | if (status < 0) { | |
1141 | perror("rtnl_from_file: fread"); | |
1142 | return -1; | |
1143 | } | |
1144 | if (status < l) { | |
1145 | fprintf(stderr, "rtnl-from_file: truncated message\n"); | |
1146 | return -1; | |
1147 | } | |
1148 | ||
cd554f2c | 1149 | err = handler(NULL, h, jarg); |
aba5acdf SH |
1150 | if (err < 0) |
1151 | return err; | |
1152 | } | |
1153 | } | |
1154 | ||
2aa3dd29 SH |
1155 | int addattr(struct nlmsghdr *n, int maxlen, int type) |
1156 | { | |
1157 | return addattr_l(n, maxlen, type, NULL, 0); | |
1158 | } | |
1159 | ||
1160 | int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data) | |
1161 | { | |
1162 | return addattr_l(n, maxlen, type, &data, sizeof(__u8)); | |
1163 | } | |
1164 | ||
1165 | int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data) | |
1166 | { | |
1167 | return addattr_l(n, maxlen, type, &data, sizeof(__u16)); | |
1168 | } | |
1169 | ||
aba5acdf SH |
1170 | int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data) |
1171 | { | |
2aa3dd29 SH |
1172 | return addattr_l(n, maxlen, type, &data, sizeof(__u32)); |
1173 | } | |
1174 | ||
1175 | int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data) | |
1176 | { | |
1177 | return addattr_l(n, maxlen, type, &data, sizeof(__u64)); | |
1178 | } | |
1179 | ||
1180 | int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str) | |
1181 | { | |
1182 | return addattr_l(n, maxlen, type, str, strlen(str)+1); | |
aba5acdf SH |
1183 | } |
1184 | ||
8ed63ab1 | 1185 | int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, |
6dc9f016 | 1186 | int alen) |
aba5acdf SH |
1187 | { |
1188 | int len = RTA_LENGTH(alen); | |
1189 | struct rtattr *rta; | |
1190 | ||
3dabdbb3 | 1191 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { |
2c500a4d SH |
1192 | fprintf(stderr, |
1193 | "addattr_l ERROR: message exceeded bound of %d\n", | |
1194 | maxlen); | |
aba5acdf | 1195 | return -1; |
007d3a3e | 1196 | } |
07f94362 | 1197 | rta = NLMSG_TAIL(n); |
aba5acdf SH |
1198 | rta->rta_type = type; |
1199 | rta->rta_len = len; | |
893deac4 PS |
1200 | if (alen) |
1201 | memcpy(RTA_DATA(rta), data, alen); | |
3dabdbb3 | 1202 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); |
aba5acdf SH |
1203 | return 0; |
1204 | } | |
1205 | ||
07f94362 | 1206 | int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len) |
1207 | { | |
1208 | if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) { | |
2c500a4d SH |
1209 | fprintf(stderr, |
1210 | "addraw_l ERROR: message exceeded bound of %d\n", | |
1211 | maxlen); | |
07f94362 | 1212 | return -1; |
1213 | } | |
1214 | ||
1215 | memcpy(NLMSG_TAIL(n), data, len); | |
1216 | memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len); | |
1217 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len); | |
1218 | return 0; | |
1219 | } | |
1220 | ||
2f90c9c0 PM |
1221 | struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type) |
1222 | { | |
1223 | struct rtattr *nest = NLMSG_TAIL(n); | |
1224 | ||
1225 | addattr_l(n, maxlen, type, NULL, 0); | |
1226 | return nest; | |
1227 | } | |
1228 | ||
1229 | int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest) | |
1230 | { | |
1231 | nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest; | |
1232 | return n->nlmsg_len; | |
1233 | } | |
1234 | ||
1235 | struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type, | |
1236 | const void *data, int len) | |
1237 | { | |
1238 | struct rtattr *start = NLMSG_TAIL(n); | |
1239 | ||
1240 | addattr_l(n, maxlen, type, data, len); | |
1241 | addattr_nest(n, maxlen, type); | |
1242 | return start; | |
1243 | } | |
1244 | ||
1245 | int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start) | |
1246 | { | |
1247 | struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len); | |
1248 | ||
1249 | start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start; | |
1250 | addattr_nest_end(n, nest); | |
1251 | return n->nlmsg_len; | |
1252 | } | |
1253 | ||
aba5acdf SH |
1254 | int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data) |
1255 | { | |
1256 | int len = RTA_LENGTH(4); | |
1257 | struct rtattr *subrta; | |
1258 | ||
007d3a3e | 1259 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) { |
2c500a4d SH |
1260 | fprintf(stderr, |
1261 | "rta_addattr32: Error! max allowed bound %d exceeded\n", | |
1262 | maxlen); | |
aba5acdf | 1263 | return -1; |
007d3a3e | 1264 | } |
2c500a4d | 1265 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); |
aba5acdf SH |
1266 | subrta->rta_type = type; |
1267 | subrta->rta_len = len; | |
1268 | memcpy(RTA_DATA(subrta), &data, 4); | |
1269 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len; | |
1270 | return 0; | |
1271 | } | |
1272 | ||
8ed63ab1 | 1273 | int rta_addattr_l(struct rtattr *rta, int maxlen, int type, |
6dc9f016 | 1274 | const void *data, int alen) |
aba5acdf SH |
1275 | { |
1276 | struct rtattr *subrta; | |
1277 | int len = RTA_LENGTH(alen); | |
1278 | ||
3dabdbb3 | 1279 | if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) { |
2c500a4d SH |
1280 | fprintf(stderr, |
1281 | "rta_addattr_l: Error! max allowed bound %d exceeded\n", | |
1282 | maxlen); | |
aba5acdf | 1283 | return -1; |
007d3a3e | 1284 | } |
2c500a4d | 1285 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); |
aba5acdf SH |
1286 | subrta->rta_type = type; |
1287 | subrta->rta_len = len; | |
893deac4 PS |
1288 | if (alen) |
1289 | memcpy(RTA_DATA(subrta), data, alen); | |
3dabdbb3 | 1290 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); |
aba5acdf SH |
1291 | return 0; |
1292 | } | |
1293 | ||
303cc9cb RP |
1294 | int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data) |
1295 | { | |
1296 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8)); | |
1297 | } | |
1298 | ||
1299 | int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data) | |
1300 | { | |
1301 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16)); | |
1302 | } | |
1303 | ||
1304 | int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data) | |
1305 | { | |
1306 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64)); | |
1307 | } | |
1308 | ||
1309 | struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type) | |
1310 | { | |
1311 | struct rtattr *nest = RTA_TAIL(rta); | |
1312 | ||
1313 | rta_addattr_l(rta, maxlen, type, NULL, 0); | |
1314 | ||
1315 | return nest; | |
1316 | } | |
1317 | ||
1318 | int rta_nest_end(struct rtattr *rta, struct rtattr *nest) | |
1319 | { | |
1320 | nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest; | |
1321 | ||
1322 | return rta->rta_len; | |
1323 | } | |
1324 | ||
aba5acdf SH |
1325 | int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) |
1326 | { | |
b1b7ce0f VY |
1327 | return parse_rtattr_flags(tb, max, rta, len, 0); |
1328 | } | |
1329 | ||
1330 | int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta, | |
1331 | int len, unsigned short flags) | |
1332 | { | |
1333 | unsigned short type; | |
1334 | ||
175e2440 | 1335 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); |
aba5acdf | 1336 | while (RTA_OK(rta, len)) { |
b1b7ce0f VY |
1337 | type = rta->rta_type & ~flags; |
1338 | if ((type <= max) && (!tb[type])) | |
1339 | tb[type] = rta; | |
2c500a4d | 1340 | rta = RTA_NEXT(rta, len); |
aba5acdf SH |
1341 | } |
1342 | if (len) | |
2c500a4d SH |
1343 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", |
1344 | len, rta->rta_len); | |
aba5acdf SH |
1345 | return 0; |
1346 | } | |
c7699875 | 1347 | |
decbb437 JP |
1348 | struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len) |
1349 | { | |
1350 | while (RTA_OK(rta, len)) { | |
1351 | if (rta->rta_type == type) | |
1352 | return rta; | |
1353 | rta = RTA_NEXT(rta, len); | |
1354 | } | |
2c500a4d | 1355 | |
decbb437 | 1356 | if (len) |
2c500a4d SH |
1357 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", |
1358 | len, rta->rta_len); | |
decbb437 JP |
1359 | return NULL; |
1360 | } | |
1361 | ||
2c500a4d SH |
1362 | int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, |
1363 | struct rtattr *rta, | |
1364 | int len) | |
2f90c9c0 PM |
1365 | { |
1366 | if (RTA_PAYLOAD(rta) < len) | |
1367 | return -1; | |
1368 | if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) { | |
1369 | rta = RTA_DATA(rta) + RTA_ALIGN(len); | |
1370 | return parse_rtattr_nested(tb, max, rta); | |
1371 | } | |
037c635e | 1372 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); |
2f90c9c0 PM |
1373 | return 0; |
1374 | } |