ctrl_iface.c 101 KB

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