ctrl_iface.c 89 KB

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