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