ap.c 26 KB

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