ctrl_iface.c 93 KB

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