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