ctrl_iface.c 106 KB

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