kernel_socket.c 22.7 KB
Newer Older
1 2
/*
Copyright (c) 2007 by Grégoire Henry
3
Copyright (c) 2008, 2009 by Juliusz Chroboczek
dermiste's avatar
dermiste committed
4
Copyright (c) 2010 by Vincent Gross
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

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <strings.h>
#include <netinet/in.h>
34
#include <netinet/icmp6.h>
35 36 37 38 39
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
40
#include <ifaddrs.h>
41 42
#include <net/if.h>
#include <net/if_dl.h>
43
#include <net/if_media.h>
44 45
#include <net/route.h>

46
#include "babeld.h"
47 48 49 50
#include "neighbour.h"
#include "kernel.h"
#include "util.h"

51 52 53

static int get_sdl(struct sockaddr_dl *sdl, char *ifname);

54 55 56
static const unsigned char v4prefix[16] =
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };

57
int export_table = -1, import_table_count = 0, import_tables[MAX_IMPORT_TABLES];
58

59 60 61
int
if_eui64(char *ifname, int ifindex, unsigned char *eui)
{
62 63
    struct sockaddr_dl sdl;
    char *tmp = NULL;
64
    if(get_sdl(&sdl, ifname) < 0) {
65 66 67
        return -1;
    }
    tmp = sdl.sdl_data + sdl.sdl_nlen;
68
    if(sdl.sdl_alen == 8) {
69 70
        memcpy(eui, tmp, 8);
        eui[0] ^= 2;
71
    } else if(sdl.sdl_alen == 6) {
72 73 74 75 76 77 78 79
        memcpy(eui,   tmp,   3);
        eui[3] = 0xFF;
        eui[4] = 0xFE;
        memcpy(eui+5, tmp+3, 3);
    } else {
        return -1;
    }
    return 0;
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
/* fill sdl with the structure corresponding to ifname.
 Warning: make a syscall (and get all interfaces).
 return -1 if an error occurs, 0 otherwise. */
static int
get_sdl(struct sockaddr_dl *sdl, char *ifname)
{
    int mib[6];
    size_t buf_len = 0;
    int offset = 0;
    char *buffer = NULL;
    struct if_msghdr *ifm = NULL;
    struct sockaddr_dl *tmp_sdl = NULL;
    int rc;

    mib[0] = CTL_NET;
    mib[1] = PF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    mib[5] = 0;

    rc = sysctl(mib, 6, NULL, &buf_len, NULL, 0);
    if(rc < 0)
        return -1;

    buffer = (char *)malloc(buf_len);
108 109
    if(buffer == NULL)
        return -1;
110 111 112 113 114 115

    rc = sysctl(mib, 6, buffer, &buf_len, NULL, 0);
    if(rc < 0)
        goto fail;

    offset = 0;
116
    while(offset < (int) buf_len) {
117
        ifm = (struct if_msghdr *) &buffer[offset];
118 119 120 121 122 123 124 125 126 127
        switch(ifm->ifm_type) {
        case RTM_IFINFO:
            tmp_sdl = (struct sockaddr_dl *) (ifm + 1);
            if(strncmp(ifname, tmp_sdl->sdl_data, tmp_sdl->sdl_nlen) == 0
               && strlen(ifname) == tmp_sdl->sdl_nlen) {
                memcpy(sdl, tmp_sdl, sizeof(struct sockaddr_dl));
                return 0;
            }
        default:
            break;
128 129 130 131 132 133 134 135
        }
        offset += ifm->ifm_msglen;
    }

fail:
    free(buffer);
    return -1;
}
136

137
/* KAME said : "Following two macros are highly depending on KAME Release" */
138
#define IN6_LINKLOCAL_IFINDEX(a)  ((a).s6_addr[2] << 8 | (a).s6_addr[3])
139 140 141 142
#define SET_IN6_LINKLOCAL_IFINDEX(a, i)         \
    do {                                        \
        (a).s6_addr[2] = ((i) >> 8) & 0xff;     \
        (a).s6_addr[3] = (i) & 0xff;            \
143
    } while(0)
144

145
#if defined(__APPLE__)
146 147
#define ROUNDUP(a) \
    ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
148 149 150 151
#else
#define ROUNDUP(a) \
    ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
#endif
152 153 154 155 156 157 158

static int old_forwarding = -1;
static int old_accept_redirects = -1;

