disambiguation.c 14.9 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 26 27 28 29 30 31 32 33 34 35 36
/*
Copyright (c) 2014 by Matthieu Boutier and 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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>

#include "babeld.h"
#include "util.h"
#include "interface.h"
#include "kernel.h"
#include "route.h"
#include "source.h"
#include "neighbour.h"
37
#include "rule.h"
38 39 40 41 42 43 44 45

struct zone {
    const unsigned char *dst_prefix;
    unsigned char dst_plen;
    const unsigned char *src_prefix;
    unsigned char src_plen;
};

46 47 48
/* This function assumes rt1 and rt2 non disjoint. */
static int
rt_cmp(const struct babel_route *rt1, const struct babel_route *rt2)
49
{
50 51 52
    enum prefix_status dst_st, src_st;
    const struct source *r1 = rt1->src, *r2 = rt2->src;
    dst_st = prefix_cmp(r1->prefix, r1->plen, r2->prefix, r2->plen);
53
    if(dst_st == PST_MORE_SPECIFIC)
54
        return -1;
55
    else if(dst_st == PST_LESS_SPECIFIC)
56 57 58
        return 1;
    src_st = prefix_cmp(r1->src_prefix, r1->src_plen,
                        r2->src_prefix, r2->src_plen);
59
    if(src_st == PST_MORE_SPECIFIC)
60
        return -1;
61
    else if(src_st == PST_LESS_SPECIFIC)
62 63
        return 1;
    return 0;
64 65
}

66 67
static const struct babel_route *
min_route(const struct babel_route *r1, const struct babel_route *r2)
68
{
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
69
    int rc;
Matthieu Boutier's avatar
Matthieu Boutier committed
70 71
    if(!r1) return r2;
    if(!r2) return r1;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
72
    rc = rt_cmp(r1, r2);
73 74
    return rc <= 0 ? r1 : r2;
}
75

76 77 78 79 80 81
static int
conflicts(const struct babel_route *rt, const struct babel_route *rt1)
{
    enum prefix_status dst_st, src_st;
    const struct source *r = rt->src, *r1 = rt1->src;
    dst_st = prefix_cmp(r->prefix, r->plen, r1->prefix, r1->plen);
82
    if(dst_st == PST_DISJOINT || dst_st == PST_EQUALS)
83
        return 0;
84 85 86 87
    src_st = prefix_cmp(r->src_prefix, r->src_plen,
                        r1->src_prefix, r1->src_plen);
    return ((dst_st == PST_LESS_SPECIFIC && src_st == PST_MORE_SPECIFIC) ||
            (dst_st == PST_MORE_SPECIFIC && src_st == PST_LESS_SPECIFIC));
88 89
}

90 91
static const struct zone*
to_zone(const struct babel_route *rt, struct zone *zone)
92
{
93 94 95 96 97
    zone->dst_prefix = rt->src->prefix;
    zone->dst_plen = rt->src->plen;
    zone->src_prefix = rt->src->src_prefix;
    zone->src_plen = rt->src->src_plen;
    return zone;
98 99
}

100 101 102 103 104
/* fill zone with rt cap rt1, and returns a pointer to zone, or NULL if the
   intersection is empty. */
static const struct zone*
inter(const struct babel_route *rt, const struct babel_route *rt1,
      struct zone *zone)
105
{
106 107 108
    enum prefix_status dst_st, src_st;
    const struct source *r = rt->src, *r1 = rt1->src;
    dst_st = prefix_cmp(r->prefix, r->plen, r1->prefix, r1->plen);
109
    if(dst_st == PST_DISJOINT)
110 111 112
        return NULL;
    src_st = prefix_cmp(r->src_prefix, r->src_plen,
                        r1->src_prefix, r1->src_plen);
113
    if(src_st == PST_DISJOINT)
114
        return NULL;
Matthieu Boutier's avatar
Matthieu Boutier committed
115
    if(dst_st == PST_MORE_SPECIFIC || dst_st == PST_EQUALS) {
116 117
        zone->dst_prefix = r->prefix;
        zone->dst_plen = r->plen;
118
    } else {
119 120
        zone->dst_prefix = r1->prefix;
        zone->dst_plen = r1->plen;
121
    }
Matthieu Boutier's avatar
Matthieu Boutier committed
122
    if(src_st == PST_MORE_SPECIFIC || src_st == PST_EQUALS) {
123 124 125 126 127 128 129
        zone->src_prefix = r->src_prefix;
        zone->src_plen = r->src_plen;
    } else {
        zone->src_prefix = r1->src_prefix;
        zone->src_plen = r1->src_plen;
    }
    return zone;
130 131 132
}

