ctrl_iface.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  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. #include <sys/un.h>
  11. #include <sys/stat.h>
  12. #include <stddef.h>
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "common/version.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "drivers/driver.h"
  18. #include "radius/radius_client.h"
  19. #include "radius/radius_server.h"
  20. #include "ap/hostapd.h"
  21. #include "ap/ap_config.h"
  22. #include "ap/ieee802_1x.h"
  23. #include "ap/wpa_auth.h"
  24. #include "ap/ieee802_11.h"
  25. #include "ap/sta_info.h"
  26. #include "ap/wps_hostapd.h"
  27. #include "ap/ctrl_iface_ap.h"
  28. #include "ap/ap_drv_ops.h"
  29. #include "ap/hs20.h"
  30. #include "ap/wnm_ap.h"
  31. #include "ap/wpa_auth.h"
  32. #include "wps/wps_defs.h"
  33. #include "wps/wps.h"
  34. #include "config_file.h"
  35. #include "ctrl_iface.h"
  36. struct wpa_ctrl_dst {
  37. struct wpa_ctrl_dst *next;
  38. struct sockaddr_un addr;
  39. socklen_t addrlen;
  40. int debug_level;
  41. int errors;
  42. };
  43. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  44. const char *buf, size_t len);
  45. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  46. struct sockaddr_un *from,
  47. socklen_t fromlen)
  48. {
  49. struct wpa_ctrl_dst *dst;
  50. dst = os_zalloc(sizeof(*dst));
  51. if (dst == NULL)
  52. return -1;
  53. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  54. dst->addrlen = fromlen;
  55. dst->debug_level = MSG_INFO;
  56. dst->next = hapd->ctrl_dst;
  57. hapd->ctrl_dst = dst;
  58. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  59. (u8 *) from->sun_path,
  60. fromlen - offsetof(struct sockaddr_un, sun_path));
  61. return 0;
  62. }
  63. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  64. struct sockaddr_un *from,
  65. socklen_t fromlen)
  66. {
  67. struct wpa_ctrl_dst *dst, *prev = NULL;
  68. dst = hapd->ctrl_dst;
  69. while (dst) {
  70. if (fromlen == dst->addrlen &&
  71. os_memcmp(from->sun_path, dst->addr.sun_path,
  72. fromlen - offsetof(struct sockaddr_un, sun_path))
  73. == 0) {
  74. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  75. (u8 *) from->sun_path,
  76. fromlen -
  77. offsetof(struct sockaddr_un, sun_path));
  78. if (prev == NULL)
  79. hapd->ctrl_dst = dst->next;
  80. else
  81. prev->next = dst->next;
  82. os_free(dst);
  83. return 0;
  84. }
  85. prev = dst;
  86. dst = dst->next;
  87. }
  88. return -1;
  89. }
  90. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  91. struct sockaddr_un *from,
  92. socklen_t fromlen,
  93. char *level)
  94. {
  95. struct wpa_ctrl_dst *dst;
  96. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  97. dst = hapd->ctrl_dst;
  98. while (dst) {
  99. if (fromlen == dst->addrlen &&
  100. os_memcmp(from->sun_path, dst->addr.sun_path,
  101. fromlen - offsetof(struct sockaddr_un, sun_path))
  102. == 0) {
  103. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  104. "level", (u8 *) from->sun_path, fromlen -
  105. offsetof(struct sockaddr_un, sun_path));
  106. dst->debug_level = atoi(level);
  107. return 0;
  108. }
  109. dst = dst->next;
  110. }
  111. return -1;
  112. }
  113. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  114. const char *txtaddr)
  115. {
  116. u8 addr[ETH_ALEN];
  117. struct sta_info *sta;
  118. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  119. if (hwaddr_aton(txtaddr, addr))
  120. return -1;
  121. sta = ap_get_sta(hapd, addr);
  122. if (sta)
  123. return 0;
  124. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  125. "notification", MAC2STR(addr));
  126. sta = ap_sta_add(hapd, addr);
  127. if (sta == NULL)
  128. return -1;
  129. hostapd_new_assoc_sta(hapd, sta, 0);
  130. return 0;
  131. }
  132. #ifdef CONFIG_IEEE80211W
  133. #ifdef NEED_AP_MLME
  134. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  135. const char *txtaddr)
  136. {
  137. u8 addr[ETH_ALEN];
  138. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  139. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  140. if (hwaddr_aton(txtaddr, addr) ||
  141. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  142. return -1;
  143. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  144. return 0;
  145. }
  146. #endif /* NEED_AP_MLME */
  147. #endif /* CONFIG_IEEE80211W */
  148. #ifdef CONFIG_WPS
  149. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  150. {
  151. char *pin = os_strchr(txt, ' ');
  152. char *timeout_txt;
  153. int timeout;
  154. u8 addr_buf[ETH_ALEN], *addr = NULL;
  155. char *pos;
  156. if (pin == NULL)
  157. return -1;
  158. *pin++ = '\0';
  159. timeout_txt = os_strchr(pin, ' ');
  160. if (timeout_txt) {
  161. *timeout_txt++ = '\0';
  162. timeout = atoi(timeout_txt);
  163. pos = os_strchr(timeout_txt, ' ');
  164. if (pos) {
  165. *pos++ = '\0';
  166. if (hwaddr_aton(pos, addr_buf) == 0)
  167. addr = addr_buf;
  168. }
  169. } else
  170. timeout = 0;
  171. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  172. }
  173. static int hostapd_ctrl_iface_wps_check_pin(
  174. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  175. {
  176. char pin[9];
  177. size_t len;
  178. char *pos;
  179. int ret;
  180. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  181. (u8 *) cmd, os_strlen(cmd));
  182. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  183. if (*pos < '0' || *pos > '9')
  184. continue;
  185. pin[len++] = *pos;
  186. if (len == 9) {
  187. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  188. return -1;
  189. }
  190. }
  191. if (len != 4 && len != 8) {
  192. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  193. return -1;
  194. }
  195. pin[len] = '\0';
  196. if (len == 8) {
  197. unsigned int pin_val;
  198. pin_val = atoi(pin);
  199. if (!wps_pin_valid(pin_val)) {
  200. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  201. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  202. if (ret < 0 || (size_t) ret >= buflen)
  203. return -1;
  204. return ret;
  205. }
  206. }
  207. ret = os_snprintf(buf, buflen, "%s", pin);
  208. if (ret < 0 || (size_t) ret >= buflen)
  209. return -1;
  210. return ret;
  211. }
  212. #ifdef CONFIG_WPS_NFC
  213. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  214. char *pos)
  215. {
  216. size_t len;
  217. struct wpabuf *buf;
  218. int ret;
  219. len = os_strlen(pos);
  220. if (len & 0x01)
  221. return -1;
  222. len /= 2;
  223. buf = wpabuf_alloc(len);
  224. if (buf == NULL)
  225. return -1;
  226. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  227. wpabuf_free(buf);
  228. return -1;
  229. }
  230. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  231. wpabuf_free(buf);
  232. return ret;
  233. }
  234. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  235. char *cmd, char *reply,
  236. size_t max_len)
  237. {
  238. int ndef;
  239. struct wpabuf *buf;
  240. int res;
  241. if (os_strcmp(cmd, "WPS") == 0)
  242. ndef = 0;
  243. else if (os_strcmp(cmd, "NDEF") == 0)
  244. ndef = 1;
  245. else
  246. return -1;
  247. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  248. if (buf == NULL)
  249. return -1;
  250. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  251. wpabuf_len(buf));
  252. reply[res++] = '\n';
  253. reply[res] = '\0';
  254. wpabuf_free(buf);
  255. return res;
  256. }
  257. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  258. char *reply, size_t max_len,
  259. int ndef)
  260. {
  261. struct wpabuf *buf;
  262. int res;
  263. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  264. if (buf == NULL)
  265. return -1;
  266. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  267. wpabuf_len(buf));
  268. reply[res++] = '\n';
  269. reply[res] = '\0';
  270. wpabuf_free(buf);
  271. return res;
  272. }
  273. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  274. char *cmd, char *reply,
  275. size_t max_len)
  276. {
  277. if (os_strcmp(cmd, "WPS") == 0)
  278. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  279. max_len, 0);
  280. if (os_strcmp(cmd, "NDEF") == 0)
  281. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  282. max_len, 1);
  283. if (os_strcmp(cmd, "enable") == 0)
  284. return hostapd_wps_nfc_token_enable(hapd);
  285. if (os_strcmp(cmd, "disable") == 0) {
  286. hostapd_wps_nfc_token_disable(hapd);
  287. return 0;
  288. }
  289. return -1;
  290. }
  291. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  292. char *cmd, char *reply,
  293. size_t max_len)
  294. {
  295. struct wpabuf *buf;
  296. int res;
  297. char *pos;
  298. int ndef;
  299. pos = os_strchr(cmd, ' ');
  300. if (pos == NULL)
  301. return -1;
  302. *pos++ = '\0';
  303. if (os_strcmp(cmd, "WPS") == 0)
  304. ndef = 0;
  305. else if (os_strcmp(cmd, "NDEF") == 0)
  306. ndef = 1;
  307. else
  308. return -1;
  309. if (os_strcmp(pos, "WPS-CR") == 0)
  310. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  311. else
  312. buf = NULL;
  313. if (buf == NULL)
  314. return -1;
  315. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  316. wpabuf_len(buf));
  317. reply[res++] = '\n';
  318. reply[res] = '\0';
  319. wpabuf_free(buf);
  320. return res;
  321. }
  322. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  323. char *cmd)
  324. {
  325. size_t len;
  326. struct wpabuf *req, *sel;
  327. int ret;
  328. char *pos, *role, *type, *pos2;
  329. role = cmd;
  330. pos = os_strchr(role, ' ');
  331. if (pos == NULL)
  332. return -1;
  333. *pos++ = '\0';
  334. type = pos;
  335. pos = os_strchr(type, ' ');
  336. if (pos == NULL)
  337. return -1;
  338. *pos++ = '\0';
  339. pos2 = os_strchr(pos, ' ');
  340. if (pos2 == NULL)
  341. return -1;
  342. *pos2++ = '\0';
  343. len = os_strlen(pos);
  344. if (len & 0x01)
  345. return -1;
  346. len /= 2;
  347. req = wpabuf_alloc(len);
  348. if (req == NULL)
  349. return -1;
  350. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  351. wpabuf_free(req);
  352. return -1;
  353. }
  354. len = os_strlen(pos2);
  355. if (len & 0x01) {
  356. wpabuf_free(req);
  357. return -1;
  358. }
  359. len /= 2;
  360. sel = wpabuf_alloc(len);
  361. if (sel == NULL) {
  362. wpabuf_free(req);
  363. return -1;
  364. }
  365. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  366. wpabuf_free(req);
  367. wpabuf_free(sel);
  368. return -1;
  369. }
  370. if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
  371. ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
  372. } else {
  373. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  374. "reported: role=%s type=%s", role, type);
  375. ret = -1;
  376. }
  377. wpabuf_free(req);
  378. wpabuf_free(sel);
  379. return ret;
  380. }
  381. #endif /* CONFIG_WPS_NFC */
  382. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  383. char *buf, size_t buflen)
  384. {
  385. int timeout = 300;
  386. char *pos;
  387. const char *pin_txt;
  388. pos = os_strchr(txt, ' ');
  389. if (pos)
  390. *pos++ = '\0';
  391. if (os_strcmp(txt, "disable") == 0) {
  392. hostapd_wps_ap_pin_disable(hapd);
  393. return os_snprintf(buf, buflen, "OK\n");
  394. }
  395. if (os_strcmp(txt, "random") == 0) {
  396. if (pos)
  397. timeout = atoi(pos);
  398. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  399. if (pin_txt == NULL)
  400. return -1;
  401. return os_snprintf(buf, buflen, "%s", pin_txt);
  402. }
  403. if (os_strcmp(txt, "get") == 0) {
  404. pin_txt = hostapd_wps_ap_pin_get(hapd);
  405. if (pin_txt == NULL)
  406. return -1;
  407. return os_snprintf(buf, buflen, "%s", pin_txt);
  408. }
  409. if (os_strcmp(txt, "set") == 0) {
  410. char *pin;
  411. if (pos == NULL)
  412. return -1;
  413. pin = pos;
  414. pos = os_strchr(pos, ' ');
  415. if (pos) {
  416. *pos++ = '\0';
  417. timeout = atoi(pos);
  418. }
  419. if (os_strlen(pin) > buflen)
  420. return -1;
  421. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  422. return -1;
  423. return os_snprintf(buf, buflen, "%s", pin);
  424. }
  425. return -1;
  426. }
  427. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  428. {
  429. char *pos;
  430. char *ssid, *auth, *encr = NULL, *key = NULL;
  431. ssid = txt;
  432. pos = os_strchr(txt, ' ');
  433. if (!pos)
  434. return -1;
  435. *pos++ = '\0';
  436. auth = pos;
  437. pos = os_strchr(pos, ' ');
  438. if (pos) {
  439. *pos++ = '\0';
  440. encr = pos;
  441. pos = os_strchr(pos, ' ');
  442. if (pos) {
  443. *pos++ = '\0';
  444. key = pos;
  445. }
  446. }
  447. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  448. }
  449. static const char * pbc_status_str(enum pbc_status status)
  450. {
  451. switch (status) {
  452. case WPS_PBC_STATUS_DISABLE:
  453. return "Disabled";
  454. case WPS_PBC_STATUS_ACTIVE:
  455. return "Active";
  456. case WPS_PBC_STATUS_TIMEOUT:
  457. return "Timed-out";
  458. case WPS_PBC_STATUS_OVERLAP:
  459. return "Overlap";
  460. default:
  461. return "Unknown";
  462. }
  463. }
  464. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  465. char *buf, size_t buflen)
  466. {
  467. int ret;
  468. char *pos, *end;
  469. pos = buf;
  470. end = buf + buflen;
  471. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  472. pbc_status_str(hapd->wps_stats.pbc_status));
  473. if (ret < 0 || ret >= end - pos)
  474. return pos - buf;
  475. pos += ret;
  476. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  477. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  478. "Success":
  479. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  480. "Failed" : "None")));
  481. if (ret < 0 || ret >= end - pos)
  482. return pos - buf;
  483. pos += ret;
  484. /* If status == Failure - Add possible Reasons */
  485. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  486. hapd->wps_stats.failure_reason > 0) {
  487. ret = os_snprintf(pos, end - pos,
  488. "Failure Reason: %s\n",
  489. wps_ei_str(hapd->wps_stats.failure_reason));
  490. if (ret < 0 || ret >= end - pos)
  491. return pos - buf;
  492. pos += ret;
  493. }
  494. if (hapd->wps_stats.status) {
  495. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  496. MAC2STR(hapd->wps_stats.peer_addr));
  497. if (ret < 0 || ret >= end - pos)
  498. return pos - buf;
  499. pos += ret;
  500. }
  501. return pos - buf;
  502. }
  503. #endif /* CONFIG_WPS */
  504. #ifdef CONFIG_HS20
  505. static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
  506. const char *cmd)
  507. {
  508. u8 addr[ETH_ALEN];
  509. const char *url;
  510. if (hwaddr_aton(cmd, addr))
  511. return -1;
  512. url = cmd + 17;
  513. if (*url == '\0') {
  514. url = NULL;
  515. } else {
  516. if (*url != ' ')
  517. return -1;
  518. url++;
  519. if (*url == '\0')
  520. url = NULL;
  521. }
  522. return hs20_send_wnm_notification(hapd, addr, 1, url);
  523. }
  524. static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
  525. const char *cmd)
  526. {
  527. u8 addr[ETH_ALEN];
  528. int code, reauth_delay, ret;
  529. const char *pos;
  530. size_t url_len;
  531. struct wpabuf *req;
  532. /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
  533. if (hwaddr_aton(cmd, addr))
  534. return -1;
  535. pos = os_strchr(cmd, ' ');
  536. if (pos == NULL)
  537. return -1;
  538. pos++;
  539. code = atoi(pos);
  540. pos = os_strchr(pos, ' ');
  541. if (pos == NULL)
  542. return -1;
  543. pos++;
  544. reauth_delay = atoi(pos);
  545. url_len = 0;
  546. pos = os_strchr(pos, ' ');
  547. if (pos) {
  548. pos++;
  549. url_len = os_strlen(pos);
  550. }
  551. req = wpabuf_alloc(4 + url_len);
  552. if (req == NULL)
  553. return -1;
  554. wpabuf_put_u8(req, code);
  555. wpabuf_put_le16(req, reauth_delay);
  556. wpabuf_put_u8(req, url_len);
  557. if (pos)
  558. wpabuf_put_data(req, pos, url_len);
  559. wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
  560. " to indicate imminent deauthentication (code=%d "
  561. "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
  562. ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
  563. wpabuf_free(req);
  564. return ret;
  565. }
  566. #endif /* CONFIG_HS20 */
  567. #ifdef CONFIG_INTERWORKING
  568. static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
  569. const char *cmd)
  570. {
  571. u8 qos_map_set[16 + 2 * 21], count = 0;
  572. const char *pos = cmd;
  573. int val, ret;
  574. for (;;) {
  575. if (count == sizeof(qos_map_set)) {
  576. wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
  577. return -1;
  578. }
  579. val = atoi(pos);
  580. if (val < 0 || val > 255) {
  581. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  582. return -1;
  583. }
  584. qos_map_set[count++] = val;
  585. pos = os_strchr(pos, ',');
  586. if (!pos)
  587. break;
  588. pos++;
  589. }
  590. if (count < 16 || count & 1) {
  591. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  592. return -1;
  593. }
  594. ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
  595. if (ret) {
  596. wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
  597. return -1;
  598. }
  599. os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
  600. hapd->conf->qos_map_set_len = count;
  601. return 0;
  602. }
  603. static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
  604. const char *cmd)
  605. {
  606. u8 addr[ETH_ALEN];
  607. struct sta_info *sta;
  608. struct wpabuf *buf;
  609. u8 *qos_map_set = hapd->conf->qos_map_set;
  610. u8 qos_map_set_len = hapd->conf->qos_map_set_len;
  611. int ret;
  612. if (!qos_map_set_len) {
  613. wpa_printf(MSG_INFO, "QoS Map Set is not set");
  614. return -1;
  615. }
  616. if (hwaddr_aton(cmd, addr))
  617. return -1;
  618. sta = ap_get_sta(hapd, addr);
  619. if (sta == NULL) {
  620. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  621. "for QoS Map Configuration message",
  622. MAC2STR(addr));
  623. return -1;
  624. }
  625. if (!sta->qos_map_enabled) {
  626. wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
  627. "support for QoS Map", MAC2STR(addr));
  628. return -1;
  629. }
  630. buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
  631. if (buf == NULL)
  632. return -1;
  633. wpabuf_put_u8(buf, WLAN_ACTION_QOS);
  634. wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
  635. /* QoS Map Set Element */
  636. wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
  637. wpabuf_put_u8(buf, qos_map_set_len);
  638. wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
  639. ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
  640. wpabuf_head(buf), wpabuf_len(buf));
  641. wpabuf_free(buf);
  642. return ret;
  643. }
  644. #endif /* CONFIG_INTERWORKING */
  645. #ifdef CONFIG_WNM
  646. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  647. const char *cmd)
  648. {
  649. u8 addr[ETH_ALEN];
  650. int disassoc_timer;
  651. struct sta_info *sta;
  652. if (hwaddr_aton(cmd, addr))
  653. return -1;
  654. if (cmd[17] != ' ')
  655. return -1;
  656. disassoc_timer = atoi(cmd + 17);
  657. sta = ap_get_sta(hapd, addr);
  658. if (sta == NULL) {
  659. wpa_printf(MSG_DEBUG, "Station " MACSTR
  660. " not found for disassociation imminent message",
  661. MAC2STR(addr));
  662. return -1;
  663. }
  664. return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
  665. }
  666. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  667. const char *cmd)
  668. {
  669. u8 addr[ETH_ALEN];
  670. const char *url, *timerstr;
  671. int disassoc_timer;
  672. struct sta_info *sta;
  673. if (hwaddr_aton(cmd, addr))
  674. return -1;
  675. sta = ap_get_sta(hapd, addr);
  676. if (sta == NULL) {
  677. wpa_printf(MSG_DEBUG, "Station " MACSTR
  678. " not found for ESS disassociation imminent message",
  679. MAC2STR(addr));
  680. return -1;
  681. }
  682. timerstr = cmd + 17;
  683. if (*timerstr != ' ')
  684. return -1;
  685. timerstr++;
  686. disassoc_timer = atoi(timerstr);
  687. if (disassoc_timer < 0 || disassoc_timer > 65535)
  688. return -1;
  689. url = os_strchr(timerstr, ' ');
  690. if (url == NULL)
  691. return -1;
  692. url++;
  693. return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
  694. }
  695. #endif /* CONFIG_WNM */
  696. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  697. char *buf, size_t buflen)
  698. {
  699. int ret;
  700. char *pos, *end;
  701. pos = buf;
  702. end = buf + buflen;
  703. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  704. "ssid=%s\n",
  705. MAC2STR(hapd->own_addr),
  706. wpa_ssid_txt(hapd->conf->ssid.ssid,
  707. hapd->conf->ssid.ssid_len));
  708. if (ret < 0 || ret >= end - pos)
  709. return pos - buf;
  710. pos += ret;
  711. #ifdef CONFIG_WPS
  712. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  713. hapd->conf->wps_state == 0 ? "disabled" :
  714. (hapd->conf->wps_state == 1 ? "not configured" :
  715. "configured"));
  716. if (ret < 0 || ret >= end - pos)
  717. return pos - buf;
  718. pos += ret;
  719. if (hapd->conf->wps_state && hapd->conf->wpa &&
  720. hapd->conf->ssid.wpa_passphrase) {
  721. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  722. hapd->conf->ssid.wpa_passphrase);
  723. if (ret < 0 || ret >= end - pos)
  724. return pos - buf;
  725. pos += ret;
  726. }
  727. if (hapd->conf->wps_state && hapd->conf->wpa &&
  728. hapd->conf->ssid.wpa_psk &&
  729. hapd->conf->ssid.wpa_psk->group) {
  730. char hex[PMK_LEN * 2 + 1];
  731. wpa_snprintf_hex(hex, sizeof(hex),
  732. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  733. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  734. if (ret < 0 || ret >= end - pos)
  735. return pos - buf;
  736. pos += ret;
  737. }
  738. #endif /* CONFIG_WPS */
  739. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  740. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  741. if (ret < 0 || ret >= end - pos)
  742. return pos - buf;
  743. pos += ret;
  744. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  745. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  746. if (ret < 0 || ret >= end - pos)
  747. return pos - buf;
  748. pos += ret;
  749. }
  750. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  751. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  752. if (ret < 0 || ret >= end - pos)
  753. return pos - buf;
  754. pos += ret;
  755. }
  756. #ifdef CONFIG_IEEE80211R
  757. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  758. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  759. if (ret < 0 || ret >= end - pos)
  760. return pos - buf;
  761. pos += ret;
  762. }
  763. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  764. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  765. if (ret < 0 || ret >= end - pos)
  766. return pos - buf;
  767. pos += ret;
  768. }
  769. #ifdef CONFIG_SAE
  770. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  771. ret = os_snprintf(pos, end - pos, "FT-SAE ");
  772. if (ret < 0 || ret >= end - pos)
  773. return pos - buf;
  774. pos += ret;
  775. }
  776. #endif /* CONFIG_SAE */
  777. #endif /* CONFIG_IEEE80211R */
  778. #ifdef CONFIG_IEEE80211W
  779. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  780. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  781. if (ret < 0 || ret >= end - pos)
  782. return pos - buf;
  783. pos += ret;
  784. }
  785. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  786. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  787. if (ret < 0 || ret >= end - pos)
  788. return pos - buf;
  789. pos += ret;
  790. }
  791. #endif /* CONFIG_IEEE80211W */
  792. #ifdef CONFIG_SAE
  793. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
  794. ret = os_snprintf(pos, end - pos, "SAE ");
  795. if (ret < 0 || ret >= end - pos)
  796. return pos - buf;
  797. pos += ret;
  798. }
  799. #endif /* CONFIG_SAE */
  800. ret = os_snprintf(pos, end - pos, "\n");
  801. if (ret < 0 || ret >= end - pos)
  802. return pos - buf;
  803. pos += ret;
  804. }
  805. if (hapd->conf->wpa) {
  806. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  807. wpa_cipher_txt(hapd->conf->wpa_group));
  808. if (ret < 0 || ret >= end - pos)
  809. return pos - buf;
  810. pos += ret;
  811. }
  812. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  813. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  814. if (ret < 0 || ret >= end - pos)
  815. return pos - buf;
  816. pos += ret;
  817. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  818. " ");
  819. if (ret < 0)
  820. return pos - buf;
  821. pos += ret;
  822. ret = os_snprintf(pos, end - pos, "\n");
  823. if (ret < 0 || ret >= end - pos)
  824. return pos - buf;
  825. pos += ret;
  826. }
  827. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  828. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  829. if (ret < 0 || ret >= end - pos)
  830. return pos - buf;
  831. pos += ret;
  832. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  833. " ");
  834. if (ret < 0)
  835. return pos - buf;
  836. pos += ret;
  837. ret = os_snprintf(pos, end - pos, "\n");
  838. if (ret < 0 || ret >= end - pos)
  839. return pos - buf;
  840. pos += ret;
  841. }
  842. return pos - buf;
  843. }
  844. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  845. {
  846. char *value;
  847. int ret = 0;
  848. value = os_strchr(cmd, ' ');
  849. if (value == NULL)
  850. return -1;
  851. *value++ = '\0';
  852. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  853. if (0) {
  854. #ifdef CONFIG_WPS_TESTING
  855. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  856. long int val;
  857. val = strtol(value, NULL, 0);
  858. if (val < 0 || val > 0xff) {
  859. ret = -1;
  860. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  861. "wps_version_number %ld", val);
  862. } else {
  863. wps_version_number = val;
  864. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  865. "version %u.%u",
  866. (wps_version_number & 0xf0) >> 4,
  867. wps_version_number & 0x0f);
  868. hostapd_wps_update_ie(hapd);
  869. }
  870. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  871. wps_testing_dummy_cred = atoi(value);
  872. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  873. wps_testing_dummy_cred);
  874. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  875. wps_corrupt_pkhash = atoi(value);
  876. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  877. wps_corrupt_pkhash);
  878. #endif /* CONFIG_WPS_TESTING */
  879. #ifdef CONFIG_INTERWORKING
  880. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  881. int val = atoi(value);
  882. if (val <= 0)
  883. ret = -1;
  884. else
  885. hapd->gas_frag_limit = val;
  886. #endif /* CONFIG_INTERWORKING */
  887. #ifdef CONFIG_TESTING_OPTIONS
  888. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  889. hapd->ext_mgmt_frame_handling = atoi(value);
  890. #endif /* CONFIG_TESTING_OPTIONS */
  891. } else {
  892. struct sta_info *sta;
  893. int vlan_id;
  894. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  895. if (ret)
  896. return ret;
  897. if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
  898. for (sta = hapd->sta_list; sta; sta = sta->next) {
  899. if (hostapd_maclist_found(
  900. hapd->conf->deny_mac,
  901. hapd->conf->num_deny_mac, sta->addr,
  902. &vlan_id) &&
  903. (!vlan_id || vlan_id == sta->vlan_id))
  904. ap_sta_disconnect(
  905. hapd, sta, sta->addr,
  906. WLAN_REASON_UNSPECIFIED);
  907. }
  908. } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
  909. os_strcasecmp(cmd, "accept_mac_file") == 0) {
  910. for (sta = hapd->sta_list; sta; sta = sta->next) {
  911. if (!hostapd_maclist_found(
  912. hapd->conf->accept_mac,
  913. hapd->conf->num_accept_mac,
  914. sta->addr, &vlan_id) ||
  915. (vlan_id && vlan_id != sta->vlan_id))
  916. ap_sta_disconnect(
  917. hapd, sta, sta->addr,
  918. WLAN_REASON_UNSPECIFIED);
  919. }
  920. }
  921. }
  922. return ret;
  923. }
  924. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  925. char *buf, size_t buflen)
  926. {
  927. int res;
  928. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  929. if (os_strcmp(cmd, "version") == 0) {
  930. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  931. if (res < 0 || (unsigned int) res >= buflen)
  932. return -1;
  933. return res;
  934. }
  935. return -1;
  936. }
  937. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  938. {
  939. if (hostapd_enable_iface(iface) < 0) {
  940. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  941. return -1;
  942. }
  943. return 0;
  944. }
  945. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  946. {
  947. if (hostapd_reload_iface(iface) < 0) {
  948. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  949. return -1;
  950. }
  951. return 0;
  952. }
  953. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  954. {
  955. if (hostapd_disable_iface(iface) < 0) {
  956. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  957. return -1;
  958. }
  959. return 0;
  960. }
  961. #ifdef CONFIG_TESTING_OPTIONS
  962. static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
  963. {
  964. union wpa_event_data data;
  965. char *pos, *param;
  966. enum wpa_event_type event;
  967. wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
  968. os_memset(&data, 0, sizeof(data));
  969. param = os_strchr(cmd, ' ');
  970. if (param == NULL)
  971. return -1;
  972. *param++ = '\0';
  973. if (os_strcmp(cmd, "DETECTED") == 0)
  974. event = EVENT_DFS_RADAR_DETECTED;
  975. else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
  976. event = EVENT_DFS_CAC_FINISHED;
  977. else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
  978. event = EVENT_DFS_CAC_ABORTED;
  979. else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
  980. event = EVENT_DFS_NOP_FINISHED;
  981. else {
  982. wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
  983. cmd);
  984. return -1;
  985. }
  986. pos = os_strstr(param, "freq=");
  987. if (pos)
  988. data.dfs_event.freq = atoi(pos + 5);
  989. pos = os_strstr(param, "ht_enabled=1");
  990. if (pos)
  991. data.dfs_event.ht_enabled = 1;
  992. pos = os_strstr(param, "chan_offset=");
  993. if (pos)
  994. data.dfs_event.chan_offset = atoi(pos + 12);
  995. pos = os_strstr(param, "chan_width=");
  996. if (pos)
  997. data.dfs_event.chan_width = atoi(pos + 11);
  998. pos = os_strstr(param, "cf1=");
  999. if (pos)
  1000. data.dfs_event.cf1 = atoi(pos + 4);
  1001. pos = os_strstr(param, "cf2=");
  1002. if (pos)
  1003. data.dfs_event.cf2 = atoi(pos + 4);
  1004. wpa_supplicant_event(hapd, event, &data);
  1005. return 0;
  1006. }
  1007. static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
  1008. {
  1009. size_t len;
  1010. u8 *buf;
  1011. int res;
  1012. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  1013. len = os_strlen(cmd);
  1014. if (len & 1)
  1015. return -1;
  1016. len /= 2;
  1017. buf = os_malloc(len);
  1018. if (buf == NULL)
  1019. return -1;
  1020. if (hexstr2bin(cmd, buf, len) < 0) {
  1021. os_free(buf);
  1022. return -1;
  1023. }
  1024. res = hostapd_drv_send_mlme(hapd, buf, len, 0);
  1025. os_free(buf);
  1026. return res;
  1027. }
  1028. #endif /* CONFIG_TESTING_OPTIONS */
  1029. static int hostapd_ctrl_iface_chan_switch(struct hostapd_data *hapd, char *pos)
  1030. {
  1031. #ifdef NEED_AP_MLME
  1032. struct csa_settings settings;
  1033. int ret = hostapd_parse_csa_settings(pos, &settings);
  1034. if (ret)
  1035. return ret;
  1036. return hostapd_switch_channel(hapd, &settings);
  1037. #else /* NEED_AP_MLME */
  1038. return -1;
  1039. #endif /* NEED_AP_MLME */
  1040. }
  1041. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  1042. int reply_size, const char *param)
  1043. {
  1044. #ifdef RADIUS_SERVER
  1045. if (os_strcmp(param, "radius_server") == 0) {
  1046. return radius_server_get_mib(hapd->radius_srv, reply,
  1047. reply_size);
  1048. }
  1049. #endif /* RADIUS_SERVER */
  1050. return -1;
  1051. }
  1052. static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
  1053. char *buf, size_t buflen)
  1054. {
  1055. int ret;
  1056. char *pos;
  1057. u8 *data = NULL;
  1058. unsigned int vendor_id, subcmd;
  1059. struct wpabuf *reply;
  1060. size_t data_len = 0;
  1061. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  1062. vendor_id = strtoul(cmd, &pos, 16);
  1063. if (!isblank(*pos))
  1064. return -EINVAL;
  1065. subcmd = strtoul(pos, &pos, 10);
  1066. if (*pos != '\0') {
  1067. if (!isblank(*pos++))
  1068. return -EINVAL;
  1069. data_len = os_strlen(pos);
  1070. }
  1071. if (data_len) {
  1072. data_len /= 2;
  1073. data = os_malloc(data_len);
  1074. if (!data)
  1075. return -ENOBUFS;
  1076. if (hexstr2bin(pos, data, data_len)) {
  1077. wpa_printf(MSG_DEBUG,
  1078. "Vendor command: wrong parameter format");
  1079. os_free(data);
  1080. return -EINVAL;
  1081. }
  1082. }
  1083. reply = wpabuf_alloc((buflen - 1) / 2);
  1084. if (!reply) {
  1085. os_free(data);
  1086. return -ENOBUFS;
  1087. }
  1088. ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
  1089. reply);
  1090. if (ret == 0)
  1091. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  1092. wpabuf_len(reply));
  1093. wpabuf_free(reply);
  1094. os_free(data);
  1095. return ret;
  1096. }
  1097. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  1098. void *sock_ctx)
  1099. {
  1100. struct hostapd_data *hapd = eloop_ctx;
  1101. char buf[4096];
  1102. int res;
  1103. struct sockaddr_un from;
  1104. socklen_t fromlen = sizeof(from);
  1105. char *reply;
  1106. const int reply_size = 4096;
  1107. int reply_len;
  1108. int level = MSG_DEBUG;
  1109. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1110. (struct sockaddr *) &from, &fromlen);
  1111. if (res < 0) {
  1112. perror("recvfrom(ctrl_iface)");
  1113. return;
  1114. }
  1115. buf[res] = '\0';
  1116. if (os_strcmp(buf, "PING") == 0)
  1117. level = MSG_EXCESSIVE;
  1118. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  1119. reply = os_malloc(reply_size);
  1120. if (reply == NULL) {
  1121. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  1122. fromlen);
  1123. return;
  1124. }
  1125. os_memcpy(reply, "OK\n", 3);
  1126. reply_len = 3;
  1127. if (os_strcmp(buf, "PING") == 0) {
  1128. os_memcpy(reply, "PONG\n", 5);
  1129. reply_len = 5;
  1130. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1131. if (wpa_debug_reopen_file() < 0)
  1132. reply_len = -1;
  1133. } else if (os_strcmp(buf, "STATUS") == 0) {
  1134. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  1135. reply_size);
  1136. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  1137. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  1138. } else if (os_strcmp(buf, "MIB") == 0) {
  1139. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  1140. if (reply_len >= 0) {
  1141. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  1142. reply_size - reply_len);
  1143. if (res < 0)
  1144. reply_len = -1;
  1145. else
  1146. reply_len += res;
  1147. }
  1148. if (reply_len >= 0) {
  1149. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  1150. reply_size - reply_len);
  1151. if (res < 0)
  1152. reply_len = -1;
  1153. else
  1154. reply_len += res;
  1155. }
  1156. #ifndef CONFIG_NO_RADIUS
  1157. if (reply_len >= 0) {
  1158. res = radius_client_get_mib(hapd->radius,
  1159. reply + reply_len,
  1160. reply_size - reply_len);
  1161. if (res < 0)
  1162. reply_len = -1;
  1163. else
  1164. reply_len += res;
  1165. }
  1166. #endif /* CONFIG_NO_RADIUS */
  1167. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  1168. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  1169. buf + 4);
  1170. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1171. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  1172. reply_size);
  1173. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1174. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  1175. reply_size);
  1176. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1177. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  1178. reply_size);
  1179. } else if (os_strcmp(buf, "ATTACH") == 0) {
  1180. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  1181. reply_len = -1;
  1182. } else if (os_strcmp(buf, "DETACH") == 0) {
  1183. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  1184. reply_len = -1;
  1185. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  1186. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  1187. buf + 6))
  1188. reply_len = -1;
  1189. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  1190. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  1191. reply_len = -1;
  1192. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  1193. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  1194. reply_len = -1;
  1195. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  1196. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  1197. reply_len = -1;
  1198. #ifdef CONFIG_IEEE80211W
  1199. #ifdef NEED_AP_MLME
  1200. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  1201. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  1202. reply_len = -1;
  1203. #endif /* NEED_AP_MLME */
  1204. #endif /* CONFIG_IEEE80211W */
  1205. #ifdef CONFIG_WPS
  1206. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1207. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  1208. reply_len = -1;
  1209. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  1210. reply_len = hostapd_ctrl_iface_wps_check_pin(
  1211. hapd, buf + 14, reply, reply_size);
  1212. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1213. if (hostapd_wps_button_pushed(hapd, NULL))
  1214. reply_len = -1;
  1215. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  1216. if (hostapd_wps_cancel(hapd))
  1217. reply_len = -1;
  1218. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  1219. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  1220. reply, reply_size);
  1221. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  1222. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  1223. reply_len = -1;
  1224. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  1225. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  1226. reply_size);
  1227. #ifdef CONFIG_WPS_NFC
  1228. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  1229. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  1230. reply_len = -1;
  1231. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  1232. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  1233. hapd, buf + 21, reply, reply_size);
  1234. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  1235. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  1236. hapd, buf + 14, reply, reply_size);
  1237. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  1238. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  1239. hapd, buf + 21, reply, reply_size);
  1240. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1241. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1242. reply_len = -1;
  1243. #endif /* CONFIG_WPS_NFC */
  1244. #endif /* CONFIG_WPS */
  1245. #ifdef CONFIG_INTERWORKING
  1246. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1247. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1248. reply_len = -1;
  1249. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1250. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1251. reply_len = -1;
  1252. #endif /* CONFIG_INTERWORKING */
  1253. #ifdef CONFIG_HS20
  1254. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  1255. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  1256. reply_len = -1;
  1257. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  1258. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  1259. reply_len = -1;
  1260. #endif /* CONFIG_HS20 */
  1261. #ifdef CONFIG_WNM
  1262. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1263. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1264. reply_len = -1;
  1265. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1266. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1267. reply_len = -1;
  1268. #endif /* CONFIG_WNM */
  1269. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1270. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1271. reply_size);
  1272. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1273. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1274. reply_len = -1;
  1275. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1276. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1277. reply_size);
  1278. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1279. if (hostapd_ctrl_iface_enable(hapd->iface))
  1280. reply_len = -1;
  1281. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1282. if (hostapd_ctrl_iface_reload(hapd->iface))
  1283. reply_len = -1;
  1284. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1285. if (hostapd_ctrl_iface_disable(hapd->iface))
  1286. reply_len = -1;
  1287. #ifdef CONFIG_TESTING_OPTIONS
  1288. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  1289. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  1290. reply_len = -1;
  1291. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  1292. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  1293. reply_len = -1;
  1294. #endif /* CONFIG_TESTING_OPTIONS */
  1295. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  1296. if (hostapd_ctrl_iface_chan_switch(hapd, buf + 12))
  1297. reply_len = -1;
  1298. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  1299. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  1300. reply_size);
  1301. } else {
  1302. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1303. reply_len = 16;
  1304. }
  1305. if (reply_len < 0) {
  1306. os_memcpy(reply, "FAIL\n", 5);
  1307. reply_len = 5;
  1308. }
  1309. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1310. os_free(reply);
  1311. }
  1312. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  1313. {
  1314. char *buf;
  1315. size_t len;
  1316. if (hapd->conf->ctrl_interface == NULL)
  1317. return NULL;
  1318. len = os_strlen(hapd->conf->ctrl_interface) +
  1319. os_strlen(hapd->conf->iface) + 2;
  1320. buf = os_malloc(len);
  1321. if (buf == NULL)
  1322. return NULL;
  1323. os_snprintf(buf, len, "%s/%s",
  1324. hapd->conf->ctrl_interface, hapd->conf->iface);
  1325. buf[len - 1] = '\0';
  1326. return buf;
  1327. }
  1328. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
  1329. const char *txt, size_t len)
  1330. {
  1331. struct hostapd_data *hapd = ctx;
  1332. if (hapd == NULL)
  1333. return;
  1334. hostapd_ctrl_iface_send(hapd, level, txt, len);
  1335. }
  1336. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  1337. {
  1338. struct sockaddr_un addr;
  1339. int s = -1;
  1340. char *fname = NULL;
  1341. if (hapd->ctrl_sock > -1) {
  1342. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  1343. return 0;
  1344. }
  1345. if (hapd->conf->ctrl_interface == NULL)
  1346. return 0;
  1347. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1348. if (errno == EEXIST) {
  1349. wpa_printf(MSG_DEBUG, "Using existing control "
  1350. "interface directory.");
  1351. } else {
  1352. perror("mkdir[ctrl_interface]");
  1353. goto fail;
  1354. }
  1355. }
  1356. if (hapd->conf->ctrl_interface_gid_set &&
  1357. chown(hapd->conf->ctrl_interface, -1,
  1358. hapd->conf->ctrl_interface_gid) < 0) {
  1359. perror("chown[ctrl_interface]");
  1360. return -1;
  1361. }
  1362. if (!hapd->conf->ctrl_interface_gid_set &&
  1363. hapd->iface->interfaces->ctrl_iface_group &&
  1364. chown(hapd->conf->ctrl_interface, -1,
  1365. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1366. perror("chown[ctrl_interface]");
  1367. return -1;
  1368. }
  1369. #ifdef ANDROID
  1370. /*
  1371. * Android is using umask 0077 which would leave the control interface
  1372. * directory without group access. This breaks things since Wi-Fi
  1373. * framework assumes that this directory can be accessed by other
  1374. * applications in the wifi group. Fix this by adding group access even
  1375. * if umask value would prevent this.
  1376. */
  1377. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1378. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  1379. strerror(errno));
  1380. /* Try to continue anyway */
  1381. }
  1382. #endif /* ANDROID */
  1383. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  1384. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  1385. goto fail;
  1386. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1387. if (s < 0) {
  1388. perror("socket(PF_UNIX)");
  1389. goto fail;
  1390. }
  1391. os_memset(&addr, 0, sizeof(addr));
  1392. #ifdef __FreeBSD__
  1393. addr.sun_len = sizeof(addr);
  1394. #endif /* __FreeBSD__ */
  1395. addr.sun_family = AF_UNIX;
  1396. fname = hostapd_ctrl_iface_path(hapd);
  1397. if (fname == NULL)
  1398. goto fail;
  1399. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1400. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1401. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1402. strerror(errno));
  1403. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1404. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1405. " allow connections - assuming it was left"
  1406. "over from forced program termination");
  1407. if (unlink(fname) < 0) {
  1408. perror("unlink[ctrl_iface]");
  1409. wpa_printf(MSG_ERROR, "Could not unlink "
  1410. "existing ctrl_iface socket '%s'",
  1411. fname);
  1412. goto fail;
  1413. }
  1414. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1415. 0) {
  1416. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  1417. goto fail;
  1418. }
  1419. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1420. "ctrl_iface socket '%s'", fname);
  1421. } else {
  1422. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1423. "be in use - cannot override it");
  1424. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1425. "not used anymore", fname);
  1426. os_free(fname);
  1427. fname = NULL;
  1428. goto fail;
  1429. }
  1430. }
  1431. if (hapd->conf->ctrl_interface_gid_set &&
  1432. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  1433. perror("chown[ctrl_interface/ifname]");
  1434. goto fail;
  1435. }
  1436. if (!hapd->conf->ctrl_interface_gid_set &&
  1437. hapd->iface->interfaces->ctrl_iface_group &&
  1438. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1439. perror("chown[ctrl_interface/ifname]");
  1440. goto fail;
  1441. }
  1442. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1443. perror("chmod[ctrl_interface/ifname]");
  1444. goto fail;
  1445. }
  1446. os_free(fname);
  1447. hapd->ctrl_sock = s;
  1448. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  1449. NULL);
  1450. hapd->msg_ctx = hapd;
  1451. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  1452. return 0;
  1453. fail:
  1454. if (s >= 0)
  1455. close(s);
  1456. if (fname) {
  1457. unlink(fname);
  1458. os_free(fname);
  1459. }
  1460. return -1;
  1461. }
  1462. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  1463. {
  1464. struct wpa_ctrl_dst *dst, *prev;
  1465. if (hapd->ctrl_sock > -1) {
  1466. char *fname;
  1467. eloop_unregister_read_sock(hapd->ctrl_sock);
  1468. close(hapd->ctrl_sock);
  1469. hapd->ctrl_sock = -1;
  1470. fname = hostapd_ctrl_iface_path(hapd);
  1471. if (fname)
  1472. unlink(fname);
  1473. os_free(fname);
  1474. if (hapd->conf->ctrl_interface &&
  1475. rmdir(hapd->conf->ctrl_interface) < 0) {
  1476. if (errno == ENOTEMPTY) {
  1477. wpa_printf(MSG_DEBUG, "Control interface "
  1478. "directory not empty - leaving it "
  1479. "behind");
  1480. } else {
  1481. wpa_printf(MSG_ERROR,
  1482. "rmdir[ctrl_interface=%s]: %s",
  1483. hapd->conf->ctrl_interface,
  1484. strerror(errno));
  1485. }
  1486. }
  1487. }
  1488. dst = hapd->ctrl_dst;
  1489. while (dst) {
  1490. prev = dst;
  1491. dst = dst->next;
  1492. os_free(prev);
  1493. }
  1494. }
  1495. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1496. char *buf)
  1497. {
  1498. if (hostapd_add_iface(interfaces, buf) < 0) {
  1499. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1500. return -1;
  1501. }
  1502. return 0;
  1503. }
  1504. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1505. char *buf)
  1506. {
  1507. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1508. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1509. return -1;
  1510. }
  1511. return 0;
  1512. }
  1513. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  1514. {
  1515. #ifdef CONFIG_WPS_TESTING
  1516. wps_version_number = 0x20;
  1517. wps_testing_dummy_cred = 0;
  1518. wps_corrupt_pkhash = 0;
  1519. #endif /* CONFIG_WPS_TESTING */
  1520. }
  1521. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1522. void *sock_ctx)
  1523. {
  1524. void *interfaces = eloop_ctx;
  1525. char buf[256];
  1526. int res;
  1527. struct sockaddr_un from;
  1528. socklen_t fromlen = sizeof(from);
  1529. char reply[24];
  1530. int reply_len;
  1531. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1532. (struct sockaddr *) &from, &fromlen);
  1533. if (res < 0) {
  1534. perror("recvfrom(ctrl_iface)");
  1535. return;
  1536. }
  1537. buf[res] = '\0';
  1538. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  1539. os_memcpy(reply, "OK\n", 3);
  1540. reply_len = 3;
  1541. if (os_strcmp(buf, "PING") == 0) {
  1542. os_memcpy(reply, "PONG\n", 5);
  1543. reply_len = 5;
  1544. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1545. if (wpa_debug_reopen_file() < 0)
  1546. reply_len = -1;
  1547. } else if (os_strcmp(buf, "FLUSH") == 0) {
  1548. hostapd_ctrl_iface_flush(interfaces);
  1549. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1550. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1551. reply_len = -1;
  1552. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1553. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1554. reply_len = -1;
  1555. #ifdef CONFIG_MODULE_TESTS
  1556. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  1557. int hapd_module_tests(void);
  1558. if (hapd_module_tests() < 0)
  1559. reply_len = -1;
  1560. #endif /* CONFIG_MODULE_TESTS */
  1561. } else {
  1562. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1563. "ignored");
  1564. reply_len = -1;
  1565. }
  1566. if (reply_len < 0) {
  1567. os_memcpy(reply, "FAIL\n", 5);
  1568. reply_len = 5;
  1569. }
  1570. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1571. }
  1572. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1573. {
  1574. char *buf;
  1575. size_t len;
  1576. if (interface->global_iface_path == NULL)
  1577. return NULL;
  1578. len = os_strlen(interface->global_iface_path) +
  1579. os_strlen(interface->global_iface_name) + 2;
  1580. buf = os_malloc(len);
  1581. if (buf == NULL)
  1582. return NULL;
  1583. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1584. interface->global_iface_name);
  1585. buf[len - 1] = '\0';
  1586. return buf;
  1587. }
  1588. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1589. {
  1590. struct sockaddr_un addr;
  1591. int s = -1;
  1592. char *fname = NULL;
  1593. if (interface->global_iface_path == NULL) {
  1594. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1595. return 0;
  1596. }
  1597. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1598. if (errno == EEXIST) {
  1599. wpa_printf(MSG_DEBUG, "Using existing control "
  1600. "interface directory.");
  1601. } else {
  1602. perror("mkdir[ctrl_interface]");
  1603. goto fail;
  1604. }
  1605. } else if (interface->ctrl_iface_group &&
  1606. chown(interface->global_iface_path, -1,
  1607. interface->ctrl_iface_group) < 0) {
  1608. perror("chown[ctrl_interface]");
  1609. goto fail;
  1610. }
  1611. if (os_strlen(interface->global_iface_path) + 1 +
  1612. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1613. goto fail;
  1614. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1615. if (s < 0) {
  1616. perror("socket(PF_UNIX)");
  1617. goto fail;
  1618. }
  1619. os_memset(&addr, 0, sizeof(addr));
  1620. #ifdef __FreeBSD__
  1621. addr.sun_len = sizeof(addr);
  1622. #endif /* __FreeBSD__ */
  1623. addr.sun_family = AF_UNIX;
  1624. fname = hostapd_global_ctrl_iface_path(interface);
  1625. if (fname == NULL)
  1626. goto fail;
  1627. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1628. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1629. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1630. strerror(errno));
  1631. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1632. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1633. " allow connections - assuming it was left"
  1634. "over from forced program termination");
  1635. if (unlink(fname) < 0) {
  1636. perror("unlink[ctrl_iface]");
  1637. wpa_printf(MSG_ERROR, "Could not unlink "
  1638. "existing ctrl_iface socket '%s'",
  1639. fname);
  1640. goto fail;
  1641. }
  1642. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1643. 0) {
  1644. perror("bind(PF_UNIX)");
  1645. goto fail;
  1646. }
  1647. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1648. "ctrl_iface socket '%s'", fname);
  1649. } else {
  1650. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1651. "be in use - cannot override it");
  1652. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1653. "not used anymore", fname);
  1654. os_free(fname);
  1655. fname = NULL;
  1656. goto fail;
  1657. }
  1658. }
  1659. if (interface->ctrl_iface_group &&
  1660. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  1661. perror("chown[ctrl_interface]");
  1662. goto fail;
  1663. }
  1664. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1665. perror("chmod[ctrl_interface/ifname]");
  1666. goto fail;
  1667. }
  1668. os_free(fname);
  1669. interface->global_ctrl_sock = s;
  1670. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1671. interface, NULL);
  1672. return 0;
  1673. fail:
  1674. if (s >= 0)
  1675. close(s);
  1676. if (fname) {
  1677. unlink(fname);
  1678. os_free(fname);
  1679. }
  1680. return -1;
  1681. }
  1682. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1683. {
  1684. char *fname = NULL;
  1685. if (interfaces->global_ctrl_sock > -1) {
  1686. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1687. close(interfaces->global_ctrl_sock);
  1688. interfaces->global_ctrl_sock = -1;
  1689. fname = hostapd_global_ctrl_iface_path(interfaces);
  1690. if (fname) {
  1691. unlink(fname);
  1692. os_free(fname);
  1693. }
  1694. if (interfaces->global_iface_path &&
  1695. rmdir(interfaces->global_iface_path) < 0) {
  1696. if (errno == ENOTEMPTY) {
  1697. wpa_printf(MSG_DEBUG, "Control interface "
  1698. "directory not empty - leaving it "
  1699. "behind");
  1700. } else {
  1701. wpa_printf(MSG_ERROR,
  1702. "rmdir[ctrl_interface=%s]: %s",
  1703. interfaces->global_iface_path,
  1704. strerror(errno));
  1705. }
  1706. }
  1707. os_free(interfaces->global_iface_path);
  1708. interfaces->global_iface_path = NULL;
  1709. }
  1710. }
  1711. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1712. const char *buf, size_t len)
  1713. {
  1714. struct wpa_ctrl_dst *dst, *next;
  1715. struct msghdr msg;
  1716. int idx;
  1717. struct iovec io[2];
  1718. char levelstr[10];
  1719. dst = hapd->ctrl_dst;
  1720. if (hapd->ctrl_sock < 0 || dst == NULL)
  1721. return;
  1722. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1723. io[0].iov_base = levelstr;
  1724. io[0].iov_len = os_strlen(levelstr);
  1725. io[1].iov_base = (char *) buf;
  1726. io[1].iov_len = len;
  1727. os_memset(&msg, 0, sizeof(msg));
  1728. msg.msg_iov = io;
  1729. msg.msg_iovlen = 2;
  1730. idx = 0;
  1731. while (dst) {
  1732. next = dst->next;
  1733. if (level >= dst->debug_level) {
  1734. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1735. (u8 *) dst->addr.sun_path, dst->addrlen -
  1736. offsetof(struct sockaddr_un, sun_path));
  1737. msg.msg_name = &dst->addr;
  1738. msg.msg_namelen = dst->addrlen;
  1739. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1740. int _errno = errno;
  1741. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1742. "%d - %s",
  1743. idx, errno, strerror(errno));
  1744. dst->errors++;
  1745. if (dst->errors > 10 || _errno == ENOENT) {
  1746. hostapd_ctrl_iface_detach(
  1747. hapd, &dst->addr,
  1748. dst->addrlen);
  1749. }
  1750. } else
  1751. dst->errors = 0;
  1752. }
  1753. idx++;
  1754. dst = next;
  1755. }
  1756. }
  1757. #endif /* CONFIG_NATIVE_WINDOWS */