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