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