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