ap.c 21 KB

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