Blame view

interface.c 16.1 KB
Juliusz Chroboczek 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
/*
Copyright (c) 2007, 2008 by Juliusz Chroboczek

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 <string.h>
#include <stdio.h>
#include <stdlib.h>
Juliusz Chroboczek committed
26
#include <errno.h>
Juliusz Chroboczek committed
27
#include <assert.h>
Juliusz Chroboczek committed
28
#include <sys/time.h>
Juliusz Chroboczek committed
29 30 31 32 33
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
Juliusz Chroboczek committed
34

Juliusz Chroboczek committed
35
#include "babeld.h"
Juliusz Chroboczek committed
36 37
#include "util.h"
#include "kernel.h"
Juliusz Chroboczek committed
38
#include "interface.h"
Juliusz Chroboczek committed
39 40
#include "neighbour.h"
#include "message.h"
Juliusz Chroboczek committed
41
#include "route.h"
Juliusz Chroboczek committed
42
#include "configuration.h"
Juliusz Chroboczek committed
43
#include "local.h"
Matthieu Boutier committed
44
#include "xroute.h"
Juliusz Chroboczek committed
45

Matthieu Boutier committed
46
struct interface *interfaces = NULL;
Juliusz Chroboczek committed
47

Matthieu Boutier committed
48 49
static struct interface *
last_interface(void)
Juliusz Chroboczek committed
50
{
Matthieu Boutier committed
51
    struct interface *ifp = interfaces;
Juliusz Chroboczek committed
52

Matthieu Boutier committed
53
    if(!ifp)
Juliusz Chroboczek committed
54 55
        return NULL;

Matthieu Boutier committed
56 57
    while(ifp->next)
        ifp = ifp->next;
Juliusz Chroboczek committed
58

Matthieu Boutier committed
59
    return ifp;
Juliusz Chroboczek committed
60
}
Juliusz Chroboczek committed
61

Matthieu Boutier committed
62 63
struct interface *
add_interface(char *ifname, struct interface_conf *if_conf)
Juliusz Chroboczek committed
64
{
Matthieu Boutier committed
65
    struct interface *ifp;
Juliusz Chroboczek committed
66

Matthieu Boutier committed
67 68
    FOR_ALL_INTERFACES(ifp) {
        if(strcmp(ifp->name, ifname) == 0) {
Juliusz Chroboczek committed
69 70 71 72
            if(if_conf)
                fprintf(stderr,
                        "Warning: attempting to add existing interface, "
                        "new configuration ignored.\n");
Matthieu Boutier committed
73
            return ifp;
Juliusz Chroboczek committed
74
        }
Juliusz Chroboczek committed
75 76
    }

Juliusz Chroboczek committed
77
    ifp = calloc(1, sizeof(struct interface));
Matthieu Boutier committed
78
    if(ifp == NULL)
Juliusz Chroboczek committed
79
        return NULL;
Juliusz Chroboczek committed
80

Matthieu Boutier committed
81
    strncpy(ifp->name, ifname, IF_NAMESIZE);
Juliusz Chroboczek committed
82
    ifp->conf = if_conf ? if_conf : default_interface_conf;
Matthieu Boutier committed
83 84 85
    ifp->bucket_time = now.tv_sec;
    ifp->bucket = BUCKET_TOKENS_MAX;
    ifp->hello_seqno = (random() & 0xFFFF);
Juliusz Chroboczek committed
86

Matthieu Boutier committed
87 88
    if(interfaces == NULL)
        interfaces = ifp;
Juliusz Chroboczek committed
89
    else
Matthieu Boutier committed
90
        last_interface()->next = ifp;
Juliusz Chroboczek committed
91

Juliusz Chroboczek committed
92 93
    local_notify_interface(ifp, LOCAL_ADD);

Matthieu Boutier committed
94
    return ifp;
Juliusz Chroboczek committed
95 96
}

Juliusz Chroboczek committed
97
int
Juliusz Chroboczek committed
98 99 100 101 102 103 104 105 106 107 108 109 110
flush_interface(char *ifname)
{
    struct interface *ifp, *prev;

    prev = NULL;
    ifp = interfaces;
    while(ifp) {
        if(strcmp(ifp->name, ifname) == 0)
            break;
        prev = ifp;
        ifp = ifp->next;
    }

Juliusz Chroboczek committed
111 112 113
    if(ifp == NULL)
        return 0;

Juliusz Chroboczek committed
114 115 116 117 118 119
    interface_up(ifp, 0);
    if(prev)
        prev->next = ifp->next;
    else
        interfaces = ifp->next;

Juliusz Chroboczek committed
120 121
    local_notify_interface(ifp, LOCAL_FLUSH);

Juliusz Chroboczek committed
122 123
    if(ifp->conf != NULL && ifp->conf != default_interface_conf)
        flush_ifconf(ifp->conf);
Juliusz Chroboczek committed
124 125 126

    local_notify_interface(ifp, LOCAL_FLUSH);

Juliusz Chroboczek committed
127
    free(ifp);
Juliusz Chroboczek committed
128 129

    return 1;
Juliusz Chroboczek committed
130 131
}

Juliusz Chroboczek committed
132 133
/* This should be no more than half the hello interval, so that hellos
   aren't sent late.  The result is in milliseconds. */
