ap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * WPA Supplicant - Basic AP mode support routines
  3. * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2009, Atheros Communications
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "utils/includes.h"
  16. #include "utils/common.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/wpa_ctrl.h"
  19. #include "ap/hostapd.h"
  20. #include "ap/ap_config.h"
  21. #ifdef NEED_AP_MLME
  22. #include "ap/ieee802_11.h"
  23. #endif /* NEED_AP_MLME */
  24. #include "ap/beacon.h"
  25. #include "ap/ieee802_1x.h"
  26. #include "ap/wps_hostapd.h"
  27. #include "ap/ctrl_iface_ap.h"
  28. #include "eap_common/eap_defs.h"
  29. #include "eap_server/eap_methods.h"
  30. #include "eap_common/eap_wsc_common.h"
  31. #include "wps/wps.h"
  32. #include "common/ieee802_11_defs.h"
  33. #include "config_ssid.h"
  34. #include "config.h"
  35. #include "wpa_supplicant_i.h"
  36. #include "driver_i.h"
  37. #include "p2p_supplicant.h"
  38. #include "ap.h"
  39. #include "ap/sta_info.h"
  40. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  41. struct wpa_ssid *ssid,
  42. struct hostapd_config *conf)
  43. {
  44. struct hostapd_bss_config *bss = &conf->bss[0];
  45. int pairwise;
  46. conf->driver = wpa_s->driver;
  47. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  48. if (ssid->frequency == 0) {
  49. /* default channel 11 */
  50. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  51. conf->channel = 11;
  52. } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
  53. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  54. conf->channel = (ssid->frequency - 2407) / 5;
  55. } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
  56. (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
  57. conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
  58. conf->channel = (ssid->frequency - 5000) / 5;
  59. } else {
  60. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  61. ssid->frequency);
  62. return -1;
  63. }
  64. /* TODO: enable HT if driver supports it;
  65. * drop to 11b if driver does not support 11g */
  66. #ifdef CONFIG_P2P
  67. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
  68. /* Remove 802.11b rates from supported and basic rate sets */
  69. int *list = os_malloc(4 * sizeof(int));
  70. if (list) {
  71. list[0] = 60;
  72. list[1] = 120;
  73. list[2] = 240;
  74. list[3] = -1;
  75. }
  76. conf->basic_rates = list;
  77. list = os_malloc(9 * sizeof(int));
  78. if (list) {
  79. list[0] = 60;
  80. list[1] = 90;
  81. list[2] = 120;
  82. list[3] = 180;
  83. list[4] = 240;
  84. list[5] = 360;
  85. list[6] = 480;
  86. list[7] = 540;
  87. list[8] = -1;
  88. }
  89. conf->supported_rates = list;
  90. }
  91. #endif /* CONFIG_P2P */
  92. if (ssid->ssid_len == 0) {
  93. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  94. return -1;
  95. }
  96. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  97. bss->ssid.ssid[ssid->ssid_len] = '\0';
  98. bss->ssid.ssid_len = ssid->ssid_len;
  99. bss->ssid.ssid_set = 1;
  100. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  101. bss->wpa = ssid->proto;
  102. bss->wpa_key_mgmt = ssid->key_mgmt;
  103. bss->wpa_pairwise = ssid->pairwise_cipher;
  104. if (ssid->passphrase) {
  105. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  106. } else if (ssid->psk_set) {
  107. os_free(bss->ssid.wpa_psk);
  108. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  109. if (bss->ssid.wpa_psk == NULL)
  110. return -1;
  111. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  112. bss->ssid.wpa_psk->group = 1;
  113. }
  114. /* Select group cipher based on the enabled pairwise cipher suites */
  115. pairwise = 0;
  116. if (bss->wpa & 1)
  117. pairwise |= bss->wpa_pairwise;
  118. if (bss->wpa & 2) {
  119. if (bss->rsn_pairwise == 0)
  120. bss->rsn_pairwise = bss->wpa_pairwise;
  121. pairwise |= bss->rsn_pairwise;
  122. }
  123. if (pairwise & WPA_CIPHER_TKIP)
  124. bss->wpa_group = WPA_CIPHER_TKIP;
  125. else
  126. bss->wpa_group = WPA_CIPHER_CCMP;
  127. if (bss->wpa && bss->ieee802_1x)
  128. bss->ssid.security_policy = SECURITY_WPA;
  129. else if (bss->wpa)
  130. bss->ssid.security_policy = SECURITY_WPA_PSK;
  131. else if (bss->ieee802_1x) {
  132. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  133. bss->ssid.wep.default_len = bss->default_wep_key_len;
  134. } else if (bss->ssid.wep.keys_set)
  135. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  136. else
  137. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  138. #ifdef CONFIG_WPS
  139. /*
  140. * Enable WPS by default, but require user interaction to actually use
  141. * it. Only the internal Registrar is supported.
  142. */
  143. bss->eap_server = 1;
  144. bss->wps_state = 2;
  145. bss->ap_setup_locked = 1;
  146. if (wpa_s->conf->config_methods)
  147. bss->config_methods = os_strdup(wpa_s->conf->config_methods);
  148. if (wpa_s->conf->device_type)
  149. bss->device_type = os_strdup(wpa_s->conf->device_type);
  150. if (wpa_s->conf->device_name) {
  151. bss->device_name = os_strdup(wpa_s->conf->device_name);
  152. bss->friendly_name = os_strdup(wpa_s->conf->device_name);
  153. }
  154. if (wpa_s->conf->manufacturer)
  155. bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
  156. if (wpa_s->conf->model_name)
  157. bss->model_name = os_strdup(wpa_s->conf->model_name);
  158. if (wpa_s->conf->model_number)
  159. bss->model_number = os_strdup(wpa_s->conf->model_number);
  160. if (wpa_s->conf->serial_number)
  161. bss->serial_number = os_strdup(wpa_s->conf->serial_number);
  162. os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  163. os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
  164. #endif /* CONFIG_WPS */
  165. bss->max_num_sta = wpa_s->conf->max_num_sta;
  166. return 0;
  167. }
  168. static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  169. {
  170. #ifdef CONFIG_P2P
  171. struct wpa_supplicant *wpa_s = ctx;
  172. const struct ieee80211_mgmt *mgmt;
  173. size_t hdr_len;
  174. mgmt = (const struct ieee80211_mgmt *) buf;
  175. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  176. if (hdr_len > len)
  177. return;
  178. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  179. mgmt->u.action.category,
  180. &mgmt->u.action.u.vs_public_action.action,
  181. len - hdr_len, freq);
  182. #endif /* CONFIG_P2P */
  183. }
  184. static void ap_wps_event_cb(void *ctx, enum wps_event event,
  185. union wps_event_data *data)
  186. {
  187. struct wpa_supplicant *wpa_s = ctx;
  188. if (event == WPS_EV_FAIL && wpa_s->parent && wpa_s->parent != wpa_s) {
  189. struct wps_event_fail *fail = &data->fail;
  190. /*
  191. * src/ap/wps_hostapd.c has already sent this on the main
  192. * interface, so only send on the parent interface here if
  193. * needed.
  194. */
  195. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  196. "msg=%d config_error=%d",
  197. fail->msg, fail->config_error);
  198. }
  199. }
  200. static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  201. {
  202. #ifdef CONFIG_P2P
  203. struct wpa_supplicant *wpa_s = ctx;
  204. const struct ieee80211_mgmt *mgmt;
  205. size_t hdr_len;
  206. mgmt = (const struct ieee80211_mgmt *) buf;
  207. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  208. if (hdr_len > len)
  209. return -1;
  210. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  211. mgmt->u.action.category,
  212. &mgmt->u.action.u.vs_public_action.action,
  213. len - hdr_len, freq);
  214. #endif /* CONFIG_P2P */
  215. return 0;
  216. }
  217. static int ap_probe_req_rx(void *ctx, const u8 *addr, const u8 *ie,
  218. size_t ie_len)
  219. {
  220. #ifdef CONFIG_P2P
  221. struct wpa_supplicant *wpa_s = ctx;
  222. return wpas_p2p_probe_req_rx(wpa_s, addr, ie, ie_len);
  223. #else /* CONFIG_P2P */
  224. return 0;
  225. #endif /* CONFIG_P2P */
  226. }
  227. static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  228. const u8 *uuid_e)
  229. {
  230. #ifdef CONFIG_P2P
  231. struct wpa_supplicant *wpa_s = ctx;
  232. wpas_p2p_wps_success(wpa_s, mac_addr, 1);
  233. #endif /* CONFIG_P2P */
  234. }
  235. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  236. struct wpa_ssid *ssid)
  237. {
  238. struct wpa_driver_associate_params params;
  239. struct hostapd_iface *hapd_iface;
  240. struct hostapd_config *conf;
  241. size_t i;
  242. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  243. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  244. return -1;
  245. }
  246. wpa_supplicant_ap_deinit(wpa_s);
  247. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  248. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  249. os_memset(&params, 0, sizeof(params));
  250. params.ssid = ssid->ssid;
  251. params.ssid_len = ssid->ssid_len;
  252. switch (ssid->mode) {
  253. case WPAS_MODE_INFRA:
  254. params.mode = IEEE80211_MODE_INFRA;
  255. break;
  256. case WPAS_MODE_IBSS:
  257. params.mode = IEEE80211_MODE_IBSS;
  258. break;
  259. case WPAS_MODE_AP:
  260. case WPAS_MODE_P2P_GO:
  261. case WPAS_MODE_P2P_GROUP_FORMATION:
  262. params.mode = IEEE80211_MODE_AP;
  263. break;
  264. }
  265. params.freq = ssid->frequency;
  266. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
  267. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  268. else
  269. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  270. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  271. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  272. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  273. else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
  274. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  275. else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
  276. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  277. else {
  278. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  279. "cipher.");
  280. return -1;
  281. }
  282. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  283. params.group_suite = params.pairwise_suite;
  284. #ifdef CONFIG_P2P
  285. if (ssid->mode == WPAS_MODE_P2P_GO ||
  286. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  287. params.p2p = 1;
  288. wpa_drv_set_intra_bss(wpa_s, wpa_s->conf->p2p_intra_bss);
  289. #endif /* CONFIG_P2P */
  290. if (wpa_s->parent->set_ap_uapsd)
  291. params.uapsd = wpa_s->parent->ap_uapsd;
  292. else
  293. params.uapsd = -1;
  294. if (wpa_drv_associate(wpa_s, &params) < 0) {
  295. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  296. return -1;
  297. }
  298. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  299. if (hapd_iface == NULL)
  300. return -1;
  301. hapd_iface->owner = wpa_s;
  302. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  303. if (conf == NULL) {
  304. wpa_supplicant_ap_deinit(wpa_s);
  305. return -1;
  306. }
  307. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  308. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  309. wpa_supplicant_ap_deinit(wpa_s);
  310. return -1;
  311. }
  312. #ifdef CONFIG_P2P
  313. if (ssid->mode == WPAS_MODE_P2P_GO)
  314. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  315. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  316. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  317. P2P_GROUP_FORMATION;
  318. #endif /* CONFIG_P2P */
  319. hapd_iface->num_bss = conf->num_bss;
  320. hapd_iface->bss = os_zalloc(conf->num_bss *
  321. sizeof(struct hostapd_data *));
  322. if (hapd_iface->bss == NULL) {
  323. wpa_supplicant_ap_deinit(wpa_s);
  324. return -1;
  325. }
  326. for (i = 0; i < conf->num_bss; i++) {
  327. hapd_iface->bss[i] =
  328. hostapd_alloc_bss_data(hapd_iface, conf,
  329. &conf->bss[i]);
  330. if (hapd_iface->bss[i] == NULL) {
  331. wpa_supplicant_ap_deinit(wpa_s);
  332. return -1;
  333. }
  334. hapd_iface->bss[i]->msg_ctx = wpa_s;
  335. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  336. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  337. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  338. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  339. hostapd_register_probereq_cb(hapd_iface->bss[i],
  340. ap_probe_req_rx, wpa_s);
  341. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  342. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  343. hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
  344. hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
  345. #ifdef CONFIG_P2P
  346. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  347. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
  348. wpa_s, ssid->p2p_persistent_group,
  349. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
  350. #endif /* CONFIG_P2P */
  351. }
  352. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  353. hapd_iface->bss[0]->driver = wpa_s->driver;
  354. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  355. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  356. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  357. wpa_supplicant_ap_deinit(wpa_s);
  358. return -1;
  359. }
  360. wpa_s->current_ssid = ssid;
  361. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  362. wpa_s->assoc_freq = ssid->frequency;
  363. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  364. if (wpa_s->ap_configured_cb)
  365. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  366. wpa_s->ap_configured_cb_data);
  367. return 0;
  368. }
  369. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  370. {
  371. if (wpa_s->ap_iface == NULL)
  372. return;
  373. wpa_s->current_ssid = NULL;
  374. #ifdef CONFIG_P2P
  375. if (wpa_s->ap_iface->bss)
  376. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  377. wpas_p2p_group_deinit(wpa_s);
  378. #endif /* CONFIG_P2P */
  379. hostapd_interface_deinit(wpa_s->ap_iface);
  380. hostapd_interface_free(wpa_s->ap_iface);
  381. wpa_s->ap_iface = NULL;
  382. wpa_drv_deinit_ap(wpa_s);
  383. }
  384. void ap_tx_status(void *ctx, const u8 *addr,
  385. const u8 *buf, size_t len, int ack)
  386. {
  387. #ifdef NEED_AP_MLME
  388. struct wpa_supplicant *wpa_s = ctx;
  389. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  390. #endif /* NEED_AP_MLME */
  391. }
  392. void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
  393. {
  394. #ifdef NEED_AP_MLME
  395. struct wpa_supplicant *wpa_s = ctx;
  396. const struct ieee80211_hdr *hdr =
  397. (const struct ieee80211_hdr *) frame;
  398. u16 fc = le_to_host16(hdr->frame_control);
  399. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
  400. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  401. (WLAN_FC_TODS | WLAN_FC_FROMDS));
  402. #endif /* NEED_AP_MLME */
  403. }
  404. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  405. {
  406. #ifdef NEED_AP_MLME
  407. struct wpa_supplicant *wpa_s = ctx;
  408. struct hostapd_frame_info fi;
  409. os_memset(&fi, 0, sizeof(fi));
  410. fi.datarate = rx_mgmt->datarate;
  411. fi.ssi_signal = rx_mgmt->ssi_signal;
  412. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  413. rx_mgmt->frame_len, &fi);
  414. #endif /* NEED_AP_MLME */
  415. }
  416. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  417. {
  418. #ifdef NEED_AP_MLME
  419. struct wpa_supplicant *wpa_s = ctx;
  420. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  421. #endif /* NEED_AP_MLME */
  422. }
  423. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  424. const u8 *src_addr, const u8 *buf, size_t len)
  425. {
  426. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  427. }
  428. #ifdef CONFIG_WPS
  429. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  430. {
  431. if (!wpa_s->ap_iface)
  432. return -1;
  433. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
  434. }
  435. static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
  436. struct sta_info *sta, void *ctx)
  437. {
  438. if (sta && (sta->flags & WLAN_STA_WPS)) {
  439. ap_sta_deauthenticate(hapd, sta,
  440. WLAN_REASON_PREV_AUTH_NOT_VALID);
  441. wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
  442. __func__, MAC2STR(sta->addr));
  443. return 1;
  444. }
  445. return 0;
  446. }
  447. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  448. {
  449. struct wps_registrar *reg;
  450. int reg_sel = 0, wps_sta = 0;
  451. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  452. return -1;
  453. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  454. reg_sel = wps_registrar_wps_cancel(reg);
  455. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  456. wpa_supplicant_ap_wps_sta_cancel, NULL);
  457. if (!reg_sel && !wps_sta) {
  458. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  459. "time");
  460. return -1;
  461. }
  462. /*
  463. * There are 2 cases to return wps cancel as success:
  464. * 1. When wps cancel was initiated but no connection has been
  465. * established with client yet.
  466. * 2. Client is in the middle of exchanging WPS messages.
  467. */
  468. return 0;
  469. }
  470. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  471. const char *pin, char *buf, size_t buflen)
  472. {
  473. int ret, ret_len = 0;
  474. if (!wpa_s->ap_iface)
  475. return -1;
  476. if (pin == NULL) {
  477. unsigned int rpin = wps_generate_pin();
  478. ret_len = os_snprintf(buf, buflen, "%d", rpin);
  479. pin = buf;
  480. } else
  481. ret_len = os_snprintf(buf, buflen, "%s", pin);
  482. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  483. 0);
  484. if (ret)
  485. return -1;
  486. return ret_len;
  487. }
  488. #endif /* CONFIG_WPS */
  489. #ifdef CONFIG_CTRL_IFACE
  490. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  491. char *buf, size_t buflen)
  492. {
  493. if (wpa_s->ap_iface == NULL)
  494. return -1;
  495. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  496. buf, buflen);
  497. }
  498. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  499. char *buf, size_t buflen)
  500. {
  501. if (wpa_s->ap_iface == NULL)
  502. return -1;
  503. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  504. buf, buflen);
  505. }
  506. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  507. char *buf, size_t buflen)
  508. {
  509. if (wpa_s->ap_iface == NULL)
  510. return -1;
  511. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  512. buf, buflen);
  513. }
  514. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  515. size_t buflen, int verbose)
  516. {
  517. char *pos = buf, *end = buf + buflen;
  518. int ret;
  519. struct hostapd_bss_config *conf;
  520. if (wpa_s->ap_iface == NULL)
  521. return -1;
  522. conf = wpa_s->ap_iface->bss[0]->conf;
  523. if (conf->wpa == 0)
  524. return 0;
  525. ret = os_snprintf(pos, end - pos,
  526. "pairwise_cipher=%s\n"
  527. "group_cipher=%s\n"
  528. "key_mgmt=%s\n",
  529. wpa_cipher_txt(conf->rsn_pairwise),
  530. wpa_cipher_txt(conf->wpa_group),
  531. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  532. conf->wpa));
  533. if (ret < 0 || ret >= end - pos)
  534. return pos - buf;
  535. pos += ret;
  536. return pos - buf;
  537. }
  538. #endif /* CONFIG_CTRL_IFACE */
  539. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  540. {
  541. struct hostapd_iface *iface = wpa_s->ap_iface;
  542. struct wpa_ssid *ssid = wpa_s->current_ssid;
  543. struct hostapd_data *hapd;
  544. if (ssid == NULL || wpa_s->ap_iface == NULL)
  545. return -1;
  546. #ifdef CONFIG_P2P
  547. if (ssid->mode == WPAS_MODE_P2P_GO)
  548. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  549. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  550. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  551. P2P_GROUP_FORMATION;
  552. #endif /* CONFIG_P2P */
  553. ieee802_11_set_beacons(iface);
  554. hapd = iface->bss[0];
  555. hapd->drv.set_ap_wps_ie(hapd);
  556. return 0;
  557. }
  558. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  559. const u8 *addr)
  560. {
  561. struct hostapd_data *hapd;
  562. struct hostapd_bss_config *conf;
  563. if (!wpa_s->ap_iface)
  564. return -1;
  565. if (addr)
  566. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  567. MAC2STR(addr));
  568. else
  569. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  570. hapd = wpa_s->ap_iface->bss[0];
  571. conf = hapd->conf;
  572. os_free(conf->accept_mac);
  573. conf->accept_mac = NULL;
  574. conf->num_accept_mac = 0;
  575. os_free(conf->deny_mac);
  576. conf->deny_mac = NULL;
  577. conf->num_deny_mac = 0;
  578. if (addr == NULL) {
  579. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  580. return 0;
  581. }
  582. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  583. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  584. if (conf->accept_mac == NULL)
  585. return -1;
  586. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  587. conf->num_accept_mac = 1;
  588. return 0;
  589. }