2 * Copyright (C) 2003 Yasuhiro Ohara
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 /* Include other stuffs */
34 #include "ospf6_proto.h"
35 #include "ospf6_lsa.h"
36 #include "ospf6_lsdb.h"
37 #include "ospf6_message.h"
38 #include "ospf6_asbr.h"
39 #include "ospf6_zebra.h"
41 #include "ospf6_top.h"
42 #include "ospf6_area.h"
43 #include "ospf6_interface.h"
44 #include "ospf6_neighbor.h"
46 #include "ospf6_flood.h"
49 #ifndef VTYSH_EXTRACT_PL
50 #include "ospf6d/ospf6_lsa_clippy.c"
53 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_LSA
, "OSPF6 LSA");
54 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_LSA_HEADER
, "OSPF6 LSA header");
55 DEFINE_MTYPE_STATIC(OSPF6D
, OSPF6_LSA_SUMMARY
, "OSPF6 LSA summary");
57 static struct ospf6_lsa_handler
*lsa_handlers
[OSPF6_LSTYPE_SIZE
];
59 struct ospf6
*ospf6_get_by_lsdb(struct ospf6_lsa
*lsa
)
61 struct ospf6
*ospf6
= NULL
;
63 switch (OSPF6_LSA_SCOPE(lsa
->header
->type
)) {
64 case OSPF6_SCOPE_LINKLOCAL
:
65 ospf6
= OSPF6_INTERFACE(lsa
->lsdb
->data
)->area
->ospf6
;
67 case OSPF6_SCOPE_AREA
:
68 ospf6
= OSPF6_AREA(lsa
->lsdb
->data
)->ospf6
;
71 ospf6
= OSPF6_PROCESS(lsa
->lsdb
->data
);
80 static int ospf6_unknown_lsa_show(struct vty
*vty
, struct ospf6_lsa
*lsa
,
81 json_object
*json_obj
, bool use_json
)
83 uint8_t *start
, *end
, *current
;
85 start
= (uint8_t *)lsa
->header
+ sizeof(struct ospf6_lsa_header
);
86 end
= (uint8_t *)lsa
->header
+ ntohs(lsa
->header
->length
);
89 json_object_string_add(json_obj
, "LsaType", "unknown");
91 vty_out(vty
, " Unknown contents:\n");
92 for (current
= start
; current
< end
; current
++) {
93 if ((current
- start
) % 16 == 0)
95 else if ((current
- start
) % 4 == 0)
98 vty_out(vty
, "%02x", *current
);
101 vty_out(vty
, "\n\n");
106 static struct ospf6_lsa_handler unknown_handler
= {
107 .lh_type
= OSPF6_LSTYPE_UNKNOWN
,
108 .lh_name
= "Unknown",
109 .lh_short_name
= "Unk",
110 .lh_show
= ospf6_unknown_lsa_show
,
111 .lh_get_prefix_str
= NULL
,
112 .lh_debug
= 0 /* No default debug */
115 void ospf6_install_lsa_handler(struct ospf6_lsa_handler
*handler
)
117 /* type in handler is host byte order */
118 unsigned int index
= handler
->lh_type
& OSPF6_LSTYPE_FCODE_MASK
;
120 assertf(index
< array_size(lsa_handlers
), "index=%x", index
);
121 assertf(lsa_handlers
[index
] == NULL
, "old=%s, new=%s",
122 lsa_handlers
[index
]->lh_name
, handler
->lh_name
);
124 lsa_handlers
[index
] = handler
;
127 struct ospf6_lsa_handler
*ospf6_get_lsa_handler(uint16_t type
)
129 struct ospf6_lsa_handler
*handler
= NULL
;
130 unsigned int index
= ntohs(type
) & OSPF6_LSTYPE_FCODE_MASK
;
132 if (index
< array_size(lsa_handlers
))
133 handler
= lsa_handlers
[index
];
136 handler
= &unknown_handler
;
141 const char *ospf6_lstype_name(uint16_t type
)
144 const struct ospf6_lsa_handler
*handler
;
146 handler
= ospf6_get_lsa_handler(type
);
147 if (handler
&& handler
!= &unknown_handler
)
148 return handler
->lh_name
;
150 snprintf(buf
, sizeof(buf
), "0x%04hx", ntohs(type
));
154 const char *ospf6_lstype_short_name(uint16_t type
)
157 const struct ospf6_lsa_handler
*handler
;
159 handler
= ospf6_get_lsa_handler(type
);
161 return handler
->lh_short_name
;
163 snprintf(buf
, sizeof(buf
), "0x%04hx", ntohs(type
));
167 uint8_t ospf6_lstype_debug(uint16_t type
)
169 const struct ospf6_lsa_handler
*handler
;
170 handler
= ospf6_get_lsa_handler(type
);
171 return handler
->lh_debug
;
174 int metric_type(struct ospf6
*ospf6
, int type
, uint8_t instance
)
176 struct ospf6_redist
*red
;
178 red
= ospf6_redist_lookup(ospf6
, type
, instance
);
180 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
181 : red
->dmetric
.type
);
184 int metric_value(struct ospf6
*ospf6
, int type
, uint8_t instance
)
186 struct ospf6_redist
*red
;
188 red
= ospf6_redist_lookup(ospf6
, type
, instance
);
189 if (!red
|| red
->dmetric
.value
< 0) {
190 if (type
== DEFAULT_ROUTE
) {
191 if (ospf6
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
192 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
194 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
196 return DEFAULT_DEFAULT_METRIC
;
199 return red
->dmetric
.value
;
202 /* RFC2328: Section 13.2 */
203 int ospf6_lsa_is_differ(struct ospf6_lsa
*lsa1
, struct ospf6_lsa
*lsa2
)
207 assert(OSPF6_LSA_IS_SAME(lsa1
, lsa2
));
209 /* XXX, Options ??? */
211 ospf6_lsa_age_current(lsa1
);
212 ospf6_lsa_age_current(lsa2
);
213 if (ntohs(lsa1
->header
->age
) == OSPF_LSA_MAXAGE
214 && ntohs(lsa2
->header
->age
) != OSPF_LSA_MAXAGE
)
216 if (ntohs(lsa1
->header
->age
) != OSPF_LSA_MAXAGE
217 && ntohs(lsa2
->header
->age
) == OSPF_LSA_MAXAGE
)
221 if (ntohs(lsa1
->header
->length
) != ntohs(lsa2
->header
->length
))
224 len
= ntohs(lsa1
->header
->length
) - sizeof(struct ospf6_lsa_header
);
225 return memcmp(lsa1
->header
+ 1, lsa2
->header
+ 1, len
);
228 int ospf6_lsa_is_changed(struct ospf6_lsa
*lsa1
, struct ospf6_lsa
*lsa2
)
232 if (OSPF6_LSA_IS_MAXAGE(lsa1
) ^ OSPF6_LSA_IS_MAXAGE(lsa2
))
234 if (ntohs(lsa1
->header
->length
) != ntohs(lsa2
->header
->length
))
236 /* Going beyond LSA headers to compare the payload only makes sense,
237 * when both LSAs aren't header-only. */
238 if (CHECK_FLAG(lsa1
->flag
, OSPF6_LSA_HEADERONLY
)
239 != CHECK_FLAG(lsa2
->flag
, OSPF6_LSA_HEADERONLY
)) {
241 "%s: only one of two (%s, %s) LSAs compared is header-only",
242 __func__
, lsa1
->name
, lsa2
->name
);
245 if (CHECK_FLAG(lsa1
->flag
, OSPF6_LSA_HEADERONLY
))
248 length
= OSPF6_LSA_SIZE(lsa1
->header
) - sizeof(struct ospf6_lsa_header
);
249 /* Once upper layer verifies LSAs received, length underrun should
250 * become a warning. */
254 return memcmp(OSPF6_LSA_HEADER_END(lsa1
->header
),
255 OSPF6_LSA_HEADER_END(lsa2
->header
), length
);
258 /* ospf6 age functions */
259 /* calculate birth */
260 void ospf6_lsa_age_set(struct ospf6_lsa
*lsa
)
264 assert(lsa
&& lsa
->header
);
268 lsa
->birth
.tv_sec
= now
.tv_sec
- ntohs(lsa
->header
->age
);
269 lsa
->birth
.tv_usec
= now
.tv_usec
;
274 /* this function calculates current age from its birth,
275 then update age field of LSA header. return value is current age */
276 uint16_t ospf6_lsa_age_current(struct ospf6_lsa
*lsa
)
288 if (ntohs(lsa
->header
->age
) >= OSPF_LSA_MAXAGE
) {
289 /* ospf6_lsa_premature_aging () sets age to MAXAGE; when using
290 relative time, we cannot compare against lsa birth time, so
291 we catch this special case here. */
292 lsa
->header
->age
= htons(OSPF_LSA_MAXAGE
);
293 return OSPF_LSA_MAXAGE
;
296 ulage
= now
.tv_sec
- lsa
->birth
.tv_sec
;
298 /* if over MAXAGE, set to it */
299 age
= (ulage
> OSPF_LSA_MAXAGE
? OSPF_LSA_MAXAGE
: ulage
);
301 lsa
->header
->age
= htons(age
);
305 /* update age field of LSA header with adding InfTransDelay */
306 void ospf6_lsa_age_update_to_send(struct ospf6_lsa
*lsa
, uint32_t transdelay
)
310 age
= ospf6_lsa_age_current(lsa
) + transdelay
;
311 if (age
> OSPF_LSA_MAXAGE
)
312 age
= OSPF_LSA_MAXAGE
;
313 lsa
->header
->age
= htons(age
);
316 void ospf6_lsa_premature_aging(struct ospf6_lsa
*lsa
)
319 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa
->header
->type
))
320 zlog_debug("LSA: Premature aging: %s", lsa
->name
);
322 THREAD_OFF(lsa
->expire
);
323 THREAD_OFF(lsa
->refresh
);
326 * We clear the LSA from the neighbor retx lists now because it
327 * will not get deleted later. Essentially, changing the age to
328 * MaxAge will prevent this LSA from being matched with its
329 * existing entries in the retx list thereby causing those entries
330 * to be silently replaced with its MaxAged version, but with ever
331 * increasing retx count causing this LSA to remain forever and
332 * for the MaxAge remover thread to be called forever too.
334 * The reason the previous entry silently disappears is that when
335 * entry is added to a neighbor's retx list, it replaces the existing
336 * entry. But since the ospf6_lsdb_add() routine is generic and not
338 * of the special semantics of retx count, the retx count is not
339 * decremented when its replaced. Attempting to add the incr and decr
340 * retx count routines as the hook_add and hook_remove for the retx
342 * have a problem because the hook_remove routine is called for MaxAge
343 * entries (as will be the case in a traditional LSDB, unlike in this
345 * where an LSDB is used as an efficient tree structure to store all
347 * of data) that are added instead of calling the hook_add routine.
350 ospf6_flood_clear(lsa
);
352 lsa
->header
->age
= htons(OSPF_LSA_MAXAGE
);
353 thread_execute(master
, ospf6_lsa_expire
, lsa
, 0);
356 /* check which is more recent. if a is more recent, return -1;
357 if the same, return 0; otherwise(b is more recent), return 1 */
358 int ospf6_lsa_compare(struct ospf6_lsa
*a
, struct ospf6_lsa
*b
)
360 int32_t seqnuma
, seqnumb
;
361 uint16_t cksuma
, cksumb
;
364 assert(a
&& a
->header
);
365 assert(b
&& b
->header
);
366 assert(OSPF6_LSA_IS_SAME(a
, b
));
368 seqnuma
= (int32_t)ntohl(a
->header
->seqnum
);
369 seqnumb
= (int32_t)ntohl(b
->header
->seqnum
);
371 /* compare by sequence number */
372 if (seqnuma
> seqnumb
)
374 if (seqnuma
< seqnumb
)
378 cksuma
= ntohs(a
->header
->checksum
);
379 cksumb
= ntohs(b
->header
->checksum
);
386 agea
= ospf6_lsa_age_current(a
);
387 ageb
= ospf6_lsa_age_current(b
);
390 if (agea
== OSPF_LSA_MAXAGE
&& ageb
!= OSPF_LSA_MAXAGE
)
392 else if (agea
!= OSPF_LSA_MAXAGE
&& ageb
== OSPF_LSA_MAXAGE
)
396 if (agea
> ageb
&& agea
- ageb
>= OSPF_LSA_MAXAGE_DIFF
)
398 else if (agea
< ageb
&& ageb
- agea
>= OSPF_LSA_MAXAGE_DIFF
)
405 char *ospf6_lsa_printbuf(struct ospf6_lsa
*lsa
, char *buf
, int size
)
407 char id
[16], adv_router
[16];
408 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
409 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
411 snprintf(buf
, size
, "[%s Id:%s Adv:%s]",
412 ospf6_lstype_name(lsa
->header
->type
), id
, adv_router
);
416 void ospf6_lsa_header_print_raw(struct ospf6_lsa_header
*header
)
418 char id
[16], adv_router
[16];
419 inet_ntop(AF_INET
, &header
->id
, id
, sizeof(id
));
420 inet_ntop(AF_INET
, &header
->adv_router
, adv_router
, sizeof(adv_router
));
421 zlog_debug(" [%s Id:%s Adv:%s]", ospf6_lstype_name(header
->type
), id
,
423 zlog_debug(" Age: %4hu SeqNum: %#08lx Cksum: %04hx Len: %d",
424 ntohs(header
->age
), (unsigned long)ntohl(header
->seqnum
),
425 ntohs(header
->checksum
), ntohs(header
->length
));
428 void ospf6_lsa_header_print(struct ospf6_lsa
*lsa
)
430 ospf6_lsa_age_current(lsa
);
431 ospf6_lsa_header_print_raw(lsa
->header
);
434 void ospf6_lsa_show_summary_header(struct vty
*vty
)
436 vty_out(vty
, "%-4s %-15s%-15s%4s %8s %30s\n", "Type", "LSId",
437 "AdvRouter", "Age", "SeqNum", "Payload");
440 void ospf6_lsa_show_summary(struct vty
*vty
, struct ospf6_lsa
*lsa
,
441 json_object
*json_array
, bool use_json
)
443 char adv_router
[16], id
[16];
445 const struct ospf6_lsa_handler
*handler
;
448 json_object
*json_obj
= NULL
;
453 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
454 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
457 type
= ntohs(lsa
->header
->type
);
458 handler
= ospf6_get_lsa_handler(lsa
->header
->type
);
461 json_obj
= json_object_new_object();
464 case OSPF6_LSTYPE_INTER_PREFIX
:
465 case OSPF6_LSTYPE_INTER_ROUTER
:
466 case OSPF6_LSTYPE_AS_EXTERNAL
:
467 case OSPF6_LSTYPE_TYPE_7
:
469 json_object_string_add(
471 ospf6_lstype_short_name(lsa
->header
->type
));
472 json_object_string_add(json_obj
, "lsId", id
);
473 json_object_string_add(json_obj
, "advRouter",
475 json_object_int_add(json_obj
, "age",
476 ospf6_lsa_age_current(lsa
));
479 (unsigned long)ntohl(lsa
->header
->seqnum
));
480 json_object_string_add(
482 handler
->lh_get_prefix_str(lsa
, buf
,
484 json_object_array_add(json_array
, json_obj
);
486 vty_out(vty
, "%-4s %-15s%-15s%4hu %8lx %30s\n",
487 ospf6_lstype_short_name(lsa
->header
->type
), id
,
488 adv_router
, ospf6_lsa_age_current(lsa
),
489 (unsigned long)ntohl(lsa
->header
->seqnum
),
490 handler
->lh_get_prefix_str(lsa
, buf
,
493 case OSPF6_LSTYPE_ROUTER
:
494 case OSPF6_LSTYPE_NETWORK
:
495 case OSPF6_LSTYPE_GROUP_MEMBERSHIP
:
496 case OSPF6_LSTYPE_LINK
:
497 case OSPF6_LSTYPE_INTRA_PREFIX
:
498 while (handler
->lh_get_prefix_str(lsa
, buf
, sizeof(buf
), cnt
)
501 json_object_string_add(
503 ospf6_lstype_short_name(
505 json_object_string_add(json_obj
, "lsId", id
);
506 json_object_string_add(json_obj
, "advRouter",
508 json_object_int_add(json_obj
, "age",
509 ospf6_lsa_age_current(lsa
));
512 (unsigned long)ntohl(
513 lsa
->header
->seqnum
));
514 json_object_string_add(json_obj
, "payload",
516 json_object_array_add(json_array
, json_obj
);
517 json_obj
= json_object_new_object();
519 vty_out(vty
, "%-4s %-15s%-15s%4hu %8lx %30s\n",
520 ospf6_lstype_short_name(
523 ospf6_lsa_age_current(lsa
),
524 (unsigned long)ntohl(
525 lsa
->header
->seqnum
),
530 json_object_free(json_obj
);
534 json_object_string_add(
536 ospf6_lstype_short_name(lsa
->header
->type
));
537 json_object_string_add(json_obj
, "lsId", id
);
538 json_object_string_add(json_obj
, "advRouter",
540 json_object_int_add(json_obj
, "age",
541 ospf6_lsa_age_current(lsa
));
544 (unsigned long)ntohl(lsa
->header
->seqnum
));
545 json_object_array_add(json_array
, json_obj
);
547 vty_out(vty
, "%-4s %-15s%-15s%4hu %8lx\n",
548 ospf6_lstype_short_name(lsa
->header
->type
), id
,
549 adv_router
, ospf6_lsa_age_current(lsa
),
550 (unsigned long)ntohl(lsa
->header
->seqnum
));
555 void ospf6_lsa_show_dump(struct vty
*vty
, struct ospf6_lsa
*lsa
,
556 json_object
*json_array
, bool use_json
)
558 uint8_t *start
= NULL
;
560 uint8_t *current
= NULL
;
562 char *header_str
= NULL
;
563 char adv_router
[INET6_ADDRSTRLEN
];
564 char id
[INET6_ADDRSTRLEN
];
565 json_object
*json
= NULL
;
567 start
= (uint8_t *)lsa
->header
;
568 end
= (uint8_t *)lsa
->header
+ ntohs(lsa
->header
->length
);
571 json
= json_object_new_object();
572 size_t header_str_sz
= (2 * (end
- start
)) + 1;
574 header_str
= XMALLOC(MTYPE_TMP
, header_str_sz
);
576 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
577 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
580 frrstr_hex(header_str
, header_str_sz
, start
, end
- start
);
582 json_object_string_add(json
, "linkStateId", id
);
583 json_object_string_add(json
, "advertisingRouter", adv_router
);
584 json_object_string_add(json
, "header", header_str
);
585 json_object_array_add(json_array
, json
);
587 XFREE(MTYPE_TMP
, header_str
);
589 vty_out(vty
, "\n%s:\n", lsa
->name
);
591 for (current
= start
; current
< end
; current
++) {
592 if ((current
- start
) % 16 == 0)
594 else if ((current
- start
) % 4 == 0)
597 snprintf(byte
, sizeof(byte
), "%02x", *current
);
598 vty_out(vty
, "%s", byte
);
601 vty_out(vty
, "\n\n");
607 void ospf6_lsa_show_internal(struct vty
*vty
, struct ospf6_lsa
*lsa
,
608 json_object
*json_array
, bool use_json
)
610 char adv_router
[64], id
[64];
611 json_object
*json_obj
;
613 assert(lsa
&& lsa
->header
);
615 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
616 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
620 json_obj
= json_object_new_object();
621 json_object_int_add(json_obj
, "age",
622 ospf6_lsa_age_current(lsa
));
623 json_object_string_add(json_obj
, "type",
624 ospf6_lstype_name(lsa
->header
->type
));
625 json_object_string_add(json_obj
, "linkStateId", id
);
626 json_object_string_add(json_obj
, "advertisingRouter",
628 json_object_int_add(json_obj
, "lsSequenceNumber",
629 (unsigned long)ntohl(lsa
->header
->seqnum
));
630 json_object_int_add(json_obj
, "checksum",
631 ntohs(lsa
->header
->checksum
));
632 json_object_int_add(json_obj
, "length",
633 ntohs(lsa
->header
->length
));
634 json_object_int_add(json_obj
, "flag", lsa
->flag
);
635 json_object_int_add(json_obj
, "lock", lsa
->lock
);
636 json_object_int_add(json_obj
, "reTxCount", lsa
->retrans_count
);
638 /* Threads Data not added */
639 json_object_array_add(json_array
, json_obj
);
642 vty_out(vty
, "Age: %4hu Type: %s\n", ospf6_lsa_age_current(lsa
),
643 ospf6_lstype_name(lsa
->header
->type
));
644 vty_out(vty
, "Link State ID: %s\n", id
);
645 vty_out(vty
, "Advertising Router: %s\n", adv_router
);
646 vty_out(vty
, "LS Sequence Number: %#010lx\n",
647 (unsigned long)ntohl(lsa
->header
->seqnum
));
648 vty_out(vty
, "CheckSum: %#06hx Length: %hu\n",
649 ntohs(lsa
->header
->checksum
),
650 ntohs(lsa
->header
->length
));
651 vty_out(vty
, "Flag: %x \n", lsa
->flag
);
652 vty_out(vty
, "Lock: %d \n", lsa
->lock
);
653 vty_out(vty
, "ReTx Count: %d\n", lsa
->retrans_count
);
654 vty_out(vty
, "Threads: Expire: %p, Refresh: %p\n", lsa
->expire
,
661 void ospf6_lsa_show(struct vty
*vty
, struct ospf6_lsa
*lsa
,
662 json_object
*json_array
, bool use_json
)
664 char adv_router
[64], id
[64];
665 const struct ospf6_lsa_handler
*handler
;
666 struct timeval now
, res
;
668 json_object
*json_obj
= NULL
;
670 assert(lsa
&& lsa
->header
);
672 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
673 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
677 timersub(&now
, &lsa
->installed
, &res
);
678 timerstring(&res
, duration
, sizeof(duration
));
680 json_obj
= json_object_new_object();
681 json_object_int_add(json_obj
, "age",
682 ospf6_lsa_age_current(lsa
));
683 json_object_string_add(json_obj
, "type",
684 ospf6_lstype_name(lsa
->header
->type
));
685 json_object_string_add(json_obj
, "linkStateId", id
);
686 json_object_string_add(json_obj
, "advertisingRouter",
688 json_object_int_add(json_obj
, "lsSequenceNumber",
689 (unsigned long)ntohl(lsa
->header
->seqnum
));
690 json_object_int_add(json_obj
, "checksum",
691 ntohs(lsa
->header
->checksum
));
692 json_object_int_add(json_obj
, "length",
693 ntohs(lsa
->header
->length
));
694 json_object_string_add(json_obj
, "duration", duration
);
696 vty_out(vty
, "Age: %4hu Type: %s\n", ospf6_lsa_age_current(lsa
),
697 ospf6_lstype_name(lsa
->header
->type
));
698 vty_out(vty
, "Link State ID: %s\n", id
);
699 vty_out(vty
, "Advertising Router: %s\n", adv_router
);
700 vty_out(vty
, "LS Sequence Number: %#010lx\n",
701 (unsigned long)ntohl(lsa
->header
->seqnum
));
702 vty_out(vty
, "CheckSum: %#06hx Length: %hu\n",
703 ntohs(lsa
->header
->checksum
),
704 ntohs(lsa
->header
->length
));
705 vty_out(vty
, "Duration: %s\n", duration
);
708 handler
= ospf6_get_lsa_handler(lsa
->header
->type
);
710 if (handler
->lh_show
!= NULL
)
711 handler
->lh_show(vty
, lsa
, json_obj
, use_json
);
713 assert(unknown_handler
.lh_show
!= NULL
);
714 unknown_handler
.lh_show(vty
, lsa
, json_obj
, use_json
);
718 json_object_array_add(json_array
, json_obj
);
723 struct ospf6_lsa
*ospf6_lsa_alloc(size_t lsa_length
)
725 struct ospf6_lsa
*lsa
;
727 lsa
= XCALLOC(MTYPE_OSPF6_LSA
, sizeof(struct ospf6_lsa
));
728 lsa
->header
= XMALLOC(MTYPE_OSPF6_LSA_HEADER
, lsa_length
);
733 /* OSPFv3 LSA creation/deletion function */
734 struct ospf6_lsa
*ospf6_lsa_create(struct ospf6_lsa_header
*header
)
736 struct ospf6_lsa
*lsa
= NULL
;
737 uint16_t lsa_size
= 0;
739 /* size of the entire LSA */
740 lsa_size
= ntohs(header
->length
); /* XXX vulnerable */
742 lsa
= ospf6_lsa_alloc(lsa_size
);
744 /* copy LSA from original header */
745 memcpy(lsa
->header
, header
, lsa_size
);
748 ospf6_lsa_printbuf(lsa
, lsa
->name
, sizeof(lsa
->name
));
750 /* calculate birth of this lsa */
751 ospf6_lsa_age_set(lsa
);
756 struct ospf6_lsa
*ospf6_lsa_create_headeronly(struct ospf6_lsa_header
*header
)
758 struct ospf6_lsa
*lsa
= NULL
;
760 lsa
= ospf6_lsa_alloc(sizeof(struct ospf6_lsa_header
));
762 memcpy(lsa
->header
, header
, sizeof(struct ospf6_lsa_header
));
764 SET_FLAG(lsa
->flag
, OSPF6_LSA_HEADERONLY
);
767 ospf6_lsa_printbuf(lsa
, lsa
->name
, sizeof(lsa
->name
));
769 /* calculate birth of this lsa */
770 ospf6_lsa_age_set(lsa
);
775 void ospf6_lsa_delete(struct ospf6_lsa
*lsa
)
777 assert(lsa
->lock
== 0);
780 THREAD_OFF(lsa
->expire
);
781 THREAD_OFF(lsa
->refresh
);
784 XFREE(MTYPE_OSPF6_LSA_HEADER
, lsa
->header
);
785 XFREE(MTYPE_OSPF6_LSA
, lsa
);
788 struct ospf6_lsa
*ospf6_lsa_copy(struct ospf6_lsa
*lsa
)
790 struct ospf6_lsa
*copy
= NULL
;
792 ospf6_lsa_age_current(lsa
);
793 if (CHECK_FLAG(lsa
->flag
, OSPF6_LSA_HEADERONLY
))
794 copy
= ospf6_lsa_create_headeronly(lsa
->header
);
796 copy
= ospf6_lsa_create(lsa
->header
);
797 assert(copy
->lock
== 0);
799 copy
->birth
= lsa
->birth
;
800 copy
->originated
= lsa
->originated
;
801 copy
->received
= lsa
->received
;
802 copy
->installed
= lsa
->installed
;
803 copy
->lsdb
= lsa
->lsdb
;
809 /* increment reference counter of struct ospf6_lsa */
810 struct ospf6_lsa
*ospf6_lsa_lock(struct ospf6_lsa
*lsa
)
816 /* decrement reference counter of struct ospf6_lsa */
817 struct ospf6_lsa
*ospf6_lsa_unlock(struct ospf6_lsa
*lsa
)
819 /* decrement reference counter */
820 assert(lsa
->lock
> 0);
826 ospf6_lsa_delete(lsa
);
831 /* ospf6 lsa expiry */
832 int ospf6_lsa_expire(struct thread
*thread
)
834 struct ospf6_lsa
*lsa
;
837 lsa
= (struct ospf6_lsa
*)THREAD_ARG(thread
);
839 assert(lsa
&& lsa
->header
);
840 assert(OSPF6_LSA_IS_MAXAGE(lsa
));
841 assert(!lsa
->refresh
);
843 lsa
->expire
= (struct thread
*)NULL
;
845 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa
->header
->type
)) {
846 zlog_debug("LSA Expire:");
847 ospf6_lsa_header_print(lsa
);
850 if (CHECK_FLAG(lsa
->flag
, OSPF6_LSA_HEADERONLY
))
851 return 0; /* dbexchange will do something ... */
852 ospf6
= ospf6_get_by_lsdb(lsa
);
856 ospf6_install_lsa(lsa
);
859 ospf6_flood(NULL
, lsa
);
861 /* schedule maxage remover */
862 ospf6_maxage_remove(ospf6
);
867 int ospf6_lsa_refresh(struct thread
*thread
)
869 struct ospf6_lsa
*old
, *self
, *new;
870 struct ospf6_lsdb
*lsdb_self
;
872 old
= (struct ospf6_lsa
*)THREAD_ARG(thread
);
873 assert(old
&& old
->header
);
875 old
->refresh
= (struct thread
*)NULL
;
877 lsdb_self
= ospf6_get_scoped_lsdb_self(old
);
878 self
= ospf6_lsdb_lookup(old
->header
->type
, old
->header
->id
,
879 old
->header
->adv_router
, lsdb_self
);
881 if (IS_OSPF6_DEBUG_LSA_TYPE(old
->header
->type
))
882 zlog_debug("Refresh: could not find self LSA, flush %s",
884 ospf6_lsa_premature_aging(old
);
888 /* Reset age, increment LS sequence number. */
889 self
->header
->age
= htons(0);
890 self
->header
->seqnum
=
891 ospf6_new_ls_seqnum(self
->header
->type
, self
->header
->id
,
892 self
->header
->adv_router
, old
->lsdb
);
893 ospf6_lsa_checksum(self
->header
);
895 new = ospf6_lsa_create(self
->header
);
896 new->lsdb
= old
->lsdb
;
897 thread_add_timer(master
, ospf6_lsa_refresh
, new, OSPF_LS_REFRESH_TIME
,
900 /* store it in the LSDB for self-originated LSAs */
901 ospf6_lsdb_add(ospf6_lsa_copy(new), lsdb_self
);
903 if (IS_OSPF6_DEBUG_LSA_TYPE(new->header
->type
)) {
904 zlog_debug("LSA Refresh:");
905 ospf6_lsa_header_print(new);
908 ospf6_install_lsa(new);
909 ospf6_flood(NULL
, new);
914 void ospf6_flush_self_originated_lsas_now(struct ospf6
*ospf6
)
916 struct listnode
*node
, *nnode
;
917 struct ospf6_area
*oa
;
918 struct ospf6_lsa
*lsa
;
919 const struct route_node
*end
= NULL
;
920 uint32_t type
, adv_router
;
921 struct ospf6_interface
*oi
;
923 ospf6
->inst_shutdown
= 1;
925 for (ALL_LIST_ELEMENTS_RO(ospf6
->area_list
, node
, oa
)) {
926 end
= ospf6_lsdb_head(oa
->lsdb_self
, 0, 0, ospf6
->router_id
,
929 /* RFC 2328 (14.1): Set MAXAGE */
930 lsa
->header
->age
= htons(OSPF_LSA_MAXAGE
);
931 /* Flood MAXAGE LSA*/
932 ospf6_flood(NULL
, lsa
);
934 lsa
= ospf6_lsdb_next(end
, lsa
);
937 for (ALL_LIST_ELEMENTS(oa
->if_list
, node
, nnode
, oi
)) {
938 end
= ospf6_lsdb_head(oi
->lsdb_self
, 0, 0,
939 ospf6
->router_id
, &lsa
);
941 /* RFC 2328 (14.1): Set MAXAGE */
942 lsa
->header
->age
= htons(OSPF_LSA_MAXAGE
);
943 /* Flood MAXAGE LSA*/
944 ospf6_flood(NULL
, lsa
);
946 lsa
= ospf6_lsdb_next(end
, lsa
);
951 type
= htons(OSPF6_LSTYPE_AS_EXTERNAL
);
952 adv_router
= ospf6
->router_id
;
953 for (ALL_LSDB_TYPED_ADVRTR(ospf6
->lsdb
, type
, adv_router
, lsa
)) {
954 /* RFC 2328 (14.1): Set MAXAGE */
955 lsa
->header
->age
= htons(OSPF_LSA_MAXAGE
);
956 ospf6_flood(NULL
, lsa
);
960 /* Fletcher Checksum -- Refer to RFC1008. */
962 /* All the offsets are zero-based. The offsets in the RFC1008 are
964 unsigned short ospf6_lsa_checksum(struct ospf6_lsa_header
*lsa_header
)
966 uint8_t *buffer
= (uint8_t *)&lsa_header
->type
;
968 buffer
- (uint8_t *)&lsa_header
->age
; /* should be 2 */
970 /* Skip the AGE field */
971 uint16_t len
= ntohs(lsa_header
->length
) - type_offset
;
973 /* Checksum offset starts from "type" field, not the beginning of the
974 lsa_header struct. The offset is 14, rather than 16. */
975 int checksum_offset
= (uint8_t *)&lsa_header
->checksum
- buffer
;
977 return (unsigned short)fletcher_checksum(buffer
, len
, checksum_offset
);
980 int ospf6_lsa_checksum_valid(struct ospf6_lsa_header
*lsa_header
)
982 uint8_t *buffer
= (uint8_t *)&lsa_header
->type
;
984 buffer
- (uint8_t *)&lsa_header
->age
; /* should be 2 */
986 /* Skip the AGE field */
987 uint16_t len
= ntohs(lsa_header
->length
) - type_offset
;
989 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
993 void ospf6_lsa_init(void)
995 ospf6_install_lsa_handler(&unknown_handler
);
998 void ospf6_lsa_terminate(void)
1002 static char *ospf6_lsa_handler_name(const struct ospf6_lsa_handler
*h
)
1004 static char buf
[64];
1006 unsigned int size
= strlen(h
->lh_name
);
1008 if (!strcmp(h
->lh_name
, "unknown")
1009 && h
->lh_type
!= OSPF6_LSTYPE_UNKNOWN
) {
1010 snprintf(buf
, sizeof(buf
), "%#04hx", h
->lh_type
);
1014 for (i
= 0; i
< MIN(size
, sizeof(buf
)); i
++) {
1015 if (!islower((unsigned char)h
->lh_name
[i
]))
1016 buf
[i
] = tolower((unsigned char)h
->lh_name
[i
]);
1018 buf
[i
] = h
->lh_name
[i
];
1024 void ospf6_lsa_debug_set_all(bool val
)
1027 struct ospf6_lsa_handler
*handler
= NULL
;
1029 for (i
= 0; i
< array_size(lsa_handlers
); i
++) {
1030 handler
= lsa_handlers
[i
];
1031 if (handler
== NULL
)
1034 SET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_ALL
);
1036 UNSET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_ALL
);
1040 DEFPY (debug_ospf6_lsa_all
,
1041 debug_ospf6_lsa_all_cmd
,
1042 "[no$no] debug ospf6 lsa all",
1046 "Debug Link State Advertisements (LSAs)\n"
1047 "Display for all types of LSAs\n")
1049 ospf6_lsa_debug_set_all(!no
);
1053 DEFPY (debug_ospf6_lsa_aggregation
,
1054 debug_ospf6_lsa_aggregation_cmd
,
1055 "[no] debug ospf6 lsa aggregation",
1059 "Debug Link State Advertisements (LSAs)\n"
1060 "External LSA Aggregation\n")
1063 struct ospf6_lsa_handler
*handler
;
1065 handler
= ospf6_get_lsa_handler(OSPF6_LSTYPE_AS_EXTERNAL
);
1066 if (handler
== NULL
)
1067 return CMD_WARNING_CONFIG_FAILED
;
1070 UNSET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_AGGR
);
1072 SET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_AGGR
);
1077 DEFUN (debug_ospf6_lsa_type
,
1078 debug_ospf6_lsa_hex_cmd
,
1079 "debug ospf6 lsa <router|network|inter-prefix|inter-router|as-external|nssa|link|intra-prefix|unknown> [<originate|examine|flooding>]",
1082 "Debug Link State Advertisements (LSAs)\n"
1083 "Display Router LSAs\n"
1084 "Display Network LSAs\n"
1085 "Display Inter-Area-Prefix LSAs\n"
1086 "Display Inter-Router LSAs\n"
1087 "Display As-External LSAs\n"
1088 "Display NSSA LSAs\n"
1089 "Display Link LSAs\n"
1090 "Display Intra-Area-Prefix LSAs\n"
1091 "Display LSAs of unknown origin\n"
1092 "Display details of LSAs\n"
1094 "Display LSA's internal information\n")
1099 struct ospf6_lsa_handler
*handler
= NULL
;
1101 for (i
= 0; i
< array_size(lsa_handlers
); i
++) {
1102 handler
= lsa_handlers
[i
];
1103 if (handler
== NULL
)
1105 if (strncmp(argv
[idx_lsa
]->arg
, ospf6_lsa_handler_name(handler
),
1106 strlen(argv
[idx_lsa
]->arg
))
1109 if (!strcasecmp(argv
[idx_lsa
]->arg
, handler
->lh_name
))
1114 if (handler
== NULL
)
1115 handler
= &unknown_handler
;
1118 if (strmatch(argv
[idx_type
]->text
, "originate"))
1119 SET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_ORIGINATE
);
1120 else if (strmatch(argv
[idx_type
]->text
, "examine"))
1121 SET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_EXAMIN
);
1122 else if (strmatch(argv
[idx_type
]->text
, "flooding"))
1123 SET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_FLOOD
);
1125 SET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG
);
1130 DEFUN (no_debug_ospf6_lsa_type
,
1131 no_debug_ospf6_lsa_hex_cmd
,
1132 "no debug ospf6 lsa <router|network|inter-prefix|inter-router|as-external|nssa|link|intra-prefix|unknown> [<originate|examine|flooding>]",
1136 "Debug Link State Advertisements (LSAs)\n"
1137 "Display Router LSAs\n"
1138 "Display Network LSAs\n"
1139 "Display Inter-Area-Prefix LSAs\n"
1140 "Display Inter-Router LSAs\n"
1141 "Display As-External LSAs\n"
1142 "Display NSSA LSAs\n"
1143 "Display Link LSAs\n"
1144 "Display Intra-Area-Prefix LSAs\n"
1145 "Display LSAs of unknown origin\n"
1146 "Display details of LSAs\n"
1148 "Display LSA's internal information\n")
1153 struct ospf6_lsa_handler
*handler
= NULL
;
1155 for (i
= 0; i
< array_size(lsa_handlers
); i
++) {
1156 handler
= lsa_handlers
[i
];
1157 if (handler
== NULL
)
1159 if (strncmp(argv
[idx_lsa
]->arg
, ospf6_lsa_handler_name(handler
),
1160 strlen(argv
[idx_lsa
]->arg
))
1163 if (!strcasecmp(argv
[idx_lsa
]->arg
, handler
->lh_name
))
1167 if (handler
== NULL
)
1171 if (strmatch(argv
[idx_type
]->text
, "originate"))
1172 UNSET_FLAG(handler
->lh_debug
,
1173 OSPF6_LSA_DEBUG_ORIGINATE
);
1174 if (strmatch(argv
[idx_type
]->text
, "examine"))
1175 UNSET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_EXAMIN
);
1176 if (strmatch(argv
[idx_type
]->text
, "flooding"))
1177 UNSET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_FLOOD
);
1179 UNSET_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG
);
1184 void install_element_ospf6_debug_lsa(void)
1186 install_element(ENABLE_NODE
, &debug_ospf6_lsa_all_cmd
);
1187 install_element(CONFIG_NODE
, &debug_ospf6_lsa_all_cmd
);
1188 install_element(ENABLE_NODE
, &debug_ospf6_lsa_hex_cmd
);
1189 install_element(ENABLE_NODE
, &no_debug_ospf6_lsa_hex_cmd
);
1190 install_element(CONFIG_NODE
, &debug_ospf6_lsa_hex_cmd
);
1191 install_element(CONFIG_NODE
, &no_debug_ospf6_lsa_hex_cmd
);
1193 install_element(ENABLE_NODE
, &debug_ospf6_lsa_aggregation_cmd
);
1194 install_element(CONFIG_NODE
, &debug_ospf6_lsa_aggregation_cmd
);
1197 int config_write_ospf6_debug_lsa(struct vty
*vty
)
1200 const struct ospf6_lsa_handler
*handler
;
1201 bool debug_all
= true;
1203 for (i
= 0; i
< array_size(lsa_handlers
); i
++) {
1204 handler
= lsa_handlers
[i
];
1205 if (handler
== NULL
)
1207 if (CHECK_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_ALL
)
1208 < OSPF6_LSA_DEBUG_ALL
) {
1215 vty_out(vty
, "debug ospf6 lsa all\n");
1219 for (i
= 0; i
< array_size(lsa_handlers
); i
++) {
1220 handler
= lsa_handlers
[i
];
1221 if (handler
== NULL
)
1223 if (CHECK_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG
))
1224 vty_out(vty
, "debug ospf6 lsa %s\n",
1225 ospf6_lsa_handler_name(handler
));
1226 if (CHECK_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_ORIGINATE
))
1227 vty_out(vty
, "debug ospf6 lsa %s originate\n",
1228 ospf6_lsa_handler_name(handler
));
1229 if (CHECK_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_EXAMIN
))
1230 vty_out(vty
, "debug ospf6 lsa %s examine\n",
1231 ospf6_lsa_handler_name(handler
));
1232 if (CHECK_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_FLOOD
))
1233 vty_out(vty
, "debug ospf6 lsa %s flooding\n",
1234 ospf6_lsa_handler_name(handler
));
1235 if (CHECK_FLAG(handler
->lh_debug
, OSPF6_LSA_DEBUG_AGGR
))
1236 vty_out(vty
, "debug ospf6 lsa aggregation\n");