P2P: Add initial version of P2P Module
[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 "common/ieee802_11_defs.h"
20 #include "ap/hostapd.h"
21 #include "ap/ap_config.h"
22 #ifdef NEED_AP_MLME
23 #include "ap/ieee802_11.h"
24 #endif /* NEED_AP_MLME */
25 #include "ap/beacon.h"
26 #include "ap/ieee802_1x.h"
27 #include "ap/wps_hostapd.h"
28 #include "ap/ctrl_iface_ap.h"
29 #include "eap_common/eap_defs.h"
30 #include "eap_server/eap_methods.h"
31 #include "eap_common/eap_wsc_common.h"
32 #include "wps/wps.h"
33 #include "config_ssid.h"
34 #include "config.h"
35 #include "wpa_supplicant_i.h"
36 #include "driver_i.h"
37 #include "ap.h"
38
39
40 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
41                                   struct wpa_ssid *ssid,
42                                   struct hostapd_config *conf)
43 {
44         struct hostapd_bss_config *bss = &conf->bss[0];
45         int pairwise;
46
47         conf->driver = wpa_s->driver;
48
49         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
50
51         if (ssid->frequency == 0) {
52                 /* default channel 11 */
53                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
54                 conf->channel = 11;
55         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
56                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
57                 conf->channel = (ssid->frequency - 2407) / 5;
58         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
59                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
60                 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
61                 conf->channel = (ssid->frequency - 5000) / 5;
62         } else {
63                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
64                            ssid->frequency);
65                 return -1;
66         }
67
68         /* TODO: enable HT if driver supports it;
69          * drop to 11b if driver does not support 11g */
70
71 #ifdef CONFIG_P2P
72         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
73                 /* Remove 802.11b rates from supported and basic rate sets */
74                 int *list = os_malloc(4 * sizeof(int));
75                 if (list) {
76                         list[0] = 60;
77                         list[1] = 120;
78                         list[2] = 240;
79                         list[3] = -1;
80                 }
81                 conf->basic_rates = list;
82
83                 list = os_malloc(9 * sizeof(int));
84                 if (list) {
85                         list[0] = 60;
86                         list[1] = 90;
87                         list[2] = 120;
88                         list[3] = 180;
89                         list[4] = 240;
90                         list[5] = 360;
91                         list[6] = 480;
92                         list[7] = 540;
93                         list[8] = -1;
94                 }
95                 conf->supported_rates = list;
96         }
97 #endif /* CONFIG_P2P */
98
99         if (ssid->ssid_len == 0) {
100                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
101                 return -1;
102         }
103         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
104         bss->ssid.ssid[ssid->ssid_len] = '\0';
105         bss->ssid.ssid_len = ssid->ssid_len;
106         bss->ssid.ssid_set = 1;
107
108         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
109                 bss->wpa = ssid->proto;
110         bss->wpa_key_mgmt = ssid->key_mgmt;
111         bss->wpa_pairwise = ssid->pairwise_cipher;
112         if (ssid->passphrase) {
113                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
114         } else if (ssid->psk_set) {
115                 os_free(bss->ssid.wpa_psk);
116                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
117                 if (bss->ssid.wpa_psk == NULL)
118                         return -1;
119                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
120                 bss->ssid.wpa_psk->group = 1;
121         }
122
123         /* Select group cipher based on the enabled pairwise cipher suites */
124         pairwise = 0;
125         if (bss->wpa & 1)
126                 pairwise |= bss->wpa_pairwise;
127         if (bss->wpa & 2) {
128                 if (bss->rsn_pairwise == 0)
129                         bss->rsn_pairwise = bss->wpa_pairwise;
130                 pairwise |= bss->rsn_pairwise;
131         }
132         if (pairwise & WPA_CIPHER_TKIP)
133                 bss->wpa_group = WPA_CIPHER_TKIP;
134         else
135                 bss->wpa_group = WPA_CIPHER_CCMP;
136
137         if (bss->wpa && bss->ieee802_1x)
138                 bss->ssid.security_policy = SECURITY_WPA;
139         else if (bss->wpa)
140                 bss->ssid.security_policy = SECURITY_WPA_PSK;
141         else if (bss->ieee802_1x) {
142                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
143                 bss->ssid.wep.default_len = bss->default_wep_key_len;
144         } else if (bss->ssid.wep.keys_set)
145                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
146         else
147                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
148
149 #ifdef CONFIG_WPS
150         /*
151          * Enable WPS by default, but require user interaction to actually use
152          * it. Only the internal Registrar is supported.
153          */
154         bss->eap_server = 1;
155         bss->wps_state = 2;
156         bss->ap_setup_locked = 1;
157         if (wpa_s->conf->config_methods)
158                 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
159         if (wpa_s->conf->device_type)
160                 bss->device_type = os_strdup(wpa_s->conf->device_type);
161 #endif /* CONFIG_WPS */
162
163         return 0;
164 }
165
166
167 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
168 {
169 }
170
171
172 static int ap_probe_req_rx(void *ctx, const u8 *addr, const u8 *ie,
173                            size_t ie_len)
174 {
175         return 0;
176 }
177
178
179 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
180                                   const u8 *uuid_e)
181 {
182 }
183
184
185 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
186                              struct wpa_ssid *ssid)
187 {
188         struct wpa_driver_associate_params params;
189         struct hostapd_iface *hapd_iface;
190         struct hostapd_config *conf;
191         size_t i;
192
193         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
194                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
195                 return -1;
196         }
197
198         wpa_supplicant_ap_deinit(wpa_s);
199
200         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
201                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
202
203         os_memset(&params, 0, sizeof(params));
204         params.ssid = ssid->ssid;
205         params.ssid_len = ssid->ssid_len;
206         switch (ssid->mode) {
207         case WPAS_MODE_INFRA:
208                 params.mode = IEEE80211_MODE_INFRA;
209                 break;
210         case WPAS_MODE_IBSS:
211                 params.mode = IEEE80211_MODE_IBSS;
212                 break;
213         case WPAS_MODE_AP:
214         case WPAS_MODE_P2P_GO:
215         case WPAS_MODE_P2P_GROUP_FORMATION:
216                 params.mode = IEEE80211_MODE_AP;
217                 break;
218         }
219         params.freq = ssid->frequency;
220
221         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
222                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
223         else
224                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
225         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
226
227         if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
228                 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
229         else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
230                 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
231         else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
232                 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
233         else {
234                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
235                            "cipher.");
236                 return -1;
237         }
238         params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
239         params.group_suite = params.pairwise_suite;
240
241         if (wpa_drv_associate(wpa_s, &params) < 0) {
242                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
243                 return -1;
244         }
245
246         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
247         if (hapd_iface == NULL)
248                 return -1;
249         hapd_iface->owner = wpa_s;
250
251         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
252         if (conf == NULL) {
253                 wpa_supplicant_ap_deinit(wpa_s);
254                 return -1;
255         }
256
257         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
258                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
259                 wpa_supplicant_ap_deinit(wpa_s);
260                 return -1;
261         }
262
263         hapd_iface->num_bss = conf->num_bss;
264         hapd_iface->bss = os_zalloc(conf->num_bss *
265                                     sizeof(struct hostapd_data *));
266         if (hapd_iface->bss == NULL) {
267                 wpa_supplicant_ap_deinit(wpa_s);
268                 return -1;
269         }
270
271         for (i = 0; i < conf->num_bss; i++) {
272                 hapd_iface->bss[i] =
273                         hostapd_alloc_bss_data(hapd_iface, conf,
274                                                &conf->bss[i]);
275                 if (hapd_iface->bss[i] == NULL) {
276                         wpa_supplicant_ap_deinit(wpa_s);
277                         return -1;
278                 }
279
280                 hapd_iface->bss[i]->msg_ctx = wpa_s;
281                 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
282                 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
283                 hostapd_register_probereq_cb(hapd_iface->bss[i],
284                                              ap_probe_req_rx, wpa_s);
285                 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
286                 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
287         }
288
289         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
290         hapd_iface->bss[0]->driver = wpa_s->driver;
291         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
292
293         if (hostapd_setup_interface(wpa_s->ap_iface)) {
294                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
295                 wpa_supplicant_ap_deinit(wpa_s);
296                 return -1;
297         }
298
299         wpa_s->current_ssid = ssid;
300         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
301         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
302
303         if (wpa_s->ap_configured_cb)
304                 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
305                                         wpa_s->ap_configured_cb_data);
306
307         return 0;
308 }
309
310
311 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
312 {
313         if (wpa_s->ap_iface == NULL)
314                 return;
315
316         wpa_s->current_ssid = NULL;
317         hostapd_interface_deinit(wpa_s->ap_iface);
318         hostapd_interface_free(wpa_s->ap_iface);
319         wpa_s->ap_iface = NULL;
320         wpa_drv_deinit_ap(wpa_s);
321 }
322
323
324 void ap_tx_status(void *ctx, const u8 *addr,
325                   const u8 *buf, size_t len, int ack)
326 {
327 #ifdef NEED_AP_MLME
328         struct wpa_supplicant *wpa_s = ctx;
329         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
330 #endif /* NEED_AP_MLME */
331 }
332
333
334 void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
335 {
336 #ifdef NEED_AP_MLME
337         struct wpa_supplicant *wpa_s = ctx;
338         const struct ieee80211_hdr *hdr =
339                 (const struct ieee80211_hdr *) frame;
340         u16 fc = le_to_host16(hdr->frame_control);
341         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
342                                    (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
343                                    (WLAN_FC_TODS | WLAN_FC_FROMDS));
344 #endif /* NEED_AP_MLME */
345 }
346
347
348 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
349 {
350 #ifdef NEED_AP_MLME
351         struct wpa_supplicant *wpa_s = ctx;
352         struct hostapd_frame_info fi;
353         os_memset(&fi, 0, sizeof(fi));
354         fi.datarate = rx_mgmt->datarate;
355         fi.ssi_signal = rx_mgmt->ssi_signal;
356         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
357                         rx_mgmt->frame_len, &fi);
358 #endif /* NEED_AP_MLME */
359 }
360
361
362 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
363 {
364 #ifdef NEED_AP_MLME
365         struct wpa_supplicant *wpa_s = ctx;
366         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
367 #endif /* NEED_AP_MLME */
368 }
369
370
371 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
372                                 const u8 *src_addr, const u8 *buf, size_t len)
373 {
374         ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
375 }
376
377
378 #ifdef CONFIG_WPS
379
380 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
381 {
382         if (!wpa_s->ap_iface)
383                 return -1;
384         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
385 }
386
387
388 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
389                               const char *pin, char *buf, size_t buflen)
390 {
391         int ret, ret_len = 0;
392
393         if (!wpa_s->ap_iface)
394                 return -1;
395
396         if (pin == NULL) {
397                 unsigned int rpin = wps_generate_pin();
398                 ret_len = os_snprintf(buf, buflen, "%d", rpin);
399                 pin = buf;
400         }
401
402         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
403                                   0);
404         if (ret)
405                 return -1;
406         return ret_len;
407 }
408
409 #endif /* CONFIG_WPS */
410
411
412 #ifdef CONFIG_CTRL_IFACE
413
414 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
415                             char *buf, size_t buflen)
416 {
417         if (wpa_s->ap_iface == NULL)
418                 return -1;
419         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
420                                             buf, buflen);
421 }
422
423
424 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
425                       char *buf, size_t buflen)
426 {
427         if (wpa_s->ap_iface == NULL)
428                 return -1;
429         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
430                                       buf, buflen);
431 }
432
433
434 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
435                            char *buf, size_t buflen)
436 {
437         if (wpa_s->ap_iface == NULL)
438                 return -1;
439         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
440                                            buf, buflen);
441 }
442
443
444 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
445                                  size_t buflen, int verbose)
446 {
447         char *pos = buf, *end = buf + buflen;
448         int ret;
449         struct hostapd_bss_config *conf;
450
451         if (wpa_s->ap_iface == NULL)
452                 return -1;
453
454         conf = wpa_s->ap_iface->bss[0]->conf;
455         if (conf->wpa == 0)
456                 return 0;
457
458         ret = os_snprintf(pos, end - pos,
459                           "pairwise_cipher=%s\n"
460                           "group_cipher=%s\n"
461                           "key_mgmt=%s\n",
462                           wpa_cipher_txt(conf->rsn_pairwise),
463                           wpa_cipher_txt(conf->wpa_group),
464                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
465                                            conf->wpa));
466         if (ret < 0 || ret >= end - pos)
467                 return pos - buf;
468         pos += ret;
469         return pos - buf;
470 }
471
472 #endif /* CONFIG_CTRL_IFACE */
473
474
475 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
476 {
477         struct hostapd_iface *iface = wpa_s->ap_iface;
478         struct wpa_ssid *ssid = wpa_s->current_ssid;
479         struct hostapd_data *hapd;
480
481         if (ssid == NULL || wpa_s->ap_iface == NULL)
482                 return -1;
483
484         ieee802_11_set_beacons(iface);
485         hapd = iface->bss[0];
486         hapd->drv.set_ap_wps_ie(hapd);
487
488         return 0;
489 }
490
491
492 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
493                                       const u8 *addr)
494 {
495         struct hostapd_data *hapd;
496         struct hostapd_bss_config *conf;
497
498         if (!wpa_s->ap_iface)
499                 return -1;
500
501         if (addr)
502                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
503                            MAC2STR(addr));
504         else
505                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
506
507         hapd = wpa_s->ap_iface->bss[0];
508         conf = hapd->conf;
509
510         os_free(conf->accept_mac);
511         conf->accept_mac = NULL;
512         conf->num_accept_mac = 0;
513         os_free(conf->deny_mac);
514         conf->deny_mac = NULL;
515         conf->num_deny_mac = 0;
516
517         if (addr == NULL) {
518                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
519                 return 0;
520         }
521
522         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
523         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
524         if (conf->accept_mac == NULL)
525                 return -1;
526         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
527         conf->num_accept_mac = 1;
528
529         return 0;
530 }