Juliusz Chroboczek committed
134
unsigned
Matthieu Boutier committed
135
jitter(struct interface *ifp, int urgent)
Juliusz Chroboczek committed
136
{
Matthieu Boutier committed
137
    unsigned interval = ifp->hello_interval;
Juliusz Chroboczek committed
138 139 140
    if(urgent)
        interval = MIN(interval, 100);
    else
Juliusz Chroboczek committed
141 142
        interval = MIN(interval, 4000);
    return roughly(interval) / 4;
Juliusz Chroboczek committed
143 144
}

Juliusz Chroboczek committed
145
unsigned
Matthieu Boutier committed
146
update_jitter(struct interface *ifp, int urgent)
Juliusz Chroboczek committed
147
{
Matthieu Boutier committed
148
    unsigned interval = ifp->hello_interval;
Juliusz Chroboczek committed
149 150
    if(urgent)
        interval = MIN(interval, 100);
Juliusz Chroboczek committed
151 152
    else
        interval = MIN(interval, 4000);
Juliusz Chroboczek committed
153
    return roughly(interval);
Juliusz Chroboczek committed
154
}
Juliusz Chroboczek committed
155

Juliusz Chroboczek committed
156
void
Juliusz Chroboczek committed
157
set_timeout(struct timeval *timeout, int msecs)
Juliusz Chroboczek committed
158
{
Juliusz Chroboczek committed
159
    timeval_add_msec(timeout, &now, roughly(msecs));
Juliusz Chroboczek committed
160 161
}

Juliusz Chroboczek committed
162
static int
Matthieu Boutier committed
163
check_interface_ipv4(struct interface *ifp)
Juliusz Chroboczek committed
164 165 166 167
{
    unsigned char ipv4[4];
    int rc;

Matthieu Boutier committed
168 169
    if(ifp->ifindex > 0)
        rc = kernel_interface_ipv4(ifp->name, ifp->ifindex, ipv4);
Juliusz Chroboczek committed
170 171 172 173
    else
        rc = 0;

    if(rc > 0) {
Matthieu Boutier committed
174 175 176 177 178 179 180
        if(!ifp->ipv4 || memcmp(ipv4, ifp->ipv4, 4) != 0) {
            debugf("Noticed IPv4 change for %s.\n", ifp->name);
            flush_interface_routes(ifp, 0);
            if(!ifp->ipv4)
                ifp->ipv4 = malloc(4);
            if(ifp->ipv4)
                memcpy(ifp->ipv4, ipv4, 4);
Juliusz Chroboczek committed
181
            local_notify_interface(ifp, LOCAL_CHANGE);
Juliusz Chroboczek committed
182 183 184
            return 1;
        }
    } else {
Matthieu Boutier committed
185 186 187 188 189
        if(ifp->ipv4) {
            debugf("Noticed IPv4 change for %s.\n", ifp->name);
            flush_interface_routes(ifp, 0);
            free(ifp->ipv4);
            ifp->ipv4 = NULL;
Juliusz Chroboczek committed
190
            local_notify_interface(ifp, LOCAL_CHANGE);
Juliusz Chroboczek committed
191 192 193 194 195 196
            return 1;
        }
    }
    return 0;
}

