ap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 "common/ieee802_11_defs.h"
  18. #include "ap/hostapd.h"
  19. #include "ap/ap_config.h"
  20. #ifdef NEED_AP_MLME
  21. #include "ap/ieee802_11.h"
  22. #endif /* NEED_AP_MLME */
  23. #include "ap/beacon.h"
  24. #include "ap/ieee802_1x.h"
  25. #include "ap/wps_hostapd.h"
  26. #include "ap/ctrl_iface_ap.h"
  27. #include "eap_common/eap_defs.h"
  28. #include "eap_server/eap_methods.h"
  29. #include "eap_common/eap_wsc_common.h"
  30. #include "wps/wps.h"
  31. #include "config_ssid.h"
  32. #include "config.h"
  33. #include "wpa_supplicant_i.h"
  34. #include "driver_i.h"
  35. #include "ap.h"
  36. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  37. struct wpa_ssid *ssid,
  38. struct hostapd_config *conf)
  39. {
  40. struct hostapd_bss_config *bss = &conf->bss[0];
  41. int pairwise;
  42. conf->driver = wpa_s->driver;
  43. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  44. if (ssid->frequency == 0) {
  45. /* default channel 11 */
  46. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  47. conf->channel = 11;
  48. } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
  49. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  50. conf->channel = (ssid->frequency - 2407) / 5;
  51. } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
  52. (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
  53. conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
  54. conf->channel = (ssid->frequency - 5000) / 5;
  55. } else {
  56. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  57. ssid->frequency);
  58. return -1;
  59. }
  60. /* TODO: enable HT if driver supports it;
  61. * drop to 11b if driver does not support 11g */
  62. #ifdef CONFIG_P2P
  63. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
  64. /* Remove 802.11b rates from supported and basic rate sets */
  65. int *list = os_malloc(4 * sizeof(int));
  66. if (list) {
  67. list[0] = 60;
  68. list[1] = 120;
  69. list[2] = 240;
  70. list[3] = -1;
  71. }
  72. conf->basic_rates = list;
  73. list = os_malloc(9 * sizeof(int));
  74. if (list) {
  75. list[0] = 60;
  76. list[1] = 90;
  77. list[2] = 120;
  78. list[3] = 180;
  79. list[4] = 240;
  80. list[5] = 360;
  81. list[6] = 480;
  82. list[7] = 540;
  83. list[8] = -1;
  84. }
  85. conf->supported_rates = list;
  86. }
  87. #endif /* CONFIG_P2P */
  88. if (ssid->ssid_len == 0) {
  89. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  90. return -1;
  91. }
  92. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  93. bss->ssid.ssid[ssid->ssid_len] = '\0';
  94. bss->ssid.ssid_len = ssid->ssid_len;
  95. bss->ssid.ssid_set = 1;
  96. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  97. bss->wpa = ssid->proto;
  98. bss->wpa_key_mgmt = ssid->key_mgmt;
  99. bss->wpa_pairwise = ssid->pairwise_cipher;
  100. if (ssid->passphrase) {
  101. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  102. } else if (ssid->psk_set) {
  103. os_free(bss->ssid.wpa_psk);
  104. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  105. if (bss->ssid.wpa_psk == NULL)
  106. return -1;
  107. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  108. bss->ssid.wpa_psk->group = 1;
  109. }
  110. /* Select group cipher based on the enabled pairwise cipher suites */
  111. pairwise = 0;
  112. if (bss->wpa & 1)
  113. pairwise |= bss->wpa_pairwise;
  114. if (bss->wpa & 2) {
  115. if (bss->rsn_pairwise == 0)
  116. bss->rsn_pairwise = bss->wpa_pairwise;
  117. pairwise |= bss->rsn_pairwise;
  118. }
  119. if (pairwise & WPA_CIPHER_TKIP)
  120. bss->wpa_group = WPA_CIPHER_TKIP;
  121. else
  122. bss->wpa_group = WPA_CIPHER_CCMP;
  123. if (bss->wpa && bss->ieee802_1x)
  124. bss->ssid.security_policy = SECURITY_WPA;
  125. else if (bss->wpa)
  126. bss->ssid.security_policy = SECURITY_WPA_PSK;
  127. else if (bss->ieee802_1x) {
  128. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  129. bss->ssid.wep.default_len = bss->default_wep_key_len;
  130. } else if (bss->ssid.wep.keys_set)
  131. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  132. else
  133. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  134. #ifdef CONFIG_WPS
  135. /*
  136. * Enable WPS by default, but require user interaction to actually use
  137. * it. Only the internal Registrar is supported.
  138. */
  139. bss->eap_server = 1;
  140. bss->wps_state = 2;
  141. bss->ap_setup_locked = 1;
  142. if (wpa_s->conf->config_methods)
  143. bss->config_methods = os_strdup(wpa_s->conf->config_methods);
  144. if (wpa_s->conf->device_type)
  145. bss->device_type = os_strdup(wpa_s->conf->device_type);
  146. #endif /* CONFIG_WPS */
  147. return 0;
  148. }
  149. static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
  150. {
  151. }
  152. static int ap_probe_req_rx(void *ctx, const u8 *addr, const u8 *ie,
  153. size_t ie_len)
  154. {
  155. return 0;
  156. }
  157. static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  158. const u8 *uuid_e)
  159. {
  160. }
  161. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  162. struct wpa_ssid *ssid)
  163. {
  164. struct wpa_driver_associate_params params;
  165. struct hostapd_iface *hapd_iface;
  166. struct hostapd_config *conf;
  167. size_t i;
  168. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  169. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  170. return -1;
  171. }
  172. wpa_supplicant_ap_deinit(wpa_s);
  173. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  174. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  175. os_memset(&params, 0, sizeof(params));
  176. params.ssid = ssid->ssid;
  177. params.ssid_len = ssid->ssid_len;
  178. switch (ssid->mode) {
  179. case WPAS_MODE_INFRA:
  180. params.mode = IEEE80211_MODE_INFRA;
  181. break;
  182. case WPAS_MODE_IBSS:
  183. params.mode = IEEE80211_MODE_IBSS;
  184. break;
  185. case WPAS_MODE_AP:
  186. case WPAS_MODE_P2P_GO:
  187. case WPAS_MODE_P2P_GROUP_FORMATION:
  188. params.mode = IEEE80211_MODE_AP;
  189. break;
  190. }
  191. params.freq = ssid->frequency;
  192. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
  193. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  194. else
  195. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  196. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  197. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  198. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  199. else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
  200. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  201. else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
  202. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  203. else {
  204. wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
  205. "cipher.");
  206. return -1;
  207. }
  208. params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
  209. params.group_suite = params.pairwise_suite;
  210. if (wpa_drv_associate(wpa_s, &params) < 0) {
  211. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  212. return -1;
  213. }
  214. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  215. if (hapd_iface == NULL)
  216. return -1;
  217. hapd_iface->owner = wpa_s;
  218. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  219. if (conf == NULL) {
  220. wpa_supplicant_ap_deinit(wpa_s);
  221. return -1;
  222. }
  223. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  224. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  225. wpa_supplicant_ap_deinit(wpa_s);
  226. return -1;
  227. }
  228. hapd_iface->num_bss = conf->num_bss;
  229. hapd_iface->bss = os_zalloc(conf->num_bss *
  230. sizeof(struct hostapd_data *));
  231. if (hapd_iface->bss == NULL) {
  232. wpa_supplicant_ap_deinit(wpa_s);
  233. return -1;
  234. }
  235. for (i = 0; i < conf->num_bss; i++) {
  236. hapd_iface->bss[i] =
  237. hostapd_alloc_bss_data(hapd_iface, conf,
  238. &conf->bss[i]);
  239. if (hapd_iface->bss[i] == NULL) {
  240. wpa_supplicant_ap_deinit(wpa_s);
  241. return -1;
  242. }
  243. hapd_iface->bss[i]->msg_ctx = wpa_s;
  244. hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
  245. hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
  246. hostapd_register_probereq_cb(hapd_iface->bss[i],
  247. ap_probe_req_rx, wpa_s);
  248. hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
  249. hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
  250. }
  251. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  252. hapd_iface->bss[0]->driver = wpa_s->driver;
  253. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  254. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  255. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  256. wpa_supplicant_ap_deinit(wpa_s);
  257. return -1;
  258. }
  259. wpa_s->current_ssid = ssid;
  260. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  261. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  262. if (wpa_s->ap_configured_cb)
  263. wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
  264. wpa_s->ap_configured_cb_data);
  265. return 0;
  266. }
  267. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  268. {
  269. if (wpa_s->ap_iface == NULL)
  270. return;
  271. wpa_s->current_ssid = NULL;
  272. hostapd_interface_deinit(wpa_s->ap_iface);
  273. hostapd_interface_free(wpa_s->ap_iface);
  274. wpa_s->ap_iface = NULL;
  275. wpa_drv_deinit_ap(wpa_s);
  276. }
  277. void ap_tx_status(void *ctx, const u8 *addr,
  278. const u8 *buf, size_t len, int ack)
  279. {
  280. #ifdef NEED_AP_MLME
  281. struct wpa_supplicant *wpa_s = ctx;
  282. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  283. #endif /* NEED_AP_MLME */
  284. }
  285. void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
  286. {
  287. #ifdef NEED_AP_MLME
  288. struct wpa_supplicant *wpa_s = ctx;
  289. const struct ieee80211_hdr *hdr =
  290. (const struct ieee80211_hdr *) frame;
  291. u16 fc = le_to_host16(hdr->frame_control);
  292. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
  293. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  294. (WLAN_FC_TODS | WLAN_FC_FROMDS));
  295. #endif /* NEED_AP_MLME */
  296. }
  297. void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
  298. {
  299. #ifdef NEED_AP_MLME
  300. struct wpa_supplicant *wpa_s = ctx;
  301. struct hostapd_frame_info fi;
  302. os_memset(&fi, 0, sizeof(fi));
  303. fi.datarate = rx_mgmt->datarate;
  304. fi.ssi_signal = rx_mgmt->ssi_signal;
  305. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
  306. rx_mgmt->frame_len, &fi);
  307. #endif /* NEED_AP_MLME */
  308. }
  309. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  310. {
  311. #ifdef NEED_AP_MLME
  312. struct wpa_supplicant *wpa_s = ctx;
  313. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  314. #endif /* NEED_AP_MLME */
  315. }
  316. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  317. const u8 *src_addr, const u8 *buf, size_t len)
  318. {
  319. ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  320. }
  321. #ifdef CONFIG_WPS
  322. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  323. {
  324. if (!wpa_s->ap_iface)
  325. return -1;
  326. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
  327. }
  328. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  329. const char *pin, char *buf, size_t buflen)
  330. {
  331. int ret, ret_len = 0;
  332. if (!wpa_s->ap_iface)
  333. return -1;
  334. if (pin == NULL) {
  335. unsigned int rpin = wps_generate_pin();
  336. ret_len = os_snprintf(buf, buflen, "%d", rpin);
  337. pin = buf;
  338. }
  339. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
  340. 0);
  341. if (ret)
  342. return -1;
  343. return ret_len;
  344. }
  345. #endif /* CONFIG_WPS */
  346. #ifdef CONFIG_CTRL_IFACE
  347. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  348. char *buf, size_t buflen)
  349. {
  350. if (wpa_s->ap_iface == NULL)
  351. return -1;
  352. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  353. buf, buflen);
  354. }
  355. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  356. char *buf, size_t buflen)
  357. {
  358. if (wpa_s->ap_iface == NULL)
  359. return -1;
  360. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  361. buf, buflen);
  362. }
  363. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  364. char *buf, size_t buflen)
  365. {
  366. if (wpa_s->ap_iface == NULL)
  367. return -1;
  368. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  369. buf, buflen);
  370. }
  371. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  372. size_t buflen, int verbose)
  373. {
  374. char *pos = buf, *end = buf + buflen;
  375. int ret;
  376. struct hostapd_bss_config *conf;
  377. if (wpa_s->ap_iface == NULL)
  378. return -1;
  379. conf = wpa_s->ap_iface->bss[0]->conf;
  380. if (conf->wpa == 0)
  381. return 0;
  382. ret = os_snprintf(pos, end - pos,
  383. "pairwise_cipher=%s\n"
  384. "group_cipher=%s\n"
  385. "key_mgmt=%s\n",
  386. wpa_cipher_txt(conf->rsn_pairwise),
  387. wpa_cipher_txt(conf->wpa_group),
  388. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  389. conf->wpa));
  390. if (ret < 0 || ret >= end - pos)
  391. return pos - buf;
  392. pos += ret;
  393. return pos - buf;
  394. }
  395. #endif /* CONFIG_CTRL_IFACE */
  396. int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
  397. {
  398. struct hostapd_iface *iface = wpa_s->ap_iface;
  399. struct wpa_ssid *ssid = wpa_s->current_ssid;
  400. struct hostapd_data *hapd;
  401. if (ssid == NULL || wpa_s->ap_iface == NULL)
  402. return -1;
  403. ieee802_11_set_beacons(iface);
  404. hapd = iface->bss[0];
  405. hapd->drv.set_ap_wps_ie(hapd);
  406. return 0;
  407. }
  408. int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
  409. const u8 *addr)
  410. {
  411. struct hostapd_data *hapd;
  412. struct hostapd_bss_config *conf;
  413. if (!wpa_s->ap_iface)
  414. return -1;
  415. if (addr)
  416. wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
  417. MAC2STR(addr));
  418. else
  419. wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
  420. hapd = wpa_s->ap_iface->bss[0];
  421. conf = hapd->conf;
  422. os_free(conf->accept_mac);
  423. conf->accept_mac = NULL;
  424. conf->num_accept_mac = 0;
  425. os_free(conf->deny_mac);
  426. conf->deny_mac = NULL;
  427. conf->num_deny_mac = 0;
  428. if (addr == NULL) {
  429. conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
  430. return 0;
  431. }
  432. conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
  433. conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
  434. if (conf->accept_mac == NULL)
  435. return -1;
  436. os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
  437. conf->num_accept_mac = 1;
  438. return 0;
  439. }