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