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