ap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. #endif /* CONFIG_P2P */
  271. if (wpa_s->parent->set_ap_uapsd)
  272. params.uapsd = wpa_s->parent->ap_uapsd;
  273. else
  274. params.uapsd = -1;
  275. if (wpa_drv_associate(wpa_s, &params) < 0) {
  276. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  277. return -1;
  278. }
  279. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  280. if (hapd_iface == NULL)
  281. return -1;
  282. hapd_iface->owner = wpa_s;
  283. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  284. if (conf == NULL) {
  285. wpa_supplicant_ap_deinit(wpa_s);
  286. return -1;
  287. }
  288. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  289. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  290. wpa_supplicant_ap_deinit(wpa_s);
  291. return -1;
  292. }
  293. #ifdef CONFIG_P2P
  294. if (ssid->mode == WPAS_MODE_P2P_GO)
  295. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  296. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  297. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  298. P2P_GROUP_FORMATION;
  299. #endif /* CONFIG_P2P */
  300. hapd_iface->num_bss = conf->num_bss;
  301. hapd_iface->bss = os_zalloc(conf->num_bss *
  302. sizeof(struct hostapd_data *));
  303. if (hapd_iface->bss == NULL) {
  304. wpa_supplicant_ap_deinit(wpa_s);
  305. return -1;
  306. }
  307. for (i = 0; i < conf->num_bss; i++) {
  308. hapd_iface->bss[i] =
  309. hostapd_alloc_bss_data(hapd_iface, conf,
  310. &conf->bss[i]);
  311. if (hapd_iface->bss[i] == NULL) {
  312. wpa_supplicant_ap_deinit(wpa_s);
  313. return -1;
  314. }
  315. hapd_iface->bss[i]->msg_ctx = wpa_s;
  316. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  317. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  318. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  319. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  320. hostapd_register_probereq_cb(hapd_iface->bss[i],
  321. ap_probe_req_rx, wpa_s);
  322. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  323. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  324. #ifdef CONFIG_P2P
  325. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  326. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
  327. wpa_s, ssid->p2p_persistent_group,
  328. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
  329. #endif /* CONFIG_P2P */
  330. }
  331. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  332. hapd_iface->bss[0]->driver = wpa_s->driver;
  333. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  334. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  335. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  336. wpa_supplicant_ap_deinit(wpa_s);
  337. return -1;
  338. }
  339. wpa_s->current_ssid = ssid;
  340. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  341. wpa_s->assoc_freq = ssid->frequency;
  342. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  343. if (wpa_s->ap_configured_cb)
  344. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  345. wpa_s->ap_configured_cb_data);
  346. return 0;
  347. }
  348. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  349. {
  350. if (wpa_s->ap_iface == NULL)
  351. return;
  352. wpa_s->current_ssid = NULL;
  353. #ifdef CONFIG_P2P
  354. if (wpa_s->ap_iface->bss)
  355. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  356. wpas_p2p_group_deinit(wpa_s);
  357. #endif /* CONFIG_P2P */
  358. hostapd_interface_deinit(wpa_s->ap_iface);
  359. hostapd_interface_free(wpa_s->ap_iface);
  360. wpa_s->ap_iface = NULL;
  361. wpa_drv_deinit_ap(wpa_s);
  362. }
  363. void ap_tx_status(void *ctx, const u8 *addr,
  364. const u8 *buf, size_t len, int ack)
  365. {
  366. #ifdef NEED_AP_MLME
  367. struct wpa_supplicant *wpa_s = ctx;
  368. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  369. #endif /* NEED_AP_MLME */
  370. }
  371. void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
  372. {
  373. #ifdef NEED_AP_MLME
  374. struct wpa_supplicant *wpa_s = ctx;
  375. const struct ieee80211_hdr *hdr =
  376. (const struct ieee80211_hdr *) frame;
  377. u16 fc = le_to_host16(hdr->frame_control);
  378. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
  379. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  380. (WLAN_FC_TODS | WLAN_FC_FROMDS));
  381. #endif /* NEED_AP_MLME */
  382. }
  383. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  384. {
  385. #ifdef NEED_AP_MLME
  386. struct wpa_supplicant *wpa_s = ctx;
  387. struct hostapd_frame_info fi;
  388. os_memset(&fi, 0, sizeof(fi));
  389. fi.datarate = rx_mgmt->datarate;
  390. fi.ssi_signal = rx_mgmt->ssi_signal;
  391. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  392. rx_mgmt->frame_len, &fi);
  393. #endif /* NEED_AP_MLME */
  394. }
  395. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  396. {
  397. #ifdef NEED_AP_MLME
  398. struct wpa_supplicant *wpa_s = ctx;
  399. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  400. #endif /* NEED_AP_MLME */
  401. }
  402. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  403. const u8 *src_addr, const u8 *buf, size_t len)
  404. {
  405. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  406. }
  407. #ifdef CONFIG_WPS
  408. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  409. {
  410. if (!wpa_s->ap_iface)
  411. return -1;
  412. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
  413. }
  414. static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
  415. struct sta_info *sta, void *ctx)
  416. {
  417. if (sta && (sta->flags & WLAN_STA_WPS)) {
  418. ap_sta_deauthenticate(hapd, sta,
  419. WLAN_REASON_PREV_AUTH_NOT_VALID);
  420. wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
  421. __func__, MAC2STR(sta->addr));
  422. return 1;
  423. }
  424. return 0;
  425. }
  426. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  427. {
  428. struct wps_registrar *reg;
  429. int reg_sel = 0, wps_sta = 0;
  430. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  431. return -1;
  432. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  433. reg_sel = wps_registrar_wps_cancel(reg);
  434. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  435. wpa_supplicant_ap_wps_sta_cancel, NULL);
  436. if (!reg_sel && !wps_sta) {
  437. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  438. "time");
  439. return -1;
  440. }
  441. /*
  442. * There are 2 cases to return wps cancel as success:
  443. * 1. When wps cancel was initiated but no connection has been
  444. * established with client yet.
  445. * 2. Client is in the middle of exchanging WPS messages.
  446. */
  447. return 0;
  448. }
  449. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  450. const char *pin, char *buf, size_t buflen)
  451. {
  452. int ret, ret_len = 0;
  453. if (!wpa_s->ap_iface)
  454. return -1;
  455. if (pin == NULL) {
  456. unsigned int rpin = wps_generate_pin();
  457. ret_len = os_snprintf(buf, buflen, "%d", rpin);
  458. pin = buf;
  459. } else
  460. ret_len = os_snprintf(buf, buflen, "%s", pin);
  461. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  462. 0);
  463. if (ret)
  464. return -1;
  465. return ret_len;
  466. }
  467. #endif /* CONFIG_WPS */
  468. #ifdef CONFIG_CTRL_IFACE
  469. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  470. char *buf, size_t buflen)
  471. {
  472. if (wpa_s->ap_iface == NULL)
  473. return -1;
  474. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  475. buf, buflen);
  476. }
  477. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  478. char *buf, size_t buflen)
  479. {
  480. if (wpa_s->ap_iface == NULL)
  481. return -1;
  482. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  483. buf, buflen);
  484. }
  485. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  486. char *buf, size_t buflen)
  487. {
  488. if (wpa_s->ap_iface == NULL)
  489. return -1;
  490. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  491. buf, buflen);
  492. }
  493. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  494. size_t buflen, int verbose)
  495. {
  496. char *pos = buf, *end = buf + buflen;
  497. int ret;
  498. struct hostapd_bss_config *conf;
  499. if (wpa_s->ap_iface == NULL)
  500. return -1;
  501. conf = wpa_s->ap_iface->bss[0]->conf;
  502. if (conf->wpa == 0)
  503. return 0;
  504. ret = os_snprintf(pos, end - pos,
  505. "pairwise_cipher=%s\n"
  506. "group_cipher=%s\n"
  507. "key_mgmt=%s\n",
  508. wpa_cipher_txt(conf->rsn_pairwise),
  509. wpa_cipher_txt(conf->wpa_group),
  510. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  511. conf->wpa));
  512. if (ret < 0 || ret >= end - pos)
  513. return pos - buf;
  514. pos += ret;
  515. return pos - buf;
  516. }
  517. #endif /* CONFIG_CTRL_IFACE */
  518. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  519. {
  520. struct hostapd_iface *iface = wpa_s->ap_iface;
  521. struct wpa_ssid *ssid = wpa_s->current_ssid;
  522. struct hostapd_data *hapd;
  523. if (ssid == NULL || wpa_s->ap_iface == NULL)
  524. return -1;
  525. #ifdef CONFIG_P2P
  526. if (ssid->mode == WPAS_MODE_P2P_GO)
  527. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  528. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  529. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  530. P2P_GROUP_FORMATION;
  531. #endif /* CONFIG_P2P */
  532. ieee802_11_set_beacons(iface);
  533. hapd = iface->bss[0];
  534. hapd->drv.set_ap_wps_ie(hapd);
  535. return 0;
  536. }
  537. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  538. const u8 *addr)
  539. {
  540. struct hostapd_data *hapd;
  541. struct hostapd_bss_config *conf;
  542. if (!wpa_s->ap_iface)
  543. return -1;
  544. if (addr)
  545. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  546. MAC2STR(addr));
  547. else
  548. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  549. hapd = wpa_s->ap_iface->bss[0];
  550. conf = hapd->conf;
  551. os_free(conf->accept_mac);
  552. conf->accept_mac = NULL;
  553. conf->num_accept_mac = 0;
  554. os_free(conf->deny_mac);
  555. conf->deny_mac = NULL;
  556. conf->num_deny_mac = 0;
  557. if (addr == NULL) {
  558. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  559. return 0;
  560. }
  561. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  562. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  563. if (conf->accept_mac == NULL)
  564. return -1;
  565. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  566. conf->num_accept_mac = 1;
  567. return 0;
  568. }