ctrl_iface.c 89 KB

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