ctrl_iface.c 102 KB

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