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