static int
133
zone_equal(const struct zone *z1, const struct zone *z2)
134
{
135
    return z1 && z2 && z1->dst_plen == z2->dst_plen &&
136
        memcmp(z1->dst_prefix, z2->dst_prefix, 16) == 0 &&
137
        z1->src_plen == z2->src_plen &&
138
        memcmp(z1->src_prefix, z2->src_prefix, 16) == 0;
139
}
140

141 142 143 144 145 146 147
static const struct babel_route *
min_conflict(const struct zone *zone, const struct babel_route *rt)
{
    struct babel_route *rt1 = NULL;
    const struct babel_route *min = NULL;
    struct route_stream *stream = NULL;
    struct zone curr_zone;
148
    stream = route_stream(ROUTE_INSTALLED);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    if(!stream) {
        fprintf(stderr, "Couldn't allocate route stream.\n");
        return NULL;
    }
    while(1) {
        rt1 = route_stream_next(stream);
        if(rt1 == NULL) break;
        if(!(conflicts(rt, rt1) &&
             zone_equal(inter(rt, rt1, &curr_zone), zone)))
            continue;
        min = min_route(rt1, min);
    }
    route_stream_done(stream);
    return min;
}
164

165 166 167 168 169 170 171 172 173
static const struct babel_route *
conflict_solution(const struct babel_route *rt)
{
    const struct babel_route *rt1 = NULL, *rt2 = NULL;
    struct route_stream *stream1 = NULL;
    struct route_stream *stream2 = NULL;
    const struct babel_route *min = NULL; /* == solution */
    struct zone zone;
    struct zone tmp;
174 175
    /* Having a conflict requires at least one specific route. */
    stream1 = route_stream(ROUTE_SS_INSTALLED);
176 177 178 179 180 181 182
    if(!stream1) {
        return NULL;
    }
    while(1) {
        rt1 = route_stream_next(stream1);
        if(rt1 == NULL) break;

183
        stream2 = route_stream(ROUTE_INSTALLED);
184 185 186 187
        if(!stream2) {
            route_stream_done(stream1);
            fprintf(stderr, "Couldn't allocate route stream.\n");
            return NULL;
188
        }
189 190 191

        while(1) {
            rt2 = route_stream_next(stream2);
Matthieu Boutier's avatar
Matthieu Boutier committed
192
            if(rt2 == NULL) break;
193 194 195 196 197 198 199
            if(!(conflicts(rt1, rt2) &&
                 zone_equal(inter(rt1, rt2, &tmp), to_zone(rt, &zone)) &&
                 rt_cmp(rt1, rt2) < 0))
                continue;
            min = min_route(rt1, min);
        }
        route_stream_done(stream2);
200
    }
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    route_stream_done(stream1);
    return min;
}

static int
is_installed(struct zone *zone)
{
    return zone != NULL &&
        find_installed_route(zone->dst_prefix, zone->dst_plen,
                             zone->src_prefix, zone->src_plen) != NULL;
}

static int
add_route(const struct zone *zone, const struct babel_route *route)
{
216 217 218
    int table = find_table(zone->dst_prefix, zone->dst_plen,
                           zone->src_prefix, zone->src_plen);
    return kernel_route(ROUTE_ADD, table, zone->dst_prefix, zone->dst_plen,
219 220 221
                        zone->src_prefix, zone->src_plen,
                        route->nexthop,
                        route->neigh->ifp->ifindex,
222
                        metric_to_kernel(route_metric(route)), NULL, 0, 0, 0);
223 224 225 226 227
}

static int
del_route(const struct zone *zone, const struct babel_route *route)
{
228 229 230
    int table = find_table(zone->dst_prefix, zone->dst_plen,
                           zone->src_prefix, zone->src_plen);
    return kernel_route(ROUTE_FLUSH, table, zone->dst_prefix, zone->dst_plen,
231 232 233
                        zone->src_prefix, zone->src_plen,
                        route->nexthop,
                        route->neigh->ifp->ifindex,
234
                        metric_to_kernel(route_metric(route)), NULL, 0, 0, 0);
235 236 237 238 239 240
}

