interface.c 17.3 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
#include "hmac.h"
46

47 48
#define MIN_MTU 512

49
struct interface *interfaces = NULL;
50

51 52
static struct interface *
last_interface(void)
53
{
54
    struct interface *ifp = interfaces;
55

56
    if(!ifp)
57 58
        return NULL;

59 60
    while(ifp->next)
        ifp = ifp->next;
61

62
    return ifp;
63
}
64

65 66
struct interface *
add_interface(char *ifname, struct interface_conf *if_conf)
67
{
68
    struct interface *ifp;
69

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

80
    ifp = calloc(1, sizeof(struct interface));
81
    if(ifp == NULL)
82
        return NULL;
83

84
    strncpy(ifp->name, ifname, IF_NAMESIZE);
85
    ifp->conf = if_conf ? if_conf : default_interface_conf;
86
    ifp->hello_seqno = (random() & 0xFFFF);
87

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

93 94
    local_notify_interface(ifp, LOCAL_ADD);

95
    return ifp;
96 97
}

98
int
99 100 101 102 103 104 105 106 107 108 109 110 111
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;
    }

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

115
    interface_updown(ifp, 0);
116 117 118 119 120 121 122
    if(prev)
        prev->next = ifp->next;
    else
        interfaces = ifp->next;

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

    local_notify_interface(ifp, LOCAL_FLUSH);

126
    free(ifp);
127 128

    return 1;
129 130
}

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

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

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

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

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

    if(rc > 0) {
173 174 175 176 177 178 179
        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);
180
            local_notify_interface(ifp, LOCAL_CHANGE);
181 182 183
            return 1;
        }
    } else {
184 185 186 187 188
        if(ifp->ipv4) {
            debugf("Noticed IPv4 change for %s.\n", ifp->name);
            flush_interface_routes(ifp, 0);
            free(ifp->ipv4);
            ifp->ipv4 = NULL;
189
            local_notify_interface(ifp, LOCAL_CHANGE);
190 191 192 193 194 195
            return 1;
        }
    }
    return 0;
}

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

202
    if(channel == IF_CHANNEL_UNKNOWN) {
203 204 205 206 207 208 209 210
        /* 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;
211 212 213
        }
    }

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

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

227
    rc = kernel_addresses(ifp->ifindex, 1, ll, 32);
228 229 230 231 232 233 234 235 236 237 238 239
    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);
240 241 242 243 244
        /* Most probably DAD hasn't finished yet.  Reschedule us
           real soon. */
        schedule_interfaces_check(2000, 0);
        return -1;
    } else {
245 246 247 248 249 250 251 252 253
        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;
                }
            }
254
        } else {
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
            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);
270 271 272 273 274 275
        }
    }

    return 0;
}

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

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

