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