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