blob: 2ebc7f628bc9eb9a6890b3283ccfbf5519223c43 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
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 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
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"
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070017#include "eapol_supp/eapol_supp_sm.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080018#include "crypto/dh_group5.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "ap/hostapd.h"
20#include "ap/ap_config.h"
21#include "ap/ap_drv_ops.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080022#ifdef NEED_AP_MLME
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#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"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "wps/wps.h"
30#include "common/ieee802_11_defs.h"
31#include "config_ssid.h"
32#include "config.h"
33#include "wpa_supplicant_i.h"
34#include "driver_i.h"
35#include "p2p_supplicant.h"
36#include "ap.h"
37#include "ap/sta_info.h"
38#include "notify.h"
39
40
41#ifdef CONFIG_WPS
42static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
43#endif /* CONFIG_WPS */
44
45
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070046#ifdef CONFIG_IEEE80211N
47static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
48 struct hostapd_config *conf,
49 struct hostapd_hw_modes *mode)
50{
Dmitry Shmidt21de2142014-04-08 10:50:52 -070051#ifdef CONFIG_P2P
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070052 u8 center_chan = 0;
53 u8 channel = conf->channel;
54
55 if (!conf->secondary_channel)
56 goto no_vht;
57
58 center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
59 if (!center_chan)
60 goto no_vht;
61
62 /* Use 80 MHz channel */
63 conf->vht_oper_chwidth = 1;
64 conf->vht_oper_centr_freq_seg0_idx = center_chan;
65 return;
66
67no_vht:
68 conf->vht_oper_centr_freq_seg0_idx =
69 channel + conf->secondary_channel * 2;
Dmitry Shmidt21de2142014-04-08 10:50:52 -070070#else /* CONFIG_P2P */
71 conf->vht_oper_centr_freq_seg0_idx =
72 conf->channel + conf->secondary_channel * 2;
73#endif /* CONFIG_P2P */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -070074}
75#endif /* CONFIG_IEEE80211N */
76
77
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080078void wpa_supplicant_conf_ap_ht(struct wpa_supplicant *wpa_s,
79 struct wpa_ssid *ssid,
80 struct hostapd_config *conf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070082 /* TODO: enable HT40 if driver supports it;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083 * drop to 11b if driver does not support 11g */
84
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070085#ifdef CONFIG_IEEE80211N
86 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080087 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
88 * and a mask of allowed capabilities within conf->ht_capab.
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070089 * Using default config settings for: conf->ht_op_mode_fixed,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080090 * conf->secondary_channel, conf->require_ht
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070091 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080092 if (wpa_s->hw.modes) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070093 struct hostapd_hw_modes *mode = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -070094 int i, no_ht = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080095 for (i = 0; i < wpa_s->hw.num_modes; i++) {
96 if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
97 mode = &wpa_s->hw.modes[i];
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070098 break;
99 }
100 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700101
102#ifdef CONFIG_HT_OVERRIDES
103 if (ssid->disable_ht) {
104 conf->ieee80211n = 0;
105 conf->ht_capab = 0;
106 no_ht = 1;
107 }
108#endif /* CONFIG_HT_OVERRIDES */
109
110 if (!no_ht && mode && mode->ht_capab) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700111 conf->ieee80211n = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700112#ifdef CONFIG_P2P
113 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
114 (mode->ht_capab &
115 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
116 ssid->ht40)
117 conf->secondary_channel =
118 wpas_p2p_get_ht40_mode(wpa_s, mode,
119 conf->channel);
120 if (conf->secondary_channel)
121 conf->ht_capab |=
122 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
123#endif /* CONFIG_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800124
125 /*
126 * white-list capabilities that won't cause issues
127 * to connecting stations, while leaving the current
128 * capabilities intact (currently disabled SMPS).
129 */
130 conf->ht_capab |= mode->ht_capab &
131 (HT_CAP_INFO_GREEN_FIELD |
132 HT_CAP_INFO_SHORT_GI20MHZ |
133 HT_CAP_INFO_SHORT_GI40MHZ |
134 HT_CAP_INFO_RX_STBC_MASK |
135 HT_CAP_INFO_MAX_AMSDU_SIZE);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700136
137 if (mode->vht_capab && ssid->vht) {
138 conf->ieee80211ac = 1;
139 wpas_conf_ap_vht(wpa_s, conf, mode);
140 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800141 }
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700142 }
143#endif /* CONFIG_IEEE80211N */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800144}
145
146
147static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
148 struct wpa_ssid *ssid,
149 struct hostapd_config *conf)
150{
151 struct hostapd_bss_config *bss = conf->bss[0];
152
153 conf->driver = wpa_s->driver;
154
155 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
156
157 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
158 &conf->channel);
159 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
160 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
161 ssid->frequency);
162 return -1;
163 }
164
165 wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167#ifdef CONFIG_P2P
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700168 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
169 (ssid->mode == WPAS_MODE_P2P_GO ||
170 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171 /* Remove 802.11b rates from supported and basic rate sets */
172 int *list = os_malloc(4 * sizeof(int));
173 if (list) {
174 list[0] = 60;
175 list[1] = 120;
176 list[2] = 240;
177 list[3] = -1;
178 }
179 conf->basic_rates = list;
180
181 list = os_malloc(9 * sizeof(int));
182 if (list) {
183 list[0] = 60;
184 list[1] = 90;
185 list[2] = 120;
186 list[3] = 180;
187 list[4] = 240;
188 list[5] = 360;
189 list[6] = 480;
190 list[7] = 540;
191 list[8] = -1;
192 }
193 conf->supported_rates = list;
194 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800195
196 bss->isolate = !wpa_s->conf->p2p_intra_bss;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700197 bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800198
199 if (ssid->p2p_group) {
200 os_memcpy(bss->ip_addr_go, wpa_s->parent->conf->ip_addr_go, 4);
201 os_memcpy(bss->ip_addr_mask, wpa_s->parent->conf->ip_addr_mask,
202 4);
203 os_memcpy(bss->ip_addr_start,
204 wpa_s->parent->conf->ip_addr_start, 4);
205 os_memcpy(bss->ip_addr_end, wpa_s->parent->conf->ip_addr_end,
206 4);
207 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208#endif /* CONFIG_P2P */
209
210 if (ssid->ssid_len == 0) {
211 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
212 return -1;
213 }
214 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 bss->ssid.ssid_len = ssid->ssid_len;
216 bss->ssid.ssid_set = 1;
217
Dmitry Shmidt04949592012-07-19 12:16:46 -0700218 bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
219
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800220 if (ssid->auth_alg)
221 bss->auth_algs = ssid->auth_alg;
222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
224 bss->wpa = ssid->proto;
225 bss->wpa_key_mgmt = ssid->key_mgmt;
226 bss->wpa_pairwise = ssid->pairwise_cipher;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800227 if (ssid->psk_set) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800228 bin_clear_free(bss->ssid.wpa_psk, sizeof(*bss->ssid.wpa_psk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
230 if (bss->ssid.wpa_psk == NULL)
231 return -1;
232 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
233 bss->ssid.wpa_psk->group = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800234 } else if (ssid->passphrase) {
235 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800236 } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
237 ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
238 struct hostapd_wep_keys *wep = &bss->ssid.wep;
239 int i;
240 for (i = 0; i < NUM_WEP_KEYS; i++) {
241 if (ssid->wep_key_len[i] == 0)
242 continue;
243 wep->key[i] = os_malloc(ssid->wep_key_len[i]);
244 if (wep->key[i] == NULL)
245 return -1;
246 os_memcpy(wep->key[i], ssid->wep_key[i],
247 ssid->wep_key_len[i]);
248 wep->len[i] = ssid->wep_key_len[i];
249 }
250 wep->idx = ssid->wep_tx_keyidx;
251 wep->keys_set = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252 }
253
Dmitry Shmidt04949592012-07-19 12:16:46 -0700254 if (ssid->ap_max_inactivity)
255 bss->ap_max_inactivity = ssid->ap_max_inactivity;
256
257 if (ssid->dtim_period)
258 bss->dtim_period = ssid->dtim_period;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800259 else if (wpa_s->conf->dtim_period)
260 bss->dtim_period = wpa_s->conf->dtim_period;
261
262 if (ssid->beacon_int)
263 conf->beacon_int = ssid->beacon_int;
264 else if (wpa_s->conf->beacon_int)
265 conf->beacon_int = wpa_s->conf->beacon_int;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700266
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800267 if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
268 bss->rsn_pairwise = bss->wpa_pairwise;
269 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
270 bss->rsn_pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271
272 if (bss->wpa && bss->ieee802_1x)
273 bss->ssid.security_policy = SECURITY_WPA;
274 else if (bss->wpa)
275 bss->ssid.security_policy = SECURITY_WPA_PSK;
276 else if (bss->ieee802_1x) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800277 int cipher = WPA_CIPHER_NONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
279 bss->ssid.wep.default_len = bss->default_wep_key_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800280 if (bss->default_wep_key_len)
281 cipher = bss->default_wep_key_len >= 13 ?
282 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
283 bss->wpa_group = cipher;
284 bss->wpa_pairwise = cipher;
285 bss->rsn_pairwise = cipher;
286 } else if (bss->ssid.wep.keys_set) {
287 int cipher = WPA_CIPHER_WEP40;
288 if (bss->ssid.wep.len[0] >= 13)
289 cipher = WPA_CIPHER_WEP104;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 bss->ssid.security_policy = SECURITY_STATIC_WEP;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 bss->wpa_group = cipher;
292 bss->wpa_pairwise = cipher;
293 bss->rsn_pairwise = cipher;
294 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 bss->ssid.security_policy = SECURITY_PLAINTEXT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800296 bss->wpa_group = WPA_CIPHER_NONE;
297 bss->wpa_pairwise = WPA_CIPHER_NONE;
298 bss->rsn_pairwise = WPA_CIPHER_NONE;
299 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700301 if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
302 (bss->wpa_group == WPA_CIPHER_CCMP ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800303 bss->wpa_group == WPA_CIPHER_GCMP ||
304 bss->wpa_group == WPA_CIPHER_CCMP_256 ||
305 bss->wpa_group == WPA_CIPHER_GCMP_256)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700306 /*
307 * Strong ciphers do not need frequent rekeying, so increase
308 * the default GTK rekeying period to 24 hours.
309 */
310 bss->wpa_group_rekey = 86400;
311 }
312
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700313#ifdef CONFIG_IEEE80211W
314 if (ssid->ieee80211w != MGMT_FRAME_PROTECTION_DEFAULT)
315 bss->ieee80211w = ssid->ieee80211w;
316#endif /* CONFIG_IEEE80211W */
317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318#ifdef CONFIG_WPS
319 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320 * Enable WPS by default for open and WPA/WPA2-Personal network, but
321 * require user interaction to actually use it. Only the internal
322 * Registrar is supported.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324 if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
325 bss->ssid.security_policy != SECURITY_PLAINTEXT)
326 goto no_wps;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800327 if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800328 (!(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ||
329 !(bss->wpa & 2)))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
331 * configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332 bss->eap_server = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700333
334 if (!ssid->ignore_broadcast_ssid)
335 bss->wps_state = 2;
336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 bss->ap_setup_locked = 2;
338 if (wpa_s->conf->config_methods)
339 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
340 os_memcpy(bss->device_type, wpa_s->conf->device_type,
341 WPS_DEV_TYPE_LEN);
342 if (wpa_s->conf->device_name) {
343 bss->device_name = os_strdup(wpa_s->conf->device_name);
344 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
345 }
346 if (wpa_s->conf->manufacturer)
347 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
348 if (wpa_s->conf->model_name)
349 bss->model_name = os_strdup(wpa_s->conf->model_name);
350 if (wpa_s->conf->model_number)
351 bss->model_number = os_strdup(wpa_s->conf->model_number);
352 if (wpa_s->conf->serial_number)
353 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
354 if (is_nil_uuid(wpa_s->conf->uuid))
355 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
356 else
357 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
358 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700359 bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360no_wps:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361#endif /* CONFIG_WPS */
362
363 if (wpa_s->max_stations &&
364 wpa_s->max_stations < wpa_s->conf->max_num_sta)
365 bss->max_num_sta = wpa_s->max_stations;
366 else
367 bss->max_num_sta = wpa_s->conf->max_num_sta;
368
369 bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
370
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -0700371 if (wpa_s->conf->ap_vendor_elements) {
372 bss->vendor_elements =
373 wpabuf_dup(wpa_s->conf->ap_vendor_elements);
374 }
375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 return 0;
377}
378
379
380static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
381{
382#ifdef CONFIG_P2P
383 struct wpa_supplicant *wpa_s = ctx;
384 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385
386 mgmt = (const struct ieee80211_mgmt *) buf;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700387 if (len < IEEE80211_HDRLEN + 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 return;
Dmitry Shmidt18463232014-01-24 12:29:41 -0800389 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
390 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
392 mgmt->u.action.category,
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700393 buf + IEEE80211_HDRLEN + 1,
394 len - IEEE80211_HDRLEN - 1, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395#endif /* CONFIG_P2P */
396}
397
398
399static void ap_wps_event_cb(void *ctx, enum wps_event event,
400 union wps_event_data *data)
401{
402#ifdef CONFIG_P2P
403 struct wpa_supplicant *wpa_s = ctx;
404
Jouni Malinen75ecf522011-06-27 15:19:46 -0700405 if (event == WPS_EV_FAIL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 struct wps_event_fail *fail = &data->fail;
407
Jouni Malinen75ecf522011-06-27 15:19:46 -0700408 if (wpa_s->parent && wpa_s->parent != wpa_s &&
409 wpa_s == wpa_s->global->p2p_group_formation) {
410 /*
411 * src/ap/wps_hostapd.c has already sent this on the
412 * main interface, so only send on the parent interface
413 * here if needed.
414 */
415 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
416 "msg=%d config_error=%d",
417 fail->msg, fail->config_error);
418 }
419 wpas_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 }
421#endif /* CONFIG_P2P */
422}
423
424
425static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800426 int authorized, const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800428 wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429}
430
431
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700432#ifdef CONFIG_P2P
433static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
434 const u8 *psk, size_t psk_len)
435{
436
437 struct wpa_supplicant *wpa_s = ctx;
438 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
439 return;
440 wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
441}
442#endif /* CONFIG_P2P */
443
444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
446{
447#ifdef CONFIG_P2P
448 struct wpa_supplicant *wpa_s = ctx;
449 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450
451 mgmt = (const struct ieee80211_mgmt *) buf;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700452 if (len < IEEE80211_HDRLEN + 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 return -1;
454 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
455 mgmt->u.action.category,
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700456 buf + IEEE80211_HDRLEN + 1,
457 len - IEEE80211_HDRLEN - 1, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458#endif /* CONFIG_P2P */
459 return 0;
460}
461
462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800463static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700464 const u8 *bssid, const u8 *ie, size_t ie_len,
465 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700468 return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
469 ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470}
471
472
473static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
474 const u8 *uuid_e)
475{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 struct wpa_supplicant *wpa_s = ctx;
477 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478}
479
480
481static void wpas_ap_configured_cb(void *ctx)
482{
483 struct wpa_supplicant *wpa_s = ctx;
484
485 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
486
487 if (wpa_s->ap_configured_cb)
488 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
489 wpa_s->ap_configured_cb_data);
490}
491
492
493int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
494 struct wpa_ssid *ssid)
495{
496 struct wpa_driver_associate_params params;
497 struct hostapd_iface *hapd_iface;
498 struct hostapd_config *conf;
499 size_t i;
500
501 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
502 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
503 return -1;
504 }
505
506 wpa_supplicant_ap_deinit(wpa_s);
507
508 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
509 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
510
511 os_memset(&params, 0, sizeof(params));
512 params.ssid = ssid->ssid;
513 params.ssid_len = ssid->ssid_len;
514 switch (ssid->mode) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 case WPAS_MODE_AP:
516 case WPAS_MODE_P2P_GO:
517 case WPAS_MODE_P2P_GROUP_FORMATION:
518 params.mode = IEEE80211_MODE_AP;
519 break;
Dmitry Shmidt3c479372014-02-04 10:50:36 -0800520 default:
521 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522 }
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700523 if (ssid->frequency == 0)
524 ssid->frequency = 2462; /* default channel 11 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700525 params.freq.freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800527 params.wpa_proto = ssid->proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
529 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
530 else
531 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800532 params.key_mgmt_suite = wpa_s->key_mgmt;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800534 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
535 1);
536 if (wpa_s->pairwise_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
538 "cipher.");
539 return -1;
540 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800541 params.pairwise_suite = wpa_s->pairwise_cipher;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 params.group_suite = params.pairwise_suite;
543
544#ifdef CONFIG_P2P
545 if (ssid->mode == WPAS_MODE_P2P_GO ||
546 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
547 params.p2p = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548#endif /* CONFIG_P2P */
549
550 if (wpa_s->parent->set_ap_uapsd)
551 params.uapsd = wpa_s->parent->ap_uapsd;
Dmitry Shmidtec58b162014-02-19 12:44:18 -0800552 else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
553 params.uapsd = 1; /* mandatory for P2P GO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 else
555 params.uapsd = -1;
556
557 if (wpa_drv_associate(wpa_s, &params) < 0) {
558 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
559 return -1;
560 }
561
562 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
563 if (hapd_iface == NULL)
564 return -1;
565 hapd_iface->owner = wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800566 hapd_iface->drv_flags = wpa_s->drv_flags;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800567 hapd_iface->smps_modes = wpa_s->drv_smps_modes;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800568 hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700569 hapd_iface->extended_capa = wpa_s->extended_capa;
570 hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
571 hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572
573 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
574 if (conf == NULL) {
575 wpa_supplicant_ap_deinit(wpa_s);
576 return -1;
577 }
578
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700579 os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
580 wpa_s->conf->wmm_ac_params,
581 sizeof(wpa_s->conf->wmm_ac_params));
582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583 if (params.uapsd > 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800584 conf->bss[0]->wmm_enabled = 1;
585 conf->bss[0]->wmm_uapsd = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800586 }
587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
589 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
590 wpa_supplicant_ap_deinit(wpa_s);
591 return -1;
592 }
593
594#ifdef CONFIG_P2P
595 if (ssid->mode == WPAS_MODE_P2P_GO)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800596 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800598 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 P2P_GROUP_FORMATION;
600#endif /* CONFIG_P2P */
601
602 hapd_iface->num_bss = conf->num_bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700603 hapd_iface->bss = os_calloc(conf->num_bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 sizeof(struct hostapd_data *));
605 if (hapd_iface->bss == NULL) {
606 wpa_supplicant_ap_deinit(wpa_s);
607 return -1;
608 }
609
610 for (i = 0; i < conf->num_bss; i++) {
611 hapd_iface->bss[i] =
612 hostapd_alloc_bss_data(hapd_iface, conf,
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800613 conf->bss[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 if (hapd_iface->bss[i] == NULL) {
615 wpa_supplicant_ap_deinit(wpa_s);
616 return -1;
617 }
618
619 hapd_iface->bss[i]->msg_ctx = wpa_s;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700620 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
622 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
623 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
624 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
625 hostapd_register_probereq_cb(hapd_iface->bss[i],
626 ap_probe_req_rx, wpa_s);
627 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
628 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
629 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
630 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
631 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
632 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
633#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700634 hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
635 hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700637 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
638 ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639#endif /* CONFIG_P2P */
640 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
641 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800642#ifdef CONFIG_TESTING_OPTIONS
643 hapd_iface->bss[i]->ext_eapol_frame_io =
644 wpa_s->ext_eapol_frame_io;
645#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 }
647
648 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
649 hapd_iface->bss[0]->driver = wpa_s->driver;
650 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
651
652 wpa_s->current_ssid = ssid;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700653 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
655 wpa_s->assoc_freq = ssid->frequency;
656
657 if (hostapd_setup_interface(wpa_s->ap_iface)) {
658 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
659 wpa_supplicant_ap_deinit(wpa_s);
660 return -1;
661 }
662
663 return 0;
664}
665
666
667void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
668{
669#ifdef CONFIG_WPS
670 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
671#endif /* CONFIG_WPS */
672
673 if (wpa_s->ap_iface == NULL)
674 return;
675
676 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700677 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 wpa_s->assoc_freq = 0;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700679 wpas_p2p_ap_deinit(wpa_s);
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800680 wpa_s->ap_iface->driver_ap_teardown =
681 !!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 hostapd_interface_deinit(wpa_s->ap_iface);
684 hostapd_interface_free(wpa_s->ap_iface);
685 wpa_s->ap_iface = NULL;
686 wpa_drv_deinit_ap(wpa_s);
687}
688
689
690void ap_tx_status(void *ctx, const u8 *addr,
691 const u8 *buf, size_t len, int ack)
692{
693#ifdef NEED_AP_MLME
694 struct wpa_supplicant *wpa_s = ctx;
695 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
696#endif /* NEED_AP_MLME */
697}
698
699
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800700void ap_eapol_tx_status(void *ctx, const u8 *dst,
701 const u8 *data, size_t len, int ack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702{
703#ifdef NEED_AP_MLME
704 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800705 if (!wpa_s->ap_iface)
706 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800707 hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
708#endif /* NEED_AP_MLME */
709}
710
711
712void ap_client_poll_ok(void *ctx, const u8 *addr)
713{
714#ifdef NEED_AP_MLME
715 struct wpa_supplicant *wpa_s = ctx;
716 if (wpa_s->ap_iface)
717 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
718#endif /* NEED_AP_MLME */
719}
720
721
722void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
723{
724#ifdef NEED_AP_MLME
725 struct wpa_supplicant *wpa_s = ctx;
726 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727#endif /* NEED_AP_MLME */
728}
729
730
731void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
732{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800733#ifdef NEED_AP_MLME
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 struct wpa_supplicant *wpa_s = ctx;
735 struct hostapd_frame_info fi;
736 os_memset(&fi, 0, sizeof(fi));
737 fi.datarate = rx_mgmt->datarate;
738 fi.ssi_signal = rx_mgmt->ssi_signal;
739 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
740 rx_mgmt->frame_len, &fi);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742}
743
744
745void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
746{
747#ifdef NEED_AP_MLME
748 struct wpa_supplicant *wpa_s = ctx;
749 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
750#endif /* NEED_AP_MLME */
751}
752
753
754void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
755 const u8 *src_addr, const u8 *buf, size_t len)
756{
757 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
758}
759
760
761#ifdef CONFIG_WPS
762
763int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
764 const u8 *p2p_dev_addr)
765{
766 if (!wpa_s->ap_iface)
767 return -1;
768 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
769 p2p_dev_addr);
770}
771
772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
774{
775 struct wps_registrar *reg;
776 int reg_sel = 0, wps_sta = 0;
777
778 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
779 return -1;
780
781 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
782 reg_sel = wps_registrar_wps_cancel(reg);
783 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
Dmitry Shmidt04949592012-07-19 12:16:46 -0700784 ap_sta_wps_cancel, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785
786 if (!reg_sel && !wps_sta) {
787 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
788 "time");
789 return -1;
790 }
791
792 /*
793 * There are 2 cases to return wps cancel as success:
794 * 1. When wps cancel was initiated but no connection has been
795 * established with client yet.
796 * 2. Client is in the middle of exchanging WPS messages.
797 */
798
799 return 0;
800}
801
802
803int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800804 const char *pin, char *buf, size_t buflen,
805 int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806{
807 int ret, ret_len = 0;
808
809 if (!wpa_s->ap_iface)
810 return -1;
811
812 if (pin == NULL) {
813 unsigned int rpin = wps_generate_pin();
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800814 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800815 if (os_snprintf_error(buflen, ret_len))
816 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 pin = buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800818 } else if (buf) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 ret_len = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800820 if (os_snprintf_error(buflen, ret_len))
821 return -1;
822 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823
824 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800825 timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 if (ret)
827 return -1;
828 return ret_len;
829}
830
831
832static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
833{
834 struct wpa_supplicant *wpa_s = eloop_data;
835 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
836 wpas_wps_ap_pin_disable(wpa_s);
837}
838
839
840static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
841{
842 struct hostapd_data *hapd;
843
844 if (wpa_s->ap_iface == NULL)
845 return;
846 hapd = wpa_s->ap_iface->bss[0];
847 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
848 hapd->ap_pin_failures = 0;
849 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
850 if (timeout > 0)
851 eloop_register_timeout(timeout, 0,
852 wpas_wps_ap_pin_timeout, wpa_s, NULL);
853}
854
855
856void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
857{
858 struct hostapd_data *hapd;
859
860 if (wpa_s->ap_iface == NULL)
861 return;
862 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
863 hapd = wpa_s->ap_iface->bss[0];
864 os_free(hapd->conf->ap_pin);
865 hapd->conf->ap_pin = NULL;
866 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
867}
868
869
870const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
871{
872 struct hostapd_data *hapd;
873 unsigned int pin;
874 char pin_txt[9];
875
876 if (wpa_s->ap_iface == NULL)
877 return NULL;
878 hapd = wpa_s->ap_iface->bss[0];
879 pin = wps_generate_pin();
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800880 os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 os_free(hapd->conf->ap_pin);
882 hapd->conf->ap_pin = os_strdup(pin_txt);
883 if (hapd->conf->ap_pin == NULL)
884 return NULL;
885 wpas_wps_ap_pin_enable(wpa_s, timeout);
886
887 return hapd->conf->ap_pin;
888}
889
890
891const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
892{
893 struct hostapd_data *hapd;
894 if (wpa_s->ap_iface == NULL)
895 return NULL;
896 hapd = wpa_s->ap_iface->bss[0];
897 return hapd->conf->ap_pin;
898}
899
900
901int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
902 int timeout)
903{
904 struct hostapd_data *hapd;
905 char pin_txt[9];
906 int ret;
907
908 if (wpa_s->ap_iface == NULL)
909 return -1;
910 hapd = wpa_s->ap_iface->bss[0];
911 ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800912 if (os_snprintf_error(sizeof(pin_txt), ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913 return -1;
914 os_free(hapd->conf->ap_pin);
915 hapd->conf->ap_pin = os_strdup(pin_txt);
916 if (hapd->conf->ap_pin == NULL)
917 return -1;
918 wpas_wps_ap_pin_enable(wpa_s, timeout);
919
920 return 0;
921}
922
923
924void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
925{
926 struct hostapd_data *hapd;
927
928 if (wpa_s->ap_iface == NULL)
929 return;
930 hapd = wpa_s->ap_iface->bss[0];
931
932 /*
933 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
934 * PIN if this happens multiple times to slow down brute force attacks.
935 */
936 hapd->ap_pin_failures++;
937 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
938 hapd->ap_pin_failures);
939 if (hapd->ap_pin_failures < 3)
940 return;
941
942 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
943 hapd->ap_pin_failures = 0;
944 os_free(hapd->conf->ap_pin);
945 hapd->conf->ap_pin = NULL;
946}
947
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800948
949#ifdef CONFIG_WPS_NFC
950
951struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
952 int ndef)
953{
954 struct hostapd_data *hapd;
955
956 if (wpa_s->ap_iface == NULL)
957 return NULL;
958 hapd = wpa_s->ap_iface->bss[0];
959 return hostapd_wps_nfc_config_token(hapd, ndef);
960}
961
962
963struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
964 int ndef)
965{
966 struct hostapd_data *hapd;
967
968 if (wpa_s->ap_iface == NULL)
969 return NULL;
970 hapd = wpa_s->ap_iface->bss[0];
971 return hostapd_wps_nfc_hs_cr(hapd, ndef);
972}
973
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800974
975int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
976 const struct wpabuf *req,
977 const struct wpabuf *sel)
978{
979 struct hostapd_data *hapd;
980
981 if (wpa_s->ap_iface == NULL)
982 return -1;
983 hapd = wpa_s->ap_iface->bss[0];
984 return hostapd_wps_nfc_report_handover(hapd, req, sel);
985}
986
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800987#endif /* CONFIG_WPS_NFC */
988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989#endif /* CONFIG_WPS */
990
991
992#ifdef CONFIG_CTRL_IFACE
993
994int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
995 char *buf, size_t buflen)
996{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800997 struct hostapd_data *hapd;
998
999 if (wpa_s->ap_iface)
1000 hapd = wpa_s->ap_iface->bss[0];
1001 else if (wpa_s->ifmsh)
1002 hapd = wpa_s->ifmsh->bss[0];
1003 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001005 return hostapd_ctrl_iface_sta_first(hapd, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006}
1007
1008
1009int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
1010 char *buf, size_t buflen)
1011{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001012 struct hostapd_data *hapd;
1013
1014 if (wpa_s->ap_iface)
1015 hapd = wpa_s->ap_iface->bss[0];
1016 else if (wpa_s->ifmsh)
1017 hapd = wpa_s->ifmsh->bss[0];
1018 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001020 return hostapd_ctrl_iface_sta(hapd, txtaddr, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021}
1022
1023
1024int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
1025 char *buf, size_t buflen)
1026{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001027 struct hostapd_data *hapd;
1028
1029 if (wpa_s->ap_iface)
1030 hapd = wpa_s->ap_iface->bss[0];
1031 else if (wpa_s->ifmsh)
1032 hapd = wpa_s->ifmsh->bss[0];
1033 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001035 return hostapd_ctrl_iface_sta_next(hapd, txtaddr, buf, buflen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036}
1037
1038
Dmitry Shmidt04949592012-07-19 12:16:46 -07001039int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
1040 const char *txtaddr)
1041{
1042 if (wpa_s->ap_iface == NULL)
1043 return -1;
1044 return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
1045 txtaddr);
1046}
1047
1048
1049int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
1050 const char *txtaddr)
1051{
1052 if (wpa_s->ap_iface == NULL)
1053 return -1;
1054 return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
1055 txtaddr);
1056}
1057
1058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
1060 size_t buflen, int verbose)
1061{
1062 char *pos = buf, *end = buf + buflen;
1063 int ret;
1064 struct hostapd_bss_config *conf;
1065
1066 if (wpa_s->ap_iface == NULL)
1067 return -1;
1068
1069 conf = wpa_s->ap_iface->bss[0]->conf;
1070 if (conf->wpa == 0)
1071 return 0;
1072
1073 ret = os_snprintf(pos, end - pos,
1074 "pairwise_cipher=%s\n"
1075 "group_cipher=%s\n"
1076 "key_mgmt=%s\n",
1077 wpa_cipher_txt(conf->rsn_pairwise),
1078 wpa_cipher_txt(conf->wpa_group),
1079 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
1080 conf->wpa));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001081 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 return pos - buf;
1083 pos += ret;
1084 return pos - buf;
1085}
1086
1087#endif /* CONFIG_CTRL_IFACE */
1088
1089
1090int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1091{
1092 struct hostapd_iface *iface = wpa_s->ap_iface;
1093 struct wpa_ssid *ssid = wpa_s->current_ssid;
1094 struct hostapd_data *hapd;
1095
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001096 if (ssid == NULL || wpa_s->ap_iface == NULL ||
1097 ssid->mode == WPAS_MODE_INFRA ||
1098 ssid->mode == WPAS_MODE_IBSS)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 return -1;
1100
1101#ifdef CONFIG_P2P
1102 if (ssid->mode == WPAS_MODE_P2P_GO)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001103 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001105 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 P2P_GROUP_FORMATION;
1107#endif /* CONFIG_P2P */
1108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 hapd = iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001110 if (hapd->drv_priv == NULL)
1111 return -1;
1112 ieee802_11_set_beacons(iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 hostapd_set_ap_wps_ie(hapd);
1114
1115 return 0;
1116}
1117
1118
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001119int ap_switch_channel(struct wpa_supplicant *wpa_s,
1120 struct csa_settings *settings)
1121{
1122#ifdef NEED_AP_MLME
1123 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1124 return -1;
1125
1126 return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
1127#else /* NEED_AP_MLME */
1128 return -1;
1129#endif /* NEED_AP_MLME */
1130}
1131
1132
1133int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
1134{
1135 struct csa_settings settings;
1136 int ret = hostapd_parse_csa_settings(pos, &settings);
1137
1138 if (ret)
1139 return ret;
1140
1141 return ap_switch_channel(wpa_s, &settings);
1142}
1143
1144
Dmitry Shmidt04949592012-07-19 12:16:46 -07001145void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001146 int offset, int width, int cf1, int cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001147{
1148 if (!wpa_s->ap_iface)
1149 return;
1150
1151 wpa_s->assoc_freq = freq;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001152 hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset, width, cf1, cf1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001153}
1154
1155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1157 const u8 *addr)
1158{
1159 struct hostapd_data *hapd;
1160 struct hostapd_bss_config *conf;
1161
1162 if (!wpa_s->ap_iface)
1163 return -1;
1164
1165 if (addr)
1166 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1167 MAC2STR(addr));
1168 else
1169 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1170
1171 hapd = wpa_s->ap_iface->bss[0];
1172 conf = hapd->conf;
1173
1174 os_free(conf->accept_mac);
1175 conf->accept_mac = NULL;
1176 conf->num_accept_mac = 0;
1177 os_free(conf->deny_mac);
1178 conf->deny_mac = NULL;
1179 conf->num_deny_mac = 0;
1180
1181 if (addr == NULL) {
1182 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1183 return 0;
1184 }
1185
1186 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1187 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1188 if (conf->accept_mac == NULL)
1189 return -1;
1190 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1191 conf->num_accept_mac = 1;
1192
1193 return 0;
1194}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001195
1196
1197#ifdef CONFIG_WPS_NFC
1198int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
1199 const struct wpabuf *pw, const u8 *pubkey_hash)
1200{
1201 struct hostapd_data *hapd;
1202 struct wps_context *wps;
1203
1204 if (!wpa_s->ap_iface)
1205 return -1;
1206 hapd = wpa_s->ap_iface->bss[0];
1207 wps = hapd->wps;
1208
1209 if (wpa_s->parent->conf->wps_nfc_dh_pubkey == NULL ||
1210 wpa_s->parent->conf->wps_nfc_dh_privkey == NULL) {
1211 wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
1212 return -1;
1213 }
1214
1215 dh5_free(wps->dh_ctx);
1216 wpabuf_free(wps->dh_pubkey);
1217 wpabuf_free(wps->dh_privkey);
1218 wps->dh_privkey = wpabuf_dup(
1219 wpa_s->parent->conf->wps_nfc_dh_privkey);
1220 wps->dh_pubkey = wpabuf_dup(
1221 wpa_s->parent->conf->wps_nfc_dh_pubkey);
1222 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
1223 wps->dh_ctx = NULL;
1224 wpabuf_free(wps->dh_pubkey);
1225 wps->dh_pubkey = NULL;
1226 wpabuf_free(wps->dh_privkey);
1227 wps->dh_privkey = NULL;
1228 return -1;
1229 }
1230 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
1231 if (wps->dh_ctx == NULL)
1232 return -1;
1233
1234 return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
1235 pw_id,
1236 pw ? wpabuf_head(pw) : NULL,
1237 pw ? wpabuf_len(pw) : 0, 1);
1238}
1239#endif /* CONFIG_WPS_NFC */