ctrl_iface.c 68 KB

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