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