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