ctrl_iface.c 42 KB

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