usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / zebra / ospf6d / ospf6_lsa.c
blobd4855a6ca9ad92452799aedb84f5adcaee3b2694
1 /*
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
9 * later version.
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
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <zebra.h>
24 /* Include other stuffs */
25 #include "log.h"
26 #include "linklist.h"
27 #include "vector.h"
28 #include "vty.h"
29 #include "command.h"
30 #include "memory.h"
31 #include "thread.h"
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"
44 #include "ospf6d.h"
46 vector ospf6_lsa_handler_vector;
48 int
49 ospf6_unknown_lsa_show (struct vty *vty, struct ospf6_lsa *lsa)
51 u_char *start, *end, *current;
52 char byte[4];
54 start = (u_char *) lsa->header + sizeof (struct ospf6_lsa_header);
55 end = (u_char *) lsa->header + ntohs (lsa->header->length);
57 vty_out (vty, " Unknown contents:%s", VNL);
58 for (current = start; current < end; current ++)
60 if ((current - start) % 16 == 0)
61 vty_out (vty, "%s ", VNL);
62 else if ((current - start) % 4 == 0)
63 vty_out (vty, " ");
65 snprintf (byte, sizeof (byte), "%02x", *current);
66 vty_out (vty, "%s", byte);
69 vty_out (vty, "%s%s", VNL, VNL);
70 return 0;
73 struct ospf6_lsa_handler unknown_handler =
75 OSPF6_LSTYPE_UNKNOWN,
76 "Unknown",
77 ospf6_unknown_lsa_show,
78 OSPF6_LSA_DEBUG,
81 void
82 ospf6_install_lsa_handler (struct ospf6_lsa_handler *handler)
84 /* type in handler is host byte order */
85 int index = handler->type & OSPF6_LSTYPE_FCODE_MASK;
86 vector_set_index (ospf6_lsa_handler_vector, index, handler);
89 struct ospf6_lsa_handler *
90 ospf6_get_lsa_handler (u_int16_t type)
92 struct ospf6_lsa_handler *handler = NULL;
93 int index = ntohs (type) & OSPF6_LSTYPE_FCODE_MASK;
95 if (ospf6_lsa_handler_vector &&
96 index < vector_max (ospf6_lsa_handler_vector))
97 handler = vector_slot (ospf6_lsa_handler_vector, index);
98 else
99 handler = &unknown_handler;
101 if (handler == NULL)
102 handler = &unknown_handler;
104 return handler;
107 char *
108 ospf6_lstype_name (u_int16_t type)
110 static char buf[8];
111 struct ospf6_lsa_handler *handler;
113 handler = ospf6_get_lsa_handler (type);
114 if (handler && handler != &unknown_handler)
115 return handler->name;
117 snprintf (buf, sizeof (buf), "0x%04hx", ntohs (type));
118 return buf;
121 u_char
122 ospf6_lstype_debug (u_int16_t type)
124 struct ospf6_lsa_handler *handler;
125 handler = ospf6_get_lsa_handler (type);
126 return handler->debug;
129 /* RFC2328: Section 13.2 */
131 ospf6_lsa_is_differ (struct ospf6_lsa *lsa1,
132 struct ospf6_lsa *lsa2)
134 int len;
136 assert (OSPF6_LSA_IS_SAME (lsa1, lsa2));
138 /* XXX, Options ??? */
140 ospf6_lsa_age_current (lsa1);
141 ospf6_lsa_age_current (lsa2);
142 if (ntohs (lsa1->header->age) == MAXAGE &&
143 ntohs (lsa2->header->age) != MAXAGE)
144 return 1;
145 if (ntohs (lsa1->header->age) != MAXAGE &&
146 ntohs (lsa2->header->age) == MAXAGE)
147 return 1;
149 /* compare body */
150 if (ntohs (lsa1->header->length) != ntohs (lsa2->header->length))
151 return 1;
153 len = ntohs (lsa1->header->length) - sizeof (struct ospf6_lsa_header);
154 return memcmp (lsa1->header + 1, lsa2->header + 1, len);
158 ospf6_lsa_is_changed (struct ospf6_lsa *lsa1,
159 struct ospf6_lsa *lsa2)
161 int length;
163 if (OSPF6_LSA_IS_MAXAGE (lsa1) ^ OSPF6_LSA_IS_MAXAGE (lsa2))
164 return 1;
165 if (ntohs (lsa1->header->length) != ntohs (lsa2->header->length))
166 return 1;
168 length = OSPF6_LSA_SIZE (lsa1->header) - sizeof (struct ospf6_lsa_header);
169 assert (length > 0);
171 return memcmp (OSPF6_LSA_HEADER_END (lsa1->header),
172 OSPF6_LSA_HEADER_END (lsa2->header), length);
175 /* ospf6 age functions */
176 /* calculate birth */
177 static void
178 ospf6_lsa_age_set (struct ospf6_lsa *lsa)
180 struct timeval now;
182 assert (lsa && lsa->header);
184 if (gettimeofday (&now, (struct timezone *)NULL) < 0)
185 zlog_warn ("LSA: gettimeofday failed, may fail LSA AGEs: %s",
186 strerror (errno));
188 lsa->birth.tv_sec = now.tv_sec - ntohs (lsa->header->age);
189 lsa->birth.tv_usec = now.tv_usec;
191 return;
194 /* this function calculates current age from its birth,
195 then update age field of LSA header. return value is current age */
196 u_int16_t
197 ospf6_lsa_age_current (struct ospf6_lsa *lsa)
199 struct timeval now;
200 u_int32_t ulage;
201 u_int16_t age;
203 assert (lsa);
204 assert (lsa->header);
206 /* current time */
207 if (gettimeofday (&now, (struct timezone *)NULL) < 0)
208 zlog_warn ("LSA: gettimeofday failed, may fail LSA AGEs: %s",
209 strerror (errno));
211 /* calculate age */
212 ulage = now.tv_sec - lsa->birth.tv_sec;
214 /* if over MAXAGE, set to it */
215 age = (ulage > MAXAGE ? MAXAGE : ulage);
217 lsa->header->age = htons (age);
218 return age;
221 /* update age field of LSA header with adding InfTransDelay */
222 void
223 ospf6_lsa_age_update_to_send (struct ospf6_lsa *lsa, u_int32_t transdelay)
225 unsigned short age;
227 age = ospf6_lsa_age_current (lsa) + transdelay;
228 if (age > MAXAGE)
229 age = MAXAGE;
230 lsa->header->age = htons (age);
233 void
234 ospf6_lsa_premature_aging (struct ospf6_lsa *lsa)
236 /* log */
237 if (IS_OSPF6_DEBUG_LSA_TYPE (lsa->header->type))
238 zlog_info ("LSA: Premature aging: %s", lsa->name);
240 THREAD_OFF (lsa->expire);
241 THREAD_OFF (lsa->refresh);
243 memset (&lsa->birth, 0, sizeof (struct timeval));
244 thread_execute (master, ospf6_lsa_expire, lsa, 0);
247 /* check which is more recent. if a is more recent, return -1;
248 if the same, return 0; otherwise(b is more recent), return 1 */
250 ospf6_lsa_compare (struct ospf6_lsa *a, struct ospf6_lsa *b)
252 signed long seqnuma, seqnumb;
253 u_int16_t cksuma, cksumb;
254 u_int16_t agea, ageb;
256 assert (a && a->header);
257 assert (b && b->header);
258 assert (OSPF6_LSA_IS_SAME (a, b));
260 seqnuma = ((signed long) ntohl (a->header->seqnum))
261 - (signed long) INITIAL_SEQUENCE_NUMBER;
262 seqnumb = ((signed long) ntohl (b->header->seqnum))
263 - (signed long) INITIAL_SEQUENCE_NUMBER;
265 /* compare by sequence number */
266 /* XXX, LS sequence number wrapping */
267 if (seqnuma > seqnumb)
268 return -1;
269 else if (seqnuma < seqnumb)
270 return 1;
272 /* Checksum */
273 cksuma = ntohs (a->header->checksum);
274 cksumb = ntohs (b->header->checksum);
275 if (cksuma > cksumb)
276 return -1;
277 if (cksuma < cksumb)
278 return 0;
280 /* Update Age */
281 agea = ospf6_lsa_age_current (a);
282 ageb = ospf6_lsa_age_current (b);
284 /* MaxAge check */
285 if (agea == MAXAGE && ageb != MAXAGE)
286 return -1;
287 else if (agea != MAXAGE && ageb == MAXAGE)
288 return 1;
290 /* Age check */
291 if (agea > ageb && agea - ageb >= MAX_AGE_DIFF)
292 return 1;
293 else if (agea < ageb && ageb - agea >= MAX_AGE_DIFF)
294 return -1;
296 /* neither recent */
297 return 0;
300 char *
301 ospf6_lsa_printbuf (struct ospf6_lsa *lsa, char *buf, int size)
303 char id[16], adv_router[16];
304 inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
305 inet_ntop (AF_INET, &lsa->header->adv_router, adv_router,
306 sizeof (adv_router));
307 snprintf (buf, size, "[%s Id:%s Adv:%s]",
308 ospf6_lstype_name (lsa->header->type), id, adv_router);
309 return buf;
312 void
313 ospf6_lsa_header_print_raw (struct ospf6_lsa_header *header)
315 char id[16], adv_router[16];
316 inet_ntop (AF_INET, &header->id, id, sizeof (id));
317 inet_ntop (AF_INET, &header->adv_router, adv_router,
318 sizeof (adv_router));
319 zlog_info (" [%s Id:%s Adv:%s]",
320 ospf6_lstype_name (header->type), id, adv_router);
321 zlog_info (" Age: %4hu SeqNum: %#08lx Cksum: %04hx Len: %d",
322 ntohs (header->age), (u_long) ntohl (header->seqnum),
323 ntohs (header->checksum), ntohs (header->length));
326 void
327 ospf6_lsa_header_print (struct ospf6_lsa *lsa)
329 ospf6_lsa_age_current (lsa);
330 ospf6_lsa_header_print_raw (lsa->header);
333 void
334 ospf6_lsa_show_summary_header (struct vty *vty)
336 vty_out (vty, "%-12s %-15s %-15s %4s %8s %4s %4s %-8s%s",
337 "Type", "LSId", "AdvRouter", "Age", "SeqNum",
338 "Cksm", "Len", "Duration", VNL);
341 void
342 ospf6_lsa_show_summary (struct vty *vty, struct ospf6_lsa *lsa)
344 char adv_router[16], id[16];
345 struct timeval now, res;
346 char duration[16];
348 assert (lsa);
349 assert (lsa->header);
351 inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
352 inet_ntop (AF_INET, &lsa->header->adv_router, adv_router,
353 sizeof (adv_router));
355 gettimeofday (&now, NULL);
356 timersub (&now, &lsa->installed, &res);
357 timerstring (&res, duration, sizeof (duration));
359 vty_out (vty, "%-12s %-15s %-15s %4hu %8lx %04hx %4hu %8s%s",
360 ospf6_lstype_name (lsa->header->type),
361 id, adv_router, ospf6_lsa_age_current (lsa),
362 (u_long) ntohl (lsa->header->seqnum),
363 ntohs (lsa->header->checksum), ntohs (lsa->header->length),
364 duration, VNL);
367 void
368 ospf6_lsa_show_dump (struct vty *vty, struct ospf6_lsa *lsa)
370 u_char *start, *end, *current;
371 char byte[4];
373 start = (u_char *) lsa->header;
374 end = (u_char *) lsa->header + ntohs (lsa->header->length);
376 vty_out (vty, "%s", VNL);
377 vty_out (vty, "%s:%s", lsa->name, VNL);
379 for (current = start; current < end; current ++)
381 if ((current - start) % 16 == 0)
382 vty_out (vty, "%s ", VNL);
383 else if ((current - start) % 4 == 0)
384 vty_out (vty, " ");
386 snprintf (byte, sizeof (byte), "%02x", *current);
387 vty_out (vty, "%s", byte);
390 vty_out (vty, "%s%s", VNL, VNL);
391 return;
394 void
395 ospf6_lsa_show_internal (struct vty *vty, struct ospf6_lsa *lsa)
397 char adv_router[64], id[64];
399 assert (lsa && lsa->header);
401 inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
402 inet_ntop (AF_INET, &lsa->header->adv_router,
403 adv_router, sizeof (adv_router));
405 vty_out (vty, "%s", VNL);
406 vty_out (vty, "Age: %4hu Type: %s%s", ospf6_lsa_age_current (lsa),
407 ospf6_lstype_name (lsa->header->type), VNL);
408 vty_out (vty, "Link State ID: %s%s", id, VNL);
409 vty_out (vty, "Advertising Router: %s%s", adv_router, VNL);
410 vty_out (vty, "LS Sequence Number: %#010lx%s",
411 (u_long) ntohl (lsa->header->seqnum), VNL);
412 vty_out (vty, "CheckSum: %#06hx Length: %hu%s",
413 ntohs (lsa->header->checksum),
414 ntohs (lsa->header->length), VNL);
415 vty_out (vty, " Prev: %p This: %p Next: %p%s",
416 lsa->prev, lsa, lsa->next, VNL);
417 vty_out (vty, "%s", VNL);
418 return;
421 void
422 ospf6_lsa_show (struct vty *vty, struct ospf6_lsa *lsa)
424 char adv_router[64], id[64];
425 struct ospf6_lsa_handler *handler;
427 assert (lsa && lsa->header);
429 inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
430 inet_ntop (AF_INET, &lsa->header->adv_router,
431 adv_router, sizeof (adv_router));
433 vty_out (vty, "Age: %4hu Type: %s%s", ospf6_lsa_age_current (lsa),
434 ospf6_lstype_name (lsa->header->type), VNL);
435 vty_out (vty, "Link State ID: %s%s", id, VNL);
436 vty_out (vty, "Advertising Router: %s%s", adv_router, VNL);
437 vty_out (vty, "LS Sequence Number: %#010lx%s",
438 (u_long) ntohl (lsa->header->seqnum), VNL);
439 vty_out (vty, "CheckSum: %#06hx Length: %hu%s",
440 ntohs (lsa->header->checksum),
441 ntohs (lsa->header->length), VNL);
443 handler = ospf6_get_lsa_handler (lsa->header->type);
444 if (handler->show == NULL)
445 handler = &unknown_handler;
446 (*handler->show) (vty, lsa);
448 vty_out (vty, "%s", VNL);
451 /* OSPFv3 LSA creation/deletion function */
452 struct ospf6_lsa *
453 ospf6_lsa_create (struct ospf6_lsa_header *header)
455 struct ospf6_lsa *lsa = NULL;
456 struct ospf6_lsa_header *new_header = NULL;
457 u_int16_t lsa_size = 0;
459 /* size of the entire LSA */
460 lsa_size = ntohs (header->length); /* XXX vulnerable */
462 /* allocate memory for this LSA */
463 new_header = (struct ospf6_lsa_header *)
464 XMALLOC (MTYPE_OSPF6_LSA, lsa_size);
466 /* copy LSA from original header */
467 memcpy (new_header, header, lsa_size);
469 /* LSA information structure */
470 /* allocate memory */
471 lsa = (struct ospf6_lsa *)
472 XMALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa));
473 memset (lsa, 0, sizeof (struct ospf6_lsa));
475 lsa->header = (struct ospf6_lsa_header *) new_header;
477 /* dump string */
478 ospf6_lsa_printbuf (lsa, lsa->name, sizeof (lsa->name));
480 /* calculate birth of this lsa */
481 ospf6_lsa_age_set (lsa);
483 return lsa;
486 struct ospf6_lsa *
487 ospf6_lsa_create_headeronly (struct ospf6_lsa_header *header)
489 struct ospf6_lsa *lsa = NULL;
490 struct ospf6_lsa_header *new_header = NULL;
492 /* allocate memory for this LSA */
493 new_header = (struct ospf6_lsa_header *)
494 XMALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa_header));
496 /* copy LSA from original header */
497 memcpy (new_header, header, sizeof (struct ospf6_lsa_header));
499 /* LSA information structure */
500 /* allocate memory */
501 lsa = (struct ospf6_lsa *)
502 XMALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa));
503 memset (lsa, 0, sizeof (struct ospf6_lsa));
505 lsa->header = (struct ospf6_lsa_header *) new_header;
506 SET_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY);
508 /* dump string */
509 ospf6_lsa_printbuf (lsa, lsa->name, sizeof (lsa->name));
511 /* calculate birth of this lsa */
512 ospf6_lsa_age_set (lsa);
514 return lsa;
517 void
518 ospf6_lsa_delete (struct ospf6_lsa *lsa)
520 assert (lsa->lock == 0);
522 /* cancel threads */
523 THREAD_OFF (lsa->expire);
524 THREAD_OFF (lsa->refresh);
526 /* do free */
527 XFREE (MTYPE_OSPF6_LSA, lsa->header);
528 XFREE (MTYPE_OSPF6_LSA, lsa);
531 struct ospf6_lsa *
532 ospf6_lsa_copy (struct ospf6_lsa *lsa)
534 struct ospf6_lsa *copy = NULL;
536 ospf6_lsa_age_current (lsa);
537 if (CHECK_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY))
538 copy = ospf6_lsa_create_headeronly (lsa->header);
539 else
540 copy = ospf6_lsa_create (lsa->header);
541 assert (copy->lock == 0);
543 copy->birth = lsa->birth;
544 copy->originated = lsa->originated;
545 copy->received = lsa->received;
546 copy->installed = lsa->installed;
547 copy->lsdb = lsa->lsdb;
549 return copy;
552 /* increment reference counter of struct ospf6_lsa */
553 void
554 ospf6_lsa_lock (struct ospf6_lsa *lsa)
556 lsa->lock++;
557 return;
560 /* decrement reference counter of struct ospf6_lsa */
561 void
562 ospf6_lsa_unlock (struct ospf6_lsa *lsa)
564 /* decrement reference counter */
565 assert (lsa->lock > 0);
566 lsa->lock--;
568 if (lsa->lock != 0)
569 return;
571 ospf6_lsa_delete (lsa);
575 /* ospf6 lsa expiry */
577 ospf6_lsa_expire (struct thread *thread)
579 struct ospf6_lsa *lsa;
581 lsa = (struct ospf6_lsa *) THREAD_ARG (thread);
583 assert (lsa && lsa->header);
584 assert (OSPF6_LSA_IS_MAXAGE (lsa));
585 assert (! lsa->refresh);
587 lsa->expire = (struct thread *) NULL;
589 if (IS_OSPF6_DEBUG_LSA_TYPE (lsa->header->type))
591 zlog_info ("LSA Expire:");
592 ospf6_lsa_header_print (lsa);
595 if (CHECK_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY))
596 return 0; /* dbexchange will do something ... */
598 /* reflood lsa */
599 ospf6_flood (NULL, lsa);
601 /* reinstall lsa */
602 ospf6_install_lsa (lsa);
604 /* schedule maxage remover */
605 ospf6_maxage_remove (ospf6);
607 return 0;
611 ospf6_lsa_refresh (struct thread *thread)
613 struct ospf6_lsa *old, *self, *new;
614 struct ospf6_lsdb *lsdb_self;
616 assert (thread);
617 old = (struct ospf6_lsa *) THREAD_ARG (thread);
618 assert (old && old->header);
620 old->refresh = (struct thread *) NULL;
622 lsdb_self = ospf6_get_scoped_lsdb_self (old);
623 self = ospf6_lsdb_lookup (old->header->type, old->header->id,
624 old->header->adv_router, lsdb_self);
625 if (self == NULL)
627 if (IS_OSPF6_DEBUG_LSA_TYPE (old->header->type))
628 zlog_info ("Refresh: could not find self LSA, flush %s", old->name);
629 ospf6_lsa_premature_aging (old);
630 return 0;
633 /* Reset age, increment LS sequence number. */
634 self->header->age = htons (0);
635 self->header->seqnum =
636 ospf6_new_ls_seqnum (self->header->type, self->header->id,
637 self->header->adv_router, old->lsdb);
638 ospf6_lsa_checksum (self->header);
640 new = ospf6_lsa_create (self->header);
641 new->lsdb = old->lsdb;
642 new->refresh = thread_add_timer (master, ospf6_lsa_refresh, new,
643 LS_REFRESH_TIME);
645 /* store it in the LSDB for self-originated LSAs */
646 ospf6_lsdb_add (ospf6_lsa_copy (new), lsdb_self);
648 if (IS_OSPF6_DEBUG_LSA_TYPE (new->header->type))
650 zlog_info ("LSA Refresh:");
651 ospf6_lsa_header_print (new);
654 ospf6_flood_clear (old);
655 ospf6_flood (NULL, new);
656 ospf6_install_lsa (new);
658 return 0;
663 /* enhanced Fletcher checksum algorithm, RFC1008 7.2 */
664 #define MODX 4102
665 #define LSA_CHECKSUM_OFFSET 15
667 unsigned short
668 ospf6_lsa_checksum (struct ospf6_lsa_header *lsa_header)
670 u_char *sp, *ep, *p, *q;
671 int c0 = 0, c1 = 0;
672 int x, y;
673 u_int16_t length;
675 lsa_header->checksum = 0;
676 length = ntohs (lsa_header->length) - 2;
677 sp = (u_char *) &lsa_header->type;
679 for (ep = sp + length; sp < ep; sp = q)
681 q = sp + MODX;
682 if (q > ep)
683 q = ep;
684 for (p = sp; p < q; p++)
686 c0 += *p;
687 c1 += c0;
689 c0 %= 255;
690 c1 %= 255;
693 /* r = (c1 << 8) + c0; */
694 x = ((length - LSA_CHECKSUM_OFFSET) * c0 - c1) % 255;
695 if (x <= 0)
696 x += 255;
697 y = 510 - c0 - x;
698 if (y > 255)
699 y -= 255;
701 lsa_header->checksum = htons ((x << 8) + y);
703 return (lsa_header->checksum);
706 void
707 ospf6_lsa_init ()
709 ospf6_lsa_handler_vector = vector_init (0);
710 ospf6_install_lsa_handler (&unknown_handler);
714 char *
715 ospf6_lsa_handler_name (struct ospf6_lsa_handler *h)
717 static char buf[64];
718 int i, size = strlen (h->name);
720 if (h->name == "Unknown" &&
721 h->type != OSPF6_LSTYPE_UNKNOWN)
723 snprintf (buf, sizeof (buf), "%#04hx", h->type);
724 return buf;
727 for (i = 0; i < MIN (size, sizeof (buf)); i++)
729 if (! islower (h->name[i]))
730 buf[i] = tolower (h->name[i]);
731 else
732 buf[i] = h->name[i];
734 buf[size] = '\0';
735 return buf;
738 DEFUN (debug_ospf6_lsa_type,
739 debug_ospf6_lsa_hex_cmd,
740 "debug ospf6 lsa XXXX/0xXXXX",
741 DEBUG_STR
742 OSPF6_STR
743 "Debug Link State Advertisements (LSAs)\n"
744 "Specify LS type as Hexadecimal\n"
747 int i;
748 struct ospf6_lsa_handler *handler = NULL;
749 unsigned long val;
750 char *endptr = NULL;
751 u_int16_t type = 0;
753 assert (argc);
755 if ((strlen (argv[0]) == 6 && ! strncmp (argv[0], "0x", 2)) ||
756 (strlen (argv[0]) == 4))
758 val = strtoul (argv[0], &endptr, 16);
759 if (*endptr == '\0')
760 type = val;
763 for (i = 0; i < vector_max (ospf6_lsa_handler_vector); i++)
765 handler = vector_slot (ospf6_lsa_handler_vector, i);
766 if (handler == NULL)
767 continue;
768 if (type && handler->type == type)
769 break;
770 if (! strcasecmp (argv[0], handler->name))
771 break;
772 handler = NULL;
775 if (type && handler == NULL)
777 handler = (struct ospf6_lsa_handler *)
778 malloc (sizeof (struct ospf6_lsa_handler));
779 memset (handler, 0, sizeof (struct ospf6_lsa_handler));
780 handler->type = type;
781 handler->name = "Unknown";
782 handler->show = ospf6_unknown_lsa_show;
783 vector_set_index (ospf6_lsa_handler_vector,
784 handler->type & OSPF6_LSTYPE_FCODE_MASK, handler);
787 if (handler == NULL)
788 handler = &unknown_handler;
790 if (argc >= 2)
792 if (! strcmp (argv[1], "originate"))
793 SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE);
794 if (! strcmp (argv[1], "examin"))
795 SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN);
796 if (! strcmp (argv[1], "flooding"))
797 SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD);
799 else
800 SET_FLAG (handler->debug, OSPF6_LSA_DEBUG);
802 return CMD_SUCCESS;
805 DEFUN (no_debug_ospf6_lsa_type,
806 no_debug_ospf6_lsa_hex_cmd,
807 "no debug ospf6 lsa XXXX/0xXXXX",
808 NO_STR
809 DEBUG_STR
810 OSPF6_STR
811 "Debug Link State Advertisements (LSAs)\n"
812 "Specify LS type as Hexadecimal\n"
815 int i;
816 struct ospf6_lsa_handler *handler = NULL;
817 unsigned long val;
818 char *endptr = NULL;
819 u_int16_t type = 0;
821 assert (argc);
823 if ((strlen (argv[0]) == 6 && ! strncmp (argv[0], "0x", 2)) ||
824 (strlen (argv[0]) == 4))
826 val = strtoul (argv[0], &endptr, 16);
827 if (*endptr == '\0')
828 type = val;
831 for (i = 0; i < vector_max (ospf6_lsa_handler_vector); i++)
833 handler = vector_slot (ospf6_lsa_handler_vector, i);
834 if (handler == NULL)
835 continue;
836 if (type && handler->type == type)
837 break;
838 if (! strcasecmp (argv[0], handler->name))
839 break;
842 if (handler == NULL)
843 return CMD_SUCCESS;
845 if (argc >= 2)
847 if (! strcmp (argv[1], "originate"))
848 UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE);
849 if (! strcmp (argv[1], "examin"))
850 UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN);
851 if (! strcmp (argv[1], "flooding"))
852 UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD);
854 else
855 UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG);
857 if (handler->debug == 0 &&
858 handler->name == "Unknown" && type != OSPF6_LSTYPE_UNKNOWN)
860 free (handler);
861 vector_slot (ospf6_lsa_handler_vector, i) = NULL;
864 return CMD_SUCCESS;
867 struct cmd_element debug_ospf6_lsa_type_cmd;
868 struct cmd_element debug_ospf6_lsa_type_detail_cmd;
869 struct cmd_element no_debug_ospf6_lsa_type_cmd;
870 struct cmd_element no_debug_ospf6_lsa_type_detail_cmd;
872 void
873 install_element_ospf6_debug_lsa ()
875 int i;
876 struct ospf6_lsa_handler *handler;
877 #define STRSIZE 256
878 #define DOCSIZE 1024
879 static char strbuf[STRSIZE];
880 static char docbuf[DOCSIZE];
881 static char detail_strbuf[STRSIZE];
882 static char detail_docbuf[DOCSIZE];
883 char *str, *no_str;
884 char *doc, *no_doc;
886 strbuf[0] = '\0';
887 no_str = &strbuf[strlen (strbuf)];
888 strncat (strbuf, "no ", STRSIZE - strlen (strbuf));
889 str = &strbuf[strlen (strbuf)];
891 strncat (strbuf, "debug ospf6 lsa (", STRSIZE - strlen (strbuf));
892 for (i = 0; i < vector_max (ospf6_lsa_handler_vector); i++)
894 handler = vector_slot (ospf6_lsa_handler_vector, i);
895 if (handler == NULL)
896 continue;
897 strncat (strbuf, ospf6_lsa_handler_name (handler),
898 STRSIZE - strlen (strbuf));
899 strncat (strbuf, "|", STRSIZE - strlen (strbuf));
901 strbuf[strlen (strbuf) - 1] = ')';
902 strbuf[strlen (strbuf)] = '\0';
904 docbuf[0] = '\0';
905 no_doc = &docbuf[strlen (docbuf)];
906 strncat (docbuf, NO_STR, DOCSIZE - strlen (docbuf));
907 doc = &docbuf[strlen (docbuf)];
909 strncat (docbuf, DEBUG_STR, DOCSIZE - strlen (docbuf));
910 strncat (docbuf, OSPF6_STR, DOCSIZE - strlen (docbuf));
911 strncat (docbuf, "Debug Link State Advertisements (LSAs)\n",
912 DOCSIZE - strlen (docbuf));
914 for (i = 0; i < vector_max (ospf6_lsa_handler_vector); i++)
916 handler = vector_slot (ospf6_lsa_handler_vector, i);
917 if (handler == NULL)
918 continue;
919 strncat (docbuf, "Debug ", DOCSIZE - strlen (docbuf));
920 strncat (docbuf, handler->name, DOCSIZE - strlen (docbuf));
921 strncat (docbuf, "-LSA\n", DOCSIZE - strlen (docbuf));
923 docbuf[strlen (docbuf)] = '\0';
925 debug_ospf6_lsa_type_cmd.string = str;
926 debug_ospf6_lsa_type_cmd.func = debug_ospf6_lsa_type;
927 debug_ospf6_lsa_type_cmd.doc = doc;
929 no_debug_ospf6_lsa_type_cmd.string = no_str;
930 no_debug_ospf6_lsa_type_cmd.func = no_debug_ospf6_lsa_type;
931 no_debug_ospf6_lsa_type_cmd.doc = no_doc;
933 strncpy (detail_strbuf, strbuf, STRSIZE);
934 strncat (detail_strbuf, " (originate|examin|flooding)",
935 STRSIZE - strlen (detail_strbuf));
936 detail_strbuf[strlen (detail_strbuf)] = '\0';
937 no_str = &detail_strbuf[0];
938 str = &detail_strbuf[strlen ("no ")];
940 strncpy (detail_docbuf, docbuf, DOCSIZE);
941 strncat (detail_docbuf, "Debug Originating LSA\n",
942 DOCSIZE - strlen (detail_docbuf));
943 strncat (detail_docbuf, "Debug Examining LSA\n",
944 DOCSIZE - strlen (detail_docbuf));
945 strncat (detail_docbuf, "Debug Flooding LSA\n",
946 DOCSIZE - strlen (detail_docbuf));
947 detail_docbuf[strlen (detail_docbuf)] = '\0';
948 no_doc = &detail_docbuf[0];
949 doc = &detail_docbuf[strlen (NO_STR)];
951 debug_ospf6_lsa_type_detail_cmd.string = str;
952 debug_ospf6_lsa_type_detail_cmd.func = debug_ospf6_lsa_type;
953 debug_ospf6_lsa_type_detail_cmd.doc = doc;
955 no_debug_ospf6_lsa_type_detail_cmd.string = no_str;
956 no_debug_ospf6_lsa_type_detail_cmd.func = no_debug_ospf6_lsa_type;
957 no_debug_ospf6_lsa_type_detail_cmd.doc = no_doc;
959 install_element (ENABLE_NODE, &debug_ospf6_lsa_hex_cmd);
960 install_element (ENABLE_NODE, &debug_ospf6_lsa_type_cmd);
961 install_element (ENABLE_NODE, &debug_ospf6_lsa_type_detail_cmd);
962 install_element (ENABLE_NODE, &no_debug_ospf6_lsa_hex_cmd);
963 install_element (ENABLE_NODE, &no_debug_ospf6_lsa_type_cmd);
964 install_element (ENABLE_NODE, &no_debug_ospf6_lsa_type_detail_cmd);
965 install_element (CONFIG_NODE, &debug_ospf6_lsa_hex_cmd);
966 install_element (CONFIG_NODE, &debug_ospf6_lsa_type_cmd);
967 install_element (CONFIG_NODE, &debug_ospf6_lsa_type_detail_cmd);
968 install_element (CONFIG_NODE, &no_debug_ospf6_lsa_hex_cmd);
969 install_element (CONFIG_NODE, &no_debug_ospf6_lsa_type_cmd);
970 install_element (CONFIG_NODE, &no_debug_ospf6_lsa_type_detail_cmd);
974 config_write_ospf6_debug_lsa (struct vty *vty)
976 int i;
977 struct ospf6_lsa_handler *handler;
979 for (i = 0; i < vector_max (ospf6_lsa_handler_vector); i++)
981 handler = vector_slot (ospf6_lsa_handler_vector, i);
982 if (handler == NULL)
983 continue;
984 if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG))
985 vty_out (vty, "debug ospf6 lsa %s%s",
986 ospf6_lsa_handler_name (handler), VNL);
987 if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE))
988 vty_out (vty, "debug ospf6 lsa %s originate%s",
989 ospf6_lsa_handler_name (handler), VNL);
990 if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN))
991 vty_out (vty, "debug ospf6 lsa %s examin%s",
992 ospf6_lsa_handler_name (handler), VNL);
993 if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD))
994 vty_out (vty, "debug ospf6 lsa %s flooding%s",
995 ospf6_lsa_handler_name (handler), VNL);
998 return 0;