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 DEFINE_HOOK(ospf_if_delete, (struct interface * ifp), (ifp))
55 
ospf_interface_neighbor_count(struct ospf_interface * oi)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 
ospf_if_get_output_cost(struct ospf_interface * oi)78 int ospf_if_get_output_cost(struct ospf_interface *oi)
79 {
80 	/* If all else fails, use default OSPF cost */
81 	uint32_t cost;
82 	uint32_t bw, refbw;
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 {
101 		cost = (uint32_t)((double)refbw / (double)bw + (double)0.5);
102 		if (cost < 1)
103 			cost = 1;
104 		else if (cost > 65535)
105 			cost = 65535;
106 	}
107 
108 	return cost;
109 }
110 
ospf_if_recalculate_output_cost(struct interface * ifp)111 void ospf_if_recalculate_output_cost(struct interface *ifp)
112 {
113 	uint32_t newcost;
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 		}
129 	}
130 }
131 
132 /* Simulate down/up on the interface.  This is needed, for example, when
133    the MTU changes. */
ospf_if_reset(struct interface * ifp)134 void ospf_if_reset(struct interface *ifp)
135 {
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 	}
147 }
148 
ospf_if_reset_variables(struct ospf_interface * oi)149 void ospf_if_reset_variables(struct ospf_interface *oi)
150 {
151 	/* Set default values. */
152 	/* don't clear this flag.  oi->flag = OSPF_IF_DISABLE; */
153 
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;
160 
161 	oi->state = ISM_Down;
162 
163 	oi->crypt_seqnum = 0;
164 
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;
169 }
170 
171 /* lookup oi for specified prefix/ifp */
ospf_if_table_lookup(struct interface * ifp,struct prefix * prefix)172 struct ospf_interface *ospf_if_table_lookup(struct interface *ifp,
173 					    struct prefix *prefix)
174 {
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;
189 }
190 
ospf_add_to_if(struct interface * ifp,struct ospf_interface * oi)191 static void ospf_add_to_if(struct interface *ifp, struct ospf_interface *oi)
192 {
193 	struct route_node *rn;
194 	struct prefix p;
195 
196 	p = *oi->address;
197 	p.prefixlen = IPV4_MAX_PREFIXLEN;
198 	apply_mask(&p);
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;
206 }
207 
ospf_delete_from_if(struct interface * ifp,struct ospf_interface * oi)208 static void ospf_delete_from_if(struct interface *ifp,
209 				struct ospf_interface *oi)
210 {
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);
223 }
224 
ospf_if_new(struct ospf * ospf,struct interface * ifp,struct prefix * p)225 struct ospf_interface *ospf_if_new(struct ospf *ospf, struct interface *ifp,
226 				   struct prefix *p)
227 {
228 	struct ospf_interface *oi;
229 
230 	oi = ospf_if_table_lookup(ifp, p);
231 	if (oi)
232 		return oi;
233 
234 	oi = XCALLOC(MTYPE_OSPF_IF, sizeof(struct ospf_interface));
235 
236 	oi->obuf = ospf_fifo_new();
237 
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;
270 
271 	QOBJ_REG(oi, ospf_interface);
272 
273 	if (IS_DEBUG_OSPF_EVENT)
274 		zlog_debug("%s: ospf interface %s vrf %s id %u created",
275 			   __func__, ifp->name, ospf_get_name(ospf),
276 			   ospf->vrf_id);
277 
278 	return oi;
279 }
280 
281 /* Restore an interface to its pre UP state
282    Used from ism_interface_down only */
ospf_if_cleanup(struct ospf_interface * oi)283 void ospf_if_cleanup(struct ospf_interface *oi)
284 {
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);
304 	}
305 
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);
324 }
325 
ospf_if_free(struct ospf_interface * oi)326 void ospf_if_free(struct ospf_interface *oi)
327 {
328 	ospf_if_down(oi);
329 
330 	ospf_fifo_free(oi->obuf);
331 
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);
340 
341 	route_table_finish(oi->nbrs);
342 	route_table_finish(oi->ls_upd_queue);
343 
344 	/* Free any lists that should be freed */
345 	list_delete(&oi->nbr_nbma);
346 
347 	list_delete(&oi->ls_ack);
348 	list_delete(&oi->ls_ack_direct.ls_ack);
349 
350 	if (IS_DEBUG_OSPF_EVENT)
351 		zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
352 			   __func__, oi->ifp->name,
353 			   ospf_vrf_id_to_name(oi->ifp->vrf_id),
354 			   oi->ifp->vrf_id);
355 
356 	ospf_delete_from_if(oi->ifp, oi);
357 
358 	listnode_delete(oi->ospf->oiflist, oi);
359 	listnode_delete(oi->area->oiflist, oi);
360 
361 	thread_cancel_event(master, oi);
362 
363 	memset(oi, 0, sizeof(*oi));
364 	XFREE(MTYPE_OSPF_IF, oi);
365 }
366 
ospf_if_is_up(struct ospf_interface * oi)367 int ospf_if_is_up(struct ospf_interface *oi)
368 {
369 	return if_is_up(oi->ifp);
370 }
371 
ospf_if_exists(struct ospf_interface * oic)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;
377 
378 	if (!oic)
379 		return NULL;
380 
381 	ospf = oic->ospf;
382 	if (ospf == NULL)
383 		return NULL;
384 
385 	for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
386 		if (oi == oic)
387 			return oi;
388 
389 	return NULL;
390 }
391 
392 /* Lookup OSPF interface by router LSA posistion */
ospf_if_lookup_by_lsa_pos(struct ospf_area * area,int lsa_pos)393 struct ospf_interface *ospf_if_lookup_by_lsa_pos(struct ospf_area *area,
394 						 int lsa_pos)
395 {
396 	struct listnode *node;
397 	struct ospf_interface *oi;
398 
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;
404 }
405 
ospf_if_lookup_by_local_addr(struct ospf * ospf,struct interface * ifp,struct in_addr address)406 struct ospf_interface *ospf_if_lookup_by_local_addr(struct ospf *ospf,
407 						    struct interface *ifp,
408 						    struct in_addr address)
409 {
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;
423 }
424 
ospf_if_lookup_by_prefix(struct ospf * ospf,struct prefix_ipv4 * p)425 struct ospf_interface *ospf_if_lookup_by_prefix(struct ospf *ospf,
426 						struct prefix_ipv4 *p)
427 {
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 		}
441 	}
442 	return NULL;
443 }
444 
445 /* determine receiving interface by ifp and source address */
ospf_if_lookup_recv_if(struct ospf * ospf,struct in_addr src,struct interface * ifp)446 struct ospf_interface *ospf_if_lookup_recv_if(struct ospf *ospf,
447 					      struct in_addr src,
448 					      struct interface *ifp)
449 {
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 
469 		if (if_is_loopback(oi->ifp) || if_is_vrf(oi->ifp))
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)) {
476 			if ((match == NULL) || (match->address->prefixlen
477 						< oi->address->prefixlen))
478 				match = oi;
479 		}
480 	}
481 
482 	return match;
483 }
484 
ospf_if_reset_stats(struct ospf_interface * oi)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 
ospf_if_stream_unset(struct ospf_interface * oi)494 void ospf_if_stream_unset(struct ospf_interface *oi)
495 {
496 	struct ospf *ospf = oi->ospf;
497 
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;
508 	}
509 }
510 
511 
ospf_new_if_params(void)512 static struct ospf_if_params *ospf_new_if_params(void)
513 {
514 	struct ospf_if_params *oip;
515 
516 	oip = XCALLOC(MTYPE_OSPF_IF_PARAMS, sizeof(struct ospf_if_params));
517 
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 	UNSET_IF_PARAM(oip, if_area);
531 
532 	oip->auth_crypt = list_new();
533 
534 	oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
535 	oip->is_v_wait_set = false;
536 
537 	return oip;
538 }
539 
ospf_del_if_params(struct ospf_if_params * oip)540 void ospf_del_if_params(struct ospf_if_params *oip)
541 {
542 	list_delete(&oip->auth_crypt);
543 	bfd_info_free(&(oip->bfd_info));
544 	XFREE(MTYPE_OSPF_IF_PARAMS, oip);
545 }
546 
ospf_free_if_params(struct interface * ifp,struct in_addr addr)547 void ospf_free_if_params(struct interface *ifp, struct in_addr addr)
548 {
549 	struct ospf_if_params *oip;
550 	struct prefix_ipv4 p;
551 	struct route_node *rn;
552 
553 	p.family = AF_INET;
554 	p.prefixlen = IPV4_MAX_PREFIXLEN;
555 	p.prefix = addr;
556 	rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
557 	if (!rn || !rn->info)
558 		return;
559 
560 	oip = rn->info;
561 	route_unlock_node(rn);
562 
563 	if (!OSPF_IF_PARAM_CONFIGURED(oip, output_cost_cmd)
564 	    && !OSPF_IF_PARAM_CONFIGURED(oip, transmit_delay)
565 	    && !OSPF_IF_PARAM_CONFIGURED(oip, retransmit_interval)
566 	    && !OSPF_IF_PARAM_CONFIGURED(oip, passive_interface)
567 	    && !OSPF_IF_PARAM_CONFIGURED(oip, v_hello)
568 	    && !OSPF_IF_PARAM_CONFIGURED(oip, fast_hello)
569 	    && !OSPF_IF_PARAM_CONFIGURED(oip, v_wait)
570 	    && !OSPF_IF_PARAM_CONFIGURED(oip, priority)
571 	    && !OSPF_IF_PARAM_CONFIGURED(oip, type)
572 	    && !OSPF_IF_PARAM_CONFIGURED(oip, auth_simple)
573 	    && !OSPF_IF_PARAM_CONFIGURED(oip, auth_type)
574 	    && !OSPF_IF_PARAM_CONFIGURED(oip, if_area)
575 	    && listcount(oip->auth_crypt) == 0) {
576 		ospf_del_if_params(oip);
577 		rn->info = NULL;
578 		route_unlock_node(rn);
579 	}
580 }
581 
ospf_lookup_if_params(struct interface * ifp,struct in_addr addr)582 struct ospf_if_params *ospf_lookup_if_params(struct interface *ifp,
583 					     struct in_addr addr)
584 {
585 	struct prefix_ipv4 p;
586 	struct route_node *rn;
587 
588 	p.family = AF_INET;
589 	p.prefixlen = IPV4_MAX_PREFIXLEN;
590 	p.prefix = addr;
591 
592 	rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
593 
594 	if (rn) {
595 		route_unlock_node(rn);
596 		return rn->info;
597 	}
598 
599 	return NULL;
600 }
601 
ospf_get_if_params(struct interface * ifp,struct in_addr addr)602 struct ospf_if_params *ospf_get_if_params(struct interface *ifp,
603 					  struct in_addr addr)
604 {
605 	struct prefix_ipv4 p;
606 	struct route_node *rn;
607 
608 	p.family = AF_INET;
609 	p.prefixlen = IPV4_MAX_PREFIXLEN;
610 	p.prefix = addr;
611 	apply_mask_ipv4(&p);
612 
613 	rn = route_node_get(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
614 
615 	if (rn->info == NULL)
616 		rn->info = ospf_new_if_params();
617 	else
618 		route_unlock_node(rn);
619 
620 	return rn->info;
621 }
622 
ospf_if_update_params(struct interface * ifp,struct in_addr addr)623 void ospf_if_update_params(struct interface *ifp, struct in_addr addr)
624 {
625 	struct route_node *rn;
626 	struct ospf_interface *oi;
627 
628 	for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
629 		if ((oi = rn->info) == NULL)
630 			continue;
631 
632 		if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &addr))
633 			oi->params = ospf_lookup_if_params(
634 				ifp, oi->address->u.prefix4);
635 	}
636 }
637 
ospf_if_new_hook(struct interface * ifp)638 int ospf_if_new_hook(struct interface *ifp)
639 {
640 	int rc = 0;
641 
642 	ifp->info = XCALLOC(MTYPE_OSPF_IF_INFO, sizeof(struct ospf_if_info));
643 
644 	IF_OIFS(ifp) = route_table_init();
645 	IF_OIFS_PARAMS(ifp) = route_table_init();
646 
647 	IF_DEF_PARAMS(ifp) = ospf_new_if_params();
648 
649 	SET_IF_PARAM(IF_DEF_PARAMS(ifp), transmit_delay);
650 	IF_DEF_PARAMS(ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
651 
652 	SET_IF_PARAM(IF_DEF_PARAMS(ifp), retransmit_interval);
653 	IF_DEF_PARAMS(ifp)->retransmit_interval =
654 		OSPF_RETRANSMIT_INTERVAL_DEFAULT;
655 
656 	SET_IF_PARAM(IF_DEF_PARAMS(ifp), priority);
657 	IF_DEF_PARAMS(ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
658 
659 	IF_DEF_PARAMS(ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
660 
661 	SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_hello);
662 	IF_DEF_PARAMS(ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
663 
664 	SET_IF_PARAM(IF_DEF_PARAMS(ifp), fast_hello);
665 	IF_DEF_PARAMS(ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
666 
667 	SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_wait);
668 	IF_DEF_PARAMS(ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
669 
670 	SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_simple);
671 	memset(IF_DEF_PARAMS(ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
672 
673 	SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_type);
674 	IF_DEF_PARAMS(ifp)->auth_type = OSPF_AUTH_NOTSET;
675 
676 	rc = ospf_opaque_new_if(ifp);
677 	return rc;
678 }
679 
ospf_if_delete_hook(struct interface * ifp)680 static int ospf_if_delete_hook(struct interface *ifp)
681 {
682 	int rc = 0;
683 	struct route_node *rn;
684 	rc = ospf_opaque_del_if(ifp);
685 
686 	route_table_finish(IF_OIFS(ifp));
687 
688 	for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
689 		if (rn->info)
690 			ospf_del_if_params(rn->info);
691 	route_table_finish(IF_OIFS_PARAMS(ifp));
692 
693 	ospf_del_if_params((struct ospf_if_params *)IF_DEF_PARAMS(ifp));
694 	XFREE(MTYPE_OSPF_IF_INFO, ifp->info);
695 
696 	return rc;
697 }
698 
ospf_if_is_enable(struct ospf_interface * oi)699 int ospf_if_is_enable(struct ospf_interface *oi)
700 {
701 	if (!(if_is_loopback(oi->ifp) || if_is_vrf(oi->ifp)))
702 		if (if_is_up(oi->ifp))
703 			return 1;
704 
705 	return 0;
706 }
707 
ospf_if_set_multicast(struct ospf_interface * oi)708 void ospf_if_set_multicast(struct ospf_interface *oi)
709 {
710 	if ((oi->state > ISM_Loopback) && (oi->type != OSPF_IFTYPE_LOOPBACK)
711 	    && (oi->type != OSPF_IFTYPE_VIRTUALLINK)
712 	    && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
713 		/* The interface should belong to the OSPF-all-routers group. */
714 		if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)
715 		    && (ospf_if_add_allspfrouters(oi->ospf, oi->address,
716 						  oi->ifp->ifindex)
717 			>= 0))
718 			/* Set the flag only if the system call to join
719 			 * succeeded. */
720 			OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
721 	} else {
722 		/* The interface should NOT belong to the OSPF-all-routers
723 		 * group. */
724 		if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)) {
725 			/* Only actually drop if this is the last reference */
726 			if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
727 				ospf_if_drop_allspfrouters(oi->ospf,
728 							   oi->address,
729 							   oi->ifp->ifindex);
730 			/* Unset the flag regardless of whether the system call
731 			   to leave
732 			   the group succeeded, since it's much safer to assume
733 			   that
734 			   we are not a member. */
735 			OI_MEMBER_LEFT(oi, MEMBER_ALLROUTERS);
736 		}
737 	}
738 
739 	if (((oi->type == OSPF_IFTYPE_BROADCAST)
740 	     || (oi->type == OSPF_IFTYPE_POINTOPOINT))
741 	    && ((oi->state == ISM_DR) || (oi->state == ISM_Backup))
742 	    && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
743 		/* The interface should belong to the OSPF-designated-routers
744 		 * group. */
745 		if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)
746 		    && (ospf_if_add_alldrouters(oi->ospf, oi->address,
747 						oi->ifp->ifindex)
748 			>= 0))
749 			/* Set the flag only if the system call to join
750 			 * succeeded. */
751 			OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
752 	} else {
753 		/* The interface should NOT belong to the
754 		 * OSPF-designated-routers group */
755 		if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)) {
756 			/* drop only if last reference */
757 			if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
758 				ospf_if_drop_alldrouters(oi->ospf, oi->address,
759 							 oi->ifp->ifindex);
760 
761 			/* Unset the flag regardless of whether the system call
762 			   to leave
763 			   the group succeeded, since it's much safer to assume
764 			   that
765 			   we are not a member. */
766 			OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
767 		}
768 	}
769 }
770 
ospf_if_up(struct ospf_interface * oi)771 int ospf_if_up(struct ospf_interface *oi)
772 {
773 	if (oi == NULL)
774 		return 0;
775 
776 	if (oi->type == OSPF_IFTYPE_LOOPBACK)
777 		OSPF_ISM_EVENT_SCHEDULE(oi, ISM_LoopInd);
778 	else {
779 		OSPF_ISM_EVENT_SCHEDULE(oi, ISM_InterfaceUp);
780 	}
781 
782 	return 1;
783 }
784 
ospf_if_down(struct ospf_interface * oi)785 int ospf_if_down(struct ospf_interface *oi)
786 {
787 	if (oi == NULL)
788 		return 0;
789 
790 	OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
791 	/* delete position in router LSA */
792 	oi->lsa_pos_beg = 0;
793 	oi->lsa_pos_end = 0;
794 	/* Shutdown packet reception and sending */
795 	ospf_if_stream_unset(oi);
796 
797 	return 1;
798 }
799 
800 
801 /* Virtual Link related functions. */
802 
ospf_vl_data_new(struct ospf_area * area,struct in_addr vl_peer)803 struct ospf_vl_data *ospf_vl_data_new(struct ospf_area *area,
804 				      struct in_addr vl_peer)
805 {
806 	struct ospf_vl_data *vl_data;
807 
808 	vl_data = XCALLOC(MTYPE_OSPF_VL_DATA, sizeof(struct ospf_vl_data));
809 
810 	vl_data->vl_peer.s_addr = vl_peer.s_addr;
811 	vl_data->vl_area_id = area->area_id;
812 	vl_data->vl_area_id_fmt = area->area_id_fmt;
813 
814 	return vl_data;
815 }
816 
ospf_vl_data_free(struct ospf_vl_data * vl_data)817 void ospf_vl_data_free(struct ospf_vl_data *vl_data)
818 {
819 	XFREE(MTYPE_OSPF_VL_DATA, vl_data);
820 }
821 
822 unsigned int vlink_count = 0;
823 
ospf_vl_new(struct ospf * ospf,struct ospf_vl_data * vl_data)824 struct ospf_interface *ospf_vl_new(struct ospf *ospf,
825 				   struct ospf_vl_data *vl_data)
826 {
827 	struct ospf_interface *voi;
828 	struct interface *vi;
829 	char ifname[INTERFACE_NAMSIZ];
830 	struct ospf_area *area;
831 	struct in_addr area_id;
832 	struct connected *co;
833 	struct prefix_ipv4 *p;
834 
835 	if (IS_DEBUG_OSPF_EVENT)
836 		zlog_debug("ospf_vl_new()(%s): Start", ospf_get_name(ospf));
837 	if (vlink_count == OSPF_VL_MAX_COUNT) {
838 		if (IS_DEBUG_OSPF_EVENT)
839 			zlog_debug(
840 				"ospf_vl_new(): Alarm: cannot create more than OSPF_MAX_VL_COUNT virtual links");
841 		return NULL;
842 	}
843 
844 	if (IS_DEBUG_OSPF_EVENT)
845 		zlog_debug(
846 			"ospf_vl_new(): creating pseudo zebra interface vrf id %u",
847 			ospf->vrf_id);
848 
849 	snprintf(ifname, sizeof(ifname), "VLINK%u", vlink_count);
850 	vi = if_create_name(ifname, ospf->vrf_id);
851 	/*
852 	 * if_create_name sets ZEBRA_INTERFACE_LINKDETECTION
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 = INADDR_ANY;
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 = INADDR_ANY;
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 
898 	if (IS_DEBUG_OSPF_EVENT)
899 		zlog_debug("ospf_vl_new(): Stop");
900 	return voi;
901 }
902 
ospf_vl_if_delete(struct ospf_vl_data * vl_data)903 static void ospf_vl_if_delete(struct ospf_vl_data *vl_data)
904 {
905 	struct interface *ifp = vl_data->vl_oi->ifp;
906 
907 	vl_data->vl_oi->address->u.prefix4.s_addr = INADDR_ANY;
908 	vl_data->vl_oi->address->prefixlen = 0;
909 	ospf_if_free(vl_data->vl_oi);
910 	if_delete(&ifp);
911 	vlink_count--;
912 }
913 
914 /* for a defined area, count the number of configured vl
915  */
ospf_vl_count(struct ospf * ospf,struct ospf_area * area)916 int ospf_vl_count(struct ospf *ospf, struct ospf_area *area)
917 {
918 	int count = 0;
919 	struct ospf_vl_data *vl_data;
920 	struct listnode *node;
921 
922 	for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
923 		if (area
924 		    && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
925 			continue;
926 		count++;
927 	}
928 	return count;
929 }
930 
931 /* Look up vl_data for given peer, optionally qualified to be in the
932  * specified area. NULL area returns first found..
933  */
ospf_vl_lookup(struct ospf * ospf,struct ospf_area * area,struct in_addr vl_peer)934 struct ospf_vl_data *ospf_vl_lookup(struct ospf *ospf, struct ospf_area *area,
935 				    struct in_addr vl_peer)
936 {
937 	struct ospf_vl_data *vl_data;
938 	struct listnode *node;
939 
940 	if (IS_DEBUG_OSPF_EVENT) {
941 		zlog_debug("%s: Looking for %s", __func__, inet_ntoa(vl_peer));
942 		if (area)
943 			zlog_debug("%s: in area %s", __func__,
944 				   inet_ntoa(area->area_id));
945 	}
946 
947 	for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
948 		if (IS_DEBUG_OSPF_EVENT)
949 			zlog_debug("%s: VL %s, peer %s", __func__,
950 				   vl_data->vl_oi->ifp->name,
951 				   inet_ntoa(vl_data->vl_peer));
952 
953 		if (area
954 		    && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
955 			continue;
956 
957 		if (IPV4_ADDR_SAME(&vl_data->vl_peer, &vl_peer))
958 			return vl_data;
959 	}
960 
961 	return NULL;
962 }
963 
ospf_vl_shutdown(struct ospf_vl_data * vl_data)964 static void ospf_vl_shutdown(struct ospf_vl_data *vl_data)
965 {
966 	struct ospf_interface *oi;
967 
968 	if ((oi = vl_data->vl_oi) == NULL)
969 		return;
970 
971 	oi->address->u.prefix4.s_addr = INADDR_ANY;
972 	oi->address->prefixlen = 0;
973 
974 	UNSET_FLAG(oi->ifp->flags, IFF_UP);
975 	/* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
976 	OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
977 }
978 
ospf_vl_add(struct ospf * ospf,struct ospf_vl_data * vl_data)979 void ospf_vl_add(struct ospf *ospf, struct ospf_vl_data *vl_data)
980 {
981 	listnode_add(ospf->vlinks, vl_data);
982 	hook_call(ospf_vl_add, vl_data);
983 }
984 
ospf_vl_delete(struct ospf * ospf,struct ospf_vl_data * vl_data)985 void ospf_vl_delete(struct ospf *ospf, struct ospf_vl_data *vl_data)
986 {
987 	ospf_vl_shutdown(vl_data);
988 	ospf_vl_if_delete(vl_data);
989 
990 	hook_call(ospf_vl_delete, vl_data);
991 	listnode_delete(ospf->vlinks, vl_data);
992 
993 	ospf_vl_data_free(vl_data);
994 }
995 
ospf_vl_set_params(struct ospf_area * area,struct ospf_vl_data * vl_data,struct vertex * v)996 static int ospf_vl_set_params(struct ospf_area *area,
997 			      struct ospf_vl_data *vl_data, struct vertex *v)
998 {
999 	int changed = 0;
1000 	struct ospf_interface *voi;
1001 	struct listnode *node;
1002 	struct vertex_parent *vp = NULL;
1003 	unsigned int i;
1004 	struct router_lsa *rl;
1005 	struct ospf_interface *oi;
1006 
1007 	voi = vl_data->vl_oi;
1008 
1009 	if (voi->output_cost != v->distance) {
1010 
1011 		voi->output_cost = v->distance;
1012 		changed = 1;
1013 	}
1014 
1015 	for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) {
1016 		vl_data->nexthop.lsa_pos = vp->nexthop->lsa_pos;
1017 		vl_data->nexthop.router = vp->nexthop->router;
1018 
1019 		/*
1020 		 * Only deal with interface data when the local
1021 		 * (calculating) node is the SPF root node
1022 		 */
1023 		if (!area->spf_dry_run) {
1024 			oi = ospf_if_lookup_by_lsa_pos(
1025 				area, vl_data->nexthop.lsa_pos);
1026 
1027 			if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
1028 					    &oi->address->u.prefix4))
1029 				changed = 1;
1030 
1031 			voi->address->u.prefix4 = oi->address->u.prefix4;
1032 			voi->address->prefixlen = oi->address->prefixlen;
1033 		}
1034 
1035 		break; /* We take the first interface. */
1036 	}
1037 
1038 	rl = (struct router_lsa *)v->lsa;
1039 
1040 	/* use SPF determined backlink index in struct vertex
1041 	 * for virtual link destination address
1042 	 */
1043 	if (vp && vp->backlink >= 0) {
1044 		if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1045 				    &rl->link[vp->backlink].link_data))
1046 			changed = 1;
1047 		vl_data->peer_addr = rl->link[vp->backlink].link_data;
1048 	} else {
1049 		/* This is highly odd, there is no backlink index
1050 		 * there should be due to the ospf_spf_has_link() check
1051 		 * in SPF. Lets warn and try pick a link anyway.
1052 		 */
1053 		zlog_info("ospf_vl_set_params: No backlink for %s!",
1054 			  vl_data->vl_oi->ifp->name);
1055 		for (i = 0; i < ntohs(rl->links); i++) {
1056 			switch (rl->link[i].type) {
1057 			case LSA_LINK_TYPE_VIRTUALLINK:
1058 				if (IS_DEBUG_OSPF_EVENT)
1059 					zlog_debug(
1060 						"found back link through VL");
1061 			/* fallthru */
1062 			case LSA_LINK_TYPE_TRANSIT:
1063 			case LSA_LINK_TYPE_POINTOPOINT:
1064 				if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1065 						    &rl->link[i].link_data))
1066 					changed = 1;
1067 				vl_data->peer_addr = rl->link[i].link_data;
1068 			}
1069 		}
1070 	}
1071 
1072 	if (IS_DEBUG_OSPF_EVENT)
1073 		zlog_debug("%s: %s peer address: %s, cost: %d,%schanged",
1074 			   __func__, vl_data->vl_oi->ifp->name,
1075 			   inet_ntoa(vl_data->peer_addr), voi->output_cost,
1076 			   (changed ? " " : " un"));
1077 
1078 	return changed;
1079 }
1080 
1081 
ospf_vl_up_check(struct ospf_area * area,struct in_addr rid,struct vertex * v)1082 void ospf_vl_up_check(struct ospf_area *area, struct in_addr rid,
1083 		      struct vertex *v)
1084 {
1085 	struct ospf *ospf = area->ospf;
1086 	struct listnode *node;
1087 	struct ospf_vl_data *vl_data;
1088 	struct ospf_interface *oi;
1089 
1090 	if (IS_DEBUG_OSPF_EVENT) {
1091 		zlog_debug("ospf_vl_up_check(): Start");
1092 		zlog_debug("ospf_vl_up_check(): Router ID is %s",
1093 			   inet_ntoa(rid));
1094 		zlog_debug("ospf_vl_up_check(): Area is %s",
1095 			   inet_ntoa(area->area_id));
1096 	}
1097 
1098 	for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1099 		if (IS_DEBUG_OSPF_EVENT) {
1100 			zlog_debug("%s: considering VL, %s in area %s",
1101 				   __func__, vl_data->vl_oi->ifp->name,
1102 				   inet_ntoa(vl_data->vl_area_id));
1103 			zlog_debug("%s: peer ID: %s", __func__,
1104 				   inet_ntoa(vl_data->vl_peer));
1105 		}
1106 
1107 		if (IPV4_ADDR_SAME(&vl_data->vl_peer, &rid)
1108 		    && IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) {
1109 			oi = vl_data->vl_oi;
1110 			SET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1111 
1112 			if (IS_DEBUG_OSPF_EVENT)
1113 				zlog_debug(
1114 					"ospf_vl_up_check(): this VL matched");
1115 
1116 			if (oi->state == ISM_Down) {
1117 				if (IS_DEBUG_OSPF_EVENT)
1118 					zlog_debug(
1119 						"ospf_vl_up_check(): VL is down, waking it up");
1120 				SET_FLAG(oi->ifp->flags, IFF_UP);
1121 				OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceUp);
1122 			}
1123 
1124 			if (ospf_vl_set_params(area, vl_data, v)) {
1125 				if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1126 					zlog_debug(
1127 						"ospf_vl_up_check: VL cost change, scheduling router lsa refresh");
1128 				if (ospf->backbone)
1129 					ospf_router_lsa_update_area(
1130 						ospf->backbone);
1131 				else if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1132 					zlog_debug(
1133 						"ospf_vl_up_check: VL cost change, no backbone!");
1134 			}
1135 		}
1136 	}
1137 }
1138 
ospf_vl_unapprove(struct ospf * ospf)1139 void ospf_vl_unapprove(struct ospf *ospf)
1140 {
1141 	struct listnode *node;
1142 	struct ospf_vl_data *vl_data;
1143 
1144 	for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data))
1145 		UNSET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1146 }
1147 
ospf_vl_shut_unapproved(struct ospf * ospf)1148 void ospf_vl_shut_unapproved(struct ospf *ospf)
1149 {
1150 	struct listnode *node, *nnode;
1151 	struct ospf_vl_data *vl_data;
1152 
1153 	for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
1154 		if (!CHECK_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED))
1155 			ospf_vl_shutdown(vl_data);
1156 }
1157 
ospf_full_virtual_nbrs(struct ospf_area * area)1158 int ospf_full_virtual_nbrs(struct ospf_area *area)
1159 {
1160 	if (IS_DEBUG_OSPF_EVENT) {
1161 		zlog_debug(
1162 			"counting fully adjacent virtual neighbors in area %s",
1163 			inet_ntoa(area->area_id));
1164 		zlog_debug("there are %d of them", area->full_vls);
1165 	}
1166 
1167 	return area->full_vls;
1168 }
1169 
ospf_vls_in_area(struct ospf_area * area)1170 int ospf_vls_in_area(struct ospf_area *area)
1171 {
1172 	struct listnode *node;
1173 	struct ospf_vl_data *vl_data;
1174 	int c = 0;
1175 
1176 	for (ALL_LIST_ELEMENTS_RO(area->ospf->vlinks, node, vl_data))
1177 		if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1178 			c++;
1179 
1180 	return c;
1181 }
1182 
1183 
ospf_crypt_key_new(void)1184 struct crypt_key *ospf_crypt_key_new(void)
1185 {
1186 	return XCALLOC(MTYPE_OSPF_CRYPT_KEY, sizeof(struct crypt_key));
1187 }
1188 
ospf_crypt_key_add(struct list * crypt,struct crypt_key * ck)1189 void ospf_crypt_key_add(struct list *crypt, struct crypt_key *ck)
1190 {
1191 	listnode_add(crypt, ck);
1192 }
1193 
ospf_crypt_key_lookup(struct list * auth_crypt,uint8_t key_id)1194 struct crypt_key *ospf_crypt_key_lookup(struct list *auth_crypt, uint8_t key_id)
1195 {
1196 	struct listnode *node;
1197 	struct crypt_key *ck;
1198 
1199 	for (ALL_LIST_ELEMENTS_RO(auth_crypt, node, ck))
1200 		if (ck->key_id == key_id)
1201 			return ck;
1202 
1203 	return NULL;
1204 }
1205 
ospf_crypt_key_delete(struct list * auth_crypt,uint8_t key_id)1206 int ospf_crypt_key_delete(struct list *auth_crypt, uint8_t key_id)
1207 {
1208 	struct listnode *node, *nnode;
1209 	struct crypt_key *ck;
1210 
1211 	for (ALL_LIST_ELEMENTS(auth_crypt, node, nnode, ck)) {
1212 		if (ck->key_id == key_id) {
1213 			listnode_delete(auth_crypt, ck);
1214 			XFREE(MTYPE_OSPF_CRYPT_KEY, ck);
1215 			return 1;
1216 		}
1217 	}
1218 
1219 	return 0;
1220 }
1221 
ospf_default_iftype(struct interface * ifp)1222 uint8_t ospf_default_iftype(struct interface *ifp)
1223 {
1224 	if (if_is_pointopoint(ifp))
1225 		return OSPF_IFTYPE_POINTOPOINT;
1226 	else if (if_is_loopback(ifp) || if_is_vrf(ifp))
1227 		return OSPF_IFTYPE_LOOPBACK;
1228 	else
1229 		return OSPF_IFTYPE_BROADCAST;
1230 }
1231 
ospf_if_interface(struct interface * ifp)1232 void ospf_if_interface(struct interface *ifp)
1233 {
1234 	hook_call(ospf_if_update, ifp);
1235 }
1236 
ospf_ifp_create(struct interface * ifp)1237 static int ospf_ifp_create(struct interface *ifp)
1238 {
1239 	struct ospf *ospf = NULL;
1240 	struct ospf_if_params *params;
1241 	struct route_node *rn;
1242 	uint32_t count = 0;
1243 	struct ospf_if_info *oii;
1244 
1245 	if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1246 		zlog_debug(
1247 			"Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u",
1248 			ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
1249 			ifp->vrf_id, ifp->ifindex,
1250 			(unsigned long long)ifp->flags, ifp->metric, ifp->mtu,
1251 			ifp->speed);
1252 
1253 	assert(ifp->info);
1254 
1255 	oii = ifp->info;
1256 	oii->curr_mtu = ifp->mtu;
1257 
1258 	if (IF_DEF_PARAMS(ifp)
1259 	    && !OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp), type)) {
1260 		SET_IF_PARAM(IF_DEF_PARAMS(ifp), type);
1261 		IF_DEF_PARAMS(ifp)->type = ospf_default_iftype(ifp);
1262 	}
1263 
1264 	ospf = ospf_lookup_by_vrf_id(ifp->vrf_id);
1265 	if (!ospf)
1266 		return 0;
1267 
1268 	params = IF_DEF_PARAMS(ifp);
1269 	if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
1270 		count++;
1271 
1272 	for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
1273 		if ((params = rn->info) && OSPF_IF_PARAM_CONFIGURED(params, if_area))
1274 			count++;
1275 
1276 	if (count > 0) {
1277 		ospf->if_ospf_cli_count += count;
1278 		ospf_interface_area_set(ospf, ifp);
1279 	}
1280 
1281 	ospf_if_recalculate_output_cost(ifp);
1282 
1283 	ospf_if_update(ospf, ifp);
1284 
1285 	hook_call(ospf_if_update, ifp);
1286 
1287 	return 0;
1288 }
1289 
ospf_ifp_up(struct interface * ifp)1290 static int ospf_ifp_up(struct interface *ifp)
1291 {
1292 	struct ospf_interface *oi;
1293 	struct route_node *rn;
1294 	struct ospf_if_info *oii = ifp->info;
1295 
1296 	ospf_if_recalculate_output_cost(ifp);
1297 
1298 	if (oii && oii->curr_mtu != ifp->mtu) {
1299 		if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1300 			zlog_debug(
1301 				"Zebra: Interface[%s] MTU change %u -> %u.",
1302 				ifp->name, oii->curr_mtu, ifp->mtu);
1303 
1304 		oii->curr_mtu = ifp->mtu;
1305 		/* Must reset the interface (simulate down/up) when MTU
1306 		 * changes. */
1307 		ospf_if_reset(ifp);
1308 
1309 		return 0;
1310 	}
1311 
1312 	if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1313 		zlog_debug("Zebra: Interface[%s] state change to up.",
1314 			   ifp->name);
1315 
1316 	for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1317 		if ((oi = rn->info) == NULL)
1318 			continue;
1319 
1320 		ospf_if_up(oi);
1321 	}
1322 
1323 	return 0;
1324 }
1325 
ospf_ifp_down(struct interface * ifp)1326 static int ospf_ifp_down(struct interface *ifp)
1327 {
1328 	struct ospf_interface *oi;
1329 	struct route_node *node;
1330 
1331 	if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1332 		zlog_debug("Zebra: Interface[%s] state change to down.",
1333 			   ifp->name);
1334 
1335 	for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
1336 		if ((oi = node->info) == NULL)
1337 			continue;
1338 		ospf_if_down(oi);
1339 	}
1340 
1341 	return 0;
1342 }
1343 
ospf_ifp_destroy(struct interface * ifp)1344 static int ospf_ifp_destroy(struct interface *ifp)
1345 {
1346 	struct ospf *ospf;
1347 	struct ospf_if_params *params;
1348 	struct route_node *rn;
1349 	uint32_t count = 0;
1350 
1351 	if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1352 		zlog_debug(
1353 			"Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
1354 			ifp->name, ospf_vrf_id_to_name(ifp->vrf_id),
1355 			ifp->vrf_id, ifp->ifindex,
1356 			(unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
1357 
1358 	hook_call(ospf_if_delete, ifp);
1359 
1360 	ospf = ospf_lookup_by_vrf_id(ifp->vrf_id);
1361 	if (ospf) {
1362 		params = IF_DEF_PARAMS(ifp);
1363 		if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
1364 			count++;
1365 
1366 		for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
1367 			if ((params = rn->info) && OSPF_IF_PARAM_CONFIGURED(params, if_area))
1368 				count++;
1369 
1370 		if (count > 0) {
1371 			ospf->if_ospf_cli_count -= count;
1372 			ospf_interface_area_unset(ospf, ifp);
1373 		}
1374 	}
1375 
1376 	for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn))
1377 		if (rn->info)
1378 			ospf_if_free((struct ospf_interface *)rn->info);
1379 
1380 	return 0;
1381 }
1382 
ospf_if_init(void)1383 void ospf_if_init(void)
1384 {
1385 	if_zapi_callbacks(ospf_ifp_create, ospf_ifp_up,
1386 			  ospf_ifp_down, ospf_ifp_destroy);
1387 
1388 	/* Initialize Zebra interface data structure. */
1389 	hook_register_prio(if_add, 0, ospf_if_new_hook);
1390 	hook_register_prio(if_del, 0, ospf_if_delete_hook);
1391 }
1392