[e2e56e67] | 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
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[3e6a98c5] | 37 | #include <stdbool.h>
|
---|
[e2e56e67] | 38 | #include <errno.h>
|
---|
| 39 | #include <fibril_synch.h>
|
---|
| 40 | #include <inet/iplink.h>
|
---|
| 41 | #include <io/log.h>
|
---|
| 42 | #include <loc.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
[0e94b979] | 44 | #include <str.h>
|
---|
[02a09ed] | 45 | #include <net/socket_codes.h>
|
---|
[ceba4bed] | 46 | #include "addrobj.h"
|
---|
[b4ec1ea] | 47 | #include "inetsrv.h"
|
---|
[e2e56e67] | 48 | #include "inet_link.h"
|
---|
[ceba4bed] | 49 | #include "pdu.h"
|
---|
[e2e56e67] | 50 |
|
---|
[02a09ed] | 51 | static int inet_link_open(service_id_t);
|
---|
| 52 | static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, uint16_t);
|
---|
[e2e56e67] | 53 |
|
---|
| 54 | static iplink_ev_ops_t inet_iplink_ev_ops = {
|
---|
| 55 | .recv = inet_iplink_recv
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | static LIST_INITIALIZE(inet_link_list);
|
---|
| 59 | static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock);
|
---|
| 60 |
|
---|
[02a09ed] | 61 | static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, uint16_t af)
|
---|
[e2e56e67] | 62 | {
|
---|
[a1a101d] | 63 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
|
---|
[02a09ed] | 64 |
|
---|
| 65 | int rc;
|
---|
| 66 | inet_packet_t packet;
|
---|
| 67 |
|
---|
| 68 | switch (af) {
|
---|
| 69 | case AF_INET:
|
---|
| 70 | rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
|
---|
| 71 | break;
|
---|
| 72 | case AF_INET6:
|
---|
[1d24ad3] | 73 | rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
|
---|
| 74 | break;
|
---|
[02a09ed] | 75 | default:
|
---|
| 76 | log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid address family");
|
---|
| 77 | return EINVAL;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[fe4310f] | 80 | if (rc != EOK) {
|
---|
[a1a101d] | 81 | log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
|
---|
[e767dbf] | 82 | return rc;
|
---|
[fe4310f] | 83 | }
|
---|
[257feec] | 84 |
|
---|
[a1a101d] | 85 | log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
|
---|
[fe4310f] | 86 | rc = inet_recv_packet(&packet);
|
---|
[a1a101d] | 87 | log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
|
---|
[7f95c904] | 88 | free(packet.data);
|
---|
[257feec] | 89 |
|
---|
[fe4310f] | 90 | return rc;
|
---|
[e2e56e67] | 91 | }
|
---|
| 92 |
|
---|
| 93 | static int inet_link_check_new(void)
|
---|
| 94 | {
|
---|
| 95 | bool already_known;
|
---|
| 96 | category_id_t iplink_cat;
|
---|
| 97 | service_id_t *svcs;
|
---|
| 98 | size_t count, i;
|
---|
| 99 | int rc;
|
---|
| 100 |
|
---|
| 101 | fibril_mutex_lock(&inet_discovery_lock);
|
---|
| 102 |
|
---|
| 103 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
---|
| 104 | if (rc != EOK) {
|
---|
[a1a101d] | 105 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'iplink'.");
|
---|
[e2e56e67] | 106 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
| 107 | return ENOENT;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
|
---|
| 111 | if (rc != EOK) {
|
---|
[a1a101d] | 112 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
|
---|
[e2e56e67] | 113 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
| 114 | return EIO;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | for (i = 0; i < count; i++) {
|
---|
| 118 | already_known = false;
|
---|
| 119 |
|
---|
| 120 | list_foreach(inet_link_list, ilink_link) {
|
---|
| 121 | inet_link_t *ilink = list_get_instance(ilink_link,
|
---|
| 122 | inet_link_t, link_list);
|
---|
| 123 | if (ilink->svc_id == svcs[i]) {
|
---|
| 124 | already_known = true;
|
---|
| 125 | break;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | if (!already_known) {
|
---|
[a1a101d] | 130 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Found IP link '%lu'",
|
---|
[e2e56e67] | 131 | (unsigned long) svcs[i]);
|
---|
| 132 | rc = inet_link_open(svcs[i]);
|
---|
| 133 | if (rc != EOK)
|
---|
[a1a101d] | 134 | log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open IP link.");
|
---|
[e2e56e67] | 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
| 139 | return EOK;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | static inet_link_t *inet_link_new(void)
|
---|
| 143 | {
|
---|
| 144 | inet_link_t *ilink = calloc(1, sizeof(inet_link_t));
|
---|
| 145 |
|
---|
| 146 | if (ilink == NULL) {
|
---|
[a1a101d] | 147 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating link structure. "
|
---|
[e2e56e67] | 148 | "Out of memory.");
|
---|
| 149 | return NULL;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | link_initialize(&ilink->link_list);
|
---|
| 153 |
|
---|
| 154 | return ilink;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | static void inet_link_delete(inet_link_t *ilink)
|
---|
| 158 | {
|
---|
| 159 | if (ilink->svc_name != NULL)
|
---|
| 160 | free(ilink->svc_name);
|
---|
| 161 | free(ilink);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | static int inet_link_open(service_id_t sid)
|
---|
| 165 | {
|
---|
| 166 | inet_link_t *ilink;
|
---|
[a2e3ee6] | 167 | inet_addr_t iaddr;
|
---|
[e2e56e67] | 168 | int rc;
|
---|
| 169 |
|
---|
[a1a101d] | 170 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_link_open()");
|
---|
[e2e56e67] | 171 | ilink = inet_link_new();
|
---|
| 172 | if (ilink == NULL)
|
---|
| 173 | return ENOMEM;
|
---|
| 174 |
|
---|
[45aa22c] | 175 | ilink->svc_id = sid;
|
---|
[347768d] | 176 | ilink->iplink = NULL;
|
---|
[45aa22c] | 177 |
|
---|
[e2e56e67] | 178 | rc = loc_service_get_name(sid, &ilink->svc_name);
|
---|
| 179 | if (rc != EOK) {
|
---|
[a1a101d] | 180 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
|
---|
[e2e56e67] | 181 | goto error;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | ilink->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
|
---|
| 185 | if (ilink->sess == NULL) {
|
---|
[a1a101d] | 186 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", ilink->svc_name);
|
---|
[e2e56e67] | 187 | goto error;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
|
---|
| 191 | if (rc != EOK) {
|
---|
[a1a101d] | 192 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
|
---|
[e2e56e67] | 193 | ilink->svc_name);
|
---|
| 194 | goto error;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[347768d] | 197 | rc = iplink_get_mtu(ilink->iplink, &ilink->def_mtu);
|
---|
| 198 | if (rc != EOK) {
|
---|
[a1a101d] | 199 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed determinning MTU of link '%s'",
|
---|
[347768d] | 200 | ilink->svc_name);
|
---|
| 201 | goto error;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[a1a101d] | 204 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened IP link '%s'", ilink->svc_name);
|
---|
[e2e56e67] | 205 | list_append(&ilink->link_list, &inet_link_list);
|
---|
| 206 |
|
---|
[ceba4bed] | 207 | inet_addrobj_t *addr;
|
---|
[1d24ad3] | 208 | inet_addrobj_t *addr6;
|
---|
[ceba4bed] | 209 |
|
---|
[081971b] | 210 | static int first = 1;
|
---|
[a2e3ee6] | 211 |
|
---|
[ceba4bed] | 212 | addr = inet_addrobj_new();
|
---|
[1d24ad3] | 213 | addr6 = inet_addrobj_new();
|
---|
[a2e3ee6] | 214 |
|
---|
[081971b] | 215 | if (first) {
|
---|
[a2e3ee6] | 216 | inet_naddr(&addr->naddr, 127, 0, 0, 1, 24);
|
---|
[1d24ad3] | 217 | inet_naddr6(&addr6->naddr, 0, 0, 0, 0, 0, 0, 0, 1, 128);
|
---|
[081971b] | 218 | first = 0;
|
---|
| 219 | } else {
|
---|
[1d24ad3] | 220 | /*
|
---|
| 221 | * FIXME
|
---|
| 222 | * Setting static IP addresses for testing purposes
|
---|
| 223 | * 10.0.2.15/24
|
---|
| 224 | * fd19:1680::4/120
|
---|
| 225 | */
|
---|
[a2e3ee6] | 226 | inet_naddr(&addr->naddr, 10, 0, 2, 15, 24);
|
---|
[1d24ad3] | 227 | inet_naddr6(&addr6->naddr, 0xfd19, 0x1680, 0, 0, 0, 0, 0, 4, 120);
|
---|
[081971b] | 228 | }
|
---|
[a2e3ee6] | 229 |
|
---|
[ceba4bed] | 230 | addr->ilink = ilink;
|
---|
[1d24ad3] | 231 | addr6->ilink = ilink;
|
---|
[0e94b979] | 232 | addr->name = str_dup("v4a");
|
---|
[1d24ad3] | 233 | addr6->name = str_dup("v6a");
|
---|
| 234 |
|
---|
[bf9e6fc] | 235 | rc = inet_addrobj_add(addr);
|
---|
| 236 | if (rc != EOK) {
|
---|
[1d24ad3] | 237 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv4 address.");
|
---|
[bf9e6fc] | 238 | inet_addrobj_delete(addr);
|
---|
| 239 | /* XXX Roll back */
|
---|
| 240 | return rc;
|
---|
| 241 | }
|
---|
[1d24ad3] | 242 |
|
---|
| 243 | rc = inet_addrobj_add(addr6);
|
---|
| 244 | if (rc != EOK) {
|
---|
| 245 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding IPv6 address.");
|
---|
| 246 | inet_addrobj_delete(addr6);
|
---|
| 247 | /* XXX Roll back */
|
---|
| 248 | return rc;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[a2e3ee6] | 251 | inet_naddr_addr(&addr->naddr, &iaddr);
|
---|
[962f03b] | 252 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
---|
| 253 | if (rc != EOK) {
|
---|
[1d24ad3] | 254 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed setting IPv4 address on internet link.");
|
---|
[bf9e6fc] | 255 | inet_addrobj_remove(addr);
|
---|
| 256 | inet_addrobj_delete(addr);
|
---|
[962f03b] | 257 | /* XXX Roll back */
|
---|
| 258 | return rc;
|
---|
| 259 | }
|
---|
[1d24ad3] | 260 |
|
---|
| 261 | inet_naddr_addr(&addr6->naddr, &iaddr);
|
---|
| 262 | rc = iplink_addr_add(ilink->iplink, &iaddr);
|
---|
| 263 | if (rc != EOK) {
|
---|
| 264 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed setting IPv6 address on internet link.");
|
---|
| 265 | inet_addrobj_remove(addr6);
|
---|
| 266 | inet_addrobj_delete(addr6);
|
---|
| 267 | /* XXX Roll back */
|
---|
| 268 | return rc;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[e2e56e67] | 271 | return EOK;
|
---|
[02a09ed] | 272 |
|
---|
[e2e56e67] | 273 | error:
|
---|
[347768d] | 274 | if (ilink->iplink != NULL)
|
---|
| 275 | iplink_close(ilink->iplink);
|
---|
[257feec] | 276 |
|
---|
[e2e56e67] | 277 | inet_link_delete(ilink);
|
---|
| 278 | return rc;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | static void inet_link_cat_change_cb(void)
|
---|
| 282 | {
|
---|
| 283 | (void) inet_link_check_new();
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | int inet_link_discovery_start(void)
|
---|
| 287 | {
|
---|
| 288 | int rc;
|
---|
| 289 |
|
---|
| 290 | rc = loc_register_cat_change_cb(inet_link_cat_change_cb);
|
---|
| 291 | if (rc != EOK) {
|
---|
[a1a101d] | 292 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for IP link "
|
---|
[e2e56e67] | 293 | "discovery (%d).", rc);
|
---|
| 294 | return rc;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | return inet_link_check_new();
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[ceba4bed] | 300 | /** Send datagram over Internet link */
|
---|
| 301 | int inet_link_send_dgram(inet_link_t *ilink, inet_addr_t *lsrc,
|
---|
[2ff150e] | 302 | inet_addr_t *ldest, inet_dgram_t *dgram, uint8_t proto, uint8_t ttl, int df)
|
---|
[ceba4bed] | 303 | {
|
---|
[347768d] | 304 | /*
|
---|
| 305 | * Fill packet structure. Fragmentation is performed by
|
---|
| 306 | * inet_pdu_encode().
|
---|
| 307 | */
|
---|
[257feec] | 308 |
|
---|
[a2e3ee6] | 309 | inet_packet_t packet;
|
---|
| 310 |
|
---|
[fe4310f] | 311 | packet.src = dgram->src;
|
---|
| 312 | packet.dest = dgram->dest;
|
---|
| 313 | packet.tos = dgram->tos;
|
---|
[2ff150e] | 314 | packet.proto = proto;
|
---|
[fe4310f] | 315 | packet.ttl = ttl;
|
---|
| 316 | packet.df = df;
|
---|
| 317 | packet.data = dgram->data;
|
---|
| 318 | packet.size = dgram->size;
|
---|
[a2e3ee6] | 319 |
|
---|
| 320 | iplink_sdu_t sdu;
|
---|
[02a09ed] | 321 | size_t offs = 0;
|
---|
| 322 | int rc;
|
---|
[a2e3ee6] | 323 |
|
---|
[02a09ed] | 324 | sdu.src = *lsrc;
|
---|
| 325 | sdu.dest = *ldest;
|
---|
[a2e3ee6] | 326 |
|
---|
[347768d] | 327 | do {
|
---|
| 328 | /* Encode one fragment */
|
---|
[02a09ed] | 329 | size_t roffs;
|
---|
[347768d] | 330 | rc = inet_pdu_encode(&packet, offs, ilink->def_mtu, &sdu.data,
|
---|
| 331 | &sdu.size, &roffs);
|
---|
| 332 | if (rc != EOK)
|
---|
| 333 | return rc;
|
---|
[a2e3ee6] | 334 |
|
---|
[347768d] | 335 | /* Send the PDU */
|
---|
| 336 | rc = iplink_send(ilink->iplink, &sdu);
|
---|
| 337 | free(sdu.data);
|
---|
[a2e3ee6] | 338 |
|
---|
[347768d] | 339 | offs = roffs;
|
---|
| 340 | } while (offs < packet.size);
|
---|
[a2e3ee6] | 341 |
|
---|
[ceba4bed] | 342 | return rc;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[45aa22c] | 345 | inet_link_t *inet_link_get_by_id(sysarg_t link_id)
|
---|
| 346 | {
|
---|
| 347 | fibril_mutex_lock(&inet_discovery_lock);
|
---|
| 348 |
|
---|
| 349 | list_foreach(inet_link_list, elem) {
|
---|
| 350 | inet_link_t *ilink = list_get_instance(elem, inet_link_t,
|
---|
| 351 | link_list);
|
---|
| 352 |
|
---|
| 353 | if (ilink->svc_id == link_id) {
|
---|
| 354 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
| 355 | return ilink;
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | fibril_mutex_unlock(&inet_discovery_lock);
|
---|
| 360 | return NULL;
|
---|
| 361 | }
|
---|
[ceba4bed] | 362 |
|
---|
[e2e56e67] | 363 | /** @}
|
---|
| 364 | */
|
---|