]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/xroute.c
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 <netinet/in.h>
54 #include "neighbour.h"
59 #include "babel_interface.h"
61 struct xroute
*xroutes
;
65 /* Add redistributed route to Babel table. */
67 babel_ipv4_route_add (struct zapi_ipv4
*api
, struct prefix_ipv4
*prefix
,
68 unsigned int ifindex
, struct in_addr
*nexthop
)
70 unsigned char uchar_prefix
[16];
72 inaddr_to_uchar(uchar_prefix
, &prefix
->prefix
);
73 debugf(BABEL_DEBUG_ROUTE
, "Adding new ipv4 route comming from Zebra.");
74 xroute_add_new_route(uchar_prefix
, prefix
->prefixlen
, api
->metric
, ifindex
,
79 /* Remove redistributed route from Babel table. */
81 babel_ipv4_route_delete (struct zapi_ipv4
*api
, struct prefix_ipv4
*prefix
,
84 unsigned char uchar_prefix
[16];
85 struct xroute
*xroute
= NULL
;
87 inaddr_to_uchar(uchar_prefix
, &prefix
->prefix
);
88 xroute
= find_xroute(uchar_prefix
, prefix
->prefixlen
);
90 debugf(BABEL_DEBUG_ROUTE
, "Removing ipv4 route (from zebra).");
96 /* Add redistributed route to Babel table. */
98 babel_ipv6_route_add (struct zapi_ipv6
*api
, struct prefix_ipv6
*prefix
,
99 unsigned int ifindex
, struct in6_addr
*nexthop
)
101 unsigned char uchar_prefix
[16];
103 in6addr_to_uchar(uchar_prefix
, &prefix
->prefix
);
104 debugf(BABEL_DEBUG_ROUTE
, "Adding new route comming from Zebra.");
105 xroute_add_new_route(uchar_prefix
, prefix
->prefixlen
, api
->metric
, ifindex
,
110 /* Remove redistributed route from Babel table. */
112 babel_ipv6_route_delete (struct zapi_ipv6
*api
, struct prefix_ipv6
*prefix
,
113 unsigned int ifindex
)
115 unsigned char uchar_prefix
[16];
116 struct xroute
*xroute
= NULL
;
118 in6addr_to_uchar(uchar_prefix
, &prefix
->prefix
);
119 xroute
= find_xroute(uchar_prefix
, prefix
->prefixlen
);
120 if (xroute
!= NULL
) {
121 debugf(BABEL_DEBUG_ROUTE
, "Removing route (from zebra).");
122 flush_xroute(xroute
);
128 find_xroute(const unsigned char *prefix
, unsigned char plen
)
131 for(i
= 0; i
< numxroutes
; i
++) {
132 if(xroutes
[i
].plen
== plen
&&
133 memcmp(xroutes
[i
].prefix
, prefix
, 16) == 0)
140 flush_xroute(struct xroute
*xroute
)
144 i
= xroute
- xroutes
;
145 assert(i
>= 0 && i
< numxroutes
);
147 if(i
!= numxroutes
- 1)
148 memcpy(xroutes
+ i
, xroutes
+ numxroutes
- 1, sizeof(struct xroute
));
150 VALGRIND_MAKE_MEM_UNDEFINED(xroutes
+ numxroutes
, sizeof(struct xroute
));
152 if(numxroutes
== 0) {
156 } else if(maxxroutes
> 8 && numxroutes
< maxxroutes
/ 4) {
157 struct xroute
*new_xroutes
;
158 int n
= maxxroutes
/ 2;
159 new_xroutes
= realloc(xroutes
, n
* sizeof(struct xroute
));
160 if(new_xroutes
== NULL
)
162 xroutes
= new_xroutes
;
168 add_xroute(unsigned char prefix
[16], unsigned char plen
,
169 unsigned short metric
, unsigned int ifindex
, int proto
)
171 struct xroute
*xroute
= find_xroute(prefix
, plen
);
173 if(xroute
->metric
<= metric
)
175 xroute
->metric
= metric
;
179 if(numxroutes
>= maxxroutes
) {
180 struct xroute
*new_xroutes
;
181 int n
= maxxroutes
< 1 ? 8 : 2 * maxxroutes
;
182 new_xroutes
= xroutes
== NULL
?
183 malloc(n
* sizeof(struct xroute
)) :
184 realloc(xroutes
, n
* sizeof(struct xroute
));
185 if(new_xroutes
== NULL
)
188 xroutes
= new_xroutes
;
191 memcpy(xroutes
[numxroutes
].prefix
, prefix
, 16);
192 xroutes
[numxroutes
].plen
= plen
;
193 xroutes
[numxroutes
].metric
= metric
;
194 xroutes
[numxroutes
].ifindex
= ifindex
;
195 xroutes
[numxroutes
].proto
= proto
;
200 /* add an xroute, verifying some conditions; return 0 if there is no changes */
202 xroute_add_new_route(unsigned char prefix
[16], unsigned char plen
,
203 unsigned short metric
, unsigned int ifindex
,
204 int proto
, int send_updates
)
207 if(martian_prefix(prefix
, plen
))
209 metric
= redistribute_filter(prefix
, plen
, ifindex
, proto
);
210 if(metric
< INFINITY
) {
211 rc
= add_xroute(prefix
, plen
, metric
, ifindex
, proto
);
214 route
= find_installed_route(prefix
, plen
);
216 if(allow_duplicates
< 0 ||
217 metric
< allow_duplicates
)
218 uninstall_route(route
);
221 send_update(NULL
, 0, prefix
, plen
);