ap.c 26 KB

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