blob: 6b599d91c903732a2c7c7581625c061548c18435 [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
29 memset(rth, 0, sizeof(rth));
30
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000031 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032
33 memset(&rth->local, 0, sizeof(rth->local));
34 rth->local.nl_family = AF_NETLINK;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000035 /*rth->local.nl_groups = subscriptions;*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000036
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000037 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038 addr_len = sizeof(rth->local);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000039 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
40 bb_perror_msg_and_die("cannot getsockname");
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);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000045 rth->seq = time(NULL);
46 return 0;
47}
48
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000049int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000050{
51 struct {
52 struct nlmsghdr nlh;
53 struct rtgenmsg g;
54 } req;
55 struct sockaddr_nl nladdr;
56
57 memset(&nladdr, 0, sizeof(nladdr));
58 nladdr.nl_family = AF_NETLINK;
59
60 req.nlh.nlmsg_len = sizeof(req);
61 req.nlh.nlmsg_type = type;
62 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
63 req.nlh.nlmsg_pid = 0;
64 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
65 req.g.rtgen_family = family;
66
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000067 return xsendto(rth->fd, (void*)&req, sizeof(req),
68 (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000069}
70
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000071int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000072{
73 struct sockaddr_nl nladdr;
74
75 memset(&nladdr, 0, sizeof(nladdr));
76 nladdr.nl_family = AF_NETLINK;
77
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000078 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079}
80
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000081int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082{
83 struct nlmsghdr nlh;
84 struct sockaddr_nl nladdr;
85 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
86 struct msghdr msg = {
87 (void*)&nladdr, sizeof(nladdr),
88 iov, 2,
89 NULL, 0,
90 0
91 };
92
93 memset(&nladdr, 0, sizeof(nladdr));
94 nladdr.nl_family = AF_NETLINK;
95
96 nlh.nlmsg_len = NLMSG_LENGTH(len);
97 nlh.nlmsg_type = type;
98 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
99 nlh.nlmsg_pid = 0;
100 nlh.nlmsg_seq = rth->dump = ++rth->seq;
101
102 return sendmsg(rth->fd, &msg, 0);
103}
104
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000105static int rtnl_dump_filter(struct rtnl_handle *rth,
Bernhard Reutner-Fischer9de46222008-06-12 15:54:49 +0000106 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000107 void *arg1/*,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000108 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000109 void *arg2*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000110{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000111 int retval = -1;
112 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113 struct sockaddr_nl nladdr;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000114 struct iovec iov = { buf, 8*1024 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115
116 while (1) {
117 int status;
118 struct nlmsghdr *h;
119
120 struct msghdr msg = {
121 (void*)&nladdr, sizeof(nladdr),
122 &iov, 1,
123 NULL, 0,
124 0
125 };
126
127 status = recvmsg(rth->fd, &msg, 0);
128
129 if (status < 0) {
130 if (errno == EINTR)
131 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 continue;
134 }
135 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000137 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138 }
139 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000141 }
142
143 h = (struct nlmsghdr*)buf;
144 while (NLMSG_OK(h, status)) {
145 int err;
146
Eric Andersen6c8161d2004-05-05 07:05:32 +0000147 if (nladdr.nl_pid != 0 ||
148 h->nlmsg_pid != rth->local.nl_pid ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149 h->nlmsg_seq != rth->dump) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000150// if (junk) {
151// err = junk(&nladdr, h, arg2);
152// if (err < 0) {
153// retval = err;
154// goto ret;
155// }
156// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157 goto skip_it;
158 }
159
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000160 if (h->nlmsg_type == NLMSG_DONE) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000161 goto ret_0;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000162 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 if (h->nlmsg_type == NLMSG_ERROR) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000164 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167 } else {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000168 errno = -l_err->error;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000171 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000172 }
173 err = filter(&nladdr, h, arg1);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000174 if (err < 0) {
175 retval = err;
176 goto ret;
177 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178
Denis Vlasenko8723a942007-12-02 06:30:57 +0000179 skip_it:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180 h = NLMSG_NEXT(h, status);
181 }
182 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000183 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 continue;
185 }
186 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000187 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000188 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000189 } /* while (1) */
190 ret_0:
191 retval++; /* = 0 */
192 ret:
193 free(buf);
194 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000195}
196
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000197int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
Bernhard Reutner-Fischer9de46222008-06-12 15:54:49 +0000198 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000199 void *arg1)
200{
201 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
202 if (ret < 0)
203 bb_error_msg_and_die("dump terminated");
204 return ret;
205}
206
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000207int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
Denis Vlasenko8723a942007-12-02 06:30:57 +0000208 pid_t peer, unsigned groups,
209 struct nlmsghdr *answer,
Bernhard Reutner-Fischer9de46222008-06-12 15:54:49 +0000210 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000211 void *jarg)
212{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000213/* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
214#define peer 0
215#define groups 0
216#define junk NULL
217#define jarg NULL
218 int retval = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000219 int status;
220 unsigned seq;
221 struct nlmsghdr *h;
222 struct sockaddr_nl nladdr;
223 struct iovec iov = { (void*)n, n->nlmsg_len };
Denis Vlasenko8723a942007-12-02 06:30:57 +0000224 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000225 struct msghdr msg = {
226 (void*)&nladdr, sizeof(nladdr),
227 &iov, 1,
228 NULL, 0,
229 0
230 };
231
232 memset(&nladdr, 0, sizeof(nladdr));
233 nladdr.nl_family = AF_NETLINK;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000234// nladdr.nl_pid = peer;
235// nladdr.nl_groups = groups;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000236
237 n->nlmsg_seq = seq = ++rtnl->seq;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000238 if (answer == NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000239 n->nlmsg_flags |= NLM_F_ACK;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000240 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000241 status = sendmsg(rtnl->fd, &msg, 0);
242
243 if (status < 0) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000244 bb_perror_msg("cannot talk to rtnetlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000245 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000246 }
247
248 iov.iov_base = buf;
249
250 while (1) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000251 iov.iov_len = 8*1024;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252 status = recvmsg(rtnl->fd, &msg, 0);
253
254 if (status < 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000255 if (errno == EINTR) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000256 continue;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000257 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000258 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 continue;
260 }
261 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000262 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000263 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 }
265 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000266 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000268 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000269// int l_err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270 int len = h->nlmsg_len;
271 int l = len - sizeof(*h);
272
Denis Vlasenko8723a942007-12-02 06:30:57 +0000273 if (l < 0 || len > status) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000275 bb_error_msg("truncated message");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000276 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000278 bb_error_msg_and_die("malformed message: len=%d!", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000279 }
280
Eric Andersen6c8161d2004-05-05 07:05:32 +0000281 if (nladdr.nl_pid != peer ||
282 h->nlmsg_pid != rtnl->local.nl_pid ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283 h->nlmsg_seq != seq) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000284// if (junk) {
285// l_err = junk(&nladdr, h, jarg);
286// if (l_err < 0) {
287// retval = l_err;
288// goto ret;
289// }
290// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000291 continue;
292 }
293
294 if (h->nlmsg_type == NLMSG_ERROR) {
295 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000296 if (l < (int)sizeof(struct nlmsgerr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000297 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000298 } else {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000299 errno = - err->error;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000300 if (errno == 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000301 if (answer) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000302 memcpy(answer, h, h->nlmsg_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000303 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000304 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000305 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000306 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000307 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000308 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000309 }
310 if (answer) {
311 memcpy(answer, h, h->nlmsg_len);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000312 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000313 }
314
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000315 bb_error_msg("unexpected reply!");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000316
317 status -= NLMSG_ALIGN(len);
318 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
319 }
320 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000321 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000322 continue;
323 }
324 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000325 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000326 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000327 } /* while (1) */
328 ret_0:
329 retval++; /* = 0 */
330 ret:
331 free(buf);
332 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000333}
334
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000335int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000336{
337 int len = RTA_LENGTH(4);
338 struct rtattr *rta;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000339 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000340 return -1;
341 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
342 rta->rta_type = type;
343 rta->rta_len = len;
344 memcpy(RTA_DATA(rta), &data, 4);
345 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
346 return 0;
347}
348
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000349int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000350{
351 int len = RTA_LENGTH(alen);
352 struct rtattr *rta;
353
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000354 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000355 return -1;
356 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
357 rta->rta_type = type;
358 rta->rta_len = len;
359 memcpy(RTA_DATA(rta), data, alen);
360 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
361 return 0;
362}
363
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000364int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000365{
366 int len = RTA_LENGTH(4);
367 struct rtattr *subrta;
368
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000369 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000370 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000371 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000372 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
373 subrta->rta_type = type;
374 subrta->rta_len = len;
375 memcpy(RTA_DATA(subrta), &data, 4);
376 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
377 return 0;
378}
379
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000380int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000381{
382 struct rtattr *subrta;
383 int len = RTA_LENGTH(alen);
384
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000385 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000386 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000387 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000388 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
389 subrta->rta_type = type;
390 subrta->rta_len = len;
391 memcpy(RTA_DATA(subrta), data, alen);
392 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
393 return 0;
394}
395
396
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000397int FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000398{
399 while (RTA_OK(rta, len)) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000400 if (rta->rta_type <= max) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401 tb[rta->rta_type] = rta;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000402 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000403 rta = RTA_NEXT(rta,len);
404 }
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000405 if (len) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000406 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000407 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000408 return 0;
409}