ap.c 26 KB

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