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