ctrl_iface.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922
  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_rx_process(struct hostapd_data *hapd,
  1258. char *cmd)
  1259. {
  1260. char *pos, *param;
  1261. size_t len;
  1262. u8 *buf;
  1263. int freq = 0, datarate = 0, ssi_signal = 0;
  1264. union wpa_event_data event;
  1265. if (!hapd->ext_mgmt_frame_handling)
  1266. return -1;
  1267. /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
  1268. wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
  1269. pos = cmd;
  1270. param = os_strstr(pos, "freq=");
  1271. if (param) {
  1272. param += 5;
  1273. freq = atoi(param);
  1274. }
  1275. param = os_strstr(pos, " datarate=");
  1276. if (param) {
  1277. param += 10;
  1278. datarate = atoi(param);
  1279. }
  1280. param = os_strstr(pos, " ssi_signal=");
  1281. if (param) {
  1282. param += 12;
  1283. ssi_signal = atoi(param);
  1284. }
  1285. param = os_strstr(pos, " frame=");
  1286. if (param == NULL)
  1287. return -1;
  1288. param += 7;
  1289. len = os_strlen(param);
  1290. if (len & 1)
  1291. return -1;
  1292. len /= 2;
  1293. buf = os_malloc(len);
  1294. if (buf == NULL)
  1295. return -1;
  1296. if (hexstr2bin(param, buf, len) < 0) {
  1297. os_free(buf);
  1298. return -1;
  1299. }
  1300. os_memset(&event, 0, sizeof(event));
  1301. event.rx_mgmt.freq = freq;
  1302. event.rx_mgmt.frame = buf;
  1303. event.rx_mgmt.frame_len = len;
  1304. event.rx_mgmt.ssi_signal = ssi_signal;
  1305. event.rx_mgmt.datarate = datarate;
  1306. hapd->ext_mgmt_frame_handling = 0;
  1307. wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
  1308. hapd->ext_mgmt_frame_handling = 1;
  1309. os_free(buf);
  1310. return 0;
  1311. }
  1312. static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
  1313. {
  1314. char *pos;
  1315. u8 src[ETH_ALEN], *buf;
  1316. int used;
  1317. size_t len;
  1318. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  1319. pos = cmd;
  1320. used = hwaddr_aton2(pos, src);
  1321. if (used < 0)
  1322. return -1;
  1323. pos += used;
  1324. while (*pos == ' ')
  1325. pos++;
  1326. len = os_strlen(pos);
  1327. if (len & 1)
  1328. return -1;
  1329. len /= 2;
  1330. buf = os_malloc(len);
  1331. if (buf == NULL)
  1332. return -1;
  1333. if (hexstr2bin(pos, buf, len) < 0) {
  1334. os_free(buf);
  1335. return -1;
  1336. }
  1337. ieee802_1x_receive(hapd, src, buf, len);
  1338. os_free(buf);
  1339. return 0;
  1340. }
  1341. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  1342. {
  1343. size_t i;
  1344. u32 sum = 0;
  1345. const u16 *pos = buf;
  1346. for (i = 0; i < len / 2; i++)
  1347. sum += *pos++;
  1348. while (sum >> 16)
  1349. sum = (sum & 0xffff) + (sum >> 16);
  1350. return sum ^ 0xffff;
  1351. }
  1352. #define HWSIM_PACKETLEN 1500
  1353. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  1354. static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  1355. size_t len)
  1356. {
  1357. struct hostapd_data *hapd = ctx;
  1358. const struct ether_header *eth;
  1359. struct iphdr ip;
  1360. const u8 *pos;
  1361. unsigned int i;
  1362. if (len != HWSIM_PACKETLEN)
  1363. return;
  1364. eth = (const struct ether_header *) buf;
  1365. os_memcpy(&ip, eth + 1, sizeof(ip));
  1366. pos = &buf[sizeof(*eth) + sizeof(ip)];
  1367. if (ip.ihl != 5 || ip.version != 4 ||
  1368. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  1369. return;
  1370. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  1371. if (*pos != (u8) i)
  1372. return;
  1373. pos++;
  1374. }
  1375. wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  1376. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  1377. }
  1378. static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
  1379. char *cmd)
  1380. {
  1381. int enabled = atoi(cmd);
  1382. char *pos;
  1383. const char *ifname;
  1384. if (!enabled) {
  1385. if (hapd->l2_test) {
  1386. l2_packet_deinit(hapd->l2_test);
  1387. hapd->l2_test = NULL;
  1388. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  1389. "test data: Disabled");
  1390. }
  1391. return 0;
  1392. }
  1393. if (hapd->l2_test)
  1394. return 0;
  1395. pos = os_strstr(cmd, " ifname=");
  1396. if (pos)
  1397. ifname = pos + 8;
  1398. else
  1399. ifname = hapd->conf->iface;
  1400. hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
  1401. ETHERTYPE_IP, hostapd_data_test_rx,
  1402. hapd, 1);
  1403. if (hapd->l2_test == NULL)
  1404. return -1;
  1405. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
  1406. return 0;
  1407. }
  1408. static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
  1409. {
  1410. u8 dst[ETH_ALEN], src[ETH_ALEN];
  1411. char *pos;
  1412. int used;
  1413. long int val;
  1414. u8 tos;
  1415. u8 buf[2 + HWSIM_PACKETLEN];
  1416. struct ether_header *eth;
  1417. struct iphdr *ip;
  1418. u8 *dpos;
  1419. unsigned int i;
  1420. if (hapd->l2_test == NULL)
  1421. return -1;
  1422. /* format: <dst> <src> <tos> */
  1423. pos = cmd;
  1424. used = hwaddr_aton2(pos, dst);
  1425. if (used < 0)
  1426. return -1;
  1427. pos += used;
  1428. while (*pos == ' ')
  1429. pos++;
  1430. used = hwaddr_aton2(pos, src);
  1431. if (used < 0)
  1432. return -1;
  1433. pos += used;
  1434. val = strtol(pos, NULL, 0);
  1435. if (val < 0 || val > 0xff)
  1436. return -1;
  1437. tos = val;
  1438. eth = (struct ether_header *) &buf[2];
  1439. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  1440. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  1441. eth->ether_type = htons(ETHERTYPE_IP);
  1442. ip = (struct iphdr *) (eth + 1);
  1443. os_memset(ip, 0, sizeof(*ip));
  1444. ip->ihl = 5;
  1445. ip->version = 4;
  1446. ip->ttl = 64;
  1447. ip->tos = tos;
  1448. ip->tot_len = htons(HWSIM_IP_LEN);
  1449. ip->protocol = 1;
  1450. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  1451. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  1452. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  1453. dpos = (u8 *) (ip + 1);
  1454. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  1455. *dpos++ = i;
  1456. if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
  1457. HWSIM_PACKETLEN) < 0)
  1458. return -1;
  1459. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
  1460. " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  1461. return 0;
  1462. }
  1463. static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
  1464. char *cmd)
  1465. {
  1466. u8 *buf;
  1467. struct ether_header *eth;
  1468. struct l2_packet_data *l2 = NULL;
  1469. size_t len;
  1470. u16 ethertype;
  1471. int res = -1;
  1472. const char *ifname = hapd->conf->iface;
  1473. if (os_strncmp(cmd, "ifname=", 7) == 0) {
  1474. cmd += 7;
  1475. ifname = cmd;
  1476. cmd = os_strchr(cmd, ' ');
  1477. if (cmd == NULL)
  1478. return -1;
  1479. *cmd++ = '\0';
  1480. }
  1481. len = os_strlen(cmd);
  1482. if (len & 1 || len < ETH_HLEN * 2)
  1483. return -1;
  1484. len /= 2;
  1485. buf = os_malloc(len);
  1486. if (buf == NULL)
  1487. return -1;
  1488. if (hexstr2bin(cmd, buf, len) < 0)
  1489. goto done;
  1490. eth = (struct ether_header *) buf;
  1491. ethertype = ntohs(eth->ether_type);
  1492. l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
  1493. hostapd_data_test_rx, hapd, 1);
  1494. if (l2 == NULL)
  1495. goto done;
  1496. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  1497. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
  1498. done:
  1499. if (l2)
  1500. l2_packet_deinit(l2);
  1501. os_free(buf);
  1502. return res < 0 ? -1 : 0;
  1503. }
  1504. static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
  1505. {
  1506. #ifdef WPA_TRACE_BFD
  1507. char *pos;
  1508. wpa_trace_fail_after = atoi(cmd);
  1509. pos = os_strchr(cmd, ':');
  1510. if (pos) {
  1511. pos++;
  1512. os_strlcpy(wpa_trace_fail_func, pos,
  1513. sizeof(wpa_trace_fail_func));
  1514. } else {
  1515. wpa_trace_fail_after = 0;
  1516. }
  1517. return 0;
  1518. #else /* WPA_TRACE_BFD */
  1519. return -1;
  1520. #endif /* WPA_TRACE_BFD */
  1521. }
  1522. static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
  1523. char *buf, size_t buflen)
  1524. {
  1525. #ifdef WPA_TRACE_BFD
  1526. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  1527. wpa_trace_fail_func);
  1528. #else /* WPA_TRACE_BFD */
  1529. return -1;
  1530. #endif /* WPA_TRACE_BFD */
  1531. }
  1532. static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
  1533. {
  1534. #ifdef WPA_TRACE_BFD
  1535. char *pos;
  1536. wpa_trace_test_fail_after = atoi(cmd);
  1537. pos = os_strchr(cmd, ':');
  1538. if (pos) {
  1539. pos++;
  1540. os_strlcpy(wpa_trace_test_fail_func, pos,
  1541. sizeof(wpa_trace_test_fail_func));
  1542. } else {
  1543. wpa_trace_test_fail_after = 0;
  1544. }
  1545. return 0;
  1546. #else /* WPA_TRACE_BFD */
  1547. return -1;
  1548. #endif /* WPA_TRACE_BFD */
  1549. }
  1550. static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
  1551. char *buf, size_t buflen)
  1552. {
  1553. #ifdef WPA_TRACE_BFD
  1554. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  1555. wpa_trace_test_fail_func);
  1556. #else /* WPA_TRACE_BFD */
  1557. return -1;
  1558. #endif /* WPA_TRACE_BFD */
  1559. }
  1560. #endif /* CONFIG_TESTING_OPTIONS */
  1561. static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
  1562. char *pos)
  1563. {
  1564. #ifdef NEED_AP_MLME
  1565. struct csa_settings settings;
  1566. int ret;
  1567. unsigned int i;
  1568. ret = hostapd_parse_csa_settings(pos, &settings);
  1569. if (ret)
  1570. return ret;
  1571. for (i = 0; i < iface->num_bss; i++) {
  1572. ret = hostapd_switch_channel(iface->bss[i], &settings);
  1573. if (ret) {
  1574. /* FIX: What do we do if CSA fails in the middle of
  1575. * submitting multi-BSS CSA requests? */
  1576. return ret;
  1577. }
  1578. }
  1579. return 0;
  1580. #else /* NEED_AP_MLME */
  1581. return -1;
  1582. #endif /* NEED_AP_MLME */
  1583. }
  1584. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  1585. int reply_size, const char *param)
  1586. {
  1587. #ifdef RADIUS_SERVER
  1588. if (os_strcmp(param, "radius_server") == 0) {
  1589. return radius_server_get_mib(hapd->radius_srv, reply,
  1590. reply_size);
  1591. }
  1592. #endif /* RADIUS_SERVER */
  1593. return -1;
  1594. }
  1595. static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
  1596. char *buf, size_t buflen)
  1597. {
  1598. int ret;
  1599. char *pos;
  1600. u8 *data = NULL;
  1601. unsigned int vendor_id, subcmd;
  1602. struct wpabuf *reply;
  1603. size_t data_len = 0;
  1604. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  1605. vendor_id = strtoul(cmd, &pos, 16);
  1606. if (!isblank((unsigned char) *pos))
  1607. return -EINVAL;
  1608. subcmd = strtoul(pos, &pos, 10);
  1609. if (*pos != '\0') {
  1610. if (!isblank((unsigned char) *pos++))
  1611. return -EINVAL;
  1612. data_len = os_strlen(pos);
  1613. }
  1614. if (data_len) {
  1615. data_len /= 2;
  1616. data = os_malloc(data_len);
  1617. if (!data)
  1618. return -ENOBUFS;
  1619. if (hexstr2bin(pos, data, data_len)) {
  1620. wpa_printf(MSG_DEBUG,
  1621. "Vendor command: wrong parameter format");
  1622. os_free(data);
  1623. return -EINVAL;
  1624. }
  1625. }
  1626. reply = wpabuf_alloc((buflen - 1) / 2);
  1627. if (!reply) {
  1628. os_free(data);
  1629. return -ENOBUFS;
  1630. }
  1631. ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
  1632. reply);
  1633. if (ret == 0)
  1634. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  1635. wpabuf_len(reply));
  1636. wpabuf_free(reply);
  1637. os_free(data);
  1638. return ret;
  1639. }
  1640. static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
  1641. const char *cmd)
  1642. {
  1643. u8 addr[ETH_ALEN];
  1644. struct sta_info *sta;
  1645. if (hwaddr_aton(cmd, addr))
  1646. return -1;
  1647. sta = ap_get_sta(hapd, addr);
  1648. if (!sta || !sta->eapol_sm)
  1649. return -1;
  1650. eapol_auth_reauthenticate(sta->eapol_sm);
  1651. return 0;
  1652. }
  1653. static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
  1654. {
  1655. u8 addr[ETH_ALEN];
  1656. struct sta_info *sta;
  1657. char *pos = cmd, *param;
  1658. if (hwaddr_aton(pos, addr) || pos[17] != ' ')
  1659. return -1;
  1660. pos += 18;
  1661. param = pos;
  1662. pos = os_strchr(pos, ' ');
  1663. if (!pos)
  1664. return -1;
  1665. *pos++ = '\0';
  1666. sta = ap_get_sta(hapd, addr);
  1667. if (!sta || !sta->eapol_sm)
  1668. return -1;
  1669. return eapol_auth_set_conf(sta->eapol_sm, param, pos);
  1670. }
  1671. static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
  1672. char *buf, size_t buflen)
  1673. {
  1674. char *pos, *end, *stamp;
  1675. int ret;
  1676. /* cmd: "LOG_LEVEL [<level>]" */
  1677. if (*cmd == '\0') {
  1678. pos = buf;
  1679. end = buf + buflen;
  1680. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1681. "Timestamp: %d\n",
  1682. debug_level_str(wpa_debug_level),
  1683. wpa_debug_timestamp);
  1684. if (os_snprintf_error(end - pos, ret))
  1685. ret = 0;
  1686. return ret;
  1687. }
  1688. while (*cmd == ' ')
  1689. cmd++;
  1690. stamp = os_strchr(cmd, ' ');
  1691. if (stamp) {
  1692. *stamp++ = '\0';
  1693. while (*stamp == ' ') {
  1694. stamp++;
  1695. }
  1696. }
  1697. if (os_strlen(cmd)) {
  1698. int level = str_to_debug_level(cmd);
  1699. if (level < 0)
  1700. return -1;
  1701. wpa_debug_level = level;
  1702. }
  1703. if (stamp && os_strlen(stamp))
  1704. wpa_debug_timestamp = atoi(stamp);
  1705. os_memcpy(buf, "OK\n", 3);
  1706. return 3;
  1707. }
  1708. #ifdef NEED_AP_MLME
  1709. static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
  1710. char *buf, size_t buflen)
  1711. {
  1712. struct hostapd_iface *iface = hapd->iface;
  1713. char *pos, *end;
  1714. struct hostapd_sta_info *info;
  1715. struct os_reltime now;
  1716. if (!iface->num_sta_seen)
  1717. return 0;
  1718. sta_track_expire(iface, 0);
  1719. pos = buf;
  1720. end = buf + buflen;
  1721. os_get_reltime(&now);
  1722. dl_list_for_each_reverse(info, &iface->sta_seen,
  1723. struct hostapd_sta_info, list) {
  1724. struct os_reltime age;
  1725. int ret;
  1726. os_reltime_sub(&now, &info->last_seen, &age);
  1727. ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
  1728. MAC2STR(info->addr), (unsigned int) age.sec,
  1729. info->ssi_signal);
  1730. if (os_snprintf_error(end - pos, ret))
  1731. break;
  1732. pos += ret;
  1733. }
  1734. return pos - buf;
  1735. }
  1736. #endif /* NEED_AP_MLME */
  1737. static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
  1738. const char *cmd)
  1739. {
  1740. u8 addr[ETH_ALEN];
  1741. if (hwaddr_aton(cmd, addr)) {
  1742. wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
  1743. return -1;
  1744. }
  1745. return hostapd_send_lci_req(hapd, addr);
  1746. }
  1747. static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
  1748. {
  1749. u8 addr[ETH_ALEN];
  1750. char *token, *context = NULL;
  1751. int random_interval, min_ap;
  1752. u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
  1753. unsigned int n_responders;
  1754. token = str_token(cmd, " ", &context);
  1755. if (!token || hwaddr_aton(token, addr)) {
  1756. wpa_printf(MSG_INFO,
  1757. "CTRL: REQ_RANGE - Bad destination address");
  1758. return -1;
  1759. }
  1760. token = str_token(cmd, " ", &context);
  1761. if (!token)
  1762. return -1;
  1763. random_interval = atoi(token);
  1764. if (random_interval < 0 || random_interval > 0xffff)
  1765. return -1;
  1766. token = str_token(cmd, " ", &context);
  1767. if (!token)
  1768. return -1;
  1769. min_ap = atoi(token);
  1770. if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
  1771. return -1;
  1772. n_responders = 0;
  1773. while ((token = str_token(cmd, " ", &context))) {
  1774. if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
  1775. wpa_printf(MSG_INFO,
  1776. "CTRL: REQ_RANGE: Too many responders");
  1777. return -1;
  1778. }
  1779. if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
  1780. wpa_printf(MSG_INFO,
  1781. "CTRL: REQ_RANGE: Bad responder address");
  1782. return -1;
  1783. }
  1784. n_responders++;
  1785. }
  1786. if (!n_responders) {
  1787. wpa_printf(MSG_INFO,
  1788. "CTRL: REQ_RANGE - No FTM responder address");
  1789. return -1;
  1790. }
  1791. return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
  1792. responders, n_responders);
  1793. }
  1794. static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
  1795. const char *cmd, char *reply,
  1796. size_t reply_size)
  1797. {
  1798. u8 addr[ETH_ALEN];
  1799. const char *pos;
  1800. struct wpabuf *req;
  1801. int ret;
  1802. u8 req_mode = 0;
  1803. if (hwaddr_aton(cmd, addr))
  1804. return -1;
  1805. pos = os_strchr(cmd, ' ');
  1806. if (!pos)
  1807. return -1;
  1808. pos++;
  1809. if (os_strncmp(pos, "req_mode=", 9) == 0) {
  1810. int val = hex2byte(pos + 9);
  1811. if (val < 0)
  1812. return -1;
  1813. req_mode = val;
  1814. pos += 11;
  1815. pos = os_strchr(pos, ' ');
  1816. if (!pos)
  1817. return -1;
  1818. pos++;
  1819. }
  1820. req = wpabuf_parse_bin(pos);
  1821. if (!req)
  1822. return -1;
  1823. ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
  1824. wpabuf_free(req);
  1825. if (ret >= 0)
  1826. ret = os_snprintf(reply, reply_size, "%d", ret);
  1827. return ret;
  1828. }
  1829. static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
  1830. {
  1831. struct wpa_ssid_value ssid;
  1832. u8 bssid[ETH_ALEN];
  1833. struct wpabuf *nr, *lci = NULL, *civic = NULL;
  1834. int stationary = 0;
  1835. char *tmp;
  1836. int ret;
  1837. if (!(hapd->conf->radio_measurements[0] &
  1838. WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
  1839. wpa_printf(MSG_ERROR,
  1840. "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
  1841. return -1;
  1842. }
  1843. if (hwaddr_aton(buf, bssid)) {
  1844. wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
  1845. return -1;
  1846. }
  1847. tmp = os_strstr(buf, "ssid=");
  1848. if (!tmp || ssid_parse(tmp + 5, &ssid)) {
  1849. wpa_printf(MSG_ERROR,
  1850. "CTRL: SET_NEIGHBOR: Bad or missing SSID");
  1851. return -1;
  1852. }
  1853. buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
  1854. if (!buf)
  1855. return -1;
  1856. tmp = os_strstr(buf, "nr=");
  1857. if (!tmp) {
  1858. wpa_printf(MSG_ERROR,
  1859. "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
  1860. return -1;
  1861. }
  1862. buf = os_strchr(tmp, ' ');
  1863. if (buf)
  1864. *buf++ = '\0';
  1865. nr = wpabuf_parse_bin(tmp + 3);
  1866. if (!nr) {
  1867. wpa_printf(MSG_ERROR,
  1868. "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
  1869. return -1;
  1870. }
  1871. if (!buf)
  1872. goto set;
  1873. tmp = os_strstr(buf, "lci=");
  1874. if (tmp) {
  1875. buf = os_strchr(tmp, ' ');
  1876. if (buf)
  1877. *buf++ = '\0';
  1878. lci = wpabuf_parse_bin(tmp + 4);
  1879. if (!lci) {
  1880. wpa_printf(MSG_ERROR,
  1881. "CTRL: SET_NEIGHBOR: Bad LCI subelement");
  1882. wpabuf_free(nr);
  1883. return -1;
  1884. }
  1885. }
  1886. if (!buf)
  1887. goto set;
  1888. tmp = os_strstr(buf, "civic=");
  1889. if (tmp) {
  1890. buf = os_strchr(tmp, ' ');
  1891. if (buf)
  1892. *buf++ = '\0';
  1893. civic = wpabuf_parse_bin(tmp + 6);
  1894. if (!civic) {
  1895. wpa_printf(MSG_ERROR,
  1896. "CTRL: SET_NEIGHBOR: Bad civic subelement");
  1897. wpabuf_free(nr);
  1898. wpabuf_free(lci);
  1899. return -1;
  1900. }
  1901. }
  1902. if (!buf)
  1903. goto set;
  1904. if (os_strstr(buf, "stat"))
  1905. stationary = 1;
  1906. set:
  1907. ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
  1908. stationary);
  1909. wpabuf_free(nr);
  1910. wpabuf_free(lci);
  1911. wpabuf_free(civic);
  1912. return ret;
  1913. }
  1914. static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
  1915. char *buf)
  1916. {
  1917. struct wpa_ssid_value ssid;
  1918. u8 bssid[ETH_ALEN];
  1919. char *tmp;
  1920. if (hwaddr_aton(buf, bssid)) {
  1921. wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
  1922. return -1;
  1923. }
  1924. tmp = os_strstr(buf, "ssid=");
  1925. if (!tmp || ssid_parse(tmp + 5, &ssid)) {
  1926. wpa_printf(MSG_ERROR,
  1927. "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
  1928. return -1;
  1929. }
  1930. return hostapd_neighbor_remove(hapd, bssid, &ssid);
  1931. }
  1932. static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
  1933. size_t buflen)
  1934. {
  1935. int ret, i;
  1936. char *pos, *end;
  1937. ret = os_snprintf(buf, buflen, "%016llX:\n",
  1938. (long long unsigned) iface->drv_flags);
  1939. if (os_snprintf_error(buflen, ret))
  1940. return -1;
  1941. pos = buf + ret;
  1942. end = buf + buflen;
  1943. for (i = 0; i < 64; i++) {
  1944. if (iface->drv_flags & (1LLU << i)) {
  1945. ret = os_snprintf(pos, end - pos, "%s\n",
  1946. driver_flag_to_string(1LLU << i));
  1947. if (os_snprintf_error(end - pos, ret))
  1948. return -1;
  1949. pos += ret;
  1950. }
  1951. }
  1952. return pos - buf;
  1953. }
  1954. static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
  1955. char *buf, char *reply,
  1956. int reply_size,
  1957. struct sockaddr_storage *from,
  1958. socklen_t fromlen)
  1959. {
  1960. int reply_len, res;
  1961. os_memcpy(reply, "OK\n", 3);
  1962. reply_len = 3;
  1963. if (os_strcmp(buf, "PING") == 0) {
  1964. os_memcpy(reply, "PONG\n", 5);
  1965. reply_len = 5;
  1966. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1967. if (wpa_debug_reopen_file() < 0)
  1968. reply_len = -1;
  1969. } else if (os_strcmp(buf, "STATUS") == 0) {
  1970. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  1971. reply_size);
  1972. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  1973. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  1974. } else if (os_strcmp(buf, "MIB") == 0) {
  1975. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  1976. if (reply_len >= 0) {
  1977. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  1978. reply_size - reply_len);
  1979. if (res < 0)
  1980. reply_len = -1;
  1981. else
  1982. reply_len += res;
  1983. }
  1984. if (reply_len >= 0) {
  1985. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  1986. reply_size - reply_len);
  1987. if (res < 0)
  1988. reply_len = -1;
  1989. else
  1990. reply_len += res;
  1991. }
  1992. #ifndef CONFIG_NO_RADIUS
  1993. if (reply_len >= 0) {
  1994. res = radius_client_get_mib(hapd->radius,
  1995. reply + reply_len,
  1996. reply_size - reply_len);
  1997. if (res < 0)
  1998. reply_len = -1;
  1999. else
  2000. reply_len += res;
  2001. }
  2002. #endif /* CONFIG_NO_RADIUS */
  2003. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  2004. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  2005. buf + 4);
  2006. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  2007. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  2008. reply_size);
  2009. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  2010. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  2011. reply_size);
  2012. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  2013. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  2014. reply_size);
  2015. } else if (os_strcmp(buf, "ATTACH") == 0) {
  2016. if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
  2017. reply_len = -1;
  2018. } else if (os_strcmp(buf, "DETACH") == 0) {
  2019. if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
  2020. reply_len = -1;
  2021. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  2022. if (hostapd_ctrl_iface_level(hapd, from, fromlen,
  2023. buf + 6))
  2024. reply_len = -1;
  2025. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  2026. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  2027. reply_len = -1;
  2028. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  2029. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  2030. reply_len = -1;
  2031. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  2032. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  2033. reply_len = -1;
  2034. #ifdef CONFIG_TAXONOMY
  2035. } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
  2036. reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
  2037. reply, reply_size);
  2038. #endif /* CONFIG_TAXONOMY */
  2039. } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
  2040. if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
  2041. reply_len = -1;
  2042. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  2043. if (hostapd_ctrl_iface_stop_ap(hapd))
  2044. reply_len = -1;
  2045. #ifdef CONFIG_IEEE80211W
  2046. #ifdef NEED_AP_MLME
  2047. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  2048. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  2049. reply_len = -1;
  2050. #endif /* NEED_AP_MLME */
  2051. #endif /* CONFIG_IEEE80211W */
  2052. #ifdef CONFIG_WPS
  2053. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  2054. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  2055. reply_len = -1;
  2056. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  2057. reply_len = hostapd_ctrl_iface_wps_check_pin(
  2058. hapd, buf + 14, reply, reply_size);
  2059. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  2060. if (hostapd_wps_button_pushed(hapd, NULL))
  2061. reply_len = -1;
  2062. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  2063. if (hostapd_wps_cancel(hapd))
  2064. reply_len = -1;
  2065. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  2066. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  2067. reply, reply_size);
  2068. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  2069. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  2070. reply_len = -1;
  2071. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  2072. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  2073. reply_size);
  2074. #ifdef CONFIG_WPS_NFC
  2075. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  2076. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  2077. reply_len = -1;
  2078. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  2079. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  2080. hapd, buf + 21, reply, reply_size);
  2081. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  2082. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  2083. hapd, buf + 14, reply, reply_size);
  2084. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  2085. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  2086. hapd, buf + 21, reply, reply_size);
  2087. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  2088. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  2089. reply_len = -1;
  2090. #endif /* CONFIG_WPS_NFC */
  2091. #endif /* CONFIG_WPS */
  2092. #ifdef CONFIG_INTERWORKING
  2093. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  2094. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  2095. reply_len = -1;
  2096. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  2097. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  2098. reply_len = -1;
  2099. #endif /* CONFIG_INTERWORKING */
  2100. #ifdef CONFIG_HS20
  2101. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  2102. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  2103. reply_len = -1;
  2104. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  2105. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  2106. reply_len = -1;
  2107. #endif /* CONFIG_HS20 */
  2108. #ifdef CONFIG_WNM_AP
  2109. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  2110. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  2111. reply_len = -1;
  2112. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  2113. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  2114. reply_len = -1;
  2115. } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
  2116. if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
  2117. reply_len = -1;
  2118. #endif /* CONFIG_WNM_AP */
  2119. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  2120. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  2121. reply_size);
  2122. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  2123. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  2124. reply_len = -1;
  2125. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  2126. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  2127. reply_size);
  2128. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  2129. if (hostapd_ctrl_iface_enable(hapd->iface))
  2130. reply_len = -1;
  2131. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  2132. if (hostapd_ctrl_iface_reload(hapd->iface))
  2133. reply_len = -1;
  2134. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  2135. if (hostapd_ctrl_iface_disable(hapd->iface))
  2136. reply_len = -1;
  2137. } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
  2138. if (ieee802_11_set_beacon(hapd))
  2139. reply_len = -1;
  2140. #ifdef CONFIG_TESTING_OPTIONS
  2141. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  2142. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  2143. reply_len = -1;
  2144. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  2145. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  2146. reply_len = -1;
  2147. } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
  2148. if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
  2149. reply_len = -1;
  2150. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  2151. if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
  2152. reply_len = -1;
  2153. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  2154. if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
  2155. reply_len = -1;
  2156. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  2157. if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
  2158. reply_len = -1;
  2159. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  2160. if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
  2161. reply_len = -1;
  2162. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  2163. if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
  2164. reply_len = -1;
  2165. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  2166. reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
  2167. reply_size);
  2168. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  2169. if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
  2170. reply_len = -1;
  2171. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  2172. reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
  2173. #endif /* CONFIG_TESTING_OPTIONS */
  2174. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  2175. if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
  2176. reply_len = -1;
  2177. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  2178. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  2179. reply_size);
  2180. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  2181. ieee802_1x_erp_flush(hapd);
  2182. #ifdef RADIUS_SERVER
  2183. radius_server_erp_flush(hapd->radius_srv);
  2184. #endif /* RADIUS_SERVER */
  2185. } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
  2186. if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
  2187. reply_len = -1;
  2188. } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
  2189. if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
  2190. reply_len = -1;
  2191. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  2192. reply_len = hostapd_ctrl_iface_log_level(
  2193. hapd, buf + 9, reply, reply_size);
  2194. #ifdef NEED_AP_MLME
  2195. } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
  2196. reply_len = hostapd_ctrl_iface_track_sta_list(
  2197. hapd, reply, reply_size);
  2198. #endif /* NEED_AP_MLME */
  2199. } else if (os_strcmp(buf, "PMKSA") == 0) {
  2200. reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
  2201. reply_size);
  2202. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  2203. hostapd_ctrl_iface_pmksa_flush(hapd);
  2204. } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
  2205. if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
  2206. reply_len = -1;
  2207. } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
  2208. if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
  2209. reply_len = -1;
  2210. } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
  2211. if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
  2212. reply_len = -1;
  2213. } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
  2214. if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
  2215. reply_len = -1;
  2216. } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
  2217. if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
  2218. reply_len = -1;
  2219. } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
  2220. reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
  2221. reply, reply_size);
  2222. } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
  2223. reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
  2224. reply_size);
  2225. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2226. eloop_terminate();
  2227. #ifdef CONFIG_DPP
  2228. } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
  2229. res = hostapd_dpp_qr_code(hapd, buf + 12);
  2230. if (res < 0) {
  2231. reply_len = -1;
  2232. } else {
  2233. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2234. if (os_snprintf_error(reply_size, reply_len))
  2235. reply_len = -1;
  2236. }
  2237. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
  2238. res = hostapd_dpp_bootstrap_gen(hapd, buf + 18);
  2239. if (res < 0) {
  2240. reply_len = -1;
  2241. } else {
  2242. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2243. if (os_snprintf_error(reply_size, reply_len))
  2244. reply_len = -1;
  2245. }
  2246. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
  2247. if (hostapd_dpp_bootstrap_remove(hapd, buf + 21) < 0)
  2248. reply_len = -1;
  2249. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
  2250. const char *uri;
  2251. uri = hostapd_dpp_bootstrap_get_uri(hapd, atoi(buf + 22));
  2252. if (!uri) {
  2253. reply_len = -1;
  2254. } else {
  2255. reply_len = os_snprintf(reply, reply_size, "%s", uri);
  2256. if (os_snprintf_error(reply_size, reply_len))
  2257. reply_len = -1;
  2258. }
  2259. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
  2260. reply_len = hostapd_dpp_bootstrap_info(hapd, atoi(buf + 19),
  2261. reply, reply_size);
  2262. } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
  2263. if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
  2264. reply_len = -1;
  2265. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
  2266. res = hostapd_dpp_configurator_add(hapd, buf + 20);
  2267. if (res < 0) {
  2268. reply_len = -1;
  2269. } else {
  2270. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2271. if (os_snprintf_error(reply_size, reply_len))
  2272. reply_len = -1;
  2273. }
  2274. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
  2275. if (hostapd_dpp_configurator_remove(hapd, buf + 24) < 0)
  2276. reply_len = -1;
  2277. } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
  2278. res = hostapd_dpp_pkex_add(hapd, buf + 12);
  2279. if (res < 0) {
  2280. reply_len = -1;
  2281. } else {
  2282. reply_len = os_snprintf(reply, reply_size, "%d", res);
  2283. if (os_snprintf_error(reply_size, reply_len))
  2284. reply_len = -1;
  2285. }
  2286. } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
  2287. if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
  2288. reply_len = -1;
  2289. #endif /* CONFIG_DPP */
  2290. } else {
  2291. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2292. reply_len = 16;
  2293. }
  2294. if (reply_len < 0) {
  2295. os_memcpy(reply, "FAIL\n", 5);
  2296. reply_len = 5;
  2297. }
  2298. return reply_len;
  2299. }
  2300. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  2301. void *sock_ctx)
  2302. {
  2303. struct hostapd_data *hapd = eloop_ctx;
  2304. char buf[4096];
  2305. int res;
  2306. struct sockaddr_storage from;
  2307. socklen_t fromlen = sizeof(from);
  2308. char *reply, *pos = buf;
  2309. const int reply_size = 4096;
  2310. int reply_len;
  2311. int level = MSG_DEBUG;
  2312. #ifdef CONFIG_CTRL_IFACE_UDP
  2313. unsigned char lcookie[COOKIE_LEN];
  2314. #endif /* CONFIG_CTRL_IFACE_UDP */
  2315. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  2316. (struct sockaddr *) &from, &fromlen);
  2317. if (res < 0) {
  2318. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  2319. strerror(errno));
  2320. return;
  2321. }
  2322. buf[res] = '\0';
  2323. reply = os_malloc(reply_size);
  2324. if (reply == NULL) {
  2325. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  2326. fromlen) < 0) {
  2327. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2328. strerror(errno));
  2329. }
  2330. return;
  2331. }
  2332. #ifdef CONFIG_CTRL_IFACE_UDP
  2333. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  2334. os_memcpy(reply, "COOKIE=", 7);
  2335. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  2336. cookie, COOKIE_LEN);
  2337. reply_len = 7 + 2 * COOKIE_LEN;
  2338. goto done;
  2339. }
  2340. if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
  2341. hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
  2342. wpa_printf(MSG_DEBUG,
  2343. "CTRL: No cookie in the request - drop request");
  2344. os_free(reply);
  2345. return;
  2346. }
  2347. if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
  2348. wpa_printf(MSG_DEBUG,
  2349. "CTRL: Invalid cookie in the request - drop request");
  2350. os_free(reply);
  2351. return;
  2352. }
  2353. pos = buf + 7 + 2 * COOKIE_LEN;
  2354. while (*pos == ' ')
  2355. pos++;
  2356. #endif /* CONFIG_CTRL_IFACE_UDP */
  2357. if (os_strcmp(pos, "PING") == 0)
  2358. level = MSG_EXCESSIVE;
  2359. wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
  2360. reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
  2361. reply, reply_size,
  2362. &from, fromlen);
  2363. #ifdef CONFIG_CTRL_IFACE_UDP
  2364. done:
  2365. #endif /* CONFIG_CTRL_IFACE_UDP */
  2366. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  2367. fromlen) < 0) {
  2368. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2369. strerror(errno));
  2370. }
  2371. os_free(reply);
  2372. }
  2373. #ifndef CONFIG_CTRL_IFACE_UDP
  2374. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  2375. {
  2376. char *buf;
  2377. size_t len;
  2378. if (hapd->conf->ctrl_interface == NULL)
  2379. return NULL;
  2380. len = os_strlen(hapd->conf->ctrl_interface) +
  2381. os_strlen(hapd->conf->iface) + 2;
  2382. buf = os_malloc(len);
  2383. if (buf == NULL)
  2384. return NULL;
  2385. os_snprintf(buf, len, "%s/%s",
  2386. hapd->conf->ctrl_interface, hapd->conf->iface);
  2387. buf[len - 1] = '\0';
  2388. return buf;
  2389. }
  2390. #endif /* CONFIG_CTRL_IFACE_UDP */
  2391. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  2392. enum wpa_msg_type type,
  2393. const char *txt, size_t len)
  2394. {
  2395. struct hostapd_data *hapd = ctx;
  2396. if (hapd == NULL)
  2397. return;
  2398. hostapd_ctrl_iface_send(hapd, level, type, txt, len);
  2399. }
  2400. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  2401. {
  2402. #ifdef CONFIG_CTRL_IFACE_UDP
  2403. int port = HOSTAPD_CTRL_IFACE_PORT;
  2404. char p[32] = { 0 };
  2405. char port_str[40], *tmp;
  2406. char *pos;
  2407. struct addrinfo hints = { 0 }, *res, *saveres;
  2408. int n;
  2409. if (hapd->ctrl_sock > -1) {
  2410. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2411. return 0;
  2412. }
  2413. if (hapd->conf->ctrl_interface == NULL)
  2414. return 0;
  2415. pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
  2416. if (pos) {
  2417. pos += 4;
  2418. port = atoi(pos);
  2419. if (port <= 0) {
  2420. wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
  2421. goto fail;
  2422. }
  2423. }
  2424. dl_list_init(&hapd->ctrl_dst);
  2425. hapd->ctrl_sock = -1;
  2426. os_get_random(cookie, COOKIE_LEN);
  2427. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  2428. hints.ai_flags = AI_PASSIVE;
  2429. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  2430. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  2431. hints.ai_family = AF_INET6;
  2432. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2433. hints.ai_family = AF_INET;
  2434. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  2435. hints.ai_socktype = SOCK_DGRAM;
  2436. try_again:
  2437. os_snprintf(p, sizeof(p), "%d", port);
  2438. n = getaddrinfo(NULL, p, &hints, &res);
  2439. if (n) {
  2440. wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
  2441. goto fail;
  2442. }
  2443. saveres = res;
  2444. hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
  2445. res->ai_protocol);
  2446. if (hapd->ctrl_sock < 0) {
  2447. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  2448. goto fail;
  2449. }
  2450. if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
  2451. port--;
  2452. if ((HOSTAPD_CTRL_IFACE_PORT - port) <
  2453. HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
  2454. goto try_again;
  2455. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  2456. goto fail;
  2457. }
  2458. freeaddrinfo(saveres);
  2459. os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
  2460. tmp = os_strdup(port_str);
  2461. if (tmp) {
  2462. os_free(hapd->conf->ctrl_interface);
  2463. hapd->conf->ctrl_interface = tmp;
  2464. }
  2465. wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
  2466. if (eloop_register_read_sock(hapd->ctrl_sock,
  2467. hostapd_ctrl_iface_receive, hapd, NULL) <
  2468. 0) {
  2469. hostapd_ctrl_iface_deinit(hapd);
  2470. return -1;
  2471. }
  2472. hapd->msg_ctx = hapd;
  2473. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  2474. return 0;
  2475. fail:
  2476. if (hapd->ctrl_sock >= 0)
  2477. close(hapd->ctrl_sock);
  2478. return -1;
  2479. #else /* CONFIG_CTRL_IFACE_UDP */
  2480. struct sockaddr_un addr;
  2481. int s = -1;
  2482. char *fname = NULL;
  2483. if (hapd->ctrl_sock > -1) {
  2484. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  2485. return 0;
  2486. }
  2487. dl_list_init(&hapd->ctrl_dst);
  2488. if (hapd->conf->ctrl_interface == NULL)
  2489. return 0;
  2490. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2491. if (errno == EEXIST) {
  2492. wpa_printf(MSG_DEBUG, "Using existing control "
  2493. "interface directory.");
  2494. } else {
  2495. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2496. strerror(errno));
  2497. goto fail;
  2498. }
  2499. }
  2500. if (hapd->conf->ctrl_interface_gid_set &&
  2501. chown(hapd->conf->ctrl_interface, -1,
  2502. hapd->conf->ctrl_interface_gid) < 0) {
  2503. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2504. strerror(errno));
  2505. return -1;
  2506. }
  2507. if (!hapd->conf->ctrl_interface_gid_set &&
  2508. hapd->iface->interfaces->ctrl_iface_group &&
  2509. chown(hapd->conf->ctrl_interface, -1,
  2510. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2511. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2512. strerror(errno));
  2513. return -1;
  2514. }
  2515. #ifdef ANDROID
  2516. /*
  2517. * Android is using umask 0077 which would leave the control interface
  2518. * directory without group access. This breaks things since Wi-Fi
  2519. * framework assumes that this directory can be accessed by other
  2520. * applications in the wifi group. Fix this by adding group access even
  2521. * if umask value would prevent this.
  2522. */
  2523. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2524. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  2525. strerror(errno));
  2526. /* Try to continue anyway */
  2527. }
  2528. #endif /* ANDROID */
  2529. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  2530. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  2531. goto fail;
  2532. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2533. if (s < 0) {
  2534. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2535. goto fail;
  2536. }
  2537. os_memset(&addr, 0, sizeof(addr));
  2538. #ifdef __FreeBSD__
  2539. addr.sun_len = sizeof(addr);
  2540. #endif /* __FreeBSD__ */
  2541. addr.sun_family = AF_UNIX;
  2542. fname = hostapd_ctrl_iface_path(hapd);
  2543. if (fname == NULL)
  2544. goto fail;
  2545. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2546. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2547. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2548. strerror(errno));
  2549. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2550. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2551. " allow connections - assuming it was left"
  2552. "over from forced program termination");
  2553. if (unlink(fname) < 0) {
  2554. wpa_printf(MSG_ERROR,
  2555. "Could not unlink existing ctrl_iface socket '%s': %s",
  2556. fname, strerror(errno));
  2557. goto fail;
  2558. }
  2559. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2560. 0) {
  2561. wpa_printf(MSG_ERROR,
  2562. "hostapd-ctrl-iface: bind(PF_UNIX): %s",
  2563. strerror(errno));
  2564. goto fail;
  2565. }
  2566. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2567. "ctrl_iface socket '%s'", fname);
  2568. } else {
  2569. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2570. "be in use - cannot override it");
  2571. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2572. "not used anymore", fname);
  2573. os_free(fname);
  2574. fname = NULL;
  2575. goto fail;
  2576. }
  2577. }
  2578. if (hapd->conf->ctrl_interface_gid_set &&
  2579. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  2580. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  2581. strerror(errno));
  2582. goto fail;
  2583. }
  2584. if (!hapd->conf->ctrl_interface_gid_set &&
  2585. hapd->iface->interfaces->ctrl_iface_group &&
  2586. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2587. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  2588. strerror(errno));
  2589. goto fail;
  2590. }
  2591. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  2592. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  2593. strerror(errno));
  2594. goto fail;
  2595. }
  2596. os_free(fname);
  2597. hapd->ctrl_sock = s;
  2598. if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  2599. NULL) < 0) {
  2600. hostapd_ctrl_iface_deinit(hapd);
  2601. return -1;
  2602. }
  2603. hapd->msg_ctx = hapd;
  2604. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  2605. return 0;
  2606. fail:
  2607. if (s >= 0)
  2608. close(s);
  2609. if (fname) {
  2610. unlink(fname);
  2611. os_free(fname);
  2612. }
  2613. return -1;
  2614. #endif /* CONFIG_CTRL_IFACE_UDP */
  2615. }
  2616. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  2617. {
  2618. struct wpa_ctrl_dst *dst, *prev;
  2619. if (hapd->ctrl_sock > -1) {
  2620. #ifndef CONFIG_CTRL_IFACE_UDP
  2621. char *fname;
  2622. #endif /* !CONFIG_CTRL_IFACE_UDP */
  2623. eloop_unregister_read_sock(hapd->ctrl_sock);
  2624. close(hapd->ctrl_sock);
  2625. hapd->ctrl_sock = -1;
  2626. #ifndef CONFIG_CTRL_IFACE_UDP
  2627. fname = hostapd_ctrl_iface_path(hapd);
  2628. if (fname)
  2629. unlink(fname);
  2630. os_free(fname);
  2631. if (hapd->conf->ctrl_interface &&
  2632. rmdir(hapd->conf->ctrl_interface) < 0) {
  2633. if (errno == ENOTEMPTY) {
  2634. wpa_printf(MSG_DEBUG, "Control interface "
  2635. "directory not empty - leaving it "
  2636. "behind");
  2637. } else {
  2638. wpa_printf(MSG_ERROR,
  2639. "rmdir[ctrl_interface=%s]: %s",
  2640. hapd->conf->ctrl_interface,
  2641. strerror(errno));
  2642. }
  2643. }
  2644. #endif /* !CONFIG_CTRL_IFACE_UDP */
  2645. }
  2646. dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
  2647. list)
  2648. os_free(dst);
  2649. #ifdef CONFIG_TESTING_OPTIONS
  2650. l2_packet_deinit(hapd->l2_test);
  2651. hapd->l2_test = NULL;
  2652. #endif /* CONFIG_TESTING_OPTIONS */
  2653. }
  2654. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  2655. char *buf)
  2656. {
  2657. if (hostapd_add_iface(interfaces, buf) < 0) {
  2658. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  2659. return -1;
  2660. }
  2661. return 0;
  2662. }
  2663. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  2664. char *buf)
  2665. {
  2666. if (hostapd_remove_iface(interfaces, buf) < 0) {
  2667. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  2668. return -1;
  2669. }
  2670. return 0;
  2671. }
  2672. static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
  2673. struct sockaddr_storage *from,
  2674. socklen_t fromlen)
  2675. {
  2676. return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen);
  2677. }
  2678. static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
  2679. struct sockaddr_storage *from,
  2680. socklen_t fromlen)
  2681. {
  2682. return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
  2683. }
  2684. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  2685. {
  2686. #ifdef CONFIG_WPS_TESTING
  2687. wps_version_number = 0x20;
  2688. wps_testing_dummy_cred = 0;
  2689. wps_corrupt_pkhash = 0;
  2690. #endif /* CONFIG_WPS_TESTING */
  2691. }
  2692. #ifdef CONFIG_FST
  2693. static int
  2694. hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
  2695. const char *cmd)
  2696. {
  2697. char ifname[IFNAMSIZ + 1];
  2698. struct fst_iface_cfg cfg;
  2699. struct hostapd_data *hapd;
  2700. struct fst_wpa_obj iface_obj;
  2701. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  2702. hapd = hostapd_get_iface(interfaces, ifname);
  2703. if (hapd) {
  2704. if (hapd->iface->fst) {
  2705. wpa_printf(MSG_INFO, "FST: Already attached");
  2706. return -1;
  2707. }
  2708. fst_hostapd_fill_iface_obj(hapd, &iface_obj);
  2709. hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
  2710. &iface_obj, &cfg);
  2711. if (hapd->iface->fst)
  2712. return 0;
  2713. }
  2714. }
  2715. return -EINVAL;
  2716. }
  2717. static int
  2718. hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
  2719. const char *cmd)
  2720. {
  2721. char ifname[IFNAMSIZ + 1];
  2722. struct hostapd_data * hapd;
  2723. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  2724. hapd = hostapd_get_iface(interfaces, ifname);
  2725. if (hapd) {
  2726. if (!fst_iface_detach(ifname)) {
  2727. hapd->iface->fst = NULL;
  2728. hapd->iface->fst_ies = NULL;
  2729. return 0;
  2730. }
  2731. }
  2732. }
  2733. return -EINVAL;
  2734. }
  2735. #endif /* CONFIG_FST */
  2736. static struct hostapd_data *
  2737. hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
  2738. const char *ifname)
  2739. {
  2740. size_t i, j;
  2741. for (i = 0; i < interfaces->count; i++) {
  2742. struct hostapd_iface *iface = interfaces->iface[i];
  2743. for (j = 0; j < iface->num_bss; j++) {
  2744. struct hostapd_data *hapd;
  2745. hapd = iface->bss[j];
  2746. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  2747. return hapd;
  2748. }
  2749. }
  2750. return NULL;
  2751. }
  2752. static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
  2753. struct hostapd_data *dst_hapd,
  2754. const char *param)
  2755. {
  2756. int res;
  2757. char *value;
  2758. value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  2759. if (!value) {
  2760. wpa_printf(MSG_ERROR,
  2761. "DUP: cannot allocate buffer to stringify %s",
  2762. param);
  2763. goto error_return;
  2764. }
  2765. if (os_strcmp(param, "wpa") == 0) {
  2766. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
  2767. src_hapd->conf->wpa);
  2768. } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
  2769. src_hapd->conf->wpa_key_mgmt) {
  2770. res = hostapd_ctrl_iface_get_key_mgmt(
  2771. src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  2772. if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
  2773. goto error_stringify;
  2774. } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
  2775. src_hapd->conf->wpa_pairwise) {
  2776. res = wpa_write_ciphers(value,
  2777. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2778. src_hapd->conf->wpa_pairwise, " ");
  2779. if (res < 0)
  2780. goto error_stringify;
  2781. } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
  2782. src_hapd->conf->rsn_pairwise) {
  2783. res = wpa_write_ciphers(value,
  2784. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2785. src_hapd->conf->rsn_pairwise, " ");
  2786. if (res < 0)
  2787. goto error_stringify;
  2788. } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
  2789. src_hapd->conf->ssid.wpa_passphrase) {
  2790. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
  2791. src_hapd->conf->ssid.wpa_passphrase);
  2792. } else if (os_strcmp(param, "wpa_psk") == 0 &&
  2793. src_hapd->conf->ssid.wpa_psk_set) {
  2794. wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2795. src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  2796. } else {
  2797. wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
  2798. goto error_return;
  2799. }
  2800. res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
  2801. os_free(value);
  2802. return res;
  2803. error_stringify:
  2804. wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
  2805. error_return:
  2806. os_free(value);
  2807. return -1;
  2808. }
  2809. static int
  2810. hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
  2811. const char *input,
  2812. char *reply, int reply_size)
  2813. {
  2814. size_t i, j;
  2815. int res;
  2816. char *pos, *end;
  2817. struct hostapd_iface *iface;
  2818. int show_ctrl = 0;
  2819. if (input)
  2820. show_ctrl = !!os_strstr(input, "ctrl");
  2821. pos = reply;
  2822. end = reply + reply_size;
  2823. for (i = 0; i < interfaces->count; i++) {
  2824. iface = interfaces->iface[i];
  2825. for (j = 0; j < iface->num_bss; j++) {
  2826. struct hostapd_bss_config *conf;
  2827. conf = iface->conf->bss[j];
  2828. if (show_ctrl)
  2829. res = os_snprintf(pos, end - pos,
  2830. "%s ctrl_iface=%s\n",
  2831. conf->iface,
  2832. conf->ctrl_interface ?
  2833. conf->ctrl_interface : "N/A");
  2834. else
  2835. res = os_snprintf(pos, end - pos, "%s\n",
  2836. conf->iface);
  2837. if (os_snprintf_error(end - pos, res)) {
  2838. *pos = '\0';
  2839. return pos - reply;
  2840. }
  2841. pos += res;
  2842. }
  2843. }
  2844. return pos - reply;
  2845. }
  2846. static int
  2847. hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
  2848. char *cmd)
  2849. {
  2850. char *p_start = cmd, *p_end;
  2851. struct hostapd_data *src_hapd, *dst_hapd;
  2852. /* cmd: "<src ifname> <dst ifname> <variable name> */
  2853. p_end = os_strchr(p_start, ' ');
  2854. if (!p_end) {
  2855. wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
  2856. cmd);
  2857. return -1;
  2858. }
  2859. *p_end = '\0';
  2860. src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  2861. if (!src_hapd) {
  2862. wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
  2863. p_start);
  2864. return -1;
  2865. }
  2866. p_start = p_end + 1;
  2867. p_end = os_strchr(p_start, ' ');
  2868. if (!p_end) {
  2869. wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
  2870. cmd);
  2871. return -1;
  2872. }
  2873. *p_end = '\0';
  2874. dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  2875. if (!dst_hapd) {
  2876. wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
  2877. p_start);
  2878. return -1;
  2879. }
  2880. p_start = p_end + 1;
  2881. return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
  2882. }
  2883. static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
  2884. const char *ifname,
  2885. char *buf, char *reply,
  2886. int reply_size,
  2887. struct sockaddr_storage *from,
  2888. socklen_t fromlen)
  2889. {
  2890. struct hostapd_data *hapd;
  2891. hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
  2892. if (hapd == NULL) {
  2893. int res;
  2894. res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
  2895. if (os_snprintf_error(reply_size, res))
  2896. return -1;
  2897. return res;
  2898. }
  2899. return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
  2900. from, fromlen);
  2901. }
  2902. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  2903. void *sock_ctx)
  2904. {
  2905. void *interfaces = eloop_ctx;
  2906. char buffer[256], *buf = buffer;
  2907. int res;
  2908. struct sockaddr_storage from;
  2909. socklen_t fromlen = sizeof(from);
  2910. char *reply;
  2911. int reply_len;
  2912. const int reply_size = 4096;
  2913. #ifdef CONFIG_CTRL_IFACE_UDP
  2914. unsigned char lcookie[COOKIE_LEN];
  2915. #endif /* CONFIG_CTRL_IFACE_UDP */
  2916. res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
  2917. (struct sockaddr *) &from, &fromlen);
  2918. if (res < 0) {
  2919. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  2920. strerror(errno));
  2921. return;
  2922. }
  2923. buf[res] = '\0';
  2924. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  2925. reply = os_malloc(reply_size);
  2926. if (reply == NULL) {
  2927. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  2928. fromlen) < 0) {
  2929. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2930. strerror(errno));
  2931. }
  2932. return;
  2933. }
  2934. os_memcpy(reply, "OK\n", 3);
  2935. reply_len = 3;
  2936. #ifdef CONFIG_CTRL_IFACE_UDP
  2937. if (os_strcmp(buf, "GET_COOKIE") == 0) {
  2938. os_memcpy(reply, "COOKIE=", 7);
  2939. wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
  2940. gcookie, COOKIE_LEN);
  2941. reply_len = 7 + 2 * COOKIE_LEN;
  2942. goto send_reply;
  2943. }
  2944. if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
  2945. hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
  2946. wpa_printf(MSG_DEBUG,
  2947. "CTRL: No cookie in the request - drop request");
  2948. os_free(reply);
  2949. return;
  2950. }
  2951. if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
  2952. wpa_printf(MSG_DEBUG,
  2953. "CTRL: Invalid cookie in the request - drop request");
  2954. os_free(reply);
  2955. return;
  2956. }
  2957. buf += 7 + 2 * COOKIE_LEN;
  2958. while (*buf == ' ')
  2959. buf++;
  2960. #endif /* CONFIG_CTRL_IFACE_UDP */
  2961. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  2962. char *pos = os_strchr(buf + 7, ' ');
  2963. if (pos) {
  2964. *pos++ = '\0';
  2965. reply_len = hostapd_global_ctrl_iface_ifname(
  2966. interfaces, buf + 7, pos, reply, reply_size,
  2967. &from, fromlen);
  2968. goto send_reply;
  2969. }
  2970. }
  2971. if (os_strcmp(buf, "PING") == 0) {
  2972. os_memcpy(reply, "PONG\n", 5);
  2973. reply_len = 5;
  2974. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  2975. if (wpa_debug_reopen_file() < 0)
  2976. reply_len = -1;
  2977. } else if (os_strcmp(buf, "FLUSH") == 0) {
  2978. hostapd_ctrl_iface_flush(interfaces);
  2979. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  2980. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  2981. reply_len = -1;
  2982. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  2983. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  2984. reply_len = -1;
  2985. } else if (os_strcmp(buf, "ATTACH") == 0) {
  2986. if (hostapd_global_ctrl_iface_attach(interfaces, &from,
  2987. fromlen))
  2988. reply_len = -1;
  2989. } else if (os_strcmp(buf, "DETACH") == 0) {
  2990. if (hostapd_global_ctrl_iface_detach(interfaces, &from,
  2991. fromlen))
  2992. reply_len = -1;
  2993. #ifdef CONFIG_MODULE_TESTS
  2994. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  2995. if (hapd_module_tests() < 0)
  2996. reply_len = -1;
  2997. #endif /* CONFIG_MODULE_TESTS */
  2998. #ifdef CONFIG_FST
  2999. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  3000. if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
  3001. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3002. else
  3003. reply_len = -1;
  3004. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  3005. if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
  3006. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3007. else
  3008. reply_len = -1;
  3009. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  3010. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  3011. #endif /* CONFIG_FST */
  3012. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  3013. if (!hostapd_global_ctrl_iface_dup_network(interfaces,
  3014. buf + 12))
  3015. reply_len = os_snprintf(reply, reply_size, "OK\n");
  3016. else
  3017. reply_len = -1;
  3018. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  3019. reply_len = hostapd_global_ctrl_iface_interfaces(
  3020. interfaces, buf + 10, reply, sizeof(buffer));
  3021. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  3022. eloop_terminate();
  3023. } else {
  3024. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  3025. "ignored");
  3026. reply_len = -1;
  3027. }
  3028. send_reply:
  3029. if (reply_len < 0) {
  3030. os_memcpy(reply, "FAIL\n", 5);
  3031. reply_len = 5;
  3032. }
  3033. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  3034. fromlen) < 0) {
  3035. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  3036. strerror(errno));
  3037. }
  3038. os_free(reply);
  3039. }
  3040. #ifndef CONFIG_CTRL_IFACE_UDP
  3041. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  3042. {
  3043. char *buf;
  3044. size_t len;
  3045. if (interface->global_iface_path == NULL)
  3046. return NULL;
  3047. len = os_strlen(interface->global_iface_path) +
  3048. os_strlen(interface->global_iface_name) + 2;
  3049. buf = os_malloc(len);
  3050. if (buf == NULL)
  3051. return NULL;
  3052. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  3053. interface->global_iface_name);
  3054. buf[len - 1] = '\0';
  3055. return buf;
  3056. }
  3057. #endif /* CONFIG_CTRL_IFACE_UDP */
  3058. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  3059. {
  3060. #ifdef CONFIG_CTRL_IFACE_UDP
  3061. int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
  3062. char p[32] = { 0 };
  3063. char *pos;
  3064. struct addrinfo hints = { 0 }, *res, *saveres;
  3065. int n;
  3066. if (interface->global_ctrl_sock > -1) {
  3067. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  3068. return 0;
  3069. }
  3070. if (interface->global_iface_path == NULL)
  3071. return 0;
  3072. pos = os_strstr(interface->global_iface_path, "udp:");
  3073. if (pos) {
  3074. pos += 4;
  3075. port = atoi(pos);
  3076. if (port <= 0) {
  3077. wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
  3078. goto fail;
  3079. }
  3080. }
  3081. os_get_random(gcookie, COOKIE_LEN);
  3082. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  3083. hints.ai_flags = AI_PASSIVE;
  3084. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  3085. #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
  3086. hints.ai_family = AF_INET6;
  3087. #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  3088. hints.ai_family = AF_INET;
  3089. #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
  3090. hints.ai_socktype = SOCK_DGRAM;
  3091. try_again:
  3092. os_snprintf(p, sizeof(p), "%d", port);
  3093. n = getaddrinfo(NULL, p, &hints, &res);
  3094. if (n) {
  3095. wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
  3096. goto fail;
  3097. }
  3098. saveres = res;
  3099. interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
  3100. res->ai_protocol);
  3101. if (interface->global_ctrl_sock < 0) {
  3102. wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
  3103. goto fail;
  3104. }
  3105. if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
  3106. 0) {
  3107. port++;
  3108. if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
  3109. HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
  3110. goto try_again;
  3111. wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
  3112. goto fail;
  3113. }
  3114. freeaddrinfo(saveres);
  3115. wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
  3116. if (eloop_register_read_sock(interface->global_ctrl_sock,
  3117. hostapd_global_ctrl_iface_receive,
  3118. interface, NULL) < 0) {
  3119. hostapd_global_ctrl_iface_deinit(interface);
  3120. return -1;
  3121. }
  3122. return 0;
  3123. fail:
  3124. if (interface->global_ctrl_sock >= 0)
  3125. close(interface->global_ctrl_sock);
  3126. return -1;
  3127. #else /* CONFIG_CTRL_IFACE_UDP */
  3128. struct sockaddr_un addr;
  3129. int s = -1;
  3130. char *fname = NULL;
  3131. if (interface->global_iface_path == NULL) {
  3132. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  3133. return 0;
  3134. }
  3135. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  3136. if (errno == EEXIST) {
  3137. wpa_printf(MSG_DEBUG, "Using existing control "
  3138. "interface directory.");
  3139. } else {
  3140. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  3141. strerror(errno));
  3142. goto fail;
  3143. }
  3144. } else if (interface->ctrl_iface_group &&
  3145. chown(interface->global_iface_path, -1,
  3146. interface->ctrl_iface_group) < 0) {
  3147. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  3148. strerror(errno));
  3149. goto fail;
  3150. }
  3151. if (os_strlen(interface->global_iface_path) + 1 +
  3152. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  3153. goto fail;
  3154. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  3155. if (s < 0) {
  3156. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  3157. goto fail;
  3158. }
  3159. os_memset(&addr, 0, sizeof(addr));
  3160. #ifdef __FreeBSD__
  3161. addr.sun_len = sizeof(addr);
  3162. #endif /* __FreeBSD__ */
  3163. addr.sun_family = AF_UNIX;
  3164. fname = hostapd_global_ctrl_iface_path(interface);
  3165. if (fname == NULL)
  3166. goto fail;
  3167. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  3168. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  3169. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  3170. strerror(errno));
  3171. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  3172. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  3173. " allow connections - assuming it was left"
  3174. "over from forced program termination");
  3175. if (unlink(fname) < 0) {
  3176. wpa_printf(MSG_ERROR,
  3177. "Could not unlink existing ctrl_iface socket '%s': %s",
  3178. fname, strerror(errno));
  3179. goto fail;
  3180. }
  3181. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  3182. 0) {
  3183. wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
  3184. strerror(errno));
  3185. goto fail;
  3186. }
  3187. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  3188. "ctrl_iface socket '%s'", fname);
  3189. } else {
  3190. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  3191. "be in use - cannot override it");
  3192. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  3193. "not used anymore", fname);
  3194. os_free(fname);
  3195. fname = NULL;
  3196. goto fail;
  3197. }
  3198. }
  3199. if (interface->ctrl_iface_group &&
  3200. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  3201. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  3202. strerror(errno));
  3203. goto fail;
  3204. }
  3205. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  3206. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  3207. strerror(errno));
  3208. goto fail;
  3209. }
  3210. os_free(fname);
  3211. interface->global_ctrl_sock = s;
  3212. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  3213. interface, NULL);
  3214. return 0;
  3215. fail:
  3216. if (s >= 0)
  3217. close(s);
  3218. if (fname) {
  3219. unlink(fname);
  3220. os_free(fname);
  3221. }
  3222. return -1;
  3223. #endif /* CONFIG_CTRL_IFACE_UDP */
  3224. }
  3225. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  3226. {
  3227. #ifndef CONFIG_CTRL_IFACE_UDP
  3228. char *fname = NULL;
  3229. #endif /* CONFIG_CTRL_IFACE_UDP */
  3230. struct wpa_ctrl_dst *dst, *prev;
  3231. if (interfaces->global_ctrl_sock > -1) {
  3232. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  3233. close(interfaces->global_ctrl_sock);
  3234. interfaces->global_ctrl_sock = -1;
  3235. #ifndef CONFIG_CTRL_IFACE_UDP
  3236. fname = hostapd_global_ctrl_iface_path(interfaces);
  3237. if (fname) {
  3238. unlink(fname);
  3239. os_free(fname);
  3240. }
  3241. if (interfaces->global_iface_path &&
  3242. rmdir(interfaces->global_iface_path) < 0) {
  3243. if (errno == ENOTEMPTY) {
  3244. wpa_printf(MSG_DEBUG, "Control interface "
  3245. "directory not empty - leaving it "
  3246. "behind");
  3247. } else {
  3248. wpa_printf(MSG_ERROR,
  3249. "rmdir[ctrl_interface=%s]: %s",
  3250. interfaces->global_iface_path,
  3251. strerror(errno));
  3252. }
  3253. }
  3254. #endif /* CONFIG_CTRL_IFACE_UDP */
  3255. }
  3256. os_free(interfaces->global_iface_path);
  3257. interfaces->global_iface_path = NULL;
  3258. dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
  3259. struct wpa_ctrl_dst, list)
  3260. os_free(dst);
  3261. }
  3262. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  3263. enum wpa_msg_type type,
  3264. const char *buf, size_t len)
  3265. {
  3266. struct wpa_ctrl_dst *dst, *next;
  3267. struct dl_list *ctrl_dst;
  3268. struct msghdr msg;
  3269. int idx;
  3270. struct iovec io[2];
  3271. char levelstr[10];
  3272. int s;
  3273. if (type != WPA_MSG_ONLY_GLOBAL) {
  3274. s = hapd->ctrl_sock;
  3275. ctrl_dst = &hapd->ctrl_dst;
  3276. } else {
  3277. s = hapd->iface->interfaces->global_ctrl_sock;
  3278. ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
  3279. }
  3280. if (s < 0 || dl_list_empty(ctrl_dst))
  3281. return;
  3282. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  3283. io[0].iov_base = levelstr;
  3284. io[0].iov_len = os_strlen(levelstr);
  3285. io[1].iov_base = (char *) buf;
  3286. io[1].iov_len = len;
  3287. os_memset(&msg, 0, sizeof(msg));
  3288. msg.msg_iov = io;
  3289. msg.msg_iovlen = 2;
  3290. idx = 0;
  3291. dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
  3292. if (level >= dst->debug_level) {
  3293. sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
  3294. &dst->addr, dst->addrlen);
  3295. msg.msg_name = &dst->addr;
  3296. msg.msg_namelen = dst->addrlen;
  3297. if (sendmsg(s, &msg, 0) < 0) {
  3298. int _errno = errno;
  3299. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  3300. "%d - %s",
  3301. idx, errno, strerror(errno));
  3302. dst->errors++;
  3303. if (dst->errors > 10 || _errno == ENOENT) {
  3304. if (type != WPA_MSG_ONLY_GLOBAL)
  3305. hostapd_ctrl_iface_detach(
  3306. hapd, &dst->addr,
  3307. dst->addrlen);
  3308. else
  3309. hostapd_global_ctrl_iface_detach(
  3310. hapd->iface->interfaces,
  3311. &dst->addr,
  3312. dst->addrlen);
  3313. }
  3314. } else
  3315. dst->errors = 0;
  3316. }
  3317. idx++;
  3318. }
  3319. }
  3320. #endif /* CONFIG_NATIVE_WINDOWS */