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