ctrl_iface.c 106 KB

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