ap.c 26 KB

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