ap.c 28 KB

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