kernel_cygwin.c 20.2 KB
Newer Older
Jondy Zhao's avatar
Jondy Zhao committed
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
/*
Copyright (c) 2007 by Grégoire Henry
Copyright (c) 2008, 2009 by Juliusz Chroboczek
Copyright (c) 2010 by Vincent Gross

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>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if.h>

#include "babeld.h"
#include "neighbour.h"
#include "kernel.h"
#include "util.h"

#include "cyginet.h"

/*
 * Some issues:
 *
 * 1. kernel_route
 *
 *    RTM_BLACKHOLE, gateway will be set as loopback, is it right?
 *
 * 2. IN6_LINKLOCAL_IFINDEX && SET_IN6_LINKLOCAL_IFINDEX
 *
 *    Do both of them work in the Windows?
 *
 * 3. kernel_interface_ipv4
 *
 *    ? interface name, ipv4, ifindex, Oh, they're mass.
 *    
 */

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

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

int export_table = -1, import_table = -1;

int
if_eui64(char *ifname, int ifindex, unsigned char *eui)
{
    struct sockaddr_dl sdl;
    char *tmp = NULL;
    if (get_sdl(&sdl, ifname) < 0) {
        return -1;
    }
    tmp = sdl.sdl_data + sdl.sdl_nlen;
    if (sdl.sdl_alen == 8) {
        memcpy(eui, tmp, 8);
        eui[0] ^= 2;
    } else if (sdl.sdl_alen == 6) {
        memcpy(eui,   tmp,   3);
        eui[3] = 0xFF;
        eui[4] = 0xFE;
        memcpy(eui+5, tmp+3, 3);
    } else {
        return -1;
    }
    return 0;
}

/* 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)
{
    return cyginet_interface_sdl(sdl, ifname);
}

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

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

static int ifindex_lo = -1;
static int if6index_lo = -1;

static int kernel_pipe_handles[2];

int
kernel_setup(int setup)
{
    int rc = 0;
    int forwarding = 1;
    int accept_redirects = 0;
    int reboot = 0;

    /* It enables ip6.forwarding and disable ip6.redirect. 
     *
     * Option 1:
     * 
     * IPV6CTL_FORWARDING (ip6.forwarding) Boolean: enable/disable
     * forward- ing of IPv6 packets.  Also, identify if the node is
     * acting as a router.  Defaults to off.
     *
     * ==> command line:
     *
     *     C:/> ipv6 ifc $If6Index forwards
     * 
     *     repeat this operation for all ipv6 interfaces
     *
     *     List all ipv6 interface by the command:
     *
     *         C:/> netsh interface ipv6 show interface
     *
     * ==> API: EnableRouter/DisableRouter (only for ipv4)
     *
     * ==> MSDN says in the registry
     *
     * HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
     *
     * Value Name: IPEnableRouter
     * Value type: REG_DWORD
     * Value Data: 1
     *
     * A value of 1 enables TCP/IP forwarding for all network
     * connections that are installed and used by this computer.
     * 
     * Refer to: http://support.microsoft.com/kb/315236/en-us
     *
     * Option 2:
     *
     * ICMPV6CTL_REDIRACCEPT
     * 
     * IPV6CTL_SENDREDIRECTS (ip6.redirect) Boolean: enable/disable
     * sending of ICMPv6 redirects in response to unforwardable IPv6
     * packets.  This option is ignored unless the node is routing
     * IPv6 packets, and should normally be enabled on all systems.
     * Defaults to on.
     *
     * ==> MSDN says in the registry
     *
     * HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
     * 
     * EnableICMPRedirect = 0
     *
     * Refer to:
     * 
     *     http://technet.microsoft.com/en-us/library/cc766102(v=ws.10).aspx
     *
     * After change them, need to reboot machine.
     *     
     * Notice:
     * 
     * MSDN says nothing about ipv6, it should use the following key
     * 
     * HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters
     *
     * Maybe later Window VISTA its corresponding APIs are
     * WSAEnumProtocols, WSCUpdateProvider.
     * 
     */
    
    if (setup) {
        int flags;
        if (0 != cyginet_startup())
            return -1;
        if ((rc = cyginet_set_ipv6_forwards(forwarding)) == -1) {
            fprintf(stderr, "Cannot enable IPv6 forwarding.\n");
            return -1;
        }
        old_forwarding = rc;
        reboot = (rc == forwarding) ? reboot : 1;

        if ((rc = cyginet_set_icmp6_redirect_accept(accept_redirects)) == -1) {
            fprintf(stderr, "Cannot disable ICMPv6 redirect.\n");
            if (reboot)
                cyginet_set_ipv6_forwards(old_forwarding);
            return -1;
        }    
        old_accept_redirects = rc;
        reboot = (rc == accept_redirects) ? reboot : 1;
        if (pipe(kernel_pipe_handles) == -1)
            return -1;
        if ((flags = fcntl(kernel_pipe_handles[0], F_GETFL, 0)) < 0)
            goto error;
        if (fcntl(kernel_pipe_handles[0], F_SETFL, flags | O_NONBLOCK) == -1)
            goto error;
    }
    else {
        if (-1 == (rc = cyginet_set_ipv6_forwards(old_forwarding)))
            return -1;
        reboot = (rc == forwarding) ? reboot : 1;
        if (-1 ==
            (rc = cyginet_set_icmp6_redirect_accept(old_accept_redirects)))
            return -1;
        reboot = (rc == accept_redirects) ? reboot : 1;
        close(kernel_pipe_handles[0]);
        close(kernel_pipe_handles[1]);     
        cyginet_cleanup();
    }

    if (reboot)
        fprintf(stderr,           
                "%s IPv6 forwarding and %s ICMPv6 redirect successfully.\n"
                "REBOOT NOW, so that these changes take effect.\n\n",
                forwarding ? "Enable" : "Disable",
                accept_redirects ? "enable" : "disable"
                );
    return 1;

 error: {
        if (reboot) {
            cyginet_set_ipv6_forwards(old_forwarding);
            cyginet_set_icmp6_redirect_accept(old_accept_redirects);
        }
        return -1;
    }
}

