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