ap.c 9.6 KB

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