libnetlink.c revision 6cf8398f5f487762586801c25539d8fe5bb33b39
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 28int rcvbuf = 1024 * 1024; 29 30void rtnl_close(struct rtnl_handle *rth) 31{ 32 if (rth->fd >= 0) { 33 close(rth->fd); 34 rth->fd = -1; 35 } 36} 37 38int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions, 39 int protocol) 40{ 41 socklen_t addr_len; 42 int sndbuf = 32768; 43 44 memset(rth, 0, sizeof(*rth)); 45 46 rth->fd = socket(AF_NETLINK, SOCK_RAW, protocol); 47 if (rth->fd < 0) { 48 perror("Cannot open netlink socket"); 49 return -1; 50 } 51 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 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 87int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions) 88{ 89 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE); 90} 91 92int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type) 93{ 94 struct { 95 struct nlmsghdr nlh; 96 struct rtgenmsg g; 97 } req; 98 99 memset(&req, 0, sizeof(req)); 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 107 return send(rth->fd, (void*)&req, sizeof(req), 0); 108} 109 110int rtnl_send(struct rtnl_handle *rth, const void *buf, int len) 111{ 112 return send(rth->fd, buf, len, 0); 113} 114 115int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int len) 116{ 117 struct nlmsghdr *h; 118 int status; 119 char resp[1024]; 120 121 status = send(rth->fd, buf, len, 0); 122 if (status < 0) 123 return status; 124 125 /* Check for immediate errors */ 126 status = recv(rth->fd, resp, sizeof(resp), MSG_DONTWAIT|MSG_PEEK); 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; 141 return -1; 142 } 143 } 144 145 return 0; 146} 147 148int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len) 149{ 150 struct nlmsghdr nlh; 151 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK }; 152 struct iovec iov[2] = { 153 { .iov_base = &nlh, .iov_len = sizeof(nlh) }, 154 { .iov_base = req, .iov_len = len } 155 }; 156 struct msghdr msg = { 157 .msg_name = &nladdr, 158 .msg_namelen = sizeof(nladdr), 159 .msg_iov = iov, 160 .msg_iovlen = 2, 161 }; 162 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 172int rtnl_dump_filter_l(struct rtnl_handle *rth, 173 const struct rtnl_dump_filter_arg *arg) 174{ 175 struct sockaddr_nl nladdr; 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]; 184 185 iov.iov_base = buf; 186 while (1) { 187 int status; 188 const struct rtnl_dump_filter_arg *a; 189 int found_done = 0; 190 int msglen = 0; 191 192 iov.iov_len = sizeof(buf); 193 status = recvmsg(rth->fd, &msg, 0); 194 195 if (status < 0) { 196 if (errno == EINTR || errno == EAGAIN) 197 continue; 198 fprintf(stderr, "netlink receive error %s (%d)\n", 199 strerror(errno), errno); 200 return -1; 201 } 202 203 if (status == 0) { 204 fprintf(stderr, "EOF on netlink\n"); 205 return -1; 206 } 207 208 for (a = arg; a->filter; a++) { 209 struct nlmsghdr *h = (struct nlmsghdr*)buf; 210 msglen = status; 211 212 while (NLMSG_OK(h, msglen)) { 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; 225 } 226 227 if (h->nlmsg_type == NLMSG_DONE) { 228 found_done = 1; 229 break; /* process next filter */ 230 } 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; 241 } 242 err = a->filter(&nladdr, h, a->arg1); 243 if (err < 0) 244 return err; 245 246skip_it: 247 h = NLMSG_NEXT(h, msglen); 248 } 249 } 250 251 if (found_done) 252 return 0; 253 254 if (msg.msg_flags & MSG_TRUNC) { 255 fprintf(stderr, "Message truncated\n"); 256 continue; 257 } 258 if (msglen) { 259 fprintf(stderr, "!!!Remnant of size %d\n", msglen); 260 exit(1); 261 } 262 } 263} 264 265int 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 279int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, 280 unsigned groups, struct nlmsghdr *answer, 281 rtnl_filter_t junk, 282 void *jarg) 283{ 284 int status; 285 unsigned seq; 286 struct nlmsghdr *h; 287 struct sockaddr_nl nladdr; 288 struct iovec iov = { 289 .iov_base = (void*) n, 290 .iov_len = n->nlmsg_len 291 }; 292 struct msghdr msg = { 293 .msg_name = &nladdr, 294 .msg_namelen = sizeof(nladdr), 295 .msg_iov = &iov, 296 .msg_iovlen = 1, 297 }; 298 char buf[16384]; 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; 306 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 317 memset(buf,0,sizeof(buf)); 318 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) { 326 if (errno == EINTR || errno == EAGAIN) 327 continue; 328 fprintf(stderr, "netlink receive error %s (%d)\n", 329 strerror(errno), errno); 330 return -1; 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 354 if (nladdr.nl_pid != peer || 355 h->nlmsg_pid != rtnl->local.nl_pid || 356 h->nlmsg_seq != seq) { 357 if (junk) { 358 err = junk(&nladdr, h, jarg); 359 if (err < 0) 360 return err; 361 } 362 /* Don't forget to skip that message. */ 363 status -= NLMSG_ALIGN(len); 364 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); 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 404int rtnl_listen(struct rtnl_handle *rtnl, 405 rtnl_filter_t handler, 406 void *jarg) 407{ 408 int status; 409 struct nlmsghdr *h; 410 struct sockaddr_nl nladdr; 411 struct iovec iov; 412 struct msghdr msg = { 413 .msg_name = &nladdr, 414 .msg_namelen = sizeof(nladdr), 415 .msg_iov = &iov, 416 .msg_iovlen = 1, 417 }; 418 char buf[8192]; 419 420 memset(&nladdr, 0, sizeof(nladdr)); 421 nladdr.nl_family = AF_NETLINK; 422 nladdr.nl_pid = 0; 423 nladdr.nl_groups = 0; 424 425 iov.iov_base = buf; 426 while (1) { 427 iov.iov_len = sizeof(buf); 428 status = recvmsg(rtnl->fd, &msg, 0); 429 430 if (status < 0) { 431 if (errno == EINTR || errno == EAGAIN) 432 continue; 433 fprintf(stderr, "netlink receive error %s (%d)\n", 434 strerror(errno), errno); 435 if (errno == ENOBUFS) 436 continue; 437 return -1; 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 479int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler, 480 void *jarg) 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) { 493 int err, len; 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; 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 533int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data) 534{ 535 int len = RTA_LENGTH(4); 536 struct rtattr *rta; 537 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) { 538 fprintf(stderr,"addattr32: Error! max allowed bound %d exceeded\n",maxlen); 539 return -1; 540 } 541 rta = NLMSG_TAIL(n); 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 549int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, 550 int alen) 551{ 552 int len = RTA_LENGTH(alen); 553 struct rtattr *rta; 554 555 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { 556 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen); 557 return -1; 558 } 559 rta = NLMSG_TAIL(n); 560 rta->rta_type = type; 561 rta->rta_len = len; 562 memcpy(RTA_DATA(rta), data, alen); 563 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len); 564 return 0; 565} 566 567int 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 580struct 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 588int 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 594struct 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 604int 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 613int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data) 614{ 615 int len = RTA_LENGTH(4); 616 struct rtattr *subrta; 617 618 if (RTA_ALIGN(rta->rta_len) + len > maxlen) { 619 fprintf(stderr,"rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen); 620 return -1; 621 } 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 630int rta_addattr_l(struct rtattr *rta, int maxlen, int type, 631 const void *data, int alen) 632{ 633 struct rtattr *subrta; 634 int len = RTA_LENGTH(alen); 635 636 if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) { 637 fprintf(stderr,"rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen); 638 return -1; 639 } 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); 644 rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len); 645 return 0; 646} 647 648int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) 649{ 650 memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); 651 while (RTA_OK(rta, len)) { 652 if ((rta->rta_type <= max) && (!tb[rta->rta_type])) 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} 660 661int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len) 662{ 663 int i = 0; 664 665 memset(tb, 0, sizeof(struct rtattr *) * max); 666 while (RTA_OK(rta, len)) { 667 if (rta->rta_type <= max && i < max) 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} 675 676int __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 } 685 memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); 686 return 0; 687} 688