ctrl_iface.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179
  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. if (hwaddr_aton(cmd, addr)) {
  720. wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
  721. return -1;
  722. }
  723. sta = ap_get_sta(hapd, addr);
  724. if (sta == NULL) {
  725. wpa_printf(MSG_DEBUG, "Station " MACSTR
  726. " not found for BSS TM Request message",
  727. MAC2STR(addr));
  728. return -1;
  729. }
  730. pos = os_strstr(cmd, " disassoc_timer=");
  731. if (pos) {
  732. pos += 16;
  733. disassoc_timer = atoi(pos);
  734. if (disassoc_timer < 0 || disassoc_timer > 65535) {
  735. wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
  736. return -1;
  737. }
  738. }
  739. pos = os_strstr(cmd, " valid_int=");
  740. if (pos) {
  741. pos += 11;
  742. valid_int = atoi(pos);
  743. }
  744. pos = os_strstr(cmd, " bss_term=");
  745. if (pos) {
  746. pos += 10;
  747. req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
  748. /* TODO: TSF configurable/learnable */
  749. bss_term_dur[0] = 4; /* Subelement ID */
  750. bss_term_dur[1] = 10; /* Length */
  751. os_memset(bss_term_dur, 2, 8);
  752. end = os_strchr(pos, ',');
  753. if (end == NULL) {
  754. wpa_printf(MSG_DEBUG, "Invalid bss_term data");
  755. return -1;
  756. }
  757. end++;
  758. WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
  759. }
  760. /*
  761. * BSS Transition Candidate List Entries - Neighbor Report elements
  762. * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
  763. * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
  764. */
  765. pos = cmd;
  766. while (pos) {
  767. u8 *nei_start;
  768. long int val;
  769. char *endptr, *tmp;
  770. pos = os_strstr(pos, " neighbor=");
  771. if (!pos)
  772. break;
  773. if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
  774. wpa_printf(MSG_DEBUG,
  775. "Not enough room for additional neighbor");
  776. return -1;
  777. }
  778. pos += 10;
  779. nei_start = nei_pos;
  780. *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
  781. nei_pos++; /* length to be filled in */
  782. if (hwaddr_aton(pos, nei_pos)) {
  783. wpa_printf(MSG_DEBUG, "Invalid BSSID");
  784. return -1;
  785. }
  786. nei_pos += ETH_ALEN;
  787. pos += 17;
  788. if (*pos != ',') {
  789. wpa_printf(MSG_DEBUG, "Missing BSSID Information");
  790. return -1;
  791. }
  792. pos++;
  793. val = strtol(pos, &endptr, 0);
  794. WPA_PUT_LE32(nei_pos, val);
  795. nei_pos += 4;
  796. if (*endptr != ',') {
  797. wpa_printf(MSG_DEBUG, "Missing Operating Class");
  798. return -1;
  799. }
  800. pos = endptr + 1;
  801. *nei_pos++ = atoi(pos); /* Operating Class */
  802. pos = os_strchr(pos, ',');
  803. if (pos == NULL) {
  804. wpa_printf(MSG_DEBUG, "Missing Channel Number");
  805. return -1;
  806. }
  807. pos++;
  808. *nei_pos++ = atoi(pos); /* Channel Number */
  809. pos = os_strchr(pos, ',');
  810. if (pos == NULL) {
  811. wpa_printf(MSG_DEBUG, "Missing PHY Type");
  812. return -1;
  813. }
  814. pos++;
  815. *nei_pos++ = atoi(pos); /* PHY Type */
  816. end = os_strchr(pos, ' ');
  817. tmp = os_strchr(pos, ',');
  818. if (tmp && (!end || tmp < end)) {
  819. /* Optional Subelements (hexdump) */
  820. size_t len;
  821. pos = tmp + 1;
  822. end = os_strchr(pos, ' ');
  823. if (end)
  824. len = end - pos;
  825. else
  826. len = os_strlen(pos);
  827. if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
  828. wpa_printf(MSG_DEBUG,
  829. "Not enough room for neighbor subelements");
  830. return -1;
  831. }
  832. if (len & 0x01 ||
  833. hexstr2bin(pos, nei_pos, len / 2) < 0) {
  834. wpa_printf(MSG_DEBUG,
  835. "Invalid neighbor subelement info");
  836. return -1;
  837. }
  838. nei_pos += len / 2;
  839. pos = end;
  840. }
  841. nei_start[1] = nei_pos - nei_start - 2;
  842. }
  843. pos = os_strstr(cmd, " url=");
  844. if (pos) {
  845. size_t len;
  846. pos += 5;
  847. end = os_strchr(pos, ' ');
  848. if (end)
  849. len = end - pos;
  850. else
  851. len = os_strlen(pos);
  852. url = os_malloc(len + 1);
  853. if (url == NULL)
  854. return -1;
  855. os_memcpy(url, pos, len);
  856. url[len] = '\0';
  857. req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
  858. }
  859. if (os_strstr(cmd, " pref=1"))
  860. req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
  861. if (os_strstr(cmd, " abridged=1"))
  862. req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
  863. if (os_strstr(cmd, " disassoc_imminent=1"))
  864. req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
  865. ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
  866. valid_int, bss_term_dur, url,
  867. nei_pos > nei_rep ? nei_rep : NULL,
  868. nei_pos - nei_rep);
  869. os_free(url);
  870. return ret;
  871. }
  872. #endif /* CONFIG_WNM */
  873. static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
  874. char *buf, size_t buflen)
  875. {
  876. int ret = 0;
  877. char *pos, *end;
  878. pos = buf;
  879. end = buf + buflen;
  880. WPA_ASSERT(hapd->conf->wpa_key_mgmt);
  881. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  882. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  883. if (os_snprintf_error(end - pos, ret))
  884. return pos - buf;
  885. pos += ret;
  886. }
  887. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  888. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  889. if (os_snprintf_error(end - pos, ret))
  890. return pos - buf;
  891. pos += ret;
  892. }
  893. #ifdef CONFIG_IEEE80211R
  894. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  895. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  896. if (os_snprintf_error(end - pos, ret))
  897. return pos - buf;
  898. pos += ret;
  899. }
  900. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  901. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  902. if (os_snprintf_error(end - pos, ret))
  903. return pos - buf;
  904. pos += ret;
  905. }
  906. #ifdef CONFIG_SAE
  907. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  908. ret = os_snprintf(pos, end - pos, "FT-SAE ");
  909. if (os_snprintf_error(end - pos, ret))
  910. return pos - buf;
  911. pos += ret;
  912. }
  913. #endif /* CONFIG_SAE */
  914. #endif /* CONFIG_IEEE80211R */
  915. #ifdef CONFIG_IEEE80211W
  916. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  917. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  918. if (os_snprintf_error(end - pos, ret))
  919. return pos - buf;
  920. pos += ret;
  921. }
  922. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  923. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  924. if (os_snprintf_error(end - pos, ret))
  925. return pos - buf;
  926. pos += ret;
  927. }
  928. #endif /* CONFIG_IEEE80211W */
  929. #ifdef CONFIG_SAE
  930. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
  931. ret = os_snprintf(pos, end - pos, "SAE ");
  932. if (os_snprintf_error(end - pos, ret))
  933. return pos - buf;
  934. pos += ret;
  935. }
  936. #endif /* CONFIG_SAE */
  937. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  938. ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
  939. if (os_snprintf_error(end - pos, ret))
  940. return pos - buf;
  941. pos += ret;
  942. }
  943. if (hapd->conf->wpa_key_mgmt &
  944. WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  945. ret = os_snprintf(pos, end - pos,
  946. "WPA-EAP-SUITE-B-192 ");
  947. if (os_snprintf_error(end - pos, ret))
  948. return pos - buf;
  949. pos += ret;
  950. }
  951. if (pos > buf && *(pos - 1) == ' ') {
  952. *(pos - 1) = '\0';
  953. pos--;
  954. }
  955. return pos - buf;
  956. }
  957. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  958. char *buf, size_t buflen)
  959. {
  960. int ret;
  961. char *pos, *end;
  962. pos = buf;
  963. end = buf + buflen;
  964. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  965. "ssid=%s\n",
  966. MAC2STR(hapd->own_addr),
  967. wpa_ssid_txt(hapd->conf->ssid.ssid,
  968. hapd->conf->ssid.ssid_len));
  969. if (os_snprintf_error(end - pos, ret))
  970. return pos - buf;
  971. pos += ret;
  972. #ifdef CONFIG_WPS
  973. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  974. hapd->conf->wps_state == 0 ? "disabled" :
  975. (hapd->conf->wps_state == 1 ? "not configured" :
  976. "configured"));
  977. if (os_snprintf_error(end - pos, ret))
  978. return pos - buf;
  979. pos += ret;
  980. if (hapd->conf->wps_state && hapd->conf->wpa &&
  981. hapd->conf->ssid.wpa_passphrase) {
  982. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  983. hapd->conf->ssid.wpa_passphrase);
  984. if (os_snprintf_error(end - pos, ret))
  985. return pos - buf;
  986. pos += ret;
  987. }
  988. if (hapd->conf->wps_state && hapd->conf->wpa &&
  989. hapd->conf->ssid.wpa_psk &&
  990. hapd->conf->ssid.wpa_psk->group) {
  991. char hex[PMK_LEN * 2 + 1];
  992. wpa_snprintf_hex(hex, sizeof(hex),
  993. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  994. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  995. if (os_snprintf_error(end - pos, ret))
  996. return pos - buf;
  997. pos += ret;
  998. }
  999. #endif /* CONFIG_WPS */
  1000. if (hapd->conf->wpa) {
  1001. ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
  1002. if (os_snprintf_error(end - pos, ret))
  1003. return pos - buf;
  1004. pos += ret;
  1005. }
  1006. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  1007. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  1008. if (os_snprintf_error(end - pos, ret))
  1009. return pos - buf;
  1010. pos += ret;
  1011. pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
  1012. ret = os_snprintf(pos, end - pos, "\n");
  1013. if (os_snprintf_error(end - pos, ret))
  1014. return pos - buf;
  1015. pos += ret;
  1016. }
  1017. if (hapd->conf->wpa) {
  1018. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  1019. wpa_cipher_txt(hapd->conf->wpa_group));
  1020. if (os_snprintf_error(end - pos, ret))
  1021. return pos - buf;
  1022. pos += ret;
  1023. }
  1024. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  1025. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  1026. if (os_snprintf_error(end - pos, ret))
  1027. return pos - buf;
  1028. pos += ret;
  1029. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  1030. " ");
  1031. if (ret < 0)
  1032. return pos - buf;
  1033. pos += ret;
  1034. ret = os_snprintf(pos, end - pos, "\n");
  1035. if (os_snprintf_error(end - pos, ret))
  1036. return pos - buf;
  1037. pos += ret;
  1038. }
  1039. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  1040. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  1041. if (os_snprintf_error(end - pos, ret))
  1042. return pos - buf;
  1043. pos += ret;
  1044. ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
  1045. " ");
  1046. if (ret < 0)
  1047. return pos - buf;
  1048. pos += ret;
  1049. ret = os_snprintf(pos, end - pos, "\n");
  1050. if (os_snprintf_error(end - pos, ret))
  1051. return pos - buf;
  1052. pos += ret;
  1053. }
  1054. return pos - buf;
  1055. }
  1056. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  1057. {
  1058. char *value;
  1059. int ret = 0;
  1060. value = os_strchr(cmd, ' ');
  1061. if (value == NULL)
  1062. return -1;
  1063. *value++ = '\0';
  1064. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  1065. if (0) {
  1066. #ifdef CONFIG_WPS_TESTING
  1067. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  1068. long int val;
  1069. val = strtol(value, NULL, 0);
  1070. if (val < 0 || val > 0xff) {
  1071. ret = -1;
  1072. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  1073. "wps_version_number %ld", val);
  1074. } else {
  1075. wps_version_number = val;
  1076. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  1077. "version %u.%u",
  1078. (wps_version_number & 0xf0) >> 4,
  1079. wps_version_number & 0x0f);
  1080. hostapd_wps_update_ie(hapd);
  1081. }
  1082. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  1083. wps_testing_dummy_cred = atoi(value);
  1084. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  1085. wps_testing_dummy_cred);
  1086. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  1087. wps_corrupt_pkhash = atoi(value);
  1088. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  1089. wps_corrupt_pkhash);
  1090. #endif /* CONFIG_WPS_TESTING */
  1091. #ifdef CONFIG_INTERWORKING
  1092. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  1093. int val = atoi(value);
  1094. if (val <= 0)
  1095. ret = -1;
  1096. else
  1097. hapd->gas_frag_limit = val;
  1098. #endif /* CONFIG_INTERWORKING */
  1099. #ifdef CONFIG_TESTING_OPTIONS
  1100. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  1101. hapd->ext_mgmt_frame_handling = atoi(value);
  1102. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  1103. hapd->ext_eapol_frame_io = atoi(value);
  1104. #endif /* CONFIG_TESTING_OPTIONS */
  1105. } else {
  1106. struct sta_info *sta;
  1107. int vlan_id;
  1108. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  1109. if (ret)
  1110. return ret;
  1111. if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
  1112. for (sta = hapd->sta_list; sta; sta = sta->next) {
  1113. if (hostapd_maclist_found(
  1114. hapd->conf->deny_mac,
  1115. hapd->conf->num_deny_mac, sta->addr,
  1116. &vlan_id) &&
  1117. (!vlan_id || vlan_id == sta->vlan_id))
  1118. ap_sta_disconnect(
  1119. hapd, sta, sta->addr,
  1120. WLAN_REASON_UNSPECIFIED);
  1121. }
  1122. } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
  1123. os_strcasecmp(cmd, "accept_mac_file") == 0) {
  1124. for (sta = hapd->sta_list; sta; sta = sta->next) {
  1125. if (!hostapd_maclist_found(
  1126. hapd->conf->accept_mac,
  1127. hapd->conf->num_accept_mac,
  1128. sta->addr, &vlan_id) ||
  1129. (vlan_id && vlan_id != sta->vlan_id))
  1130. ap_sta_disconnect(
  1131. hapd, sta, sta->addr,
  1132. WLAN_REASON_UNSPECIFIED);
  1133. }
  1134. }
  1135. }
  1136. return ret;
  1137. }
  1138. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  1139. char *buf, size_t buflen)
  1140. {
  1141. int res;
  1142. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  1143. if (os_strcmp(cmd, "version") == 0) {
  1144. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  1145. if (os_snprintf_error(buflen, res))
  1146. return -1;
  1147. return res;
  1148. } else if (os_strcmp(cmd, "tls_library") == 0) {
  1149. res = tls_get_library_version(buf, buflen);
  1150. if (os_snprintf_error(buflen, res))
  1151. return -1;
  1152. return res;
  1153. }
  1154. return -1;
  1155. }
  1156. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  1157. {
  1158. if (hostapd_enable_iface(iface) < 0) {
  1159. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  1160. return -1;
  1161. }
  1162. return 0;
  1163. }
  1164. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  1165. {
  1166. if (hostapd_reload_iface(iface) < 0) {
  1167. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  1168. return -1;
  1169. }
  1170. return 0;
  1171. }
  1172. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  1173. {
  1174. if (hostapd_disable_iface(iface) < 0) {
  1175. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  1176. return -1;
  1177. }
  1178. return 0;
  1179. }
  1180. #ifdef CONFIG_TESTING_OPTIONS
  1181. static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
  1182. {
  1183. union wpa_event_data data;
  1184. char *pos, *param;
  1185. enum wpa_event_type event;
  1186. wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
  1187. os_memset(&data, 0, sizeof(data));
  1188. param = os_strchr(cmd, ' ');
  1189. if (param == NULL)
  1190. return -1;
  1191. *param++ = '\0';
  1192. if (os_strcmp(cmd, "DETECTED") == 0)
  1193. event = EVENT_DFS_RADAR_DETECTED;
  1194. else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
  1195. event = EVENT_DFS_CAC_FINISHED;
  1196. else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
  1197. event = EVENT_DFS_CAC_ABORTED;
  1198. else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
  1199. event = EVENT_DFS_NOP_FINISHED;
  1200. else {
  1201. wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
  1202. cmd);
  1203. return -1;
  1204. }
  1205. pos = os_strstr(param, "freq=");
  1206. if (pos)
  1207. data.dfs_event.freq = atoi(pos + 5);
  1208. pos = os_strstr(param, "ht_enabled=1");
  1209. if (pos)
  1210. data.dfs_event.ht_enabled = 1;
  1211. pos = os_strstr(param, "chan_offset=");
  1212. if (pos)
  1213. data.dfs_event.chan_offset = atoi(pos + 12);
  1214. pos = os_strstr(param, "chan_width=");
  1215. if (pos)
  1216. data.dfs_event.chan_width = atoi(pos + 11);
  1217. pos = os_strstr(param, "cf1=");
  1218. if (pos)
  1219. data.dfs_event.cf1 = atoi(pos + 4);
  1220. pos = os_strstr(param, "cf2=");
  1221. if (pos)
  1222. data.dfs_event.cf2 = atoi(pos + 4);
  1223. wpa_supplicant_event(hapd, event, &data);
  1224. return 0;
  1225. }
  1226. static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
  1227. {
  1228. size_t len;
  1229. u8 *buf;
  1230. int res;
  1231. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  1232. len = os_strlen(cmd);
  1233. if (len & 1)
  1234. return -1;
  1235. len /= 2;
  1236. buf = os_malloc(len);
  1237. if (buf == NULL)
  1238. return -1;
  1239. if (hexstr2bin(cmd, buf, len) < 0) {
  1240. os_free(buf);
  1241. return -1;
  1242. }
  1243. res = hostapd_drv_send_mlme(hapd, buf, len, 0);
  1244. os_free(buf);
  1245. return res;
  1246. }
  1247. static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
  1248. {
  1249. char *pos;
  1250. u8 src[ETH_ALEN], *buf;
  1251. int used;
  1252. size_t len;
  1253. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  1254. pos = cmd;
  1255. used = hwaddr_aton2(pos, src);
  1256. if (used < 0)
  1257. return -1;
  1258. pos += used;
  1259. while (*pos == ' ')
  1260. pos++;
  1261. len = os_strlen(pos);
  1262. if (len & 1)
  1263. return -1;
  1264. len /= 2;
  1265. buf = os_malloc(len);
  1266. if (buf == NULL)
  1267. return -1;
  1268. if (hexstr2bin(pos, buf, len) < 0) {
  1269. os_free(buf);
  1270. return -1;
  1271. }
  1272. ieee802_1x_receive(hapd, src, buf, len);
  1273. os_free(buf);
  1274. return 0;
  1275. }
  1276. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  1277. {
  1278. size_t i;
  1279. u32 sum = 0;
  1280. const u16 *pos = buf;
  1281. for (i = 0; i < len / 2; i++)
  1282. sum += *pos++;
  1283. while (sum >> 16)
  1284. sum = (sum & 0xffff) + (sum >> 16);
  1285. return sum ^ 0xffff;
  1286. }
  1287. #define HWSIM_PACKETLEN 1500
  1288. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  1289. void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  1290. size_t len)
  1291. {
  1292. struct hostapd_data *hapd = ctx;
  1293. const struct ether_header *eth;
  1294. struct iphdr ip;
  1295. const u8 *pos;
  1296. unsigned int i;
  1297. if (len != HWSIM_PACKETLEN)
  1298. return;
  1299. eth = (const struct ether_header *) buf;
  1300. os_memcpy(&ip, eth + 1, sizeof(ip));
  1301. pos = &buf[sizeof(*eth) + sizeof(ip)];
  1302. if (ip.ihl != 5 || ip.version != 4 ||
  1303. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  1304. return;
  1305. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  1306. if (*pos != (u8) i)
  1307. return;
  1308. pos++;
  1309. }
  1310. wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  1311. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  1312. }
  1313. static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
  1314. char *cmd)
  1315. {
  1316. int enabled = atoi(cmd);
  1317. char *pos;
  1318. const char *ifname;
  1319. if (!enabled) {
  1320. if (hapd->l2_test) {
  1321. l2_packet_deinit(hapd->l2_test);
  1322. hapd->l2_test = NULL;
  1323. wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
  1324. "test data: Disabled");
  1325. }
  1326. return 0;
  1327. }
  1328. if (hapd->l2_test)
  1329. return 0;
  1330. pos = os_strstr(cmd, " ifname=");
  1331. if (pos)
  1332. ifname = pos + 8;
  1333. else
  1334. ifname = hapd->conf->iface;
  1335. hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
  1336. ETHERTYPE_IP, hostapd_data_test_rx,
  1337. hapd, 1);
  1338. if (hapd->l2_test == NULL)
  1339. return -1;
  1340. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
  1341. return 0;
  1342. }
  1343. static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
  1344. {
  1345. u8 dst[ETH_ALEN], src[ETH_ALEN];
  1346. char *pos;
  1347. int used;
  1348. long int val;
  1349. u8 tos;
  1350. u8 buf[2 + HWSIM_PACKETLEN];
  1351. struct ether_header *eth;
  1352. struct iphdr *ip;
  1353. u8 *dpos;
  1354. unsigned int i;
  1355. if (hapd->l2_test == NULL)
  1356. return -1;
  1357. /* format: <dst> <src> <tos> */
  1358. pos = cmd;
  1359. used = hwaddr_aton2(pos, dst);
  1360. if (used < 0)
  1361. return -1;
  1362. pos += used;
  1363. while (*pos == ' ')
  1364. pos++;
  1365. used = hwaddr_aton2(pos, src);
  1366. if (used < 0)
  1367. return -1;
  1368. pos += used;
  1369. val = strtol(pos, NULL, 0);
  1370. if (val < 0 || val > 0xff)
  1371. return -1;
  1372. tos = val;
  1373. eth = (struct ether_header *) &buf[2];
  1374. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  1375. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  1376. eth->ether_type = htons(ETHERTYPE_IP);
  1377. ip = (struct iphdr *) (eth + 1);
  1378. os_memset(ip, 0, sizeof(*ip));
  1379. ip->ihl = 5;
  1380. ip->version = 4;
  1381. ip->ttl = 64;
  1382. ip->tos = tos;
  1383. ip->tot_len = htons(HWSIM_IP_LEN);
  1384. ip->protocol = 1;
  1385. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  1386. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  1387. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  1388. dpos = (u8 *) (ip + 1);
  1389. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  1390. *dpos++ = i;
  1391. if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
  1392. HWSIM_PACKETLEN) < 0)
  1393. return -1;
  1394. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
  1395. " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  1396. return 0;
  1397. }
  1398. static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
  1399. char *cmd)
  1400. {
  1401. u8 *buf;
  1402. struct ether_header *eth;
  1403. struct l2_packet_data *l2 = NULL;
  1404. size_t len;
  1405. u16 ethertype;
  1406. int res = -1;
  1407. const char *ifname = hapd->conf->iface;
  1408. if (os_strncmp(cmd, "ifname=", 7) == 0) {
  1409. cmd += 7;
  1410. ifname = cmd;
  1411. cmd = os_strchr(cmd, ' ');
  1412. if (cmd == NULL)
  1413. return -1;
  1414. *cmd++ = '\0';
  1415. }
  1416. len = os_strlen(cmd);
  1417. if (len & 1 || len < ETH_HLEN * 2)
  1418. return -1;
  1419. len /= 2;
  1420. buf = os_malloc(len);
  1421. if (buf == NULL)
  1422. return -1;
  1423. if (hexstr2bin(cmd, buf, len) < 0)
  1424. goto done;
  1425. eth = (struct ether_header *) buf;
  1426. ethertype = ntohs(eth->ether_type);
  1427. l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
  1428. hostapd_data_test_rx, hapd, 1);
  1429. if (l2 == NULL)
  1430. goto done;
  1431. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  1432. wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
  1433. done:
  1434. if (l2)
  1435. l2_packet_deinit(l2);
  1436. os_free(buf);
  1437. return res < 0 ? -1 : 0;
  1438. }
  1439. static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
  1440. {
  1441. #ifdef WPA_TRACE_BFD
  1442. extern char wpa_trace_fail_func[256];
  1443. extern unsigned int wpa_trace_fail_after;
  1444. char *pos;
  1445. wpa_trace_fail_after = atoi(cmd);
  1446. pos = os_strchr(cmd, ':');
  1447. if (pos) {
  1448. pos++;
  1449. os_strlcpy(wpa_trace_fail_func, pos,
  1450. sizeof(wpa_trace_fail_func));
  1451. } else {
  1452. wpa_trace_fail_after = 0;
  1453. }
  1454. return 0;
  1455. #else /* WPA_TRACE_BFD */
  1456. return -1;
  1457. #endif /* WPA_TRACE_BFD */
  1458. }
  1459. static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
  1460. char *buf, size_t buflen)
  1461. {
  1462. #ifdef WPA_TRACE_BFD
  1463. extern char wpa_trace_fail_func[256];
  1464. extern unsigned int wpa_trace_fail_after;
  1465. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  1466. wpa_trace_fail_func);
  1467. #else /* WPA_TRACE_BFD */
  1468. return -1;
  1469. #endif /* WPA_TRACE_BFD */
  1470. }
  1471. static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
  1472. {
  1473. #ifdef WPA_TRACE_BFD
  1474. extern char wpa_trace_test_fail_func[256];
  1475. extern unsigned int wpa_trace_test_fail_after;
  1476. char *pos;
  1477. wpa_trace_test_fail_after = atoi(cmd);
  1478. pos = os_strchr(cmd, ':');
  1479. if (pos) {
  1480. pos++;
  1481. os_strlcpy(wpa_trace_test_fail_func, pos,
  1482. sizeof(wpa_trace_test_fail_func));
  1483. } else {
  1484. wpa_trace_test_fail_after = 0;
  1485. }
  1486. return 0;
  1487. #else /* WPA_TRACE_BFD */
  1488. return -1;
  1489. #endif /* WPA_TRACE_BFD */
  1490. }
  1491. static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
  1492. char *buf, size_t buflen)
  1493. {
  1494. #ifdef WPA_TRACE_BFD
  1495. extern char wpa_trace_test_fail_func[256];
  1496. extern unsigned int wpa_trace_test_fail_after;
  1497. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  1498. wpa_trace_test_fail_func);
  1499. #else /* WPA_TRACE_BFD */
  1500. return -1;
  1501. #endif /* WPA_TRACE_BFD */
  1502. }
  1503. #endif /* CONFIG_TESTING_OPTIONS */
  1504. static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
  1505. char *pos)
  1506. {
  1507. #ifdef NEED_AP_MLME
  1508. struct csa_settings settings;
  1509. int ret;
  1510. unsigned int i;
  1511. ret = hostapd_parse_csa_settings(pos, &settings);
  1512. if (ret)
  1513. return ret;
  1514. for (i = 0; i < iface->num_bss; i++) {
  1515. ret = hostapd_switch_channel(iface->bss[i], &settings);
  1516. if (ret) {
  1517. /* FIX: What do we do if CSA fails in the middle of
  1518. * submitting multi-BSS CSA requests? */
  1519. return ret;
  1520. }
  1521. }
  1522. return 0;
  1523. #else /* NEED_AP_MLME */
  1524. return -1;
  1525. #endif /* NEED_AP_MLME */
  1526. }
  1527. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  1528. int reply_size, const char *param)
  1529. {
  1530. #ifdef RADIUS_SERVER
  1531. if (os_strcmp(param, "radius_server") == 0) {
  1532. return radius_server_get_mib(hapd->radius_srv, reply,
  1533. reply_size);
  1534. }
  1535. #endif /* RADIUS_SERVER */
  1536. return -1;
  1537. }
  1538. static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
  1539. char *buf, size_t buflen)
  1540. {
  1541. int ret;
  1542. char *pos;
  1543. u8 *data = NULL;
  1544. unsigned int vendor_id, subcmd;
  1545. struct wpabuf *reply;
  1546. size_t data_len = 0;
  1547. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  1548. vendor_id = strtoul(cmd, &pos, 16);
  1549. if (!isblank(*pos))
  1550. return -EINVAL;
  1551. subcmd = strtoul(pos, &pos, 10);
  1552. if (*pos != '\0') {
  1553. if (!isblank(*pos++))
  1554. return -EINVAL;
  1555. data_len = os_strlen(pos);
  1556. }
  1557. if (data_len) {
  1558. data_len /= 2;
  1559. data = os_malloc(data_len);
  1560. if (!data)
  1561. return -ENOBUFS;
  1562. if (hexstr2bin(pos, data, data_len)) {
  1563. wpa_printf(MSG_DEBUG,
  1564. "Vendor command: wrong parameter format");
  1565. os_free(data);
  1566. return -EINVAL;
  1567. }
  1568. }
  1569. reply = wpabuf_alloc((buflen - 1) / 2);
  1570. if (!reply) {
  1571. os_free(data);
  1572. return -ENOBUFS;
  1573. }
  1574. ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
  1575. reply);
  1576. if (ret == 0)
  1577. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  1578. wpabuf_len(reply));
  1579. wpabuf_free(reply);
  1580. os_free(data);
  1581. return ret;
  1582. }
  1583. static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
  1584. const char *cmd)
  1585. {
  1586. u8 addr[ETH_ALEN];
  1587. struct sta_info *sta;
  1588. if (hwaddr_aton(cmd, addr))
  1589. return -1;
  1590. sta = ap_get_sta(hapd, addr);
  1591. if (!sta || !sta->eapol_sm)
  1592. return -1;
  1593. eapol_auth_reauthenticate(sta->eapol_sm);
  1594. return 0;
  1595. }
  1596. static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
  1597. {
  1598. u8 addr[ETH_ALEN];
  1599. struct sta_info *sta;
  1600. char *pos = cmd, *param;
  1601. if (hwaddr_aton(pos, addr) || pos[17] != ' ')
  1602. return -1;
  1603. pos += 18;
  1604. param = pos;
  1605. pos = os_strchr(pos, ' ');
  1606. if (!pos)
  1607. return -1;
  1608. *pos++ = '\0';
  1609. sta = ap_get_sta(hapd, addr);
  1610. if (!sta || !sta->eapol_sm)
  1611. return -1;
  1612. return eapol_auth_set_conf(sta->eapol_sm, param, pos);
  1613. }
  1614. static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
  1615. char *buf, size_t buflen)
  1616. {
  1617. char *pos, *end, *stamp;
  1618. int ret;
  1619. /* cmd: "LOG_LEVEL [<level>]" */
  1620. if (*cmd == '\0') {
  1621. pos = buf;
  1622. end = buf + buflen;
  1623. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1624. "Timestamp: %d\n",
  1625. debug_level_str(wpa_debug_level),
  1626. wpa_debug_timestamp);
  1627. if (os_snprintf_error(end - pos, ret))
  1628. ret = 0;
  1629. return ret;
  1630. }
  1631. while (*cmd == ' ')
  1632. cmd++;
  1633. stamp = os_strchr(cmd, ' ');
  1634. if (stamp) {
  1635. *stamp++ = '\0';
  1636. while (*stamp == ' ') {
  1637. stamp++;
  1638. }
  1639. }
  1640. if (cmd && os_strlen(cmd)) {
  1641. int level = str_to_debug_level(cmd);
  1642. if (level < 0)
  1643. return -1;
  1644. wpa_debug_level = level;
  1645. }
  1646. if (stamp && os_strlen(stamp))
  1647. wpa_debug_timestamp = atoi(stamp);
  1648. os_memcpy(buf, "OK\n", 3);
  1649. return 3;
  1650. }
  1651. static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
  1652. char *buf, char *reply,
  1653. int reply_size,
  1654. struct sockaddr_un *from,
  1655. socklen_t fromlen)
  1656. {
  1657. int reply_len, res;
  1658. os_memcpy(reply, "OK\n", 3);
  1659. reply_len = 3;
  1660. if (os_strcmp(buf, "PING") == 0) {
  1661. os_memcpy(reply, "PONG\n", 5);
  1662. reply_len = 5;
  1663. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1664. if (wpa_debug_reopen_file() < 0)
  1665. reply_len = -1;
  1666. } else if (os_strcmp(buf, "STATUS") == 0) {
  1667. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  1668. reply_size);
  1669. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  1670. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  1671. } else if (os_strcmp(buf, "MIB") == 0) {
  1672. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  1673. if (reply_len >= 0) {
  1674. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  1675. reply_size - reply_len);
  1676. if (res < 0)
  1677. reply_len = -1;
  1678. else
  1679. reply_len += res;
  1680. }
  1681. if (reply_len >= 0) {
  1682. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  1683. reply_size - reply_len);
  1684. if (res < 0)
  1685. reply_len = -1;
  1686. else
  1687. reply_len += res;
  1688. }
  1689. #ifndef CONFIG_NO_RADIUS
  1690. if (reply_len >= 0) {
  1691. res = radius_client_get_mib(hapd->radius,
  1692. reply + reply_len,
  1693. reply_size - reply_len);
  1694. if (res < 0)
  1695. reply_len = -1;
  1696. else
  1697. reply_len += res;
  1698. }
  1699. #endif /* CONFIG_NO_RADIUS */
  1700. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  1701. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  1702. buf + 4);
  1703. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1704. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  1705. reply_size);
  1706. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1707. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  1708. reply_size);
  1709. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1710. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  1711. reply_size);
  1712. } else if (os_strcmp(buf, "ATTACH") == 0) {
  1713. if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
  1714. reply_len = -1;
  1715. } else if (os_strcmp(buf, "DETACH") == 0) {
  1716. if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
  1717. reply_len = -1;
  1718. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  1719. if (hostapd_ctrl_iface_level(hapd, from, fromlen,
  1720. buf + 6))
  1721. reply_len = -1;
  1722. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  1723. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  1724. reply_len = -1;
  1725. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  1726. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  1727. reply_len = -1;
  1728. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  1729. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  1730. reply_len = -1;
  1731. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  1732. if (hostapd_ctrl_iface_stop_ap(hapd))
  1733. reply_len = -1;
  1734. #ifdef CONFIG_IEEE80211W
  1735. #ifdef NEED_AP_MLME
  1736. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  1737. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  1738. reply_len = -1;
  1739. #endif /* NEED_AP_MLME */
  1740. #endif /* CONFIG_IEEE80211W */
  1741. #ifdef CONFIG_WPS
  1742. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1743. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  1744. reply_len = -1;
  1745. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  1746. reply_len = hostapd_ctrl_iface_wps_check_pin(
  1747. hapd, buf + 14, reply, reply_size);
  1748. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1749. if (hostapd_wps_button_pushed(hapd, NULL))
  1750. reply_len = -1;
  1751. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  1752. if (hostapd_wps_cancel(hapd))
  1753. reply_len = -1;
  1754. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  1755. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  1756. reply, reply_size);
  1757. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  1758. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  1759. reply_len = -1;
  1760. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  1761. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  1762. reply_size);
  1763. #ifdef CONFIG_WPS_NFC
  1764. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  1765. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  1766. reply_len = -1;
  1767. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  1768. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  1769. hapd, buf + 21, reply, reply_size);
  1770. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  1771. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  1772. hapd, buf + 14, reply, reply_size);
  1773. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  1774. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  1775. hapd, buf + 21, reply, reply_size);
  1776. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1777. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1778. reply_len = -1;
  1779. #endif /* CONFIG_WPS_NFC */
  1780. #endif /* CONFIG_WPS */
  1781. #ifdef CONFIG_INTERWORKING
  1782. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1783. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1784. reply_len = -1;
  1785. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1786. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1787. reply_len = -1;
  1788. #endif /* CONFIG_INTERWORKING */
  1789. #ifdef CONFIG_HS20
  1790. } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
  1791. if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
  1792. reply_len = -1;
  1793. } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
  1794. if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
  1795. reply_len = -1;
  1796. #endif /* CONFIG_HS20 */
  1797. #ifdef CONFIG_WNM
  1798. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1799. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1800. reply_len = -1;
  1801. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1802. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1803. reply_len = -1;
  1804. } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
  1805. if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
  1806. reply_len = -1;
  1807. #endif /* CONFIG_WNM */
  1808. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1809. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1810. reply_size);
  1811. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1812. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1813. reply_len = -1;
  1814. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1815. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1816. reply_size);
  1817. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1818. if (hostapd_ctrl_iface_enable(hapd->iface))
  1819. reply_len = -1;
  1820. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1821. if (hostapd_ctrl_iface_reload(hapd->iface))
  1822. reply_len = -1;
  1823. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1824. if (hostapd_ctrl_iface_disable(hapd->iface))
  1825. reply_len = -1;
  1826. } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
  1827. if (ieee802_11_set_beacon(hapd))
  1828. reply_len = -1;
  1829. #ifdef CONFIG_TESTING_OPTIONS
  1830. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  1831. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  1832. reply_len = -1;
  1833. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  1834. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  1835. reply_len = -1;
  1836. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  1837. if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
  1838. reply_len = -1;
  1839. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  1840. if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
  1841. reply_len = -1;
  1842. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  1843. if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
  1844. reply_len = -1;
  1845. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  1846. if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
  1847. reply_len = -1;
  1848. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  1849. if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
  1850. reply_len = -1;
  1851. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  1852. reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
  1853. reply_size);
  1854. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  1855. if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
  1856. reply_len = -1;
  1857. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  1858. reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
  1859. #endif /* CONFIG_TESTING_OPTIONS */
  1860. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  1861. if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
  1862. reply_len = -1;
  1863. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  1864. reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
  1865. reply_size);
  1866. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  1867. ieee802_1x_erp_flush(hapd);
  1868. #ifdef RADIUS_SERVER
  1869. radius_server_erp_flush(hapd->radius_srv);
  1870. #endif /* RADIUS_SERVER */
  1871. } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
  1872. if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
  1873. reply_len = -1;
  1874. } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
  1875. if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
  1876. reply_len = -1;
  1877. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  1878. reply_len = hostapd_ctrl_iface_log_level(
  1879. hapd, buf + 9, reply, reply_size);
  1880. } else {
  1881. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1882. reply_len = 16;
  1883. }
  1884. if (reply_len < 0) {
  1885. os_memcpy(reply, "FAIL\n", 5);
  1886. reply_len = 5;
  1887. }
  1888. return reply_len;
  1889. }
  1890. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  1891. void *sock_ctx)
  1892. {
  1893. struct hostapd_data *hapd = eloop_ctx;
  1894. char buf[4096];
  1895. int res;
  1896. struct sockaddr_un from;
  1897. socklen_t fromlen = sizeof(from);
  1898. char *reply;
  1899. const int reply_size = 4096;
  1900. int reply_len;
  1901. int level = MSG_DEBUG;
  1902. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1903. (struct sockaddr *) &from, &fromlen);
  1904. if (res < 0) {
  1905. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  1906. strerror(errno));
  1907. return;
  1908. }
  1909. buf[res] = '\0';
  1910. if (os_strcmp(buf, "PING") == 0)
  1911. level = MSG_EXCESSIVE;
  1912. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  1913. reply = os_malloc(reply_size);
  1914. if (reply == NULL) {
  1915. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  1916. fromlen) < 0) {
  1917. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1918. strerror(errno));
  1919. }
  1920. return;
  1921. }
  1922. reply_len = hostapd_ctrl_iface_receive_process(hapd, buf,
  1923. reply, reply_size,
  1924. &from, fromlen);
  1925. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  1926. fromlen) < 0) {
  1927. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  1928. strerror(errno));
  1929. }
  1930. os_free(reply);
  1931. }
  1932. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  1933. {
  1934. char *buf;
  1935. size_t len;
  1936. if (hapd->conf->ctrl_interface == NULL)
  1937. return NULL;
  1938. len = os_strlen(hapd->conf->ctrl_interface) +
  1939. os_strlen(hapd->conf->iface) + 2;
  1940. buf = os_malloc(len);
  1941. if (buf == NULL)
  1942. return NULL;
  1943. os_snprintf(buf, len, "%s/%s",
  1944. hapd->conf->ctrl_interface, hapd->conf->iface);
  1945. buf[len - 1] = '\0';
  1946. return buf;
  1947. }
  1948. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  1949. enum wpa_msg_type type,
  1950. const char *txt, size_t len)
  1951. {
  1952. struct hostapd_data *hapd = ctx;
  1953. if (hapd == NULL)
  1954. return;
  1955. hostapd_ctrl_iface_send(hapd, level, type, txt, len);
  1956. }
  1957. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  1958. {
  1959. struct sockaddr_un addr;
  1960. int s = -1;
  1961. char *fname = NULL;
  1962. if (hapd->ctrl_sock > -1) {
  1963. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  1964. return 0;
  1965. }
  1966. if (hapd->conf->ctrl_interface == NULL)
  1967. return 0;
  1968. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1969. if (errno == EEXIST) {
  1970. wpa_printf(MSG_DEBUG, "Using existing control "
  1971. "interface directory.");
  1972. } else {
  1973. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  1974. strerror(errno));
  1975. goto fail;
  1976. }
  1977. }
  1978. if (hapd->conf->ctrl_interface_gid_set &&
  1979. chown(hapd->conf->ctrl_interface, -1,
  1980. hapd->conf->ctrl_interface_gid) < 0) {
  1981. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  1982. strerror(errno));
  1983. return -1;
  1984. }
  1985. if (!hapd->conf->ctrl_interface_gid_set &&
  1986. hapd->iface->interfaces->ctrl_iface_group &&
  1987. chown(hapd->conf->ctrl_interface, -1,
  1988. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1989. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  1990. strerror(errno));
  1991. return -1;
  1992. }
  1993. #ifdef ANDROID
  1994. /*
  1995. * Android is using umask 0077 which would leave the control interface
  1996. * directory without group access. This breaks things since Wi-Fi
  1997. * framework assumes that this directory can be accessed by other
  1998. * applications in the wifi group. Fix this by adding group access even
  1999. * if umask value would prevent this.
  2000. */
  2001. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  2002. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  2003. strerror(errno));
  2004. /* Try to continue anyway */
  2005. }
  2006. #endif /* ANDROID */
  2007. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  2008. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  2009. goto fail;
  2010. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2011. if (s < 0) {
  2012. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2013. goto fail;
  2014. }
  2015. os_memset(&addr, 0, sizeof(addr));
  2016. #ifdef __FreeBSD__
  2017. addr.sun_len = sizeof(addr);
  2018. #endif /* __FreeBSD__ */
  2019. addr.sun_family = AF_UNIX;
  2020. fname = hostapd_ctrl_iface_path(hapd);
  2021. if (fname == NULL)
  2022. goto fail;
  2023. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2024. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2025. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2026. strerror(errno));
  2027. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2028. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2029. " allow connections - assuming it was left"
  2030. "over from forced program termination");
  2031. if (unlink(fname) < 0) {
  2032. wpa_printf(MSG_ERROR,
  2033. "Could not unlink existing ctrl_iface socket '%s': %s",
  2034. fname, strerror(errno));
  2035. goto fail;
  2036. }
  2037. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2038. 0) {
  2039. wpa_printf(MSG_ERROR,
  2040. "hostapd-ctrl-iface: bind(PF_UNIX): %s",
  2041. strerror(errno));
  2042. goto fail;
  2043. }
  2044. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2045. "ctrl_iface socket '%s'", fname);
  2046. } else {
  2047. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2048. "be in use - cannot override it");
  2049. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2050. "not used anymore", fname);
  2051. os_free(fname);
  2052. fname = NULL;
  2053. goto fail;
  2054. }
  2055. }
  2056. if (hapd->conf->ctrl_interface_gid_set &&
  2057. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  2058. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  2059. strerror(errno));
  2060. goto fail;
  2061. }
  2062. if (!hapd->conf->ctrl_interface_gid_set &&
  2063. hapd->iface->interfaces->ctrl_iface_group &&
  2064. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  2065. wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
  2066. strerror(errno));
  2067. goto fail;
  2068. }
  2069. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  2070. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  2071. strerror(errno));
  2072. goto fail;
  2073. }
  2074. os_free(fname);
  2075. hapd->ctrl_sock = s;
  2076. if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  2077. NULL) < 0) {
  2078. hostapd_ctrl_iface_deinit(hapd);
  2079. return -1;
  2080. }
  2081. hapd->msg_ctx = hapd;
  2082. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  2083. return 0;
  2084. fail:
  2085. if (s >= 0)
  2086. close(s);
  2087. if (fname) {
  2088. unlink(fname);
  2089. os_free(fname);
  2090. }
  2091. return -1;
  2092. }
  2093. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  2094. {
  2095. struct wpa_ctrl_dst *dst, *prev;
  2096. if (hapd->ctrl_sock > -1) {
  2097. char *fname;
  2098. eloop_unregister_read_sock(hapd->ctrl_sock);
  2099. close(hapd->ctrl_sock);
  2100. hapd->ctrl_sock = -1;
  2101. fname = hostapd_ctrl_iface_path(hapd);
  2102. if (fname)
  2103. unlink(fname);
  2104. os_free(fname);
  2105. if (hapd->conf->ctrl_interface &&
  2106. rmdir(hapd->conf->ctrl_interface) < 0) {
  2107. if (errno == ENOTEMPTY) {
  2108. wpa_printf(MSG_DEBUG, "Control interface "
  2109. "directory not empty - leaving it "
  2110. "behind");
  2111. } else {
  2112. wpa_printf(MSG_ERROR,
  2113. "rmdir[ctrl_interface=%s]: %s",
  2114. hapd->conf->ctrl_interface,
  2115. strerror(errno));
  2116. }
  2117. }
  2118. }
  2119. dst = hapd->ctrl_dst;
  2120. hapd->ctrl_dst = NULL;
  2121. while (dst) {
  2122. prev = dst;
  2123. dst = dst->next;
  2124. os_free(prev);
  2125. }
  2126. #ifdef CONFIG_TESTING_OPTIONS
  2127. l2_packet_deinit(hapd->l2_test);
  2128. hapd->l2_test = NULL;
  2129. #endif /* CONFIG_TESTING_OPTIONS */
  2130. }
  2131. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  2132. char *buf)
  2133. {
  2134. if (hostapd_add_iface(interfaces, buf) < 0) {
  2135. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  2136. return -1;
  2137. }
  2138. return 0;
  2139. }
  2140. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  2141. char *buf)
  2142. {
  2143. if (hostapd_remove_iface(interfaces, buf) < 0) {
  2144. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  2145. return -1;
  2146. }
  2147. return 0;
  2148. }
  2149. static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
  2150. struct sockaddr_un *from,
  2151. socklen_t fromlen)
  2152. {
  2153. struct wpa_ctrl_dst *dst;
  2154. dst = os_zalloc(sizeof(*dst));
  2155. if (dst == NULL)
  2156. return -1;
  2157. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  2158. dst->addrlen = fromlen;
  2159. dst->debug_level = MSG_INFO;
  2160. dst->next = interfaces->global_ctrl_dst;
  2161. interfaces->global_ctrl_dst = dst;
  2162. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached (global)",
  2163. from->sun_path,
  2164. fromlen - offsetof(struct sockaddr_un, sun_path));
  2165. return 0;
  2166. }
  2167. static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
  2168. struct sockaddr_un *from,
  2169. socklen_t fromlen)
  2170. {
  2171. struct wpa_ctrl_dst *dst, *prev = NULL;
  2172. dst = interfaces->global_ctrl_dst;
  2173. while (dst) {
  2174. if (fromlen == dst->addrlen &&
  2175. os_memcmp(from->sun_path, dst->addr.sun_path,
  2176. fromlen - offsetof(struct sockaddr_un, sun_path))
  2177. == 0) {
  2178. wpa_hexdump(MSG_DEBUG,
  2179. "CTRL_IFACE monitor detached (global)",
  2180. from->sun_path,
  2181. fromlen -
  2182. offsetof(struct sockaddr_un, sun_path));
  2183. if (prev == NULL)
  2184. interfaces->global_ctrl_dst = dst->next;
  2185. else
  2186. prev->next = dst->next;
  2187. os_free(dst);
  2188. return 0;
  2189. }
  2190. prev = dst;
  2191. dst = dst->next;
  2192. }
  2193. return -1;
  2194. }
  2195. static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
  2196. {
  2197. #ifdef CONFIG_WPS_TESTING
  2198. wps_version_number = 0x20;
  2199. wps_testing_dummy_cred = 0;
  2200. wps_corrupt_pkhash = 0;
  2201. #endif /* CONFIG_WPS_TESTING */
  2202. }
  2203. #ifdef CONFIG_FST
  2204. static int
  2205. hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
  2206. const char *cmd)
  2207. {
  2208. char ifname[IFNAMSIZ + 1];
  2209. struct fst_iface_cfg cfg;
  2210. struct hostapd_data *hapd;
  2211. struct fst_wpa_obj iface_obj;
  2212. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  2213. hapd = hostapd_get_iface(interfaces, ifname);
  2214. if (hapd) {
  2215. if (hapd->iface->fst) {
  2216. wpa_printf(MSG_INFO, "FST: Already attached");
  2217. return -1;
  2218. }
  2219. fst_hostapd_fill_iface_obj(hapd, &iface_obj);
  2220. hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
  2221. &iface_obj, &cfg);
  2222. if (hapd->iface->fst)
  2223. return 0;
  2224. }
  2225. }
  2226. return -EINVAL;
  2227. }
  2228. static int
  2229. hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
  2230. const char *cmd)
  2231. {
  2232. char ifname[IFNAMSIZ + 1];
  2233. struct hostapd_data * hapd;
  2234. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  2235. hapd = hostapd_get_iface(interfaces, ifname);
  2236. if (hapd) {
  2237. if (!fst_iface_detach(ifname)) {
  2238. hapd->iface->fst = NULL;
  2239. hapd->iface->fst_ies = NULL;
  2240. return 0;
  2241. }
  2242. }
  2243. }
  2244. return -EINVAL;
  2245. }
  2246. #endif /* CONFIG_FST */
  2247. static struct hostapd_data *
  2248. hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
  2249. const char *ifname)
  2250. {
  2251. size_t i, j;
  2252. for (i = 0; i < interfaces->count; i++) {
  2253. struct hostapd_iface *iface = interfaces->iface[i];
  2254. for (j = 0; j < iface->num_bss; j++) {
  2255. struct hostapd_data *hapd;
  2256. hapd = iface->bss[j];
  2257. if (os_strcmp(ifname, hapd->conf->iface) == 0)
  2258. return hapd;
  2259. }
  2260. }
  2261. return NULL;
  2262. }
  2263. static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
  2264. struct hostapd_data *dst_hapd,
  2265. const char *param)
  2266. {
  2267. int res;
  2268. char *value;
  2269. value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  2270. if (!value) {
  2271. wpa_printf(MSG_ERROR,
  2272. "DUP: cannot allocate buffer to stringify %s",
  2273. param);
  2274. goto error_return;
  2275. }
  2276. if (os_strcmp(param, "wpa") == 0) {
  2277. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
  2278. src_hapd->conf->wpa);
  2279. } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
  2280. src_hapd->conf->wpa_key_mgmt) {
  2281. res = hostapd_ctrl_iface_get_key_mgmt(
  2282. src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
  2283. if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
  2284. goto error_stringify;
  2285. } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
  2286. src_hapd->conf->wpa_pairwise) {
  2287. res = wpa_write_ciphers(value,
  2288. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2289. src_hapd->conf->wpa_pairwise, " ");
  2290. if (res < 0)
  2291. goto error_stringify;
  2292. } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
  2293. src_hapd->conf->rsn_pairwise) {
  2294. res = wpa_write_ciphers(value,
  2295. value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2296. src_hapd->conf->rsn_pairwise, " ");
  2297. if (res < 0)
  2298. goto error_stringify;
  2299. } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
  2300. src_hapd->conf->ssid.wpa_passphrase) {
  2301. os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
  2302. src_hapd->conf->ssid.wpa_passphrase);
  2303. } else if (os_strcmp(param, "wpa_psk") == 0 &&
  2304. src_hapd->conf->ssid.wpa_psk_set) {
  2305. wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
  2306. src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  2307. } else {
  2308. wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
  2309. goto error_return;
  2310. }
  2311. res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
  2312. os_free(value);
  2313. return res;
  2314. error_stringify:
  2315. wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
  2316. error_return:
  2317. os_free(value);
  2318. return -1;
  2319. }
  2320. static int
  2321. hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
  2322. char *cmd)
  2323. {
  2324. char *p_start = cmd, *p_end;
  2325. struct hostapd_data *src_hapd, *dst_hapd;
  2326. /* cmd: "<src ifname> <dst ifname> <variable name> */
  2327. p_end = os_strchr(p_start, ' ');
  2328. if (!p_end) {
  2329. wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
  2330. cmd);
  2331. return -1;
  2332. }
  2333. *p_end = '\0';
  2334. src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  2335. if (!src_hapd) {
  2336. wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
  2337. p_start);
  2338. return -1;
  2339. }
  2340. p_start = p_end + 1;
  2341. p_end = os_strchr(p_start, ' ');
  2342. if (!p_end) {
  2343. wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
  2344. cmd);
  2345. return -1;
  2346. }
  2347. *p_end = '\0';
  2348. dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
  2349. if (!dst_hapd) {
  2350. wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
  2351. p_start);
  2352. return -1;
  2353. }
  2354. p_start = p_end + 1;
  2355. return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
  2356. }
  2357. static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
  2358. const char *ifname,
  2359. char *buf, char *reply,
  2360. int reply_size,
  2361. struct sockaddr_un *from,
  2362. socklen_t fromlen)
  2363. {
  2364. struct hostapd_data *hapd;
  2365. hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
  2366. if (hapd == NULL) {
  2367. int res;
  2368. res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
  2369. if (os_snprintf_error(reply_size, res))
  2370. return -1;
  2371. return res;
  2372. }
  2373. return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
  2374. from, fromlen);
  2375. }
  2376. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  2377. void *sock_ctx)
  2378. {
  2379. void *interfaces = eloop_ctx;
  2380. char buf[256];
  2381. int res;
  2382. struct sockaddr_un from;
  2383. socklen_t fromlen = sizeof(from);
  2384. char *reply;
  2385. int reply_len;
  2386. const int reply_size = 4096;
  2387. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  2388. (struct sockaddr *) &from, &fromlen);
  2389. if (res < 0) {
  2390. wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
  2391. strerror(errno));
  2392. return;
  2393. }
  2394. buf[res] = '\0';
  2395. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  2396. reply = os_malloc(reply_size);
  2397. if (reply == NULL) {
  2398. if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  2399. fromlen) < 0) {
  2400. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2401. strerror(errno));
  2402. }
  2403. return;
  2404. }
  2405. os_memcpy(reply, "OK\n", 3);
  2406. reply_len = 3;
  2407. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  2408. char *pos = os_strchr(buf + 7, ' ');
  2409. if (pos) {
  2410. *pos++ = '\0';
  2411. reply_len = hostapd_global_ctrl_iface_ifname(
  2412. interfaces, buf + 7, pos, reply, reply_size,
  2413. &from, fromlen);
  2414. goto send_reply;
  2415. }
  2416. }
  2417. if (os_strcmp(buf, "PING") == 0) {
  2418. os_memcpy(reply, "PONG\n", 5);
  2419. reply_len = 5;
  2420. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  2421. if (wpa_debug_reopen_file() < 0)
  2422. reply_len = -1;
  2423. } else if (os_strcmp(buf, "FLUSH") == 0) {
  2424. hostapd_ctrl_iface_flush(interfaces);
  2425. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  2426. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  2427. reply_len = -1;
  2428. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  2429. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  2430. reply_len = -1;
  2431. } else if (os_strcmp(buf, "ATTACH") == 0) {
  2432. if (hostapd_global_ctrl_iface_attach(interfaces, &from,
  2433. fromlen))
  2434. reply_len = -1;
  2435. } else if (os_strcmp(buf, "DETACH") == 0) {
  2436. if (hostapd_global_ctrl_iface_detach(interfaces, &from,
  2437. fromlen))
  2438. reply_len = -1;
  2439. #ifdef CONFIG_MODULE_TESTS
  2440. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  2441. int hapd_module_tests(void);
  2442. if (hapd_module_tests() < 0)
  2443. reply_len = -1;
  2444. #endif /* CONFIG_MODULE_TESTS */
  2445. #ifdef CONFIG_FST
  2446. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  2447. if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
  2448. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2449. else
  2450. reply_len = -1;
  2451. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  2452. if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
  2453. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2454. else
  2455. reply_len = -1;
  2456. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  2457. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  2458. #endif /* CONFIG_FST */
  2459. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  2460. if (!hostapd_global_ctrl_iface_dup_network(interfaces,
  2461. buf + 12))
  2462. reply_len = os_snprintf(reply, reply_size, "OK\n");
  2463. else
  2464. reply_len = -1;
  2465. } else {
  2466. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  2467. "ignored");
  2468. reply_len = -1;
  2469. }
  2470. send_reply:
  2471. if (reply_len < 0) {
  2472. os_memcpy(reply, "FAIL\n", 5);
  2473. reply_len = 5;
  2474. }
  2475. if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
  2476. fromlen) < 0) {
  2477. wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
  2478. strerror(errno));
  2479. }
  2480. os_free(reply);
  2481. }
  2482. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  2483. {
  2484. char *buf;
  2485. size_t len;
  2486. if (interface->global_iface_path == NULL)
  2487. return NULL;
  2488. len = os_strlen(interface->global_iface_path) +
  2489. os_strlen(interface->global_iface_name) + 2;
  2490. buf = os_malloc(len);
  2491. if (buf == NULL)
  2492. return NULL;
  2493. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  2494. interface->global_iface_name);
  2495. buf[len - 1] = '\0';
  2496. return buf;
  2497. }
  2498. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  2499. {
  2500. struct sockaddr_un addr;
  2501. int s = -1;
  2502. char *fname = NULL;
  2503. if (interface->global_iface_path == NULL) {
  2504. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  2505. return 0;
  2506. }
  2507. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  2508. if (errno == EEXIST) {
  2509. wpa_printf(MSG_DEBUG, "Using existing control "
  2510. "interface directory.");
  2511. } else {
  2512. wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
  2513. strerror(errno));
  2514. goto fail;
  2515. }
  2516. } else if (interface->ctrl_iface_group &&
  2517. chown(interface->global_iface_path, -1,
  2518. interface->ctrl_iface_group) < 0) {
  2519. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2520. strerror(errno));
  2521. goto fail;
  2522. }
  2523. if (os_strlen(interface->global_iface_path) + 1 +
  2524. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  2525. goto fail;
  2526. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  2527. if (s < 0) {
  2528. wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
  2529. goto fail;
  2530. }
  2531. os_memset(&addr, 0, sizeof(addr));
  2532. #ifdef __FreeBSD__
  2533. addr.sun_len = sizeof(addr);
  2534. #endif /* __FreeBSD__ */
  2535. addr.sun_family = AF_UNIX;
  2536. fname = hostapd_global_ctrl_iface_path(interface);
  2537. if (fname == NULL)
  2538. goto fail;
  2539. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  2540. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2541. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  2542. strerror(errno));
  2543. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  2544. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  2545. " allow connections - assuming it was left"
  2546. "over from forced program termination");
  2547. if (unlink(fname) < 0) {
  2548. wpa_printf(MSG_ERROR,
  2549. "Could not unlink existing ctrl_iface socket '%s': %s",
  2550. fname, strerror(errno));
  2551. goto fail;
  2552. }
  2553. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  2554. 0) {
  2555. wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
  2556. strerror(errno));
  2557. goto fail;
  2558. }
  2559. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  2560. "ctrl_iface socket '%s'", fname);
  2561. } else {
  2562. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  2563. "be in use - cannot override it");
  2564. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  2565. "not used anymore", fname);
  2566. os_free(fname);
  2567. fname = NULL;
  2568. goto fail;
  2569. }
  2570. }
  2571. if (interface->ctrl_iface_group &&
  2572. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  2573. wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
  2574. strerror(errno));
  2575. goto fail;
  2576. }
  2577. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  2578. wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
  2579. strerror(errno));
  2580. goto fail;
  2581. }
  2582. os_free(fname);
  2583. interface->global_ctrl_sock = s;
  2584. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  2585. interface, NULL);
  2586. return 0;
  2587. fail:
  2588. if (s >= 0)
  2589. close(s);
  2590. if (fname) {
  2591. unlink(fname);
  2592. os_free(fname);
  2593. }
  2594. return -1;
  2595. }
  2596. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  2597. {
  2598. char *fname = NULL;
  2599. struct wpa_ctrl_dst *dst, *prev;
  2600. if (interfaces->global_ctrl_sock > -1) {
  2601. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  2602. close(interfaces->global_ctrl_sock);
  2603. interfaces->global_ctrl_sock = -1;
  2604. fname = hostapd_global_ctrl_iface_path(interfaces);
  2605. if (fname) {
  2606. unlink(fname);
  2607. os_free(fname);
  2608. }
  2609. if (interfaces->global_iface_path &&
  2610. rmdir(interfaces->global_iface_path) < 0) {
  2611. if (errno == ENOTEMPTY) {
  2612. wpa_printf(MSG_DEBUG, "Control interface "
  2613. "directory not empty - leaving it "
  2614. "behind");
  2615. } else {
  2616. wpa_printf(MSG_ERROR,
  2617. "rmdir[ctrl_interface=%s]: %s",
  2618. interfaces->global_iface_path,
  2619. strerror(errno));
  2620. }
  2621. }
  2622. }
  2623. os_free(interfaces->global_iface_path);
  2624. interfaces->global_iface_path = NULL;
  2625. dst = interfaces->global_ctrl_dst;
  2626. interfaces->global_ctrl_dst = NULL;
  2627. while (dst) {
  2628. prev = dst;
  2629. dst = dst->next;
  2630. os_free(prev);
  2631. }
  2632. }
  2633. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  2634. enum wpa_msg_type type,
  2635. const char *buf, size_t len)
  2636. {
  2637. struct wpa_ctrl_dst *dst, *next;
  2638. struct msghdr msg;
  2639. int idx;
  2640. struct iovec io[2];
  2641. char levelstr[10];
  2642. int s;
  2643. if (type != WPA_MSG_ONLY_GLOBAL) {
  2644. s = hapd->ctrl_sock;
  2645. dst = hapd->ctrl_dst;
  2646. } else {
  2647. s = hapd->iface->interfaces->global_ctrl_sock;
  2648. dst = hapd->iface->interfaces->global_ctrl_dst;
  2649. }
  2650. if (s < 0 || dst == NULL)
  2651. return;
  2652. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  2653. io[0].iov_base = levelstr;
  2654. io[0].iov_len = os_strlen(levelstr);
  2655. io[1].iov_base = (char *) buf;
  2656. io[1].iov_len = len;
  2657. os_memset(&msg, 0, sizeof(msg));
  2658. msg.msg_iov = io;
  2659. msg.msg_iovlen = 2;
  2660. idx = 0;
  2661. while (dst) {
  2662. next = dst->next;
  2663. if (level >= dst->debug_level) {
  2664. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  2665. (u8 *) dst->addr.sun_path, dst->addrlen -
  2666. offsetof(struct sockaddr_un, sun_path));
  2667. msg.msg_name = &dst->addr;
  2668. msg.msg_namelen = dst->addrlen;
  2669. if (sendmsg(s, &msg, 0) < 0) {
  2670. int _errno = errno;
  2671. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  2672. "%d - %s",
  2673. idx, errno, strerror(errno));
  2674. dst->errors++;
  2675. if (dst->errors > 10 || _errno == ENOENT) {
  2676. if (type != WPA_MSG_ONLY_GLOBAL)
  2677. hostapd_ctrl_iface_detach(
  2678. hapd, &dst->addr,
  2679. dst->addrlen);
  2680. else
  2681. hostapd_global_ctrl_iface_detach(
  2682. hapd->iface->interfaces,
  2683. &dst->addr,
  2684. dst->addrlen);
  2685. }
  2686. } else
  2687. dst->errors = 0;
  2688. }
  2689. idx++;
  2690. dst = next;
  2691. }
  2692. }
  2693. #endif /* CONFIG_NATIVE_WINDOWS */