int
kernel_setup_socket(int setup)
{
    /* We use a pipe to notify route changed */
    if(setup) {
        if(kernel_socket < 0) {
            kernel_socket = kernel_pipe_handles[0];
            if (-1 ==
                cyginet_start_monitor_route_changes(kernel_pipe_handles[1])) {
                perror("start_monitor_route_changes");
                kernel_socket = -1;
                return -1;
            }
        }
    } else {
        cyginet_stop_monitor_route_changes();
        kernel_socket = -1;
    }
    return 1;
}

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

int
kernel_interface_operational(const char *ifname, int ifindex)
{
    int rc;
    int flags = link_detect ? (IFF_UP | IFF_RUNNING) : IFF_UP;
    
    rc = cyginet_interface_operational(ifname, ifindex);
    if (rc < 0)
        return -1;
    return ((rc & flags) == flags);
}

int
kernel_interface_ipv4(const char *ifname, int ifindex, unsigned char *addr_r)
{
    return cyginet_interface_ipv4(ifname, ifindex, addr_r);
}

int
kernel_interface_mtu(const char *ifname, int ifindex)
{
    return cyginet_interface_mtu(ifname, ifindex);
}

int
kernel_interface_wireless(const char *ifname, int ifindex)
{
    return cyginet_interface_wireless(ifname, ifindex);
}

int
kernel_interface_channel(const char *ifname, int ifindex)
{
    errno = ENOSYS;
    return -1;
}

/* 
 * RTF_REJECT 
 * 
 *   Instead of forwarding a packet like a normal route, routes with
 *   RTF_REJECT cause packets to be dropped and unreachable messages
 *   to be sent to the packet originators. This flag is only valid on
 *   routes pointing at the loopback interface.
 * 
 * RTF_BLACKHOLE 
 * 
 *   Like the RTF_REJECT flag, routes with RTF_BLACKHOLE cause packets
 *   to be dropped, but unreachable messages are not sent. This flag
 *   is only valid on routes pointing at the loopback interface.
 * 
 * Nullrouting on Windows
 * 
 *   Windows XP/Vista/7 does not support reject or blackhole arguments
 *   via route, thus an unused IP address (e.g. 192.168.0.205) must be
 *   used as the target gateway.
 *
 * RTF_GATEWAY: destination is a gateway
 *
 * RTF_HOST: host entry (net otherwise)
 *
 *   It means all the netmask bits are 1. 
 *
 * RTF_CLONING: generate new routes on use
 *
 *   No corresponding function in the Windows.
 *   
 */
