Add Beacon configuration for wpa_supplicant 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         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
83         return -1;
84 }
85
86
87 static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
88                              const u8 *addr, int key_idx, int set_tx,
89                              const u8 *seq, size_t seq_len, const u8 *key,
90                              size_t key_len)
91 {
92         struct ap_driver_data *drv = priv;
93         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
94         return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
95                                key, key_len);
96 }
97
98
99 static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
100                                 int idx, u8 *seq)
101 {
102         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
103         return -1;
104 }
105
106
107 static int ap_driver_flush(void *priv)
108 {
109         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
110         return -1;
111 }
112
113
114 static int ap_driver_read_sta_data(void *priv,
115                                    struct hostap_sta_driver_data *data,
116                                    const u8 *addr)
117 {
118         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
119         return -1;
120 }
121
122
123 static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
124                                    int flags_or, int flags_and)
125 {
126         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
127         return -1;
128 }
129
130
131 static int ap_driver_sta_deauth(void *priv, const u8 *addr, int reason)
132 {
133         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
134         return -1;
135 }
136
137
138 static int ap_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
139 {
140         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
141         return -1;
142 }
143
144
145 static int ap_driver_sta_remove(void *priv, const u8 *addr)
146 {
147         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
148         return -1;
149 }
150
151
152 static int ap_driver_send_mgmt_frame(void *priv, const void *data, size_t len,
153                                      int flags)
154 {
155         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
156         return -1;
157 }
158
159
160 static int ap_driver_sta_add(const char *ifname, void *priv,
161                              struct hostapd_sta_add_params *params)
162 {
163         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
164         return -1;
165 }
166
167
168 static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
169 {
170         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
171         return -1;
172 }
173
174
175 static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
176 {
177         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
178         return 0;
179 }
180
181
182 static int ap_driver_set_beacon(const char *iface, void *priv,
183                                 const u8 *head, size_t head_len,
184                                 const u8 *tail, size_t tail_len,
185                                 int dtim_period)
186 {
187         struct ap_driver_data *drv = priv;
188         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
189         return wpa_drv_set_beacon(wpa_s, head, head_len, tail, tail_len,
190                                   dtim_period);
191 }
192
193
194 static int ap_driver_set_beacon_int(void *priv, int value)
195 {
196         struct ap_driver_data *drv = priv;
197         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
198         return wpa_drv_set_beacon_int(wpa_s, value);
199 }
200
201
202 static int ap_driver_set_cts_protect(void *priv, int value)
203 {
204         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
205         return -1;
206 }
207
208
209 static int ap_driver_set_preamble(void *priv, int value)
210 {
211         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
212         return -1;
213 }
214
215
216 static int ap_driver_set_short_slot_time(void *priv, int value)
217 {
218         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
219         return -1;
220 }
221
222
223 static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
224                                          int cw_min, int cw_max,
225                                          int burst_time)
226 {
227         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
228         return -1;
229 }
230
231
232 static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
233                                                               u16 *num_modes,
234                                                               u16 *flags)
235 {
236         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
237         return NULL;
238 }
239
240
241 static struct hapd_driver_ops ap_driver_ops =
242 {
243         .name = "wpa_supplicant",
244         .init = ap_driver_init,
245         .deinit = ap_driver_deinit,
246         .send_ether = ap_driver_send_ether,
247         .set_key = ap_driver_set_key,
248         .get_seqnum = ap_driver_get_seqnum,
249         .flush = ap_driver_flush,
250         .read_sta_data = ap_driver_read_sta_data,
251         .sta_set_flags = ap_driver_sta_set_flags,
252         .sta_deauth = ap_driver_sta_deauth,
253         .sta_disassoc = ap_driver_sta_disassoc,
254         .sta_remove = ap_driver_sta_remove,
255         .send_mgmt_frame = ap_driver_send_mgmt_frame,
256         .sta_add = ap_driver_sta_add,
257         .get_inact_sec = ap_driver_get_inact_sec,
258         .set_freq = ap_driver_set_freq,
259         .set_beacon = ap_driver_set_beacon,
260         .set_beacon_int = ap_driver_set_beacon_int,
261         .set_cts_protect = ap_driver_set_cts_protect,
262         .set_preamble = ap_driver_set_preamble,
263         .set_short_slot_time = ap_driver_set_short_slot_time,
264         .set_tx_queue_params = ap_driver_set_tx_queue_params,
265         .get_hw_feature_data = ap_driver_get_hw_feature_data,
266 };
267
268 struct hapd_driver_ops *hostapd_drivers[] =
269 {
270         &ap_driver_ops,
271         NULL
272 };
273
274
275 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
276                                   struct wpa_ssid *ssid,
277                                   struct hostapd_config *conf)
278 {
279         struct hostapd_bss_config *bss = &conf->bss[0];
280
281         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
282
283         if (ssid->frequency == 0) {
284                 /* default channel 11 */
285                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
286                 conf->channel = 11;
287         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
288                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
289                 conf->channel = (ssid->frequency - 2407) / 5;
290         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
291                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
292                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
293                 conf->channel = (ssid->frequency - 5000) / 5;
294         } else {
295                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
296                            ssid->frequency);
297                 return -1;
298         }
299
300         /* TODO: enable HT if driver supports it;
301          * drop to 11b if driver does not support 11g */
302
303         if (ssid->ssid_len == 0) {
304                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
305                 return -1;
306         }
307         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
308         bss->ssid.ssid[ssid->ssid_len] = '\0';
309         bss->ssid.ssid_len = ssid->ssid_len;
310         bss->ssid.ssid_set = 1;
311
312         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
313                 bss->wpa = ssid->proto;
314         bss->wpa_key_mgmt = ssid->key_mgmt;
315         bss->wpa_pairwise = ssid->pairwise_cipher;
316         if (ssid->passphrase) {
317                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
318                 if (hostapd_setup_wpa_psk(bss))
319                         return -1;
320         } else if (ssid->psk_set) {
321                 os_free(bss->ssid.wpa_psk);
322                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
323                 if (bss->ssid.wpa_psk == NULL)
324                         return -1;
325                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
326                 bss->ssid.wpa_psk->group = 1;
327         }
328
329         return 0;
330 }
331
332
333 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
334                              struct wpa_ssid *ssid)
335 {
336         struct wpa_driver_associate_params params;
337         struct hostapd_iface *hapd_iface;
338         struct hostapd_config *conf;
339         size_t i;
340
341         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
342                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
343                 return -1;
344         }
345
346         wpa_supplicant_ap_deinit(wpa_s);
347
348         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
349                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
350
351         os_memset(&params, 0, sizeof(params));
352         params.ssid = ssid->ssid;
353         params.ssid_len = ssid->ssid_len;
354         params.mode = ssid->mode;
355         params.freq = ssid->frequency;
356
357         if (wpa_drv_associate(wpa_s, &params) < 0) {
358                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
359                 return -1;
360         }
361
362         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
363         if (hapd_iface == NULL)
364                 return -1;
365         hapd_iface->owner = wpa_s;
366
367         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
368         if (conf == NULL) {
369                 wpa_supplicant_ap_deinit(wpa_s);
370                 return -1;
371         }
372
373         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
374                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
375                 wpa_supplicant_ap_deinit(wpa_s);
376                 return -1;
377         }
378
379         hapd_iface->num_bss = conf->num_bss;
380         hapd_iface->bss = os_zalloc(conf->num_bss *
381                                     sizeof(struct hostapd_data *));
382         if (hapd_iface->bss == NULL) {
383                 wpa_supplicant_ap_deinit(wpa_s);
384                 return -1;
385         }
386
387         for (i = 0; i < conf->num_bss; i++) {
388                 hapd_iface->bss[i] =
389                         hostapd_alloc_bss_data(hapd_iface, conf,
390                                                &conf->bss[i]);
391                 if (hapd_iface->bss[i] == NULL) {
392                         wpa_supplicant_ap_deinit(wpa_s);
393                         return -1;
394                 }
395         }
396
397         if (hostapd_setup_interface(wpa_s->ap_iface)) {
398                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
399                 wpa_supplicant_ap_deinit(wpa_s);
400                 return -1;
401         }
402
403         return 0;
404 }
405
406
407 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
408 {
409         if (wpa_s->ap_iface == NULL)
410                 return;
411
412         hostapd_interface_deinit(wpa_s->ap_iface);
413         wpa_s->ap_iface = NULL;
414 }