ctrl_iface.c 87 KB

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