ctrl_iface.c 89 KB

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