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