[2093fbe] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jakub Jermar
|
---|
| 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 slip
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief IP over serial line IP link provider.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
[7b64cf0] | 38 | #include <stdint.h>
|
---|
[2093fbe] | 39 | #include <loc.h>
|
---|
[02a09ed] | 40 | #include <inet/addr.h>
|
---|
[2093fbe] | 41 | #include <inet/iplink_srv.h>
|
---|
[5c65e61] | 42 | #include <char_dev_iface.h>
|
---|
[2093fbe] | 43 | #include <io/log.h>
|
---|
| 44 | #include <errno.h>
|
---|
[1c635d6] | 45 | #include <task.h>
|
---|
[2093fbe] | 46 |
|
---|
| 47 | #define NAME "slip"
|
---|
| 48 | #define CAT_IPLINK "iplink"
|
---|
| 49 |
|
---|
| 50 | #define SLIP_MTU 1006 /* as per RFC 1055 */
|
---|
| 51 |
|
---|
[7b64cf0] | 52 | #define SLIP_END 0300
|
---|
| 53 | #define SLIP_ESC 0333
|
---|
[02a09ed] | 54 | #define SLIP_ESC_END 0334
|
---|
[7b64cf0] | 55 | #define SLIP_ESC_ESC 0335
|
---|
| 56 |
|
---|
[2093fbe] | 57 | static int slip_open(iplink_srv_t *);
|
---|
| 58 | static int slip_close(iplink_srv_t *);
|
---|
[02a09ed] | 59 | static int slip_send(iplink_srv_t *, iplink_sdu_t *);
|
---|
[a17356fd] | 60 | static int slip_send6(iplink_srv_t *, iplink_sdu6_t *);
|
---|
[2093fbe] | 61 | static int slip_get_mtu(iplink_srv_t *, size_t *);
|
---|
[a17356fd] | 62 | static int slip_get_mac48(iplink_srv_t *, addr48_t *);
|
---|
[02a09ed] | 63 | static int slip_addr_add(iplink_srv_t *, inet_addr_t *);
|
---|
| 64 | static int slip_addr_remove(iplink_srv_t *, inet_addr_t *);
|
---|
[2093fbe] | 65 |
|
---|
| 66 | static iplink_srv_t slip_iplink;
|
---|
| 67 |
|
---|
| 68 | static iplink_ops_t slip_iplink_ops = {
|
---|
| 69 | .open = slip_open,
|
---|
| 70 | .close = slip_close,
|
---|
| 71 | .send = slip_send,
|
---|
[a17356fd] | 72 | .send6 = slip_send6,
|
---|
[2093fbe] | 73 | .get_mtu = slip_get_mtu,
|
---|
[a17356fd] | 74 | .get_mac48 = slip_get_mac48,
|
---|
[2093fbe] | 75 | .addr_add = slip_addr_add,
|
---|
| 76 | .addr_remove = slip_addr_remove
|
---|
| 77 | };
|
---|
| 78 |
|
---|
[7b64cf0] | 79 | static uint8_t slip_send_buf[SLIP_MTU + 2];
|
---|
| 80 | static size_t slip_send_pending;
|
---|
| 81 |
|
---|
| 82 | static uint8_t slip_recv_buf[SLIP_MTU + 2];
|
---|
| 83 | static size_t slip_recv_pending;
|
---|
| 84 | static size_t slip_recv_read;
|
---|
| 85 |
|
---|
[2093fbe] | 86 | int slip_open(iplink_srv_t *srv)
|
---|
| 87 | {
|
---|
| 88 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_open()");
|
---|
| 89 | return EOK;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | int slip_close(iplink_srv_t *srv)
|
---|
| 93 | {
|
---|
| 94 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_close()");
|
---|
| 95 | return EOK;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[7b64cf0] | 98 | static void write_flush(async_sess_t *sess)
|
---|
| 99 | {
|
---|
| 100 | size_t written = 0;
|
---|
| 101 |
|
---|
| 102 | while (slip_send_pending > 0) {
|
---|
| 103 | ssize_t size;
|
---|
| 104 |
|
---|
| 105 | size = char_dev_write(sess, &slip_send_buf[written],
|
---|
| 106 | slip_send_pending);
|
---|
| 107 | if (size < 0) {
|
---|
| 108 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 109 | "char_dev_write() returned %d",
|
---|
| 110 | (int) size);
|
---|
| 111 | slip_send_pending = 0;
|
---|
| 112 | break;
|
---|
| 113 | }
|
---|
| 114 | written += size;
|
---|
| 115 | slip_send_pending -= size;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | static void write_buffered(async_sess_t *sess, uint8_t ch)
|
---|
| 120 | {
|
---|
| 121 | if (slip_send_pending == sizeof(slip_send_buf))
|
---|
| 122 | write_flush(sess);
|
---|
| 123 | slip_send_buf[slip_send_pending++] = ch;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[02a09ed] | 126 | int slip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
|
---|
[2093fbe] | 127 | {
|
---|
[a17356fd] | 128 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_send()");
|
---|
| 129 |
|
---|
[7b64cf0] | 130 | async_sess_t *sess = (async_sess_t *) srv->arg;
|
---|
| 131 | uint8_t *data = sdu->data;
|
---|
[a17356fd] | 132 |
|
---|
[7b64cf0] | 133 | /*
|
---|
[a17356fd] | 134 | * Strictly speaking, this is not prescribed by the RFC, but the RFC
|
---|
| 135 | * suggests to start with sending a SLIP_END byte as a synchronization
|
---|
| 136 | * measure for dealing with previous possible noise on the line.
|
---|
| 137 | */
|
---|
[7b64cf0] | 138 | write_buffered(sess, SLIP_END);
|
---|
[a17356fd] | 139 |
|
---|
| 140 | for (size_t i = 0; i < sdu->size; i++) {
|
---|
[7b64cf0] | 141 | switch (data[i]) {
|
---|
| 142 | case SLIP_END:
|
---|
| 143 | write_buffered(sess, SLIP_ESC);
|
---|
[02a09ed] | 144 | write_buffered(sess, SLIP_ESC_END);
|
---|
[7b64cf0] | 145 | break;
|
---|
| 146 | case SLIP_ESC:
|
---|
| 147 | write_buffered(sess, SLIP_ESC);
|
---|
| 148 | write_buffered(sess, SLIP_ESC_ESC);
|
---|
| 149 | break;
|
---|
| 150 | default:
|
---|
| 151 | write_buffered(sess, data[i]);
|
---|
| 152 | break;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
[a17356fd] | 155 |
|
---|
[7b64cf0] | 156 | write_buffered(sess, SLIP_END);
|
---|
| 157 | write_flush(sess);
|
---|
[a17356fd] | 158 |
|
---|
[7b64cf0] | 159 | return EOK;
|
---|
[2093fbe] | 160 | }
|
---|
| 161 |
|
---|
[a17356fd] | 162 | int slip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu)
|
---|
| 163 | {
|
---|
| 164 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_send6()");
|
---|
| 165 |
|
---|
| 166 | return ENOTSUP;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[2093fbe] | 169 | int slip_get_mtu(iplink_srv_t *srv, size_t *mtu)
|
---|
| 170 | {
|
---|
| 171 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_get_mtu()");
|
---|
| 172 | *mtu = SLIP_MTU;
|
---|
| 173 | return EOK;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[a17356fd] | 176 | int slip_get_mac48(iplink_srv_t *src, addr48_t *mac)
|
---|
| 177 | {
|
---|
| 178 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_get_mac48()");
|
---|
| 179 | return ENOTSUP;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[02a09ed] | 182 | int slip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
|
---|
[2093fbe] | 183 | {
|
---|
| 184 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_add()");
|
---|
| 185 | return EOK;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[02a09ed] | 188 | int slip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
|
---|
[2093fbe] | 189 | {
|
---|
| 190 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_remove()");
|
---|
| 191 | return EOK;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | static void usage(void)
|
---|
| 195 | {
|
---|
| 196 | printf("Usage: " NAME " <service-name> <link-name>\n");
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | static void slip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 200 | {
|
---|
| 201 | log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_client_conn()");
|
---|
| 202 | iplink_conn(iid, icall, &slip_iplink);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[7b64cf0] | 205 | static uint8_t read_buffered(async_sess_t *sess)
|
---|
| 206 | {
|
---|
| 207 | while (slip_recv_pending == 0) {
|
---|
| 208 | ssize_t size;
|
---|
| 209 |
|
---|
| 210 | size = char_dev_read(sess, slip_recv_buf,
|
---|
| 211 | sizeof(slip_recv_buf));
|
---|
| 212 | if (size < 0) {
|
---|
| 213 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 214 | "char_dev_read() returned %d", (int) size);
|
---|
| 215 | return SLIP_END;
|
---|
| 216 | }
|
---|
| 217 | slip_recv_pending = size;
|
---|
| 218 | slip_recv_read = 0;
|
---|
| 219 | }
|
---|
| 220 | slip_recv_pending--;
|
---|
| 221 | return slip_recv_buf[slip_recv_read++];
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | static int slip_recv_fibril(void *arg)
|
---|
| 225 | {
|
---|
| 226 | async_sess_t *sess = (async_sess_t *) arg;
|
---|
| 227 | static uint8_t recv_final[SLIP_MTU];
|
---|
[02a09ed] | 228 | iplink_recv_sdu_t sdu;
|
---|
[7b64cf0] | 229 | uint8_t ch;
|
---|
| 230 | int rc;
|
---|
| 231 |
|
---|
| 232 | sdu.data = recv_final;
|
---|
| 233 |
|
---|
| 234 | while (true) {
|
---|
| 235 | for (sdu.size = 0; sdu.size < sizeof(recv_final); /**/) {
|
---|
| 236 | ch = read_buffered(sess);
|
---|
| 237 | switch (ch) {
|
---|
| 238 | case SLIP_END:
|
---|
| 239 | if (sdu.size == 0) {
|
---|
| 240 | /*
|
---|
[02a09ed] | 241 | * Discard the empty SLIP datagram.
|
---|
| 242 | */
|
---|
[7b64cf0] | 243 | break;
|
---|
| 244 | }
|
---|
| 245 | goto pass;
|
---|
| 246 |
|
---|
| 247 | case SLIP_ESC:
|
---|
| 248 | ch = read_buffered(sess);
|
---|
| 249 | if (ch == SLIP_ESC_END) {
|
---|
| 250 | recv_final[sdu.size++] = SLIP_END;
|
---|
| 251 | break;
|
---|
| 252 | } else if (ch == SLIP_ESC_ESC) {
|
---|
| 253 | recv_final[sdu.size++] = SLIP_ESC;
|
---|
| 254 | break;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | /*
|
---|
[e141281] | 258 | * The RFC suggests to simply insert the wrongly
|
---|
| 259 | * escaped character into the packet so we fall
|
---|
| 260 | * through.
|
---|
| 261 | */
|
---|
[dc12262] | 262 | /* Fallthrough */
|
---|
[e141281] | 263 |
|
---|
[7b64cf0] | 264 | default:
|
---|
| 265 | recv_final[sdu.size++] = ch;
|
---|
| 266 | break;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | /*
|
---|
| 272 | * We have reached the limit of our MTU. Regardless of whether
|
---|
| 273 | * the datagram is properly ended with SLIP_END, pass it along.
|
---|
| 274 | * If the next character is really SLIP_END, nothing
|
---|
| 275 | * catastrophic happens. The algorithm will just see an
|
---|
| 276 | * artificially empty SLIP datagram and life will go on.
|
---|
| 277 | */
|
---|
| 278 |
|
---|
| 279 | pass:
|
---|
[417a2ba1] | 280 | rc = iplink_ev_recv(&slip_iplink, &sdu, ip_v4);
|
---|
[7b64cf0] | 281 | if (rc != EOK) {
|
---|
| 282 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 283 | "iplink_ev_recv() returned %d", rc);
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | return 0;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[2093fbe] | 290 | static int slip_init(const char *svcstr, const char *linkstr)
|
---|
| 291 | {
|
---|
| 292 | service_id_t svcid;
|
---|
| 293 | service_id_t linksid;
|
---|
| 294 | category_id_t iplinkcid;
|
---|
[3abf0760] | 295 | async_sess_t *sess_in = NULL;
|
---|
| 296 | async_sess_t *sess_out = NULL;
|
---|
[7b64cf0] | 297 | fid_t fid;
|
---|
[2093fbe] | 298 | int rc;
|
---|
| 299 |
|
---|
| 300 | iplink_srv_init(&slip_iplink);
|
---|
| 301 | slip_iplink.ops = &slip_iplink_ops;
|
---|
| 302 |
|
---|
[b688fd8] | 303 | async_set_fallback_port_handler(slip_client_conn, NULL);
|
---|
[2093fbe] | 304 |
|
---|
| 305 | rc = loc_server_register(NAME);
|
---|
| 306 | if (rc != EOK) {
|
---|
| 307 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 308 | "Failed registering server.");
|
---|
| 309 | return rc;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | rc = loc_service_get_id(svcstr, &svcid, 0);
|
---|
| 313 | if (rc != EOK) {
|
---|
| 314 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 315 | "Failed getting ID for service %s", svcstr);
|
---|
| 316 | return rc;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | rc = loc_category_get_id(CAT_IPLINK, &iplinkcid, 0);
|
---|
| 320 | if (rc != EOK) {
|
---|
| 321 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 322 | "Failed to get category ID for %s",
|
---|
| 323 | CAT_IPLINK);
|
---|
| 324 | return rc;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[7b64cf0] | 327 | /*
|
---|
[3abf0760] | 328 | * Create two sessions to allow to both read and write from the
|
---|
| 329 | * char_dev at the same time.
|
---|
[7b64cf0] | 330 | */
|
---|
[f9b2cb4c] | 331 | sess_out = loc_service_connect(svcid, INTERFACE_DDF, 0);
|
---|
[3abf0760] | 332 | if (!sess_out) {
|
---|
[2093fbe] | 333 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 334 | "Failed to connect to service %s (ID=%d)",
|
---|
| 335 | svcstr, (int) svcid);
|
---|
[b11f6fb] | 336 | return ENOENT;
|
---|
[2093fbe] | 337 | }
|
---|
[3abf0760] | 338 | slip_iplink.arg = sess_out;
|
---|
| 339 |
|
---|
[f9b2cb4c] | 340 | sess_in = loc_service_connect(svcid, INTERFACE_DDF, 0);
|
---|
[3abf0760] | 341 | if (!sess_in) {
|
---|
| 342 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 343 | "Failed to connect to service %s (ID=%d)",
|
---|
| 344 | svcstr, (int) svcid);
|
---|
[b11f6fb] | 345 | rc = ENOENT;
|
---|
[3abf0760] | 346 | goto fail;
|
---|
| 347 | }
|
---|
[2093fbe] | 348 |
|
---|
| 349 | rc = loc_service_register(linkstr, &linksid);
|
---|
| 350 | if (rc != EOK) {
|
---|
| 351 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 352 | "Failed to register service %s",
|
---|
| 353 | linkstr);
|
---|
| 354 | goto fail;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | rc = loc_service_add_to_cat(linksid, iplinkcid);
|
---|
| 358 | if (rc != EOK) {
|
---|
| 359 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
[7b64cf0] | 360 | "Failed to add service %d (%s) to category %d (%s).",
|
---|
[2093fbe] | 361 | (int) linksid, linkstr, (int) iplinkcid, CAT_IPLINK);
|
---|
| 362 | goto fail;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[3abf0760] | 365 | fid = fibril_create(slip_recv_fibril, sess_in);
|
---|
[7b64cf0] | 366 | if (!fid) {
|
---|
| 367 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
| 368 | "Failed to create receive fibril.");
|
---|
[b11f6fb] | 369 | rc = ENOENT;
|
---|
[7b64cf0] | 370 | goto fail;
|
---|
| 371 | }
|
---|
| 372 | fibril_add_ready(fid);
|
---|
| 373 |
|
---|
[2093fbe] | 374 | return EOK;
|
---|
| 375 |
|
---|
| 376 | fail:
|
---|
[3abf0760] | 377 | if (sess_out)
|
---|
| 378 | async_hangup(sess_out);
|
---|
| 379 | if (sess_in)
|
---|
| 380 | async_hangup(sess_in);
|
---|
[2093fbe] | 381 |
|
---|
| 382 | /*
|
---|
| 383 | * We assume that our registration at the location service will be
|
---|
| 384 | * cleaned up automatically as the service (i.e. this task) terminates.
|
---|
| 385 | */
|
---|
| 386 |
|
---|
| 387 | return rc;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | int main(int argc, char *argv[])
|
---|
| 391 | {
|
---|
| 392 | int rc;
|
---|
| 393 |
|
---|
| 394 | printf(NAME ": IP over serial line service\n");
|
---|
| 395 |
|
---|
| 396 | if (argc != 3) {
|
---|
| 397 | usage();
|
---|
| 398 | return EINVAL;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | rc = log_init(NAME);
|
---|
| 402 | if (rc != EOK) {
|
---|
| 403 | printf(NAME ": failed to initialize log\n");
|
---|
| 404 | return rc;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | rc = slip_init(argv[1], argv[2]);
|
---|
| 408 | if (rc != EOK)
|
---|
| 409 | return 1;
|
---|
| 410 |
|
---|
| 411 | printf(NAME ": Accepting connections.\n");
|
---|
| 412 | task_retval(0);
|
---|
| 413 | async_manager();
|
---|
| 414 |
|
---|
| 415 | /* not reached */
|
---|
| 416 | return 0;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | /** @}
|
---|
| 420 | */
|
---|