Blame view

xroute.c 13.1 KB
Juliusz Chroboczek committed
1
/*
Juliusz Chroboczek committed
2
Copyright (c) 2007, 2008 by Juliusz Chroboczek
Juliusz Chroboczek committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
Juliusz Chroboczek committed
27
#include <assert.h>
Juliusz Chroboczek committed
28
#include <sys/time.h>
Julien Cristau committed
29
#include <netinet/in.h>
Juliusz Chroboczek committed
30

Juliusz Chroboczek committed
31
#include "babeld.h"
Juliusz Chroboczek committed
32
#include "kernel.h"
Juliusz Chroboczek committed
33
#include "interface.h"
Juliusz Chroboczek committed
34
#include "neighbour.h"
Juliusz Chroboczek committed
35
#include "message.h"
Juliusz Chroboczek committed
36
#include "source.h"
Juliusz Chroboczek committed
37 38
#include "route.h"
#include "xroute.h"
Juliusz Chroboczek committed
39
#include "util.h"
Juliusz Chroboczek committed
40
#include "configuration.h"
Juliusz Chroboczek committed
41
#include "local.h"
Juliusz Chroboczek committed
42

Juliusz Chroboczek committed
43 44
static struct xroute *xroutes;
static int numxroutes = 0, maxxroutes = 0;
Juliusz Chroboczek committed
45

Juliusz Chroboczek committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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
static int
xroute_compare(const unsigned char *prefix, unsigned char plen,
               const unsigned char *src_prefix, unsigned char src_plen,
               const struct xroute *xroute)
{
    int rc;

    if(plen < xroute->plen)
        return -1;
    if(plen > xroute->plen)
        return 1;

    rc = memcmp(prefix, xroute->prefix, 16);
    if(rc != 0)
        return rc;

    if(src_plen < xroute->src_plen)
        return -1;
    if(src_plen > xroute->src_plen)
        return 1;

    rc = memcmp(src_prefix, xroute->src_prefix, 16);
    if(rc != 0)
        return rc;

    return 0;
}

static int
find_xroute_slot(const unsigned char *prefix, unsigned char plen,
                 const unsigned char *src_prefix, unsigned char src_plen,
                 int *new_return)
{
    int p, m, g, c;

    if(numxroutes < 1) {
        if(new_return)
            *new_return = 0;
        return -1;
    }

    p = 0; g = numxroutes - 1;

    do {
        m = (p + g) / 2;
        c = xroute_compare(prefix, plen, src_prefix, src_plen, &xroutes[m]);
        if(c == 0)
            return m;
        else if(c < 0)
            g = m - 1;
        else
            p = m + 1;
    } while(p <= g);

    if(new_return)
        *new_return = p;

    return -1;
}


Juliusz Chroboczek committed
107
struct xroute *
Matthieu Boutier committed
108 109
find_xroute(const unsigned char *prefix, unsigned char plen,
            const unsigned char *src_prefix, unsigned char src_plen)
Juliusz Chroboczek committed
110
{
Juliusz Chroboczek committed
111 112 113 114
    int i = find_xroute_slot(prefix, plen, src_prefix, src_plen, NULL);
    if(i >= 0)
        return &xroutes[i];

Juliusz Chroboczek committed
115
    return NULL;
Juliusz Chroboczek committed
116 117
}

Juliusz Chroboczek committed
118 119 120 121 122 123 124 125
int
add_xroute(unsigned char prefix[16], unsigned char plen,
           unsigned char src_prefix[16], unsigned char src_plen,
           unsigned short metric, unsigned int ifindex, int proto)
{
    int n = -1;
    int i = find_xroute_slot(prefix, plen, src_prefix, src_plen, &n);

Juliusz Chroboczek committed
126 127
    if(i >= 0)
        return -1;
Juliusz Chroboczek committed
128 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

    if(numxroutes >= maxxroutes) {
        struct xroute *new_xroutes;
        int num = maxxroutes < 1 ? 8 : 2 * maxxroutes;
        new_xroutes = realloc(xroutes, num * sizeof(struct xroute));
        if(new_xroutes == NULL)
            return -1;
        maxxroutes = num;
        xroutes = new_xroutes;
    }

    if(n < numxroutes)
        memmove(xroutes + n + 1, xroutes + n,
                (numxroutes - n) * sizeof(struct xroute));
    numxroutes++;

    memcpy(xroutes[n].prefix, prefix, 16);
    xroutes[n].plen = plen;
    memcpy(xroutes[n].src_prefix, src_prefix, 16);
    xroutes[n].src_plen = src_plen;
    xroutes[n].metric = metric;
    xroutes[n].ifindex = ifindex;
    xroutes[n].proto = proto;
    local_notify_xroute(&xroutes[n], LOCAL_ADD);
    return 1;
}

Juliusz Chroboczek committed
155 156 157
void
flush_xroute(struct xroute *xroute)
{
Juliusz Chroboczek committed
158
    int i;
Juliusz Chroboczek committed
159

Juliusz Chroboczek committed
160 161
    i = xroute - xroutes;
    assert(i >= 0 && i < numxroutes);
Juliusz Chroboczek committed
162

Juliusz Chroboczek committed
163
    local_notify_xroute(xroute, LOCAL_FLUSH);
Juliusz Chroboczek committed
164

Juliusz Chroboczek committed
165
    if(i != numxroutes - 1)
Juliusz Chroboczek committed
166 167
        memmove(xroutes + i, xroutes + i + 1,
                (numxroutes - i - 1) * sizeof(struct xroute));
Juliusz Chroboczek committed
168 169
    numxroutes--;
    VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute));
Juliusz Chroboczek committed
170

Juliusz Chroboczek committed
171 172 173 174 175
    if(numxroutes == 0) {
        free(xroutes);
        xroutes = NULL;
        maxxroutes = 0;
    } else if(maxxroutes > 8 && numxroutes < maxxroutes / 4) {
Juliusz Chroboczek committed
176 177 178 179 180 181
        struct xroute *new_xroutes;
        int n = maxxroutes / 2;
        new_xroutes = realloc(xroutes, n * sizeof(struct xroute));
        if(new_xroutes == NULL)
            return;
        xroutes = new_xroutes;
Juliusz Chroboczek committed
182
        maxxroutes = n;
Juliusz Chroboczek committed
183
    }
Juliusz Chroboczek committed
184 185
}

Juliusz Chroboczek committed
186 187 188 189 190 191 192
/* Returns an overestimate of the number of xroutes. */
int
xroutes_estimate()
{
    return numxroutes;
}

