ctrl_iface.c 49 KB

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