[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 libdrv
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief Driver-side RPC skeletons for IEEE 802.11 interface
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <macros.h>
|
---|
| 39 | #include <str.h>
|
---|
[053fc2b] | 40 | #include <inet/dhcp.h>
|
---|
| 41 | #include <inet/inetcfg.h>
|
---|
[59fa7ab] | 42 | #include "ops/ieee80211.h"
|
---|
| 43 | #include "ieee80211_iface.h"
|
---|
[053fc2b] | 44 | #include "nic_iface.h"
|
---|
[59fa7ab] | 45 |
|
---|
[8a64320e] | 46 | #define MAX_STRING_SIZE 32
|
---|
[59fa7ab] | 47 |
|
---|
| 48 | /** IEEE 802.11 RPC functions IDs. */
|
---|
| 49 | typedef enum {
|
---|
| 50 | IEEE80211_GET_SCAN_RESULTS,
|
---|
[1dcc0b9] | 51 | IEEE80211_CONNECT,
|
---|
| 52 | IEEE80211_DISCONNECT
|
---|
[59fa7ab] | 53 | } ieee80211_funcs_t;
|
---|
| 54 |
|
---|
| 55 | /** Get scan results from IEEE 802.11 device
|
---|
| 56 | *
|
---|
[8a64320e] | 57 | * @param[in] dev_sess Device session.
|
---|
| 58 | * @param[out] results Structure where to put scan results.
|
---|
[59fa7ab] | 59 | *
|
---|
| 60 | * @return EOK If the operation was successfully completed,
|
---|
[cde999a] | 61 | * error code otherwise.
|
---|
[8a64320e] | 62 | *
|
---|
[59fa7ab] | 63 | */
|
---|
[b7fd2a0] | 64 | errno_t ieee80211_get_scan_results(async_sess_t *dev_sess,
|
---|
[8a64320e] | 65 | ieee80211_scan_results_t *results, bool now)
|
---|
[59fa7ab] | 66 | {
|
---|
| 67 | assert(results);
|
---|
[a35b458] | 68 |
|
---|
[59fa7ab] | 69 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
[a35b458] | 70 |
|
---|
[1dcc0b9] | 71 | aid_t aid = async_send_2(exch, DEV_IFACE_ID(IEEE80211_DEV_IFACE),
|
---|
| 72 | IEEE80211_GET_SCAN_RESULTS, now, NULL);
|
---|
[b7fd2a0] | 73 | errno_t rc = async_data_read_start(exch, results,
|
---|
[8a64320e] | 74 | sizeof(ieee80211_scan_results_t));
|
---|
[59fa7ab] | 75 | async_exchange_end(exch);
|
---|
[a35b458] | 76 |
|
---|
[b7fd2a0] | 77 | errno_t res;
|
---|
[59fa7ab] | 78 | async_wait_for(aid, &res);
|
---|
[a35b458] | 79 |
|
---|
[3bacee1] | 80 | if (res != EOK)
|
---|
[b7fd2a0] | 81 | return (errno_t) res;
|
---|
[a35b458] | 82 |
|
---|
[8a64320e] | 83 | return rc;
|
---|
[59fa7ab] | 84 | }
|
---|
| 85 |
|
---|
[053fc2b] | 86 | static bool mac_matches(uint8_t *mac1, uint8_t *mac2)
|
---|
| 87 | {
|
---|
[8a64320e] | 88 | for (size_t i = 0; i < ETH_ADDR; i++) {
|
---|
| 89 | if (mac1[i] != mac2[i])
|
---|
[053fc2b] | 90 | return false;
|
---|
| 91 | }
|
---|
[a35b458] | 92 |
|
---|
[053fc2b] | 93 | return true;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | static sysarg_t get_link_id(uint8_t *mac)
|
---|
| 97 | {
|
---|
| 98 | sysarg_t *link_list;
|
---|
| 99 | inet_link_info_t link_info;
|
---|
| 100 | size_t count;
|
---|
[a35b458] | 101 |
|
---|
[b7fd2a0] | 102 | errno_t rc = inetcfg_get_link_list(&link_list, &count);
|
---|
[053fc2b] | 103 | if (rc != EOK)
|
---|
| 104 | return -1;
|
---|
[a35b458] | 105 |
|
---|
[053fc2b] | 106 | for (size_t i = 0; i < count; i++) {
|
---|
| 107 | rc = inetcfg_link_get(link_list[i], &link_info);
|
---|
| 108 | if (rc != EOK)
|
---|
| 109 | return -1;
|
---|
[a35b458] | 110 |
|
---|
[8a64320e] | 111 | if (mac_matches(mac, link_info.mac_addr))
|
---|
[053fc2b] | 112 | return link_list[i];
|
---|
| 113 | }
|
---|
[a35b458] | 114 |
|
---|
[053fc2b] | 115 | return -1;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[59fa7ab] | 118 | /** Connect to specified network.
|
---|
| 119 | *
|
---|
[8a64320e] | 120 | * @param[in] dev_sess Device session.
|
---|
[1dcc0b9] | 121 | * @param[in] ssid_start Network SSID prefix.
|
---|
[8a64320e] | 122 | * @param[in] password Network password (pass empty string if not needed).
|
---|
[59fa7ab] | 123 | *
|
---|
| 124 | * @return EOK If the operation was successfully completed,
|
---|
[cde999a] | 125 | * error code otherwise.
|
---|
[8a64320e] | 126 | *
|
---|
[59fa7ab] | 127 | */
|
---|
[b7fd2a0] | 128 | errno_t ieee80211_connect(async_sess_t *dev_sess, char *ssid_start, char *password)
|
---|
[59fa7ab] | 129 | {
|
---|
[1dcc0b9] | 130 | assert(ssid_start);
|
---|
[a35b458] | 131 |
|
---|
[b7fd2a0] | 132 | errno_t rc_orig;
|
---|
[a35b458] | 133 |
|
---|
[59fa7ab] | 134 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
[a35b458] | 135 |
|
---|
[59fa7ab] | 136 | aid_t aid = async_send_1(exch, DEV_IFACE_ID(IEEE80211_DEV_IFACE),
|
---|
| 137 | IEEE80211_CONNECT, NULL);
|
---|
[a35b458] | 138 |
|
---|
[b7fd2a0] | 139 | errno_t rc = async_data_write_start(exch, ssid_start,
|
---|
[8a64320e] | 140 | str_size(ssid_start) + 1);
|
---|
[59fa7ab] | 141 | if (rc != EOK) {
|
---|
| 142 | async_exchange_end(exch);
|
---|
| 143 | async_wait_for(aid, &rc_orig);
|
---|
[a35b458] | 144 |
|
---|
[59fa7ab] | 145 | if (rc_orig == EOK)
|
---|
[b7fd2a0] | 146 | return (errno_t) rc;
|
---|
[a35b458] | 147 |
|
---|
[b7fd2a0] | 148 | return (errno_t) rc_orig;
|
---|
[59fa7ab] | 149 | }
|
---|
[a35b458] | 150 |
|
---|
[8a64320e] | 151 | // FIXME: Typecasting string literal
|
---|
| 152 | if (password == NULL)
|
---|
[59fa7ab] | 153 | password = (char *) "";
|
---|
[a35b458] | 154 |
|
---|
[59fa7ab] | 155 | rc = async_data_write_start(exch, password, str_size(password) + 1);
|
---|
| 156 | if (rc != EOK) {
|
---|
| 157 | async_exchange_end(exch);
|
---|
| 158 | async_wait_for(aid, &rc_orig);
|
---|
[a35b458] | 159 |
|
---|
[59fa7ab] | 160 | if (rc_orig == EOK)
|
---|
[b7fd2a0] | 161 | return (errno_t) rc;
|
---|
[a35b458] | 162 |
|
---|
[b7fd2a0] | 163 | return (errno_t) rc_orig;
|
---|
[59fa7ab] | 164 | }
|
---|
[a35b458] | 165 |
|
---|
[59fa7ab] | 166 | async_exchange_end(exch);
|
---|
[a35b458] | 167 |
|
---|
[59fa7ab] | 168 | async_wait_for(aid, &rc);
|
---|
[053fc2b] | 169 | if (rc != EOK)
|
---|
| 170 | return rc;
|
---|
[a35b458] | 171 |
|
---|
[053fc2b] | 172 | /* Send DHCP discover. */
|
---|
| 173 | nic_address_t wifi_mac;
|
---|
| 174 | rc = nic_get_address(dev_sess, &wifi_mac);
|
---|
| 175 | if (rc != EOK)
|
---|
| 176 | return rc;
|
---|
[a35b458] | 177 |
|
---|
[053fc2b] | 178 | sysarg_t link_id = get_link_id(wifi_mac.address);
|
---|
[8a64320e] | 179 | if (link_id == ((sysarg_t) -1))
|
---|
[053fc2b] | 180 | return EINVAL;
|
---|
[a35b458] | 181 |
|
---|
[053fc2b] | 182 | rc = dhcp_discover(link_id);
|
---|
[a35b458] | 183 |
|
---|
[b7fd2a0] | 184 | return (errno_t) rc;
|
---|
[59fa7ab] | 185 | }
|
---|
| 186 |
|
---|
[1dcc0b9] | 187 | /** Disconnect device from network.
|
---|
| 188 | *
|
---|
| 189 | * @param[in] dev_sess Device session.
|
---|
| 190 | *
|
---|
| 191 | * @return EOK If the operation was successfully completed,
|
---|
[cde999a] | 192 | * error code otherwise.
|
---|
[8a64320e] | 193 | *
|
---|
[1dcc0b9] | 194 | */
|
---|
[b7fd2a0] | 195 | errno_t ieee80211_disconnect(async_sess_t *dev_sess)
|
---|
[1dcc0b9] | 196 | {
|
---|
| 197 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
[b7fd2a0] | 198 | errno_t rc = async_req_1_0(exch, DEV_IFACE_ID(IEEE80211_DEV_IFACE),
|
---|
[1dcc0b9] | 199 | IEEE80211_DISCONNECT);
|
---|
| 200 | async_exchange_end(exch);
|
---|
[a35b458] | 201 |
|
---|
[8a64320e] | 202 | if (rc != EOK)
|
---|
[053fc2b] | 203 | return rc;
|
---|
[a35b458] | 204 |
|
---|
[053fc2b] | 205 | nic_address_t wifi_mac;
|
---|
| 206 | rc = nic_get_address(dev_sess, &wifi_mac);
|
---|
| 207 | if (rc != EOK)
|
---|
| 208 | return rc;
|
---|
[a35b458] | 209 |
|
---|
[053fc2b] | 210 | inet_link_info_t link_info;
|
---|
| 211 | inet_addr_info_t addr_info;
|
---|
| 212 | inet_sroute_info_t route_info;
|
---|
| 213 | sysarg_t *addr_list;
|
---|
| 214 | sysarg_t *route_list;
|
---|
| 215 | size_t count;
|
---|
[a35b458] | 216 |
|
---|
[053fc2b] | 217 | /* Remove previous DHCP address. */
|
---|
| 218 | rc = inetcfg_get_addr_list(&addr_list, &count);
|
---|
| 219 | if (rc != EOK)
|
---|
| 220 | return rc;
|
---|
[a35b458] | 221 |
|
---|
[053fc2b] | 222 | for (size_t i = 0; i < count; i++) {
|
---|
| 223 | rc = inetcfg_addr_get(addr_list[i], &addr_info);
|
---|
| 224 | if (rc != EOK)
|
---|
| 225 | return rc;
|
---|
[a35b458] | 226 |
|
---|
[053fc2b] | 227 | rc = inetcfg_link_get(addr_info.ilink, &link_info);
|
---|
| 228 | if (rc != EOK)
|
---|
| 229 | return rc;
|
---|
[a35b458] | 230 |
|
---|
[8a64320e] | 231 | if (mac_matches(wifi_mac.address, link_info.mac_addr)) {
|
---|
| 232 | if (str_test_prefix(addr_info.name, "dhcp")) {
|
---|
[053fc2b] | 233 | rc = inetcfg_addr_delete(addr_list[i]);
|
---|
[8a64320e] | 234 | if (rc != EOK)
|
---|
[053fc2b] | 235 | return rc;
|
---|
[a35b458] | 236 |
|
---|
[053fc2b] | 237 | break;
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
[a35b458] | 241 |
|
---|
[8a64320e] | 242 | /*
|
---|
[053fc2b] | 243 | * TODO: At this moment there can be only one DHCP route,
|
---|
| 244 | * so it must be reimplemented after this limitation will be
|
---|
| 245 | * dropped.
|
---|
| 246 | */
|
---|
| 247 | /* Remove previous DHCP static route. */
|
---|
| 248 | rc = inetcfg_get_sroute_list(&route_list, &count);
|
---|
| 249 | if (rc != EOK)
|
---|
| 250 | return rc;
|
---|
[a35b458] | 251 |
|
---|
[053fc2b] | 252 | for (size_t i = 0; i < count; i++) {
|
---|
| 253 | rc = inetcfg_sroute_get(route_list[i], &route_info);
|
---|
| 254 | if (rc != EOK)
|
---|
| 255 | return rc;
|
---|
[a35b458] | 256 |
|
---|
[8a64320e] | 257 | if (str_test_prefix(route_info.name, "dhcp")) {
|
---|
[053fc2b] | 258 | rc = inetcfg_sroute_delete(route_list[i]);
|
---|
[8a64320e] | 259 | if (rc != EOK)
|
---|
[053fc2b] | 260 | return rc;
|
---|
[a35b458] | 261 |
|
---|
[053fc2b] | 262 | break;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
[a35b458] | 265 |
|
---|
[1dcc0b9] | 266 | return rc;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[59fa7ab] | 269 | static void remote_ieee80211_get_scan_results(ddf_fun_t *fun, void *iface,
|
---|
[984a9ba] | 270 | ipc_call_t *call)
|
---|
[59fa7ab] | 271 | {
|
---|
| 272 | ieee80211_iface_t *ieee80211_iface = (ieee80211_iface_t *) iface;
|
---|
| 273 | assert(ieee80211_iface->get_scan_results);
|
---|
[a35b458] | 274 |
|
---|
[59fa7ab] | 275 | ieee80211_scan_results_t scan_results;
|
---|
| 276 | memset(&scan_results, 0, sizeof(ieee80211_scan_results_t));
|
---|
[a35b458] | 277 |
|
---|
[1dcc0b9] | 278 | bool now = IPC_GET_ARG2(*call);
|
---|
[a35b458] | 279 |
|
---|
[b7fd2a0] | 280 | errno_t rc = ieee80211_iface->get_scan_results(fun, &scan_results, now);
|
---|
[59fa7ab] | 281 | if (rc == EOK) {
|
---|
[984a9ba] | 282 | ipc_call_t data;
|
---|
[59fa7ab] | 283 | size_t max_len;
|
---|
[984a9ba] | 284 | if (!async_data_read_receive(&data, &max_len)) {
|
---|
| 285 | async_answer_0(&data, EINVAL);
|
---|
| 286 | async_answer_0(call, EINVAL);
|
---|
[59fa7ab] | 287 | return;
|
---|
| 288 | }
|
---|
[a35b458] | 289 |
|
---|
[59fa7ab] | 290 | if (max_len < sizeof(ieee80211_scan_results_t)) {
|
---|
[984a9ba] | 291 | async_answer_0(&data, ELIMIT);
|
---|
| 292 | async_answer_0(call, ELIMIT);
|
---|
[59fa7ab] | 293 | return;
|
---|
| 294 | }
|
---|
[a35b458] | 295 |
|
---|
[984a9ba] | 296 | async_data_read_finalize(&data, &scan_results,
|
---|
[59fa7ab] | 297 | sizeof(ieee80211_scan_results_t));
|
---|
| 298 | }
|
---|
[a35b458] | 299 |
|
---|
[984a9ba] | 300 | async_answer_0(call, rc);
|
---|
[59fa7ab] | 301 | }
|
---|
| 302 |
|
---|
| 303 | static void remote_ieee80211_connect(ddf_fun_t *fun, void *iface,
|
---|
[984a9ba] | 304 | ipc_call_t *call)
|
---|
[59fa7ab] | 305 | {
|
---|
| 306 | ieee80211_iface_t *ieee80211_iface = (ieee80211_iface_t *) iface;
|
---|
| 307 | assert(ieee80211_iface->connect);
|
---|
[a35b458] | 308 |
|
---|
[1dcc0b9] | 309 | char ssid_start[MAX_STRING_SIZE];
|
---|
[59fa7ab] | 310 | char password[MAX_STRING_SIZE];
|
---|
[a35b458] | 311 |
|
---|
[984a9ba] | 312 | ipc_call_t data;
|
---|
[59fa7ab] | 313 | size_t len;
|
---|
[984a9ba] | 314 | if (!async_data_write_receive(&data, &len)) {
|
---|
| 315 | async_answer_0(&data, EINVAL);
|
---|
| 316 | async_answer_0(call, EINVAL);
|
---|
[59fa7ab] | 317 | return;
|
---|
| 318 | }
|
---|
[a35b458] | 319 |
|
---|
[59fa7ab] | 320 | if (len > MAX_STRING_SIZE) {
|
---|
[984a9ba] | 321 | async_answer_0(&data, EINVAL);
|
---|
| 322 | async_answer_0(call, EINVAL);
|
---|
[59fa7ab] | 323 | return;
|
---|
| 324 | }
|
---|
[a35b458] | 325 |
|
---|
[984a9ba] | 326 | errno_t rc = async_data_write_finalize(&data, ssid_start, len);
|
---|
[59fa7ab] | 327 | if (rc != EOK) {
|
---|
[984a9ba] | 328 | async_answer_0(&data, EINVAL);
|
---|
| 329 | async_answer_0(call, EINVAL);
|
---|
[59fa7ab] | 330 | return;
|
---|
| 331 | }
|
---|
[a35b458] | 332 |
|
---|
[984a9ba] | 333 | if (!async_data_write_receive(&data, &len)) {
|
---|
| 334 | async_answer_0(&data, EINVAL);
|
---|
| 335 | async_answer_0(call, EINVAL);
|
---|
[59fa7ab] | 336 | return;
|
---|
| 337 | }
|
---|
[a35b458] | 338 |
|
---|
[59fa7ab] | 339 | if (len > MAX_STRING_SIZE) {
|
---|
[984a9ba] | 340 | async_answer_0(&data, EINVAL);
|
---|
| 341 | async_answer_0(call, EINVAL);
|
---|
[59fa7ab] | 342 | return;
|
---|
| 343 | }
|
---|
[a35b458] | 344 |
|
---|
[984a9ba] | 345 | rc = async_data_write_finalize(&data, password, len);
|
---|
[59fa7ab] | 346 | if (rc != EOK) {
|
---|
[984a9ba] | 347 | async_answer_0(&data, EINVAL);
|
---|
| 348 | async_answer_0(call, EINVAL);
|
---|
[59fa7ab] | 349 | return;
|
---|
| 350 | }
|
---|
[a35b458] | 351 |
|
---|
[1dcc0b9] | 352 | rc = ieee80211_iface->connect(fun, ssid_start, password);
|
---|
[a35b458] | 353 |
|
---|
[984a9ba] | 354 | async_answer_0(call, rc);
|
---|
[59fa7ab] | 355 | }
|
---|
| 356 |
|
---|
[1dcc0b9] | 357 | static void remote_ieee80211_disconnect(ddf_fun_t *fun, void *iface,
|
---|
[984a9ba] | 358 | ipc_call_t *call)
|
---|
[1dcc0b9] | 359 | {
|
---|
| 360 | ieee80211_iface_t *ieee80211_iface = (ieee80211_iface_t *) iface;
|
---|
| 361 | assert(ieee80211_iface->disconnect);
|
---|
[b7fd2a0] | 362 | errno_t rc = ieee80211_iface->disconnect(fun);
|
---|
[984a9ba] | 363 | async_answer_0(call, rc);
|
---|
[1dcc0b9] | 364 | }
|
---|
| 365 |
|
---|
[59fa7ab] | 366 | /** Remote IEEE 802.11 interface operations.
|
---|
| 367 | *
|
---|
| 368 | */
|
---|
| 369 | static const remote_iface_func_ptr_t remote_ieee80211_iface_ops[] = {
|
---|
| 370 | [IEEE80211_GET_SCAN_RESULTS] = remote_ieee80211_get_scan_results,
|
---|
[1dcc0b9] | 371 | [IEEE80211_CONNECT] = remote_ieee80211_connect,
|
---|
| 372 | [IEEE80211_DISCONNECT] = remote_ieee80211_disconnect
|
---|
[59fa7ab] | 373 | };
|
---|
| 374 |
|
---|
| 375 | /** Remote IEEE 802.11 interface structure.
|
---|
| 376 | *
|
---|
| 377 | * Interface for processing request from remote
|
---|
| 378 | * clients addressed to the IEEE 802.11 interface.
|
---|
| 379 | *
|
---|
| 380 | */
|
---|
| 381 | const remote_iface_t remote_ieee80211_iface = {
|
---|
| 382 | .method_count = ARRAY_SIZE(remote_ieee80211_iface_ops),
|
---|
| 383 | .methods = remote_ieee80211_iface_ops
|
---|
| 384 | };
|
---|
| 385 |
|
---|
| 386 | /**
|
---|
| 387 | * @}
|
---|
| 388 | */
|
---|