isis_pdu.c 90 KB

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