"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 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 37 | int 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 | |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 51 | return rtnl_send(rth, (void*)&req, sizeof(req)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Denys Vlasenko | f133471 | 2011-02-09 04:39:09 +0100 | [diff] [blame] | 54 | //TODO: pass rth->fd instead of full rth? |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 55 | int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 56 | { |
| 57 | struct sockaddr_nl nladdr; |
| 58 | |
| 59 | memset(&nladdr, 0, sizeof(nladdr)); |
| 60 | nladdr.nl_family = AF_NETLINK; |
| 61 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 62 | return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 65 | 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] | 66 | { |
Denys Vlasenko | 4548293 | 2016-08-14 02:08:56 +0200 | [diff] [blame] | 67 | struct { |
| 68 | struct nlmsghdr nlh; |
| 69 | struct msghdr msg; |
| 70 | struct sockaddr_nl nladdr; |
| 71 | } s; |
| 72 | struct iovec iov[2] = { { &s.nlh, sizeof(s.nlh) }, { req, len } }; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 73 | |
Denys Vlasenko | 4548293 | 2016-08-14 02:08:56 +0200 | [diff] [blame] | 74 | memset(&s, 0, sizeof(s)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 75 | |
Denys Vlasenko | 4548293 | 2016-08-14 02:08:56 +0200 | [diff] [blame] | 76 | s.msg.msg_name = (void*)&s.nladdr; |
| 77 | s.msg.msg_namelen = sizeof(s.nladdr); |
| 78 | s.msg.msg_iov = iov; |
| 79 | s.msg.msg_iovlen = 2; |
| 80 | /*s.msg.msg_control = NULL; - already is */ |
| 81 | /*s.msg.msg_controllen = 0; - already is */ |
| 82 | /*s.msg.msg_flags = 0; - already is */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 83 | |
Denys Vlasenko | 4548293 | 2016-08-14 02:08:56 +0200 | [diff] [blame] | 84 | s.nladdr.nl_family = AF_NETLINK; |
| 85 | |
| 86 | s.nlh.nlmsg_len = NLMSG_LENGTH(len); |
| 87 | s.nlh.nlmsg_type = type; |
| 88 | s.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; |
| 89 | /*s.nlh.nlmsg_pid = 0; - already is */ |
| 90 | s.nlh.nlmsg_seq = rth->dump = ++rth->seq; |
| 91 | |
| 92 | return sendmsg(rth->fd, &s.msg, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 95 | static int rtnl_dump_filter(struct rtnl_handle *rth, |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 96 | 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] | 97 | void *arg1/*, |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 98 | int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *), |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 99 | void *arg2*/) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 100 | { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 101 | int retval = -1; |
| 102 | char *buf = xmalloc(8*1024); /* avoid big stack buffer */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 103 | struct sockaddr_nl nladdr; |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 104 | struct iovec iov = { buf, 8*1024 }; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 105 | |
| 106 | while (1) { |
| 107 | int status; |
| 108 | struct nlmsghdr *h; |
Szabolcs Nagy | 4ab372d | 2016-04-24 17:39:02 +0200 | [diff] [blame] | 109 | /* Use designated initializers, struct layout is non-portable */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | struct msghdr msg = { |
Szabolcs Nagy | 4ab372d | 2016-04-24 17:39:02 +0200 | [diff] [blame] | 111 | .msg_name = (void*)&nladdr, |
| 112 | .msg_namelen = sizeof(nladdr), |
| 113 | .msg_iov = &iov, |
| 114 | .msg_iovlen = 1, |
| 115 | .msg_control = NULL, |
| 116 | .msg_controllen = 0, |
| 117 | .msg_flags = 0 |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | status = recvmsg(rth->fd, &msg, 0); |
| 121 | |
| 122 | if (status < 0) { |
| 123 | if (errno == EINTR) |
| 124 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 125 | bb_perror_msg("OVERRUN"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 126 | continue; |
| 127 | } |
| 128 | if (status == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 129 | bb_error_msg("EOF on netlink"); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 130 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 131 | } |
| 132 | if (msg.msg_namelen != sizeof(nladdr)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 133 | 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] | 134 | } |
| 135 | |
| 136 | h = (struct nlmsghdr*)buf; |
| 137 | while (NLMSG_OK(h, status)) { |
| 138 | int err; |
| 139 | |
Eric Andersen | 6c8161d | 2004-05-05 07:05:32 +0000 | [diff] [blame] | 140 | if (nladdr.nl_pid != 0 || |
| 141 | h->nlmsg_pid != rth->local.nl_pid || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 142 | h->nlmsg_seq != rth->dump |
| 143 | ) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 144 | // if (junk) { |
| 145 | // err = junk(&nladdr, h, arg2); |
| 146 | // if (err < 0) { |
| 147 | // retval = err; |
| 148 | // goto ret; |
| 149 | // } |
| 150 | // } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 151 | goto skip_it; |
| 152 | } |
| 153 | |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 154 | if (h->nlmsg_type == NLMSG_DONE) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 155 | goto ret_0; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 156 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 157 | if (h->nlmsg_type == NLMSG_ERROR) { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 158 | struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 159 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 160 | bb_error_msg("ERROR truncated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 161 | } else { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 162 | errno = -l_err->error; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 163 | bb_perror_msg("RTNETLINK answers"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 164 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 165 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 166 | } |
| 167 | err = filter(&nladdr, h, arg1); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 168 | if (err < 0) { |
| 169 | retval = err; |
| 170 | goto ret; |
| 171 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 172 | |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 173 | skip_it: |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 174 | h = NLMSG_NEXT(h, status); |
| 175 | } |
| 176 | if (msg.msg_flags & MSG_TRUNC) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 177 | bb_error_msg("message truncated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 178 | continue; |
| 179 | } |
| 180 | if (status) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 181 | bb_error_msg_and_die("remnant of size %d!", status); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 182 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 183 | } /* while (1) */ |
| 184 | ret_0: |
| 185 | retval++; /* = 0 */ |
| 186 | ret: |
| 187 | free(buf); |
| 188 | return retval; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 191 | int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth, |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 192 | int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC, |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 193 | void *arg1) |
| 194 | { |
| 195 | int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/); |
| 196 | if (ret < 0) |
| 197 | bb_error_msg_and_die("dump terminated"); |
| 198 | return ret; |
| 199 | } |
| 200 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 201 | int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 202 | pid_t peer, unsigned groups, |
| 203 | struct nlmsghdr *answer, |
| 204 | int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *), |
| 205 | void *jarg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 206 | { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 207 | /* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */ |
| 208 | #define peer 0 |
| 209 | #define groups 0 |
| 210 | #define junk NULL |
| 211 | #define jarg NULL |
| 212 | int retval = -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 213 | int status; |
| 214 | unsigned seq; |
| 215 | struct nlmsghdr *h; |
| 216 | struct sockaddr_nl nladdr; |
| 217 | struct iovec iov = { (void*)n, n->nlmsg_len }; |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 218 | char *buf = xmalloc(8*1024); /* avoid big stack buffer */ |
Szabolcs Nagy | 4ab372d | 2016-04-24 17:39:02 +0200 | [diff] [blame] | 219 | /* Use designated initializers, struct layout is non-portable */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 220 | struct msghdr msg = { |
Szabolcs Nagy | 4ab372d | 2016-04-24 17:39:02 +0200 | [diff] [blame] | 221 | .msg_name = (void*)&nladdr, |
| 222 | .msg_namelen = sizeof(nladdr), |
| 223 | .msg_iov = &iov, |
| 224 | .msg_iovlen = 1, |
| 225 | .msg_control = NULL, |
| 226 | .msg_controllen = 0, |
| 227 | .msg_flags = 0 |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | memset(&nladdr, 0, sizeof(nladdr)); |
| 231 | nladdr.nl_family = AF_NETLINK; |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 232 | // nladdr.nl_pid = peer; |
| 233 | // nladdr.nl_groups = groups; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 234 | |
| 235 | n->nlmsg_seq = seq = ++rtnl->seq; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 236 | if (answer == NULL) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 237 | n->nlmsg_flags |= NLM_F_ACK; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 238 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 239 | status = sendmsg(rtnl->fd, &msg, 0); |
| 240 | |
| 241 | if (status < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 242 | bb_perror_msg("can't talk to rtnetlink"); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 243 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | iov.iov_base = buf; |
| 247 | |
| 248 | while (1) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 249 | iov.iov_len = 8*1024; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 250 | status = recvmsg(rtnl->fd, &msg, 0); |
| 251 | |
| 252 | if (status < 0) { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 253 | if (errno == EINTR) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 254 | continue; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 255 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 256 | bb_perror_msg("OVERRUN"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 257 | continue; |
| 258 | } |
| 259 | if (status == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 260 | bb_error_msg("EOF on netlink"); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 261 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 262 | } |
| 263 | if (msg.msg_namelen != sizeof(nladdr)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 264 | 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] | 265 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 266 | for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 267 | // int l_err; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 268 | int len = h->nlmsg_len; |
| 269 | int l = len - sizeof(*h); |
| 270 | |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 271 | if (l < 0 || len > status) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 272 | if (msg.msg_flags & MSG_TRUNC) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 273 | bb_error_msg("truncated message"); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 274 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 275 | } |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 276 | bb_error_msg_and_die("malformed message: len=%d!", len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Eric Andersen | 6c8161d | 2004-05-05 07:05:32 +0000 | [diff] [blame] | 279 | if (nladdr.nl_pid != peer || |
| 280 | h->nlmsg_pid != rtnl->local.nl_pid || |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 281 | h->nlmsg_seq != seq |
| 282 | ) { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 283 | // if (junk) { |
| 284 | // l_err = junk(&nladdr, h, jarg); |
| 285 | // if (l_err < 0) { |
| 286 | // retval = l_err; |
| 287 | // goto ret; |
| 288 | // } |
| 289 | // } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 290 | continue; |
| 291 | } |
| 292 | |
| 293 | if (h->nlmsg_type == NLMSG_ERROR) { |
| 294 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 295 | if (l < (int)sizeof(struct nlmsgerr)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 296 | bb_error_msg("ERROR truncated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 297 | } else { |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 298 | errno = - err->error; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 299 | if (errno == 0) { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 300 | if (answer) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 301 | memcpy(answer, h, h->nlmsg_len); |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 302 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 303 | goto ret_0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 304 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 305 | bb_perror_msg("RTNETLINK answers"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 306 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 307 | goto ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 308 | } |
| 309 | if (answer) { |
| 310 | memcpy(answer, h, h->nlmsg_len); |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 311 | goto ret_0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 314 | bb_error_msg("unexpected reply!"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 315 | |
| 316 | status -= NLMSG_ALIGN(len); |
| 317 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); |
| 318 | } |
| 319 | if (msg.msg_flags & MSG_TRUNC) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 320 | bb_error_msg("message truncated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 321 | continue; |
| 322 | } |
| 323 | if (status) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 324 | bb_error_msg_and_die("remnant of size %d!", status); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 325 | } |
Denis Vlasenko | 8723a94 | 2007-12-02 06:30:57 +0000 | [diff] [blame] | 326 | } /* while (1) */ |
| 327 | ret_0: |
| 328 | retval++; /* = 0 */ |
| 329 | ret: |
| 330 | free(buf); |
| 331 | return retval; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 334 | 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] | 335 | { |
| 336 | int len = RTA_LENGTH(4); |
| 337 | struct rtattr *rta; |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 338 | |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 339 | if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 340 | return -1; |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 341 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 342 | rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len)); |
| 343 | rta->rta_type = type; |
| 344 | rta->rta_len = len; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 345 | move_to_unaligned32(RTA_DATA(rta), data); |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 346 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 350 | 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] | 351 | { |
| 352 | int len = RTA_LENGTH(alen); |
| 353 | struct rtattr *rta; |
| 354 | |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 355 | if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 356 | return -1; |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 357 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 358 | rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len)); |
| 359 | rta->rta_type = type; |
| 360 | rta->rta_len = len; |
| 361 | memcpy(RTA_DATA(rta), data, alen); |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 362 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 366 | 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] | 367 | { |
| 368 | int len = RTA_LENGTH(4); |
| 369 | struct rtattr *subrta; |
| 370 | |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 371 | if (RTA_ALIGN(rta->rta_len + len) > maxlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 372 | return -1; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 373 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 374 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
| 375 | subrta->rta_type = type; |
| 376 | subrta->rta_len = len; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 377 | move_to_unaligned32(RTA_DATA(subrta), data); |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 378 | rta->rta_len = NLMSG_ALIGN(rta->rta_len + len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
Denis Vlasenko | 0db2c2e | 2008-06-29 06:22:40 +0000 | [diff] [blame] | 382 | 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] | 383 | { |
| 384 | struct rtattr *subrta; |
| 385 | int len = RTA_LENGTH(alen); |
| 386 | |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 387 | if (RTA_ALIGN(rta->rta_len + len) > maxlen) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 388 | return -1; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 389 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 390 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
| 391 | subrta->rta_type = type; |
| 392 | subrta->rta_len = len; |
| 393 | memcpy(RTA_DATA(subrta), data, alen); |
Hauke Mehrtens | 4ff86df | 2016-09-16 22:40:28 +0200 | [diff] [blame] | 394 | rta->rta_len = NLMSG_ALIGN(rta->rta_len + len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | |
Natanael Copa | b78ac5a | 2010-07-23 01:31:24 +0200 | [diff] [blame] | 399 | 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] | 400 | { |
Denys Vlasenko | 68ae542 | 2018-02-08 08:42:37 +0100 | [diff] [blame] | 401 | memset(tb, 0, (max + 1) * sizeof(tb[0])); |
| 402 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 403 | while (RTA_OK(rta, len)) { |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 404 | if (rta->rta_type <= max) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 405 | tb[rta->rta_type] = rta; |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 406 | } |
Denys Vlasenko | 3bb235c | 2011-02-23 01:20:44 +0100 | [diff] [blame] | 407 | rta = RTA_NEXT(rta, len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 408 | } |
Glenn L McGrath | d4de975 | 2002-11-28 12:35:46 +0000 | [diff] [blame] | 409 | if (len) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 410 | 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] | 411 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 412 | } |