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