ospf6_message.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  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 "memory.h"
  23. #include "log.h"
  24. #include "vty.h"
  25. #include "command.h"
  26. #include "thread.h"
  27. #include "linklist.h"
  28. #include "ospf6_proto.h"
  29. #include "ospf6_lsa.h"
  30. #include "ospf6_lsdb.h"
  31. #include "ospf6_network.h"
  32. #include "ospf6_message.h"
  33. #include "ospf6_top.h"
  34. #include "ospf6_area.h"
  35. #include "ospf6_neighbor.h"
  36. #include "ospf6_interface.h"
  37. #include "ospf6_flood.h"
  38. #include "ospf6d.h"
  39. unsigned char conf_debug_ospf6_message[6] = {0x03, 0, 0, 0, 0, 0};
  40. const char *ospf6_message_type_str[] =
  41. { "Unknown", "Hello", "DbDesc", "LSReq", "LSUpdate", "LSAck" };
  42. /* print functions */
  43. static void
  44. ospf6_header_print (struct ospf6_header *oh)
  45. {
  46. char router_id[16], area_id[16];
  47. inet_ntop (AF_INET, &oh->router_id, router_id, sizeof (router_id));
  48. inet_ntop (AF_INET, &oh->area_id, area_id, sizeof (area_id));
  49. zlog_debug (" OSPFv%d Type:%d Len:%hu Router-ID:%s",
  50. oh->version, oh->type, ntohs (oh->length), router_id);
  51. zlog_debug (" Area-ID:%s Cksum:%hx Instance-ID:%d",
  52. area_id, ntohs (oh->checksum), oh->instance_id);
  53. }
  54. void
  55. ospf6_hello_print (struct ospf6_header *oh)
  56. {
  57. struct ospf6_hello *hello;
  58. char options[16];
  59. char drouter[16], bdrouter[16], neighbor[16];
  60. char *p;
  61. ospf6_header_print (oh);
  62. assert (oh->type == OSPF6_MESSAGE_TYPE_HELLO);
  63. hello = (struct ospf6_hello *)
  64. ((caddr_t) oh + sizeof (struct ospf6_header));
  65. inet_ntop (AF_INET, &hello->drouter, drouter, sizeof (drouter));
  66. inet_ntop (AF_INET, &hello->bdrouter, bdrouter, sizeof (bdrouter));
  67. ospf6_options_printbuf (hello->options, options, sizeof (options));
  68. zlog_debug (" I/F-Id:%ld Priority:%d Option:%s",
  69. (u_long) ntohl (hello->interface_id), hello->priority, options);
  70. zlog_debug (" HelloInterval:%hu DeadInterval:%hu",
  71. ntohs (hello->hello_interval), ntohs (hello->dead_interval));
  72. zlog_debug (" DR:%s BDR:%s", drouter, bdrouter);
  73. for (p = (char *) ((caddr_t) hello + sizeof (struct ospf6_hello));
  74. p + sizeof (u_int32_t) <= OSPF6_MESSAGE_END (oh);
  75. p += sizeof (u_int32_t))
  76. {
  77. inet_ntop (AF_INET, (void *) p, neighbor, sizeof (neighbor));
  78. zlog_debug (" Neighbor: %s", neighbor);
  79. }
  80. if (p != OSPF6_MESSAGE_END (oh))
  81. zlog_debug ("Trailing garbage exists");
  82. }
  83. void
  84. ospf6_dbdesc_print (struct ospf6_header *oh)
  85. {
  86. struct ospf6_dbdesc *dbdesc;
  87. char options[16];
  88. char *p;
  89. ospf6_header_print (oh);
  90. assert (oh->type == OSPF6_MESSAGE_TYPE_DBDESC);
  91. dbdesc = (struct ospf6_dbdesc *)
  92. ((caddr_t) oh + sizeof (struct ospf6_header));
  93. ospf6_options_printbuf (dbdesc->options, options, sizeof (options));
  94. zlog_debug (" MBZ: %#x Option: %s IfMTU: %hu",
  95. dbdesc->reserved1, options, ntohs (dbdesc->ifmtu));
  96. zlog_debug (" MBZ: %#x Bits: %s%s%s SeqNum: %#lx",
  97. dbdesc->reserved2,
  98. (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT) ? "I" : "-"),
  99. (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MBIT) ? "M" : "-"),
  100. (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT) ? "m" : "s"),
  101. (u_long) ntohl (dbdesc->seqnum));
  102. for (p = (char *) ((caddr_t) dbdesc + sizeof (struct ospf6_dbdesc));
  103. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  104. p += sizeof (struct ospf6_lsa_header))
  105. ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
  106. if (p != OSPF6_MESSAGE_END (oh))
  107. zlog_debug ("Trailing garbage exists");
  108. }
  109. void
  110. ospf6_lsreq_print (struct ospf6_header *oh)
  111. {
  112. char id[16], adv_router[16];
  113. char *p;
  114. ospf6_header_print (oh);
  115. assert (oh->type == OSPF6_MESSAGE_TYPE_LSREQ);
  116. for (p = (char *) ((caddr_t) oh + sizeof (struct ospf6_header));
  117. p + sizeof (struct ospf6_lsreq_entry) <= OSPF6_MESSAGE_END (oh);
  118. p += sizeof (struct ospf6_lsreq_entry))
  119. {
  120. struct ospf6_lsreq_entry *e = (struct ospf6_lsreq_entry *) p;
  121. inet_ntop (AF_INET, &e->adv_router, adv_router, sizeof (adv_router));
  122. inet_ntop (AF_INET, &e->id, id, sizeof (id));
  123. zlog_debug (" [%s Id:%s Adv:%s]",
  124. ospf6_lstype_name (e->type), id, adv_router);
  125. }
  126. if (p != OSPF6_MESSAGE_END (oh))
  127. zlog_debug ("Trailing garbage exists");
  128. }
  129. void
  130. ospf6_lsupdate_print (struct ospf6_header *oh)
  131. {
  132. struct ospf6_lsupdate *lsupdate;
  133. u_long num;
  134. char *p;
  135. ospf6_header_print (oh);
  136. assert (oh->type == OSPF6_MESSAGE_TYPE_LSUPDATE);
  137. lsupdate = (struct ospf6_lsupdate *)
  138. ((caddr_t) oh + sizeof (struct ospf6_header));
  139. num = ntohl (lsupdate->lsa_number);
  140. zlog_debug (" Number of LSA: %ld", num);
  141. for (p = (char *) ((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  142. p < OSPF6_MESSAGE_END (oh) &&
  143. p + OSPF6_LSA_SIZE (p) <= OSPF6_MESSAGE_END (oh);
  144. p += OSPF6_LSA_SIZE (p))
  145. {
  146. ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
  147. if (OSPF6_LSA_SIZE (p) < sizeof (struct ospf6_lsa_header))
  148. {
  149. zlog_debug (" Malformed LSA length, quit printing");
  150. break;
  151. }
  152. }
  153. if (p != OSPF6_MESSAGE_END (oh))
  154. {
  155. char buf[32];
  156. int num = 0;
  157. memset (buf, 0, sizeof (buf));
  158. zlog_debug (" Trailing garbage exists");
  159. while (p < OSPF6_MESSAGE_END (oh))
  160. {
  161. snprintf (buf, sizeof (buf), "%s %2x", buf, *p++);
  162. num++;
  163. if (num == 8)
  164. {
  165. zlog_debug (" %s", buf);
  166. memset (buf, 0, sizeof (buf));
  167. num = 0;
  168. }
  169. }
  170. if (num)
  171. zlog_debug (" %s", buf);
  172. }
  173. }
  174. void
  175. ospf6_lsack_print (struct ospf6_header *oh)
  176. {
  177. char *p;
  178. ospf6_header_print (oh);
  179. assert (oh->type == OSPF6_MESSAGE_TYPE_LSACK);
  180. for (p = (char *) ((caddr_t) oh + sizeof (struct ospf6_header));
  181. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  182. p += sizeof (struct ospf6_lsa_header))
  183. ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
  184. if (p != OSPF6_MESSAGE_END (oh))
  185. zlog_debug ("Trailing garbage exists");
  186. }
  187. /* Receive function */
  188. #define MSG_OK 0
  189. #define MSG_NG 1
  190. static int
  191. ospf6_header_examin (struct in6_addr *src, struct in6_addr *dst,
  192. struct ospf6_interface *oi, struct ospf6_header *oh)
  193. {
  194. u_char type;
  195. type = OSPF6_MESSAGE_TYPE_CANONICAL (oh->type);
  196. /* version check */
  197. if (oh->version != OSPFV3_VERSION)
  198. {
  199. if (IS_OSPF6_DEBUG_MESSAGE (type, RECV))
  200. zlog_debug ("Message with unknown version");
  201. return MSG_NG;
  202. }
  203. /* Area-ID check */
  204. if (oh->area_id != oi->area->area_id)
  205. {
  206. if (oh->area_id == BACKBONE_AREA_ID)
  207. {
  208. if (IS_OSPF6_DEBUG_MESSAGE (type, RECV))
  209. zlog_debug ("Message may be via Virtual Link: not supported");
  210. return MSG_NG;
  211. }
  212. if (IS_OSPF6_DEBUG_MESSAGE (type, RECV))
  213. zlog_debug ("Area-ID mismatch");
  214. return MSG_NG;
  215. }
  216. /* Instance-ID check */
  217. if (oh->instance_id != oi->instance_id)
  218. {
  219. if (IS_OSPF6_DEBUG_MESSAGE (type, RECV))
  220. zlog_debug ("Instance-ID mismatch");
  221. return MSG_NG;
  222. }
  223. /* Router-ID check */
  224. if (oh->router_id == oi->area->ospf6->router_id)
  225. zlog_warn ("Detect duplicate Router-ID");
  226. return MSG_OK;
  227. }
  228. static void
  229. ospf6_hello_recv (struct in6_addr *src, struct in6_addr *dst,
  230. struct ospf6_interface *oi, struct ospf6_header *oh)
  231. {
  232. struct ospf6_hello *hello;
  233. struct ospf6_neighbor *on;
  234. char *p;
  235. int twoway = 0;
  236. int neighborchange = 0;
  237. int backupseen = 0;
  238. if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
  239. return;
  240. hello = (struct ospf6_hello *)
  241. ((caddr_t) oh + sizeof (struct ospf6_header));
  242. /* HelloInterval check */
  243. if (ntohs (hello->hello_interval) != oi->hello_interval)
  244. {
  245. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  246. zlog_debug ("HelloInterval mismatch");
  247. return;
  248. }
  249. /* RouterDeadInterval check */
  250. if (ntohs (hello->dead_interval) != oi->dead_interval)
  251. {
  252. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  253. zlog_debug ("RouterDeadInterval mismatch");
  254. return;
  255. }
  256. /* E-bit check */
  257. if (OSPF6_OPT_ISSET (hello->options, OSPF6_OPT_E) !=
  258. OSPF6_OPT_ISSET (oi->area->options, OSPF6_OPT_E))
  259. {
  260. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  261. zlog_debug ("E-bit mismatch");
  262. return;
  263. }
  264. /* Find neighbor, create if not exist */
  265. on = ospf6_neighbor_lookup (oh->router_id, oi);
  266. if (on == NULL)
  267. {
  268. on = ospf6_neighbor_create (oh->router_id, oi);
  269. on->prev_drouter = on->drouter = hello->drouter;
  270. on->prev_bdrouter = on->bdrouter = hello->bdrouter;
  271. on->priority = hello->priority;
  272. }
  273. /* always override neighbor's source address and ifindex */
  274. on->ifindex = ntohl (hello->interface_id);
  275. memcpy (&on->linklocal_addr, src, sizeof (struct in6_addr));
  276. /* TwoWay check */
  277. for (p = (char *) ((caddr_t) hello + sizeof (struct ospf6_hello));
  278. p + sizeof (u_int32_t) <= OSPF6_MESSAGE_END (oh);
  279. p += sizeof (u_int32_t))
  280. {
  281. u_int32_t *router_id = (u_int32_t *) p;
  282. if (*router_id == oi->area->ospf6->router_id)
  283. twoway++;
  284. }
  285. if (p != OSPF6_MESSAGE_END (oh))
  286. {
  287. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  288. zlog_debug ("Trailing garbage ignored");
  289. }
  290. /* RouterPriority check */
  291. if (on->priority != hello->priority)
  292. {
  293. on->priority = hello->priority;
  294. neighborchange++;
  295. }
  296. /* DR check */
  297. if (on->drouter != hello->drouter)
  298. {
  299. on->prev_drouter = on->drouter;
  300. on->drouter = hello->drouter;
  301. if (on->prev_drouter == on->router_id || on->drouter == on->router_id)
  302. neighborchange++;
  303. }
  304. /* BDR check */
  305. if (on->bdrouter != hello->bdrouter)
  306. {
  307. on->prev_bdrouter = on->bdrouter;
  308. on->bdrouter = hello->bdrouter;
  309. if (on->prev_bdrouter == on->router_id || on->bdrouter == on->router_id)
  310. neighborchange++;
  311. }
  312. /* BackupSeen check */
  313. if (oi->state == OSPF6_INTERFACE_WAITING)
  314. {
  315. if (hello->bdrouter == on->router_id)
  316. backupseen++;
  317. else if (hello->drouter == on->router_id && hello->bdrouter == htonl (0))
  318. backupseen++;
  319. }
  320. /* Execute neighbor events */
  321. thread_execute (master, hello_received, on, 0);
  322. if (twoway)
  323. thread_execute (master, twoway_received, on, 0);
  324. else
  325. thread_execute (master, oneway_received, on, 0);
  326. /* Schedule interface events */
  327. if (backupseen)
  328. thread_add_event (master, backup_seen, oi, 0);
  329. if (neighborchange)
  330. thread_add_event (master, neighbor_change, oi, 0);
  331. }
  332. static void
  333. ospf6_dbdesc_recv_master (struct ospf6_header *oh,
  334. struct ospf6_neighbor *on)
  335. {
  336. struct ospf6_dbdesc *dbdesc;
  337. char *p;
  338. dbdesc = (struct ospf6_dbdesc *)
  339. ((caddr_t) oh + sizeof (struct ospf6_header));
  340. if (on->state < OSPF6_NEIGHBOR_INIT)
  341. {
  342. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  343. zlog_debug ("Neighbor state less than Init, ignore");
  344. return;
  345. }
  346. switch (on->state)
  347. {
  348. case OSPF6_NEIGHBOR_TWOWAY:
  349. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  350. zlog_debug ("Neighbor state is 2-Way, ignore");
  351. return;
  352. case OSPF6_NEIGHBOR_INIT:
  353. thread_execute (master, twoway_received, on, 0);
  354. if (on->state != OSPF6_NEIGHBOR_EXSTART)
  355. {
  356. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  357. zlog_debug ("Neighbor state is not ExStart, ignore");
  358. return;
  359. }
  360. /* else fall through to ExStart */
  361. case OSPF6_NEIGHBOR_EXSTART:
  362. /* if neighbor obeys us as our slave, schedule negotiation_done
  363. and process LSA Headers. Otherwise, ignore this message */
  364. if (! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT) &&
  365. ! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT) &&
  366. ntohl (dbdesc->seqnum) == on->dbdesc_seqnum)
  367. {
  368. /* execute NegotiationDone */
  369. thread_execute (master, negotiation_done, on, 0);
  370. /* Record neighbor options */
  371. memcpy (on->options, dbdesc->options, sizeof (on->options));
  372. }
  373. else
  374. {
  375. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  376. zlog_debug ("Negotiation failed");
  377. return;
  378. }
  379. /* fall through to exchange */
  380. case OSPF6_NEIGHBOR_EXCHANGE:
  381. if (! memcmp (dbdesc, &on->dbdesc_last, sizeof (struct ospf6_dbdesc)))
  382. {
  383. /* Duplicated DatabaseDescription is dropped by master */
  384. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  385. zlog_debug ("Duplicated dbdesc discarded by Master, ignore");
  386. return;
  387. }
  388. if (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT))
  389. {
  390. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  391. zlog_debug ("Master/Slave bit mismatch");
  392. thread_add_event (master, seqnumber_mismatch, on, 0);
  393. return;
  394. }
  395. if (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT))
  396. {
  397. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  398. zlog_debug ("Initialize bit mismatch");
  399. thread_add_event (master, seqnumber_mismatch, on, 0);
  400. return;
  401. }
  402. if (memcmp (on->options, dbdesc->options, sizeof (on->options)))
  403. {
  404. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  405. zlog_debug ("Option field mismatch");
  406. thread_add_event (master, seqnumber_mismatch, on, 0);
  407. return;
  408. }
  409. if (ntohl (dbdesc->seqnum) != on->dbdesc_seqnum)
  410. {
  411. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  412. zlog_debug ("Sequence number mismatch (%#lx expected)",
  413. (u_long) on->dbdesc_seqnum);
  414. thread_add_event (master, seqnumber_mismatch, on, 0);
  415. return;
  416. }
  417. break;
  418. case OSPF6_NEIGHBOR_LOADING:
  419. case OSPF6_NEIGHBOR_FULL:
  420. if (! memcmp (dbdesc, &on->dbdesc_last, sizeof (struct ospf6_dbdesc)))
  421. {
  422. /* Duplicated DatabaseDescription is dropped by master */
  423. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  424. zlog_debug ("Duplicated dbdesc discarded by Master, ignore");
  425. return;
  426. }
  427. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  428. zlog_debug ("Not duplicate dbdesc in state %s",
  429. ospf6_neighbor_state_str[on->state]);
  430. thread_add_event (master, seqnumber_mismatch, on, 0);
  431. return;
  432. default:
  433. assert (0);
  434. break;
  435. }
  436. /* Process LSA headers */
  437. for (p = (char *) ((caddr_t) dbdesc + sizeof (struct ospf6_dbdesc));
  438. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  439. p += sizeof (struct ospf6_lsa_header))
  440. {
  441. struct ospf6_lsa *his, *mine;
  442. struct ospf6_lsdb *lsdb = NULL;
  443. his = ospf6_lsa_create_headeronly ((struct ospf6_lsa_header *) p);
  444. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  445. zlog_debug ("%s", his->name);
  446. switch (OSPF6_LSA_SCOPE (his->header->type))
  447. {
  448. case OSPF6_SCOPE_LINKLOCAL:
  449. lsdb = on->ospf6_if->lsdb;
  450. break;
  451. case OSPF6_SCOPE_AREA:
  452. lsdb = on->ospf6_if->area->lsdb;
  453. break;
  454. case OSPF6_SCOPE_AS:
  455. lsdb = on->ospf6_if->area->ospf6->lsdb;
  456. break;
  457. case OSPF6_SCOPE_RESERVED:
  458. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  459. zlog_debug ("Ignoring LSA of reserved scope");
  460. ospf6_lsa_delete (his);
  461. continue;
  462. break;
  463. }
  464. if (ntohs (his->header->type) == OSPF6_LSTYPE_AS_EXTERNAL &&
  465. IS_AREA_STUB (on->ospf6_if->area))
  466. {
  467. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  468. zlog_debug ("SeqNumMismatch (E-bit mismatch), discard");
  469. ospf6_lsa_delete (his);
  470. thread_add_event (master, seqnumber_mismatch, on, 0);
  471. return;
  472. }
  473. mine = ospf6_lsdb_lookup (his->header->type, his->header->id,
  474. his->header->adv_router, lsdb);
  475. if (mine == NULL)
  476. {
  477. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  478. zlog_debug ("Add request (No database copy)");
  479. ospf6_lsdb_add (his, on->request_list);
  480. }
  481. else if (ospf6_lsa_compare (his, mine) < 0)
  482. {
  483. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  484. zlog_debug ("Add request (Received MoreRecent)");
  485. ospf6_lsdb_add (his, on->request_list);
  486. }
  487. else
  488. {
  489. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  490. zlog_debug ("Discard (Existing MoreRecent)");
  491. ospf6_lsa_delete (his);
  492. }
  493. }
  494. if (p != OSPF6_MESSAGE_END (oh))
  495. {
  496. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  497. zlog_debug ("Trailing garbage ignored");
  498. }
  499. /* Increment sequence number */
  500. on->dbdesc_seqnum ++;
  501. /* schedule send lsreq */
  502. if (on->thread_send_lsreq == NULL)
  503. on->thread_send_lsreq =
  504. thread_add_event (master, ospf6_lsreq_send, on, 0);
  505. THREAD_OFF (on->thread_send_dbdesc);
  506. /* More bit check */
  507. if (! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MBIT) &&
  508. ! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT))
  509. thread_add_event (master, exchange_done, on, 0);
  510. else
  511. on->thread_send_dbdesc =
  512. thread_add_event (master, ospf6_dbdesc_send_newone, on, 0);
  513. /* save last received dbdesc */
  514. memcpy (&on->dbdesc_last, dbdesc, sizeof (struct ospf6_dbdesc));
  515. }
  516. static void
  517. ospf6_dbdesc_recv_slave (struct ospf6_header *oh,
  518. struct ospf6_neighbor *on)
  519. {
  520. struct ospf6_dbdesc *dbdesc;
  521. char *p;
  522. dbdesc = (struct ospf6_dbdesc *)
  523. ((caddr_t) oh + sizeof (struct ospf6_header));
  524. if (on->state < OSPF6_NEIGHBOR_INIT)
  525. {
  526. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  527. zlog_debug ("Neighbor state less than Init, ignore");
  528. return;
  529. }
  530. switch (on->state)
  531. {
  532. case OSPF6_NEIGHBOR_TWOWAY:
  533. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  534. zlog_debug ("Neighbor state is 2-Way, ignore");
  535. return;
  536. case OSPF6_NEIGHBOR_INIT:
  537. thread_execute (master, twoway_received, on, 0);
  538. if (on->state != OSPF6_NEIGHBOR_EXSTART)
  539. {
  540. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  541. zlog_debug ("Neighbor state is not ExStart, ignore");
  542. return;
  543. }
  544. /* else fall through to ExStart */
  545. case OSPF6_NEIGHBOR_EXSTART:
  546. /* If the neighbor is Master, act as Slave. Schedule negotiation_done
  547. and process LSA Headers. Otherwise, ignore this message */
  548. if (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT) &&
  549. CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MBIT) &&
  550. CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT) &&
  551. ntohs (oh->length) == sizeof (struct ospf6_header) +
  552. sizeof (struct ospf6_dbdesc))
  553. {
  554. /* set the master/slave bit to slave */
  555. UNSET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT);
  556. /* set the DD sequence number to one specified by master */
  557. on->dbdesc_seqnum = ntohl (dbdesc->seqnum);
  558. /* schedule NegotiationDone */
  559. thread_execute (master, negotiation_done, on, 0);
  560. /* Record neighbor options */
  561. memcpy (on->options, dbdesc->options, sizeof (on->options));
  562. }
  563. else
  564. {
  565. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  566. zlog_debug ("Negotiation failed");
  567. return;
  568. }
  569. break;
  570. case OSPF6_NEIGHBOR_EXCHANGE:
  571. if (! memcmp (dbdesc, &on->dbdesc_last, sizeof (struct ospf6_dbdesc)))
  572. {
  573. /* Duplicated DatabaseDescription causes slave to retransmit */
  574. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  575. zlog_debug ("Duplicated dbdesc causes retransmit");
  576. THREAD_OFF (on->thread_send_dbdesc);
  577. on->thread_send_dbdesc =
  578. thread_add_event (master, ospf6_dbdesc_send, on, 0);
  579. return;
  580. }
  581. if (! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT))
  582. {
  583. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  584. zlog_debug ("Master/Slave bit mismatch");
  585. thread_add_event (master, seqnumber_mismatch, on, 0);
  586. return;
  587. }
  588. if (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT))
  589. {
  590. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  591. zlog_debug ("Initialize bit mismatch");
  592. thread_add_event (master, seqnumber_mismatch, on, 0);
  593. return;
  594. }
  595. if (memcmp (on->options, dbdesc->options, sizeof (on->options)))
  596. {
  597. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  598. zlog_debug ("Option field mismatch");
  599. thread_add_event (master, seqnumber_mismatch, on, 0);
  600. return;
  601. }
  602. if (ntohl (dbdesc->seqnum) != on->dbdesc_seqnum + 1)
  603. {
  604. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  605. zlog_debug ("Sequence number mismatch (%#lx expected)",
  606. (u_long) on->dbdesc_seqnum + 1);
  607. thread_add_event (master, seqnumber_mismatch, on, 0);
  608. return;
  609. }
  610. break;
  611. case OSPF6_NEIGHBOR_LOADING:
  612. case OSPF6_NEIGHBOR_FULL:
  613. if (! memcmp (dbdesc, &on->dbdesc_last, sizeof (struct ospf6_dbdesc)))
  614. {
  615. /* Duplicated DatabaseDescription causes slave to retransmit */
  616. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  617. zlog_debug ("Duplicated dbdesc causes retransmit");
  618. THREAD_OFF (on->thread_send_dbdesc);
  619. on->thread_send_dbdesc =
  620. thread_add_event (master, ospf6_dbdesc_send, on, 0);
  621. return;
  622. }
  623. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  624. zlog_debug ("Not duplicate dbdesc in state %s",
  625. ospf6_neighbor_state_str[on->state]);
  626. thread_add_event (master, seqnumber_mismatch, on, 0);
  627. return;
  628. default:
  629. assert (0);
  630. break;
  631. }
  632. /* Process LSA headers */
  633. for (p = (char *) ((caddr_t) dbdesc + sizeof (struct ospf6_dbdesc));
  634. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  635. p += sizeof (struct ospf6_lsa_header))
  636. {
  637. struct ospf6_lsa *his, *mine;
  638. struct ospf6_lsdb *lsdb = NULL;
  639. his = ospf6_lsa_create_headeronly ((struct ospf6_lsa_header *) p);
  640. switch (OSPF6_LSA_SCOPE (his->header->type))
  641. {
  642. case OSPF6_SCOPE_LINKLOCAL:
  643. lsdb = on->ospf6_if->lsdb;
  644. break;
  645. case OSPF6_SCOPE_AREA:
  646. lsdb = on->ospf6_if->area->lsdb;
  647. break;
  648. case OSPF6_SCOPE_AS:
  649. lsdb = on->ospf6_if->area->ospf6->lsdb;
  650. break;
  651. case OSPF6_SCOPE_RESERVED:
  652. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  653. zlog_debug ("Ignoring LSA of reserved scope");
  654. ospf6_lsa_delete (his);
  655. continue;
  656. break;
  657. }
  658. if (OSPF6_LSA_SCOPE (his->header->type) == OSPF6_SCOPE_AS &&
  659. IS_AREA_STUB (on->ospf6_if->area))
  660. {
  661. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  662. zlog_debug ("E-bit mismatch with LSA Headers");
  663. ospf6_lsa_delete (his);
  664. thread_add_event (master, seqnumber_mismatch, on, 0);
  665. return;
  666. }
  667. mine = ospf6_lsdb_lookup (his->header->type, his->header->id,
  668. his->header->adv_router, lsdb);
  669. if (mine == NULL || ospf6_lsa_compare (his, mine) < 0)
  670. {
  671. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  672. zlog_debug ("Add request-list: %s", his->name);
  673. ospf6_lsdb_add (his, on->request_list);
  674. }
  675. else
  676. ospf6_lsa_delete (his);
  677. }
  678. if (p != OSPF6_MESSAGE_END (oh))
  679. {
  680. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  681. zlog_debug ("Trailing garbage ignored");
  682. }
  683. /* Set sequence number to Master's */
  684. on->dbdesc_seqnum = ntohl (dbdesc->seqnum);
  685. /* schedule send lsreq */
  686. if (on->thread_send_lsreq == NULL)
  687. on->thread_send_lsreq =
  688. thread_add_event (master, ospf6_lsreq_send, on, 0);
  689. THREAD_OFF (on->thread_send_dbdesc);
  690. on->thread_send_dbdesc =
  691. thread_add_event (master, ospf6_dbdesc_send_newone, on, 0);
  692. /* save last received dbdesc */
  693. memcpy (&on->dbdesc_last, dbdesc, sizeof (struct ospf6_dbdesc));
  694. }
  695. static void
  696. ospf6_dbdesc_recv (struct in6_addr *src, struct in6_addr *dst,
  697. struct ospf6_interface *oi, struct ospf6_header *oh)
  698. {
  699. struct ospf6_neighbor *on;
  700. struct ospf6_dbdesc *dbdesc;
  701. if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
  702. return;
  703. on = ospf6_neighbor_lookup (oh->router_id, oi);
  704. if (on == NULL)
  705. {
  706. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  707. zlog_debug ("Neighbor not found, ignore");
  708. return;
  709. }
  710. dbdesc = (struct ospf6_dbdesc *)
  711. ((caddr_t) oh + sizeof (struct ospf6_header));
  712. /* Interface MTU check */
  713. if (ntohs (dbdesc->ifmtu) != oi->ifmtu)
  714. {
  715. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  716. zlog_debug ("I/F MTU mismatch");
  717. return;
  718. }
  719. if (dbdesc->reserved1 || dbdesc->reserved2)
  720. {
  721. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  722. zlog_debug ("Non-0 reserved field in %s's DbDesc, correct",
  723. on->name);
  724. dbdesc->reserved1 = 0;
  725. dbdesc->reserved2 = 0;
  726. }
  727. if (ntohl (oh->router_id) < ntohl (ospf6->router_id))
  728. ospf6_dbdesc_recv_master (oh, on);
  729. else if (ntohl (ospf6->router_id) < ntohl (oh->router_id))
  730. ospf6_dbdesc_recv_slave (oh, on);
  731. else
  732. {
  733. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  734. zlog_debug ("Can't decide which is master, ignore");
  735. }
  736. }
  737. static void
  738. ospf6_lsreq_recv (struct in6_addr *src, struct in6_addr *dst,
  739. struct ospf6_interface *oi, struct ospf6_header *oh)
  740. {
  741. struct ospf6_neighbor *on;
  742. char *p;
  743. struct ospf6_lsreq_entry *e;
  744. struct ospf6_lsdb *lsdb = NULL;
  745. struct ospf6_lsa *lsa;
  746. if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
  747. return;
  748. on = ospf6_neighbor_lookup (oh->router_id, oi);
  749. if (on == NULL)
  750. {
  751. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  752. zlog_debug ("Neighbor not found, ignore");
  753. return;
  754. }
  755. if (on->state != OSPF6_NEIGHBOR_EXCHANGE &&
  756. on->state != OSPF6_NEIGHBOR_LOADING &&
  757. on->state != OSPF6_NEIGHBOR_FULL)
  758. {
  759. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  760. zlog_debug ("Neighbor state less than Exchange, ignore");
  761. return;
  762. }
  763. /* Process each request */
  764. for (p = (char *) ((caddr_t) oh + sizeof (struct ospf6_header));
  765. p + sizeof (struct ospf6_lsreq_entry) <= OSPF6_MESSAGE_END (oh);
  766. p += sizeof (struct ospf6_lsreq_entry))
  767. {
  768. e = (struct ospf6_lsreq_entry *) p;
  769. switch (OSPF6_LSA_SCOPE (e->type))
  770. {
  771. case OSPF6_SCOPE_LINKLOCAL:
  772. lsdb = on->ospf6_if->lsdb;
  773. break;
  774. case OSPF6_SCOPE_AREA:
  775. lsdb = on->ospf6_if->area->lsdb;
  776. break;
  777. case OSPF6_SCOPE_AS:
  778. lsdb = on->ospf6_if->area->ospf6->lsdb;
  779. break;
  780. default:
  781. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  782. zlog_debug ("Ignoring LSA of reserved scope");
  783. continue;
  784. break;
  785. }
  786. /* Find database copy */
  787. lsa = ospf6_lsdb_lookup (e->type, e->id, e->adv_router, lsdb);
  788. if (lsa == NULL)
  789. {
  790. char id[16], adv_router[16];
  791. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  792. {
  793. inet_ntop (AF_INET, &e->id, id, sizeof (id));
  794. inet_ntop (AF_INET, &e->adv_router, adv_router,
  795. sizeof (adv_router));
  796. zlog_debug ("Can't find requested [%s Id:%s Adv:%s]",
  797. ospf6_lstype_name (e->type), id, adv_router);
  798. }
  799. thread_add_event (master, bad_lsreq, on, 0);
  800. return;
  801. }
  802. ospf6_lsdb_add (ospf6_lsa_copy (lsa), on->lsupdate_list);
  803. }
  804. if (p != OSPF6_MESSAGE_END (oh))
  805. {
  806. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  807. zlog_debug ("Trailing garbage ignored");
  808. }
  809. /* schedule send lsupdate */
  810. THREAD_OFF (on->thread_send_lsupdate);
  811. on->thread_send_lsupdate =
  812. thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0);
  813. }
  814. static void
  815. ospf6_lsupdate_recv (struct in6_addr *src, struct in6_addr *dst,
  816. struct ospf6_interface *oi, struct ospf6_header *oh)
  817. {
  818. struct ospf6_neighbor *on;
  819. struct ospf6_lsupdate *lsupdate;
  820. unsigned long num;
  821. char *p;
  822. if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
  823. return;
  824. on = ospf6_neighbor_lookup (oh->router_id, oi);
  825. if (on == NULL)
  826. {
  827. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  828. zlog_debug ("Neighbor not found, ignore");
  829. return;
  830. }
  831. if (on->state != OSPF6_NEIGHBOR_EXCHANGE &&
  832. on->state != OSPF6_NEIGHBOR_LOADING &&
  833. on->state != OSPF6_NEIGHBOR_FULL)
  834. {
  835. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  836. zlog_debug ("Neighbor state less than Exchange, ignore");
  837. return;
  838. }
  839. lsupdate = (struct ospf6_lsupdate *)
  840. ((caddr_t) oh + sizeof (struct ospf6_header));
  841. num = ntohl (lsupdate->lsa_number);
  842. /* Process LSAs */
  843. for (p = (char *) ((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  844. p < OSPF6_MESSAGE_END (oh) &&
  845. p + OSPF6_LSA_SIZE (p) <= OSPF6_MESSAGE_END (oh);
  846. p += OSPF6_LSA_SIZE (p))
  847. {
  848. if (num == 0)
  849. break;
  850. if (OSPF6_LSA_SIZE (p) < sizeof (struct ospf6_lsa_header))
  851. {
  852. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  853. zlog_debug ("Malformed LSA length, quit processing");
  854. break;
  855. }
  856. ospf6_receive_lsa (on, (struct ospf6_lsa_header *) p);
  857. num--;
  858. }
  859. if (num != 0)
  860. {
  861. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  862. zlog_debug ("Malformed LSA number or LSA length");
  863. }
  864. if (p != OSPF6_MESSAGE_END (oh))
  865. {
  866. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  867. zlog_debug ("Trailing garbage ignored");
  868. }
  869. /* RFC2328 Section 10.9: When the neighbor responds to these requests
  870. with the proper Link State Update packet(s), the Link state request
  871. list is truncated and a new Link State Request packet is sent. */
  872. /* send new Link State Request packet if this LS Update packet
  873. can be recognized as a response to our previous LS Request */
  874. if (! IN6_IS_ADDR_MULTICAST (dst) &&
  875. (on->state == OSPF6_NEIGHBOR_EXCHANGE ||
  876. on->state == OSPF6_NEIGHBOR_LOADING))
  877. {
  878. THREAD_OFF (on->thread_send_lsreq);
  879. on->thread_send_lsreq =
  880. thread_add_event (master, ospf6_lsreq_send, on, 0);
  881. }
  882. }
  883. static void
  884. ospf6_lsack_recv (struct in6_addr *src, struct in6_addr *dst,
  885. struct ospf6_interface *oi, struct ospf6_header *oh)
  886. {
  887. struct ospf6_neighbor *on;
  888. char *p;
  889. struct ospf6_lsa *his, *mine;
  890. struct ospf6_lsdb *lsdb = NULL;
  891. assert (oh->type == OSPF6_MESSAGE_TYPE_LSACK);
  892. if (ospf6_header_examin (src, dst, oi, oh) != MSG_OK)
  893. return;
  894. on = ospf6_neighbor_lookup (oh->router_id, oi);
  895. if (on == NULL)
  896. {
  897. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  898. zlog_debug ("Neighbor not found, ignore");
  899. return;
  900. }
  901. if (on->state != OSPF6_NEIGHBOR_EXCHANGE &&
  902. on->state != OSPF6_NEIGHBOR_LOADING &&
  903. on->state != OSPF6_NEIGHBOR_FULL)
  904. {
  905. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  906. zlog_debug ("Neighbor state less than Exchange, ignore");
  907. return;
  908. }
  909. for (p = (char *) ((caddr_t) oh + sizeof (struct ospf6_header));
  910. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  911. p += sizeof (struct ospf6_lsa_header))
  912. {
  913. his = ospf6_lsa_create_headeronly ((struct ospf6_lsa_header *) p);
  914. switch (OSPF6_LSA_SCOPE (his->header->type))
  915. {
  916. case OSPF6_SCOPE_LINKLOCAL:
  917. lsdb = on->ospf6_if->lsdb;
  918. break;
  919. case OSPF6_SCOPE_AREA:
  920. lsdb = on->ospf6_if->area->lsdb;
  921. break;
  922. case OSPF6_SCOPE_AS:
  923. lsdb = on->ospf6_if->area->ospf6->lsdb;
  924. break;
  925. case OSPF6_SCOPE_RESERVED:
  926. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  927. zlog_debug ("Ignoring LSA of reserved scope");
  928. ospf6_lsa_delete (his);
  929. continue;
  930. break;
  931. }
  932. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  933. zlog_debug ("%s acknowledged by %s", his->name, on->name);
  934. /* Find database copy */
  935. mine = ospf6_lsdb_lookup (his->header->type, his->header->id,
  936. his->header->adv_router, lsdb);
  937. if (mine == NULL)
  938. {
  939. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  940. zlog_debug ("No database copy");
  941. ospf6_lsa_delete (his);
  942. continue;
  943. }
  944. /* Check if the LSA is on his retrans-list */
  945. mine = ospf6_lsdb_lookup (his->header->type, his->header->id,
  946. his->header->adv_router, on->retrans_list);
  947. if (mine == NULL)
  948. {
  949. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  950. zlog_debug ("Not on %s's retrans-list", on->name);
  951. ospf6_lsa_delete (his);
  952. continue;
  953. }
  954. if (ospf6_lsa_compare (his, mine) != 0)
  955. {
  956. /* Log this questionable acknowledgement,
  957. and examine the next one. */
  958. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  959. zlog_debug ("Questionable acknowledgement");
  960. ospf6_lsa_delete (his);
  961. continue;
  962. }
  963. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  964. zlog_debug ("Acknowledged, remove from %s's retrans-list",
  965. on->name);
  966. ospf6_decrement_retrans_count (mine);
  967. if (OSPF6_LSA_IS_MAXAGE (mine))
  968. ospf6_maxage_remove (on->ospf6_if->area->ospf6);
  969. ospf6_lsdb_remove (mine, on->retrans_list);
  970. ospf6_lsa_delete (his);
  971. }
  972. if (p != OSPF6_MESSAGE_END (oh))
  973. {
  974. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  975. zlog_debug ("Trailing garbage ignored");
  976. }
  977. }
  978. static u_char *recvbuf = NULL;
  979. static u_char *sendbuf = NULL;
  980. static unsigned int iobuflen = 0;
  981. int
  982. ospf6_iobuf_size (unsigned int size)
  983. {
  984. u_char *recvnew, *sendnew;
  985. if (size <= iobuflen)
  986. return iobuflen;
  987. recvnew = XMALLOC (MTYPE_OSPF6_MESSAGE, size);
  988. sendnew = XMALLOC (MTYPE_OSPF6_MESSAGE, size);
  989. if (recvnew == NULL || sendnew == NULL)
  990. {
  991. if (recvnew)
  992. XFREE (MTYPE_OSPF6_MESSAGE, recvnew);
  993. if (sendnew)
  994. XFREE (MTYPE_OSPF6_MESSAGE, sendnew);
  995. zlog_debug ("Could not allocate I/O buffer of size %d.", size);
  996. return iobuflen;
  997. }
  998. if (recvbuf)
  999. XFREE (MTYPE_OSPF6_MESSAGE, recvbuf);
  1000. if (sendbuf)
  1001. XFREE (MTYPE_OSPF6_MESSAGE, sendbuf);
  1002. recvbuf = recvnew;
  1003. sendbuf = sendnew;
  1004. iobuflen = size;
  1005. return iobuflen;
  1006. }
  1007. int
  1008. ospf6_receive (struct thread *thread)
  1009. {
  1010. int sockfd;
  1011. unsigned int len;
  1012. char srcname[64], dstname[64];
  1013. struct in6_addr src, dst;
  1014. unsigned int ifindex;
  1015. struct iovec iovector[2];
  1016. struct ospf6_interface *oi;
  1017. struct ospf6_header *oh;
  1018. /* add next read thread */
  1019. sockfd = THREAD_FD (thread);
  1020. thread_add_read (master, ospf6_receive, NULL, sockfd);
  1021. /* initialize */
  1022. memset (&src, 0, sizeof (src));
  1023. memset (&dst, 0, sizeof (dst));
  1024. ifindex = 0;
  1025. memset (recvbuf, 0, iobuflen);
  1026. iovector[0].iov_base = recvbuf;
  1027. iovector[0].iov_len = iobuflen;
  1028. iovector[1].iov_base = NULL;
  1029. iovector[1].iov_len = 0;
  1030. /* receive message */
  1031. len = ospf6_recvmsg (&src, &dst, &ifindex, iovector);
  1032. if (len > iobuflen)
  1033. {
  1034. zlog_err ("Excess message read");
  1035. return 0;
  1036. }
  1037. else if (len < sizeof (struct ospf6_header))
  1038. {
  1039. zlog_err ("Deficient message read");
  1040. return 0;
  1041. }
  1042. oi = ospf6_interface_lookup_by_ifindex (ifindex);
  1043. if (oi == NULL || oi->area == NULL)
  1044. {
  1045. zlog_debug ("Message received on disabled interface");
  1046. return 0;
  1047. }
  1048. oh = (struct ospf6_header *) recvbuf;
  1049. /* Log */
  1050. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1051. {
  1052. inet_ntop (AF_INET6, &src, srcname, sizeof (srcname));
  1053. inet_ntop (AF_INET6, &dst, dstname, sizeof (dstname));
  1054. zlog_debug ("%s received on %s",
  1055. OSPF6_MESSAGE_TYPE_NAME (oh->type), oi->interface->name);
  1056. zlog_debug (" src: %s", srcname);
  1057. zlog_debug (" dst: %s", dstname);
  1058. if (len != ntohs (oh->length))
  1059. zlog_debug ("Message length does not match actually received: %d", len);
  1060. switch (oh->type)
  1061. {
  1062. case OSPF6_MESSAGE_TYPE_HELLO:
  1063. ospf6_hello_print (oh);
  1064. break;
  1065. case OSPF6_MESSAGE_TYPE_DBDESC:
  1066. ospf6_dbdesc_print (oh);
  1067. break;
  1068. case OSPF6_MESSAGE_TYPE_LSREQ:
  1069. ospf6_lsreq_print (oh);
  1070. break;
  1071. case OSPF6_MESSAGE_TYPE_LSUPDATE:
  1072. ospf6_lsupdate_print (oh);
  1073. break;
  1074. case OSPF6_MESSAGE_TYPE_LSACK:
  1075. ospf6_lsack_print (oh);
  1076. break;
  1077. default:
  1078. zlog_debug ("Unknown message");
  1079. break;
  1080. }
  1081. }
  1082. if (CHECK_FLAG (oi->flag, OSPF6_INTERFACE_PASSIVE))
  1083. {
  1084. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1085. zlog_debug ("Ignore message on passive interface %s",
  1086. oi->interface->name);
  1087. return 0;
  1088. }
  1089. switch (oh->type)
  1090. {
  1091. case OSPF6_MESSAGE_TYPE_HELLO:
  1092. ospf6_hello_recv (&src, &dst, oi, oh);
  1093. break;
  1094. case OSPF6_MESSAGE_TYPE_DBDESC:
  1095. ospf6_dbdesc_recv (&src, &dst, oi, oh);
  1096. break;
  1097. case OSPF6_MESSAGE_TYPE_LSREQ:
  1098. ospf6_lsreq_recv (&src, &dst, oi, oh);
  1099. break;
  1100. case OSPF6_MESSAGE_TYPE_LSUPDATE:
  1101. ospf6_lsupdate_recv (&src, &dst, oi, oh);
  1102. break;
  1103. case OSPF6_MESSAGE_TYPE_LSACK:
  1104. ospf6_lsack_recv (&src, &dst, oi, oh);
  1105. break;
  1106. default:
  1107. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1108. zlog_debug ("Unknown message");
  1109. break;
  1110. }
  1111. return 0;
  1112. }
  1113. static void
  1114. ospf6_send (struct in6_addr *src, struct in6_addr *dst,
  1115. struct ospf6_interface *oi, struct ospf6_header *oh)
  1116. {
  1117. int len;
  1118. char srcname[64], dstname[64];
  1119. struct iovec iovector[2];
  1120. /* initialize */
  1121. iovector[0].iov_base = (caddr_t) oh;
  1122. iovector[0].iov_len = ntohs (oh->length);
  1123. iovector[1].iov_base = NULL;
  1124. iovector[1].iov_len = 0;
  1125. /* fill OSPF header */
  1126. oh->version = OSPFV3_VERSION;
  1127. /* message type must be set before */
  1128. /* message length must be set before */
  1129. oh->router_id = oi->area->ospf6->router_id;
  1130. oh->area_id = oi->area->area_id;
  1131. /* checksum is calculated by kernel */
  1132. oh->instance_id = oi->instance_id;
  1133. oh->reserved = 0;
  1134. /* Log */
  1135. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, SEND))
  1136. {
  1137. inet_ntop (AF_INET6, dst, dstname, sizeof (dstname));
  1138. if (src)
  1139. inet_ntop (AF_INET6, src, srcname, sizeof (srcname));
  1140. else
  1141. memset (srcname, 0, sizeof (srcname));
  1142. zlog_debug ("%s send on %s",
  1143. OSPF6_MESSAGE_TYPE_NAME (oh->type), oi->interface->name);
  1144. zlog_debug (" src: %s", srcname);
  1145. zlog_debug (" dst: %s", dstname);
  1146. switch (oh->type)
  1147. {
  1148. case OSPF6_MESSAGE_TYPE_HELLO:
  1149. ospf6_hello_print (oh);
  1150. break;
  1151. case OSPF6_MESSAGE_TYPE_DBDESC:
  1152. ospf6_dbdesc_print (oh);
  1153. break;
  1154. case OSPF6_MESSAGE_TYPE_LSREQ:
  1155. ospf6_lsreq_print (oh);
  1156. break;
  1157. case OSPF6_MESSAGE_TYPE_LSUPDATE:
  1158. ospf6_lsupdate_print (oh);
  1159. break;
  1160. case OSPF6_MESSAGE_TYPE_LSACK:
  1161. ospf6_lsack_print (oh);
  1162. break;
  1163. default:
  1164. zlog_debug ("Unknown message");
  1165. assert (0);
  1166. break;
  1167. }
  1168. }
  1169. /* send message */
  1170. len = ospf6_sendmsg (src, dst, &oi->interface->ifindex, iovector);
  1171. if (len != ntohs (oh->length))
  1172. zlog_err ("Could not send entire message");
  1173. }
  1174. int
  1175. ospf6_hello_send (struct thread *thread)
  1176. {
  1177. struct ospf6_interface *oi;
  1178. struct ospf6_header *oh;
  1179. struct ospf6_hello *hello;
  1180. u_char *p;
  1181. struct listnode *node, *nnode;
  1182. struct ospf6_neighbor *on;
  1183. oi = (struct ospf6_interface *) THREAD_ARG (thread);
  1184. oi->thread_send_hello = (struct thread *) NULL;
  1185. if (oi->state <= OSPF6_INTERFACE_DOWN)
  1186. {
  1187. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_HELLO, SEND))
  1188. zlog_debug ("Unable to send Hello on down interface %s",
  1189. oi->interface->name);
  1190. return 0;
  1191. }
  1192. /* set next thread */
  1193. oi->thread_send_hello = thread_add_timer (master, ospf6_hello_send,
  1194. oi, oi->hello_interval);
  1195. memset (sendbuf, 0, iobuflen);
  1196. oh = (struct ospf6_header *) sendbuf;
  1197. hello = (struct ospf6_hello *)((caddr_t) oh + sizeof (struct ospf6_header));
  1198. hello->interface_id = htonl (oi->interface->ifindex);
  1199. hello->priority = oi->priority;
  1200. hello->options[0] = oi->area->options[0];
  1201. hello->options[1] = oi->area->options[1];
  1202. hello->options[2] = oi->area->options[2];
  1203. hello->hello_interval = htons (oi->hello_interval);
  1204. hello->dead_interval = htons (oi->dead_interval);
  1205. hello->drouter = oi->drouter;
  1206. hello->bdrouter = oi->bdrouter;
  1207. p = (u_char *)((caddr_t) hello + sizeof (struct ospf6_hello));
  1208. for (ALL_LIST_ELEMENTS (oi->neighbor_list, node, nnode, on))
  1209. {
  1210. if (on->state < OSPF6_NEIGHBOR_INIT)
  1211. continue;
  1212. if (p - sendbuf + sizeof (u_int32_t) > oi->ifmtu)
  1213. {
  1214. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_HELLO, SEND))
  1215. zlog_debug ("sending Hello message: exceeds I/F MTU");
  1216. break;
  1217. }
  1218. memcpy (p, &on->router_id, sizeof (u_int32_t));
  1219. p += sizeof (u_int32_t);
  1220. }
  1221. oh->type = OSPF6_MESSAGE_TYPE_HELLO;
  1222. oh->length = htons (p - sendbuf);
  1223. ospf6_send (oi->linklocal_addr, &allspfrouters6, oi, oh);
  1224. return 0;
  1225. }
  1226. int
  1227. ospf6_dbdesc_send (struct thread *thread)
  1228. {
  1229. struct ospf6_neighbor *on;
  1230. struct ospf6_header *oh;
  1231. struct ospf6_dbdesc *dbdesc;
  1232. u_char *p;
  1233. struct ospf6_lsa *lsa;
  1234. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1235. on->thread_send_dbdesc = (struct thread *) NULL;
  1236. if (on->state < OSPF6_NEIGHBOR_EXSTART)
  1237. {
  1238. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_DBDESC, SEND))
  1239. zlog_debug ("Quit to send DbDesc to neighbor %s state %s",
  1240. on->name, ospf6_neighbor_state_str[on->state]);
  1241. return 0;
  1242. }
  1243. /* set next thread if master */
  1244. if (CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT))
  1245. on->thread_send_dbdesc =
  1246. thread_add_timer (master, ospf6_dbdesc_send, on,
  1247. on->ospf6_if->rxmt_interval);
  1248. memset (sendbuf, 0, iobuflen);
  1249. oh = (struct ospf6_header *) sendbuf;
  1250. dbdesc = (struct ospf6_dbdesc *)((caddr_t) oh +
  1251. sizeof (struct ospf6_header));
  1252. /* if this is initial one, initialize sequence number for DbDesc */
  1253. if (CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT))
  1254. {
  1255. struct timeval tv;
  1256. if (quagga_gettime (QUAGGA_CLK_MONOTONIC, &tv) < 0)
  1257. tv.tv_sec = 1;
  1258. on->dbdesc_seqnum = tv.tv_sec;
  1259. }
  1260. dbdesc->options[0] = on->ospf6_if->area->options[0];
  1261. dbdesc->options[1] = on->ospf6_if->area->options[1];
  1262. dbdesc->options[2] = on->ospf6_if->area->options[2];
  1263. dbdesc->ifmtu = htons (on->ospf6_if->ifmtu);
  1264. dbdesc->bits = on->dbdesc_bits;
  1265. dbdesc->seqnum = htonl (on->dbdesc_seqnum);
  1266. /* if this is not initial one, set LSA headers in dbdesc */
  1267. p = (u_char *)((caddr_t) dbdesc + sizeof (struct ospf6_dbdesc));
  1268. if (! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT))
  1269. {
  1270. for (lsa = ospf6_lsdb_head (on->dbdesc_list); lsa;
  1271. lsa = ospf6_lsdb_next (lsa))
  1272. {
  1273. ospf6_lsa_age_update_to_send (lsa, on->ospf6_if->transdelay);
  1274. /* MTU check */
  1275. if (p - sendbuf + sizeof (struct ospf6_lsa_header) >
  1276. on->ospf6_if->ifmtu)
  1277. {
  1278. ospf6_lsa_unlock (lsa);
  1279. break;
  1280. }
  1281. memcpy (p, lsa->header, sizeof (struct ospf6_lsa_header));
  1282. p += sizeof (struct ospf6_lsa_header);
  1283. }
  1284. }
  1285. oh->type = OSPF6_MESSAGE_TYPE_DBDESC;
  1286. oh->length = htons (p - sendbuf);
  1287. ospf6_send (on->ospf6_if->linklocal_addr, &on->linklocal_addr,
  1288. on->ospf6_if, oh);
  1289. return 0;
  1290. }
  1291. int
  1292. ospf6_dbdesc_send_newone (struct thread *thread)
  1293. {
  1294. struct ospf6_neighbor *on;
  1295. struct ospf6_lsa *lsa;
  1296. unsigned int size = 0;
  1297. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1298. ospf6_lsdb_remove_all (on->dbdesc_list);
  1299. /* move LSAs from summary_list to dbdesc_list (within neighbor structure)
  1300. so that ospf6_send_dbdesc () can send those LSAs */
  1301. size = sizeof (struct ospf6_lsa_header) + sizeof (struct ospf6_dbdesc);
  1302. for (lsa = ospf6_lsdb_head (on->summary_list); lsa;
  1303. lsa = ospf6_lsdb_next (lsa))
  1304. {
  1305. if (size + sizeof (struct ospf6_lsa_header) > on->ospf6_if->ifmtu)
  1306. {
  1307. ospf6_lsa_unlock (lsa);
  1308. break;
  1309. }
  1310. ospf6_lsdb_add (ospf6_lsa_copy (lsa), on->dbdesc_list);
  1311. ospf6_lsdb_remove (lsa, on->summary_list);
  1312. size += sizeof (struct ospf6_lsa_header);
  1313. }
  1314. if (on->summary_list->count == 0)
  1315. UNSET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT);
  1316. /* If slave, More bit check must be done here */
  1317. if (! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT) && /* Slave */
  1318. ! CHECK_FLAG (on->dbdesc_last.bits, OSPF6_DBDESC_MBIT) &&
  1319. ! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT))
  1320. thread_add_event (master, exchange_done, on, 0);
  1321. thread_execute (master, ospf6_dbdesc_send, on, 0);
  1322. return 0;
  1323. }
  1324. int
  1325. ospf6_lsreq_send (struct thread *thread)
  1326. {
  1327. struct ospf6_neighbor *on;
  1328. struct ospf6_header *oh;
  1329. struct ospf6_lsreq_entry *e;
  1330. u_char *p;
  1331. struct ospf6_lsa *lsa;
  1332. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1333. on->thread_send_lsreq = (struct thread *) NULL;
  1334. /* LSReq will be sent only in ExStart or Loading */
  1335. if (on->state != OSPF6_NEIGHBOR_EXCHANGE &&
  1336. on->state != OSPF6_NEIGHBOR_LOADING)
  1337. {
  1338. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSREQ, SEND))
  1339. zlog_debug ("Quit to send LSReq to neighbor %s state %s",
  1340. on->name, ospf6_neighbor_state_str[on->state]);
  1341. return 0;
  1342. }
  1343. /* schedule loading_done if request list is empty */
  1344. if (on->request_list->count == 0)
  1345. {
  1346. thread_add_event (master, loading_done, on, 0);
  1347. return 0;
  1348. }
  1349. /* set next thread */
  1350. on->thread_send_lsreq =
  1351. thread_add_timer (master, ospf6_lsreq_send, on,
  1352. on->ospf6_if->rxmt_interval);
  1353. memset (sendbuf, 0, iobuflen);
  1354. oh = (struct ospf6_header *) sendbuf;
  1355. /* set Request entries in lsreq */
  1356. p = (u_char *)((caddr_t) oh + sizeof (struct ospf6_header));
  1357. for (lsa = ospf6_lsdb_head (on->request_list); lsa;
  1358. lsa = ospf6_lsdb_next (lsa))
  1359. {
  1360. /* MTU check */
  1361. if (p - sendbuf + sizeof (struct ospf6_lsreq_entry) > on->ospf6_if->ifmtu)
  1362. {
  1363. ospf6_lsa_unlock (lsa);
  1364. break;
  1365. }
  1366. e = (struct ospf6_lsreq_entry *) p;
  1367. e->type = lsa->header->type;
  1368. e->id = lsa->header->id;
  1369. e->adv_router = lsa->header->adv_router;
  1370. p += sizeof (struct ospf6_lsreq_entry);
  1371. }
  1372. oh->type = OSPF6_MESSAGE_TYPE_LSREQ;
  1373. oh->length = htons (p - sendbuf);
  1374. ospf6_send (on->ospf6_if->linklocal_addr, &on->linklocal_addr,
  1375. on->ospf6_if, oh);
  1376. return 0;
  1377. }
  1378. int
  1379. ospf6_lsupdate_send_neighbor (struct thread *thread)
  1380. {
  1381. struct ospf6_neighbor *on;
  1382. struct ospf6_header *oh;
  1383. struct ospf6_lsupdate *lsupdate;
  1384. u_char *p;
  1385. int num;
  1386. struct ospf6_lsa *lsa;
  1387. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1388. on->thread_send_lsupdate = (struct thread *) NULL;
  1389. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSUPDATE, SEND))
  1390. zlog_debug ("LSUpdate to neighbor %s", on->name);
  1391. if (on->state < OSPF6_NEIGHBOR_EXCHANGE)
  1392. {
  1393. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSUPDATE, SEND))
  1394. zlog_debug ("Quit to send (neighbor state %s)",
  1395. ospf6_neighbor_state_str[on->state]);
  1396. return 0;
  1397. }
  1398. /* if we have nothing to send, return */
  1399. if (on->lsupdate_list->count == 0 &&
  1400. on->retrans_list->count == 0)
  1401. {
  1402. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSUPDATE, SEND))
  1403. zlog_debug ("Quit to send (nothing to send)");
  1404. return 0;
  1405. }
  1406. memset (sendbuf, 0, iobuflen);
  1407. oh = (struct ospf6_header *) sendbuf;
  1408. lsupdate = (struct ospf6_lsupdate *)
  1409. ((caddr_t) oh + sizeof (struct ospf6_header));
  1410. p = (u_char *)((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  1411. num = 0;
  1412. /* lsupdate_list lists those LSA which doesn't need to be
  1413. retransmitted. remove those from the list */
  1414. for (lsa = ospf6_lsdb_head (on->lsupdate_list); lsa;
  1415. lsa = ospf6_lsdb_next (lsa))
  1416. {
  1417. /* MTU check */
  1418. if ( (p - sendbuf + (unsigned int)OSPF6_LSA_SIZE (lsa->header))
  1419. > on->ospf6_if->ifmtu)
  1420. {
  1421. ospf6_lsa_unlock (lsa);
  1422. break;
  1423. }
  1424. ospf6_lsa_age_update_to_send (lsa, on->ospf6_if->transdelay);
  1425. memcpy (p, lsa->header, OSPF6_LSA_SIZE (lsa->header));
  1426. p += OSPF6_LSA_SIZE (lsa->header);
  1427. num++;
  1428. assert (lsa->lock == 2);
  1429. ospf6_lsdb_remove (lsa, on->lsupdate_list);
  1430. }
  1431. for (lsa = ospf6_lsdb_head (on->retrans_list); lsa;
  1432. lsa = ospf6_lsdb_next (lsa))
  1433. {
  1434. /* MTU check */
  1435. if ( (p - sendbuf + (unsigned int)OSPF6_LSA_SIZE (lsa->header))
  1436. > on->ospf6_if->ifmtu)
  1437. {
  1438. ospf6_lsa_unlock (lsa);
  1439. break;
  1440. }
  1441. ospf6_lsa_age_update_to_send (lsa, on->ospf6_if->transdelay);
  1442. memcpy (p, lsa->header, OSPF6_LSA_SIZE (lsa->header));
  1443. p += OSPF6_LSA_SIZE (lsa->header);
  1444. num++;
  1445. }
  1446. lsupdate->lsa_number = htonl (num);
  1447. oh->type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  1448. oh->length = htons (p - sendbuf);
  1449. ospf6_send (on->ospf6_if->linklocal_addr, &on->linklocal_addr,
  1450. on->ospf6_if, oh);
  1451. if (on->lsupdate_list->count != 0 ||
  1452. on->retrans_list->count != 0)
  1453. {
  1454. if (on->lsupdate_list->count != 0)
  1455. on->thread_send_lsupdate =
  1456. thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0);
  1457. else
  1458. on->thread_send_lsupdate =
  1459. thread_add_timer (master, ospf6_lsupdate_send_neighbor, on,
  1460. on->ospf6_if->rxmt_interval);
  1461. }
  1462. return 0;
  1463. }
  1464. int
  1465. ospf6_lsupdate_send_interface (struct thread *thread)
  1466. {
  1467. struct ospf6_interface *oi;
  1468. struct ospf6_header *oh;
  1469. struct ospf6_lsupdate *lsupdate;
  1470. u_char *p;
  1471. int num;
  1472. struct ospf6_lsa *lsa;
  1473. oi = (struct ospf6_interface *) THREAD_ARG (thread);
  1474. oi->thread_send_lsupdate = (struct thread *) NULL;
  1475. if (oi->state <= OSPF6_INTERFACE_WAITING)
  1476. {
  1477. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSUPDATE, SEND))
  1478. zlog_debug ("Quit to send LSUpdate to interface %s state %s",
  1479. oi->interface->name, ospf6_interface_state_str[oi->state]);
  1480. return 0;
  1481. }
  1482. /* if we have nothing to send, return */
  1483. if (oi->lsupdate_list->count == 0)
  1484. return 0;
  1485. memset (sendbuf, 0, iobuflen);
  1486. oh = (struct ospf6_header *) sendbuf;
  1487. lsupdate = (struct ospf6_lsupdate *)((caddr_t) oh +
  1488. sizeof (struct ospf6_header));
  1489. p = (u_char *)((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  1490. num = 0;
  1491. for (lsa = ospf6_lsdb_head (oi->lsupdate_list); lsa;
  1492. lsa = ospf6_lsdb_next (lsa))
  1493. {
  1494. /* MTU check */
  1495. if ( (p - sendbuf + ((unsigned int)OSPF6_LSA_SIZE (lsa->header)))
  1496. > oi->ifmtu)
  1497. {
  1498. ospf6_lsa_unlock (lsa);
  1499. break;
  1500. }
  1501. ospf6_lsa_age_update_to_send (lsa, oi->transdelay);
  1502. memcpy (p, lsa->header, OSPF6_LSA_SIZE (lsa->header));
  1503. p += OSPF6_LSA_SIZE (lsa->header);
  1504. num++;
  1505. assert (lsa->lock == 2);
  1506. ospf6_lsdb_remove (lsa, oi->lsupdate_list);
  1507. }
  1508. lsupdate->lsa_number = htonl (num);
  1509. oh->type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  1510. oh->length = htons (p - sendbuf);
  1511. if (oi->state == OSPF6_INTERFACE_DR ||
  1512. oi->state == OSPF6_INTERFACE_BDR)
  1513. ospf6_send (oi->linklocal_addr, &allspfrouters6, oi, oh);
  1514. else
  1515. ospf6_send (oi->linklocal_addr, &alldrouters6, oi, oh);
  1516. if (oi->lsupdate_list->count > 0)
  1517. {
  1518. oi->thread_send_lsupdate =
  1519. thread_add_event (master, ospf6_lsupdate_send_interface, oi, 0);
  1520. }
  1521. return 0;
  1522. }
  1523. int
  1524. ospf6_lsack_send_neighbor (struct thread *thread)
  1525. {
  1526. struct ospf6_neighbor *on;
  1527. struct ospf6_header *oh;
  1528. u_char *p;
  1529. struct ospf6_lsa *lsa;
  1530. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1531. on->thread_send_lsack = (struct thread *) NULL;
  1532. if (on->state < OSPF6_NEIGHBOR_EXCHANGE)
  1533. {
  1534. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSACK, SEND))
  1535. zlog_debug ("Quit to send LSAck to neighbor %s state %s",
  1536. on->name, ospf6_neighbor_state_str[on->state]);
  1537. return 0;
  1538. }
  1539. /* if we have nothing to send, return */
  1540. if (on->lsack_list->count == 0)
  1541. return 0;
  1542. memset (sendbuf, 0, iobuflen);
  1543. oh = (struct ospf6_header *) sendbuf;
  1544. p = (u_char *)((caddr_t) oh + sizeof (struct ospf6_header));
  1545. for (lsa = ospf6_lsdb_head (on->lsack_list); lsa;
  1546. lsa = ospf6_lsdb_next (lsa))
  1547. {
  1548. /* MTU check */
  1549. if (p - sendbuf + sizeof (struct ospf6_lsa_header) > on->ospf6_if->ifmtu)
  1550. {
  1551. /* if we run out of packet size/space here,
  1552. better to try again soon. */
  1553. THREAD_OFF (on->thread_send_lsack);
  1554. on->thread_send_lsack =
  1555. thread_add_event (master, ospf6_lsack_send_neighbor, on, 0);
  1556. ospf6_lsa_unlock (lsa);
  1557. break;
  1558. }
  1559. ospf6_lsa_age_update_to_send (lsa, on->ospf6_if->transdelay);
  1560. memcpy (p, lsa->header, sizeof (struct ospf6_lsa_header));
  1561. p += sizeof (struct ospf6_lsa_header);
  1562. assert (lsa->lock == 2);
  1563. ospf6_lsdb_remove (lsa, on->lsack_list);
  1564. }
  1565. oh->type = OSPF6_MESSAGE_TYPE_LSACK;
  1566. oh->length = htons (p - sendbuf);
  1567. ospf6_send (on->ospf6_if->linklocal_addr, &on->linklocal_addr,
  1568. on->ospf6_if, oh);
  1569. return 0;
  1570. }
  1571. int
  1572. ospf6_lsack_send_interface (struct thread *thread)
  1573. {
  1574. struct ospf6_interface *oi;
  1575. struct ospf6_header *oh;
  1576. u_char *p;
  1577. struct ospf6_lsa *lsa;
  1578. oi = (struct ospf6_interface *) THREAD_ARG (thread);
  1579. oi->thread_send_lsack = (struct thread *) NULL;
  1580. if (oi->state <= OSPF6_INTERFACE_WAITING)
  1581. {
  1582. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSACK, SEND))
  1583. zlog_debug ("Quit to send LSAck to interface %s state %s",
  1584. oi->interface->name, ospf6_interface_state_str[oi->state]);
  1585. return 0;
  1586. }
  1587. /* if we have nothing to send, return */
  1588. if (oi->lsack_list->count == 0)
  1589. return 0;
  1590. memset (sendbuf, 0, iobuflen);
  1591. oh = (struct ospf6_header *) sendbuf;
  1592. p = (u_char *)((caddr_t) oh + sizeof (struct ospf6_header));
  1593. for (lsa = ospf6_lsdb_head (oi->lsack_list); lsa;
  1594. lsa = ospf6_lsdb_next (lsa))
  1595. {
  1596. /* MTU check */
  1597. if (p - sendbuf + sizeof (struct ospf6_lsa_header) > oi->ifmtu)
  1598. {
  1599. /* if we run out of packet size/space here,
  1600. better to try again soon. */
  1601. THREAD_OFF (oi->thread_send_lsack);
  1602. oi->thread_send_lsack =
  1603. thread_add_event (master, ospf6_lsack_send_interface, oi, 0);
  1604. ospf6_lsa_unlock (lsa);
  1605. break;
  1606. }
  1607. ospf6_lsa_age_update_to_send (lsa, oi->transdelay);
  1608. memcpy (p, lsa->header, sizeof (struct ospf6_lsa_header));
  1609. p += sizeof (struct ospf6_lsa_header);
  1610. assert (lsa->lock == 2);
  1611. ospf6_lsdb_remove (lsa, oi->lsack_list);
  1612. }
  1613. oh->type = OSPF6_MESSAGE_TYPE_LSACK;
  1614. oh->length = htons (p - sendbuf);
  1615. if (oi->state == OSPF6_INTERFACE_DR ||
  1616. oi->state == OSPF6_INTERFACE_BDR)
  1617. ospf6_send (oi->linklocal_addr, &allspfrouters6, oi, oh);
  1618. else
  1619. ospf6_send (oi->linklocal_addr, &alldrouters6, oi, oh);
  1620. if (oi->thread_send_lsack == NULL && oi->lsack_list->count > 0)
  1621. {
  1622. oi->thread_send_lsack =
  1623. thread_add_event (master, ospf6_lsack_send_interface, oi, 0);
  1624. }
  1625. return 0;
  1626. }
  1627. /* Commands */
  1628. DEFUN (debug_ospf6_message,
  1629. debug_ospf6_message_cmd,
  1630. "debug ospf6 message (unknown|hello|dbdesc|lsreq|lsupdate|lsack|all)",
  1631. DEBUG_STR
  1632. OSPF6_STR
  1633. "Debug OSPFv3 message\n"
  1634. "Debug Unknown message\n"
  1635. "Debug Hello message\n"
  1636. "Debug Database Description message\n"
  1637. "Debug Link State Request message\n"
  1638. "Debug Link State Update message\n"
  1639. "Debug Link State Acknowledgement message\n"
  1640. "Debug All message\n"
  1641. )
  1642. {
  1643. unsigned char level = 0;
  1644. int type = 0;
  1645. int i;
  1646. assert (argc > 0);
  1647. /* check type */
  1648. if (! strncmp (argv[0], "u", 1))
  1649. type = OSPF6_MESSAGE_TYPE_UNKNOWN;
  1650. else if (! strncmp (argv[0], "h", 1))
  1651. type = OSPF6_MESSAGE_TYPE_HELLO;
  1652. else if (! strncmp (argv[0], "d", 1))
  1653. type = OSPF6_MESSAGE_TYPE_DBDESC;
  1654. else if (! strncmp (argv[0], "lsr", 3))
  1655. type = OSPF6_MESSAGE_TYPE_LSREQ;
  1656. else if (! strncmp (argv[0], "lsu", 3))
  1657. type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  1658. else if (! strncmp (argv[0], "lsa", 3))
  1659. type = OSPF6_MESSAGE_TYPE_LSACK;
  1660. else if (! strncmp (argv[0], "a", 1))
  1661. type = OSPF6_MESSAGE_TYPE_ALL;
  1662. if (argc == 1)
  1663. level = OSPF6_DEBUG_MESSAGE_SEND | OSPF6_DEBUG_MESSAGE_RECV;
  1664. else if (! strncmp (argv[1], "s", 1))
  1665. level = OSPF6_DEBUG_MESSAGE_SEND;
  1666. else if (! strncmp (argv[1], "r", 1))
  1667. level = OSPF6_DEBUG_MESSAGE_RECV;
  1668. if (type == OSPF6_MESSAGE_TYPE_ALL)
  1669. {
  1670. for (i = 0; i < 6; i++)
  1671. OSPF6_DEBUG_MESSAGE_ON (i, level);
  1672. }
  1673. else
  1674. OSPF6_DEBUG_MESSAGE_ON (type, level);
  1675. return CMD_SUCCESS;
  1676. }
  1677. ALIAS (debug_ospf6_message,
  1678. debug_ospf6_message_sendrecv_cmd,
  1679. "debug ospf6 message (unknown|hello|dbdesc|lsreq|lsupdate|lsack|all) (send|recv)",
  1680. DEBUG_STR
  1681. OSPF6_STR
  1682. "Debug OSPFv3 message\n"
  1683. "Debug Unknown message\n"
  1684. "Debug Hello message\n"
  1685. "Debug Database Description message\n"
  1686. "Debug Link State Request message\n"
  1687. "Debug Link State Update message\n"
  1688. "Debug Link State Acknowledgement message\n"
  1689. "Debug All message\n"
  1690. "Debug only sending message\n"
  1691. "Debug only receiving message\n"
  1692. )
  1693. DEFUN (no_debug_ospf6_message,
  1694. no_debug_ospf6_message_cmd,
  1695. "no debug ospf6 message (unknown|hello|dbdesc|lsreq|lsupdate|lsack|all)",
  1696. NO_STR
  1697. DEBUG_STR
  1698. OSPF6_STR
  1699. "Debug OSPFv3 message\n"
  1700. "Debug Unknown message\n"
  1701. "Debug Hello message\n"
  1702. "Debug Database Description message\n"
  1703. "Debug Link State Request message\n"
  1704. "Debug Link State Update message\n"
  1705. "Debug Link State Acknowledgement message\n"
  1706. "Debug All message\n"
  1707. )
  1708. {
  1709. unsigned char level = 0;
  1710. int type = 0;
  1711. int i;
  1712. assert (argc > 0);
  1713. /* check type */
  1714. if (! strncmp (argv[0], "u", 1))
  1715. type = OSPF6_MESSAGE_TYPE_UNKNOWN;
  1716. else if (! strncmp (argv[0], "h", 1))
  1717. type = OSPF6_MESSAGE_TYPE_HELLO;
  1718. else if (! strncmp (argv[0], "d", 1))
  1719. type = OSPF6_MESSAGE_TYPE_DBDESC;
  1720. else if (! strncmp (argv[0], "lsr", 3))
  1721. type = OSPF6_MESSAGE_TYPE_LSREQ;
  1722. else if (! strncmp (argv[0], "lsu", 3))
  1723. type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  1724. else if (! strncmp (argv[0], "lsa", 3))
  1725. type = OSPF6_MESSAGE_TYPE_LSACK;
  1726. else if (! strncmp (argv[0], "a", 1))
  1727. type = OSPF6_MESSAGE_TYPE_ALL;
  1728. if (argc == 1)
  1729. level = OSPF6_DEBUG_MESSAGE_SEND | OSPF6_DEBUG_MESSAGE_RECV;
  1730. else if (! strncmp (argv[1], "s", 1))
  1731. level = OSPF6_DEBUG_MESSAGE_SEND;
  1732. else if (! strncmp (argv[1], "r", 1))
  1733. level = OSPF6_DEBUG_MESSAGE_RECV;
  1734. if (type == OSPF6_MESSAGE_TYPE_ALL)
  1735. {
  1736. for (i = 0; i < 6; i++)
  1737. OSPF6_DEBUG_MESSAGE_OFF (i, level);
  1738. }
  1739. else
  1740. OSPF6_DEBUG_MESSAGE_OFF (type, level);
  1741. return CMD_SUCCESS;
  1742. }
  1743. ALIAS (no_debug_ospf6_message,
  1744. no_debug_ospf6_message_sendrecv_cmd,
  1745. "no debug ospf6 message "
  1746. "(unknown|hello|dbdesc|lsreq|lsupdate|lsack|all) (send|recv)",
  1747. NO_STR
  1748. DEBUG_STR
  1749. OSPF6_STR
  1750. "Debug OSPFv3 message\n"
  1751. "Debug Unknown message\n"
  1752. "Debug Hello message\n"
  1753. "Debug Database Description message\n"
  1754. "Debug Link State Request message\n"
  1755. "Debug Link State Update message\n"
  1756. "Debug Link State Acknowledgement message\n"
  1757. "Debug All message\n"
  1758. "Debug only sending message\n"
  1759. "Debug only receiving message\n"
  1760. )
  1761. int
  1762. config_write_ospf6_debug_message (struct vty *vty)
  1763. {
  1764. const char *type_str[] = {"unknown", "hello", "dbdesc",
  1765. "lsreq", "lsupdate", "lsack"};
  1766. unsigned char s = 0, r = 0;
  1767. int i;
  1768. for (i = 0; i < 6; i++)
  1769. {
  1770. if (IS_OSPF6_DEBUG_MESSAGE (i, SEND))
  1771. s |= 1 << i;
  1772. if (IS_OSPF6_DEBUG_MESSAGE (i, RECV))
  1773. r |= 1 << i;
  1774. }
  1775. if (s == 0x3f && r == 0x3f)
  1776. {
  1777. vty_out (vty, "debug ospf6 message all%s", VNL);
  1778. return 0;
  1779. }
  1780. if (s == 0x3f && r == 0)
  1781. {
  1782. vty_out (vty, "debug ospf6 message all send%s", VNL);
  1783. return 0;
  1784. }
  1785. else if (s == 0 && r == 0x3f)
  1786. {
  1787. vty_out (vty, "debug ospf6 message all recv%s", VNL);
  1788. return 0;
  1789. }
  1790. /* Unknown message is logged by default */
  1791. if (! IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, SEND) &&
  1792. ! IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1793. vty_out (vty, "no debug ospf6 message unknown%s", VNL);
  1794. else if (! IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, SEND))
  1795. vty_out (vty, "no debug ospf6 message unknown send%s", VNL);
  1796. else if (! IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1797. vty_out (vty, "no debug ospf6 message unknown recv%s", VNL);
  1798. for (i = 1; i < 6; i++)
  1799. {
  1800. if (IS_OSPF6_DEBUG_MESSAGE (i, SEND) &&
  1801. IS_OSPF6_DEBUG_MESSAGE (i, RECV))
  1802. vty_out (vty, "debug ospf6 message %s%s", type_str[i], VNL);
  1803. else if (IS_OSPF6_DEBUG_MESSAGE (i, SEND))
  1804. vty_out (vty, "debug ospf6 message %s send%s", type_str[i],
  1805. VNL);
  1806. else if (IS_OSPF6_DEBUG_MESSAGE (i, RECV))
  1807. vty_out (vty, "debug ospf6 message %s recv%s", type_str[i],
  1808. VNL);
  1809. }
  1810. return 0;
  1811. }
  1812. void
  1813. install_element_ospf6_debug_message (void)
  1814. {
  1815. install_element (ENABLE_NODE, &debug_ospf6_message_cmd);
  1816. install_element (ENABLE_NODE, &no_debug_ospf6_message_cmd);
  1817. install_element (ENABLE_NODE, &debug_ospf6_message_sendrecv_cmd);
  1818. install_element (ENABLE_NODE, &no_debug_ospf6_message_sendrecv_cmd);
  1819. install_element (CONFIG_NODE, &debug_ospf6_message_cmd);
  1820. install_element (CONFIG_NODE, &no_debug_ospf6_message_cmd);
  1821. install_element (CONFIG_NODE, &debug_ospf6_message_sendrecv_cmd);
  1822. install_element (CONFIG_NODE, &no_debug_ospf6_message_sendrecv_cmd);
  1823. }