ctrl_iface.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462
  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. wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
  1658. /* First, use a zero key to avoid any possible duplicate key
  1659. * avoidance in the driver. */
  1660. if (hostapd_drv_set_key(hapd->conf->iface, hapd,
  1661. hapd->last_gtk_alg,
  1662. broadcast_ether_addr,
  1663. hapd->last_gtk_key_idx, 1, NULL, 0,
  1664. zero, hapd->last_gtk_len) < 0)
  1665. return -1;
  1666. /* Set the previously configured key to reset its TSC */
  1667. return hostapd_drv_set_key(hapd->conf->iface, hapd,
  1668. hapd->last_gtk_alg,
  1669. broadcast_ether_addr,
  1670. hapd->last_gtk_key_idx, 1, NULL, 0,
  1671. hapd->last_gtk, hapd->last_gtk_len);
  1672. }
  1673. sta = ap_get_sta(hapd, addr);
  1674. if (!sta)
  1675. return -1;
  1676. if (sta->last_tk_alg == WPA_ALG_NONE)
  1677. return -1;
  1678. wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
  1679. MAC2STR(sta->addr));
  1680. /* First, use a zero key to avoid any possible duplicate key avoidance
  1681. * in the driver. */
  1682. if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
  1683. sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
  1684. zero, sta->last_tk_len) < 0)
  1685. return -1;
  1686. /* Set the previously configured key to reset its TSC/RSC */
  1687. return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
  1688. sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
  1689. sta->last_tk, sta->last_tk_len);
  1690. }
  1691. static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
  1692. {
  1693. u8 addr[ETH_ALEN];
  1694. const char *pos = cmd;
  1695. enum wpa_alg alg;
  1696. int idx, set_tx;
  1697. u8 seq[6], key[WPA_TK_MAX_LEN];
  1698. size_t key_len;
  1699. /* parameters: alg addr idx set_tx seq key */
  1700. alg = atoi(pos);
  1701. pos = os_strchr(pos, ' ');
  1702. if (!pos)
  1703. return -1;
  1704. pos++;
  1705. if (hwaddr_aton(pos, addr))
  1706. return -1;
  1707. pos += 17;
  1708. if (*pos != ' ')
  1709. return -1;
  1710. pos++;
  1711. idx = atoi(pos);
  1712. pos = os_strchr(pos, ' ');
  1713. if (!pos)
  1714. return -1;
  1715. pos++;
  1716. set_tx = atoi(pos);
  1717. pos = os_strchr(pos, ' ');
  1718. if (!pos)
  1719. return -1;
  1720. pos++;
  1721. if (hexstr2bin(pos, seq, sizeof(6)) < 0)
  1722. return -1;
  1723. pos += 2 * 6;
  1724. if (*pos != ' ')
  1725. return -1;
  1726. pos++;
  1727. key_len = os_strlen(pos) / 2;
  1728. if (hexstr2bin(pos, key, key_len) < 0)
  1729. return -1;
  1730. wpa_printf(MSG_INFO, "TESTING: Set key");
  1731. return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx,
  1732. set_tx, seq, 6, key, key_len);
  1733. }
  1734. static void restore_tk(void *ctx1, void *ctx2)
  1735. {
  1736. struct hostapd_data *hapd = ctx1;
  1737. struct sta_info *sta = ctx2;
  1738. wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
  1739. MAC2STR(sta->addr));
  1740. /* This does not really restore the TSC properly, so this will result
  1741. * in replay protection issues for now since there is no clean way of
  1742. * preventing encryption of a single EAPOL frame. */
  1743. hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
  1744. sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
  1745. sta->last_tk, sta->last_tk_len);
  1746. }
  1747. static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
  1748. {
  1749. struct sta_info *sta;
  1750. u8 addr[ETH_ALEN];
  1751. int plain = os_strstr(cmd, "plaintext") != NULL;
  1752. if (hwaddr_aton(cmd, addr))
  1753. return -1;
  1754. sta = ap_get_sta(hapd, addr);
  1755. if (!sta || !sta->wpa_sm)
  1756. return -1;
  1757. if (plain && sta->last_tk_alg == WPA_ALG_NONE)
  1758. plain = 0; /* no need for special processing */
  1759. if (plain) {
  1760. wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
  1761. MAC2STR(sta->addr));
  1762. hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
  1763. sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
  1764. NULL, 0);
  1765. }
  1766. wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
  1767. return wpa_auth_resend_m1(sta->wpa_sm,
  1768. os_strstr(cmd, "change-anonce") != NULL,
  1769. plain ? restore_tk : NULL, hapd, sta);
  1770. }
  1771. static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
  1772. {
  1773. struct sta_info *sta;
  1774. u8 addr[ETH_ALEN];
  1775. int plain = os_strstr(cmd, "plaintext") != NULL;
  1776. if (hwaddr_aton(cmd, addr))
  1777. return -1;
  1778. sta = ap_get_sta(hapd, addr);
  1779. if (!sta || !sta->wpa_sm)
  1780. return -1;
  1781. if (plain && sta->last_tk_alg == WPA_ALG_NONE)
  1782. plain = 0; /* no need for special processing */
  1783. if (plain) {
  1784. wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
  1785. MAC2STR(sta->addr));
  1786. hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
  1787. sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
  1788. NULL, 0);
  1789. }
  1790. wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
  1791. return wpa_auth_resend_m3(sta->wpa_sm,
  1792. plain ? restore_tk : NULL, hapd, sta);
  1793. }
  1794. static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
  1795. const char *cmd)
  1796. {
  1797. struct sta_info *sta;
  1798. u8 addr[ETH_ALEN];
  1799. int plain = os_strstr(cmd, "plaintext") != NULL;
  1800. if (hwaddr_aton(cmd, addr))
  1801. return -1;
  1802. sta = ap_get_sta(hapd, addr);
  1803. if (!sta || !sta->wpa_sm)
  1804. return -1;
  1805. if (plain && sta->last_tk_alg == WPA_ALG_NONE)
  1806. plain = 0; /* no need for special processing */
  1807. if (plain) {
  1808. wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
  1809. MAC2STR(sta->addr));
  1810. hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
  1811. sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
  1812. NULL, 0);
  1813. }
  1814. wpa_printf(MSG_INFO,
  1815. "TESTING: Send group M1 for the same GTK and zero RSC to "
  1816. MACSTR, MAC2STR(sta->addr));
  1817. return wpa_auth_resend_group_m1(sta->wpa_sm,
  1818. plain ? restore_tk : NULL, hapd, sta);
  1819. }
  1820. #endif /* CONFIG_TESTING_OPTIONS */
  1821. static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
  1822. char *pos)
  1823. {
  1824. #ifdef NEED_AP_MLME
  1825. struct csa_settings settings;
  1826. int ret;
  1827. unsigned int i;
  1828. ret = hostapd_parse_csa_settings(pos, &settings);
  1829. if (ret)
  1830. return ret;
  1831. for (i = 0; i < iface->num_bss; i++) {
  1832. ret = hostapd_switch_channel(iface->bss[i], &settings);
  1833. if (ret) {
  1834. /* FIX: What do we do if CSA fails in the middle of
  1835. * submitting multi-BSS CSA requests? */
  1836. return ret;
  1837. }
  1838. }
  1839. return 0;
  1840. #else /* NEED_AP_MLME */
  1841. return -1;
  1842. #endif /* NEED_AP_MLME */
  1843. }
  1844. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  1845. int reply_size, const char *param)
  1846. {
  1847. #ifdef RADIUS_SERVER
  1848. if (os_strcmp(param, "radius_server") == 0) {
  1849. return radius_server_get_mib(hapd->radius_srv, reply,
  1850. reply_size);
  1851. }
  1852. #endif /* RADIUS_SERVER */
  1853. return -1;
  1854. }
  1855. static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
  1856. char *buf, size_t buflen)
  1857. {
  1858. int ret;
  1859. char *pos;
  1860. u8 *data = NULL;
  1861. unsigned int vendor_id, subcmd;
  1862. struct wpabuf *reply;
  1863. size_t data_len = 0;
  1864. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  1865. vendor_id = strtoul(cmd, &pos, 16);
  1866. if (!isblank((unsigned char) *pos))
  1867. return -EINVAL;
  1868. subcmd = strtoul(pos, &pos, 10);
  1869. if (*pos != '\0') {
  1870. if (!isblank((unsigned char) *pos++))
  1871. return -EINVAL;
  1872. data_len = os_strlen(pos);
  1873. }
  1874. if (data_len) {
  1875. data_len /= 2;
  1876. data = os_malloc(data_len);
  1877. if (!data)
  1878. return -ENOBUFS;
  1879. if (hexstr2bin(pos, data, data_len)) {
  1880. wpa_printf(MSG_DEBUG,
  1881. "Vendor command: wrong parameter format");
  1882. os_free(data);
  1883. return -EINVAL;
  1884. }
  1885. }
  1886. reply = wpabuf_alloc((buflen - 1) / 2);
  1887. if (!reply) {
  1888. os_free(data);
  1889. return -ENOBUFS;
  1890. }
  1891. ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
  1892. reply);
  1893. if (ret == 0)
  1894. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  1895. wpabuf_len(reply));
  1896. wpabuf_free(reply);
  1897. os_free(data);
  1898. return ret;
  1899. }
  1900. static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
  1901. const char *cmd)
  1902. {
  1903. u8 addr[ETH_ALEN];
  1904. struct sta_info *sta;
  1905. if (hwaddr_aton(cmd, addr))
  1906. return -1;
  1907. sta = ap_get_sta(hapd, addr);
  1908. if (!sta || !sta->eapol_sm)
  1909. return -1;
  1910. eapol_auth_reauthenticate(sta->eapol_sm);
  1911. return 0;
  1912. }
  1913. static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
  1914. {
  1915. u8 addr[ETH_ALEN];
  1916. struct sta_info *sta;
  1917. char *pos = cmd, *param;
  1918. if (hwaddr_aton(pos, addr) || pos[17] != ' ')
  1919. return -1;
  1920. pos += 18;
  1921. param = pos;
  1922. pos = os_strchr(pos, ' ');
  1923. if (!pos)
  1924. return -1;
  1925. *pos++ = '\0';
  1926. sta = ap_get_sta(hapd, addr);
  1927. if (!sta || !sta->eapol_sm)
  1928. return -1;
  1929. return eapol_auth_set_conf(sta->eapol_sm, param, pos);
  1930. }
  1931. static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
  1932. char *buf, size_t buflen)
  1933. {
  1934. char *pos, *end, *stamp;
  1935. int ret;
  1936. /* cmd: "LOG_LEVEL [<level>]" */
  1937. if (*cmd == '\0') {
  1938. pos = buf;
  1939. end = buf + buflen;
  1940. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1941. "Timestamp: %d\n",
  1942. debug_level_str(wpa_debug_level),
  1943. wpa_debug_timestamp);
  1944. if (os_snprintf_error(end - pos, ret))
  1945. ret = 0;
  1946. return ret;
  1947. }
  1948. while (*cmd == ' ')
  1949. cmd++;
  1950. stamp = os_strchr(cmd, ' ');
  1951. if (stamp) {
  1952. *stamp++ = '\0';
  1953. while (*stamp == ' ') {
  1954. stamp++;
  1955. }
  1956. }
  1957. if (os_strlen(cmd)) {
  1958. int level = str_to_debug_level(cmd);
  1959. if (level < 0)
  1960. return -1;
  1961. wpa_debug_level = level;
  1962. }
  1963. if (stamp && os_strlen(stamp))
  1964. wpa_debug_timestamp = atoi(stamp);
  1965. os_memcpy(buf, "OK\n", 3);
  1966. return 3;
  1967. }
  1968. #ifdef NEED_AP_MLME
  1969. static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
  1970. char *buf, size_t buflen)
  1971. {
  1972. struct hostapd_iface *iface = hapd->iface;
  1973. char *pos, *end;
  1974. struct hostapd_sta_info *info;
  1975. struct os_reltime now;
  1976. if (!iface->num_sta_seen)
  1977. return 0;
  1978. sta_track_expire(iface, 0);
  1979. pos = buf;
  1980. end = buf + buflen;
  1981. os_get_reltime(&now);
  1982. dl_list_for_each_reverse(info, &iface->sta_seen,
  1983. struct hostapd_sta_info, list) {
  1984. struct os_reltime age;
  1985. int ret;
  1986. os_reltime_sub(&now, &info->last_seen, &age);
  1987. ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
  1988. MAC2STR(info->addr), (unsigned int) age.sec,
  1989. info->ssi_signal);
  1990. if (os_snprintf_error(end - pos, ret))
  1991. break;
  1992. pos += ret;
  1993. }
  1994. return pos - buf;
  1995. }
  1996. #endif /* NEED_AP_MLME */
  1997. static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
  1998. const char *cmd)
  1999. {
  2000. u8 addr[ETH_ALEN];
  2001. if (hwaddr_aton(cmd, addr)) {
  2002. wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
  2003. return -1;
  2004. }
  2005. return hostapd_send_lci_req(hapd, addr);
  2006. }
  2007. static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
  2008. {
  2009. u8 addr[ETH_ALEN];
  2010. char *token, *context = NULL;
  2011. int random_interval, min_ap;
  2012. u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
  2013. unsigned int n_responders;
  2014. token = str_token(cmd, " ", &context);
  2015. if (!token || hwaddr_aton(token, addr)) {
  2016. wpa_printf(MSG_INFO,
  2017. "CTRL: REQ_RANGE - Bad destination address");
  2018. return -1;
  2019. }
  2020. token = str_token(cmd, " ", &context);
  2021. if (!token)
  2022. return -1;
  2023. random_interval = atoi(token);
  2024. if (random_interval < 0 || random_interval > 0xffff)
  2025. return -1;
  2026. token = str_token(cmd, " ", &context);
  2027. if (!token)
  2028. return -1;
  2029. min_ap = atoi(token);
  2030. if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
  2031. return -1;
  2032. n_responders = 0;
  2033. while ((token = str_token(cmd, " ", &context))) {
  2034. if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
  2035. wpa_printf(MSG_INFO,
  2036. "CTRL: REQ_RANGE: Too many responders");
  2037. return -1;
  2038. }
  2039. if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
  2040. wpa_printf(MSG_INFO,
  2041. "CTRL: REQ_RANGE: Bad responder address");
  2042. return -1;
  2043. }
  2044. n_responders++;
  2045. }
  2046. if (!n_responders) {
  2047. wpa_printf(MSG_INFO,
  2048. "CTRL: REQ_RANGE - No FTM responder address");
  2049. return -1;
  2050. }
  2051. return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
  2052. responders, n_responders);
  2053. }
  2054. static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
  2055. const char *cmd, char *reply,
  2056. size_t reply_size)
  2057. {
  2058. u8 addr[ETH_ALEN];
  2059. const char *pos;
  2060. struct wpabuf *req;
  2061. int ret;
  2062. u8 req_mode = 0;
  2063. if (hwaddr_aton(cmd, addr))
  2064. return -1;
  2065. pos = os_strchr(cmd, ' ');
  2066. if (!pos)
  2067. return -1;
  2068. pos++;
  2069. if (os_strncmp(pos, "req_mode=", 9) == 0) {
  2070. int val = hex2byte(pos + 9);
  2071. if (val < 0)
  2072. return -1;
  2073. req_mode = val;
  2074. pos += 11;
  2075. pos = os_strchr(pos, ' ');
  2076. if (!pos)
  2077. return -1;
  2078. pos++;
  2079. }
  2080. req = wpabuf_parse_bin(pos);
  2081. if (!req)
  2082. return -1;
  2083. ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
  2084. wpabuf_free(req);
  2085. if (ret >= 0)
  2086. ret = os_snprintf(reply, reply_size, "%d", ret);
  2087. return ret;
  2088. }
  2089. static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
  2090. {
  2091. struct wpa_ssid_value ssid;
  2092. u8 bssid[ETH_ALEN];
  2093. struct wpabuf *nr, *lci = NULL, *civic = NULL;
  2094. int stationary = 0;
  2095. char *tmp;
  2096. int ret;
  2097. if (!(hapd->conf->radio_measurements[0] &
  2098. WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
  2099. wpa_printf(MSG_ERROR,
  2100. "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
  2101. return -1;
  2102. }
  2103. if (hwaddr_aton(buf, bssid)) {
  2104. wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
  2105. return -1;
  2106. }
  2107. tmp = os_strstr(buf, "ssid=");
  2108. if (!tmp || ssid_parse(tmp + 5, &ssid)) {
  2109. wpa_printf(MSG_ERROR,
  2110. "CTRL: SET_NEIGHBOR: Bad or missing SSID");
  2111. return -1;
  2112. }
  2113. buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
  2114. if (!buf)
  2115. return -1;
  2116. tmp = os_strstr(buf, "nr=");
  2117. if (!tmp) {
  2118. wpa_printf(MSG_ERROR,
  2119. "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
  2120. return -1;
  2121. }
  2122. buf = os_strchr(tmp, ' ');
  2123. if (buf)
  2124. *buf++ = '\0';
  2125. nr = wpabuf_parse_bin(tmp + 3);
  2126. if (!nr) {
  2127. wpa_printf(MSG_ERROR,
  2128. "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
  2129. return -1;
  2130. }
  2131. if (!buf)
  2132. goto set;
  2133. tmp = os_strstr(buf, "lci=");
  2134. if (tmp) {
  2135. buf = os_strchr(tmp, ' ');
  2136. if (buf)
  2137. *buf++ = '\0';
  2138. lci = wpabuf_parse_bin(tmp + 4);
  2139. if (!lci) {
  2140. wpa_printf(MSG_ERROR,
  2141. "CTRL: SET_NEIGHBOR: Bad LCI subelement");
  2142. wpabuf_free(nr);
  2143. return -1;
  2144. }
  2145. }
  2146. if (!buf)
  2147. goto set;
  2148. tmp = os_strstr(buf, "civic=");
  2149. if (tmp) {
  2150. buf = os_strchr(tmp, ' ');
  2151. if (buf)
  2152. *buf++ = '\0';
  2153. civic = wpabuf_parse_bin(tmp + 6);
  2154. if (!civic) {
  2155. wpa_printf(MSG_ERROR,
  2156. "CTRL: SET_NEIGHBOR: Bad civic subelement");
  2157. wpabuf_free(nr);
  2158. wpabuf_free(lci);
  2159. return -1;
  2160. }
  2161. }
  2162. if (!buf)
  2163. goto set;
  2164. if (os_strstr(buf, "stat"))
  2165. stationary = 1;
  2166. set:
  2167. ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
  2168. stationary);
  2169. wpabuf_free(nr);
  2170. wpabuf_free(lci);
  2171. wpabuf_free(civic);
  2172. return ret;
  2173. }
  2174. static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
  2175. char *buf)
  2176. {
  2177. struct wpa_ssid_value ssid;
  2178. u8 bssid[ETH_ALEN];
  2179. char *tmp;
  2180. if (hwaddr_aton(buf, bssid)) {
  2181. wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
  2182. return -1;
  2183. }
  2184. tmp = os_strstr(buf, "ssid=");
  2185. if (!tmp || ssid_parse(tmp + 5, &ssid)) {
  2186. wpa_printf(MSG_ERROR,
  2187. "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
  2188. return -1;
  2189. }
  2190. return hostapd_neighbor_remove(hapd, bssid, &ssid);
  2191. }
  2192. static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
  2193. size_t buflen)
  2194. {
  2195. int ret, i;
  2196. char *pos, *end;
  2197. ret = os_snprintf(buf, buflen, "%016llX:\n",
  2198. (long long unsigned) iface->drv_flags);
  2199. if (os_snprintf_error(buflen, ret))
  2200. return -1;
  2201. pos = buf + ret;
  2202. end = buf + buflen;
  2203. for (i = 0; i < 64; i++) {
  2204. if (iface->drv_flags & (1LLU << i)) {
  2205. ret = os_snprintf(pos, end - pos, "%s\n",
  2206. driver_flag_to_string(1LLU << i));
  2207. if (os_snprintf_error(end - pos, ret))
  2208. return -1;
  2209. pos += ret;
  2210. }
  2211. }
  2212. return pos - buf;
  2213. }
  2214. #ifdef KRACK_TEST_CLIENT
  2215. static int hostapd_get_tk(struct hostapd_data *hapd, const char *txtaddr, char *buf, size_t buflen)
  2216. {
  2217. u8 addr[ETH_ALEN];
  2218. struct sta_info *sta;
  2219. int klen;
  2220. int res;
  2221. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET_TK %s", txtaddr);
  2222. if (hwaddr_aton(txtaddr, addr)) return -1;
  2223. sta = ap_get_sta(hapd, addr);
  2224. if (!sta) return -1;
  2225. if (sta->wpa_sm == NULL) return -1;
  2226. klen = wpa_cipher_key_len(sta->wpa_sm->pairwise);
  2227. if (klen <= 0) return -1;
  2228. res = wpa_snprintf_hex(buf, buflen, sta->wpa_sm->PTK.tk, klen);
  2229. buf[res++] = '\n';
  2230. buf[res] = '\0';
  2231. return res;
  2232. }
  2233. #endif
  2234. static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
  2235. const char *txtaddr)
  2236. {
  2237. u8 addr[ETH_ALEN];
  2238. struct vlan_description vlan_id;
  2239. if (!(*num))
  2240. return 0;
  2241. if (hwaddr_aton(txtaddr, addr))
  2242. return -1;
  2243. if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
  2244. hostapd_remove_acl_mac(acl, num, addr);
  2245. return 0;
  2246. }
  2247. static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
  2248. int *num)
  2249. {
  2250. while (*num)
  2251. hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
  2252. }
  2253. static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
  2254. char *buf, size_t buflen)
  2255. {
  2256. int i = 0, len = 0, ret = 0;
  2257. if (!acl)
  2258. return 0;
  2259. while (i < num) {
  2260. ret = os_snprintf(buf + len, buflen - len,
  2261. MACSTR " VLAN_ID=%d\n",
  2262. MAC2STR(acl[i].addr),
  2263. acl[i].vlan_id.untagged);
  2264. if (ret < 0 || (size_t) ret >= buflen - len)
  2265. return len;
  2266. i++;
  2267. len += ret;
  2268. }
  2269. return len;
  2270. }
  2271. static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
  2272. const char *cmd)
  2273. {
  2274. u8 addr[ETH_ALEN];
  2275. struct vlan_description vlan_id;
  2276. int ret = 0, vlanid = 0;
  2277. const char *pos;
  2278. if (hwaddr_aton(cmd, addr))
  2279. return -1;
  2280. pos = os_strstr(cmd, "VLAN_ID=");
  2281. if (pos)
  2282. vlanid = atoi(pos + 8);
  2283. if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
  2284. ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
  2285. if (ret != -1 && *acl)
  2286. qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
  2287. }
  2288. return ret < 0 ? -1 : 0;
  2289. }
  2290. static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
  2291. char *buf, char *reply,
  2292. int reply_size,
  2293. struct sockaddr_storage *from,
  2294. socklen_t fromlen)
  2295. {
  2296. int reply_len, res;
  2297. os_memcpy(reply, "OK\n", 3);
  2298. reply_len = 3;
  2299. if (os_strcmp(buf, "PING") == 0) {
  2300. os_memcpy(reply, "PONG\n", 5);
  2301. reply_len = 5;
  2302. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  2303. if (wpa_debug_reopen_file() < 0)
  2304. reply_len = -1;
  2305. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  2306. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  2307. } else if (os_strcmp(buf, "STATUS") == 0) {
  2308. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  2309. reply_size);
  2310. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  2311. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  2312. } else if (os_strcmp(buf, "MIB") == 0) {
  2313. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  2314. if (reply_len >= 0) {
  2315. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  2316. reply_size - reply_len);
  2317. if (res < 0)
  2318. reply_len = -1;
  2319. else
  2320. reply_len += res;
  2321. }
  2322. if (reply_len >= 0) {
  2323. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  2324. reply_size - reply_len);
  2325. if (res < 0)
  2326. reply_len = -1;
  2327. else
  2328. reply_len += res;
  2329. }
  2330. #ifndef CONFIG_NO_RADIUS
  2331. if (reply_len >= 0) {
  2332. res = radius_client_get_mib(hapd->radius,
  2333. reply + reply_len,
  2334. reply_size - reply_len);
  2335. if (res < 0)
  2336. reply_len = -1;
  2337. else
  2338. reply_len += res;
  2339. }
  2340. #endif /* CONFIG_NO_RADIUS */
  2341. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  2342. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  2343. buf + 4);
  2344. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  2345. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  2346. reply_size);
  2347. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  2348. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  2349. reply_size);
  2350. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  2351. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  2352. reply_size);
  2353. } else if (os_strcmp(buf, "ATTACH") == 0) {
  2354. if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
  2355. reply_len = -1;
  2356. } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
  2357. if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
  2358. reply_len = -1;
  2359. } else if (os_strcmp(buf, "DETACH") == 0) {
  2360. if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
  2361. reply_len = -1;
  2362. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  2363. if (hostapd_ctrl_iface_level(hapd, from, fromlen,
  2364. buf + 6))
  2365. reply_len = -1;
  2366. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  2367. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  2368. reply_len = -1;
  2369. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  2370. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  2371. reply_len = -1;
  2372. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  2373. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  2374. reply_len = -1;
  2375. #ifdef CONFIG_TAXONOMY
  2376. } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
  2377. reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
  2378. reply, reply_size);
  2379. #endif /* CONFIG_TAXONOMY */
  2380. } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
  2381. if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
  2382. reply_len = -1;
  2383. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  2384. if (hostapd_ctrl_iface_stop_ap(hapd))
  2385. reply_len = -1;
  2386. #ifdef CONFIG_IEEE80211W
  2387. #ifdef NEED_AP_MLME
  2388. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  2389. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  2390. reply_len = -1;
  2391. #endif /* NEED_AP_MLME */
  2392. #endif /* CONFIG_IEEE80211W */
  2393. #ifdef CONFIG_WPS
  2394. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  2395. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  2396. reply_len = -1;
  2397. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  2398. reply_len = hostapd_ctrl_iface_wps_check_pin(
  2399. hapd, buf + 14, reply, reply_size);
  2400. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  2401. if (hostapd_wps_button_pushed(hapd, NULL))
  2402. reply_len = -1;
  2403. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  2404. if (hostapd_wps_cancel(hapd))
  2405. reply_len = -1;
  2406. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  2407. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  2408. reply, reply_size);
  2409. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  2410. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  2411. reply_len = -1;
  2412. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  2413. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  2414. reply_size);
  2415. #ifdef CONFIG_WPS_NFC
  2416. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  2417. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  2418. reply_len = -1;
  2419. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  2420. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  2421. hapd, buf + 21, reply, reply_size);
  2422. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  2423. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  2424. hapd, buf + 14, reply, reply_size);
  2425. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  2426. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  2427. hapd, buf + 21, reply, reply_size);
  2428. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  2429. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  2430. reply_len = -1;
  2431. #endif /* CONFIG_WPS_NFC */
  2432. #endif /* CONFIG_WPS */
  2433. #ifdef CONFIG_INTERWORKING
  2434. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  2435. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  2436. reply_len = -1;
  2437. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  2438. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  2439. reply_len = -1;
  2440. #endif /* CONFIG_INTERWORKING */
  2441. #ifdef CONFIG_HS20
  2442. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  2443. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  2444. reply_len = -1;
  2445. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  2446. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  2447. reply_len = -1;
  2448. #endif /* CONFIG_HS20 */
  2449. #ifdef CONFIG_WNM_AP
  2450. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  2451. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  2452. reply_len = -1;
  2453. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  2454. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  2455. reply_len = -1;
  2456. } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
  2457. if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
  2458. reply_len = -1;
  2459. #endif /* CONFIG_WNM_AP */
  2460. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  2461. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  2462. reply_size);
  2463. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  2464. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  2465. reply_len = -1;
  2466. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  2467. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  2468. reply_size);
  2469. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  2470. if (hostapd_ctrl_iface_enable(hapd->iface))
  2471. reply_len = -1;
  2472. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  2473. if (hostapd_ctrl_iface_reload(hapd->iface))
  2474. reply_len = -1;
  2475. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  2476. if (hostapd_ctrl_iface_disable(hapd->iface))
  2477. reply_len = -1;
  2478. } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
  2479. if (ieee802_11_set_beacon(hapd))
  2480. reply_len = -1;
  2481. #ifdef CONFIG_TESTING_OPTIONS
  2482. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  2483. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  2484. reply_len = -1;
  2485. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  2486. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  2487. reply_len = -1;
  2488. } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
  2489. if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
  2490. buf + 23) < 0)
  2491. reply_len = -1;
  2492. } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
  2493. if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
  2494. reply_len = -1;
  2495. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  2496. if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
  2497. reply_len = -1;
  2498. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  2499. if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
  2500. reply_len = -1;
  2501. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  2502. if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
  2503. reply_len = -1;
  2504. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  2505. if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
  2506. reply_len = -1;
  2507. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  2508. if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
  2509. reply_len = -1;
  2510. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  2511. reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
  2512. reply_size);
  2513. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  2514. if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
  2515. reply_len = -1;
  2516. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  2517. reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
  2518. } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
  2519. if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
  2520. reply_len = -1;
  2521. } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
  2522. if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
  2523. reply_len = -1;
  2524. } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
  2525. if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
  2526. reply_len = -1;
  2527. } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
  2528. if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
  2529. reply_len = -1;
  2530. } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
  2531. if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
  2532. reply_len = -1;
  2533. } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
  2534. if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
  2535. reply_len = -1;
  2536. #endif /* CONFIG_TESTING_OPTIONS */
  2537. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  2538. if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
  2539. reply_len = -1;
  2540. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  2541. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  2542. reply_size);
  2543. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  2544. ieee802_1x_erp_flush(hapd);
  2545. #ifdef RADIUS_SERVER
  2546. radius_server_erp_flush(hapd->radius_srv);
  2547. #endif /* RADIUS_SERVER */
  2548. } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
  2549. if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
  2550. reply_len = -1;
  2551. } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
  2552. if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
  2553. reply_len = -1;
  2554. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  2555. reply_len = hostapd_ctrl_iface_log_level(
  2556. hapd, buf + 9, reply, reply_size);
  2557. #ifdef NEED_AP_MLME
  2558. } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
  2559. reply_len = hostapd_ctrl_iface_track_sta_list(
  2560. hapd, reply, reply_size);
  2561. #endif /* NEED_AP_MLME */
  2562. } else if (os_strcmp(buf, "PMKSA") == 0) {
  2563. reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
  2564. reply_size);
  2565. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  2566. hostapd_ctrl_iface_pmksa_flush(hapd);
  2567. } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
  2568. if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
  2569. reply_len = -1;
  2570. } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
  2571. if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
  2572. reply_len = -1;
  2573. } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
  2574. if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
  2575. reply_len = -1;
  2576. } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
  2577. if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
  2578. reply_len = -1;
  2579. } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
  2580. if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
  2581. reply_len = -1;
  2582. } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
  2583. reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
  2584. reply, reply_size);
  2585. } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
  2586. reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
  2587. reply_size);
  2588. #ifdef KRACK_TEST_CLIENT
  2589. } else if (os_strcmp(buf, "TEST_TPTK") == 0) {
  2590. poc_test_tptk_construction(hapd->wpa_auth, TEST_TPTK_REPLAY);
  2591. } else if (os_strcmp(buf, "TEST_TPTK_RAND") == 0) {
  2592. poc_test_tptk_construction(hapd->wpa_auth, TEST_TPTK_RAND);
  2593. } else if (os_strcmp(buf, "START_GROUP_TESTS") == 0) {
  2594. poc_start_testing_group_handshake(hapd->wpa_auth);
  2595. } else if (os_strncmp(buf, "GET_TK ", 7) == 0) {
  2596. reply_len = hostapd_get_tk(hapd, buf + 7, reply, reply_size);
  2597. #endif
  2598. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2599. eloop_terminate();
  2600. } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
  2601. if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
  2602. if (!hostapd_ctrl_iface_acl_add_mac(
  2603. &hapd->conf->accept_mac,
  2604. &hapd->conf->num_accept_mac, buf + 19))
  2605. hostapd_disassoc_accept_mac(hapd);
  2606. else
  2607. reply_len = -1;
  2608. } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
  2609. hostapd_ctrl_iface_acl_del_mac(
  2610. &hapd->conf->accept_mac,
  2611. &hapd->conf->num_accept_mac, buf + 19);
  2612. } else if (os_strcmp(buf + 11, "SHOW") == 0) {
  2613. reply_len = hostapd_ctrl_iface_acl_show_mac(
  2614. hapd->conf->accept_mac,
  2615. hapd->conf->num_accept_mac, reply, reply_size);
  2616. } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
  2617. hostapd_ctrl_iface_acl_clear_list(
  2618. &hapd->conf->accept_mac,
  2619. &hapd->conf->num_accept_mac);
  2620. }
  2621. } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
  2622. if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
  2623. if (!hostapd_ctrl_iface_acl_add_mac(
  2624. &hapd->conf->deny_mac,
  2625. &hapd->conf->num_deny_mac, buf + 17))
  2626. hostapd_disassoc_deny_mac(hapd);
  2627. } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
  2628. hostapd_ctrl_iface_acl_del_mac(
  2629. &hapd->conf->deny_mac,
  2630. &hapd->conf->num_deny_mac, buf + 17);
  2631. } else if (os_strcmp(buf + 9, "SHOW") == 0) {
  2632. reply_len = hostapd_ctrl_iface_acl_show_mac(
  2633. hapd->conf->deny_mac,
  2634. hapd->conf->num_deny_mac, reply, reply_size);
  2635. } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
  2636. hostapd_ctrl_iface_acl_clear_list(
  2637. &hapd->conf->deny_mac,
  2638. &hapd->conf->num_deny_mac);
  2639. }
  2640. #ifdef CONFIG_DPP
  2641. } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
  2642. res = hostapd_dpp_qr_code(hapd, buf + 12);
  2643. if (res < 0) {
  2644. reply_len = -1;
  2645. } else {
  2646. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2647. if (os_snprintf_error(reply_size, reply_len))
  2648. reply_len = -1;
  2649. }
  2650. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
  2651. res = hostapd_dpp_bootstrap_gen(hapd, buf + 18);
  2652. if (res < 0) {
  2653. reply_len = -1;
  2654. } else {
  2655. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2656. if (os_snprintf_error(reply_size, reply_len))
  2657. reply_len = -1;
  2658. }
  2659. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
  2660. if (hostapd_dpp_bootstrap_remove(hapd, buf + 21) < 0)
  2661. reply_len = -1;
  2662. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
  2663. const char *uri;
  2664. uri = hostapd_dpp_bootstrap_get_uri(hapd, atoi(buf + 22));
  2665. if (!uri) {
  2666. reply_len = -1;
  2667. } else {
  2668. reply_len = os_snprintf(reply, reply_size, "%s", uri);
  2669. if (os_snprintf_error(reply_size, reply_len))
  2670. reply_len = -1;
  2671. }
  2672. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
  2673. reply_len = hostapd_dpp_bootstrap_info(hapd, atoi(buf + 19),
  2674. reply, reply_size);
  2675. } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
  2676. if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
  2677. reply_len = -1;
  2678. } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
  2679. if (hostapd_dpp_listen(hapd, buf + 11) < 0)
  2680. reply_len = -1;
  2681. } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
  2682. hostapd_dpp_stop(hapd);
  2683. hostapd_dpp_listen_stop(hapd);
  2684. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
  2685. res = hostapd_dpp_configurator_add(hapd, buf + 20);
  2686. if (res < 0) {
  2687. reply_len = -1;
  2688. } else {
  2689. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2690. if (os_snprintf_error(reply_size, reply_len))
  2691. reply_len = -1;
  2692. }
  2693. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
  2694. if (hostapd_dpp_configurator_remove(hapd, buf + 24) < 0)
  2695. reply_len = -1;
  2696. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
  2697. if (hostapd_dpp_configurator_sign(hapd, buf + 22) < 0)
  2698. reply_len = -1;
  2699. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
  2700. reply_len = hostapd_dpp_configurator_get_key(hapd,
  2701. atoi(buf + 25),
  2702. reply, reply_size);
  2703. } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
  2704. res = hostapd_dpp_pkex_add(hapd, buf + 12);
  2705. if (res < 0) {
  2706. reply_len = -1;
  2707. } else {
  2708. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2709. if (os_snprintf_error(reply_size, reply_len))
  2710. reply_len = -1;
  2711. }
  2712. } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
  2713. if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
  2714. reply_len = -1;
  2715. #endif /* CONFIG_DPP */
  2716. } else {
  2717. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2718. reply_len = 16;
  2719. }
  2720. if (reply_len < 0) {
  2721. os_memcpy(reply, "FAIL\n", 5);
  2722. reply_len = 5;
  2723. }
  2724. return reply_len;
  2725. }
  2726. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  2727. void *sock_ctx)
  2728. {
  2729. struct hostapd_data *hapd = eloop_ctx;
  2730. char buf[4096];
  2731. int res;
  2732. struct sockaddr_storage from;
  2733. socklen_t fromlen = sizeof(from);
  2734. char *reply, *pos = buf;
  2735. const int reply_size = 4096;
  2736. int reply_len;
  2737. int level = MSG_DEBUG;
  2738. #ifdef CONFIG_CTRL_IFACE_UDP
  2739. unsigned char lcookie[COOKIE_LEN];
  2740. #endif /* CONFIG_CTRL_IFACE_UDP */
  2741. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  2742. (struct sockaddr *) &from, &fromlen);
  2743. if (res < 0) {
  2744. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  2745. strerror(errno));
  2746. return;
  2747. }
  2748. buf[res] = '\0';
  2749. reply = os_malloc(reply_size);
  2750. if (reply == NULL) {
  2751. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  2752. fromlen) < 0) {
  2753. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2754. strerror(errno));
  2755. }
  2756. return;
  2757. }
  2758. #ifdef CONFIG_CTRL_IFACE_UDP
  2759. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  2760. os_memcpy(reply, "COOKIE=", 7);
  2761. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  2762. cookie, COOKIE_LEN);
  2763. reply_len = 7 + 2 * COOKIE_LEN;
  2764. goto done;
  2765. }
  2766. if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
  2767. hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
  2768. wpa_printf(MSG_DEBUG,
  2769. "CTRL: No cookie in the request - drop request");
  2770. os_free(reply);
  2771. return;
  2772. }
  2773. if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
  2774. wpa_printf(MSG_DEBUG,
  2775. "CTRL: Invalid cookie in the request - drop request");
  2776. os_free(reply);
  2777. return;
  2778. }
  2779. pos = buf + 7 + 2 * COOKIE_LEN;
  2780. while (*pos == ' ')
  2781. pos++;
  2782. #endif /* CONFIG_CTRL_IFACE_UDP */
  2783. if (os_strcmp(pos, "PING") == 0)
  2784. level = MSG_EXCESSIVE;
  2785. wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
  2786. reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
  2787. reply, reply_size,
  2788. &from, fromlen);
  2789. #ifdef CONFIG_CTRL_IFACE_UDP
  2790. done:
  2791. #endif /* CONFIG_CTRL_IFACE_UDP */
  2792. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  2793. fromlen) < 0) {
  2794. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2795. strerror(errno));
  2796. }
  2797. os_free(reply);
  2798. }
  2799. #ifndef CONFIG_CTRL_IFACE_UDP
  2800. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  2801. {
  2802. char *buf;
  2803. size_t len;
  2804. if (hapd->conf->ctrl_interface == NULL)
  2805. return NULL;
  2806. len = os_strlen(hapd->conf->ctrl_interface) +
  2807. os_strlen(hapd->conf->iface) + 2;
  2808. buf = os_malloc(len);
  2809. if (buf == NULL)
  2810. return NULL;
  2811. os_snprintf(buf, len, "%s/%s",
  2812. hapd->conf->ctrl_interface, hapd->conf->iface);
  2813. buf[len - 1] = '\0';
  2814. return buf;
  2815. }
  2816. #endif /* CONFIG_CTRL_IFACE_UDP */
  2817. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  2818. enum wpa_msg_type type,
  2819. const char *txt, size_t len)
  2820. {
  2821. struct hostapd_data *hapd = ctx;
  2822. if (hapd == NULL)
  2823. return;
  2824. hostapd_ctrl_iface_send(hapd, level, type, txt, len);
  2825. }
  2826. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  2827. {
  2828. #ifdef CONFIG_CTRL_IFACE_UDP
  2829. int port = HOSTAPD_CTRL_IFACE_PORT;
  2830. char p[32] = { 0 };
  2831. char port_str[40], *tmp;
  2832. char *pos;
  2833. struct addrinfo hints = { 0 }, *res, *saveres;
  2834. int n;
  2835. if (hapd->ctrl_sock > -1) {
  2836. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2837. return 0;
  2838. }
  2839. if (hapd->conf->ctrl_interface == NULL)
  2840. return 0;
  2841. pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
  2842. if (pos) {
  2843. pos += 4;
  2844. port = atoi(pos);
  2845. if (port <= 0) {
  2846. wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
  2847. goto fail;
  2848. }
  2849. }
  2850. dl_list_init(&hapd->ctrl_dst);
  2851. hapd->ctrl_sock = -1;
  2852. os_get_random(cookie, COOKIE_LEN);
  2853. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  2854. hints.ai_flags = AI_PASSIVE;
  2855. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  2856. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  2857. hints.ai_family = AF_INET6;
  2858. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2859. hints.ai_family = AF_INET;
  2860. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2861. hints.ai_socktype = SOCK_DGRAM;
  2862. try_again:
  2863. os_snprintf(p, sizeof(p), "%d", port);
  2864. n = getaddrinfo(NULL, p, &hints, &res);
  2865. if (n) {
  2866. wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
  2867. goto fail;
  2868. }
  2869. saveres = res;
  2870. hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
  2871. res->ai_protocol);
  2872. if (hapd->ctrl_sock < 0) {
  2873. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  2874. goto fail;
  2875. }
  2876. if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
  2877. port--;
  2878. if ((HOSTAPD_CTRL_IFACE_PORT - port) <
  2879. HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
  2880. goto try_again;
  2881. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  2882. goto fail;
  2883. }
  2884. freeaddrinfo(saveres);
  2885. os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
  2886. tmp = os_strdup(port_str);
  2887. if (tmp) {
  2888. os_free(hapd->conf->ctrl_interface);
  2889. hapd->conf->ctrl_interface = tmp;
  2890. }
  2891. wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
  2892. if (eloop_register_read_sock(hapd->ctrl_sock,
  2893. hostapd_ctrl_iface_receive, hapd, NULL) <
  2894. 0) {
  2895. hostapd_ctrl_iface_deinit(hapd);
  2896. return -1;
  2897. }
  2898. hapd->msg_ctx = hapd;
  2899. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  2900. return 0;
  2901. fail:
  2902. if (hapd->ctrl_sock >= 0)
  2903. close(hapd->ctrl_sock);
  2904. return -1;
  2905. #else /* CONFIG_CTRL_IFACE_UDP */
  2906. struct sockaddr_un addr;
  2907. int s = -1;
  2908. char *fname = NULL;
  2909. if (hapd->ctrl_sock > -1) {
  2910. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2911. return 0;
  2912. }
  2913. dl_list_init(&hapd->ctrl_dst);
  2914. if (hapd->conf->ctrl_interface == NULL)
  2915. return 0;
  2916. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2917. if (errno == EEXIST) {
  2918. wpa_printf(MSG_DEBUG, "Using existing control "
  2919. "interface directory.");
  2920. } else {
  2921. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2922. strerror(errno));
  2923. goto fail;
  2924. }
  2925. }
  2926. if (hapd->conf->ctrl_interface_gid_set &&
  2927. chown(hapd->conf->ctrl_interface, -1,
  2928. hapd->conf->ctrl_interface_gid) < 0) {
  2929. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2930. strerror(errno));
  2931. return -1;
  2932. }
  2933. if (!hapd->conf->ctrl_interface_gid_set &&
  2934. hapd->iface->interfaces->ctrl_iface_group &&
  2935. chown(hapd->conf->ctrl_interface, -1,
  2936. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2937. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2938. strerror(errno));
  2939. return -1;
  2940. }
  2941. #ifdef ANDROID
  2942. /*
  2943. * Android is using umask 0077 which would leave the control interface
  2944. * directory without group access. This breaks things since Wi-Fi
  2945. * framework assumes that this directory can be accessed by other
  2946. * applications in the wifi group. Fix this by adding group access even
  2947. * if umask value would prevent this.
  2948. */
  2949. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2950. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  2951. strerror(errno));
  2952. /* Try to continue anyway */
  2953. }
  2954. #endif /* ANDROID */
  2955. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  2956. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  2957. goto fail;
  2958. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2959. if (s < 0) {
  2960. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2961. goto fail;
  2962. }
  2963. os_memset(&addr, 0, sizeof(addr));
  2964. #ifdef __FreeBSD__
  2965. addr.sun_len = sizeof(addr);
  2966. #endif /* __FreeBSD__ */
  2967. addr.sun_family = AF_UNIX;
  2968. fname = hostapd_ctrl_iface_path(hapd);
  2969. if (fname == NULL)
  2970. goto fail;
  2971. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2972. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2973. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2974. strerror(errno));
  2975. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2976. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2977. " allow connections - assuming it was left"
  2978. "over from forced program termination");
  2979. if (unlink(fname) < 0) {
  2980. wpa_printf(MSG_ERROR,
  2981. "Could not unlink existing ctrl_iface socket '%s': %s",
  2982. fname, strerror(errno));
  2983. goto fail;
  2984. }
  2985. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2986. 0) {
  2987. wpa_printf(MSG_ERROR,
  2988. "hostapd-ctrl-iface: bind(PF_UNIX): %s",
  2989. strerror(errno));
  2990. goto fail;
  2991. }
  2992. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2993. "ctrl_iface socket '%s'", fname);
  2994. } else {
  2995. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2996. "be in use - cannot override it");
  2997. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2998. "not used anymore", fname);
  2999. os_free(fname);
  3000. fname = NULL;
  3001. goto fail;
  3002. }
  3003. }
  3004. if (hapd->conf->ctrl_interface_gid_set &&
  3005. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  3006. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  3007. strerror(errno));
  3008. goto fail;
  3009. }
  3010. if (!hapd->conf->ctrl_interface_gid_set &&
  3011. hapd->iface->interfaces->ctrl_iface_group &&
  3012. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  3013. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  3014. strerror(errno));
  3015. goto fail;
  3016. }
  3017. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  3018. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  3019. strerror(errno));
  3020. goto fail;
  3021. }
  3022. os_free(fname);
  3023. hapd->ctrl_sock = s;
  3024. if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  3025. NULL) < 0) {
  3026. hostapd_ctrl_iface_deinit(hapd);
  3027. return -1;
  3028. }
  3029. hapd->msg_ctx = hapd;
  3030. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  3031. return 0;
  3032. fail:
  3033. if (s >= 0)
  3034. close(s);
  3035. if (fname) {
  3036. unlink(fname);
  3037. os_free(fname);
  3038. }
  3039. return -1;
  3040. #endif /* CONFIG_CTRL_IFACE_UDP */
  3041. }
  3042. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  3043. {
  3044. struct wpa_ctrl_dst *dst, *prev;
  3045. if (hapd->ctrl_sock > -1) {
  3046. #ifndef CONFIG_CTRL_IFACE_UDP
  3047. char *fname;
  3048. #endif /* !CONFIG_CTRL_IFACE_UDP */
  3049. eloop_unregister_read_sock(hapd->ctrl_sock);
  3050. close(hapd->ctrl_sock);
  3051. hapd->ctrl_sock = -1;
  3052. #ifndef CONFIG_CTRL_IFACE_UDP
  3053. fname = hostapd_ctrl_iface_path(hapd);
  3054. if (fname)
  3055. unlink(fname);
  3056. os_free(fname);
  3057. if (hapd->conf->ctrl_interface &&
  3058. rmdir(hapd->conf->ctrl_interface) < 0) {
  3059. if (errno == ENOTEMPTY) {
  3060. wpa_printf(MSG_DEBUG, "Control interface "
  3061. "directory not empty - leaving it "
  3062. "behind");
  3063. } else {
  3064. wpa_printf(MSG_ERROR,
  3065. "rmdir[ctrl_interface=%s]: %s",
  3066. hapd->conf->ctrl_interface,
  3067. strerror(errno));
  3068. }
  3069. }
  3070. #endif /* !CONFIG_CTRL_IFACE_UDP */
  3071. }
  3072. dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
  3073. list)
  3074. os_free(dst);
  3075. #ifdef CONFIG_TESTING_OPTIONS
  3076. l2_packet_deinit(hapd->l2_test);
  3077. hapd->l2_test = NULL;
  3078. #endif /* CONFIG_TESTING_OPTIONS */
  3079. }
  3080. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  3081. char *buf)
  3082. {
  3083. if (hostapd_add_iface(interfaces, buf) < 0) {
  3084. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  3085. return -1;
  3086. }
  3087. return 0;
  3088. }
  3089. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  3090. char *buf)
  3091. {
  3092. if (hostapd_remove_iface(interfaces, buf) < 0) {
  3093. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  3094. return -1;
  3095. }
  3096. return 0;
  3097. }
  3098. static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
  3099. struct sockaddr_storage *from,
  3100. socklen_t fromlen, char *input)
  3101. {
  3102. return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
  3103. input);
  3104. }
  3105. static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
  3106. struct sockaddr_storage *from,
  3107. socklen_t fromlen)
  3108. {
  3109. return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
  3110. }
  3111. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  3112. {
  3113. #ifdef CONFIG_WPS_TESTING
  3114. wps_version_number = 0x20;
  3115. wps_testing_dummy_cred = 0;
  3116. wps_corrupt_pkhash = 0;
  3117. #endif /* CONFIG_WPS_TESTING */
  3118. #ifdef CONFIG_TESTING_OPTIONS
  3119. #ifdef CONFIG_DPP
  3120. dpp_test = DPP_TEST_DISABLED;
  3121. #endif /* CONFIG_DPP */
  3122. #endif /* CONFIG_TESTING_OPTIONS */
  3123. #ifdef CONFIG_DPP
  3124. hostapd_dpp_deinit_global(interfaces);
  3125. #endif /* CONFIG_DPP */
  3126. }
  3127. #ifdef CONFIG_FST
  3128. static int
  3129. hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
  3130. const char *cmd)
  3131. {
  3132. char ifname[IFNAMSIZ + 1];
  3133. struct fst_iface_cfg cfg;
  3134. struct hostapd_data *hapd;
  3135. struct fst_wpa_obj iface_obj;
  3136. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  3137. hapd = hostapd_get_iface(interfaces, ifname);
  3138. if (hapd) {
  3139. if (hapd->iface->fst) {
  3140. wpa_printf(MSG_INFO, "FST: Already attached");
  3141. return -1;
  3142. }
  3143. fst_hostapd_fill_iface_obj(hapd, &iface_obj);
  3144. hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
  3145. &iface_obj, &cfg);
  3146. if (hapd->iface->fst)
  3147. return 0;
  3148. }
  3149. }
  3150. return -EINVAL;
  3151. }
  3152. static int
  3153. hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
  3154. const char *cmd)
  3155. {
  3156. char ifname[IFNAMSIZ + 1];
  3157. struct hostapd_data * hapd;
  3158. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  3159. hapd = hostapd_get_iface(interfaces, ifname);
  3160. if (hapd) {
  3161. if (!fst_iface_detach(ifname)) {
  3162. hapd->iface->fst = NULL;
  3163. hapd->iface->fst_ies = NULL;
  3164. return 0;
  3165. }
  3166. }
  3167. }
  3168. return -EINVAL;
  3169. }
  3170. #endif /* CONFIG_FST */
  3171. static struct hostapd_data *
  3172. hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
  3173. const char *ifname)
  3174. {
  3175. size_t i, j;
  3176. for (i = 0; i < interfaces->count; i++) {
  3177. struct hostapd_iface *iface = interfaces->iface[i];
  3178. for (j = 0; j < iface->num_bss; j++) {
  3179. struct hostapd_data *hapd;
  3180. hapd = iface->bss[j];
  3181. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  3182. return hapd;
  3183. }
  3184. }
  3185. return NULL;
  3186. }
  3187. static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
  3188. struct hostapd_data *dst_hapd,
  3189. const char *param)
  3190. {
  3191. int res;
  3192. char *value;
  3193. value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  3194. if (!value) {
  3195. wpa_printf(MSG_ERROR,
  3196. "DUP: cannot allocate buffer to stringify %s",
  3197. param);
  3198. goto error_return;
  3199. }
  3200. if (os_strcmp(param, "wpa") == 0) {
  3201. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
  3202. src_hapd->conf->wpa);
  3203. } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
  3204. src_hapd->conf->wpa_key_mgmt) {
  3205. res = hostapd_ctrl_iface_get_key_mgmt(
  3206. src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  3207. if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
  3208. goto error_stringify;
  3209. } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
  3210. src_hapd->conf->wpa_pairwise) {
  3211. res = wpa_write_ciphers(value,
  3212. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  3213. src_hapd->conf->wpa_pairwise, " ");
  3214. if (res < 0)
  3215. goto error_stringify;
  3216. } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
  3217. src_hapd->conf->rsn_pairwise) {
  3218. res = wpa_write_ciphers(value,
  3219. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  3220. src_hapd->conf->rsn_pairwise, " ");
  3221. if (res < 0)
  3222. goto error_stringify;
  3223. } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
  3224. src_hapd->conf->ssid.wpa_passphrase) {
  3225. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
  3226. src_hapd->conf->ssid.wpa_passphrase);
  3227. } else if (os_strcmp(param, "wpa_psk") == 0 &&
  3228. src_hapd->conf->ssid.wpa_psk_set) {
  3229. wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  3230. src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  3231. } else {
  3232. wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
  3233. goto error_return;
  3234. }
  3235. res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
  3236. os_free(value);
  3237. return res;
  3238. error_stringify:
  3239. wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
  3240. error_return:
  3241. os_free(value);
  3242. return -1;
  3243. }
  3244. static int
  3245. hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
  3246. const char *input,
  3247. char *reply, int reply_size)
  3248. {
  3249. size_t i, j;
  3250. int res;
  3251. char *pos, *end;
  3252. struct hostapd_iface *iface;
  3253. int show_ctrl = 0;
  3254. if (input)
  3255. show_ctrl = !!os_strstr(input, "ctrl");
  3256. pos = reply;
  3257. end = reply + reply_size;
  3258. for (i = 0; i < interfaces->count; i++) {
  3259. iface = interfaces->iface[i];
  3260. for (j = 0; j < iface->num_bss; j++) {
  3261. struct hostapd_bss_config *conf;
  3262. conf = iface->conf->bss[j];
  3263. if (show_ctrl)
  3264. res = os_snprintf(pos, end - pos,
  3265. "%s ctrl_iface=%s\n",
  3266. conf->iface,
  3267. conf->ctrl_interface ?
  3268. conf->ctrl_interface : "N/A");
  3269. else
  3270. res = os_snprintf(pos, end - pos, "%s\n",
  3271. conf->iface);
  3272. if (os_snprintf_error(end - pos, res)) {
  3273. *pos = '\0';
  3274. return pos - reply;
  3275. }
  3276. pos += res;
  3277. }
  3278. }
  3279. return pos - reply;
  3280. }
  3281. static int
  3282. hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
  3283. char *cmd)
  3284. {
  3285. char *p_start = cmd, *p_end;
  3286. struct hostapd_data *src_hapd, *dst_hapd;
  3287. /* cmd: "<src ifname> <dst ifname> <variable name> */
  3288. p_end = os_strchr(p_start, ' ');
  3289. if (!p_end) {
  3290. wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
  3291. cmd);
  3292. return -1;
  3293. }
  3294. *p_end = '\0';
  3295. src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  3296. if (!src_hapd) {
  3297. wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
  3298. p_start);
  3299. return -1;
  3300. }
  3301. p_start = p_end + 1;
  3302. p_end = os_strchr(p_start, ' ');
  3303. if (!p_end) {
  3304. wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
  3305. cmd);
  3306. return -1;
  3307. }
  3308. *p_end = '\0';
  3309. dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  3310. if (!dst_hapd) {
  3311. wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
  3312. p_start);
  3313. return -1;
  3314. }
  3315. p_start = p_end + 1;
  3316. return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
  3317. }
  3318. static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
  3319. const char *ifname,
  3320. char *buf, char *reply,
  3321. int reply_size,
  3322. struct sockaddr_storage *from,
  3323. socklen_t fromlen)
  3324. {
  3325. struct hostapd_data *hapd;
  3326. hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
  3327. if (hapd == NULL) {
  3328. int res;
  3329. res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
  3330. if (os_snprintf_error(reply_size, res))
  3331. return -1;
  3332. return res;
  3333. }
  3334. return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
  3335. from, fromlen);
  3336. }
  3337. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  3338. void *sock_ctx)
  3339. {
  3340. void *interfaces = eloop_ctx;
  3341. char buffer[256], *buf = buffer;
  3342. int res;
  3343. struct sockaddr_storage from;
  3344. socklen_t fromlen = sizeof(from);
  3345. char *reply;
  3346. int reply_len;
  3347. const int reply_size = 4096;
  3348. #ifdef CONFIG_CTRL_IFACE_UDP
  3349. unsigned char lcookie[COOKIE_LEN];
  3350. #endif /* CONFIG_CTRL_IFACE_UDP */
  3351. res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
  3352. (struct sockaddr *) &from, &fromlen);
  3353. if (res < 0) {
  3354. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  3355. strerror(errno));
  3356. return;
  3357. }
  3358. buf[res] = '\0';
  3359. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  3360. reply = os_malloc(reply_size);
  3361. if (reply == NULL) {
  3362. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  3363. fromlen) < 0) {
  3364. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  3365. strerror(errno));
  3366. }
  3367. return;
  3368. }
  3369. os_memcpy(reply, "OK\n", 3);
  3370. reply_len = 3;
  3371. #ifdef CONFIG_CTRL_IFACE_UDP
  3372. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  3373. os_memcpy(reply, "COOKIE=", 7);
  3374. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  3375. gcookie, COOKIE_LEN);
  3376. reply_len = 7 + 2 * COOKIE_LEN;
  3377. goto send_reply;
  3378. }
  3379. if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
  3380. hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
  3381. wpa_printf(MSG_DEBUG,
  3382. "CTRL: No cookie in the request - drop request");
  3383. os_free(reply);
  3384. return;
  3385. }
  3386. if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
  3387. wpa_printf(MSG_DEBUG,
  3388. "CTRL: Invalid cookie in the request - drop request");
  3389. os_free(reply);
  3390. return;
  3391. }
  3392. buf += 7 + 2 * COOKIE_LEN;
  3393. while (*buf == ' ')
  3394. buf++;
  3395. #endif /* CONFIG_CTRL_IFACE_UDP */
  3396. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  3397. char *pos = os_strchr(buf + 7, ' ');
  3398. if (pos) {
  3399. *pos++ = '\0';
  3400. reply_len = hostapd_global_ctrl_iface_ifname(
  3401. interfaces, buf + 7, pos, reply, reply_size,
  3402. &from, fromlen);
  3403. goto send_reply;
  3404. }
  3405. }
  3406. if (os_strcmp(buf, "PING") == 0) {
  3407. os_memcpy(reply, "PONG\n", 5);
  3408. reply_len = 5;
  3409. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  3410. if (wpa_debug_reopen_file() < 0)
  3411. reply_len = -1;
  3412. } else if (os_strcmp(buf, "FLUSH") == 0) {
  3413. hostapd_ctrl_iface_flush(interfaces);
  3414. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  3415. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  3416. reply_len = -1;
  3417. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  3418. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  3419. reply_len = -1;
  3420. } else if (os_strcmp(buf, "ATTACH") == 0) {
  3421. if (hostapd_global_ctrl_iface_attach(interfaces, &from,
  3422. fromlen, NULL))
  3423. reply_len = -1;
  3424. } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
  3425. if (hostapd_global_ctrl_iface_attach(interfaces, &from,
  3426. fromlen, buf + 7))
  3427. reply_len = -1;
  3428. } else if (os_strcmp(buf, "DETACH") == 0) {
  3429. if (hostapd_global_ctrl_iface_detach(interfaces, &from,
  3430. fromlen))
  3431. reply_len = -1;
  3432. #ifdef CONFIG_MODULE_TESTS
  3433. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  3434. if (hapd_module_tests() < 0)
  3435. reply_len = -1;
  3436. #endif /* CONFIG_MODULE_TESTS */
  3437. #ifdef CONFIG_FST
  3438. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  3439. if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
  3440. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3441. else
  3442. reply_len = -1;
  3443. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  3444. if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
  3445. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3446. else
  3447. reply_len = -1;
  3448. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  3449. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  3450. #endif /* CONFIG_FST */
  3451. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  3452. if (!hostapd_global_ctrl_iface_dup_network(interfaces,
  3453. buf + 12))
  3454. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3455. else
  3456. reply_len = -1;
  3457. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  3458. reply_len = hostapd_global_ctrl_iface_interfaces(
  3459. interfaces, buf + 10, reply, sizeof(buffer));
  3460. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  3461. eloop_terminate();
  3462. } else {
  3463. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  3464. "ignored");
  3465. reply_len = -1;
  3466. }
  3467. send_reply:
  3468. if (reply_len < 0) {
  3469. os_memcpy(reply, "FAIL\n", 5);
  3470. reply_len = 5;
  3471. }
  3472. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  3473. fromlen) < 0) {
  3474. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  3475. strerror(errno));
  3476. }
  3477. os_free(reply);
  3478. }
  3479. #ifndef CONFIG_CTRL_IFACE_UDP
  3480. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  3481. {
  3482. char *buf;
  3483. size_t len;
  3484. if (interface->global_iface_path == NULL)
  3485. return NULL;
  3486. len = os_strlen(interface->global_iface_path) +
  3487. os_strlen(interface->global_iface_name) + 2;
  3488. buf = os_malloc(len);
  3489. if (buf == NULL)
  3490. return NULL;
  3491. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  3492. interface->global_iface_name);
  3493. buf[len - 1] = '\0';
  3494. return buf;
  3495. }
  3496. #endif /* CONFIG_CTRL_IFACE_UDP */
  3497. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  3498. {
  3499. #ifdef CONFIG_CTRL_IFACE_UDP
  3500. int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
  3501. char p[32] = { 0 };
  3502. char *pos;
  3503. struct addrinfo hints = { 0 }, *res, *saveres;
  3504. int n;
  3505. if (interface->global_ctrl_sock > -1) {
  3506. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  3507. return 0;
  3508. }
  3509. if (interface->global_iface_path == NULL)
  3510. return 0;
  3511. pos = os_strstr(interface->global_iface_path, "udp:");
  3512. if (pos) {
  3513. pos += 4;
  3514. port = atoi(pos);
  3515. if (port <= 0) {
  3516. wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
  3517. goto fail;
  3518. }
  3519. }
  3520. os_get_random(gcookie, COOKIE_LEN);
  3521. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  3522. hints.ai_flags = AI_PASSIVE;
  3523. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  3524. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  3525. hints.ai_family = AF_INET6;
  3526. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  3527. hints.ai_family = AF_INET;
  3528. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  3529. hints.ai_socktype = SOCK_DGRAM;
  3530. try_again:
  3531. os_snprintf(p, sizeof(p), "%d", port);
  3532. n = getaddrinfo(NULL, p, &hints, &res);
  3533. if (n) {
  3534. wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
  3535. goto fail;
  3536. }
  3537. saveres = res;
  3538. interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
  3539. res->ai_protocol);
  3540. if (interface->global_ctrl_sock < 0) {
  3541. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  3542. goto fail;
  3543. }
  3544. if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
  3545. 0) {
  3546. port++;
  3547. if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
  3548. HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
  3549. goto try_again;
  3550. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  3551. goto fail;
  3552. }
  3553. freeaddrinfo(saveres);
  3554. wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
  3555. if (eloop_register_read_sock(interface->global_ctrl_sock,
  3556. hostapd_global_ctrl_iface_receive,
  3557. interface, NULL) < 0) {
  3558. hostapd_global_ctrl_iface_deinit(interface);
  3559. return -1;
  3560. }
  3561. return 0;
  3562. fail:
  3563. if (interface->global_ctrl_sock >= 0)
  3564. close(interface->global_ctrl_sock);
  3565. return -1;
  3566. #else /* CONFIG_CTRL_IFACE_UDP */
  3567. struct sockaddr_un addr;
  3568. int s = -1;
  3569. char *fname = NULL;
  3570. if (interface->global_iface_path == NULL) {
  3571. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  3572. return 0;
  3573. }
  3574. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  3575. if (errno == EEXIST) {
  3576. wpa_printf(MSG_DEBUG, "Using existing control "
  3577. "interface directory.");
  3578. } else {
  3579. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  3580. strerror(errno));
  3581. goto fail;
  3582. }
  3583. } else if (interface->ctrl_iface_group &&
  3584. chown(interface->global_iface_path, -1,
  3585. interface->ctrl_iface_group) < 0) {
  3586. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  3587. strerror(errno));
  3588. goto fail;
  3589. }
  3590. if (os_strlen(interface->global_iface_path) + 1 +
  3591. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  3592. goto fail;
  3593. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  3594. if (s < 0) {
  3595. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  3596. goto fail;
  3597. }
  3598. os_memset(&addr, 0, sizeof(addr));
  3599. #ifdef __FreeBSD__
  3600. addr.sun_len = sizeof(addr);
  3601. #endif /* __FreeBSD__ */
  3602. addr.sun_family = AF_UNIX;
  3603. fname = hostapd_global_ctrl_iface_path(interface);
  3604. if (fname == NULL)
  3605. goto fail;
  3606. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  3607. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  3608. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  3609. strerror(errno));
  3610. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  3611. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  3612. " allow connections - assuming it was left"
  3613. "over from forced program termination");
  3614. if (unlink(fname) < 0) {
  3615. wpa_printf(MSG_ERROR,
  3616. "Could not unlink existing ctrl_iface socket '%s': %s",
  3617. fname, strerror(errno));
  3618. goto fail;
  3619. }
  3620. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  3621. 0) {
  3622. wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
  3623. strerror(errno));
  3624. goto fail;
  3625. }
  3626. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  3627. "ctrl_iface socket '%s'", fname);
  3628. } else {
  3629. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  3630. "be in use - cannot override it");
  3631. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  3632. "not used anymore", fname);
  3633. os_free(fname);
  3634. fname = NULL;
  3635. goto fail;
  3636. }
  3637. }
  3638. if (interface->ctrl_iface_group &&
  3639. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  3640. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  3641. strerror(errno));
  3642. goto fail;
  3643. }
  3644. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  3645. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  3646. strerror(errno));
  3647. goto fail;
  3648. }
  3649. os_free(fname);
  3650. interface->global_ctrl_sock = s;
  3651. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  3652. interface, NULL);
  3653. return 0;
  3654. fail:
  3655. if (s >= 0)
  3656. close(s);
  3657. if (fname) {
  3658. unlink(fname);
  3659. os_free(fname);
  3660. }
  3661. return -1;
  3662. #endif /* CONFIG_CTRL_IFACE_UDP */
  3663. }
  3664. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  3665. {
  3666. #ifndef CONFIG_CTRL_IFACE_UDP
  3667. char *fname = NULL;
  3668. #endif /* CONFIG_CTRL_IFACE_UDP */
  3669. struct wpa_ctrl_dst *dst, *prev;
  3670. if (interfaces->global_ctrl_sock > -1) {
  3671. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  3672. close(interfaces->global_ctrl_sock);
  3673. interfaces->global_ctrl_sock = -1;
  3674. #ifndef CONFIG_CTRL_IFACE_UDP
  3675. fname = hostapd_global_ctrl_iface_path(interfaces);
  3676. if (fname) {
  3677. unlink(fname);
  3678. os_free(fname);
  3679. }
  3680. if (interfaces->global_iface_path &&
  3681. rmdir(interfaces->global_iface_path) < 0) {
  3682. if (errno == ENOTEMPTY) {
  3683. wpa_printf(MSG_DEBUG, "Control interface "
  3684. "directory not empty - leaving it "
  3685. "behind");
  3686. } else {
  3687. wpa_printf(MSG_ERROR,
  3688. "rmdir[ctrl_interface=%s]: %s",
  3689. interfaces->global_iface_path,
  3690. strerror(errno));
  3691. }
  3692. }
  3693. #endif /* CONFIG_CTRL_IFACE_UDP */
  3694. }
  3695. os_free(interfaces->global_iface_path);
  3696. interfaces->global_iface_path = NULL;
  3697. dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
  3698. struct wpa_ctrl_dst, list)
  3699. os_free(dst);
  3700. }
  3701. static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
  3702. const char *buf)
  3703. {
  3704. /* Enable Probe Request events based on explicit request.
  3705. * Other events are enabled by default.
  3706. */
  3707. if (str_starts(buf, RX_PROBE_REQUEST))
  3708. return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
  3709. return 1;
  3710. }
  3711. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  3712. enum wpa_msg_type type,
  3713. const char *buf, size_t len)
  3714. {
  3715. struct wpa_ctrl_dst *dst, *next;
  3716. struct dl_list *ctrl_dst;
  3717. struct msghdr msg;
  3718. int idx;
  3719. struct iovec io[2];
  3720. char levelstr[10];
  3721. int s;
  3722. if (type != WPA_MSG_ONLY_GLOBAL) {
  3723. s = hapd->ctrl_sock;
  3724. ctrl_dst = &hapd->ctrl_dst;
  3725. } else {
  3726. s = hapd->iface->interfaces->global_ctrl_sock;
  3727. ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
  3728. }
  3729. if (s < 0 || dl_list_empty(ctrl_dst))
  3730. return;
  3731. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  3732. io[0].iov_base = levelstr;
  3733. io[0].iov_len = os_strlen(levelstr);
  3734. io[1].iov_base = (char *) buf;
  3735. io[1].iov_len = len;
  3736. os_memset(&msg, 0, sizeof(msg));
  3737. msg.msg_iov = io;
  3738. msg.msg_iovlen = 2;
  3739. idx = 0;
  3740. dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
  3741. if ((level >= dst->debug_level) &&
  3742. hostapd_ctrl_check_event_enabled(dst, buf)) {
  3743. sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
  3744. &dst->addr, dst->addrlen);
  3745. msg.msg_name = &dst->addr;
  3746. msg.msg_namelen = dst->addrlen;
  3747. if (sendmsg(s, &msg, 0) < 0) {
  3748. int _errno = errno;
  3749. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  3750. "%d - %s",
  3751. idx, errno, strerror(errno));
  3752. dst->errors++;
  3753. if (dst->errors > 10 || _errno == ENOENT) {
  3754. if (type != WPA_MSG_ONLY_GLOBAL)
  3755. hostapd_ctrl_iface_detach(
  3756. hapd, &dst->addr,
  3757. dst->addrlen);
  3758. else
  3759. hostapd_global_ctrl_iface_detach(
  3760. hapd->iface->interfaces,
  3761. &dst->addr,
  3762. dst->addrlen);
  3763. }
  3764. } else
  3765. dst->errors = 0;
  3766. }
  3767. idx++;
  3768. }
  3769. }
  3770. #endif /* CONFIG_NATIVE_WINDOWS */