rule.c 8.17 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
/*
Copyright (c) 2015 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
27
#include <sys/time.h>
28 29 30 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 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 115 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 142 143 144 145 146 147 148

#include "babeld.h"
#include "util.h"
#include "kernel.h"
#include "configuration.h"
#include "rule.h"

int src_table_idx = 10;
int src_table_prio = 100;

/* The table used for non-specific routes is "export_table", therefore, we can
   take the convention of plen == 0 <=> empty table. */
struct rule {
    unsigned char src[16];
    unsigned char plen;
    unsigned char table;
};

/* rules contains informations about the rules we installed. It is an array
   indexed by: <table priority> - src_table_prio.
   (First entries are the most specific, since they have priority.) */
static struct rule rules[SRC_TABLE_NUM];
/* used tables is indexed by: <table number> - src_table_idx
   used_tables[i] == 1 <=> the table number (i + src_table_idx) is used */
static char used_tables[SRC_TABLE_NUM] = {0};

static int
get_free_table(void)
{
    int i;
    for(i = 0; i < SRC_TABLE_NUM; i++)
        if(!used_tables[i]) {
            used_tables[i] = 1;
            return i + src_table_idx;
        }
    return -1;
}

static void
release_table(int i)
{
    used_tables[i - src_table_idx] = 0;
}

static int
find_hole_around(int i)
{
    int j;
    for(j = i; j < SRC_TABLE_NUM; j++)
        if(rules[j].plen == 0)
            return j;
    for(j = i - 1; j >= 0; j--)
        if(rules[j].plen == 0)
            return j;
    return -1;
}

static int
shift_rule(int from, int to)
{
    int dec = (from < to) ? 1 /* right */ : -1 /* left */;
    int rc;
    while(to != from) {
        to -= dec;
        rc = change_rule(to + dec + src_table_prio, to + src_table_prio,
                         rules[to].src, rules[to].plen, rules[to].table);
        if(rc < 0) {
            perror("change_table_priority");
            return -1;
        }
        rules[to+dec] = rules[to];
        rules[to].plen = 0;
    }
    return 0;
}

/* Return a new table at index [idx] of rules.  If cell at that index is not
   free, we need to shift cells (and rules).  If it's full, return NULL. */
static struct rule *
insert_table(const unsigned char *src, unsigned short src_plen, int idx)
{
    int table;
    int rc;
    int hole;

    if(idx < 0 || idx >= SRC_TABLE_NUM) {
        fprintf(stderr, "Incorrect table number %d\n", idx);
        return NULL;
    }

    table = get_free_table();
    if(table < 0) {
        kdebugf("All allowed routing tables are used!\n");
        return NULL;
    }

    hole = find_hole_around(idx);
    if(hole < 0) {
        fprintf(stderr, "Have free table but not free rule.\n");
        goto fail;
    }
    rc = shift_rule(idx, hole);
    if(rc < 0)
        goto fail;

    rc = add_rule(idx + src_table_prio, src, src_plen, table);
    if(rc < 0) {
        perror("add rule");
        goto fail;
    }
    memcpy(rules[idx].src, src, 16);
    rules[idx].plen = src_plen;
    rules[idx].table = table;

    return &rules[idx];
 fail:
    release_table(table);
    return NULL;
}

/* Sorting rules in a well ordered fashion will increase code complexity and
Matthieu Boutier committed
149
   decrease performances, because more rule shifts will be required, so more
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
   system calls invoked. */
static int
find_table_slot(const unsigned char *src, unsigned short src_plen, int *found)
{
    struct rule *kr = NULL;
    int i;
    *found = 0;

    for(i = 0; i < SRC_TABLE_NUM; i++) {
        kr = &rules[i];
        if(kr->plen == 0)
            return i;
        switch(prefix_cmp(src, src_plen, kr->src, kr->plen)) {
        case PST_LESS_SPECIFIC:
        case PST_DISJOINT:
            continue;
        case PST_MORE_SPECIFIC:
            return i;
        case PST_EQUALS:
            *found = 1;
            return i;
        }
    }

    return -1;
}

