ap.c 22 KB

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