ap.c 27 KB

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