Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is free software: you may copy, redistribute and/or modify it |
| 3 | * under the terms of the GNU General Public License as published by the |
| 4 | * Free Software Foundation, either version 2 of the License, or (at your |
| 5 | * option) any later version. |
| 6 | * |
| 7 | * This file is distributed in the hope that it will be useful, but |
| 8 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 | * General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | * |
| 15 | * This file incorporates work covered by the following copyright and |
| 16 | * permission notice: |
| 17 | * |
| 18 | Copyright (c) 2007, 2008 by Juliusz Chroboczek |
| 19 | Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek |
| 20 | |
| 21 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 22 | of this software and associated documentation files (the "Software"), to deal |
| 23 | in the Software without restriction, including without limitation the rights |
| 24 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 25 | copies of the Software, and to permit persons to whom the Software is |
| 26 | furnished to do so, subject to the following conditions: |
| 27 | |
| 28 | The above copyright notice and this permission notice shall be included in |
| 29 | all copies or substantial portions of the Software. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 32 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 33 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 34 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 35 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 36 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 37 | THE SOFTWARE. |
| 38 | */ |
| 39 | |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 40 | #include <zebra.h> |
| 41 | #include "if.h" |
| 42 | #include "log.h" |
| 43 | |
| 44 | #include "babeld.h" |
| 45 | #include "kernel.h" |
| 46 | #include "neighbour.h" |
| 47 | #include "message.h" |
| 48 | #include "route.h" |
| 49 | #include "xroute.h" |
| 50 | #include "util.h" |
| 51 | #include "babel_interface.h" |
| 52 | |
| 53 | struct xroute *xroutes; |
| 54 | int numxroutes = 0; |
| 55 | int maxxroutes = 0; |
| 56 | |
| 57 | /* Add redistributed route to Babel table. */ |
| 58 | int |
| 59 | babel_ipv4_route_add (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix, |
| 60 | unsigned int ifindex, struct in_addr *nexthop) |
| 61 | { |
| 62 | unsigned char uchar_prefix[16]; |
| 63 | |
| 64 | inaddr_to_uchar(uchar_prefix, &prefix->prefix); |
| 65 | debugf(BABEL_DEBUG_ROUTE, "Adding new ipv4 route comming from Zebra."); |
Matthieu Boutier | 6dfeb3f | 2012-01-20 17:53:57 +0100 | [diff] [blame] | 66 | xroute_add_new_route(uchar_prefix, prefix->prefixlen + 96, |
| 67 | api->metric, ifindex, 0, 1); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* Remove redistributed route from Babel table. */ |
| 72 | int |
| 73 | babel_ipv4_route_delete (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix, |
| 74 | unsigned int ifindex) |
| 75 | { |
| 76 | unsigned char uchar_prefix[16]; |
| 77 | struct xroute *xroute = NULL; |
| 78 | |
| 79 | inaddr_to_uchar(uchar_prefix, &prefix->prefix); |
Matthieu Boutier | 6dfeb3f | 2012-01-20 17:53:57 +0100 | [diff] [blame] | 80 | xroute = find_xroute(uchar_prefix, prefix->prefixlen + 96); |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 81 | if (xroute != NULL) { |
| 82 | debugf(BABEL_DEBUG_ROUTE, "Removing ipv4 route (from zebra)."); |
| 83 | flush_xroute(xroute); |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* Add redistributed route to Babel table. */ |
| 89 | int |
| 90 | babel_ipv6_route_add (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix, |
| 91 | unsigned int ifindex, struct in6_addr *nexthop) |
| 92 | { |
| 93 | unsigned char uchar_prefix[16]; |
| 94 | |
| 95 | in6addr_to_uchar(uchar_prefix, &prefix->prefix); |
| 96 | debugf(BABEL_DEBUG_ROUTE, "Adding new route comming from Zebra."); |
| 97 | xroute_add_new_route(uchar_prefix, prefix->prefixlen, api->metric, ifindex, |
| 98 | 0, 1); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* Remove redistributed route from Babel table. */ |
| 103 | int |
| 104 | babel_ipv6_route_delete (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix, |
| 105 | unsigned int ifindex) |
| 106 | { |
| 107 | unsigned char uchar_prefix[16]; |
| 108 | struct xroute *xroute = NULL; |
| 109 | |
| 110 | in6addr_to_uchar(uchar_prefix, &prefix->prefix); |
| 111 | xroute = find_xroute(uchar_prefix, prefix->prefixlen); |
| 112 | if (xroute != NULL) { |
| 113 | debugf(BABEL_DEBUG_ROUTE, "Removing route (from zebra)."); |
| 114 | flush_xroute(xroute); |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | struct xroute * |
| 120 | find_xroute(const unsigned char *prefix, unsigned char plen) |
| 121 | { |
| 122 | int i; |
| 123 | for(i = 0; i < numxroutes; i++) { |
| 124 | if(xroutes[i].plen == plen && |
| 125 | memcmp(xroutes[i].prefix, prefix, 16) == 0) |
| 126 | return &xroutes[i]; |
| 127 | } |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | flush_xroute(struct xroute *xroute) |
| 133 | { |
| 134 | int i; |
| 135 | |
| 136 | i = xroute - xroutes; |
| 137 | assert(i >= 0 && i < numxroutes); |
| 138 | |
| 139 | if(i != numxroutes - 1) |
| 140 | memcpy(xroutes + i, xroutes + numxroutes - 1, sizeof(struct xroute)); |
| 141 | numxroutes--; |
| 142 | VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute)); |
| 143 | |
| 144 | if(numxroutes == 0) { |
| 145 | free(xroutes); |
| 146 | xroutes = NULL; |
| 147 | maxxroutes = 0; |
| 148 | } else if(maxxroutes > 8 && numxroutes < maxxroutes / 4) { |
| 149 | struct xroute *new_xroutes; |
| 150 | int n = maxxroutes / 2; |
| 151 | new_xroutes = realloc(xroutes, n * sizeof(struct xroute)); |
| 152 | if(new_xroutes == NULL) |
| 153 | return; |
| 154 | xroutes = new_xroutes; |
| 155 | maxxroutes = n; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static int |
| 160 | add_xroute(unsigned char prefix[16], unsigned char plen, |
| 161 | unsigned short metric, unsigned int ifindex, int proto) |
| 162 | { |
| 163 | struct xroute *xroute = find_xroute(prefix, plen); |
| 164 | if(xroute) { |
| 165 | if(xroute->metric <= metric) |
| 166 | return 0; |
| 167 | xroute->metric = metric; |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | if(numxroutes >= maxxroutes) { |
| 172 | struct xroute *new_xroutes; |
| 173 | int n = maxxroutes < 1 ? 8 : 2 * maxxroutes; |
| 174 | new_xroutes = xroutes == NULL ? |
| 175 | malloc(n * sizeof(struct xroute)) : |
| 176 | realloc(xroutes, n * sizeof(struct xroute)); |
| 177 | if(new_xroutes == NULL) |
| 178 | return -1; |
| 179 | maxxroutes = n; |
| 180 | xroutes = new_xroutes; |
| 181 | } |
| 182 | |
| 183 | memcpy(xroutes[numxroutes].prefix, prefix, 16); |
| 184 | xroutes[numxroutes].plen = plen; |
| 185 | xroutes[numxroutes].metric = metric; |
| 186 | xroutes[numxroutes].ifindex = ifindex; |
| 187 | xroutes[numxroutes].proto = proto; |
| 188 | numxroutes++; |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | /* add an xroute, verifying some conditions; return 0 if there is no changes */ |
| 193 | int |
| 194 | xroute_add_new_route(unsigned char prefix[16], unsigned char plen, |
| 195 | unsigned short metric, unsigned int ifindex, |
| 196 | int proto, int send_updates) |
| 197 | { |
| 198 | int rc; |
| 199 | if(martian_prefix(prefix, plen)) |
| 200 | return 0; |
| 201 | metric = redistribute_filter(prefix, plen, ifindex, proto); |
| 202 | if(metric < INFINITY) { |
| 203 | rc = add_xroute(prefix, plen, metric, ifindex, proto); |
| 204 | if(rc > 0) { |
Denis Ovsienko | ef4de4d | 2012-01-08 15:29:19 +0400 | [diff] [blame] | 205 | struct babel_route *route; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 206 | route = find_installed_route(prefix, plen); |
| 207 | if(route) { |
| 208 | if(allow_duplicates < 0 || |
| 209 | metric < allow_duplicates) |
| 210 | uninstall_route(route); |
| 211 | } |
| 212 | if(send_updates) |
| 213 | send_update(NULL, 0, prefix, plen); |
| 214 | return 1; |
| 215 | } |
| 216 | } |
| 217 | return 0; |
| 218 | } |