Set hostapd configuration based on wpa_supplicant AP mode config
[libeap.git] / 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
101 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
102                                   struct wpa_ssid *ssid,
103                                   struct hostapd_config *conf)
104 {
105         struct hostapd_bss_config *bss = &conf->bss[0];
106
107         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
108
109         if (ssid->frequency == 0) {
110                 /* default channel 11 */
111                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
112                 conf->channel = 11;
113         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
114                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
115                 conf->channel = (ssid->frequency - 2407) / 5;
116         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
117                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
118                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
119                 conf->channel = (ssid->frequency - 5000) / 5;
120         } else {
121                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
122                            ssid->frequency);
123                 return -1;
124         }
125
126         /* TODO: enable HT if driver supports it;
127          * drop to 11b if driver does not support 11g */
128
129         if (ssid->ssid_len == 0) {
130                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
131                 return -1;
132         }
133         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
134         bss->ssid.ssid[ssid->ssid_len] = '\0';
135         bss->ssid.ssid_len = ssid->ssid_len;
136         bss->ssid.ssid_set = 1;
137
138         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
139                 bss->wpa = ssid->proto;
140         bss->wpa_key_mgmt = ssid->key_mgmt;
141         bss->wpa_pairwise = ssid->pairwise_cipher;
142         if (ssid->passphrase) {
143                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
144                 if (hostapd_setup_wpa_psk(bss))
145                         return -1;
146         } else if (ssid->psk_set) {
147                 os_free(bss->ssid.wpa_psk);
148                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
149                 if (bss->ssid.wpa_psk == NULL)
150                         return -1;
151                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
152                 bss->ssid.wpa_psk->group = 1;
153         }
154
155         return 0;
156 }
157
158
159 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
160                              struct wpa_ssid *ssid)
161 {
162         struct wpa_driver_associate_params params;
163         struct hostapd_iface *hapd_iface;
164         struct hostapd_config *conf;
165         size_t i;
166
167         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
168                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
169                 return -1;
170         }
171
172         wpa_supplicant_ap_deinit(wpa_s);
173         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
174         if (hapd_iface == NULL)
175                 return -1;
176
177         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
178         if (conf == NULL) {
179                 wpa_supplicant_ap_deinit(wpa_s);
180                 return -1;
181         }
182
183         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
184                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
185                 wpa_supplicant_ap_deinit(wpa_s);
186                 return -1;
187         }
188
189         hapd_iface->num_bss = conf->num_bss;
190         hapd_iface->bss = os_zalloc(conf->num_bss *
191                                     sizeof(struct hostapd_data *));
192         if (hapd_iface->bss == NULL) {
193                 wpa_supplicant_ap_deinit(wpa_s);
194                 return -1;
195         }
196
197         for (i = 0; i < conf->num_bss; i++) {
198                 hapd_iface->bss[i] =
199                         hostapd_alloc_bss_data(hapd_iface, conf,
200                                                &conf->bss[i]);
201                 if (hapd_iface->bss[i] == NULL) {
202                         wpa_supplicant_ap_deinit(wpa_s);
203                         return -1;
204                 }
205         }
206
207         if (hostapd_setup_interface(wpa_s->ap_iface)) {
208                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
209                 wpa_supplicant_ap_deinit(wpa_s);
210                 return -1;
211         }
212
213         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
214                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
215
216         os_memset(&params, 0, sizeof(params));
217         params.ssid = ssid->ssid;
218         params.ssid_len = ssid->ssid_len;
219         params.mode = ssid->mode;
220
221         if (wpa_drv_associate(wpa_s, &params) < 0) {
222                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
223                 return -1;
224         }
225
226         return 0;
227 }
228
229
230 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
231 {
232         if (wpa_s->ap_iface == NULL)
233                 return;
234
235         hostapd_interface_deinit(wpa_s->ap_iface);
236         wpa_s->ap_iface = NULL;
237 }