blob: 7ad2de9fa1f0fa9775bb56d559f05be5de25c764 [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/*
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 McGrath9a2d2722002-11-10 01:33:55 +000014#include <sys/socket.h>
Eric Andersen0f08e532003-06-20 09:05:00 +000015#include <sys/uio.h>
16
Denis Vlasenkofad2b862007-05-31 22:16:38 +000017#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018#include "libnetlink.h"
19
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000020void FAST_FUNC rtnl_close(struct rtnl_handle *rth)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000021{
22 close(rth->fd);
23}
24
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000025int FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000026{
Eric Andersend78aea82006-01-30 18:00:02 +000027 socklen_t addr_len;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000028
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000029 memset(rth, 0, sizeof(*rth));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000030 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000031 rth->local.nl_family = AF_NETLINK;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000032 /*rth->local.nl_groups = subscriptions;*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000033
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000034 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000035 addr_len = sizeof(rth->local);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000036 getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len);
37
38/* too much paranoia
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000039 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +000040 bb_perror_msg_and_die("getsockname");
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000041 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 Vlasenkoa771e7c2009-04-21 23:48:38 +000045*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046 rth->seq = time(NULL);
47 return 0;
48}
49
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000050int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051{
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-Fischerb2908892007-04-12 11:34:39 +000068 return xsendto(rth->fd, (void*)&req, sizeof(req),
69 (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070}
71
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000072int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000073{
74 struct sockaddr_nl nladdr;
75
76 memset(&nladdr, 0, sizeof(nladdr));
77 nladdr.nl_family = AF_NETLINK;
78
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000079 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080}
81
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000082int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000083{
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-Fischerb2908892007-04-12 11:34:39 +0000106static int rtnl_dump_filter(struct rtnl_handle *rth,
Bernhard Reutner-Fischer9de46222008-06-12 15:54:49 +0000107 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000108 void *arg1/*,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000109 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000110 void *arg2*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000112 int retval = -1;
113 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000114 struct sockaddr_nl nladdr;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000115 struct iovec iov = { buf, 8*1024 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000116
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 cad53642003-03-19 09:13:01 +0000133 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000134 continue;
135 }
136 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000138 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000139 }
140 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142 }
143
144 h = (struct nlmsghdr*)buf;
145 while (NLMSG_OK(h, status)) {
146 int err;
147
Eric Andersen6c8161d2004-05-05 07:05:32 +0000148 if (nladdr.nl_pid != 0 ||
149 h->nlmsg_pid != rth->local.nl_pid ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150 h->nlmsg_seq != rth->dump) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000151// if (junk) {
152// err = junk(&nladdr, h, arg2);
153// if (err < 0) {
154// retval = err;
155// goto ret;
156// }
157// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000158 goto skip_it;
159 }
160
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000161 if (h->nlmsg_type == NLMSG_DONE) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000162 goto ret_0;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000163 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000164 if (h->nlmsg_type == NLMSG_ERROR) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000165 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168 } else {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000169 errno = -l_err->error;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000170 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000171 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000172 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000173 }
174 err = filter(&nladdr, h, arg1);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000175 if (err < 0) {
176 retval = err;
177 goto ret;
178 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000179
Denis Vlasenko8723a942007-12-02 06:30:57 +0000180 skip_it:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000181 h = NLMSG_NEXT(h, status);
182 }
183 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000184 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000185 continue;
186 }
187 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000188 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000189 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000190 } /* while (1) */
191 ret_0:
192 retval++; /* = 0 */
193 ret:
194 free(buf);
195 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000196}
197
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000198int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
Bernhard Reutner-Fischer9de46222008-06-12 15:54:49 +0000199 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000200 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 Vlasenko0db2c2e2008-06-29 06:22:40 +0000208int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
Denis Vlasenko8723a942007-12-02 06:30:57 +0000209 pid_t peer, unsigned groups,
210 struct nlmsghdr *answer,
Bernhard Reutner-Fischer9de46222008-06-12 15:54:49 +0000211 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000212 void *jarg)
213{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000214/* 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 McGrath9a2d2722002-11-10 01:33:55 +0000220 int status;
221 unsigned seq;
222 struct nlmsghdr *h;
223 struct sockaddr_nl nladdr;
224 struct iovec iov = { (void*)n, n->nlmsg_len };
Denis Vlasenko8723a942007-12-02 06:30:57 +0000225 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000226 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 Vlasenko8723a942007-12-02 06:30:57 +0000235// nladdr.nl_pid = peer;
236// nladdr.nl_groups = groups;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000237
238 n->nlmsg_seq = seq = ++rtnl->seq;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000239 if (answer == NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000240 n->nlmsg_flags |= NLM_F_ACK;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000241 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000242 status = sendmsg(rtnl->fd, &msg, 0);
243
244 if (status < 0) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000245 bb_perror_msg("cannot talk to rtnetlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000246 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247 }
248
249 iov.iov_base = buf;
250
251 while (1) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000252 iov.iov_len = 8*1024;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253 status = recvmsg(rtnl->fd, &msg, 0);
254
255 if (status < 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000256 if (errno == EINTR) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257 continue;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000258 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000259 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000260 continue;
261 }
262 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000263 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000264 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000265 }
266 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000267 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000268 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000269 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000270// int l_err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000271 int len = h->nlmsg_len;
272 int l = len - sizeof(*h);
273
Denis Vlasenko8723a942007-12-02 06:30:57 +0000274 if (l < 0 || len > status) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000275 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000276 bb_error_msg("truncated message");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000277 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000279 bb_error_msg_and_die("malformed message: len=%d!", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000280 }
281
Eric Andersen6c8161d2004-05-05 07:05:32 +0000282 if (nladdr.nl_pid != peer ||
283 h->nlmsg_pid != rtnl->local.nl_pid ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000284 h->nlmsg_seq != seq) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000285// 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 McGrath9a2d2722002-11-10 01:33:55 +0000292 continue;
293 }
294
295 if (h->nlmsg_type == NLMSG_ERROR) {
296 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000297 if (l < (int)sizeof(struct nlmsgerr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000298 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000299 } else {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000300 errno = - err->error;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000301 if (errno == 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000302 if (answer) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000303 memcpy(answer, h, h->nlmsg_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000304 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000305 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000306 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000307 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000308 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000309 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000310 }
311 if (answer) {
312 memcpy(answer, h, h->nlmsg_len);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000313 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000314 }
315
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000316 bb_error_msg("unexpected reply!");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000317
318 status -= NLMSG_ALIGN(len);
319 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
320 }
321 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000322 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000323 continue;
324 }
325 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000326 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000327 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000328 } /* while (1) */
329 ret_0:
330 retval++; /* = 0 */
331 ret:
332 free(buf);
333 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000334}
335
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000336int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000337{
338 int len = RTA_LENGTH(4);
339 struct rtattr *rta;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000340 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000341 return -1;
342 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
343 rta->rta_type = type;
344 rta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000345 move_to_unaligned32(RTA_DATA(rta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000346 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
347 return 0;
348}
349
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000350int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000351{
352 int len = RTA_LENGTH(alen);
353 struct rtattr *rta;
354
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000355 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000356 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 Vlasenko0db2c2e2008-06-29 06:22:40 +0000365int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000366{
367 int len = RTA_LENGTH(4);
368 struct rtattr *subrta;
369
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000370 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000372 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000373 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
374 subrta->rta_type = type;
375 subrta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000376 move_to_unaligned32(RTA_DATA(subrta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000377 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
378 return 0;
379}
380
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000381int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000382{
383 struct rtattr *subrta;
384 int len = RTA_LENGTH(alen);
385
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000386 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000387 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000388 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000389 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 Vlasenko0db2c2e2008-06-29 06:22:40 +0000398int FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000399{
400 while (RTA_OK(rta, len)) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000401 if (rta->rta_type <= max) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000402 tb[rta->rta_type] = rta;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000403 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000404 rta = RTA_NEXT(rta,len);
405 }
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000406 if (len) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000407 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000408 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000409 return 0;
410}