]> git.proxmox.com Git - mirror_iproute2.git/blame - lib/libnetlink.c
ss: output dctcp diag information
[mirror_iproute2.git] / lib / libnetlink.c
CommitLineData
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>
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
7f03191f
PM
28int rcvbuf = 1024 * 1024;
29
aba5acdf
SH
30void rtnl_close(struct rtnl_handle *rth)
31{
3bfa73ff
SH
32 if (rth->fd >= 0) {
33 close(rth->fd);
34 rth->fd = -1;
35 }
aba5acdf
SH
36}
37
8ed63ab1
SH
38int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
39 int protocol)
aba5acdf 40{
f332d169 41 socklen_t addr_len;
007d3a3e 42 int sndbuf = 32768;
aba5acdf 43
b16621ca 44 memset(rth, 0, sizeof(*rth));
aba5acdf 45
bcb9d403 46 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
aba5acdf
SH
47 if (rth->fd < 0) {
48 perror("Cannot open netlink socket");
49 return -1;
50 }
51
007d3a3e
SH
52 if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) {
53 perror("SO_SNDBUF");
54 return -1;
55 }
56
57 if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) {
58 perror("SO_RCVBUF");
59 return -1;
60 }
61
aba5acdf
SH
62 memset(&rth->local, 0, sizeof(rth->local));
63 rth->local.nl_family = AF_NETLINK;
64 rth->local.nl_groups = subscriptions;
65
66 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
67 perror("Cannot bind netlink socket");
68 return -1;
69 }
70 addr_len = sizeof(rth->local);
71 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
72 perror("Cannot getsockname");
73 return -1;
74 }
75 if (addr_len != sizeof(rth->local)) {
76 fprintf(stderr, "Wrong address length %d\n", addr_len);
77 return -1;
78 }
79 if (rth->local.nl_family != AF_NETLINK) {
80 fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family);
81 return -1;
82 }
83 rth->seq = time(NULL);
84 return 0;
85}
86
c7699875 87int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
88{
89 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
90}
91
aba5acdf 92int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
9eff0e5c
VY
93{
94 return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF);
95}
96
97int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int family, int type,
98 __u32 filt_mask)
aba5acdf
SH
99{
100 struct {
101 struct nlmsghdr nlh;
63338dca 102 struct ifinfomsg ifm;
257422f7
LJ
103 /* attribute has to be NLMSG aligned */
104 struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO)));
bd886ebb 105 __u32 ext_filter_mask;
aba5acdf 106 } req;
aba5acdf 107
8ed63ab1 108 memset(&req, 0, sizeof(req));
aba5acdf
SH
109 req.nlh.nlmsg_len = sizeof(req);
110 req.nlh.nlmsg_type = type;
aa38c3ee 111 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
aba5acdf
SH
112 req.nlh.nlmsg_pid = 0;
113 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
63338dca 114 req.ifm.ifi_family = family;
aba5acdf 115
bd886ebb
RG
116 req.ext_req.rta_type = IFLA_EXT_MASK;
117 req.ext_req.rta_len = RTA_LENGTH(sizeof(__u32));
9eff0e5c 118 req.ext_filter_mask = filt_mask;
bd886ebb 119
f31a37f7 120 return send(rth->fd, (void*)&req, sizeof(req), 0);
aba5acdf
SH
121}
122
6cf8398f 123int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
aba5acdf 124{
f31a37f7
SH
125 return send(rth->fd, buf, len, 0);
126}
127
6cf8398f 128int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
f31a37f7 129{
54bb35c6
SH
130 struct nlmsghdr *h;
131 int status;
132 char resp[1024];
aba5acdf 133
f31a37f7 134 status = send(rth->fd, buf, len, 0);
54bb35c6
SH
135 if (status < 0)
136 return status;
137
2d8240f8
SH
138 /* Check for immediate errors */
139 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
54bb35c6
SH
140 if (status < 0) {
141 if (errno == EAGAIN)
142 return 0;
143 return -1;
144 }
145
146 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
147 h = NLMSG_NEXT(h, status)) {
148 if (h->nlmsg_type == NLMSG_ERROR) {
149 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
150 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
151 fprintf(stderr, "ERROR truncated\n");
152 else
153 errno = -err->error;
24f38182 154 return -1;
54bb35c6 155 }
54bb35c6
SH
156 }
157
158 return 0;
aba5acdf
SH
159}
160
161int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
162{
163 struct nlmsghdr nlh;
6cf8398f 164 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
8ed63ab1
SH
165 struct iovec iov[2] = {
166 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
167 { .iov_base = req, .iov_len = len }
168 };
aba5acdf 169 struct msghdr msg = {
8ed63ab1
SH
170 .msg_name = &nladdr,
171 .msg_namelen = sizeof(nladdr),
172 .msg_iov = iov,
173 .msg_iovlen = 2,
aba5acdf
SH
174 };
175
aba5acdf
SH
176 nlh.nlmsg_len = NLMSG_LENGTH(len);
177 nlh.nlmsg_type = type;
aa38c3ee 178 nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
aba5acdf
SH
179 nlh.nlmsg_pid = 0;
180 nlh.nlmsg_seq = rth->dump = ++rth->seq;
181
182 return sendmsg(rth->fd, &msg, 0);
183}
184
b49240ec
SH
185int rtnl_dump_filter_l(struct rtnl_handle *rth,
186 const struct rtnl_dump_filter_arg *arg)
aba5acdf 187{
aba5acdf 188 struct sockaddr_nl nladdr;
8ed63ab1
SH
189 struct iovec iov;
190 struct msghdr msg = {
191 .msg_name = &nladdr,
192 .msg_namelen = sizeof(nladdr),
193 .msg_iov = &iov,
194 .msg_iovlen = 1,
195 };
196 char buf[16384];
16f02e14 197 int dump_intr = 0;
aba5acdf 198
8ed63ab1 199 iov.iov_base = buf;
aba5acdf
SH
200 while (1) {
201 int status;
b49240ec 202 const struct rtnl_dump_filter_arg *a;
3bc1c4f2
BG
203 int found_done = 0;
204 int msglen = 0;
aba5acdf 205
8ed63ab1 206 iov.iov_len = sizeof(buf);
aba5acdf
SH
207 status = recvmsg(rth->fd, &msg, 0);
208
209 if (status < 0) {
aa8032e6 210 if (errno == EINTR || errno == EAGAIN)
aba5acdf 211 continue;
aa8032e6
SH
212 fprintf(stderr, "netlink receive error %s (%d)\n",
213 strerror(errno), errno);
214 return -1;
aba5acdf 215 }
8ed63ab1 216
aba5acdf
SH
217 if (status == 0) {
218 fprintf(stderr, "EOF on netlink\n");
219 return -1;
220 }
aba5acdf 221
b49240ec
SH
222 for (a = arg; a->filter; a++) {
223 struct nlmsghdr *h = (struct nlmsghdr*)buf;
3bc1c4f2 224 msglen = status;
b49240ec 225
3bc1c4f2 226 while (NLMSG_OK(h, msglen)) {
b49240ec
SH
227 int err;
228
229 if (nladdr.nl_pid != 0 ||
230 h->nlmsg_pid != rth->local.nl_pid ||
cd70f3f5 231 h->nlmsg_seq != rth->dump)
b49240ec 232 goto skip_it;
aba5acdf 233
16f02e14
ND
234 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
235 dump_intr = 1;
236
3bc1c4f2
BG
237 if (h->nlmsg_type == NLMSG_DONE) {
238 found_done = 1;
239 break; /* process next filter */
240 }
b49240ec
SH
241 if (h->nlmsg_type == NLMSG_ERROR) {
242 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
243 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
244 fprintf(stderr,
245 "ERROR truncated\n");
246 } else {
247 errno = -err->error;
248 perror("RTNETLINK answers");
249 }
250 return -1;
aba5acdf 251 }
b49240ec
SH
252 err = a->filter(&nladdr, h, a->arg1);
253 if (err < 0)
254 return err;
aba5acdf
SH
255
256skip_it:
3bc1c4f2 257 h = NLMSG_NEXT(h, msglen);
b49240ec 258 }
3bc1c4f2
BG
259 }
260
16f02e14
ND
261 if (found_done) {
262 if (dump_intr)
263 fprintf(stderr,
264 "Dump was interrupted and may be inconsistent.\n");
3bc1c4f2 265 return 0;
16f02e14 266 }
3bc1c4f2 267
aba5acdf
SH
268 if (msg.msg_flags & MSG_TRUNC) {
269 fprintf(stderr, "Message truncated\n");
270 continue;
271 }
3bc1c4f2
BG
272 if (msglen) {
273 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
aba5acdf
SH
274 exit(1);
275 }
276 }
277}
278
b49240ec
SH
279int rtnl_dump_filter(struct rtnl_handle *rth,
280 rtnl_filter_t filter,
cd70f3f5 281 void *arg1)
b49240ec
SH
282{
283 const struct rtnl_dump_filter_arg a[2] = {
cd70f3f5
SH
284 { .filter = filter, .arg1 = arg1, },
285 { .filter = NULL, .arg1 = NULL, },
b49240ec
SH
286 };
287
288 return rtnl_dump_filter_l(rth, a);
289}
290
aba5acdf 291int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
cd70f3f5 292 unsigned groups, struct nlmsghdr *answer)
aba5acdf
SH
293{
294 int status;
295 unsigned seq;
296 struct nlmsghdr *h;
297 struct sockaddr_nl nladdr;
fb229759
SH
298 struct iovec iov = {
299 .iov_base = (void*) n,
300 .iov_len = n->nlmsg_len
301 };
aba5acdf 302 struct msghdr msg = {
8ed63ab1
SH
303 .msg_name = &nladdr,
304 .msg_namelen = sizeof(nladdr),
305 .msg_iov = &iov,
306 .msg_iovlen = 1,
aba5acdf 307 };
8ed63ab1 308 char buf[16384];
aba5acdf
SH
309
310 memset(&nladdr, 0, sizeof(nladdr));
311 nladdr.nl_family = AF_NETLINK;
312 nladdr.nl_pid = peer;
313 nladdr.nl_groups = groups;
314
315 n->nlmsg_seq = seq = ++rtnl->seq;
007d3a3e 316
aba5acdf
SH
317 if (answer == NULL)
318 n->nlmsg_flags |= NLM_F_ACK;
319
320 status = sendmsg(rtnl->fd, &msg, 0);
321
322 if (status < 0) {
323 perror("Cannot talk to rtnetlink");
324 return -1;
325 }
326
007d3a3e
SH
327 memset(buf,0,sizeof(buf));
328
aba5acdf
SH
329 iov.iov_base = buf;
330
331 while (1) {
332 iov.iov_len = sizeof(buf);
333 status = recvmsg(rtnl->fd, &msg, 0);
334
335 if (status < 0) {
aa8032e6 336 if (errno == EINTR || errno == EAGAIN)
aba5acdf 337 continue;
aa8032e6
SH
338 fprintf(stderr, "netlink receive error %s (%d)\n",
339 strerror(errno), errno);
340 return -1;
aba5acdf
SH
341 }
342 if (status == 0) {
343 fprintf(stderr, "EOF on netlink\n");
344 return -1;
345 }
346 if (msg.msg_namelen != sizeof(nladdr)) {
347 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
348 exit(1);
349 }
350 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
aba5acdf
SH
351 int len = h->nlmsg_len;
352 int l = len - sizeof(*h);
353
cd70f3f5 354 if (l < 0 || len>status) {
aba5acdf
SH
355 if (msg.msg_flags & MSG_TRUNC) {
356 fprintf(stderr, "Truncated message\n");
357 return -1;
358 }
359 fprintf(stderr, "!!!malformed message: len=%d\n", len);
360 exit(1);
361 }
362
10f57ef1 363 if (nladdr.nl_pid != peer ||
364 h->nlmsg_pid != rtnl->local.nl_pid ||
aba5acdf 365 h->nlmsg_seq != seq) {
4cca16f2
SH
366 /* Don't forget to skip that message. */
367 status -= NLMSG_ALIGN(len);
368 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
aba5acdf
SH
369 continue;
370 }
371
372 if (h->nlmsg_type == NLMSG_ERROR) {
373 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
374 if (l < sizeof(struct nlmsgerr)) {
375 fprintf(stderr, "ERROR truncated\n");
376 } else {
b8cf1e9a 377 if (!err->error) {
aba5acdf
SH
378 if (answer)
379 memcpy(answer, h, h->nlmsg_len);
380 return 0;
381 }
b8cf1e9a
PE
382
383 fprintf(stderr, "RTNETLINK answers: %s\n", strerror(-err->error));
384 errno = -err->error;
aba5acdf
SH
385 }
386 return -1;
387 }
388 if (answer) {
389 memcpy(answer, h, h->nlmsg_len);
390 return 0;
391 }
392
393 fprintf(stderr, "Unexpected reply!!!\n");
394
395 status -= NLMSG_ALIGN(len);
396 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
397 }
398 if (msg.msg_flags & MSG_TRUNC) {
399 fprintf(stderr, "Message truncated\n");
400 continue;
401 }
402 if (status) {
403 fprintf(stderr, "!!!Remnant of size %d\n", status);
404 exit(1);
405 }
406 }
407}
408
8ed63ab1 409int rtnl_listen(struct rtnl_handle *rtnl,
6dc9f016
SH
410 rtnl_filter_t handler,
411 void *jarg)
aba5acdf
SH
412{
413 int status;
414 struct nlmsghdr *h;
415 struct sockaddr_nl nladdr;
416 struct iovec iov;
aba5acdf 417 struct msghdr msg = {
8ed63ab1
SH
418 .msg_name = &nladdr,
419 .msg_namelen = sizeof(nladdr),
420 .msg_iov = &iov,
421 .msg_iovlen = 1,
aba5acdf 422 };
8ed63ab1 423 char buf[8192];
aba5acdf
SH
424
425 memset(&nladdr, 0, sizeof(nladdr));
426 nladdr.nl_family = AF_NETLINK;
427 nladdr.nl_pid = 0;
428 nladdr.nl_groups = 0;
429
aba5acdf 430 iov.iov_base = buf;
aba5acdf
SH
431 while (1) {
432 iov.iov_len = sizeof(buf);
433 status = recvmsg(rtnl->fd, &msg, 0);
434
435 if (status < 0) {
aa8032e6 436 if (errno == EINTR || errno == EAGAIN)
aba5acdf 437 continue;
aa8032e6
SH
438 fprintf(stderr, "netlink receive error %s (%d)\n",
439 strerror(errno), errno);
7f03191f
PM
440 if (errno == ENOBUFS)
441 continue;
aa8032e6 442 return -1;
aba5acdf
SH
443 }
444 if (status == 0) {
445 fprintf(stderr, "EOF on netlink\n");
446 return -1;
447 }
448 if (msg.msg_namelen != sizeof(nladdr)) {
449 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
450 exit(1);
451 }
452 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
453 int err;
454 int len = h->nlmsg_len;
455 int l = len - sizeof(*h);
456
457 if (l<0 || len>status) {
458 if (msg.msg_flags & MSG_TRUNC) {
459 fprintf(stderr, "Truncated message\n");
460 return -1;
461 }
462 fprintf(stderr, "!!!malformed message: len=%d\n", len);
463 exit(1);
464 }
465
466 err = handler(&nladdr, h, jarg);
467 if (err < 0)
468 return err;
469
470 status -= NLMSG_ALIGN(len);
471 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
472 }
473 if (msg.msg_flags & MSG_TRUNC) {
474 fprintf(stderr, "Message truncated\n");
475 continue;
476 }
477 if (status) {
478 fprintf(stderr, "!!!Remnant of size %d\n", status);
479 exit(1);
480 }
481 }
482}
483
6dc9f016
SH
484int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
485 void *jarg)
aba5acdf
SH
486{
487 int status;
488 struct sockaddr_nl nladdr;
489 char buf[8192];
490 struct nlmsghdr *h = (void*)buf;
491
492 memset(&nladdr, 0, sizeof(nladdr));
493 nladdr.nl_family = AF_NETLINK;
494 nladdr.nl_pid = 0;
495 nladdr.nl_groups = 0;
496
497 while (1) {
2dd9f8e0 498 int err, len;
aba5acdf
SH
499 int l;
500
501 status = fread(&buf, 1, sizeof(*h), rtnl);
502
503 if (status < 0) {
504 if (errno == EINTR)
505 continue;
506 perror("rtnl_from_file: fread");
507 return -1;
508 }
509 if (status == 0)
510 return 0;
511
512 len = h->nlmsg_len;
aba5acdf
SH
513 l = len - sizeof(*h);
514
515 if (l<0 || len>sizeof(buf)) {
516 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
517 len, ftell(rtnl));
518 return -1;
519 }
520
521 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
522
523 if (status < 0) {
524 perror("rtnl_from_file: fread");
525 return -1;
526 }
527 if (status < l) {
528 fprintf(stderr, "rtnl-from_file: truncated message\n");
529 return -1;
530 }
531
532 err = handler(&nladdr, h, jarg);
533 if (err < 0)
534 return err;
535 }
536}
537
2aa3dd29
SH
538int addattr(struct nlmsghdr *n, int maxlen, int type)
539{
540 return addattr_l(n, maxlen, type, NULL, 0);
541}
542
543int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
544{
545 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
546}
547
548int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
549{
550 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
551}
552
aba5acdf
SH
553int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
554{
2aa3dd29
SH
555 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
556}
557
558int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
559{
560 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
561}
562
563int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
564{
565 return addattr_l(n, maxlen, type, str, strlen(str)+1);
aba5acdf
SH
566}
567
8ed63ab1 568int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
6dc9f016 569 int alen)
aba5acdf
SH
570{
571 int len = RTA_LENGTH(alen);
572 struct rtattr *rta;
573
3dabdbb3 574 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
007d3a3e 575 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
aba5acdf 576 return -1;
007d3a3e 577 }
07f94362 578 rta = NLMSG_TAIL(n);
aba5acdf
SH
579 rta->rta_type = type;
580 rta->rta_len = len;
581 memcpy(RTA_DATA(rta), data, alen);
3dabdbb3 582 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
aba5acdf
SH
583 return 0;
584}
585
07f94362 586int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
587{
588 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
589 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
590 return -1;
591 }
592
593 memcpy(NLMSG_TAIL(n), data, len);
594 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
595 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
596 return 0;
597}
598
2f90c9c0
PM
599struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
600{
601 struct rtattr *nest = NLMSG_TAIL(n);
602
603 addattr_l(n, maxlen, type, NULL, 0);
604 return nest;
605}
606
607int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
608{
609 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
610 return n->nlmsg_len;
611}
612
613struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
614 const void *data, int len)
615{
616 struct rtattr *start = NLMSG_TAIL(n);
617
618 addattr_l(n, maxlen, type, data, len);
619 addattr_nest(n, maxlen, type);
620 return start;
621}
622
623int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
624{
625 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
626
627 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
628 addattr_nest_end(n, nest);
629 return n->nlmsg_len;
630}
631
aba5acdf
SH
632int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
633{
634 int len = RTA_LENGTH(4);
635 struct rtattr *subrta;
636
007d3a3e
SH
637 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
638 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
aba5acdf 639 return -1;
007d3a3e 640 }
aba5acdf
SH
641 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
642 subrta->rta_type = type;
643 subrta->rta_len = len;
644 memcpy(RTA_DATA(subrta), &data, 4);
645 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
646 return 0;
647}
648
8ed63ab1 649int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
6dc9f016 650 const void *data, int alen)
aba5acdf
SH
651{
652 struct rtattr *subrta;
653 int len = RTA_LENGTH(alen);
654
3dabdbb3 655 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
007d3a3e 656 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
aba5acdf 657 return -1;
007d3a3e 658 }
aba5acdf
SH
659 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
660 subrta->rta_type = type;
661 subrta->rta_len = len;
662 memcpy(RTA_DATA(subrta), data, alen);
3dabdbb3 663 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
aba5acdf
SH
664 return 0;
665}
666
aba5acdf
SH
667int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
668{
b1b7ce0f
VY
669 return parse_rtattr_flags(tb, max, rta, len, 0);
670}
671
672int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
673 int len, unsigned short flags)
674{
675 unsigned short type;
676
175e2440 677 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
aba5acdf 678 while (RTA_OK(rta, len)) {
b1b7ce0f
VY
679 type = rta->rta_type & ~flags;
680 if ((type <= max) && (!tb[type]))
681 tb[type] = rta;
aba5acdf
SH
682 rta = RTA_NEXT(rta,len);
683 }
684 if (len)
685 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
686 return 0;
687}
c7699875 688
689int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
690{
691 int i = 0;
175e2440
SH
692
693 memset(tb, 0, sizeof(struct rtattr *) * max);
c7699875 694 while (RTA_OK(rta, len)) {
175e2440 695 if (rta->rta_type <= max && i < max)
c7699875 696 tb[i++] = rta;
697 rta = RTA_NEXT(rta,len);
698 }
699 if (len)
700 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
701 return i;
702}
2f90c9c0
PM
703
704int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
705 int len)
706{
707 if (RTA_PAYLOAD(rta) < len)
708 return -1;
709 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
710 rta = RTA_DATA(rta) + RTA_ALIGN(len);
711 return parse_rtattr_nested(tb, max, rta);
712 }
037c635e 713 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
2f90c9c0
PM
714 return 0;
715}