From 5734509c0545ebd95a5b8e3f22a911c1a39ffa1b Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Sun, 25 Dec 2011 17:52:09 +0100 Subject: babeld: Initial import, for Babel routing protocol. * Initial import of the Babel routing protocol, ported to Quagga. * LICENCE: Update the original LICENCE file to include all known potentially applicable copyright claims. Ask that any future contributors to babeld/ grant MIT/X11 licence to their work. * *.{c,h}: Add GPL headers, in according with the SFLC guidance on dealing with potentially mixed GPL/other licensed work, at: https://www.softwarefreedom.org/resources/2007/gpl-non-gpl-collaboration.html --- babeld/xroute.c | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 babeld/xroute.c (limited to 'babeld/xroute.c') diff --git a/babeld/xroute.c b/babeld/xroute.c new file mode 100644 index 00000000..42f83d07 --- /dev/null +++ b/babeld/xroute.c @@ -0,0 +1,226 @@ +/* + * This file is free software: you may copy, redistribute and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 2 of the License, or (at your + * option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * This file incorporates work covered by the following copyright and + * permission notice: + * +Copyright (c) 2007, 2008 by Juliusz Chroboczek +Copyright 2011 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 +#include +#include +#include +#include +#include +#include + +#include +#include "if.h" +#include "log.h" + +#include "babeld.h" +#include "kernel.h" +#include "neighbour.h" +#include "message.h" +#include "route.h" +#include "xroute.h" +#include "util.h" +#include "babel_interface.h" + +struct xroute *xroutes; +int numxroutes = 0; +int maxxroutes = 0; + +/* Add redistributed route to Babel table. */ +int +babel_ipv4_route_add (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix, + unsigned int ifindex, struct in_addr *nexthop) +{ + unsigned char uchar_prefix[16]; + + inaddr_to_uchar(uchar_prefix, &prefix->prefix); + debugf(BABEL_DEBUG_ROUTE, "Adding new ipv4 route comming from Zebra."); + xroute_add_new_route(uchar_prefix, prefix->prefixlen, api->metric, ifindex, + 0, 1); + return 0; +} + +/* Remove redistributed route from Babel table. */ +int +babel_ipv4_route_delete (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix, + unsigned int ifindex) +{ + unsigned char uchar_prefix[16]; + struct xroute *xroute = NULL; + + inaddr_to_uchar(uchar_prefix, &prefix->prefix); + xroute = find_xroute(uchar_prefix, prefix->prefixlen); + if (xroute != NULL) { + debugf(BABEL_DEBUG_ROUTE, "Removing ipv4 route (from zebra)."); + flush_xroute(xroute); + } + return 0; +} + +/* Add redistributed route to Babel table. */ +int +babel_ipv6_route_add (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix, + unsigned int ifindex, struct in6_addr *nexthop) +{ + unsigned char uchar_prefix[16]; + + in6addr_to_uchar(uchar_prefix, &prefix->prefix); + debugf(BABEL_DEBUG_ROUTE, "Adding new route comming from Zebra."); + xroute_add_new_route(uchar_prefix, prefix->prefixlen, api->metric, ifindex, + 0, 1); + return 0; +} + +/* Remove redistributed route from Babel table. */ +int +babel_ipv6_route_delete (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix, + unsigned int ifindex) +{ + unsigned char uchar_prefix[16]; + struct xroute *xroute = NULL; + + in6addr_to_uchar(uchar_prefix, &prefix->prefix); + xroute = find_xroute(uchar_prefix, prefix->prefixlen); + if (xroute != NULL) { + debugf(BABEL_DEBUG_ROUTE, "Removing route (from zebra)."); + flush_xroute(xroute); + } + return 0; +} + +struct xroute * +find_xroute(const unsigned char *prefix, unsigned char plen) +{ + int i; + for(i = 0; i < numxroutes; i++) { + if(xroutes[i].plen == plen && + memcmp(xroutes[i].prefix, prefix, 16) == 0) + return &xroutes[i]; + } + return NULL; +} + +void +flush_xroute(struct xroute *xroute) +{ + int i; + + i = xroute - xroutes; + assert(i >= 0 && i < numxroutes); + + if(i != numxroutes - 1) + memcpy(xroutes + i, xroutes + numxroutes - 1, sizeof(struct xroute)); + numxroutes--; + VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute)); + + if(numxroutes == 0) { + free(xroutes); + xroutes = NULL; + maxxroutes = 0; + } else if(maxxroutes > 8 && numxroutes < maxxroutes / 4) { + struct xroute *new_xroutes; + int n = maxxroutes / 2; + new_xroutes = realloc(xroutes, n * sizeof(struct xroute)); + if(new_xroutes == NULL) + return; + xroutes = new_xroutes; + maxxroutes = n; + } +} + +static int +add_xroute(unsigned char prefix[16], unsigned char plen, + unsigned short metric, unsigned int ifindex, int proto) +{ + struct xroute *xroute = find_xroute(prefix, plen); + if(xroute) { + if(xroute->metric <= metric) + return 0; + xroute->metric = metric; + return 1; + } + + if(numxroutes >= maxxroutes) { + struct xroute *new_xroutes; + int n = maxxroutes < 1 ? 8 : 2 * maxxroutes; + new_xroutes = xroutes == NULL ? + malloc(n * sizeof(struct xroute)) : + realloc(xroutes, n * sizeof(struct xroute)); + if(new_xroutes == NULL) + return -1; + maxxroutes = n; + xroutes = new_xroutes; + } + + 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; +} + +/* add an xroute, verifying some conditions; return 0 if there is no changes */ +int +xroute_add_new_route(unsigned char prefix[16], unsigned char plen, + unsigned short metric, unsigned int ifindex, + int proto, int send_updates) +{ + int rc; + if(martian_prefix(prefix, plen)) + return 0; + metric = redistribute_filter(prefix, plen, ifindex, proto); + if(metric < INFINITY) { + rc = add_xroute(prefix, plen, metric, ifindex, proto); + if(rc > 0) { + struct route *route; + route = find_installed_route(prefix, plen); + if(route) { + if(allow_duplicates < 0 || + metric < allow_duplicates) + uninstall_route(route); + } + if(send_updates) + send_update(NULL, 0, prefix, plen); + return 1; + } + } + return 0; +} -- cgit v1.2.1 From ef4de4d36c2dc10a68d41e518057d04b262ec867 Mon Sep 17 00:00:00 2001 From: Denis Ovsienko Date: Sun, 8 Jan 2012 15:29:19 +0400 Subject: babeld: address FreeBSD "struct route" issue FreeBSD system headers have their own "struct route", which made it impossible to compile babeld. Switching babeld to "struct babel_route". --- babeld/xroute.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'babeld/xroute.c') diff --git a/babeld/xroute.c b/babeld/xroute.c index 42f83d07..7b09cb87 100644 --- a/babeld/xroute.c +++ b/babeld/xroute.c @@ -210,7 +210,7 @@ xroute_add_new_route(unsigned char prefix[16], unsigned char plen, if(metric < INFINITY) { rc = add_xroute(prefix, plen, metric, ifindex, proto); if(rc > 0) { - struct route *route; + struct babel_route *route; route = find_installed_route(prefix, plen); if(route) { if(allow_duplicates < 0 || -- cgit v1.2.1 From 3dbda0ceebe369a1071600fe7d8d8ecf45f1027c Mon Sep 17 00:00:00 2001 From: Denis Ovsienko Date: Sun, 8 Jan 2012 16:52:36 +0400 Subject: babeld: address some compilation warnings Including system headers is not necessary with zebra.h included and sometimes results in "__ASSERT_FUNCTION redefined" compilation warning. * babeld.c * babel_distribute_update_interface(): make static * babel_interface.c * interface_config_write(): unused 'babel_ifp' * don't include system headers * message.c * send_request(): unused 'babel_ifp' * send_multihop_request(): idem * don't include system headers * route.c: don't include system headers * xroute.c: idem * source.h: newline at EOF * message.h: idem --- babeld/xroute.c | 8 -------- 1 file changed, 8 deletions(-) (limited to 'babeld/xroute.c') diff --git a/babeld/xroute.c b/babeld/xroute.c index 7b09cb87..eb4aaeb5 100644 --- a/babeld/xroute.c +++ b/babeld/xroute.c @@ -37,14 +37,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include -#include -#include -#include -#include - #include #include "if.h" #include "log.h" -- cgit v1.2.1 From 6dfeb3f6cfaf89ce0e11421b48feb2965a43ffd6 Mon Sep 17 00:00:00 2001 From: Matthieu Boutier Date: Fri, 20 Jan 2012 17:53:57 +0100 Subject: babeld: fix bug due to v4mapped addresses. --- babeld/xroute.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'babeld/xroute.c') diff --git a/babeld/xroute.c b/babeld/xroute.c index eb4aaeb5..8330b492 100644 --- a/babeld/xroute.c +++ b/babeld/xroute.c @@ -63,8 +63,8 @@ babel_ipv4_route_add (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix, inaddr_to_uchar(uchar_prefix, &prefix->prefix); debugf(BABEL_DEBUG_ROUTE, "Adding new ipv4 route comming from Zebra."); - xroute_add_new_route(uchar_prefix, prefix->prefixlen, api->metric, ifindex, - 0, 1); + xroute_add_new_route(uchar_prefix, prefix->prefixlen + 96, + api->metric, ifindex, 0, 1); return 0; } @@ -77,7 +77,7 @@ babel_ipv4_route_delete (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix, struct xroute *xroute = NULL; inaddr_to_uchar(uchar_prefix, &prefix->prefix); - xroute = find_xroute(uchar_prefix, prefix->prefixlen); + xroute = find_xroute(uchar_prefix, prefix->prefixlen + 96); if (xroute != NULL) { debugf(BABEL_DEBUG_ROUTE, "Removing ipv4 route (from zebra)."); flush_xroute(xroute); -- cgit v1.2.1 From c35fafdf887aa32c5be6ad738d3a3b0140cea6e8 Mon Sep 17 00:00:00 2001 From: Matthieu Boutier Date: Mon, 23 Jan 2012 23:46:32 +0100 Subject: babeld: babelz merge. Babelz is the last version of the stand-alone babel daemon. In particular, it use multiple channels to diminuate interferences. Please refer to this one for more details. --- babeld/xroute.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'babeld/xroute.c') diff --git a/babeld/xroute.c b/babeld/xroute.c index 8330b492..80651671 100644 --- a/babeld/xroute.c +++ b/babeld/xroute.c @@ -50,9 +50,12 @@ THE SOFTWARE. #include "util.h" #include "babel_interface.h" -struct xroute *xroutes; -int numxroutes = 0; -int maxxroutes = 0; +static int xroute_add_new_route(unsigned char prefix[16], unsigned char plen, + unsigned short metric, unsigned int ifindex, + int proto, int send_updates); + +static struct xroute *xroutes; +static int numxroutes = 0, maxxroutes = 0; /* Add redistributed route to Babel table. */ int @@ -189,8 +192,24 @@ add_xroute(unsigned char prefix[16], unsigned char plen, return 1; } -/* add an xroute, verifying some conditions; return 0 if there is no changes */ +/* Returns an overestimate of the number of xroutes. */ int +xroutes_estimate() +{ + return numxroutes; +} + +void +for_all_xroutes(void (*f)(struct xroute*, void*), void *closure) +{ + int i; + + for(i = 0; i < numxroutes; i++) + (*f)(&xroutes[i], closure); +} + +/* add an xroute, verifying some conditions; return 0 if there is no changes */ +static int xroute_add_new_route(unsigned char prefix[16], unsigned char plen, unsigned short metric, unsigned int ifindex, int proto, int send_updates) -- cgit v1.2.1