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 #warning "libmnl required for error support"
107 /* No extended error ack without libmnl */
108 static int nl_dump_ext_err(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
114 void rtnl_close(struct rtnl_handle
*rth
)
122 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
129 memset(rth
, 0, sizeof(*rth
));
131 rth
->proto
= protocol
;
132 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
134 perror("Cannot open netlink socket");
138 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
139 &sndbuf
, sizeof(sndbuf
)) < 0) {
144 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
145 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
150 /* Older kernels may no support extended ACK reporting */
151 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
154 memset(&rth
->local
, 0, sizeof(rth
->local
));
155 rth
->local
.nl_family
= AF_NETLINK
;
156 rth
->local
.nl_groups
= subscriptions
;
158 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
159 sizeof(rth
->local
)) < 0) {
160 perror("Cannot bind netlink socket");
163 addr_len
= sizeof(rth
->local
);
164 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
166 perror("Cannot getsockname");
169 if (addr_len
!= sizeof(rth
->local
)) {
170 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
173 if (rth
->local
.nl_family
!= AF_NETLINK
) {
174 fprintf(stderr
, "Wrong address family %d\n",
175 rth
->local
.nl_family
);
178 rth
->seq
= time(NULL
);
182 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
184 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
187 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
189 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
192 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
197 struct ifinfomsg ifm
;
198 /* attribute has to be NLMSG aligned */
199 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
200 __u32 ext_filter_mask
;
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
,
212 return send(rth
->fd
, &req
, sizeof(req
), 0);
215 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int family
, int type
,
216 req_filter_fn_t filter_fn
)
220 struct ifinfomsg ifm
;
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
,
234 err
= filter_fn(&req
.nlh
, sizeof(req
));
238 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
241 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
246 struct if_stats_msg ifsm
;
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
;
258 return send(rth
->fd
, &req
, sizeof(req
), 0);
261 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
263 return send(rth
->fd
, buf
, len
, 0);
266 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
272 status
= send(rth
->fd
, buf
, len
, 0);
276 /* Check for immediate errors */
277 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
284 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
285 h
= NLMSG_NEXT(h
, status
)) {
286 if (h
->nlmsg_type
== NLMSG_ERROR
) {
287 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
289 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
290 fprintf(stderr
, "ERROR truncated\n");
300 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
302 struct nlmsghdr nlh
= {
303 .nlmsg_len
= NLMSG_LENGTH(len
),
305 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
306 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
308 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
309 struct iovec iov
[2] = {
310 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
311 { .iov_base
= req
, .iov_len
= len
}
313 struct msghdr msg
= {
315 .msg_namelen
= sizeof(nladdr
),
320 return sendmsg(rth
->fd
, &msg
, 0);
323 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
325 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
328 .iov_len
= n
->nlmsg_len
330 struct msghdr msg
= {
332 .msg_namelen
= sizeof(nladdr
),
337 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
339 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
341 return sendmsg(rth
->fd
, &msg
, 0);
344 static int rtnl_dump_done(const struct rtnl_handle
*rth
,
347 int len
= *(int *)NLMSG_DATA(h
);
349 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
350 fprintf(stderr
, "DONE truncated\n");
362 "Error: Buffer too small for object.\n");
365 perror("RTNETLINK answers");
373 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
377 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
378 fprintf(stderr
, "ERROR truncated\n");
380 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
383 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
385 errno
== EOPNOTSUPP
))
388 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
389 perror("RTNETLINK answers");
393 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
394 const struct rtnl_dump_filter_arg
*arg
)
396 struct sockaddr_nl nladdr
;
398 struct msghdr msg
= {
400 .msg_namelen
= sizeof(nladdr
),
410 const struct rtnl_dump_filter_arg
*a
;
414 iov
.iov_len
= sizeof(buf
);
415 status
= recvmsg(rth
->fd
, &msg
, 0);
418 if (errno
== EINTR
|| errno
== EAGAIN
)
420 fprintf(stderr
, "netlink receive error %s (%d)\n",
421 strerror(errno
), errno
);
426 fprintf(stderr
, "EOF on netlink\n");
431 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
433 for (a
= arg
; a
->filter
; a
++) {
434 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
438 while (NLMSG_OK(h
, msglen
)) {
441 h
->nlmsg_flags
&= ~a
->nc_flags
;
443 if (nladdr
.nl_pid
!= 0 ||
444 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
445 h
->nlmsg_seq
!= rth
->dump
)
448 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
451 if (h
->nlmsg_type
== NLMSG_DONE
) {
452 err
= rtnl_dump_done(rth
, h
);
457 break; /* process next filter */
460 if (h
->nlmsg_type
== NLMSG_ERROR
) {
461 rtnl_dump_error(rth
, h
);
466 err
= a
->filter(&nladdr
, h
, a
->arg1
);
472 h
= NLMSG_NEXT(h
, msglen
);
479 "Dump was interrupted and may be inconsistent.\n");
483 if (msg
.msg_flags
& MSG_TRUNC
) {
484 fprintf(stderr
, "Message truncated\n");
488 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
494 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
495 rtnl_filter_t filter
,
496 void *arg1
, __u16 nc_flags
)
498 const struct rtnl_dump_filter_arg a
[2] = {
499 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
500 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
503 return rtnl_dump_filter_l(rth
, a
);
506 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
507 nl_ext_ack_fn_t errfn
)
509 if (nl_dump_ext_err(h
, errfn
))
512 fprintf(stderr
, "RTNETLINK answers: %s\n",
513 strerror(-err
->error
));
516 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
517 struct nlmsghdr
*answer
, size_t maxlen
,
518 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
523 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
526 .iov_len
= n
->nlmsg_len
528 struct msghdr msg
= {
530 .msg_namelen
= sizeof(nladdr
),
534 char buf
[32768] = {};
536 n
->nlmsg_seq
= seq
= ++rtnl
->seq
;
539 n
->nlmsg_flags
|= NLM_F_ACK
;
541 status
= sendmsg(rtnl
->fd
, &msg
, 0);
543 perror("Cannot talk to rtnetlink");
549 iov
.iov_len
= sizeof(buf
);
550 status
= recvmsg(rtnl
->fd
, &msg
, 0);
553 if (errno
== EINTR
|| errno
== EAGAIN
)
555 fprintf(stderr
, "netlink receive error %s (%d)\n",
556 strerror(errno
), errno
);
560 fprintf(stderr
, "EOF on netlink\n");
563 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
565 "sender address length == %d\n",
569 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
570 int len
= h
->nlmsg_len
;
571 int l
= len
- sizeof(*h
);
573 if (l
< 0 || len
> status
) {
574 if (msg
.msg_flags
& MSG_TRUNC
) {
575 fprintf(stderr
, "Truncated message\n");
579 "!!!malformed message: len=%d\n",
584 if (nladdr
.nl_pid
!= 0 ||
585 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
586 h
->nlmsg_seq
!= seq
) {
587 /* Don't forget to skip that message. */
588 status
-= NLMSG_ALIGN(len
);
589 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
593 if (h
->nlmsg_type
== NLMSG_ERROR
) {
594 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
596 if (l
< sizeof(struct nlmsgerr
)) {
597 fprintf(stderr
, "ERROR truncated\n");
598 } else if (!err
->error
) {
601 MIN(maxlen
, h
->nlmsg_len
));
605 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
607 rtnl_talk_error(h
, err
, errfn
);
615 MIN(maxlen
, h
->nlmsg_len
));
619 fprintf(stderr
, "Unexpected reply!!!\n");
621 status
-= NLMSG_ALIGN(len
);
622 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
625 if (msg
.msg_flags
& MSG_TRUNC
) {
626 fprintf(stderr
, "Message truncated\n");
631 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
637 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
638 struct nlmsghdr
*answer
, size_t maxlen
)
640 return __rtnl_talk(rtnl
, n
, answer
, maxlen
, true, NULL
);
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
)
647 return __rtnl_talk(rtnl
, n
, answer
, maxlen
, true, errfn
);
650 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
651 struct nlmsghdr
*answer
, size_t maxlen
)
653 return __rtnl_talk(rtnl
, n
, answer
, maxlen
, false, NULL
);
656 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
660 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
662 perror("NETLINK_LISTEN_ALL_NSID");
665 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
669 int rtnl_listen(struct rtnl_handle
*rtnl
,
670 rtnl_listen_filter_t handler
,
675 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
677 struct msghdr msg
= {
679 .msg_namelen
= sizeof(nladdr
),
684 char cmsgbuf
[BUFSIZ
];
686 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
687 msg
.msg_control
= &cmsgbuf
;
688 msg
.msg_controllen
= sizeof(cmsgbuf
);
693 struct rtnl_ctrl_data ctrl
;
694 struct cmsghdr
*cmsg
;
696 iov
.iov_len
= sizeof(buf
);
697 status
= recvmsg(rtnl
->fd
, &msg
, 0);
700 if (errno
== EINTR
|| errno
== EAGAIN
)
702 fprintf(stderr
, "netlink receive error %s (%d)\n",
703 strerror(errno
), errno
);
704 if (errno
== ENOBUFS
)
709 fprintf(stderr
, "EOF on netlink\n");
712 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
714 "Sender address length == %d\n",
719 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
720 memset(&ctrl
, 0, sizeof(ctrl
));
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
);
733 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
735 int len
= h
->nlmsg_len
;
736 int l
= len
- sizeof(*h
);
738 if (l
< 0 || len
> status
) {
739 if (msg
.msg_flags
& MSG_TRUNC
) {
740 fprintf(stderr
, "Truncated message\n");
744 "!!!malformed message: len=%d\n",
749 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
753 status
-= NLMSG_ALIGN(len
);
754 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
756 if (msg
.msg_flags
& MSG_TRUNC
) {
757 fprintf(stderr
, "Message truncated\n");
761 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
767 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
771 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
773 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
779 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
784 perror("rtnl_from_file: fread");
791 l
= len
- sizeof(*h
);
793 if (l
< 0 || len
> sizeof(buf
)) {
794 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
799 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
802 perror("rtnl_from_file: fread");
806 fprintf(stderr
, "rtnl-from_file: truncated message\n");
810 err
= handler(&nladdr
, NULL
, h
, jarg
);
816 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
818 return addattr_l(n
, maxlen
, type
, NULL
, 0);
821 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
823 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
826 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
828 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
831 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
833 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
836 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
838 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
841 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
843 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
846 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
849 int len
= RTA_LENGTH(alen
);
852 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
854 "addattr_l ERROR: message exceeded bound of %d\n",
859 rta
->rta_type
= type
;
861 memcpy(RTA_DATA(rta
), data
, alen
);
862 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
866 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
868 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
870 "addraw_l ERROR: message exceeded bound of %d\n",
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
);
881 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
883 struct rtattr
*nest
= NLMSG_TAIL(n
);
885 addattr_l(n
, maxlen
, type
, NULL
, 0);
889 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
891 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
895 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
896 const void *data
, int len
)
898 struct rtattr
*start
= NLMSG_TAIL(n
);
900 addattr_l(n
, maxlen
, type
, data
, len
);
901 addattr_nest(n
, maxlen
, type
);
905 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
907 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
909 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
910 addattr_nest_end(n
, nest
);
914 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
916 int len
= RTA_LENGTH(4);
917 struct rtattr
*subrta
;
919 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
921 "rta_addattr32: Error! max allowed bound %d exceeded\n",
925 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
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
;
933 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
934 const void *data
, int alen
)
936 struct rtattr
*subrta
;
937 int len
= RTA_LENGTH(alen
);
939 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
941 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
945 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
946 subrta
->rta_type
= type
;
947 subrta
->rta_len
= len
;
948 memcpy(RTA_DATA(subrta
), data
, alen
);
949 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
953 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
955 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
958 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
960 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
963 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
965 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
968 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
970 struct rtattr
*nest
= RTA_TAIL(rta
);
972 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
977 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
979 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
984 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
986 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
989 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
990 int len
, unsigned short flags
)
994 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
995 while (RTA_OK(rta
, len
)) {
996 type
= rta
->rta_type
& ~flags
;
997 if ((type
<= max
) && (!tb
[type
]))
999 rta
= RTA_NEXT(rta
, len
);
1002 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1007 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
1008 struct rtattr
*rta
, int len
)
1012 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
1013 while (RTA_OK(rta
, len
)) {
1014 if (rta
->rta_type
<= max
&& i
< max
)
1016 rta
= RTA_NEXT(rta
, len
);
1019 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1024 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1026 while (RTA_OK(rta
, len
)) {
1027 if (rta
->rta_type
== type
)
1029 rta
= RTA_NEXT(rta
, len
);
1033 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1038 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1042 if (RTA_PAYLOAD(rta
) < len
)
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
);
1048 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));