int
kernel_route(int operation, const unsigned char *dest, unsigned short plen,
             const unsigned char *gate, int ifindex, unsigned int metric,
             const unsigned char *newgate, int newifindex,
             unsigned int newmetric)
{
    int rc, ipv4;
    struct sockaddr destination, gateway;
    int route_ifindex;
    int prefix_len;

    struct in6_addr local6 = {{IN6ADDR_LOOPBACK_INIT}};
    char local4[1][1][16] =
        {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x01 }}};

    /* Check that the protocol family is consistent. */
    if(plen >= 96 && v4mapped(dest)) {
        if(!v4mapped(gate)) {
            errno = EINVAL;
            return -1;
        }
        ipv4 = 1;
    } else {
        if(v4mapped(gate)) {
            errno = EINVAL;
            return -1;
        }
        ipv4 = 0;
    }

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

    if(operation == ROUTE_MODIFY) {
        /* Do not use ROUTE_MODIFY when changing to a neighbour.
           It is the only way to remove the "gateway" flag. */
        if(ipv4 && plen == 128 && memcmp(dest, newgate, 16) == 0) {
            kernel_route(ROUTE_FLUSH, dest, plen,
                         gate, ifindex, metric,
                         NULL, 0, 0);
            return kernel_route(ROUTE_ADD, dest, plen,
                                newgate, newifindex, newmetric,
                                NULL, 0, 0);
        } else {
            metric = newmetric;
            gate = newgate;
            ifindex = newifindex;
        }
    }

    kdebugf("kernel_route: %s %s/%d metric %d dev %d nexthop %s\n",
            operation == ROUTE_ADD ? "add" :
            operation == ROUTE_FLUSH ? "flush" : "change",
            format_address(dest), plen, metric, ifindex,
            format_address(gate));

    if(kernel_socket < 0) kernel_setup_socket(1);

    memset(&destination, 0, sizeof(destination));
    memset(&gateway, 0, sizeof(gateway));

    route_ifindex = ifindex;
    prefix_len = ipv4 ? plen - 96 : plen;

    if(metric == KERNEL_INFINITY) {
        /* RTF_BLACKHOLE; */
        /* ==> Set gateway to an unused ip address in the Windows */
        if(ipv4) {
            if (ifindex_lo < 0) {
                ifindex_lo = cyginet_loopback_index(AF_INET);
                if(ifindex_lo <= 0)
                    return -1;
            }
            route_ifindex = ifindex_lo;
        }
        else {
            if (if6index_lo < 0) {
                if6index_lo = cyginet_loopback_index(AF_INET6);
                if(if6index_lo <= 0)
                    return -1;
            }
            route_ifindex = if6index_lo;
        }
    }

#define PUSHADDR(dst, src)                                              \
    do { struct sockaddr_in *sin = (struct sockaddr_in*)(&(dst));       \
        sin->sin_family = AF_INET;                                      \
        memcpy(&sin->sin_addr, (src) + 12, 4);                          \
    } while (0)

#define PUSHADDR6(dst, src)                                             \
    do { struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)(&(dst));    \
        sin6->sin6_family = AF_INET6;                                   \
        memcpy(&sin6->sin6_addr, (src), 16);                            \
        if(IN6_IS_ADDR_LINKLOCAL (&sin6->sin6_addr))                    \
            SET_IN6_LINKLOCAL_IFINDEX (sin6->sin6_addr, ifindex);       \
    } while (0)
    
    if(ipv4) {

        PUSHADDR(destination, dest);
        if (metric == KERNEL_INFINITY)
            PUSHADDR(gateway, **local4);
        else 
            PUSHADDR(gateway, gate);

    } else {
        PUSHADDR6(destination, dest);
        if (metric == KERNEL_INFINITY)
            PUSHADDR6(gateway, &local6);
        else 
            PUSHADDR6(gateway, gate);
    }
#undef PUSHADDR
#undef PUSHADDR6

    switch(operation) {
    case ROUTE_FLUSH:
        rc = cyginet_delete_route_entry(&destination,
                                        prefix_len,
                                        &gateway,
                                        route_ifindex,
                                        metric
                                        );
        break;
    case ROUTE_ADD:
        rc = cyginet_add_route_entry(&destination,
                                     prefix_len,
                                     &gateway,
                                     route_ifindex,
                                     metric
                                     );
        break;
    case ROUTE_MODIFY:
        rc = cyginet_update_route_entry(&destination,
                                        prefix_len,
                                        &gateway,
                                        route_ifindex,
                                        metric
                                        );
        break;
    default:
        return -1;
    };

    /* Monitor thread will write data to kernel pipe when any change
       in the route table is happened. Here it's babeld itself to
       change the route table, so kernel pipe need to be clean. */
    int ch;
    while (read(kernel_pipe_handles[0], &ch, 1) > 0);
    return rc;
}

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

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

    fprintf(stderr,
            "%s kernel route: dest: %s gw: %s metric: %d if: %s(%d) \n",
            add == RTM_ADD ? "Add" :
            add == RTM_DELETE ? "Delete" : "Change",
            format_prefix(route->prefix, route->plen),
            format_address(route->gw),
            route->metric,
            ifname, route->ifindex
            );
}