285
    if(up) {
286
        ifp->flags |= IF_UP;
287
        if(ifp->ifindex <= 0) {
288
            fprintf(stderr,
289
                    "Upping unknown interface %s.\n", ifp->name);
290
            goto fail;
291 292
        }

293
        rc = kernel_setup_interface(1, ifp->name, ifp->ifindex);
294
        if(rc < 0) {
295
            fprintf(stderr, "kernel_setup_interface(%s, %u) failed.\n",
296
                    ifp->name, ifp->ifindex);
297
            goto fail;
298 299
        }

300 301 302 303 304 305
        memset(&ifp->buf.sin6, 0, sizeof(ifp->buf.sin6));
        ifp->buf.sin6.sin6_family = AF_INET6;
        memcpy(&ifp->buf.sin6.sin6_addr, protocol_group, 16);
        ifp->buf.sin6.sin6_port = htons(protocol_port);
        ifp->buf.sin6.sin6_scope_id = ifp->ifindex;

306
        mtu = kernel_interface_mtu(ifp->name, ifp->ifindex);
307
        if(mtu < 0) {
308 309 310
            fprintf(stderr,
                    "Warning: couldn't get MTU of interface %s (%u), "
                    "using 1280\n",
311
                    ifp->name, ifp->ifindex);
312 313
            mtu = 1280;
        }
314

315 316 317 318 319 320
        /* We need to be able to fit at least a router-ID and an update,
           up to 116 bytes, and that's not counting sub-TLVs or crypto keys.
           In IPv6, the minimum MTU is 1280, and every host must be able
           to reassemble up to 1500 bytes.  In IPv4, every host must be
           able to reassemble up to 576 bytes.  At any rate, the Babel spec
           says that every node must be able to parse packets of size 512. */
321
        if(mtu < MIN_MTU) {
322
            fprintf(stderr,
323 324
                    "Suspiciously low MTU %d on interface %s (%u), using %d.\n",
                    mtu, ifp->name, ifp->ifindex, MIN_MTU);
325
            mtu = 512;
326 327
        }

328 329
        if(ifp->buf.buf)
            free(ifp->buf.buf);
330 331

        /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
332 333 334
        ifp->buf.size = mtu - sizeof(packet_header) - 60;
        ifp->buf.buf = malloc(ifp->buf.size);
        if(ifp->buf.buf == NULL) {
335
            fprintf(stderr, "Couldn't allocate sendbuf.\n");
336
            ifp->buf.size = 0;
337
            goto fail;
338
        }
339
        ifp->buf.hello = -1;
340

341 342 343
        rc = resize_receive_buffer(mtu);
        if(rc < 0)
            fprintf(stderr, "Warning: couldn't resize "
344
                    "receive buffer for interface %s (%u) (%d bytes).\n",
345
                    ifp->name, ifp->ifindex, mtu);
346

347 348 349 350
        type = IF_CONF(ifp, type);
        if(type == IF_TYPE_DEFAULT) {
            if(all_wireless) {
                type = IF_TYPE_WIRELESS;
351
            } else {
352 353 354
                rc = kernel_interface_wireless(ifp->name, ifp->ifindex);
                if(rc < 0) {
                    fprintf(stderr,
355
                            "Warning: couldn't determine whether %s (%u) "
356 357 358 359 360
                            "is a wireless interface.\n",
                            ifp->name, ifp->ifindex);
                } else if(rc) {
                    type = IF_TYPE_WIRELESS;
                }
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
        /* 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;
393

394 395
        if(IF_CONF(ifp, faraway) == CONFIG_YES)
            ifp->flags |= IF_FARAWAY;
396

397 398
        if(IF_CONF(ifp, unicast) == CONFIG_YES)
            ifp->flags |= IF_UNICAST;
399 400
        if(IF_CONF(ifp, no_hmac_verify) == CONFIG_YES)
            ifp->flags |= IF_NO_HMAC_VERIFY;
401 402
        if(IF_CONF(ifp, hello_interval) > 0)
            ifp->hello_interval = IF_CONF(ifp, hello_interval);
403
        else if(type == IF_TYPE_WIRELESS)
404
            ifp->hello_interval = default_wireless_hello_interval;
405 406
        else
            ifp->hello_interval = default_wired_hello_interval;
407

408 409 410
        ifp->update_interval =
            IF_CONF(ifp, update_interval) > 0 ?
            IF_CONF(ifp, update_interval) :
411
            ifp->hello_interval * 4;
412

413 414 415 416
        /* This must be no more than half the Hello interval, or else
           Hellos will arrive late. */
        ifp->buf.flush_interval = ifp->hello_interval / 2;

417 418 419
        ifp->rtt_decay =
            IF_CONF(ifp, rtt_decay) > 0 ?
            IF_CONF(ifp, rtt_decay) : 42;
420 421 422 423 424 425 426 427 428

        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,
429 430
                    "Uh, rtt-max is less than or equal to rtt-min (%u <= %u). "
                    "Setting it to %u.\n", ifp->rtt_max, ifp->rtt_min,
431 432 433 434
                    ifp->rtt_min + 10000);
            ifp->rtt_max = ifp->rtt_min + 10000;
        }
        ifp->max_rtt_penalty = IF_CONF(ifp, max_rtt_penalty);
435 436
        if(ifp->max_rtt_penalty == 0 && type == IF_TYPE_TUNNEL)
            ifp->max_rtt_penalty = 96;
437

438
        if(IF_CONF(ifp, enable_timestamps) == CONFIG_YES)
439
            ifp->flags |= IF_TIMESTAMPS;
440
        else if(IF_CONF(ifp, enable_timestamps) == CONFIG_NO)
441
            ifp->flags &= ~IF_TIMESTAMPS;
442
        else if(type == IF_TYPE_TUNNEL)
443
            ifp->flags |= IF_TIMESTAMPS;
444
        else
445 446
            ifp->flags &= ~IF_TIMESTAMPS;
        if(ifp->max_rtt_penalty > 0 && !(ifp->flags & IF_TIMESTAMPS))
447 448 449 450
            fprintf(stderr,
                    "Warning: max_rtt_penalty is set "
                    "but timestamps are disabled on interface %s.\n",
                    ifp->name);
451

452 453 454 455
        if(IF_CONF(ifp, rfc6126) == CONFIG_YES)
            ifp->flags |= IF_RFC6126;
        else
            ifp->flags &= ~IF_RFC6126;
456

457 458 459 460
        rc = check_link_local_addresses(ifp);
        if(rc < 0) {
            goto fail;
        }
461 462 463
        memset(&mreq, 0, sizeof(mreq));
        memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
        mreq.ipv6mr_interface = ifp->ifindex;
464 465 466 467 468 469 470
        rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
                        (char*)&mreq, sizeof(mreq));
        if(rc < 0) {
            perror("setsockopt(IPV6_JOIN_GROUP)");
            goto fail;
        }

