wpa_supplicant AP: Fix build after hostapd driver_ops changes
[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                              struct wpa_init_params *params)
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 *own_addr, const u8 *addr,
137                                 int reason)
138 {
139         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
140         return -1;
141 }
142
143
144 static int ap_driver_sta_disassoc(void *priv, const u8 *own_addr,
145                                   const u8 *addr, int reason)
146 {
147         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
148         return -1;
149 }
150
151
152 static int ap_driver_sta_remove(void *priv, const u8 *addr)
153 {
154         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
155         return -1;
156 }
157
158
159 static int ap_driver_send_mlme(void *priv, const u8 *data, size_t len)
160 {
161         struct ap_driver_data *drv = priv;
162         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
163         return wpa_drv_send_mlme(wpa_s, data, len);
164 }
165
166
167 static int ap_driver_sta_add(const char *ifname, void *priv,
168                              struct hostapd_sta_add_params *params)
169 {
170         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
171         return -1;
172 }
173
174
175 static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
176 {
177         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
178         return -1;
179 }
180
181
182 static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
183 {
184         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
185         return 0;
186 }
187
188
189 static int ap_driver_set_beacon(const char *iface, void *priv,
190                                 const u8 *head, size_t head_len,
191                                 const u8 *tail, size_t tail_len,
192                                 int dtim_period)
193 {
194         struct ap_driver_data *drv = priv;
195         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
196         return wpa_drv_set_beacon(wpa_s, head, head_len, tail, tail_len,
197                                   dtim_period);
198 }
199
200
201 static int ap_driver_set_beacon_int(void *priv, int value)
202 {
203         struct ap_driver_data *drv = priv;
204         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
205         return wpa_drv_set_beacon_int(wpa_s, value);
206 }
207
208
209 static int ap_driver_set_cts_protect(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_preamble(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_short_slot_time(void *priv, int value)
224 {
225         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
226         return -1;
227 }
228
229
230 static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
231                                          int cw_min, int cw_max,
232                                          int burst_time)
233 {
234         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
235         return -1;
236 }
237
238
239 static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
240                                                               u16 *num_modes,
241                                                               u16 *flags)
242 {
243         struct ap_driver_data *drv = priv;
244         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
245         return wpa_drv_get_hw_feature_data(wpa_s, num_modes, flags);
246 }
247
248
249 struct wpa_driver_ops ap_driver_ops =
250 {
251         .name = "wpa_supplicant",
252         .hapd_init = ap_driver_init,
253         .hapd_deinit = ap_driver_deinit,
254         .send_ether = ap_driver_send_ether,
255         .hapd_set_key = ap_driver_set_key,
256         .get_seqnum = ap_driver_get_seqnum,
257         .flush = ap_driver_flush,
258         .read_sta_data = ap_driver_read_sta_data,
259         .sta_set_flags = ap_driver_sta_set_flags,
260         .sta_deauth = ap_driver_sta_deauth,
261         .sta_disassoc = ap_driver_sta_disassoc,
262         .sta_remove = ap_driver_sta_remove,
263         .send_mlme = ap_driver_send_mlme,
264         .sta_add = ap_driver_sta_add,
265         .get_inact_sec = ap_driver_get_inact_sec,
266         .set_freq = ap_driver_set_freq,
267         .hapd_set_beacon = ap_driver_set_beacon,
268         .set_beacon_int = ap_driver_set_beacon_int,
269         .set_cts_protect = ap_driver_set_cts_protect,
270         .set_preamble = ap_driver_set_preamble,
271         .set_short_slot_time = ap_driver_set_short_slot_time,
272         .set_tx_queue_params = ap_driver_set_tx_queue_params,
273         .get_hw_feature_data = ap_driver_get_hw_feature_data,
274 };
275
276
277 extern struct wpa_driver_ops *wpa_drivers[];
278
279 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
280                                   struct wpa_ssid *ssid,
281                                   struct hostapd_config *conf)
282 {
283         struct hostapd_bss_config *bss = &conf->bss[0];
284         int j;
285
286         for (j = 0; wpa_drivers[j]; j++) {
287                 if (os_strcmp("wpa_supplicant", wpa_drivers[j]->name) == 0) {
288                         conf->driver = wpa_drivers[j];
289                         break;
290                 }
291         }
292         if (conf->driver == NULL) {
293                 wpa_printf(MSG_ERROR, "No AP driver ops found");
294                 return -1;
295         }
296
297         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
298
299         if (ssid->frequency == 0) {
300                 /* default channel 11 */
301                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
302                 conf->channel = 11;
303         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
304                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
305                 conf->channel = (ssid->frequency - 2407) / 5;
306         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
307                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
308                 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
309                 conf->channel = (ssid->frequency - 5000) / 5;
310         } else {
311                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
312                            ssid->frequency);
313                 return -1;
314         }
315
316         /* TODO: enable HT if driver supports it;
317          * drop to 11b if driver does not support 11g */
318
319         if (ssid->ssid_len == 0) {
320                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
321                 return -1;
322         }
323         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
324         bss->ssid.ssid[ssid->ssid_len] = '\0';
325         bss->ssid.ssid_len = ssid->ssid_len;
326         bss->ssid.ssid_set = 1;
327
328         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
329                 bss->wpa = ssid->proto;
330         bss->wpa_key_mgmt = ssid->key_mgmt;
331         bss->wpa_pairwise = ssid->pairwise_cipher;
332         if (ssid->passphrase) {
333                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
334                 if (hostapd_setup_wpa_psk(bss))
335                         return -1;
336         } else if (ssid->psk_set) {
337                 os_free(bss->ssid.wpa_psk);
338                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
339                 if (bss->ssid.wpa_psk == NULL)
340                         return -1;
341                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
342                 bss->ssid.wpa_psk->group = 1;
343         }
344
345         return 0;
346 }
347
348
349 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
350                              struct wpa_ssid *ssid)
351 {
352         struct wpa_driver_associate_params params;
353         struct hostapd_iface *hapd_iface;
354         struct hostapd_config *conf;
355         size_t i;
356
357         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
358                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
359                 return -1;
360         }
361
362         wpa_supplicant_ap_deinit(wpa_s);
363
364         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
365                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
366
367         os_memset(&params, 0, sizeof(params));
368         params.ssid = ssid->ssid;
369         params.ssid_len = ssid->ssid_len;
370         params.mode = ssid->mode;
371         params.freq = ssid->frequency;
372
373         if (wpa_drv_associate(wpa_s, &params) < 0) {
374                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
375                 return -1;
376         }
377
378         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
379         if (hapd_iface == NULL)
380                 return -1;
381         hapd_iface->owner = wpa_s;
382
383         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
384         if (conf == NULL) {
385                 wpa_supplicant_ap_deinit(wpa_s);
386                 return -1;
387         }
388
389         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
390                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
391                 wpa_supplicant_ap_deinit(wpa_s);
392                 return -1;
393         }
394
395         hapd_iface->num_bss = conf->num_bss;
396         hapd_iface->bss = os_zalloc(conf->num_bss *
397                                     sizeof(struct hostapd_data *));
398         if (hapd_iface->bss == NULL) {
399                 wpa_supplicant_ap_deinit(wpa_s);
400                 return -1;
401         }
402
403         for (i = 0; i < conf->num_bss; i++) {
404                 hapd_iface->bss[i] =
405                         hostapd_alloc_bss_data(hapd_iface, conf,
406                                                &conf->bss[i]);
407                 if (hapd_iface->bss[i] == NULL) {
408                         wpa_supplicant_ap_deinit(wpa_s);
409                         return -1;
410                 }
411         }
412
413         if (hostapd_setup_interface(wpa_s->ap_iface)) {
414                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
415                 wpa_supplicant_ap_deinit(wpa_s);
416                 return -1;
417         }
418
419         return 0;
420 }
421
422
423 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
424 {
425         if (wpa_s->ap_iface == NULL)
426                 return;
427
428         hostapd_interface_deinit(wpa_s->ap_iface);
429         wpa_s->ap_iface = NULL;
430 }
431
432
433 void ap_tx_status(void *ctx, const u8 *addr,
434                   const u8 *buf, size_t len, int ack)
435 {
436         struct wpa_supplicant *wpa_s = ctx;
437         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
438 }
439
440
441 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr)
442 {
443         struct wpa_supplicant *wpa_s = ctx;
444         ap_rx_from_unknown_sta(wpa_s->ap_iface->bss[0], addr);
445 }
446
447
448 #ifdef NEED_MLME
449 void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
450                 struct hostapd_frame_info *fi)
451 {
452         struct wpa_supplicant *wpa_s = ctx;
453         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, stype, fi);
454 }
455
456
457 void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok)
458 {
459         struct wpa_supplicant *wpa_s = ctx;
460         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
461 }
462 #endif /* NEED_MLME */