ospf6_message.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  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. /* for structures and macros ospf6_lsa_examin() needs */
  38. #include "ospf6_abr.h"
  39. #include "ospf6_asbr.h"
  40. #include "ospf6_intra.h"
  41. #include "ospf6_flood.h"
  42. #include "ospf6d.h"
  43. #include <netinet/ip6.h>
  44. unsigned char conf_debug_ospf6_message[6] = {0x03, 0, 0, 0, 0, 0};
  45. static const struct message ospf6_message_type_str [] =
  46. {
  47. { OSPF6_MESSAGE_TYPE_HELLO, "Hello" },
  48. { OSPF6_MESSAGE_TYPE_DBDESC, "DbDesc" },
  49. { OSPF6_MESSAGE_TYPE_LSREQ, "LSReq" },
  50. { OSPF6_MESSAGE_TYPE_LSUPDATE, "LSUpdate" },
  51. { OSPF6_MESSAGE_TYPE_LSACK, "LSAck" },
  52. };
  53. static const size_t ospf6_message_type_str_max = array_size(ospf6_message_type_str);
  54. /* Minimum (besides the standard OSPF packet header) lengths for OSPF
  55. packets of particular types, offset is the "type" field. */
  56. const u_int16_t ospf6_packet_minlen[OSPF6_MESSAGE_TYPE_ALL] =
  57. {
  58. 0,
  59. OSPF6_HELLO_MIN_SIZE,
  60. OSPF6_DB_DESC_MIN_SIZE,
  61. OSPF6_LS_REQ_MIN_SIZE,
  62. OSPF6_LS_UPD_MIN_SIZE,
  63. OSPF6_LS_ACK_MIN_SIZE
  64. };
  65. /* Minimum (besides the standard LSA header) lengths for LSAs of particular
  66. types, offset is the "LSA function code" portion of "LSA type" field. */
  67. const u_int16_t ospf6_lsa_minlen[OSPF6_LSTYPE_SIZE] =
  68. {
  69. 0,
  70. /* 0x2001 */ OSPF6_ROUTER_LSA_MIN_SIZE,
  71. /* 0x2002 */ OSPF6_NETWORK_LSA_MIN_SIZE,
  72. /* 0x2003 */ OSPF6_INTER_PREFIX_LSA_MIN_SIZE,
  73. /* 0x2004 */ OSPF6_INTER_ROUTER_LSA_FIX_SIZE,
  74. /* 0x4005 */ OSPF6_AS_EXTERNAL_LSA_MIN_SIZE,
  75. /* 0x2006 */ 0,
  76. /* 0x2007 */ OSPF6_AS_EXTERNAL_LSA_MIN_SIZE,
  77. /* 0x0008 */ OSPF6_LINK_LSA_MIN_SIZE,
  78. /* 0x2009 */ OSPF6_INTRA_PREFIX_LSA_MIN_SIZE
  79. };
  80. /* print functions */
  81. static void
  82. ospf6_header_print (struct ospf6_header *oh)
  83. {
  84. char router_id[16], area_id[16];
  85. inet_ntop (AF_INET, &oh->router_id, router_id, sizeof (router_id));
  86. inet_ntop (AF_INET, &oh->area_id, area_id, sizeof (area_id));
  87. zlog_debug (" OSPFv%d Type:%d Len:%hu Router-ID:%s",
  88. oh->version, oh->type, ntohs (oh->length), router_id);
  89. zlog_debug (" Area-ID:%s Cksum:%hx Instance-ID:%d",
  90. area_id, ntohs (oh->checksum), oh->instance_id);
  91. }
  92. void
  93. ospf6_hello_print (struct ospf6_header *oh)
  94. {
  95. struct ospf6_hello *hello;
  96. char options[16];
  97. char drouter[16], bdrouter[16], neighbor[16];
  98. char *p;
  99. ospf6_header_print (oh);
  100. assert (oh->type == OSPF6_MESSAGE_TYPE_HELLO);
  101. hello = (struct ospf6_hello *)
  102. ((caddr_t) oh + sizeof (struct ospf6_header));
  103. inet_ntop (AF_INET, &hello->drouter, drouter, sizeof (drouter));
  104. inet_ntop (AF_INET, &hello->bdrouter, bdrouter, sizeof (bdrouter));
  105. ospf6_options_printbuf (hello->options, options, sizeof (options));
  106. zlog_debug (" I/F-Id:%ld Priority:%d Option:%s",
  107. (u_long) ntohl (hello->interface_id), hello->priority, options);
  108. zlog_debug (" HelloInterval:%hu DeadInterval:%hu",
  109. ntohs (hello->hello_interval), ntohs (hello->dead_interval));
  110. zlog_debug (" DR:%s BDR:%s", drouter, bdrouter);
  111. for (p = (char *) ((caddr_t) hello + sizeof (struct ospf6_hello));
  112. p + sizeof (u_int32_t) <= OSPF6_MESSAGE_END (oh);
  113. p += sizeof (u_int32_t))
  114. {
  115. inet_ntop (AF_INET, (void *) p, neighbor, sizeof (neighbor));
  116. zlog_debug (" Neighbor: %s", neighbor);
  117. }
  118. assert (p == OSPF6_MESSAGE_END (oh));
  119. }
  120. void
  121. ospf6_dbdesc_print (struct ospf6_header *oh)
  122. {
  123. struct ospf6_dbdesc *dbdesc;
  124. char options[16];
  125. char *p;
  126. ospf6_header_print (oh);
  127. assert (oh->type == OSPF6_MESSAGE_TYPE_DBDESC);
  128. dbdesc = (struct ospf6_dbdesc *)
  129. ((caddr_t) oh + sizeof (struct ospf6_header));
  130. ospf6_options_printbuf (dbdesc->options, options, sizeof (options));
  131. zlog_debug (" MBZ: %#x Option: %s IfMTU: %hu",
  132. dbdesc->reserved1, options, ntohs (dbdesc->ifmtu));
  133. zlog_debug (" MBZ: %#x Bits: %s%s%s SeqNum: %#lx",
  134. dbdesc->reserved2,
  135. (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT) ? "I" : "-"),
  136. (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MBIT) ? "M" : "-"),
  137. (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT) ? "m" : "s"),
  138. (u_long) ntohl (dbdesc->seqnum));
  139. for (p = (char *) ((caddr_t) dbdesc + sizeof (struct ospf6_dbdesc));
  140. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  141. p += sizeof (struct ospf6_lsa_header))
  142. ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
  143. assert (p == OSPF6_MESSAGE_END (oh));
  144. }
  145. void
  146. ospf6_lsreq_print (struct ospf6_header *oh)
  147. {
  148. char id[16], adv_router[16];
  149. char *p;
  150. ospf6_header_print (oh);
  151. assert (oh->type == OSPF6_MESSAGE_TYPE_LSREQ);
  152. for (p = (char *) ((caddr_t) oh + sizeof (struct ospf6_header));
  153. p + sizeof (struct ospf6_lsreq_entry) <= OSPF6_MESSAGE_END (oh);
  154. p += sizeof (struct ospf6_lsreq_entry))
  155. {
  156. struct ospf6_lsreq_entry *e = (struct ospf6_lsreq_entry *) p;
  157. inet_ntop (AF_INET, &e->adv_router, adv_router, sizeof (adv_router));
  158. inet_ntop (AF_INET, &e->id, id, sizeof (id));
  159. zlog_debug (" [%s Id:%s Adv:%s]",
  160. ospf6_lstype_name (e->type), id, adv_router);
  161. }
  162. assert (p == OSPF6_MESSAGE_END (oh));
  163. }
  164. void
  165. ospf6_lsupdate_print (struct ospf6_header *oh)
  166. {
  167. struct ospf6_lsupdate *lsupdate;
  168. u_long num;
  169. char *p;
  170. ospf6_header_print (oh);
  171. assert (oh->type == OSPF6_MESSAGE_TYPE_LSUPDATE);
  172. lsupdate = (struct ospf6_lsupdate *)
  173. ((caddr_t) oh + sizeof (struct ospf6_header));
  174. num = ntohl (lsupdate->lsa_number);
  175. zlog_debug (" Number of LSA: %ld", num);
  176. for (p = (char *) ((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  177. p < OSPF6_MESSAGE_END (oh) &&
  178. p + OSPF6_LSA_SIZE (p) <= OSPF6_MESSAGE_END (oh);
  179. p += OSPF6_LSA_SIZE (p))
  180. {
  181. ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
  182. }
  183. assert (p == OSPF6_MESSAGE_END (oh));
  184. }
  185. void
  186. ospf6_lsack_print (struct ospf6_header *oh)
  187. {
  188. char *p;
  189. ospf6_header_print (oh);
  190. assert (oh->type == OSPF6_MESSAGE_TYPE_LSACK);
  191. for (p = (char *) ((caddr_t) oh + sizeof (struct ospf6_header));
  192. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  193. p += sizeof (struct ospf6_lsa_header))
  194. ospf6_lsa_header_print_raw ((struct ospf6_lsa_header *) p);
  195. assert (p == OSPF6_MESSAGE_END (oh));
  196. }
  197. static void
  198. ospf6_hello_recv (struct in6_addr *src, struct in6_addr *dst,
  199. struct ospf6_interface *oi, struct ospf6_header *oh)
  200. {
  201. struct ospf6_hello *hello;
  202. struct ospf6_neighbor *on;
  203. char *p;
  204. int twoway = 0;
  205. int neighborchange = 0;
  206. int backupseen = 0;
  207. hello = (struct ospf6_hello *)
  208. ((caddr_t) oh + sizeof (struct ospf6_header));
  209. /* HelloInterval check */
  210. if (ntohs (hello->hello_interval) != oi->hello_interval)
  211. {
  212. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  213. zlog_debug ("HelloInterval mismatch");
  214. return;
  215. }
  216. /* RouterDeadInterval check */
  217. if (ntohs (hello->dead_interval) != oi->dead_interval)
  218. {
  219. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  220. zlog_debug ("RouterDeadInterval mismatch");
  221. return;
  222. }
  223. /* E-bit check */
  224. if (OSPF6_OPT_ISSET (hello->options, OSPF6_OPT_E) !=
  225. OSPF6_OPT_ISSET (oi->area->options, OSPF6_OPT_E))
  226. {
  227. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  228. zlog_debug ("E-bit mismatch");
  229. return;
  230. }
  231. /* Find neighbor, create if not exist */
  232. on = ospf6_neighbor_lookup (oh->router_id, oi);
  233. if (on == NULL)
  234. {
  235. on = ospf6_neighbor_create (oh->router_id, oi);
  236. on->prev_drouter = on->drouter = hello->drouter;
  237. on->prev_bdrouter = on->bdrouter = hello->bdrouter;
  238. on->priority = hello->priority;
  239. }
  240. /* always override neighbor's source address and ifindex */
  241. on->ifindex = ntohl (hello->interface_id);
  242. memcpy (&on->linklocal_addr, src, sizeof (struct in6_addr));
  243. /* TwoWay check */
  244. for (p = (char *) ((caddr_t) hello + sizeof (struct ospf6_hello));
  245. p + sizeof (u_int32_t) <= OSPF6_MESSAGE_END (oh);
  246. p += sizeof (u_int32_t))
  247. {
  248. u_int32_t *router_id = (u_int32_t *) p;
  249. if (*router_id == oi->area->ospf6->router_id)
  250. twoway++;
  251. }
  252. assert (p == OSPF6_MESSAGE_END (oh));
  253. /* RouterPriority check */
  254. if (on->priority != hello->priority)
  255. {
  256. on->priority = hello->priority;
  257. neighborchange++;
  258. }
  259. /* DR check */
  260. if (on->drouter != hello->drouter)
  261. {
  262. on->prev_drouter = on->drouter;
  263. on->drouter = hello->drouter;
  264. if (on->prev_drouter == on->router_id || on->drouter == on->router_id)
  265. neighborchange++;
  266. }
  267. /* BDR check */
  268. if (on->bdrouter != hello->bdrouter)
  269. {
  270. on->prev_bdrouter = on->bdrouter;
  271. on->bdrouter = hello->bdrouter;
  272. if (on->prev_bdrouter == on->router_id || on->bdrouter == on->router_id)
  273. neighborchange++;
  274. }
  275. /* BackupSeen check */
  276. if (oi->state == OSPF6_INTERFACE_WAITING)
  277. {
  278. if (hello->bdrouter == on->router_id)
  279. backupseen++;
  280. else if (hello->drouter == on->router_id && hello->bdrouter == htonl (0))
  281. backupseen++;
  282. }
  283. /* Execute neighbor events */
  284. thread_execute (master, hello_received, on, 0);
  285. if (twoway)
  286. thread_execute (master, twoway_received, on, 0);
  287. else
  288. thread_execute (master, oneway_received, on, 0);
  289. /* Schedule interface events */
  290. if (backupseen)
  291. thread_add_event (master, backup_seen, oi, 0);
  292. if (neighborchange)
  293. thread_add_event (master, neighbor_change, oi, 0);
  294. }
  295. static void
  296. ospf6_dbdesc_recv_master (struct ospf6_header *oh,
  297. struct ospf6_neighbor *on)
  298. {
  299. struct ospf6_dbdesc *dbdesc;
  300. char *p;
  301. dbdesc = (struct ospf6_dbdesc *)
  302. ((caddr_t) oh + sizeof (struct ospf6_header));
  303. if (on->state < OSPF6_NEIGHBOR_INIT)
  304. {
  305. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  306. zlog_debug ("Neighbor state less than Init, ignore");
  307. return;
  308. }
  309. switch (on->state)
  310. {
  311. case OSPF6_NEIGHBOR_TWOWAY:
  312. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  313. zlog_debug ("Neighbor state is 2-Way, ignore");
  314. return;
  315. case OSPF6_NEIGHBOR_INIT:
  316. thread_execute (master, twoway_received, on, 0);
  317. if (on->state != OSPF6_NEIGHBOR_EXSTART)
  318. {
  319. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  320. zlog_debug ("Neighbor state is not ExStart, ignore");
  321. return;
  322. }
  323. /* else fall through to ExStart */
  324. case OSPF6_NEIGHBOR_EXSTART:
  325. /* if neighbor obeys us as our slave, schedule negotiation_done
  326. and process LSA Headers. Otherwise, ignore this message */
  327. if (! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT) &&
  328. ! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT) &&
  329. ntohl (dbdesc->seqnum) == on->dbdesc_seqnum)
  330. {
  331. /* execute NegotiationDone */
  332. thread_execute (master, negotiation_done, on, 0);
  333. /* Record neighbor options */
  334. memcpy (on->options, dbdesc->options, sizeof (on->options));
  335. }
  336. else
  337. {
  338. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  339. zlog_debug ("Negotiation failed");
  340. return;
  341. }
  342. /* fall through to exchange */
  343. case OSPF6_NEIGHBOR_EXCHANGE:
  344. if (! memcmp (dbdesc, &on->dbdesc_last, sizeof (struct ospf6_dbdesc)))
  345. {
  346. /* Duplicated DatabaseDescription is dropped by master */
  347. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  348. zlog_debug ("Duplicated dbdesc discarded by Master, ignore");
  349. return;
  350. }
  351. if (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT))
  352. {
  353. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  354. zlog_debug ("Master/Slave bit mismatch");
  355. thread_add_event (master, seqnumber_mismatch, on, 0);
  356. return;
  357. }
  358. if (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT))
  359. {
  360. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  361. zlog_debug ("Initialize bit mismatch");
  362. thread_add_event (master, seqnumber_mismatch, on, 0);
  363. return;
  364. }
  365. if (memcmp (on->options, dbdesc->options, sizeof (on->options)))
  366. {
  367. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  368. zlog_debug ("Option field mismatch");
  369. thread_add_event (master, seqnumber_mismatch, on, 0);
  370. return;
  371. }
  372. if (ntohl (dbdesc->seqnum) != on->dbdesc_seqnum)
  373. {
  374. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  375. zlog_debug ("Sequence number mismatch (%#lx expected)",
  376. (u_long) on->dbdesc_seqnum);
  377. thread_add_event (master, seqnumber_mismatch, on, 0);
  378. return;
  379. }
  380. break;
  381. case OSPF6_NEIGHBOR_LOADING:
  382. case OSPF6_NEIGHBOR_FULL:
  383. if (! memcmp (dbdesc, &on->dbdesc_last, sizeof (struct ospf6_dbdesc)))
  384. {
  385. /* Duplicated DatabaseDescription is dropped by master */
  386. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  387. zlog_debug ("Duplicated dbdesc discarded by Master, ignore");
  388. return;
  389. }
  390. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  391. zlog_debug ("Not duplicate dbdesc in state %s",
  392. ospf6_neighbor_state_str[on->state]);
  393. thread_add_event (master, seqnumber_mismatch, on, 0);
  394. return;
  395. default:
  396. assert (0);
  397. break;
  398. }
  399. /* Process LSA headers */
  400. for (p = (char *) ((caddr_t) dbdesc + sizeof (struct ospf6_dbdesc));
  401. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  402. p += sizeof (struct ospf6_lsa_header))
  403. {
  404. struct ospf6_lsa *his, *mine;
  405. struct ospf6_lsdb *lsdb = NULL;
  406. his = ospf6_lsa_create_headeronly ((struct ospf6_lsa_header *) p);
  407. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  408. zlog_debug ("%s", his->name);
  409. switch (OSPF6_LSA_SCOPE (his->header->type))
  410. {
  411. case OSPF6_SCOPE_LINKLOCAL:
  412. lsdb = on->ospf6_if->lsdb;
  413. break;
  414. case OSPF6_SCOPE_AREA:
  415. lsdb = on->ospf6_if->area->lsdb;
  416. break;
  417. case OSPF6_SCOPE_AS:
  418. lsdb = on->ospf6_if->area->ospf6->lsdb;
  419. break;
  420. case OSPF6_SCOPE_RESERVED:
  421. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  422. zlog_debug ("Ignoring LSA of reserved scope");
  423. ospf6_lsa_delete (his);
  424. continue;
  425. break;
  426. }
  427. if (ntohs (his->header->type) == OSPF6_LSTYPE_AS_EXTERNAL &&
  428. IS_AREA_STUB (on->ospf6_if->area))
  429. {
  430. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  431. zlog_debug ("SeqNumMismatch (E-bit mismatch), discard");
  432. ospf6_lsa_delete (his);
  433. thread_add_event (master, seqnumber_mismatch, on, 0);
  434. return;
  435. }
  436. mine = ospf6_lsdb_lookup (his->header->type, his->header->id,
  437. his->header->adv_router, lsdb);
  438. if (mine == NULL)
  439. {
  440. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  441. zlog_debug ("Add request (No database copy)");
  442. ospf6_lsdb_add (ospf6_lsa_copy(his), on->request_list);
  443. }
  444. else if (ospf6_lsa_compare (his, mine) < 0)
  445. {
  446. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  447. zlog_debug ("Add request (Received MoreRecent)");
  448. ospf6_lsdb_add (ospf6_lsa_copy(his), on->request_list);
  449. }
  450. else
  451. {
  452. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  453. zlog_debug ("Discard (Existing MoreRecent)");
  454. }
  455. ospf6_lsa_delete (his);
  456. }
  457. assert (p == OSPF6_MESSAGE_END (oh));
  458. /* Increment sequence number */
  459. on->dbdesc_seqnum ++;
  460. /* schedule send lsreq */
  461. if (on->request_list->count && (on->thread_send_lsreq == NULL))
  462. on->thread_send_lsreq =
  463. thread_add_event (master, ospf6_lsreq_send, on, 0);
  464. THREAD_OFF (on->thread_send_dbdesc);
  465. /* More bit check */
  466. if (! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MBIT) &&
  467. ! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT))
  468. thread_add_event (master, exchange_done, on, 0);
  469. else
  470. on->thread_send_dbdesc =
  471. thread_add_event (master, ospf6_dbdesc_send_newone, on, 0);
  472. /* save last received dbdesc */
  473. memcpy (&on->dbdesc_last, dbdesc, sizeof (struct ospf6_dbdesc));
  474. }
  475. static void
  476. ospf6_dbdesc_recv_slave (struct ospf6_header *oh,
  477. struct ospf6_neighbor *on)
  478. {
  479. struct ospf6_dbdesc *dbdesc;
  480. char *p;
  481. dbdesc = (struct ospf6_dbdesc *)
  482. ((caddr_t) oh + sizeof (struct ospf6_header));
  483. if (on->state < OSPF6_NEIGHBOR_INIT)
  484. {
  485. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  486. zlog_debug ("Neighbor state less than Init, ignore");
  487. return;
  488. }
  489. switch (on->state)
  490. {
  491. case OSPF6_NEIGHBOR_TWOWAY:
  492. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  493. zlog_debug ("Neighbor state is 2-Way, ignore");
  494. return;
  495. case OSPF6_NEIGHBOR_INIT:
  496. thread_execute (master, twoway_received, on, 0);
  497. if (on->state != OSPF6_NEIGHBOR_EXSTART)
  498. {
  499. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  500. zlog_debug ("Neighbor state is not ExStart, ignore");
  501. return;
  502. }
  503. /* else fall through to ExStart */
  504. case OSPF6_NEIGHBOR_EXSTART:
  505. /* If the neighbor is Master, act as Slave. Schedule negotiation_done
  506. and process LSA Headers. Otherwise, ignore this message */
  507. if (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT) &&
  508. CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MBIT) &&
  509. CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT) &&
  510. ntohs (oh->length) == sizeof (struct ospf6_header) +
  511. sizeof (struct ospf6_dbdesc))
  512. {
  513. /* set the master/slave bit to slave */
  514. UNSET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT);
  515. /* set the DD sequence number to one specified by master */
  516. on->dbdesc_seqnum = ntohl (dbdesc->seqnum);
  517. /* schedule NegotiationDone */
  518. thread_execute (master, negotiation_done, on, 0);
  519. /* Record neighbor options */
  520. memcpy (on->options, dbdesc->options, sizeof (on->options));
  521. }
  522. else
  523. {
  524. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  525. zlog_debug ("Negotiation failed");
  526. return;
  527. }
  528. break;
  529. case OSPF6_NEIGHBOR_EXCHANGE:
  530. if (! memcmp (dbdesc, &on->dbdesc_last, sizeof (struct ospf6_dbdesc)))
  531. {
  532. /* Duplicated DatabaseDescription causes slave to retransmit */
  533. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  534. zlog_debug ("Duplicated dbdesc causes retransmit");
  535. THREAD_OFF (on->thread_send_dbdesc);
  536. on->thread_send_dbdesc =
  537. thread_add_event (master, ospf6_dbdesc_send, on, 0);
  538. return;
  539. }
  540. if (! CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_MSBIT))
  541. {
  542. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  543. zlog_debug ("Master/Slave bit mismatch");
  544. thread_add_event (master, seqnumber_mismatch, on, 0);
  545. return;
  546. }
  547. if (CHECK_FLAG (dbdesc->bits, OSPF6_DBDESC_IBIT))
  548. {
  549. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  550. zlog_debug ("Initialize bit mismatch");
  551. thread_add_event (master, seqnumber_mismatch, on, 0);
  552. return;
  553. }
  554. if (memcmp (on->options, dbdesc->options, sizeof (on->options)))
  555. {
  556. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  557. zlog_debug ("Option field mismatch");
  558. thread_add_event (master, seqnumber_mismatch, on, 0);
  559. return;
  560. }
  561. if (ntohl (dbdesc->seqnum) != on->dbdesc_seqnum + 1)
  562. {
  563. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  564. zlog_debug ("Sequence number mismatch (%#lx expected)",
  565. (u_long) on->dbdesc_seqnum + 1);
  566. thread_add_event (master, seqnumber_mismatch, on, 0);
  567. return;
  568. }
  569. break;
  570. case OSPF6_NEIGHBOR_LOADING:
  571. case OSPF6_NEIGHBOR_FULL:
  572. if (! memcmp (dbdesc, &on->dbdesc_last, sizeof (struct ospf6_dbdesc)))
  573. {
  574. /* Duplicated DatabaseDescription causes slave to retransmit */
  575. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  576. zlog_debug ("Duplicated dbdesc causes retransmit");
  577. THREAD_OFF (on->thread_send_dbdesc);
  578. on->thread_send_dbdesc =
  579. thread_add_event (master, ospf6_dbdesc_send, on, 0);
  580. return;
  581. }
  582. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  583. zlog_debug ("Not duplicate dbdesc in state %s",
  584. ospf6_neighbor_state_str[on->state]);
  585. thread_add_event (master, seqnumber_mismatch, on, 0);
  586. return;
  587. default:
  588. assert (0);
  589. break;
  590. }
  591. /* Process LSA headers */
  592. for (p = (char *) ((caddr_t) dbdesc + sizeof (struct ospf6_dbdesc));
  593. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  594. p += sizeof (struct ospf6_lsa_header))
  595. {
  596. struct ospf6_lsa *his, *mine;
  597. struct ospf6_lsdb *lsdb = NULL;
  598. his = ospf6_lsa_create_headeronly ((struct ospf6_lsa_header *) p);
  599. switch (OSPF6_LSA_SCOPE (his->header->type))
  600. {
  601. case OSPF6_SCOPE_LINKLOCAL:
  602. lsdb = on->ospf6_if->lsdb;
  603. break;
  604. case OSPF6_SCOPE_AREA:
  605. lsdb = on->ospf6_if->area->lsdb;
  606. break;
  607. case OSPF6_SCOPE_AS:
  608. lsdb = on->ospf6_if->area->ospf6->lsdb;
  609. break;
  610. case OSPF6_SCOPE_RESERVED:
  611. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  612. zlog_debug ("Ignoring LSA of reserved scope");
  613. ospf6_lsa_delete (his);
  614. continue;
  615. break;
  616. }
  617. if (OSPF6_LSA_SCOPE (his->header->type) == OSPF6_SCOPE_AS &&
  618. IS_AREA_STUB (on->ospf6_if->area))
  619. {
  620. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  621. zlog_debug ("E-bit mismatch with LSA Headers");
  622. ospf6_lsa_delete (his);
  623. thread_add_event (master, seqnumber_mismatch, on, 0);
  624. return;
  625. }
  626. mine = ospf6_lsdb_lookup (his->header->type, his->header->id,
  627. his->header->adv_router, lsdb);
  628. if (mine == NULL || ospf6_lsa_compare (his, mine) < 0)
  629. {
  630. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  631. zlog_debug ("Add request-list: %s", his->name);
  632. ospf6_lsdb_add (ospf6_lsa_copy(his), on->request_list);
  633. }
  634. ospf6_lsa_delete (his);
  635. }
  636. assert (p == OSPF6_MESSAGE_END (oh));
  637. /* Set sequence number to Master's */
  638. on->dbdesc_seqnum = ntohl (dbdesc->seqnum);
  639. /* schedule send lsreq */
  640. if ((on->thread_send_lsreq == NULL) &&
  641. (on->request_list->count))
  642. on->thread_send_lsreq =
  643. thread_add_event (master, ospf6_lsreq_send, on, 0);
  644. THREAD_OFF (on->thread_send_dbdesc);
  645. on->thread_send_dbdesc =
  646. thread_add_event (master, ospf6_dbdesc_send_newone, on, 0);
  647. /* save last received dbdesc */
  648. memcpy (&on->dbdesc_last, dbdesc, sizeof (struct ospf6_dbdesc));
  649. }
  650. static void
  651. ospf6_dbdesc_recv (struct in6_addr *src, struct in6_addr *dst,
  652. struct ospf6_interface *oi, struct ospf6_header *oh)
  653. {
  654. struct ospf6_neighbor *on;
  655. struct ospf6_dbdesc *dbdesc;
  656. on = ospf6_neighbor_lookup (oh->router_id, oi);
  657. if (on == NULL)
  658. {
  659. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  660. zlog_debug ("Neighbor not found, ignore");
  661. return;
  662. }
  663. dbdesc = (struct ospf6_dbdesc *)
  664. ((caddr_t) oh + sizeof (struct ospf6_header));
  665. /* Interface MTU check */
  666. if (!oi->mtu_ignore && ntohs (dbdesc->ifmtu) != oi->ifmtu)
  667. {
  668. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  669. zlog_debug ("I/F MTU mismatch");
  670. return;
  671. }
  672. if (dbdesc->reserved1 || dbdesc->reserved2)
  673. {
  674. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  675. zlog_debug ("Non-0 reserved field in %s's DbDesc, correct",
  676. on->name);
  677. dbdesc->reserved1 = 0;
  678. dbdesc->reserved2 = 0;
  679. }
  680. if (ntohl (oh->router_id) < ntohl (ospf6->router_id))
  681. ospf6_dbdesc_recv_master (oh, on);
  682. else if (ntohl (ospf6->router_id) < ntohl (oh->router_id))
  683. ospf6_dbdesc_recv_slave (oh, on);
  684. else
  685. {
  686. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  687. zlog_debug ("Can't decide which is master, ignore");
  688. }
  689. }
  690. static void
  691. ospf6_lsreq_recv (struct in6_addr *src, struct in6_addr *dst,
  692. struct ospf6_interface *oi, struct ospf6_header *oh)
  693. {
  694. struct ospf6_neighbor *on;
  695. char *p;
  696. struct ospf6_lsreq_entry *e;
  697. struct ospf6_lsdb *lsdb = NULL;
  698. struct ospf6_lsa *lsa;
  699. on = ospf6_neighbor_lookup (oh->router_id, oi);
  700. if (on == NULL)
  701. {
  702. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  703. zlog_debug ("Neighbor not found, ignore");
  704. return;
  705. }
  706. if (on->state != OSPF6_NEIGHBOR_EXCHANGE &&
  707. on->state != OSPF6_NEIGHBOR_LOADING &&
  708. on->state != OSPF6_NEIGHBOR_FULL)
  709. {
  710. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  711. zlog_debug ("Neighbor state less than Exchange, ignore");
  712. return;
  713. }
  714. /* Process each request */
  715. for (p = (char *) ((caddr_t) oh + sizeof (struct ospf6_header));
  716. p + sizeof (struct ospf6_lsreq_entry) <= OSPF6_MESSAGE_END (oh);
  717. p += sizeof (struct ospf6_lsreq_entry))
  718. {
  719. e = (struct ospf6_lsreq_entry *) p;
  720. switch (OSPF6_LSA_SCOPE (e->type))
  721. {
  722. case OSPF6_SCOPE_LINKLOCAL:
  723. lsdb = on->ospf6_if->lsdb;
  724. break;
  725. case OSPF6_SCOPE_AREA:
  726. lsdb = on->ospf6_if->area->lsdb;
  727. break;
  728. case OSPF6_SCOPE_AS:
  729. lsdb = on->ospf6_if->area->ospf6->lsdb;
  730. break;
  731. default:
  732. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  733. zlog_debug ("Ignoring LSA of reserved scope");
  734. continue;
  735. break;
  736. }
  737. /* Find database copy */
  738. lsa = ospf6_lsdb_lookup (e->type, e->id, e->adv_router, lsdb);
  739. if (lsa == NULL)
  740. {
  741. char id[16], adv_router[16];
  742. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  743. {
  744. inet_ntop (AF_INET, &e->id, id, sizeof (id));
  745. inet_ntop (AF_INET, &e->adv_router, adv_router,
  746. sizeof (adv_router));
  747. zlog_debug ("Can't find requested [%s Id:%s Adv:%s]",
  748. ospf6_lstype_name (e->type), id, adv_router);
  749. }
  750. thread_add_event (master, bad_lsreq, on, 0);
  751. return;
  752. }
  753. ospf6_lsdb_add (ospf6_lsa_copy (lsa), on->lsupdate_list);
  754. }
  755. assert (p == OSPF6_MESSAGE_END (oh));
  756. /* schedule send lsupdate */
  757. THREAD_OFF (on->thread_send_lsupdate);
  758. on->thread_send_lsupdate =
  759. thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0);
  760. }
  761. /* Verify, that the specified memory area contains exactly N valid IPv6
  762. prefixes as specified by RFC5340, A.4.1. */
  763. static unsigned
  764. ospf6_prefixes_examin
  765. (
  766. struct ospf6_prefix *current, /* start of buffer */
  767. unsigned length,
  768. const u_int32_t req_num_pfxs /* always compared with the actual number of prefixes */
  769. )
  770. {
  771. u_char requested_pfx_bytes;
  772. u_int32_t real_num_pfxs = 0;
  773. while (length)
  774. {
  775. if (length < OSPF6_PREFIX_MIN_SIZE)
  776. {
  777. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  778. zlog_debug ("%s: undersized IPv6 prefix header", __func__);
  779. return MSG_NG;
  780. }
  781. /* safe to look deeper */
  782. if (current->prefix_length > IPV6_MAX_BITLEN)
  783. {
  784. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  785. zlog_debug ("%s: invalid PrefixLength (%u bits)", __func__, current->prefix_length);
  786. return MSG_NG;
  787. }
  788. /* covers both fixed- and variable-sized fields */
  789. requested_pfx_bytes = OSPF6_PREFIX_MIN_SIZE + OSPF6_PREFIX_SPACE (current->prefix_length);
  790. if (requested_pfx_bytes > length)
  791. {
  792. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  793. zlog_debug ("%s: undersized IPv6 prefix", __func__);
  794. return MSG_NG;
  795. }
  796. /* next prefix */
  797. length -= requested_pfx_bytes;
  798. current = (struct ospf6_prefix *) ((caddr_t) current + requested_pfx_bytes);
  799. real_num_pfxs++;
  800. }
  801. if (real_num_pfxs != req_num_pfxs)
  802. {
  803. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  804. zlog_debug ("%s: IPv6 prefix number mismatch (%u required, %u real)",
  805. __func__, req_num_pfxs, real_num_pfxs);
  806. return MSG_NG;
  807. }
  808. return MSG_OK;
  809. }
  810. /* Verify an LSA to have a valid length and dispatch further (where
  811. appropriate) to check if the contents, including nested IPv6 prefixes,
  812. is properly sized/aligned within the LSA. Note that this function gets
  813. LSA type in network byte order, uses in host byte order and passes to
  814. ospf6_lstype_name() in network byte order again. */
  815. static unsigned
  816. ospf6_lsa_examin (struct ospf6_lsa_header *lsah, const u_int16_t lsalen, const u_char headeronly)
  817. {
  818. struct ospf6_intra_prefix_lsa *intra_prefix_lsa;
  819. struct ospf6_as_external_lsa *as_external_lsa;
  820. struct ospf6_link_lsa *link_lsa;
  821. unsigned exp_length;
  822. u_int8_t ltindex;
  823. u_int16_t lsatype;
  824. /* In case an additional minimum length constraint is defined for current
  825. LSA type, make sure that this constraint is met. */
  826. lsatype = ntohs (lsah->type);
  827. ltindex = lsatype & OSPF6_LSTYPE_FCODE_MASK;
  828. if
  829. (
  830. ltindex < OSPF6_LSTYPE_SIZE &&
  831. ospf6_lsa_minlen[ltindex] &&
  832. lsalen < ospf6_lsa_minlen[ltindex] + OSPF6_LSA_HEADER_SIZE
  833. )
  834. {
  835. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  836. zlog_debug ("%s: undersized (%u B) LSA", __func__, lsalen);
  837. return MSG_NG;
  838. }
  839. switch (lsatype)
  840. {
  841. case OSPF6_LSTYPE_ROUTER:
  842. /* RFC5340 A.4.3, LSA header + OSPF6_ROUTER_LSA_MIN_SIZE bytes followed
  843. by N>=0 interface descriptions. */
  844. if ((lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_ROUTER_LSA_MIN_SIZE) % OSPF6_ROUTER_LSDESC_FIX_SIZE)
  845. {
  846. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  847. zlog_debug ("%s: interface description alignment error", __func__);
  848. return MSG_NG;
  849. }
  850. break;
  851. case OSPF6_LSTYPE_NETWORK:
  852. /* RFC5340 A.4.4, LSA header + OSPF6_NETWORK_LSA_MIN_SIZE bytes
  853. followed by N>=0 attached router descriptions. */
  854. if ((lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_NETWORK_LSA_MIN_SIZE) % OSPF6_NETWORK_LSDESC_FIX_SIZE)
  855. {
  856. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  857. zlog_debug ("%s: router description alignment error", __func__);
  858. return MSG_NG;
  859. }
  860. break;
  861. case OSPF6_LSTYPE_INTER_PREFIX:
  862. /* RFC5340 A.4.5, LSA header + OSPF6_INTER_PREFIX_LSA_MIN_SIZE bytes
  863. followed by 3-4 fields of a single IPv6 prefix. */
  864. if (headeronly)
  865. break;
  866. return ospf6_prefixes_examin
  867. (
  868. (struct ospf6_prefix *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE + OSPF6_INTER_PREFIX_LSA_MIN_SIZE),
  869. lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_INTER_PREFIX_LSA_MIN_SIZE,
  870. 1
  871. );
  872. case OSPF6_LSTYPE_INTER_ROUTER:
  873. /* RFC5340 A.4.6, fixed-size LSA. */
  874. if (lsalen > OSPF6_LSA_HEADER_SIZE + OSPF6_INTER_ROUTER_LSA_FIX_SIZE)
  875. {
  876. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  877. zlog_debug ("%s: oversized (%u B) LSA", __func__, lsalen);
  878. return MSG_NG;
  879. }
  880. break;
  881. case OSPF6_LSTYPE_AS_EXTERNAL: /* RFC5340 A.4.7, same as A.4.8. */
  882. case OSPF6_LSTYPE_TYPE_7:
  883. /* RFC5340 A.4.8, LSA header + OSPF6_AS_EXTERNAL_LSA_MIN_SIZE bytes
  884. followed by 3-4 fields of IPv6 prefix and 3 conditional LSA fields:
  885. 16 bytes of forwarding address, 4 bytes of external route tag,
  886. 4 bytes of referenced link state ID. */
  887. if (headeronly)
  888. break;
  889. as_external_lsa = (struct ospf6_as_external_lsa *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE);
  890. exp_length = OSPF6_LSA_HEADER_SIZE + OSPF6_AS_EXTERNAL_LSA_MIN_SIZE;
  891. /* To find out if the last optional field (Referenced Link State ID) is
  892. assumed in this LSA, we need to access fixed fields of the IPv6
  893. prefix before ospf6_prefix_examin() confirms its sizing. */
  894. if (exp_length + OSPF6_PREFIX_MIN_SIZE > lsalen)
  895. {
  896. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  897. zlog_debug ("%s: undersized (%u B) LSA header", __func__, lsalen);
  898. return MSG_NG;
  899. }
  900. /* forwarding address */
  901. if (CHECK_FLAG (as_external_lsa->bits_metric, OSPF6_ASBR_BIT_F))
  902. exp_length += 16;
  903. /* external route tag */
  904. if (CHECK_FLAG (as_external_lsa->bits_metric, OSPF6_ASBR_BIT_T))
  905. exp_length += 4;
  906. /* referenced link state ID */
  907. if (as_external_lsa->prefix.u._prefix_referenced_lstype)
  908. exp_length += 4;
  909. /* All the fixed-size fields (mandatory and optional) must fit. I.e.,
  910. this check does not include any IPv6 prefix fields. */
  911. if (exp_length > lsalen)
  912. {
  913. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  914. zlog_debug ("%s: undersized (%u B) LSA header", __func__, lsalen);
  915. return MSG_NG;
  916. }
  917. /* The last call completely covers the remainder (IPv6 prefix). */
  918. return ospf6_prefixes_examin
  919. (
  920. (struct ospf6_prefix *) ((caddr_t) as_external_lsa + OSPF6_AS_EXTERNAL_LSA_MIN_SIZE),
  921. lsalen - exp_length,
  922. 1
  923. );
  924. case OSPF6_LSTYPE_LINK:
  925. /* RFC5340 A.4.9, LSA header + OSPF6_LINK_LSA_MIN_SIZE bytes followed
  926. by N>=0 IPv6 prefix blocks (with N declared beforehand). */
  927. if (headeronly)
  928. break;
  929. link_lsa = (struct ospf6_link_lsa *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE);
  930. return ospf6_prefixes_examin
  931. (
  932. (struct ospf6_prefix *) ((caddr_t) link_lsa + OSPF6_LINK_LSA_MIN_SIZE),
  933. lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_LINK_LSA_MIN_SIZE,
  934. ntohl (link_lsa->prefix_num) /* 32 bits */
  935. );
  936. case OSPF6_LSTYPE_INTRA_PREFIX:
  937. /* RFC5340 A.4.10, LSA header + OSPF6_INTRA_PREFIX_LSA_MIN_SIZE bytes
  938. followed by N>=0 IPv6 prefixes (with N declared beforehand). */
  939. if (headeronly)
  940. break;
  941. intra_prefix_lsa = (struct ospf6_intra_prefix_lsa *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE);
  942. return ospf6_prefixes_examin
  943. (
  944. (struct ospf6_prefix *) ((caddr_t) intra_prefix_lsa + OSPF6_INTRA_PREFIX_LSA_MIN_SIZE),
  945. lsalen - OSPF6_LSA_HEADER_SIZE - OSPF6_INTRA_PREFIX_LSA_MIN_SIZE,
  946. ntohs (intra_prefix_lsa->prefix_num) /* 16 bits */
  947. );
  948. }
  949. /* No additional validation is possible for unknown LSA types, which are
  950. themselves valid in OPSFv3, hence the default decision is to accept. */
  951. return MSG_OK;
  952. }
  953. /* Verify if the provided input buffer is a valid sequence of LSAs. This
  954. includes verification of LSA blocks length/alignment and dispatching
  955. of deeper-level checks. */
  956. static unsigned
  957. ospf6_lsaseq_examin
  958. (
  959. struct ospf6_lsa_header *lsah, /* start of buffered data */
  960. size_t length,
  961. const u_char headeronly,
  962. /* When declared_num_lsas is not 0, compare it to the real number of LSAs
  963. and treat the difference as an error. */
  964. const u_int32_t declared_num_lsas
  965. )
  966. {
  967. u_int32_t counted_lsas = 0;
  968. while (length)
  969. {
  970. u_int16_t lsalen;
  971. if (length < OSPF6_LSA_HEADER_SIZE)
  972. {
  973. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  974. zlog_debug ("%s: undersized (%zu B) trailing (#%u) LSA header",
  975. __func__, length, counted_lsas);
  976. return MSG_NG;
  977. }
  978. /* save on ntohs() calls here and in the LSA validator */
  979. lsalen = OSPF6_LSA_SIZE (lsah);
  980. if (lsalen < OSPF6_LSA_HEADER_SIZE)
  981. {
  982. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  983. zlog_debug ("%s: malformed LSA header #%u, declared length is %u B",
  984. __func__, counted_lsas, lsalen);
  985. return MSG_NG;
  986. }
  987. if (headeronly)
  988. {
  989. /* less checks here and in ospf6_lsa_examin() */
  990. if (MSG_OK != ospf6_lsa_examin (lsah, lsalen, 1))
  991. {
  992. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  993. zlog_debug ("%s: anomaly in header-only %s LSA #%u", __func__,
  994. ospf6_lstype_name (lsah->type), counted_lsas);
  995. return MSG_NG;
  996. }
  997. lsah = (struct ospf6_lsa_header *) ((caddr_t) lsah + OSPF6_LSA_HEADER_SIZE);
  998. length -= OSPF6_LSA_HEADER_SIZE;
  999. }
  1000. else
  1001. {
  1002. /* make sure the input buffer is deep enough before further checks */
  1003. if (lsalen > length)
  1004. {
  1005. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1006. zlog_debug ("%s: anomaly in %s LSA #%u: declared length is %u B, buffered length is %zu B",
  1007. __func__, ospf6_lstype_name (lsah->type), counted_lsas, lsalen, length);
  1008. return MSG_NG;
  1009. }
  1010. if (MSG_OK != ospf6_lsa_examin (lsah, lsalen, 0))
  1011. {
  1012. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1013. zlog_debug ("%s: anomaly in %s LSA #%u", __func__,
  1014. ospf6_lstype_name (lsah->type), counted_lsas);
  1015. return MSG_NG;
  1016. }
  1017. lsah = (struct ospf6_lsa_header *) ((caddr_t) lsah + lsalen);
  1018. length -= lsalen;
  1019. }
  1020. counted_lsas++;
  1021. }
  1022. if (declared_num_lsas && counted_lsas != declared_num_lsas)
  1023. {
  1024. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1025. zlog_debug ("%s: #LSAs declared (%u) does not match actual (%u)",
  1026. __func__, declared_num_lsas, counted_lsas);
  1027. return MSG_NG;
  1028. }
  1029. return MSG_OK;
  1030. }
  1031. /* Verify a complete OSPF packet for proper sizing/alignment. */
  1032. static unsigned
  1033. ospf6_packet_examin (struct ospf6_header *oh, const unsigned bytesonwire)
  1034. {
  1035. struct ospf6_lsupdate *lsupd;
  1036. unsigned test;
  1037. /* length, 1st approximation */
  1038. if (bytesonwire < OSPF6_HEADER_SIZE)
  1039. {
  1040. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1041. zlog_debug ("%s: undersized (%u B) packet", __func__, bytesonwire);
  1042. return MSG_NG;
  1043. }
  1044. /* Now it is safe to access header fields. */
  1045. if (bytesonwire != ntohs (oh->length))
  1046. {
  1047. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1048. zlog_debug ("%s: packet length error (%u real, %u declared)",
  1049. __func__, bytesonwire, ntohs (oh->length));
  1050. return MSG_NG;
  1051. }
  1052. /* version check */
  1053. if (oh->version != OSPFV3_VERSION)
  1054. {
  1055. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1056. zlog_debug ("%s: invalid (%u) protocol version", __func__, oh->version);
  1057. return MSG_NG;
  1058. }
  1059. /* length, 2nd approximation */
  1060. if
  1061. (
  1062. oh->type < OSPF6_MESSAGE_TYPE_ALL &&
  1063. ospf6_packet_minlen[oh->type] &&
  1064. bytesonwire < OSPF6_HEADER_SIZE + ospf6_packet_minlen[oh->type]
  1065. )
  1066. {
  1067. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1068. zlog_debug ("%s: undersized (%u B) %s packet", __func__,
  1069. bytesonwire, LOOKUP (ospf6_message_type_str, oh->type));
  1070. return MSG_NG;
  1071. }
  1072. /* type-specific deeper validation */
  1073. switch (oh->type)
  1074. {
  1075. case OSPF6_MESSAGE_TYPE_HELLO:
  1076. /* RFC5340 A.3.2, packet header + OSPF6_HELLO_MIN_SIZE bytes followed
  1077. by N>=0 router-IDs. */
  1078. if (0 == (bytesonwire - OSPF6_HEADER_SIZE - OSPF6_HELLO_MIN_SIZE) % 4)
  1079. return MSG_OK;
  1080. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1081. zlog_debug ("%s: alignment error in %s packet",
  1082. __func__, LOOKUP (ospf6_message_type_str, oh->type));
  1083. return MSG_NG;
  1084. case OSPF6_MESSAGE_TYPE_DBDESC:
  1085. /* RFC5340 A.3.3, packet header + OSPF6_DB_DESC_MIN_SIZE bytes followed
  1086. by N>=0 header-only LSAs. */
  1087. test = ospf6_lsaseq_examin
  1088. (
  1089. (struct ospf6_lsa_header *) ((caddr_t) oh + OSPF6_HEADER_SIZE + OSPF6_DB_DESC_MIN_SIZE),
  1090. bytesonwire - OSPF6_HEADER_SIZE - OSPF6_DB_DESC_MIN_SIZE,
  1091. 1,
  1092. 0
  1093. );
  1094. break;
  1095. case OSPF6_MESSAGE_TYPE_LSREQ:
  1096. /* RFC5340 A.3.4, packet header + N>=0 LS description blocks. */
  1097. if (0 == (bytesonwire - OSPF6_HEADER_SIZE - OSPF6_LS_REQ_MIN_SIZE) % OSPF6_LSREQ_LSDESC_FIX_SIZE)
  1098. return MSG_OK;
  1099. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1100. zlog_debug ("%s: alignment error in %s packet",
  1101. __func__, LOOKUP (ospf6_message_type_str, oh->type));
  1102. return MSG_NG;
  1103. case OSPF6_MESSAGE_TYPE_LSUPDATE:
  1104. /* RFC5340 A.3.5, packet header + OSPF6_LS_UPD_MIN_SIZE bytes followed
  1105. by N>=0 full LSAs (with N declared beforehand). */
  1106. lsupd = (struct ospf6_lsupdate *) ((caddr_t) oh + OSPF6_HEADER_SIZE);
  1107. test = ospf6_lsaseq_examin
  1108. (
  1109. (struct ospf6_lsa_header *) ((caddr_t) lsupd + OSPF6_LS_UPD_MIN_SIZE),
  1110. bytesonwire - OSPF6_HEADER_SIZE - OSPF6_LS_UPD_MIN_SIZE,
  1111. 0,
  1112. ntohl (lsupd->lsa_number) /* 32 bits */
  1113. );
  1114. break;
  1115. case OSPF6_MESSAGE_TYPE_LSACK:
  1116. /* RFC5340 A.3.6, packet header + N>=0 header-only LSAs. */
  1117. test = ospf6_lsaseq_examin
  1118. (
  1119. (struct ospf6_lsa_header *) ((caddr_t) oh + OSPF6_HEADER_SIZE + OSPF6_LS_ACK_MIN_SIZE),
  1120. bytesonwire - OSPF6_HEADER_SIZE - OSPF6_LS_ACK_MIN_SIZE,
  1121. 1,
  1122. 0
  1123. );
  1124. break;
  1125. default:
  1126. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1127. zlog_debug ("%s: invalid (%u) message type", __func__, oh->type);
  1128. return MSG_NG;
  1129. }
  1130. if (test != MSG_OK && IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1131. zlog_debug ("%s: anomaly in %s packet", __func__, LOOKUP (ospf6_message_type_str, oh->type));
  1132. return test;
  1133. }
  1134. /* Verify particular fields of otherwise correct received OSPF packet to
  1135. meet the requirements of RFC. */
  1136. static int
  1137. ospf6_rxpacket_examin (struct ospf6_interface *oi, struct ospf6_header *oh, const unsigned bytesonwire)
  1138. {
  1139. char buf[2][INET_ADDRSTRLEN];
  1140. if (MSG_OK != ospf6_packet_examin (oh, bytesonwire))
  1141. return MSG_NG;
  1142. /* Area-ID check */
  1143. if (oh->area_id != oi->area->area_id)
  1144. {
  1145. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1146. {
  1147. if (oh->area_id == OSPF_AREA_BACKBONE)
  1148. zlog_debug ("%s: Message may be via Virtual Link: not supported", __func__);
  1149. else
  1150. zlog_debug
  1151. (
  1152. "%s: Area-ID mismatch (my %s, rcvd %s)", __func__,
  1153. inet_ntop (AF_INET, &oi->area->area_id, buf[0], INET_ADDRSTRLEN),
  1154. inet_ntop (AF_INET, &oh->area_id, buf[1], INET_ADDRSTRLEN)
  1155. );
  1156. }
  1157. return MSG_NG;
  1158. }
  1159. /* Instance-ID check */
  1160. if (oh->instance_id != oi->instance_id)
  1161. {
  1162. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1163. zlog_debug ("%s: Instance-ID mismatch (my %u, rcvd %u)", __func__, oi->instance_id, oh->instance_id);
  1164. return MSG_NG;
  1165. }
  1166. /* Router-ID check */
  1167. if (oh->router_id == oi->area->ospf6->router_id)
  1168. {
  1169. zlog_warn ("%s: Duplicate Router-ID (%s)", __func__, inet_ntop (AF_INET, &oh->router_id, buf[0], INET_ADDRSTRLEN));
  1170. return MSG_NG;
  1171. }
  1172. return MSG_OK;
  1173. }
  1174. static void
  1175. ospf6_lsupdate_recv (struct in6_addr *src, struct in6_addr *dst,
  1176. struct ospf6_interface *oi, struct ospf6_header *oh)
  1177. {
  1178. struct ospf6_neighbor *on;
  1179. struct ospf6_lsupdate *lsupdate;
  1180. char *p;
  1181. on = ospf6_neighbor_lookup (oh->router_id, oi);
  1182. if (on == NULL)
  1183. {
  1184. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1185. zlog_debug ("Neighbor not found, ignore");
  1186. return;
  1187. }
  1188. if (on->state != OSPF6_NEIGHBOR_EXCHANGE &&
  1189. on->state != OSPF6_NEIGHBOR_LOADING &&
  1190. on->state != OSPF6_NEIGHBOR_FULL)
  1191. {
  1192. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1193. zlog_debug ("Neighbor state less than Exchange, ignore");
  1194. return;
  1195. }
  1196. lsupdate = (struct ospf6_lsupdate *)
  1197. ((caddr_t) oh + sizeof (struct ospf6_header));
  1198. /* Process LSAs */
  1199. for (p = (char *) ((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  1200. p < OSPF6_MESSAGE_END (oh) &&
  1201. p + OSPF6_LSA_SIZE (p) <= OSPF6_MESSAGE_END (oh);
  1202. p += OSPF6_LSA_SIZE (p))
  1203. {
  1204. ospf6_receive_lsa (on, (struct ospf6_lsa_header *) p);
  1205. }
  1206. assert (p == OSPF6_MESSAGE_END (oh));
  1207. }
  1208. static void
  1209. ospf6_lsack_recv (struct in6_addr *src, struct in6_addr *dst,
  1210. struct ospf6_interface *oi, struct ospf6_header *oh)
  1211. {
  1212. struct ospf6_neighbor *on;
  1213. char *p;
  1214. struct ospf6_lsa *his, *mine;
  1215. struct ospf6_lsdb *lsdb = NULL;
  1216. assert (oh->type == OSPF6_MESSAGE_TYPE_LSACK);
  1217. on = ospf6_neighbor_lookup (oh->router_id, oi);
  1218. if (on == NULL)
  1219. {
  1220. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1221. zlog_debug ("Neighbor not found, ignore");
  1222. return;
  1223. }
  1224. if (on->state != OSPF6_NEIGHBOR_EXCHANGE &&
  1225. on->state != OSPF6_NEIGHBOR_LOADING &&
  1226. on->state != OSPF6_NEIGHBOR_FULL)
  1227. {
  1228. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1229. zlog_debug ("Neighbor state less than Exchange, ignore");
  1230. return;
  1231. }
  1232. for (p = (char *) ((caddr_t) oh + sizeof (struct ospf6_header));
  1233. p + sizeof (struct ospf6_lsa_header) <= OSPF6_MESSAGE_END (oh);
  1234. p += sizeof (struct ospf6_lsa_header))
  1235. {
  1236. his = ospf6_lsa_create_headeronly ((struct ospf6_lsa_header *) p);
  1237. switch (OSPF6_LSA_SCOPE (his->header->type))
  1238. {
  1239. case OSPF6_SCOPE_LINKLOCAL:
  1240. lsdb = on->ospf6_if->lsdb;
  1241. break;
  1242. case OSPF6_SCOPE_AREA:
  1243. lsdb = on->ospf6_if->area->lsdb;
  1244. break;
  1245. case OSPF6_SCOPE_AS:
  1246. lsdb = on->ospf6_if->area->ospf6->lsdb;
  1247. break;
  1248. case OSPF6_SCOPE_RESERVED:
  1249. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1250. zlog_debug ("Ignoring LSA of reserved scope");
  1251. ospf6_lsa_delete (his);
  1252. continue;
  1253. break;
  1254. }
  1255. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1256. zlog_debug ("%s acknowledged by %s", his->name, on->name);
  1257. /* Find database copy */
  1258. mine = ospf6_lsdb_lookup (his->header->type, his->header->id,
  1259. his->header->adv_router, lsdb);
  1260. if (mine == NULL)
  1261. {
  1262. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1263. zlog_debug ("No database copy");
  1264. ospf6_lsa_delete (his);
  1265. continue;
  1266. }
  1267. /* Check if the LSA is on his retrans-list */
  1268. mine = ospf6_lsdb_lookup (his->header->type, his->header->id,
  1269. his->header->adv_router, on->retrans_list);
  1270. if (mine == NULL)
  1271. {
  1272. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1273. zlog_debug ("Not on %s's retrans-list", on->name);
  1274. ospf6_lsa_delete (his);
  1275. continue;
  1276. }
  1277. if (ospf6_lsa_compare (his, mine) != 0)
  1278. {
  1279. /* Log this questionable acknowledgement,
  1280. and examine the next one. */
  1281. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1282. zlog_debug ("Questionable acknowledgement");
  1283. ospf6_lsa_delete (his);
  1284. continue;
  1285. }
  1286. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1287. zlog_debug ("Acknowledged, remove from %s's retrans-list",
  1288. on->name);
  1289. ospf6_decrement_retrans_count (mine);
  1290. if (OSPF6_LSA_IS_MAXAGE (mine))
  1291. ospf6_maxage_remove (on->ospf6_if->area->ospf6);
  1292. ospf6_lsdb_remove (mine, on->retrans_list);
  1293. ospf6_lsa_delete (his);
  1294. }
  1295. assert (p == OSPF6_MESSAGE_END (oh));
  1296. }
  1297. static u_char *recvbuf = NULL;
  1298. static u_char *sendbuf = NULL;
  1299. static unsigned int iobuflen = 0;
  1300. int
  1301. ospf6_iobuf_size (unsigned int size)
  1302. {
  1303. u_char *recvnew, *sendnew;
  1304. if (size <= iobuflen)
  1305. return iobuflen;
  1306. recvnew = XMALLOC (MTYPE_OSPF6_MESSAGE, size);
  1307. sendnew = XMALLOC (MTYPE_OSPF6_MESSAGE, size);
  1308. if (recvnew == NULL || sendnew == NULL)
  1309. {
  1310. if (recvnew)
  1311. XFREE (MTYPE_OSPF6_MESSAGE, recvnew);
  1312. if (sendnew)
  1313. XFREE (MTYPE_OSPF6_MESSAGE, sendnew);
  1314. zlog_debug ("Could not allocate I/O buffer of size %d.", size);
  1315. return iobuflen;
  1316. }
  1317. if (recvbuf)
  1318. XFREE (MTYPE_OSPF6_MESSAGE, recvbuf);
  1319. if (sendbuf)
  1320. XFREE (MTYPE_OSPF6_MESSAGE, sendbuf);
  1321. recvbuf = recvnew;
  1322. sendbuf = sendnew;
  1323. iobuflen = size;
  1324. return iobuflen;
  1325. }
  1326. void
  1327. ospf6_message_terminate (void)
  1328. {
  1329. if (recvbuf)
  1330. {
  1331. XFREE (MTYPE_OSPF6_MESSAGE, recvbuf);
  1332. recvbuf = NULL;
  1333. }
  1334. if (sendbuf)
  1335. {
  1336. XFREE (MTYPE_OSPF6_MESSAGE, sendbuf);
  1337. sendbuf = NULL;
  1338. }
  1339. iobuflen = 0;
  1340. }
  1341. int
  1342. ospf6_receive (struct thread *thread)
  1343. {
  1344. int sockfd;
  1345. unsigned int len;
  1346. char srcname[64], dstname[64];
  1347. struct in6_addr src, dst;
  1348. ifindex_t ifindex;
  1349. struct iovec iovector[2];
  1350. struct ospf6_interface *oi;
  1351. struct ospf6_header *oh;
  1352. /* add next read thread */
  1353. sockfd = THREAD_FD (thread);
  1354. thread_add_read (master, ospf6_receive, NULL, sockfd);
  1355. /* initialize */
  1356. memset (&src, 0, sizeof (src));
  1357. memset (&dst, 0, sizeof (dst));
  1358. ifindex = 0;
  1359. memset (recvbuf, 0, iobuflen);
  1360. iovector[0].iov_base = recvbuf;
  1361. iovector[0].iov_len = iobuflen;
  1362. iovector[1].iov_base = NULL;
  1363. iovector[1].iov_len = 0;
  1364. /* receive message */
  1365. len = ospf6_recvmsg (&src, &dst, &ifindex, iovector);
  1366. if (len > iobuflen)
  1367. {
  1368. zlog_err ("Excess message read");
  1369. return 0;
  1370. }
  1371. oi = ospf6_interface_lookup_by_ifindex (ifindex);
  1372. if (oi == NULL || oi->area == NULL || CHECK_FLAG(oi->flag, OSPF6_INTERFACE_DISABLE))
  1373. {
  1374. zlog_debug ("Message received on disabled interface");
  1375. return 0;
  1376. }
  1377. if (CHECK_FLAG (oi->flag, OSPF6_INTERFACE_PASSIVE))
  1378. {
  1379. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  1380. zlog_debug ("%s: Ignore message on passive interface %s",
  1381. __func__, oi->interface->name);
  1382. return 0;
  1383. }
  1384. oh = (struct ospf6_header *) recvbuf;
  1385. if (ospf6_rxpacket_examin (oi, oh, len) != MSG_OK)
  1386. return 0;
  1387. /* Being here means, that no sizing/alignment issues were detected in
  1388. the input packet. This renders the additional checks performed below
  1389. and also in the type-specific dispatching functions a dead code,
  1390. which can be dismissed in a cleanup-focused review round later. */
  1391. /* Log */
  1392. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, RECV))
  1393. {
  1394. inet_ntop (AF_INET6, &src, srcname, sizeof (srcname));
  1395. inet_ntop (AF_INET6, &dst, dstname, sizeof (dstname));
  1396. zlog_debug ("%s received on %s",
  1397. LOOKUP (ospf6_message_type_str, oh->type), oi->interface->name);
  1398. zlog_debug (" src: %s", srcname);
  1399. zlog_debug (" dst: %s", dstname);
  1400. switch (oh->type)
  1401. {
  1402. case OSPF6_MESSAGE_TYPE_HELLO:
  1403. ospf6_hello_print (oh);
  1404. break;
  1405. case OSPF6_MESSAGE_TYPE_DBDESC:
  1406. ospf6_dbdesc_print (oh);
  1407. break;
  1408. case OSPF6_MESSAGE_TYPE_LSREQ:
  1409. ospf6_lsreq_print (oh);
  1410. break;
  1411. case OSPF6_MESSAGE_TYPE_LSUPDATE:
  1412. ospf6_lsupdate_print (oh);
  1413. break;
  1414. case OSPF6_MESSAGE_TYPE_LSACK:
  1415. ospf6_lsack_print (oh);
  1416. break;
  1417. default:
  1418. assert (0);
  1419. }
  1420. }
  1421. switch (oh->type)
  1422. {
  1423. case OSPF6_MESSAGE_TYPE_HELLO:
  1424. ospf6_hello_recv (&src, &dst, oi, oh);
  1425. break;
  1426. case OSPF6_MESSAGE_TYPE_DBDESC:
  1427. ospf6_dbdesc_recv (&src, &dst, oi, oh);
  1428. break;
  1429. case OSPF6_MESSAGE_TYPE_LSREQ:
  1430. ospf6_lsreq_recv (&src, &dst, oi, oh);
  1431. break;
  1432. case OSPF6_MESSAGE_TYPE_LSUPDATE:
  1433. ospf6_lsupdate_recv (&src, &dst, oi, oh);
  1434. break;
  1435. case OSPF6_MESSAGE_TYPE_LSACK:
  1436. ospf6_lsack_recv (&src, &dst, oi, oh);
  1437. break;
  1438. default:
  1439. assert (0);
  1440. }
  1441. return 0;
  1442. }
  1443. static void
  1444. ospf6_send (struct in6_addr *src, struct in6_addr *dst,
  1445. struct ospf6_interface *oi, struct ospf6_header *oh)
  1446. {
  1447. unsigned int len;
  1448. char srcname[64], dstname[64];
  1449. struct iovec iovector[2];
  1450. /* initialize */
  1451. iovector[0].iov_base = (caddr_t) oh;
  1452. iovector[0].iov_len = ntohs (oh->length);
  1453. iovector[1].iov_base = NULL;
  1454. iovector[1].iov_len = 0;
  1455. /* fill OSPF header */
  1456. oh->version = OSPFV3_VERSION;
  1457. /* message type must be set before */
  1458. /* message length must be set before */
  1459. oh->router_id = oi->area->ospf6->router_id;
  1460. oh->area_id = oi->area->area_id;
  1461. /* checksum is calculated by kernel */
  1462. oh->instance_id = oi->instance_id;
  1463. oh->reserved = 0;
  1464. /* Log */
  1465. if (IS_OSPF6_DEBUG_MESSAGE (oh->type, SEND))
  1466. {
  1467. inet_ntop (AF_INET6, dst, dstname, sizeof (dstname));
  1468. if (src)
  1469. inet_ntop (AF_INET6, src, srcname, sizeof (srcname));
  1470. else
  1471. memset (srcname, 0, sizeof (srcname));
  1472. zlog_debug ("%s send on %s",
  1473. LOOKUP (ospf6_message_type_str, oh->type), oi->interface->name);
  1474. zlog_debug (" src: %s", srcname);
  1475. zlog_debug (" dst: %s", dstname);
  1476. switch (oh->type)
  1477. {
  1478. case OSPF6_MESSAGE_TYPE_HELLO:
  1479. ospf6_hello_print (oh);
  1480. break;
  1481. case OSPF6_MESSAGE_TYPE_DBDESC:
  1482. ospf6_dbdesc_print (oh);
  1483. break;
  1484. case OSPF6_MESSAGE_TYPE_LSREQ:
  1485. ospf6_lsreq_print (oh);
  1486. break;
  1487. case OSPF6_MESSAGE_TYPE_LSUPDATE:
  1488. ospf6_lsupdate_print (oh);
  1489. break;
  1490. case OSPF6_MESSAGE_TYPE_LSACK:
  1491. ospf6_lsack_print (oh);
  1492. break;
  1493. default:
  1494. zlog_debug ("Unknown message");
  1495. assert (0);
  1496. break;
  1497. }
  1498. }
  1499. /* send message */
  1500. len = ospf6_sendmsg (src, dst, &oi->interface->ifindex, iovector);
  1501. if (len != ntohs (oh->length))
  1502. zlog_err ("Could not send entire message");
  1503. }
  1504. static uint32_t
  1505. ospf6_packet_max(struct ospf6_interface *oi)
  1506. {
  1507. assert (oi->ifmtu > sizeof (struct ip6_hdr));
  1508. return oi->ifmtu - (sizeof (struct ip6_hdr));
  1509. }
  1510. int
  1511. ospf6_hello_send (struct thread *thread)
  1512. {
  1513. struct ospf6_interface *oi;
  1514. struct ospf6_header *oh;
  1515. struct ospf6_hello *hello;
  1516. u_char *p;
  1517. struct listnode *node, *nnode;
  1518. struct ospf6_neighbor *on;
  1519. oi = (struct ospf6_interface *) THREAD_ARG (thread);
  1520. oi->thread_send_hello = (struct thread *) NULL;
  1521. if (oi->state <= OSPF6_INTERFACE_DOWN)
  1522. {
  1523. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_HELLO, SEND))
  1524. zlog_debug ("Unable to send Hello on down interface %s",
  1525. oi->interface->name);
  1526. return 0;
  1527. }
  1528. if (iobuflen == 0)
  1529. {
  1530. zlog_debug ("Unable to send Hello on interface %s iobuflen is 0",
  1531. oi->interface->name);
  1532. return 0;
  1533. }
  1534. /* set next thread */
  1535. oi->thread_send_hello = thread_add_timer (master, ospf6_hello_send,
  1536. oi, oi->hello_interval);
  1537. memset (sendbuf, 0, iobuflen);
  1538. oh = (struct ospf6_header *) sendbuf;
  1539. hello = (struct ospf6_hello *)((caddr_t) oh + sizeof (struct ospf6_header));
  1540. hello->interface_id = htonl (oi->interface->ifindex);
  1541. hello->priority = oi->priority;
  1542. hello->options[0] = oi->area->options[0];
  1543. hello->options[1] = oi->area->options[1];
  1544. hello->options[2] = oi->area->options[2];
  1545. hello->hello_interval = htons (oi->hello_interval);
  1546. hello->dead_interval = htons (oi->dead_interval);
  1547. hello->drouter = oi->drouter;
  1548. hello->bdrouter = oi->bdrouter;
  1549. p = (u_char *)((caddr_t) hello + sizeof (struct ospf6_hello));
  1550. for (ALL_LIST_ELEMENTS (oi->neighbor_list, node, nnode, on))
  1551. {
  1552. if (on->state < OSPF6_NEIGHBOR_INIT)
  1553. continue;
  1554. if (p - sendbuf + sizeof (u_int32_t) > ospf6_packet_max(oi))
  1555. {
  1556. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_HELLO, SEND))
  1557. zlog_debug ("sending Hello message: exceeds I/F MTU");
  1558. break;
  1559. }
  1560. memcpy (p, &on->router_id, sizeof (u_int32_t));
  1561. p += sizeof (u_int32_t);
  1562. }
  1563. oh->type = OSPF6_MESSAGE_TYPE_HELLO;
  1564. oh->length = htons (p - sendbuf);
  1565. ospf6_send (oi->linklocal_addr, &allspfrouters6, oi, oh);
  1566. return 0;
  1567. }
  1568. int
  1569. ospf6_dbdesc_send (struct thread *thread)
  1570. {
  1571. struct ospf6_neighbor *on;
  1572. struct ospf6_header *oh;
  1573. struct ospf6_dbdesc *dbdesc;
  1574. u_char *p;
  1575. struct ospf6_lsa *lsa;
  1576. struct in6_addr *dst;
  1577. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1578. on->thread_send_dbdesc = (struct thread *) NULL;
  1579. if (on->state < OSPF6_NEIGHBOR_EXSTART)
  1580. {
  1581. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_DBDESC, SEND))
  1582. zlog_debug ("Quit to send DbDesc to neighbor %s state %s",
  1583. on->name, ospf6_neighbor_state_str[on->state]);
  1584. return 0;
  1585. }
  1586. /* set next thread if master */
  1587. if (CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT))
  1588. on->thread_send_dbdesc =
  1589. thread_add_timer (master, ospf6_dbdesc_send, on,
  1590. on->ospf6_if->rxmt_interval);
  1591. memset (sendbuf, 0, iobuflen);
  1592. oh = (struct ospf6_header *) sendbuf;
  1593. dbdesc = (struct ospf6_dbdesc *)((caddr_t) oh +
  1594. sizeof (struct ospf6_header));
  1595. /* if this is initial one, initialize sequence number for DbDesc */
  1596. if (CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT) &&
  1597. (on->dbdesc_seqnum == 0))
  1598. {
  1599. struct timeval tv;
  1600. if (quagga_gettime (QUAGGA_CLK_MONOTONIC, &tv) < 0)
  1601. tv.tv_sec = 1;
  1602. on->dbdesc_seqnum = tv.tv_sec;
  1603. }
  1604. dbdesc->options[0] = on->ospf6_if->area->options[0];
  1605. dbdesc->options[1] = on->ospf6_if->area->options[1];
  1606. dbdesc->options[2] = on->ospf6_if->area->options[2];
  1607. dbdesc->ifmtu = htons (on->ospf6_if->ifmtu);
  1608. dbdesc->bits = on->dbdesc_bits;
  1609. dbdesc->seqnum = htonl (on->dbdesc_seqnum);
  1610. /* if this is not initial one, set LSA headers in dbdesc */
  1611. p = (u_char *)((caddr_t) dbdesc + sizeof (struct ospf6_dbdesc));
  1612. if (! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT))
  1613. {
  1614. for (lsa = ospf6_lsdb_head (on->dbdesc_list); lsa;
  1615. lsa = ospf6_lsdb_next (lsa))
  1616. {
  1617. ospf6_lsa_age_update_to_send (lsa, on->ospf6_if->transdelay);
  1618. /* MTU check */
  1619. if (p - sendbuf + sizeof (struct ospf6_lsa_header) >
  1620. ospf6_packet_max(on->ospf6_if))
  1621. {
  1622. ospf6_lsdb_lsa_unlock (lsa);
  1623. break;
  1624. }
  1625. memcpy (p, lsa->header, sizeof (struct ospf6_lsa_header));
  1626. p += sizeof (struct ospf6_lsa_header);
  1627. }
  1628. }
  1629. oh->type = OSPF6_MESSAGE_TYPE_DBDESC;
  1630. oh->length = htons (p - sendbuf);
  1631. if (on->ospf6_if->state == OSPF6_INTERFACE_POINTTOPOINT)
  1632. dst = &allspfrouters6;
  1633. else
  1634. dst = &on->linklocal_addr;
  1635. ospf6_send (on->ospf6_if->linklocal_addr, dst, on->ospf6_if, oh);
  1636. return 0;
  1637. }
  1638. int
  1639. ospf6_dbdesc_send_newone (struct thread *thread)
  1640. {
  1641. struct ospf6_neighbor *on;
  1642. struct ospf6_lsa *lsa;
  1643. unsigned int size = 0;
  1644. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1645. ospf6_lsdb_remove_all (on->dbdesc_list);
  1646. /* move LSAs from summary_list to dbdesc_list (within neighbor structure)
  1647. so that ospf6_send_dbdesc () can send those LSAs */
  1648. size = sizeof (struct ospf6_lsa_header) + sizeof (struct ospf6_dbdesc);
  1649. for (lsa = ospf6_lsdb_head (on->summary_list); lsa;
  1650. lsa = ospf6_lsdb_next (lsa))
  1651. {
  1652. if (size + sizeof (struct ospf6_lsa_header) > ospf6_packet_max(on->ospf6_if))
  1653. {
  1654. ospf6_lsdb_lsa_unlock (lsa);
  1655. break;
  1656. }
  1657. ospf6_lsdb_add (ospf6_lsa_copy (lsa), on->dbdesc_list);
  1658. ospf6_lsdb_remove (lsa, on->summary_list);
  1659. size += sizeof (struct ospf6_lsa_header);
  1660. }
  1661. if (on->summary_list->count == 0)
  1662. UNSET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT);
  1663. /* If slave, More bit check must be done here */
  1664. if (! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT) && /* Slave */
  1665. ! CHECK_FLAG (on->dbdesc_last.bits, OSPF6_DBDESC_MBIT) &&
  1666. ! CHECK_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT))
  1667. thread_add_event (master, exchange_done, on, 0);
  1668. thread_execute (master, ospf6_dbdesc_send, on, 0);
  1669. return 0;
  1670. }
  1671. int
  1672. ospf6_lsreq_send (struct thread *thread)
  1673. {
  1674. struct ospf6_neighbor *on;
  1675. struct ospf6_header *oh;
  1676. struct ospf6_lsreq_entry *e;
  1677. u_char *p;
  1678. struct ospf6_lsa *lsa, *last_req;
  1679. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1680. on->thread_send_lsreq = (struct thread *) NULL;
  1681. /* LSReq will be sent only in ExStart or Loading */
  1682. if (on->state != OSPF6_NEIGHBOR_EXCHANGE &&
  1683. on->state != OSPF6_NEIGHBOR_LOADING)
  1684. {
  1685. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSREQ, SEND))
  1686. zlog_debug ("Quit to send LSReq to neighbor %s state %s",
  1687. on->name, ospf6_neighbor_state_str[on->state]);
  1688. return 0;
  1689. }
  1690. /* schedule loading_done if request list is empty */
  1691. if (on->request_list->count == 0)
  1692. {
  1693. thread_add_event (master, loading_done, on, 0);
  1694. return 0;
  1695. }
  1696. memset (sendbuf, 0, iobuflen);
  1697. oh = (struct ospf6_header *) sendbuf;
  1698. last_req = NULL;
  1699. /* set Request entries in lsreq */
  1700. p = (u_char *)((caddr_t) oh + sizeof (struct ospf6_header));
  1701. for (lsa = ospf6_lsdb_head (on->request_list); lsa;
  1702. lsa = ospf6_lsdb_next (lsa))
  1703. {
  1704. /* MTU check */
  1705. if (p - sendbuf + sizeof (struct ospf6_lsreq_entry) > ospf6_packet_max(on->ospf6_if))
  1706. {
  1707. ospf6_lsdb_lsa_unlock (lsa);
  1708. break;
  1709. }
  1710. e = (struct ospf6_lsreq_entry *) p;
  1711. e->type = lsa->header->type;
  1712. e->id = lsa->header->id;
  1713. e->adv_router = lsa->header->adv_router;
  1714. p += sizeof (struct ospf6_lsreq_entry);
  1715. last_req = lsa;
  1716. }
  1717. if (last_req != NULL)
  1718. {
  1719. if (on->last_ls_req != NULL)
  1720. {
  1721. ospf6_lsa_unlock (on->last_ls_req);
  1722. }
  1723. ospf6_lsa_lock (last_req);
  1724. on->last_ls_req = last_req;
  1725. }
  1726. oh->type = OSPF6_MESSAGE_TYPE_LSREQ;
  1727. oh->length = htons (p - sendbuf);
  1728. if (on->ospf6_if->state == OSPF6_INTERFACE_POINTTOPOINT)
  1729. ospf6_send (on->ospf6_if->linklocal_addr, &allspfrouters6,
  1730. on->ospf6_if, oh);
  1731. else
  1732. ospf6_send (on->ospf6_if->linklocal_addr, &on->linklocal_addr,
  1733. on->ospf6_if, oh);
  1734. /* set next thread */
  1735. if (on->request_list->count != 0)
  1736. {
  1737. on->thread_send_lsreq =
  1738. thread_add_timer (master, ospf6_lsreq_send, on,
  1739. on->ospf6_if->rxmt_interval);
  1740. }
  1741. return 0;
  1742. }
  1743. int
  1744. ospf6_lsupdate_send_neighbor (struct thread *thread)
  1745. {
  1746. struct ospf6_neighbor *on;
  1747. struct ospf6_header *oh;
  1748. struct ospf6_lsupdate *lsupdate;
  1749. u_char *p;
  1750. int lsa_cnt;
  1751. struct ospf6_lsa *lsa;
  1752. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1753. on->thread_send_lsupdate = (struct thread *) NULL;
  1754. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSUPDATE, SEND))
  1755. zlog_debug ("LSUpdate to neighbor %s", on->name);
  1756. if (on->state < OSPF6_NEIGHBOR_EXCHANGE)
  1757. {
  1758. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSUPDATE, SEND))
  1759. zlog_debug ("Quit to send (neighbor state %s)",
  1760. ospf6_neighbor_state_str[on->state]);
  1761. return 0;
  1762. }
  1763. memset (sendbuf, 0, iobuflen);
  1764. oh = (struct ospf6_header *) sendbuf;
  1765. lsupdate = (struct ospf6_lsupdate *)
  1766. ((caddr_t) oh + sizeof (struct ospf6_header));
  1767. p = (u_char *)((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  1768. lsa_cnt = 0;
  1769. /* lsupdate_list lists those LSA which doesn't need to be
  1770. retransmitted. remove those from the list */
  1771. for (lsa = ospf6_lsdb_head (on->lsupdate_list); lsa;
  1772. lsa = ospf6_lsdb_next (lsa))
  1773. {
  1774. /* MTU check */
  1775. if ( (p - sendbuf + (unsigned int)OSPF6_LSA_SIZE (lsa->header))
  1776. > ospf6_packet_max(on->ospf6_if))
  1777. {
  1778. ospf6_lsdb_lsa_unlock (lsa);
  1779. break;
  1780. }
  1781. ospf6_lsa_age_update_to_send (lsa, on->ospf6_if->transdelay);
  1782. memcpy (p, lsa->header, OSPF6_LSA_SIZE (lsa->header));
  1783. p += OSPF6_LSA_SIZE (lsa->header);
  1784. lsa_cnt++;
  1785. assert (lsa->lock == 2);
  1786. ospf6_lsdb_remove (lsa, on->lsupdate_list);
  1787. }
  1788. if (lsa_cnt)
  1789. {
  1790. oh->type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  1791. oh->length = htons (p - sendbuf);
  1792. lsupdate->lsa_number = htonl (lsa_cnt);
  1793. if ((on->ospf6_if->state == OSPF6_INTERFACE_POINTTOPOINT) ||
  1794. (on->ospf6_if->state == OSPF6_INTERFACE_DR) ||
  1795. (on->ospf6_if->state == OSPF6_INTERFACE_BDR))
  1796. ospf6_send (on->ospf6_if->linklocal_addr, &allspfrouters6,
  1797. on->ospf6_if, oh);
  1798. else
  1799. ospf6_send (on->ospf6_if->linklocal_addr, &on->linklocal_addr,
  1800. on->ospf6_if, oh);
  1801. }
  1802. /* The addresses used for retransmissions are different from those sent the
  1803. first time and so we need to separate them here.
  1804. */
  1805. memset (sendbuf, 0, iobuflen);
  1806. oh = (struct ospf6_header *) sendbuf;
  1807. lsupdate = (struct ospf6_lsupdate *)
  1808. ((caddr_t) oh + sizeof (struct ospf6_header));
  1809. p = (u_char *)((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  1810. lsa_cnt = 0;
  1811. for (lsa = ospf6_lsdb_head (on->retrans_list); lsa;
  1812. lsa = ospf6_lsdb_next (lsa))
  1813. {
  1814. /* MTU check */
  1815. if ( (p - sendbuf + (unsigned int)OSPF6_LSA_SIZE (lsa->header))
  1816. > ospf6_packet_max(on->ospf6_if))
  1817. {
  1818. ospf6_lsdb_lsa_unlock (lsa);
  1819. break;
  1820. }
  1821. ospf6_lsa_age_update_to_send (lsa, on->ospf6_if->transdelay);
  1822. memcpy (p, lsa->header, OSPF6_LSA_SIZE (lsa->header));
  1823. p += OSPF6_LSA_SIZE (lsa->header);
  1824. lsa_cnt++;
  1825. }
  1826. if (lsa_cnt)
  1827. {
  1828. oh->type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  1829. oh->length = htons (p - sendbuf);
  1830. lsupdate->lsa_number = htonl (lsa_cnt);
  1831. if (on->ospf6_if->state == OSPF6_INTERFACE_POINTTOPOINT)
  1832. ospf6_send (on->ospf6_if->linklocal_addr, &allspfrouters6,
  1833. on->ospf6_if, oh);
  1834. else
  1835. ospf6_send (on->ospf6_if->linklocal_addr, &on->linklocal_addr,
  1836. on->ospf6_if, oh);
  1837. }
  1838. if (on->lsupdate_list->count != 0)
  1839. on->thread_send_lsupdate =
  1840. thread_add_event (master, ospf6_lsupdate_send_neighbor, on, 0);
  1841. else if (on->retrans_list->count != 0)
  1842. on->thread_send_lsupdate =
  1843. thread_add_timer (master, ospf6_lsupdate_send_neighbor, on,
  1844. on->ospf6_if->rxmt_interval);
  1845. return 0;
  1846. }
  1847. int
  1848. ospf6_lsupdate_send_interface (struct thread *thread)
  1849. {
  1850. struct ospf6_interface *oi;
  1851. struct ospf6_header *oh;
  1852. struct ospf6_lsupdate *lsupdate;
  1853. u_char *p;
  1854. int lsa_cnt;
  1855. struct ospf6_lsa *lsa;
  1856. oi = (struct ospf6_interface *) THREAD_ARG (thread);
  1857. oi->thread_send_lsupdate = (struct thread *) NULL;
  1858. if (oi->state <= OSPF6_INTERFACE_WAITING)
  1859. {
  1860. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSUPDATE, SEND))
  1861. zlog_debug ("Quit to send LSUpdate to interface %s state %s",
  1862. oi->interface->name, ospf6_interface_state_str[oi->state]);
  1863. return 0;
  1864. }
  1865. /* if we have nothing to send, return */
  1866. if (oi->lsupdate_list->count == 0)
  1867. return 0;
  1868. memset (sendbuf, 0, iobuflen);
  1869. oh = (struct ospf6_header *) sendbuf;
  1870. lsupdate = (struct ospf6_lsupdate *)((caddr_t) oh +
  1871. sizeof (struct ospf6_header));
  1872. p = (u_char *)((caddr_t) lsupdate + sizeof (struct ospf6_lsupdate));
  1873. lsa_cnt = 0;
  1874. for (lsa = ospf6_lsdb_head (oi->lsupdate_list); lsa;
  1875. lsa = ospf6_lsdb_next (lsa))
  1876. {
  1877. /* MTU check */
  1878. if ( (p - sendbuf + ((unsigned int)OSPF6_LSA_SIZE (lsa->header)))
  1879. > ospf6_packet_max(oi))
  1880. {
  1881. ospf6_lsdb_lsa_unlock (lsa);
  1882. break;
  1883. }
  1884. ospf6_lsa_age_update_to_send (lsa, oi->transdelay);
  1885. memcpy (p, lsa->header, OSPF6_LSA_SIZE (lsa->header));
  1886. p += OSPF6_LSA_SIZE (lsa->header);
  1887. lsa_cnt++;
  1888. assert (lsa->lock == 2);
  1889. ospf6_lsdb_remove (lsa, oi->lsupdate_list);
  1890. }
  1891. if (lsa_cnt)
  1892. {
  1893. lsupdate->lsa_number = htonl (lsa_cnt);
  1894. oh->type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  1895. oh->length = htons (p - sendbuf);
  1896. if ((oi->state == OSPF6_INTERFACE_POINTTOPOINT) ||
  1897. (oi->state == OSPF6_INTERFACE_DR) ||
  1898. (oi->state == OSPF6_INTERFACE_BDR))
  1899. ospf6_send (oi->linklocal_addr, &allspfrouters6, oi, oh);
  1900. else
  1901. ospf6_send (oi->linklocal_addr, &alldrouters6, oi, oh);
  1902. }
  1903. if (oi->lsupdate_list->count > 0)
  1904. {
  1905. oi->thread_send_lsupdate =
  1906. thread_add_event (master, ospf6_lsupdate_send_interface, oi, 0);
  1907. }
  1908. return 0;
  1909. }
  1910. int
  1911. ospf6_lsack_send_neighbor (struct thread *thread)
  1912. {
  1913. struct ospf6_neighbor *on;
  1914. struct ospf6_header *oh;
  1915. u_char *p;
  1916. struct ospf6_lsa *lsa;
  1917. int lsa_cnt = 0;
  1918. on = (struct ospf6_neighbor *) THREAD_ARG (thread);
  1919. on->thread_send_lsack = (struct thread *) NULL;
  1920. if (on->state < OSPF6_NEIGHBOR_EXCHANGE)
  1921. {
  1922. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSACK, SEND))
  1923. zlog_debug ("Quit to send LSAck to neighbor %s state %s",
  1924. on->name, ospf6_neighbor_state_str[on->state]);
  1925. return 0;
  1926. }
  1927. /* if we have nothing to send, return */
  1928. if (on->lsack_list->count == 0)
  1929. return 0;
  1930. memset (sendbuf, 0, iobuflen);
  1931. oh = (struct ospf6_header *) sendbuf;
  1932. p = (u_char *)((caddr_t) oh + sizeof (struct ospf6_header));
  1933. for (lsa = ospf6_lsdb_head (on->lsack_list); lsa;
  1934. lsa = ospf6_lsdb_next (lsa))
  1935. {
  1936. /* MTU check */
  1937. if (p - sendbuf + sizeof (struct ospf6_lsa_header) > ospf6_packet_max(on->ospf6_if))
  1938. {
  1939. /* if we run out of packet size/space here,
  1940. better to try again soon. */
  1941. THREAD_OFF (on->thread_send_lsack);
  1942. on->thread_send_lsack =
  1943. thread_add_event (master, ospf6_lsack_send_neighbor, on, 0);
  1944. ospf6_lsdb_lsa_unlock (lsa);
  1945. break;
  1946. }
  1947. ospf6_lsa_age_update_to_send (lsa, on->ospf6_if->transdelay);
  1948. memcpy (p, lsa->header, sizeof (struct ospf6_lsa_header));
  1949. p += sizeof (struct ospf6_lsa_header);
  1950. assert (lsa->lock == 2);
  1951. ospf6_lsdb_remove (lsa, on->lsack_list);
  1952. lsa_cnt++;
  1953. }
  1954. if (lsa_cnt)
  1955. {
  1956. oh->type = OSPF6_MESSAGE_TYPE_LSACK;
  1957. oh->length = htons (p - sendbuf);
  1958. ospf6_send (on->ospf6_if->linklocal_addr, &on->linklocal_addr,
  1959. on->ospf6_if, oh);
  1960. }
  1961. if (on->thread_send_lsack == NULL && on->lsack_list->count > 0)
  1962. {
  1963. on->thread_send_lsack =
  1964. thread_add_event (master, ospf6_lsack_send_neighbor, on, 0);
  1965. }
  1966. return 0;
  1967. }
  1968. int
  1969. ospf6_lsack_send_interface (struct thread *thread)
  1970. {
  1971. struct ospf6_interface *oi;
  1972. struct ospf6_header *oh;
  1973. u_char *p;
  1974. struct ospf6_lsa *lsa;
  1975. int lsa_cnt = 0;
  1976. oi = (struct ospf6_interface *) THREAD_ARG (thread);
  1977. oi->thread_send_lsack = (struct thread *) NULL;
  1978. if (oi->state <= OSPF6_INTERFACE_WAITING)
  1979. {
  1980. if (IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_LSACK, SEND))
  1981. zlog_debug ("Quit to send LSAck to interface %s state %s",
  1982. oi->interface->name, ospf6_interface_state_str[oi->state]);
  1983. return 0;
  1984. }
  1985. /* if we have nothing to send, return */
  1986. if (oi->lsack_list->count == 0)
  1987. return 0;
  1988. memset (sendbuf, 0, iobuflen);
  1989. oh = (struct ospf6_header *) sendbuf;
  1990. p = (u_char *)((caddr_t) oh + sizeof (struct ospf6_header));
  1991. for (lsa = ospf6_lsdb_head (oi->lsack_list); lsa;
  1992. lsa = ospf6_lsdb_next (lsa))
  1993. {
  1994. /* MTU check */
  1995. if (p - sendbuf + sizeof (struct ospf6_lsa_header) > ospf6_packet_max(oi))
  1996. {
  1997. /* if we run out of packet size/space here,
  1998. better to try again soon. */
  1999. THREAD_OFF (oi->thread_send_lsack);
  2000. oi->thread_send_lsack =
  2001. thread_add_event (master, ospf6_lsack_send_interface, oi, 0);
  2002. ospf6_lsdb_lsa_unlock (lsa);
  2003. break;
  2004. }
  2005. ospf6_lsa_age_update_to_send (lsa, oi->transdelay);
  2006. memcpy (p, lsa->header, sizeof (struct ospf6_lsa_header));
  2007. p += sizeof (struct ospf6_lsa_header);
  2008. assert (lsa->lock == 2);
  2009. ospf6_lsdb_remove (lsa, oi->lsack_list);
  2010. lsa_cnt++;
  2011. }
  2012. if (lsa_cnt)
  2013. {
  2014. oh->type = OSPF6_MESSAGE_TYPE_LSACK;
  2015. oh->length = htons (p - sendbuf);
  2016. if ((oi->state == OSPF6_INTERFACE_POINTTOPOINT) ||
  2017. (oi->state == OSPF6_INTERFACE_DR) ||
  2018. (oi->state == OSPF6_INTERFACE_BDR))
  2019. ospf6_send (oi->linklocal_addr, &allspfrouters6, oi, oh);
  2020. else
  2021. ospf6_send (oi->linklocal_addr, &alldrouters6, oi, oh);
  2022. }
  2023. if (oi->thread_send_lsack == NULL && oi->lsack_list->count > 0)
  2024. {
  2025. oi->thread_send_lsack =
  2026. thread_add_event (master, ospf6_lsack_send_interface, oi, 0);
  2027. }
  2028. return 0;
  2029. }
  2030. /* Commands */
  2031. DEFUN (debug_ospf6_message,
  2032. debug_ospf6_message_cmd,
  2033. "debug ospf6 message (unknown|hello|dbdesc|lsreq|lsupdate|lsack|all)",
  2034. DEBUG_STR
  2035. OSPF6_STR
  2036. "Debug OSPFv3 message\n"
  2037. "Debug Unknown message\n"
  2038. "Debug Hello message\n"
  2039. "Debug Database Description message\n"
  2040. "Debug Link State Request message\n"
  2041. "Debug Link State Update message\n"
  2042. "Debug Link State Acknowledgement message\n"
  2043. "Debug All message\n"
  2044. )
  2045. {
  2046. unsigned char level = 0;
  2047. int type = 0;
  2048. int i;
  2049. assert (argc > 0);
  2050. /* check type */
  2051. if (! strncmp (argv[0], "u", 1))
  2052. type = OSPF6_MESSAGE_TYPE_UNKNOWN;
  2053. else if (! strncmp (argv[0], "h", 1))
  2054. type = OSPF6_MESSAGE_TYPE_HELLO;
  2055. else if (! strncmp (argv[0], "d", 1))
  2056. type = OSPF6_MESSAGE_TYPE_DBDESC;
  2057. else if (! strncmp (argv[0], "lsr", 3))
  2058. type = OSPF6_MESSAGE_TYPE_LSREQ;
  2059. else if (! strncmp (argv[0], "lsu", 3))
  2060. type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  2061. else if (! strncmp (argv[0], "lsa", 3))
  2062. type = OSPF6_MESSAGE_TYPE_LSACK;
  2063. else if (! strncmp (argv[0], "a", 1))
  2064. type = OSPF6_MESSAGE_TYPE_ALL;
  2065. if (argc == 1)
  2066. level = OSPF6_DEBUG_MESSAGE_SEND | OSPF6_DEBUG_MESSAGE_RECV;
  2067. else if (! strncmp (argv[1], "s", 1))
  2068. level = OSPF6_DEBUG_MESSAGE_SEND;
  2069. else if (! strncmp (argv[1], "r", 1))
  2070. level = OSPF6_DEBUG_MESSAGE_RECV;
  2071. if (type == OSPF6_MESSAGE_TYPE_ALL)
  2072. {
  2073. for (i = 0; i < 6; i++)
  2074. OSPF6_DEBUG_MESSAGE_ON (i, level);
  2075. }
  2076. else
  2077. OSPF6_DEBUG_MESSAGE_ON (type, level);
  2078. return CMD_SUCCESS;
  2079. }
  2080. ALIAS (debug_ospf6_message,
  2081. debug_ospf6_message_sendrecv_cmd,
  2082. "debug ospf6 message (unknown|hello|dbdesc|lsreq|lsupdate|lsack|all) (send|recv)",
  2083. DEBUG_STR
  2084. OSPF6_STR
  2085. "Debug OSPFv3 message\n"
  2086. "Debug Unknown message\n"
  2087. "Debug Hello message\n"
  2088. "Debug Database Description message\n"
  2089. "Debug Link State Request message\n"
  2090. "Debug Link State Update message\n"
  2091. "Debug Link State Acknowledgement message\n"
  2092. "Debug All message\n"
  2093. "Debug only sending message\n"
  2094. "Debug only receiving message\n"
  2095. )
  2096. DEFUN (no_debug_ospf6_message,
  2097. no_debug_ospf6_message_cmd,
  2098. "no debug ospf6 message (unknown|hello|dbdesc|lsreq|lsupdate|lsack|all)",
  2099. NO_STR
  2100. DEBUG_STR
  2101. OSPF6_STR
  2102. "Debug OSPFv3 message\n"
  2103. "Debug Unknown message\n"
  2104. "Debug Hello message\n"
  2105. "Debug Database Description message\n"
  2106. "Debug Link State Request message\n"
  2107. "Debug Link State Update message\n"
  2108. "Debug Link State Acknowledgement message\n"
  2109. "Debug All message\n"
  2110. )
  2111. {
  2112. unsigned char level = 0;
  2113. int type = 0;
  2114. int i;
  2115. assert (argc > 0);
  2116. /* check type */
  2117. if (! strncmp (argv[0], "u", 1))
  2118. type = OSPF6_MESSAGE_TYPE_UNKNOWN;
  2119. else if (! strncmp (argv[0], "h", 1))
  2120. type = OSPF6_MESSAGE_TYPE_HELLO;
  2121. else if (! strncmp (argv[0], "d", 1))
  2122. type = OSPF6_MESSAGE_TYPE_DBDESC;
  2123. else if (! strncmp (argv[0], "lsr", 3))
  2124. type = OSPF6_MESSAGE_TYPE_LSREQ;
  2125. else if (! strncmp (argv[0], "lsu", 3))
  2126. type = OSPF6_MESSAGE_TYPE_LSUPDATE;
  2127. else if (! strncmp (argv[0], "lsa", 3))
  2128. type = OSPF6_MESSAGE_TYPE_LSACK;
  2129. else if (! strncmp (argv[0], "a", 1))
  2130. type = OSPF6_MESSAGE_TYPE_ALL;
  2131. if (argc == 1)
  2132. level = OSPF6_DEBUG_MESSAGE_SEND | OSPF6_DEBUG_MESSAGE_RECV;
  2133. else if (! strncmp (argv[1], "s", 1))
  2134. level = OSPF6_DEBUG_MESSAGE_SEND;
  2135. else if (! strncmp (argv[1], "r", 1))
  2136. level = OSPF6_DEBUG_MESSAGE_RECV;
  2137. if (type == OSPF6_MESSAGE_TYPE_ALL)
  2138. {
  2139. for (i = 0; i < 6; i++)
  2140. OSPF6_DEBUG_MESSAGE_OFF (i, level);
  2141. }
  2142. else
  2143. OSPF6_DEBUG_MESSAGE_OFF (type, level);
  2144. return CMD_SUCCESS;
  2145. }
  2146. ALIAS (no_debug_ospf6_message,
  2147. no_debug_ospf6_message_sendrecv_cmd,
  2148. "no debug ospf6 message "
  2149. "(unknown|hello|dbdesc|lsreq|lsupdate|lsack|all) (send|recv)",
  2150. NO_STR
  2151. DEBUG_STR
  2152. OSPF6_STR
  2153. "Debug OSPFv3 message\n"
  2154. "Debug Unknown message\n"
  2155. "Debug Hello message\n"
  2156. "Debug Database Description message\n"
  2157. "Debug Link State Request message\n"
  2158. "Debug Link State Update message\n"
  2159. "Debug Link State Acknowledgement message\n"
  2160. "Debug All message\n"
  2161. "Debug only sending message\n"
  2162. "Debug only receiving message\n"
  2163. )
  2164. int
  2165. config_write_ospf6_debug_message (struct vty *vty)
  2166. {
  2167. const char *type_str[] = {"unknown", "hello", "dbdesc",
  2168. "lsreq", "lsupdate", "lsack"};
  2169. unsigned char s = 0, r = 0;
  2170. int i;
  2171. for (i = 0; i < 6; i++)
  2172. {
  2173. if (IS_OSPF6_DEBUG_MESSAGE (i, SEND))
  2174. s |= 1 << i;
  2175. if (IS_OSPF6_DEBUG_MESSAGE (i, RECV))
  2176. r |= 1 << i;
  2177. }
  2178. if (s == 0x3f && r == 0x3f)
  2179. {
  2180. vty_out (vty, "debug ospf6 message all%s", VNL);
  2181. return 0;
  2182. }
  2183. if (s == 0x3f && r == 0)
  2184. {
  2185. vty_out (vty, "debug ospf6 message all send%s", VNL);
  2186. return 0;
  2187. }
  2188. else if (s == 0 && r == 0x3f)
  2189. {
  2190. vty_out (vty, "debug ospf6 message all recv%s", VNL);
  2191. return 0;
  2192. }
  2193. /* Unknown message is logged by default */
  2194. if (! IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, SEND) &&
  2195. ! IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  2196. vty_out (vty, "no debug ospf6 message unknown%s", VNL);
  2197. else if (! IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, SEND))
  2198. vty_out (vty, "no debug ospf6 message unknown send%s", VNL);
  2199. else if (! IS_OSPF6_DEBUG_MESSAGE (OSPF6_MESSAGE_TYPE_UNKNOWN, RECV))
  2200. vty_out (vty, "no debug ospf6 message unknown recv%s", VNL);
  2201. for (i = 1; i < 6; i++)
  2202. {
  2203. if (IS_OSPF6_DEBUG_MESSAGE (i, SEND) &&
  2204. IS_OSPF6_DEBUG_MESSAGE (i, RECV))
  2205. vty_out (vty, "debug ospf6 message %s%s", type_str[i], VNL);
  2206. else if (IS_OSPF6_DEBUG_MESSAGE (i, SEND))
  2207. vty_out (vty, "debug ospf6 message %s send%s", type_str[i],
  2208. VNL);
  2209. else if (IS_OSPF6_DEBUG_MESSAGE (i, RECV))
  2210. vty_out (vty, "debug ospf6 message %s recv%s", type_str[i],
  2211. VNL);
  2212. }
  2213. return 0;
  2214. }
  2215. void
  2216. install_element_ospf6_debug_message (void)
  2217. {
  2218. install_element (ENABLE_NODE, &debug_ospf6_message_cmd);
  2219. install_element (ENABLE_NODE, &no_debug_ospf6_message_cmd);
  2220. install_element (ENABLE_NODE, &debug_ospf6_message_sendrecv_cmd);
  2221. install_element (ENABLE_NODE, &no_debug_ospf6_message_sendrecv_cmd);
  2222. install_element (CONFIG_NODE, &debug_ospf6_message_cmd);
  2223. install_element (CONFIG_NODE, &no_debug_ospf6_message_cmd);
  2224. install_element (CONFIG_NODE, &debug_ospf6_message_sendrecv_cmd);
  2225. install_element (CONFIG_NODE, &no_debug_ospf6_message_sendrecv_cmd);
  2226. }