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