ap.c 26 KB

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