static int ifindex_lo = -1;
static int seq;

159
static int
160
mask2len(const unsigned char *p, const int size)
161 162 163
{
    int i = 0, j;

164
    for(j = 0; j < size; j++, p++) {
165 166 167 168
        if(*p != 0xff)
            break;
        i += 8;
    }
169
    if(j < size) {
170
        switch(*p) {
171
#define MASKLEN(m, l) case m: do { i += l; break; } while(0)
172 173 174 175 176 177 178
            MASKLEN(0xfe, 7); break;
            MASKLEN(0xfc, 6); break;
            MASKLEN(0xf8, 5); break;
            MASKLEN(0xf0, 4); break;
            MASKLEN(0xe0, 3); break;
            MASKLEN(0xc0, 2); break;
            MASKLEN(0x80, 1); break;
179
#undef MASKLEN
180 181 182 183 184
        }
    }
    return i;
}

185
static void
186 187 188 189 190 191 192 193 194 195 196
plen2mask(int n, struct in6_addr *dest)
{
    unsigned char *p;
    int i;

    static const int pl2m[9] = {
        0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
    };

    memset(dest, 0, sizeof(struct in6_addr));
    p = (u_char *)dest;
197 198
    for(i = 0; i < 16; i++, p++, n -= 8) {
        if(n >= 8) {
199 200 201 202 203 204 205 206 207 208
            *p = 0xff;
            continue;
        }
        *p = pl2m[n];
        break;
    }
    return;
}

int
209
kernel_setup(int setup)
210
{
dermiste's avatar
dermiste committed
211
    int rc = 0;
212 213 214 215 216
    int forwarding = 1;
    int accept_redirects = 0;
    int mib[4];
    size_t datasize;

217 218
    if(skip_kernel_setup) return 1;

219 220 221 222 223 224 225
    mib[0] = CTL_NET;
    mib[1] = AF_INET6;
    seq = time(NULL);

    mib[2] = IPPROTO_IPV6;
    mib[3] = IPV6CTL_FORWARDING;
    datasize = sizeof(old_forwarding);
226 227 228 229 230 231 232 233
    if(setup) {
        rc = sysctl(mib, 4, &old_forwarding, &datasize, NULL, 0);
        if(rc == 0 && old_forwarding != forwarding) {
            rc = sysctl(mib, 4, &old_forwarding, &datasize,
                        &forwarding, datasize);
        }
    }
    else if(old_forwarding >= 0 && old_forwarding != forwarding)
234
        rc = sysctl(mib, 4, NULL, NULL,
dermiste's avatar
dermiste committed
235
                    &old_forwarding, datasize);
236
    if(rc == -1) {
dermiste's avatar
dermiste committed
237
        perror("Couldn't tweak forwarding knob.");
238 239 240
        return -1;
    }

dermiste's avatar
dermiste committed
241
    rc = 0;
242
    mib[2] = IPPROTO_ICMPV6;
243 244 245
#if defined(IPV6CTL_SENDREDIRECTS)
    mib[3] = IPV6CTL_SENDREDIRECTS;
#else
246
    mib[3] = ICMPV6CTL_REDIRACCEPT;
247
#endif
248
    datasize = sizeof(old_accept_redirects);
249 250 251 252 253 254
    if(setup) {
        rc = sysctl(mib, 4, &old_accept_redirects, &datasize, NULL, 0);
        if(rc == 0 && old_accept_redirects != accept_redirects) {
            rc = sysctl(mib, 4, &old_accept_redirects, &datasize,
                        &accept_redirects, datasize);
        }
Matthieu Boutier's avatar
Matthieu Boutier committed
255
    } else if(old_accept_redirects >= 0 && old_accept_redirects != accept_redirects)
256
        rc = sysctl(mib, 4, NULL, NULL,
dermiste's avatar
dermiste committed
257
                    &old_accept_redirects, datasize);
258
    if(rc == -1) {
dermiste's avatar
dermiste committed
259
        perror("Couldn't tweak accept_redirects knob.");
260
        return -1;
261
    }
262
    return 1;
263 264 265 266 267 268 269 270 271
}

