1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
* changes: Michal Kowalczuk <michal@kowalczuk.eu>
*
* This file is a based on the ip/ipaddress.c file from iproute2
* ipaddress.c authors (note: those folks are not involved in Dibbler development):
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
* Changes: Laszlo Valko <valko@linux.karinthy.hu>
*
* released under GNU GPL v2 only licence
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <fnmatch.h>
#include <time.h>
#include <errno.h>
#include "libnetlink.h"
#include "ll_map.h"
#include "utils.h"
#include "rt_names.h"
#include "Portable.h"
/*
#define LOWLEVEL_DEBUG 1
*/
#ifndef IPV6_RECVPKTINFO
#define IPV6_RECVPKTINFO IPV6_PKTINFO
#endif
#define ADDROPER_DEL 0
#define ADDROPER_ADD 1
#define ADDROPER_UPDATE 2
struct rtnl_handle rth;
char Message[1024] = {0};
int lowlevelInit()
{
if (rtnl_open(&rth, 0) < 0) {
sprintf(Message, "Cannot open rtnetlink\n");
return LOWLEVEL_ERROR_SOCKET;
}
return 0;
}
int lowlevelExit()
{
rtnl_close(&rth);
return 0;
}
struct nlmsg_list
{
struct nlmsg_list *next;
struct nlmsghdr h;
};
int print_linkinfo(struct nlmsghdr *n);
int print_addrinfo(struct nlmsghdr *n);
int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo);
void ipaddr_local_get(int *count, char **buf, int ifindex, struct nlmsg_list *ainfo);
void ipaddr_global_get(int *count, char **buf, int ifindex, struct nlmsg_list *ainfo);
void print_link_flags( unsigned flags);
int default_scope(inet_prefix *lcl);
int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{
struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
struct nlmsg_list *h;
struct nlmsg_list **lp;
h = malloc(n->nlmsg_len+sizeof(void*));
if (h == NULL)
return -1;
memcpy(&h->h, n, n->nlmsg_len);
h->next = NULL;
for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
*lp = h;
ll_remember_index(who, n, NULL);
return 0;
}
void release_nlmsg_list(struct nlmsg_list *n) {
struct nlmsg_list *tmp;
while (n) {
tmp = n->next;
free(n);
n = tmp;
}
}
void if_list_release(struct iface * list) {
struct iface * tmp;
while (list) {
tmp = list->next;
if (list->linkaddrcount)
free(list->linkaddr);
if (list->globaladdrcount)
free(list->globaladdr);
free(list);
list = tmp;
}
}
#define IF_RA_MANAGED 0x40
#define IF_RA_OTHERCONF 0x80
/**
* extracts M(managed) and O(other conf) flags set by Router Advertisement
*
* @param rta a pointer to tb[IFLA_PROTINFO] message received over netlink
*
* @return flags (see IF_RA_MANAGED and IF_RA_OTHERCONF)
*/
uint32_t link_get_mo_bits(struct rtattr* rta) {
size_t rtasize1;
void *rtadata;
struct rtattr *rta1;
uint32_t mo_flags = 0;
rtadata = RTA_DATA(rta);
rtasize1 = rta->rta_len;
for (rta1 = (struct rtattr *)rtadata; RTA_OK(rta1, rtasize1);
rta1 = RTA_NEXT(rta1, rtasize1)) {
void *rtadata1 = RTA_DATA(rta1);
switch(rta1->rta_type) {
case IFLA_INET6_FLAGS:
mo_flags = *((u_int32_t *)rtadata1);
#ifdef LOWLEVEL_DEBUG
if (mo_flags & IF_RA_MANAGED)
printf("M flag!");
if (mo_flags & IF_RA_OTHERCONF)
printf("O flag!");
#endif
return mo_flags;
}
}
return 0;
}
/*
* returns interface list with detailed informations
*/
struct iface * if_list_get()
{
struct nlmsg_list *linfo = NULL;
struct nlmsg_list *ainfo = NULL;
struct nlmsg_list *l;
struct rtnl_handle rth;
struct iface * head = NULL;
struct iface * tmp;
uint32_t mo_bits = 0;
/* required to display information about interface */
struct ifinfomsg *ifi;
/* table for storing base interface information */
struct rtattr * tb[IFLA_MAX + 1];
int len;
memset(tb, 0, sizeof(tb));
memset(&rth,0, sizeof(rth));
rtnl_open(&rth, 0);
rtnl_wilddump_request(&rth, AF_INET6, RTM_GETLINK);
rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL);
/* 2nd attribute: AF_UNSPEC, AF_INET, AF_INET6 */
rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETADDR);
rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL);
/* build list with interface names */
for (l=linfo; l; l = l->next) {
ifi = NLMSG_DATA(&l->h);
len = (&l->h)->nlmsg_len;
len -= NLMSG_LENGTH(sizeof(*ifi));
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
/* let's get M,O flags for interface */
mo_bits = 0;
if (tb[IFLA_PROTINFO]) {
mo_bits = link_get_mo_bits(tb[IFLA_PROTINFO]);
}
#ifdef LOWLEVEL_DEBUG
printf("### iface %d %s (flags:%d) (mo bits:%d)###\n",ifi->ifi_index,
(char*)RTA_DATA(tb[IFLA_IFNAME]),ifi->ifi_flags, mo_bits);
#endif
tmp = malloc(sizeof(struct iface));
memset(tmp, 0, sizeof(struct iface));
snprintf(tmp->name,MAX_IFNAME_LENGTH,"%s",(char*)RTA_DATA(tb[IFLA_IFNAME]));
tmp->id=ifi->ifi_index;
tmp->flags=ifi->ifi_flags;
tmp->hardwareType = ifi->ifi_type;
tmp->next=head;
head=tmp;
tmp->m_bit = (mo_bits & IF_RA_MANAGED)?1:0;
tmp->o_bit = (mo_bits & IF_RA_OTHERCONF)?1:0;
memset(tmp->mac,0,255);
/* This stuff reads MAC addr */
/* Does inetface have LL_ADDR? */
if (tb[IFLA_ADDRESS]) {
tmp->maclen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
memcpy(tmp->mac,RTA_DATA(tb[IFLA_ADDRESS]), tmp->maclen);
}
/* Tunnels can have no LL_ADDR. RTA_PAYLOAD doesn't check it and try to
* dereference it in this manner
* #define RTA_PAYLOAD(rta) ((int)((rta)->rta_len) - RTA_LENGTH(0))
* */
else {
tmp->maclen=0;
}
ipaddr_local_get(&tmp->linkaddrcount, &tmp->linkaddr, tmp->id, ainfo);
ipaddr_global_get(&tmp->globaladdrcount, &tmp->globaladdr, tmp->id, ainfo);
}
release_nlmsg_list(linfo);
release_nlmsg_list(ainfo);
rtnl_close(&rth);
return head;
}
/**
* returns local addresses for specified interface
*/
void ipaddr_local_get(int *count, char **bufPtr, int ifindex, struct nlmsg_list *ainfo) {
int cnt=0;
char * buf=0, * tmpbuf=0;
char addr[16];
struct rtattr * rta_tb[IFA_MAX+1];
int pos;
for ( ;ainfo ; ainfo = ainfo->next) {
struct nlmsghdr *n = &ainfo->h;
struct ifaddrmsg *ifa = NLMSG_DATA(n);
if ( (ifa->ifa_family == AF_INET6) && (ifa->ifa_index == ifindex) ) {
memset(rta_tb, 0, sizeof(*rta_tb));
parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
memcpy(addr,(char*)RTA_DATA(rta_tb[IFLA_ADDRESS]),16);
if (addr[0]!=0xfe || addr[1]!=0x80) {
continue; /* ignore non link-scoped addrs */
}
/* ifa->ifa_flags & 128 - permenent */
/* printf("flags:%d : ",ifa->ifa_flags); */
pos = cnt*16;
buf = (char*) malloc( pos + 16);
if (pos > 0) {
memcpy(buf, tmpbuf, pos); /* copy old addrs */
}
memcpy(buf+pos,addr,16); /* copy new addr */
if (pos > 0) {
free(tmpbuf);
}
tmpbuf = buf;
cnt++;
}
}
*count = cnt;
*bufPtr = buf;
}
/**
* returns non-local addresses for specified interface
*/
void ipaddr_global_get(int *count, char **bufPtr, int ifindex, struct nlmsg_list *ainfo) {
int cnt=0;
char * buf=0, * tmpbuf=0;
char addr[16];
struct rtattr * rta_tb[IFA_MAX+1];
int pos;
for ( ;ainfo ; ainfo = ainfo->next) {
struct nlmsghdr *n = &ainfo->h;
struct ifaddrmsg *ifa = NLMSG_DATA(n);
if ( (ifa->ifa_family == AF_INET6) && (ifa->ifa_index == ifindex) ) {
memset(rta_tb, 0, sizeof(*rta_tb));
parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
memcpy(addr,(char*)RTA_DATA(rta_tb[IFLA_ADDRESS]),16);
if ( (addr[0]==0xfe && addr[1]==0x80) || /* link local */
(addr[0]==0xff) ) { /* multicast */
continue; /* ignore non link-scoped addrs */
}
/* ifa->ifa_flags & 128 - permenent */
/* printf("flags:%d : ",ifa->ifa_flags); */
pos = cnt*16;
buf = (char*) malloc( pos + 16);
if (pos > 0) {
memcpy(buf,tmpbuf, pos); /* copy old addrs */
}
memcpy(buf+pos,addr,16); /* copy new addr */
if (pos > 0) {
free(tmpbuf);
}
tmpbuf = buf;
cnt++;
}
}
*count = cnt;
*bufPtr = buf;
}
/**
* adds, updates or deletes addresses to interface
*
* @param addr
* @param ifacename
* @param prefixLen
* @param preferred
* @param valid
* @param mode - 0-delete, 1-add, 2-update
*
* @return
*/
int ipaddr_add_or_del(const char * addr, const char *ifacename, int prefixLen,
unsigned long preferred, unsigned long valid, int mode)
{
struct rtnl_handle rth;
struct {
struct nlmsghdr n;
struct ifaddrmsg ifa;
char buf[256];
} req;
inet_prefix lcl;
/* inet_prefix peer; */
int local_len = 0;
int peer_len = 0;
int scoped = 0;
struct ifa_cacheinfo ci;
#ifdef LOWLEVEL_DEBUG
printf("### iface=%s, addr=%s, add=%d ###\n", ifacename, addr, add);
#endif
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
switch (mode) {
case ADDROPER_DEL:
req.n.nlmsg_type = RTM_DELADDR; /* del address */
req.n.nlmsg_flags = NLM_F_REQUEST;
break;
case ADDROPER_ADD:
req.n.nlmsg_type = RTM_NEWADDR; /* add address */
req.n.nlmsg_flags = NLM_F_REQUEST |NLM_F_CREATE|NLM_F_EXCL;
break;
case ADDROPER_UPDATE:
req.n.nlmsg_type = RTM_NEWADDR; /* update address */
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE;
break;
}
req.ifa.ifa_family = AF_INET6;
req.ifa.ifa_flags = 0;
req.ifa.ifa_prefixlen = prefixLen;
get_prefix_1(&lcl, (char*)addr, AF_INET6);
addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
local_len = lcl.bytelen;
memset(&ci, 0, sizeof(ci));
ci.ifa_valid = valid;
ci.ifa_prefered = preferred;
addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &ci, sizeof(ci));
if (peer_len == 0 && local_len) {
/* peer = lcl; */
addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
}
if (req.ifa.ifa_prefixlen == 0)
req.ifa.ifa_prefixlen = lcl.bitlen;
if (!scoped)
req.ifa.ifa_scope = default_scope(&lcl);
rtnl_open(&rth, 0);
ll_init_map(&rth);
/* is there an interface with this ifindex? */
if ((req.ifa.ifa_index = ll_name_to_index((char*)ifacename)) == 0) {
sprintf(Message, "Cannot find device: %s", ifacename);
return LOWLEVEL_ERROR_UNSPEC;
}
rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL); fflush(stdout);
return LOWLEVEL_NO_ERROR;
}
int ipaddr_add(const char * ifacename, int ifaceid, const char * addr, unsigned long pref,
unsigned long valid, int prefixLength)
{
return ipaddr_add_or_del(addr,ifacename, prefixLength, pref, valid, ADDROPER_ADD);
}
int ipaddr_update(const char* ifacename, int ifindex, const char* addr,
unsigned long pref, unsigned long valid, int prefixLength)
{
/** @todo: Linux kernel currently does not provide API for dynamic addresses */
return ipaddr_add_or_del(addr, ifacename, prefixLength, pref, valid, ADDROPER_UPDATE);
}
int ipaddr_del(const char * ifacename, int ifaceid, const char * addr, int prefixLength)
{
return ipaddr_add_or_del(addr,ifacename, prefixLength, 0/*pref*/, 0/*valid*/, ADDROPER_DEL);
}
int sock_add(char * ifacename,int ifaceid, char * addr, int port, int thisifaceonly, int reuse)
{
int error;
int on = 1;
struct addrinfo hints;
struct addrinfo *res, *res2;
struct ipv6_mreq mreq6;
int Insock;
int multicast;
char port_char[6];
char * tmp;
struct sockaddr_in6 bindme;
sprintf(port_char,"%d",port);
if (!strncasecmp(addr,"ff",2))
multicast = 1;
else
multicast = 0;
#ifdef LOWLEVEL_DEBUG
printf("### iface: %s(id=%d), addr=%s, port=%d, ifaceonly=%d reuse=%d###\n",
ifacename,ifaceid, addr, port, thisifaceonly, reuse);
fflush(stdout);
#endif
/* Open a socket for inbound traffic */
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET6;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
if( (error = getaddrinfo(NULL, port_char, &hints, &res)) ){
sprintf(Message, "getaddrinfo failed. Is IPv6 protocol supported by kernel?");
return LOWLEVEL_ERROR_GETADDRINFO;
}
if( (Insock = socket(AF_INET6, SOCK_DGRAM,0 )) < 0){
sprintf(Message, "socket creation failed. Is IPv6 protocol supported by kernel?");
return LOWLEVEL_ERROR_UNSPEC;
}
/* Set the options to receivce ipv6 traffic */
if (setsockopt(Insock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on)) < 0) {
sprintf(Message, "Unable to set up socket option IPV6_RECVPKTINFO.");
return LOWLEVEL_ERROR_SOCK_OPTS;
}
/* bind to device only when running as root */
if (thisifaceonly && !getuid()) {
if (setsockopt(Insock, SOL_SOCKET, SO_BINDTODEVICE, ifacename, strlen(ifacename)+1) <0) {
sprintf(Message, "Unable to bind socket to interface %s.", ifacename);
return LOWLEVEL_ERROR_BIND_IFACE;
}
}
/* allow address reuse (this option sucks - why allow running multiple servers?) */
if (reuse!=0) {
if (setsockopt(Insock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
sprintf(Message, "Unable to set up socket option SO_REUSEADDR.");
return LOWLEVEL_ERROR_REUSE_FAILED;
}
}
/* bind socket to a specified port */
bzero(&bindme, sizeof(struct sockaddr_in6));
bindme.sin6_family = AF_INET6;
bindme.sin6_port = htons(port);
bindme.sin6_scope_id = ifaceid;
tmp = (char*)(&bindme.sin6_addr);
inet_pton6(addr, tmp);
if (bind(Insock, (struct sockaddr*)&bindme, sizeof(bindme)) < 0) {
sprintf(Message, "Unable to bind socket: %s", strerror(errno) );
return LOWLEVEL_ERROR_BIND_FAILED;
}
freeaddrinfo(res);
/* multicast server stuff */
if (multicast) {
hints.ai_flags = 0;
if((error = getaddrinfo(addr, port_char, &hints, &res2))){
sprintf(Message, "Failed to obtain getaddrinfo");
return LOWLEVEL_ERROR_GETADDRINFO;
}
memset(&mreq6, 0, sizeof(mreq6));
mreq6.ipv6mr_interface = ifaceid;
memcpy(&mreq6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)res2->ai_addr)->sin6_addr,
sizeof(mreq6.ipv6mr_multiaddr));
/* Add to the all agent multicast address */
if (setsockopt(Insock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6))) {
sprintf(Message, "error joining ipv6 group");
return LOWLEVEL_ERROR_MCAST_MEMBERSHIP;
}
freeaddrinfo(res2);
}
return Insock;
}
int sock_del(int fd)
{
return close(fd);
}
int sock_send(int sock, char *addr, char *buf, int message_len, int port, int iface )
{
struct addrinfo hints, *res;
int result;
char cport[10];
sprintf(cport,"%d",port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET6;
hints.ai_socktype = SOCK_DGRAM;
if (getaddrinfo(addr, cport, &hints, &res) < 0) {
return -1; /* Error in transmitting */
}
result = sendto(sock, buf, message_len, 0, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
if (result<0) {
sprintf(Message, "Unable to send data (dst addr: %s)", addr);
return LOWLEVEL_ERROR_SOCKET;
}
return LOWLEVEL_NO_ERROR;
}
/*
*
*/
int sock_recv(int fd, char * myPlainAddr, char * peerPlainAddr, char * buf, int buflen)
{
struct msghdr msg; /* message received by recvmsg */
struct sockaddr_in6 peerAddr; /* sender address */
struct sockaddr_in6 myAddr; /* my address */
struct iovec iov; /* simple structure containing buffer address and length */
struct cmsghdr *cm; /* control message */
struct in6_pktinfo *pktinfo;
char control[CMSG_SPACE(sizeof(struct in6_pktinfo))];
char controlLen = CMSG_SPACE(sizeof(struct in6_pktinfo));
int result = 0;
bzero(&msg, sizeof(msg));
bzero(&peerAddr, sizeof(peerAddr));
bzero(&myAddr, sizeof(myAddr));
bzero(&control, sizeof(control));
iov.iov_base = buf;
iov.iov_len = buflen;
msg.msg_name = &peerAddr;
msg.msg_namelen = sizeof(peerAddr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control;
msg.msg_controllen = controlLen;
result = recvmsg(fd, &msg, 0);
if (result==-1) {
return LOWLEVEL_ERROR_UNSPEC;
}
/* get source address */
inet_ntop6((void*)&peerAddr.sin6_addr, peerPlainAddr);
/* get destination address */
for(cm = (struct cmsghdr *) CMSG_FIRSTHDR(&msg); cm; cm = (struct cmsghdr *) CMSG_NXTHDR(&msg, cm)){
if (cm->cmsg_level != IPPROTO_IPV6 || cm->cmsg_type != IPV6_PKTINFO)
continue;
pktinfo= (struct in6_pktinfo *) (CMSG_DATA(cm));
inet_ntop6((void*)&pktinfo->ipi6_addr, myPlainAddr);
}
return result;
}
void microsleep(int microsecs)
{
struct timespec x,y;
x.tv_sec = (int) microsecs / 1000000;
x.tv_nsec = ( microsecs - x.tv_sec*1000000) * 1000;
nanosleep(&x,&y);
}
/*
* returns: -1 - address not found, 0 - addr is ok, 1 - addr is tentative
*/
int is_addr_tentative(char * ifacename, int iface, char * addr)
{
char buf[256];
char packed1[16];
char packed2[16];
struct rtattr * rta_tb[IFA_MAX+1];
struct nlmsg_list *ainfo = NULL;
struct nlmsg_list *head = NULL;
struct rtnl_handle rth;
int tentative = LOWLEVEL_TENTATIVE_DONT_KNOW;
inet_pton6(addr,packed1);
rtnl_open(&rth, 0);
/* 2nd attribute: AF_UNSPEC, AF_INET, AF_INET6 */
/* rtnl_wilddump_request(&rth, AF_PACKET, RTM_GETLINK); */
rtnl_wilddump_request(&rth, AF_INET6, RTM_GETADDR);
rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL);
head = ainfo;
while (ainfo) {
struct nlmsghdr *n = &ainfo->h;
struct ifaddrmsg *ifa = NLMSG_DATA(n);
memset(rta_tb, 0, sizeof(*rta_tb));
if (ifa->ifa_index == iface && ifa->ifa_family==AF_INET6) {
parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
inet_ntop6(RTA_DATA(rta_tb[IFA_LOCAL]), buf /*, sizeof(buf)*/);
memcpy(packed2,RTA_DATA(rta_tb[IFA_LOCAL]),16);
/* print_packed(packed1); printf(" "); print_packed(packed2); printf("\n"); */
/* is this addr which are we looking for? */
if (!memcmp(packed1,packed2,16) ) {
if (ifa->ifa_flags & IFA_F_TENTATIVE)
tentative = LOWLEVEL_TENTATIVE_YES;
else
tentative = LOWLEVEL_TENTATIVE_NO;
}
}
ainfo = ainfo->next;
}
/* now delete list */
while (head) {
ainfo = head;
head = head->next;
free(ainfo);
}
rtnl_close(&rth);
return tentative;
}
char * getAAAKeyFilename(uint32_t SPI)
{
static char filename[1024];
if (SPI != 0)
snprintf(filename, 1024, "%s%s%08x", "/var/lib/dibbler/AAA/", "AAA-key-", SPI);
else
strcpy(filename, "/var/lib/dibbler/AAA/AAA-key");
return filename;
}
char * getAAAKey(uint32_t SPI, unsigned *len) {
char * filename = 0;
struct stat st;
char * retval;
int offset = 0;
int fd;
int ret;
filename = getAAAKeyFilename(SPI);
if (stat(filename, &st))
return NULL;
fd = open(filename, O_RDONLY);
if (0 > fd)
return NULL;
retval = malloc(st.st_size);
if (!retval) {
close(fd);
return NULL;
}
while (offset < st.st_size) {
ret = read(fd, retval + offset, st.st_size - offset);
if (!ret) break;
if (ret < 0) {
close(fd);
free(retval);
return NULL;
}
offset += ret;
}
close(fd);
if (offset != st.st_size) {
free(retval);
return NULL;
}
*len = st.st_size;
return retval;
}
char * error_message()
{
return Message;
}
int get_mac_from_ipv6(const char* iface_name, int ifindex, const char* v6addr,
char* mac, int* mac_len) {
/// @todo: Implement this for Linux
/// see "/sbin/ip -6 neigh"
return LOWLEVEL_ERROR_NOT_IMPLEMENTED;
}
/* iproute.c dummy link section */
int show_stats = 0; /* to disable iproute.c messages */
int preferred_family = AF_INET6;
char* rtnl_rtntype_n2a(int id, char *buf, int len) { return 0;}
char* rtnl_dsfield_n2a(int id, char *buf, int len) { return 0;}
char* rtnl_rttable_n2a(int id, char *buf, int len) { return 0;}
char* rtnl_rtprot_n2a(int id, char *buf, int len) { return 0;}
char* rtnl_rtscope_n2a(int id, char *buf, int len) { return 0;}
char* rtnl_rtrealm_n2a(int id, char *buf, int len) { return 0;}
int rtnl_rtprot_a2n(__u32 *id, char *arg) { return 0; }
int rtnl_rtscope_a2n(__u32 *id, char *arg) { return 0; }
int rtnl_rttable_a2n(__u32 *id, char *arg) { return 0; }
int rtnl_rtrealm_a2n(__u32 *id, char *arg) { return 0; }
int rtnl_dsfield_a2n(__u32 *id, char *arg) { return 0; }
int rtnl_rtntype_a2n(int *id, char *arg) { return 0; }
int get_rt_realms(__u32 *realms, char *arg){ return 0; }
int default_scope(inet_prefix *lcl) { return 0; }
char * _SL_;
int dnet_pton(int af, const char *src, void *addr) { return 0; }
const char *dnet_ntop(int af, const void *addr, char *str, size_t len){ return 0; }
const char *ipx_ntop(int af, const void *addr, char *str, size_t len) { return 0; }
#ifdef DEBUG
#include <execinfo.h>
/*
[Thomson: very clever function. It prints backtrace. Done by Michal Kowalczuk.]
*/
void print_trace(void)
{
void *array[10];
size_t size;
size = backtrace (array, 10);
backtrace_symbols_fd (array, size, 1);
}
#endif
|