ap.c 26 KB

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