ap.c 25 KB

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