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>
18 #include <net/if_arp.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
26 #include "libnetlink.h"
29 #define SOL_NETLINK 270
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
36 int rcvbuf
= 1024 * 1024;
39 #include <libmnl/libmnl.h>
41 static const enum mnl_attr_data_type extack_policy
[NLMSGERR_ATTR_MAX
+ 1] = {
42 [NLMSGERR_ATTR_MSG
] = MNL_TYPE_NUL_STRING
,
43 [NLMSGERR_ATTR_OFFS
] = MNL_TYPE_U32
,
46 static int err_attr_cb(const struct nlattr
*attr
, void *data
)
48 const struct nlattr
**tb
= data
;
51 if (mnl_attr_type_valid(attr
, NLMSGERR_ATTR_MAX
) < 0) {
52 fprintf(stderr
, "Invalid extack attribute\n");
56 type
= mnl_attr_get_type(attr
);
57 if (mnl_attr_validate(attr
, extack_policy
[type
]) < 0) {
58 fprintf(stderr
, "extack attribute %d failed validation\n",
67 /* dump netlink extended ack error message */
68 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
70 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
71 const struct nlmsgerr
*err
= mnl_nlmsg_get_payload(nlh
);
72 const struct nlmsghdr
*err_nlh
= NULL
;
73 unsigned int hlen
= sizeof(*err
);
74 const char *msg
= NULL
;
77 /* no TLVs, nothing to do here */
78 if (!(nlh
->nlmsg_flags
& NLM_F_ACK_TLVS
))
81 /* if NLM_F_CAPPED is set then the inner err msg was capped */
82 if (!(nlh
->nlmsg_flags
& NLM_F_CAPPED
))
83 hlen
+= mnl_nlmsg_get_payload_len(&err
->msg
);
85 if (mnl_attr_parse(nlh
, hlen
, err_attr_cb
, tb
) != MNL_CB_OK
)
88 if (tb
[NLMSGERR_ATTR_MSG
])
89 msg
= 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
))
103 return errfn(msg
, off
, err_nlh
);
105 if (msg
&& *msg
!= '\0') {
106 bool is_err
= !!err
->error
;
108 fprintf(stderr
, "%s: %s",
109 is_err
? "Error" : "Warning", msg
);
110 if (msg
[strlen(msg
) - 1] != '.')
111 fprintf(stderr
, ".");
112 fprintf(stderr
, "\n");
114 return is_err
? 1 : 0;
120 #warning "libmnl required for error support"
122 /* No extended error ack without libmnl */
123 int nl_dump_ext_ack(const struct nlmsghdr
*nlh
, nl_ext_ack_fn_t errfn
)
129 void rtnl_close(struct rtnl_handle
*rth
)
137 int rtnl_open_byproto(struct rtnl_handle
*rth
, unsigned int subscriptions
,
144 memset(rth
, 0, sizeof(*rth
));
146 rth
->proto
= protocol
;
147 rth
->fd
= socket(AF_NETLINK
, SOCK_RAW
| SOCK_CLOEXEC
, protocol
);
149 perror("Cannot open netlink socket");
153 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_SNDBUF
,
154 &sndbuf
, sizeof(sndbuf
)) < 0) {
159 if (setsockopt(rth
->fd
, SOL_SOCKET
, SO_RCVBUF
,
160 &rcvbuf
, sizeof(rcvbuf
)) < 0) {
165 /* Older kernels may no support extended ACK reporting */
166 setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_EXT_ACK
,
169 memset(&rth
->local
, 0, sizeof(rth
->local
));
170 rth
->local
.nl_family
= AF_NETLINK
;
171 rth
->local
.nl_groups
= subscriptions
;
173 if (bind(rth
->fd
, (struct sockaddr
*)&rth
->local
,
174 sizeof(rth
->local
)) < 0) {
175 perror("Cannot bind netlink socket");
178 addr_len
= sizeof(rth
->local
);
179 if (getsockname(rth
->fd
, (struct sockaddr
*)&rth
->local
,
181 perror("Cannot getsockname");
184 if (addr_len
!= sizeof(rth
->local
)) {
185 fprintf(stderr
, "Wrong address length %d\n", addr_len
);
188 if (rth
->local
.nl_family
!= AF_NETLINK
) {
189 fprintf(stderr
, "Wrong address family %d\n",
190 rth
->local
.nl_family
);
193 rth
->seq
= time(NULL
);
197 int rtnl_open(struct rtnl_handle
*rth
, unsigned int subscriptions
)
199 return rtnl_open_byproto(rth
, subscriptions
, NETLINK_ROUTE
);
202 int rtnl_wilddump_request(struct rtnl_handle
*rth
, int family
, int type
)
204 return rtnl_wilddump_req_filter(rth
, family
, type
, RTEXT_FILTER_VF
);
207 int rtnl_wilddump_req_filter(struct rtnl_handle
*rth
, int family
, int type
,
212 struct ifinfomsg ifm
;
213 /* attribute has to be NLMSG aligned */
214 struct rtattr ext_req
__attribute__ ((aligned(NLMSG_ALIGNTO
)));
215 __u32 ext_filter_mask
;
217 .nlh
.nlmsg_len
= sizeof(req
),
218 .nlh
.nlmsg_type
= type
,
219 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
220 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
221 .ifm
.ifi_family
= family
,
222 .ext_req
.rta_type
= IFLA_EXT_MASK
,
223 .ext_req
.rta_len
= RTA_LENGTH(sizeof(__u32
)),
224 .ext_filter_mask
= filt_mask
,
227 return send(rth
->fd
, &req
, sizeof(req
), 0);
230 int rtnl_wilddump_req_filter_fn(struct rtnl_handle
*rth
, int family
, int type
,
231 req_filter_fn_t filter_fn
)
235 struct ifinfomsg ifm
;
238 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
239 .nlh
.nlmsg_type
= type
,
240 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
241 .nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
,
242 .ifm
.ifi_family
= family
,
249 err
= filter_fn(&req
.nlh
, sizeof(req
));
253 return send(rth
->fd
, &req
, req
.nlh
.nlmsg_len
, 0);
256 int rtnl_wilddump_stats_req_filter(struct rtnl_handle
*rth
, int fam
, int type
,
261 struct if_stats_msg ifsm
;
264 memset(&req
, 0, sizeof(req
));
265 req
.nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct if_stats_msg
));
266 req
.nlh
.nlmsg_type
= type
;
267 req
.nlh
.nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
268 req
.nlh
.nlmsg_pid
= 0;
269 req
.nlh
.nlmsg_seq
= rth
->dump
= ++rth
->seq
;
270 req
.ifsm
.family
= fam
;
271 req
.ifsm
.filter_mask
= filt_mask
;
273 return send(rth
->fd
, &req
, sizeof(req
), 0);
276 int rtnl_send(struct rtnl_handle
*rth
, const void *buf
, int len
)
278 return send(rth
->fd
, buf
, len
, 0);
281 int rtnl_send_check(struct rtnl_handle
*rth
, const void *buf
, int len
)
287 status
= send(rth
->fd
, buf
, len
, 0);
291 /* Check for immediate errors */
292 status
= recv(rth
->fd
, resp
, sizeof(resp
), MSG_DONTWAIT
|MSG_PEEK
);
299 for (h
= (struct nlmsghdr
*)resp
; NLMSG_OK(h
, status
);
300 h
= NLMSG_NEXT(h
, status
)) {
301 if (h
->nlmsg_type
== NLMSG_ERROR
) {
302 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
304 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
)))
305 fprintf(stderr
, "ERROR truncated\n");
315 int rtnl_dump_request(struct rtnl_handle
*rth
, int type
, void *req
, int len
)
317 struct nlmsghdr nlh
= {
318 .nlmsg_len
= NLMSG_LENGTH(len
),
320 .nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
321 .nlmsg_seq
= rth
->dump
= ++rth
->seq
,
323 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
324 struct iovec iov
[2] = {
325 { .iov_base
= &nlh
, .iov_len
= sizeof(nlh
) },
326 { .iov_base
= req
, .iov_len
= len
}
328 struct msghdr msg
= {
330 .msg_namelen
= sizeof(nladdr
),
335 return sendmsg(rth
->fd
, &msg
, 0);
338 int rtnl_dump_request_n(struct rtnl_handle
*rth
, struct nlmsghdr
*n
)
340 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
343 .iov_len
= n
->nlmsg_len
345 struct msghdr msg
= {
347 .msg_namelen
= sizeof(nladdr
),
352 n
->nlmsg_flags
= NLM_F_DUMP
|NLM_F_REQUEST
;
354 n
->nlmsg_seq
= rth
->dump
= ++rth
->seq
;
356 return sendmsg(rth
->fd
, &msg
, 0);
359 static int rtnl_dump_done(struct nlmsghdr
*h
)
361 int len
= *(int *)NLMSG_DATA(h
);
363 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(int))) {
364 fprintf(stderr
, "DONE truncated\n");
376 "Error: Buffer too small for object.\n");
379 perror("RTNETLINK answers");
384 /* check for any messages returned from kernel */
385 nl_dump_ext_ack(h
, NULL
);
390 static void rtnl_dump_error(const struct rtnl_handle
*rth
,
394 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
395 fprintf(stderr
, "ERROR truncated\n");
397 const struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
400 if (rth
->proto
== NETLINK_SOCK_DIAG
&&
402 errno
== EOPNOTSUPP
))
405 if (!(rth
->flags
& RTNL_HANDLE_F_SUPPRESS_NLERR
))
406 perror("RTNETLINK answers");
410 static int __rtnl_recvmsg(int fd
, struct msghdr
*msg
, int flags
)
415 len
= recvmsg(fd
, msg
, flags
);
416 } while (len
< 0 && (errno
== EINTR
|| errno
== EAGAIN
));
419 fprintf(stderr
, "netlink receive error %s (%d)\n",
420 strerror(errno
), errno
);
425 fprintf(stderr
, "EOF on netlink\n");
432 static int rtnl_recvmsg(int fd
, struct msghdr
*msg
, char **answer
)
434 struct iovec
*iov
= msg
->msg_iov
;
438 iov
->iov_base
= NULL
;
441 len
= __rtnl_recvmsg(fd
, msg
, MSG_PEEK
| MSG_TRUNC
);
447 fprintf(stderr
, "malloc error: not enough buffer\n");
454 len
= __rtnl_recvmsg(fd
, msg
, 0);
468 int rtnl_dump_filter_l(struct rtnl_handle
*rth
,
469 const struct rtnl_dump_filter_arg
*arg
)
471 struct sockaddr_nl nladdr
;
473 struct msghdr msg
= {
475 .msg_namelen
= sizeof(nladdr
),
484 const struct rtnl_dump_filter_arg
*a
;
488 status
= rtnl_recvmsg(rth
->fd
, &msg
, &buf
);
493 fwrite(buf
, 1, NLMSG_ALIGN(status
), rth
->dump_fp
);
495 for (a
= arg
; a
->filter
; a
++) {
496 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
500 while (NLMSG_OK(h
, msglen
)) {
503 h
->nlmsg_flags
&= ~a
->nc_flags
;
505 if (nladdr
.nl_pid
!= 0 ||
506 h
->nlmsg_pid
!= rth
->local
.nl_pid
||
507 h
->nlmsg_seq
!= rth
->dump
)
510 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
513 if (h
->nlmsg_type
== NLMSG_DONE
) {
514 err
= rtnl_dump_done(h
);
521 break; /* process next filter */
524 if (h
->nlmsg_type
== NLMSG_ERROR
) {
525 rtnl_dump_error(rth
, h
);
531 err
= a
->filter(&nladdr
, h
, a
->arg1
);
539 h
= NLMSG_NEXT(h
, msglen
);
547 "Dump was interrupted and may be inconsistent.\n");
551 if (msg
.msg_flags
& MSG_TRUNC
) {
552 fprintf(stderr
, "Message truncated\n");
556 fprintf(stderr
, "!!!Remnant of size %d\n", msglen
);
562 int rtnl_dump_filter_nc(struct rtnl_handle
*rth
,
563 rtnl_filter_t filter
,
564 void *arg1
, __u16 nc_flags
)
566 const struct rtnl_dump_filter_arg a
[2] = {
567 { .filter
= filter
, .arg1
= arg1
, .nc_flags
= nc_flags
, },
568 { .filter
= NULL
, .arg1
= NULL
, .nc_flags
= 0, },
571 return rtnl_dump_filter_l(rth
, a
);
574 static void rtnl_talk_error(struct nlmsghdr
*h
, struct nlmsgerr
*err
,
575 nl_ext_ack_fn_t errfn
)
577 if (nl_dump_ext_ack(h
, errfn
))
580 fprintf(stderr
, "RTNETLINK answers: %s\n",
581 strerror(-err
->error
));
585 static int __rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iov
,
586 size_t iovlen
, struct nlmsghdr
**answer
,
587 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
589 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
591 struct msghdr msg
= {
593 .msg_namelen
= sizeof(nladdr
),
595 .msg_iovlen
= iovlen
,
597 unsigned int seq
= 0;
602 for (i
= 0; i
< iovlen
; i
++) {
604 h
->nlmsg_seq
= seq
= ++rtnl
->seq
;
606 h
->nlmsg_flags
|= NLM_F_ACK
;
609 status
= sendmsg(rtnl
->fd
, &msg
, 0);
611 perror("Cannot talk to rtnetlink");
615 /* change msg to use the response iov */
621 status
= rtnl_recvmsg(rtnl
->fd
, &msg
, &buf
);
627 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
629 "sender address length == %d\n",
633 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
634 int len
= h
->nlmsg_len
;
635 int l
= len
- sizeof(*h
);
637 if (l
< 0 || len
> status
) {
638 if (msg
.msg_flags
& MSG_TRUNC
) {
639 fprintf(stderr
, "Truncated message\n");
644 "!!!malformed message: len=%d\n",
649 if (nladdr
.nl_pid
!= 0 ||
650 h
->nlmsg_pid
!= rtnl
->local
.nl_pid
||
651 h
->nlmsg_seq
> seq
|| h
->nlmsg_seq
< seq
- iovlen
) {
652 /* Don't forget to skip that message. */
653 status
-= NLMSG_ALIGN(len
);
654 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
658 if (h
->nlmsg_type
== NLMSG_ERROR
) {
659 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
661 if (l
< sizeof(struct nlmsgerr
)) {
662 fprintf(stderr
, "ERROR truncated\n");
663 } else if (!err
->error
) {
664 /* check messages from kernel */
665 nl_dump_ext_ack(h
, errfn
);
668 *answer
= (struct nlmsghdr
*)buf
;
671 if (h
->nlmsg_seq
== seq
)
678 if (rtnl
->proto
!= NETLINK_SOCK_DIAG
&&
680 rtnl_talk_error(h
, err
, errfn
);
688 *answer
= (struct nlmsghdr
*)buf
;
692 fprintf(stderr
, "Unexpected reply!!!\n");
694 status
-= NLMSG_ALIGN(len
);
695 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
699 if (msg
.msg_flags
& MSG_TRUNC
) {
700 fprintf(stderr
, "Message truncated\n");
705 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
711 static int __rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
712 struct nlmsghdr
**answer
,
713 bool show_rtnl_err
, nl_ext_ack_fn_t errfn
)
717 .iov_len
= n
->nlmsg_len
720 return __rtnl_talk_iov(rtnl
, &iov
, 1, answer
, show_rtnl_err
, errfn
);
723 int rtnl_talk(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
724 struct nlmsghdr
**answer
)
726 return __rtnl_talk(rtnl
, n
, answer
, true, NULL
);
729 int rtnl_talk_iov(struct rtnl_handle
*rtnl
, struct iovec
*iovec
, size_t iovlen
,
730 struct nlmsghdr
**answer
)
732 return __rtnl_talk_iov(rtnl
, iovec
, iovlen
, answer
, true, NULL
);
735 int rtnl_talk_extack(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
736 struct nlmsghdr
**answer
,
737 nl_ext_ack_fn_t errfn
)
739 return __rtnl_talk(rtnl
, n
, answer
, true, errfn
);
742 int rtnl_talk_suppress_rtnl_errmsg(struct rtnl_handle
*rtnl
, struct nlmsghdr
*n
,
743 struct nlmsghdr
**answer
)
745 return __rtnl_talk(rtnl
, n
, answer
, false, NULL
);
748 int rtnl_listen_all_nsid(struct rtnl_handle
*rth
)
752 if (setsockopt(rth
->fd
, SOL_NETLINK
, NETLINK_LISTEN_ALL_NSID
, &on
,
754 perror("NETLINK_LISTEN_ALL_NSID");
757 rth
->flags
|= RTNL_HANDLE_F_LISTEN_ALL_NSID
;
761 int rtnl_listen(struct rtnl_handle
*rtnl
,
762 rtnl_listen_filter_t handler
,
767 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
769 struct msghdr msg
= {
771 .msg_namelen
= sizeof(nladdr
),
776 char cmsgbuf
[BUFSIZ
];
778 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
779 msg
.msg_control
= &cmsgbuf
;
780 msg
.msg_controllen
= sizeof(cmsgbuf
);
785 struct rtnl_ctrl_data ctrl
;
786 struct cmsghdr
*cmsg
;
788 iov
.iov_len
= sizeof(buf
);
789 status
= recvmsg(rtnl
->fd
, &msg
, 0);
792 if (errno
== EINTR
|| errno
== EAGAIN
)
794 fprintf(stderr
, "netlink receive error %s (%d)\n",
795 strerror(errno
), errno
);
796 if (errno
== ENOBUFS
)
801 fprintf(stderr
, "EOF on netlink\n");
804 if (msg
.msg_namelen
!= sizeof(nladdr
)) {
806 "Sender address length == %d\n",
811 if (rtnl
->flags
& RTNL_HANDLE_F_LISTEN_ALL_NSID
) {
812 memset(&ctrl
, 0, sizeof(ctrl
));
814 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
;
815 cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
816 if (cmsg
->cmsg_level
== SOL_NETLINK
&&
817 cmsg
->cmsg_type
== NETLINK_LISTEN_ALL_NSID
&&
818 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
819 int *data
= (int *)CMSG_DATA(cmsg
);
825 for (h
= (struct nlmsghdr
*)buf
; status
>= sizeof(*h
); ) {
827 int len
= h
->nlmsg_len
;
828 int l
= len
- sizeof(*h
);
830 if (l
< 0 || len
> status
) {
831 if (msg
.msg_flags
& MSG_TRUNC
) {
832 fprintf(stderr
, "Truncated message\n");
836 "!!!malformed message: len=%d\n",
841 err
= handler(&nladdr
, &ctrl
, h
, jarg
);
845 status
-= NLMSG_ALIGN(len
);
846 h
= (struct nlmsghdr
*)((char *)h
+ NLMSG_ALIGN(len
));
848 if (msg
.msg_flags
& MSG_TRUNC
) {
849 fprintf(stderr
, "Message truncated\n");
853 fprintf(stderr
, "!!!Remnant of size %d\n", status
);
859 int rtnl_from_file(FILE *rtnl
, rtnl_listen_filter_t handler
,
863 struct sockaddr_nl nladdr
= { .nl_family
= AF_NETLINK
};
865 struct nlmsghdr
*h
= (struct nlmsghdr
*)buf
;
871 status
= fread(&buf
, 1, sizeof(*h
), rtnl
);
876 perror("rtnl_from_file: fread");
883 l
= len
- sizeof(*h
);
885 if (l
< 0 || len
> sizeof(buf
)) {
886 fprintf(stderr
, "!!!malformed message: len=%d @%lu\n",
891 status
= fread(NLMSG_DATA(h
), 1, NLMSG_ALIGN(l
), rtnl
);
894 perror("rtnl_from_file: fread");
898 fprintf(stderr
, "rtnl-from_file: truncated message\n");
902 err
= handler(&nladdr
, NULL
, h
, jarg
);
908 int addattr(struct nlmsghdr
*n
, int maxlen
, int type
)
910 return addattr_l(n
, maxlen
, type
, NULL
, 0);
913 int addattr8(struct nlmsghdr
*n
, int maxlen
, int type
, __u8 data
)
915 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u8
));
918 int addattr16(struct nlmsghdr
*n
, int maxlen
, int type
, __u16 data
)
920 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u16
));
923 int addattr32(struct nlmsghdr
*n
, int maxlen
, int type
, __u32 data
)
925 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u32
));
928 int addattr64(struct nlmsghdr
*n
, int maxlen
, int type
, __u64 data
)
930 return addattr_l(n
, maxlen
, type
, &data
, sizeof(__u64
));
933 int addattrstrz(struct nlmsghdr
*n
, int maxlen
, int type
, const char *str
)
935 return addattr_l(n
, maxlen
, type
, str
, strlen(str
)+1);
938 int addattr_l(struct nlmsghdr
*n
, int maxlen
, int type
, const void *data
,
941 int len
= RTA_LENGTH(alen
);
944 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
) {
946 "addattr_l ERROR: message exceeded bound of %d\n",
951 rta
->rta_type
= type
;
954 memcpy(RTA_DATA(rta
), data
, alen
);
955 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
959 int addraw_l(struct nlmsghdr
*n
, int maxlen
, const void *data
, int len
)
961 if (NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
) > maxlen
) {
963 "addraw_l ERROR: message exceeded bound of %d\n",
968 memcpy(NLMSG_TAIL(n
), data
, len
);
969 memset((void *) NLMSG_TAIL(n
) + len
, 0, NLMSG_ALIGN(len
) - len
);
970 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + NLMSG_ALIGN(len
);
974 struct rtattr
*addattr_nest(struct nlmsghdr
*n
, int maxlen
, int type
)
976 struct rtattr
*nest
= NLMSG_TAIL(n
);
978 addattr_l(n
, maxlen
, type
, NULL
, 0);
982 int addattr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
984 nest
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)nest
;
988 struct rtattr
*addattr_nest_compat(struct nlmsghdr
*n
, int maxlen
, int type
,
989 const void *data
, int len
)
991 struct rtattr
*start
= NLMSG_TAIL(n
);
993 addattr_l(n
, maxlen
, type
, data
, len
);
994 addattr_nest(n
, maxlen
, type
);
998 int addattr_nest_compat_end(struct nlmsghdr
*n
, struct rtattr
*start
)
1000 struct rtattr
*nest
= (void *)start
+ NLMSG_ALIGN(start
->rta_len
);
1002 start
->rta_len
= (void *)NLMSG_TAIL(n
) - (void *)start
;
1003 addattr_nest_end(n
, nest
);
1004 return n
->nlmsg_len
;
1007 int rta_addattr32(struct rtattr
*rta
, int maxlen
, int type
, __u32 data
)
1009 int len
= RTA_LENGTH(4);
1010 struct rtattr
*subrta
;
1012 if (RTA_ALIGN(rta
->rta_len
) + len
> maxlen
) {
1014 "rta_addattr32: Error! max allowed bound %d exceeded\n",
1018 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1019 subrta
->rta_type
= type
;
1020 subrta
->rta_len
= len
;
1021 memcpy(RTA_DATA(subrta
), &data
, 4);
1022 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + len
;
1026 int rta_addattr_l(struct rtattr
*rta
, int maxlen
, int type
,
1027 const void *data
, int alen
)
1029 struct rtattr
*subrta
;
1030 int len
= RTA_LENGTH(alen
);
1032 if (RTA_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
) > maxlen
) {
1034 "rta_addattr_l: Error! max allowed bound %d exceeded\n",
1038 subrta
= (struct rtattr
*)(((char *)rta
) + RTA_ALIGN(rta
->rta_len
));
1039 subrta
->rta_type
= type
;
1040 subrta
->rta_len
= len
;
1042 memcpy(RTA_DATA(subrta
), data
, alen
);
1043 rta
->rta_len
= NLMSG_ALIGN(rta
->rta_len
) + RTA_ALIGN(len
);
1047 int rta_addattr8(struct rtattr
*rta
, int maxlen
, int type
, __u8 data
)
1049 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u8
));
1052 int rta_addattr16(struct rtattr
*rta
, int maxlen
, int type
, __u16 data
)
1054 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u16
));
1057 int rta_addattr64(struct rtattr
*rta
, int maxlen
, int type
, __u64 data
)
1059 return rta_addattr_l(rta
, maxlen
, type
, &data
, sizeof(__u64
));
1062 struct rtattr
*rta_nest(struct rtattr
*rta
, int maxlen
, int type
)
1064 struct rtattr
*nest
= RTA_TAIL(rta
);
1066 rta_addattr_l(rta
, maxlen
, type
, NULL
, 0);
1071 int rta_nest_end(struct rtattr
*rta
, struct rtattr
*nest
)
1073 nest
->rta_len
= (void *)RTA_TAIL(rta
) - (void *)nest
;
1075 return rta
->rta_len
;
1078 int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
1080 return parse_rtattr_flags(tb
, max
, rta
, len
, 0);
1083 int parse_rtattr_flags(struct rtattr
*tb
[], int max
, struct rtattr
*rta
,
1084 int len
, unsigned short flags
)
1086 unsigned short type
;
1088 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
1089 while (RTA_OK(rta
, len
)) {
1090 type
= rta
->rta_type
& ~flags
;
1091 if ((type
<= max
) && (!tb
[type
]))
1093 rta
= RTA_NEXT(rta
, len
);
1096 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1101 int parse_rtattr_byindex(struct rtattr
*tb
[], int max
,
1102 struct rtattr
*rta
, int len
)
1106 memset(tb
, 0, sizeof(struct rtattr
*) * max
);
1107 while (RTA_OK(rta
, len
)) {
1108 if (rta
->rta_type
<= max
&& i
< max
)
1110 rta
= RTA_NEXT(rta
, len
);
1113 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1118 struct rtattr
*parse_rtattr_one(int type
, struct rtattr
*rta
, int len
)
1120 while (RTA_OK(rta
, len
)) {
1121 if (rta
->rta_type
== type
)
1123 rta
= RTA_NEXT(rta
, len
);
1127 fprintf(stderr
, "!!!Deficit %d, rta_len=%d\n",
1132 int __parse_rtattr_nested_compat(struct rtattr
*tb
[], int max
,
1136 if (RTA_PAYLOAD(rta
) < len
)
1138 if (RTA_PAYLOAD(rta
) >= RTA_ALIGN(len
) + sizeof(struct rtattr
)) {
1139 rta
= RTA_DATA(rta
) + RTA_ALIGN(len
);
1140 return parse_rtattr_nested(tb
, max
, rta
);
1142 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));