ap.c 11 KB

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