Matthieu Boutier committed
197
static int
Matthieu Boutier committed
198
check_interface_channel(struct interface *ifp)
Juliusz Chroboczek committed
199
{
Matthieu Boutier committed
200
    int channel = IF_CONF(ifp, channel);
Juliusz Chroboczek committed
201
    int rc = 1;
Juliusz Chroboczek committed
202

Matthieu Boutier committed
203
    if(channel == IF_CHANNEL_UNKNOWN) {
Juliusz Chroboczek committed
204 205 206 207 208 209 210 211
        /* IF_WIRELESS merely means that we know for sure that the
           interface is wireless, so check unconditionally. */
        channel = kernel_interface_channel(ifp->name, ifp->ifindex);
        if(channel < 0) {
            if((ifp->flags & IF_WIRELESS))
                rc = -1;
            channel = (ifp->flags & IF_WIRELESS) ?
                IF_CHANNEL_INTERFERING : IF_CHANNEL_NONINTERFERING;
Juliusz Chroboczek committed
212 213 214
        }
    }

Matthieu Boutier committed
215 216
    if(ifp->channel != channel) {
        ifp->channel = channel;
Juliusz Chroboczek committed
217
        return rc;
Juliusz Chroboczek committed
218 219 220 221
    }
    return 0;
}

Matthieu Boutier committed
222 223 224 225 226
static int
check_link_local_addresses(struct interface *ifp)
{
    struct kernel_route ll[32];
    int rc, i;
Juliusz Chroboczek committed
227

Matthieu Boutier committed
228
    rc = kernel_addresses(ifp->ifindex, 1, ll, 32);
Juliusz Chroboczek committed
229 230 231 232 233 234 235 236 237 238 239 240
    if(rc <= 0) {
        if(rc < 0)
            perror("kernel_addresses(link local)");
        else
            fprintf(stderr, "Interface %s has no link-local address.\n",
                    ifp->name);
        if(ifp->ll) {
            free(ifp->ll);
            ifp->numll = 0;
            ifp->ll = NULL;
        }
        local_notify_interface(ifp, LOCAL_CHANGE);
Matthieu Boutier committed
241 242 243 244 245
        /* Most probably DAD hasn't finished yet.  Reschedule us
           real soon. */
        schedule_interfaces_check(2000, 0);
        return -1;
    } else {
Juliusz Chroboczek committed
246 247 248 249 250 251 252 253 254
        int changed;
        if(rc == ifp->numll) {
            changed = 0;
            for(i = 0; i < rc; i++) {
                if(memcmp(ifp->ll[i], ll[i].prefix, 16) != 0) {
                    changed = 1;
                    break;
                }
            }
Matthieu Boutier committed
255
        } else {
Juliusz Chroboczek committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
            changed = 1;
        }

        if(changed) {
            free(ifp->ll);
            ifp->numll = 0;
            ifp->ll = malloc(16 * rc);
            if(ifp->ll == NULL) {
                perror("malloc(ll)");
            } else {
                for(i = 0; i < rc; i++)
                    memcpy(ifp->ll[i], ll[i].prefix, 16);
                ifp->numll = rc;
            }
            local_notify_interface(ifp, LOCAL_CHANGE);
Matthieu Boutier committed
271 272 273 274 275 276
        }
    }

    return 0;
}

