network.c 11.7 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 27 28 29 30
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
31 32 33 34 35 36 37

#include "babel.h"
#include "util.h"
#include "kernel.h"
#include "network.h"
#include "neighbour.h"
#include "message.h"
38
#include "route.h"
39
#include "config.h"
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
struct network *networks = NULL;

static struct network *
last_network(void)
{
    struct network *net = networks;

    if(!net)
        return NULL;

    while(net->next)
        net = net->next;

    return net;
}
56

57
struct network *
58
add_network(char *ifname, struct network_conf *conf)
59
{
60 61
    struct network *net;

62 63 64 65 66
    if(conf) {
        if(strcmp(ifname, conf->ifname) != 0)
            return NULL;
    }

67 68 69 70 71
    FOR_ALL_NETS(net) {
        if(strcmp(net->ifname, ifname) == 0)
            return net;
    }

72 73
    net = malloc(sizeof(struct network));
    if(net == NULL)
74
        return NULL;
75 76 77

    memset(net, 0, sizeof(struct network));
    strncpy(net->ifname, ifname, IF_NAMESIZE);
78 79
    net->conf = conf;
    net->activity_time = now.tv_sec;
80 81 82
    net->bucket_time = now.tv_sec;
    net->bucket = BUCKET_TOKENS_MAX;
    net->hello_seqno = (random() & 0xFFFF);
83 84 85 86 87

    if(networks == NULL)
        networks = net;
    else
        last_network()->next = net;
88
    return net;
89 90 91 92 93 94 95 96 97 98 99 100 101
}

int
network_idle(struct network *net)
{
    return (idle_hello_interval > 0 &&
            net->activity_time < now.tv_sec - idle_time);
}

int
update_hello_interval(struct network *net)
{
    int rc = 0;
102 103 104 105
    unsigned short interval;

    if(network_idle(net))
        interval = idle_hello_interval;
106 107
    else if(NET_CONF(net, hello_interval) > 0)
        interval = NET_CONF(net, hello_interval);
108 109 110 111
    else if((net->flags & NET_WIRED))
        interval = wired_hello_interval;
    else
        interval = wireless_hello_interval;
112

113 114
    if(net->hello_interval != interval) {
        net->hello_interval = interval;
115 116 117 118 119 120 121 122
            rc = 1;
    }

    return rc;
}

/* This should be no more than half the hello interval, so that hellos
   aren't sent late.  The result is in milliseconds. */
123
unsigned
124
jitter(struct network *net, int urgent)
125
{
126
    unsigned interval = net->hello_interval;
127 128 129
    if(urgent)
        interval = MIN(interval, 100);
    else
130 131
        interval = MIN(interval, 4000);
    return roughly(interval) / 4;
132 133
}

134
unsigned
135 136
update_jitter(struct network *net, int urgent)
{
137
    unsigned interval = net->hello_interval;
138 139
    if(urgent)
        interval = MIN(interval, 100);
140 141
    else
        interval = MIN(interval, 4000);
142
    return roughly(interval);
143
}
144

