Fix hostapd wpa_msg() calls ctx for wpa_supplicant AP mode
[libeap.git] / wpa_supplicant / ap.c
1 /*
2  * WPA Supplicant - Basic AP mode support routines
3  * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2009, Atheros Communications
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17
18 #include "common.h"
19 #include "../hostapd/hostapd.h"
20 #include "../hostapd/config.h"
21 #ifdef NEED_AP_MLME
22 #include "../hostapd/ieee802_11.h"
23 #endif /* NEED_AP_MLME */
24 #include "../hostapd/wps_hostapd.h"
25 #include "../hostapd/ctrl_iface_ap.h"
26 #include "eap_common/eap_defs.h"
27 #include "eap_server/eap_methods.h"
28 #include "eap_common/eap_wsc_common.h"
29 #include "wps/wps.h"
30 #include "config_ssid.h"
31 #include "wpa_supplicant_i.h"
32 #include "driver_i.h"
33 #include "ap.h"
34
35
36 int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
37                                          void *ctx), void *ctx)
38 {
39         /* TODO */
40         return 0;
41 }
42
43
44 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
45 {
46         return 0;
47 }
48
49
50 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
51 {
52 }
53
54
55 struct ap_driver_data {
56         struct hostapd_data *hapd;
57 };
58
59
60 static void * ap_driver_init(struct hostapd_data *hapd,
61                              struct wpa_init_params *params)
62 {
63         struct ap_driver_data *drv;
64         struct wpa_supplicant *wpa_s = hapd->iface->owner;
65
66         drv = os_zalloc(sizeof(struct ap_driver_data));
67         if (drv == NULL) {
68                 wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
69                            "driver data");
70                 return NULL;
71         }
72         drv->hapd = hapd;
73         os_memcpy(hapd->own_addr, wpa_s->own_addr, ETH_ALEN);
74
75         return drv;
76 }
77
78
79 static void ap_driver_deinit(void *priv)
80 {
81         struct ap_driver_data *drv = priv;
82
83         os_free(drv);
84 }
85
86
87 static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
88                                 u16 proto, const u8 *data, size_t data_len)
89 {
90         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
91         return -1;
92 }
93
94
95 static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
96                              const u8 *addr, int key_idx, int set_tx,
97                              const u8 *seq, size_t seq_len, const u8 *key,
98                              size_t key_len)
99 {
100         struct ap_driver_data *drv = priv;
101         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
102         return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
103                                key, key_len);
104 }
105
106
107 static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
108                                 int idx, u8 *seq)
109 {
110         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
111         return -1;
112 }
113
114
115 static int ap_driver_flush(void *priv)
116 {
117         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
118         return -1;
119 }
120
121
122 static int ap_driver_read_sta_data(void *priv,
123                                    struct hostap_sta_driver_data *data,
124                                    const u8 *addr)
125 {
126         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
127         return -1;
128 }
129
130
131 static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
132                                    int flags_or, int flags_and)
133 {
134         struct ap_driver_data *drv = priv;
135         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
136         return wpa_drv_sta_set_flags(wpa_s, addr, total_flags, flags_or,
137                                      flags_and);
138 }
139
140
141 static int ap_driver_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
142                                 int reason)
143 {
144         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
145         return -1;
146 }
147
148
149 static int ap_driver_sta_disassoc(void *priv, const u8 *own_addr,
150                                   const u8 *addr, int reason)
151 {
152         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
153         return -1;
154 }
155
156
157 static int ap_driver_sta_remove(void *priv, const u8 *addr)
158 {
159         struct ap_driver_data *drv = priv;
160         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
161         return wpa_drv_sta_remove(wpa_s, addr);
162 }
163
164
165 static int ap_driver_send_mlme(void *priv, const u8 *data, size_t len)
166 {
167         struct ap_driver_data *drv = priv;
168         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
169         return wpa_drv_send_mlme(wpa_s, data, len);
170 }
171
172
173 static int ap_driver_sta_add(const char *ifname, void *priv,
174                              struct hostapd_sta_add_params *params)
175 {
176         struct ap_driver_data *drv = priv;
177         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
178         return wpa_drv_sta_add(wpa_s, params);
179 }
180
181
182 static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
183 {
184         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
185         return -1;
186 }
187
188
189 static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
190 {
191         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
192         return 0;
193 }
194
195
196 static int ap_driver_set_beacon(const char *iface, void *priv,
197                                 const u8 *head, size_t head_len,
198                                 const u8 *tail, size_t tail_len,
199                                 int dtim_period)
200 {
201         struct ap_driver_data *drv = priv;
202         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
203         return wpa_drv_set_beacon(wpa_s, head, head_len, tail, tail_len,
204                                   dtim_period);
205 }
206
207
208 static int ap_driver_set_beacon_int(void *priv, int value)
209 {
210         struct ap_driver_data *drv = priv;
211         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
212         return wpa_drv_set_beacon_int(wpa_s, value);
213 }
214
215
216 static int ap_driver_set_cts_protect(void *priv, int value)
217 {
218         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
219         return -1;
220 }
221
222
223 static int ap_driver_set_preamble(void *priv, int value)
224 {
225         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
226         return -1;
227 }
228
229
230 static int ap_driver_set_short_slot_time(void *priv, int value)
231 {
232         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
233         return -1;
234 }
235
236
237 static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
238                                          int cw_min, int cw_max,
239                                          int burst_time)
240 {
241         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
242         return -1;
243 }
244
245
246 static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
247                                                               u16 *num_modes,
248                                                               u16 *flags)
249 {
250         struct ap_driver_data *drv = priv;
251         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
252         return wpa_drv_get_hw_feature_data(wpa_s, num_modes, flags);
253 }
254
255
256 static int ap_driver_hapd_send_eapol(void *priv, const u8 *addr,
257                                      const u8 *data, size_t data_len,
258                                      int encrypt, const u8 *own_addr)
259 {
260         struct ap_driver_data *drv = priv;
261         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
262         return wpa_drv_hapd_send_eapol(wpa_s, addr, data, data_len, encrypt,
263                                        own_addr);
264 }
265
266
267 struct wpa_driver_ops ap_driver_ops =
268 {
269         .name = "wpa_supplicant",
270         .hapd_init = ap_driver_init,
271         .hapd_deinit = ap_driver_deinit,
272         .send_ether = ap_driver_send_ether,
273         .hapd_set_key = ap_driver_set_key,
274         .get_seqnum = ap_driver_get_seqnum,
275         .flush = ap_driver_flush,
276         .read_sta_data = ap_driver_read_sta_data,
277         .sta_set_flags = ap_driver_sta_set_flags,
278         .sta_deauth = ap_driver_sta_deauth,
279         .sta_disassoc = ap_driver_sta_disassoc,
280         .sta_remove = ap_driver_sta_remove,
281         .send_mlme = ap_driver_send_mlme,
282         .sta_add = ap_driver_sta_add,
283         .get_inact_sec = ap_driver_get_inact_sec,
284         .set_freq = ap_driver_set_freq,
285         .hapd_set_beacon = ap_driver_set_beacon,
286         .set_beacon_int = ap_driver_set_beacon_int,
287         .set_cts_protect = ap_driver_set_cts_protect,
288         .set_preamble = ap_driver_set_preamble,
289         .set_short_slot_time = ap_driver_set_short_slot_time,
290         .set_tx_queue_params = ap_driver_set_tx_queue_params,
291         .get_hw_feature_data = ap_driver_get_hw_feature_data,
292         .hapd_send_eapol = ap_driver_hapd_send_eapol,
293 };
294
295
296 extern struct wpa_driver_ops *wpa_drivers[];
297
298 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
299                                   struct wpa_ssid *ssid,
300                                   struct hostapd_config *conf)
301 {
302         struct hostapd_bss_config *bss = &conf->bss[0];
303         int j, pairwise;
304
305         for (j = 0; wpa_drivers[j]; j++) {
306                 if (os_strcmp("wpa_supplicant", wpa_drivers[j]->name) == 0) {
307                         conf->driver = wpa_drivers[j];
308                         break;
309                 }
310         }
311         if (conf->driver == NULL) {
312                 wpa_printf(MSG_ERROR, "No AP driver ops found");
313                 return -1;
314         }
315
316         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
317
318         if (ssid->frequency == 0) {
319                 /* default channel 11 */
320                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
321                 conf->channel = 11;
322         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
323                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
324                 conf->channel = (ssid->frequency - 2407) / 5;
325         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
326                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
327                 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
328                 conf->channel = (ssid->frequency - 5000) / 5;
329         } else {
330                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
331                            ssid->frequency);
332                 return -1;
333         }
334
335         /* TODO: enable HT if driver supports it;
336          * drop to 11b if driver does not support 11g */
337
338         if (ssid->ssid_len == 0) {
339                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
340                 return -1;
341         }
342         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
343         bss->ssid.ssid[ssid->ssid_len] = '\0';
344         bss->ssid.ssid_len = ssid->ssid_len;
345         bss->ssid.ssid_set = 1;
346
347         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
348                 bss->wpa = ssid->proto;
349         bss->wpa_key_mgmt = ssid->key_mgmt;
350         bss->wpa_pairwise = ssid->pairwise_cipher;
351         if (ssid->passphrase) {
352                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
353         } else if (ssid->psk_set) {
354                 os_free(bss->ssid.wpa_psk);
355                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
356                 if (bss->ssid.wpa_psk == NULL)
357                         return -1;
358                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
359                 bss->ssid.wpa_psk->group = 1;
360         }
361
362         /* Select group cipher based on the enabled pairwise cipher suites */
363         pairwise = 0;
364         if (bss->wpa & 1)
365                 pairwise |= bss->wpa_pairwise;
366         if (bss->wpa & 2) {
367                 if (bss->rsn_pairwise == 0)
368                         bss->rsn_pairwise = bss->wpa_pairwise;
369                 pairwise |= bss->rsn_pairwise;
370         }
371         if (pairwise & WPA_CIPHER_TKIP)
372                 bss->wpa_group = WPA_CIPHER_TKIP;
373         else
374                 bss->wpa_group = WPA_CIPHER_CCMP;
375
376         if (bss->wpa && bss->ieee802_1x)
377                 bss->ssid.security_policy = SECURITY_WPA;
378         else if (bss->wpa)
379                 bss->ssid.security_policy = SECURITY_WPA_PSK;
380         else if (bss->ieee802_1x) {
381                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
382                 bss->ssid.wep.default_len = bss->default_wep_key_len;
383         } else if (bss->ssid.wep.keys_set)
384                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
385         else
386                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
387
388 #ifdef CONFIG_WPS
389         /*
390          * Enable WPS by default, but require user interaction to actually use
391          * it. Only the internal Registrar is supported.
392          */
393         bss->eap_server = 1;
394         bss->wps_state = 2;
395         bss->ap_setup_locked = 1;
396         bss->config_methods = os_strdup("display push_button");
397 #endif /* CONFIG_WPS */
398
399         return 0;
400 }
401
402
403 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
404                              struct wpa_ssid *ssid)
405 {
406         struct wpa_driver_associate_params params;
407         struct hostapd_iface *hapd_iface;
408         struct hostapd_config *conf;
409         size_t i;
410
411         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
412                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
413                 return -1;
414         }
415
416         wpa_supplicant_ap_deinit(wpa_s);
417
418         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
419                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
420
421         os_memset(&params, 0, sizeof(params));
422         params.ssid = ssid->ssid;
423         params.ssid_len = ssid->ssid_len;
424         params.mode = ssid->mode;
425         params.freq = ssid->frequency;
426
427         if (wpa_drv_associate(wpa_s, &params) < 0) {
428                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
429                 return -1;
430         }
431
432         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
433         if (hapd_iface == NULL)
434                 return -1;
435         hapd_iface->owner = wpa_s;
436
437         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
438         if (conf == NULL) {
439                 wpa_supplicant_ap_deinit(wpa_s);
440                 return -1;
441         }
442
443         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
444                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
445                 wpa_supplicant_ap_deinit(wpa_s);
446                 return -1;
447         }
448
449         hapd_iface->num_bss = conf->num_bss;
450         hapd_iface->bss = os_zalloc(conf->num_bss *
451                                     sizeof(struct hostapd_data *));
452         if (hapd_iface->bss == NULL) {
453                 wpa_supplicant_ap_deinit(wpa_s);
454                 return -1;
455         }
456
457         for (i = 0; i < conf->num_bss; i++) {
458                 hapd_iface->bss[i] =
459                         hostapd_alloc_bss_data(hapd_iface, conf,
460                                                &conf->bss[i]);
461                 if (hapd_iface->bss[i] == NULL) {
462                         wpa_supplicant_ap_deinit(wpa_s);
463                         return -1;
464                 }
465
466                 hapd_iface->bss[i]->msg_ctx = wpa_s;
467         }
468
469         if (hostapd_setup_interface(wpa_s->ap_iface)) {
470                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
471                 wpa_supplicant_ap_deinit(wpa_s);
472                 return -1;
473         }
474
475         wpa_s->current_ssid = ssid;
476         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
477
478         return 0;
479 }
480
481
482 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
483 {
484         if (wpa_s->ap_iface == NULL)
485                 return;
486
487         hostapd_interface_deinit(wpa_s->ap_iface);
488         wpa_s->ap_iface = NULL;
489 }
490
491
492 void ap_tx_status(void *ctx, const u8 *addr,
493                   const u8 *buf, size_t len, int ack)
494 {
495         struct wpa_supplicant *wpa_s = ctx;
496         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
497 }
498
499
500 void ap_rx_from_unknown_sta(void *ctx, struct ieee80211_hdr *hdr, size_t len)
501 {
502         struct wpa_supplicant *wpa_s = ctx;
503         hostapd_rx_from_unknown_sta(wpa_s->ap_iface->bss[0], hdr, len);
504 }
505
506
507 #ifdef NEED_AP_MLME
508 void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
509                 struct hostapd_frame_info *fi)
510 {
511         struct wpa_supplicant *wpa_s = ctx;
512         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, stype, fi);
513 }
514
515
516 void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok)
517 {
518         struct wpa_supplicant *wpa_s = ctx;
519         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
520 }
521 #endif /* NEED_AP_MLME */
522
523
524 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
525                                 const u8 *src_addr, const u8 *buf, size_t len)
526 {
527         hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
528 }
529
530
531 #ifdef CONFIG_WPS
532
533 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
534 {
535         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
536 }
537
538
539 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
540                               const char *pin, char *buf, size_t buflen)
541 {
542         int ret, ret_len = 0;
543
544         if (pin == NULL) {
545                 unsigned int rpin = wps_generate_pin();
546                 ret_len = os_snprintf(buf, buflen, "%d", rpin);
547                 pin = buf;
548         }
549
550         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
551         if (ret)
552                 return -1;
553         return ret_len;
554 }
555
556 #endif /* CONFIG_WPS */
557
558
559 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
560                             char *buf, size_t buflen)
561 {
562         if (wpa_s->ap_iface == NULL)
563                 return -1;
564         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
565                                             buf, buflen);
566 }
567
568
569 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
570                       char *buf, size_t buflen)
571 {
572         if (wpa_s->ap_iface == NULL)
573                 return -1;
574         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
575                                       buf, buflen);
576 }
577
578
579 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
580                            char *buf, size_t buflen)
581 {
582         if (wpa_s->ap_iface == NULL)
583                 return -1;
584         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
585                                            buf, buflen);
586 }