ieee802_1x_kay.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583
  1. /*
  2. * IEEE 802.1X-2010 Key Agree Protocol of PAE state machine
  3. * Copyright (c) 2013, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include <time.h>
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "list.h"
  12. #include "eloop.h"
  13. #include "wpabuf.h"
  14. #include "state_machine.h"
  15. #include "l2_packet/l2_packet.h"
  16. #include "common/eapol_common.h"
  17. #include "crypto/aes_wrap.h"
  18. #include "ieee802_1x_cp.h"
  19. #include "ieee802_1x_key.h"
  20. #include "ieee802_1x_kay.h"
  21. #include "ieee802_1x_kay_i.h"
  22. #include "ieee802_1x_secy_ops.h"
  23. #define DEFAULT_SA_KEY_LEN 16
  24. #define DEFAULT_ICV_LEN 16
  25. #define MAX_ICV_LEN 32 /* 32 bytes, 256 bits */
  26. #define PENDING_PN_EXHAUSTION 0xC0000000
  27. #define MKA_ALIGN_LENGTH(len) (((len) + 0x3) & ~0x3)
  28. /* IEEE Std 802.1X-2010, Table 9-1 - MKA Algorithm Agility */
  29. #define MKA_ALGO_AGILITY_2009 { 0x00, 0x80, 0xC2, 0x01 }
  30. static u8 mka_algo_agility[4] = MKA_ALGO_AGILITY_2009;
  31. /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
  32. static struct macsec_ciphersuite cipher_suite_tbl[] = {
  33. /* GCM-AES-128 */
  34. {
  35. .id = CS_ID_GCM_AES_128,
  36. .name = CS_NAME_GCM_AES_128,
  37. .capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50,
  38. .sak_len = DEFAULT_SA_KEY_LEN,
  39. .index = 0,
  40. },
  41. };
  42. #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
  43. #define DEFAULT_CS_INDEX 0
  44. static struct mka_alg mka_alg_tbl[] = {
  45. {
  46. .parameter = MKA_ALGO_AGILITY_2009,
  47. /* 128-bit CAK, KEK, ICK, ICV */
  48. .cak_len = DEFAULT_ICV_LEN,
  49. .kek_len = DEFAULT_ICV_LEN,
  50. .ick_len = DEFAULT_ICV_LEN,
  51. .icv_len = DEFAULT_ICV_LEN,
  52. .cak_trfm = ieee802_1x_cak_128bits_aes_cmac,
  53. .ckn_trfm = ieee802_1x_ckn_128bits_aes_cmac,
  54. .kek_trfm = ieee802_1x_kek_128bits_aes_cmac,
  55. .ick_trfm = ieee802_1x_ick_128bits_aes_cmac,
  56. .icv_hash = ieee802_1x_icv_128bits_aes_cmac,
  57. .index = 1,
  58. },
  59. };
  60. #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
  61. static int is_ki_equal(struct ieee802_1x_mka_ki *ki1,
  62. struct ieee802_1x_mka_ki *ki2)
  63. {
  64. return os_memcmp(ki1->mi, ki2->mi, MI_LEN) == 0 &&
  65. ki1->kn == ki2->kn;
  66. }
  67. static void set_mka_param_body_len(void *body, unsigned int len)
  68. {
  69. struct ieee802_1x_mka_hdr *hdr = body;
  70. hdr->length = (len >> 8) & 0x0f;
  71. hdr->length1 = len & 0xff;
  72. }
  73. static unsigned int get_mka_param_body_len(const void *body)
  74. {
  75. const struct ieee802_1x_mka_hdr *hdr = body;
  76. return (hdr->length << 8) | hdr->length1;
  77. }
  78. static u8 get_mka_param_body_type(const void *body)
  79. {
  80. const struct ieee802_1x_mka_hdr *hdr = body;
  81. return hdr->type;
  82. }
  83. /**
  84. * ieee802_1x_mka_dump_basic_body -
  85. */
  86. static void
  87. ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
  88. {
  89. size_t body_len;
  90. if (!body)
  91. return;
  92. body_len = get_mka_param_body_len(body);
  93. wpa_printf(MSG_DEBUG, "*** MKA Basic Parameter set ***");
  94. wpa_printf(MSG_DEBUG, "\tVersion.......: %d", body->version);
  95. wpa_printf(MSG_DEBUG, "\tPriority......: %d", body->priority);
  96. wpa_printf(MSG_DEBUG, "\tKeySvr........: %d", body->key_server);
  97. wpa_printf(MSG_DEBUG, "\tMACSecDesired.: %d", body->macsec_desired);
  98. wpa_printf(MSG_DEBUG, "\tMACSecCapable.: %d", body->macsec_capability);
  99. wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
  100. wpa_printf(MSG_DEBUG, "\tSCI MAC.......: " MACSTR,
  101. MAC2STR(body->actor_sci.addr));
  102. wpa_printf(MSG_DEBUG, "\tSCI Port .....: %d",
  103. be_to_host16(body->actor_sci.port));
  104. wpa_hexdump(MSG_DEBUG, "\tMember Id.....:",
  105. body->actor_mi, sizeof(body->actor_mi));
  106. wpa_printf(MSG_DEBUG, "\tMessage Number: %d",
  107. be_to_host32(body->actor_mn));
  108. wpa_hexdump(MSG_DEBUG, "\tAlgo Agility..:",
  109. body->algo_agility, sizeof(body->algo_agility));
  110. wpa_hexdump_ascii(MSG_DEBUG, "\tCAK Name......:", body->ckn,
  111. body_len + MKA_HDR_LEN - sizeof(*body));
  112. }
  113. /**
  114. * ieee802_1x_mka_dump_peer_body -
  115. */
  116. static void
  117. ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
  118. {
  119. size_t body_len;
  120. size_t i;
  121. u8 *mi;
  122. be32 mn;
  123. if (body == NULL)
  124. return;
  125. body_len = get_mka_param_body_len(body);
  126. if (body->type == MKA_LIVE_PEER_LIST) {
  127. wpa_printf(MSG_DEBUG, "*** Live Peer List ***");
  128. wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
  129. } else if (body->type == MKA_POTENTIAL_PEER_LIST) {
  130. wpa_printf(MSG_DEBUG, "*** Potential Live Peer List ***");
  131. wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
  132. }
  133. for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
  134. mi = body->peer + i;
  135. os_memcpy(&mn, mi + MI_LEN, sizeof(mn));
  136. wpa_hexdump_ascii(MSG_DEBUG, "\tMember Id.....:", mi, MI_LEN);
  137. wpa_printf(MSG_DEBUG, "\tMessage Number: %d", be_to_host32(mn));
  138. }
  139. }
  140. /**
  141. * ieee802_1x_mka_dump_dist_sak_body -
  142. */
  143. static void
  144. ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
  145. {
  146. size_t body_len;
  147. if (body == NULL)
  148. return;
  149. body_len = get_mka_param_body_len(body);
  150. wpa_printf(MSG_INFO, "*** Distributed SAK ***");
  151. wpa_printf(MSG_INFO, "\tDistributed AN........: %d", body->dan);
  152. wpa_printf(MSG_INFO, "\tConfidentiality Offset: %d",
  153. body->confid_offset);
  154. wpa_printf(MSG_INFO, "\tBody Length...........: %zu", body_len);
  155. if (!body_len)
  156. return;
  157. wpa_printf(MSG_INFO, "\tKey Number............: %d",
  158. be_to_host32(body->kn));
  159. wpa_hexdump(MSG_INFO, "\tAES Key Wrap of SAK...:", body->sak, 24);
  160. }
  161. static const char * yes_no(int val)
  162. {
  163. return val ? "Yes" : "No";
  164. }
  165. /**
  166. * ieee802_1x_mka_dump_sak_use_body -
  167. */
  168. static void
  169. ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
  170. {
  171. int body_len;
  172. if (body == NULL)
  173. return;
  174. body_len = get_mka_param_body_len(body);
  175. wpa_printf(MSG_DEBUG, "*** MACsec SAK Use ***");
  176. wpa_printf(MSG_DEBUG, "\tLatest Key AN....: %d", body->lan);
  177. wpa_printf(MSG_DEBUG, "\tLatest Key Tx....: %s", yes_no(body->ltx));
  178. wpa_printf(MSG_DEBUG, "\tLatest Key Rx....: %s", yes_no(body->lrx));
  179. wpa_printf(MSG_DEBUG, "\tOld Key AN....: %d", body->oan);
  180. wpa_printf(MSG_DEBUG, "\tOld Key Tx....: %s", yes_no(body->otx));
  181. wpa_printf(MSG_DEBUG, "\tOld Key Rx....: %s", yes_no(body->orx));
  182. wpa_printf(MSG_DEBUG, "\tPlain Key Tx....: %s", yes_no(body->ptx));
  183. wpa_printf(MSG_DEBUG, "\tPlain Key Rx....: %s", yes_no(body->prx));
  184. wpa_printf(MSG_DEBUG, "\tDelay Protect....: %s",
  185. yes_no(body->delay_protect));
  186. wpa_printf(MSG_DEBUG, "\tBody Length......: %d", body_len);
  187. if (!body_len)
  188. return;
  189. wpa_hexdump(MSG_DEBUG, "\tKey Server MI....:",
  190. body->lsrv_mi, sizeof(body->lsrv_mi));
  191. wpa_printf(MSG_DEBUG, "\tKey Number.......: %u",
  192. be_to_host32(body->lkn));
  193. wpa_printf(MSG_DEBUG, "\tLowest PN........: %u",
  194. be_to_host32(body->llpn));
  195. wpa_hexdump_ascii(MSG_DEBUG, "\tOld Key Server MI....:",
  196. body->osrv_mi, sizeof(body->osrv_mi));
  197. wpa_printf(MSG_DEBUG, "\tOld Key Number.......: %u",
  198. be_to_host32(body->okn));
  199. wpa_printf(MSG_DEBUG, "\tOld Lowest PN........: %u",
  200. be_to_host32(body->olpn));
  201. }
  202. /**
  203. * ieee802_1x_kay_get_participant -
  204. */
  205. static struct ieee802_1x_mka_participant *
  206. ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
  207. {
  208. struct ieee802_1x_mka_participant *participant;
  209. dl_list_for_each(participant, &kay->participant_list,
  210. struct ieee802_1x_mka_participant, list) {
  211. if (os_memcmp(participant->ckn.name, ckn,
  212. participant->ckn.len) == 0)
  213. return participant;
  214. }
  215. wpa_printf(MSG_DEBUG, "KaY: participant is not found");
  216. return NULL;
  217. }
  218. /**
  219. * ieee802_1x_kay_get_principal_participant -
  220. */
  221. static struct ieee802_1x_mka_participant *
  222. ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
  223. {
  224. struct ieee802_1x_mka_participant *participant;
  225. dl_list_for_each(participant, &kay->participant_list,
  226. struct ieee802_1x_mka_participant, list) {
  227. if (participant->principal)
  228. return participant;
  229. }
  230. wpa_printf(MSG_DEBUG, "KaY: principal participant is not found");
  231. return NULL;
  232. }
  233. static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
  234. const u8 *mi)
  235. {
  236. struct ieee802_1x_kay_peer *peer;
  237. dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
  238. if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
  239. return peer;
  240. }
  241. return NULL;
  242. }
  243. /**
  244. * ieee802_1x_kay_get_potential_peer
  245. */
  246. static struct ieee802_1x_kay_peer *
  247. ieee802_1x_kay_get_potential_peer(
  248. struct ieee802_1x_mka_participant *participant, const u8 *mi)
  249. {
  250. return get_peer_mi(&participant->potential_peers, mi);
  251. }
  252. /**
  253. * ieee802_1x_kay_get_live_peer
  254. */
  255. static struct ieee802_1x_kay_peer *
  256. ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
  257. const u8 *mi)
  258. {
  259. return get_peer_mi(&participant->live_peers, mi);
  260. }
  261. /**
  262. * ieee802_1x_kay_is_in_potential_peer
  263. */
  264. static Boolean
  265. ieee802_1x_kay_is_in_potential_peer(
  266. struct ieee802_1x_mka_participant *participant, const u8 *mi)
  267. {
  268. return ieee802_1x_kay_get_potential_peer(participant, mi) != NULL;
  269. }
  270. /**
  271. * ieee802_1x_kay_is_in_live_peer
  272. */
  273. static Boolean
  274. ieee802_1x_kay_is_in_live_peer(
  275. struct ieee802_1x_mka_participant *participant, const u8 *mi)
  276. {
  277. return ieee802_1x_kay_get_live_peer(participant, mi) != NULL;
  278. }
  279. /**
  280. * ieee802_1x_kay_get_peer
  281. */
  282. static struct ieee802_1x_kay_peer *
  283. ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
  284. const u8 *mi)
  285. {
  286. struct ieee802_1x_kay_peer *peer;
  287. peer = ieee802_1x_kay_get_live_peer(participant, mi);
  288. if (peer)
  289. return peer;
  290. return ieee802_1x_kay_get_potential_peer(participant, mi);
  291. }
  292. /**
  293. * ieee802_1x_kay_get_cipher_suite
  294. */
  295. static struct macsec_ciphersuite *
  296. ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
  297. const u8 *cs_id)
  298. {
  299. unsigned int i;
  300. u64 cs;
  301. be64 _cs;
  302. os_memcpy(&_cs, cs_id, CS_ID_LEN);
  303. cs = be_to_host64(_cs);
  304. for (i = 0; i < CS_TABLE_SIZE; i++) {
  305. if (cipher_suite_tbl[i].id == cs)
  306. return &cipher_suite_tbl[i];
  307. }
  308. return NULL;
  309. }
  310. u64 mka_sci_u64(struct ieee802_1x_mka_sci *sci)
  311. {
  312. struct ieee802_1x_mka_sci tmp;
  313. os_memcpy(tmp.addr, sci->addr, ETH_ALEN);
  314. tmp.port = sci->port;
  315. return *((u64 *) &tmp);
  316. }
  317. static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
  318. const struct ieee802_1x_mka_sci *b)
  319. {
  320. return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
  321. }
  322. /**
  323. * ieee802_1x_kay_get_peer_sci
  324. */
  325. static struct ieee802_1x_kay_peer *
  326. ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
  327. const struct ieee802_1x_mka_sci *sci)
  328. {
  329. struct ieee802_1x_kay_peer *peer;
  330. dl_list_for_each(peer, &participant->live_peers,
  331. struct ieee802_1x_kay_peer, list) {
  332. if (sci_equal(&peer->sci, sci))
  333. return peer;
  334. }
  335. dl_list_for_each(peer, &participant->potential_peers,
  336. struct ieee802_1x_kay_peer, list) {
  337. if (sci_equal(&peer->sci, sci))
  338. return peer;
  339. }
  340. return NULL;
  341. }
  342. static void ieee802_1x_kay_use_data_key(struct data_key *pkey);
  343. /**
  344. * ieee802_1x_kay_init_receive_sa -
  345. */
  346. static struct receive_sa *
  347. ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
  348. struct data_key *key)
  349. {
  350. struct receive_sa *psa;
  351. if (!psc || !key)
  352. return NULL;
  353. psa = os_zalloc(sizeof(*psa));
  354. if (!psa) {
  355. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  356. return NULL;
  357. }
  358. ieee802_1x_kay_use_data_key(key);
  359. psa->pkey = key;
  360. psa->lowest_pn = lowest_pn;
  361. psa->next_pn = lowest_pn;
  362. psa->an = an;
  363. psa->sc = psc;
  364. os_get_time(&psa->created_time);
  365. psa->in_use = FALSE;
  366. dl_list_add(&psc->sa_list, &psa->list);
  367. wpa_printf(MSG_DEBUG,
  368. "KaY: Create receive SA(AN: %hhu lowest_pn: %u of SC",
  369. an, lowest_pn);
  370. return psa;
  371. }
  372. static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey);
  373. /**
  374. * ieee802_1x_kay_deinit_receive_sa -
  375. */
  376. static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
  377. {
  378. ieee802_1x_kay_deinit_data_key(psa->pkey);
  379. psa->pkey = NULL;
  380. wpa_printf(MSG_DEBUG,
  381. "KaY: Delete receive SA(an: %hhu) of SC",
  382. psa->an);
  383. dl_list_del(&psa->list);
  384. os_free(psa);
  385. }
  386. /**
  387. * ieee802_1x_kay_init_receive_sc -
  388. */
  389. static struct receive_sc *
  390. ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci)
  391. {
  392. struct receive_sc *psc;
  393. if (!psci)
  394. return NULL;
  395. psc = os_zalloc(sizeof(*psc));
  396. if (!psc) {
  397. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  398. return NULL;
  399. }
  400. os_memcpy(&psc->sci, psci, sizeof(psc->sci));
  401. os_get_time(&psc->created_time);
  402. psc->receiving = FALSE;
  403. dl_list_init(&psc->sa_list);
  404. wpa_printf(MSG_DEBUG, "KaY: Create receive SC");
  405. wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
  406. return psc;
  407. }
  408. static void ieee802_1x_delete_receive_sa(struct ieee802_1x_kay *kay,
  409. struct receive_sa *sa)
  410. {
  411. secy_disable_receive_sa(kay, sa);
  412. secy_delete_receive_sa(kay, sa);
  413. ieee802_1x_kay_deinit_receive_sa(sa);
  414. }
  415. /**
  416. * ieee802_1x_kay_deinit_receive_sc -
  417. **/
  418. static void
  419. ieee802_1x_kay_deinit_receive_sc(
  420. struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
  421. {
  422. struct receive_sa *psa, *pre_sa;
  423. wpa_printf(MSG_DEBUG, "KaY: Delete receive SC");
  424. dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
  425. list)
  426. ieee802_1x_delete_receive_sa(participant->kay, psa);
  427. dl_list_del(&psc->list);
  428. secy_delete_receive_sc(participant->kay, psc);
  429. os_free(psc);
  430. }
  431. static void ieee802_1x_kay_dump_peer(struct ieee802_1x_kay_peer *peer)
  432. {
  433. wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
  434. wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
  435. wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
  436. wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
  437. }
  438. static struct ieee802_1x_kay_peer *
  439. ieee802_1x_kay_create_peer(const u8 *mi, u32 mn)
  440. {
  441. struct ieee802_1x_kay_peer *peer;
  442. peer = os_zalloc(sizeof(*peer));
  443. if (!peer) {
  444. wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
  445. return NULL;
  446. }
  447. os_memcpy(peer->mi, mi, MI_LEN);
  448. peer->mn = mn;
  449. peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
  450. peer->sak_used = FALSE;
  451. return peer;
  452. }
  453. /**
  454. * ieee802_1x_kay_create_live_peer
  455. */
  456. static struct ieee802_1x_kay_peer *
  457. ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
  458. const u8 *mi, u32 mn)
  459. {
  460. struct ieee802_1x_kay_peer *peer;
  461. struct receive_sc *rxsc;
  462. peer = ieee802_1x_kay_create_peer(mi, mn);
  463. if (!peer)
  464. return NULL;
  465. os_memcpy(&peer->sci, &participant->current_peer_sci,
  466. sizeof(peer->sci));
  467. rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci);
  468. if (!rxsc) {
  469. os_free(peer);
  470. return NULL;
  471. }
  472. dl_list_add(&participant->live_peers, &peer->list);
  473. dl_list_add(&participant->rxsc_list, &rxsc->list);
  474. secy_create_receive_sc(participant->kay, rxsc);
  475. wpa_printf(MSG_DEBUG, "KaY: Live peer created");
  476. ieee802_1x_kay_dump_peer(peer);
  477. return peer;
  478. }
  479. /**
  480. * ieee802_1x_kay_create_potential_peer
  481. */
  482. static struct ieee802_1x_kay_peer *
  483. ieee802_1x_kay_create_potential_peer(
  484. struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
  485. {
  486. struct ieee802_1x_kay_peer *peer;
  487. peer = ieee802_1x_kay_create_peer(mi, mn);
  488. if (!peer)
  489. return NULL;
  490. dl_list_add(&participant->potential_peers, &peer->list);
  491. wpa_printf(MSG_DEBUG, "KaY: potential peer created");
  492. ieee802_1x_kay_dump_peer(peer);
  493. return peer;
  494. }
  495. /**
  496. * ieee802_1x_kay_move_live_peer
  497. */
  498. static struct ieee802_1x_kay_peer *
  499. ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
  500. u8 *mi, u32 mn)
  501. {
  502. struct ieee802_1x_kay_peer *peer;
  503. struct receive_sc *rxsc;
  504. peer = ieee802_1x_kay_get_potential_peer(participant, mi);
  505. if (!peer)
  506. return NULL;
  507. rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci);
  508. if (!rxsc)
  509. return NULL;
  510. os_memcpy(&peer->sci, &participant->current_peer_sci,
  511. sizeof(peer->sci));
  512. peer->mn = mn;
  513. peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
  514. wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
  515. ieee802_1x_kay_dump_peer(peer);
  516. dl_list_del(&peer->list);
  517. dl_list_add_tail(&participant->live_peers, &peer->list);
  518. dl_list_add(&participant->rxsc_list, &rxsc->list);
  519. secy_create_receive_sc(participant->kay, rxsc);
  520. return peer;
  521. }
  522. /**
  523. * ieee802_1x_mka_basic_body_present -
  524. */
  525. static Boolean
  526. ieee802_1x_mka_basic_body_present(
  527. struct ieee802_1x_mka_participant *participant)
  528. {
  529. return TRUE;
  530. }
  531. /**
  532. * ieee802_1x_mka_basic_body_length -
  533. */
  534. static int
  535. ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
  536. {
  537. int length;
  538. length = sizeof(struct ieee802_1x_mka_basic_body);
  539. length += participant->ckn.len;
  540. return MKA_ALIGN_LENGTH(length);
  541. }
  542. /**
  543. * ieee802_1x_mka_encode_basic_body
  544. */
  545. static int
  546. ieee802_1x_mka_encode_basic_body(
  547. struct ieee802_1x_mka_participant *participant,
  548. struct wpabuf *buf)
  549. {
  550. struct ieee802_1x_mka_basic_body *body;
  551. struct ieee802_1x_kay *kay = participant->kay;
  552. unsigned int length = ieee802_1x_mka_basic_body_length(participant);
  553. body = wpabuf_put(buf, length);
  554. body->version = kay->mka_version;
  555. body->priority = kay->actor_priority;
  556. if (participant->is_elected)
  557. body->key_server = participant->is_key_server;
  558. else
  559. body->key_server = participant->can_be_key_server;
  560. body->macsec_desired = kay->macsec_desired;
  561. body->macsec_capability = kay->macsec_capable;
  562. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  563. os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
  564. sizeof(kay->actor_sci.addr));
  565. body->actor_sci.port = kay->actor_sci.port;
  566. os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
  567. participant->mn = participant->mn + 1;
  568. body->actor_mn = host_to_be32(participant->mn);
  569. os_memcpy(body->algo_agility, kay->algo_agility,
  570. sizeof(body->algo_agility));
  571. os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
  572. ieee802_1x_mka_dump_basic_body(body);
  573. return 0;
  574. }
  575. static Boolean
  576. reset_participant_mi(struct ieee802_1x_mka_participant *participant)
  577. {
  578. if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
  579. return FALSE;
  580. participant->mn = 0;
  581. return TRUE;
  582. }
  583. /**
  584. * ieee802_1x_mka_decode_basic_body -
  585. */
  586. static struct ieee802_1x_mka_participant *
  587. ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
  588. size_t msg_len)
  589. {
  590. struct ieee802_1x_mka_participant *participant;
  591. const struct ieee802_1x_mka_basic_body *body;
  592. struct ieee802_1x_kay_peer *peer;
  593. body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
  594. if (body->version > MKA_VERSION_ID) {
  595. wpa_printf(MSG_DEBUG,
  596. "KaY: peer's version(%d) greater than mka current version(%d)",
  597. body->version, MKA_VERSION_ID);
  598. }
  599. if (kay->is_obliged_key_server && body->key_server) {
  600. wpa_printf(MSG_DEBUG, "I must be as key server");
  601. return NULL;
  602. }
  603. participant = ieee802_1x_kay_get_participant(kay, body->ckn);
  604. if (!participant) {
  605. wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
  606. return NULL;
  607. }
  608. /* If the peer's MI is my MI, I will choose new MI */
  609. if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
  610. if (!reset_participant_mi(participant))
  611. return NULL;
  612. }
  613. os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
  614. participant->current_peer_id.mn = body->actor_mn;
  615. os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
  616. sizeof(participant->current_peer_sci.addr));
  617. participant->current_peer_sci.port = body->actor_sci.port;
  618. /* handler peer */
  619. peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
  620. if (!peer) {
  621. /* Check duplicated SCI */
  622. /* TODO: What policy should be applied to detect duplicated SCI
  623. * is active attacker or a valid peer whose MI is be changed?
  624. */
  625. peer = ieee802_1x_kay_get_peer_sci(participant,
  626. &body->actor_sci);
  627. if (peer) {
  628. wpa_printf(MSG_WARNING,
  629. "KaY: duplicated SCI detected, Maybe active attacker");
  630. dl_list_del(&peer->list);
  631. os_free(peer);
  632. }
  633. peer = ieee802_1x_kay_create_potential_peer(
  634. participant, body->actor_mi,
  635. be_to_host32(body->actor_mn));
  636. if (!peer)
  637. return NULL;
  638. peer->macsec_desired = body->macsec_desired;
  639. peer->macsec_capability = body->macsec_capability;
  640. peer->is_key_server = (Boolean) body->key_server;
  641. peer->key_server_priority = body->priority;
  642. } else if (peer->mn < be_to_host32(body->actor_mn)) {
  643. peer->mn = be_to_host32(body->actor_mn);
  644. peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
  645. peer->macsec_desired = body->macsec_desired;
  646. peer->macsec_capability = body->macsec_capability;
  647. peer->is_key_server = (Boolean) body->key_server;
  648. peer->key_server_priority = body->priority;
  649. } else {
  650. wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
  651. return NULL;
  652. }
  653. return participant;
  654. }
  655. /**
  656. * ieee802_1x_mka_live_peer_body_present
  657. */
  658. static Boolean
  659. ieee802_1x_mka_live_peer_body_present(
  660. struct ieee802_1x_mka_participant *participant)
  661. {
  662. return !dl_list_empty(&participant->live_peers);
  663. }
  664. /**
  665. * ieee802_1x_kay_get_live_peer_length
  666. */
  667. static int
  668. ieee802_1x_mka_get_live_peer_length(
  669. struct ieee802_1x_mka_participant *participant)
  670. {
  671. int len = MKA_HDR_LEN;
  672. struct ieee802_1x_kay_peer *peer;
  673. dl_list_for_each(peer, &participant->live_peers,
  674. struct ieee802_1x_kay_peer, list)
  675. len += sizeof(struct ieee802_1x_mka_peer_id);
  676. return MKA_ALIGN_LENGTH(len);
  677. }
  678. /**
  679. * ieee802_1x_mka_encode_live_peer_body -
  680. */
  681. static int
  682. ieee802_1x_mka_encode_live_peer_body(
  683. struct ieee802_1x_mka_participant *participant,
  684. struct wpabuf *buf)
  685. {
  686. struct ieee802_1x_mka_peer_body *body;
  687. struct ieee802_1x_kay_peer *peer;
  688. unsigned int length;
  689. struct ieee802_1x_mka_peer_id *body_peer;
  690. length = ieee802_1x_mka_get_live_peer_length(participant);
  691. body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
  692. body->type = MKA_LIVE_PEER_LIST;
  693. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  694. dl_list_for_each(peer, &participant->live_peers,
  695. struct ieee802_1x_kay_peer, list) {
  696. body_peer = wpabuf_put(buf,
  697. sizeof(struct ieee802_1x_mka_peer_id));
  698. os_memcpy(body_peer->mi, peer->mi, MI_LEN);
  699. body_peer->mn = host_to_be32(peer->mn);
  700. }
  701. ieee802_1x_mka_dump_peer_body(body);
  702. return 0;
  703. }
  704. /**
  705. * ieee802_1x_mka_potential_peer_body_present
  706. */
  707. static Boolean
  708. ieee802_1x_mka_potential_peer_body_present(
  709. struct ieee802_1x_mka_participant *participant)
  710. {
  711. return !dl_list_empty(&participant->potential_peers);
  712. }
  713. /**
  714. * ieee802_1x_kay_get_potential_peer_length
  715. */
  716. static int
  717. ieee802_1x_mka_get_potential_peer_length(
  718. struct ieee802_1x_mka_participant *participant)
  719. {
  720. int len = MKA_HDR_LEN;
  721. struct ieee802_1x_kay_peer *peer;
  722. dl_list_for_each(peer, &participant->potential_peers,
  723. struct ieee802_1x_kay_peer, list)
  724. len += sizeof(struct ieee802_1x_mka_peer_id);
  725. return MKA_ALIGN_LENGTH(len);
  726. }
  727. /**
  728. * ieee802_1x_mka_encode_potential_peer_body -
  729. */
  730. static int
  731. ieee802_1x_mka_encode_potential_peer_body(
  732. struct ieee802_1x_mka_participant *participant,
  733. struct wpabuf *buf)
  734. {
  735. struct ieee802_1x_mka_peer_body *body;
  736. struct ieee802_1x_kay_peer *peer;
  737. unsigned int length;
  738. struct ieee802_1x_mka_peer_id *body_peer;
  739. length = ieee802_1x_mka_get_potential_peer_length(participant);
  740. body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
  741. body->type = MKA_POTENTIAL_PEER_LIST;
  742. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  743. dl_list_for_each(peer, &participant->potential_peers,
  744. struct ieee802_1x_kay_peer, list) {
  745. body_peer = wpabuf_put(buf,
  746. sizeof(struct ieee802_1x_mka_peer_id));
  747. os_memcpy(body_peer->mi, peer->mi, MI_LEN);
  748. body_peer->mn = host_to_be32(peer->mn);
  749. }
  750. ieee802_1x_mka_dump_peer_body(body);
  751. return 0;
  752. }
  753. /**
  754. * ieee802_1x_mka_i_in_peerlist -
  755. */
  756. static Boolean
  757. ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
  758. const u8 *mka_msg, size_t msg_len)
  759. {
  760. struct ieee802_1x_mka_hdr *hdr;
  761. size_t body_len;
  762. size_t left_len;
  763. u8 body_type;
  764. const u8 *pos;
  765. size_t i;
  766. for (pos = mka_msg, left_len = msg_len;
  767. left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
  768. left_len -= body_len + MKA_HDR_LEN,
  769. pos += body_len + MKA_HDR_LEN) {
  770. hdr = (struct ieee802_1x_mka_hdr *) pos;
  771. body_len = get_mka_param_body_len(hdr);
  772. body_type = get_mka_param_body_type(hdr);
  773. if (left_len < (MKA_HDR_LEN + MKA_ALIGN_LENGTH(body_len) + DEFAULT_ICV_LEN)) {
  774. wpa_printf(MSG_ERROR,
  775. "KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
  776. left_len, MKA_HDR_LEN,
  777. MKA_ALIGN_LENGTH(body_len),
  778. DEFAULT_ICV_LEN);
  779. return FALSE;
  780. }
  781. if (body_type != MKA_LIVE_PEER_LIST &&
  782. body_type != MKA_POTENTIAL_PEER_LIST)
  783. continue;
  784. if ((body_len % 16) != 0) {
  785. wpa_printf(MSG_ERROR,
  786. "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
  787. body_len);
  788. continue;
  789. }
  790. ieee802_1x_mka_dump_peer_body(
  791. (struct ieee802_1x_mka_peer_body *)pos);
  792. for (i = 0; i < body_len;
  793. i += sizeof(struct ieee802_1x_mka_peer_id)) {
  794. const struct ieee802_1x_mka_peer_id *peer_mi;
  795. peer_mi = (const struct ieee802_1x_mka_peer_id *)
  796. (pos + MKA_HDR_LEN + i);
  797. if (os_memcmp(peer_mi->mi, participant->mi,
  798. MI_LEN) == 0 &&
  799. be_to_host32(peer_mi->mn) == participant->mn)
  800. return TRUE;
  801. }
  802. }
  803. return FALSE;
  804. }
  805. /**
  806. * ieee802_1x_mka_decode_live_peer_body -
  807. */
  808. static int ieee802_1x_mka_decode_live_peer_body(
  809. struct ieee802_1x_mka_participant *participant,
  810. const u8 *peer_msg, size_t msg_len)
  811. {
  812. const struct ieee802_1x_mka_hdr *hdr;
  813. struct ieee802_1x_kay_peer *peer;
  814. size_t body_len;
  815. size_t i;
  816. Boolean is_included;
  817. is_included = ieee802_1x_kay_is_in_live_peer(
  818. participant, participant->current_peer_id.mi);
  819. hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
  820. body_len = get_mka_param_body_len(hdr);
  821. if (body_len % 16 != 0) {
  822. wpa_printf(MSG_ERROR,
  823. "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
  824. body_len);
  825. return -1;
  826. }
  827. for (i = 0; i < body_len; i += sizeof(struct ieee802_1x_mka_peer_id)) {
  828. const struct ieee802_1x_mka_peer_id *peer_mi;
  829. u32 peer_mn;
  830. peer_mi = (const struct ieee802_1x_mka_peer_id *)
  831. (peer_msg + MKA_HDR_LEN + i);
  832. peer_mn = be_to_host32(peer_mi->mn);
  833. /* it is myself */
  834. if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
  835. /* My message id is used by other participant */
  836. if (peer_mn > participant->mn &&
  837. !reset_participant_mi(participant))
  838. wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
  839. continue;
  840. }
  841. if (!is_included)
  842. continue;
  843. peer = ieee802_1x_kay_get_peer(participant, peer_mi->mi);
  844. if (peer) {
  845. peer->mn = peer_mn;
  846. peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
  847. } else if (!ieee802_1x_kay_create_potential_peer(
  848. participant, peer_mi->mi, peer_mn)) {
  849. return -1;
  850. }
  851. }
  852. return 0;
  853. }
  854. /**
  855. * ieee802_1x_mka_decode_potential_peer_body -
  856. */
  857. static int
  858. ieee802_1x_mka_decode_potential_peer_body(
  859. struct ieee802_1x_mka_participant *participant,
  860. const u8 *peer_msg, size_t msg_len)
  861. {
  862. const struct ieee802_1x_mka_hdr *hdr;
  863. size_t body_len;
  864. size_t i;
  865. hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
  866. body_len = get_mka_param_body_len(hdr);
  867. if (body_len % 16 != 0) {
  868. wpa_printf(MSG_ERROR,
  869. "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
  870. body_len);
  871. return -1;
  872. }
  873. for (i = 0; i < body_len; i += sizeof(struct ieee802_1x_mka_peer_id)) {
  874. const struct ieee802_1x_mka_peer_id *peer_mi;
  875. u32 peer_mn;
  876. peer_mi = (struct ieee802_1x_mka_peer_id *)
  877. (peer_msg + MKA_HDR_LEN + i);
  878. peer_mn = be_to_host32(peer_mi->mn);
  879. /* it is myself */
  880. if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
  881. /* My message id is used by other participant */
  882. if (peer_mn > participant->mn &&
  883. !reset_participant_mi(participant))
  884. wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
  885. continue;
  886. }
  887. }
  888. return 0;
  889. }
  890. /**
  891. * ieee802_1x_mka_sak_use_body_present
  892. */
  893. static Boolean
  894. ieee802_1x_mka_sak_use_body_present(
  895. struct ieee802_1x_mka_participant *participant)
  896. {
  897. return participant->to_use_sak;
  898. }
  899. /**
  900. * ieee802_1x_mka_get_sak_use_length
  901. */
  902. static int
  903. ieee802_1x_mka_get_sak_use_length(
  904. struct ieee802_1x_mka_participant *participant)
  905. {
  906. int length = MKA_HDR_LEN;
  907. if (participant->kay->macsec_desired && participant->advised_desired)
  908. length = sizeof(struct ieee802_1x_mka_sak_use_body);
  909. return MKA_ALIGN_LENGTH(length);
  910. }
  911. /**
  912. *
  913. */
  914. static u32
  915. ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
  916. struct ieee802_1x_mka_ki *ki)
  917. {
  918. struct receive_sa *rxsa;
  919. struct receive_sc *rxsc;
  920. u32 lpn = 0;
  921. dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
  922. dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
  923. {
  924. if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
  925. secy_get_receive_lowest_pn(principal->kay,
  926. rxsa);
  927. lpn = lpn > rxsa->lowest_pn ?
  928. lpn : rxsa->lowest_pn;
  929. break;
  930. }
  931. }
  932. }
  933. if (lpn == 0)
  934. lpn = 1;
  935. return lpn;
  936. }
  937. /**
  938. * ieee802_1x_mka_encode_sak_use_body -
  939. */
  940. static int
  941. ieee802_1x_mka_encode_sak_use_body(
  942. struct ieee802_1x_mka_participant *participant,
  943. struct wpabuf *buf)
  944. {
  945. struct ieee802_1x_mka_sak_use_body *body;
  946. struct ieee802_1x_kay *kay = participant->kay;
  947. unsigned int length;
  948. u32 pn = 1;
  949. length = ieee802_1x_mka_get_sak_use_length(participant);
  950. body = wpabuf_put(buf, length);
  951. body->type = MKA_SAK_USE;
  952. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  953. if (length == MKA_HDR_LEN) {
  954. body->ptx = TRUE;
  955. body->prx = TRUE;
  956. body->lan = 0;
  957. body->lrx = FALSE;
  958. body->ltx = FALSE;
  959. body->delay_protect = FALSE;
  960. return 0;
  961. }
  962. /* data protect, lowest accept packet number */
  963. body->delay_protect = kay->macsec_replay_protect;
  964. pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
  965. if (pn > kay->pn_exhaustion) {
  966. wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
  967. if (participant->is_key_server)
  968. participant->new_sak = TRUE;
  969. }
  970. body->llpn = host_to_be32(pn);
  971. pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
  972. body->olpn = host_to_be32(pn);
  973. /* plain tx, plain rx */
  974. body->ptx = !kay->macsec_protect;
  975. body->prx = kay->macsec_validate != Strict;
  976. /* latest key: rx, tx, key server member identifier key number */
  977. body->lan = participant->lan;
  978. os_memcpy(body->lsrv_mi, participant->lki.mi, sizeof(body->lsrv_mi));
  979. body->lkn = host_to_be32(participant->lki.kn);
  980. body->lrx = participant->lrx;
  981. body->ltx = participant->ltx;
  982. /* old key: rx, tx, key server member identifier key number */
  983. body->oan = participant->oan;
  984. if (participant->oki.kn != participant->lki.kn &&
  985. participant->oki.kn != 0) {
  986. body->otx = TRUE;
  987. body->orx = TRUE;
  988. os_memcpy(body->osrv_mi, participant->oki.mi,
  989. sizeof(body->osrv_mi));
  990. body->okn = host_to_be32(participant->oki.kn);
  991. } else {
  992. body->otx = FALSE;
  993. body->orx = FALSE;
  994. }
  995. /* set CP's variable */
  996. if (body->ltx) {
  997. kay->tx_enable = TRUE;
  998. kay->port_enable = TRUE;
  999. }
  1000. if (body->lrx)
  1001. kay->rx_enable = TRUE;
  1002. ieee802_1x_mka_dump_sak_use_body(body);
  1003. return 0;
  1004. }
  1005. /**
  1006. * ieee802_1x_mka_decode_sak_use_body -
  1007. */
  1008. static int
  1009. ieee802_1x_mka_decode_sak_use_body(
  1010. struct ieee802_1x_mka_participant *participant,
  1011. const u8 *mka_msg, size_t msg_len)
  1012. {
  1013. struct ieee802_1x_mka_hdr *hdr;
  1014. struct ieee802_1x_mka_sak_use_body *body;
  1015. struct ieee802_1x_kay_peer *peer;
  1016. struct transmit_sa *txsa;
  1017. struct data_key *sa_key = NULL;
  1018. size_t body_len;
  1019. struct ieee802_1x_mka_ki ki;
  1020. u32 lpn;
  1021. Boolean all_receiving;
  1022. Boolean found;
  1023. struct ieee802_1x_kay *kay = participant->kay;
  1024. if (!participant->principal) {
  1025. wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
  1026. return -1;
  1027. }
  1028. peer = ieee802_1x_kay_get_live_peer(participant,
  1029. participant->current_peer_id.mi);
  1030. if (!peer) {
  1031. wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
  1032. return -1;
  1033. }
  1034. hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
  1035. body_len = get_mka_param_body_len(hdr);
  1036. body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
  1037. ieee802_1x_mka_dump_sak_use_body(body);
  1038. if ((body_len != 0) && (body_len < 40)) {
  1039. wpa_printf(MSG_ERROR,
  1040. "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 40, or more octets",
  1041. body_len);
  1042. return -1;
  1043. }
  1044. /* TODO: what action should I take when peer does not support MACsec */
  1045. if (body_len == 0) {
  1046. wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
  1047. return 0;
  1048. }
  1049. /* TODO: when the plain tx or rx of peer is true, should I change
  1050. * the attribute of controlled port
  1051. */
  1052. if (body->prx)
  1053. wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
  1054. if (body->ptx)
  1055. wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
  1056. /* check latest key is valid */
  1057. if (body->ltx || body->lrx) {
  1058. found = FALSE;
  1059. os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
  1060. ki.kn = be_to_host32(body->lkn);
  1061. dl_list_for_each(sa_key, &participant->sak_list,
  1062. struct data_key, list) {
  1063. if (is_ki_equal(&sa_key->key_identifier, &ki)) {
  1064. found = TRUE;
  1065. break;
  1066. }
  1067. }
  1068. if (!found) {
  1069. wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
  1070. return -1;
  1071. }
  1072. if (os_memcmp(participant->lki.mi, body->lsrv_mi,
  1073. sizeof(participant->lki.mi)) == 0 &&
  1074. be_to_host32(body->lkn) == participant->lki.kn &&
  1075. body->lan == participant->lan) {
  1076. peer->sak_used = TRUE;
  1077. }
  1078. if (body->ltx && peer->is_key_server) {
  1079. ieee802_1x_cp_set_servertransmitting(kay->cp, TRUE);
  1080. ieee802_1x_cp_sm_step(kay->cp);
  1081. }
  1082. }
  1083. /* check old key is valid */
  1084. if (body->otx || body->orx) {
  1085. if (os_memcmp(participant->oki.mi, body->osrv_mi,
  1086. sizeof(participant->oki.mi)) != 0 ||
  1087. be_to_host32(body->okn) != participant->oki.kn ||
  1088. body->oan != participant->oan) {
  1089. wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
  1090. return -1;
  1091. }
  1092. }
  1093. /* TODO: how to set the MACsec hardware when delay_protect is true */
  1094. if (body->delay_protect &&
  1095. (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
  1096. wpa_printf(MSG_WARNING,
  1097. "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
  1098. return -1;
  1099. }
  1100. /* check all live peer have used the sak for receiving sa */
  1101. all_receiving = TRUE;
  1102. dl_list_for_each(peer, &participant->live_peers,
  1103. struct ieee802_1x_kay_peer, list) {
  1104. if (!peer->sak_used) {
  1105. all_receiving = FALSE;
  1106. break;
  1107. }
  1108. }
  1109. if (all_receiving) {
  1110. participant->to_dist_sak = FALSE;
  1111. ieee802_1x_cp_set_allreceiving(kay->cp, TRUE);
  1112. ieee802_1x_cp_sm_step(kay->cp);
  1113. }
  1114. /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
  1115. lpn = be_to_host32(body->llpn);
  1116. if (lpn > kay->pn_exhaustion) {
  1117. if (participant->is_key_server) {
  1118. participant->new_sak = TRUE;
  1119. wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
  1120. }
  1121. }
  1122. found = FALSE;
  1123. dl_list_for_each(txsa, &participant->txsc->sa_list,
  1124. struct transmit_sa, list) {
  1125. if (sa_key != NULL && txsa->pkey == sa_key) {
  1126. found = TRUE;
  1127. break;
  1128. }
  1129. }
  1130. if (!found) {
  1131. wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
  1132. return -1;
  1133. }
  1134. /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
  1135. * npn is larger than txsa's npn, set it to txsa.
  1136. */
  1137. secy_get_transmit_next_pn(kay, txsa);
  1138. if (lpn > txsa->next_pn) {
  1139. secy_set_transmit_next_pn(kay, txsa);
  1140. wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
  1141. }
  1142. return 0;
  1143. }
  1144. /**
  1145. * ieee802_1x_mka_dist_sak_body_present
  1146. */
  1147. static Boolean
  1148. ieee802_1x_mka_dist_sak_body_present(
  1149. struct ieee802_1x_mka_participant *participant)
  1150. {
  1151. return participant->to_dist_sak && participant->new_key;
  1152. }
  1153. /**
  1154. * ieee802_1x_kay_get_dist_sak_length
  1155. */
  1156. static int
  1157. ieee802_1x_mka_get_dist_sak_length(
  1158. struct ieee802_1x_mka_participant *participant)
  1159. {
  1160. int length = MKA_HDR_LEN;
  1161. unsigned int cs_index = participant->kay->macsec_csindex;
  1162. if (participant->advised_desired && cs_index < CS_TABLE_SIZE) {
  1163. length = sizeof(struct ieee802_1x_mka_dist_sak_body);
  1164. if (cs_index != DEFAULT_CS_INDEX)
  1165. length += CS_ID_LEN;
  1166. length += cipher_suite_tbl[cs_index].sak_len + 8;
  1167. }
  1168. return MKA_ALIGN_LENGTH(length);
  1169. }
  1170. /**
  1171. * ieee802_1x_mka_encode_dist_sak_body -
  1172. */
  1173. static int
  1174. ieee802_1x_mka_encode_dist_sak_body(
  1175. struct ieee802_1x_mka_participant *participant,
  1176. struct wpabuf *buf)
  1177. {
  1178. struct ieee802_1x_mka_dist_sak_body *body;
  1179. struct data_key *sak;
  1180. unsigned int length;
  1181. unsigned int cs_index;
  1182. int sak_pos;
  1183. length = ieee802_1x_mka_get_dist_sak_length(participant);
  1184. body = wpabuf_put(buf, length);
  1185. body->type = MKA_DISTRIBUTED_SAK;
  1186. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  1187. if (length == MKA_HDR_LEN) {
  1188. body->confid_offset = 0;
  1189. body->dan = 0;
  1190. return 0;
  1191. }
  1192. sak = participant->new_key;
  1193. body->confid_offset = sak->confidentiality_offset;
  1194. body->dan = sak->an;
  1195. body->kn = host_to_be32(sak->key_identifier.kn);
  1196. cs_index = participant->kay->macsec_csindex;
  1197. sak_pos = 0;
  1198. if (cs_index >= CS_TABLE_SIZE)
  1199. return -1;
  1200. if (cs_index != DEFAULT_CS_INDEX) {
  1201. be64 cs;
  1202. cs = host_to_be64(cipher_suite_tbl[cs_index].id);
  1203. os_memcpy(body->sak, &cs, CS_ID_LEN);
  1204. sak_pos = CS_ID_LEN;
  1205. }
  1206. if (aes_wrap(participant->kek.key, 16,
  1207. cipher_suite_tbl[cs_index].sak_len / 8,
  1208. sak->key, body->sak + sak_pos)) {
  1209. wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
  1210. return -1;
  1211. }
  1212. ieee802_1x_mka_dump_dist_sak_body(body);
  1213. return 0;
  1214. }
  1215. /**
  1216. * ieee802_1x_kay_init_data_key -
  1217. */
  1218. static void ieee802_1x_kay_init_data_key(struct data_key *pkey)
  1219. {
  1220. pkey->transmits = TRUE;
  1221. pkey->receives = TRUE;
  1222. os_get_time(&pkey->created_time);
  1223. pkey->user = 1;
  1224. }
  1225. /**
  1226. * ieee802_1x_kay_decode_dist_sak_body -
  1227. */
  1228. static int
  1229. ieee802_1x_mka_decode_dist_sak_body(
  1230. struct ieee802_1x_mka_participant *participant,
  1231. const u8 *mka_msg, size_t msg_len)
  1232. {
  1233. struct ieee802_1x_mka_hdr *hdr;
  1234. struct ieee802_1x_mka_dist_sak_body *body;
  1235. struct ieee802_1x_kay_peer *peer;
  1236. struct macsec_ciphersuite *cs;
  1237. size_t body_len;
  1238. struct data_key *sa_key = NULL;
  1239. int sak_len;
  1240. u8 *wrap_sak;
  1241. u8 *unwrap_sak;
  1242. struct ieee802_1x_kay *kay = participant->kay;
  1243. hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
  1244. body_len = get_mka_param_body_len(hdr);
  1245. if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
  1246. wpa_printf(MSG_ERROR,
  1247. "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 28, 36, or more octets",
  1248. body_len);
  1249. return -1;
  1250. }
  1251. if (!participant->principal) {
  1252. wpa_printf(MSG_ERROR,
  1253. "KaY: I can't accept the distributed SAK as I am not principal");
  1254. return -1;
  1255. }
  1256. if (participant->is_key_server) {
  1257. wpa_printf(MSG_ERROR,
  1258. "KaY: I can't accept the distributed SAK as myself is key server ");
  1259. return -1;
  1260. }
  1261. if (!kay->macsec_desired ||
  1262. kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
  1263. wpa_printf(MSG_ERROR,
  1264. "KaY: I am not MACsec-desired or without MACsec capable");
  1265. return -1;
  1266. }
  1267. peer = ieee802_1x_kay_get_live_peer(participant,
  1268. participant->current_peer_id.mi);
  1269. if (!peer) {
  1270. wpa_printf(MSG_ERROR,
  1271. "KaY: The key server is not in my live peers list");
  1272. return -1;
  1273. }
  1274. if (!sci_equal(&kay->key_server_sci, &peer->sci)) {
  1275. wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
  1276. return -1;
  1277. }
  1278. if (body_len == 0) {
  1279. kay->authenticated = TRUE;
  1280. kay->secured = FALSE;
  1281. kay->failed = FALSE;
  1282. participant->advised_desired = FALSE;
  1283. ieee802_1x_cp_connect_authenticated(kay->cp);
  1284. ieee802_1x_cp_sm_step(kay->cp);
  1285. wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
  1286. participant->to_use_sak = FALSE;
  1287. return 0;
  1288. }
  1289. participant->advised_desired = TRUE;
  1290. kay->authenticated = FALSE;
  1291. kay->secured = TRUE;
  1292. kay->failed = FALSE;
  1293. ieee802_1x_cp_connect_secure(kay->cp);
  1294. ieee802_1x_cp_sm_step(kay->cp);
  1295. body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
  1296. ieee802_1x_mka_dump_dist_sak_body(body);
  1297. dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
  1298. {
  1299. if (os_memcmp(sa_key->key_identifier.mi,
  1300. participant->current_peer_id.mi, MI_LEN) == 0 &&
  1301. sa_key->key_identifier.kn == be_to_host32(body->kn)) {
  1302. wpa_printf(MSG_WARNING, "KaY:The Key has installed");
  1303. return 0;
  1304. }
  1305. }
  1306. if (body_len == 28) {
  1307. sak_len = DEFAULT_SA_KEY_LEN;
  1308. wrap_sak = body->sak;
  1309. kay->macsec_csindex = DEFAULT_CS_INDEX;
  1310. cs = &cipher_suite_tbl[kay->macsec_csindex];
  1311. } else {
  1312. cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
  1313. if (!cs) {
  1314. wpa_printf(MSG_ERROR,
  1315. "KaY: I can't support the Cipher Suite advised by key server");
  1316. return -1;
  1317. }
  1318. sak_len = cs->sak_len;
  1319. wrap_sak = body->sak + CS_ID_LEN;
  1320. kay->macsec_csindex = cs->index;
  1321. }
  1322. unwrap_sak = os_zalloc(sak_len);
  1323. if (!unwrap_sak) {
  1324. wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
  1325. return -1;
  1326. }
  1327. if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
  1328. unwrap_sak)) {
  1329. wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
  1330. os_free(unwrap_sak);
  1331. return -1;
  1332. }
  1333. wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
  1334. sa_key = os_zalloc(sizeof(*sa_key));
  1335. if (!sa_key) {
  1336. os_free(unwrap_sak);
  1337. return -1;
  1338. }
  1339. os_memcpy(&sa_key->key_identifier.mi, &participant->current_peer_id.mi,
  1340. MI_LEN);
  1341. sa_key->key_identifier.kn = be_to_host32(body->kn);
  1342. sa_key->key = unwrap_sak;
  1343. sa_key->key_len = sak_len;
  1344. sa_key->confidentiality_offset = body->confid_offset;
  1345. sa_key->an = body->dan;
  1346. ieee802_1x_kay_init_data_key(sa_key);
  1347. ieee802_1x_kay_use_data_key(sa_key);
  1348. dl_list_add(&participant->sak_list, &sa_key->list);
  1349. ieee802_1x_cp_set_ciphersuite(kay->cp, cs->id);
  1350. ieee802_1x_cp_sm_step(kay->cp);
  1351. ieee802_1x_cp_set_offset(kay->cp, body->confid_offset);
  1352. ieee802_1x_cp_sm_step(kay->cp);
  1353. ieee802_1x_cp_set_distributedki(kay->cp, &sa_key->key_identifier);
  1354. ieee802_1x_cp_set_distributedan(kay->cp, body->dan);
  1355. ieee802_1x_cp_signal_newsak(kay->cp);
  1356. ieee802_1x_cp_sm_step(kay->cp);
  1357. kay->rcvd_keys++;
  1358. participant->to_use_sak = TRUE;
  1359. return 0;
  1360. }
  1361. /**
  1362. * ieee802_1x_mka_icv_body_present
  1363. */
  1364. static Boolean
  1365. ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
  1366. {
  1367. return TRUE;
  1368. }
  1369. /**
  1370. * ieee802_1x_kay_get_icv_length
  1371. */
  1372. static int
  1373. ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
  1374. {
  1375. int length;
  1376. length = sizeof(struct ieee802_1x_mka_icv_body);
  1377. length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
  1378. return MKA_ALIGN_LENGTH(length);
  1379. }
  1380. /**
  1381. * ieee802_1x_mka_encode_icv_body -
  1382. */
  1383. static int
  1384. ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
  1385. struct wpabuf *buf)
  1386. {
  1387. struct ieee802_1x_mka_icv_body *body;
  1388. unsigned int length;
  1389. u8 cmac[MAX_ICV_LEN];
  1390. length = ieee802_1x_mka_get_icv_length(participant);
  1391. if (length != DEFAULT_ICV_LEN) {
  1392. body = wpabuf_put(buf, MKA_HDR_LEN);
  1393. body->type = MKA_ICV_INDICATOR;
  1394. set_mka_param_body_len(body, length - MKA_HDR_LEN);
  1395. }
  1396. if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
  1397. participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
  1398. wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
  1399. return -1;
  1400. }
  1401. if (length != DEFAULT_ICV_LEN)
  1402. length -= MKA_HDR_LEN;
  1403. os_memcpy(wpabuf_put(buf, length), cmac, length);
  1404. return 0;
  1405. }
  1406. /**
  1407. * ieee802_1x_mka_decode_icv_body -
  1408. */
  1409. static u8 *
  1410. ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
  1411. const u8 *mka_msg, size_t msg_len)
  1412. {
  1413. struct ieee802_1x_mka_hdr *hdr;
  1414. struct ieee802_1x_mka_icv_body *body;
  1415. size_t body_len;
  1416. size_t left_len;
  1417. u8 body_type;
  1418. const u8 *pos;
  1419. pos = mka_msg;
  1420. left_len = msg_len;
  1421. while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
  1422. hdr = (struct ieee802_1x_mka_hdr *) pos;
  1423. body_len = get_mka_param_body_len(hdr);
  1424. body_type = get_mka_param_body_type(hdr);
  1425. if (left_len < (body_len + MKA_HDR_LEN))
  1426. break;
  1427. if (body_type != MKA_ICV_INDICATOR) {
  1428. left_len -= MKA_HDR_LEN + body_len;
  1429. pos += MKA_HDR_LEN + body_len;
  1430. continue;
  1431. }
  1432. body = (struct ieee802_1x_mka_icv_body *)pos;
  1433. if (body_len
  1434. < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
  1435. return NULL;
  1436. }
  1437. return body->icv;
  1438. }
  1439. return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
  1440. }
  1441. /**
  1442. * ieee802_1x_mka_decode_dist_cak_body-
  1443. */
  1444. static int
  1445. ieee802_1x_mka_decode_dist_cak_body(
  1446. struct ieee802_1x_mka_participant *participant,
  1447. const u8 *mka_msg, size_t msg_len)
  1448. {
  1449. struct ieee802_1x_mka_hdr *hdr;
  1450. size_t body_len;
  1451. hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
  1452. body_len = get_mka_param_body_len(hdr);
  1453. if (body_len < 28) {
  1454. wpa_printf(MSG_ERROR,
  1455. "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 28 or more octets",
  1456. body_len);
  1457. return -1;
  1458. }
  1459. return 0;
  1460. }
  1461. /**
  1462. * ieee802_1x_mka_decode_kmd_body -
  1463. */
  1464. static int
  1465. ieee802_1x_mka_decode_kmd_body(
  1466. struct ieee802_1x_mka_participant *participant,
  1467. const u8 *mka_msg, size_t msg_len)
  1468. {
  1469. struct ieee802_1x_mka_hdr *hdr;
  1470. size_t body_len;
  1471. hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
  1472. body_len = get_mka_param_body_len(hdr);
  1473. if (body_len < 5) {
  1474. wpa_printf(MSG_ERROR,
  1475. "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 5 or more octets",
  1476. body_len);
  1477. return -1;
  1478. }
  1479. return 0;
  1480. }
  1481. /**
  1482. * ieee802_1x_mka_decode_announce_body -
  1483. */
  1484. static int ieee802_1x_mka_decode_announce_body(
  1485. struct ieee802_1x_mka_participant *participant,
  1486. const u8 *mka_msg, size_t msg_len)
  1487. {
  1488. return 0;
  1489. }
  1490. struct mka_param_body_handler {
  1491. int (*body_tx)(struct ieee802_1x_mka_participant *participant,
  1492. struct wpabuf *buf);
  1493. int (*body_rx)(struct ieee802_1x_mka_participant *participant,
  1494. const u8 *mka_msg, size_t msg_len);
  1495. int (*body_length)(struct ieee802_1x_mka_participant *participant);
  1496. Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
  1497. };
  1498. static struct mka_param_body_handler mka_body_handler[] = {
  1499. /* basic parameter set */
  1500. {
  1501. .body_tx = ieee802_1x_mka_encode_basic_body,
  1502. .body_rx = NULL,
  1503. .body_length = ieee802_1x_mka_basic_body_length,
  1504. .body_present = ieee802_1x_mka_basic_body_present
  1505. },
  1506. /* live peer list parameter set */
  1507. {
  1508. .body_tx = ieee802_1x_mka_encode_live_peer_body,
  1509. .body_rx = ieee802_1x_mka_decode_live_peer_body,
  1510. .body_length = ieee802_1x_mka_get_live_peer_length,
  1511. .body_present = ieee802_1x_mka_live_peer_body_present
  1512. },
  1513. /* potential peer list parameter set */
  1514. {
  1515. .body_tx = ieee802_1x_mka_encode_potential_peer_body,
  1516. .body_rx = ieee802_1x_mka_decode_potential_peer_body,
  1517. .body_length = ieee802_1x_mka_get_potential_peer_length,
  1518. .body_present = ieee802_1x_mka_potential_peer_body_present
  1519. },
  1520. /* sak use parameter set */
  1521. {
  1522. .body_tx = ieee802_1x_mka_encode_sak_use_body,
  1523. .body_rx = ieee802_1x_mka_decode_sak_use_body,
  1524. .body_length = ieee802_1x_mka_get_sak_use_length,
  1525. .body_present = ieee802_1x_mka_sak_use_body_present
  1526. },
  1527. /* distribute sak parameter set */
  1528. {
  1529. .body_tx = ieee802_1x_mka_encode_dist_sak_body,
  1530. .body_rx = ieee802_1x_mka_decode_dist_sak_body,
  1531. .body_length = ieee802_1x_mka_get_dist_sak_length,
  1532. .body_present = ieee802_1x_mka_dist_sak_body_present
  1533. },
  1534. /* distribute cak parameter set */
  1535. {
  1536. .body_tx = NULL,
  1537. .body_rx = ieee802_1x_mka_decode_dist_cak_body,
  1538. .body_length = NULL,
  1539. .body_present = NULL
  1540. },
  1541. /* kmd parameter set */
  1542. {
  1543. .body_tx = NULL,
  1544. .body_rx = ieee802_1x_mka_decode_kmd_body,
  1545. .body_length = NULL,
  1546. .body_present = NULL
  1547. },
  1548. /* announce parameter set */
  1549. {
  1550. .body_tx = NULL,
  1551. .body_rx = ieee802_1x_mka_decode_announce_body,
  1552. .body_length = NULL,
  1553. .body_present = NULL
  1554. },
  1555. /* icv parameter set */
  1556. {
  1557. .body_tx = ieee802_1x_mka_encode_icv_body,
  1558. .body_rx = NULL,
  1559. .body_length = ieee802_1x_mka_get_icv_length,
  1560. .body_present = ieee802_1x_mka_icv_body_present
  1561. },
  1562. };
  1563. /**
  1564. * ieee802_1x_kay_use_data_key - Take reference on a key
  1565. */
  1566. static void ieee802_1x_kay_use_data_key(struct data_key *pkey)
  1567. {
  1568. pkey->user++;
  1569. }
  1570. /**
  1571. * ieee802_1x_kay_deinit_data_key - Release reference on a key and
  1572. * free if there are no remaining users
  1573. */
  1574. static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
  1575. {
  1576. if (!pkey)
  1577. return;
  1578. pkey->user--;
  1579. if (pkey->user > 1)
  1580. return;
  1581. os_free(pkey->key);
  1582. os_free(pkey);
  1583. }
  1584. /**
  1585. * ieee802_1x_kay_generate_new_sak -
  1586. */
  1587. static int
  1588. ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
  1589. {
  1590. struct data_key *sa_key = NULL;
  1591. struct ieee802_1x_kay_peer *peer;
  1592. struct ieee802_1x_kay *kay = participant->kay;
  1593. int ctx_len, ctx_offset;
  1594. u8 *context;
  1595. unsigned int key_len;
  1596. u8 *key;
  1597. struct macsec_ciphersuite *cs;
  1598. /* check condition for generating a fresh SAK:
  1599. * must have one live peer
  1600. * and MKA life time elapse since last distribution
  1601. * or potential peer is empty
  1602. */
  1603. if (dl_list_empty(&participant->live_peers)) {
  1604. wpa_printf(MSG_ERROR,
  1605. "KaY: Live peers list must not empty when generating fresh SAK");
  1606. return -1;
  1607. }
  1608. /* FIXME: A fresh SAK not generated until
  1609. * the live peer list contains at least one peer and
  1610. * MKA life time has elapsed since the prior SAK was first distributed,
  1611. * or the Key server's potential peer is empty
  1612. * but I can't understand the second item, so
  1613. * here only check first item and ingore
  1614. * && (!dl_list_empty(&participant->potential_peers))) {
  1615. */
  1616. if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
  1617. wpa_printf(MSG_ERROR,
  1618. "KaY: Life time have not elapsed since prior SAK distributed");
  1619. return -1;
  1620. }
  1621. cs = &cipher_suite_tbl[kay->macsec_csindex];
  1622. key_len = cs->sak_len;
  1623. key = os_zalloc(key_len);
  1624. if (!key) {
  1625. wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
  1626. return -1;
  1627. }
  1628. ctx_len = key_len + sizeof(kay->dist_kn);
  1629. dl_list_for_each(peer, &participant->live_peers,
  1630. struct ieee802_1x_kay_peer, list)
  1631. ctx_len += sizeof(peer->mi);
  1632. ctx_len += sizeof(participant->mi);
  1633. context = os_zalloc(ctx_len);
  1634. if (!context)
  1635. goto fail;
  1636. ctx_offset = 0;
  1637. if (os_get_random(context + ctx_offset, key_len) < 0)
  1638. goto fail;
  1639. ctx_offset += key_len;
  1640. dl_list_for_each(peer, &participant->live_peers,
  1641. struct ieee802_1x_kay_peer, list) {
  1642. os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
  1643. ctx_offset += sizeof(peer->mi);
  1644. }
  1645. os_memcpy(context + ctx_offset, participant->mi,
  1646. sizeof(participant->mi));
  1647. ctx_offset += sizeof(participant->mi);
  1648. os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
  1649. if (key_len == 16) {
  1650. ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
  1651. context, ctx_len, key);
  1652. } else if (key_len == 32) {
  1653. ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
  1654. context, ctx_len, key);
  1655. } else {
  1656. wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
  1657. goto fail;
  1658. }
  1659. wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK", key, key_len);
  1660. os_free(context);
  1661. context = NULL;
  1662. sa_key = os_zalloc(sizeof(*sa_key));
  1663. if (!sa_key) {
  1664. wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
  1665. goto fail;
  1666. }
  1667. sa_key->key = key;
  1668. sa_key->key_len = key_len;
  1669. os_memcpy(sa_key->key_identifier.mi, participant->mi, MI_LEN);
  1670. sa_key->key_identifier.kn = kay->dist_kn;
  1671. sa_key->confidentiality_offset = kay->macsec_confidentiality;
  1672. sa_key->an = kay->dist_an;
  1673. ieee802_1x_kay_init_data_key(sa_key);
  1674. participant->new_key = sa_key;
  1675. ieee802_1x_kay_use_data_key(sa_key);
  1676. dl_list_add(&participant->sak_list, &sa_key->list);
  1677. ieee802_1x_cp_set_ciphersuite(kay->cp, cs->id);
  1678. ieee802_1x_cp_sm_step(kay->cp);
  1679. ieee802_1x_cp_set_offset(kay->cp, kay->macsec_confidentiality);
  1680. ieee802_1x_cp_sm_step(kay->cp);
  1681. ieee802_1x_cp_set_distributedki(kay->cp, &sa_key->key_identifier);
  1682. ieee802_1x_cp_set_distributedan(kay->cp, sa_key->an);
  1683. ieee802_1x_cp_signal_newsak(kay->cp);
  1684. ieee802_1x_cp_sm_step(kay->cp);
  1685. dl_list_for_each(peer, &participant->live_peers,
  1686. struct ieee802_1x_kay_peer, list)
  1687. peer->sak_used = FALSE;
  1688. kay->dist_kn++;
  1689. kay->dist_an++;
  1690. if (kay->dist_an > 3)
  1691. kay->dist_an = 0;
  1692. kay->dist_time = time(NULL);
  1693. return 0;
  1694. fail:
  1695. os_free(key);
  1696. os_free(context);
  1697. return -1;
  1698. }
  1699. static int compare_priorities(const struct ieee802_1x_kay_peer *peer,
  1700. const struct ieee802_1x_kay_peer *other)
  1701. {
  1702. if (peer->key_server_priority < other->key_server_priority)
  1703. return -1;
  1704. if (other->key_server_priority < peer->key_server_priority)
  1705. return 1;
  1706. return os_memcmp(peer->sci.addr, other->sci.addr, ETH_ALEN);
  1707. }
  1708. /**
  1709. * ieee802_1x_kay_elect_key_server - elect the key server
  1710. * when to elect: whenever the live peers list changes
  1711. */
  1712. static int
  1713. ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
  1714. {
  1715. struct ieee802_1x_kay_peer *peer;
  1716. struct ieee802_1x_kay_peer *key_server = NULL;
  1717. struct ieee802_1x_kay *kay = participant->kay;
  1718. Boolean i_is_key_server;
  1719. if (participant->is_obliged_key_server) {
  1720. participant->new_sak = TRUE;
  1721. participant->to_dist_sak = FALSE;
  1722. ieee802_1x_cp_set_electedself(kay->cp, TRUE);
  1723. return 0;
  1724. }
  1725. /* elect the key server among the peers */
  1726. dl_list_for_each(peer, &participant->live_peers,
  1727. struct ieee802_1x_kay_peer, list) {
  1728. if (!peer->is_key_server)
  1729. continue;
  1730. if (!key_server) {
  1731. key_server = peer;
  1732. continue;
  1733. }
  1734. if (compare_priorities(peer, key_server) < 0)
  1735. key_server = peer;
  1736. }
  1737. /* elect the key server between me and the above elected peer */
  1738. i_is_key_server = FALSE;
  1739. if (key_server && participant->can_be_key_server) {
  1740. struct ieee802_1x_kay_peer tmp;
  1741. tmp.key_server_priority = kay->actor_priority;
  1742. os_memcpy(&tmp.sci, &kay->actor_sci, sizeof(tmp.sci));
  1743. if (compare_priorities(&tmp, key_server) < 0)
  1744. i_is_key_server = TRUE;
  1745. } else if (participant->can_be_key_server) {
  1746. i_is_key_server = TRUE;
  1747. }
  1748. if (i_is_key_server) {
  1749. ieee802_1x_cp_set_electedself(kay->cp, TRUE);
  1750. if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
  1751. ieee802_1x_cp_signal_chgdserver(kay->cp);
  1752. ieee802_1x_cp_sm_step(kay->cp);
  1753. }
  1754. participant->is_key_server = TRUE;
  1755. participant->principal = TRUE;
  1756. participant->new_sak = TRUE;
  1757. wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
  1758. participant->to_dist_sak = FALSE;
  1759. participant->is_elected = TRUE;
  1760. os_memcpy(&kay->key_server_sci, &kay->actor_sci,
  1761. sizeof(kay->key_server_sci));
  1762. kay->key_server_priority = kay->actor_priority;
  1763. } else if (key_server) {
  1764. ieee802_1x_cp_set_electedself(kay->cp, FALSE);
  1765. if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
  1766. ieee802_1x_cp_signal_chgdserver(kay->cp);
  1767. ieee802_1x_cp_sm_step(kay->cp);
  1768. }
  1769. participant->is_key_server = FALSE;
  1770. participant->principal = TRUE;
  1771. participant->is_elected = TRUE;
  1772. os_memcpy(&kay->key_server_sci, &key_server->sci,
  1773. sizeof(kay->key_server_sci));
  1774. kay->key_server_priority = key_server->key_server_priority;
  1775. } else {
  1776. participant->principal = FALSE;
  1777. participant->is_key_server = FALSE;
  1778. participant->is_elected = FALSE;
  1779. }
  1780. return 0;
  1781. }
  1782. /**
  1783. * ieee802_1x_kay_decide_macsec_use - the key server determinate
  1784. * how to use MACsec: whether use MACsec and its capability
  1785. * protectFrames will be advised if the key server and one of its live peers are
  1786. * MACsec capable and one of those request MACsec protection
  1787. */
  1788. static int
  1789. ieee802_1x_kay_decide_macsec_use(
  1790. struct ieee802_1x_mka_participant *participant)
  1791. {
  1792. struct ieee802_1x_kay *kay = participant->kay;
  1793. struct ieee802_1x_kay_peer *peer;
  1794. enum macsec_cap less_capability;
  1795. Boolean has_peer;
  1796. if (!participant->is_key_server)
  1797. return -1;
  1798. /* key server self is MACsec-desired and requesting MACsec */
  1799. if (!kay->macsec_desired) {
  1800. participant->advised_desired = FALSE;
  1801. return -1;
  1802. }
  1803. if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
  1804. participant->advised_desired = FALSE;
  1805. return -1;
  1806. }
  1807. less_capability = kay->macsec_capable;
  1808. /* at least one of peers is MACsec-desired and requesting MACsec */
  1809. has_peer = FALSE;
  1810. dl_list_for_each(peer, &participant->live_peers,
  1811. struct ieee802_1x_kay_peer, list) {
  1812. if (!peer->macsec_desired)
  1813. continue;
  1814. if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
  1815. continue;
  1816. less_capability = (less_capability < peer->macsec_capability) ?
  1817. less_capability : peer->macsec_capability;
  1818. has_peer = TRUE;
  1819. }
  1820. if (has_peer) {
  1821. participant->advised_desired = TRUE;
  1822. participant->advised_capability = less_capability;
  1823. kay->authenticated = FALSE;
  1824. kay->secured = TRUE;
  1825. kay->failed = FALSE;
  1826. ieee802_1x_cp_connect_secure(kay->cp);
  1827. ieee802_1x_cp_sm_step(kay->cp);
  1828. } else {
  1829. participant->advised_desired = FALSE;
  1830. participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
  1831. participant->to_use_sak = FALSE;
  1832. kay->authenticated = TRUE;
  1833. kay->secured = FALSE;
  1834. kay->failed = FALSE;
  1835. kay->ltx_kn = 0;
  1836. kay->ltx_an = 0;
  1837. kay->lrx_kn = 0;
  1838. kay->lrx_an = 0;
  1839. kay->otx_kn = 0;
  1840. kay->otx_an = 0;
  1841. kay->orx_kn = 0;
  1842. kay->orx_an = 0;
  1843. ieee802_1x_cp_connect_authenticated(kay->cp);
  1844. ieee802_1x_cp_sm_step(kay->cp);
  1845. }
  1846. return 0;
  1847. }
  1848. static const u8 pae_group_addr[ETH_ALEN] = {
  1849. 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
  1850. };
  1851. /**
  1852. * ieee802_1x_kay_encode_mkpdu -
  1853. */
  1854. static int
  1855. ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
  1856. struct wpabuf *pbuf)
  1857. {
  1858. unsigned int i;
  1859. struct ieee8023_hdr *ether_hdr;
  1860. struct ieee802_1x_hdr *eapol_hdr;
  1861. ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
  1862. os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
  1863. os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
  1864. sizeof(ether_hdr->dest));
  1865. ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
  1866. eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
  1867. eapol_hdr->version = EAPOL_VERSION;
  1868. eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
  1869. eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
  1870. for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
  1871. if (mka_body_handler[i].body_present &&
  1872. mka_body_handler[i].body_present(participant)) {
  1873. if (mka_body_handler[i].body_tx(participant, pbuf))
  1874. return -1;
  1875. }
  1876. }
  1877. return 0;
  1878. }
  1879. /**
  1880. * ieee802_1x_participant_send_mkpdu -
  1881. */
  1882. static int
  1883. ieee802_1x_participant_send_mkpdu(
  1884. struct ieee802_1x_mka_participant *participant)
  1885. {
  1886. struct wpabuf *buf;
  1887. struct ieee802_1x_kay *kay = participant->kay;
  1888. size_t length = 0;
  1889. unsigned int i;
  1890. wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
  1891. length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
  1892. for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
  1893. if (mka_body_handler[i].body_present &&
  1894. mka_body_handler[i].body_present(participant))
  1895. length += mka_body_handler[i].body_length(participant);
  1896. }
  1897. buf = wpabuf_alloc(length);
  1898. if (!buf) {
  1899. wpa_printf(MSG_ERROR, "KaY: out of memory");
  1900. return -1;
  1901. }
  1902. if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
  1903. wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
  1904. return -1;
  1905. }
  1906. l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
  1907. wpabuf_free(buf);
  1908. kay->active = TRUE;
  1909. participant->active = TRUE;
  1910. return 0;
  1911. }
  1912. static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
  1913. static void ieee802_1x_delete_transmit_sa(struct ieee802_1x_kay *kay,
  1914. struct transmit_sa *sa)
  1915. {
  1916. secy_disable_transmit_sa(kay, sa);
  1917. secy_delete_transmit_sa(kay, sa);
  1918. ieee802_1x_kay_deinit_transmit_sa(sa);
  1919. }
  1920. /**
  1921. * ieee802_1x_participant_timer -
  1922. */
  1923. static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
  1924. {
  1925. struct ieee802_1x_mka_participant *participant;
  1926. struct ieee802_1x_kay *kay;
  1927. struct ieee802_1x_kay_peer *peer, *pre_peer;
  1928. time_t now = time(NULL);
  1929. Boolean lp_changed;
  1930. struct receive_sc *rxsc, *pre_rxsc;
  1931. struct transmit_sa *txsa, *pre_txsa;
  1932. participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
  1933. kay = participant->kay;
  1934. if (participant->cak_life) {
  1935. if (now > participant->cak_life)
  1936. goto delete_mka;
  1937. }
  1938. /* should delete MKA instance if there are not live peers
  1939. * when the MKA life elapsed since its creating */
  1940. if (participant->mka_life) {
  1941. if (dl_list_empty(&participant->live_peers)) {
  1942. if (now > participant->mka_life)
  1943. goto delete_mka;
  1944. } else {
  1945. participant->mka_life = 0;
  1946. }
  1947. }
  1948. lp_changed = FALSE;
  1949. dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
  1950. struct ieee802_1x_kay_peer, list) {
  1951. if (now > peer->expire) {
  1952. wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
  1953. wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
  1954. sizeof(peer->mi));
  1955. wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
  1956. dl_list_for_each_safe(rxsc, pre_rxsc,
  1957. &participant->rxsc_list,
  1958. struct receive_sc, list) {
  1959. if (sci_equal(&rxsc->sci, &peer->sci)) {
  1960. ieee802_1x_kay_deinit_receive_sc(
  1961. participant, rxsc);
  1962. }
  1963. }
  1964. dl_list_del(&peer->list);
  1965. os_free(peer);
  1966. lp_changed = TRUE;
  1967. }
  1968. }
  1969. if (lp_changed) {
  1970. if (dl_list_empty(&participant->live_peers)) {
  1971. participant->advised_desired = FALSE;
  1972. participant->advised_capability =
  1973. MACSEC_CAP_NOT_IMPLEMENTED;
  1974. participant->to_use_sak = FALSE;
  1975. participant->ltx = FALSE;
  1976. participant->lrx = FALSE;
  1977. participant->otx = FALSE;
  1978. participant->orx = FALSE;
  1979. participant->is_key_server = FALSE;
  1980. participant->is_elected = FALSE;
  1981. kay->authenticated = TRUE;
  1982. kay->secured = FALSE;
  1983. kay->failed = FALSE;
  1984. kay->ltx_kn = 0;
  1985. kay->ltx_an = 0;
  1986. kay->lrx_kn = 0;
  1987. kay->lrx_an = 0;
  1988. kay->otx_kn = 0;
  1989. kay->otx_an = 0;
  1990. kay->orx_kn = 0;
  1991. kay->orx_an = 0;
  1992. dl_list_for_each_safe(txsa, pre_txsa,
  1993. &participant->txsc->sa_list,
  1994. struct transmit_sa, list) {
  1995. ieee802_1x_delete_transmit_sa(kay, txsa);
  1996. }
  1997. ieee802_1x_cp_connect_authenticated(kay->cp);
  1998. ieee802_1x_cp_sm_step(kay->cp);
  1999. } else {
  2000. ieee802_1x_kay_elect_key_server(participant);
  2001. ieee802_1x_kay_decide_macsec_use(participant);
  2002. }
  2003. }
  2004. dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
  2005. struct ieee802_1x_kay_peer, list) {
  2006. if (now > peer->expire) {
  2007. wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
  2008. wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
  2009. sizeof(peer->mi));
  2010. wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
  2011. dl_list_del(&peer->list);
  2012. os_free(peer);
  2013. }
  2014. }
  2015. if (participant->new_sak) {
  2016. if (!ieee802_1x_kay_generate_new_sak(participant))
  2017. participant->to_dist_sak = TRUE;
  2018. participant->new_sak = FALSE;
  2019. }
  2020. if (participant->retry_count < MAX_RETRY_CNT ||
  2021. participant->mode == PSK) {
  2022. ieee802_1x_participant_send_mkpdu(participant);
  2023. participant->retry_count++;
  2024. }
  2025. eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
  2026. ieee802_1x_participant_timer,
  2027. participant, NULL);
  2028. return;
  2029. delete_mka:
  2030. kay->authenticated = FALSE;
  2031. kay->secured = FALSE;
  2032. kay->failed = TRUE;
  2033. ieee802_1x_kay_delete_mka(kay, &participant->ckn);
  2034. }
  2035. /**
  2036. * ieee802_1x_kay_init_transmit_sa -
  2037. */
  2038. static struct transmit_sa *
  2039. ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
  2040. struct data_key *key)
  2041. {
  2042. struct transmit_sa *psa;
  2043. key->tx_latest = TRUE;
  2044. key->rx_latest = TRUE;
  2045. psa = os_zalloc(sizeof(*psa));
  2046. if (!psa) {
  2047. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  2048. return NULL;
  2049. }
  2050. if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
  2051. key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
  2052. psa->confidentiality = TRUE;
  2053. else
  2054. psa->confidentiality = FALSE;
  2055. psa->an = an;
  2056. ieee802_1x_kay_use_data_key(key);
  2057. psa->pkey = key;
  2058. psa->next_pn = next_PN;
  2059. psa->sc = psc;
  2060. os_get_time(&psa->created_time);
  2061. psa->in_use = FALSE;
  2062. dl_list_add(&psc->sa_list, &psa->list);
  2063. wpa_printf(MSG_DEBUG,
  2064. "KaY: Create transmit SA(an: %hhu, next_PN: %u) of SC",
  2065. an, next_PN);
  2066. return psa;
  2067. }
  2068. /**
  2069. * ieee802_1x_kay_deinit_transmit_sa -
  2070. */
  2071. static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
  2072. {
  2073. ieee802_1x_kay_deinit_data_key(psa->pkey);
  2074. psa->pkey = NULL;
  2075. wpa_printf(MSG_DEBUG,
  2076. "KaY: Delete transmit SA(an: %hhu) of SC",
  2077. psa->an);
  2078. dl_list_del(&psa->list);
  2079. os_free(psa);
  2080. }
  2081. /**
  2082. * init_transmit_sc -
  2083. */
  2084. static struct transmit_sc *
  2085. ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci)
  2086. {
  2087. struct transmit_sc *psc;
  2088. psc = os_zalloc(sizeof(*psc));
  2089. if (!psc) {
  2090. wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
  2091. return NULL;
  2092. }
  2093. os_memcpy(&psc->sci, sci, sizeof(psc->sci));
  2094. os_get_time(&psc->created_time);
  2095. psc->transmitting = FALSE;
  2096. psc->encoding_sa = FALSE;
  2097. psc->enciphering_sa = FALSE;
  2098. dl_list_init(&psc->sa_list);
  2099. wpa_printf(MSG_DEBUG, "KaY: Create transmit SC");
  2100. wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
  2101. return psc;
  2102. }
  2103. /**
  2104. * ieee802_1x_kay_deinit_transmit_sc -
  2105. */
  2106. static void
  2107. ieee802_1x_kay_deinit_transmit_sc(
  2108. struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
  2109. {
  2110. struct transmit_sa *psa, *tmp;
  2111. wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC");
  2112. dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa, list)
  2113. ieee802_1x_delete_transmit_sa(participant->kay, psa);
  2114. secy_delete_transmit_sc(participant->kay, psc);
  2115. os_free(psc);
  2116. }
  2117. /****************** Interface between CP and KAY *********************/
  2118. /**
  2119. * ieee802_1x_kay_set_latest_sa_attr -
  2120. */
  2121. int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
  2122. struct ieee802_1x_mka_ki *lki, u8 lan,
  2123. Boolean ltx, Boolean lrx)
  2124. {
  2125. struct ieee802_1x_mka_participant *principal;
  2126. principal = ieee802_1x_kay_get_principal_participant(kay);
  2127. if (!principal)
  2128. return -1;
  2129. if (!lki)
  2130. os_memset(&principal->lki, 0, sizeof(principal->lki));
  2131. else
  2132. os_memcpy(&principal->lki, lki, sizeof(principal->lki));
  2133. principal->lan = lan;
  2134. principal->ltx = ltx;
  2135. principal->lrx = lrx;
  2136. if (!lki) {
  2137. kay->ltx_kn = 0;
  2138. kay->lrx_kn = 0;
  2139. } else {
  2140. kay->ltx_kn = lki->kn;
  2141. kay->lrx_kn = lki->kn;
  2142. }
  2143. kay->ltx_an = lan;
  2144. kay->lrx_an = lan;
  2145. return 0;
  2146. }
  2147. /**
  2148. * ieee802_1x_kay_set_old_sa_attr -
  2149. */
  2150. int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
  2151. struct ieee802_1x_mka_ki *oki,
  2152. u8 oan, Boolean otx, Boolean orx)
  2153. {
  2154. struct ieee802_1x_mka_participant *principal;
  2155. principal = ieee802_1x_kay_get_principal_participant(kay);
  2156. if (!principal)
  2157. return -1;
  2158. if (!oki)
  2159. os_memset(&principal->oki, 0, sizeof(principal->oki));
  2160. else
  2161. os_memcpy(&principal->oki, oki, sizeof(principal->oki));
  2162. principal->oan = oan;
  2163. principal->otx = otx;
  2164. principal->orx = orx;
  2165. if (!oki) {
  2166. kay->otx_kn = 0;
  2167. kay->orx_kn = 0;
  2168. } else {
  2169. kay->otx_kn = oki->kn;
  2170. kay->orx_kn = oki->kn;
  2171. }
  2172. kay->otx_an = oan;
  2173. kay->orx_an = oan;
  2174. return 0;
  2175. }
  2176. static struct transmit_sa * lookup_txsa_by_an(struct transmit_sc *txsc, u8 an)
  2177. {
  2178. struct transmit_sa *txsa;
  2179. dl_list_for_each(txsa, &txsc->sa_list, struct transmit_sa, list) {
  2180. if (txsa->an == an)
  2181. return txsa;
  2182. }
  2183. return NULL;
  2184. }
  2185. static struct receive_sa * lookup_rxsa_by_an(struct receive_sc *rxsc, u8 an)
  2186. {
  2187. struct receive_sa *rxsa;
  2188. dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list) {
  2189. if (rxsa->an == an)
  2190. return rxsa;
  2191. }
  2192. return NULL;
  2193. }
  2194. /**
  2195. * ieee802_1x_kay_create_sas -
  2196. */
  2197. int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
  2198. struct ieee802_1x_mka_ki *lki)
  2199. {
  2200. struct data_key *sa_key, *latest_sak;
  2201. struct ieee802_1x_mka_participant *principal;
  2202. struct receive_sc *rxsc;
  2203. struct receive_sa *rxsa;
  2204. struct transmit_sa *txsa;
  2205. principal = ieee802_1x_kay_get_principal_participant(kay);
  2206. if (!principal)
  2207. return -1;
  2208. latest_sak = NULL;
  2209. dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
  2210. if (is_ki_equal(&sa_key->key_identifier, lki)) {
  2211. sa_key->rx_latest = TRUE;
  2212. sa_key->tx_latest = TRUE;
  2213. latest_sak = sa_key;
  2214. principal->to_use_sak = TRUE;
  2215. } else {
  2216. sa_key->rx_latest = FALSE;
  2217. sa_key->tx_latest = FALSE;
  2218. }
  2219. }
  2220. if (!latest_sak) {
  2221. wpa_printf(MSG_ERROR, "lki related sak not found");
  2222. return -1;
  2223. }
  2224. dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
  2225. while ((rxsa = lookup_rxsa_by_an(rxsc, latest_sak->an)) != NULL)
  2226. ieee802_1x_delete_receive_sa(kay, rxsa);
  2227. rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
  2228. latest_sak);
  2229. if (!rxsa)
  2230. return -1;
  2231. secy_create_receive_sa(kay, rxsa);
  2232. }
  2233. while ((txsa = lookup_txsa_by_an(principal->txsc, latest_sak->an)) !=
  2234. NULL)
  2235. ieee802_1x_delete_transmit_sa(kay, txsa);
  2236. txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
  2237. 1, latest_sak);
  2238. if (!txsa)
  2239. return -1;
  2240. secy_create_transmit_sa(kay, txsa);
  2241. return 0;
  2242. }
  2243. /**
  2244. * ieee802_1x_kay_delete_sas -
  2245. */
  2246. int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
  2247. struct ieee802_1x_mka_ki *ki)
  2248. {
  2249. struct data_key *sa_key, *pre_key;
  2250. struct transmit_sa *txsa, *pre_txsa;
  2251. struct receive_sa *rxsa, *pre_rxsa;
  2252. struct receive_sc *rxsc;
  2253. struct ieee802_1x_mka_participant *principal;
  2254. wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
  2255. principal = ieee802_1x_kay_get_principal_participant(kay);
  2256. if (!principal)
  2257. return -1;
  2258. /* remove the transmit sa */
  2259. dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
  2260. struct transmit_sa, list) {
  2261. if (is_ki_equal(&txsa->pkey->key_identifier, ki))
  2262. ieee802_1x_delete_transmit_sa(kay, txsa);
  2263. }
  2264. /* remove the receive sa */
  2265. dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
  2266. dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
  2267. struct receive_sa, list) {
  2268. if (is_ki_equal(&rxsa->pkey->key_identifier, ki))
  2269. ieee802_1x_delete_receive_sa(kay, rxsa);
  2270. }
  2271. }
  2272. /* remove the sak */
  2273. dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
  2274. struct data_key, list) {
  2275. if (is_ki_equal(&sa_key->key_identifier, ki)) {
  2276. dl_list_del(&sa_key->list);
  2277. ieee802_1x_kay_deinit_data_key(sa_key);
  2278. break;
  2279. }
  2280. if (principal->new_key == sa_key)
  2281. principal->new_key = NULL;
  2282. }
  2283. return 0;
  2284. }
  2285. /**
  2286. * ieee802_1x_kay_enable_tx_sas -
  2287. */
  2288. int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
  2289. struct ieee802_1x_mka_ki *lki)
  2290. {
  2291. struct ieee802_1x_mka_participant *principal;
  2292. struct transmit_sa *txsa;
  2293. principal = ieee802_1x_kay_get_principal_participant(kay);
  2294. if (!principal)
  2295. return -1;
  2296. dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
  2297. list) {
  2298. if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
  2299. txsa->in_use = TRUE;
  2300. secy_enable_transmit_sa(kay, txsa);
  2301. ieee802_1x_cp_set_usingtransmitas(
  2302. principal->kay->cp, TRUE);
  2303. ieee802_1x_cp_sm_step(principal->kay->cp);
  2304. }
  2305. }
  2306. return 0;
  2307. }
  2308. /**
  2309. * ieee802_1x_kay_enable_rx_sas -
  2310. */
  2311. int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
  2312. struct ieee802_1x_mka_ki *lki)
  2313. {
  2314. struct ieee802_1x_mka_participant *principal;
  2315. struct receive_sa *rxsa;
  2316. struct receive_sc *rxsc;
  2317. principal = ieee802_1x_kay_get_principal_participant(kay);
  2318. if (!principal)
  2319. return -1;
  2320. dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
  2321. dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
  2322. {
  2323. if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
  2324. rxsa->in_use = TRUE;
  2325. secy_enable_receive_sa(kay, rxsa);
  2326. ieee802_1x_cp_set_usingreceivesas(
  2327. principal->kay->cp, TRUE);
  2328. ieee802_1x_cp_sm_step(principal->kay->cp);
  2329. }
  2330. }
  2331. }
  2332. return 0;
  2333. }
  2334. /**
  2335. * ieee802_1x_kay_enable_new_info -
  2336. */
  2337. int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
  2338. {
  2339. struct ieee802_1x_mka_participant *principal;
  2340. principal = ieee802_1x_kay_get_principal_participant(kay);
  2341. if (!principal)
  2342. return -1;
  2343. if (principal->retry_count < MAX_RETRY_CNT || principal->mode == PSK) {
  2344. ieee802_1x_participant_send_mkpdu(principal);
  2345. principal->retry_count++;
  2346. }
  2347. return 0;
  2348. }
  2349. /**
  2350. * ieee802_1x_kay_mkpdu_sanity_check -
  2351. * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
  2352. */
  2353. static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
  2354. const u8 *buf, size_t len)
  2355. {
  2356. struct ieee8023_hdr *eth_hdr;
  2357. struct ieee802_1x_hdr *eapol_hdr;
  2358. struct ieee802_1x_mka_hdr *mka_hdr;
  2359. struct ieee802_1x_mka_basic_body *body;
  2360. size_t mka_msg_len;
  2361. struct ieee802_1x_mka_participant *participant;
  2362. size_t body_len;
  2363. u8 icv[MAX_ICV_LEN];
  2364. u8 *msg_icv;
  2365. eth_hdr = (struct ieee8023_hdr *) buf;
  2366. eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
  2367. mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
  2368. /* destination address should be not individual address */
  2369. if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
  2370. wpa_printf(MSG_MSGDUMP,
  2371. "KaY: ethernet destination address is not PAE group address");
  2372. return -1;
  2373. }
  2374. /* MKPDU should not be less than 32 octets */
  2375. mka_msg_len = be_to_host16(eapol_hdr->length);
  2376. if (mka_msg_len < 32) {
  2377. wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
  2378. return -1;
  2379. }
  2380. /* MKPDU should be a multiple of 4 octets */
  2381. if ((mka_msg_len % 4) != 0) {
  2382. wpa_printf(MSG_MSGDUMP,
  2383. "KaY: MKPDU is not multiple of 4 octets");
  2384. return -1;
  2385. }
  2386. body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
  2387. ieee802_1x_mka_dump_basic_body(body);
  2388. body_len = get_mka_param_body_len(body);
  2389. /* EAPOL-MKA body should comprise basic parameter set and ICV */
  2390. if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
  2391. wpa_printf(MSG_ERROR,
  2392. "KaY: Received EAPOL-MKA Packet Body Length (%zu bytes) is less than the Basic Parameter Set Header Length (%zu bytes) + the Basic Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
  2393. mka_msg_len, MKA_HDR_LEN,
  2394. body_len, DEFAULT_ICV_LEN);
  2395. return -1;
  2396. }
  2397. /* CKN should be owned by I */
  2398. participant = ieee802_1x_kay_get_participant(kay, body->ckn);
  2399. if (!participant) {
  2400. wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
  2401. return -1;
  2402. }
  2403. /* algorithm agility check */
  2404. if (os_memcmp(body->algo_agility, mka_algo_agility,
  2405. sizeof(body->algo_agility)) != 0) {
  2406. wpa_printf(MSG_ERROR,
  2407. "KaY: peer's algorithm agility not supported for me");
  2408. return -1;
  2409. }
  2410. /* ICV check */
  2411. /*
  2412. * The ICV will comprise the final octets of the packet body, whatever
  2413. * its size, not the fixed length 16 octets, indicated by the EAPOL
  2414. * packet body length.
  2415. */
  2416. if (mka_alg_tbl[kay->mka_algindex].icv_hash(
  2417. participant->ick.key,
  2418. buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
  2419. wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
  2420. return -1;
  2421. }
  2422. msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
  2423. mka_msg_len);
  2424. if (!msg_icv) {
  2425. wpa_printf(MSG_ERROR, "KaY: No ICV");
  2426. return -1;
  2427. }
  2428. if (os_memcmp_const(msg_icv, icv,
  2429. mka_alg_tbl[kay->mka_algindex].icv_len) != 0) {
  2430. wpa_printf(MSG_ERROR,
  2431. "KaY: Computed ICV is not equal to Received ICV");
  2432. return -1;
  2433. }
  2434. return 0;
  2435. }
  2436. /**
  2437. * ieee802_1x_kay_decode_mkpdu -
  2438. */
  2439. static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
  2440. const u8 *buf, size_t len)
  2441. {
  2442. struct ieee802_1x_mka_participant *participant;
  2443. struct ieee802_1x_mka_hdr *hdr;
  2444. size_t body_len;
  2445. size_t left_len;
  2446. u8 body_type;
  2447. int i;
  2448. const u8 *pos;
  2449. Boolean handled[256];
  2450. if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
  2451. return -1;
  2452. /* handle basic parameter set */
  2453. pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
  2454. left_len = len - sizeof(struct ieee8023_hdr) -
  2455. sizeof(struct ieee802_1x_hdr);
  2456. participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
  2457. if (!participant)
  2458. return -1;
  2459. /* to skip basic parameter set */
  2460. hdr = (struct ieee802_1x_mka_hdr *) pos;
  2461. body_len = get_mka_param_body_len(hdr);
  2462. pos += body_len + MKA_HDR_LEN;
  2463. left_len -= body_len + MKA_HDR_LEN;
  2464. /* check i am in the peer's peer list */
  2465. if (ieee802_1x_mka_i_in_peerlist(participant, pos, left_len) &&
  2466. !ieee802_1x_kay_is_in_live_peer(participant,
  2467. participant->current_peer_id.mi)) {
  2468. /* accept the peer as live peer */
  2469. if (ieee802_1x_kay_is_in_potential_peer(
  2470. participant, participant->current_peer_id.mi)) {
  2471. if (!ieee802_1x_kay_move_live_peer(
  2472. participant,
  2473. participant->current_peer_id.mi,
  2474. be_to_host32(participant->
  2475. current_peer_id.mn)))
  2476. return -1;
  2477. } else if (!ieee802_1x_kay_create_live_peer(
  2478. participant, participant->current_peer_id.mi,
  2479. be_to_host32(participant->
  2480. current_peer_id.mn))) {
  2481. return -1;
  2482. }
  2483. ieee802_1x_kay_elect_key_server(participant);
  2484. ieee802_1x_kay_decide_macsec_use(participant);
  2485. }
  2486. /*
  2487. * Handle other parameter set than basic parameter set.
  2488. * Each parameter set should be present only once.
  2489. */
  2490. for (i = 0; i < 256; i++)
  2491. handled[i] = FALSE;
  2492. handled[0] = TRUE;
  2493. for (; left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
  2494. pos += body_len + MKA_HDR_LEN,
  2495. left_len -= body_len + MKA_HDR_LEN) {
  2496. hdr = (struct ieee802_1x_mka_hdr *) pos;
  2497. body_len = get_mka_param_body_len(hdr);
  2498. body_type = get_mka_param_body_type(hdr);
  2499. if (body_type == MKA_ICV_INDICATOR)
  2500. return 0;
  2501. if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
  2502. wpa_printf(MSG_ERROR,
  2503. "KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
  2504. left_len, MKA_HDR_LEN,
  2505. body_len, DEFAULT_ICV_LEN);
  2506. return -1;
  2507. }
  2508. if (handled[body_type])
  2509. continue;
  2510. handled[body_type] = TRUE;
  2511. if (body_type < ARRAY_SIZE(mka_body_handler) &&
  2512. mka_body_handler[body_type].body_rx) {
  2513. mka_body_handler[body_type].body_rx
  2514. (participant, pos, left_len);
  2515. } else {
  2516. wpa_printf(MSG_ERROR,
  2517. "The type %d is not supported in this MKA version %d",
  2518. body_type, MKA_VERSION_ID);
  2519. }
  2520. }
  2521. kay->active = TRUE;
  2522. participant->retry_count = 0;
  2523. participant->active = TRUE;
  2524. return 0;
  2525. }
  2526. static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
  2527. size_t len)
  2528. {
  2529. struct ieee802_1x_kay *kay = ctx;
  2530. struct ieee8023_hdr *eth_hdr;
  2531. struct ieee802_1x_hdr *eapol_hdr;
  2532. /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
  2533. if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
  2534. wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
  2535. (unsigned long) len);
  2536. return;
  2537. }
  2538. eth_hdr = (struct ieee8023_hdr *) buf;
  2539. eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
  2540. if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
  2541. be_to_host16(eapol_hdr->length)) {
  2542. wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
  2543. (unsigned long) len,
  2544. (unsigned long) be_to_host16(eapol_hdr->length));
  2545. return;
  2546. }
  2547. if (eapol_hdr->version < EAPOL_VERSION) {
  2548. wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
  2549. eapol_hdr->version);
  2550. return;
  2551. }
  2552. if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
  2553. eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
  2554. return;
  2555. wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
  2556. if (dl_list_empty(&kay->participant_list)) {
  2557. wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
  2558. return;
  2559. }
  2560. ieee802_1x_kay_decode_mkpdu(kay, buf, len);
  2561. }
  2562. /**
  2563. * ieee802_1x_kay_init -
  2564. */
  2565. struct ieee802_1x_kay *
  2566. ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
  2567. u16 port, u8 priority, const char *ifname, const u8 *addr)
  2568. {
  2569. struct ieee802_1x_kay *kay;
  2570. kay = os_zalloc(sizeof(*kay));
  2571. if (!kay) {
  2572. wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
  2573. os_free(ctx);
  2574. return NULL;
  2575. }
  2576. kay->ctx = ctx;
  2577. kay->enable = TRUE;
  2578. kay->active = FALSE;
  2579. kay->authenticated = FALSE;
  2580. kay->secured = FALSE;
  2581. kay->failed = FALSE;
  2582. kay->policy = policy;
  2583. os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
  2584. os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
  2585. kay->actor_sci.port = host_to_be16(port ? port : 0x0001);
  2586. kay->actor_priority = priority;
  2587. /* While actor acts as a key server, shall distribute sakey */
  2588. kay->dist_kn = 1;
  2589. kay->dist_an = 0;
  2590. kay->dist_time = 0;
  2591. kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
  2592. kay->macsec_csindex = DEFAULT_CS_INDEX;
  2593. kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
  2594. kay->mka_version = MKA_VERSION_ID;
  2595. os_memcpy(kay->algo_agility, mka_algo_agility,
  2596. sizeof(kay->algo_agility));
  2597. dl_list_init(&kay->participant_list);
  2598. if (policy != DO_NOT_SECURE &&
  2599. secy_get_capability(kay, &kay->macsec_capable) < 0)
  2600. goto error;
  2601. if (policy == DO_NOT_SECURE ||
  2602. kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
  2603. kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
  2604. kay->macsec_desired = FALSE;
  2605. kay->macsec_protect = FALSE;
  2606. kay->macsec_validate = Disabled;
  2607. kay->macsec_replay_protect = FALSE;
  2608. kay->macsec_replay_window = 0;
  2609. kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
  2610. } else {
  2611. kay->macsec_desired = TRUE;
  2612. kay->macsec_protect = TRUE;
  2613. kay->macsec_encrypt = policy == SHOULD_ENCRYPT;
  2614. kay->macsec_validate = Strict;
  2615. kay->macsec_replay_protect = FALSE;
  2616. kay->macsec_replay_window = 0;
  2617. if (kay->macsec_capable >= MACSEC_CAP_INTEG_AND_CONF)
  2618. kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
  2619. else
  2620. kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
  2621. }
  2622. wpa_printf(MSG_DEBUG, "KaY: state machine created");
  2623. /* Initialize the SecY must be prio to CP, as CP will control SecY */
  2624. if (secy_init_macsec(kay) < 0) {
  2625. wpa_printf(MSG_DEBUG, "KaY: Could not initialize MACsec");
  2626. goto error;
  2627. }
  2628. wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
  2629. /* init CP */
  2630. kay->cp = ieee802_1x_cp_sm_init(kay);
  2631. if (kay->cp == NULL)
  2632. goto error;
  2633. if (policy == DO_NOT_SECURE) {
  2634. ieee802_1x_cp_connect_authenticated(kay->cp);
  2635. ieee802_1x_cp_sm_step(kay->cp);
  2636. } else {
  2637. kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
  2638. kay_l2_receive, kay, 1);
  2639. if (kay->l2_mka == NULL) {
  2640. wpa_printf(MSG_WARNING,
  2641. "KaY: Failed to initialize L2 packet processing for MKA packet");
  2642. goto error;
  2643. }
  2644. }
  2645. return kay;
  2646. error:
  2647. ieee802_1x_kay_deinit(kay);
  2648. return NULL;
  2649. }
  2650. /**
  2651. * ieee802_1x_kay_deinit -
  2652. */
  2653. void
  2654. ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
  2655. {
  2656. struct ieee802_1x_mka_participant *participant;
  2657. if (!kay)
  2658. return;
  2659. wpa_printf(MSG_DEBUG, "KaY: state machine removed");
  2660. while (!dl_list_empty(&kay->participant_list)) {
  2661. participant = dl_list_entry(kay->participant_list.next,
  2662. struct ieee802_1x_mka_participant,
  2663. list);
  2664. ieee802_1x_kay_delete_mka(kay, &participant->ckn);
  2665. }
  2666. ieee802_1x_cp_sm_deinit(kay->cp);
  2667. secy_deinit_macsec(kay);
  2668. if (kay->l2_mka) {
  2669. l2_packet_deinit(kay->l2_mka);
  2670. kay->l2_mka = NULL;
  2671. }
  2672. os_free(kay->ctx);
  2673. os_free(kay);
  2674. }
  2675. /**
  2676. * ieee802_1x_kay_create_mka -
  2677. */
  2678. struct ieee802_1x_mka_participant *
  2679. ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
  2680. struct mka_key *cak, u32 life,
  2681. enum mka_created_mode mode, Boolean is_authenticator)
  2682. {
  2683. struct ieee802_1x_mka_participant *participant;
  2684. unsigned int usecs;
  2685. if (!kay || !ckn || !cak) {
  2686. wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
  2687. return NULL;
  2688. }
  2689. if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
  2690. wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
  2691. return NULL;
  2692. }
  2693. if (ckn->len > MAX_CKN_LEN) {
  2694. wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
  2695. return NULL;
  2696. }
  2697. if (!kay->enable) {
  2698. wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
  2699. return NULL;
  2700. }
  2701. participant = os_zalloc(sizeof(*participant));
  2702. if (!participant) {
  2703. wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
  2704. return NULL;
  2705. }
  2706. participant->ckn.len = ckn->len;
  2707. os_memcpy(participant->ckn.name, ckn->name, ckn->len);
  2708. participant->cak.len = cak->len;
  2709. os_memcpy(participant->cak.key, cak->key, cak->len);
  2710. if (life)
  2711. participant->cak_life = life + time(NULL);
  2712. switch (mode) {
  2713. case EAP_EXCHANGE:
  2714. if (is_authenticator) {
  2715. participant->is_obliged_key_server = TRUE;
  2716. participant->can_be_key_server = TRUE;
  2717. participant->is_key_server = TRUE;
  2718. participant->principal = TRUE;
  2719. os_memcpy(&kay->key_server_sci, &kay->actor_sci,
  2720. sizeof(kay->key_server_sci));
  2721. kay->key_server_priority = kay->actor_priority;
  2722. participant->is_elected = TRUE;
  2723. } else {
  2724. participant->is_obliged_key_server = FALSE;
  2725. participant->can_be_key_server = FALSE;
  2726. participant->is_key_server = FALSE;
  2727. participant->is_elected = TRUE;
  2728. }
  2729. break;
  2730. default:
  2731. participant->is_obliged_key_server = FALSE;
  2732. participant->can_be_key_server = TRUE;
  2733. participant->is_key_server = TRUE;
  2734. participant->is_elected = FALSE;
  2735. break;
  2736. }
  2737. participant->cached = FALSE;
  2738. participant->active = FALSE;
  2739. participant->participant = FALSE;
  2740. participant->retain = FALSE;
  2741. participant->activate = DEFAULT;
  2742. if (participant->is_key_server)
  2743. participant->principal = TRUE;
  2744. dl_list_init(&participant->live_peers);
  2745. dl_list_init(&participant->potential_peers);
  2746. participant->retry_count = 0;
  2747. participant->kay = kay;
  2748. if (!reset_participant_mi(participant))
  2749. goto fail;
  2750. participant->lrx = FALSE;
  2751. participant->ltx = FALSE;
  2752. participant->orx = FALSE;
  2753. participant->otx = FALSE;
  2754. participant->to_dist_sak = FALSE;
  2755. participant->to_use_sak = FALSE;
  2756. participant->new_sak = FALSE;
  2757. dl_list_init(&participant->sak_list);
  2758. participant->new_key = NULL;
  2759. dl_list_init(&participant->rxsc_list);
  2760. participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci);
  2761. secy_cp_control_protect_frames(kay, kay->macsec_protect);
  2762. secy_cp_control_replay(kay, kay->macsec_replay_protect,
  2763. kay->macsec_replay_window);
  2764. secy_create_transmit_sc(kay, participant->txsc);
  2765. /* to derive KEK from CAK and CKN */
  2766. participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
  2767. if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
  2768. participant->ckn.name,
  2769. participant->ckn.len,
  2770. participant->kek.key)) {
  2771. wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
  2772. goto fail;
  2773. }
  2774. wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
  2775. participant->kek.key, participant->kek.len);
  2776. /* to derive ICK from CAK and CKN */
  2777. participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
  2778. if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
  2779. participant->ckn.name,
  2780. participant->ckn.len,
  2781. participant->ick.key)) {
  2782. wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
  2783. goto fail;
  2784. }
  2785. wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
  2786. participant->ick.key, participant->ick.len);
  2787. dl_list_add(&kay->participant_list, &participant->list);
  2788. wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
  2789. ckn->name, ckn->len);
  2790. usecs = os_random() % (MKA_HELLO_TIME * 1000);
  2791. eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
  2792. participant, NULL);
  2793. /* Disable MKA lifetime for PSK mode.
  2794. * The peer(s) can take a long time to come up, because we
  2795. * create a "standby" MKA, and we need it to remain live until
  2796. * some peer appears.
  2797. */
  2798. if (mode != PSK) {
  2799. participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
  2800. usecs / 1000000;
  2801. }
  2802. participant->mode = mode;
  2803. return participant;
  2804. fail:
  2805. os_free(participant);
  2806. return NULL;
  2807. }
  2808. /**
  2809. * ieee802_1x_kay_delete_mka -
  2810. */
  2811. void
  2812. ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
  2813. {
  2814. struct ieee802_1x_mka_participant *participant;
  2815. struct ieee802_1x_kay_peer *peer;
  2816. struct data_key *sak;
  2817. struct receive_sc *rxsc;
  2818. if (!kay || !ckn)
  2819. return;
  2820. wpa_printf(MSG_DEBUG, "KaY: participant removed");
  2821. /* get the participant */
  2822. participant = ieee802_1x_kay_get_participant(kay, ckn->name);
  2823. if (!participant) {
  2824. wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
  2825. ckn->name, ckn->len);
  2826. return;
  2827. }
  2828. eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
  2829. dl_list_del(&participant->list);
  2830. /* remove live peer */
  2831. while (!dl_list_empty(&participant->live_peers)) {
  2832. peer = dl_list_entry(participant->live_peers.next,
  2833. struct ieee802_1x_kay_peer, list);
  2834. dl_list_del(&peer->list);
  2835. os_free(peer);
  2836. }
  2837. /* remove potential peer */
  2838. while (!dl_list_empty(&participant->potential_peers)) {
  2839. peer = dl_list_entry(participant->potential_peers.next,
  2840. struct ieee802_1x_kay_peer, list);
  2841. dl_list_del(&peer->list);
  2842. os_free(peer);
  2843. }
  2844. /* remove sak */
  2845. while (!dl_list_empty(&participant->sak_list)) {
  2846. sak = dl_list_entry(participant->sak_list.next,
  2847. struct data_key, list);
  2848. dl_list_del(&sak->list);
  2849. ieee802_1x_kay_deinit_data_key(sak);
  2850. }
  2851. while (!dl_list_empty(&participant->rxsc_list)) {
  2852. rxsc = dl_list_entry(participant->rxsc_list.next,
  2853. struct receive_sc, list);
  2854. ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
  2855. }
  2856. ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
  2857. os_memset(&participant->cak, 0, sizeof(participant->cak));
  2858. os_memset(&participant->kek, 0, sizeof(participant->kek));
  2859. os_memset(&participant->ick, 0, sizeof(participant->ick));
  2860. os_free(participant);
  2861. }
  2862. /**
  2863. * ieee802_1x_kay_mka_participate -
  2864. */
  2865. void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
  2866. struct mka_key_name *ckn,
  2867. Boolean status)
  2868. {
  2869. struct ieee802_1x_mka_participant *participant;
  2870. if (!kay || !ckn)
  2871. return;
  2872. participant = ieee802_1x_kay_get_participant(kay, ckn->name);
  2873. if (!participant)
  2874. return;
  2875. participant->active = status;
  2876. }
  2877. /**
  2878. * ieee802_1x_kay_new_sak -
  2879. */
  2880. int
  2881. ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
  2882. {
  2883. struct ieee802_1x_mka_participant *participant;
  2884. if (!kay)
  2885. return -1;
  2886. participant = ieee802_1x_kay_get_principal_participant(kay);
  2887. if (!participant)
  2888. return -1;
  2889. participant->new_sak = TRUE;
  2890. wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
  2891. return 0;
  2892. }
  2893. /**
  2894. * ieee802_1x_kay_change_cipher_suite -
  2895. */
  2896. int
  2897. ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay,
  2898. unsigned int cs_index)
  2899. {
  2900. struct ieee802_1x_mka_participant *participant;
  2901. enum macsec_cap secy_cap;
  2902. if (!kay)
  2903. return -1;
  2904. if (cs_index >= CS_TABLE_SIZE) {
  2905. wpa_printf(MSG_ERROR,
  2906. "KaY: Configured cipher suite index is out of range");
  2907. return -1;
  2908. }
  2909. if (kay->macsec_csindex == cs_index)
  2910. return -2;
  2911. if (cs_index == 0)
  2912. kay->macsec_desired = FALSE;
  2913. kay->macsec_csindex = cs_index;
  2914. kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
  2915. if (secy_get_capability(kay, &secy_cap) < 0)
  2916. return -3;
  2917. if (kay->macsec_capable > secy_cap)
  2918. kay->macsec_capable = secy_cap;
  2919. participant = ieee802_1x_kay_get_principal_participant(kay);
  2920. if (participant) {
  2921. wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
  2922. participant->new_sak = TRUE;
  2923. }
  2924. return 0;
  2925. }
  2926. #ifdef CONFIG_CTRL_IFACE
  2927. /**
  2928. * ieee802_1x_kay_get_status - Get IEEE 802.1X KaY status details
  2929. * @sm: Pointer to KaY allocated with ieee802_1x_kay_init()
  2930. * @buf: Buffer for status information
  2931. * @buflen: Maximum buffer length
  2932. * @verbose: Whether to include verbose status information
  2933. * Returns: Number of bytes written to buf.
  2934. *
  2935. * Query KAY status information. This function fills in a text area with current
  2936. * status information. If the buffer (buf) is not large enough, status
  2937. * information will be truncated to fit the buffer.
  2938. */
  2939. int ieee802_1x_kay_get_status(struct ieee802_1x_kay *kay, char *buf,
  2940. size_t buflen)
  2941. {
  2942. int len;
  2943. if (!kay)
  2944. return 0;
  2945. len = os_snprintf(buf, buflen,
  2946. "PAE KaY status=%s\n"
  2947. "Authenticated=%s\n"
  2948. "Secured=%s\n"
  2949. "Failed=%s\n"
  2950. "Actor Priority=%u\n"
  2951. "Key Server Priority=%u\n"
  2952. "Is Key Server=%s\n"
  2953. "Number of Keys Distributed=%u\n"
  2954. "Number of Keys Received=%u\n",
  2955. kay->active ? "Active" : "Not-Active",
  2956. kay->authenticated ? "Yes" : "No",
  2957. kay->secured ? "Yes" : "No",
  2958. kay->failed ? "Yes" : "No",
  2959. kay->actor_priority,
  2960. kay->key_server_priority,
  2961. kay->is_key_server ? "Yes" : "No",
  2962. kay->dist_kn - 1,
  2963. kay->rcvd_keys);
  2964. if (os_snprintf_error(buflen, len))
  2965. return 0;
  2966. return len;
  2967. }
  2968. #endif /* CONFIG_CTRL_IFACE */