]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_asbr.c
ospfd: Make external routes in ospf VRF aware
[mirror_frr.git] / ospfd / ospf_asbr.c
1 /*
2 * OSPF AS Boundary Router functions.
3 * Copyright (C) 1999, 2000 Kunihiro Ishiguro, Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "thread.h"
25 #include "memory.h"
26 #include "linklist.h"
27 #include "prefix.h"
28 #include "if.h"
29 #include "table.h"
30 #include "vty.h"
31 #include "filter.h"
32 #include "log.h"
33
34 #include "ospfd/ospfd.h"
35 #include "ospfd/ospf_interface.h"
36 #include "ospfd/ospf_asbr.h"
37 #include "ospfd/ospf_lsa.h"
38 #include "ospfd/ospf_lsdb.h"
39 #include "ospfd/ospf_neighbor.h"
40 #include "ospfd/ospf_spf.h"
41 #include "ospfd/ospf_flood.h"
42 #include "ospfd/ospf_route.h"
43 #include "ospfd/ospf_zebra.h"
44 #include "ospfd/ospf_dump.h"
45
46
47 /* Remove external route. */
48 void ospf_external_route_remove(struct ospf *ospf, struct prefix_ipv4 *p)
49 {
50 struct route_node *rn;
51 struct ospf_route * or ;
52
53 rn = route_node_lookup(ospf->old_external_route, (struct prefix *)p);
54 if (rn)
55 if ((or = rn->info)) {
56 zlog_info("Route[%s/%d]: external path deleted",
57 inet_ntoa(p->prefix), p->prefixlen);
58
59 /* Remove route from zebra. */
60 if (or->type == OSPF_DESTINATION_NETWORK)
61 ospf_zebra_delete(ospf,
62 (struct prefix_ipv4 *)&rn->p,
63 or);
64
65 ospf_route_free(or);
66 rn->info = NULL;
67
68 route_unlock_node(rn);
69 route_unlock_node(rn);
70 return;
71 }
72
73 zlog_info("Route[%s/%d]: no such external path", inet_ntoa(p->prefix),
74 p->prefixlen);
75 }
76
77 /* Lookup external route. */
78 struct ospf_route *ospf_external_route_lookup(struct ospf *ospf,
79 struct prefix_ipv4 *p)
80 {
81 struct route_node *rn;
82
83 rn = route_node_lookup(ospf->old_external_route, (struct prefix *)p);
84 if (rn) {
85 route_unlock_node(rn);
86 if (rn->info)
87 return rn->info;
88 }
89
90 zlog_warn("Route[%s/%d]: lookup, no such prefix", inet_ntoa(p->prefix),
91 p->prefixlen);
92
93 return NULL;
94 }
95
96
97 /* Add an External info for AS-external-LSA. */
98 struct external_info *ospf_external_info_new(u_char type, u_short instance)
99 {
100 struct external_info *new;
101
102 new = (struct external_info *)XCALLOC(MTYPE_OSPF_EXTERNAL_INFO,
103 sizeof(struct external_info));
104 new->type = type;
105 new->instance = instance;
106
107 ospf_reset_route_map_set_values(&new->route_map_set);
108 return new;
109 }
110
111 static void ospf_external_info_free(struct external_info *ei)
112 {
113 XFREE(MTYPE_OSPF_EXTERNAL_INFO, ei);
114 }
115
116 void ospf_reset_route_map_set_values(struct route_map_set_values *values)
117 {
118 values->metric = -1;
119 values->metric_type = -1;
120 }
121
122 int ospf_route_map_set_compare(struct route_map_set_values *values1,
123 struct route_map_set_values *values2)
124 {
125 return values1->metric == values2->metric
126 && values1->metric_type == values2->metric_type;
127 }
128
129 /* Add an External info for AS-external-LSA. */
130 struct external_info *ospf_external_info_add(struct ospf *ospf, u_char type,
131 u_short instance,
132 struct prefix_ipv4 p,
133 ifindex_t ifindex,
134 struct in_addr nexthop,
135 route_tag_t tag)
136 {
137 struct external_info *new;
138 struct route_node *rn;
139 struct ospf_external *ext;
140 char inetbuf[INET6_BUFSIZ];
141
142 ext = ospf_external_lookup(ospf, type, instance);
143 if (!ext)
144 ext = ospf_external_add(ospf, type, instance);
145
146 rn = route_node_get(EXTERNAL_INFO(ext), (struct prefix *)&p);
147 /* If old info exists, -- discard new one or overwrite with new one? */
148 if (rn)
149 if (rn->info) {
150 new = rn->info;
151 if ((new->ifindex == ifindex)
152 && (new->nexthop.s_addr == nexthop.s_addr)
153 && (new->tag == tag)) {
154 route_unlock_node(rn);
155 return NULL; /* NULL => no LSA to refresh */
156 }
157
158 inet_ntop(AF_INET, (void *)&nexthop.s_addr, inetbuf,
159 INET6_BUFSIZ);
160 zlog_warn(
161 "Redistribute[%s][%d][%u]: %s/%d discarding old info with NH %s.",
162 ospf_redist_string(type), instance,
163 ospf->vrf_id, inet_ntoa(p.prefix),
164 p.prefixlen, inetbuf);
165 XFREE(MTYPE_OSPF_EXTERNAL_INFO, rn->info);
166 rn->info = NULL;
167 }
168
169 /* Create new External info instance. */
170 new = ospf_external_info_new(type, instance);
171 new->p = p;
172 new->ifindex = ifindex;
173 new->nexthop = nexthop;
174 new->tag = tag;
175
176 /* we don't unlock rn from the get() because we're attaching the info */
177 if (rn)
178 rn->info = new;
179
180 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
181 inet_ntop(AF_INET, (void *)&nexthop.s_addr, inetbuf,
182 INET6_BUFSIZ);
183 zlog_debug(
184 "Redistribute[%s][%u]: %s/%d external info created, with NH %s",
185 ospf_redist_string(type), ospf->vrf_id,
186 inet_ntoa(p.prefix), p.prefixlen, inetbuf);
187 }
188 return new;
189 }
190
191 void ospf_external_info_delete(struct ospf *ospf, u_char type, u_short instance,
192 struct prefix_ipv4 p)
193 {
194 struct route_node *rn;
195 struct ospf_external *ext;
196
197 ext = ospf_external_lookup(ospf, type, instance);
198 if (!ext)
199 return;
200
201 rn = route_node_lookup(EXTERNAL_INFO(ext), (struct prefix *)&p);
202 if (rn) {
203 ospf_external_info_free(rn->info);
204 rn->info = NULL;
205 route_unlock_node(rn);
206 route_unlock_node(rn);
207 }
208 }
209
210 struct external_info *ospf_external_info_lookup(struct ospf *ospf, u_char type,
211 u_short instance,
212 struct prefix_ipv4 *p)
213 {
214 struct route_node *rn;
215 struct ospf_external *ext;
216
217 ext = ospf_external_lookup(ospf, type, instance);
218 if (!ext)
219 return NULL;
220
221 rn = route_node_lookup(EXTERNAL_INFO(ext), (struct prefix *)p);
222 if (rn) {
223 route_unlock_node(rn);
224 if (rn->info)
225 return rn->info;
226 }
227
228 return NULL;
229 }
230
231 struct ospf_lsa *ospf_external_info_find_lsa(struct ospf *ospf,
232 struct prefix_ipv4 *p)
233 {
234 struct ospf_lsa *lsa;
235 struct as_external_lsa *al;
236 struct in_addr mask, id;
237
238 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
239 p->prefix, ospf->router_id);
240
241 if (!lsa)
242 return NULL;
243
244 al = (struct as_external_lsa *)lsa->data;
245
246 masklen2ip(p->prefixlen, &mask);
247
248 if (mask.s_addr != al->mask.s_addr) {
249 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
250 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
251 id, ospf->router_id);
252 if (!lsa)
253 return NULL;
254 }
255
256 return lsa;
257 }
258
259
260 /* Update ASBR status. */
261 void ospf_asbr_status_update(struct ospf *ospf, u_char status)
262 {
263 zlog_info("ASBR[Status:%d]: Update", status);
264
265 /* ASBR on. */
266 if (status) {
267 /* Already ASBR. */
268 if (IS_OSPF_ASBR(ospf)) {
269 zlog_info("ASBR[Status:%d]: Already ASBR", status);
270 return;
271 }
272 SET_FLAG(ospf->flags, OSPF_FLAG_ASBR);
273 } else {
274 /* Already non ASBR. */
275 if (!IS_OSPF_ASBR(ospf)) {
276 zlog_info("ASBR[Status:%d]: Already non ASBR", status);
277 return;
278 }
279 UNSET_FLAG(ospf->flags, OSPF_FLAG_ASBR);
280 }
281
282 /* Transition from/to status ASBR, schedule timer. */
283 ospf_spf_calculate_schedule(ospf, SPF_FLAG_ASBR_STATUS_CHANGE);
284 ospf_router_lsa_update(ospf);
285 }
286
287 void ospf_redistribute_withdraw(struct ospf *ospf, u_char type,
288 u_short instance)
289 {
290 struct route_node *rn;
291 struct external_info *ei;
292 struct ospf_external *ext;
293
294 ext = ospf_external_lookup(ospf, type, instance);
295 if (!ext)
296 return;
297
298 /* Delete external info for specified type. */
299 if (EXTERNAL_INFO(ext))
300 for (rn = route_top(EXTERNAL_INFO(ext)); rn;
301 rn = route_next(rn))
302 if ((ei = rn->info))
303 if (ospf_external_info_find_lsa(ospf, &ei->p)) {
304 if (is_prefix_default(&ei->p)
305 && ospf->default_originate
306 != DEFAULT_ORIGINATE_NONE)
307 continue;
308 ospf_external_lsa_flush(
309 ospf, type, &ei->p,
310 ei->ifindex /*, ei->nexthop */);
311
312 ospf_external_info_free(ei);
313 route_unlock_node(rn);
314 rn->info = NULL;
315 }
316 }