ctrl_iface.c 86 KB

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