ospf6_lsa.c 26 KB

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