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