ctrl_iface.c 88 KB

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