ctrl_iface.c 75 KB

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