145
void
146
delay_jitter(struct timeval *timeout, int msecs)
147
{
148
    timeval_plus_msec(timeout, &now, roughly(msecs));
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
static int
check_network_ipv4(struct network *net)
{
    unsigned char ipv4[4];
    int rc;

    if(net->ifindex > 0)
        rc = kernel_interface_ipv4(net->ifname, net->ifindex, ipv4);
    else
        rc = 0;

    if(rc > 0) {
        if(!net->ipv4 || memcmp(ipv4, net->ipv4, 4) != 0) {
            debugf("Noticed IPv4 change for %s.\n", net->ifname);
            if(!net->ipv4)
                net->ipv4 = malloc(4);
            if(net->ipv4)
                memcpy(net->ipv4, ipv4, 4);
            return 1;
        }
    } else {
        debugf("Noticed IPv4 change for %s.\n", net->ifname);
        if(net->ipv4) {
            free(net->ipv4);
            net->ipv4 = NULL;
            return 1;
        }
    }
    return 0;
}

182
int
183 184
network_up(struct network *net, int up)
{
185
    int mtu, rc, wired;
186
    struct ipv6_mreq mreq;
187

188
    if((!!up) == net_up(net))
189 190
        return 0;

191 192 193 194
    if(up)
        net->flags |= NET_UP;
    else
        net->flags &= ~NET_UP;
195

196
    if(up) {
197
        unsigned char ll[32][16];
198 199 200 201 202 203
        if(net->ifindex <= 0) {
            fprintf(stderr,
                    "Upping unknown interface %s.\n", net->ifname);
            return network_up(net, 0);
        }

204 205 206 207 208 209 210
        rc = kernel_setup_interface(1, net->ifname, net->ifindex);
        if(rc < 0) {
            fprintf(stderr, "kernel_setup_interface(%s, %d) failed.\n",
                    net->ifname, net->ifindex);
            return network_up(net, 0);
        }

211 212 213 214 215 216
        mtu = kernel_interface_mtu(net->ifname, net->ifindex);
        if(mtu < 0) {
            fprintf(stderr, "Warning: couldn't get MTU of interface %s (%d).\n",
                    net->ifname, net->ifindex);
            mtu = 1280;
        }
217 218 219 220 221 222 223 224 225 226 227

        /* 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",
                    mtu, net->ifname, net->ifindex);
            mtu = 128;
        }

228 229
        if(net->sendbuf)
            free(net->sendbuf);
230 231 232

        /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
        net->bufsize = mtu - sizeof(packet_header) - 60;
233 234 235 236 237 238
        net->sendbuf = malloc(net->bufsize);
        if(net->sendbuf == NULL) {
            fprintf(stderr, "Couldn't allocate sendbuf.\n");
            net->bufsize = 0;
            return network_up(net, 0);
        }
239

240 241
        resize_receive_buffer(mtu);

242
        if(NET_CONF(net, wired) == CONFIG_NO) {
243 244 245
            wired = 0;
        } else if(NET_CONF(net, wired) == CONFIG_YES) {
            wired = 1;
246 247
        } else if(all_wireless) {
            wired = 0;
248 249 250 251 252 253 254 255 256 257 258 259 260
        } else {
            rc = kernel_interface_wireless(net->ifname, net->ifindex);
            if(rc < 0) {
                fprintf(stderr,
                        "Warning: couldn't determine whether %s (%d) "
                        "is a wireless interface.\n",
                        net->ifname, net->ifindex);
                wired = 0;
            } else {
                wired = !rc;
            }
        }

261 262
        if(wired) {
            net->flags |= NET_WIRED;
263
            net->cost = NET_CONF(net, cost);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
264
            if(net->cost <= 0) net->cost = 96;
265 266 267 268 269
            if(NET_CONF(net, split_horizon) == CONFIG_NO)
                net->flags &= ~NET_SPLIT_HORIZON;
            else if(NET_CONF(net, split_horizon) == CONFIG_YES)
                net->flags |= NET_SPLIT_HORIZON;
            else if(split_horizon)
270
                net->flags |= NET_SPLIT_HORIZON;
271 272 273 274 275 276
            else
                net->flags &= ~NET_SPLIT_HORIZON;
            if(NET_CONF(net, lq) == CONFIG_YES)
                net->flags |= NET_LQ;
            else
                net->flags &= ~NET_LQ;
277 278 279 280
            net->update_interval =
                NET_CONF(net, hello_interval) > 0 ?
                NET_CONF(net, hello_interval) * 5 :
                wired_hello_interval * 5;
281 282
        } else {
            net->flags &= ~NET_WIRED;
283
            net->cost = NET_CONF(net, cost);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
284
            if(net->cost <= 0) net->cost = 256;
285 286 287 288 289 290 291 292
            if(NET_CONF(net, split_horizon) == CONFIG_YES)
                net->flags |= NET_SPLIT_HORIZON;
            else
                net->flags &= ~NET_SPLIT_HORIZON;
            if(NET_CONF(net, lq) == CONFIG_NO)
                net->flags &= ~NET_LQ;
            else
                net->flags |= NET_LQ;
293 294 295 296
            net->update_interval =
                NET_CONF(net, hello_interval) > 0 ?
                NET_CONF(net, hello_interval) * 5 :
                wireless_hello_interval * 5;
297
        }
298
        update_hello_interval(net);
299 300 301 302 303 304 305 306 307

        memset(&mreq, 0, sizeof(mreq));
        memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
        mreq.ipv6mr_interface = net->ifindex;

        rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
                        (char*)&mreq, sizeof(mreq));
        if(rc < 0) {
            perror("setsockopt(IPV6_JOIN_GROUP)");
308 309 310 311
            /* This is probably due to a missing link-local address,
               so down this network, and wait until the main loop
               tries to up it again. */
            return network_up(net, 0);
312
        }
313 314 315

        if(net->ll)
            free(net->ll);
316
        net->numll = 0;
317 318 319 320 321 322 323 324 325
        net->ll = NULL;
        rc = kernel_ll_addresses(net->ifname, net->ifindex, ll, 32);
        if(rc < 0) {
            perror("kernel_ll_addresses");
        } else if(rc > 0) {
            net->ll = malloc(16 * rc);
            if(net->ll == NULL) {
                perror("malloc(ll)");
            } else {
326
                net->numll = rc;
327 328 329
                memcpy(net->ll, ll, rc * 16);
            }
        }
330
        delay_jitter(&net->hello_timeout, net->hello_interval);
331
        delay_jitter(&net->update_timeout, net->update_interval);
332
        send_hello(net);
333
        send_request(net, NULL, 0);
334 335 336 337
    } else {
        net->buffered = 0;
        net->bufsize = 0;
        free(net->sendbuf);
338 339 340 341
        net->num_buffered_updates = 0;
        net->update_bufsize = 0;
        if(net->buffered_updates)
            free(net->buffered_updates);
342
        net->buffered_updates = NULL;
343
        net->sendbuf = NULL;
344 345 346 347 348 349
        if(net->ifindex > 0) {
            memset(&mreq, 0, sizeof(mreq));
            memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
            mreq.ipv6mr_interface = net->ifindex;
            rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
                            (char*)&mreq, sizeof(mreq));
350
            if(rc < 0)
351
                perror("setsockopt(IPV6_LEAVE_GROUP)");
352
            kernel_setup_interface(0, net->ifname, net->ifindex);
353
        }
354 355 356
        if(net->ll)
            free(net->ll);
        net->ll = NULL;
357
        net->numll = 0;
358 359
    }

