2 * libnetlink.c RTnetlink service routines.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 #include <net/if_arp.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
27 #include "libnetlink.h"
30 #define SOL_NETLINK 270
34 #define MIN(a, b) ((a) < (b) ? (a) : (b))
37 int rcvbuf
= 1024 * 1024;
40 #include <libmnl/libmnl.h>
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
,
47 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
49 const struct nlattr
**tb
= data
;
52 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0)
55 type
= mnl_attr_get_type(attr
);
56 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0)
65 /* dump netlink extended ack error message */
66 static int nl_dump_ext_err(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
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
;
78 /* no TLVs, nothing to do here */
79 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
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
);
86 mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
);
88 if (tb
[NLMSGERR_ATTR_MSG
])
89 errmsg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
91 if (tb
[NLMSGERR_ATTR_OFFS
]) {
92 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
94 if (off
> nlh
->nlmsg_len
) {
96 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
98 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
102 return errfn(errmsg
, off
, err_nlh
);
105 /* No extended error ack without libmnl */
106 static int nl_dump_ext_err(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
112 void rtnl_close(struct rtnl_handle
*rth
)
120 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
127 memset(rth
, 0, sizeof(*rth
));
129 rth
->proto
= protocol
;
130 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
132 perror("Cannot open netlink socket");
136 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
137 &sndbuf
, sizeof(sndbuf
)) < 0) {
142 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
143 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
148 /* Older kernels may no support extended ACK reporting */
149 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
152 memset(&rth
->local
, 0, sizeof(rth
->local
));
153 rth
->local
.nl_family
= AF_NETLINK
;
154 rth
->local
.nl_groups
= subscriptions
;
156 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
157 sizeof(rth
->local
)) < 0) {
158 perror("Cannot bind netlink socket");
161 addr_len
= sizeof(rth
->local
);
162 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
164 perror("Cannot getsockname");
167 if (addr_len
!= sizeof(rth
->local
)) {
168 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
171 if (rth
->local
.nl_family
!= AF_NETLINK
) {
172 fprintf(stderr
, "Wrong address family %d\n",
173 rth
->local
.nl_family
);
176 rth
->seq
= time(NULL
);
180 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
182 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
185 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
187 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
190 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
195 struct ifinfomsg ifm
;
196 /* attribute has to be NLMSG aligned */
197 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
198 __u32 ext_filter_mask
;
200 .nlh
.nlmsg_len
= sizeof(req
),
201 .nlh
.nlmsg_type
= type
,
202 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
203 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
204 .ifm
.ifi_family
= family
,
205 .ext_req
.rta_type
= IFLA_EXT_MASK
,
206 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
207 .ext_filter_mask
= filt_mask
,
210 return send(rth
->fd
, &req
, sizeof(req
), 0);
213 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int family
, int type
,
214 req_filter_fn_t filter_fn
)
218 struct ifinfomsg ifm
;
221 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
222 .nlh
.nlmsg_type
= type
,
223 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
224 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
225 .ifm
.ifi_family
= family
,
232 err
= filter_fn(&req
.nlh
, sizeof(req
));
236 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
239 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
244 struct if_stats_msg ifsm
;
247 memset(&req
, 0, sizeof(req
));
248 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
249 req
.nlh
.nlmsg_type
= type
;
250 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
251 req
.nlh
.nlmsg_pid
= 0;
252 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
253 req
.ifsm
.family
= fam
;
254 req
.ifsm
.filter_mask
= filt_mask
;
256 return send(rth
->fd
, &req
, sizeof(req
), 0);
259 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
261 return send(rth
->fd
, buf
, len
, 0);
264 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
270 status
= send(rth
->fd
, buf
, len
, 0);
274 /* Check for immediate errors */
275 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
282 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
283 h
= NLMSG_NEXT(h
, status
)) {
284 if (h
->nlmsg_type
== NLMSG_ERROR
) {
285 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
287 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
288 fprintf(stderr
, "ERROR truncated\n");
298 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
300 struct nlmsghdr nlh
= {
301 .nlmsg_len
= NLMSG_LENGTH(len
),
303 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
304 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
306 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
307 struct iovec iov
[2] = {
308 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
309 { .iov_base
= req
, .iov_len
= len
}
311 struct msghdr msg
= {
313 .msg_namelen
= sizeof(nladdr
),
318 return sendmsg(rth
->fd
, &msg
, 0);
321 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
323 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
326 .iov_len
= n
->nlmsg_len
328 struct msghdr msg
= {
330 .msg_namelen
= sizeof(nladdr
),
335 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
337 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
339 return sendmsg(rth
->fd
, &msg
, 0);
342 static int rtnl_dump_done(const struct rtnl_handle
*rth
,
345 int len
= *(int *)NLMSG_DATA(h
);
347 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
348 fprintf(stderr
, "DONE truncated\n");
360 "Error: Buffer too small for object.\n");
363 perror("RTNETLINK answers");
371 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
375 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
376 fprintf(stderr
, "ERROR truncated\n");
378 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
381 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
383 errno
== EOPNOTSUPP
))
386 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
387 perror("RTNETLINK answers");
391 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
392 const struct rtnl_dump_filter_arg
*arg
)
394 struct sockaddr_nl nladdr
;
396 struct msghdr msg
= {
398 .msg_namelen
= sizeof(nladdr
),
408 const struct rtnl_dump_filter_arg
*a
;
412 iov
.iov_len
= sizeof(buf
);
413 status
= recvmsg(rth
->fd
, &msg
, 0);
416 if (errno
== EINTR
|| errno
== EAGAIN
)
418 fprintf(stderr
, "netlink receive error %s (%d)\n",
419 strerror(errno
), errno
);
424 fprintf(stderr
, "EOF on netlink\n");
429 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
431 for (a
= arg
; a
->filter
; a
++) {
432 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
436 while (NLMSG_OK(h
, msglen
)) {
439 h
->nlmsg_flags
&= ~a
->nc_flags
;
441 if (nladdr
.nl_pid
!= 0 ||
442 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
443 h
->nlmsg_seq
!= rth
->dump
)
446 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
449 if (h
->nlmsg_type
== NLMSG_DONE
) {
450 err
= rtnl_dump_done(rth
, h
);
455 break; /* process next filter */
458 if (h
->nlmsg_type
== NLMSG_ERROR
) {
459 rtnl_dump_error(rth
, h
);
464 err
= a
->filter(&nladdr
, h
, a
->arg1
);
470 h
= NLMSG_NEXT(h
, msglen
);
477 "Dump was interrupted and may be inconsistent.\n");
481 if (msg
.msg_flags
& MSG_TRUNC
) {
482 fprintf(stderr
, "Message truncated\n");
486 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
492 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
493 rtnl_filter_t filter
,
494 void *arg1
, __u16 nc_flags
)
496 const struct rtnl_dump_filter_arg a
[2] = {
497 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
498 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
501 return rtnl_dump_filter_l(rth
, a
);
504 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
505 nl_ext_ack_fn_t errfn
)
507 if (nl_dump_ext_err(h
, errfn
))
510 fprintf(stderr
, "RTNETLINK answers: %s\n",
511 strerror(-err
->error
));
514 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
515 struct nlmsghdr
*answer
, size_t maxlen
,
516 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
521 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
524 .iov_len
= n
->nlmsg_len
526 struct msghdr msg
= {
528 .msg_namelen
= sizeof(nladdr
),
532 char buf
[32768] = {};
534 n
->nlmsg_seq
= seq
= ++rtnl
->seq
;
537 n
->nlmsg_flags
|= NLM_F_ACK
;
539 status
= sendmsg(rtnl
->fd
, &msg
, 0);
541 perror("Cannot talk to rtnetlink");
547 iov
.iov_len
= sizeof(buf
);
548 status
= recvmsg(rtnl
->fd
, &msg
, 0);
551 if (errno
== EINTR
|| errno
== EAGAIN
)
553 fprintf(stderr
, "netlink receive error %s (%d)\n",
554 strerror(errno
), errno
);
558 fprintf(stderr
, "EOF on netlink\n");
561 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
563 "sender address length == %d\n",
567 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
568 int len
= h
->nlmsg_len
;
569 int l
= len
- sizeof(*h
);
571 if (l
< 0 || len
> status
) {
572 if (msg
.msg_flags
& MSG_TRUNC
) {
573 fprintf(stderr
, "Truncated message\n");
577 "!!!malformed message: len=%d\n",
582 if (nladdr
.nl_pid
!= 0 ||
583 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
584 h
->nlmsg_seq
!= seq
) {
585 /* Don't forget to skip that message. */
586 status
-= NLMSG_ALIGN(len
);
587 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
591 if (h
->nlmsg_type
== NLMSG_ERROR
) {
592 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
594 if (l
< sizeof(struct nlmsgerr
)) {
595 fprintf(stderr
, "ERROR truncated\n");
596 } else if (!err
->error
) {
599 MIN(maxlen
, h
->nlmsg_len
));
603 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
605 rtnl_talk_error(h
, err
, errfn
);
613 MIN(maxlen
, h
->nlmsg_len
));
617 fprintf(stderr
, "Unexpected reply!!!\n");
619 status
-= NLMSG_ALIGN(len
);
620 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
623 if (msg
.msg_flags
& MSG_TRUNC
) {
624 fprintf(stderr
, "Message truncated\n");
629 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
635 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
636 struct nlmsghdr
*answer
, size_t maxlen
)
638 return __rtnl_talk(rtnl
, n
, answer
, maxlen
, true, NULL
);
641 int rtnl_talk_extack(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
642 struct nlmsghdr
*answer
, size_t maxlen
,
643 nl_ext_ack_fn_t errfn
)
645 return __rtnl_talk(rtnl
, n
, answer
, maxlen
, true, errfn
);
648 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
649 struct nlmsghdr
*answer
, size_t maxlen
)
651 return __rtnl_talk(rtnl
, n
, answer
, maxlen
, false, NULL
);
654 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
658 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
660 perror("NETLINK_LISTEN_ALL_NSID");
663 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
667 int rtnl_listen(struct rtnl_handle
*rtnl
,
668 rtnl_listen_filter_t handler
,
673 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
675 struct msghdr msg
= {
677 .msg_namelen
= sizeof(nladdr
),
682 char cmsgbuf
[BUFSIZ
];
684 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
685 msg
.msg_control
= &cmsgbuf
;
686 msg
.msg_controllen
= sizeof(cmsgbuf
);
691 struct rtnl_ctrl_data ctrl
;
692 struct cmsghdr
*cmsg
;
694 iov
.iov_len
= sizeof(buf
);
695 status
= recvmsg(rtnl
->fd
, &msg
, 0);
698 if (errno
== EINTR
|| errno
== EAGAIN
)
700 fprintf(stderr
, "netlink receive error %s (%d)\n",
701 strerror(errno
), errno
);
702 if (errno
== ENOBUFS
)
707 fprintf(stderr
, "EOF on netlink\n");
710 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
712 "Sender address length == %d\n",
717 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
718 memset(&ctrl
, 0, sizeof(ctrl
));
720 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
721 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
722 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
723 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
724 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
725 int *data
= (int *)CMSG_DATA(cmsg
);
731 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
733 int len
= h
->nlmsg_len
;
734 int l
= len
- sizeof(*h
);
736 if (l
< 0 || len
> status
) {
737 if (msg
.msg_flags
& MSG_TRUNC
) {
738 fprintf(stderr
, "Truncated message\n");
742 "!!!malformed message: len=%d\n",
747 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
751 status
-= NLMSG_ALIGN(len
);
752 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
754 if (msg
.msg_flags
& MSG_TRUNC
) {
755 fprintf(stderr
, "Message truncated\n");
759 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
765 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
769 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
771 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
777 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
782 perror("rtnl_from_file: fread");
789 l
= len
- sizeof(*h
);
791 if (l
< 0 || len
> sizeof(buf
)) {
792 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
797 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
800 perror("rtnl_from_file: fread");
804 fprintf(stderr
, "rtnl-from_file: truncated message\n");
808 err
= handler(&nladdr
, NULL
, h
, jarg
);
814 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
816 return addattr_l(n
, maxlen
, type
, NULL
, 0);
819 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
821 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
824 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
826 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
829 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
831 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
834 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
836 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
839 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
841 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
844 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
847 int len
= RTA_LENGTH(alen
);
850 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
852 "addattr_l ERROR: message exceeded bound of %d\n",
857 rta
->rta_type
= type
;
859 memcpy(RTA_DATA(rta
), data
, alen
);
860 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
864 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
866 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
868 "addraw_l ERROR: message exceeded bound of %d\n",
873 memcpy(NLMSG_TAIL(n
), data
, len
);
874 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
875 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
879 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
881 struct rtattr
*nest
= NLMSG_TAIL(n
);
883 addattr_l(n
, maxlen
, type
, NULL
, 0);
887 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
889 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
893 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
894 const void *data
, int len
)
896 struct rtattr
*start
= NLMSG_TAIL(n
);
898 addattr_l(n
, maxlen
, type
, data
, len
);
899 addattr_nest(n
, maxlen
, type
);
903 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
905 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
907 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
908 addattr_nest_end(n
, nest
);
912 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
914 int len
= RTA_LENGTH(4);
915 struct rtattr
*subrta
;
917 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
919 "rta_addattr32: Error! max allowed bound %d exceeded\n",
923 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
924 subrta
->rta_type
= type
;
925 subrta
->rta_len
= len
;
926 memcpy(RTA_DATA(subrta
), &data
, 4);
927 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
931 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
932 const void *data
, int alen
)
934 struct rtattr
*subrta
;
935 int len
= RTA_LENGTH(alen
);
937 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
939 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
943 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
944 subrta
->rta_type
= type
;
945 subrta
->rta_len
= len
;
946 memcpy(RTA_DATA(subrta
), data
, alen
);
947 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
951 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
953 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
956 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
958 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
961 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
963 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
966 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
968 struct rtattr
*nest
= RTA_TAIL(rta
);
970 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
975 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
977 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
982 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
984 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
987 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
988 int len
, unsigned short flags
)
992 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
993 while (RTA_OK(rta
, len
)) {
994 type
= rta
->rta_type
& ~flags
;
995 if ((type
<= max
) && (!tb
[type
]))
997 rta
= RTA_NEXT(rta
, len
);
1000 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1005 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
1006 struct rtattr
*rta
, int len
)
1010 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
1011 while (RTA_OK(rta
, len
)) {
1012 if (rta
->rta_type
<= max
&& i
< max
)
1014 rta
= RTA_NEXT(rta
, len
);
1017 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1022 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1024 while (RTA_OK(rta
, len
)) {
1025 if (rta
->rta_type
== type
)
1027 rta
= RTA_NEXT(rta
, len
);
1031 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1036 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1040 if (RTA_PAYLOAD(rta
) < len
)
1042 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1043 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1044 return parse_rtattr_nested(tb
, max
, rta
);
1046 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));