blob: 67f046fbc49e508a3e260a497942eca508b6d82a [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * libnetlink.c RTnetlink service routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
18#include <net/if_arp.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <string.h>
22#include <errno.h>
23#include <time.h>
24#include <sys/uio.h>
25
26#include "libnetlink.h"
27
Patrick McHardy7f031912009-10-28 20:50:48 +010028int rcvbuf = 1024 * 1024;
29
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030void rtnl_close(struct rtnl_handle *rth)
31{
Stephen Hemminger3bfa73f2006-09-26 10:41:57 -070032 if (rth->fd >= 0) {
33 close(rth->fd);
34 rth->fd = -1;
35 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000036}
37
shemminger8ed63ab2005-09-21 19:33:17 +000038int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
39 int protocol)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000040{
shemmingerf332d162005-07-05 22:37:15 +000041 socklen_t addr_len;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +000042 int sndbuf = 32768;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000043
Stephen Hemmingerb16621c2007-05-10 19:33:21 -070044 memset(rth, 0, sizeof(*rth));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000046 rth->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000047 if (rth->fd < 0) {
48 perror("Cannot open netlink socket");
49 return -1;
50 }
51
osdl.net!shemminger007d3a32004-08-13 23:54:55 +000052 if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) {
53 perror("SO_SNDBUF");
54 return -1;
55 }
56
57 if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) {
58 perror("SO_RCVBUF");
59 return -1;
60 }
61
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062 memset(&rth->local, 0, sizeof(rth->local));
63 rth->local.nl_family = AF_NETLINK;
64 rth->local.nl_groups = subscriptions;
65
66 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
67 perror("Cannot bind netlink socket");
68 return -1;
69 }
70 addr_len = sizeof(rth->local);
71 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
72 perror("Cannot getsockname");
73 return -1;
74 }
75 if (addr_len != sizeof(rth->local)) {
76 fprintf(stderr, "Wrong address length %d\n", addr_len);
77 return -1;
78 }
79 if (rth->local.nl_family != AF_NETLINK) {
80 fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family);
81 return -1;
82 }
83 rth->seq = time(NULL);
84 return 0;
85}
86
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000087int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
88{
89 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
90}
91
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000092int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
93{
Vlad Yasevich9eff0e52013-02-28 10:04:05 +000094 return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF);
95}
96
97int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int family, int type,
98 __u32 filt_mask)
99{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000100 struct {
101 struct nlmsghdr nlh;
102 struct rtgenmsg g;
Lutz Jaenicke257422f2012-08-30 05:01:34 +0000103 /* attribute has to be NLMSG aligned */
104 struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO)));
Rose, Gregory Vbd886eb2012-02-21 10:43:09 +0000105 __u32 ext_filter_mask;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106 } req;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000107
shemminger8ed63ab2005-09-21 19:33:17 +0000108 memset(&req, 0, sizeof(req));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000109 req.nlh.nlmsg_len = sizeof(req);
110 req.nlh.nlmsg_type = type;
Masatake YAMATOaa38c3e2012-01-19 14:16:12 -0800111 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000112 req.nlh.nlmsg_pid = 0;
113 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
114 req.g.rtgen_family = family;
115
Rose, Gregory Vbd886eb2012-02-21 10:43:09 +0000116 req.ext_req.rta_type = IFLA_EXT_MASK;
117 req.ext_req.rta_len = RTA_LENGTH(sizeof(__u32));
Vlad Yasevich9eff0e52013-02-28 10:04:05 +0000118 req.ext_filter_mask = filt_mask;
Rose, Gregory Vbd886eb2012-02-21 10:43:09 +0000119
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800120 return send(rth->fd, (void*)&req, sizeof(req), 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000121}
122
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800123int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000124{
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800125 return send(rth->fd, buf, len, 0);
126}
127
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800128int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800129{
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800130 struct nlmsghdr *h;
131 int status;
132 char resp[1024];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000133
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800134 status = send(rth->fd, buf, len, 0);
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800135 if (status < 0)
136 return status;
137
Stephen Hemminger2d8240f2009-07-13 10:15:23 -0700138 /* Check for immediate errors */
139 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800140 if (status < 0) {
141 if (errno == EAGAIN)
142 return 0;
143 return -1;
144 }
145
146 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
147 h = NLMSG_NEXT(h, status)) {
148 if (h->nlmsg_type == NLMSG_ERROR) {
149 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
150 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
151 fprintf(stderr, "ERROR truncated\n");
152 else
153 errno = -err->error;
Sven Anders24f38182009-11-10 09:07:26 -0800154 return -1;
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800155 }
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800156 }
157
158 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000159}
160
161int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
162{
163 struct nlmsghdr nlh;
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800164 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
shemminger8ed63ab2005-09-21 19:33:17 +0000165 struct iovec iov[2] = {
166 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
167 { .iov_base = req, .iov_len = len }
168 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000169 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000170 .msg_name = &nladdr,
171 .msg_namelen = sizeof(nladdr),
172 .msg_iov = iov,
173 .msg_iovlen = 2,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000174 };
175
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000176 nlh.nlmsg_len = NLMSG_LENGTH(len);
177 nlh.nlmsg_type = type;
Masatake YAMATOaa38c3e2012-01-19 14:16:12 -0800178 nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000179 nlh.nlmsg_pid = 0;
180 nlh.nlmsg_seq = rth->dump = ++rth->seq;
181
182 return sendmsg(rth->fd, &msg, 0);
183}
184
Simon Hormanb49240e2009-12-03 12:08:27 +1100185int rtnl_dump_filter_l(struct rtnl_handle *rth,
186 const struct rtnl_dump_filter_arg *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000187{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000188 struct sockaddr_nl nladdr;
shemminger8ed63ab2005-09-21 19:33:17 +0000189 struct iovec iov;
190 struct msghdr msg = {
191 .msg_name = &nladdr,
192 .msg_namelen = sizeof(nladdr),
193 .msg_iov = &iov,
194 .msg_iovlen = 1,
195 };
196 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000197
shemminger8ed63ab2005-09-21 19:33:17 +0000198 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000199 while (1) {
200 int status;
Simon Hormanb49240e2009-12-03 12:08:27 +1100201 const struct rtnl_dump_filter_arg *a;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700202 int found_done = 0;
203 int msglen = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000204
shemminger8ed63ab2005-09-21 19:33:17 +0000205 iov.iov_len = sizeof(buf);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000206 status = recvmsg(rth->fd, &msg, 0);
207
208 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800209 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000210 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800211 fprintf(stderr, "netlink receive error %s (%d)\n",
212 strerror(errno), errno);
213 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000214 }
shemminger8ed63ab2005-09-21 19:33:17 +0000215
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000216 if (status == 0) {
217 fprintf(stderr, "EOF on netlink\n");
218 return -1;
219 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000220
Simon Hormanb49240e2009-12-03 12:08:27 +1100221 for (a = arg; a->filter; a++) {
222 struct nlmsghdr *h = (struct nlmsghdr*)buf;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700223 msglen = status;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000224
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700225 while (NLMSG_OK(h, msglen)) {
Simon Hormanb49240e2009-12-03 12:08:27 +1100226 int err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000227
Simon Hormanb49240e2009-12-03 12:08:27 +1100228 if (nladdr.nl_pid != 0 ||
229 h->nlmsg_pid != rth->local.nl_pid ||
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800230 h->nlmsg_seq != rth->dump)
Simon Hormanb49240e2009-12-03 12:08:27 +1100231 goto skip_it;
Simon Hormanb49240e2009-12-03 12:08:27 +1100232
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700233 if (h->nlmsg_type == NLMSG_DONE) {
234 found_done = 1;
235 break; /* process next filter */
236 }
Simon Hormanb49240e2009-12-03 12:08:27 +1100237 if (h->nlmsg_type == NLMSG_ERROR) {
238 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
239 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
240 fprintf(stderr,
241 "ERROR truncated\n");
242 } else {
243 errno = -err->error;
244 perror("RTNETLINK answers");
245 }
246 return -1;
247 }
248 err = a->filter(&nladdr, h, a->arg1);
249 if (err < 0)
250 return err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000251
252skip_it:
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700253 h = NLMSG_NEXT(h, msglen);
Simon Hormanb49240e2009-12-03 12:08:27 +1100254 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700255 }
256
257 if (found_done)
258 return 0;
259
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000260 if (msg.msg_flags & MSG_TRUNC) {
261 fprintf(stderr, "Message truncated\n");
262 continue;
263 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700264 if (msglen) {
265 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000266 exit(1);
267 }
268 }
269}
270
Simon Hormanb49240e2009-12-03 12:08:27 +1100271int rtnl_dump_filter(struct rtnl_handle *rth,
272 rtnl_filter_t filter,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800273 void *arg1)
Simon Hormanb49240e2009-12-03 12:08:27 +1100274{
275 const struct rtnl_dump_filter_arg a[2] = {
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800276 { .filter = filter, .arg1 = arg1, },
277 { .filter = NULL, .arg1 = NULL, },
Simon Hormanb49240e2009-12-03 12:08:27 +1100278 };
279
280 return rtnl_dump_filter_l(rth, a);
281}
282
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000283int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800284 unsigned groups, struct nlmsghdr *answer)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000285{
286 int status;
287 unsigned seq;
288 struct nlmsghdr *h;
289 struct sockaddr_nl nladdr;
shemmingerfb229752005-10-04 23:15:32 +0000290 struct iovec iov = {
291 .iov_base = (void*) n,
292 .iov_len = n->nlmsg_len
293 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000294 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000295 .msg_name = &nladdr,
296 .msg_namelen = sizeof(nladdr),
297 .msg_iov = &iov,
298 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000299 };
shemminger8ed63ab2005-09-21 19:33:17 +0000300 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000301
302 memset(&nladdr, 0, sizeof(nladdr));
303 nladdr.nl_family = AF_NETLINK;
304 nladdr.nl_pid = peer;
305 nladdr.nl_groups = groups;
306
307 n->nlmsg_seq = seq = ++rtnl->seq;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000308
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000309 if (answer == NULL)
310 n->nlmsg_flags |= NLM_F_ACK;
311
312 status = sendmsg(rtnl->fd, &msg, 0);
313
314 if (status < 0) {
315 perror("Cannot talk to rtnetlink");
316 return -1;
317 }
318
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000319 memset(buf,0,sizeof(buf));
320
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000321 iov.iov_base = buf;
322
323 while (1) {
324 iov.iov_len = sizeof(buf);
325 status = recvmsg(rtnl->fd, &msg, 0);
326
327 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800328 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000329 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800330 fprintf(stderr, "netlink receive error %s (%d)\n",
331 strerror(errno), errno);
332 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000333 }
334 if (status == 0) {
335 fprintf(stderr, "EOF on netlink\n");
336 return -1;
337 }
338 if (msg.msg_namelen != sizeof(nladdr)) {
339 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
340 exit(1);
341 }
342 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000343 int len = h->nlmsg_len;
344 int l = len - sizeof(*h);
345
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800346 if (l < 0 || len>status) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000347 if (msg.msg_flags & MSG_TRUNC) {
348 fprintf(stderr, "Truncated message\n");
349 return -1;
350 }
351 fprintf(stderr, "!!!malformed message: len=%d\n", len);
352 exit(1);
353 }
354
org[shemminger]!shemminger10f57ef2004-06-07 22:04:04 +0000355 if (nladdr.nl_pid != peer ||
356 h->nlmsg_pid != rtnl->local.nl_pid ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000357 h->nlmsg_seq != seq) {
shemminger4cca16f2006-03-10 23:31:46 +0000358 /* Don't forget to skip that message. */
359 status -= NLMSG_ALIGN(len);
360 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000361 continue;
362 }
363
364 if (h->nlmsg_type == NLMSG_ERROR) {
365 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
366 if (l < sizeof(struct nlmsgerr)) {
367 fprintf(stderr, "ERROR truncated\n");
368 } else {
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400369 if (!err->error) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000370 if (answer)
371 memcpy(answer, h, h->nlmsg_len);
372 return 0;
373 }
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400374
375 fprintf(stderr, "RTNETLINK answers: %s\n", strerror(-err->error));
376 errno = -err->error;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000377 }
378 return -1;
379 }
380 if (answer) {
381 memcpy(answer, h, h->nlmsg_len);
382 return 0;
383 }
384
385 fprintf(stderr, "Unexpected reply!!!\n");
386
387 status -= NLMSG_ALIGN(len);
388 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
389 }
390 if (msg.msg_flags & MSG_TRUNC) {
391 fprintf(stderr, "Message truncated\n");
392 continue;
393 }
394 if (status) {
395 fprintf(stderr, "!!!Remnant of size %d\n", status);
396 exit(1);
397 }
398 }
399}
400
shemminger8ed63ab2005-09-21 19:33:17 +0000401int rtnl_listen(struct rtnl_handle *rtnl,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000402 rtnl_filter_t handler,
403 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000404{
405 int status;
406 struct nlmsghdr *h;
407 struct sockaddr_nl nladdr;
408 struct iovec iov;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000409 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000410 .msg_name = &nladdr,
411 .msg_namelen = sizeof(nladdr),
412 .msg_iov = &iov,
413 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000414 };
shemminger8ed63ab2005-09-21 19:33:17 +0000415 char buf[8192];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000416
417 memset(&nladdr, 0, sizeof(nladdr));
418 nladdr.nl_family = AF_NETLINK;
419 nladdr.nl_pid = 0;
420 nladdr.nl_groups = 0;
421
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000422 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000423 while (1) {
424 iov.iov_len = sizeof(buf);
425 status = recvmsg(rtnl->fd, &msg, 0);
426
427 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800428 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000429 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800430 fprintf(stderr, "netlink receive error %s (%d)\n",
431 strerror(errno), errno);
Patrick McHardy7f031912009-10-28 20:50:48 +0100432 if (errno == ENOBUFS)
433 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800434 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000435 }
436 if (status == 0) {
437 fprintf(stderr, "EOF on netlink\n");
438 return -1;
439 }
440 if (msg.msg_namelen != sizeof(nladdr)) {
441 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
442 exit(1);
443 }
444 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
445 int err;
446 int len = h->nlmsg_len;
447 int l = len - sizeof(*h);
448
449 if (l<0 || len>status) {
450 if (msg.msg_flags & MSG_TRUNC) {
451 fprintf(stderr, "Truncated message\n");
452 return -1;
453 }
454 fprintf(stderr, "!!!malformed message: len=%d\n", len);
455 exit(1);
456 }
457
458 err = handler(&nladdr, h, jarg);
459 if (err < 0)
460 return err;
461
462 status -= NLMSG_ALIGN(len);
463 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
464 }
465 if (msg.msg_flags & MSG_TRUNC) {
466 fprintf(stderr, "Message truncated\n");
467 continue;
468 }
469 if (status) {
470 fprintf(stderr, "!!!Remnant of size %d\n", status);
471 exit(1);
472 }
473 }
474}
475
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000476int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
477 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000478{
479 int status;
480 struct sockaddr_nl nladdr;
481 char buf[8192];
482 struct nlmsghdr *h = (void*)buf;
483
484 memset(&nladdr, 0, sizeof(nladdr));
485 nladdr.nl_family = AF_NETLINK;
486 nladdr.nl_pid = 0;
487 nladdr.nl_groups = 0;
488
489 while (1) {
Stephen Hemminger2dd9f8e2011-06-20 14:34:46 -0700490 int err, len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000491 int l;
492
493 status = fread(&buf, 1, sizeof(*h), rtnl);
494
495 if (status < 0) {
496 if (errno == EINTR)
497 continue;
498 perror("rtnl_from_file: fread");
499 return -1;
500 }
501 if (status == 0)
502 return 0;
503
504 len = h->nlmsg_len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000505 l = len - sizeof(*h);
506
507 if (l<0 || len>sizeof(buf)) {
508 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
509 len, ftell(rtnl));
510 return -1;
511 }
512
513 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
514
515 if (status < 0) {
516 perror("rtnl_from_file: fread");
517 return -1;
518 }
519 if (status < l) {
520 fprintf(stderr, "rtnl-from_file: truncated message\n");
521 return -1;
522 }
523
524 err = handler(&nladdr, h, jarg);
525 if (err < 0)
526 return err;
527 }
528}
529
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800530int addattr(struct nlmsghdr *n, int maxlen, int type)
531{
532 return addattr_l(n, maxlen, type, NULL, 0);
533}
534
535int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
536{
537 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
538}
539
540int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
541{
542 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
543}
544
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000545int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
546{
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800547 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
548}
549
550int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
551{
552 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
553}
554
555int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
556{
557 return addattr_l(n, maxlen, type, str, strlen(str)+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000558}
559
shemminger8ed63ab2005-09-21 19:33:17 +0000560int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000561 int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000562{
563 int len = RTA_LENGTH(alen);
564 struct rtattr *rta;
565
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000566 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000567 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000568 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000569 }
6!tgraf07f94362005-01-18 13:58:49 +0000570 rta = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000571 rta->rta_type = type;
572 rta->rta_len = len;
573 memcpy(RTA_DATA(rta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000574 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000575 return 0;
576}
577
6!tgraf07f94362005-01-18 13:58:49 +0000578int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
579{
580 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
581 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
582 return -1;
583 }
584
585 memcpy(NLMSG_TAIL(n), data, len);
586 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
587 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
588 return 0;
589}
590
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700591struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
592{
593 struct rtattr *nest = NLMSG_TAIL(n);
594
595 addattr_l(n, maxlen, type, NULL, 0);
596 return nest;
597}
598
599int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
600{
601 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
602 return n->nlmsg_len;
603}
604
605struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
606 const void *data, int len)
607{
608 struct rtattr *start = NLMSG_TAIL(n);
609
610 addattr_l(n, maxlen, type, data, len);
611 addattr_nest(n, maxlen, type);
612 return start;
613}
614
615int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
616{
617 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
618
619 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
620 addattr_nest_end(n, nest);
621 return n->nlmsg_len;
622}
623
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000624int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
625{
626 int len = RTA_LENGTH(4);
627 struct rtattr *subrta;
628
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000629 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
630 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000631 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000632 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000633 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
634 subrta->rta_type = type;
635 subrta->rta_len = len;
636 memcpy(RTA_DATA(subrta), &data, 4);
637 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
638 return 0;
639}
640
shemminger8ed63ab2005-09-21 19:33:17 +0000641int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000642 const void *data, int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000643{
644 struct rtattr *subrta;
645 int len = RTA_LENGTH(alen);
646
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000647 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000648 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000649 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000650 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000651 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
652 subrta->rta_type = type;
653 subrta->rta_len = len;
654 memcpy(RTA_DATA(subrta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000655 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000656 return 0;
657}
658
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000659int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
660{
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000661 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000662 while (RTA_OK(rta, len)) {
Williams, Mitch A6e46ec82010-02-10 01:46:47 +0000663 if ((rta->rta_type <= max) && (!tb[rta->rta_type]))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000664 tb[rta->rta_type] = rta;
665 rta = RTA_NEXT(rta,len);
666 }
667 if (len)
668 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
669 return 0;
670}
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000671
672int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
673{
674 int i = 0;
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000675
676 memset(tb, 0, sizeof(struct rtattr *) * max);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000677 while (RTA_OK(rta, len)) {
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000678 if (rta->rta_type <= max && i < max)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000679 tb[i++] = rta;
680 rta = RTA_NEXT(rta,len);
681 }
682 if (len)
683 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
684 return i;
685}
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700686
687int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
688 int len)
689{
690 if (RTA_PAYLOAD(rta) < len)
691 return -1;
692 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
693 rta = RTA_DATA(rta) + RTA_ALIGN(len);
694 return parse_rtattr_nested(tb, max, rta);
695 }
Stephen Hemminger037c6352007-12-10 11:34:40 -0800696 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700697 return 0;
698}