2e0882a575398bbf30e3f0a8ed1e32e273858f1d
[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, int beacon_int)
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, beacon_int);
205 }
206
207
208 static int ap_driver_set_cts_protect(void *priv, int value)
209 {
210         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
211         return -1;
212 }
213
214
215 static int ap_driver_set_preamble(void *priv, int value)
216 {
217         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
218         return -1;
219 }
220
221
222 static int ap_driver_set_short_slot_time(void *priv, int value)
223 {
224         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
225         return -1;
226 }
227
228
229 static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
230                                          int cw_min, int cw_max,
231                                          int burst_time)
232 {
233         wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
234         return -1;
235 }
236
237
238 static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
239                                                               u16 *num_modes,
240                                                               u16 *flags)
241 {
242         struct ap_driver_data *drv = priv;
243         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
244         return wpa_drv_get_hw_feature_data(wpa_s, num_modes, flags);
245 }
246
247
248 static int ap_driver_hapd_send_eapol(void *priv, const u8 *addr,
249                                      const u8 *data, size_t data_len,
250                                      int encrypt, const u8 *own_addr)
251 {
252         struct ap_driver_data *drv = priv;
253         struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
254         return wpa_drv_hapd_send_eapol(wpa_s, addr, data, data_len, encrypt,
255                                        own_addr);
256 }
257
258
259 struct wpa_driver_ops ap_driver_ops =
260 {
261         .name = "wpa_supplicant",
262         .hapd_init = ap_driver_init,
263         .hapd_deinit = ap_driver_deinit,
264         .send_ether = ap_driver_send_ether,
265         .set_key = ap_driver_set_key,
266         .get_seqnum = ap_driver_get_seqnum,
267         .flush = ap_driver_flush,
268         .read_sta_data = ap_driver_read_sta_data,
269         .sta_set_flags = ap_driver_sta_set_flags,
270         .sta_deauth = ap_driver_sta_deauth,
271         .sta_disassoc = ap_driver_sta_disassoc,
272         .sta_remove = ap_driver_sta_remove,
273         .send_mlme = ap_driver_send_mlme,
274         .sta_add = ap_driver_sta_add,
275         .get_inact_sec = ap_driver_get_inact_sec,
276         .set_freq = ap_driver_set_freq,
277         .set_beacon = ap_driver_set_beacon,
278         .set_cts_protect = ap_driver_set_cts_protect,
279         .set_preamble = ap_driver_set_preamble,
280         .set_short_slot_time = ap_driver_set_short_slot_time,
281         .set_tx_queue_params = ap_driver_set_tx_queue_params,
282         .get_hw_feature_data = ap_driver_get_hw_feature_data,
283         .hapd_send_eapol = ap_driver_hapd_send_eapol,
284 };
285
286
287 extern struct wpa_driver_ops *wpa_drivers[];
288
289 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
290                                   struct wpa_ssid *ssid,
291                                   struct hostapd_config *conf)
292 {
293         struct hostapd_bss_config *bss = &conf->bss[0];
294         int j, pairwise;
295
296         for (j = 0; wpa_drivers[j]; j++) {
297                 if (os_strcmp("wpa_supplicant", wpa_drivers[j]->name) == 0) {
298                         conf->driver = wpa_drivers[j];
299                         break;
300                 }
301         }
302         if (conf->driver == NULL) {
303                 wpa_printf(MSG_ERROR, "No AP driver ops found");
304                 return -1;
305         }
306
307         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
308
309         if (ssid->frequency == 0) {
310                 /* default channel 11 */
311                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
312                 conf->channel = 11;
313         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
314                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
315                 conf->channel = (ssid->frequency - 2407) / 5;
316         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
317                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
318                 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
319                 conf->channel = (ssid->frequency - 5000) / 5;
320         } else {
321                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
322                            ssid->frequency);
323                 return -1;
324         }
325
326         /* TODO: enable HT if driver supports it;
327          * drop to 11b if driver does not support 11g */
328
329         if (ssid->ssid_len == 0) {
330                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
331                 return -1;
332         }
333         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
334         bss->ssid.ssid[ssid->ssid_len] = '\0';
335         bss->ssid.ssid_len = ssid->ssid_len;
336         bss->ssid.ssid_set = 1;
337
338         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
339                 bss->wpa = ssid->proto;
340         bss->wpa_key_mgmt = ssid->key_mgmt;
341         bss->wpa_pairwise = ssid->pairwise_cipher;
342         if (ssid->passphrase) {
343                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
344         } else if (ssid->psk_set) {
345                 os_free(bss->ssid.wpa_psk);
346                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
347                 if (bss->ssid.wpa_psk == NULL)
348                         return -1;
349                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
350                 bss->ssid.wpa_psk->group = 1;
351         }
352
353         /* Select group cipher based on the enabled pairwise cipher suites */
354         pairwise = 0;
355         if (bss->wpa & 1)
356                 pairwise |= bss->wpa_pairwise;
357         if (bss->wpa & 2) {
358                 if (bss->rsn_pairwise == 0)
359                         bss->rsn_pairwise = bss->wpa_pairwise;
360                 pairwise |= bss->rsn_pairwise;
361         }
362         if (pairwise & WPA_CIPHER_TKIP)
363                 bss->wpa_group = WPA_CIPHER_TKIP;
364         else
365                 bss->wpa_group = WPA_CIPHER_CCMP;
366
367         if (bss->wpa && bss->ieee802_1x)
368                 bss->ssid.security_policy = SECURITY_WPA;
369         else if (bss->wpa)
370                 bss->ssid.security_policy = SECURITY_WPA_PSK;
371         else if (bss->ieee802_1x) {
372                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
373                 bss->ssid.wep.default_len = bss->default_wep_key_len;
374         } else if (bss->ssid.wep.keys_set)
375                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
376         else
377                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
378
379 #ifdef CONFIG_WPS
380         /*
381          * Enable WPS by default, but require user interaction to actually use
382          * it. Only the internal Registrar is supported.
383          */
384         bss->eap_server = 1;
385         bss->wps_state = 2;
386         bss->ap_setup_locked = 1;
387         bss->config_methods = os_strdup("display push_button");
388 #endif /* CONFIG_WPS */
389
390         return 0;
391 }
392
393
394 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
395                              struct wpa_ssid *ssid)
396 {
397         struct wpa_driver_associate_params params;
398         struct hostapd_iface *hapd_iface;
399         struct hostapd_config *conf;
400         size_t i;
401
402         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
403                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
404                 return -1;
405         }
406
407         wpa_supplicant_ap_deinit(wpa_s);
408
409         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
410                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
411
412         os_memset(&params, 0, sizeof(params));
413         params.ssid = ssid->ssid;
414         params.ssid_len = ssid->ssid_len;
415         params.mode = ssid->mode;
416         params.freq = ssid->frequency;
417
418         if (wpa_drv_associate(wpa_s, &params) < 0) {
419                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
420                 return -1;
421         }
422
423         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
424         if (hapd_iface == NULL)
425                 return -1;
426         hapd_iface->owner = wpa_s;
427
428         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
429         if (conf == NULL) {
430                 wpa_supplicant_ap_deinit(wpa_s);
431                 return -1;
432         }
433
434         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
435                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
436                 wpa_supplicant_ap_deinit(wpa_s);
437                 return -1;
438         }
439
440         hapd_iface->num_bss = conf->num_bss;
441         hapd_iface->bss = os_zalloc(conf->num_bss *
442                                     sizeof(struct hostapd_data *));
443         if (hapd_iface->bss == NULL) {
444                 wpa_supplicant_ap_deinit(wpa_s);
445                 return -1;
446         }
447
448         for (i = 0; i < conf->num_bss; i++) {
449                 hapd_iface->bss[i] =
450                         hostapd_alloc_bss_data(hapd_iface, conf,
451                                                &conf->bss[i]);
452                 if (hapd_iface->bss[i] == NULL) {
453                         wpa_supplicant_ap_deinit(wpa_s);
454                         return -1;
455                 }
456
457                 hapd_iface->bss[i]->msg_ctx = wpa_s;
458         }
459
460         if (hostapd_setup_interface(wpa_s->ap_iface)) {
461                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
462                 wpa_supplicant_ap_deinit(wpa_s);
463                 return -1;
464         }
465
466         wpa_s->current_ssid = ssid;
467         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
468         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
469
470         return 0;
471 }
472
473
474 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
475 {
476         if (wpa_s->ap_iface == NULL)
477                 return;
478
479         hostapd_interface_deinit(wpa_s->ap_iface);
480         wpa_s->ap_iface = NULL;
481 }
482
483
484 void ap_tx_status(void *ctx, const u8 *addr,
485                   const u8 *buf, size_t len, int ack)
486 {
487 #ifdef NEED_AP_MLME
488         struct wpa_supplicant *wpa_s = ctx;
489         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
490 #endif /* NEED_AP_MLME */
491 }
492
493
494 void ap_rx_from_unknown_sta(void *ctx, const struct ieee80211_hdr *hdr,
495                             size_t len)
496 {
497 #ifdef NEED_AP_MLME
498         struct wpa_supplicant *wpa_s = ctx;
499         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2);
500 #endif /* NEED_AP_MLME */
501 }
502
503
504 void ap_mgmt_rx(void *ctx, u8 *buf, size_t len,
505                 struct hostapd_frame_info *fi)
506 {
507 #ifdef NEED_AP_MLME
508         struct wpa_supplicant *wpa_s = ctx;
509         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, fi);
510 #endif /* NEED_AP_MLME */
511 }
512
513
514 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
515 {
516 #ifdef NEED_AP_MLME
517         struct wpa_supplicant *wpa_s = ctx;
518         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
519 #endif /* NEED_AP_MLME */
520 }
521
522
523 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
524                                 const u8 *src_addr, const u8 *buf, size_t len)
525 {
526         hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
527 }
528
529
530 #ifdef CONFIG_WPS
531
532 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
533 {
534         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
535 }
536
537
538 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
539                               const char *pin, char *buf, size_t buflen)
540 {
541         int ret, ret_len = 0;
542
543         if (pin == NULL) {
544                 unsigned int rpin = wps_generate_pin();
545                 ret_len = os_snprintf(buf, buflen, "%d", rpin);
546                 pin = buf;
547         }
548
549         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
550         if (ret)
551                 return -1;
552         return ret_len;
553 }
554
555 #endif /* CONFIG_WPS */
556
557
558 #ifdef CONFIG_CTRL_IFACE
559
560 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
561                             char *buf, size_t buflen)
562 {
563         if (wpa_s->ap_iface == NULL)
564                 return -1;
565         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
566                                             buf, buflen);
567 }
568
569
570 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
571                       char *buf, size_t buflen)
572 {
573         if (wpa_s->ap_iface == NULL)
574                 return -1;
575         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
576                                       buf, buflen);
577 }
578
579
580 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
581                            char *buf, size_t buflen)
582 {
583         if (wpa_s->ap_iface == NULL)
584                 return -1;
585         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
586                                            buf, buflen);
587 }
588
589
590 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
591                                  size_t buflen, int verbose)
592 {
593         char *pos = buf, *end = buf + buflen;
594         int ret;
595         struct hostapd_bss_config *conf;
596
597         if (wpa_s->ap_iface == NULL)
598                 return -1;
599
600         conf = wpa_s->ap_iface->bss[0]->conf;
601         if (conf->wpa == 0)
602                 return 0;
603
604         ret = os_snprintf(pos, end - pos,
605                           "pairwise_cipher=%s\n"
606                           "group_cipher=%s\n"
607                           "key_mgmt=%s\n",
608                           wpa_cipher_txt(conf->rsn_pairwise),
609                           wpa_cipher_txt(conf->wpa_group),
610                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
611                                            conf->wpa));
612         if (ret < 0 || ret >= end - pos)
613                 return pos - buf;
614         pos += ret;
615         return pos - buf;
616 }
617
618 #endif /* CONFIG_CTRL_IFACE */