]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_asbr.c
Merge pull request #5722 from donaldsharp/kernel_routes
[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(
62 ospf, (struct prefix_ipv4 *)&rn->p, or);
63
64 ospf_route_free(or);
65 rn->info = NULL;
66
67 route_unlock_node(rn);
68 route_unlock_node(rn);
69 return;
70 }
71
72 zlog_info("Route[%s/%d]: no such external path", inet_ntoa(p->prefix),
73 p->prefixlen);
74 }
75
76 /* Add an External info for AS-external-LSA. */
77 struct external_info *ospf_external_info_new(uint8_t type,
78 unsigned short instance)
79 {
80 struct external_info *new;
81
82 new = XCALLOC(MTYPE_OSPF_EXTERNAL_INFO, sizeof(struct external_info));
83 new->type = type;
84 new->instance = instance;
85
86 ospf_reset_route_map_set_values(&new->route_map_set);
87 return new;
88 }
89
90 static void ospf_external_info_free(struct external_info *ei)
91 {
92 XFREE(MTYPE_OSPF_EXTERNAL_INFO, ei);
93 }
94
95 void ospf_reset_route_map_set_values(struct route_map_set_values *values)
96 {
97 values->metric = -1;
98 values->metric_type = -1;
99 }
100
101 int ospf_route_map_set_compare(struct route_map_set_values *values1,
102 struct route_map_set_values *values2)
103 {
104 return values1->metric == values2->metric
105 && values1->metric_type == values2->metric_type;
106 }
107
108 /* Add an External info for AS-external-LSA. */
109 struct external_info *
110 ospf_external_info_add(struct ospf *ospf, uint8_t type, unsigned short instance,
111 struct prefix_ipv4 p, ifindex_t ifindex,
112 struct in_addr nexthop, route_tag_t tag)
113 {
114 struct external_info *new;
115 struct route_node *rn;
116 struct ospf_external *ext;
117 char inetbuf[INET6_BUFSIZ];
118
119 ext = ospf_external_lookup(ospf, type, instance);
120 if (!ext)
121 ext = ospf_external_add(ospf, type, instance);
122
123 rn = route_node_get(EXTERNAL_INFO(ext), (struct prefix *)&p);
124 /* If old info exists, -- discard new one or overwrite with new one? */
125 if (rn)
126 if (rn->info) {
127 new = rn->info;
128 if ((new->ifindex == ifindex)
129 && (new->nexthop.s_addr == nexthop.s_addr)
130 && (new->tag == tag)) {
131 route_unlock_node(rn);
132 return NULL; /* NULL => no LSA to refresh */
133 }
134
135 inet_ntop(AF_INET, (void *)&nexthop.s_addr, inetbuf,
136 INET6_BUFSIZ);
137 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
138 zlog_debug(
139 "Redistribute[%s][%d][%u]: %s/%d discarding old info with NH %s.",
140 ospf_redist_string(type), instance,
141 ospf->vrf_id, inet_ntoa(p.prefix),
142 p.prefixlen, inetbuf);
143 XFREE(MTYPE_OSPF_EXTERNAL_INFO, rn->info);
144 }
145
146 /* Create new External info instance. */
147 new = ospf_external_info_new(type, instance);
148 new->p = p;
149 new->ifindex = ifindex;
150 new->nexthop = nexthop;
151 new->tag = tag;
152
153 /* we don't unlock rn from the get() because we're attaching the info */
154 if (rn)
155 rn->info = new;
156
157 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
158 inet_ntop(AF_INET, (void *)&nexthop.s_addr, inetbuf,
159 INET6_BUFSIZ);
160 zlog_debug(
161 "Redistribute[%s][%u]: %s/%d external info created, with NH %s",
162 ospf_redist_string(type), ospf->vrf_id,
163 inet_ntoa(p.prefix), p.prefixlen, inetbuf);
164 }
165 return new;
166 }
167
168 void ospf_external_info_delete(struct ospf *ospf, uint8_t type,
169 unsigned short instance, struct prefix_ipv4 p)
170 {
171 struct route_node *rn;
172 struct ospf_external *ext;
173
174 ext = ospf_external_lookup(ospf, type, instance);
175 if (!ext)
176 return;
177
178 rn = route_node_lookup(EXTERNAL_INFO(ext), (struct prefix *)&p);
179 if (rn) {
180 ospf_external_info_free(rn->info);
181 rn->info = NULL;
182 route_unlock_node(rn);
183 route_unlock_node(rn);
184 }
185 }
186
187 struct external_info *ospf_external_info_lookup(struct ospf *ospf, uint8_t type,
188 unsigned short instance,
189 struct prefix_ipv4 *p)
190 {
191 struct route_node *rn;
192 struct ospf_external *ext;
193
194 ext = ospf_external_lookup(ospf, type, instance);
195 if (!ext)
196 return NULL;
197
198 rn = route_node_lookup(EXTERNAL_INFO(ext), (struct prefix *)p);
199 if (rn) {
200 route_unlock_node(rn);
201 if (rn->info)
202 return rn->info;
203 }
204
205 return NULL;
206 }
207
208 struct ospf_lsa *ospf_external_info_find_lsa(struct ospf *ospf,
209 struct prefix_ipv4 *p)
210 {
211 struct ospf_lsa *lsa;
212 struct as_external_lsa *al;
213 struct in_addr mask, id;
214
215 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
216 p->prefix, ospf->router_id);
217
218 if (!lsa)
219 return NULL;
220
221 al = (struct as_external_lsa *)lsa->data;
222
223 masklen2ip(p->prefixlen, &mask);
224
225 if (mask.s_addr != al->mask.s_addr) {
226 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
227 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
228 id, ospf->router_id);
229 if (!lsa)
230 return NULL;
231 }
232
233 return lsa;
234 }
235
236
237 /* Update ASBR status. */
238 void ospf_asbr_status_update(struct ospf *ospf, uint8_t status)
239 {
240 zlog_info("ASBR[%s:Status:%d]: Update",
241 ospf_get_name(ospf), status);
242
243 /* ASBR on. */
244 if (status) {
245 /* Already ASBR. */
246 if (IS_OSPF_ASBR(ospf)) {
247 zlog_info("ASBR[%s:Status:%d]: Already ASBR",
248 ospf_get_name(ospf), status);
249 return;
250 }
251 SET_FLAG(ospf->flags, OSPF_FLAG_ASBR);
252 } else {
253 /* Already non ASBR. */
254 if (!IS_OSPF_ASBR(ospf)) {
255 zlog_info("ASBR[%s:Status:%d]: Already non ASBR",
256 ospf_get_name(ospf), status);
257 return;
258 }
259 UNSET_FLAG(ospf->flags, OSPF_FLAG_ASBR);
260 }
261
262 /* Transition from/to status ASBR, schedule timer. */
263 ospf_spf_calculate_schedule(ospf, SPF_FLAG_ASBR_STATUS_CHANGE);
264 ospf_router_lsa_update(ospf);
265 }
266
267 void ospf_redistribute_withdraw(struct ospf *ospf, uint8_t type,
268 unsigned short instance)
269 {
270 struct route_node *rn;
271 struct external_info *ei;
272 struct ospf_external *ext;
273
274 ext = ospf_external_lookup(ospf, type, instance);
275 if (!ext)
276 return;
277
278 /* Delete external info for specified type. */
279 if (EXTERNAL_INFO(ext))
280 for (rn = route_top(EXTERNAL_INFO(ext)); rn;
281 rn = route_next(rn))
282 if ((ei = rn->info))
283 if (ospf_external_info_find_lsa(ospf, &ei->p)) {
284 if (is_prefix_default(&ei->p)
285 && ospf->default_originate
286 != DEFAULT_ORIGINATE_NONE)
287 continue;
288 ospf_external_lsa_flush(
289 ospf, type, &ei->p,
290 ei->ifindex /*, ei->nexthop */);
291
292 ospf_external_info_free(ei);
293 route_unlock_node(rn);
294 rn->info = NULL;
295 }
296 }