ap.c 11 KB

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