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 */
33 #include "ospf6_proto.h"
34 #include "ospf6_lsa.h"
35 #include "ospf6_lsdb.h"
36 #include "ospf6_message.h"
38 #include "ospf6_top.h"
39 #include "ospf6_area.h"
40 #include "ospf6_interface.h"
41 #include "ospf6_neighbor.h"
43 #include "ospf6_flood.h"
46 vector ospf6_lsa_handler_vector
;
48 static int ospf6_unknown_lsa_show(struct vty
*vty
, struct ospf6_lsa
*lsa
)
50 u_char
*start
, *end
, *current
;
53 start
= (u_char
*)lsa
->header
+ sizeof(struct ospf6_lsa_header
);
54 end
= (u_char
*)lsa
->header
+ ntohs(lsa
->header
->length
);
56 vty_out(vty
, " Unknown contents:\n");
57 for (current
= start
; current
< end
; current
++) {
58 if ((current
- start
) % 16 == 0)
60 else if ((current
- start
) % 4 == 0)
63 snprintf(byte
, sizeof(byte
), "%02x", *current
);
64 vty_out(vty
, "%s", byte
);
71 struct ospf6_lsa_handler unknown_handler
= {
72 OSPF6_LSTYPE_UNKNOWN
, "Unknown", "Unk", ospf6_unknown_lsa_show
, NULL
};
74 void ospf6_install_lsa_handler(struct ospf6_lsa_handler
*handler
)
76 /* type in handler is host byte order */
77 int index
= handler
->type
& OSPF6_LSTYPE_FCODE_MASK
;
78 vector_set_index(ospf6_lsa_handler_vector
, index
, handler
);
81 struct ospf6_lsa_handler
*ospf6_get_lsa_handler(u_int16_t type
)
83 struct ospf6_lsa_handler
*handler
= NULL
;
84 unsigned int index
= ntohs(type
) & OSPF6_LSTYPE_FCODE_MASK
;
86 if (index
>= vector_active(ospf6_lsa_handler_vector
))
87 handler
= &unknown_handler
;
89 handler
= vector_slot(ospf6_lsa_handler_vector
, index
);
92 handler
= &unknown_handler
;
97 const char *ospf6_lstype_name(u_int16_t type
)
100 struct ospf6_lsa_handler
*handler
;
102 handler
= ospf6_get_lsa_handler(type
);
103 if (handler
&& handler
!= &unknown_handler
)
104 return handler
->name
;
106 snprintf(buf
, sizeof(buf
), "0x%04hx", ntohs(type
));
110 const char *ospf6_lstype_short_name(u_int16_t type
)
113 struct ospf6_lsa_handler
*handler
;
115 handler
= ospf6_get_lsa_handler(type
);
116 if (handler
&& handler
!= &unknown_handler
)
117 return handler
->short_name
;
119 snprintf(buf
, sizeof(buf
), "0x%04hx", ntohs(type
));
123 u_char
ospf6_lstype_debug(u_int16_t type
)
125 struct ospf6_lsa_handler
*handler
;
126 handler
= ospf6_get_lsa_handler(type
);
127 return handler
->debug
;
130 /* RFC2328: Section 13.2 */
131 int ospf6_lsa_is_differ(struct ospf6_lsa
*lsa1
, struct ospf6_lsa
*lsa2
)
135 assert(OSPF6_LSA_IS_SAME(lsa1
, lsa2
));
137 /* XXX, Options ??? */
139 ospf6_lsa_age_current(lsa1
);
140 ospf6_lsa_age_current(lsa2
);
141 if (ntohs(lsa1
->header
->age
) == OSPF_LSA_MAXAGE
142 && ntohs(lsa2
->header
->age
) != OSPF_LSA_MAXAGE
)
144 if (ntohs(lsa1
->header
->age
) != OSPF_LSA_MAXAGE
145 && ntohs(lsa2
->header
->age
) == OSPF_LSA_MAXAGE
)
149 if (ntohs(lsa1
->header
->length
) != ntohs(lsa2
->header
->length
))
152 len
= ntohs(lsa1
->header
->length
) - sizeof(struct ospf6_lsa_header
);
153 return memcmp(lsa1
->header
+ 1, lsa2
->header
+ 1, len
);
156 int ospf6_lsa_is_changed(struct ospf6_lsa
*lsa1
, struct ospf6_lsa
*lsa2
)
160 if (OSPF6_LSA_IS_MAXAGE(lsa1
) ^ OSPF6_LSA_IS_MAXAGE(lsa2
))
162 if (ntohs(lsa1
->header
->length
) != ntohs(lsa2
->header
->length
))
164 /* Going beyond LSA headers to compare the payload only makes sense,
165 * when both LSAs aren't header-only. */
166 if (CHECK_FLAG(lsa1
->flag
, OSPF6_LSA_HEADERONLY
)
167 != CHECK_FLAG(lsa2
->flag
, OSPF6_LSA_HEADERONLY
)) {
169 "%s: only one of two (%s, %s) LSAs compared is header-only",
170 __func__
, lsa1
->name
, lsa2
->name
);
173 if (CHECK_FLAG(lsa1
->flag
, OSPF6_LSA_HEADERONLY
))
176 length
= OSPF6_LSA_SIZE(lsa1
->header
) - sizeof(struct ospf6_lsa_header
);
177 /* Once upper layer verifies LSAs received, length underrun should
178 * become a warning. */
182 return memcmp(OSPF6_LSA_HEADER_END(lsa1
->header
),
183 OSPF6_LSA_HEADER_END(lsa2
->header
), length
);
186 /* ospf6 age functions */
187 /* calculate birth */
188 static void ospf6_lsa_age_set(struct ospf6_lsa
*lsa
)
192 assert(lsa
&& lsa
->header
);
196 lsa
->birth
.tv_sec
= now
.tv_sec
- ntohs(lsa
->header
->age
);
197 lsa
->birth
.tv_usec
= now
.tv_usec
;
202 /* this function calculates current age from its birth,
203 then update age field of LSA header. return value is current age */
204 u_int16_t
ospf6_lsa_age_current(struct ospf6_lsa
*lsa
)
216 if (ntohs(lsa
->header
->age
) >= OSPF_LSA_MAXAGE
) {
217 /* ospf6_lsa_premature_aging () sets age to MAXAGE; when using
218 relative time, we cannot compare against lsa birth time, so
219 we catch this special case here. */
220 lsa
->header
->age
= htons(OSPF_LSA_MAXAGE
);
221 return OSPF_LSA_MAXAGE
;
224 ulage
= now
.tv_sec
- lsa
->birth
.tv_sec
;
226 /* if over MAXAGE, set to it */
227 age
= (ulage
> OSPF_LSA_MAXAGE
? OSPF_LSA_MAXAGE
: ulage
);
229 lsa
->header
->age
= htons(age
);
233 /* update age field of LSA header with adding InfTransDelay */
234 void ospf6_lsa_age_update_to_send(struct ospf6_lsa
*lsa
, u_int32_t transdelay
)
238 age
= ospf6_lsa_age_current(lsa
) + transdelay
;
239 if (age
> OSPF_LSA_MAXAGE
)
240 age
= OSPF_LSA_MAXAGE
;
241 lsa
->header
->age
= htons(age
);
244 void ospf6_lsa_premature_aging(struct ospf6_lsa
*lsa
)
247 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa
->header
->type
))
248 zlog_debug("LSA: Premature aging: %s", lsa
->name
);
250 THREAD_OFF(lsa
->expire
);
251 THREAD_OFF(lsa
->refresh
);
254 * We clear the LSA from the neighbor retx lists now because it
255 * will not get deleted later. Essentially, changing the age to
256 * MaxAge will prevent this LSA from being matched with its
257 * existing entries in the retx list thereby causing those entries
258 * to be silently replaced with its MaxAged version, but with ever
259 * increasing retx count causing this LSA to remain forever and
260 * for the MaxAge remover thread to be called forever too.
262 * The reason the previous entry silently disappears is that when
263 * entry is added to a neighbor's retx list, it replaces the existing
264 * entry. But since the ospf6_lsdb_add() routine is generic and not
266 * of the special semantics of retx count, the retx count is not
267 * decremented when its replaced. Attempting to add the incr and decr
268 * retx count routines as the hook_add and hook_remove for the retx
270 * have a problem because the hook_remove routine is called for MaxAge
271 * entries (as will be the case in a traditional LSDB, unlike in this
273 * where an LSDB is used as an efficient tree structure to store all
275 * of data) that are added instead of calling the hook_add routine.
278 ospf6_flood_clear(lsa
);
280 lsa
->header
->age
= htons(OSPF_LSA_MAXAGE
);
281 thread_execute(master
, ospf6_lsa_expire
, lsa
, 0);
284 /* check which is more recent. if a is more recent, return -1;
285 if the same, return 0; otherwise(b is more recent), return 1 */
286 int ospf6_lsa_compare(struct ospf6_lsa
*a
, struct ospf6_lsa
*b
)
288 int32_t seqnuma
, seqnumb
;
289 u_int16_t cksuma
, cksumb
;
290 u_int16_t agea
, ageb
;
292 assert(a
&& a
->header
);
293 assert(b
&& b
->header
);
294 assert(OSPF6_LSA_IS_SAME(a
, b
));
296 seqnuma
= (int32_t)ntohl(a
->header
->seqnum
);
297 seqnumb
= (int32_t)ntohl(b
->header
->seqnum
);
299 /* compare by sequence number */
300 if (seqnuma
> seqnumb
)
302 if (seqnuma
< seqnumb
)
306 cksuma
= ntohs(a
->header
->checksum
);
307 cksumb
= ntohs(b
->header
->checksum
);
314 agea
= ospf6_lsa_age_current(a
);
315 ageb
= ospf6_lsa_age_current(b
);
318 if (agea
== OSPF_LSA_MAXAGE
&& ageb
!= OSPF_LSA_MAXAGE
)
320 else if (agea
!= OSPF_LSA_MAXAGE
&& ageb
== OSPF_LSA_MAXAGE
)
324 if (agea
> ageb
&& agea
- ageb
>= OSPF_LSA_MAXAGE_DIFF
)
326 else if (agea
< ageb
&& ageb
- agea
>= OSPF_LSA_MAXAGE_DIFF
)
333 char *ospf6_lsa_printbuf(struct ospf6_lsa
*lsa
, char *buf
, int size
)
335 char id
[16], adv_router
[16];
336 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
337 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
339 snprintf(buf
, size
, "[%s Id:%s Adv:%s]",
340 ospf6_lstype_name(lsa
->header
->type
), id
, adv_router
);
344 void ospf6_lsa_header_print_raw(struct ospf6_lsa_header
*header
)
346 char id
[16], adv_router
[16];
347 inet_ntop(AF_INET
, &header
->id
, id
, sizeof(id
));
348 inet_ntop(AF_INET
, &header
->adv_router
, adv_router
, sizeof(adv_router
));
349 zlog_debug(" [%s Id:%s Adv:%s]", ospf6_lstype_name(header
->type
), id
,
351 zlog_debug(" Age: %4hu SeqNum: %#08lx Cksum: %04hx Len: %d",
352 ntohs(header
->age
), (u_long
)ntohl(header
->seqnum
),
353 ntohs(header
->checksum
), ntohs(header
->length
));
356 void ospf6_lsa_header_print(struct ospf6_lsa
*lsa
)
358 ospf6_lsa_age_current(lsa
);
359 ospf6_lsa_header_print_raw(lsa
->header
);
362 void ospf6_lsa_show_summary_header(struct vty
*vty
)
364 vty_out(vty
, "%-4s %-15s%-15s%4s %8s %30s\n", "Type", "LSId",
365 "AdvRouter", "Age", "SeqNum", "Payload");
368 void ospf6_lsa_show_summary(struct vty
*vty
, struct ospf6_lsa
*lsa
)
370 char adv_router
[16], id
[16];
372 struct ospf6_lsa_handler
*handler
;
373 char buf
[64], tmpbuf
[80];
379 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
380 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
383 type
= ntohs(lsa
->header
->type
);
384 handler
= ospf6_get_lsa_handler(lsa
->header
->type
);
385 if ((type
== OSPF6_LSTYPE_INTER_PREFIX
)
386 || (type
== OSPF6_LSTYPE_INTER_ROUTER
)
387 || (type
== OSPF6_LSTYPE_AS_EXTERNAL
)) {
388 vty_out(vty
, "%-4s %-15s%-15s%4hu %8lx %30s\n",
389 ospf6_lstype_short_name(lsa
->header
->type
), id
,
390 adv_router
, ospf6_lsa_age_current(lsa
),
391 (u_long
)ntohl(lsa
->header
->seqnum
),
392 handler
->get_prefix_str(lsa
, buf
, sizeof(buf
), 0));
393 } else if (type
!= OSPF6_LSTYPE_UNKNOWN
) {
394 sprintf(tmpbuf
, "%-4s %-15s%-15s%4hu %8lx",
395 ospf6_lstype_short_name(lsa
->header
->type
), id
,
396 adv_router
, ospf6_lsa_age_current(lsa
),
397 (u_long
)ntohl(lsa
->header
->seqnum
));
399 while (handler
->get_prefix_str(lsa
, buf
, sizeof(buf
), cnt
)
401 vty_out(vty
, "%s %30s\n", tmpbuf
, buf
);
405 vty_out(vty
, "%-4s %-15s%-15s%4hu %8lx\n",
406 ospf6_lstype_short_name(lsa
->header
->type
), id
,
407 adv_router
, ospf6_lsa_age_current(lsa
),
408 (u_long
)ntohl(lsa
->header
->seqnum
));
412 void ospf6_lsa_show_dump(struct vty
*vty
, struct ospf6_lsa
*lsa
)
414 u_char
*start
, *end
, *current
;
417 start
= (u_char
*)lsa
->header
;
418 end
= (u_char
*)lsa
->header
+ ntohs(lsa
->header
->length
);
421 vty_out(vty
, "%s:\n", lsa
->name
);
423 for (current
= start
; current
< end
; current
++) {
424 if ((current
- start
) % 16 == 0)
426 else if ((current
- start
) % 4 == 0)
429 snprintf(byte
, sizeof(byte
), "%02x", *current
);
430 vty_out(vty
, "%s", byte
);
433 vty_out(vty
, "\n\n");
437 void ospf6_lsa_show_internal(struct vty
*vty
, struct ospf6_lsa
*lsa
)
439 char adv_router
[64], id
[64];
441 assert(lsa
&& lsa
->header
);
443 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
444 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
448 vty_out(vty
, "Age: %4hu Type: %s\n", ospf6_lsa_age_current(lsa
),
449 ospf6_lstype_name(lsa
->header
->type
));
450 vty_out(vty
, "Link State ID: %s\n", id
);
451 vty_out(vty
, "Advertising Router: %s\n", adv_router
);
452 vty_out(vty
, "LS Sequence Number: %#010lx\n",
453 (u_long
)ntohl(lsa
->header
->seqnum
));
454 vty_out(vty
, "CheckSum: %#06hx Length: %hu\n",
455 ntohs(lsa
->header
->checksum
), ntohs(lsa
->header
->length
));
456 vty_out(vty
, "Flag: %x \n", lsa
->flag
);
457 vty_out(vty
, "Lock: %d \n", lsa
->lock
);
458 vty_out(vty
, "ReTx Count: %d\n", lsa
->retrans_count
);
459 vty_out(vty
, "Threads: Expire: 0x%p, Refresh: 0x%p \n",
460 (void *)lsa
->expire
, (void *)lsa
->refresh
);
465 void ospf6_lsa_show(struct vty
*vty
, struct ospf6_lsa
*lsa
)
467 char adv_router
[64], id
[64];
468 struct ospf6_lsa_handler
*handler
;
469 struct timeval now
, res
;
472 assert(lsa
&& lsa
->header
);
474 inet_ntop(AF_INET
, &lsa
->header
->id
, id
, sizeof(id
));
475 inet_ntop(AF_INET
, &lsa
->header
->adv_router
, adv_router
,
479 timersub(&now
, &lsa
->installed
, &res
);
480 timerstring(&res
, duration
, sizeof(duration
));
482 vty_out(vty
, "Age: %4hu Type: %s\n", ospf6_lsa_age_current(lsa
),
483 ospf6_lstype_name(lsa
->header
->type
));
484 vty_out(vty
, "Link State ID: %s\n", id
);
485 vty_out(vty
, "Advertising Router: %s\n", adv_router
);
486 vty_out(vty
, "LS Sequence Number: %#010lx\n",
487 (u_long
)ntohl(lsa
->header
->seqnum
));
488 vty_out(vty
, "CheckSum: %#06hx Length: %hu\n",
489 ntohs(lsa
->header
->checksum
), ntohs(lsa
->header
->length
));
490 vty_out(vty
, "Duration: %s\n", duration
);
492 handler
= ospf6_get_lsa_handler(lsa
->header
->type
);
493 if (handler
->show
== NULL
)
494 handler
= &unknown_handler
;
495 (*handler
->show
)(vty
, lsa
);
500 /* OSPFv3 LSA creation/deletion function */
501 struct ospf6_lsa
*ospf6_lsa_create(struct ospf6_lsa_header
*header
)
503 struct ospf6_lsa
*lsa
= NULL
;
504 struct ospf6_lsa_header
*new_header
= NULL
;
505 u_int16_t lsa_size
= 0;
507 /* size of the entire LSA */
508 lsa_size
= ntohs(header
->length
); /* XXX vulnerable */
510 /* allocate memory for this LSA */
512 (struct ospf6_lsa_header
*)XMALLOC(MTYPE_OSPF6_LSA
, lsa_size
);
514 /* copy LSA from original header */
515 memcpy(new_header
, header
, lsa_size
);
517 /* LSA information structure */
518 /* allocate memory */
519 lsa
= (struct ospf6_lsa
*)XCALLOC(MTYPE_OSPF6_LSA
,
520 sizeof(struct ospf6_lsa
));
522 lsa
->header
= (struct ospf6_lsa_header
*)new_header
;
525 ospf6_lsa_printbuf(lsa
, lsa
->name
, sizeof(lsa
->name
));
527 /* calculate birth of this lsa */
528 ospf6_lsa_age_set(lsa
);
533 struct ospf6_lsa
*ospf6_lsa_create_headeronly(struct ospf6_lsa_header
*header
)
535 struct ospf6_lsa
*lsa
= NULL
;
536 struct ospf6_lsa_header
*new_header
= NULL
;
538 /* allocate memory for this LSA */
539 new_header
= (struct ospf6_lsa_header
*)XMALLOC(
540 MTYPE_OSPF6_LSA
, sizeof(struct ospf6_lsa_header
));
542 /* copy LSA from original header */
543 memcpy(new_header
, header
, sizeof(struct ospf6_lsa_header
));
545 /* LSA information structure */
546 /* allocate memory */
547 lsa
= (struct ospf6_lsa
*)XCALLOC(MTYPE_OSPF6_LSA
,
548 sizeof(struct ospf6_lsa
));
550 lsa
->header
= (struct ospf6_lsa_header
*)new_header
;
551 SET_FLAG(lsa
->flag
, OSPF6_LSA_HEADERONLY
);
554 ospf6_lsa_printbuf(lsa
, lsa
->name
, sizeof(lsa
->name
));
556 /* calculate birth of this lsa */
557 ospf6_lsa_age_set(lsa
);
562 void ospf6_lsa_delete(struct ospf6_lsa
*lsa
)
564 assert(lsa
->lock
== 0);
567 THREAD_OFF(lsa
->expire
);
568 THREAD_OFF(lsa
->refresh
);
571 XFREE(MTYPE_OSPF6_LSA
, lsa
->header
);
572 XFREE(MTYPE_OSPF6_LSA
, lsa
);
575 struct ospf6_lsa
*ospf6_lsa_copy(struct ospf6_lsa
*lsa
)
577 struct ospf6_lsa
*copy
= NULL
;
579 ospf6_lsa_age_current(lsa
);
580 if (CHECK_FLAG(lsa
->flag
, OSPF6_LSA_HEADERONLY
))
581 copy
= ospf6_lsa_create_headeronly(lsa
->header
);
583 copy
= ospf6_lsa_create(lsa
->header
);
584 assert(copy
->lock
== 0);
586 copy
->birth
= lsa
->birth
;
587 copy
->originated
= lsa
->originated
;
588 copy
->received
= lsa
->received
;
589 copy
->installed
= lsa
->installed
;
590 copy
->lsdb
= lsa
->lsdb
;
596 /* increment reference counter of struct ospf6_lsa */
597 void ospf6_lsa_lock(struct ospf6_lsa
*lsa
)
603 /* decrement reference counter of struct ospf6_lsa */
604 void ospf6_lsa_unlock(struct ospf6_lsa
*lsa
)
606 /* decrement reference counter */
607 assert(lsa
->lock
> 0);
613 ospf6_lsa_delete(lsa
);
617 /* ospf6 lsa expiry */
618 int ospf6_lsa_expire(struct thread
*thread
)
620 struct ospf6_lsa
*lsa
;
622 lsa
= (struct ospf6_lsa
*)THREAD_ARG(thread
);
624 assert(lsa
&& lsa
->header
);
625 assert(OSPF6_LSA_IS_MAXAGE(lsa
));
626 assert(!lsa
->refresh
);
628 lsa
->expire
= (struct thread
*)NULL
;
630 if (IS_OSPF6_DEBUG_LSA_TYPE(lsa
->header
->type
)) {
631 zlog_debug("LSA Expire:");
632 ospf6_lsa_header_print(lsa
);
635 if (CHECK_FLAG(lsa
->flag
, OSPF6_LSA_HEADERONLY
))
636 return 0; /* dbexchange will do something ... */
639 ospf6_install_lsa(lsa
);
642 ospf6_flood(NULL
, lsa
);
644 /* schedule maxage remover */
645 ospf6_maxage_remove(ospf6
);
650 int ospf6_lsa_refresh(struct thread
*thread
)
652 struct ospf6_lsa
*old
, *self
, *new;
653 struct ospf6_lsdb
*lsdb_self
;
655 old
= (struct ospf6_lsa
*)THREAD_ARG(thread
);
656 assert(old
&& old
->header
);
658 old
->refresh
= (struct thread
*)NULL
;
660 lsdb_self
= ospf6_get_scoped_lsdb_self(old
);
661 self
= ospf6_lsdb_lookup(old
->header
->type
, old
->header
->id
,
662 old
->header
->adv_router
, lsdb_self
);
664 if (IS_OSPF6_DEBUG_LSA_TYPE(old
->header
->type
))
665 zlog_debug("Refresh: could not find self LSA, flush %s",
667 ospf6_lsa_premature_aging(old
);
671 /* Reset age, increment LS sequence number. */
672 self
->header
->age
= htons(0);
673 self
->header
->seqnum
=
674 ospf6_new_ls_seqnum(self
->header
->type
, self
->header
->id
,
675 self
->header
->adv_router
, old
->lsdb
);
676 ospf6_lsa_checksum(self
->header
);
678 new = ospf6_lsa_create(self
->header
);
679 new->lsdb
= old
->lsdb
;
681 thread_add_timer(master
, ospf6_lsa_refresh
, new, OSPF_LS_REFRESH_TIME
,
684 /* store it in the LSDB for self-originated LSAs */
685 ospf6_lsdb_add(ospf6_lsa_copy(new), lsdb_self
);
687 if (IS_OSPF6_DEBUG_LSA_TYPE(new->header
->type
)) {
688 zlog_debug("LSA Refresh:");
689 ospf6_lsa_header_print(new);
692 ospf6_install_lsa(new);
693 ospf6_flood(NULL
, new);
699 /* Fletcher Checksum -- Refer to RFC1008. */
701 /* All the offsets are zero-based. The offsets in the RFC1008 are
703 unsigned short ospf6_lsa_checksum(struct ospf6_lsa_header
*lsa_header
)
705 u_char
*buffer
= (u_char
*)&lsa_header
->type
;
706 int type_offset
= buffer
- (u_char
*)&lsa_header
->age
; /* should be 2 */
708 /* Skip the AGE field */
709 u_int16_t len
= ntohs(lsa_header
->length
) - type_offset
;
711 /* Checksum offset starts from "type" field, not the beginning of the
712 lsa_header struct. The offset is 14, rather than 16. */
713 int checksum_offset
= (u_char
*)&lsa_header
->checksum
- buffer
;
715 return (unsigned short)fletcher_checksum(buffer
, len
, checksum_offset
);
718 int ospf6_lsa_checksum_valid(struct ospf6_lsa_header
*lsa_header
)
720 u_char
*buffer
= (u_char
*)&lsa_header
->type
;
721 int type_offset
= buffer
- (u_char
*)&lsa_header
->age
; /* should be 2 */
723 /* Skip the AGE field */
724 u_int16_t len
= ntohs(lsa_header
->length
) - type_offset
;
726 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
730 void ospf6_lsa_init(void)
732 ospf6_lsa_handler_vector
= vector_init(0);
733 ospf6_install_lsa_handler(&unknown_handler
);
736 void ospf6_lsa_terminate(void)
738 vector_free(ospf6_lsa_handler_vector
);
741 static char *ospf6_lsa_handler_name(struct ospf6_lsa_handler
*h
)
745 unsigned int size
= strlen(h
->name
);
747 if (!strcmp(h
->name
, "unknown") && h
->type
!= OSPF6_LSTYPE_UNKNOWN
) {
748 snprintf(buf
, sizeof(buf
), "%#04hx", h
->type
);
752 for (i
= 0; i
< MIN(size
, sizeof(buf
)); i
++) {
753 if (!islower((unsigned char)h
->name
[i
]))
754 buf
[i
] = tolower((unsigned char)h
->name
[i
]);
762 DEFUN (debug_ospf6_lsa_type
,
763 debug_ospf6_lsa_hex_cmd
,
764 "debug ospf6 lsa <router|network|inter-prefix|inter-router|as-external|link|intra-prefix|unknown> [<originate|examine|flooding>]",
767 "Debug Link State Advertisements (LSAs)\n"
768 "Display Router LSAs\n"
769 "Display Network LSAs\n"
770 "Display Inter-Area-Prefix LSAs\n"
771 "Display Inter-Router LSAs\n"
772 "Display As-External LSAs\n"
773 "Display Link LSAs\n"
774 "Display Intra-Area-Prefix LSAs\n"
775 "Display LSAs of unknown origin\n"
776 "Display details of LSAs\n"
778 "Display LSA's internal information\n")
783 struct ospf6_lsa_handler
*handler
= NULL
;
785 for (i
= 0; i
< vector_active(ospf6_lsa_handler_vector
); i
++) {
786 handler
= vector_slot(ospf6_lsa_handler_vector
, i
);
789 if (strncmp(argv
[idx_lsa
]->arg
, ospf6_lsa_handler_name(handler
),
790 strlen(argv
[idx_lsa
]->arg
))
793 if (!strcasecmp(argv
[idx_lsa
]->arg
, handler
->name
))
799 handler
= &unknown_handler
;
802 if (strmatch(argv
[idx_type
]->text
, "originate"))
803 SET_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_ORIGINATE
);
804 else if (strmatch(argv
[idx_type
]->text
, "examine"))
805 SET_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_EXAMIN
);
806 else if (strmatch(argv
[idx_type
]->text
, "flooding"))
807 SET_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_FLOOD
);
809 SET_FLAG(handler
->debug
, OSPF6_LSA_DEBUG
);
814 DEFUN (no_debug_ospf6_lsa_type
,
815 no_debug_ospf6_lsa_hex_cmd
,
816 "no debug ospf6 lsa <router|network|inter-prefix|inter-router|as-external|link|intra-prefix|unknown> [<originate|examine|flooding>]",
820 "Debug Link State Advertisements (LSAs)\n"
821 "Display Router LSAs\n"
822 "Display Network LSAs\n"
823 "Display Inter-Area-Prefix LSAs\n"
824 "Display Inter-Router LSAs\n"
825 "Display As-External LSAs\n"
826 "Display Link LSAs\n"
827 "Display Intra-Area-Prefix LSAs\n"
828 "Display LSAs of unknown origin\n"
829 "Display details of LSAs\n"
831 "Display LSA's internal information\n")
836 struct ospf6_lsa_handler
*handler
= NULL
;
838 for (i
= 0; i
< vector_active(ospf6_lsa_handler_vector
); i
++) {
839 handler
= vector_slot(ospf6_lsa_handler_vector
, i
);
842 if (strncmp(argv
[idx_lsa
]->arg
, ospf6_lsa_handler_name(handler
),
843 strlen(argv
[idx_lsa
]->arg
))
846 if (!strcasecmp(argv
[idx_lsa
]->arg
, handler
->name
))
854 if (strmatch(argv
[idx_type
]->text
, "originate"))
855 UNSET_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_ORIGINATE
);
856 if (strmatch(argv
[idx_type
]->text
, "examine"))
857 UNSET_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_EXAMIN
);
858 if (strmatch(argv
[idx_type
]->text
, "flooding"))
859 UNSET_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_FLOOD
);
861 UNSET_FLAG(handler
->debug
, OSPF6_LSA_DEBUG
);
866 void install_element_ospf6_debug_lsa(void)
868 install_element(ENABLE_NODE
, &debug_ospf6_lsa_hex_cmd
);
869 install_element(ENABLE_NODE
, &no_debug_ospf6_lsa_hex_cmd
);
870 install_element(CONFIG_NODE
, &debug_ospf6_lsa_hex_cmd
);
871 install_element(CONFIG_NODE
, &no_debug_ospf6_lsa_hex_cmd
);
874 int config_write_ospf6_debug_lsa(struct vty
*vty
)
877 struct ospf6_lsa_handler
*handler
;
879 for (i
= 0; i
< vector_active(ospf6_lsa_handler_vector
); i
++) {
880 handler
= vector_slot(ospf6_lsa_handler_vector
, i
);
883 if (CHECK_FLAG(handler
->debug
, OSPF6_LSA_DEBUG
))
884 vty_out(vty
, "debug ospf6 lsa %s\n",
885 ospf6_lsa_handler_name(handler
));
886 if (CHECK_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_ORIGINATE
))
887 vty_out(vty
, "debug ospf6 lsa %s originate\n",
888 ospf6_lsa_handler_name(handler
));
889 if (CHECK_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_EXAMIN
))
890 vty_out(vty
, "debug ospf6 lsa %s examine\n",
891 ospf6_lsa_handler_name(handler
));
892 if (CHECK_FLAG(handler
->debug
, OSPF6_LSA_DEBUG_FLOOD
))
893 vty_out(vty
, "debug ospf6 lsa %s flooding\n",
894 ospf6_lsa_handler_name(handler
));