blob: b0d4166ac47906c218640f09bfa22ccc77cff02a [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 */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000010#include <sys/socket.h>
Eric Andersen0f08e532003-06-20 09:05:00 +000011#include <sys/uio.h>
12
Denis Vlasenkofad2b862007-05-31 22:16:38 +000013#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014#include "libnetlink.h"
15
Natanael Copab78ac5a2010-07-23 01:31:24 +020016void FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017{
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000018 memset(rth, 0, sizeof(*rth));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000019 rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020 rth->local.nl_family = AF_NETLINK;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000021 /*rth->local.nl_groups = subscriptions;*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000022
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000023 xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
Denys Vlasenkoba3b9db2018-02-11 14:55:46 +010024 bb_getsockname(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000025
26/* too much paranoia
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000027 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +000028 bb_perror_msg_and_die("getsockname");
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000029 if (addr_len != sizeof(rth->local))
30 bb_error_msg_and_die("wrong address length %d", addr_len);
31 if (rth->local.nl_family != AF_NETLINK)
32 bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +000033*/
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034 rth->seq = time(NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000035}
36
Denys Vlasenko028c5aa2019-05-22 13:54:46 +020037void FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038{
39 struct {
40 struct nlmsghdr nlh;
41 struct rtgenmsg g;
42 } req;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000043
44 req.nlh.nlmsg_len = sizeof(req);
45 req.nlh.nlmsg_type = type;
46 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
47 req.nlh.nlmsg_pid = 0;
48 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
49 req.g.rtgen_family = family;
50
Denys Vlasenko028c5aa2019-05-22 13:54:46 +020051 rtnl_send(rth, (void*)&req, sizeof(req));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000052}
53
Denys Vlasenko028c5aa2019-05-22 13:54:46 +020054/* A version which checks for e.g. EPERM errors.
55 * Try: setuidgid 1:1 ip addr flush dev eth0
56 */
57int FAST_FUNC rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058{
Denys Vlasenko028c5aa2019-05-22 13:54:46 +020059 struct nlmsghdr *h;
60 int status;
61 char resp[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062
Denys Vlasenko028c5aa2019-05-22 13:54:46 +020063 status = write(rth->fd, buf, len);
64 if (status < 0)
65 return status;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066
Denys Vlasenko028c5aa2019-05-22 13:54:46 +020067 /* Check for immediate errors */
68 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
69 if (status < 0) {
70 if (errno == EAGAIN) /* if no error, this happens */
71 return 0;
72 return -1;
73 }
74
75 for (h = (struct nlmsghdr *)resp;
76 NLMSG_OK(h, status);
77 h = NLMSG_NEXT(h, status)
78 ) {
79 if (h->nlmsg_type == NLMSG_ERROR) {
80 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
81 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
82 bb_error_msg("ERROR truncated");
83 else
84 errno = -err->error;
85 return -1;
86 }
87 }
88
89 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090}
91
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +000092int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093{
Denys Vlasenko45482932016-08-14 02:08:56 +020094 struct {
95 struct nlmsghdr nlh;
96 struct msghdr msg;
97 struct sockaddr_nl nladdr;
98 } s;
99 struct iovec iov[2] = { { &s.nlh, sizeof(s.nlh) }, { req, len } };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100
Denys Vlasenko45482932016-08-14 02:08:56 +0200101 memset(&s, 0, sizeof(s));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102
Denys Vlasenko45482932016-08-14 02:08:56 +0200103 s.msg.msg_name = (void*)&s.nladdr;
104 s.msg.msg_namelen = sizeof(s.nladdr);
105 s.msg.msg_iov = iov;
106 s.msg.msg_iovlen = 2;
107 /*s.msg.msg_control = NULL; - already is */
108 /*s.msg.msg_controllen = 0; - already is */
109 /*s.msg.msg_flags = 0; - already is */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000110
Denys Vlasenko45482932016-08-14 02:08:56 +0200111 s.nladdr.nl_family = AF_NETLINK;
112
113 s.nlh.nlmsg_len = NLMSG_LENGTH(len);
114 s.nlh.nlmsg_type = type;
115 s.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
116 /*s.nlh.nlmsg_pid = 0; - already is */
117 s.nlh.nlmsg_seq = rth->dump = ++rth->seq;
118
119 return sendmsg(rth->fd, &s.msg, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000120}
121
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000122static int rtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200123 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000124 void *arg1/*,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000125 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000126 void *arg2*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000128 int retval = -1;
129 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130 struct sockaddr_nl nladdr;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000131 struct iovec iov = { buf, 8*1024 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000132
133 while (1) {
134 int status;
135 struct nlmsghdr *h;
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200136 /* Use designated initializers, struct layout is non-portable */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137 struct msghdr msg = {
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200138 .msg_name = (void*)&nladdr,
139 .msg_namelen = sizeof(nladdr),
140 .msg_iov = &iov,
141 .msg_iovlen = 1,
142 .msg_control = NULL,
143 .msg_controllen = 0,
144 .msg_flags = 0
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145 };
146
147 status = recvmsg(rth->fd, &msg, 0);
148
149 if (status < 0) {
150 if (errno == EINTR)
151 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 continue;
154 }
155 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000157 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000158 }
159 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 }
162
163 h = (struct nlmsghdr*)buf;
164 while (NLMSG_OK(h, status)) {
165 int err;
166
Eric Andersen6c8161d2004-05-05 07:05:32 +0000167 if (nladdr.nl_pid != 0 ||
168 h->nlmsg_pid != rth->local.nl_pid ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100169 h->nlmsg_seq != rth->dump
170 ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000171// if (junk) {
172// err = junk(&nladdr, h, arg2);
173// if (err < 0) {
174// retval = err;
175// goto ret;
176// }
177// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178 goto skip_it;
179 }
180
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000181 if (h->nlmsg_type == NLMSG_DONE) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000182 goto ret_0;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000183 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 if (h->nlmsg_type == NLMSG_ERROR) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000185 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000186 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000187 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000188 } else {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000189 errno = -l_err->error;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000190 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000192 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193 }
194 err = filter(&nladdr, h, arg1);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000195 if (err < 0) {
196 retval = err;
197 goto ret;
198 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199
Denis Vlasenko8723a942007-12-02 06:30:57 +0000200 skip_it:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201 h = NLMSG_NEXT(h, status);
202 }
203 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000204 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000205 continue;
206 }
207 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000208 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000210 } /* while (1) */
211 ret_0:
212 retval++; /* = 0 */
213 ret:
214 free(buf);
215 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000216}
217
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000218int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200219 int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000220 void *arg1)
221{
222 int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
223 if (ret < 0)
224 bb_error_msg_and_die("dump terminated");
225 return ret;
226}
227
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000228int FAST_FUNC rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200229 pid_t peer, unsigned groups,
230 struct nlmsghdr *answer,
231 int (*junk)(struct sockaddr_nl *, struct nlmsghdr *, void *),
232 void *jarg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000233{
Denis Vlasenko8723a942007-12-02 06:30:57 +0000234/* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
235#define peer 0
236#define groups 0
237#define junk NULL
238#define jarg NULL
239 int retval = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000240 int status;
241 unsigned seq;
242 struct nlmsghdr *h;
243 struct sockaddr_nl nladdr;
244 struct iovec iov = { (void*)n, n->nlmsg_len };
Denis Vlasenko8723a942007-12-02 06:30:57 +0000245 char *buf = xmalloc(8*1024); /* avoid big stack buffer */
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200246 /* Use designated initializers, struct layout is non-portable */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247 struct msghdr msg = {
Szabolcs Nagy4ab372d2016-04-24 17:39:02 +0200248 .msg_name = (void*)&nladdr,
249 .msg_namelen = sizeof(nladdr),
250 .msg_iov = &iov,
251 .msg_iovlen = 1,
252 .msg_control = NULL,
253 .msg_controllen = 0,
254 .msg_flags = 0
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 };
256
257 memset(&nladdr, 0, sizeof(nladdr));
258 nladdr.nl_family = AF_NETLINK;
Denis Vlasenko8723a942007-12-02 06:30:57 +0000259// nladdr.nl_pid = peer;
260// nladdr.nl_groups = groups;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000261
262 n->nlmsg_seq = seq = ++rtnl->seq;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000263 if (answer == NULL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 n->nlmsg_flags |= NLM_F_ACK;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000265 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 status = sendmsg(rtnl->fd, &msg, 0);
267
268 if (status < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100269 bb_perror_msg("can't talk to rtnetlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000270 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000271 }
272
273 iov.iov_base = buf;
274
275 while (1) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000276 iov.iov_len = 8*1024;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 status = recvmsg(rtnl->fd, &msg, 0);
278
279 if (status < 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000280 if (errno == EINTR) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 continue;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000282 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000283 bb_perror_msg("OVERRUN");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000284 continue;
285 }
286 if (status == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000287 bb_error_msg("EOF on netlink");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000288 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000289 }
290 if (msg.msg_namelen != sizeof(nladdr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000291 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292 }
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000293 for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000294// int l_err;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000295 int len = h->nlmsg_len;
296 int l = len - sizeof(*h);
297
Denis Vlasenko8723a942007-12-02 06:30:57 +0000298 if (l < 0 || len > status) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000299 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000300 bb_error_msg("truncated message");
Denis Vlasenko8723a942007-12-02 06:30:57 +0000301 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000302 }
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000303 bb_error_msg_and_die("malformed message: len=%d!", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000304 }
305
Eric Andersen6c8161d2004-05-05 07:05:32 +0000306 if (nladdr.nl_pid != peer ||
307 h->nlmsg_pid != rtnl->local.nl_pid ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100308 h->nlmsg_seq != seq
309 ) {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000310// if (junk) {
311// l_err = junk(&nladdr, h, jarg);
312// if (l_err < 0) {
313// retval = l_err;
314// goto ret;
315// }
316// }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000317 continue;
318 }
319
320 if (h->nlmsg_type == NLMSG_ERROR) {
321 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000322 if (l < (int)sizeof(struct nlmsgerr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000323 bb_error_msg("ERROR truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000324 } else {
Denis Vlasenko8723a942007-12-02 06:30:57 +0000325 errno = - err->error;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000326 if (errno == 0) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000327 if (answer) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000328 memcpy(answer, h, h->nlmsg_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000329 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000330 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000331 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000332 bb_perror_msg("RTNETLINK answers");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000333 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000334 goto ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000335 }
336 if (answer) {
337 memcpy(answer, h, h->nlmsg_len);
Denis Vlasenko8723a942007-12-02 06:30:57 +0000338 goto ret_0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000339 }
340
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000341 bb_error_msg("unexpected reply!");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000342
343 status -= NLMSG_ALIGN(len);
344 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
345 }
346 if (msg.msg_flags & MSG_TRUNC) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000347 bb_error_msg("message truncated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000348 continue;
349 }
350 if (status) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000351 bb_error_msg_and_die("remnant of size %d!", status);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000352 }
Denis Vlasenko8723a942007-12-02 06:30:57 +0000353 } /* while (1) */
354 ret_0:
355 retval++; /* = 0 */
356 ret:
357 free(buf);
358 return retval;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000359}
360
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000361int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000362{
363 int len = RTA_LENGTH(4);
364 struct rtattr *rta;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200365
Hauke Mehrtens4ff86df2016-09-16 22:40:28 +0200366 if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000367 return -1;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200368 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000369 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
370 rta->rta_type = type;
371 rta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000372 move_to_unaligned32(RTA_DATA(rta), data);
Hauke Mehrtens4ff86df2016-09-16 22:40:28 +0200373 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000374 return 0;
375}
376
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000377int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000378{
379 int len = RTA_LENGTH(alen);
380 struct rtattr *rta;
381
Hauke Mehrtens4ff86df2016-09-16 22:40:28 +0200382 if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000383 return -1;
Natanael Copab78ac5a2010-07-23 01:31:24 +0200384 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000385 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
386 rta->rta_type = type;
387 rta->rta_len = len;
388 memcpy(RTA_DATA(rta), data, alen);
Hauke Mehrtens4ff86df2016-09-16 22:40:28 +0200389 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000390 return 0;
391}
392
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000393int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000394{
395 int len = RTA_LENGTH(4);
396 struct rtattr *subrta;
397
Hauke Mehrtens4ff86df2016-09-16 22:40:28 +0200398 if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000399 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000400 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
402 subrta->rta_type = type;
403 subrta->rta_len = len;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000404 move_to_unaligned32(RTA_DATA(subrta), data);
Hauke Mehrtens4ff86df2016-09-16 22:40:28 +0200405 rta->rta_len = NLMSG_ALIGN(rta->rta_len + len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000406 return 0;
407}
408
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000409int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000410{
411 struct rtattr *subrta;
412 int len = RTA_LENGTH(alen);
413
Hauke Mehrtens4ff86df2016-09-16 22:40:28 +0200414 if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000415 return -1;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000416 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000417 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
418 subrta->rta_type = type;
419 subrta->rta_len = len;
420 memcpy(RTA_DATA(subrta), data, alen);
Hauke Mehrtens4ff86df2016-09-16 22:40:28 +0200421 rta->rta_len = NLMSG_ALIGN(rta->rta_len + len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000422 return 0;
423}
424
425
Natanael Copab78ac5a2010-07-23 01:31:24 +0200426void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000427{
Denys Vlasenko68ae5422018-02-08 08:42:37 +0100428 memset(tb, 0, (max + 1) * sizeof(tb[0]));
429
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000430 while (RTA_OK(rta, len)) {
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000431 if (rta->rta_type <= max) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000432 tb[rta->rta_type] = rta;
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000433 }
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100434 rta = RTA_NEXT(rta, len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000435 }
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000436 if (len) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000437 bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
Glenn L McGrathd4de9752002-11-28 12:35:46 +0000438 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000439}