ap.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  411. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  412. else if (ssid->pairwise_cipher & WPA_CIPHER_GCMP)
  413. wpa_s->pairwise_cipher = WPA_CIPHER_GCMP;
  414. else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
  415. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  416. else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
  417. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  418. else {
  419. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  420. "cipher.");
  421. return -1;
  422. }
  423. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  424. params.group_suite = params.pairwise_suite;
  425. #ifdef CONFIG_P2P
  426. if (ssid->mode == WPAS_MODE_P2P_GO ||
  427. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  428. params.p2p = 1;
  429. #endif /* CONFIG_P2P */
  430. if (wpa_s->parent->set_ap_uapsd)
  431. params.uapsd = wpa_s->parent->ap_uapsd;
  432. else
  433. params.uapsd = -1;
  434. if (wpa_drv_associate(wpa_s, &params) < 0) {
  435. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  436. return -1;
  437. }
  438. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  439. if (hapd_iface == NULL)
  440. return -1;
  441. hapd_iface->owner = wpa_s;
  442. hapd_iface->drv_flags = wpa_s->drv_flags;
  443. hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
  444. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  445. if (conf == NULL) {
  446. wpa_supplicant_ap_deinit(wpa_s);
  447. return -1;
  448. }
  449. os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
  450. wpa_s->conf->wmm_ac_params,
  451. sizeof(wpa_s->conf->wmm_ac_params));
  452. if (params.uapsd > 0) {
  453. conf->bss->wmm_enabled = 1;
  454. conf->bss->wmm_uapsd = 1;
  455. }
  456. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  457. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  458. wpa_supplicant_ap_deinit(wpa_s);
  459. return -1;
  460. }
  461. #ifdef CONFIG_P2P
  462. if (ssid->mode == WPAS_MODE_P2P_GO)
  463. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  464. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  465. conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  466. P2P_GROUP_FORMATION;
  467. #endif /* CONFIG_P2P */
  468. hapd_iface->num_bss = conf->num_bss;
  469. hapd_iface->bss = os_calloc(conf->num_bss,
  470. sizeof(struct hostapd_data *));
  471. if (hapd_iface->bss == NULL) {
  472. wpa_supplicant_ap_deinit(wpa_s);
  473. return -1;
  474. }
  475. for (i = 0; i < conf->num_bss; i++) {
  476. hapd_iface->bss[i] =
  477. hostapd_alloc_bss_data(hapd_iface, conf,
  478. &conf->bss[i]);
  479. if (hapd_iface->bss[i] == NULL) {
  480. wpa_supplicant_ap_deinit(wpa_s);
  481. return -1;
  482. }
  483. hapd_iface->bss[i]->msg_ctx = wpa_s;
  484. hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
  485. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  486. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  487. hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
  488. hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
  489. hostapd_register_probereq_cb(hapd_iface->bss[i],
  490. ap_probe_req_rx, wpa_s);
  491. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  492. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  493. hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
  494. hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
  495. hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
  496. hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
  497. #ifdef CONFIG_P2P
  498. hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
  499. hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
  500. ssid);
  501. #endif /* CONFIG_P2P */
  502. hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
  503. hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
  504. }
  505. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  506. hapd_iface->bss[0]->driver = wpa_s->driver;
  507. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  508. wpa_s->current_ssid = ssid;
  509. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  510. wpa_s->assoc_freq = ssid->frequency;
  511. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  512. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  513. wpa_supplicant_ap_deinit(wpa_s);
  514. return -1;
  515. }
  516. return 0;
  517. }
  518. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  519. {
  520. #ifdef CONFIG_WPS
  521. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  522. #endif /* CONFIG_WPS */
  523. if (wpa_s->ap_iface == NULL)
  524. return;
  525. wpa_s->current_ssid = NULL;
  526. wpa_s->assoc_freq = 0;
  527. #ifdef CONFIG_P2P
  528. if (wpa_s->ap_iface->bss)
  529. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  530. wpas_p2p_group_deinit(wpa_s);
  531. #endif /* CONFIG_P2P */
  532. hostapd_interface_deinit(wpa_s->ap_iface);
  533. hostapd_interface_free(wpa_s->ap_iface);
  534. wpa_s->ap_iface = NULL;
  535. wpa_drv_deinit_ap(wpa_s);
  536. }
  537. void ap_tx_status(void *ctx, const u8 *addr,
  538. const u8 *buf, size_t len, int ack)
  539. {
  540. #ifdef NEED_AP_MLME
  541. struct wpa_supplicant *wpa_s = ctx;
  542. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  543. #endif /* NEED_AP_MLME */
  544. }
  545. void ap_eapol_tx_status(void *ctx, const u8 *dst,
  546. const u8 *data, size_t len, int ack)
  547. {
  548. #ifdef NEED_AP_MLME
  549. struct wpa_supplicant *wpa_s = ctx;
  550. hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
  551. #endif /* NEED_AP_MLME */
  552. }
  553. void ap_client_poll_ok(void *ctx, const u8 *addr)
  554. {
  555. #ifdef NEED_AP_MLME
  556. struct wpa_supplicant *wpa_s = ctx;
  557. if (wpa_s->ap_iface)
  558. hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
  559. #endif /* NEED_AP_MLME */
  560. }
  561. void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
  562. {
  563. #ifdef NEED_AP_MLME
  564. struct wpa_supplicant *wpa_s = ctx;
  565. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
  566. #endif /* NEED_AP_MLME */
  567. }
  568. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  569. {
  570. #ifdef NEED_AP_MLME
  571. struct wpa_supplicant *wpa_s = ctx;
  572. struct hostapd_frame_info fi;
  573. os_memset(&fi, 0, sizeof(fi));
  574. fi.datarate = rx_mgmt->datarate;
  575. fi.ssi_signal = rx_mgmt->ssi_signal;
  576. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  577. rx_mgmt->frame_len, &fi);
  578. #endif /* NEED_AP_MLME */
  579. }
  580. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  581. {
  582. #ifdef NEED_AP_MLME
  583. struct wpa_supplicant *wpa_s = ctx;
  584. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  585. #endif /* NEED_AP_MLME */
  586. }
  587. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  588. const u8 *src_addr, const u8 *buf, size_t len)
  589. {
  590. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  591. }
  592. #ifdef CONFIG_WPS
  593. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  594. const u8 *p2p_dev_addr)
  595. {
  596. if (!wpa_s->ap_iface)
  597. return -1;
  598. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
  599. p2p_dev_addr);
  600. }
  601. int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
  602. {
  603. struct wps_registrar *reg;
  604. int reg_sel = 0, wps_sta = 0;
  605. if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
  606. return -1;
  607. reg = wpa_s->ap_iface->bss[0]->wps->registrar;
  608. reg_sel = wps_registrar_wps_cancel(reg);
  609. wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
  610. ap_sta_wps_cancel, NULL);
  611. if (!reg_sel && !wps_sta) {
  612. wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
  613. "time");
  614. return -1;
  615. }
  616. /*
  617. * There are 2 cases to return wps cancel as success:
  618. * 1. When wps cancel was initiated but no connection has been
  619. * established with client yet.
  620. * 2. Client is in the middle of exchanging WPS messages.
  621. */
  622. return 0;
  623. }
  624. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  625. const char *pin, char *buf, size_t buflen,
  626. int timeout)
  627. {
  628. int ret, ret_len = 0;
  629. if (!wpa_s->ap_iface)
  630. return -1;
  631. if (pin == NULL) {
  632. unsigned int rpin = wps_generate_pin();
  633. ret_len = os_snprintf(buf, buflen, "%08d", rpin);
  634. pin = buf;
  635. } else
  636. ret_len = os_snprintf(buf, buflen, "%s", pin);
  637. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  638. timeout);
  639. if (ret)
  640. return -1;
  641. return ret_len;
  642. }
  643. static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
  644. {
  645. struct wpa_supplicant *wpa_s = eloop_data;
  646. wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
  647. wpas_wps_ap_pin_disable(wpa_s);
  648. }
  649. static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
  650. {
  651. struct hostapd_data *hapd;
  652. if (wpa_s->ap_iface == NULL)
  653. return;
  654. hapd = wpa_s->ap_iface->bss[0];
  655. wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
  656. hapd->ap_pin_failures = 0;
  657. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  658. if (timeout > 0)
  659. eloop_register_timeout(timeout, 0,
  660. wpas_wps_ap_pin_timeout, wpa_s, NULL);
  661. }
  662. void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
  663. {
  664. struct hostapd_data *hapd;
  665. if (wpa_s->ap_iface == NULL)
  666. return;
  667. wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
  668. hapd = wpa_s->ap_iface->bss[0];
  669. os_free(hapd->conf->ap_pin);
  670. hapd->conf->ap_pin = NULL;
  671. eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
  672. }
  673. const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
  674. {
  675. struct hostapd_data *hapd;
  676. unsigned int pin;
  677. char pin_txt[9];
  678. if (wpa_s->ap_iface == NULL)
  679. return NULL;
  680. hapd = wpa_s->ap_iface->bss[0];
  681. pin = wps_generate_pin();
  682. os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
  683. os_free(hapd->conf->ap_pin);
  684. hapd->conf->ap_pin = os_strdup(pin_txt);
  685. if (hapd->conf->ap_pin == NULL)
  686. return NULL;
  687. wpas_wps_ap_pin_enable(wpa_s, timeout);
  688. return hapd->conf->ap_pin;
  689. }
  690. const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
  691. {
  692. struct hostapd_data *hapd;
  693. if (wpa_s->ap_iface == NULL)
  694. return NULL;
  695. hapd = wpa_s->ap_iface->bss[0];
  696. return hapd->conf->ap_pin;
  697. }
  698. int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
  699. int timeout)
  700. {
  701. struct hostapd_data *hapd;
  702. char pin_txt[9];
  703. int ret;
  704. if (wpa_s->ap_iface == NULL)
  705. return -1;
  706. hapd = wpa_s->ap_iface->bss[0];
  707. ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
  708. if (ret < 0 || ret >= (int) sizeof(pin_txt))
  709. return -1;
  710. os_free(hapd->conf->ap_pin);
  711. hapd->conf->ap_pin = os_strdup(pin_txt);
  712. if (hapd->conf->ap_pin == NULL)
  713. return -1;
  714. wpas_wps_ap_pin_enable(wpa_s, timeout);
  715. return 0;
  716. }
  717. void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
  718. {
  719. struct hostapd_data *hapd;
  720. if (wpa_s->ap_iface == NULL)
  721. return;
  722. hapd = wpa_s->ap_iface->bss[0];
  723. /*
  724. * Registrar failed to prove its knowledge of the AP PIN. Disable AP
  725. * PIN if this happens multiple times to slow down brute force attacks.
  726. */
  727. hapd->ap_pin_failures++;
  728. wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
  729. hapd->ap_pin_failures);
  730. if (hapd->ap_pin_failures < 3)
  731. return;
  732. wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
  733. hapd->ap_pin_failures = 0;
  734. os_free(hapd->conf->ap_pin);
  735. hapd->conf->ap_pin = NULL;
  736. }
  737. #endif /* CONFIG_WPS */
  738. #ifdef CONFIG_CTRL_IFACE
  739. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  740. char *buf, size_t buflen)
  741. {
  742. if (wpa_s->ap_iface == NULL)
  743. return -1;
  744. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  745. buf, buflen);
  746. }
  747. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  748. char *buf, size_t buflen)
  749. {
  750. if (wpa_s->ap_iface == NULL)
  751. return -1;
  752. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  753. buf, buflen);
  754. }
  755. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  756. char *buf, size_t buflen)
  757. {
  758. if (wpa_s->ap_iface == NULL)
  759. return -1;
  760. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  761. buf, buflen);
  762. }
  763. int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
  764. const char *txtaddr)
  765. {
  766. if (wpa_s->ap_iface == NULL)
  767. return -1;
  768. return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
  769. txtaddr);
  770. }
  771. int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
  772. const char *txtaddr)
  773. {
  774. if (wpa_s->ap_iface == NULL)
  775. return -1;
  776. return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
  777. txtaddr);
  778. }
  779. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  780. size_t buflen, int verbose)
  781. {
  782. char *pos = buf, *end = buf + buflen;
  783. int ret;
  784. struct hostapd_bss_config *conf;
  785. if (wpa_s->ap_iface == NULL)
  786. return -1;
  787. conf = wpa_s->ap_iface->bss[0]->conf;
  788. if (conf->wpa == 0)
  789. return 0;
  790. ret = os_snprintf(pos, end - pos,
  791. "pairwise_cipher=%s\n"
  792. "group_cipher=%s\n"
  793. "key_mgmt=%s\n",
  794. wpa_cipher_txt(conf->rsn_pairwise),
  795. wpa_cipher_txt(conf->wpa_group),
  796. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  797. conf->wpa));
  798. if (ret < 0 || ret >= end - pos)
  799. return pos - buf;
  800. pos += ret;
  801. return pos - buf;
  802. }
  803. #endif /* CONFIG_CTRL_IFACE */
  804. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  805. {
  806. struct hostapd_iface *iface = wpa_s->ap_iface;
  807. struct wpa_ssid *ssid = wpa_s->current_ssid;
  808. struct hostapd_data *hapd;
  809. if (ssid == NULL || wpa_s->ap_iface == NULL ||
  810. ssid->mode == WPAS_MODE_INFRA ||
  811. ssid->mode == WPAS_MODE_IBSS)
  812. return -1;
  813. #ifdef CONFIG_P2P
  814. if (ssid->mode == WPAS_MODE_P2P_GO)
  815. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
  816. else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  817. iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
  818. P2P_GROUP_FORMATION;
  819. #endif /* CONFIG_P2P */
  820. hapd = iface->bss[0];
  821. if (hapd->drv_priv == NULL)
  822. return -1;
  823. ieee802_11_set_beacons(iface);
  824. hostapd_set_ap_wps_ie(hapd);
  825. return 0;
  826. }
  827. void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
  828. int offset)
  829. {
  830. if (!wpa_s->ap_iface)
  831. return;
  832. wpa_s->assoc_freq = freq;
  833. hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset);
  834. }
  835. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  836. const u8 *addr)
  837. {
  838. struct hostapd_data *hapd;
  839. struct hostapd_bss_config *conf;
  840. if (!wpa_s->ap_iface)
  841. return -1;
  842. if (addr)
  843. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  844. MAC2STR(addr));
  845. else
  846. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  847. hapd = wpa_s->ap_iface->bss[0];
  848. conf = hapd->conf;
  849. os_free(conf->accept_mac);
  850. conf->accept_mac = NULL;
  851. conf->num_accept_mac = 0;
  852. os_free(conf->deny_mac);
  853. conf->deny_mac = NULL;
  854. conf->num_deny_mac = 0;
  855. if (addr == NULL) {
  856. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  857. return 0;
  858. }
  859. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  860. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  861. if (conf->accept_mac == NULL)
  862. return -1;
  863. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  864. conf->num_accept_mac = 1;
  865. return 0;
  866. }