int
kernel_setup_socket(int setup)
{
    int rc;
    int zero = 0;
    if(setup) {
        if(kernel_socket < 0) {
272
            kernel_socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
273 274 275
            if(kernel_socket < 0)
                return -1;
        }
276
        rc = setsockopt(kernel_socket, SOL_SOCKET, SO_USELOOPBACK,
277
                        &zero, sizeof(zero));
278
        if(rc < 0)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
            goto error;
        return 1;
    } else {
        close(kernel_socket);
        kernel_socket = -1;
        return 1;
    }

 error: {
        int savederrno = errno;
        perror("setsockopt(kernel_socket)");
        close(kernel_socket);
        errno = savederrno;
        kernel_socket = -1;
        return -1;
    }
}

int
kernel_setup_interface(int setup, const char *ifname, int ifindex)
{
    return 1;
}

303 304 305
int
kernel_interface_operational(const char *ifname, int ifindex)
{
306
    struct ifreq req;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
307
    int s, rc;
308 309
    int flags = link_detect ? (IFF_UP | IFF_RUNNING) : IFF_UP;

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
310 311 312 313
    s = socket(PF_INET, SOCK_DGRAM, 0);
    if(s < 0)
        return -1;

314 315 316
    memset(&req, 0, sizeof(req));
    memset(&req, 0, sizeof(req));
    strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
317 318
    rc = ioctl(s, SIOCGIFFLAGS, &req);
    close(s);
319 320 321
    if(rc < 0)
        return -1;
    return ((req.ifr_flags & flags) == flags);
322 323
}

324
int
325
kernel_interface_ipv4(const char *ifname, int ifindex, unsigned char *addr_r)
326 327 328 329 330 331 332 333 334 335 336 337
{
    struct ifreq req;
    int s, rc;

    s = socket(PF_INET, SOCK_DGRAM, 0);
    if(s < 0)
        return -1;

    memset(&req, 0, sizeof(req));
    strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
    req.ifr_addr.sa_family = AF_INET;
    rc = ioctl(s, SIOCGIFADDR, &req);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
338
    close(s);
339 340 341 342 343 344 345 346
    if(rc < 0) {
        return -1;
    }

    memcpy(addr_r, &((struct sockaddr_in*)&req.ifr_addr)->sin_addr, 4);
    return 1;
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
int
kernel_interface_mtu(const char *ifname, int ifindex)
{
    struct ifreq req;
    int s, rc;

    s = socket(PF_INET, SOCK_DGRAM, 0);
    if(s < 0)
        return -1;

    memset(&req, 0, sizeof(req));
    strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
    rc = ioctl(s, SIOCGIFMTU, &req);
    if(rc < 0) {
        close(s);
        return -1;
    }

    return req.ifr_mtu;
}

int
kernel_interface_wireless(const char *ifname, int ifindex)
{
371 372 373 374 375 376 377 378
    struct ifmediareq ifmr;
    int s, rc;

    s = socket(PF_INET6, SOCK_DGRAM, 0);
    memset(&ifmr, 0, sizeof(ifmr));
    strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
    rc = ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr);
    close(s);
379
    if(rc < 0)
dermiste's avatar
dermiste committed
380
        return rc;
381
    if((ifmr.ifm_active & IFM_NMASK) == IFM_IEEE80211)
dermiste's avatar
dermiste committed
382
        return 1;
383
    else
dermiste's avatar
dermiste committed
384
        return 0;
385 386
}

387 388 389 390 391 392 393
int
kernel_interface_channel(const char *ifname, int ifindex)
{
    errno = ENOSYS;
    return -1;
}

394 395 396 397 398 399
int
kernel_disambiguate(int v4)
{
    return 0;
}

400 401 402 403 404 405
int
kernel_has_ipv6_subtrees(void)
{
    return 0;
}

406
int
407 408
kernel_route(int operation, int table,
             const unsigned char *dest, unsigned short plen,
409
             const unsigned char *src, unsigned short src_plen,
410 411
             const unsigned char *gate, int ifindex, unsigned int metric,
             const unsigned char *newgate, int newifindex,
412
             unsigned int newmetric, int newtable)
413
{
414
    struct {
415 416
        struct rt_msghdr m_rtm;
        char m_space[512];
417 418 419
    } msg;
    char *data = msg.m_space;
    int rc, ipv4;
420

421 422
    char local6[1][1][16] = IN6ADDR_LOOPBACK_INIT;
    char local4[1][1][16] =
dermiste's avatar
dermiste committed
423 424
        {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x01 }}};
