ap.c 25 KB

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