ctrl_iface.c 70 KB

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