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) {
53 fprintf(stderr
, "Invalid extack attribute\n");
57 type
= mnl_attr_get_type(attr
);
58 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
59 fprintf(stderr
, "extack attribute %d failed validation\n",
68 /* dump netlink extended ack error message */
69 static int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
71 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
72 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
73 const struct nlmsghdr
*err_nlh
= NULL
;
74 unsigned int hlen
= sizeof(*err
);
75 const char *msg
= 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 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
89 if (tb
[NLMSGERR_ATTR_MSG
])
90 msg
= mnl_attr_get_str(tb
[NLMSGERR_ATTR_MSG
]);
92 if (tb
[NLMSGERR_ATTR_OFFS
]) {
93 off
= mnl_attr_get_u32(tb
[NLMSGERR_ATTR_OFFS
]);
95 if (off
> nlh
->nlmsg_len
) {
97 "Invalid offset for NLMSGERR_ATTR_OFFS\n");
99 } else if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
104 return errfn(msg
, off
, err_nlh
);
106 if (msg
&& *msg
!= '\0') {
107 bool is_err
= !!err
->error
;
109 fprintf(stderr
, "%s: %s",
110 is_err
? "Error" : "Warning", msg
);
111 if (msg
[strlen(msg
) - 1] != '.')
112 fprintf(stderr
, ".");
113 fprintf(stderr
, "\n");
115 return is_err
? 1 : 0;
121 #warning "libmnl required for error support"
123 /* No extended error ack without libmnl */
124 static int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
130 void rtnl_close(struct rtnl_handle
*rth
)
138 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
145 memset(rth
, 0, sizeof(*rth
));
147 rth
->proto
= protocol
;
148 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
150 perror("Cannot open netlink socket");
154 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
155 &sndbuf
, sizeof(sndbuf
)) < 0) {
160 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
161 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
166 /* Older kernels may no support extended ACK reporting */
167 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
170 memset(&rth
->local
, 0, sizeof(rth
->local
));
171 rth
->local
.nl_family
= AF_NETLINK
;
172 rth
->local
.nl_groups
= subscriptions
;
174 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
175 sizeof(rth
->local
)) < 0) {
176 perror("Cannot bind netlink socket");
179 addr_len
= sizeof(rth
->local
);
180 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
182 perror("Cannot getsockname");
185 if (addr_len
!= sizeof(rth
->local
)) {
186 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
189 if (rth
->local
.nl_family
!= AF_NETLINK
) {
190 fprintf(stderr
, "Wrong address family %d\n",
191 rth
->local
.nl_family
);
194 rth
->seq
= time(NULL
);
198 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
200 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
203 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
205 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
208 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
213 struct ifinfomsg ifm
;
214 /* attribute has to be NLMSG aligned */
215 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
216 __u32 ext_filter_mask
;
218 .nlh
.nlmsg_len
= sizeof(req
),
219 .nlh
.nlmsg_type
= type
,
220 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
221 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
222 .ifm
.ifi_family
= family
,
223 .ext_req
.rta_type
= IFLA_EXT_MASK
,
224 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
225 .ext_filter_mask
= filt_mask
,
228 return send(rth
->fd
, &req
, sizeof(req
), 0);
231 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int family
, int type
,
232 req_filter_fn_t filter_fn
)
236 struct ifinfomsg ifm
;
239 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
240 .nlh
.nlmsg_type
= type
,
241 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
242 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
243 .ifm
.ifi_family
= family
,
250 err
= filter_fn(&req
.nlh
, sizeof(req
));
254 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
257 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
262 struct if_stats_msg ifsm
;
265 memset(&req
, 0, sizeof(req
));
266 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
267 req
.nlh
.nlmsg_type
= type
;
268 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
269 req
.nlh
.nlmsg_pid
= 0;
270 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
271 req
.ifsm
.family
= fam
;
272 req
.ifsm
.filter_mask
= filt_mask
;
274 return send(rth
->fd
, &req
, sizeof(req
), 0);
277 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
279 return send(rth
->fd
, buf
, len
, 0);
282 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
288 status
= send(rth
->fd
, buf
, len
, 0);
292 /* Check for immediate errors */
293 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
300 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
301 h
= NLMSG_NEXT(h
, status
)) {
302 if (h
->nlmsg_type
== NLMSG_ERROR
) {
303 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
305 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
306 fprintf(stderr
, "ERROR truncated\n");
316 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
318 struct nlmsghdr nlh
= {
319 .nlmsg_len
= NLMSG_LENGTH(len
),
321 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
322 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
324 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
325 struct iovec iov
[2] = {
326 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
327 { .iov_base
= req
, .iov_len
= len
}
329 struct msghdr msg
= {
331 .msg_namelen
= sizeof(nladdr
),
336 return sendmsg(rth
->fd
, &msg
, 0);
339 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
341 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
344 .iov_len
= n
->nlmsg_len
346 struct msghdr msg
= {
348 .msg_namelen
= sizeof(nladdr
),
353 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
355 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
357 return sendmsg(rth
->fd
, &msg
, 0);
360 static int rtnl_dump_done(struct nlmsghdr
*h
)
362 int len
= *(int *)NLMSG_DATA(h
);
364 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
365 fprintf(stderr
, "DONE truncated\n");
377 "Error: Buffer too small for object.\n");
380 perror("RTNETLINK answers");
385 /* check for any messages returned from kernel */
386 nl_dump_ext_ack(h
, NULL
);
391 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
395 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
396 fprintf(stderr
, "ERROR truncated\n");
398 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
401 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
403 errno
== EOPNOTSUPP
))
406 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
407 perror("RTNETLINK answers");
411 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
416 len
= recvmsg(fd
, msg
, flags
);
417 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
420 fprintf(stderr
, "netlink receive error %s (%d)\n",
421 strerror(errno
), errno
);
426 fprintf(stderr
, "EOF on netlink\n");
433 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
435 struct iovec
*iov
= msg
->msg_iov
;
439 iov
->iov_base
= NULL
;
442 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
448 fprintf(stderr
, "malloc error: not enough buffer\n");
455 len
= __rtnl_recvmsg(fd
, msg
, 0);
469 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
470 const struct rtnl_dump_filter_arg
*arg
)
472 struct sockaddr_nl nladdr
;
474 struct msghdr msg
= {
476 .msg_namelen
= sizeof(nladdr
),
485 const struct rtnl_dump_filter_arg
*a
;
489 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
494 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
496 for (a
= arg
; a
->filter
; a
++) {
497 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
501 while (NLMSG_OK(h
, msglen
)) {
504 h
->nlmsg_flags
&= ~a
->nc_flags
;
506 if (nladdr
.nl_pid
!= 0 ||
507 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
508 h
->nlmsg_seq
!= rth
->dump
)
511 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
514 if (h
->nlmsg_type
== NLMSG_DONE
) {
515 err
= rtnl_dump_done(h
);
522 break; /* process next filter */
525 if (h
->nlmsg_type
== NLMSG_ERROR
) {
526 rtnl_dump_error(rth
, h
);
532 err
= a
->filter(&nladdr
, h
, a
->arg1
);
540 h
= NLMSG_NEXT(h
, msglen
);
548 "Dump was interrupted and may be inconsistent.\n");
552 if (msg
.msg_flags
& MSG_TRUNC
) {
553 fprintf(stderr
, "Message truncated\n");
557 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
563 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
564 rtnl_filter_t filter
,
565 void *arg1
, __u16 nc_flags
)
567 const struct rtnl_dump_filter_arg a
[2] = {
568 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
569 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
572 return rtnl_dump_filter_l(rth
, a
);
575 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
576 nl_ext_ack_fn_t errfn
)
578 if (nl_dump_ext_ack(h
, errfn
))
581 fprintf(stderr
, "RTNETLINK answers: %s\n",
582 strerror(-err
->error
));
585 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
586 struct nlmsghdr
**answer
,
587 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
592 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
595 .iov_len
= n
->nlmsg_len
597 struct msghdr msg
= {
599 .msg_namelen
= sizeof(nladdr
),
605 n
->nlmsg_seq
= seq
= ++rtnl
->seq
;
608 n
->nlmsg_flags
|= NLM_F_ACK
;
610 status
= sendmsg(rtnl
->fd
, &msg
, 0);
612 perror("Cannot talk to rtnetlink");
617 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
622 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
624 "sender address length == %d\n",
628 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
629 int len
= h
->nlmsg_len
;
630 int l
= len
- sizeof(*h
);
632 if (l
< 0 || len
> status
) {
633 if (msg
.msg_flags
& MSG_TRUNC
) {
634 fprintf(stderr
, "Truncated message\n");
639 "!!!malformed message: len=%d\n",
644 if (nladdr
.nl_pid
!= 0 ||
645 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
646 h
->nlmsg_seq
!= seq
) {
647 /* Don't forget to skip that message. */
648 status
-= NLMSG_ALIGN(len
);
649 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
653 if (h
->nlmsg_type
== NLMSG_ERROR
) {
654 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
656 if (l
< sizeof(struct nlmsgerr
)) {
657 fprintf(stderr
, "ERROR truncated\n");
658 } else if (!err
->error
) {
659 /* check messages from kernel */
660 nl_dump_ext_ack(h
, errfn
);
663 *answer
= (struct nlmsghdr
*)buf
;
669 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
671 rtnl_talk_error(h
, err
, errfn
);
679 *answer
= (struct nlmsghdr
*)buf
;
683 fprintf(stderr
, "Unexpected reply!!!\n");
685 status
-= NLMSG_ALIGN(len
);
686 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
690 if (msg
.msg_flags
& MSG_TRUNC
) {
691 fprintf(stderr
, "Message truncated\n");
696 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
702 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
703 struct nlmsghdr
**answer
)
705 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
708 int rtnl_talk_extack(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
709 struct nlmsghdr
**answer
,
710 nl_ext_ack_fn_t errfn
)
712 return __rtnl_talk(rtnl
, n
, answer
, true, errfn
);
715 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
716 struct nlmsghdr
**answer
)
718 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
721 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
725 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
727 perror("NETLINK_LISTEN_ALL_NSID");
730 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
734 int rtnl_listen(struct rtnl_handle
*rtnl
,
735 rtnl_listen_filter_t handler
,
740 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
742 struct msghdr msg
= {
744 .msg_namelen
= sizeof(nladdr
),
749 char cmsgbuf
[BUFSIZ
];
751 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
752 msg
.msg_control
= &cmsgbuf
;
753 msg
.msg_controllen
= sizeof(cmsgbuf
);
758 struct rtnl_ctrl_data ctrl
;
759 struct cmsghdr
*cmsg
;
761 iov
.iov_len
= sizeof(buf
);
762 status
= recvmsg(rtnl
->fd
, &msg
, 0);
765 if (errno
== EINTR
|| errno
== EAGAIN
)
767 fprintf(stderr
, "netlink receive error %s (%d)\n",
768 strerror(errno
), errno
);
769 if (errno
== ENOBUFS
)
774 fprintf(stderr
, "EOF on netlink\n");
777 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
779 "Sender address length == %d\n",
784 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
785 memset(&ctrl
, 0, sizeof(ctrl
));
787 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
788 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
789 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
790 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
791 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
792 int *data
= (int *)CMSG_DATA(cmsg
);
798 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
800 int len
= h
->nlmsg_len
;
801 int l
= len
- sizeof(*h
);
803 if (l
< 0 || len
> status
) {
804 if (msg
.msg_flags
& MSG_TRUNC
) {
805 fprintf(stderr
, "Truncated message\n");
809 "!!!malformed message: len=%d\n",
814 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
818 status
-= NLMSG_ALIGN(len
);
819 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
821 if (msg
.msg_flags
& MSG_TRUNC
) {
822 fprintf(stderr
, "Message truncated\n");
826 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
832 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
836 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
838 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
844 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
849 perror("rtnl_from_file: fread");
856 l
= len
- sizeof(*h
);
858 if (l
< 0 || len
> sizeof(buf
)) {
859 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
864 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
867 perror("rtnl_from_file: fread");
871 fprintf(stderr
, "rtnl-from_file: truncated message\n");
875 err
= handler(&nladdr
, NULL
, h
, jarg
);
881 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
883 return addattr_l(n
, maxlen
, type
, NULL
, 0);
886 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
888 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
891 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
893 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
896 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
898 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
901 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
903 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
906 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
908 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
911 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
914 int len
= RTA_LENGTH(alen
);
917 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
919 "addattr_l ERROR: message exceeded bound of %d\n",
924 rta
->rta_type
= type
;
927 memcpy(RTA_DATA(rta
), data
, alen
);
928 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
932 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
934 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
936 "addraw_l ERROR: message exceeded bound of %d\n",
941 memcpy(NLMSG_TAIL(n
), data
, len
);
942 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
943 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
947 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
949 struct rtattr
*nest
= NLMSG_TAIL(n
);
951 addattr_l(n
, maxlen
, type
, NULL
, 0);
955 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
957 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
961 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
962 const void *data
, int len
)
964 struct rtattr
*start
= NLMSG_TAIL(n
);
966 addattr_l(n
, maxlen
, type
, data
, len
);
967 addattr_nest(n
, maxlen
, type
);
971 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
973 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
975 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
976 addattr_nest_end(n
, nest
);
980 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
982 int len
= RTA_LENGTH(4);
983 struct rtattr
*subrta
;
985 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
987 "rta_addattr32: Error! max allowed bound %d exceeded\n",
991 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
992 subrta
->rta_type
= type
;
993 subrta
->rta_len
= len
;
994 memcpy(RTA_DATA(subrta
), &data
, 4);
995 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
999 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1000 const void *data
, int alen
)
1002 struct rtattr
*subrta
;
1003 int len
= RTA_LENGTH(alen
);
1005 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1007 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1011 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1012 subrta
->rta_type
= type
;
1013 subrta
->rta_len
= len
;
1015 memcpy(RTA_DATA(subrta
), data
, alen
);
1016 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1020 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1022 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1025 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1027 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1030 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1032 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1035 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1037 struct rtattr
*nest
= RTA_TAIL(rta
);
1039 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1044 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1046 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1048 return rta
->rta_len
;
1051 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1053 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1056 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1057 int len
, unsigned short flags
)
1059 unsigned short type
;
1061 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1062 while (RTA_OK(rta
, len
)) {
1063 type
= rta
->rta_type
& ~flags
;
1064 if ((type
<= max
) && (!tb
[type
]))
1066 rta
= RTA_NEXT(rta
, len
);
1069 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1074 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
1075 struct rtattr
*rta
, int len
)
1079 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
1080 while (RTA_OK(rta
, len
)) {
1081 if (rta
->rta_type
<= max
&& i
< max
)
1083 rta
= RTA_NEXT(rta
, len
);
1086 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1091 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1093 while (RTA_OK(rta
, len
)) {
1094 if (rta
->rta_type
== type
)
1096 rta
= RTA_NEXT(rta
, len
);
1100 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1105 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1109 if (RTA_PAYLOAD(rta
) < len
)
1111 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1112 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1113 return parse_rtattr_nested(tb
, max
, rta
);
1115 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));