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