| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Jiri Svoboda
|
|---|
| 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 loopip
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief Loopback IP link provider
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <adt/prodcons.h>
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <inet/iplink_srv.h>
|
|---|
| 42 | #include <inet/addr.h>
|
|---|
| 43 | #include <io/log.h>
|
|---|
| 44 | #include <loc.h>
|
|---|
| 45 | #include <stdio.h>
|
|---|
| 46 | #include <stdlib.h>
|
|---|
| 47 | #include <task.h>
|
|---|
| 48 |
|
|---|
| 49 | #define NAME "loopip"
|
|---|
| 50 |
|
|---|
| 51 | static int loopip_open(iplink_srv_t *srv);
|
|---|
| 52 | static int loopip_close(iplink_srv_t *srv);
|
|---|
| 53 | static int loopip_send(iplink_srv_t *srv, iplink_sdu_t *sdu);
|
|---|
| 54 | static int loopip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu);
|
|---|
| 55 | static int loopip_get_mtu(iplink_srv_t *srv, size_t *mtu);
|
|---|
| 56 | static int loopip_get_mac48(iplink_srv_t *srv, addr48_t *mac);
|
|---|
| 57 | static int loopip_addr_add(iplink_srv_t *srv, inet_addr_t *addr);
|
|---|
| 58 | static int loopip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr);
|
|---|
| 59 |
|
|---|
| 60 | static void loopip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
|---|
| 61 |
|
|---|
| 62 | static iplink_ops_t loopip_iplink_ops = {
|
|---|
| 63 | .open = loopip_open,
|
|---|
| 64 | .close = loopip_close,
|
|---|
| 65 | .send = loopip_send,
|
|---|
| 66 | .send6 = loopip_send6,
|
|---|
| 67 | .get_mtu = loopip_get_mtu,
|
|---|
| 68 | .get_mac48 = loopip_get_mac48,
|
|---|
| 69 | .addr_add = loopip_addr_add,
|
|---|
| 70 | .addr_remove = loopip_addr_remove
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | static iplink_srv_t loopip_iplink;
|
|---|
| 74 | static prodcons_t loopip_rcv_queue;
|
|---|
| 75 |
|
|---|
| 76 | typedef struct {
|
|---|
| 77 | link_t link;
|
|---|
| 78 |
|
|---|
| 79 | /* XXX Version should be part of SDU */
|
|---|
| 80 | ip_ver_t ver;
|
|---|
| 81 | iplink_recv_sdu_t sdu;
|
|---|
| 82 | } rqueue_entry_t;
|
|---|
| 83 |
|
|---|
| 84 | static int loopip_recv_fibril(void *arg)
|
|---|
| 85 | {
|
|---|
| 86 | while (true) {
|
|---|
| 87 | log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_recv_fibril(): Wait for one item");
|
|---|
| 88 | link_t *link = prodcons_consume(&loopip_rcv_queue);
|
|---|
| 89 | rqueue_entry_t *rqe =
|
|---|
| 90 | list_get_instance(link, rqueue_entry_t, link);
|
|---|
| 91 |
|
|---|
| 92 | (void) iplink_ev_recv(&loopip_iplink, &rqe->sdu, rqe->ver);
|
|---|
| 93 |
|
|---|
| 94 | free(rqe->sdu.data);
|
|---|
| 95 | free(rqe);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | return 0;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static int loopip_init(void)
|
|---|
| 102 | {
|
|---|
| 103 | async_set_fallback_port_handler(loopip_client_conn, NULL);
|
|---|
| 104 |
|
|---|
| 105 | int rc = loc_server_register(NAME);
|
|---|
| 106 | if (rc != EOK) {
|
|---|
| 107 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server.");
|
|---|
| 108 | return rc;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | iplink_srv_init(&loopip_iplink);
|
|---|
| 112 | loopip_iplink.ops = &loopip_iplink_ops;
|
|---|
| 113 | loopip_iplink.arg = NULL;
|
|---|
| 114 |
|
|---|
| 115 | prodcons_initialize(&loopip_rcv_queue);
|
|---|
| 116 |
|
|---|
| 117 | const char *svc_name = "net/loopback";
|
|---|
| 118 | service_id_t sid;
|
|---|
| 119 | rc = loc_service_register(svc_name, &sid);
|
|---|
| 120 | if (rc != EOK) {
|
|---|
| 121 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.",
|
|---|
| 122 | svc_name);
|
|---|
| 123 | return rc;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | category_id_t iplink_cat;
|
|---|
| 127 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
|---|
| 128 | if (rc != EOK) {
|
|---|
| 129 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
|
|---|
| 130 | return rc;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | rc = loc_service_add_to_cat(sid, iplink_cat);
|
|---|
| 134 | if (rc != EOK) {
|
|---|
| 135 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.",
|
|---|
| 136 | svc_name);
|
|---|
| 137 | return rc;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | fid_t fid = fibril_create(loopip_recv_fibril, NULL);
|
|---|
| 141 | if (fid == 0)
|
|---|
| 142 | return ENOMEM;
|
|---|
| 143 |
|
|---|
| 144 | fibril_add_ready(fid);
|
|---|
| 145 |
|
|---|
| 146 | return EOK;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | static void loopip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 150 | {
|
|---|
| 151 | log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_client_conn()");
|
|---|
| 152 | iplink_conn(iid, icall, &loopip_iplink);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | static int loopip_open(iplink_srv_t *srv)
|
|---|
| 156 | {
|
|---|
| 157 | log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_open()");
|
|---|
| 158 | return EOK;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | static int loopip_close(iplink_srv_t *srv)
|
|---|
| 162 | {
|
|---|
| 163 | log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_close()");
|
|---|
| 164 | return EOK;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | static int loopip_send(iplink_srv_t *srv, iplink_sdu_t *sdu)
|
|---|
| 168 | {
|
|---|
| 169 | log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_send()");
|
|---|
| 170 |
|
|---|
| 171 | rqueue_entry_t *rqe = calloc(1, sizeof(rqueue_entry_t));
|
|---|
| 172 | if (rqe == NULL)
|
|---|
| 173 | return ENOMEM;
|
|---|
| 174 |
|
|---|
| 175 | /*
|
|---|
| 176 | * Clone SDU
|
|---|
| 177 | */
|
|---|
| 178 | rqe->ver = ip_v4;
|
|---|
| 179 | rqe->sdu.data = malloc(sdu->size);
|
|---|
| 180 | if (rqe->sdu.data == NULL) {
|
|---|
| 181 | free(rqe);
|
|---|
| 182 | return ENOMEM;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | memcpy(rqe->sdu.data, sdu->data, sdu->size);
|
|---|
| 186 | rqe->sdu.size = sdu->size;
|
|---|
| 187 |
|
|---|
| 188 | /*
|
|---|
| 189 | * Insert to receive queue
|
|---|
| 190 | */
|
|---|
| 191 | prodcons_produce(&loopip_rcv_queue, &rqe->link);
|
|---|
| 192 |
|
|---|
| 193 | return EOK;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | static int loopip_send6(iplink_srv_t *srv, iplink_sdu6_t *sdu)
|
|---|
| 197 | {
|
|---|
| 198 | log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip6_send()");
|
|---|
| 199 |
|
|---|
| 200 | rqueue_entry_t *rqe = calloc(1, sizeof(rqueue_entry_t));
|
|---|
| 201 | if (rqe == NULL)
|
|---|
| 202 | return ENOMEM;
|
|---|
| 203 |
|
|---|
| 204 | /*
|
|---|
| 205 | * Clone SDU
|
|---|
| 206 | */
|
|---|
| 207 | rqe->ver = ip_v6;
|
|---|
| 208 | rqe->sdu.data = malloc(sdu->size);
|
|---|
| 209 | if (rqe->sdu.data == NULL) {
|
|---|
| 210 | free(rqe);
|
|---|
| 211 | return ENOMEM;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | memcpy(rqe->sdu.data, sdu->data, sdu->size);
|
|---|
| 215 | rqe->sdu.size = sdu->size;
|
|---|
| 216 |
|
|---|
| 217 | /*
|
|---|
| 218 | * Insert to receive queue
|
|---|
| 219 | */
|
|---|
| 220 | prodcons_produce(&loopip_rcv_queue, &rqe->link);
|
|---|
| 221 |
|
|---|
| 222 | return EOK;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | static int loopip_get_mtu(iplink_srv_t *srv, size_t *mtu)
|
|---|
| 226 | {
|
|---|
| 227 | log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_get_mtu()");
|
|---|
| 228 | *mtu = 1500;
|
|---|
| 229 | return EOK;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | static int loopip_get_mac48(iplink_srv_t *src, addr48_t *mac)
|
|---|
| 233 | {
|
|---|
| 234 | log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_get_mac48()");
|
|---|
| 235 | return ENOTSUP;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | static int loopip_addr_add(iplink_srv_t *srv, inet_addr_t *addr)
|
|---|
| 239 | {
|
|---|
| 240 | return EOK;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | static int loopip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr)
|
|---|
| 244 | {
|
|---|
| 245 | return EOK;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | int main(int argc, char *argv[])
|
|---|
| 249 | {
|
|---|
| 250 | printf("%s: HelenOS loopback IP link provider\n", NAME);
|
|---|
| 251 |
|
|---|
| 252 | int rc = log_init(NAME);
|
|---|
| 253 | if (rc != EOK) {
|
|---|
| 254 | printf("%s: Failed to initialize logging: %s.\n", NAME, str_error(rc));
|
|---|
| 255 | return rc;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | rc = loopip_init();
|
|---|
| 259 | if (rc != EOK) {
|
|---|
| 260 | printf("%s: Failed to initialize loopip: %s.\n", NAME, str_error(rc));
|
|---|
| 261 | return rc;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | printf("%s: Accepting connections.\n", NAME);
|
|---|
| 265 | task_retval(0);
|
|---|
| 266 | async_manager();
|
|---|
| 267 |
|
|---|
| 268 | /* Not reached */
|
|---|
| 269 | return 0;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /** @}
|
|---|
| 273 | */
|
|---|