ctrl_iface.c 97 KB

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