]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * OSPF Interface functions. | |
3 | * Copyright (C) 1999, 2000 Toshiaki Takada | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
896014f4 | 6 | * |
718e3744 | 7 | * GNU Zebra is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation; either version 2, or (at your | |
10 | * option) any 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 | * | |
896014f4 DL |
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 | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "thread.h" | |
25 | #include "linklist.h" | |
26 | #include "prefix.h" | |
27 | #include "if.h" | |
28 | #include "table.h" | |
29 | #include "memory.h" | |
30 | #include "command.h" | |
31 | #include "stream.h" | |
32 | #include "log.h" | |
c0325cfa | 33 | #include "zclient.h" |
7f342629 | 34 | #include "bfd.h" |
718e3744 | 35 | |
68980084 | 36 | #include "ospfd/ospfd.h" |
718e3744 | 37 | #include "ospfd/ospf_spf.h" |
38 | #include "ospfd/ospf_interface.h" | |
39 | #include "ospfd/ospf_ism.h" | |
40 | #include "ospfd/ospf_asbr.h" | |
41 | #include "ospfd/ospf_lsa.h" | |
42 | #include "ospfd/ospf_lsdb.h" | |
43 | #include "ospfd/ospf_neighbor.h" | |
44 | #include "ospfd/ospf_nsm.h" | |
45 | #include "ospfd/ospf_packet.h" | |
46 | #include "ospfd/ospf_abr.h" | |
718e3744 | 47 | #include "ospfd/ospf_network.h" |
48 | #include "ospfd/ospf_dump.h" | |
718e3744 | 49 | |
ae19c240 | 50 | DEFINE_QOBJ_TYPE(ospf_interface) |
d62a17ae | 51 | DEFINE_HOOK(ospf_vl_add, (struct ospf_vl_data * vd), (vd)) |
52 | DEFINE_HOOK(ospf_vl_delete, (struct ospf_vl_data * vd), (vd)) | |
ef7bd2a3 | 53 | DEFINE_HOOK(ospf_if_update, (struct interface * ifp), (ifp)) |
3c3c3252 | 54 | DEFINE_HOOK(ospf_if_delete, (struct interface * ifp), (ifp)) |
6b0655a2 | 55 | |
2bc7673f CS |
56 | int ospf_interface_neighbor_count(struct ospf_interface *oi) |
57 | { | |
58 | int count = 0; | |
59 | struct route_node *rn; | |
60 | struct ospf_neighbor *nbr = NULL; | |
61 | ||
62 | for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) { | |
63 | nbr = rn->info; | |
64 | if (nbr) { | |
65 | /* Do not show myself. */ | |
66 | if (nbr == oi->nbr_self) | |
67 | continue; | |
68 | /* Down state is not shown. */ | |
69 | if (nbr->state == NSM_Down) | |
70 | continue; | |
71 | count++; | |
72 | } | |
73 | } | |
74 | ||
75 | return count; | |
76 | } | |
77 | ||
d62a17ae | 78 | int ospf_if_get_output_cost(struct ospf_interface *oi) |
718e3744 | 79 | { |
d62a17ae | 80 | /* If all else fails, use default OSPF cost */ |
d7c0a89a QY |
81 | uint32_t cost; |
82 | uint32_t bw, refbw; | |
d62a17ae | 83 | |
84 | /* ifp speed and bw can be 0 in some platforms, use ospf default bw | |
85 | if bw is configured under interface it would be used. | |
86 | */ | |
87 | if (!oi->ifp->bandwidth && oi->ifp->speed) | |
88 | bw = oi->ifp->speed; | |
89 | else | |
90 | bw = oi->ifp->bandwidth ? oi->ifp->bandwidth | |
91 | : OSPF_DEFAULT_BANDWIDTH; | |
92 | refbw = oi->ospf->ref_bandwidth; | |
93 | ||
94 | /* A specifed ip ospf cost overrides a calculated one. */ | |
95 | if (OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(oi->ifp), output_cost_cmd) | |
96 | || OSPF_IF_PARAM_CONFIGURED(oi->params, output_cost_cmd)) | |
97 | cost = OSPF_IF_PARAM(oi, output_cost_cmd); | |
98 | /* See if a cost can be calculated from the zebra processes | |
99 | interface bandwidth field. */ | |
100 | else { | |
d7c0a89a | 101 | cost = (uint32_t)((double)refbw / (double)bw + (double)0.5); |
d62a17ae | 102 | if (cost < 1) |
103 | cost = 1; | |
104 | else if (cost > 65535) | |
105 | cost = 65535; | |
106 | } | |
107 | ||
108 | return cost; | |
718e3744 | 109 | } |
110 | ||
d62a17ae | 111 | void ospf_if_recalculate_output_cost(struct interface *ifp) |
718e3744 | 112 | { |
d7c0a89a | 113 | uint32_t newcost; |
d62a17ae | 114 | struct route_node *rn; |
115 | ||
116 | for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) { | |
117 | struct ospf_interface *oi; | |
118 | ||
119 | if ((oi = rn->info) == NULL) | |
120 | continue; | |
121 | ||
122 | newcost = ospf_if_get_output_cost(oi); | |
123 | ||
124 | /* Is actual output cost changed? */ | |
125 | if (oi->output_cost != newcost) { | |
126 | oi->output_cost = newcost; | |
127 | ospf_router_lsa_update_area(oi->area); | |
128 | } | |
718e3744 | 129 | } |
718e3744 | 130 | } |
131 | ||
d62a17ae | 132 | /* Simulate down/up on the interface. This is needed, for example, when |
a608bbf2 | 133 | the MTU changes. */ |
d62a17ae | 134 | void ospf_if_reset(struct interface *ifp) |
a608bbf2 | 135 | { |
d62a17ae | 136 | struct route_node *rn; |
137 | ||
138 | for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) { | |
139 | struct ospf_interface *oi; | |
140 | ||
141 | if ((oi = rn->info) == NULL) | |
142 | continue; | |
143 | ||
144 | ospf_if_down(oi); | |
145 | ospf_if_up(oi); | |
146 | } | |
a608bbf2 | 147 | } |
148 | ||
d62a17ae | 149 | void ospf_if_reset_variables(struct ospf_interface *oi) |
718e3744 | 150 | { |
d62a17ae | 151 | /* Set default values. */ |
152 | /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */ | |
718e3744 | 153 | |
d62a17ae | 154 | if (oi->vl_data) |
155 | oi->type = OSPF_IFTYPE_VIRTUALLINK; | |
156 | else | |
157 | /* preserve network-type */ | |
158 | if (oi->type != OSPF_IFTYPE_NBMA) | |
159 | oi->type = OSPF_IFTYPE_BROADCAST; | |
718e3744 | 160 | |
d62a17ae | 161 | oi->state = ISM_Down; |
718e3744 | 162 | |
d62a17ae | 163 | oi->crypt_seqnum = 0; |
718e3744 | 164 | |
d62a17ae | 165 | /* This must be short, (less than RxmtInterval) |
166 | - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being | |
167 | held back for too long - MAG */ | |
168 | oi->v_ls_ack = 1; | |
718e3744 | 169 | } |
170 | ||
20916fba | 171 | /* lookup oi for specified prefix/ifp */ |
d62a17ae | 172 | struct ospf_interface *ospf_if_table_lookup(struct interface *ifp, |
173 | struct prefix *prefix) | |
20916fba | 174 | { |
d62a17ae | 175 | struct prefix p; |
176 | struct route_node *rn; | |
177 | struct ospf_interface *rninfo = NULL; | |
178 | ||
179 | p = *prefix; | |
180 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
181 | ||
182 | /* route_node_get implicitely locks */ | |
183 | if ((rn = route_node_lookup(IF_OIFS(ifp), &p))) { | |
184 | rninfo = (struct ospf_interface *)rn->info; | |
185 | route_unlock_node(rn); | |
186 | } | |
187 | ||
188 | return rninfo; | |
20916fba | 189 | } |
190 | ||
d62a17ae | 191 | static void ospf_add_to_if(struct interface *ifp, struct ospf_interface *oi) |
718e3744 | 192 | { |
d62a17ae | 193 | struct route_node *rn; |
194 | struct prefix p; | |
195 | ||
196 | p = *oi->address; | |
197 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
a6435618 | 198 | apply_mask(&p); |
d62a17ae | 199 | |
200 | rn = route_node_get(IF_OIFS(ifp), &p); | |
201 | /* rn->info should either be NULL or equal to this oi | |
202 | * as route_node_get may return an existing node | |
203 | */ | |
204 | assert(!rn->info || rn->info == oi); | |
205 | rn->info = oi; | |
718e3744 | 206 | } |
207 | ||
d62a17ae | 208 | static void ospf_delete_from_if(struct interface *ifp, |
209 | struct ospf_interface *oi) | |
718e3744 | 210 | { |
d62a17ae | 211 | struct route_node *rn; |
212 | struct prefix p; | |
213 | ||
214 | p = *oi->address; | |
215 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
216 | ||
217 | rn = route_node_lookup(IF_OIFS(oi->ifp), &p); | |
218 | assert(rn); | |
219 | assert(rn->info); | |
220 | rn->info = NULL; | |
221 | route_unlock_node(rn); | |
222 | route_unlock_node(rn); | |
718e3744 | 223 | } |
224 | ||
d62a17ae | 225 | struct ospf_interface *ospf_if_new(struct ospf *ospf, struct interface *ifp, |
226 | struct prefix *p) | |
718e3744 | 227 | { |
d62a17ae | 228 | struct ospf_interface *oi; |
229 | ||
36a106e0 DS |
230 | oi = ospf_if_table_lookup(ifp, p); |
231 | if (oi) | |
d62a17ae | 232 | return oi; |
233 | ||
36a106e0 DS |
234 | oi = XCALLOC(MTYPE_OSPF_IF, sizeof(struct ospf_interface)); |
235 | ||
236 | oi->obuf = ospf_fifo_new(); | |
237 | ||
d62a17ae | 238 | /* Set zebra interface pointer. */ |
239 | oi->ifp = ifp; | |
240 | oi->address = p; | |
241 | ||
242 | ospf_add_to_if(ifp, oi); | |
243 | listnode_add(ospf->oiflist, oi); | |
244 | ||
245 | /* Initialize neighbor list. */ | |
246 | oi->nbrs = route_table_init(); | |
247 | ||
248 | /* Initialize static neighbor list. */ | |
249 | oi->nbr_nbma = list_new(); | |
250 | ||
251 | /* Initialize Link State Acknowledgment list. */ | |
252 | oi->ls_ack = list_new(); | |
253 | oi->ls_ack_direct.ls_ack = list_new(); | |
254 | ||
255 | /* Set default values. */ | |
256 | ospf_if_reset_variables(oi); | |
257 | ||
258 | /* Set pseudo neighbor to Null */ | |
259 | oi->nbr_self = NULL; | |
260 | ||
261 | oi->ls_upd_queue = route_table_init(); | |
262 | oi->t_ls_upd_event = NULL; | |
263 | oi->t_ls_ack_direct = NULL; | |
264 | ||
265 | oi->crypt_seqnum = time(NULL); | |
266 | ||
267 | ospf_opaque_type9_lsa_init(oi); | |
268 | ||
269 | oi->ospf = ospf; | |
4fc8a852 | 270 | |
d62a17ae | 271 | QOBJ_REG(oi, ospf_interface); |
272 | ||
b5a8894d CS |
273 | if (IS_DEBUG_OSPF_EVENT) |
274 | zlog_debug("%s: ospf interface %s vrf %s id %u created", | |
275 | __PRETTY_FUNCTION__, ifp->name, | |
276 | ospf_vrf_id_to_name(ospf->vrf_id), ospf->vrf_id); | |
277 | ||
d62a17ae | 278 | return oi; |
718e3744 | 279 | } |
280 | ||
281 | /* Restore an interface to its pre UP state | |
282 | Used from ism_interface_down only */ | |
d62a17ae | 283 | void ospf_if_cleanup(struct ospf_interface *oi) |
718e3744 | 284 | { |
d62a17ae | 285 | struct route_node *rn; |
286 | struct listnode *node, *nnode; | |
287 | struct ospf_neighbor *nbr; | |
288 | struct ospf_nbr_nbma *nbr_nbma; | |
289 | struct ospf_lsa *lsa; | |
290 | ||
291 | /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */ | |
292 | /* delete all static neighbors attached to this interface */ | |
293 | for (ALL_LIST_ELEMENTS(oi->nbr_nbma, node, nnode, nbr_nbma)) { | |
294 | OSPF_POLL_TIMER_OFF(nbr_nbma->t_poll); | |
295 | ||
296 | if (nbr_nbma->nbr) { | |
297 | nbr_nbma->nbr->nbr_nbma = NULL; | |
298 | nbr_nbma->nbr = NULL; | |
299 | } | |
300 | ||
301 | nbr_nbma->oi = NULL; | |
302 | ||
303 | listnode_delete(oi->nbr_nbma, nbr_nbma); | |
718e3744 | 304 | } |
305 | ||
d62a17ae | 306 | /* send Neighbor event KillNbr to all associated neighbors. */ |
307 | for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) | |
308 | if ((nbr = rn->info) != NULL) | |
309 | if (nbr != oi->nbr_self) | |
310 | OSPF_NSM_EVENT_EXECUTE(nbr, NSM_KillNbr); | |
311 | ||
312 | /* Cleanup Link State Acknowlegdment list. */ | |
313 | for (ALL_LIST_ELEMENTS(oi->ls_ack, node, nnode, lsa)) | |
314 | ospf_lsa_unlock(&lsa); /* oi->ls_ack */ | |
315 | list_delete_all_node(oi->ls_ack); | |
316 | ||
317 | oi->crypt_seqnum = 0; | |
318 | ||
319 | /* Empty link state update queue */ | |
320 | ospf_ls_upd_queue_empty(oi); | |
321 | ||
322 | /* Reset pseudo neighbor. */ | |
323 | ospf_nbr_self_reset(oi, oi->ospf->router_id); | |
718e3744 | 324 | } |
325 | ||
d62a17ae | 326 | void ospf_if_free(struct ospf_interface *oi) |
718e3744 | 327 | { |
d62a17ae | 328 | ospf_if_down(oi); |
329 | ||
36a106e0 | 330 | ospf_fifo_free(oi->obuf); |
4fc8a852 | 331 | |
d62a17ae | 332 | assert(oi->state == ISM_Down); |
333 | ||
334 | ospf_opaque_type9_lsa_term(oi); | |
335 | ||
336 | QOBJ_UNREG(oi); | |
337 | ||
338 | /* Free Pseudo Neighbour */ | |
339 | ospf_nbr_delete(oi->nbr_self); | |
718e3744 | 340 | |
d62a17ae | 341 | route_table_finish(oi->nbrs); |
342 | route_table_finish(oi->ls_upd_queue); | |
718e3744 | 343 | |
d62a17ae | 344 | /* Free any lists that should be freed */ |
6a154c88 | 345 | list_delete(&oi->nbr_nbma); |
718e3744 | 346 | |
6a154c88 DL |
347 | list_delete(&oi->ls_ack); |
348 | list_delete(&oi->ls_ack_direct.ls_ack); | |
ae19c240 | 349 | |
b5a8894d CS |
350 | if (IS_DEBUG_OSPF_EVENT) |
351 | zlog_debug("%s: ospf interface %s vrf %s id %u deleted", | |
352 | __PRETTY_FUNCTION__, oi->ifp->name, | |
a36898e7 DS |
353 | ospf_vrf_id_to_name(oi->ifp->vrf_id), |
354 | oi->ifp->vrf_id); | |
b5a8894d | 355 | |
d62a17ae | 356 | ospf_delete_from_if(oi->ifp, oi); |
718e3744 | 357 | |
d62a17ae | 358 | listnode_delete(oi->ospf->oiflist, oi); |
359 | listnode_delete(oi->area->oiflist, oi); | |
718e3744 | 360 | |
d62a17ae | 361 | thread_cancel_event(master, oi); |
cfd670f3 | 362 | |
d62a17ae | 363 | memset(oi, 0, sizeof(*oi)); |
364 | XFREE(MTYPE_OSPF_IF, oi); | |
718e3744 | 365 | } |
366 | ||
d62a17ae | 367 | int ospf_if_is_up(struct ospf_interface *oi) |
718e3744 | 368 | { |
d62a17ae | 369 | return if_is_up(oi->ifp); |
718e3744 | 370 | } |
371 | ||
d62a17ae | 372 | struct ospf_interface *ospf_if_exists(struct ospf_interface *oic) |
373 | { | |
374 | struct listnode *node; | |
375 | struct ospf *ospf; | |
376 | struct ospf_interface *oi; | |
2db3d05d | 377 | |
b5a8894d CS |
378 | if (!oic) |
379 | return NULL; | |
380 | ||
381 | ospf = oic->ospf; | |
382 | if (ospf == NULL) | |
d62a17ae | 383 | return NULL; |
2db3d05d | 384 | |
d62a17ae | 385 | for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) |
386 | if (oi == oic) | |
387 | return oi; | |
1eb8ef25 | 388 | |
d62a17ae | 389 | return NULL; |
2db3d05d | 390 | } |
391 | ||
c81ee5c9 | 392 | /* Lookup OSPF interface by router LSA posistion */ |
d62a17ae | 393 | struct ospf_interface *ospf_if_lookup_by_lsa_pos(struct ospf_area *area, |
394 | int lsa_pos) | |
c81ee5c9 | 395 | { |
d62a17ae | 396 | struct listnode *node; |
397 | struct ospf_interface *oi; | |
c81ee5c9 | 398 | |
d62a17ae | 399 | for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) { |
400 | if (lsa_pos >= oi->lsa_pos_beg && lsa_pos < oi->lsa_pos_end) | |
401 | return oi; | |
402 | } | |
403 | return NULL; | |
c81ee5c9 JT |
404 | } |
405 | ||
d62a17ae | 406 | struct ospf_interface *ospf_if_lookup_by_local_addr(struct ospf *ospf, |
407 | struct interface *ifp, | |
408 | struct in_addr address) | |
718e3744 | 409 | { |
d62a17ae | 410 | struct listnode *node; |
411 | struct ospf_interface *oi; | |
412 | ||
413 | for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) | |
414 | if (oi->type != OSPF_IFTYPE_VIRTUALLINK) { | |
415 | if (ifp && oi->ifp != ifp) | |
416 | continue; | |
417 | ||
418 | if (IPV4_ADDR_SAME(&address, &oi->address->u.prefix4)) | |
419 | return oi; | |
420 | } | |
421 | ||
422 | return NULL; | |
718e3744 | 423 | } |
424 | ||
d62a17ae | 425 | struct ospf_interface *ospf_if_lookup_by_prefix(struct ospf *ospf, |
426 | struct prefix_ipv4 *p) | |
718e3744 | 427 | { |
d62a17ae | 428 | struct listnode *node; |
429 | struct ospf_interface *oi; | |
430 | ||
431 | /* Check each Interface. */ | |
432 | for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) { | |
433 | if (oi->type != OSPF_IFTYPE_VIRTUALLINK) { | |
434 | struct prefix ptmp; | |
435 | ||
436 | prefix_copy(&ptmp, CONNECTED_PREFIX(oi->connected)); | |
437 | apply_mask(&ptmp); | |
438 | if (prefix_same(&ptmp, (struct prefix *)p)) | |
439 | return oi; | |
440 | } | |
68980084 | 441 | } |
d62a17ae | 442 | return NULL; |
718e3744 | 443 | } |
444 | ||
05cf46ba | 445 | /* determine receiving interface by ifp and source address */ |
d62a17ae | 446 | struct ospf_interface *ospf_if_lookup_recv_if(struct ospf *ospf, |
447 | struct in_addr src, | |
448 | struct interface *ifp) | |
718e3744 | 449 | { |
d62a17ae | 450 | struct route_node *rn; |
451 | struct prefix_ipv4 addr; | |
452 | struct ospf_interface *oi, *match; | |
453 | ||
454 | addr.family = AF_INET; | |
455 | addr.prefix = src; | |
456 | addr.prefixlen = IPV4_MAX_BITLEN; | |
457 | ||
458 | match = NULL; | |
459 | ||
460 | for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) { | |
461 | oi = rn->info; | |
462 | ||
463 | if (!oi) /* oi can be NULL for PtP aliases */ | |
464 | continue; | |
465 | ||
466 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) | |
467 | continue; | |
468 | ||
0c74bbe0 | 469 | if (if_is_loopback(oi->ifp) || if_is_vrf(oi->ifp)) |
d62a17ae | 470 | continue; |
471 | ||
472 | if (CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)) | |
473 | match = oi; | |
474 | else if (prefix_match(CONNECTED_PREFIX(oi->connected), | |
475 | (struct prefix *)&addr)) { | |
9d303b37 DL |
476 | if ((match == NULL) || (match->address->prefixlen |
477 | < oi->address->prefixlen)) | |
d62a17ae | 478 | match = oi; |
479 | } | |
718e3744 | 480 | } |
718e3744 | 481 | |
d62a17ae | 482 | return match; |
718e3744 | 483 | } |
6b0655a2 | 484 | |
85c8ef06 CS |
485 | static void ospf_if_reset_stats(struct ospf_interface *oi) |
486 | { | |
487 | oi->hello_in = oi->hello_out = 0; | |
488 | oi->db_desc_in = oi->db_desc_out = 0; | |
489 | oi->ls_req_in = oi->ls_req_out = 0; | |
490 | oi->ls_upd_in = oi->ls_upd_out = 0; | |
491 | oi->ls_ack_in = oi->ls_ack_out = 0; | |
492 | } | |
493 | ||
d62a17ae | 494 | void ospf_if_stream_unset(struct ospf_interface *oi) |
718e3744 | 495 | { |
d62a17ae | 496 | struct ospf *ospf = oi->ospf; |
497 | ||
36a106e0 DS |
498 | /* flush the interface packet queue */ |
499 | ospf_fifo_flush(oi->obuf); | |
500 | /*reset protocol stats */ | |
501 | ospf_if_reset_stats(oi); | |
502 | ||
503 | if (oi->on_write_q) { | |
504 | listnode_delete(ospf->oi_write_q, oi); | |
505 | if (list_isempty(ospf->oi_write_q)) | |
506 | OSPF_TIMER_OFF(ospf->t_write); | |
507 | oi->on_write_q = 0; | |
d62a17ae | 508 | } |
718e3744 | 509 | } |
68980084 | 510 | |
6b0655a2 | 511 | |
d62a17ae | 512 | static struct ospf_if_params *ospf_new_if_params(void) |
718e3744 | 513 | { |
d62a17ae | 514 | struct ospf_if_params *oip; |
515 | ||
516 | oip = XCALLOC(MTYPE_OSPF_IF_PARAMS, sizeof(struct ospf_if_params)); | |
517 | ||
d62a17ae | 518 | UNSET_IF_PARAM(oip, output_cost_cmd); |
519 | UNSET_IF_PARAM(oip, transmit_delay); | |
520 | UNSET_IF_PARAM(oip, retransmit_interval); | |
521 | UNSET_IF_PARAM(oip, passive_interface); | |
522 | UNSET_IF_PARAM(oip, v_hello); | |
523 | UNSET_IF_PARAM(oip, fast_hello); | |
524 | UNSET_IF_PARAM(oip, v_wait); | |
525 | UNSET_IF_PARAM(oip, priority); | |
526 | UNSET_IF_PARAM(oip, type); | |
527 | UNSET_IF_PARAM(oip, auth_simple); | |
528 | UNSET_IF_PARAM(oip, auth_crypt); | |
529 | UNSET_IF_PARAM(oip, auth_type); | |
530 | ||
531 | oip->auth_crypt = list_new(); | |
532 | ||
533 | oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER); | |
534 | ||
535 | return oip; | |
718e3744 | 536 | } |
537 | ||
d62a17ae | 538 | void ospf_del_if_params(struct ospf_if_params *oip) |
718e3744 | 539 | { |
6a154c88 | 540 | list_delete(&oip->auth_crypt); |
d62a17ae | 541 | bfd_info_free(&(oip->bfd_info)); |
542 | XFREE(MTYPE_OSPF_IF_PARAMS, oip); | |
718e3744 | 543 | } |
544 | ||
d62a17ae | 545 | void ospf_free_if_params(struct interface *ifp, struct in_addr addr) |
718e3744 | 546 | { |
d62a17ae | 547 | struct ospf_if_params *oip; |
548 | struct prefix_ipv4 p; | |
549 | struct route_node *rn; | |
550 | ||
551 | p.family = AF_INET; | |
552 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
553 | p.prefix = addr; | |
554 | rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p); | |
555 | if (!rn || !rn->info) | |
556 | return; | |
557 | ||
558 | oip = rn->info; | |
559 | route_unlock_node(rn); | |
560 | ||
561 | if (!OSPF_IF_PARAM_CONFIGURED(oip, output_cost_cmd) | |
562 | && !OSPF_IF_PARAM_CONFIGURED(oip, transmit_delay) | |
563 | && !OSPF_IF_PARAM_CONFIGURED(oip, retransmit_interval) | |
564 | && !OSPF_IF_PARAM_CONFIGURED(oip, passive_interface) | |
565 | && !OSPF_IF_PARAM_CONFIGURED(oip, v_hello) | |
566 | && !OSPF_IF_PARAM_CONFIGURED(oip, fast_hello) | |
567 | && !OSPF_IF_PARAM_CONFIGURED(oip, v_wait) | |
568 | && !OSPF_IF_PARAM_CONFIGURED(oip, priority) | |
569 | && !OSPF_IF_PARAM_CONFIGURED(oip, type) | |
570 | && !OSPF_IF_PARAM_CONFIGURED(oip, auth_simple) | |
571 | && !OSPF_IF_PARAM_CONFIGURED(oip, auth_type) | |
572 | && listcount(oip->auth_crypt) == 0 | |
573 | && ntohl(oip->network_lsa_seqnum) != OSPF_INITIAL_SEQUENCE_NUMBER) { | |
574 | ospf_del_if_params(oip); | |
575 | rn->info = NULL; | |
576 | route_unlock_node(rn); | |
577 | } | |
718e3744 | 578 | } |
579 | ||
d62a17ae | 580 | struct ospf_if_params *ospf_lookup_if_params(struct interface *ifp, |
581 | struct in_addr addr) | |
718e3744 | 582 | { |
d62a17ae | 583 | struct prefix_ipv4 p; |
584 | struct route_node *rn; | |
585 | ||
586 | p.family = AF_INET; | |
587 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
588 | p.prefix = addr; | |
589 | ||
590 | rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p); | |
591 | ||
592 | if (rn) { | |
593 | route_unlock_node(rn); | |
594 | return rn->info; | |
595 | } | |
596 | ||
597 | return NULL; | |
718e3744 | 598 | } |
599 | ||
d62a17ae | 600 | struct ospf_if_params *ospf_get_if_params(struct interface *ifp, |
601 | struct in_addr addr) | |
718e3744 | 602 | { |
d62a17ae | 603 | struct prefix_ipv4 p; |
604 | struct route_node *rn; | |
605 | ||
606 | p.family = AF_INET; | |
607 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
608 | p.prefix = addr; | |
a6435618 | 609 | apply_mask_ipv4(&p); |
d62a17ae | 610 | |
611 | rn = route_node_get(IF_OIFS_PARAMS(ifp), (struct prefix *)&p); | |
612 | ||
613 | if (rn->info == NULL) | |
614 | rn->info = ospf_new_if_params(); | |
615 | else | |
616 | route_unlock_node(rn); | |
617 | ||
618 | return rn->info; | |
718e3744 | 619 | } |
620 | ||
d62a17ae | 621 | void ospf_if_update_params(struct interface *ifp, struct in_addr addr) |
718e3744 | 622 | { |
d62a17ae | 623 | struct route_node *rn; |
624 | struct ospf_interface *oi; | |
625 | ||
626 | for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) { | |
627 | if ((oi = rn->info) == NULL) | |
628 | continue; | |
629 | ||
630 | if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &addr)) | |
631 | oi->params = ospf_lookup_if_params( | |
632 | ifp, oi->address->u.prefix4); | |
633 | } | |
718e3744 | 634 | } |
635 | ||
d62a17ae | 636 | int ospf_if_new_hook(struct interface *ifp) |
718e3744 | 637 | { |
d62a17ae | 638 | int rc = 0; |
639 | ||
640 | ifp->info = XCALLOC(MTYPE_OSPF_IF_INFO, sizeof(struct ospf_if_info)); | |
641 | ||
642 | IF_OIFS(ifp) = route_table_init(); | |
643 | IF_OIFS_PARAMS(ifp) = route_table_init(); | |
644 | ||
645 | IF_DEF_PARAMS(ifp) = ospf_new_if_params(); | |
646 | ||
647 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), transmit_delay); | |
648 | IF_DEF_PARAMS(ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT; | |
649 | ||
650 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), retransmit_interval); | |
651 | IF_DEF_PARAMS(ifp)->retransmit_interval = | |
652 | OSPF_RETRANSMIT_INTERVAL_DEFAULT; | |
653 | ||
654 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), priority); | |
655 | IF_DEF_PARAMS(ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT; | |
656 | ||
657 | IF_DEF_PARAMS(ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT; | |
658 | ||
659 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_hello); | |
660 | IF_DEF_PARAMS(ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT; | |
661 | ||
662 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), fast_hello); | |
663 | IF_DEF_PARAMS(ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT; | |
664 | ||
665 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_wait); | |
666 | IF_DEF_PARAMS(ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT; | |
667 | ||
668 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_simple); | |
669 | memset(IF_DEF_PARAMS(ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE); | |
670 | ||
671 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_type); | |
672 | IF_DEF_PARAMS(ifp)->auth_type = OSPF_AUTH_NOTSET; | |
673 | ||
674 | rc = ospf_opaque_new_if(ifp); | |
675 | return rc; | |
718e3744 | 676 | } |
677 | ||
d62a17ae | 678 | static int ospf_if_delete_hook(struct interface *ifp) |
718e3744 | 679 | { |
d62a17ae | 680 | int rc = 0; |
681 | struct route_node *rn; | |
682 | rc = ospf_opaque_del_if(ifp); | |
940b01aa | 683 | |
d62a17ae | 684 | route_table_finish(IF_OIFS(ifp)); |
940b01aa | 685 | |
d62a17ae | 686 | for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn)) |
687 | if (rn->info) | |
688 | ospf_del_if_params(rn->info); | |
689 | route_table_finish(IF_OIFS_PARAMS(ifp)); | |
940b01aa | 690 | |
d62a17ae | 691 | ospf_del_if_params((struct ospf_if_params *)IF_DEF_PARAMS(ifp)); |
692 | XFREE(MTYPE_OSPF_IF_INFO, ifp->info); | |
693 | ifp->info = NULL; | |
718e3744 | 694 | |
d62a17ae | 695 | return rc; |
718e3744 | 696 | } |
697 | ||
d62a17ae | 698 | int ospf_if_is_enable(struct ospf_interface *oi) |
718e3744 | 699 | { |
0c74bbe0 | 700 | if (!(if_is_loopback(oi->ifp) || if_is_vrf(oi->ifp))) |
d62a17ae | 701 | if (if_is_up(oi->ifp)) |
702 | return 1; | |
718e3744 | 703 | |
d62a17ae | 704 | return 0; |
718e3744 | 705 | } |
706 | ||
d62a17ae | 707 | void ospf_if_set_multicast(struct ospf_interface *oi) |
ba6454ec | 708 | { |
d62a17ae | 709 | if ((oi->state > ISM_Loopback) && (oi->type != OSPF_IFTYPE_LOOPBACK) |
710 | && (oi->type != OSPF_IFTYPE_VIRTUALLINK) | |
711 | && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) { | |
712 | /* The interface should belong to the OSPF-all-routers group. */ | |
713 | if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS) | |
714 | && (ospf_if_add_allspfrouters(oi->ospf, oi->address, | |
715 | oi->ifp->ifindex) | |
716 | >= 0)) | |
717 | /* Set the flag only if the system call to join | |
718 | * succeeded. */ | |
719 | OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS); | |
720 | } else { | |
721 | /* The interface should NOT belong to the OSPF-all-routers | |
722 | * group. */ | |
723 | if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)) { | |
724 | /* Only actually drop if this is the last reference */ | |
725 | if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1) | |
726 | ospf_if_drop_allspfrouters(oi->ospf, | |
727 | oi->address, | |
728 | oi->ifp->ifindex); | |
729 | /* Unset the flag regardless of whether the system call | |
730 | to leave | |
731 | the group succeeded, since it's much safer to assume | |
732 | that | |
733 | we are not a member. */ | |
734 | OI_MEMBER_LEFT(oi, MEMBER_ALLROUTERS); | |
735 | } | |
736 | } | |
737 | ||
738 | if (((oi->type == OSPF_IFTYPE_BROADCAST) | |
739 | || (oi->type == OSPF_IFTYPE_POINTOPOINT)) | |
740 | && ((oi->state == ISM_DR) || (oi->state == ISM_Backup)) | |
741 | && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) { | |
742 | /* The interface should belong to the OSPF-designated-routers | |
743 | * group. */ | |
744 | if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS) | |
745 | && (ospf_if_add_alldrouters(oi->ospf, oi->address, | |
746 | oi->ifp->ifindex) | |
747 | >= 0)) | |
748 | /* Set the flag only if the system call to join | |
749 | * succeeded. */ | |
750 | OI_MEMBER_JOINED(oi, MEMBER_DROUTERS); | |
751 | } else { | |
752 | /* The interface should NOT belong to the | |
753 | * OSPF-designated-routers group */ | |
754 | if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)) { | |
755 | /* drop only if last reference */ | |
756 | if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1) | |
757 | ospf_if_drop_alldrouters(oi->ospf, oi->address, | |
758 | oi->ifp->ifindex); | |
759 | ||
760 | /* Unset the flag regardless of whether the system call | |
761 | to leave | |
762 | the group succeeded, since it's much safer to assume | |
763 | that | |
764 | we are not a member. */ | |
765 | OI_MEMBER_LEFT(oi, MEMBER_DROUTERS); | |
766 | } | |
767 | } | |
ba6454ec | 768 | } |
769 | ||
d62a17ae | 770 | int ospf_if_up(struct ospf_interface *oi) |
718e3744 | 771 | { |
d62a17ae | 772 | if (oi == NULL) |
773 | return 0; | |
774 | ||
775 | if (oi->type == OSPF_IFTYPE_LOOPBACK) | |
776 | OSPF_ISM_EVENT_SCHEDULE(oi, ISM_LoopInd); | |
777 | else { | |
d62a17ae | 778 | OSPF_ISM_EVENT_SCHEDULE(oi, ISM_InterfaceUp); |
779 | } | |
780 | ||
781 | return 1; | |
718e3744 | 782 | } |
783 | ||
d62a17ae | 784 | int ospf_if_down(struct ospf_interface *oi) |
718e3744 | 785 | { |
d62a17ae | 786 | if (oi == NULL) |
787 | return 0; | |
718e3744 | 788 | |
d62a17ae | 789 | OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown); |
790 | /* delete position in router LSA */ | |
791 | oi->lsa_pos_beg = 0; | |
792 | oi->lsa_pos_end = 0; | |
793 | /* Shutdown packet reception and sending */ | |
794 | ospf_if_stream_unset(oi); | |
718e3744 | 795 | |
d62a17ae | 796 | return 1; |
718e3744 | 797 | } |
798 | ||
6b0655a2 | 799 | |
718e3744 | 800 | /* Virtual Link related functions. */ |
801 | ||
d62a17ae | 802 | struct ospf_vl_data *ospf_vl_data_new(struct ospf_area *area, |
803 | struct in_addr vl_peer) | |
718e3744 | 804 | { |
d62a17ae | 805 | struct ospf_vl_data *vl_data; |
718e3744 | 806 | |
d62a17ae | 807 | vl_data = XCALLOC(MTYPE_OSPF_VL_DATA, sizeof(struct ospf_vl_data)); |
718e3744 | 808 | |
d62a17ae | 809 | vl_data->vl_peer.s_addr = vl_peer.s_addr; |
810 | vl_data->vl_area_id = area->area_id; | |
811 | vl_data->vl_area_id_fmt = area->area_id_fmt; | |
718e3744 | 812 | |
d62a17ae | 813 | return vl_data; |
718e3744 | 814 | } |
815 | ||
d62a17ae | 816 | void ospf_vl_data_free(struct ospf_vl_data *vl_data) |
718e3744 | 817 | { |
d62a17ae | 818 | XFREE(MTYPE_OSPF_VL_DATA, vl_data); |
718e3744 | 819 | } |
820 | ||
d7c0a89a | 821 | unsigned int vlink_count = 0; |
718e3744 | 822 | |
d62a17ae | 823 | struct ospf_interface *ospf_vl_new(struct ospf *ospf, |
824 | struct ospf_vl_data *vl_data) | |
718e3744 | 825 | { |
d62a17ae | 826 | struct ospf_interface *voi; |
827 | struct interface *vi; | |
bcc24579 | 828 | char ifname[INTERFACE_NAMSIZ]; |
d62a17ae | 829 | struct ospf_area *area; |
830 | struct in_addr area_id; | |
831 | struct connected *co; | |
832 | struct prefix_ipv4 *p; | |
833 | ||
834 | if (IS_DEBUG_OSPF_EVENT) | |
835 | zlog_debug("ospf_vl_new(): Start"); | |
836 | if (vlink_count == OSPF_VL_MAX_COUNT) { | |
837 | if (IS_DEBUG_OSPF_EVENT) | |
838 | zlog_debug( | |
839 | "ospf_vl_new(): Alarm: " | |
840 | "cannot create more than OSPF_MAX_VL_COUNT virtual links"); | |
841 | return NULL; | |
842 | } | |
843 | ||
844 | if (IS_DEBUG_OSPF_EVENT) | |
996c9314 LB |
845 | zlog_debug( |
846 | "ospf_vl_new(): creating pseudo zebra interface vrf id %u", | |
847 | ospf->vrf_id); | |
d62a17ae | 848 | |
2ec42b85 | 849 | snprintf(ifname, sizeof(ifname), "VLINK%u", vlink_count); |
d5c65bf1 | 850 | vi = if_create_name(ifname, ospf->vrf_id); |
d62a17ae | 851 | /* |
d5c65bf1 | 852 | * if_create_name sets ZEBRA_INTERFACE_LINKDETECTION |
d62a17ae | 853 | * virtual links don't need this. |
854 | */ | |
855 | UNSET_FLAG(vi->status, ZEBRA_INTERFACE_LINKDETECTION); | |
856 | co = connected_new(); | |
857 | co->ifp = vi; | |
858 | listnode_add(vi->connected, co); | |
859 | ||
860 | p = prefix_ipv4_new(); | |
861 | p->family = AF_INET; | |
862 | p->prefix.s_addr = 0; | |
863 | p->prefixlen = 0; | |
864 | ||
865 | co->address = (struct prefix *)p; | |
866 | ||
867 | voi = ospf_if_new(ospf, vi, co->address); | |
868 | if (voi == NULL) { | |
869 | if (IS_DEBUG_OSPF_EVENT) | |
870 | zlog_debug( | |
871 | "ospf_vl_new(): Alarm: OSPF int structure is not created"); | |
872 | return NULL; | |
873 | } | |
874 | voi->connected = co; | |
875 | voi->vl_data = vl_data; | |
876 | voi->ifp->mtu = OSPF_VL_MTU; | |
877 | voi->type = OSPF_IFTYPE_VIRTUALLINK; | |
878 | ||
879 | vlink_count++; | |
880 | if (IS_DEBUG_OSPF_EVENT) | |
881 | zlog_debug("ospf_vl_new(): Created name: %s", ifname); | |
882 | if (IS_DEBUG_OSPF_EVENT) | |
883 | zlog_debug("ospf_vl_new(): set if->name to %s", vi->name); | |
884 | ||
885 | area_id.s_addr = 0; | |
886 | area = ospf_area_get(ospf, area_id); | |
887 | voi->area = area; | |
888 | ||
889 | if (IS_DEBUG_OSPF_EVENT) | |
890 | zlog_debug( | |
891 | "ospf_vl_new(): set associated area to the backbone"); | |
892 | ||
893 | /* Add pseudo neighbor. */ | |
894 | ospf_nbr_self_reset(voi, voi->ospf->router_id); | |
895 | ||
896 | ospf_area_add_if(voi->area, voi); | |
897 | ||
d62a17ae | 898 | if (IS_DEBUG_OSPF_EVENT) |
899 | zlog_debug("ospf_vl_new(): Stop"); | |
900 | return voi; | |
718e3744 | 901 | } |
902 | ||
d62a17ae | 903 | static void ospf_vl_if_delete(struct ospf_vl_data *vl_data) |
718e3744 | 904 | { |
d62a17ae | 905 | struct interface *ifp = vl_data->vl_oi->ifp; |
906 | vl_data->vl_oi->address->u.prefix4.s_addr = 0; | |
907 | vl_data->vl_oi->address->prefixlen = 0; | |
908 | ospf_if_free(vl_data->vl_oi); | |
909 | if_delete(ifp); | |
910 | vlink_count--; | |
718e3744 | 911 | } |
912 | ||
19ce7d5e PG |
913 | /* for a defined area, count the number of configured vl |
914 | */ | |
915 | int ospf_vl_count(struct ospf *ospf, struct ospf_area *area) | |
916 | { | |
917 | int count = 0; | |
918 | struct ospf_vl_data *vl_data; | |
919 | struct listnode *node; | |
920 | ||
921 | for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) { | |
922 | if (area | |
923 | && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) | |
924 | continue; | |
925 | count++; | |
926 | } | |
927 | return count; | |
928 | } | |
929 | ||
9c27ef9b PJ |
930 | /* Look up vl_data for given peer, optionally qualified to be in the |
931 | * specified area. NULL area returns first found.. | |
932 | */ | |
d62a17ae | 933 | struct ospf_vl_data *ospf_vl_lookup(struct ospf *ospf, struct ospf_area *area, |
934 | struct in_addr vl_peer) | |
718e3744 | 935 | { |
d62a17ae | 936 | struct ospf_vl_data *vl_data; |
937 | struct listnode *node; | |
938 | ||
939 | if (IS_DEBUG_OSPF_EVENT) { | |
940 | zlog_debug("%s: Looking for %s", __func__, inet_ntoa(vl_peer)); | |
941 | if (area) | |
942 | zlog_debug("%s: in area %s", __func__, | |
943 | inet_ntoa(area->area_id)); | |
944 | } | |
945 | ||
946 | for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) { | |
947 | if (IS_DEBUG_OSPF_EVENT) | |
948 | zlog_debug("%s: VL %s, peer %s", __func__, | |
949 | vl_data->vl_oi->ifp->name, | |
950 | inet_ntoa(vl_data->vl_peer)); | |
951 | ||
952 | if (area | |
953 | && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) | |
954 | continue; | |
955 | ||
956 | if (IPV4_ADDR_SAME(&vl_data->vl_peer, &vl_peer)) | |
957 | return vl_data; | |
958 | } | |
959 | ||
960 | return NULL; | |
718e3744 | 961 | } |
962 | ||
d62a17ae | 963 | static void ospf_vl_shutdown(struct ospf_vl_data *vl_data) |
718e3744 | 964 | { |
d62a17ae | 965 | struct ospf_interface *oi; |
718e3744 | 966 | |
d62a17ae | 967 | if ((oi = vl_data->vl_oi) == NULL) |
968 | return; | |
718e3744 | 969 | |
d62a17ae | 970 | oi->address->u.prefix4.s_addr = 0; |
971 | oi->address->prefixlen = 0; | |
718e3744 | 972 | |
d62a17ae | 973 | UNSET_FLAG(oi->ifp->flags, IFF_UP); |
974 | /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */ | |
975 | OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown); | |
718e3744 | 976 | } |
977 | ||
d62a17ae | 978 | void ospf_vl_add(struct ospf *ospf, struct ospf_vl_data *vl_data) |
718e3744 | 979 | { |
d62a17ae | 980 | listnode_add(ospf->vlinks, vl_data); |
981 | hook_call(ospf_vl_add, vl_data); | |
718e3744 | 982 | } |
983 | ||
d62a17ae | 984 | void ospf_vl_delete(struct ospf *ospf, struct ospf_vl_data *vl_data) |
718e3744 | 985 | { |
d62a17ae | 986 | ospf_vl_shutdown(vl_data); |
987 | ospf_vl_if_delete(vl_data); | |
718e3744 | 988 | |
d62a17ae | 989 | hook_call(ospf_vl_delete, vl_data); |
990 | listnode_delete(ospf->vlinks, vl_data); | |
718e3744 | 991 | |
d62a17ae | 992 | ospf_vl_data_free(vl_data); |
718e3744 | 993 | } |
994 | ||
d62a17ae | 995 | static int ospf_vl_set_params(struct ospf_vl_data *vl_data, struct vertex *v) |
718e3744 | 996 | { |
d62a17ae | 997 | int changed = 0; |
998 | struct ospf_interface *voi; | |
999 | struct listnode *node; | |
1000 | struct vertex_parent *vp = NULL; | |
1001 | unsigned int i; | |
1002 | struct router_lsa *rl; | |
1003 | ||
1004 | voi = vl_data->vl_oi; | |
1005 | ||
1006 | if (voi->output_cost != v->distance) { | |
1007 | ||
1008 | voi->output_cost = v->distance; | |
1009 | changed = 1; | |
1010 | } | |
1011 | ||
1012 | for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) { | |
1013 | vl_data->nexthop.oi = vp->nexthop->oi; | |
1014 | vl_data->nexthop.router = vp->nexthop->router; | |
1015 | ||
1016 | if (!IPV4_ADDR_SAME(&voi->address->u.prefix4, | |
1017 | &vl_data->nexthop.oi->address->u.prefix4)) | |
1018 | changed = 1; | |
1019 | ||
1020 | voi->address->u.prefix4 = | |
1021 | vl_data->nexthop.oi->address->u.prefix4; | |
1022 | voi->address->prefixlen = | |
1023 | vl_data->nexthop.oi->address->prefixlen; | |
1024 | ||
1025 | break; /* We take the first interface. */ | |
1026 | } | |
1027 | ||
1028 | rl = (struct router_lsa *)v->lsa; | |
1029 | ||
1030 | /* use SPF determined backlink index in struct vertex | |
1031 | * for virtual link destination address | |
1032 | */ | |
1033 | if (vp && vp->backlink >= 0) { | |
1034 | if (!IPV4_ADDR_SAME(&vl_data->peer_addr, | |
1035 | &rl->link[vp->backlink].link_data)) | |
1036 | changed = 1; | |
1037 | vl_data->peer_addr = rl->link[vp->backlink].link_data; | |
1038 | } else { | |
1039 | /* This is highly odd, there is no backlink index | |
1040 | * there should be due to the ospf_spf_has_link() check | |
1041 | * in SPF. Lets warn and try pick a link anyway. | |
1042 | */ | |
266469eb | 1043 | zlog_info("ospf_vl_set_params: No backlink for %s!", |
d62a17ae | 1044 | vl_data->vl_oi->ifp->name); |
1045 | for (i = 0; i < ntohs(rl->links); i++) { | |
1046 | switch (rl->link[i].type) { | |
1047 | case LSA_LINK_TYPE_VIRTUALLINK: | |
1048 | if (IS_DEBUG_OSPF_EVENT) | |
1049 | zlog_debug( | |
1050 | "found back link through VL"); | |
1051 | /* fallthru */ | |
1052 | case LSA_LINK_TYPE_TRANSIT: | |
1053 | case LSA_LINK_TYPE_POINTOPOINT: | |
1054 | if (!IPV4_ADDR_SAME(&vl_data->peer_addr, | |
1055 | &rl->link[i].link_data)) | |
1056 | changed = 1; | |
1057 | vl_data->peer_addr = rl->link[i].link_data; | |
1058 | } | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | if (IS_DEBUG_OSPF_EVENT) | |
1063 | zlog_debug("%s: %s peer address: %s, cost: %d,%schanged", | |
1064 | __func__, vl_data->vl_oi->ifp->name, | |
1065 | inet_ntoa(vl_data->peer_addr), voi->output_cost, | |
1066 | (changed ? " " : " un")); | |
1067 | ||
1068 | return changed; | |
718e3744 | 1069 | } |
1070 | ||
1071 | ||
d62a17ae | 1072 | void ospf_vl_up_check(struct ospf_area *area, struct in_addr rid, |
1073 | struct vertex *v) | |
718e3744 | 1074 | { |
d62a17ae | 1075 | struct ospf *ospf = area->ospf; |
1076 | struct listnode *node; | |
1077 | struct ospf_vl_data *vl_data; | |
1078 | struct ospf_interface *oi; | |
1079 | ||
1080 | if (IS_DEBUG_OSPF_EVENT) { | |
1081 | zlog_debug("ospf_vl_up_check(): Start"); | |
1082 | zlog_debug("ospf_vl_up_check(): Router ID is %s", | |
1083 | inet_ntoa(rid)); | |
1084 | zlog_debug("ospf_vl_up_check(): Area is %s", | |
1085 | inet_ntoa(area->area_id)); | |
718e3744 | 1086 | } |
1087 | ||
d62a17ae | 1088 | for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) { |
1089 | if (IS_DEBUG_OSPF_EVENT) { | |
1090 | zlog_debug("%s: considering VL, %s in area %s", | |
1091 | __func__, vl_data->vl_oi->ifp->name, | |
1092 | inet_ntoa(vl_data->vl_area_id)); | |
1093 | zlog_debug("%s: peer ID: %s", __func__, | |
1094 | inet_ntoa(vl_data->vl_peer)); | |
1095 | } | |
1096 | ||
1097 | if (IPV4_ADDR_SAME(&vl_data->vl_peer, &rid) | |
1098 | && IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) { | |
1099 | oi = vl_data->vl_oi; | |
1100 | SET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED); | |
1101 | ||
1102 | if (IS_DEBUG_OSPF_EVENT) | |
1103 | zlog_debug( | |
1104 | "ospf_vl_up_check(): this VL matched"); | |
1105 | ||
1106 | if (oi->state == ISM_Down) { | |
1107 | if (IS_DEBUG_OSPF_EVENT) | |
1108 | zlog_debug( | |
1109 | "ospf_vl_up_check(): VL is down, waking it up"); | |
1110 | SET_FLAG(oi->ifp->flags, IFF_UP); | |
1111 | OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceUp); | |
1112 | } | |
1113 | ||
1114 | if (ospf_vl_set_params(vl_data, v)) { | |
1115 | if (IS_DEBUG_OSPF(ism, ISM_EVENTS)) | |
1116 | zlog_debug( | |
1117 | "ospf_vl_up_check: VL cost change," | |
1118 | " scheduling router lsa refresh"); | |
1119 | if (ospf->backbone) | |
1120 | ospf_router_lsa_update_area( | |
1121 | ospf->backbone); | |
1122 | else if (IS_DEBUG_OSPF(ism, ISM_EVENTS)) | |
1123 | zlog_debug( | |
1124 | "ospf_vl_up_check: VL cost change, no backbone!"); | |
1125 | } | |
1126 | } | |
1127 | } | |
718e3744 | 1128 | } |
1129 | ||
d62a17ae | 1130 | void ospf_vl_unapprove(struct ospf *ospf) |
718e3744 | 1131 | { |
d62a17ae | 1132 | struct listnode *node; |
1133 | struct ospf_vl_data *vl_data; | |
718e3744 | 1134 | |
d62a17ae | 1135 | for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) |
1136 | UNSET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED); | |
718e3744 | 1137 | } |
1138 | ||
d62a17ae | 1139 | void ospf_vl_shut_unapproved(struct ospf *ospf) |
718e3744 | 1140 | { |
d62a17ae | 1141 | struct listnode *node, *nnode; |
1142 | struct ospf_vl_data *vl_data; | |
718e3744 | 1143 | |
d62a17ae | 1144 | for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data)) |
1145 | if (!CHECK_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED)) | |
1146 | ospf_vl_shutdown(vl_data); | |
718e3744 | 1147 | } |
1148 | ||
d62a17ae | 1149 | int ospf_full_virtual_nbrs(struct ospf_area *area) |
718e3744 | 1150 | { |
d62a17ae | 1151 | if (IS_DEBUG_OSPF_EVENT) { |
1152 | zlog_debug( | |
1153 | "counting fully adjacent virtual neighbors in area %s", | |
1154 | inet_ntoa(area->area_id)); | |
1155 | zlog_debug("there are %d of them", area->full_vls); | |
1156 | } | |
1157 | ||
1158 | return area->full_vls; | |
718e3744 | 1159 | } |
1160 | ||
d62a17ae | 1161 | int ospf_vls_in_area(struct ospf_area *area) |
718e3744 | 1162 | { |
d62a17ae | 1163 | struct listnode *node; |
1164 | struct ospf_vl_data *vl_data; | |
1165 | int c = 0; | |
718e3744 | 1166 | |
d62a17ae | 1167 | for (ALL_LIST_ELEMENTS_RO(area->ospf->vlinks, node, vl_data)) |
1168 | if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) | |
1169 | c++; | |
718e3744 | 1170 | |
d62a17ae | 1171 | return c; |
718e3744 | 1172 | } |
1173 | ||
6b0655a2 | 1174 | |
4d762f26 | 1175 | struct crypt_key *ospf_crypt_key_new(void) |
718e3744 | 1176 | { |
d62a17ae | 1177 | return XCALLOC(MTYPE_OSPF_CRYPT_KEY, sizeof(struct crypt_key)); |
718e3744 | 1178 | } |
1179 | ||
d62a17ae | 1180 | void ospf_crypt_key_add(struct list *crypt, struct crypt_key *ck) |
718e3744 | 1181 | { |
d62a17ae | 1182 | listnode_add(crypt, ck); |
718e3744 | 1183 | } |
1184 | ||
d7c0a89a | 1185 | struct crypt_key *ospf_crypt_key_lookup(struct list *auth_crypt, uint8_t key_id) |
718e3744 | 1186 | { |
d62a17ae | 1187 | struct listnode *node; |
1188 | struct crypt_key *ck; | |
718e3744 | 1189 | |
d62a17ae | 1190 | for (ALL_LIST_ELEMENTS_RO(auth_crypt, node, ck)) |
1191 | if (ck->key_id == key_id) | |
1192 | return ck; | |
718e3744 | 1193 | |
d62a17ae | 1194 | return NULL; |
718e3744 | 1195 | } |
1196 | ||
d7c0a89a | 1197 | int ospf_crypt_key_delete(struct list *auth_crypt, uint8_t key_id) |
718e3744 | 1198 | { |
d62a17ae | 1199 | struct listnode *node, *nnode; |
1200 | struct crypt_key *ck; | |
1201 | ||
1202 | for (ALL_LIST_ELEMENTS(auth_crypt, node, nnode, ck)) { | |
1203 | if (ck->key_id == key_id) { | |
1204 | listnode_delete(auth_crypt, ck); | |
1205 | XFREE(MTYPE_OSPF_CRYPT_KEY, ck); | |
1206 | return 1; | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | return 0; | |
718e3744 | 1211 | } |
1212 | ||
d7c0a89a | 1213 | uint8_t ospf_default_iftype(struct interface *ifp) |
bc18d616 | 1214 | { |
d62a17ae | 1215 | if (if_is_pointopoint(ifp)) |
1216 | return OSPF_IFTYPE_POINTOPOINT; | |
0c74bbe0 | 1217 | else if (if_is_loopback(ifp) || if_is_vrf(ifp)) |
d62a17ae | 1218 | return OSPF_IFTYPE_LOOPBACK; |
1219 | else | |
1220 | return OSPF_IFTYPE_BROADCAST; | |
bc18d616 | 1221 | } |
1222 | ||
ef7bd2a3 DS |
1223 | void ospf_if_interface(struct interface *ifp) |
1224 | { | |
1225 | hook_call(ospf_if_update, ifp); | |
1226 | } | |
1227 | ||
138c5a74 DS |
1228 | static int ospf_ifp_create(struct interface *ifp) |
1229 | { | |
ef7bd2a3 DS |
1230 | struct ospf *ospf = NULL; |
1231 | ||
1232 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) | |
1233 | zlog_debug( | |
1234 | "Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u", | |
1235 | ifp->name, ospf_vrf_id_to_name(ifp->vrf_id), | |
1236 | ifp->vrf_id, ifp->ifindex, | |
1237 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu, | |
1238 | ifp->speed); | |
1239 | ||
1240 | assert(ifp->info); | |
1241 | ||
1242 | if (IF_DEF_PARAMS(ifp) | |
1243 | && !OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp), type)) { | |
1244 | SET_IF_PARAM(IF_DEF_PARAMS(ifp), type); | |
1245 | IF_DEF_PARAMS(ifp)->type = ospf_default_iftype(ifp); | |
1246 | } | |
1247 | ||
1248 | ospf = ospf_lookup_by_vrf_id(ifp->vrf_id); | |
1249 | if (!ospf) | |
1250 | return 0; | |
1251 | ||
1252 | ospf_if_recalculate_output_cost(ifp); | |
1253 | ||
1254 | ospf_if_update(ospf, ifp); | |
1255 | ||
1256 | hook_call(ospf_if_update, ifp); | |
1257 | ||
138c5a74 DS |
1258 | return 0; |
1259 | } | |
1260 | ||
1261 | static int ospf_ifp_up(struct interface *ifp) | |
1262 | { | |
ddbf3e60 DS |
1263 | struct ospf_interface *oi; |
1264 | struct route_node *rn; | |
1265 | ||
1266 | /* Interface is already up. */ | |
1267 | if (if_is_operative(ifp)) { | |
1268 | /* Temporarily keep ifp values. */ | |
1269 | struct interface if_tmp; | |
1270 | memcpy(&if_tmp, ifp, sizeof(struct interface)); | |
1271 | ||
1272 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) | |
1273 | zlog_debug( | |
1274 | "Zebra: Interface[%s] state update speed %u -> %u, bw %d -> %d", | |
1275 | ifp->name, if_tmp.speed, ifp->speed, | |
1276 | if_tmp.bandwidth, ifp->bandwidth); | |
1277 | ||
1278 | ospf_if_recalculate_output_cost(ifp); | |
1279 | ||
1280 | if (if_tmp.mtu != ifp->mtu) { | |
1281 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) | |
1282 | zlog_debug( | |
1283 | "Zebra: Interface[%s] MTU change %u -> %u.", | |
1284 | ifp->name, if_tmp.mtu, ifp->mtu); | |
1285 | ||
1286 | /* Must reset the interface (simulate down/up) when MTU | |
1287 | * changes. */ | |
1288 | ospf_if_reset(ifp); | |
1289 | } | |
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) | |
1294 | zlog_debug("Zebra: Interface[%s] state change to up.", | |
1295 | ifp->name); | |
1296 | ||
1297 | for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) { | |
1298 | if ((oi = rn->info) == NULL) | |
1299 | continue; | |
1300 | ||
1301 | ospf_if_up(oi); | |
1302 | } | |
1303 | ||
138c5a74 DS |
1304 | return 0; |
1305 | } | |
1306 | ||
1307 | static int ospf_ifp_down(struct interface *ifp) | |
1308 | { | |
b0b69e59 DS |
1309 | struct ospf_interface *oi; |
1310 | struct route_node *node; | |
1311 | ||
1312 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) | |
1313 | zlog_debug("Zebra: Interface[%s] state change to down.", | |
1314 | ifp->name); | |
1315 | ||
1316 | for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) { | |
1317 | if ((oi = node->info) == NULL) | |
1318 | continue; | |
1319 | ospf_if_down(oi); | |
1320 | } | |
1321 | ||
138c5a74 DS |
1322 | return 0; |
1323 | } | |
1324 | ||
1325 | static int ospf_ifp_destroy(struct interface *ifp) | |
1326 | { | |
3c3c3252 DS |
1327 | struct route_node *rn; |
1328 | ||
1329 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) | |
1330 | zlog_debug( | |
1331 | "Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d", | |
1332 | ifp->name, ospf_vrf_id_to_name(ifp->vrf_id), | |
1333 | ifp->vrf_id, ifp->ifindex, | |
1334 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu); | |
1335 | ||
1336 | hook_call(ospf_if_delete, ifp); | |
1337 | ||
1338 | for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) | |
1339 | if (rn->info) | |
1340 | ospf_if_free((struct ospf_interface *)rn->info); | |
1341 | ||
138c5a74 DS |
1342 | return 0; |
1343 | } | |
1344 | ||
4d762f26 | 1345 | void ospf_if_init(void) |
718e3744 | 1346 | { |
138c5a74 DS |
1347 | if_zapi_callbacks(ospf_ifp_create, ospf_ifp_up, |
1348 | ospf_ifp_down, ospf_ifp_destroy); | |
1349 | ||
d62a17ae | 1350 | /* Initialize Zebra interface data structure. */ |
ce19a04a DL |
1351 | hook_register_prio(if_add, 0, ospf_if_new_hook); |
1352 | hook_register_prio(if_del, 0, ospf_if_delete_hook); | |
718e3744 | 1353 | } |