"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU General Public License |
| 5 | * as published by the Free Software Foundation; either version |
| 6 | * 2 of the License, or (at your option) any later version. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 7 | * |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 9 | */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 10 | #include <sys/socket.h> |
Eric Andersen | 0f08e53 | 2003-06-20 09:05:00 +0000 | [diff] [blame] | 11 | #include <sys/uio.h> |
| 12 | |
Denis Vlasenko | fad2b86 | 2007-05-31 22:16:38 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 14 | #include "libnetlink.h" |
| 15 | |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 16 | void FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | { |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 18 | memset(rth, 0, sizeof(*rth)); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 19 | rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 20 | rth->local.nl_family = AF_NETLINK; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 21 | /*rth->local.nl_groups = subscriptions;*/ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 22 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 23 | xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)); |
Denys Vlasenko | ba3b9db | 2018-02-11 14:55:46 +0100 | [diff] [blame] | 24 | bb_getsockname(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)); |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 25 | |
| 26 | /* too much paranoia |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 27 | if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) |
Bernhard Reutner-Fischer | d73cbd3 | 2008-07-21 14:41:33 +0000 | [diff] [blame] | 28 | bb_perror_msg_and_die("getsockname"); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 29 | if (addr_len != sizeof(rth->local)) |
| 30 | bb_error_msg_and_die("wrong address length %d", addr_len); |
| 31 | if (rth->local.nl_family != AF_NETLINK) |
| 32 | bb_error_msg_and_die("wrong address family %d", rth->local.nl_family); |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 33 | */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 34 | rth->seq = time(NULL); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Denys Vlasenko | 028c5aa | 2019-05-22 13:54:46 +0200 | [diff] [blame] | 37 | void FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 38 | { |
| 39 | struct { |
| 40 | struct nlmsghdr nlh; |
| 41 | struct rtgenmsg g; |
| 42 | } req; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 43 | |
| 44 | req.nlh.nlmsg_len = sizeof(req); |
| 45 | req.nlh.nlmsg_type = type; |
| 46 | req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; |
| 47 | req.nlh.nlmsg_pid = 0; |
| 48 | req.nlh.nlmsg_seq = rth->dump = ++rth->seq; |
| 49 | req.g.rtgen_family = family; |
| 50 | |
Denys Vlasenko | 028c5aa | 2019-05-22 13:54:46 +0200 | [diff] [blame] | 51 | rtnl_send(rth, (void*)&req, sizeof(req)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Denys Vlasenko | 028c5aa | 2019-05-22 13:54:46 +0200 | [diff] [blame] | 54 | /* A version which checks for e.g. EPERM errors. |
| 55 | * Try: setuidgid 1:1 ip addr flush dev eth0 |
| 56 | */ |
| 57 | int FAST_FUNC rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 58 | { |
Denys Vlasenko | 028c5aa | 2019-05-22 13:54:46 +0200 | [diff] [blame] | 59 | struct nlmsghdr *h; |
| 60 | int status; |
| 61 | char resp[1024]; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 62 | |
Denys Vlasenko | 028c5aa | 2019-05-22 13:54:46 +0200 | [diff] [blame] | 63 | status = write(rth->fd, buf, len); |
| 64 | if (status < 0) |
| 65 | return status; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 66 | |
Denys Vlasenko | 028c5aa | 2019-05-22 13:54:46 +0200 | [diff] [blame] | 67 | /* Check for immediate errors */ |
| 68 | status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK); |
| 69 | if (status < 0) { |
| 70 | if (errno == EAGAIN) /* if no error, this happens */ |
| 71 | return 0; |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | for (h = (struct nlmsghdr *)resp; |
| 76 | NLMSG_OK(h, status); |
| 77 | h = NLMSG_NEXT(h, status) |
| 78 | ) { |
| 79 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 80 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
| 81 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 82 | bb_simple_error_msg("ERROR truncated"); |
Denys Vlasenko | 028c5aa | 2019-05-22 13:54:46 +0200 | [diff] [blame] | 83 | else |
| 84 | errno = -err->error; |
| 85 | return -1; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 92 | int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 93 | { |
Denys Vlasenko | 4548293 | 2016-08-14 02:08:56 +0200 | [diff] [blame] | 94 | struct { |
| 95 | struct nlmsghdr nlh; |
| 96 | struct msghdr msg; |
| 97 | struct sockaddr_nl nladdr; |
| 98 | } s; |
| 99 | struct iovec iov[2] = { { &s.nlh, sizeof(s.nlh) }, { req, len } }; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 100 | |
Denys Vlasenko | 4548293 | 2016-08-14 02:08:56 +0200 | [diff] [blame] | 101 | memset(&s, 0, sizeof(s)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 102 | |
Denys Vlasenko | 4548293 | 2016-08-14 02:08:56 +0200 | [diff] [blame] | 103 | s.msg.msg_name = (void*)&s.nladdr; |
| 104 | s.msg.msg_namelen = sizeof(s.nladdr); |
| 105 | s.msg.msg_iov = iov; |
| 106 | s.msg.msg_iovlen = 2; |
| 107 | /*s.msg.msg_control = NULL; - already is */ |
| 108 | /*s.msg.msg_controllen = 0; - already is */ |
| 109 | /*s.msg.msg_flags = 0; - already is */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | |
Denys Vlasenko | 4548293 | 2016-08-14 02:08:56 +0200 | [diff] [blame] | 111 | s.nladdr.nl_family = AF_NETLINK; |
| 112 | |
| 113 | s.nlh.nlmsg_len = NLMSG_LENGTH(len); |
| 114 | s.nlh.nlmsg_type = type; |
| 115 | s.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; |
| 116 | /*s.nlh.nlmsg_pid = 0; - already is */ |
| 117 | s.nlh.nlmsg_seq = rth->dump = ++rth->seq; |
| 118 | |
| 119 | return sendmsg(rth->fd, &s.msg, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 122 | static int rtnl_dump_filter(struct rtnl_handle *rth, |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 123 | int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC, |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 124 | void *arg1/*, |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 125 | int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *), |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 126 | void *arg2*/) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 127 | { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 128 | int retval = -1; |
| 129 | char *buf = xmalloc(8*1024); /* avoid big stack buffer */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 130 | struct sockaddr_nl nladdr; |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 131 | struct iovec iov = { buf, 8*1024 }; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 132 | |
| 133 | while (1) { |
| 134 | int status; |
| 135 | struct nlmsghdr *h; |
Szabolcs Nagy | 4ab372d | 2016-04-24 17:39:02 +0200 | [diff] [blame] | 136 | /* Use designated initializers, struct layout is non-portable */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 137 | struct msghdr msg = { |
Szabolcs Nagy | 4ab372d | 2016-04-24 17:39:02 +0200 | [diff] [blame] | 138 | .msg_name = (void*)&nladdr, |
| 139 | .msg_namelen = sizeof(nladdr), |
| 140 | .msg_iov = &iov, |
| 141 | .msg_iovlen = 1, |
| 142 | .msg_control = NULL, |
| 143 | .msg_controllen = 0, |
| 144 | .msg_flags = 0 |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | status = recvmsg(rth->fd, &msg, 0); |
| 148 | |
| 149 | if (status < 0) { |
| 150 | if (errno == EINTR) |
| 151 | continue; |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 152 | bb_simple_perror_msg("OVERRUN"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 153 | continue; |
| 154 | } |
| 155 | if (status == 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 156 | bb_simple_error_msg("EOF on netlink"); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 157 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 158 | } |
| 159 | if (msg.msg_namelen != sizeof(nladdr)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 160 | bb_error_msg_and_die("sender address length == %d", msg.msg_namelen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | h = (struct nlmsghdr*)buf; |
| 164 | while (NLMSG_OK(h, status)) { |
| 165 | int err; |
| 166 | |
Eric Andersen | 6c8161d | 2004-05-05 07:05:32 +0000 | [diff] [blame] | 167 | if (nladdr.nl_pid != 0 || |
| 168 | h->nlmsg_pid != rth->local.nl_pid || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 169 | h->nlmsg_seq != rth->dump |
| 170 | ) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 171 | // if (junk) { |
| 172 | // err = junk(&nladdr, h, arg2); |
| 173 | // if (err < 0) { |
| 174 | // retval = err; |
| 175 | // goto ret; |
| 176 | // } |
| 177 | // } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 178 | goto skip_it; |
| 179 | } |
| 180 | |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 181 | if (h->nlmsg_type == NLMSG_DONE) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 182 | goto ret_0; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 183 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 184 | if (h->nlmsg_type == NLMSG_ERROR) { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 185 | struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 186 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 187 | bb_simple_error_msg("ERROR truncated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 188 | } else { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 189 | errno = -l_err->error; |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 190 | bb_simple_perror_msg("RTNETLINK answers"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 191 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 192 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 193 | } |
| 194 | err = filter(&nladdr, h, arg1); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 195 | if (err < 0) { |
| 196 | retval = err; |
| 197 | goto ret; |
| 198 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 199 | |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 200 | skip_it: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 201 | h = NLMSG_NEXT(h, status); |
| 202 | } |
| 203 | if (msg.msg_flags & MSG_TRUNC) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 204 | bb_simple_error_msg("message truncated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 205 | continue; |
| 206 | } |
| 207 | if (status) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 208 | bb_error_msg_and_die("remnant of size %d!", status); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 209 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 210 | } /* while (1) */ |
| 211 | ret_0: |
| 212 | retval++; /* = 0 */ |
| 213 | ret: |
| 214 | free(buf); |
| 215 | return retval; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 218 | int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth, |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 219 | int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC, |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 220 | void *arg1) |
| 221 | { |
| 222 | int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/); |
| 223 | if (ret < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 224 | bb_simple_error_msg_and_die("dump terminated"); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 225 | return ret; |
| 226 | } |
| 227 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 228 | int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 229 | pid_t peer, unsigned groups, |
| 230 | struct nlmsghdr *answer, |
| 231 | int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *), |
| 232 | void *jarg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 233 | { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 234 | /* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */ |
| 235 | #define peer 0 |
| 236 | #define groups 0 |
| 237 | #define junk NULL |
| 238 | #define jarg NULL |
| 239 | int retval = -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 240 | int status; |
| 241 | unsigned seq; |
| 242 | struct nlmsghdr *h; |
| 243 | struct sockaddr_nl nladdr; |
| 244 | struct iovec iov = { (void*)n, n->nlmsg_len }; |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 245 | char *buf = xmalloc(8*1024); /* avoid big stack buffer */ |
Szabolcs Nagy | 4ab372d | 2016-04-24 17:39:02 +0200 | [diff] [blame] | 246 | /* Use designated initializers, struct layout is non-portable */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 247 | struct msghdr msg = { |
Szabolcs Nagy | 4ab372d | 2016-04-24 17:39:02 +0200 | [diff] [blame] | 248 | .msg_name = (void*)&nladdr, |
| 249 | .msg_namelen = sizeof(nladdr), |
| 250 | .msg_iov = &iov, |
| 251 | .msg_iovlen = 1, |
| 252 | .msg_control = NULL, |
| 253 | .msg_controllen = 0, |
| 254 | .msg_flags = 0 |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | memset(&nladdr, 0, sizeof(nladdr)); |
| 258 | nladdr.nl_family = AF_NETLINK; |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 259 | // nladdr.nl_pid = peer; |
| 260 | // nladdr.nl_groups = groups; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 261 | |
| 262 | n->nlmsg_seq = seq = ++rtnl->seq; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 263 | if (answer == NULL) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 264 | n->nlmsg_flags |= NLM_F_ACK; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 265 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 266 | status = sendmsg(rtnl->fd, &msg, 0); |
| 267 | |
| 268 | if (status < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 269 | bb_simple_perror_msg("can't talk to rtnetlink"); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 270 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | iov.iov_base = buf; |
| 274 | |
| 275 | while (1) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 276 | iov.iov_len = 8*1024; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 277 | status = recvmsg(rtnl->fd, &msg, 0); |
| 278 | |
| 279 | if (status < 0) { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 280 | if (errno == EINTR) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 281 | continue; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 282 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 283 | bb_simple_perror_msg("OVERRUN"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 284 | continue; |
| 285 | } |
| 286 | if (status == 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 287 | bb_simple_error_msg("EOF on netlink"); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 288 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 289 | } |
| 290 | if (msg.msg_namelen != sizeof(nladdr)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 291 | bb_error_msg_and_die("sender address length == %d", msg.msg_namelen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 292 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 293 | for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 294 | // int l_err; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 295 | int len = h->nlmsg_len; |
| 296 | int l = len - sizeof(*h); |
| 297 | |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 298 | if (l < 0 || len > status) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 299 | if (msg.msg_flags & MSG_TRUNC) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 300 | bb_simple_error_msg("truncated message"); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 301 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 302 | } |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 303 | bb_error_msg_and_die("malformed message: len=%d!", len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Eric Andersen | 6c8161d | 2004-05-05 07:05:32 +0000 | [diff] [blame] | 306 | if (nladdr.nl_pid != peer || |
| 307 | h->nlmsg_pid != rtnl->local.nl_pid || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 308 | h->nlmsg_seq != seq |
| 309 | ) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 310 | // if (junk) { |
| 311 | // l_err = junk(&nladdr, h, jarg); |
| 312 | // if (l_err < 0) { |
| 313 | // retval = l_err; |
| 314 | // goto ret; |
| 315 | // } |
| 316 | // } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 317 | continue; |
| 318 | } |
| 319 | |
| 320 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 321 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 322 | if (l < (int)sizeof(struct nlmsgerr)) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 323 | bb_simple_error_msg("ERROR truncated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 324 | } else { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 325 | errno = - err->error; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 326 | if (errno == 0) { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 327 | if (answer) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 328 | memcpy(answer, h, h->nlmsg_len); |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 329 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 330 | goto ret_0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 331 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 332 | bb_simple_perror_msg("RTNETLINK answers"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 333 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 334 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 335 | } |
| 336 | if (answer) { |
| 337 | memcpy(answer, h, h->nlmsg_len); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 338 | goto ret_0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 339 | } |
| 340 | |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 341 | bb_simple_error_msg("unexpected reply!"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 342 | |
| 343 | status -= NLMSG_ALIGN(len); |
| 344 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); |
| 345 | } |
| 346 | if (msg.msg_flags & MSG_TRUNC) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 347 | bb_simple_error_msg("message truncated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 348 | continue; |
| 349 | } |
| 350 | if (status) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 351 | bb_error_msg_and_die("remnant of size %d!", status); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 352 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 353 | } /* while (1) */ |
| 354 | ret_0: |
| 355 | retval++; /* = 0 */ |
| 356 | ret: |
| 357 | free(buf); |
| 358 | return retval; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 361 | int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 362 | { |
| 363 | int len = RTA_LENGTH(4); |
| 364 | struct rtattr *rta; |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 365 | |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 366 | if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 367 | return -1; |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 368 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 369 | rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len)); |
| 370 | rta->rta_type = type; |
| 371 | rta->rta_len = len; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 372 | move_to_unaligned32(RTA_DATA(rta), data); |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 373 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 377 | int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 378 | { |
| 379 | int len = RTA_LENGTH(alen); |
| 380 | struct rtattr *rta; |
| 381 | |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 382 | if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 383 | return -1; |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 384 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 385 | rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len)); |
| 386 | rta->rta_type = type; |
| 387 | rta->rta_len = len; |
| 388 | memcpy(RTA_DATA(rta), data, alen); |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 389 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 393 | int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 394 | { |
| 395 | int len = RTA_LENGTH(4); |
| 396 | struct rtattr *subrta; |
| 397 | |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 398 | if (RTA_ALIGN(rta->rta_len + len) > maxlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 399 | return -1; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 400 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 401 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
| 402 | subrta->rta_type = type; |
| 403 | subrta->rta_len = len; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 404 | move_to_unaligned32(RTA_DATA(subrta), data); |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 405 | rta->rta_len = NLMSG_ALIGN(rta->rta_len + len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 409 | int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 410 | { |
| 411 | struct rtattr *subrta; |
| 412 | int len = RTA_LENGTH(alen); |
| 413 | |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 414 | if (RTA_ALIGN(rta->rta_len + len) > maxlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 415 | return -1; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 416 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 417 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
| 418 | subrta->rta_type = type; |
| 419 | subrta->rta_len = len; |
| 420 | memcpy(RTA_DATA(subrta), data, alen); |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 421 | rta->rta_len = NLMSG_ALIGN(rta->rta_len + len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 426 | void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 427 | { |
Denys Vlasenko | 68ae542 | 2018-02-08 08:42:37 +0100 | [diff] [blame] | 428 | memset(tb, 0, (max + 1) * sizeof(tb[0])); |
| 429 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 430 | while (RTA_OK(rta, len)) { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 431 | if (rta->rta_type <= max) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 432 | tb[rta->rta_type] = rta; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 433 | } |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 434 | rta = RTA_NEXT(rta, len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 435 | } |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 436 | if (len) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 437 | bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len); |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 438 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 439 | } |