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