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