Do not use virtual driver_ops for wpa_supplicant AP mode
[libeap.git] / 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 "ap/hostapd.h"
20 #include "ap/config.h"
21 #ifdef NEED_AP_MLME
22 #include "ap/ieee802_11.h"
23 #endif /* NEED_AP_MLME */
24 #include "ap/wps_hostapd.h"
25 #include "ap/ctrl_iface_ap.h"
26 #include "eap_common/eap_defs.h"
27 #include "eap_server/eap_methods.h"
28 #include "eap_common/eap_wsc_common.h"
29 #include "wps/wps.h"
30 #include "config_ssid.h"
31 #include "config.h"
32 #include "wpa_supplicant_i.h"
33 #include "driver_i.h"
34 #include "ap.h"
35
36
37 struct hapd_interfaces {
38         size_t count;
39         struct hostapd_iface **iface;
40 };
41
42
43 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
44                                int (*cb)(struct hostapd_iface *iface,
45                                          void *ctx), void *ctx)
46 {
47         /* TODO */
48         return 0;
49 }
50
51
52 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
53 {
54         return 0;
55 }
56
57
58 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
59 {
60 }
61
62
63 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
64                                   struct wpa_ssid *ssid,
65                                   struct hostapd_config *conf)
66 {
67         struct hostapd_bss_config *bss = &conf->bss[0];
68         int pairwise;
69
70         conf->driver = wpa_s->driver;
71
72         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
73
74         if (ssid->frequency == 0) {
75                 /* default channel 11 */
76                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
77                 conf->channel = 11;
78         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
79                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
80                 conf->channel = (ssid->frequency - 2407) / 5;
81         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
82                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
83                 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
84                 conf->channel = (ssid->frequency - 5000) / 5;
85         } else {
86                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
87                            ssid->frequency);
88                 return -1;
89         }
90
91         /* TODO: enable HT if driver supports it;
92          * drop to 11b if driver does not support 11g */
93
94         if (ssid->ssid_len == 0) {
95                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
96                 return -1;
97         }
98         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
99         bss->ssid.ssid[ssid->ssid_len] = '\0';
100         bss->ssid.ssid_len = ssid->ssid_len;
101         bss->ssid.ssid_set = 1;
102
103         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
104                 bss->wpa = ssid->proto;
105         bss->wpa_key_mgmt = ssid->key_mgmt;
106         bss->wpa_pairwise = ssid->pairwise_cipher;
107         if (ssid->passphrase) {
108                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
109         } else if (ssid->psk_set) {
110                 os_free(bss->ssid.wpa_psk);
111                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
112                 if (bss->ssid.wpa_psk == NULL)
113                         return -1;
114                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
115                 bss->ssid.wpa_psk->group = 1;
116         }
117
118         /* Select group cipher based on the enabled pairwise cipher suites */
119         pairwise = 0;
120         if (bss->wpa & 1)
121                 pairwise |= bss->wpa_pairwise;
122         if (bss->wpa & 2) {
123                 if (bss->rsn_pairwise == 0)
124                         bss->rsn_pairwise = bss->wpa_pairwise;
125                 pairwise |= bss->rsn_pairwise;
126         }
127         if (pairwise & WPA_CIPHER_TKIP)
128                 bss->wpa_group = WPA_CIPHER_TKIP;
129         else
130                 bss->wpa_group = WPA_CIPHER_CCMP;
131
132         if (bss->wpa && bss->ieee802_1x)
133                 bss->ssid.security_policy = SECURITY_WPA;
134         else if (bss->wpa)
135                 bss->ssid.security_policy = SECURITY_WPA_PSK;
136         else if (bss->ieee802_1x) {
137                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
138                 bss->ssid.wep.default_len = bss->default_wep_key_len;
139         } else if (bss->ssid.wep.keys_set)
140                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
141         else
142                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
143
144 #ifdef CONFIG_WPS
145         /*
146          * Enable WPS by default, but require user interaction to actually use
147          * it. Only the internal Registrar is supported.
148          */
149         bss->eap_server = 1;
150         bss->wps_state = 2;
151         bss->ap_setup_locked = 1;
152         if (wpa_s->conf->config_methods)
153                 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
154         if (wpa_s->conf->device_type)
155                 bss->device_type = os_strdup(wpa_s->conf->device_type);
156 #endif /* CONFIG_WPS */
157
158         return 0;
159 }
160
161
162 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
163                              struct wpa_ssid *ssid)
164 {
165         struct wpa_driver_associate_params params;
166         struct hostapd_iface *hapd_iface;
167         struct hostapd_config *conf;
168         size_t i;
169
170         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
171                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
172                 return -1;
173         }
174
175         wpa_supplicant_ap_deinit(wpa_s);
176
177         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
178                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
179
180         os_memset(&params, 0, sizeof(params));
181         params.ssid = ssid->ssid;
182         params.ssid_len = ssid->ssid_len;
183         params.mode = ssid->mode;
184         params.freq = ssid->frequency;
185
186         if (wpa_drv_associate(wpa_s, &params) < 0) {
187                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
188                 return -1;
189         }
190
191         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
192         if (hapd_iface == NULL)
193                 return -1;
194         hapd_iface->owner = wpa_s;
195
196         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
197         if (conf == NULL) {
198                 wpa_supplicant_ap_deinit(wpa_s);
199                 return -1;
200         }
201
202         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
203                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
204                 wpa_supplicant_ap_deinit(wpa_s);
205                 return -1;
206         }
207
208         hapd_iface->num_bss = conf->num_bss;
209         hapd_iface->bss = os_zalloc(conf->num_bss *
210                                     sizeof(struct hostapd_data *));
211         if (hapd_iface->bss == NULL) {
212                 wpa_supplicant_ap_deinit(wpa_s);
213                 return -1;
214         }
215
216         for (i = 0; i < conf->num_bss; i++) {
217                 hapd_iface->bss[i] =
218                         hostapd_alloc_bss_data(hapd_iface, conf,
219                                                &conf->bss[i]);
220                 if (hapd_iface->bss[i] == NULL) {
221                         wpa_supplicant_ap_deinit(wpa_s);
222                         return -1;
223                 }
224
225                 hapd_iface->bss[i]->msg_ctx = wpa_s;
226         }
227
228         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
229         hapd_iface->bss[0]->driver = wpa_s->driver;
230         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
231
232         if (hostapd_setup_interface(wpa_s->ap_iface)) {
233                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
234                 wpa_supplicant_ap_deinit(wpa_s);
235                 return -1;
236         }
237
238         wpa_s->current_ssid = ssid;
239         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
240         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
241
242         return 0;
243 }
244
245
246 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
247 {
248         if (wpa_s->ap_iface == NULL)
249                 return;
250
251         hostapd_interface_deinit(wpa_s->ap_iface);
252         wpa_s->ap_iface = NULL;
253 }
254
255
256 void ap_tx_status(void *ctx, const u8 *addr,
257                   const u8 *buf, size_t len, int ack)
258 {
259 #ifdef NEED_AP_MLME
260         struct wpa_supplicant *wpa_s = ctx;
261         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
262 #endif /* NEED_AP_MLME */
263 }
264
265
266 void ap_rx_from_unknown_sta(void *ctx, const struct ieee80211_hdr *hdr,
267                             size_t len)
268 {
269 #ifdef NEED_AP_MLME
270         struct wpa_supplicant *wpa_s = ctx;
271         u16 fc = le_to_host16(hdr->frame_control);
272         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
273                                    (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
274                                    (WLAN_FC_TODS | WLAN_FC_FROMDS));
275 #endif /* NEED_AP_MLME */
276 }
277
278
279 void ap_mgmt_rx(void *ctx, const u8 *buf, size_t len,
280                 struct hostapd_frame_info *fi)
281 {
282 #ifdef NEED_AP_MLME
283         struct wpa_supplicant *wpa_s = ctx;
284         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, fi);
285 #endif /* NEED_AP_MLME */
286 }
287
288
289 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
290 {
291 #ifdef NEED_AP_MLME
292         struct wpa_supplicant *wpa_s = ctx;
293         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
294 #endif /* NEED_AP_MLME */
295 }
296
297
298 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
299                                 const u8 *src_addr, const u8 *buf, size_t len)
300 {
301         hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
302 }
303
304
305 #ifdef CONFIG_WPS
306
307 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
308 {
309         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
310 }
311
312
313 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
314                               const char *pin, char *buf, size_t buflen)
315 {
316         int ret, ret_len = 0;
317
318         if (pin == NULL) {
319                 unsigned int rpin = wps_generate_pin();
320                 ret_len = os_snprintf(buf, buflen, "%d", rpin);
321                 pin = buf;
322         }
323
324         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
325         if (ret)
326                 return -1;
327         return ret_len;
328 }
329
330 #endif /* CONFIG_WPS */
331
332
333 #ifdef CONFIG_CTRL_IFACE
334
335 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
336                             char *buf, size_t buflen)
337 {
338         if (wpa_s->ap_iface == NULL)
339                 return -1;
340         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
341                                             buf, buflen);
342 }
343
344
345 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
346                       char *buf, size_t buflen)
347 {
348         if (wpa_s->ap_iface == NULL)
349                 return -1;
350         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
351                                       buf, buflen);
352 }
353
354
355 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
356                            char *buf, size_t buflen)
357 {
358         if (wpa_s->ap_iface == NULL)
359                 return -1;
360         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
361                                            buf, buflen);
362 }
363
364
365 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
366                                  size_t buflen, int verbose)
367 {
368         char *pos = buf, *end = buf + buflen;
369         int ret;
370         struct hostapd_bss_config *conf;
371
372         if (wpa_s->ap_iface == NULL)
373                 return -1;
374
375         conf = wpa_s->ap_iface->bss[0]->conf;
376         if (conf->wpa == 0)
377                 return 0;
378
379         ret = os_snprintf(pos, end - pos,
380                           "pairwise_cipher=%s\n"
381                           "group_cipher=%s\n"
382                           "key_mgmt=%s\n",
383                           wpa_cipher_txt(conf->rsn_pairwise),
384                           wpa_cipher_txt(conf->wpa_group),
385                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
386                                            conf->wpa));
387         if (ret < 0 || ret >= end - pos)
388                 return pos - buf;
389         pos += ret;
390         return pos - buf;
391 }
392
393 #endif /* CONFIG_CTRL_IFACE */