filter.c 11.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 29 30
#include "babel.h"
#include "util.h"
#include "filter.h"

31 32 33 34 35 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 67 68 69 70
struct filter *input_filters = NULL;
struct filter *output_filters = NULL;
struct filter *redistribute_filters = NULL;

/* 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;
    if(c == '\n')
        return -2;
    do {
        if(i >= 255) return -2;
        buf[i++] = c;
        c = gnc(closure);
71
    } while(c != ' ' && c != '\t' && c != '\n' && c >= 0);
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
    buf[i] = '\0';
    *token_r = strdup(buf);
    return c;
}

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;
}

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;
    }
111 112
    free(t);

113
    ip = malloc(16);
114
    if(ip == NULL) {
115
        return -2;
116
    }
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    memcpy(ip, addr, 16);
    *ip_r = ip;
    if(af_r)
        *af_r = af;
    return c;
}

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;
    }
142
    free(t);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    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 error_filter;

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;
165
    filter->plen_le = 128;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

    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;
191 192
            filter->plen_ge = MAX(filter->plen_ge, p);
            filter->plen_le = MIN(filter->plen_le, p);
193 194 195 196 197
        } else if(strcmp(token, "le") == 0) {
            int p;
            c = getint(c, &p, gnc, closure);
            if(c < -1)
                goto error;
198
            filter->plen_le = MIN(filter->plen_le, p);
199 200 201 202 203
        } else if(strcmp(token, "ge") == 0) {
            int p;
            c = getint(c, &p, gnc, closure);
            if(c < -1)
                goto error;
204
            filter->plen_ge = MAX(filter->plen_ge, p);
205 206 207 208 209 210
        } else if(strcmp(token, "neigh") == 0) {
            unsigned char *neigh;
            c = getip(c, &neigh, NULL, gnc, closure);
            if(c < -1)
                goto error;
            filter->neigh = neigh;
211 212 213 214 215 216
        } else if(strcmp(token, "id") == 0) {
            unsigned char *id;
            c = getip(c, &id, NULL, gnc, closure);
            if(c < -1)
                goto error;
            filter->id = id;
217 218 219 220 221 222
        } else if(strcmp(token, "proto") == 0) {
            int proto;
            c = getint(c, &proto, gnc, closure);
            if(c < -1)
                goto error;
            filter->proto = proto;
223
        } else if(strcmp(token, "local") == 0) {
224
            filter->proto = RTPROT_BABEL_LOCAL;
225 226 227 228 229
        } else if(strcmp(token, "if") == 0) {
            char *interface;
            c = getword(c, &interface, gnc, closure);
            if(c < -1)
                goto error;
230
            filter->ifname = strdup(interface);
231 232 233 234 235 236 237 238
            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);
239
            if(c < -1) goto error;
240 241 242 243 244 245 246 247 248 249 250
            if(metric <= 0 || metric > INFINITY)
                goto error;
            filter->result = metric;
        } else if(strcmp(token, "inherit") == 0) {
            filter->result = METRIC_INHERIT;
        } else {
            goto error;
        }
        free(token);
    }
    if(filter->af == 0) {
251
        if(filter->plen_le < 128 || filter->plen_ge > 0)
252 253
            filter->af = AF_INET6;
    } else if(filter->af == AF_INET) {
254 255
        filter->plen_le += 96;
        filter->plen_ge += 96;
256 257 258 259 260 261 262 263 264 265 266 267 268 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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    }
    return filter;
 error:
    free(filter);
    return &error_filter;
}

static void
add_filter(struct filter *filter, struct filter **filters)
{
    struct filter *f;
    if(*filters == NULL) {
        filter->next = NULL;
        *filters = filter;
    } else {
        f = *filters;
        while(f->next)
            f = f->next;
        filter->next = NULL;
        f->next = filter;
    }
}

static int
parse_config(gnc_t gnc, void *closure)
{
    int kind;
    int c;
    char *token;
    struct filter *filter;
    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) {
            kind = 1;
        } else if(strcmp(token, "out") == 0) {
            kind = 2;
        } else if(strcmp(token, "redistribute") == 0) {
            kind = 3;
        } else {
            return -1;
        }
315
        free(token);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

        filter = parse_filter(gnc, closure);
        if(filter == &error_filter)
            return -1;

        if(filter == NULL)
            continue;

        if(kind == 1)
            add_filter(filter, &input_filters);
        else if(kind == 2)
            add_filter(filter, &output_filters);
        else if(kind == 3)
            add_filter(filter, &redistribute_filters);
        else
            return -1;
    }
    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);
}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
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);
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
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) {
        if(!id || memcmp(f->id, id, 16) != 0)
            return 0;
    }
    if(f->prefix) {
406
        if(!prefix || plen < f->plen || !in_prefix(prefix, f->prefix, f->plen))
407 408
            return 0;
    }
409
    if(f->plen_ge > 0 || f->plen_le < 128) {
410 411
        if(!prefix)
            return 0;
412
        if(plen > f->plen_le)
413
            return 0;
414
        if(plen < f->plen_ge)
415 416 417 418 419 420
            return 0;
    }
    if(f->neigh) {
        if(!neigh || memcmp(f->neigh, neigh, 16) != 0)
            return 0;
    }
421 422 423
    if(f->ifname) {
        if(!f->ifindex)         /* no such interface */
            return 0;
424 425 426 427 428 429
        if(!ifindex || f->ifindex != ifindex)
            return 0;
    }
    if(f->proto) {
        if(!proto || f->proto != proto)
            return 0;
430
    } else if(proto == RTPROT_BABEL_LOCAL) {
431
        return 0;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    }
    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;
}

449
int
450 451 452
input_filter(const unsigned char *id,
             const unsigned char *prefix, unsigned short plen,
             const unsigned char *neigh, unsigned int ifindex)
453
{
454 455 456 457 458
    int res;
    res = do_filter(input_filters, id, prefix, plen, neigh, ifindex, 0);
    if(res < 0 || res == METRIC_INHERIT)
        res = 0;
    return res;
459 460 461
}

int
462 463
output_filter(const unsigned char *id, const unsigned char *prefix,
              unsigned short plen, unsigned int ifindex)
464
{
465 466 467 468 469
    int res;
    res = do_filter(output_filters, id, prefix, plen, NULL, ifindex, 0);
    if(res < 0 || res == METRIC_INHERIT)
        res = 0;
    return res;
470 471
}

472 473 474 475 476 477 478 479 480 481 482
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;
}
483 484 485 486 487 488 489 490 491 492 493 494 495 496

int
finalise_filters()
{
    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);

    return 1;
}