ap.c 22 KB

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