]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_asbr.c
release: FRR 3.0-rc1
[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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "thread.h"
26 #include "memory.h"
27 #include "linklist.h"
28 #include "prefix.h"
29 #include "if.h"
30 #include "table.h"
31 #include "vty.h"
32 #include "filter.h"
33 #include "log.h"
34
35 #include "ospfd/ospfd.h"
36 #include "ospfd/ospf_interface.h"
37 #include "ospfd/ospf_asbr.h"
38 #include "ospfd/ospf_lsa.h"
39 #include "ospfd/ospf_lsdb.h"
40 #include "ospfd/ospf_neighbor.h"
41 #include "ospfd/ospf_spf.h"
42 #include "ospfd/ospf_flood.h"
43 #include "ospfd/ospf_route.h"
44 #include "ospfd/ospf_zebra.h"
45 #include "ospfd/ospf_dump.h"
46
47
48 /* Remove external route. */
49 void ospf_external_route_remove(struct ospf *ospf, struct prefix_ipv4 *p)
50 {
51 struct route_node *rn;
52 struct ospf_route * or ;
53
54 rn = route_node_lookup(ospf->old_external_route, (struct prefix *)p);
55 if (rn)
56 if ((or = rn->info)) {
57 zlog_info("Route[%s/%d]: external path deleted",
58 inet_ntoa(p->prefix), p->prefixlen);
59
60 /* Remove route from zebra. */
61 if (or->type == OSPF_DESTINATION_NETWORK)
62 ospf_zebra_delete((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(u_char type, u_short instance,
131 struct prefix_ipv4 p,
132 ifindex_t ifindex,
133 struct in_addr nexthop,
134 route_tag_t tag)
135 {
136 struct external_info *new;
137 struct route_node *rn;
138 struct ospf_external *ext;
139 char inetbuf[INET6_BUFSIZ];
140
141 ext = ospf_external_lookup(type, instance);
142 if (!ext)
143 ext = ospf_external_add(type, instance);
144
145 rn = route_node_get(EXTERNAL_INFO(ext), (struct prefix *)&p);
146 /* If old info exists, -- discard new one or overwrite with new one? */
147 if (rn)
148 if (rn->info) {
149 new = rn->info;
150 if ((new->ifindex == ifindex)
151 && (new->nexthop.s_addr == nexthop.s_addr)
152 && (new->tag == tag)) {
153 route_unlock_node(rn);
154 return NULL; /* NULL => no LSA to refresh */
155 }
156
157 inet_ntop(AF_INET, (void *)&nexthop.s_addr, inetbuf,
158 INET6_BUFSIZ);
159 zlog_warn(
160 "Redistribute[%s][%d]: %s/%d discarding old info with NH %s.",
161 ospf_redist_string(type), instance,
162 inet_ntoa(p.prefix), p.prefixlen, inetbuf);
163 XFREE(MTYPE_OSPF_EXTERNAL_INFO, rn->info);
164 rn->info = NULL;
165 }
166
167 /* Create new External info instance. */
168 new = ospf_external_info_new(type, instance);
169 new->p = p;
170 new->ifindex = ifindex;
171 new->nexthop = nexthop;
172 new->tag = tag;
173
174 /* we don't unlock rn from the get() because we're attaching the info */
175 if (rn)
176 rn->info = new;
177
178 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
179 inet_ntop(AF_INET, (void *)&nexthop.s_addr, inetbuf,
180 INET6_BUFSIZ);
181 zlog_debug(
182 "Redistribute[%s]: %s/%d external info created, with NH %s",
183 ospf_redist_string(type), inet_ntoa(p.prefix),
184 p.prefixlen, inetbuf);
185 }
186 return new;
187 }
188
189 void ospf_external_info_delete(u_char type, u_short instance,
190 struct prefix_ipv4 p)
191 {
192 struct route_node *rn;
193 struct ospf_external *ext;
194
195 ext = ospf_external_lookup(type, instance);
196 if (!ext)
197 return;
198
199 rn = route_node_lookup(EXTERNAL_INFO(ext), (struct prefix *)&p);
200 if (rn) {
201 ospf_external_info_free(rn->info);
202 rn->info = NULL;
203 route_unlock_node(rn);
204 route_unlock_node(rn);
205 }
206 }
207
208 struct external_info *ospf_external_info_lookup(u_char type, u_short instance,
209 struct prefix_ipv4 *p)
210 {
211 struct route_node *rn;
212 struct ospf_external *ext;
213
214 ext = ospf_external_lookup(type, instance);
215 if (!ext)
216 return NULL;
217
218 rn = route_node_lookup(EXTERNAL_INFO(ext), (struct prefix *)p);
219 if (rn) {
220 route_unlock_node(rn);
221 if (rn->info)
222 return rn->info;
223 }
224
225 return NULL;
226 }
227
228 struct ospf_lsa *ospf_external_info_find_lsa(struct ospf *ospf,
229 struct prefix_ipv4 *p)
230 {
231 struct ospf_lsa *lsa;
232 struct as_external_lsa *al;
233 struct in_addr mask, id;
234
235 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
236 p->prefix, ospf->router_id);
237
238 if (!lsa)
239 return NULL;
240
241 al = (struct as_external_lsa *)lsa->data;
242
243 masklen2ip(p->prefixlen, &mask);
244
245 if (mask.s_addr != al->mask.s_addr) {
246 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
247 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
248 id, ospf->router_id);
249 if (!lsa)
250 return NULL;
251 }
252
253 return lsa;
254 }
255
256
257 /* Update ASBR status. */
258 void ospf_asbr_status_update(struct ospf *ospf, u_char status)
259 {
260 zlog_info("ASBR[Status:%d]: Update", status);
261
262 /* ASBR on. */
263 if (status) {
264 /* Already ASBR. */
265 if (IS_OSPF_ASBR(ospf)) {
266 zlog_info("ASBR[Status:%d]: Already ASBR", status);
267 return;
268 }
269 SET_FLAG(ospf->flags, OSPF_FLAG_ASBR);
270 } else {
271 /* Already non ASBR. */
272 if (!IS_OSPF_ASBR(ospf)) {
273 zlog_info("ASBR[Status:%d]: Already non ASBR", status);
274 return;
275 }
276 UNSET_FLAG(ospf->flags, OSPF_FLAG_ASBR);
277 }
278
279 /* Transition from/to status ASBR, schedule timer. */
280 ospf_spf_calculate_schedule(ospf, SPF_FLAG_ASBR_STATUS_CHANGE);
281 ospf_router_lsa_update(ospf);
282 }
283
284 void ospf_redistribute_withdraw(struct ospf *ospf, u_char type,
285 u_short instance)
286 {
287 struct route_node *rn;
288 struct external_info *ei;
289 struct ospf_external *ext;
290
291 ext = ospf_external_lookup(type, instance);
292 if (!ext)
293 return;
294
295 /* Delete external info for specified type. */
296 if (EXTERNAL_INFO(ext))
297 for (rn = route_top(EXTERNAL_INFO(ext)); rn;
298 rn = route_next(rn))
299 if ((ei = rn->info))
300 if (ospf_external_info_find_lsa(ospf, &ei->p)) {
301 if (is_prefix_default(&ei->p)
302 && ospf->default_originate
303 != DEFAULT_ORIGINATE_NONE)
304 continue;
305 ospf_external_lsa_flush(
306 ospf, type, &ei->p,
307 ei->ifindex /*, ei->nexthop */);
308
309 ospf_external_info_free(ei);
310 route_unlock_node(rn);
311 rn->info = NULL;
312 }
313 }