ap.c 22 KB

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