blob: cbb5daf95fb2d7a5af693a059d3dfdcf044084b3 [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 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 *
Natanael Copab78ac5a2010-07-23 01:31:24 +02008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 */
10
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000011#include <sys/socket.h>
Eric Andersen0f08e532003-06-20 09:05:00 +000012#include <sys/uio.h>
13
Denis Vlasenkofad2b862007-05-31 22:16:38 +000014#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include "libnetlink.h"
16
Natanael Copab78ac5a2010-07-23 01:31:24 +020017void FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018{
Eric Andersend78aea82006-01-30 18:00:02 +000019 socklen_t addr_len;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000021 memset(rth, 0, sizeof(*rth));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000022 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000023 rth->local.nl_family = AF_NETLINK;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000024 /*rth->local.nl_groups = subscriptions;*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000025
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000026 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027 addr_len = sizeof(rth->local);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000028 getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len);
29
30/* too much paranoia
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000031 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +000032 bb_perror_msg_and_die("getsockname");
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000033 if (addr_len != sizeof(rth->local))
34 bb_error_msg_and_die("wrong address length %d", addr_len);
35 if (rth->local.nl_family != AF_NETLINK)
36 bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000037*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038 rth->seq = time(NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000039}
40
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000041int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042{
43 struct {
44 struct nlmsghdr nlh;
45 struct rtgenmsg g;
46 } req;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047
48 req.nlh.nlmsg_len = sizeof(req);
49 req.nlh.nlmsg_type = type;
50 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
51 req.nlh.nlmsg_pid = 0;
52 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
53 req.g.rtgen_family = family;
54
Natanael Copab78ac5a2010-07-23 01:31:24 +020055 return rtnl_send(rth, (void*)&req, sizeof(req));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000056}
57
Denys Vlasenkof1334712011-02-09 04:39:09 +010058//TODO: pass rth->fd instead of full rth?
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000059int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060{
61 struct sockaddr_nl nladdr;
62
63 memset(&nladdr, 0, sizeof(nladdr));
64 nladdr.nl_family = AF_NETLINK;
65
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000066 return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000067}
68
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000069int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070{
71 struct nlmsghdr nlh;
72 struct sockaddr_nl nladdr;
73 struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +020074 /* Use designated initializers, struct layout is non-portable */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075 struct msghdr msg = {
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +020076 .msg_name = (void*)&nladdr,
77 .msg_namelen = sizeof(nladdr),
78 .msg_iov = iov,
79 .msg_iovlen = 2,
80 .msg_control = NULL,
81 .msg_controllen = 0,
82 .msg_flags = 0
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000083 };
84
85 memset(&nladdr, 0, sizeof(nladdr));
86 nladdr.nl_family = AF_NETLINK;
87
88 nlh.nlmsg_len = NLMSG_LENGTH(len);
89 nlh.nlmsg_type = type;
90 nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
91 nlh.nlmsg_pid = 0;
92 nlh.nlmsg_seq = rth->dump = ++rth->seq;
93
94 return sendmsg(rth->fd, &msg, 0);
95}
96
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000097static int rtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020098 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000099 void *arg1/*,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000100 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000101 void *arg2*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000103 int retval = -1;
104 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105 struct sockaddr_nl nladdr;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000106 struct iovec iov = { buf, 8*1024 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107
108 while (1) {
109 int status;
110 struct nlmsghdr *h;
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200111 /* Use designated initializers, struct layout is non-portable */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000112 struct msghdr msg = {
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200113 .msg_name = (void*)&nladdr,
114 .msg_namelen = sizeof(nladdr),
115 .msg_iov = &iov,
116 .msg_iovlen = 1,
117 .msg_control = NULL,
118 .msg_controllen = 0,
119 .msg_flags = 0
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000120 };
121
122 status = recvmsg(rth->fd, &msg, 0);
123
124 if (status < 0) {
125 if (errno == EINTR)
126 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128 continue;
129 }
130 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000132 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 }
134 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 }
137
138 h = (struct nlmsghdr*)buf;
139 while (NLMSG_OK(h, status)) {
140 int err;
141
Eric Andersen6c8161d2004-05-05 07:05:32 +0000142 if (nladdr.nl_pid != 0 ||
143 h->nlmsg_pid != rth->local.nl_pid ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100144 h->nlmsg_seq != rth->dump
145 ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000146// if (junk) {
147// err = junk(&nladdr, h, arg2);
148// if (err < 0) {
149// retval = err;
150// goto ret;
151// }
152// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 goto skip_it;
154 }
155
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000156 if (h->nlmsg_type == NLMSG_DONE) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000157 goto ret_0;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000158 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000159 if (h->nlmsg_type == NLMSG_ERROR) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000160 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 } else {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000164 errno = -l_err->error;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000167 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168 }
169 err = filter(&nladdr, h, arg1);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000170 if (err < 0) {
171 retval = err;
172 goto ret;
173 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174
Denis Vlasenko8723a942007-12-02 06:30:57 +0000175 skip_it:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000176 h = NLMSG_NEXT(h, status);
177 }
178 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000179 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180 continue;
181 }
182 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000183 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000185 } /* while (1) */
186 ret_0:
187 retval++; /* = 0 */
188 ret:
189 free(buf);
190 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191}
192
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000193int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200194 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000195 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
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000203int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200204 pid_t peer, unsigned groups,
205 struct nlmsghdr *answer,
206 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
207 void *jarg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000208{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000209/* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
210#define peer 0
211#define groups 0
212#define junk NULL
213#define jarg NULL
214 int retval = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215 int status;
216 unsigned seq;
217 struct nlmsghdr *h;
218 struct sockaddr_nl nladdr;
219 struct iovec iov = { (void*)n, n->nlmsg_len };
Denis Vlasenko8723a942007-12-02 06:30:57 +0000220 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200221 /* Use designated initializers, struct layout is non-portable */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222 struct msghdr msg = {
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200223 .msg_name = (void*)&nladdr,
224 .msg_namelen = sizeof(nladdr),
225 .msg_iov = &iov,
226 .msg_iovlen = 1,
227 .msg_control = NULL,
228 .msg_controllen = 0,
229 .msg_flags = 0
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000230 };
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) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100244 bb_perror_msg("can't 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 ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100283 h->nlmsg_seq != seq
284 ) {
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;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200340
341 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000342 return -1;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200343 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000344 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
Natanael Copab78ac5a2010-07-23 01:31:24 +0200357 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000358 return -1;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200359 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000360 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
361 rta->rta_type = type;
362 rta->rta_len = len;
363 memcpy(RTA_DATA(rta), data, alen);
364 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
365 return 0;
366}
367
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000368int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000369{
370 int len = RTA_LENGTH(4);
371 struct rtattr *subrta;
372
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000373 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000374 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000375 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000376 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
377 subrta->rta_type = type;
378 subrta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000379 move_to_unaligned32(RTA_DATA(subrta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000380 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
381 return 0;
382}
383
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000384int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000385{
386 struct rtattr *subrta;
387 int len = RTA_LENGTH(alen);
388
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000389 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000390 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000391 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000392 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
393 subrta->rta_type = type;
394 subrta->rta_len = len;
395 memcpy(RTA_DATA(subrta), data, alen);
396 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
397 return 0;
398}
399
400
Natanael Copab78ac5a2010-07-23 01:31:24 +0200401void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000402{
403 while (RTA_OK(rta, len)) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000404 if (rta->rta_type <= max) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000405 tb[rta->rta_type] = rta;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000406 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100407 rta = RTA_NEXT(rta, len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000408 }
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000409 if (len) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000410 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000411 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000412}