[c76e926] | 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 inet
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief Internet Protocol service
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <adt/list.h>
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <fibril_synch.h>
|
---|
| 41 | #include <io/log.h>
|
---|
| 42 | #include <ipc/inet.h>
|
---|
| 43 | #include <ipc/services.h>
|
---|
| 44 | #include <loc.h>
|
---|
| 45 | #include <stdio.h>
|
---|
[ecff3d9] | 46 | #include <stdlib.h>
|
---|
[c76e926] | 47 | #include <sys/types.h>
|
---|
| 48 |
|
---|
[ceba4bed] | 49 | #include "addrobj.h"
|
---|
[59157eb] | 50 | #include "inet.h"
|
---|
[e2e56e67] | 51 | #include "inet_link.h"
|
---|
[c76e926] | 52 |
|
---|
[59157eb] | 53 | #define NAME "inet"
|
---|
[c76e926] | 54 |
|
---|
| 55 | static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
| 56 |
|
---|
| 57 | static FIBRIL_MUTEX_INITIALIZE(client_list_lock);
|
---|
| 58 | static LIST_INITIALIZE(client_list);
|
---|
| 59 |
|
---|
| 60 | static int inet_init(void)
|
---|
| 61 | {
|
---|
| 62 | service_id_t sid;
|
---|
| 63 | int rc;
|
---|
| 64 |
|
---|
| 65 | log_msg(LVL_DEBUG, "inet_init()");
|
---|
| 66 |
|
---|
| 67 | async_set_client_connection(inet_client_conn);
|
---|
| 68 |
|
---|
| 69 | rc = loc_server_register(NAME);
|
---|
| 70 | if (rc != EOK) {
|
---|
| 71 | log_msg(LVL_ERROR, "Failed registering server (%d).", rc);
|
---|
| 72 | return EEXIST;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | rc = loc_service_register(SERVICE_NAME_INET, &sid);
|
---|
| 76 | if (rc != EOK) {
|
---|
| 77 | log_msg(LVL_ERROR, "Failed registering service (%d).", rc);
|
---|
| 78 | return EEXIST;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[e2e56e67] | 81 | rc = inet_link_discovery_start();
|
---|
| 82 | if (rc != EOK)
|
---|
| 83 | return EEXIST;
|
---|
| 84 |
|
---|
[c76e926] | 85 | return EOK;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[ceba4bed] | 88 | static void inet_callback_create_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
[c76e926] | 89 | ipc_call_t *call)
|
---|
| 90 | {
|
---|
[ceba4bed] | 91 | log_msg(LVL_DEBUG, "inet_callback_create_srv()");
|
---|
[c76e926] | 92 |
|
---|
| 93 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 94 | if (sess == NULL) {
|
---|
| 95 | async_answer_0(callid, ENOMEM);
|
---|
| 96 | return;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | client->sess = sess;
|
---|
| 100 | async_answer_0(callid, EOK);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[e767dbf] | 103 | static int inet_route_packet(inet_dgram_t *dgram, uint8_t ttl, int df)
|
---|
[ceba4bed] | 104 | {
|
---|
| 105 | inet_addrobj_t *addr;
|
---|
| 106 |
|
---|
[e767dbf] | 107 | addr = inet_addrobj_find(&dgram->dest, iaf_net);
|
---|
[ceba4bed] | 108 | if (addr != NULL) {
|
---|
| 109 | /* Destination is directly accessible */
|
---|
| 110 | return inet_addrobj_send_dgram(addr, dgram, ttl, df);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /* TODO: Gateways */
|
---|
| 114 | log_msg(LVL_DEBUG, "inet_send: No route to destination.");
|
---|
| 115 | return ENOENT;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[e767dbf] | 118 | static int inet_send(inet_client_t *client, inet_dgram_t *dgram,
|
---|
| 119 | uint8_t ttl, int df)
|
---|
| 120 | {
|
---|
| 121 | return inet_route_packet(dgram, ttl, df);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[ceba4bed] | 124 | static void inet_get_srcaddr_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
[ecff3d9] | 125 | ipc_call_t *call)
|
---|
| 126 | {
|
---|
[ceba4bed] | 127 | log_msg(LVL_DEBUG, "inet_get_srcaddr_srv()");
|
---|
[ecff3d9] | 128 |
|
---|
| 129 | async_answer_0(callid, ENOTSUP);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[ceba4bed] | 132 | static void inet_send_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
[c76e926] | 133 | ipc_call_t *call)
|
---|
| 134 | {
|
---|
[59157eb] | 135 | inet_dgram_t dgram;
|
---|
[ecff3d9] | 136 | uint8_t ttl;
|
---|
| 137 | int df;
|
---|
| 138 | int rc;
|
---|
| 139 |
|
---|
[ceba4bed] | 140 | log_msg(LVL_DEBUG, "inet_send_srv()");
|
---|
[ecff3d9] | 141 |
|
---|
[59157eb] | 142 | dgram.src.ipv4 = IPC_GET_ARG1(*call);
|
---|
| 143 | dgram.dest.ipv4 = IPC_GET_ARG2(*call);
|
---|
| 144 | dgram.tos = IPC_GET_ARG3(*call);
|
---|
[ecff3d9] | 145 | ttl = IPC_GET_ARG4(*call);
|
---|
| 146 | df = IPC_GET_ARG5(*call);
|
---|
| 147 |
|
---|
[59157eb] | 148 | rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size);
|
---|
[ecff3d9] | 149 | if (rc != EOK) {
|
---|
| 150 | async_answer_0(callid, rc);
|
---|
| 151 | return;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[ceba4bed] | 154 | rc = inet_send(client, &dgram, ttl, df);
|
---|
| 155 |
|
---|
[59157eb] | 156 | free(dgram.data);
|
---|
[ceba4bed] | 157 | async_answer_0(callid, rc);
|
---|
[c76e926] | 158 | }
|
---|
| 159 |
|
---|
[ceba4bed] | 160 | static void inet_set_proto_srv(inet_client_t *client, ipc_callid_t callid,
|
---|
[c76e926] | 161 | ipc_call_t *call)
|
---|
| 162 | {
|
---|
| 163 | sysarg_t proto;
|
---|
| 164 |
|
---|
| 165 | proto = IPC_GET_ARG1(*call);
|
---|
[ceba4bed] | 166 | log_msg(LVL_DEBUG, "inet_set_proto_srv(%lu)", (unsigned long) proto);
|
---|
[c76e926] | 167 |
|
---|
| 168 | if (proto > UINT8_MAX) {
|
---|
| 169 | async_answer_0(callid, EINVAL);
|
---|
| 170 | return;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | client->protocol = proto;
|
---|
| 174 | async_answer_0(callid, EOK);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | static void inet_client_init(inet_client_t *client)
|
---|
| 178 | {
|
---|
| 179 | client->sess = NULL;
|
---|
| 180 |
|
---|
| 181 | fibril_mutex_lock(&client_list_lock);
|
---|
| 182 | list_append(&client->client_list, &client_list);
|
---|
| 183 | fibril_mutex_unlock(&client_list_lock);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | static void inet_client_fini(inet_client_t *client)
|
---|
| 187 | {
|
---|
| 188 | async_hangup(client->sess);
|
---|
| 189 | client->sess = NULL;
|
---|
| 190 |
|
---|
| 191 | fibril_mutex_lock(&client_list_lock);
|
---|
| 192 | list_remove(&client->client_list);
|
---|
| 193 | fibril_mutex_unlock(&client_list_lock);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | static void inet_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 197 | {
|
---|
| 198 | inet_client_t client;
|
---|
| 199 |
|
---|
| 200 | log_msg(LVL_DEBUG, "inet_client_conn()");
|
---|
| 201 |
|
---|
| 202 | /* Accept the connection */
|
---|
| 203 | async_answer_0(iid, EOK);
|
---|
| 204 |
|
---|
| 205 | inet_client_init(&client);
|
---|
| 206 |
|
---|
| 207 | while (true) {
|
---|
| 208 | ipc_call_t call;
|
---|
| 209 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 210 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 211 |
|
---|
| 212 | if (!method) {
|
---|
| 213 | /* The other side has hung up */
|
---|
| 214 | async_answer_0(callid, EOK);
|
---|
| 215 | return;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | switch (method) {
|
---|
| 219 | case INET_CALLBACK_CREATE:
|
---|
[ceba4bed] | 220 | inet_callback_create_srv(&client, callid, &call);
|
---|
[c76e926] | 221 | break;
|
---|
[ecff3d9] | 222 | case INET_GET_SRCADDR:
|
---|
[ceba4bed] | 223 | inet_get_srcaddr_srv(&client, callid, &call);
|
---|
[ecff3d9] | 224 | break;
|
---|
[c76e926] | 225 | case INET_SEND:
|
---|
[ceba4bed] | 226 | inet_send_srv(&client, callid, &call);
|
---|
[c76e926] | 227 | break;
|
---|
| 228 | case INET_SET_PROTO:
|
---|
[ceba4bed] | 229 | inet_set_proto_srv(&client, callid, &call);
|
---|
[c76e926] | 230 | break;
|
---|
| 231 | default:
|
---|
| 232 | async_answer_0(callid, EINVAL);
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | inet_client_fini(&client);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[59157eb] | 239 | int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram)
|
---|
| 240 | {
|
---|
| 241 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
| 242 |
|
---|
| 243 | ipc_call_t answer;
|
---|
| 244 | aid_t req = async_send_3(exch, INET_EV_RECV, dgram->src.ipv4,
|
---|
| 245 | dgram->dest.ipv4, dgram->tos, &answer);
|
---|
| 246 | int rc = async_data_write_start(exch, dgram->data, dgram->size);
|
---|
| 247 | async_exchange_end(exch);
|
---|
| 248 |
|
---|
| 249 | if (rc != EOK) {
|
---|
| 250 | async_wait_for(req, NULL);
|
---|
| 251 | return rc;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | sysarg_t retval;
|
---|
| 255 | async_wait_for(req, &retval);
|
---|
| 256 | if (retval != EOK)
|
---|
| 257 | return retval;
|
---|
| 258 |
|
---|
| 259 | return EOK;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[e767dbf] | 262 | int inet_recv_packet(inet_dgram_t *dgram, uint8_t ttl, int df)
|
---|
| 263 | {
|
---|
| 264 | return inet_route_packet(dgram, ttl, df);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[c76e926] | 267 | int main(int argc, char *argv[])
|
---|
| 268 | {
|
---|
| 269 | int rc;
|
---|
| 270 |
|
---|
[e2e56e67] | 271 | printf(NAME ": HelenOS Internet Protocol service\n");
|
---|
[c76e926] | 272 |
|
---|
| 273 | if (log_init(NAME, LVL_DEBUG) != EOK) {
|
---|
[e2e56e67] | 274 | printf(NAME ": Failed to initialize logging.\n");
|
---|
[c76e926] | 275 | return 1;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | rc = inet_init();
|
---|
| 279 | if (rc != EOK)
|
---|
| 280 | return 1;
|
---|
| 281 |
|
---|
[ecff3d9] | 282 | printf(NAME ": Accepting connections.\n");
|
---|
[c76e926] | 283 | task_retval(0);
|
---|
| 284 | async_manager();
|
---|
| 285 |
|
---|
| 286 | /* Not reached */
|
---|
| 287 | return 0;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /** @}
|
---|
| 291 | */
|
---|