ctrl_iface.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2012, 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 "ap/hostapd.h"
  20. #include "ap/ap_config.h"
  21. #include "ap/ieee802_1x.h"
  22. #include "ap/wpa_auth.h"
  23. #include "ap/ieee802_11.h"
  24. #include "ap/sta_info.h"
  25. #include "ap/wps_hostapd.h"
  26. #include "ap/ctrl_iface_ap.h"
  27. #include "ap/ap_drv_ops.h"
  28. #include "ap/wpa_auth.h"
  29. #include "wps/wps_defs.h"
  30. #include "wps/wps.h"
  31. #include "config_file.h"
  32. #include "ctrl_iface.h"
  33. struct wpa_ctrl_dst {
  34. struct wpa_ctrl_dst *next;
  35. struct sockaddr_un addr;
  36. socklen_t addrlen;
  37. int debug_level;
  38. int errors;
  39. };
  40. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  41. const char *buf, size_t len);
  42. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  43. struct sockaddr_un *from,
  44. socklen_t fromlen)
  45. {
  46. struct wpa_ctrl_dst *dst;
  47. dst = os_zalloc(sizeof(*dst));
  48. if (dst == NULL)
  49. return -1;
  50. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  51. dst->addrlen = fromlen;
  52. dst->debug_level = MSG_INFO;
  53. dst->next = hapd->ctrl_dst;
  54. hapd->ctrl_dst = dst;
  55. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  56. (u8 *) from->sun_path,
  57. fromlen - offsetof(struct sockaddr_un, sun_path));
  58. return 0;
  59. }
  60. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  61. struct sockaddr_un *from,
  62. socklen_t fromlen)
  63. {
  64. struct wpa_ctrl_dst *dst, *prev = NULL;
  65. dst = hapd->ctrl_dst;
  66. while (dst) {
  67. if (fromlen == dst->addrlen &&
  68. os_memcmp(from->sun_path, dst->addr.sun_path,
  69. fromlen - offsetof(struct sockaddr_un, sun_path))
  70. == 0) {
  71. if (prev == NULL)
  72. hapd->ctrl_dst = dst->next;
  73. else
  74. prev->next = dst->next;
  75. os_free(dst);
  76. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  77. (u8 *) from->sun_path,
  78. fromlen -
  79. offsetof(struct sockaddr_un, sun_path));
  80. return 0;
  81. }
  82. prev = dst;
  83. dst = dst->next;
  84. }
  85. return -1;
  86. }
  87. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  88. struct sockaddr_un *from,
  89. socklen_t fromlen,
  90. char *level)
  91. {
  92. struct wpa_ctrl_dst *dst;
  93. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  94. dst = hapd->ctrl_dst;
  95. while (dst) {
  96. if (fromlen == dst->addrlen &&
  97. os_memcmp(from->sun_path, dst->addr.sun_path,
  98. fromlen - offsetof(struct sockaddr_un, sun_path))
  99. == 0) {
  100. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  101. "level", (u8 *) from->sun_path, fromlen -
  102. offsetof(struct sockaddr_un, sun_path));
  103. dst->debug_level = atoi(level);
  104. return 0;
  105. }
  106. dst = dst->next;
  107. }
  108. return -1;
  109. }
  110. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  111. const char *txtaddr)
  112. {
  113. u8 addr[ETH_ALEN];
  114. struct sta_info *sta;
  115. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  116. if (hwaddr_aton(txtaddr, addr))
  117. return -1;
  118. sta = ap_get_sta(hapd, addr);
  119. if (sta)
  120. return 0;
  121. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  122. "notification", MAC2STR(addr));
  123. sta = ap_sta_add(hapd, addr);
  124. if (sta == NULL)
  125. return -1;
  126. hostapd_new_assoc_sta(hapd, sta, 0);
  127. return 0;
  128. }
  129. #ifdef CONFIG_IEEE80211W
  130. #ifdef NEED_AP_MLME
  131. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  132. const char *txtaddr)
  133. {
  134. u8 addr[ETH_ALEN];
  135. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  136. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  137. if (hwaddr_aton(txtaddr, addr) ||
  138. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  139. return -1;
  140. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  141. return 0;
  142. }
  143. #endif /* NEED_AP_MLME */
  144. #endif /* CONFIG_IEEE80211W */
  145. #ifdef CONFIG_WPS
  146. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  147. {
  148. char *pin = os_strchr(txt, ' ');
  149. char *timeout_txt;
  150. int timeout;
  151. u8 addr_buf[ETH_ALEN], *addr = NULL;
  152. char *pos;
  153. if (pin == NULL)
  154. return -1;
  155. *pin++ = '\0';
  156. timeout_txt = os_strchr(pin, ' ');
  157. if (timeout_txt) {
  158. *timeout_txt++ = '\0';
  159. timeout = atoi(timeout_txt);
  160. pos = os_strchr(timeout_txt, ' ');
  161. if (pos) {
  162. *pos++ = '\0';
  163. if (hwaddr_aton(pos, addr_buf) == 0)
  164. addr = addr_buf;
  165. }
  166. } else
  167. timeout = 0;
  168. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  169. }
  170. static int hostapd_ctrl_iface_wps_check_pin(
  171. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  172. {
  173. char pin[9];
  174. size_t len;
  175. char *pos;
  176. int ret;
  177. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  178. (u8 *) cmd, os_strlen(cmd));
  179. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  180. if (*pos < '0' || *pos > '9')
  181. continue;
  182. pin[len++] = *pos;
  183. if (len == 9) {
  184. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  185. return -1;
  186. }
  187. }
  188. if (len != 4 && len != 8) {
  189. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  190. return -1;
  191. }
  192. pin[len] = '\0';
  193. if (len == 8) {
  194. unsigned int pin_val;
  195. pin_val = atoi(pin);
  196. if (!wps_pin_valid(pin_val)) {
  197. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  198. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  199. if (ret < 0 || (size_t) ret >= buflen)
  200. return -1;
  201. return ret;
  202. }
  203. }
  204. ret = os_snprintf(buf, buflen, "%s", pin);
  205. if (ret < 0 || (size_t) ret >= buflen)
  206. return -1;
  207. return ret;
  208. }
  209. #ifdef CONFIG_WPS_NFC
  210. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  211. char *pos)
  212. {
  213. size_t len;
  214. struct wpabuf *buf;
  215. int ret;
  216. len = os_strlen(pos);
  217. if (len & 0x01)
  218. return -1;
  219. len /= 2;
  220. buf = wpabuf_alloc(len);
  221. if (buf == NULL)
  222. return -1;
  223. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  224. wpabuf_free(buf);
  225. return -1;
  226. }
  227. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  228. wpabuf_free(buf);
  229. return ret;
  230. }
  231. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  232. char *cmd, char *reply,
  233. size_t max_len)
  234. {
  235. int ndef;
  236. struct wpabuf *buf;
  237. int res;
  238. if (os_strcmp(cmd, "WPS") == 0)
  239. ndef = 0;
  240. else if (os_strcmp(cmd, "NDEF") == 0)
  241. ndef = 1;
  242. else
  243. return -1;
  244. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  245. if (buf == NULL)
  246. return -1;
  247. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  248. wpabuf_len(buf));
  249. reply[res++] = '\n';
  250. reply[res] = '\0';
  251. wpabuf_free(buf);
  252. return res;
  253. }
  254. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  255. char *reply, size_t max_len,
  256. int ndef)
  257. {
  258. struct wpabuf *buf;
  259. int res;
  260. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  261. if (buf == NULL)
  262. return -1;
  263. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  264. wpabuf_len(buf));
  265. reply[res++] = '\n';
  266. reply[res] = '\0';
  267. wpabuf_free(buf);
  268. return res;
  269. }
  270. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  271. char *cmd, char *reply,
  272. size_t max_len)
  273. {
  274. if (os_strcmp(cmd, "WPS") == 0)
  275. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  276. max_len, 0);
  277. if (os_strcmp(cmd, "NDEF") == 0)
  278. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  279. max_len, 1);
  280. if (os_strcmp(cmd, "enable") == 0)
  281. return hostapd_wps_nfc_token_enable(hapd);
  282. if (os_strcmp(cmd, "disable") == 0) {
  283. hostapd_wps_nfc_token_disable(hapd);
  284. return 0;
  285. }
  286. return -1;
  287. }
  288. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  289. char *cmd, char *reply,
  290. size_t max_len)
  291. {
  292. struct wpabuf *buf;
  293. int res;
  294. char *pos;
  295. int ndef;
  296. pos = os_strchr(cmd, ' ');
  297. if (pos == NULL)
  298. return -1;
  299. *pos++ = '\0';
  300. if (os_strcmp(cmd, "WPS") == 0)
  301. ndef = 0;
  302. else if (os_strcmp(cmd, "NDEF") == 0)
  303. ndef = 1;
  304. else
  305. return -1;
  306. if (os_strcmp(pos, "WPS-CR") == 0)
  307. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  308. else
  309. buf = NULL;
  310. if (buf == NULL)
  311. return -1;
  312. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  313. wpabuf_len(buf));
  314. reply[res++] = '\n';
  315. reply[res] = '\0';
  316. wpabuf_free(buf);
  317. return res;
  318. }
  319. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  320. char *cmd)
  321. {
  322. /*
  323. * Since NFC connection handover provided full WPS Credential, there is
  324. * no need for additional operations within hostapd. Just report this in
  325. * debug log.
  326. */
  327. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported: %s", cmd);
  328. return 0;
  329. }
  330. #endif /* CONFIG_WPS_NFC */
  331. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  332. char *buf, size_t buflen)
  333. {
  334. int timeout = 300;
  335. char *pos;
  336. const char *pin_txt;
  337. pos = os_strchr(txt, ' ');
  338. if (pos)
  339. *pos++ = '\0';
  340. if (os_strcmp(txt, "disable") == 0) {
  341. hostapd_wps_ap_pin_disable(hapd);
  342. return os_snprintf(buf, buflen, "OK\n");
  343. }
  344. if (os_strcmp(txt, "random") == 0) {
  345. if (pos)
  346. timeout = atoi(pos);
  347. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  348. if (pin_txt == NULL)
  349. return -1;
  350. return os_snprintf(buf, buflen, "%s", pin_txt);
  351. }
  352. if (os_strcmp(txt, "get") == 0) {
  353. pin_txt = hostapd_wps_ap_pin_get(hapd);
  354. if (pin_txt == NULL)
  355. return -1;
  356. return os_snprintf(buf, buflen, "%s", pin_txt);
  357. }
  358. if (os_strcmp(txt, "set") == 0) {
  359. char *pin;
  360. if (pos == NULL)
  361. return -1;
  362. pin = pos;
  363. pos = os_strchr(pos, ' ');
  364. if (pos) {
  365. *pos++ = '\0';
  366. timeout = atoi(pos);
  367. }
  368. if (os_strlen(pin) > buflen)
  369. return -1;
  370. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  371. return -1;
  372. return os_snprintf(buf, buflen, "%s", pin);
  373. }
  374. return -1;
  375. }
  376. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  377. {
  378. char *pos;
  379. char *ssid, *auth, *encr = NULL, *key = NULL;
  380. ssid = txt;
  381. pos = os_strchr(txt, ' ');
  382. if (!pos)
  383. return -1;
  384. *pos++ = '\0';
  385. auth = pos;
  386. pos = os_strchr(pos, ' ');
  387. if (pos) {
  388. *pos++ = '\0';
  389. encr = pos;
  390. pos = os_strchr(pos, ' ');
  391. if (pos) {
  392. *pos++ = '\0';
  393. key = pos;
  394. }
  395. }
  396. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  397. }
  398. static const char * pbc_status_str(enum pbc_status status)
  399. {
  400. switch (status) {
  401. case WPS_PBC_STATUS_DISABLE:
  402. return "Disabled";
  403. case WPS_PBC_STATUS_ACTIVE:
  404. return "Active";
  405. case WPS_PBC_STATUS_TIMEOUT:
  406. return "Timed-out";
  407. case WPS_PBC_STATUS_OVERLAP:
  408. return "Overlap";
  409. default:
  410. return "Unknown";
  411. }
  412. }
  413. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  414. char *buf, size_t buflen)
  415. {
  416. int ret;
  417. char *pos, *end;
  418. pos = buf;
  419. end = buf + buflen;
  420. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  421. pbc_status_str(hapd->wps_stats.pbc_status));
  422. if (ret < 0 || ret >= end - pos)
  423. return pos - buf;
  424. pos += ret;
  425. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  426. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  427. "Success":
  428. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  429. "Failed" : "None")));
  430. if (ret < 0 || ret >= end - pos)
  431. return pos - buf;
  432. pos += ret;
  433. /* If status == Failure - Add possible Reasons */
  434. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  435. hapd->wps_stats.failure_reason > 0) {
  436. ret = os_snprintf(pos, end - pos,
  437. "Failure Reason: %s\n",
  438. wps_ei_str(hapd->wps_stats.failure_reason));
  439. if (ret < 0 || ret >= end - pos)
  440. return pos - buf;
  441. pos += ret;
  442. }
  443. if (hapd->wps_stats.status) {
  444. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  445. MAC2STR(hapd->wps_stats.peer_addr));
  446. if (ret < 0 || ret >= end - pos)
  447. return pos - buf;
  448. pos += ret;
  449. }
  450. return pos - buf;
  451. }
  452. #endif /* CONFIG_WPS */
  453. #ifdef CONFIG_WNM
  454. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  455. const char *cmd)
  456. {
  457. u8 addr[ETH_ALEN];
  458. u8 buf[1000], *pos;
  459. struct ieee80211_mgmt *mgmt;
  460. int disassoc_timer;
  461. if (hwaddr_aton(cmd, addr))
  462. return -1;
  463. if (cmd[17] != ' ')
  464. return -1;
  465. disassoc_timer = atoi(cmd + 17);
  466. os_memset(buf, 0, sizeof(buf));
  467. mgmt = (struct ieee80211_mgmt *) buf;
  468. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  469. WLAN_FC_STYPE_ACTION);
  470. os_memcpy(mgmt->da, addr, ETH_ALEN);
  471. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  472. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  473. mgmt->u.action.category = WLAN_ACTION_WNM;
  474. mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
  475. mgmt->u.action.u.bss_tm_req.dialog_token = 1;
  476. mgmt->u.action.u.bss_tm_req.req_mode =
  477. WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
  478. mgmt->u.action.u.bss_tm_req.disassoc_timer =
  479. host_to_le16(disassoc_timer);
  480. mgmt->u.action.u.bss_tm_req.validity_interval = 0;
  481. pos = mgmt->u.action.u.bss_tm_req.variable;
  482. if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
  483. wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
  484. "Management Request frame");
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  490. const char *cmd)
  491. {
  492. u8 addr[ETH_ALEN];
  493. const char *url, *timerstr;
  494. u8 buf[1000], *pos;
  495. struct ieee80211_mgmt *mgmt;
  496. size_t url_len;
  497. int disassoc_timer;
  498. if (hwaddr_aton(cmd, addr))
  499. return -1;
  500. timerstr = cmd + 17;
  501. if (*timerstr != ' ')
  502. return -1;
  503. timerstr++;
  504. disassoc_timer = atoi(timerstr);
  505. if (disassoc_timer < 0 || disassoc_timer > 65535)
  506. return -1;
  507. url = os_strchr(timerstr, ' ');
  508. if (url == NULL)
  509. return -1;
  510. url++;
  511. url_len = os_strlen(url);
  512. if (url_len > 255)
  513. return -1;
  514. os_memset(buf, 0, sizeof(buf));
  515. mgmt = (struct ieee80211_mgmt *) buf;
  516. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  517. WLAN_FC_STYPE_ACTION);
  518. os_memcpy(mgmt->da, addr, ETH_ALEN);
  519. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  520. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  521. mgmt->u.action.category = WLAN_ACTION_WNM;
  522. mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
  523. mgmt->u.action.u.bss_tm_req.dialog_token = 1;
  524. mgmt->u.action.u.bss_tm_req.req_mode =
  525. WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
  526. mgmt->u.action.u.bss_tm_req.disassoc_timer =
  527. host_to_le16(disassoc_timer);
  528. mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
  529. pos = mgmt->u.action.u.bss_tm_req.variable;
  530. /* Session Information URL */
  531. *pos++ = url_len;
  532. os_memcpy(pos, url, url_len);
  533. pos += url_len;
  534. if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
  535. wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
  536. "Management Request frame");
  537. return -1;
  538. }
  539. /* send disassociation frame after time-out */
  540. if (disassoc_timer) {
  541. struct sta_info *sta;
  542. int timeout, beacon_int;
  543. /*
  544. * Prevent STA from reconnecting using cached PMKSA to force
  545. * full authentication with the authentication server (which may
  546. * decide to reject the connection),
  547. */
  548. wpa_auth_pmksa_remove(hapd->wpa_auth, addr);
  549. sta = ap_get_sta(hapd, addr);
  550. if (sta == NULL) {
  551. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  552. "for ESS disassociation imminent message",
  553. MAC2STR(addr));
  554. return -1;
  555. }
  556. beacon_int = hapd->iconf->beacon_int;
  557. if (beacon_int < 1)
  558. beacon_int = 100; /* best guess */
  559. /* Calculate timeout in ms based on beacon_int in TU */
  560. timeout = disassoc_timer * beacon_int * 128 / 125;
  561. wpa_printf(MSG_DEBUG, "Disassociation timer for " MACSTR
  562. " set to %d ms", MAC2STR(addr), timeout);
  563. sta->timeout_next = STA_DISASSOC_FROM_CLI;
  564. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  565. eloop_register_timeout(timeout / 1000,
  566. timeout % 1000 * 1000,
  567. ap_handle_timer, hapd, sta);
  568. }
  569. return 0;
  570. }
  571. #endif /* CONFIG_WNM */
  572. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  573. char *buf, size_t buflen)
  574. {
  575. int ret;
  576. char *pos, *end;
  577. pos = buf;
  578. end = buf + buflen;
  579. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  580. "ssid=%s\n",
  581. MAC2STR(hapd->own_addr),
  582. wpa_ssid_txt(hapd->conf->ssid.ssid,
  583. hapd->conf->ssid.ssid_len));
  584. if (ret < 0 || ret >= end - pos)
  585. return pos - buf;
  586. pos += ret;
  587. #ifdef CONFIG_WPS
  588. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  589. hapd->conf->wps_state == 0 ? "disabled" :
  590. (hapd->conf->wps_state == 1 ? "not configured" :
  591. "configured"));
  592. if (ret < 0 || ret >= end - pos)
  593. return pos - buf;
  594. pos += ret;
  595. if (hapd->conf->wps_state && hapd->conf->wpa &&
  596. hapd->conf->ssid.wpa_passphrase) {
  597. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  598. hapd->conf->ssid.wpa_passphrase);
  599. if (ret < 0 || ret >= end - pos)
  600. return pos - buf;
  601. pos += ret;
  602. }
  603. if (hapd->conf->wps_state && hapd->conf->wpa &&
  604. hapd->conf->ssid.wpa_psk &&
  605. hapd->conf->ssid.wpa_psk->group) {
  606. char hex[PMK_LEN * 2 + 1];
  607. wpa_snprintf_hex(hex, sizeof(hex),
  608. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  609. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  610. if (ret < 0 || ret >= end - pos)
  611. return pos - buf;
  612. pos += ret;
  613. }
  614. #endif /* CONFIG_WPS */
  615. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  616. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  617. if (ret < 0 || ret >= end - pos)
  618. return pos - buf;
  619. pos += ret;
  620. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  621. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  622. if (ret < 0 || ret >= end - pos)
  623. return pos - buf;
  624. pos += ret;
  625. }
  626. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  627. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  628. if (ret < 0 || ret >= end - pos)
  629. return pos - buf;
  630. pos += ret;
  631. }
  632. #ifdef CONFIG_IEEE80211R
  633. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  634. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  635. if (ret < 0 || ret >= end - pos)
  636. return pos - buf;
  637. pos += ret;
  638. }
  639. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  640. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  641. if (ret < 0 || ret >= end - pos)
  642. return pos - buf;
  643. pos += ret;
  644. }
  645. #endif /* CONFIG_IEEE80211R */
  646. #ifdef CONFIG_IEEE80211W
  647. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  648. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  649. if (ret < 0 || ret >= end - pos)
  650. return pos - buf;
  651. pos += ret;
  652. }
  653. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  654. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  655. if (ret < 0 || ret >= end - pos)
  656. return pos - buf;
  657. pos += ret;
  658. }
  659. #endif /* CONFIG_IEEE80211W */
  660. ret = os_snprintf(pos, end - pos, "\n");
  661. if (ret < 0 || ret >= end - pos)
  662. return pos - buf;
  663. pos += ret;
  664. }
  665. if (hapd->conf->wpa) {
  666. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  667. wpa_cipher_txt(hapd->conf->wpa_group));
  668. if (ret < 0 || ret >= end - pos)
  669. return pos - buf;
  670. pos += ret;
  671. }
  672. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  673. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  674. if (ret < 0 || ret >= end - pos)
  675. return pos - buf;
  676. pos += ret;
  677. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  678. " ");
  679. if (ret < 0)
  680. return pos - buf;
  681. pos += ret;
  682. ret = os_snprintf(pos, end - pos, "\n");
  683. if (ret < 0 || ret >= end - pos)
  684. return pos - buf;
  685. pos += ret;
  686. }
  687. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  688. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  689. if (ret < 0 || ret >= end - pos)
  690. return pos - buf;
  691. pos += ret;
  692. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  693. " ");
  694. if (ret < 0)
  695. return pos - buf;
  696. pos += ret;
  697. ret = os_snprintf(pos, end - pos, "\n");
  698. if (ret < 0 || ret >= end - pos)
  699. return pos - buf;
  700. pos += ret;
  701. }
  702. return pos - buf;
  703. }
  704. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  705. {
  706. char *value;
  707. int ret = 0;
  708. value = os_strchr(cmd, ' ');
  709. if (value == NULL)
  710. return -1;
  711. *value++ = '\0';
  712. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  713. if (0) {
  714. #ifdef CONFIG_WPS_TESTING
  715. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  716. long int val;
  717. val = strtol(value, NULL, 0);
  718. if (val < 0 || val > 0xff) {
  719. ret = -1;
  720. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  721. "wps_version_number %ld", val);
  722. } else {
  723. wps_version_number = val;
  724. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  725. "version %u.%u",
  726. (wps_version_number & 0xf0) >> 4,
  727. wps_version_number & 0x0f);
  728. hostapd_wps_update_ie(hapd);
  729. }
  730. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  731. wps_testing_dummy_cred = atoi(value);
  732. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  733. wps_testing_dummy_cred);
  734. #endif /* CONFIG_WPS_TESTING */
  735. #ifdef CONFIG_INTERWORKING
  736. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  737. int val = atoi(value);
  738. if (val <= 0)
  739. ret = -1;
  740. else
  741. hapd->gas_frag_limit = val;
  742. #endif /* CONFIG_INTERWORKING */
  743. } else {
  744. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  745. }
  746. return ret;
  747. }
  748. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  749. char *buf, size_t buflen)
  750. {
  751. int res;
  752. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  753. if (os_strcmp(cmd, "version") == 0) {
  754. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  755. if (res < 0 || (unsigned int) res >= buflen)
  756. return -1;
  757. return res;
  758. }
  759. return -1;
  760. }
  761. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  762. {
  763. if (hostapd_enable_iface(iface) < 0) {
  764. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  765. return -1;
  766. }
  767. return 0;
  768. }
  769. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  770. {
  771. if (hostapd_reload_iface(iface) < 0) {
  772. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  773. return -1;
  774. }
  775. return 0;
  776. }
  777. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  778. {
  779. if (hostapd_disable_iface(iface) < 0) {
  780. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  781. return -1;
  782. }
  783. return 0;
  784. }
  785. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  786. void *sock_ctx)
  787. {
  788. struct hostapd_data *hapd = eloop_ctx;
  789. char buf[256];
  790. int res;
  791. struct sockaddr_un from;
  792. socklen_t fromlen = sizeof(from);
  793. char *reply;
  794. const int reply_size = 4096;
  795. int reply_len;
  796. int level = MSG_DEBUG;
  797. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  798. (struct sockaddr *) &from, &fromlen);
  799. if (res < 0) {
  800. perror("recvfrom(ctrl_iface)");
  801. return;
  802. }
  803. buf[res] = '\0';
  804. if (os_strcmp(buf, "PING") == 0)
  805. level = MSG_EXCESSIVE;
  806. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  807. reply = os_malloc(reply_size);
  808. if (reply == NULL) {
  809. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  810. fromlen);
  811. return;
  812. }
  813. os_memcpy(reply, "OK\n", 3);
  814. reply_len = 3;
  815. if (os_strcmp(buf, "PING") == 0) {
  816. os_memcpy(reply, "PONG\n", 5);
  817. reply_len = 5;
  818. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  819. if (wpa_debug_reopen_file() < 0)
  820. reply_len = -1;
  821. } else if (os_strcmp(buf, "MIB") == 0) {
  822. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  823. if (reply_len >= 0) {
  824. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  825. reply_size - reply_len);
  826. if (res < 0)
  827. reply_len = -1;
  828. else
  829. reply_len += res;
  830. }
  831. if (reply_len >= 0) {
  832. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  833. reply_size - reply_len);
  834. if (res < 0)
  835. reply_len = -1;
  836. else
  837. reply_len += res;
  838. }
  839. #ifndef CONFIG_NO_RADIUS
  840. if (reply_len >= 0) {
  841. res = radius_client_get_mib(hapd->radius,
  842. reply + reply_len,
  843. reply_size - reply_len);
  844. if (res < 0)
  845. reply_len = -1;
  846. else
  847. reply_len += res;
  848. }
  849. #endif /* CONFIG_NO_RADIUS */
  850. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  851. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  852. reply_size);
  853. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  854. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  855. reply_size);
  856. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  857. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  858. reply_size);
  859. } else if (os_strcmp(buf, "ATTACH") == 0) {
  860. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  861. reply_len = -1;
  862. } else if (os_strcmp(buf, "DETACH") == 0) {
  863. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  864. reply_len = -1;
  865. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  866. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  867. buf + 6))
  868. reply_len = -1;
  869. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  870. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  871. reply_len = -1;
  872. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  873. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  874. reply_len = -1;
  875. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  876. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  877. reply_len = -1;
  878. #ifdef CONFIG_IEEE80211W
  879. #ifdef NEED_AP_MLME
  880. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  881. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  882. reply_len = -1;
  883. #endif /* NEED_AP_MLME */
  884. #endif /* CONFIG_IEEE80211W */
  885. #ifdef CONFIG_WPS
  886. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  887. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  888. reply_len = -1;
  889. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  890. reply_len = hostapd_ctrl_iface_wps_check_pin(
  891. hapd, buf + 14, reply, reply_size);
  892. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  893. if (hostapd_wps_button_pushed(hapd, NULL))
  894. reply_len = -1;
  895. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  896. if (hostapd_wps_cancel(hapd))
  897. reply_len = -1;
  898. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  899. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  900. reply, reply_size);
  901. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  902. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  903. reply_len = -1;
  904. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  905. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  906. reply_size);
  907. #ifdef CONFIG_WPS_NFC
  908. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  909. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  910. reply_len = -1;
  911. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  912. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  913. hapd, buf + 21, reply, reply_size);
  914. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  915. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  916. hapd, buf + 14, reply, reply_size);
  917. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  918. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  919. hapd, buf + 21, reply, reply_size);
  920. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  921. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  922. reply_len = -1;
  923. #endif /* CONFIG_WPS_NFC */
  924. #endif /* CONFIG_WPS */
  925. #ifdef CONFIG_WNM
  926. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  927. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  928. reply_len = -1;
  929. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  930. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  931. reply_len = -1;
  932. #endif /* CONFIG_WNM */
  933. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  934. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  935. reply_size);
  936. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  937. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  938. reply_len = -1;
  939. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  940. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  941. reply_size);
  942. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  943. if (hostapd_ctrl_iface_enable(hapd->iface))
  944. reply_len = -1;
  945. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  946. if (hostapd_ctrl_iface_reload(hapd->iface))
  947. reply_len = -1;
  948. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  949. if (hostapd_ctrl_iface_disable(hapd->iface))
  950. reply_len = -1;
  951. } else {
  952. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  953. reply_len = 16;
  954. }
  955. if (reply_len < 0) {
  956. os_memcpy(reply, "FAIL\n", 5);
  957. reply_len = 5;
  958. }
  959. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  960. os_free(reply);
  961. }
  962. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  963. {
  964. char *buf;
  965. size_t len;
  966. if (hapd->conf->ctrl_interface == NULL)
  967. return NULL;
  968. len = os_strlen(hapd->conf->ctrl_interface) +
  969. os_strlen(hapd->conf->iface) + 2;
  970. buf = os_malloc(len);
  971. if (buf == NULL)
  972. return NULL;
  973. os_snprintf(buf, len, "%s/%s",
  974. hapd->conf->ctrl_interface, hapd->conf->iface);
  975. buf[len - 1] = '\0';
  976. return buf;
  977. }
  978. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
  979. const char *txt, size_t len)
  980. {
  981. struct hostapd_data *hapd = ctx;
  982. if (hapd == NULL)
  983. return;
  984. hostapd_ctrl_iface_send(hapd, level, txt, len);
  985. }
  986. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  987. {
  988. struct sockaddr_un addr;
  989. int s = -1;
  990. char *fname = NULL;
  991. if (hapd->ctrl_sock > -1) {
  992. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  993. return 0;
  994. }
  995. if (hapd->conf->ctrl_interface == NULL)
  996. return 0;
  997. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  998. if (errno == EEXIST) {
  999. wpa_printf(MSG_DEBUG, "Using existing control "
  1000. "interface directory.");
  1001. } else {
  1002. perror("mkdir[ctrl_interface]");
  1003. goto fail;
  1004. }
  1005. }
  1006. if (hapd->conf->ctrl_interface_gid_set &&
  1007. chown(hapd->conf->ctrl_interface, -1,
  1008. hapd->conf->ctrl_interface_gid) < 0) {
  1009. perror("chown[ctrl_interface]");
  1010. return -1;
  1011. }
  1012. if (!hapd->conf->ctrl_interface_gid_set &&
  1013. hapd->iface->interfaces->ctrl_iface_group &&
  1014. chown(hapd->conf->ctrl_interface, -1,
  1015. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1016. perror("chown[ctrl_interface]");
  1017. return -1;
  1018. }
  1019. #ifdef ANDROID
  1020. /*
  1021. * Android is using umask 0077 which would leave the control interface
  1022. * directory without group access. This breaks things since Wi-Fi
  1023. * framework assumes that this directory can be accessed by other
  1024. * applications in the wifi group. Fix this by adding group access even
  1025. * if umask value would prevent this.
  1026. */
  1027. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1028. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  1029. strerror(errno));
  1030. /* Try to continue anyway */
  1031. }
  1032. #endif /* ANDROID */
  1033. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  1034. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  1035. goto fail;
  1036. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1037. if (s < 0) {
  1038. perror("socket(PF_UNIX)");
  1039. goto fail;
  1040. }
  1041. os_memset(&addr, 0, sizeof(addr));
  1042. #ifdef __FreeBSD__
  1043. addr.sun_len = sizeof(addr);
  1044. #endif /* __FreeBSD__ */
  1045. addr.sun_family = AF_UNIX;
  1046. fname = hostapd_ctrl_iface_path(hapd);
  1047. if (fname == NULL)
  1048. goto fail;
  1049. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1050. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1051. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1052. strerror(errno));
  1053. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1054. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1055. " allow connections - assuming it was left"
  1056. "over from forced program termination");
  1057. if (unlink(fname) < 0) {
  1058. perror("unlink[ctrl_iface]");
  1059. wpa_printf(MSG_ERROR, "Could not unlink "
  1060. "existing ctrl_iface socket '%s'",
  1061. fname);
  1062. goto fail;
  1063. }
  1064. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1065. 0) {
  1066. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  1067. goto fail;
  1068. }
  1069. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1070. "ctrl_iface socket '%s'", fname);
  1071. } else {
  1072. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1073. "be in use - cannot override it");
  1074. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1075. "not used anymore", fname);
  1076. os_free(fname);
  1077. fname = NULL;
  1078. goto fail;
  1079. }
  1080. }
  1081. if (hapd->conf->ctrl_interface_gid_set &&
  1082. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  1083. perror("chown[ctrl_interface/ifname]");
  1084. goto fail;
  1085. }
  1086. if (!hapd->conf->ctrl_interface_gid_set &&
  1087. hapd->iface->interfaces->ctrl_iface_group &&
  1088. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1089. perror("chown[ctrl_interface/ifname]");
  1090. goto fail;
  1091. }
  1092. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1093. perror("chmod[ctrl_interface/ifname]");
  1094. goto fail;
  1095. }
  1096. os_free(fname);
  1097. hapd->ctrl_sock = s;
  1098. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  1099. NULL);
  1100. hapd->msg_ctx = hapd;
  1101. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  1102. return 0;
  1103. fail:
  1104. if (s >= 0)
  1105. close(s);
  1106. if (fname) {
  1107. unlink(fname);
  1108. os_free(fname);
  1109. }
  1110. return -1;
  1111. }
  1112. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  1113. {
  1114. struct wpa_ctrl_dst *dst, *prev;
  1115. if (hapd->ctrl_sock > -1) {
  1116. char *fname;
  1117. eloop_unregister_read_sock(hapd->ctrl_sock);
  1118. close(hapd->ctrl_sock);
  1119. hapd->ctrl_sock = -1;
  1120. fname = hostapd_ctrl_iface_path(hapd);
  1121. if (fname)
  1122. unlink(fname);
  1123. os_free(fname);
  1124. if (hapd->conf->ctrl_interface &&
  1125. rmdir(hapd->conf->ctrl_interface) < 0) {
  1126. if (errno == ENOTEMPTY) {
  1127. wpa_printf(MSG_DEBUG, "Control interface "
  1128. "directory not empty - leaving it "
  1129. "behind");
  1130. } else {
  1131. wpa_printf(MSG_ERROR,
  1132. "rmdir[ctrl_interface=%s]: %s",
  1133. hapd->conf->ctrl_interface,
  1134. strerror(errno));
  1135. }
  1136. }
  1137. }
  1138. dst = hapd->ctrl_dst;
  1139. while (dst) {
  1140. prev = dst;
  1141. dst = dst->next;
  1142. os_free(prev);
  1143. }
  1144. }
  1145. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1146. char *buf)
  1147. {
  1148. if (hostapd_add_iface(interfaces, buf) < 0) {
  1149. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1150. return -1;
  1151. }
  1152. return 0;
  1153. }
  1154. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1155. char *buf)
  1156. {
  1157. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1158. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1159. return -1;
  1160. }
  1161. return 0;
  1162. }
  1163. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1164. void *sock_ctx)
  1165. {
  1166. void *interfaces = eloop_ctx;
  1167. char buf[256];
  1168. int res;
  1169. struct sockaddr_un from;
  1170. socklen_t fromlen = sizeof(from);
  1171. char reply[24];
  1172. int reply_len;
  1173. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1174. (struct sockaddr *) &from, &fromlen);
  1175. if (res < 0) {
  1176. perror("recvfrom(ctrl_iface)");
  1177. return;
  1178. }
  1179. buf[res] = '\0';
  1180. os_memcpy(reply, "OK\n", 3);
  1181. reply_len = 3;
  1182. if (os_strcmp(buf, "PING") == 0) {
  1183. os_memcpy(reply, "PONG\n", 5);
  1184. reply_len = 5;
  1185. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1186. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1187. reply_len = -1;
  1188. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1189. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1190. reply_len = -1;
  1191. } else {
  1192. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1193. "ignored");
  1194. reply_len = -1;
  1195. }
  1196. if (reply_len < 0) {
  1197. os_memcpy(reply, "FAIL\n", 5);
  1198. reply_len = 5;
  1199. }
  1200. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1201. }
  1202. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1203. {
  1204. char *buf;
  1205. size_t len;
  1206. if (interface->global_iface_path == NULL)
  1207. return NULL;
  1208. len = os_strlen(interface->global_iface_path) +
  1209. os_strlen(interface->global_iface_name) + 2;
  1210. buf = os_malloc(len);
  1211. if (buf == NULL)
  1212. return NULL;
  1213. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1214. interface->global_iface_name);
  1215. buf[len - 1] = '\0';
  1216. return buf;
  1217. }
  1218. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1219. {
  1220. struct sockaddr_un addr;
  1221. int s = -1;
  1222. char *fname = NULL;
  1223. if (interface->global_iface_path == NULL) {
  1224. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1225. return 0;
  1226. }
  1227. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1228. if (errno == EEXIST) {
  1229. wpa_printf(MSG_DEBUG, "Using existing control "
  1230. "interface directory.");
  1231. } else {
  1232. perror("mkdir[ctrl_interface]");
  1233. goto fail;
  1234. }
  1235. } else if (interface->ctrl_iface_group &&
  1236. chown(interface->global_iface_path, -1,
  1237. interface->ctrl_iface_group) < 0) {
  1238. perror("chown[ctrl_interface]");
  1239. goto fail;
  1240. }
  1241. if (os_strlen(interface->global_iface_path) + 1 +
  1242. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1243. goto fail;
  1244. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1245. if (s < 0) {
  1246. perror("socket(PF_UNIX)");
  1247. goto fail;
  1248. }
  1249. os_memset(&addr, 0, sizeof(addr));
  1250. #ifdef __FreeBSD__
  1251. addr.sun_len = sizeof(addr);
  1252. #endif /* __FreeBSD__ */
  1253. addr.sun_family = AF_UNIX;
  1254. fname = hostapd_global_ctrl_iface_path(interface);
  1255. if (fname == NULL)
  1256. goto fail;
  1257. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1258. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1259. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1260. strerror(errno));
  1261. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1262. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1263. " allow connections - assuming it was left"
  1264. "over from forced program termination");
  1265. if (unlink(fname) < 0) {
  1266. perror("unlink[ctrl_iface]");
  1267. wpa_printf(MSG_ERROR, "Could not unlink "
  1268. "existing ctrl_iface socket '%s'",
  1269. fname);
  1270. goto fail;
  1271. }
  1272. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1273. 0) {
  1274. perror("bind(PF_UNIX)");
  1275. goto fail;
  1276. }
  1277. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1278. "ctrl_iface socket '%s'", fname);
  1279. } else {
  1280. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1281. "be in use - cannot override it");
  1282. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1283. "not used anymore", fname);
  1284. os_free(fname);
  1285. fname = NULL;
  1286. goto fail;
  1287. }
  1288. }
  1289. if (interface->ctrl_iface_group &&
  1290. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  1291. perror("chown[ctrl_interface]");
  1292. goto fail;
  1293. }
  1294. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1295. perror("chmod[ctrl_interface/ifname]");
  1296. goto fail;
  1297. }
  1298. os_free(fname);
  1299. interface->global_ctrl_sock = s;
  1300. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1301. interface, NULL);
  1302. return 0;
  1303. fail:
  1304. if (s >= 0)
  1305. close(s);
  1306. if (fname) {
  1307. unlink(fname);
  1308. os_free(fname);
  1309. }
  1310. return -1;
  1311. }
  1312. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1313. {
  1314. char *fname = NULL;
  1315. if (interfaces->global_ctrl_sock > -1) {
  1316. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1317. close(interfaces->global_ctrl_sock);
  1318. interfaces->global_ctrl_sock = -1;
  1319. fname = hostapd_global_ctrl_iface_path(interfaces);
  1320. if (fname) {
  1321. unlink(fname);
  1322. os_free(fname);
  1323. }
  1324. if (interfaces->global_iface_path &&
  1325. rmdir(interfaces->global_iface_path) < 0) {
  1326. if (errno == ENOTEMPTY) {
  1327. wpa_printf(MSG_DEBUG, "Control interface "
  1328. "directory not empty - leaving it "
  1329. "behind");
  1330. } else {
  1331. wpa_printf(MSG_ERROR,
  1332. "rmdir[ctrl_interface=%s]: %s",
  1333. interfaces->global_iface_path,
  1334. strerror(errno));
  1335. }
  1336. }
  1337. os_free(interfaces->global_iface_path);
  1338. interfaces->global_iface_path = NULL;
  1339. }
  1340. }
  1341. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1342. const char *buf, size_t len)
  1343. {
  1344. struct wpa_ctrl_dst *dst, *next;
  1345. struct msghdr msg;
  1346. int idx;
  1347. struct iovec io[2];
  1348. char levelstr[10];
  1349. dst = hapd->ctrl_dst;
  1350. if (hapd->ctrl_sock < 0 || dst == NULL)
  1351. return;
  1352. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1353. io[0].iov_base = levelstr;
  1354. io[0].iov_len = os_strlen(levelstr);
  1355. io[1].iov_base = (char *) buf;
  1356. io[1].iov_len = len;
  1357. os_memset(&msg, 0, sizeof(msg));
  1358. msg.msg_iov = io;
  1359. msg.msg_iovlen = 2;
  1360. idx = 0;
  1361. while (dst) {
  1362. next = dst->next;
  1363. if (level >= dst->debug_level) {
  1364. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1365. (u8 *) dst->addr.sun_path, dst->addrlen -
  1366. offsetof(struct sockaddr_un, sun_path));
  1367. msg.msg_name = &dst->addr;
  1368. msg.msg_namelen = dst->addrlen;
  1369. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1370. int _errno = errno;
  1371. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1372. "%d - %s",
  1373. idx, errno, strerror(errno));
  1374. dst->errors++;
  1375. if (dst->errors > 10 || _errno == ENOENT) {
  1376. hostapd_ctrl_iface_detach(
  1377. hapd, &dst->addr,
  1378. dst->addrlen);
  1379. }
  1380. } else
  1381. dst->errors = 0;
  1382. }
  1383. idx++;
  1384. dst = next;
  1385. }
  1386. }
  1387. #endif /* CONFIG_NATIVE_WINDOWS */