Juliusz Chroboczek committed
193 194 195 196 197 198 199
struct xroute_stream {
    int index;
};

struct
xroute_stream *
xroute_stream()
Juliusz Chroboczek committed
200
{
Juliusz Chroboczek committed
201
    struct xroute_stream *stream = calloc(1, sizeof(struct xroute_stream));
Juliusz Chroboczek committed
202
    if(stream == NULL)
Matthieu Boutier committed
203
        return NULL;
Juliusz Chroboczek committed
204 205 206 207

    return stream;
}

Juliusz Chroboczek committed
208

Juliusz Chroboczek committed
209 210 211 212 213 214 215 216 217 218 219 220 221
struct xroute *
xroute_stream_next(struct xroute_stream *stream)
{
    if(stream->index < numxroutes)
        return &xroutes[stream->index++];
    else
        return NULL;
}

void
xroute_stream_done(struct xroute_stream *stream)
{
    free(stream);
Juliusz Chroboczek committed
222 223
}

Matthieu Boutier committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static int
filter_route(struct kernel_route *route, void *data) {
    void **args = (void**)data;
    int maxroutes = *(int*)args[0];
    struct kernel_route *routes = (struct kernel_route *)args[1];
    int *found = (int*)args[2];

    if(*found >= maxroutes)
        return -1;

    if(martian_prefix(route->prefix, route->plen) ||
       martian_prefix(route->src_prefix, route->src_plen))
        return 0;

    routes[*found] = *route;
    ++ *found;

    return 0;
}

static int
kernel_routes(struct kernel_route *routes, int maxroutes)
{
    int found = 0;
    void *data[3] = { &maxroutes, routes, &found };
    struct kernel_filter filter = {0};
    filter.route = filter_route;
    filter.route_closure = data;

    kernel_dump(CHANGE_ROUTE, &filter);

    return found;
}

Matthieu Boutier committed
258 259 260 261 262 263
static int
filter_address(struct kernel_addr *addr, void *data) {
    void **args = (void **)data;
    int maxroutes = *(int *)args[0];
    struct kernel_route *routes = (struct kernel_route*)args[1];
    int *found = (int *)args[2];
Matthieu Boutier committed
264
    int ifindex = *(int*)args[3];
Matthieu Boutier committed
265 266 267 268 269 270 271 272 273
    int ll = args[4] ? !!*(int*)args[4] : 0;
    struct kernel_route *route = NULL;

    if(*found >= maxroutes)
        return 0;

    if(ll == !IN6_IS_ADDR_LINKLOCAL(&addr->addr))
        return 0;

Matthieu Boutier committed
274 275
    /* ifindex may be 0 -- see kernel_addresses */
    if(ifindex && addr->ifindex != ifindex)
Matthieu Boutier committed
276 277 278 279 280 281
        return 0;

    route = &routes[*found];
    memcpy(route->prefix, addr->addr.s6_addr, 16);
    route->plen = 128;
    route->metric = 0;
Jernej Kos committed
282
    route->ifindex = addr->ifindex;
Matthieu Boutier committed
283 284 285 286 287 288 289
    route->proto = RTPROT_BABEL_LOCAL;
    memset(route->gw, 0, 16);
    ++ *found;

    return 1;
}