Juliusz Chroboczek committed
277
int
Matthieu Boutier committed
278
interface_up(struct interface *ifp, int up)
Juliusz Chroboczek committed
279
{
Juliusz Chroboczek committed
280
    int mtu, rc, type;
Juliusz Chroboczek committed
281
    struct ipv6_mreq mreq;
Juliusz Chroboczek committed
282

Matthieu Boutier committed
283
    if((!!up) == if_up(ifp))
Juliusz Chroboczek committed
284 285
        return 0;

Juliusz Chroboczek committed
286
    if(up)
Matthieu Boutier committed
287
        ifp->flags |= IF_UP;
Juliusz Chroboczek committed
288
    else
Matthieu Boutier committed
289
        ifp->flags &= ~IF_UP;
Juliusz Chroboczek committed
290

Juliusz Chroboczek committed
291
    if(up) {
Matthieu Boutier committed
292
        if(ifp->ifindex <= 0) {
Juliusz Chroboczek committed
293
            fprintf(stderr,
Matthieu Boutier committed
294
                    "Upping unknown interface %s.\n", ifp->name);
Juliusz Chroboczek committed
295
            goto fail;
Juliusz Chroboczek committed
296 297
        }

Matthieu Boutier committed
298
        rc = kernel_setup_interface(1, ifp->name, ifp->ifindex);
Juliusz Chroboczek committed
299 300
        if(rc < 0) {
            fprintf(stderr, "kernel_setup_interface(%s, %d) failed.\n",
Matthieu Boutier committed
301
                    ifp->name, ifp->ifindex);
Juliusz Chroboczek committed
302
            goto fail;
Juliusz Chroboczek committed
303 304
        }

Matthieu Boutier committed
305
        mtu = kernel_interface_mtu(ifp->name, ifp->ifindex);
Juliusz Chroboczek committed
306 307
        if(mtu < 0) {
            fprintf(stderr, "Warning: couldn't get MTU of interface %s (%d).\n",
Matthieu Boutier committed
308
                    ifp->name, ifp->ifindex);
Juliusz Chroboczek committed
309 310
            mtu = 1280;
        }
Juliusz Chroboczek committed
311 312 313 314 315 316 317

        /* We need to be able to fit at least two messages into a packet,
           so MTUs below 116 require lower layer fragmentation. */
        /* In IPv6, the minimum MTU is 1280, and every host must be able
           to reassemble up to 1500 bytes, but I'd rather not rely on this. */
        if(mtu < 128) {
            fprintf(stderr, "Suspiciously low MTU %d on interface %s (%d).\n",
Matthieu Boutier committed
318
                    mtu, ifp->name, ifp->ifindex);
Juliusz Chroboczek committed
319 320 321
            mtu = 128;
        }

Matthieu Boutier committed
322 323
        if(ifp->sendbuf)
            free(ifp->sendbuf);
Juliusz Chroboczek committed
324 325

        /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
Matthieu Boutier committed
326 327 328
        ifp->bufsize = mtu - sizeof(packet_header) - 60;
        ifp->sendbuf = malloc(ifp->bufsize);
        if(ifp->sendbuf == NULL) {
Juliusz Chroboczek committed
329
            fprintf(stderr, "Couldn't allocate sendbuf.\n");
Matthieu Boutier committed
330
            ifp->bufsize = 0;
Juliusz Chroboczek committed
331
            goto fail;
Juliusz Chroboczek committed
332
        }
Juliusz Chroboczek committed
333

Juliusz Chroboczek committed
334 335 336 337 338
        rc = resize_receive_buffer(mtu);
        if(rc < 0)
            fprintf(stderr, "Warning: couldn't resize "
                    "receive buffer for interface %s (%d) (%d bytes).\n",
                    ifp->name, ifp->ifindex, mtu);
Juliusz Chroboczek committed
339

Juliusz Chroboczek committed
340 341 342 343
        type = IF_CONF(ifp, type);
        if(type == IF_TYPE_DEFAULT) {
            if(all_wireless) {
                type = IF_TYPE_WIRELESS;
Juliusz Chroboczek committed
344
            } else {
Juliusz Chroboczek committed
345 346 347 348 349 350 351 352 353
                rc = kernel_interface_wireless(ifp->name, ifp->ifindex);
                if(rc < 0) {
                    fprintf(stderr,
                            "Warning: couldn't determine whether %s (%d) "
                            "is a wireless interface.\n",
                            ifp->name, ifp->ifindex);
                } else if(rc) {
                    type = IF_TYPE_WIRELESS;
                }
Juliusz Chroboczek committed
354 355 356
            }
        }

Juliusz Chroboczek committed
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
        /* Type is CONFIG_TYPE_AUTO if interface is not known to be
           wireless, so provide sane defaults for that case. */

        if(type == IF_TYPE_WIRELESS)
            ifp->flags |= IF_WIRELESS;
        else
            ifp->flags &= ~IF_WIRELESS;

        ifp->cost = IF_CONF(ifp, cost);
        if(ifp->cost <= 0)
            ifp->cost = type == IF_TYPE_WIRELESS ? 256 : 96;

        if(IF_CONF(ifp, split_horizon) == CONFIG_YES)
            ifp->flags |= IF_SPLIT_HORIZON;
        else if(IF_CONF(ifp, split_horizon) == CONFIG_NO)
            ifp->flags &= ~IF_SPLIT_HORIZON;
        else if(type == IF_TYPE_WIRED)
            ifp->flags |= IF_SPLIT_HORIZON;
        else
            ifp->flags &= ~IF_SPLIT_HORIZON;

        if(IF_CONF(ifp, lq) == CONFIG_YES)
            ifp->flags |= IF_LQ;
        else if(IF_CONF(ifp, lq) == CONFIG_NO)
            ifp->flags &= ~IF_LQ;
        else if(type == IF_TYPE_WIRELESS)
            ifp->flags |= IF_LQ;
        else
            ifp->flags &= ~IF_LQ;
Juliusz Chroboczek committed
386

Matthieu Boutier committed
387 388
        if(IF_CONF(ifp, faraway) == CONFIG_YES)
            ifp->flags |= IF_FARAWAY;
Juliusz Chroboczek committed
389

Juliusz Chroboczek committed
390 391
        if(IF_CONF(ifp, hello_interval) > 0)
            ifp->hello_interval = IF_CONF(ifp, hello_interval);
Juliusz Chroboczek committed
392
        else if(type == IF_TYPE_WIRELESS)
Juliusz Chroboczek committed
393
            ifp->hello_interval = default_wireless_hello_interval;
Juliusz Chroboczek committed
394 395
        else
            ifp->hello_interval = default_wired_hello_interval;
Juliusz Chroboczek committed
396

Matthieu Boutier committed
397 398 399
        ifp->update_interval =
            IF_CONF(ifp, update_interval) > 0 ?
            IF_CONF(ifp, update_interval) :
Matthieu Boutier committed
400
            ifp->hello_interval * 4;
Juliusz Chroboczek committed
401

Juliusz Chroboczek committed
402 403 404
        ifp->rtt_decay =
            IF_CONF(ifp, rtt_decay) > 0 ?
            IF_CONF(ifp, rtt_decay) : 42;
Baptiste Jonglez committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

        ifp->rtt_min =
            IF_CONF(ifp, rtt_min) > 0 ?
            IF_CONF(ifp, rtt_min) : 10000;
        ifp->rtt_max =
            IF_CONF(ifp, rtt_max) > 0 ?
            IF_CONF(ifp, rtt_max) : 120000;
        if(ifp->rtt_max <= ifp->rtt_min) {
            fprintf(stderr,
                    "Uh, rtt-max is less than or equal to rtt-min (%d <= %d). "
                    "Setting it to %d.\n", ifp->rtt_max, ifp->rtt_min,
                    ifp->rtt_min + 10000);
            ifp->rtt_max = ifp->rtt_min + 10000;
        }
        ifp->max_rtt_penalty = IF_CONF(ifp, max_rtt_penalty);
Juliusz Chroboczek committed
420 421
        if(ifp->max_rtt_penalty == 0 && type == IF_TYPE_TUNNEL)
            ifp->max_rtt_penalty = 96;
Baptiste Jonglez committed
422

Juliusz Chroboczek committed
423 424 425 426 427
        if(IF_CONF(ifp, enable_timestamps) == CONFIG_YES)
            ifp->flags |= IF_TIMESTAMPS;
        else if(IF_CONF(ifp, enable_timestamps) == CONFIG_NO)
            ifp->flags &= ~IF_TIMESTAMPS;
        else if(type == IF_TYPE_TUNNEL)
Juliusz Chroboczek committed
428
            ifp->flags |= IF_TIMESTAMPS;
Juliusz Chroboczek committed
429 430 431 432 433 434 435
        else
            ifp->flags &= ~IF_TIMESTAMPS;
        if(ifp->max_rtt_penalty > 0 && !(ifp->flags & IF_TIMESTAMPS))
            fprintf(stderr,
                    "Warning: max_rtt_penalty is set "
                    "but timestamps are disabled on interface %s.\n",
                    ifp->name);
Juliusz Chroboczek committed
436

Juliusz Chroboczek committed
437 438 439 440 441
        if(IF_CONF(ifp, rfc6126) == CONFIG_YES)
            ifp->flags |= IF_RFC6126;
        else
            ifp->flags &= ~IF_RFC6126;

Matthieu Boutier committed
442 443 444 445
        rc = check_link_local_addresses(ifp);
        if(rc < 0) {
            goto fail;
        }
Juliusz Chroboczek committed
446 447 448
        memset(&mreq, 0, sizeof(mreq));
        memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
        mreq.ipv6mr_interface = ifp->ifindex;
Juliusz Chroboczek committed
449 450 451 452 453 454 455
        rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
                        (char*)&mreq, sizeof(mreq));
        if(rc < 0) {
            perror("setsockopt(IPV6_JOIN_GROUP)");
            goto fail;
        }

Juliusz Chroboczek committed
456 457 458 459 460
        rc = check_interface_channel(ifp);
        if(rc < 0)
            fprintf(stderr,
                    "Warning: couldn't determine channel of interface %s.\n",
                    ifp->name);
Juliusz Chroboczek committed
461 462 463
        update_interface_metric(ifp);
        rc = check_interface_ipv4(ifp);

Juliusz Chroboczek committed
464
        debugf("Upped interface %s (cost=%d, channel=%d%s).\n",
Juliusz Chroboczek committed
465 466 467 468 469
               ifp->name,
               ifp->cost,
               ifp->channel,
               ifp->ipv4 ? ", IPv4" : "");

Matthieu Boutier committed
470 471 472
        set_timeout(&ifp->hello_timeout, ifp->hello_interval);
        set_timeout(&ifp->update_timeout, ifp->update_interval);
        send_hello(ifp);
Juliusz Chroboczek committed
473
        if(rc > 0)
Matthieu Boutier committed
474
            send_update(ifp, 0, NULL, 0, NULL, 0);
Juliusz Chroboczek committed
475
    } else {
Matthieu Boutier committed
476 477 478 479 480 481 482 483 484 485 486
        flush_interface_routes(ifp, 0);
        ifp->buffered = 0;
        ifp->bufsize = 0;
        free(ifp->sendbuf);
        ifp->num_buffered_updates = 0;
        ifp->update_bufsize = 0;
        if(ifp->buffered_updates)
            free(ifp->buffered_updates);
        ifp->buffered_updates = NULL;
        ifp->sendbuf = NULL;
        if(ifp->ifindex > 0) {
Juliusz Chroboczek committed
487 488
            memset(&mreq, 0, sizeof(mreq));
            memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
Matthieu Boutier committed
489
            mreq.ipv6mr_interface = ifp->ifindex;
Juliusz Chroboczek committed
490 491
            rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
                            (char*)&mreq, sizeof(mreq));
Juliusz Chroboczek committed
492
            if(rc < 0)
Juliusz Chroboczek committed
493
                perror("setsockopt(IPV6_LEAVE_GROUP)");
Matthieu Boutier committed
494
            kernel_setup_interface(0, ifp->name, ifp->ifindex);
Juliusz Chroboczek committed
495
        }
