Add definitions for new cipher suites from IEEE Std 802.11ac-2013
[mech_eap.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 software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9
10 #include "utils/includes.h"
11
12 #include "utils/common.h"
13 #include "utils/eloop.h"
14 #include "utils/uuid.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "eapol_supp/eapol_supp_sm.h"
18 #include "ap/hostapd.h"
19 #include "ap/ap_config.h"
20 #include "ap/ap_drv_ops.h"
21 #ifdef NEED_AP_MLME
22 #include "ap/ieee802_11.h"
23 #endif /* NEED_AP_MLME */
24 #include "ap/beacon.h"
25 #include "ap/ieee802_1x.h"
26 #include "ap/wps_hostapd.h"
27 #include "ap/ctrl_iface_ap.h"
28 #include "wps/wps.h"
29 #include "common/ieee802_11_defs.h"
30 #include "config_ssid.h"
31 #include "config.h"
32 #include "wpa_supplicant_i.h"
33 #include "driver_i.h"
34 #include "p2p_supplicant.h"
35 #include "ap.h"
36 #include "ap/sta_info.h"
37 #include "notify.h"
38
39
40 #ifdef CONFIG_WPS
41 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
42 #endif /* CONFIG_WPS */
43
44
45 #ifdef CONFIG_IEEE80211N
46 static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
47                              struct hostapd_config *conf,
48                              struct hostapd_hw_modes *mode)
49 {
50         u8 center_chan = 0;
51         u8 channel = conf->channel;
52
53         if (!conf->secondary_channel)
54                 goto no_vht;
55
56         center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
57         if (!center_chan)
58                 goto no_vht;
59
60         /* Use 80 MHz channel */
61         conf->vht_oper_chwidth = 1;
62         conf->vht_oper_centr_freq_seg0_idx = center_chan;
63         return;
64
65 no_vht:
66         conf->vht_oper_centr_freq_seg0_idx =
67                 channel + conf->secondary_channel * 2;
68 }
69 #endif /* CONFIG_IEEE80211N */
70
71
72 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
73                                   struct wpa_ssid *ssid,
74                                   struct hostapd_config *conf)
75 {
76         struct hostapd_bss_config *bss = conf->bss[0];
77
78         conf->driver = wpa_s->driver;
79
80         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
81
82         conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
83                                                &conf->channel);
84         if (conf->hw_mode == NUM_HOSTAPD_MODES) {
85                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
86                            ssid->frequency);
87                 return -1;
88         }
89
90         /* TODO: enable HT40 if driver supports it;
91          * drop to 11b if driver does not support 11g */
92
93 #ifdef CONFIG_IEEE80211N
94         /*
95          * Enable HT20 if the driver supports it, by setting conf->ieee80211n
96          * and a mask of allowed capabilities within conf->ht_capab.
97          * Using default config settings for: conf->ht_op_mode_fixed,
98          * conf->secondary_channel, conf->require_ht
99          */
100         if (wpa_s->hw.modes) {
101                 struct hostapd_hw_modes *mode = NULL;
102                 int i, no_ht = 0;
103                 for (i = 0; i < wpa_s->hw.num_modes; i++) {
104                         if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
105                                 mode = &wpa_s->hw.modes[i];
106                                 break;
107                         }
108                 }
109
110 #ifdef CONFIG_HT_OVERRIDES
111                 if (ssid->disable_ht) {
112                         conf->ieee80211n = 0;
113                         conf->ht_capab = 0;
114                         no_ht = 1;
115                 }
116 #endif /* CONFIG_HT_OVERRIDES */
117
118                 if (!no_ht && mode && mode->ht_capab) {
119                         conf->ieee80211n = 1;
120 #ifdef CONFIG_P2P
121                         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
122                             (mode->ht_capab &
123                              HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
124                             ssid->ht40)
125                                 conf->secondary_channel =
126                                         wpas_p2p_get_ht40_mode(wpa_s, mode,
127                                                                conf->channel);
128                         if (conf->secondary_channel)
129                                 conf->ht_capab |=
130                                         HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
131 #endif /* CONFIG_P2P */
132
133                         /*
134                          * white-list capabilities that won't cause issues
135                          * to connecting stations, while leaving the current
136                          * capabilities intact (currently disabled SMPS).
137                          */
138                         conf->ht_capab |= mode->ht_capab &
139                                 (HT_CAP_INFO_GREEN_FIELD |
140                                  HT_CAP_INFO_SHORT_GI20MHZ |
141                                  HT_CAP_INFO_SHORT_GI40MHZ |
142                                  HT_CAP_INFO_RX_STBC_MASK |
143                                  HT_CAP_INFO_MAX_AMSDU_SIZE);
144
145                         if (mode->vht_capab && ssid->vht) {
146                                 conf->ieee80211ac = 1;
147                                 wpas_conf_ap_vht(wpa_s, conf, mode);
148                         }
149                 }
150         }
151 #endif /* CONFIG_IEEE80211N */
152
153 #ifdef CONFIG_P2P
154         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
155             (ssid->mode == WPAS_MODE_P2P_GO ||
156              ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
157                 /* Remove 802.11b rates from supported and basic rate sets */
158                 int *list = os_malloc(4 * sizeof(int));
159                 if (list) {
160                         list[0] = 60;
161                         list[1] = 120;
162                         list[2] = 240;
163                         list[3] = -1;
164                 }
165                 conf->basic_rates = list;
166
167                 list = os_malloc(9 * sizeof(int));
168                 if (list) {
169                         list[0] = 60;
170                         list[1] = 90;
171                         list[2] = 120;
172                         list[3] = 180;
173                         list[4] = 240;
174                         list[5] = 360;
175                         list[6] = 480;
176                         list[7] = 540;
177                         list[8] = -1;
178                 }
179                 conf->supported_rates = list;
180         }
181
182         bss->isolate = !wpa_s->conf->p2p_intra_bss;
183         bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
184 #endif /* CONFIG_P2P */
185
186         if (ssid->ssid_len == 0) {
187                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
188                 return -1;
189         }
190         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
191         bss->ssid.ssid_len = ssid->ssid_len;
192         bss->ssid.ssid_set = 1;
193
194         bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
195
196         if (ssid->auth_alg)
197                 bss->auth_algs = ssid->auth_alg;
198
199         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
200                 bss->wpa = ssid->proto;
201         bss->wpa_key_mgmt = ssid->key_mgmt;
202         bss->wpa_pairwise = ssid->pairwise_cipher;
203         if (ssid->psk_set) {
204                 os_free(bss->ssid.wpa_psk);
205                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
206                 if (bss->ssid.wpa_psk == NULL)
207                         return -1;
208                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
209                 bss->ssid.wpa_psk->group = 1;
210         } else if (ssid->passphrase) {
211                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
212         } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
213                    ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
214                 struct hostapd_wep_keys *wep = &bss->ssid.wep;
215                 int i;
216                 for (i = 0; i < NUM_WEP_KEYS; i++) {
217                         if (ssid->wep_key_len[i] == 0)
218                                 continue;
219                         wep->key[i] = os_malloc(ssid->wep_key_len[i]);
220                         if (wep->key[i] == NULL)
221                                 return -1;
222                         os_memcpy(wep->key[i], ssid->wep_key[i],
223                                   ssid->wep_key_len[i]);
224                         wep->len[i] = ssid->wep_key_len[i];
225                 }
226                 wep->idx = ssid->wep_tx_keyidx;
227                 wep->keys_set = 1;
228         }
229
230         if (ssid->ap_max_inactivity)
231                 bss->ap_max_inactivity = ssid->ap_max_inactivity;
232
233         if (ssid->dtim_period)
234                 bss->dtim_period = ssid->dtim_period;
235         else if (wpa_s->conf->dtim_period)
236                 bss->dtim_period = wpa_s->conf->dtim_period;
237
238         if (ssid->beacon_int)
239                 conf->beacon_int = ssid->beacon_int;
240         else if (wpa_s->conf->beacon_int)
241                 conf->beacon_int = wpa_s->conf->beacon_int;
242
243         if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
244                 bss->rsn_pairwise = bss->wpa_pairwise;
245         bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
246                                                     bss->rsn_pairwise);
247
248         if (bss->wpa && bss->ieee802_1x)
249                 bss->ssid.security_policy = SECURITY_WPA;
250         else if (bss->wpa)
251                 bss->ssid.security_policy = SECURITY_WPA_PSK;
252         else if (bss->ieee802_1x) {
253                 int cipher = WPA_CIPHER_NONE;
254                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
255                 bss->ssid.wep.default_len = bss->default_wep_key_len;
256                 if (bss->default_wep_key_len)
257                         cipher = bss->default_wep_key_len >= 13 ?
258                                 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
259                 bss->wpa_group = cipher;
260                 bss->wpa_pairwise = cipher;
261                 bss->rsn_pairwise = cipher;
262         } else if (bss->ssid.wep.keys_set) {
263                 int cipher = WPA_CIPHER_WEP40;
264                 if (bss->ssid.wep.len[0] >= 13)
265                         cipher = WPA_CIPHER_WEP104;
266                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
267                 bss->wpa_group = cipher;
268                 bss->wpa_pairwise = cipher;
269                 bss->rsn_pairwise = cipher;
270         } else {
271                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
272                 bss->wpa_group = WPA_CIPHER_NONE;
273                 bss->wpa_pairwise = WPA_CIPHER_NONE;
274                 bss->rsn_pairwise = WPA_CIPHER_NONE;
275         }
276
277         if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
278             (bss->wpa_group == WPA_CIPHER_CCMP ||
279              bss->wpa_group == WPA_CIPHER_GCMP ||
280              bss->wpa_group == WPA_CIPHER_CCMP_256 ||
281              bss->wpa_group == WPA_CIPHER_GCMP_256)) {
282                 /*
283                  * Strong ciphers do not need frequent rekeying, so increase
284                  * the default GTK rekeying period to 24 hours.
285                  */
286                 bss->wpa_group_rekey = 86400;
287         }
288
289 #ifdef CONFIG_WPS
290         /*
291          * Enable WPS by default for open and WPA/WPA2-Personal network, but
292          * require user interaction to actually use it. Only the internal
293          * Registrar is supported.
294          */
295         if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
296             bss->ssid.security_policy != SECURITY_PLAINTEXT)
297                 goto no_wps;
298 #ifdef CONFIG_WPS2
299         if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
300             (!(bss->rsn_pairwise & WPA_CIPHER_CCMP) || !(bss->wpa & 2)))
301                 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
302                               * configuration */
303 #endif /* CONFIG_WPS2 */
304         bss->eap_server = 1;
305
306         if (!ssid->ignore_broadcast_ssid)
307                 bss->wps_state = 2;
308
309         bss->ap_setup_locked = 2;
310         if (wpa_s->conf->config_methods)
311                 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
312         os_memcpy(bss->device_type, wpa_s->conf->device_type,
313                   WPS_DEV_TYPE_LEN);
314         if (wpa_s->conf->device_name) {
315                 bss->device_name = os_strdup(wpa_s->conf->device_name);
316                 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
317         }
318         if (wpa_s->conf->manufacturer)
319                 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
320         if (wpa_s->conf->model_name)
321                 bss->model_name = os_strdup(wpa_s->conf->model_name);
322         if (wpa_s->conf->model_number)
323                 bss->model_number = os_strdup(wpa_s->conf->model_number);
324         if (wpa_s->conf->serial_number)
325                 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
326         if (is_nil_uuid(wpa_s->conf->uuid))
327                 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
328         else
329                 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
330         os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
331         bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
332 no_wps:
333 #endif /* CONFIG_WPS */
334
335         if (wpa_s->max_stations &&
336             wpa_s->max_stations < wpa_s->conf->max_num_sta)
337                 bss->max_num_sta = wpa_s->max_stations;
338         else
339                 bss->max_num_sta = wpa_s->conf->max_num_sta;
340
341         bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
342
343         if (wpa_s->conf->ap_vendor_elements) {
344                 bss->vendor_elements =
345                         wpabuf_dup(wpa_s->conf->ap_vendor_elements);
346         }
347
348         return 0;
349 }
350
351
352 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
353 {
354 #ifdef CONFIG_P2P
355         struct wpa_supplicant *wpa_s = ctx;
356         const struct ieee80211_mgmt *mgmt;
357         size_t hdr_len;
358
359         mgmt = (const struct ieee80211_mgmt *) buf;
360         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
361         if (hdr_len > len)
362                 return;
363         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
364                            mgmt->u.action.category,
365                            &mgmt->u.action.u.vs_public_action.action,
366                            len - hdr_len, freq);
367 #endif /* CONFIG_P2P */
368 }
369
370
371 static void ap_wps_event_cb(void *ctx, enum wps_event event,
372                             union wps_event_data *data)
373 {
374 #ifdef CONFIG_P2P
375         struct wpa_supplicant *wpa_s = ctx;
376
377         if (event == WPS_EV_FAIL) {
378                 struct wps_event_fail *fail = &data->fail;
379
380                 if (wpa_s->parent && wpa_s->parent != wpa_s &&
381                     wpa_s == wpa_s->global->p2p_group_formation) {
382                         /*
383                          * src/ap/wps_hostapd.c has already sent this on the
384                          * main interface, so only send on the parent interface
385                          * here if needed.
386                          */
387                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
388                                 "msg=%d config_error=%d",
389                                 fail->msg, fail->config_error);
390                 }
391                 wpas_p2p_wps_failed(wpa_s, fail);
392         }
393 #endif /* CONFIG_P2P */
394 }
395
396
397 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
398                                  int authorized, const u8 *p2p_dev_addr)
399 {
400         wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
401 }
402
403
404 #ifdef CONFIG_P2P
405 static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
406                           const u8 *psk, size_t psk_len)
407 {
408
409         struct wpa_supplicant *wpa_s = ctx;
410         if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
411                 return;
412         wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
413 }
414 #endif /* CONFIG_P2P */
415
416
417 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
418 {
419 #ifdef CONFIG_P2P
420         struct wpa_supplicant *wpa_s = ctx;
421         const struct ieee80211_mgmt *mgmt;
422         size_t hdr_len;
423
424         mgmt = (const struct ieee80211_mgmt *) buf;
425         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
426         if (hdr_len > len)
427                 return -1;
428         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
429                            mgmt->u.action.category,
430                            &mgmt->u.action.u.vs_public_action.action,
431                            len - hdr_len, freq);
432 #endif /* CONFIG_P2P */
433         return 0;
434 }
435
436
437 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
438                            const u8 *bssid, const u8 *ie, size_t ie_len,
439                            int ssi_signal)
440 {
441 #ifdef CONFIG_P2P
442         struct wpa_supplicant *wpa_s = ctx;
443         return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
444                                      ssi_signal);
445 #else /* CONFIG_P2P */
446         return 0;
447 #endif /* CONFIG_P2P */
448 }
449
450
451 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
452                                   const u8 *uuid_e)
453 {
454 #ifdef CONFIG_P2P
455         struct wpa_supplicant *wpa_s = ctx;
456         wpas_p2p_wps_success(wpa_s, mac_addr, 1);
457 #endif /* CONFIG_P2P */
458 }
459
460
461 static void wpas_ap_configured_cb(void *ctx)
462 {
463         struct wpa_supplicant *wpa_s = ctx;
464
465         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
466
467         if (wpa_s->ap_configured_cb)
468                 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
469                                         wpa_s->ap_configured_cb_data);
470 }
471
472
473 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
474                              struct wpa_ssid *ssid)
475 {
476         struct wpa_driver_associate_params params;
477         struct hostapd_iface *hapd_iface;
478         struct hostapd_config *conf;
479         size_t i;
480
481         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
482                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
483                 return -1;
484         }
485
486         wpa_supplicant_ap_deinit(wpa_s);
487
488         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
489                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
490
491         os_memset(&params, 0, sizeof(params));
492         params.ssid = ssid->ssid;
493         params.ssid_len = ssid->ssid_len;
494         switch (ssid->mode) {
495         case WPAS_MODE_INFRA:
496                 params.mode = IEEE80211_MODE_INFRA;
497                 break;
498         case WPAS_MODE_IBSS:
499                 params.mode = IEEE80211_MODE_IBSS;
500                 break;
501         case WPAS_MODE_AP:
502         case WPAS_MODE_P2P_GO:
503         case WPAS_MODE_P2P_GROUP_FORMATION:
504                 params.mode = IEEE80211_MODE_AP;
505                 break;
506         }
507         if (ssid->frequency == 0)
508                 ssid->frequency = 2462; /* default channel 11 */
509         params.freq = ssid->frequency;
510
511         params.wpa_proto = ssid->proto;
512         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
513                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
514         else
515                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
516         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
517
518         wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
519                                                           1);
520         if (wpa_s->pairwise_cipher < 0) {
521                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
522                            "cipher.");
523                 return -1;
524         }
525         params.pairwise_suite =
526                 wpa_cipher_to_suite_driver(wpa_s->pairwise_cipher);
527         params.group_suite = params.pairwise_suite;
528
529 #ifdef CONFIG_P2P
530         if (ssid->mode == WPAS_MODE_P2P_GO ||
531             ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
532                 params.p2p = 1;
533 #endif /* CONFIG_P2P */
534
535         if (wpa_s->parent->set_ap_uapsd)
536                 params.uapsd = wpa_s->parent->ap_uapsd;
537         else
538                 params.uapsd = -1;
539
540         if (wpa_drv_associate(wpa_s, &params) < 0) {
541                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
542                 return -1;
543         }
544
545         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
546         if (hapd_iface == NULL)
547                 return -1;
548         hapd_iface->owner = wpa_s;
549         hapd_iface->drv_flags = wpa_s->drv_flags;
550         hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
551         hapd_iface->extended_capa = wpa_s->extended_capa;
552         hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
553         hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
554
555         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
556         if (conf == NULL) {
557                 wpa_supplicant_ap_deinit(wpa_s);
558                 return -1;
559         }
560
561         os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
562                   wpa_s->conf->wmm_ac_params,
563                   sizeof(wpa_s->conf->wmm_ac_params));
564
565         if (params.uapsd > 0) {
566                 conf->bss[0]->wmm_enabled = 1;
567                 conf->bss[0]->wmm_uapsd = 1;
568         }
569
570         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
571                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
572                 wpa_supplicant_ap_deinit(wpa_s);
573                 return -1;
574         }
575
576 #ifdef CONFIG_P2P
577         if (ssid->mode == WPAS_MODE_P2P_GO)
578                 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
579         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
580                 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
581                         P2P_GROUP_FORMATION;
582 #endif /* CONFIG_P2P */
583
584         hapd_iface->num_bss = conf->num_bss;
585         hapd_iface->bss = os_calloc(conf->num_bss,
586                                     sizeof(struct hostapd_data *));
587         if (hapd_iface->bss == NULL) {
588                 wpa_supplicant_ap_deinit(wpa_s);
589                 return -1;
590         }
591
592         for (i = 0; i < conf->num_bss; i++) {
593                 hapd_iface->bss[i] =
594                         hostapd_alloc_bss_data(hapd_iface, conf,
595                                                conf->bss[i]);
596                 if (hapd_iface->bss[i] == NULL) {
597                         wpa_supplicant_ap_deinit(wpa_s);
598                         return -1;
599                 }
600
601                 hapd_iface->bss[i]->msg_ctx = wpa_s;
602                 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
603                 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
604                 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
605                 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
606                 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
607                 hostapd_register_probereq_cb(hapd_iface->bss[i],
608                                              ap_probe_req_rx, wpa_s);
609                 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
610                 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
611                 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
612                 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
613                 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
614                 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
615 #ifdef CONFIG_P2P
616                 hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
617                 hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
618                 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
619                 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
620                                                                     ssid);
621 #endif /* CONFIG_P2P */
622                 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
623                 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
624         }
625
626         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
627         hapd_iface->bss[0]->driver = wpa_s->driver;
628         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
629
630         wpa_s->current_ssid = ssid;
631         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
632         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
633         wpa_s->assoc_freq = ssid->frequency;
634
635         if (hostapd_setup_interface(wpa_s->ap_iface)) {
636                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
637                 wpa_supplicant_ap_deinit(wpa_s);
638                 return -1;
639         }
640
641         return 0;
642 }
643
644
645 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
646 {
647 #ifdef CONFIG_WPS
648         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
649 #endif /* CONFIG_WPS */
650
651         if (wpa_s->ap_iface == NULL)
652                 return;
653
654         wpa_s->current_ssid = NULL;
655         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
656         wpa_s->assoc_freq = 0;
657 #ifdef CONFIG_P2P
658         if (wpa_s->ap_iface->bss)
659                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
660         wpas_p2p_group_deinit(wpa_s);
661 #endif /* CONFIG_P2P */
662         hostapd_interface_deinit(wpa_s->ap_iface);
663         hostapd_interface_free(wpa_s->ap_iface);
664         wpa_s->ap_iface = NULL;
665         wpa_drv_deinit_ap(wpa_s);
666 }
667
668
669 void ap_tx_status(void *ctx, const u8 *addr,
670                   const u8 *buf, size_t len, int ack)
671 {
672 #ifdef NEED_AP_MLME
673         struct wpa_supplicant *wpa_s = ctx;
674         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
675 #endif /* NEED_AP_MLME */
676 }
677
678
679 void ap_eapol_tx_status(void *ctx, const u8 *dst,
680                         const u8 *data, size_t len, int ack)
681 {
682 #ifdef NEED_AP_MLME
683         struct wpa_supplicant *wpa_s = ctx;
684         hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
685 #endif /* NEED_AP_MLME */
686 }
687
688
689 void ap_client_poll_ok(void *ctx, const u8 *addr)
690 {
691 #ifdef NEED_AP_MLME
692         struct wpa_supplicant *wpa_s = ctx;
693         if (wpa_s->ap_iface)
694                 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
695 #endif /* NEED_AP_MLME */
696 }
697
698
699 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
700 {
701 #ifdef NEED_AP_MLME
702         struct wpa_supplicant *wpa_s = ctx;
703         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
704 #endif /* NEED_AP_MLME */
705 }
706
707
708 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
709 {
710 #ifdef NEED_AP_MLME
711         struct wpa_supplicant *wpa_s = ctx;
712         struct hostapd_frame_info fi;
713         os_memset(&fi, 0, sizeof(fi));
714         fi.datarate = rx_mgmt->datarate;
715         fi.ssi_signal = rx_mgmt->ssi_signal;
716         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
717                         rx_mgmt->frame_len, &fi);
718 #endif /* NEED_AP_MLME */
719 }
720
721
722 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
723 {
724 #ifdef NEED_AP_MLME
725         struct wpa_supplicant *wpa_s = ctx;
726         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
727 #endif /* NEED_AP_MLME */
728 }
729
730
731 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
732                                 const u8 *src_addr, const u8 *buf, size_t len)
733 {
734         ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
735 }
736
737
738 #ifdef CONFIG_WPS
739
740 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
741                               const u8 *p2p_dev_addr)
742 {
743         if (!wpa_s->ap_iface)
744                 return -1;
745         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
746                                          p2p_dev_addr);
747 }
748
749
750 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
751 {
752         struct wps_registrar *reg;
753         int reg_sel = 0, wps_sta = 0;
754
755         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
756                 return -1;
757
758         reg = wpa_s->ap_iface->bss[0]->wps->registrar;
759         reg_sel = wps_registrar_wps_cancel(reg);
760         wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
761                                   ap_sta_wps_cancel, NULL);
762
763         if (!reg_sel && !wps_sta) {
764                 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
765                            "time");
766                 return -1;
767         }
768
769         /*
770          * There are 2 cases to return wps cancel as success:
771          * 1. When wps cancel was initiated but no connection has been
772          *    established with client yet.
773          * 2. Client is in the middle of exchanging WPS messages.
774          */
775
776         return 0;
777 }
778
779
780 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
781                               const char *pin, char *buf, size_t buflen,
782                               int timeout)
783 {
784         int ret, ret_len = 0;
785
786         if (!wpa_s->ap_iface)
787                 return -1;
788
789         if (pin == NULL) {
790                 unsigned int rpin = wps_generate_pin();
791                 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
792                 pin = buf;
793         } else
794                 ret_len = os_snprintf(buf, buflen, "%s", pin);
795
796         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
797                                   timeout);
798         if (ret)
799                 return -1;
800         return ret_len;
801 }
802
803
804 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
805 {
806         struct wpa_supplicant *wpa_s = eloop_data;
807         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
808         wpas_wps_ap_pin_disable(wpa_s);
809 }
810
811
812 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
813 {
814         struct hostapd_data *hapd;
815
816         if (wpa_s->ap_iface == NULL)
817                 return;
818         hapd = wpa_s->ap_iface->bss[0];
819         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
820         hapd->ap_pin_failures = 0;
821         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
822         if (timeout > 0)
823                 eloop_register_timeout(timeout, 0,
824                                        wpas_wps_ap_pin_timeout, wpa_s, NULL);
825 }
826
827
828 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
829 {
830         struct hostapd_data *hapd;
831
832         if (wpa_s->ap_iface == NULL)
833                 return;
834         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
835         hapd = wpa_s->ap_iface->bss[0];
836         os_free(hapd->conf->ap_pin);
837         hapd->conf->ap_pin = NULL;
838         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
839 }
840
841
842 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
843 {
844         struct hostapd_data *hapd;
845         unsigned int pin;
846         char pin_txt[9];
847
848         if (wpa_s->ap_iface == NULL)
849                 return NULL;
850         hapd = wpa_s->ap_iface->bss[0];
851         pin = wps_generate_pin();
852         os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
853         os_free(hapd->conf->ap_pin);
854         hapd->conf->ap_pin = os_strdup(pin_txt);
855         if (hapd->conf->ap_pin == NULL)
856                 return NULL;
857         wpas_wps_ap_pin_enable(wpa_s, timeout);
858
859         return hapd->conf->ap_pin;
860 }
861
862
863 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
864 {
865         struct hostapd_data *hapd;
866         if (wpa_s->ap_iface == NULL)
867                 return NULL;
868         hapd = wpa_s->ap_iface->bss[0];
869         return hapd->conf->ap_pin;
870 }
871
872
873 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
874                         int timeout)
875 {
876         struct hostapd_data *hapd;
877         char pin_txt[9];
878         int ret;
879
880         if (wpa_s->ap_iface == NULL)
881                 return -1;
882         hapd = wpa_s->ap_iface->bss[0];
883         ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
884         if (ret < 0 || ret >= (int) sizeof(pin_txt))
885                 return -1;
886         os_free(hapd->conf->ap_pin);
887         hapd->conf->ap_pin = os_strdup(pin_txt);
888         if (hapd->conf->ap_pin == NULL)
889                 return -1;
890         wpas_wps_ap_pin_enable(wpa_s, timeout);
891
892         return 0;
893 }
894
895
896 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
897 {
898         struct hostapd_data *hapd;
899
900         if (wpa_s->ap_iface == NULL)
901                 return;
902         hapd = wpa_s->ap_iface->bss[0];
903
904         /*
905          * Registrar failed to prove its knowledge of the AP PIN. Disable AP
906          * PIN if this happens multiple times to slow down brute force attacks.
907          */
908         hapd->ap_pin_failures++;
909         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
910                    hapd->ap_pin_failures);
911         if (hapd->ap_pin_failures < 3)
912                 return;
913
914         wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
915         hapd->ap_pin_failures = 0;
916         os_free(hapd->conf->ap_pin);
917         hapd->conf->ap_pin = NULL;
918 }
919
920
921 #ifdef CONFIG_WPS_NFC
922
923 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
924                                              int ndef)
925 {
926         struct hostapd_data *hapd;
927
928         if (wpa_s->ap_iface == NULL)
929                 return NULL;
930         hapd = wpa_s->ap_iface->bss[0];
931         return hostapd_wps_nfc_config_token(hapd, ndef);
932 }
933
934
935 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
936                                              int ndef)
937 {
938         struct hostapd_data *hapd;
939
940         if (wpa_s->ap_iface == NULL)
941                 return NULL;
942         hapd = wpa_s->ap_iface->bss[0];
943         return hostapd_wps_nfc_hs_cr(hapd, ndef);
944 }
945
946 #endif /* CONFIG_WPS_NFC */
947
948 #endif /* CONFIG_WPS */
949
950
951 #ifdef CONFIG_CTRL_IFACE
952
953 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
954                             char *buf, size_t buflen)
955 {
956         if (wpa_s->ap_iface == NULL)
957                 return -1;
958         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
959                                             buf, buflen);
960 }
961
962
963 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
964                       char *buf, size_t buflen)
965 {
966         if (wpa_s->ap_iface == NULL)
967                 return -1;
968         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
969                                       buf, buflen);
970 }
971
972
973 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
974                            char *buf, size_t buflen)
975 {
976         if (wpa_s->ap_iface == NULL)
977                 return -1;
978         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
979                                            buf, buflen);
980 }
981
982
983 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
984                                    const char *txtaddr)
985 {
986         if (wpa_s->ap_iface == NULL)
987                 return -1;
988         return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
989                                                txtaddr);
990 }
991
992
993 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
994                                      const char *txtaddr)
995 {
996         if (wpa_s->ap_iface == NULL)
997                 return -1;
998         return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
999                                                  txtaddr);
1000 }
1001
1002
1003 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
1004                                  size_t buflen, int verbose)
1005 {
1006         char *pos = buf, *end = buf + buflen;
1007         int ret;
1008         struct hostapd_bss_config *conf;
1009
1010         if (wpa_s->ap_iface == NULL)
1011                 return -1;
1012
1013         conf = wpa_s->ap_iface->bss[0]->conf;
1014         if (conf->wpa == 0)
1015                 return 0;
1016
1017         ret = os_snprintf(pos, end - pos,
1018                           "pairwise_cipher=%s\n"
1019                           "group_cipher=%s\n"
1020                           "key_mgmt=%s\n",
1021                           wpa_cipher_txt(conf->rsn_pairwise),
1022                           wpa_cipher_txt(conf->wpa_group),
1023                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
1024                                            conf->wpa));
1025         if (ret < 0 || ret >= end - pos)
1026                 return pos - buf;
1027         pos += ret;
1028         return pos - buf;
1029 }
1030
1031 #endif /* CONFIG_CTRL_IFACE */
1032
1033
1034 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1035 {
1036         struct hostapd_iface *iface = wpa_s->ap_iface;
1037         struct wpa_ssid *ssid = wpa_s->current_ssid;
1038         struct hostapd_data *hapd;
1039
1040         if (ssid == NULL || wpa_s->ap_iface == NULL ||
1041             ssid->mode == WPAS_MODE_INFRA ||
1042             ssid->mode == WPAS_MODE_IBSS)
1043                 return -1;
1044
1045 #ifdef CONFIG_P2P
1046         if (ssid->mode == WPAS_MODE_P2P_GO)
1047                 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
1048         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1049                 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
1050                         P2P_GROUP_FORMATION;
1051 #endif /* CONFIG_P2P */
1052
1053         hapd = iface->bss[0];
1054         if (hapd->drv_priv == NULL)
1055                 return -1;
1056         ieee802_11_set_beacons(iface);
1057         hostapd_set_ap_wps_ie(hapd);
1058
1059         return 0;
1060 }
1061
1062
1063 int ap_switch_channel(struct wpa_supplicant *wpa_s,
1064                       struct csa_settings *settings)
1065 {
1066 #ifdef NEED_AP_MLME
1067         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1068                 return -1;
1069
1070         return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
1071 #else /* NEED_AP_MLME */
1072         return -1;
1073 #endif /* NEED_AP_MLME */
1074 }
1075
1076
1077 int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
1078 {
1079         struct csa_settings settings;
1080         int ret = hostapd_parse_csa_settings(pos, &settings);
1081
1082         if (ret)
1083                 return ret;
1084
1085         return ap_switch_channel(wpa_s, &settings);
1086 }
1087
1088
1089 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1090                        int offset, int width, int cf1, int cf2)
1091 {
1092         if (!wpa_s->ap_iface)
1093                 return;
1094
1095         wpa_s->assoc_freq = freq;
1096         hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset, width, cf1, cf1);
1097 }
1098
1099
1100 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1101                                       const u8 *addr)
1102 {
1103         struct hostapd_data *hapd;
1104         struct hostapd_bss_config *conf;
1105
1106         if (!wpa_s->ap_iface)
1107                 return -1;
1108
1109         if (addr)
1110                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1111                            MAC2STR(addr));
1112         else
1113                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1114
1115         hapd = wpa_s->ap_iface->bss[0];
1116         conf = hapd->conf;
1117
1118         os_free(conf->accept_mac);
1119         conf->accept_mac = NULL;
1120         conf->num_accept_mac = 0;
1121         os_free(conf->deny_mac);
1122         conf->deny_mac = NULL;
1123         conf->num_deny_mac = 0;
1124
1125         if (addr == NULL) {
1126                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1127                 return 0;
1128         }
1129
1130         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1131         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1132         if (conf->accept_mac == NULL)
1133                 return -1;
1134         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1135         conf->num_accept_mac = 1;
1136
1137         return 0;
1138 }