network.c 8.16 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 "filter.h"
40

41 42 43
struct network nets[MAXNETS];
int numnets = 0;

44
struct network *
45
add_network(char *ifname)
46 47 48 49 50 51 52
{
    if(numnets >= MAXNETS) {
        fprintf(stderr, "Too many networks.\n");
        return NULL;
    }

    memset(nets + numnets, 0, sizeof(struct network));
53 54
    nets[numnets].up = 0;
    nets[numnets].ifindex = 0;
55 56
    nets[numnets].ipv4 = NULL;
    nets[numnets].activity_time = now.tv_sec;
57
    nets[numnets].bufsize = 0;
58
    strncpy(nets[numnets].ifname, ifname, IF_NAMESIZE);
59
    nets[numnets].sendbuf = NULL;
60 61 62
    nets[numnets].buffered = 0;
    nets[numnets].bucket_time = now.tv_sec;
    nets[numnets].bucket = 0;
63
    nets[numnets].hello_interval = 10000;
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
    nets[numnets].hello_seqno = (random() & 0xFFFF);
    numnets++;
    return &nets[numnets - 1];
}

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;

    if(network_idle(net)) {
        if(net->hello_interval != idle_hello_interval) {
            net->hello_interval = idle_hello_interval;
            rc = 1;
        }
    } else if(net->wired) {
        if(net->hello_interval != wired_hello_interval) {
            net->hello_interval = wired_hello_interval;
            rc = 1;
        }
    } else {
        if(net->hello_interval != wireless_hello_interval) {
            net->hello_interval = wireless_hello_interval;
            rc = 1;
        }
    }

    if(net->ihu_interval != 3 * net->hello_interval) {
        net->ihu_interval = 3 * net->hello_interval;
        rc = 1;
    }

    net->self_update_interval =
        MAX(15 + net->hello_interval / 2, net->hello_interval);

    return rc;
}

/* This should be no more than half the hello interval, so that hellos
   aren't sent late.  The result is in milliseconds. */
unsigned int
jitter(struct network *net)
{
    unsigned interval = net->hello_interval * 1000;
115
    interval = MIN(interval, 2000);
116 117 118 119 120 121 122 123 124
    return (interval / 2 + random() % interval) / 4;
}

unsigned int
update_jitter(struct network *net, int urgent)
{
    unsigned interval = net->hello_interval * 1000;
    if(urgent)
        interval = MIN(interval, 100);
125 126
    else
        interval = MIN(interval, 4000);
127 128
    return (interval / 2 + random() % interval);
}
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
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;
}

161
int
162 163
network_up(struct network *net, int up)
{
164
    int mtu, rc, wired;
165
    struct ipv6_mreq mreq;
166

167 168 169 170 171
    if(up == net->up)
        return 0;

    net->up = up;

172
    if(up) {
173 174 175 176 177 178
        if(net->ifindex <= 0) {
            fprintf(stderr,
                    "Upping unknown interface %s.\n", net->ifname);
            return network_up(net, 0);
        }

179 180 181 182 183 184 185
        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);
        }

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        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;
        }
        /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
        mtu -= 60;

        if(net->sendbuf)
            free(net->sendbuf);
        net->bufsize = mtu - sizeof(packet_header);
        net->sendbuf = malloc(net->bufsize);
        if(net->sendbuf == NULL) {
            fprintf(stderr, "Couldn't allocate sendbuf.\n");
            net->bufsize = 0;
            return network_up(net, 0);
        }
204

205 206
        resize_receive_buffer(mtu);

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        if(all_wireless) {
            wired = 1;
        } 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;
            }
        }

        net->wired = wired;
        net->cost = wired ? 128 : 256;
224
        update_hello_interval(net);
225 226 227 228 229 230 231 232 233 234 235

        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)");
            /* But don't bail out for now. */
        }
236
        send_hello(net);
237
        send_request(net, NULL, 0, 0, 0, 0);
238 239 240 241 242
    } else {
        net->buffered = 0;
        net->bufsize = 0;
        free(net->sendbuf);
        net->sendbuf = NULL;
243 244 245 246 247 248 249 250 251
        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));
            if(rc < 0) {
                perror("setsockopt(IPV6_LEAVE_GROUP)");
            }
252
            kernel_setup_interface(0, net->ifname, net->ifindex);
253
        }
254 255
    }

256
    check_network_ipv4(net);
257
    update_network_metric(net);
258

259 260 261
    return 1;
}

262 263 264
void
check_networks(void)
{
265
    int i, rc, ifindex, ifindex_changed = 0;
266

267
    for(i = 0; i < numnets; i++) {
268 269 270
        ifindex = if_nametoindex(nets[i].ifname);
        if(ifindex != nets[i].ifindex) {
            debugf("Noticed ifindex change for %s.\n", nets[i].ifname);
271
            nets[i].ifindex = 0;
272 273 274 275 276 277 278 279 280
            network_up(&nets[i], 0);
            nets[i].ifindex = ifindex;
            ifindex_changed = 1;
        }

        if(nets[i].ifindex > 0)
            rc = kernel_interface_operational(nets[i].ifname, nets[i].ifindex);
        else
            rc = 0;
281
        if((rc > 0) != nets[i].up) {
282
            debugf("Noticed status change for %s.\n", nets[i].ifname);
283
            network_up(&nets[i], rc > 0);
284
        }
285 286

        check_network_ipv4(&nets[i]);
287
    }
288 289 290

    if(ifindex_changed)
        renumber_filters();
291
}