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