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