ospf6_lsa.c 26 KB

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