471 472 473 474 475
        rc = check_interface_channel(ifp);
        if(rc < 0)
            fprintf(stderr,
                    "Warning: couldn't determine channel of interface %s.\n",
                    ifp->name);
476 477 478
        update_interface_metric(ifp);
        rc = check_interface_ipv4(ifp);

479 480 481 482 483 484 485 486 487
        if(IF_CONF(ifp, key) != ifp->key) {
            if(ifp->key != NULL)
                release_key(ifp->key);
            if(IF_CONF(ifp, key) != NULL)
                ifp->key = retain_key(IF_CONF(ifp, key));
            else
                ifp->key = NULL;
        }

488
        debugf("Upped interface %s (cost=%d, channel=%d%s).\n",
489 490 491 492 493
               ifp->name,
               ifp->cost,
               ifp->channel,
               ifp->ipv4 ? ", IPv4" : "");

494 495 496
        set_timeout(&ifp->hello_timeout, ifp->hello_interval);
        set_timeout(&ifp->update_timeout, ifp->update_interval);
        send_hello(ifp);
497
        if(rc > 0)
498
            send_update(ifp, 0, NULL, 0, NULL, 0);
499
        send_multicast_request(ifp, NULL, 0, NULL, 0);
500
    } else {
501
        ifp->flags &= ~IF_UP;
502
        flush_interface_routes(ifp, 0);
503 504 505
        ifp->buf.len = 0;
        ifp->buf.size = 0;
        free(ifp->buf.buf);
506 507 508 509 510
        ifp->num_buffered_updates = 0;
        ifp->update_bufsize = 0;
        if(ifp->buffered_updates)
            free(ifp->buffered_updates);
        ifp->buffered_updates = NULL;
511
        ifp->buf.buf = NULL;
512
        if(ifp->ifindex > 0) {
513 514
            memset(&mreq, 0, sizeof(mreq));
            memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
515
            mreq.ipv6mr_interface = ifp->ifindex;
516 517
            rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
                            (char*)&mreq, sizeof(mreq));
518
            if(rc < 0)
519
                perror("setsockopt(IPV6_LEAVE_GROUP)");
520
            kernel_setup_interface(0, ifp->name, ifp->ifindex);
521
        }
522 523 524 525
        if(ifp->ll)
            free(ifp->ll);
        ifp->ll = NULL;
        ifp->numll = 0;
526 527
    }

528 529
    local_notify_interface(ifp, LOCAL_CHANGE);

530
    return 1;
531 532 533

 fail:
    assert(up);
534
    interface_updown(ifp, 0);
535
    local_notify_interface(ifp, LOCAL_CHANGE);
536
    return -1;
537 538
}

539
int
540
interface_ll_address(struct interface *ifp, const unsigned char *address)
541 542 543
{
    int i;

544
    if(!if_up(ifp))
545 546
        return 0;

547 548
    for(i = 0; i < ifp->numll; i++)
        if(memcmp(ifp->ll[i], address, 16) == 0)
549
            return 1;
550 551 552 553

    return 0;
}

554
void
555
check_interfaces(void)
556
{
557
    struct interface *ifp;
558 559
    int rc, ifindex_changed = 0;
    unsigned int ifindex;
560

561 562 563 564
    FOR_ALL_INTERFACES(ifp) {
        ifindex = if_nametoindex(ifp->name);
        if(ifindex != ifp->ifindex) {
            debugf("Noticed ifindex change for %s.\n", ifp->name);
565
            interface_updown(ifp, 0);
566
            ifp->ifindex = ifindex;
567 568 569
            ifindex_changed = 1;
        }

570 571
        if(ifp->ifindex > 0)
            rc = kernel_interface_operational(ifp->name, ifp->ifindex);
572 573
        else
            rc = 0;
574 575
        if((rc > 0) != if_up(ifp)) {
            debugf("Noticed status change for %s.\n", ifp->name);
576
            interface_updown(ifp, rc > 0);
577
        }
578

579
        if(if_up(ifp)) {
580
            /* Bother, said Pooh.  We should probably check for a change
581 582
               in IPv4 addresses at this point. */
            check_link_local_addresses(ifp);
583 584 585
            check_interface_channel(ifp);
            rc = check_interface_ipv4(ifp);
            if(rc > 0) {
586
                send_multicast_request(ifp, NULL, 0, NULL, 0);
587
                send_update(ifp, 0, NULL, 0, NULL, 0);
588 589
            }
        }
590
    }
591 592 593

    if(ifindex_changed)
        renumber_filters();
594
}