1 /*
2  * OSPF Link State Advertisement
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 it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2, or (at your option) any
10  * later version.
11  *
12  * GNU Zebra is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; see the file COPYING; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <zebra.h>
23 
24 #include "monotime.h"
25 #include "linklist.h"
26 #include "prefix.h"
27 #include "if.h"
28 #include "table.h"
29 #include "memory.h"
30 #include "stream.h"
31 #include "log.h"
32 #include "thread.h"
33 #include "hash.h"
34 #include "sockunion.h" /* for inet_aton() */
35 #include "checksum.h"
36 #include "network.h"
37 
38 #include "ospfd/ospfd.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_ism.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_lsa.h"
43 #include "ospfd/ospf_lsdb.h"
44 #include "ospfd/ospf_neighbor.h"
45 #include "ospfd/ospf_nsm.h"
46 #include "ospfd/ospf_flood.h"
47 #include "ospfd/ospf_packet.h"
48 #include "ospfd/ospf_spf.h"
49 #include "ospfd/ospf_dump.h"
50 #include "ospfd/ospf_route.h"
51 #include "ospfd/ospf_ase.h"
52 #include "ospfd/ospf_zebra.h"
53 #include "ospfd/ospf_abr.h"
54 #include "ospfd/ospf_errors.h"
55 
get_metric(uint8_t * metric)56 uint32_t get_metric(uint8_t *metric)
57 {
58 	uint32_t m;
59 	m = metric[0];
60 	m = (m << 8) + metric[1];
61 	m = (m << 8) + metric[2];
62 	return m;
63 }
64 
65 
int2tv(int a)66 struct timeval int2tv(int a)
67 {
68 	struct timeval ret;
69 
70 	ret.tv_sec = a;
71 	ret.tv_usec = 0;
72 
73 	return ret;
74 }
75 
msec2tv(int a)76 struct timeval msec2tv(int a)
77 {
78 	struct timeval ret;
79 
80 	ret.tv_sec = a / 1000;
81 	ret.tv_usec = (a % 1000) * 1000;
82 
83 	return ret;
84 }
85 
ospf_lsa_refresh_delay(struct ospf_lsa * lsa)86 int ospf_lsa_refresh_delay(struct ospf_lsa *lsa)
87 {
88 	struct timeval delta;
89 	int delay = 0;
90 
91 	if (monotime_since(&lsa->tv_orig, &delta)
92 	    < OSPF_MIN_LS_INTERVAL * 1000LL) {
93 		struct timeval minv = msec2tv(OSPF_MIN_LS_INTERVAL);
94 		timersub(&minv, &delta, &minv);
95 
96 		/* TBD: remove padding to full sec, return timeval instead */
97 		delay = minv.tv_sec + !!minv.tv_usec;
98 
99 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
100 			zlog_debug(
101 				"LSA[Type%d:%s]: Refresh timer delay %d seconds",
102 				lsa->data->type, inet_ntoa(lsa->data->id),
103 				delay);
104 
105 		assert(delay > 0);
106 	}
107 
108 	return delay;
109 }
110 
111 
get_age(struct ospf_lsa * lsa)112 int get_age(struct ospf_lsa *lsa)
113 {
114 	struct timeval rel;
115 
116 	monotime_since(&lsa->tv_recv, &rel);
117 	return ntohs(lsa->data->ls_age) + rel.tv_sec;
118 }
119 
120 
121 /* Fletcher Checksum -- Refer to RFC1008. */
122 
123 /* All the offsets are zero-based. The offsets in the RFC1008 are
124    one-based. */
ospf_lsa_checksum(struct lsa_header * lsa)125 uint16_t ospf_lsa_checksum(struct lsa_header *lsa)
126 {
127 	uint8_t *buffer = &lsa->options;
128 	int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */
129 
130 	/* Skip the AGE field */
131 	uint16_t len = ntohs(lsa->length) - options_offset;
132 
133 	/* Checksum offset starts from "options" field, not the beginning of the
134 	   lsa_header struct. The offset is 14, rather than 16. */
135 	int checksum_offset = (uint8_t *)&lsa->checksum - buffer;
136 
137 	return fletcher_checksum(buffer, len, checksum_offset);
138 }
139 
ospf_lsa_checksum_valid(struct lsa_header * lsa)140 int ospf_lsa_checksum_valid(struct lsa_header *lsa)
141 {
142 	uint8_t *buffer = &lsa->options;
143 	int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */
144 
145 	/* Skip the AGE field */
146 	uint16_t len = ntohs(lsa->length) - options_offset;
147 
148 	return (fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE)
149 		== 0);
150 }
151 
152 
153 /* Create OSPF LSA. */
ospf_lsa_new(void)154 struct ospf_lsa *ospf_lsa_new(void)
155 {
156 	struct ospf_lsa *new;
157 
158 	new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));
159 
160 	new->flags = 0;
161 	new->lock = 1;
162 	new->retransmit_counter = 0;
163 	monotime(&new->tv_recv);
164 	new->tv_orig = new->tv_recv;
165 	new->refresh_list = -1;
166 	new->vrf_id = VRF_DEFAULT;
167 
168 	return new;
169 }
170 
ospf_lsa_new_and_data(size_t size)171 struct ospf_lsa *ospf_lsa_new_and_data(size_t size)
172 {
173 	struct ospf_lsa *new;
174 
175 	new = ospf_lsa_new();
176 	new->data = ospf_lsa_data_new(size);
177 
178 	return new;
179 }
180 
181 /* Duplicate OSPF LSA. */
ospf_lsa_dup(struct ospf_lsa * lsa)182 struct ospf_lsa *ospf_lsa_dup(struct ospf_lsa *lsa)
183 {
184 	struct ospf_lsa *new;
185 
186 	if (lsa == NULL)
187 		return NULL;
188 
189 	new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));
190 
191 	memcpy(new, lsa, sizeof(struct ospf_lsa));
192 	UNSET_FLAG(new->flags, OSPF_LSA_DISCARD);
193 	new->lock = 1;
194 	new->retransmit_counter = 0;
195 	new->data = ospf_lsa_data_dup(lsa->data);
196 
197 	/* kevinm: Clear the refresh_list, otherwise there are going
198 	   to be problems when we try to remove the LSA from the
199 	   queue (which it's not a member of.)
200 	   XXX: Should we add the LSA to the refresh_list queue? */
201 	new->refresh_list = -1;
202 
203 	if (IS_DEBUG_OSPF(lsa, LSA))
204 		zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa,
205 			   (void *)new);
206 
207 	return new;
208 }
209 
210 /* Free OSPF LSA. */
ospf_lsa_free(struct ospf_lsa * lsa)211 void ospf_lsa_free(struct ospf_lsa *lsa)
212 {
213 	assert(lsa->lock == 0);
214 
215 	if (IS_DEBUG_OSPF(lsa, LSA))
216 		zlog_debug("LSA: freed %p", (void *)lsa);
217 
218 	/* Delete LSA data. */
219 	if (lsa->data != NULL)
220 		ospf_lsa_data_free(lsa->data);
221 
222 	assert(lsa->refresh_list < 0);
223 
224 	memset(lsa, 0, sizeof(struct ospf_lsa));
225 	XFREE(MTYPE_OSPF_LSA, lsa);
226 }
227 
228 /* Lock LSA. */
ospf_lsa_lock(struct ospf_lsa * lsa)229 struct ospf_lsa *ospf_lsa_lock(struct ospf_lsa *lsa)
230 {
231 	lsa->lock++;
232 	return lsa;
233 }
234 
235 /* Unlock LSA. */
ospf_lsa_unlock(struct ospf_lsa ** lsa)236 void ospf_lsa_unlock(struct ospf_lsa **lsa)
237 {
238 	/* This is sanity check. */
239 	if (!lsa || !*lsa)
240 		return;
241 
242 	(*lsa)->lock--;
243 
244 	assert((*lsa)->lock >= 0);
245 
246 	if ((*lsa)->lock == 0) {
247 		assert(CHECK_FLAG((*lsa)->flags, OSPF_LSA_DISCARD));
248 		ospf_lsa_free(*lsa);
249 		*lsa = NULL;
250 	}
251 }
252 
253 /* Check discard flag. */
ospf_lsa_discard(struct ospf_lsa * lsa)254 void ospf_lsa_discard(struct ospf_lsa *lsa)
255 {
256 	if (!CHECK_FLAG(lsa->flags, OSPF_LSA_DISCARD)) {
257 		SET_FLAG(lsa->flags, OSPF_LSA_DISCARD);
258 		ospf_lsa_unlock(&lsa);
259 	}
260 }
261 
262 /* Create LSA data. */
ospf_lsa_data_new(size_t size)263 struct lsa_header *ospf_lsa_data_new(size_t size)
264 {
265 	return XCALLOC(MTYPE_OSPF_LSA_DATA, size);
266 }
267 
268 /* Duplicate LSA data. */
ospf_lsa_data_dup(struct lsa_header * lsah)269 struct lsa_header *ospf_lsa_data_dup(struct lsa_header *lsah)
270 {
271 	struct lsa_header *new;
272 
273 	new = ospf_lsa_data_new(ntohs(lsah->length));
274 	memcpy(new, lsah, ntohs(lsah->length));
275 
276 	return new;
277 }
278 
279 /* Free LSA data. */
ospf_lsa_data_free(struct lsa_header * lsah)280 void ospf_lsa_data_free(struct lsa_header *lsah)
281 {
282 	if (IS_DEBUG_OSPF(lsa, LSA))
283 		zlog_debug("LSA[Type%d:%s]: data freed %p", lsah->type,
284 			   inet_ntoa(lsah->id), (void *)lsah);
285 
286 	XFREE(MTYPE_OSPF_LSA_DATA, lsah);
287 }
288 
289 
290 /* LSA general functions. */
291 
dump_lsa_key(struct ospf_lsa * lsa)292 const char *dump_lsa_key(struct ospf_lsa *lsa)
293 {
294 	static char buf[sizeof("Type255,id(255.255.255.255),ar(255.255.255.255)")+1];
295 	struct lsa_header *lsah;
296 
297 	if (lsa != NULL && (lsah = lsa->data) != NULL) {
298 		char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
299 		strlcpy(id, inet_ntoa(lsah->id), sizeof(id));
300 		strlcpy(ar, inet_ntoa(lsah->adv_router), sizeof(ar));
301 
302 		snprintf(buf, sizeof(buf), "Type%d,id(%s),ar(%s)", lsah->type,
303 			 id, ar);
304 	} else
305 		strlcpy(buf, "NULL", sizeof(buf));
306 
307 	return buf;
308 }
309 
lsa_seqnum_increment(struct ospf_lsa * lsa)310 uint32_t lsa_seqnum_increment(struct ospf_lsa *lsa)
311 {
312 	uint32_t seqnum;
313 
314 	seqnum = ntohl(lsa->data->ls_seqnum) + 1;
315 
316 	return htonl(seqnum);
317 }
318 
lsa_header_set(struct stream * s,uint8_t options,uint8_t type,struct in_addr id,struct in_addr router_id)319 void lsa_header_set(struct stream *s, uint8_t options, uint8_t type,
320 		    struct in_addr id, struct in_addr router_id)
321 {
322 	struct lsa_header *lsah;
323 
324 	lsah = (struct lsa_header *)STREAM_DATA(s);
325 
326 	lsah->ls_age = htons(OSPF_LSA_INITIAL_AGE);
327 	lsah->options = options;
328 	lsah->type = type;
329 	lsah->id = id;
330 	lsah->adv_router = router_id;
331 	lsah->ls_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
332 
333 	stream_forward_endp(s, OSPF_LSA_HEADER_SIZE);
334 }
335 
336 
337 /* router-LSA related functions. */
338 /* Get router-LSA flags. */
router_lsa_flags(struct ospf_area * area)339 static uint8_t router_lsa_flags(struct ospf_area *area)
340 {
341 	uint8_t flags;
342 
343 	flags = area->ospf->flags;
344 
345 	/* Set virtual link flag. */
346 	if (ospf_full_virtual_nbrs(area))
347 		SET_FLAG(flags, ROUTER_LSA_VIRTUAL);
348 	else
349 		/* Just sanity check */
350 		UNSET_FLAG(flags, ROUTER_LSA_VIRTUAL);
351 
352 	/* Set Shortcut ABR behabiour flag. */
353 	UNSET_FLAG(flags, ROUTER_LSA_SHORTCUT);
354 	if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
355 		if (!OSPF_IS_AREA_BACKBONE(area))
356 			if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT
357 			     && area->ospf->backbone == NULL)
358 			    || area->shortcut_configured
359 				       == OSPF_SHORTCUT_ENABLE)
360 				SET_FLAG(flags, ROUTER_LSA_SHORTCUT);
361 
362 	/* ASBR can't exit in stub area. */
363 	if (area->external_routing == OSPF_AREA_STUB)
364 		UNSET_FLAG(flags, ROUTER_LSA_EXTERNAL);
365 	/* If ASBR set External flag */
366 	else if (IS_OSPF_ASBR(area->ospf))
367 		SET_FLAG(flags, ROUTER_LSA_EXTERNAL);
368 
369 	/* Set ABR dependent flags */
370 	if (IS_OSPF_ABR(area->ospf)) {
371 		SET_FLAG(flags, ROUTER_LSA_BORDER);
372 		/* If Area is NSSA and we are both ABR and unconditional
373 		 * translator,
374 		 * set Nt bit to inform other routers.
375 		 */
376 		if ((area->external_routing == OSPF_AREA_NSSA)
377 		    && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
378 			SET_FLAG(flags, ROUTER_LSA_NT);
379 	}
380 	return flags;
381 }
382 
383 /* Lookup neighbor other than myself.
384    And check neighbor count,
385    Point-to-Point link must have only 1 neighbor. */
ospf_nbr_lookup_ptop(struct ospf_interface * oi)386 struct ospf_neighbor *ospf_nbr_lookup_ptop(struct ospf_interface *oi)
387 {
388 	struct ospf_neighbor *nbr = NULL;
389 	struct route_node *rn;
390 
391 	/* Search neighbor, there must be one of two nbrs. */
392 	for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
393 		if ((nbr = rn->info))
394 			if (!IPV4_ADDR_SAME(&nbr->router_id,
395 					    &oi->ospf->router_id))
396 				if (nbr->state == NSM_Full) {
397 					route_unlock_node(rn);
398 					break;
399 				}
400 
401 	/* PtoP link must have only 1 neighbor. */
402 	if (ospf_nbr_count(oi, 0) > 1)
403 		flog_warn(EC_OSPF_PTP_NEIGHBOR,
404 			  "Point-to-Point link has more than 1 neighobrs.");
405 
406 	return nbr;
407 }
408 
409 /* Determine cost of link, taking RFC3137 stub-router support into
410  * consideration
411  */
ospf_link_cost(struct ospf_interface * oi)412 static uint16_t ospf_link_cost(struct ospf_interface *oi)
413 {
414 	/* RFC3137 stub router support */
415 	if (!CHECK_FLAG(oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
416 		return oi->output_cost;
417 	else
418 		return OSPF_OUTPUT_COST_INFINITE;
419 }
420 
421 /* Set a link information. */
link_info_set(struct stream ** s,struct in_addr id,struct in_addr data,uint8_t type,uint8_t tos,uint16_t cost)422 static char link_info_set(struct stream **s, struct in_addr id,
423 			  struct in_addr data, uint8_t type, uint8_t tos,
424 			  uint16_t cost)
425 {
426 	/* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
427 	 * vast majority of cases. Some rare routers with lots of links need
428 	 * more.
429 	 * we try accomodate those here.
430 	 */
431 	if (STREAM_WRITEABLE(*s) < OSPF_ROUTER_LSA_LINK_SIZE) {
432 		size_t ret = OSPF_MAX_LSA_SIZE;
433 
434 		/* Can we enlarge the stream still? */
435 		if (STREAM_SIZE(*s) == OSPF_MAX_LSA_SIZE) {
436 			/* we futz the size here for simplicity, really we need
437 			 * to account
438 			 * for just:
439 			 * IP Header - (sizeof(struct ip))
440 			 * OSPF Header - OSPF_HEADER_SIZE
441 			 * LSA Header - OSPF_LSA_HEADER_SIZE
442 			 * MD5 auth data, if MD5 is configured -
443 			 * OSPF_AUTH_MD5_SIZE.
444 			 *
445 			 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
446 			 */
447 			ret = stream_resize_inplace(
448 				s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
449 		}
450 
451 		if (ret == OSPF_MAX_LSA_SIZE) {
452 			flog_warn(
453 				EC_OSPF_LSA_SIZE,
454 				"%s: Out of space in LSA stream, left %zd, size %zd",
455 				__func__, STREAM_WRITEABLE(*s),
456 				STREAM_SIZE(*s));
457 			return 0;
458 		}
459 	}
460 
461 	/* TOS based routing is not supported. */
462 	stream_put_ipv4(*s, id.s_addr);   /* Link ID. */
463 	stream_put_ipv4(*s, data.s_addr); /* Link Data. */
464 	stream_putc(*s, type);		  /* Link Type. */
465 	stream_putc(*s, tos);		  /* TOS = 0. */
466 	stream_putw(*s, cost);		  /* Link Cost. */
467 
468 	return 1;
469 }
470 
471 /* Describe Point-to-Point link (Section 12.4.1.1). */
lsa_link_ptop_set(struct stream ** s,struct ospf_interface * oi)472 static int lsa_link_ptop_set(struct stream **s, struct ospf_interface *oi)
473 {
474 	int links = 0;
475 	struct ospf_neighbor *nbr;
476 	struct in_addr id, mask, data;
477 	uint16_t cost = ospf_link_cost(oi);
478 
479 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
480 		zlog_debug("LSA[Type1]: Set link Point-to-Point");
481 
482 	if ((nbr = ospf_nbr_lookup_ptop(oi)))
483 		if (nbr->state == NSM_Full) {
484 			if (CHECK_FLAG(oi->connected->flags,
485 				       ZEBRA_IFA_UNNUMBERED)) {
486 				/* For unnumbered point-to-point networks, the
487 				   Link Data field
488 				   should specify the interface's MIB-II ifIndex
489 				   value. */
490 				data.s_addr = htonl(oi->ifp->ifindex);
491 				links += link_info_set(
492 					s, nbr->router_id, data,
493 					LSA_LINK_TYPE_POINTOPOINT, 0, cost);
494 			} else {
495 				links += link_info_set(
496 					s, nbr->router_id,
497 					oi->address->u.prefix4,
498 					LSA_LINK_TYPE_POINTOPOINT, 0, cost);
499 			}
500 		}
501 
502 	/* no need for a stub link for unnumbered interfaces */
503 	if (!CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)) {
504 		/* Regardless of the state of the neighboring router, we must
505 		   add a Type 3 link (stub network).
506 		   N.B. Options 1 & 2 share basically the same logic. */
507 		masklen2ip(oi->address->prefixlen, &mask);
508 		id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr
509 			    & mask.s_addr;
510 		links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
511 				       oi->output_cost);
512 	}
513 
514 	return links;
515 }
516 
517 /* Describe Broadcast Link. */
lsa_link_broadcast_set(struct stream ** s,struct ospf_interface * oi)518 static int lsa_link_broadcast_set(struct stream **s, struct ospf_interface *oi)
519 {
520 	struct ospf_neighbor *dr;
521 	struct in_addr id, mask;
522 	uint16_t cost = ospf_link_cost(oi);
523 
524 	/* Describe Type 3 Link. */
525 	if (oi->state == ISM_Waiting) {
526 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
527 			zlog_debug(
528 				"LSA[Type1]: Interface %s is in state Waiting. Adding stub interface",
529 				oi->ifp->name);
530 		masklen2ip(oi->address->prefixlen, &mask);
531 		id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
532 		return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
533 				     oi->output_cost);
534 	}
535 
536 	dr = ospf_nbr_lookup_by_addr(oi->nbrs, &DR(oi));
537 	/* Describe Type 2 link. */
538 	if (dr && (dr->state == NSM_Full
539 		   || IPV4_ADDR_SAME(&oi->address->u.prefix4, &DR(oi)))
540 	    && ospf_nbr_count(oi, NSM_Full) > 0) {
541 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
542 			zlog_debug(
543 				"LSA[Type1]: Interface %s has a DR. Adding transit interface",
544 				oi->ifp->name);
545 		return link_info_set(s, DR(oi), oi->address->u.prefix4,
546 				     LSA_LINK_TYPE_TRANSIT, 0, cost);
547 	}
548 	/* Describe type 3 link. */
549 	else {
550 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
551 			zlog_debug(
552 				"LSA[Type1]: Interface %s has no DR. Adding stub interface",
553 				oi->ifp->name);
554 		masklen2ip(oi->address->prefixlen, &mask);
555 		id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
556 		return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
557 				     oi->output_cost);
558 	}
559 }
560 
lsa_link_loopback_set(struct stream ** s,struct ospf_interface * oi)561 static int lsa_link_loopback_set(struct stream **s, struct ospf_interface *oi)
562 {
563 	struct in_addr id, mask;
564 
565 	/* Describe Type 3 Link. */
566 	if (oi->state != ISM_Loopback)
567 		return 0;
568 
569 	mask.s_addr = 0xffffffff;
570 	id.s_addr = oi->address->u.prefix4.s_addr;
571 	return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
572 }
573 
574 /* Describe Virtual Link. */
lsa_link_virtuallink_set(struct stream ** s,struct ospf_interface * oi)575 static int lsa_link_virtuallink_set(struct stream **s,
576 				    struct ospf_interface *oi)
577 {
578 	struct ospf_neighbor *nbr;
579 	uint16_t cost = ospf_link_cost(oi);
580 
581 	if (oi->state == ISM_PointToPoint)
582 		if ((nbr = ospf_nbr_lookup_ptop(oi)))
583 			if (nbr->state == NSM_Full) {
584 				return link_info_set(s, nbr->router_id,
585 						     oi->address->u.prefix4,
586 						     LSA_LINK_TYPE_VIRTUALLINK,
587 						     0, cost);
588 			}
589 
590 	return 0;
591 }
592 
593 #define lsa_link_nbma_set(S,O)  lsa_link_broadcast_set (S, O)
594 
595 /* this function add for support point-to-multipoint ,see rfc2328
596 12.4.1.4.*/
597 /* from "edward rrr" <edward_rrr@hotmail.com>
598    http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
lsa_link_ptomp_set(struct stream ** s,struct ospf_interface * oi)599 static int lsa_link_ptomp_set(struct stream **s, struct ospf_interface *oi)
600 {
601 	int links = 0;
602 	struct route_node *rn;
603 	struct ospf_neighbor *nbr = NULL;
604 	struct in_addr id, mask;
605 	uint16_t cost = ospf_link_cost(oi);
606 
607 	mask.s_addr = 0xffffffff;
608 	id.s_addr = oi->address->u.prefix4.s_addr;
609 	links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
610 
611 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
612 		zlog_debug("PointToMultipoint: running ptomultip_set");
613 
614 	/* Search neighbor, */
615 	for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
616 		if ((nbr = rn->info) != NULL)
617 			/* Ignore myself. */
618 			if (!IPV4_ADDR_SAME(&nbr->router_id,
619 					    &oi->ospf->router_id))
620 				if (nbr->state == NSM_Full)
621 
622 				{
623 					links += link_info_set(
624 						s, nbr->router_id,
625 						oi->address->u.prefix4,
626 						LSA_LINK_TYPE_POINTOPOINT, 0,
627 						cost);
628 					if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
629 						zlog_debug(
630 							"PointToMultipoint: set link to %s",
631 							inet_ntoa(
632 								oi->address->u
633 									.prefix4));
634 				}
635 
636 	return links;
637 }
638 
639 /* Set router-LSA link information. */
router_lsa_link_set(struct stream ** s,struct ospf_area * area)640 static int router_lsa_link_set(struct stream **s, struct ospf_area *area)
641 {
642 	struct listnode *node;
643 	struct ospf_interface *oi;
644 	int links = 0;
645 
646 	for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
647 		struct interface *ifp = oi->ifp;
648 
649 		/* Check interface is up, OSPF is enable. */
650 		if (if_is_operative(ifp)) {
651 			if (oi->state != ISM_Down) {
652 				oi->lsa_pos_beg = links;
653 				/* Describe each link. */
654 				switch (oi->type) {
655 				case OSPF_IFTYPE_POINTOPOINT:
656 					links += lsa_link_ptop_set(s, oi);
657 					break;
658 				case OSPF_IFTYPE_BROADCAST:
659 					links += lsa_link_broadcast_set(s, oi);
660 					break;
661 				case OSPF_IFTYPE_NBMA:
662 					links += lsa_link_nbma_set(s, oi);
663 					break;
664 				case OSPF_IFTYPE_POINTOMULTIPOINT:
665 					links += lsa_link_ptomp_set(s, oi);
666 					break;
667 				case OSPF_IFTYPE_VIRTUALLINK:
668 					links +=
669 						lsa_link_virtuallink_set(s, oi);
670 					break;
671 				case OSPF_IFTYPE_LOOPBACK:
672 					links += lsa_link_loopback_set(s, oi);
673 				}
674 				oi->lsa_pos_end = links;
675 			}
676 		}
677 	}
678 
679 	return links;
680 }
681 
682 /* Set router-LSA body. */
ospf_router_lsa_body_set(struct stream ** s,struct ospf_area * area)683 static void ospf_router_lsa_body_set(struct stream **s, struct ospf_area *area)
684 {
685 	unsigned long putp;
686 	uint16_t cnt;
687 
688 	/* Set flags. */
689 	stream_putc(*s, router_lsa_flags(area));
690 
691 	/* Set Zero fields. */
692 	stream_putc(*s, 0);
693 
694 	/* Keep pointer to # links. */
695 	putp = stream_get_endp(*s);
696 
697 	/* Forward word */
698 	stream_putw(*s, 0);
699 
700 	/* Set all link information. */
701 	cnt = router_lsa_link_set(s, area);
702 
703 	/* Set # of links here. */
704 	stream_putw_at(*s, putp, cnt);
705 }
706 
ospf_stub_router_timer(struct thread * t)707 static int ospf_stub_router_timer(struct thread *t)
708 {
709 	struct ospf_area *area = THREAD_ARG(t);
710 
711 	area->t_stub_router = NULL;
712 
713 	SET_FLAG(area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
714 
715 	/* clear stub route state and generate router-lsa refresh, don't
716 	 * clobber an administratively set stub-router state though.
717 	 */
718 	if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
719 		return 0;
720 
721 	UNSET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
722 
723 	ospf_router_lsa_update_area(area);
724 
725 	return 0;
726 }
727 
ospf_stub_router_check(struct ospf_area * area)728 static void ospf_stub_router_check(struct ospf_area *area)
729 {
730 	/* area must either be administratively configured to be stub
731 	 * or startup-time stub-router must be configured and we must in a
732 	 * pre-stub
733 	 * state.
734 	 */
735 	if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED)) {
736 		SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
737 		return;
738 	}
739 
740 	/* not admin-stubbed, check whether startup stubbing is configured and
741 	 * whether it's not been done yet
742 	 */
743 	if (CHECK_FLAG(area->stub_router_state,
744 		       OSPF_AREA_WAS_START_STUB_ROUTED))
745 		return;
746 
747 	if (area->ospf->stub_router_startup_time
748 	    == OSPF_STUB_ROUTER_UNCONFIGURED) {
749 		/* stub-router is hence done forever for this area, even if
750 		 * someone
751 		 * tries configure it (take effect next restart).
752 		 */
753 		SET_FLAG(area->stub_router_state,
754 			 OSPF_AREA_WAS_START_STUB_ROUTED);
755 		return;
756 	}
757 
758 	/* startup stub-router configured and not yet done */
759 	SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
760 
761 	OSPF_AREA_TIMER_ON(area->t_stub_router, ospf_stub_router_timer,
762 			   area->ospf->stub_router_startup_time);
763 }
764 
765 /* Create new router-LSA. */
ospf_router_lsa_new(struct ospf_area * area)766 static struct ospf_lsa *ospf_router_lsa_new(struct ospf_area *area)
767 {
768 	struct ospf *ospf = area->ospf;
769 	struct stream *s;
770 	struct lsa_header *lsah;
771 	struct ospf_lsa *new;
772 	int length;
773 
774 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
775 		zlog_debug("LSA[Type1]: Create router-LSA instance");
776 
777 	/* check whether stub-router is desired, and if this is the first
778 	 * router LSA.
779 	 */
780 	ospf_stub_router_check(area);
781 
782 	/* Create a stream for LSA. */
783 	s = stream_new(OSPF_MAX_LSA_SIZE);
784 	/* Set LSA common header fields. */
785 	lsa_header_set(s, LSA_OPTIONS_GET(area) | LSA_OPTIONS_NSSA_GET(area),
786 		       OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
787 
788 	/* Set router-LSA body fields. */
789 	ospf_router_lsa_body_set(&s, area);
790 
791 	/* Set length. */
792 	length = stream_get_endp(s);
793 	lsah = (struct lsa_header *)STREAM_DATA(s);
794 	lsah->length = htons(length);
795 
796 	/* Now, create OSPF LSA instance. */
797 	new = ospf_lsa_new_and_data(length);
798 
799 	new->area = area;
800 	SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
801 	new->vrf_id = area->ospf->vrf_id;
802 
803 	/* Copy LSA data to store, discard stream. */
804 	memcpy(new->data, lsah, length);
805 	stream_free(s);
806 
807 	return new;
808 }
809 
810 /* Originate Router-LSA. */
ospf_router_lsa_originate(struct ospf_area * area)811 static struct ospf_lsa *ospf_router_lsa_originate(struct ospf_area *area)
812 {
813 	struct ospf_lsa *new;
814 
815 	/* Create new router-LSA instance. */
816 	if ((new = ospf_router_lsa_new(area)) == NULL) {
817 		zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
818 		return NULL;
819 	}
820 
821 	/* Sanity check. */
822 	if (new->data->adv_router.s_addr == INADDR_ANY) {
823 		if (IS_DEBUG_OSPF_EVENT)
824 			zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
825 		ospf_lsa_discard(new);
826 		return NULL;
827 	}
828 
829 	/* Install LSA to LSDB. */
830 	new = ospf_lsa_install(area->ospf, NULL, new);
831 
832 	/* Update LSA origination count. */
833 	area->ospf->lsa_originate_count++;
834 
835 	/* Flooding new LSA through area. */
836 	ospf_flood_through_area(area, NULL, new);
837 
838 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
839 		zlog_debug("LSA[Type%d:%s]: Originate router-LSA %p",
840 			   new->data->type, inet_ntoa(new->data->id),
841 			   (void *)new);
842 		ospf_lsa_header_dump(new->data);
843 	}
844 
845 	return new;
846 }
847 
848 /* Refresh router-LSA. */
ospf_router_lsa_refresh(struct ospf_lsa * lsa)849 static struct ospf_lsa *ospf_router_lsa_refresh(struct ospf_lsa *lsa)
850 {
851 	struct ospf_area *area = lsa->area;
852 	struct ospf_lsa *new;
853 
854 	/* Sanity check. */
855 	assert(lsa->data);
856 
857 	/* Delete LSA from neighbor retransmit-list. */
858 	ospf_ls_retransmit_delete_nbr_area(area, lsa);
859 
860 	/* Unregister LSA from refresh-list */
861 	ospf_refresher_unregister_lsa(area->ospf, lsa);
862 
863 	/* Create new router-LSA instance. */
864 	if ((new = ospf_router_lsa_new(area)) == NULL) {
865 		zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
866 		return NULL;
867 	}
868 
869 	new->data->ls_seqnum = lsa_seqnum_increment(lsa);
870 
871 	ospf_lsa_install(area->ospf, NULL, new);
872 
873 	/* Flood LSA through area. */
874 	ospf_flood_through_area(area, NULL, new);
875 
876 	/* Debug logging. */
877 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
878 		zlog_debug("LSA[Type%d:%s]: router-LSA refresh",
879 			   new->data->type, inet_ntoa(new->data->id));
880 		ospf_lsa_header_dump(new->data);
881 	}
882 
883 	return NULL;
884 }
885 
ospf_router_lsa_update_area(struct ospf_area * area)886 int ospf_router_lsa_update_area(struct ospf_area *area)
887 {
888 	if (IS_DEBUG_OSPF_EVENT)
889 		zlog_debug("[router-LSA]: (router-LSA area update)");
890 
891 	/* Now refresh router-LSA. */
892 	if (area->router_lsa_self)
893 		ospf_lsa_refresh(area->ospf, area->router_lsa_self);
894 	/* Newly originate router-LSA. */
895 	else
896 		ospf_router_lsa_originate(area);
897 
898 	return 0;
899 }
900 
ospf_router_lsa_update(struct ospf * ospf)901 int ospf_router_lsa_update(struct ospf *ospf)
902 {
903 	struct listnode *node, *nnode;
904 	struct ospf_area *area;
905 
906 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
907 		zlog_debug("Timer[router-LSA Update]: (timer expire)");
908 
909 	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
910 		struct ospf_lsa *lsa = area->router_lsa_self;
911 		struct router_lsa *rl;
912 		const char *area_str;
913 
914 		/* Keep Area ID string. */
915 		area_str = AREA_NAME(area);
916 
917 		/* If LSA not exist in this Area, originate new. */
918 		if (lsa == NULL) {
919 			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
920 				zlog_debug(
921 					"LSA[Type1]: Create router-LSA for Area %s",
922 					area_str);
923 
924 			ospf_router_lsa_originate(area);
925 		}
926 		/* If router-ID is changed, Link ID must change.
927 		   First flush old LSA, then originate new. */
928 		else if (!IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id)) {
929 			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
930 				zlog_debug(
931 					"LSA[Type%d:%s]: Refresh router-LSA for Area %s",
932 					lsa->data->type,
933 					inet_ntoa(lsa->data->id), area_str);
934 			ospf_refresher_unregister_lsa(ospf, lsa);
935 			ospf_lsa_flush_area(lsa, area);
936 			ospf_lsa_unlock(&area->router_lsa_self);
937 			area->router_lsa_self = NULL;
938 
939 			/* Refresh router-LSA, (not install) and flood through
940 			 * area. */
941 			ospf_router_lsa_update_area(area);
942 		} else {
943 			rl = (struct router_lsa *)lsa->data;
944 			/* Refresh router-LSA, (not install) and flood through
945 			 * area. */
946 			if (rl->flags != ospf->flags)
947 				ospf_router_lsa_update_area(area);
948 		}
949 	}
950 
951 	return 0;
952 }
953 
954 
955 /* network-LSA related functions. */
956 /* Originate Network-LSA. */
ospf_network_lsa_body_set(struct stream * s,struct ospf_interface * oi)957 static void ospf_network_lsa_body_set(struct stream *s,
958 				      struct ospf_interface *oi)
959 {
960 	struct in_addr mask;
961 	struct route_node *rn;
962 	struct ospf_neighbor *nbr;
963 
964 	masklen2ip(oi->address->prefixlen, &mask);
965 	stream_put_ipv4(s, mask.s_addr);
966 
967 	/* The network-LSA lists those routers that are fully adjacent to
968 	  the Designated Router; each fully adjacent router is identified by
969 	  its OSPF Router ID.  The Designated Router includes itself in this
970 	  list. RFC2328, Section 12.4.2 */
971 
972 	for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
973 		if ((nbr = rn->info) != NULL)
974 			if (nbr->state == NSM_Full || nbr == oi->nbr_self)
975 				stream_put_ipv4(s, nbr->router_id.s_addr);
976 }
977 
ospf_network_lsa_new(struct ospf_interface * oi)978 static struct ospf_lsa *ospf_network_lsa_new(struct ospf_interface *oi)
979 {
980 	struct stream *s;
981 	struct ospf_lsa *new;
982 	struct lsa_header *lsah;
983 	struct ospf_if_params *oip;
984 	int length;
985 
986 	/* If there are no neighbours on this network (the net is stub),
987 	   the router does not originate network-LSA (see RFC 12.4.2) */
988 	if (oi->full_nbrs == 0)
989 		return NULL;
990 
991 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
992 		zlog_debug("LSA[Type2]: Create network-LSA instance");
993 
994 	/* Create new stream for LSA. */
995 	s = stream_new(OSPF_MAX_LSA_SIZE);
996 	lsah = (struct lsa_header *)STREAM_DATA(s);
997 
998 	lsa_header_set(s, (OPTIONS(oi) | LSA_OPTIONS_GET(oi->area)),
999 		       OSPF_NETWORK_LSA, DR(oi), oi->ospf->router_id);
1000 
1001 	/* Set network-LSA body fields. */
1002 	ospf_network_lsa_body_set(s, oi);
1003 
1004 	/* Set length. */
1005 	length = stream_get_endp(s);
1006 	lsah->length = htons(length);
1007 
1008 	/* Create OSPF LSA instance. */
1009 	new = ospf_lsa_new_and_data(length);
1010 
1011 	new->area = oi->area;
1012 	SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1013 	new->vrf_id = oi->ospf->vrf_id;
1014 
1015 	/* Copy LSA to store. */
1016 	memcpy(new->data, lsah, length);
1017 	stream_free(s);
1018 
1019 	/* Remember prior network LSA sequence numbers, even if we stop
1020 	 * originating one for this oi, to try avoid re-originating LSAs with a
1021 	 * prior sequence number, and thus speed up adjency forming &
1022 	 * convergence.
1023 	 */
1024 	if ((oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4))) {
1025 		new->data->ls_seqnum = oip->network_lsa_seqnum;
1026 		new->data->ls_seqnum = lsa_seqnum_increment(new);
1027 	} else {
1028 		oip = ospf_get_if_params(oi->ifp, oi->address->u.prefix4);
1029 		ospf_if_update_params(oi->ifp, oi->address->u.prefix4);
1030 	}
1031 	oip->network_lsa_seqnum = new->data->ls_seqnum;
1032 
1033 	return new;
1034 }
1035 
1036 /* Originate network-LSA. */
ospf_network_lsa_update(struct ospf_interface * oi)1037 void ospf_network_lsa_update(struct ospf_interface *oi)
1038 {
1039 	struct ospf_lsa *new;
1040 
1041 	if (oi->network_lsa_self != NULL) {
1042 		ospf_lsa_refresh(oi->ospf, oi->network_lsa_self);
1043 		return;
1044 	}
1045 
1046 	/* Create new network-LSA instance. */
1047 	new = ospf_network_lsa_new(oi);
1048 	if (new == NULL)
1049 		return;
1050 
1051 	/* Install LSA to LSDB. */
1052 	new = ospf_lsa_install(oi->ospf, oi, new);
1053 
1054 	/* Update LSA origination count. */
1055 	oi->ospf->lsa_originate_count++;
1056 
1057 	/* Flooding new LSA through area. */
1058 	ospf_flood_through_area(oi->area, NULL, new);
1059 
1060 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1061 		zlog_debug("LSA[Type%d:%s]: Originate network-LSA %p",
1062 			   new->data->type, inet_ntoa(new->data->id),
1063 			   (void *)new);
1064 		ospf_lsa_header_dump(new->data);
1065 	}
1066 
1067 	return;
1068 }
1069 
ospf_network_lsa_refresh(struct ospf_lsa * lsa)1070 static struct ospf_lsa *ospf_network_lsa_refresh(struct ospf_lsa *lsa)
1071 {
1072 	struct ospf_area *area = lsa->area;
1073 	struct ospf_lsa *new, *new2;
1074 	struct ospf_if_params *oip;
1075 	struct ospf_interface *oi;
1076 
1077 	assert(lsa->data);
1078 
1079 	/* Retrieve the oi for the network LSA */
1080 	oi = ospf_if_lookup_by_local_addr(area->ospf, NULL, lsa->data->id);
1081 	if (oi == NULL) {
1082 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1083 			zlog_debug(
1084 				"LSA[Type%d:%s]: network-LSA refresh: no oi found, ick, ignoring.",
1085 				lsa->data->type, inet_ntoa(lsa->data->id));
1086 			ospf_lsa_header_dump(lsa->data);
1087 		}
1088 		return NULL;
1089 	}
1090 	/* Delete LSA from neighbor retransmit-list. */
1091 	ospf_ls_retransmit_delete_nbr_area(area, lsa);
1092 
1093 	/* Unregister LSA from refresh-list */
1094 	ospf_refresher_unregister_lsa(area->ospf, lsa);
1095 
1096 	/* Create new network-LSA instance. */
1097 	new = ospf_network_lsa_new(oi);
1098 	if (new == NULL)
1099 		return NULL;
1100 
1101 	oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4);
1102 	assert(oip != NULL);
1103 	oip->network_lsa_seqnum = new->data->ls_seqnum =
1104 		lsa_seqnum_increment(lsa);
1105 
1106 	new2 = ospf_lsa_install(area->ospf, oi, new);
1107 
1108 	assert(new2 == new);
1109 
1110 	/* Flood LSA through aera. */
1111 	ospf_flood_through_area(area, NULL, new);
1112 
1113 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1114 		zlog_debug("LSA[Type%d:%s]: network-LSA refresh",
1115 			   new->data->type, inet_ntoa(new->data->id));
1116 		ospf_lsa_header_dump(new->data);
1117 	}
1118 
1119 	return new;
1120 }
1121 
stream_put_ospf_metric(struct stream * s,uint32_t metric_value)1122 static void stream_put_ospf_metric(struct stream *s, uint32_t metric_value)
1123 {
1124 	uint32_t metric;
1125 	char *mp;
1126 
1127 	/* Put 0 metric. TOS metric is not supported. */
1128 	metric = htonl(metric_value);
1129 	mp = (char *)&metric;
1130 	mp++;
1131 	stream_put(s, mp, 3);
1132 }
1133 
1134 /* summary-LSA related functions. */
ospf_summary_lsa_body_set(struct stream * s,struct prefix * p,uint32_t metric)1135 static void ospf_summary_lsa_body_set(struct stream *s, struct prefix *p,
1136 				      uint32_t metric)
1137 {
1138 	struct in_addr mask;
1139 
1140 	masklen2ip(p->prefixlen, &mask);
1141 
1142 	/* Put Network Mask. */
1143 	stream_put_ipv4(s, mask.s_addr);
1144 
1145 	/* Set # TOS. */
1146 	stream_putc(s, (uint8_t)0);
1147 
1148 	/* Set metric. */
1149 	stream_put_ospf_metric(s, metric);
1150 }
1151 
ospf_summary_lsa_new(struct ospf_area * area,struct prefix * p,uint32_t metric,struct in_addr id)1152 static struct ospf_lsa *ospf_summary_lsa_new(struct ospf_area *area,
1153 					     struct prefix *p, uint32_t metric,
1154 					     struct in_addr id)
1155 {
1156 	struct stream *s;
1157 	struct ospf_lsa *new;
1158 	struct lsa_header *lsah;
1159 	int length;
1160 
1161 	if (id.s_addr == 0xffffffff) {
1162 		/* Maybe Link State ID not available. */
1163 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1164 			zlog_debug(
1165 				"LSA[Type%d]: Link ID not available, can't originate",
1166 				OSPF_SUMMARY_LSA);
1167 		return NULL;
1168 	}
1169 
1170 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1171 		zlog_debug("LSA[Type3]: Create summary-LSA instance");
1172 
1173 	/* Create new stream for LSA. */
1174 	s = stream_new(OSPF_MAX_LSA_SIZE);
1175 	lsah = (struct lsa_header *)STREAM_DATA(s);
1176 
1177 	lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_SUMMARY_LSA, id,
1178 		       area->ospf->router_id);
1179 
1180 	/* Set summary-LSA body fields. */
1181 	ospf_summary_lsa_body_set(s, p, metric);
1182 
1183 	/* Set length. */
1184 	length = stream_get_endp(s);
1185 	lsah->length = htons(length);
1186 
1187 	/* Create OSPF LSA instance. */
1188 	new = ospf_lsa_new_and_data(length);
1189 	new->area = area;
1190 	SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1191 	new->vrf_id = area->ospf->vrf_id;
1192 
1193 	/* Copy LSA to store. */
1194 	memcpy(new->data, lsah, length);
1195 	stream_free(s);
1196 
1197 	return new;
1198 }
1199 
1200 /* Originate Summary-LSA. */
ospf_summary_lsa_originate(struct prefix_ipv4 * p,uint32_t metric,struct ospf_area * area)1201 struct ospf_lsa *ospf_summary_lsa_originate(struct prefix_ipv4 *p,
1202 					    uint32_t metric,
1203 					    struct ospf_area *area)
1204 {
1205 	struct ospf_lsa *new;
1206 	struct in_addr id;
1207 
1208 	id = ospf_lsa_unique_id(area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
1209 
1210 	if (id.s_addr == 0xffffffff) {
1211 		/* Maybe Link State ID not available. */
1212 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1213 			zlog_debug(
1214 				"LSA[Type%d]: Link ID not available, can't originate",
1215 				OSPF_SUMMARY_LSA);
1216 		return NULL;
1217 	}
1218 
1219 	/* Create new summary-LSA instance. */
1220 	if (!(new = ospf_summary_lsa_new(area, (struct prefix *)p, metric, id)))
1221 		return NULL;
1222 
1223 	/* Instlal LSA to LSDB. */
1224 	new = ospf_lsa_install(area->ospf, NULL, new);
1225 
1226 	/* Update LSA origination count. */
1227 	area->ospf->lsa_originate_count++;
1228 
1229 	/* Flooding new LSA through area. */
1230 	ospf_flood_through_area(area, NULL, new);
1231 
1232 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1233 		zlog_debug("LSA[Type%d:%s]: Originate summary-LSA %p",
1234 			   new->data->type, inet_ntoa(new->data->id),
1235 			   (void *)new);
1236 		ospf_lsa_header_dump(new->data);
1237 	}
1238 
1239 	return new;
1240 }
1241 
ospf_summary_lsa_refresh(struct ospf * ospf,struct ospf_lsa * lsa)1242 static struct ospf_lsa *ospf_summary_lsa_refresh(struct ospf *ospf,
1243 						 struct ospf_lsa *lsa)
1244 {
1245 	struct ospf_lsa *new;
1246 	struct summary_lsa *sl;
1247 	struct prefix p;
1248 
1249 	/* Sanity check. */
1250 	assert(lsa->data);
1251 
1252 	sl = (struct summary_lsa *)lsa->data;
1253 	p.prefixlen = ip_masklen(sl->mask);
1254 	new = ospf_summary_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
1255 				   sl->header.id);
1256 
1257 	if (!new)
1258 		return NULL;
1259 
1260 	new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1261 
1262 	ospf_lsa_install(ospf, NULL, new);
1263 
1264 	/* Flood LSA through AS. */
1265 	ospf_flood_through_area(new->area, NULL, new);
1266 
1267 	/* Debug logging. */
1268 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1269 		zlog_debug("LSA[Type%d:%s]: summary-LSA refresh",
1270 			   new->data->type, inet_ntoa(new->data->id));
1271 		ospf_lsa_header_dump(new->data);
1272 	}
1273 
1274 	return new;
1275 }
1276 
1277 
1278 /* summary-ASBR-LSA related functions. */
ospf_summary_asbr_lsa_body_set(struct stream * s,struct prefix * p,uint32_t metric)1279 static void ospf_summary_asbr_lsa_body_set(struct stream *s, struct prefix *p,
1280 					   uint32_t metric)
1281 {
1282 	/* Put Network Mask. */
1283 	stream_put_ipv4(s, (uint32_t)0);
1284 
1285 	/* Set # TOS. */
1286 	stream_putc(s, (uint8_t)0);
1287 
1288 	/* Set metric. */
1289 	stream_put_ospf_metric(s, metric);
1290 }
1291 
ospf_summary_asbr_lsa_new(struct ospf_area * area,struct prefix * p,uint32_t metric,struct in_addr id)1292 static struct ospf_lsa *ospf_summary_asbr_lsa_new(struct ospf_area *area,
1293 						  struct prefix *p,
1294 						  uint32_t metric,
1295 						  struct in_addr id)
1296 {
1297 	struct stream *s;
1298 	struct ospf_lsa *new;
1299 	struct lsa_header *lsah;
1300 	int length;
1301 
1302 	if (id.s_addr == 0xffffffff) {
1303 		/* Maybe Link State ID not available. */
1304 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1305 			zlog_debug(
1306 				"LSA[Type%d]: Link ID not available, can't originate",
1307 				OSPF_ASBR_SUMMARY_LSA);
1308 		return NULL;
1309 	}
1310 
1311 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1312 		zlog_debug("LSA[Type3]: Create summary-LSA instance");
1313 
1314 	/* Create new stream for LSA. */
1315 	s = stream_new(OSPF_MAX_LSA_SIZE);
1316 	lsah = (struct lsa_header *)STREAM_DATA(s);
1317 
1318 	lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_ASBR_SUMMARY_LSA, id,
1319 		       area->ospf->router_id);
1320 
1321 	/* Set summary-LSA body fields. */
1322 	ospf_summary_asbr_lsa_body_set(s, p, metric);
1323 
1324 	/* Set length. */
1325 	length = stream_get_endp(s);
1326 	lsah->length = htons(length);
1327 
1328 	/* Create OSPF LSA instance. */
1329 	new = ospf_lsa_new_and_data(length);
1330 	new->area = area;
1331 	SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1332 	new->vrf_id = area->ospf->vrf_id;
1333 
1334 	/* Copy LSA to store. */
1335 	memcpy(new->data, lsah, length);
1336 	stream_free(s);
1337 
1338 	return new;
1339 }
1340 
1341 /* Originate summary-ASBR-LSA. */
ospf_summary_asbr_lsa_originate(struct prefix_ipv4 * p,uint32_t metric,struct ospf_area * area)1342 struct ospf_lsa *ospf_summary_asbr_lsa_originate(struct prefix_ipv4 *p,
1343 						 uint32_t metric,
1344 						 struct ospf_area *area)
1345 {
1346 	struct ospf_lsa *new;
1347 	struct in_addr id;
1348 
1349 	id = ospf_lsa_unique_id(area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA,
1350 				p);
1351 
1352 	if (id.s_addr == 0xffffffff) {
1353 		/* Maybe Link State ID not available. */
1354 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1355 			zlog_debug(
1356 				"LSA[Type%d]: Link ID not available, can't originate",
1357 				OSPF_ASBR_SUMMARY_LSA);
1358 		return NULL;
1359 	}
1360 
1361 	/* Create new summary-LSA instance. */
1362 	new = ospf_summary_asbr_lsa_new(area, (struct prefix *)p, metric, id);
1363 	if (!new)
1364 		return NULL;
1365 
1366 	/* Install LSA to LSDB. */
1367 	new = ospf_lsa_install(area->ospf, NULL, new);
1368 
1369 	/* Update LSA origination count. */
1370 	area->ospf->lsa_originate_count++;
1371 
1372 	/* Flooding new LSA through area. */
1373 	ospf_flood_through_area(area, NULL, new);
1374 
1375 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1376 		zlog_debug("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1377 			   new->data->type, inet_ntoa(new->data->id),
1378 			   (void *)new);
1379 		ospf_lsa_header_dump(new->data);
1380 	}
1381 
1382 	return new;
1383 }
1384 
ospf_summary_asbr_lsa_refresh(struct ospf * ospf,struct ospf_lsa * lsa)1385 static struct ospf_lsa *ospf_summary_asbr_lsa_refresh(struct ospf *ospf,
1386 						      struct ospf_lsa *lsa)
1387 {
1388 	struct ospf_lsa *new;
1389 	struct summary_lsa *sl;
1390 	struct prefix p;
1391 
1392 	/* Sanity check. */
1393 	assert(lsa->data);
1394 
1395 	sl = (struct summary_lsa *)lsa->data;
1396 	p.prefixlen = ip_masklen(sl->mask);
1397 	new = ospf_summary_asbr_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
1398 					sl->header.id);
1399 	if (!new)
1400 		return NULL;
1401 
1402 	new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1403 
1404 	ospf_lsa_install(ospf, NULL, new);
1405 
1406 	/* Flood LSA through area. */
1407 	ospf_flood_through_area(new->area, NULL, new);
1408 
1409 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1410 		zlog_debug("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1411 			   new->data->type, inet_ntoa(new->data->id));
1412 		ospf_lsa_header_dump(new->data);
1413 	}
1414 
1415 	return new;
1416 }
1417 
1418 /* AS-external-LSA related functions. */
1419 
1420 /* Get nexthop for AS-external-LSAs.  Return nexthop if its interface
1421    is connected, else 0*/
ospf_external_lsa_nexthop_get(struct ospf * ospf,struct in_addr nexthop)1422 static struct in_addr ospf_external_lsa_nexthop_get(struct ospf *ospf,
1423 						    struct in_addr nexthop)
1424 {
1425 	struct in_addr fwd;
1426 	struct prefix nh;
1427 	struct listnode *node;
1428 	struct ospf_interface *oi;
1429 
1430 	fwd.s_addr = 0;
1431 
1432 	if (!nexthop.s_addr)
1433 		return fwd;
1434 
1435 	/* Check whether nexthop is covered by OSPF network. */
1436 	nh.family = AF_INET;
1437 	nh.u.prefix4 = nexthop;
1438 	nh.prefixlen = IPV4_MAX_BITLEN;
1439 
1440 	/* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1441 	 * better to make use of the per-ifp table of ois.
1442 	 */
1443 	for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
1444 		if (if_is_operative(oi->ifp))
1445 			if (oi->address->family == AF_INET)
1446 				if (prefix_match(oi->address, &nh))
1447 					return nexthop;
1448 
1449 	return fwd;
1450 }
1451 
1452 /* NSSA-external-LSA related functions. */
1453 
1454 /* Get 1st IP connection for Forward Addr */
1455 
ospf_get_ip_from_ifp(struct ospf_interface * oi)1456 struct in_addr ospf_get_ip_from_ifp(struct ospf_interface *oi)
1457 {
1458 	struct in_addr fwd;
1459 
1460 	fwd.s_addr = INADDR_ANY;
1461 
1462 	if (if_is_operative(oi->ifp))
1463 		return oi->address->u.prefix4;
1464 
1465 	return fwd;
1466 }
1467 
1468 /* Get 1st IP connection for Forward Addr */
ospf_get_nssa_ip(struct ospf_area * area)1469 struct in_addr ospf_get_nssa_ip(struct ospf_area *area)
1470 {
1471 	struct in_addr fwd;
1472 	struct in_addr best_default;
1473 	struct listnode *node;
1474 	struct ospf_interface *oi;
1475 
1476 	fwd.s_addr = 0;
1477 	best_default.s_addr = 0;
1478 
1479 	for (ALL_LIST_ELEMENTS_RO(area->ospf->oiflist, node, oi)) {
1480 		if (if_is_operative(oi->ifp))
1481 			if (oi->area->external_routing == OSPF_AREA_NSSA)
1482 				if (oi->address
1483 				    && oi->address->family == AF_INET) {
1484 					if (best_default.s_addr == 0)
1485 						best_default =
1486 							oi->address->u.prefix4;
1487 					if (oi->area == area)
1488 						return oi->address->u.prefix4;
1489 				}
1490 	}
1491 	if (best_default.s_addr != 0)
1492 		return best_default;
1493 
1494 	if (best_default.s_addr != 0)
1495 		return best_default;
1496 
1497 	return fwd;
1498 }
1499 
metric_type(struct ospf * ospf,uint8_t src,unsigned short instance)1500 int metric_type(struct ospf *ospf, uint8_t src, unsigned short instance)
1501 {
1502 	struct ospf_redist *red;
1503 
1504 	red = ospf_redist_lookup(ospf, src, instance);
1505 
1506 	return ((!red || red->dmetric.type < 0) ? DEFAULT_METRIC_TYPE
1507 						: red->dmetric.type);
1508 }
1509 
metric_value(struct ospf * ospf,uint8_t src,unsigned short instance)1510 int metric_value(struct ospf *ospf, uint8_t src, unsigned short instance)
1511 {
1512 	struct ospf_redist *red;
1513 
1514 	red = ospf_redist_lookup(ospf, src, instance);
1515 	if (!red || red->dmetric.value < 0) {
1516 		if (src == DEFAULT_ROUTE) {
1517 			if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1518 				return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1519 			else
1520 				return DEFAULT_DEFAULT_ALWAYS_METRIC;
1521 		} else if (ospf->default_metric < 0)
1522 			return DEFAULT_DEFAULT_METRIC;
1523 		else
1524 			return ospf->default_metric;
1525 	}
1526 
1527 	return red->dmetric.value;
1528 }
1529 
1530 /* Set AS-external-LSA body. */
ospf_external_lsa_body_set(struct stream * s,struct external_info * ei,struct ospf * ospf)1531 static void ospf_external_lsa_body_set(struct stream *s,
1532 				       struct external_info *ei,
1533 				       struct ospf *ospf)
1534 {
1535 	struct prefix_ipv4 *p = &ei->p;
1536 	struct in_addr mask, fwd_addr;
1537 	uint32_t mvalue;
1538 	int mtype;
1539 	int type;
1540 	unsigned short instance;
1541 
1542 	/* Put Network Mask. */
1543 	masklen2ip(p->prefixlen, &mask);
1544 	stream_put_ipv4(s, mask.s_addr);
1545 
1546 	/* If prefix is default, specify DEFAULT_ROUTE. */
1547 	type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
1548 	instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
1549 
1550 	mtype = (ROUTEMAP_METRIC_TYPE(ei) != -1)
1551 			? ROUTEMAP_METRIC_TYPE(ei)
1552 			: metric_type(ospf, type, instance);
1553 
1554 	mvalue = (ROUTEMAP_METRIC(ei) != -1)
1555 			 ? ROUTEMAP_METRIC(ei)
1556 			 : metric_value(ospf, type, instance);
1557 
1558 	/* Put type of external metric. */
1559 	stream_putc(s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1560 
1561 	/* Put 0 metric. TOS metric is not supported. */
1562 	stream_put_ospf_metric(s, mvalue);
1563 
1564 	/* Get forwarding address to nexthop if on the Connection List, else 0.
1565 	 */
1566 	fwd_addr = ospf_external_lsa_nexthop_get(ospf, ei->nexthop);
1567 
1568 	/* Put forwarding address. */
1569 	stream_put_ipv4(s, fwd_addr.s_addr);
1570 
1571 	/* Put route tag */
1572 	stream_putl(s, ei->tag);
1573 }
1574 
1575 /* Create new external-LSA. */
ospf_external_lsa_new(struct ospf * ospf,struct external_info * ei,struct in_addr * old_id)1576 static struct ospf_lsa *ospf_external_lsa_new(struct ospf *ospf,
1577 					      struct external_info *ei,
1578 					      struct in_addr *old_id)
1579 {
1580 	struct stream *s;
1581 	struct lsa_header *lsah;
1582 	struct ospf_lsa *new;
1583 	struct in_addr id;
1584 	int length;
1585 
1586 	if (ei == NULL) {
1587 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1588 			zlog_debug(
1589 				"LSA[Type5]: External info is NULL, can't originate");
1590 		return NULL;
1591 	}
1592 
1593 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1594 		zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1595 
1596 	/* If old Link State ID is specified, refresh LSA with same ID. */
1597 	if (old_id)
1598 		id = *old_id;
1599 	/* Get Link State with unique ID. */
1600 	else {
1601 		id = ospf_lsa_unique_id(ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
1602 					&ei->p);
1603 		if (id.s_addr == 0xffffffff) {
1604 			/* Maybe Link State ID not available. */
1605 			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1606 				zlog_debug(
1607 					"LSA[Type5]: Link ID not available, can't originate");
1608 			return NULL;
1609 		}
1610 	}
1611 
1612 	/* Create new stream for LSA. */
1613 	s = stream_new(OSPF_MAX_LSA_SIZE);
1614 	lsah = (struct lsa_header *)STREAM_DATA(s);
1615 
1616 	/* Set LSA common header fields. */
1617 	lsa_header_set(s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA, id,
1618 		       ospf->router_id);
1619 
1620 	/* Set AS-external-LSA body fields. */
1621 	ospf_external_lsa_body_set(s, ei, ospf);
1622 
1623 	/* Set length. */
1624 	length = stream_get_endp(s);
1625 	lsah->length = htons(length);
1626 
1627 	/* Now, create OSPF LSA instance. */
1628 	new = ospf_lsa_new_and_data(length);
1629 	new->area = NULL;
1630 	SET_FLAG(new->flags,
1631 		 OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
1632 	new->vrf_id = ospf->vrf_id;
1633 
1634 	/* Copy LSA data to store, discard stream. */
1635 	memcpy(new->data, lsah, length);
1636 	stream_free(s);
1637 
1638 	return new;
1639 }
1640 
1641 /* As Type-7 */
ospf_install_flood_nssa(struct ospf * ospf,struct ospf_lsa * lsa,struct external_info * ei)1642 static void ospf_install_flood_nssa(struct ospf *ospf, struct ospf_lsa *lsa,
1643 				    struct external_info *ei)
1644 {
1645 	struct ospf_lsa *new;
1646 	struct as_external_lsa *extlsa;
1647 	struct ospf_area *area;
1648 	struct listnode *node, *nnode;
1649 
1650 	/* LSA may be a Type-5 originated via translation of a Type-7 LSA
1651 	 * which originated from an NSSA area. In which case it should not be
1652 	 * flooded back to NSSA areas.
1653 	 */
1654 	if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
1655 		return;
1656 
1657 	/* NSSA Originate or Refresh (If anyNSSA)
1658 
1659 	LSA is self-originated. And just installed as Type-5.
1660 	Additionally, install as Type-7 LSDB for every attached NSSA.
1661 
1662 	P-Bit controls which ABR performs translation to outside world; If
1663 	we are an ABR....do not set the P-bit, because we send the Type-5,
1664 	not as the ABR Translator, but as the ASBR owner within the AS!
1665 
1666 	If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set.  The
1667 	elected ABR Translator will see the P-bit, Translate, and re-flood.
1668 
1669 	Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1670 	Type-5's to non-NSSA Areas.  (it will also attempt a re-install) */
1671 
1672 	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
1673 		/* Don't install Type-7 LSA's into nonNSSA area */
1674 		if (area->external_routing != OSPF_AREA_NSSA)
1675 			continue;
1676 
1677 		/* make lsa duplicate, lock=1 */
1678 		new = ospf_lsa_dup(lsa);
1679 		new->area = area;
1680 		new->data->type = OSPF_AS_NSSA_LSA;
1681 
1682 		/* set P-bit if not ABR */
1683 		if (!IS_OSPF_ABR(ospf)) {
1684 			SET_FLAG(new->data->options, OSPF_OPTION_NP);
1685 
1686 			/* set non-zero FWD ADDR
1687 
1688 			draft-ietf-ospf-nssa-update-09.txt
1689 
1690 			if the network between the NSSA AS boundary router and
1691 			the
1692 			adjacent AS is advertised into OSPF as an internal OSPF
1693 			route,
1694 			the forwarding address should be the next op address as
1695 			is cu
1696 			currently done with type-5 LSAs.  If the intervening
1697 			network is
1698 			not adversited into OSPF as an internal OSPF route and
1699 			the
1700 			type-7 LSA's P-bit is set a forwarding address should be
1701 			selected from one of the router's active OSPF interface
1702 			addresses
1703 			which belong to the NSSA.  If no such addresses exist,
1704 			then
1705 			no type-7 LSA's with the P-bit set should originate from
1706 			this
1707 			router.   */
1708 
1709 			/* kevinm: not updating lsa anymore, just new */
1710 			extlsa = (struct as_external_lsa *)(new->data);
1711 
1712 			if (extlsa->e[0].fwd_addr.s_addr == 0)
1713 				extlsa->e[0].fwd_addr = ospf_get_nssa_ip(
1714 					area); /* this NSSA area in ifp */
1715 
1716 			if (extlsa->e[0].fwd_addr.s_addr == 0) {
1717 				if (IS_DEBUG_OSPF_NSSA)
1718 					zlog_debug(
1719 						"LSA[Type-7]: Could not build FWD-ADDR");
1720 				ospf_lsa_discard(new);
1721 				return;
1722 			}
1723 		}
1724 
1725 		/* install also as Type-7 */
1726 		ospf_lsa_install(ospf, NULL,
1727 				 new); /* Remove Old, Lock New = 2 */
1728 
1729 		/* will send each copy, lock=2+n */
1730 		ospf_flood_through_as(
1731 			ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
1732 	}
1733 }
1734 
ospf_lsa_translated_nssa_new(struct ospf * ospf,struct ospf_lsa * type7)1735 static struct ospf_lsa *ospf_lsa_translated_nssa_new(struct ospf *ospf,
1736 						     struct ospf_lsa *type7)
1737 {
1738 
1739 	struct ospf_lsa *new;
1740 	struct as_external_lsa *ext, *extnew;
1741 	struct external_info ei;
1742 
1743 	ext = (struct as_external_lsa *)(type7->data);
1744 
1745 	/* need external_info struct, fill in bare minimum */
1746 	ei.p.family = AF_INET;
1747 	ei.p.prefix = type7->data->id;
1748 	ei.p.prefixlen = ip_masklen(ext->mask);
1749 	ei.type = ZEBRA_ROUTE_OSPF;
1750 	ei.nexthop = ext->header.adv_router;
1751 	ei.route_map_set.metric = -1;
1752 	ei.route_map_set.metric_type = -1;
1753 	ei.tag = 0;
1754 	ei.instance = 0;
1755 
1756 	if ((new = ospf_external_lsa_new(ospf, &ei, &type7->data->id))
1757 	    == NULL) {
1758 		if (IS_DEBUG_OSPF_NSSA)
1759 			zlog_debug(
1760 				"ospf_nssa_translate_originate(): Could not originate Translated Type-5 for %s",
1761 				inet_ntoa(ei.p.prefix));
1762 		return NULL;
1763 	}
1764 
1765 	extnew = (struct as_external_lsa *)(new->data);
1766 
1767 	/* copy over Type-7 data to new */
1768 	extnew->e[0].tos = ext->e[0].tos;
1769 	extnew->e[0].route_tag = ext->e[0].route_tag;
1770 	extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
1771 	new->data->ls_seqnum = type7->data->ls_seqnum;
1772 
1773 	/* add translated flag, checksum and lock new lsa */
1774 	SET_FLAG(new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7  */
1775 	new = ospf_lsa_lock(new);
1776 
1777 	return new;
1778 }
1779 
1780 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
ospf_translated_nssa_originate(struct ospf * ospf,struct ospf_lsa * type7)1781 struct ospf_lsa *ospf_translated_nssa_originate(struct ospf *ospf,
1782 						struct ospf_lsa *type7)
1783 {
1784 	struct ospf_lsa *new;
1785 	struct as_external_lsa *extnew;
1786 
1787 	/* we cant use ospf_external_lsa_originate() as we need to set
1788 	 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1789 	 */
1790 
1791 	if ((new = ospf_lsa_translated_nssa_new(ospf, type7)) == NULL) {
1792 		if (IS_DEBUG_OSPF_NSSA)
1793 			zlog_debug(
1794 				"ospf_translated_nssa_originate(): Could not translate Type-7, Id %s, to Type-5",
1795 				inet_ntoa(type7->data->id));
1796 		return NULL;
1797 	}
1798 
1799 	extnew = (struct as_external_lsa *)new->data;
1800 
1801 	if ((new = ospf_lsa_install(ospf, NULL, new)) == NULL) {
1802 		flog_warn(
1803 			EC_OSPF_LSA_INSTALL_FAILURE,
1804 			"ospf_lsa_translated_nssa_originate(): Could not install LSA id %s",
1805 			inet_ntoa(type7->data->id));
1806 		return NULL;
1807 	}
1808 
1809 	if (IS_DEBUG_OSPF_NSSA) {
1810 		zlog_debug(
1811 			"ospf_translated_nssa_originate(): translated Type 7, installed:");
1812 		ospf_lsa_header_dump(new->data);
1813 		zlog_debug("   Network mask: %d", ip_masklen(extnew->mask));
1814 		zlog_debug("   Forward addr: %s",
1815 			   inet_ntoa(extnew->e[0].fwd_addr));
1816 	}
1817 
1818 	ospf->lsa_originate_count++;
1819 	ospf_flood_through_as(ospf, NULL, new);
1820 
1821 	return new;
1822 }
1823 
1824 /* Refresh Translated from NSSA AS-external-LSA. */
ospf_translated_nssa_refresh(struct ospf * ospf,struct ospf_lsa * type7,struct ospf_lsa * type5)1825 struct ospf_lsa *ospf_translated_nssa_refresh(struct ospf *ospf,
1826 					      struct ospf_lsa *type7,
1827 					      struct ospf_lsa *type5)
1828 {
1829 	struct ospf_lsa *new = NULL;
1830 
1831 	/* Sanity checks. */
1832 	assert(type7 || type5);
1833 	if (!(type7 || type5))
1834 		return NULL;
1835 	if (type7)
1836 		assert(type7->data);
1837 	if (type5)
1838 		assert(type5->data);
1839 	assert(ospf->anyNSSA);
1840 
1841 	/* get required data according to what has been given */
1842 	if (type7 && type5 == NULL) {
1843 		/* find the translated Type-5 for this Type-7 */
1844 		struct as_external_lsa *ext =
1845 			(struct as_external_lsa *)(type7->data);
1846 		struct prefix_ipv4 p = {
1847 			.prefix = type7->data->id,
1848 			.prefixlen = ip_masklen(ext->mask),
1849 			.family = AF_INET,
1850 		};
1851 
1852 		type5 = ospf_external_info_find_lsa(ospf, &p);
1853 	} else if (type5 && type7 == NULL) {
1854 		/* find the type-7 from which supplied type-5 was translated,
1855 		 * ie find first type-7 with same LSA Id.
1856 		 */
1857 		struct listnode *ln, *lnn;
1858 		struct route_node *rn;
1859 		struct ospf_lsa *lsa;
1860 		struct ospf_area *area;
1861 
1862 		for (ALL_LIST_ELEMENTS(ospf->areas, ln, lnn, area)) {
1863 			if (area->external_routing != OSPF_AREA_NSSA && !type7)
1864 				continue;
1865 
1866 			LSDB_LOOP (NSSA_LSDB(area), rn, lsa) {
1867 				if (lsa->data->id.s_addr
1868 				    == type5->data->id.s_addr) {
1869 					type7 = lsa;
1870 					break;
1871 				}
1872 			}
1873 		}
1874 	}
1875 
1876 	/* do we have type7? */
1877 	if (!type7) {
1878 		if (IS_DEBUG_OSPF_NSSA)
1879 			zlog_debug(
1880 				"ospf_translated_nssa_refresh(): no Type-7 found for Type-5 LSA Id %s",
1881 				inet_ntoa(type5->data->id));
1882 		return NULL;
1883 	}
1884 
1885 	/* do we have valid translated type5? */
1886 	if (type5 == NULL || !CHECK_FLAG(type5->flags, OSPF_LSA_LOCAL_XLT)) {
1887 		if (IS_DEBUG_OSPF_NSSA)
1888 			zlog_debug(
1889 				"ospf_translated_nssa_refresh(): No translated Type-5 found for Type-7 with Id %s",
1890 				inet_ntoa(type7->data->id));
1891 		return NULL;
1892 	}
1893 
1894 	/* Delete LSA from neighbor retransmit-list. */
1895 	ospf_ls_retransmit_delete_nbr_as(ospf, type5);
1896 
1897 	/* create new translated LSA */
1898 	if ((new = ospf_lsa_translated_nssa_new(ospf, type7)) == NULL) {
1899 		if (IS_DEBUG_OSPF_NSSA)
1900 			zlog_debug(
1901 				"ospf_translated_nssa_refresh(): Could not translate Type-7 for %s to Type-5",
1902 				inet_ntoa(type7->data->id));
1903 		return NULL;
1904 	}
1905 
1906 	if (!(new = ospf_lsa_install(ospf, NULL, new))) {
1907 		flog_warn(
1908 			EC_OSPF_LSA_INSTALL_FAILURE,
1909 			"ospf_translated_nssa_refresh(): Could not install translated LSA, Id %s",
1910 			inet_ntoa(type7->data->id));
1911 		return NULL;
1912 	}
1913 
1914 	/* Flood LSA through area. */
1915 	ospf_flood_through_as(ospf, NULL, new);
1916 
1917 	return new;
1918 }
1919 
is_prefix_default(struct prefix_ipv4 * p)1920 int is_prefix_default(struct prefix_ipv4 *p)
1921 {
1922 	struct prefix_ipv4 q;
1923 
1924 	q.family = AF_INET;
1925 	q.prefix.s_addr = INADDR_ANY;
1926 	q.prefixlen = 0;
1927 
1928 	return prefix_same((struct prefix *)p, (struct prefix *)&q);
1929 }
1930 
1931 /* Originate an AS-external-LSA, install and flood. */
ospf_external_lsa_originate(struct ospf * ospf,struct external_info * ei)1932 struct ospf_lsa *ospf_external_lsa_originate(struct ospf *ospf,
1933 					     struct external_info *ei)
1934 {
1935 	struct ospf_lsa *new;
1936 
1937 	/* Added for NSSA project....
1938 
1939 	     External LSAs are originated in ASBRs as usual, but for NSSA
1940 	   systems.
1941 	   there is the global Type-5 LSDB and a Type-7 LSDB installed for
1942 	   every area.  The Type-7's are flooded to every IR and every ABR; We
1943 	   install the Type-5 LSDB so that the normal "refresh" code operates
1944 	   as usual, and flag them as not used during ASE calculations.  The
1945 	   Type-7 LSDB is used for calculations.  Each Type-7 has a Forwarding
1946 	   Address of non-zero.
1947 
1948 	   If an ABR is the elected NSSA translator, following SPF and during
1949 	   the ABR task it will translate all the scanned Type-7's, with P-bit
1950 	   ON and not-self generated, and translate to Type-5's throughout the
1951 	   non-NSSA/STUB AS.
1952 
1953 	   A difference in operation depends whether this ASBR is an ABR
1954 	   or not.  If not an ABR, the P-bit is ON, to indicate that any
1955 	   elected NSSA-ABR can perform its translation.
1956 
1957 	   If an ABR, the P-bit is OFF;  No ABR will perform translation and
1958 	   this ASBR will flood the Type-5 LSA as usual.
1959 
1960 	   For the case where this ASBR is not an ABR, the ASE calculations
1961 	   are based on the Type-5 LSDB;  The Type-7 LSDB exists just to
1962 	   demonstrate to the user that there are LSA's that belong to any
1963 	   attached NSSA.
1964 
1965 	   Finally, it just so happens that when the ABR is translating every
1966 	   Type-7 into Type-5, it installs it into the Type-5 LSDB as an
1967 	   approved Type-5 (translated from Type-7);  at the end of translation
1968 	   if any Translated Type-5's remain unapproved, then they must be
1969 	   flushed from the AS.
1970 
1971 	   */
1972 
1973 	if (ospf->router_id.s_addr == INADDR_ANY) {
1974 		if (IS_DEBUG_OSPF_EVENT)
1975 			zlog_debug(
1976 				"LSA[Type5:%pI4]: deferring AS-external-LSA origination, router ID is zero",
1977 				&ei->p.prefix);
1978 		return NULL;
1979 	}
1980 
1981 	/* Check the AS-external-LSA should be originated. */
1982 	if (!ospf_redistribute_check(ospf, ei, NULL))
1983 		return NULL;
1984 
1985 	/* Create new AS-external-LSA instance. */
1986 	if ((new = ospf_external_lsa_new(ospf, ei, NULL)) == NULL) {
1987 		if (IS_DEBUG_OSPF_EVENT)
1988 			zlog_debug(
1989 				"LSA[Type5:%s]: Could not originate AS-external-LSA",
1990 				inet_ntoa(ei->p.prefix));
1991 		return NULL;
1992 	}
1993 
1994 	/* Install newly created LSA into Type-5 LSDB, lock = 1. */
1995 	ospf_lsa_install(ospf, NULL, new);
1996 
1997 	/* Update LSA origination count. */
1998 	ospf->lsa_originate_count++;
1999 
2000 	/* Flooding new LSA. only to AS (non-NSSA/STUB) */
2001 	ospf_flood_through_as(ospf, NULL, new);
2002 
2003 	/* If there is any attached NSSA, do special handling */
2004 	if (ospf->anyNSSA &&
2005 	    /* stay away from translated LSAs! */
2006 	    !(CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)))
2007 		ospf_install_flood_nssa(
2008 			ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
2009 
2010 	/* Debug logging. */
2011 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2012 		zlog_debug("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2013 			   new->data->type, inet_ntoa(new->data->id),
2014 			   (void *)new);
2015 		ospf_lsa_header_dump(new->data);
2016 	}
2017 
2018 	return new;
2019 }
2020 
ospf_default_external_info(struct ospf * ospf)2021 static struct external_info *ospf_default_external_info(struct ospf *ospf)
2022 {
2023 	int type;
2024 	struct prefix_ipv4 p;
2025 	struct external_info *default_ei;
2026 	int ret = 0;
2027 
2028 	p.family = AF_INET;
2029 	p.prefix.s_addr = 0;
2030 	p.prefixlen = 0;
2031 
2032 	default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
2033 					       ospf->instance, &p);
2034 	if (!default_ei)
2035 		return NULL;
2036 
2037 	/* First, lookup redistributed default route. */
2038 	for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
2039 		struct list *ext_list;
2040 
2041 		if (type == ZEBRA_ROUTE_OSPF)
2042 			continue;
2043 
2044 		ext_list = ospf->external[type];
2045 		if (!ext_list)
2046 			continue;
2047 
2048 		ret = ospf_external_default_routemap_apply_walk(ospf, ext_list,
2049 								default_ei);
2050 		if (ret)
2051 			return default_ei;
2052 	}
2053 
2054 	return NULL;
2055 }
2056 
ospf_external_lsa_rid_change(struct ospf * ospf)2057 void ospf_external_lsa_rid_change(struct ospf *ospf)
2058 {
2059 	struct external_info *ei;
2060 	int type;
2061 
2062 	for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
2063 		struct route_node *rn;
2064 		struct route_table *rt;
2065 		struct list *ext_list;
2066 		struct listnode *node;
2067 		struct ospf_external *ext;
2068 
2069 		ext_list = ospf->external[type];
2070 		if (!ext_list)
2071 			continue;
2072 
2073 		for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
2074 			/* Originate As-external-LSA from all type of
2075 			 * distribute source.
2076 			 */
2077 			rt = ext->external_info;
2078 			if (!rt)
2079 				continue;
2080 
2081 			for (rn = route_top(rt); rn; rn = route_next(rn)) {
2082 				ei = rn->info;
2083 
2084 				if (!ei)
2085 					continue;
2086 
2087 				if (is_prefix_default(
2088 						(struct prefix_ipv4 *)&ei->p))
2089 					continue;
2090 
2091 				if (!ospf_external_lsa_originate(ospf, ei))
2092 					flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
2093 						"LSA: AS-external-LSA was not originated.");
2094 			}
2095 		}
2096 	}
2097 
2098 	ei = ospf_default_external_info(ospf);
2099 	if (ei && !ospf_external_lsa_originate(ospf, ei)) {
2100 		flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
2101 			"LSA: AS-external-LSA for default route was not originated.");
2102 	}
2103 }
2104 
2105 /* Flush any NSSA LSAs for given prefix */
ospf_nssa_lsa_flush(struct ospf * ospf,struct prefix_ipv4 * p)2106 void ospf_nssa_lsa_flush(struct ospf *ospf, struct prefix_ipv4 *p)
2107 {
2108 	struct listnode *node, *nnode;
2109 	struct ospf_lsa *lsa = NULL;
2110 	struct ospf_area *area;
2111 
2112 	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
2113 		if (area->external_routing == OSPF_AREA_NSSA) {
2114 			lsa = ospf_lsa_lookup(ospf, area, OSPF_AS_NSSA_LSA,
2115 					      p->prefix, ospf->router_id);
2116 			if (!lsa) {
2117 				if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2118 					zlog_debug(
2119 						"LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2120 						inet_ntoa(p->prefix),
2121 						p->prefixlen);
2122 				continue;
2123 			}
2124 			ospf_ls_retransmit_delete_nbr_area(area, lsa);
2125 			if (!IS_LSA_MAXAGE(lsa)) {
2126 				ospf_refresher_unregister_lsa(ospf, lsa);
2127 				ospf_lsa_flush_area(lsa, area);
2128 			}
2129 		}
2130 	}
2131 }
2132 
2133 /* Flush an AS-external-LSA from LSDB and routing domain. */
ospf_external_lsa_flush(struct ospf * ospf,uint8_t type,struct prefix_ipv4 * p,ifindex_t ifindex)2134 void ospf_external_lsa_flush(struct ospf *ospf, uint8_t type,
2135 			     struct prefix_ipv4 *p,
2136 			     ifindex_t ifindex /*, struct in_addr nexthop */)
2137 {
2138 	struct ospf_lsa *lsa;
2139 
2140 	if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2141 		zlog_debug("LSA: Flushing AS-external-LSA %s/%d",
2142 			   inet_ntoa(p->prefix), p->prefixlen);
2143 
2144 	/* First lookup LSA from LSDB. */
2145 	if (!(lsa = ospf_external_info_find_lsa(ospf, p))) {
2146 		if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2147 			zlog_debug(
2148 				"LSA: There is no such AS-external-LSA %s/%d in LSDB",
2149 				inet_ntoa(p->prefix), p->prefixlen);
2150 		return;
2151 	}
2152 
2153 	/* If LSA is selforiginated, not a translated LSA, and there is
2154 	 * NSSA area, flush Type-7 LSA's at first.
2155 	 */
2156 	if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
2157 	    && !(CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT)))
2158 		ospf_nssa_lsa_flush(ospf, p);
2159 
2160 	/* Sweep LSA from Link State Retransmit List. */
2161 	ospf_ls_retransmit_delete_nbr_as(ospf, lsa);
2162 
2163 /* There must be no self-originated LSA in rtrs_external. */
2164 #if 0
2165   /* Remove External route from Zebra. */
2166   ospf_zebra_delete ((struct prefix_ipv4 *) p, &nexthop);
2167 #endif
2168 
2169 	if (!IS_LSA_MAXAGE(lsa)) {
2170 		/* Unregister LSA from Refresh queue. */
2171 		ospf_refresher_unregister_lsa(ospf, lsa);
2172 
2173 		/* Flush AS-external-LSA through AS. */
2174 		ospf_lsa_flush_as(ospf, lsa);
2175 	}
2176 
2177 	if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2178 		zlog_debug("ospf_external_lsa_flush(): stop");
2179 }
2180 
ospf_external_lsa_refresh_default(struct ospf * ospf)2181 void ospf_external_lsa_refresh_default(struct ospf *ospf)
2182 {
2183 	struct prefix_ipv4 p;
2184 	struct external_info *ei;
2185 	struct ospf_lsa *lsa;
2186 
2187 	p.family = AF_INET;
2188 	p.prefixlen = 0;
2189 	p.prefix.s_addr = INADDR_ANY;
2190 
2191 	ei = ospf_default_external_info(ospf);
2192 	lsa = ospf_external_info_find_lsa(ospf, &p);
2193 
2194 	if (ei && lsa) {
2195 		if (IS_DEBUG_OSPF_EVENT)
2196 			zlog_debug("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2197 				   (void *)lsa);
2198 		ospf_external_lsa_refresh(ospf, lsa, ei, LSA_REFRESH_FORCE);
2199 	} else if (ei && !lsa) {
2200 		if (IS_DEBUG_OSPF_EVENT)
2201 			zlog_debug(
2202 				"LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2203 		ospf_external_lsa_originate(ospf, ei);
2204 	} else if (lsa) {
2205 		if (IS_DEBUG_OSPF_EVENT)
2206 			zlog_debug("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2207 		ospf_external_lsa_flush(ospf, DEFAULT_ROUTE, &p, 0);
2208 	}
2209 }
2210 
ospf_external_lsa_refresh_type(struct ospf * ospf,uint8_t type,unsigned short instance,int force)2211 void ospf_external_lsa_refresh_type(struct ospf *ospf, uint8_t type,
2212 				    unsigned short instance, int force)
2213 {
2214 	struct route_node *rn;
2215 	struct external_info *ei;
2216 	struct ospf_external *ext;
2217 
2218 	if (type == DEFAULT_ROUTE)
2219 		return;
2220 
2221 	ext = ospf_external_lookup(ospf, type, instance);
2222 
2223 	if (ext && EXTERNAL_INFO(ext)) {
2224 		/* Refresh each redistributed AS-external-LSAs. */
2225 		for (rn = route_top(EXTERNAL_INFO(ext)); rn;
2226 		     rn = route_next(rn)) {
2227 			ei = rn->info;
2228 			if (ei) {
2229 				if (!is_prefix_default(&ei->p)) {
2230 					struct ospf_lsa *lsa;
2231 
2232 					lsa = ospf_external_info_find_lsa(
2233 						ospf, &ei->p);
2234 					if (lsa)
2235 						ospf_external_lsa_refresh(
2236 							ospf, lsa, ei, force);
2237 					else
2238 						ospf_external_lsa_originate(
2239 							ospf, ei);
2240 				}
2241 			}
2242 		}
2243 	}
2244 }
2245 
2246 /* Refresh AS-external-LSA. */
ospf_external_lsa_refresh(struct ospf * ospf,struct ospf_lsa * lsa,struct external_info * ei,int force)2247 struct ospf_lsa *ospf_external_lsa_refresh(struct ospf *ospf,
2248 					   struct ospf_lsa *lsa,
2249 					   struct external_info *ei, int force)
2250 {
2251 	struct ospf_lsa *new;
2252 	int changed;
2253 
2254 	/* Check the AS-external-LSA should be originated. */
2255 	if (!ospf_redistribute_check(ospf, ei, &changed)) {
2256 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2257 			zlog_debug(
2258 				"LSA[Type%d:%s]: Could not be refreshed, redist check fail",
2259 				lsa->data->type, inet_ntoa(lsa->data->id));
2260 		ospf_external_lsa_flush(ospf, ei->type, &ei->p,
2261 					ei->ifindex /*, ei->nexthop */);
2262 		return NULL;
2263 	}
2264 
2265 	if (!changed && !force) {
2266 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2267 			zlog_debug(
2268 				"LSA[Type%d:%s]: Not refreshed, not changed/forced",
2269 				lsa->data->type, inet_ntoa(lsa->data->id));
2270 		return NULL;
2271 	}
2272 
2273 	/* Delete LSA from neighbor retransmit-list. */
2274 	ospf_ls_retransmit_delete_nbr_as(ospf, lsa);
2275 
2276 	/* Unregister AS-external-LSA from refresh-list. */
2277 	ospf_refresher_unregister_lsa(ospf, lsa);
2278 
2279 	new = ospf_external_lsa_new(ospf, ei, &lsa->data->id);
2280 
2281 	if (new == NULL) {
2282 		if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2283 			zlog_debug("LSA[Type%d:%s]: Could not be refreshed",
2284 				   lsa->data->type, inet_ntoa(lsa->data->id));
2285 		return NULL;
2286 	}
2287 
2288 	new->data->ls_seqnum = lsa_seqnum_increment(lsa);
2289 
2290 	ospf_lsa_install(ospf, NULL, new); /* As type-5. */
2291 
2292 	/* Flood LSA through AS. */
2293 	ospf_flood_through_as(ospf, NULL, new);
2294 
2295 	/* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2296 	if (ospf->anyNSSA && !(CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)))
2297 		ospf_install_flood_nssa(ospf, new,
2298 					ei); /* Install/Flood per new rules */
2299 
2300 	/* Register self-originated LSA to refresh queue.
2301 	 * Translated LSAs should not be registered, but refreshed upon
2302 	 * refresh of the Type-7
2303 	 */
2304 	if (!CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT))
2305 		ospf_refresher_register_lsa(ospf, new);
2306 
2307 	/* Debug logging. */
2308 	if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2309 		zlog_debug("LSA[Type%d:%s]: AS-external-LSA refresh",
2310 			   new->data->type, inet_ntoa(new->data->id));
2311 		ospf_lsa_header_dump(new->data);
2312 	}
2313 
2314 	return new;
2315 }
2316 
2317 
2318 /* LSA installation functions. */
2319 
2320 /* Install router-LSA to an area. */
2321 static struct ospf_lsa *
ospf_router_lsa_install(struct ospf * ospf,struct ospf_lsa * new,int rt_recalc)2322 ospf_router_lsa_install(struct ospf *ospf, struct ospf_lsa *new, int rt_recalc)
2323 {
2324 	struct ospf_area *area = new->area;
2325 
2326 	/* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2327 	   The entire routing table must be recalculated, starting with
2328 	   the shortest path calculations for each area (not just the
2329 	   area whose link-state database has changed).
2330 	*/
2331 
2332 	if (IS_LSA_SELF(new)) {
2333 
2334 		/* Only install LSA if it is originated/refreshed by us.
2335 		 * If LSA was received by flooding, the RECEIVED flag is set so
2336 		 * do
2337 		 * not link the LSA */
2338 		if (CHECK_FLAG(new->flags, OSPF_LSA_RECEIVED))
2339 			return new; /* ignore stale LSA */
2340 
2341 		/* Set self-originated router-LSA. */
2342 		ospf_lsa_unlock(&area->router_lsa_self);
2343 		area->router_lsa_self = ospf_lsa_lock(new);
2344 
2345 		ospf_refresher_register_lsa(ospf, new);
2346 	}
2347 	if (rt_recalc)
2348 		ospf_spf_calculate_schedule(ospf, SPF_FLAG_ROUTER_LSA_INSTALL);
2349 	return new;
2350 }
2351 
2352 #define OSPF_INTERFACE_TIMER_ON(T, F, V)                                       \
2353 	if (!(T))                                                              \
2354 	(T) = thread_add_timer(master, (F), oi, (V))
2355 
2356 /* Install network-LSA to an area. */
ospf_network_lsa_install(struct ospf * ospf,struct ospf_interface * oi,struct ospf_lsa * new,int rt_recalc)2357 static struct ospf_lsa *ospf_network_lsa_install(struct ospf *ospf,
2358 						 struct ospf_interface *oi,
2359 						 struct ospf_lsa *new,
2360 						 int rt_recalc)
2361 {
2362 
2363 	/* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2364 	   The entire routing table must be recalculated, starting with
2365 	   the shortest path calculations for each area (not just the
2366 	   area whose link-state database has changed).
2367 	*/
2368 	if (IS_LSA_SELF(new)) {
2369 		/* We supposed that when LSA is originated by us, we pass the
2370 		   int
2371 		   for which it was originated. If LSA was received by flooding,
2372 		   the RECEIVED flag is set, so we do not link the LSA to the
2373 		   int. */
2374 		if (CHECK_FLAG(new->flags, OSPF_LSA_RECEIVED))
2375 			return new; /* ignore stale LSA */
2376 
2377 		ospf_lsa_unlock(&oi->network_lsa_self);
2378 		oi->network_lsa_self = ospf_lsa_lock(new);
2379 		ospf_refresher_register_lsa(ospf, new);
2380 	}
2381 	if (rt_recalc)
2382 		ospf_spf_calculate_schedule(ospf, SPF_FLAG_NETWORK_LSA_INSTALL);
2383 
2384 	return new;
2385 }
2386 
2387 /* Install summary-LSA to an area. */
2388 static struct ospf_lsa *
ospf_summary_lsa_install(struct ospf * ospf,struct ospf_lsa * new,int rt_recalc)2389 ospf_summary_lsa_install(struct ospf *ospf, struct ospf_lsa *new, int rt_recalc)
2390 {
2391 	if (rt_recalc && !IS_LSA_SELF(new)) {
2392 /* RFC 2328 Section 13.2 Summary-LSAs
2393    The best route to the destination described by the summary-
2394    LSA must be recalculated (see Section 16.5).  If this
2395    destination is an AS boundary router, it may also be
2396    necessary to re-examine all the AS-external-LSAs.
2397 */
2398 
2399 #if 0
2400       /* This doesn't exist yet... */
2401       ospf_summary_incremental_update(new); */
2402 #else /* #if 0 */
2403 		ospf_spf_calculate_schedule(ospf, SPF_FLAG_SUMMARY_LSA_INSTALL);
2404 #endif /* #if 0 */
2405 	}
2406 
2407 	if (IS_LSA_SELF(new))
2408 		ospf_refresher_register_lsa(ospf, new);
2409 
2410 	return new;
2411 }
2412 
2413 /* Install ASBR-summary-LSA to an area. */
ospf_summary_asbr_lsa_install(struct ospf * ospf,struct ospf_lsa * new,int rt_recalc)2414 static struct ospf_lsa *ospf_summary_asbr_lsa_install(struct ospf *ospf,
2415 						      struct ospf_lsa *new,
2416 						      int rt_recalc)
2417 {
2418 	if (rt_recalc && !IS_LSA_SELF(new)) {
2419 /* RFC 2328 Section 13.2 Summary-LSAs
2420    The best route to the destination described by the summary-
2421    LSA must be recalculated (see Section 16.5).  If this
2422    destination is an AS boundary router, it may also be
2423    necessary to re-examine all the AS-external-LSAs.
2424 */
2425 #if 0
2426       /* These don't exist yet... */
2427       ospf_summary_incremental_update(new);
2428       /* Isn't this done by the above call?
2429 	 - RFC 2328 Section 16.5 implies it should be */
2430       /* ospf_ase_calculate_schedule(); */
2431 #else  /* #if 0 */
2432 		ospf_spf_calculate_schedule(ospf,
2433 					    SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL);
2434 #endif /* #if 0 */
2435 	}
2436 
2437 	/* register LSA to refresh-list. */
2438 	if (IS_LSA_SELF(new))
2439 		ospf_refresher_register_lsa(ospf, new);
2440 
2441 	return new;
2442 }
2443 
2444 /* Install AS-external-LSA. */
ospf_external_lsa_install(struct ospf * ospf,struct ospf_lsa * new,int rt_recalc)2445 static struct ospf_lsa *ospf_external_lsa_install(struct ospf *ospf,
2446 						  struct ospf_lsa *new,
2447 						  int rt_recalc)
2448 {
2449 	ospf_ase_register_external_lsa(new, ospf);
2450 	/* If LSA is not self-originated, calculate an external route. */
2451 	if (rt_recalc) {
2452 		/* RFC 2328 Section 13.2 AS-external-LSAs
2453 		      The best route to the destination described by the AS-
2454 		      external-LSA must be recalculated (see Section 16.6).
2455 		*/
2456 
2457 		if (!IS_LSA_SELF(new))
2458 			ospf_ase_incremental_update(ospf, new);
2459 	}
2460 
2461 	if (new->data->type == OSPF_AS_NSSA_LSA) {
2462 		/* There is no point to register selforiginate Type-7 LSA for
2463 		 * refreshing. We rely on refreshing Type-5 LSA's
2464 		 */
2465 		if (IS_LSA_SELF(new))
2466 			return new;
2467 		else {
2468 			/* Try refresh type-5 translated LSA for this LSA, if
2469 			 * one exists.
2470 			 * New translations will be taken care of by the
2471 			 * abr_task.
2472 			 */
2473 			ospf_translated_nssa_refresh(ospf, new, NULL);
2474 			ospf_schedule_abr_task(ospf);
2475 		}
2476 	}
2477 
2478 	/* Register self-originated LSA to refresh queue.
2479 	 * Leave Translated LSAs alone if NSSA is enabled
2480 	 */
2481 	if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT))
2482 		ospf_refresher_register_lsa(ospf, new);
2483 
2484 	return new;
2485 }
2486 
ospf_discard_from_db(struct ospf * ospf,struct ospf_lsdb * lsdb,struct ospf_lsa * lsa)2487 void ospf_discard_from_db(struct ospf *ospf, struct ospf_lsdb *lsdb,
2488 			  struct ospf_lsa *lsa)
2489 {
2490 	struct ospf_lsa *old;
2491 
2492 	if (!lsdb)
2493 		return;
2494 
2495 	old = ospf_lsdb_lookup(lsdb, lsa);
2496 
2497 	if (!old)
2498 		return;
2499 
2500 	if (old->refresh_list >= 0)
2501 		ospf_refresher_unregister_lsa(ospf, old);
2502 
2503 	switch (old->data->type) {
2504 	case OSPF_AS_EXTERNAL_LSA:
2505 		ospf_ase_unregister_external_lsa(old, ospf);
2506 		ospf_ls_retransmit_delete_nbr_as(ospf, old);
2507 		break;
2508 	case OSPF_OPAQUE_AS_LSA:
2509 		ospf_ls_retransmit_delete_nbr_as(ospf, old);
2510 		break;
2511 	case OSPF_AS_NSSA_LSA:
2512 		ospf_ls_retransmit_delete_nbr_area(old->area, old);
2513 		ospf_ase_unregister_external_lsa(old, ospf);
2514 		break;
2515 	default:
2516 		ospf_ls_retransmit_delete_nbr_area(old->area, old);
2517 		break;
2518 	}
2519 
2520 	ospf_lsa_maxage_delete(ospf, old);
2521 	ospf_lsa_discard(old);
2522 }
2523 
ospf_lsa_install(struct ospf * ospf,struct ospf_interface * oi,struct ospf_lsa * lsa)2524 struct ospf_lsa *ospf_lsa_install(struct ospf *ospf, struct ospf_interface *oi,
2525 				  struct ospf_lsa *lsa)
2526 {
2527 	struct ospf_lsa *new = NULL;
2528 	struct ospf_lsa *old = NULL;
2529 	struct ospf_lsdb *lsdb = NULL;
2530 	int rt_recalc;
2531 
2532 	/* Set LSDB. */
2533 	switch (lsa->data->type) {
2534 	/* kevinm */
2535 	case OSPF_AS_NSSA_LSA:
2536 		if (lsa->area)
2537 			lsdb = lsa->area->lsdb;
2538 		else
2539 			lsdb = ospf->lsdb;
2540 		break;
2541 	case OSPF_AS_EXTERNAL_LSA:
2542 	case OSPF_OPAQUE_AS_LSA:
2543 		lsdb = ospf->lsdb;
2544 		break;
2545 	default:
2546 		if (lsa->area)
2547 			lsdb = lsa->area->lsdb;
2548 		break;
2549 	}
2550 
2551 	assert(lsdb);
2552 
2553 	/*  RFC 2328 13.2.  Installing LSAs in the database
2554 
2555 	      Installing a new LSA in the database, either as the result of
2556 	      flooding or a newly self-originated LSA, may cause the OSPF
2557 	      routing table structure to be recalculated.  The contents of the
2558 	      new LSA should be compared to the old instance, if present.  If
2559 	      there is no difference, there is no need to recalculate the
2560 	      routing table. When comparing an LSA to its previous instance,
2561 	      the following are all considered to be differences in contents:
2562 
2563 		  o   The LSA's Options field has changed.
2564 
2565 		  o   One of the LSA instances has LS age set to MaxAge, and
2566 		      the other does not.
2567 
2568 		  o   The length field in the LSA header has changed.
2569 
2570 		  o   The body of the LSA (i.e., anything outside the 20-byte
2571 		      LSA header) has changed. Note that this excludes changes
2572 		      in LS Sequence Number and LS Checksum.
2573 
2574 	*/
2575 	/* Look up old LSA and determine if any SPF calculation or incremental
2576 	   update is needed */
2577 	old = ospf_lsdb_lookup(lsdb, lsa);
2578 
2579 	/* Do comparision and record if recalc needed. */
2580 	rt_recalc = 0;
2581 	if (old == NULL || ospf_lsa_different(old, lsa))
2582 		rt_recalc = 1;
2583 
2584 	/*
2585 	   Sequence number check (Section 14.1 of rfc 2328)
2586 	   "Premature aging is used when it is time for a self-originated
2587 	    LSA's sequence number field to wrap.  At this point, the current
2588 	    LSA instance (having LS sequence number MaxSequenceNumber) must
2589 	    be prematurely aged and flushed from the routing domain before a
2590 	    new instance with sequence number equal to InitialSequenceNumber
2591 	    can be originated. "
2592 	 */
2593 
2594 	if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER) {
2595 		if (ospf_lsa_is_self_originated(ospf, lsa)) {
2596 			lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
2597 
2598 			if (!IS_LSA_MAXAGE(lsa))
2599 				lsa->flags |= OSPF_LSA_PREMATURE_AGE;
2600 			lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
2601 
2602 			if (IS_DEBUG_OSPF(lsa, LSA_REFRESH)) {
2603 				zlog_debug(
2604 					"ospf_lsa_install() Premature Aging lsa 0x%p, seqnum 0x%x",
2605 					(void *)lsa,
2606 					ntohl(lsa->data->ls_seqnum));
2607 				ospf_lsa_header_dump(lsa->data);
2608 			}
2609 		} else {
2610 			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2611 				zlog_debug(
2612 					"ospf_lsa_install() got an lsa with seq 0x80000000 that was not self originated. Ignoring\n");
2613 				ospf_lsa_header_dump(lsa->data);
2614 			}
2615 			return old;
2616 		}
2617 	}
2618 
2619 	/* discard old LSA from LSDB */
2620 	if (old != NULL)
2621 		ospf_discard_from_db(ospf, lsdb, lsa);
2622 
2623 	/* Calculate Checksum if self-originated?. */
2624 	if (IS_LSA_SELF(lsa))
2625 		ospf_lsa_checksum(lsa->data);
2626 
2627 	/* Insert LSA to LSDB. */
2628 	ospf_lsdb_add(lsdb, lsa);
2629 	lsa->lsdb = lsdb;
2630 
2631 	/* Do LSA specific installation process. */
2632 	switch (lsa->data->type) {
2633 	case OSPF_ROUTER_LSA:
2634 		new = ospf_router_lsa_install(ospf, lsa, rt_recalc);
2635 		break;
2636 	case OSPF_NETWORK_LSA:
2637 		assert(oi);
2638 		new = ospf_network_lsa_install(ospf, oi, lsa, rt_recalc);
2639 		break;
2640 	case OSPF_SUMMARY_LSA:
2641 		new = ospf_summary_lsa_install(ospf, lsa, rt_recalc);
2642 		break;
2643 	case OSPF_ASBR_SUMMARY_LSA:
2644 		new = ospf_summary_asbr_lsa_install(ospf, lsa, rt_recalc);
2645 		break;
2646 	case OSPF_AS_EXTERNAL_LSA:
2647 		new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
2648 		break;
2649 	case OSPF_OPAQUE_LINK_LSA:
2650 		if (IS_LSA_SELF(lsa))
2651 			lsa->oi = oi; /* Specify outgoing ospf-interface for
2652 					 this LSA. */
2653 		else {
2654 			/* Incoming "oi" for this LSA has set at LSUpd
2655 			 * reception. */
2656 		}
2657 	/* Fallthrough */
2658 	case OSPF_OPAQUE_AREA_LSA:
2659 	case OSPF_OPAQUE_AS_LSA:
2660 		new = ospf_opaque_lsa_install(lsa, rt_recalc);
2661 		break;
2662 	case OSPF_AS_NSSA_LSA:
2663 		new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
2664 	default: /* type-6,8,9....nothing special */
2665 		break;
2666 	}
2667 
2668 	if (new == NULL)
2669 		return new; /* Installation failed, cannot proceed further --
2670 			       endo. */
2671 
2672 	/* Debug logs. */
2673 	if (IS_DEBUG_OSPF(lsa, LSA_INSTALL)) {
2674 		char area_str[INET_ADDRSTRLEN];
2675 
2676 		switch (lsa->data->type) {
2677 		case OSPF_AS_EXTERNAL_LSA:
2678 		case OSPF_OPAQUE_AS_LSA:
2679 		case OSPF_AS_NSSA_LSA:
2680 			zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2681 				   lookup_msg(ospf_lsa_type_msg,
2682 					      new->data->type, NULL));
2683 			break;
2684 		default:
2685 			strlcpy(area_str, inet_ntoa(new->area->area_id),
2686 				sizeof(area_str));
2687 			zlog_debug("LSA[%s]: Install %s to Area %s",
2688 				   dump_lsa_key(new),
2689 				   lookup_msg(ospf_lsa_type_msg,
2690 					      new->data->type, NULL),
2691 				   area_str);
2692 			break;
2693 		}
2694 	}
2695 
2696 	/*
2697 	   If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2698 	   (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2699 	 */
2700 	if (IS_LSA_MAXAGE(new)) {
2701 		if (IS_DEBUG_OSPF(lsa, LSA_INSTALL))
2702 			zlog_debug("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2703 				   new->data->type, inet_ntoa(new->data->id),
2704 				   (void *)lsa);
2705 		ospf_lsa_maxage(ospf, lsa);
2706 	}
2707 
2708 	return new;
2709 }
2710 
2711 
ospf_check_nbr_status(struct ospf * ospf)2712 int ospf_check_nbr_status(struct ospf *ospf)
2713 {
2714 	struct listnode *node, *nnode;
2715 	struct ospf_interface *oi;
2716 
2717 	for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
2718 		struct route_node *rn;
2719 		struct ospf_neighbor *nbr;
2720 
2721 		if (ospf_if_is_enable(oi))
2722 			for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
2723 				if ((nbr = rn->info) != NULL)
2724 					if (nbr->state == NSM_Exchange
2725 					    || nbr->state == NSM_Loading) {
2726 						route_unlock_node(rn);
2727 						return 0;
2728 					}
2729 	}
2730 
2731 	return 1;
2732 }
2733 
2734 
ospf_maxage_lsa_remover(struct thread * thread)2735 static int ospf_maxage_lsa_remover(struct thread *thread)
2736 {
2737 	struct ospf *ospf = THREAD_ARG(thread);
2738 	struct ospf_lsa *lsa;
2739 	struct route_node *rn;
2740 	int reschedule = 0;
2741 
2742 	ospf->t_maxage = NULL;
2743 
2744 	if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2745 		zlog_debug("LSA[MaxAge]: remover Start");
2746 
2747 	reschedule = !ospf_check_nbr_status(ospf);
2748 
2749 	if (!reschedule)
2750 		for (rn = route_top(ospf->maxage_lsa); rn;
2751 		     rn = route_next(rn)) {
2752 			if ((lsa = rn->info) == NULL) {
2753 				continue;
2754 			}
2755 
2756 			/* There is at least one neighbor from which we still
2757 			 * await an ack
2758 			 * for that LSA, so we are not allowed to remove it from
2759 			 * our lsdb yet
2760 			 * as per RFC 2328 section 14 para 4 a) */
2761 			if (lsa->retransmit_counter > 0) {
2762 				reschedule = 1;
2763 				continue;
2764 			}
2765 
2766 			/* TODO: maybe convert this function to a work-queue */
2767 			if (thread_should_yield(thread)) {
2768 				OSPF_TIMER_ON(ospf->t_maxage,
2769 					      ospf_maxage_lsa_remover, 0);
2770 				route_unlock_node(
2771 					rn); /* route_top/route_next */
2772 				return 0;
2773 			}
2774 
2775 			/* Remove LSA from the LSDB */
2776 			if (IS_LSA_SELF(lsa))
2777 				if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2778 					zlog_debug(
2779 						"LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2780 						lsa->data->type,
2781 						inet_ntoa(lsa->data->id),
2782 						(unsigned long)lsa);
2783 
2784 			if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2785 				zlog_debug(
2786 					"LSA[Type%d:%s]: MaxAge LSA removed from list",
2787 					lsa->data->type,
2788 					inet_ntoa(lsa->data->id));
2789 
2790 			if (CHECK_FLAG(lsa->flags, OSPF_LSA_PREMATURE_AGE)) {
2791 				if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2792 					zlog_debug(
2793 						"originating new lsa for lsa 0x%p",
2794 						(void *)lsa);
2795 				ospf_lsa_refresh(ospf, lsa);
2796 			}
2797 
2798 			/* Remove from lsdb. */
2799 			if (lsa->lsdb) {
2800 				ospf_discard_from_db(ospf, lsa->lsdb, lsa);
2801 				ospf_lsdb_delete(lsa->lsdb, lsa);
2802 			} else {
2803 				if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2804 					zlog_debug(
2805 						"%s: LSA[Type%d:%s]: No associated LSDB!",
2806 						__func__, lsa->data->type,
2807 						inet_ntoa(lsa->data->id));
2808 			}
2809 		}
2810 
2811 	/*    A MaxAge LSA must be removed immediately from the router's link
2812 	      state database as soon as both a) it is no longer contained on any
2813 	      neighbor Link state retransmission lists and b) none of the
2814 	   router's
2815 	      neighbors are in states Exchange or Loading. */
2816 	if (reschedule)
2817 		OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
2818 			      ospf->maxage_delay);
2819 
2820 	return 0;
2821 }
2822 
ospf_lsa_maxage_delete(struct ospf * ospf,struct ospf_lsa * lsa)2823 void ospf_lsa_maxage_delete(struct ospf *ospf, struct ospf_lsa *lsa)
2824 {
2825 	struct route_node *rn;
2826 	struct prefix lsa_prefix;
2827 
2828 	memset(&lsa_prefix, 0, sizeof(struct prefix));
2829 	lsa_prefix.family = 0;
2830 	lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
2831 	lsa_prefix.u.ptr = (uintptr_t)lsa;
2832 
2833 	if ((rn = route_node_lookup(ospf->maxage_lsa, &lsa_prefix))) {
2834 		if (rn->info == lsa) {
2835 			UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2836 			ospf_lsa_unlock(&lsa); /* maxage_lsa */
2837 			rn->info = NULL;
2838 			route_unlock_node(
2839 				rn); /* unlock node because lsa is deleted */
2840 		}
2841 		route_unlock_node(rn); /* route_node_lookup */
2842 	} else {
2843 		if (IS_DEBUG_OSPF_EVENT)
2844 			zlog_debug("%s: lsa %s is not found in maxage db.",
2845 				   __func__, dump_lsa_key(lsa));
2846 	}
2847 }
2848 
2849 /* Add LSA onto the MaxAge list, and schedule for removal.
2850  * This does *not* lead to the LSA being flooded, that must be taken
2851  * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2852  * function).
2853  */
ospf_lsa_maxage(struct ospf * ospf,struct ospf_lsa * lsa)2854 void ospf_lsa_maxage(struct ospf *ospf, struct ospf_lsa *lsa)
2855 {
2856 	struct prefix lsa_prefix;
2857 	struct route_node *rn;
2858 
2859 	/* When we saw a MaxAge LSA flooded to us, we put it on the list
2860 	   and schedule the MaxAge LSA remover. */
2861 	if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE)) {
2862 		if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2863 			zlog_debug(
2864 				"LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2865 				lsa->data->type, inet_ntoa(lsa->data->id),
2866 				(void *)lsa);
2867 		return;
2868 	}
2869 
2870 	memset(&lsa_prefix, 0, sizeof(struct prefix));
2871 	lsa_prefix.family = 0;
2872 	lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
2873 	lsa_prefix.u.ptr = (uintptr_t)lsa;
2874 
2875 	rn = route_node_get(ospf->maxage_lsa, &lsa_prefix);
2876 	if (rn->info != NULL) {
2877 		if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2878 			zlog_debug(
2879 				   "LSA[%s]: found LSA (%p) in table for LSA %p %d",
2880 				   dump_lsa_key(lsa), rn->info,
2881 				   (void *)lsa, lsa_prefix.prefixlen);
2882 		route_unlock_node(rn);
2883 	} else {
2884 		rn->info = ospf_lsa_lock(lsa);
2885 		SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2886 	}
2887 
2888 	if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2889 		zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
2890 			   dump_lsa_key(lsa));
2891 
2892 	OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
2893 		      ospf->maxage_delay);
2894 }
2895 
ospf_lsa_maxage_walker_remover(struct ospf * ospf,struct ospf_lsa * lsa)2896 static int ospf_lsa_maxage_walker_remover(struct ospf *ospf,
2897 					  struct ospf_lsa *lsa)
2898 {
2899 	/* Stay away from any Local Translated Type-7 LSAs */
2900 	if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
2901 		return 0;
2902 
2903 	if (IS_LSA_MAXAGE(lsa))
2904 		/* Self-originated LSAs should NOT time-out instead,
2905 		   they're flushed and submitted to the max_age list explicitly.
2906 		   */
2907 		if (!ospf_lsa_is_self_originated(ospf, lsa)) {
2908 			if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2909 				zlog_debug("LSA[%s]: is MaxAge",
2910 					   dump_lsa_key(lsa));
2911 
2912 			switch (lsa->data->type) {
2913 			case OSPF_OPAQUE_LINK_LSA:
2914 			case OSPF_OPAQUE_AREA_LSA:
2915 			case OSPF_OPAQUE_AS_LSA:
2916 				/*
2917 				 * As a general rule, whenever network topology
2918 				 * has changed
2919 				 * (due to an LSA removal in this case), routing
2920 				 * recalculation
2921 				 * should be triggered. However, this is not
2922 				 * true for opaque
2923 				 * LSAs. Even if an opaque LSA instance is going
2924 				 * to be removed
2925 				 * from the routing domain, it does not mean a
2926 				 * change in network
2927 				 * topology, and thus, routing recalculation is
2928 				 * not needed here.
2929 				 */
2930 				break;
2931 			case OSPF_AS_EXTERNAL_LSA:
2932 			case OSPF_AS_NSSA_LSA:
2933 				ospf_ase_incremental_update(ospf, lsa);
2934 				break;
2935 			default:
2936 				ospf_spf_calculate_schedule(ospf,
2937 							    SPF_FLAG_MAXAGE);
2938 				break;
2939 			}
2940 			ospf_lsa_maxage(ospf, lsa);
2941 		}
2942 
2943 	if (IS_LSA_MAXAGE(lsa) && !ospf_lsa_is_self_originated(ospf, lsa))
2944 		if (LS_AGE(lsa) > OSPF_LSA_MAXAGE + 30)
2945 			printf("Eek! Shouldn't happen!\n");
2946 
2947 	return 0;
2948 }
2949 
2950 /* Periodical check of MaxAge LSA. */
ospf_lsa_maxage_walker(struct thread * thread)2951 int ospf_lsa_maxage_walker(struct thread *thread)
2952 {
2953 	struct ospf *ospf = THREAD_ARG(thread);
2954 	struct route_node *rn;
2955 	struct ospf_lsa *lsa;
2956 	struct ospf_area *area;
2957 	struct listnode *node, *nnode;
2958 
2959 	ospf->t_maxage_walker = NULL;
2960 
2961 	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
2962 		LSDB_LOOP (ROUTER_LSDB(area), rn, lsa)
2963 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2964 		LSDB_LOOP (NETWORK_LSDB(area), rn, lsa)
2965 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2966 		LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
2967 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2968 		LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
2969 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2970 		LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
2971 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2972 		LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
2973 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2974 		LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
2975 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2976 	}
2977 
2978 	/* for AS-external-LSAs. */
2979 	if (ospf->lsdb) {
2980 		LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
2981 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2982 		LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
2983 			ospf_lsa_maxage_walker_remover(ospf, lsa);
2984 	}
2985 
2986 	OSPF_TIMER_ON(ospf->t_maxage_walker, ospf_lsa_maxage_walker,
2987 		      OSPF_LSA_MAXAGE_CHECK_INTERVAL);
2988 	return 0;
2989 }
2990 
ospf_lsa_lookup_by_prefix(struct ospf_lsdb * lsdb,uint8_t type,struct prefix_ipv4 * p,struct in_addr router_id)2991 struct ospf_lsa *ospf_lsa_lookup_by_prefix(struct ospf_lsdb *lsdb, uint8_t type,
2992 					   struct prefix_ipv4 *p,
2993 					   struct in_addr router_id)
2994 {
2995 	struct ospf_lsa *lsa;
2996 	struct in_addr mask, id;
2997 	struct lsa_header_mask {
2998 		struct lsa_header header;
2999 		struct in_addr mask;
3000 	} * hmask;
3001 
3002 	lsa = ospf_lsdb_lookup_by_id(lsdb, type, p->prefix, router_id);
3003 	if (lsa == NULL)
3004 		return NULL;
3005 
3006 	masklen2ip(p->prefixlen, &mask);
3007 
3008 	hmask = (struct lsa_header_mask *)lsa->data;
3009 
3010 	if (mask.s_addr != hmask->mask.s_addr) {
3011 		id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3012 		lsa = ospf_lsdb_lookup_by_id(lsdb, type, id, router_id);
3013 		if (!lsa)
3014 			return NULL;
3015 	}
3016 
3017 	return lsa;
3018 }
3019 
ospf_lsa_lookup(struct ospf * ospf,struct ospf_area * area,uint32_t type,struct in_addr id,struct in_addr adv_router)3020 struct ospf_lsa *ospf_lsa_lookup(struct ospf *ospf, struct ospf_area *area,
3021 				 uint32_t type, struct in_addr id,
3022 				 struct in_addr adv_router)
3023 {
3024 	if (!ospf)
3025 		return NULL;
3026 
3027 	switch (type) {
3028 	case OSPF_ROUTER_LSA:
3029 	case OSPF_NETWORK_LSA:
3030 	case OSPF_SUMMARY_LSA:
3031 	case OSPF_ASBR_SUMMARY_LSA:
3032 	case OSPF_AS_NSSA_LSA:
3033 	case OSPF_OPAQUE_LINK_LSA:
3034 	case OSPF_OPAQUE_AREA_LSA:
3035 		return ospf_lsdb_lookup_by_id(area->lsdb, type, id, adv_router);
3036 	case OSPF_AS_EXTERNAL_LSA:
3037 	case OSPF_OPAQUE_AS_LSA:
3038 		return ospf_lsdb_lookup_by_id(ospf->lsdb, type, id, adv_router);
3039 	default:
3040 		break;
3041 	}
3042 
3043 	return NULL;
3044 }
3045 
ospf_lsa_lookup_by_id(struct ospf_area * area,uint32_t type,struct in_addr id)3046 struct ospf_lsa *ospf_lsa_lookup_by_id(struct ospf_area *area, uint32_t type,
3047 				       struct in_addr id)
3048 {
3049 	struct ospf_lsa *lsa;
3050 	struct route_node *rn;
3051 
3052 	switch (type) {
3053 	case OSPF_ROUTER_LSA:
3054 		return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
3055 	case OSPF_NETWORK_LSA:
3056 		for (rn = route_top(NETWORK_LSDB(area)); rn;
3057 		     rn = route_next(rn))
3058 			if ((lsa = rn->info))
3059 				if (IPV4_ADDR_SAME(&lsa->data->id, &id)) {
3060 					route_unlock_node(rn);
3061 					return lsa;
3062 				}
3063 		break;
3064 	case OSPF_SUMMARY_LSA:
3065 	case OSPF_ASBR_SUMMARY_LSA:
3066 		/* Currently not used. */
3067 		assert(1);
3068 		return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
3069 	case OSPF_AS_EXTERNAL_LSA:
3070 	case OSPF_AS_NSSA_LSA:
3071 	case OSPF_OPAQUE_LINK_LSA:
3072 	case OSPF_OPAQUE_AREA_LSA:
3073 	case OSPF_OPAQUE_AS_LSA:
3074 		/* Currently not used. */
3075 		break;
3076 	default:
3077 		break;
3078 	}
3079 
3080 	return NULL;
3081 }
3082 
ospf_lsa_lookup_by_header(struct ospf_area * area,struct lsa_header * lsah)3083 struct ospf_lsa *ospf_lsa_lookup_by_header(struct ospf_area *area,
3084 					   struct lsa_header *lsah)
3085 {
3086 	struct ospf_lsa *match;
3087 
3088 	/*
3089 	 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3090 	 * is redefined to have two subfields; opaque-type and opaque-id.
3091 	 * However, it is harmless to treat the two sub fields together, as if
3092 	 * they two were forming a unique LSA-ID.
3093 	 */
3094 
3095 	match = ospf_lsa_lookup(area->ospf, area, lsah->type, lsah->id,
3096 				lsah->adv_router);
3097 
3098 	if (match == NULL)
3099 		if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
3100 			zlog_debug("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3101 				   lsah->type, inet_ntoa(lsah->id));
3102 
3103 	return match;
3104 }
3105 
3106 /* return +n, l1 is more recent.
3107    return -n, l2 is more recent.
3108    return 0, l1 and l2 is identical. */
ospf_lsa_more_recent(struct ospf_lsa * l1,struct ospf_lsa * l2)3109 int ospf_lsa_more_recent(struct ospf_lsa *l1, struct ospf_lsa *l2)
3110 {
3111 	int r;
3112 	int x, y;
3113 
3114 	if (l1 == NULL && l2 == NULL)
3115 		return 0;
3116 	if (l1 == NULL)
3117 		return -1;
3118 	if (l2 == NULL)
3119 		return 1;
3120 
3121 	/* compare LS sequence number. */
3122 	x = (int)ntohl(l1->data->ls_seqnum);
3123 	y = (int)ntohl(l2->data->ls_seqnum);
3124 	if (x > y)
3125 		return 1;
3126 	if (x < y)
3127 		return -1;
3128 
3129 	/* compare LS checksum. */
3130 	r = ntohs(l1->data->checksum) - ntohs(l2->data->checksum);
3131 	if (r)
3132 		return r;
3133 
3134 	/* compare LS age. */
3135 	if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
3136 		return 1;
3137 	else if (!IS_LSA_MAXAGE(l1) && IS_LSA_MAXAGE(l2))
3138 		return -1;
3139 
3140 	/* compare LS age with MaxAgeDiff. */
3141 	if (LS_AGE(l1) - LS_AGE(l2) > OSPF_LSA_MAXAGE_DIFF)
3142 		return -1;
3143 	else if (LS_AGE(l2) - LS_AGE(l1) > OSPF_LSA_MAXAGE_DIFF)
3144 		return 1;
3145 
3146 	/* LSAs are identical. */
3147 	return 0;
3148 }
3149 
3150 /* If two LSAs are different, return 1, otherwise return 0. */
ospf_lsa_different(struct ospf_lsa * l1,struct ospf_lsa * l2)3151 int ospf_lsa_different(struct ospf_lsa *l1, struct ospf_lsa *l2)
3152 {
3153 	char *p1, *p2;
3154 	assert(l1);
3155 	assert(l2);
3156 	assert(l1->data);
3157 	assert(l2->data);
3158 
3159 	if (l1->data->options != l2->data->options)
3160 		return 1;
3161 
3162 	if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
3163 		return 1;
3164 
3165 	if (IS_LSA_MAXAGE(l2) && !IS_LSA_MAXAGE(l1))
3166 		return 1;
3167 
3168 	if (l1->data->length != l2->data->length)
3169 		return 1;
3170 
3171 	if (l1->data->length == 0)
3172 		return 1;
3173 
3174 	if (CHECK_FLAG((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
3175 		return 1; /* May be a stale LSA in the LSBD */
3176 
3177 	assert(ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
3178 
3179 	p1 = (char *)l1->data;
3180 	p2 = (char *)l2->data;
3181 
3182 	if (memcmp(p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
3183 		   ntohs(l1->data->length) - OSPF_LSA_HEADER_SIZE)
3184 	    != 0)
3185 		return 1;
3186 
3187 	return 0;
3188 }
3189 
ospf_lsa_flush_schedule(struct ospf * ospf,struct ospf_lsa * lsa)3190 int ospf_lsa_flush_schedule(struct ospf *ospf, struct ospf_lsa *lsa)
3191 {
3192 	if (lsa == NULL || !IS_LSA_SELF(lsa))
3193 		return 0;
3194 
3195 	if (IS_DEBUG_OSPF_EVENT)
3196 		zlog_debug(
3197 			"LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3198 			lsa->data->type, inet_ntoa(lsa->data->id));
3199 
3200 	/* Force given lsa's age to MaxAge. */
3201 	lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
3202 
3203 	switch (lsa->data->type) {
3204 	/* Opaque wants to be notified of flushes */
3205 	case OSPF_OPAQUE_LINK_LSA:
3206 	case OSPF_OPAQUE_AREA_LSA:
3207 	case OSPF_OPAQUE_AS_LSA:
3208 		ospf_opaque_lsa_refresh(lsa);
3209 		break;
3210 	default:
3211 		ospf_refresher_unregister_lsa(ospf, lsa);
3212 		ospf_lsa_flush(ospf, lsa);
3213 		break;
3214 	}
3215 
3216 	return 0;
3217 }
3218 
ospf_flush_self_originated_lsas_now(struct ospf * ospf)3219 void ospf_flush_self_originated_lsas_now(struct ospf *ospf)
3220 {
3221 	struct listnode *node, *nnode;
3222 	struct listnode *node2, *nnode2;
3223 	struct ospf_area *area;
3224 	struct ospf_interface *oi;
3225 	struct ospf_lsa *lsa;
3226 	struct route_node *rn;
3227 	int need_to_flush_ase = 0;
3228 
3229 	ospf->inst_shutdown = 1;
3230 
3231 	for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
3232 		if ((lsa = area->router_lsa_self) != NULL) {
3233 			if (IS_DEBUG_OSPF_EVENT)
3234 				zlog_debug(
3235 					"LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3236 					lsa->data->type,
3237 					inet_ntoa(lsa->data->id));
3238 
3239 			ospf_refresher_unregister_lsa(ospf, lsa);
3240 			ospf_lsa_flush_area(lsa, area);
3241 			ospf_lsa_unlock(&area->router_lsa_self);
3242 			area->router_lsa_self = NULL;
3243 		}
3244 
3245 		for (ALL_LIST_ELEMENTS(area->oiflist, node2, nnode2, oi)) {
3246 			if ((lsa = oi->network_lsa_self) != NULL
3247 			    && oi->state == ISM_DR && oi->full_nbrs > 0) {
3248 				if (IS_DEBUG_OSPF_EVENT)
3249 					zlog_debug(
3250 						"LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3251 						lsa->data->type,
3252 						inet_ntoa(lsa->data->id));
3253 
3254 				ospf_refresher_unregister_lsa(
3255 					ospf, oi->network_lsa_self);
3256 				ospf_lsa_flush_area(oi->network_lsa_self, area);
3257 				ospf_lsa_unlock(&oi->network_lsa_self);
3258 				oi->network_lsa_self = NULL;
3259 			}
3260 
3261 			if (oi->type != OSPF_IFTYPE_VIRTUALLINK
3262 			    && area->external_routing == OSPF_AREA_DEFAULT)
3263 				need_to_flush_ase = 1;
3264 		}
3265 
3266 		LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
3267 			ospf_lsa_flush_schedule(ospf, lsa);
3268 		LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
3269 			ospf_lsa_flush_schedule(ospf, lsa);
3270 		LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
3271 			ospf_lsa_flush_schedule(ospf, lsa);
3272 		LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
3273 			ospf_lsa_flush_schedule(ospf, lsa);
3274 	}
3275 
3276 	if (need_to_flush_ase) {
3277 		LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
3278 			ospf_lsa_flush_schedule(ospf, lsa);
3279 		LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
3280 			ospf_lsa_flush_schedule(ospf, lsa);
3281 	}
3282 
3283 	/*
3284 	 * Make sure that the MaxAge LSA remover is executed immediately,
3285 	 * without conflicting to other threads.
3286 	 */
3287 	if (ospf->t_maxage != NULL) {
3288 		OSPF_TIMER_OFF(ospf->t_maxage);
3289 		thread_execute(master, ospf_maxage_lsa_remover, ospf, 0);
3290 	}
3291 
3292 	return;
3293 }
3294 
3295 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3296 /* An interface-independent version of ospf_lsa_is_self_originated */
ospf_lsa_is_self_originated(struct ospf * ospf,struct ospf_lsa * lsa)3297 int ospf_lsa_is_self_originated(struct ospf *ospf, struct ospf_lsa *lsa)
3298 {
3299 	struct listnode *node;
3300 	struct ospf_interface *oi;
3301 
3302 	/* This LSA is already checked. */
3303 	if (CHECK_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED))
3304 		return IS_LSA_SELF(lsa);
3305 
3306 	/* Make sure LSA is self-checked. */
3307 	SET_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED);
3308 
3309 	/* AdvRouter and Router ID is the same. */
3310 	if (IPV4_ADDR_SAME(&lsa->data->adv_router, &ospf->router_id))
3311 		SET_FLAG(lsa->flags, OSPF_LSA_SELF);
3312 
3313 	/* LSA is router-LSA. */
3314 	else if (lsa->data->type == OSPF_ROUTER_LSA
3315 		 && IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id))
3316 		SET_FLAG(lsa->flags, OSPF_LSA_SELF);
3317 
3318 	/* LSA is network-LSA.  Compare Link ID with all interfaces. */
3319 	else if (lsa->data->type == OSPF_NETWORK_LSA)
3320 		for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
3321 			/* Ignore virtual link. */
3322 			if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
3323 				if (oi->address->family == AF_INET)
3324 					if (IPV4_ADDR_SAME(
3325 						    &lsa->data->id,
3326 						    &oi->address->u.prefix4)) {
3327 						/* to make it easier later */
3328 						SET_FLAG(lsa->flags,
3329 							 OSPF_LSA_SELF);
3330 						return IS_LSA_SELF(lsa);
3331 					}
3332 		}
3333 
3334 	return IS_LSA_SELF(lsa);
3335 }
3336 
3337 /* Get unique Link State ID. */
ospf_lsa_unique_id(struct ospf * ospf,struct ospf_lsdb * lsdb,uint8_t type,struct prefix_ipv4 * p)3338 struct in_addr ospf_lsa_unique_id(struct ospf *ospf, struct ospf_lsdb *lsdb,
3339 				  uint8_t type, struct prefix_ipv4 *p)
3340 {
3341 	struct ospf_lsa *lsa;
3342 	struct in_addr mask, id;
3343 
3344 	id = p->prefix;
3345 
3346 	/* Check existence of LSA instance. */
3347 	lsa = ospf_lsdb_lookup_by_id(lsdb, type, id, ospf->router_id);
3348 	if (lsa) {
3349 		struct as_external_lsa *al =
3350 			(struct as_external_lsa *)lsa->data;
3351 		if (ip_masklen(al->mask) == p->prefixlen) {
3352 			if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3353 				zlog_debug(
3354 					"ospf_lsa_unique_id(): Can't get Link State ID for %s/%d",
3355 					inet_ntoa(p->prefix), p->prefixlen);
3356 			/*	  id.s_addr = 0; */
3357 			id.s_addr = 0xffffffff;
3358 			return id;
3359 		}
3360 		/* Masklen differs, then apply wildcard mask to Link State ID.
3361 		   */
3362 		else {
3363 			masklen2ip(p->prefixlen, &mask);
3364 
3365 			id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3366 			lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, type, id,
3367 						     ospf->router_id);
3368 			if (lsa) {
3369 				if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3370 					zlog_debug(
3371 						"ospf_lsa_unique_id(): Can't get Link State ID for %s/%d",
3372 						inet_ntoa(p->prefix),
3373 						p->prefixlen);
3374 				/* 	      id.s_addr = 0; */
3375 				id.s_addr = 0xffffffff;
3376 				return id;
3377 			}
3378 		}
3379 	}
3380 
3381 	return id;
3382 }
3383 
3384 
3385 #define LSA_ACTION_FLOOD_AREA 1
3386 #define LSA_ACTION_FLUSH_AREA 2
3387 
3388 struct lsa_action {
3389 	uint8_t action;
3390 	struct ospf_area *area;
3391 	struct ospf_lsa *lsa;
3392 };
3393 
ospf_lsa_action(struct thread * t)3394 static int ospf_lsa_action(struct thread *t)
3395 {
3396 	struct lsa_action *data;
3397 
3398 	data = THREAD_ARG(t);
3399 
3400 	if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
3401 		zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3402 			   data->action);
3403 
3404 	switch (data->action) {
3405 	case LSA_ACTION_FLOOD_AREA:
3406 		ospf_flood_through_area(data->area, NULL, data->lsa);
3407 		break;
3408 	case LSA_ACTION_FLUSH_AREA:
3409 		ospf_lsa_flush_area(data->lsa, data->area);
3410 		break;
3411 	}
3412 
3413 	ospf_lsa_unlock(&data->lsa); /* Message */
3414 	XFREE(MTYPE_OSPF_MESSAGE, data);
3415 	return 0;
3416 }
3417 
ospf_schedule_lsa_flood_area(struct ospf_area * area,struct ospf_lsa * lsa)3418 void ospf_schedule_lsa_flood_area(struct ospf_area *area, struct ospf_lsa *lsa)
3419 {
3420 	struct lsa_action *data;
3421 
3422 	data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
3423 	data->action = LSA_ACTION_FLOOD_AREA;
3424 	data->area = area;
3425 	data->lsa = ospf_lsa_lock(lsa); /* Message / Flood area */
3426 
3427 	thread_add_event(master, ospf_lsa_action, data, 0, NULL);
3428 }
3429 
ospf_schedule_lsa_flush_area(struct ospf_area * area,struct ospf_lsa * lsa)3430 void ospf_schedule_lsa_flush_area(struct ospf_area *area, struct ospf_lsa *lsa)
3431 {
3432 	struct lsa_action *data;
3433 
3434 	data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
3435 	data->action = LSA_ACTION_FLUSH_AREA;
3436 	data->area = area;
3437 	data->lsa = ospf_lsa_lock(lsa); /* Message / Flush area */
3438 
3439 	thread_add_event(master, ospf_lsa_action, data, 0, NULL);
3440 }
3441 
3442 
3443 /* LSA Refreshment functions. */
ospf_lsa_refresh(struct ospf * ospf,struct ospf_lsa * lsa)3444 struct ospf_lsa *ospf_lsa_refresh(struct ospf *ospf, struct ospf_lsa *lsa)
3445 {
3446 	struct external_info *ei;
3447 	struct ospf_lsa *new = NULL;
3448 	assert(CHECK_FLAG(lsa->flags, OSPF_LSA_SELF));
3449 	assert(IS_LSA_SELF(lsa));
3450 	assert(lsa->lock > 0);
3451 
3452 	switch (lsa->data->type) {
3453 	/* Router and Network LSAs are processed differently. */
3454 	case OSPF_ROUTER_LSA:
3455 		new = ospf_router_lsa_refresh(lsa);
3456 		break;
3457 	case OSPF_NETWORK_LSA:
3458 		new = ospf_network_lsa_refresh(lsa);
3459 		break;
3460 	case OSPF_SUMMARY_LSA:
3461 		new = ospf_summary_lsa_refresh(ospf, lsa);
3462 		break;
3463 	case OSPF_ASBR_SUMMARY_LSA:
3464 		new = ospf_summary_asbr_lsa_refresh(ospf, lsa);
3465 		break;
3466 	case OSPF_AS_EXTERNAL_LSA:
3467 		/* Translated from NSSA Type-5s are refreshed when
3468 		 * from refresh of Type-7 - do not refresh these directly.
3469 		 */
3470 		if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
3471 			break;
3472 		ei = ospf_external_info_check(ospf, lsa);
3473 		if (ei)
3474 			new = ospf_external_lsa_refresh(ospf, lsa, ei,
3475 							LSA_REFRESH_FORCE);
3476 		else
3477 			ospf_lsa_flush_as(ospf, lsa);
3478 		break;
3479 	case OSPF_OPAQUE_LINK_LSA:
3480 	case OSPF_OPAQUE_AREA_LSA:
3481 	case OSPF_OPAQUE_AS_LSA:
3482 		new = ospf_opaque_lsa_refresh(lsa);
3483 		break;
3484 	default:
3485 		break;
3486 	}
3487 	return new;
3488 }
3489 
ospf_refresher_register_lsa(struct ospf * ospf,struct ospf_lsa * lsa)3490 void ospf_refresher_register_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
3491 {
3492 	uint16_t index, current_index;
3493 
3494 	assert(lsa->lock > 0);
3495 	assert(IS_LSA_SELF(lsa));
3496 
3497 	if (lsa->refresh_list < 0) {
3498 		int delay;
3499 		int min_delay =
3500 			OSPF_LS_REFRESH_TIME - (2 * OSPF_LS_REFRESH_JITTER);
3501 		int max_delay = OSPF_LS_REFRESH_TIME - OSPF_LS_REFRESH_JITTER;
3502 
3503 		/* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3504 		 * is
3505 		 * 1800s. Use jitter so that we send the LSA sometime between
3506 		 * 1680s
3507 		 * and 1740s.
3508 		 */
3509 		delay = (frr_weak_random() % (max_delay - min_delay))
3510 			+ min_delay;
3511 
3512 		current_index = ospf->lsa_refresh_queue.index
3513 				+ (monotime(NULL) - ospf->lsa_refresher_started)
3514 					  / OSPF_LSA_REFRESHER_GRANULARITY;
3515 
3516 		index = (current_index + delay / OSPF_LSA_REFRESHER_GRANULARITY)
3517 			% (OSPF_LSA_REFRESHER_SLOTS);
3518 
3519 		if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3520 			zlog_debug(
3521 				"LSA[Refresh:Type%d:%s]: age %d, added to index %d",
3522 				lsa->data->type, inet_ntoa(lsa->data->id),
3523 				LS_AGE(lsa), index);
3524 
3525 		if (!ospf->lsa_refresh_queue.qs[index])
3526 			ospf->lsa_refresh_queue.qs[index] = list_new();
3527 
3528 		listnode_add(ospf->lsa_refresh_queue.qs[index],
3529 			     ospf_lsa_lock(lsa)); /* lsa_refresh_queue */
3530 		lsa->refresh_list = index;
3531 
3532 		if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3533 			zlog_debug(
3534 				"LSA[Refresh:Type%d:%s]: ospf_refresher_register_lsa(): setting refresh_list on lsa %p (slod %d)",
3535 				lsa->data->type, inet_ntoa(lsa->data->id),
3536 				(void *)lsa, index);
3537 	}
3538 }
3539 
ospf_refresher_unregister_lsa(struct ospf * ospf,struct ospf_lsa * lsa)3540 void ospf_refresher_unregister_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
3541 {
3542 	assert(lsa->lock > 0);
3543 	assert(IS_LSA_SELF(lsa));
3544 	if (lsa->refresh_list >= 0) {
3545 		struct list *refresh_list =
3546 			ospf->lsa_refresh_queue.qs[lsa->refresh_list];
3547 		listnode_delete(refresh_list, lsa);
3548 		if (!listcount(refresh_list)) {
3549 			list_delete(&refresh_list);
3550 			ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
3551 		}
3552 		lsa->refresh_list = -1;
3553 		ospf_lsa_unlock(&lsa); /* lsa_refresh_queue */
3554 	}
3555 }
3556 
ospf_lsa_refresh_walker(struct thread * t)3557 int ospf_lsa_refresh_walker(struct thread *t)
3558 {
3559 	struct list *refresh_list;
3560 	struct listnode *node, *nnode;
3561 	struct ospf *ospf = THREAD_ARG(t);
3562 	struct ospf_lsa *lsa;
3563 	int i;
3564 	struct list *lsa_to_refresh = list_new();
3565 
3566 	if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3567 		zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3568 
3569 
3570 	i = ospf->lsa_refresh_queue.index;
3571 
3572 	/* Note: if clock has jumped backwards, then time change could be
3573 	   negative,
3574 	   so we are careful to cast the expression to unsigned before taking
3575 	   modulus. */
3576 	ospf->lsa_refresh_queue.index =
3577 		((unsigned long)(ospf->lsa_refresh_queue.index
3578 				 + (monotime(NULL)
3579 				    - ospf->lsa_refresher_started)
3580 					   / OSPF_LSA_REFRESHER_GRANULARITY))
3581 		% OSPF_LSA_REFRESHER_SLOTS;
3582 
3583 	if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3584 		zlog_debug(
3585 			"LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3586 			ospf->lsa_refresh_queue.index);
3587 
3588 	for (; i != ospf->lsa_refresh_queue.index;
3589 	     i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS) {
3590 		if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3591 			zlog_debug(
3592 				"LSA[Refresh]: ospf_lsa_refresh_walker(): refresh index %d",
3593 				i);
3594 
3595 		refresh_list = ospf->lsa_refresh_queue.qs[i];
3596 
3597 		assert(i >= 0);
3598 
3599 		ospf->lsa_refresh_queue.qs[i] = NULL;
3600 
3601 		if (refresh_list) {
3602 			for (ALL_LIST_ELEMENTS(refresh_list, node, nnode,
3603 					       lsa)) {
3604 				if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3605 					zlog_debug(
3606 						"LSA[Refresh:Type%d:%s]: ospf_lsa_refresh_walker(): refresh lsa %p (slot %d)",
3607 						lsa->data->type,
3608 						inet_ntoa(lsa->data->id),
3609 						(void *)lsa, i);
3610 
3611 				assert(lsa->lock > 0);
3612 				list_delete_node(refresh_list, node);
3613 				lsa->refresh_list = -1;
3614 				listnode_add(lsa_to_refresh, lsa);
3615 			}
3616 			list_delete(&refresh_list);
3617 		}
3618 	}
3619 
3620 	ospf->t_lsa_refresher = NULL;
3621 	thread_add_timer(master, ospf_lsa_refresh_walker, ospf,
3622 			 ospf->lsa_refresh_interval, &ospf->t_lsa_refresher);
3623 	ospf->lsa_refresher_started = monotime(NULL);
3624 
3625 	for (ALL_LIST_ELEMENTS(lsa_to_refresh, node, nnode, lsa)) {
3626 		ospf_lsa_refresh(ospf, lsa);
3627 		assert(lsa->lock > 0);
3628 		ospf_lsa_unlock(
3629 			&lsa); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3630 	}
3631 
3632 	list_delete(&lsa_to_refresh);
3633 
3634 	if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3635 		zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3636 
3637 	return 0;
3638 }
3639 
3640 /* Flush the LSAs for the specific area */
ospf_flush_lsa_from_area(struct ospf * ospf,struct in_addr area_id,int type)3641 void ospf_flush_lsa_from_area(struct ospf *ospf, struct in_addr area_id,
3642 			      int type)
3643 {
3644 	struct ospf_area *area;
3645 	struct route_node *rn;
3646 	struct ospf_lsa *lsa;
3647 
3648 	area = ospf_area_get(ospf, area_id);
3649 
3650 	switch (type) {
3651 	case OSPF_AS_EXTERNAL_LSA:
3652 		if ((area->external_routing == OSPF_AREA_NSSA) ||
3653 		    (area->external_routing == OSPF_AREA_STUB)) {
3654 			LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
3655 				if (IS_LSA_SELF(lsa) &&
3656 				    !(CHECK_FLAG(lsa->flags,
3657 						 OSPF_LSA_LOCAL_XLT)))
3658 					ospf_lsa_flush_area(lsa, area);
3659 		}
3660 		break;
3661 	case OSPF_AS_NSSA_LSA:
3662 		LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
3663 			if (IS_LSA_SELF(lsa))
3664 				ospf_lsa_flush_area(lsa, area);
3665 		break;
3666 	default:
3667 		break;
3668 	}
3669 }
3670