blob: e3b7862c32a25ef4325321b0886793f77034e3ba [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
vadimk8a4025f2014-12-04 12:32:58 +020046 rth->proto = protocol;
Andrey Vaginbcb9d402013-06-04 12:01:14 +040047 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000048 if (rth->fd < 0) {
49 perror("Cannot open netlink socket");
50 return -1;
51 }
52
osdl.net!shemminger007d3a32004-08-13 23:54:55 +000053 if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) {
54 perror("SO_SNDBUF");
55 return -1;
56 }
57
58 if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) {
59 perror("SO_RCVBUF");
60 return -1;
61 }
62
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000063 memset(&rth->local, 0, sizeof(rth->local));
64 rth->local.nl_family = AF_NETLINK;
65 rth->local.nl_groups = subscriptions;
66
67 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
68 perror("Cannot bind netlink socket");
69 return -1;
70 }
71 addr_len = sizeof(rth->local);
72 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
73 perror("Cannot getsockname");
74 return -1;
75 }
76 if (addr_len != sizeof(rth->local)) {
77 fprintf(stderr, "Wrong address length %d\n", addr_len);
78 return -1;
79 }
80 if (rth->local.nl_family != AF_NETLINK) {
81 fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family);
82 return -1;
83 }
84 rth->seq = time(NULL);
85 return 0;
86}
87
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000088int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
89{
90 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
91}
92
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000093int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
94{
Vlad Yasevich9eff0e52013-02-28 10:04:05 +000095 return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF);
96}
97
98int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int family, int type,
99 __u32 filt_mask)
100{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101 struct {
102 struct nlmsghdr nlh;
Alexander Duyck63338dc2013-04-25 12:07:10 +0000103 struct ifinfomsg ifm;
Lutz Jaenicke257422f2012-08-30 05:01:34 +0000104 /* attribute has to be NLMSG aligned */
105 struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO)));
Rose, Gregory Vbd886eb2012-02-21 10:43:09 +0000106 __u32 ext_filter_mask;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000107 } req;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000108
shemminger8ed63ab2005-09-21 19:33:17 +0000109 memset(&req, 0, sizeof(req));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000110 req.nlh.nlmsg_len = sizeof(req);
111 req.nlh.nlmsg_type = type;
Masatake YAMATOaa38c3e2012-01-19 14:16:12 -0800112 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000113 req.nlh.nlmsg_pid = 0;
114 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
Alexander Duyck63338dc2013-04-25 12:07:10 +0000115 req.ifm.ifi_family = family;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000116
Rose, Gregory Vbd886eb2012-02-21 10:43:09 +0000117 req.ext_req.rta_type = IFLA_EXT_MASK;
118 req.ext_req.rta_len = RTA_LENGTH(sizeof(__u32));
Vlad Yasevich9eff0e52013-02-28 10:04:05 +0000119 req.ext_filter_mask = filt_mask;
Rose, Gregory Vbd886eb2012-02-21 10:43:09 +0000120
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800121 return send(rth->fd, (void*)&req, sizeof(req), 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000122}
123
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800124int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000125{
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800126 return send(rth->fd, buf, len, 0);
127}
128
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800129int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800130{
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800131 struct nlmsghdr *h;
132 int status;
133 char resp[1024];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000134
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800135 status = send(rth->fd, buf, len, 0);
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800136 if (status < 0)
137 return status;
138
Stephen Hemminger2d8240f2009-07-13 10:15:23 -0700139 /* Check for immediate errors */
140 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800141 if (status < 0) {
142 if (errno == EAGAIN)
143 return 0;
144 return -1;
145 }
146
147 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
148 h = NLMSG_NEXT(h, status)) {
149 if (h->nlmsg_type == NLMSG_ERROR) {
150 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
151 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
152 fprintf(stderr, "ERROR truncated\n");
153 else
154 errno = -err->error;
Sven Anders24f38182009-11-10 09:07:26 -0800155 return -1;
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800156 }
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800157 }
158
159 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000160}
161
162int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
163{
164 struct nlmsghdr nlh;
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800165 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
shemminger8ed63ab2005-09-21 19:33:17 +0000166 struct iovec iov[2] = {
167 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
168 { .iov_base = req, .iov_len = len }
169 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000170 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000171 .msg_name = &nladdr,
172 .msg_namelen = sizeof(nladdr),
173 .msg_iov = iov,
174 .msg_iovlen = 2,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000175 };
176
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000177 nlh.nlmsg_len = NLMSG_LENGTH(len);
178 nlh.nlmsg_type = type;
Masatake YAMATOaa38c3e2012-01-19 14:16:12 -0800179 nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000180 nlh.nlmsg_pid = 0;
181 nlh.nlmsg_seq = rth->dump = ++rth->seq;
182
183 return sendmsg(rth->fd, &msg, 0);
184}
185
Simon Hormanb49240e2009-12-03 12:08:27 +1100186int rtnl_dump_filter_l(struct rtnl_handle *rth,
187 const struct rtnl_dump_filter_arg *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000188{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000189 struct sockaddr_nl nladdr;
shemminger8ed63ab2005-09-21 19:33:17 +0000190 struct iovec iov;
191 struct msghdr msg = {
192 .msg_name = &nladdr,
193 .msg_namelen = sizeof(nladdr),
194 .msg_iov = &iov,
195 .msg_iovlen = 1,
196 };
197 char buf[16384];
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000198 int dump_intr = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000199
shemminger8ed63ab2005-09-21 19:33:17 +0000200 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000201 while (1) {
202 int status;
Simon Hormanb49240e2009-12-03 12:08:27 +1100203 const struct rtnl_dump_filter_arg *a;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700204 int found_done = 0;
205 int msglen = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000206
shemminger8ed63ab2005-09-21 19:33:17 +0000207 iov.iov_len = sizeof(buf);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000208 status = recvmsg(rth->fd, &msg, 0);
209
210 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800211 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000212 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800213 fprintf(stderr, "netlink receive error %s (%d)\n",
214 strerror(errno), errno);
215 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000216 }
shemminger8ed63ab2005-09-21 19:33:17 +0000217
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000218 if (status == 0) {
219 fprintf(stderr, "EOF on netlink\n");
220 return -1;
221 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000222
Simon Hormanb49240e2009-12-03 12:08:27 +1100223 for (a = arg; a->filter; a++) {
224 struct nlmsghdr *h = (struct nlmsghdr*)buf;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700225 msglen = status;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000226
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700227 while (NLMSG_OK(h, msglen)) {
Simon Hormanb49240e2009-12-03 12:08:27 +1100228 int err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000229
Simon Hormanb49240e2009-12-03 12:08:27 +1100230 if (nladdr.nl_pid != 0 ||
231 h->nlmsg_pid != rth->local.nl_pid ||
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800232 h->nlmsg_seq != rth->dump)
Simon Hormanb49240e2009-12-03 12:08:27 +1100233 goto skip_it;
Simon Hormanb49240e2009-12-03 12:08:27 +1100234
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000235 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
236 dump_intr = 1;
237
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700238 if (h->nlmsg_type == NLMSG_DONE) {
239 found_done = 1;
240 break; /* process next filter */
241 }
Simon Hormanb49240e2009-12-03 12:08:27 +1100242 if (h->nlmsg_type == NLMSG_ERROR) {
243 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
244 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
245 fprintf(stderr,
246 "ERROR truncated\n");
247 } else {
248 errno = -err->error;
vadimk8a4025f2014-12-04 12:32:58 +0200249 if (rth->proto == NETLINK_SOCK_DIAG &&
250 errno == ENOENT)
251 return -1;
252
Simon Hormanb49240e2009-12-03 12:08:27 +1100253 perror("RTNETLINK answers");
254 }
255 return -1;
256 }
257 err = a->filter(&nladdr, h, a->arg1);
258 if (err < 0)
259 return err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000260
261skip_it:
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700262 h = NLMSG_NEXT(h, msglen);
Simon Hormanb49240e2009-12-03 12:08:27 +1100263 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700264 }
265
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000266 if (found_done) {
267 if (dump_intr)
268 fprintf(stderr,
269 "Dump was interrupted and may be inconsistent.\n");
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700270 return 0;
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000271 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700272
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000273 if (msg.msg_flags & MSG_TRUNC) {
274 fprintf(stderr, "Message truncated\n");
275 continue;
276 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700277 if (msglen) {
278 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000279 exit(1);
280 }
281 }
282}
283
Simon Hormanb49240e2009-12-03 12:08:27 +1100284int rtnl_dump_filter(struct rtnl_handle *rth,
285 rtnl_filter_t filter,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800286 void *arg1)
Simon Hormanb49240e2009-12-03 12:08:27 +1100287{
288 const struct rtnl_dump_filter_arg a[2] = {
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800289 { .filter = filter, .arg1 = arg1, },
290 { .filter = NULL, .arg1 = NULL, },
Simon Hormanb49240e2009-12-03 12:08:27 +1100291 };
292
293 return rtnl_dump_filter_l(rth, a);
294}
295
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000296int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800297 unsigned groups, struct nlmsghdr *answer)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298{
299 int status;
300 unsigned seq;
301 struct nlmsghdr *h;
302 struct sockaddr_nl nladdr;
shemmingerfb229752005-10-04 23:15:32 +0000303 struct iovec iov = {
304 .iov_base = (void*) n,
305 .iov_len = n->nlmsg_len
306 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000307 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000308 .msg_name = &nladdr,
309 .msg_namelen = sizeof(nladdr),
310 .msg_iov = &iov,
311 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000312 };
shemminger8ed63ab2005-09-21 19:33:17 +0000313 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000314
315 memset(&nladdr, 0, sizeof(nladdr));
316 nladdr.nl_family = AF_NETLINK;
317 nladdr.nl_pid = peer;
318 nladdr.nl_groups = groups;
319
320 n->nlmsg_seq = seq = ++rtnl->seq;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000321
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000322 if (answer == NULL)
323 n->nlmsg_flags |= NLM_F_ACK;
324
325 status = sendmsg(rtnl->fd, &msg, 0);
326
327 if (status < 0) {
328 perror("Cannot talk to rtnetlink");
329 return -1;
330 }
331
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000332 memset(buf,0,sizeof(buf));
333
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000334 iov.iov_base = buf;
335
336 while (1) {
337 iov.iov_len = sizeof(buf);
338 status = recvmsg(rtnl->fd, &msg, 0);
339
340 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800341 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000342 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800343 fprintf(stderr, "netlink receive error %s (%d)\n",
344 strerror(errno), errno);
345 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000346 }
347 if (status == 0) {
348 fprintf(stderr, "EOF on netlink\n");
349 return -1;
350 }
351 if (msg.msg_namelen != sizeof(nladdr)) {
352 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
353 exit(1);
354 }
355 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000356 int len = h->nlmsg_len;
357 int l = len - sizeof(*h);
358
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800359 if (l < 0 || len>status) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000360 if (msg.msg_flags & MSG_TRUNC) {
361 fprintf(stderr, "Truncated message\n");
362 return -1;
363 }
364 fprintf(stderr, "!!!malformed message: len=%d\n", len);
365 exit(1);
366 }
367
org[shemminger]!shemminger10f57ef2004-06-07 22:04:04 +0000368 if (nladdr.nl_pid != peer ||
369 h->nlmsg_pid != rtnl->local.nl_pid ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000370 h->nlmsg_seq != seq) {
shemminger4cca16f2006-03-10 23:31:46 +0000371 /* Don't forget to skip that message. */
372 status -= NLMSG_ALIGN(len);
373 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000374 continue;
375 }
376
377 if (h->nlmsg_type == NLMSG_ERROR) {
378 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
379 if (l < sizeof(struct nlmsgerr)) {
380 fprintf(stderr, "ERROR truncated\n");
381 } else {
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400382 if (!err->error) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000383 if (answer)
384 memcpy(answer, h, h->nlmsg_len);
385 return 0;
386 }
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400387
388 fprintf(stderr, "RTNETLINK answers: %s\n", strerror(-err->error));
389 errno = -err->error;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000390 }
391 return -1;
392 }
393 if (answer) {
394 memcpy(answer, h, h->nlmsg_len);
395 return 0;
396 }
397
398 fprintf(stderr, "Unexpected reply!!!\n");
399
400 status -= NLMSG_ALIGN(len);
401 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
402 }
403 if (msg.msg_flags & MSG_TRUNC) {
404 fprintf(stderr, "Message truncated\n");
405 continue;
406 }
407 if (status) {
408 fprintf(stderr, "!!!Remnant of size %d\n", status);
409 exit(1);
410 }
411 }
412}
413
shemminger8ed63ab2005-09-21 19:33:17 +0000414int rtnl_listen(struct rtnl_handle *rtnl,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000415 rtnl_filter_t handler,
416 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000417{
418 int status;
419 struct nlmsghdr *h;
420 struct sockaddr_nl nladdr;
421 struct iovec iov;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000422 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000423 .msg_name = &nladdr,
424 .msg_namelen = sizeof(nladdr),
425 .msg_iov = &iov,
426 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000427 };
Eric Dumazete5572122014-10-11 09:43:13 -0700428 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000429
430 memset(&nladdr, 0, sizeof(nladdr));
431 nladdr.nl_family = AF_NETLINK;
432 nladdr.nl_pid = 0;
433 nladdr.nl_groups = 0;
434
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000435 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000436 while (1) {
437 iov.iov_len = sizeof(buf);
438 status = recvmsg(rtnl->fd, &msg, 0);
439
440 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800441 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000442 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800443 fprintf(stderr, "netlink receive error %s (%d)\n",
444 strerror(errno), errno);
Patrick McHardy7f031912009-10-28 20:50:48 +0100445 if (errno == ENOBUFS)
446 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800447 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000448 }
449 if (status == 0) {
450 fprintf(stderr, "EOF on netlink\n");
451 return -1;
452 }
453 if (msg.msg_namelen != sizeof(nladdr)) {
454 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
455 exit(1);
456 }
457 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
458 int err;
459 int len = h->nlmsg_len;
460 int l = len - sizeof(*h);
461
462 if (l<0 || len>status) {
463 if (msg.msg_flags & MSG_TRUNC) {
464 fprintf(stderr, "Truncated message\n");
465 return -1;
466 }
467 fprintf(stderr, "!!!malformed message: len=%d\n", len);
468 exit(1);
469 }
470
471 err = handler(&nladdr, h, jarg);
472 if (err < 0)
473 return err;
474
475 status -= NLMSG_ALIGN(len);
476 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
477 }
478 if (msg.msg_flags & MSG_TRUNC) {
479 fprintf(stderr, "Message truncated\n");
480 continue;
481 }
482 if (status) {
483 fprintf(stderr, "!!!Remnant of size %d\n", status);
484 exit(1);
485 }
486 }
487}
488
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000489int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
490 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000491{
492 int status;
493 struct sockaddr_nl nladdr;
Eric Dumazete5572122014-10-11 09:43:13 -0700494 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000495 struct nlmsghdr *h = (void*)buf;
496
497 memset(&nladdr, 0, sizeof(nladdr));
498 nladdr.nl_family = AF_NETLINK;
499 nladdr.nl_pid = 0;
500 nladdr.nl_groups = 0;
501
502 while (1) {
Stephen Hemminger2dd9f8e2011-06-20 14:34:46 -0700503 int err, len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000504 int l;
505
506 status = fread(&buf, 1, sizeof(*h), rtnl);
507
508 if (status < 0) {
509 if (errno == EINTR)
510 continue;
511 perror("rtnl_from_file: fread");
512 return -1;
513 }
514 if (status == 0)
515 return 0;
516
517 len = h->nlmsg_len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000518 l = len - sizeof(*h);
519
520 if (l<0 || len>sizeof(buf)) {
521 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
522 len, ftell(rtnl));
523 return -1;
524 }
525
526 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
527
528 if (status < 0) {
529 perror("rtnl_from_file: fread");
530 return -1;
531 }
532 if (status < l) {
533 fprintf(stderr, "rtnl-from_file: truncated message\n");
534 return -1;
535 }
536
537 err = handler(&nladdr, h, jarg);
538 if (err < 0)
539 return err;
540 }
541}
542
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800543int addattr(struct nlmsghdr *n, int maxlen, int type)
544{
545 return addattr_l(n, maxlen, type, NULL, 0);
546}
547
548int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
549{
550 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
551}
552
553int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
554{
555 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
556}
557
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000558int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
559{
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800560 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
561}
562
563int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
564{
565 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
566}
567
568int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
569{
570 return addattr_l(n, maxlen, type, str, strlen(str)+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000571}
572
shemminger8ed63ab2005-09-21 19:33:17 +0000573int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000574 int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000575{
576 int len = RTA_LENGTH(alen);
577 struct rtattr *rta;
578
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000579 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000580 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000581 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000582 }
6!tgraf07f94362005-01-18 13:58:49 +0000583 rta = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000584 rta->rta_type = type;
585 rta->rta_len = len;
586 memcpy(RTA_DATA(rta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000587 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000588 return 0;
589}
590
6!tgraf07f94362005-01-18 13:58:49 +0000591int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
592{
593 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
594 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
595 return -1;
596 }
597
598 memcpy(NLMSG_TAIL(n), data, len);
599 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
600 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
601 return 0;
602}
603
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700604struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
605{
606 struct rtattr *nest = NLMSG_TAIL(n);
607
608 addattr_l(n, maxlen, type, NULL, 0);
609 return nest;
610}
611
612int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
613{
614 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
615 return n->nlmsg_len;
616}
617
618struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
619 const void *data, int len)
620{
621 struct rtattr *start = NLMSG_TAIL(n);
622
623 addattr_l(n, maxlen, type, data, len);
624 addattr_nest(n, maxlen, type);
625 return start;
626}
627
628int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
629{
630 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
631
632 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
633 addattr_nest_end(n, nest);
634 return n->nlmsg_len;
635}
636
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000637int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
638{
639 int len = RTA_LENGTH(4);
640 struct rtattr *subrta;
641
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000642 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
643 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000644 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000645 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000646 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
647 subrta->rta_type = type;
648 subrta->rta_len = len;
649 memcpy(RTA_DATA(subrta), &data, 4);
650 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
651 return 0;
652}
653
shemminger8ed63ab2005-09-21 19:33:17 +0000654int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000655 const void *data, int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000656{
657 struct rtattr *subrta;
658 int len = RTA_LENGTH(alen);
659
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000660 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000661 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000662 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000663 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000664 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
665 subrta->rta_type = type;
666 subrta->rta_len = len;
667 memcpy(RTA_DATA(subrta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000668 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000669 return 0;
670}
671
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000672int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
673{
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700674 return parse_rtattr_flags(tb, max, rta, len, 0);
675}
676
677int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
678 int len, unsigned short flags)
679{
680 unsigned short type;
681
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000682 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000683 while (RTA_OK(rta, len)) {
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700684 type = rta->rta_type & ~flags;
685 if ((type <= max) && (!tb[type]))
686 tb[type] = rta;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000687 rta = RTA_NEXT(rta,len);
688 }
689 if (len)
690 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
691 return 0;
692}
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000693
694int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
695{
696 int i = 0;
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000697
698 memset(tb, 0, sizeof(struct rtattr *) * max);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000699 while (RTA_OK(rta, len)) {
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000700 if (rta->rta_type <= max && i < max)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000701 tb[i++] = rta;
702 rta = RTA_NEXT(rta,len);
703 }
704 if (len)
705 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
706 return i;
707}
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700708
709int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
710 int len)
711{
712 if (RTA_PAYLOAD(rta) < len)
713 return -1;
714 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
715 rta = RTA_DATA(rta) + RTA_ALIGN(len);
716 return parse_rtattr_nested(tb, max, rta);
717 }
Stephen Hemminger037c6352007-12-10 11:34:40 -0800718 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700719 return 0;
720}