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