ctrl_iface.c 91 KB

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