ap.c 28 KB

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