Matthieu Boutier committed
290 291
/* ifindex is 0 for all interfaces.  ll indicates whether we are
   interested in link-local or global addresses. */
Matthieu Boutier committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
int
kernel_addresses(int ifindex, int ll, struct kernel_route *routes,
                 int maxroutes)
{
    int found = 0;
    void *data[5] = { &maxroutes, routes, &found, &ifindex, &ll };
    struct kernel_filter filter = {0};
    filter.addr = filter_address;
    filter.addr_closure = data;

    kernel_dump(CHANGE_ADDR, &filter);

    return found;
}

Juliusz Chroboczek committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
/* This must coincide with the ordering defined by xroute_compare above. */
static int
kernel_route_compare(const void *v1, const void *v2)
{
    const struct kernel_route *route1 = (struct kernel_route*)v1;
    const struct kernel_route *route2 = (struct kernel_route*)v2;
    int rc;

    if(route1->plen < route2->plen)
        return -1;
    if(route1->plen > route2->plen)
        return 1;

    rc = memcmp(route1->prefix, route2->prefix, 16);
    if(rc != 0)
        return rc;

    if(route1->src_plen < route2->src_plen)
        return -1;
    if(route1->src_plen > route2->src_plen)
        return 1;

    rc = memcmp(route1->src_prefix, route2->src_prefix, 16);
    if(rc != 0)
        return rc;

    return 0;
}

