blob: b17e1aae9b69e3e497263a41f629b2debe90dd99 [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;
Alexander Duyck63338dc2013-04-25 12:07:10 +0000102 struct ifinfomsg ifm;
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;
Alexander Duyck63338dc2013-04-25 12:07:10 +0000114 req.ifm.ifi_family = family;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000115
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];
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000197 int dump_intr = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000198
shemminger8ed63ab2005-09-21 19:33:17 +0000199 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000200 while (1) {
201 int status;
Simon Hormanb49240e2009-12-03 12:08:27 +1100202 const struct rtnl_dump_filter_arg *a;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700203 int found_done = 0;
204 int msglen = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000205
shemminger8ed63ab2005-09-21 19:33:17 +0000206 iov.iov_len = sizeof(buf);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000207 status = recvmsg(rth->fd, &msg, 0);
208
209 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800210 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000211 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800212 fprintf(stderr, "netlink receive error %s (%d)\n",
213 strerror(errno), errno);
214 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000215 }
shemminger8ed63ab2005-09-21 19:33:17 +0000216
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000217 if (status == 0) {
218 fprintf(stderr, "EOF on netlink\n");
219 return -1;
220 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000221
Simon Hormanb49240e2009-12-03 12:08:27 +1100222 for (a = arg; a->filter; a++) {
223 struct nlmsghdr *h = (struct nlmsghdr*)buf;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700224 msglen = status;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000225
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700226 while (NLMSG_OK(h, msglen)) {
Simon Hormanb49240e2009-12-03 12:08:27 +1100227 int err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000228
Simon Hormanb49240e2009-12-03 12:08:27 +1100229 if (nladdr.nl_pid != 0 ||
230 h->nlmsg_pid != rth->local.nl_pid ||
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800231 h->nlmsg_seq != rth->dump)
Simon Hormanb49240e2009-12-03 12:08:27 +1100232 goto skip_it;
Simon Hormanb49240e2009-12-03 12:08:27 +1100233
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000234 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
235 dump_intr = 1;
236
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700237 if (h->nlmsg_type == NLMSG_DONE) {
238 found_done = 1;
239 break; /* process next filter */
240 }
Simon Hormanb49240e2009-12-03 12:08:27 +1100241 if (h->nlmsg_type == NLMSG_ERROR) {
242 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
243 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
244 fprintf(stderr,
245 "ERROR truncated\n");
246 } else {
247 errno = -err->error;
248 perror("RTNETLINK answers");
249 }
250 return -1;
251 }
252 err = a->filter(&nladdr, h, a->arg1);
253 if (err < 0)
254 return err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000255
256skip_it:
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700257 h = NLMSG_NEXT(h, msglen);
Simon Hormanb49240e2009-12-03 12:08:27 +1100258 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700259 }
260
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000261 if (found_done) {
262 if (dump_intr)
263 fprintf(stderr,
264 "Dump was interrupted and may be inconsistent.\n");
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700265 return 0;
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000266 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700267
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000268 if (msg.msg_flags & MSG_TRUNC) {
269 fprintf(stderr, "Message truncated\n");
270 continue;
271 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700272 if (msglen) {
273 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000274 exit(1);
275 }
276 }
277}
278
Simon Hormanb49240e2009-12-03 12:08:27 +1100279int rtnl_dump_filter(struct rtnl_handle *rth,
280 rtnl_filter_t filter,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800281 void *arg1)
Simon Hormanb49240e2009-12-03 12:08:27 +1100282{
283 const struct rtnl_dump_filter_arg a[2] = {
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800284 { .filter = filter, .arg1 = arg1, },
285 { .filter = NULL, .arg1 = NULL, },
Simon Hormanb49240e2009-12-03 12:08:27 +1100286 };
287
288 return rtnl_dump_filter_l(rth, a);
289}
290
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000291int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800292 unsigned groups, struct nlmsghdr *answer)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000293{
294 int status;
295 unsigned seq;
296 struct nlmsghdr *h;
297 struct sockaddr_nl nladdr;
shemmingerfb229752005-10-04 23:15:32 +0000298 struct iovec iov = {
299 .iov_base = (void*) n,
300 .iov_len = n->nlmsg_len
301 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000302 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000303 .msg_name = &nladdr,
304 .msg_namelen = sizeof(nladdr),
305 .msg_iov = &iov,
306 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000307 };
shemminger8ed63ab2005-09-21 19:33:17 +0000308 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000309
310 memset(&nladdr, 0, sizeof(nladdr));
311 nladdr.nl_family = AF_NETLINK;
312 nladdr.nl_pid = peer;
313 nladdr.nl_groups = groups;
314
315 n->nlmsg_seq = seq = ++rtnl->seq;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000316
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000317 if (answer == NULL)
318 n->nlmsg_flags |= NLM_F_ACK;
319
320 status = sendmsg(rtnl->fd, &msg, 0);
321
322 if (status < 0) {
323 perror("Cannot talk to rtnetlink");
324 return -1;
325 }
326
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000327 memset(buf,0,sizeof(buf));
328
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000329 iov.iov_base = buf;
330
331 while (1) {
332 iov.iov_len = sizeof(buf);
333 status = recvmsg(rtnl->fd, &msg, 0);
334
335 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800336 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000337 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800338 fprintf(stderr, "netlink receive error %s (%d)\n",
339 strerror(errno), errno);
340 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000341 }
342 if (status == 0) {
343 fprintf(stderr, "EOF on netlink\n");
344 return -1;
345 }
346 if (msg.msg_namelen != sizeof(nladdr)) {
347 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
348 exit(1);
349 }
350 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000351 int len = h->nlmsg_len;
352 int l = len - sizeof(*h);
353
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800354 if (l < 0 || len>status) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000355 if (msg.msg_flags & MSG_TRUNC) {
356 fprintf(stderr, "Truncated message\n");
357 return -1;
358 }
359 fprintf(stderr, "!!!malformed message: len=%d\n", len);
360 exit(1);
361 }
362
org[shemminger]!shemminger10f57ef2004-06-07 22:04:04 +0000363 if (nladdr.nl_pid != peer ||
364 h->nlmsg_pid != rtnl->local.nl_pid ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000365 h->nlmsg_seq != seq) {
shemminger4cca16f2006-03-10 23:31:46 +0000366 /* Don't forget to skip that message. */
367 status -= NLMSG_ALIGN(len);
368 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000369 continue;
370 }
371
372 if (h->nlmsg_type == NLMSG_ERROR) {
373 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
374 if (l < sizeof(struct nlmsgerr)) {
375 fprintf(stderr, "ERROR truncated\n");
376 } else {
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400377 if (!err->error) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000378 if (answer)
379 memcpy(answer, h, h->nlmsg_len);
380 return 0;
381 }
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400382
383 fprintf(stderr, "RTNETLINK answers: %s\n", strerror(-err->error));
384 errno = -err->error;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000385 }
386 return -1;
387 }
388 if (answer) {
389 memcpy(answer, h, h->nlmsg_len);
390 return 0;
391 }
392
393 fprintf(stderr, "Unexpected reply!!!\n");
394
395 status -= NLMSG_ALIGN(len);
396 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
397 }
398 if (msg.msg_flags & MSG_TRUNC) {
399 fprintf(stderr, "Message truncated\n");
400 continue;
401 }
402 if (status) {
403 fprintf(stderr, "!!!Remnant of size %d\n", status);
404 exit(1);
405 }
406 }
407}
408
shemminger8ed63ab2005-09-21 19:33:17 +0000409int rtnl_listen(struct rtnl_handle *rtnl,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000410 rtnl_filter_t handler,
411 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000412{
413 int status;
414 struct nlmsghdr *h;
415 struct sockaddr_nl nladdr;
416 struct iovec iov;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000417 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000418 .msg_name = &nladdr,
419 .msg_namelen = sizeof(nladdr),
420 .msg_iov = &iov,
421 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000422 };
shemminger8ed63ab2005-09-21 19:33:17 +0000423 char buf[8192];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000424
425 memset(&nladdr, 0, sizeof(nladdr));
426 nladdr.nl_family = AF_NETLINK;
427 nladdr.nl_pid = 0;
428 nladdr.nl_groups = 0;
429
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000430 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000431 while (1) {
432 iov.iov_len = sizeof(buf);
433 status = recvmsg(rtnl->fd, &msg, 0);
434
435 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800436 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000437 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800438 fprintf(stderr, "netlink receive error %s (%d)\n",
439 strerror(errno), errno);
Patrick McHardy7f031912009-10-28 20:50:48 +0100440 if (errno == ENOBUFS)
441 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800442 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000443 }
444 if (status == 0) {
445 fprintf(stderr, "EOF on netlink\n");
446 return -1;
447 }
448 if (msg.msg_namelen != sizeof(nladdr)) {
449 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
450 exit(1);
451 }
452 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
453 int err;
454 int len = h->nlmsg_len;
455 int l = len - sizeof(*h);
456
457 if (l<0 || len>status) {
458 if (msg.msg_flags & MSG_TRUNC) {
459 fprintf(stderr, "Truncated message\n");
460 return -1;
461 }
462 fprintf(stderr, "!!!malformed message: len=%d\n", len);
463 exit(1);
464 }
465
466 err = handler(&nladdr, h, jarg);
467 if (err < 0)
468 return err;
469
470 status -= NLMSG_ALIGN(len);
471 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
472 }
473 if (msg.msg_flags & MSG_TRUNC) {
474 fprintf(stderr, "Message truncated\n");
475 continue;
476 }
477 if (status) {
478 fprintf(stderr, "!!!Remnant of size %d\n", status);
479 exit(1);
480 }
481 }
482}
483
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000484int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
485 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000486{
487 int status;
488 struct sockaddr_nl nladdr;
489 char buf[8192];
490 struct nlmsghdr *h = (void*)buf;
491
492 memset(&nladdr, 0, sizeof(nladdr));
493 nladdr.nl_family = AF_NETLINK;
494 nladdr.nl_pid = 0;
495 nladdr.nl_groups = 0;
496
497 while (1) {
Stephen Hemminger2dd9f8e2011-06-20 14:34:46 -0700498 int err, len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000499 int l;
500
501 status = fread(&buf, 1, sizeof(*h), rtnl);
502
503 if (status < 0) {
504 if (errno == EINTR)
505 continue;
506 perror("rtnl_from_file: fread");
507 return -1;
508 }
509 if (status == 0)
510 return 0;
511
512 len = h->nlmsg_len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000513 l = len - sizeof(*h);
514
515 if (l<0 || len>sizeof(buf)) {
516 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
517 len, ftell(rtnl));
518 return -1;
519 }
520
521 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
522
523 if (status < 0) {
524 perror("rtnl_from_file: fread");
525 return -1;
526 }
527 if (status < l) {
528 fprintf(stderr, "rtnl-from_file: truncated message\n");
529 return -1;
530 }
531
532 err = handler(&nladdr, h, jarg);
533 if (err < 0)
534 return err;
535 }
536}
537
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800538int addattr(struct nlmsghdr *n, int maxlen, int type)
539{
540 return addattr_l(n, maxlen, type, NULL, 0);
541}
542
543int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
544{
545 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
546}
547
548int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
549{
550 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
551}
552
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000553int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
554{
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800555 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
556}
557
558int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
559{
560 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
561}
562
563int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
564{
565 return addattr_l(n, maxlen, type, str, strlen(str)+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000566}
567
shemminger8ed63ab2005-09-21 19:33:17 +0000568int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000569 int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000570{
571 int len = RTA_LENGTH(alen);
572 struct rtattr *rta;
573
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000574 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000575 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000576 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000577 }
6!tgraf07f94362005-01-18 13:58:49 +0000578 rta = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000579 rta->rta_type = type;
580 rta->rta_len = len;
581 memcpy(RTA_DATA(rta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000582 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000583 return 0;
584}
585
6!tgraf07f94362005-01-18 13:58:49 +0000586int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
587{
588 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
589 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
590 return -1;
591 }
592
593 memcpy(NLMSG_TAIL(n), data, len);
594 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
595 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
596 return 0;
597}
598
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700599struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
600{
601 struct rtattr *nest = NLMSG_TAIL(n);
602
603 addattr_l(n, maxlen, type, NULL, 0);
604 return nest;
605}
606
607int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
608{
609 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
610 return n->nlmsg_len;
611}
612
613struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
614 const void *data, int len)
615{
616 struct rtattr *start = NLMSG_TAIL(n);
617
618 addattr_l(n, maxlen, type, data, len);
619 addattr_nest(n, maxlen, type);
620 return start;
621}
622
623int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
624{
625 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
626
627 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
628 addattr_nest_end(n, nest);
629 return n->nlmsg_len;
630}
631
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000632int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
633{
634 int len = RTA_LENGTH(4);
635 struct rtattr *subrta;
636
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000637 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
638 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000639 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000640 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000641 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
642 subrta->rta_type = type;
643 subrta->rta_len = len;
644 memcpy(RTA_DATA(subrta), &data, 4);
645 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
646 return 0;
647}
648
shemminger8ed63ab2005-09-21 19:33:17 +0000649int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000650 const void *data, int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000651{
652 struct rtattr *subrta;
653 int len = RTA_LENGTH(alen);
654
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000655 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000656 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000657 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000658 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000659 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
660 subrta->rta_type = type;
661 subrta->rta_len = len;
662 memcpy(RTA_DATA(subrta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000663 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000664 return 0;
665}
666
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000667int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
668{
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700669 return parse_rtattr_flags(tb, max, rta, len, 0);
670}
671
672int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
673 int len, unsigned short flags)
674{
675 unsigned short type;
676
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000677 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000678 while (RTA_OK(rta, len)) {
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700679 type = rta->rta_type & ~flags;
680 if ((type <= max) && (!tb[type]))
681 tb[type] = rta;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000682 rta = RTA_NEXT(rta,len);
683 }
684 if (len)
685 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
686 return 0;
687}
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000688
689int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
690{
691 int i = 0;
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000692
693 memset(tb, 0, sizeof(struct rtattr *) * max);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000694 while (RTA_OK(rta, len)) {
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000695 if (rta->rta_type <= max && i < max)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000696 tb[i++] = rta;
697 rta = RTA_NEXT(rta,len);
698 }
699 if (len)
700 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
701 return i;
702}
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700703
704int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
705 int len)
706{
707 if (RTA_PAYLOAD(rta) < len)
708 return -1;
709 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
710 rta = RTA_DATA(rta) + RTA_ALIGN(len);
711 return parse_rtattr_nested(tb, max, rta);
712 }
Stephen Hemminger037c6352007-12-10 11:34:40 -0800713 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700714 return 0;
715}