]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/xroute.c
806516718f685d3c2434ec83dddb3159e676b3f4
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.
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.
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/>.
15 * This file incorporates work covered by the following copyright and
18 Copyright (c) 2007, 2008 by Juliusz Chroboczek
19 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
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:
28 The above copyright notice and this permission notice shall be included in
29 all copies or substantial portions of the Software.
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
46 #include "neighbour.h"
51 #include "babel_interface.h"
53 static int xroute_add_new_route(unsigned char prefix
[16], unsigned char plen
,
54 unsigned short metric
, unsigned int ifindex
,
55 int proto
, int send_updates
);
57 static struct xroute
*xroutes
;
58 static int numxroutes
= 0, maxxroutes
= 0;
60 /* Add redistributed route to Babel table. */
62 babel_ipv4_route_add (struct zapi_ipv4
*api
, struct prefix_ipv4
*prefix
,
63 unsigned int ifindex
, struct in_addr
*nexthop
)
65 unsigned char uchar_prefix
[16];
67 inaddr_to_uchar(uchar_prefix
, &prefix
->prefix
);
68 debugf(BABEL_DEBUG_ROUTE
, "Adding new ipv4 route comming from Zebra.");
69 xroute_add_new_route(uchar_prefix
, prefix
->prefixlen
+ 96,
70 api
->metric
, ifindex
, 0, 1);
74 /* Remove redistributed route from Babel table. */
76 babel_ipv4_route_delete (struct zapi_ipv4
*api
, struct prefix_ipv4
*prefix
,
79 unsigned char uchar_prefix
[16];
80 struct xroute
*xroute
= NULL
;
82 inaddr_to_uchar(uchar_prefix
, &prefix
->prefix
);
83 xroute
= find_xroute(uchar_prefix
, prefix
->prefixlen
+ 96);
85 debugf(BABEL_DEBUG_ROUTE
, "Removing ipv4 route (from zebra).");
91 /* Add redistributed route to Babel table. */
93 babel_ipv6_route_add (struct zapi_ipv6
*api
, struct prefix_ipv6
*prefix
,
94 unsigned int ifindex
, struct in6_addr
*nexthop
)
96 unsigned char uchar_prefix
[16];
98 in6addr_to_uchar(uchar_prefix
, &prefix
->prefix
);
99 debugf(BABEL_DEBUG_ROUTE
, "Adding new route comming from Zebra.");
100 xroute_add_new_route(uchar_prefix
, prefix
->prefixlen
, api
->metric
, ifindex
,
105 /* Remove redistributed route from Babel table. */
107 babel_ipv6_route_delete (struct zapi_ipv6
*api
, struct prefix_ipv6
*prefix
,
108 unsigned int ifindex
)
110 unsigned char uchar_prefix
[16];
111 struct xroute
*xroute
= NULL
;
113 in6addr_to_uchar(uchar_prefix
, &prefix
->prefix
);
114 xroute
= find_xroute(uchar_prefix
, prefix
->prefixlen
);
115 if (xroute
!= NULL
) {
116 debugf(BABEL_DEBUG_ROUTE
, "Removing route (from zebra).");
117 flush_xroute(xroute
);
123 find_xroute(const unsigned char *prefix
, unsigned char plen
)
126 for(i
= 0; i
< numxroutes
; i
++) {
127 if(xroutes
[i
].plen
== plen
&&
128 memcmp(xroutes
[i
].prefix
, prefix
, 16) == 0)
135 flush_xroute(struct xroute
*xroute
)
139 i
= xroute
- xroutes
;
140 assert(i
>= 0 && i
< numxroutes
);
142 if(i
!= numxroutes
- 1)
143 memcpy(xroutes
+ i
, xroutes
+ numxroutes
- 1, sizeof(struct xroute
));
145 VALGRIND_MAKE_MEM_UNDEFINED(xroutes
+ numxroutes
, sizeof(struct xroute
));
147 if(numxroutes
== 0) {
151 } else if(maxxroutes
> 8 && numxroutes
< maxxroutes
/ 4) {
152 struct xroute
*new_xroutes
;
153 int n
= maxxroutes
/ 2;
154 new_xroutes
= realloc(xroutes
, n
* sizeof(struct xroute
));
155 if(new_xroutes
== NULL
)
157 xroutes
= new_xroutes
;
163 add_xroute(unsigned char prefix
[16], unsigned char plen
,
164 unsigned short metric
, unsigned int ifindex
, int proto
)
166 struct xroute
*xroute
= find_xroute(prefix
, plen
);
168 if(xroute
->metric
<= metric
)
170 xroute
->metric
= metric
;
174 if(numxroutes
>= maxxroutes
) {
175 struct xroute
*new_xroutes
;
176 int n
= maxxroutes
< 1 ? 8 : 2 * maxxroutes
;
177 new_xroutes
= xroutes
== NULL
?
178 malloc(n
* sizeof(struct xroute
)) :
179 realloc(xroutes
, n
* sizeof(struct xroute
));
180 if(new_xroutes
== NULL
)
183 xroutes
= new_xroutes
;
186 memcpy(xroutes
[numxroutes
].prefix
, prefix
, 16);
187 xroutes
[numxroutes
].plen
= plen
;
188 xroutes
[numxroutes
].metric
= metric
;
189 xroutes
[numxroutes
].ifindex
= ifindex
;
190 xroutes
[numxroutes
].proto
= proto
;
195 /* Returns an overestimate of the number of xroutes. */
203 for_all_xroutes(void (*f
)(struct xroute
*, void*), void *closure
)
207 for(i
= 0; i
< numxroutes
; i
++)
208 (*f
)(&xroutes
[i
], closure
);
211 /* add an xroute, verifying some conditions; return 0 if there is no changes */
213 xroute_add_new_route(unsigned char prefix
[16], unsigned char plen
,
214 unsigned short metric
, unsigned int ifindex
,
215 int proto
, int send_updates
)
218 if(martian_prefix(prefix
, plen
))
220 metric
= redistribute_filter(prefix
, plen
, ifindex
, proto
);
221 if(metric
< INFINITY
) {
222 rc
= add_xroute(prefix
, plen
, metric
, ifindex
, proto
);
224 struct babel_route
*route
;
225 route
= find_installed_route(prefix
, plen
);
227 if(allow_duplicates
< 0 ||
228 metric
< allow_duplicates
)
229 uninstall_route(route
);
232 send_update(NULL
, 0, prefix
, plen
);