ctrl_iface.c 91 KB

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