Add preliminary hostapd data structure initialization for AP mode
[wpasupplicant] / wpa_supplicant / ap.c
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
16 #include "includes.h"
17
18 #include "common.h"
19 #include "../hostapd/hostapd.h"
20 #include "../hostapd/config.h"
21 #include "../hostapd/driver.h"
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
30
31 int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
32                                          void *ctx), void *ctx)
33 {
34         /* TODO */
35         return 0;
36 }
37
38
39 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
40 {
41         return 0;
42 }
43
44
45 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
46 {
47 }
48
49
50 struct ap_driver_data {
51         struct hostapd_data *hapd;
52 };
53
54
55 static void * ap_driver_init(struct hostapd_data *hapd)
56 {
57         struct ap_driver_data *drv;
58
59         drv = os_zalloc(sizeof(struct ap_driver_data));
60         if (drv == NULL) {
61                 wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
62                            "driver data");
63                 return NULL;
64         }
65         drv->hapd = hapd;
66
67         return drv;
68 }
69
70
71 static void ap_driver_deinit(void *priv)
72 {
73         struct ap_driver_data *drv = priv;
74
75         os_free(drv);
76 }
77
78
79 static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
80                                 u16 proto, const u8 *data, size_t data_len)
81 {
82         return 0;
83 }
84
85
86 static struct hapd_driver_ops ap_driver_ops =
87 {
88         .name = "wpa_supplicant",
89         .init = ap_driver_init,
90         .deinit = ap_driver_deinit,
91         .send_ether = ap_driver_send_ether,
92 };
93
94 struct hapd_driver_ops *hostapd_drivers[] =
95 {
96         &ap_driver_ops,
97         NULL
98 };
99
100 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
101                              struct wpa_ssid *ssid)
102 {
103         struct wpa_driver_associate_params params;
104         struct hostapd_iface *hapd_iface;
105         struct hostapd_config *conf;
106         size_t i;
107
108         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
109                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
110                 return -1;
111         }
112
113         wpa_supplicant_ap_deinit(wpa_s);
114         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
115         if (hapd_iface == NULL)
116                 return -1;
117
118         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
119         if (conf == NULL) {
120                 wpa_supplicant_ap_deinit(wpa_s);
121                 return -1;
122         }
123
124         hapd_iface->num_bss = conf->num_bss;
125         hapd_iface->bss = os_zalloc(conf->num_bss *
126                                     sizeof(struct hostapd_data *));
127         if (hapd_iface->bss == NULL) {
128                 wpa_supplicant_ap_deinit(wpa_s);
129                 return -1;
130         }
131
132         for (i = 0; i < conf->num_bss; i++) {
133                 hapd_iface->bss[i] =
134                         hostapd_alloc_bss_data(hapd_iface, conf,
135                                                &conf->bss[i]);
136                 if (hapd_iface->bss[i] == NULL) {
137                         wpa_supplicant_ap_deinit(wpa_s);
138                         return -1;
139                 }
140         }
141
142         if (hostapd_setup_interface(wpa_s->ap_iface)) {
143                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
144                 wpa_supplicant_ap_deinit(wpa_s);
145                 return -1;
146         }
147
148         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
149                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
150
151         os_memset(&params, 0, sizeof(params));
152         params.ssid = ssid->ssid;
153         params.ssid_len = ssid->ssid_len;
154         params.mode = ssid->mode;
155
156         if (wpa_drv_associate(wpa_s, &params) < 0) {
157                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
158                 return -1;
159         }
160
161         return 0;
162 }
163
164
165 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
166 {
167         if (wpa_s->ap_iface == NULL)
168                 return;
169
170         hostapd_interface_deinit(wpa_s->ap_iface);
171         wpa_s->ap_iface = NULL;
172 }