interface.c 16.1 KB
Newer Older
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>
26
#include <errno.h>
27
#include <assert.h>
28
#include <sys/time.h>
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>
34

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

46
struct interface *interfaces = NULL;
47

48 49
static struct interface *
last_interface(void)
50
{
51
    struct interface *ifp = interfaces;
52

53
    if(!ifp)
54 55
        return NULL;

56 57
    while(ifp->next)
        ifp = ifp->next;
58

59
    return ifp;
60
}
61

62 63
struct interface *
add_interface(char *ifname, struct interface_conf *if_conf)
64
{
65
    struct interface *ifp;
66

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

77
    ifp = calloc(1, sizeof(struct interface));
78
    if(ifp == NULL)
79
        return NULL;
80

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

87 88
    if(interfaces == NULL)
        interfaces = ifp;
89
    else
90
        last_interface()->next = ifp;
91

92 93
    local_notify_interface(ifp, LOCAL_ADD);

94
    return ifp;
95 96
}

97
int
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;
    }

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

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

120 121
    local_notify_interface(ifp, LOCAL_FLUSH);

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

    local_notify_interface(ifp, LOCAL_FLUSH);

127
    free(ifp);
128 129

    return 1;
130 131
}

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

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

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

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

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

    if(rc > 0) {
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);
181
            local_notify_interface(ifp, LOCAL_CHANGE);
182 183 184
            return 1;
        }
    } else {
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;
190
            local_notify_interface(ifp, LOCAL_CHANGE);
191 192 193 194 195 196
            return 1;
        }
    }
    return 0;
}

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

203
    if(channel == IF_CHANNEL_UNKNOWN) {
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;
212 213 214
        }
    }

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

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

228
    rc = kernel_addresses(ifp->ifindex, 1, ll, 32);
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);
241 242 243 244 245
        /* Most probably DAD hasn't finished yet.  Reschedule us
           real soon. */
        schedule_interfaces_check(2000, 0);
        return -1;
    } else {
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;
                }
            }
255
        } else {
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);
271 272 273 274 275 276
        }
    }

    return 0;
}

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

283
    if((!!up) == if_up(ifp))
284 285
        return 0;

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

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

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

305
        mtu = kernel_interface_mtu(ifp->name, ifp->ifindex);
306 307
        if(mtu < 0) {
            fprintf(stderr, "Warning: couldn't get MTU of interface %s (%d).\n",
308
                    ifp->name, ifp->ifindex);
309 310
            mtu = 1280;
        }
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",
318
                    mtu, ifp->name, ifp->ifindex);
319 320 321
            mtu = 128;
        }

322 323
        if(ifp->sendbuf)
            free(ifp->sendbuf);
324 325

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

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);
339

340 341 342 343
        type = IF_CONF(ifp, type);
        if(type == IF_TYPE_DEFAULT) {
            if(all_wireless) {
                type = IF_TYPE_WIRELESS;
344
            } else {
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;
                }
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
        /* 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;
386

387 388
        if(IF_CONF(ifp, faraway) == CONFIG_YES)
            ifp->flags |= IF_FARAWAY;
389

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

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

402 403 404
        ifp->rtt_decay =
            IF_CONF(ifp, rtt_decay) > 0 ?
            IF_CONF(ifp, rtt_decay) : 42;
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);
420 421
        if(ifp->max_rtt_penalty == 0 && type == IF_TYPE_TUNNEL)
            ifp->max_rtt_penalty = 96;
422

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)
428
            ifp->flags |= IF_TIMESTAMPS;
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);
436

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

442 443 444 445
        rc = check_link_local_addresses(ifp);
        if(rc < 0) {
            goto fail;
        }
446 447 448
        memset(&mreq, 0, sizeof(mreq));
        memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
        mreq.ipv6mr_interface = ifp->ifindex;
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;
        }

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);
461 462 463
        update_interface_metric(ifp);
        rc = check_interface_ipv4(ifp);

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

470 471 472
        set_timeout(&ifp->hello_timeout, ifp->hello_interval);
        set_timeout(&ifp->update_timeout, ifp->update_interval);
        send_hello(ifp);
473
        if(rc > 0)
474
            send_update(ifp, 0, NULL, 0, NULL, 0);
475
    } else {
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) {
487 488
            memset(&mreq, 0, sizeof(mreq));
            memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
489
            mreq.ipv6mr_interface = ifp->ifindex;
490 491
            rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
                            (char*)&mreq, sizeof(mreq));
492
            if(rc < 0)
493
                perror("setsockopt(IPV6_LEAVE_GROUP)");
494
            kernel_setup_interface(0, ifp->name, ifp->ifindex);
495
        }
496 497 498 499
        if(ifp->ll)
            free(ifp->ll);
        ifp->ll = NULL;
        ifp->numll = 0;
500 501
    }

502 503
    local_notify_interface(ifp, LOCAL_CHANGE);

504
    return 1;
505 506 507 508

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

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

518
    if(!if_up(ifp))
519 520
        return 0;

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

    return 0;
}

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

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;
542 543 544
            ifindex_changed = 1;
        }

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

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

    if(ifindex_changed)
        renumber_filters();
568
}