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