ctrl_iface.c 46 KB

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