ap.c 27 KB

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