blob: ba24832d6a44830da038665fa4838ed0e3686207 [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,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200107 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
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 ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100150 h->nlmsg_seq != rth->dump
151 ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000152// if (junk) {
153// err = junk(&nladdr, h, arg2);
154// if (err < 0) {
155// retval = err;
156// goto ret;
157// }
158// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000159 goto skip_it;
160 }
161
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000162 if (h->nlmsg_type == NLMSG_DONE) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000163 goto ret_0;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000164 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 if (h->nlmsg_type == NLMSG_ERROR) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000166 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169 } else {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000170 errno = -l_err->error;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000172 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000173 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174 }
175 err = filter(&nladdr, h, arg1);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000176 if (err < 0) {
177 retval = err;
178 goto ret;
179 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180
Denis Vlasenko8723a942007-12-02 06:30:57 +0000181 skip_it:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000182 h = NLMSG_NEXT(h, status);
183 }
184 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000185 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000186 continue;
187 }
188 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000189 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000191 } /* while (1) */
192 ret_0:
193 retval++; /* = 0 */
194 ret:
195 free(buf);
196 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000197}
198
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000199int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200200 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000201 void *arg1)
202{
203 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
204 if (ret < 0)
205 bb_error_msg_and_die("dump terminated");
206 return ret;
207}
208
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000209int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200210 pid_t peer, unsigned groups,
211 struct nlmsghdr *answer,
212 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
213 void *jarg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000214{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000215/* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
216#define peer 0
217#define groups 0
218#define junk NULL
219#define jarg NULL
220 int retval = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221 int status;
222 unsigned seq;
223 struct nlmsghdr *h;
224 struct sockaddr_nl nladdr;
225 struct iovec iov = { (void*)n, n->nlmsg_len };
Denis Vlasenko8723a942007-12-02 06:30:57 +0000226 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 struct msghdr msg = {
228 (void*)&nladdr, sizeof(nladdr),
229 &iov, 1,
230 NULL, 0,
231 0
232 };
233
234 memset(&nladdr, 0, sizeof(nladdr));
235 nladdr.nl_family = AF_NETLINK;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000236// nladdr.nl_pid = peer;
237// nladdr.nl_groups = groups;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000238
239 n->nlmsg_seq = seq = ++rtnl->seq;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000240 if (answer == NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000241 n->nlmsg_flags |= NLM_F_ACK;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000242 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243 status = sendmsg(rtnl->fd, &msg, 0);
244
245 if (status < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100246 bb_perror_msg("can't talk to rtnetlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000247 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000248 }
249
250 iov.iov_base = buf;
251
252 while (1) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000253 iov.iov_len = 8*1024;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000254 status = recvmsg(rtnl->fd, &msg, 0);
255
256 if (status < 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000257 if (errno == EINTR) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000258 continue;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000259 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000260 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000261 continue;
262 }
263 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000264 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000265 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 }
267 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000268 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000269 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000270 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000271// int l_err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 int len = h->nlmsg_len;
273 int l = len - sizeof(*h);
274
Denis Vlasenko8723a942007-12-02 06:30:57 +0000275 if (l < 0 || len > status) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000277 bb_error_msg("truncated message");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000278 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000279 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000280 bb_error_msg_and_die("malformed message: len=%d!", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 }
282
Eric Andersen6c8161d2004-05-05 07:05:32 +0000283 if (nladdr.nl_pid != peer ||
284 h->nlmsg_pid != rtnl->local.nl_pid ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100285 h->nlmsg_seq != seq
286 ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000287// if (junk) {
288// l_err = junk(&nladdr, h, jarg);
289// if (l_err < 0) {
290// retval = l_err;
291// goto ret;
292// }
293// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000294 continue;
295 }
296
297 if (h->nlmsg_type == NLMSG_ERROR) {
298 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000299 if (l < (int)sizeof(struct nlmsgerr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000300 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000301 } else {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000302 errno = - err->error;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000303 if (errno == 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000304 if (answer) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000305 memcpy(answer, h, h->nlmsg_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000306 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000307 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000308 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000309 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000310 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000311 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000312 }
313 if (answer) {
314 memcpy(answer, h, h->nlmsg_len);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000315 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000316 }
317
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000318 bb_error_msg("unexpected reply!");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000319
320 status -= NLMSG_ALIGN(len);
321 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
322 }
323 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000324 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000325 continue;
326 }
327 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000328 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000329 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000330 } /* while (1) */
331 ret_0:
332 retval++; /* = 0 */
333 ret:
334 free(buf);
335 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000336}
337
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000338int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000339{
340 int len = RTA_LENGTH(4);
341 struct rtattr *rta;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000342 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000343 return -1;
344 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
345 rta->rta_type = type;
346 rta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000347 move_to_unaligned32(RTA_DATA(rta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000348 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
349 return 0;
350}
351
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000352int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000353{
354 int len = RTA_LENGTH(alen);
355 struct rtattr *rta;
356
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000357 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000358 return -1;
359 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
360 rta->rta_type = type;
361 rta->rta_len = len;
362 memcpy(RTA_DATA(rta), data, alen);
363 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
364 return 0;
365}
366
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000367int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000368{
369 int len = RTA_LENGTH(4);
370 struct rtattr *subrta;
371
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000372 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000373 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000374 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000375 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
376 subrta->rta_type = type;
377 subrta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000378 move_to_unaligned32(RTA_DATA(subrta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000379 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
380 return 0;
381}
382
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000383int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000384{
385 struct rtattr *subrta;
386 int len = RTA_LENGTH(alen);
387
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000388 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000389 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000390 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000391 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
392 subrta->rta_type = type;
393 subrta->rta_len = len;
394 memcpy(RTA_DATA(subrta), data, alen);
395 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
396 return 0;
397}
398
399
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000400int FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401{
402 while (RTA_OK(rta, len)) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000403 if (rta->rta_type <= max) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000404 tb[rta->rta_type] = rta;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000405 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000406 rta = RTA_NEXT(rta,len);
407 }
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000408 if (len) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000409 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000410 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000411 return 0;
412}