1 | /*
|
---|
2 | * Copyright (c) 2015 Jan Kolarik
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup nic
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file WiFi device configuration utility.
|
---|
33 | *
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <ieee80211_iface.h>
|
---|
37 |
|
---|
38 | #include <inet/inetcfg.h>
|
---|
39 | #include <inet/dhcp.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include <str_error.h>
|
---|
45 | #include <loc.h>
|
---|
46 |
|
---|
47 | #define NAME "wifi_supplicant"
|
---|
48 |
|
---|
49 | #define enum_name(name_arr, i) \
|
---|
50 | ((i < 0) ? "NA" : name_arr[i])
|
---|
51 |
|
---|
52 | static const char *ieee80211_security_type_strs[] = {
|
---|
53 | "OPEN", "WEP", "WPA", "WPA2"
|
---|
54 | };
|
---|
55 |
|
---|
56 | static const char *ieee80211_security_alg_strs[] = {
|
---|
57 | "WEP40", "WEP104", "CCMP", "TKIP"
|
---|
58 | };
|
---|
59 |
|
---|
60 | static const char *ieee80211_security_auth_strs[] = {
|
---|
61 | "PSK", "8021X"
|
---|
62 | };
|
---|
63 |
|
---|
64 | static void print_syntax(void)
|
---|
65 | {
|
---|
66 | printf("syntax:\n");
|
---|
67 | printf("\t" NAME " [<cmd> [<args...>]]\n");
|
---|
68 | printf("\t<cmd> is:\n");
|
---|
69 | printf("\tlist - list wifi devices in <index>: <name> format\n");
|
---|
70 | printf("\tscan <index> [-n] - output scan results (force scan "
|
---|
71 | "immediately)\n");
|
---|
72 | printf("\tconnect <index> <ssid_prefix> [<password>] - connect to "
|
---|
73 | "network\n");
|
---|
74 | printf("\tdisconnect <index> - disconnect from network\n");
|
---|
75 | }
|
---|
76 |
|
---|
77 | static void nic_addr_format(nic_address_t *addr, char *out, size_t out_size)
|
---|
78 | {
|
---|
79 | snprintf(out, out_size, "%02x:%02x:%02x:%02x:%02x:%02x",
|
---|
80 | addr->address[0], addr->address[1], addr->address[2],
|
---|
81 | addr->address[3], addr->address[4], addr->address[5]);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static errno_t get_wifi_list(service_id_t **wifis, size_t *count)
|
---|
85 | {
|
---|
86 | category_id_t wifi_cat;
|
---|
87 | errno_t rc = loc_category_get_id("ieee80211", &wifi_cat, 0);
|
---|
88 | if (rc != EOK) {
|
---|
89 | printf("Error resolving category 'ieee80211'.\n");
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | rc = loc_category_get_svcs(wifi_cat, wifis, count);
|
---|
94 | if (rc != EOK) {
|
---|
95 | printf("Error getting list of WIFIs.\n");
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return EOK;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static async_sess_t *get_wifi_by_index(size_t i)
|
---|
103 | {
|
---|
104 | service_id_t *wifis = NULL;
|
---|
105 | size_t count;
|
---|
106 |
|
---|
107 | errno_t rc = get_wifi_list(&wifis, &count);
|
---|
108 | if (rc != EOK) {
|
---|
109 | printf("Error fetching wifi list.\n");
|
---|
110 | return NULL;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (i >= count) {
|
---|
114 | printf("Invalid wifi index.\n");
|
---|
115 | free(wifis);
|
---|
116 | return NULL;
|
---|
117 | }
|
---|
118 |
|
---|
119 | async_sess_t *sess =
|
---|
120 | loc_service_connect(wifis[i], INTERFACE_DDF, 0);
|
---|
121 | if (sess == NULL) {
|
---|
122 | printf("Error connecting to service.\n");
|
---|
123 | free(wifis);
|
---|
124 | return NULL;
|
---|
125 | }
|
---|
126 |
|
---|
127 | return sess;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static errno_t wifi_list(void)
|
---|
131 | {
|
---|
132 | service_id_t *wifis = NULL;
|
---|
133 | size_t count;
|
---|
134 |
|
---|
135 | errno_t rc = get_wifi_list(&wifis, &count);
|
---|
136 | if (rc != EOK) {
|
---|
137 | printf("Error fetching wifi list: %s\n", str_error(rc));
|
---|
138 | return EINVAL;
|
---|
139 | }
|
---|
140 |
|
---|
141 | printf("[Index]: [Service Name]\n");
|
---|
142 | for (size_t i = 0; i < count; i++) {
|
---|
143 | char *svc_name;
|
---|
144 | rc = loc_service_get_name(wifis[i], &svc_name);
|
---|
145 | if (rc != EOK) {
|
---|
146 | printf("Error getting service name: %s\n", str_error(rc));
|
---|
147 | free(wifis);
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | printf("%zu: %s\n", i, svc_name);
|
---|
152 |
|
---|
153 | free(svc_name);
|
---|
154 | }
|
---|
155 |
|
---|
156 | return EOK;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static errno_t wifi_connect(uint32_t index, char *ssid_start, char *password)
|
---|
160 | {
|
---|
161 | assert(ssid_start);
|
---|
162 |
|
---|
163 | async_sess_t *sess = get_wifi_by_index(index);
|
---|
164 | if (sess == NULL) {
|
---|
165 | printf("Specified WIFI doesn't exist or cannot connect to "
|
---|
166 | "it.\n");
|
---|
167 | return EINVAL;
|
---|
168 | }
|
---|
169 |
|
---|
170 | errno_t rc = ieee80211_disconnect(sess);
|
---|
171 | if (rc != EOK) {
|
---|
172 | if (rc == EREFUSED)
|
---|
173 | printf("Device is not ready yet.\n");
|
---|
174 | else
|
---|
175 | printf("Error when disconnecting device: %s\n",
|
---|
176 | str_error(rc));
|
---|
177 |
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | rc = ieee80211_connect(sess, ssid_start, password);
|
---|
182 | if (rc != EOK) {
|
---|
183 | if (rc == EREFUSED)
|
---|
184 | printf("Device is not ready yet.\n");
|
---|
185 | else if (rc == ETIMEOUT)
|
---|
186 | printf("Timeout when authenticating to network.\n");
|
---|
187 | else if (rc == ENOENT)
|
---|
188 | printf("Given SSID not in scan results.\n");
|
---|
189 | else
|
---|
190 | printf("Error when connecting to network: %s\n",
|
---|
191 | str_error(rc));
|
---|
192 |
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 |
|
---|
196 | // TODO: Wait for DHCP address?
|
---|
197 |
|
---|
198 | printf("Successfully connected to network!\n");
|
---|
199 |
|
---|
200 | return EOK;
|
---|
201 | }
|
---|
202 |
|
---|
203 | static errno_t wifi_disconnect(uint32_t index)
|
---|
204 | {
|
---|
205 | async_sess_t *sess = get_wifi_by_index(index);
|
---|
206 | if (sess == NULL) {
|
---|
207 | printf("Specified WIFI doesn't exist or cannot connect to "
|
---|
208 | "it.\n");
|
---|
209 | return EINVAL;
|
---|
210 | }
|
---|
211 |
|
---|
212 | errno_t rc = ieee80211_disconnect(sess);
|
---|
213 | if (rc != EOK) {
|
---|
214 | if (rc == EREFUSED)
|
---|
215 | printf("Device is not ready yet.\n");
|
---|
216 | else if (rc == EINVAL)
|
---|
217 | printf("Not connected to any WiFi network.\n");
|
---|
218 | else
|
---|
219 | printf("Error when disconnecting from network: %s\n",
|
---|
220 | str_error(rc));
|
---|
221 |
|
---|
222 | return rc;
|
---|
223 | }
|
---|
224 |
|
---|
225 | printf("Successfully disconnected.\n");
|
---|
226 |
|
---|
227 | return EOK;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static errno_t wifi_scan(uint32_t index, bool now)
|
---|
231 | {
|
---|
232 | async_sess_t *sess = get_wifi_by_index(index);
|
---|
233 | if (sess == NULL) {
|
---|
234 | printf("Specified WIFI doesn't exist or cannot connect to "
|
---|
235 | "it.\n");
|
---|
236 | return EINVAL;
|
---|
237 | }
|
---|
238 |
|
---|
239 | ieee80211_scan_results_t scan_results;
|
---|
240 | errno_t rc = ieee80211_get_scan_results(sess, &scan_results, now);
|
---|
241 | if (rc != EOK) {
|
---|
242 | if (rc == EREFUSED)
|
---|
243 | printf("Device is not ready yet.\n");
|
---|
244 | else
|
---|
245 | printf("Failed to fetch scan results: %s\n",
|
---|
246 | str_error(rc));
|
---|
247 |
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (scan_results.length == 0)
|
---|
252 | return EOK;
|
---|
253 |
|
---|
254 | printf("%16.16s %17s %4s %5s %5s %7s %7s\n",
|
---|
255 | "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG");
|
---|
256 |
|
---|
257 | for (uint8_t i = 0; i < scan_results.length; i++) {
|
---|
258 | ieee80211_scan_result_t result = scan_results.results[i];
|
---|
259 | char mac_addr_hex[18];
|
---|
260 | nic_addr_format(&result.bssid, mac_addr_hex, 18);
|
---|
261 |
|
---|
262 | printf("%16.16s %17s %4d %5s %5s %7s %7s\n",
|
---|
263 | result.ssid, mac_addr_hex,
|
---|
264 | result.channel,
|
---|
265 | enum_name(ieee80211_security_type_strs, result.security.type),
|
---|
266 | enum_name(ieee80211_security_auth_strs, result.security.auth),
|
---|
267 | enum_name(ieee80211_security_alg_strs, result.security.pair_alg),
|
---|
268 | enum_name(ieee80211_security_alg_strs, result.security.group_alg));
|
---|
269 | }
|
---|
270 |
|
---|
271 | return EOK;
|
---|
272 | }
|
---|
273 |
|
---|
274 | int main(int argc, char *argv[])
|
---|
275 | {
|
---|
276 | errno_t rc = inetcfg_init();
|
---|
277 | if (rc != EOK) {
|
---|
278 | printf("%s: Failed connecting to inetcfg service: %s.\n",
|
---|
279 | NAME, str_error(rc));
|
---|
280 | return 1;
|
---|
281 | }
|
---|
282 |
|
---|
283 | rc = dhcp_init();
|
---|
284 | if (rc != EOK) {
|
---|
285 | printf("%s: Failed connecting to dhcp service: %s.\n",
|
---|
286 | NAME, str_error(rc));
|
---|
287 | return 1;
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (argc == 2) {
|
---|
291 | if (!str_cmp(argv[1], "list"))
|
---|
292 | return wifi_list();
|
---|
293 | } else if (argc > 2) {
|
---|
294 | uint32_t index;
|
---|
295 | rc = str_uint32_t(argv[2], NULL, 10, false, &index);
|
---|
296 | if (rc != EOK) {
|
---|
297 | printf("%s: Invalid argument.\n", NAME);
|
---|
298 | print_syntax();
|
---|
299 | return EINVAL;
|
---|
300 | }
|
---|
301 |
|
---|
302 | if (!str_cmp(argv[1], "scan")) {
|
---|
303 | bool now = false;
|
---|
304 | if (argc > 3)
|
---|
305 | if (!str_cmp(argv[3], "-n"))
|
---|
306 | now = true;
|
---|
307 |
|
---|
308 | return wifi_scan(index, now);
|
---|
309 | } else if (!str_cmp(argv[1], "connect")) {
|
---|
310 | char *pass = NULL;
|
---|
311 | if (argc > 3) {
|
---|
312 | if (argc > 4)
|
---|
313 | pass = argv[4];
|
---|
314 |
|
---|
315 | return wifi_connect(index, argv[3], pass);
|
---|
316 | }
|
---|
317 | } else if (!str_cmp(argv[1], "disconnect"))
|
---|
318 | return wifi_disconnect(index);
|
---|
319 | }
|
---|
320 |
|
---|
321 | print_syntax();
|
---|
322 |
|
---|
323 | return EOK;
|
---|
324 | }
|
---|
325 |
|
---|
326 | /** @}
|
---|
327 | */
|
---|