ctrl_iface.c 45 KB

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