static int
chg_route(const struct zone *zone, const struct babel_route *old,
          const struct babel_route *new)
{
241 242 243
    int table = find_table(zone->dst_prefix, zone->dst_plen,
                           zone->src_prefix, zone->src_plen);
    return kernel_route(ROUTE_MODIFY, table, zone->dst_prefix, zone->dst_plen,
244 245 246 247
                        zone->src_prefix, zone->src_plen,
                        old->nexthop, old->neigh->ifp->ifindex,
                        metric_to_kernel(route_metric(old)),
                        new->nexthop, new->neigh->ifp->ifindex,
248
                        metric_to_kernel(route_metric(new)), table);
249 250 251 252 253 254
}

static int
chg_route_metric(const struct zone *zone, const struct babel_route *route,
                 int old_metric, int new_metric)
{
255 256 257
    int table = find_table(zone->dst_prefix, zone->dst_plen,
                           zone->src_prefix, zone->src_plen);
    return kernel_route(ROUTE_MODIFY, table, zone->dst_prefix, zone->dst_plen,
258 259 260 261
                        zone->src_prefix, zone->src_plen,
                        route->nexthop, route->neigh->ifp->ifindex,
                        old_metric,
                        route->nexthop, route->neigh->ifp->ifindex,
262
                        new_metric, table);
263 264 265 266 267 268 269
}

int
kinstall_route(const struct babel_route *route)
{
    int rc;
    struct zone zone;
270 271
    const struct babel_route *rt1 = NULL;
    const struct babel_route *rt2 = NULL;
272
    struct route_stream *stream = NULL;
273
    int v4 = v4mapped(route->nexthop);
274 275 276 277 278

    debugf("install_route(%s from %s)\n",
           format_prefix(route->src->prefix, route->src->plen),
           format_prefix(route->src->src_prefix, route->src->src_plen));
    /* Install source-specific conflicting routes */
279 280 281 282 283
    if(kernel_disambiguate(v4)) {
        to_zone(route, &zone);
        rc = add_route(&zone, route);
        goto end;
    }
284

285
    stream = route_stream(ROUTE_INSTALLED);
286 287 288
    if(!stream) {
        fprintf(stderr, "Couldn't allocate route stream.\n");
        return -1;
289
    }
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    /* Install source-specific conflicting routes */
    while(1) {
        rt1 = route_stream_next(stream);
        if(rt1 == NULL) break;

        inter(route, rt1, &zone);
        if(!(conflicts(route, rt1) &&
             !is_installed(&zone) &&
             rt_cmp(rt1, min_conflict(&zone, route)) == 0))
            continue;
        rt2 = min_conflict(&zone, rt1);
        if(rt2 == NULL)
            add_route(&zone, min_route(route, rt1));
        else if(rt_cmp(route, rt2) < 0 && rt_cmp(route, rt1) < 0)
            chg_route(&zone, rt2, route);
    }
    route_stream_done(stream);
307 308

    /* Non conflicting case */
309 310 311 312 313 314
    to_zone(route, &zone);
    rt1 = conflict_solution(route);
    if(rt1 == NULL)
        rc = add_route(&zone, route);
    else
        rc = chg_route(&zone, rt1, route);
315
 end:
316 317 318 319 320 321 322 323 324 325 326 327 328 329
    if(rc < 0) {
        int save = errno;
        perror("kernel_route(ADD)");
        if(save != EEXIST)
            return -1;
    }
    return 0;
}

int
kuninstall_route(const struct babel_route *route)
{
    int rc;
    struct zone zone;
330
    const struct babel_route *rt1 = NULL, *rt2 = NULL;
331
    struct route_stream *stream = NULL;
332
    int v4 = v4mapped(route->nexthop);
333 334 335 336

    debugf("uninstall_route(%s from %s)\n",
           format_prefix(route->src->prefix, route->src->plen),
           format_prefix(route->src->src_prefix, route->src->src_plen));
337
    to_zone(route, &zone);
338 339 340 341 342 343 344
    if(kernel_disambiguate(v4)) {
        rc = del_route(&zone, route);
        if(rc < 0)
            perror("kernel_route(FLUSH)");
        return rc;
    }
    /* Remove the route, or change if the route was solving a conflict. */
345 346 347 348 349
    rt1 = conflict_solution(route);
    if(rt1 == NULL)
        rc = del_route(&zone, route);
    else
        rc = chg_route(&zone, route, rt1);
350 351 352 353
    if(rc < 0)
        perror("kernel_route(FLUSH)");

    /* Remove source-specific conflicting routes */
354
    stream = route_stream(ROUTE_INSTALLED);
355 356 357 358 359 360 361
    if(!stream) {
        fprintf(stderr, "Couldn't allocate route stream.\n");
        return -1;
    }
    while(1) {
        rt1 = route_stream_next(stream);
        if(rt1 == NULL) break;
362

363 364 365 366 367 368 369 370 371 372
        inter(route, rt1, &zone);
        if(!(conflicts(route, rt1) &&
             !is_installed(&zone) &&
             rt_cmp(rt1, min_conflict(&zone, route)) == 0))
            continue;
        rt2 = min_conflict(&zone, rt1);
        if(rt2 == NULL)
            del_route(&zone, min_route(route, rt1));
        else if(rt_cmp(route, rt2) < 0 && rt_cmp(route, rt1) < 0)
            chg_route(&zone, route, rt2);
373
    }
374
    route_stream_done(stream);
375 376 377 378 379 380 381

    return rc;
}

