ctrl_iface.c 83 KB

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