ctrl_iface.c 87 KB

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