ap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. #ifdef CONFIG_P2P
  386. if (wpa_s->ap_iface->bss)
  387. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  388. wpas_p2p_group_deinit(wpa_s);
  389. #endif /* CONFIG_P2P */
  390. hostapd_interface_deinit(wpa_s->ap_iface);
  391. hostapd_interface_free(wpa_s->ap_iface);
  392. wpa_s->ap_iface = NULL;
  393. wpa_drv_deinit_ap(wpa_s);
  394. }
  395. void ap_tx_status(void *ctx, const u8 *addr,
  396. const u8 *buf, size_t len, int ack)
  397. {
  398. #ifdef NEED_AP_MLME
  399. struct wpa_supplicant *wpa_s = ctx;
  400. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  401. #endif /* NEED_AP_MLME */
  402. }
  403. void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
  404. {
  405. #ifdef NEED_AP_MLME
  406. struct wpa_supplicant *wpa_s = ctx;
  407. const struct ieee80211_hdr *hdr =
  408. (const struct ieee80211_hdr *) frame;
  409. u16 fc = le_to_host16(hdr->frame_control);
  410. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
  411. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  412. (WLAN_FC_TODS | WLAN_FC_FROMDS));
  413. #endif /* NEED_AP_MLME */
  414. }
  415. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  416. {
  417. #ifdef NEED_AP_MLME
  418. struct wpa_supplicant *wpa_s = ctx;
  419. struct hostapd_frame_info fi;
  420. os_memset(&fi, 0, sizeof(fi));
  421. fi.datarate = rx_mgmt->datarate;
  422. fi.ssi_signal = rx_mgmt->ssi_signal;
  423. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  424. rx_mgmt->frame_len, &fi);
  425. #endif /* NEED_AP_MLME */
  426. }
  427. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  428. {
  429. #ifdef NEED_AP_MLME
  430. struct wpa_supplicant *wpa_s = ctx;
  431. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  432. #endif /* NEED_AP_MLME */
  433. }
  434. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  435. const u8 *src_addr, const u8 *buf, size_t len)
  436. {
  437. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  438. }
  439. #ifdef CONFIG_WPS
  440. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  441. {
  442. if (!wpa_s->ap_iface)
  443. return -1;
  444. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
  445. }
  446. static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
  447. struct sta_info *sta, void *ctx)
  448. {
  449. if (sta && (sta->flags & WLAN_STA_WPS)) {
  450. ap_sta_deauthenticate(hapd, sta,
  451. WLAN_REASON_PREV_AUTH_NOT_VALID);
  452. wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
  453. __func__, MAC2STR(sta->addr));
  454. return 1;
  455. }
  456. return 0;
  457. }
  458. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  459. {
  460. struct wps_registrar *reg;
  461. int reg_sel = 0, wps_sta = 0;
  462. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  463. return -1;
  464. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  465. reg_sel = wps_registrar_wps_cancel(reg);
  466. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  467. wpa_supplicant_ap_wps_sta_cancel, NULL);
  468. if (!reg_sel && !wps_sta) {
  469. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  470. "time");
  471. return -1;
  472. }
  473. /*
  474. * There are 2 cases to return wps cancel as success:
  475. * 1. When wps cancel was initiated but no connection has been
  476. * established with client yet.
  477. * 2. Client is in the middle of exchanging WPS messages.
  478. */
  479. return 0;
  480. }
  481. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  482. const char *pin, char *buf, size_t buflen)
  483. {
  484. int ret, ret_len = 0;
  485. if (!wpa_s->ap_iface)
  486. return -1;
  487. if (pin == NULL) {
  488. unsigned int rpin = wps_generate_pin();
  489. ret_len = os_snprintf(buf, buflen, "%d", rpin);
  490. pin = buf;
  491. } else
  492. ret_len = os_snprintf(buf, buflen, "%s", pin);
  493. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  494. 0);
  495. if (ret)
  496. return -1;
  497. return ret_len;
  498. }
  499. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  500. {
  501. struct wpa_supplicant *wpa_s = eloop_data;
  502. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  503. wpas_wps_ap_pin_disable(wpa_s);
  504. }
  505. static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
  506. {
  507. struct hostapd_data *hapd;
  508. if (wpa_s->ap_iface == NULL)
  509. return;
  510. hapd = wpa_s->ap_iface->bss[0];
  511. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  512. hapd->ap_pin_failures = 0;
  513. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  514. if (timeout > 0)
  515. eloop_register_timeout(timeout, 0,
  516. wpas_wps_ap_pin_timeout, wpa_s, NULL);
  517. }
  518. void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
  519. {
  520. struct hostapd_data *hapd;
  521. if (wpa_s->ap_iface == NULL)
  522. return;
  523. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  524. hapd = wpa_s->ap_iface->bss[0];
  525. os_free(hapd->conf->ap_pin);
  526. hapd->conf->ap_pin = NULL;
  527. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  528. }
  529. const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
  530. {
  531. struct hostapd_data *hapd;
  532. unsigned int pin;
  533. char pin_txt[9];
  534. if (wpa_s->ap_iface == NULL)
  535. return NULL;
  536. hapd = wpa_s->ap_iface->bss[0];
  537. pin = wps_generate_pin();
  538. os_snprintf(pin_txt, sizeof(pin_txt), "%u", pin);
  539. os_free(hapd->conf->ap_pin);
  540. hapd->conf->ap_pin = os_strdup(pin_txt);
  541. if (hapd->conf->ap_pin == NULL)
  542. return NULL;
  543. wpas_wps_ap_pin_enable(wpa_s, timeout);
  544. return hapd->conf->ap_pin;
  545. }
  546. const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
  547. {
  548. struct hostapd_data *hapd;
  549. if (wpa_s->ap_iface == NULL)
  550. return NULL;
  551. hapd = wpa_s->ap_iface->bss[0];
  552. return hapd->conf->ap_pin;
  553. }
  554. int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
  555. int timeout)
  556. {
  557. struct hostapd_data *hapd;
  558. char pin_txt[9];
  559. int ret;
  560. if (wpa_s->ap_iface == NULL)
  561. return -1;
  562. hapd = wpa_s->ap_iface->bss[0];
  563. ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
  564. if (ret < 0 || ret >= (int) sizeof(pin_txt))
  565. return -1;
  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 -1;
  570. wpas_wps_ap_pin_enable(wpa_s, timeout);
  571. return 0;
  572. }
  573. void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
  574. {
  575. struct hostapd_data *hapd;
  576. if (wpa_s->ap_iface == NULL)
  577. return;
  578. hapd = wpa_s->ap_iface->bss[0];
  579. /*
  580. * Registrar failed to prove its knowledge of the AP PIN. Disable AP
  581. * PIN if this happens multiple times to slow down brute force attacks.
  582. */
  583. hapd->ap_pin_failures++;
  584. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  585. hapd->ap_pin_failures);
  586. if (hapd->ap_pin_failures < 3)
  587. return;
  588. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
  589. hapd->ap_pin_failures = 0;
  590. os_free(hapd->conf->ap_pin);
  591. hapd->conf->ap_pin = NULL;
  592. }
  593. #endif /* CONFIG_WPS */
  594. #ifdef CONFIG_CTRL_IFACE
  595. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  596. char *buf, size_t buflen)
  597. {
  598. if (wpa_s->ap_iface == NULL)
  599. return -1;
  600. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  601. buf, buflen);
  602. }
  603. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  604. char *buf, size_t buflen)
  605. {
  606. if (wpa_s->ap_iface == NULL)
  607. return -1;
  608. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  609. buf, buflen);
  610. }
  611. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  612. char *buf, size_t buflen)
  613. {
  614. if (wpa_s->ap_iface == NULL)
  615. return -1;
  616. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  617. buf, buflen);
  618. }
  619. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  620. size_t buflen, int verbose)
  621. {
  622. char *pos = buf, *end = buf + buflen;
  623. int ret;
  624. struct hostapd_bss_config *conf;
  625. if (wpa_s->ap_iface == NULL)
  626. return -1;
  627. conf = wpa_s->ap_iface->bss[0]->conf;
  628. if (conf->wpa == 0)
  629. return 0;
  630. ret = os_snprintf(pos, end - pos,
  631. "pairwise_cipher=%s\n"
  632. "group_cipher=%s\n"
  633. "key_mgmt=%s\n",
  634. wpa_cipher_txt(conf->rsn_pairwise),
  635. wpa_cipher_txt(conf->wpa_group),
  636. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  637. conf->wpa));
  638. if (ret < 0 || ret >= end - pos)
  639. return pos - buf;
  640. pos += ret;
  641. return pos - buf;
  642. }
  643. #endif /* CONFIG_CTRL_IFACE */
  644. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  645. {
  646. struct hostapd_iface *iface = wpa_s->ap_iface;
  647. struct wpa_ssid *ssid = wpa_s->current_ssid;
  648. struct hostapd_data *hapd;
  649. if (ssid == NULL || wpa_s->ap_iface == NULL)
  650. return -1;
  651. #ifdef CONFIG_P2P
  652. if (ssid->mode == WPAS_MODE_P2P_GO)
  653. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  654. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  655. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  656. P2P_GROUP_FORMATION;
  657. #endif /* CONFIG_P2P */
  658. ieee802_11_set_beacons(iface);
  659. hapd = iface->bss[0];
  660. hostapd_set_ap_wps_ie(hapd);
  661. return 0;
  662. }
  663. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  664. const u8 *addr)
  665. {
  666. struct hostapd_data *hapd;
  667. struct hostapd_bss_config *conf;
  668. if (!wpa_s->ap_iface)
  669. return -1;
  670. if (addr)
  671. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  672. MAC2STR(addr));
  673. else
  674. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  675. hapd = wpa_s->ap_iface->bss[0];
  676. conf = hapd->conf;
  677. os_free(conf->accept_mac);
  678. conf->accept_mac = NULL;
  679. conf->num_accept_mac = 0;
  680. os_free(conf->deny_mac);
  681. conf->deny_mac = NULL;
  682. conf->num_deny_mac = 0;
  683. if (addr == NULL) {
  684. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  685. return 0;
  686. }
  687. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  688. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  689. if (conf->accept_mac == NULL)
  690. return -1;
  691. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  692. conf->num_accept_mac = 1;
  693. return 0;
  694. }