ctrl_iface.c 61 KB

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