Convert remaining SSID routines from char* to u8*
[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         if (params.uapsd > 0) {
494                 conf->bss->wmm_enabled = 1;
495                 conf->bss->wmm_uapsd = 1;
496         }
497
498         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
499                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
500                 wpa_supplicant_ap_deinit(wpa_s);
501                 return -1;
502         }
503
504 #ifdef CONFIG_P2P
505         if (ssid->mode == WPAS_MODE_P2P_GO)
506                 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
507         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
508                 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
509                         P2P_GROUP_FORMATION;
510 #endif /* CONFIG_P2P */
511
512         hapd_iface->num_bss = conf->num_bss;
513         hapd_iface->bss = os_zalloc(conf->num_bss *
514                                     sizeof(struct hostapd_data *));
515         if (hapd_iface->bss == NULL) {
516                 wpa_supplicant_ap_deinit(wpa_s);
517                 return -1;
518         }
519
520         for (i = 0; i < conf->num_bss; i++) {
521                 hapd_iface->bss[i] =
522                         hostapd_alloc_bss_data(hapd_iface, conf,
523                                                &conf->bss[i]);
524                 if (hapd_iface->bss[i] == NULL) {
525                         wpa_supplicant_ap_deinit(wpa_s);
526                         return -1;
527                 }
528
529                 hapd_iface->bss[i]->msg_ctx = wpa_s;
530                 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
531                 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
532                 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
533                 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
534                 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
535                 hostapd_register_probereq_cb(hapd_iface->bss[i],
536                                              ap_probe_req_rx, wpa_s);
537                 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
538                 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
539                 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
540                 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
541                 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
542                 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
543 #ifdef CONFIG_P2P
544                 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
545                 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
546                                                                     ssid);
547 #endif /* CONFIG_P2P */
548                 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
549                 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
550         }
551
552         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
553         hapd_iface->bss[0]->driver = wpa_s->driver;
554         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
555
556         wpa_s->current_ssid = ssid;
557         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
558         wpa_s->assoc_freq = ssid->frequency;
559
560         if (hostapd_setup_interface(wpa_s->ap_iface)) {
561                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
562                 wpa_supplicant_ap_deinit(wpa_s);
563                 return -1;
564         }
565
566         return 0;
567 }
568
569
570 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
571 {
572 #ifdef CONFIG_WPS
573         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
574 #endif /* CONFIG_WPS */
575
576         if (wpa_s->ap_iface == NULL)
577                 return;
578
579         wpa_s->current_ssid = NULL;
580         wpa_s->assoc_freq = 0;
581         wpa_s->reassociated_connection = 0;
582 #ifdef CONFIG_P2P
583         if (wpa_s->ap_iface->bss)
584                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
585         wpas_p2p_group_deinit(wpa_s);
586 #endif /* CONFIG_P2P */
587         hostapd_interface_deinit(wpa_s->ap_iface);
588         hostapd_interface_free(wpa_s->ap_iface);
589         wpa_s->ap_iface = NULL;
590         wpa_drv_deinit_ap(wpa_s);
591 }
592
593
594 void ap_tx_status(void *ctx, const u8 *addr,
595                   const u8 *buf, size_t len, int ack)
596 {
597 #ifdef NEED_AP_MLME
598         struct wpa_supplicant *wpa_s = ctx;
599         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
600 #endif /* NEED_AP_MLME */
601 }
602
603
604 void ap_eapol_tx_status(void *ctx, const u8 *dst,
605                         const u8 *data, size_t len, int ack)
606 {
607 #ifdef NEED_AP_MLME
608         struct wpa_supplicant *wpa_s = ctx;
609         hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
610 #endif /* NEED_AP_MLME */
611 }
612
613
614 void ap_client_poll_ok(void *ctx, const u8 *addr)
615 {
616 #ifdef NEED_AP_MLME
617         struct wpa_supplicant *wpa_s = ctx;
618         if (wpa_s->ap_iface)
619                 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
620 #endif /* NEED_AP_MLME */
621 }
622
623
624 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
625 {
626 #ifdef NEED_AP_MLME
627         struct wpa_supplicant *wpa_s = ctx;
628         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
629 #endif /* NEED_AP_MLME */
630 }
631
632
633 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
634 {
635 #ifdef NEED_AP_MLME
636         struct wpa_supplicant *wpa_s = ctx;
637         struct hostapd_frame_info fi;
638         os_memset(&fi, 0, sizeof(fi));
639         fi.datarate = rx_mgmt->datarate;
640         fi.ssi_signal = rx_mgmt->ssi_signal;
641         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
642                         rx_mgmt->frame_len, &fi);
643 #endif /* NEED_AP_MLME */
644 }
645
646
647 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
648 {
649 #ifdef NEED_AP_MLME
650         struct wpa_supplicant *wpa_s = ctx;
651         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
652 #endif /* NEED_AP_MLME */
653 }
654
655
656 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
657                                 const u8 *src_addr, const u8 *buf, size_t len)
658 {
659         ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
660 }
661
662
663 #ifdef CONFIG_WPS
664
665 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
666                               const u8 *p2p_dev_addr)
667 {
668         if (!wpa_s->ap_iface)
669                 return -1;
670         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
671                                          p2p_dev_addr);
672 }
673
674
675 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
676 {
677         struct wps_registrar *reg;
678         int reg_sel = 0, wps_sta = 0;
679
680         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
681                 return -1;
682
683         reg = wpa_s->ap_iface->bss[0]->wps->registrar;
684         reg_sel = wps_registrar_wps_cancel(reg);
685         wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
686                                   ap_sta_wps_cancel, NULL);
687
688         if (!reg_sel && !wps_sta) {
689                 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
690                            "time");
691                 return -1;
692         }
693
694         /*
695          * There are 2 cases to return wps cancel as success:
696          * 1. When wps cancel was initiated but no connection has been
697          *    established with client yet.
698          * 2. Client is in the middle of exchanging WPS messages.
699          */
700
701         return 0;
702 }
703
704
705 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
706                               const char *pin, char *buf, size_t buflen)
707 {
708         int ret, ret_len = 0;
709
710         if (!wpa_s->ap_iface)
711                 return -1;
712
713         if (pin == NULL) {
714                 unsigned int rpin = wps_generate_pin();
715                 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
716                 pin = buf;
717         } else
718                 ret_len = os_snprintf(buf, buflen, "%s", pin);
719
720         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
721                                   0);
722         if (ret)
723                 return -1;
724         return ret_len;
725 }
726
727
728 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
729 {
730         struct wpa_supplicant *wpa_s = eloop_data;
731         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
732         wpas_wps_ap_pin_disable(wpa_s);
733 }
734
735
736 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
737 {
738         struct hostapd_data *hapd;
739
740         if (wpa_s->ap_iface == NULL)
741                 return;
742         hapd = wpa_s->ap_iface->bss[0];
743         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
744         hapd->ap_pin_failures = 0;
745         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
746         if (timeout > 0)
747                 eloop_register_timeout(timeout, 0,
748                                        wpas_wps_ap_pin_timeout, wpa_s, NULL);
749 }
750
751
752 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
753 {
754         struct hostapd_data *hapd;
755
756         if (wpa_s->ap_iface == NULL)
757                 return;
758         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
759         hapd = wpa_s->ap_iface->bss[0];
760         os_free(hapd->conf->ap_pin);
761         hapd->conf->ap_pin = NULL;
762         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
763 }
764
765
766 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
767 {
768         struct hostapd_data *hapd;
769         unsigned int pin;
770         char pin_txt[9];
771
772         if (wpa_s->ap_iface == NULL)
773                 return NULL;
774         hapd = wpa_s->ap_iface->bss[0];
775         pin = wps_generate_pin();
776         os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
777         os_free(hapd->conf->ap_pin);
778         hapd->conf->ap_pin = os_strdup(pin_txt);
779         if (hapd->conf->ap_pin == NULL)
780                 return NULL;
781         wpas_wps_ap_pin_enable(wpa_s, timeout);
782
783         return hapd->conf->ap_pin;
784 }
785
786
787 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
788 {
789         struct hostapd_data *hapd;
790         if (wpa_s->ap_iface == NULL)
791                 return NULL;
792         hapd = wpa_s->ap_iface->bss[0];
793         return hapd->conf->ap_pin;
794 }
795
796
797 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
798                         int timeout)
799 {
800         struct hostapd_data *hapd;
801         char pin_txt[9];
802         int ret;
803
804         if (wpa_s->ap_iface == NULL)
805                 return -1;
806         hapd = wpa_s->ap_iface->bss[0];
807         ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
808         if (ret < 0 || ret >= (int) sizeof(pin_txt))
809                 return -1;
810         os_free(hapd->conf->ap_pin);
811         hapd->conf->ap_pin = os_strdup(pin_txt);
812         if (hapd->conf->ap_pin == NULL)
813                 return -1;
814         wpas_wps_ap_pin_enable(wpa_s, timeout);
815
816         return 0;
817 }
818
819
820 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
821 {
822         struct hostapd_data *hapd;
823
824         if (wpa_s->ap_iface == NULL)
825                 return;
826         hapd = wpa_s->ap_iface->bss[0];
827
828         /*
829          * Registrar failed to prove its knowledge of the AP PIN. Disable AP
830          * PIN if this happens multiple times to slow down brute force attacks.
831          */
832         hapd->ap_pin_failures++;
833         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
834                    hapd->ap_pin_failures);
835         if (hapd->ap_pin_failures < 3)
836                 return;
837
838         wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
839         hapd->ap_pin_failures = 0;
840         os_free(hapd->conf->ap_pin);
841         hapd->conf->ap_pin = NULL;
842 }
843
844 #endif /* CONFIG_WPS */
845
846
847 #ifdef CONFIG_CTRL_IFACE
848
849 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
850                             char *buf, size_t buflen)
851 {
852         if (wpa_s->ap_iface == NULL)
853                 return -1;
854         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
855                                             buf, buflen);
856 }
857
858
859 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
860                       char *buf, size_t buflen)
861 {
862         if (wpa_s->ap_iface == NULL)
863                 return -1;
864         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
865                                       buf, buflen);
866 }
867
868
869 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
870                            char *buf, size_t buflen)
871 {
872         if (wpa_s->ap_iface == NULL)
873                 return -1;
874         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
875                                            buf, buflen);
876 }
877
878
879 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
880                                    const char *txtaddr)
881 {
882         if (wpa_s->ap_iface == NULL)
883                 return -1;
884         return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
885                                                txtaddr);
886 }
887
888
889 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
890                                      const char *txtaddr)
891 {
892         if (wpa_s->ap_iface == NULL)
893                 return -1;
894         return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
895                                                  txtaddr);
896 }
897
898
899 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
900                                  size_t buflen, int verbose)
901 {
902         char *pos = buf, *end = buf + buflen;
903         int ret;
904         struct hostapd_bss_config *conf;
905
906         if (wpa_s->ap_iface == NULL)
907                 return -1;
908
909         conf = wpa_s->ap_iface->bss[0]->conf;
910         if (conf->wpa == 0)
911                 return 0;
912
913         ret = os_snprintf(pos, end - pos,
914                           "pairwise_cipher=%s\n"
915                           "group_cipher=%s\n"
916                           "key_mgmt=%s\n",
917                           wpa_cipher_txt(conf->rsn_pairwise),
918                           wpa_cipher_txt(conf->wpa_group),
919                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
920                                            conf->wpa));
921         if (ret < 0 || ret >= end - pos)
922                 return pos - buf;
923         pos += ret;
924         return pos - buf;
925 }
926
927 #endif /* CONFIG_CTRL_IFACE */
928
929
930 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
931 {
932         struct hostapd_iface *iface = wpa_s->ap_iface;
933         struct wpa_ssid *ssid = wpa_s->current_ssid;
934         struct hostapd_data *hapd;
935
936         if (ssid == NULL || wpa_s->ap_iface == NULL ||
937             ssid->mode == WPAS_MODE_INFRA ||
938             ssid->mode == WPAS_MODE_IBSS)
939                 return -1;
940
941 #ifdef CONFIG_P2P
942         if (ssid->mode == WPAS_MODE_P2P_GO)
943                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
944         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
945                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
946                         P2P_GROUP_FORMATION;
947 #endif /* CONFIG_P2P */
948
949         hapd = iface->bss[0];
950         if (hapd->drv_priv == NULL)
951                 return -1;
952         ieee802_11_set_beacons(iface);
953         hostapd_set_ap_wps_ie(hapd);
954
955         return 0;
956 }
957
958
959 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
960                        int offset)
961 {
962         if (!wpa_s->ap_iface)
963                 return;
964
965         wpa_s->assoc_freq = freq;
966         hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset);
967 }
968
969
970 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
971                                       const u8 *addr)
972 {
973         struct hostapd_data *hapd;
974         struct hostapd_bss_config *conf;
975
976         if (!wpa_s->ap_iface)
977                 return -1;
978
979         if (addr)
980                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
981                            MAC2STR(addr));
982         else
983                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
984
985         hapd = wpa_s->ap_iface->bss[0];
986         conf = hapd->conf;
987
988         os_free(conf->accept_mac);
989         conf->accept_mac = NULL;
990         conf->num_accept_mac = 0;
991         os_free(conf->deny_mac);
992         conf->deny_mac = NULL;
993         conf->num_deny_mac = 0;
994
995         if (addr == NULL) {
996                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
997                 return 0;
998         }
999
1000         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1001         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1002         if (conf->accept_mac == NULL)
1003                 return -1;
1004         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1005         conf->num_accept_mac = 1;
1006
1007         return 0;
1008 }