Matthieu Boutier committed
496 497 498 499
        if(ifp->ll)
            free(ifp->ll);
        ifp->ll = NULL;
        ifp->numll = 0;
Juliusz Chroboczek committed
500 501
    }

Juliusz Chroboczek committed
502 503
    local_notify_interface(ifp, LOCAL_CHANGE);

Juliusz Chroboczek committed
504
    return 1;
Juliusz Chroboczek committed
505 506 507 508

 fail:
    assert(up);
    interface_up(ifp, 0);
Juliusz Chroboczek committed
509
    local_notify_interface(ifp, LOCAL_CHANGE);
Juliusz Chroboczek committed
510
    return -1;
Juliusz Chroboczek committed
511 512
}

Juliusz Chroboczek committed
513
int
Matthieu Boutier committed
514
interface_ll_address(struct interface *ifp, const unsigned char *address)
Juliusz Chroboczek committed
515 516 517
{
    int i;

Matthieu Boutier committed
518
    if(!if_up(ifp))
Juliusz Chroboczek committed
519 520
        return 0;

Matthieu Boutier committed
521 522
    for(i = 0; i < ifp->numll; i++)
        if(memcmp(ifp->ll[i], address, 16) == 0)
Matthieu Boutier committed
523
            return 1;
Juliusz Chroboczek committed
524 525 526 527

    return 0;
}

