ctrl_iface.c 90 KB

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