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