osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
Patrick McHardy | 7f03191 | 2009-10-28 20:50:48 +0100 | [diff] [blame] | 28 | int rcvbuf = 1024 * 1024; |
| 29 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 30 | void rtnl_close(struct rtnl_handle *rth) |
| 31 | { |
Stephen Hemminger | 3bfa73f | 2006-09-26 10:41:57 -0700 | [diff] [blame] | 32 | if (rth->fd >= 0) { |
| 33 | close(rth->fd); |
| 34 | rth->fd = -1; |
| 35 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 36 | } |
| 37 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 38 | int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions, |
| 39 | int protocol) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 40 | { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 41 | socklen_t addr_len; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 42 | int sndbuf = 32768; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 43 | |
Stephen Hemminger | b16621c | 2007-05-10 19:33:21 -0700 | [diff] [blame] | 44 | memset(rth, 0, sizeof(*rth)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 45 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 46 | rth->fd = socket(AF_NETLINK, SOCK_RAW, protocol); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 47 | if (rth->fd < 0) { |
| 48 | perror("Cannot open netlink socket"); |
| 49 | return -1; |
| 50 | } |
| 51 | |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 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 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 87 | int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions) |
| 88 | { |
| 89 | return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE); |
| 90 | } |
| 91 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 92 | int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type) |
| 93 | { |
| 94 | struct { |
| 95 | struct nlmsghdr nlh; |
| 96 | struct rtgenmsg g; |
Rose, Gregory V | bd886eb | 2012-02-21 10:43:09 +0000 | [diff] [blame] | 97 | __u16 align_rta; /* attribute has to be 32bit aligned */ |
| 98 | struct rtattr ext_req; |
| 99 | __u32 ext_filter_mask; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 100 | } req; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 101 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 102 | memset(&req, 0, sizeof(req)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 103 | req.nlh.nlmsg_len = sizeof(req); |
| 104 | req.nlh.nlmsg_type = type; |
Masatake YAMATO | aa38c3e | 2012-01-19 14:16:12 -0800 | [diff] [blame] | 105 | req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 106 | req.nlh.nlmsg_pid = 0; |
| 107 | req.nlh.nlmsg_seq = rth->dump = ++rth->seq; |
| 108 | req.g.rtgen_family = family; |
| 109 | |
Rose, Gregory V | bd886eb | 2012-02-21 10:43:09 +0000 | [diff] [blame] | 110 | req.ext_req.rta_type = IFLA_EXT_MASK; |
| 111 | req.ext_req.rta_len = RTA_LENGTH(sizeof(__u32)); |
| 112 | req.ext_filter_mask = RTEXT_FILTER_VF; |
| 113 | |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 114 | return send(rth->fd, (void*)&req, sizeof(req), 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Stephen Hemminger | 6cf8398 | 2011-12-23 10:40:04 -0800 | [diff] [blame] | 117 | int rtnl_send(struct rtnl_handle *rth, const void *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 118 | { |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 119 | return send(rth->fd, buf, len, 0); |
| 120 | } |
| 121 | |
Stephen Hemminger | 6cf8398 | 2011-12-23 10:40:04 -0800 | [diff] [blame] | 122 | int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len) |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 123 | { |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 124 | struct nlmsghdr *h; |
| 125 | int status; |
| 126 | char resp[1024]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 127 | |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 128 | status = send(rth->fd, buf, len, 0); |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 129 | if (status < 0) |
| 130 | return status; |
| 131 | |
Stephen Hemminger | 2d8240f | 2009-07-13 10:15:23 -0700 | [diff] [blame] | 132 | /* Check for immediate errors */ |
| 133 | status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK); |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 134 | if (status < 0) { |
| 135 | if (errno == EAGAIN) |
| 136 | return 0; |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status); |
| 141 | h = NLMSG_NEXT(h, status)) { |
| 142 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 143 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
| 144 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) |
| 145 | fprintf(stderr, "ERROR truncated\n"); |
| 146 | else |
| 147 | errno = -err->error; |
Sven Anders | 24f3818 | 2009-11-10 09:07:26 -0800 | [diff] [blame] | 148 | return -1; |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 149 | } |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len) |
| 156 | { |
| 157 | struct nlmsghdr nlh; |
Stephen Hemminger | 6cf8398 | 2011-12-23 10:40:04 -0800 | [diff] [blame] | 158 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 159 | struct iovec iov[2] = { |
| 160 | { .iov_base = &nlh, .iov_len = sizeof(nlh) }, |
| 161 | { .iov_base = req, .iov_len = len } |
| 162 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 163 | struct msghdr msg = { |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 164 | .msg_name = &nladdr, |
| 165 | .msg_namelen = sizeof(nladdr), |
| 166 | .msg_iov = iov, |
| 167 | .msg_iovlen = 2, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 170 | nlh.nlmsg_len = NLMSG_LENGTH(len); |
| 171 | nlh.nlmsg_type = type; |
Masatake YAMATO | aa38c3e | 2012-01-19 14:16:12 -0800 | [diff] [blame] | 172 | nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 173 | nlh.nlmsg_pid = 0; |
| 174 | nlh.nlmsg_seq = rth->dump = ++rth->seq; |
| 175 | |
| 176 | return sendmsg(rth->fd, &msg, 0); |
| 177 | } |
| 178 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 179 | int rtnl_dump_filter_l(struct rtnl_handle *rth, |
| 180 | const struct rtnl_dump_filter_arg *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 181 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 182 | struct sockaddr_nl nladdr; |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 183 | struct iovec iov; |
| 184 | struct msghdr msg = { |
| 185 | .msg_name = &nladdr, |
| 186 | .msg_namelen = sizeof(nladdr), |
| 187 | .msg_iov = &iov, |
| 188 | .msg_iovlen = 1, |
| 189 | }; |
| 190 | char buf[16384]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 191 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 192 | iov.iov_base = buf; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 193 | while (1) { |
| 194 | int status; |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 195 | const struct rtnl_dump_filter_arg *a; |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 196 | int found_done = 0; |
| 197 | int msglen = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 198 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 199 | iov.iov_len = sizeof(buf); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 200 | status = recvmsg(rth->fd, &msg, 0); |
| 201 | |
| 202 | if (status < 0) { |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 203 | if (errno == EINTR || errno == EAGAIN) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 204 | continue; |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 205 | fprintf(stderr, "netlink receive error %s (%d)\n", |
| 206 | strerror(errno), errno); |
| 207 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 208 | } |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 209 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 210 | if (status == 0) { |
| 211 | fprintf(stderr, "EOF on netlink\n"); |
| 212 | return -1; |
| 213 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 214 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 215 | for (a = arg; a->filter; a++) { |
| 216 | struct nlmsghdr *h = (struct nlmsghdr*)buf; |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 217 | msglen = status; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 218 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 219 | while (NLMSG_OK(h, msglen)) { |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 220 | int err; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 221 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 222 | if (nladdr.nl_pid != 0 || |
| 223 | h->nlmsg_pid != rth->local.nl_pid || |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 224 | h->nlmsg_seq != rth->dump) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 225 | goto skip_it; |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 226 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 227 | if (h->nlmsg_type == NLMSG_DONE) { |
| 228 | found_done = 1; |
| 229 | break; /* process next filter */ |
| 230 | } |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 231 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 232 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
| 233 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
| 234 | fprintf(stderr, |
| 235 | "ERROR truncated\n"); |
| 236 | } else { |
| 237 | errno = -err->error; |
| 238 | perror("RTNETLINK answers"); |
| 239 | } |
| 240 | return -1; |
| 241 | } |
| 242 | err = a->filter(&nladdr, h, a->arg1); |
| 243 | if (err < 0) |
| 244 | return err; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 245 | |
| 246 | skip_it: |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 247 | h = NLMSG_NEXT(h, msglen); |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 248 | } |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | if (found_done) |
| 252 | return 0; |
| 253 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 254 | if (msg.msg_flags & MSG_TRUNC) { |
| 255 | fprintf(stderr, "Message truncated\n"); |
| 256 | continue; |
| 257 | } |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 258 | if (msglen) { |
| 259 | fprintf(stderr, "!!!Remnant of size %d\n", msglen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 260 | exit(1); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 265 | int rtnl_dump_filter(struct rtnl_handle *rth, |
| 266 | rtnl_filter_t filter, |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 267 | void *arg1) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 268 | { |
| 269 | const struct rtnl_dump_filter_arg a[2] = { |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 270 | { .filter = filter, .arg1 = arg1, }, |
| 271 | { .filter = NULL, .arg1 = NULL, }, |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | return rtnl_dump_filter_l(rth, a); |
| 275 | } |
| 276 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 277 | int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 278 | unsigned groups, struct nlmsghdr *answer) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 279 | { |
| 280 | int status; |
| 281 | unsigned seq; |
| 282 | struct nlmsghdr *h; |
| 283 | struct sockaddr_nl nladdr; |
shemminger | fb22975 | 2005-10-04 23:15:32 +0000 | [diff] [blame] | 284 | struct iovec iov = { |
| 285 | .iov_base = (void*) n, |
| 286 | .iov_len = n->nlmsg_len |
| 287 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 288 | struct msghdr msg = { |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 289 | .msg_name = &nladdr, |
| 290 | .msg_namelen = sizeof(nladdr), |
| 291 | .msg_iov = &iov, |
| 292 | .msg_iovlen = 1, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 293 | }; |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 294 | char buf[16384]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 295 | |
| 296 | memset(&nladdr, 0, sizeof(nladdr)); |
| 297 | nladdr.nl_family = AF_NETLINK; |
| 298 | nladdr.nl_pid = peer; |
| 299 | nladdr.nl_groups = groups; |
| 300 | |
| 301 | n->nlmsg_seq = seq = ++rtnl->seq; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 302 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 303 | if (answer == NULL) |
| 304 | n->nlmsg_flags |= NLM_F_ACK; |
| 305 | |
| 306 | status = sendmsg(rtnl->fd, &msg, 0); |
| 307 | |
| 308 | if (status < 0) { |
| 309 | perror("Cannot talk to rtnetlink"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 313 | memset(buf,0,sizeof(buf)); |
| 314 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 315 | iov.iov_base = buf; |
| 316 | |
| 317 | while (1) { |
| 318 | iov.iov_len = sizeof(buf); |
| 319 | status = recvmsg(rtnl->fd, &msg, 0); |
| 320 | |
| 321 | if (status < 0) { |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 322 | if (errno == EINTR || errno == EAGAIN) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 323 | continue; |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 324 | fprintf(stderr, "netlink receive error %s (%d)\n", |
| 325 | strerror(errno), errno); |
| 326 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 327 | } |
| 328 | if (status == 0) { |
| 329 | fprintf(stderr, "EOF on netlink\n"); |
| 330 | return -1; |
| 331 | } |
| 332 | if (msg.msg_namelen != sizeof(nladdr)) { |
| 333 | fprintf(stderr, "sender address length == %d\n", msg.msg_namelen); |
| 334 | exit(1); |
| 335 | } |
| 336 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 337 | int len = h->nlmsg_len; |
| 338 | int l = len - sizeof(*h); |
| 339 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 340 | if (l < 0 || len>status) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 341 | if (msg.msg_flags & MSG_TRUNC) { |
| 342 | fprintf(stderr, "Truncated message\n"); |
| 343 | return -1; |
| 344 | } |
| 345 | fprintf(stderr, "!!!malformed message: len=%d\n", len); |
| 346 | exit(1); |
| 347 | } |
| 348 | |
org[shemminger]!shemminger | 10f57ef | 2004-06-07 22:04:04 +0000 | [diff] [blame] | 349 | if (nladdr.nl_pid != peer || |
| 350 | h->nlmsg_pid != rtnl->local.nl_pid || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 351 | h->nlmsg_seq != seq) { |
shemminger | 4cca16f | 2006-03-10 23:31:46 +0000 | [diff] [blame] | 352 | /* Don't forget to skip that message. */ |
| 353 | status -= NLMSG_ALIGN(len); |
| 354 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 355 | continue; |
| 356 | } |
| 357 | |
| 358 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 359 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
| 360 | if (l < sizeof(struct nlmsgerr)) { |
| 361 | fprintf(stderr, "ERROR truncated\n"); |
| 362 | } else { |
Pavel Emelyanov | b8cf1e9 | 2012-08-20 12:08:40 +0400 | [diff] [blame^] | 363 | if (!err->error) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 364 | if (answer) |
| 365 | memcpy(answer, h, h->nlmsg_len); |
| 366 | return 0; |
| 367 | } |
Pavel Emelyanov | b8cf1e9 | 2012-08-20 12:08:40 +0400 | [diff] [blame^] | 368 | |
| 369 | fprintf(stderr, "RTNETLINK answers: %s\n", strerror(-err->error)); |
| 370 | errno = -err->error; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 371 | } |
| 372 | return -1; |
| 373 | } |
| 374 | if (answer) { |
| 375 | memcpy(answer, h, h->nlmsg_len); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | fprintf(stderr, "Unexpected reply!!!\n"); |
| 380 | |
| 381 | status -= NLMSG_ALIGN(len); |
| 382 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); |
| 383 | } |
| 384 | if (msg.msg_flags & MSG_TRUNC) { |
| 385 | fprintf(stderr, "Message truncated\n"); |
| 386 | continue; |
| 387 | } |
| 388 | if (status) { |
| 389 | fprintf(stderr, "!!!Remnant of size %d\n", status); |
| 390 | exit(1); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 395 | int rtnl_listen(struct rtnl_handle *rtnl, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 396 | rtnl_filter_t handler, |
| 397 | void *jarg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 398 | { |
| 399 | int status; |
| 400 | struct nlmsghdr *h; |
| 401 | struct sockaddr_nl nladdr; |
| 402 | struct iovec iov; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 403 | struct msghdr msg = { |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 404 | .msg_name = &nladdr, |
| 405 | .msg_namelen = sizeof(nladdr), |
| 406 | .msg_iov = &iov, |
| 407 | .msg_iovlen = 1, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 408 | }; |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 409 | char buf[8192]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 410 | |
| 411 | memset(&nladdr, 0, sizeof(nladdr)); |
| 412 | nladdr.nl_family = AF_NETLINK; |
| 413 | nladdr.nl_pid = 0; |
| 414 | nladdr.nl_groups = 0; |
| 415 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 416 | iov.iov_base = buf; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 417 | while (1) { |
| 418 | iov.iov_len = sizeof(buf); |
| 419 | status = recvmsg(rtnl->fd, &msg, 0); |
| 420 | |
| 421 | if (status < 0) { |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 422 | if (errno == EINTR || errno == EAGAIN) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 423 | continue; |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 424 | fprintf(stderr, "netlink receive error %s (%d)\n", |
| 425 | strerror(errno), errno); |
Patrick McHardy | 7f03191 | 2009-10-28 20:50:48 +0100 | [diff] [blame] | 426 | if (errno == ENOBUFS) |
| 427 | continue; |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 428 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 429 | } |
| 430 | if (status == 0) { |
| 431 | fprintf(stderr, "EOF on netlink\n"); |
| 432 | return -1; |
| 433 | } |
| 434 | if (msg.msg_namelen != sizeof(nladdr)) { |
| 435 | fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen); |
| 436 | exit(1); |
| 437 | } |
| 438 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { |
| 439 | int err; |
| 440 | int len = h->nlmsg_len; |
| 441 | int l = len - sizeof(*h); |
| 442 | |
| 443 | if (l<0 || len>status) { |
| 444 | if (msg.msg_flags & MSG_TRUNC) { |
| 445 | fprintf(stderr, "Truncated message\n"); |
| 446 | return -1; |
| 447 | } |
| 448 | fprintf(stderr, "!!!malformed message: len=%d\n", len); |
| 449 | exit(1); |
| 450 | } |
| 451 | |
| 452 | err = handler(&nladdr, h, jarg); |
| 453 | if (err < 0) |
| 454 | return err; |
| 455 | |
| 456 | status -= NLMSG_ALIGN(len); |
| 457 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); |
| 458 | } |
| 459 | if (msg.msg_flags & MSG_TRUNC) { |
| 460 | fprintf(stderr, "Message truncated\n"); |
| 461 | continue; |
| 462 | } |
| 463 | if (status) { |
| 464 | fprintf(stderr, "!!!Remnant of size %d\n", status); |
| 465 | exit(1); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 470 | int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler, |
| 471 | void *jarg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 472 | { |
| 473 | int status; |
| 474 | struct sockaddr_nl nladdr; |
| 475 | char buf[8192]; |
| 476 | struct nlmsghdr *h = (void*)buf; |
| 477 | |
| 478 | memset(&nladdr, 0, sizeof(nladdr)); |
| 479 | nladdr.nl_family = AF_NETLINK; |
| 480 | nladdr.nl_pid = 0; |
| 481 | nladdr.nl_groups = 0; |
| 482 | |
| 483 | while (1) { |
Stephen Hemminger | 2dd9f8e | 2011-06-20 14:34:46 -0700 | [diff] [blame] | 484 | int err, len; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 485 | int l; |
| 486 | |
| 487 | status = fread(&buf, 1, sizeof(*h), rtnl); |
| 488 | |
| 489 | if (status < 0) { |
| 490 | if (errno == EINTR) |
| 491 | continue; |
| 492 | perror("rtnl_from_file: fread"); |
| 493 | return -1; |
| 494 | } |
| 495 | if (status == 0) |
| 496 | return 0; |
| 497 | |
| 498 | len = h->nlmsg_len; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 499 | l = len - sizeof(*h); |
| 500 | |
| 501 | if (l<0 || len>sizeof(buf)) { |
| 502 | fprintf(stderr, "!!!malformed message: len=%d @%lu\n", |
| 503 | len, ftell(rtnl)); |
| 504 | return -1; |
| 505 | } |
| 506 | |
| 507 | status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl); |
| 508 | |
| 509 | if (status < 0) { |
| 510 | perror("rtnl_from_file: fread"); |
| 511 | return -1; |
| 512 | } |
| 513 | if (status < l) { |
| 514 | fprintf(stderr, "rtnl-from_file: truncated message\n"); |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | err = handler(&nladdr, h, jarg); |
| 519 | if (err < 0) |
| 520 | return err; |
| 521 | } |
| 522 | } |
| 523 | |
Stephen Hemminger | 2aa3dd2 | 2011-12-23 10:43:54 -0800 | [diff] [blame] | 524 | int addattr(struct nlmsghdr *n, int maxlen, int type) |
| 525 | { |
| 526 | return addattr_l(n, maxlen, type, NULL, 0); |
| 527 | } |
| 528 | |
| 529 | int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data) |
| 530 | { |
| 531 | return addattr_l(n, maxlen, type, &data, sizeof(__u8)); |
| 532 | } |
| 533 | |
| 534 | int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data) |
| 535 | { |
| 536 | return addattr_l(n, maxlen, type, &data, sizeof(__u16)); |
| 537 | } |
| 538 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 539 | int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data) |
| 540 | { |
Stephen Hemminger | 2aa3dd2 | 2011-12-23 10:43:54 -0800 | [diff] [blame] | 541 | return addattr_l(n, maxlen, type, &data, sizeof(__u32)); |
| 542 | } |
| 543 | |
| 544 | int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data) |
| 545 | { |
| 546 | return addattr_l(n, maxlen, type, &data, sizeof(__u64)); |
| 547 | } |
| 548 | |
| 549 | int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str) |
| 550 | { |
| 551 | return addattr_l(n, maxlen, type, str, strlen(str)+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 552 | } |
| 553 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 554 | int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 555 | int alen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 556 | { |
| 557 | int len = RTA_LENGTH(alen); |
| 558 | struct rtattr *rta; |
| 559 | |
net[shemminger]!shemminger | 3dabdbb | 2005-03-30 18:18:12 +0000 | [diff] [blame] | 560 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 561 | fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 562 | return -1; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 563 | } |
6!tgraf | 07f9436 | 2005-01-18 13:58:49 +0000 | [diff] [blame] | 564 | rta = NLMSG_TAIL(n); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 565 | rta->rta_type = type; |
| 566 | rta->rta_len = len; |
| 567 | memcpy(RTA_DATA(rta), data, alen); |
net[shemminger]!shemminger | 3dabdbb | 2005-03-30 18:18:12 +0000 | [diff] [blame] | 568 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
6!tgraf | 07f9436 | 2005-01-18 13:58:49 +0000 | [diff] [blame] | 572 | int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len) |
| 573 | { |
| 574 | if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) { |
| 575 | fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen); |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | memcpy(NLMSG_TAIL(n), data, len); |
| 580 | memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len); |
| 581 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len); |
| 582 | return 0; |
| 583 | } |
| 584 | |
Patrick McHardy | 2f90c9c | 2007-08-14 11:21:19 -0700 | [diff] [blame] | 585 | struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type) |
| 586 | { |
| 587 | struct rtattr *nest = NLMSG_TAIL(n); |
| 588 | |
| 589 | addattr_l(n, maxlen, type, NULL, 0); |
| 590 | return nest; |
| 591 | } |
| 592 | |
| 593 | int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest) |
| 594 | { |
| 595 | nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest; |
| 596 | return n->nlmsg_len; |
| 597 | } |
| 598 | |
| 599 | struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type, |
| 600 | const void *data, int len) |
| 601 | { |
| 602 | struct rtattr *start = NLMSG_TAIL(n); |
| 603 | |
| 604 | addattr_l(n, maxlen, type, data, len); |
| 605 | addattr_nest(n, maxlen, type); |
| 606 | return start; |
| 607 | } |
| 608 | |
| 609 | int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start) |
| 610 | { |
| 611 | struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len); |
| 612 | |
| 613 | start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start; |
| 614 | addattr_nest_end(n, nest); |
| 615 | return n->nlmsg_len; |
| 616 | } |
| 617 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 618 | int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data) |
| 619 | { |
| 620 | int len = RTA_LENGTH(4); |
| 621 | struct rtattr *subrta; |
| 622 | |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 623 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) { |
| 624 | fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 625 | return -1; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 626 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 627 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
| 628 | subrta->rta_type = type; |
| 629 | subrta->rta_len = len; |
| 630 | memcpy(RTA_DATA(subrta), &data, 4); |
| 631 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len; |
| 632 | return 0; |
| 633 | } |
| 634 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 635 | int rta_addattr_l(struct rtattr *rta, int maxlen, int type, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 636 | const void *data, int alen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 637 | { |
| 638 | struct rtattr *subrta; |
| 639 | int len = RTA_LENGTH(alen); |
| 640 | |
net[shemminger]!shemminger | 3dabdbb | 2005-03-30 18:18:12 +0000 | [diff] [blame] | 641 | if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) { |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 642 | fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 643 | return -1; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 644 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 645 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
| 646 | subrta->rta_type = type; |
| 647 | subrta->rta_len = len; |
| 648 | memcpy(RTA_DATA(subrta), data, alen); |
net[shemminger]!shemminger | 3dabdbb | 2005-03-30 18:18:12 +0000 | [diff] [blame] | 649 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 653 | int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) |
| 654 | { |
osdl.net!shemminger | 175e244 | 2005-02-07 18:17:38 +0000 | [diff] [blame] | 655 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 656 | while (RTA_OK(rta, len)) { |
Williams, Mitch A | 6e46ec8 | 2010-02-10 01:46:47 +0000 | [diff] [blame] | 657 | if ((rta->rta_type <= max) && (!tb[rta->rta_type])) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 658 | tb[rta->rta_type] = rta; |
| 659 | rta = RTA_NEXT(rta,len); |
| 660 | } |
| 661 | if (len) |
| 662 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len); |
| 663 | return 0; |
| 664 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 665 | |
| 666 | int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len) |
| 667 | { |
| 668 | int i = 0; |
osdl.net!shemminger | 175e244 | 2005-02-07 18:17:38 +0000 | [diff] [blame] | 669 | |
| 670 | memset(tb, 0, sizeof(struct rtattr *) * max); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 671 | while (RTA_OK(rta, len)) { |
osdl.net!shemminger | 175e244 | 2005-02-07 18:17:38 +0000 | [diff] [blame] | 672 | if (rta->rta_type <= max && i < max) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 673 | tb[i++] = rta; |
| 674 | rta = RTA_NEXT(rta,len); |
| 675 | } |
| 676 | if (len) |
| 677 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len); |
| 678 | return i; |
| 679 | } |
Patrick McHardy | 2f90c9c | 2007-08-14 11:21:19 -0700 | [diff] [blame] | 680 | |
| 681 | int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, |
| 682 | int len) |
| 683 | { |
| 684 | if (RTA_PAYLOAD(rta) < len) |
| 685 | return -1; |
| 686 | if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) { |
| 687 | rta = RTA_DATA(rta) + RTA_ALIGN(len); |
| 688 | return parse_rtattr_nested(tb, max, rta); |
| 689 | } |
Stephen Hemminger | 037c635 | 2007-12-10 11:34:40 -0800 | [diff] [blame] | 690 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); |
Patrick McHardy | 2f90c9c | 2007-08-14 11:21:19 -0700 | [diff] [blame] | 691 | return 0; |
| 692 | } |