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