int
178 179
find_table(const unsigned char *dest, unsigned short plen,
           const unsigned char *src, unsigned short src_plen)
180 181 182 183
{
    struct rule *kr = NULL;
    int i, found;

184
    if(is_default(src, src_plen)) {
185 186 187
        return export_table;
    } else if(kernel_disambiguate(v4mapped(dest))) {
        return export_table;
188 189 190 191 192 193 194 195 196 197 198 199 200 201 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 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 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
    }

    i = find_table_slot(src, src_plen, &found);
    if(found)
        return rules[i].table;
    if(i < 0)
        return -1;
    kr = insert_table(src, src_plen, i);
    return kr == NULL ? -1 : kr->table;
}

void
release_tables(void)
{
    int i;
    for(i = 0; i < SRC_TABLE_NUM; i++) {
        if(rules[i].plen != 0) {
            flush_rule(i + src_table_prio,
                       v4mapped(rules[i].src) ? AF_INET : AF_INET6);
            rules[i].plen = 0;
        }
        used_tables[i] = 0;
    }
}

static int
filter_rule(struct kernel_rule *rule, void *data)
{
    int i;
    char (*rule_exists)[2][SRC_TABLE_NUM] = data;
    int is_v4 = v4mapped(rule->src);
    int r = is_v4 ? 0 : 1;

    if(martian_prefix(rule->src, rule->src_plen))
        return 0;

    i = rule->priority - src_table_prio;

    if(i < 0 || SRC_TABLE_NUM <= i)
        return 0;

    if(prefix_cmp(rule->src, rule->src_plen,
                  rules[i].src, rules[i].plen) == PST_EQUALS &&
       rule->table == rules[i].table &&
       (*rule_exists)[r][i] == 0)
        (*rule_exists)[r][i] = 1;
    else
        (*rule_exists)[r][i] = -1;

    return 1;
}

/* This functions should be executed wrt the code just bellow: [rule_exists]
   contains is a boolean array telling whether the rules we should have
   installed in the kernel are installed or not.  If they aren't, then reinstall
   them (this can append when rules are modified by third parties). */

static void
install_missing_rules(char rule_exists[SRC_TABLE_NUM], int v4)
{
    int i, rc;
    for(i = 0; i < SRC_TABLE_NUM; i++) {
        int priority = i + src_table_prio;
        if(rule_exists[i] == 1)
            continue;

        if(rule_exists[i] != 0) {
            int rc;
            do {
                rc = flush_rule(priority, v4 ? AF_INET : AF_INET6);
            } while(rc >= 0);
            if(errno != ENOENT && errno != EEXIST)
                fprintf(stderr,
                        "Cannot remove rule %d: from %s table %d (%s)\n",
                        priority,
                        format_prefix(rules[i].src, rules[i].plen),
                        rules[i].table, strerror(errno));
        }

        /* Be wise, our priority are both for v4 and v6 (does not overlap). */
        if(!!v4mapped(rules[i].src) == !!v4 && rules[i].plen != 0) {
            rc = add_rule(priority, rules[i].src,
                          rules[i].plen, rules[i].table);
            if(rc < 0)
                fprintf(stderr,
                        "Cannot install rule %d: from %s table %d (%s)\n",
                        priority,
                        format_prefix(rules[i].src, rules[i].plen),
                        rules[i].table, strerror(errno));
        }
    }
}

int
check_rules(void)
{
    int rc;
    char rule_exists[2][SRC_TABLE_NUM]; /* v4, v6 */
    struct kernel_filter filter = {0};
    filter.rule = filter_rule;
    filter.rule_closure = (void*) rule_exists;
    memset(rule_exists, 0, sizeof(rule_exists));

    rc = kernel_dump(CHANGE_RULE, &filter);
    if(rc < 0)
        return -1;
    install_missing_rules(rule_exists[0], 1);
    install_missing_rules(rule_exists[1], 0);

    return 0;
}