ap.c 12 KB

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