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 | |
Nicolas Dichtel | 449b824 | 2015-05-20 16:20:00 +0200 | [diff] [blame] | 28 | #ifndef SOL_NETLINK |
| 29 | #define SOL_NETLINK 270 |
| 30 | #endif |
| 31 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 32 | #ifndef MIN |
| 33 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 34 | #endif |
| 35 | |
Patrick McHardy | 7f03191 | 2009-10-28 20:50:48 +0100 | [diff] [blame] | 36 | int rcvbuf = 1024 * 1024; |
| 37 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 38 | void rtnl_close(struct rtnl_handle *rth) |
| 39 | { |
Stephen Hemminger | 3bfa73f | 2006-09-26 10:41:57 -0700 | [diff] [blame] | 40 | if (rth->fd >= 0) { |
| 41 | close(rth->fd); |
| 42 | rth->fd = -1; |
| 43 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 46 | int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions, |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 47 | int protocol) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 48 | { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 49 | socklen_t addr_len; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 50 | int sndbuf = 32768; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 51 | |
Stephen Hemminger | b16621c | 2007-05-10 19:33:21 -0700 | [diff] [blame] | 52 | memset(rth, 0, sizeof(*rth)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 53 | |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 54 | rth->proto = protocol; |
Andrey Vagin | bcb9d40 | 2013-06-04 12:01:14 +0400 | [diff] [blame] | 55 | rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 56 | if (rth->fd < 0) { |
| 57 | perror("Cannot open netlink socket"); |
| 58 | return -1; |
| 59 | } |
| 60 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 61 | if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, |
| 62 | &sndbuf, sizeof(sndbuf)) < 0) { |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 63 | perror("SO_SNDBUF"); |
| 64 | return -1; |
| 65 | } |
| 66 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 67 | if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF, |
| 68 | &rcvbuf, sizeof(rcvbuf)) < 0) { |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 69 | perror("SO_RCVBUF"); |
| 70 | return -1; |
| 71 | } |
| 72 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 73 | memset(&rth->local, 0, sizeof(rth->local)); |
| 74 | rth->local.nl_family = AF_NETLINK; |
| 75 | rth->local.nl_groups = subscriptions; |
| 76 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 77 | if (bind(rth->fd, (struct sockaddr *)&rth->local, |
| 78 | sizeof(rth->local)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 79 | perror("Cannot bind netlink socket"); |
| 80 | return -1; |
| 81 | } |
| 82 | addr_len = sizeof(rth->local); |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 83 | if (getsockname(rth->fd, (struct sockaddr *)&rth->local, |
| 84 | &addr_len) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 85 | perror("Cannot getsockname"); |
| 86 | return -1; |
| 87 | } |
| 88 | if (addr_len != sizeof(rth->local)) { |
| 89 | fprintf(stderr, "Wrong address length %d\n", addr_len); |
| 90 | return -1; |
| 91 | } |
| 92 | if (rth->local.nl_family != AF_NETLINK) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 93 | fprintf(stderr, "Wrong address family %d\n", |
| 94 | rth->local.nl_family); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | rth->seq = time(NULL); |
| 98 | return 0; |
| 99 | } |
| 100 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 101 | int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 102 | { |
| 103 | return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE); |
| 104 | } |
| 105 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 106 | int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type) |
| 107 | { |
Vlad Yasevich | 9eff0e5 | 2013-02-28 10:04:05 +0000 | [diff] [blame] | 108 | return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF); |
| 109 | } |
| 110 | |
| 111 | int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int family, int type, |
| 112 | __u32 filt_mask) |
| 113 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 114 | struct { |
| 115 | struct nlmsghdr nlh; |
Alexander Duyck | 63338dc | 2013-04-25 12:07:10 +0000 | [diff] [blame] | 116 | struct ifinfomsg ifm; |
Lutz Jaenicke | 257422f | 2012-08-30 05:01:34 +0000 | [diff] [blame] | 117 | /* attribute has to be NLMSG aligned */ |
| 118 | struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO))); |
Rose, Gregory V | bd886eb | 2012-02-21 10:43:09 +0000 | [diff] [blame] | 119 | __u32 ext_filter_mask; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 120 | } req = { |
| 121 | .nlh.nlmsg_len = sizeof(req), |
| 122 | .nlh.nlmsg_type = type, |
| 123 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 124 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, |
| 125 | .ifm.ifi_family = family, |
| 126 | .ext_req.rta_type = IFLA_EXT_MASK, |
| 127 | .ext_req.rta_len = RTA_LENGTH(sizeof(__u32)), |
| 128 | .ext_filter_mask = filt_mask, |
| 129 | }; |
Rose, Gregory V | bd886eb | 2012-02-21 10:43:09 +0000 | [diff] [blame] | 130 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 131 | return send(rth->fd, &req, sizeof(req), 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 132 | } |
| 133 | |
David Ahern | b0a4ce6 | 2016-05-11 06:51:58 -0700 | [diff] [blame] | 134 | int rtnl_wilddump_req_filter_fn(struct rtnl_handle *rth, int family, int type, |
| 135 | req_filter_fn_t filter_fn) |
| 136 | { |
| 137 | struct { |
| 138 | struct nlmsghdr nlh; |
| 139 | struct ifinfomsg ifm; |
| 140 | char buf[1024]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 141 | } req = { |
| 142 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
| 143 | .nlh.nlmsg_type = type, |
| 144 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 145 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, |
| 146 | .ifm.ifi_family = family, |
| 147 | }; |
David Ahern | b0a4ce6 | 2016-05-11 06:51:58 -0700 | [diff] [blame] | 148 | int err; |
| 149 | |
| 150 | if (!filter_fn) |
| 151 | return -EINVAL; |
| 152 | |
David Ahern | b0a4ce6 | 2016-05-11 06:51:58 -0700 | [diff] [blame] | 153 | err = filter_fn(&req.nlh, sizeof(req)); |
| 154 | if (err) |
| 155 | return err; |
| 156 | |
Zhang Shengju | 1b109a3 | 2016-11-19 23:50:13 +0800 | [diff] [blame] | 157 | return send(rth->fd, &req, req.nlh.nlmsg_len, 0); |
David Ahern | b0a4ce6 | 2016-05-11 06:51:58 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Nikolay Aleksandrov | 7abf5de | 2016-08-25 14:28:55 +0200 | [diff] [blame] | 160 | int rtnl_wilddump_stats_req_filter(struct rtnl_handle *rth, int fam, int type, |
| 161 | __u32 filt_mask) |
| 162 | { |
| 163 | struct { |
| 164 | struct nlmsghdr nlh; |
| 165 | struct if_stats_msg ifsm; |
| 166 | } req; |
| 167 | |
| 168 | memset(&req, 0, sizeof(req)); |
| 169 | req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct if_stats_msg)); |
| 170 | req.nlh.nlmsg_type = type; |
| 171 | req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST; |
| 172 | req.nlh.nlmsg_pid = 0; |
| 173 | req.nlh.nlmsg_seq = rth->dump = ++rth->seq; |
| 174 | req.ifsm.family = fam; |
| 175 | req.ifsm.filter_mask = filt_mask; |
| 176 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 177 | return send(rth->fd, &req, sizeof(req), 0); |
Nikolay Aleksandrov | 7abf5de | 2016-08-25 14:28:55 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Stephen Hemminger | 6cf8398 | 2011-12-23 10:40:04 -0800 | [diff] [blame] | 180 | 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] | 181 | { |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 182 | return send(rth->fd, buf, len, 0); |
| 183 | } |
| 184 | |
Stephen Hemminger | 6cf8398 | 2011-12-23 10:40:04 -0800 | [diff] [blame] | 185 | 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] | 186 | { |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 187 | struct nlmsghdr *h; |
| 188 | int status; |
| 189 | char resp[1024]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 190 | |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 191 | status = send(rth->fd, buf, len, 0); |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 192 | if (status < 0) |
| 193 | return status; |
| 194 | |
Stephen Hemminger | 2d8240f | 2009-07-13 10:15:23 -0700 | [diff] [blame] | 195 | /* Check for immediate errors */ |
| 196 | status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK); |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 197 | if (status < 0) { |
| 198 | if (errno == EAGAIN) |
| 199 | return 0; |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status); |
| 204 | h = NLMSG_NEXT(h, status)) { |
| 205 | if (h->nlmsg_type == NLMSG_ERROR) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 206 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); |
| 207 | |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 208 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) |
| 209 | fprintf(stderr, "ERROR truncated\n"); |
Stephen Hemminger | e9e9365 | 2016-03-27 10:47:46 -0700 | [diff] [blame] | 210 | else |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 211 | errno = -err->error; |
Sven Anders | 24f3818 | 2009-11-10 09:07:26 -0800 | [diff] [blame] | 212 | return -1; |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 213 | } |
Stephen Hemminger | 54bb35c | 2008-01-26 11:09:42 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len) |
| 220 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 221 | struct nlmsghdr nlh = { |
| 222 | .nlmsg_len = NLMSG_LENGTH(len), |
| 223 | .nlmsg_type = type, |
| 224 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 225 | .nlmsg_seq = rth->dump = ++rth->seq, |
| 226 | }; |
Stephen Hemminger | 6cf8398 | 2011-12-23 10:40:04 -0800 | [diff] [blame] | 227 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 228 | struct iovec iov[2] = { |
| 229 | { .iov_base = &nlh, .iov_len = sizeof(nlh) }, |
| 230 | { .iov_base = req, .iov_len = len } |
| 231 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 232 | struct msghdr msg = { |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 233 | .msg_name = &nladdr, |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 234 | .msg_namelen = sizeof(nladdr), |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 235 | .msg_iov = iov, |
| 236 | .msg_iovlen = 2, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 239 | return sendmsg(rth->fd, &msg, 0); |
| 240 | } |
| 241 | |
David Ahern | 0d238ca | 2015-10-02 09:42:27 -0700 | [diff] [blame] | 242 | int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n) |
| 243 | { |
| 244 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
| 245 | struct iovec iov = { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 246 | .iov_base = n, |
David Ahern | 0d238ca | 2015-10-02 09:42:27 -0700 | [diff] [blame] | 247 | .iov_len = n->nlmsg_len |
| 248 | }; |
| 249 | struct msghdr msg = { |
| 250 | .msg_name = &nladdr, |
| 251 | .msg_namelen = sizeof(nladdr), |
| 252 | .msg_iov = &iov, |
| 253 | .msg_iovlen = 1, |
| 254 | }; |
| 255 | |
| 256 | n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST; |
| 257 | n->nlmsg_pid = 0; |
| 258 | n->nlmsg_seq = rth->dump = ++rth->seq; |
| 259 | |
| 260 | return sendmsg(rth->fd, &msg, 0); |
| 261 | } |
| 262 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 263 | int rtnl_dump_filter_l(struct rtnl_handle *rth, |
| 264 | const struct rtnl_dump_filter_arg *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 265 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 266 | struct sockaddr_nl nladdr; |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 267 | struct iovec iov; |
| 268 | struct msghdr msg = { |
| 269 | .msg_name = &nladdr, |
| 270 | .msg_namelen = sizeof(nladdr), |
| 271 | .msg_iov = &iov, |
| 272 | .msg_iovlen = 1, |
| 273 | }; |
Phil Sutter | 72b365e | 2016-03-04 19:57:28 +0100 | [diff] [blame] | 274 | char buf[32768]; |
Nicolas Dichtel | 16f02e1 | 2013-03-22 06:34:02 +0000 | [diff] [blame] | 275 | int dump_intr = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 276 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 277 | iov.iov_base = buf; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 278 | while (1) { |
| 279 | int status; |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 280 | const struct rtnl_dump_filter_arg *a; |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 281 | int found_done = 0; |
| 282 | int msglen = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 283 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 284 | iov.iov_len = sizeof(buf); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 285 | status = recvmsg(rth->fd, &msg, 0); |
| 286 | |
| 287 | if (status < 0) { |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 288 | if (errno == EINTR || errno == EAGAIN) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 289 | continue; |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 290 | fprintf(stderr, "netlink receive error %s (%d)\n", |
| 291 | strerror(errno), errno); |
| 292 | return -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 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 295 | if (status == 0) { |
| 296 | fprintf(stderr, "EOF on netlink\n"); |
| 297 | return -1; |
| 298 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 299 | |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 300 | if (rth->dump_fp) |
| 301 | fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp); |
| 302 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 303 | for (a = arg; a->filter; a++) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 304 | struct nlmsghdr *h = (struct nlmsghdr *)buf; |
| 305 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 306 | msglen = status; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 307 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 308 | while (NLMSG_OK(h, msglen)) { |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 309 | int err = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 310 | |
Phil Sutter | 8e72880 | 2015-11-24 15:31:01 +0100 | [diff] [blame] | 311 | h->nlmsg_flags &= ~a->nc_flags; |
| 312 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 313 | if (nladdr.nl_pid != 0 || |
| 314 | h->nlmsg_pid != rth->local.nl_pid || |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 315 | h->nlmsg_seq != rth->dump) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 316 | goto skip_it; |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 317 | |
Nicolas Dichtel | 16f02e1 | 2013-03-22 06:34:02 +0000 | [diff] [blame] | 318 | if (h->nlmsg_flags & NLM_F_DUMP_INTR) |
| 319 | dump_intr = 1; |
| 320 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 321 | if (h->nlmsg_type == NLMSG_DONE) { |
| 322 | found_done = 1; |
| 323 | break; /* process next filter */ |
| 324 | } |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 325 | if (h->nlmsg_type == NLMSG_ERROR) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 326 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); |
| 327 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 328 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
| 329 | fprintf(stderr, |
| 330 | "ERROR truncated\n"); |
| 331 | } else { |
| 332 | errno = -err->error; |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 333 | if (rth->proto == NETLINK_SOCK_DIAG && |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 334 | (errno == ENOENT || |
| 335 | errno == EOPNOTSUPP)) |
vadimk | 8a4025f | 2014-12-04 12:32:58 +0200 | [diff] [blame] | 336 | return -1; |
| 337 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 338 | perror("RTNETLINK answers"); |
| 339 | } |
| 340 | return -1; |
| 341 | } |
Vadim Kochan | 486ccd9 | 2014-12-26 04:26:27 +0200 | [diff] [blame] | 342 | |
| 343 | if (!rth->dump_fp) { |
| 344 | err = a->filter(&nladdr, h, a->arg1); |
| 345 | if (err < 0) |
| 346 | return err; |
| 347 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 348 | |
| 349 | skip_it: |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 350 | h = NLMSG_NEXT(h, msglen); |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 351 | } |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Nicolas Dichtel | 16f02e1 | 2013-03-22 06:34:02 +0000 | [diff] [blame] | 354 | if (found_done) { |
| 355 | if (dump_intr) |
| 356 | fprintf(stderr, |
| 357 | "Dump was interrupted and may be inconsistent.\n"); |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 358 | return 0; |
Nicolas Dichtel | 16f02e1 | 2013-03-22 06:34:02 +0000 | [diff] [blame] | 359 | } |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 360 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 361 | if (msg.msg_flags & MSG_TRUNC) { |
| 362 | fprintf(stderr, "Message truncated\n"); |
| 363 | continue; |
| 364 | } |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 365 | if (msglen) { |
| 366 | fprintf(stderr, "!!!Remnant of size %d\n", msglen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 367 | exit(1); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
Phil Sutter | 8e72880 | 2015-11-24 15:31:01 +0100 | [diff] [blame] | 372 | int rtnl_dump_filter_nc(struct rtnl_handle *rth, |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 373 | rtnl_filter_t filter, |
Phil Sutter | 8e72880 | 2015-11-24 15:31:01 +0100 | [diff] [blame] | 374 | void *arg1, __u16 nc_flags) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 375 | { |
| 376 | const struct rtnl_dump_filter_arg a[2] = { |
Phil Sutter | 8e72880 | 2015-11-24 15:31:01 +0100 | [diff] [blame] | 377 | { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, }, |
| 378 | { .filter = NULL, .arg1 = NULL, .nc_flags = 0, }, |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | return rtnl_dump_filter_l(rth, a); |
| 382 | } |
| 383 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 384 | int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, |
Nicolas Dichtel | ed108cf | 2015-12-03 17:13:48 +0100 | [diff] [blame] | 385 | struct nlmsghdr *answer, size_t maxlen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 386 | { |
| 387 | int status; |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 388 | unsigned int seq; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 389 | struct nlmsghdr *h; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 390 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
shemminger | fb22975 | 2005-10-04 23:15:32 +0000 | [diff] [blame] | 391 | struct iovec iov = { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 392 | .iov_base = n, |
shemminger | fb22975 | 2005-10-04 23:15:32 +0000 | [diff] [blame] | 393 | .iov_len = n->nlmsg_len |
| 394 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 395 | struct msghdr msg = { |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 396 | .msg_name = &nladdr, |
| 397 | .msg_namelen = sizeof(nladdr), |
| 398 | .msg_iov = &iov, |
| 399 | .msg_iovlen = 1, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 400 | }; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 401 | char buf[32768] = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 402 | |
| 403 | n->nlmsg_seq = seq = ++rtnl->seq; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 404 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 405 | if (answer == NULL) |
| 406 | n->nlmsg_flags |= NLM_F_ACK; |
| 407 | |
| 408 | status = sendmsg(rtnl->fd, &msg, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 409 | if (status < 0) { |
| 410 | perror("Cannot talk to rtnetlink"); |
| 411 | return -1; |
| 412 | } |
| 413 | |
| 414 | iov.iov_base = buf; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 415 | while (1) { |
| 416 | iov.iov_len = sizeof(buf); |
| 417 | status = recvmsg(rtnl->fd, &msg, 0); |
| 418 | |
| 419 | if (status < 0) { |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 420 | if (errno == EINTR || errno == EAGAIN) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 421 | continue; |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 422 | fprintf(stderr, "netlink receive error %s (%d)\n", |
| 423 | strerror(errno), errno); |
| 424 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 425 | } |
| 426 | if (status == 0) { |
| 427 | fprintf(stderr, "EOF on netlink\n"); |
| 428 | return -1; |
| 429 | } |
| 430 | if (msg.msg_namelen != sizeof(nladdr)) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 431 | fprintf(stderr, |
| 432 | "sender address length == %d\n", |
| 433 | msg.msg_namelen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 434 | exit(1); |
| 435 | } |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 436 | for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 437 | int len = h->nlmsg_len; |
| 438 | int l = len - sizeof(*h); |
| 439 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 440 | if (l < 0 || len > status) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 441 | if (msg.msg_flags & MSG_TRUNC) { |
| 442 | fprintf(stderr, "Truncated message\n"); |
| 443 | return -1; |
| 444 | } |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 445 | fprintf(stderr, |
| 446 | "!!!malformed message: len=%d\n", |
| 447 | len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 448 | exit(1); |
| 449 | } |
| 450 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 451 | if (nladdr.nl_pid != 0 || |
org[shemminger]!shemminger | 10f57ef | 2004-06-07 22:04:04 +0000 | [diff] [blame] | 452 | h->nlmsg_pid != rtnl->local.nl_pid || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 453 | h->nlmsg_seq != seq) { |
shemminger | 4cca16f | 2006-03-10 23:31:46 +0000 | [diff] [blame] | 454 | /* Don't forget to skip that message. */ |
| 455 | status -= NLMSG_ALIGN(len); |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 456 | h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 457 | continue; |
| 458 | } |
| 459 | |
| 460 | if (h->nlmsg_type == NLMSG_ERROR) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 461 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h); |
| 462 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 463 | if (l < sizeof(struct nlmsgerr)) { |
| 464 | fprintf(stderr, "ERROR truncated\n"); |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 465 | } else if (!err->error) { |
| 466 | if (answer) |
| 467 | memcpy(answer, h, |
Nicolas Dichtel | ed108cf | 2015-12-03 17:13:48 +0100 | [diff] [blame] | 468 | MIN(maxlen, h->nlmsg_len)); |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 469 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 470 | } |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 471 | |
Lorenzo Colitti | 57fdf2d | 2016-01-08 17:32:36 +0900 | [diff] [blame] | 472 | if (rtnl->proto != NETLINK_SOCK_DIAG) |
| 473 | fprintf(stderr, |
| 474 | "RTNETLINK answers: %s\n", |
| 475 | strerror(-err->error)); |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 476 | errno = -err->error; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 477 | return -1; |
| 478 | } |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 479 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 480 | if (answer) { |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 481 | memcpy(answer, h, |
Nicolas Dichtel | ed108cf | 2015-12-03 17:13:48 +0100 | [diff] [blame] | 482 | MIN(maxlen, h->nlmsg_len)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | fprintf(stderr, "Unexpected reply!!!\n"); |
| 487 | |
| 488 | status -= NLMSG_ALIGN(len); |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 489 | h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 490 | } |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 491 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 492 | if (msg.msg_flags & MSG_TRUNC) { |
| 493 | fprintf(stderr, "Message truncated\n"); |
| 494 | continue; |
| 495 | } |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 496 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 497 | if (status) { |
| 498 | fprintf(stderr, "!!!Remnant of size %d\n", status); |
| 499 | exit(1); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Nicolas Dichtel | 449b824 | 2015-05-20 16:20:00 +0200 | [diff] [blame] | 504 | int rtnl_listen_all_nsid(struct rtnl_handle *rth) |
| 505 | { |
| 506 | unsigned int on = 1; |
| 507 | |
| 508 | if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on, |
| 509 | sizeof(on)) < 0) { |
| 510 | perror("NETLINK_LISTEN_ALL_NSID"); |
| 511 | return -1; |
| 512 | } |
| 513 | rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID; |
| 514 | return 0; |
| 515 | } |
| 516 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 517 | int rtnl_listen(struct rtnl_handle *rtnl, |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 518 | rtnl_listen_filter_t handler, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 519 | void *jarg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 520 | { |
| 521 | int status; |
| 522 | struct nlmsghdr *h; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 523 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 524 | struct iovec iov; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 525 | struct msghdr msg = { |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 526 | .msg_name = &nladdr, |
| 527 | .msg_namelen = sizeof(nladdr), |
| 528 | .msg_iov = &iov, |
| 529 | .msg_iovlen = 1, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 530 | }; |
Eric Dumazet | e557212 | 2014-10-11 09:43:13 -0700 | [diff] [blame] | 531 | char buf[16384]; |
Nicolas Dichtel | 449b824 | 2015-05-20 16:20:00 +0200 | [diff] [blame] | 532 | char cmsgbuf[BUFSIZ]; |
| 533 | |
| 534 | if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) { |
| 535 | msg.msg_control = &cmsgbuf; |
| 536 | msg.msg_controllen = sizeof(cmsgbuf); |
| 537 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 538 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 539 | iov.iov_base = buf; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 540 | while (1) { |
Nicolas Dichtel | 449b824 | 2015-05-20 16:20:00 +0200 | [diff] [blame] | 541 | struct rtnl_ctrl_data ctrl; |
| 542 | struct cmsghdr *cmsg; |
| 543 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 544 | iov.iov_len = sizeof(buf); |
| 545 | status = recvmsg(rtnl->fd, &msg, 0); |
| 546 | |
| 547 | if (status < 0) { |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 548 | if (errno == EINTR || errno == EAGAIN) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 549 | continue; |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 550 | fprintf(stderr, "netlink receive error %s (%d)\n", |
| 551 | strerror(errno), errno); |
Patrick McHardy | 7f03191 | 2009-10-28 20:50:48 +0100 | [diff] [blame] | 552 | if (errno == ENOBUFS) |
| 553 | continue; |
Stephen Hemminger | aa8032e | 2008-01-25 15:39:09 -0800 | [diff] [blame] | 554 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 555 | } |
| 556 | if (status == 0) { |
| 557 | fprintf(stderr, "EOF on netlink\n"); |
| 558 | return -1; |
| 559 | } |
| 560 | if (msg.msg_namelen != sizeof(nladdr)) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 561 | fprintf(stderr, |
| 562 | "Sender address length == %d\n", |
| 563 | msg.msg_namelen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 564 | exit(1); |
| 565 | } |
Nicolas Dichtel | 449b824 | 2015-05-20 16:20:00 +0200 | [diff] [blame] | 566 | |
| 567 | if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) { |
| 568 | memset(&ctrl, 0, sizeof(ctrl)); |
| 569 | ctrl.nsid = -1; |
| 570 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; |
| 571 | cmsg = CMSG_NXTHDR(&msg, cmsg)) |
| 572 | if (cmsg->cmsg_level == SOL_NETLINK && |
| 573 | cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID && |
| 574 | cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { |
| 575 | int *data = (int *)CMSG_DATA(cmsg); |
| 576 | |
| 577 | ctrl.nsid = *data; |
| 578 | } |
| 579 | } |
| 580 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 581 | for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 582 | int err; |
| 583 | int len = h->nlmsg_len; |
| 584 | int l = len - sizeof(*h); |
| 585 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 586 | if (l < 0 || len > status) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 587 | if (msg.msg_flags & MSG_TRUNC) { |
| 588 | fprintf(stderr, "Truncated message\n"); |
| 589 | return -1; |
| 590 | } |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 591 | fprintf(stderr, |
| 592 | "!!!malformed message: len=%d\n", |
| 593 | len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 594 | exit(1); |
| 595 | } |
| 596 | |
Nicolas Dichtel | 449b824 | 2015-05-20 16:20:00 +0200 | [diff] [blame] | 597 | err = handler(&nladdr, &ctrl, h, jarg); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 598 | if (err < 0) |
| 599 | return err; |
| 600 | |
| 601 | status -= NLMSG_ALIGN(len); |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 602 | h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 603 | } |
| 604 | if (msg.msg_flags & MSG_TRUNC) { |
| 605 | fprintf(stderr, "Message truncated\n"); |
| 606 | continue; |
| 607 | } |
| 608 | if (status) { |
| 609 | fprintf(stderr, "!!!Remnant of size %d\n", status); |
| 610 | exit(1); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 615 | int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 616 | void *jarg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 617 | { |
| 618 | int status; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 619 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 620 | char buf[16384]; |
| 621 | struct nlmsghdr *h = (struct nlmsghdr *)buf; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 622 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 623 | while (1) { |
Stephen Hemminger | 2dd9f8e | 2011-06-20 14:34:46 -0700 | [diff] [blame] | 624 | int err, len; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 625 | int l; |
| 626 | |
| 627 | status = fread(&buf, 1, sizeof(*h), rtnl); |
| 628 | |
| 629 | if (status < 0) { |
| 630 | if (errno == EINTR) |
| 631 | continue; |
| 632 | perror("rtnl_from_file: fread"); |
| 633 | return -1; |
| 634 | } |
| 635 | if (status == 0) |
| 636 | return 0; |
| 637 | |
| 638 | len = h->nlmsg_len; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 639 | l = len - sizeof(*h); |
| 640 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 641 | if (l < 0 || len > sizeof(buf)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 642 | fprintf(stderr, "!!!malformed message: len=%d @%lu\n", |
| 643 | len, ftell(rtnl)); |
| 644 | return -1; |
| 645 | } |
| 646 | |
| 647 | status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl); |
| 648 | |
| 649 | if (status < 0) { |
| 650 | perror("rtnl_from_file: fread"); |
| 651 | return -1; |
| 652 | } |
| 653 | if (status < l) { |
| 654 | fprintf(stderr, "rtnl-from_file: truncated message\n"); |
| 655 | return -1; |
| 656 | } |
| 657 | |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 658 | err = handler(&nladdr, NULL, h, jarg); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 659 | if (err < 0) |
| 660 | return err; |
| 661 | } |
| 662 | } |
| 663 | |
Stephen Hemminger | 2aa3dd2 | 2011-12-23 10:43:54 -0800 | [diff] [blame] | 664 | int addattr(struct nlmsghdr *n, int maxlen, int type) |
| 665 | { |
| 666 | return addattr_l(n, maxlen, type, NULL, 0); |
| 667 | } |
| 668 | |
| 669 | int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data) |
| 670 | { |
| 671 | return addattr_l(n, maxlen, type, &data, sizeof(__u8)); |
| 672 | } |
| 673 | |
| 674 | int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data) |
| 675 | { |
| 676 | return addattr_l(n, maxlen, type, &data, sizeof(__u16)); |
| 677 | } |
| 678 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 679 | int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data) |
| 680 | { |
Stephen Hemminger | 2aa3dd2 | 2011-12-23 10:43:54 -0800 | [diff] [blame] | 681 | return addattr_l(n, maxlen, type, &data, sizeof(__u32)); |
| 682 | } |
| 683 | |
| 684 | int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data) |
| 685 | { |
| 686 | return addattr_l(n, maxlen, type, &data, sizeof(__u64)); |
| 687 | } |
| 688 | |
| 689 | int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str) |
| 690 | { |
| 691 | return addattr_l(n, maxlen, type, str, strlen(str)+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 692 | } |
| 693 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 694 | 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] | 695 | int alen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 696 | { |
| 697 | int len = RTA_LENGTH(alen); |
| 698 | struct rtattr *rta; |
| 699 | |
net[shemminger]!shemminger | 3dabdbb | 2005-03-30 18:18:12 +0000 | [diff] [blame] | 700 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 701 | fprintf(stderr, |
| 702 | "addattr_l ERROR: message exceeded bound of %d\n", |
| 703 | maxlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 704 | return -1; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 705 | } |
6!tgraf | 07f9436 | 2005-01-18 13:58:49 +0000 | [diff] [blame] | 706 | rta = NLMSG_TAIL(n); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 707 | rta->rta_type = type; |
| 708 | rta->rta_len = len; |
| 709 | memcpy(RTA_DATA(rta), data, alen); |
net[shemminger]!shemminger | 3dabdbb | 2005-03-30 18:18:12 +0000 | [diff] [blame] | 710 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 711 | return 0; |
| 712 | } |
| 713 | |
6!tgraf | 07f9436 | 2005-01-18 13:58:49 +0000 | [diff] [blame] | 714 | int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len) |
| 715 | { |
| 716 | if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 717 | fprintf(stderr, |
| 718 | "addraw_l ERROR: message exceeded bound of %d\n", |
| 719 | maxlen); |
6!tgraf | 07f9436 | 2005-01-18 13:58:49 +0000 | [diff] [blame] | 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | memcpy(NLMSG_TAIL(n), data, len); |
| 724 | memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len); |
| 725 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len); |
| 726 | return 0; |
| 727 | } |
| 728 | |
Patrick McHardy | 2f90c9c | 2007-08-14 11:21:19 -0700 | [diff] [blame] | 729 | struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type) |
| 730 | { |
| 731 | struct rtattr *nest = NLMSG_TAIL(n); |
| 732 | |
| 733 | addattr_l(n, maxlen, type, NULL, 0); |
| 734 | return nest; |
| 735 | } |
| 736 | |
| 737 | int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest) |
| 738 | { |
| 739 | nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest; |
| 740 | return n->nlmsg_len; |
| 741 | } |
| 742 | |
| 743 | struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type, |
| 744 | const void *data, int len) |
| 745 | { |
| 746 | struct rtattr *start = NLMSG_TAIL(n); |
| 747 | |
| 748 | addattr_l(n, maxlen, type, data, len); |
| 749 | addattr_nest(n, maxlen, type); |
| 750 | return start; |
| 751 | } |
| 752 | |
| 753 | int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start) |
| 754 | { |
| 755 | struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len); |
| 756 | |
| 757 | start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start; |
| 758 | addattr_nest_end(n, nest); |
| 759 | return n->nlmsg_len; |
| 760 | } |
| 761 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 762 | int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data) |
| 763 | { |
| 764 | int len = RTA_LENGTH(4); |
| 765 | struct rtattr *subrta; |
| 766 | |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 767 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 768 | fprintf(stderr, |
| 769 | "rta_addattr32: Error! max allowed bound %d exceeded\n", |
| 770 | maxlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 771 | return -1; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 772 | } |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 773 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 774 | subrta->rta_type = type; |
| 775 | subrta->rta_len = len; |
| 776 | memcpy(RTA_DATA(subrta), &data, 4); |
| 777 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len; |
| 778 | return 0; |
| 779 | } |
| 780 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 781 | int rta_addattr_l(struct rtattr *rta, int maxlen, int type, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 782 | const void *data, int alen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 783 | { |
| 784 | struct rtattr *subrta; |
| 785 | int len = RTA_LENGTH(alen); |
| 786 | |
net[shemminger]!shemminger | 3dabdbb | 2005-03-30 18:18:12 +0000 | [diff] [blame] | 787 | if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) { |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 788 | fprintf(stderr, |
| 789 | "rta_addattr_l: Error! max allowed bound %d exceeded\n", |
| 790 | maxlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 791 | return -1; |
osdl.net!shemminger | 007d3a3 | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 792 | } |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 793 | subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 794 | subrta->rta_type = type; |
| 795 | subrta->rta_len = len; |
| 796 | memcpy(RTA_DATA(subrta), data, alen); |
net[shemminger]!shemminger | 3dabdbb | 2005-03-30 18:18:12 +0000 | [diff] [blame] | 797 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 798 | return 0; |
| 799 | } |
| 800 | |
Roopa Prabhu | 303cc9c | 2015-10-15 13:13:38 +0200 | [diff] [blame] | 801 | int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data) |
| 802 | { |
| 803 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8)); |
| 804 | } |
| 805 | |
| 806 | int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data) |
| 807 | { |
| 808 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16)); |
| 809 | } |
| 810 | |
| 811 | int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data) |
| 812 | { |
| 813 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64)); |
| 814 | } |
| 815 | |
| 816 | struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type) |
| 817 | { |
| 818 | struct rtattr *nest = RTA_TAIL(rta); |
| 819 | |
| 820 | rta_addattr_l(rta, maxlen, type, NULL, 0); |
| 821 | |
| 822 | return nest; |
| 823 | } |
| 824 | |
| 825 | int rta_nest_end(struct rtattr *rta, struct rtattr *nest) |
| 826 | { |
| 827 | nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest; |
| 828 | |
| 829 | return rta->rta_len; |
| 830 | } |
| 831 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 832 | int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) |
| 833 | { |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 834 | return parse_rtattr_flags(tb, max, rta, len, 0); |
| 835 | } |
| 836 | |
| 837 | int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta, |
| 838 | int len, unsigned short flags) |
| 839 | { |
| 840 | unsigned short type; |
| 841 | |
osdl.net!shemminger | 175e244 | 2005-02-07 18:17:38 +0000 | [diff] [blame] | 842 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 843 | while (RTA_OK(rta, len)) { |
Vlad Yasevich | b1b7ce0 | 2013-03-15 10:01:28 -0700 | [diff] [blame] | 844 | type = rta->rta_type & ~flags; |
| 845 | if ((type <= max) && (!tb[type])) |
| 846 | tb[type] = rta; |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 847 | rta = RTA_NEXT(rta, len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 848 | } |
| 849 | if (len) |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 850 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", |
| 851 | len, rta->rta_len); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 852 | return 0; |
| 853 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 854 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 855 | int parse_rtattr_byindex(struct rtattr *tb[], int max, |
| 856 | struct rtattr *rta, int len) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 857 | { |
| 858 | int i = 0; |
osdl.net!shemminger | 175e244 | 2005-02-07 18:17:38 +0000 | [diff] [blame] | 859 | |
| 860 | memset(tb, 0, sizeof(struct rtattr *) * max); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 861 | while (RTA_OK(rta, len)) { |
osdl.net!shemminger | 175e244 | 2005-02-07 18:17:38 +0000 | [diff] [blame] | 862 | if (rta->rta_type <= max && i < max) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 863 | tb[i++] = rta; |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 864 | rta = RTA_NEXT(rta, len); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 865 | } |
| 866 | if (len) |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 867 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", |
| 868 | len, rta->rta_len); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 869 | return i; |
| 870 | } |
Patrick McHardy | 2f90c9c | 2007-08-14 11:21:19 -0700 | [diff] [blame] | 871 | |
Jiri Pirko | decbb43 | 2015-01-06 17:23:45 +0100 | [diff] [blame] | 872 | struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len) |
| 873 | { |
| 874 | while (RTA_OK(rta, len)) { |
| 875 | if (rta->rta_type == type) |
| 876 | return rta; |
| 877 | rta = RTA_NEXT(rta, len); |
| 878 | } |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 879 | |
Jiri Pirko | decbb43 | 2015-01-06 17:23:45 +0100 | [diff] [blame] | 880 | if (len) |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 881 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", |
| 882 | len, rta->rta_len); |
Jiri Pirko | decbb43 | 2015-01-06 17:23:45 +0100 | [diff] [blame] | 883 | return NULL; |
| 884 | } |
| 885 | |
Stephen Hemminger | 2c500a4 | 2016-11-29 13:15:08 -0800 | [diff] [blame] | 886 | int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, |
| 887 | struct rtattr *rta, |
| 888 | int len) |
Patrick McHardy | 2f90c9c | 2007-08-14 11:21:19 -0700 | [diff] [blame] | 889 | { |
| 890 | if (RTA_PAYLOAD(rta) < len) |
| 891 | return -1; |
| 892 | if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) { |
| 893 | rta = RTA_DATA(rta) + RTA_ALIGN(len); |
| 894 | return parse_rtattr_nested(tb, max, rta); |
| 895 | } |
Stephen Hemminger | 037c635 | 2007-12-10 11:34:40 -0800 | [diff] [blame] | 896 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); |
Patrick McHardy | 2f90c9c | 2007-08-14 11:21:19 -0700 | [diff] [blame] | 897 | return 0; |
| 898 | } |