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