ap.c 18 KB

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