ctrl_iface.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519
  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_strcmp(buf, "STOP_AP") == 0) {
  1782. if (hostapd_ctrl_iface_stop_ap(hapd))
  1783. reply_len = -1;
  1784. #ifdef CONFIG_IEEE80211W
  1785. #ifdef NEED_AP_MLME
  1786. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  1787. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  1788. reply_len = -1;
  1789. #endif /* NEED_AP_MLME */
  1790. #endif /* CONFIG_IEEE80211W */
  1791. #ifdef CONFIG_WPS
  1792. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1793. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  1794. reply_len = -1;
  1795. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  1796. reply_len = hostapd_ctrl_iface_wps_check_pin(
  1797. hapd, buf + 14, reply, reply_size);
  1798. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1799. if (hostapd_wps_button_pushed(hapd, NULL))
  1800. reply_len = -1;
  1801. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  1802. if (hostapd_wps_cancel(hapd))
  1803. reply_len = -1;
  1804. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  1805. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  1806. reply, reply_size);
  1807. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  1808. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  1809. reply_len = -1;
  1810. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  1811. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  1812. reply_size);
  1813. #ifdef CONFIG_WPS_NFC
  1814. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  1815. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  1816. reply_len = -1;
  1817. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  1818. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  1819. hapd, buf + 21, reply, reply_size);
  1820. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  1821. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  1822. hapd, buf + 14, reply, reply_size);
  1823. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  1824. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  1825. hapd, buf + 21, reply, reply_size);
  1826. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1827. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1828. reply_len = -1;
  1829. #endif /* CONFIG_WPS_NFC */
  1830. #endif /* CONFIG_WPS */
  1831. #ifdef CONFIG_INTERWORKING
  1832. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1833. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1834. reply_len = -1;
  1835. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1836. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1837. reply_len = -1;
  1838. #endif /* CONFIG_INTERWORKING */
  1839. #ifdef CONFIG_HS20
  1840. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  1841. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  1842. reply_len = -1;
  1843. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  1844. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  1845. reply_len = -1;
  1846. #endif /* CONFIG_HS20 */
  1847. #ifdef CONFIG_WNM
  1848. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1849. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1850. reply_len = -1;
  1851. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1852. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1853. reply_len = -1;
  1854. } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
  1855. if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
  1856. reply_len = -1;
  1857. #endif /* CONFIG_WNM */
  1858. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1859. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1860. reply_size);
  1861. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1862. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1863. reply_len = -1;
  1864. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1865. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1866. reply_size);
  1867. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1868. if (hostapd_ctrl_iface_enable(hapd->iface))
  1869. reply_len = -1;
  1870. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1871. if (hostapd_ctrl_iface_reload(hapd->iface))
  1872. reply_len = -1;
  1873. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1874. if (hostapd_ctrl_iface_disable(hapd->iface))
  1875. reply_len = -1;
  1876. } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
  1877. if (ieee802_11_set_beacon(hapd))
  1878. reply_len = -1;
  1879. #ifdef CONFIG_TESTING_OPTIONS
  1880. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  1881. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  1882. reply_len = -1;
  1883. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  1884. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  1885. reply_len = -1;
  1886. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  1887. if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
  1888. reply_len = -1;
  1889. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  1890. if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
  1891. reply_len = -1;
  1892. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  1893. if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
  1894. reply_len = -1;
  1895. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  1896. if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
  1897. reply_len = -1;
  1898. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  1899. if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
  1900. reply_len = -1;
  1901. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  1902. reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
  1903. reply_size);
  1904. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  1905. if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
  1906. reply_len = -1;
  1907. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  1908. reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
  1909. #endif /* CONFIG_TESTING_OPTIONS */
  1910. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  1911. if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
  1912. reply_len = -1;
  1913. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  1914. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  1915. reply_size);
  1916. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  1917. ieee802_1x_erp_flush(hapd);
  1918. #ifdef RADIUS_SERVER
  1919. radius_server_erp_flush(hapd->radius_srv);
  1920. #endif /* RADIUS_SERVER */
  1921. } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
  1922. if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
  1923. reply_len = -1;
  1924. } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
  1925. if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
  1926. reply_len = -1;
  1927. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  1928. reply_len = hostapd_ctrl_iface_log_level(
  1929. hapd, buf + 9, reply, reply_size);
  1930. #ifdef NEED_AP_MLME
  1931. } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
  1932. reply_len = hostapd_ctrl_iface_track_sta_list(
  1933. hapd, reply, reply_size);
  1934. #endif /* NEED_AP_MLME */
  1935. } else if (os_strcmp(buf, "PMKSA") == 0) {
  1936. reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
  1937. reply_size);
  1938. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  1939. hostapd_ctrl_iface_pmksa_flush(hapd);
  1940. } else {
  1941. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1942. reply_len = 16;
  1943. }
  1944. if (reply_len < 0) {
  1945. os_memcpy(reply, "FAIL\n", 5);
  1946. reply_len = 5;
  1947. }
  1948. return reply_len;
  1949. }
  1950. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  1951. void *sock_ctx)
  1952. {
  1953. struct hostapd_data *hapd = eloop_ctx;
  1954. char buf[4096];
  1955. int res;
  1956. struct sockaddr_storage from;
  1957. socklen_t fromlen = sizeof(from);
  1958. char *reply, *pos = buf;
  1959. const int reply_size = 4096;
  1960. int reply_len;
  1961. int level = MSG_DEBUG;
  1962. #ifdef CONFIG_CTRL_IFACE_UDP
  1963. unsigned char lcookie[COOKIE_LEN];
  1964. #endif /* CONFIG_CTRL_IFACE_UDP */
  1965. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1966. (struct sockaddr *) &from, &fromlen);
  1967. if (res < 0) {
  1968. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  1969. strerror(errno));
  1970. return;
  1971. }
  1972. buf[res] = '\0';
  1973. reply = os_malloc(reply_size);
  1974. if (reply == NULL) {
  1975. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  1976. fromlen) < 0) {
  1977. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1978. strerror(errno));
  1979. }
  1980. return;
  1981. }
  1982. #ifdef CONFIG_CTRL_IFACE_UDP
  1983. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  1984. os_memcpy(reply, "COOKIE=", 7);
  1985. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  1986. cookie, COOKIE_LEN);
  1987. reply_len = 7 + 2 * COOKIE_LEN;
  1988. goto done;
  1989. }
  1990. if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
  1991. hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
  1992. wpa_printf(MSG_DEBUG,
  1993. "CTRL: No cookie in the request - drop request");
  1994. os_free(reply);
  1995. return;
  1996. }
  1997. if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
  1998. wpa_printf(MSG_DEBUG,
  1999. "CTRL: Invalid cookie in the request - drop request");
  2000. os_free(reply);
  2001. return;
  2002. }
  2003. pos = buf + 7 + 2 * COOKIE_LEN;
  2004. while (*pos == ' ')
  2005. pos++;
  2006. #endif /* CONFIG_CTRL_IFACE_UDP */
  2007. if (os_strcmp(pos, "PING") == 0)
  2008. level = MSG_EXCESSIVE;
  2009. wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
  2010. reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
  2011. reply, reply_size,
  2012. &from, fromlen);
  2013. #ifdef CONFIG_CTRL_IFACE_UDP
  2014. done:
  2015. #endif /* CONFIG_CTRL_IFACE_UDP */
  2016. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  2017. fromlen) < 0) {
  2018. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2019. strerror(errno));
  2020. }
  2021. os_free(reply);
  2022. }
  2023. #ifndef CONFIG_CTRL_IFACE_UDP
  2024. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  2025. {
  2026. char *buf;
  2027. size_t len;
  2028. if (hapd->conf->ctrl_interface == NULL)
  2029. return NULL;
  2030. len = os_strlen(hapd->conf->ctrl_interface) +
  2031. os_strlen(hapd->conf->iface) + 2;
  2032. buf = os_malloc(len);
  2033. if (buf == NULL)
  2034. return NULL;
  2035. os_snprintf(buf, len, "%s/%s",
  2036. hapd->conf->ctrl_interface, hapd->conf->iface);
  2037. buf[len - 1] = '\0';
  2038. return buf;
  2039. }
  2040. #endif /* CONFIG_CTRL_IFACE_UDP */
  2041. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  2042. enum wpa_msg_type type,
  2043. const char *txt, size_t len)
  2044. {
  2045. struct hostapd_data *hapd = ctx;
  2046. if (hapd == NULL)
  2047. return;
  2048. hostapd_ctrl_iface_send(hapd, level, type, txt, len);
  2049. }
  2050. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  2051. {
  2052. #ifdef CONFIG_CTRL_IFACE_UDP
  2053. int port = HOSTAPD_CTRL_IFACE_PORT;
  2054. char p[32] = { 0 };
  2055. char port_str[40], *tmp;
  2056. char *pos;
  2057. struct addrinfo hints = { 0 }, *res, *saveres;
  2058. int n;
  2059. if (hapd->ctrl_sock > -1) {
  2060. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2061. return 0;
  2062. }
  2063. if (hapd->conf->ctrl_interface == NULL)
  2064. return 0;
  2065. pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
  2066. if (pos) {
  2067. pos += 4;
  2068. port = atoi(pos);
  2069. if (port <= 0) {
  2070. wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
  2071. goto fail;
  2072. }
  2073. }
  2074. dl_list_init(&hapd->ctrl_dst);
  2075. hapd->ctrl_sock = -1;
  2076. os_get_random(cookie, COOKIE_LEN);
  2077. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  2078. hints.ai_flags = AI_PASSIVE;
  2079. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  2080. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  2081. hints.ai_family = AF_INET6;
  2082. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2083. hints.ai_family = AF_INET;
  2084. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2085. hints.ai_socktype = SOCK_DGRAM;
  2086. try_again:
  2087. os_snprintf(p, sizeof(p), "%d", port);
  2088. n = getaddrinfo(NULL, p, &hints, &res);
  2089. if (n) {
  2090. wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
  2091. goto fail;
  2092. }
  2093. saveres = res;
  2094. hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
  2095. res->ai_protocol);
  2096. if (hapd->ctrl_sock < 0) {
  2097. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  2098. goto fail;
  2099. }
  2100. if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
  2101. port--;
  2102. if ((HOSTAPD_CTRL_IFACE_PORT - port) <
  2103. HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
  2104. goto try_again;
  2105. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  2106. goto fail;
  2107. }
  2108. freeaddrinfo(saveres);
  2109. os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
  2110. tmp = os_strdup(port_str);
  2111. if (tmp) {
  2112. os_free(hapd->conf->ctrl_interface);
  2113. hapd->conf->ctrl_interface = tmp;
  2114. }
  2115. wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
  2116. if (eloop_register_read_sock(hapd->ctrl_sock,
  2117. hostapd_ctrl_iface_receive, hapd, NULL) <
  2118. 0) {
  2119. hostapd_ctrl_iface_deinit(hapd);
  2120. return -1;
  2121. }
  2122. hapd->msg_ctx = hapd;
  2123. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  2124. return 0;
  2125. fail:
  2126. if (hapd->ctrl_sock >= 0)
  2127. close(hapd->ctrl_sock);
  2128. return -1;
  2129. #else /* CONFIG_CTRL_IFACE_UDP */
  2130. struct sockaddr_un addr;
  2131. int s = -1;
  2132. char *fname = NULL;
  2133. if (hapd->ctrl_sock > -1) {
  2134. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2135. return 0;
  2136. }
  2137. dl_list_init(&hapd->ctrl_dst);
  2138. if (hapd->conf->ctrl_interface == NULL)
  2139. return 0;
  2140. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2141. if (errno == EEXIST) {
  2142. wpa_printf(MSG_DEBUG, "Using existing control "
  2143. "interface directory.");
  2144. } else {
  2145. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2146. strerror(errno));
  2147. goto fail;
  2148. }
  2149. }
  2150. if (hapd->conf->ctrl_interface_gid_set &&
  2151. chown(hapd->conf->ctrl_interface, -1,
  2152. hapd->conf->ctrl_interface_gid) < 0) {
  2153. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2154. strerror(errno));
  2155. return -1;
  2156. }
  2157. if (!hapd->conf->ctrl_interface_gid_set &&
  2158. hapd->iface->interfaces->ctrl_iface_group &&
  2159. chown(hapd->conf->ctrl_interface, -1,
  2160. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2161. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2162. strerror(errno));
  2163. return -1;
  2164. }
  2165. #ifdef ANDROID
  2166. /*
  2167. * Android is using umask 0077 which would leave the control interface
  2168. * directory without group access. This breaks things since Wi-Fi
  2169. * framework assumes that this directory can be accessed by other
  2170. * applications in the wifi group. Fix this by adding group access even
  2171. * if umask value would prevent this.
  2172. */
  2173. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2174. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  2175. strerror(errno));
  2176. /* Try to continue anyway */
  2177. }
  2178. #endif /* ANDROID */
  2179. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  2180. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  2181. goto fail;
  2182. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2183. if (s < 0) {
  2184. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2185. goto fail;
  2186. }
  2187. os_memset(&addr, 0, sizeof(addr));
  2188. #ifdef __FreeBSD__
  2189. addr.sun_len = sizeof(addr);
  2190. #endif /* __FreeBSD__ */
  2191. addr.sun_family = AF_UNIX;
  2192. fname = hostapd_ctrl_iface_path(hapd);
  2193. if (fname == NULL)
  2194. goto fail;
  2195. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2196. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2197. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2198. strerror(errno));
  2199. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2200. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2201. " allow connections - assuming it was left"
  2202. "over from forced program termination");
  2203. if (unlink(fname) < 0) {
  2204. wpa_printf(MSG_ERROR,
  2205. "Could not unlink existing ctrl_iface socket '%s': %s",
  2206. fname, strerror(errno));
  2207. goto fail;
  2208. }
  2209. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2210. 0) {
  2211. wpa_printf(MSG_ERROR,
  2212. "hostapd-ctrl-iface: bind(PF_UNIX): %s",
  2213. strerror(errno));
  2214. goto fail;
  2215. }
  2216. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2217. "ctrl_iface socket '%s'", fname);
  2218. } else {
  2219. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2220. "be in use - cannot override it");
  2221. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2222. "not used anymore", fname);
  2223. os_free(fname);
  2224. fname = NULL;
  2225. goto fail;
  2226. }
  2227. }
  2228. if (hapd->conf->ctrl_interface_gid_set &&
  2229. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  2230. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  2231. strerror(errno));
  2232. goto fail;
  2233. }
  2234. if (!hapd->conf->ctrl_interface_gid_set &&
  2235. hapd->iface->interfaces->ctrl_iface_group &&
  2236. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2237. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  2238. strerror(errno));
  2239. goto fail;
  2240. }
  2241. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  2242. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  2243. strerror(errno));
  2244. goto fail;
  2245. }
  2246. os_free(fname);
  2247. hapd->ctrl_sock = s;
  2248. if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  2249. NULL) < 0) {
  2250. hostapd_ctrl_iface_deinit(hapd);
  2251. return -1;
  2252. }
  2253. hapd->msg_ctx = hapd;
  2254. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  2255. return 0;
  2256. fail:
  2257. if (s >= 0)
  2258. close(s);
  2259. if (fname) {
  2260. unlink(fname);
  2261. os_free(fname);
  2262. }
  2263. return -1;
  2264. #endif /* CONFIG_CTRL_IFACE_UDP */
  2265. }
  2266. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  2267. {
  2268. struct wpa_ctrl_dst *dst, *prev;
  2269. if (hapd->ctrl_sock > -1) {
  2270. #ifndef CONFIG_CTRL_IFACE_UDP
  2271. char *fname;
  2272. #endif /* !CONFIG_CTRL_IFACE_UDP */
  2273. eloop_unregister_read_sock(hapd->ctrl_sock);
  2274. close(hapd->ctrl_sock);
  2275. hapd->ctrl_sock = -1;
  2276. #ifndef CONFIG_CTRL_IFACE_UDP
  2277. fname = hostapd_ctrl_iface_path(hapd);
  2278. if (fname)
  2279. unlink(fname);
  2280. os_free(fname);
  2281. if (hapd->conf->ctrl_interface &&
  2282. rmdir(hapd->conf->ctrl_interface) < 0) {
  2283. if (errno == ENOTEMPTY) {
  2284. wpa_printf(MSG_DEBUG, "Control interface "
  2285. "directory not empty - leaving it "
  2286. "behind");
  2287. } else {
  2288. wpa_printf(MSG_ERROR,
  2289. "rmdir[ctrl_interface=%s]: %s",
  2290. hapd->conf->ctrl_interface,
  2291. strerror(errno));
  2292. }
  2293. }
  2294. #endif /* !CONFIG_CTRL_IFACE_UDP */
  2295. }
  2296. dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
  2297. list)
  2298. os_free(dst);
  2299. #ifdef CONFIG_TESTING_OPTIONS
  2300. l2_packet_deinit(hapd->l2_test);
  2301. hapd->l2_test = NULL;
  2302. #endif /* CONFIG_TESTING_OPTIONS */
  2303. }
  2304. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  2305. char *buf)
  2306. {
  2307. if (hostapd_add_iface(interfaces, buf) < 0) {
  2308. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  2309. return -1;
  2310. }
  2311. return 0;
  2312. }
  2313. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  2314. char *buf)
  2315. {
  2316. if (hostapd_remove_iface(interfaces, buf) < 0) {
  2317. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  2318. return -1;
  2319. }
  2320. return 0;
  2321. }
  2322. static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
  2323. struct sockaddr_storage *from,
  2324. socklen_t fromlen)
  2325. {
  2326. return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen);
  2327. }
  2328. static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
  2329. struct sockaddr_storage *from,
  2330. socklen_t fromlen)
  2331. {
  2332. return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
  2333. }
  2334. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  2335. {
  2336. #ifdef CONFIG_WPS_TESTING
  2337. wps_version_number = 0x20;
  2338. wps_testing_dummy_cred = 0;
  2339. wps_corrupt_pkhash = 0;
  2340. #endif /* CONFIG_WPS_TESTING */
  2341. }
  2342. #ifdef CONFIG_FST
  2343. static int
  2344. hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
  2345. const char *cmd)
  2346. {
  2347. char ifname[IFNAMSIZ + 1];
  2348. struct fst_iface_cfg cfg;
  2349. struct hostapd_data *hapd;
  2350. struct fst_wpa_obj iface_obj;
  2351. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  2352. hapd = hostapd_get_iface(interfaces, ifname);
  2353. if (hapd) {
  2354. if (hapd->iface->fst) {
  2355. wpa_printf(MSG_INFO, "FST: Already attached");
  2356. return -1;
  2357. }
  2358. fst_hostapd_fill_iface_obj(hapd, &iface_obj);
  2359. hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
  2360. &iface_obj, &cfg);
  2361. if (hapd->iface->fst)
  2362. return 0;
  2363. }
  2364. }
  2365. return -EINVAL;
  2366. }
  2367. static int
  2368. hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
  2369. const char *cmd)
  2370. {
  2371. char ifname[IFNAMSIZ + 1];
  2372. struct hostapd_data * hapd;
  2373. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  2374. hapd = hostapd_get_iface(interfaces, ifname);
  2375. if (hapd) {
  2376. if (!fst_iface_detach(ifname)) {
  2377. hapd->iface->fst = NULL;
  2378. hapd->iface->fst_ies = NULL;
  2379. return 0;
  2380. }
  2381. }
  2382. }
  2383. return -EINVAL;
  2384. }
  2385. #endif /* CONFIG_FST */
  2386. static struct hostapd_data *
  2387. hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
  2388. const char *ifname)
  2389. {
  2390. size_t i, j;
  2391. for (i = 0; i < interfaces->count; i++) {
  2392. struct hostapd_iface *iface = interfaces->iface[i];
  2393. for (j = 0; j < iface->num_bss; j++) {
  2394. struct hostapd_data *hapd;
  2395. hapd = iface->bss[j];
  2396. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  2397. return hapd;
  2398. }
  2399. }
  2400. return NULL;
  2401. }
  2402. static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
  2403. struct hostapd_data *dst_hapd,
  2404. const char *param)
  2405. {
  2406. int res;
  2407. char *value;
  2408. value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  2409. if (!value) {
  2410. wpa_printf(MSG_ERROR,
  2411. "DUP: cannot allocate buffer to stringify %s",
  2412. param);
  2413. goto error_return;
  2414. }
  2415. if (os_strcmp(param, "wpa") == 0) {
  2416. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
  2417. src_hapd->conf->wpa);
  2418. } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
  2419. src_hapd->conf->wpa_key_mgmt) {
  2420. res = hostapd_ctrl_iface_get_key_mgmt(
  2421. src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  2422. if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
  2423. goto error_stringify;
  2424. } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
  2425. src_hapd->conf->wpa_pairwise) {
  2426. res = wpa_write_ciphers(value,
  2427. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2428. src_hapd->conf->wpa_pairwise, " ");
  2429. if (res < 0)
  2430. goto error_stringify;
  2431. } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
  2432. src_hapd->conf->rsn_pairwise) {
  2433. res = wpa_write_ciphers(value,
  2434. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2435. src_hapd->conf->rsn_pairwise, " ");
  2436. if (res < 0)
  2437. goto error_stringify;
  2438. } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
  2439. src_hapd->conf->ssid.wpa_passphrase) {
  2440. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
  2441. src_hapd->conf->ssid.wpa_passphrase);
  2442. } else if (os_strcmp(param, "wpa_psk") == 0 &&
  2443. src_hapd->conf->ssid.wpa_psk_set) {
  2444. wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2445. src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  2446. } else {
  2447. wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
  2448. goto error_return;
  2449. }
  2450. res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
  2451. os_free(value);
  2452. return res;
  2453. error_stringify:
  2454. wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
  2455. error_return:
  2456. os_free(value);
  2457. return -1;
  2458. }
  2459. static int
  2460. hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
  2461. const char *input,
  2462. char *reply, int reply_size)
  2463. {
  2464. size_t i, j;
  2465. int res;
  2466. char *pos, *end;
  2467. struct hostapd_iface *iface;
  2468. int show_ctrl = 0;
  2469. if (input)
  2470. show_ctrl = !!os_strstr(input, "ctrl");
  2471. pos = reply;
  2472. end = reply + reply_size;
  2473. for (i = 0; i < interfaces->count; i++) {
  2474. iface = interfaces->iface[i];
  2475. for (j = 0; j < iface->num_bss; j++) {
  2476. struct hostapd_bss_config *conf;
  2477. conf = iface->conf->bss[j];
  2478. if (show_ctrl)
  2479. res = os_snprintf(pos, end - pos,
  2480. "%s ctrl_iface=%s\n",
  2481. conf->iface,
  2482. conf->ctrl_interface ?
  2483. conf->ctrl_interface : "N/A");
  2484. else
  2485. res = os_snprintf(pos, end - pos, "%s\n",
  2486. conf->iface);
  2487. if (os_snprintf_error(end - pos, res)) {
  2488. *pos = '\0';
  2489. return pos - reply;
  2490. }
  2491. pos += res;
  2492. }
  2493. }
  2494. return pos - reply;
  2495. }
  2496. static int
  2497. hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
  2498. char *cmd)
  2499. {
  2500. char *p_start = cmd, *p_end;
  2501. struct hostapd_data *src_hapd, *dst_hapd;
  2502. /* cmd: "<src ifname> <dst ifname> <variable name> */
  2503. p_end = os_strchr(p_start, ' ');
  2504. if (!p_end) {
  2505. wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
  2506. cmd);
  2507. return -1;
  2508. }
  2509. *p_end = '\0';
  2510. src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  2511. if (!src_hapd) {
  2512. wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
  2513. p_start);
  2514. return -1;
  2515. }
  2516. p_start = p_end + 1;
  2517. p_end = os_strchr(p_start, ' ');
  2518. if (!p_end) {
  2519. wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
  2520. cmd);
  2521. return -1;
  2522. }
  2523. *p_end = '\0';
  2524. dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  2525. if (!dst_hapd) {
  2526. wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
  2527. p_start);
  2528. return -1;
  2529. }
  2530. p_start = p_end + 1;
  2531. return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
  2532. }
  2533. static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
  2534. const char *ifname,
  2535. char *buf, char *reply,
  2536. int reply_size,
  2537. struct sockaddr_storage *from,
  2538. socklen_t fromlen)
  2539. {
  2540. struct hostapd_data *hapd;
  2541. hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
  2542. if (hapd == NULL) {
  2543. int res;
  2544. res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
  2545. if (os_snprintf_error(reply_size, res))
  2546. return -1;
  2547. return res;
  2548. }
  2549. return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
  2550. from, fromlen);
  2551. }
  2552. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  2553. void *sock_ctx)
  2554. {
  2555. void *interfaces = eloop_ctx;
  2556. char buffer[256], *buf = buffer;
  2557. int res;
  2558. struct sockaddr_storage from;
  2559. socklen_t fromlen = sizeof(from);
  2560. char *reply;
  2561. int reply_len;
  2562. const int reply_size = 4096;
  2563. #ifdef CONFIG_CTRL_IFACE_UDP
  2564. unsigned char lcookie[COOKIE_LEN];
  2565. #endif /* CONFIG_CTRL_IFACE_UDP */
  2566. res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
  2567. (struct sockaddr *) &from, &fromlen);
  2568. if (res < 0) {
  2569. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  2570. strerror(errno));
  2571. return;
  2572. }
  2573. buf[res] = '\0';
  2574. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  2575. reply = os_malloc(reply_size);
  2576. if (reply == NULL) {
  2577. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  2578. fromlen) < 0) {
  2579. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2580. strerror(errno));
  2581. }
  2582. return;
  2583. }
  2584. os_memcpy(reply, "OK\n", 3);
  2585. reply_len = 3;
  2586. #ifdef CONFIG_CTRL_IFACE_UDP
  2587. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  2588. os_memcpy(reply, "COOKIE=", 7);
  2589. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  2590. gcookie, COOKIE_LEN);
  2591. reply_len = 7 + 2 * COOKIE_LEN;
  2592. goto send_reply;
  2593. }
  2594. if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
  2595. hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
  2596. wpa_printf(MSG_DEBUG,
  2597. "CTRL: No cookie in the request - drop request");
  2598. os_free(reply);
  2599. return;
  2600. }
  2601. if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
  2602. wpa_printf(MSG_DEBUG,
  2603. "CTRL: Invalid cookie in the request - drop request");
  2604. os_free(reply);
  2605. return;
  2606. }
  2607. buf += 7 + 2 * COOKIE_LEN;
  2608. while (*buf == ' ')
  2609. buf++;
  2610. #endif /* CONFIG_CTRL_IFACE_UDP */
  2611. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  2612. char *pos = os_strchr(buf + 7, ' ');
  2613. if (pos) {
  2614. *pos++ = '\0';
  2615. reply_len = hostapd_global_ctrl_iface_ifname(
  2616. interfaces, buf + 7, pos, reply, reply_size,
  2617. &from, fromlen);
  2618. goto send_reply;
  2619. }
  2620. }
  2621. if (os_strcmp(buf, "PING") == 0) {
  2622. os_memcpy(reply, "PONG\n", 5);
  2623. reply_len = 5;
  2624. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  2625. if (wpa_debug_reopen_file() < 0)
  2626. reply_len = -1;
  2627. } else if (os_strcmp(buf, "FLUSH") == 0) {
  2628. hostapd_ctrl_iface_flush(interfaces);
  2629. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  2630. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  2631. reply_len = -1;
  2632. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  2633. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  2634. reply_len = -1;
  2635. } else if (os_strcmp(buf, "ATTACH") == 0) {
  2636. if (hostapd_global_ctrl_iface_attach(interfaces, &from,
  2637. fromlen))
  2638. reply_len = -1;
  2639. } else if (os_strcmp(buf, "DETACH") == 0) {
  2640. if (hostapd_global_ctrl_iface_detach(interfaces, &from,
  2641. fromlen))
  2642. reply_len = -1;
  2643. #ifdef CONFIG_MODULE_TESTS
  2644. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  2645. int hapd_module_tests(void);
  2646. if (hapd_module_tests() < 0)
  2647. reply_len = -1;
  2648. #endif /* CONFIG_MODULE_TESTS */
  2649. #ifdef CONFIG_FST
  2650. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  2651. if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
  2652. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2653. else
  2654. reply_len = -1;
  2655. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  2656. if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
  2657. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2658. else
  2659. reply_len = -1;
  2660. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  2661. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  2662. #endif /* CONFIG_FST */
  2663. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  2664. if (!hostapd_global_ctrl_iface_dup_network(interfaces,
  2665. buf + 12))
  2666. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2667. else
  2668. reply_len = -1;
  2669. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  2670. reply_len = hostapd_global_ctrl_iface_interfaces(
  2671. interfaces, buf + 10, reply, sizeof(buffer));
  2672. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2673. eloop_terminate();
  2674. } else {
  2675. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  2676. "ignored");
  2677. reply_len = -1;
  2678. }
  2679. send_reply:
  2680. if (reply_len < 0) {
  2681. os_memcpy(reply, "FAIL\n", 5);
  2682. reply_len = 5;
  2683. }
  2684. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  2685. fromlen) < 0) {
  2686. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2687. strerror(errno));
  2688. }
  2689. os_free(reply);
  2690. }
  2691. #ifndef CONFIG_CTRL_IFACE_UDP
  2692. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  2693. {
  2694. char *buf;
  2695. size_t len;
  2696. if (interface->global_iface_path == NULL)
  2697. return NULL;
  2698. len = os_strlen(interface->global_iface_path) +
  2699. os_strlen(interface->global_iface_name) + 2;
  2700. buf = os_malloc(len);
  2701. if (buf == NULL)
  2702. return NULL;
  2703. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  2704. interface->global_iface_name);
  2705. buf[len - 1] = '\0';
  2706. return buf;
  2707. }
  2708. #endif /* CONFIG_CTRL_IFACE_UDP */
  2709. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  2710. {
  2711. #ifdef CONFIG_CTRL_IFACE_UDP
  2712. int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
  2713. char p[32] = { 0 };
  2714. char *pos;
  2715. struct addrinfo hints = { 0 }, *res, *saveres;
  2716. int n;
  2717. if (interface->global_ctrl_sock > -1) {
  2718. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2719. return 0;
  2720. }
  2721. if (interface->global_iface_path == NULL)
  2722. return 0;
  2723. pos = os_strstr(interface->global_iface_path, "udp:");
  2724. if (pos) {
  2725. pos += 4;
  2726. port = atoi(pos);
  2727. if (port <= 0) {
  2728. wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
  2729. goto fail;
  2730. }
  2731. }
  2732. dl_list_init(&interface->global_ctrl_dst);
  2733. interface->global_ctrl_sock = -1;
  2734. os_get_random(gcookie, COOKIE_LEN);
  2735. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  2736. hints.ai_flags = AI_PASSIVE;
  2737. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  2738. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  2739. hints.ai_family = AF_INET6;
  2740. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2741. hints.ai_family = AF_INET;
  2742. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2743. hints.ai_socktype = SOCK_DGRAM;
  2744. try_again:
  2745. os_snprintf(p, sizeof(p), "%d", port);
  2746. n = getaddrinfo(NULL, p, &hints, &res);
  2747. if (n) {
  2748. wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
  2749. goto fail;
  2750. }
  2751. saveres = res;
  2752. interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
  2753. res->ai_protocol);
  2754. if (interface->global_ctrl_sock < 0) {
  2755. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  2756. goto fail;
  2757. }
  2758. if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
  2759. 0) {
  2760. port++;
  2761. if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
  2762. HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
  2763. goto try_again;
  2764. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  2765. goto fail;
  2766. }
  2767. freeaddrinfo(saveres);
  2768. wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
  2769. if (eloop_register_read_sock(interface->global_ctrl_sock,
  2770. hostapd_global_ctrl_iface_receive,
  2771. interface, NULL) < 0) {
  2772. hostapd_global_ctrl_iface_deinit(interface);
  2773. return -1;
  2774. }
  2775. return 0;
  2776. fail:
  2777. if (interface->global_ctrl_sock >= 0)
  2778. close(interface->global_ctrl_sock);
  2779. return -1;
  2780. #else /* CONFIG_CTRL_IFACE_UDP */
  2781. struct sockaddr_un addr;
  2782. int s = -1;
  2783. char *fname = NULL;
  2784. if (interface->global_iface_path == NULL) {
  2785. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  2786. return 0;
  2787. }
  2788. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  2789. if (errno == EEXIST) {
  2790. wpa_printf(MSG_DEBUG, "Using existing control "
  2791. "interface directory.");
  2792. } else {
  2793. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2794. strerror(errno));
  2795. goto fail;
  2796. }
  2797. } else if (interface->ctrl_iface_group &&
  2798. chown(interface->global_iface_path, -1,
  2799. interface->ctrl_iface_group) < 0) {
  2800. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2801. strerror(errno));
  2802. goto fail;
  2803. }
  2804. if (os_strlen(interface->global_iface_path) + 1 +
  2805. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  2806. goto fail;
  2807. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2808. if (s < 0) {
  2809. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2810. goto fail;
  2811. }
  2812. os_memset(&addr, 0, sizeof(addr));
  2813. #ifdef __FreeBSD__
  2814. addr.sun_len = sizeof(addr);
  2815. #endif /* __FreeBSD__ */
  2816. addr.sun_family = AF_UNIX;
  2817. fname = hostapd_global_ctrl_iface_path(interface);
  2818. if (fname == NULL)
  2819. goto fail;
  2820. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2821. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2822. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2823. strerror(errno));
  2824. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2825. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2826. " allow connections - assuming it was left"
  2827. "over from forced program termination");
  2828. if (unlink(fname) < 0) {
  2829. wpa_printf(MSG_ERROR,
  2830. "Could not unlink existing ctrl_iface socket '%s': %s",
  2831. fname, strerror(errno));
  2832. goto fail;
  2833. }
  2834. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2835. 0) {
  2836. wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
  2837. strerror(errno));
  2838. goto fail;
  2839. }
  2840. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2841. "ctrl_iface socket '%s'", fname);
  2842. } else {
  2843. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2844. "be in use - cannot override it");
  2845. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2846. "not used anymore", fname);
  2847. os_free(fname);
  2848. fname = NULL;
  2849. goto fail;
  2850. }
  2851. }
  2852. if (interface->ctrl_iface_group &&
  2853. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  2854. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2855. strerror(errno));
  2856. goto fail;
  2857. }
  2858. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  2859. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  2860. strerror(errno));
  2861. goto fail;
  2862. }
  2863. os_free(fname);
  2864. interface->global_ctrl_sock = s;
  2865. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  2866. interface, NULL);
  2867. return 0;
  2868. fail:
  2869. if (s >= 0)
  2870. close(s);
  2871. if (fname) {
  2872. unlink(fname);
  2873. os_free(fname);
  2874. }
  2875. return -1;
  2876. #endif /* CONFIG_CTRL_IFACE_UDP */
  2877. }
  2878. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  2879. {
  2880. #ifndef CONFIG_CTRL_IFACE_UDP
  2881. char *fname = NULL;
  2882. #endif /* CONFIG_CTRL_IFACE_UDP */
  2883. struct wpa_ctrl_dst *dst, *prev;
  2884. if (interfaces->global_ctrl_sock > -1) {
  2885. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  2886. close(interfaces->global_ctrl_sock);
  2887. interfaces->global_ctrl_sock = -1;
  2888. #ifndef CONFIG_CTRL_IFACE_UDP
  2889. fname = hostapd_global_ctrl_iface_path(interfaces);
  2890. if (fname) {
  2891. unlink(fname);
  2892. os_free(fname);
  2893. }
  2894. if (interfaces->global_iface_path &&
  2895. rmdir(interfaces->global_iface_path) < 0) {
  2896. if (errno == ENOTEMPTY) {
  2897. wpa_printf(MSG_DEBUG, "Control interface "
  2898. "directory not empty - leaving it "
  2899. "behind");
  2900. } else {
  2901. wpa_printf(MSG_ERROR,
  2902. "rmdir[ctrl_interface=%s]: %s",
  2903. interfaces->global_iface_path,
  2904. strerror(errno));
  2905. }
  2906. }
  2907. #endif /* CONFIG_CTRL_IFACE_UDP */
  2908. }
  2909. os_free(interfaces->global_iface_path);
  2910. interfaces->global_iface_path = NULL;
  2911. dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
  2912. struct wpa_ctrl_dst, list)
  2913. os_free(dst);
  2914. }
  2915. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  2916. enum wpa_msg_type type,
  2917. const char *buf, size_t len)
  2918. {
  2919. struct wpa_ctrl_dst *dst, *next;
  2920. struct dl_list *ctrl_dst;
  2921. struct msghdr msg;
  2922. int idx;
  2923. struct iovec io[2];
  2924. char levelstr[10];
  2925. int s;
  2926. if (type != WPA_MSG_ONLY_GLOBAL) {
  2927. s = hapd->ctrl_sock;
  2928. ctrl_dst = &hapd->ctrl_dst;
  2929. } else {
  2930. s = hapd->iface->interfaces->global_ctrl_sock;
  2931. ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
  2932. }
  2933. if (s < 0 || dl_list_empty(ctrl_dst))
  2934. return;
  2935. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  2936. io[0].iov_base = levelstr;
  2937. io[0].iov_len = os_strlen(levelstr);
  2938. io[1].iov_base = (char *) buf;
  2939. io[1].iov_len = len;
  2940. os_memset(&msg, 0, sizeof(msg));
  2941. msg.msg_iov = io;
  2942. msg.msg_iovlen = 2;
  2943. idx = 0;
  2944. dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
  2945. if (level >= dst->debug_level) {
  2946. sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
  2947. &dst->addr, dst->addrlen);
  2948. msg.msg_name = &dst->addr;
  2949. msg.msg_namelen = dst->addrlen;
  2950. if (sendmsg(s, &msg, 0) < 0) {
  2951. int _errno = errno;
  2952. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  2953. "%d - %s",
  2954. idx, errno, strerror(errno));
  2955. dst->errors++;
  2956. if (dst->errors > 10 || _errno == ENOENT) {
  2957. if (type != WPA_MSG_ONLY_GLOBAL)
  2958. hostapd_ctrl_iface_detach(
  2959. hapd, &dst->addr,
  2960. dst->addrlen);
  2961. else
  2962. hostapd_global_ctrl_iface_detach(
  2963. hapd->iface->interfaces,
  2964. &dst->addr,
  2965. dst->addrlen);
  2966. }
  2967. } else
  2968. dst->errors = 0;
  2969. }
  2970. idx++;
  2971. }
  2972. }
  2973. #endif /* CONFIG_NATIVE_WINDOWS */