ap.c 27 KB

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