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