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