ap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. return 0;
  166. }
  167. static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  168. {
  169. #ifdef CONFIG_P2P
  170. struct wpa_supplicant *wpa_s = ctx;
  171. const struct ieee80211_mgmt *mgmt;
  172. size_t hdr_len;
  173. mgmt = (const struct ieee80211_mgmt *) buf;
  174. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  175. if (hdr_len > len)
  176. return;
  177. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  178. mgmt->u.action.category,
  179. &mgmt->u.action.u.vs_public_action.action,
  180. len - hdr_len, freq);
  181. #endif /* CONFIG_P2P */
  182. }
  183. static void ap_wps_event_cb(void *ctx, enum wps_event event,
  184. union wps_event_data *data)
  185. {
  186. struct wpa_supplicant *wpa_s = ctx;
  187. if (event == WPS_EV_FAIL && wpa_s->parent && wpa_s->parent != wpa_s) {
  188. struct wps_event_fail *fail = &data->fail;
  189. /*
  190. * src/ap/wps_hostapd.c has already sent this on the main
  191. * interface, so only send on the parent interface here if
  192. * needed.
  193. */
  194. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  195. "msg=%d config_error=%d",
  196. fail->msg, fail->config_error);
  197. }
  198. }
  199. static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  200. {
  201. #ifdef CONFIG_P2P
  202. struct wpa_supplicant *wpa_s = ctx;
  203. const struct ieee80211_mgmt *mgmt;
  204. size_t hdr_len;
  205. mgmt = (const struct ieee80211_mgmt *) buf;
  206. hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
  207. if (hdr_len > len)
  208. return -1;
  209. wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
  210. mgmt->u.action.category,
  211. &mgmt->u.action.u.vs_public_action.action,
  212. len - hdr_len, freq);
  213. #endif /* CONFIG_P2P */
  214. return 0;
  215. }
  216. static int ap_probe_req_rx(void *ctx, const u8 *addr, const u8 *ie,
  217. size_t ie_len)
  218. {
  219. #ifdef CONFIG_P2P
  220. struct wpa_supplicant *wpa_s = ctx;
  221. return wpas_p2p_probe_req_rx(wpa_s, addr, ie, ie_len);
  222. #else /* CONFIG_P2P */
  223. return 0;
  224. #endif /* CONFIG_P2P */
  225. }
  226. static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  227. const u8 *uuid_e)
  228. {
  229. #ifdef CONFIG_P2P
  230. struct wpa_supplicant *wpa_s = ctx;
  231. wpas_p2p_wps_success(wpa_s, mac_addr, 1);
  232. #endif /* CONFIG_P2P */
  233. }
  234. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  235. struct wpa_ssid *ssid)
  236. {
  237. struct wpa_driver_associate_params params;
  238. struct hostapd_iface *hapd_iface;
  239. struct hostapd_config *conf;
  240. size_t i;
  241. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  242. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  243. return -1;
  244. }
  245. wpa_supplicant_ap_deinit(wpa_s);
  246. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  247. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  248. os_memset(&params, 0, sizeof(params));
  249. params.ssid = ssid->ssid;
  250. params.ssid_len = ssid->ssid_len;
  251. switch (ssid->mode) {
  252. case WPAS_MODE_INFRA:
  253. params.mode = IEEE80211_MODE_INFRA;
  254. break;
  255. case WPAS_MODE_IBSS:
  256. params.mode = IEEE80211_MODE_IBSS;
  257. break;
  258. case WPAS_MODE_AP:
  259. case WPAS_MODE_P2P_GO:
  260. case WPAS_MODE_P2P_GROUP_FORMATION:
  261. params.mode = IEEE80211_MODE_AP;
  262. break;
  263. }
  264. params.freq = ssid->frequency;
  265. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
  266. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  267. else
  268. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  269. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  270. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  271. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  272. else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
  273. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  274. else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
  275. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  276. else {
  277. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  278. "cipher.");
  279. return -1;
  280. }
  281. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  282. params.group_suite = params.pairwise_suite;
  283. #ifdef CONFIG_P2P
  284. if (ssid->mode == WPAS_MODE_P2P_GO ||
  285. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  286. params.p2p = 1;
  287. wpa_drv_set_intra_bss(wpa_s, wpa_s->conf->p2p_intra_bss);
  288. #endif /* CONFIG_P2P */
  289. if (wpa_s->parent->set_ap_uapsd)
  290. params.uapsd = wpa_s->parent->ap_uapsd;
  291. else
  292. params.uapsd = -1;
  293. if (wpa_drv_associate(wpa_s, &params) < 0) {
  294. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  295. return -1;
  296. }
  297. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  298. if (hapd_iface == NULL)
  299. return -1;
  300. hapd_iface->owner = wpa_s;
  301. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  302. if (conf == NULL) {
  303. wpa_supplicant_ap_deinit(wpa_s);
  304. return -1;
  305. }
  306. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  307. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  308. wpa_supplicant_ap_deinit(wpa_s);
  309. return -1;
  310. }
  311. #ifdef CONFIG_P2P
  312. if (ssid->mode == WPAS_MODE_P2P_GO)
  313. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  314. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  315. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  316. P2P_GROUP_FORMATION;
  317. #endif /* CONFIG_P2P */
  318. hapd_iface->num_bss = conf->num_bss;
  319. hapd_iface->bss = os_zalloc(conf->num_bss *
  320. sizeof(struct hostapd_data *));
  321. if (hapd_iface->bss == NULL) {
  322. wpa_supplicant_ap_deinit(wpa_s);
  323. return -1;
  324. }
  325. for (i = 0; i < conf->num_bss; i++) {
  326. hapd_iface->bss[i] =
  327. hostapd_alloc_bss_data(hapd_iface, conf,
  328. &conf->bss[i]);
  329. if (hapd_iface->bss[i] == NULL) {
  330. wpa_supplicant_ap_deinit(wpa_s);
  331. return -1;
  332. }
  333. hapd_iface->bss[i]->msg_ctx = wpa_s;
  334. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  335. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  336. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  337. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  338. hostapd_register_probereq_cb(hapd_iface->bss[i],
  339. ap_probe_req_rx, wpa_s);
  340. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  341. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  342. hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
  343. hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
  344. #ifdef CONFIG_P2P
  345. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  346. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
  347. wpa_s, ssid->p2p_persistent_group,
  348. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
  349. #endif /* CONFIG_P2P */
  350. }
  351. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  352. hapd_iface->bss[0]->driver = wpa_s->driver;
  353. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  354. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  355. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  356. wpa_supplicant_ap_deinit(wpa_s);
  357. return -1;
  358. }
  359. wpa_s->current_ssid = ssid;
  360. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  361. wpa_s->assoc_freq = ssid->frequency;
  362. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  363. if (wpa_s->ap_configured_cb)
  364. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  365. wpa_s->ap_configured_cb_data);
  366. return 0;
  367. }
  368. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  369. {
  370. if (wpa_s->ap_iface == NULL)
  371. return;
  372. wpa_s->current_ssid = NULL;
  373. #ifdef CONFIG_P2P
  374. if (wpa_s->ap_iface->bss)
  375. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  376. wpas_p2p_group_deinit(wpa_s);
  377. #endif /* CONFIG_P2P */
  378. hostapd_interface_deinit(wpa_s->ap_iface);
  379. hostapd_interface_free(wpa_s->ap_iface);
  380. wpa_s->ap_iface = NULL;
  381. wpa_drv_deinit_ap(wpa_s);
  382. }
  383. void ap_tx_status(void *ctx, const u8 *addr,
  384. const u8 *buf, size_t len, int ack)
  385. {
  386. #ifdef NEED_AP_MLME
  387. struct wpa_supplicant *wpa_s = ctx;
  388. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  389. #endif /* NEED_AP_MLME */
  390. }
  391. void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
  392. {
  393. #ifdef NEED_AP_MLME
  394. struct wpa_supplicant *wpa_s = ctx;
  395. const struct ieee80211_hdr *hdr =
  396. (const struct ieee80211_hdr *) frame;
  397. u16 fc = le_to_host16(hdr->frame_control);
  398. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
  399. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  400. (WLAN_FC_TODS | WLAN_FC_FROMDS));
  401. #endif /* NEED_AP_MLME */
  402. }
  403. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  404. {
  405. #ifdef NEED_AP_MLME
  406. struct wpa_supplicant *wpa_s = ctx;
  407. struct hostapd_frame_info fi;
  408. os_memset(&fi, 0, sizeof(fi));
  409. fi.datarate = rx_mgmt->datarate;
  410. fi.ssi_signal = rx_mgmt->ssi_signal;
  411. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  412. rx_mgmt->frame_len, &fi);
  413. #endif /* NEED_AP_MLME */
  414. }
  415. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  416. {
  417. #ifdef NEED_AP_MLME
  418. struct wpa_supplicant *wpa_s = ctx;
  419. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  420. #endif /* NEED_AP_MLME */
  421. }
  422. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  423. const u8 *src_addr, const u8 *buf, size_t len)
  424. {
  425. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  426. }
  427. #ifdef CONFIG_WPS
  428. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  429. {
  430. if (!wpa_s->ap_iface)
  431. return -1;
  432. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
  433. }
  434. static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
  435. struct sta_info *sta, void *ctx)
  436. {
  437. if (sta && (sta->flags & WLAN_STA_WPS)) {
  438. ap_sta_deauthenticate(hapd, sta,
  439. WLAN_REASON_PREV_AUTH_NOT_VALID);
  440. wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
  441. __func__, MAC2STR(sta->addr));
  442. return 1;
  443. }
  444. return 0;
  445. }
  446. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  447. {
  448. struct wps_registrar *reg;
  449. int reg_sel = 0, wps_sta = 0;
  450. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  451. return -1;
  452. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  453. reg_sel = wps_registrar_wps_cancel(reg);
  454. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  455. wpa_supplicant_ap_wps_sta_cancel, NULL);
  456. if (!reg_sel && !wps_sta) {
  457. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  458. "time");
  459. return -1;
  460. }
  461. /*
  462. * There are 2 cases to return wps cancel as success:
  463. * 1. When wps cancel was initiated but no connection has been
  464. * established with client yet.
  465. * 2. Client is in the middle of exchanging WPS messages.
  466. */
  467. return 0;
  468. }
  469. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  470. const char *pin, char *buf, size_t buflen)
  471. {
  472. int ret, ret_len = 0;
  473. if (!wpa_s->ap_iface)
  474. return -1;
  475. if (pin == NULL) {
  476. unsigned int rpin = wps_generate_pin();
  477. ret_len = os_snprintf(buf, buflen, "%d", rpin);
  478. pin = buf;
  479. } else
  480. ret_len = os_snprintf(buf, buflen, "%s", pin);
  481. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  482. 0);
  483. if (ret)
  484. return -1;
  485. return ret_len;
  486. }
  487. #endif /* CONFIG_WPS */
  488. #ifdef CONFIG_CTRL_IFACE
  489. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  490. char *buf, size_t buflen)
  491. {
  492. if (wpa_s->ap_iface == NULL)
  493. return -1;
  494. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  495. buf, buflen);
  496. }
  497. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  498. char *buf, size_t buflen)
  499. {
  500. if (wpa_s->ap_iface == NULL)
  501. return -1;
  502. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  503. buf, buflen);
  504. }
  505. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  506. char *buf, size_t buflen)
  507. {
  508. if (wpa_s->ap_iface == NULL)
  509. return -1;
  510. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  511. buf, buflen);
  512. }
  513. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  514. size_t buflen, int verbose)
  515. {
  516. char *pos = buf, *end = buf + buflen;
  517. int ret;
  518. struct hostapd_bss_config *conf;
  519. if (wpa_s->ap_iface == NULL)
  520. return -1;
  521. conf = wpa_s->ap_iface->bss[0]->conf;
  522. if (conf->wpa == 0)
  523. return 0;
  524. ret = os_snprintf(pos, end - pos,
  525. "pairwise_cipher=%s\n"
  526. "group_cipher=%s\n"
  527. "key_mgmt=%s\n",
  528. wpa_cipher_txt(conf->rsn_pairwise),
  529. wpa_cipher_txt(conf->wpa_group),
  530. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  531. conf->wpa));
  532. if (ret < 0 || ret >= end - pos)
  533. return pos - buf;
  534. pos += ret;
  535. return pos - buf;
  536. }
  537. #endif /* CONFIG_CTRL_IFACE */
  538. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  539. {
  540. struct hostapd_iface *iface = wpa_s->ap_iface;
  541. struct wpa_ssid *ssid = wpa_s->current_ssid;
  542. struct hostapd_data *hapd;
  543. if (ssid == NULL || wpa_s->ap_iface == NULL)
  544. return -1;
  545. #ifdef CONFIG_P2P
  546. if (ssid->mode == WPAS_MODE_P2P_GO)
  547. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  548. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  549. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  550. P2P_GROUP_FORMATION;
  551. #endif /* CONFIG_P2P */
  552. ieee802_11_set_beacons(iface);
  553. hapd = iface->bss[0];
  554. hapd->drv.set_ap_wps_ie(hapd);
  555. return 0;
  556. }
  557. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  558. const u8 *addr)
  559. {
  560. struct hostapd_data *hapd;
  561. struct hostapd_bss_config *conf;
  562. if (!wpa_s->ap_iface)
  563. return -1;
  564. if (addr)
  565. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  566. MAC2STR(addr));
  567. else
  568. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  569. hapd = wpa_s->ap_iface->bss[0];
  570. conf = hapd->conf;
  571. os_free(conf->accept_mac);
  572. conf->accept_mac = NULL;
  573. conf->num_accept_mac = 0;
  574. os_free(conf->deny_mac);
  575. conf->deny_mac = NULL;
  576. conf->num_deny_mac = 0;
  577. if (addr == NULL) {
  578. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  579. return 0;
  580. }
  581. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  582. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  583. if (conf->accept_mac == NULL)
  584. return -1;
  585. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  586. conf->num_accept_mac = 1;
  587. return 0;
  588. }