ap.c 22 KB

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