blob: 09b4277844ca3fc8ea44ef923bedfe30a031f729 [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{
94 struct {
95 struct nlmsghdr nlh;
96 struct rtgenmsg g;
Lutz Jaenicke257422f2012-08-30 05:01:34 +000097 /* attribute has to be NLMSG aligned */
98 struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO)));
Rose, Gregory Vbd886eb2012-02-21 10:43:09 +000099 __u32 ext_filter_mask;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000100 } req;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101
shemminger8ed63ab2005-09-21 19:33:17 +0000102 memset(&req, 0, sizeof(req));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000103 req.nlh.nlmsg_len = sizeof(req);
104 req.nlh.nlmsg_type = type;
Masatake YAMATOaa38c3e2012-01-19 14:16:12 -0800105 req.nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106 req.nlh.nlmsg_pid = 0;
107 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
108 req.g.rtgen_family = family;
109
Rose, Gregory Vbd886eb2012-02-21 10:43:09 +0000110 req.ext_req.rta_type = IFLA_EXT_MASK;
111 req.ext_req.rta_len = RTA_LENGTH(sizeof(__u32));
112 req.ext_filter_mask = RTEXT_FILTER_VF;
113
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800114 return send(rth->fd, (void*)&req, sizeof(req), 0);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000115}
116
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800117int rtnl_send(struct rtnl_handle *rth, const void *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000118{
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800119 return send(rth->fd, buf, len, 0);
120}
121
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800122int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len)
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800123{
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800124 struct nlmsghdr *h;
125 int status;
126 char resp[1024];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000127
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800128 status = send(rth->fd, buf, len, 0);
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800129 if (status < 0)
130 return status;
131
Stephen Hemminger2d8240f2009-07-13 10:15:23 -0700132 /* Check for immediate errors */
133 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK);
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800134 if (status < 0) {
135 if (errno == EAGAIN)
136 return 0;
137 return -1;
138 }
139
140 for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status);
141 h = NLMSG_NEXT(h, status)) {
142 if (h->nlmsg_type == NLMSG_ERROR) {
143 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
144 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
145 fprintf(stderr, "ERROR truncated\n");
146 else
147 errno = -err->error;
Sven Anders24f38182009-11-10 09:07:26 -0800148 return -1;
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800149 }
Stephen Hemminger54bb35c2008-01-26 11:09:42 -0800150 }
151
152 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000153}
154
155int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
156{
157 struct nlmsghdr nlh;
Stephen Hemminger6cf83982011-12-23 10:40:04 -0800158 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
shemminger8ed63ab2005-09-21 19:33:17 +0000159 struct iovec iov[2] = {
160 { .iov_base = &nlh, .iov_len = sizeof(nlh) },
161 { .iov_base = req, .iov_len = len }
162 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000163 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000164 .msg_name = &nladdr,
165 .msg_namelen = sizeof(nladdr),
166 .msg_iov = iov,
167 .msg_iovlen = 2,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000168 };
169
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000170 nlh.nlmsg_len = NLMSG_LENGTH(len);
171 nlh.nlmsg_type = type;
Masatake YAMATOaa38c3e2012-01-19 14:16:12 -0800172 nlh.nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000173 nlh.nlmsg_pid = 0;
174 nlh.nlmsg_seq = rth->dump = ++rth->seq;
175
176 return sendmsg(rth->fd, &msg, 0);
177}
178
Simon Hormanb49240e2009-12-03 12:08:27 +1100179int rtnl_dump_filter_l(struct rtnl_handle *rth,
180 const struct rtnl_dump_filter_arg *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000181{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000182 struct sockaddr_nl nladdr;
shemminger8ed63ab2005-09-21 19:33:17 +0000183 struct iovec iov;
184 struct msghdr msg = {
185 .msg_name = &nladdr,
186 .msg_namelen = sizeof(nladdr),
187 .msg_iov = &iov,
188 .msg_iovlen = 1,
189 };
190 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000191
shemminger8ed63ab2005-09-21 19:33:17 +0000192 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000193 while (1) {
194 int status;
Simon Hormanb49240e2009-12-03 12:08:27 +1100195 const struct rtnl_dump_filter_arg *a;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700196 int found_done = 0;
197 int msglen = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000198
shemminger8ed63ab2005-09-21 19:33:17 +0000199 iov.iov_len = sizeof(buf);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000200 status = recvmsg(rth->fd, &msg, 0);
201
202 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800203 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000204 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800205 fprintf(stderr, "netlink receive error %s (%d)\n",
206 strerror(errno), errno);
207 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000208 }
shemminger8ed63ab2005-09-21 19:33:17 +0000209
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000210 if (status == 0) {
211 fprintf(stderr, "EOF on netlink\n");
212 return -1;
213 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000214
Simon Hormanb49240e2009-12-03 12:08:27 +1100215 for (a = arg; a->filter; a++) {
216 struct nlmsghdr *h = (struct nlmsghdr*)buf;
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700217 msglen = status;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000218
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700219 while (NLMSG_OK(h, msglen)) {
Simon Hormanb49240e2009-12-03 12:08:27 +1100220 int err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000221
Simon Hormanb49240e2009-12-03 12:08:27 +1100222 if (nladdr.nl_pid != 0 ||
223 h->nlmsg_pid != rth->local.nl_pid ||
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800224 h->nlmsg_seq != rth->dump)
Simon Hormanb49240e2009-12-03 12:08:27 +1100225 goto skip_it;
Simon Hormanb49240e2009-12-03 12:08:27 +1100226
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700227 if (h->nlmsg_type == NLMSG_DONE) {
228 found_done = 1;
229 break; /* process next filter */
230 }
Simon Hormanb49240e2009-12-03 12:08:27 +1100231 if (h->nlmsg_type == NLMSG_ERROR) {
232 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
233 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
234 fprintf(stderr,
235 "ERROR truncated\n");
236 } else {
237 errno = -err->error;
238 perror("RTNETLINK answers");
239 }
240 return -1;
241 }
242 err = a->filter(&nladdr, h, a->arg1);
243 if (err < 0)
244 return err;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000245
246skip_it:
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700247 h = NLMSG_NEXT(h, msglen);
Simon Hormanb49240e2009-12-03 12:08:27 +1100248 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700249 }
250
251 if (found_done)
252 return 0;
253
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000254 if (msg.msg_flags & MSG_TRUNC) {
255 fprintf(stderr, "Message truncated\n");
256 continue;
257 }
Ben Greear3bc1c4f2010-08-16 10:00:08 -0700258 if (msglen) {
259 fprintf(stderr, "!!!Remnant of size %d\n", msglen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000260 exit(1);
261 }
262 }
263}
264
Simon Hormanb49240e2009-12-03 12:08:27 +1100265int rtnl_dump_filter(struct rtnl_handle *rth,
266 rtnl_filter_t filter,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800267 void *arg1)
Simon Hormanb49240e2009-12-03 12:08:27 +1100268{
269 const struct rtnl_dump_filter_arg a[2] = {
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800270 { .filter = filter, .arg1 = arg1, },
271 { .filter = NULL, .arg1 = NULL, },
Simon Hormanb49240e2009-12-03 12:08:27 +1100272 };
273
274 return rtnl_dump_filter_l(rth, a);
275}
276
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000277int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800278 unsigned groups, struct nlmsghdr *answer)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000279{
280 int status;
281 unsigned seq;
282 struct nlmsghdr *h;
283 struct sockaddr_nl nladdr;
shemmingerfb229752005-10-04 23:15:32 +0000284 struct iovec iov = {
285 .iov_base = (void*) n,
286 .iov_len = n->nlmsg_len
287 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000288 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000289 .msg_name = &nladdr,
290 .msg_namelen = sizeof(nladdr),
291 .msg_iov = &iov,
292 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000293 };
shemminger8ed63ab2005-09-21 19:33:17 +0000294 char buf[16384];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000295
296 memset(&nladdr, 0, sizeof(nladdr));
297 nladdr.nl_family = AF_NETLINK;
298 nladdr.nl_pid = peer;
299 nladdr.nl_groups = groups;
300
301 n->nlmsg_seq = seq = ++rtnl->seq;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000302
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000303 if (answer == NULL)
304 n->nlmsg_flags |= NLM_F_ACK;
305
306 status = sendmsg(rtnl->fd, &msg, 0);
307
308 if (status < 0) {
309 perror("Cannot talk to rtnetlink");
310 return -1;
311 }
312
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000313 memset(buf,0,sizeof(buf));
314
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000315 iov.iov_base = buf;
316
317 while (1) {
318 iov.iov_len = sizeof(buf);
319 status = recvmsg(rtnl->fd, &msg, 0);
320
321 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800322 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000323 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800324 fprintf(stderr, "netlink receive error %s (%d)\n",
325 strerror(errno), errno);
326 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000327 }
328 if (status == 0) {
329 fprintf(stderr, "EOF on netlink\n");
330 return -1;
331 }
332 if (msg.msg_namelen != sizeof(nladdr)) {
333 fprintf(stderr, "sender address length == %d\n", msg.msg_namelen);
334 exit(1);
335 }
336 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000337 int len = h->nlmsg_len;
338 int l = len - sizeof(*h);
339
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800340 if (l < 0 || len>status) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000341 if (msg.msg_flags & MSG_TRUNC) {
342 fprintf(stderr, "Truncated message\n");
343 return -1;
344 }
345 fprintf(stderr, "!!!malformed message: len=%d\n", len);
346 exit(1);
347 }
348
org[shemminger]!shemminger10f57ef2004-06-07 22:04:04 +0000349 if (nladdr.nl_pid != peer ||
350 h->nlmsg_pid != rtnl->local.nl_pid ||
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000351 h->nlmsg_seq != seq) {
shemminger4cca16f2006-03-10 23:31:46 +0000352 /* Don't forget to skip that message. */
353 status -= NLMSG_ALIGN(len);
354 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000355 continue;
356 }
357
358 if (h->nlmsg_type == NLMSG_ERROR) {
359 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
360 if (l < sizeof(struct nlmsgerr)) {
361 fprintf(stderr, "ERROR truncated\n");
362 } else {
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400363 if (!err->error) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000364 if (answer)
365 memcpy(answer, h, h->nlmsg_len);
366 return 0;
367 }
Pavel Emelyanovb8cf1e92012-08-20 12:08:40 +0400368
369 fprintf(stderr, "RTNETLINK answers: %s\n", strerror(-err->error));
370 errno = -err->error;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000371 }
372 return -1;
373 }
374 if (answer) {
375 memcpy(answer, h, h->nlmsg_len);
376 return 0;
377 }
378
379 fprintf(stderr, "Unexpected reply!!!\n");
380
381 status -= NLMSG_ALIGN(len);
382 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
383 }
384 if (msg.msg_flags & MSG_TRUNC) {
385 fprintf(stderr, "Message truncated\n");
386 continue;
387 }
388 if (status) {
389 fprintf(stderr, "!!!Remnant of size %d\n", status);
390 exit(1);
391 }
392 }
393}
394
shemminger8ed63ab2005-09-21 19:33:17 +0000395int rtnl_listen(struct rtnl_handle *rtnl,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000396 rtnl_filter_t handler,
397 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000398{
399 int status;
400 struct nlmsghdr *h;
401 struct sockaddr_nl nladdr;
402 struct iovec iov;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000403 struct msghdr msg = {
shemminger8ed63ab2005-09-21 19:33:17 +0000404 .msg_name = &nladdr,
405 .msg_namelen = sizeof(nladdr),
406 .msg_iov = &iov,
407 .msg_iovlen = 1,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000408 };
shemminger8ed63ab2005-09-21 19:33:17 +0000409 char buf[8192];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000410
411 memset(&nladdr, 0, sizeof(nladdr));
412 nladdr.nl_family = AF_NETLINK;
413 nladdr.nl_pid = 0;
414 nladdr.nl_groups = 0;
415
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000416 iov.iov_base = buf;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000417 while (1) {
418 iov.iov_len = sizeof(buf);
419 status = recvmsg(rtnl->fd, &msg, 0);
420
421 if (status < 0) {
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800422 if (errno == EINTR || errno == EAGAIN)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000423 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800424 fprintf(stderr, "netlink receive error %s (%d)\n",
425 strerror(errno), errno);
Patrick McHardy7f031912009-10-28 20:50:48 +0100426 if (errno == ENOBUFS)
427 continue;
Stephen Hemmingeraa8032e2008-01-25 15:39:09 -0800428 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000429 }
430 if (status == 0) {
431 fprintf(stderr, "EOF on netlink\n");
432 return -1;
433 }
434 if (msg.msg_namelen != sizeof(nladdr)) {
435 fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen);
436 exit(1);
437 }
438 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
439 int err;
440 int len = h->nlmsg_len;
441 int l = len - sizeof(*h);
442
443 if (l<0 || len>status) {
444 if (msg.msg_flags & MSG_TRUNC) {
445 fprintf(stderr, "Truncated message\n");
446 return -1;
447 }
448 fprintf(stderr, "!!!malformed message: len=%d\n", len);
449 exit(1);
450 }
451
452 err = handler(&nladdr, h, jarg);
453 if (err < 0)
454 return err;
455
456 status -= NLMSG_ALIGN(len);
457 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
458 }
459 if (msg.msg_flags & MSG_TRUNC) {
460 fprintf(stderr, "Message truncated\n");
461 continue;
462 }
463 if (status) {
464 fprintf(stderr, "!!!Remnant of size %d\n", status);
465 exit(1);
466 }
467 }
468}
469
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000470int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
471 void *jarg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000472{
473 int status;
474 struct sockaddr_nl nladdr;
475 char buf[8192];
476 struct nlmsghdr *h = (void*)buf;
477
478 memset(&nladdr, 0, sizeof(nladdr));
479 nladdr.nl_family = AF_NETLINK;
480 nladdr.nl_pid = 0;
481 nladdr.nl_groups = 0;
482
483 while (1) {
Stephen Hemminger2dd9f8e2011-06-20 14:34:46 -0700484 int err, len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000485 int l;
486
487 status = fread(&buf, 1, sizeof(*h), rtnl);
488
489 if (status < 0) {
490 if (errno == EINTR)
491 continue;
492 perror("rtnl_from_file: fread");
493 return -1;
494 }
495 if (status == 0)
496 return 0;
497
498 len = h->nlmsg_len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000499 l = len - sizeof(*h);
500
501 if (l<0 || len>sizeof(buf)) {
502 fprintf(stderr, "!!!malformed message: len=%d @%lu\n",
503 len, ftell(rtnl));
504 return -1;
505 }
506
507 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
508
509 if (status < 0) {
510 perror("rtnl_from_file: fread");
511 return -1;
512 }
513 if (status < l) {
514 fprintf(stderr, "rtnl-from_file: truncated message\n");
515 return -1;
516 }
517
518 err = handler(&nladdr, h, jarg);
519 if (err < 0)
520 return err;
521 }
522}
523
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800524int addattr(struct nlmsghdr *n, int maxlen, int type)
525{
526 return addattr_l(n, maxlen, type, NULL, 0);
527}
528
529int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
530{
531 return addattr_l(n, maxlen, type, &data, sizeof(__u8));
532}
533
534int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
535{
536 return addattr_l(n, maxlen, type, &data, sizeof(__u16));
537}
538
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000539int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
540{
Stephen Hemminger2aa3dd22011-12-23 10:43:54 -0800541 return addattr_l(n, maxlen, type, &data, sizeof(__u32));
542}
543
544int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
545{
546 return addattr_l(n, maxlen, type, &data, sizeof(__u64));
547}
548
549int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
550{
551 return addattr_l(n, maxlen, type, str, strlen(str)+1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000552}
553
shemminger8ed63ab2005-09-21 19:33:17 +0000554int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000555 int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000556{
557 int len = RTA_LENGTH(alen);
558 struct rtattr *rta;
559
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000560 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000561 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000562 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000563 }
6!tgraf07f94362005-01-18 13:58:49 +0000564 rta = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000565 rta->rta_type = type;
566 rta->rta_len = len;
567 memcpy(RTA_DATA(rta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000568 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000569 return 0;
570}
571
6!tgraf07f94362005-01-18 13:58:49 +0000572int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
573{
574 if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
575 fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen);
576 return -1;
577 }
578
579 memcpy(NLMSG_TAIL(n), data, len);
580 memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
581 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
582 return 0;
583}
584
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700585struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
586{
587 struct rtattr *nest = NLMSG_TAIL(n);
588
589 addattr_l(n, maxlen, type, NULL, 0);
590 return nest;
591}
592
593int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
594{
595 nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
596 return n->nlmsg_len;
597}
598
599struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type,
600 const void *data, int len)
601{
602 struct rtattr *start = NLMSG_TAIL(n);
603
604 addattr_l(n, maxlen, type, data, len);
605 addattr_nest(n, maxlen, type);
606 return start;
607}
608
609int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start)
610{
611 struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len);
612
613 start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start;
614 addattr_nest_end(n, nest);
615 return n->nlmsg_len;
616}
617
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000618int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data)
619{
620 int len = RTA_LENGTH(4);
621 struct rtattr *subrta;
622
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000623 if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
624 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000625 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000626 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000627 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
628 subrta->rta_type = type;
629 subrta->rta_len = len;
630 memcpy(RTA_DATA(subrta), &data, 4);
631 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
632 return 0;
633}
634
shemminger8ed63ab2005-09-21 19:33:17 +0000635int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000636 const void *data, int alen)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000637{
638 struct rtattr *subrta;
639 int len = RTA_LENGTH(alen);
640
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000641 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000642 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000643 return -1;
osdl.net!shemminger007d3a32004-08-13 23:54:55 +0000644 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000645 subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
646 subrta->rta_type = type;
647 subrta->rta_len = len;
648 memcpy(RTA_DATA(subrta), data, alen);
net[shemminger]!shemminger3dabdbb2005-03-30 18:18:12 +0000649 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000650 return 0;
651}
652
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000653int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
654{
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000655 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000656 while (RTA_OK(rta, len)) {
Williams, Mitch A6e46ec82010-02-10 01:46:47 +0000657 if ((rta->rta_type <= max) && (!tb[rta->rta_type]))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000658 tb[rta->rta_type] = rta;
659 rta = RTA_NEXT(rta,len);
660 }
661 if (len)
662 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
663 return 0;
664}
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000665
666int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
667{
668 int i = 0;
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000669
670 memset(tb, 0, sizeof(struct rtattr *) * max);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000671 while (RTA_OK(rta, len)) {
osdl.net!shemminger175e2442005-02-07 18:17:38 +0000672 if (rta->rta_type <= max && i < max)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000673 tb[i++] = rta;
674 rta = RTA_NEXT(rta,len);
675 }
676 if (len)
677 fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
678 return i;
679}
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700680
681int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta,
682 int len)
683{
684 if (RTA_PAYLOAD(rta) < len)
685 return -1;
686 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
687 rta = RTA_DATA(rta) + RTA_ALIGN(len);
688 return parse_rtattr_nested(tb, max, rta);
689 }
Stephen Hemminger037c6352007-12-10 11:34:40 -0800690 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
Patrick McHardy2f90c9c2007-08-14 11:21:19 -0700691 return 0;
692}