ctrl_iface.c 62 KB

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