Merge wpa_supplicant and hostapd driver wrapper implementations
[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 #ifdef NEED_MLME
22 #include "../hostapd/ieee802_11.h"
23 #endif /* NEED_MLME */
24 #include "eap_common/eap_defs.h"
25 #include "eap_server/eap_methods.h"
26 #include "eap_common/eap_wsc_common.h"
27 #include "config_ssid.h"
28 #include "wpa_supplicant_i.h"
29 #include "driver_i.h"
30 #include "ap.h"
31
32
33 int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
34                                          void *ctx), void *ctx)
35 {
36         /* TODO */
37         return 0;
38 }
39
40
41 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
42 {
43         return 0;
44 }
45
46
47 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
48 {
49 }
50
51
52 struct ap_driver_data {
53         struct hostapd_data *hapd;
54 };
55
56
57 static void * ap_driver_init(struct hostapd_data *hapd)
58 {
59         struct ap_driver_data *drv;
60         struct wpa_supplicant *wpa_s = hapd->iface->owner;
61
62         drv = os_zalloc(sizeof(struct ap_driver_data));
63         if (drv == NULL) {
64                 wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
65                            "driver data");
66                 return NULL;
67         }
68         drv->hapd = hapd;
69         os_memcpy(hapd->own_addr, wpa_s->own_addr, ETH_ALEN);
70
71         return drv;
72 }
73
74
75 static void ap_driver_deinit(void *priv)
76 {
77         struct ap_driver_data *drv = priv;
78
79         os_free(drv);
80 }
81
82
83 static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
84                                 u16 proto, const u8 *data, size_t data_len)
85 {
86         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
87         return -1;
88 }
89
90
91 static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
92                              const u8 *addr, int key_idx, int set_tx,
93                              const u8 *seq, size_t seq_len, const u8 *key,
94                              size_t key_len)
95 {
96         struct ap_driver_data *drv = priv;
97         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
98         return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
99                                key, key_len);
100 }
101
102
103 static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
104                                 int idx, u8 *seq)
105 {
106         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
107         return -1;
108 }
109
110
111 static int ap_driver_flush(void *priv)
112 {
113         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
114         return -1;
115 }
116
117
118 static int ap_driver_read_sta_data(void *priv,
119                                    struct hostap_sta_driver_data *data,
120                                    const u8 *addr)
121 {
122         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
123         return -1;
124 }
125
126
127 static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
128                                    int flags_or, int flags_and)
129 {
130         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
131         return -1;
132 }
133
134
135 static int ap_driver_sta_deauth(void *priv, const u8 *addr, int reason)
136 {
137         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
138         return -1;
139 }
140
141
142 static int ap_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
143 {
144         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
145         return -1;
146 }
147
148
149 static int ap_driver_sta_remove(void *priv, const u8 *addr)
150 {
151         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
152         return -1;
153 }
154
155
156 static int ap_driver_send_mgmt_frame(void *priv, const void *data, size_t len,
157                                      int flags)
158 {
159         struct ap_driver_data *drv = priv;
160         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
161         return wpa_drv_send_mlme(wpa_s, data, len);
162 }
163
164
165 static int ap_driver_sta_add(const char *ifname, void *priv,
166                              struct hostapd_sta_add_params *params)
167 {
168         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
169         return -1;
170 }
171
172
173 static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
174 {
175         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
176         return -1;
177 }
178
179
180 static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
181 {
182         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
183         return 0;
184 }
185
186
187 static int ap_driver_set_beacon(const char *iface, void *priv,
188                                 const u8 *head, size_t head_len,
189                                 const u8 *tail, size_t tail_len,
190                                 int dtim_period)
191 {
192         struct ap_driver_data *drv = priv;
193         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
194         return wpa_drv_set_beacon(wpa_s, head, head_len, tail, tail_len,
195                                   dtim_period);
196 }
197
198
199 static int ap_driver_set_beacon_int(void *priv, int value)
200 {
201         struct ap_driver_data *drv = priv;
202         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
203         return wpa_drv_set_beacon_int(wpa_s, value);
204 }
205
206
207 static int ap_driver_set_cts_protect(void *priv, int value)
208 {
209         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
210         return -1;
211 }
212
213
214 static int ap_driver_set_preamble(void *priv, int value)
215 {
216         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
217         return -1;
218 }
219
220
221 static int ap_driver_set_short_slot_time(void *priv, int value)
222 {
223         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
224         return -1;
225 }
226
227
228 static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
229                                          int cw_min, int cw_max,
230                                          int burst_time)
231 {
232         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
233         return -1;
234 }
235
236
237 static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
238                                                               u16 *num_modes,
239                                                               u16 *flags)
240 {
241         struct ap_driver_data *drv = priv;
242         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
243         return wpa_drv_get_hw_feature_data(wpa_s, num_modes, flags);
244 }
245
246
247 struct wpa_driver_ops ap_driver_ops =
248 {
249         .name = "wpa_supplicant",
250         .hapd_init = ap_driver_init,
251         .hapd_deinit = ap_driver_deinit,
252         .send_ether = ap_driver_send_ether,
253         .hapd_set_key = ap_driver_set_key,
254         .get_seqnum = ap_driver_get_seqnum,
255         .flush = ap_driver_flush,
256         .read_sta_data = ap_driver_read_sta_data,
257         .sta_set_flags = ap_driver_sta_set_flags,
258         .sta_deauth = ap_driver_sta_deauth,
259         .sta_disassoc = ap_driver_sta_disassoc,
260         .sta_remove = ap_driver_sta_remove,
261         .send_mgmt_frame = ap_driver_send_mgmt_frame,
262         .sta_add = ap_driver_sta_add,
263         .get_inact_sec = ap_driver_get_inact_sec,
264         .set_freq = ap_driver_set_freq,
265         .hapd_set_beacon = ap_driver_set_beacon,
266         .hapd_set_beacon_int = ap_driver_set_beacon_int,
267         .set_cts_protect = ap_driver_set_cts_protect,
268         .set_preamble = ap_driver_set_preamble,
269         .set_short_slot_time = ap_driver_set_short_slot_time,
270         .set_tx_queue_params = ap_driver_set_tx_queue_params,
271         .hapd_get_hw_feature_data = ap_driver_get_hw_feature_data,
272 };
273
274
275 extern struct wpa_driver_ops *wpa_drivers[];
276
277 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
278                                   struct wpa_ssid *ssid,
279                                   struct hostapd_config *conf)
280 {
281         struct hostapd_bss_config *bss = &conf->bss[0];
282         int j;
283
284         for (j = 0; wpa_drivers[j]; j++) {
285                 if (os_strcmp("wpa_supplicant", wpa_drivers[j]->name) == 0) {
286                         conf->driver = wpa_drivers[j];
287                         break;
288                 }
289         }
290         if (conf->driver == NULL) {
291                 wpa_printf(MSG_ERROR, "No AP driver ops found");
292                 return -1;
293         }
294
295         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
296
297         if (ssid->frequency == 0) {
298                 /* default channel 11 */
299                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
300                 conf->channel = 11;
301         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
302                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
303                 conf->channel = (ssid->frequency - 2407) / 5;
304         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
305                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
306                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
307                 conf->channel = (ssid->frequency - 5000) / 5;
308         } else {
309                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
310                            ssid->frequency);
311                 return -1;
312         }
313
314         /* TODO: enable HT if driver supports it;
315          * drop to 11b if driver does not support 11g */
316
317         if (ssid->ssid_len == 0) {
318                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
319                 return -1;
320         }
321         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
322         bss->ssid.ssid[ssid->ssid_len] = '\0';
323         bss->ssid.ssid_len = ssid->ssid_len;
324         bss->ssid.ssid_set = 1;
325
326         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
327                 bss->wpa = ssid->proto;
328         bss->wpa_key_mgmt = ssid->key_mgmt;
329         bss->wpa_pairwise = ssid->pairwise_cipher;
330         if (ssid->passphrase) {
331                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
332                 if (hostapd_setup_wpa_psk(bss))
333                         return -1;
334         } else if (ssid->psk_set) {
335                 os_free(bss->ssid.wpa_psk);
336                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
337                 if (bss->ssid.wpa_psk == NULL)
338                         return -1;
339                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
340                 bss->ssid.wpa_psk->group = 1;
341         }
342
343         return 0;
344 }
345
346
347 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
348                              struct wpa_ssid *ssid)
349 {
350         struct wpa_driver_associate_params params;
351         struct hostapd_iface *hapd_iface;
352         struct hostapd_config *conf;
353         size_t i;
354
355         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
356                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
357                 return -1;
358         }
359
360         wpa_supplicant_ap_deinit(wpa_s);
361
362         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
363                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
364
365         os_memset(&params, 0, sizeof(params));
366         params.ssid = ssid->ssid;
367         params.ssid_len = ssid->ssid_len;
368         params.mode = ssid->mode;
369         params.freq = ssid->frequency;
370
371         if (wpa_drv_associate(wpa_s, &params) < 0) {
372                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
373                 return -1;
374         }
375
376         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
377         if (hapd_iface == NULL)
378                 return -1;
379         hapd_iface->owner = wpa_s;
380
381         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
382         if (conf == NULL) {
383                 wpa_supplicant_ap_deinit(wpa_s);
384                 return -1;
385         }
386
387         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
388                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
389                 wpa_supplicant_ap_deinit(wpa_s);
390                 return -1;
391         }
392
393         hapd_iface->num_bss = conf->num_bss;
394         hapd_iface->bss = os_zalloc(conf->num_bss *
395                                     sizeof(struct hostapd_data *));
396         if (hapd_iface->bss == NULL) {
397                 wpa_supplicant_ap_deinit(wpa_s);
398                 return -1;
399         }
400
401         for (i = 0; i < conf->num_bss; i++) {
402                 hapd_iface->bss[i] =
403                         hostapd_alloc_bss_data(hapd_iface, conf,
404                                                &conf->bss[i]);
405                 if (hapd_iface->bss[i] == NULL) {
406                         wpa_supplicant_ap_deinit(wpa_s);
407                         return -1;
408                 }
409         }
410
411         if (hostapd_setup_interface(wpa_s->ap_iface)) {
412                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
413                 wpa_supplicant_ap_deinit(wpa_s);
414                 return -1;
415         }
416
417         return 0;
418 }
419
420
421 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
422 {
423         if (wpa_s->ap_iface == NULL)
424                 return;
425
426         hostapd_interface_deinit(wpa_s->ap_iface);
427         wpa_s->ap_iface = NULL;
428 }
429
430
431 void ap_tx_status(void *ctx, const u8 *addr,
432                   const u8 *buf, size_t len, int ack)
433 {
434         struct wpa_supplicant *wpa_s = ctx;
435         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
436 }
437
438
439 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr)
440 {
441         struct wpa_supplicant *wpa_s = ctx;
442         ap_rx_from_unknown_sta(wpa_s->ap_iface->bss[0], addr);
443 }
444
445
446 #ifdef NEED_MLME
447 void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
448                 struct hostapd_frame_info *fi)
449 {
450         struct wpa_supplicant *wpa_s = ctx;
451         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, stype, fi);
452 }
453
454
455 void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok)
456 {
457         struct wpa_supplicant *wpa_s = ctx;
458         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
459 }
460 #endif /* NEED_MLME */