ospf_packet.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912
  1. /*
  2. * OSPF Sending and Receiving OSPF Packets.
  3. * Copyright (C) 1999, 2000 Toshiaki Takada
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "thread.h"
  24. #include "memory.h"
  25. #include "linklist.h"
  26. #include "prefix.h"
  27. #include "if.h"
  28. #include "table.h"
  29. #include "sockunion.h"
  30. #include "stream.h"
  31. #include "log.h"
  32. #include "sockopt.h"
  33. #include "checksum.h"
  34. #include "md5.h"
  35. #include "ospfd/ospfd.h"
  36. #include "ospfd/ospf_network.h"
  37. #include "ospfd/ospf_interface.h"
  38. #include "ospfd/ospf_ism.h"
  39. #include "ospfd/ospf_asbr.h"
  40. #include "ospfd/ospf_lsa.h"
  41. #include "ospfd/ospf_lsdb.h"
  42. #include "ospfd/ospf_neighbor.h"
  43. #include "ospfd/ospf_nsm.h"
  44. #include "ospfd/ospf_packet.h"
  45. #include "ospfd/ospf_spf.h"
  46. #include "ospfd/ospf_flood.h"
  47. #include "ospfd/ospf_dump.h"
  48. /* Packet Type String. */
  49. const struct message ospf_packet_type_str[] =
  50. {
  51. { OSPF_MSG_HELLO, "Hello" },
  52. { OSPF_MSG_DB_DESC, "Database Description" },
  53. { OSPF_MSG_LS_REQ, "Link State Request" },
  54. { OSPF_MSG_LS_UPD, "Link State Update" },
  55. { OSPF_MSG_LS_ACK, "Link State Acknowledgment" },
  56. };
  57. const size_t ospf_packet_type_str_max = sizeof (ospf_packet_type_str) /
  58. sizeof (ospf_packet_type_str[0]);
  59. /* Minimum (besides OSPF_HEADER_SIZE) lengths for OSPF packets of
  60. particular types, offset is the "type" field of a packet. */
  61. static const u_int16_t ospf_packet_minlen[] =
  62. {
  63. 0,
  64. OSPF_HELLO_MIN_SIZE,
  65. OSPF_DB_DESC_MIN_SIZE,
  66. OSPF_LS_REQ_MIN_SIZE,
  67. OSPF_LS_UPD_MIN_SIZE,
  68. OSPF_LS_ACK_MIN_SIZE,
  69. };
  70. /* Minimum (besides OSPF_LSA_HEADER_SIZE) lengths for LSAs of particular
  71. types, offset is the "LSA type" field. */
  72. static const u_int16_t ospf_lsa_minlen[] =
  73. {
  74. 0,
  75. OSPF_ROUTER_LSA_MIN_SIZE,
  76. OSPF_NETWORK_LSA_MIN_SIZE,
  77. OSPF_SUMMARY_LSA_MIN_SIZE,
  78. OSPF_SUMMARY_LSA_MIN_SIZE,
  79. OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
  80. 0,
  81. OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
  82. 0,
  83. 0,
  84. 0,
  85. 0,
  86. };
  87. /* for ospf_check_auth() */
  88. static int ospf_check_sum (struct ospf_header *);
  89. /* OSPF authentication checking function */
  90. static int
  91. ospf_auth_type (struct ospf_interface *oi)
  92. {
  93. int auth_type;
  94. if (OSPF_IF_PARAM (oi, auth_type) == OSPF_AUTH_NOTSET)
  95. auth_type = oi->area->auth_type;
  96. else
  97. auth_type = OSPF_IF_PARAM (oi, auth_type);
  98. /* Handle case where MD5 key list is not configured aka Cisco */
  99. if (auth_type == OSPF_AUTH_CRYPTOGRAPHIC &&
  100. list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
  101. return OSPF_AUTH_NULL;
  102. return auth_type;
  103. }
  104. struct ospf_packet *
  105. ospf_packet_new (size_t size)
  106. {
  107. struct ospf_packet *new;
  108. new = XCALLOC (MTYPE_OSPF_PACKET, sizeof (struct ospf_packet));
  109. new->s = stream_new (size);
  110. return new;
  111. }
  112. void
  113. ospf_packet_free (struct ospf_packet *op)
  114. {
  115. if (op->s)
  116. stream_free (op->s);
  117. XFREE (MTYPE_OSPF_PACKET, op);
  118. op = NULL;
  119. }
  120. struct ospf_fifo *
  121. ospf_fifo_new ()
  122. {
  123. struct ospf_fifo *new;
  124. new = XCALLOC (MTYPE_OSPF_FIFO, sizeof (struct ospf_fifo));
  125. return new;
  126. }
  127. /* Add new packet to fifo. */
  128. void
  129. ospf_fifo_push (struct ospf_fifo *fifo, struct ospf_packet *op)
  130. {
  131. if (fifo->tail)
  132. fifo->tail->next = op;
  133. else
  134. fifo->head = op;
  135. fifo->tail = op;
  136. fifo->count++;
  137. }
  138. /* Add new packet to head of fifo. */
  139. static void
  140. ospf_fifo_push_head (struct ospf_fifo *fifo, struct ospf_packet *op)
  141. {
  142. op->next = fifo->head;
  143. if (fifo->tail == NULL)
  144. fifo->tail = op;
  145. fifo->head = op;
  146. fifo->count++;
  147. }
  148. /* Delete first packet from fifo. */
  149. struct ospf_packet *
  150. ospf_fifo_pop (struct ospf_fifo *fifo)
  151. {
  152. struct ospf_packet *op;
  153. op = fifo->head;
  154. if (op)
  155. {
  156. fifo->head = op->next;
  157. if (fifo->head == NULL)
  158. fifo->tail = NULL;
  159. fifo->count--;
  160. }
  161. return op;
  162. }
  163. /* Return first fifo entry. */
  164. struct ospf_packet *
  165. ospf_fifo_head (struct ospf_fifo *fifo)
  166. {
  167. return fifo->head;
  168. }
  169. /* Flush ospf packet fifo. */
  170. void
  171. ospf_fifo_flush (struct ospf_fifo *fifo)
  172. {
  173. struct ospf_packet *op;
  174. struct ospf_packet *next;
  175. for (op = fifo->head; op; op = next)
  176. {
  177. next = op->next;
  178. ospf_packet_free (op);
  179. }
  180. fifo->head = fifo->tail = NULL;
  181. fifo->count = 0;
  182. }
  183. /* Free ospf packet fifo. */
  184. void
  185. ospf_fifo_free (struct ospf_fifo *fifo)
  186. {
  187. ospf_fifo_flush (fifo);
  188. XFREE (MTYPE_OSPF_FIFO, fifo);
  189. }
  190. void
  191. ospf_packet_add (struct ospf_interface *oi, struct ospf_packet *op)
  192. {
  193. if (!oi->obuf)
  194. {
  195. zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, "
  196. "destination %s) called with NULL obuf, ignoring "
  197. "(please report this bug)!\n",
  198. IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state),
  199. LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)),
  200. inet_ntoa (op->dst));
  201. return;
  202. }
  203. /* Add packet to end of queue. */
  204. ospf_fifo_push (oi->obuf, op);
  205. /* Debug of packet fifo*/
  206. /* ospf_fifo_debug (oi->obuf); */
  207. }
  208. static void
  209. ospf_packet_add_top (struct ospf_interface *oi, struct ospf_packet *op)
  210. {
  211. if (!oi->obuf)
  212. {
  213. zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, "
  214. "destination %s) called with NULL obuf, ignoring "
  215. "(please report this bug)!\n",
  216. IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state),
  217. LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)),
  218. inet_ntoa (op->dst));
  219. return;
  220. }
  221. /* Add packet to head of queue. */
  222. ospf_fifo_push_head (oi->obuf, op);
  223. /* Debug of packet fifo*/
  224. /* ospf_fifo_debug (oi->obuf); */
  225. }
  226. void
  227. ospf_packet_delete (struct ospf_interface *oi)
  228. {
  229. struct ospf_packet *op;
  230. op = ospf_fifo_pop (oi->obuf);
  231. if (op)
  232. ospf_packet_free (op);
  233. }
  234. struct ospf_packet *
  235. ospf_packet_dup (struct ospf_packet *op)
  236. {
  237. struct ospf_packet *new;
  238. if (stream_get_endp(op->s) != op->length)
  239. /* XXX size_t */
  240. zlog_warn ("ospf_packet_dup stream %lu ospf_packet %u size mismatch",
  241. (u_long)STREAM_SIZE(op->s), op->length);
  242. /* Reserve space for MD5 authentication that may be added later. */
  243. new = ospf_packet_new (stream_get_endp(op->s) + OSPF_AUTH_MD5_SIZE);
  244. stream_copy (new->s, op->s);
  245. new->dst = op->dst;
  246. new->length = op->length;
  247. return new;
  248. }
  249. /* XXX inline */
  250. static unsigned int
  251. ospf_packet_authspace (struct ospf_interface *oi)
  252. {
  253. int auth = 0;
  254. if ( ospf_auth_type (oi) == OSPF_AUTH_CRYPTOGRAPHIC)
  255. auth = OSPF_AUTH_MD5_SIZE;
  256. return auth;
  257. }
  258. static unsigned int
  259. ospf_packet_max (struct ospf_interface *oi)
  260. {
  261. int max;
  262. max = oi->ifp->mtu - ospf_packet_authspace(oi);
  263. max -= (OSPF_HEADER_SIZE + sizeof (struct ip));
  264. return max;
  265. }
  266. static int
  267. ospf_check_md5_digest (struct ospf_interface *oi, struct ospf_header *ospfh)
  268. {
  269. MD5_CTX ctx;
  270. unsigned char digest[OSPF_AUTH_MD5_SIZE];
  271. struct crypt_key *ck;
  272. struct ospf_neighbor *nbr;
  273. u_int16_t length = ntohs (ospfh->length);
  274. /* Get secret key. */
  275. ck = ospf_crypt_key_lookup (OSPF_IF_PARAM (oi, auth_crypt),
  276. ospfh->u.crypt.key_id);
  277. if (ck == NULL)
  278. {
  279. zlog_warn ("interface %s: ospf_check_md5 no key %d",
  280. IF_NAME (oi), ospfh->u.crypt.key_id);
  281. return 0;
  282. }
  283. /* check crypto seqnum. */
  284. nbr = ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id);
  285. if (nbr && ntohl(nbr->crypt_seqnum) > ntohl(ospfh->u.crypt.crypt_seqnum))
  286. {
  287. zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)",
  288. IF_NAME (oi),
  289. ntohl(ospfh->u.crypt.crypt_seqnum),
  290. ntohl(nbr->crypt_seqnum));
  291. return 0;
  292. }
  293. /* Generate a digest for the ospf packet - their digest + our digest. */
  294. memset(&ctx, 0, sizeof(ctx));
  295. MD5Init(&ctx);
  296. MD5Update(&ctx, ospfh, length);
  297. MD5Update(&ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
  298. MD5Final(digest, &ctx);
  299. /* compare the two */
  300. if (memcmp ((caddr_t)ospfh + length, digest, OSPF_AUTH_MD5_SIZE))
  301. {
  302. zlog_warn ("interface %s: ospf_check_md5 checksum mismatch",
  303. IF_NAME (oi));
  304. return 0;
  305. }
  306. /* save neighbor's crypt_seqnum */
  307. if (nbr)
  308. nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
  309. return 1;
  310. }
  311. /* This function is called from ospf_write(), it will detect the
  312. authentication scheme and if it is MD5, it will change the sequence
  313. and update the MD5 digest. */
  314. static int
  315. ospf_make_md5_digest (struct ospf_interface *oi, struct ospf_packet *op)
  316. {
  317. struct ospf_header *ospfh;
  318. unsigned char digest[OSPF_AUTH_MD5_SIZE] = {0};
  319. MD5_CTX ctx;
  320. void *ibuf;
  321. u_int32_t t;
  322. struct crypt_key *ck;
  323. const u_int8_t *auth_key;
  324. ibuf = STREAM_DATA (op->s);
  325. ospfh = (struct ospf_header *) ibuf;
  326. if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
  327. return 0;
  328. /* We do this here so when we dup a packet, we don't have to
  329. waste CPU rewriting other headers.
  330. Note that quagga_time /deliberately/ is not used here */
  331. t = (time(NULL) & 0xFFFFFFFF);
  332. if (t > oi->crypt_seqnum)
  333. oi->crypt_seqnum = t;
  334. else
  335. oi->crypt_seqnum++;
  336. ospfh->u.crypt.crypt_seqnum = htonl (oi->crypt_seqnum);
  337. /* Get MD5 Authentication key from auth_key list. */
  338. if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
  339. auth_key = (const u_int8_t *) digest;
  340. else
  341. {
  342. ck = listgetdata (listtail(OSPF_IF_PARAM (oi, auth_crypt)));
  343. auth_key = ck->auth_key;
  344. }
  345. /* Generate a digest for the entire packet + our secret key. */
  346. memset(&ctx, 0, sizeof(ctx));
  347. MD5Init(&ctx);
  348. MD5Update(&ctx, ibuf, ntohs (ospfh->length));
  349. MD5Update(&ctx, auth_key, OSPF_AUTH_MD5_SIZE);
  350. MD5Final(digest, &ctx);
  351. /* Append md5 digest to the end of the stream. */
  352. stream_put (op->s, digest, OSPF_AUTH_MD5_SIZE);
  353. /* We do *NOT* increment the OSPF header length. */
  354. op->length = ntohs (ospfh->length) + OSPF_AUTH_MD5_SIZE;
  355. if (stream_get_endp(op->s) != op->length)
  356. /* XXX size_t */
  357. zlog_warn("ospf_make_md5_digest: length mismatch stream %lu ospf_packet %u",
  358. (u_long)stream_get_endp(op->s), op->length);
  359. return OSPF_AUTH_MD5_SIZE;
  360. }
  361. static int
  362. ospf_ls_req_timer (struct thread *thread)
  363. {
  364. struct ospf_neighbor *nbr;
  365. nbr = THREAD_ARG (thread);
  366. nbr->t_ls_req = NULL;
  367. /* Send Link State Request. */
  368. if (ospf_ls_request_count (nbr))
  369. ospf_ls_req_send (nbr);
  370. /* Set Link State Request retransmission timer. */
  371. OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
  372. return 0;
  373. }
  374. void
  375. ospf_ls_req_event (struct ospf_neighbor *nbr)
  376. {
  377. if (nbr->t_ls_req)
  378. {
  379. thread_cancel (nbr->t_ls_req);
  380. nbr->t_ls_req = NULL;
  381. }
  382. nbr->t_ls_req = thread_add_event (master, ospf_ls_req_timer, nbr, 0);
  383. }
  384. /* Cyclic timer function. Fist registered in ospf_nbr_new () in
  385. ospf_neighbor.c */
  386. int
  387. ospf_ls_upd_timer (struct thread *thread)
  388. {
  389. struct ospf_neighbor *nbr;
  390. nbr = THREAD_ARG (thread);
  391. nbr->t_ls_upd = NULL;
  392. /* Send Link State Update. */
  393. if (ospf_ls_retransmit_count (nbr) > 0)
  394. {
  395. struct list *update;
  396. struct ospf_lsdb *lsdb;
  397. int i;
  398. int retransmit_interval;
  399. retransmit_interval = OSPF_IF_PARAM (nbr->oi, retransmit_interval);
  400. lsdb = &nbr->ls_rxmt;
  401. update = list_new ();
  402. for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
  403. {
  404. struct route_table *table = lsdb->type[i].db;
  405. struct route_node *rn;
  406. for (rn = route_top (table); rn; rn = route_next (rn))
  407. {
  408. struct ospf_lsa *lsa;
  409. if ((lsa = rn->info) != NULL)
  410. /* Don't retransmit an LSA if we received it within
  411. the last RxmtInterval seconds - this is to allow the
  412. neighbour a chance to acknowledge the LSA as it may
  413. have ben just received before the retransmit timer
  414. fired. This is a small tweak to what is in the RFC,
  415. but it will cut out out a lot of retransmit traffic
  416. - MAG */
  417. if (tv_cmp (tv_sub (recent_relative_time (), lsa->tv_recv),
  418. int2tv (retransmit_interval)) >= 0)
  419. listnode_add (update, rn->info);
  420. }
  421. }
  422. if (listcount (update) > 0)
  423. ospf_ls_upd_send (nbr, update, OSPF_SEND_PACKET_DIRECT);
  424. list_delete (update);
  425. }
  426. /* Set LS Update retransmission timer. */
  427. OSPF_NSM_TIMER_ON (nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd);
  428. return 0;
  429. }
  430. int
  431. ospf_ls_ack_timer (struct thread *thread)
  432. {
  433. struct ospf_interface *oi;
  434. oi = THREAD_ARG (thread);
  435. oi->t_ls_ack = NULL;
  436. /* Send Link State Acknowledgment. */
  437. if (listcount (oi->ls_ack) > 0)
  438. ospf_ls_ack_send_delayed (oi);
  439. /* Set LS Ack timer. */
  440. OSPF_ISM_TIMER_ON (oi->t_ls_ack, ospf_ls_ack_timer, oi->v_ls_ack);
  441. return 0;
  442. }
  443. #ifdef WANT_OSPF_WRITE_FRAGMENT
  444. static void
  445. ospf_write_frags (int fd, struct ospf_packet *op, struct ip *iph,
  446. struct msghdr *msg, unsigned int maxdatasize,
  447. unsigned int mtu, int flags, u_char type)
  448. {
  449. #define OSPF_WRITE_FRAG_SHIFT 3
  450. u_int16_t offset;
  451. struct iovec *iovp;
  452. int ret;
  453. assert ( op->length == stream_get_endp(op->s) );
  454. assert (msg->msg_iovlen == 2);
  455. /* we can but try.
  456. *
  457. * SunOS, BSD and BSD derived kernels likely will clear ip_id, as
  458. * well as the IP_MF flag, making this all quite pointless.
  459. *
  460. * However, for a system on which IP_MF is left alone, and ip_id left
  461. * alone or else which sets same ip_id for each fragment this might
  462. * work, eg linux.
  463. *
  464. * XXX-TODO: It would be much nicer to have the kernel's use their
  465. * existing fragmentation support to do this for us. Bugs/RFEs need to
  466. * be raised against the various kernels.
  467. */
  468. /* set More Frag */
  469. iph->ip_off |= IP_MF;
  470. /* ip frag offset is expressed in units of 8byte words */
  471. offset = maxdatasize >> OSPF_WRITE_FRAG_SHIFT;
  472. iovp = &msg->msg_iov[1];
  473. while ( (stream_get_endp(op->s) - stream_get_getp (op->s))
  474. > maxdatasize )
  475. {
  476. /* data length of this frag is to next offset value */
  477. iovp->iov_len = offset << OSPF_WRITE_FRAG_SHIFT;
  478. iph->ip_len = iovp->iov_len + sizeof (struct ip);
  479. assert (iph->ip_len <= mtu);
  480. sockopt_iphdrincl_swab_htosys (iph);
  481. ret = sendmsg (fd, msg, flags);
  482. sockopt_iphdrincl_swab_systoh (iph);
  483. if (ret < 0)
  484. zlog_warn ("*** ospf_write_frags: sendmsg failed to %s,"
  485. " id %d, off %d, len %d, mtu %u failed with %s",
  486. inet_ntoa (iph->ip_dst),
  487. iph->ip_id,
  488. iph->ip_off,
  489. iph->ip_len,
  490. mtu,
  491. safe_strerror (errno));
  492. if (IS_DEBUG_OSPF_PACKET (type - 1, SEND))
  493. {
  494. zlog_debug ("ospf_write_frags: sent id %d, off %d, len %d to %s\n",
  495. iph->ip_id, iph->ip_off, iph->ip_len,
  496. inet_ntoa (iph->ip_dst));
  497. if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
  498. {
  499. zlog_debug ("-----------------IP Header Dump----------------------");
  500. ospf_ip_header_dump (iph);
  501. zlog_debug ("-----------------------------------------------------");
  502. }
  503. }
  504. iph->ip_off += offset;
  505. stream_forward_getp (op->s, iovp->iov_len);
  506. iovp->iov_base = STREAM_PNT (op->s);
  507. }
  508. /* setup for final fragment */
  509. iovp->iov_len = stream_get_endp(op->s) - stream_get_getp (op->s);
  510. iph->ip_len = iovp->iov_len + sizeof (struct ip);
  511. iph->ip_off &= (~IP_MF);
  512. }
  513. #endif /* WANT_OSPF_WRITE_FRAGMENT */
  514. static int
  515. ospf_write (struct thread *thread)
  516. {
  517. struct ospf *ospf = THREAD_ARG (thread);
  518. struct ospf_interface *oi;
  519. struct ospf_packet *op;
  520. struct sockaddr_in sa_dst;
  521. struct ip iph;
  522. struct msghdr msg;
  523. struct iovec iov[2];
  524. u_char type;
  525. int ret;
  526. int flags = 0;
  527. struct listnode *node;
  528. #ifdef WANT_OSPF_WRITE_FRAGMENT
  529. static u_int16_t ipid = 0;
  530. u_int16_t maxdatasize;
  531. #endif /* WANT_OSPF_WRITE_FRAGMENT */
  532. #define OSPF_WRITE_IPHL_SHIFT 2
  533. ospf->t_write = NULL;
  534. node = listhead (ospf->oi_write_q);
  535. assert (node);
  536. oi = listgetdata (node);
  537. assert (oi);
  538. #ifdef WANT_OSPF_WRITE_FRAGMENT
  539. /* seed ipid static with low order bits of time */
  540. if (ipid == 0)
  541. ipid = (time(NULL) & 0xffff);
  542. /* convenience - max OSPF data per packet,
  543. * and reliability - not more data, than our
  544. * socket can accept
  545. */
  546. maxdatasize = MIN (oi->ifp->mtu, ospf->maxsndbuflen) -
  547. sizeof (struct ip);
  548. #endif /* WANT_OSPF_WRITE_FRAGMENT */
  549. /* Get one packet from queue. */
  550. op = ospf_fifo_head (oi->obuf);
  551. assert (op);
  552. assert (op->length >= OSPF_HEADER_SIZE);
  553. if (op->dst.s_addr == htonl (OSPF_ALLSPFROUTERS)
  554. || op->dst.s_addr == htonl (OSPF_ALLDROUTERS))
  555. ospf_if_ipmulticast (ospf, oi->address, oi->ifp->ifindex);
  556. /* Rewrite the md5 signature & update the seq */
  557. ospf_make_md5_digest (oi, op);
  558. /* Retrieve OSPF packet type. */
  559. stream_set_getp (op->s, 1);
  560. type = stream_getc (op->s);
  561. /* reset get pointer */
  562. stream_set_getp (op->s, 0);
  563. memset (&iph, 0, sizeof (struct ip));
  564. memset (&sa_dst, 0, sizeof (sa_dst));
  565. sa_dst.sin_family = AF_INET;
  566. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  567. sa_dst.sin_len = sizeof(sa_dst);
  568. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  569. sa_dst.sin_addr = op->dst;
  570. sa_dst.sin_port = htons (0);
  571. /* Set DONTROUTE flag if dst is unicast. */
  572. if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
  573. if (!IN_MULTICAST (htonl (op->dst.s_addr)))
  574. flags = MSG_DONTROUTE;
  575. iph.ip_hl = sizeof (struct ip) >> OSPF_WRITE_IPHL_SHIFT;
  576. /* it'd be very strange for header to not be 4byte-word aligned but.. */
  577. if ( sizeof (struct ip)
  578. > (unsigned int)(iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) )
  579. iph.ip_hl++; /* we presume sizeof struct ip cant overflow ip_hl.. */
  580. iph.ip_v = IPVERSION;
  581. iph.ip_tos = IPTOS_PREC_INTERNETCONTROL;
  582. iph.ip_len = (iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) + op->length;
  583. #if defined(__DragonFly__)
  584. /*
  585. * DragonFly's raw socket expects ip_len/ip_off in network byte order.
  586. */
  587. iph.ip_len = htons(iph.ip_len);
  588. #endif
  589. #ifdef WANT_OSPF_WRITE_FRAGMENT
  590. /* XXX-MT: not thread-safe at all..
  591. * XXX: this presumes this is only programme sending OSPF packets
  592. * otherwise, no guarantee ipid will be unique
  593. */
  594. iph.ip_id = ++ipid;
  595. #endif /* WANT_OSPF_WRITE_FRAGMENT */
  596. iph.ip_off = 0;
  597. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  598. iph.ip_ttl = OSPF_VL_IP_TTL;
  599. else
  600. iph.ip_ttl = OSPF_IP_TTL;
  601. iph.ip_p = IPPROTO_OSPFIGP;
  602. iph.ip_sum = 0;
  603. iph.ip_src.s_addr = oi->address->u.prefix4.s_addr;
  604. iph.ip_dst.s_addr = op->dst.s_addr;
  605. memset (&msg, 0, sizeof (msg));
  606. msg.msg_name = (caddr_t) &sa_dst;
  607. msg.msg_namelen = sizeof (sa_dst);
  608. msg.msg_iov = iov;
  609. msg.msg_iovlen = 2;
  610. iov[0].iov_base = (char*)&iph;
  611. iov[0].iov_len = iph.ip_hl << OSPF_WRITE_IPHL_SHIFT;
  612. iov[1].iov_base = STREAM_PNT (op->s);
  613. iov[1].iov_len = op->length;
  614. /* Sadly we can not rely on kernels to fragment packets because of either
  615. * IP_HDRINCL and/or multicast destination being set.
  616. */
  617. #ifdef WANT_OSPF_WRITE_FRAGMENT
  618. if ( op->length > maxdatasize )
  619. ospf_write_frags (ospf->fd, op, &iph, &msg, maxdatasize,
  620. oi->ifp->mtu, flags, type);
  621. #endif /* WANT_OSPF_WRITE_FRAGMENT */
  622. /* send final fragment (could be first) */
  623. sockopt_iphdrincl_swab_htosys (&iph);
  624. ret = sendmsg (ospf->fd, &msg, flags);
  625. sockopt_iphdrincl_swab_systoh (&iph);
  626. if (ret < 0)
  627. zlog_warn ("*** sendmsg in ospf_write failed to %s, "
  628. "id %d, off %d, len %d, interface %s, mtu %u: %s",
  629. inet_ntoa (iph.ip_dst), iph.ip_id, iph.ip_off, iph.ip_len,
  630. oi->ifp->name, oi->ifp->mtu, safe_strerror (errno));
  631. /* Show debug sending packet. */
  632. if (IS_DEBUG_OSPF_PACKET (type - 1, SEND))
  633. {
  634. if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
  635. {
  636. zlog_debug ("-----------------------------------------------------");
  637. ospf_ip_header_dump (&iph);
  638. stream_set_getp (op->s, 0);
  639. ospf_packet_dump (op->s);
  640. }
  641. zlog_debug ("%s sent to [%s] via [%s].",
  642. LOOKUP (ospf_packet_type_str, type), inet_ntoa (op->dst),
  643. IF_NAME (oi));
  644. if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
  645. zlog_debug ("-----------------------------------------------------");
  646. }
  647. /* Now delete packet from queue. */
  648. ospf_packet_delete (oi);
  649. /* Move this interface to the tail of write_q to
  650. serve everyone in a round robin fashion */
  651. listnode_move_to_tail (ospf->oi_write_q, node);
  652. if (ospf_fifo_head (oi->obuf) == NULL)
  653. {
  654. oi->on_write_q = 0;
  655. list_delete_node (ospf->oi_write_q, node);
  656. }
  657. /* If packets still remain in queue, call write thread. */
  658. if (!list_isempty (ospf->oi_write_q))
  659. ospf->t_write =
  660. thread_add_write (master, ospf_write, ospf, ospf->fd);
  661. return 0;
  662. }
  663. /* OSPF Hello message read -- RFC2328 Section 10.5. */
  664. static void
  665. ospf_hello (struct ip *iph, struct ospf_header *ospfh,
  666. struct stream * s, struct ospf_interface *oi, int size)
  667. {
  668. struct ospf_hello *hello;
  669. struct ospf_neighbor *nbr;
  670. int old_state;
  671. struct prefix p;
  672. /* increment statistics. */
  673. oi->hello_in++;
  674. hello = (struct ospf_hello *) STREAM_PNT (s);
  675. /* If Hello is myself, silently discard. */
  676. if (IPV4_ADDR_SAME (&ospfh->router_id, &oi->ospf->router_id))
  677. {
  678. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  679. {
  680. zlog_debug ("ospf_header[%s/%s]: selforiginated, "
  681. "dropping.",
  682. LOOKUP (ospf_packet_type_str, ospfh->type),
  683. inet_ntoa (iph->ip_src));
  684. }
  685. return;
  686. }
  687. /* get neighbor prefix. */
  688. p.family = AF_INET;
  689. p.prefixlen = ip_masklen (hello->network_mask);
  690. p.u.prefix4 = iph->ip_src;
  691. /* Compare network mask. */
  692. /* Checking is ignored for Point-to-Point and Virtual link. */
  693. if (oi->type != OSPF_IFTYPE_POINTOPOINT
  694. && oi->type != OSPF_IFTYPE_VIRTUALLINK)
  695. if (oi->address->prefixlen != p.prefixlen)
  696. {
  697. zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).",
  698. inet_ntoa(ospfh->router_id), IF_NAME(oi),
  699. (int)oi->address->prefixlen, (int)p.prefixlen);
  700. return;
  701. }
  702. /* Compare Router Dead Interval. */
  703. if (OSPF_IF_PARAM (oi, v_wait) != ntohl (hello->dead_interval))
  704. {
  705. zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch "
  706. "(expected %u, but received %u).",
  707. inet_ntoa(ospfh->router_id),
  708. OSPF_IF_PARAM(oi, v_wait), ntohl(hello->dead_interval));
  709. return;
  710. }
  711. /* Compare Hello Interval - ignored if fast-hellos are set. */
  712. if (OSPF_IF_PARAM (oi, fast_hello) == 0)
  713. {
  714. if (OSPF_IF_PARAM (oi, v_hello) != ntohs (hello->hello_interval))
  715. {
  716. zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch "
  717. "(expected %u, but received %u).",
  718. inet_ntoa(ospfh->router_id),
  719. OSPF_IF_PARAM(oi, v_hello), ntohs(hello->hello_interval));
  720. return;
  721. }
  722. }
  723. if (IS_DEBUG_OSPF_EVENT)
  724. zlog_debug ("Packet %s [Hello:RECV]: Options %s",
  725. inet_ntoa (ospfh->router_id),
  726. ospf_options_dump (hello->options));
  727. /* Compare options. */
  728. #define REJECT_IF_TBIT_ON 1 /* XXX */
  729. #ifdef REJECT_IF_TBIT_ON
  730. if (CHECK_FLAG (hello->options, OSPF_OPTION_MT))
  731. {
  732. /*
  733. * This router does not support non-zero TOS.
  734. * Drop this Hello packet not to establish neighbor relationship.
  735. */
  736. zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.",
  737. inet_ntoa (ospfh->router_id));
  738. return;
  739. }
  740. #endif /* REJECT_IF_TBIT_ON */
  741. if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)
  742. && CHECK_FLAG (hello->options, OSPF_OPTION_O))
  743. {
  744. /*
  745. * This router does know the correct usage of O-bit
  746. * the bit should be set in DD packet only.
  747. */
  748. zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
  749. inet_ntoa (ospfh->router_id));
  750. #ifdef STRICT_OBIT_USAGE_CHECK
  751. return; /* Reject this packet. */
  752. #else /* STRICT_OBIT_USAGE_CHECK */
  753. UNSET_FLAG (hello->options, OSPF_OPTION_O); /* Ignore O-bit. */
  754. #endif /* STRICT_OBIT_USAGE_CHECK */
  755. }
  756. /* new for NSSA is to ensure that NP is on and E is off */
  757. if (oi->area->external_routing == OSPF_AREA_NSSA)
  758. {
  759. if (! (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_NP)
  760. && CHECK_FLAG (hello->options, OSPF_OPTION_NP)
  761. && ! CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E)
  762. && ! CHECK_FLAG (hello->options, OSPF_OPTION_E)))
  763. {
  764. zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh->router_id), OPTIONS (oi), hello->options);
  765. return;
  766. }
  767. if (IS_DEBUG_OSPF_NSSA)
  768. zlog_debug ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh->router_id));
  769. }
  770. else
  771. /* The setting of the E-bit found in the Hello Packet's Options
  772. field must match this area's ExternalRoutingCapability A
  773. mismatch causes processing to stop and the packet to be
  774. dropped. The setting of the rest of the bits in the Hello
  775. Packet's Options field should be ignored. */
  776. if (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) !=
  777. CHECK_FLAG (hello->options, OSPF_OPTION_E))
  778. {
  779. zlog_warn ("Packet %s [Hello:RECV]: my options: %x, his options %x",
  780. inet_ntoa(ospfh->router_id), OPTIONS (oi), hello->options);
  781. return;
  782. }
  783. /* get neighbour struct */
  784. nbr = ospf_nbr_get (oi, ospfh, iph, &p);
  785. /* neighbour must be valid, ospf_nbr_get creates if none existed */
  786. assert (nbr);
  787. old_state = nbr->state;
  788. /* Add event to thread. */
  789. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  790. /* RFC2328 Section 9.5.1
  791. If the router is not eligible to become Designated Router,
  792. (snip) It must also send an Hello Packet in reply to an
  793. Hello Packet received from any eligible neighbor (other than
  794. the current Designated Router and Backup Designated Router). */
  795. if (oi->type == OSPF_IFTYPE_NBMA)
  796. if (PRIORITY(oi) == 0 && hello->priority > 0
  797. && IPV4_ADDR_CMP(&DR(oi), &iph->ip_src)
  798. && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src))
  799. OSPF_NSM_TIMER_ON (nbr->t_hello_reply, ospf_hello_reply_timer,
  800. OSPF_HELLO_REPLY_DELAY);
  801. /* on NBMA network type, it happens to receive bidirectional Hello packet
  802. without advance 1-Way Received event.
  803. To avoid incorrect DR-seletion, raise 1-Way Received event.*/
  804. if (oi->type == OSPF_IFTYPE_NBMA &&
  805. (old_state == NSM_Down || old_state == NSM_Attempt))
  806. {
  807. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived);
  808. nbr->priority = hello->priority;
  809. nbr->d_router = hello->d_router;
  810. nbr->bd_router = hello->bd_router;
  811. return;
  812. }
  813. if (ospf_nbr_bidirectional (&oi->ospf->router_id, hello->neighbors,
  814. size - OSPF_HELLO_MIN_SIZE))
  815. {
  816. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_TwoWayReceived);
  817. nbr->options |= hello->options;
  818. }
  819. else
  820. {
  821. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived);
  822. /* Set neighbor information. */
  823. nbr->priority = hello->priority;
  824. nbr->d_router = hello->d_router;
  825. nbr->bd_router = hello->bd_router;
  826. return;
  827. }
  828. /* If neighbor itself declares DR and no BDR exists,
  829. cause event BackupSeen */
  830. if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router))
  831. if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting)
  832. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
  833. /* neighbor itself declares BDR. */
  834. if (oi->state == ISM_Waiting &&
  835. IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router))
  836. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
  837. /* had not previously. */
  838. if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router) &&
  839. IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->d_router)) ||
  840. (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->d_router) &&
  841. IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->d_router)))
  842. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  843. /* had not previously. */
  844. if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router) &&
  845. IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->bd_router)) ||
  846. (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->bd_router) &&
  847. IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->bd_router)))
  848. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  849. /* Neighbor priority check. */
  850. if (nbr->priority >= 0 && nbr->priority != hello->priority)
  851. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  852. /* Set neighbor information. */
  853. nbr->priority = hello->priority;
  854. nbr->d_router = hello->d_router;
  855. nbr->bd_router = hello->bd_router;
  856. }
  857. /* Save DD flags/options/Seqnum received. */
  858. static void
  859. ospf_db_desc_save_current (struct ospf_neighbor *nbr,
  860. struct ospf_db_desc *dd)
  861. {
  862. nbr->last_recv.flags = dd->flags;
  863. nbr->last_recv.options = dd->options;
  864. nbr->last_recv.dd_seqnum = ntohl (dd->dd_seqnum);
  865. }
  866. /* Process rest of DD packet. */
  867. static void
  868. ospf_db_desc_proc (struct stream *s, struct ospf_interface *oi,
  869. struct ospf_neighbor *nbr, struct ospf_db_desc *dd,
  870. u_int16_t size)
  871. {
  872. struct ospf_lsa *new, *find;
  873. struct lsa_header *lsah;
  874. stream_forward_getp (s, OSPF_DB_DESC_MIN_SIZE);
  875. for (size -= OSPF_DB_DESC_MIN_SIZE;
  876. size >= OSPF_LSA_HEADER_SIZE; size -= OSPF_LSA_HEADER_SIZE)
  877. {
  878. lsah = (struct lsa_header *) STREAM_PNT (s);
  879. stream_forward_getp (s, OSPF_LSA_HEADER_SIZE);
  880. /* Unknown LS type. */
  881. if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
  882. {
  883. zlog_warn ("Packet [DD:RECV]: Unknown LS type %d.", lsah->type);
  884. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  885. return;
  886. }
  887. if (IS_OPAQUE_LSA (lsah->type)
  888. && ! CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  889. {
  890. zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
  891. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  892. return;
  893. }
  894. switch (lsah->type)
  895. {
  896. case OSPF_AS_EXTERNAL_LSA:
  897. case OSPF_OPAQUE_AS_LSA:
  898. /* Check for stub area. Reject if AS-External from stub but
  899. allow if from NSSA. */
  900. if (oi->area->external_routing == OSPF_AREA_STUB)
  901. {
  902. zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
  903. lsah->type, inet_ntoa (lsah->id),
  904. (oi->area->external_routing == OSPF_AREA_STUB) ?\
  905. "STUB" : "NSSA");
  906. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  907. return;
  908. }
  909. break;
  910. default:
  911. break;
  912. }
  913. /* Create LS-request object. */
  914. new = ospf_ls_request_new (lsah);
  915. /* Lookup received LSA, then add LS request list. */
  916. find = ospf_lsa_lookup_by_header (oi->area, lsah);
  917. /* ospf_lsa_more_recent is fine with NULL pointers */
  918. switch (ospf_lsa_more_recent (find, new))
  919. {
  920. case -1:
  921. /* Neighbour has a more recent LSA, we must request it */
  922. ospf_ls_request_add (nbr, new);
  923. case 0:
  924. /* If we have a copy of this LSA, it's either less recent
  925. * and we're requesting it from neighbour (the case above), or
  926. * it's as recent and we both have same copy (this case).
  927. *
  928. * In neither of these two cases is there any point in
  929. * describing our copy of the LSA to the neighbour in a
  930. * DB-Summary packet, if we're still intending to do so.
  931. *
  932. * See: draft-ogier-ospf-dbex-opt-00.txt, describing the
  933. * backward compatible optimisation to OSPF DB Exchange /
  934. * DB Description process implemented here.
  935. */
  936. if (find)
  937. ospf_lsdb_delete (&nbr->db_sum, find);
  938. ospf_lsa_discard (new);
  939. break;
  940. default:
  941. /* We have the more recent copy, nothing specific to do:
  942. * - no need to request neighbours stale copy
  943. * - must leave DB summary list copy alone
  944. */
  945. if (IS_DEBUG_OSPF_EVENT)
  946. zlog_debug ("Packet [DD:RECV]: LSA received Type %d, "
  947. "ID %s is not recent.", lsah->type, inet_ntoa (lsah->id));
  948. ospf_lsa_discard (new);
  949. }
  950. }
  951. /* Master */
  952. if (IS_SET_DD_MS (nbr->dd_flags))
  953. {
  954. nbr->dd_seqnum++;
  955. /* Both sides have no More, then we're done with Exchange */
  956. if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags))
  957. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
  958. else
  959. ospf_db_desc_send (nbr);
  960. }
  961. /* Slave */
  962. else
  963. {
  964. nbr->dd_seqnum = ntohl (dd->dd_seqnum);
  965. /* Send DD packet in reply.
  966. *
  967. * Must be done to acknowledge the Master's DD, regardless of
  968. * whether we have more LSAs ourselves to describe.
  969. *
  970. * This function will clear the 'More' bit, if after this DD
  971. * we have no more LSAs to describe to the master..
  972. */
  973. ospf_db_desc_send (nbr);
  974. /* Slave can raise ExchangeDone now, if master is also done */
  975. if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags))
  976. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
  977. }
  978. /* Save received neighbor values from DD. */
  979. ospf_db_desc_save_current (nbr, dd);
  980. }
  981. static int
  982. ospf_db_desc_is_dup (struct ospf_db_desc *dd, struct ospf_neighbor *nbr)
  983. {
  984. /* Is DD duplicated? */
  985. if (dd->options == nbr->last_recv.options &&
  986. dd->flags == nbr->last_recv.flags &&
  987. dd->dd_seqnum == htonl (nbr->last_recv.dd_seqnum))
  988. return 1;
  989. return 0;
  990. }
  991. /* OSPF Database Description message read -- RFC2328 Section 10.6. */
  992. static void
  993. ospf_db_desc (struct ip *iph, struct ospf_header *ospfh,
  994. struct stream *s, struct ospf_interface *oi, u_int16_t size)
  995. {
  996. struct ospf_db_desc *dd;
  997. struct ospf_neighbor *nbr;
  998. /* Increment statistics. */
  999. oi->db_desc_in++;
  1000. dd = (struct ospf_db_desc *) STREAM_PNT (s);
  1001. nbr = ospf_nbr_lookup (oi, iph, ospfh);
  1002. if (nbr == NULL)
  1003. {
  1004. zlog_warn ("Packet[DD]: Unknown Neighbor %s",
  1005. inet_ntoa (ospfh->router_id));
  1006. return;
  1007. }
  1008. /* Check MTU. */
  1009. if ((OSPF_IF_PARAM (oi, mtu_ignore) == 0) &&
  1010. (ntohs (dd->mtu) > oi->ifp->mtu))
  1011. {
  1012. zlog_warn ("Packet[DD]: Neighbor %s MTU %u is larger than [%s]'s MTU %u",
  1013. inet_ntoa (nbr->router_id), ntohs (dd->mtu),
  1014. IF_NAME (oi), oi->ifp->mtu);
  1015. return;
  1016. }
  1017. /*
  1018. * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not
  1019. * required. In fact at least JunOS sends DD packets with P bit clear.
  1020. * Until proper solution is developped, this hack should help.
  1021. *
  1022. * Update: According to the RFCs, N bit is specified /only/ for Hello
  1023. * options, unfortunately its use in DD options is not specified. Hence some
  1024. * implementations follow E-bit semantics and set it in DD options, and some
  1025. * treat it as unspecified and hence follow the directive "default for
  1026. * options is clear", ie unset.
  1027. *
  1028. * Reset the flag, as ospfd follows E-bit semantics.
  1029. */
  1030. if ( (oi->area->external_routing == OSPF_AREA_NSSA)
  1031. && (CHECK_FLAG (nbr->options, OSPF_OPTION_NP))
  1032. && (!CHECK_FLAG (dd->options, OSPF_OPTION_NP)) )
  1033. {
  1034. if (IS_DEBUG_OSPF_EVENT)
  1035. zlog_debug ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
  1036. inet_ntoa (nbr->router_id) );
  1037. SET_FLAG (dd->options, OSPF_OPTION_NP);
  1038. }
  1039. #ifdef REJECT_IF_TBIT_ON
  1040. if (CHECK_FLAG (dd->options, OSPF_OPTION_MT))
  1041. {
  1042. /*
  1043. * In Hello protocol, optional capability must have checked
  1044. * to prevent this T-bit enabled router be my neighbor.
  1045. */
  1046. zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr->router_id));
  1047. return;
  1048. }
  1049. #endif /* REJECT_IF_TBIT_ON */
  1050. if (CHECK_FLAG (dd->options, OSPF_OPTION_O)
  1051. && !CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  1052. {
  1053. /*
  1054. * This node is not configured to handle O-bit, for now.
  1055. * Clear it to ignore unsupported capability proposed by neighbor.
  1056. */
  1057. UNSET_FLAG (dd->options, OSPF_OPTION_O);
  1058. }
  1059. /* Add event to thread. */
  1060. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  1061. /* Process DD packet by neighbor status. */
  1062. switch (nbr->state)
  1063. {
  1064. case NSM_Down:
  1065. case NSM_Attempt:
  1066. case NSM_TwoWay:
  1067. zlog_warn ("Packet[DD]: Neighbor %s state is %s, packet discarded.",
  1068. inet_ntoa(nbr->router_id),
  1069. LOOKUP (ospf_nsm_state_msg, nbr->state));
  1070. break;
  1071. case NSM_Init:
  1072. OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived);
  1073. /* If the new state is ExStart, the processing of the current
  1074. packet should then continue in this new state by falling
  1075. through to case ExStart below. */
  1076. if (nbr->state != NSM_ExStart)
  1077. break;
  1078. case NSM_ExStart:
  1079. /* Initial DBD */
  1080. if ((IS_SET_DD_ALL (dd->flags) == OSPF_DD_FLAG_ALL) &&
  1081. (size == OSPF_DB_DESC_MIN_SIZE))
  1082. {
  1083. if (IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) > 0)
  1084. {
  1085. /* We're Slave---obey */
  1086. zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Slave).",
  1087. inet_ntoa(nbr->router_id));
  1088. nbr->dd_seqnum = ntohl (dd->dd_seqnum);
  1089. /* Reset I/MS */
  1090. UNSET_FLAG (nbr->dd_flags, (OSPF_DD_FLAG_MS|OSPF_DD_FLAG_I));
  1091. }
  1092. else
  1093. {
  1094. /* We're Master, ignore the initial DBD from Slave */
  1095. zlog_info ("Packet[DD]: Neighbor %s: Initial DBD from Slave, "
  1096. "ignoring.", inet_ntoa(nbr->router_id));
  1097. break;
  1098. }
  1099. }
  1100. /* Ack from the Slave */
  1101. else if (!IS_SET_DD_MS (dd->flags) && !IS_SET_DD_I (dd->flags) &&
  1102. ntohl (dd->dd_seqnum) == nbr->dd_seqnum &&
  1103. IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) < 0)
  1104. {
  1105. zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Master).",
  1106. inet_ntoa(nbr->router_id));
  1107. /* Reset I, leaving MS */
  1108. UNSET_FLAG (nbr->dd_flags, OSPF_DD_FLAG_I);
  1109. }
  1110. else
  1111. {
  1112. zlog_warn ("Packet[DD]: Neighbor %s Negotiation fails.",
  1113. inet_ntoa(nbr->router_id));
  1114. break;
  1115. }
  1116. /* This is where the real Options are saved */
  1117. nbr->options = dd->options;
  1118. if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  1119. {
  1120. if (IS_DEBUG_OSPF_EVENT)
  1121. zlog_debug ("Neighbor[%s] is %sOpaque-capable.",
  1122. inet_ntoa (nbr->router_id),
  1123. CHECK_FLAG (nbr->options, OSPF_OPTION_O) ? "" : "NOT ");
  1124. if (! CHECK_FLAG (nbr->options, OSPF_OPTION_O)
  1125. && IPV4_ADDR_SAME (&DR (oi), &nbr->address.u.prefix4))
  1126. {
  1127. zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; "
  1128. "Opaque-LSAs cannot be reliably advertised "
  1129. "in this network.",
  1130. inet_ntoa (nbr->router_id));
  1131. /* This situation is undesirable, but not a real error. */
  1132. }
  1133. }
  1134. OSPF_NSM_EVENT_EXECUTE (nbr, NSM_NegotiationDone);
  1135. /* continue processing rest of packet. */
  1136. ospf_db_desc_proc (s, oi, nbr, dd, size);
  1137. break;
  1138. case NSM_Exchange:
  1139. if (ospf_db_desc_is_dup (dd, nbr))
  1140. {
  1141. if (IS_SET_DD_MS (nbr->dd_flags))
  1142. /* Master: discard duplicated DD packet. */
  1143. zlog_info ("Packet[DD] (Master): Neighbor %s packet duplicated.",
  1144. inet_ntoa (nbr->router_id));
  1145. else
  1146. /* Slave: cause to retransmit the last Database Description. */
  1147. {
  1148. zlog_info ("Packet[DD] [Slave]: Neighbor %s packet duplicated.",
  1149. inet_ntoa (nbr->router_id));
  1150. ospf_db_desc_resend (nbr);
  1151. }
  1152. break;
  1153. }
  1154. /* Otherwise DD packet should be checked. */
  1155. /* Check Master/Slave bit mismatch */
  1156. if (IS_SET_DD_MS (dd->flags) != IS_SET_DD_MS (nbr->last_recv.flags))
  1157. {
  1158. zlog_warn ("Packet[DD]: Neighbor %s MS-bit mismatch.",
  1159. inet_ntoa(nbr->router_id));
  1160. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1161. if (IS_DEBUG_OSPF_EVENT)
  1162. zlog_debug ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
  1163. dd->flags, nbr->dd_flags);
  1164. break;
  1165. }
  1166. /* Check initialize bit is set. */
  1167. if (IS_SET_DD_I (dd->flags))
  1168. {
  1169. zlog_info ("Packet[DD]: Neighbor %s I-bit set.",
  1170. inet_ntoa(nbr->router_id));
  1171. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1172. break;
  1173. }
  1174. /* Check DD Options. */
  1175. if (dd->options != nbr->options)
  1176. {
  1177. #ifdef ORIGINAL_CODING
  1178. /* Save the new options for debugging */
  1179. nbr->options = dd->options;
  1180. #endif /* ORIGINAL_CODING */
  1181. zlog_warn ("Packet[DD]: Neighbor %s options mismatch.",
  1182. inet_ntoa(nbr->router_id));
  1183. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1184. break;
  1185. }
  1186. /* Check DD sequence number. */
  1187. if ((IS_SET_DD_MS (nbr->dd_flags) &&
  1188. ntohl (dd->dd_seqnum) != nbr->dd_seqnum) ||
  1189. (!IS_SET_DD_MS (nbr->dd_flags) &&
  1190. ntohl (dd->dd_seqnum) != nbr->dd_seqnum + 1))
  1191. {
  1192. zlog_warn ("Packet[DD]: Neighbor %s sequence number mismatch.",
  1193. inet_ntoa(nbr->router_id));
  1194. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1195. break;
  1196. }
  1197. /* Continue processing rest of packet. */
  1198. ospf_db_desc_proc (s, oi, nbr, dd, size);
  1199. break;
  1200. case NSM_Loading:
  1201. case NSM_Full:
  1202. if (ospf_db_desc_is_dup (dd, nbr))
  1203. {
  1204. if (IS_SET_DD_MS (nbr->dd_flags))
  1205. {
  1206. /* Master should discard duplicate DD packet. */
  1207. zlog_info ("Packet[DD]: Neighbor %s duplicated, "
  1208. "packet discarded.",
  1209. inet_ntoa(nbr->router_id));
  1210. break;
  1211. }
  1212. else
  1213. {
  1214. struct timeval t, now;
  1215. quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
  1216. t = tv_sub (now, nbr->last_send_ts);
  1217. if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0)
  1218. {
  1219. /* In states Loading and Full the slave must resend
  1220. its last Database Description packet in response to
  1221. duplicate Database Description packets received
  1222. from the master. For this reason the slave must
  1223. wait RouterDeadInterval seconds before freeing the
  1224. last Database Description packet. Reception of a
  1225. Database Description packet from the master after
  1226. this interval will generate a SeqNumberMismatch
  1227. neighbor event. RFC2328 Section 10.8 */
  1228. ospf_db_desc_resend (nbr);
  1229. break;
  1230. }
  1231. }
  1232. }
  1233. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1234. break;
  1235. default:
  1236. zlog_warn ("Packet[DD]: Neighbor %s NSM illegal status %u.",
  1237. inet_ntoa(nbr->router_id), nbr->state);
  1238. break;
  1239. }
  1240. }
  1241. #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
  1242. /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
  1243. static void
  1244. ospf_ls_req (struct ip *iph, struct ospf_header *ospfh,
  1245. struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1246. {
  1247. struct ospf_neighbor *nbr;
  1248. u_int32_t ls_type;
  1249. struct in_addr ls_id;
  1250. struct in_addr adv_router;
  1251. struct ospf_lsa *find;
  1252. struct list *ls_upd;
  1253. unsigned int length;
  1254. /* Increment statistics. */
  1255. oi->ls_req_in++;
  1256. nbr = ospf_nbr_lookup (oi, iph, ospfh);
  1257. if (nbr == NULL)
  1258. {
  1259. zlog_warn ("Link State Request: Unknown Neighbor %s.",
  1260. inet_ntoa (ospfh->router_id));
  1261. return;
  1262. }
  1263. /* Add event to thread. */
  1264. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  1265. /* Neighbor State should be Exchange or later. */
  1266. if (nbr->state != NSM_Exchange &&
  1267. nbr->state != NSM_Loading &&
  1268. nbr->state != NSM_Full)
  1269. {
  1270. zlog_warn ("Link State Request received from %s: "
  1271. "Neighbor state is %s, packet discarded.",
  1272. inet_ntoa (ospfh->router_id),
  1273. LOOKUP (ospf_nsm_state_msg, nbr->state));
  1274. return;
  1275. }
  1276. /* Send Link State Update for ALL requested LSAs. */
  1277. ls_upd = list_new ();
  1278. length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
  1279. while (size >= OSPF_LSA_KEY_SIZE)
  1280. {
  1281. /* Get one slice of Link State Request. */
  1282. ls_type = stream_getl (s);
  1283. ls_id.s_addr = stream_get_ipv4 (s);
  1284. adv_router.s_addr = stream_get_ipv4 (s);
  1285. /* Verify LSA type. */
  1286. if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA)
  1287. {
  1288. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1289. list_delete (ls_upd);
  1290. return;
  1291. }
  1292. /* Search proper LSA in LSDB. */
  1293. find = ospf_lsa_lookup (oi->area, ls_type, ls_id, adv_router);
  1294. if (find == NULL)
  1295. {
  1296. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1297. list_delete (ls_upd);
  1298. return;
  1299. }
  1300. /* Packet overflows MTU size, send immediately. */
  1301. if (length + ntohs (find->data->length) > ospf_packet_max (oi))
  1302. {
  1303. if (oi->type == OSPF_IFTYPE_NBMA)
  1304. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
  1305. else
  1306. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
  1307. /* Only remove list contents. Keep ls_upd. */
  1308. list_delete_all_node (ls_upd);
  1309. length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
  1310. }
  1311. /* Append LSA to update list. */
  1312. listnode_add (ls_upd, find);
  1313. length += ntohs (find->data->length);
  1314. size -= OSPF_LSA_KEY_SIZE;
  1315. }
  1316. /* Send rest of Link State Update. */
  1317. if (listcount (ls_upd) > 0)
  1318. {
  1319. if (oi->type == OSPF_IFTYPE_NBMA)
  1320. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
  1321. else
  1322. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
  1323. list_delete (ls_upd);
  1324. }
  1325. else
  1326. list_free (ls_upd);
  1327. }
  1328. /* Get the list of LSAs from Link State Update packet.
  1329. And process some validation -- RFC2328 Section 13. (1)-(2). */
  1330. static struct list *
  1331. ospf_ls_upd_list_lsa (struct ospf_neighbor *nbr, struct stream *s,
  1332. struct ospf_interface *oi, size_t size)
  1333. {
  1334. u_int16_t count, sum;
  1335. u_int32_t length;
  1336. struct lsa_header *lsah;
  1337. struct ospf_lsa *lsa;
  1338. struct list *lsas;
  1339. lsas = list_new ();
  1340. count = stream_getl (s);
  1341. size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */
  1342. for (; size >= OSPF_LSA_HEADER_SIZE && count > 0;
  1343. size -= length, stream_forward_getp (s, length), count--)
  1344. {
  1345. lsah = (struct lsa_header *) STREAM_PNT (s);
  1346. length = ntohs (lsah->length);
  1347. if (length > size)
  1348. {
  1349. zlog_warn ("Link State Update: LSA length exceeds packet size.");
  1350. break;
  1351. }
  1352. /* Validate the LSA's LS checksum. */
  1353. sum = lsah->checksum;
  1354. if (! ospf_lsa_checksum_valid (lsah))
  1355. {
  1356. /* (bug #685) more details in a one-line message make it possible
  1357. * to identify problem source on the one hand and to have a better
  1358. * chance to compress repeated messages in syslog on the other */
  1359. zlog_warn ("Link State Update: LSA checksum error %x/%x, ID=%s from: nbr %s, router ID %s, adv router %s",
  1360. sum, lsah->checksum, inet_ntoa (lsah->id),
  1361. inet_ntoa (nbr->src), inet_ntoa (nbr->router_id),
  1362. inet_ntoa (lsah->adv_router));
  1363. continue;
  1364. }
  1365. /* Examine the LSA's LS type. */
  1366. if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
  1367. {
  1368. zlog_warn ("Link State Update: Unknown LS type %d", lsah->type);
  1369. continue;
  1370. }
  1371. /*
  1372. * What if the received LSA's age is greater than MaxAge?
  1373. * Treat it as a MaxAge case -- endo.
  1374. */
  1375. if (ntohs (lsah->ls_age) > OSPF_LSA_MAXAGE)
  1376. lsah->ls_age = htons (OSPF_LSA_MAXAGE);
  1377. if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  1378. {
  1379. #ifdef STRICT_OBIT_USAGE_CHECK
  1380. if ((IS_OPAQUE_LSA(lsah->type) &&
  1381. ! CHECK_FLAG (lsah->options, OSPF_OPTION_O))
  1382. || (! IS_OPAQUE_LSA(lsah->type) &&
  1383. CHECK_FLAG (lsah->options, OSPF_OPTION_O)))
  1384. {
  1385. /*
  1386. * This neighbor must know the exact usage of O-bit;
  1387. * the bit will be set in Type-9,10,11 LSAs only.
  1388. */
  1389. zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah->type, inet_ntoa (lsah->id));
  1390. continue;
  1391. }
  1392. #endif /* STRICT_OBIT_USAGE_CHECK */
  1393. /* Do not take in AS External Opaque-LSAs if we are a stub. */
  1394. if (lsah->type == OSPF_OPAQUE_AS_LSA
  1395. && nbr->oi->area->external_routing != OSPF_AREA_DEFAULT)
  1396. {
  1397. if (IS_DEBUG_OSPF_EVENT)
  1398. zlog_debug ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah->type, inet_ntoa (lsah->id));
  1399. continue;
  1400. }
  1401. }
  1402. else if (IS_OPAQUE_LSA(lsah->type))
  1403. {
  1404. zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
  1405. continue;
  1406. }
  1407. /* Create OSPF LSA instance. */
  1408. lsa = ospf_lsa_new ();
  1409. /* We may wish to put some error checking if type NSSA comes in
  1410. and area not in NSSA mode */
  1411. switch (lsah->type)
  1412. {
  1413. case OSPF_AS_EXTERNAL_LSA:
  1414. case OSPF_OPAQUE_AS_LSA:
  1415. lsa->area = NULL;
  1416. break;
  1417. case OSPF_OPAQUE_LINK_LSA:
  1418. lsa->oi = oi; /* Remember incoming interface for flooding control. */
  1419. /* Fallthrough */
  1420. default:
  1421. lsa->area = oi->area;
  1422. break;
  1423. }
  1424. lsa->data = ospf_lsa_data_new (length);
  1425. memcpy (lsa->data, lsah, length);
  1426. if (IS_DEBUG_OSPF_EVENT)
  1427. zlog_debug("LSA[Type%d:%s]: %p new LSA created with Link State Update",
  1428. lsa->data->type, inet_ntoa (lsa->data->id), (void *)lsa);
  1429. listnode_add (lsas, lsa);
  1430. }
  1431. return lsas;
  1432. }
  1433. /* Cleanup Update list. */
  1434. static void
  1435. ospf_upd_list_clean (struct list *lsas)
  1436. {
  1437. struct listnode *node, *nnode;
  1438. struct ospf_lsa *lsa;
  1439. for (ALL_LIST_ELEMENTS (lsas, node, nnode, lsa))
  1440. ospf_lsa_discard (lsa);
  1441. list_delete (lsas);
  1442. }
  1443. /* OSPF Link State Update message read -- RFC2328 Section 13. */
  1444. static void
  1445. ospf_ls_upd (struct ospf *ospf, struct ip *iph, struct ospf_header *ospfh,
  1446. struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1447. {
  1448. struct ospf_neighbor *nbr;
  1449. struct list *lsas;
  1450. struct listnode *node, *nnode;
  1451. struct ospf_lsa *lsa = NULL;
  1452. /* unsigned long ls_req_found = 0; */
  1453. /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */
  1454. /* Increment statistics. */
  1455. oi->ls_upd_in++;
  1456. /* Check neighbor. */
  1457. nbr = ospf_nbr_lookup (oi, iph, ospfh);
  1458. if (nbr == NULL)
  1459. {
  1460. zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
  1461. inet_ntoa (ospfh->router_id), IF_NAME (oi));
  1462. return;
  1463. }
  1464. /* Add event to thread. */
  1465. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  1466. /* Check neighbor state. */
  1467. if (nbr->state < NSM_Exchange)
  1468. {
  1469. zlog_warn ("Link State Update: "
  1470. "Neighbor[%s] state %s is less than Exchange",
  1471. inet_ntoa (ospfh->router_id),
  1472. LOOKUP(ospf_nsm_state_msg, nbr->state));
  1473. return;
  1474. }
  1475. /* Get list of LSAs from Link State Update packet. - Also perorms Stages
  1476. * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
  1477. * of section 13.
  1478. */
  1479. lsas = ospf_ls_upd_list_lsa (nbr, s, oi, size);
  1480. #define DISCARD_LSA(L,N) {\
  1481. if (IS_DEBUG_OSPF_EVENT) \
  1482. zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p" \
  1483. " Type-%d", N, (void *)lsa, (int) lsa->data->type); \
  1484. ospf_lsa_discard (L); \
  1485. continue; }
  1486. /* Process each LSA received in the one packet.
  1487. *
  1488. * Numbers in parentheses, e.g. (1), (2), etc., and the corresponding
  1489. * text below are from the steps in RFC 2328, Section 13.
  1490. */
  1491. for (ALL_LIST_ELEMENTS (lsas, node, nnode, lsa))
  1492. {
  1493. struct ospf_lsa *ls_ret, *current;
  1494. int ret = 1;
  1495. if (IS_DEBUG_OSPF_NSSA)
  1496. {
  1497. char buf1[INET_ADDRSTRLEN];
  1498. char buf2[INET_ADDRSTRLEN];
  1499. char buf3[INET_ADDRSTRLEN];
  1500. zlog_debug("LSA Type-%d from %s, ID: %s, ADV: %s",
  1501. lsa->data->type,
  1502. inet_ntop (AF_INET, &ospfh->router_id,
  1503. buf1, INET_ADDRSTRLEN),
  1504. inet_ntop (AF_INET, &lsa->data->id,
  1505. buf2, INET_ADDRSTRLEN),
  1506. inet_ntop (AF_INET, &lsa->data->adv_router,
  1507. buf3, INET_ADDRSTRLEN));
  1508. }
  1509. listnode_delete (lsas, lsa); /* We don't need it in list anymore */
  1510. /* (1) Validate Checksum - Done above by ospf_ls_upd_list_lsa() */
  1511. /* (2) LSA Type - Done above by ospf_ls_upd_list_lsa() */
  1512. /* (3) Do not take in AS External LSAs if we are a stub or NSSA. */
  1513. /* Do not take in AS NSSA if this neighbor and we are not NSSA */
  1514. /* Do take in Type-7's if we are an NSSA */
  1515. /* If we are also an ABR, later translate them to a Type-5 packet */
  1516. /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
  1517. translate them to a separate Type-5 packet. */
  1518. if (lsa->data->type == OSPF_AS_EXTERNAL_LSA)
  1519. /* Reject from STUB or NSSA */
  1520. if (nbr->oi->area->external_routing != OSPF_AREA_DEFAULT)
  1521. {
  1522. if (IS_DEBUG_OSPF_NSSA)
  1523. zlog_debug("Incoming External LSA Discarded: We are NSSA/STUB Area");
  1524. DISCARD_LSA (lsa, 1);
  1525. }
  1526. if (lsa->data->type == OSPF_AS_NSSA_LSA)
  1527. if (nbr->oi->area->external_routing != OSPF_AREA_NSSA)
  1528. {
  1529. if (IS_DEBUG_OSPF_NSSA)
  1530. zlog_debug("Incoming NSSA LSA Discarded: Not NSSA Area");
  1531. DISCARD_LSA (lsa,2);
  1532. }
  1533. /* VU229804: Router-LSA Adv-ID must be equal to LS-ID */
  1534. if (lsa->data->type == OSPF_ROUTER_LSA)
  1535. if (!IPV4_ADDR_SAME(&lsa->data->id, &lsa->data->adv_router))
  1536. {
  1537. char buf1[INET_ADDRSTRLEN];
  1538. char buf2[INET_ADDRSTRLEN];
  1539. char buf3[INET_ADDRSTRLEN];
  1540. zlog_err("Incoming Router-LSA from %s with "
  1541. "Adv-ID[%s] != LS-ID[%s]",
  1542. inet_ntop (AF_INET, &ospfh->router_id,
  1543. buf1, INET_ADDRSTRLEN),
  1544. inet_ntop (AF_INET, &lsa->data->id,
  1545. buf2, INET_ADDRSTRLEN),
  1546. inet_ntop (AF_INET, &lsa->data->adv_router,
  1547. buf3, INET_ADDRSTRLEN));
  1548. zlog_err("OSPF domain compromised by attack or corruption. "
  1549. "Verify correct operation of -ALL- OSPF routers.");
  1550. DISCARD_LSA (lsa, 0);
  1551. }
  1552. /* Find the LSA in the current database. */
  1553. current = ospf_lsa_lookup_by_header (oi->area, lsa->data);
  1554. /* (4) If the LSA's LS age is equal to MaxAge, and there is currently
  1555. no instance of the LSA in the router's link state database,
  1556. and none of router's neighbors are in states Exchange or Loading,
  1557. then take the following actions: */
  1558. if (IS_LSA_MAXAGE (lsa) && !current &&
  1559. ospf_check_nbr_status(oi->ospf))
  1560. {
  1561. /* (4a) Response Link State Acknowledgment. */
  1562. ospf_ls_ack_send (nbr, lsa);
  1563. /* (4b) Discard LSA. */
  1564. if (IS_DEBUG_OSPF (lsa, LSA))
  1565. {
  1566. zlog_debug ("Link State Update[%s]: LS age is equal to MaxAge.",
  1567. dump_lsa_key(lsa));
  1568. }
  1569. DISCARD_LSA (lsa, 3);
  1570. }
  1571. if (IS_OPAQUE_LSA (lsa->data->type)
  1572. && IPV4_ADDR_SAME (&lsa->data->adv_router, &oi->ospf->router_id))
  1573. {
  1574. /*
  1575. * Even if initial flushing seems to be completed, there might
  1576. * be a case that self-originated LSA with MaxAge still remain
  1577. * in the routing domain.
  1578. * Just send an LSAck message to cease retransmission.
  1579. */
  1580. if (IS_LSA_MAXAGE (lsa))
  1581. {
  1582. zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa));
  1583. ospf_ls_ack_send (nbr, lsa);
  1584. ospf_lsa_discard (lsa);
  1585. if (current != NULL && ! IS_LSA_MAXAGE (current))
  1586. ospf_opaque_lsa_refresh_schedule (current);
  1587. continue;
  1588. }
  1589. /*
  1590. * If an instance of self-originated Opaque-LSA is not found
  1591. * in the LSDB, there are some possible cases here.
  1592. *
  1593. * 1) This node lost opaque-capability after restart.
  1594. * 2) Else, a part of opaque-type is no more supported.
  1595. * 3) Else, a part of opaque-id is no more supported.
  1596. *
  1597. * Anyway, it is still this node's responsibility to flush it.
  1598. * Otherwise, the LSA instance remains in the routing domain
  1599. * until its age reaches to MaxAge.
  1600. */
  1601. /* XXX: We should deal with this for *ALL* LSAs, not just opaque */
  1602. if (current == NULL)
  1603. {
  1604. if (IS_DEBUG_OSPF_EVENT)
  1605. zlog_debug ("LSA[%s]: Previously originated Opaque-LSA,"
  1606. "not found in the LSDB.", dump_lsa_key (lsa));
  1607. SET_FLAG (lsa->flags, OSPF_LSA_SELF);
  1608. ospf_opaque_self_originated_lsa_received (nbr, lsa);
  1609. ospf_ls_ack_send (nbr, lsa);
  1610. continue;
  1611. }
  1612. }
  1613. /* It might be happen that received LSA is self-originated network LSA, but
  1614. * router ID is changed. So, we should check if LSA is a network-LSA whose
  1615. * Link State ID is one of the router's own IP interface addresses but whose
  1616. * Advertising Router is not equal to the router's own Router ID
  1617. * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed.
  1618. */
  1619. if(lsa->data->type == OSPF_NETWORK_LSA)
  1620. {
  1621. struct listnode *oinode, *oinnode;
  1622. struct ospf_interface *out_if;
  1623. int Flag = 0;
  1624. for (ALL_LIST_ELEMENTS (oi->ospf->oiflist, oinode, oinnode, out_if))
  1625. {
  1626. if(out_if == NULL)
  1627. break;
  1628. if((IPV4_ADDR_SAME(&out_if->address->u.prefix4, &lsa->data->id)) &&
  1629. (!(IPV4_ADDR_SAME(&oi->ospf->router_id, &lsa->data->adv_router))))
  1630. {
  1631. if(out_if->network_lsa_self)
  1632. {
  1633. ospf_lsa_flush_area(lsa,out_if->area);
  1634. if(IS_DEBUG_OSPF_EVENT)
  1635. zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
  1636. (void *)lsa, (int) lsa->data->type);
  1637. ospf_lsa_discard (lsa);
  1638. Flag = 1;
  1639. }
  1640. break;
  1641. }
  1642. }
  1643. if(Flag)
  1644. continue;
  1645. }
  1646. /* (5) Find the instance of this LSA that is currently contained
  1647. in the router's link state database. If there is no
  1648. database copy, or the received LSA is more recent than
  1649. the database copy the following steps must be performed.
  1650. (The sub steps from RFC 2328 section 13 step (5) will be performed in
  1651. ospf_flood() ) */
  1652. if (current == NULL ||
  1653. (ret = ospf_lsa_more_recent (current, lsa)) < 0)
  1654. {
  1655. /* Actual flooding procedure. */
  1656. if (ospf_flood (oi->ospf, nbr, current, lsa) < 0) /* Trap NSSA later. */
  1657. DISCARD_LSA (lsa, 4);
  1658. continue;
  1659. }
  1660. /* (6) Else, If there is an instance of the LSA on the sending
  1661. neighbor's Link state request list, an error has occurred in
  1662. the Database Exchange process. In this case, restart the
  1663. Database Exchange process by generating the neighbor event
  1664. BadLSReq for the sending neighbor and stop processing the
  1665. Link State Update packet. */
  1666. if (ospf_ls_request_lookup (nbr, lsa))
  1667. {
  1668. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1669. zlog_warn("LSA[%s] instance exists on Link state request list",
  1670. dump_lsa_key(lsa));
  1671. /* Clean list of LSAs. */
  1672. ospf_upd_list_clean (lsas);
  1673. /* this lsa is not on lsas list already. */
  1674. ospf_lsa_discard (lsa);
  1675. return;
  1676. }
  1677. /* If the received LSA is the same instance as the database copy
  1678. (i.e., neither one is more recent) the following two steps
  1679. should be performed: */
  1680. if (ret == 0)
  1681. {
  1682. /* If the LSA is listed in the Link state retransmission list
  1683. for the receiving adjacency, the router itself is expecting
  1684. an acknowledgment for this LSA. The router should treat the
  1685. received LSA as an acknowledgment by removing the LSA from
  1686. the Link state retransmission list. This is termed an
  1687. "implied acknowledgment". */
  1688. ls_ret = ospf_ls_retransmit_lookup (nbr, lsa);
  1689. if (ls_ret != NULL)
  1690. {
  1691. ospf_ls_retransmit_delete (nbr, ls_ret);
  1692. /* Delayed acknowledgment sent if advertisement received
  1693. from Designated Router, otherwise do nothing. */
  1694. if (oi->state == ISM_Backup)
  1695. if (NBR_IS_DR (nbr))
  1696. listnode_add (oi->ls_ack, ospf_lsa_lock (lsa));
  1697. DISCARD_LSA (lsa, 5);
  1698. }
  1699. else
  1700. /* Acknowledge the receipt of the LSA by sending a
  1701. Link State Acknowledgment packet back out the receiving
  1702. interface. */
  1703. {
  1704. ospf_ls_ack_send (nbr, lsa);
  1705. DISCARD_LSA (lsa, 6);
  1706. }
  1707. }
  1708. /* The database copy is more recent. If the database copy
  1709. has LS age equal to MaxAge and LS sequence number equal to
  1710. MaxSequenceNumber, simply discard the received LSA without
  1711. acknowledging it. (In this case, the LSA's LS sequence number is
  1712. wrapping, and the MaxSequenceNumber LSA must be completely
  1713. flushed before any new LSA instance can be introduced). */
  1714. else if (ret > 0) /* Database copy is more recent */
  1715. {
  1716. if (IS_LSA_MAXAGE (current) &&
  1717. current->data->ls_seqnum == htonl (OSPF_MAX_SEQUENCE_NUMBER))
  1718. {
  1719. DISCARD_LSA (lsa, 7);
  1720. }
  1721. /* Otherwise, as long as the database copy has not been sent in a
  1722. Link State Update within the last MinLSArrival seconds, send the
  1723. database copy back to the sending neighbor, encapsulated within
  1724. a Link State Update Packet. The Link State Update Packet should
  1725. be sent directly to the neighbor. In so doing, do not put the
  1726. database copy of the LSA on the neighbor's link state
  1727. retransmission list, and do not acknowledge the received (less
  1728. recent) LSA instance. */
  1729. else
  1730. {
  1731. struct timeval now;
  1732. quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
  1733. if (tv_cmp (tv_sub (now, current->tv_orig),
  1734. msec2tv (ospf->min_ls_arrival)) >= 0)
  1735. /* Trap NSSA type later.*/
  1736. ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT);
  1737. DISCARD_LSA (lsa, 8);
  1738. }
  1739. }
  1740. }
  1741. #undef DISCARD_LSA
  1742. assert (listcount (lsas) == 0);
  1743. list_delete (lsas);
  1744. }
  1745. /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
  1746. static void
  1747. ospf_ls_ack (struct ip *iph, struct ospf_header *ospfh,
  1748. struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1749. {
  1750. struct ospf_neighbor *nbr;
  1751. /* increment statistics. */
  1752. oi->ls_ack_in++;
  1753. nbr = ospf_nbr_lookup (oi, iph, ospfh);
  1754. if (nbr == NULL)
  1755. {
  1756. zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
  1757. inet_ntoa (ospfh->router_id));
  1758. return;
  1759. }
  1760. /* Add event to thread. */
  1761. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  1762. if (nbr->state < NSM_Exchange)
  1763. {
  1764. zlog_warn ("Link State Acknowledgment: "
  1765. "Neighbor[%s] state %s is less than Exchange",
  1766. inet_ntoa (ospfh->router_id),
  1767. LOOKUP(ospf_nsm_state_msg, nbr->state));
  1768. return;
  1769. }
  1770. while (size >= OSPF_LSA_HEADER_SIZE)
  1771. {
  1772. struct ospf_lsa *lsa, *lsr;
  1773. lsa = ospf_lsa_new ();
  1774. lsa->data = (struct lsa_header *) STREAM_PNT (s);
  1775. /* lsah = (struct lsa_header *) STREAM_PNT (s); */
  1776. size -= OSPF_LSA_HEADER_SIZE;
  1777. stream_forward_getp (s, OSPF_LSA_HEADER_SIZE);
  1778. if (lsa->data->type < OSPF_MIN_LSA || lsa->data->type >= OSPF_MAX_LSA)
  1779. {
  1780. lsa->data = NULL;
  1781. ospf_lsa_discard (lsa);
  1782. continue;
  1783. }
  1784. lsr = ospf_ls_retransmit_lookup (nbr, lsa);
  1785. if (lsr != NULL && ospf_lsa_more_recent (lsr, lsa) == 0)
  1786. ospf_ls_retransmit_delete (nbr, lsr);
  1787. lsa->data = NULL;
  1788. ospf_lsa_discard (lsa);
  1789. }
  1790. return;
  1791. }
  1792. static struct stream *
  1793. ospf_recv_packet (int fd, struct interface **ifp, struct stream *ibuf)
  1794. {
  1795. int ret;
  1796. struct ip *iph;
  1797. u_int16_t ip_len;
  1798. ifindex_t ifindex = 0;
  1799. struct iovec iov;
  1800. /* Header and data both require alignment. */
  1801. char buff [CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
  1802. struct msghdr msgh;
  1803. memset (&msgh, 0, sizeof (struct msghdr));
  1804. msgh.msg_iov = &iov;
  1805. msgh.msg_iovlen = 1;
  1806. msgh.msg_control = (caddr_t) buff;
  1807. msgh.msg_controllen = sizeof (buff);
  1808. ret = stream_recvmsg (ibuf, fd, &msgh, 0, OSPF_MAX_PACKET_SIZE+1);
  1809. if (ret < 0)
  1810. {
  1811. zlog_warn("stream_recvmsg failed: %s", safe_strerror(errno));
  1812. return NULL;
  1813. }
  1814. if ((unsigned int)ret < sizeof(iph)) /* ret must be > 0 now */
  1815. {
  1816. zlog_warn("ospf_recv_packet: discarding runt packet of length %d "
  1817. "(ip header size is %u)",
  1818. ret, (u_int)sizeof(iph));
  1819. return NULL;
  1820. }
  1821. /* Note that there should not be alignment problems with this assignment
  1822. because this is at the beginning of the stream data buffer. */
  1823. iph = (struct ip *) STREAM_DATA(ibuf);
  1824. sockopt_iphdrincl_swab_systoh (iph);
  1825. ip_len = iph->ip_len;
  1826. #if !defined(GNU_LINUX) && (OpenBSD < 200311) && (__FreeBSD_version < 1000000)
  1827. /*
  1828. * Kernel network code touches incoming IP header parameters,
  1829. * before protocol specific processing.
  1830. *
  1831. * 1) Convert byteorder to host representation.
  1832. * --> ip_len, ip_id, ip_off
  1833. *
  1834. * 2) Adjust ip_len to strip IP header size!
  1835. * --> If user process receives entire IP packet via RAW
  1836. * socket, it must consider adding IP header size to
  1837. * the "ip_len" field of "ip" structure.
  1838. *
  1839. * For more details, see <netinet/ip_input.c>.
  1840. */
  1841. ip_len = ip_len + (iph->ip_hl << 2);
  1842. #endif
  1843. #if defined(__DragonFly__)
  1844. /*
  1845. * in DragonFly's raw socket, ip_len/ip_off are read
  1846. * in network byte order.
  1847. * As OpenBSD < 200311 adjust ip_len to strip IP header size!
  1848. */
  1849. ip_len = ntohs(iph->ip_len) + (iph->ip_hl << 2);
  1850. #endif
  1851. ifindex = getsockopt_ifindex (AF_INET, &msgh);
  1852. *ifp = if_lookup_by_index (ifindex);
  1853. if (ret != ip_len)
  1854. {
  1855. zlog_warn ("ospf_recv_packet read length mismatch: ip_len is %d, "
  1856. "but recvmsg returned %d", ip_len, ret);
  1857. return NULL;
  1858. }
  1859. return ibuf;
  1860. }
  1861. static struct ospf_interface *
  1862. ospf_associate_packet_vl (struct ospf *ospf, struct interface *ifp,
  1863. struct ip *iph, struct ospf_header *ospfh)
  1864. {
  1865. struct ospf_interface *rcv_oi;
  1866. struct ospf_vl_data *vl_data;
  1867. struct ospf_area *vl_area;
  1868. struct listnode *node;
  1869. if (IN_MULTICAST (ntohl (iph->ip_dst.s_addr)) ||
  1870. !OSPF_IS_AREA_BACKBONE (ospfh))
  1871. return NULL;
  1872. /* look for local OSPF interface matching the destination
  1873. * to determine Area ID. We presume therefore the destination address
  1874. * is unique, or at least (for "unnumbered" links), not used in other
  1875. * areas
  1876. */
  1877. if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, NULL,
  1878. iph->ip_dst)) == NULL)
  1879. return NULL;
  1880. for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
  1881. {
  1882. vl_area = ospf_area_lookup_by_area_id (ospf, vl_data->vl_area_id);
  1883. if (!vl_area)
  1884. continue;
  1885. if (OSPF_AREA_SAME (&vl_area, &rcv_oi->area) &&
  1886. IPV4_ADDR_SAME (&vl_data->vl_peer, &ospfh->router_id))
  1887. {
  1888. if (IS_DEBUG_OSPF_EVENT)
  1889. zlog_debug ("associating packet with %s",
  1890. IF_NAME (vl_data->vl_oi));
  1891. if (! CHECK_FLAG (vl_data->vl_oi->ifp->flags, IFF_UP))
  1892. {
  1893. if (IS_DEBUG_OSPF_EVENT)
  1894. zlog_debug ("This VL is not up yet, sorry");
  1895. return NULL;
  1896. }
  1897. return vl_data->vl_oi;
  1898. }
  1899. }
  1900. if (IS_DEBUG_OSPF_EVENT)
  1901. zlog_debug ("couldn't find any VL to associate the packet with");
  1902. return NULL;
  1903. }
  1904. static int
  1905. ospf_check_area_id (struct ospf_interface *oi, struct ospf_header *ospfh)
  1906. {
  1907. /* Check match the Area ID of the receiving interface. */
  1908. if (OSPF_AREA_SAME (&oi->area, &ospfh))
  1909. return 1;
  1910. return 0;
  1911. }
  1912. /* Unbound socket will accept any Raw IP packets if proto is matched.
  1913. To prevent it, compare src IP address and i/f address with masking
  1914. i/f network mask. */
  1915. static int
  1916. ospf_check_network_mask (struct ospf_interface *oi, struct in_addr ip_src)
  1917. {
  1918. struct in_addr mask, me, him;
  1919. if (oi->type == OSPF_IFTYPE_POINTOPOINT ||
  1920. oi->type == OSPF_IFTYPE_VIRTUALLINK)
  1921. return 1;
  1922. masklen2ip (oi->address->prefixlen, &mask);
  1923. me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
  1924. him.s_addr = ip_src.s_addr & mask.s_addr;
  1925. if (IPV4_ADDR_SAME (&me, &him))
  1926. return 1;
  1927. return 0;
  1928. }
  1929. /* Return 1, if the packet is properly authenticated and checksummed,
  1930. 0 otherwise. In particular, check that AuType header field is valid and
  1931. matches the locally configured AuType, and that D.5 requirements are met. */
  1932. static int
  1933. ospf_check_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
  1934. {
  1935. struct crypt_key *ck;
  1936. u_int16_t iface_auth_type;
  1937. u_int16_t pkt_auth_type = ntohs (ospfh->auth_type);
  1938. switch (pkt_auth_type)
  1939. {
  1940. case OSPF_AUTH_NULL: /* RFC2328 D.5.1 */
  1941. if (OSPF_AUTH_NULL != (iface_auth_type = ospf_auth_type (oi)))
  1942. {
  1943. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1944. zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Null",
  1945. IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
  1946. return 0;
  1947. }
  1948. if (! ospf_check_sum (ospfh))
  1949. {
  1950. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1951. zlog_warn ("interface %s: Null auth OK, but checksum error, Router-ID %s",
  1952. IF_NAME (oi), inet_ntoa (ospfh->router_id));
  1953. return 0;
  1954. }
  1955. return 1;
  1956. case OSPF_AUTH_SIMPLE: /* RFC2328 D.5.2 */
  1957. if (OSPF_AUTH_SIMPLE != (iface_auth_type = ospf_auth_type (oi)))
  1958. {
  1959. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1960. zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Simple",
  1961. IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
  1962. return 0;
  1963. }
  1964. if (memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE))
  1965. {
  1966. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1967. zlog_warn ("interface %s: Simple auth failed", IF_NAME (oi));
  1968. return 0;
  1969. }
  1970. if (! ospf_check_sum (ospfh))
  1971. {
  1972. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1973. zlog_warn ("interface %s: Simple auth OK, checksum error, Router-ID %s",
  1974. IF_NAME (oi), inet_ntoa (ospfh->router_id));
  1975. return 0;
  1976. }
  1977. return 1;
  1978. case OSPF_AUTH_CRYPTOGRAPHIC: /* RFC2328 D.5.3 */
  1979. if (OSPF_AUTH_CRYPTOGRAPHIC != (iface_auth_type = ospf_auth_type (oi)))
  1980. {
  1981. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1982. zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Cryptographic",
  1983. IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
  1984. return 0;
  1985. }
  1986. if (ospfh->checksum)
  1987. {
  1988. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1989. zlog_warn ("interface %s: OSPF header checksum is not 0", IF_NAME (oi));
  1990. return 0;
  1991. }
  1992. /* only MD5 crypto method can pass ospf_packet_examin() */
  1993. if
  1994. (
  1995. NULL == (ck = listgetdata (listtail(OSPF_IF_PARAM (oi,auth_crypt)))) ||
  1996. ospfh->u.crypt.key_id != ck->key_id ||
  1997. /* Condition above uses the last key ID on the list, which is
  1998. different from what ospf_crypt_key_lookup() does. A bug? */
  1999. ! ospf_check_md5_digest (oi, ospfh)
  2000. )
  2001. {
  2002. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2003. zlog_warn ("interface %s: MD5 auth failed", IF_NAME (oi));
  2004. return 0;
  2005. }
  2006. return 1;
  2007. default:
  2008. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2009. zlog_warn ("interface %s: invalid packet auth-type (%02x)",
  2010. IF_NAME (oi), pkt_auth_type);
  2011. return 0;
  2012. }
  2013. }
  2014. static int
  2015. ospf_check_sum (struct ospf_header *ospfh)
  2016. {
  2017. u_int32_t ret;
  2018. u_int16_t sum;
  2019. /* clear auth_data for checksum. */
  2020. memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
  2021. /* keep checksum and clear. */
  2022. sum = ospfh->checksum;
  2023. memset (&ospfh->checksum, 0, sizeof (u_int16_t));
  2024. /* calculate checksum. */
  2025. ret = in_cksum (ospfh, ntohs (ospfh->length));
  2026. if (ret != sum)
  2027. {
  2028. zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X",
  2029. ret, sum);
  2030. return 0;
  2031. }
  2032. return 1;
  2033. }
  2034. /* Verify, that given link/TOS records are properly sized/aligned and match
  2035. Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */
  2036. static unsigned
  2037. ospf_router_lsa_links_examin
  2038. (
  2039. struct router_lsa_link * link,
  2040. u_int16_t linkbytes,
  2041. const u_int16_t num_links
  2042. )
  2043. {
  2044. unsigned counted_links = 0, thislinklen;
  2045. while (linkbytes)
  2046. {
  2047. thislinklen = OSPF_ROUTER_LSA_LINK_SIZE + 4 * link->m[0].tos_count;
  2048. if (thislinklen > linkbytes)
  2049. {
  2050. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2051. zlog_debug ("%s: length error in link block #%u", __func__, counted_links);
  2052. return MSG_NG;
  2053. }
  2054. link = (struct router_lsa_link *)((caddr_t) link + thislinklen);
  2055. linkbytes -= thislinklen;
  2056. counted_links++;
  2057. }
  2058. if (counted_links != num_links)
  2059. {
  2060. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2061. zlog_debug ("%s: %u link blocks declared, %u present",
  2062. __func__, num_links, counted_links);
  2063. return MSG_NG;
  2064. }
  2065. return MSG_OK;
  2066. }
  2067. /* Verify, that the given LSA is properly sized/aligned (including type-specific
  2068. minimum length constraint). */
  2069. static unsigned
  2070. ospf_lsa_examin (struct lsa_header * lsah, const u_int16_t lsalen, const u_char headeronly)
  2071. {
  2072. unsigned ret;
  2073. struct router_lsa * rlsa;
  2074. if
  2075. (
  2076. lsah->type < OSPF_MAX_LSA &&
  2077. ospf_lsa_minlen[lsah->type] &&
  2078. lsalen < OSPF_LSA_HEADER_SIZE + ospf_lsa_minlen[lsah->type]
  2079. )
  2080. {
  2081. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2082. zlog_debug ("%s: undersized (%u B) %s",
  2083. __func__, lsalen, LOOKUP (ospf_lsa_type_msg, lsah->type));
  2084. return MSG_NG;
  2085. }
  2086. switch (lsah->type)
  2087. {
  2088. case OSPF_ROUTER_LSA:
  2089. /* RFC2328 A.4.2, LSA header + 4 bytes followed by N>=1 (12+)-byte link blocks */
  2090. if (headeronly)
  2091. {
  2092. ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_ROUTER_LSA_MIN_SIZE) % 4 ? MSG_NG : MSG_OK;
  2093. break;
  2094. }
  2095. rlsa = (struct router_lsa *) lsah;
  2096. ret = ospf_router_lsa_links_examin
  2097. (
  2098. (struct router_lsa_link *) rlsa->link,
  2099. lsalen - OSPF_LSA_HEADER_SIZE - 4, /* skip: basic header, "flags", 0, "# links" */
  2100. ntohs (rlsa->links) /* 16 bits */
  2101. );
  2102. break;
  2103. case OSPF_AS_EXTERNAL_LSA:
  2104. /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long blocks */
  2105. case OSPF_AS_NSSA_LSA:
  2106. /* RFC3101 C, idem */
  2107. ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_AS_EXTERNAL_LSA_MIN_SIZE) % 12 ? MSG_NG : MSG_OK;
  2108. break;
  2109. /* Following LSA types are considered OK length-wise as soon as their minimum
  2110. * length constraint is met and length of the whole LSA is a multiple of 4
  2111. * (basic LSA header size is already a multiple of 4). */
  2112. case OSPF_NETWORK_LSA:
  2113. /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */
  2114. case OSPF_SUMMARY_LSA:
  2115. case OSPF_ASBR_SUMMARY_LSA:
  2116. /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS blocks */
  2117. case OSPF_OPAQUE_LINK_LSA:
  2118. case OSPF_OPAQUE_AREA_LSA:
  2119. case OSPF_OPAQUE_AS_LSA:
  2120. /* RFC5250 A.2, "some number of octets (of application-specific
  2121. * data) padded to 32-bit alignment." This is considered equivalent
  2122. * to 4-byte alignment of all other LSA types, see OSPF-ALIGNMENT.txt
  2123. * file for the detailed analysis of this passage. */
  2124. ret = lsalen % 4 ? MSG_NG : MSG_OK;
  2125. break;
  2126. default:
  2127. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2128. zlog_debug ("%s: unsupported LSA type 0x%02x", __func__, lsah->type);
  2129. return MSG_NG;
  2130. }
  2131. if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV))
  2132. zlog_debug ("%s: alignment error in %s",
  2133. __func__, LOOKUP (ospf_lsa_type_msg, lsah->type));
  2134. return ret;
  2135. }
  2136. /* Verify if the provided input buffer is a valid sequence of LSAs. This
  2137. includes verification of LSA blocks length/alignment and dispatching
  2138. of deeper-level checks. */
  2139. static unsigned
  2140. ospf_lsaseq_examin
  2141. (
  2142. struct lsa_header *lsah, /* start of buffered data */
  2143. size_t length,
  2144. const u_char headeronly,
  2145. /* When declared_num_lsas is not 0, compare it to the real number of LSAs
  2146. and treat the difference as an error. */
  2147. const u_int32_t declared_num_lsas
  2148. )
  2149. {
  2150. u_int32_t counted_lsas = 0;
  2151. while (length)
  2152. {
  2153. u_int16_t lsalen;
  2154. if (length < OSPF_LSA_HEADER_SIZE)
  2155. {
  2156. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2157. zlog_debug ("%s: undersized (%zu B) trailing (#%u) LSA header",
  2158. __func__, length, counted_lsas);
  2159. return MSG_NG;
  2160. }
  2161. /* save on ntohs() calls here and in the LSA validator */
  2162. lsalen = ntohs (lsah->length);
  2163. if (lsalen < OSPF_LSA_HEADER_SIZE)
  2164. {
  2165. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2166. zlog_debug ("%s: malformed LSA header #%u, declared length is %u B",
  2167. __func__, counted_lsas, lsalen);
  2168. return MSG_NG;
  2169. }
  2170. if (headeronly)
  2171. {
  2172. /* less checks here and in ospf_lsa_examin() */
  2173. if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 1))
  2174. {
  2175. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2176. zlog_debug ("%s: malformed header-only LSA #%u", __func__, counted_lsas);
  2177. return MSG_NG;
  2178. }
  2179. lsah = (struct lsa_header *) ((caddr_t) lsah + OSPF_LSA_HEADER_SIZE);
  2180. length -= OSPF_LSA_HEADER_SIZE;
  2181. }
  2182. else
  2183. {
  2184. /* make sure the input buffer is deep enough before further checks */
  2185. if (lsalen > length)
  2186. {
  2187. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2188. zlog_debug ("%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B",
  2189. __func__, counted_lsas, lsalen, length);
  2190. return MSG_NG;
  2191. }
  2192. if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 0))
  2193. {
  2194. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2195. zlog_debug ("%s: malformed LSA #%u", __func__, counted_lsas);
  2196. return MSG_NG;
  2197. }
  2198. lsah = (struct lsa_header *) ((caddr_t) lsah + lsalen);
  2199. length -= lsalen;
  2200. }
  2201. counted_lsas++;
  2202. }
  2203. if (declared_num_lsas && counted_lsas != declared_num_lsas)
  2204. {
  2205. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2206. zlog_debug ("%s: #LSAs declared (%u) does not match actual (%u)",
  2207. __func__, declared_num_lsas, counted_lsas);
  2208. return MSG_NG;
  2209. }
  2210. return MSG_OK;
  2211. }
  2212. /* Verify a complete OSPF packet for proper sizing/alignment. */
  2213. static unsigned
  2214. ospf_packet_examin (struct ospf_header * oh, const unsigned bytesonwire)
  2215. {
  2216. u_int16_t bytesdeclared, bytesauth;
  2217. unsigned ret;
  2218. struct ospf_ls_update * lsupd;
  2219. /* Length, 1st approximation. */
  2220. if (bytesonwire < OSPF_HEADER_SIZE)
  2221. {
  2222. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2223. zlog_debug ("%s: undersized (%u B) packet", __func__, bytesonwire);
  2224. return MSG_NG;
  2225. }
  2226. /* Now it is safe to access header fields. Performing length check, allow
  2227. * for possible extra bytes of crypto auth/padding, which are not counted
  2228. * in the OSPF header "length" field. */
  2229. if (oh->version != OSPF_VERSION)
  2230. {
  2231. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2232. zlog_debug ("%s: invalid (%u) protocol version", __func__, oh->version);
  2233. return MSG_NG;
  2234. }
  2235. bytesdeclared = ntohs (oh->length);
  2236. if (ntohs (oh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
  2237. bytesauth = 0;
  2238. else
  2239. {
  2240. if (oh->u.crypt.auth_data_len != OSPF_AUTH_MD5_SIZE)
  2241. {
  2242. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2243. zlog_debug ("%s: unsupported crypto auth length (%u B)",
  2244. __func__, oh->u.crypt.auth_data_len);
  2245. return MSG_NG;
  2246. }
  2247. bytesauth = OSPF_AUTH_MD5_SIZE;
  2248. }
  2249. if (bytesdeclared + bytesauth > bytesonwire)
  2250. {
  2251. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2252. zlog_debug ("%s: packet length error (%u real, %u+%u declared)",
  2253. __func__, bytesonwire, bytesdeclared, bytesauth);
  2254. return MSG_NG;
  2255. }
  2256. /* Length, 2nd approximation. The type-specific constraint is checked
  2257. against declared length, not amount of bytes on wire. */
  2258. if
  2259. (
  2260. oh->type >= OSPF_MSG_HELLO &&
  2261. oh->type <= OSPF_MSG_LS_ACK &&
  2262. bytesdeclared < OSPF_HEADER_SIZE + ospf_packet_minlen[oh->type]
  2263. )
  2264. {
  2265. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2266. zlog_debug ("%s: undersized (%u B) %s packet", __func__,
  2267. bytesdeclared, LOOKUP (ospf_packet_type_str, oh->type));
  2268. return MSG_NG;
  2269. }
  2270. switch (oh->type)
  2271. {
  2272. case OSPF_MSG_HELLO:
  2273. /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes followed
  2274. by N>=0 router-IDs. */
  2275. ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_HELLO_MIN_SIZE) % 4 ? MSG_NG : MSG_OK;
  2276. break;
  2277. case OSPF_MSG_DB_DESC:
  2278. /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes followed
  2279. by N>=0 header-only LSAs. */
  2280. ret = ospf_lsaseq_examin
  2281. (
  2282. (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_DB_DESC_MIN_SIZE),
  2283. bytesdeclared - OSPF_HEADER_SIZE - OSPF_DB_DESC_MIN_SIZE,
  2284. 1, /* header-only LSAs */
  2285. 0
  2286. );
  2287. break;
  2288. case OSPF_MSG_LS_REQ:
  2289. /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes request blocks. */
  2290. ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_REQ_MIN_SIZE) %
  2291. OSPF_LSA_KEY_SIZE ? MSG_NG : MSG_OK;
  2292. break;
  2293. case OSPF_MSG_LS_UPD:
  2294. /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes followed
  2295. by N>=0 full LSAs (with N declared beforehand). */
  2296. lsupd = (struct ospf_ls_update *) ((caddr_t) oh + OSPF_HEADER_SIZE);
  2297. ret = ospf_lsaseq_examin
  2298. (
  2299. (struct lsa_header *) ((caddr_t) lsupd + OSPF_LS_UPD_MIN_SIZE),
  2300. bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_UPD_MIN_SIZE,
  2301. 0, /* full LSAs */
  2302. ntohl (lsupd->num_lsas) /* 32 bits */
  2303. );
  2304. break;
  2305. case OSPF_MSG_LS_ACK:
  2306. /* RFC2328 A.3.6, packet header followed by N>=0 header-only LSAs. */
  2307. ret = ospf_lsaseq_examin
  2308. (
  2309. (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_LS_ACK_MIN_SIZE),
  2310. bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_ACK_MIN_SIZE,
  2311. 1, /* header-only LSAs */
  2312. 0
  2313. );
  2314. break;
  2315. default:
  2316. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2317. zlog_debug ("%s: invalid packet type 0x%02x", __func__, oh->type);
  2318. return MSG_NG;
  2319. }
  2320. if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV))
  2321. zlog_debug ("%s: malformed %s packet", __func__, LOOKUP (ospf_packet_type_str, oh->type));
  2322. return ret;
  2323. }
  2324. /* OSPF Header verification. */
  2325. static int
  2326. ospf_verify_header (struct stream *ibuf, struct ospf_interface *oi,
  2327. struct ip *iph, struct ospf_header *ospfh)
  2328. {
  2329. /* Check Area ID. */
  2330. if (!ospf_check_area_id (oi, ospfh))
  2331. {
  2332. zlog_warn ("interface %s: ospf_read invalid Area ID %s.",
  2333. IF_NAME (oi), inet_ntoa (ospfh->area_id));
  2334. return -1;
  2335. }
  2336. /* Check network mask, Silently discarded. */
  2337. if (! ospf_check_network_mask (oi, iph->ip_src))
  2338. {
  2339. zlog_warn ("interface %s: ospf_read network address is not same [%s]",
  2340. IF_NAME (oi), inet_ntoa (iph->ip_src));
  2341. return -1;
  2342. }
  2343. /* Check authentication. The function handles logging actions, where required. */
  2344. if (! ospf_check_auth (oi, ospfh))
  2345. return -1;
  2346. return 0;
  2347. }
  2348. /* Starting point of packet process function. */
  2349. int
  2350. ospf_read (struct thread *thread)
  2351. {
  2352. int ret;
  2353. struct stream *ibuf;
  2354. struct ospf *ospf;
  2355. struct ospf_interface *oi;
  2356. struct ip *iph;
  2357. struct ospf_header *ospfh;
  2358. u_int16_t length;
  2359. struct interface *ifp;
  2360. /* first of all get interface pointer. */
  2361. ospf = THREAD_ARG (thread);
  2362. /* prepare for next packet. */
  2363. ospf->t_read = thread_add_read (master, ospf_read, ospf, ospf->fd);
  2364. stream_reset(ospf->ibuf);
  2365. if (!(ibuf = ospf_recv_packet (ospf->fd, &ifp, ospf->ibuf)))
  2366. return -1;
  2367. /* This raw packet is known to be at least as big as its IP header. */
  2368. /* Note that there should not be alignment problems with this assignment
  2369. because this is at the beginning of the stream data buffer. */
  2370. iph = (struct ip *) STREAM_DATA (ibuf);
  2371. /* Note that sockopt_iphdrincl_swab_systoh was called in ospf_recv_packet. */
  2372. if (ifp == NULL)
  2373. /* Handle cases where the platform does not support retrieving the ifindex,
  2374. and also platforms (such as Solaris 8) that claim to support ifindex
  2375. retrieval but do not. */
  2376. ifp = if_lookup_address (iph->ip_src);
  2377. if (ifp == NULL)
  2378. return 0;
  2379. /* IP Header dump. */
  2380. if (IS_DEBUG_OSPF_PACKET(0, RECV))
  2381. ospf_ip_header_dump (iph);
  2382. /* Self-originated packet should be discarded silently. */
  2383. if (ospf_if_lookup_by_local_addr (ospf, NULL, iph->ip_src))
  2384. {
  2385. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2386. {
  2387. zlog_debug ("ospf_read[%s]: Dropping self-originated packet",
  2388. inet_ntoa (iph->ip_src));
  2389. }
  2390. return 0;
  2391. }
  2392. /* Advance from IP header to OSPF header (iph->ip_hl has been verified
  2393. by ospf_recv_packet() to be correct). */
  2394. stream_forward_getp (ibuf, iph->ip_hl * 4);
  2395. ospfh = (struct ospf_header *) STREAM_PNT (ibuf);
  2396. if (MSG_OK != ospf_packet_examin (ospfh, stream_get_endp (ibuf) - stream_get_getp (ibuf)))
  2397. return -1;
  2398. /* Now it is safe to access all fields of OSPF packet header. */
  2399. /* associate packet with ospf interface */
  2400. oi = ospf_if_lookup_recv_if (ospf, iph->ip_src, ifp);
  2401. /* ospf_verify_header() relies on a valid "oi" and thus can be called only
  2402. after the passive/backbone/other checks below are passed. These checks
  2403. in turn access the fields of unverified "ospfh" structure for their own
  2404. purposes and must remain very accurate in doing this. */
  2405. /* If incoming interface is passive one, ignore it. */
  2406. if (oi && OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
  2407. {
  2408. char buf[3][INET_ADDRSTRLEN];
  2409. if (IS_DEBUG_OSPF_EVENT)
  2410. zlog_debug ("ignoring packet from router %s sent to %s, "
  2411. "received on a passive interface, %s",
  2412. inet_ntop(AF_INET, &ospfh->router_id, buf[0], sizeof(buf[0])),
  2413. inet_ntop(AF_INET, &iph->ip_dst, buf[1], sizeof(buf[1])),
  2414. inet_ntop(AF_INET, &oi->address->u.prefix4,
  2415. buf[2], sizeof(buf[2])));
  2416. if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS))
  2417. {
  2418. /* Try to fix multicast membership.
  2419. * Some OS:es may have problems in this area,
  2420. * make sure it is removed.
  2421. */
  2422. OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
  2423. ospf_if_set_multicast(oi);
  2424. }
  2425. return 0;
  2426. }
  2427. /* if no local ospf_interface,
  2428. * or header area is backbone but ospf_interface is not
  2429. * check for VLINK interface
  2430. */
  2431. if ( (oi == NULL) ||
  2432. (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id)
  2433. && !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id))
  2434. )
  2435. {
  2436. if ((oi = ospf_associate_packet_vl (ospf, ifp, iph, ospfh)) == NULL)
  2437. {
  2438. if (IS_DEBUG_OSPF_EVENT)
  2439. zlog_debug ("Packet from [%s] received on link %s"
  2440. " but no ospf_interface",
  2441. inet_ntoa (iph->ip_src), ifp->name);
  2442. return 0;
  2443. }
  2444. }
  2445. /* else it must be a local ospf interface, check it was received on
  2446. * correct link
  2447. */
  2448. else if (oi->ifp != ifp)
  2449. {
  2450. if (IS_DEBUG_OSPF_EVENT)
  2451. zlog_warn ("Packet from [%s] received on wrong link %s",
  2452. inet_ntoa (iph->ip_src), ifp->name);
  2453. return 0;
  2454. }
  2455. else if (oi->state == ISM_Down)
  2456. {
  2457. char buf[2][INET_ADDRSTRLEN];
  2458. zlog_warn ("Ignoring packet from %s to %s received on interface that is "
  2459. "down [%s]; interface flags are %s",
  2460. inet_ntop(AF_INET, &iph->ip_src, buf[0], sizeof(buf[0])),
  2461. inet_ntop(AF_INET, &iph->ip_dst, buf[1], sizeof(buf[1])),
  2462. ifp->name, if_flag_dump(ifp->flags));
  2463. /* Fix multicast memberships? */
  2464. if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS))
  2465. OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
  2466. else if (iph->ip_dst.s_addr == htonl(OSPF_ALLDROUTERS))
  2467. OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
  2468. if (oi->multicast_memberships)
  2469. ospf_if_set_multicast(oi);
  2470. return 0;
  2471. }
  2472. /*
  2473. * If the received packet is destined for AllDRouters, the packet
  2474. * should be accepted only if the received ospf interface state is
  2475. * either DR or Backup -- endo.
  2476. */
  2477. if (iph->ip_dst.s_addr == htonl (OSPF_ALLDROUTERS)
  2478. && (oi->state != ISM_DR && oi->state != ISM_Backup))
  2479. {
  2480. zlog_warn ("Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
  2481. inet_ntoa (iph->ip_src), IF_NAME (oi),
  2482. LOOKUP (ospf_ism_state_msg, oi->state));
  2483. /* Try to fix multicast membership. */
  2484. SET_FLAG(oi->multicast_memberships, MEMBER_DROUTERS);
  2485. ospf_if_set_multicast(oi);
  2486. return 0;
  2487. }
  2488. /* Verify more OSPF header fields. */
  2489. ret = ospf_verify_header (ibuf, oi, iph, ospfh);
  2490. if (ret < 0)
  2491. {
  2492. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2493. zlog_debug ("ospf_read[%s]: Header check failed, "
  2494. "dropping.",
  2495. inet_ntoa (iph->ip_src));
  2496. return ret;
  2497. }
  2498. /* Show debug receiving packet. */
  2499. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2500. {
  2501. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
  2502. {
  2503. zlog_debug ("-----------------------------------------------------");
  2504. ospf_packet_dump (ibuf);
  2505. }
  2506. zlog_debug ("%s received from [%s] via [%s]",
  2507. LOOKUP (ospf_packet_type_str, ospfh->type),
  2508. inet_ntoa (ospfh->router_id), IF_NAME (oi));
  2509. zlog_debug (" src [%s],", inet_ntoa (iph->ip_src));
  2510. zlog_debug (" dst [%s]", inet_ntoa (iph->ip_dst));
  2511. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
  2512. zlog_debug ("-----------------------------------------------------");
  2513. }
  2514. stream_forward_getp (ibuf, OSPF_HEADER_SIZE);
  2515. /* Adjust size to message length. */
  2516. length = ntohs (ospfh->length) - OSPF_HEADER_SIZE;
  2517. /* Read rest of the packet and call each sort of packet routine. */
  2518. switch (ospfh->type)
  2519. {
  2520. case OSPF_MSG_HELLO:
  2521. ospf_hello (iph, ospfh, ibuf, oi, length);
  2522. break;
  2523. case OSPF_MSG_DB_DESC:
  2524. ospf_db_desc (iph, ospfh, ibuf, oi, length);
  2525. break;
  2526. case OSPF_MSG_LS_REQ:
  2527. ospf_ls_req (iph, ospfh, ibuf, oi, length);
  2528. break;
  2529. case OSPF_MSG_LS_UPD:
  2530. ospf_ls_upd (ospf, iph, ospfh, ibuf, oi, length);
  2531. break;
  2532. case OSPF_MSG_LS_ACK:
  2533. ospf_ls_ack (iph, ospfh, ibuf, oi, length);
  2534. break;
  2535. default:
  2536. zlog (NULL, LOG_WARNING,
  2537. "interface %s: OSPF packet header type %d is illegal",
  2538. IF_NAME (oi), ospfh->type);
  2539. break;
  2540. }
  2541. return 0;
  2542. }
  2543. /* Make OSPF header. */
  2544. static void
  2545. ospf_make_header (int type, struct ospf_interface *oi, struct stream *s)
  2546. {
  2547. struct ospf_header *ospfh;
  2548. ospfh = (struct ospf_header *) STREAM_DATA (s);
  2549. ospfh->version = (u_char) OSPF_VERSION;
  2550. ospfh->type = (u_char) type;
  2551. ospfh->router_id = oi->ospf->router_id;
  2552. ospfh->checksum = 0;
  2553. ospfh->area_id = oi->area->area_id;
  2554. ospfh->auth_type = htons (ospf_auth_type (oi));
  2555. memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
  2556. stream_forward_endp (s, OSPF_HEADER_SIZE);
  2557. }
  2558. /* Make Authentication Data. */
  2559. static int
  2560. ospf_make_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
  2561. {
  2562. struct crypt_key *ck;
  2563. switch (ospf_auth_type (oi))
  2564. {
  2565. case OSPF_AUTH_NULL:
  2566. /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
  2567. break;
  2568. case OSPF_AUTH_SIMPLE:
  2569. memcpy (ospfh->u.auth_data, OSPF_IF_PARAM (oi, auth_simple),
  2570. OSPF_AUTH_SIMPLE_SIZE);
  2571. break;
  2572. case OSPF_AUTH_CRYPTOGRAPHIC:
  2573. /* If key is not set, then set 0. */
  2574. if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
  2575. {
  2576. ospfh->u.crypt.zero = 0;
  2577. ospfh->u.crypt.key_id = 0;
  2578. ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
  2579. }
  2580. else
  2581. {
  2582. ck = listgetdata (listtail(OSPF_IF_PARAM (oi, auth_crypt)));
  2583. ospfh->u.crypt.zero = 0;
  2584. ospfh->u.crypt.key_id = ck->key_id;
  2585. ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
  2586. }
  2587. /* note: the seq is done in ospf_make_md5_digest() */
  2588. break;
  2589. default:
  2590. /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
  2591. break;
  2592. }
  2593. return 0;
  2594. }
  2595. /* Fill rest of OSPF header. */
  2596. static void
  2597. ospf_fill_header (struct ospf_interface *oi,
  2598. struct stream *s, u_int16_t length)
  2599. {
  2600. struct ospf_header *ospfh;
  2601. ospfh = (struct ospf_header *) STREAM_DATA (s);
  2602. /* Fill length. */
  2603. ospfh->length = htons (length);
  2604. /* Calculate checksum. */
  2605. if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
  2606. ospfh->checksum = in_cksum (ospfh, length);
  2607. else
  2608. ospfh->checksum = 0;
  2609. /* Add Authentication Data. */
  2610. ospf_make_auth (oi, ospfh);
  2611. }
  2612. static int
  2613. ospf_make_hello (struct ospf_interface *oi, struct stream *s)
  2614. {
  2615. struct ospf_neighbor *nbr;
  2616. struct route_node *rn;
  2617. u_int16_t length = OSPF_HELLO_MIN_SIZE;
  2618. struct in_addr mask;
  2619. unsigned long p;
  2620. int flag = 0;
  2621. /* Set netmask of interface. */
  2622. if (!(CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED) &&
  2623. oi->type == OSPF_IFTYPE_POINTOPOINT) &&
  2624. oi->type != OSPF_IFTYPE_VIRTUALLINK)
  2625. masklen2ip (oi->address->prefixlen, &mask);
  2626. else
  2627. memset ((char *) &mask, 0, sizeof (struct in_addr));
  2628. stream_put_ipv4 (s, mask.s_addr);
  2629. /* Set Hello Interval. */
  2630. if (OSPF_IF_PARAM (oi, fast_hello) == 0)
  2631. stream_putw (s, OSPF_IF_PARAM (oi, v_hello));
  2632. else
  2633. stream_putw (s, 0); /* hello-interval of 0 for fast-hellos */
  2634. if (IS_DEBUG_OSPF_EVENT)
  2635. zlog_debug ("make_hello: options: %x, int: %s",
  2636. OPTIONS(oi), IF_NAME (oi));
  2637. /* Set Options. */
  2638. stream_putc (s, OPTIONS (oi));
  2639. /* Set Router Priority. */
  2640. stream_putc (s, PRIORITY (oi));
  2641. /* Set Router Dead Interval. */
  2642. stream_putl (s, OSPF_IF_PARAM (oi, v_wait));
  2643. /* Set Designated Router. */
  2644. stream_put_ipv4 (s, DR (oi).s_addr);
  2645. p = stream_get_endp (s);
  2646. /* Set Backup Designated Router. */
  2647. stream_put_ipv4 (s, BDR (oi).s_addr);
  2648. /* Add neighbor seen. */
  2649. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  2650. if ((nbr = rn->info))
  2651. if (nbr->router_id.s_addr != 0) /* Ignore 0.0.0.0 node. */
  2652. if (nbr->state != NSM_Attempt) /* Ignore Down neighbor. */
  2653. if (nbr->state != NSM_Down) /* This is myself for DR election. */
  2654. if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  2655. {
  2656. /* Check neighbor is sane? */
  2657. if (nbr->d_router.s_addr != 0
  2658. && IPV4_ADDR_SAME (&nbr->d_router, &oi->address->u.prefix4)
  2659. && IPV4_ADDR_SAME (&nbr->bd_router, &oi->address->u.prefix4))
  2660. flag = 1;
  2661. stream_put_ipv4 (s, nbr->router_id.s_addr);
  2662. length += 4;
  2663. }
  2664. /* Let neighbor generate BackupSeen. */
  2665. if (flag == 1)
  2666. stream_putl_at (s, p, 0); /* ipv4 address, normally */
  2667. return length;
  2668. }
  2669. static int
  2670. ospf_make_db_desc (struct ospf_interface *oi, struct ospf_neighbor *nbr,
  2671. struct stream *s)
  2672. {
  2673. struct ospf_lsa *lsa;
  2674. u_int16_t length = OSPF_DB_DESC_MIN_SIZE;
  2675. u_char options;
  2676. unsigned long pp;
  2677. int i;
  2678. struct ospf_lsdb *lsdb;
  2679. /* Set Interface MTU. */
  2680. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  2681. stream_putw (s, 0);
  2682. else
  2683. stream_putw (s, oi->ifp->mtu);
  2684. /* Set Options. */
  2685. options = OPTIONS (oi);
  2686. if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  2687. SET_FLAG (options, OSPF_OPTION_O);
  2688. stream_putc (s, options);
  2689. /* DD flags */
  2690. pp = stream_get_endp (s);
  2691. stream_putc (s, nbr->dd_flags);
  2692. /* Set DD Sequence Number. */
  2693. stream_putl (s, nbr->dd_seqnum);
  2694. /* shortcut unneeded walk of (empty) summary LSDBs */
  2695. if (ospf_db_summary_isempty (nbr))
  2696. goto empty;
  2697. /* Describe LSA Header from Database Summary List. */
  2698. lsdb = &nbr->db_sum;
  2699. for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
  2700. {
  2701. struct route_table *table = lsdb->type[i].db;
  2702. struct route_node *rn;
  2703. for (rn = route_top (table); rn; rn = route_next (rn))
  2704. if ((lsa = rn->info) != NULL)
  2705. {
  2706. if (IS_OPAQUE_LSA (lsa->data->type)
  2707. && (! CHECK_FLAG (options, OSPF_OPTION_O)))
  2708. {
  2709. /* Suppress advertising opaque-informations. */
  2710. /* Remove LSA from DB summary list. */
  2711. ospf_lsdb_delete (lsdb, lsa);
  2712. continue;
  2713. }
  2714. if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
  2715. {
  2716. struct lsa_header *lsah;
  2717. u_int16_t ls_age;
  2718. /* DD packet overflows interface MTU. */
  2719. if (length + OSPF_LSA_HEADER_SIZE > ospf_packet_max (oi))
  2720. break;
  2721. /* Keep pointer to LS age. */
  2722. lsah = (struct lsa_header *) (STREAM_DATA (s) +
  2723. stream_get_endp (s));
  2724. /* Proceed stream pointer. */
  2725. stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
  2726. length += OSPF_LSA_HEADER_SIZE;
  2727. /* Set LS age. */
  2728. ls_age = LS_AGE (lsa);
  2729. lsah->ls_age = htons (ls_age);
  2730. }
  2731. /* Remove LSA from DB summary list. */
  2732. ospf_lsdb_delete (lsdb, lsa);
  2733. }
  2734. }
  2735. /* Update 'More' bit */
  2736. if (ospf_db_summary_isempty (nbr))
  2737. {
  2738. empty:
  2739. if (nbr->state >= NSM_Exchange)
  2740. {
  2741. UNSET_FLAG (nbr->dd_flags, OSPF_DD_FLAG_M);
  2742. /* Rewrite DD flags */
  2743. stream_putc_at (s, pp, nbr->dd_flags);
  2744. }
  2745. else
  2746. {
  2747. assert (IS_SET_DD_M(nbr->dd_flags));
  2748. }
  2749. }
  2750. return length;
  2751. }
  2752. static int
  2753. ospf_make_ls_req_func (struct stream *s, u_int16_t *length,
  2754. unsigned long delta, struct ospf_neighbor *nbr,
  2755. struct ospf_lsa *lsa)
  2756. {
  2757. struct ospf_interface *oi;
  2758. oi = nbr->oi;
  2759. /* LS Request packet overflows interface MTU. */
  2760. if (*length + delta > ospf_packet_max(oi))
  2761. return 0;
  2762. stream_putl (s, lsa->data->type);
  2763. stream_put_ipv4 (s, lsa->data->id.s_addr);
  2764. stream_put_ipv4 (s, lsa->data->adv_router.s_addr);
  2765. ospf_lsa_unlock (&nbr->ls_req_last);
  2766. nbr->ls_req_last = ospf_lsa_lock (lsa);
  2767. *length += 12;
  2768. return 1;
  2769. }
  2770. static int
  2771. ospf_make_ls_req (struct ospf_neighbor *nbr, struct stream *s)
  2772. {
  2773. struct ospf_lsa *lsa;
  2774. u_int16_t length = OSPF_LS_REQ_MIN_SIZE;
  2775. unsigned long delta = stream_get_endp(s)+12;
  2776. struct route_table *table;
  2777. struct route_node *rn;
  2778. int i;
  2779. struct ospf_lsdb *lsdb;
  2780. lsdb = &nbr->ls_req;
  2781. for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
  2782. {
  2783. table = lsdb->type[i].db;
  2784. for (rn = route_top (table); rn; rn = route_next (rn))
  2785. if ((lsa = (rn->info)) != NULL)
  2786. if (ospf_make_ls_req_func (s, &length, delta, nbr, lsa) == 0)
  2787. {
  2788. route_unlock_node (rn);
  2789. break;
  2790. }
  2791. }
  2792. return length;
  2793. }
  2794. static int
  2795. ls_age_increment (struct ospf_lsa *lsa, int delay)
  2796. {
  2797. int age;
  2798. age = IS_LSA_MAXAGE (lsa) ? OSPF_LSA_MAXAGE : LS_AGE (lsa) + delay;
  2799. return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age);
  2800. }
  2801. static int
  2802. ospf_make_ls_upd (struct ospf_interface *oi, struct list *update, struct stream *s)
  2803. {
  2804. struct ospf_lsa *lsa;
  2805. struct listnode *node;
  2806. u_int16_t length = 0;
  2807. unsigned int size_noauth;
  2808. unsigned long delta = stream_get_endp (s);
  2809. unsigned long pp;
  2810. int count = 0;
  2811. if (IS_DEBUG_OSPF_EVENT)
  2812. zlog_debug ("ospf_make_ls_upd: Start");
  2813. pp = stream_get_endp (s);
  2814. stream_forward_endp (s, OSPF_LS_UPD_MIN_SIZE);
  2815. length += OSPF_LS_UPD_MIN_SIZE;
  2816. /* Calculate amount of packet usable for data. */
  2817. size_noauth = stream_get_size(s) - ospf_packet_authspace(oi);
  2818. while ((node = listhead (update)) != NULL)
  2819. {
  2820. struct lsa_header *lsah;
  2821. u_int16_t ls_age;
  2822. if (IS_DEBUG_OSPF_EVENT)
  2823. zlog_debug ("ospf_make_ls_upd: List Iteration %d", count);
  2824. lsa = listgetdata (node);
  2825. assert (lsa->data);
  2826. /* Will it fit? */
  2827. if (length + delta + ntohs (lsa->data->length) > size_noauth)
  2828. break;
  2829. /* Keep pointer to LS age. */
  2830. lsah = (struct lsa_header *) (STREAM_DATA (s) + stream_get_endp (s));
  2831. /* Put LSA to Link State Request. */
  2832. stream_put (s, lsa->data, ntohs (lsa->data->length));
  2833. /* Set LS age. */
  2834. /* each hop must increment an lsa_age by transmit_delay
  2835. of OSPF interface */
  2836. ls_age = ls_age_increment (lsa, OSPF_IF_PARAM (oi, transmit_delay));
  2837. lsah->ls_age = htons (ls_age);
  2838. length += ntohs (lsa->data->length);
  2839. count++;
  2840. list_delete_node (update, node);
  2841. ospf_lsa_unlock (&lsa); /* oi->ls_upd_queue */
  2842. }
  2843. /* Now set #LSAs. */
  2844. stream_putl_at (s, pp, count);
  2845. if (IS_DEBUG_OSPF_EVENT)
  2846. zlog_debug ("ospf_make_ls_upd: Stop");
  2847. return length;
  2848. }
  2849. static int
  2850. ospf_make_ls_ack (struct ospf_interface *oi, struct list *ack, struct stream *s)
  2851. {
  2852. struct listnode *node, *nnode;
  2853. u_int16_t length = OSPF_LS_ACK_MIN_SIZE;
  2854. unsigned long delta = stream_get_endp(s) + 24;
  2855. struct ospf_lsa *lsa;
  2856. for (ALL_LIST_ELEMENTS (ack, node, nnode, lsa))
  2857. {
  2858. assert (lsa);
  2859. if (length + delta > ospf_packet_max (oi))
  2860. break;
  2861. stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
  2862. length += OSPF_LSA_HEADER_SIZE;
  2863. listnode_delete (ack, lsa);
  2864. ospf_lsa_unlock (&lsa); /* oi->ls_ack_direct.ls_ack */
  2865. }
  2866. return length;
  2867. }
  2868. static void
  2869. ospf_hello_send_sub (struct ospf_interface *oi, in_addr_t addr)
  2870. {
  2871. struct ospf_packet *op;
  2872. u_int16_t length = OSPF_HEADER_SIZE;
  2873. op = ospf_packet_new (oi->ifp->mtu);
  2874. /* Prepare OSPF common header. */
  2875. ospf_make_header (OSPF_MSG_HELLO, oi, op->s);
  2876. /* Prepare OSPF Hello body. */
  2877. length += ospf_make_hello (oi, op->s);
  2878. /* Fill OSPF header. */
  2879. ospf_fill_header (oi, op->s, length);
  2880. /* Set packet length. */
  2881. op->length = length;
  2882. op->dst.s_addr = addr;
  2883. /* Add packet to the top of the interface output queue, so that they
  2884. * can't get delayed by things like long queues of LS Update packets
  2885. */
  2886. ospf_packet_add_top (oi, op);
  2887. /* Hook thread to write packet. */
  2888. OSPF_ISM_WRITE_ON (oi->ospf);
  2889. }
  2890. static void
  2891. ospf_poll_send (struct ospf_nbr_nbma *nbr_nbma)
  2892. {
  2893. struct ospf_interface *oi;
  2894. oi = nbr_nbma->oi;
  2895. assert(oi);
  2896. /* If this is passive interface, do not send OSPF Hello. */
  2897. if (OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
  2898. return;
  2899. if (oi->type != OSPF_IFTYPE_NBMA)
  2900. return;
  2901. if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down)
  2902. return;
  2903. if (PRIORITY(oi) == 0)
  2904. return;
  2905. if (nbr_nbma->priority == 0
  2906. && oi->state != ISM_DR && oi->state != ISM_Backup)
  2907. return;
  2908. ospf_hello_send_sub (oi, nbr_nbma->addr.s_addr);
  2909. }
  2910. int
  2911. ospf_poll_timer (struct thread *thread)
  2912. {
  2913. struct ospf_nbr_nbma *nbr_nbma;
  2914. nbr_nbma = THREAD_ARG (thread);
  2915. nbr_nbma->t_poll = NULL;
  2916. if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
  2917. zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (Poll timer expire)",
  2918. IF_NAME (nbr_nbma->oi), inet_ntoa (nbr_nbma->addr));
  2919. ospf_poll_send (nbr_nbma);
  2920. if (nbr_nbma->v_poll > 0)
  2921. OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
  2922. nbr_nbma->v_poll);
  2923. return 0;
  2924. }
  2925. int
  2926. ospf_hello_reply_timer (struct thread *thread)
  2927. {
  2928. struct ospf_neighbor *nbr;
  2929. nbr = THREAD_ARG (thread);
  2930. nbr->t_hello_reply = NULL;
  2931. assert (nbr->oi);
  2932. if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
  2933. zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (hello-reply timer expire)",
  2934. IF_NAME (nbr->oi), inet_ntoa (nbr->router_id));
  2935. ospf_hello_send_sub (nbr->oi, nbr->address.u.prefix4.s_addr);
  2936. return 0;
  2937. }
  2938. /* Send OSPF Hello. */
  2939. void
  2940. ospf_hello_send (struct ospf_interface *oi)
  2941. {
  2942. /* If this is passive interface, do not send OSPF Hello. */
  2943. if (OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
  2944. return;
  2945. if (oi->type == OSPF_IFTYPE_NBMA)
  2946. {
  2947. struct ospf_neighbor *nbr;
  2948. struct route_node *rn;
  2949. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  2950. if ((nbr = rn->info))
  2951. if (nbr != oi->nbr_self)
  2952. if (nbr->state != NSM_Down)
  2953. {
  2954. /* RFC 2328 Section 9.5.1
  2955. If the router is not eligible to become Designated Router,
  2956. it must periodically send Hello Packets to both the
  2957. Designated Router and the Backup Designated Router (if they
  2958. exist). */
  2959. if (PRIORITY(oi) == 0 &&
  2960. IPV4_ADDR_CMP(&DR(oi), &nbr->address.u.prefix4) &&
  2961. IPV4_ADDR_CMP(&BDR(oi), &nbr->address.u.prefix4))
  2962. continue;
  2963. /* If the router is eligible to become Designated Router, it
  2964. must periodically send Hello Packets to all neighbors that
  2965. are also eligible. In addition, if the router is itself the
  2966. Designated Router or Backup Designated Router, it must also
  2967. send periodic Hello Packets to all other neighbors. */
  2968. if (nbr->priority == 0 && oi->state == ISM_DROther)
  2969. continue;
  2970. /* if oi->state == Waiting, send hello to all neighbors */
  2971. ospf_hello_send_sub (oi, nbr->address.u.prefix4.s_addr);
  2972. }
  2973. }
  2974. else
  2975. {
  2976. /* Decide destination address. */
  2977. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  2978. ospf_hello_send_sub (oi, oi->vl_data->peer_addr.s_addr);
  2979. else
  2980. ospf_hello_send_sub (oi, htonl (OSPF_ALLSPFROUTERS));
  2981. }
  2982. }
  2983. /* Send OSPF Database Description. */
  2984. void
  2985. ospf_db_desc_send (struct ospf_neighbor *nbr)
  2986. {
  2987. struct ospf_interface *oi;
  2988. struct ospf_packet *op;
  2989. u_int16_t length = OSPF_HEADER_SIZE;
  2990. oi = nbr->oi;
  2991. op = ospf_packet_new (oi->ifp->mtu);
  2992. /* Prepare OSPF common header. */
  2993. ospf_make_header (OSPF_MSG_DB_DESC, oi, op->s);
  2994. /* Prepare OSPF Database Description body. */
  2995. length += ospf_make_db_desc (oi, nbr, op->s);
  2996. /* Fill OSPF header. */
  2997. ospf_fill_header (oi, op->s, length);
  2998. /* Set packet length. */
  2999. op->length = length;
  3000. /* Decide destination address. */
  3001. if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3002. op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3003. else
  3004. op->dst = nbr->address.u.prefix4;
  3005. /* Add packet to the interface output queue. */
  3006. ospf_packet_add (oi, op);
  3007. /* Hook thread to write packet. */
  3008. OSPF_ISM_WRITE_ON (oi->ospf);
  3009. /* Remove old DD packet, then copy new one and keep in neighbor structure. */
  3010. if (nbr->last_send)
  3011. ospf_packet_free (nbr->last_send);
  3012. nbr->last_send = ospf_packet_dup (op);
  3013. quagga_gettime (QUAGGA_CLK_MONOTONIC, &nbr->last_send_ts);
  3014. }
  3015. /* Re-send Database Description. */
  3016. void
  3017. ospf_db_desc_resend (struct ospf_neighbor *nbr)
  3018. {
  3019. struct ospf_interface *oi;
  3020. oi = nbr->oi;
  3021. /* Add packet to the interface output queue. */
  3022. ospf_packet_add (oi, ospf_packet_dup (nbr->last_send));
  3023. /* Hook thread to write packet. */
  3024. OSPF_ISM_WRITE_ON (oi->ospf);
  3025. }
  3026. /* Send Link State Request. */
  3027. void
  3028. ospf_ls_req_send (struct ospf_neighbor *nbr)
  3029. {
  3030. struct ospf_interface *oi;
  3031. struct ospf_packet *op;
  3032. u_int16_t length = OSPF_HEADER_SIZE;
  3033. oi = nbr->oi;
  3034. op = ospf_packet_new (oi->ifp->mtu);
  3035. /* Prepare OSPF common header. */
  3036. ospf_make_header (OSPF_MSG_LS_REQ, oi, op->s);
  3037. /* Prepare OSPF Link State Request body. */
  3038. length += ospf_make_ls_req (nbr, op->s);
  3039. if (length == OSPF_HEADER_SIZE)
  3040. {
  3041. ospf_packet_free (op);
  3042. return;
  3043. }
  3044. /* Fill OSPF header. */
  3045. ospf_fill_header (oi, op->s, length);
  3046. /* Set packet length. */
  3047. op->length = length;
  3048. /* Decide destination address. */
  3049. if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3050. op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3051. else
  3052. op->dst = nbr->address.u.prefix4;
  3053. /* Add packet to the interface output queue. */
  3054. ospf_packet_add (oi, op);
  3055. /* Hook thread to write packet. */
  3056. OSPF_ISM_WRITE_ON (oi->ospf);
  3057. /* Add Link State Request Retransmission Timer. */
  3058. OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
  3059. }
  3060. /* Send Link State Update with an LSA. */
  3061. void
  3062. ospf_ls_upd_send_lsa (struct ospf_neighbor *nbr, struct ospf_lsa *lsa,
  3063. int flag)
  3064. {
  3065. struct list *update;
  3066. update = list_new ();
  3067. listnode_add (update, lsa);
  3068. ospf_ls_upd_send (nbr, update, flag);
  3069. list_delete (update);
  3070. }
  3071. /* Determine size for packet. Must be at least big enough to accomodate next
  3072. * LSA on list, which may be bigger than MTU size.
  3073. *
  3074. * Return pointer to new ospf_packet
  3075. * NULL if we can not allocate, eg because LSA is bigger than imposed limit
  3076. * on packet sizes (in which case offending LSA is deleted from update list)
  3077. */
  3078. static struct ospf_packet *
  3079. ospf_ls_upd_packet_new (struct list *update, struct ospf_interface *oi)
  3080. {
  3081. struct ospf_lsa *lsa;
  3082. struct listnode *ln;
  3083. size_t size;
  3084. static char warned = 0;
  3085. lsa = listgetdata((ln = listhead (update)));
  3086. assert (lsa->data);
  3087. if ((OSPF_LS_UPD_MIN_SIZE + ntohs (lsa->data->length))
  3088. > ospf_packet_max (oi))
  3089. {
  3090. if (!warned)
  3091. {
  3092. zlog_warn ("ospf_ls_upd_packet_new: oversized LSA encountered!"
  3093. "will need to fragment. Not optimal. Try divide up"
  3094. " your network with areas. Use 'debug ospf packet send'"
  3095. " to see details, or look at 'show ip ospf database ..'");
  3096. warned = 1;
  3097. }
  3098. if (IS_DEBUG_OSPF_PACKET (0, SEND))
  3099. zlog_debug ("ospf_ls_upd_packet_new: oversized LSA id:%s,"
  3100. " %d bytes originated by %s, will be fragmented!",
  3101. inet_ntoa (lsa->data->id),
  3102. ntohs (lsa->data->length),
  3103. inet_ntoa (lsa->data->adv_router));
  3104. /*
  3105. * Allocate just enough to fit this LSA only, to avoid including other
  3106. * LSAs in fragmented LSA Updates.
  3107. */
  3108. size = ntohs (lsa->data->length) + (oi->ifp->mtu - ospf_packet_max (oi))
  3109. + OSPF_LS_UPD_MIN_SIZE;
  3110. }
  3111. else
  3112. size = oi->ifp->mtu;
  3113. if (size > OSPF_MAX_PACKET_SIZE)
  3114. {
  3115. zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s too big,"
  3116. " %d bytes, packet size %ld, dropping it completely."
  3117. " OSPF routing is broken!",
  3118. inet_ntoa (lsa->data->id), ntohs (lsa->data->length),
  3119. (long int) size);
  3120. list_delete_node (update, ln);
  3121. return NULL;
  3122. }
  3123. /* IP header is built up separately by ospf_write(). This means, that we must
  3124. * reduce the "affordable" size just calculated by length of an IP header.
  3125. * This makes sure, that even if we manage to fill the payload with LSA data
  3126. * completely, the final packet (our data plus IP header) still fits into
  3127. * outgoing interface MTU. This correction isn't really meaningful for an
  3128. * oversized LSA, but for consistency the correction is done for both cases.
  3129. *
  3130. * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size
  3131. */
  3132. return ospf_packet_new (size - sizeof (struct ip));
  3133. }
  3134. static void
  3135. ospf_ls_upd_queue_send (struct ospf_interface *oi, struct list *update,
  3136. struct in_addr addr)
  3137. {
  3138. struct ospf_packet *op;
  3139. u_int16_t length = OSPF_HEADER_SIZE;
  3140. if (IS_DEBUG_OSPF_EVENT)
  3141. zlog_debug ("listcount = %d, [%s]dst %s", listcount (update), IF_NAME(oi),
  3142. inet_ntoa(addr));
  3143. op = ospf_ls_upd_packet_new (update, oi);
  3144. /* Prepare OSPF common header. */
  3145. ospf_make_header (OSPF_MSG_LS_UPD, oi, op->s);
  3146. /* Prepare OSPF Link State Update body.
  3147. * Includes Type-7 translation.
  3148. */
  3149. length += ospf_make_ls_upd (oi, update, op->s);
  3150. /* Fill OSPF header. */
  3151. ospf_fill_header (oi, op->s, length);
  3152. /* Set packet length. */
  3153. op->length = length;
  3154. /* Decide destination address. */
  3155. if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3156. op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3157. else
  3158. op->dst.s_addr = addr.s_addr;
  3159. /* Add packet to the interface output queue. */
  3160. ospf_packet_add (oi, op);
  3161. /* Hook thread to write packet. */
  3162. OSPF_ISM_WRITE_ON (oi->ospf);
  3163. }
  3164. static int
  3165. ospf_ls_upd_send_queue_event (struct thread *thread)
  3166. {
  3167. struct ospf_interface *oi = THREAD_ARG(thread);
  3168. struct route_node *rn;
  3169. struct route_node *rnext;
  3170. struct list *update;
  3171. char again = 0;
  3172. oi->t_ls_upd_event = NULL;
  3173. if (IS_DEBUG_OSPF_EVENT)
  3174. zlog_debug ("ospf_ls_upd_send_queue start");
  3175. for (rn = route_top (oi->ls_upd_queue); rn; rn = rnext)
  3176. {
  3177. rnext = route_next (rn);
  3178. if (rn->info == NULL)
  3179. continue;
  3180. update = (struct list *)rn->info;
  3181. ospf_ls_upd_queue_send (oi, update, rn->p.u.prefix4);
  3182. /* list might not be empty. */
  3183. if (listcount(update) == 0)
  3184. {
  3185. list_delete (rn->info);
  3186. rn->info = NULL;
  3187. route_unlock_node (rn);
  3188. }
  3189. else
  3190. again = 1;
  3191. }
  3192. if (again != 0)
  3193. {
  3194. if (IS_DEBUG_OSPF_EVENT)
  3195. zlog_debug ("ospf_ls_upd_send_queue: update lists not cleared,"
  3196. " %d nodes to try again, raising new event", again);
  3197. oi->t_ls_upd_event =
  3198. thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0);
  3199. }
  3200. if (IS_DEBUG_OSPF_EVENT)
  3201. zlog_debug ("ospf_ls_upd_send_queue stop");
  3202. return 0;
  3203. }
  3204. void
  3205. ospf_ls_upd_send (struct ospf_neighbor *nbr, struct list *update, int flag)
  3206. {
  3207. struct ospf_interface *oi;
  3208. struct ospf_lsa *lsa;
  3209. struct prefix_ipv4 p;
  3210. struct route_node *rn;
  3211. struct listnode *node;
  3212. oi = nbr->oi;
  3213. p.family = AF_INET;
  3214. p.prefixlen = IPV4_MAX_BITLEN;
  3215. /* Decide destination address. */
  3216. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  3217. p.prefix = oi->vl_data->peer_addr;
  3218. else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3219. p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3220. else if (flag == OSPF_SEND_PACKET_DIRECT)
  3221. p.prefix = nbr->address.u.prefix4;
  3222. else if (oi->state == ISM_DR || oi->state == ISM_Backup)
  3223. p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3224. else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
  3225. p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3226. else
  3227. p.prefix.s_addr = htonl (OSPF_ALLDROUTERS);
  3228. if (oi->type == OSPF_IFTYPE_NBMA)
  3229. {
  3230. if (flag == OSPF_SEND_PACKET_INDIRECT)
  3231. zlog_warn ("* LS-Update is directly sent on NBMA network.");
  3232. if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix.s_addr))
  3233. zlog_warn ("* LS-Update is sent to myself.");
  3234. }
  3235. rn = route_node_get (oi->ls_upd_queue, (struct prefix *) &p);
  3236. if (rn->info == NULL)
  3237. rn->info = list_new ();
  3238. else
  3239. route_unlock_node (rn);
  3240. for (ALL_LIST_ELEMENTS_RO (update, node, lsa))
  3241. listnode_add (rn->info, ospf_lsa_lock (lsa)); /* oi->ls_upd_queue */
  3242. if (oi->t_ls_upd_event == NULL)
  3243. oi->t_ls_upd_event =
  3244. thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0);
  3245. }
  3246. static void
  3247. ospf_ls_ack_send_list (struct ospf_interface *oi, struct list *ack,
  3248. struct in_addr dst)
  3249. {
  3250. struct ospf_packet *op;
  3251. u_int16_t length = OSPF_HEADER_SIZE;
  3252. op = ospf_packet_new (oi->ifp->mtu);
  3253. /* Prepare OSPF common header. */
  3254. ospf_make_header (OSPF_MSG_LS_ACK, oi, op->s);
  3255. /* Prepare OSPF Link State Acknowledgment body. */
  3256. length += ospf_make_ls_ack (oi, ack, op->s);
  3257. /* Fill OSPF header. */
  3258. ospf_fill_header (oi, op->s, length);
  3259. /* Set packet length. */
  3260. op->length = length;
  3261. /* Decide destination address. */
  3262. if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3263. op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3264. else
  3265. op->dst.s_addr = dst.s_addr;
  3266. /* Add packet to the interface output queue. */
  3267. ospf_packet_add (oi, op);
  3268. /* Hook thread to write packet. */
  3269. OSPF_ISM_WRITE_ON (oi->ospf);
  3270. }
  3271. static int
  3272. ospf_ls_ack_send_event (struct thread *thread)
  3273. {
  3274. struct ospf_interface *oi = THREAD_ARG (thread);
  3275. oi->t_ls_ack_direct = NULL;
  3276. while (listcount (oi->ls_ack_direct.ls_ack))
  3277. ospf_ls_ack_send_list (oi, oi->ls_ack_direct.ls_ack,
  3278. oi->ls_ack_direct.dst);
  3279. return 0;
  3280. }
  3281. void
  3282. ospf_ls_ack_send (struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
  3283. {
  3284. struct ospf_interface *oi = nbr->oi;
  3285. if (listcount (oi->ls_ack_direct.ls_ack) == 0)
  3286. oi->ls_ack_direct.dst = nbr->address.u.prefix4;
  3287. listnode_add (oi->ls_ack_direct.ls_ack, ospf_lsa_lock (lsa));
  3288. if (oi->t_ls_ack_direct == NULL)
  3289. oi->t_ls_ack_direct =
  3290. thread_add_event (master, ospf_ls_ack_send_event, oi, 0);
  3291. }
  3292. /* Send Link State Acknowledgment delayed. */
  3293. void
  3294. ospf_ls_ack_send_delayed (struct ospf_interface *oi)
  3295. {
  3296. struct in_addr dst;
  3297. /* Decide destination address. */
  3298. /* RFC2328 Section 13.5 On non-broadcast
  3299. networks, delayed Link State Acknowledgment packets must be
  3300. unicast separately over each adjacency (i.e., neighbor whose
  3301. state is >= Exchange). */
  3302. if (oi->type == OSPF_IFTYPE_NBMA)
  3303. {
  3304. struct ospf_neighbor *nbr;
  3305. struct route_node *rn;
  3306. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  3307. if ((nbr = rn->info) != NULL)
  3308. if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange)
  3309. while (listcount (oi->ls_ack))
  3310. ospf_ls_ack_send_list (oi, oi->ls_ack, nbr->address.u.prefix4);
  3311. return;
  3312. }
  3313. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  3314. dst.s_addr = oi->vl_data->peer_addr.s_addr;
  3315. else if (oi->state == ISM_DR || oi->state == ISM_Backup)
  3316. dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3317. else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3318. dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3319. else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
  3320. dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3321. else
  3322. dst.s_addr = htonl (OSPF_ALLDROUTERS);
  3323. while (listcount (oi->ls_ack))
  3324. ospf_ls_ack_send_list (oi, oi->ls_ack, dst);
  3325. }