ctrl_iface.c 62 KB

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