ap.c 21 KB

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