]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/xroute.c
2 Copyright (c) 2007, 2008 by Juliusz Chroboczek
3 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
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:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
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
30 #include "neighbour.h"
35 #include "babel_interface.h"
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
);
41 static struct xroute
*xroutes
;
42 static int numxroutes
= 0, maxxroutes
= 0;
44 /* Add redistributed route to Babel table. */
46 babel_ipv4_route_add (struct zapi_ipv4
*api
, struct prefix_ipv4
*prefix
,
47 unsigned int ifindex
, struct in_addr
*nexthop
)
49 unsigned char uchar_prefix
[16];
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);
58 /* Remove redistributed route from Babel table. */
60 babel_ipv4_route_delete (struct zapi_ipv4
*api
, struct prefix_ipv4
*prefix
,
63 unsigned char uchar_prefix
[16];
64 struct xroute
*xroute
= NULL
;
66 inaddr_to_uchar(uchar_prefix
, &prefix
->prefix
);
67 xroute
= find_xroute(uchar_prefix
, prefix
->prefixlen
+ 96);
69 debugf(BABEL_DEBUG_ROUTE
, "Removing ipv4 route (from zebra).");
75 /* Add redistributed route to Babel table. */
77 babel_ipv6_route_add (struct zapi_ipv6
*api
, struct prefix_ipv6
*prefix
,
78 unsigned int ifindex
, struct in6_addr
*nexthop
)
80 unsigned char uchar_prefix
[16];
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
,
89 /* Remove redistributed route from Babel table. */
91 babel_ipv6_route_delete (struct zapi_ipv6
*api
, struct prefix_ipv6
*prefix
,
94 unsigned char uchar_prefix
[16];
95 struct xroute
*xroute
= NULL
;
97 in6addr_to_uchar(uchar_prefix
, &prefix
->prefix
);
98 xroute
= find_xroute(uchar_prefix
, prefix
->prefixlen
);
100 debugf(BABEL_DEBUG_ROUTE
, "Removing route (from zebra).");
101 flush_xroute(xroute
);
107 find_xroute(const unsigned char *prefix
, unsigned char plen
)
110 for(i
= 0; i
< numxroutes
; i
++) {
111 if(xroutes
[i
].plen
== plen
&&
112 memcmp(xroutes
[i
].prefix
, prefix
, 16) == 0)
119 flush_xroute(struct xroute
*xroute
)
123 i
= xroute
- xroutes
;
124 assert(i
>= 0 && i
< numxroutes
);
126 if(i
!= numxroutes
- 1)
127 memcpy(xroutes
+ i
, xroutes
+ numxroutes
- 1, sizeof(struct xroute
));
129 VALGRIND_MAKE_MEM_UNDEFINED(xroutes
+ numxroutes
, sizeof(struct xroute
));
131 if(numxroutes
== 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
)
141 xroutes
= new_xroutes
;
147 add_xroute(unsigned char prefix
[16], unsigned char plen
,
148 unsigned short metric
, unsigned int ifindex
, int proto
)
150 struct xroute
*xroute
= find_xroute(prefix
, plen
);
152 if(xroute
->metric
<= metric
)
154 xroute
->metric
= metric
;
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
)
167 xroutes
= new_xroutes
;
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
;
179 /* Returns an overestimate of the number of xroutes. */
186 struct xroute_stream
{
194 struct xroute_stream
*stream
= malloc(sizeof(struct xroute_stream
));
203 xroute_stream_next(struct xroute_stream
*stream
)
205 if(stream
->index
< numxroutes
)
206 return &xroutes
[stream
->index
++];
212 xroute_stream_done(struct xroute_stream
*stream
)
217 /* add an xroute, verifying some conditions; return 0 if there is no changes */
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
)
224 if(martian_prefix(prefix
, plen
))
226 metric
= redistribute_filter(prefix
, plen
, ifindex
, proto
);
227 if(metric
< INFINITY
) {
228 rc
= add_xroute(prefix
, plen
, metric
, ifindex
, proto
);
230 struct babel_route
*route
;
231 route
= find_installed_route(prefix
, plen
);
233 uninstall_route(route
);
235 send_update(NULL
, 0, prefix
, plen
);