ap.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "../hostapd/hostapd.h"
  18. #include "../hostapd/config.h"
  19. #include "../hostapd/driver.h"
  20. #include "eap_common/eap_defs.h"
  21. #include "eap_server/eap_methods.h"
  22. #include "eap_common/eap_wsc_common.h"
  23. #include "config_ssid.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "ap.h"
  27. int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
  28. void *ctx), void *ctx)
  29. {
  30. /* TODO */
  31. return 0;
  32. }
  33. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  34. {
  35. return 0;
  36. }
  37. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  38. {
  39. }
  40. struct ap_driver_data {
  41. struct hostapd_data *hapd;
  42. };
  43. static void * ap_driver_init(struct hostapd_data *hapd)
  44. {
  45. struct ap_driver_data *drv;
  46. drv = os_zalloc(sizeof(struct ap_driver_data));
  47. if (drv == NULL) {
  48. wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
  49. "driver data");
  50. return NULL;
  51. }
  52. drv->hapd = hapd;
  53. return drv;
  54. }
  55. static void ap_driver_deinit(void *priv)
  56. {
  57. struct ap_driver_data *drv = priv;
  58. os_free(drv);
  59. }
  60. static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
  61. u16 proto, const u8 *data, size_t data_len)
  62. {
  63. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  64. return -1;
  65. }
  66. static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
  67. const u8 *addr, int key_idx, int set_tx,
  68. const u8 *seq, size_t seq_len, const u8 *key,
  69. size_t key_len)
  70. {
  71. struct ap_driver_data *drv = priv;
  72. struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
  73. return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
  74. key, key_len);
  75. }
  76. static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
  77. int idx, u8 *seq)
  78. {
  79. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  80. return -1;
  81. }
  82. static int ap_driver_flush(void *priv)
  83. {
  84. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  85. return -1;
  86. }
  87. static int ap_driver_read_sta_data(void *priv,
  88. struct hostap_sta_driver_data *data,
  89. const u8 *addr)
  90. {
  91. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  92. return -1;
  93. }
  94. static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
  95. int flags_or, int flags_and)
  96. {
  97. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  98. return -1;
  99. }
  100. static int ap_driver_sta_deauth(void *priv, const u8 *addr, int reason)
  101. {
  102. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  103. return -1;
  104. }
  105. static int ap_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
  106. {
  107. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  108. return -1;
  109. }
  110. static int ap_driver_sta_remove(void *priv, const u8 *addr)
  111. {
  112. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  113. return -1;
  114. }
  115. static int ap_driver_send_mgmt_frame(void *priv, const void *data, size_t len,
  116. int flags)
  117. {
  118. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  119. return -1;
  120. }
  121. static int ap_driver_sta_add(const char *ifname, void *priv,
  122. struct hostapd_sta_add_params *params)
  123. {
  124. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  125. return -1;
  126. }
  127. static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
  128. {
  129. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  130. return -1;
  131. }
  132. static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
  133. {
  134. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  135. return 0;
  136. }
  137. static int ap_driver_set_beacon(const char *iface, void *priv,
  138. u8 *head, size_t head_len,
  139. u8 *tail, size_t tail_len)
  140. {
  141. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  142. return -1;
  143. }
  144. static int ap_driver_set_beacon_int(void *priv, int value)
  145. {
  146. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  147. return -1;
  148. }
  149. static int ap_driver_set_dtim_period(const char *iface, void *priv, int value)
  150. {
  151. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  152. return -1;
  153. }
  154. static int ap_driver_set_cts_protect(void *priv, int value)
  155. {
  156. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  157. return -1;
  158. }
  159. static int ap_driver_set_preamble(void *priv, int value)
  160. {
  161. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  162. return -1;
  163. }
  164. static int ap_driver_set_short_slot_time(void *priv, int value)
  165. {
  166. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  167. return -1;
  168. }
  169. static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
  170. int cw_min, int cw_max,
  171. int burst_time)
  172. {
  173. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  174. return -1;
  175. }
  176. static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
  177. u16 *num_modes,
  178. u16 *flags)
  179. {
  180. wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
  181. return NULL;
  182. }
  183. static struct hapd_driver_ops ap_driver_ops =
  184. {
  185. .name = "wpa_supplicant",
  186. .init = ap_driver_init,
  187. .deinit = ap_driver_deinit,
  188. .send_ether = ap_driver_send_ether,
  189. .set_key = ap_driver_set_key,
  190. .get_seqnum = ap_driver_get_seqnum,
  191. .flush = ap_driver_flush,
  192. .read_sta_data = ap_driver_read_sta_data,
  193. .sta_set_flags = ap_driver_sta_set_flags,
  194. .sta_deauth = ap_driver_sta_deauth,
  195. .sta_disassoc = ap_driver_sta_disassoc,
  196. .sta_remove = ap_driver_sta_remove,
  197. .send_mgmt_frame = ap_driver_send_mgmt_frame,
  198. .sta_add = ap_driver_sta_add,
  199. .get_inact_sec = ap_driver_get_inact_sec,
  200. .set_freq = ap_driver_set_freq,
  201. .set_beacon = ap_driver_set_beacon,
  202. .set_beacon_int = ap_driver_set_beacon_int,
  203. .set_dtim_period = ap_driver_set_dtim_period,
  204. .set_cts_protect = ap_driver_set_cts_protect,
  205. .set_preamble = ap_driver_set_preamble,
  206. .set_short_slot_time = ap_driver_set_short_slot_time,
  207. .set_tx_queue_params = ap_driver_set_tx_queue_params,
  208. .get_hw_feature_data = ap_driver_get_hw_feature_data,
  209. };
  210. struct hapd_driver_ops *hostapd_drivers[] =
  211. {
  212. &ap_driver_ops,
  213. NULL
  214. };
  215. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  216. struct wpa_ssid *ssid,
  217. struct hostapd_config *conf)
  218. {
  219. struct hostapd_bss_config *bss = &conf->bss[0];
  220. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  221. if (ssid->frequency == 0) {
  222. /* default channel 11 */
  223. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  224. conf->channel = 11;
  225. } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
  226. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  227. conf->channel = (ssid->frequency - 2407) / 5;
  228. } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
  229. (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
  230. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  231. conf->channel = (ssid->frequency - 5000) / 5;
  232. } else {
  233. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  234. ssid->frequency);
  235. return -1;
  236. }
  237. /* TODO: enable HT if driver supports it;
  238. * drop to 11b if driver does not support 11g */
  239. if (ssid->ssid_len == 0) {
  240. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  241. return -1;
  242. }
  243. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  244. bss->ssid.ssid[ssid->ssid_len] = '\0';
  245. bss->ssid.ssid_len = ssid->ssid_len;
  246. bss->ssid.ssid_set = 1;
  247. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  248. bss->wpa = ssid->proto;
  249. bss->wpa_key_mgmt = ssid->key_mgmt;
  250. bss->wpa_pairwise = ssid->pairwise_cipher;
  251. if (ssid->passphrase) {
  252. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  253. if (hostapd_setup_wpa_psk(bss))
  254. return -1;
  255. } else if (ssid->psk_set) {
  256. os_free(bss->ssid.wpa_psk);
  257. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  258. if (bss->ssid.wpa_psk == NULL)
  259. return -1;
  260. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  261. bss->ssid.wpa_psk->group = 1;
  262. }
  263. return 0;
  264. }
  265. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  266. struct wpa_ssid *ssid)
  267. {
  268. struct wpa_driver_associate_params params;
  269. struct hostapd_iface *hapd_iface;
  270. struct hostapd_config *conf;
  271. size_t i;
  272. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  273. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  274. return -1;
  275. }
  276. wpa_supplicant_ap_deinit(wpa_s);
  277. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  278. if (hapd_iface == NULL)
  279. return -1;
  280. hapd_iface->owner = wpa_s;
  281. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  282. if (conf == NULL) {
  283. wpa_supplicant_ap_deinit(wpa_s);
  284. return -1;
  285. }
  286. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  287. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  288. wpa_supplicant_ap_deinit(wpa_s);
  289. return -1;
  290. }
  291. hapd_iface->num_bss = conf->num_bss;
  292. hapd_iface->bss = os_zalloc(conf->num_bss *
  293. sizeof(struct hostapd_data *));
  294. if (hapd_iface->bss == NULL) {
  295. wpa_supplicant_ap_deinit(wpa_s);
  296. return -1;
  297. }
  298. for (i = 0; i < conf->num_bss; i++) {
  299. hapd_iface->bss[i] =
  300. hostapd_alloc_bss_data(hapd_iface, conf,
  301. &conf->bss[i]);
  302. if (hapd_iface->bss[i] == NULL) {
  303. wpa_supplicant_ap_deinit(wpa_s);
  304. return -1;
  305. }
  306. }
  307. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  308. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  309. wpa_supplicant_ap_deinit(wpa_s);
  310. return -1;
  311. }
  312. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  313. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  314. os_memset(&params, 0, sizeof(params));
  315. params.ssid = ssid->ssid;
  316. params.ssid_len = ssid->ssid_len;
  317. params.mode = ssid->mode;
  318. if (wpa_drv_associate(wpa_s, &params) < 0) {
  319. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  320. return -1;
  321. }
  322. return 0;
  323. }
  324. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  325. {
  326. if (wpa_s->ap_iface == NULL)
  327. return;
  328. hostapd_interface_deinit(wpa_s->ap_iface);
  329. wpa_s->ap_iface = NULL;
  330. }