ctrl_iface.c 42 KB

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