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