ctrl_iface.c 98 KB

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