]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_asbr.c
ospfd: replace inet_ntoa
[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[%pFX]: external path deleted", p);
57
58 /* Remove route from zebra. */
59 if (or->type == OSPF_DESTINATION_NETWORK)
60 ospf_zebra_delete(
61 ospf, (struct prefix_ipv4 *)&rn->p, or);
62
63 ospf_route_free(or);
64 rn->info = NULL;
65
66 route_unlock_node(rn);
67 route_unlock_node(rn);
68 return;
69 }
70
71 zlog_info("Route[%pFX]: no such external path", p);
72 }
73
74 /* Add an External info for AS-external-LSA. */
75 struct external_info *ospf_external_info_new(uint8_t type,
76 unsigned short instance)
77 {
78 struct external_info *new;
79
80 new = XCALLOC(MTYPE_OSPF_EXTERNAL_INFO, sizeof(struct external_info));
81 new->type = type;
82 new->instance = instance;
83
84 ospf_reset_route_map_set_values(&new->route_map_set);
85 return new;
86 }
87
88 static void ospf_external_info_free(struct external_info *ei)
89 {
90 XFREE(MTYPE_OSPF_EXTERNAL_INFO, ei);
91 }
92
93 void ospf_reset_route_map_set_values(struct route_map_set_values *values)
94 {
95 values->metric = -1;
96 values->metric_type = -1;
97 }
98
99 int ospf_route_map_set_compare(struct route_map_set_values *values1,
100 struct route_map_set_values *values2)
101 {
102 return values1->metric == values2->metric
103 && values1->metric_type == values2->metric_type;
104 }
105
106 /* Add an External info for AS-external-LSA. */
107 struct external_info *
108 ospf_external_info_add(struct ospf *ospf, uint8_t type, unsigned short instance,
109 struct prefix_ipv4 p, ifindex_t ifindex,
110 struct in_addr nexthop, route_tag_t tag)
111 {
112 struct external_info *new;
113 struct route_node *rn;
114 struct ospf_external *ext;
115 char inetbuf[INET6_BUFSIZ];
116
117 ext = ospf_external_lookup(ospf, type, instance);
118 if (!ext)
119 ext = ospf_external_add(ospf, type, instance);
120
121 rn = route_node_get(EXTERNAL_INFO(ext), (struct prefix *)&p);
122 /* If old info exists, -- discard new one or overwrite with new one? */
123 if (rn)
124 if (rn->info) {
125 new = rn->info;
126 if ((new->ifindex == ifindex)
127 && (new->nexthop.s_addr == nexthop.s_addr)
128 && (new->tag == tag)) {
129 route_unlock_node(rn);
130 return NULL; /* NULL => no LSA to refresh */
131 }
132
133 inet_ntop(AF_INET, (void *)&nexthop.s_addr, inetbuf,
134 sizeof(inetbuf));
135 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
136 zlog_debug(
137 "Redistribute[%s][%d][%u]: %pFX discarding old info with NH %s.",
138 ospf_redist_string(type), instance,
139 ospf->vrf_id, &p, inetbuf);
140 XFREE(MTYPE_OSPF_EXTERNAL_INFO, rn->info);
141 }
142
143 /* Create new External info instance. */
144 new = ospf_external_info_new(type, instance);
145 new->p = p;
146 new->ifindex = ifindex;
147 new->nexthop = nexthop;
148 new->tag = tag;
149 new->orig_tag = tag;
150
151 /* we don't unlock rn from the get() because we're attaching the info */
152 if (rn)
153 rn->info = new;
154
155 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
156 inet_ntop(AF_INET, (void *)&nexthop.s_addr, inetbuf,
157 sizeof(inetbuf));
158 zlog_debug(
159 "Redistribute[%s][%u]: %pFX external info created, with NH %s",
160 ospf_redist_string(type), ospf->vrf_id,
161 &p, inetbuf);
162 }
163 return new;
164 }
165
166 void ospf_external_info_delete(struct ospf *ospf, uint8_t type,
167 unsigned short instance, struct prefix_ipv4 p)
168 {
169 struct route_node *rn;
170 struct ospf_external *ext;
171
172 ext = ospf_external_lookup(ospf, type, instance);
173 if (!ext)
174 return;
175
176 rn = route_node_lookup(EXTERNAL_INFO(ext), (struct prefix *)&p);
177 if (rn) {
178 ospf_external_info_free(rn->info);
179 rn->info = NULL;
180 route_unlock_node(rn);
181 route_unlock_node(rn);
182 }
183 }
184
185 struct external_info *ospf_external_info_lookup(struct ospf *ospf, uint8_t type,
186 unsigned short instance,
187 struct prefix_ipv4 *p)
188 {
189 struct route_node *rn;
190 struct ospf_external *ext;
191
192 ext = ospf_external_lookup(ospf, type, instance);
193 if (!ext)
194 return NULL;
195
196 rn = route_node_lookup(EXTERNAL_INFO(ext), (struct prefix *)p);
197 if (rn) {
198 route_unlock_node(rn);
199 if (rn->info)
200 return rn->info;
201 }
202
203 return NULL;
204 }
205
206 struct ospf_lsa *ospf_external_info_find_lsa(struct ospf *ospf,
207 struct prefix_ipv4 *p)
208 {
209 struct ospf_lsa *lsa;
210 struct as_external_lsa *al;
211 struct in_addr mask, id;
212
213 /* Fisrt search the lsdb with address specifc LSID
214 * where all the host bits are set, if there a matched
215 * LSA, return.
216 * Ex: For route 10.0.0.0/16, LSID is 10.0.255.255
217 * If no lsa with above LSID, use received address as
218 * LSID and check if any LSA in LSDB.
219 * If LSA found, check if the mask is same b/w the matched
220 * LSA and received prefix, if same then it is the LSA for
221 * this prefix.
222 * Ex: For route 10.0.0.0/16, LSID is 10.0.0.0
223 */
224
225 masklen2ip(p->prefixlen, &mask);
226 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
227 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA, id,
228 ospf->router_id);
229 if (lsa)
230 return lsa;
231
232 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
233 p->prefix, ospf->router_id);
234
235 if (lsa) {
236 al = (struct as_external_lsa *)lsa->data;
237 if (mask.s_addr == al->mask.s_addr)
238 return lsa;
239 }
240
241 return NULL;
242 }
243
244
245 /* Update ASBR status. */
246 void ospf_asbr_status_update(struct ospf *ospf, uint8_t status)
247 {
248 zlog_info("ASBR[%s:Status:%d]: Update",
249 ospf_get_name(ospf), status);
250
251 /* ASBR on. */
252 if (status) {
253 /* Already ASBR. */
254 if (IS_OSPF_ASBR(ospf)) {
255 zlog_info("ASBR[%s:Status:%d]: Already ASBR",
256 ospf_get_name(ospf), status);
257 return;
258 }
259 SET_FLAG(ospf->flags, OSPF_FLAG_ASBR);
260 } else {
261 /* Already non ASBR. */
262 if (!IS_OSPF_ASBR(ospf)) {
263 zlog_info("ASBR[%s:Status:%d]: Already non ASBR",
264 ospf_get_name(ospf), status);
265 return;
266 }
267 UNSET_FLAG(ospf->flags, OSPF_FLAG_ASBR);
268 }
269
270 /* Transition from/to status ASBR, schedule timer. */
271 ospf_spf_calculate_schedule(ospf, SPF_FLAG_ASBR_STATUS_CHANGE);
272 ospf_router_lsa_update(ospf);
273 }
274
275 /* If there's redistribution configured, we need to refresh external
276 * LSAs in order to install Type-7 and flood to all NSSA Areas
277 */
278 void ospf_asbr_nssa_redist_task(struct ospf *ospf)
279 {
280 int type;
281
282 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
283 struct list *red_list;
284 struct listnode *node;
285 struct ospf_redist *red;
286
287 red_list = ospf->redist[type];
288 if (!red_list)
289 continue;
290
291 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
292 ospf_external_lsa_refresh_type(
293 ospf, type, red->instance, LSA_REFRESH_FORCE);
294 }
295
296 ospf_external_lsa_refresh_default(ospf);
297 }
298
299 void ospf_redistribute_withdraw(struct ospf *ospf, uint8_t type,
300 unsigned short instance)
301 {
302 struct route_node *rn;
303 struct external_info *ei;
304 struct ospf_external *ext;
305
306 ext = ospf_external_lookup(ospf, type, instance);
307 if (!ext)
308 return;
309
310 /* Delete external info for specified type. */
311 if (EXTERNAL_INFO(ext))
312 for (rn = route_top(EXTERNAL_INFO(ext)); rn;
313 rn = route_next(rn))
314 if ((ei = rn->info))
315 if (ospf_external_info_find_lsa(ospf, &ei->p)) {
316 if (is_prefix_default(&ei->p)
317 && ospf->default_originate
318 != DEFAULT_ORIGINATE_NONE)
319 continue;
320 ospf_external_lsa_flush(
321 ospf, type, &ei->p,
322 ei->ifindex /*, ei->nexthop */);
323
324 ospf_external_info_free(ei);
325 route_unlock_node(rn);
326 rn->info = NULL;
327 }
328 }