]>
Commit | Line | Data |
---|---|---|
ca10883e DS |
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 | |
74489921 | 46 | babel_route_add (struct zapi_route *api) |
ca10883e DS |
47 | { |
48 | unsigned char uchar_prefix[16]; | |
49 | ||
74489921 RW |
50 | switch (api->prefix.family) { |
51 | case AF_INET: | |
52 | inaddr_to_uchar(uchar_prefix, &api->prefix.u.prefix4); | |
53 | debugf(BABEL_DEBUG_ROUTE, "Adding new ipv4 route coming from Zebra."); | |
54 | xroute_add_new_route(uchar_prefix, api->prefix.prefixlen + 96, | |
55 | api->metric, api->nexthops[0].ifindex, 0, 1); | |
56 | break; | |
57 | case AF_INET6: | |
58 | in6addr_to_uchar(uchar_prefix, &api->prefix.u.prefix6); | |
59 | debugf(BABEL_DEBUG_ROUTE, "Adding new ipv6 route coming from Zebra."); | |
60 | xroute_add_new_route(uchar_prefix, api->prefix.prefixlen, | |
61 | api->metric, api->nexthops[0].ifindex, 0, 1); | |
62 | break; | |
ca10883e | 63 | } |
ca10883e | 64 | |
ca10883e DS |
65 | return 0; |
66 | } | |
67 | ||
68 | /* Remove redistributed route from Babel table. */ | |
69 | int | |
74489921 | 70 | babel_route_delete (struct zapi_route *api) |
ca10883e DS |
71 | { |
72 | unsigned char uchar_prefix[16]; | |
73 | struct xroute *xroute = NULL; | |
74 | ||
74489921 RW |
75 | switch (api->prefix.family) { |
76 | case AF_INET: | |
77 | inaddr_to_uchar(uchar_prefix, &api->prefix.u.prefix4); | |
78 | xroute = find_xroute(uchar_prefix, api->prefix.prefixlen + 96); | |
79 | if (xroute != NULL) { | |
80 | debugf(BABEL_DEBUG_ROUTE, "Removing ipv4 route (from zebra)."); | |
81 | flush_xroute(xroute); | |
82 | } | |
83 | break; | |
84 | case AF_INET6: | |
85 | in6addr_to_uchar(uchar_prefix, &api->prefix.u.prefix6); | |
86 | xroute = find_xroute(uchar_prefix, api->prefix.prefixlen); | |
87 | if (xroute != NULL) { | |
88 | debugf(BABEL_DEBUG_ROUTE, "Removing ipv6 route (from zebra)."); | |
89 | flush_xroute(xroute); | |
90 | } | |
91 | break; | |
ca10883e | 92 | } |
74489921 | 93 | |
ca10883e DS |
94 | return 0; |
95 | } | |
96 | ||
97 | struct xroute * | |
98 | find_xroute(const unsigned char *prefix, unsigned char plen) | |
99 | { | |
100 | int i; | |
101 | for(i = 0; i < numxroutes; i++) { | |
102 | if(xroutes[i].plen == plen && | |
103 | memcmp(xroutes[i].prefix, prefix, 16) == 0) | |
104 | return &xroutes[i]; | |
105 | } | |
106 | return NULL; | |
107 | } | |
108 | ||
109 | void | |
110 | flush_xroute(struct xroute *xroute) | |
111 | { | |
112 | int i; | |
113 | ||
114 | i = xroute - xroutes; | |
115 | assert(i >= 0 && i < numxroutes); | |
116 | ||
117 | if(i != numxroutes - 1) | |
118 | memcpy(xroutes + i, xroutes + numxroutes - 1, sizeof(struct xroute)); | |
119 | numxroutes--; | |
120 | VALGRIND_MAKE_MEM_UNDEFINED(xroutes + numxroutes, sizeof(struct xroute)); | |
121 | ||
122 | if(numxroutes == 0) { | |
123 | free(xroutes); | |
124 | xroutes = NULL; | |
125 | maxxroutes = 0; | |
126 | } else if(maxxroutes > 8 && numxroutes < maxxroutes / 4) { | |
127 | struct xroute *new_xroutes; | |
128 | int n = maxxroutes / 2; | |
129 | new_xroutes = realloc(xroutes, n * sizeof(struct xroute)); | |
130 | if(new_xroutes == NULL) | |
131 | return; | |
132 | xroutes = new_xroutes; | |
133 | maxxroutes = n; | |
134 | } | |
135 | } | |
136 | ||
137 | static int | |
138 | add_xroute(unsigned char prefix[16], unsigned char plen, | |
139 | unsigned short metric, unsigned int ifindex, int proto) | |
140 | { | |
141 | struct xroute *xroute = find_xroute(prefix, plen); | |
142 | if(xroute) { | |
143 | if(xroute->metric <= metric) | |
144 | return 0; | |
145 | xroute->metric = metric; | |
146 | return 1; | |
147 | } | |
148 | ||
149 | if(numxroutes >= maxxroutes) { | |
150 | struct xroute *new_xroutes; | |
151 | int n = maxxroutes < 1 ? 8 : 2 * maxxroutes; | |
152 | new_xroutes = xroutes == NULL ? | |
153 | malloc(n * sizeof(struct xroute)) : | |
154 | realloc(xroutes, n * sizeof(struct xroute)); | |
155 | if(new_xroutes == NULL) | |
156 | return -1; | |
157 | maxxroutes = n; | |
158 | xroutes = new_xroutes; | |
159 | } | |
160 | ||
161 | memcpy(xroutes[numxroutes].prefix, prefix, 16); | |
162 | xroutes[numxroutes].plen = plen; | |
163 | xroutes[numxroutes].metric = metric; | |
164 | xroutes[numxroutes].ifindex = ifindex; | |
165 | xroutes[numxroutes].proto = proto; | |
166 | numxroutes++; | |
167 | return 1; | |
168 | } | |
169 | ||
170 | /* Returns an overestimate of the number of xroutes. */ | |
171 | int | |
4d762f26 | 172 | xroutes_estimate(void) |
ca10883e DS |
173 | { |
174 | return numxroutes; | |
175 | } | |
176 | ||
177 | struct xroute_stream { | |
178 | int index; | |
179 | }; | |
180 | ||
181 | struct | |
182 | xroute_stream * | |
dd15627e | 183 | xroute_stream(void) |
ca10883e DS |
184 | { |
185 | struct xroute_stream *stream = malloc(sizeof(struct xroute_stream)); | |
186 | if(stream == NULL) | |
187 | return NULL; | |
188 | ||
189 | stream->index = 0; | |
190 | return stream; | |
191 | } | |
192 | ||
193 | struct xroute * | |
194 | xroute_stream_next(struct xroute_stream *stream) | |
195 | { | |
196 | if(stream->index < numxroutes) | |
197 | return &xroutes[stream->index++]; | |
198 | else | |
199 | return NULL; | |
200 | } | |
201 | ||
202 | void | |
203 | xroute_stream_done(struct xroute_stream *stream) | |
204 | { | |
205 | free(stream); | |
206 | } | |
207 | ||
208 | /* add an xroute, verifying some conditions; return 0 if there is no changes */ | |
209 | static int | |
210 | xroute_add_new_route(unsigned char prefix[16], unsigned char plen, | |
211 | unsigned short metric, unsigned int ifindex, | |
212 | int proto, int send_updates) | |
213 | { | |
214 | int rc; | |
215 | if(martian_prefix(prefix, plen)) | |
216 | return 0; | |
217 | metric = redistribute_filter(prefix, plen, ifindex, proto); | |
218 | if(metric < INFINITY) { | |
219 | rc = add_xroute(prefix, plen, metric, ifindex, proto); | |
220 | if(rc > 0) { | |
221 | struct babel_route *route; | |
222 | route = find_installed_route(prefix, plen); | |
223 | if(route) | |
224 | uninstall_route(route); | |
225 | if(send_updates) | |
226 | send_update(NULL, 0, prefix, plen); | |
227 | return 1; | |
228 | } | |
229 | } | |
230 | return 0; | |
231 | } |