ctrl_iface.c 91 KB

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