ctrl_iface.c 102 KB

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