ospf6_lsa.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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
  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.
  20. */
  21. #include <zebra.h>
  22. /* Include other stuffs */
  23. #include "log.h"
  24. #include "linklist.h"
  25. #include "vector.h"
  26. #include "vty.h"
  27. #include "command.h"
  28. #include "memory.h"
  29. #include "thread.h"
  30. #include "checksum.h"
  31. #include "ospf6_proto.h"
  32. #include "ospf6_lsa.h"
  33. #include "ospf6_lsdb.h"
  34. #include "ospf6_message.h"
  35. #include "ospf6_top.h"
  36. #include "ospf6_area.h"
  37. #include "ospf6_interface.h"
  38. #include "ospf6_neighbor.h"
  39. #include "ospf6_flood.h"
  40. #include "ospf6d.h"
  41. vector ospf6_lsa_handler_vector;
  42. static int
  43. ospf6_unknown_lsa_show (struct vty *vty, struct ospf6_lsa *lsa)
  44. {
  45. u_char *start, *end, *current;
  46. char byte[4];
  47. start = (u_char *) lsa->header + sizeof (struct ospf6_lsa_header);
  48. end = (u_char *) lsa->header + ntohs (lsa->header->length);
  49. vty_out (vty, " Unknown contents:%s", VNL);
  50. for (current = start; current < end; current ++)
  51. {
  52. if ((current - start) % 16 == 0)
  53. vty_out (vty, "%s ", VNL);
  54. else if ((current - start) % 4 == 0)
  55. vty_out (vty, " ");
  56. snprintf (byte, sizeof (byte), "%02x", *current);
  57. vty_out (vty, "%s", byte);
  58. }
  59. vty_out (vty, "%s%s", VNL, VNL);
  60. return 0;
  61. }
  62. struct ospf6_lsa_handler unknown_handler =
  63. {
  64. OSPF6_LSTYPE_UNKNOWN,
  65. "Unknown",
  66. ospf6_unknown_lsa_show,
  67. OSPF6_LSA_DEBUG,
  68. };
  69. void
  70. ospf6_install_lsa_handler (struct ospf6_lsa_handler *handler)
  71. {
  72. /* type in handler is host byte order */
  73. int index = handler->type & OSPF6_LSTYPE_FCODE_MASK;
  74. vector_set_index (ospf6_lsa_handler_vector, index, handler);
  75. }
  76. struct ospf6_lsa_handler *
  77. ospf6_get_lsa_handler (u_int16_t type)
  78. {
  79. struct ospf6_lsa_handler *handler = NULL;
  80. unsigned int index = ntohs (type) & OSPF6_LSTYPE_FCODE_MASK;
  81. if (index >= vector_active (ospf6_lsa_handler_vector))
  82. handler = &unknown_handler;
  83. else
  84. handler = vector_slot (ospf6_lsa_handler_vector, index);
  85. if (handler == NULL)
  86. handler = &unknown_handler;
  87. return handler;
  88. }
  89. const char *
  90. ospf6_lstype_name (u_int16_t type)
  91. {
  92. static char buf[8];
  93. struct ospf6_lsa_handler *handler;
  94. handler = ospf6_get_lsa_handler (type);
  95. if (handler && handler != &unknown_handler)
  96. return handler->name;
  97. snprintf (buf, sizeof (buf), "0x%04hx", ntohs (type));
  98. return buf;
  99. }
  100. u_char
  101. ospf6_lstype_debug (u_int16_t type)
  102. {
  103. struct ospf6_lsa_handler *handler;
  104. handler = ospf6_get_lsa_handler (type);
  105. return handler->debug;
  106. }
  107. /* RFC2328: Section 13.2 */
  108. int
  109. ospf6_lsa_is_differ (struct ospf6_lsa *lsa1,
  110. struct ospf6_lsa *lsa2)
  111. {
  112. int len;
  113. assert (OSPF6_LSA_IS_SAME (lsa1, lsa2));
  114. /* XXX, Options ??? */
  115. ospf6_lsa_age_current (lsa1);
  116. ospf6_lsa_age_current (lsa2);
  117. if (ntohs (lsa1->header->age) == MAXAGE &&
  118. ntohs (lsa2->header->age) != MAXAGE)
  119. return 1;
  120. if (ntohs (lsa1->header->age) != MAXAGE &&
  121. ntohs (lsa2->header->age) == MAXAGE)
  122. return 1;
  123. /* compare body */
  124. if (ntohs (lsa1->header->length) != ntohs (lsa2->header->length))
  125. return 1;
  126. len = ntohs (lsa1->header->length) - sizeof (struct ospf6_lsa_header);
  127. return memcmp (lsa1->header + 1, lsa2->header + 1, len);
  128. }
  129. int
  130. ospf6_lsa_is_changed (struct ospf6_lsa *lsa1,
  131. struct ospf6_lsa *lsa2)
  132. {
  133. int length;
  134. if (OSPF6_LSA_IS_MAXAGE (lsa1) ^ OSPF6_LSA_IS_MAXAGE (lsa2))
  135. return 1;
  136. if (ntohs (lsa1->header->length) != ntohs (lsa2->header->length))
  137. return 1;
  138. /* Going beyond LSA headers to compare the payload only makes sense, when both LSAs aren't header-only. */
  139. if (CHECK_FLAG (lsa1->flag, OSPF6_LSA_HEADERONLY) != CHECK_FLAG (lsa2->flag, OSPF6_LSA_HEADERONLY))
  140. {
  141. zlog_warn ("%s: only one of two (%s, %s) LSAs compared is header-only", __func__, lsa1->name, lsa2->name);
  142. return 1;
  143. }
  144. if (CHECK_FLAG (lsa1->flag, OSPF6_LSA_HEADERONLY))
  145. return 0;
  146. length = OSPF6_LSA_SIZE (lsa1->header) - sizeof (struct ospf6_lsa_header);
  147. /* Once upper layer verifies LSAs received, length underrun should become a warning. */
  148. if (length <= 0)
  149. return 0;
  150. return memcmp (OSPF6_LSA_HEADER_END (lsa1->header),
  151. OSPF6_LSA_HEADER_END (lsa2->header), length);
  152. }
  153. /* ospf6 age functions */
  154. /* calculate birth */
  155. static void
  156. ospf6_lsa_age_set (struct ospf6_lsa *lsa)
  157. {
  158. struct timeval now;
  159. assert (lsa && lsa->header);
  160. if (quagga_gettime (QUAGGA_CLK_MONOTONIC, &now) < 0)
  161. zlog_warn ("LSA: quagga_gettime failed, may fail LSA AGEs: %s",
  162. safe_strerror (errno));
  163. lsa->birth.tv_sec = now.tv_sec - ntohs (lsa->header->age);
  164. lsa->birth.tv_usec = now.tv_usec;
  165. return;
  166. }
  167. /* this function calculates current age from its birth,
  168. then update age field of LSA header. return value is current age */
  169. u_int16_t
  170. ospf6_lsa_age_current (struct ospf6_lsa *lsa)
  171. {
  172. struct timeval now;
  173. u_int32_t ulage;
  174. u_int16_t age;
  175. assert (lsa);
  176. assert (lsa->header);
  177. /* current time */
  178. if (quagga_gettime (QUAGGA_CLK_MONOTONIC, &now) < 0)
  179. zlog_warn ("LSA: quagga_gettime failed, may fail LSA AGEs: %s",
  180. safe_strerror (errno));
  181. if (ntohs (lsa->header->age) >= MAXAGE)
  182. {
  183. /* ospf6_lsa_premature_aging () sets age to MAXAGE; when using
  184. relative time, we cannot compare against lsa birth time, so
  185. we catch this special case here. */
  186. lsa->header->age = htons (MAXAGE);
  187. return MAXAGE;
  188. }
  189. /* calculate age */
  190. ulage = now.tv_sec - lsa->birth.tv_sec;
  191. /* if over MAXAGE, set to it */
  192. age = (ulage > MAXAGE ? MAXAGE : ulage);
  193. lsa->header->age = htons (age);
  194. return age;
  195. }
  196. /* update age field of LSA header with adding InfTransDelay */
  197. void
  198. ospf6_lsa_age_update_to_send (struct ospf6_lsa *lsa, u_int32_t transdelay)
  199. {
  200. unsigned short age;
  201. age = ospf6_lsa_age_current (lsa) + transdelay;
  202. if (age > MAXAGE)
  203. age = MAXAGE;
  204. lsa->header->age = htons (age);
  205. }
  206. void
  207. ospf6_lsa_premature_aging (struct ospf6_lsa *lsa)
  208. {
  209. /* log */
  210. if (IS_OSPF6_DEBUG_LSA_TYPE (lsa->header->type))
  211. zlog_debug ("LSA: Premature aging: %s", lsa->name);
  212. THREAD_OFF (lsa->expire);
  213. THREAD_OFF (lsa->refresh);
  214. lsa->header->age = htons (MAXAGE);
  215. thread_execute (master, ospf6_lsa_expire, lsa, 0);
  216. }
  217. /* check which is more recent. if a is more recent, return -1;
  218. if the same, return 0; otherwise(b is more recent), return 1 */
  219. int
  220. ospf6_lsa_compare (struct ospf6_lsa *a, struct ospf6_lsa *b)
  221. {
  222. int seqnuma, seqnumb;
  223. u_int16_t cksuma, cksumb;
  224. u_int16_t agea, ageb;
  225. assert (a && a->header);
  226. assert (b && b->header);
  227. assert (OSPF6_LSA_IS_SAME (a, b));
  228. seqnuma = (int) ntohl (a->header->seqnum);
  229. seqnumb = (int) ntohl (b->header->seqnum);
  230. /* compare by sequence number */
  231. if (seqnuma > seqnumb)
  232. return -1;
  233. if (seqnuma < seqnumb)
  234. return 1;
  235. /* Checksum */
  236. cksuma = ntohs (a->header->checksum);
  237. cksumb = ntohs (b->header->checksum);
  238. if (cksuma > cksumb)
  239. return -1;
  240. if (cksuma < cksumb)
  241. return 0;
  242. /* Update Age */
  243. agea = ospf6_lsa_age_current (a);
  244. ageb = ospf6_lsa_age_current (b);
  245. /* MaxAge check */
  246. if (agea == MAXAGE && ageb != MAXAGE)
  247. return -1;
  248. else if (agea != MAXAGE && ageb == MAXAGE)
  249. return 1;
  250. /* Age check */
  251. if (agea > ageb && agea - ageb >= MAX_AGE_DIFF)
  252. return 1;
  253. else if (agea < ageb && ageb - agea >= MAX_AGE_DIFF)
  254. return -1;
  255. /* neither recent */
  256. return 0;
  257. }
  258. char *
  259. ospf6_lsa_printbuf (struct ospf6_lsa *lsa, char *buf, int size)
  260. {
  261. char id[16], adv_router[16];
  262. inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
  263. inet_ntop (AF_INET, &lsa->header->adv_router, adv_router,
  264. sizeof (adv_router));
  265. snprintf (buf, size, "[%s Id:%s Adv:%s]",
  266. ospf6_lstype_name (lsa->header->type), id, adv_router);
  267. return buf;
  268. }
  269. void
  270. ospf6_lsa_header_print_raw (struct ospf6_lsa_header *header)
  271. {
  272. char id[16], adv_router[16];
  273. inet_ntop (AF_INET, &header->id, id, sizeof (id));
  274. inet_ntop (AF_INET, &header->adv_router, adv_router,
  275. sizeof (adv_router));
  276. zlog_debug (" [%s Id:%s Adv:%s]",
  277. ospf6_lstype_name (header->type), id, adv_router);
  278. zlog_debug (" Age: %4hu SeqNum: %#08lx Cksum: %04hx Len: %d",
  279. ntohs (header->age), (u_long) ntohl (header->seqnum),
  280. ntohs (header->checksum), ntohs (header->length));
  281. }
  282. void
  283. ospf6_lsa_header_print (struct ospf6_lsa *lsa)
  284. {
  285. ospf6_lsa_age_current (lsa);
  286. ospf6_lsa_header_print_raw (lsa->header);
  287. }
  288. void
  289. ospf6_lsa_show_summary_header (struct vty *vty)
  290. {
  291. vty_out (vty, "%-12s %-15s %-15s %4s %8s %4s %4s %-8s%s",
  292. "Type", "LSId", "AdvRouter", "Age", "SeqNum",
  293. "Cksm", "Len", "Duration", VNL);
  294. }
  295. void
  296. ospf6_lsa_show_summary (struct vty *vty, struct ospf6_lsa *lsa)
  297. {
  298. char adv_router[16], id[16];
  299. struct timeval now, res;
  300. char duration[16];
  301. assert (lsa);
  302. assert (lsa->header);
  303. inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
  304. inet_ntop (AF_INET, &lsa->header->adv_router, adv_router,
  305. sizeof (adv_router));
  306. quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
  307. timersub (&now, &lsa->installed, &res);
  308. timerstring (&res, duration, sizeof (duration));
  309. vty_out (vty, "%-12s %-15s %-15s %4hu %8lx %04hx %4hu %8s%s",
  310. ospf6_lstype_name (lsa->header->type),
  311. id, adv_router, ospf6_lsa_age_current (lsa),
  312. (u_long) ntohl (lsa->header->seqnum),
  313. ntohs (lsa->header->checksum), ntohs (lsa->header->length),
  314. duration, VNL);
  315. }
  316. void
  317. ospf6_lsa_show_dump (struct vty *vty, struct ospf6_lsa *lsa)
  318. {
  319. u_char *start, *end, *current;
  320. char byte[4];
  321. start = (u_char *) lsa->header;
  322. end = (u_char *) lsa->header + ntohs (lsa->header->length);
  323. vty_out (vty, "%s", VNL);
  324. vty_out (vty, "%s:%s", lsa->name, VNL);
  325. for (current = start; current < end; current ++)
  326. {
  327. if ((current - start) % 16 == 0)
  328. vty_out (vty, "%s ", VNL);
  329. else if ((current - start) % 4 == 0)
  330. vty_out (vty, " ");
  331. snprintf (byte, sizeof (byte), "%02x", *current);
  332. vty_out (vty, "%s", byte);
  333. }
  334. vty_out (vty, "%s%s", VNL, VNL);
  335. return;
  336. }
  337. void
  338. ospf6_lsa_show_internal (struct vty *vty, struct ospf6_lsa *lsa)
  339. {
  340. char adv_router[64], id[64];
  341. assert (lsa && lsa->header);
  342. inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
  343. inet_ntop (AF_INET, &lsa->header->adv_router,
  344. adv_router, sizeof (adv_router));
  345. vty_out (vty, "%s", VNL);
  346. vty_out (vty, "Age: %4hu Type: %s%s", ospf6_lsa_age_current (lsa),
  347. ospf6_lstype_name (lsa->header->type), VNL);
  348. vty_out (vty, "Link State ID: %s%s", id, VNL);
  349. vty_out (vty, "Advertising Router: %s%s", adv_router, VNL);
  350. vty_out (vty, "LS Sequence Number: %#010lx%s",
  351. (u_long) ntohl (lsa->header->seqnum), VNL);
  352. vty_out (vty, "CheckSum: %#06hx Length: %hu%s",
  353. ntohs (lsa->header->checksum),
  354. ntohs (lsa->header->length), VNL);
  355. vty_out (vty, " Prev: %p This: %p Next: %p%s",
  356. lsa->prev, lsa, lsa->next, VNL);
  357. vty_out (vty, "%s", VNL);
  358. return;
  359. }
  360. void
  361. ospf6_lsa_show (struct vty *vty, struct ospf6_lsa *lsa)
  362. {
  363. char adv_router[64], id[64];
  364. struct ospf6_lsa_handler *handler;
  365. assert (lsa && lsa->header);
  366. inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));
  367. inet_ntop (AF_INET, &lsa->header->adv_router,
  368. adv_router, sizeof (adv_router));
  369. vty_out (vty, "Age: %4hu Type: %s%s", ospf6_lsa_age_current (lsa),
  370. ospf6_lstype_name (lsa->header->type), VNL);
  371. vty_out (vty, "Link State ID: %s%s", id, VNL);
  372. vty_out (vty, "Advertising Router: %s%s", adv_router, VNL);
  373. vty_out (vty, "LS Sequence Number: %#010lx%s",
  374. (u_long) ntohl (lsa->header->seqnum), VNL);
  375. vty_out (vty, "CheckSum: %#06hx Length: %hu%s",
  376. ntohs (lsa->header->checksum),
  377. ntohs (lsa->header->length), VNL);
  378. handler = ospf6_get_lsa_handler (lsa->header->type);
  379. if (handler->show == NULL)
  380. handler = &unknown_handler;
  381. (*handler->show) (vty, lsa);
  382. vty_out (vty, "%s", VNL);
  383. }
  384. /* OSPFv3 LSA creation/deletion function */
  385. struct ospf6_lsa *
  386. ospf6_lsa_create (struct ospf6_lsa_header *header)
  387. {
  388. struct ospf6_lsa *lsa = NULL;
  389. struct ospf6_lsa_header *new_header = NULL;
  390. u_int16_t lsa_size = 0;
  391. /* size of the entire LSA */
  392. lsa_size = ntohs (header->length); /* XXX vulnerable */
  393. /* allocate memory for this LSA */
  394. new_header = (struct ospf6_lsa_header *)
  395. XMALLOC (MTYPE_OSPF6_LSA, lsa_size);
  396. /* copy LSA from original header */
  397. memcpy (new_header, header, lsa_size);
  398. /* LSA information structure */
  399. /* allocate memory */
  400. lsa = (struct ospf6_lsa *)
  401. XCALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa));
  402. lsa->header = (struct ospf6_lsa_header *) new_header;
  403. /* dump string */
  404. ospf6_lsa_printbuf (lsa, lsa->name, sizeof (lsa->name));
  405. /* calculate birth of this lsa */
  406. ospf6_lsa_age_set (lsa);
  407. return lsa;
  408. }
  409. struct ospf6_lsa *
  410. ospf6_lsa_create_headeronly (struct ospf6_lsa_header *header)
  411. {
  412. struct ospf6_lsa *lsa = NULL;
  413. struct ospf6_lsa_header *new_header = NULL;
  414. /* allocate memory for this LSA */
  415. new_header = (struct ospf6_lsa_header *)
  416. XMALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa_header));
  417. /* copy LSA from original header */
  418. memcpy (new_header, header, sizeof (struct ospf6_lsa_header));
  419. /* LSA information structure */
  420. /* allocate memory */
  421. lsa = (struct ospf6_lsa *)
  422. XCALLOC (MTYPE_OSPF6_LSA, sizeof (struct ospf6_lsa));
  423. lsa->header = (struct ospf6_lsa_header *) new_header;
  424. SET_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY);
  425. /* dump string */
  426. ospf6_lsa_printbuf (lsa, lsa->name, sizeof (lsa->name));
  427. /* calculate birth of this lsa */
  428. ospf6_lsa_age_set (lsa);
  429. return lsa;
  430. }
  431. void
  432. ospf6_lsa_delete (struct ospf6_lsa *lsa)
  433. {
  434. assert (lsa->lock == 0);
  435. /* cancel threads */
  436. THREAD_OFF (lsa->expire);
  437. THREAD_OFF (lsa->refresh);
  438. /* do free */
  439. XFREE (MTYPE_OSPF6_LSA, lsa->header);
  440. XFREE (MTYPE_OSPF6_LSA, lsa);
  441. }
  442. struct ospf6_lsa *
  443. ospf6_lsa_copy (struct ospf6_lsa *lsa)
  444. {
  445. struct ospf6_lsa *copy = NULL;
  446. ospf6_lsa_age_current (lsa);
  447. if (CHECK_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY))
  448. copy = ospf6_lsa_create_headeronly (lsa->header);
  449. else
  450. copy = ospf6_lsa_create (lsa->header);
  451. assert (copy->lock == 0);
  452. copy->birth = lsa->birth;
  453. copy->originated = lsa->originated;
  454. copy->received = lsa->received;
  455. copy->installed = lsa->installed;
  456. copy->lsdb = lsa->lsdb;
  457. return copy;
  458. }
  459. /* increment reference counter of struct ospf6_lsa */
  460. void
  461. ospf6_lsa_lock (struct ospf6_lsa *lsa)
  462. {
  463. lsa->lock++;
  464. return;
  465. }
  466. /* decrement reference counter of struct ospf6_lsa */
  467. void
  468. ospf6_lsa_unlock (struct ospf6_lsa *lsa)
  469. {
  470. /* decrement reference counter */
  471. assert (lsa->lock > 0);
  472. lsa->lock--;
  473. if (lsa->lock != 0)
  474. return;
  475. ospf6_lsa_delete (lsa);
  476. }
  477. /* ospf6 lsa expiry */
  478. int
  479. ospf6_lsa_expire (struct thread *thread)
  480. {
  481. struct ospf6_lsa *lsa;
  482. lsa = (struct ospf6_lsa *) THREAD_ARG (thread);
  483. assert (lsa && lsa->header);
  484. assert (OSPF6_LSA_IS_MAXAGE (lsa));
  485. assert (! lsa->refresh);
  486. lsa->expire = (struct thread *) NULL;
  487. if (IS_OSPF6_DEBUG_LSA_TYPE (lsa->header->type))
  488. {
  489. zlog_debug ("LSA Expire:");
  490. ospf6_lsa_header_print (lsa);
  491. }
  492. if (CHECK_FLAG (lsa->flag, OSPF6_LSA_HEADERONLY))
  493. return 0; /* dbexchange will do something ... */
  494. /* reflood lsa */
  495. ospf6_flood (NULL, lsa);
  496. /* reinstall lsa */
  497. ospf6_install_lsa (lsa);
  498. /* schedule maxage remover */
  499. ospf6_maxage_remove (ospf6);
  500. return 0;
  501. }
  502. int
  503. ospf6_lsa_refresh (struct thread *thread)
  504. {
  505. struct ospf6_lsa *old, *self, *new;
  506. struct ospf6_lsdb *lsdb_self;
  507. assert (thread);
  508. old = (struct ospf6_lsa *) THREAD_ARG (thread);
  509. assert (old && old->header);
  510. old->refresh = (struct thread *) NULL;
  511. lsdb_self = ospf6_get_scoped_lsdb_self (old);
  512. self = ospf6_lsdb_lookup (old->header->type, old->header->id,
  513. old->header->adv_router, lsdb_self);
  514. if (self == NULL)
  515. {
  516. if (IS_OSPF6_DEBUG_LSA_TYPE (old->header->type))
  517. zlog_debug ("Refresh: could not find self LSA, flush %s", old->name);
  518. ospf6_lsa_premature_aging (old);
  519. return 0;
  520. }
  521. /* Reset age, increment LS sequence number. */
  522. self->header->age = htons (0);
  523. self->header->seqnum =
  524. ospf6_new_ls_seqnum (self->header->type, self->header->id,
  525. self->header->adv_router, old->lsdb);
  526. ospf6_lsa_checksum (self->header);
  527. new = ospf6_lsa_create (self->header);
  528. new->lsdb = old->lsdb;
  529. new->refresh = thread_add_timer (master, ospf6_lsa_refresh, new,
  530. LS_REFRESH_TIME);
  531. /* store it in the LSDB for self-originated LSAs */
  532. ospf6_lsdb_add (ospf6_lsa_copy (new), lsdb_self);
  533. if (IS_OSPF6_DEBUG_LSA_TYPE (new->header->type))
  534. {
  535. zlog_debug ("LSA Refresh:");
  536. ospf6_lsa_header_print (new);
  537. }
  538. ospf6_flood_clear (old);
  539. ospf6_flood (NULL, new);
  540. ospf6_install_lsa (new);
  541. return 0;
  542. }
  543. /* Fletcher Checksum -- Refer to RFC1008. */
  544. /* All the offsets are zero-based. The offsets in the RFC1008 are
  545. one-based. */
  546. unsigned short
  547. ospf6_lsa_checksum (struct ospf6_lsa_header *lsa_header)
  548. {
  549. u_char *buffer = (u_char *) &lsa_header->type;
  550. int type_offset = buffer - (u_char *) &lsa_header->age; /* should be 2 */
  551. /* Skip the AGE field */
  552. u_int16_t len = ntohs(lsa_header->length) - type_offset;
  553. /* Checksum offset starts from "type" field, not the beginning of the
  554. lsa_header struct. The offset is 14, rather than 16. */
  555. int checksum_offset = (u_char *) &lsa_header->checksum - buffer;
  556. return (unsigned short)fletcher_checksum(buffer, len, checksum_offset);
  557. }
  558. int
  559. ospf6_lsa_checksum_valid (struct ospf6_lsa_header *lsa_header)
  560. {
  561. u_char *buffer = (u_char *) &lsa_header->type;
  562. int type_offset = buffer - (u_char *) &lsa_header->age; /* should be 2 */
  563. /* Skip the AGE field */
  564. u_int16_t len = ntohs(lsa_header->length) - type_offset;
  565. return (fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE) == 0);
  566. }
  567. void
  568. ospf6_lsa_init (void)
  569. {
  570. ospf6_lsa_handler_vector = vector_init (0);
  571. ospf6_install_lsa_handler (&unknown_handler);
  572. }
  573. void
  574. ospf6_lsa_terminate (void)
  575. {
  576. vector_free (ospf6_lsa_handler_vector);
  577. }
  578. static char *
  579. ospf6_lsa_handler_name (struct ospf6_lsa_handler *h)
  580. {
  581. static char buf[64];
  582. unsigned int i;
  583. unsigned int size = strlen (h->name);
  584. if (!strcmp(h->name, "Unknown") &&
  585. h->type != OSPF6_LSTYPE_UNKNOWN)
  586. {
  587. snprintf (buf, sizeof (buf), "%#04hx", h->type);
  588. return buf;
  589. }
  590. for (i = 0; i < MIN (size, sizeof (buf)); i++)
  591. {
  592. if (! islower (h->name[i]))
  593. buf[i] = tolower (h->name[i]);
  594. else
  595. buf[i] = h->name[i];
  596. }
  597. buf[size] = '\0';
  598. return buf;
  599. }
  600. DEFUN (debug_ospf6_lsa_type,
  601. debug_ospf6_lsa_hex_cmd,
  602. "debug ospf6 lsa XXXX/0xXXXX",
  603. DEBUG_STR
  604. OSPF6_STR
  605. "Debug Link State Advertisements (LSAs)\n"
  606. "Specify LS type as Hexadecimal\n"
  607. )
  608. {
  609. unsigned int i;
  610. struct ospf6_lsa_handler *handler = NULL;
  611. unsigned long val;
  612. char *endptr = NULL;
  613. u_int16_t type = 0;
  614. assert (argc);
  615. if ((strlen (argv[0]) == 6 && ! strncmp (argv[0], "0x", 2)) ||
  616. (strlen (argv[0]) == 4))
  617. {
  618. val = strtoul (argv[0], &endptr, 16);
  619. if (*endptr == '\0')
  620. type = val;
  621. }
  622. for (i = 0; i < vector_active (ospf6_lsa_handler_vector); i++)
  623. {
  624. handler = vector_slot (ospf6_lsa_handler_vector, i);
  625. if (handler == NULL)
  626. continue;
  627. if (type && handler->type == type)
  628. break;
  629. if (! strcasecmp (argv[0], handler->name))
  630. break;
  631. handler = NULL;
  632. }
  633. if (type && handler == NULL)
  634. {
  635. handler = (struct ospf6_lsa_handler *)
  636. malloc (sizeof (struct ospf6_lsa_handler));
  637. memset (handler, 0, sizeof (struct ospf6_lsa_handler));
  638. handler->type = type;
  639. handler->name = "Unknown";
  640. handler->show = ospf6_unknown_lsa_show;
  641. vector_set_index (ospf6_lsa_handler_vector,
  642. handler->type & OSPF6_LSTYPE_FCODE_MASK, handler);
  643. }
  644. if (handler == NULL)
  645. handler = &unknown_handler;
  646. if (argc >= 2)
  647. {
  648. if (! strcmp (argv[1], "originate"))
  649. SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE);
  650. if (! strcmp (argv[1], "examin"))
  651. SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN);
  652. if (! strcmp (argv[1], "flooding"))
  653. SET_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD);
  654. }
  655. else
  656. SET_FLAG (handler->debug, OSPF6_LSA_DEBUG);
  657. return CMD_SUCCESS;
  658. }
  659. DEFUN (no_debug_ospf6_lsa_type,
  660. no_debug_ospf6_lsa_hex_cmd,
  661. "no debug ospf6 lsa XXXX/0xXXXX",
  662. NO_STR
  663. DEBUG_STR
  664. OSPF6_STR
  665. "Debug Link State Advertisements (LSAs)\n"
  666. "Specify LS type as Hexadecimal\n"
  667. )
  668. {
  669. u_int i;
  670. struct ospf6_lsa_handler *handler = NULL;
  671. unsigned long val;
  672. char *endptr = NULL;
  673. u_int16_t type = 0;
  674. assert (argc);
  675. if ((strlen (argv[0]) == 6 && ! strncmp (argv[0], "0x", 2)) ||
  676. (strlen (argv[0]) == 4))
  677. {
  678. val = strtoul (argv[0], &endptr, 16);
  679. if (*endptr == '\0')
  680. type = val;
  681. }
  682. for (i = 0; i < vector_active (ospf6_lsa_handler_vector); i++)
  683. {
  684. handler = vector_slot (ospf6_lsa_handler_vector, i);
  685. if (handler == NULL)
  686. continue;
  687. if (type && handler->type == type)
  688. break;
  689. if (! strcasecmp (argv[0], handler->name))
  690. break;
  691. }
  692. if (handler == NULL)
  693. return CMD_SUCCESS;
  694. if (argc >= 2)
  695. {
  696. if (! strcmp (argv[1], "originate"))
  697. UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE);
  698. if (! strcmp (argv[1], "examin"))
  699. UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN);
  700. if (! strcmp (argv[1], "flooding"))
  701. UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD);
  702. }
  703. else
  704. UNSET_FLAG (handler->debug, OSPF6_LSA_DEBUG);
  705. if (handler->debug == 0 &&
  706. !strcmp(handler->name, "Unknown") && type != OSPF6_LSTYPE_UNKNOWN)
  707. {
  708. free (handler);
  709. vector_slot (ospf6_lsa_handler_vector, i) = NULL;
  710. }
  711. return CMD_SUCCESS;
  712. }
  713. struct cmd_element debug_ospf6_lsa_type_cmd;
  714. struct cmd_element debug_ospf6_lsa_type_detail_cmd;
  715. struct cmd_element no_debug_ospf6_lsa_type_cmd;
  716. struct cmd_element no_debug_ospf6_lsa_type_detail_cmd;
  717. void
  718. install_element_ospf6_debug_lsa (void)
  719. {
  720. u_int i;
  721. struct ospf6_lsa_handler *handler;
  722. #define STRSIZE 256
  723. #define DOCSIZE 1024
  724. static char strbuf[STRSIZE];
  725. static char docbuf[DOCSIZE];
  726. static char detail_strbuf[STRSIZE];
  727. static char detail_docbuf[DOCSIZE];
  728. char *str, *no_str;
  729. char *doc, *no_doc;
  730. strbuf[0] = '\0';
  731. no_str = &strbuf[strlen (strbuf)];
  732. strncat (strbuf, "no ", STRSIZE - strlen (strbuf));
  733. str = &strbuf[strlen (strbuf)];
  734. strncat (strbuf, "debug ospf6 lsa (", STRSIZE - strlen (strbuf));
  735. for (i = 0; i < vector_active (ospf6_lsa_handler_vector); i++)
  736. {
  737. handler = vector_slot (ospf6_lsa_handler_vector, i);
  738. if (handler == NULL)
  739. continue;
  740. strncat (strbuf, ospf6_lsa_handler_name (handler),
  741. STRSIZE - strlen (strbuf));
  742. strncat (strbuf, "|", STRSIZE - strlen (strbuf));
  743. }
  744. strbuf[strlen (strbuf) - 1] = ')';
  745. strbuf[strlen (strbuf)] = '\0';
  746. docbuf[0] = '\0';
  747. no_doc = &docbuf[strlen (docbuf)];
  748. strncat (docbuf, NO_STR, DOCSIZE - strlen (docbuf));
  749. doc = &docbuf[strlen (docbuf)];
  750. strncat (docbuf, DEBUG_STR, DOCSIZE - strlen (docbuf));
  751. strncat (docbuf, OSPF6_STR, DOCSIZE - strlen (docbuf));
  752. strncat (docbuf, "Debug Link State Advertisements (LSAs)\n",
  753. DOCSIZE - strlen (docbuf));
  754. for (i = 0; i < vector_active (ospf6_lsa_handler_vector); i++)
  755. {
  756. handler = vector_slot (ospf6_lsa_handler_vector, i);
  757. if (handler == NULL)
  758. continue;
  759. strncat (docbuf, "Debug ", DOCSIZE - strlen (docbuf));
  760. strncat (docbuf, handler->name, DOCSIZE - strlen (docbuf));
  761. strncat (docbuf, "-LSA\n", DOCSIZE - strlen (docbuf));
  762. }
  763. docbuf[strlen (docbuf)] = '\0';
  764. debug_ospf6_lsa_type_cmd.string = str;
  765. debug_ospf6_lsa_type_cmd.func = debug_ospf6_lsa_type;
  766. debug_ospf6_lsa_type_cmd.doc = doc;
  767. no_debug_ospf6_lsa_type_cmd.string = no_str;
  768. no_debug_ospf6_lsa_type_cmd.func = no_debug_ospf6_lsa_type;
  769. no_debug_ospf6_lsa_type_cmd.doc = no_doc;
  770. strncpy (detail_strbuf, strbuf, STRSIZE);
  771. strncat (detail_strbuf, " (originate|examin|flooding)",
  772. STRSIZE - strlen (detail_strbuf));
  773. detail_strbuf[strlen (detail_strbuf)] = '\0';
  774. no_str = &detail_strbuf[0];
  775. str = &detail_strbuf[strlen ("no ")];
  776. strncpy (detail_docbuf, docbuf, DOCSIZE);
  777. strncat (detail_docbuf, "Debug Originating LSA\n",
  778. DOCSIZE - strlen (detail_docbuf));
  779. strncat (detail_docbuf, "Debug Examining LSA\n",
  780. DOCSIZE - strlen (detail_docbuf));
  781. strncat (detail_docbuf, "Debug Flooding LSA\n",
  782. DOCSIZE - strlen (detail_docbuf));
  783. detail_docbuf[strlen (detail_docbuf)] = '\0';
  784. no_doc = &detail_docbuf[0];
  785. doc = &detail_docbuf[strlen (NO_STR)];
  786. debug_ospf6_lsa_type_detail_cmd.string = str;
  787. debug_ospf6_lsa_type_detail_cmd.func = debug_ospf6_lsa_type;
  788. debug_ospf6_lsa_type_detail_cmd.doc = doc;
  789. no_debug_ospf6_lsa_type_detail_cmd.string = no_str;
  790. no_debug_ospf6_lsa_type_detail_cmd.func = no_debug_ospf6_lsa_type;
  791. no_debug_ospf6_lsa_type_detail_cmd.doc = no_doc;
  792. install_element (ENABLE_NODE, &debug_ospf6_lsa_hex_cmd);
  793. install_element (ENABLE_NODE, &debug_ospf6_lsa_type_cmd);
  794. install_element (ENABLE_NODE, &debug_ospf6_lsa_type_detail_cmd);
  795. install_element (ENABLE_NODE, &no_debug_ospf6_lsa_hex_cmd);
  796. install_element (ENABLE_NODE, &no_debug_ospf6_lsa_type_cmd);
  797. install_element (ENABLE_NODE, &no_debug_ospf6_lsa_type_detail_cmd);
  798. install_element (CONFIG_NODE, &debug_ospf6_lsa_hex_cmd);
  799. install_element (CONFIG_NODE, &debug_ospf6_lsa_type_cmd);
  800. install_element (CONFIG_NODE, &debug_ospf6_lsa_type_detail_cmd);
  801. install_element (CONFIG_NODE, &no_debug_ospf6_lsa_hex_cmd);
  802. install_element (CONFIG_NODE, &no_debug_ospf6_lsa_type_cmd);
  803. install_element (CONFIG_NODE, &no_debug_ospf6_lsa_type_detail_cmd);
  804. }
  805. int
  806. config_write_ospf6_debug_lsa (struct vty *vty)
  807. {
  808. u_int i;
  809. struct ospf6_lsa_handler *handler;
  810. for (i = 0; i < vector_active (ospf6_lsa_handler_vector); i++)
  811. {
  812. handler = vector_slot (ospf6_lsa_handler_vector, i);
  813. if (handler == NULL)
  814. continue;
  815. if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG))
  816. vty_out (vty, "debug ospf6 lsa %s%s",
  817. ospf6_lsa_handler_name (handler), VNL);
  818. if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_ORIGINATE))
  819. vty_out (vty, "debug ospf6 lsa %s originate%s",
  820. ospf6_lsa_handler_name (handler), VNL);
  821. if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_EXAMIN))
  822. vty_out (vty, "debug ospf6 lsa %s examin%s",
  823. ospf6_lsa_handler_name (handler), VNL);
  824. if (CHECK_FLAG (handler->debug, OSPF6_LSA_DEBUG_FLOOD))
  825. vty_out (vty, "debug ospf6 lsa %s flooding%s",
  826. ospf6_lsa_handler_name (handler), VNL);
  827. }
  828. return 0;
  829. }