blob: 3a37d97f87cb6ae9591ce63a4d15a81bd6658247 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Natanael Copab78ac5a2010-07-23 01:31:24 +02003 * RTnetlink service routines.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Natanael Copab78ac5a2010-07-23 01:31:24 +02005 * 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 McGrath9a2d2722002-11-10 01:33:55 +00009 *
Natanael Copab78ac5a2010-07-23 01:31:24 +020010 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000011 */
12
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013#include <sys/socket.h>
Eric Andersen0f08e532003-06-20 09:05:00 +000014#include <sys/uio.h>
15
Denis Vlasenkofad2b862007-05-31 22:16:38 +000016#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017#include "libnetlink.h"
18
Natanael Copab78ac5a2010-07-23 01:31:24 +020019void FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020{
Eric Andersend78aea82006-01-30 18:00:02 +000021 socklen_t addr_len;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000022
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000023 memset(rth, 0, sizeof(*rth));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000024 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000025 rth->local.nl_family = AF_NETLINK;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000026 /*rth->local.nl_groups = subscriptions;*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000028 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000029 addr_len = sizeof(rth->local);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000030 getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len);
31
32/* too much paranoia
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000033 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +000034 bb_perror_msg_and_die("getsockname");
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000035 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 Vlasenkoa771e7c2009-04-21 23:48:38 +000039*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000040 rth->seq = time(NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000041}
42
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000043int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000044{
45 struct {
46 struct nlmsghdr nlh;
47 struct rtgenmsg g;
48 } req;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049
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 Copab78ac5a2010-07-23 01:31:24 +020057 return rtnl_send(rth, (void*)&req, sizeof(req));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058}
59
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000060int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061{
62 struct sockaddr_nl nladdr;
63
64 memset(&nladdr, 0, sizeof(nladdr));
65 nladdr.nl_family = AF_NETLINK;
66
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000067 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068}
69
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000070int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071{
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 Vlasenkofb132e42010-10-29 11:46:52 +020077 iov, 2,
78 NULL, 0,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 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-Fischerb2908892007-04-12 11:34:39 +000094static int rtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020095 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000096 void *arg1/*,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000097 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000098 void *arg2*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000100 int retval = -1;
101 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102 struct sockaddr_nl nladdr;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000103 struct iovec iov = { buf, 8*1024 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104
105 while (1) {
106 int status;
107 struct nlmsghdr *h;
108
109 struct msghdr msg = {
110 (void*)&nladdr, sizeof(nladdr),
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200111 &iov, 1,
112 NULL, 0,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113 0
114 };
115
116 status = recvmsg(rth->fd, &msg, 0);
117
118 if (status < 0) {
119 if (errno == EINTR)
120 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122 continue;
123 }
124 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000126 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127 }
128 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130 }
131
132 h = (struct nlmsghdr*)buf;
133 while (NLMSG_OK(h, status)) {
134 int err;
135
Eric Andersen6c8161d2004-05-05 07:05:32 +0000136 if (nladdr.nl_pid != 0 ||
137 h->nlmsg_pid != rth->local.nl_pid ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100138 h->nlmsg_seq != rth->dump
139 ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000140// if (junk) {
141// err = junk(&nladdr, h, arg2);
142// if (err < 0) {
143// retval = err;
144// goto ret;
145// }
146// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000147 goto skip_it;
148 }
149
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000150 if (h->nlmsg_type == NLMSG_DONE) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000151 goto ret_0;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000152 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 if (h->nlmsg_type == NLMSG_ERROR) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000154 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157 } else {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000158 errno = -l_err->error;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000160 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000161 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162 }
163 err = filter(&nladdr, h, arg1);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000164 if (err < 0) {
165 retval = err;
166 goto ret;
167 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168
Denis Vlasenko8723a942007-12-02 06:30:57 +0000169 skip_it:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 h = NLMSG_NEXT(h, status);
171 }
172 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000173 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174 continue;
175 }
176 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000177 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000179 } /* while (1) */
180 ret_0:
181 retval++; /* = 0 */
182 ret:
183 free(buf);
184 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000185}
186
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000187int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200188 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000189 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 Vlasenko0db2c2e2008-06-29 06:22:40 +0000197int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200198 pid_t peer, unsigned groups,
199 struct nlmsghdr *answer,
200 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
201 void *jarg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000202{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000203/* 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 McGrath9a2d2722002-11-10 01:33:55 +0000209 int status;
210 unsigned seq;
211 struct nlmsghdr *h;
212 struct sockaddr_nl nladdr;
213 struct iovec iov = { (void*)n, n->nlmsg_len };
Denis Vlasenko8723a942007-12-02 06:30:57 +0000214 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215 struct msghdr msg = {
216 (void*)&nladdr, sizeof(nladdr),
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200217 &iov, 1,
218 NULL, 0,
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000219 0
220 };
221
222 memset(&nladdr, 0, sizeof(nladdr));
223 nladdr.nl_family = AF_NETLINK;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000224// nladdr.nl_pid = peer;
225// nladdr.nl_groups = groups;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000226
227 n->nlmsg_seq = seq = ++rtnl->seq;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000228 if (answer == NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000229 n->nlmsg_flags |= NLM_F_ACK;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000230 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000231 status = sendmsg(rtnl->fd, &msg, 0);
232
233 if (status < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100234 bb_perror_msg("can't talk to rtnetlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000235 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000236 }
237
238 iov.iov_base = buf;
239
240 while (1) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000241 iov.iov_len = 8*1024;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000242 status = recvmsg(rtnl->fd, &msg, 0);
243
244 if (status < 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000245 if (errno == EINTR) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000246 continue;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000247 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000248 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249 continue;
250 }
251 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000252 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000253 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000254 }
255 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000256 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000258 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000259// int l_err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000260 int len = h->nlmsg_len;
261 int l = len - sizeof(*h);
262
Denis Vlasenko8723a942007-12-02 06:30:57 +0000263 if (l < 0 || len > status) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000265 bb_error_msg("truncated message");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000266 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000268 bb_error_msg_and_die("malformed message: len=%d!", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000269 }
270
Eric Andersen6c8161d2004-05-05 07:05:32 +0000271 if (nladdr.nl_pid != peer ||
272 h->nlmsg_pid != rtnl->local.nl_pid ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100273 h->nlmsg_seq != seq
274 ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000275// 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 McGrath9a2d2722002-11-10 01:33:55 +0000282 continue;
283 }
284
285 if (h->nlmsg_type == NLMSG_ERROR) {
286 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000287 if (l < (int)sizeof(struct nlmsgerr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000288 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000289 } else {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000290 errno = - err->error;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000291 if (errno == 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000292 if (answer) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000293 memcpy(answer, h, h->nlmsg_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000294 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000295 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000296 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000297 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000298 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000299 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000300 }
301 if (answer) {
302 memcpy(answer, h, h->nlmsg_len);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000303 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000304 }
305
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000306 bb_error_msg("unexpected reply!");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000307
308 status -= NLMSG_ALIGN(len);
309 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
310 }
311 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000312 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000313 continue;
314 }
315 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000316 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000317 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000318 } /* while (1) */
319 ret_0:
320 retval++; /* = 0 */
321 ret:
322 free(buf);
323 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000324}
325
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000326int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000327{
328 int len = RTA_LENGTH(4);
329 struct rtattr *rta;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200330
331 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000332 return -1;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200333 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000334 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
335 rta->rta_type = type;
336 rta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000337 move_to_unaligned32(RTA_DATA(rta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000338 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
339 return 0;
340}
341
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000342int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000343{
344 int len = RTA_LENGTH(alen);
345 struct rtattr *rta;
346
Natanael Copab78ac5a2010-07-23 01:31:24 +0200347 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000348 return -1;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200349 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000350 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 Vlasenko0db2c2e2008-06-29 06:22:40 +0000358int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000359{
360 int len = RTA_LENGTH(4);
361 struct rtattr *subrta;
362
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000363 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000364 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000365 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000366 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
367 subrta->rta_type = type;
368 subrta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000369 move_to_unaligned32(RTA_DATA(subrta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000370 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
371 return 0;
372}
373
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000374int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000375{
376 struct rtattr *subrta;
377 int len = RTA_LENGTH(alen);
378
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000379 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000380 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000381 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000382 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 Copab78ac5a2010-07-23 01:31:24 +0200391void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000392{
393 while (RTA_OK(rta, len)) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000394 if (rta->rta_type <= max) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000395 tb[rta->rta_type] = rta;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000396 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000397 rta = RTA_NEXT(rta,len);
398 }
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000399 if (len) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000400 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000401 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000402}