Move wpa_supplicant_create_ap() into ap.c
[wpasupplicant] / 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 #include "eap_common/eap_defs.h"
22 #include "eap_server/eap_methods.h"
23 #include "eap_common/eap_wsc_common.h"
24 #include "config_ssid.h"
25 #include "wpa_supplicant_i.h"
26
27
28 int hostapd_reload_config(struct hostapd_iface *iface)
29 {
30         /* TODO */
31         return -1;
32 }
33
34
35 int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
36                           const u8 *addr, int *vlan_id)
37 {
38         int start, end, middle, res;
39
40         start = 0;
41         end = num_entries - 1;
42
43         while (start <= end) {
44                 middle = (start + end) / 2;
45                 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
46                 if (res == 0) {
47                         if (vlan_id)
48                                 *vlan_id = list[middle].vlan_id;
49                         return 1;
50                 }
51                 if (res < 0)
52                         start = middle + 1;
53                 else
54                         end = middle - 1;
55         }
56
57         return 0;
58 }
59
60
61 int hostapd_rate_found(int *list, int rate)
62 {
63         int i;
64
65         if (list == NULL)
66                 return 0;
67
68         for (i = 0; list[i] >= 0; i++)
69                 if (list[i] == rate)
70                         return 1;
71
72         return 0;
73 }
74
75
76 const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
77 {
78         return NULL;
79 }
80
81
82 int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
83                                          void *ctx), void *ctx)
84 {
85         /* TODO */
86         return 0;
87 }
88
89
90 const struct hostapd_eap_user *
91 hostapd_get_eap_user(const struct hostapd_bss_config *conf, const u8 *identity,
92                      size_t identity_len, int phase2)
93 {
94         struct hostapd_eap_user *user = conf->eap_user;
95
96 #ifdef CONFIG_WPS
97         if (conf->wps_state && identity_len == WSC_ID_ENROLLEE_LEN &&
98             os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0) {
99                 static struct hostapd_eap_user wsc_enrollee;
100                 os_memset(&wsc_enrollee, 0, sizeof(wsc_enrollee));
101                 wsc_enrollee.methods[0].method = eap_server_get_type(
102                         "WSC", &wsc_enrollee.methods[0].vendor);
103                 return &wsc_enrollee;
104         }
105
106         if (conf->wps_state && conf->ap_pin &&
107             identity_len == WSC_ID_REGISTRAR_LEN &&
108             os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0) {
109                 static struct hostapd_eap_user wsc_registrar;
110                 os_memset(&wsc_registrar, 0, sizeof(wsc_registrar));
111                 wsc_registrar.methods[0].method = eap_server_get_type(
112                         "WSC", &wsc_registrar.methods[0].vendor);
113                 wsc_registrar.password = (u8 *) conf->ap_pin;
114                 wsc_registrar.password_len = os_strlen(conf->ap_pin);
115                 return &wsc_registrar;
116         }
117 #endif /* CONFIG_WPS */
118
119         while (user) {
120                 if (!phase2 && user->identity == NULL) {
121                         /* Wildcard match */
122                         break;
123                 }
124
125                 if (user->phase2 == !!phase2 && user->wildcard_prefix &&
126                     identity_len >= user->identity_len &&
127                     os_memcmp(user->identity, identity, user->identity_len) ==
128                     0) {
129                         /* Wildcard prefix match */
130                         break;
131                 }
132
133                 if (user->phase2 == !!phase2 &&
134                     user->identity_len == identity_len &&
135                     os_memcmp(user->identity, identity, identity_len) == 0)
136                         break;
137                 user = user->next;
138         }
139
140         return user;
141 }
142
143
144 void wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
145                               struct wpa_ssid *ssid)
146 {
147         struct wpa_driver_associate_params params;
148
149         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
150                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
151                 return;
152         }
153
154         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
155                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
156
157         os_memset(&params, 0, sizeof(params));
158         params.ssid = ssid->ssid;
159         params.ssid_len = ssid->ssid_len;
160         params.mode = ssid->mode;
161
162         if (wpa_drv_associate(wpa_s, &params) < 0)
163                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
164 }