425

426 427 428 429 430 431
    /* Source-specific routes are not implemented yet for BSD. */
    if(src_plen > 0) {
        errno = ENOSYS;
        return -1;
    }

432 433 434 435 436 437
    /* Check that the protocol family is consistent. */
    if(plen >= 96 && v4mapped(dest)) {
        if(!v4mapped(gate)) {
            errno = EINVAL;
            return -1;
        }
438
        ipv4 = 1;
439 440 441 442 443
    } else {
        if(v4mapped(gate)) {
            errno = EINVAL;
            return -1;
        }
444
        ipv4 = 0;
445 446
    }

447
    if(operation == ROUTE_MODIFY && newmetric == metric &&
448
       memcmp(newgate, gate, 16) == 0 && newifindex == ifindex)
449
        return 0;
450

451

452
    if(operation == ROUTE_MODIFY) {
453 454

        /* Avoid atomic route changes that is buggy on OS X. */
455
        kernel_route(ROUTE_FLUSH, table, dest, plen,
456
                     src, src_plen,
457
                     gate, ifindex, metric,
458 459
                     NULL, 0, 0, 0);
        return kernel_route(ROUTE_ADD, table, dest, plen,
460
                            src, src_plen,
461
                            newgate, newifindex, newmetric,
462
                            NULL, 0, 0, 0);
463

464 465
    }

466
    kdebugf("kernel_route: %s %s/%d metric %d dev %d nexthop %s\n",
dermiste's avatar
dermiste committed
467 468 469 470
            operation == ROUTE_ADD ? "add" :
            operation == ROUTE_FLUSH ? "flush" : "change",
            format_address(dest), plen, metric, ifindex,
            format_address(gate));
471 472

    if(kernel_socket < 0) kernel_setup_socket(1);
dermiste's avatar
dermiste committed
473

474
    memset(&msg, 0, sizeof(msg));
475
    msg.m_rtm.rtm_version = RTM_VERSION;
476 477
    switch(operation) {
    case ROUTE_FLUSH:
478
        msg.m_rtm.rtm_type = RTM_DELETE; break;
479
    case ROUTE_ADD:
480 481 482 483
        msg.m_rtm.rtm_type = RTM_ADD; break;
    case ROUTE_MODIFY:
        msg.m_rtm.rtm_type = RTM_CHANGE; break;
    default:
dermiste's avatar
dermiste committed
484
        return -1;
485
    };
486
    msg.m_rtm.rtm_index = ifindex;
487
    msg.m_rtm.rtm_flags = RTF_UP | RTF_PROTO2;
488
    if(plen == 128) msg.m_rtm.rtm_flags |= RTF_HOST;
489
    if(metric == KERNEL_INFINITY) {
490
        msg.m_rtm.rtm_flags |= RTF_BLACKHOLE;
491 492 493 494 495
        if(ifindex_lo < 0) {
            ifindex_lo = if_nametoindex("lo0");
            if(ifindex_lo <= 0)
                return -1;
        }
496
        msg.m_rtm.rtm_index = ifindex_lo;
497
    }
498 499 500 501
    msg.m_rtm.rtm_seq = ++seq;
    msg.m_rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
    if(plen != 128) msg.m_rtm.rtm_addrs |= RTA_NETMASK;

502 503 504 505 506 507 508 509
#define PUSHEUI(ifindex) \
    do { char ifname[IFNAMSIZ]; \
         struct sockaddr_dl *sdl = (struct sockaddr_dl*) data; \
         if(!if_indextoname((ifindex), ifname))  \
             return -1; \
         if(get_sdl(sdl, ifname) < 0)   \
             return -1; \
         data = data + ROUNDUP(sdl->sdl_len); \
510
    } while(0)
511

512 513 514 515 516 517
#define PUSHADDR(src) \
    do { struct sockaddr_in *sin = (struct sockaddr_in*) data; \
         sin->sin_len = sizeof(struct sockaddr_in);  \
         sin->sin_family = AF_INET; \
         memcpy(&sin->sin_addr, (src) + 12, 4); \
         data = data + ROUNDUP(sin->sin_len); \
518
    } while(0)
519

