ctrl_iface.c 86 KB

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