]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_lsa.c
ospfd: Add OSPF_WARN_LSA_INSTALL_FAILURE error code
[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 #include "ospfd/ospf_errors.h"
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_inplace(
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),
452 STREAM_SIZE(*s));
453 return 0;
454 }
455 }
456
457 /* TOS based routing is not supported. */
458 stream_put_ipv4(*s, id.s_addr); /* Link ID. */
459 stream_put_ipv4(*s, data.s_addr); /* Link Data. */
460 stream_putc(*s, type); /* Link Type. */
461 stream_putc(*s, tos); /* TOS = 0. */
462 stream_putw(*s, cost); /* Link Cost. */
463
464 return 1;
465 }
466
467 /* Describe Point-to-Point link (Section 12.4.1.1). */
468 static int lsa_link_ptop_set(struct stream **s, struct ospf_interface *oi)
469 {
470 int links = 0;
471 struct ospf_neighbor *nbr;
472 struct in_addr id, mask, data;
473 uint16_t cost = ospf_link_cost(oi);
474
475 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
476 zlog_debug("LSA[Type1]: Set link Point-to-Point");
477
478 if ((nbr = ospf_nbr_lookup_ptop(oi)))
479 if (nbr->state == NSM_Full) {
480 if (CHECK_FLAG(oi->connected->flags,
481 ZEBRA_IFA_UNNUMBERED)) {
482 /* For unnumbered point-to-point networks, the
483 Link Data field
484 should specify the interface's MIB-II ifIndex
485 value. */
486 data.s_addr = htonl(oi->ifp->ifindex);
487 links += link_info_set(
488 s, nbr->router_id, data,
489 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
490 } else {
491 links += link_info_set(
492 s, nbr->router_id,
493 oi->address->u.prefix4,
494 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
495 }
496 }
497
498 /* no need for a stub link for unnumbered interfaces */
499 if (!CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)) {
500 /* Regardless of the state of the neighboring router, we must
501 add a Type 3 link (stub network).
502 N.B. Options 1 & 2 share basically the same logic. */
503 masklen2ip(oi->address->prefixlen, &mask);
504 id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr
505 & mask.s_addr;
506 links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
507 oi->output_cost);
508 }
509
510 return links;
511 }
512
513 /* Describe Broadcast Link. */
514 static int lsa_link_broadcast_set(struct stream **s, struct ospf_interface *oi)
515 {
516 struct ospf_neighbor *dr;
517 struct in_addr id, mask;
518 uint16_t cost = ospf_link_cost(oi);
519
520 /* Describe Type 3 Link. */
521 if (oi->state == ISM_Waiting) {
522 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
523 zlog_debug(
524 "LSA[Type1]: Interface %s is in state Waiting. "
525 "Adding stub interface",
526 oi->ifp->name);
527 masklen2ip(oi->address->prefixlen, &mask);
528 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
529 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
530 oi->output_cost);
531 }
532
533 dr = ospf_nbr_lookup_by_addr(oi->nbrs, &DR(oi));
534 /* Describe Type 2 link. */
535 if (dr && (dr->state == NSM_Full
536 || IPV4_ADDR_SAME(&oi->address->u.prefix4, &DR(oi)))
537 && ospf_nbr_count(oi, NSM_Full) > 0) {
538 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
539 zlog_debug(
540 "LSA[Type1]: Interface %s has a DR. "
541 "Adding transit interface",
542 oi->ifp->name);
543 return link_info_set(s, DR(oi), oi->address->u.prefix4,
544 LSA_LINK_TYPE_TRANSIT, 0, cost);
545 }
546 /* Describe type 3 link. */
547 else {
548 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
549 zlog_debug(
550 "LSA[Type1]: Interface %s has no DR. "
551 "Adding stub interface",
552 oi->ifp->name);
553 masklen2ip(oi->address->prefixlen, &mask);
554 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
555 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
556 oi->output_cost);
557 }
558 }
559
560 static int lsa_link_loopback_set(struct stream **s, struct ospf_interface *oi)
561 {
562 struct in_addr id, mask;
563
564 /* Describe Type 3 Link. */
565 if (oi->state != ISM_Loopback)
566 return 0;
567
568 mask.s_addr = 0xffffffff;
569 id.s_addr = oi->address->u.prefix4.s_addr;
570 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
571 }
572
573 /* Describe Virtual Link. */
574 static int lsa_link_virtuallink_set(struct stream **s,
575 struct ospf_interface *oi)
576 {
577 struct ospf_neighbor *nbr;
578 uint16_t cost = ospf_link_cost(oi);
579
580 if (oi->state == ISM_PointToPoint)
581 if ((nbr = ospf_nbr_lookup_ptop(oi)))
582 if (nbr->state == NSM_Full) {
583 return link_info_set(s, nbr->router_id,
584 oi->address->u.prefix4,
585 LSA_LINK_TYPE_VIRTUALLINK,
586 0, cost);
587 }
588
589 return 0;
590 }
591
592 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
593
594 /* this function add for support point-to-multipoint ,see rfc2328
595 12.4.1.4.*/
596 /* from "edward rrr" <edward_rrr@hotmail.com>
597 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
598 static int lsa_link_ptomp_set(struct stream **s, struct ospf_interface *oi)
599 {
600 int links = 0;
601 struct route_node *rn;
602 struct ospf_neighbor *nbr = NULL;
603 struct in_addr id, mask;
604 uint16_t cost = ospf_link_cost(oi);
605
606 mask.s_addr = 0xffffffff;
607 id.s_addr = oi->address->u.prefix4.s_addr;
608 links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
609
610 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
611 zlog_debug("PointToMultipoint: running ptomultip_set");
612
613 /* Search neighbor, */
614 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
615 if ((nbr = rn->info) != NULL)
616 /* Ignore myself. */
617 if (!IPV4_ADDR_SAME(&nbr->router_id,
618 &oi->ospf->router_id))
619 if (nbr->state == NSM_Full)
620
621 {
622 links += link_info_set(
623 s, nbr->router_id,
624 oi->address->u.prefix4,
625 LSA_LINK_TYPE_POINTOPOINT, 0,
626 cost);
627 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
628 zlog_debug(
629 "PointToMultipoint: set link to %s",
630 inet_ntoa(
631 oi->address->u
632 .prefix4));
633 }
634
635 return links;
636 }
637
638 /* Set router-LSA link information. */
639 static int router_lsa_link_set(struct stream **s, struct ospf_area *area)
640 {
641 struct listnode *node;
642 struct ospf_interface *oi;
643 int links = 0;
644
645 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
646 struct interface *ifp = oi->ifp;
647
648 /* Check interface is up, OSPF is enable. */
649 if (if_is_operative(ifp)) {
650 if (oi->state != ISM_Down) {
651 oi->lsa_pos_beg = links;
652 /* Describe each link. */
653 switch (oi->type) {
654 case OSPF_IFTYPE_POINTOPOINT:
655 links += lsa_link_ptop_set(s, oi);
656 break;
657 case OSPF_IFTYPE_BROADCAST:
658 links += lsa_link_broadcast_set(s, oi);
659 break;
660 case OSPF_IFTYPE_NBMA:
661 links += lsa_link_nbma_set(s, oi);
662 break;
663 case OSPF_IFTYPE_POINTOMULTIPOINT:
664 links += lsa_link_ptomp_set(s, oi);
665 break;
666 case OSPF_IFTYPE_VIRTUALLINK:
667 links +=
668 lsa_link_virtuallink_set(s, oi);
669 break;
670 case OSPF_IFTYPE_LOOPBACK:
671 links += lsa_link_loopback_set(s, oi);
672 }
673 oi->lsa_pos_end = links;
674 }
675 }
676 }
677
678 return links;
679 }
680
681 /* Set router-LSA body. */
682 static void ospf_router_lsa_body_set(struct stream **s, struct ospf_area *area)
683 {
684 unsigned long putp;
685 uint16_t cnt;
686
687 /* Set flags. */
688 stream_putc(*s, router_lsa_flags(area));
689
690 /* Set Zero fields. */
691 stream_putc(*s, 0);
692
693 /* Keep pointer to # links. */
694 putp = stream_get_endp(*s);
695
696 /* Forward word */
697 stream_putw(*s, 0);
698
699 /* Set all link information. */
700 cnt = router_lsa_link_set(s, area);
701
702 /* Set # of links here. */
703 stream_putw_at(*s, putp, cnt);
704 }
705
706 static int ospf_stub_router_timer(struct thread *t)
707 {
708 struct ospf_area *area = THREAD_ARG(t);
709
710 area->t_stub_router = NULL;
711
712 SET_FLAG(area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
713
714 /* clear stub route state and generate router-lsa refresh, don't
715 * clobber an administratively set stub-router state though.
716 */
717 if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
718 return 0;
719
720 UNSET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
721
722 ospf_router_lsa_update_area(area);
723
724 return 0;
725 }
726
727 static void ospf_stub_router_check(struct ospf_area *area)
728 {
729 /* area must either be administratively configured to be stub
730 * or startup-time stub-router must be configured and we must in a
731 * pre-stub
732 * state.
733 */
734 if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED)) {
735 SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
736 return;
737 }
738
739 /* not admin-stubbed, check whether startup stubbing is configured and
740 * whether it's not been done yet
741 */
742 if (CHECK_FLAG(area->stub_router_state,
743 OSPF_AREA_WAS_START_STUB_ROUTED))
744 return;
745
746 if (area->ospf->stub_router_startup_time
747 == OSPF_STUB_ROUTER_UNCONFIGURED) {
748 /* stub-router is hence done forever for this area, even if
749 * someone
750 * tries configure it (take effect next restart).
751 */
752 SET_FLAG(area->stub_router_state,
753 OSPF_AREA_WAS_START_STUB_ROUTED);
754 return;
755 }
756
757 /* startup stub-router configured and not yet done */
758 SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
759
760 OSPF_AREA_TIMER_ON(area->t_stub_router, ospf_stub_router_timer,
761 area->ospf->stub_router_startup_time);
762 }
763
764 /* Create new router-LSA. */
765 static struct ospf_lsa *ospf_router_lsa_new(struct ospf_area *area)
766 {
767 struct ospf *ospf = area->ospf;
768 struct stream *s;
769 struct lsa_header *lsah;
770 struct ospf_lsa *new;
771 int length;
772
773 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
774 zlog_debug("LSA[Type1]: Create router-LSA instance");
775
776 /* check whether stub-router is desired, and if this is the first
777 * router LSA.
778 */
779 ospf_stub_router_check(area);
780
781 /* Create a stream for LSA. */
782 s = stream_new(OSPF_MAX_LSA_SIZE);
783 /* Set LSA common header fields. */
784 lsa_header_set(s, LSA_OPTIONS_GET(area) | LSA_OPTIONS_NSSA_GET(area),
785 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
786
787 /* Set router-LSA body fields. */
788 ospf_router_lsa_body_set(&s, area);
789
790 /* Set length. */
791 length = stream_get_endp(s);
792 lsah = (struct lsa_header *)STREAM_DATA(s);
793 lsah->length = htons(length);
794
795 /* Now, create OSPF LSA instance. */
796 new = ospf_lsa_new_and_data(length);
797
798 new->area = area;
799 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
800 new->vrf_id = area->ospf->vrf_id;
801
802 /* Copy LSA data to store, discard stream. */
803 memcpy(new->data, lsah, length);
804 stream_free(s);
805
806 return new;
807 }
808
809 /* Originate Router-LSA. */
810 static struct ospf_lsa *ospf_router_lsa_originate(struct ospf_area *area)
811 {
812 struct ospf_lsa *new;
813
814 /* Create new router-LSA instance. */
815 if ((new = ospf_router_lsa_new(area)) == NULL) {
816 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
817 return NULL;
818 }
819
820 /* Sanity check. */
821 if (new->data->adv_router.s_addr == 0) {
822 if (IS_DEBUG_OSPF_EVENT)
823 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
824 ospf_lsa_discard(new);
825 return NULL;
826 }
827
828 /* Install LSA to LSDB. */
829 new = ospf_lsa_install(area->ospf, NULL, new);
830
831 /* Update LSA origination count. */
832 area->ospf->lsa_originate_count++;
833
834 /* Flooding new LSA through area. */
835 ospf_flood_through_area(area, NULL, new);
836
837 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
838 zlog_debug("LSA[Type%d:%s]: Originate router-LSA %p",
839 new->data->type, inet_ntoa(new->data->id),
840 (void *)new);
841 ospf_lsa_header_dump(new->data);
842 }
843
844 return new;
845 }
846
847 /* Refresh router-LSA. */
848 static struct ospf_lsa *ospf_router_lsa_refresh(struct ospf_lsa *lsa)
849 {
850 struct ospf_area *area = lsa->area;
851 struct ospf_lsa *new;
852
853 /* Sanity check. */
854 assert(lsa->data);
855
856 /* Delete LSA from neighbor retransmit-list. */
857 ospf_ls_retransmit_delete_nbr_area(area, lsa);
858
859 /* Unregister LSA from refresh-list */
860 ospf_refresher_unregister_lsa(area->ospf, lsa);
861
862 /* Create new router-LSA instance. */
863 if ((new = ospf_router_lsa_new(area)) == NULL) {
864 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
865 return NULL;
866 }
867
868 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
869
870 ospf_lsa_install(area->ospf, NULL, new);
871
872 /* Flood LSA through area. */
873 ospf_flood_through_area(area, NULL, new);
874
875 /* Debug logging. */
876 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
877 zlog_debug("LSA[Type%d:%s]: router-LSA refresh",
878 new->data->type, inet_ntoa(new->data->id));
879 ospf_lsa_header_dump(new->data);
880 }
881
882 return NULL;
883 }
884
885 int ospf_router_lsa_update_area(struct ospf_area *area)
886 {
887 if (IS_DEBUG_OSPF_EVENT)
888 zlog_debug("[router-LSA]: (router-LSA area update)");
889
890 /* Now refresh router-LSA. */
891 if (area->router_lsa_self)
892 ospf_lsa_refresh(area->ospf, area->router_lsa_self);
893 /* Newly originate router-LSA. */
894 else
895 ospf_router_lsa_originate(area);
896
897 return 0;
898 }
899
900 int ospf_router_lsa_update(struct ospf *ospf)
901 {
902 struct listnode *node, *nnode;
903 struct ospf_area *area;
904
905 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
906 zlog_debug("Timer[router-LSA Update]: (timer expire)");
907
908 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
909 struct ospf_lsa *lsa = area->router_lsa_self;
910 struct router_lsa *rl;
911 const char *area_str;
912
913 /* Keep Area ID string. */
914 area_str = AREA_NAME(area);
915
916 /* If LSA not exist in this Area, originate new. */
917 if (lsa == NULL) {
918 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
919 zlog_debug(
920 "LSA[Type1]: Create router-LSA for Area %s",
921 area_str);
922
923 ospf_router_lsa_originate(area);
924 }
925 /* If router-ID is changed, Link ID must change.
926 First flush old LSA, then originate new. */
927 else if (!IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id)) {
928 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
929 zlog_debug(
930 "LSA[Type%d:%s]: Refresh router-LSA for Area %s",
931 lsa->data->type,
932 inet_ntoa(lsa->data->id), area_str);
933 ospf_refresher_unregister_lsa(ospf, lsa);
934 ospf_lsa_flush_area(lsa, area);
935 ospf_lsa_unlock(&area->router_lsa_self);
936 area->router_lsa_self = NULL;
937
938 /* Refresh router-LSA, (not install) and flood through
939 * area. */
940 ospf_router_lsa_update_area(area);
941 } else {
942 rl = (struct router_lsa *)lsa->data;
943 /* Refresh router-LSA, (not install) and flood through
944 * area. */
945 if (rl->flags != ospf->flags)
946 ospf_router_lsa_update_area(area);
947 }
948 }
949
950 return 0;
951 }
952
953
954 /* network-LSA related functions. */
955 /* Originate Network-LSA. */
956 static void ospf_network_lsa_body_set(struct stream *s,
957 struct ospf_interface *oi)
958 {
959 struct in_addr mask;
960 struct route_node *rn;
961 struct ospf_neighbor *nbr;
962
963 masklen2ip(oi->address->prefixlen, &mask);
964 stream_put_ipv4(s, mask.s_addr);
965
966 /* The network-LSA lists those routers that are fully adjacent to
967 the Designated Router; each fully adjacent router is identified by
968 its OSPF Router ID. The Designated Router includes itself in this
969 list. RFC2328, Section 12.4.2 */
970
971 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
972 if ((nbr = rn->info) != NULL)
973 if (nbr->state == NSM_Full || nbr == oi->nbr_self)
974 stream_put_ipv4(s, nbr->router_id.s_addr);
975 }
976
977 static struct ospf_lsa *ospf_network_lsa_new(struct ospf_interface *oi)
978 {
979 struct stream *s;
980 struct ospf_lsa *new;
981 struct lsa_header *lsah;
982 struct ospf_if_params *oip;
983 int length;
984
985 /* If there are no neighbours on this network (the net is stub),
986 the router does not originate network-LSA (see RFC 12.4.2) */
987 if (oi->full_nbrs == 0)
988 return NULL;
989
990 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
991 zlog_debug("LSA[Type2]: Create network-LSA instance");
992
993 /* Create new stream for LSA. */
994 s = stream_new(OSPF_MAX_LSA_SIZE);
995 lsah = (struct lsa_header *)STREAM_DATA(s);
996
997 lsa_header_set(s, (OPTIONS(oi) | LSA_OPTIONS_GET(oi->area)),
998 OSPF_NETWORK_LSA, DR(oi), oi->ospf->router_id);
999
1000 /* Set network-LSA body fields. */
1001 ospf_network_lsa_body_set(s, oi);
1002
1003 /* Set length. */
1004 length = stream_get_endp(s);
1005 lsah->length = htons(length);
1006
1007 /* Create OSPF LSA instance. */
1008 new = ospf_lsa_new_and_data(length);
1009
1010 new->area = oi->area;
1011 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1012 new->vrf_id = oi->ospf->vrf_id;
1013
1014 /* Copy LSA to store. */
1015 memcpy(new->data, lsah, length);
1016 stream_free(s);
1017
1018 /* Remember prior network LSA sequence numbers, even if we stop
1019 * originating one for this oi, to try avoid re-originating LSAs with a
1020 * prior sequence number, and thus speed up adjency forming &
1021 * convergence.
1022 */
1023 if ((oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4))) {
1024 new->data->ls_seqnum = oip->network_lsa_seqnum;
1025 new->data->ls_seqnum = lsa_seqnum_increment(new);
1026 } else {
1027 oip = ospf_get_if_params(oi->ifp, oi->address->u.prefix4);
1028 ospf_if_update_params(oi->ifp, oi->address->u.prefix4);
1029 }
1030 oip->network_lsa_seqnum = new->data->ls_seqnum;
1031
1032 return new;
1033 }
1034
1035 /* Originate network-LSA. */
1036 void ospf_network_lsa_update(struct ospf_interface *oi)
1037 {
1038 struct ospf_lsa *new;
1039
1040 if (oi->network_lsa_self != NULL) {
1041 ospf_lsa_refresh(oi->ospf, oi->network_lsa_self);
1042 return;
1043 }
1044
1045 /* Create new network-LSA instance. */
1046 new = ospf_network_lsa_new(oi);
1047 if (new == NULL)
1048 return;
1049
1050 /* Install LSA to LSDB. */
1051 new = ospf_lsa_install(oi->ospf, oi, new);
1052
1053 /* Update LSA origination count. */
1054 oi->ospf->lsa_originate_count++;
1055
1056 /* Flooding new LSA through area. */
1057 ospf_flood_through_area(oi->area, NULL, new);
1058
1059 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1060 zlog_debug("LSA[Type%d:%s]: Originate network-LSA %p",
1061 new->data->type, inet_ntoa(new->data->id),
1062 (void *)new);
1063 ospf_lsa_header_dump(new->data);
1064 }
1065
1066 return;
1067 }
1068
1069 static struct ospf_lsa *ospf_network_lsa_refresh(struct ospf_lsa *lsa)
1070 {
1071 struct ospf_area *area = lsa->area;
1072 struct ospf_lsa *new, *new2;
1073 struct ospf_if_params *oip;
1074 struct ospf_interface *oi;
1075
1076 assert(lsa->data);
1077
1078 /* Retrieve the oi for the network LSA */
1079 oi = ospf_if_lookup_by_local_addr(area->ospf, NULL, lsa->data->id);
1080 if (oi == NULL) {
1081 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1082 zlog_debug(
1083 "LSA[Type%d:%s]: network-LSA refresh: "
1084 "no oi found, ick, ignoring.",
1085 lsa->data->type, inet_ntoa(lsa->data->id));
1086 ospf_lsa_header_dump(lsa->data);
1087 }
1088 return NULL;
1089 }
1090 /* Delete LSA from neighbor retransmit-list. */
1091 ospf_ls_retransmit_delete_nbr_area(area, lsa);
1092
1093 /* Unregister LSA from refresh-list */
1094 ospf_refresher_unregister_lsa(area->ospf, lsa);
1095
1096 /* Create new network-LSA instance. */
1097 new = ospf_network_lsa_new(oi);
1098 if (new == NULL)
1099 return NULL;
1100
1101 oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4);
1102 assert(oip != NULL);
1103 oip->network_lsa_seqnum = new->data->ls_seqnum =
1104 lsa_seqnum_increment(lsa);
1105
1106 new2 = ospf_lsa_install(area->ospf, oi, new);
1107
1108 assert(new2 == new);
1109
1110 /* Flood LSA through aera. */
1111 ospf_flood_through_area(area, NULL, new);
1112
1113 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1114 zlog_debug("LSA[Type%d:%s]: network-LSA refresh",
1115 new->data->type, inet_ntoa(new->data->id));
1116 ospf_lsa_header_dump(new->data);
1117 }
1118
1119 return new;
1120 }
1121
1122 static void stream_put_ospf_metric(struct stream *s, uint32_t metric_value)
1123 {
1124 uint32_t metric;
1125 char *mp;
1126
1127 /* Put 0 metric. TOS metric is not supported. */
1128 metric = htonl(metric_value);
1129 mp = (char *)&metric;
1130 mp++;
1131 stream_put(s, mp, 3);
1132 }
1133
1134 /* summary-LSA related functions. */
1135 static void ospf_summary_lsa_body_set(struct stream *s, struct prefix *p,
1136 uint32_t metric)
1137 {
1138 struct in_addr mask;
1139
1140 masklen2ip(p->prefixlen, &mask);
1141
1142 /* Put Network Mask. */
1143 stream_put_ipv4(s, mask.s_addr);
1144
1145 /* Set # TOS. */
1146 stream_putc(s, (uint8_t)0);
1147
1148 /* Set metric. */
1149 stream_put_ospf_metric(s, metric);
1150 }
1151
1152 static struct ospf_lsa *ospf_summary_lsa_new(struct ospf_area *area,
1153 struct prefix *p, uint32_t metric,
1154 struct in_addr id)
1155 {
1156 struct stream *s;
1157 struct ospf_lsa *new;
1158 struct lsa_header *lsah;
1159 int length;
1160
1161 if (id.s_addr == 0xffffffff) {
1162 /* Maybe Link State ID not available. */
1163 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1164 zlog_debug(
1165 "LSA[Type%d]: Link ID not available, can't originate",
1166 OSPF_SUMMARY_LSA);
1167 return NULL;
1168 }
1169
1170 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1171 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1172
1173 /* Create new stream for LSA. */
1174 s = stream_new(OSPF_MAX_LSA_SIZE);
1175 lsah = (struct lsa_header *)STREAM_DATA(s);
1176
1177 lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_SUMMARY_LSA, id,
1178 area->ospf->router_id);
1179
1180 /* Set summary-LSA body fields. */
1181 ospf_summary_lsa_body_set(s, p, metric);
1182
1183 /* Set length. */
1184 length = stream_get_endp(s);
1185 lsah->length = htons(length);
1186
1187 /* Create OSPF LSA instance. */
1188 new = ospf_lsa_new_and_data(length);
1189 new->area = area;
1190 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1191 new->vrf_id = area->ospf->vrf_id;
1192
1193 /* Copy LSA to store. */
1194 memcpy(new->data, lsah, length);
1195 stream_free(s);
1196
1197 return new;
1198 }
1199
1200 /* Originate Summary-LSA. */
1201 struct ospf_lsa *ospf_summary_lsa_originate(struct prefix_ipv4 *p,
1202 uint32_t metric,
1203 struct ospf_area *area)
1204 {
1205 struct ospf_lsa *new;
1206 struct in_addr id;
1207
1208 id = ospf_lsa_unique_id(area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
1209
1210 if (id.s_addr == 0xffffffff) {
1211 /* Maybe Link State ID not available. */
1212 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1213 zlog_debug(
1214 "LSA[Type%d]: Link ID not available, can't originate",
1215 OSPF_SUMMARY_LSA);
1216 return NULL;
1217 }
1218
1219 /* Create new summary-LSA instance. */
1220 if (!(new = ospf_summary_lsa_new(area, (struct prefix *)p, metric, id)))
1221 return NULL;
1222
1223 /* Instlal LSA to LSDB. */
1224 new = ospf_lsa_install(area->ospf, NULL, new);
1225
1226 /* Update LSA origination count. */
1227 area->ospf->lsa_originate_count++;
1228
1229 /* Flooding new LSA through area. */
1230 ospf_flood_through_area(area, NULL, new);
1231
1232 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1233 zlog_debug("LSA[Type%d:%s]: Originate summary-LSA %p",
1234 new->data->type, inet_ntoa(new->data->id),
1235 (void *)new);
1236 ospf_lsa_header_dump(new->data);
1237 }
1238
1239 return new;
1240 }
1241
1242 static struct ospf_lsa *ospf_summary_lsa_refresh(struct ospf *ospf,
1243 struct ospf_lsa *lsa)
1244 {
1245 struct ospf_lsa *new;
1246 struct summary_lsa *sl;
1247 struct prefix p;
1248
1249 /* Sanity check. */
1250 assert(lsa->data);
1251
1252 sl = (struct summary_lsa *)lsa->data;
1253 p.prefixlen = ip_masklen(sl->mask);
1254 new = ospf_summary_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
1255 sl->header.id);
1256
1257 if (!new)
1258 return NULL;
1259
1260 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1261
1262 ospf_lsa_install(ospf, NULL, new);
1263
1264 /* Flood LSA through AS. */
1265 ospf_flood_through_area(new->area, NULL, new);
1266
1267 /* Debug logging. */
1268 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1269 zlog_debug("LSA[Type%d:%s]: summary-LSA refresh",
1270 new->data->type, inet_ntoa(new->data->id));
1271 ospf_lsa_header_dump(new->data);
1272 }
1273
1274 return new;
1275 }
1276
1277
1278 /* summary-ASBR-LSA related functions. */
1279 static void ospf_summary_asbr_lsa_body_set(struct stream *s, struct prefix *p,
1280 uint32_t metric)
1281 {
1282 /* Put Network Mask. */
1283 stream_put_ipv4(s, (uint32_t)0);
1284
1285 /* Set # TOS. */
1286 stream_putc(s, (uint8_t)0);
1287
1288 /* Set metric. */
1289 stream_put_ospf_metric(s, metric);
1290 }
1291
1292 static struct ospf_lsa *ospf_summary_asbr_lsa_new(struct ospf_area *area,
1293 struct prefix *p,
1294 uint32_t metric,
1295 struct in_addr id)
1296 {
1297 struct stream *s;
1298 struct ospf_lsa *new;
1299 struct lsa_header *lsah;
1300 int length;
1301
1302 if (id.s_addr == 0xffffffff) {
1303 /* Maybe Link State ID not available. */
1304 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1305 zlog_debug(
1306 "LSA[Type%d]: Link ID not available, can't originate",
1307 OSPF_ASBR_SUMMARY_LSA);
1308 return NULL;
1309 }
1310
1311 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1312 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1313
1314 /* Create new stream for LSA. */
1315 s = stream_new(OSPF_MAX_LSA_SIZE);
1316 lsah = (struct lsa_header *)STREAM_DATA(s);
1317
1318 lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_ASBR_SUMMARY_LSA, id,
1319 area->ospf->router_id);
1320
1321 /* Set summary-LSA body fields. */
1322 ospf_summary_asbr_lsa_body_set(s, p, metric);
1323
1324 /* Set length. */
1325 length = stream_get_endp(s);
1326 lsah->length = htons(length);
1327
1328 /* Create OSPF LSA instance. */
1329 new = ospf_lsa_new_and_data(length);
1330 new->area = area;
1331 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1332 new->vrf_id = area->ospf->vrf_id;
1333
1334 /* Copy LSA to store. */
1335 memcpy(new->data, lsah, length);
1336 stream_free(s);
1337
1338 return new;
1339 }
1340
1341 /* Originate summary-ASBR-LSA. */
1342 struct ospf_lsa *ospf_summary_asbr_lsa_originate(struct prefix_ipv4 *p,
1343 uint32_t metric,
1344 struct ospf_area *area)
1345 {
1346 struct ospf_lsa *new;
1347 struct in_addr id;
1348
1349 id = ospf_lsa_unique_id(area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA,
1350 p);
1351
1352 if (id.s_addr == 0xffffffff) {
1353 /* Maybe Link State ID not available. */
1354 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1355 zlog_debug(
1356 "LSA[Type%d]: Link ID not available, can't originate",
1357 OSPF_ASBR_SUMMARY_LSA);
1358 return NULL;
1359 }
1360
1361 /* Create new summary-LSA instance. */
1362 new = ospf_summary_asbr_lsa_new(area, (struct prefix *)p, metric, id);
1363 if (!new)
1364 return NULL;
1365
1366 /* Install LSA to LSDB. */
1367 new = ospf_lsa_install(area->ospf, NULL, new);
1368
1369 /* Update LSA origination count. */
1370 area->ospf->lsa_originate_count++;
1371
1372 /* Flooding new LSA through area. */
1373 ospf_flood_through_area(area, NULL, new);
1374
1375 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1376 zlog_debug("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1377 new->data->type, inet_ntoa(new->data->id),
1378 (void *)new);
1379 ospf_lsa_header_dump(new->data);
1380 }
1381
1382 return new;
1383 }
1384
1385 static struct ospf_lsa *ospf_summary_asbr_lsa_refresh(struct ospf *ospf,
1386 struct ospf_lsa *lsa)
1387 {
1388 struct ospf_lsa *new;
1389 struct summary_lsa *sl;
1390 struct prefix p;
1391
1392 /* Sanity check. */
1393 assert(lsa->data);
1394
1395 sl = (struct summary_lsa *)lsa->data;
1396 p.prefixlen = ip_masklen(sl->mask);
1397 new = ospf_summary_asbr_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
1398 sl->header.id);
1399 if (!new)
1400 return NULL;
1401
1402 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1403
1404 ospf_lsa_install(ospf, NULL, new);
1405
1406 /* Flood LSA through area. */
1407 ospf_flood_through_area(new->area, NULL, new);
1408
1409 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1410 zlog_debug("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1411 new->data->type, inet_ntoa(new->data->id));
1412 ospf_lsa_header_dump(new->data);
1413 }
1414
1415 return new;
1416 }
1417
1418 /* AS-external-LSA related functions. */
1419
1420 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1421 is connected, else 0*/
1422 static struct in_addr ospf_external_lsa_nexthop_get(struct ospf *ospf,
1423 struct in_addr nexthop)
1424 {
1425 struct in_addr fwd;
1426 struct prefix nh;
1427 struct listnode *node;
1428 struct ospf_interface *oi;
1429
1430 fwd.s_addr = 0;
1431
1432 if (!nexthop.s_addr)
1433 return fwd;
1434
1435 /* Check whether nexthop is covered by OSPF network. */
1436 nh.family = AF_INET;
1437 nh.u.prefix4 = nexthop;
1438 nh.prefixlen = IPV4_MAX_BITLEN;
1439
1440 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1441 * better to make use of the per-ifp table of ois.
1442 */
1443 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
1444 if (if_is_operative(oi->ifp))
1445 if (oi->address->family == AF_INET)
1446 if (prefix_match(oi->address, &nh))
1447 return nexthop;
1448
1449 return fwd;
1450 }
1451
1452 /* NSSA-external-LSA related functions. */
1453
1454 /* Get 1st IP connection for Forward Addr */
1455
1456 struct in_addr ospf_get_ip_from_ifp(struct ospf_interface *oi)
1457 {
1458 struct in_addr fwd;
1459
1460 fwd.s_addr = 0;
1461
1462 if (if_is_operative(oi->ifp))
1463 return oi->address->u.prefix4;
1464
1465 return fwd;
1466 }
1467
1468 /* Get 1st IP connection for Forward Addr */
1469 struct in_addr ospf_get_nssa_ip(struct ospf_area *area)
1470 {
1471 struct in_addr fwd;
1472 struct in_addr best_default;
1473 struct listnode *node;
1474 struct ospf_interface *oi;
1475
1476 fwd.s_addr = 0;
1477 best_default.s_addr = 0;
1478
1479 for (ALL_LIST_ELEMENTS_RO(area->ospf->oiflist, node, oi)) {
1480 if (if_is_operative(oi->ifp))
1481 if (oi->area->external_routing == OSPF_AREA_NSSA)
1482 if (oi->address
1483 && oi->address->family == AF_INET) {
1484 if (best_default.s_addr == 0)
1485 best_default =
1486 oi->address->u.prefix4;
1487 if (oi->area == area)
1488 return oi->address->u.prefix4;
1489 }
1490 }
1491 if (best_default.s_addr != 0)
1492 return best_default;
1493
1494 if (best_default.s_addr != 0)
1495 return best_default;
1496
1497 return fwd;
1498 }
1499
1500 #define DEFAULT_DEFAULT_METRIC 20
1501 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1502 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1503
1504 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1505
1506 int metric_type(struct ospf *ospf, uint8_t src, unsigned short instance)
1507 {
1508 struct ospf_redist *red;
1509
1510 red = ospf_redist_lookup(ospf, src, instance);
1511
1512 return ((!red || red->dmetric.type < 0) ? DEFAULT_METRIC_TYPE
1513 : red->dmetric.type);
1514 }
1515
1516 int metric_value(struct ospf *ospf, uint8_t src, unsigned short instance)
1517 {
1518 struct ospf_redist *red;
1519
1520 red = ospf_redist_lookup(ospf, src, instance);
1521 if (!red || red->dmetric.value < 0) {
1522 if (src == DEFAULT_ROUTE) {
1523 if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1524 return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1525 else
1526 return DEFAULT_DEFAULT_ALWAYS_METRIC;
1527 } else if (ospf->default_metric < 0)
1528 return DEFAULT_DEFAULT_METRIC;
1529 else
1530 return ospf->default_metric;
1531 }
1532
1533 return red->dmetric.value;
1534 }
1535
1536 /* Set AS-external-LSA body. */
1537 static void ospf_external_lsa_body_set(struct stream *s,
1538 struct external_info *ei,
1539 struct ospf *ospf)
1540 {
1541 struct prefix_ipv4 *p = &ei->p;
1542 struct in_addr mask, fwd_addr;
1543 uint32_t mvalue;
1544 int mtype;
1545 int type;
1546 unsigned short instance;
1547
1548 /* Put Network Mask. */
1549 masklen2ip(p->prefixlen, &mask);
1550 stream_put_ipv4(s, mask.s_addr);
1551
1552 /* If prefix is default, specify DEFAULT_ROUTE. */
1553 type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
1554 instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
1555
1556 mtype = (ROUTEMAP_METRIC_TYPE(ei) != -1)
1557 ? ROUTEMAP_METRIC_TYPE(ei)
1558 : metric_type(ospf, type, instance);
1559
1560 mvalue = (ROUTEMAP_METRIC(ei) != -1)
1561 ? ROUTEMAP_METRIC(ei)
1562 : metric_value(ospf, type, instance);
1563
1564 /* Put type of external metric. */
1565 stream_putc(s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1566
1567 /* Put 0 metric. TOS metric is not supported. */
1568 stream_put_ospf_metric(s, mvalue);
1569
1570 /* Get forwarding address to nexthop if on the Connection List, else 0.
1571 */
1572 fwd_addr = ospf_external_lsa_nexthop_get(ospf, ei->nexthop);
1573
1574 /* Put forwarding address. */
1575 stream_put_ipv4(s, fwd_addr.s_addr);
1576
1577 /* Put route tag */
1578 stream_putl(s, ei->tag);
1579 }
1580
1581 /* Create new external-LSA. */
1582 static struct ospf_lsa *ospf_external_lsa_new(struct ospf *ospf,
1583 struct external_info *ei,
1584 struct in_addr *old_id)
1585 {
1586 struct stream *s;
1587 struct lsa_header *lsah;
1588 struct ospf_lsa *new;
1589 struct in_addr id;
1590 int length;
1591
1592 if (ei == NULL) {
1593 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1594 zlog_debug(
1595 "LSA[Type5]: External info is NULL, can't originate");
1596 return NULL;
1597 }
1598
1599 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1600 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1601
1602 /* If old Link State ID is specified, refresh LSA with same ID. */
1603 if (old_id)
1604 id = *old_id;
1605 /* Get Link State with unique ID. */
1606 else {
1607 id = ospf_lsa_unique_id(ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
1608 &ei->p);
1609 if (id.s_addr == 0xffffffff) {
1610 /* Maybe Link State ID not available. */
1611 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1612 zlog_debug(
1613 "LSA[Type5]: Link ID not available, can't originate");
1614 return NULL;
1615 }
1616 }
1617
1618 /* Create new stream for LSA. */
1619 s = stream_new(OSPF_MAX_LSA_SIZE);
1620 lsah = (struct lsa_header *)STREAM_DATA(s);
1621
1622 /* Set LSA common header fields. */
1623 lsa_header_set(s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA, id,
1624 ospf->router_id);
1625
1626 /* Set AS-external-LSA body fields. */
1627 ospf_external_lsa_body_set(s, ei, ospf);
1628
1629 /* Set length. */
1630 length = stream_get_endp(s);
1631 lsah->length = htons(length);
1632
1633 /* Now, create OSPF LSA instance. */
1634 new = ospf_lsa_new_and_data(length);
1635 new->area = NULL;
1636 SET_FLAG(new->flags,
1637 OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
1638 new->vrf_id = ospf->vrf_id;
1639
1640 /* Copy LSA data to store, discard stream. */
1641 memcpy(new->data, lsah, length);
1642 stream_free(s);
1643
1644 return new;
1645 }
1646
1647 /* As Type-7 */
1648 static void ospf_install_flood_nssa(struct ospf *ospf, struct ospf_lsa *lsa,
1649 struct external_info *ei)
1650 {
1651 struct ospf_lsa *new;
1652 struct as_external_lsa *extlsa;
1653 struct ospf_area *area;
1654 struct listnode *node, *nnode;
1655
1656 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1657 * which originated from an NSSA area. In which case it should not be
1658 * flooded back to NSSA areas.
1659 */
1660 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
1661 return;
1662
1663 /* NSSA Originate or Refresh (If anyNSSA)
1664
1665 LSA is self-originated. And just installed as Type-5.
1666 Additionally, install as Type-7 LSDB for every attached NSSA.
1667
1668 P-Bit controls which ABR performs translation to outside world; If
1669 we are an ABR....do not set the P-bit, because we send the Type-5,
1670 not as the ABR Translator, but as the ASBR owner within the AS!
1671
1672 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1673 elected ABR Translator will see the P-bit, Translate, and re-flood.
1674
1675 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1676 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1677
1678 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
1679 /* Don't install Type-7 LSA's into nonNSSA area */
1680 if (area->external_routing != OSPF_AREA_NSSA)
1681 continue;
1682
1683 /* make lsa duplicate, lock=1 */
1684 new = ospf_lsa_dup(lsa);
1685 new->area = area;
1686 new->data->type = OSPF_AS_NSSA_LSA;
1687
1688 /* set P-bit if not ABR */
1689 if (!IS_OSPF_ABR(ospf)) {
1690 SET_FLAG(new->data->options, OSPF_OPTION_NP);
1691
1692 /* set non-zero FWD ADDR
1693
1694 draft-ietf-ospf-nssa-update-09.txt
1695
1696 if the network between the NSSA AS boundary router and
1697 the
1698 adjacent AS is advertised into OSPF as an internal OSPF
1699 route,
1700 the forwarding address should be the next op address as
1701 is cu
1702 currently done with type-5 LSAs. If the intervening
1703 network is
1704 not adversited into OSPF as an internal OSPF route and
1705 the
1706 type-7 LSA's P-bit is set a forwarding address should be
1707 selected from one of the router's active OSPF interface
1708 addresses
1709 which belong to the NSSA. If no such addresses exist,
1710 then
1711 no type-7 LSA's with the P-bit set should originate from
1712 this
1713 router. */
1714
1715 /* kevinm: not updating lsa anymore, just new */
1716 extlsa = (struct as_external_lsa *)(new->data);
1717
1718 if (extlsa->e[0].fwd_addr.s_addr == 0)
1719 extlsa->e[0].fwd_addr = ospf_get_nssa_ip(
1720 area); /* this NSSA area in ifp */
1721
1722 if (extlsa->e[0].fwd_addr.s_addr == 0) {
1723 if (IS_DEBUG_OSPF_NSSA)
1724 zlog_debug(
1725 "LSA[Type-7]: Could not build FWD-ADDR");
1726 ospf_lsa_discard(new);
1727 return;
1728 }
1729 }
1730
1731 /* install also as Type-7 */
1732 ospf_lsa_install(ospf, NULL,
1733 new); /* Remove Old, Lock New = 2 */
1734
1735 /* will send each copy, lock=2+n */
1736 ospf_flood_through_as(
1737 ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
1738 }
1739 }
1740
1741 static struct ospf_lsa *ospf_lsa_translated_nssa_new(struct ospf *ospf,
1742 struct ospf_lsa *type7)
1743 {
1744
1745 struct ospf_lsa *new;
1746 struct as_external_lsa *ext, *extnew;
1747 struct external_info ei;
1748
1749 ext = (struct as_external_lsa *)(type7->data);
1750
1751 /* need external_info struct, fill in bare minimum */
1752 ei.p.family = AF_INET;
1753 ei.p.prefix = type7->data->id;
1754 ei.p.prefixlen = ip_masklen(ext->mask);
1755 ei.type = ZEBRA_ROUTE_OSPF;
1756 ei.nexthop = ext->header.adv_router;
1757 ei.route_map_set.metric = -1;
1758 ei.route_map_set.metric_type = -1;
1759 ei.tag = 0;
1760 ei.instance = 0;
1761
1762 if ((new = ospf_external_lsa_new(ospf, &ei, &type7->data->id))
1763 == NULL) {
1764 if (IS_DEBUG_OSPF_NSSA)
1765 zlog_debug(
1766 "ospf_nssa_translate_originate(): Could not originate "
1767 "Translated Type-5 for %s",
1768 inet_ntoa(ei.p.prefix));
1769 return NULL;
1770 }
1771
1772 extnew = (struct as_external_lsa *)(new->data);
1773
1774 /* copy over Type-7 data to new */
1775 extnew->e[0].tos = ext->e[0].tos;
1776 extnew->e[0].route_tag = ext->e[0].route_tag;
1777 extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
1778 new->data->ls_seqnum = type7->data->ls_seqnum;
1779
1780 /* add translated flag, checksum and lock new lsa */
1781 SET_FLAG(new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7 */
1782 new = ospf_lsa_lock(new);
1783
1784 return new;
1785 }
1786
1787 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1788 struct ospf_lsa *ospf_translated_nssa_originate(struct ospf *ospf,
1789 struct ospf_lsa *type7)
1790 {
1791 struct ospf_lsa *new;
1792 struct as_external_lsa *extnew;
1793
1794 /* we cant use ospf_external_lsa_originate() as we need to set
1795 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1796 */
1797
1798 if ((new = ospf_lsa_translated_nssa_new(ospf, type7)) == NULL) {
1799 if (IS_DEBUG_OSPF_NSSA)
1800 zlog_debug(
1801 "ospf_translated_nssa_originate(): Could not translate "
1802 "Type-7, Id %s, to Type-5",
1803 inet_ntoa(type7->data->id));
1804 return NULL;
1805 }
1806
1807 extnew = (struct as_external_lsa *)new;
1808
1809 if (IS_DEBUG_OSPF_NSSA) {
1810 zlog_debug(
1811 "ospf_translated_nssa_originate(): "
1812 "translated Type 7, installed:");
1813 ospf_lsa_header_dump(new->data);
1814 zlog_debug(" Network mask: %d", ip_masklen(extnew->mask));
1815 zlog_debug(" Forward addr: %s",
1816 inet_ntoa(extnew->e[0].fwd_addr));
1817 }
1818
1819 if ((new = ospf_lsa_install(ospf, NULL, new)) == NULL) {
1820 flog_warn(OSPF_WARN_LSA_INSTALL_FAILURE,
1821 "ospf_lsa_translated_nssa_originate(): "
1822 "Could not install LSA "
1823 "id %s",
1824 inet_ntoa(type7->data->id));
1825 return NULL;
1826 }
1827
1828 ospf->lsa_originate_count++;
1829 ospf_flood_through_as(ospf, NULL, new);
1830
1831 return new;
1832 }
1833
1834 /* Refresh Translated from NSSA AS-external-LSA. */
1835 struct ospf_lsa *ospf_translated_nssa_refresh(struct ospf *ospf,
1836 struct ospf_lsa *type7,
1837 struct ospf_lsa *type5)
1838 {
1839 struct ospf_lsa *new = NULL;
1840
1841 /* Sanity checks. */
1842 assert(type7 || type5);
1843 if (!(type7 || type5))
1844 return NULL;
1845 if (type7)
1846 assert(type7->data);
1847 if (type5)
1848 assert(type5->data);
1849 assert(ospf->anyNSSA);
1850
1851 /* get required data according to what has been given */
1852 if (type7 && type5 == NULL) {
1853 /* find the translated Type-5 for this Type-7 */
1854 struct as_external_lsa *ext =
1855 (struct as_external_lsa *)(type7->data);
1856 struct prefix_ipv4 p = {
1857 .prefix = type7->data->id,
1858 .prefixlen = ip_masklen(ext->mask),
1859 .family = AF_INET,
1860 };
1861
1862 type5 = ospf_external_info_find_lsa(ospf, &p);
1863 } else if (type5 && type7 == NULL) {
1864 /* find the type-7 from which supplied type-5 was translated,
1865 * ie find first type-7 with same LSA Id.
1866 */
1867 struct listnode *ln, *lnn;
1868 struct route_node *rn;
1869 struct ospf_lsa *lsa;
1870 struct ospf_area *area;
1871
1872 for (ALL_LIST_ELEMENTS(ospf->areas, ln, lnn, area)) {
1873 if (area->external_routing != OSPF_AREA_NSSA && !type7)
1874 continue;
1875
1876 LSDB_LOOP (NSSA_LSDB(area), rn, lsa) {
1877 if (lsa->data->id.s_addr
1878 == type5->data->id.s_addr) {
1879 type7 = lsa;
1880 break;
1881 }
1882 }
1883 }
1884 }
1885
1886 /* do we have type7? */
1887 if (!type7) {
1888 if (IS_DEBUG_OSPF_NSSA)
1889 zlog_debug(
1890 "ospf_translated_nssa_refresh(): no Type-7 found for "
1891 "Type-5 LSA Id %s",
1892 type5 ? inet_ntoa(type5->data->id) : "(null)");
1893 return NULL;
1894 }
1895
1896 /* do we have valid translated type5? */
1897 if (type5 == NULL || !CHECK_FLAG(type5->flags, OSPF_LSA_LOCAL_XLT)) {
1898 if (IS_DEBUG_OSPF_NSSA)
1899 zlog_debug(
1900 "ospf_translated_nssa_refresh(): No translated Type-5 "
1901 "found for Type-7 with Id %s",
1902 type7 ? inet_ntoa(type7->data->id) : "(null)");
1903 return NULL;
1904 }
1905
1906 /* Delete LSA from neighbor retransmit-list. */
1907 ospf_ls_retransmit_delete_nbr_as(ospf, type5);
1908
1909 /* create new translated LSA */
1910 if ((new = ospf_lsa_translated_nssa_new(ospf, type7)) == NULL) {
1911 if (IS_DEBUG_OSPF_NSSA)
1912 zlog_debug(
1913 "ospf_translated_nssa_refresh(): Could not translate "
1914 "Type-7 for %s to Type-5",
1915 type7 ? inet_ntoa(type7->data->id) : "(null)");
1916 return NULL;
1917 }
1918
1919 if (!(new = ospf_lsa_install(ospf, NULL, new))) {
1920 flog_warn(
1921 OSPF_WARN_LSA_INSTALL_FAILURE,
1922 "ospf_translated_nssa_refresh(): Could not install 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 return;
2525
2526 old = ospf_lsdb_lookup(lsdb, lsa);
2527
2528 if (!old)
2529 return;
2530
2531 if (old->refresh_list >= 0)
2532 ospf_refresher_unregister_lsa(ospf, old);
2533
2534 switch (old->data->type) {
2535 case OSPF_AS_EXTERNAL_LSA:
2536 ospf_ase_unregister_external_lsa(old, ospf);
2537 ospf_ls_retransmit_delete_nbr_as(ospf, old);
2538 break;
2539 case OSPF_OPAQUE_AS_LSA:
2540 ospf_ls_retransmit_delete_nbr_as(ospf, old);
2541 break;
2542 case OSPF_AS_NSSA_LSA:
2543 ospf_ls_retransmit_delete_nbr_area(old->area, old);
2544 ospf_ase_unregister_external_lsa(old, ospf);
2545 break;
2546 default:
2547 ospf_ls_retransmit_delete_nbr_area(old->area, old);
2548 break;
2549 }
2550
2551 ospf_lsa_maxage_delete(ospf, old);
2552 ospf_lsa_discard(old);
2553 }
2554
2555 struct ospf_lsa *ospf_lsa_install(struct ospf *ospf, struct ospf_interface *oi,
2556 struct ospf_lsa *lsa)
2557 {
2558 struct ospf_lsa *new = NULL;
2559 struct ospf_lsa *old = NULL;
2560 struct ospf_lsdb *lsdb = NULL;
2561 int rt_recalc;
2562
2563 /* Set LSDB. */
2564 switch (lsa->data->type) {
2565 /* kevinm */
2566 case OSPF_AS_NSSA_LSA:
2567 if (lsa->area)
2568 lsdb = lsa->area->lsdb;
2569 else
2570 lsdb = ospf->lsdb;
2571 break;
2572 case OSPF_AS_EXTERNAL_LSA:
2573 case OSPF_OPAQUE_AS_LSA:
2574 lsdb = ospf->lsdb;
2575 break;
2576 default:
2577 if (lsa->area)
2578 lsdb = lsa->area->lsdb;
2579 break;
2580 }
2581
2582 assert(lsdb);
2583
2584 /* RFC 2328 13.2. Installing LSAs in the database
2585
2586 Installing a new LSA in the database, either as the result of
2587 flooding or a newly self-originated LSA, may cause the OSPF
2588 routing table structure to be recalculated. The contents of the
2589 new LSA should be compared to the old instance, if present. If
2590 there is no difference, there is no need to recalculate the
2591 routing table. When comparing an LSA to its previous instance,
2592 the following are all considered to be differences in contents:
2593
2594 o The LSA's Options field has changed.
2595
2596 o One of the LSA instances has LS age set to MaxAge, and
2597 the other does not.
2598
2599 o The length field in the LSA header has changed.
2600
2601 o The body of the LSA (i.e., anything outside the 20-byte
2602 LSA header) has changed. Note that this excludes changes
2603 in LS Sequence Number and LS Checksum.
2604
2605 */
2606 /* Look up old LSA and determine if any SPF calculation or incremental
2607 update is needed */
2608 old = ospf_lsdb_lookup(lsdb, lsa);
2609
2610 /* Do comparision and record if recalc needed. */
2611 rt_recalc = 0;
2612 if (old == NULL || ospf_lsa_different(old, lsa))
2613 rt_recalc = 1;
2614
2615 /*
2616 Sequence number check (Section 14.1 of rfc 2328)
2617 "Premature aging is used when it is time for a self-originated
2618 LSA's sequence number field to wrap. At this point, the current
2619 LSA instance (having LS sequence number MaxSequenceNumber) must
2620 be prematurely aged and flushed from the routing domain before a
2621 new instance with sequence number equal to InitialSequenceNumber
2622 can be originated. "
2623 */
2624
2625 if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER) {
2626 if (ospf_lsa_is_self_originated(ospf, lsa)) {
2627 lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
2628
2629 if (!IS_LSA_MAXAGE(lsa))
2630 lsa->flags |= OSPF_LSA_PREMATURE_AGE;
2631 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
2632
2633 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH)) {
2634 zlog_debug(
2635 "ospf_lsa_install() Premature Aging "
2636 "lsa 0x%p, seqnum 0x%x",
2637 (void *)lsa,
2638 ntohl(lsa->data->ls_seqnum));
2639 ospf_lsa_header_dump(lsa->data);
2640 }
2641 } else {
2642 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2643 zlog_debug(
2644 "ospf_lsa_install() got an lsa with seq 0x80000000 "
2645 "that was not self originated. Ignoring\n");
2646 ospf_lsa_header_dump(lsa->data);
2647 }
2648 return old;
2649 }
2650 }
2651
2652 /* discard old LSA from LSDB */
2653 if (old != NULL)
2654 ospf_discard_from_db(ospf, lsdb, lsa);
2655
2656 /* Calculate Checksum if self-originated?. */
2657 if (IS_LSA_SELF(lsa))
2658 ospf_lsa_checksum(lsa->data);
2659
2660 /* Insert LSA to LSDB. */
2661 ospf_lsdb_add(lsdb, lsa);
2662 lsa->lsdb = lsdb;
2663
2664 /* Do LSA specific installation process. */
2665 switch (lsa->data->type) {
2666 case OSPF_ROUTER_LSA:
2667 new = ospf_router_lsa_install(ospf, lsa, rt_recalc);
2668 break;
2669 case OSPF_NETWORK_LSA:
2670 assert(oi);
2671 new = ospf_network_lsa_install(ospf, oi, lsa, rt_recalc);
2672 break;
2673 case OSPF_SUMMARY_LSA:
2674 new = ospf_summary_lsa_install(ospf, lsa, rt_recalc);
2675 break;
2676 case OSPF_ASBR_SUMMARY_LSA:
2677 new = ospf_summary_asbr_lsa_install(ospf, lsa, rt_recalc);
2678 break;
2679 case OSPF_AS_EXTERNAL_LSA:
2680 new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
2681 break;
2682 case OSPF_OPAQUE_LINK_LSA:
2683 if (IS_LSA_SELF(lsa))
2684 lsa->oi = oi; /* Specify outgoing ospf-interface for
2685 this LSA. */
2686 else {
2687 /* Incoming "oi" for this LSA has set at LSUpd
2688 * reception. */
2689 }
2690 /* Fallthrough */
2691 case OSPF_OPAQUE_AREA_LSA:
2692 case OSPF_OPAQUE_AS_LSA:
2693 new = ospf_opaque_lsa_install(lsa, rt_recalc);
2694 break;
2695 case OSPF_AS_NSSA_LSA:
2696 new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
2697 default: /* type-6,8,9....nothing special */
2698 break;
2699 }
2700
2701 if (new == NULL)
2702 return new; /* Installation failed, cannot proceed further --
2703 endo. */
2704
2705 /* Debug logs. */
2706 if (IS_DEBUG_OSPF(lsa, LSA_INSTALL)) {
2707 char area_str[INET_ADDRSTRLEN];
2708
2709 switch (lsa->data->type) {
2710 case OSPF_AS_EXTERNAL_LSA:
2711 case OSPF_OPAQUE_AS_LSA:
2712 case OSPF_AS_NSSA_LSA:
2713 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2714 lookup_msg(ospf_lsa_type_msg,
2715 new->data->type, NULL));
2716 break;
2717 default:
2718 strlcpy(area_str, inet_ntoa(new->area->area_id),
2719 sizeof(area_str));
2720 zlog_debug("LSA[%s]: Install %s to Area %s",
2721 dump_lsa_key(new),
2722 lookup_msg(ospf_lsa_type_msg,
2723 new->data->type, NULL),
2724 area_str);
2725 break;
2726 }
2727 }
2728
2729 /*
2730 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2731 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2732 */
2733 if (IS_LSA_MAXAGE(new)) {
2734 if (IS_DEBUG_OSPF(lsa, LSA_INSTALL))
2735 zlog_debug("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2736 new->data->type, inet_ntoa(new->data->id),
2737 (void *)lsa);
2738 ospf_lsa_maxage(ospf, lsa);
2739 }
2740
2741 return new;
2742 }
2743
2744
2745 int ospf_check_nbr_status(struct ospf *ospf)
2746 {
2747 struct listnode *node, *nnode;
2748 struct ospf_interface *oi;
2749
2750 for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
2751 struct route_node *rn;
2752 struct ospf_neighbor *nbr;
2753
2754 if (ospf_if_is_enable(oi))
2755 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
2756 if ((nbr = rn->info) != NULL)
2757 if (nbr->state == NSM_Exchange
2758 || nbr->state == NSM_Loading) {
2759 route_unlock_node(rn);
2760 return 0;
2761 }
2762 }
2763
2764 return 1;
2765 }
2766
2767
2768 static int ospf_maxage_lsa_remover(struct thread *thread)
2769 {
2770 struct ospf *ospf = THREAD_ARG(thread);
2771 struct ospf_lsa *lsa;
2772 struct route_node *rn;
2773 int reschedule = 0;
2774
2775 ospf->t_maxage = NULL;
2776
2777 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2778 zlog_debug("LSA[MaxAge]: remover Start");
2779
2780 reschedule = !ospf_check_nbr_status(ospf);
2781
2782 if (!reschedule)
2783 for (rn = route_top(ospf->maxage_lsa); rn;
2784 rn = route_next(rn)) {
2785 if ((lsa = rn->info) == NULL) {
2786 continue;
2787 }
2788
2789 /* There is at least one neighbor from which we still
2790 * await an ack
2791 * for that LSA, so we are not allowed to remove it from
2792 * our lsdb yet
2793 * as per RFC 2328 section 14 para 4 a) */
2794 if (lsa->retransmit_counter > 0) {
2795 reschedule = 1;
2796 continue;
2797 }
2798
2799 /* TODO: maybe convert this function to a work-queue */
2800 if (thread_should_yield(thread)) {
2801 OSPF_TIMER_ON(ospf->t_maxage,
2802 ospf_maxage_lsa_remover, 0);
2803 route_unlock_node(
2804 rn); /* route_top/route_next */
2805 return 0;
2806 }
2807
2808 /* Remove LSA from the LSDB */
2809 if (IS_LSA_SELF(lsa))
2810 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2811 zlog_debug(
2812 "LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2813 lsa->data->type,
2814 inet_ntoa(lsa->data->id),
2815 (unsigned long)lsa);
2816
2817 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2818 zlog_debug(
2819 "LSA[Type%d:%s]: MaxAge LSA removed from list",
2820 lsa->data->type,
2821 inet_ntoa(lsa->data->id));
2822
2823 if (CHECK_FLAG(lsa->flags, OSPF_LSA_PREMATURE_AGE)) {
2824 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2825 zlog_debug(
2826 "originating new lsa for lsa 0x%p\n",
2827 (void *)lsa);
2828 ospf_lsa_refresh(ospf, lsa);
2829 }
2830
2831 /* Remove from lsdb. */
2832 if (lsa->lsdb) {
2833 ospf_discard_from_db(ospf, lsa->lsdb, lsa);
2834 ospf_lsdb_delete(lsa->lsdb, lsa);
2835 } else
2836 zlog_warn(
2837 "%s: LSA[Type%d:%s]: No associated LSDB!",
2838 __func__, lsa->data->type,
2839 inet_ntoa(lsa->data->id));
2840 }
2841
2842 /* A MaxAge LSA must be removed immediately from the router's link
2843 state database as soon as both a) it is no longer contained on any
2844 neighbor Link state retransmission lists and b) none of the
2845 router's
2846 neighbors are in states Exchange or Loading. */
2847 if (reschedule)
2848 OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
2849 ospf->maxage_delay);
2850
2851 return 0;
2852 }
2853
2854 void ospf_lsa_maxage_delete(struct ospf *ospf, struct ospf_lsa *lsa)
2855 {
2856 struct route_node *rn;
2857 struct prefix lsa_prefix;
2858
2859 memset(&lsa_prefix, 0, sizeof(struct prefix));
2860 lsa_prefix.family = 0;
2861 lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
2862 lsa_prefix.u.ptr = (uintptr_t)lsa;
2863
2864 if ((rn = route_node_lookup(ospf->maxage_lsa,
2865 (struct prefix *)&lsa_prefix))) {
2866 if (rn->info == lsa) {
2867 UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2868 ospf_lsa_unlock(&lsa); /* maxage_lsa */
2869 rn->info = NULL;
2870 route_unlock_node(
2871 rn); /* unlock node because lsa is deleted */
2872 }
2873 route_unlock_node(rn); /* route_node_lookup */
2874 } else {
2875 if (IS_DEBUG_OSPF_EVENT)
2876 zlog_debug("%s: lsa %s is not found in maxage db.",
2877 __PRETTY_FUNCTION__, dump_lsa_key(lsa));
2878 }
2879 }
2880
2881 /* Add LSA onto the MaxAge list, and schedule for removal.
2882 * This does *not* lead to the LSA being flooded, that must be taken
2883 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2884 * function).
2885 */
2886 void ospf_lsa_maxage(struct ospf *ospf, struct ospf_lsa *lsa)
2887 {
2888 struct prefix lsa_prefix;
2889 struct route_node *rn;
2890
2891 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2892 and schedule the MaxAge LSA remover. */
2893 if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE)) {
2894 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2895 zlog_debug(
2896 "LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2897 lsa->data->type, inet_ntoa(lsa->data->id),
2898 (void *)lsa);
2899 return;
2900 }
2901
2902 memset(&lsa_prefix, 0, sizeof(struct prefix));
2903 lsa_prefix.family = 0;
2904 lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
2905 lsa_prefix.u.ptr = (uintptr_t)lsa;
2906
2907 rn = route_node_get(ospf->maxage_lsa, (struct prefix *)&lsa_prefix);
2908 if (rn->info != NULL) {
2909 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2910 zlog_debug(
2911 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
2912 dump_lsa_key(lsa), rn->info,
2913 (void *)lsa, lsa_prefix.prefixlen);
2914 route_unlock_node(rn);
2915 } else {
2916 rn->info = ospf_lsa_lock(lsa);
2917 SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2918 }
2919
2920 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2921 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
2922 dump_lsa_key(lsa));
2923
2924 OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
2925 ospf->maxage_delay);
2926 }
2927
2928 static int ospf_lsa_maxage_walker_remover(struct ospf *ospf,
2929 struct ospf_lsa *lsa)
2930 {
2931 /* Stay away from any Local Translated Type-7 LSAs */
2932 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
2933 return 0;
2934
2935 if (IS_LSA_MAXAGE(lsa))
2936 /* Self-originated LSAs should NOT time-out instead,
2937 they're flushed and submitted to the max_age list explicitly.
2938 */
2939 if (!ospf_lsa_is_self_originated(ospf, lsa)) {
2940 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2941 zlog_debug("LSA[%s]: is MaxAge",
2942 dump_lsa_key(lsa));
2943
2944 switch (lsa->data->type) {
2945 case OSPF_OPAQUE_LINK_LSA:
2946 case OSPF_OPAQUE_AREA_LSA:
2947 case OSPF_OPAQUE_AS_LSA:
2948 /*
2949 * As a general rule, whenever network topology
2950 * has changed
2951 * (due to an LSA removal in this case), routing
2952 * recalculation
2953 * should be triggered. However, this is not
2954 * true for opaque
2955 * LSAs. Even if an opaque LSA instance is going
2956 * to be removed
2957 * from the routing domain, it does not mean a
2958 * change in network
2959 * topology, and thus, routing recalculation is
2960 * not needed here.
2961 */
2962 break;
2963 case OSPF_AS_EXTERNAL_LSA:
2964 case OSPF_AS_NSSA_LSA:
2965 ospf_ase_incremental_update(ospf, lsa);
2966 break;
2967 default:
2968 ospf_spf_calculate_schedule(ospf,
2969 SPF_FLAG_MAXAGE);
2970 break;
2971 }
2972 ospf_lsa_maxage(ospf, lsa);
2973 }
2974
2975 if (IS_LSA_MAXAGE(lsa) && !ospf_lsa_is_self_originated(ospf, lsa))
2976 if (LS_AGE(lsa) > OSPF_LSA_MAXAGE + 30)
2977 printf("Eek! Shouldn't happen!\n");
2978
2979 return 0;
2980 }
2981
2982 /* Periodical check of MaxAge LSA. */
2983 int ospf_lsa_maxage_walker(struct thread *thread)
2984 {
2985 struct ospf *ospf = THREAD_ARG(thread);
2986 struct route_node *rn;
2987 struct ospf_lsa *lsa;
2988 struct ospf_area *area;
2989 struct listnode *node, *nnode;
2990
2991 ospf->t_maxage_walker = NULL;
2992
2993 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
2994 LSDB_LOOP (ROUTER_LSDB(area), rn, lsa)
2995 ospf_lsa_maxage_walker_remover(ospf, lsa);
2996 LSDB_LOOP (NETWORK_LSDB(area), rn, lsa)
2997 ospf_lsa_maxage_walker_remover(ospf, lsa);
2998 LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
2999 ospf_lsa_maxage_walker_remover(ospf, lsa);
3000 LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
3001 ospf_lsa_maxage_walker_remover(ospf, lsa);
3002 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
3003 ospf_lsa_maxage_walker_remover(ospf, lsa);
3004 LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
3005 ospf_lsa_maxage_walker_remover(ospf, lsa);
3006 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
3007 ospf_lsa_maxage_walker_remover(ospf, lsa);
3008 }
3009
3010 /* for AS-external-LSAs. */
3011 if (ospf->lsdb) {
3012 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
3013 ospf_lsa_maxage_walker_remover(ospf, lsa);
3014 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
3015 ospf_lsa_maxage_walker_remover(ospf, lsa);
3016 }
3017
3018 OSPF_TIMER_ON(ospf->t_maxage_walker, ospf_lsa_maxage_walker,
3019 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
3020 return 0;
3021 }
3022
3023 struct ospf_lsa *ospf_lsa_lookup_by_prefix(struct ospf_lsdb *lsdb, uint8_t type,
3024 struct prefix_ipv4 *p,
3025 struct in_addr router_id)
3026 {
3027 struct ospf_lsa *lsa;
3028 struct in_addr mask, id;
3029 struct lsa_header_mask {
3030 struct lsa_header header;
3031 struct in_addr mask;
3032 } * hmask;
3033
3034 lsa = ospf_lsdb_lookup_by_id(lsdb, type, p->prefix, router_id);
3035 if (lsa == NULL)
3036 return NULL;
3037
3038 masklen2ip(p->prefixlen, &mask);
3039
3040 hmask = (struct lsa_header_mask *)lsa->data;
3041
3042 if (mask.s_addr != hmask->mask.s_addr) {
3043 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3044 lsa = ospf_lsdb_lookup_by_id(lsdb, type, id, router_id);
3045 if (!lsa)
3046 return NULL;
3047 }
3048
3049 return lsa;
3050 }
3051
3052 struct ospf_lsa *ospf_lsa_lookup(struct ospf *ospf, struct ospf_area *area,
3053 uint32_t type, struct in_addr id,
3054 struct in_addr adv_router)
3055 {
3056 if (!ospf)
3057 return NULL;
3058
3059 switch (type) {
3060 case OSPF_ROUTER_LSA:
3061 case OSPF_NETWORK_LSA:
3062 case OSPF_SUMMARY_LSA:
3063 case OSPF_ASBR_SUMMARY_LSA:
3064 case OSPF_AS_NSSA_LSA:
3065 case OSPF_OPAQUE_LINK_LSA:
3066 case OSPF_OPAQUE_AREA_LSA:
3067 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, adv_router);
3068 case OSPF_AS_EXTERNAL_LSA:
3069 case OSPF_OPAQUE_AS_LSA:
3070 return ospf_lsdb_lookup_by_id(ospf->lsdb, type, id, adv_router);
3071 default:
3072 break;
3073 }
3074
3075 return NULL;
3076 }
3077
3078 struct ospf_lsa *ospf_lsa_lookup_by_id(struct ospf_area *area, uint32_t type,
3079 struct in_addr id)
3080 {
3081 struct ospf_lsa *lsa;
3082 struct route_node *rn;
3083
3084 switch (type) {
3085 case OSPF_ROUTER_LSA:
3086 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
3087 case OSPF_NETWORK_LSA:
3088 for (rn = route_top(NETWORK_LSDB(area)); rn;
3089 rn = route_next(rn))
3090 if ((lsa = rn->info))
3091 if (IPV4_ADDR_SAME(&lsa->data->id, &id)) {
3092 route_unlock_node(rn);
3093 return lsa;
3094 }
3095 break;
3096 case OSPF_SUMMARY_LSA:
3097 case OSPF_ASBR_SUMMARY_LSA:
3098 /* Currently not used. */
3099 assert(1);
3100 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
3101 case OSPF_AS_EXTERNAL_LSA:
3102 case OSPF_AS_NSSA_LSA:
3103 case OSPF_OPAQUE_LINK_LSA:
3104 case OSPF_OPAQUE_AREA_LSA:
3105 case OSPF_OPAQUE_AS_LSA:
3106 /* Currently not used. */
3107 break;
3108 default:
3109 break;
3110 }
3111
3112 return NULL;
3113 }
3114
3115 struct ospf_lsa *ospf_lsa_lookup_by_header(struct ospf_area *area,
3116 struct lsa_header *lsah)
3117 {
3118 struct ospf_lsa *match;
3119
3120 /*
3121 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3122 * is redefined to have two subfields; opaque-type and opaque-id.
3123 * However, it is harmless to treat the two sub fields together, as if
3124 * they two were forming a unique LSA-ID.
3125 */
3126
3127 match = ospf_lsa_lookup(area->ospf, area, lsah->type, lsah->id,
3128 lsah->adv_router);
3129
3130 if (match == NULL)
3131 if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
3132 zlog_debug("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3133 lsah->type, inet_ntoa(lsah->id));
3134
3135 return match;
3136 }
3137
3138 /* return +n, l1 is more recent.
3139 return -n, l2 is more recent.
3140 return 0, l1 and l2 is identical. */
3141 int ospf_lsa_more_recent(struct ospf_lsa *l1, struct ospf_lsa *l2)
3142 {
3143 int r;
3144 int x, y;
3145
3146 if (l1 == NULL && l2 == NULL)
3147 return 0;
3148 if (l1 == NULL)
3149 return -1;
3150 if (l2 == NULL)
3151 return 1;
3152
3153 /* compare LS sequence number. */
3154 x = (int)ntohl(l1->data->ls_seqnum);
3155 y = (int)ntohl(l2->data->ls_seqnum);
3156 if (x > y)
3157 return 1;
3158 if (x < y)
3159 return -1;
3160
3161 /* compare LS checksum. */
3162 r = ntohs(l1->data->checksum) - ntohs(l2->data->checksum);
3163 if (r)
3164 return r;
3165
3166 /* compare LS age. */
3167 if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
3168 return 1;
3169 else if (!IS_LSA_MAXAGE(l1) && IS_LSA_MAXAGE(l2))
3170 return -1;
3171
3172 /* compare LS age with MaxAgeDiff. */
3173 if (LS_AGE(l1) - LS_AGE(l2) > OSPF_LSA_MAXAGE_DIFF)
3174 return -1;
3175 else if (LS_AGE(l2) - LS_AGE(l1) > OSPF_LSA_MAXAGE_DIFF)
3176 return 1;
3177
3178 /* LSAs are identical. */
3179 return 0;
3180 }
3181
3182 /* If two LSAs are different, return 1, otherwise return 0. */
3183 int ospf_lsa_different(struct ospf_lsa *l1, struct ospf_lsa *l2)
3184 {
3185 char *p1, *p2;
3186 assert(l1);
3187 assert(l2);
3188 assert(l1->data);
3189 assert(l2->data);
3190
3191 if (l1->data->options != l2->data->options)
3192 return 1;
3193
3194 if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
3195 return 1;
3196
3197 if (IS_LSA_MAXAGE(l2) && !IS_LSA_MAXAGE(l1))
3198 return 1;
3199
3200 if (l1->data->length != l2->data->length)
3201 return 1;
3202
3203 if (l1->data->length == 0)
3204 return 1;
3205
3206 if (CHECK_FLAG((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
3207 return 1; /* May be a stale LSA in the LSBD */
3208
3209 assert(ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
3210
3211 p1 = (char *)l1->data;
3212 p2 = (char *)l2->data;
3213
3214 if (memcmp(p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
3215 ntohs(l1->data->length) - OSPF_LSA_HEADER_SIZE)
3216 != 0)
3217 return 1;
3218
3219 return 0;
3220 }
3221
3222 #ifdef ORIGINAL_CODING
3223 void ospf_lsa_flush_self_originated(struct ospf_neighbor *nbr,
3224 struct ospf_lsa *self, struct ospf_lsa *new)
3225 {
3226 uint32_t seqnum;
3227
3228 /* Adjust LS Sequence Number. */
3229 seqnum = ntohl(new->data->ls_seqnum) + 1;
3230 self->data->ls_seqnum = htonl(seqnum);
3231
3232 /* Recalculate LSA checksum. */
3233 ospf_lsa_checksum(self->data);
3234
3235 /* Reflooding LSA. */
3236 /* RFC2328 Section 13.3
3237 On non-broadcast networks, separate Link State Update
3238 packets must be sent, as unicasts, to each adjacent neighbor
3239 (i.e., those in state Exchange or greater). The destination
3240 IP addresses for these packets are the neighbors' IP
3241 addresses. */
3242 if (nbr->oi->type == OSPF_IFTYPE_NBMA) {
3243 struct route_node *rn;
3244 struct ospf_neighbor *onbr;
3245
3246 for (rn = route_top(nbr->oi->nbrs); rn; rn = route_next(rn))
3247 if ((onbr = rn->info) != NULL)
3248 if (onbr != nbr->oi->nbr_self
3249 && onbr->status >= NSM_Exchange)
3250 ospf_ls_upd_send_lsa(
3251 onbr, self,
3252 OSPF_SEND_PACKET_DIRECT);
3253 } else
3254 ospf_ls_upd_send_lsa(nbr, self, OSPF_SEND_PACKET_INDIRECT);
3255
3256 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3257 zlog_debug("LSA[Type%d:%s]: Flush self-originated LSA",
3258 self->data->type, inet_ntoa(self->data->id));
3259 }
3260 #else /* ORIGINAL_CODING */
3261 int ospf_lsa_flush_schedule(struct ospf *ospf, struct ospf_lsa *lsa)
3262 {
3263 if (lsa == NULL || !IS_LSA_SELF(lsa))
3264 return 0;
3265
3266 if (IS_DEBUG_OSPF_EVENT)
3267 zlog_debug(
3268 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3269 lsa->data->type, inet_ntoa(lsa->data->id));
3270
3271 /* Force given lsa's age to MaxAge. */
3272 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
3273
3274 switch (lsa->data->type) {
3275 /* Opaque wants to be notified of flushes */
3276 case OSPF_OPAQUE_LINK_LSA:
3277 case OSPF_OPAQUE_AREA_LSA:
3278 case OSPF_OPAQUE_AS_LSA:
3279 ospf_opaque_lsa_refresh(lsa);
3280 break;
3281 default:
3282 ospf_refresher_unregister_lsa(ospf, lsa);
3283 ospf_lsa_flush(ospf, lsa);
3284 break;
3285 }
3286
3287 return 0;
3288 }
3289
3290 void ospf_flush_self_originated_lsas_now(struct ospf *ospf)
3291 {
3292 struct listnode *node, *nnode;
3293 struct listnode *node2, *nnode2;
3294 struct ospf_area *area;
3295 struct ospf_interface *oi;
3296 struct ospf_lsa *lsa;
3297 struct route_node *rn;
3298 int need_to_flush_ase = 0;
3299
3300 ospf->inst_shutdown = 1;
3301
3302 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
3303 if ((lsa = area->router_lsa_self) != NULL) {
3304 if (IS_DEBUG_OSPF_EVENT)
3305 zlog_debug(
3306 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3307 lsa->data->type,
3308 inet_ntoa(lsa->data->id));
3309
3310 ospf_refresher_unregister_lsa(ospf, lsa);
3311 ospf_lsa_flush_area(lsa, area);
3312 ospf_lsa_unlock(&area->router_lsa_self);
3313 area->router_lsa_self = NULL;
3314 }
3315
3316 for (ALL_LIST_ELEMENTS(area->oiflist, node2, nnode2, oi)) {
3317 if ((lsa = oi->network_lsa_self) != NULL
3318 && oi->state == ISM_DR && oi->full_nbrs > 0) {
3319 if (IS_DEBUG_OSPF_EVENT)
3320 zlog_debug(
3321 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3322 lsa->data->type,
3323 inet_ntoa(lsa->data->id));
3324
3325 ospf_refresher_unregister_lsa(
3326 ospf, oi->network_lsa_self);
3327 ospf_lsa_flush_area(oi->network_lsa_self, area);
3328 ospf_lsa_unlock(&oi->network_lsa_self);
3329 oi->network_lsa_self = NULL;
3330 }
3331
3332 if (oi->type != OSPF_IFTYPE_VIRTUALLINK
3333 && area->external_routing == OSPF_AREA_DEFAULT)
3334 need_to_flush_ase = 1;
3335 }
3336
3337 LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
3338 ospf_lsa_flush_schedule(ospf, lsa);
3339 LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
3340 ospf_lsa_flush_schedule(ospf, lsa);
3341 LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
3342 ospf_lsa_flush_schedule(ospf, lsa);
3343 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
3344 ospf_lsa_flush_schedule(ospf, lsa);
3345 }
3346
3347 if (need_to_flush_ase) {
3348 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
3349 ospf_lsa_flush_schedule(ospf, lsa);
3350 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
3351 ospf_lsa_flush_schedule(ospf, lsa);
3352 }
3353
3354 /*
3355 * Make sure that the MaxAge LSA remover is executed immediately,
3356 * without conflicting to other threads.
3357 */
3358 if (ospf->t_maxage != NULL) {
3359 OSPF_TIMER_OFF(ospf->t_maxage);
3360 thread_execute(master, ospf_maxage_lsa_remover, ospf, 0);
3361 }
3362
3363 return;
3364 }
3365 #endif /* ORIGINAL_CODING */
3366
3367 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3368 /* An interface-independent version of ospf_lsa_is_self_originated */
3369 int ospf_lsa_is_self_originated(struct ospf *ospf, struct ospf_lsa *lsa)
3370 {
3371 struct listnode *node;
3372 struct ospf_interface *oi;
3373
3374 /* This LSA is already checked. */
3375 if (CHECK_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED))
3376 return IS_LSA_SELF(lsa);
3377
3378 /* Make sure LSA is self-checked. */
3379 SET_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED);
3380
3381 /* AdvRouter and Router ID is the same. */
3382 if (IPV4_ADDR_SAME(&lsa->data->adv_router, &ospf->router_id))
3383 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
3384
3385 /* LSA is router-LSA. */
3386 else if (lsa->data->type == OSPF_ROUTER_LSA
3387 && IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id))
3388 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
3389
3390 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3391 else if (lsa->data->type == OSPF_NETWORK_LSA)
3392 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
3393 /* Ignore virtual link. */
3394 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
3395 if (oi->address->family == AF_INET)
3396 if (IPV4_ADDR_SAME(
3397 &lsa->data->id,
3398 &oi->address->u.prefix4)) {
3399 /* to make it easier later */
3400 SET_FLAG(lsa->flags,
3401 OSPF_LSA_SELF);
3402 return IS_LSA_SELF(lsa);
3403 }
3404 }
3405
3406 return IS_LSA_SELF(lsa);
3407 }
3408
3409 /* Get unique Link State ID. */
3410 struct in_addr ospf_lsa_unique_id(struct ospf *ospf, struct ospf_lsdb *lsdb,
3411 uint8_t type, struct prefix_ipv4 *p)
3412 {
3413 struct ospf_lsa *lsa;
3414 struct in_addr mask, id;
3415
3416 id = p->prefix;
3417
3418 /* Check existence of LSA instance. */
3419 lsa = ospf_lsdb_lookup_by_id(lsdb, type, id, ospf->router_id);
3420 if (lsa) {
3421 struct as_external_lsa *al =
3422 (struct as_external_lsa *)lsa->data;
3423 if (ip_masklen(al->mask) == p->prefixlen) {
3424 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3425 zlog_debug(
3426 "ospf_lsa_unique_id(): "
3427 "Can't get Link State ID for %s/%d",
3428 inet_ntoa(p->prefix), p->prefixlen);
3429 /* id.s_addr = 0; */
3430 id.s_addr = 0xffffffff;
3431 return id;
3432 }
3433 /* Masklen differs, then apply wildcard mask to Link State ID.
3434 */
3435 else {
3436 masklen2ip(p->prefixlen, &mask);
3437
3438 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3439 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, type, id,
3440 ospf->router_id);
3441 if (lsa) {
3442 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3443 zlog_debug(
3444 "ospf_lsa_unique_id(): "
3445 "Can't get Link State ID for %s/%d",
3446 inet_ntoa(p->prefix),
3447 p->prefixlen);
3448 /* id.s_addr = 0; */
3449 id.s_addr = 0xffffffff;
3450 return id;
3451 }
3452 }
3453 }
3454
3455 return id;
3456 }
3457
3458
3459 #define LSA_ACTION_FLOOD_AREA 1
3460 #define LSA_ACTION_FLUSH_AREA 2
3461
3462 struct lsa_action {
3463 uint8_t action;
3464 struct ospf_area *area;
3465 struct ospf_lsa *lsa;
3466 };
3467
3468 static int ospf_lsa_action(struct thread *t)
3469 {
3470 struct lsa_action *data;
3471
3472 data = THREAD_ARG(t);
3473
3474 if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
3475 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3476 data->action);
3477
3478 switch (data->action) {
3479 case LSA_ACTION_FLOOD_AREA:
3480 ospf_flood_through_area(data->area, NULL, data->lsa);
3481 break;
3482 case LSA_ACTION_FLUSH_AREA:
3483 ospf_lsa_flush_area(data->lsa, data->area);
3484 break;
3485 }
3486
3487 ospf_lsa_unlock(&data->lsa); /* Message */
3488 XFREE(MTYPE_OSPF_MESSAGE, data);
3489 return 0;
3490 }
3491
3492 void ospf_schedule_lsa_flood_area(struct ospf_area *area, struct ospf_lsa *lsa)
3493 {
3494 struct lsa_action *data;
3495
3496 data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
3497 data->action = LSA_ACTION_FLOOD_AREA;
3498 data->area = area;
3499 data->lsa = ospf_lsa_lock(lsa); /* Message / Flood area */
3500
3501 thread_add_event(master, ospf_lsa_action, data, 0, NULL);
3502 }
3503
3504 void ospf_schedule_lsa_flush_area(struct ospf_area *area, struct ospf_lsa *lsa)
3505 {
3506 struct lsa_action *data;
3507
3508 data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
3509 data->action = LSA_ACTION_FLUSH_AREA;
3510 data->area = area;
3511 data->lsa = ospf_lsa_lock(lsa); /* Message / Flush area */
3512
3513 thread_add_event(master, ospf_lsa_action, data, 0, NULL);
3514 }
3515
3516
3517 /* LSA Refreshment functions. */
3518 struct ospf_lsa *ospf_lsa_refresh(struct ospf *ospf, struct ospf_lsa *lsa)
3519 {
3520 struct external_info *ei;
3521 struct ospf_lsa *new = NULL;
3522 assert(CHECK_FLAG(lsa->flags, OSPF_LSA_SELF));
3523 assert(IS_LSA_SELF(lsa));
3524 assert(lsa->lock > 0);
3525
3526 switch (lsa->data->type) {
3527 /* Router and Network LSAs are processed differently. */
3528 case OSPF_ROUTER_LSA:
3529 new = ospf_router_lsa_refresh(lsa);
3530 break;
3531 case OSPF_NETWORK_LSA:
3532 new = ospf_network_lsa_refresh(lsa);
3533 break;
3534 case OSPF_SUMMARY_LSA:
3535 new = ospf_summary_lsa_refresh(ospf, lsa);
3536 break;
3537 case OSPF_ASBR_SUMMARY_LSA:
3538 new = ospf_summary_asbr_lsa_refresh(ospf, lsa);
3539 break;
3540 case OSPF_AS_EXTERNAL_LSA:
3541 /* Translated from NSSA Type-5s are refreshed when
3542 * from refresh of Type-7 - do not refresh these directly.
3543 */
3544 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
3545 break;
3546 ei = ospf_external_info_check(ospf, lsa);
3547 if (ei)
3548 new = ospf_external_lsa_refresh(ospf, lsa, ei,
3549 LSA_REFRESH_FORCE);
3550 else
3551 ospf_lsa_flush_as(ospf, lsa);
3552 break;
3553 case OSPF_OPAQUE_LINK_LSA:
3554 case OSPF_OPAQUE_AREA_LSA:
3555 case OSPF_OPAQUE_AS_LSA:
3556 new = ospf_opaque_lsa_refresh(lsa);
3557 break;
3558 default:
3559 break;
3560 }
3561 return new;
3562 }
3563
3564 void ospf_refresher_register_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
3565 {
3566 uint16_t index, current_index;
3567
3568 assert(lsa->lock > 0);
3569 assert(IS_LSA_SELF(lsa));
3570
3571 if (lsa->refresh_list < 0) {
3572 int delay;
3573 int min_delay =
3574 OSPF_LS_REFRESH_TIME - (2 * OSPF_LS_REFRESH_JITTER);
3575 int max_delay = OSPF_LS_REFRESH_TIME - OSPF_LS_REFRESH_JITTER;
3576
3577 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3578 * is
3579 * 1800s. Use jitter so that we send the LSA sometime between
3580 * 1680s
3581 * and 1740s.
3582 */
3583 delay = (random() % (max_delay - min_delay)) + min_delay;
3584
3585 current_index = ospf->lsa_refresh_queue.index
3586 + (monotime(NULL) - ospf->lsa_refresher_started)
3587 / OSPF_LSA_REFRESHER_GRANULARITY;
3588
3589 index = (current_index + delay / OSPF_LSA_REFRESHER_GRANULARITY)
3590 % (OSPF_LSA_REFRESHER_SLOTS);
3591
3592 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3593 zlog_debug(
3594 "LSA[Refresh:Type%d:%s]: age %d, added to index %d",
3595 lsa->data->type, inet_ntoa(lsa->data->id),
3596 LS_AGE(lsa), index);
3597
3598 if (!ospf->lsa_refresh_queue.qs[index])
3599 ospf->lsa_refresh_queue.qs[index] = list_new();
3600
3601 listnode_add(ospf->lsa_refresh_queue.qs[index],
3602 ospf_lsa_lock(lsa)); /* lsa_refresh_queue */
3603 lsa->refresh_list = index;
3604
3605 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3606 zlog_debug(
3607 "LSA[Refresh:Type%d:%s]: ospf_refresher_register_lsa(): "
3608 "setting refresh_list on lsa %p (slod %d)",
3609 lsa->data->type, inet_ntoa(lsa->data->id),
3610 (void *)lsa, index);
3611 }
3612 }
3613
3614 void ospf_refresher_unregister_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
3615 {
3616 assert(lsa->lock > 0);
3617 assert(IS_LSA_SELF(lsa));
3618 if (lsa->refresh_list >= 0) {
3619 struct list *refresh_list =
3620 ospf->lsa_refresh_queue.qs[lsa->refresh_list];
3621 listnode_delete(refresh_list, lsa);
3622 if (!listcount(refresh_list)) {
3623 list_delete_and_null(&refresh_list);
3624 ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
3625 }
3626 ospf_lsa_unlock(&lsa); /* lsa_refresh_queue */
3627 lsa->refresh_list = -1;
3628 }
3629 }
3630
3631 int ospf_lsa_refresh_walker(struct thread *t)
3632 {
3633 struct list *refresh_list;
3634 struct listnode *node, *nnode;
3635 struct ospf *ospf = THREAD_ARG(t);
3636 struct ospf_lsa *lsa;
3637 int i;
3638 struct list *lsa_to_refresh = list_new();
3639
3640 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3641 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3642
3643
3644 i = ospf->lsa_refresh_queue.index;
3645
3646 /* Note: if clock has jumped backwards, then time change could be
3647 negative,
3648 so we are careful to cast the expression to unsigned before taking
3649 modulus. */
3650 ospf->lsa_refresh_queue.index =
3651 ((unsigned long)(ospf->lsa_refresh_queue.index
3652 + (monotime(NULL)
3653 - ospf->lsa_refresher_started)
3654 / OSPF_LSA_REFRESHER_GRANULARITY))
3655 % OSPF_LSA_REFRESHER_SLOTS;
3656
3657 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3658 zlog_debug(
3659 "LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3660 ospf->lsa_refresh_queue.index);
3661
3662 for (; i != ospf->lsa_refresh_queue.index;
3663 i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS) {
3664 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3665 zlog_debug(
3666 "LSA[Refresh]: ospf_lsa_refresh_walker(): "
3667 "refresh index %d",
3668 i);
3669
3670 refresh_list = ospf->lsa_refresh_queue.qs[i];
3671
3672 assert(i >= 0);
3673
3674 ospf->lsa_refresh_queue.qs[i] = NULL;
3675
3676 if (refresh_list) {
3677 for (ALL_LIST_ELEMENTS(refresh_list, node, nnode,
3678 lsa)) {
3679 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3680 zlog_debug(
3681 "LSA[Refresh:Type%d:%s]: ospf_lsa_refresh_walker(): "
3682 "refresh lsa %p (slot %d)",
3683 lsa->data->type,
3684 inet_ntoa(lsa->data->id),
3685 (void *)lsa, i);
3686
3687 assert(lsa->lock > 0);
3688 list_delete_node(refresh_list, node);
3689 lsa->refresh_list = -1;
3690 listnode_add(lsa_to_refresh, lsa);
3691 }
3692 list_delete_and_null(&refresh_list);
3693 }
3694 }
3695
3696 ospf->t_lsa_refresher = NULL;
3697 thread_add_timer(master, ospf_lsa_refresh_walker, ospf,
3698 ospf->lsa_refresh_interval, &ospf->t_lsa_refresher);
3699 ospf->lsa_refresher_started = monotime(NULL);
3700
3701 for (ALL_LIST_ELEMENTS(lsa_to_refresh, node, nnode, lsa)) {
3702 ospf_lsa_refresh(ospf, lsa);
3703 assert(lsa->lock > 0);
3704 ospf_lsa_unlock(
3705 &lsa); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3706 }
3707
3708 list_delete_and_null(&lsa_to_refresh);
3709
3710 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3711 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3712
3713 return 0;
3714 }