ctrl_iface.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2014, 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. #include <sys/un.h>
  11. #include <sys/stat.h>
  12. #include <stddef.h>
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "common/version.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "drivers/driver.h"
  18. #include "radius/radius_client.h"
  19. #include "radius/radius_server.h"
  20. #include "ap/hostapd.h"
  21. #include "ap/ap_config.h"
  22. #include "ap/ieee802_1x.h"
  23. #include "ap/wpa_auth.h"
  24. #include "ap/ieee802_11.h"
  25. #include "ap/sta_info.h"
  26. #include "ap/wps_hostapd.h"
  27. #include "ap/ctrl_iface_ap.h"
  28. #include "ap/ap_drv_ops.h"
  29. #include "ap/wnm_ap.h"
  30. #include "ap/wpa_auth.h"
  31. #include "wps/wps_defs.h"
  32. #include "wps/wps.h"
  33. #include "config_file.h"
  34. #include "ctrl_iface.h"
  35. struct wpa_ctrl_dst {
  36. struct wpa_ctrl_dst *next;
  37. struct sockaddr_un addr;
  38. socklen_t addrlen;
  39. int debug_level;
  40. int errors;
  41. };
  42. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  43. const char *buf, size_t len);
  44. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  45. struct sockaddr_un *from,
  46. socklen_t fromlen)
  47. {
  48. struct wpa_ctrl_dst *dst;
  49. dst = os_zalloc(sizeof(*dst));
  50. if (dst == NULL)
  51. return -1;
  52. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  53. dst->addrlen = fromlen;
  54. dst->debug_level = MSG_INFO;
  55. dst->next = hapd->ctrl_dst;
  56. hapd->ctrl_dst = dst;
  57. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  58. (u8 *) from->sun_path,
  59. fromlen - offsetof(struct sockaddr_un, sun_path));
  60. return 0;
  61. }
  62. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  63. struct sockaddr_un *from,
  64. socklen_t fromlen)
  65. {
  66. struct wpa_ctrl_dst *dst, *prev = NULL;
  67. dst = hapd->ctrl_dst;
  68. while (dst) {
  69. if (fromlen == dst->addrlen &&
  70. os_memcmp(from->sun_path, dst->addr.sun_path,
  71. fromlen - offsetof(struct sockaddr_un, sun_path))
  72. == 0) {
  73. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  74. (u8 *) from->sun_path,
  75. fromlen -
  76. offsetof(struct sockaddr_un, sun_path));
  77. if (prev == NULL)
  78. hapd->ctrl_dst = dst->next;
  79. else
  80. prev->next = dst->next;
  81. os_free(dst);
  82. return 0;
  83. }
  84. prev = dst;
  85. dst = dst->next;
  86. }
  87. return -1;
  88. }
  89. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  90. struct sockaddr_un *from,
  91. socklen_t fromlen,
  92. char *level)
  93. {
  94. struct wpa_ctrl_dst *dst;
  95. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  96. dst = hapd->ctrl_dst;
  97. while (dst) {
  98. if (fromlen == dst->addrlen &&
  99. os_memcmp(from->sun_path, dst->addr.sun_path,
  100. fromlen - offsetof(struct sockaddr_un, sun_path))
  101. == 0) {
  102. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  103. "level", (u8 *) from->sun_path, fromlen -
  104. offsetof(struct sockaddr_un, sun_path));
  105. dst->debug_level = atoi(level);
  106. return 0;
  107. }
  108. dst = dst->next;
  109. }
  110. return -1;
  111. }
  112. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  113. const char *txtaddr)
  114. {
  115. u8 addr[ETH_ALEN];
  116. struct sta_info *sta;
  117. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  118. if (hwaddr_aton(txtaddr, addr))
  119. return -1;
  120. sta = ap_get_sta(hapd, addr);
  121. if (sta)
  122. return 0;
  123. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  124. "notification", MAC2STR(addr));
  125. sta = ap_sta_add(hapd, addr);
  126. if (sta == NULL)
  127. return -1;
  128. hostapd_new_assoc_sta(hapd, sta, 0);
  129. return 0;
  130. }
  131. #ifdef CONFIG_IEEE80211W
  132. #ifdef NEED_AP_MLME
  133. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  134. const char *txtaddr)
  135. {
  136. u8 addr[ETH_ALEN];
  137. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  138. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  139. if (hwaddr_aton(txtaddr, addr) ||
  140. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  141. return -1;
  142. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  143. return 0;
  144. }
  145. #endif /* NEED_AP_MLME */
  146. #endif /* CONFIG_IEEE80211W */
  147. #ifdef CONFIG_WPS
  148. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  149. {
  150. char *pin = os_strchr(txt, ' ');
  151. char *timeout_txt;
  152. int timeout;
  153. u8 addr_buf[ETH_ALEN], *addr = NULL;
  154. char *pos;
  155. if (pin == NULL)
  156. return -1;
  157. *pin++ = '\0';
  158. timeout_txt = os_strchr(pin, ' ');
  159. if (timeout_txt) {
  160. *timeout_txt++ = '\0';
  161. timeout = atoi(timeout_txt);
  162. pos = os_strchr(timeout_txt, ' ');
  163. if (pos) {
  164. *pos++ = '\0';
  165. if (hwaddr_aton(pos, addr_buf) == 0)
  166. addr = addr_buf;
  167. }
  168. } else
  169. timeout = 0;
  170. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  171. }
  172. static int hostapd_ctrl_iface_wps_check_pin(
  173. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  174. {
  175. char pin[9];
  176. size_t len;
  177. char *pos;
  178. int ret;
  179. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  180. (u8 *) cmd, os_strlen(cmd));
  181. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  182. if (*pos < '0' || *pos > '9')
  183. continue;
  184. pin[len++] = *pos;
  185. if (len == 9) {
  186. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  187. return -1;
  188. }
  189. }
  190. if (len != 4 && len != 8) {
  191. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  192. return -1;
  193. }
  194. pin[len] = '\0';
  195. if (len == 8) {
  196. unsigned int pin_val;
  197. pin_val = atoi(pin);
  198. if (!wps_pin_valid(pin_val)) {
  199. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  200. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  201. if (ret < 0 || (size_t) ret >= buflen)
  202. return -1;
  203. return ret;
  204. }
  205. }
  206. ret = os_snprintf(buf, buflen, "%s", pin);
  207. if (ret < 0 || (size_t) ret >= buflen)
  208. return -1;
  209. return ret;
  210. }
  211. #ifdef CONFIG_WPS_NFC
  212. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  213. char *pos)
  214. {
  215. size_t len;
  216. struct wpabuf *buf;
  217. int ret;
  218. len = os_strlen(pos);
  219. if (len & 0x01)
  220. return -1;
  221. len /= 2;
  222. buf = wpabuf_alloc(len);
  223. if (buf == NULL)
  224. return -1;
  225. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  226. wpabuf_free(buf);
  227. return -1;
  228. }
  229. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  230. wpabuf_free(buf);
  231. return ret;
  232. }
  233. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  234. char *cmd, char *reply,
  235. size_t max_len)
  236. {
  237. int ndef;
  238. struct wpabuf *buf;
  239. int res;
  240. if (os_strcmp(cmd, "WPS") == 0)
  241. ndef = 0;
  242. else if (os_strcmp(cmd, "NDEF") == 0)
  243. ndef = 1;
  244. else
  245. return -1;
  246. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  247. if (buf == NULL)
  248. return -1;
  249. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  250. wpabuf_len(buf));
  251. reply[res++] = '\n';
  252. reply[res] = '\0';
  253. wpabuf_free(buf);
  254. return res;
  255. }
  256. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  257. char *reply, size_t max_len,
  258. int ndef)
  259. {
  260. struct wpabuf *buf;
  261. int res;
  262. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  263. if (buf == NULL)
  264. return -1;
  265. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  266. wpabuf_len(buf));
  267. reply[res++] = '\n';
  268. reply[res] = '\0';
  269. wpabuf_free(buf);
  270. return res;
  271. }
  272. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  273. char *cmd, char *reply,
  274. size_t max_len)
  275. {
  276. if (os_strcmp(cmd, "WPS") == 0)
  277. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  278. max_len, 0);
  279. if (os_strcmp(cmd, "NDEF") == 0)
  280. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  281. max_len, 1);
  282. if (os_strcmp(cmd, "enable") == 0)
  283. return hostapd_wps_nfc_token_enable(hapd);
  284. if (os_strcmp(cmd, "disable") == 0) {
  285. hostapd_wps_nfc_token_disable(hapd);
  286. return 0;
  287. }
  288. return -1;
  289. }
  290. static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
  291. char *cmd, char *reply,
  292. size_t max_len)
  293. {
  294. struct wpabuf *buf;
  295. int res;
  296. char *pos;
  297. int ndef;
  298. pos = os_strchr(cmd, ' ');
  299. if (pos == NULL)
  300. return -1;
  301. *pos++ = '\0';
  302. if (os_strcmp(cmd, "WPS") == 0)
  303. ndef = 0;
  304. else if (os_strcmp(cmd, "NDEF") == 0)
  305. ndef = 1;
  306. else
  307. return -1;
  308. if (os_strcmp(pos, "WPS-CR") == 0)
  309. buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
  310. else
  311. buf = NULL;
  312. if (buf == NULL)
  313. return -1;
  314. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  315. wpabuf_len(buf));
  316. reply[res++] = '\n';
  317. reply[res] = '\0';
  318. wpabuf_free(buf);
  319. return res;
  320. }
  321. static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
  322. char *cmd)
  323. {
  324. /*
  325. * Since NFC connection handover provided full WPS Credential, there is
  326. * no need for additional operations within hostapd. Just report this in
  327. * debug log.
  328. */
  329. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported: %s", cmd);
  330. return 0;
  331. }
  332. #endif /* CONFIG_WPS_NFC */
  333. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  334. char *buf, size_t buflen)
  335. {
  336. int timeout = 300;
  337. char *pos;
  338. const char *pin_txt;
  339. pos = os_strchr(txt, ' ');
  340. if (pos)
  341. *pos++ = '\0';
  342. if (os_strcmp(txt, "disable") == 0) {
  343. hostapd_wps_ap_pin_disable(hapd);
  344. return os_snprintf(buf, buflen, "OK\n");
  345. }
  346. if (os_strcmp(txt, "random") == 0) {
  347. if (pos)
  348. timeout = atoi(pos);
  349. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  350. if (pin_txt == NULL)
  351. return -1;
  352. return os_snprintf(buf, buflen, "%s", pin_txt);
  353. }
  354. if (os_strcmp(txt, "get") == 0) {
  355. pin_txt = hostapd_wps_ap_pin_get(hapd);
  356. if (pin_txt == NULL)
  357. return -1;
  358. return os_snprintf(buf, buflen, "%s", pin_txt);
  359. }
  360. if (os_strcmp(txt, "set") == 0) {
  361. char *pin;
  362. if (pos == NULL)
  363. return -1;
  364. pin = pos;
  365. pos = os_strchr(pos, ' ');
  366. if (pos) {
  367. *pos++ = '\0';
  368. timeout = atoi(pos);
  369. }
  370. if (os_strlen(pin) > buflen)
  371. return -1;
  372. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  373. return -1;
  374. return os_snprintf(buf, buflen, "%s", pin);
  375. }
  376. return -1;
  377. }
  378. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  379. {
  380. char *pos;
  381. char *ssid, *auth, *encr = NULL, *key = NULL;
  382. ssid = txt;
  383. pos = os_strchr(txt, ' ');
  384. if (!pos)
  385. return -1;
  386. *pos++ = '\0';
  387. auth = pos;
  388. pos = os_strchr(pos, ' ');
  389. if (pos) {
  390. *pos++ = '\0';
  391. encr = pos;
  392. pos = os_strchr(pos, ' ');
  393. if (pos) {
  394. *pos++ = '\0';
  395. key = pos;
  396. }
  397. }
  398. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  399. }
  400. static const char * pbc_status_str(enum pbc_status status)
  401. {
  402. switch (status) {
  403. case WPS_PBC_STATUS_DISABLE:
  404. return "Disabled";
  405. case WPS_PBC_STATUS_ACTIVE:
  406. return "Active";
  407. case WPS_PBC_STATUS_TIMEOUT:
  408. return "Timed-out";
  409. case WPS_PBC_STATUS_OVERLAP:
  410. return "Overlap";
  411. default:
  412. return "Unknown";
  413. }
  414. }
  415. static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
  416. char *buf, size_t buflen)
  417. {
  418. int ret;
  419. char *pos, *end;
  420. pos = buf;
  421. end = buf + buflen;
  422. ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
  423. pbc_status_str(hapd->wps_stats.pbc_status));
  424. if (ret < 0 || ret >= end - pos)
  425. return pos - buf;
  426. pos += ret;
  427. ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
  428. (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
  429. "Success":
  430. (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
  431. "Failed" : "None")));
  432. if (ret < 0 || ret >= end - pos)
  433. return pos - buf;
  434. pos += ret;
  435. /* If status == Failure - Add possible Reasons */
  436. if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
  437. hapd->wps_stats.failure_reason > 0) {
  438. ret = os_snprintf(pos, end - pos,
  439. "Failure Reason: %s\n",
  440. wps_ei_str(hapd->wps_stats.failure_reason));
  441. if (ret < 0 || ret >= end - pos)
  442. return pos - buf;
  443. pos += ret;
  444. }
  445. if (hapd->wps_stats.status) {
  446. ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
  447. MAC2STR(hapd->wps_stats.peer_addr));
  448. if (ret < 0 || ret >= end - pos)
  449. return pos - buf;
  450. pos += ret;
  451. }
  452. return pos - buf;
  453. }
  454. #endif /* CONFIG_WPS */
  455. #ifdef CONFIG_INTERWORKING
  456. static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
  457. const char *cmd)
  458. {
  459. u8 qos_map_set[16 + 2 * 21], count = 0;
  460. const char *pos = cmd;
  461. int val, ret;
  462. for (;;) {
  463. if (count == sizeof(qos_map_set)) {
  464. wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
  465. return -1;
  466. }
  467. val = atoi(pos);
  468. if (val < 0 || val > 255) {
  469. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  470. return -1;
  471. }
  472. qos_map_set[count++] = val;
  473. pos = os_strchr(pos, ',');
  474. if (!pos)
  475. break;
  476. pos++;
  477. }
  478. if (count < 16 || count & 1) {
  479. wpa_printf(MSG_INFO, "Invalid QoS Map Set");
  480. return -1;
  481. }
  482. ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
  483. if (ret) {
  484. wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
  485. return -1;
  486. }
  487. os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
  488. hapd->conf->qos_map_set_len = count;
  489. return 0;
  490. }
  491. static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
  492. const char *cmd)
  493. {
  494. u8 addr[ETH_ALEN];
  495. struct sta_info *sta;
  496. struct wpabuf *buf;
  497. u8 *qos_map_set = hapd->conf->qos_map_set;
  498. u8 qos_map_set_len = hapd->conf->qos_map_set_len;
  499. int ret;
  500. if (!qos_map_set_len) {
  501. wpa_printf(MSG_INFO, "QoS Map Set is not set");
  502. return -1;
  503. }
  504. if (hwaddr_aton(cmd, addr))
  505. return -1;
  506. sta = ap_get_sta(hapd, addr);
  507. if (sta == NULL) {
  508. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  509. "for QoS Map Configuration message",
  510. MAC2STR(addr));
  511. return -1;
  512. }
  513. if (!sta->qos_map_enabled) {
  514. wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
  515. "support for QoS Map", MAC2STR(addr));
  516. return -1;
  517. }
  518. buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
  519. if (buf == NULL)
  520. return -1;
  521. wpabuf_put_u8(buf, WLAN_ACTION_QOS);
  522. wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
  523. /* QoS Map Set Element */
  524. wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
  525. wpabuf_put_u8(buf, qos_map_set_len);
  526. wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
  527. ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
  528. wpabuf_head(buf), wpabuf_len(buf));
  529. wpabuf_free(buf);
  530. return ret;
  531. }
  532. #endif /* CONFIG_INTERWORKING */
  533. #ifdef CONFIG_WNM
  534. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  535. const char *cmd)
  536. {
  537. u8 addr[ETH_ALEN];
  538. int disassoc_timer;
  539. struct sta_info *sta;
  540. if (hwaddr_aton(cmd, addr))
  541. return -1;
  542. if (cmd[17] != ' ')
  543. return -1;
  544. disassoc_timer = atoi(cmd + 17);
  545. sta = ap_get_sta(hapd, addr);
  546. if (sta == NULL) {
  547. wpa_printf(MSG_DEBUG, "Station " MACSTR
  548. " not found for disassociation imminent message",
  549. MAC2STR(addr));
  550. return -1;
  551. }
  552. return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
  553. }
  554. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  555. const char *cmd)
  556. {
  557. u8 addr[ETH_ALEN];
  558. const char *url, *timerstr;
  559. int disassoc_timer;
  560. struct sta_info *sta;
  561. if (hwaddr_aton(cmd, addr))
  562. return -1;
  563. sta = ap_get_sta(hapd, addr);
  564. if (sta == NULL) {
  565. wpa_printf(MSG_DEBUG, "Station " MACSTR
  566. " not found for ESS disassociation imminent message",
  567. MAC2STR(addr));
  568. return -1;
  569. }
  570. timerstr = cmd + 17;
  571. if (*timerstr != ' ')
  572. return -1;
  573. timerstr++;
  574. disassoc_timer = atoi(timerstr);
  575. if (disassoc_timer < 0 || disassoc_timer > 65535)
  576. return -1;
  577. url = os_strchr(timerstr, ' ');
  578. if (url == NULL)
  579. return -1;
  580. url++;
  581. return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
  582. }
  583. #endif /* CONFIG_WNM */
  584. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  585. char *buf, size_t buflen)
  586. {
  587. int ret;
  588. char *pos, *end;
  589. pos = buf;
  590. end = buf + buflen;
  591. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  592. "ssid=%s\n",
  593. MAC2STR(hapd->own_addr),
  594. wpa_ssid_txt(hapd->conf->ssid.ssid,
  595. hapd->conf->ssid.ssid_len));
  596. if (ret < 0 || ret >= end - pos)
  597. return pos - buf;
  598. pos += ret;
  599. #ifdef CONFIG_WPS
  600. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  601. hapd->conf->wps_state == 0 ? "disabled" :
  602. (hapd->conf->wps_state == 1 ? "not configured" :
  603. "configured"));
  604. if (ret < 0 || ret >= end - pos)
  605. return pos - buf;
  606. pos += ret;
  607. if (hapd->conf->wps_state && hapd->conf->wpa &&
  608. hapd->conf->ssid.wpa_passphrase) {
  609. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  610. hapd->conf->ssid.wpa_passphrase);
  611. if (ret < 0 || ret >= end - pos)
  612. return pos - buf;
  613. pos += ret;
  614. }
  615. if (hapd->conf->wps_state && hapd->conf->wpa &&
  616. hapd->conf->ssid.wpa_psk &&
  617. hapd->conf->ssid.wpa_psk->group) {
  618. char hex[PMK_LEN * 2 + 1];
  619. wpa_snprintf_hex(hex, sizeof(hex),
  620. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  621. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  622. if (ret < 0 || ret >= end - pos)
  623. return pos - buf;
  624. pos += ret;
  625. }
  626. #endif /* CONFIG_WPS */
  627. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  628. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  629. if (ret < 0 || ret >= end - pos)
  630. return pos - buf;
  631. pos += ret;
  632. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  633. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  634. if (ret < 0 || ret >= end - pos)
  635. return pos - buf;
  636. pos += ret;
  637. }
  638. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  639. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  640. if (ret < 0 || ret >= end - pos)
  641. return pos - buf;
  642. pos += ret;
  643. }
  644. #ifdef CONFIG_IEEE80211R
  645. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  646. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  647. if (ret < 0 || ret >= end - pos)
  648. return pos - buf;
  649. pos += ret;
  650. }
  651. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  652. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  653. if (ret < 0 || ret >= end - pos)
  654. return pos - buf;
  655. pos += ret;
  656. }
  657. #endif /* CONFIG_IEEE80211R */
  658. #ifdef CONFIG_IEEE80211W
  659. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  660. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  661. if (ret < 0 || ret >= end - pos)
  662. return pos - buf;
  663. pos += ret;
  664. }
  665. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  666. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  667. if (ret < 0 || ret >= end - pos)
  668. return pos - buf;
  669. pos += ret;
  670. }
  671. #endif /* CONFIG_IEEE80211W */
  672. ret = os_snprintf(pos, end - pos, "\n");
  673. if (ret < 0 || ret >= end - pos)
  674. return pos - buf;
  675. pos += ret;
  676. }
  677. if (hapd->conf->wpa) {
  678. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  679. wpa_cipher_txt(hapd->conf->wpa_group));
  680. if (ret < 0 || ret >= end - pos)
  681. return pos - buf;
  682. pos += ret;
  683. }
  684. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  685. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  686. if (ret < 0 || ret >= end - pos)
  687. return pos - buf;
  688. pos += ret;
  689. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  690. " ");
  691. if (ret < 0)
  692. return pos - buf;
  693. pos += ret;
  694. ret = os_snprintf(pos, end - pos, "\n");
  695. if (ret < 0 || ret >= end - pos)
  696. return pos - buf;
  697. pos += ret;
  698. }
  699. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  700. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  701. if (ret < 0 || ret >= end - pos)
  702. return pos - buf;
  703. pos += ret;
  704. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  705. " ");
  706. if (ret < 0)
  707. return pos - buf;
  708. pos += ret;
  709. ret = os_snprintf(pos, end - pos, "\n");
  710. if (ret < 0 || ret >= end - pos)
  711. return pos - buf;
  712. pos += ret;
  713. }
  714. return pos - buf;
  715. }
  716. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  717. {
  718. char *value;
  719. int ret = 0;
  720. value = os_strchr(cmd, ' ');
  721. if (value == NULL)
  722. return -1;
  723. *value++ = '\0';
  724. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  725. if (0) {
  726. #ifdef CONFIG_WPS_TESTING
  727. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  728. long int val;
  729. val = strtol(value, NULL, 0);
  730. if (val < 0 || val > 0xff) {
  731. ret = -1;
  732. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  733. "wps_version_number %ld", val);
  734. } else {
  735. wps_version_number = val;
  736. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  737. "version %u.%u",
  738. (wps_version_number & 0xf0) >> 4,
  739. wps_version_number & 0x0f);
  740. hostapd_wps_update_ie(hapd);
  741. }
  742. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  743. wps_testing_dummy_cred = atoi(value);
  744. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  745. wps_testing_dummy_cred);
  746. #endif /* CONFIG_WPS_TESTING */
  747. #ifdef CONFIG_INTERWORKING
  748. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  749. int val = atoi(value);
  750. if (val <= 0)
  751. ret = -1;
  752. else
  753. hapd->gas_frag_limit = val;
  754. #endif /* CONFIG_INTERWORKING */
  755. #ifdef CONFIG_TESTING_OPTIONS
  756. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  757. hapd->ext_mgmt_frame_handling = atoi(value);
  758. #endif /* CONFIG_TESTING_OPTIONS */
  759. } else {
  760. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  761. }
  762. return ret;
  763. }
  764. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  765. char *buf, size_t buflen)
  766. {
  767. int res;
  768. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  769. if (os_strcmp(cmd, "version") == 0) {
  770. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  771. if (res < 0 || (unsigned int) res >= buflen)
  772. return -1;
  773. return res;
  774. }
  775. return -1;
  776. }
  777. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  778. {
  779. if (hostapd_enable_iface(iface) < 0) {
  780. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  781. return -1;
  782. }
  783. return 0;
  784. }
  785. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  786. {
  787. if (hostapd_reload_iface(iface) < 0) {
  788. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  789. return -1;
  790. }
  791. return 0;
  792. }
  793. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  794. {
  795. if (hostapd_disable_iface(iface) < 0) {
  796. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  797. return -1;
  798. }
  799. return 0;
  800. }
  801. #ifdef CONFIG_TESTING_OPTIONS
  802. static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
  803. {
  804. union wpa_event_data data;
  805. char *pos, *param;
  806. enum wpa_event_type event;
  807. wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
  808. os_memset(&data, 0, sizeof(data));
  809. param = os_strchr(cmd, ' ');
  810. if (param == NULL)
  811. return -1;
  812. *param++ = '\0';
  813. if (os_strcmp(cmd, "DETECTED") == 0)
  814. event = EVENT_DFS_RADAR_DETECTED;
  815. else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
  816. event = EVENT_DFS_CAC_FINISHED;
  817. else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
  818. event = EVENT_DFS_CAC_ABORTED;
  819. else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
  820. event = EVENT_DFS_NOP_FINISHED;
  821. else {
  822. wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
  823. cmd);
  824. return -1;
  825. }
  826. pos = os_strstr(param, "freq=");
  827. if (pos)
  828. data.dfs_event.freq = atoi(pos + 5);
  829. pos = os_strstr(param, "ht_enabled=1");
  830. if (pos)
  831. data.dfs_event.ht_enabled = 1;
  832. pos = os_strstr(param, "chan_offset=");
  833. if (pos)
  834. data.dfs_event.chan_offset = atoi(pos + 12);
  835. pos = os_strstr(param, "chan_width=");
  836. if (pos)
  837. data.dfs_event.chan_width = atoi(pos + 11);
  838. pos = os_strstr(param, "cf1=");
  839. if (pos)
  840. data.dfs_event.cf1 = atoi(pos + 4);
  841. pos = os_strstr(param, "cf2=");
  842. if (pos)
  843. data.dfs_event.cf2 = atoi(pos + 4);
  844. wpa_supplicant_event(hapd, event, &data);
  845. return 0;
  846. }
  847. static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
  848. {
  849. size_t len;
  850. u8 *buf;
  851. int res;
  852. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  853. len = os_strlen(cmd);
  854. if (len & 1)
  855. return -1;
  856. len /= 2;
  857. buf = os_malloc(len);
  858. if (buf == NULL)
  859. return -1;
  860. if (hexstr2bin(cmd, buf, len) < 0) {
  861. os_free(buf);
  862. return -1;
  863. }
  864. res = hostapd_drv_send_mlme(hapd, buf, len, 0);
  865. os_free(buf);
  866. return res;
  867. }
  868. #endif /* CONFIG_TESTING_OPTIONS */
  869. static int hostapd_ctrl_iface_chan_switch(struct hostapd_data *hapd, char *pos)
  870. {
  871. #ifdef NEED_AP_MLME
  872. struct csa_settings settings;
  873. int ret = hostapd_parse_csa_settings(pos, &settings);
  874. if (ret)
  875. return ret;
  876. return hostapd_switch_channel(hapd, &settings);
  877. #else /* NEED_AP_MLME */
  878. return -1;
  879. #endif /* NEED_AP_MLME */
  880. }
  881. static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
  882. int reply_size, const char *param)
  883. {
  884. #ifdef RADIUS_SERVER
  885. if (os_strcmp(param, "radius_server") == 0) {
  886. return radius_server_get_mib(hapd->radius_srv, reply,
  887. reply_size);
  888. }
  889. #endif /* RADIUS_SERVER */
  890. return -1;
  891. }
  892. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  893. void *sock_ctx)
  894. {
  895. struct hostapd_data *hapd = eloop_ctx;
  896. char buf[256];
  897. int res;
  898. struct sockaddr_un from;
  899. socklen_t fromlen = sizeof(from);
  900. char *reply;
  901. const int reply_size = 4096;
  902. int reply_len;
  903. int level = MSG_DEBUG;
  904. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  905. (struct sockaddr *) &from, &fromlen);
  906. if (res < 0) {
  907. perror("recvfrom(ctrl_iface)");
  908. return;
  909. }
  910. buf[res] = '\0';
  911. if (os_strcmp(buf, "PING") == 0)
  912. level = MSG_EXCESSIVE;
  913. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  914. reply = os_malloc(reply_size);
  915. if (reply == NULL) {
  916. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  917. fromlen);
  918. return;
  919. }
  920. os_memcpy(reply, "OK\n", 3);
  921. reply_len = 3;
  922. if (os_strcmp(buf, "PING") == 0) {
  923. os_memcpy(reply, "PONG\n", 5);
  924. reply_len = 5;
  925. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  926. if (wpa_debug_reopen_file() < 0)
  927. reply_len = -1;
  928. } else if (os_strcmp(buf, "STATUS") == 0) {
  929. reply_len = hostapd_ctrl_iface_status(hapd, reply,
  930. reply_size);
  931. } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
  932. reply_len = hostapd_drv_status(hapd, reply, reply_size);
  933. } else if (os_strcmp(buf, "MIB") == 0) {
  934. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  935. if (reply_len >= 0) {
  936. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  937. reply_size - reply_len);
  938. if (res < 0)
  939. reply_len = -1;
  940. else
  941. reply_len += res;
  942. }
  943. if (reply_len >= 0) {
  944. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  945. reply_size - reply_len);
  946. if (res < 0)
  947. reply_len = -1;
  948. else
  949. reply_len += res;
  950. }
  951. #ifndef CONFIG_NO_RADIUS
  952. if (reply_len >= 0) {
  953. res = radius_client_get_mib(hapd->radius,
  954. reply + reply_len,
  955. reply_size - reply_len);
  956. if (res < 0)
  957. reply_len = -1;
  958. else
  959. reply_len += res;
  960. }
  961. #endif /* CONFIG_NO_RADIUS */
  962. } else if (os_strncmp(buf, "MIB ", 4) == 0) {
  963. reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
  964. buf + 4);
  965. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  966. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  967. reply_size);
  968. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  969. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  970. reply_size);
  971. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  972. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  973. reply_size);
  974. } else if (os_strcmp(buf, "ATTACH") == 0) {
  975. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  976. reply_len = -1;
  977. } else if (os_strcmp(buf, "DETACH") == 0) {
  978. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  979. reply_len = -1;
  980. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  981. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  982. buf + 6))
  983. reply_len = -1;
  984. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  985. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  986. reply_len = -1;
  987. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  988. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  989. reply_len = -1;
  990. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  991. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  992. reply_len = -1;
  993. #ifdef CONFIG_IEEE80211W
  994. #ifdef NEED_AP_MLME
  995. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  996. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  997. reply_len = -1;
  998. #endif /* NEED_AP_MLME */
  999. #endif /* CONFIG_IEEE80211W */
  1000. #ifdef CONFIG_WPS
  1001. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1002. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  1003. reply_len = -1;
  1004. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  1005. reply_len = hostapd_ctrl_iface_wps_check_pin(
  1006. hapd, buf + 14, reply, reply_size);
  1007. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1008. if (hostapd_wps_button_pushed(hapd, NULL))
  1009. reply_len = -1;
  1010. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  1011. if (hostapd_wps_cancel(hapd))
  1012. reply_len = -1;
  1013. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  1014. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  1015. reply, reply_size);
  1016. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  1017. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  1018. reply_len = -1;
  1019. } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
  1020. reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
  1021. reply_size);
  1022. #ifdef CONFIG_WPS_NFC
  1023. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  1024. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  1025. reply_len = -1;
  1026. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  1027. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  1028. hapd, buf + 21, reply, reply_size);
  1029. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  1030. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  1031. hapd, buf + 14, reply, reply_size);
  1032. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  1033. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  1034. hapd, buf + 21, reply, reply_size);
  1035. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  1036. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  1037. reply_len = -1;
  1038. #endif /* CONFIG_WPS_NFC */
  1039. #endif /* CONFIG_WPS */
  1040. #ifdef CONFIG_INTERWORKING
  1041. } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
  1042. if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
  1043. reply_len = -1;
  1044. } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
  1045. if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
  1046. reply_len = -1;
  1047. #endif /* CONFIG_INTERWORKING */
  1048. #ifdef CONFIG_WNM
  1049. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  1050. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  1051. reply_len = -1;
  1052. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  1053. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  1054. reply_len = -1;
  1055. #endif /* CONFIG_WNM */
  1056. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  1057. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  1058. reply_size);
  1059. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1060. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  1061. reply_len = -1;
  1062. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  1063. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  1064. reply_size);
  1065. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  1066. if (hostapd_ctrl_iface_enable(hapd->iface))
  1067. reply_len = -1;
  1068. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  1069. if (hostapd_ctrl_iface_reload(hapd->iface))
  1070. reply_len = -1;
  1071. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  1072. if (hostapd_ctrl_iface_disable(hapd->iface))
  1073. reply_len = -1;
  1074. #ifdef CONFIG_TESTING_OPTIONS
  1075. } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
  1076. if (hostapd_ctrl_iface_radar(hapd, buf + 6))
  1077. reply_len = -1;
  1078. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  1079. if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
  1080. reply_len = -1;
  1081. #endif /* CONFIG_TESTING_OPTIONS */
  1082. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  1083. if (hostapd_ctrl_iface_chan_switch(hapd, buf + 12))
  1084. reply_len = -1;
  1085. } else {
  1086. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1087. reply_len = 16;
  1088. }
  1089. if (reply_len < 0) {
  1090. os_memcpy(reply, "FAIL\n", 5);
  1091. reply_len = 5;
  1092. }
  1093. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1094. os_free(reply);
  1095. }
  1096. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  1097. {
  1098. char *buf;
  1099. size_t len;
  1100. if (hapd->conf->ctrl_interface == NULL)
  1101. return NULL;
  1102. len = os_strlen(hapd->conf->ctrl_interface) +
  1103. os_strlen(hapd->conf->iface) + 2;
  1104. buf = os_malloc(len);
  1105. if (buf == NULL)
  1106. return NULL;
  1107. os_snprintf(buf, len, "%s/%s",
  1108. hapd->conf->ctrl_interface, hapd->conf->iface);
  1109. buf[len - 1] = '\0';
  1110. return buf;
  1111. }
  1112. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
  1113. const char *txt, size_t len)
  1114. {
  1115. struct hostapd_data *hapd = ctx;
  1116. if (hapd == NULL)
  1117. return;
  1118. hostapd_ctrl_iface_send(hapd, level, txt, len);
  1119. }
  1120. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  1121. {
  1122. struct sockaddr_un addr;
  1123. int s = -1;
  1124. char *fname = NULL;
  1125. if (hapd->ctrl_sock > -1) {
  1126. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  1127. return 0;
  1128. }
  1129. if (hapd->conf->ctrl_interface == NULL)
  1130. return 0;
  1131. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1132. if (errno == EEXIST) {
  1133. wpa_printf(MSG_DEBUG, "Using existing control "
  1134. "interface directory.");
  1135. } else {
  1136. perror("mkdir[ctrl_interface]");
  1137. goto fail;
  1138. }
  1139. }
  1140. if (hapd->conf->ctrl_interface_gid_set &&
  1141. chown(hapd->conf->ctrl_interface, -1,
  1142. hapd->conf->ctrl_interface_gid) < 0) {
  1143. perror("chown[ctrl_interface]");
  1144. return -1;
  1145. }
  1146. if (!hapd->conf->ctrl_interface_gid_set &&
  1147. hapd->iface->interfaces->ctrl_iface_group &&
  1148. chown(hapd->conf->ctrl_interface, -1,
  1149. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1150. perror("chown[ctrl_interface]");
  1151. return -1;
  1152. }
  1153. #ifdef ANDROID
  1154. /*
  1155. * Android is using umask 0077 which would leave the control interface
  1156. * directory without group access. This breaks things since Wi-Fi
  1157. * framework assumes that this directory can be accessed by other
  1158. * applications in the wifi group. Fix this by adding group access even
  1159. * if umask value would prevent this.
  1160. */
  1161. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  1162. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  1163. strerror(errno));
  1164. /* Try to continue anyway */
  1165. }
  1166. #endif /* ANDROID */
  1167. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  1168. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  1169. goto fail;
  1170. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1171. if (s < 0) {
  1172. perror("socket(PF_UNIX)");
  1173. goto fail;
  1174. }
  1175. os_memset(&addr, 0, sizeof(addr));
  1176. #ifdef __FreeBSD__
  1177. addr.sun_len = sizeof(addr);
  1178. #endif /* __FreeBSD__ */
  1179. addr.sun_family = AF_UNIX;
  1180. fname = hostapd_ctrl_iface_path(hapd);
  1181. if (fname == NULL)
  1182. goto fail;
  1183. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1184. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1185. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1186. strerror(errno));
  1187. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1188. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1189. " allow connections - assuming it was left"
  1190. "over from forced program termination");
  1191. if (unlink(fname) < 0) {
  1192. perror("unlink[ctrl_iface]");
  1193. wpa_printf(MSG_ERROR, "Could not unlink "
  1194. "existing ctrl_iface socket '%s'",
  1195. fname);
  1196. goto fail;
  1197. }
  1198. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1199. 0) {
  1200. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  1201. goto fail;
  1202. }
  1203. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1204. "ctrl_iface socket '%s'", fname);
  1205. } else {
  1206. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1207. "be in use - cannot override it");
  1208. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1209. "not used anymore", fname);
  1210. os_free(fname);
  1211. fname = NULL;
  1212. goto fail;
  1213. }
  1214. }
  1215. if (hapd->conf->ctrl_interface_gid_set &&
  1216. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  1217. perror("chown[ctrl_interface/ifname]");
  1218. goto fail;
  1219. }
  1220. if (!hapd->conf->ctrl_interface_gid_set &&
  1221. hapd->iface->interfaces->ctrl_iface_group &&
  1222. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1223. perror("chown[ctrl_interface/ifname]");
  1224. goto fail;
  1225. }
  1226. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1227. perror("chmod[ctrl_interface/ifname]");
  1228. goto fail;
  1229. }
  1230. os_free(fname);
  1231. hapd->ctrl_sock = s;
  1232. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  1233. NULL);
  1234. hapd->msg_ctx = hapd;
  1235. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  1236. return 0;
  1237. fail:
  1238. if (s >= 0)
  1239. close(s);
  1240. if (fname) {
  1241. unlink(fname);
  1242. os_free(fname);
  1243. }
  1244. return -1;
  1245. }
  1246. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  1247. {
  1248. struct wpa_ctrl_dst *dst, *prev;
  1249. if (hapd->ctrl_sock > -1) {
  1250. char *fname;
  1251. eloop_unregister_read_sock(hapd->ctrl_sock);
  1252. close(hapd->ctrl_sock);
  1253. hapd->ctrl_sock = -1;
  1254. fname = hostapd_ctrl_iface_path(hapd);
  1255. if (fname)
  1256. unlink(fname);
  1257. os_free(fname);
  1258. if (hapd->conf->ctrl_interface &&
  1259. rmdir(hapd->conf->ctrl_interface) < 0) {
  1260. if (errno == ENOTEMPTY) {
  1261. wpa_printf(MSG_DEBUG, "Control interface "
  1262. "directory not empty - leaving it "
  1263. "behind");
  1264. } else {
  1265. wpa_printf(MSG_ERROR,
  1266. "rmdir[ctrl_interface=%s]: %s",
  1267. hapd->conf->ctrl_interface,
  1268. strerror(errno));
  1269. }
  1270. }
  1271. }
  1272. dst = hapd->ctrl_dst;
  1273. while (dst) {
  1274. prev = dst;
  1275. dst = dst->next;
  1276. os_free(prev);
  1277. }
  1278. }
  1279. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1280. char *buf)
  1281. {
  1282. if (hostapd_add_iface(interfaces, buf) < 0) {
  1283. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1284. return -1;
  1285. }
  1286. return 0;
  1287. }
  1288. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1289. char *buf)
  1290. {
  1291. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1292. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1293. return -1;
  1294. }
  1295. return 0;
  1296. }
  1297. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1298. void *sock_ctx)
  1299. {
  1300. void *interfaces = eloop_ctx;
  1301. char buf[256];
  1302. int res;
  1303. struct sockaddr_un from;
  1304. socklen_t fromlen = sizeof(from);
  1305. char reply[24];
  1306. int reply_len;
  1307. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1308. (struct sockaddr *) &from, &fromlen);
  1309. if (res < 0) {
  1310. perror("recvfrom(ctrl_iface)");
  1311. return;
  1312. }
  1313. buf[res] = '\0';
  1314. wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
  1315. os_memcpy(reply, "OK\n", 3);
  1316. reply_len = 3;
  1317. if (os_strcmp(buf, "PING") == 0) {
  1318. os_memcpy(reply, "PONG\n", 5);
  1319. reply_len = 5;
  1320. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  1321. if (wpa_debug_reopen_file() < 0)
  1322. reply_len = -1;
  1323. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1324. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1325. reply_len = -1;
  1326. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1327. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1328. reply_len = -1;
  1329. } else {
  1330. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1331. "ignored");
  1332. reply_len = -1;
  1333. }
  1334. if (reply_len < 0) {
  1335. os_memcpy(reply, "FAIL\n", 5);
  1336. reply_len = 5;
  1337. }
  1338. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1339. }
  1340. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1341. {
  1342. char *buf;
  1343. size_t len;
  1344. if (interface->global_iface_path == NULL)
  1345. return NULL;
  1346. len = os_strlen(interface->global_iface_path) +
  1347. os_strlen(interface->global_iface_name) + 2;
  1348. buf = os_malloc(len);
  1349. if (buf == NULL)
  1350. return NULL;
  1351. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1352. interface->global_iface_name);
  1353. buf[len - 1] = '\0';
  1354. return buf;
  1355. }
  1356. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1357. {
  1358. struct sockaddr_un addr;
  1359. int s = -1;
  1360. char *fname = NULL;
  1361. if (interface->global_iface_path == NULL) {
  1362. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1363. return 0;
  1364. }
  1365. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1366. if (errno == EEXIST) {
  1367. wpa_printf(MSG_DEBUG, "Using existing control "
  1368. "interface directory.");
  1369. } else {
  1370. perror("mkdir[ctrl_interface]");
  1371. goto fail;
  1372. }
  1373. } else if (interface->ctrl_iface_group &&
  1374. chown(interface->global_iface_path, -1,
  1375. interface->ctrl_iface_group) < 0) {
  1376. perror("chown[ctrl_interface]");
  1377. goto fail;
  1378. }
  1379. if (os_strlen(interface->global_iface_path) + 1 +
  1380. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1381. goto fail;
  1382. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1383. if (s < 0) {
  1384. perror("socket(PF_UNIX)");
  1385. goto fail;
  1386. }
  1387. os_memset(&addr, 0, sizeof(addr));
  1388. #ifdef __FreeBSD__
  1389. addr.sun_len = sizeof(addr);
  1390. #endif /* __FreeBSD__ */
  1391. addr.sun_family = AF_UNIX;
  1392. fname = hostapd_global_ctrl_iface_path(interface);
  1393. if (fname == NULL)
  1394. goto fail;
  1395. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1396. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1397. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1398. strerror(errno));
  1399. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1400. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1401. " allow connections - assuming it was left"
  1402. "over from forced program termination");
  1403. if (unlink(fname) < 0) {
  1404. perror("unlink[ctrl_iface]");
  1405. wpa_printf(MSG_ERROR, "Could not unlink "
  1406. "existing ctrl_iface socket '%s'",
  1407. fname);
  1408. goto fail;
  1409. }
  1410. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1411. 0) {
  1412. perror("bind(PF_UNIX)");
  1413. goto fail;
  1414. }
  1415. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1416. "ctrl_iface socket '%s'", fname);
  1417. } else {
  1418. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1419. "be in use - cannot override it");
  1420. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1421. "not used anymore", fname);
  1422. os_free(fname);
  1423. fname = NULL;
  1424. goto fail;
  1425. }
  1426. }
  1427. if (interface->ctrl_iface_group &&
  1428. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  1429. perror("chown[ctrl_interface]");
  1430. goto fail;
  1431. }
  1432. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1433. perror("chmod[ctrl_interface/ifname]");
  1434. goto fail;
  1435. }
  1436. os_free(fname);
  1437. interface->global_ctrl_sock = s;
  1438. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1439. interface, NULL);
  1440. return 0;
  1441. fail:
  1442. if (s >= 0)
  1443. close(s);
  1444. if (fname) {
  1445. unlink(fname);
  1446. os_free(fname);
  1447. }
  1448. return -1;
  1449. }
  1450. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1451. {
  1452. char *fname = NULL;
  1453. if (interfaces->global_ctrl_sock > -1) {
  1454. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1455. close(interfaces->global_ctrl_sock);
  1456. interfaces->global_ctrl_sock = -1;
  1457. fname = hostapd_global_ctrl_iface_path(interfaces);
  1458. if (fname) {
  1459. unlink(fname);
  1460. os_free(fname);
  1461. }
  1462. if (interfaces->global_iface_path &&
  1463. rmdir(interfaces->global_iface_path) < 0) {
  1464. if (errno == ENOTEMPTY) {
  1465. wpa_printf(MSG_DEBUG, "Control interface "
  1466. "directory not empty - leaving it "
  1467. "behind");
  1468. } else {
  1469. wpa_printf(MSG_ERROR,
  1470. "rmdir[ctrl_interface=%s]: %s",
  1471. interfaces->global_iface_path,
  1472. strerror(errno));
  1473. }
  1474. }
  1475. os_free(interfaces->global_iface_path);
  1476. interfaces->global_iface_path = NULL;
  1477. }
  1478. }
  1479. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1480. const char *buf, size_t len)
  1481. {
  1482. struct wpa_ctrl_dst *dst, *next;
  1483. struct msghdr msg;
  1484. int idx;
  1485. struct iovec io[2];
  1486. char levelstr[10];
  1487. dst = hapd->ctrl_dst;
  1488. if (hapd->ctrl_sock < 0 || dst == NULL)
  1489. return;
  1490. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1491. io[0].iov_base = levelstr;
  1492. io[0].iov_len = os_strlen(levelstr);
  1493. io[1].iov_base = (char *) buf;
  1494. io[1].iov_len = len;
  1495. os_memset(&msg, 0, sizeof(msg));
  1496. msg.msg_iov = io;
  1497. msg.msg_iovlen = 2;
  1498. idx = 0;
  1499. while (dst) {
  1500. next = dst->next;
  1501. if (level >= dst->debug_level) {
  1502. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1503. (u8 *) dst->addr.sun_path, dst->addrlen -
  1504. offsetof(struct sockaddr_un, sun_path));
  1505. msg.msg_name = &dst->addr;
  1506. msg.msg_namelen = dst->addrlen;
  1507. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1508. int _errno = errno;
  1509. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1510. "%d - %s",
  1511. idx, errno, strerror(errno));
  1512. dst->errors++;
  1513. if (dst->errors > 10 || _errno == ENOENT) {
  1514. hostapd_ctrl_iface_detach(
  1515. hapd, &dst->addr,
  1516. dst->addrlen);
  1517. }
  1518. } else
  1519. dst->errors = 0;
  1520. }
  1521. idx++;
  1522. dst = next;
  1523. }
  1524. }
  1525. #endif /* CONFIG_NATIVE_WINDOWS */