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