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