ctrl_iface.c 97 KB

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