Juliusz Chroboczek committed
336
int
Juliusz Chroboczek committed
337
check_xroutes(int send_updates)
Juliusz Chroboczek committed
338
{
Juliusz Chroboczek committed
339
    int i, j, change = 0, rc;
Juliusz Chroboczek committed
340
    struct kernel_route *routes;
Matthieu Boutier committed
341
    struct filter_result filter_result;
Juliusz Chroboczek committed
342
    int numroutes;
Juliusz Chroboczek committed
343
    static int maxroutes = 8;
Dave Taht committed
344
    const int maxmaxroutes = 256 * 1024;
Juliusz Chroboczek committed
345

Juliusz Chroboczek committed
346 347
    debugf("\nChecking kernel routes.\n");

Juliusz Chroboczek committed
348
 again:
Matthieu Boutier committed
349
    routes = calloc(maxroutes, sizeof(struct kernel_route));
Juliusz Chroboczek committed
350 351 352
    if(routes == NULL)
        return -1;

Matthieu Boutier committed
353
    rc = kernel_addresses(0, 0, routes, maxroutes);
Juliusz Chroboczek committed
354
    if(rc < 0) {
Juliusz Chroboczek committed
355
        perror("kernel_addresses");
Juliusz Chroboczek committed
356
        numroutes = 0;
Juliusz Chroboczek committed
357
    } else {
Juliusz Chroboczek committed
358
        numroutes = rc;
Juliusz Chroboczek committed
359 360
    }

Juliusz Chroboczek committed
361 362 363 364 365 366 367 368 369 370 371
    if(numroutes >= maxroutes)
        goto resize;

    rc = kernel_routes(routes + numroutes, maxroutes - numroutes);
    if(rc < 0)
        fprintf(stderr, "Couldn't get kernel routes.\n");
    else
        numroutes += rc;

    if(numroutes >= maxroutes)
        goto resize;
Juliusz Chroboczek committed
372

Juliusz Chroboczek committed
373
    for(i = 0; i < numroutes; i++) {
Juliusz Chroboczek committed
374 375 376 377 378 379 380
        routes[i].metric = redistribute_filter(routes[i].prefix, routes[i].plen,
                                               routes[i].src_prefix,
                                               routes[i].src_plen,
                                               routes[i].ifindex,
                                               routes[i].proto,
                                               &filter_result);
        if(filter_result.src_prefix != NULL) {
Matthieu Boutier committed
381 382 383 384 385
            memcpy(routes[i].src_prefix, filter_result.src_prefix, 16);
            routes[i].src_plen = filter_result.src_plen;
        }
    }

Juliusz Chroboczek committed
386
    qsort(routes, numroutes, sizeof(struct kernel_route), kernel_route_compare);
Juliusz Chroboczek committed
387
    i = 0;
Juliusz Chroboczek committed
388 389
    j = 0;
    while(i < numroutes || j < numxroutes) {
Juliusz Chroboczek committed
390 391 392 393 394 395
        /* Ignore routes filtered out. */
        if(i < numroutes && routes[i].metric >= INFINITY) {
            i++;
            continue;
        }

Juliusz Chroboczek committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        if(i >= numroutes)
            rc = +1;
        else if(j >= numxroutes)
            rc = -1;
        else
            rc = xroute_compare(routes[i].prefix, routes[i].plen,
                                routes[i].src_prefix, routes[i].src_plen,
                                &xroutes[j]);
        if(rc < 0) {
            /* Add route i. */
            if(!martian_prefix(routes[i].prefix, routes[i].plen) &&
               routes[i].metric < INFINITY) {
                rc = add_xroute(routes[i].prefix, routes[i].plen,
                                routes[i].src_prefix, routes[i].src_plen,
                                routes[i].metric, routes[i].ifindex,
                                routes[i].proto);
                if(rc > 0) {
                    struct babel_route *route;
                    route = find_installed_route(routes[i].prefix,
                                                 routes[i].plen,
                                                 routes[i].src_prefix,
                                                 routes[i].src_plen);
                    if(route) {
                        if(allow_duplicates < 0 ||
                           routes[i].metric < allow_duplicates)
                            uninstall_route(route);
                    }
                    if(send_updates)
                        send_update(NULL, 0, routes[i].prefix, routes[i].plen,
                                    routes[i].src_prefix, routes[i].src_plen);
                    j++;
Juliusz Chroboczek committed
427
                }
Juliusz Chroboczek committed
428
            }
Juliusz Chroboczek committed
429 430 431
            i++;
        } else if(rc > 0) {
            /* Flush xroute j. */
Juliusz Chroboczek committed
432
            unsigned char prefix[16], plen;
Matthieu Boutier committed
433
            unsigned char src_prefix[16], src_plen;
Denis Ovsienko committed
434
            struct babel_route *route;
Juliusz Chroboczek committed
435 436
            memcpy(prefix, xroutes[i].prefix, 16);
            plen = xroutes[i].plen;
Matthieu Boutier committed
437 438
            memcpy(src_prefix, xroutes[i].src_prefix, 16);
            src_plen = xroutes[i].src_plen;
Juliusz Chroboczek committed
439 440 441 442
            flush_xroute(&xroutes[j]);
            route = find_best_route(prefix, plen, src_prefix, src_plen,
                                    1, NULL);
            if(route != NULL) {
Juliusz Chroboczek committed
443
                install_route(route);
Juliusz Chroboczek committed
444 445
                send_update(NULL, 0, prefix, plen, src_prefix, src_plen);
            } else {
Matthieu Boutier committed
446
                send_update_resend(NULL, prefix, plen, src_prefix, src_plen);
Juliusz Chroboczek committed
447
            }
Juliusz Chroboczek committed
448
        } else {
Juliusz Chroboczek committed
449 450
            if(routes[i].metric != xroutes[j].metric ||
               routes[i].proto != xroutes[j].proto) {
Juliusz Chroboczek committed
451 452 453
                xroutes[j].metric = routes[i].metric;
                xroutes[j].proto = routes[i].proto;
                local_notify_xroute(&xroutes[j], LOCAL_CHANGE);
Juliusz Chroboczek committed
454
                if(send_updates)
Juliusz Chroboczek committed
455 456
                    send_update(NULL, 0, xroutes[j].prefix, xroutes[j].plen,
                                xroutes[j].src_prefix, xroutes[j].src_plen);
Juliusz Chroboczek committed
457
            }
Juliusz Chroboczek committed
458
            i++;
Juliusz Chroboczek committed
459
            j++;
Juliusz Chroboczek committed
460 461
        }
    }
Juliusz Chroboczek committed
462

Juliusz Chroboczek committed
463
    free(routes);
Juliusz Chroboczek committed
464 465
    /* Set up maxroutes for the next call. */
    maxroutes = MIN(numroutes + 8, maxmaxroutes);
Juliusz Chroboczek committed
466
    return change;
Juliusz Chroboczek committed
467 468 469 470 471 472 473

 resize:
    free(routes);
    if(maxroutes >= maxmaxroutes)
        return -1;
    maxroutes = MIN(maxmaxroutes, 2 * maxroutes);
    goto again;
Juliusz Chroboczek committed
474
}