]>
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_route_add (struct zapi_route
*api
)
48 unsigned char uchar_prefix
[16];
50 switch (api
->prefix
.family
) {
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);
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);
68 /* Remove redistributed route from Babel table. */
70 babel_route_delete (struct zapi_route
*api
)
72 unsigned char uchar_prefix
[16];
73 struct xroute
*xroute
= NULL
;
75 switch (api
->prefix
.family
) {
77 inaddr_to_uchar(uchar_prefix
, &api
->prefix
.u
.prefix4
);
78 xroute
= find_xroute(uchar_prefix
, api
->prefix
.prefixlen
+ 96);
80 debugf(BABEL_DEBUG_ROUTE
, "Removing ipv4 route (from zebra).");
85 in6addr_to_uchar(uchar_prefix
, &api
->prefix
.u
.prefix6
);
86 xroute
= find_xroute(uchar_prefix
, api
->prefix
.prefixlen
);
88 debugf(BABEL_DEBUG_ROUTE
, "Removing ipv6 route (from zebra).");
98 find_xroute(const unsigned char *prefix
, unsigned char plen
)
101 for(i
= 0; i
< numxroutes
; i
++) {
102 if(xroutes
[i
].plen
== plen
&&
103 memcmp(xroutes
[i
].prefix
, prefix
, 16) == 0)
110 flush_xroute(struct xroute
*xroute
)
114 i
= xroute
- xroutes
;
115 assert(i
>= 0 && i
< numxroutes
);
117 if(i
!= numxroutes
- 1)
118 memcpy(xroutes
+ i
, xroutes
+ numxroutes
- 1, sizeof(struct xroute
));
120 VALGRIND_MAKE_MEM_UNDEFINED(xroutes
+ numxroutes
, sizeof(struct xroute
));
122 if(numxroutes
== 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
)
132 xroutes
= new_xroutes
;
138 add_xroute(unsigned char prefix
[16], unsigned char plen
,
139 unsigned short metric
, unsigned int ifindex
, int proto
)
141 struct xroute
*xroute
= find_xroute(prefix
, plen
);
143 if(xroute
->metric
<= metric
)
145 xroute
->metric
= metric
;
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
)
158 xroutes
= new_xroutes
;
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
;
170 /* Returns an overestimate of the number of xroutes. */
177 struct xroute_stream
{
185 struct xroute_stream
*stream
= malloc(sizeof(struct xroute_stream
));
194 xroute_stream_next(struct xroute_stream
*stream
)
196 if(stream
->index
< numxroutes
)
197 return &xroutes
[stream
->index
++];
203 xroute_stream_done(struct xroute_stream
*stream
)
208 /* add an xroute, verifying some conditions; return 0 if there is no changes */
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
)
215 if(martian_prefix(prefix
, plen
))
217 metric
= redistribute_filter(prefix
, plen
, ifindex
, proto
);
218 if(metric
< INFINITY
) {
219 rc
= add_xroute(prefix
, plen
, metric
, ifindex
, proto
);
221 struct babel_route
*route
;
222 route
= find_installed_route(prefix
, plen
);
224 uninstall_route(route
);
226 send_update(NULL
, 0, prefix
, plen
);