ctrl_iface.c 63 KB

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