config.c 16.9 KB
Newer Older
1
/*
2
Copyright (c) 2007, 2008 by Juliusz Chroboczek
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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.
*/

23 24 25 26
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

27 28
#include "babel.h"
#include "util.h"
29
#include "network.h"
30
#include "config.h"
31

32 33 34
struct filter *input_filters = NULL;
struct filter *output_filters = NULL;
struct filter *redistribute_filters = NULL;
35
struct network_conf *network_confs = NULL;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

/* get_next_char callback */
typedef int (*gnc_t)(void*);

static int
skip_whitespace(int c, gnc_t gnc, void *closure)
{
    while(c == ' ' || c == '\t')
        c = gnc(closure);
    return c;
}

static int
skip_to_eol(int c, gnc_t gnc, void *closure)
{
    while(c != '\n' && c >= 0)
        c = gnc(closure);
    if(c == '\n')
        c = gnc(closure);
    return c;
}

static int
getword(int c, char **token_r, gnc_t gnc, void *closure)
{
    char buf[256];
    int i = 0;

    c = skip_whitespace(c, gnc, closure);
    if(c < 0)
        return c;
67
    if(c == '"' || c == '\n')
68 69 70 71 72
        return -2;
    do {
        if(i >= 255) return -2;
        buf[i++] = c;
        c = gnc(closure);
73
    } while(c != ' ' && c != '\t' && c != '\n' && c >= 0);
74 75 76 77 78
    buf[i] = '\0';
    *token_r = strdup(buf);
    return c;
}

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
static int
getstring(int c, char **token_r, gnc_t gnc, void *closure)
{
    char buf[256];
    int i = 0;

    c = skip_whitespace(c, gnc, closure);
    if(c < 0)
        return c;
    if(c == '\n')
        return -2;

    /* Unquoted strings have the same syntax as words. */
    if(c != '"')
        return getword(c, token_r, gnc, closure);

    c = gnc(closure);

    while(1) {
        if(i >= 255 || c == '\n') return -2;
        if(c == '"') {
            c = gnc(closure);
            break;
        }
        if(c == '\\')
            c = gnc(closure);
        buf[i++] = c;
        c = gnc(closure);
    }

    buf[i] = '\0';
    *token_r = strdup(buf);
    return c;
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static int
getint(int c, int *int_r, gnc_t gnc, void *closure)
{
    char *t, *end;
    int i;
    c = getword(c, &t, gnc, closure);
    if(c < -1)
        return c;
    i = strtol(t, &end, 0);
    if(*end != '\0') {
        free(t);
        return -2;
    }
    free(t);
    *int_r = i;
    return c;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
static int
getmsec(int c, int *int_r, gnc_t gnc, void *closure)
{
    char *t;
    int i;
    c = getword(c, &t, gnc, closure);
    if(c < -1)
        return c;
    i = parse_msec(t);
    if(i < 0) {
        free(t);
        return -2;
    }
    free(t);
    *int_r = i;
    return c;
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
150 151 152 153 154 155 156 157 158 159 160 161
static int
getbool(int c, int *int_r, gnc_t gnc, void *closure)
{
    char *t;
    int i;
    c = getword(c, &t, gnc, closure);
    if(c < -1)
        return c;
    if(strcmp(t, "true") == 0 || strcmp(t, "yes") == 0)
        i = CONFIG_YES;
    else if(strcmp(t, "false") == 0 || strcmp(t, "no") == 0)
        i = CONFIG_NO;
162
    else if(strcmp(t, "default") == 0 || strcmp(t, "auto") == 0)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
163 164 165 166 167 168 169 170 171 172
        i = CONFIG_DEFAULT;
    else {
        free(t);
        return -2;
    }
    free(t);
    *int_r = i;
    return c;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
static int
getip(int c, unsigned char **ip_r, int *af_r, gnc_t gnc, void *closure)
{
    char *t;
    unsigned char *ip;
    unsigned char addr[16];
    int af, rc;

    c = getword(c, &t, gnc, closure);
    if(c < -1)
        return c;
    rc = parse_address(t, addr, &af);
    if(rc < 0) {
        free(t);
        return -2;
    }
189 190
    free(t);

191
    ip = malloc(16);
192
    if(ip == NULL) {
193
        return -2;
194
    }
195 196 197 198 199 200 201
    memcpy(ip, addr, 16);
    *ip_r = ip;
    if(af_r)
        *af_r = af;
    return c;
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static int
getid(int c, unsigned char **id_r, gnc_t gnc, void *closure)
{
    char *t;
    unsigned char *idp;
    unsigned char id[8];
    int rc;

    c = getword(c, &t, gnc, closure);
    if(c < -1)
        return c;
    rc = parse_eui64(t, id);
    if(rc < 0) {
        free(t);
        return -2;
    }
    free(t);

    idp = malloc(8);
    if(idp == NULL) {
        return -2;
    }
    memcpy(idp, id, 8);
    *id_r = idp;
    return c;
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
static int
getnet(int c, unsigned char **p_r, unsigned char *plen_r, int *af_r,
          gnc_t gnc, void *closure)
{
    char *t;
    unsigned char *ip;
    unsigned char addr[16];
    unsigned char plen;
    int af, rc;

    c = getword(c, &t, gnc, closure);
    if(c < -1)
        return c;
    rc = parse_net(t, addr, &plen, &af);
    if(rc < 0) {
        free(t);
        return -2;
    }
247
    free(t);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    ip = malloc(16);
    if(ip == NULL)
        return -2;
    memcpy(ip, addr, 16);
    *p_r = ip;
    *plen_r = plen;
    if(af_r) *af_r = af;
    return c;
}

static struct filter *
parse_filter(gnc_t gnc, void *closure)
{
    int c;
    char *token;
    struct filter *filter;

    filter = calloc(1, sizeof(struct filter));
    if(filter == NULL)
        goto error;
268
    filter->plen_le = 128;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

    c = gnc(closure);
    if(c < -1)
        goto error;

    while(c >= 0 && c != '\n') {
        c = skip_whitespace(c, gnc, closure);
        if(c == '#') {
            c = skip_to_eol(c, gnc, closure);
            break;
        }
        c = getword(c, &token, gnc, closure);
        if(c < -1)
            goto error;

        if(strcmp(token, "ip") == 0) {
            c = getnet(c, &filter->prefix, &filter->plen, &filter->af,
                       gnc, closure);
            if(c < -1)
                goto error;
        } else if(strcmp(token, "eq") == 0) {
            int p;
            c = getint(c, &p, gnc, closure);
            if(c < -1)
                goto error;
294 295
            filter->plen_ge = MAX(filter->plen_ge, p);
            filter->plen_le = MIN(filter->plen_le, p);
296 297 298 299 300
        } else if(strcmp(token, "le") == 0) {
            int p;
            c = getint(c, &p, gnc, closure);
            if(c < -1)
                goto error;
301
            filter->plen_le = MIN(filter->plen_le, p);
302 303 304 305 306
        } else if(strcmp(token, "ge") == 0) {
            int p;
            c = getint(c, &p, gnc, closure);
            if(c < -1)
                goto error;
307
            filter->plen_ge = MAX(filter->plen_ge, p);
308 309 310 311 312 313
        } else if(strcmp(token, "neigh") == 0) {
            unsigned char *neigh;
            c = getip(c, &neigh, NULL, gnc, closure);
            if(c < -1)
                goto error;
            filter->neigh = neigh;
314 315
        } else if(strcmp(token, "id") == 0) {
            unsigned char *id;
316
            c = getid(c, &id, gnc, closure);
317 318 319
            if(c < -1)
                goto error;
            filter->id = id;
320 321 322 323 324 325
        } else if(strcmp(token, "proto") == 0) {
            int proto;
            c = getint(c, &proto, gnc, closure);
            if(c < -1)
                goto error;
            filter->proto = proto;
326
        } else if(strcmp(token, "local") == 0) {
327
            filter->proto = RTPROT_BABEL_LOCAL;
328 329
        } else if(strcmp(token, "if") == 0) {
            char *interface;
330
            c = getstring(c, &interface, gnc, closure);
331 332
            if(c < -1)
                goto error;
333
            filter->ifname = interface;
334 335 336 337 338 339 340 341
            filter->ifindex = if_nametoindex(interface);
        } else if(strcmp(token, "allow") == 0) {
            filter->result = 0;
        } else if(strcmp(token, "deny") == 0) {
            filter->result = INFINITY;
        } else if(strcmp(token, "metric") == 0) {
            int metric;
            c = getint(c, &metric, gnc, closure);
342
            if(c < -1) goto error;
343 344 345 346 347 348 349 350 351
            if(metric <= 0 || metric > INFINITY)
                goto error;
            filter->result = metric;
        } else {
            goto error;
        }
        free(token);
    }
    if(filter->af == 0) {
352
        if(filter->plen_le < 128 || filter->plen_ge > 0)
353 354
            filter->af = AF_INET6;
    } else if(filter->af == AF_INET) {
355 356
        filter->plen_le += 96;
        filter->plen_ge += 96;
357 358
    }
    return filter;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
359

360 361
 error:
    free(filter);
362
    return NULL;
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
static struct network_conf *
parse_nconf(gnc_t gnc, void *closure)
{
    int c;
    char *token;
    struct network_conf *nconf;

    nconf = calloc(1, sizeof(struct network_conf));
    if(nconf == NULL)
        goto error;

    c = gnc(closure);
    if(c < -1)
        goto error;

    c = skip_whitespace(c, gnc, closure);
    if(c < -1 || c == '#')
        goto error;

    c = getstring(c, &token, gnc, closure);
    if(c < -1 || token == NULL)
        goto error;

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
388
    nconf->ifname = token;
389 390 391 392 393 394 395 396 397 398 399 400

    while(c >= 0 && c != '\n') {
        c = skip_whitespace(c, gnc, closure);

        if(c == '#') {
            c = skip_to_eol(c, gnc, closure);
            break;
        }
        c = getword(c, &token, gnc, closure);
        if(c < -1)
            goto error;

401 402 403 404 405 406
        if(strcmp(token, "rxcost") == 0) {
            int cost;
            c = getint(c, &cost, gnc, closure);
            if(c < -1 || cost <= 0 || cost > 0xFFFF)
                goto error;
            nconf->cost = cost;
407 408
        } else if(strcmp(token, "hello-interval") == 0) {
            int interval;
409
            c = getmsec(c, &interval, gnc, closure);
410
            if(c < -1 || interval <= 0 || interval > 10 * 0xFFFF)
411 412
                goto error;
            nconf->hello_interval = interval;
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
        } else if(strcmp(token, "wired") == 0) {
            int v;
            c = getbool(c, &v, gnc, closure);
            if(c < -1)
                goto error;
            nconf->wired = v;
        } else if(strcmp(token, "link-quality") == 0) {
            int v;
            c = getbool(c, &v, gnc, closure);
            if(c < -1)
                goto error;
            nconf->lq = v;
        } else if(strcmp(token, "split-horizon") == 0) {
            int v;
            c = getbool(c, &v, gnc, closure);
            if(c < -1)
                goto error;
            nconf->split_horizon = v;
431 432 433
        } else {
            goto error;
        }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
434
        free(token);
435 436 437 438 439 440 441 442 443
    }

    return nconf;

 error:
    free(nconf);
    return NULL;
}

444 445 446 447 448 449 450
static void
add_filter(struct filter *filter, struct filter **filters)
{
    if(*filters == NULL) {
        filter->next = NULL;
        *filters = filter;
    } else {
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
451
        struct filter *f;
452 453 454 455 456 457 458 459
        f = *filters;
        while(f->next)
            f = f->next;
        filter->next = NULL;
        f->next = filter;
    }
}

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
static void
merge_nconf(struct network_conf *dest, struct network_conf *src)
{
    return;
}

static void
add_nconf(struct network_conf *nconf, struct network_conf **nconfs)
{
    if(*nconfs == NULL) {
        nconf->next = NULL;
        *nconfs = nconf;
    } else {
        struct network_conf *n;
        n = *nconfs;
        while(n->next) {
            if(strcmp(n->ifname, nconf->ifname) == 0) {
                merge_nconf(n, nconf);
                free(nconf);
                return;
            }
            n = n->next;
        }
        nconf->next = NULL;
        n->next = nconf;
    }
}

488 489 490 491 492
static int
parse_config(gnc_t gnc, void *closure)
{
    int c;
    char *token;
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    c = gnc(closure);
    if(c < 2)
        return -1;

    while(c >= 0) {
        c = skip_whitespace(c, gnc, closure);
        if(c == '#') {
            c = skip_to_eol(c, gnc, closure);
            continue;
        }
        if(c == '\n') {
            c = gnc(closure);
            continue;
        }
        if(c < 0)
            break;
        c = getword(c, &token, gnc, closure);
        if(c < -1)
            return -1;

        if(strcmp(token, "in") == 0) {
515 516 517 518 519
            struct filter *filter;
            filter = parse_filter(gnc, closure);
            if(filter == NULL)
                return -1;
            add_filter(filter, &input_filters);
520
        } else if(strcmp(token, "out") == 0) {
521 522 523 524 525
            struct filter *filter;
            filter = parse_filter(gnc, closure);
            if(filter == NULL)
                return -1;
            add_filter(filter, &output_filters);
526
        } else if(strcmp(token, "redistribute") == 0) {
527 528 529 530 531 532 533 534 535 536 537
            struct filter *filter;
            filter = parse_filter(gnc, closure);
            if(filter == NULL)
                return -1;
            add_filter(filter, &redistribute_filters);
        } else if(strcmp(token, "interface") == 0) {
            struct network_conf *nconf;
            nconf = parse_nconf(gnc, closure);
            if(nconf == NULL)
                return -1;
            add_nconf(nconf, &network_confs);
538 539 540
        } else {
            return -1;
        }
541
        free(token);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    }
    return 1;
}

int
parse_config_from_file(char *filename)
{
    FILE *f;
    int rc;

    f = fopen(filename, "r");
    if(f == NULL)
        return -1;
    rc = parse_config((gnc_t)fgetc, f);
    fclose(f);
    return rc;
}

struct string_state {
    char *string;
    int n;
};

static int
gnc_string(struct string_state *s)
{
    if(s->string[s->n] == '\0')
        return -1;
    else
        return s->string[s->n++];
}

int
parse_config_from_string(char *string)
{
    struct string_state s = { string, 0 };
    return parse_config((gnc_t)gnc_string, &s);
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
static void
renumber_filter(struct filter *filter)
{
    while(filter) {
        if(filter->ifname)
            filter->ifindex = if_nametoindex(filter->ifname);
        filter = filter->next;
    }
}

void
renumber_filters()
{
    renumber_filter(input_filters);
    renumber_filter(output_filters);
    renumber_filter(redistribute_filters);
}

599 600 601 602 603 604 605 606 607 608 609 610 611
static int
filter_match(struct filter *f, const unsigned char *id,
             const unsigned char *prefix, unsigned short plen,
             const unsigned char *neigh, unsigned int ifindex, int proto)
{
    if(f->af) {
        if(plen >= 96 && v4mapped(prefix)) {
            if(f->af == AF_INET6) return 0;
        } else {
            if(f->af == AF_INET) return 0;
        }
    }
    if(f->id) {
612
        if(!id || memcmp(f->id, id, 8) != 0)
613 614 615
            return 0;
    }
    if(f->prefix) {
616
        if(!prefix || plen < f->plen || !in_prefix(prefix, f->prefix, f->plen))
617 618
            return 0;
    }
619
    if(f->plen_ge > 0 || f->plen_le < 128) {
620 621
        if(!prefix)
            return 0;
622
        if(plen > f->plen_le)
623
            return 0;
624
        if(plen < f->plen_ge)
625 626 627 628 629 630
            return 0;
    }
    if(f->neigh) {
        if(!neigh || memcmp(f->neigh, neigh, 16) != 0)
            return 0;
    }
631 632 633
    if(f->ifname) {
        if(!f->ifindex)         /* no such interface */
            return 0;
634 635 636 637 638 639
        if(!ifindex || f->ifindex != ifindex)
            return 0;
    }
    if(f->proto) {
        if(!proto || f->proto != proto)
            return 0;
640
    } else if(proto == RTPROT_BABEL_LOCAL) {
641
        return 0;
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    }
    return 1;
}

static int
do_filter(struct filter *f, const unsigned char *id,
          const unsigned char *prefix, unsigned short plen,
          const unsigned char *neigh, unsigned int ifindex, int proto)
{
    while(f) {
        if(filter_match(f, id, prefix, plen, neigh, ifindex, proto))
            return f->result;
        f = f->next;
    }
    return -1;
}

659
int
660 661 662
input_filter(const unsigned char *id,
             const unsigned char *prefix, unsigned short plen,
             const unsigned char *neigh, unsigned int ifindex)
663
{
664 665
    int res;
    res = do_filter(input_filters, id, prefix, plen, neigh, ifindex, 0);
666
    if(res < 0)
667 668
        res = 0;
    return res;
669 670 671
}

int
672 673
output_filter(const unsigned char *id, const unsigned char *prefix,
              unsigned short plen, unsigned int ifindex)
674
{
675 676
    int res;
    res = do_filter(output_filters, id, prefix, plen, NULL, ifindex, 0);
677
    if(res < 0)
678 679
        res = 0;
    return res;
680 681
}

682 683 684 685 686 687 688 689 690 691 692
int
redistribute_filter(const unsigned char *prefix, unsigned short plen,
                    unsigned int ifindex, int proto)
{
    int res;
    res = do_filter(redistribute_filters, NULL, prefix, plen, NULL,
                    ifindex, proto);
    if(res < 0)
        res = INFINITY;
    return res;
}
693 694

int
695
finalise_config()
696 697 698 699 700 701 702 703 704
{
    struct filter *filter = calloc(1, sizeof(struct filter));
    if(filter == NULL)
        return -1;

    filter->proto = RTPROT_BABEL_LOCAL;
    filter->plen_le = 128;
    add_filter(filter, &redistribute_filters);

705 706 707 708 709 710 711 712 713 714 715 716 717
    while(network_confs) {
        struct network_conf *n;
        void *vrc;
        n = network_confs;
        network_confs = network_confs->next;
        n->next = NULL;
        vrc = add_network(n->ifname, n);
        if(vrc == NULL) {
            fprintf(stderr, "Couldn't add interface %s.\n", n->ifname);
            return -1;
        }
    }

718 719
    return 1;
}