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