]> git.proxmox.com Git - mirror_frr.git/blob - babeld/xroute.c
babeld: Initial import, for Babel routing protocol.
[mirror_frr.git] / babeld / xroute.c
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
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <assert.h>
45 #include <sys/time.h>
46 #include <netinet/in.h>
47
48 #include <zebra.h>
49 #include "if.h"
50 #include "log.h"
51
52 #include "babeld.h"
53 #include "kernel.h"
54 #include "neighbour.h"
55 #include "message.h"
56 #include "route.h"
57 #include "xroute.h"
58 #include "util.h"
59 #include "babel_interface.h"
60
61 struct xroute *xroutes;
62 int numxroutes = 0;
63 int maxxroutes = 0;
64
65 /* Add redistributed route to Babel table. */
66 int
67 babel_ipv4_route_add (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix,
68 unsigned int ifindex, struct in_addr *nexthop)
69 {
70 unsigned char uchar_prefix[16];
71
72 inaddr_to_uchar(uchar_prefix, &prefix->prefix);
73 debugf(BABEL_DEBUG_ROUTE, "Adding new ipv4 route comming from Zebra.");
74 xroute_add_new_route(uchar_prefix, prefix->prefixlen, api->metric, ifindex,
75 0, 1);
76 return 0;
77 }
78
79 /* Remove redistributed route from Babel table. */
80 int
81 babel_ipv4_route_delete (struct zapi_ipv4 *api, struct prefix_ipv4 *prefix,
82 unsigned int ifindex)
83 {
84 unsigned char uchar_prefix[16];
85 struct xroute *xroute = NULL;
86
87 inaddr_to_uchar(uchar_prefix, &prefix->prefix);
88 xroute = find_xroute(uchar_prefix, prefix->prefixlen);
89 if (xroute != NULL) {
90 debugf(BABEL_DEBUG_ROUTE, "Removing ipv4 route (from zebra).");
91 flush_xroute(xroute);
92 }
93 return 0;
94 }
95
96 /* Add redistributed route to Babel table. */
97 int
98 babel_ipv6_route_add (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix,
99 unsigned int ifindex, struct in6_addr *nexthop)
100 {
101 unsigned char uchar_prefix[16];
102
103 in6addr_to_uchar(uchar_prefix, &prefix->prefix);
104 debugf(BABEL_DEBUG_ROUTE, "Adding new route comming from Zebra.");
105 xroute_add_new_route(uchar_prefix, prefix->prefixlen, api->metric, ifindex,
106 0, 1);
107 return 0;
108 }
109
110 /* Remove redistributed route from Babel table. */
111 int
112 babel_ipv6_route_delete (struct zapi_ipv6 *api, struct prefix_ipv6 *prefix,
113 unsigned int ifindex)
114 {
115 unsigned char uchar_prefix[16];
116 struct xroute *xroute = NULL;
117
118 in6addr_to_uchar(uchar_prefix, &prefix->prefix);
119 xroute = find_xroute(uchar_prefix, prefix->prefixlen);
120 if (xroute != NULL) {
121 debugf(BABEL_DEBUG_ROUTE, "Removing route (from zebra).");
122 flush_xroute(xroute);
123 }
124 return 0;
125 }
126
127 struct xroute *
128 find_xroute(const unsigned char *prefix, unsigned char plen)
129 {
130 int i;
131 for(i = 0; i < numxroutes; i++) {
132 if(xroutes[i].plen == plen &&
133 memcmp(xroutes[i].prefix, prefix, 16) == 0)
134 return &xroutes[i];
135 }
136 return NULL;
137 }
138
139 void
140 flush_xroute(struct xroute *xroute)
141 {
142 int i;
143
144 i = xroute - xroutes;
145 assert(i >= 0 && i < numxroutes);
146
147 if(i != numxroutes - 1)
148 memcpy(xroutes + i, xroutes + numxroutes - 1, sizeof(struct xroute));
149 numxroutes--;
150 VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute));
151
152 if(numxroutes == 0) {
153 free(xroutes);
154 xroutes = NULL;
155 maxxroutes = 0;
156 } else if(maxxroutes > 8 && numxroutes < maxxroutes / 4) {
157 struct xroute *new_xroutes;
158 int n = maxxroutes / 2;
159 new_xroutes = realloc(xroutes, n * sizeof(struct xroute));
160 if(new_xroutes == NULL)
161 return;
162 xroutes = new_xroutes;
163 maxxroutes = n;
164 }
165 }
166
167 static int
168 add_xroute(unsigned char prefix[16], unsigned char plen,
169 unsigned short metric, unsigned int ifindex, int proto)
170 {
171 struct xroute *xroute = find_xroute(prefix, plen);
172 if(xroute) {
173 if(xroute->metric <= metric)
174 return 0;
175 xroute->metric = metric;
176 return 1;
177 }
178
179 if(numxroutes >= maxxroutes) {
180 struct xroute *new_xroutes;
181 int n = maxxroutes < 1 ? 8 : 2 * maxxroutes;
182 new_xroutes = xroutes == NULL ?
183 malloc(n * sizeof(struct xroute)) :
184 realloc(xroutes, n * sizeof(struct xroute));
185 if(new_xroutes == NULL)
186 return -1;
187 maxxroutes = n;
188 xroutes = new_xroutes;
189 }
190
191 memcpy(xroutes[numxroutes].prefix, prefix, 16);
192 xroutes[numxroutes].plen = plen;
193 xroutes[numxroutes].metric = metric;
194 xroutes[numxroutes].ifindex = ifindex;
195 xroutes[numxroutes].proto = proto;
196 numxroutes++;
197 return 1;
198 }
199
200 /* add an xroute, verifying some conditions; return 0 if there is no changes */
201 int
202 xroute_add_new_route(unsigned char prefix[16], unsigned char plen,
203 unsigned short metric, unsigned int ifindex,
204 int proto, int send_updates)
205 {
206 int rc;
207 if(martian_prefix(prefix, plen))
208 return 0;
209 metric = redistribute_filter(prefix, plen, ifindex, proto);
210 if(metric < INFINITY) {
211 rc = add_xroute(prefix, plen, metric, ifindex, proto);
212 if(rc > 0) {
213 struct route *route;
214 route = find_installed_route(prefix, plen);
215 if(route) {
216 if(allow_duplicates < 0 ||
217 metric < allow_duplicates)
218 uninstall_route(route);
219 }
220 if(send_updates)
221 send_update(NULL, 0, prefix, plen);
222 return 1;
223 }
224 }
225 return 0;
226 }