ctrl_iface.c 59 KB

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