]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
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 | ||
449b824a ND |
28 | #ifndef SOL_NETLINK |
29 | #define SOL_NETLINK 270 | |
30 | #endif | |
31 | ||
c079e121 SH |
32 | #ifndef MIN |
33 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
34 | #endif | |
35 | ||
7f03191f PM |
36 | int rcvbuf = 1024 * 1024; |
37 | ||
aba5acdf SH |
38 | void rtnl_close(struct rtnl_handle *rth) |
39 | { | |
3bfa73ff SH |
40 | if (rth->fd >= 0) { |
41 | close(rth->fd); | |
42 | rth->fd = -1; | |
43 | } | |
aba5acdf SH |
44 | } |
45 | ||
8ed63ab1 SH |
46 | int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions, |
47 | int protocol) | |
aba5acdf | 48 | { |
f332d169 | 49 | socklen_t addr_len; |
007d3a3e | 50 | int sndbuf = 32768; |
aba5acdf | 51 | |
b16621ca | 52 | memset(rth, 0, sizeof(*rth)); |
aba5acdf | 53 | |
8a4025f6 | 54 | rth->proto = protocol; |
bcb9d403 | 55 | rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol); |
aba5acdf SH |
56 | if (rth->fd < 0) { |
57 | perror("Cannot open netlink socket"); | |
58 | return -1; | |
59 | } | |
60 | ||
007d3a3e SH |
61 | if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) { |
62 | perror("SO_SNDBUF"); | |
63 | return -1; | |
64 | } | |
65 | ||
66 | if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) { | |
67 | perror("SO_RCVBUF"); | |
68 | return -1; | |
69 | } | |
70 | ||
aba5acdf SH |
71 | memset(&rth->local, 0, sizeof(rth->local)); |
72 | rth->local.nl_family = AF_NETLINK; | |
73 | rth->local.nl_groups = subscriptions; | |
74 | ||
75 | if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) { | |
76 | perror("Cannot bind netlink socket"); | |
77 | return -1; | |
78 | } | |
79 | addr_len = sizeof(rth->local); | |
80 | if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) { | |
81 | perror("Cannot getsockname"); | |
82 | return -1; | |
83 | } | |
84 | if (addr_len != sizeof(rth->local)) { | |
85 | fprintf(stderr, "Wrong address length %d\n", addr_len); | |
86 | return -1; | |
87 | } | |
88 | if (rth->local.nl_family != AF_NETLINK) { | |
89 | fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family); | |
90 | return -1; | |
91 | } | |
92 | rth->seq = time(NULL); | |
93 | return 0; | |
94 | } | |
95 | ||
c7699875 | 96 | int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions) |
97 | { | |
98 | return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE); | |
99 | } | |
100 | ||
aba5acdf | 101 | int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type) |
9eff0e5c VY |
102 | { |
103 | return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF); | |
104 | } | |
105 | ||
106 | int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int family, int type, | |
107 | __u32 filt_mask) | |
aba5acdf SH |
108 | { |
109 | struct { | |
110 | struct nlmsghdr nlh; | |
63338dca | 111 | struct ifinfomsg ifm; |
257422f7 LJ |
112 | /* attribute has to be NLMSG aligned */ |
113 | struct rtattr ext_req __attribute__ ((aligned(NLMSG_ALIGNTO))); | |
bd886ebb | 114 | __u32 ext_filter_mask; |
d17b136f PS |
115 | } req = { |
116 | .nlh.nlmsg_len = sizeof(req), | |
117 | .nlh.nlmsg_type = type, | |
118 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
119 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
120 | .ifm.ifi_family = family, | |
121 | .ext_req.rta_type = IFLA_EXT_MASK, | |
122 | .ext_req.rta_len = RTA_LENGTH(sizeof(__u32)), | |
123 | .ext_filter_mask = filt_mask, | |
124 | }; | |
bd886ebb | 125 | |
f31a37f7 | 126 | return send(rth->fd, (void*)&req, sizeof(req), 0); |
aba5acdf SH |
127 | } |
128 | ||
b0a4ce62 DA |
129 | int rtnl_wilddump_req_filter_fn(struct rtnl_handle *rth, int family, int type, |
130 | req_filter_fn_t filter_fn) | |
131 | { | |
132 | struct { | |
133 | struct nlmsghdr nlh; | |
134 | struct ifinfomsg ifm; | |
135 | char buf[1024]; | |
d17b136f PS |
136 | } req = { |
137 | .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
138 | .nlh.nlmsg_type = type, | |
139 | .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
140 | .nlh.nlmsg_seq = rth->dump = ++rth->seq, | |
141 | .ifm.ifi_family = family, | |
142 | }; | |
b0a4ce62 DA |
143 | int err; |
144 | ||
145 | if (!filter_fn) | |
146 | return -EINVAL; | |
147 | ||
b0a4ce62 DA |
148 | err = filter_fn(&req.nlh, sizeof(req)); |
149 | if (err) | |
150 | return err; | |
151 | ||
152 | return send(rth->fd, (void*)&req, sizeof(req), 0); | |
153 | } | |
154 | ||
6cf8398f | 155 | int rtnl_send(struct rtnl_handle *rth, const void *buf, int len) |
aba5acdf | 156 | { |
f31a37f7 SH |
157 | return send(rth->fd, buf, len, 0); |
158 | } | |
159 | ||
6cf8398f | 160 | int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len) |
f31a37f7 | 161 | { |
54bb35c6 SH |
162 | struct nlmsghdr *h; |
163 | int status; | |
164 | char resp[1024]; | |
aba5acdf | 165 | |
f31a37f7 | 166 | status = send(rth->fd, buf, len, 0); |
54bb35c6 SH |
167 | if (status < 0) |
168 | return status; | |
169 | ||
2d8240f8 SH |
170 | /* Check for immediate errors */ |
171 | status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK); | |
54bb35c6 SH |
172 | if (status < 0) { |
173 | if (errno == EAGAIN) | |
174 | return 0; | |
175 | return -1; | |
176 | } | |
177 | ||
178 | for (h = (struct nlmsghdr *)resp; NLMSG_OK(h, status); | |
179 | h = NLMSG_NEXT(h, status)) { | |
180 | if (h->nlmsg_type == NLMSG_ERROR) { | |
181 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | |
182 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) | |
183 | fprintf(stderr, "ERROR truncated\n"); | |
e9e9365b | 184 | else |
54bb35c6 | 185 | errno = -err->error; |
24f38182 | 186 | return -1; |
54bb35c6 | 187 | } |
54bb35c6 SH |
188 | } |
189 | ||
190 | return 0; | |
aba5acdf SH |
191 | } |
192 | ||
193 | int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len) | |
194 | { | |
d17b136f PS |
195 | struct nlmsghdr nlh = { |
196 | .nlmsg_len = NLMSG_LENGTH(len), | |
197 | .nlmsg_type = type, | |
198 | .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
199 | .nlmsg_seq = rth->dump = ++rth->seq, | |
200 | }; | |
6cf8398f | 201 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
8ed63ab1 SH |
202 | struct iovec iov[2] = { |
203 | { .iov_base = &nlh, .iov_len = sizeof(nlh) }, | |
204 | { .iov_base = req, .iov_len = len } | |
205 | }; | |
aba5acdf | 206 | struct msghdr msg = { |
8ed63ab1 | 207 | .msg_name = &nladdr, |
d17b136f | 208 | .msg_namelen = sizeof(nladdr), |
8ed63ab1 SH |
209 | .msg_iov = iov, |
210 | .msg_iovlen = 2, | |
aba5acdf SH |
211 | }; |
212 | ||
aba5acdf SH |
213 | return sendmsg(rth->fd, &msg, 0); |
214 | } | |
215 | ||
0d238ca2 DA |
216 | int rtnl_dump_request_n(struct rtnl_handle *rth, struct nlmsghdr *n) |
217 | { | |
218 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; | |
219 | struct iovec iov = { | |
220 | .iov_base = (void*) n, | |
221 | .iov_len = n->nlmsg_len | |
222 | }; | |
223 | struct msghdr msg = { | |
224 | .msg_name = &nladdr, | |
225 | .msg_namelen = sizeof(nladdr), | |
226 | .msg_iov = &iov, | |
227 | .msg_iovlen = 1, | |
228 | }; | |
229 | ||
230 | n->nlmsg_flags = NLM_F_DUMP|NLM_F_REQUEST; | |
231 | n->nlmsg_pid = 0; | |
232 | n->nlmsg_seq = rth->dump = ++rth->seq; | |
233 | ||
234 | return sendmsg(rth->fd, &msg, 0); | |
235 | } | |
236 | ||
b49240ec SH |
237 | int rtnl_dump_filter_l(struct rtnl_handle *rth, |
238 | const struct rtnl_dump_filter_arg *arg) | |
aba5acdf | 239 | { |
aba5acdf | 240 | struct sockaddr_nl nladdr; |
8ed63ab1 SH |
241 | struct iovec iov; |
242 | struct msghdr msg = { | |
243 | .msg_name = &nladdr, | |
244 | .msg_namelen = sizeof(nladdr), | |
245 | .msg_iov = &iov, | |
246 | .msg_iovlen = 1, | |
247 | }; | |
72b365e8 | 248 | char buf[32768]; |
16f02e14 | 249 | int dump_intr = 0; |
aba5acdf | 250 | |
8ed63ab1 | 251 | iov.iov_base = buf; |
aba5acdf SH |
252 | while (1) { |
253 | int status; | |
b49240ec | 254 | const struct rtnl_dump_filter_arg *a; |
3bc1c4f2 BG |
255 | int found_done = 0; |
256 | int msglen = 0; | |
aba5acdf | 257 | |
8ed63ab1 | 258 | iov.iov_len = sizeof(buf); |
aba5acdf SH |
259 | status = recvmsg(rth->fd, &msg, 0); |
260 | ||
261 | if (status < 0) { | |
aa8032e6 | 262 | if (errno == EINTR || errno == EAGAIN) |
aba5acdf | 263 | continue; |
aa8032e6 SH |
264 | fprintf(stderr, "netlink receive error %s (%d)\n", |
265 | strerror(errno), errno); | |
266 | return -1; | |
aba5acdf | 267 | } |
8ed63ab1 | 268 | |
aba5acdf SH |
269 | if (status == 0) { |
270 | fprintf(stderr, "EOF on netlink\n"); | |
271 | return -1; | |
272 | } | |
aba5acdf | 273 | |
486ccd99 VK |
274 | if (rth->dump_fp) |
275 | fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp); | |
276 | ||
b49240ec SH |
277 | for (a = arg; a->filter; a++) { |
278 | struct nlmsghdr *h = (struct nlmsghdr*)buf; | |
3bc1c4f2 | 279 | msglen = status; |
b49240ec | 280 | |
3bc1c4f2 | 281 | while (NLMSG_OK(h, msglen)) { |
486ccd99 | 282 | int err = 0; |
b49240ec | 283 | |
8e72880f PS |
284 | h->nlmsg_flags &= ~a->nc_flags; |
285 | ||
b49240ec SH |
286 | if (nladdr.nl_pid != 0 || |
287 | h->nlmsg_pid != rth->local.nl_pid || | |
cd70f3f5 | 288 | h->nlmsg_seq != rth->dump) |
b49240ec | 289 | goto skip_it; |
aba5acdf | 290 | |
16f02e14 ND |
291 | if (h->nlmsg_flags & NLM_F_DUMP_INTR) |
292 | dump_intr = 1; | |
293 | ||
3bc1c4f2 BG |
294 | if (h->nlmsg_type == NLMSG_DONE) { |
295 | found_done = 1; | |
296 | break; /* process next filter */ | |
297 | } | |
b49240ec SH |
298 | if (h->nlmsg_type == NLMSG_ERROR) { |
299 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | |
300 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | |
301 | fprintf(stderr, | |
302 | "ERROR truncated\n"); | |
303 | } else { | |
304 | errno = -err->error; | |
8a4025f6 | 305 | if (rth->proto == NETLINK_SOCK_DIAG && |
486ccd99 VK |
306 | (errno == ENOENT || |
307 | errno == EOPNOTSUPP)) | |
8a4025f6 | 308 | return -1; |
309 | ||
b49240ec SH |
310 | perror("RTNETLINK answers"); |
311 | } | |
312 | return -1; | |
aba5acdf | 313 | } |
486ccd99 VK |
314 | |
315 | if (!rth->dump_fp) { | |
316 | err = a->filter(&nladdr, h, a->arg1); | |
317 | if (err < 0) | |
318 | return err; | |
319 | } | |
aba5acdf SH |
320 | |
321 | skip_it: | |
3bc1c4f2 | 322 | h = NLMSG_NEXT(h, msglen); |
b49240ec | 323 | } |
3bc1c4f2 BG |
324 | } |
325 | ||
16f02e14 ND |
326 | if (found_done) { |
327 | if (dump_intr) | |
328 | fprintf(stderr, | |
329 | "Dump was interrupted and may be inconsistent.\n"); | |
3bc1c4f2 | 330 | return 0; |
16f02e14 | 331 | } |
3bc1c4f2 | 332 | |
aba5acdf SH |
333 | if (msg.msg_flags & MSG_TRUNC) { |
334 | fprintf(stderr, "Message truncated\n"); | |
335 | continue; | |
336 | } | |
3bc1c4f2 BG |
337 | if (msglen) { |
338 | fprintf(stderr, "!!!Remnant of size %d\n", msglen); | |
aba5acdf SH |
339 | exit(1); |
340 | } | |
341 | } | |
342 | } | |
343 | ||
8e72880f | 344 | int rtnl_dump_filter_nc(struct rtnl_handle *rth, |
b49240ec | 345 | rtnl_filter_t filter, |
8e72880f | 346 | void *arg1, __u16 nc_flags) |
b49240ec SH |
347 | { |
348 | const struct rtnl_dump_filter_arg a[2] = { | |
8e72880f PS |
349 | { .filter = filter, .arg1 = arg1, .nc_flags = nc_flags, }, |
350 | { .filter = NULL, .arg1 = NULL, .nc_flags = 0, }, | |
b49240ec SH |
351 | }; |
352 | ||
353 | return rtnl_dump_filter_l(rth, a); | |
354 | } | |
355 | ||
c079e121 | 356 | int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, |
ed108cfc | 357 | struct nlmsghdr *answer, size_t maxlen) |
aba5acdf SH |
358 | { |
359 | int status; | |
360 | unsigned seq; | |
361 | struct nlmsghdr *h; | |
d17b136f | 362 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
fb229759 SH |
363 | struct iovec iov = { |
364 | .iov_base = (void*) n, | |
365 | .iov_len = n->nlmsg_len | |
366 | }; | |
aba5acdf | 367 | struct msghdr msg = { |
8ed63ab1 SH |
368 | .msg_name = &nladdr, |
369 | .msg_namelen = sizeof(nladdr), | |
370 | .msg_iov = &iov, | |
371 | .msg_iovlen = 1, | |
aba5acdf | 372 | }; |
d17b136f | 373 | char buf[32768] = {}; |
aba5acdf SH |
374 | |
375 | n->nlmsg_seq = seq = ++rtnl->seq; | |
007d3a3e | 376 | |
aba5acdf SH |
377 | if (answer == NULL) |
378 | n->nlmsg_flags |= NLM_F_ACK; | |
379 | ||
380 | status = sendmsg(rtnl->fd, &msg, 0); | |
aba5acdf SH |
381 | if (status < 0) { |
382 | perror("Cannot talk to rtnetlink"); | |
383 | return -1; | |
384 | } | |
385 | ||
386 | iov.iov_base = buf; | |
aba5acdf SH |
387 | while (1) { |
388 | iov.iov_len = sizeof(buf); | |
389 | status = recvmsg(rtnl->fd, &msg, 0); | |
390 | ||
391 | if (status < 0) { | |
aa8032e6 | 392 | if (errno == EINTR || errno == EAGAIN) |
aba5acdf | 393 | continue; |
aa8032e6 SH |
394 | fprintf(stderr, "netlink receive error %s (%d)\n", |
395 | strerror(errno), errno); | |
396 | return -1; | |
aba5acdf SH |
397 | } |
398 | if (status == 0) { | |
399 | fprintf(stderr, "EOF on netlink\n"); | |
400 | return -1; | |
401 | } | |
402 | if (msg.msg_namelen != sizeof(nladdr)) { | |
403 | fprintf(stderr, "sender address length == %d\n", msg.msg_namelen); | |
404 | exit(1); | |
405 | } | |
406 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { | |
aba5acdf SH |
407 | int len = h->nlmsg_len; |
408 | int l = len - sizeof(*h); | |
409 | ||
cd70f3f5 | 410 | if (l < 0 || len>status) { |
aba5acdf SH |
411 | if (msg.msg_flags & MSG_TRUNC) { |
412 | fprintf(stderr, "Truncated message\n"); | |
413 | return -1; | |
414 | } | |
415 | fprintf(stderr, "!!!malformed message: len=%d\n", len); | |
416 | exit(1); | |
417 | } | |
418 | ||
c079e121 | 419 | if (nladdr.nl_pid != 0 || |
10f57ef1 | 420 | h->nlmsg_pid != rtnl->local.nl_pid || |
aba5acdf | 421 | h->nlmsg_seq != seq) { |
4cca16f2 SH |
422 | /* Don't forget to skip that message. */ |
423 | status -= NLMSG_ALIGN(len); | |
424 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); | |
aba5acdf SH |
425 | continue; |
426 | } | |
427 | ||
428 | if (h->nlmsg_type == NLMSG_ERROR) { | |
429 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | |
430 | if (l < sizeof(struct nlmsgerr)) { | |
431 | fprintf(stderr, "ERROR truncated\n"); | |
c079e121 SH |
432 | } else if (!err->error) { |
433 | if (answer) | |
434 | memcpy(answer, h, | |
ed108cfc | 435 | MIN(maxlen, h->nlmsg_len)); |
c079e121 | 436 | return 0; |
aba5acdf | 437 | } |
c079e121 | 438 | |
57fdf2d4 LC |
439 | if (rtnl->proto != NETLINK_SOCK_DIAG) |
440 | fprintf(stderr, | |
441 | "RTNETLINK answers: %s\n", | |
442 | strerror(-err->error)); | |
c079e121 | 443 | errno = -err->error; |
aba5acdf SH |
444 | return -1; |
445 | } | |
c079e121 | 446 | |
aba5acdf | 447 | if (answer) { |
c079e121 | 448 | memcpy(answer, h, |
ed108cfc | 449 | MIN(maxlen, h->nlmsg_len)); |
aba5acdf SH |
450 | return 0; |
451 | } | |
452 | ||
453 | fprintf(stderr, "Unexpected reply!!!\n"); | |
454 | ||
455 | status -= NLMSG_ALIGN(len); | |
456 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); | |
457 | } | |
c079e121 | 458 | |
aba5acdf SH |
459 | if (msg.msg_flags & MSG_TRUNC) { |
460 | fprintf(stderr, "Message truncated\n"); | |
461 | continue; | |
462 | } | |
c079e121 | 463 | |
aba5acdf SH |
464 | if (status) { |
465 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
466 | exit(1); | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
449b824a ND |
471 | int rtnl_listen_all_nsid(struct rtnl_handle *rth) |
472 | { | |
473 | unsigned int on = 1; | |
474 | ||
475 | if (setsockopt(rth->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &on, | |
476 | sizeof(on)) < 0) { | |
477 | perror("NETLINK_LISTEN_ALL_NSID"); | |
478 | return -1; | |
479 | } | |
480 | rth->flags |= RTNL_HANDLE_F_LISTEN_ALL_NSID; | |
481 | return 0; | |
482 | } | |
483 | ||
8ed63ab1 | 484 | int rtnl_listen(struct rtnl_handle *rtnl, |
0628cddd | 485 | rtnl_listen_filter_t handler, |
6dc9f016 | 486 | void *jarg) |
aba5acdf SH |
487 | { |
488 | int status; | |
489 | struct nlmsghdr *h; | |
d17b136f | 490 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
aba5acdf | 491 | struct iovec iov; |
aba5acdf | 492 | struct msghdr msg = { |
8ed63ab1 SH |
493 | .msg_name = &nladdr, |
494 | .msg_namelen = sizeof(nladdr), | |
495 | .msg_iov = &iov, | |
496 | .msg_iovlen = 1, | |
aba5acdf | 497 | }; |
e557212e | 498 | char buf[16384]; |
449b824a ND |
499 | char cmsgbuf[BUFSIZ]; |
500 | ||
501 | if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) { | |
502 | msg.msg_control = &cmsgbuf; | |
503 | msg.msg_controllen = sizeof(cmsgbuf); | |
504 | } | |
aba5acdf | 505 | |
aba5acdf | 506 | iov.iov_base = buf; |
aba5acdf | 507 | while (1) { |
449b824a ND |
508 | struct rtnl_ctrl_data ctrl; |
509 | struct cmsghdr *cmsg; | |
510 | ||
aba5acdf SH |
511 | iov.iov_len = sizeof(buf); |
512 | status = recvmsg(rtnl->fd, &msg, 0); | |
513 | ||
514 | if (status < 0) { | |
aa8032e6 | 515 | if (errno == EINTR || errno == EAGAIN) |
aba5acdf | 516 | continue; |
aa8032e6 SH |
517 | fprintf(stderr, "netlink receive error %s (%d)\n", |
518 | strerror(errno), errno); | |
7f03191f PM |
519 | if (errno == ENOBUFS) |
520 | continue; | |
aa8032e6 | 521 | return -1; |
aba5acdf SH |
522 | } |
523 | if (status == 0) { | |
524 | fprintf(stderr, "EOF on netlink\n"); | |
525 | return -1; | |
526 | } | |
527 | if (msg.msg_namelen != sizeof(nladdr)) { | |
528 | fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen); | |
529 | exit(1); | |
530 | } | |
449b824a ND |
531 | |
532 | if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) { | |
533 | memset(&ctrl, 0, sizeof(ctrl)); | |
534 | ctrl.nsid = -1; | |
535 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; | |
536 | cmsg = CMSG_NXTHDR(&msg, cmsg)) | |
537 | if (cmsg->cmsg_level == SOL_NETLINK && | |
538 | cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID && | |
539 | cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { | |
540 | int *data = (int *)CMSG_DATA(cmsg); | |
541 | ||
542 | ctrl.nsid = *data; | |
543 | } | |
544 | } | |
545 | ||
aba5acdf SH |
546 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { |
547 | int err; | |
548 | int len = h->nlmsg_len; | |
549 | int l = len - sizeof(*h); | |
550 | ||
551 | if (l<0 || len>status) { | |
552 | if (msg.msg_flags & MSG_TRUNC) { | |
553 | fprintf(stderr, "Truncated message\n"); | |
554 | return -1; | |
555 | } | |
556 | fprintf(stderr, "!!!malformed message: len=%d\n", len); | |
557 | exit(1); | |
558 | } | |
559 | ||
449b824a | 560 | err = handler(&nladdr, &ctrl, h, jarg); |
aba5acdf SH |
561 | if (err < 0) |
562 | return err; | |
563 | ||
564 | status -= NLMSG_ALIGN(len); | |
565 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); | |
566 | } | |
567 | if (msg.msg_flags & MSG_TRUNC) { | |
568 | fprintf(stderr, "Message truncated\n"); | |
569 | continue; | |
570 | } | |
571 | if (status) { | |
572 | fprintf(stderr, "!!!Remnant of size %d\n", status); | |
573 | exit(1); | |
574 | } | |
575 | } | |
576 | } | |
577 | ||
0628cddd | 578 | int rtnl_from_file(FILE *rtnl, rtnl_listen_filter_t handler, |
6dc9f016 | 579 | void *jarg) |
aba5acdf SH |
580 | { |
581 | int status; | |
d17b136f | 582 | struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; |
e557212e | 583 | char buf[16384]; |
aba5acdf SH |
584 | struct nlmsghdr *h = (void*)buf; |
585 | ||
aba5acdf | 586 | while (1) { |
2dd9f8e0 | 587 | int err, len; |
aba5acdf SH |
588 | int l; |
589 | ||
590 | status = fread(&buf, 1, sizeof(*h), rtnl); | |
591 | ||
592 | if (status < 0) { | |
593 | if (errno == EINTR) | |
594 | continue; | |
595 | perror("rtnl_from_file: fread"); | |
596 | return -1; | |
597 | } | |
598 | if (status == 0) | |
599 | return 0; | |
600 | ||
601 | len = h->nlmsg_len; | |
aba5acdf SH |
602 | l = len - sizeof(*h); |
603 | ||
604 | if (l<0 || len>sizeof(buf)) { | |
605 | fprintf(stderr, "!!!malformed message: len=%d @%lu\n", | |
606 | len, ftell(rtnl)); | |
607 | return -1; | |
608 | } | |
609 | ||
610 | status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl); | |
611 | ||
612 | if (status < 0) { | |
613 | perror("rtnl_from_file: fread"); | |
614 | return -1; | |
615 | } | |
616 | if (status < l) { | |
617 | fprintf(stderr, "rtnl-from_file: truncated message\n"); | |
618 | return -1; | |
619 | } | |
620 | ||
0628cddd | 621 | err = handler(&nladdr, NULL, h, jarg); |
aba5acdf SH |
622 | if (err < 0) |
623 | return err; | |
624 | } | |
625 | } | |
626 | ||
2aa3dd29 SH |
627 | int addattr(struct nlmsghdr *n, int maxlen, int type) |
628 | { | |
629 | return addattr_l(n, maxlen, type, NULL, 0); | |
630 | } | |
631 | ||
632 | int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data) | |
633 | { | |
634 | return addattr_l(n, maxlen, type, &data, sizeof(__u8)); | |
635 | } | |
636 | ||
637 | int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data) | |
638 | { | |
639 | return addattr_l(n, maxlen, type, &data, sizeof(__u16)); | |
640 | } | |
641 | ||
aba5acdf SH |
642 | int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data) |
643 | { | |
2aa3dd29 SH |
644 | return addattr_l(n, maxlen, type, &data, sizeof(__u32)); |
645 | } | |
646 | ||
647 | int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data) | |
648 | { | |
649 | return addattr_l(n, maxlen, type, &data, sizeof(__u64)); | |
650 | } | |
651 | ||
652 | int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str) | |
653 | { | |
654 | return addattr_l(n, maxlen, type, str, strlen(str)+1); | |
aba5acdf SH |
655 | } |
656 | ||
8ed63ab1 | 657 | int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, |
6dc9f016 | 658 | int alen) |
aba5acdf SH |
659 | { |
660 | int len = RTA_LENGTH(alen); | |
661 | struct rtattr *rta; | |
662 | ||
3dabdbb3 | 663 | if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { |
007d3a3e | 664 | fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen); |
aba5acdf | 665 | return -1; |
007d3a3e | 666 | } |
07f94362 | 667 | rta = NLMSG_TAIL(n); |
aba5acdf SH |
668 | rta->rta_type = type; |
669 | rta->rta_len = len; | |
670 | memcpy(RTA_DATA(rta), data, alen); | |
3dabdbb3 | 671 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); |
aba5acdf SH |
672 | return 0; |
673 | } | |
674 | ||
07f94362 | 675 | int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len) |
676 | { | |
677 | if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) { | |
678 | fprintf(stderr, "addraw_l ERROR: message exceeded bound of %d\n",maxlen); | |
679 | return -1; | |
680 | } | |
681 | ||
682 | memcpy(NLMSG_TAIL(n), data, len); | |
683 | memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len); | |
684 | n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len); | |
685 | return 0; | |
686 | } | |
687 | ||
2f90c9c0 PM |
688 | struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type) |
689 | { | |
690 | struct rtattr *nest = NLMSG_TAIL(n); | |
691 | ||
692 | addattr_l(n, maxlen, type, NULL, 0); | |
693 | return nest; | |
694 | } | |
695 | ||
696 | int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest) | |
697 | { | |
698 | nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest; | |
699 | return n->nlmsg_len; | |
700 | } | |
701 | ||
702 | struct rtattr *addattr_nest_compat(struct nlmsghdr *n, int maxlen, int type, | |
703 | const void *data, int len) | |
704 | { | |
705 | struct rtattr *start = NLMSG_TAIL(n); | |
706 | ||
707 | addattr_l(n, maxlen, type, data, len); | |
708 | addattr_nest(n, maxlen, type); | |
709 | return start; | |
710 | } | |
711 | ||
712 | int addattr_nest_compat_end(struct nlmsghdr *n, struct rtattr *start) | |
713 | { | |
714 | struct rtattr *nest = (void *)start + NLMSG_ALIGN(start->rta_len); | |
715 | ||
716 | start->rta_len = (void *)NLMSG_TAIL(n) - (void *)start; | |
717 | addattr_nest_end(n, nest); | |
718 | return n->nlmsg_len; | |
719 | } | |
720 | ||
aba5acdf SH |
721 | int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data) |
722 | { | |
723 | int len = RTA_LENGTH(4); | |
724 | struct rtattr *subrta; | |
725 | ||
007d3a3e SH |
726 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) { |
727 | fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen); | |
aba5acdf | 728 | return -1; |
007d3a3e | 729 | } |
aba5acdf SH |
730 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
731 | subrta->rta_type = type; | |
732 | subrta->rta_len = len; | |
733 | memcpy(RTA_DATA(subrta), &data, 4); | |
734 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len; | |
735 | return 0; | |
736 | } | |
737 | ||
8ed63ab1 | 738 | int rta_addattr_l(struct rtattr *rta, int maxlen, int type, |
6dc9f016 | 739 | const void *data, int alen) |
aba5acdf SH |
740 | { |
741 | struct rtattr *subrta; | |
742 | int len = RTA_LENGTH(alen); | |
743 | ||
3dabdbb3 | 744 | if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) { |
007d3a3e | 745 | fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen); |
aba5acdf | 746 | return -1; |
007d3a3e | 747 | } |
aba5acdf SH |
748 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
749 | subrta->rta_type = type; | |
750 | subrta->rta_len = len; | |
751 | memcpy(RTA_DATA(subrta), data, alen); | |
3dabdbb3 | 752 | rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); |
aba5acdf SH |
753 | return 0; |
754 | } | |
755 | ||
303cc9cb RP |
756 | int rta_addattr8(struct rtattr *rta, int maxlen, int type, __u8 data) |
757 | { | |
758 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u8)); | |
759 | } | |
760 | ||
761 | int rta_addattr16(struct rtattr *rta, int maxlen, int type, __u16 data) | |
762 | { | |
763 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u16)); | |
764 | } | |
765 | ||
766 | int rta_addattr64(struct rtattr *rta, int maxlen, int type, __u64 data) | |
767 | { | |
768 | return rta_addattr_l(rta, maxlen, type, &data, sizeof(__u64)); | |
769 | } | |
770 | ||
771 | struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type) | |
772 | { | |
773 | struct rtattr *nest = RTA_TAIL(rta); | |
774 | ||
775 | rta_addattr_l(rta, maxlen, type, NULL, 0); | |
776 | ||
777 | return nest; | |
778 | } | |
779 | ||
780 | int rta_nest_end(struct rtattr *rta, struct rtattr *nest) | |
781 | { | |
782 | nest->rta_len = (void *)RTA_TAIL(rta) - (void *)nest; | |
783 | ||
784 | return rta->rta_len; | |
785 | } | |
786 | ||
aba5acdf SH |
787 | int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) |
788 | { | |
b1b7ce0f VY |
789 | return parse_rtattr_flags(tb, max, rta, len, 0); |
790 | } | |
791 | ||
792 | int parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta, | |
793 | int len, unsigned short flags) | |
794 | { | |
795 | unsigned short type; | |
796 | ||
175e2440 | 797 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); |
aba5acdf | 798 | while (RTA_OK(rta, len)) { |
b1b7ce0f VY |
799 | type = rta->rta_type & ~flags; |
800 | if ((type <= max) && (!tb[type])) | |
801 | tb[type] = rta; | |
aba5acdf SH |
802 | rta = RTA_NEXT(rta,len); |
803 | } | |
804 | if (len) | |
805 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len); | |
806 | return 0; | |
807 | } | |
c7699875 | 808 | |
809 | int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len) | |
810 | { | |
811 | int i = 0; | |
175e2440 SH |
812 | |
813 | memset(tb, 0, sizeof(struct rtattr *) * max); | |
c7699875 | 814 | while (RTA_OK(rta, len)) { |
175e2440 | 815 | if (rta->rta_type <= max && i < max) |
c7699875 | 816 | tb[i++] = rta; |
817 | rta = RTA_NEXT(rta,len); | |
818 | } | |
819 | if (len) | |
820 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len); | |
821 | return i; | |
822 | } | |
2f90c9c0 | 823 | |
decbb437 JP |
824 | struct rtattr *parse_rtattr_one(int type, struct rtattr *rta, int len) |
825 | { | |
826 | while (RTA_OK(rta, len)) { | |
827 | if (rta->rta_type == type) | |
828 | return rta; | |
829 | rta = RTA_NEXT(rta, len); | |
830 | } | |
831 | if (len) | |
832 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len); | |
833 | return NULL; | |
834 | } | |
835 | ||
2f90c9c0 PM |
836 | int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rta, |
837 | int len) | |
838 | { | |
839 | if (RTA_PAYLOAD(rta) < len) | |
840 | return -1; | |
841 | if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) { | |
842 | rta = RTA_DATA(rta) + RTA_ALIGN(len); | |
843 | return parse_rtattr_nested(tb, max, rta); | |
844 | } | |
037c635e | 845 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); |
2f90c9c0 PM |
846 | return 0; |
847 | } |