blob: 77e07ef7cf602dd2b18e78e3702da9358088a868 [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
Vadim Kochan486ccd92014-12-26 04:26:27 +0200223 if (rth->dump_fp)
224 fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
225
Simon Hormanb49240e2009-12-03 12:08:27 +1100226 for (a = arg; a->filter; a++) {
227 struct nlmsghdr *h = (struct nlmsghdr*)buf;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700228 msglen = status;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000229
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700230 while (NLMSG_OK(h, msglen)) {
Vadim Kochan486ccd92014-12-26 04:26:27 +0200231 int err = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000232
Simon Hormanb49240e2009-12-03 12:08:27 +1100233 if (nladdr.nl_pid != 0 ||
234 h->nlmsg_pid != rth->local.nl_pid ||
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800235 h->nlmsg_seq != rth->dump)
Simon Hormanb49240e2009-12-03 12:08:27 +1100236 goto skip_it;
Simon Hormanb49240e2009-12-03 12:08:27 +1100237
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000238 if (h->nlmsg_flags & NLM_F_DUMP_INTR)
239 dump_intr = 1;
240
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700241 if (h->nlmsg_type == NLMSG_DONE) {
242 found_done = 1;
243 break; /* process next filter */
244 }
Simon Hormanb49240e2009-12-03 12:08:27 +1100245 if (h->nlmsg_type == NLMSG_ERROR) {
246 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
247 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
248 fprintf(stderr,
249 "ERROR truncated\n");
250 } else {
251 errno = -err->error;
vadimk8a4025f2014-12-04 12:32:58 +0200252 if (rth->proto == NETLINK_SOCK_DIAG &&
Vadim Kochan486ccd92014-12-26 04:26:27 +0200253 (errno == ENOENT ||
254 errno == EOPNOTSUPP))
vadimk8a4025f2014-12-04 12:32:58 +0200255 return -1;
256
Simon Hormanb49240e2009-12-03 12:08:27 +1100257 perror("RTNETLINK answers");
258 }
259 return -1;
260 }
Vadim Kochan486ccd92014-12-26 04:26:27 +0200261
262 if (!rth->dump_fp) {
263 err = a->filter(&nladdr, h, a->arg1);
264 if (err < 0)
265 return err;
266 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000267
268skip_it:
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700269 h = NLMSG_NEXT(h, msglen);
Simon Hormanb49240e2009-12-03 12:08:27 +1100270 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700271 }
272
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000273 if (found_done) {
274 if (dump_intr)
275 fprintf(stderr,
276 "Dump was interrupted and may be inconsistent.\n");
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700277 return 0;
Nicolas Dichtel16f02e12013-03-22 06:34:02 +0000278 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700279
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000280 if (msg.msg_flags & MSG_TRUNC) {
281 fprintf(stderr, "Message truncated\n");
282 continue;
283 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700284 if (msglen) {
285 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000286 exit(1);
287 }
288 }
289}
290
Simon Hormanb49240e2009-12-03 12:08:27 +1100291int rtnl_dump_filter(struct rtnl_handle *rth,
292 rtnl_filter_t filter,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800293 void *arg1)
Simon Hormanb49240e2009-12-03 12:08:27 +1100294{
295 const struct rtnl_dump_filter_arg a[2] = {
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800296 { .filter = filter, .arg1 = arg1, },
297 { .filter = NULL, .arg1 = NULL, },
Simon Hormanb49240e2009-12-03 12:08:27 +1100298 };
299
300 return rtnl_dump_filter_l(rth, a);
301}
302
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000303int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800304 unsigned groups, struct nlmsghdr *answer)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000305{
306 int status;
307 unsigned seq;
308 struct nlmsghdr *h;
309 struct sockaddr_nl nladdr;
shemmingerfb229752005-10-04 23:15:32 +0000310 struct iovec iov = {
311 .iov_base = (void*) n,
312 .iov_len = n->nlmsg_len
313 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000314 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000315 .msg_name = &nladdr,
316 .msg_namelen = sizeof(nladdr),
317 .msg_iov = &iov,
318 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000319 };
shemminger8ed63ab2005-09-21 19:33:17 +0000320 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000321
322 memset(&nladdr, 0, sizeof(nladdr));
323 nladdr.nl_family = AF_NETLINK;
324 nladdr.nl_pid = peer;
325 nladdr.nl_groups = groups;
326
327 n->nlmsg_seq = seq = ++rtnl->seq;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000328
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000329 if (answer == NULL)
330 n->nlmsg_flags |= NLM_F_ACK;
331
332 status = sendmsg(rtnl->fd, &msg, 0);
333
334 if (status < 0) {
335 perror("Cannot talk to rtnetlink");
336 return -1;
337 }
338
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000339 memset(buf,0,sizeof(buf));
340
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000341 iov.iov_base = buf;
342
343 while (1) {
344 iov.iov_len = sizeof(buf);
345 status = recvmsg(rtnl->fd, &msg, 0);
346
347 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800348 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000349 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800350 fprintf(stderr, "netlink receive error %s (%d)\n",
351 strerror(errno), errno);
352 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000353 }
354 if (status == 0) {
355 fprintf(stderr, "EOF on netlink\n");
356 return -1;
357 }
358 if (msg.msg_namelen != sizeof(nladdr)) {
359 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
360 exit(1);
361 }
362 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000363 int len = h->nlmsg_len;
364 int l = len - sizeof(*h);
365
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800366 if (l < 0 || len>status) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000367 if (msg.msg_flags & MSG_TRUNC) {
368 fprintf(stderr, "Truncated message\n");
369 return -1;
370 }
371 fprintf(stderr, "!!!malformed message: len=%d\n", len);
372 exit(1);
373 }
374
org[shemminger]!shemminger10f57ef2004-06-07 22:04:04 +0000375 if (nladdr.nl_pid != peer ||
376 h->nlmsg_pid != rtnl->local.nl_pid ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000377 h->nlmsg_seq != seq) {
shemminger4cca16f2006-03-10 23:31:46 +0000378 /* Don't forget to skip that message. */
379 status -= NLMSG_ALIGN(len);
380 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000381 continue;
382 }
383
384 if (h->nlmsg_type == NLMSG_ERROR) {
385 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
386 if (l < sizeof(struct nlmsgerr)) {
387 fprintf(stderr, "ERROR truncated\n");
388 } else {
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400389 if (!err->error) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000390 if (answer)
391 memcpy(answer, h, h->nlmsg_len);
392 return 0;
393 }
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400394
395 fprintf(stderr, "RTNETLINK answers: %s\n", strerror(-err->error));
396 errno = -err->error;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000397 }
398 return -1;
399 }
400 if (answer) {
401 memcpy(answer, h, h->nlmsg_len);
402 return 0;
403 }
404
405 fprintf(stderr, "Unexpected reply!!!\n");
406
407 status -= NLMSG_ALIGN(len);
408 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
409 }
410 if (msg.msg_flags & MSG_TRUNC) {
411 fprintf(stderr, "Message truncated\n");
412 continue;
413 }
414 if (status) {
415 fprintf(stderr, "!!!Remnant of size %d\n", status);
416 exit(1);
417 }
418 }
419}
420
shemminger8ed63ab2005-09-21 19:33:17 +0000421int rtnl_listen(struct rtnl_handle *rtnl,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000422 rtnl_filter_t handler,
423 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000424{
425 int status;
426 struct nlmsghdr *h;
427 struct sockaddr_nl nladdr;
428 struct iovec iov;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000429 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000430 .msg_name = &nladdr,
431 .msg_namelen = sizeof(nladdr),
432 .msg_iov = &iov,
433 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000434 };
Eric Dumazete5572122014-10-11 09:43:13 -0700435 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000436
437 memset(&nladdr, 0, sizeof(nladdr));
438 nladdr.nl_family = AF_NETLINK;
439 nladdr.nl_pid = 0;
440 nladdr.nl_groups = 0;
441
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000442 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000443 while (1) {
444 iov.iov_len = sizeof(buf);
445 status = recvmsg(rtnl->fd, &msg, 0);
446
447 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800448 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000449 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800450 fprintf(stderr, "netlink receive error %s (%d)\n",
451 strerror(errno), errno);
Patrick McHardy7f031912009-10-28 20:50:48 +0100452 if (errno == ENOBUFS)
453 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800454 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000455 }
456 if (status == 0) {
457 fprintf(stderr, "EOF on netlink\n");
458 return -1;
459 }
460 if (msg.msg_namelen != sizeof(nladdr)) {
461 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
462 exit(1);
463 }
464 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
465 int err;
466 int len = h->nlmsg_len;
467 int l = len - sizeof(*h);
468
469 if (l<0 || len>status) {
470 if (msg.msg_flags & MSG_TRUNC) {
471 fprintf(stderr, "Truncated message\n");
472 return -1;
473 }
474 fprintf(stderr, "!!!malformed message: len=%d\n", len);
475 exit(1);
476 }
477
478 err = handler(&nladdr, h, jarg);
479 if (err < 0)
480 return err;
481
482 status -= NLMSG_ALIGN(len);
483 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
484 }
485 if (msg.msg_flags & MSG_TRUNC) {
486 fprintf(stderr, "Message truncated\n");
487 continue;
488 }
489 if (status) {
490 fprintf(stderr, "!!!Remnant of size %d\n", status);
491 exit(1);
492 }
493 }
494}
495
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000496int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
497 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000498{
499 int status;
500 struct sockaddr_nl nladdr;
Eric Dumazete5572122014-10-11 09:43:13 -0700501 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000502 struct nlmsghdr *h = (void*)buf;
503
504 memset(&nladdr, 0, sizeof(nladdr));
505 nladdr.nl_family = AF_NETLINK;
506 nladdr.nl_pid = 0;
507 nladdr.nl_groups = 0;
508
509 while (1) {
Stephen Hemminger2dd9f8e2011-06-20 14:34:46 -0700510 int err, len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000511 int l;
512
513 status = fread(&buf, 1, sizeof(*h), rtnl);
514
515 if (status < 0) {
516 if (errno == EINTR)
517 continue;
518 perror("rtnl_from_file: fread");
519 return -1;
520 }
521 if (status == 0)
522 return 0;
523
524 len = h->nlmsg_len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000525 l = len - sizeof(*h);
526
527 if (l<0 || len>sizeof(buf)) {
528 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
529 len, ftell(rtnl));
530 return -1;
531 }
532
533 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
534
535 if (status < 0) {
536 perror("rtnl_from_file: fread");
537 return -1;
538 }
539 if (status < l) {
540 fprintf(stderr, "rtnl-from_file: truncated message\n");
541 return -1;
542 }
543
544 err = handler(&nladdr, h, jarg);
545 if (err < 0)
546 return err;
547 }
548}
549
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800550int addattr(struct nlmsghdr *n, int maxlen, int type)
551{
552 return addattr_l(n, maxlen, type, NULL, 0);
553}
554
555int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
556{
557 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
558}
559
560int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
561{
562 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
563}
564
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000565int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
566{
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800567 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
568}
569
570int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
571{
572 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
573}
574
575int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
576{
577 return addattr_l(n, maxlen, type, str, strlen(str)+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000578}
579
shemminger8ed63ab2005-09-21 19:33:17 +0000580int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000581 int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000582{
583 int len = RTA_LENGTH(alen);
584 struct rtattr *rta;
585
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000586 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000587 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000588 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000589 }
6!tgraf07f94362005-01-18 13:58:49 +0000590 rta = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000591 rta->rta_type = type;
592 rta->rta_len = len;
593 memcpy(RTA_DATA(rta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000594 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000595 return 0;
596}
597
6!tgraf07f94362005-01-18 13:58:49 +0000598int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
599{
600 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
601 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
602 return -1;
603 }
604
605 memcpy(NLMSG_TAIL(n), data, len);
606 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
607 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
608 return 0;
609}
610
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700611struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
612{
613 struct rtattr *nest = NLMSG_TAIL(n);
614
615 addattr_l(n, maxlen, type, NULL, 0);
616 return nest;
617}
618
619int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
620{
621 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
622 return n->nlmsg_len;
623}
624
625struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
626 const void *data, int len)
627{
628 struct rtattr *start = NLMSG_TAIL(n);
629
630 addattr_l(n, maxlen, type, data, len);
631 addattr_nest(n, maxlen, type);
632 return start;
633}
634
635int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
636{
637 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
638
639 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
640 addattr_nest_end(n, nest);
641 return n->nlmsg_len;
642}
643
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000644int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
645{
646 int len = RTA_LENGTH(4);
647 struct rtattr *subrta;
648
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000649 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
650 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000651 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000652 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000653 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
654 subrta->rta_type = type;
655 subrta->rta_len = len;
656 memcpy(RTA_DATA(subrta), &data, 4);
657 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
658 return 0;
659}
660
shemminger8ed63ab2005-09-21 19:33:17 +0000661int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000662 const void *data, int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000663{
664 struct rtattr *subrta;
665 int len = RTA_LENGTH(alen);
666
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000667 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000668 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000669 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000670 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000671 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
672 subrta->rta_type = type;
673 subrta->rta_len = len;
674 memcpy(RTA_DATA(subrta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000675 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000676 return 0;
677}
678
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000679int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
680{
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700681 return parse_rtattr_flags(tb, max, rta, len, 0);
682}
683
684int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
685 int len, unsigned short flags)
686{
687 unsigned short type;
688
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000689 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000690 while (RTA_OK(rta, len)) {
Vlad Yasevichb1b7ce02013-03-15 10:01:28 -0700691 type = rta->rta_type & ~flags;
692 if ((type <= max) && (!tb[type]))
693 tb[type] = rta;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000694 rta = RTA_NEXT(rta,len);
695 }
696 if (len)
697 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
698 return 0;
699}
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000700
701int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
702{
703 int i = 0;
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000704
705 memset(tb, 0, sizeof(struct rtattr *) * max);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000706 while (RTA_OK(rta, len)) {
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000707 if (rta->rta_type <= max && i < max)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000708 tb[i++] = rta;
709 rta = RTA_NEXT(rta,len);
710 }
711 if (len)
712 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
713 return i;
714}
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700715
Jiri Pirkodecbb432015-01-06 17:23:45 +0100716struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len)
717{
718 while (RTA_OK(rta, len)) {
719 if (rta->rta_type == type)
720 return rta;
721 rta = RTA_NEXT(rta, len);
722 }
723 if (len)
724 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
725 return NULL;
726}
727
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700728int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
729 int len)
730{
731 if (RTA_PAYLOAD(rta) < len)
732 return -1;
733 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
734 rta = RTA_DATA(rta) + RTA_ALIGN(len);
735 return parse_rtattr_nested(tb, max, rta);
736 }
Stephen Hemminger037c6352007-12-10 11:34:40 -0800737 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700738 return 0;
739}