ctrl_iface.c 77 KB

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