ap.c 18 KB

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