]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_lsa.c
acc8fe38040932b35b1884be043cd53dcece2baf
[mirror_frr.git] / ospf6d / ospf6_lsa.c
1 /*
2 * Copyright (C) 2003 Yasuhiro Ohara
3 *
4 * This file is part of GNU Zebra.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <zebra.h>
22
23 /* Include other stuffs */
24 #include "log.h"
25 #include "linklist.h"
26 #include "vector.h"
27 #include "vty.h"
28 #include "command.h"
29 #include "memory.h"
30 #include "thread.h"
31 #include "checksum.h"
32
33 #include "ospf6_proto.h"
34 #include "ospf6_lsa.h"
35 #include "ospf6_lsdb.h"
36 #include "ospf6_message.h"
37
38 #include "ospf6_top.h"
39 #include "ospf6_area.h"
40 #include "ospf6_interface.h"
41 #include "ospf6_neighbor.h"
42
43 #include "ospf6_flood.h"
44 #include "ospf6d.h"
45
46 vector ospf6_lsa_handler_vector;
47
48 static int
49 ospf6_unknown_lsa_show (struct vty *vty, struct ospf6_lsa *lsa)
50 {
51 u_char *start, *end, *current;
52 char byte[4];
53
54 start = (u_char *) lsa->header + sizeof (struct ospf6_lsa_header);
55 end = (u_char *) lsa->header + ntohs (lsa->header->length);
56
57 vty_out (vty, " Unknown contents:%s", VNL);
58 for (current = start; current < end; current ++)
59 {
60 if ((current - start) % 16 == 0)
61 vty_out (vty, "%s ", VNL);
62 else if ((current - start) % 4 == 0)
63 vty_out (vty, " ");
64
65 snprintf (byte, sizeof (byte), "%02x", *current);
66 vty_out (vty, "%s", byte);
67 }
68
69 vty_out (vty, "%s%s", VNL, VNL);
70 return 0;
71 }
72
73 struct ospf6_lsa_handler unknown_handler =
74 {
75 OSPF6_LSTYPE_UNKNOWN,
76 "Unknown",
77 "Unk",
78 ospf6_unknown_lsa_show,
79 NULL
80 };
81
82 void
83 ospf6_install_lsa_handler (struct ospf6_lsa_handler *handler)
84 {
85 /* type in handler is host byte order */
86 int index = handler->type & OSPF6_LSTYPE_FCODE_MASK;
87 vector_set_index (ospf6_lsa_handler_vector, index, handler);
88 }
89
90 struct ospf6_lsa_handler *
91 ospf6_get_lsa_handler (u_int16_t type)
92 {
93 struct ospf6_lsa_handler *handler = NULL;
94 unsigned int index = ntohs (type) & OSPF6_LSTYPE_FCODE_MASK;
95
96 if (index >= vector_active (ospf6_lsa_handler_vector))
97 handler = &unknown_handler;
98 else
99 handler = vector_slot (ospf6_lsa_handler_vector, index);
100
101 if (handler == NULL)
102 handler = &unknown_handler;
103
104 return handler;
105 }
106
107 const char *
108 ospf6_lstype_name (u_int16_t type)
109 {
110 static char buf[8];
111 struct ospf6_lsa_handler *handler;
112
113 handler = ospf6_get_lsa_handler (type);
114 if (handler && handler != &unknown_handler)
115 return handler->name;
116
117 snprintf (buf, sizeof (buf), "0x%04hx", ntohs (type));
118 return buf;
119 }
120
121 const char *
122 ospf6_lstype_short_name (u_int16_t type)
123 {
124 static char buf[8];
125 struct ospf6_lsa_handler *handler;
126
127 handler = ospf6_get_lsa_handler (type);
128 if (handler && handler != &unknown_handler)
129 return handler->short_name;
130
131 snprintf (buf, sizeof (buf), "0x%04hx", ntohs (type));
132 return buf;
133 }
134
135 u_char
136 ospf6_lstype_debug (u_int16_t type)
137 {
138 struct ospf6_lsa_handler *handler;
139 handler = ospf6_get_lsa_handler (type);
140 return handler->debug;
141 }
142
143 /* RFC2328: Section 13.2 */
144 int
145 ospf6_lsa_is_differ (struct ospf6_lsa *lsa1,
146 struct ospf6_lsa *lsa2)
147 {
148 int len;
149
150 assert (OSPF6_LSA_IS_SAME (lsa1, lsa2));
151
152 /* XXX, Options ??? */
153
154 ospf6_lsa_age_current (lsa1);
155 ospf6_lsa_age_current (lsa2);
156 if (ntohs (lsa1->header->age) == OSPF_LSA_MAXAGE &&
157 ntohs (lsa2->header->age) != OSPF_LSA_MAXAGE)
158 return 1;
159 if (ntohs (lsa1->header->age) != OSPF_LSA_MAXAGE &&
160 ntohs (lsa2->header->age) == OSPF_LSA_MAXAGE)
161 return 1;
162
163 /* compare body */
164 if (ntohs (lsa1->header->length) != ntohs (lsa2->header->length))
165 return 1;
166
167 len = ntohs (lsa1->header->length) - sizeof (struct ospf6_lsa_header);
168 return memcmp (lsa1->header + 1, lsa2->header + 1, len);
169 }
170
171 int
172 ospf6_lsa_is_changed (struct ospf6_lsa *lsa1,
173 struct ospf6_lsa *lsa2)
174 {
175 int length;
176
177 if (OSPF6_LSA_IS_MAXAGE (lsa1) ^ OSPF6_LSA_IS_MAXAGE (lsa2))
178 return 1;
179 if (ntohs (lsa1->header->length) != ntohs (lsa2->header->length))
180 return 1;
181 /* Going beyond LSA headers to compare the payload only makes sense, when both LSAs aren't header-only. */
182 if (CHECK_FLAG (lsa1->flag, OSPF6_LSA_HEADERONLY) != CHECK_FLAG (lsa2->flag, OSPF6_LSA_HEADERONLY))
183 {
184 zlog_warn ("%s: only one of two (%s, %s) LSAs compared is header-only", __func__, lsa1->name, lsa2->name);
185 return 1;
186 }
187 if (CHECK_FLAG (lsa1->flag, OSPF6_LSA_HEADERONLY))
188 return 0;
189
190 length = OSPF6_LSA_SIZE (lsa1->header) - sizeof (struct ospf6_lsa_header);
191 /* Once upper layer verifies LSAs received, length underrun should become a warning. */
192 if (length <= 0)
193 return 0;
194
195 return memcmp (OSPF6_LSA_HEADER_END (lsa1->header),
196 OSPF6_LSA_HEADER_END (lsa2->header), length);
197 }
198
199 /* ospf6 age functions */
200 /* calculate birth */
201 static void
202 ospf6_lsa_age_set (struct ospf6_lsa *lsa)
203 {
204 struct timeval now;
205
206 assert (lsa && lsa->header);
207
208 monotime(&now);
209
210 lsa->birth.tv_sec = now.tv_sec - ntohs (lsa->header->age);
211 lsa->birth.tv_usec = now.tv_usec;
212
213 return;
214 }
215
216 /* this function calculates current age from its birth,
217 then update age field of LSA header. return value is current age */
218 u_int16_t
219 ospf6_lsa_age_current (struct ospf6_lsa *lsa)
220 {
221 struct timeval now;
222 u_int32_t ulage;
223 u_int16_t age;
224
225 assert (lsa);
226 assert (lsa->header);
227
228 /* current time */
229 monotime(&now);
230
231 if (ntohs (lsa->header->age) >= OSPF_LSA_MAXAGE)
232 {
233 /* ospf6_lsa_premature_aging () sets age to MAXAGE; when using
234 relative time, we cannot compare against lsa birth time, so
235 we catch this special case here. */
236 lsa->header->age = htons (OSPF_LSA_MAXAGE);
237 return OSPF_LSA_MAXAGE;
238 }
239 /* calculate age */
240 ulage = now.tv_sec - lsa->birth.tv_sec;
241
242 /* if over MAXAGE, set to it */
243 age = (ulage > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : ulage);
244
245 lsa->header->age = htons (age);
246 return age;
247 }
248
249 /* update age field of LSA header with adding InfTransDelay */
250 void
251 ospf6_lsa_age_update_to_send (struct ospf6_lsa *lsa, u_int32_t transdelay)
252 {
253 unsigned short age;
254
255 age = ospf6_lsa_age_current (lsa) + transdelay;
256 if (age > OSPF_LSA_MAXAGE)
257 age = OSPF_LSA_MAXAGE;
258 lsa->header->age = htons (age);
259 }
260
261 void
262 ospf6_lsa_premature_aging (struct ospf6_lsa *lsa)
263 {
264 /* log */
265 if (IS_OSPF6_DEBUG_LSA_TYPE (lsa->header->type))
266 zlog_debug ("LSA: Premature aging: %s", lsa->name);
267
268 THREAD_OFF (lsa->expire);
269 THREAD_OFF (lsa->refresh);
270
271 /*
272 * We clear the LSA from the neighbor retx lists now because it
273 * will not get deleted later. Essentially, changing the age to
274 * MaxAge will prevent this LSA from being matched with its
275 * existing entries in the retx list thereby causing those entries
276 * to be silently replaced with its MaxAged version, but with ever
277 * increasing retx count causing this LSA to remain forever and
278 * for the MaxAge remover thread to be called forever too.
279 *
280 * The reason the previous entry silently disappears is that when
281 * entry is added to a neighbor's retx list, it replaces the existing
282 * entry. But since the ospf6_lsdb_add() routine is generic and not aware
283 * of the special semantics of retx count, the retx count is not
284 * decremented when its replaced. Attempting to add the incr and decr
285 * retx count routines as the hook_add and hook_remove for the retx lists
286 * have a problem because the hook_remove routine is called for MaxAge
287 * entries (as will be the case in a traditional LSDB, unlike in this case
288 * where an LSDB is used as an efficient tree structure to store all kinds
289 * of data) that are added instead of calling the hook_add routine.
290 */
291
292 ospf6_flood_clear (lsa);
293
294 lsa->header->age = htons (OSPF_LSA_MAXAGE);
295 thread_execute (master, ospf6_lsa_expire, lsa, 0);
296 }
297
298 /* check which is more recent. if a is more recent, return -1;
299 if the same, return 0; otherwise(b is more recent), return 1 */
300 int
301 ospf6_lsa_compare (struct ospf6_lsa *a, struct ospf6_lsa *b)
302 {
303 int32_t seqnuma, seqnumb;
304 u_int16_t cksuma, cksumb;
305 u_int16_t agea, ageb;
306
307 assert (a && a->header);
308 assert (b && b->header);
309 assert (OSPF6_LSA_IS_SAME (a, b));
310
311 seqnuma = (int32_t) ntohl (a->header->seqnum);
312 seqnumb = (int32_t) ntohl (b->header->seqnum);
313
314 /* compare by sequence number */
315 if (seqnuma > seqnumb)
316 return -1;
317 if (seqnuma < seqnumb)
318 return 1;
319
320 /* Checksum */
321 cksuma = ntohs (a->header->checksum);
322 cksumb = ntohs (b->header->checksum);
323 if (cksuma > cksumb)
324 return -1;
325 if (cksuma < cksumb)
326 return 0;
327
328 /* Update Age */
329 agea = ospf6_lsa_age_current (a);
330 ageb = ospf6_lsa_age_current (b);
331
332 /* MaxAge check */
333 if (agea == OSPF_LSA_MAXAGE && ageb != OSPF_LSA_MAXAGE)
334 return -1;
335 else if (agea != OSPF_LSA_MAXAGE && ageb == OSPF_LSA_MAXAGE)
336 return 1;
337
338 /* Age check */
339 if (agea > ageb && agea - ageb >= OSPF_LSA_MAXAGE_DIFF)
340 return 1;
341 else if (agea < ageb && ageb - agea >= OSPF_LSA_MAXAGE_DIFF)
342 return -1;
343
344 /* neither recent */
345 return 0;
346 }
347
348 char *
349 ospf6_lsa_printbuf (struct ospf6_lsa *lsa, char *buf, int size)
350 {
351 char id[16], adv_router[16];
352 inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
353 inet_ntop (AF_INET, &lsa->header->adv_router, adv_router,
354 sizeof (adv_router));
355 snprintf (buf, size, "[%s Id:%s Adv:%s]",
356 ospf6_lstype_name (lsa->header->type), id, adv_router);
357 return buf;
358 }
359
360 void
361 ospf6_lsa_header_print_raw (struct ospf6_lsa_header *header)
362 {
363 char id[16], adv_router[16];
364 inet_ntop (AF_INET, &header->id, id, sizeof (id));
365 inet_ntop (AF_INET, &header->adv_router, adv_router,
366 sizeof (adv_router));
367 zlog_debug (" [%s Id:%s Adv:%s]",
368 ospf6_lstype_name (header->type), id, adv_router);
369 zlog_debug (" Age: %4hu SeqNum: %#08lx Cksum: %04hx Len: %d",
370 ntohs (header->age), (u_long) ntohl (header->seqnum),
371 ntohs (header->checksum), ntohs (header->length));
372 }
373
374 void
375 ospf6_lsa_header_print (struct ospf6_lsa *lsa)
376 {
377 ospf6_lsa_age_current (lsa);
378 ospf6_lsa_header_print_raw (lsa->header);
379 }
380
381 void
382 ospf6_lsa_show_summary_header (struct vty *vty)
383 {
384 vty_out (vty, "%-4s %-15s%-15s%4s %8s %30s%s",
385 "Type", "LSId", "AdvRouter", "Age", "SeqNum",
386 "Payload", VNL);
387 }
388
389 void
390 ospf6_lsa_show_summary (struct vty *vty, struct ospf6_lsa *lsa)
391 {
392 char adv_router[16], id[16];
393 int type;
394 struct ospf6_lsa_handler *handler;
395 char buf[64], tmpbuf[80];
396 int cnt = 0;
397
398 assert (lsa);
399 assert (lsa->header);
400
401 inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
402 inet_ntop (AF_INET, &lsa->header->adv_router, adv_router,
403 sizeof (adv_router));
404
405 type = ntohs(lsa->header->type);
406 handler = ospf6_get_lsa_handler (lsa->header->type);
407 if ((type == OSPF6_LSTYPE_INTER_PREFIX) ||
408 (type == OSPF6_LSTYPE_INTER_ROUTER) ||
409 (type == OSPF6_LSTYPE_AS_EXTERNAL))
410 {
411 vty_out (vty, "%-4s %-15s%-15s%4hu %8lx %30s%s",
412 ospf6_lstype_short_name (lsa->header->type),
413 id, adv_router, ospf6_lsa_age_current (lsa),
414 (u_long) ntohl (lsa->header->seqnum),
415 handler->get_prefix_str(lsa, buf, sizeof(buf), 0), VNL);
416 }
417 else if (type != OSPF6_LSTYPE_UNKNOWN)
418 {
419 sprintf (tmpbuf, "%-4s %-15s%-15s%4hu %8lx",
420 ospf6_lstype_short_name (lsa->header->type),
421 id, adv_router, ospf6_lsa_age_current (lsa),
422 (u_long) ntohl (lsa->header->seqnum));
423
424 while (handler->get_prefix_str(lsa, buf, sizeof(buf), cnt) != NULL)
425 {
426 vty_out (vty, "%s %30s%s", tmpbuf, buf, VNL);
427 cnt++;
428 }
429 }
430 else
431 {
432 vty_out (vty, "%-4s %-15s%-15s%4hu %8lx%s",
433 ospf6_lstype_short_name (lsa->header->type),
434 id, adv_router, ospf6_lsa_age_current (lsa),
435 (u_long) ntohl (lsa->header->seqnum), VNL);
436 }
437 }
438
439 void
440 ospf6_lsa_show_dump (struct vty *vty, struct ospf6_lsa *lsa)
441 {
442 u_char *start, *end, *current;
443 char byte[4];
444
445 start = (u_char *) lsa->header;
446 end = (u_char *) lsa->header + ntohs (lsa->header->length);
447
448 vty_out (vty, "%s", VNL);
449 vty_out (vty, "%s:%s", lsa->name, VNL);
450
451 for (current = start; current < end; current ++)
452 {
453 if ((current - start) % 16 == 0)
454 vty_out (vty, "%s ", VNL);
455 else if ((current - start) % 4 == 0)
456 vty_out (vty, " ");
457
458 snprintf (byte, sizeof (byte), "%02x", *current);
459 vty_out (vty, "%s", byte);
460 }
461
462 vty_out (vty, "%s%s", VNL, VNL);
463 return;
464 }
465
466 void
467 ospf6_lsa_show_internal (struct vty *vty, struct ospf6_lsa *lsa)
468 {
469 char adv_router[64], id[64];
470
471 assert (lsa && lsa->header);
472
473 inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
474 inet_ntop (AF_INET, &lsa->header->adv_router,
475 adv_router, sizeof (adv_router));
476
477 vty_out (vty, "%s", VNL);
478 vty_out (vty, "Age: %4hu Type: %s%s", ospf6_lsa_age_current (lsa),
479 ospf6_lstype_name (lsa->header->type), VNL);
480 vty_out (vty, "Link State ID: %s%s", id, VNL);
481 vty_out (vty, "Advertising Router: %s%s", adv_router, VNL);
482 vty_out (vty, "LS Sequence Number: %#010lx%s",
483 (u_long) ntohl (lsa->header->seqnum), VNL);
484 vty_out (vty, "CheckSum: %#06hx Length: %hu%s",
485 ntohs (lsa->header->checksum),
486 ntohs (lsa->header->length), VNL);
487 vty_out (vty, "Flag: %x %s", lsa->flag, VNL);
488 vty_out (vty, "Lock: %d %s", lsa->lock, VNL);
489 vty_out (vty, "ReTx Count: %d%s", lsa->retrans_count, VNL);
490 vty_out (vty, "Threads: Expire: 0x%p, Refresh: 0x%p %s",
491 (void *)lsa->expire, (void *)lsa->refresh, VNL);
492 vty_out (vty, "%s", VNL);
493 return;
494 }
495
496 void
497 ospf6_lsa_show (struct vty *vty, struct ospf6_lsa *lsa)
498 {
499 char adv_router[64], id[64];
500 struct ospf6_lsa_handler *handler;
501 struct timeval now, res;
502 char duration[16];
503
504 assert (lsa && lsa->header);
505
506 inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
507 inet_ntop (AF_INET, &lsa->header->adv_router,
508 adv_router, sizeof (adv_router));
509
510 monotime(&now);
511 timersub (&now, &lsa->installed, &res);
512 timerstring (&res, duration, sizeof (duration));
513
514 vty_out (vty, "Age: %4hu Type: %s%s", ospf6_lsa_age_current (lsa),
515 ospf6_lstype_name (lsa->header->type), VNL);
516 vty_out (vty, "Link State ID: %s%s", id, VNL);
517 vty_out (vty, "Advertising Router: %s%s", adv_router, VNL);
518 vty_out (vty, "LS Sequence Number: %#010lx%s",
519 (u_long) ntohl (lsa->header->seqnum), VNL);
520 vty_out (vty, "CheckSum: %#06hx Length: %hu%s",
521 ntohs (lsa->header->checksum),
522 ntohs (lsa->header->length), VNL);
523 vty_out (vty, "Duration: %s%s", duration, VNL);
524
525 handler = ospf6_get_lsa_handler (lsa->header->type);
526 if (handler->show == NULL)
527 handler = &unknown_handler;
528 (*handler->show) (vty, lsa);
529
530 vty_out (vty, "%s", VNL);
531 }
532
533 /* OSPFv3 LSA creation/deletion function */
534 struct ospf6_lsa *
535 ospf6_lsa_create (struct ospf6_lsa_header *header)
536 {
537 struct ospf6_lsa *lsa = NULL;
538 struct ospf6_lsa_header *new_header = NULL;
539 u_int16_t lsa_size = 0;
540
541 /* size of the entire LSA */
542 lsa_size = ntohs (header->length); /* XXX vulnerable */
543
544 /* allocate memory for this LSA */
545 new_header = (struct ospf6_lsa_header *)
546 XMALLOC (MTYPE_OSPF6_LSA, lsa_size);
547
548 /* copy LSA from original header */
549 memcpy (new_header, header, lsa_size);
550
551 /* LSA information structure */
552 /* allocate memory */
553 lsa = (struct ospf6_lsa *)
554 XCALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa));
555
556 lsa->header = (struct ospf6_lsa_header *) new_header;
557
558 /* dump string */
559 ospf6_lsa_printbuf (lsa, lsa->name, sizeof (lsa->name));
560
561 /* calculate birth of this lsa */
562 ospf6_lsa_age_set (lsa);
563
564 return lsa;
565 }
566
567 struct ospf6_lsa *
568 ospf6_lsa_create_headeronly (struct ospf6_lsa_header *header)
569 {
570 struct ospf6_lsa *lsa = NULL;
571 struct ospf6_lsa_header *new_header = NULL;
572
573 /* allocate memory for this LSA */
574 new_header = (struct ospf6_lsa_header *)
575 XMALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa_header));
576
577 /* copy LSA from original header */
578 memcpy (new_header, header, sizeof (struct ospf6_lsa_header));
579
580 /* LSA information structure */
581 /* allocate memory */
582 lsa = (struct ospf6_lsa *)
583 XCALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa));
584
585 lsa->header = (struct ospf6_lsa_header *) new_header;
586 SET_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY);
587
588 /* dump string */
589 ospf6_lsa_printbuf (lsa, lsa->name, sizeof (lsa->name));
590
591 /* calculate birth of this lsa */
592 ospf6_lsa_age_set (lsa);
593
594 return lsa;
595 }
596
597 void
598 ospf6_lsa_delete (struct ospf6_lsa *lsa)
599 {
600 assert (lsa->lock == 0);
601
602 /* cancel threads */
603 THREAD_OFF (lsa->expire);
604 THREAD_OFF (lsa->refresh);
605
606 /* do free */
607 XFREE (MTYPE_OSPF6_LSA, lsa->header);
608 XFREE (MTYPE_OSPF6_LSA, lsa);
609 }
610
611 struct ospf6_lsa *
612 ospf6_lsa_copy (struct ospf6_lsa *lsa)
613 {
614 struct ospf6_lsa *copy = NULL;
615
616 ospf6_lsa_age_current (lsa);
617 if (CHECK_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY))
618 copy = ospf6_lsa_create_headeronly (lsa->header);
619 else
620 copy = ospf6_lsa_create (lsa->header);
621 assert (copy->lock == 0);
622
623 copy->birth = lsa->birth;
624 copy->originated = lsa->originated;
625 copy->received = lsa->received;
626 copy->installed = lsa->installed;
627 copy->lsdb = lsa->lsdb;
628 copy->rn = NULL;
629
630 return copy;
631 }
632
633 /* increment reference counter of struct ospf6_lsa */
634 void
635 ospf6_lsa_lock (struct ospf6_lsa *lsa)
636 {
637 lsa->lock++;
638 return;
639 }
640
641 /* decrement reference counter of struct ospf6_lsa */
642 void
643 ospf6_lsa_unlock (struct ospf6_lsa *lsa)
644 {
645 /* decrement reference counter */
646 assert (lsa->lock > 0);
647 lsa->lock--;
648
649 if (lsa->lock != 0)
650 return;
651
652 ospf6_lsa_delete (lsa);
653 }
654
655
656 /* ospf6 lsa expiry */
657 int
658 ospf6_lsa_expire (struct thread *thread)
659 {
660 struct ospf6_lsa *lsa;
661
662 lsa = (struct ospf6_lsa *) THREAD_ARG (thread);
663
664 assert (lsa && lsa->header);
665 assert (OSPF6_LSA_IS_MAXAGE (lsa));
666 assert (! lsa->refresh);
667
668 lsa->expire = (struct thread *) NULL;
669
670 if (IS_OSPF6_DEBUG_LSA_TYPE (lsa->header->type))
671 {
672 zlog_debug ("LSA Expire:");
673 ospf6_lsa_header_print (lsa);
674 }
675
676 if (CHECK_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY))
677 return 0; /* dbexchange will do something ... */
678
679 /* reinstall lsa */
680 ospf6_install_lsa (lsa);
681
682 /* reflood lsa */
683 ospf6_flood (NULL, lsa);
684
685 /* schedule maxage remover */
686 ospf6_maxage_remove (ospf6);
687
688 return 0;
689 }
690
691 int
692 ospf6_lsa_refresh (struct thread *thread)
693 {
694 struct ospf6_lsa *old, *self, *new;
695 struct ospf6_lsdb *lsdb_self;
696
697 old = (struct ospf6_lsa *) THREAD_ARG (thread);
698 assert (old && old->header);
699
700 old->refresh = (struct thread *) NULL;
701
702 lsdb_self = ospf6_get_scoped_lsdb_self (old);
703 self = ospf6_lsdb_lookup (old->header->type, old->header->id,
704 old->header->adv_router, lsdb_self);
705 if (self == NULL)
706 {
707 if (IS_OSPF6_DEBUG_LSA_TYPE (old->header->type))
708 zlog_debug ("Refresh: could not find self LSA, flush %s", old->name);
709 ospf6_lsa_premature_aging (old);
710 return 0;
711 }
712
713 /* Reset age, increment LS sequence number. */
714 self->header->age = htons (0);
715 self->header->seqnum =
716 ospf6_new_ls_seqnum (self->header->type, self->header->id,
717 self->header->adv_router, old->lsdb);
718 ospf6_lsa_checksum (self->header);
719
720 new = ospf6_lsa_create (self->header);
721 new->lsdb = old->lsdb;
722 new->refresh = NULL;
723 thread_add_timer(master, ospf6_lsa_refresh, new, OSPF_LS_REFRESH_TIME,
724 &new->refresh);
725
726 /* store it in the LSDB for self-originated LSAs */
727 ospf6_lsdb_add (ospf6_lsa_copy (new), lsdb_self);
728
729 if (IS_OSPF6_DEBUG_LSA_TYPE (new->header->type))
730 {
731 zlog_debug ("LSA Refresh:");
732 ospf6_lsa_header_print (new);
733 }
734
735 ospf6_install_lsa (new);
736 ospf6_flood (NULL, new);
737
738 return 0;
739 }
740
741
742
743 /* Fletcher Checksum -- Refer to RFC1008. */
744
745 /* All the offsets are zero-based. The offsets in the RFC1008 are
746 one-based. */
747 unsigned short
748 ospf6_lsa_checksum (struct ospf6_lsa_header *lsa_header)
749 {
750 u_char *buffer = (u_char *) &lsa_header->type;
751 int type_offset = buffer - (u_char *) &lsa_header->age; /* should be 2 */
752
753 /* Skip the AGE field */
754 u_int16_t len = ntohs(lsa_header->length) - type_offset;
755
756 /* Checksum offset starts from "type" field, not the beginning of the
757 lsa_header struct. The offset is 14, rather than 16. */
758 int checksum_offset = (u_char *) &lsa_header->checksum - buffer;
759
760 return (unsigned short)fletcher_checksum(buffer, len, checksum_offset);
761 }
762
763 int
764 ospf6_lsa_checksum_valid (struct ospf6_lsa_header *lsa_header)
765 {
766 u_char *buffer = (u_char *) &lsa_header->type;
767 int type_offset = buffer - (u_char *) &lsa_header->age; /* should be 2 */
768
769 /* Skip the AGE field */
770 u_int16_t len = ntohs(lsa_header->length) - type_offset;
771
772 return (fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE) == 0);
773 }
774
775 void
776 ospf6_lsa_init (void)
777 {
778 ospf6_lsa_handler_vector = vector_init (0);
779 ospf6_install_lsa_handler (&unknown_handler);
780 }
781
782 void
783 ospf6_lsa_terminate (void)
784 {
785 vector_free (ospf6_lsa_handler_vector);
786 }
787
788 static char *
789 ospf6_lsa_handler_name (struct ospf6_lsa_handler *h)
790 {
791 static char buf[64];
792 unsigned int i;
793 unsigned int size = strlen (h->name);
794
795 if (!strcmp(h->name, "unknown") &&
796 h->type != OSPF6_LSTYPE_UNKNOWN)
797 {
798 snprintf (buf, sizeof (buf), "%#04hx", h->type);
799 return buf;
800 }
801
802 for (i = 0; i < MIN (size, sizeof (buf)); i++)
803 {
804 if (! islower ((unsigned char)h->name[i]))
805 buf[i] = tolower ((unsigned char)h->name[i]);
806 else
807 buf[i] = h->name[i];
808 }
809 buf[size] = '\0';
810 return buf;
811 }
812
813 DEFUN (debug_ospf6_lsa_type,
814 debug_ospf6_lsa_hex_cmd,
815 "debug ospf6 lsa <router|network|inter-prefix|inter-router|as-external|link|intra-prefix|unknown> [<originate|examine|flooding>]",
816 DEBUG_STR
817 OSPF6_STR
818 "Debug Link State Advertisements (LSAs)\n"
819 "Display Router LSAs\n"
820 "Display Network LSAs\n"
821 "Display Inter-Area-Prefix LSAs\n"
822 "Display Inter-Router LSAs\n"
823 "Display As-External LSAs\n"
824 "Display Link LSAs\n"
825 "Display Intra-Area-Prefix LSAs\n"
826 "Display LSAs of unknown origin\n"
827 "Display details of LSAs\n"
828 "Dump LSAs\n"
829 "Display LSA's internal information\n")
830 {
831 int idx_lsa = 3;
832 int idx_type = 4;
833 unsigned int i;
834 struct ospf6_lsa_handler *handler = NULL;
835
836 for (i = 0; i < vector_active (ospf6_lsa_handler_vector); i++)
837 {
838 handler = vector_slot (ospf6_lsa_handler_vector, i);
839 if (handler == NULL)
840 continue;
841 if (strncmp (argv[idx_lsa]->arg, ospf6_lsa_handler_name(handler), strlen(argv[idx_lsa]->arg)) == 0)
842 break;
843 if (! strcasecmp (argv[idx_lsa]->arg, handler->name))
844 break;
845 handler = NULL;
846 }
847
848 if (handler == NULL)
849 handler = &unknown_handler;
850
851 if (argc == 5)
852 {
853 if (! strcmp (argv[idx_type]->text, "originate"))
854 SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE);
855 else if (! strcmp (argv[idx_type]->text, "examine"))
856 SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN);
857 else if (! strcmp (argv[idx_type]->text, "flooding"))
858 SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD);
859 }
860 else
861 SET_FLAG (handler->debug, OSPF6_LSA_DEBUG);
862
863 return CMD_SUCCESS;
864 }
865
866 DEFUN (no_debug_ospf6_lsa_type,
867 no_debug_ospf6_lsa_hex_cmd,
868 "no debug ospf6 lsa <router|network|inter-prefix|inter-router|as-external|link|intra-prefix|unknown> [<originate|examine|flooding>]",
869 NO_STR
870 DEBUG_STR
871 OSPF6_STR
872 "Debug Link State Advertisements (LSAs)\n"
873 "Display Router LSAs\n"
874 "Display Network LSAs\n"
875 "Display Inter-Area-Prefix LSAs\n"
876 "Display Inter-Router LSAs\n"
877 "Display As-External LSAs\n"
878 "Display Link LSAs\n"
879 "Display Intra-Area-Prefix LSAs\n"
880 "Display LSAs of unknown origin\n"
881 "Display details of LSAs\n"
882 "Dump LSAs\n"
883 "Display LSA's internal information\n")
884 {
885 int idx_lsa = 4;
886 int idx_type = 5;
887 u_int i;
888 struct ospf6_lsa_handler *handler = NULL;
889
890 for (i = 0; i < vector_active (ospf6_lsa_handler_vector); i++)
891 {
892 handler = vector_slot (ospf6_lsa_handler_vector, i);
893 if (handler == NULL)
894 continue;
895 if (strncmp (argv[idx_lsa]->arg, ospf6_lsa_handler_name(handler), strlen(argv[idx_lsa]->arg)) == 0)
896 break;
897 if (! strcasecmp (argv[idx_lsa]->arg, handler->name))
898 break;
899 }
900
901 if (handler == NULL)
902 return CMD_SUCCESS;
903
904 if (argc == 6)
905 {
906 if (! strcmp (argv[idx_type]->text, "originate"))
907 UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE);
908 if (! strcmp (argv[idx_type]->text, "examine"))
909 UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN);
910 if (! strcmp (argv[idx_type]->text, "flooding"))
911 UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD);
912 }
913 else
914 UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG);
915
916 return CMD_SUCCESS;
917 }
918
919 void
920 install_element_ospf6_debug_lsa (void)
921 {
922 install_element (ENABLE_NODE, &debug_ospf6_lsa_hex_cmd);
923 install_element (ENABLE_NODE, &no_debug_ospf6_lsa_hex_cmd);
924 install_element (CONFIG_NODE, &debug_ospf6_lsa_hex_cmd);
925 install_element (CONFIG_NODE, &no_debug_ospf6_lsa_hex_cmd);
926 }
927
928 int
929 config_write_ospf6_debug_lsa (struct vty *vty)
930 {
931 u_int i;
932 struct ospf6_lsa_handler *handler;
933
934 for (i = 0; i < vector_active (ospf6_lsa_handler_vector); i++)
935 {
936 handler = vector_slot (ospf6_lsa_handler_vector, i);
937 if (handler == NULL)
938 continue;
939 if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG))
940 vty_out (vty, "debug ospf6 lsa %s%s",
941 ospf6_lsa_handler_name (handler), VNL);
942 if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE))
943 vty_out (vty, "debug ospf6 lsa %s originate%s",
944 ospf6_lsa_handler_name (handler), VNL);
945 if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN))
946 vty_out (vty, "debug ospf6 lsa %s examine%s",
947 ospf6_lsa_handler_name (handler), VNL);
948 if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD))
949 vty_out (vty, "debug ospf6 lsa %s flooding%s",
950 ospf6_lsa_handler_name (handler), VNL);
951 }
952
953 return 0;
954 }
955
956