[59fa7ab] | 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 |
|
---|
[053fc2b] | 38 | #include <inet/inetcfg.h>
|
---|
| 39 | #include <inet/dhcp.h>
|
---|
[59fa7ab] | 40 | #include <errno.h>
|
---|
| 41 | #include <stdio.h>
|
---|
[38d150e] | 42 | #include <stdlib.h>
|
---|
[1d6dd2a] | 43 | #include <str.h>
|
---|
[c1694b6b] | 44 | #include <str_error.h>
|
---|
[59fa7ab] | 45 | #include <loc.h>
|
---|
| 46 |
|
---|
[8a64320e] | 47 | #define NAME "wifi_supplicant"
|
---|
[59fa7ab] | 48 |
|
---|
[8a64320e] | 49 | #define enum_name(name_arr, i) \
|
---|
| 50 | ((i < 0) ? "NA" : name_arr[i])
|
---|
[1dcc0b9] | 51 |
|
---|
[1433ecda] | 52 | static const char *ieee80211_security_type_strs[] = {
|
---|
[1dcc0b9] | 53 | "OPEN", "WEP", "WPA", "WPA2"
|
---|
| 54 | };
|
---|
| 55 |
|
---|
[1433ecda] | 56 | static const char *ieee80211_security_alg_strs[] = {
|
---|
[1dcc0b9] | 57 | "WEP40", "WEP104", "CCMP", "TKIP"
|
---|
| 58 | };
|
---|
| 59 |
|
---|
[1433ecda] | 60 | static const char *ieee80211_security_auth_strs[] = {
|
---|
[1dcc0b9] | 61 | "PSK", "8021X"
|
---|
| 62 | };
|
---|
| 63 |
|
---|
[59fa7ab] | 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");
|
---|
[1dcc0b9] | 70 | printf("\tscan <index> [-n] - output scan results (force scan "
|
---|
[8a64320e] | 71 | "immediately)\n");
|
---|
[1dcc0b9] | 72 | printf("\tconnect <index> <ssid_prefix> [<password>] - connect to "
|
---|
[8a64320e] | 73 | "network\n");
|
---|
[1dcc0b9] | 74 | printf("\tdisconnect <index> - disconnect from network\n");
|
---|
[59fa7ab] | 75 | }
|
---|
| 76 |
|
---|
[0eea807] | 77 | static void nic_addr_format(nic_address_t *addr, char *out, size_t out_size)
|
---|
[59fa7ab] | 78 | {
|
---|
[0eea807] | 79 | snprintf(out, out_size, "%02x:%02x:%02x:%02x:%02x:%02x",
|
---|
[8a64320e] | 80 | addr->address[0], addr->address[1], addr->address[2],
|
---|
| 81 | addr->address[3], addr->address[4], addr->address[5]);
|
---|
[59fa7ab] | 82 | }
|
---|
| 83 |
|
---|
[b7fd2a0] | 84 | static errno_t get_wifi_list(service_id_t **wifis, size_t *count)
|
---|
[59fa7ab] | 85 | {
|
---|
| 86 | category_id_t wifi_cat;
|
---|
[b7fd2a0] | 87 | errno_t rc = loc_category_get_id("ieee80211", &wifi_cat, 0);
|
---|
[59fa7ab] | 88 | if (rc != EOK) {
|
---|
| 89 | printf("Error resolving category 'ieee80211'.\n");
|
---|
| 90 | return rc;
|
---|
| 91 | }
|
---|
[a35b458] | 92 |
|
---|
[59fa7ab] | 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 | }
|
---|
[a35b458] | 98 |
|
---|
[59fa7ab] | 99 | return EOK;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | static async_sess_t *get_wifi_by_index(size_t i)
|
---|
| 103 | {
|
---|
| 104 | service_id_t *wifis = NULL;
|
---|
[8a64320e] | 105 | size_t count;
|
---|
[a35b458] | 106 |
|
---|
[b7fd2a0] | 107 | errno_t rc = get_wifi_list(&wifis, &count);
|
---|
[59fa7ab] | 108 | if (rc != EOK) {
|
---|
| 109 | printf("Error fetching wifi list.\n");
|
---|
| 110 | return NULL;
|
---|
| 111 | }
|
---|
[a35b458] | 112 |
|
---|
[8a64320e] | 113 | if (i >= count) {
|
---|
[59fa7ab] | 114 | printf("Invalid wifi index.\n");
|
---|
| 115 | free(wifis);
|
---|
| 116 | return NULL;
|
---|
| 117 | }
|
---|
[a35b458] | 118 |
|
---|
[8a64320e] | 119 | async_sess_t *sess =
|
---|
[f9b2cb4c] | 120 | loc_service_connect(wifis[i], INTERFACE_DDF, 0);
|
---|
[59fa7ab] | 121 | if (sess == NULL) {
|
---|
| 122 | printf("Error connecting to service.\n");
|
---|
| 123 | free(wifis);
|
---|
| 124 | return NULL;
|
---|
| 125 | }
|
---|
[a35b458] | 126 |
|
---|
[59fa7ab] | 127 | return sess;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[b7fd2a0] | 130 | static errno_t wifi_list(void)
|
---|
[59fa7ab] | 131 | {
|
---|
| 132 | service_id_t *wifis = NULL;
|
---|
| 133 | size_t count;
|
---|
[a35b458] | 134 |
|
---|
[b7fd2a0] | 135 | errno_t rc = get_wifi_list(&wifis, &count);
|
---|
[59fa7ab] | 136 | if (rc != EOK) {
|
---|
[c1694b6b] | 137 | printf("Error fetching wifi list: %s\n", str_error(rc));
|
---|
[59fa7ab] | 138 | return EINVAL;
|
---|
| 139 | }
|
---|
[a35b458] | 140 |
|
---|
[59fa7ab] | 141 | printf("[Index]: [Service Name]\n");
|
---|
| 142 | for (size_t i = 0; i < count; i++) {
|
---|
[8a64320e] | 143 | char *svc_name;
|
---|
[59fa7ab] | 144 | rc = loc_service_get_name(wifis[i], &svc_name);
|
---|
| 145 | if (rc != EOK) {
|
---|
[c1694b6b] | 146 | printf("Error getting service name: %s\n", str_error(rc));
|
---|
[59fa7ab] | 147 | free(wifis);
|
---|
| 148 | return rc;
|
---|
| 149 | }
|
---|
[a35b458] | 150 |
|
---|
[59fa7ab] | 151 | printf("%zu: %s\n", i, svc_name);
|
---|
[a35b458] | 152 |
|
---|
[59fa7ab] | 153 | free(svc_name);
|
---|
| 154 | }
|
---|
[a35b458] | 155 |
|
---|
[59fa7ab] | 156 | return EOK;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[b7fd2a0] | 159 | static errno_t wifi_connect(uint32_t index, char *ssid_start, char *password)
|
---|
[59fa7ab] | 160 | {
|
---|
[1dcc0b9] | 161 | assert(ssid_start);
|
---|
[a35b458] | 162 |
|
---|
[59fa7ab] | 163 | async_sess_t *sess = get_wifi_by_index(index);
|
---|
| 164 | if (sess == NULL) {
|
---|
[1dcc0b9] | 165 | printf("Specified WIFI doesn't exist or cannot connect to "
|
---|
[8a64320e] | 166 | "it.\n");
|
---|
[59fa7ab] | 167 | return EINVAL;
|
---|
| 168 | }
|
---|
[a35b458] | 169 |
|
---|
[b7fd2a0] | 170 | errno_t rc = ieee80211_disconnect(sess);
|
---|
[1433ecda] | 171 | if (rc != EOK) {
|
---|
[8a64320e] | 172 | if (rc == EREFUSED)
|
---|
| 173 | printf("Device is not ready yet.\n");
|
---|
| 174 | else
|
---|
[c1694b6b] | 175 | printf("Error when disconnecting device: %s\n",
|
---|
| 176 | str_error(rc));
|
---|
[a35b458] | 177 |
|
---|
[053fc2b] | 178 | return rc;
|
---|
| 179 | }
|
---|
[a35b458] | 180 |
|
---|
[a931b7b] | 181 | rc = ieee80211_connect(sess, ssid_start, password);
|
---|
[1433ecda] | 182 | if (rc != EOK) {
|
---|
[8a64320e] | 183 | if (rc == EREFUSED)
|
---|
| 184 | printf("Device is not ready yet.\n");
|
---|
| 185 | else if (rc == ETIMEOUT)
|
---|
[1dcc0b9] | 186 | printf("Timeout when authenticating to network.\n");
|
---|
[8a64320e] | 187 | else if (rc == ENOENT)
|
---|
[053fc2b] | 188 | printf("Given SSID not in scan results.\n");
|
---|
[8a64320e] | 189 | else
|
---|
[c1694b6b] | 190 | printf("Error when connecting to network: %s\n",
|
---|
| 191 | str_error(rc));
|
---|
[a35b458] | 192 |
|
---|
[1dcc0b9] | 193 | return rc;
|
---|
| 194 | }
|
---|
[a35b458] | 195 |
|
---|
[8a64320e] | 196 | // TODO: Wait for DHCP address?
|
---|
[a35b458] | 197 |
|
---|
[1dcc0b9] | 198 | printf("Successfully connected to network!\n");
|
---|
[a35b458] | 199 |
|
---|
[1dcc0b9] | 200 | return EOK;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[b7fd2a0] | 203 | static errno_t wifi_disconnect(uint32_t index)
|
---|
[1dcc0b9] | 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 "
|
---|
[8a64320e] | 208 | "it.\n");
|
---|
[59fa7ab] | 209 | return EINVAL;
|
---|
| 210 | }
|
---|
[a35b458] | 211 |
|
---|
[b7fd2a0] | 212 | errno_t rc = ieee80211_disconnect(sess);
|
---|
[8a64320e] | 213 | if (rc != EOK) {
|
---|
| 214 | if (rc == EREFUSED)
|
---|
[1dcc0b9] | 215 | printf("Device is not ready yet.\n");
|
---|
[8a64320e] | 216 | else if (rc == EINVAL)
|
---|
[1dcc0b9] | 217 | printf("Not connected to any WiFi network.\n");
|
---|
[8a64320e] | 218 | else
|
---|
[c1694b6b] | 219 | printf("Error when disconnecting from network: %s\n",
|
---|
| 220 | str_error(rc));
|
---|
[a35b458] | 221 |
|
---|
[1dcc0b9] | 222 | return rc;
|
---|
| 223 | }
|
---|
[a35b458] | 224 |
|
---|
[1dcc0b9] | 225 | printf("Successfully disconnected.\n");
|
---|
[a35b458] | 226 |
|
---|
[59fa7ab] | 227 | return EOK;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[b7fd2a0] | 230 | static errno_t wifi_scan(uint32_t index, bool now)
|
---|
[59fa7ab] | 231 | {
|
---|
| 232 | async_sess_t *sess = get_wifi_by_index(index);
|
---|
| 233 | if (sess == NULL) {
|
---|
[1dcc0b9] | 234 | printf("Specified WIFI doesn't exist or cannot connect to "
|
---|
[8a64320e] | 235 | "it.\n");
|
---|
[59fa7ab] | 236 | return EINVAL;
|
---|
| 237 | }
|
---|
[a35b458] | 238 |
|
---|
[8a64320e] | 239 | ieee80211_scan_results_t scan_results;
|
---|
[b7fd2a0] | 240 | errno_t rc = ieee80211_get_scan_results(sess, &scan_results, now);
|
---|
[8a64320e] | 241 | if (rc != EOK) {
|
---|
| 242 | if (rc == EREFUSED)
|
---|
[1dcc0b9] | 243 | printf("Device is not ready yet.\n");
|
---|
[8a64320e] | 244 | else
|
---|
[c1694b6b] | 245 | printf("Failed to fetch scan results: %s\n",
|
---|
| 246 | str_error(rc));
|
---|
[a35b458] | 247 |
|
---|
[1dcc0b9] | 248 | return rc;
|
---|
[59fa7ab] | 249 | }
|
---|
[a35b458] | 250 |
|
---|
[8a64320e] | 251 | if (scan_results.length == 0)
|
---|
[1dcc0b9] | 252 | return EOK;
|
---|
[a35b458] | 253 |
|
---|
[8a64320e] | 254 | printf("%16.16s %17s %4s %5s %5s %7s %7s\n",
|
---|
| 255 | "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG");
|
---|
[a35b458] | 256 |
|
---|
[8a64320e] | 257 | for (uint8_t i = 0; i < scan_results.length; i++) {
|
---|
[59fa7ab] | 258 | ieee80211_scan_result_t result = scan_results.results[i];
|
---|
[0eea807] | 259 | char mac_addr_hex[18];
|
---|
| 260 | nic_addr_format(&result.bssid, mac_addr_hex, 18);
|
---|
[a35b458] | 261 |
|
---|
[8a64320e] | 262 | printf("%16.16s %17s %4d %5s %5s %7s %7s\n",
|
---|
[0eea807] | 263 | result.ssid, mac_addr_hex,
|
---|
[8a64320e] | 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));
|
---|
[59fa7ab] | 269 | }
|
---|
[a35b458] | 270 |
|
---|
[59fa7ab] | 271 | return EOK;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | int main(int argc, char *argv[])
|
---|
| 275 | {
|
---|
[b7fd2a0] | 276 | errno_t rc = inetcfg_init();
|
---|
[053fc2b] | 277 | if (rc != EOK) {
|
---|
[c1694b6b] | 278 | printf("%s: Failed connecting to inetcfg service: %s.\n",
|
---|
| 279 | NAME, str_error(rc));
|
---|
[053fc2b] | 280 | return 1;
|
---|
| 281 | }
|
---|
[a35b458] | 282 |
|
---|
[053fc2b] | 283 | rc = dhcp_init();
|
---|
| 284 | if (rc != EOK) {
|
---|
[c1694b6b] | 285 | printf("%s: Failed connecting to dhcp service: %s.\n",
|
---|
| 286 | NAME, str_error(rc));
|
---|
[053fc2b] | 287 | return 1;
|
---|
| 288 | }
|
---|
[a35b458] | 289 |
|
---|
[8a64320e] | 290 | if (argc == 2) {
|
---|
| 291 | if (!str_cmp(argv[1], "list"))
|
---|
[59fa7ab] | 292 | return wifi_list();
|
---|
[8a64320e] | 293 | } else if (argc > 2) {
|
---|
| 294 | uint32_t index;
|
---|
[59fa7ab] | 295 | rc = str_uint32_t(argv[2], NULL, 10, false, &index);
|
---|
[8a64320e] | 296 | if (rc != EOK) {
|
---|
| 297 | printf("%s: Invalid argument.\n", NAME);
|
---|
[59fa7ab] | 298 | print_syntax();
|
---|
| 299 | return EINVAL;
|
---|
| 300 | }
|
---|
[a35b458] | 301 |
|
---|
[8a64320e] | 302 | if (!str_cmp(argv[1], "scan")) {
|
---|
[1dcc0b9] | 303 | bool now = false;
|
---|
[8a64320e] | 304 | if (argc > 3)
|
---|
| 305 | if (!str_cmp(argv[3], "-n"))
|
---|
[1dcc0b9] | 306 | now = true;
|
---|
[a35b458] | 307 |
|
---|
[1dcc0b9] | 308 | return wifi_scan(index, now);
|
---|
[8a64320e] | 309 | } else if (!str_cmp(argv[1], "connect")) {
|
---|
[59fa7ab] | 310 | char *pass = NULL;
|
---|
[8a64320e] | 311 | if (argc > 3) {
|
---|
| 312 | if (argc > 4)
|
---|
[59fa7ab] | 313 | pass = argv[4];
|
---|
[a35b458] | 314 |
|
---|
[59fa7ab] | 315 | return wifi_connect(index, argv[3], pass);
|
---|
[8a64320e] | 316 | }
|
---|
| 317 | } else if (!str_cmp(argv[1], "disconnect"))
|
---|
[1dcc0b9] | 318 | return wifi_disconnect(index);
|
---|
[59fa7ab] | 319 | }
|
---|
[a35b458] | 320 |
|
---|
[59fa7ab] | 321 | print_syntax();
|
---|
[a35b458] | 322 |
|
---|
[59fa7ab] | 323 | return EOK;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | /** @}
|
---|
[8a64320e] | 327 | */
|
---|