Juliusz Chroboczek committed
528
void
Matthieu Boutier committed
529
check_interfaces(void)
Juliusz Chroboczek committed
530
{
Matthieu Boutier committed
531
    struct interface *ifp;
Matthieu Boutier committed
532 533
    int rc, ifindex_changed = 0;
    unsigned int ifindex;
Juliusz Chroboczek committed
534

Matthieu Boutier committed
535 536 537 538 539 540 541
    FOR_ALL_INTERFACES(ifp) {
        ifindex = if_nametoindex(ifp->name);
        if(ifindex != ifp->ifindex) {
            debugf("Noticed ifindex change for %s.\n", ifp->name);
            ifp->ifindex = 0;
            interface_up(ifp, 0);
            ifp->ifindex = ifindex;
Juliusz Chroboczek committed
542 543 544
            ifindex_changed = 1;
        }

Matthieu Boutier committed
545 546
        if(ifp->ifindex > 0)
            rc = kernel_interface_operational(ifp->name, ifp->ifindex);
Juliusz Chroboczek committed
547 548
        else
            rc = 0;
Matthieu Boutier committed
549 550 551
        if((rc > 0) != if_up(ifp)) {
            debugf("Noticed status change for %s.\n", ifp->name);
            interface_up(ifp, rc > 0);
Juliusz Chroboczek committed
552
        }
Juliusz Chroboczek committed
553

Juliusz Chroboczek committed
554
        if(if_up(ifp)) {
Juliusz Chroboczek committed
555
            /* Bother, said Pooh.  We should probably check for a change
Matthieu Boutier committed
556 557
               in IPv4 addresses at this point. */
            check_link_local_addresses(ifp);
Juliusz Chroboczek committed
558 559 560
            check_interface_channel(ifp);
            rc = check_interface_ipv4(ifp);
            if(rc > 0) {
Matthieu Boutier committed
561
                send_update(ifp, 0, NULL, 0, NULL, 0);
Juliusz Chroboczek committed
562 563
            }
        }
Juliusz Chroboczek committed
564
    }
Juliusz Chroboczek committed
565 566 567

    if(ifindex_changed)
        renumber_filters();
Juliusz Chroboczek committed
568
}