ctrl_iface.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #ifndef CONFIG_NATIVE_WINDOWS
  10. #include <sys/un.h>
  11. #include <sys/stat.h>
  12. #include <stddef.h>
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "common/version.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "drivers/driver.h"
  18. #include "radius/radius_client.h"
  19. #include "ap/hostapd.h"
  20. #include "ap/ap_config.h"
  21. #include "ap/ieee802_1x.h"
  22. #include "ap/wpa_auth.h"
  23. #include "ap/ieee802_11.h"
  24. #include "ap/sta_info.h"
  25. #include "ap/wps_hostapd.h"
  26. #include "ap/ctrl_iface_ap.h"
  27. #include "ap/ap_drv_ops.h"
  28. #include "ap/wpa_auth.h"
  29. #include "wps/wps_defs.h"
  30. #include "wps/wps.h"
  31. #include "config_file.h"
  32. #include "ctrl_iface.h"
  33. struct wpa_ctrl_dst {
  34. struct wpa_ctrl_dst *next;
  35. struct sockaddr_un addr;
  36. socklen_t addrlen;
  37. int debug_level;
  38. int errors;
  39. };
  40. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  41. const char *buf, size_t len);
  42. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  43. struct sockaddr_un *from,
  44. socklen_t fromlen)
  45. {
  46. struct wpa_ctrl_dst *dst;
  47. dst = os_zalloc(sizeof(*dst));
  48. if (dst == NULL)
  49. return -1;
  50. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  51. dst->addrlen = fromlen;
  52. dst->debug_level = MSG_INFO;
  53. dst->next = hapd->ctrl_dst;
  54. hapd->ctrl_dst = dst;
  55. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  56. (u8 *) from->sun_path,
  57. fromlen - offsetof(struct sockaddr_un, sun_path));
  58. return 0;
  59. }
  60. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  61. struct sockaddr_un *from,
  62. socklen_t fromlen)
  63. {
  64. struct wpa_ctrl_dst *dst, *prev = NULL;
  65. dst = hapd->ctrl_dst;
  66. while (dst) {
  67. if (fromlen == dst->addrlen &&
  68. os_memcmp(from->sun_path, dst->addr.sun_path,
  69. fromlen - offsetof(struct sockaddr_un, sun_path))
  70. == 0) {
  71. if (prev == NULL)
  72. hapd->ctrl_dst = dst->next;
  73. else
  74. prev->next = dst->next;
  75. os_free(dst);
  76. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  77. (u8 *) from->sun_path,
  78. fromlen -
  79. offsetof(struct sockaddr_un, sun_path));
  80. return 0;
  81. }
  82. prev = dst;
  83. dst = dst->next;
  84. }
  85. return -1;
  86. }
  87. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  88. struct sockaddr_un *from,
  89. socklen_t fromlen,
  90. char *level)
  91. {
  92. struct wpa_ctrl_dst *dst;
  93. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  94. dst = hapd->ctrl_dst;
  95. while (dst) {
  96. if (fromlen == dst->addrlen &&
  97. os_memcmp(from->sun_path, dst->addr.sun_path,
  98. fromlen - offsetof(struct sockaddr_un, sun_path))
  99. == 0) {
  100. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  101. "level", (u8 *) from->sun_path, fromlen -
  102. offsetof(struct sockaddr_un, sun_path));
  103. dst->debug_level = atoi(level);
  104. return 0;
  105. }
  106. dst = dst->next;
  107. }
  108. return -1;
  109. }
  110. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  111. const char *txtaddr)
  112. {
  113. u8 addr[ETH_ALEN];
  114. struct sta_info *sta;
  115. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  116. if (hwaddr_aton(txtaddr, addr))
  117. return -1;
  118. sta = ap_get_sta(hapd, addr);
  119. if (sta)
  120. return 0;
  121. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  122. "notification", MAC2STR(addr));
  123. sta = ap_sta_add(hapd, addr);
  124. if (sta == NULL)
  125. return -1;
  126. hostapd_new_assoc_sta(hapd, sta, 0);
  127. return 0;
  128. }
  129. #ifdef CONFIG_IEEE80211W
  130. #ifdef NEED_AP_MLME
  131. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  132. const char *txtaddr)
  133. {
  134. u8 addr[ETH_ALEN];
  135. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  136. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  137. if (hwaddr_aton(txtaddr, addr) ||
  138. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  139. return -1;
  140. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  141. return 0;
  142. }
  143. #endif /* NEED_AP_MLME */
  144. #endif /* CONFIG_IEEE80211W */
  145. #ifdef CONFIG_WPS
  146. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  147. {
  148. char *pin = os_strchr(txt, ' ');
  149. char *timeout_txt;
  150. int timeout;
  151. u8 addr_buf[ETH_ALEN], *addr = NULL;
  152. char *pos;
  153. if (pin == NULL)
  154. return -1;
  155. *pin++ = '\0';
  156. timeout_txt = os_strchr(pin, ' ');
  157. if (timeout_txt) {
  158. *timeout_txt++ = '\0';
  159. timeout = atoi(timeout_txt);
  160. pos = os_strchr(timeout_txt, ' ');
  161. if (pos) {
  162. *pos++ = '\0';
  163. if (hwaddr_aton(pos, addr_buf) == 0)
  164. addr = addr_buf;
  165. }
  166. } else
  167. timeout = 0;
  168. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  169. }
  170. static int hostapd_ctrl_iface_wps_check_pin(
  171. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  172. {
  173. char pin[9];
  174. size_t len;
  175. char *pos;
  176. int ret;
  177. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  178. (u8 *) cmd, os_strlen(cmd));
  179. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  180. if (*pos < '0' || *pos > '9')
  181. continue;
  182. pin[len++] = *pos;
  183. if (len == 9) {
  184. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  185. return -1;
  186. }
  187. }
  188. if (len != 4 && len != 8) {
  189. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  190. return -1;
  191. }
  192. pin[len] = '\0';
  193. if (len == 8) {
  194. unsigned int pin_val;
  195. pin_val = atoi(pin);
  196. if (!wps_pin_valid(pin_val)) {
  197. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  198. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  199. if (ret < 0 || (size_t) ret >= buflen)
  200. return -1;
  201. return ret;
  202. }
  203. }
  204. ret = os_snprintf(buf, buflen, "%s", pin);
  205. if (ret < 0 || (size_t) ret >= buflen)
  206. return -1;
  207. return ret;
  208. }
  209. #ifdef CONFIG_WPS_NFC
  210. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  211. char *pos)
  212. {
  213. size_t len;
  214. struct wpabuf *buf;
  215. int ret;
  216. len = os_strlen(pos);
  217. if (len & 0x01)
  218. return -1;
  219. len /= 2;
  220. buf = wpabuf_alloc(len);
  221. if (buf == NULL)
  222. return -1;
  223. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  224. wpabuf_free(buf);
  225. return -1;
  226. }
  227. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  228. wpabuf_free(buf);
  229. return ret;
  230. }
  231. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  232. char *cmd, char *reply,
  233. size_t max_len)
  234. {
  235. int ndef;
  236. struct wpabuf *buf;
  237. int res;
  238. if (os_strcmp(cmd, "WPS") == 0)
  239. ndef = 0;
  240. else if (os_strcmp(cmd, "NDEF") == 0)
  241. ndef = 1;
  242. else
  243. return -1;
  244. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  245. if (buf == NULL)
  246. return -1;
  247. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  248. wpabuf_len(buf));
  249. reply[res++] = '\n';
  250. reply[res] = '\0';
  251. wpabuf_free(buf);
  252. return res;
  253. }
  254. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  255. char *reply, size_t max_len,
  256. int ndef)
  257. {
  258. struct wpabuf *buf;
  259. int res;
  260. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  261. if (buf == NULL)
  262. return -1;
  263. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  264. wpabuf_len(buf));
  265. reply[res++] = '\n';
  266. reply[res] = '\0';
  267. wpabuf_free(buf);
  268. return res;
  269. }
  270. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  271. char *cmd, char *reply,
  272. size_t max_len)
  273. {
  274. if (os_strcmp(cmd, "WPS") == 0)
  275. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  276. max_len, 0);
  277. if (os_strcmp(cmd, "NDEF") == 0)
  278. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  279. max_len, 1);
  280. if (os_strcmp(cmd, "enable") == 0)
  281. return hostapd_wps_nfc_token_enable(hapd);
  282. if (os_strcmp(cmd, "disable") == 0) {
  283. hostapd_wps_nfc_token_disable(hapd);
  284. return 0;
  285. }
  286. return -1;
  287. }
  288. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  289. char *cmd, char *reply,
  290. size_t max_len)
  291. {
  292. struct wpabuf *buf;
  293. int res;
  294. char *pos;
  295. int ndef;
  296. pos = os_strchr(cmd, ' ');
  297. if (pos == NULL)
  298. return -1;
  299. *pos++ = '\0';
  300. if (os_strcmp(cmd, "WPS") == 0)
  301. ndef = 0;
  302. else if (os_strcmp(cmd, "NDEF") == 0)
  303. ndef = 1;
  304. else
  305. return -1;
  306. if (os_strcmp(pos, "WPS-CR") == 0)
  307. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  308. else
  309. buf = NULL;
  310. if (buf == NULL)
  311. return -1;
  312. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  313. wpabuf_len(buf));
  314. reply[res++] = '\n';
  315. reply[res] = '\0';
  316. wpabuf_free(buf);
  317. return res;
  318. }
  319. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  320. char *cmd)
  321. {
  322. /*
  323. * Since NFC connection handover provided full WPS Credential, there is
  324. * no need for additional operations within hostapd. Just report this in
  325. * debug log.
  326. */
  327. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported: %s", cmd);
  328. return 0;
  329. }
  330. #endif /* CONFIG_WPS_NFC */
  331. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  332. char *buf, size_t buflen)
  333. {
  334. int timeout = 300;
  335. char *pos;
  336. const char *pin_txt;
  337. pos = os_strchr(txt, ' ');
  338. if (pos)
  339. *pos++ = '\0';
  340. if (os_strcmp(txt, "disable") == 0) {
  341. hostapd_wps_ap_pin_disable(hapd);
  342. return os_snprintf(buf, buflen, "OK\n");
  343. }
  344. if (os_strcmp(txt, "random") == 0) {
  345. if (pos)
  346. timeout = atoi(pos);
  347. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  348. if (pin_txt == NULL)
  349. return -1;
  350. return os_snprintf(buf, buflen, "%s", pin_txt);
  351. }
  352. if (os_strcmp(txt, "get") == 0) {
  353. pin_txt = hostapd_wps_ap_pin_get(hapd);
  354. if (pin_txt == NULL)
  355. return -1;
  356. return os_snprintf(buf, buflen, "%s", pin_txt);
  357. }
  358. if (os_strcmp(txt, "set") == 0) {
  359. char *pin;
  360. if (pos == NULL)
  361. return -1;
  362. pin = pos;
  363. pos = os_strchr(pos, ' ');
  364. if (pos) {
  365. *pos++ = '\0';
  366. timeout = atoi(pos);
  367. }
  368. if (os_strlen(pin) > buflen)
  369. return -1;
  370. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  371. return -1;
  372. return os_snprintf(buf, buflen, "%s", pin);
  373. }
  374. return -1;
  375. }
  376. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  377. {
  378. char *pos;
  379. char *ssid, *auth, *encr = NULL, *key = NULL;
  380. ssid = txt;
  381. pos = os_strchr(txt, ' ');
  382. if (!pos)
  383. return -1;
  384. *pos++ = '\0';
  385. auth = pos;
  386. pos = os_strchr(pos, ' ');
  387. if (pos) {
  388. *pos++ = '\0';
  389. encr = pos;
  390. pos = os_strchr(pos, ' ');
  391. if (pos) {
  392. *pos++ = '\0';
  393. key = pos;
  394. }
  395. }
  396. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  397. }
  398. static const char * pbc_status_str(enum pbc_status status)
  399. {
  400. switch (status) {
  401. case WPS_PBC_STATUS_DISABLE:
  402. return "Disabled";
  403. case WPS_PBC_STATUS_ACTIVE:
  404. return "Active";
  405. case WPS_PBC_STATUS_TIMEOUT:
  406. return "Timed-out";
  407. case WPS_PBC_STATUS_OVERLAP:
  408. return "Overlap";
  409. default:
  410. return "Unknown";
  411. }
  412. }
  413. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  414. char *buf, size_t buflen)
  415. {
  416. int ret;
  417. char *pos, *end;
  418. pos = buf;
  419. end = buf + buflen;
  420. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  421. pbc_status_str(hapd->wps_stats.pbc_status));
  422. if (ret < 0 || ret >= end - pos)
  423. return pos - buf;
  424. pos += ret;
  425. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  426. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  427. "Success":
  428. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  429. "Failed" : "None")));
  430. if (ret < 0 || ret >= end - pos)
  431. return pos - buf;
  432. pos += ret;
  433. /* If status == Failure - Add possible Reasons */
  434. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  435. hapd->wps_stats.failure_reason > 0) {
  436. ret = os_snprintf(pos, end - pos,
  437. "Failure Reason: %s\n",
  438. wps_ei_str(hapd->wps_stats.failure_reason));
  439. if (ret < 0 || ret >= end - pos)
  440. return pos - buf;
  441. pos += ret;
  442. }
  443. if (hapd->wps_stats.status) {
  444. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  445. MAC2STR(hapd->wps_stats.peer_addr));
  446. if (ret < 0 || ret >= end - pos)
  447. return pos - buf;
  448. pos += ret;
  449. }
  450. return pos - buf;
  451. }
  452. #endif /* CONFIG_WPS */
  453. #ifdef CONFIG_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. } else {
  823. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  824. }
  825. return ret;
  826. }
  827. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  828. char *buf, size_t buflen)
  829. {
  830. int res;
  831. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  832. if (os_strcmp(cmd, "version") == 0) {
  833. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  834. if (res < 0 || (unsigned int) res >= buflen)
  835. return -1;
  836. return res;
  837. }
  838. return -1;
  839. }
  840. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  841. {
  842. if (hostapd_enable_iface(iface) < 0) {
  843. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  844. return -1;
  845. }
  846. return 0;
  847. }
  848. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  849. {
  850. if (hostapd_reload_iface(iface) < 0) {
  851. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  852. return -1;
  853. }
  854. return 0;
  855. }
  856. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  857. {
  858. if (hostapd_disable_iface(iface) < 0) {
  859. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  860. return -1;
  861. }
  862. return 0;
  863. }
  864. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  865. void *sock_ctx)
  866. {
  867. struct hostapd_data *hapd = eloop_ctx;
  868. char buf[256];
  869. int res;
  870. struct sockaddr_un from;
  871. socklen_t fromlen = sizeof(from);
  872. char *reply;
  873. const int reply_size = 4096;
  874. int reply_len;
  875. int level = MSG_DEBUG;
  876. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  877. (struct sockaddr *) &from, &fromlen);
  878. if (res < 0) {
  879. perror("recvfrom(ctrl_iface)");
  880. return;
  881. }
  882. buf[res] = '\0';
  883. if (os_strcmp(buf, "PING") == 0)
  884. level = MSG_EXCESSIVE;
  885. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  886. reply = os_malloc(reply_size);
  887. if (reply == NULL) {
  888. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  889. fromlen);
  890. return;
  891. }
  892. os_memcpy(reply, "OK\n", 3);
  893. reply_len = 3;
  894. if (os_strcmp(buf, "PING") == 0) {
  895. os_memcpy(reply, "PONG\n", 5);
  896. reply_len = 5;
  897. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  898. if (wpa_debug_reopen_file() < 0)
  899. reply_len = -1;
  900. } else if (os_strcmp(buf, "MIB") == 0) {
  901. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  902. if (reply_len >= 0) {
  903. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  904. reply_size - reply_len);
  905. if (res < 0)
  906. reply_len = -1;
  907. else
  908. reply_len += res;
  909. }
  910. if (reply_len >= 0) {
  911. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  912. reply_size - reply_len);
  913. if (res < 0)
  914. reply_len = -1;
  915. else
  916. reply_len += res;
  917. }
  918. #ifndef CONFIG_NO_RADIUS
  919. if (reply_len >= 0) {
  920. res = radius_client_get_mib(hapd->radius,
  921. reply + reply_len,
  922. reply_size - reply_len);
  923. if (res < 0)
  924. reply_len = -1;
  925. else
  926. reply_len += res;
  927. }
  928. #endif /* CONFIG_NO_RADIUS */
  929. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  930. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  931. reply_size);
  932. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  933. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  934. reply_size);
  935. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  936. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  937. reply_size);
  938. } else if (os_strcmp(buf, "ATTACH") == 0) {
  939. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  940. reply_len = -1;
  941. } else if (os_strcmp(buf, "DETACH") == 0) {
  942. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  943. reply_len = -1;
  944. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  945. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  946. buf + 6))
  947. reply_len = -1;
  948. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  949. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  950. reply_len = -1;
  951. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  952. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  953. reply_len = -1;
  954. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  955. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  956. reply_len = -1;
  957. #ifdef CONFIG_IEEE80211W
  958. #ifdef NEED_AP_MLME
  959. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  960. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  961. reply_len = -1;
  962. #endif /* NEED_AP_MLME */
  963. #endif /* CONFIG_IEEE80211W */
  964. #ifdef CONFIG_WPS
  965. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  966. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  967. reply_len = -1;
  968. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  969. reply_len = hostapd_ctrl_iface_wps_check_pin(
  970. hapd, buf + 14, reply, reply_size);
  971. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  972. if (hostapd_wps_button_pushed(hapd, NULL))
  973. reply_len = -1;
  974. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  975. if (hostapd_wps_cancel(hapd))
  976. reply_len = -1;
  977. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  978. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  979. reply, reply_size);
  980. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  981. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  982. reply_len = -1;
  983. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  984. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  985. reply_size);
  986. #ifdef CONFIG_WPS_NFC
  987. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  988. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  989. reply_len = -1;
  990. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  991. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  992. hapd, buf + 21, reply, reply_size);
  993. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  994. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  995. hapd, buf + 14, reply, reply_size);
  996. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  997. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  998. hapd, buf + 21, reply, reply_size);
  999. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1000. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1001. reply_len = -1;
  1002. #endif /* CONFIG_WPS_NFC */
  1003. #endif /* CONFIG_WPS */
  1004. #ifdef CONFIG_INTERWORKING
  1005. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1006. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1007. reply_len = -1;
  1008. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1009. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1010. reply_len = -1;
  1011. #endif /* CONFIG_INTERWORKING */
  1012. #ifdef CONFIG_WNM
  1013. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1014. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1015. reply_len = -1;
  1016. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1017. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1018. reply_len = -1;
  1019. #endif /* CONFIG_WNM */
  1020. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1021. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1022. reply_size);
  1023. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1024. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1025. reply_len = -1;
  1026. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1027. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1028. reply_size);
  1029. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1030. if (hostapd_ctrl_iface_enable(hapd->iface))
  1031. reply_len = -1;
  1032. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1033. if (hostapd_ctrl_iface_reload(hapd->iface))
  1034. reply_len = -1;
  1035. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1036. if (hostapd_ctrl_iface_disable(hapd->iface))
  1037. reply_len = -1;
  1038. } else {
  1039. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1040. reply_len = 16;
  1041. }
  1042. if (reply_len < 0) {
  1043. os_memcpy(reply, "FAIL\n", 5);
  1044. reply_len = 5;
  1045. }
  1046. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1047. os_free(reply);
  1048. }
  1049. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  1050. {
  1051. char *buf;
  1052. size_t len;
  1053. if (hapd->conf->ctrl_interface == NULL)
  1054. return NULL;
  1055. len = os_strlen(hapd->conf->ctrl_interface) +
  1056. os_strlen(hapd->conf->iface) + 2;
  1057. buf = os_malloc(len);
  1058. if (buf == NULL)
  1059. return NULL;
  1060. os_snprintf(buf, len, "%s/%s",
  1061. hapd->conf->ctrl_interface, hapd->conf->iface);
  1062. buf[len - 1] = '\0';
  1063. return buf;
  1064. }
  1065. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
  1066. const char *txt, size_t len)
  1067. {
  1068. struct hostapd_data *hapd = ctx;
  1069. if (hapd == NULL)
  1070. return;
  1071. hostapd_ctrl_iface_send(hapd, level, txt, len);
  1072. }
  1073. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  1074. {
  1075. struct sockaddr_un addr;
  1076. int s = -1;
  1077. char *fname = NULL;
  1078. if (hapd->ctrl_sock > -1) {
  1079. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  1080. return 0;
  1081. }
  1082. if (hapd->conf->ctrl_interface == NULL)
  1083. return 0;
  1084. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1085. if (errno == EEXIST) {
  1086. wpa_printf(MSG_DEBUG, "Using existing control "
  1087. "interface directory.");
  1088. } else {
  1089. perror("mkdir[ctrl_interface]");
  1090. goto fail;
  1091. }
  1092. }
  1093. if (hapd->conf->ctrl_interface_gid_set &&
  1094. chown(hapd->conf->ctrl_interface, -1,
  1095. hapd->conf->ctrl_interface_gid) < 0) {
  1096. perror("chown[ctrl_interface]");
  1097. return -1;
  1098. }
  1099. if (!hapd->conf->ctrl_interface_gid_set &&
  1100. hapd->iface->interfaces->ctrl_iface_group &&
  1101. chown(hapd->conf->ctrl_interface, -1,
  1102. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1103. perror("chown[ctrl_interface]");
  1104. return -1;
  1105. }
  1106. #ifdef ANDROID
  1107. /*
  1108. * Android is using umask 0077 which would leave the control interface
  1109. * directory without group access. This breaks things since Wi-Fi
  1110. * framework assumes that this directory can be accessed by other
  1111. * applications in the wifi group. Fix this by adding group access even
  1112. * if umask value would prevent this.
  1113. */
  1114. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1115. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  1116. strerror(errno));
  1117. /* Try to continue anyway */
  1118. }
  1119. #endif /* ANDROID */
  1120. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  1121. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  1122. goto fail;
  1123. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1124. if (s < 0) {
  1125. perror("socket(PF_UNIX)");
  1126. goto fail;
  1127. }
  1128. os_memset(&addr, 0, sizeof(addr));
  1129. #ifdef __FreeBSD__
  1130. addr.sun_len = sizeof(addr);
  1131. #endif /* __FreeBSD__ */
  1132. addr.sun_family = AF_UNIX;
  1133. fname = hostapd_ctrl_iface_path(hapd);
  1134. if (fname == NULL)
  1135. goto fail;
  1136. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1137. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1138. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1139. strerror(errno));
  1140. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1141. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1142. " allow connections - assuming it was left"
  1143. "over from forced program termination");
  1144. if (unlink(fname) < 0) {
  1145. perror("unlink[ctrl_iface]");
  1146. wpa_printf(MSG_ERROR, "Could not unlink "
  1147. "existing ctrl_iface socket '%s'",
  1148. fname);
  1149. goto fail;
  1150. }
  1151. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1152. 0) {
  1153. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  1154. goto fail;
  1155. }
  1156. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1157. "ctrl_iface socket '%s'", fname);
  1158. } else {
  1159. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1160. "be in use - cannot override it");
  1161. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1162. "not used anymore", fname);
  1163. os_free(fname);
  1164. fname = NULL;
  1165. goto fail;
  1166. }
  1167. }
  1168. if (hapd->conf->ctrl_interface_gid_set &&
  1169. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  1170. perror("chown[ctrl_interface/ifname]");
  1171. goto fail;
  1172. }
  1173. if (!hapd->conf->ctrl_interface_gid_set &&
  1174. hapd->iface->interfaces->ctrl_iface_group &&
  1175. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1176. perror("chown[ctrl_interface/ifname]");
  1177. goto fail;
  1178. }
  1179. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1180. perror("chmod[ctrl_interface/ifname]");
  1181. goto fail;
  1182. }
  1183. os_free(fname);
  1184. hapd->ctrl_sock = s;
  1185. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  1186. NULL);
  1187. hapd->msg_ctx = hapd;
  1188. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  1189. return 0;
  1190. fail:
  1191. if (s >= 0)
  1192. close(s);
  1193. if (fname) {
  1194. unlink(fname);
  1195. os_free(fname);
  1196. }
  1197. return -1;
  1198. }
  1199. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  1200. {
  1201. struct wpa_ctrl_dst *dst, *prev;
  1202. if (hapd->ctrl_sock > -1) {
  1203. char *fname;
  1204. eloop_unregister_read_sock(hapd->ctrl_sock);
  1205. close(hapd->ctrl_sock);
  1206. hapd->ctrl_sock = -1;
  1207. fname = hostapd_ctrl_iface_path(hapd);
  1208. if (fname)
  1209. unlink(fname);
  1210. os_free(fname);
  1211. if (hapd->conf->ctrl_interface &&
  1212. rmdir(hapd->conf->ctrl_interface) < 0) {
  1213. if (errno == ENOTEMPTY) {
  1214. wpa_printf(MSG_DEBUG, "Control interface "
  1215. "directory not empty - leaving it "
  1216. "behind");
  1217. } else {
  1218. wpa_printf(MSG_ERROR,
  1219. "rmdir[ctrl_interface=%s]: %s",
  1220. hapd->conf->ctrl_interface,
  1221. strerror(errno));
  1222. }
  1223. }
  1224. }
  1225. dst = hapd->ctrl_dst;
  1226. while (dst) {
  1227. prev = dst;
  1228. dst = dst->next;
  1229. os_free(prev);
  1230. }
  1231. }
  1232. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1233. char *buf)
  1234. {
  1235. if (hostapd_add_iface(interfaces, buf) < 0) {
  1236. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1237. return -1;
  1238. }
  1239. return 0;
  1240. }
  1241. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1242. char *buf)
  1243. {
  1244. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1245. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1246. return -1;
  1247. }
  1248. return 0;
  1249. }
  1250. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1251. void *sock_ctx)
  1252. {
  1253. void *interfaces = eloop_ctx;
  1254. char buf[256];
  1255. int res;
  1256. struct sockaddr_un from;
  1257. socklen_t fromlen = sizeof(from);
  1258. char reply[24];
  1259. int reply_len;
  1260. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1261. (struct sockaddr *) &from, &fromlen);
  1262. if (res < 0) {
  1263. perror("recvfrom(ctrl_iface)");
  1264. return;
  1265. }
  1266. buf[res] = '\0';
  1267. os_memcpy(reply, "OK\n", 3);
  1268. reply_len = 3;
  1269. if (os_strcmp(buf, "PING") == 0) {
  1270. os_memcpy(reply, "PONG\n", 5);
  1271. reply_len = 5;
  1272. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1273. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1274. reply_len = -1;
  1275. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1276. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1277. reply_len = -1;
  1278. } else {
  1279. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1280. "ignored");
  1281. reply_len = -1;
  1282. }
  1283. if (reply_len < 0) {
  1284. os_memcpy(reply, "FAIL\n", 5);
  1285. reply_len = 5;
  1286. }
  1287. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1288. }
  1289. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1290. {
  1291. char *buf;
  1292. size_t len;
  1293. if (interface->global_iface_path == NULL)
  1294. return NULL;
  1295. len = os_strlen(interface->global_iface_path) +
  1296. os_strlen(interface->global_iface_name) + 2;
  1297. buf = os_malloc(len);
  1298. if (buf == NULL)
  1299. return NULL;
  1300. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1301. interface->global_iface_name);
  1302. buf[len - 1] = '\0';
  1303. return buf;
  1304. }
  1305. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1306. {
  1307. struct sockaddr_un addr;
  1308. int s = -1;
  1309. char *fname = NULL;
  1310. if (interface->global_iface_path == NULL) {
  1311. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1312. return 0;
  1313. }
  1314. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1315. if (errno == EEXIST) {
  1316. wpa_printf(MSG_DEBUG, "Using existing control "
  1317. "interface directory.");
  1318. } else {
  1319. perror("mkdir[ctrl_interface]");
  1320. goto fail;
  1321. }
  1322. } else if (interface->ctrl_iface_group &&
  1323. chown(interface->global_iface_path, -1,
  1324. interface->ctrl_iface_group) < 0) {
  1325. perror("chown[ctrl_interface]");
  1326. goto fail;
  1327. }
  1328. if (os_strlen(interface->global_iface_path) + 1 +
  1329. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1330. goto fail;
  1331. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1332. if (s < 0) {
  1333. perror("socket(PF_UNIX)");
  1334. goto fail;
  1335. }
  1336. os_memset(&addr, 0, sizeof(addr));
  1337. #ifdef __FreeBSD__
  1338. addr.sun_len = sizeof(addr);
  1339. #endif /* __FreeBSD__ */
  1340. addr.sun_family = AF_UNIX;
  1341. fname = hostapd_global_ctrl_iface_path(interface);
  1342. if (fname == NULL)
  1343. goto fail;
  1344. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1345. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1346. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1347. strerror(errno));
  1348. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1349. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1350. " allow connections - assuming it was left"
  1351. "over from forced program termination");
  1352. if (unlink(fname) < 0) {
  1353. perror("unlink[ctrl_iface]");
  1354. wpa_printf(MSG_ERROR, "Could not unlink "
  1355. "existing ctrl_iface socket '%s'",
  1356. fname);
  1357. goto fail;
  1358. }
  1359. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1360. 0) {
  1361. perror("bind(PF_UNIX)");
  1362. goto fail;
  1363. }
  1364. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1365. "ctrl_iface socket '%s'", fname);
  1366. } else {
  1367. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1368. "be in use - cannot override it");
  1369. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1370. "not used anymore", fname);
  1371. os_free(fname);
  1372. fname = NULL;
  1373. goto fail;
  1374. }
  1375. }
  1376. if (interface->ctrl_iface_group &&
  1377. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  1378. perror("chown[ctrl_interface]");
  1379. goto fail;
  1380. }
  1381. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1382. perror("chmod[ctrl_interface/ifname]");
  1383. goto fail;
  1384. }
  1385. os_free(fname);
  1386. interface->global_ctrl_sock = s;
  1387. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1388. interface, NULL);
  1389. return 0;
  1390. fail:
  1391. if (s >= 0)
  1392. close(s);
  1393. if (fname) {
  1394. unlink(fname);
  1395. os_free(fname);
  1396. }
  1397. return -1;
  1398. }
  1399. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1400. {
  1401. char *fname = NULL;
  1402. if (interfaces->global_ctrl_sock > -1) {
  1403. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1404. close(interfaces->global_ctrl_sock);
  1405. interfaces->global_ctrl_sock = -1;
  1406. fname = hostapd_global_ctrl_iface_path(interfaces);
  1407. if (fname) {
  1408. unlink(fname);
  1409. os_free(fname);
  1410. }
  1411. if (interfaces->global_iface_path &&
  1412. rmdir(interfaces->global_iface_path) < 0) {
  1413. if (errno == ENOTEMPTY) {
  1414. wpa_printf(MSG_DEBUG, "Control interface "
  1415. "directory not empty - leaving it "
  1416. "behind");
  1417. } else {
  1418. wpa_printf(MSG_ERROR,
  1419. "rmdir[ctrl_interface=%s]: %s",
  1420. interfaces->global_iface_path,
  1421. strerror(errno));
  1422. }
  1423. }
  1424. os_free(interfaces->global_iface_path);
  1425. interfaces->global_iface_path = NULL;
  1426. }
  1427. }
  1428. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1429. const char *buf, size_t len)
  1430. {
  1431. struct wpa_ctrl_dst *dst, *next;
  1432. struct msghdr msg;
  1433. int idx;
  1434. struct iovec io[2];
  1435. char levelstr[10];
  1436. dst = hapd->ctrl_dst;
  1437. if (hapd->ctrl_sock < 0 || dst == NULL)
  1438. return;
  1439. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1440. io[0].iov_base = levelstr;
  1441. io[0].iov_len = os_strlen(levelstr);
  1442. io[1].iov_base = (char *) buf;
  1443. io[1].iov_len = len;
  1444. os_memset(&msg, 0, sizeof(msg));
  1445. msg.msg_iov = io;
  1446. msg.msg_iovlen = 2;
  1447. idx = 0;
  1448. while (dst) {
  1449. next = dst->next;
  1450. if (level >= dst->debug_level) {
  1451. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1452. (u8 *) dst->addr.sun_path, dst->addrlen -
  1453. offsetof(struct sockaddr_un, sun_path));
  1454. msg.msg_name = &dst->addr;
  1455. msg.msg_namelen = dst->addrlen;
  1456. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1457. int _errno = errno;
  1458. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1459. "%d - %s",
  1460. idx, errno, strerror(errno));
  1461. dst->errors++;
  1462. if (dst->errors > 10 || _errno == ENOENT) {
  1463. hostapd_ctrl_iface_detach(
  1464. hapd, &dst->addr,
  1465. dst->addrlen);
  1466. }
  1467. } else
  1468. dst->errors = 0;
  1469. }
  1470. idx++;
  1471. dst = next;
  1472. }
  1473. }
  1474. #endif /* CONFIG_NATIVE_WINDOWS */