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