isis_pdu.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247
  1. /*
  2. * IS-IS Rout(e)ing protocol - isis_pdu.c
  3. * PDU processing
  4. *
  5. * Copyright (C) 2001,2002 Sampo Saaristo
  6. * Tampere University of Technology
  7. * Institute of Communications Engineering
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public Licenseas published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "memory.h"
  24. #include "thread.h"
  25. #include "linklist.h"
  26. #include "log.h"
  27. #include "stream.h"
  28. #include "vty.h"
  29. #include "hash.h"
  30. #include "prefix.h"
  31. #include "if.h"
  32. #include "checksum.h"
  33. #include "md5.h"
  34. #include "isisd/dict.h"
  35. #include "isisd/include-netbsd/iso.h"
  36. #include "isisd/isis_constants.h"
  37. #include "isisd/isis_common.h"
  38. #include "isisd/isis_flags.h"
  39. #include "isisd/isis_adjacency.h"
  40. #include "isisd/isis_circuit.h"
  41. #include "isisd/isis_network.h"
  42. #include "isisd/isis_misc.h"
  43. #include "isisd/isis_dr.h"
  44. #include "isisd/isis_tlv.h"
  45. #include "isisd/isisd.h"
  46. #include "isisd/isis_dynhn.h"
  47. #include "isisd/isis_lsp.h"
  48. #include "isisd/isis_pdu.h"
  49. #include "isisd/iso_checksum.h"
  50. #include "isisd/isis_csm.h"
  51. #include "isisd/isis_events.h"
  52. #define ISIS_MINIMUM_FIXED_HDR_LEN 15
  53. #define ISIS_MIN_PDU_LEN 13 /* partial seqnum pdu with id_len=2 */
  54. #ifndef PNBBY
  55. #define PNBBY 8
  56. #endif /* PNBBY */
  57. /* Utility mask array. */
  58. static const u_char maskbit[] = {
  59. 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  60. };
  61. /*
  62. * HELPER FUNCS
  63. */
  64. /*
  65. * Compares two sets of area addresses
  66. */
  67. static int
  68. area_match (struct list *left, struct list *right)
  69. {
  70. struct area_addr *addr1, *addr2;
  71. struct listnode *node1, *node2;
  72. for (ALL_LIST_ELEMENTS_RO (left, node1, addr1))
  73. {
  74. for (ALL_LIST_ELEMENTS_RO (right, node2, addr2))
  75. {
  76. if (addr1->addr_len == addr2->addr_len &&
  77. !memcmp (addr1->area_addr, addr2->area_addr, (int) addr1->addr_len))
  78. return 1; /* match */
  79. }
  80. }
  81. return 0; /* mismatch */
  82. }
  83. /*
  84. * Check if ip2 is in the ip1's network (function like Prefix.h:prefix_match() )
  85. * param ip1 the IS interface ip address structure
  86. * param ip2 the IIH's ip address
  87. * return 0 the IIH's IP is not in the IS's subnetwork
  88. * 1 the IIH's IP is in the IS's subnetwork
  89. */
  90. static int
  91. ip_same_subnet (struct prefix_ipv4 *ip1, struct in_addr *ip2)
  92. {
  93. u_char *addr1, *addr2;
  94. int shift, offset, offsetloop;
  95. int len;
  96. addr1 = (u_char *) & ip1->prefix.s_addr;
  97. addr2 = (u_char *) & ip2->s_addr;
  98. len = ip1->prefixlen;
  99. shift = len % PNBBY;
  100. offsetloop = offset = len / PNBBY;
  101. while (offsetloop--)
  102. if (addr1[offsetloop] != addr2[offsetloop])
  103. return 0;
  104. if (shift)
  105. if (maskbit[shift] & (addr1[offset] ^ addr2[offset]))
  106. return 0;
  107. return 1; /* match */
  108. }
  109. /*
  110. * Compares two set of ip addresses
  111. * param left the local interface's ip addresses
  112. * param right the iih interface's ip address
  113. * return 0 no match;
  114. * 1 match;
  115. */
  116. static int
  117. ip_match (struct list *left, struct list *right)
  118. {
  119. struct prefix_ipv4 *ip1;
  120. struct in_addr *ip2;
  121. struct listnode *node1, *node2;
  122. if ((left == NULL) || (right == NULL))
  123. return 0;
  124. for (ALL_LIST_ELEMENTS_RO (left, node1, ip1))
  125. {
  126. for (ALL_LIST_ELEMENTS_RO (right, node2, ip2))
  127. {
  128. if (ip_same_subnet (ip1, ip2))
  129. {
  130. return 1; /* match */
  131. }
  132. }
  133. }
  134. return 0;
  135. }
  136. /*
  137. * Checks whether we should accept a PDU of given level
  138. */
  139. static int
  140. accept_level (int level, int circuit_t)
  141. {
  142. int retval = ((circuit_t & level) == level); /* simple approach */
  143. return retval;
  144. }
  145. /*
  146. * Verify authentication information
  147. * Support cleartext and HMAC MD5 authentication
  148. */
  149. static int
  150. authentication_check (struct isis_passwd *remote, struct isis_passwd *local,
  151. struct stream *stream, uint32_t auth_tlv_offset)
  152. {
  153. unsigned char digest[ISIS_AUTH_MD5_SIZE];
  154. /* Auth fail () - passwd type mismatch */
  155. if (local->type != remote->type)
  156. return ISIS_ERROR;
  157. switch (local->type)
  158. {
  159. /* No authentication required */
  160. case ISIS_PASSWD_TYPE_UNUSED:
  161. break;
  162. /* Cleartext (ISO 10589) */
  163. case ISIS_PASSWD_TYPE_CLEARTXT:
  164. /* Auth fail () - passwd len mismatch */
  165. if (remote->len != local->len)
  166. return ISIS_ERROR;
  167. return memcmp (local->passwd, remote->passwd, local->len);
  168. /* HMAC MD5 (RFC 3567) */
  169. case ISIS_PASSWD_TYPE_HMAC_MD5:
  170. /* Auth fail () - passwd len mismatch */
  171. if (remote->len != ISIS_AUTH_MD5_SIZE)
  172. return ISIS_ERROR;
  173. /* Set the authentication value to 0 before the check */
  174. memset (STREAM_DATA (stream) + auth_tlv_offset + 3, 0,
  175. ISIS_AUTH_MD5_SIZE);
  176. /* Compute the digest */
  177. hmac_md5 (STREAM_DATA (stream), stream_get_endp (stream),
  178. (unsigned char *) &(local->passwd), local->len,
  179. (unsigned char *) &digest);
  180. /* Copy back the authentication value after the check */
  181. memcpy (STREAM_DATA (stream) + auth_tlv_offset + 3,
  182. remote->passwd, ISIS_AUTH_MD5_SIZE);
  183. return memcmp (digest, remote->passwd, ISIS_AUTH_MD5_SIZE);
  184. default:
  185. zlog_err ("Unsupported authentication type");
  186. return ISIS_ERROR;
  187. }
  188. /* Authentication pass when no authentication is configured */
  189. return ISIS_OK;
  190. }
  191. static int
  192. lsp_authentication_check (struct stream *stream, struct isis_area *area,
  193. int level, struct isis_passwd *passwd)
  194. {
  195. struct isis_link_state_hdr *hdr;
  196. uint32_t expected = 0, found = 0, auth_tlv_offset = 0;
  197. uint16_t checksum, rem_lifetime, pdu_len;
  198. struct tlvs tlvs;
  199. int retval = ISIS_OK;
  200. hdr = (struct isis_link_state_hdr *) (STREAM_PNT (stream));
  201. pdu_len = ntohs (hdr->pdu_len);
  202. expected |= TLVFLAG_AUTH_INFO;
  203. auth_tlv_offset = stream_get_getp (stream) + ISIS_LSP_HDR_LEN;
  204. retval = parse_tlvs (area->area_tag, STREAM_PNT (stream) + ISIS_LSP_HDR_LEN,
  205. pdu_len - ISIS_FIXED_HDR_LEN - ISIS_LSP_HDR_LEN,
  206. &expected, &found, &tlvs, &auth_tlv_offset);
  207. if (retval != ISIS_OK)
  208. {
  209. zlog_err ("ISIS-Upd (%s): Parse failed L%d LSP %s, seq 0x%08x, "
  210. "cksum 0x%04x, lifetime %us, len %u",
  211. area->area_tag, level, rawlspid_print (hdr->lsp_id),
  212. ntohl (hdr->seq_num), ntohs (hdr->checksum),
  213. ntohs (hdr->rem_lifetime), pdu_len);
  214. if ((isis->debugs & DEBUG_UPDATE_PACKETS) &&
  215. (isis->debugs & DEBUG_PACKET_DUMP))
  216. zlog_dump_data (STREAM_DATA (stream), stream_get_endp (stream));
  217. return retval;
  218. }
  219. if (!(found & TLVFLAG_AUTH_INFO))
  220. {
  221. zlog_err ("No authentication tlv in LSP");
  222. return ISIS_ERROR;
  223. }
  224. if (tlvs.auth_info.type != ISIS_PASSWD_TYPE_CLEARTXT &&
  225. tlvs.auth_info.type != ISIS_PASSWD_TYPE_HMAC_MD5)
  226. {
  227. zlog_err ("Unknown authentication type in LSP");
  228. return ISIS_ERROR;
  229. }
  230. /*
  231. * RFC 5304 set checksum and remaining lifetime to zero before
  232. * verification and reset to old values after verification.
  233. */
  234. checksum = hdr->checksum;
  235. rem_lifetime = hdr->rem_lifetime;
  236. hdr->checksum = 0;
  237. hdr->rem_lifetime = 0;
  238. retval = authentication_check (&tlvs.auth_info, passwd, stream,
  239. auth_tlv_offset);
  240. hdr->checksum = checksum;
  241. hdr->rem_lifetime = rem_lifetime;
  242. return retval;
  243. }
  244. /*
  245. * Processing helper functions
  246. */
  247. static void
  248. del_addr (void *val)
  249. {
  250. XFREE (MTYPE_ISIS_TMP, val);
  251. }
  252. static void
  253. tlvs_to_adj_area_addrs (struct tlvs *tlvs, struct isis_adjacency *adj)
  254. {
  255. struct listnode *node;
  256. struct area_addr *area_addr, *malloced;
  257. if (adj->area_addrs)
  258. {
  259. adj->area_addrs->del = del_addr;
  260. list_delete (adj->area_addrs);
  261. }
  262. adj->area_addrs = list_new ();
  263. if (tlvs->area_addrs)
  264. {
  265. for (ALL_LIST_ELEMENTS_RO (tlvs->area_addrs, node, area_addr))
  266. {
  267. malloced = XMALLOC (MTYPE_ISIS_TMP, sizeof (struct area_addr));
  268. memcpy (malloced, area_addr, sizeof (struct area_addr));
  269. listnode_add (adj->area_addrs, malloced);
  270. }
  271. }
  272. }
  273. static int
  274. tlvs_to_adj_nlpids (struct tlvs *tlvs, struct isis_adjacency *adj)
  275. {
  276. int i;
  277. struct nlpids *tlv_nlpids;
  278. if (tlvs->nlpids)
  279. {
  280. tlv_nlpids = tlvs->nlpids;
  281. if (tlv_nlpids->count > array_size (adj->nlpids.nlpids))
  282. return 1;
  283. adj->nlpids.count = tlv_nlpids->count;
  284. for (i = 0; i < tlv_nlpids->count; i++)
  285. {
  286. adj->nlpids.nlpids[i] = tlv_nlpids->nlpids[i];
  287. }
  288. }
  289. return 0;
  290. }
  291. static void
  292. tlvs_to_adj_ipv4_addrs (struct tlvs *tlvs, struct isis_adjacency *adj)
  293. {
  294. struct listnode *node;
  295. struct in_addr *ipv4_addr, *malloced;
  296. if (adj->ipv4_addrs)
  297. {
  298. adj->ipv4_addrs->del = del_addr;
  299. list_delete (adj->ipv4_addrs);
  300. }
  301. adj->ipv4_addrs = list_new ();
  302. if (tlvs->ipv4_addrs)
  303. {
  304. for (ALL_LIST_ELEMENTS_RO (tlvs->ipv4_addrs, node, ipv4_addr))
  305. {
  306. malloced = XMALLOC (MTYPE_ISIS_TMP, sizeof (struct in_addr));
  307. memcpy (malloced, ipv4_addr, sizeof (struct in_addr));
  308. listnode_add (adj->ipv4_addrs, malloced);
  309. }
  310. }
  311. }
  312. #ifdef HAVE_IPV6
  313. static void
  314. tlvs_to_adj_ipv6_addrs (struct tlvs *tlvs, struct isis_adjacency *adj)
  315. {
  316. struct listnode *node;
  317. struct in6_addr *ipv6_addr, *malloced;
  318. if (adj->ipv6_addrs)
  319. {
  320. adj->ipv6_addrs->del = del_addr;
  321. list_delete (adj->ipv6_addrs);
  322. }
  323. adj->ipv6_addrs = list_new ();
  324. if (tlvs->ipv6_addrs)
  325. {
  326. for (ALL_LIST_ELEMENTS_RO (tlvs->ipv6_addrs, node, ipv6_addr))
  327. {
  328. malloced = XMALLOC (MTYPE_ISIS_TMP, sizeof (struct in6_addr));
  329. memcpy (malloced, ipv6_addr, sizeof (struct in6_addr));
  330. listnode_add (adj->ipv6_addrs, malloced);
  331. }
  332. }
  333. }
  334. #endif /* HAVE_IPV6 */
  335. /*
  336. * RECEIVE SIDE
  337. */
  338. /*
  339. * Process P2P IIH
  340. * ISO - 10589
  341. * Section 8.2.5 - Receiving point-to-point IIH PDUs
  342. *
  343. */
  344. static int
  345. process_p2p_hello (struct isis_circuit *circuit)
  346. {
  347. int retval = ISIS_OK;
  348. struct isis_p2p_hello_hdr *hdr;
  349. struct isis_adjacency *adj;
  350. u_int32_t expected = 0, found = 0, auth_tlv_offset = 0;
  351. uint16_t pdu_len;
  352. struct tlvs tlvs;
  353. int v4_usable = 0, v6_usable = 0;
  354. if (isis->debugs & DEBUG_ADJ_PACKETS)
  355. {
  356. zlog_debug ("ISIS-Adj (%s): Rcvd P2P IIH on %s, cirType %s, cirID %u",
  357. circuit->area->area_tag, circuit->interface->name,
  358. circuit_t2string (circuit->is_type), circuit->circuit_id);
  359. if (isis->debugs & DEBUG_PACKET_DUMP)
  360. zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
  361. stream_get_endp (circuit->rcv_stream));
  362. }
  363. if (circuit->circ_type != CIRCUIT_T_P2P)
  364. {
  365. zlog_warn ("p2p hello on non p2p circuit");
  366. return ISIS_WARNING;
  367. }
  368. if ((stream_get_endp (circuit->rcv_stream) -
  369. stream_get_getp (circuit->rcv_stream)) < ISIS_P2PHELLO_HDRLEN)
  370. {
  371. zlog_warn ("Packet too short");
  372. return ISIS_WARNING;
  373. }
  374. /* 8.2.5.1 PDU acceptance tests */
  375. /* 8.2.5.1 a) external domain untrue */
  376. /* FIXME: not useful at all? */
  377. /* 8.2.5.1 b) ID Length mismatch */
  378. /* checked at the handle_pdu */
  379. /* 8.2.5.2 IIH PDU Processing */
  380. /* 8.2.5.2 a) 1) Maximum Area Addresses */
  381. /* Already checked, and can also be ommited */
  382. /*
  383. * Get the header
  384. */
  385. hdr = (struct isis_p2p_hello_hdr *) STREAM_PNT (circuit->rcv_stream);
  386. pdu_len = ntohs (hdr->pdu_len);
  387. if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_P2PHELLO_HDRLEN) ||
  388. pdu_len > ISO_MTU(circuit) ||
  389. pdu_len > stream_get_endp (circuit->rcv_stream))
  390. {
  391. zlog_warn ("ISIS-Adj (%s): Rcvd P2P IIH from (%s) with "
  392. "invalid pdu length %d",
  393. circuit->area->area_tag, circuit->interface->name, pdu_len);
  394. return ISIS_WARNING;
  395. }
  396. /*
  397. * Set the stream endp to PDU length, ignoring additional padding
  398. * introduced by transport chips.
  399. */
  400. if (pdu_len < stream_get_endp (circuit->rcv_stream))
  401. stream_set_endp (circuit->rcv_stream, pdu_len);
  402. stream_forward_getp (circuit->rcv_stream, ISIS_P2PHELLO_HDRLEN);
  403. /*
  404. * Lets get the TLVS now
  405. */
  406. expected |= TLVFLAG_AREA_ADDRS;
  407. expected |= TLVFLAG_AUTH_INFO;
  408. expected |= TLVFLAG_NLPID;
  409. expected |= TLVFLAG_IPV4_ADDR;
  410. expected |= TLVFLAG_IPV6_ADDR;
  411. auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
  412. retval = parse_tlvs (circuit->area->area_tag,
  413. STREAM_PNT (circuit->rcv_stream),
  414. pdu_len - ISIS_P2PHELLO_HDRLEN - ISIS_FIXED_HDR_LEN,
  415. &expected, &found, &tlvs, &auth_tlv_offset);
  416. if (retval > ISIS_WARNING)
  417. {
  418. zlog_warn ("parse_tlvs() failed");
  419. free_tlvs (&tlvs);
  420. return retval;
  421. };
  422. if (!(found & TLVFLAG_AREA_ADDRS))
  423. {
  424. zlog_warn ("No Area addresses TLV in P2P IS to IS hello");
  425. free_tlvs (&tlvs);
  426. return ISIS_WARNING;
  427. }
  428. if (!(found & TLVFLAG_NLPID))
  429. {
  430. zlog_warn ("No supported protocols TLV in P2P IS to IS hello");
  431. free_tlvs (&tlvs);
  432. return ISIS_WARNING;
  433. }
  434. /* 8.2.5.1 c) Authentication */
  435. if (circuit->passwd.type)
  436. {
  437. if (!(found & TLVFLAG_AUTH_INFO) ||
  438. authentication_check (&tlvs.auth_info, &circuit->passwd,
  439. circuit->rcv_stream, auth_tlv_offset))
  440. {
  441. isis_event_auth_failure (circuit->area->area_tag,
  442. "P2P hello authentication failure",
  443. hdr->source_id);
  444. free_tlvs (&tlvs);
  445. return ISIS_OK;
  446. }
  447. }
  448. /*
  449. * check if it's own interface ip match iih ip addrs
  450. */
  451. if (found & TLVFLAG_IPV4_ADDR)
  452. {
  453. if (ip_match (circuit->ip_addrs, tlvs.ipv4_addrs))
  454. v4_usable = 1;
  455. else
  456. zlog_warn ("ISIS-Adj: IPv4 addresses present but no overlap "
  457. "in P2P IIH from %s\n", circuit->interface->name);
  458. }
  459. #ifndef HAVE_IPV6
  460. else /* !(found & TLVFLAG_IPV4_ADDR) */
  461. zlog_warn ("ISIS-Adj: no IPv4 in P2P IIH from %s "
  462. "(this isisd has no IPv6)\n", circuit->interface->name);
  463. #else
  464. if (found & TLVFLAG_IPV6_ADDR)
  465. {
  466. /* TBA: check that we have a linklocal ourselves? */
  467. struct listnode *node;
  468. struct in6_addr *ip;
  469. for (ALL_LIST_ELEMENTS_RO (tlvs.ipv6_addrs, node, ip))
  470. if (IN6_IS_ADDR_LINKLOCAL (ip))
  471. {
  472. v6_usable = 1;
  473. break;
  474. }
  475. if (!v6_usable)
  476. zlog_warn ("ISIS-Adj: IPv6 addresses present but no link-local "
  477. "in P2P IIH from %s\n", circuit->interface->name);
  478. }
  479. if (!(found & (TLVFLAG_IPV4_ADDR | TLVFLAG_IPV6_ADDR)))
  480. zlog_warn ("ISIS-Adj: neither IPv4 nor IPv6 addr in P2P IIH from %s\n",
  481. circuit->interface->name);
  482. #endif
  483. if (!v6_usable && !v4_usable)
  484. {
  485. free_tlvs (&tlvs);
  486. return ISIS_WARNING;
  487. }
  488. /*
  489. * it's own p2p IIH PDU - discard
  490. */
  491. if (!memcmp (hdr->source_id, isis->sysid, ISIS_SYS_ID_LEN))
  492. {
  493. zlog_warn ("ISIS-Adj (%s): it's own IIH PDU - discarded",
  494. circuit->area->area_tag);
  495. free_tlvs (&tlvs);
  496. return ISIS_WARNING;
  497. }
  498. /*
  499. * My interpertation of the ISO, if no adj exists we will create one for
  500. * the circuit
  501. */
  502. adj = circuit->u.p2p.neighbor;
  503. /* If an adjacency exists, check it is with the source of the hello
  504. * packets */
  505. if (adj)
  506. {
  507. if (memcmp(hdr->source_id, adj->sysid, ISIS_SYS_ID_LEN))
  508. {
  509. zlog_debug("hello source and adjacency do not match, set adj down\n");
  510. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "adj do not exist");
  511. return 0;
  512. }
  513. }
  514. if (!adj || adj->level != hdr->circuit_t)
  515. {
  516. if (!adj)
  517. {
  518. adj = isis_new_adj (hdr->source_id, NULL, hdr->circuit_t, circuit);
  519. if (adj == NULL)
  520. return ISIS_ERROR;
  521. }
  522. else
  523. {
  524. adj->level = hdr->circuit_t;
  525. }
  526. circuit->u.p2p.neighbor = adj;
  527. /* Build lsp with the new neighbor entry when a new
  528. * adjacency is formed. Set adjacency circuit type to
  529. * IIH PDU header circuit type before lsp is regenerated
  530. * when an adjacency is up. This will result in the new
  531. * adjacency entry getting added to the lsp tlv neighbor list.
  532. */
  533. adj->circuit_t = hdr->circuit_t;
  534. isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
  535. adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
  536. }
  537. /* 8.2.6 Monitoring point-to-point adjacencies */
  538. adj->hold_time = ntohs (hdr->hold_time);
  539. adj->last_upd = time (NULL);
  540. /* we do this now because the adj may not survive till the end... */
  541. tlvs_to_adj_area_addrs (&tlvs, adj);
  542. /* which protocol are spoken ??? */
  543. if (tlvs_to_adj_nlpids (&tlvs, adj))
  544. {
  545. free_tlvs (&tlvs);
  546. return ISIS_WARNING;
  547. }
  548. /* we need to copy addresses to the adj */
  549. if (found & TLVFLAG_IPV4_ADDR)
  550. tlvs_to_adj_ipv4_addrs (&tlvs, adj);
  551. #ifdef HAVE_IPV6
  552. if (found & TLVFLAG_IPV6_ADDR)
  553. tlvs_to_adj_ipv6_addrs (&tlvs, adj);
  554. #endif /* HAVE_IPV6 */
  555. /* lets take care of the expiry */
  556. THREAD_TIMER_OFF (adj->t_expire);
  557. THREAD_TIMER_ON (master, adj->t_expire, isis_adj_expire, adj,
  558. (long) adj->hold_time);
  559. /* 8.2.5.2 a) a match was detected */
  560. if (area_match (circuit->area->area_addrs, tlvs.area_addrs))
  561. {
  562. /* 8.2.5.2 a) 2) If the system is L1 - table 5 */
  563. if (circuit->area->is_type == IS_LEVEL_1)
  564. {
  565. switch (hdr->circuit_t)
  566. {
  567. case IS_LEVEL_1:
  568. case IS_LEVEL_1_AND_2:
  569. if (adj->adj_state != ISIS_ADJ_UP)
  570. {
  571. /* (4) adj state up */
  572. isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
  573. /* (5) adj usage level 1 */
  574. adj->adj_usage = ISIS_ADJ_LEVEL1;
  575. }
  576. else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
  577. {
  578. ; /* accept */
  579. }
  580. break;
  581. case IS_LEVEL_2:
  582. if (adj->adj_state != ISIS_ADJ_UP)
  583. {
  584. /* (7) reject - wrong system type event */
  585. zlog_warn ("wrongSystemType");
  586. free_tlvs (&tlvs);
  587. return ISIS_WARNING; /* Reject */
  588. }
  589. else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
  590. {
  591. /* (6) down - wrong system */
  592. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
  593. }
  594. break;
  595. }
  596. }
  597. /* 8.2.5.2 a) 3) If the system is L1L2 - table 6 */
  598. if (circuit->area->is_type == IS_LEVEL_1_AND_2)
  599. {
  600. switch (hdr->circuit_t)
  601. {
  602. case IS_LEVEL_1:
  603. if (adj->adj_state != ISIS_ADJ_UP)
  604. {
  605. /* (6) adj state up */
  606. isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
  607. /* (7) adj usage level 1 */
  608. adj->adj_usage = ISIS_ADJ_LEVEL1;
  609. }
  610. else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
  611. {
  612. ; /* accept */
  613. }
  614. else if ((adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
  615. (adj->adj_usage == ISIS_ADJ_LEVEL2))
  616. {
  617. /* (8) down - wrong system */
  618. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
  619. }
  620. break;
  621. case IS_LEVEL_2:
  622. if (adj->adj_state != ISIS_ADJ_UP)
  623. {
  624. /* (6) adj state up */
  625. isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
  626. /* (9) adj usage level 2 */
  627. adj->adj_usage = ISIS_ADJ_LEVEL2;
  628. }
  629. else if ((adj->adj_usage == ISIS_ADJ_LEVEL1) ||
  630. (adj->adj_usage == ISIS_ADJ_LEVEL1AND2))
  631. {
  632. /* (8) down - wrong system */
  633. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
  634. }
  635. else if (adj->adj_usage == ISIS_ADJ_LEVEL2)
  636. {
  637. ; /* Accept */
  638. }
  639. break;
  640. case IS_LEVEL_1_AND_2:
  641. if (adj->adj_state != ISIS_ADJ_UP)
  642. {
  643. /* (6) adj state up */
  644. isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
  645. /* (10) adj usage level 1 */
  646. adj->adj_usage = ISIS_ADJ_LEVEL1AND2;
  647. }
  648. else if ((adj->adj_usage == ISIS_ADJ_LEVEL1) ||
  649. (adj->adj_usage == ISIS_ADJ_LEVEL2))
  650. {
  651. /* (8) down - wrong system */
  652. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
  653. }
  654. else if (adj->adj_usage == ISIS_ADJ_LEVEL1AND2)
  655. {
  656. ; /* Accept */
  657. }
  658. break;
  659. }
  660. }
  661. /* 8.2.5.2 a) 4) If the system is L2 - table 7 */
  662. if (circuit->area->is_type == IS_LEVEL_2)
  663. {
  664. switch (hdr->circuit_t)
  665. {
  666. case IS_LEVEL_1:
  667. if (adj->adj_state != ISIS_ADJ_UP)
  668. {
  669. /* (5) reject - wrong system type event */
  670. zlog_warn ("wrongSystemType");
  671. free_tlvs (&tlvs);
  672. return ISIS_WARNING; /* Reject */
  673. }
  674. else if ((adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
  675. (adj->adj_usage == ISIS_ADJ_LEVEL2))
  676. {
  677. /* (6) down - wrong system */
  678. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
  679. }
  680. break;
  681. case IS_LEVEL_1_AND_2:
  682. case IS_LEVEL_2:
  683. if (adj->adj_state != ISIS_ADJ_UP)
  684. {
  685. /* (7) adj state up */
  686. isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
  687. /* (8) adj usage level 2 */
  688. adj->adj_usage = ISIS_ADJ_LEVEL2;
  689. }
  690. else if (adj->adj_usage == ISIS_ADJ_LEVEL1AND2)
  691. {
  692. /* (6) down - wrong system */
  693. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
  694. }
  695. else if (adj->adj_usage == ISIS_ADJ_LEVEL2)
  696. {
  697. ; /* Accept */
  698. }
  699. break;
  700. }
  701. }
  702. }
  703. /* 8.2.5.2 b) if no match was detected */
  704. else if (listcount (circuit->area->area_addrs) > 0)
  705. {
  706. if (circuit->area->is_type == IS_LEVEL_1)
  707. {
  708. /* 8.2.5.2 b) 1) is_type L1 and adj is not up */
  709. if (adj->adj_state != ISIS_ADJ_UP)
  710. {
  711. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Area Mismatch");
  712. /* 8.2.5.2 b) 2)is_type L1 and adj is up */
  713. }
  714. else
  715. {
  716. isis_adj_state_change (adj, ISIS_ADJ_DOWN,
  717. "Down - Area Mismatch");
  718. }
  719. }
  720. /* 8.2.5.2 b 3 If the system is L2 or L1L2 - table 8 */
  721. else
  722. {
  723. switch (hdr->circuit_t)
  724. {
  725. case IS_LEVEL_1:
  726. if (adj->adj_state != ISIS_ADJ_UP)
  727. {
  728. /* (6) reject - Area Mismatch event */
  729. zlog_warn ("AreaMismatch");
  730. free_tlvs (&tlvs);
  731. return ISIS_WARNING; /* Reject */
  732. }
  733. else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
  734. {
  735. /* (7) down - area mismatch */
  736. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Area Mismatch");
  737. }
  738. else if ((adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
  739. (adj->adj_usage == ISIS_ADJ_LEVEL2))
  740. {
  741. /* (7) down - wrong system */
  742. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
  743. }
  744. break;
  745. case IS_LEVEL_1_AND_2:
  746. case IS_LEVEL_2:
  747. if (adj->adj_state != ISIS_ADJ_UP)
  748. {
  749. /* (8) adj state up */
  750. isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
  751. /* (9) adj usage level 2 */
  752. adj->adj_usage = ISIS_ADJ_LEVEL2;
  753. }
  754. else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
  755. {
  756. /* (7) down - wrong system */
  757. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
  758. }
  759. else if (adj->adj_usage == ISIS_ADJ_LEVEL1AND2)
  760. {
  761. if (hdr->circuit_t == IS_LEVEL_2)
  762. {
  763. /* (7) down - wrong system */
  764. isis_adj_state_change (adj, ISIS_ADJ_DOWN,
  765. "Wrong System");
  766. }
  767. else
  768. {
  769. /* (7) down - area mismatch */
  770. isis_adj_state_change (adj, ISIS_ADJ_DOWN,
  771. "Area Mismatch");
  772. }
  773. }
  774. else if (adj->adj_usage == ISIS_ADJ_LEVEL2)
  775. {
  776. ; /* Accept */
  777. }
  778. break;
  779. }
  780. }
  781. }
  782. else
  783. {
  784. /* down - area mismatch */
  785. isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Area Mismatch");
  786. }
  787. /* 8.2.5.2 c) if the action was up - comparing circuit IDs */
  788. /* FIXME - Missing parts */
  789. /* some of my own understanding of the ISO, why the heck does
  790. * it not say what should I change the system_type to...
  791. */
  792. switch (adj->adj_usage)
  793. {
  794. case ISIS_ADJ_LEVEL1:
  795. adj->sys_type = ISIS_SYSTYPE_L1_IS;
  796. break;
  797. case ISIS_ADJ_LEVEL2:
  798. adj->sys_type = ISIS_SYSTYPE_L2_IS;
  799. break;
  800. case ISIS_ADJ_LEVEL1AND2:
  801. adj->sys_type = ISIS_SYSTYPE_L2_IS;
  802. break;
  803. case ISIS_ADJ_NONE:
  804. adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
  805. break;
  806. }
  807. if (isis->debugs & DEBUG_ADJ_PACKETS)
  808. {
  809. zlog_debug ("ISIS-Adj (%s): Rcvd P2P IIH from (%s), cir type %s,"
  810. " cir id %02d, length %d",
  811. circuit->area->area_tag, circuit->interface->name,
  812. circuit_t2string (circuit->is_type),
  813. circuit->circuit_id, pdu_len);
  814. }
  815. free_tlvs (&tlvs);
  816. return retval;
  817. }
  818. /*
  819. * Process IS-IS LAN Level 1/2 Hello PDU
  820. */
  821. static int
  822. process_lan_hello (int level, struct isis_circuit *circuit, const u_char *ssnpa)
  823. {
  824. int retval = ISIS_OK;
  825. struct isis_lan_hello_hdr hdr;
  826. struct isis_adjacency *adj;
  827. u_int32_t expected = 0, found = 0, auth_tlv_offset = 0;
  828. struct tlvs tlvs;
  829. u_char *snpa;
  830. struct listnode *node;
  831. int v4_usable = 0, v6_usable = 0;
  832. if (isis->debugs & DEBUG_ADJ_PACKETS)
  833. {
  834. zlog_debug ("ISIS-Adj (%s): Rcvd L%d LAN IIH on %s, cirType %s, "
  835. "cirID %u",
  836. circuit->area->area_tag, level, circuit->interface->name,
  837. circuit_t2string (circuit->is_type), circuit->circuit_id);
  838. if (isis->debugs & DEBUG_PACKET_DUMP)
  839. zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
  840. stream_get_endp (circuit->rcv_stream));
  841. }
  842. if (circuit->circ_type != CIRCUIT_T_BROADCAST)
  843. {
  844. zlog_warn ("lan hello on non broadcast circuit");
  845. return ISIS_WARNING;
  846. }
  847. if ((stream_get_endp (circuit->rcv_stream) -
  848. stream_get_getp (circuit->rcv_stream)) < ISIS_LANHELLO_HDRLEN)
  849. {
  850. zlog_warn ("Packet too short");
  851. return ISIS_WARNING;
  852. }
  853. if (circuit->ext_domain)
  854. {
  855. zlog_debug ("level %d LAN Hello received over circuit with "
  856. "externalDomain = true", level);
  857. return ISIS_WARNING;
  858. }
  859. if (!accept_level (level, circuit->is_type))
  860. {
  861. if (isis->debugs & DEBUG_ADJ_PACKETS)
  862. {
  863. zlog_debug ("ISIS-Adj (%s): Interface level mismatch, %s",
  864. circuit->area->area_tag, circuit->interface->name);
  865. }
  866. return ISIS_WARNING;
  867. }
  868. #if 0
  869. /* Cisco's debug message compatability */
  870. if (!accept_level (level, circuit->area->is_type))
  871. {
  872. if (isis->debugs & DEBUG_ADJ_PACKETS)
  873. {
  874. zlog_debug ("ISIS-Adj (%s): is type mismatch",
  875. circuit->area->area_tag);
  876. }
  877. return ISIS_WARNING;
  878. }
  879. #endif
  880. /*
  881. * Fill the header
  882. */
  883. hdr.circuit_t = stream_getc (circuit->rcv_stream);
  884. stream_get (hdr.source_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
  885. hdr.hold_time = stream_getw (circuit->rcv_stream);
  886. hdr.pdu_len = stream_getw (circuit->rcv_stream);
  887. hdr.prio = stream_getc (circuit->rcv_stream);
  888. stream_get (hdr.lan_id, circuit->rcv_stream, ISIS_SYS_ID_LEN + 1);
  889. if (hdr.pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_LANHELLO_HDRLEN) ||
  890. hdr.pdu_len > ISO_MTU(circuit) ||
  891. hdr.pdu_len > stream_get_endp (circuit->rcv_stream))
  892. {
  893. zlog_warn ("ISIS-Adj (%s): Rcvd LAN IIH from (%s) with "
  894. "invalid pdu length %d",
  895. circuit->area->area_tag, circuit->interface->name,
  896. hdr.pdu_len);
  897. return ISIS_WARNING;
  898. }
  899. /*
  900. * Set the stream endp to PDU length, ignoring additional padding
  901. * introduced by transport chips.
  902. */
  903. if (hdr.pdu_len < stream_get_endp (circuit->rcv_stream))
  904. stream_set_endp (circuit->rcv_stream, hdr.pdu_len);
  905. if (hdr.circuit_t != IS_LEVEL_1 &&
  906. hdr.circuit_t != IS_LEVEL_2 &&
  907. hdr.circuit_t != IS_LEVEL_1_AND_2 &&
  908. (level & hdr.circuit_t) == 0)
  909. {
  910. zlog_err ("Level %d LAN Hello with Circuit Type %d", level,
  911. hdr.circuit_t);
  912. return ISIS_ERROR;
  913. }
  914. /*
  915. * Then get the tlvs
  916. */
  917. expected |= TLVFLAG_AUTH_INFO;
  918. expected |= TLVFLAG_AREA_ADDRS;
  919. expected |= TLVFLAG_LAN_NEIGHS;
  920. expected |= TLVFLAG_NLPID;
  921. expected |= TLVFLAG_IPV4_ADDR;
  922. expected |= TLVFLAG_IPV6_ADDR;
  923. auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
  924. retval = parse_tlvs (circuit->area->area_tag,
  925. STREAM_PNT (circuit->rcv_stream),
  926. hdr.pdu_len - ISIS_LANHELLO_HDRLEN - ISIS_FIXED_HDR_LEN,
  927. &expected, &found, &tlvs,
  928. &auth_tlv_offset);
  929. if (retval > ISIS_WARNING)
  930. {
  931. zlog_warn ("parse_tlvs() failed");
  932. goto out;
  933. }
  934. if (!(found & TLVFLAG_AREA_ADDRS))
  935. {
  936. zlog_warn ("No Area addresses TLV in Level %d LAN IS to IS hello",
  937. level);
  938. retval = ISIS_WARNING;
  939. goto out;
  940. }
  941. if (!(found & TLVFLAG_NLPID))
  942. {
  943. zlog_warn ("No supported protocols TLV in Level %d LAN IS to IS hello",
  944. level);
  945. retval = ISIS_WARNING;
  946. goto out;
  947. }
  948. /* Verify authentication, either cleartext of HMAC MD5 */
  949. if (circuit->passwd.type)
  950. {
  951. if (!(found & TLVFLAG_AUTH_INFO) ||
  952. authentication_check (&tlvs.auth_info, &circuit->passwd,
  953. circuit->rcv_stream, auth_tlv_offset))
  954. {
  955. isis_event_auth_failure (circuit->area->area_tag,
  956. "LAN hello authentication failure",
  957. hdr.source_id);
  958. retval = ISIS_WARNING;
  959. goto out;
  960. }
  961. }
  962. if (!memcmp (hdr.source_id, isis->sysid, ISIS_SYS_ID_LEN))
  963. {
  964. zlog_warn ("ISIS-Adj (%s): duplicate system ID on interface %s",
  965. circuit->area->area_tag, circuit->interface->name);
  966. return ISIS_WARNING;
  967. }
  968. /*
  969. * Accept the level 1 adjacency only if a match between local and
  970. * remote area addresses is found
  971. */
  972. if (listcount (circuit->area->area_addrs) == 0 ||
  973. (level == IS_LEVEL_1 &&
  974. area_match (circuit->area->area_addrs, tlvs.area_addrs) == 0))
  975. {
  976. if (isis->debugs & DEBUG_ADJ_PACKETS)
  977. {
  978. zlog_debug ("ISIS-Adj (%s): Area mismatch, level %d IIH on %s",
  979. circuit->area->area_tag, level,
  980. circuit->interface->name);
  981. }
  982. retval = ISIS_OK;
  983. goto out;
  984. }
  985. /*
  986. * it's own IIH PDU - discard silently
  987. */
  988. if (!memcmp (circuit->u.bc.snpa, ssnpa, ETH_ALEN))
  989. {
  990. zlog_debug ("ISIS-Adj (%s): it's own IIH PDU - discarded",
  991. circuit->area->area_tag);
  992. retval = ISIS_OK;
  993. goto out;
  994. }
  995. /*
  996. * check if it's own interface ip match iih ip addrs
  997. */
  998. if (found & TLVFLAG_IPV4_ADDR)
  999. {
  1000. if (ip_match (circuit->ip_addrs, tlvs.ipv4_addrs))
  1001. v4_usable = 1;
  1002. else
  1003. zlog_warn ("ISIS-Adj: IPv4 addresses present but no overlap "
  1004. "in LAN IIH from %s\n", circuit->interface->name);
  1005. }
  1006. #ifndef HAVE_IPV6
  1007. else /* !(found & TLVFLAG_IPV4_ADDR) */
  1008. zlog_warn ("ISIS-Adj: no IPv4 in LAN IIH from %s "
  1009. "(this isisd has no IPv6)\n", circuit->interface->name);
  1010. #else
  1011. if (found & TLVFLAG_IPV6_ADDR)
  1012. {
  1013. /* TBA: check that we have a linklocal ourselves? */
  1014. struct listnode *node;
  1015. struct in6_addr *ip;
  1016. for (ALL_LIST_ELEMENTS_RO (tlvs.ipv6_addrs, node, ip))
  1017. if (IN6_IS_ADDR_LINKLOCAL (ip))
  1018. {
  1019. v6_usable = 1;
  1020. break;
  1021. }
  1022. if (!v6_usable)
  1023. zlog_warn ("ISIS-Adj: IPv6 addresses present but no link-local "
  1024. "in LAN IIH from %s\n", circuit->interface->name);
  1025. }
  1026. if (!(found & (TLVFLAG_IPV4_ADDR | TLVFLAG_IPV6_ADDR)))
  1027. zlog_warn ("ISIS-Adj: neither IPv4 nor IPv6 addr in LAN IIH from %s\n",
  1028. circuit->interface->name);
  1029. #endif
  1030. if (!v6_usable && !v4_usable)
  1031. {
  1032. free_tlvs (&tlvs);
  1033. return ISIS_WARNING;
  1034. }
  1035. adj = isis_adj_lookup (hdr.source_id, circuit->u.bc.adjdb[level - 1]);
  1036. if ((adj == NULL) || (memcmp(adj->snpa, ssnpa, ETH_ALEN)) ||
  1037. (adj->level != level))
  1038. {
  1039. if (!adj)
  1040. {
  1041. /*
  1042. * Do as in 8.4.2.5
  1043. */
  1044. adj = isis_new_adj (hdr.source_id, ssnpa, level, circuit);
  1045. if (adj == NULL)
  1046. {
  1047. retval = ISIS_ERROR;
  1048. goto out;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. if (ssnpa) {
  1054. memcpy (adj->snpa, ssnpa, 6);
  1055. } else {
  1056. memset (adj->snpa, ' ', 6);
  1057. }
  1058. adj->level = level;
  1059. }
  1060. isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
  1061. if (level == IS_LEVEL_1)
  1062. adj->sys_type = ISIS_SYSTYPE_L1_IS;
  1063. else
  1064. adj->sys_type = ISIS_SYSTYPE_L2_IS;
  1065. list_delete_all_node (circuit->u.bc.lan_neighs[level - 1]);
  1066. isis_adj_build_neigh_list (circuit->u.bc.adjdb[level - 1],
  1067. circuit->u.bc.lan_neighs[level - 1]);
  1068. }
  1069. if(adj->dis_record[level-1].dis==ISIS_IS_DIS)
  1070. switch (level)
  1071. {
  1072. case 1:
  1073. if (memcmp (circuit->u.bc.l1_desig_is, hdr.lan_id, ISIS_SYS_ID_LEN + 1))
  1074. {
  1075. thread_add_event (master, isis_event_dis_status_change, circuit, 0);
  1076. memcpy (&circuit->u.bc.l1_desig_is, hdr.lan_id,
  1077. ISIS_SYS_ID_LEN + 1);
  1078. }
  1079. break;
  1080. case 2:
  1081. if (memcmp (circuit->u.bc.l2_desig_is, hdr.lan_id, ISIS_SYS_ID_LEN + 1))
  1082. {
  1083. thread_add_event (master, isis_event_dis_status_change, circuit, 0);
  1084. memcpy (&circuit->u.bc.l2_desig_is, hdr.lan_id,
  1085. ISIS_SYS_ID_LEN + 1);
  1086. }
  1087. break;
  1088. }
  1089. adj->hold_time = hdr.hold_time;
  1090. adj->last_upd = time (NULL);
  1091. adj->prio[level - 1] = hdr.prio;
  1092. memcpy (adj->lanid, hdr.lan_id, ISIS_SYS_ID_LEN + 1);
  1093. tlvs_to_adj_area_addrs (&tlvs, adj);
  1094. /* which protocol are spoken ??? */
  1095. if (tlvs_to_adj_nlpids (&tlvs, adj))
  1096. {
  1097. retval = ISIS_WARNING;
  1098. goto out;
  1099. }
  1100. /* we need to copy addresses to the adj */
  1101. if (found & TLVFLAG_IPV4_ADDR)
  1102. tlvs_to_adj_ipv4_addrs (&tlvs, adj);
  1103. #ifdef HAVE_IPV6
  1104. if (found & TLVFLAG_IPV6_ADDR)
  1105. tlvs_to_adj_ipv6_addrs (&tlvs, adj);
  1106. #endif /* HAVE_IPV6 */
  1107. adj->circuit_t = hdr.circuit_t;
  1108. /* lets take care of the expiry */
  1109. THREAD_TIMER_OFF (adj->t_expire);
  1110. THREAD_TIMER_ON (master, adj->t_expire, isis_adj_expire, adj,
  1111. (long) adj->hold_time);
  1112. /*
  1113. * If the snpa for this circuit is found from LAN Neighbours TLV
  1114. * we have two-way communication -> adjacency can be put to state "up"
  1115. */
  1116. if (found & TLVFLAG_LAN_NEIGHS)
  1117. {
  1118. if (adj->adj_state != ISIS_ADJ_UP)
  1119. {
  1120. for (ALL_LIST_ELEMENTS_RO (tlvs.lan_neighs, node, snpa))
  1121. {
  1122. if (!memcmp (snpa, circuit->u.bc.snpa, ETH_ALEN))
  1123. {
  1124. isis_adj_state_change (adj, ISIS_ADJ_UP,
  1125. "own SNPA found in LAN Neighbours TLV");
  1126. }
  1127. }
  1128. }
  1129. else
  1130. {
  1131. int found = 0;
  1132. for (ALL_LIST_ELEMENTS_RO (tlvs.lan_neighs, node, snpa))
  1133. if (!memcmp (snpa, circuit->u.bc.snpa, ETH_ALEN))
  1134. {
  1135. found = 1;
  1136. break;
  1137. }
  1138. if (found == 0)
  1139. isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING,
  1140. "own SNPA not found in LAN Neighbours TLV");
  1141. }
  1142. }
  1143. else if (adj->adj_state == ISIS_ADJ_UP)
  1144. {
  1145. isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING,
  1146. "no LAN Neighbours TLV found");
  1147. }
  1148. out:
  1149. if (isis->debugs & DEBUG_ADJ_PACKETS)
  1150. {
  1151. zlog_debug ("ISIS-Adj (%s): Rcvd L%d LAN IIH from %s on %s, cirType %s, "
  1152. "cirID %u, length %zd",
  1153. circuit->area->area_tag,
  1154. level, snpa_print (ssnpa), circuit->interface->name,
  1155. circuit_t2string (circuit->is_type),
  1156. circuit->circuit_id,
  1157. stream_get_endp (circuit->rcv_stream));
  1158. }
  1159. free_tlvs (&tlvs);
  1160. return retval;
  1161. }
  1162. /*
  1163. * Process Level 1/2 Link State
  1164. * ISO - 10589
  1165. * Section 7.3.15.1 - Action on receipt of a link state PDU
  1166. */
  1167. static int
  1168. process_lsp (int level, struct isis_circuit *circuit, const u_char *ssnpa)
  1169. {
  1170. struct isis_link_state_hdr *hdr;
  1171. struct isis_adjacency *adj = NULL;
  1172. struct isis_lsp *lsp, *lsp0 = NULL;
  1173. int retval = ISIS_OK, comp = 0;
  1174. u_char lspid[ISIS_SYS_ID_LEN + 2];
  1175. struct isis_passwd *passwd;
  1176. uint16_t pdu_len;
  1177. int lsp_confusion;
  1178. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  1179. {
  1180. zlog_debug ("ISIS-Upd (%s): Rcvd L%d LSP on %s, cirType %s, cirID %u",
  1181. circuit->area->area_tag, level, circuit->interface->name,
  1182. circuit_t2string (circuit->is_type), circuit->circuit_id);
  1183. if (isis->debugs & DEBUG_PACKET_DUMP)
  1184. zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
  1185. stream_get_endp (circuit->rcv_stream));
  1186. }
  1187. if ((stream_get_endp (circuit->rcv_stream) -
  1188. stream_get_getp (circuit->rcv_stream)) < ISIS_LSP_HDR_LEN)
  1189. {
  1190. zlog_warn ("Packet too short");
  1191. return ISIS_WARNING;
  1192. }
  1193. /* Reference the header */
  1194. hdr = (struct isis_link_state_hdr *) STREAM_PNT (circuit->rcv_stream);
  1195. pdu_len = ntohs (hdr->pdu_len);
  1196. /* lsp length check */
  1197. if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN) ||
  1198. pdu_len > ISO_MTU(circuit) ||
  1199. pdu_len > stream_get_endp (circuit->rcv_stream))
  1200. {
  1201. zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP length %d",
  1202. circuit->area->area_tag,
  1203. rawlspid_print (hdr->lsp_id), pdu_len);
  1204. return ISIS_WARNING;
  1205. }
  1206. /*
  1207. * Set the stream endp to PDU length, ignoring additional padding
  1208. * introduced by transport chips.
  1209. */
  1210. if (pdu_len < stream_get_endp (circuit->rcv_stream))
  1211. stream_set_endp (circuit->rcv_stream, pdu_len);
  1212. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  1213. {
  1214. zlog_debug ("ISIS-Upd (%s): Rcvd L%d LSP %s, seq 0x%08x, cksum 0x%04x, "
  1215. "lifetime %us, len %u, on %s",
  1216. circuit->area->area_tag,
  1217. level,
  1218. rawlspid_print (hdr->lsp_id),
  1219. ntohl (hdr->seq_num),
  1220. ntohs (hdr->checksum),
  1221. ntohs (hdr->rem_lifetime),
  1222. pdu_len,
  1223. circuit->interface->name);
  1224. }
  1225. /* lsp is_type check */
  1226. if ((hdr->lsp_bits & IS_LEVEL_1_AND_2) != IS_LEVEL_1 &&
  1227. (hdr->lsp_bits & IS_LEVEL_1_AND_2) != IS_LEVEL_1_AND_2)
  1228. {
  1229. zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP is type %x",
  1230. circuit->area->area_tag,
  1231. rawlspid_print (hdr->lsp_id), hdr->lsp_bits);
  1232. /* continue as per RFC1122 Be liberal in what you accept, and
  1233. * conservative in what you send */
  1234. }
  1235. /* Checksum sanity check - FIXME: move to correct place */
  1236. /* 12 = sysid+pdu+remtime */
  1237. if (iso_csum_verify (STREAM_PNT (circuit->rcv_stream) + 4,
  1238. pdu_len - 12, &hdr->checksum))
  1239. {
  1240. zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP checksum 0x%04x",
  1241. circuit->area->area_tag,
  1242. rawlspid_print (hdr->lsp_id), ntohs (hdr->checksum));
  1243. return ISIS_WARNING;
  1244. }
  1245. /* 7.3.15.1 a) 1 - external domain circuit will discard lsps */
  1246. if (circuit->ext_domain)
  1247. {
  1248. zlog_debug
  1249. ("ISIS-Upd (%s): LSP %s received at level %d over circuit with "
  1250. "externalDomain = true", circuit->area->area_tag,
  1251. rawlspid_print (hdr->lsp_id), level);
  1252. return ISIS_WARNING;
  1253. }
  1254. /* 7.3.15.1 a) 2,3 - manualL2OnlyMode not implemented */
  1255. if (!accept_level (level, circuit->is_type))
  1256. {
  1257. zlog_debug ("ISIS-Upd (%s): LSP %s received at level %d over circuit of"
  1258. " type %s",
  1259. circuit->area->area_tag,
  1260. rawlspid_print (hdr->lsp_id),
  1261. level, circuit_t2string (circuit->is_type));
  1262. return ISIS_WARNING;
  1263. }
  1264. /* 7.3.15.1 a) 4 - need to make sure IDLength matches */
  1265. /* 7.3.15.1 a) 5 - maximum area match, can be ommited since we only use 3 */
  1266. /* 7.3.15.1 a) 7 - password check */
  1267. (level == IS_LEVEL_1) ? (passwd = &circuit->area->area_passwd) :
  1268. (passwd = &circuit->area->domain_passwd);
  1269. if (passwd->type)
  1270. {
  1271. if (lsp_authentication_check (circuit->rcv_stream, circuit->area,
  1272. level, passwd))
  1273. {
  1274. isis_event_auth_failure (circuit->area->area_tag,
  1275. "LSP authentication failure", hdr->lsp_id);
  1276. return ISIS_WARNING;
  1277. }
  1278. }
  1279. /* Find the LSP in our database and compare it to this Link State header */
  1280. lsp = lsp_search (hdr->lsp_id, circuit->area->lspdb[level - 1]);
  1281. if (lsp)
  1282. comp = lsp_compare (circuit->area->area_tag, lsp, hdr->seq_num,
  1283. hdr->checksum, hdr->rem_lifetime);
  1284. if (lsp && (lsp->own_lsp
  1285. #ifdef TOPOLOGY_GENERATE
  1286. || lsp->from_topology
  1287. #endif /* TOPOLOGY_GENERATE */
  1288. ))
  1289. goto dontcheckadj;
  1290. /* 7.3.15.1 a) 6 - Must check that we have an adjacency of the same level */
  1291. /* for broadcast circuits, snpa should be compared */
  1292. if (circuit->circ_type == CIRCUIT_T_BROADCAST)
  1293. {
  1294. adj = isis_adj_lookup_snpa (ssnpa, circuit->u.bc.adjdb[level - 1]);
  1295. if (!adj)
  1296. {
  1297. zlog_debug ("(%s): DS ======= LSP %s, seq 0x%08x, cksum 0x%04x, "
  1298. "lifetime %us on %s",
  1299. circuit->area->area_tag,
  1300. rawlspid_print (hdr->lsp_id),
  1301. ntohl (hdr->seq_num),
  1302. ntohs (hdr->checksum),
  1303. ntohs (hdr->rem_lifetime), circuit->interface->name);
  1304. return ISIS_WARNING; /* Silently discard */
  1305. }
  1306. }
  1307. /* for non broadcast, we just need to find same level adj */
  1308. else
  1309. {
  1310. /* If no adj, or no sharing of level */
  1311. if (!circuit->u.p2p.neighbor)
  1312. {
  1313. return ISIS_OK; /* Silently discard */
  1314. }
  1315. else
  1316. {
  1317. if (((level == IS_LEVEL_1) &&
  1318. (circuit->u.p2p.neighbor->adj_usage == ISIS_ADJ_LEVEL2)) ||
  1319. ((level == IS_LEVEL_2) &&
  1320. (circuit->u.p2p.neighbor->adj_usage == ISIS_ADJ_LEVEL1)))
  1321. return ISIS_WARNING; /* Silently discard */
  1322. adj = circuit->u.p2p.neighbor;
  1323. }
  1324. }
  1325. dontcheckadj:
  1326. /* 7.3.15.1 a) 7 - Passwords for level 1 - not implemented */
  1327. /* 7.3.15.1 a) 8 - Passwords for level 2 - not implemented */
  1328. /* 7.3.15.1 a) 9 - OriginatingLSPBufferSize - not implemented FIXME: do it */
  1329. /* 7.3.16.2 - If this is an LSP from another IS with identical seq_num but
  1330. * wrong checksum, initiate a purge. */
  1331. if (lsp
  1332. && (lsp->lsp_header->seq_num == hdr->seq_num)
  1333. && (lsp->lsp_header->checksum != hdr->checksum))
  1334. {
  1335. zlog_warn("ISIS-Upd (%s): LSP %s seq 0x%08x with confused checksum received.",
  1336. circuit->area->area_tag, rawlspid_print(hdr->lsp_id),
  1337. ntohl(hdr->seq_num));
  1338. hdr->rem_lifetime = 0;
  1339. lsp_confusion = 1;
  1340. }
  1341. else
  1342. lsp_confusion = 0;
  1343. /* 7.3.15.1 b) - If the remaining life time is 0, we perform 7.3.16.4 */
  1344. if (hdr->rem_lifetime == 0)
  1345. {
  1346. if (!lsp)
  1347. {
  1348. /* 7.3.16.4 a) 1) No LSP in db -> send an ack, but don't save */
  1349. /* only needed on explicit update, eg - p2p */
  1350. if (circuit->circ_type == CIRCUIT_T_P2P)
  1351. ack_lsp (hdr, circuit, level);
  1352. return retval; /* FIXME: do we need a purge? */
  1353. }
  1354. else
  1355. {
  1356. if (memcmp (hdr->lsp_id, isis->sysid, ISIS_SYS_ID_LEN))
  1357. {
  1358. /* LSP by some other system -> do 7.3.16.4 b) */
  1359. /* 7.3.16.4 b) 1) */
  1360. if (comp == LSP_NEWER)
  1361. {
  1362. lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
  1363. /* ii */
  1364. lsp_set_all_srmflags (lsp);
  1365. /* v */
  1366. ISIS_FLAGS_CLEAR_ALL (lsp->SSNflags); /* FIXME: OTHER than c */
  1367. /* For the case of lsp confusion, flood the purge back to its
  1368. * originator so that it can react. Otherwise, don't reflood
  1369. * through incoming circuit as usual */
  1370. if (!lsp_confusion)
  1371. {
  1372. /* iii */
  1373. ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
  1374. /* iv */
  1375. if (circuit->circ_type != CIRCUIT_T_BROADCAST)
  1376. ISIS_SET_FLAG (lsp->SSNflags, circuit);
  1377. }
  1378. } /* 7.3.16.4 b) 2) */
  1379. else if (comp == LSP_EQUAL)
  1380. {
  1381. /* i */
  1382. ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
  1383. /* ii */
  1384. if (circuit->circ_type != CIRCUIT_T_BROADCAST)
  1385. ISIS_SET_FLAG (lsp->SSNflags, circuit);
  1386. } /* 7.3.16.4 b) 3) */
  1387. else
  1388. {
  1389. ISIS_SET_FLAG (lsp->SRMflags, circuit);
  1390. ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
  1391. }
  1392. }
  1393. else if (lsp->lsp_header->rem_lifetime != 0)
  1394. {
  1395. /* our own LSP -> 7.3.16.4 c) */
  1396. if (comp == LSP_NEWER)
  1397. {
  1398. lsp_inc_seqnum (lsp, ntohl (hdr->seq_num));
  1399. lsp_set_all_srmflags (lsp);
  1400. }
  1401. else
  1402. {
  1403. ISIS_SET_FLAG (lsp->SRMflags, circuit);
  1404. ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
  1405. }
  1406. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  1407. zlog_debug ("ISIS-Upd (%s): (1) re-originating LSP %s new "
  1408. "seq 0x%08x", circuit->area->area_tag,
  1409. rawlspid_print (hdr->lsp_id),
  1410. ntohl (lsp->lsp_header->seq_num));
  1411. }
  1412. }
  1413. return retval;
  1414. }
  1415. /* 7.3.15.1 c) - If this is our own lsp and we don't have it initiate a
  1416. * purge */
  1417. if (memcmp (hdr->lsp_id, isis->sysid, ISIS_SYS_ID_LEN) == 0)
  1418. {
  1419. if (!lsp)
  1420. {
  1421. /* 7.3.16.4: initiate a purge */
  1422. lsp_purge_non_exist(level, hdr, circuit->area);
  1423. return ISIS_OK;
  1424. }
  1425. /* 7.3.15.1 d) - If this is our own lsp and we have it */
  1426. /* In 7.3.16.1, If an Intermediate system R somewhere in the domain
  1427. * has information that the current sequence number for source S is
  1428. * "greater" than that held by S, ... */
  1429. if (ntohl (hdr->seq_num) > ntohl (lsp->lsp_header->seq_num))
  1430. {
  1431. /* 7.3.16.1 */
  1432. lsp_inc_seqnum (lsp, ntohl (hdr->seq_num));
  1433. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  1434. zlog_debug ("ISIS-Upd (%s): (2) re-originating LSP %s new seq "
  1435. "0x%08x", circuit->area->area_tag,
  1436. rawlspid_print (hdr->lsp_id),
  1437. ntohl (lsp->lsp_header->seq_num));
  1438. }
  1439. /* If the received LSP is older or equal,
  1440. * resend the LSP which will act as ACK */
  1441. lsp_set_all_srmflags (lsp);
  1442. }
  1443. else
  1444. {
  1445. /* 7.3.15.1 e) - This lsp originated on another system */
  1446. /* 7.3.15.1 e) 1) LSP newer than the one in db or no LSP in db */
  1447. if ((!lsp || comp == LSP_NEWER))
  1448. {
  1449. /*
  1450. * If this lsp is a frag, need to see if we have zero lsp present
  1451. */
  1452. if (LSP_FRAGMENT (hdr->lsp_id) != 0)
  1453. {
  1454. memcpy (lspid, hdr->lsp_id, ISIS_SYS_ID_LEN + 1);
  1455. LSP_FRAGMENT (lspid) = 0;
  1456. lsp0 = lsp_search (lspid, circuit->area->lspdb[level - 1]);
  1457. if (!lsp0)
  1458. {
  1459. zlog_debug ("Got lsp frag, while zero lsp not in database");
  1460. return ISIS_OK;
  1461. }
  1462. }
  1463. /* i */
  1464. if (!lsp)
  1465. {
  1466. lsp = lsp_new_from_stream_ptr (circuit->rcv_stream,
  1467. pdu_len, lsp0,
  1468. circuit->area, level);
  1469. lsp_insert (lsp, circuit->area->lspdb[level - 1]);
  1470. }
  1471. else /* exists, so we overwrite */
  1472. {
  1473. lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
  1474. }
  1475. /* ii */
  1476. lsp_set_all_srmflags (lsp);
  1477. /* iii */
  1478. ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
  1479. /* iv */
  1480. if (circuit->circ_type != CIRCUIT_T_BROADCAST)
  1481. ISIS_SET_FLAG (lsp->SSNflags, circuit);
  1482. /* FIXME: v) */
  1483. }
  1484. /* 7.3.15.1 e) 2) LSP equal to the one in db */
  1485. else if (comp == LSP_EQUAL)
  1486. {
  1487. ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
  1488. lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
  1489. if (circuit->circ_type != CIRCUIT_T_BROADCAST)
  1490. ISIS_SET_FLAG (lsp->SSNflags, circuit);
  1491. }
  1492. /* 7.3.15.1 e) 3) LSP older than the one in db */
  1493. else
  1494. {
  1495. ISIS_SET_FLAG (lsp->SRMflags, circuit);
  1496. ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
  1497. }
  1498. }
  1499. return retval;
  1500. }
  1501. /*
  1502. * Process Sequence Numbers
  1503. * ISO - 10589
  1504. * Section 7.3.15.2 - Action on receipt of a sequence numbers PDU
  1505. */
  1506. static int
  1507. process_snp (int snp_type, int level, struct isis_circuit *circuit,
  1508. const u_char *ssnpa)
  1509. {
  1510. int retval = ISIS_OK;
  1511. int cmp, own_lsp;
  1512. char typechar = ' ';
  1513. uint16_t pdu_len;
  1514. struct isis_adjacency *adj;
  1515. struct isis_complete_seqnum_hdr *chdr = NULL;
  1516. struct isis_partial_seqnum_hdr *phdr = NULL;
  1517. uint32_t found = 0, expected = 0, auth_tlv_offset = 0;
  1518. struct isis_lsp *lsp;
  1519. struct lsp_entry *entry;
  1520. struct listnode *node, *nnode;
  1521. struct listnode *node2, *nnode2;
  1522. struct tlvs tlvs;
  1523. struct list *lsp_list = NULL;
  1524. struct isis_passwd *passwd;
  1525. if (snp_type == ISIS_SNP_CSNP_FLAG)
  1526. {
  1527. /* getting the header info */
  1528. typechar = 'C';
  1529. chdr =
  1530. (struct isis_complete_seqnum_hdr *) STREAM_PNT (circuit->rcv_stream);
  1531. stream_forward_getp (circuit->rcv_stream, ISIS_CSNP_HDRLEN);
  1532. pdu_len = ntohs (chdr->pdu_len);
  1533. if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_CSNP_HDRLEN) ||
  1534. pdu_len > ISO_MTU(circuit) ||
  1535. pdu_len > stream_get_endp (circuit->rcv_stream))
  1536. {
  1537. zlog_warn ("Received a CSNP with bogus length %d", pdu_len);
  1538. return ISIS_WARNING;
  1539. }
  1540. }
  1541. else
  1542. {
  1543. typechar = 'P';
  1544. phdr =
  1545. (struct isis_partial_seqnum_hdr *) STREAM_PNT (circuit->rcv_stream);
  1546. stream_forward_getp (circuit->rcv_stream, ISIS_PSNP_HDRLEN);
  1547. pdu_len = ntohs (phdr->pdu_len);
  1548. if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_PSNP_HDRLEN) ||
  1549. pdu_len > ISO_MTU(circuit) ||
  1550. pdu_len > stream_get_endp (circuit->rcv_stream))
  1551. {
  1552. zlog_warn ("Received a PSNP with bogus length %d", pdu_len);
  1553. return ISIS_WARNING;
  1554. }
  1555. }
  1556. /*
  1557. * Set the stream endp to PDU length, ignoring additional padding
  1558. * introduced by transport chips.
  1559. */
  1560. if (pdu_len < stream_get_endp (circuit->rcv_stream))
  1561. stream_set_endp (circuit->rcv_stream, pdu_len);
  1562. /* 7.3.15.2 a) 1 - external domain circuit will discard snp pdu */
  1563. if (circuit->ext_domain)
  1564. {
  1565. zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
  1566. "skipping: circuit externalDomain = true",
  1567. circuit->area->area_tag,
  1568. level, typechar, circuit->interface->name);
  1569. return ISIS_OK;
  1570. }
  1571. /* 7.3.15.2 a) 2,3 - manualL2OnlyMode not implemented */
  1572. if (!accept_level (level, circuit->is_type))
  1573. {
  1574. zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
  1575. "skipping: circuit type %s does not match level %d",
  1576. circuit->area->area_tag,
  1577. level,
  1578. typechar,
  1579. circuit->interface->name,
  1580. circuit_t2string (circuit->is_type), level);
  1581. return ISIS_OK;
  1582. }
  1583. /* 7.3.15.2 a) 4 - not applicable for CSNP only PSNPs on broadcast */
  1584. if ((snp_type == ISIS_SNP_PSNP_FLAG) &&
  1585. (circuit->circ_type == CIRCUIT_T_BROADCAST) &&
  1586. (!circuit->u.bc.is_dr[level - 1]))
  1587. {
  1588. zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s, "
  1589. "skipping: we are not the DIS",
  1590. circuit->area->area_tag,
  1591. level,
  1592. typechar, snpa_print (ssnpa), circuit->interface->name);
  1593. return ISIS_OK;
  1594. }
  1595. /* 7.3.15.2 a) 5 - need to make sure IDLength matches - already checked */
  1596. /* 7.3.15.2 a) 6 - maximum area match, can be ommited since we only use 3
  1597. * - already checked */
  1598. /* 7.3.15.2 a) 7 - Must check that we have an adjacency of the same level */
  1599. /* for broadcast circuits, snpa should be compared */
  1600. /* FIXME : Do we need to check SNPA? */
  1601. if (circuit->circ_type == CIRCUIT_T_BROADCAST)
  1602. {
  1603. if (snp_type == ISIS_SNP_CSNP_FLAG)
  1604. {
  1605. adj =
  1606. isis_adj_lookup (chdr->source_id, circuit->u.bc.adjdb[level - 1]);
  1607. }
  1608. else
  1609. {
  1610. /* a psnp on a broadcast, how lovely of Juniper :) */
  1611. adj =
  1612. isis_adj_lookup (phdr->source_id, circuit->u.bc.adjdb[level - 1]);
  1613. }
  1614. if (!adj)
  1615. return ISIS_OK; /* Silently discard */
  1616. }
  1617. else
  1618. {
  1619. if (!circuit->u.p2p.neighbor)
  1620. {
  1621. zlog_warn ("no p2p neighbor on circuit %s", circuit->interface->name);
  1622. return ISIS_OK; /* Silently discard */
  1623. }
  1624. }
  1625. /* 7.3.15.2 a) 8 - Passwords for level 1 - not implemented */
  1626. /* 7.3.15.2 a) 9 - Passwords for level 2 - not implemented */
  1627. memset (&tlvs, 0, sizeof (struct tlvs));
  1628. /* parse the SNP */
  1629. expected |= TLVFLAG_LSP_ENTRIES;
  1630. expected |= TLVFLAG_AUTH_INFO;
  1631. auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
  1632. retval = parse_tlvs (circuit->area->area_tag,
  1633. STREAM_PNT (circuit->rcv_stream),
  1634. pdu_len - stream_get_getp (circuit->rcv_stream),
  1635. &expected, &found, &tlvs, &auth_tlv_offset);
  1636. if (retval > ISIS_WARNING)
  1637. {
  1638. zlog_warn ("something went very wrong processing SNP");
  1639. free_tlvs (&tlvs);
  1640. return retval;
  1641. }
  1642. if (level == IS_LEVEL_1)
  1643. passwd = &circuit->area->area_passwd;
  1644. else
  1645. passwd = &circuit->area->domain_passwd;
  1646. if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_RECV))
  1647. {
  1648. if (passwd->type)
  1649. {
  1650. if (!(found & TLVFLAG_AUTH_INFO) ||
  1651. authentication_check (&tlvs.auth_info, passwd,
  1652. circuit->rcv_stream, auth_tlv_offset))
  1653. {
  1654. isis_event_auth_failure (circuit->area->area_tag,
  1655. "SNP authentication" " failure",
  1656. phdr ? phdr->source_id :
  1657. chdr->source_id);
  1658. free_tlvs (&tlvs);
  1659. return ISIS_OK;
  1660. }
  1661. }
  1662. }
  1663. /* debug isis snp-packets */
  1664. if (isis->debugs & DEBUG_SNP_PACKETS)
  1665. {
  1666. zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s",
  1667. circuit->area->area_tag,
  1668. level,
  1669. typechar, snpa_print (ssnpa), circuit->interface->name);
  1670. if (tlvs.lsp_entries)
  1671. {
  1672. for (ALL_LIST_ELEMENTS_RO (tlvs.lsp_entries, node, entry))
  1673. {
  1674. zlog_debug ("ISIS-Snp (%s): %cSNP entry %s, seq 0x%08x,"
  1675. " cksum 0x%04x, lifetime %us",
  1676. circuit->area->area_tag,
  1677. typechar,
  1678. rawlspid_print (entry->lsp_id),
  1679. ntohl (entry->seq_num),
  1680. ntohs (entry->checksum), ntohs (entry->rem_lifetime));
  1681. }
  1682. }
  1683. }
  1684. /* 7.3.15.2 b) Actions on LSP_ENTRIES reported */
  1685. if (tlvs.lsp_entries)
  1686. {
  1687. for (ALL_LIST_ELEMENTS_RO (tlvs.lsp_entries, node, entry))
  1688. {
  1689. lsp = lsp_search (entry->lsp_id, circuit->area->lspdb[level - 1]);
  1690. own_lsp = !memcmp (entry->lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
  1691. if (lsp)
  1692. {
  1693. /* 7.3.15.2 b) 1) is this LSP newer */
  1694. cmp = lsp_compare (circuit->area->area_tag, lsp, entry->seq_num,
  1695. entry->checksum, entry->rem_lifetime);
  1696. /* 7.3.15.2 b) 2) if it equals, clear SRM on p2p */
  1697. if (cmp == LSP_EQUAL)
  1698. {
  1699. /* if (circuit->circ_type != CIRCUIT_T_BROADCAST) */
  1700. ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
  1701. }
  1702. /* 7.3.15.2 b) 3) if it is older, clear SSN and set SRM */
  1703. else if (cmp == LSP_OLDER)
  1704. {
  1705. ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
  1706. ISIS_SET_FLAG (lsp->SRMflags, circuit);
  1707. }
  1708. /* 7.3.15.2 b) 4) if it is newer, set SSN and clear SRM on p2p */
  1709. else
  1710. {
  1711. if (own_lsp)
  1712. {
  1713. lsp_inc_seqnum (lsp, ntohl (entry->seq_num));
  1714. ISIS_SET_FLAG (lsp->SRMflags, circuit);
  1715. }
  1716. else
  1717. {
  1718. ISIS_SET_FLAG (lsp->SSNflags, circuit);
  1719. /* if (circuit->circ_type != CIRCUIT_T_BROADCAST) */
  1720. ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
  1721. }
  1722. }
  1723. }
  1724. else
  1725. {
  1726. /* 7.3.15.2 b) 5) if it was not found, and all of those are not 0,
  1727. * insert it and set SSN on it */
  1728. if (entry->rem_lifetime && entry->checksum && entry->seq_num &&
  1729. memcmp (entry->lsp_id, isis->sysid, ISIS_SYS_ID_LEN))
  1730. {
  1731. lsp = lsp_new(circuit->area, entry->lsp_id,
  1732. ntohs(entry->rem_lifetime),
  1733. 0, 0, entry->checksum, level);
  1734. lsp_insert (lsp, circuit->area->lspdb[level - 1]);
  1735. ISIS_FLAGS_CLEAR_ALL (lsp->SRMflags);
  1736. ISIS_SET_FLAG (lsp->SSNflags, circuit);
  1737. }
  1738. }
  1739. }
  1740. }
  1741. /* 7.3.15.2 c) on CSNP set SRM for all in range which were not reported */
  1742. if (snp_type == ISIS_SNP_CSNP_FLAG)
  1743. {
  1744. /*
  1745. * Build a list from our own LSP db bounded with
  1746. * start_lsp_id and stop_lsp_id
  1747. */
  1748. lsp_list = list_new ();
  1749. lsp_build_list_nonzero_ht (chdr->start_lsp_id, chdr->stop_lsp_id,
  1750. lsp_list, circuit->area->lspdb[level - 1]);
  1751. /* Fixme: Find a better solution */
  1752. if (tlvs.lsp_entries)
  1753. {
  1754. for (ALL_LIST_ELEMENTS (tlvs.lsp_entries, node, nnode, entry))
  1755. {
  1756. for (ALL_LIST_ELEMENTS (lsp_list, node2, nnode2, lsp))
  1757. {
  1758. if (lsp_id_cmp (lsp->lsp_header->lsp_id, entry->lsp_id) == 0)
  1759. {
  1760. list_delete_node (lsp_list, node2);
  1761. break;
  1762. }
  1763. }
  1764. }
  1765. }
  1766. /* on remaining LSPs we set SRM (neighbor knew not of) */
  1767. for (ALL_LIST_ELEMENTS_RO (lsp_list, node, lsp))
  1768. ISIS_SET_FLAG (lsp->SRMflags, circuit);
  1769. /* lets free it */
  1770. list_delete (lsp_list);
  1771. }
  1772. free_tlvs (&tlvs);
  1773. return retval;
  1774. }
  1775. static int
  1776. process_csnp (int level, struct isis_circuit *circuit, const u_char *ssnpa)
  1777. {
  1778. if (isis->debugs & DEBUG_SNP_PACKETS)
  1779. {
  1780. zlog_debug ("ISIS-Snp (%s): Rcvd L%d CSNP on %s, cirType %s, cirID %u",
  1781. circuit->area->area_tag, level, circuit->interface->name,
  1782. circuit_t2string (circuit->is_type), circuit->circuit_id);
  1783. if (isis->debugs & DEBUG_PACKET_DUMP)
  1784. zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
  1785. stream_get_endp (circuit->rcv_stream));
  1786. }
  1787. /* Sanity check - FIXME: move to correct place */
  1788. if ((stream_get_endp (circuit->rcv_stream) -
  1789. stream_get_getp (circuit->rcv_stream)) < ISIS_CSNP_HDRLEN)
  1790. {
  1791. zlog_warn ("Packet too short ( < %d)", ISIS_CSNP_HDRLEN);
  1792. return ISIS_WARNING;
  1793. }
  1794. return process_snp (ISIS_SNP_CSNP_FLAG, level, circuit, ssnpa);
  1795. }
  1796. static int
  1797. process_psnp (int level, struct isis_circuit *circuit, const u_char *ssnpa)
  1798. {
  1799. if (isis->debugs & DEBUG_SNP_PACKETS)
  1800. {
  1801. zlog_debug ("ISIS-Snp (%s): Rcvd L%d PSNP on %s, cirType %s, cirID %u",
  1802. circuit->area->area_tag, level, circuit->interface->name,
  1803. circuit_t2string (circuit->is_type), circuit->circuit_id);
  1804. if (isis->debugs & DEBUG_PACKET_DUMP)
  1805. zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
  1806. stream_get_endp (circuit->rcv_stream));
  1807. }
  1808. if ((stream_get_endp (circuit->rcv_stream) -
  1809. stream_get_getp (circuit->rcv_stream)) < ISIS_PSNP_HDRLEN)
  1810. {
  1811. zlog_warn ("Packet too short ( < %d)", ISIS_PSNP_HDRLEN);
  1812. return ISIS_WARNING;
  1813. }
  1814. return process_snp (ISIS_SNP_PSNP_FLAG, level, circuit, ssnpa);
  1815. }
  1816. /*
  1817. * PDU Dispatcher
  1818. */
  1819. static int
  1820. isis_handle_pdu (struct isis_circuit *circuit, u_char * ssnpa)
  1821. {
  1822. struct isis_fixed_hdr *hdr;
  1823. int retval = ISIS_OK;
  1824. /*
  1825. * Let's first read data from stream to the header
  1826. */
  1827. hdr = (struct isis_fixed_hdr *) STREAM_DATA (circuit->rcv_stream);
  1828. if ((hdr->idrp != ISO10589_ISIS) && (hdr->idrp != ISO9542_ESIS))
  1829. {
  1830. zlog_err ("Not an IS-IS or ES-IS packet IDRP=%02x", hdr->idrp);
  1831. return ISIS_ERROR;
  1832. }
  1833. /* now we need to know if this is an ISO 9542 packet and
  1834. * take real good care of it, waaa!
  1835. */
  1836. if (hdr->idrp == ISO9542_ESIS)
  1837. {
  1838. zlog_err ("No support for ES-IS packet IDRP=%02x", hdr->idrp);
  1839. return ISIS_ERROR;
  1840. }
  1841. stream_set_getp (circuit->rcv_stream, ISIS_FIXED_HDR_LEN);
  1842. /*
  1843. * and then process it
  1844. */
  1845. if (hdr->length < ISIS_MINIMUM_FIXED_HDR_LEN)
  1846. {
  1847. zlog_err ("Fixed header length = %d", hdr->length);
  1848. return ISIS_ERROR;
  1849. }
  1850. if (hdr->version1 != 1)
  1851. {
  1852. zlog_warn ("Unsupported ISIS version %u", hdr->version1);
  1853. return ISIS_WARNING;
  1854. }
  1855. /* either 6 or 0 */
  1856. if ((hdr->id_len != 0) && (hdr->id_len != ISIS_SYS_ID_LEN))
  1857. {
  1858. zlog_err
  1859. ("IDFieldLengthMismatch: ID Length field in a received PDU %u, "
  1860. "while the parameter for this IS is %u", hdr->id_len,
  1861. ISIS_SYS_ID_LEN);
  1862. return ISIS_ERROR;
  1863. }
  1864. if (hdr->version2 != 1)
  1865. {
  1866. zlog_warn ("Unsupported ISIS version %u", hdr->version2);
  1867. return ISIS_WARNING;
  1868. }
  1869. if (circuit->is_passive)
  1870. {
  1871. zlog_warn ("Received ISIS PDU on passive circuit %s",
  1872. circuit->interface->name);
  1873. return ISIS_WARNING;
  1874. }
  1875. /* either 3 or 0 */
  1876. if ((hdr->max_area_addrs != 0)
  1877. && (hdr->max_area_addrs != isis->max_area_addrs))
  1878. {
  1879. zlog_err ("maximumAreaAddressesMismatch: maximumAreaAdresses in a "
  1880. "received PDU %u while the parameter for this IS is %u",
  1881. hdr->max_area_addrs, isis->max_area_addrs);
  1882. return ISIS_ERROR;
  1883. }
  1884. switch (hdr->pdu_type)
  1885. {
  1886. case L1_LAN_HELLO:
  1887. retval = process_lan_hello (ISIS_LEVEL1, circuit, ssnpa);
  1888. break;
  1889. case L2_LAN_HELLO:
  1890. retval = process_lan_hello (ISIS_LEVEL2, circuit, ssnpa);
  1891. break;
  1892. case P2P_HELLO:
  1893. retval = process_p2p_hello (circuit);
  1894. break;
  1895. case L1_LINK_STATE:
  1896. retval = process_lsp (ISIS_LEVEL1, circuit, ssnpa);
  1897. break;
  1898. case L2_LINK_STATE:
  1899. retval = process_lsp (ISIS_LEVEL2, circuit, ssnpa);
  1900. break;
  1901. case L1_COMPLETE_SEQ_NUM:
  1902. retval = process_csnp (ISIS_LEVEL1, circuit, ssnpa);
  1903. break;
  1904. case L2_COMPLETE_SEQ_NUM:
  1905. retval = process_csnp (ISIS_LEVEL2, circuit, ssnpa);
  1906. break;
  1907. case L1_PARTIAL_SEQ_NUM:
  1908. retval = process_psnp (ISIS_LEVEL1, circuit, ssnpa);
  1909. break;
  1910. case L2_PARTIAL_SEQ_NUM:
  1911. retval = process_psnp (ISIS_LEVEL2, circuit, ssnpa);
  1912. break;
  1913. default:
  1914. return ISIS_ERROR;
  1915. }
  1916. return retval;
  1917. }
  1918. #ifdef GNU_LINUX
  1919. int
  1920. isis_receive (struct thread *thread)
  1921. {
  1922. struct isis_circuit *circuit;
  1923. u_char ssnpa[ETH_ALEN];
  1924. int retval;
  1925. /*
  1926. * Get the circuit
  1927. */
  1928. circuit = THREAD_ARG (thread);
  1929. assert (circuit);
  1930. isis_circuit_stream(circuit, &circuit->rcv_stream);
  1931. retval = circuit->rx (circuit, ssnpa);
  1932. circuit->t_read = NULL;
  1933. if (retval == ISIS_OK)
  1934. retval = isis_handle_pdu (circuit, ssnpa);
  1935. /*
  1936. * prepare for next packet.
  1937. */
  1938. if (!circuit->is_passive)
  1939. {
  1940. THREAD_READ_ON (master, circuit->t_read, isis_receive, circuit,
  1941. circuit->fd);
  1942. }
  1943. return retval;
  1944. }
  1945. #else
  1946. int
  1947. isis_receive (struct thread *thread)
  1948. {
  1949. struct isis_circuit *circuit;
  1950. u_char ssnpa[ETH_ALEN];
  1951. int retval;
  1952. /*
  1953. * Get the circuit
  1954. */
  1955. circuit = THREAD_ARG (thread);
  1956. assert (circuit);
  1957. circuit->t_read = NULL;
  1958. isis_circuit_stream(circuit, &circuit->rcv_stream);
  1959. retval = circuit->rx (circuit, ssnpa);
  1960. if (retval == ISIS_OK)
  1961. retval = isis_handle_pdu (circuit, ssnpa);
  1962. /*
  1963. * prepare for next packet.
  1964. */
  1965. if (!circuit->is_passive)
  1966. {
  1967. circuit->t_read = thread_add_timer_msec (master, isis_receive, circuit,
  1968. listcount
  1969. (circuit->area->circuit_list) *
  1970. 100);
  1971. }
  1972. return retval;
  1973. }
  1974. #endif
  1975. /* filling of the fixed isis header */
  1976. void
  1977. fill_fixed_hdr (struct isis_fixed_hdr *hdr, u_char pdu_type)
  1978. {
  1979. memset (hdr, 0, sizeof (struct isis_fixed_hdr));
  1980. hdr->idrp = ISO10589_ISIS;
  1981. switch (pdu_type)
  1982. {
  1983. case L1_LAN_HELLO:
  1984. case L2_LAN_HELLO:
  1985. hdr->length = ISIS_LANHELLO_HDRLEN;
  1986. break;
  1987. case P2P_HELLO:
  1988. hdr->length = ISIS_P2PHELLO_HDRLEN;
  1989. break;
  1990. case L1_LINK_STATE:
  1991. case L2_LINK_STATE:
  1992. hdr->length = ISIS_LSP_HDR_LEN;
  1993. break;
  1994. case L1_COMPLETE_SEQ_NUM:
  1995. case L2_COMPLETE_SEQ_NUM:
  1996. hdr->length = ISIS_CSNP_HDRLEN;
  1997. break;
  1998. case L1_PARTIAL_SEQ_NUM:
  1999. case L2_PARTIAL_SEQ_NUM:
  2000. hdr->length = ISIS_PSNP_HDRLEN;
  2001. break;
  2002. default:
  2003. zlog_warn ("fill_fixed_hdr(): unknown pdu type %d", pdu_type);
  2004. return;
  2005. }
  2006. hdr->length += ISIS_FIXED_HDR_LEN;
  2007. hdr->pdu_type = pdu_type;
  2008. hdr->version1 = 1;
  2009. hdr->id_len = 0; /* ISIS_SYS_ID_LEN - 0==6 */
  2010. hdr->version2 = 1;
  2011. hdr->max_area_addrs = 0; /* isis->max_area_addrs - 0==3 */
  2012. }
  2013. /*
  2014. * SEND SIDE
  2015. */
  2016. static void
  2017. fill_fixed_hdr_andstream (struct isis_fixed_hdr *hdr, u_char pdu_type,
  2018. struct stream *stream)
  2019. {
  2020. fill_fixed_hdr (hdr, pdu_type);
  2021. stream_putc (stream, hdr->idrp);
  2022. stream_putc (stream, hdr->length);
  2023. stream_putc (stream, hdr->version1);
  2024. stream_putc (stream, hdr->id_len);
  2025. stream_putc (stream, hdr->pdu_type);
  2026. stream_putc (stream, hdr->version2);
  2027. stream_putc (stream, hdr->reserved);
  2028. stream_putc (stream, hdr->max_area_addrs);
  2029. return;
  2030. }
  2031. int
  2032. send_hello (struct isis_circuit *circuit, int level)
  2033. {
  2034. struct isis_fixed_hdr fixed_hdr;
  2035. struct isis_lan_hello_hdr hello_hdr;
  2036. struct isis_p2p_hello_hdr p2p_hello_hdr;
  2037. unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
  2038. size_t len_pointer, length, auth_tlv_offset = 0;
  2039. u_int32_t interval;
  2040. int retval;
  2041. if (circuit->is_passive)
  2042. return ISIS_OK;
  2043. if (circuit->interface->mtu == 0)
  2044. {
  2045. zlog_warn ("circuit has zero MTU");
  2046. return ISIS_WARNING;
  2047. }
  2048. isis_circuit_stream(circuit, &circuit->snd_stream);
  2049. if (circuit->circ_type == CIRCUIT_T_BROADCAST)
  2050. if (level == IS_LEVEL_1)
  2051. fill_fixed_hdr_andstream (&fixed_hdr, L1_LAN_HELLO,
  2052. circuit->snd_stream);
  2053. else
  2054. fill_fixed_hdr_andstream (&fixed_hdr, L2_LAN_HELLO,
  2055. circuit->snd_stream);
  2056. else
  2057. fill_fixed_hdr_andstream (&fixed_hdr, P2P_HELLO, circuit->snd_stream);
  2058. /*
  2059. * Fill LAN Level 1 or 2 Hello PDU header
  2060. */
  2061. memset (&hello_hdr, 0, sizeof (struct isis_lan_hello_hdr));
  2062. interval = circuit->hello_multiplier[level - 1] *
  2063. circuit->hello_interval[level - 1];
  2064. if (interval > USHRT_MAX)
  2065. interval = USHRT_MAX;
  2066. hello_hdr.circuit_t = circuit->is_type;
  2067. memcpy (hello_hdr.source_id, isis->sysid, ISIS_SYS_ID_LEN);
  2068. hello_hdr.hold_time = htons ((u_int16_t) interval);
  2069. hello_hdr.pdu_len = 0; /* Update the PDU Length later */
  2070. len_pointer = stream_get_endp (circuit->snd_stream) + 3 + ISIS_SYS_ID_LEN;
  2071. /* copy the shared part of the hello to the p2p hello if needed */
  2072. if (circuit->circ_type == CIRCUIT_T_P2P)
  2073. {
  2074. memcpy (&p2p_hello_hdr, &hello_hdr, 5 + ISIS_SYS_ID_LEN);
  2075. p2p_hello_hdr.local_id = circuit->circuit_id;
  2076. /* FIXME: need better understanding */
  2077. stream_put (circuit->snd_stream, &p2p_hello_hdr, ISIS_P2PHELLO_HDRLEN);
  2078. }
  2079. else
  2080. {
  2081. hello_hdr.prio = circuit->priority[level - 1];
  2082. if (level == IS_LEVEL_1)
  2083. {
  2084. memcpy (hello_hdr.lan_id, circuit->u.bc.l1_desig_is,
  2085. ISIS_SYS_ID_LEN + 1);
  2086. }
  2087. else if (level == IS_LEVEL_2)
  2088. {
  2089. memcpy (hello_hdr.lan_id, circuit->u.bc.l2_desig_is,
  2090. ISIS_SYS_ID_LEN + 1);
  2091. }
  2092. stream_put (circuit->snd_stream, &hello_hdr, ISIS_LANHELLO_HDRLEN);
  2093. }
  2094. /*
  2095. * Then the variable length part.
  2096. */
  2097. /* add circuit password */
  2098. switch (circuit->passwd.type)
  2099. {
  2100. /* Cleartext */
  2101. case ISIS_PASSWD_TYPE_CLEARTXT:
  2102. if (tlv_add_authinfo (circuit->passwd.type, circuit->passwd.len,
  2103. circuit->passwd.passwd, circuit->snd_stream))
  2104. return ISIS_WARNING;
  2105. break;
  2106. /* HMAC MD5 */
  2107. case ISIS_PASSWD_TYPE_HMAC_MD5:
  2108. /* Remember where TLV is written so we can later overwrite the MD5 hash */
  2109. auth_tlv_offset = stream_get_endp (circuit->snd_stream);
  2110. memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
  2111. if (tlv_add_authinfo (circuit->passwd.type, ISIS_AUTH_MD5_SIZE,
  2112. hmac_md5_hash, circuit->snd_stream))
  2113. return ISIS_WARNING;
  2114. break;
  2115. default:
  2116. break;
  2117. }
  2118. /* Area Addresses TLV */
  2119. if (listcount (circuit->area->area_addrs) == 0)
  2120. return ISIS_WARNING;
  2121. if (tlv_add_area_addrs (circuit->area->area_addrs, circuit->snd_stream))
  2122. return ISIS_WARNING;
  2123. /* LAN Neighbors TLV */
  2124. if (circuit->circ_type == CIRCUIT_T_BROADCAST)
  2125. {
  2126. if (level == IS_LEVEL_1 && circuit->u.bc.lan_neighs[0] &&
  2127. listcount (circuit->u.bc.lan_neighs[0]) > 0)
  2128. if (tlv_add_lan_neighs (circuit->u.bc.lan_neighs[0],
  2129. circuit->snd_stream))
  2130. return ISIS_WARNING;
  2131. if (level == IS_LEVEL_2 && circuit->u.bc.lan_neighs[1] &&
  2132. listcount (circuit->u.bc.lan_neighs[1]) > 0)
  2133. if (tlv_add_lan_neighs (circuit->u.bc.lan_neighs[1],
  2134. circuit->snd_stream))
  2135. return ISIS_WARNING;
  2136. }
  2137. /* Protocols Supported TLV */
  2138. if (circuit->nlpids.count > 0)
  2139. if (tlv_add_nlpid (&circuit->nlpids, circuit->snd_stream))
  2140. return ISIS_WARNING;
  2141. /* IP interface Address TLV */
  2142. if (circuit->ip_router && circuit->ip_addrs &&
  2143. listcount (circuit->ip_addrs) > 0)
  2144. if (tlv_add_ip_addrs (circuit->ip_addrs, circuit->snd_stream))
  2145. return ISIS_WARNING;
  2146. #ifdef HAVE_IPV6
  2147. /* IPv6 Interface Address TLV */
  2148. if (circuit->ipv6_router && circuit->ipv6_link &&
  2149. listcount (circuit->ipv6_link) > 0)
  2150. if (tlv_add_ipv6_addrs (circuit->ipv6_link, circuit->snd_stream))
  2151. return ISIS_WARNING;
  2152. #endif /* HAVE_IPV6 */
  2153. if (circuit->pad_hellos)
  2154. if (tlv_add_padding (circuit->snd_stream))
  2155. return ISIS_WARNING;
  2156. length = stream_get_endp (circuit->snd_stream);
  2157. /* Update PDU length */
  2158. stream_putw_at (circuit->snd_stream, len_pointer, (u_int16_t) length);
  2159. /* For HMAC MD5 we need to compute the md5 hash and store it */
  2160. if (circuit->passwd.type == ISIS_PASSWD_TYPE_HMAC_MD5)
  2161. {
  2162. hmac_md5 (STREAM_DATA (circuit->snd_stream),
  2163. stream_get_endp (circuit->snd_stream),
  2164. (unsigned char *) &circuit->passwd.passwd, circuit->passwd.len,
  2165. (unsigned char *) &hmac_md5_hash);
  2166. /* Copy the hash into the stream */
  2167. memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
  2168. hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
  2169. }
  2170. if (isis->debugs & DEBUG_ADJ_PACKETS)
  2171. {
  2172. if (circuit->circ_type == CIRCUIT_T_BROADCAST)
  2173. {
  2174. zlog_debug ("ISIS-Adj (%s): Sending L%d LAN IIH on %s, length %zd",
  2175. circuit->area->area_tag, level, circuit->interface->name,
  2176. length);
  2177. }
  2178. else
  2179. {
  2180. zlog_debug ("ISIS-Adj (%s): Sending P2P IIH on %s, length %zd",
  2181. circuit->area->area_tag, circuit->interface->name,
  2182. length);
  2183. }
  2184. if (isis->debugs & DEBUG_PACKET_DUMP)
  2185. zlog_dump_data (STREAM_DATA (circuit->snd_stream),
  2186. stream_get_endp (circuit->snd_stream));
  2187. }
  2188. retval = circuit->tx (circuit, level);
  2189. if (retval != ISIS_OK)
  2190. zlog_err ("ISIS-Adj (%s): Send L%d IIH on %s failed",
  2191. circuit->area->area_tag, level, circuit->interface->name);
  2192. return retval;
  2193. }
  2194. int
  2195. send_lan_l1_hello (struct thread *thread)
  2196. {
  2197. struct isis_circuit *circuit;
  2198. int retval;
  2199. circuit = THREAD_ARG (thread);
  2200. assert (circuit);
  2201. circuit->u.bc.t_send_lan_hello[0] = NULL;
  2202. if (!(circuit->area->is_type & IS_LEVEL_1))
  2203. {
  2204. zlog_warn ("ISIS-Hello (%s): Trying to send L1 IIH in L2-only area",
  2205. circuit->area->area_tag);
  2206. return 1;
  2207. }
  2208. if (circuit->u.bc.run_dr_elect[0])
  2209. retval = isis_dr_elect (circuit, 1);
  2210. retval = send_hello (circuit, 1);
  2211. /* set next timer thread */
  2212. THREAD_TIMER_ON (master, circuit->u.bc.t_send_lan_hello[0],
  2213. send_lan_l1_hello, circuit,
  2214. isis_jitter (circuit->hello_interval[0], IIH_JITTER));
  2215. return retval;
  2216. }
  2217. int
  2218. send_lan_l2_hello (struct thread *thread)
  2219. {
  2220. struct isis_circuit *circuit;
  2221. int retval;
  2222. circuit = THREAD_ARG (thread);
  2223. assert (circuit);
  2224. circuit->u.bc.t_send_lan_hello[1] = NULL;
  2225. if (!(circuit->area->is_type & IS_LEVEL_2))
  2226. {
  2227. zlog_warn ("ISIS-Hello (%s): Trying to send L2 IIH in L1 area",
  2228. circuit->area->area_tag);
  2229. return 1;
  2230. }
  2231. if (circuit->u.bc.run_dr_elect[1])
  2232. retval = isis_dr_elect (circuit, 2);
  2233. retval = send_hello (circuit, 2);
  2234. /* set next timer thread */
  2235. THREAD_TIMER_ON (master, circuit->u.bc.t_send_lan_hello[1],
  2236. send_lan_l2_hello, circuit,
  2237. isis_jitter (circuit->hello_interval[1], IIH_JITTER));
  2238. return retval;
  2239. }
  2240. int
  2241. send_p2p_hello (struct thread *thread)
  2242. {
  2243. struct isis_circuit *circuit;
  2244. circuit = THREAD_ARG (thread);
  2245. assert (circuit);
  2246. circuit->u.p2p.t_send_p2p_hello = NULL;
  2247. send_hello (circuit, 1);
  2248. /* set next timer thread */
  2249. THREAD_TIMER_ON (master, circuit->u.p2p.t_send_p2p_hello, send_p2p_hello,
  2250. circuit, isis_jitter (circuit->hello_interval[1],
  2251. IIH_JITTER));
  2252. return ISIS_OK;
  2253. }
  2254. static int
  2255. build_csnp (int level, u_char * start, u_char * stop, struct list *lsps,
  2256. struct isis_circuit *circuit)
  2257. {
  2258. struct isis_fixed_hdr fixed_hdr;
  2259. struct isis_passwd *passwd;
  2260. unsigned long lenp;
  2261. u_int16_t length;
  2262. unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
  2263. unsigned long auth_tlv_offset = 0;
  2264. int retval = ISIS_OK;
  2265. isis_circuit_stream(circuit, &circuit->snd_stream);
  2266. if (level == IS_LEVEL_1)
  2267. fill_fixed_hdr_andstream (&fixed_hdr, L1_COMPLETE_SEQ_NUM,
  2268. circuit->snd_stream);
  2269. else
  2270. fill_fixed_hdr_andstream (&fixed_hdr, L2_COMPLETE_SEQ_NUM,
  2271. circuit->snd_stream);
  2272. /*
  2273. * Fill Level 1 or 2 Complete Sequence Numbers header
  2274. */
  2275. lenp = stream_get_endp (circuit->snd_stream);
  2276. stream_putw (circuit->snd_stream, 0); /* PDU length - when we know it */
  2277. /* no need to send the source here, it is always us if we csnp */
  2278. stream_put (circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
  2279. /* with zero circuit id - ref 9.10, 9.11 */
  2280. stream_putc (circuit->snd_stream, 0x00);
  2281. stream_put (circuit->snd_stream, start, ISIS_SYS_ID_LEN + 2);
  2282. stream_put (circuit->snd_stream, stop, ISIS_SYS_ID_LEN + 2);
  2283. /*
  2284. * And TLVs
  2285. */
  2286. if (level == IS_LEVEL_1)
  2287. passwd = &circuit->area->area_passwd;
  2288. else
  2289. passwd = &circuit->area->domain_passwd;
  2290. if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
  2291. {
  2292. switch (passwd->type)
  2293. {
  2294. /* Cleartext */
  2295. case ISIS_PASSWD_TYPE_CLEARTXT:
  2296. if (tlv_add_authinfo (ISIS_PASSWD_TYPE_CLEARTXT, passwd->len,
  2297. passwd->passwd, circuit->snd_stream))
  2298. return ISIS_WARNING;
  2299. break;
  2300. /* HMAC MD5 */
  2301. case ISIS_PASSWD_TYPE_HMAC_MD5:
  2302. /* Remember where TLV is written so we can later overwrite the MD5 hash */
  2303. auth_tlv_offset = stream_get_endp (circuit->snd_stream);
  2304. memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
  2305. if (tlv_add_authinfo (ISIS_PASSWD_TYPE_HMAC_MD5, ISIS_AUTH_MD5_SIZE,
  2306. hmac_md5_hash, circuit->snd_stream))
  2307. return ISIS_WARNING;
  2308. break;
  2309. default:
  2310. break;
  2311. }
  2312. }
  2313. retval = tlv_add_lsp_entries (lsps, circuit->snd_stream);
  2314. if (retval != ISIS_OK)
  2315. return retval;
  2316. length = (u_int16_t) stream_get_endp (circuit->snd_stream);
  2317. /* Update PU length */
  2318. stream_putw_at (circuit->snd_stream, lenp, length);
  2319. /* For HMAC MD5 we need to compute the md5 hash and store it */
  2320. if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND) &&
  2321. passwd->type == ISIS_PASSWD_TYPE_HMAC_MD5)
  2322. {
  2323. hmac_md5 (STREAM_DATA (circuit->snd_stream),
  2324. stream_get_endp(circuit->snd_stream),
  2325. (unsigned char *) &passwd->passwd, passwd->len,
  2326. (unsigned char *) &hmac_md5_hash);
  2327. /* Copy the hash into the stream */
  2328. memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
  2329. hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
  2330. }
  2331. return retval;
  2332. }
  2333. /*
  2334. * Count the maximum number of lsps that can be accomodated by a given size.
  2335. */
  2336. static uint16_t
  2337. get_max_lsp_count (uint16_t size)
  2338. {
  2339. uint16_t tlv_count;
  2340. uint16_t lsp_count;
  2341. uint16_t remaining_size;
  2342. /* First count the full size TLVs */
  2343. tlv_count = size / MAX_LSP_ENTRIES_TLV_SIZE;
  2344. lsp_count = tlv_count * (MAX_LSP_ENTRIES_TLV_SIZE / LSP_ENTRIES_LEN);
  2345. /* The last TLV, if any */
  2346. remaining_size = size % MAX_LSP_ENTRIES_TLV_SIZE;
  2347. if (remaining_size - 2 >= LSP_ENTRIES_LEN)
  2348. lsp_count += (remaining_size - 2) / LSP_ENTRIES_LEN;
  2349. return lsp_count;
  2350. }
  2351. /*
  2352. * Calculate the length of Authentication Info. TLV.
  2353. */
  2354. static uint16_t
  2355. auth_tlv_length (int level, struct isis_circuit *circuit)
  2356. {
  2357. struct isis_passwd *passwd;
  2358. uint16_t length;
  2359. if (level == IS_LEVEL_1)
  2360. passwd = &circuit->area->area_passwd;
  2361. else
  2362. passwd = &circuit->area->domain_passwd;
  2363. /* Also include the length of TLV header */
  2364. length = AUTH_INFO_HDRLEN;
  2365. if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
  2366. {
  2367. switch (passwd->type)
  2368. {
  2369. /* Cleartext */
  2370. case ISIS_PASSWD_TYPE_CLEARTXT:
  2371. length += passwd->len;
  2372. break;
  2373. /* HMAC MD5 */
  2374. case ISIS_PASSWD_TYPE_HMAC_MD5:
  2375. length += ISIS_AUTH_MD5_SIZE;
  2376. break;
  2377. default:
  2378. break;
  2379. }
  2380. }
  2381. return length;
  2382. }
  2383. /*
  2384. * Calculate the maximum number of lsps that can be accomodated in a CSNP/PSNP.
  2385. */
  2386. static uint16_t
  2387. max_lsps_per_snp (int snp_type, int level, struct isis_circuit *circuit)
  2388. {
  2389. int snp_hdr_len;
  2390. int auth_tlv_len;
  2391. uint16_t lsp_count;
  2392. snp_hdr_len = ISIS_FIXED_HDR_LEN;
  2393. if (snp_type == ISIS_SNP_CSNP_FLAG)
  2394. snp_hdr_len += ISIS_CSNP_HDRLEN;
  2395. else
  2396. snp_hdr_len += ISIS_PSNP_HDRLEN;
  2397. auth_tlv_len = auth_tlv_length (level, circuit);
  2398. lsp_count = get_max_lsp_count (
  2399. stream_get_size (circuit->snd_stream) - snp_hdr_len - auth_tlv_len);
  2400. return lsp_count;
  2401. }
  2402. /*
  2403. * FIXME: support multiple CSNPs
  2404. */
  2405. int
  2406. send_csnp (struct isis_circuit *circuit, int level)
  2407. {
  2408. u_char start[ISIS_SYS_ID_LEN + 2];
  2409. u_char stop[ISIS_SYS_ID_LEN + 2];
  2410. struct list *list = NULL;
  2411. struct listnode *node;
  2412. struct isis_lsp *lsp;
  2413. u_char num_lsps, loop = 1;
  2414. int i, retval = ISIS_OK;
  2415. if (circuit->area->lspdb[level - 1] == NULL ||
  2416. dict_count (circuit->area->lspdb[level - 1]) == 0)
  2417. return retval;
  2418. memset (start, 0x00, ISIS_SYS_ID_LEN + 2);
  2419. memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
  2420. num_lsps = max_lsps_per_snp (ISIS_SNP_CSNP_FLAG, level, circuit);
  2421. while (loop)
  2422. {
  2423. list = list_new ();
  2424. lsp_build_list (start, stop, num_lsps, list,
  2425. circuit->area->lspdb[level - 1]);
  2426. /*
  2427. * Update the stop lsp_id before encoding this CSNP.
  2428. */
  2429. if (listcount (list) < num_lsps)
  2430. {
  2431. memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
  2432. }
  2433. else
  2434. {
  2435. node = listtail (list);
  2436. lsp = listgetdata (node);
  2437. memcpy (stop, lsp->lsp_header->lsp_id, ISIS_SYS_ID_LEN + 2);
  2438. }
  2439. retval = build_csnp (level, start, stop, list, circuit);
  2440. if (retval != ISIS_OK)
  2441. {
  2442. zlog_err ("ISIS-Snp (%s): Build L%d CSNP on %s failed",
  2443. circuit->area->area_tag, level, circuit->interface->name);
  2444. list_delete (list);
  2445. return retval;
  2446. }
  2447. if (isis->debugs & DEBUG_SNP_PACKETS)
  2448. {
  2449. zlog_debug ("ISIS-Snp (%s): Sending L%d CSNP on %s, length %zd",
  2450. circuit->area->area_tag, level, circuit->interface->name,
  2451. stream_get_endp (circuit->snd_stream));
  2452. for (ALL_LIST_ELEMENTS_RO (list, node, lsp))
  2453. {
  2454. zlog_debug ("ISIS-Snp (%s): CSNP entry %s, seq 0x%08x,"
  2455. " cksum 0x%04x, lifetime %us",
  2456. circuit->area->area_tag,
  2457. rawlspid_print (lsp->lsp_header->lsp_id),
  2458. ntohl (lsp->lsp_header->seq_num),
  2459. ntohs (lsp->lsp_header->checksum),
  2460. ntohs (lsp->lsp_header->rem_lifetime));
  2461. }
  2462. if (isis->debugs & DEBUG_PACKET_DUMP)
  2463. zlog_dump_data (STREAM_DATA (circuit->snd_stream),
  2464. stream_get_endp (circuit->snd_stream));
  2465. }
  2466. retval = circuit->tx (circuit, level);
  2467. if (retval != ISIS_OK)
  2468. {
  2469. zlog_err ("ISIS-Snp (%s): Send L%d CSNP on %s failed",
  2470. circuit->area->area_tag, level,
  2471. circuit->interface->name);
  2472. list_delete (list);
  2473. return retval;
  2474. }
  2475. /*
  2476. * Start lsp_id of the next CSNP should be one plus the
  2477. * stop lsp_id in this current CSNP.
  2478. */
  2479. memcpy (start, stop, ISIS_SYS_ID_LEN + 2);
  2480. loop = 0;
  2481. for (i = ISIS_SYS_ID_LEN + 1; i >= 0; --i)
  2482. {
  2483. if (start[i] < (u_char)0xff)
  2484. {
  2485. start[i] += 1;
  2486. loop = 1;
  2487. break;
  2488. }
  2489. }
  2490. memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
  2491. list_delete (list);
  2492. }
  2493. return retval;
  2494. }
  2495. int
  2496. send_l1_csnp (struct thread *thread)
  2497. {
  2498. struct isis_circuit *circuit;
  2499. int retval = ISIS_OK;
  2500. circuit = THREAD_ARG (thread);
  2501. assert (circuit);
  2502. circuit->t_send_csnp[0] = NULL;
  2503. if (circuit->circ_type == CIRCUIT_T_BROADCAST && circuit->u.bc.is_dr[0])
  2504. {
  2505. send_csnp (circuit, 1);
  2506. }
  2507. /* set next timer thread */
  2508. THREAD_TIMER_ON (master, circuit->t_send_csnp[0], send_l1_csnp, circuit,
  2509. isis_jitter (circuit->csnp_interval[0], CSNP_JITTER));
  2510. return retval;
  2511. }
  2512. int
  2513. send_l2_csnp (struct thread *thread)
  2514. {
  2515. struct isis_circuit *circuit;
  2516. int retval = ISIS_OK;
  2517. circuit = THREAD_ARG (thread);
  2518. assert (circuit);
  2519. circuit->t_send_csnp[1] = NULL;
  2520. if (circuit->circ_type == CIRCUIT_T_BROADCAST && circuit->u.bc.is_dr[1])
  2521. {
  2522. send_csnp (circuit, 2);
  2523. }
  2524. /* set next timer thread */
  2525. THREAD_TIMER_ON (master, circuit->t_send_csnp[1], send_l2_csnp, circuit,
  2526. isis_jitter (circuit->csnp_interval[1], CSNP_JITTER));
  2527. return retval;
  2528. }
  2529. static int
  2530. build_psnp (int level, struct isis_circuit *circuit, struct list *lsps)
  2531. {
  2532. struct isis_fixed_hdr fixed_hdr;
  2533. unsigned long lenp;
  2534. u_int16_t length;
  2535. struct isis_lsp *lsp;
  2536. struct isis_passwd *passwd;
  2537. struct listnode *node;
  2538. unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
  2539. unsigned long auth_tlv_offset = 0;
  2540. int retval = ISIS_OK;
  2541. isis_circuit_stream(circuit, &circuit->snd_stream);
  2542. if (level == IS_LEVEL_1)
  2543. fill_fixed_hdr_andstream (&fixed_hdr, L1_PARTIAL_SEQ_NUM,
  2544. circuit->snd_stream);
  2545. else
  2546. fill_fixed_hdr_andstream (&fixed_hdr, L2_PARTIAL_SEQ_NUM,
  2547. circuit->snd_stream);
  2548. /*
  2549. * Fill Level 1 or 2 Partial Sequence Numbers header
  2550. */
  2551. lenp = stream_get_endp (circuit->snd_stream);
  2552. stream_putw (circuit->snd_stream, 0); /* PDU length - when we know it */
  2553. stream_put (circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
  2554. stream_putc (circuit->snd_stream, circuit->idx);
  2555. /*
  2556. * And TLVs
  2557. */
  2558. if (level == IS_LEVEL_1)
  2559. passwd = &circuit->area->area_passwd;
  2560. else
  2561. passwd = &circuit->area->domain_passwd;
  2562. if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
  2563. {
  2564. switch (passwd->type)
  2565. {
  2566. /* Cleartext */
  2567. case ISIS_PASSWD_TYPE_CLEARTXT:
  2568. if (tlv_add_authinfo (ISIS_PASSWD_TYPE_CLEARTXT, passwd->len,
  2569. passwd->passwd, circuit->snd_stream))
  2570. return ISIS_WARNING;
  2571. break;
  2572. /* HMAC MD5 */
  2573. case ISIS_PASSWD_TYPE_HMAC_MD5:
  2574. /* Remember where TLV is written so we can later overwrite the MD5 hash */
  2575. auth_tlv_offset = stream_get_endp (circuit->snd_stream);
  2576. memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
  2577. if (tlv_add_authinfo (ISIS_PASSWD_TYPE_HMAC_MD5, ISIS_AUTH_MD5_SIZE,
  2578. hmac_md5_hash, circuit->snd_stream))
  2579. return ISIS_WARNING;
  2580. break;
  2581. default:
  2582. break;
  2583. }
  2584. }
  2585. retval = tlv_add_lsp_entries (lsps, circuit->snd_stream);
  2586. if (retval != ISIS_OK)
  2587. return retval;
  2588. if (isis->debugs & DEBUG_SNP_PACKETS)
  2589. {
  2590. for (ALL_LIST_ELEMENTS_RO (lsps, node, lsp))
  2591. {
  2592. zlog_debug ("ISIS-Snp (%s): PSNP entry %s, seq 0x%08x,"
  2593. " cksum 0x%04x, lifetime %us",
  2594. circuit->area->area_tag,
  2595. rawlspid_print (lsp->lsp_header->lsp_id),
  2596. ntohl (lsp->lsp_header->seq_num),
  2597. ntohs (lsp->lsp_header->checksum),
  2598. ntohs (lsp->lsp_header->rem_lifetime));
  2599. }
  2600. }
  2601. length = (u_int16_t) stream_get_endp (circuit->snd_stream);
  2602. /* Update PDU length */
  2603. stream_putw_at (circuit->snd_stream, lenp, length);
  2604. /* For HMAC MD5 we need to compute the md5 hash and store it */
  2605. if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND) &&
  2606. passwd->type == ISIS_PASSWD_TYPE_HMAC_MD5)
  2607. {
  2608. hmac_md5 (STREAM_DATA (circuit->snd_stream),
  2609. stream_get_endp(circuit->snd_stream),
  2610. (unsigned char *) &passwd->passwd, passwd->len,
  2611. (unsigned char *) &hmac_md5_hash);
  2612. /* Copy the hash into the stream */
  2613. memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
  2614. hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
  2615. }
  2616. return ISIS_OK;
  2617. }
  2618. /*
  2619. * 7.3.15.4 action on expiration of partial SNP interval
  2620. * level 1
  2621. */
  2622. static int
  2623. send_psnp (int level, struct isis_circuit *circuit)
  2624. {
  2625. struct isis_lsp *lsp;
  2626. struct list *list = NULL;
  2627. struct listnode *node;
  2628. u_char num_lsps;
  2629. int retval = ISIS_OK;
  2630. if (circuit->circ_type == CIRCUIT_T_BROADCAST &&
  2631. circuit->u.bc.is_dr[level - 1])
  2632. return ISIS_OK;
  2633. if (circuit->area->lspdb[level - 1] == NULL ||
  2634. dict_count (circuit->area->lspdb[level - 1]) == 0)
  2635. return ISIS_OK;
  2636. if (! circuit->snd_stream)
  2637. return ISIS_ERROR;
  2638. num_lsps = max_lsps_per_snp (ISIS_SNP_PSNP_FLAG, level, circuit);
  2639. while (1)
  2640. {
  2641. list = list_new ();
  2642. lsp_build_list_ssn (circuit, num_lsps, list,
  2643. circuit->area->lspdb[level - 1]);
  2644. if (listcount (list) == 0)
  2645. {
  2646. list_delete (list);
  2647. return ISIS_OK;
  2648. }
  2649. retval = build_psnp (level, circuit, list);
  2650. if (retval != ISIS_OK)
  2651. {
  2652. zlog_err ("ISIS-Snp (%s): Build L%d PSNP on %s failed",
  2653. circuit->area->area_tag, level, circuit->interface->name);
  2654. list_delete (list);
  2655. return retval;
  2656. }
  2657. if (isis->debugs & DEBUG_SNP_PACKETS)
  2658. {
  2659. zlog_debug ("ISIS-Snp (%s): Sending L%d PSNP on %s, length %zd",
  2660. circuit->area->area_tag, level,
  2661. circuit->interface->name,
  2662. stream_get_endp (circuit->snd_stream));
  2663. if (isis->debugs & DEBUG_PACKET_DUMP)
  2664. zlog_dump_data (STREAM_DATA (circuit->snd_stream),
  2665. stream_get_endp (circuit->snd_stream));
  2666. }
  2667. retval = circuit->tx (circuit, level);
  2668. if (retval != ISIS_OK)
  2669. {
  2670. zlog_err ("ISIS-Snp (%s): Send L%d PSNP on %s failed",
  2671. circuit->area->area_tag, level,
  2672. circuit->interface->name);
  2673. list_delete (list);
  2674. return retval;
  2675. }
  2676. /*
  2677. * sending succeeded, we can clear SSN flags of this circuit
  2678. * for the LSPs in list
  2679. */
  2680. for (ALL_LIST_ELEMENTS_RO (list, node, lsp))
  2681. ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
  2682. list_delete (list);
  2683. }
  2684. return retval;
  2685. }
  2686. int
  2687. send_l1_psnp (struct thread *thread)
  2688. {
  2689. struct isis_circuit *circuit;
  2690. int retval = ISIS_OK;
  2691. circuit = THREAD_ARG (thread);
  2692. assert (circuit);
  2693. circuit->t_send_psnp[0] = NULL;
  2694. send_psnp (1, circuit);
  2695. /* set next timer thread */
  2696. THREAD_TIMER_ON (master, circuit->t_send_psnp[0], send_l1_psnp, circuit,
  2697. isis_jitter (circuit->psnp_interval[0], PSNP_JITTER));
  2698. return retval;
  2699. }
  2700. /*
  2701. * 7.3.15.4 action on expiration of partial SNP interval
  2702. * level 2
  2703. */
  2704. int
  2705. send_l2_psnp (struct thread *thread)
  2706. {
  2707. struct isis_circuit *circuit;
  2708. int retval = ISIS_OK;
  2709. circuit = THREAD_ARG (thread);
  2710. assert (circuit);
  2711. circuit->t_send_psnp[1] = NULL;
  2712. send_psnp (2, circuit);
  2713. /* set next timer thread */
  2714. THREAD_TIMER_ON (master, circuit->t_send_psnp[1], send_l2_psnp, circuit,
  2715. isis_jitter (circuit->psnp_interval[1], PSNP_JITTER));
  2716. return retval;
  2717. }
  2718. /*
  2719. * ISO 10589 - 7.3.14.3
  2720. */
  2721. int
  2722. send_lsp (struct thread *thread)
  2723. {
  2724. struct isis_circuit *circuit;
  2725. struct isis_lsp *lsp;
  2726. struct listnode *node;
  2727. int clear_srm = 1;
  2728. int retval = ISIS_OK;
  2729. circuit = THREAD_ARG (thread);
  2730. assert (circuit);
  2731. if (!circuit->lsp_queue)
  2732. return ISIS_OK;
  2733. node = listhead (circuit->lsp_queue);
  2734. /*
  2735. * Handle case where there are no LSPs on the queue. This can
  2736. * happen, for instance, if an adjacency goes down before this
  2737. * thread gets a chance to run.
  2738. */
  2739. if (!node)
  2740. return ISIS_OK;
  2741. /*
  2742. * Delete LSP from lsp_queue. If it's still in queue, it is assumed
  2743. * as 'transmit pending', but send_lsp may never be called again.
  2744. * Retry will happen because SRM flag will not be cleared.
  2745. */
  2746. lsp = listgetdata(node);
  2747. list_delete_node (circuit->lsp_queue, node);
  2748. /* Set the last-cleared time if the queue is empty. */
  2749. /* TODO: Is is possible that new lsps keep being added to the queue
  2750. * that the queue is never empty? */
  2751. if (list_isempty (circuit->lsp_queue))
  2752. circuit->lsp_queue_last_cleared = time (NULL);
  2753. if (circuit->state != C_STATE_UP || circuit->is_passive == 1)
  2754. goto out;
  2755. /*
  2756. * Do not send if levels do not match
  2757. */
  2758. if (!(lsp->level & circuit->is_type))
  2759. goto out;
  2760. /*
  2761. * Do not send if we do not have adjacencies in state up on the circuit
  2762. */
  2763. if (circuit->upadjcount[lsp->level - 1] == 0)
  2764. goto out;
  2765. /* stream_copy will assert and stop program execution if LSP is larger than
  2766. * the circuit's MTU. So handle and log this case here. */
  2767. if (stream_get_endp(lsp->pdu) > stream_get_size(circuit->snd_stream))
  2768. {
  2769. zlog_err("ISIS-Upd (%s): Can't send L%d LSP %s, seq 0x%08x,"
  2770. " cksum 0x%04x, lifetime %us on %s. LSP Size is %zu"
  2771. " while interface stream size is %zu.",
  2772. circuit->area->area_tag, lsp->level,
  2773. rawlspid_print(lsp->lsp_header->lsp_id),
  2774. ntohl(lsp->lsp_header->seq_num),
  2775. ntohs(lsp->lsp_header->checksum),
  2776. ntohs(lsp->lsp_header->rem_lifetime),
  2777. circuit->interface->name,
  2778. stream_get_endp(lsp->pdu),
  2779. stream_get_size(circuit->snd_stream));
  2780. if (isis->debugs & DEBUG_PACKET_DUMP)
  2781. zlog_dump_data(STREAM_DATA(lsp->pdu), stream_get_endp(lsp->pdu));
  2782. retval = ISIS_ERROR;
  2783. goto out;
  2784. }
  2785. /* copy our lsp to the send buffer */
  2786. stream_copy (circuit->snd_stream, lsp->pdu);
  2787. if (isis->debugs & DEBUG_UPDATE_PACKETS)
  2788. {
  2789. zlog_debug
  2790. ("ISIS-Upd (%s): Sending L%d LSP %s, seq 0x%08x, cksum 0x%04x,"
  2791. " lifetime %us on %s", circuit->area->area_tag, lsp->level,
  2792. rawlspid_print (lsp->lsp_header->lsp_id),
  2793. ntohl (lsp->lsp_header->seq_num),
  2794. ntohs (lsp->lsp_header->checksum),
  2795. ntohs (lsp->lsp_header->rem_lifetime),
  2796. circuit->interface->name);
  2797. if (isis->debugs & DEBUG_PACKET_DUMP)
  2798. zlog_dump_data (STREAM_DATA (circuit->snd_stream),
  2799. stream_get_endp (circuit->snd_stream));
  2800. }
  2801. clear_srm = 0;
  2802. retval = circuit->tx (circuit, lsp->level);
  2803. if (retval != ISIS_OK)
  2804. {
  2805. zlog_err ("ISIS-Upd (%s): Send L%d LSP on %s failed %s",
  2806. circuit->area->area_tag, lsp->level,
  2807. circuit->interface->name,
  2808. (retval == ISIS_WARNING) ? "temporarily" : "permanently");
  2809. }
  2810. out:
  2811. if (clear_srm
  2812. || (retval == ISIS_OK && circuit->circ_type == CIRCUIT_T_BROADCAST)
  2813. || (retval != ISIS_OK && retval != ISIS_WARNING))
  2814. {
  2815. /* SRM flag will trigger retransmission. We will not retransmit if we
  2816. * encountered a fatal error.
  2817. * On success, they should only be cleared if it's a broadcast circuit.
  2818. * On a P2P circuit, we will wait for the ack from the neighbor to clear
  2819. * the fag.
  2820. */
  2821. ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
  2822. }
  2823. return retval;
  2824. }
  2825. int
  2826. ack_lsp (struct isis_link_state_hdr *hdr, struct isis_circuit *circuit,
  2827. int level)
  2828. {
  2829. unsigned long lenp;
  2830. int retval;
  2831. u_int16_t length;
  2832. struct isis_fixed_hdr fixed_hdr;
  2833. isis_circuit_stream(circuit, &circuit->snd_stream);
  2834. // fill_llc_hdr (stream);
  2835. if (level == IS_LEVEL_1)
  2836. fill_fixed_hdr_andstream (&fixed_hdr, L1_PARTIAL_SEQ_NUM,
  2837. circuit->snd_stream);
  2838. else
  2839. fill_fixed_hdr_andstream (&fixed_hdr, L2_PARTIAL_SEQ_NUM,
  2840. circuit->snd_stream);
  2841. lenp = stream_get_endp (circuit->snd_stream);
  2842. stream_putw (circuit->snd_stream, 0); /* PDU length */
  2843. stream_put (circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
  2844. stream_putc (circuit->snd_stream, circuit->idx);
  2845. stream_putc (circuit->snd_stream, 9); /* code */
  2846. stream_putc (circuit->snd_stream, 16); /* len */
  2847. stream_putw (circuit->snd_stream, ntohs (hdr->rem_lifetime));
  2848. stream_put (circuit->snd_stream, hdr->lsp_id, ISIS_SYS_ID_LEN + 2);
  2849. stream_putl (circuit->snd_stream, ntohl (hdr->seq_num));
  2850. stream_putw (circuit->snd_stream, ntohs (hdr->checksum));
  2851. length = (u_int16_t) stream_get_endp (circuit->snd_stream);
  2852. /* Update PDU length */
  2853. stream_putw_at (circuit->snd_stream, lenp, length);
  2854. retval = circuit->tx (circuit, level);
  2855. if (retval != ISIS_OK)
  2856. zlog_err ("ISIS-Upd (%s): Send L%d LSP PSNP on %s failed",
  2857. circuit->area->area_tag, level,
  2858. circuit->interface->name);
  2859. return retval;
  2860. }