int
kswitch_routes(const struct babel_route *old, const struct babel_route *new)
{
382 383
    int rc;
    struct zone zone;
384 385 386 387 388 389
    struct babel_route *rt1 = NULL;
    struct route_stream *stream = NULL;

    debugf("switch_routes(%s from %s)\n",
           format_prefix(old->src->prefix, old->src->plen),
           format_prefix(old->src->src_prefix, old->src->src_plen));
390 391
    to_zone(old, &zone);
    rc = chg_route(&zone, old, new);
392 393 394 395 396 397
    if(rc < 0) {
        perror("kernel_route(MODIFY)");
        return -1;
    }

    /* Remove source-specific conflicting routes */
398
    if(!kernel_disambiguate(v4mapped(old->nexthop))) {
399
        stream = route_stream(ROUTE_INSTALLED);
400 401 402 403 404 405 406
        if(!stream) {
            fprintf(stderr, "Couldn't allocate route stream.\n");
            return -1;
        }
        while(1) {
            rt1 = route_stream_next(stream);
            if(rt1 == NULL) break;
407 408 409 410 411 412 413 414 415

            inter(old, rt1, &zone);
            if(!(conflicts(old, rt1) &&
                 !is_installed(&zone) &&
                 rt_cmp(rt1, min_conflict(&zone, old)) == 0 &&
                 rt_cmp(old, rt1) < 0 &&
                 rt_cmp(old, min_conflict(&zone, rt1)) == 0))
                continue;
            chg_route(&zone, old, new);
416 417
        }
        route_stream_done(stream);
418 419 420 421 422 423 424 425 426 427 428 429 430 431
    }

    return rc;
}

int
kchange_route_metric(const struct babel_route *route,
                     unsigned refmetric, unsigned cost, unsigned add)
{
    int old_metric = metric_to_kernel(route_metric(route));
    int new_metric = metric_to_kernel(MIN(refmetric + cost + add, INFINITY));
    int rc;
    struct babel_route *rt1 = NULL;
    struct route_stream *stream = NULL;
432
    struct zone zone;
433 434 435 436 437

    debugf("change_route_metric(%s from %s, %d -> %d)\n",
           format_prefix(route->src->prefix, route->src->plen),
           format_prefix(route->src->src_prefix, route->src->src_plen),
           old_metric, new_metric);
438 439
    to_zone(route, &zone);
    rc = chg_route_metric(&zone, route, old_metric, new_metric);
440 441 442 443 444
    if(rc < 0) {
        perror("kernel_route(MODIFY metric)");
        return -1;
    }

445
    if(!kernel_disambiguate(v4mapped(route->nexthop))) {
446
        stream = route_stream(ROUTE_INSTALLED);
447 448 449 450
        if(!stream) {
            fprintf(stderr, "Couldn't allocate route stream.\n");
            return -1;
        }
451

452 453 454
        while(1) {
            rt1 = route_stream_next(stream);
            if(rt1 == NULL) break;
455 456 457 458 459 460 461 462 463

            inter(route, rt1, &zone);
            if(!(conflicts(route, rt1) &&
                 !is_installed(&zone) &&
                 rt_cmp(rt1, min_conflict(&zone, route)) == 0 &&
                 rt_cmp(route, rt1) < 0 &&
                 rt_cmp(route, min_conflict(&zone, rt1)) == 0))
                continue;
            chg_route_metric(&zone, route, old_metric, new_metric);
464 465
        }
        route_stream_done(stream);
466 467 468 469
    }

    return rc;
}