]>
Commit | Line | Data |
---|---|---|
1e6d2d09 VD |
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> | |
15 | #include <unistd.h> | |
16 | #include <syslog.h> | |
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 | ||
28 | void rtnl_close(struct rtnl_handle *rth) | |
29 | { | |
30 | close(rth->fd); | |
31 | } | |
32 | ||
33 | int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions, | |
34 | int protocol) | |
35 | { | |
36 | socklen_t addr_len; | |
37 | int sndbuf = 32768; | |
38 | int rcvbuf = 32768; | |
39 | ||
40 | memset(rth, 0, sizeof(*rth)); | |
41 | ||
42 | rth->fd = socket(AF_NETLINK, SOCK_RAW, protocol); | |
43 | if (rth->fd < 0) { | |
44 | perror("Cannot open netlink socket"); | |
45 | return -1; | |
46 | } | |
47 | ||
48 | if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) | |
49 | < 0) { | |
50 | perror("SO_SNDBUF"); | |
51 | return -1; | |
52 | } | |
53 | ||
54 | if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) | |
55 | < 0) { | |
56 | perror("SO_RCVBUF"); | |
57 | return -1; | |
58 | } | |
59 | ||
60 | memset(&rth->local, 0, sizeof(rth->local)); | |
61 | rth->local.nl_family = AF_NETLINK; | |
62 | rth->local.nl_groups = subscriptions; | |
63 | ||
64 | if (bind(rth->fd, (struct sockaddr *)&rth->local, sizeof(rth->local)) < | |
65 | 0) { | |
66 | perror("Cannot bind netlink socket"); | |
67 | return -1; | |
68 | } | |
69 | addr_len = sizeof(rth->local); | |
70 | if (getsockname(rth->fd, (struct sockaddr *)&rth->local, &addr_len) < 0) { | |
71 | perror("Cannot getsockname"); | |
72 | return -1; | |
73 | } | |
74 | if (addr_len != sizeof(rth->local)) { | |
75 | fprintf(stderr, "Wrong address length %d\n", addr_len); | |
76 | return -1; | |
77 | } | |
78 | if (rth->local.nl_family != AF_NETLINK) { | |
79 | fprintf(stderr, "Wrong address family %d\n", | |
80 | rth->local.nl_family); | |
81 | return -1; | |
82 | } | |
83 | rth->seq = time(NULL); | |
84 | return 0; | |
85 | } | |
86 | ||
87 | int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions) | |
88 | { | |
89 | return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE); | |
90 | } | |
91 | ||
92 | int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type) | |
93 | { | |
94 | struct { | |
95 | struct nlmsghdr nlh; | |
96 | struct rtgenmsg g; | |
97 | } req; | |
98 | struct sockaddr_nl nladdr; | |
99 | ||
100 | memset(&nladdr, 0, sizeof(nladdr)); | |
101 | nladdr.nl_family = AF_NETLINK; | |
102 | ||
103 | memset(&req, 0, sizeof(req)); | |
104 | req.nlh.nlmsg_len = sizeof(req); | |
105 | req.nlh.nlmsg_type = type; | |
106 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; | |
107 | req.nlh.nlmsg_pid = 0; | |
108 | req.nlh.nlmsg_seq = rth->dump = ++rth->seq; | |
109 | req.g.rtgen_family = family; | |
110 | ||
111 | return sendto(rth->fd, (void *)&req, sizeof(req), 0, | |
112 | (struct sockaddr *)&nladdr, sizeof(nladdr)); | |
113 | } | |
114 | ||
115 | int rtnl_send(struct rtnl_handle *rth, const char *buf, int len) | |
116 | { | |
117 | struct sockaddr_nl nladdr; | |
118 | ||
119 | memset(&nladdr, 0, sizeof(nladdr)); | |
120 | nladdr.nl_family = AF_NETLINK; | |
121 | ||
122 | return sendto(rth->fd, buf, len, 0, (struct sockaddr *)&nladdr, | |
123 | sizeof(nladdr)); | |
124 | } | |
125 | ||
126 | int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len) | |
127 | { | |
128 | struct nlmsghdr nlh; | |
129 | struct sockaddr_nl nladdr; | |
130 | struct iovec iov[2] = { | |
131 | {.iov_base = &nlh,.iov_len = sizeof(nlh)} | |
132 | , | |
133 | {.iov_base = req,.iov_len = len} | |
134 | }; | |
135 | struct msghdr msg = { | |
136 | .msg_name = &nladdr, | |
137 | .msg_namelen = sizeof(nladdr), | |
138 | .msg_iov = iov, | |
139 | .msg_iovlen = 2, | |
140 | }; | |
141 | ||
142 | memset(&nladdr, 0, sizeof(nladdr)); | |
143 | nladdr.nl_family = AF_NETLINK; | |
144 | ||
145 | nlh.nlmsg_len = NLMSG_LENGTH(len); | |
146 | nlh.nlmsg_type = type; | |
147 | nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; | |
148 | nlh.nlmsg_pid = 0; | |
149 | nlh.nlmsg_seq = rth->dump = ++rth->seq; | |
150 | ||
151 | return sendmsg(rth->fd, &msg, 0); | |
152 | } | |
153 | ||
154 | int rtnl_dump_filter(struct rtnl_handle *rth, | |
155 | rtnl_filter_t filter, | |
156 | void *arg1, rtnl_filter_t junk, void *arg2) | |
157 | { | |
158 | struct sockaddr_nl nladdr; | |
159 | struct iovec iov; | |
160 | struct msghdr msg = { | |
161 | .msg_name = &nladdr, | |
162 | .msg_namelen = sizeof(nladdr), | |
163 | .msg_iov = &iov, | |
164 | .msg_iovlen = 1, | |
165 | }; | |
166 | char buf[16384]; | |
167 | ||
168 | iov.iov_base = buf; | |
169 | while (1) { | |
170 | int status; | |
171 | struct nlmsghdr *h; | |
172 | ||
173 | iov.iov_len = sizeof(buf); | |
174 | status = recvmsg(rth->fd, &msg, 0); | |
175 | ||
176 | if (status < 0) { | |
177 | if (errno == EINTR) | |
178 | continue; | |
179 | perror("OVERRUN"); | |
180 | continue; | |
181 | } | |
182 | ||
183 | if (status == 0) { | |
184 | fprintf(stderr, "EOF on netlink\n"); | |
185 | return -1; | |
186 | } | |
187 | ||
188 | h = (struct nlmsghdr *)buf; | |
189 | while (NLMSG_OK(h, status)) { | |
190 | int err; | |
191 | ||
192 | if (nladdr.nl_pid != 0 || | |
193 | h->nlmsg_pid != rth->local.nl_pid || | |
194 | h->nlmsg_seq != rth->dump) { | |
195 | if (junk) { | |
196 | err = junk(&nladdr, h, arg2); | |
197 | if (err < 0) | |
198 | return err; | |
199 | } | |
200 | goto skip_it; | |
201 | } | |
202 | ||
203 | if (h->nlmsg_type == NLMSG_DONE) | |
204 | return 0; | |
205 | if (h->nlmsg_type == NLMSG_ERROR) { | |
206 | struct nlmsgerr *err = | |
207 | (struct nlmsgerr *)NLMSG_DATA(h); | |
208 | if (h->nlmsg_len < | |
209 | NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
210 | fprintf(stderr, "ERROR truncated\n"); | |
211 | } else { | |
212 | errno = -err->error; | |
213 | perror("RTNETLINK answers"); | |
214 | } | |
215 | return -1; | |
216 | } | |
217 | err = filter(&nladdr, h, arg1); | |
218 | if (err < 0) | |
219 | return err; | |
220 | ||
221 | skip_it: | |
222 | h = NLMSG_NEXT(h, status); | |
223 | } | |
224 | if (msg.msg_flags & MSG_TRUNC) { | |
225 | fprintf(stderr, "Message truncated\n"); | |
226 | continue; | |
227 | } | |
228 | if (status) { | |
229 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
230 | return -1; | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, | |
236 | unsigned groups, struct nlmsghdr *answer, | |
237 | rtnl_filter_t junk, void *jarg) | |
238 | { | |
239 | int status; | |
240 | unsigned seq; | |
241 | struct nlmsghdr *h; | |
242 | struct sockaddr_nl nladdr; | |
243 | struct iovec iov = { | |
244 | .iov_base = (void *)n, | |
245 | .iov_len = n->nlmsg_len | |
246 | }; | |
247 | struct msghdr msg = { | |
248 | .msg_name = &nladdr, | |
249 | .msg_namelen = sizeof(nladdr), | |
250 | .msg_iov = &iov, | |
251 | .msg_iovlen = 1, | |
252 | }; | |
253 | char buf[16384]; | |
254 | ||
255 | memset(&nladdr, 0, sizeof(nladdr)); | |
256 | nladdr.nl_family = AF_NETLINK; | |
257 | nladdr.nl_pid = peer; | |
258 | nladdr.nl_groups = groups; | |
259 | ||
260 | n->nlmsg_seq = seq = ++rtnl->seq; | |
261 | ||
262 | if (answer == NULL) | |
263 | n->nlmsg_flags |= NLM_F_ACK; | |
264 | ||
265 | status = sendmsg(rtnl->fd, &msg, 0); | |
266 | ||
267 | if (status < 0) { | |
268 | perror("Cannot talk to rtnetlink"); | |
269 | return -1; | |
270 | } | |
271 | ||
272 | memset(buf, 0, sizeof(buf)); | |
273 | ||
274 | iov.iov_base = buf; | |
275 | ||
276 | while (1) { | |
277 | iov.iov_len = sizeof(buf); | |
278 | status = recvmsg(rtnl->fd, &msg, 0); | |
279 | ||
280 | if (status < 0) { | |
281 | if (errno == EINTR) | |
282 | continue; | |
283 | perror("OVERRUN"); | |
284 | continue; | |
285 | } | |
286 | if (status == 0) { | |
287 | fprintf(stderr, "EOF on netlink\n"); | |
288 | return -1; | |
289 | } | |
290 | if (msg.msg_namelen != sizeof(nladdr)) { | |
291 | fprintf(stderr, "sender address length == %d\n", | |
292 | msg.msg_namelen); | |
293 | return -1; | |
294 | } | |
295 | for (h = (struct nlmsghdr *)buf; status >= sizeof(*h);) { | |
296 | int err; | |
297 | int len = h->nlmsg_len; | |
298 | int l = len - sizeof(*h); | |
299 | ||
300 | if (l < 0 || len > status) { | |
301 | if (msg.msg_flags & MSG_TRUNC) { | |
302 | fprintf(stderr, "Truncated message\n"); | |
303 | return -1; | |
304 | } | |
305 | fprintf(stderr, | |
306 | "!!!malformed message: len=%d\n", len); | |
307 | return -1; | |
308 | } | |
309 | ||
310 | if (nladdr.nl_pid != peer || | |
311 | h->nlmsg_pid != rtnl->local.nl_pid || | |
312 | h->nlmsg_seq != seq) { | |
313 | if (junk) { | |
314 | err = junk(&nladdr, h, jarg); | |
315 | if (err < 0) | |
316 | return err; | |
317 | } | |
318 | /* Don't forget to skip that message. */ | |
319 | status -= NLMSG_ALIGN(len); | |
320 | h = (struct nlmsghdr *)((char *)h + | |
321 | NLMSG_ALIGN(len)); | |
322 | continue; | |
323 | } | |
324 | ||
325 | if (h->nlmsg_type == NLMSG_ERROR) { | |
326 | struct nlmsgerr *err = | |
327 | (struct nlmsgerr *)NLMSG_DATA(h); | |
328 | if (l < sizeof(struct nlmsgerr)) { | |
329 | fprintf(stderr, "ERROR truncated\n"); | |
330 | } else { | |
331 | errno = -err->error; | |
332 | if (errno == 0) { | |
333 | if (answer) | |
334 | memcpy(answer, h, | |
335 | h->nlmsg_len); | |
336 | return 0; | |
337 | } | |
338 | perror("RTNETLINK answers"); | |
339 | } | |
340 | return -1; | |
341 | } | |
342 | if (answer) { | |
343 | memcpy(answer, h, h->nlmsg_len); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | fprintf(stderr, "Unexpected reply!!!\n"); | |
348 | ||
349 | status -= NLMSG_ALIGN(len); | |
350 | h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); | |
351 | } | |
352 | if (msg.msg_flags & MSG_TRUNC) { | |
353 | fprintf(stderr, "Message truncated\n"); | |
354 | continue; | |
355 | } | |
356 | if (status) { | |
357 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
358 | return -1; | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | int rtnl_listen(struct rtnl_handle *rtnl, rtnl_filter_t handler, void *jarg) | |
364 | { | |
365 | int status; | |
366 | struct nlmsghdr *h; | |
367 | struct sockaddr_nl nladdr; | |
368 | struct iovec iov; | |
369 | struct msghdr msg = { | |
370 | .msg_name = &nladdr, | |
371 | .msg_namelen = sizeof(nladdr), | |
372 | .msg_iov = &iov, | |
373 | .msg_iovlen = 1, | |
374 | }; | |
375 | char buf[8192]; | |
376 | ||
377 | memset(&nladdr, 0, sizeof(nladdr)); | |
378 | nladdr.nl_family = AF_NETLINK; | |
379 | nladdr.nl_pid = 0; | |
380 | nladdr.nl_groups = 0; | |
381 | ||
382 | iov.iov_base = buf; | |
383 | while (1) { | |
384 | iov.iov_len = sizeof(buf); | |
385 | status = recvmsg(rtnl->fd, &msg, 0); | |
386 | ||
387 | if (status < 0) { | |
388 | if (errno == EINTR) | |
389 | continue; | |
390 | if (errno == EAGAIN) | |
391 | return 0; | |
392 | perror("OVERRUN"); | |
393 | return -1; | |
394 | } | |
395 | if (status == 0) { | |
396 | fprintf(stderr, "EOF on netlink\n"); | |
397 | return -1; | |
398 | } | |
399 | if (msg.msg_namelen != sizeof(nladdr)) { | |
400 | fprintf(stderr, "Sender address length == %d\n", | |
401 | msg.msg_namelen); | |
402 | return -1; | |
403 | } | |
404 | for (h = (struct nlmsghdr *)buf; status >= sizeof(*h);) { | |
405 | int err; | |
406 | int len = h->nlmsg_len; | |
407 | int l = len - sizeof(*h); | |
408 | ||
409 | if (l < 0 || len > status) { | |
410 | if (msg.msg_flags & MSG_TRUNC) { | |
411 | fprintf(stderr, "Truncated message\n"); | |
412 | return -1; | |
413 | } | |
414 | fprintf(stderr, | |
415 | "!!!malformed message: len=%d\n", len); | |
416 | return -1; | |
417 | } | |
418 | ||
419 | err = handler(&nladdr, h, jarg); | |
420 | if (err < 0) { | |
421 | fprintf(stderr, "Handler returned %d\n", err); | |
422 | return err; | |
423 | } | |
424 | ||
425 | status -= NLMSG_ALIGN(len); | |
426 | h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); | |
427 | } | |
428 | if (msg.msg_flags & MSG_TRUNC) { | |
429 | fprintf(stderr, "Message truncated\n"); | |
430 | continue; | |
431 | } | |
432 | if (status) { | |
433 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
434 | return -1; | |
435 | } | |
436 | } | |
437 | } | |
438 | ||
439 | int rtnl_from_file(FILE * rtnl, rtnl_filter_t handler, void *jarg) | |
440 | { | |
441 | int status; | |
442 | struct sockaddr_nl nladdr; | |
443 | char buf[8192]; | |
444 | struct nlmsghdr *h = (void *)buf; | |
445 | ||
446 | memset(&nladdr, 0, sizeof(nladdr)); | |
447 | nladdr.nl_family = AF_NETLINK; | |
448 | nladdr.nl_pid = 0; | |
449 | nladdr.nl_groups = 0; | |
450 | ||
451 | while (1) { | |
452 | int err, len, type; | |
453 | int l; | |
454 | ||
455 | status = fread(&buf, 1, sizeof(*h), rtnl); | |
456 | ||
457 | if (status < 0) { | |
458 | if (errno == EINTR) | |
459 | continue; | |
460 | perror("rtnl_from_file: fread"); | |
461 | return -1; | |
462 | } | |
463 | if (status == 0) | |
464 | return 0; | |
465 | ||
466 | len = h->nlmsg_len; | |
467 | type = h->nlmsg_type; | |
468 | l = len - sizeof(*h); | |
469 | ||
470 | if (l < 0 || len > sizeof(buf)) { | |
471 | fprintf(stderr, "!!!malformed message: len=%d @%lu\n", | |
472 | len, ftell(rtnl)); | |
473 | return -1; | |
474 | } | |
475 | ||
476 | status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl); | |
477 | ||
478 | if (status < 0) { | |
479 | perror("rtnl_from_file: fread"); | |
480 | return -1; | |
481 | } | |
482 | if (status < l) { | |
483 | fprintf(stderr, "rtnl-from_file: truncated message\n"); | |
484 | return -1; | |
485 | } | |
486 | ||
487 | err = handler(&nladdr, h, jarg); | |
488 | if (err < 0) | |
489 | return err; | |
490 | } | |
491 | } | |
492 | ||
493 | int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data) | |
494 | { | |
495 | int len = RTA_LENGTH(4); | |
496 | struct rtattr *rta; | |
497 | if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) { | |
498 | fprintf(stderr, | |
499 | "addattr32: Error! max allowed bound %d exceeded\n", | |
500 | maxlen); | |
501 | return -1; | |
502 | } | |
503 | rta = NLMSG_TAIL(n); | |
504 | rta->rta_type = type; | |
505 | rta->rta_len = len; | |
506 | memcpy(RTA_DATA(rta), &data, 4); | |
507 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len; | |
508 | return 0; | |
509 | } | |
510 | ||
511 | int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data) | |
512 | { | |
513 | int len = RTA_LENGTH(1); | |
514 | struct rtattr *rta; | |
515 | if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) { | |
516 | fprintf(stderr, | |
517 | "addattr8: Error! max allowed bound %d exceeded\n", | |
518 | maxlen); | |
519 | return -1; | |
520 | } | |
521 | rta = NLMSG_TAIL(n); | |
522 | rta->rta_type = type; | |
523 | rta->rta_len = len; | |
524 | memcpy(RTA_DATA(rta), &data, 1); | |
525 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len; | |
526 | return 0; | |
527 | } | |
528 | ||
529 | int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, | |
530 | int alen) | |
531 | { | |
532 | int len = RTA_LENGTH(alen); | |
533 | struct rtattr *rta; | |
534 | ||
535 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { | |
536 | fprintf(stderr, | |
537 | "addattr_l ERROR: message exceeded bound of %d\n", | |
538 | maxlen); | |
539 | return -1; | |
540 | } | |
541 | rta = NLMSG_TAIL(n); | |
542 | rta->rta_type = type; | |
543 | rta->rta_len = len; | |
544 | memcpy(RTA_DATA(rta), data, alen); | |
545 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); | |
546 | return 0; | |
547 | } | |
548 | ||
549 | int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len) | |
550 | { | |
551 | if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) { | |
552 | fprintf(stderr, | |
553 | "addraw_l ERROR: message exceeded bound of %d\n", | |
554 | maxlen); | |
555 | return -1; | |
556 | } | |
557 | ||
558 | memcpy(NLMSG_TAIL(n), data, len); | |
559 | memset((void *)NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len); | |
560 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len); | |
561 | return 0; | |
562 | } | |
563 | ||
564 | int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data) | |
565 | { | |
566 | int len = RTA_LENGTH(4); | |
567 | struct rtattr *subrta; | |
568 | ||
569 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) { | |
570 | fprintf(stderr, | |
571 | "rta_addattr32: Error! max allowed bound %d exceeded\n", | |
572 | maxlen); | |
573 | return -1; | |
574 | } | |
575 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); | |
576 | subrta->rta_type = type; | |
577 | subrta->rta_len = len; | |
578 | memcpy(RTA_DATA(subrta), &data, 4); | |
579 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len; | |
580 | return 0; | |
581 | } | |
582 | ||
583 | int rta_addattr_l(struct rtattr *rta, int maxlen, int type, | |
584 | const void *data, int alen) | |
585 | { | |
586 | struct rtattr *subrta; | |
587 | int len = RTA_LENGTH(alen); | |
588 | ||
589 | if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) { | |
590 | fprintf(stderr, | |
591 | "rta_addattr_l: Error! max allowed bound %d exceeded\n", | |
592 | maxlen); | |
593 | return -1; | |
594 | } | |
595 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); | |
596 | subrta->rta_type = type; | |
597 | subrta->rta_len = len; | |
598 | memcpy(RTA_DATA(subrta), data, alen); | |
599 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); | |
600 | return 0; | |
601 | } | |
602 | ||
603 | int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) | |
604 | { | |
605 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); | |
606 | while (RTA_OK(rta, len)) { | |
607 | if (rta->rta_type <= max) | |
608 | tb[rta->rta_type] = rta; | |
609 | rta = RTA_NEXT(rta, len); | |
610 | } | |
611 | if (len) | |
612 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, | |
613 | rta->rta_len); | |
614 | return 0; | |
615 | } | |
616 | ||
617 | int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, | |
618 | int len) | |
619 | { | |
620 | int i = 0; | |
621 | ||
622 | memset(tb, 0, sizeof(struct rtattr *) * max); | |
623 | while (RTA_OK(rta, len)) { | |
624 | if (rta->rta_type <= max && i < max) | |
625 | tb[i++] = rta; | |
626 | rta = RTA_NEXT(rta, len); | |
627 | } | |
628 | if (len) | |
629 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, | |
630 | rta->rta_len); | |
631 | return i; | |
632 | } |