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