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