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