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