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