ap.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "ap/hostapd.h"
  18. #include "ap/ap_config.h"
  19. #ifdef NEED_AP_MLME
  20. #include "ap/ieee802_11.h"
  21. #endif /* NEED_AP_MLME */
  22. #include "ap/wps_hostapd.h"
  23. #include "ap/ctrl_iface_ap.h"
  24. #include "eap_common/eap_defs.h"
  25. #include "eap_server/eap_methods.h"
  26. #include "eap_common/eap_wsc_common.h"
  27. #include "wps/wps.h"
  28. #include "config_ssid.h"
  29. #include "config.h"
  30. #include "wpa_supplicant_i.h"
  31. #include "driver_i.h"
  32. #include "ap.h"
  33. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  34. struct wpa_ssid *ssid,
  35. struct hostapd_config *conf)
  36. {
  37. struct hostapd_bss_config *bss = &conf->bss[0];
  38. int pairwise;
  39. conf->driver = wpa_s->driver;
  40. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  41. if (ssid->frequency == 0) {
  42. /* default channel 11 */
  43. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  44. conf->channel = 11;
  45. } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
  46. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  47. conf->channel = (ssid->frequency - 2407) / 5;
  48. } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
  49. (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
  50. conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
  51. conf->channel = (ssid->frequency - 5000) / 5;
  52. } else {
  53. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  54. ssid->frequency);
  55. return -1;
  56. }
  57. /* TODO: enable HT if driver supports it;
  58. * drop to 11b if driver does not support 11g */
  59. if (ssid->ssid_len == 0) {
  60. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  61. return -1;
  62. }
  63. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  64. bss->ssid.ssid[ssid->ssid_len] = '\0';
  65. bss->ssid.ssid_len = ssid->ssid_len;
  66. bss->ssid.ssid_set = 1;
  67. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  68. bss->wpa = ssid->proto;
  69. bss->wpa_key_mgmt = ssid->key_mgmt;
  70. bss->wpa_pairwise = ssid->pairwise_cipher;
  71. if (ssid->passphrase) {
  72. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  73. } else if (ssid->psk_set) {
  74. os_free(bss->ssid.wpa_psk);
  75. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  76. if (bss->ssid.wpa_psk == NULL)
  77. return -1;
  78. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  79. bss->ssid.wpa_psk->group = 1;
  80. }
  81. /* Select group cipher based on the enabled pairwise cipher suites */
  82. pairwise = 0;
  83. if (bss->wpa & 1)
  84. pairwise |= bss->wpa_pairwise;
  85. if (bss->wpa & 2) {
  86. if (bss->rsn_pairwise == 0)
  87. bss->rsn_pairwise = bss->wpa_pairwise;
  88. pairwise |= bss->rsn_pairwise;
  89. }
  90. if (pairwise & WPA_CIPHER_TKIP)
  91. bss->wpa_group = WPA_CIPHER_TKIP;
  92. else
  93. bss->wpa_group = WPA_CIPHER_CCMP;
  94. if (bss->wpa && bss->ieee802_1x)
  95. bss->ssid.security_policy = SECURITY_WPA;
  96. else if (bss->wpa)
  97. bss->ssid.security_policy = SECURITY_WPA_PSK;
  98. else if (bss->ieee802_1x) {
  99. bss->ssid.security_policy = SECURITY_IEEE_802_1X;
  100. bss->ssid.wep.default_len = bss->default_wep_key_len;
  101. } else if (bss->ssid.wep.keys_set)
  102. bss->ssid.security_policy = SECURITY_STATIC_WEP;
  103. else
  104. bss->ssid.security_policy = SECURITY_PLAINTEXT;
  105. #ifdef CONFIG_WPS
  106. /*
  107. * Enable WPS by default, but require user interaction to actually use
  108. * it. Only the internal Registrar is supported.
  109. */
  110. bss->eap_server = 1;
  111. bss->wps_state = 2;
  112. bss->ap_setup_locked = 1;
  113. if (wpa_s->conf->config_methods)
  114. bss->config_methods = os_strdup(wpa_s->conf->config_methods);
  115. if (wpa_s->conf->device_type)
  116. bss->device_type = os_strdup(wpa_s->conf->device_type);
  117. #endif /* CONFIG_WPS */
  118. return 0;
  119. }
  120. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  121. struct wpa_ssid *ssid)
  122. {
  123. struct wpa_driver_associate_params params;
  124. struct hostapd_iface *hapd_iface;
  125. struct hostapd_config *conf;
  126. size_t i;
  127. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  128. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  129. return -1;
  130. }
  131. wpa_supplicant_ap_deinit(wpa_s);
  132. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  133. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  134. os_memset(&params, 0, sizeof(params));
  135. params.ssid = ssid->ssid;
  136. params.ssid_len = ssid->ssid_len;
  137. params.mode = ssid->mode;
  138. params.freq = ssid->frequency;
  139. if (wpa_drv_associate(wpa_s, &params) < 0) {
  140. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  141. return -1;
  142. }
  143. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  144. if (hapd_iface == NULL)
  145. return -1;
  146. hapd_iface->owner = wpa_s;
  147. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  148. if (conf == NULL) {
  149. wpa_supplicant_ap_deinit(wpa_s);
  150. return -1;
  151. }
  152. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  153. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  154. wpa_supplicant_ap_deinit(wpa_s);
  155. return -1;
  156. }
  157. hapd_iface->num_bss = conf->num_bss;
  158. hapd_iface->bss = os_zalloc(conf->num_bss *
  159. sizeof(struct hostapd_data *));
  160. if (hapd_iface->bss == NULL) {
  161. wpa_supplicant_ap_deinit(wpa_s);
  162. return -1;
  163. }
  164. for (i = 0; i < conf->num_bss; i++) {
  165. hapd_iface->bss[i] =
  166. hostapd_alloc_bss_data(hapd_iface, conf,
  167. &conf->bss[i]);
  168. if (hapd_iface->bss[i] == NULL) {
  169. wpa_supplicant_ap_deinit(wpa_s);
  170. return -1;
  171. }
  172. hapd_iface->bss[i]->msg_ctx = wpa_s;
  173. }
  174. os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
  175. hapd_iface->bss[0]->driver = wpa_s->driver;
  176. hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
  177. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  178. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  179. wpa_supplicant_ap_deinit(wpa_s);
  180. return -1;
  181. }
  182. wpa_s->current_ssid = ssid;
  183. os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
  184. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  185. return 0;
  186. }
  187. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  188. {
  189. if (wpa_s->ap_iface == NULL)
  190. return;
  191. hostapd_interface_deinit(wpa_s->ap_iface);
  192. wpa_s->ap_iface = NULL;
  193. }
  194. void ap_tx_status(void *ctx, const u8 *addr,
  195. const u8 *buf, size_t len, int ack)
  196. {
  197. #ifdef NEED_AP_MLME
  198. struct wpa_supplicant *wpa_s = ctx;
  199. hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
  200. #endif /* NEED_AP_MLME */
  201. }
  202. void ap_rx_from_unknown_sta(void *ctx, const struct ieee80211_hdr *hdr,
  203. size_t len)
  204. {
  205. #ifdef NEED_AP_MLME
  206. struct wpa_supplicant *wpa_s = ctx;
  207. u16 fc = le_to_host16(hdr->frame_control);
  208. ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
  209. (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  210. (WLAN_FC_TODS | WLAN_FC_FROMDS));
  211. #endif /* NEED_AP_MLME */
  212. }
  213. void ap_mgmt_rx(void *ctx, const u8 *buf, size_t len,
  214. struct hostapd_frame_info *fi)
  215. {
  216. #ifdef NEED_AP_MLME
  217. struct wpa_supplicant *wpa_s = ctx;
  218. ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, fi);
  219. #endif /* NEED_AP_MLME */
  220. }
  221. void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
  222. {
  223. #ifdef NEED_AP_MLME
  224. struct wpa_supplicant *wpa_s = ctx;
  225. ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
  226. #endif /* NEED_AP_MLME */
  227. }
  228. void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
  229. const u8 *src_addr, const u8 *buf, size_t len)
  230. {
  231. hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
  232. }
  233. #ifdef CONFIG_WPS
  234. int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  235. {
  236. return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
  237. }
  238. int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  239. const char *pin, char *buf, size_t buflen)
  240. {
  241. int ret, ret_len = 0;
  242. if (pin == NULL) {
  243. unsigned int rpin = wps_generate_pin();
  244. ret_len = os_snprintf(buf, buflen, "%d", rpin);
  245. pin = buf;
  246. }
  247. ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
  248. if (ret)
  249. return -1;
  250. return ret_len;
  251. }
  252. #endif /* CONFIG_WPS */
  253. #ifdef CONFIG_CTRL_IFACE
  254. int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
  255. char *buf, size_t buflen)
  256. {
  257. if (wpa_s->ap_iface == NULL)
  258. return -1;
  259. return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
  260. buf, buflen);
  261. }
  262. int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
  263. char *buf, size_t buflen)
  264. {
  265. if (wpa_s->ap_iface == NULL)
  266. return -1;
  267. return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
  268. buf, buflen);
  269. }
  270. int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
  271. char *buf, size_t buflen)
  272. {
  273. if (wpa_s->ap_iface == NULL)
  274. return -1;
  275. return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
  276. buf, buflen);
  277. }
  278. int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
  279. size_t buflen, int verbose)
  280. {
  281. char *pos = buf, *end = buf + buflen;
  282. int ret;
  283. struct hostapd_bss_config *conf;
  284. if (wpa_s->ap_iface == NULL)
  285. return -1;
  286. conf = wpa_s->ap_iface->bss[0]->conf;
  287. if (conf->wpa == 0)
  288. return 0;
  289. ret = os_snprintf(pos, end - pos,
  290. "pairwise_cipher=%s\n"
  291. "group_cipher=%s\n"
  292. "key_mgmt=%s\n",
  293. wpa_cipher_txt(conf->rsn_pairwise),
  294. wpa_cipher_txt(conf->wpa_group),
  295. wpa_key_mgmt_txt(conf->wpa_key_mgmt,
  296. conf->wpa));
  297. if (ret < 0 || ret >= end - pos)
  298. return pos - buf;
  299. pos += ret;
  300. return pos - buf;
  301. }
  302. #endif /* CONFIG_CTRL_IFACE */