ctrl_iface.c 34 KB

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