ctrl_iface.c 35 KB

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