ctrl_iface.c 92 KB

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