blob: 9d5c6416b1f30ec718388faf0c9b1f5159d8ab1e [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{
Denys Vlasenko45482932016-08-14 02:08:56 +020071 struct {
72 struct nlmsghdr nlh;
73 struct msghdr msg;
74 struct sockaddr_nl nladdr;
75 } s;
76 struct iovec iov[2] = { { &s.nlh, sizeof(s.nlh) }, { req, len } };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077
Denys Vlasenko45482932016-08-14 02:08:56 +020078 memset(&s, 0, sizeof(s));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079
Denys Vlasenko45482932016-08-14 02:08:56 +020080 s.msg.msg_name = (void*)&s.nladdr;
81 s.msg.msg_namelen = sizeof(s.nladdr);
82 s.msg.msg_iov = iov;
83 s.msg.msg_iovlen = 2;
84 /*s.msg.msg_control = NULL; - already is */
85 /*s.msg.msg_controllen = 0; - already is */
86 /*s.msg.msg_flags = 0; - already is */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000087
Denys Vlasenko45482932016-08-14 02:08:56 +020088 s.nladdr.nl_family = AF_NETLINK;
89
90 s.nlh.nlmsg_len = NLMSG_LENGTH(len);
91 s.nlh.nlmsg_type = type;
92 s.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
93 /*s.nlh.nlmsg_pid = 0; - already is */
94 s.nlh.nlmsg_seq = rth->dump = ++rth->seq;
95
96 return sendmsg(rth->fd, &s.msg, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097}
98
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000099static int rtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200100 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000101 void *arg1/*,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000102 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000103 void *arg2*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000105 int retval = -1;
106 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107 struct sockaddr_nl nladdr;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000108 struct iovec iov = { buf, 8*1024 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109
110 while (1) {
111 int status;
112 struct nlmsghdr *h;
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200113 /* Use designated initializers, struct layout is non-portable */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000114 struct msghdr msg = {
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200115 .msg_name = (void*)&nladdr,
116 .msg_namelen = sizeof(nladdr),
117 .msg_iov = &iov,
118 .msg_iovlen = 1,
119 .msg_control = NULL,
120 .msg_controllen = 0,
121 .msg_flags = 0
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122 };
123
124 status = recvmsg(rth->fd, &msg, 0);
125
126 if (status < 0) {
127 if (errno == EINTR)
128 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130 continue;
131 }
132 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000134 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000135 }
136 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138 }
139
140 h = (struct nlmsghdr*)buf;
141 while (NLMSG_OK(h, status)) {
142 int err;
143
Eric Andersen6c8161d2004-05-05 07:05:32 +0000144 if (nladdr.nl_pid != 0 ||
145 h->nlmsg_pid != rth->local.nl_pid ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100146 h->nlmsg_seq != rth->dump
147 ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000148// if (junk) {
149// err = junk(&nladdr, h, arg2);
150// if (err < 0) {
151// retval = err;
152// goto ret;
153// }
154// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 goto skip_it;
156 }
157
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000158 if (h->nlmsg_type == NLMSG_DONE) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000159 goto ret_0;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000160 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 if (h->nlmsg_type == NLMSG_ERROR) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000162 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 } else {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000166 errno = -l_err->error;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000169 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 }
171 err = filter(&nladdr, h, arg1);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000172 if (err < 0) {
173 retval = err;
174 goto ret;
175 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000176
Denis Vlasenko8723a942007-12-02 06:30:57 +0000177 skip_it:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178 h = NLMSG_NEXT(h, status);
179 }
180 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000181 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000182 continue;
183 }
184 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000185 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000186 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000187 } /* while (1) */
188 ret_0:
189 retval++; /* = 0 */
190 ret:
191 free(buf);
192 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193}
194
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000195int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200196 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000197 void *arg1)
198{
199 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
200 if (ret < 0)
201 bb_error_msg_and_die("dump terminated");
202 return ret;
203}
204
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000205int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200206 pid_t peer, unsigned groups,
207 struct nlmsghdr *answer,
208 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
209 void *jarg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000210{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000211/* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
212#define peer 0
213#define groups 0
214#define junk NULL
215#define jarg NULL
216 int retval = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000217 int status;
218 unsigned seq;
219 struct nlmsghdr *h;
220 struct sockaddr_nl nladdr;
221 struct iovec iov = { (void*)n, n->nlmsg_len };
Denis Vlasenko8723a942007-12-02 06:30:57 +0000222 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200223 /* Use designated initializers, struct layout is non-portable */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 struct msghdr msg = {
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200225 .msg_name = (void*)&nladdr,
226 .msg_namelen = sizeof(nladdr),
227 .msg_iov = &iov,
228 .msg_iovlen = 1,
229 .msg_control = NULL,
230 .msg_controllen = 0,
231 .msg_flags = 0
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000232 };
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;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200342
343 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000344 return -1;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200345 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000346 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
347 rta->rta_type = type;
348 rta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000349 move_to_unaligned32(RTA_DATA(rta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000350 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
351 return 0;
352}
353
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000354int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000355{
356 int len = RTA_LENGTH(alen);
357 struct rtattr *rta;
358
Natanael Copab78ac5a2010-07-23 01:31:24 +0200359 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000360 return -1;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200361 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000362 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
363 rta->rta_type = type;
364 rta->rta_len = len;
365 memcpy(RTA_DATA(rta), data, alen);
366 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
367 return 0;
368}
369
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000370int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371{
372 int len = RTA_LENGTH(4);
373 struct rtattr *subrta;
374
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000375 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000376 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000377 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000378 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
379 subrta->rta_type = type;
380 subrta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000381 move_to_unaligned32(RTA_DATA(subrta), data);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000382 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
383 return 0;
384}
385
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000386int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000387{
388 struct rtattr *subrta;
389 int len = RTA_LENGTH(alen);
390
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000391 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000392 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000393 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000394 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
395 subrta->rta_type = type;
396 subrta->rta_len = len;
397 memcpy(RTA_DATA(subrta), data, alen);
398 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
399 return 0;
400}
401
402
Natanael Copab78ac5a2010-07-23 01:31:24 +0200403void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000404{
405 while (RTA_OK(rta, len)) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000406 if (rta->rta_type <= max) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000407 tb[rta->rta_type] = rta;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000408 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100409 rta = RTA_NEXT(rta, len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000410 }
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000411 if (len) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000412 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000413 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000414}