ap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. return 0;
  64. }
  65. static struct hapd_driver_ops ap_driver_ops =
  66. {
  67. .name = "wpa_supplicant",
  68. .init = ap_driver_init,
  69. .deinit = ap_driver_deinit,
  70. .send_ether = ap_driver_send_ether,
  71. };
  72. struct hapd_driver_ops *hostapd_drivers[] =
  73. {
  74. &ap_driver_ops,
  75. NULL
  76. };
  77. static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
  78. struct wpa_ssid *ssid,
  79. struct hostapd_config *conf)
  80. {
  81. struct hostapd_bss_config *bss = &conf->bss[0];
  82. os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
  83. if (ssid->frequency == 0) {
  84. /* default channel 11 */
  85. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  86. conf->channel = 11;
  87. } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
  88. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  89. conf->channel = (ssid->frequency - 2407) / 5;
  90. } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
  91. (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
  92. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  93. conf->channel = (ssid->frequency - 5000) / 5;
  94. } else {
  95. wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
  96. ssid->frequency);
  97. return -1;
  98. }
  99. /* TODO: enable HT if driver supports it;
  100. * drop to 11b if driver does not support 11g */
  101. if (ssid->ssid_len == 0) {
  102. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  103. return -1;
  104. }
  105. os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
  106. bss->ssid.ssid[ssid->ssid_len] = '\0';
  107. bss->ssid.ssid_len = ssid->ssid_len;
  108. bss->ssid.ssid_set = 1;
  109. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
  110. bss->wpa = ssid->proto;
  111. bss->wpa_key_mgmt = ssid->key_mgmt;
  112. bss->wpa_pairwise = ssid->pairwise_cipher;
  113. if (ssid->passphrase) {
  114. bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
  115. if (hostapd_setup_wpa_psk(bss))
  116. return -1;
  117. } else if (ssid->psk_set) {
  118. os_free(bss->ssid.wpa_psk);
  119. bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
  120. if (bss->ssid.wpa_psk == NULL)
  121. return -1;
  122. os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
  123. bss->ssid.wpa_psk->group = 1;
  124. }
  125. return 0;
  126. }
  127. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  128. struct wpa_ssid *ssid)
  129. {
  130. struct wpa_driver_associate_params params;
  131. struct hostapd_iface *hapd_iface;
  132. struct hostapd_config *conf;
  133. size_t i;
  134. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  135. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  136. return -1;
  137. }
  138. wpa_supplicant_ap_deinit(wpa_s);
  139. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  140. if (hapd_iface == NULL)
  141. return -1;
  142. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  143. if (conf == NULL) {
  144. wpa_supplicant_ap_deinit(wpa_s);
  145. return -1;
  146. }
  147. if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
  148. wpa_printf(MSG_ERROR, "Failed to create AP configuration");
  149. wpa_supplicant_ap_deinit(wpa_s);
  150. return -1;
  151. }
  152. hapd_iface->num_bss = conf->num_bss;
  153. hapd_iface->bss = os_zalloc(conf->num_bss *
  154. sizeof(struct hostapd_data *));
  155. if (hapd_iface->bss == NULL) {
  156. wpa_supplicant_ap_deinit(wpa_s);
  157. return -1;
  158. }
  159. for (i = 0; i < conf->num_bss; i++) {
  160. hapd_iface->bss[i] =
  161. hostapd_alloc_bss_data(hapd_iface, conf,
  162. &conf->bss[i]);
  163. if (hapd_iface->bss[i] == NULL) {
  164. wpa_supplicant_ap_deinit(wpa_s);
  165. return -1;
  166. }
  167. }
  168. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  169. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  170. wpa_supplicant_ap_deinit(wpa_s);
  171. return -1;
  172. }
  173. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  174. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  175. os_memset(&params, 0, sizeof(params));
  176. params.ssid = ssid->ssid;
  177. params.ssid_len = ssid->ssid_len;
  178. params.mode = ssid->mode;
  179. if (wpa_drv_associate(wpa_s, &params) < 0) {
  180. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  181. return -1;
  182. }
  183. return 0;
  184. }
  185. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  186. {
  187. if (wpa_s->ap_iface == NULL)
  188. return;
  189. hostapd_interface_deinit(wpa_s->ap_iface);
  190. wpa_s->ap_iface = NULL;
  191. }