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