static int
parse_kernel_route(struct kernel_route *route)
{
    struct sockaddr *sa;

    if(ifindex_lo < 0) {
        ifindex_lo = cyginet_loopback_index(AF_INET);
        if(ifindex_lo <= 0)
            return -1;
    }

    if(if6index_lo < 0) {
        if6index_lo = cyginet_loopback_index(AF_INET6);
        if(if6index_lo <= 0)
            return -1;
    }

    sa = (struct sockaddr *)route -> prefix;
    if(sa->sa_family == AF_INET6) {
        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
        if(IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)
             || IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
           return -1;
    } else if(sa->sa_family == AF_INET) {
        struct sockaddr_in *sin = (struct sockaddr_in *)sa;
#if defined(IN_LINKLOCAL)
        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);
    } else {
        return -1;
    }

    /* Gateway */
    sa = (struct sockaddr *)route -> gw;
    if(sa->sa_family == AF_INET6) {
        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
        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);
    }

    if((sa->sa_family == AF_INET6) && (route->ifindex == if6index_lo))    
        return -1;
    if((sa->sa_family == AF_INET) && (route->ifindex == ifindex_lo))
        return -1;

    /* Netmask */
    if(v4mapped(route->prefix)) route->plen += 96;

    return 0;
}

int
kernel_routes(struct kernel_route *routes, int maxroutes)
{
    int rc, i;
    int count;
    struct kernel_route * proute = routes;
    struct kernel_route * pdest = proute;
    
    memset(routes, 0, sizeof(struct kernel_route) * maxroutes);

    rc = cyginet_dump_route_table(routes, maxroutes);
    if (rc < 0)
        return -1;

    count = 0;
    for (i = 0; i < rc; i++, proute++) {

        if (parse_kernel_route(proute) != 0)
            continue;
        
        if (proute != pdest)
            memcpy(pdest, proute, sizeof(struct kernel_route));

        if(debug > 2)
            print_kernel_route(RTM_ADD, pdest);

        pdest++;
    }
    return (pdest - routes);
}

int
kernel_addresses(char *ifname, int ifindex, int ll,
                 struct kernel_route *routes, int maxroutes)
{
    struct ifaddrs *ifa, *ifap;
    int rc, i;
    /* Two issues:
     * 1. ifname maybe includes a suffix number in the cygwin
     * 2. Ipv4 IfIndex maybe need to be changed as Ipv6IfIndex
     */
    rc = getifaddrs(&ifa);
    if(rc < 0)
        return -1;

    ifap = ifa;
    i = 0;

    while(ifap && i < maxroutes) {
        if((ifname != NULL && strcmp(ifap->ifa_name, ifname) != 0))
            goto next;
        if(ifap->ifa_addr->sa_family == AF_INET6) {
            struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ifap->ifa_addr;
            if(!!ll != !!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
                goto next;
            memcpy(routes[i].prefix, &sin6->sin6_addr, 16);
            if(ll)
                /* This a perfect example of counter-productive optimisation :
                   KAME encodes interface index onto bytes 2 and 3, so we have to
                   reset those bytes to 0 before passing them to babeld. */
                memset(routes[i].prefix + 2, 0, 2);
            routes[i].plen = 128;
            routes[i].metric = 0;
            routes[i].ifindex = ifindex;
            routes[i].proto = RTPROT_BABEL_LOCAL;
            memset(routes[i].gw, 0, 16);
            i++;
        } else if(ifap->ifa_addr->sa_family == AF_INET) {
            struct sockaddr_in *sin = (struct sockaddr_in*)ifap->ifa_addr;
            if(ll)
                goto next;
#if defined(IN_LINKLOCAL)
            if(IN_LINKLOCAL(htonl(sin->sin_addr.s_addr)))
                goto next;
#endif
            memcpy(routes[i].prefix, v4prefix, 12);
            memcpy(routes[i].prefix + 12, &sin->sin_addr, 4);
            routes[i].plen = 128;
            routes[i].metric = 0;
            routes[i].ifindex = ifindex;
            routes[i].proto = RTPROT_BABEL_LOCAL;
            memset(routes[i].gw, 0, 16);
            i++;
        }
 next:
        ifap = ifap->ifa_next;
    }

    freeifaddrs(ifa);
    return i;
}

int
kernel_callback(int (*fn)(int, void*), void *closure)
{
    if (kernel_socket < 0) kernel_setup_socket(1);

    /* In the Windows, we can't get the exact changed route, but the
       route table is really changed. */
    kdebugf("Kernel table changed.");
    return fn(~0, closure);
}

/* Local Variables:      */
/* c-basic-offset: 4     */
/* indent-tabs-mode: nil */
/* End:                  */