ap.c 22 KB

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