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