520 521 522 523 524 525
#define PUSHADDR6(src) \
    do { struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) data; \
         sin6->sin6_len = sizeof(struct sockaddr_in6); \
         sin6->sin6_family = AF_INET6; \
         memcpy(&sin6->sin6_addr, (src), 16); \
         if(IN6_IS_ADDR_LINKLOCAL (&sin6->sin6_addr)) \
526
             SET_IN6_LINKLOCAL_IFINDEX (sin6->sin6_addr, ifindex); \
527
         data = data + ROUNDUP(sin6->sin6_len); \
528
    } while(0)
529

dermiste's avatar
dermiste committed
530 531
    /* KAME ipv6 stack does not support IPv4 mapped IPv6, so we have to
     * duplicate the codepath */
532
    if(ipv4) {
533 534

        PUSHADDR(dest);
535
        if(metric == KERNEL_INFINITY) {
536
            PUSHADDR(**local4);
537 538 539 540 541
        } else if(plen == 128 && memcmp(dest+12, gate+12, 4) == 0) {
#if defined(RTF_CLONING)
            msg.m_rtm.rtm_flags |= RTF_CLONING;
#endif
            PUSHEUI(ifindex);
542
        } else {
543
            msg.m_rtm.rtm_flags |= RTF_GATEWAY;
544 545 546
            PUSHADDR(gate);
        }
        if((msg.m_rtm.rtm_addrs & RTA_NETMASK) != 0) {
dermiste's avatar
dermiste committed
547
            struct in6_addr tmp_sin6_addr;
548
            plen2mask(plen, &tmp_sin6_addr);
549
            PUSHADDR((char *)&tmp_sin6_addr);
550
        }
551

552
    } else {
553 554

        PUSHADDR6(dest);
555
        if(metric == KERNEL_INFINITY) {
556 557
            PUSHADDR6(**local6);
        } else {
558
            msg.m_rtm.rtm_flags |= RTF_GATEWAY;
559 560 561 562 563 564
            PUSHADDR6(gate);
        }
        if((msg.m_rtm.rtm_addrs & RTA_NETMASK) != 0) {
            struct in6_addr tmp_sin6_addr;
            plen2mask(plen, &tmp_sin6_addr);
            PUSHADDR6((char*)&tmp_sin6_addr);
565
        }
566

567
    }
568

569
#undef PUSHEUI
570 571 572 573 574
#undef PUSHADDR
#undef PUSHADDR6

    msg.m_rtm.rtm_msglen = data - (char *)&msg;
    rc = write(kernel_socket, (char*)&msg, msg.m_rtm.rtm_msglen);
575
    if(rc < msg.m_rtm.rtm_msglen)
576 577 578 579 580 581 582 583 584
        return -1;

    return 1;
}

static void
print_kernel_route(int add, struct kernel_route *route)
{
    char ifname[IFNAMSIZ];
585 586

    if(!if_indextoname(route->ifindex, ifname))
587
        memcpy(ifname,"unk",4);
588 589

    fprintf(stderr,
590
            "%s kernel route: dest: %s gw: %s metric: %d if: %s(%d) \n",
591 592
            add == RTM_ADD ? "Add" :
            add == RTM_DELETE ? "Delete" : "Change",
593 594 595 596
            format_prefix(route->prefix, route->plen),
            format_address(route->gw),
            route->metric,
            ifname, route->ifindex
597 598 599 600 601 602
            );
}

