Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ap.c 22KB

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