ap.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
  78. struct wpa_ssid *ssid)
  79. {
  80. struct wpa_driver_associate_params params;
  81. struct hostapd_iface *hapd_iface;
  82. struct hostapd_config *conf;
  83. size_t i;
  84. if (ssid->ssid == NULL || ssid->ssid_len == 0) {
  85. wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
  86. return -1;
  87. }
  88. wpa_supplicant_ap_deinit(wpa_s);
  89. wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
  90. if (hapd_iface == NULL)
  91. return -1;
  92. wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
  93. if (conf == NULL) {
  94. wpa_supplicant_ap_deinit(wpa_s);
  95. return -1;
  96. }
  97. hapd_iface->num_bss = conf->num_bss;
  98. hapd_iface->bss = os_zalloc(conf->num_bss *
  99. sizeof(struct hostapd_data *));
  100. if (hapd_iface->bss == NULL) {
  101. wpa_supplicant_ap_deinit(wpa_s);
  102. return -1;
  103. }
  104. for (i = 0; i < conf->num_bss; i++) {
  105. hapd_iface->bss[i] =
  106. hostapd_alloc_bss_data(hapd_iface, conf,
  107. &conf->bss[i]);
  108. if (hapd_iface->bss[i] == NULL) {
  109. wpa_supplicant_ap_deinit(wpa_s);
  110. return -1;
  111. }
  112. }
  113. if (hostapd_setup_interface(wpa_s->ap_iface)) {
  114. wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
  115. wpa_supplicant_ap_deinit(wpa_s);
  116. return -1;
  117. }
  118. wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
  119. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  120. os_memset(&params, 0, sizeof(params));
  121. params.ssid = ssid->ssid;
  122. params.ssid_len = ssid->ssid_len;
  123. params.mode = ssid->mode;
  124. if (wpa_drv_associate(wpa_s, &params) < 0) {
  125. wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
  126. return -1;
  127. }
  128. return 0;
  129. }
  130. void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
  131. {
  132. if (wpa_s->ap_iface == NULL)
  133. return;
  134. hostapd_interface_deinit(wpa_s->ap_iface);
  135. wpa_s->ap_iface = NULL;
  136. }