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