static int
parse_kernel_route(const struct rt_msghdr *rtm, struct kernel_route *route)
{
603
    struct sockaddr *sa;
604
    char *rta = (char*)rtm + sizeof(struct rt_msghdr);
605
    uint32_t excluded_flags = 0;
606 607 608 609 610 611

    if(ifindex_lo < 0) {
        ifindex_lo = if_nametoindex("lo0");
        if(ifindex_lo <= 0)
            return -1;
    }
612 613

    memset(route, 0, sizeof(*route));
614
    route->metric = 0;
615 616
    route->ifindex = rtm->rtm_index;

617 618 619 620 621 622 623 624
#if defined(RTF_IFSCOPE)
    /* Filter out kernel route on OS X */
    excluded_flags |= RTF_IFSCOPE;
#endif
#if defined(RTF_MULTICAST)
    /* Filter out multicast route on others BSD */
    excluded_flags |= RTF_MULTICAST;
#endif
625 626 627 628
    /* Filter out our own route */
    excluded_flags |= RTF_PROTO2;
    if((rtm->rtm_flags & excluded_flags) != 0)
        return -1;
629

630 631 632 633 634 635
    /* Prefix */
    if(!(rtm->rtm_addrs & RTA_DST))
        return -1;
    sa = (struct sockaddr *)rta;
    rta += ROUNDUP(sa->sa_len);
    if(sa->sa_family == AF_INET6) {
636
        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
637 638
        memcpy(route->prefix, &sin6->sin6_addr, 16);
        if(IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)
639 640
           || IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
            return -1;
641 642
    } else if(sa->sa_family == AF_INET) {
        struct sockaddr_in *sin = (struct sockaddr_in *)sa;
643
#if defined(IN_LINKLOCAL)
644 645 646 647 648 649
        if(IN_LINKLOCAL(ntohl(sin->sin_addr.s_addr)))
            return -1;
#endif
        if(IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
            return -1;
        v4tov6(route->prefix, (unsigned char *)&sin->sin_addr);
650
    } else {
651
        return -1;
652 653
    }

654 655 656 657 658 659
    /* Gateway */
    if(!(rtm->rtm_addrs & RTA_GATEWAY))
        return -1;
    sa = (struct sockaddr *)rta;
    rta += ROUNDUP(sa->sa_len);
    if(sa->sa_family == AF_INET6) {
660
        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
661 662 663 664 665 666 667 668
        memcpy(route->gw, &sin6->sin6_addr, 16);
        if(IN6_IS_ADDR_LINKLOCAL (&sin6->sin6_addr)) {
            route->ifindex = IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr);
            SET_IN6_LINKLOCAL_IFINDEX(sin6->sin6_addr, 0);
        }
    } else if(sa->sa_family == AF_INET) {
        struct sockaddr_in *sin = (struct sockaddr_in *)sa;
        v4tov6(route->gw, (unsigned char *)&sin->sin_addr);
669
    }
670
    if((int)route->ifindex == ifindex_lo)
671 672
        return -1;

673 674 675 676 677
    /* Netmask */
    if((rtm->rtm_addrs & RTA_NETMASK) != 0) {
        sa = (struct sockaddr *)rta;
        rta += ROUNDUP(sa->sa_len);
        if(!v4mapped(route->prefix)) {
678 679
            struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
            route->plen = mask2len((unsigned char*)&sin6->sin6_addr, 16);
680 681 682 683 684 685 686
        } else {
            struct sockaddr_in *sin = (struct sockaddr_in *)sa;
            route->plen = mask2len((unsigned char*)&sin->sin_addr, 4);
        }
    }
    if(v4mapped(route->prefix)) route->plen += 96;
    if(rtm->rtm_flags & RTF_HOST) route->plen = 128;
687

688
    return 0;
689 690
}

691 692
static int
kernel_routes(struct kernel_filter *filter) {
693 694 695 696
    int mib[6];
    char *buf, *p;
    size_t len;
    struct rt_msghdr *rtm;
697
    int rc;
698

699 700 701
    mib[0] = CTL_NET;
    mib[1] = PF_ROUTE;
    mib[2] = 0;
702
    mib[3] = AF_UNSPEC;      /* Address family */
703
    mib[4] = NET_RT_DUMP; /* Dump the kernel routing table */
704
    mib[5] = 0;           /* No flags */
705 706

    rc = sysctl(mib, 6, NULL, &len, NULL, 0);
707
    if(rc < 0) {
708 709 710 711 712 713 714 715 716 717 718
        perror("kernel_routes(len)");
        return -1;
    }

    buf = malloc(len);
    if(!buf) {
        perror("kernel_routes(malloc)");
        return -1;
    }

    rc = sysctl(mib, 6, buf, &len, NULL, 0);
719
    if(rc < 0) {
720 721 722 723
        perror("kernel_routes(dump)");
        goto fail;
    }

724 725
    for(p = buf; p < buf + len; p += rtm->rtm_msglen) {
        struct kernel_route route;
726
        rtm = (struct rt_msghdr*)p;
727
        rc = parse_kernel_route(rtm, &route);
Matthieu Boutier's avatar
Matthieu Boutier committed
728 729
        if(rc < 0)
            continue;
730 731

        if(debug > 2)
732
            print_kernel_route(1, &route);
733

734 735 736
        rc = filter->route(&route, filter->route_closure);
        if(rc < 0)
            break;
737 738 739
    }

    free(buf);
740
    return 0;
741 742 743 744 745 746 747 748

 fail:
    free(buf);
    return -1;

}

