ctrl_iface.c 101 KB

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