ospf_packet.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952
  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. #endif /* WANT_OSPF_WRITE_FRAGMENT */
  531. u_int16_t maxdatasize;
  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. #endif /* WANT_OSPF_WRITE_FRAGMENT */
  543. /* convenience - max OSPF data per packet,
  544. * and reliability - not more data, than our
  545. * socket can accept
  546. */
  547. maxdatasize = MIN (oi->ifp->mtu, ospf->maxsndbuflen) -
  548. sizeof (struct ip);
  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_T))
  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. #ifdef HAVE_OPAQUE_LSA
  742. if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)
  743. && CHECK_FLAG (hello->options, OSPF_OPTION_O))
  744. {
  745. /*
  746. * This router does know the correct usage of O-bit
  747. * the bit should be set in DD packet only.
  748. */
  749. zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
  750. inet_ntoa (ospfh->router_id));
  751. #ifdef STRICT_OBIT_USAGE_CHECK
  752. return; /* Reject this packet. */
  753. #else /* STRICT_OBIT_USAGE_CHECK */
  754. UNSET_FLAG (hello->options, OSPF_OPTION_O); /* Ignore O-bit. */
  755. #endif /* STRICT_OBIT_USAGE_CHECK */
  756. }
  757. #endif /* HAVE_OPAQUE_LSA */
  758. /* new for NSSA is to ensure that NP is on and E is off */
  759. if (oi->area->external_routing == OSPF_AREA_NSSA)
  760. {
  761. if (! (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_NP)
  762. && CHECK_FLAG (hello->options, OSPF_OPTION_NP)
  763. && ! CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E)
  764. && ! CHECK_FLAG (hello->options, OSPF_OPTION_E)))
  765. {
  766. zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh->router_id), OPTIONS (oi), hello->options);
  767. return;
  768. }
  769. if (IS_DEBUG_OSPF_NSSA)
  770. zlog_debug ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh->router_id));
  771. }
  772. else
  773. /* The setting of the E-bit found in the Hello Packet's Options
  774. field must match this area's ExternalRoutingCapability A
  775. mismatch causes processing to stop and the packet to be
  776. dropped. The setting of the rest of the bits in the Hello
  777. Packet's Options field should be ignored. */
  778. if (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) !=
  779. CHECK_FLAG (hello->options, OSPF_OPTION_E))
  780. {
  781. zlog_warn ("Packet %s [Hello:RECV]: my options: %x, his options %x",
  782. inet_ntoa(ospfh->router_id), OPTIONS (oi), hello->options);
  783. return;
  784. }
  785. /* get neighbour struct */
  786. nbr = ospf_nbr_get (oi, ospfh, iph, &p);
  787. /* neighbour must be valid, ospf_nbr_get creates if none existed */
  788. assert (nbr);
  789. old_state = nbr->state;
  790. /* Add event to thread. */
  791. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  792. /* RFC2328 Section 9.5.1
  793. If the router is not eligible to become Designated Router,
  794. (snip) It must also send an Hello Packet in reply to an
  795. Hello Packet received from any eligible neighbor (other than
  796. the current Designated Router and Backup Designated Router). */
  797. if (oi->type == OSPF_IFTYPE_NBMA)
  798. if (PRIORITY(oi) == 0 && hello->priority > 0
  799. && IPV4_ADDR_CMP(&DR(oi), &iph->ip_src)
  800. && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src))
  801. OSPF_NSM_TIMER_ON (nbr->t_hello_reply, ospf_hello_reply_timer,
  802. OSPF_HELLO_REPLY_DELAY);
  803. /* on NBMA network type, it happens to receive bidirectional Hello packet
  804. without advance 1-Way Received event.
  805. To avoid incorrect DR-seletion, raise 1-Way Received event.*/
  806. if (oi->type == OSPF_IFTYPE_NBMA &&
  807. (old_state == NSM_Down || old_state == NSM_Attempt))
  808. {
  809. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived);
  810. nbr->priority = hello->priority;
  811. nbr->d_router = hello->d_router;
  812. nbr->bd_router = hello->bd_router;
  813. return;
  814. }
  815. if (ospf_nbr_bidirectional (&oi->ospf->router_id, hello->neighbors,
  816. size - OSPF_HELLO_MIN_SIZE))
  817. {
  818. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_TwoWayReceived);
  819. nbr->options |= hello->options;
  820. }
  821. else
  822. {
  823. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived);
  824. /* Set neighbor information. */
  825. nbr->priority = hello->priority;
  826. nbr->d_router = hello->d_router;
  827. nbr->bd_router = hello->bd_router;
  828. return;
  829. }
  830. /* If neighbor itself declares DR and no BDR exists,
  831. cause event BackupSeen */
  832. if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router))
  833. if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting)
  834. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
  835. /* neighbor itself declares BDR. */
  836. if (oi->state == ISM_Waiting &&
  837. IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router))
  838. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
  839. /* had not previously. */
  840. if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router) &&
  841. IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->d_router)) ||
  842. (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->d_router) &&
  843. IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->d_router)))
  844. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  845. /* had not previously. */
  846. if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router) &&
  847. IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->bd_router)) ||
  848. (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->bd_router) &&
  849. IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->bd_router)))
  850. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  851. /* Neighbor priority check. */
  852. if (nbr->priority >= 0 && nbr->priority != hello->priority)
  853. OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  854. /* Set neighbor information. */
  855. nbr->priority = hello->priority;
  856. nbr->d_router = hello->d_router;
  857. nbr->bd_router = hello->bd_router;
  858. }
  859. /* Save DD flags/options/Seqnum received. */
  860. static void
  861. ospf_db_desc_save_current (struct ospf_neighbor *nbr,
  862. struct ospf_db_desc *dd)
  863. {
  864. nbr->last_recv.flags = dd->flags;
  865. nbr->last_recv.options = dd->options;
  866. nbr->last_recv.dd_seqnum = ntohl (dd->dd_seqnum);
  867. }
  868. /* Process rest of DD packet. */
  869. static void
  870. ospf_db_desc_proc (struct stream *s, struct ospf_interface *oi,
  871. struct ospf_neighbor *nbr, struct ospf_db_desc *dd,
  872. u_int16_t size)
  873. {
  874. struct ospf_lsa *new, *find;
  875. struct lsa_header *lsah;
  876. stream_forward_getp (s, OSPF_DB_DESC_MIN_SIZE);
  877. for (size -= OSPF_DB_DESC_MIN_SIZE;
  878. size >= OSPF_LSA_HEADER_SIZE; size -= OSPF_LSA_HEADER_SIZE)
  879. {
  880. lsah = (struct lsa_header *) STREAM_PNT (s);
  881. stream_forward_getp (s, OSPF_LSA_HEADER_SIZE);
  882. /* Unknown LS type. */
  883. if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
  884. {
  885. zlog_warn ("Packet [DD:RECV]: Unknown LS type %d.", lsah->type);
  886. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  887. return;
  888. }
  889. #ifdef HAVE_OPAQUE_LSA
  890. if (IS_OPAQUE_LSA (lsah->type)
  891. && ! CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  892. {
  893. zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
  894. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  895. return;
  896. }
  897. #endif /* HAVE_OPAQUE_LSA */
  898. switch (lsah->type)
  899. {
  900. case OSPF_AS_EXTERNAL_LSA:
  901. #ifdef HAVE_OPAQUE_LSA
  902. case OSPF_OPAQUE_AS_LSA:
  903. #endif /* HAVE_OPAQUE_LSA */
  904. /* Check for stub area. Reject if AS-External from stub but
  905. allow if from NSSA. */
  906. if (oi->area->external_routing == OSPF_AREA_STUB)
  907. {
  908. zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
  909. lsah->type, inet_ntoa (lsah->id),
  910. (oi->area->external_routing == OSPF_AREA_STUB) ?\
  911. "STUB" : "NSSA");
  912. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  913. return;
  914. }
  915. break;
  916. default:
  917. break;
  918. }
  919. /* Create LS-request object. */
  920. new = ospf_ls_request_new (lsah);
  921. /* Lookup received LSA, then add LS request list. */
  922. find = ospf_lsa_lookup_by_header (oi->area, lsah);
  923. /* ospf_lsa_more_recent is fine with NULL pointers */
  924. switch (ospf_lsa_more_recent (find, new))
  925. {
  926. case -1:
  927. /* Neighbour has a more recent LSA, we must request it */
  928. ospf_ls_request_add (nbr, new);
  929. case 0:
  930. /* If we have a copy of this LSA, it's either less recent
  931. * and we're requesting it from neighbour (the case above), or
  932. * it's as recent and we both have same copy (this case).
  933. *
  934. * In neither of these two cases is there any point in
  935. * describing our copy of the LSA to the neighbour in a
  936. * DB-Summary packet, if we're still intending to do so.
  937. *
  938. * See: draft-ogier-ospf-dbex-opt-00.txt, describing the
  939. * backward compatible optimisation to OSPF DB Exchange /
  940. * DB Description process implemented here.
  941. */
  942. if (find)
  943. ospf_lsdb_delete (&nbr->db_sum, find);
  944. ospf_lsa_discard (new);
  945. break;
  946. default:
  947. /* We have the more recent copy, nothing specific to do:
  948. * - no need to request neighbours stale copy
  949. * - must leave DB summary list copy alone
  950. */
  951. if (IS_DEBUG_OSPF_EVENT)
  952. zlog_debug ("Packet [DD:RECV]: LSA received Type %d, "
  953. "ID %s is not recent.", lsah->type, inet_ntoa (lsah->id));
  954. ospf_lsa_discard (new);
  955. }
  956. }
  957. /* Master */
  958. if (IS_SET_DD_MS (nbr->dd_flags))
  959. {
  960. nbr->dd_seqnum++;
  961. /* Both sides have no More, then we're done with Exchange */
  962. if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags))
  963. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
  964. else
  965. ospf_db_desc_send (nbr);
  966. }
  967. /* Slave */
  968. else
  969. {
  970. nbr->dd_seqnum = ntohl (dd->dd_seqnum);
  971. /* Send DD packet in reply.
  972. *
  973. * Must be done to acknowledge the Master's DD, regardless of
  974. * whether we have more LSAs ourselves to describe.
  975. *
  976. * This function will clear the 'More' bit, if after this DD
  977. * we have no more LSAs to describe to the master..
  978. */
  979. ospf_db_desc_send (nbr);
  980. /* Slave can raise ExchangeDone now, if master is also done */
  981. if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags))
  982. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
  983. }
  984. /* Save received neighbor values from DD. */
  985. ospf_db_desc_save_current (nbr, dd);
  986. }
  987. static int
  988. ospf_db_desc_is_dup (struct ospf_db_desc *dd, struct ospf_neighbor *nbr)
  989. {
  990. /* Is DD duplicated? */
  991. if (dd->options == nbr->last_recv.options &&
  992. dd->flags == nbr->last_recv.flags &&
  993. dd->dd_seqnum == htonl (nbr->last_recv.dd_seqnum))
  994. return 1;
  995. return 0;
  996. }
  997. /* OSPF Database Description message read -- RFC2328 Section 10.6. */
  998. static void
  999. ospf_db_desc (struct ip *iph, struct ospf_header *ospfh,
  1000. struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1001. {
  1002. struct ospf_db_desc *dd;
  1003. struct ospf_neighbor *nbr;
  1004. /* Increment statistics. */
  1005. oi->db_desc_in++;
  1006. dd = (struct ospf_db_desc *) STREAM_PNT (s);
  1007. nbr = ospf_nbr_lookup (oi, iph, ospfh);
  1008. if (nbr == NULL)
  1009. {
  1010. zlog_warn ("Packet[DD]: Unknown Neighbor %s",
  1011. inet_ntoa (ospfh->router_id));
  1012. return;
  1013. }
  1014. /* Check MTU. */
  1015. if ((OSPF_IF_PARAM (oi, mtu_ignore) == 0) &&
  1016. (ntohs (dd->mtu) > oi->ifp->mtu))
  1017. {
  1018. zlog_warn ("Packet[DD]: Neighbor %s MTU %u is larger than [%s]'s MTU %u",
  1019. inet_ntoa (nbr->router_id), ntohs (dd->mtu),
  1020. IF_NAME (oi), oi->ifp->mtu);
  1021. return;
  1022. }
  1023. /*
  1024. * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not
  1025. * required. In fact at least JunOS sends DD packets with P bit clear.
  1026. * Until proper solution is developped, this hack should help.
  1027. *
  1028. * Update: According to the RFCs, N bit is specified /only/ for Hello
  1029. * options, unfortunately its use in DD options is not specified. Hence some
  1030. * implementations follow E-bit semantics and set it in DD options, and some
  1031. * treat it as unspecified and hence follow the directive "default for
  1032. * options is clear", ie unset.
  1033. *
  1034. * Reset the flag, as ospfd follows E-bit semantics.
  1035. */
  1036. if ( (oi->area->external_routing == OSPF_AREA_NSSA)
  1037. && (CHECK_FLAG (nbr->options, OSPF_OPTION_NP))
  1038. && (!CHECK_FLAG (dd->options, OSPF_OPTION_NP)) )
  1039. {
  1040. if (IS_DEBUG_OSPF_EVENT)
  1041. zlog_debug ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
  1042. inet_ntoa (nbr->router_id) );
  1043. SET_FLAG (dd->options, OSPF_OPTION_NP);
  1044. }
  1045. #ifdef REJECT_IF_TBIT_ON
  1046. if (CHECK_FLAG (dd->options, OSPF_OPTION_T))
  1047. {
  1048. /*
  1049. * In Hello protocol, optional capability must have checked
  1050. * to prevent this T-bit enabled router be my neighbor.
  1051. */
  1052. zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr->router_id));
  1053. return;
  1054. }
  1055. #endif /* REJECT_IF_TBIT_ON */
  1056. #ifdef HAVE_OPAQUE_LSA
  1057. if (CHECK_FLAG (dd->options, OSPF_OPTION_O)
  1058. && !CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  1059. {
  1060. /*
  1061. * This node is not configured to handle O-bit, for now.
  1062. * Clear it to ignore unsupported capability proposed by neighbor.
  1063. */
  1064. UNSET_FLAG (dd->options, OSPF_OPTION_O);
  1065. }
  1066. #endif /* HAVE_OPAQUE_LSA */
  1067. /* Add event to thread. */
  1068. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  1069. /* Process DD packet by neighbor status. */
  1070. switch (nbr->state)
  1071. {
  1072. case NSM_Down:
  1073. case NSM_Attempt:
  1074. case NSM_TwoWay:
  1075. zlog_warn ("Packet[DD]: Neighbor %s state is %s, packet discarded.",
  1076. inet_ntoa(nbr->router_id),
  1077. LOOKUP (ospf_nsm_state_msg, nbr->state));
  1078. break;
  1079. case NSM_Init:
  1080. OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived);
  1081. /* If the new state is ExStart, the processing of the current
  1082. packet should then continue in this new state by falling
  1083. through to case ExStart below. */
  1084. if (nbr->state != NSM_ExStart)
  1085. break;
  1086. case NSM_ExStart:
  1087. /* Initial DBD */
  1088. if ((IS_SET_DD_ALL (dd->flags) == OSPF_DD_FLAG_ALL) &&
  1089. (size == OSPF_DB_DESC_MIN_SIZE))
  1090. {
  1091. if (IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) > 0)
  1092. {
  1093. /* We're Slave---obey */
  1094. zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Slave).",
  1095. inet_ntoa(nbr->router_id));
  1096. nbr->dd_seqnum = ntohl (dd->dd_seqnum);
  1097. /* Reset I/MS */
  1098. UNSET_FLAG (nbr->dd_flags, (OSPF_DD_FLAG_MS|OSPF_DD_FLAG_I));
  1099. }
  1100. else
  1101. {
  1102. /* We're Master, ignore the initial DBD from Slave */
  1103. zlog_info ("Packet[DD]: Neighbor %s: Initial DBD from Slave, "
  1104. "ignoring.", inet_ntoa(nbr->router_id));
  1105. break;
  1106. }
  1107. }
  1108. /* Ack from the Slave */
  1109. else if (!IS_SET_DD_MS (dd->flags) && !IS_SET_DD_I (dd->flags) &&
  1110. ntohl (dd->dd_seqnum) == nbr->dd_seqnum &&
  1111. IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) < 0)
  1112. {
  1113. zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Master).",
  1114. inet_ntoa(nbr->router_id));
  1115. /* Reset I, leaving MS */
  1116. UNSET_FLAG (nbr->dd_flags, OSPF_DD_FLAG_I);
  1117. }
  1118. else
  1119. {
  1120. zlog_warn ("Packet[DD]: Neighbor %s Negotiation fails.",
  1121. inet_ntoa(nbr->router_id));
  1122. break;
  1123. }
  1124. /* This is where the real Options are saved */
  1125. nbr->options = dd->options;
  1126. #ifdef HAVE_OPAQUE_LSA
  1127. if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  1128. {
  1129. if (IS_DEBUG_OSPF_EVENT)
  1130. zlog_debug ("Neighbor[%s] is %sOpaque-capable.",
  1131. inet_ntoa (nbr->router_id),
  1132. CHECK_FLAG (nbr->options, OSPF_OPTION_O) ? "" : "NOT ");
  1133. if (! CHECK_FLAG (nbr->options, OSPF_OPTION_O)
  1134. && IPV4_ADDR_SAME (&DR (oi), &nbr->address.u.prefix4))
  1135. {
  1136. zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; "
  1137. "Opaque-LSAs cannot be reliably advertised "
  1138. "in this network.",
  1139. inet_ntoa (nbr->router_id));
  1140. /* This situation is undesirable, but not a real error. */
  1141. }
  1142. }
  1143. #endif /* HAVE_OPAQUE_LSA */
  1144. OSPF_NSM_EVENT_EXECUTE (nbr, NSM_NegotiationDone);
  1145. /* continue processing rest of packet. */
  1146. ospf_db_desc_proc (s, oi, nbr, dd, size);
  1147. break;
  1148. case NSM_Exchange:
  1149. if (ospf_db_desc_is_dup (dd, nbr))
  1150. {
  1151. if (IS_SET_DD_MS (nbr->dd_flags))
  1152. /* Master: discard duplicated DD packet. */
  1153. zlog_info ("Packet[DD] (Master): Neighbor %s packet duplicated.",
  1154. inet_ntoa (nbr->router_id));
  1155. else
  1156. /* Slave: cause to retransmit the last Database Description. */
  1157. {
  1158. zlog_info ("Packet[DD] [Slave]: Neighbor %s packet duplicated.",
  1159. inet_ntoa (nbr->router_id));
  1160. ospf_db_desc_resend (nbr);
  1161. }
  1162. break;
  1163. }
  1164. /* Otherwise DD packet should be checked. */
  1165. /* Check Master/Slave bit mismatch */
  1166. if (IS_SET_DD_MS (dd->flags) != IS_SET_DD_MS (nbr->last_recv.flags))
  1167. {
  1168. zlog_warn ("Packet[DD]: Neighbor %s MS-bit mismatch.",
  1169. inet_ntoa(nbr->router_id));
  1170. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1171. if (IS_DEBUG_OSPF_EVENT)
  1172. zlog_debug ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
  1173. dd->flags, nbr->dd_flags);
  1174. break;
  1175. }
  1176. /* Check initialize bit is set. */
  1177. if (IS_SET_DD_I (dd->flags))
  1178. {
  1179. zlog_info ("Packet[DD]: Neighbor %s I-bit set.",
  1180. inet_ntoa(nbr->router_id));
  1181. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1182. break;
  1183. }
  1184. /* Check DD Options. */
  1185. if (dd->options != nbr->options)
  1186. {
  1187. #ifdef ORIGINAL_CODING
  1188. /* Save the new options for debugging */
  1189. nbr->options = dd->options;
  1190. #endif /* ORIGINAL_CODING */
  1191. zlog_warn ("Packet[DD]: Neighbor %s options mismatch.",
  1192. inet_ntoa(nbr->router_id));
  1193. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1194. break;
  1195. }
  1196. /* Check DD sequence number. */
  1197. if ((IS_SET_DD_MS (nbr->dd_flags) &&
  1198. ntohl (dd->dd_seqnum) != nbr->dd_seqnum) ||
  1199. (!IS_SET_DD_MS (nbr->dd_flags) &&
  1200. ntohl (dd->dd_seqnum) != nbr->dd_seqnum + 1))
  1201. {
  1202. zlog_warn ("Packet[DD]: Neighbor %s sequence number mismatch.",
  1203. inet_ntoa(nbr->router_id));
  1204. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1205. break;
  1206. }
  1207. /* Continue processing rest of packet. */
  1208. ospf_db_desc_proc (s, oi, nbr, dd, size);
  1209. break;
  1210. case NSM_Loading:
  1211. case NSM_Full:
  1212. if (ospf_db_desc_is_dup (dd, nbr))
  1213. {
  1214. if (IS_SET_DD_MS (nbr->dd_flags))
  1215. {
  1216. /* Master should discard duplicate DD packet. */
  1217. zlog_info ("Packet[DD]: Neighbor %s duplicated, "
  1218. "packet discarded.",
  1219. inet_ntoa(nbr->router_id));
  1220. break;
  1221. }
  1222. else
  1223. {
  1224. struct timeval t, now;
  1225. quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
  1226. t = tv_sub (now, nbr->last_send_ts);
  1227. if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0)
  1228. {
  1229. /* In states Loading and Full the slave must resend
  1230. its last Database Description packet in response to
  1231. duplicate Database Description packets received
  1232. from the master. For this reason the slave must
  1233. wait RouterDeadInterval seconds before freeing the
  1234. last Database Description packet. Reception of a
  1235. Database Description packet from the master after
  1236. this interval will generate a SeqNumberMismatch
  1237. neighbor event. RFC2328 Section 10.8 */
  1238. ospf_db_desc_resend (nbr);
  1239. break;
  1240. }
  1241. }
  1242. }
  1243. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1244. break;
  1245. default:
  1246. zlog_warn ("Packet[DD]: Neighbor %s NSM illegal status %u.",
  1247. inet_ntoa(nbr->router_id), nbr->state);
  1248. break;
  1249. }
  1250. }
  1251. #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
  1252. /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
  1253. static void
  1254. ospf_ls_req (struct ip *iph, struct ospf_header *ospfh,
  1255. struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1256. {
  1257. struct ospf_neighbor *nbr;
  1258. u_int32_t ls_type;
  1259. struct in_addr ls_id;
  1260. struct in_addr adv_router;
  1261. struct ospf_lsa *find;
  1262. struct list *ls_upd;
  1263. unsigned int length;
  1264. /* Increment statistics. */
  1265. oi->ls_req_in++;
  1266. nbr = ospf_nbr_lookup (oi, iph, ospfh);
  1267. if (nbr == NULL)
  1268. {
  1269. zlog_warn ("Link State Request: Unknown Neighbor %s.",
  1270. inet_ntoa (ospfh->router_id));
  1271. return;
  1272. }
  1273. /* Add event to thread. */
  1274. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  1275. /* Neighbor State should be Exchange or later. */
  1276. if (nbr->state != NSM_Exchange &&
  1277. nbr->state != NSM_Loading &&
  1278. nbr->state != NSM_Full)
  1279. {
  1280. zlog_warn ("Link State Request received from %s: "
  1281. "Neighbor state is %s, packet discarded.",
  1282. inet_ntoa (ospfh->router_id),
  1283. LOOKUP (ospf_nsm_state_msg, nbr->state));
  1284. return;
  1285. }
  1286. /* Send Link State Update for ALL requested LSAs. */
  1287. ls_upd = list_new ();
  1288. length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
  1289. while (size >= OSPF_LSA_KEY_SIZE)
  1290. {
  1291. /* Get one slice of Link State Request. */
  1292. ls_type = stream_getl (s);
  1293. ls_id.s_addr = stream_get_ipv4 (s);
  1294. adv_router.s_addr = stream_get_ipv4 (s);
  1295. /* Verify LSA type. */
  1296. if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA)
  1297. {
  1298. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1299. list_delete (ls_upd);
  1300. return;
  1301. }
  1302. /* Search proper LSA in LSDB. */
  1303. find = ospf_lsa_lookup (oi->area, ls_type, ls_id, adv_router);
  1304. if (find == NULL)
  1305. {
  1306. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1307. list_delete (ls_upd);
  1308. return;
  1309. }
  1310. /* Packet overflows MTU size, send immediately. */
  1311. if (length + ntohs (find->data->length) > ospf_packet_max (oi))
  1312. {
  1313. if (oi->type == OSPF_IFTYPE_NBMA)
  1314. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
  1315. else
  1316. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
  1317. /* Only remove list contents. Keep ls_upd. */
  1318. list_delete_all_node (ls_upd);
  1319. length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
  1320. }
  1321. /* Append LSA to update list. */
  1322. listnode_add (ls_upd, find);
  1323. length += ntohs (find->data->length);
  1324. size -= OSPF_LSA_KEY_SIZE;
  1325. }
  1326. /* Send rest of Link State Update. */
  1327. if (listcount (ls_upd) > 0)
  1328. {
  1329. if (oi->type == OSPF_IFTYPE_NBMA)
  1330. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
  1331. else
  1332. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
  1333. list_delete (ls_upd);
  1334. }
  1335. else
  1336. list_free (ls_upd);
  1337. }
  1338. /* Get the list of LSAs from Link State Update packet.
  1339. And process some validation -- RFC2328 Section 13. (1)-(2). */
  1340. static struct list *
  1341. ospf_ls_upd_list_lsa (struct ospf_neighbor *nbr, struct stream *s,
  1342. struct ospf_interface *oi, size_t size)
  1343. {
  1344. u_int16_t count, sum;
  1345. u_int32_t length;
  1346. struct lsa_header *lsah;
  1347. struct ospf_lsa *lsa;
  1348. struct list *lsas;
  1349. lsas = list_new ();
  1350. count = stream_getl (s);
  1351. size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */
  1352. for (; size >= OSPF_LSA_HEADER_SIZE && count > 0;
  1353. size -= length, stream_forward_getp (s, length), count--)
  1354. {
  1355. lsah = (struct lsa_header *) STREAM_PNT (s);
  1356. length = ntohs (lsah->length);
  1357. if (length > size)
  1358. {
  1359. zlog_warn ("Link State Update: LSA length exceeds packet size.");
  1360. break;
  1361. }
  1362. /* Validate the LSA's LS checksum. */
  1363. sum = lsah->checksum;
  1364. if (! ospf_lsa_checksum_valid (lsah))
  1365. {
  1366. /* (bug #685) more details in a one-line message make it possible
  1367. * to identify problem source on the one hand and to have a better
  1368. * chance to compress repeated messages in syslog on the other */
  1369. zlog_warn ("Link State Update: LSA checksum error %x/%x, ID=%s from: nbr %s, router ID %s, adv router %s",
  1370. sum, lsah->checksum, inet_ntoa (lsah->id),
  1371. inet_ntoa (nbr->src), inet_ntoa (nbr->router_id),
  1372. inet_ntoa (lsah->adv_router));
  1373. continue;
  1374. }
  1375. /* Examine the LSA's LS type. */
  1376. if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
  1377. {
  1378. zlog_warn ("Link State Update: Unknown LS type %d", lsah->type);
  1379. continue;
  1380. }
  1381. /*
  1382. * What if the received LSA's age is greater than MaxAge?
  1383. * Treat it as a MaxAge case -- endo.
  1384. */
  1385. if (ntohs (lsah->ls_age) > OSPF_LSA_MAXAGE)
  1386. lsah->ls_age = htons (OSPF_LSA_MAXAGE);
  1387. #ifdef HAVE_OPAQUE_LSA
  1388. if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  1389. {
  1390. #ifdef STRICT_OBIT_USAGE_CHECK
  1391. if ((IS_OPAQUE_LSA(lsah->type) &&
  1392. ! CHECK_FLAG (lsah->options, OSPF_OPTION_O))
  1393. || (! IS_OPAQUE_LSA(lsah->type) &&
  1394. CHECK_FLAG (lsah->options, OSPF_OPTION_O)))
  1395. {
  1396. /*
  1397. * This neighbor must know the exact usage of O-bit;
  1398. * the bit will be set in Type-9,10,11 LSAs only.
  1399. */
  1400. zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah->type, inet_ntoa (lsah->id));
  1401. continue;
  1402. }
  1403. #endif /* STRICT_OBIT_USAGE_CHECK */
  1404. /* Do not take in AS External Opaque-LSAs if we are a stub. */
  1405. if (lsah->type == OSPF_OPAQUE_AS_LSA
  1406. && nbr->oi->area->external_routing != OSPF_AREA_DEFAULT)
  1407. {
  1408. if (IS_DEBUG_OSPF_EVENT)
  1409. zlog_debug ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah->type, inet_ntoa (lsah->id));
  1410. continue;
  1411. }
  1412. }
  1413. else if (IS_OPAQUE_LSA(lsah->type))
  1414. {
  1415. zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
  1416. continue;
  1417. }
  1418. #endif /* HAVE_OPAQUE_LSA */
  1419. /* Create OSPF LSA instance. */
  1420. lsa = ospf_lsa_new ();
  1421. /* We may wish to put some error checking if type NSSA comes in
  1422. and area not in NSSA mode */
  1423. switch (lsah->type)
  1424. {
  1425. case OSPF_AS_EXTERNAL_LSA:
  1426. #ifdef HAVE_OPAQUE_LSA
  1427. case OSPF_OPAQUE_AS_LSA:
  1428. #endif /* HAVE_OPAQUE_LSA */
  1429. lsa->area = NULL;
  1430. break;
  1431. #ifdef HAVE_OPAQUE_LSA
  1432. case OSPF_OPAQUE_LINK_LSA:
  1433. lsa->oi = oi; /* Remember incoming interface for flooding control. */
  1434. /* Fallthrough */
  1435. #endif /* HAVE_OPAQUE_LSA */
  1436. default:
  1437. lsa->area = oi->area;
  1438. break;
  1439. }
  1440. lsa->data = ospf_lsa_data_new (length);
  1441. memcpy (lsa->data, lsah, length);
  1442. if (IS_DEBUG_OSPF_EVENT)
  1443. zlog_debug("LSA[Type%d:%s]: %p new LSA created with Link State Update",
  1444. lsa->data->type, inet_ntoa (lsa->data->id), (void *)lsa);
  1445. listnode_add (lsas, lsa);
  1446. }
  1447. return lsas;
  1448. }
  1449. /* Cleanup Update list. */
  1450. static void
  1451. ospf_upd_list_clean (struct list *lsas)
  1452. {
  1453. struct listnode *node, *nnode;
  1454. struct ospf_lsa *lsa;
  1455. for (ALL_LIST_ELEMENTS (lsas, node, nnode, lsa))
  1456. ospf_lsa_discard (lsa);
  1457. list_delete (lsas);
  1458. }
  1459. /* OSPF Link State Update message read -- RFC2328 Section 13. */
  1460. static void
  1461. ospf_ls_upd (struct ip *iph, struct ospf_header *ospfh,
  1462. struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1463. {
  1464. struct ospf_neighbor *nbr;
  1465. struct list *lsas;
  1466. struct listnode *node, *nnode;
  1467. struct ospf_lsa *lsa = NULL;
  1468. /* unsigned long ls_req_found = 0; */
  1469. /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */
  1470. /* Increment statistics. */
  1471. oi->ls_upd_in++;
  1472. /* Check neighbor. */
  1473. nbr = ospf_nbr_lookup (oi, iph, ospfh);
  1474. if (nbr == NULL)
  1475. {
  1476. zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
  1477. inet_ntoa (ospfh->router_id), IF_NAME (oi));
  1478. return;
  1479. }
  1480. /* Add event to thread. */
  1481. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  1482. /* Check neighbor state. */
  1483. if (nbr->state < NSM_Exchange)
  1484. {
  1485. zlog_warn ("Link State Update: "
  1486. "Neighbor[%s] state %s is less than Exchange",
  1487. inet_ntoa (ospfh->router_id),
  1488. LOOKUP(ospf_nsm_state_msg, nbr->state));
  1489. return;
  1490. }
  1491. /* Get list of LSAs from Link State Update packet. - Also perorms Stages
  1492. * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
  1493. * of section 13.
  1494. */
  1495. lsas = ospf_ls_upd_list_lsa (nbr, s, oi, size);
  1496. #ifdef HAVE_OPAQUE_LSA
  1497. /*
  1498. * If self-originated Opaque-LSAs that have flooded before restart
  1499. * are contained in the received LSUpd message, corresponding LSReq
  1500. * messages to be sent may have to be modified.
  1501. * To eliminate possible race conditions such that flushing and normal
  1502. * updating for the same LSA would take place alternately, this trick
  1503. * must be done before entering to the loop below.
  1504. */
  1505. /* XXX: Why is this Opaque specific? Either our core code is deficient
  1506. * and this should be fixed generally, or Opaque is inventing strawman
  1507. * problems */
  1508. ospf_opaque_adjust_lsreq (nbr, lsas);
  1509. #endif /* HAVE_OPAQUE_LSA */
  1510. #define DISCARD_LSA(L,N) {\
  1511. if (IS_DEBUG_OSPF_EVENT) \
  1512. zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p" \
  1513. " Type-%d", N, (void *)lsa, (int) lsa->data->type); \
  1514. ospf_lsa_discard (L); \
  1515. continue; }
  1516. /* Process each LSA received in the one packet.
  1517. *
  1518. * Numbers in parentheses, e.g. (1), (2), etc., and the corresponding
  1519. * text below are from the steps in RFC 2328, Section 13.
  1520. */
  1521. for (ALL_LIST_ELEMENTS (lsas, node, nnode, lsa))
  1522. {
  1523. struct ospf_lsa *ls_ret, *current;
  1524. int ret = 1;
  1525. if (IS_DEBUG_OSPF_NSSA)
  1526. {
  1527. char buf1[INET_ADDRSTRLEN];
  1528. char buf2[INET_ADDRSTRLEN];
  1529. char buf3[INET_ADDRSTRLEN];
  1530. zlog_debug("LSA Type-%d from %s, ID: %s, ADV: %s",
  1531. lsa->data->type,
  1532. inet_ntop (AF_INET, &ospfh->router_id,
  1533. buf1, INET_ADDRSTRLEN),
  1534. inet_ntop (AF_INET, &lsa->data->id,
  1535. buf2, INET_ADDRSTRLEN),
  1536. inet_ntop (AF_INET, &lsa->data->adv_router,
  1537. buf3, INET_ADDRSTRLEN));
  1538. }
  1539. listnode_delete (lsas, lsa); /* We don't need it in list anymore */
  1540. /* (1) Validate Checksum - Done above by ospf_ls_upd_list_lsa() */
  1541. /* (2) LSA Type - Done above by ospf_ls_upd_list_lsa() */
  1542. /* (3) Do not take in AS External LSAs if we are a stub or NSSA. */
  1543. /* Do not take in AS NSSA if this neighbor and we are not NSSA */
  1544. /* Do take in Type-7's if we are an NSSA */
  1545. /* If we are also an ABR, later translate them to a Type-5 packet */
  1546. /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
  1547. translate them to a separate Type-5 packet. */
  1548. if (lsa->data->type == OSPF_AS_EXTERNAL_LSA)
  1549. /* Reject from STUB or NSSA */
  1550. if (nbr->oi->area->external_routing != OSPF_AREA_DEFAULT)
  1551. {
  1552. if (IS_DEBUG_OSPF_NSSA)
  1553. zlog_debug("Incoming External LSA Discarded: We are NSSA/STUB Area");
  1554. DISCARD_LSA (lsa, 1);
  1555. }
  1556. if (lsa->data->type == OSPF_AS_NSSA_LSA)
  1557. if (nbr->oi->area->external_routing != OSPF_AREA_NSSA)
  1558. {
  1559. if (IS_DEBUG_OSPF_NSSA)
  1560. zlog_debug("Incoming NSSA LSA Discarded: Not NSSA Area");
  1561. DISCARD_LSA (lsa,2);
  1562. }
  1563. /* VU229804: Router-LSA Adv-ID must be equal to LS-ID */
  1564. if (lsa->data->type == OSPF_ROUTER_LSA)
  1565. if (!IPV4_ADDR_SAME(&lsa->data->id, &lsa->data->adv_router))
  1566. {
  1567. char buf1[INET_ADDRSTRLEN];
  1568. char buf2[INET_ADDRSTRLEN];
  1569. char buf3[INET_ADDRSTRLEN];
  1570. zlog_err("Incoming Router-LSA from %s with "
  1571. "Adv-ID[%s] != LS-ID[%s]",
  1572. inet_ntop (AF_INET, &ospfh->router_id,
  1573. buf1, INET_ADDRSTRLEN),
  1574. inet_ntop (AF_INET, &lsa->data->id,
  1575. buf2, INET_ADDRSTRLEN),
  1576. inet_ntop (AF_INET, &lsa->data->adv_router,
  1577. buf3, INET_ADDRSTRLEN));
  1578. zlog_err("OSPF domain compromised by attack or corruption. "
  1579. "Verify correct operation of -ALL- OSPF routers.");
  1580. DISCARD_LSA (lsa, 0);
  1581. }
  1582. /* Find the LSA in the current database. */
  1583. current = ospf_lsa_lookup_by_header (oi->area, lsa->data);
  1584. /* (4) If the LSA's LS age is equal to MaxAge, and there is currently
  1585. no instance of the LSA in the router's link state database,
  1586. and none of router's neighbors are in states Exchange or Loading,
  1587. then take the following actions: */
  1588. if (IS_LSA_MAXAGE (lsa) && !current &&
  1589. ospf_check_nbr_status(oi->ospf))
  1590. {
  1591. /* (4a) Response Link State Acknowledgment. */
  1592. ospf_ls_ack_send (nbr, lsa);
  1593. /* (4b) Discard LSA. */
  1594. if (IS_DEBUG_OSPF (lsa, LSA))
  1595. {
  1596. zlog_debug ("Link State Update[%s]: LS age is equal to MaxAge.",
  1597. dump_lsa_key(lsa));
  1598. }
  1599. DISCARD_LSA (lsa, 3);
  1600. }
  1601. #ifdef HAVE_OPAQUE_LSA
  1602. if (IS_OPAQUE_LSA (lsa->data->type)
  1603. && IPV4_ADDR_SAME (&lsa->data->adv_router, &oi->ospf->router_id))
  1604. {
  1605. /*
  1606. * Even if initial flushing seems to be completed, there might
  1607. * be a case that self-originated LSA with MaxAge still remain
  1608. * in the routing domain.
  1609. * Just send an LSAck message to cease retransmission.
  1610. */
  1611. if (IS_LSA_MAXAGE (lsa))
  1612. {
  1613. zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa));
  1614. ospf_ls_ack_send (nbr, lsa);
  1615. ospf_lsa_discard (lsa);
  1616. if (current != NULL && ! IS_LSA_MAXAGE (current))
  1617. ospf_opaque_lsa_refresh_schedule (current);
  1618. continue;
  1619. }
  1620. /*
  1621. * If an instance of self-originated Opaque-LSA is not found
  1622. * in the LSDB, there are some possible cases here.
  1623. *
  1624. * 1) This node lost opaque-capability after restart.
  1625. * 2) Else, a part of opaque-type is no more supported.
  1626. * 3) Else, a part of opaque-id is no more supported.
  1627. *
  1628. * Anyway, it is still this node's responsibility to flush it.
  1629. * Otherwise, the LSA instance remains in the routing domain
  1630. * until its age reaches to MaxAge.
  1631. */
  1632. /* XXX: We should deal with this for *ALL* LSAs, not just opaque */
  1633. if (current == NULL)
  1634. {
  1635. if (IS_DEBUG_OSPF_EVENT)
  1636. zlog_debug ("LSA[%s]: Previously originated Opaque-LSA,"
  1637. "not found in the LSDB.", dump_lsa_key (lsa));
  1638. SET_FLAG (lsa->flags, OSPF_LSA_SELF);
  1639. ospf_opaque_self_originated_lsa_received (nbr, lsa);
  1640. ospf_ls_ack_send (nbr, lsa);
  1641. continue;
  1642. }
  1643. }
  1644. #endif /* HAVE_OPAQUE_LSA */
  1645. /* It might be happen that received LSA is self-originated network LSA, but
  1646. * router ID is changed. So, we should check if LSA is a network-LSA whose
  1647. * Link State ID is one of the router's own IP interface addresses but whose
  1648. * Advertising Router is not equal to the router's own Router ID
  1649. * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed.
  1650. */
  1651. if(lsa->data->type == OSPF_NETWORK_LSA)
  1652. {
  1653. struct listnode *oinode, *oinnode;
  1654. struct ospf_interface *out_if;
  1655. int Flag = 0;
  1656. for (ALL_LIST_ELEMENTS (oi->ospf->oiflist, oinode, oinnode, out_if))
  1657. {
  1658. if(out_if == NULL)
  1659. break;
  1660. if((IPV4_ADDR_SAME(&out_if->address->u.prefix4, &lsa->data->id)) &&
  1661. (!(IPV4_ADDR_SAME(&oi->ospf->router_id, &lsa->data->adv_router))))
  1662. {
  1663. if(out_if->network_lsa_self)
  1664. {
  1665. ospf_lsa_flush_area(lsa,out_if->area);
  1666. if(IS_DEBUG_OSPF_EVENT)
  1667. zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
  1668. (void *)lsa, (int) lsa->data->type);
  1669. ospf_lsa_discard (lsa);
  1670. Flag = 1;
  1671. }
  1672. break;
  1673. }
  1674. }
  1675. if(Flag)
  1676. continue;
  1677. }
  1678. /* (5) Find the instance of this LSA that is currently contained
  1679. in the router's link state database. If there is no
  1680. database copy, or the received LSA is more recent than
  1681. the database copy the following steps must be performed.
  1682. (The sub steps from RFC 2328 section 13 step (5) will be performed in
  1683. ospf_flood() ) */
  1684. if (current == NULL ||
  1685. (ret = ospf_lsa_more_recent (current, lsa)) < 0)
  1686. {
  1687. /* Actual flooding procedure. */
  1688. if (ospf_flood (oi->ospf, nbr, current, lsa) < 0) /* Trap NSSA later. */
  1689. DISCARD_LSA (lsa, 4);
  1690. continue;
  1691. }
  1692. /* (6) Else, If there is an instance of the LSA on the sending
  1693. neighbor's Link state request list, an error has occurred in
  1694. the Database Exchange process. In this case, restart the
  1695. Database Exchange process by generating the neighbor event
  1696. BadLSReq for the sending neighbor and stop processing the
  1697. Link State Update packet. */
  1698. if (ospf_ls_request_lookup (nbr, lsa))
  1699. {
  1700. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1701. zlog_warn("LSA[%s] instance exists on Link state request list",
  1702. dump_lsa_key(lsa));
  1703. /* Clean list of LSAs. */
  1704. ospf_upd_list_clean (lsas);
  1705. /* this lsa is not on lsas list already. */
  1706. ospf_lsa_discard (lsa);
  1707. return;
  1708. }
  1709. /* If the received LSA is the same instance as the database copy
  1710. (i.e., neither one is more recent) the following two steps
  1711. should be performed: */
  1712. if (ret == 0)
  1713. {
  1714. /* If the LSA is listed in the Link state retransmission list
  1715. for the receiving adjacency, the router itself is expecting
  1716. an acknowledgment for this LSA. The router should treat the
  1717. received LSA as an acknowledgment by removing the LSA from
  1718. the Link state retransmission list. This is termed an
  1719. "implied acknowledgment". */
  1720. ls_ret = ospf_ls_retransmit_lookup (nbr, lsa);
  1721. if (ls_ret != NULL)
  1722. {
  1723. ospf_ls_retransmit_delete (nbr, ls_ret);
  1724. /* Delayed acknowledgment sent if advertisement received
  1725. from Designated Router, otherwise do nothing. */
  1726. if (oi->state == ISM_Backup)
  1727. if (NBR_IS_DR (nbr))
  1728. listnode_add (oi->ls_ack, ospf_lsa_lock (lsa));
  1729. DISCARD_LSA (lsa, 5);
  1730. }
  1731. else
  1732. /* Acknowledge the receipt of the LSA by sending a
  1733. Link State Acknowledgment packet back out the receiving
  1734. interface. */
  1735. {
  1736. ospf_ls_ack_send (nbr, lsa);
  1737. DISCARD_LSA (lsa, 6);
  1738. }
  1739. }
  1740. /* The database copy is more recent. If the database copy
  1741. has LS age equal to MaxAge and LS sequence number equal to
  1742. MaxSequenceNumber, simply discard the received LSA without
  1743. acknowledging it. (In this case, the LSA's LS sequence number is
  1744. wrapping, and the MaxSequenceNumber LSA must be completely
  1745. flushed before any new LSA instance can be introduced). */
  1746. else if (ret > 0) /* Database copy is more recent */
  1747. {
  1748. if (IS_LSA_MAXAGE (current) &&
  1749. current->data->ls_seqnum == htonl (OSPF_MAX_SEQUENCE_NUMBER))
  1750. {
  1751. DISCARD_LSA (lsa, 7);
  1752. }
  1753. /* Otherwise, as long as the database copy has not been sent in a
  1754. Link State Update within the last MinLSArrival seconds, send the
  1755. database copy back to the sending neighbor, encapsulated within
  1756. a Link State Update Packet. The Link State Update Packet should
  1757. be sent directly to the neighbor. In so doing, do not put the
  1758. database copy of the LSA on the neighbor's link state
  1759. retransmission list, and do not acknowledge the received (less
  1760. recent) LSA instance. */
  1761. else
  1762. {
  1763. struct timeval now;
  1764. quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
  1765. if (tv_cmp (tv_sub (now, current->tv_orig),
  1766. int2tv (OSPF_MIN_LS_ARRIVAL)) >= 0)
  1767. /* Trap NSSA type later.*/
  1768. ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT);
  1769. DISCARD_LSA (lsa, 8);
  1770. }
  1771. }
  1772. }
  1773. #undef DISCARD_LSA
  1774. assert (listcount (lsas) == 0);
  1775. list_delete (lsas);
  1776. }
  1777. /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
  1778. static void
  1779. ospf_ls_ack (struct ip *iph, struct ospf_header *ospfh,
  1780. struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1781. {
  1782. struct ospf_neighbor *nbr;
  1783. /* increment statistics. */
  1784. oi->ls_ack_in++;
  1785. nbr = ospf_nbr_lookup (oi, iph, ospfh);
  1786. if (nbr == NULL)
  1787. {
  1788. zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
  1789. inet_ntoa (ospfh->router_id));
  1790. return;
  1791. }
  1792. /* Add event to thread. */
  1793. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
  1794. if (nbr->state < NSM_Exchange)
  1795. {
  1796. zlog_warn ("Link State Acknowledgment: "
  1797. "Neighbor[%s] state %s is less than Exchange",
  1798. inet_ntoa (ospfh->router_id),
  1799. LOOKUP(ospf_nsm_state_msg, nbr->state));
  1800. return;
  1801. }
  1802. while (size >= OSPF_LSA_HEADER_SIZE)
  1803. {
  1804. struct ospf_lsa *lsa, *lsr;
  1805. lsa = ospf_lsa_new ();
  1806. lsa->data = (struct lsa_header *) STREAM_PNT (s);
  1807. /* lsah = (struct lsa_header *) STREAM_PNT (s); */
  1808. size -= OSPF_LSA_HEADER_SIZE;
  1809. stream_forward_getp (s, OSPF_LSA_HEADER_SIZE);
  1810. if (lsa->data->type < OSPF_MIN_LSA || lsa->data->type >= OSPF_MAX_LSA)
  1811. {
  1812. lsa->data = NULL;
  1813. ospf_lsa_discard (lsa);
  1814. continue;
  1815. }
  1816. lsr = ospf_ls_retransmit_lookup (nbr, lsa);
  1817. if (lsr != NULL && ospf_lsa_more_recent (lsr, lsa) == 0)
  1818. {
  1819. #ifdef HAVE_OPAQUE_LSA
  1820. if (IS_OPAQUE_LSA (lsr->data->type))
  1821. ospf_opaque_ls_ack_received (nbr, lsr);
  1822. #endif /* HAVE_OPAQUE_LSA */
  1823. ospf_ls_retransmit_delete (nbr, lsr);
  1824. }
  1825. lsa->data = NULL;
  1826. ospf_lsa_discard (lsa);
  1827. }
  1828. return;
  1829. }
  1830. static struct stream *
  1831. ospf_recv_packet (int fd, struct interface **ifp, struct stream *ibuf)
  1832. {
  1833. int ret;
  1834. struct ip *iph;
  1835. u_int16_t ip_len;
  1836. unsigned int ifindex = 0;
  1837. struct iovec iov;
  1838. /* Header and data both require alignment. */
  1839. char buff [CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
  1840. struct msghdr msgh;
  1841. memset (&msgh, 0, sizeof (struct msghdr));
  1842. msgh.msg_iov = &iov;
  1843. msgh.msg_iovlen = 1;
  1844. msgh.msg_control = (caddr_t) buff;
  1845. msgh.msg_controllen = sizeof (buff);
  1846. ret = stream_recvmsg (ibuf, fd, &msgh, 0, OSPF_MAX_PACKET_SIZE+1);
  1847. if (ret < 0)
  1848. {
  1849. zlog_warn("stream_recvmsg failed: %s", safe_strerror(errno));
  1850. return NULL;
  1851. }
  1852. if ((unsigned int)ret < sizeof(iph)) /* ret must be > 0 now */
  1853. {
  1854. zlog_warn("ospf_recv_packet: discarding runt packet of length %d "
  1855. "(ip header size is %u)",
  1856. ret, (u_int)sizeof(iph));
  1857. return NULL;
  1858. }
  1859. /* Note that there should not be alignment problems with this assignment
  1860. because this is at the beginning of the stream data buffer. */
  1861. iph = (struct ip *) STREAM_DATA(ibuf);
  1862. sockopt_iphdrincl_swab_systoh (iph);
  1863. ip_len = iph->ip_len;
  1864. #if !defined(GNU_LINUX) && (OpenBSD < 200311) && (__FreeBSD_version < 1000000)
  1865. /*
  1866. * Kernel network code touches incoming IP header parameters,
  1867. * before protocol specific processing.
  1868. *
  1869. * 1) Convert byteorder to host representation.
  1870. * --> ip_len, ip_id, ip_off
  1871. *
  1872. * 2) Adjust ip_len to strip IP header size!
  1873. * --> If user process receives entire IP packet via RAW
  1874. * socket, it must consider adding IP header size to
  1875. * the "ip_len" field of "ip" structure.
  1876. *
  1877. * For more details, see <netinet/ip_input.c>.
  1878. */
  1879. ip_len = ip_len + (iph->ip_hl << 2);
  1880. #endif
  1881. #if defined(__DragonFly__)
  1882. /*
  1883. * in DragonFly's raw socket, ip_len/ip_off are read
  1884. * in network byte order.
  1885. * As OpenBSD < 200311 adjust ip_len to strip IP header size!
  1886. */
  1887. ip_len = ntohs(iph->ip_len) + (iph->ip_hl << 2);
  1888. #endif
  1889. ifindex = getsockopt_ifindex (AF_INET, &msgh);
  1890. *ifp = if_lookup_by_index (ifindex);
  1891. if (ret != ip_len)
  1892. {
  1893. zlog_warn ("ospf_recv_packet read length mismatch: ip_len is %d, "
  1894. "but recvmsg returned %d", ip_len, ret);
  1895. return NULL;
  1896. }
  1897. return ibuf;
  1898. }
  1899. static struct ospf_interface *
  1900. ospf_associate_packet_vl (struct ospf *ospf, struct interface *ifp,
  1901. struct ip *iph, struct ospf_header *ospfh)
  1902. {
  1903. struct ospf_interface *rcv_oi;
  1904. struct ospf_vl_data *vl_data;
  1905. struct ospf_area *vl_area;
  1906. struct listnode *node;
  1907. if (IN_MULTICAST (ntohl (iph->ip_dst.s_addr)) ||
  1908. !OSPF_IS_AREA_BACKBONE (ospfh))
  1909. return NULL;
  1910. /* look for local OSPF interface matching the destination
  1911. * to determine Area ID. We presume therefore the destination address
  1912. * is unique, or at least (for "unnumbered" links), not used in other
  1913. * areas
  1914. */
  1915. if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, NULL,
  1916. iph->ip_dst)) == NULL)
  1917. return NULL;
  1918. for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
  1919. {
  1920. vl_area = ospf_area_lookup_by_area_id (ospf, vl_data->vl_area_id);
  1921. if (!vl_area)
  1922. continue;
  1923. if (OSPF_AREA_SAME (&vl_area, &rcv_oi->area) &&
  1924. IPV4_ADDR_SAME (&vl_data->vl_peer, &ospfh->router_id))
  1925. {
  1926. if (IS_DEBUG_OSPF_EVENT)
  1927. zlog_debug ("associating packet with %s",
  1928. IF_NAME (vl_data->vl_oi));
  1929. if (! CHECK_FLAG (vl_data->vl_oi->ifp->flags, IFF_UP))
  1930. {
  1931. if (IS_DEBUG_OSPF_EVENT)
  1932. zlog_debug ("This VL is not up yet, sorry");
  1933. return NULL;
  1934. }
  1935. return vl_data->vl_oi;
  1936. }
  1937. }
  1938. if (IS_DEBUG_OSPF_EVENT)
  1939. zlog_debug ("couldn't find any VL to associate the packet with");
  1940. return NULL;
  1941. }
  1942. static int
  1943. ospf_check_area_id (struct ospf_interface *oi, struct ospf_header *ospfh)
  1944. {
  1945. /* Check match the Area ID of the receiving interface. */
  1946. if (OSPF_AREA_SAME (&oi->area, &ospfh))
  1947. return 1;
  1948. return 0;
  1949. }
  1950. /* Unbound socket will accept any Raw IP packets if proto is matched.
  1951. To prevent it, compare src IP address and i/f address with masking
  1952. i/f network mask. */
  1953. static int
  1954. ospf_check_network_mask (struct ospf_interface *oi, struct in_addr ip_src)
  1955. {
  1956. struct in_addr mask, me, him;
  1957. if (oi->type == OSPF_IFTYPE_POINTOPOINT ||
  1958. oi->type == OSPF_IFTYPE_VIRTUALLINK)
  1959. return 1;
  1960. masklen2ip (oi->address->prefixlen, &mask);
  1961. me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
  1962. him.s_addr = ip_src.s_addr & mask.s_addr;
  1963. if (IPV4_ADDR_SAME (&me, &him))
  1964. return 1;
  1965. return 0;
  1966. }
  1967. /* Return 1, if the packet is properly authenticated and checksummed,
  1968. 0 otherwise. In particular, check that AuType header field is valid and
  1969. matches the locally configured AuType, and that D.5 requirements are met. */
  1970. static int
  1971. ospf_check_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
  1972. {
  1973. struct crypt_key *ck;
  1974. u_int16_t iface_auth_type;
  1975. u_int16_t pkt_auth_type = ntohs (ospfh->auth_type);
  1976. switch (pkt_auth_type)
  1977. {
  1978. case OSPF_AUTH_NULL: /* RFC2328 D.5.1 */
  1979. if (OSPF_AUTH_NULL != (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 Null",
  1983. IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
  1984. return 0;
  1985. }
  1986. if (! ospf_check_sum (ospfh))
  1987. {
  1988. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1989. zlog_warn ("interface %s: Null auth OK, but checksum error, Router-ID %s",
  1990. IF_NAME (oi), inet_ntoa (ospfh->router_id));
  1991. return 0;
  1992. }
  1993. return 1;
  1994. case OSPF_AUTH_SIMPLE: /* RFC2328 D.5.2 */
  1995. if (OSPF_AUTH_SIMPLE != (iface_auth_type = ospf_auth_type (oi)))
  1996. {
  1997. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1998. zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Simple",
  1999. IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
  2000. return 0;
  2001. }
  2002. if (memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE))
  2003. {
  2004. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2005. zlog_warn ("interface %s: Simple auth failed", IF_NAME (oi));
  2006. return 0;
  2007. }
  2008. if (! ospf_check_sum (ospfh))
  2009. {
  2010. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2011. zlog_warn ("interface %s: Simple auth OK, checksum error, Router-ID %s",
  2012. IF_NAME (oi), inet_ntoa (ospfh->router_id));
  2013. return 0;
  2014. }
  2015. return 1;
  2016. case OSPF_AUTH_CRYPTOGRAPHIC: /* RFC2328 D.5.3 */
  2017. if (OSPF_AUTH_CRYPTOGRAPHIC != (iface_auth_type = ospf_auth_type (oi)))
  2018. {
  2019. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2020. zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Cryptographic",
  2021. IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
  2022. return 0;
  2023. }
  2024. if (ospfh->checksum)
  2025. {
  2026. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2027. zlog_warn ("interface %s: OSPF header checksum is not 0", IF_NAME (oi));
  2028. return 0;
  2029. }
  2030. /* only MD5 crypto method can pass ospf_packet_examin() */
  2031. if
  2032. (
  2033. NULL == (ck = listgetdata (listtail(OSPF_IF_PARAM (oi,auth_crypt)))) ||
  2034. ospfh->u.crypt.key_id != ck->key_id ||
  2035. /* Condition above uses the last key ID on the list, which is
  2036. different from what ospf_crypt_key_lookup() does. A bug? */
  2037. ! ospf_check_md5_digest (oi, ospfh)
  2038. )
  2039. {
  2040. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2041. zlog_warn ("interface %s: MD5 auth failed", IF_NAME (oi));
  2042. return 0;
  2043. }
  2044. return 1;
  2045. default:
  2046. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2047. zlog_warn ("interface %s: invalid packet auth-type (%02x)",
  2048. IF_NAME (oi), pkt_auth_type);
  2049. return 0;
  2050. }
  2051. }
  2052. static int
  2053. ospf_check_sum (struct ospf_header *ospfh)
  2054. {
  2055. u_int32_t ret;
  2056. u_int16_t sum;
  2057. /* clear auth_data for checksum. */
  2058. memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
  2059. /* keep checksum and clear. */
  2060. sum = ospfh->checksum;
  2061. memset (&ospfh->checksum, 0, sizeof (u_int16_t));
  2062. /* calculate checksum. */
  2063. ret = in_cksum (ospfh, ntohs (ospfh->length));
  2064. if (ret != sum)
  2065. {
  2066. zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X",
  2067. ret, sum);
  2068. return 0;
  2069. }
  2070. return 1;
  2071. }
  2072. /* Verify, that given link/TOS records are properly sized/aligned and match
  2073. Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */
  2074. static unsigned
  2075. ospf_router_lsa_links_examin
  2076. (
  2077. struct router_lsa_link * link,
  2078. u_int16_t linkbytes,
  2079. const u_int16_t num_links
  2080. )
  2081. {
  2082. unsigned counted_links = 0, thislinklen;
  2083. while (linkbytes)
  2084. {
  2085. thislinklen = OSPF_ROUTER_LSA_LINK_SIZE + 4 * link->m[0].tos_count;
  2086. if (thislinklen > linkbytes)
  2087. {
  2088. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2089. zlog_debug ("%s: length error in link block #%u", __func__, counted_links);
  2090. return MSG_NG;
  2091. }
  2092. link = (struct router_lsa_link *)((caddr_t) link + thislinklen);
  2093. linkbytes -= thislinklen;
  2094. counted_links++;
  2095. }
  2096. if (counted_links != num_links)
  2097. {
  2098. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2099. zlog_debug ("%s: %u link blocks declared, %u present",
  2100. __func__, num_links, counted_links);
  2101. return MSG_NG;
  2102. }
  2103. return MSG_OK;
  2104. }
  2105. /* Verify, that the given LSA is properly sized/aligned (including type-specific
  2106. minimum length constraint). */
  2107. static unsigned
  2108. ospf_lsa_examin (struct lsa_header * lsah, const u_int16_t lsalen, const u_char headeronly)
  2109. {
  2110. unsigned ret;
  2111. struct router_lsa * rlsa;
  2112. if
  2113. (
  2114. lsah->type < OSPF_MAX_LSA &&
  2115. ospf_lsa_minlen[lsah->type] &&
  2116. lsalen < OSPF_LSA_HEADER_SIZE + ospf_lsa_minlen[lsah->type]
  2117. )
  2118. {
  2119. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2120. zlog_debug ("%s: undersized (%u B) %s",
  2121. __func__, lsalen, LOOKUP (ospf_lsa_type_msg, lsah->type));
  2122. return MSG_NG;
  2123. }
  2124. switch (lsah->type)
  2125. {
  2126. case OSPF_ROUTER_LSA:
  2127. /* RFC2328 A.4.2, LSA header + 4 bytes followed by N>=1 (12+)-byte link blocks */
  2128. if (headeronly)
  2129. {
  2130. ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_ROUTER_LSA_MIN_SIZE) % 4 ? MSG_NG : MSG_OK;
  2131. break;
  2132. }
  2133. rlsa = (struct router_lsa *) lsah;
  2134. ret = ospf_router_lsa_links_examin
  2135. (
  2136. (struct router_lsa_link *) rlsa->link,
  2137. lsalen - OSPF_LSA_HEADER_SIZE - 4, /* skip: basic header, "flags", 0, "# links" */
  2138. ntohs (rlsa->links) /* 16 bits */
  2139. );
  2140. break;
  2141. case OSPF_AS_EXTERNAL_LSA:
  2142. /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long blocks */
  2143. case OSPF_AS_NSSA_LSA:
  2144. /* RFC3101 C, idem */
  2145. ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_AS_EXTERNAL_LSA_MIN_SIZE) % 12 ? MSG_NG : MSG_OK;
  2146. break;
  2147. /* Following LSA types are considered OK length-wise as soon as their minimum
  2148. * length constraint is met and length of the whole LSA is a multiple of 4
  2149. * (basic LSA header size is already a multiple of 4). */
  2150. case OSPF_NETWORK_LSA:
  2151. /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */
  2152. case OSPF_SUMMARY_LSA:
  2153. case OSPF_ASBR_SUMMARY_LSA:
  2154. /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS blocks */
  2155. #ifdef HAVE_OPAQUE_LSA
  2156. case OSPF_OPAQUE_LINK_LSA:
  2157. case OSPF_OPAQUE_AREA_LSA:
  2158. case OSPF_OPAQUE_AS_LSA:
  2159. /* RFC5250 A.2, "some number of octets (of application-specific
  2160. * data) padded to 32-bit alignment." This is considered equivalent
  2161. * to 4-byte alignment of all other LSA types, see OSPF-ALIGNMENT.txt
  2162. * file for the detailed analysis of this passage. */
  2163. #endif
  2164. ret = lsalen % 4 ? MSG_NG : MSG_OK;
  2165. break;
  2166. default:
  2167. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2168. zlog_debug ("%s: unsupported LSA type 0x%02x", __func__, lsah->type);
  2169. return MSG_NG;
  2170. }
  2171. if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV))
  2172. zlog_debug ("%s: alignment error in %s",
  2173. __func__, LOOKUP (ospf_lsa_type_msg, lsah->type));
  2174. return ret;
  2175. }
  2176. /* Verify if the provided input buffer is a valid sequence of LSAs. This
  2177. includes verification of LSA blocks length/alignment and dispatching
  2178. of deeper-level checks. */
  2179. static unsigned
  2180. ospf_lsaseq_examin
  2181. (
  2182. struct lsa_header *lsah, /* start of buffered data */
  2183. size_t length,
  2184. const u_char headeronly,
  2185. /* When declared_num_lsas is not 0, compare it to the real number of LSAs
  2186. and treat the difference as an error. */
  2187. const u_int32_t declared_num_lsas
  2188. )
  2189. {
  2190. u_int32_t counted_lsas = 0;
  2191. while (length)
  2192. {
  2193. u_int16_t lsalen;
  2194. if (length < OSPF_LSA_HEADER_SIZE)
  2195. {
  2196. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2197. zlog_debug ("%s: undersized (%zu B) trailing (#%u) LSA header",
  2198. __func__, length, counted_lsas);
  2199. return MSG_NG;
  2200. }
  2201. /* save on ntohs() calls here and in the LSA validator */
  2202. lsalen = ntohs (lsah->length);
  2203. if (lsalen < OSPF_LSA_HEADER_SIZE)
  2204. {
  2205. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2206. zlog_debug ("%s: malformed LSA header #%u, declared length is %u B",
  2207. __func__, counted_lsas, lsalen);
  2208. return MSG_NG;
  2209. }
  2210. if (headeronly)
  2211. {
  2212. /* less checks here and in ospf_lsa_examin() */
  2213. if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 1))
  2214. {
  2215. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2216. zlog_debug ("%s: malformed header-only LSA #%u", __func__, counted_lsas);
  2217. return MSG_NG;
  2218. }
  2219. lsah = (struct lsa_header *) ((caddr_t) lsah + OSPF_LSA_HEADER_SIZE);
  2220. length -= OSPF_LSA_HEADER_SIZE;
  2221. }
  2222. else
  2223. {
  2224. /* make sure the input buffer is deep enough before further checks */
  2225. if (lsalen > length)
  2226. {
  2227. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2228. zlog_debug ("%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B",
  2229. __func__, counted_lsas, lsalen, length);
  2230. return MSG_NG;
  2231. }
  2232. if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 0))
  2233. {
  2234. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2235. zlog_debug ("%s: malformed LSA #%u", __func__, counted_lsas);
  2236. return MSG_NG;
  2237. }
  2238. lsah = (struct lsa_header *) ((caddr_t) lsah + lsalen);
  2239. length -= lsalen;
  2240. }
  2241. counted_lsas++;
  2242. }
  2243. if (declared_num_lsas && counted_lsas != declared_num_lsas)
  2244. {
  2245. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2246. zlog_debug ("%s: #LSAs declared (%u) does not match actual (%u)",
  2247. __func__, declared_num_lsas, counted_lsas);
  2248. return MSG_NG;
  2249. }
  2250. return MSG_OK;
  2251. }
  2252. /* Verify a complete OSPF packet for proper sizing/alignment. */
  2253. static unsigned
  2254. ospf_packet_examin (struct ospf_header * oh, const unsigned bytesonwire)
  2255. {
  2256. u_int16_t bytesdeclared, bytesauth;
  2257. unsigned ret;
  2258. struct ospf_ls_update * lsupd;
  2259. /* Length, 1st approximation. */
  2260. if (bytesonwire < OSPF_HEADER_SIZE)
  2261. {
  2262. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2263. zlog_debug ("%s: undersized (%u B) packet", __func__, bytesonwire);
  2264. return MSG_NG;
  2265. }
  2266. /* Now it is safe to access header fields. Performing length check, allow
  2267. * for possible extra bytes of crypto auth/padding, which are not counted
  2268. * in the OSPF header "length" field. */
  2269. if (oh->version != OSPF_VERSION)
  2270. {
  2271. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2272. zlog_debug ("%s: invalid (%u) protocol version", __func__, oh->version);
  2273. return MSG_NG;
  2274. }
  2275. bytesdeclared = ntohs (oh->length);
  2276. if (ntohs (oh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
  2277. bytesauth = 0;
  2278. else
  2279. {
  2280. if (oh->u.crypt.auth_data_len != OSPF_AUTH_MD5_SIZE)
  2281. {
  2282. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2283. zlog_debug ("%s: unsupported crypto auth length (%u B)",
  2284. __func__, oh->u.crypt.auth_data_len);
  2285. return MSG_NG;
  2286. }
  2287. bytesauth = OSPF_AUTH_MD5_SIZE;
  2288. }
  2289. if (bytesdeclared + bytesauth > bytesonwire)
  2290. {
  2291. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2292. zlog_debug ("%s: packet length error (%u real, %u+%u declared)",
  2293. __func__, bytesonwire, bytesdeclared, bytesauth);
  2294. return MSG_NG;
  2295. }
  2296. /* Length, 2nd approximation. The type-specific constraint is checked
  2297. against declared length, not amount of bytes on wire. */
  2298. if
  2299. (
  2300. oh->type >= OSPF_MSG_HELLO &&
  2301. oh->type <= OSPF_MSG_LS_ACK &&
  2302. bytesdeclared < OSPF_HEADER_SIZE + ospf_packet_minlen[oh->type]
  2303. )
  2304. {
  2305. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2306. zlog_debug ("%s: undersized (%u B) %s packet", __func__,
  2307. bytesdeclared, LOOKUP (ospf_packet_type_str, oh->type));
  2308. return MSG_NG;
  2309. }
  2310. switch (oh->type)
  2311. {
  2312. case OSPF_MSG_HELLO:
  2313. /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes followed
  2314. by N>=0 router-IDs. */
  2315. ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_HELLO_MIN_SIZE) % 4 ? MSG_NG : MSG_OK;
  2316. break;
  2317. case OSPF_MSG_DB_DESC:
  2318. /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes followed
  2319. by N>=0 header-only LSAs. */
  2320. ret = ospf_lsaseq_examin
  2321. (
  2322. (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_DB_DESC_MIN_SIZE),
  2323. bytesdeclared - OSPF_HEADER_SIZE - OSPF_DB_DESC_MIN_SIZE,
  2324. 1, /* header-only LSAs */
  2325. 0
  2326. );
  2327. break;
  2328. case OSPF_MSG_LS_REQ:
  2329. /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes request blocks. */
  2330. ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_REQ_MIN_SIZE) %
  2331. OSPF_LSA_KEY_SIZE ? MSG_NG : MSG_OK;
  2332. break;
  2333. case OSPF_MSG_LS_UPD:
  2334. /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes followed
  2335. by N>=0 full LSAs (with N declared beforehand). */
  2336. lsupd = (struct ospf_ls_update *) ((caddr_t) oh + OSPF_HEADER_SIZE);
  2337. ret = ospf_lsaseq_examin
  2338. (
  2339. (struct lsa_header *) ((caddr_t) lsupd + OSPF_LS_UPD_MIN_SIZE),
  2340. bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_UPD_MIN_SIZE,
  2341. 0, /* full LSAs */
  2342. ntohl (lsupd->num_lsas) /* 32 bits */
  2343. );
  2344. break;
  2345. case OSPF_MSG_LS_ACK:
  2346. /* RFC2328 A.3.6, packet header followed by N>=0 header-only LSAs. */
  2347. ret = ospf_lsaseq_examin
  2348. (
  2349. (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_LS_ACK_MIN_SIZE),
  2350. bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_ACK_MIN_SIZE,
  2351. 1, /* header-only LSAs */
  2352. 0
  2353. );
  2354. break;
  2355. default:
  2356. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2357. zlog_debug ("%s: invalid packet type 0x%02x", __func__, oh->type);
  2358. return MSG_NG;
  2359. }
  2360. if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV))
  2361. zlog_debug ("%s: malformed %s packet", __func__, LOOKUP (ospf_packet_type_str, oh->type));
  2362. return ret;
  2363. }
  2364. /* OSPF Header verification. */
  2365. static int
  2366. ospf_verify_header (struct stream *ibuf, struct ospf_interface *oi,
  2367. struct ip *iph, struct ospf_header *ospfh)
  2368. {
  2369. /* Check Area ID. */
  2370. if (!ospf_check_area_id (oi, ospfh))
  2371. {
  2372. zlog_warn ("interface %s: ospf_read invalid Area ID %s.",
  2373. IF_NAME (oi), inet_ntoa (ospfh->area_id));
  2374. return -1;
  2375. }
  2376. /* Check network mask, Silently discarded. */
  2377. if (! ospf_check_network_mask (oi, iph->ip_src))
  2378. {
  2379. zlog_warn ("interface %s: ospf_read network address is not same [%s]",
  2380. IF_NAME (oi), inet_ntoa (iph->ip_src));
  2381. return -1;
  2382. }
  2383. /* Check authentication. The function handles logging actions, where required. */
  2384. if (! ospf_check_auth (oi, ospfh))
  2385. return -1;
  2386. return 0;
  2387. }
  2388. /* Starting point of packet process function. */
  2389. int
  2390. ospf_read (struct thread *thread)
  2391. {
  2392. int ret;
  2393. struct stream *ibuf;
  2394. struct ospf *ospf;
  2395. struct ospf_interface *oi;
  2396. struct ip *iph;
  2397. struct ospf_header *ospfh;
  2398. u_int16_t length;
  2399. struct interface *ifp;
  2400. /* first of all get interface pointer. */
  2401. ospf = THREAD_ARG (thread);
  2402. /* prepare for next packet. */
  2403. ospf->t_read = thread_add_read (master, ospf_read, ospf, ospf->fd);
  2404. stream_reset(ospf->ibuf);
  2405. if (!(ibuf = ospf_recv_packet (ospf->fd, &ifp, ospf->ibuf)))
  2406. return -1;
  2407. /* This raw packet is known to be at least as big as its IP header. */
  2408. /* Note that there should not be alignment problems with this assignment
  2409. because this is at the beginning of the stream data buffer. */
  2410. iph = (struct ip *) STREAM_DATA (ibuf);
  2411. /* Note that sockopt_iphdrincl_swab_systoh was called in ospf_recv_packet. */
  2412. if (ifp == NULL)
  2413. /* Handle cases where the platform does not support retrieving the ifindex,
  2414. and also platforms (such as Solaris 8) that claim to support ifindex
  2415. retrieval but do not. */
  2416. ifp = if_lookup_address (iph->ip_src);
  2417. if (ifp == NULL)
  2418. return 0;
  2419. /* IP Header dump. */
  2420. if (IS_DEBUG_OSPF_PACKET(0, RECV))
  2421. ospf_ip_header_dump (iph);
  2422. /* Self-originated packet should be discarded silently. */
  2423. if (ospf_if_lookup_by_local_addr (ospf, NULL, iph->ip_src))
  2424. {
  2425. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2426. {
  2427. zlog_debug ("ospf_read[%s]: Dropping self-originated packet",
  2428. inet_ntoa (iph->ip_src));
  2429. }
  2430. return 0;
  2431. }
  2432. /* Advance from IP header to OSPF header (iph->ip_hl has been verified
  2433. by ospf_recv_packet() to be correct). */
  2434. stream_forward_getp (ibuf, iph->ip_hl * 4);
  2435. ospfh = (struct ospf_header *) STREAM_PNT (ibuf);
  2436. if (MSG_OK != ospf_packet_examin (ospfh, stream_get_endp (ibuf) - stream_get_getp (ibuf)))
  2437. return -1;
  2438. /* Now it is safe to access all fields of OSPF packet header. */
  2439. /* associate packet with ospf interface */
  2440. oi = ospf_if_lookup_recv_if (ospf, iph->ip_src, ifp);
  2441. /* ospf_verify_header() relies on a valid "oi" and thus can be called only
  2442. after the passive/backbone/other checks below are passed. These checks
  2443. in turn access the fields of unverified "ospfh" structure for their own
  2444. purposes and must remain very accurate in doing this. */
  2445. /* If incoming interface is passive one, ignore it. */
  2446. if (oi && OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
  2447. {
  2448. char buf[3][INET_ADDRSTRLEN];
  2449. if (IS_DEBUG_OSPF_EVENT)
  2450. zlog_debug ("ignoring packet from router %s sent to %s, "
  2451. "received on a passive interface, %s",
  2452. inet_ntop(AF_INET, &ospfh->router_id, buf[0], sizeof(buf[0])),
  2453. inet_ntop(AF_INET, &iph->ip_dst, buf[1], sizeof(buf[1])),
  2454. inet_ntop(AF_INET, &oi->address->u.prefix4,
  2455. buf[2], sizeof(buf[2])));
  2456. if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS))
  2457. {
  2458. /* Try to fix multicast membership.
  2459. * Some OS:es may have problems in this area,
  2460. * make sure it is removed.
  2461. */
  2462. OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
  2463. ospf_if_set_multicast(oi);
  2464. }
  2465. return 0;
  2466. }
  2467. /* if no local ospf_interface,
  2468. * or header area is backbone but ospf_interface is not
  2469. * check for VLINK interface
  2470. */
  2471. if ( (oi == NULL) ||
  2472. (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id)
  2473. && !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id))
  2474. )
  2475. {
  2476. if ((oi = ospf_associate_packet_vl (ospf, ifp, iph, ospfh)) == NULL)
  2477. {
  2478. if (IS_DEBUG_OSPF_EVENT)
  2479. zlog_debug ("Packet from [%s] received on link %s"
  2480. " but no ospf_interface",
  2481. inet_ntoa (iph->ip_src), ifp->name);
  2482. return 0;
  2483. }
  2484. }
  2485. /* else it must be a local ospf interface, check it was received on
  2486. * correct link
  2487. */
  2488. else if (oi->ifp != ifp)
  2489. {
  2490. if (IS_DEBUG_OSPF_EVENT)
  2491. zlog_warn ("Packet from [%s] received on wrong link %s",
  2492. inet_ntoa (iph->ip_src), ifp->name);
  2493. return 0;
  2494. }
  2495. else if (oi->state == ISM_Down)
  2496. {
  2497. char buf[2][INET_ADDRSTRLEN];
  2498. zlog_warn ("Ignoring packet from %s to %s received on interface that is "
  2499. "down [%s]; interface flags are %s",
  2500. inet_ntop(AF_INET, &iph->ip_src, buf[0], sizeof(buf[0])),
  2501. inet_ntop(AF_INET, &iph->ip_dst, buf[1], sizeof(buf[1])),
  2502. ifp->name, if_flag_dump(ifp->flags));
  2503. /* Fix multicast memberships? */
  2504. if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS))
  2505. OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
  2506. else if (iph->ip_dst.s_addr == htonl(OSPF_ALLDROUTERS))
  2507. OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
  2508. if (oi->multicast_memberships)
  2509. ospf_if_set_multicast(oi);
  2510. return 0;
  2511. }
  2512. /*
  2513. * If the received packet is destined for AllDRouters, the packet
  2514. * should be accepted only if the received ospf interface state is
  2515. * either DR or Backup -- endo.
  2516. */
  2517. if (iph->ip_dst.s_addr == htonl (OSPF_ALLDROUTERS)
  2518. && (oi->state != ISM_DR && oi->state != ISM_Backup))
  2519. {
  2520. zlog_warn ("Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
  2521. inet_ntoa (iph->ip_src), IF_NAME (oi),
  2522. LOOKUP (ospf_ism_state_msg, oi->state));
  2523. /* Try to fix multicast membership. */
  2524. SET_FLAG(oi->multicast_memberships, MEMBER_DROUTERS);
  2525. ospf_if_set_multicast(oi);
  2526. return 0;
  2527. }
  2528. /* Verify more OSPF header fields. */
  2529. ret = ospf_verify_header (ibuf, oi, iph, ospfh);
  2530. if (ret < 0)
  2531. {
  2532. if (IS_DEBUG_OSPF_PACKET (0, RECV))
  2533. zlog_debug ("ospf_read[%s]: Header check failed, "
  2534. "dropping.",
  2535. inet_ntoa (iph->ip_src));
  2536. return ret;
  2537. }
  2538. /* Show debug receiving packet. */
  2539. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  2540. {
  2541. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
  2542. {
  2543. zlog_debug ("-----------------------------------------------------");
  2544. ospf_packet_dump (ibuf);
  2545. }
  2546. zlog_debug ("%s received from [%s] via [%s]",
  2547. LOOKUP (ospf_packet_type_str, ospfh->type),
  2548. inet_ntoa (ospfh->router_id), IF_NAME (oi));
  2549. zlog_debug (" src [%s],", inet_ntoa (iph->ip_src));
  2550. zlog_debug (" dst [%s]", inet_ntoa (iph->ip_dst));
  2551. if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
  2552. zlog_debug ("-----------------------------------------------------");
  2553. }
  2554. stream_forward_getp (ibuf, OSPF_HEADER_SIZE);
  2555. /* Adjust size to message length. */
  2556. length = ntohs (ospfh->length) - OSPF_HEADER_SIZE;
  2557. /* Read rest of the packet and call each sort of packet routine. */
  2558. switch (ospfh->type)
  2559. {
  2560. case OSPF_MSG_HELLO:
  2561. ospf_hello (iph, ospfh, ibuf, oi, length);
  2562. break;
  2563. case OSPF_MSG_DB_DESC:
  2564. ospf_db_desc (iph, ospfh, ibuf, oi, length);
  2565. break;
  2566. case OSPF_MSG_LS_REQ:
  2567. ospf_ls_req (iph, ospfh, ibuf, oi, length);
  2568. break;
  2569. case OSPF_MSG_LS_UPD:
  2570. ospf_ls_upd (iph, ospfh, ibuf, oi, length);
  2571. break;
  2572. case OSPF_MSG_LS_ACK:
  2573. ospf_ls_ack (iph, ospfh, ibuf, oi, length);
  2574. break;
  2575. default:
  2576. zlog (NULL, LOG_WARNING,
  2577. "interface %s: OSPF packet header type %d is illegal",
  2578. IF_NAME (oi), ospfh->type);
  2579. break;
  2580. }
  2581. return 0;
  2582. }
  2583. /* Make OSPF header. */
  2584. static void
  2585. ospf_make_header (int type, struct ospf_interface *oi, struct stream *s)
  2586. {
  2587. struct ospf_header *ospfh;
  2588. ospfh = (struct ospf_header *) STREAM_DATA (s);
  2589. ospfh->version = (u_char) OSPF_VERSION;
  2590. ospfh->type = (u_char) type;
  2591. ospfh->router_id = oi->ospf->router_id;
  2592. ospfh->checksum = 0;
  2593. ospfh->area_id = oi->area->area_id;
  2594. ospfh->auth_type = htons (ospf_auth_type (oi));
  2595. memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
  2596. stream_forward_endp (s, OSPF_HEADER_SIZE);
  2597. }
  2598. /* Make Authentication Data. */
  2599. static int
  2600. ospf_make_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
  2601. {
  2602. struct crypt_key *ck;
  2603. switch (ospf_auth_type (oi))
  2604. {
  2605. case OSPF_AUTH_NULL:
  2606. /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
  2607. break;
  2608. case OSPF_AUTH_SIMPLE:
  2609. memcpy (ospfh->u.auth_data, OSPF_IF_PARAM (oi, auth_simple),
  2610. OSPF_AUTH_SIMPLE_SIZE);
  2611. break;
  2612. case OSPF_AUTH_CRYPTOGRAPHIC:
  2613. /* If key is not set, then set 0. */
  2614. if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
  2615. {
  2616. ospfh->u.crypt.zero = 0;
  2617. ospfh->u.crypt.key_id = 0;
  2618. ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
  2619. }
  2620. else
  2621. {
  2622. ck = listgetdata (listtail(OSPF_IF_PARAM (oi, auth_crypt)));
  2623. ospfh->u.crypt.zero = 0;
  2624. ospfh->u.crypt.key_id = ck->key_id;
  2625. ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
  2626. }
  2627. /* note: the seq is done in ospf_make_md5_digest() */
  2628. break;
  2629. default:
  2630. /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
  2631. break;
  2632. }
  2633. return 0;
  2634. }
  2635. /* Fill rest of OSPF header. */
  2636. static void
  2637. ospf_fill_header (struct ospf_interface *oi,
  2638. struct stream *s, u_int16_t length)
  2639. {
  2640. struct ospf_header *ospfh;
  2641. ospfh = (struct ospf_header *) STREAM_DATA (s);
  2642. /* Fill length. */
  2643. ospfh->length = htons (length);
  2644. /* Calculate checksum. */
  2645. if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
  2646. ospfh->checksum = in_cksum (ospfh, length);
  2647. else
  2648. ospfh->checksum = 0;
  2649. /* Add Authentication Data. */
  2650. ospf_make_auth (oi, ospfh);
  2651. }
  2652. static int
  2653. ospf_make_hello (struct ospf_interface *oi, struct stream *s)
  2654. {
  2655. struct ospf_neighbor *nbr;
  2656. struct route_node *rn;
  2657. u_int16_t length = OSPF_HELLO_MIN_SIZE;
  2658. struct in_addr mask;
  2659. unsigned long p;
  2660. int flag = 0;
  2661. /* Set netmask of interface. */
  2662. if (oi->type != OSPF_IFTYPE_POINTOPOINT &&
  2663. oi->type != OSPF_IFTYPE_VIRTUALLINK)
  2664. masklen2ip (oi->address->prefixlen, &mask);
  2665. else
  2666. memset ((char *) &mask, 0, sizeof (struct in_addr));
  2667. stream_put_ipv4 (s, mask.s_addr);
  2668. /* Set Hello Interval. */
  2669. if (OSPF_IF_PARAM (oi, fast_hello) == 0)
  2670. stream_putw (s, OSPF_IF_PARAM (oi, v_hello));
  2671. else
  2672. stream_putw (s, 0); /* hello-interval of 0 for fast-hellos */
  2673. if (IS_DEBUG_OSPF_EVENT)
  2674. zlog_debug ("make_hello: options: %x, int: %s",
  2675. OPTIONS(oi), IF_NAME (oi));
  2676. /* Set Options. */
  2677. stream_putc (s, OPTIONS (oi));
  2678. /* Set Router Priority. */
  2679. stream_putc (s, PRIORITY (oi));
  2680. /* Set Router Dead Interval. */
  2681. stream_putl (s, OSPF_IF_PARAM (oi, v_wait));
  2682. /* Set Designated Router. */
  2683. stream_put_ipv4 (s, DR (oi).s_addr);
  2684. p = stream_get_endp (s);
  2685. /* Set Backup Designated Router. */
  2686. stream_put_ipv4 (s, BDR (oi).s_addr);
  2687. /* Add neighbor seen. */
  2688. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  2689. if ((nbr = rn->info))
  2690. if (nbr->router_id.s_addr != 0) /* Ignore 0.0.0.0 node. */
  2691. if (nbr->state != NSM_Attempt) /* Ignore Down neighbor. */
  2692. if (nbr->state != NSM_Down) /* This is myself for DR election. */
  2693. if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  2694. {
  2695. /* Check neighbor is sane? */
  2696. if (nbr->d_router.s_addr != 0
  2697. && IPV4_ADDR_SAME (&nbr->d_router, &oi->address->u.prefix4)
  2698. && IPV4_ADDR_SAME (&nbr->bd_router, &oi->address->u.prefix4))
  2699. flag = 1;
  2700. stream_put_ipv4 (s, nbr->router_id.s_addr);
  2701. length += 4;
  2702. }
  2703. /* Let neighbor generate BackupSeen. */
  2704. if (flag == 1)
  2705. stream_putl_at (s, p, 0); /* ipv4 address, normally */
  2706. return length;
  2707. }
  2708. static int
  2709. ospf_make_db_desc (struct ospf_interface *oi, struct ospf_neighbor *nbr,
  2710. struct stream *s)
  2711. {
  2712. struct ospf_lsa *lsa;
  2713. u_int16_t length = OSPF_DB_DESC_MIN_SIZE;
  2714. u_char options;
  2715. unsigned long pp;
  2716. int i;
  2717. struct ospf_lsdb *lsdb;
  2718. /* Set Interface MTU. */
  2719. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  2720. stream_putw (s, 0);
  2721. else
  2722. stream_putw (s, oi->ifp->mtu);
  2723. /* Set Options. */
  2724. options = OPTIONS (oi);
  2725. #ifdef HAVE_OPAQUE_LSA
  2726. if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  2727. SET_FLAG (options, OSPF_OPTION_O);
  2728. #endif /* HAVE_OPAQUE_LSA */
  2729. stream_putc (s, options);
  2730. /* DD flags */
  2731. pp = stream_get_endp (s);
  2732. stream_putc (s, nbr->dd_flags);
  2733. /* Set DD Sequence Number. */
  2734. stream_putl (s, nbr->dd_seqnum);
  2735. /* shortcut unneeded walk of (empty) summary LSDBs */
  2736. if (ospf_db_summary_isempty (nbr))
  2737. goto empty;
  2738. /* Describe LSA Header from Database Summary List. */
  2739. lsdb = &nbr->db_sum;
  2740. for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
  2741. {
  2742. struct route_table *table = lsdb->type[i].db;
  2743. struct route_node *rn;
  2744. for (rn = route_top (table); rn; rn = route_next (rn))
  2745. if ((lsa = rn->info) != NULL)
  2746. {
  2747. #ifdef HAVE_OPAQUE_LSA
  2748. if (IS_OPAQUE_LSA (lsa->data->type)
  2749. && (! CHECK_FLAG (options, OSPF_OPTION_O)))
  2750. {
  2751. /* Suppress advertising opaque-informations. */
  2752. /* Remove LSA from DB summary list. */
  2753. ospf_lsdb_delete (lsdb, lsa);
  2754. continue;
  2755. }
  2756. #endif /* HAVE_OPAQUE_LSA */
  2757. if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
  2758. {
  2759. struct lsa_header *lsah;
  2760. u_int16_t ls_age;
  2761. /* DD packet overflows interface MTU. */
  2762. if (length + OSPF_LSA_HEADER_SIZE > ospf_packet_max (oi))
  2763. break;
  2764. /* Keep pointer to LS age. */
  2765. lsah = (struct lsa_header *) (STREAM_DATA (s) +
  2766. stream_get_endp (s));
  2767. /* Proceed stream pointer. */
  2768. stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
  2769. length += OSPF_LSA_HEADER_SIZE;
  2770. /* Set LS age. */
  2771. ls_age = LS_AGE (lsa);
  2772. lsah->ls_age = htons (ls_age);
  2773. }
  2774. /* Remove LSA from DB summary list. */
  2775. ospf_lsdb_delete (lsdb, lsa);
  2776. }
  2777. }
  2778. /* Update 'More' bit */
  2779. if (ospf_db_summary_isempty (nbr))
  2780. {
  2781. empty:
  2782. if (nbr->state >= NSM_Exchange)
  2783. {
  2784. UNSET_FLAG (nbr->dd_flags, OSPF_DD_FLAG_M);
  2785. /* Rewrite DD flags */
  2786. stream_putc_at (s, pp, nbr->dd_flags);
  2787. }
  2788. else
  2789. {
  2790. assert (IS_SET_DD_M(nbr->dd_flags));
  2791. }
  2792. }
  2793. return length;
  2794. }
  2795. static int
  2796. ospf_make_ls_req_func (struct stream *s, u_int16_t *length,
  2797. unsigned long delta, struct ospf_neighbor *nbr,
  2798. struct ospf_lsa *lsa)
  2799. {
  2800. struct ospf_interface *oi;
  2801. oi = nbr->oi;
  2802. /* LS Request packet overflows interface MTU. */
  2803. if (*length + delta > ospf_packet_max(oi))
  2804. return 0;
  2805. stream_putl (s, lsa->data->type);
  2806. stream_put_ipv4 (s, lsa->data->id.s_addr);
  2807. stream_put_ipv4 (s, lsa->data->adv_router.s_addr);
  2808. ospf_lsa_unlock (&nbr->ls_req_last);
  2809. nbr->ls_req_last = ospf_lsa_lock (lsa);
  2810. *length += 12;
  2811. return 1;
  2812. }
  2813. static int
  2814. ospf_make_ls_req (struct ospf_neighbor *nbr, struct stream *s)
  2815. {
  2816. struct ospf_lsa *lsa;
  2817. u_int16_t length = OSPF_LS_REQ_MIN_SIZE;
  2818. unsigned long delta = stream_get_endp(s)+12;
  2819. struct route_table *table;
  2820. struct route_node *rn;
  2821. int i;
  2822. struct ospf_lsdb *lsdb;
  2823. lsdb = &nbr->ls_req;
  2824. for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
  2825. {
  2826. table = lsdb->type[i].db;
  2827. for (rn = route_top (table); rn; rn = route_next (rn))
  2828. if ((lsa = (rn->info)) != NULL)
  2829. if (ospf_make_ls_req_func (s, &length, delta, nbr, lsa) == 0)
  2830. {
  2831. route_unlock_node (rn);
  2832. break;
  2833. }
  2834. }
  2835. return length;
  2836. }
  2837. static int
  2838. ls_age_increment (struct ospf_lsa *lsa, int delay)
  2839. {
  2840. int age;
  2841. age = IS_LSA_MAXAGE (lsa) ? OSPF_LSA_MAXAGE : LS_AGE (lsa) + delay;
  2842. return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age);
  2843. }
  2844. static int
  2845. ospf_make_ls_upd (struct ospf_interface *oi, struct list *update, struct stream *s)
  2846. {
  2847. struct ospf_lsa *lsa;
  2848. struct listnode *node;
  2849. u_int16_t length = 0;
  2850. unsigned int size_noauth;
  2851. unsigned long delta = stream_get_endp (s);
  2852. unsigned long pp;
  2853. int count = 0;
  2854. if (IS_DEBUG_OSPF_EVENT)
  2855. zlog_debug ("ospf_make_ls_upd: Start");
  2856. pp = stream_get_endp (s);
  2857. stream_forward_endp (s, OSPF_LS_UPD_MIN_SIZE);
  2858. length += OSPF_LS_UPD_MIN_SIZE;
  2859. /* Calculate amount of packet usable for data. */
  2860. size_noauth = stream_get_size(s) - ospf_packet_authspace(oi);
  2861. while ((node = listhead (update)) != NULL)
  2862. {
  2863. struct lsa_header *lsah;
  2864. u_int16_t ls_age;
  2865. if (IS_DEBUG_OSPF_EVENT)
  2866. zlog_debug ("ospf_make_ls_upd: List Iteration %d", count);
  2867. lsa = listgetdata (node);
  2868. assert (lsa->data);
  2869. /* Will it fit? */
  2870. if (length + delta + ntohs (lsa->data->length) > size_noauth)
  2871. break;
  2872. /* Keep pointer to LS age. */
  2873. lsah = (struct lsa_header *) (STREAM_DATA (s) + stream_get_endp (s));
  2874. /* Put LSA to Link State Request. */
  2875. stream_put (s, lsa->data, ntohs (lsa->data->length));
  2876. /* Set LS age. */
  2877. /* each hop must increment an lsa_age by transmit_delay
  2878. of OSPF interface */
  2879. ls_age = ls_age_increment (lsa, OSPF_IF_PARAM (oi, transmit_delay));
  2880. lsah->ls_age = htons (ls_age);
  2881. length += ntohs (lsa->data->length);
  2882. count++;
  2883. list_delete_node (update, node);
  2884. ospf_lsa_unlock (&lsa); /* oi->ls_upd_queue */
  2885. }
  2886. /* Now set #LSAs. */
  2887. stream_putl_at (s, pp, count);
  2888. if (IS_DEBUG_OSPF_EVENT)
  2889. zlog_debug ("ospf_make_ls_upd: Stop");
  2890. return length;
  2891. }
  2892. static int
  2893. ospf_make_ls_ack (struct ospf_interface *oi, struct list *ack, struct stream *s)
  2894. {
  2895. struct listnode *node, *nnode;
  2896. u_int16_t length = OSPF_LS_ACK_MIN_SIZE;
  2897. unsigned long delta = stream_get_endp(s) + 24;
  2898. struct ospf_lsa *lsa;
  2899. for (ALL_LIST_ELEMENTS (ack, node, nnode, lsa))
  2900. {
  2901. assert (lsa);
  2902. if (length + delta > ospf_packet_max (oi))
  2903. break;
  2904. stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
  2905. length += OSPF_LSA_HEADER_SIZE;
  2906. listnode_delete (ack, lsa);
  2907. ospf_lsa_unlock (&lsa); /* oi->ls_ack_direct.ls_ack */
  2908. }
  2909. return length;
  2910. }
  2911. static void
  2912. ospf_hello_send_sub (struct ospf_interface *oi, in_addr_t addr)
  2913. {
  2914. struct ospf_packet *op;
  2915. u_int16_t length = OSPF_HEADER_SIZE;
  2916. op = ospf_packet_new (oi->ifp->mtu);
  2917. /* Prepare OSPF common header. */
  2918. ospf_make_header (OSPF_MSG_HELLO, oi, op->s);
  2919. /* Prepare OSPF Hello body. */
  2920. length += ospf_make_hello (oi, op->s);
  2921. /* Fill OSPF header. */
  2922. ospf_fill_header (oi, op->s, length);
  2923. /* Set packet length. */
  2924. op->length = length;
  2925. op->dst.s_addr = addr;
  2926. /* Add packet to the top of the interface output queue, so that they
  2927. * can't get delayed by things like long queues of LS Update packets
  2928. */
  2929. ospf_packet_add_top (oi, op);
  2930. /* Hook thread to write packet. */
  2931. OSPF_ISM_WRITE_ON (oi->ospf);
  2932. }
  2933. static void
  2934. ospf_poll_send (struct ospf_nbr_nbma *nbr_nbma)
  2935. {
  2936. struct ospf_interface *oi;
  2937. oi = nbr_nbma->oi;
  2938. assert(oi);
  2939. /* If this is passive interface, do not send OSPF Hello. */
  2940. if (OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
  2941. return;
  2942. if (oi->type != OSPF_IFTYPE_NBMA)
  2943. return;
  2944. if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down)
  2945. return;
  2946. if (PRIORITY(oi) == 0)
  2947. return;
  2948. if (nbr_nbma->priority == 0
  2949. && oi->state != ISM_DR && oi->state != ISM_Backup)
  2950. return;
  2951. ospf_hello_send_sub (oi, nbr_nbma->addr.s_addr);
  2952. }
  2953. int
  2954. ospf_poll_timer (struct thread *thread)
  2955. {
  2956. struct ospf_nbr_nbma *nbr_nbma;
  2957. nbr_nbma = THREAD_ARG (thread);
  2958. nbr_nbma->t_poll = NULL;
  2959. if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
  2960. zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (Poll timer expire)",
  2961. IF_NAME (nbr_nbma->oi), inet_ntoa (nbr_nbma->addr));
  2962. ospf_poll_send (nbr_nbma);
  2963. if (nbr_nbma->v_poll > 0)
  2964. OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
  2965. nbr_nbma->v_poll);
  2966. return 0;
  2967. }
  2968. int
  2969. ospf_hello_reply_timer (struct thread *thread)
  2970. {
  2971. struct ospf_neighbor *nbr;
  2972. nbr = THREAD_ARG (thread);
  2973. nbr->t_hello_reply = NULL;
  2974. assert (nbr->oi);
  2975. if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
  2976. zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (hello-reply timer expire)",
  2977. IF_NAME (nbr->oi), inet_ntoa (nbr->router_id));
  2978. ospf_hello_send_sub (nbr->oi, nbr->address.u.prefix4.s_addr);
  2979. return 0;
  2980. }
  2981. /* Send OSPF Hello. */
  2982. void
  2983. ospf_hello_send (struct ospf_interface *oi)
  2984. {
  2985. /* If this is passive interface, do not send OSPF Hello. */
  2986. if (OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
  2987. return;
  2988. if (oi->type == OSPF_IFTYPE_NBMA)
  2989. {
  2990. struct ospf_neighbor *nbr;
  2991. struct route_node *rn;
  2992. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  2993. if ((nbr = rn->info))
  2994. if (nbr != oi->nbr_self)
  2995. if (nbr->state != NSM_Down)
  2996. {
  2997. /* RFC 2328 Section 9.5.1
  2998. If the router is not eligible to become Designated Router,
  2999. it must periodically send Hello Packets to both the
  3000. Designated Router and the Backup Designated Router (if they
  3001. exist). */
  3002. if (PRIORITY(oi) == 0 &&
  3003. IPV4_ADDR_CMP(&DR(oi), &nbr->address.u.prefix4) &&
  3004. IPV4_ADDR_CMP(&BDR(oi), &nbr->address.u.prefix4))
  3005. continue;
  3006. /* If the router is eligible to become Designated Router, it
  3007. must periodically send Hello Packets to all neighbors that
  3008. are also eligible. In addition, if the router is itself the
  3009. Designated Router or Backup Designated Router, it must also
  3010. send periodic Hello Packets to all other neighbors. */
  3011. if (nbr->priority == 0 && oi->state == ISM_DROther)
  3012. continue;
  3013. /* if oi->state == Waiting, send hello to all neighbors */
  3014. ospf_hello_send_sub (oi, nbr->address.u.prefix4.s_addr);
  3015. }
  3016. }
  3017. else
  3018. {
  3019. /* Decide destination address. */
  3020. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  3021. ospf_hello_send_sub (oi, oi->vl_data->peer_addr.s_addr);
  3022. else
  3023. ospf_hello_send_sub (oi, htonl (OSPF_ALLSPFROUTERS));
  3024. }
  3025. }
  3026. /* Send OSPF Database Description. */
  3027. void
  3028. ospf_db_desc_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_DB_DESC, oi, op->s);
  3037. /* Prepare OSPF Database Description body. */
  3038. length += ospf_make_db_desc (oi, nbr, op->s);
  3039. /* Fill OSPF header. */
  3040. ospf_fill_header (oi, op->s, length);
  3041. /* Set packet length. */
  3042. op->length = length;
  3043. /* Decide destination address. */
  3044. if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3045. op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3046. else
  3047. op->dst = nbr->address.u.prefix4;
  3048. /* Add packet to the interface output queue. */
  3049. ospf_packet_add (oi, op);
  3050. /* Hook thread to write packet. */
  3051. OSPF_ISM_WRITE_ON (oi->ospf);
  3052. /* Remove old DD packet, then copy new one and keep in neighbor structure. */
  3053. if (nbr->last_send)
  3054. ospf_packet_free (nbr->last_send);
  3055. nbr->last_send = ospf_packet_dup (op);
  3056. quagga_gettime (QUAGGA_CLK_MONOTONIC, &nbr->last_send_ts);
  3057. }
  3058. /* Re-send Database Description. */
  3059. void
  3060. ospf_db_desc_resend (struct ospf_neighbor *nbr)
  3061. {
  3062. struct ospf_interface *oi;
  3063. oi = nbr->oi;
  3064. /* Add packet to the interface output queue. */
  3065. ospf_packet_add (oi, ospf_packet_dup (nbr->last_send));
  3066. /* Hook thread to write packet. */
  3067. OSPF_ISM_WRITE_ON (oi->ospf);
  3068. }
  3069. /* Send Link State Request. */
  3070. void
  3071. ospf_ls_req_send (struct ospf_neighbor *nbr)
  3072. {
  3073. struct ospf_interface *oi;
  3074. struct ospf_packet *op;
  3075. u_int16_t length = OSPF_HEADER_SIZE;
  3076. oi = nbr->oi;
  3077. op = ospf_packet_new (oi->ifp->mtu);
  3078. /* Prepare OSPF common header. */
  3079. ospf_make_header (OSPF_MSG_LS_REQ, oi, op->s);
  3080. /* Prepare OSPF Link State Request body. */
  3081. length += ospf_make_ls_req (nbr, op->s);
  3082. if (length == OSPF_HEADER_SIZE)
  3083. {
  3084. ospf_packet_free (op);
  3085. return;
  3086. }
  3087. /* Fill OSPF header. */
  3088. ospf_fill_header (oi, op->s, length);
  3089. /* Set packet length. */
  3090. op->length = length;
  3091. /* Decide destination address. */
  3092. if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3093. op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3094. else
  3095. op->dst = nbr->address.u.prefix4;
  3096. /* Add packet to the interface output queue. */
  3097. ospf_packet_add (oi, op);
  3098. /* Hook thread to write packet. */
  3099. OSPF_ISM_WRITE_ON (oi->ospf);
  3100. /* Add Link State Request Retransmission Timer. */
  3101. OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
  3102. }
  3103. /* Send Link State Update with an LSA. */
  3104. void
  3105. ospf_ls_upd_send_lsa (struct ospf_neighbor *nbr, struct ospf_lsa *lsa,
  3106. int flag)
  3107. {
  3108. struct list *update;
  3109. update = list_new ();
  3110. listnode_add (update, lsa);
  3111. ospf_ls_upd_send (nbr, update, flag);
  3112. list_delete (update);
  3113. }
  3114. /* Determine size for packet. Must be at least big enough to accomodate next
  3115. * LSA on list, which may be bigger than MTU size.
  3116. *
  3117. * Return pointer to new ospf_packet
  3118. * NULL if we can not allocate, eg because LSA is bigger than imposed limit
  3119. * on packet sizes (in which case offending LSA is deleted from update list)
  3120. */
  3121. static struct ospf_packet *
  3122. ospf_ls_upd_packet_new (struct list *update, struct ospf_interface *oi)
  3123. {
  3124. struct ospf_lsa *lsa;
  3125. struct listnode *ln;
  3126. size_t size;
  3127. static char warned = 0;
  3128. lsa = listgetdata((ln = listhead (update)));
  3129. assert (lsa->data);
  3130. if ((OSPF_LS_UPD_MIN_SIZE + ntohs (lsa->data->length))
  3131. > ospf_packet_max (oi))
  3132. {
  3133. if (!warned)
  3134. {
  3135. zlog_warn ("ospf_ls_upd_packet_new: oversized LSA encountered!"
  3136. "will need to fragment. Not optimal. Try divide up"
  3137. " your network with areas. Use 'debug ospf packet send'"
  3138. " to see details, or look at 'show ip ospf database ..'");
  3139. warned = 1;
  3140. }
  3141. if (IS_DEBUG_OSPF_PACKET (0, SEND))
  3142. zlog_debug ("ospf_ls_upd_packet_new: oversized LSA id:%s,"
  3143. " %d bytes originated by %s, will be fragmented!",
  3144. inet_ntoa (lsa->data->id),
  3145. ntohs (lsa->data->length),
  3146. inet_ntoa (lsa->data->adv_router));
  3147. /*
  3148. * Allocate just enough to fit this LSA only, to avoid including other
  3149. * LSAs in fragmented LSA Updates.
  3150. */
  3151. size = ntohs (lsa->data->length) + (oi->ifp->mtu - ospf_packet_max (oi))
  3152. + OSPF_LS_UPD_MIN_SIZE;
  3153. }
  3154. else
  3155. size = oi->ifp->mtu;
  3156. if (size > OSPF_MAX_PACKET_SIZE)
  3157. {
  3158. zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s too big,"
  3159. " %d bytes, packet size %ld, dropping it completely."
  3160. " OSPF routing is broken!",
  3161. inet_ntoa (lsa->data->id), ntohs (lsa->data->length),
  3162. (long int) size);
  3163. list_delete_node (update, ln);
  3164. return NULL;
  3165. }
  3166. /* IP header is built up separately by ospf_write(). This means, that we must
  3167. * reduce the "affordable" size just calculated by length of an IP header.
  3168. * This makes sure, that even if we manage to fill the payload with LSA data
  3169. * completely, the final packet (our data plus IP header) still fits into
  3170. * outgoing interface MTU. This correction isn't really meaningful for an
  3171. * oversized LSA, but for consistency the correction is done for both cases.
  3172. *
  3173. * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size
  3174. */
  3175. return ospf_packet_new (size - sizeof (struct ip));
  3176. }
  3177. static void
  3178. ospf_ls_upd_queue_send (struct ospf_interface *oi, struct list *update,
  3179. struct in_addr addr)
  3180. {
  3181. struct ospf_packet *op;
  3182. u_int16_t length = OSPF_HEADER_SIZE;
  3183. if (IS_DEBUG_OSPF_EVENT)
  3184. zlog_debug ("listcount = %d, [%s]dst %s", listcount (update), IF_NAME(oi),
  3185. inet_ntoa(addr));
  3186. op = ospf_ls_upd_packet_new (update, oi);
  3187. /* Prepare OSPF common header. */
  3188. ospf_make_header (OSPF_MSG_LS_UPD, oi, op->s);
  3189. /* Prepare OSPF Link State Update body.
  3190. * Includes Type-7 translation.
  3191. */
  3192. length += ospf_make_ls_upd (oi, update, op->s);
  3193. /* Fill OSPF header. */
  3194. ospf_fill_header (oi, op->s, length);
  3195. /* Set packet length. */
  3196. op->length = length;
  3197. /* Decide destination address. */
  3198. if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3199. op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3200. else
  3201. op->dst.s_addr = addr.s_addr;
  3202. /* Add packet to the interface output queue. */
  3203. ospf_packet_add (oi, op);
  3204. /* Hook thread to write packet. */
  3205. OSPF_ISM_WRITE_ON (oi->ospf);
  3206. }
  3207. static int
  3208. ospf_ls_upd_send_queue_event (struct thread *thread)
  3209. {
  3210. struct ospf_interface *oi = THREAD_ARG(thread);
  3211. struct route_node *rn;
  3212. struct route_node *rnext;
  3213. struct list *update;
  3214. char again = 0;
  3215. oi->t_ls_upd_event = NULL;
  3216. if (IS_DEBUG_OSPF_EVENT)
  3217. zlog_debug ("ospf_ls_upd_send_queue start");
  3218. for (rn = route_top (oi->ls_upd_queue); rn; rn = rnext)
  3219. {
  3220. rnext = route_next (rn);
  3221. if (rn->info == NULL)
  3222. continue;
  3223. update = (struct list *)rn->info;
  3224. ospf_ls_upd_queue_send (oi, update, rn->p.u.prefix4);
  3225. /* list might not be empty. */
  3226. if (listcount(update) == 0)
  3227. {
  3228. list_delete (rn->info);
  3229. rn->info = NULL;
  3230. route_unlock_node (rn);
  3231. }
  3232. else
  3233. again = 1;
  3234. }
  3235. if (again != 0)
  3236. {
  3237. if (IS_DEBUG_OSPF_EVENT)
  3238. zlog_debug ("ospf_ls_upd_send_queue: update lists not cleared,"
  3239. " %d nodes to try again, raising new event", again);
  3240. oi->t_ls_upd_event =
  3241. thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0);
  3242. }
  3243. if (IS_DEBUG_OSPF_EVENT)
  3244. zlog_debug ("ospf_ls_upd_send_queue stop");
  3245. return 0;
  3246. }
  3247. void
  3248. ospf_ls_upd_send (struct ospf_neighbor *nbr, struct list *update, int flag)
  3249. {
  3250. struct ospf_interface *oi;
  3251. struct ospf_lsa *lsa;
  3252. struct prefix_ipv4 p;
  3253. struct route_node *rn;
  3254. struct listnode *node;
  3255. oi = nbr->oi;
  3256. p.family = AF_INET;
  3257. p.prefixlen = IPV4_MAX_BITLEN;
  3258. /* Decide destination address. */
  3259. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  3260. p.prefix = oi->vl_data->peer_addr;
  3261. else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3262. p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3263. else if (flag == OSPF_SEND_PACKET_DIRECT)
  3264. p.prefix = nbr->address.u.prefix4;
  3265. else if (oi->state == ISM_DR || oi->state == ISM_Backup)
  3266. p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3267. else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
  3268. p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3269. else
  3270. p.prefix.s_addr = htonl (OSPF_ALLDROUTERS);
  3271. if (oi->type == OSPF_IFTYPE_NBMA)
  3272. {
  3273. if (flag == OSPF_SEND_PACKET_INDIRECT)
  3274. zlog_warn ("* LS-Update is directly sent on NBMA network.");
  3275. if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix.s_addr))
  3276. zlog_warn ("* LS-Update is sent to myself.");
  3277. }
  3278. rn = route_node_get (oi->ls_upd_queue, (struct prefix *) &p);
  3279. if (rn->info == NULL)
  3280. rn->info = list_new ();
  3281. for (ALL_LIST_ELEMENTS_RO (update, node, lsa))
  3282. listnode_add (rn->info, ospf_lsa_lock (lsa)); /* oi->ls_upd_queue */
  3283. if (oi->t_ls_upd_event == NULL)
  3284. oi->t_ls_upd_event =
  3285. thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0);
  3286. }
  3287. static void
  3288. ospf_ls_ack_send_list (struct ospf_interface *oi, struct list *ack,
  3289. struct in_addr dst)
  3290. {
  3291. struct ospf_packet *op;
  3292. u_int16_t length = OSPF_HEADER_SIZE;
  3293. op = ospf_packet_new (oi->ifp->mtu);
  3294. /* Prepare OSPF common header. */
  3295. ospf_make_header (OSPF_MSG_LS_ACK, oi, op->s);
  3296. /* Prepare OSPF Link State Acknowledgment body. */
  3297. length += ospf_make_ls_ack (oi, ack, op->s);
  3298. /* Fill OSPF header. */
  3299. ospf_fill_header (oi, op->s, length);
  3300. /* Set packet length. */
  3301. op->length = length;
  3302. /* Set destination IP address. */
  3303. op->dst = dst;
  3304. /* Add packet to the interface output queue. */
  3305. ospf_packet_add (oi, op);
  3306. /* Hook thread to write packet. */
  3307. OSPF_ISM_WRITE_ON (oi->ospf);
  3308. }
  3309. static int
  3310. ospf_ls_ack_send_event (struct thread *thread)
  3311. {
  3312. struct ospf_interface *oi = THREAD_ARG (thread);
  3313. oi->t_ls_ack_direct = NULL;
  3314. while (listcount (oi->ls_ack_direct.ls_ack))
  3315. ospf_ls_ack_send_list (oi, oi->ls_ack_direct.ls_ack,
  3316. oi->ls_ack_direct.dst);
  3317. return 0;
  3318. }
  3319. void
  3320. ospf_ls_ack_send (struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
  3321. {
  3322. struct ospf_interface *oi = nbr->oi;
  3323. if (listcount (oi->ls_ack_direct.ls_ack) == 0)
  3324. oi->ls_ack_direct.dst = nbr->address.u.prefix4;
  3325. listnode_add (oi->ls_ack_direct.ls_ack, ospf_lsa_lock (lsa));
  3326. if (oi->t_ls_ack_direct == NULL)
  3327. oi->t_ls_ack_direct =
  3328. thread_add_event (master, ospf_ls_ack_send_event, oi, 0);
  3329. }
  3330. /* Send Link State Acknowledgment delayed. */
  3331. void
  3332. ospf_ls_ack_send_delayed (struct ospf_interface *oi)
  3333. {
  3334. struct in_addr dst;
  3335. /* Decide destination address. */
  3336. /* RFC2328 Section 13.5 On non-broadcast
  3337. networks, delayed Link State Acknowledgment packets must be
  3338. unicast separately over each adjacency (i.e., neighbor whose
  3339. state is >= Exchange). */
  3340. if (oi->type == OSPF_IFTYPE_NBMA)
  3341. {
  3342. struct ospf_neighbor *nbr;
  3343. struct route_node *rn;
  3344. for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  3345. if ((nbr = rn->info) != NULL)
  3346. if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange)
  3347. while (listcount (oi->ls_ack))
  3348. ospf_ls_ack_send_list (oi, oi->ls_ack, nbr->address.u.prefix4);
  3349. return;
  3350. }
  3351. if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  3352. dst.s_addr = oi->vl_data->peer_addr.s_addr;
  3353. else if (oi->state == ISM_DR || oi->state == ISM_Backup)
  3354. dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3355. else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  3356. dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3357. else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
  3358. dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  3359. else
  3360. dst.s_addr = htonl (OSPF_ALLDROUTERS);
  3361. while (listcount (oi->ls_ack))
  3362. ospf_ls_ack_send_list (oi, oi->ls_ack, dst);
  3363. }