360
    update_network_metric(net);
361 362 363
    rc = check_network_ipv4(net);
    if(up && rc > 0)
        send_update(net, 0, NULL, 0);
364

365 366 367
    return 1;
}

368 369 370 371 372
int
network_ll_address(struct network *net, const unsigned char *address)
{
    int i;

373
    if(!net_up(net))
374 375
        return 0;

376
    for(i = 0; i < net->numll; i++)
377 378 379 380 381 382
        if(memcmp(net->ll[i], address, 16) == 0)
           return 1;

    return 0;
}

383 384 385
void
check_networks(void)
{
386 387
    struct network *net;
    int rc, ifindex, ifindex_changed = 0;
388

389
    FOR_ALL_NETS(net) {
390 391 392 393 394 395
        ifindex = if_nametoindex(net->ifname);
        if(ifindex != net->ifindex) {
            debugf("Noticed ifindex change for %s.\n", net->ifname);
            net->ifindex = 0;
            network_up(net, 0);
            net->ifindex = ifindex;
396 397 398
            ifindex_changed = 1;
        }

399 400
        if(net->ifindex > 0)
            rc = kernel_interface_operational(net->ifname, net->ifindex);
401 402
        else
            rc = 0;
403
        if((rc > 0) != net_up(net)) {
404 405
            debugf("Noticed status change for %s.\n", net->ifname);
            network_up(net, rc > 0);
406
        }
407

408
        rc = check_network_ipv4(net);
409
        if(rc > 0) {
410
            send_request(net, NULL, 0);
411
            send_update(net, 0, NULL, 0);
412
         }
413
    }
414 415 416

    if(ifindex_changed)
        renumber_filters();
417
}