xroute.c 5.53 KB
Newer Older
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
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) 2007 by 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 <stdio.h>
#include <string.h>
#include <errno.h>
27
#include <assert.h>
28
#include <netinet/in.h>
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
29

30
#include "babel.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
31 32
#include "kernel.h"
#include "neighbour.h"
33
#include "message.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
34 35
#include "route.h"
#include "xroute.h"
36
#include "util.h"
37
#include "filter.h"
38
#include "network.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
39 40 41 42 43

struct xroute xroutes[MAXXROUTES];
int numxroutes = 0;

struct xroute *
44
find_xroute(const unsigned char *prefix, unsigned char plen)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
45 46 47
{
    int i;
    for(i = 0; i < numxroutes; i++) {
48 49 50
        if(xroutes[i].plen == plen &&
           memcmp(xroutes[i].prefix, prefix, 16) == 0)
            return &xroutes[i];
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
51
    }
52
    return NULL;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
53 54
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68
void
flush_xroute(struct xroute *xroute)
{
    int n;

    n = xroute - xroutes;
    assert(n >= 0 && n < numxroutes);

    if(n != numxroutes - 1)
        memcpy(xroutes + n, xroutes + numxroutes - 1, sizeof(struct xroute));
    numxroutes--;
    VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute));
}

69
int
70
add_xroute(int kind, unsigned char prefix[16], unsigned char plen,
71
           unsigned short metric, unsigned int ifindex, int proto)
72
{
73 74
    struct xroute *xroute = find_xroute(prefix, plen);
    if(xroute) {
75
        if(xroute->kind < kind)
76
            return 0;
77 78 79 80 81 82
        else if(xroute->kind > kind) {
            flush_xroute(xroute);
            return add_xroute(kind, prefix, plen, metric, ifindex, proto);
        }

        if(xroute->metric <= metric)
83 84 85 86
            return 0;
        xroute->metric = metric;
        return 1;
    }
87

88 89
    if(numxroutes >= MAXXROUTES)
        return -1;
90

91
    xroutes[numxroutes].kind = kind;
92 93 94 95 96 97 98 99
    memcpy(xroutes[numxroutes].prefix, prefix, 16);
    xroutes[numxroutes].plen = plen;
    xroutes[numxroutes].metric = metric;
    xroutes[numxroutes].ifindex = ifindex;
    xroutes[numxroutes].proto = proto;
    numxroutes++;
    return 1;
}
100

101 102 103
int
check_xroutes()
{
104
    int i, j, metric, export, change = 0, rc;
105
    struct kernel_route routes[240];
106
    int numroutes;
107

108 109
    debugf("\nChecking kernel routes.\n");

110
    rc = kernel_addresses(routes, 240);
111
    if(rc < 0) {
112
        perror("kernel_addresses");
113
        numroutes = 0;
114
    } else {
115
        numroutes = rc;
116 117
    }

118 119 120 121 122 123 124
    if(numroutes <= 240) {
        rc = kernel_routes(routes + numroutes, 240 - numroutes);
        if(rc < 0) {
            fprintf(stderr, "Couldn't get kernel routes.\n");
        } else {
            numroutes += rc;
        }
125
    } else {
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
126 127
        fprintf(stderr,
                "Too many local addresses -- ignoring kernel routes.\n");
128 129 130
    }

    /* Check for any routes that need to be flushed */
131

132 133
    i = 0;
    while(i < numxroutes) {
134
        export = 0;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        if(xroutes[i].kind == XROUTE_FORCED) {
            export = 1;
        } else if(xroutes[i].kind == XROUTE_REDISTRIBUTED) {
            metric = redistribute_filter(xroutes[i].prefix, xroutes[i].plen,
                                         xroutes[i].ifindex, xroutes[i].proto);
            if((metric < INFINITY && metric == xroutes[i].metric) ||
               metric == METRIC_INHERIT) {
                for(j = 0; j < numroutes; j++) {
                    if(xroutes[i].plen == routes[j].plen &&
                       memcmp(xroutes[i].prefix, routes[j].prefix, 16) == 0 &&
                       xroutes[i].ifindex == routes[j].ifindex &&
                       xroutes[i].proto == routes[j].proto) {
                        if(metric < INFINITY) {
                            export = 1;
                            break;
                        } else if(metric == METRIC_INHERIT &&
151
                           xroutes[i].metric == routes[j].metric * 256) {
152 153 154
                            export = 1;
                            break;
                        }
155 156
                    }
                }
157 158
            }
        }
159

160 161
        if(!export) {
            flush_xroute(&xroutes[i]);
162
            change = 1;
163 164 165 166 167
        } else {
            i++;
        }
    }

168 169 170
    /* Add any new routes */

    for(i = 0; i < numroutes; i++) {
171 172
        if(martian_prefix(routes[i].prefix, routes[i].plen))
            continue;
173 174 175
        metric = redistribute_filter(routes[i].prefix, routes[i].plen,
                                     routes[i].ifindex, routes[i].proto);
        if(metric == METRIC_INHERIT)
176
            metric = routes[i].metric * 256;
177
        if(metric < INFINITY) {
178 179
            rc = add_xroute(XROUTE_REDISTRIBUTED,
                            routes[i].prefix, routes[i].plen,
180 181 182
                            metric, routes[i].ifindex, routes[i].proto);
            if(rc)
                change = 1;
183 184 185 186
        }
    }
    return change;
}