static int
749
socket_read(int sock, struct kernel_filter *filter)
750 751 752 753 754 755 756 757 758 759 760 761 762 763
{
    int rc;
    struct {
        struct rt_msghdr rtm;
        struct sockaddr_storage addr[RTAX_MAX];
    } buf;

    rc = read(sock, &buf, sizeof(buf));
    if(rc <= 0) {
        perror("kernel_callback(read)");
        return 0;
    }

    if(buf.rtm.rtm_msglen != rc) {
764
        kdebugf("kernel_callback(length)\n");
765 766 767 768
        return -1;
    }

    if(buf.rtm.rtm_type == RTM_ADD ||
769
       buf.rtm.rtm_type == RTM_DELETE ||
770 771 772 773 774 775 776 777 778
       buf.rtm.rtm_type == RTM_CHANGE) {
        struct kernel_route route;

        if(buf.rtm.rtm_errno)
            return 0;

        rc = parse_kernel_route(&buf.rtm, &route);
        if(rc < 0)
            return 0;
779
        filter->route(&route, filter->route_closure);
780 781 782 783 784 785 786 787 788 789
        if(debug > 2)
            print_kernel_route(1,&route);
        return 1;

    }

    return 0;

}

790 791
static int
kernel_addresses(struct kernel_filter *filter)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
792
{
793
    struct ifaddrs *ifa, *ifap;
794
    int rc;
795 796 797 798 799

    rc = getifaddrs(&ifa);
    if(rc < 0)
        return -1;

800 801 802 803 804
    for(ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
        struct kernel_addr addr;
        addr.ifindex = if_nametoindex(ifap->ifa_name);
        if(!addr.ifindex)
            continue;
805 806 807

        if(ifap->ifa_addr->sa_family == AF_INET6) {
            struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ifap->ifa_addr;
808 809
            memcpy(&addr.addr, &sin6->sin6_addr, 16);
            if(IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
dermiste's avatar
dermiste committed
810
                /* This a perfect example of counter-productive optimisation :
811 812
                   KAME encodes interface index onto bytes 2 and 3, so we have
                   to reset those bytes to 0 before passing them to babeld. */
813
                memset(((char*)&addr.addr) + 2, 0, 2);
814 815
        } else if(ifap->ifa_addr->sa_family == AF_INET) {
            struct sockaddr_in *sin = (struct sockaddr_in*)ifap->ifa_addr;
816
#if defined(IN_LINKLOCAL)
817
            if(IN_LINKLOCAL(htonl(sin->sin_addr.s_addr)))
818
                continue;
819
#endif
820 821 822
            v4tov6((void*)&addr.addr, (void*) &sin->sin_addr);
        } else {
            continue;
823
        }
824
        filter->addr(&addr, filter->addr_closure);
825 826 827
    }

    freeifaddrs(ifa);
828 829 830 831 832 833 834 835 836 837 838 839 840
    return 0;
}

int
kernel_dump(int operation, struct kernel_filter *filter)
{
    switch(operation) {
    case CHANGE_ROUTE: return kernel_routes(filter);
    case CHANGE_ADDR: return kernel_addresses(filter);
    default: break;
    }

    return -1;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
841 842
}

843
int
844
kernel_callback(struct kernel_filter *filter)
845 846 847
{
    if(kernel_socket < 0) kernel_setup_socket(1);

848
    kdebugf("Reading kernel table modification.");
849
    socket_read(kernel_socket, filter);
850 851 852 853 854

    return 0;

}

855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
int
add_rule(int prio, const unsigned char *src_prefix, int src_plen, int table)
{
    errno = ENOSYS;
    return -1;
}

int
flush_rule(int prio, int family)
{
    errno = ENOSYS;
    return -1;
}

int
change_rule(int new_prio, int old_prio,
            const unsigned char *src, int plen, int table)
{
    errno = ENOSYS;
    return -1;
}


878 879 880 881
/* Local Variables:      */
/* c-basic-offset: 4     */
/* indent-tabs-mode: nil */
/* End:                  */