ctrl_iface.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2017, Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
  4. * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "utils/includes.h"
  10. #ifndef CONFIG_NATIVE_WINDOWS
  11. #ifdef CONFIG_TESTING_OPTIONS
  12. #include <net/ethernet.h>
  13. #include <netinet/ip.h>
  14. #endif /* CONFIG_TESTING_OPTIONS */
  15. #include <sys/un.h>
  16. #include <sys/stat.h>
  17. #include <stddef.h>
  18. #ifdef CONFIG_CTRL_IFACE_UDP
  19. #include <netdb.h>
  20. #endif /* CONFIG_CTRL_IFACE_UDP */
  21. #include "utils/common.h"
  22. #include "utils/eloop.h"
  23. #include "utils/module_tests.h"
  24. #include "common/version.h"
  25. #include "common/ieee802_11_defs.h"
  26. #include "common/ctrl_iface_common.h"
  27. #ifdef CONFIG_DPP
  28. #include "common/dpp.h"
  29. #endif /* CONFIG_DPP */
  30. #include "common/wpa_ctrl.h"
  31. #include "crypto/tls.h"
  32. #include "drivers/driver.h"
  33. #include "eapol_auth/eapol_auth_sm.h"
  34. #include "radius/radius_client.h"
  35. #include "radius/radius_server.h"
  36. #include "l2_packet/l2_packet.h"
  37. #include "ap/hostapd.h"
  38. #include "ap/ap_config.h"
  39. #include "ap/ieee802_1x.h"
  40. #include "ap/wpa_auth.h"
  41. #include "ap/ieee802_11.h"
  42. #include "ap/sta_info.h"
  43. #include "ap/wps_hostapd.h"
  44. #include "ap/ctrl_iface_ap.h"
  45. #include "ap/ap_drv_ops.h"
  46. #include "ap/hs20.h"
  47. #include "ap/wnm_ap.h"
  48. #include "ap/wpa_auth.h"
  49. #include "ap/beacon.h"
  50. #include "ap/neighbor_db.h"
  51. #include "ap/rrm.h"
  52. #include "ap/dpp_hostapd.h"
  53. #include "wps/wps_defs.h"
  54. #include "wps/wps.h"
  55. #include "fst/fst_ctrl_iface.h"
  56. #include "config_file.h"
  57. #include "ctrl_iface.h"
  58. #include "common/attacks.h"
  59. #include "../src/ap/wpa_auth_i.h"
  60. #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
  61. #ifdef CONFIG_CTRL_IFACE_UDP
  62. #define COOKIE_LEN 8
  63. static unsigned char cookie[COOKIE_LEN];
  64. static unsigned char gcookie[COOKIE_LEN];
  65. #define HOSTAPD_CTRL_IFACE_PORT 8877
  66. #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
  67. #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
  68. #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
  69. #endif /* CONFIG_CTRL_IFACE_UDP */
  70. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  71. enum wpa_msg_type type,
  72. const char *buf, size_t len);
  73. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  74. struct sockaddr_storage *from,
  75. socklen_t fromlen, const char *input)
  76. {
  77. return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
  78. }
  79. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  80. struct sockaddr_storage *from,
  81. socklen_t fromlen)
  82. {
  83. return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
  84. }
  85. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  86. struct sockaddr_storage *from,
  87. socklen_t fromlen,
  88. char *level)
  89. {
  90. return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
  91. }
  92. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  93. const char *txtaddr)
  94. {
  95. u8 addr[ETH_ALEN];
  96. struct sta_info *sta;
  97. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  98. if (hwaddr_aton(txtaddr, addr))
  99. return -1;
  100. sta = ap_get_sta(hapd, addr);
  101. if (sta)
  102. return 0;
  103. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  104. "notification", MAC2STR(addr));
  105. sta = ap_sta_add(hapd, addr);
  106. if (sta == NULL)
  107. return -1;
  108. hostapd_new_assoc_sta(hapd, sta, 0);
  109. return 0;
  110. }
  111. #ifdef CONFIG_IEEE80211W
  112. #ifdef NEED_AP_MLME
  113. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  114. const char *txtaddr)
  115. {
  116. u8 addr[ETH_ALEN];
  117. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  118. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  119. if (hwaddr_aton(txtaddr, addr) ||
  120. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  121. return -1;
  122. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  123. return 0;
  124. }
  125. #endif /* NEED_AP_MLME */
  126. #endif /* CONFIG_IEEE80211W */
  127. #ifdef CONFIG_WPS
  128. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  129. {
  130. char *pin = os_strchr(txt, ' ');
  131. char *timeout_txt;
  132. int timeout;
  133. u8 addr_buf[ETH_ALEN], *addr = NULL;
  134. char *pos;
  135. if (pin == NULL)
  136. return -1;
  137. *pin++ = '\0';
  138. timeout_txt = os_strchr(pin, ' ');
  139. if (timeout_txt) {
  140. *timeout_txt++ = '\0';
  141. timeout = atoi(timeout_txt);
  142. pos = os_strchr(timeout_txt, ' ');
  143. if (pos) {
  144. *pos++ = '\0';
  145. if (hwaddr_aton(pos, addr_buf) == 0)
  146. addr = addr_buf;
  147. }
  148. } else
  149. timeout = 0;
  150. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  151. }
  152. static int hostapd_ctrl_iface_wps_check_pin(
  153. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  154. {
  155. char pin[9];
  156. size_t len;
  157. char *pos;
  158. int ret;
  159. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  160. (u8 *) cmd, os_strlen(cmd));
  161. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  162. if (*pos < '0' || *pos > '9')
  163. continue;
  164. pin[len++] = *pos;
  165. if (len == 9) {
  166. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  167. return -1;
  168. }
  169. }
  170. if (len != 4 && len != 8) {
  171. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  172. return -1;
  173. }
  174. pin[len] = '\0';
  175. if (len == 8) {
  176. unsigned int pin_val;
  177. pin_val = atoi(pin);
  178. if (!wps_pin_valid(pin_val)) {
  179. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  180. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  181. if (os_snprintf_error(buflen, ret))
  182. return -1;
  183. return ret;
  184. }
  185. }
  186. ret = os_snprintf(buf, buflen, "%s", pin);
  187. if (os_snprintf_error(buflen, ret))
  188. return -1;
  189. return ret;
  190. }
  191. #ifdef CONFIG_WPS_NFC
  192. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  193. char *pos)
  194. {
  195. size_t len;
  196. struct wpabuf *buf;
  197. int ret;
  198. len = os_strlen(pos);
  199. if (len & 0x01)
  200. return -1;
  201. len /= 2;
  202. buf = wpabuf_alloc(len);
  203. if (buf == NULL)
  204. return -1;
  205. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  206. wpabuf_free(buf);
  207. return -1;
  208. }
  209. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  210. wpabuf_free(buf);
  211. return ret;
  212. }
  213. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  214. char *cmd, char *reply,
  215. size_t max_len)
  216. {
  217. int ndef;
  218. struct wpabuf *buf;
  219. int res;
  220. if (os_strcmp(cmd, "WPS") == 0)
  221. ndef = 0;
  222. else if (os_strcmp(cmd, "NDEF") == 0)
  223. ndef = 1;
  224. else
  225. return -1;
  226. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  227. if (buf == NULL)
  228. return -1;
  229. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  230. wpabuf_len(buf));
  231. reply[res++] = '\n';
  232. reply[res] = '\0';
  233. wpabuf_free(buf);
  234. return res;
  235. }
  236. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  237. char *reply, size_t max_len,
  238. int ndef)
  239. {
  240. struct wpabuf *buf;
  241. int res;
  242. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  243. if (buf == NULL)
  244. return -1;
  245. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  246. wpabuf_len(buf));
  247. reply[res++] = '\n';
  248. reply[res] = '\0';
  249. wpabuf_free(buf);
  250. return res;
  251. }
  252. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  253. char *cmd, char *reply,
  254. size_t max_len)
  255. {
  256. if (os_strcmp(cmd, "WPS") == 0)
  257. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  258. max_len, 0);
  259. if (os_strcmp(cmd, "NDEF") == 0)
  260. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  261. max_len, 1);
  262. if (os_strcmp(cmd, "enable") == 0)
  263. return hostapd_wps_nfc_token_enable(hapd);
  264. if (os_strcmp(cmd, "disable") == 0) {
  265. hostapd_wps_nfc_token_disable(hapd);
  266. return 0;
  267. }
  268. return -1;
  269. }
  270. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  271. char *cmd, char *reply,
  272. size_t max_len)
  273. {
  274. struct wpabuf *buf;
  275. int res;
  276. char *pos;
  277. int ndef;
  278. pos = os_strchr(cmd, ' ');
  279. if (pos == NULL)
  280. return -1;
  281. *pos++ = '\0';
  282. if (os_strcmp(cmd, "WPS") == 0)
  283. ndef = 0;
  284. else if (os_strcmp(cmd, "NDEF") == 0)
  285. ndef = 1;
  286. else
  287. return -1;
  288. if (os_strcmp(pos, "WPS-CR") == 0)
  289. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  290. else
  291. buf = NULL;
  292. if (buf == NULL)
  293. return -1;
  294. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  295. wpabuf_len(buf));
  296. reply[res++] = '\n';
  297. reply[res] = '\0';
  298. wpabuf_free(buf);
  299. return res;
  300. }
  301. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  302. char *cmd)
  303. {
  304. size_t len;
  305. struct wpabuf *req, *sel;
  306. int ret;
  307. char *pos, *role, *type, *pos2;
  308. role = cmd;
  309. pos = os_strchr(role, ' ');
  310. if (pos == NULL)
  311. return -1;
  312. *pos++ = '\0';
  313. type = pos;
  314. pos = os_strchr(type, ' ');
  315. if (pos == NULL)
  316. return -1;
  317. *pos++ = '\0';
  318. pos2 = os_strchr(pos, ' ');
  319. if (pos2 == NULL)
  320. return -1;
  321. *pos2++ = '\0';
  322. len = os_strlen(pos);
  323. if (len & 0x01)
  324. return -1;
  325. len /= 2;
  326. req = wpabuf_alloc(len);
  327. if (req == NULL)
  328. return -1;
  329. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  330. wpabuf_free(req);
  331. return -1;
  332. }
  333. len = os_strlen(pos2);
  334. if (len & 0x01) {
  335. wpabuf_free(req);
  336. return -1;
  337. }
  338. len /= 2;
  339. sel = wpabuf_alloc(len);
  340. if (sel == NULL) {
  341. wpabuf_free(req);
  342. return -1;
  343. }
  344. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  345. wpabuf_free(req);
  346. wpabuf_free(sel);
  347. return -1;
  348. }
  349. if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
  350. ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
  351. } else {
  352. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  353. "reported: role=%s type=%s", role, type);
  354. ret = -1;
  355. }
  356. wpabuf_free(req);
  357. wpabuf_free(sel);
  358. return ret;
  359. }
  360. #endif /* CONFIG_WPS_NFC */
  361. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  362. char *buf, size_t buflen)
  363. {
  364. int timeout = 300;
  365. char *pos;
  366. const char *pin_txt;
  367. pos = os_strchr(txt, ' ');
  368. if (pos)
  369. *pos++ = '\0';
  370. if (os_strcmp(txt, "disable") == 0) {
  371. hostapd_wps_ap_pin_disable(hapd);
  372. return os_snprintf(buf, buflen, "OK\n");
  373. }
  374. if (os_strcmp(txt, "random") == 0) {
  375. if (pos)
  376. timeout = atoi(pos);
  377. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  378. if (pin_txt == NULL)
  379. return -1;
  380. return os_snprintf(buf, buflen, "%s", pin_txt);
  381. }
  382. if (os_strcmp(txt, "get") == 0) {
  383. pin_txt = hostapd_wps_ap_pin_get(hapd);
  384. if (pin_txt == NULL)
  385. return -1;
  386. return os_snprintf(buf, buflen, "%s", pin_txt);
  387. }
  388. if (os_strcmp(txt, "set") == 0) {
  389. char *pin;
  390. if (pos == NULL)
  391. return -1;
  392. pin = pos;
  393. pos = os_strchr(pos, ' ');
  394. if (pos) {
  395. *pos++ = '\0';
  396. timeout = atoi(pos);
  397. }
  398. if (os_strlen(pin) > buflen)
  399. return -1;
  400. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  401. return -1;
  402. return os_snprintf(buf, buflen, "%s", pin);
  403. }
  404. return -1;
  405. }
  406. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  407. {
  408. char *pos;
  409. char *ssid, *auth, *encr = NULL, *key = NULL;
  410. ssid = txt;
  411. pos = os_strchr(txt, ' ');
  412. if (!pos)
  413. return -1;
  414. *pos++ = '\0';
  415. auth = pos;
  416. pos = os_strchr(pos, ' ');
  417. if (pos) {
  418. *pos++ = '\0';
  419. encr = pos;
  420. pos = os_strchr(pos, ' ');
  421. if (pos) {
  422. *pos++ = '\0';
  423. key = pos;
  424. }
  425. }
  426. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  427. }
  428. static const char * pbc_status_str(enum pbc_status status)
  429. {
  430. switch (status) {
  431. case WPS_PBC_STATUS_DISABLE:
  432. return "Disabled";
  433. case WPS_PBC_STATUS_ACTIVE:
  434. return "Active";
  435. case WPS_PBC_STATUS_TIMEOUT:
  436. return "Timed-out";
  437. case WPS_PBC_STATUS_OVERLAP:
  438. return "Overlap";
  439. default:
  440. return "Unknown";
  441. }
  442. }
  443. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  444. char *buf, size_t buflen)
  445. {
  446. int ret;
  447. char *pos, *end;
  448. pos = buf;
  449. end = buf + buflen;
  450. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  451. pbc_status_str(hapd->wps_stats.pbc_status));
  452. if (os_snprintf_error(end - pos, ret))
  453. return pos - buf;
  454. pos += ret;
  455. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  456. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  457. "Success":
  458. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  459. "Failed" : "None")));
  460. if (os_snprintf_error(end - pos, ret))
  461. return pos - buf;
  462. pos += ret;
  463. /* If status == Failure - Add possible Reasons */
  464. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  465. hapd->wps_stats.failure_reason > 0) {
  466. ret = os_snprintf(pos, end - pos,
  467. "Failure Reason: %s\n",
  468. wps_ei_str(hapd->wps_stats.failure_reason));
  469. if (os_snprintf_error(end - pos, ret))
  470. return pos - buf;
  471. pos += ret;
  472. }
  473. if (hapd->wps_stats.status) {
  474. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  475. MAC2STR(hapd->wps_stats.peer_addr));
  476. if (os_snprintf_error(end - pos, ret))
  477. return pos - buf;
  478. pos += ret;
  479. }
  480. return pos - buf;
  481. }
  482. #endif /* CONFIG_WPS */
  483. #ifdef CONFIG_HS20
  484. static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
  485. const char *cmd)
  486. {
  487. u8 addr[ETH_ALEN];
  488. const char *url;
  489. if (hwaddr_aton(cmd, addr))
  490. return -1;
  491. url = cmd + 17;
  492. if (*url == '\0') {
  493. url = NULL;
  494. } else {
  495. if (*url != ' ')
  496. return -1;
  497. url++;
  498. if (*url == '\0')
  499. url = NULL;
  500. }
  501. return hs20_send_wnm_notification(hapd, addr, 1, url);
  502. }
  503. static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
  504. const char *cmd)
  505. {
  506. u8 addr[ETH_ALEN];
  507. int code, reauth_delay, ret;
  508. const char *pos;
  509. size_t url_len;
  510. struct wpabuf *req;
  511. /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
  512. if (hwaddr_aton(cmd, addr))
  513. return -1;
  514. pos = os_strchr(cmd, ' ');
  515. if (pos == NULL)
  516. return -1;
  517. pos++;
  518. code = atoi(pos);
  519. pos = os_strchr(pos, ' ');
  520. if (pos == NULL)
  521. return -1;
  522. pos++;
  523. reauth_delay = atoi(pos);
  524. url_len = 0;
  525. pos = os_strchr(pos, ' ');
  526. if (pos) {
  527. pos++;
  528. url_len = os_strlen(pos);
  529. }
  530. req = wpabuf_alloc(4 + url_len);
  531. if (req == NULL)
  532. return -1;
  533. wpabuf_put_u8(req, code);
  534. wpabuf_put_le16(req, reauth_delay);
  535. wpabuf_put_u8(req, url_len);
  536. if (pos)
  537. wpabuf_put_data(req, pos, url_len);
  538. wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
  539. " to indicate imminent deauthentication (code=%d "
  540. "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
  541. ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
  542. wpabuf_free(req);
  543. return ret;
  544. }
  545. #endif /* CONFIG_HS20 */
  546. #ifdef CONFIG_INTERWORKING
  547. static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
  548. const char *cmd)
  549. {
  550. u8 qos_map_set[16 + 2 * 21], count = 0;
  551. const char *pos = cmd;
  552. int val, ret;
  553. for (;;) {
  554. if (count == sizeof(qos_map_set)) {
  555. wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
  556. return -1;
  557. }
  558. val = atoi(pos);
  559. if (val < 0 || val > 255) {
  560. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  561. return -1;
  562. }
  563. qos_map_set[count++] = val;
  564. pos = os_strchr(pos, ',');
  565. if (!pos)
  566. break;
  567. pos++;
  568. }
  569. if (count < 16 || count & 1) {
  570. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  571. return -1;
  572. }
  573. ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
  574. if (ret) {
  575. wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
  576. return -1;
  577. }
  578. os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
  579. hapd->conf->qos_map_set_len = count;
  580. return 0;
  581. }
  582. static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
  583. const char *cmd)
  584. {
  585. u8 addr[ETH_ALEN];
  586. struct sta_info *sta;
  587. struct wpabuf *buf;
  588. u8 *qos_map_set = hapd->conf->qos_map_set;
  589. u8 qos_map_set_len = hapd->conf->qos_map_set_len;
  590. int ret;
  591. if (!qos_map_set_len) {
  592. wpa_printf(MSG_INFO, "QoS Map Set is not set");
  593. return -1;
  594. }
  595. if (hwaddr_aton(cmd, addr))
  596. return -1;
  597. sta = ap_get_sta(hapd, addr);
  598. if (sta == NULL) {
  599. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  600. "for QoS Map Configuration message",
  601. MAC2STR(addr));
  602. return -1;
  603. }
  604. if (!sta->qos_map_enabled) {
  605. wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
  606. "support for QoS Map", MAC2STR(addr));
  607. return -1;
  608. }
  609. buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
  610. if (buf == NULL)
  611. return -1;
  612. wpabuf_put_u8(buf, WLAN_ACTION_QOS);
  613. wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
  614. /* QoS Map Set Element */
  615. wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
  616. wpabuf_put_u8(buf, qos_map_set_len);
  617. wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
  618. ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
  619. wpabuf_head(buf), wpabuf_len(buf));
  620. wpabuf_free(buf);
  621. return ret;
  622. }
  623. #endif /* CONFIG_INTERWORKING */
  624. #ifdef CONFIG_WNM_AP
  625. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  626. const char *cmd)
  627. {
  628. u8 addr[ETH_ALEN];
  629. int disassoc_timer;
  630. struct sta_info *sta;
  631. if (hwaddr_aton(cmd, addr))
  632. return -1;
  633. if (cmd[17] != ' ')
  634. return -1;
  635. disassoc_timer = atoi(cmd + 17);
  636. sta = ap_get_sta(hapd, addr);
  637. if (sta == NULL) {
  638. wpa_printf(MSG_DEBUG, "Station " MACSTR
  639. " not found for disassociation imminent message",
  640. MAC2STR(addr));
  641. return -1;
  642. }
  643. return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
  644. }
  645. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  646. const char *cmd)
  647. {
  648. u8 addr[ETH_ALEN];
  649. const char *url, *timerstr;
  650. int disassoc_timer;
  651. struct sta_info *sta;
  652. if (hwaddr_aton(cmd, addr))
  653. return -1;
  654. sta = ap_get_sta(hapd, addr);
  655. if (sta == NULL) {
  656. wpa_printf(MSG_DEBUG, "Station " MACSTR
  657. " not found for ESS disassociation imminent message",
  658. MAC2STR(addr));
  659. return -1;
  660. }
  661. timerstr = cmd + 17;
  662. if (*timerstr != ' ')
  663. return -1;
  664. timerstr++;
  665. disassoc_timer = atoi(timerstr);
  666. if (disassoc_timer < 0 || disassoc_timer > 65535)
  667. return -1;
  668. url = os_strchr(timerstr, ' ');
  669. if (url == NULL)
  670. return -1;
  671. url++;
  672. return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
  673. }
  674. static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
  675. const char *cmd)
  676. {
  677. u8 addr[ETH_ALEN];
  678. const char *pos, *end;
  679. int disassoc_timer = 0;
  680. struct sta_info *sta;
  681. u8 req_mode = 0, valid_int = 0x01;
  682. u8 bss_term_dur[12];
  683. char *url = NULL;
  684. int ret;
  685. u8 nei_rep[1000];
  686. int nei_len;
  687. u8 mbo[10];
  688. size_t mbo_len = 0;
  689. if (hwaddr_aton(cmd, addr)) {
  690. wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
  691. return -1;
  692. }
  693. sta = ap_get_sta(hapd, addr);
  694. if (sta == NULL) {
  695. wpa_printf(MSG_DEBUG, "Station " MACSTR
  696. " not found for BSS TM Request message",
  697. MAC2STR(addr));
  698. return -1;
  699. }
  700. pos = os_strstr(cmd, " disassoc_timer=");
  701. if (pos) {
  702. pos += 16;
  703. disassoc_timer = atoi(pos);
  704. if (disassoc_timer < 0 || disassoc_timer > 65535) {
  705. wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
  706. return -1;
  707. }
  708. }
  709. pos = os_strstr(cmd, " valid_int=");
  710. if (pos) {
  711. pos += 11;
  712. valid_int = atoi(pos);
  713. }
  714. pos = os_strstr(cmd, " bss_term=");
  715. if (pos) {
  716. pos += 10;
  717. req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
  718. /* TODO: TSF configurable/learnable */
  719. bss_term_dur[0] = 4; /* Subelement ID */
  720. bss_term_dur[1] = 10; /* Length */
  721. os_memset(bss_term_dur, 2, 8);
  722. end = os_strchr(pos, ',');
  723. if (end == NULL) {
  724. wpa_printf(MSG_DEBUG, "Invalid bss_term data");
  725. return -1;
  726. }
  727. end++;
  728. WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
  729. }
  730. nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep,
  731. sizeof(nei_rep));
  732. if (nei_len < 0)
  733. return -1;
  734. pos = os_strstr(cmd, " url=");
  735. if (pos) {
  736. size_t len;
  737. pos += 5;
  738. end = os_strchr(pos, ' ');
  739. if (end)
  740. len = end - pos;
  741. else
  742. len = os_strlen(pos);
  743. url = os_malloc(len + 1);
  744. if (url == NULL)
  745. return -1;
  746. os_memcpy(url, pos, len);
  747. url[len] = '\0';
  748. req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
  749. }
  750. if (os_strstr(cmd, " pref=1"))
  751. req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
  752. if (os_strstr(cmd, " abridged=1"))
  753. req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
  754. if (os_strstr(cmd, " disassoc_imminent=1"))
  755. req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
  756. #ifdef CONFIG_MBO
  757. pos = os_strstr(cmd, "mbo=");
  758. if (pos) {
  759. unsigned int mbo_reason, cell_pref, reassoc_delay;
  760. u8 *mbo_pos = mbo;
  761. ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
  762. &reassoc_delay, &cell_pref);
  763. if (ret != 3) {
  764. wpa_printf(MSG_DEBUG,
  765. "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
  766. ret = -1;
  767. goto fail;
  768. }
  769. if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
  770. wpa_printf(MSG_DEBUG,
  771. "Invalid MBO transition reason code %u",
  772. mbo_reason);
  773. ret = -1;
  774. goto fail;
  775. }
  776. /* Valid values for Cellular preference are: 0, 1, 255 */
  777. if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
  778. wpa_printf(MSG_DEBUG,
  779. "Invalid MBO cellular capability %u",
  780. cell_pref);
  781. ret = -1;
  782. goto fail;
  783. }
  784. if (reassoc_delay > 65535 ||
  785. (reassoc_delay &&
  786. !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
  787. wpa_printf(MSG_DEBUG,
  788. "MBO: Assoc retry delay is only valid in disassoc imminent mode");
  789. ret = -1;
  790. goto fail;
  791. }
  792. *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
  793. *mbo_pos++ = 1;
  794. *mbo_pos++ = mbo_reason;
  795. *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
  796. *mbo_pos++ = 1;
  797. *mbo_pos++ = cell_pref;
  798. if (reassoc_delay) {
  799. *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
  800. *mbo_pos++ = 2;
  801. WPA_PUT_LE16(mbo_pos, reassoc_delay);
  802. mbo_pos += 2;
  803. }
  804. mbo_len = mbo_pos - mbo;
  805. }
  806. #endif /* CONFIG_MBO */
  807. ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
  808. valid_int, bss_term_dur, url,
  809. nei_len ? nei_rep : NULL, nei_len,
  810. mbo_len ? mbo : NULL, mbo_len);
  811. #ifdef CONFIG_MBO
  812. fail:
  813. #endif /* CONFIG_MBO */
  814. os_free(url);
  815. return ret;
  816. }
  817. #endif /* CONFIG_WNM_AP */
  818. static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
  819. char *buf, size_t buflen)
  820. {
  821. int ret = 0;
  822. char *pos, *end;
  823. pos = buf;
  824. end = buf + buflen;
  825. WPA_ASSERT(hapd->conf->wpa_key_mgmt);
  826. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  827. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  828. if (os_snprintf_error(end - pos, ret))
  829. return pos - buf;
  830. pos += ret;
  831. }
  832. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  833. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  834. if (os_snprintf_error(end - pos, ret))
  835. return pos - buf;
  836. pos += ret;
  837. }
  838. #ifdef CONFIG_IEEE80211R_AP
  839. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  840. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  841. if (os_snprintf_error(end - pos, ret))
  842. return pos - buf;
  843. pos += ret;
  844. }
  845. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  846. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  847. if (os_snprintf_error(end - pos, ret))
  848. return pos - buf;
  849. pos += ret;
  850. }
  851. #ifdef CONFIG_SAE
  852. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  853. ret = os_snprintf(pos, end - pos, "FT-SAE ");
  854. if (os_snprintf_error(end - pos, ret))
  855. return pos - buf;
  856. pos += ret;
  857. }
  858. #endif /* CONFIG_SAE */
  859. #ifdef CONFIG_FILS
  860. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
  861. ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
  862. if (os_snprintf_error(end - pos, ret))
  863. return pos - buf;
  864. pos += ret;
  865. }
  866. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
  867. ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
  868. if (os_snprintf_error(end - pos, ret))
  869. return pos - buf;
  870. pos += ret;
  871. }
  872. #endif /* CONFIG_FILS */
  873. #endif /* CONFIG_IEEE80211R_AP */
  874. #ifdef CONFIG_IEEE80211W
  875. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  876. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  877. if (os_snprintf_error(end - pos, ret))
  878. return pos - buf;
  879. pos += ret;
  880. }
  881. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  882. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  883. if (os_snprintf_error(end - pos, ret))
  884. return pos - buf;
  885. pos += ret;
  886. }
  887. #endif /* CONFIG_IEEE80211W */
  888. #ifdef CONFIG_SAE
  889. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
  890. ret = os_snprintf(pos, end - pos, "SAE ");
  891. if (os_snprintf_error(end - pos, ret))
  892. return pos - buf;
  893. pos += ret;
  894. }
  895. #endif /* CONFIG_SAE */
  896. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  897. ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
  898. if (os_snprintf_error(end - pos, ret))
  899. return pos - buf;
  900. pos += ret;
  901. }
  902. if (hapd->conf->wpa_key_mgmt &
  903. WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  904. ret = os_snprintf(pos, end - pos,
  905. "WPA-EAP-SUITE-B-192 ");
  906. if (os_snprintf_error(end - pos, ret))
  907. return pos - buf;
  908. pos += ret;
  909. }
  910. #ifdef CONFIG_FILS
  911. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
  912. ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
  913. if (os_snprintf_error(end - pos, ret))
  914. return pos - buf;
  915. pos += ret;
  916. }
  917. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
  918. ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
  919. if (os_snprintf_error(end - pos, ret))
  920. return pos - buf;
  921. pos += ret;
  922. }
  923. #endif /* CONFIG_FILS */
  924. #ifdef CONFIG_OWE
  925. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
  926. ret = os_snprintf(pos, end - pos, "OWE ");
  927. if (os_snprintf_error(end - pos, ret))
  928. return pos - buf;
  929. pos += ret;
  930. }
  931. #endif /* CONFIG_OWE */
  932. #ifdef CONFIG_DPP
  933. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
  934. ret = os_snprintf(pos, end - pos, "DPP ");
  935. if (os_snprintf_error(end - pos, ret))
  936. return pos - buf;
  937. pos += ret;
  938. }
  939. #endif /* CONFIG_DPP */
  940. if (pos > buf && *(pos - 1) == ' ') {
  941. *(pos - 1) = '\0';
  942. pos--;
  943. }
  944. return pos - buf;
  945. }
  946. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  947. char *buf, size_t buflen)
  948. {
  949. int ret;
  950. char *pos, *end;
  951. pos = buf;
  952. end = buf + buflen;
  953. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  954. "ssid=%s\n",
  955. MAC2STR(hapd->own_addr),
  956. wpa_ssid_txt(hapd->conf->ssid.ssid,
  957. hapd->conf->ssid.ssid_len));
  958. if (os_snprintf_error(end - pos, ret))
  959. return pos - buf;
  960. pos += ret;
  961. #ifdef CONFIG_WPS
  962. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  963. hapd->conf->wps_state == 0 ? "disabled" :
  964. (hapd->conf->wps_state == 1 ? "not configured" :
  965. "configured"));
  966. if (os_snprintf_error(end - pos, ret))
  967. return pos - buf;
  968. pos += ret;
  969. if (hapd->conf->wps_state && hapd->conf->wpa &&
  970. hapd->conf->ssid.wpa_passphrase) {
  971. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  972. hapd->conf->ssid.wpa_passphrase);
  973. if (os_snprintf_error(end - pos, ret))
  974. return pos - buf;
  975. pos += ret;
  976. }
  977. if (hapd->conf->wps_state && hapd->conf->wpa &&
  978. hapd->conf->ssid.wpa_psk &&
  979. hapd->conf->ssid.wpa_psk->group) {
  980. char hex[PMK_LEN * 2 + 1];
  981. wpa_snprintf_hex(hex, sizeof(hex),
  982. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  983. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  984. if (os_snprintf_error(end - pos, ret))
  985. return pos - buf;
  986. pos += ret;
  987. }
  988. #endif /* CONFIG_WPS */
  989. if (hapd->conf->wpa) {
  990. ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
  991. if (os_snprintf_error(end - pos, ret))
  992. return pos - buf;
  993. pos += ret;
  994. }
  995. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  996. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  997. if (os_snprintf_error(end - pos, ret))
  998. return pos - buf;
  999. pos += ret;
  1000. pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
  1001. ret = os_snprintf(pos, end - pos, "\n");
  1002. if (os_snprintf_error(end - pos, ret))
  1003. return pos - buf;
  1004. pos += ret;
  1005. }
  1006. if (hapd->conf->wpa) {
  1007. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  1008. wpa_cipher_txt(hapd->conf->wpa_group));
  1009. if (os_snprintf_error(end - pos, ret))
  1010. return pos - buf;
  1011. pos += ret;
  1012. }
  1013. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  1014. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  1015. if (os_snprintf_error(end - pos, ret))
  1016. return pos - buf;
  1017. pos += ret;
  1018. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  1019. " ");
  1020. if (ret < 0)
  1021. return pos - buf;
  1022. pos += ret;
  1023. ret = os_snprintf(pos, end - pos, "\n");
  1024. if (os_snprintf_error(end - pos, ret))
  1025. return pos - buf;
  1026. pos += ret;
  1027. }
  1028. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  1029. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  1030. if (os_snprintf_error(end - pos, ret))
  1031. return pos - buf;
  1032. pos += ret;
  1033. ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
  1034. " ");
  1035. if (ret < 0)
  1036. return pos - buf;
  1037. pos += ret;
  1038. ret = os_snprintf(pos, end - pos, "\n");
  1039. if (os_snprintf_error(end - pos, ret))
  1040. return pos - buf;
  1041. pos += ret;
  1042. }
  1043. return pos - buf;
  1044. }
  1045. static void hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
  1046. {
  1047. struct sta_info *sta;
  1048. struct vlan_description vlan_id;
  1049. if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
  1050. return;
  1051. for (sta = hapd->sta_list; sta; sta = sta->next) {
  1052. if (!hostapd_maclist_found(hapd->conf->accept_mac,
  1053. hapd->conf->num_accept_mac,
  1054. sta->addr, &vlan_id) ||
  1055. (vlan_id.notempty &&
  1056. vlan_compare(&vlan_id, sta->vlan_desc)))
  1057. ap_sta_disconnect(hapd, sta, sta->addr,
  1058. WLAN_REASON_UNSPECIFIED);
  1059. }
  1060. }
  1061. static void hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
  1062. {
  1063. struct sta_info *sta;
  1064. struct vlan_description vlan_id;
  1065. for (sta = hapd->sta_list; sta; sta = sta->next) {
  1066. if (hostapd_maclist_found(hapd->conf->deny_mac,
  1067. hapd->conf->num_deny_mac, sta->addr,
  1068. &vlan_id) &&
  1069. (!vlan_id.notempty ||
  1070. !vlan_compare(&vlan_id, sta->vlan_desc)))
  1071. ap_sta_disconnect(hapd, sta, sta->addr,
  1072. WLAN_REASON_UNSPECIFIED);
  1073. }
  1074. }
  1075. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  1076. {
  1077. char *value;
  1078. int ret = 0;
  1079. value = os_strchr(cmd, ' ');
  1080. if (value == NULL)
  1081. return -1;
  1082. *value++ = '\0';
  1083. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  1084. if (0) {
  1085. #ifdef CONFIG_WPS_TESTING
  1086. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  1087. long int val;
  1088. val = strtol(value, NULL, 0);
  1089. if (val < 0 || val > 0xff) {
  1090. ret = -1;
  1091. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  1092. "wps_version_number %ld", val);
  1093. } else {
  1094. wps_version_number = val;
  1095. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  1096. "version %u.%u",
  1097. (wps_version_number & 0xf0) >> 4,
  1098. wps_version_number & 0x0f);
  1099. hostapd_wps_update_ie(hapd);
  1100. }
  1101. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  1102. wps_testing_dummy_cred = atoi(value);
  1103. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  1104. wps_testing_dummy_cred);
  1105. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  1106. wps_corrupt_pkhash = atoi(value);
  1107. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  1108. wps_corrupt_pkhash);
  1109. #endif /* CONFIG_WPS_TESTING */
  1110. #ifdef CONFIG_TESTING_OPTIONS
  1111. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  1112. hapd->ext_mgmt_frame_handling = atoi(value);
  1113. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  1114. hapd->ext_eapol_frame_io = atoi(value);
  1115. #ifdef CONFIG_DPP
  1116. } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
  1117. os_free(hapd->dpp_config_obj_override);
  1118. hapd->dpp_config_obj_override = os_strdup(value);
  1119. } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
  1120. os_free(hapd->dpp_discovery_override);
  1121. hapd->dpp_discovery_override = os_strdup(value);
  1122. } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
  1123. os_free(hapd->dpp_groups_override);
  1124. hapd->dpp_groups_override = os_strdup(value);
  1125. } else if (os_strcasecmp(cmd,
  1126. "dpp_ignore_netaccesskey_mismatch") == 0) {
  1127. hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
  1128. } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
  1129. dpp_test = atoi(value);
  1130. #endif /* CONFIG_DPP */
  1131. #endif /* CONFIG_TESTING_OPTIONS */
  1132. #ifdef CONFIG_MBO
  1133. } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
  1134. int val;
  1135. if (!hapd->conf->mbo_enabled)
  1136. return -1;
  1137. val = atoi(value);
  1138. if (val < 0 || val > 1)
  1139. return -1;
  1140. hapd->mbo_assoc_disallow = val;
  1141. ieee802_11_update_beacons(hapd->iface);
  1142. /*
  1143. * TODO: Need to configure drivers that do AP MLME offload with
  1144. * disallowing station logic.
  1145. */
  1146. #endif /* CONFIG_MBO */
  1147. #ifdef CONFIG_DPP
  1148. } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
  1149. os_free(hapd->dpp_configurator_params);
  1150. hapd->dpp_configurator_params = os_strdup(value);
  1151. #endif /* CONFIG_DPP */
  1152. } else {
  1153. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  1154. if (ret)
  1155. return ret;
  1156. if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
  1157. hostapd_disassoc_deny_mac(hapd);
  1158. } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
  1159. hostapd_disassoc_accept_mac(hapd);
  1160. }
  1161. }
  1162. return ret;
  1163. }
  1164. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  1165. char *buf, size_t buflen)
  1166. {
  1167. int res;
  1168. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  1169. if (os_strcmp(cmd, "version") == 0) {
  1170. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  1171. if (os_snprintf_error(buflen, res))
  1172. return -1;
  1173. return res;
  1174. } else if (os_strcmp(cmd, "tls_library") == 0) {
  1175. res = tls_get_library_version(buf, buflen);
  1176. if (os_snprintf_error(buflen, res))
  1177. return -1;
  1178. return res;
  1179. }
  1180. return -1;
  1181. }
  1182. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  1183. {
  1184. if (hostapd_enable_iface(iface) < 0) {
  1185. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  1186. return -1;
  1187. }
  1188. return 0;
  1189. }
  1190. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  1191. {
  1192. if (hostapd_reload_iface(iface) < 0) {
  1193. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  1194. return -1;
  1195. }
  1196. return 0;
  1197. }
  1198. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  1199. {
  1200. if (hostapd_disable_iface(iface) < 0) {
  1201. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  1202. return -1;
  1203. }
  1204. return 0;
  1205. }
  1206. #ifdef CONFIG_TESTING_OPTIONS
  1207. static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
  1208. {
  1209. union wpa_event_data data;
  1210. char *pos, *param;
  1211. enum wpa_event_type event;
  1212. wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
  1213. os_memset(&data, 0, sizeof(data));
  1214. param = os_strchr(cmd, ' ');
  1215. if (param == NULL)
  1216. return -1;
  1217. *param++ = '\0';
  1218. if (os_strcmp(cmd, "DETECTED") == 0)
  1219. event = EVENT_DFS_RADAR_DETECTED;
  1220. else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
  1221. event = EVENT_DFS_CAC_FINISHED;
  1222. else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
  1223. event = EVENT_DFS_CAC_ABORTED;
  1224. else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
  1225. event = EVENT_DFS_NOP_FINISHED;
  1226. else {
  1227. wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
  1228. cmd);
  1229. return -1;
  1230. }
  1231. pos = os_strstr(param, "freq=");
  1232. if (pos)
  1233. data.dfs_event.freq = atoi(pos + 5);
  1234. pos = os_strstr(param, "ht_enabled=1");
  1235. if (pos)
  1236. data.dfs_event.ht_enabled = 1;
  1237. pos = os_strstr(param, "chan_offset=");
  1238. if (pos)
  1239. data.dfs_event.chan_offset = atoi(pos + 12);
  1240. pos = os_strstr(param, "chan_width=");
  1241. if (pos)
  1242. data.dfs_event.chan_width = atoi(pos + 11);
  1243. pos = os_strstr(param, "cf1=");
  1244. if (pos)
  1245. data.dfs_event.cf1 = atoi(pos + 4);
  1246. pos = os_strstr(param, "cf2=");
  1247. if (pos)
  1248. data.dfs_event.cf2 = atoi(pos + 4);
  1249. wpa_supplicant_event(hapd, event, &data);
  1250. return 0;
  1251. }
  1252. static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
  1253. {
  1254. size_t len;
  1255. u8 *buf;
  1256. int res;
  1257. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  1258. len = os_strlen(cmd);
  1259. if (len & 1)
  1260. return -1;
  1261. len /= 2;
  1262. buf = os_malloc(len);
  1263. if (buf == NULL)
  1264. return -1;
  1265. if (hexstr2bin(cmd, buf, len) < 0) {
  1266. os_free(buf);
  1267. return -1;
  1268. }
  1269. res = hostapd_drv_send_mlme(hapd, buf, len, 0);
  1270. os_free(buf);
  1271. return res;
  1272. }
  1273. static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
  1274. char *cmd)
  1275. {
  1276. char *pos, *param;
  1277. size_t len;
  1278. u8 *buf;
  1279. int stype = 0, ok = 0;
  1280. union wpa_event_data event;
  1281. if (!hapd->ext_mgmt_frame_handling)
  1282. return -1;
  1283. /* stype=<val> ok=<0/1> buf=<frame hexdump> */
  1284. wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
  1285. pos = cmd;
  1286. param = os_strstr(pos, "stype=");
  1287. if (param) {
  1288. param += 6;
  1289. stype = atoi(param);
  1290. }
  1291. param = os_strstr(pos, " ok=");
  1292. if (param) {
  1293. param += 4;
  1294. ok = atoi(param);
  1295. }
  1296. param = os_strstr(pos, " buf=");
  1297. if (!param)
  1298. return -1;
  1299. param += 5;
  1300. len = os_strlen(param);
  1301. if (len & 1)
  1302. return -1;
  1303. len /= 2;
  1304. buf = os_malloc(len);
  1305. if (!buf || hexstr2bin(param, buf, len) < 0) {
  1306. os_free(buf);
  1307. return -1;
  1308. }
  1309. os_memset(&event, 0, sizeof(event));
  1310. event.tx_status.type = WLAN_FC_TYPE_MGMT;
  1311. event.tx_status.data = buf;
  1312. event.tx_status.data_len = len;
  1313. event.tx_status.stype = stype;
  1314. event.tx_status.ack = ok;
  1315. hapd->ext_mgmt_frame_handling = 0;
  1316. wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
  1317. hapd->ext_mgmt_frame_handling = 1;
  1318. os_free(buf);
  1319. return 0;
  1320. }
  1321. static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
  1322. char *cmd)
  1323. {
  1324. char *pos, *param;
  1325. size_t len;
  1326. u8 *buf;
  1327. int freq = 0, datarate = 0, ssi_signal = 0;
  1328. union wpa_event_data event;
  1329. if (!hapd->ext_mgmt_frame_handling)
  1330. return -1;
  1331. /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
  1332. wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
  1333. pos = cmd;
  1334. param = os_strstr(pos, "freq=");
  1335. if (param) {
  1336. param += 5;
  1337. freq = atoi(param);
  1338. }
  1339. param = os_strstr(pos, " datarate=");
  1340. if (param) {
  1341. param += 10;
  1342. datarate = atoi(param);
  1343. }
  1344. param = os_strstr(pos, " ssi_signal=");
  1345. if (param) {
  1346. param += 12;
  1347. ssi_signal = atoi(param);
  1348. }
  1349. param = os_strstr(pos, " frame=");
  1350. if (param == NULL)
  1351. return -1;
  1352. param += 7;
  1353. len = os_strlen(param);
  1354. if (len & 1)
  1355. return -1;
  1356. len /= 2;
  1357. buf = os_malloc(len);
  1358. if (buf == NULL)
  1359. return -1;
  1360. if (hexstr2bin(param, buf, len) < 0) {
  1361. os_free(buf);
  1362. return -1;
  1363. }
  1364. os_memset(&event, 0, sizeof(event));
  1365. event.rx_mgmt.freq = freq;
  1366. event.rx_mgmt.frame = buf;
  1367. event.rx_mgmt.frame_len = len;
  1368. event.rx_mgmt.ssi_signal = ssi_signal;
  1369. event.rx_mgmt.datarate = datarate;
  1370. hapd->ext_mgmt_frame_handling = 0;
  1371. wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
  1372. hapd->ext_mgmt_frame_handling = 1;
  1373. os_free(buf);
  1374. return 0;
  1375. }
  1376. static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
  1377. {
  1378. char *pos;
  1379. u8 src[ETH_ALEN], *buf;
  1380. int used;
  1381. size_t len;
  1382. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  1383. pos = cmd;
  1384. used = hwaddr_aton2(pos, src);
  1385. if (used < 0)
  1386. return -1;
  1387. pos += used;
  1388. while (*pos == ' ')
  1389. pos++;
  1390. len = os_strlen(pos);
  1391. if (len & 1)
  1392. return -1;
  1393. len /= 2;
  1394. buf = os_malloc(len);
  1395. if (buf == NULL)
  1396. return -1;
  1397. if (hexstr2bin(pos, buf, len) < 0) {
  1398. os_free(buf);
  1399. return -1;
  1400. }
  1401. ieee802_1x_receive(hapd, src, buf, len);
  1402. os_free(buf);
  1403. return 0;
  1404. }
  1405. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  1406. {
  1407. size_t i;
  1408. u32 sum = 0;
  1409. const u16 *pos = buf;
  1410. for (i = 0; i < len / 2; i++)
  1411. sum += *pos++;
  1412. while (sum >> 16)
  1413. sum = (sum & 0xffff) + (sum >> 16);
  1414. return sum ^ 0xffff;
  1415. }
  1416. #define HWSIM_PACKETLEN 1500
  1417. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  1418. static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  1419. size_t len)
  1420. {
  1421. struct hostapd_data *hapd = ctx;
  1422. const struct ether_header *eth;
  1423. struct iphdr ip;
  1424. const u8 *pos;
  1425. unsigned int i;
  1426. if (len != HWSIM_PACKETLEN)
  1427. return;
  1428. eth = (const struct ether_header *) buf;
  1429. os_memcpy(&ip, eth + 1, sizeof(ip));
  1430. pos = &buf[sizeof(*eth) + sizeof(ip)];
  1431. if (ip.ihl != 5 || ip.version != 4 ||
  1432. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  1433. return;
  1434. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  1435. if (*pos != (u8) i)
  1436. return;
  1437. pos++;
  1438. }
  1439. wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  1440. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  1441. }
  1442. static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
  1443. char *cmd)
  1444. {
  1445. int enabled = atoi(cmd);
  1446. char *pos;
  1447. const char *ifname;
  1448. if (!enabled) {
  1449. if (hapd->l2_test) {
  1450. l2_packet_deinit(hapd->l2_test);
  1451. hapd->l2_test = NULL;
  1452. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  1453. "test data: Disabled");
  1454. }
  1455. return 0;
  1456. }
  1457. if (hapd->l2_test)
  1458. return 0;
  1459. pos = os_strstr(cmd, " ifname=");
  1460. if (pos)
  1461. ifname = pos + 8;
  1462. else
  1463. ifname = hapd->conf->iface;
  1464. hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
  1465. ETHERTYPE_IP, hostapd_data_test_rx,
  1466. hapd, 1);
  1467. if (hapd->l2_test == NULL)
  1468. return -1;
  1469. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
  1470. return 0;
  1471. }
  1472. static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
  1473. {
  1474. u8 dst[ETH_ALEN], src[ETH_ALEN];
  1475. char *pos;
  1476. int used;
  1477. long int val;
  1478. u8 tos;
  1479. u8 buf[2 + HWSIM_PACKETLEN];
  1480. struct ether_header *eth;
  1481. struct iphdr *ip;
  1482. u8 *dpos;
  1483. unsigned int i;
  1484. if (hapd->l2_test == NULL)
  1485. return -1;
  1486. /* format: <dst> <src> <tos> */
  1487. pos = cmd;
  1488. used = hwaddr_aton2(pos, dst);
  1489. if (used < 0)
  1490. return -1;
  1491. pos += used;
  1492. while (*pos == ' ')
  1493. pos++;
  1494. used = hwaddr_aton2(pos, src);
  1495. if (used < 0)
  1496. return -1;
  1497. pos += used;
  1498. val = strtol(pos, NULL, 0);
  1499. if (val < 0 || val > 0xff)
  1500. return -1;
  1501. tos = val;
  1502. eth = (struct ether_header *) &buf[2];
  1503. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  1504. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  1505. eth->ether_type = htons(ETHERTYPE_IP);
  1506. ip = (struct iphdr *) (eth + 1);
  1507. os_memset(ip, 0, sizeof(*ip));
  1508. ip->ihl = 5;
  1509. ip->version = 4;
  1510. ip->ttl = 64;
  1511. ip->tos = tos;
  1512. ip->tot_len = htons(HWSIM_IP_LEN);
  1513. ip->protocol = 1;
  1514. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  1515. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  1516. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  1517. dpos = (u8 *) (ip + 1);
  1518. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  1519. *dpos++ = i;
  1520. if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
  1521. HWSIM_PACKETLEN) < 0)
  1522. return -1;
  1523. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
  1524. " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  1525. return 0;
  1526. }
  1527. static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
  1528. char *cmd)
  1529. {
  1530. u8 *buf;
  1531. struct ether_header *eth;
  1532. struct l2_packet_data *l2 = NULL;
  1533. size_t len;
  1534. u16 ethertype;
  1535. int res = -1;
  1536. const char *ifname = hapd->conf->iface;
  1537. if (os_strncmp(cmd, "ifname=", 7) == 0) {
  1538. cmd += 7;
  1539. ifname = cmd;
  1540. cmd = os_strchr(cmd, ' ');
  1541. if (cmd == NULL)
  1542. return -1;
  1543. *cmd++ = '\0';
  1544. }
  1545. len = os_strlen(cmd);
  1546. if (len & 1 || len < ETH_HLEN * 2)
  1547. return -1;
  1548. len /= 2;
  1549. buf = os_malloc(len);
  1550. if (buf == NULL)
  1551. return -1;
  1552. if (hexstr2bin(cmd, buf, len) < 0)
  1553. goto done;
  1554. eth = (struct ether_header *) buf;
  1555. ethertype = ntohs(eth->ether_type);
  1556. l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
  1557. hostapd_data_test_rx, hapd, 1);
  1558. if (l2 == NULL)
  1559. goto done;
  1560. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  1561. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
  1562. done:
  1563. if (l2)
  1564. l2_packet_deinit(l2);
  1565. os_free(buf);
  1566. return res < 0 ? -1 : 0;
  1567. }
  1568. static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
  1569. {
  1570. #ifdef WPA_TRACE_BFD
  1571. char *pos;
  1572. wpa_trace_fail_after = atoi(cmd);
  1573. pos = os_strchr(cmd, ':');
  1574. if (pos) {
  1575. pos++;
  1576. os_strlcpy(wpa_trace_fail_func, pos,
  1577. sizeof(wpa_trace_fail_func));
  1578. } else {
  1579. wpa_trace_fail_after = 0;
  1580. }
  1581. return 0;
  1582. #else /* WPA_TRACE_BFD */
  1583. return -1;
  1584. #endif /* WPA_TRACE_BFD */
  1585. }
  1586. static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
  1587. char *buf, size_t buflen)
  1588. {
  1589. #ifdef WPA_TRACE_BFD
  1590. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  1591. wpa_trace_fail_func);
  1592. #else /* WPA_TRACE_BFD */
  1593. return -1;
  1594. #endif /* WPA_TRACE_BFD */
  1595. }
  1596. static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
  1597. {
  1598. #ifdef WPA_TRACE_BFD
  1599. char *pos;
  1600. wpa_trace_test_fail_after = atoi(cmd);
  1601. pos = os_strchr(cmd, ':');
  1602. if (pos) {
  1603. pos++;
  1604. os_strlcpy(wpa_trace_test_fail_func, pos,
  1605. sizeof(wpa_trace_test_fail_func));
  1606. } else {
  1607. wpa_trace_test_fail_after = 0;
  1608. }
  1609. return 0;
  1610. #else /* WPA_TRACE_BFD */
  1611. return -1;
  1612. #endif /* WPA_TRACE_BFD */
  1613. }
  1614. static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
  1615. char *buf, size_t buflen)
  1616. {
  1617. #ifdef WPA_TRACE_BFD
  1618. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  1619. wpa_trace_test_fail_func);
  1620. #else /* WPA_TRACE_BFD */
  1621. return -1;
  1622. #endif /* WPA_TRACE_BFD */
  1623. }
  1624. static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
  1625. {
  1626. struct sta_info *sta;
  1627. u8 addr[ETH_ALEN];
  1628. u8 zero[WPA_TK_MAX_LEN];
  1629. os_memset(zero, 0, sizeof(zero));
  1630. if (hwaddr_aton(cmd, addr))
  1631. return -1;
  1632. #ifdef CONFIG_IEEE80211W
  1633. if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
  1634. if (hapd->last_igtk_alg == WPA_ALG_NONE)
  1635. return -1;
  1636. wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
  1637. /* First, use a zero key to avoid any possible duplicate key
  1638. * avoidance in the driver. */
  1639. if (hostapd_drv_set_key(hapd->conf->iface, hapd,
  1640. hapd->last_igtk_alg,
  1641. broadcast_ether_addr,
  1642. hapd->last_igtk_key_idx, 1, NULL, 0,
  1643. zero, hapd->last_igtk_len) < 0)
  1644. return -1;
  1645. /* Set the previously configured key to reset its TSC */
  1646. return hostapd_drv_set_key(hapd->conf->iface, hapd,
  1647. hapd->last_igtk_alg,
  1648. broadcast_ether_addr,
  1649. hapd->last_igtk_key_idx, 1, NULL, 0,
  1650. hapd->last_igtk,
  1651. hapd->last_igtk_len);
  1652. }
  1653. #endif /* CONFIG_IEEE80211W */
  1654. if (is_broadcast_ether_addr(addr)) {
  1655. if (hapd->last_gtk_alg == WPA_ALG_NONE)
  1656. return -1;
  1657. #ifdef KRACK_TEST_CLIENT
  1658. poc_log(NULL, "Reset PN for GTK\n");
  1659. #else
  1660. wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
  1661. #endif
  1662. /* First, use a zero key to avoid any possible duplicate key
  1663. * avoidance in the driver. */
  1664. if (hostapd_drv_set_key(hapd->conf->iface, hapd,
  1665. hapd->last_gtk_alg,
  1666. broadcast_ether_addr,
  1667. hapd->last_gtk_key_idx, 1, NULL, 0,
  1668. zero, hapd->last_gtk_len) < 0)
  1669. return -1;
  1670. /* Set the previously configured key to reset its TSC */
  1671. return hostapd_drv_set_key(hapd->conf->iface, hapd,
  1672. hapd->last_gtk_alg,
  1673. broadcast_ether_addr,
  1674. hapd->last_gtk_key_idx, 1, NULL, 0,
  1675. hapd->last_gtk, hapd->last_gtk_len);
  1676. }
  1677. sta = ap_get_sta(hapd, addr);
  1678. if (!sta)
  1679. return -1;
  1680. if (sta->last_tk_alg == WPA_ALG_NONE)
  1681. return -1;
  1682. #ifdef KRACK_TEST_CLIENT
  1683. poc_log(sta->addr, "Reset PN\n");
  1684. #else
  1685. wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
  1686. MAC2STR(sta->addr));
  1687. #endif
  1688. /* First, use a zero key to avoid any possible duplicate key avoidance
  1689. * in the driver. */
  1690. if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
  1691. sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
  1692. zero, sta->last_tk_len) < 0)
  1693. return -1;
  1694. /* Set the previously configured key to reset its TSC/RSC */
  1695. return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
  1696. sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
  1697. sta->last_tk, sta->last_tk_len);
  1698. }
  1699. static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
  1700. {
  1701. u8 addr[ETH_ALEN];
  1702. const char *pos = cmd;
  1703. enum wpa_alg alg;
  1704. int idx, set_tx;
  1705. u8 seq[6], key[WPA_TK_MAX_LEN];
  1706. size_t key_len;
  1707. /* parameters: alg addr idx set_tx seq key */
  1708. alg = atoi(pos);
  1709. pos = os_strchr(pos, ' ');
  1710. if (!pos)
  1711. return -1;
  1712. pos++;
  1713. if (hwaddr_aton(pos, addr))
  1714. return -1;
  1715. pos += 17;
  1716. if (*pos != ' ')
  1717. return -1;
  1718. pos++;
  1719. idx = atoi(pos);
  1720. pos = os_strchr(pos, ' ');
  1721. if (!pos)
  1722. return -1;
  1723. pos++;
  1724. set_tx = atoi(pos);
  1725. pos = os_strchr(pos, ' ');
  1726. if (!pos)
  1727. return -1;
  1728. pos++;
  1729. if (hexstr2bin(pos, seq, sizeof(6)) < 0)
  1730. return -1;
  1731. pos += 2 * 6;
  1732. if (*pos != ' ')
  1733. return -1;
  1734. pos++;
  1735. key_len = os_strlen(pos) / 2;
  1736. if (hexstr2bin(pos, key, key_len) < 0)
  1737. return -1;
  1738. wpa_printf(MSG_INFO, "TESTING: Set key");
  1739. return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx,
  1740. set_tx, seq, 6, key, key_len);
  1741. }
  1742. static void restore_tk(void *ctx1, void *ctx2)
  1743. {
  1744. struct hostapd_data *hapd = ctx1;
  1745. struct sta_info *sta = ctx2;
  1746. wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
  1747. MAC2STR(sta->addr));
  1748. /* This does not really restore the TSC properly, so this will result
  1749. * in replay protection issues for now since there is no clean way of
  1750. * preventing encryption of a single EAPOL frame. */
  1751. hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
  1752. sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
  1753. sta->last_tk, sta->last_tk_len);
  1754. }
  1755. static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
  1756. {
  1757. struct sta_info *sta;
  1758. u8 addr[ETH_ALEN];
  1759. int plain = os_strstr(cmd, "plaintext") != NULL;
  1760. if (hwaddr_aton(cmd, addr))
  1761. return -1;
  1762. sta = ap_get_sta(hapd, addr);
  1763. if (!sta || !sta->wpa_sm)
  1764. return -1;
  1765. if (plain && sta->last_tk_alg == WPA_ALG_NONE)
  1766. plain = 0; /* no need for special processing */
  1767. if (plain) {
  1768. wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
  1769. MAC2STR(sta->addr));
  1770. hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
  1771. sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
  1772. NULL, 0);
  1773. }
  1774. #ifdef KRACK_TEST_CLIENT
  1775. poc_log(sta->addr, "sending a new 4-way message 1 (%s)\n",
  1776. os_strstr(cmd, "change-anonce") != NULL ? "with random ANonce" : "with same ANonce");
  1777. #else
  1778. wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
  1779. #endif
  1780. return wpa_auth_resend_m1(sta->wpa_sm,
  1781. os_strstr(cmd, "change-anonce") != NULL,
  1782. plain ? restore_tk : NULL, hapd, sta);
  1783. }
  1784. static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
  1785. {
  1786. struct sta_info *sta;
  1787. u8 addr[ETH_ALEN];
  1788. int plain = os_strstr(cmd, "plaintext") != NULL;
  1789. if (hwaddr_aton(cmd, addr))
  1790. return -1;
  1791. sta = ap_get_sta(hapd, addr);
  1792. if (!sta || !sta->wpa_sm)
  1793. return -1;
  1794. if (plain && sta->last_tk_alg == WPA_ALG_NONE)
  1795. plain = 0; /* no need for special processing */
  1796. if (plain) {
  1797. wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
  1798. MAC2STR(sta->addr));
  1799. hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
  1800. sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
  1801. NULL, 0);
  1802. }
  1803. #ifdef KRACK_TEST_CLIENT
  1804. poc_log(sta->addr, "sending a new 4-way message 3\n");
  1805. #else
  1806. wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
  1807. #endif
  1808. return wpa_auth_resend_m3(sta->wpa_sm,
  1809. plain ? restore_tk : NULL, hapd, sta);
  1810. }
  1811. static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
  1812. const char *cmd)
  1813. {
  1814. struct sta_info *sta;
  1815. u8 addr[ETH_ALEN];
  1816. int plain = os_strstr(cmd, "plaintext") != NULL;
  1817. if (hwaddr_aton(cmd, addr))
  1818. return -1;
  1819. sta = ap_get_sta(hapd, addr);
  1820. if (!sta || !sta->wpa_sm)
  1821. return -1;
  1822. if (plain && sta->last_tk_alg == WPA_ALG_NONE)
  1823. plain = 0; /* no need for special processing */
  1824. if (plain) {
  1825. wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
  1826. MAC2STR(sta->addr));
  1827. hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
  1828. sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
  1829. NULL, 0);
  1830. }
  1831. #ifdef KRACK_TEST_CLIENT
  1832. poc_log(sta->addr, "Send group M1 for the same GTK and zero RSC\n");
  1833. #else
  1834. wpa_printf(MSG_INFO,
  1835. "TESTING: Send group M1 for the same GTK and zero RSC to "
  1836. MACSTR, MAC2STR(sta->addr));
  1837. #endif
  1838. return wpa_auth_resend_group_m1(sta->wpa_sm,
  1839. plain ? restore_tk : NULL, hapd, sta);
  1840. }
  1841. #endif /* CONFIG_TESTING_OPTIONS */
  1842. static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
  1843. char *pos)
  1844. {
  1845. #ifdef NEED_AP_MLME
  1846. struct csa_settings settings;
  1847. int ret;
  1848. unsigned int i;
  1849. ret = hostapd_parse_csa_settings(pos, &settings);
  1850. if (ret)
  1851. return ret;
  1852. for (i = 0; i < iface->num_bss; i++) {
  1853. ret = hostapd_switch_channel(iface->bss[i], &settings);
  1854. if (ret) {
  1855. /* FIX: What do we do if CSA fails in the middle of
  1856. * submitting multi-BSS CSA requests? */
  1857. return ret;
  1858. }
  1859. }
  1860. return 0;
  1861. #else /* NEED_AP_MLME */
  1862. return -1;
  1863. #endif /* NEED_AP_MLME */
  1864. }
  1865. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  1866. int reply_size, const char *param)
  1867. {
  1868. #ifdef RADIUS_SERVER
  1869. if (os_strcmp(param, "radius_server") == 0) {
  1870. return radius_server_get_mib(hapd->radius_srv, reply,
  1871. reply_size);
  1872. }
  1873. #endif /* RADIUS_SERVER */
  1874. return -1;
  1875. }
  1876. static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
  1877. char *buf, size_t buflen)
  1878. {
  1879. int ret;
  1880. char *pos;
  1881. u8 *data = NULL;
  1882. unsigned int vendor_id, subcmd;
  1883. struct wpabuf *reply;
  1884. size_t data_len = 0;
  1885. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  1886. vendor_id = strtoul(cmd, &pos, 16);
  1887. if (!isblank((unsigned char) *pos))
  1888. return -EINVAL;
  1889. subcmd = strtoul(pos, &pos, 10);
  1890. if (*pos != '\0') {
  1891. if (!isblank((unsigned char) *pos++))
  1892. return -EINVAL;
  1893. data_len = os_strlen(pos);
  1894. }
  1895. if (data_len) {
  1896. data_len /= 2;
  1897. data = os_malloc(data_len);
  1898. if (!data)
  1899. return -ENOBUFS;
  1900. if (hexstr2bin(pos, data, data_len)) {
  1901. wpa_printf(MSG_DEBUG,
  1902. "Vendor command: wrong parameter format");
  1903. os_free(data);
  1904. return -EINVAL;
  1905. }
  1906. }
  1907. reply = wpabuf_alloc((buflen - 1) / 2);
  1908. if (!reply) {
  1909. os_free(data);
  1910. return -ENOBUFS;
  1911. }
  1912. ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
  1913. reply);
  1914. if (ret == 0)
  1915. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  1916. wpabuf_len(reply));
  1917. wpabuf_free(reply);
  1918. os_free(data);
  1919. return ret;
  1920. }
  1921. static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
  1922. const char *cmd)
  1923. {
  1924. u8 addr[ETH_ALEN];
  1925. struct sta_info *sta;
  1926. if (hwaddr_aton(cmd, addr))
  1927. return -1;
  1928. sta = ap_get_sta(hapd, addr);
  1929. if (!sta || !sta->eapol_sm)
  1930. return -1;
  1931. eapol_auth_reauthenticate(sta->eapol_sm);
  1932. return 0;
  1933. }
  1934. static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
  1935. {
  1936. u8 addr[ETH_ALEN];
  1937. struct sta_info *sta;
  1938. char *pos = cmd, *param;
  1939. if (hwaddr_aton(pos, addr) || pos[17] != ' ')
  1940. return -1;
  1941. pos += 18;
  1942. param = pos;
  1943. pos = os_strchr(pos, ' ');
  1944. if (!pos)
  1945. return -1;
  1946. *pos++ = '\0';
  1947. sta = ap_get_sta(hapd, addr);
  1948. if (!sta || !sta->eapol_sm)
  1949. return -1;
  1950. return eapol_auth_set_conf(sta->eapol_sm, param, pos);
  1951. }
  1952. static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
  1953. char *buf, size_t buflen)
  1954. {
  1955. char *pos, *end, *stamp;
  1956. int ret;
  1957. /* cmd: "LOG_LEVEL [<level>]" */
  1958. if (*cmd == '\0') {
  1959. pos = buf;
  1960. end = buf + buflen;
  1961. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1962. "Timestamp: %d\n",
  1963. debug_level_str(wpa_debug_level),
  1964. wpa_debug_timestamp);
  1965. if (os_snprintf_error(end - pos, ret))
  1966. ret = 0;
  1967. return ret;
  1968. }
  1969. while (*cmd == ' ')
  1970. cmd++;
  1971. stamp = os_strchr(cmd, ' ');
  1972. if (stamp) {
  1973. *stamp++ = '\0';
  1974. while (*stamp == ' ') {
  1975. stamp++;
  1976. }
  1977. }
  1978. if (os_strlen(cmd)) {
  1979. int level = str_to_debug_level(cmd);
  1980. if (level < 0)
  1981. return -1;
  1982. wpa_debug_level = level;
  1983. }
  1984. if (stamp && os_strlen(stamp))
  1985. wpa_debug_timestamp = atoi(stamp);
  1986. os_memcpy(buf, "OK\n", 3);
  1987. return 3;
  1988. }
  1989. #ifdef NEED_AP_MLME
  1990. static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
  1991. char *buf, size_t buflen)
  1992. {
  1993. struct hostapd_iface *iface = hapd->iface;
  1994. char *pos, *end;
  1995. struct hostapd_sta_info *info;
  1996. struct os_reltime now;
  1997. if (!iface->num_sta_seen)
  1998. return 0;
  1999. sta_track_expire(iface, 0);
  2000. pos = buf;
  2001. end = buf + buflen;
  2002. os_get_reltime(&now);
  2003. dl_list_for_each_reverse(info, &iface->sta_seen,
  2004. struct hostapd_sta_info, list) {
  2005. struct os_reltime age;
  2006. int ret;
  2007. os_reltime_sub(&now, &info->last_seen, &age);
  2008. ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
  2009. MAC2STR(info->addr), (unsigned int) age.sec,
  2010. info->ssi_signal);
  2011. if (os_snprintf_error(end - pos, ret))
  2012. break;
  2013. pos += ret;
  2014. }
  2015. return pos - buf;
  2016. }
  2017. #endif /* NEED_AP_MLME */
  2018. static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
  2019. const char *cmd)
  2020. {
  2021. u8 addr[ETH_ALEN];
  2022. if (hwaddr_aton(cmd, addr)) {
  2023. wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
  2024. return -1;
  2025. }
  2026. return hostapd_send_lci_req(hapd, addr);
  2027. }
  2028. static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
  2029. {
  2030. u8 addr[ETH_ALEN];
  2031. char *token, *context = NULL;
  2032. int random_interval, min_ap;
  2033. u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
  2034. unsigned int n_responders;
  2035. token = str_token(cmd, " ", &context);
  2036. if (!token || hwaddr_aton(token, addr)) {
  2037. wpa_printf(MSG_INFO,
  2038. "CTRL: REQ_RANGE - Bad destination address");
  2039. return -1;
  2040. }
  2041. token = str_token(cmd, " ", &context);
  2042. if (!token)
  2043. return -1;
  2044. random_interval = atoi(token);
  2045. if (random_interval < 0 || random_interval > 0xffff)
  2046. return -1;
  2047. token = str_token(cmd, " ", &context);
  2048. if (!token)
  2049. return -1;
  2050. min_ap = atoi(token);
  2051. if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
  2052. return -1;
  2053. n_responders = 0;
  2054. while ((token = str_token(cmd, " ", &context))) {
  2055. if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
  2056. wpa_printf(MSG_INFO,
  2057. "CTRL: REQ_RANGE: Too many responders");
  2058. return -1;
  2059. }
  2060. if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
  2061. wpa_printf(MSG_INFO,
  2062. "CTRL: REQ_RANGE: Bad responder address");
  2063. return -1;
  2064. }
  2065. n_responders++;
  2066. }
  2067. if (!n_responders) {
  2068. wpa_printf(MSG_INFO,
  2069. "CTRL: REQ_RANGE - No FTM responder address");
  2070. return -1;
  2071. }
  2072. return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
  2073. responders, n_responders);
  2074. }
  2075. static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
  2076. const char *cmd, char *reply,
  2077. size_t reply_size)
  2078. {
  2079. u8 addr[ETH_ALEN];
  2080. const char *pos;
  2081. struct wpabuf *req;
  2082. int ret;
  2083. u8 req_mode = 0;
  2084. if (hwaddr_aton(cmd, addr))
  2085. return -1;
  2086. pos = os_strchr(cmd, ' ');
  2087. if (!pos)
  2088. return -1;
  2089. pos++;
  2090. if (os_strncmp(pos, "req_mode=", 9) == 0) {
  2091. int val = hex2byte(pos + 9);
  2092. if (val < 0)
  2093. return -1;
  2094. req_mode = val;
  2095. pos += 11;
  2096. pos = os_strchr(pos, ' ');
  2097. if (!pos)
  2098. return -1;
  2099. pos++;
  2100. }
  2101. req = wpabuf_parse_bin(pos);
  2102. if (!req)
  2103. return -1;
  2104. ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
  2105. wpabuf_free(req);
  2106. if (ret >= 0)
  2107. ret = os_snprintf(reply, reply_size, "%d", ret);
  2108. return ret;
  2109. }
  2110. static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
  2111. {
  2112. struct wpa_ssid_value ssid;
  2113. u8 bssid[ETH_ALEN];
  2114. struct wpabuf *nr, *lci = NULL, *civic = NULL;
  2115. int stationary = 0;
  2116. char *tmp;
  2117. int ret;
  2118. if (!(hapd->conf->radio_measurements[0] &
  2119. WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
  2120. wpa_printf(MSG_ERROR,
  2121. "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
  2122. return -1;
  2123. }
  2124. if (hwaddr_aton(buf, bssid)) {
  2125. wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
  2126. return -1;
  2127. }
  2128. tmp = os_strstr(buf, "ssid=");
  2129. if (!tmp || ssid_parse(tmp + 5, &ssid)) {
  2130. wpa_printf(MSG_ERROR,
  2131. "CTRL: SET_NEIGHBOR: Bad or missing SSID");
  2132. return -1;
  2133. }
  2134. buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
  2135. if (!buf)
  2136. return -1;
  2137. tmp = os_strstr(buf, "nr=");
  2138. if (!tmp) {
  2139. wpa_printf(MSG_ERROR,
  2140. "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
  2141. return -1;
  2142. }
  2143. buf = os_strchr(tmp, ' ');
  2144. if (buf)
  2145. *buf++ = '\0';
  2146. nr = wpabuf_parse_bin(tmp + 3);
  2147. if (!nr) {
  2148. wpa_printf(MSG_ERROR,
  2149. "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
  2150. return -1;
  2151. }
  2152. if (!buf)
  2153. goto set;
  2154. tmp = os_strstr(buf, "lci=");
  2155. if (tmp) {
  2156. buf = os_strchr(tmp, ' ');
  2157. if (buf)
  2158. *buf++ = '\0';
  2159. lci = wpabuf_parse_bin(tmp + 4);
  2160. if (!lci) {
  2161. wpa_printf(MSG_ERROR,
  2162. "CTRL: SET_NEIGHBOR: Bad LCI subelement");
  2163. wpabuf_free(nr);
  2164. return -1;
  2165. }
  2166. }
  2167. if (!buf)
  2168. goto set;
  2169. tmp = os_strstr(buf, "civic=");
  2170. if (tmp) {
  2171. buf = os_strchr(tmp, ' ');
  2172. if (buf)
  2173. *buf++ = '\0';
  2174. civic = wpabuf_parse_bin(tmp + 6);
  2175. if (!civic) {
  2176. wpa_printf(MSG_ERROR,
  2177. "CTRL: SET_NEIGHBOR: Bad civic subelement");
  2178. wpabuf_free(nr);
  2179. wpabuf_free(lci);
  2180. return -1;
  2181. }
  2182. }
  2183. if (!buf)
  2184. goto set;
  2185. if (os_strstr(buf, "stat"))
  2186. stationary = 1;
  2187. set:
  2188. ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
  2189. stationary);
  2190. wpabuf_free(nr);
  2191. wpabuf_free(lci);
  2192. wpabuf_free(civic);
  2193. return ret;
  2194. }
  2195. static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
  2196. char *buf)
  2197. {
  2198. struct wpa_ssid_value ssid;
  2199. u8 bssid[ETH_ALEN];
  2200. char *tmp;
  2201. if (hwaddr_aton(buf, bssid)) {
  2202. wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
  2203. return -1;
  2204. }
  2205. tmp = os_strstr(buf, "ssid=");
  2206. if (!tmp || ssid_parse(tmp + 5, &ssid)) {
  2207. wpa_printf(MSG_ERROR,
  2208. "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
  2209. return -1;
  2210. }
  2211. return hostapd_neighbor_remove(hapd, bssid, &ssid);
  2212. }
  2213. static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
  2214. size_t buflen)
  2215. {
  2216. int ret, i;
  2217. char *pos, *end;
  2218. ret = os_snprintf(buf, buflen, "%016llX:\n",
  2219. (long long unsigned) iface->drv_flags);
  2220. if (os_snprintf_error(buflen, ret))
  2221. return -1;
  2222. pos = buf + ret;
  2223. end = buf + buflen;
  2224. for (i = 0; i < 64; i++) {
  2225. if (iface->drv_flags & (1LLU << i)) {
  2226. ret = os_snprintf(pos, end - pos, "%s\n",
  2227. driver_flag_to_string(1LLU << i));
  2228. if (os_snprintf_error(end - pos, ret))
  2229. return -1;
  2230. pos += ret;
  2231. }
  2232. }
  2233. return pos - buf;
  2234. }
  2235. #ifdef KRACK_TEST_CLIENT
  2236. static int hostapd_get_tk(struct hostapd_data *hapd, const char *txtaddr, char *buf, size_t buflen)
  2237. {
  2238. u8 addr[ETH_ALEN];
  2239. struct sta_info *sta;
  2240. int klen;
  2241. int res;
  2242. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET_TK %s", txtaddr);
  2243. if (hwaddr_aton(txtaddr, addr)) return -1;
  2244. sta = ap_get_sta(hapd, addr);
  2245. if (!sta) return -1;
  2246. if (sta->wpa_sm == NULL) return -1;
  2247. klen = wpa_cipher_key_len(sta->wpa_sm->pairwise);
  2248. if (klen <= 0) return -1;
  2249. res = wpa_snprintf_hex(buf, buflen, sta->wpa_sm->PTK.tk, klen);
  2250. buf[res++] = '\n';
  2251. buf[res] = '\0';
  2252. return res;
  2253. }
  2254. #endif
  2255. static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
  2256. const char *txtaddr)
  2257. {
  2258. u8 addr[ETH_ALEN];
  2259. struct vlan_description vlan_id;
  2260. if (!(*num))
  2261. return 0;
  2262. if (hwaddr_aton(txtaddr, addr))
  2263. return -1;
  2264. if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
  2265. hostapd_remove_acl_mac(acl, num, addr);
  2266. return 0;
  2267. }
  2268. static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
  2269. int *num)
  2270. {
  2271. while (*num)
  2272. hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
  2273. }
  2274. static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
  2275. char *buf, size_t buflen)
  2276. {
  2277. int i = 0, len = 0, ret = 0;
  2278. if (!acl)
  2279. return 0;
  2280. while (i < num) {
  2281. ret = os_snprintf(buf + len, buflen - len,
  2282. MACSTR " VLAN_ID=%d\n",
  2283. MAC2STR(acl[i].addr),
  2284. acl[i].vlan_id.untagged);
  2285. if (ret < 0 || (size_t) ret >= buflen - len)
  2286. return len;
  2287. i++;
  2288. len += ret;
  2289. }
  2290. return len;
  2291. }
  2292. static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
  2293. const char *cmd)
  2294. {
  2295. u8 addr[ETH_ALEN];
  2296. struct vlan_description vlan_id;
  2297. int ret = 0, vlanid = 0;
  2298. const char *pos;
  2299. if (hwaddr_aton(cmd, addr))
  2300. return -1;
  2301. pos = os_strstr(cmd, "VLAN_ID=");
  2302. if (pos)
  2303. vlanid = atoi(pos + 8);
  2304. if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
  2305. ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
  2306. if (ret != -1 && *acl)
  2307. qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
  2308. }
  2309. return ret < 0 ? -1 : 0;
  2310. }
  2311. static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
  2312. char *buf, char *reply,
  2313. int reply_size,
  2314. struct sockaddr_storage *from,
  2315. socklen_t fromlen)
  2316. {
  2317. int reply_len, res;
  2318. os_memcpy(reply, "OK\n", 3);
  2319. reply_len = 3;
  2320. if (os_strcmp(buf, "PING") == 0) {
  2321. os_memcpy(reply, "PONG\n", 5);
  2322. reply_len = 5;
  2323. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  2324. if (wpa_debug_reopen_file() < 0)
  2325. reply_len = -1;
  2326. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  2327. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  2328. } else if (os_strcmp(buf, "STATUS") == 0) {
  2329. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  2330. reply_size);
  2331. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  2332. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  2333. } else if (os_strcmp(buf, "MIB") == 0) {
  2334. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  2335. if (reply_len >= 0) {
  2336. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  2337. reply_size - reply_len);
  2338. if (res < 0)
  2339. reply_len = -1;
  2340. else
  2341. reply_len += res;
  2342. }
  2343. if (reply_len >= 0) {
  2344. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  2345. reply_size - reply_len);
  2346. if (res < 0)
  2347. reply_len = -1;
  2348. else
  2349. reply_len += res;
  2350. }
  2351. #ifndef CONFIG_NO_RADIUS
  2352. if (reply_len >= 0) {
  2353. res = radius_client_get_mib(hapd->radius,
  2354. reply + reply_len,
  2355. reply_size - reply_len);
  2356. if (res < 0)
  2357. reply_len = -1;
  2358. else
  2359. reply_len += res;
  2360. }
  2361. #endif /* CONFIG_NO_RADIUS */
  2362. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  2363. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  2364. buf + 4);
  2365. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  2366. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  2367. reply_size);
  2368. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  2369. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  2370. reply_size);
  2371. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  2372. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  2373. reply_size);
  2374. } else if (os_strcmp(buf, "ATTACH") == 0) {
  2375. if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
  2376. reply_len = -1;
  2377. } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
  2378. if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
  2379. reply_len = -1;
  2380. } else if (os_strcmp(buf, "DETACH") == 0) {
  2381. if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
  2382. reply_len = -1;
  2383. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  2384. if (hostapd_ctrl_iface_level(hapd, from, fromlen,
  2385. buf + 6))
  2386. reply_len = -1;
  2387. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  2388. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  2389. reply_len = -1;
  2390. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  2391. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  2392. reply_len = -1;
  2393. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  2394. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  2395. reply_len = -1;
  2396. #ifdef CONFIG_TAXONOMY
  2397. } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
  2398. reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
  2399. reply, reply_size);
  2400. #endif /* CONFIG_TAXONOMY */
  2401. } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
  2402. if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
  2403. reply_len = -1;
  2404. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  2405. if (hostapd_ctrl_iface_stop_ap(hapd))
  2406. reply_len = -1;
  2407. #ifdef CONFIG_IEEE80211W
  2408. #ifdef NEED_AP_MLME
  2409. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  2410. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  2411. reply_len = -1;
  2412. #endif /* NEED_AP_MLME */
  2413. #endif /* CONFIG_IEEE80211W */
  2414. #ifdef CONFIG_WPS
  2415. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  2416. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  2417. reply_len = -1;
  2418. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  2419. reply_len = hostapd_ctrl_iface_wps_check_pin(
  2420. hapd, buf + 14, reply, reply_size);
  2421. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  2422. if (hostapd_wps_button_pushed(hapd, NULL))
  2423. reply_len = -1;
  2424. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  2425. if (hostapd_wps_cancel(hapd))
  2426. reply_len = -1;
  2427. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  2428. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  2429. reply, reply_size);
  2430. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  2431. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  2432. reply_len = -1;
  2433. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  2434. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  2435. reply_size);
  2436. #ifdef CONFIG_WPS_NFC
  2437. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  2438. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  2439. reply_len = -1;
  2440. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  2441. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  2442. hapd, buf + 21, reply, reply_size);
  2443. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  2444. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  2445. hapd, buf + 14, reply, reply_size);
  2446. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  2447. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  2448. hapd, buf + 21, reply, reply_size);
  2449. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  2450. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  2451. reply_len = -1;
  2452. #endif /* CONFIG_WPS_NFC */
  2453. #endif /* CONFIG_WPS */
  2454. #ifdef CONFIG_INTERWORKING
  2455. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  2456. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  2457. reply_len = -1;
  2458. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  2459. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  2460. reply_len = -1;
  2461. #endif /* CONFIG_INTERWORKING */
  2462. #ifdef CONFIG_HS20
  2463. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  2464. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  2465. reply_len = -1;
  2466. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  2467. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  2468. reply_len = -1;
  2469. #endif /* CONFIG_HS20 */
  2470. #ifdef CONFIG_WNM_AP
  2471. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  2472. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  2473. reply_len = -1;
  2474. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  2475. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  2476. reply_len = -1;
  2477. } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
  2478. if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
  2479. reply_len = -1;
  2480. #endif /* CONFIG_WNM_AP */
  2481. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  2482. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  2483. reply_size);
  2484. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  2485. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  2486. reply_len = -1;
  2487. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  2488. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  2489. reply_size);
  2490. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  2491. if (hostapd_ctrl_iface_enable(hapd->iface))
  2492. reply_len = -1;
  2493. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  2494. if (hostapd_ctrl_iface_reload(hapd->iface))
  2495. reply_len = -1;
  2496. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  2497. if (hostapd_ctrl_iface_disable(hapd->iface))
  2498. reply_len = -1;
  2499. } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
  2500. if (ieee802_11_set_beacon(hapd))
  2501. reply_len = -1;
  2502. #ifdef CONFIG_TESTING_OPTIONS
  2503. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  2504. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  2505. reply_len = -1;
  2506. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  2507. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  2508. reply_len = -1;
  2509. } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
  2510. if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
  2511. buf + 23) < 0)
  2512. reply_len = -1;
  2513. } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
  2514. if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
  2515. reply_len = -1;
  2516. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  2517. if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
  2518. reply_len = -1;
  2519. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  2520. if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
  2521. reply_len = -1;
  2522. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  2523. if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
  2524. reply_len = -1;
  2525. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  2526. if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
  2527. reply_len = -1;
  2528. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  2529. if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
  2530. reply_len = -1;
  2531. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  2532. reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
  2533. reply_size);
  2534. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  2535. if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
  2536. reply_len = -1;
  2537. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  2538. reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
  2539. } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
  2540. if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
  2541. reply_len = -1;
  2542. } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
  2543. if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
  2544. reply_len = -1;
  2545. } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
  2546. if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
  2547. reply_len = -1;
  2548. } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
  2549. if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
  2550. reply_len = -1;
  2551. } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
  2552. if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
  2553. reply_len = -1;
  2554. } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
  2555. if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
  2556. reply_len = -1;
  2557. #endif /* CONFIG_TESTING_OPTIONS */
  2558. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  2559. if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
  2560. reply_len = -1;
  2561. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  2562. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  2563. reply_size);
  2564. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  2565. ieee802_1x_erp_flush(hapd);
  2566. #ifdef RADIUS_SERVER
  2567. radius_server_erp_flush(hapd->radius_srv);
  2568. #endif /* RADIUS_SERVER */
  2569. } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
  2570. if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
  2571. reply_len = -1;
  2572. } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
  2573. if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
  2574. reply_len = -1;
  2575. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  2576. reply_len = hostapd_ctrl_iface_log_level(
  2577. hapd, buf + 9, reply, reply_size);
  2578. #ifdef NEED_AP_MLME
  2579. } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
  2580. reply_len = hostapd_ctrl_iface_track_sta_list(
  2581. hapd, reply, reply_size);
  2582. #endif /* NEED_AP_MLME */
  2583. } else if (os_strcmp(buf, "PMKSA") == 0) {
  2584. reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
  2585. reply_size);
  2586. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  2587. hostapd_ctrl_iface_pmksa_flush(hapd);
  2588. } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
  2589. if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
  2590. reply_len = -1;
  2591. } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
  2592. if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
  2593. reply_len = -1;
  2594. } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
  2595. if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
  2596. reply_len = -1;
  2597. } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
  2598. if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
  2599. reply_len = -1;
  2600. } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
  2601. if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
  2602. reply_len = -1;
  2603. } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
  2604. reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
  2605. reply, reply_size);
  2606. } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
  2607. reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
  2608. reply_size);
  2609. #ifdef KRACK_TEST_CLIENT
  2610. } else if (os_strncmp(buf, "GET_TK ", 7) == 0) {
  2611. reply_len = hostapd_get_tk(hapd, buf + 7, reply, reply_size);
  2612. #endif
  2613. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2614. eloop_terminate();
  2615. } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
  2616. if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
  2617. if (!hostapd_ctrl_iface_acl_add_mac(
  2618. &hapd->conf->accept_mac,
  2619. &hapd->conf->num_accept_mac, buf + 19))
  2620. hostapd_disassoc_accept_mac(hapd);
  2621. else
  2622. reply_len = -1;
  2623. } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
  2624. hostapd_ctrl_iface_acl_del_mac(
  2625. &hapd->conf->accept_mac,
  2626. &hapd->conf->num_accept_mac, buf + 19);
  2627. } else if (os_strcmp(buf + 11, "SHOW") == 0) {
  2628. reply_len = hostapd_ctrl_iface_acl_show_mac(
  2629. hapd->conf->accept_mac,
  2630. hapd->conf->num_accept_mac, reply, reply_size);
  2631. } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
  2632. hostapd_ctrl_iface_acl_clear_list(
  2633. &hapd->conf->accept_mac,
  2634. &hapd->conf->num_accept_mac);
  2635. }
  2636. } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
  2637. if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
  2638. if (!hostapd_ctrl_iface_acl_add_mac(
  2639. &hapd->conf->deny_mac,
  2640. &hapd->conf->num_deny_mac, buf + 17))
  2641. hostapd_disassoc_deny_mac(hapd);
  2642. } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
  2643. hostapd_ctrl_iface_acl_del_mac(
  2644. &hapd->conf->deny_mac,
  2645. &hapd->conf->num_deny_mac, buf + 17);
  2646. } else if (os_strcmp(buf + 9, "SHOW") == 0) {
  2647. reply_len = hostapd_ctrl_iface_acl_show_mac(
  2648. hapd->conf->deny_mac,
  2649. hapd->conf->num_deny_mac, reply, reply_size);
  2650. } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
  2651. hostapd_ctrl_iface_acl_clear_list(
  2652. &hapd->conf->deny_mac,
  2653. &hapd->conf->num_deny_mac);
  2654. }
  2655. #ifdef CONFIG_DPP
  2656. } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
  2657. res = hostapd_dpp_qr_code(hapd, buf + 12);
  2658. if (res < 0) {
  2659. reply_len = -1;
  2660. } else {
  2661. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2662. if (os_snprintf_error(reply_size, reply_len))
  2663. reply_len = -1;
  2664. }
  2665. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
  2666. res = hostapd_dpp_bootstrap_gen(hapd, buf + 18);
  2667. if (res < 0) {
  2668. reply_len = -1;
  2669. } else {
  2670. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2671. if (os_snprintf_error(reply_size, reply_len))
  2672. reply_len = -1;
  2673. }
  2674. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
  2675. if (hostapd_dpp_bootstrap_remove(hapd, buf + 21) < 0)
  2676. reply_len = -1;
  2677. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
  2678. const char *uri;
  2679. uri = hostapd_dpp_bootstrap_get_uri(hapd, atoi(buf + 22));
  2680. if (!uri) {
  2681. reply_len = -1;
  2682. } else {
  2683. reply_len = os_snprintf(reply, reply_size, "%s", uri);
  2684. if (os_snprintf_error(reply_size, reply_len))
  2685. reply_len = -1;
  2686. }
  2687. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
  2688. reply_len = hostapd_dpp_bootstrap_info(hapd, atoi(buf + 19),
  2689. reply, reply_size);
  2690. } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
  2691. if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
  2692. reply_len = -1;
  2693. } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
  2694. if (hostapd_dpp_listen(hapd, buf + 11) < 0)
  2695. reply_len = -1;
  2696. } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
  2697. hostapd_dpp_stop(hapd);
  2698. hostapd_dpp_listen_stop(hapd);
  2699. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
  2700. res = hostapd_dpp_configurator_add(hapd, buf + 20);
  2701. if (res < 0) {
  2702. reply_len = -1;
  2703. } else {
  2704. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2705. if (os_snprintf_error(reply_size, reply_len))
  2706. reply_len = -1;
  2707. }
  2708. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
  2709. if (hostapd_dpp_configurator_remove(hapd, buf + 24) < 0)
  2710. reply_len = -1;
  2711. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
  2712. if (hostapd_dpp_configurator_sign(hapd, buf + 22) < 0)
  2713. reply_len = -1;
  2714. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
  2715. reply_len = hostapd_dpp_configurator_get_key(hapd,
  2716. atoi(buf + 25),
  2717. reply, reply_size);
  2718. } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
  2719. res = hostapd_dpp_pkex_add(hapd, buf + 12);
  2720. if (res < 0) {
  2721. reply_len = -1;
  2722. } else {
  2723. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2724. if (os_snprintf_error(reply_size, reply_len))
  2725. reply_len = -1;
  2726. }
  2727. } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
  2728. if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
  2729. reply_len = -1;
  2730. #endif /* CONFIG_DPP */
  2731. } else {
  2732. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2733. reply_len = 16;
  2734. }
  2735. if (reply_len < 0) {
  2736. os_memcpy(reply, "FAIL\n", 5);
  2737. reply_len = 5;
  2738. }
  2739. return reply_len;
  2740. }
  2741. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  2742. void *sock_ctx)
  2743. {
  2744. struct hostapd_data *hapd = eloop_ctx;
  2745. char buf[4096];
  2746. int res;
  2747. struct sockaddr_storage from;
  2748. socklen_t fromlen = sizeof(from);
  2749. char *reply, *pos = buf;
  2750. const int reply_size = 4096;
  2751. int reply_len;
  2752. int level = MSG_DEBUG;
  2753. #ifdef CONFIG_CTRL_IFACE_UDP
  2754. unsigned char lcookie[COOKIE_LEN];
  2755. #endif /* CONFIG_CTRL_IFACE_UDP */
  2756. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  2757. (struct sockaddr *) &from, &fromlen);
  2758. if (res < 0) {
  2759. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  2760. strerror(errno));
  2761. return;
  2762. }
  2763. buf[res] = '\0';
  2764. reply = os_malloc(reply_size);
  2765. if (reply == NULL) {
  2766. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  2767. fromlen) < 0) {
  2768. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2769. strerror(errno));
  2770. }
  2771. return;
  2772. }
  2773. #ifdef CONFIG_CTRL_IFACE_UDP
  2774. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  2775. os_memcpy(reply, "COOKIE=", 7);
  2776. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  2777. cookie, COOKIE_LEN);
  2778. reply_len = 7 + 2 * COOKIE_LEN;
  2779. goto done;
  2780. }
  2781. if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
  2782. hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
  2783. wpa_printf(MSG_DEBUG,
  2784. "CTRL: No cookie in the request - drop request");
  2785. os_free(reply);
  2786. return;
  2787. }
  2788. if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
  2789. wpa_printf(MSG_DEBUG,
  2790. "CTRL: Invalid cookie in the request - drop request");
  2791. os_free(reply);
  2792. return;
  2793. }
  2794. pos = buf + 7 + 2 * COOKIE_LEN;
  2795. while (*pos == ' ')
  2796. pos++;
  2797. #endif /* CONFIG_CTRL_IFACE_UDP */
  2798. if (os_strcmp(pos, "PING") == 0)
  2799. level = MSG_EXCESSIVE;
  2800. wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
  2801. reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
  2802. reply, reply_size,
  2803. &from, fromlen);
  2804. #ifdef CONFIG_CTRL_IFACE_UDP
  2805. done:
  2806. #endif /* CONFIG_CTRL_IFACE_UDP */
  2807. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  2808. fromlen) < 0) {
  2809. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2810. strerror(errno));
  2811. }
  2812. os_free(reply);
  2813. }
  2814. #ifndef CONFIG_CTRL_IFACE_UDP
  2815. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  2816. {
  2817. char *buf;
  2818. size_t len;
  2819. if (hapd->conf->ctrl_interface == NULL)
  2820. return NULL;
  2821. len = os_strlen(hapd->conf->ctrl_interface) +
  2822. os_strlen(hapd->conf->iface) + 2;
  2823. buf = os_malloc(len);
  2824. if (buf == NULL)
  2825. return NULL;
  2826. os_snprintf(buf, len, "%s/%s",
  2827. hapd->conf->ctrl_interface, hapd->conf->iface);
  2828. buf[len - 1] = '\0';
  2829. return buf;
  2830. }
  2831. #endif /* CONFIG_CTRL_IFACE_UDP */
  2832. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  2833. enum wpa_msg_type type,
  2834. const char *txt, size_t len)
  2835. {
  2836. struct hostapd_data *hapd = ctx;
  2837. if (hapd == NULL)
  2838. return;
  2839. hostapd_ctrl_iface_send(hapd, level, type, txt, len);
  2840. }
  2841. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  2842. {
  2843. #ifdef CONFIG_CTRL_IFACE_UDP
  2844. int port = HOSTAPD_CTRL_IFACE_PORT;
  2845. char p[32] = { 0 };
  2846. char port_str[40], *tmp;
  2847. char *pos;
  2848. struct addrinfo hints = { 0 }, *res, *saveres;
  2849. int n;
  2850. if (hapd->ctrl_sock > -1) {
  2851. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2852. return 0;
  2853. }
  2854. if (hapd->conf->ctrl_interface == NULL)
  2855. return 0;
  2856. pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
  2857. if (pos) {
  2858. pos += 4;
  2859. port = atoi(pos);
  2860. if (port <= 0) {
  2861. wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
  2862. goto fail;
  2863. }
  2864. }
  2865. dl_list_init(&hapd->ctrl_dst);
  2866. hapd->ctrl_sock = -1;
  2867. os_get_random(cookie, COOKIE_LEN);
  2868. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  2869. hints.ai_flags = AI_PASSIVE;
  2870. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  2871. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  2872. hints.ai_family = AF_INET6;
  2873. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2874. hints.ai_family = AF_INET;
  2875. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2876. hints.ai_socktype = SOCK_DGRAM;
  2877. try_again:
  2878. os_snprintf(p, sizeof(p), "%d", port);
  2879. n = getaddrinfo(NULL, p, &hints, &res);
  2880. if (n) {
  2881. wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
  2882. goto fail;
  2883. }
  2884. saveres = res;
  2885. hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
  2886. res->ai_protocol);
  2887. if (hapd->ctrl_sock < 0) {
  2888. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  2889. goto fail;
  2890. }
  2891. if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
  2892. port--;
  2893. if ((HOSTAPD_CTRL_IFACE_PORT - port) <
  2894. HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
  2895. goto try_again;
  2896. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  2897. goto fail;
  2898. }
  2899. freeaddrinfo(saveres);
  2900. os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
  2901. tmp = os_strdup(port_str);
  2902. if (tmp) {
  2903. os_free(hapd->conf->ctrl_interface);
  2904. hapd->conf->ctrl_interface = tmp;
  2905. }
  2906. wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
  2907. if (eloop_register_read_sock(hapd->ctrl_sock,
  2908. hostapd_ctrl_iface_receive, hapd, NULL) <
  2909. 0) {
  2910. hostapd_ctrl_iface_deinit(hapd);
  2911. return -1;
  2912. }
  2913. hapd->msg_ctx = hapd;
  2914. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  2915. return 0;
  2916. fail:
  2917. if (hapd->ctrl_sock >= 0)
  2918. close(hapd->ctrl_sock);
  2919. return -1;
  2920. #else /* CONFIG_CTRL_IFACE_UDP */
  2921. struct sockaddr_un addr;
  2922. int s = -1;
  2923. char *fname = NULL;
  2924. if (hapd->ctrl_sock > -1) {
  2925. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2926. return 0;
  2927. }
  2928. dl_list_init(&hapd->ctrl_dst);
  2929. if (hapd->conf->ctrl_interface == NULL)
  2930. return 0;
  2931. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2932. if (errno == EEXIST) {
  2933. wpa_printf(MSG_DEBUG, "Using existing control "
  2934. "interface directory.");
  2935. } else {
  2936. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2937. strerror(errno));
  2938. goto fail;
  2939. }
  2940. }
  2941. if (hapd->conf->ctrl_interface_gid_set &&
  2942. chown(hapd->conf->ctrl_interface, -1,
  2943. hapd->conf->ctrl_interface_gid) < 0) {
  2944. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2945. strerror(errno));
  2946. return -1;
  2947. }
  2948. if (!hapd->conf->ctrl_interface_gid_set &&
  2949. hapd->iface->interfaces->ctrl_iface_group &&
  2950. chown(hapd->conf->ctrl_interface, -1,
  2951. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2952. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2953. strerror(errno));
  2954. return -1;
  2955. }
  2956. #ifdef ANDROID
  2957. /*
  2958. * Android is using umask 0077 which would leave the control interface
  2959. * directory without group access. This breaks things since Wi-Fi
  2960. * framework assumes that this directory can be accessed by other
  2961. * applications in the wifi group. Fix this by adding group access even
  2962. * if umask value would prevent this.
  2963. */
  2964. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2965. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  2966. strerror(errno));
  2967. /* Try to continue anyway */
  2968. }
  2969. #endif /* ANDROID */
  2970. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  2971. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  2972. goto fail;
  2973. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2974. if (s < 0) {
  2975. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2976. goto fail;
  2977. }
  2978. os_memset(&addr, 0, sizeof(addr));
  2979. #ifdef __FreeBSD__
  2980. addr.sun_len = sizeof(addr);
  2981. #endif /* __FreeBSD__ */
  2982. addr.sun_family = AF_UNIX;
  2983. fname = hostapd_ctrl_iface_path(hapd);
  2984. if (fname == NULL)
  2985. goto fail;
  2986. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2987. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2988. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2989. strerror(errno));
  2990. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2991. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2992. " allow connections - assuming it was left"
  2993. "over from forced program termination");
  2994. if (unlink(fname) < 0) {
  2995. wpa_printf(MSG_ERROR,
  2996. "Could not unlink existing ctrl_iface socket '%s': %s",
  2997. fname, strerror(errno));
  2998. goto fail;
  2999. }
  3000. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  3001. 0) {
  3002. wpa_printf(MSG_ERROR,
  3003. "hostapd-ctrl-iface: bind(PF_UNIX): %s",
  3004. strerror(errno));
  3005. goto fail;
  3006. }
  3007. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  3008. "ctrl_iface socket '%s'", fname);
  3009. } else {
  3010. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  3011. "be in use - cannot override it");
  3012. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  3013. "not used anymore", fname);
  3014. os_free(fname);
  3015. fname = NULL;
  3016. goto fail;
  3017. }
  3018. }
  3019. if (hapd->conf->ctrl_interface_gid_set &&
  3020. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  3021. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  3022. strerror(errno));
  3023. goto fail;
  3024. }
  3025. if (!hapd->conf->ctrl_interface_gid_set &&
  3026. hapd->iface->interfaces->ctrl_iface_group &&
  3027. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  3028. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  3029. strerror(errno));
  3030. goto fail;
  3031. }
  3032. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  3033. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  3034. strerror(errno));
  3035. goto fail;
  3036. }
  3037. os_free(fname);
  3038. hapd->ctrl_sock = s;
  3039. if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  3040. NULL) < 0) {
  3041. hostapd_ctrl_iface_deinit(hapd);
  3042. return -1;
  3043. }
  3044. hapd->msg_ctx = hapd;
  3045. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  3046. return 0;
  3047. fail:
  3048. if (s >= 0)
  3049. close(s);
  3050. if (fname) {
  3051. unlink(fname);
  3052. os_free(fname);
  3053. }
  3054. return -1;
  3055. #endif /* CONFIG_CTRL_IFACE_UDP */
  3056. }
  3057. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  3058. {
  3059. struct wpa_ctrl_dst *dst, *prev;
  3060. if (hapd->ctrl_sock > -1) {
  3061. #ifndef CONFIG_CTRL_IFACE_UDP
  3062. char *fname;
  3063. #endif /* !CONFIG_CTRL_IFACE_UDP */
  3064. eloop_unregister_read_sock(hapd->ctrl_sock);
  3065. close(hapd->ctrl_sock);
  3066. hapd->ctrl_sock = -1;
  3067. #ifndef CONFIG_CTRL_IFACE_UDP
  3068. fname = hostapd_ctrl_iface_path(hapd);
  3069. if (fname)
  3070. unlink(fname);
  3071. os_free(fname);
  3072. if (hapd->conf->ctrl_interface &&
  3073. rmdir(hapd->conf->ctrl_interface) < 0) {
  3074. if (errno == ENOTEMPTY) {
  3075. wpa_printf(MSG_DEBUG, "Control interface "
  3076. "directory not empty - leaving it "
  3077. "behind");
  3078. } else {
  3079. wpa_printf(MSG_ERROR,
  3080. "rmdir[ctrl_interface=%s]: %s",
  3081. hapd->conf->ctrl_interface,
  3082. strerror(errno));
  3083. }
  3084. }
  3085. #endif /* !CONFIG_CTRL_IFACE_UDP */
  3086. }
  3087. dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
  3088. list)
  3089. os_free(dst);
  3090. #ifdef CONFIG_TESTING_OPTIONS
  3091. l2_packet_deinit(hapd->l2_test);
  3092. hapd->l2_test = NULL;
  3093. #endif /* CONFIG_TESTING_OPTIONS */
  3094. }
  3095. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  3096. char *buf)
  3097. {
  3098. if (hostapd_add_iface(interfaces, buf) < 0) {
  3099. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  3100. return -1;
  3101. }
  3102. return 0;
  3103. }
  3104. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  3105. char *buf)
  3106. {
  3107. if (hostapd_remove_iface(interfaces, buf) < 0) {
  3108. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  3109. return -1;
  3110. }
  3111. return 0;
  3112. }
  3113. static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
  3114. struct sockaddr_storage *from,
  3115. socklen_t fromlen, char *input)
  3116. {
  3117. return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
  3118. input);
  3119. }
  3120. static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
  3121. struct sockaddr_storage *from,
  3122. socklen_t fromlen)
  3123. {
  3124. return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
  3125. }
  3126. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  3127. {
  3128. #ifdef CONFIG_WPS_TESTING
  3129. wps_version_number = 0x20;
  3130. wps_testing_dummy_cred = 0;
  3131. wps_corrupt_pkhash = 0;
  3132. #endif /* CONFIG_WPS_TESTING */
  3133. #ifdef CONFIG_TESTING_OPTIONS
  3134. #ifdef CONFIG_DPP
  3135. dpp_test = DPP_TEST_DISABLED;
  3136. #endif /* CONFIG_DPP */
  3137. #endif /* CONFIG_TESTING_OPTIONS */
  3138. #ifdef CONFIG_DPP
  3139. hostapd_dpp_deinit_global(interfaces);
  3140. #endif /* CONFIG_DPP */
  3141. }
  3142. #ifdef CONFIG_FST
  3143. static int
  3144. hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
  3145. const char *cmd)
  3146. {
  3147. char ifname[IFNAMSIZ + 1];
  3148. struct fst_iface_cfg cfg;
  3149. struct hostapd_data *hapd;
  3150. struct fst_wpa_obj iface_obj;
  3151. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  3152. hapd = hostapd_get_iface(interfaces, ifname);
  3153. if (hapd) {
  3154. if (hapd->iface->fst) {
  3155. wpa_printf(MSG_INFO, "FST: Already attached");
  3156. return -1;
  3157. }
  3158. fst_hostapd_fill_iface_obj(hapd, &iface_obj);
  3159. hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
  3160. &iface_obj, &cfg);
  3161. if (hapd->iface->fst)
  3162. return 0;
  3163. }
  3164. }
  3165. return -EINVAL;
  3166. }
  3167. static int
  3168. hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
  3169. const char *cmd)
  3170. {
  3171. char ifname[IFNAMSIZ + 1];
  3172. struct hostapd_data * hapd;
  3173. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  3174. hapd = hostapd_get_iface(interfaces, ifname);
  3175. if (hapd) {
  3176. if (!fst_iface_detach(ifname)) {
  3177. hapd->iface->fst = NULL;
  3178. hapd->iface->fst_ies = NULL;
  3179. return 0;
  3180. }
  3181. }
  3182. }
  3183. return -EINVAL;
  3184. }
  3185. #endif /* CONFIG_FST */
  3186. static struct hostapd_data *
  3187. hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
  3188. const char *ifname)
  3189. {
  3190. size_t i, j;
  3191. for (i = 0; i < interfaces->count; i++) {
  3192. struct hostapd_iface *iface = interfaces->iface[i];
  3193. for (j = 0; j < iface->num_bss; j++) {
  3194. struct hostapd_data *hapd;
  3195. hapd = iface->bss[j];
  3196. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  3197. return hapd;
  3198. }
  3199. }
  3200. return NULL;
  3201. }
  3202. static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
  3203. struct hostapd_data *dst_hapd,
  3204. const char *param)
  3205. {
  3206. int res;
  3207. char *value;
  3208. value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  3209. if (!value) {
  3210. wpa_printf(MSG_ERROR,
  3211. "DUP: cannot allocate buffer to stringify %s",
  3212. param);
  3213. goto error_return;
  3214. }
  3215. if (os_strcmp(param, "wpa") == 0) {
  3216. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
  3217. src_hapd->conf->wpa);
  3218. } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
  3219. src_hapd->conf->wpa_key_mgmt) {
  3220. res = hostapd_ctrl_iface_get_key_mgmt(
  3221. src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  3222. if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
  3223. goto error_stringify;
  3224. } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
  3225. src_hapd->conf->wpa_pairwise) {
  3226. res = wpa_write_ciphers(value,
  3227. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  3228. src_hapd->conf->wpa_pairwise, " ");
  3229. if (res < 0)
  3230. goto error_stringify;
  3231. } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
  3232. src_hapd->conf->rsn_pairwise) {
  3233. res = wpa_write_ciphers(value,
  3234. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  3235. src_hapd->conf->rsn_pairwise, " ");
  3236. if (res < 0)
  3237. goto error_stringify;
  3238. } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
  3239. src_hapd->conf->ssid.wpa_passphrase) {
  3240. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
  3241. src_hapd->conf->ssid.wpa_passphrase);
  3242. } else if (os_strcmp(param, "wpa_psk") == 0 &&
  3243. src_hapd->conf->ssid.wpa_psk_set) {
  3244. wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  3245. src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  3246. } else {
  3247. wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
  3248. goto error_return;
  3249. }
  3250. res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
  3251. os_free(value);
  3252. return res;
  3253. error_stringify:
  3254. wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
  3255. error_return:
  3256. os_free(value);
  3257. return -1;
  3258. }
  3259. static int
  3260. hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
  3261. const char *input,
  3262. char *reply, int reply_size)
  3263. {
  3264. size_t i, j;
  3265. int res;
  3266. char *pos, *end;
  3267. struct hostapd_iface *iface;
  3268. int show_ctrl = 0;
  3269. if (input)
  3270. show_ctrl = !!os_strstr(input, "ctrl");
  3271. pos = reply;
  3272. end = reply + reply_size;
  3273. for (i = 0; i < interfaces->count; i++) {
  3274. iface = interfaces->iface[i];
  3275. for (j = 0; j < iface->num_bss; j++) {
  3276. struct hostapd_bss_config *conf;
  3277. conf = iface->conf->bss[j];
  3278. if (show_ctrl)
  3279. res = os_snprintf(pos, end - pos,
  3280. "%s ctrl_iface=%s\n",
  3281. conf->iface,
  3282. conf->ctrl_interface ?
  3283. conf->ctrl_interface : "N/A");
  3284. else
  3285. res = os_snprintf(pos, end - pos, "%s\n",
  3286. conf->iface);
  3287. if (os_snprintf_error(end - pos, res)) {
  3288. *pos = '\0';
  3289. return pos - reply;
  3290. }
  3291. pos += res;
  3292. }
  3293. }
  3294. return pos - reply;
  3295. }
  3296. static int
  3297. hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
  3298. char *cmd)
  3299. {
  3300. char *p_start = cmd, *p_end;
  3301. struct hostapd_data *src_hapd, *dst_hapd;
  3302. /* cmd: "<src ifname> <dst ifname> <variable name> */
  3303. p_end = os_strchr(p_start, ' ');
  3304. if (!p_end) {
  3305. wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
  3306. cmd);
  3307. return -1;
  3308. }
  3309. *p_end = '\0';
  3310. src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  3311. if (!src_hapd) {
  3312. wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
  3313. p_start);
  3314. return -1;
  3315. }
  3316. p_start = p_end + 1;
  3317. p_end = os_strchr(p_start, ' ');
  3318. if (!p_end) {
  3319. wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
  3320. cmd);
  3321. return -1;
  3322. }
  3323. *p_end = '\0';
  3324. dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  3325. if (!dst_hapd) {
  3326. wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
  3327. p_start);
  3328. return -1;
  3329. }
  3330. p_start = p_end + 1;
  3331. return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
  3332. }
  3333. static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
  3334. const char *ifname,
  3335. char *buf, char *reply,
  3336. int reply_size,
  3337. struct sockaddr_storage *from,
  3338. socklen_t fromlen)
  3339. {
  3340. struct hostapd_data *hapd;
  3341. hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
  3342. if (hapd == NULL) {
  3343. int res;
  3344. res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
  3345. if (os_snprintf_error(reply_size, res))
  3346. return -1;
  3347. return res;
  3348. }
  3349. return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
  3350. from, fromlen);
  3351. }
  3352. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  3353. void *sock_ctx)
  3354. {
  3355. void *interfaces = eloop_ctx;
  3356. char buffer[256], *buf = buffer;
  3357. int res;
  3358. struct sockaddr_storage from;
  3359. socklen_t fromlen = sizeof(from);
  3360. char *reply;
  3361. int reply_len;
  3362. const int reply_size = 4096;
  3363. #ifdef CONFIG_CTRL_IFACE_UDP
  3364. unsigned char lcookie[COOKIE_LEN];
  3365. #endif /* CONFIG_CTRL_IFACE_UDP */
  3366. res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
  3367. (struct sockaddr *) &from, &fromlen);
  3368. if (res < 0) {
  3369. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  3370. strerror(errno));
  3371. return;
  3372. }
  3373. buf[res] = '\0';
  3374. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  3375. reply = os_malloc(reply_size);
  3376. if (reply == NULL) {
  3377. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  3378. fromlen) < 0) {
  3379. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  3380. strerror(errno));
  3381. }
  3382. return;
  3383. }
  3384. os_memcpy(reply, "OK\n", 3);
  3385. reply_len = 3;
  3386. #ifdef CONFIG_CTRL_IFACE_UDP
  3387. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  3388. os_memcpy(reply, "COOKIE=", 7);
  3389. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  3390. gcookie, COOKIE_LEN);
  3391. reply_len = 7 + 2 * COOKIE_LEN;
  3392. goto send_reply;
  3393. }
  3394. if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
  3395. hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
  3396. wpa_printf(MSG_DEBUG,
  3397. "CTRL: No cookie in the request - drop request");
  3398. os_free(reply);
  3399. return;
  3400. }
  3401. if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
  3402. wpa_printf(MSG_DEBUG,
  3403. "CTRL: Invalid cookie in the request - drop request");
  3404. os_free(reply);
  3405. return;
  3406. }
  3407. buf += 7 + 2 * COOKIE_LEN;
  3408. while (*buf == ' ')
  3409. buf++;
  3410. #endif /* CONFIG_CTRL_IFACE_UDP */
  3411. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  3412. char *pos = os_strchr(buf + 7, ' ');
  3413. if (pos) {
  3414. *pos++ = '\0';
  3415. reply_len = hostapd_global_ctrl_iface_ifname(
  3416. interfaces, buf + 7, pos, reply, reply_size,
  3417. &from, fromlen);
  3418. goto send_reply;
  3419. }
  3420. }
  3421. if (os_strcmp(buf, "PING") == 0) {
  3422. os_memcpy(reply, "PONG\n", 5);
  3423. reply_len = 5;
  3424. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  3425. if (wpa_debug_reopen_file() < 0)
  3426. reply_len = -1;
  3427. } else if (os_strcmp(buf, "FLUSH") == 0) {
  3428. hostapd_ctrl_iface_flush(interfaces);
  3429. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  3430. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  3431. reply_len = -1;
  3432. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  3433. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  3434. reply_len = -1;
  3435. } else if (os_strcmp(buf, "ATTACH") == 0) {
  3436. if (hostapd_global_ctrl_iface_attach(interfaces, &from,
  3437. fromlen, NULL))
  3438. reply_len = -1;
  3439. } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
  3440. if (hostapd_global_ctrl_iface_attach(interfaces, &from,
  3441. fromlen, buf + 7))
  3442. reply_len = -1;
  3443. } else if (os_strcmp(buf, "DETACH") == 0) {
  3444. if (hostapd_global_ctrl_iface_detach(interfaces, &from,
  3445. fromlen))
  3446. reply_len = -1;
  3447. #ifdef CONFIG_MODULE_TESTS
  3448. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  3449. if (hapd_module_tests() < 0)
  3450. reply_len = -1;
  3451. #endif /* CONFIG_MODULE_TESTS */
  3452. #ifdef CONFIG_FST
  3453. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  3454. if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
  3455. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3456. else
  3457. reply_len = -1;
  3458. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  3459. if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
  3460. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3461. else
  3462. reply_len = -1;
  3463. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  3464. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  3465. #endif /* CONFIG_FST */
  3466. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  3467. if (!hostapd_global_ctrl_iface_dup_network(interfaces,
  3468. buf + 12))
  3469. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3470. else
  3471. reply_len = -1;
  3472. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  3473. reply_len = hostapd_global_ctrl_iface_interfaces(
  3474. interfaces, buf + 10, reply, sizeof(buffer));
  3475. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  3476. eloop_terminate();
  3477. } else {
  3478. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  3479. "ignored");
  3480. reply_len = -1;
  3481. }
  3482. send_reply:
  3483. if (reply_len < 0) {
  3484. os_memcpy(reply, "FAIL\n", 5);
  3485. reply_len = 5;
  3486. }
  3487. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  3488. fromlen) < 0) {
  3489. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  3490. strerror(errno));
  3491. }
  3492. os_free(reply);
  3493. }
  3494. #ifndef CONFIG_CTRL_IFACE_UDP
  3495. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  3496. {
  3497. char *buf;
  3498. size_t len;
  3499. if (interface->global_iface_path == NULL)
  3500. return NULL;
  3501. len = os_strlen(interface->global_iface_path) +
  3502. os_strlen(interface->global_iface_name) + 2;
  3503. buf = os_malloc(len);
  3504. if (buf == NULL)
  3505. return NULL;
  3506. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  3507. interface->global_iface_name);
  3508. buf[len - 1] = '\0';
  3509. return buf;
  3510. }
  3511. #endif /* CONFIG_CTRL_IFACE_UDP */
  3512. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  3513. {
  3514. #ifdef CONFIG_CTRL_IFACE_UDP
  3515. int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
  3516. char p[32] = { 0 };
  3517. char *pos;
  3518. struct addrinfo hints = { 0 }, *res, *saveres;
  3519. int n;
  3520. if (interface->global_ctrl_sock > -1) {
  3521. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  3522. return 0;
  3523. }
  3524. if (interface->global_iface_path == NULL)
  3525. return 0;
  3526. pos = os_strstr(interface->global_iface_path, "udp:");
  3527. if (pos) {
  3528. pos += 4;
  3529. port = atoi(pos);
  3530. if (port <= 0) {
  3531. wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
  3532. goto fail;
  3533. }
  3534. }
  3535. os_get_random(gcookie, COOKIE_LEN);
  3536. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  3537. hints.ai_flags = AI_PASSIVE;
  3538. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  3539. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  3540. hints.ai_family = AF_INET6;
  3541. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  3542. hints.ai_family = AF_INET;
  3543. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  3544. hints.ai_socktype = SOCK_DGRAM;
  3545. try_again:
  3546. os_snprintf(p, sizeof(p), "%d", port);
  3547. n = getaddrinfo(NULL, p, &hints, &res);
  3548. if (n) {
  3549. wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
  3550. goto fail;
  3551. }
  3552. saveres = res;
  3553. interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
  3554. res->ai_protocol);
  3555. if (interface->global_ctrl_sock < 0) {
  3556. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  3557. goto fail;
  3558. }
  3559. if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
  3560. 0) {
  3561. port++;
  3562. if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
  3563. HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
  3564. goto try_again;
  3565. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  3566. goto fail;
  3567. }
  3568. freeaddrinfo(saveres);
  3569. wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
  3570. if (eloop_register_read_sock(interface->global_ctrl_sock,
  3571. hostapd_global_ctrl_iface_receive,
  3572. interface, NULL) < 0) {
  3573. hostapd_global_ctrl_iface_deinit(interface);
  3574. return -1;
  3575. }
  3576. return 0;
  3577. fail:
  3578. if (interface->global_ctrl_sock >= 0)
  3579. close(interface->global_ctrl_sock);
  3580. return -1;
  3581. #else /* CONFIG_CTRL_IFACE_UDP */
  3582. struct sockaddr_un addr;
  3583. int s = -1;
  3584. char *fname = NULL;
  3585. if (interface->global_iface_path == NULL) {
  3586. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  3587. return 0;
  3588. }
  3589. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  3590. if (errno == EEXIST) {
  3591. wpa_printf(MSG_DEBUG, "Using existing control "
  3592. "interface directory.");
  3593. } else {
  3594. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  3595. strerror(errno));
  3596. goto fail;
  3597. }
  3598. } else if (interface->ctrl_iface_group &&
  3599. chown(interface->global_iface_path, -1,
  3600. interface->ctrl_iface_group) < 0) {
  3601. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  3602. strerror(errno));
  3603. goto fail;
  3604. }
  3605. if (os_strlen(interface->global_iface_path) + 1 +
  3606. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  3607. goto fail;
  3608. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  3609. if (s < 0) {
  3610. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  3611. goto fail;
  3612. }
  3613. os_memset(&addr, 0, sizeof(addr));
  3614. #ifdef __FreeBSD__
  3615. addr.sun_len = sizeof(addr);
  3616. #endif /* __FreeBSD__ */
  3617. addr.sun_family = AF_UNIX;
  3618. fname = hostapd_global_ctrl_iface_path(interface);
  3619. if (fname == NULL)
  3620. goto fail;
  3621. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  3622. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  3623. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  3624. strerror(errno));
  3625. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  3626. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  3627. " allow connections - assuming it was left"
  3628. "over from forced program termination");
  3629. if (unlink(fname) < 0) {
  3630. wpa_printf(MSG_ERROR,
  3631. "Could not unlink existing ctrl_iface socket '%s': %s",
  3632. fname, strerror(errno));
  3633. goto fail;
  3634. }
  3635. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  3636. 0) {
  3637. wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
  3638. strerror(errno));
  3639. goto fail;
  3640. }
  3641. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  3642. "ctrl_iface socket '%s'", fname);
  3643. } else {
  3644. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  3645. "be in use - cannot override it");
  3646. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  3647. "not used anymore", fname);
  3648. os_free(fname);
  3649. fname = NULL;
  3650. goto fail;
  3651. }
  3652. }
  3653. if (interface->ctrl_iface_group &&
  3654. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  3655. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  3656. strerror(errno));
  3657. goto fail;
  3658. }
  3659. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  3660. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  3661. strerror(errno));
  3662. goto fail;
  3663. }
  3664. os_free(fname);
  3665. interface->global_ctrl_sock = s;
  3666. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  3667. interface, NULL);
  3668. return 0;
  3669. fail:
  3670. if (s >= 0)
  3671. close(s);
  3672. if (fname) {
  3673. unlink(fname);
  3674. os_free(fname);
  3675. }
  3676. return -1;
  3677. #endif /* CONFIG_CTRL_IFACE_UDP */
  3678. }
  3679. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  3680. {
  3681. #ifndef CONFIG_CTRL_IFACE_UDP
  3682. char *fname = NULL;
  3683. #endif /* CONFIG_CTRL_IFACE_UDP */
  3684. struct wpa_ctrl_dst *dst, *prev;
  3685. if (interfaces->global_ctrl_sock > -1) {
  3686. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  3687. close(interfaces->global_ctrl_sock);
  3688. interfaces->global_ctrl_sock = -1;
  3689. #ifndef CONFIG_CTRL_IFACE_UDP
  3690. fname = hostapd_global_ctrl_iface_path(interfaces);
  3691. if (fname) {
  3692. unlink(fname);
  3693. os_free(fname);
  3694. }
  3695. if (interfaces->global_iface_path &&
  3696. rmdir(interfaces->global_iface_path) < 0) {
  3697. if (errno == ENOTEMPTY) {
  3698. wpa_printf(MSG_DEBUG, "Control interface "
  3699. "directory not empty - leaving it "
  3700. "behind");
  3701. } else {
  3702. wpa_printf(MSG_ERROR,
  3703. "rmdir[ctrl_interface=%s]: %s",
  3704. interfaces->global_iface_path,
  3705. strerror(errno));
  3706. }
  3707. }
  3708. #endif /* CONFIG_CTRL_IFACE_UDP */
  3709. }
  3710. os_free(interfaces->global_iface_path);
  3711. interfaces->global_iface_path = NULL;
  3712. dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
  3713. struct wpa_ctrl_dst, list)
  3714. os_free(dst);
  3715. }
  3716. static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
  3717. const char *buf)
  3718. {
  3719. /* Enable Probe Request events based on explicit request.
  3720. * Other events are enabled by default.
  3721. */
  3722. if (str_starts(buf, RX_PROBE_REQUEST))
  3723. return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
  3724. return 1;
  3725. }
  3726. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  3727. enum wpa_msg_type type,
  3728. const char *buf, size_t len)
  3729. {
  3730. struct wpa_ctrl_dst *dst, *next;
  3731. struct dl_list *ctrl_dst;
  3732. struct msghdr msg;
  3733. int idx;
  3734. struct iovec io[2];
  3735. char levelstr[10];
  3736. int s;
  3737. if (type != WPA_MSG_ONLY_GLOBAL) {
  3738. s = hapd->ctrl_sock;
  3739. ctrl_dst = &hapd->ctrl_dst;
  3740. } else {
  3741. s = hapd->iface->interfaces->global_ctrl_sock;
  3742. ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
  3743. }
  3744. if (s < 0 || dl_list_empty(ctrl_dst))
  3745. return;
  3746. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  3747. io[0].iov_base = levelstr;
  3748. io[0].iov_len = os_strlen(levelstr);
  3749. io[1].iov_base = (char *) buf;
  3750. io[1].iov_len = len;
  3751. os_memset(&msg, 0, sizeof(msg));
  3752. msg.msg_iov = io;
  3753. msg.msg_iovlen = 2;
  3754. idx = 0;
  3755. dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
  3756. if ((level >= dst->debug_level) &&
  3757. hostapd_ctrl_check_event_enabled(dst, buf)) {
  3758. sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
  3759. &dst->addr, dst->addrlen);
  3760. msg.msg_name = &dst->addr;
  3761. msg.msg_namelen = dst->addrlen;
  3762. if (sendmsg(s, &msg, 0) < 0) {
  3763. int _errno = errno;
  3764. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  3765. "%d - %s",
  3766. idx, errno, strerror(errno));
  3767. dst->errors++;
  3768. if (dst->errors > 10 || _errno == ENOENT) {
  3769. if (type != WPA_MSG_ONLY_GLOBAL)
  3770. hostapd_ctrl_iface_detach(
  3771. hapd, &dst->addr,
  3772. dst->addrlen);
  3773. else
  3774. hostapd_global_ctrl_iface_detach(
  3775. hapd->iface->interfaces,
  3776. &dst->addr,
  3777. dst->addrlen);
  3778. }
  3779. } else
  3780. dst->errors = 0;
  3781. }
  3782. idx++;
  3783. }
  3784. }
  3785. #endif /* CONFIG_NATIVE_WINDOWS */