source: mainline/uspace/srv/net/il/ip/ip.c@ e29e09cf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e29e09cf was ffa2c8ef, checked in by Martin Decky <martin@…>, 15 years ago

do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 53.8 KB
RevLine 
[21580dd]1/*
2 * Copyright (c) 2009 Lukas Mejdrech
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 ip
[e8199d77]30 * @{
[21580dd]31 */
32
33/** @file
[e8199d77]34 * IP module implementation.
35 * @see arp.h
[21580dd]36 */
37
38#include <async.h>
39#include <errno.h>
40#include <fibril_synch.h>
41#include <stdio.h>
[19f857a]42#include <str.h>
[21580dd]43#include <ipc/services.h>
[514ee46]44#include <ipc/net.h>
[f5a3479]45#include <ipc/nil.h>
[522253c1]46#include <ipc/il.h>
[779a47d]47#include <ipc/ip.h>
[21580dd]48#include <sys/types.h>
[2687bdb]49#include <byteorder.h>
[797b704]50#include "ip.h"
[21580dd]51
[e8199d77]52#include <adt/measured_strings.h>
53#include <adt/module_map.h>
54
55#include <packet_client.h>
[058edb6]56#include <net/socket_codes.h>
[e4554d4]57#include <net/in.h>
58#include <net/in6.h>
59#include <net/inet.h>
[c7a8442]60#include <net/modules.h>
[514ee46]61#include <net/device.h>
62#include <net/packet.h>
63#include <net/icmp_codes.h>
[058edb6]64
[849ed54]65#include <arp_interface.h>
66#include <net_checksum.h>
67#include <icmp_client.h>
[f1938c6]68#include <icmp_remote.h>
[849ed54]69#include <ip_client.h>
70#include <ip_interface.h>
[4a3b501]71#include <ip_header.h>
[849ed54]72#include <net_interface.h>
[fe8dfa6]73#include <nil_remote.h>
[014dd57b]74#include <tl_remote.h>
[14f1db0]75#include <packet_remote.h>
[797b704]76#include <il_remote.h>
77#include <il_skel.h>
[21580dd]78
[e8199d77]79/** IP module name. */
80#define NAME "ip"
[849ed54]81
[e8199d77]82/** IP version 4. */
83#define IPV4 4
[21580dd]84
[e8199d77]85/** Default network interface IP version. */
[21580dd]86#define NET_DEFAULT_IPV IPV4
87
[e8199d77]88/** Default network interface IP routing. */
[21580dd]89#define NET_DEFAULT_IP_ROUTING false
90
[e8199d77]91/** Minimum IP packet content. */
92#define IP_MIN_CONTENT 576
[21580dd]93
[e8199d77]94/** ARP module name. */
95#define ARP_NAME "arp"
[21580dd]96
[e8199d77]97/** ARP module filename. */
98#define ARP_FILENAME "/srv/arp"
[21580dd]99
[e8199d77]100/** IP packet address length. */
101#define IP_ADDR sizeof(struct sockaddr_in6)
[21580dd]102
[e8199d77]103/** IP packet prefix length. */
104#define IP_PREFIX sizeof(ip_header_t)
[21580dd]105
[e8199d77]106/** IP packet suffix length. */
107#define IP_SUFFIX 0
[21580dd]108
[e8199d77]109/** IP packet maximum content length. */
110#define IP_MAX_CONTENT 65535
[21580dd]111
[e8199d77]112/** The IP localhost address. */
[aadf01e]113#define IPV4_LOCALHOST_ADDRESS htonl((127 << 24) + 1)
[21580dd]114
[e8199d77]115/** IP global data. */
116ip_globals_t ip_globals;
[21580dd]117
[e8199d77]118DEVICE_MAP_IMPLEMENT(ip_netifs, ip_netif_t);
119INT_MAP_IMPLEMENT(ip_protos, ip_proto_t);
120GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t);
[21580dd]121
[797b704]122static void ip_receiver(ipc_callid_t, ipc_call_t *);
123
[e8199d77]124/** Releases the packet and returns the result.
125 *
126 * @param[in] packet The packet queue to be released.
127 * @param[in] result The result to be returned.
128 * @return The result parameter.
[21580dd]129 */
[46d4d9f]130static int ip_release_and_return(packet_t *packet, int result)
[e8199d77]131{
132 pq_release_remote(ip_globals.net_phone, packet_get_id(packet));
133 return result;
134}
[21580dd]135
[e8199d77]136/** Returns the ICMP phone.
137 *
138 * Searches the registered protocols.
139 *
[1bfd3d3]140 * @return The found ICMP phone.
141 * @return ENOENT if the ICMP is not registered.
[21580dd]142 */
[e8199d77]143static int ip_get_icmp_phone(void)
144{
[4e5c7ba]145 ip_proto_t *proto;
[e8199d77]146 int phone;
[21580dd]147
[e8199d77]148 fibril_rwlock_read_lock(&ip_globals.protos_lock);
149 proto = ip_protos_find(&ip_globals.protos, IPPROTO_ICMP);
150 phone = proto ? proto->phone : ENOENT;
151 fibril_rwlock_read_unlock(&ip_globals.protos_lock);
152 return phone;
153}
[21580dd]154
[e8199d77]155/** Prepares the ICMP notification packet.
156 *
157 * Releases additional packets and keeps only the first one.
158 *
159 * @param[in] packet The packet or the packet queue to be reported as faulty.
160 * @param[in] header The first packet IP header. May be NULL.
[1bfd3d3]161 * @return EOK on success.
162 * @return EINVAL if there are no data in the packet.
163 * @return EINVAL if the packet is a fragment.
164 * @return ENOMEM if the packet is too short to contain the IP
[e8199d77]165 * header.
[1bfd3d3]166 * @return EAFNOSUPPORT if the address family is not supported.
167 * @return EPERM if the protocol is not allowed to send ICMP
[e8199d77]168 * notifications. The ICMP protocol itself.
[1bfd3d3]169 * @return Other error codes as defined for the packet_set_addr().
[21580dd]170 */
[46d4d9f]171static int ip_prepare_icmp(packet_t *packet, ip_header_t *header)
[e8199d77]172{
[46d4d9f]173 packet_t *next;
[e8199d77]174 struct sockaddr *dest;
175 struct sockaddr_in dest_in;
176 socklen_t addrlen;
[21580dd]177
[e8199d77]178 // detach the first packet and release the others
179 next = pq_detach(packet);
180 if (next)
181 pq_release_remote(ip_globals.net_phone, packet_get_id(next));
[21580dd]182
[e8199d77]183 if (!header) {
184 if (packet_get_data_length(packet) <= sizeof(ip_header_t))
185 return ENOMEM;
[21580dd]186
[e8199d77]187 // get header
[88a1bb9]188 header = (ip_header_t *) packet_get_data(packet);
[e8199d77]189 if (!header)
190 return EINVAL;
[21580dd]191
[e8199d77]192 }
[21580dd]193
[e8199d77]194 // only for the first fragment
195 if (IP_FRAGMENT_OFFSET(header))
196 return EINVAL;
[21580dd]197
[e8199d77]198 // not for the ICMP protocol
199 if (header->protocol == IPPROTO_ICMP)
200 return EPERM;
[21580dd]201
[e8199d77]202 // set the destination address
203 switch (header->version) {
204 case IPVERSION:
205 addrlen = sizeof(dest_in);
206 bzero(&dest_in, addrlen);
207 dest_in.sin_family = AF_INET;
208 memcpy(&dest_in.sin_addr.s_addr, &header->source_address,
209 sizeof(header->source_address));
210 dest = (struct sockaddr *) &dest_in;
211 break;
[21580dd]212
[e8199d77]213 default:
214 return EAFNOSUPPORT;
215 }
[21580dd]216
[e8199d77]217 return packet_set_addr(packet, NULL, (uint8_t *) dest, addrlen);
218}
[21580dd]219
220/** Prepares the ICMP notification packet.
[e8199d77]221 *
222 * Releases additional packets and keeps only the first one.
223 * All packets are released on error.
224 *
225 * @param[in] error The packet error service.
226 * @param[in] packet The packet or the packet queue to be reported as faulty.
227 * @param[in] header The first packet IP header. May be NULL.
[1bfd3d3]228 * @return The found ICMP phone.
229 * @return EINVAL if the error parameter is set.
230 * @return EINVAL if the ICMP phone is not found.
231 * @return EINVAL if the ip_prepare_icmp() fails.
[21580dd]232 */
[e8199d77]233static int
[46d4d9f]234ip_prepare_icmp_and_get_phone(services_t error, packet_t *packet,
[88a1bb9]235 ip_header_t *header)
[e8199d77]236{
237 int phone;
[21580dd]238
[e8199d77]239 phone = ip_get_icmp_phone();
240 if (error || (phone < 0) || ip_prepare_icmp(packet, header))
241 return ip_release_and_return(packet, EINVAL);
242 return phone;
243}
[21580dd]244
[797b704]245int il_initialize(int net_phone)
[e8199d77]246{
[aadf01e]247 fibril_rwlock_initialize(&ip_globals.lock);
248 fibril_rwlock_write_lock(&ip_globals.lock);
249 fibril_rwlock_initialize(&ip_globals.protos_lock);
250 fibril_rwlock_initialize(&ip_globals.netifs_lock);
[797b704]251
252 ip_globals.net_phone = net_phone;
[21580dd]253 ip_globals.packet_counter = 0;
254 ip_globals.gateway.address.s_addr = 0;
255 ip_globals.gateway.netmask.s_addr = 0;
256 ip_globals.gateway.gateway.s_addr = 0;
257 ip_globals.gateway.netif = NULL;
[02314f8]258
[797b704]259 int rc = ip_netifs_initialize(&ip_globals.netifs);
[02314f8]260 if (rc != EOK)
261 goto out;
262 rc = ip_protos_initialize(&ip_globals.protos);
263 if (rc != EOK)
264 goto out;
265 rc = modules_initialize(&ip_globals.modules);
266 if (rc != EOK)
267 goto out;
[61bfc370]268 rc = add_module(NULL, &ip_globals.modules, (uint8_t *) ARP_NAME,
269 (uint8_t *) ARP_FILENAME, SERVICE_ARP, 0, arp_connect_module);
[02314f8]270
271out:
[aadf01e]272 fibril_rwlock_write_unlock(&ip_globals.lock);
[21580dd]273
[02314f8]274 return rc;
[21580dd]275}
276
[e8199d77]277/** Initializes a new network interface specific data.
278 *
279 * Connects to the network interface layer module, reads the netif
280 * configuration, starts an ARP module if needed and sets the netif routing
281 * table.
282 *
283 * The device identifier and the nil service has to be set.
284 *
285 * @param[in,out] ip_netif Network interface specific data.
[1bfd3d3]286 * @return EOK on success.
287 * @return ENOTSUP if DHCP is configured.
288 * @return ENOTSUP if IPv6 is configured.
289 * @return EINVAL if any of the addresses is invalid.
290 * @return EINVAL if the used ARP module is not known.
291 * @return ENOMEM if there is not enough memory left.
292 * @return Other error codes as defined for the
[e8199d77]293 * net_get_device_conf_req() function.
[1bfd3d3]294 * @return Other error codes as defined for the bind_service()
[e8199d77]295 * function.
[1bfd3d3]296 * @return Other error codes as defined for the specific
[e8199d77]297 * arp_device_req() function.
[1bfd3d3]298 * @return Other error codes as defined for the
[e8199d77]299 * nil_packet_size_req() function.
300 */
[4e5c7ba]301static int ip_netif_initialize(ip_netif_t *ip_netif)
[e8199d77]302{
303 measured_string_t names[] = {
304 {
[61bfc370]305 (uint8_t *) "IPV",
[e8199d77]306 3
307 },
308 {
[61bfc370]309 (uint8_t *) "IP_CONFIG",
[e8199d77]310 9
311 },
312 {
[61bfc370]313 (uint8_t *) "IP_ADDR",
[e8199d77]314 7
315 },
316 {
[61bfc370]317 (uint8_t *) "IP_NETMASK",
[e8199d77]318 10
319 },
320 {
[61bfc370]321 (uint8_t *) "IP_GATEWAY",
[e8199d77]322 10
323 },
324 {
[61bfc370]325 (uint8_t *) "IP_BROADCAST",
[e8199d77]326 12
327 },
328 {
[61bfc370]329 (uint8_t *) "ARP",
[e8199d77]330 3
331 },
332 {
[61bfc370]333 (uint8_t *) "IP_ROUTING",
[e8199d77]334 10
335 }
336 };
[4eca056]337 measured_string_t *configuration;
[aadf01e]338 size_t count = sizeof(names) / sizeof(measured_string_t);
[61bfc370]339 uint8_t *data;
[aadf01e]340 measured_string_t address;
[4e5c7ba]341 ip_route_t *route;
[aadf01e]342 in_addr_t gateway;
[02314f8]343 int index;
344 int rc;
[21580dd]345
346 ip_netif->arp = NULL;
347 route = NULL;
348 ip_netif->ipv = NET_DEFAULT_IPV;
349 ip_netif->dhcp = false;
350 ip_netif->routing = NET_DEFAULT_IP_ROUTING;
[aadf01e]351 configuration = &names[0];
[e8199d77]352
[21580dd]353 // get configuration
[02314f8]354 rc = net_get_device_conf_req(ip_globals.net_phone, ip_netif->device_id,
355 &configuration, count, &data);
356 if (rc != EOK)
357 return rc;
358
[e8199d77]359 if (configuration) {
360 if (configuration[0].value)
[61bfc370]361 ip_netif->ipv = strtol((char *) configuration[0].value, NULL, 0);
362
363 ip_netif->dhcp = !str_lcmp((char *) configuration[1].value, "dhcp",
[e8199d77]364 configuration[1].length);
365
366 if (ip_netif->dhcp) {
[21580dd]367 // TODO dhcp
[aadf01e]368 net_free_settings(configuration, data);
[21580dd]369 return ENOTSUP;
[e8199d77]370 } else if (ip_netif->ipv == IPV4) {
[4e5c7ba]371 route = (ip_route_t *) malloc(sizeof(ip_route_t));
[e8199d77]372 if (!route) {
[aadf01e]373 net_free_settings(configuration, data);
[21580dd]374 return ENOMEM;
375 }
376 route->address.s_addr = 0;
377 route->netmask.s_addr = 0;
378 route->gateway.s_addr = 0;
379 route->netif = ip_netif;
[aadf01e]380 index = ip_routes_add(&ip_netif->routes, route);
[e8199d77]381 if (index < 0) {
[aadf01e]382 net_free_settings(configuration, data);
383 free(route);
[21580dd]384 return index;
385 }
[02314f8]386
[61bfc370]387 if ((inet_pton(AF_INET, (char *) configuration[2].value,
[02314f8]388 (uint8_t *) &route->address.s_addr) != EOK) ||
[61bfc370]389 (inet_pton(AF_INET, (char *) configuration[3].value,
[02314f8]390 (uint8_t *) &route->netmask.s_addr) != EOK) ||
[61bfc370]391 (inet_pton(AF_INET, (char *) configuration[4].value,
[e8199d77]392 (uint8_t *) &gateway.s_addr) == EINVAL) ||
[61bfc370]393 (inet_pton(AF_INET, (char *) configuration[5].value,
[e8199d77]394 (uint8_t *) &ip_netif->broadcast.s_addr) == EINVAL))
395 {
[aadf01e]396 net_free_settings(configuration, data);
[21580dd]397 return EINVAL;
398 }
[e8199d77]399 } else {
[21580dd]400 // TODO ipv6 in separate module
[aadf01e]401 net_free_settings(configuration, data);
[21580dd]402 return ENOTSUP;
403 }
[e8199d77]404
405 if (configuration[6].value) {
406 ip_netif->arp = get_running_module(&ip_globals.modules,
407 configuration[6].value);
408 if (!ip_netif->arp) {
409 printf("Failed to start the arp %s\n",
410 configuration[6].value);
[aadf01e]411 net_free_settings(configuration, data);
[21580dd]412 return EINVAL;
413 }
414 }
[e8199d77]415 if (configuration[7].value)
[aadf01e]416 ip_netif->routing = (configuration[7].value[0] == 'y');
[e8199d77]417
[aadf01e]418 net_free_settings(configuration, data);
[21580dd]419 }
[e8199d77]420
[21580dd]421 // binds the netif service which also initializes the device
[e8199d77]422 ip_netif->phone = nil_bind_service(ip_netif->service,
[96b02eb9]423 (sysarg_t) ip_netif->device_id, SERVICE_IP,
[797b704]424 ip_receiver);
[e8199d77]425 if (ip_netif->phone < 0) {
426 printf("Failed to contact the nil service %d\n",
427 ip_netif->service);
[21580dd]428 return ip_netif->phone;
429 }
[e8199d77]430
[21580dd]431 // has to be after the device netif module initialization
[e8199d77]432 if (ip_netif->arp) {
433 if (route) {
[61bfc370]434 address.value = (uint8_t *) &route->address.s_addr;
[1b59023]435 address.length = sizeof(in_addr_t);
[02314f8]436
437 rc = arp_device_req(ip_netif->arp->phone,
[e8199d77]438 ip_netif->device_id, SERVICE_IP, ip_netif->service,
[02314f8]439 &address);
440 if (rc != EOK)
441 return rc;
[e8199d77]442 } else {
[21580dd]443 ip_netif->arp = 0;
444 }
445 }
[e8199d77]446
[21580dd]447 // get packet dimensions
[02314f8]448 rc = nil_packet_size_req(ip_netif->phone, ip_netif->device_id,
449 &ip_netif->packet_dimension);
450 if (rc != EOK)
451 return rc;
452
[e8199d77]453 if (ip_netif->packet_dimension.content < IP_MIN_CONTENT) {
[7e752b2]454 printf("Maximum transmission unit %zu bytes is too small, at "
[e8199d77]455 "least %d bytes are needed\n",
456 ip_netif->packet_dimension.content, IP_MIN_CONTENT);
[91478aa]457 ip_netif->packet_dimension.content = IP_MIN_CONTENT;
[21580dd]458 }
[e8199d77]459
[aadf01e]460 index = ip_netifs_add(&ip_globals.netifs, ip_netif->device_id, ip_netif);
[e8199d77]461 if (index < 0)
[aadf01e]462 return index;
[e8199d77]463
464 if (gateway.s_addr) {
[21580dd]465 // the default gateway
466 ip_globals.gateway.address.s_addr = 0;
467 ip_globals.gateway.netmask.s_addr = 0;
468 ip_globals.gateway.gateway.s_addr = gateway.s_addr;
469 ip_globals.gateway.netif = ip_netif;
[774e6d1a]470
471 char defgateway[INET_ADDRSTRLEN];
472 inet_ntop(AF_INET, (uint8_t *) &gateway.s_addr,
473 defgateway, INET_ADDRSTRLEN);
474 printf("%s: Default gateway (%s)\n", NAME, defgateway);
[21580dd]475 }
[e8199d77]476
[21580dd]477 return EOK;
478}
479
[797b704]480static int ip_device_req_local(int il_phone, device_id_t device_id,
481 services_t netif)
[e8199d77]482{
[797b704]483 ip_netif_t *ip_netif;
484 ip_route_t *route;
485 int index;
486 int rc;
487
488 ip_netif = (ip_netif_t *) malloc(sizeof(ip_netif_t));
489 if (!ip_netif)
490 return ENOMEM;
491
492 rc = ip_routes_initialize(&ip_netif->routes);
493 if (rc != EOK) {
494 free(ip_netif);
495 return rc;
496 }
497
498 ip_netif->device_id = device_id;
499 ip_netif->service = netif;
500 ip_netif->state = NETIF_STOPPED;
[21580dd]501
[aadf01e]502 fibril_rwlock_write_lock(&ip_globals.netifs_lock);
[797b704]503
504 rc = ip_netif_initialize(ip_netif);
505 if (rc != EOK) {
[aadf01e]506 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
[797b704]507 ip_routes_destroy(&ip_netif->routes);
508 free(ip_netif);
509 return rc;
[21580dd]510 }
[797b704]511 if (ip_netif->arp)
512 ip_netif->arp->usage++;
513
514 // print the settings
515 printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n",
516 NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv,
517 ip_netif->dhcp ? "dhcp" : "static");
518
519 // TODO ipv6 addresses
520
521 char address[INET_ADDRSTRLEN];
522 char netmask[INET_ADDRSTRLEN];
523 char gateway[INET_ADDRSTRLEN];
524
525 for (index = 0; index < ip_routes_count(&ip_netif->routes); index++) {
526 route = ip_routes_get_index(&ip_netif->routes, index);
527 if (route) {
528 inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr,
529 address, INET_ADDRSTRLEN);
530 inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr,
531 netmask, INET_ADDRSTRLEN);
532 inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr,
533 gateway, INET_ADDRSTRLEN);
534 printf("%s: Route %d (address: %s, netmask: %s, "
535 "gateway: %s)\n", NAME, index, address, netmask,
536 gateway);
537 }
538 }
539
540 inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address,
541 INET_ADDRSTRLEN);
[aadf01e]542 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
[e8199d77]543
[797b704]544 printf("%s: Broadcast (%s)\n", NAME, address);
[e8199d77]545
[21580dd]546 return EOK;
547}
548
[797b704]549/** Searches the network interfaces if there is a suitable route.
[e8199d77]550 *
[797b704]551 * @param[in] netif The network interface to be searched for routes. May be
552 * NULL.
553 * @param[in] destination The destination address.
554 * @return The found route.
555 * @return NULL if no route was found.
[e8199d77]556 */
[797b704]557static ip_route_t *ip_netif_find_route(ip_netif_t *netif,
558 in_addr_t destination)
[e8199d77]559{
[797b704]560 int index;
561 ip_route_t *route;
562
563 if (!netif)
564 return NULL;
565
566 /* Start with the first one (the direct route) */
567 for (index = 0; index < ip_routes_count(&netif->routes); index++) {
568 route = ip_routes_get_index(&netif->routes, index);
569 if ((route) &&
570 ((route->address.s_addr & route->netmask.s_addr) ==
571 (destination.s_addr & route->netmask.s_addr)))
572 return route;
573 }
574
575 return NULL;
576}
577
578/** Searches all network interfaces if there is a suitable route.
579 *
580 * @param[in] destination The destination address.
581 * @return The found route.
582 * @return NULL if no route was found.
583 */
584static ip_route_t *ip_find_route(in_addr_t destination) {
585 int index;
586 ip_route_t *route;
[4e5c7ba]587 ip_netif_t *netif;
[21580dd]588
[797b704]589 // start with the last netif - the newest one
590 index = ip_netifs_count(&ip_globals.netifs) - 1;
591 while (index >= 0) {
592 netif = ip_netifs_get_index(&ip_globals.netifs, index);
593 if (netif && (netif->state == NETIF_ACTIVE)) {
594 route = ip_netif_find_route(netif, destination);
595 if (route)
596 return route;
597 }
598 index--;
[21580dd]599 }
[e8199d77]600
[797b704]601 return &ip_globals.gateway;
[21580dd]602}
603
[797b704]604/** Returns the network interface's IP address.
[e8199d77]605 *
[797b704]606 * @param[in] netif The network interface.
607 * @return The IP address.
608 * @return NULL if no IP address was found.
[e8199d77]609 */
[797b704]610static in_addr_t *ip_netif_address(ip_netif_t *netif)
[e8199d77]611{
[797b704]612 ip_route_t *route;
[e8199d77]613
[797b704]614 route = ip_routes_get_index(&netif->routes, 0);
615 return route ? &route->address : NULL;
[21580dd]616}
617
[e8199d77]618/** Copies the fragment header.
619 *
620 * Copies only the header itself and relevant IP options.
621 *
622 * @param[out] last The created header.
623 * @param[in] first The original header to be copied.
624 */
[88a1bb9]625static void ip_create_last_header(ip_header_t *last, ip_header_t *first)
[e8199d77]626{
[88a1bb9]627 ip_option_t *option;
[e8199d77]628 size_t next;
629 size_t length;
[21580dd]630
[e8199d77]631 // copy first itself
632 memcpy(last, first, sizeof(ip_header_t));
633 length = sizeof(ip_header_t);
634 next = sizeof(ip_header_t);
[21580dd]635
[e8199d77]636 // process all ip options
637 while (next < first->header_length) {
[88a1bb9]638 option = (ip_option_t *) (((uint8_t *) first) + next);
[e8199d77]639 // skip end or noop
640 if ((option->type == IPOPT_END) ||
641 (option->type == IPOPT_NOOP)) {
642 next++;
643 } else {
644 // copy if told so or skip
645 if (IPOPT_COPIED(option->type)) {
646 memcpy(((uint8_t *) last) + length,
647 ((uint8_t *) first) + next, option->length);
648 length += option->length;
[21580dd]649 }
[e8199d77]650 // next option
651 next += option->length;
[21580dd]652 }
653 }
654
[e8199d77]655 // align 4 byte boundary
656 if (length % 4) {
657 bzero(((uint8_t *) last) + length, 4 - (length % 4));
658 last->header_length = length / 4 + 1;
659 } else {
660 last->header_length = length / 4;
[21580dd]661 }
[e8199d77]662
663 last->header_checksum = 0;
[21580dd]664}
665
[e8199d77]666/** Prepares the outgoing packet or the packet queue.
667 *
668 * The packet queue is a fragmented packet
669 * Updates the first packet's IP header.
670 * Prefixes the additional packets with fragment headers.
671 *
672 * @param[in] source The source address.
673 * @param[in] dest The destination address.
674 * @param[in,out] packet The packet to be sent.
675 * @param[in] destination The destination hardware address.
[1bfd3d3]676 * @return EOK on success.
677 * @return EINVAL if the packet is too small to contain the IP
[e8199d77]678 * header.
[1bfd3d3]679 * @return EINVAL if the packet is too long than the IP allows.
680 * @return ENOMEM if there is not enough memory left.
681 * @return Other error codes as defined for the packet_set_addr()
[e8199d77]682 * function.
683 */
[797b704]684static int ip_prepare_packet(in_addr_t *source, in_addr_t dest,
685 packet_t *packet, measured_string_t *destination)
[e8199d77]686{
[aadf01e]687 size_t length;
[88a1bb9]688 ip_header_t *header;
689 ip_header_t *last_header;
690 ip_header_t *middle_header;
[46d4d9f]691 packet_t *next;
[02314f8]692 int rc;
[aadf01e]693
694 length = packet_get_data_length(packet);
[e8199d77]695 if ((length < sizeof(ip_header_t)) || (length > IP_MAX_CONTENT))
[aadf01e]696 return EINVAL;
[e8199d77]697
[88a1bb9]698 header = (ip_header_t *) packet_get_data(packet);
[e8199d77]699 if (destination) {
[02314f8]700 rc = packet_set_addr(packet, NULL, (uint8_t *) destination->value,
[7837101]701 destination->length);
[e8199d77]702 } else {
[02314f8]703 rc = packet_set_addr(packet, NULL, NULL, 0);
[21580dd]704 }
[02314f8]705 if (rc != EOK)
706 return rc;
707
[21580dd]708 header->version = IPV4;
709 header->fragment_offset_high = 0;
710 header->fragment_offset_low = 0;
711 header->header_checksum = 0;
[e8199d77]712 if (source)
[aadf01e]713 header->source_address = source->s_addr;
[21580dd]714 header->destination_address = dest.s_addr;
[e8199d77]715
[aadf01e]716 fibril_rwlock_write_lock(&ip_globals.lock);
[e8199d77]717 ip_globals.packet_counter++;
[aadf01e]718 header->identification = htons(ip_globals.packet_counter);
719 fibril_rwlock_write_unlock(&ip_globals.lock);
[e8199d77]720
721 if (pq_next(packet)) {
[88a1bb9]722 last_header = (ip_header_t *) malloc(IP_HEADER_LENGTH(header));
[e8199d77]723 if (!last_header)
[aadf01e]724 return ENOMEM;
725 ip_create_last_header(last_header, header);
726 next = pq_next(packet);
[e8199d77]727 while (pq_next(next)) {
[88a1bb9]728 middle_header = (ip_header_t *) packet_prefix(next,
[e8199d77]729 IP_HEADER_LENGTH(last_header));
[fd8e8e1]730 if (!middle_header) {
731 free(last_header);
[aadf01e]732 return ENOMEM;
[fd8e8e1]733 }
[e8199d77]734
735 memcpy(middle_header, last_header,
736 IP_HEADER_LENGTH(last_header));
[21580dd]737 header->flags |= IPFLAG_MORE_FRAGMENTS;
[e8199d77]738 middle_header->total_length =
739 htons(packet_get_data_length(next));
740 middle_header->fragment_offset_high =
741 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length);
742 middle_header->fragment_offset_low =
743 IP_COMPUTE_FRAGMENT_OFFSET_LOW(length);
744 middle_header->header_checksum =
745 IP_HEADER_CHECKSUM(middle_header);
746 if (destination) {
[02314f8]747 rc = packet_set_addr(next, NULL,
[e8199d77]748 (uint8_t *) destination->value,
[7837101]749 destination->length);
[02314f8]750 if (rc != EOK) {
[fd8e8e1]751 free(last_header);
[02314f8]752 return rc;
[fd8e8e1]753 }
[21580dd]754 }
[aadf01e]755 length += packet_get_data_length(next);
756 next = pq_next(next);
[21580dd]757 }
[e8199d77]758
[88a1bb9]759 middle_header = (ip_header_t *) packet_prefix(next,
[e8199d77]760 IP_HEADER_LENGTH(last_header));
[fd8e8e1]761 if (!middle_header) {
762 free(last_header);
[aadf01e]763 return ENOMEM;
[fd8e8e1]764 }
[e8199d77]765
766 memcpy(middle_header, last_header,
767 IP_HEADER_LENGTH(last_header));
768 middle_header->total_length =
769 htons(packet_get_data_length(next));
770 middle_header->fragment_offset_high =
771 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length);
772 middle_header->fragment_offset_low =
773 IP_COMPUTE_FRAGMENT_OFFSET_LOW(length);
774 middle_header->header_checksum =
775 IP_HEADER_CHECKSUM(middle_header);
776 if (destination) {
[02314f8]777 rc = packet_set_addr(next, NULL,
[e8199d77]778 (uint8_t *) destination->value,
[7837101]779 destination->length);
[02314f8]780 if (rc != EOK) {
[fd8e8e1]781 free(last_header);
[02314f8]782 return rc;
783 }
[aadf01e]784 }
785 length += packet_get_data_length(next);
786 free(last_header);
[21580dd]787 header->flags |= IPFLAG_MORE_FRAGMENTS;
788 }
[e8199d77]789
[aadf01e]790 header->total_length = htons(length);
[21580dd]791 // unnecessary for all protocols
[aadf01e]792 header->header_checksum = IP_HEADER_CHECKSUM(header);
[e8199d77]793
[21580dd]794 return EOK;
795}
796
[e8199d77]797/** Fragments the packet from the end.
798 *
799 * @param[in] packet The packet to be fragmented.
800 * @param[in,out] new_packet The new packet fragment.
801 * @param[in,out] header The original packet header.
802 * @param[in,out] new_header The new packet fragment header.
803 * @param[in] length The new fragment length.
804 * @param[in] src The source address.
805 * @param[in] dest The destiantion address.
806 * @param[in] addrlen The address length.
[1bfd3d3]807 * @return EOK on success.
808 * @return ENOMEM if the target packet is too small.
809 * @return Other error codes as defined for the packet_set_addr()
[e8199d77]810 * function.
[1bfd3d3]811 * @return Other error codes as defined for the pq_insert_after()
[e8199d77]812 * function.
813 */
[797b704]814static int ip_fragment_packet_data(packet_t *packet, packet_t *new_packet,
[88a1bb9]815 ip_header_t *header, ip_header_t *new_header, size_t length,
[e8199d77]816 const struct sockaddr *src, const struct sockaddr *dest, socklen_t addrlen)
[14f1db0]817{
[e8199d77]818 void *data;
819 size_t offset;
[02314f8]820 int rc;
[21580dd]821
[e8199d77]822 data = packet_suffix(new_packet, length);
823 if (!data)
[21580dd]824 return ENOMEM;
825
[e8199d77]826 memcpy(data, ((void *) header) + IP_TOTAL_LENGTH(header) - length,
827 length);
[02314f8]828
829 rc = packet_trim(packet, 0, length);
830 if (rc != EOK)
831 return rc;
832
[e8199d77]833 header->total_length = htons(IP_TOTAL_LENGTH(header) - length);
834 new_header->total_length = htons(IP_HEADER_LENGTH(new_header) + length);
835 offset = IP_FRAGMENT_OFFSET(header) + IP_HEADER_DATA_LENGTH(header);
836 new_header->fragment_offset_high =
837 IP_COMPUTE_FRAGMENT_OFFSET_HIGH(offset);
838 new_header->fragment_offset_low =
839 IP_COMPUTE_FRAGMENT_OFFSET_LOW(offset);
840 new_header->header_checksum = IP_HEADER_CHECKSUM(new_header);
[02314f8]841
842 rc = packet_set_addr(new_packet, (const uint8_t *) src,
843 (const uint8_t *) dest, addrlen);
844 if (rc != EOK)
845 return rc;
[21580dd]846
[e8199d77]847 return pq_insert_after(packet, new_packet);
[21580dd]848}
849
[797b704]850/** Prefixes a middle fragment header based on the last fragment header to the
851 * packet.
852 *
853 * @param[in] packet The packet to be prefixed.
854 * @param[in] last The last header to be copied.
855 * @return The prefixed middle header.
856 * @return NULL on error.
857 */
858static ip_header_t *ip_create_middle_header(packet_t *packet,
859 ip_header_t *last)
860{
861 ip_header_t *middle;
862
863 middle = (ip_header_t *) packet_suffix(packet, IP_HEADER_LENGTH(last));
864 if (!middle)
865 return NULL;
866 memcpy(middle, last, IP_HEADER_LENGTH(last));
867 middle->flags |= IPFLAG_MORE_FRAGMENTS;
868 return middle;
869}
870
[e8199d77]871/** Checks the packet length and fragments it if needed.
872 *
873 * The new fragments are queued before the original packet.
874 *
875 * @param[in,out] packet The packet to be checked.
876 * @param[in] length The maximum packet length.
877 * @param[in] prefix The minimum prefix size.
878 * @param[in] suffix The minimum suffix size.
879 * @param[in] addr_len The minimum address length.
[1bfd3d3]880 * @return EOK on success.
881 * @return EINVAL if the packet_get_addr() function fails.
882 * @return EINVAL if the packet does not contain the IP header.
883 * @return EPERM if the packet needs to be fragmented and the
[e8199d77]884 * fragmentation is not allowed.
[1bfd3d3]885 * @return ENOMEM if there is not enough memory left.
886 * @return ENOMEM if there is no packet available.
887 * @return ENOMEM if the packet is too small to contain the IP
[e8199d77]888 * header.
[1bfd3d3]889 * @return Other error codes as defined for the packet_trim()
[e8199d77]890 * function.
[1bfd3d3]891 * @return Other error codes as defined for the
[e8199d77]892 * ip_create_middle_header() function.
[1bfd3d3]893 * @return Other error codes as defined for the
[e8199d77]894 * ip_fragment_packet_data() function.
895 */
896static int
[46d4d9f]897ip_fragment_packet(packet_t *packet, size_t length, size_t prefix, size_t suffix,
[e8199d77]898 socklen_t addr_len)
[14f1db0]899{
[46d4d9f]900 packet_t *new_packet;
[88a1bb9]901 ip_header_t *header;
902 ip_header_t *middle_header;
903 ip_header_t *last_header;
[e8199d77]904 struct sockaddr *src;
905 struct sockaddr *dest;
[aadf01e]906 socklen_t addrlen;
907 int result;
[02314f8]908 int rc;
[aadf01e]909
910 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
[e8199d77]911 if (result <= 0)
[aadf01e]912 return EINVAL;
[e8199d77]913
[aadf01e]914 addrlen = (socklen_t) result;
[e8199d77]915 if (packet_get_data_length(packet) <= sizeof(ip_header_t))
[aadf01e]916 return ENOMEM;
[e8199d77]917
[21580dd]918 // get header
[88a1bb9]919 header = (ip_header_t *) packet_get_data(packet);
[e8199d77]920 if (!header)
[aadf01e]921 return EINVAL;
[e8199d77]922
[21580dd]923 // fragmentation forbidden?
[e8199d77]924 if(header->flags & IPFLAG_DONT_FRAGMENT)
[21580dd]925 return EPERM;
[e8199d77]926
[21580dd]927 // create the last fragment
[e8199d77]928 new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, length,
929 suffix, ((addrlen > addr_len) ? addrlen : addr_len));
930 if (!new_packet)
[aadf01e]931 return ENOMEM;
[e8199d77]932
[21580dd]933 // allocate as much as originally
[88a1bb9]934 last_header = (ip_header_t *) packet_suffix(new_packet,
[e8199d77]935 IP_HEADER_LENGTH(header));
936 if (!last_header)
[aadf01e]937 return ip_release_and_return(packet, ENOMEM);
[e8199d77]938
[aadf01e]939 ip_create_last_header(last_header, header);
[e8199d77]940
[21580dd]941 // trim the unused space
[02314f8]942 rc = packet_trim(new_packet, 0,
943 IP_HEADER_LENGTH(header) - IP_HEADER_LENGTH(last_header));
944 if (rc != EOK)
945 return ip_release_and_return(packet, rc);
[e8199d77]946
[21580dd]947 // biggest multiple of 8 lower than content
948 // TODO even fragmentation?
[e8199d77]949 length = length & ~0x7;
[02314f8]950
951 rc = ip_fragment_packet_data(packet, new_packet, header, last_header,
[e8199d77]952 ((IP_HEADER_DATA_LENGTH(header) -
953 ((length - IP_HEADER_LENGTH(header)) & ~0x7)) %
[02314f8]954 ((length - IP_HEADER_LENGTH(last_header)) & ~0x7)),
955 src, dest, addrlen);
956 if (rc != EOK)
957 return ip_release_and_return(packet, rc);
[e8199d77]958
[21580dd]959 // mark the first as fragmented
960 header->flags |= IPFLAG_MORE_FRAGMENTS;
[e8199d77]961
[21580dd]962 // create middle framgents
[e8199d77]963 while (IP_TOTAL_LENGTH(header) > length) {
964 new_packet = packet_get_4_remote(ip_globals.net_phone, prefix,
965 length, suffix,
966 ((addrlen >= addr_len) ? addrlen : addr_len));
967 if (!new_packet)
[aadf01e]968 return ENOMEM;
[e8199d77]969
970 middle_header = ip_create_middle_header(new_packet,
971 last_header);
972 if (!middle_header)
[aadf01e]973 return ip_release_and_return(packet, ENOMEM);
[e8199d77]974
[02314f8]975 rc = ip_fragment_packet_data(packet, new_packet, header,
976 middle_header,
977 (length - IP_HEADER_LENGTH(middle_header)) & ~0x7,
978 src, dest, addrlen);
979 if (rc != EOK)
980 return ip_release_and_return(packet, rc);
[21580dd]981 }
[e8199d77]982
[21580dd]983 // finish the first fragment
[aadf01e]984 header->header_checksum = IP_HEADER_CHECKSUM(header);
[e8199d77]985
[21580dd]986 return EOK;
987}
988
[e8199d77]989/** Checks the packet queue lengths and fragments the packets if needed.
990 *
991 * The ICMP_FRAG_NEEDED error notification may be sent if the packet needs to
992 * be fragmented and the fragmentation is not allowed.
993 *
994 * @param[in,out] packet The packet or the packet queue to be checked.
995 * @param[in] prefix The minimum prefix size.
996 * @param[in] content The maximum content size.
997 * @param[in] suffix The minimum suffix size.
998 * @param[in] addr_len The minimum address length.
999 * @param[in] error The error module service.
[1bfd3d3]1000 * @return The packet or the packet queue of the allowed length.
1001 * @return NULL if there are no packets left.
[e8199d77]1002 */
[46d4d9f]1003static packet_t *
1004ip_split_packet(packet_t *packet, size_t prefix, size_t content, size_t suffix,
[e8199d77]1005 socklen_t addr_len, services_t error)
1006{
1007 size_t length;
[46d4d9f]1008 packet_t *next;
1009 packet_t *new_packet;
[e8199d77]1010 int result;
1011 int phone;
1012
1013 next = packet;
1014 // check all packets
1015 while (next) {
1016 length = packet_get_data_length(next);
1017
1018 if (length <= content) {
1019 next = pq_next(next);
1020 continue;
1021 }
1022
1023 // too long
1024 result = ip_fragment_packet(next, content, prefix,
1025 suffix, addr_len);
1026 if (result != EOK) {
1027 new_packet = pq_detach(next);
1028 if (next == packet) {
1029 // the new first packet of the queue
1030 packet = new_packet;
1031 }
1032 // fragmentation needed?
1033 if (result == EPERM) {
1034 phone = ip_prepare_icmp_and_get_phone(
1035 error, next, NULL);
1036 if (phone >= 0) {
1037 // fragmentation necessary ICMP
1038 icmp_destination_unreachable_msg(phone,
1039 ICMP_FRAG_NEEDED, content, next);
1040 }
1041 } else {
1042 pq_release_remote(ip_globals.net_phone,
1043 packet_get_id(next));
1044 }
1045
1046 next = new_packet;
1047 continue;
1048 }
1049
1050 next = pq_next(next);
1051 }
1052
1053 return packet;
1054}
1055
1056/** Sends the packet or the packet queue via the specified route.
1057 *
1058 * The ICMP_HOST_UNREACH error notification may be sent if route hardware
1059 * destination address is found.
1060 *
1061 * @param[in,out] packet The packet to be sent.
1062 * @param[in] netif The target network interface.
1063 * @param[in] route The target route.
1064 * @param[in] src The source address.
1065 * @param[in] dest The destination address.
1066 * @param[in] error The error module service.
[1bfd3d3]1067 * @return EOK on success.
1068 * @return Other error codes as defined for the arp_translate_req()
[e8199d77]1069 * function.
[1bfd3d3]1070 * @return Other error codes as defined for the ip_prepare_packet()
[e8199d77]1071 * function.
1072 */
[797b704]1073static int ip_send_route(packet_t *packet, ip_netif_t *netif,
1074 ip_route_t *route, in_addr_t *src, in_addr_t dest, services_t error)
[e8199d77]1075{
1076 measured_string_t destination;
[4eca056]1077 measured_string_t *translation;
[61bfc370]1078 uint8_t *data;
[e8199d77]1079 int phone;
[02314f8]1080 int rc;
[aadf01e]1081
[e8199d77]1082 // get destination hardware address
1083 if (netif->arp && (route->address.s_addr != dest.s_addr)) {
1084 destination.value = route->gateway.s_addr ?
[61bfc370]1085 (uint8_t *) &route->gateway.s_addr : (uint8_t *) &dest.s_addr;
[1b59023]1086 destination.length = sizeof(dest.s_addr);
[e8199d77]1087
[02314f8]1088 rc = arp_translate_req(netif->arp->phone, netif->device_id,
1089 SERVICE_IP, &destination, &translation, &data);
1090 if (rc != EOK) {
[e8199d77]1091 pq_release_remote(ip_globals.net_phone,
1092 packet_get_id(packet));
[02314f8]1093 return rc;
[e8199d77]1094 }
1095
1096 if (!translation || !translation->value) {
1097 if (translation) {
1098 free(translation);
1099 free(data);
1100 }
1101 phone = ip_prepare_icmp_and_get_phone(error, packet,
1102 NULL);
1103 if (phone >= 0) {
1104 // unreachable ICMP if no routing
1105 icmp_destination_unreachable_msg(phone,
1106 ICMP_HOST_UNREACH, 0, packet);
1107 }
1108 return EINVAL;
1109 }
1110
1111 } else {
1112 translation = NULL;
[aadf01e]1113 }
[e8199d77]1114
[02314f8]1115 rc = ip_prepare_packet(src, dest, packet, translation);
1116 if (rc != EOK) {
[e8199d77]1117 pq_release_remote(ip_globals.net_phone, packet_get_id(packet));
1118 } else {
1119 packet = ip_split_packet(packet, netif->packet_dimension.prefix,
1120 netif->packet_dimension.content,
1121 netif->packet_dimension.suffix,
1122 netif->packet_dimension.addr_len, error);
1123 if (packet) {
1124 nil_send_msg(netif->phone, netif->device_id, packet,
1125 SERVICE_IP);
1126 }
1127 }
1128
1129 if (translation) {
1130 free(translation);
1131 free(data);
1132 }
1133
[02314f8]1134 return rc;
[21580dd]1135}
1136
[797b704]1137static int ip_send_msg_local(int il_phone, device_id_t device_id,
1138 packet_t *packet, services_t sender, services_t error)
[e8199d77]1139{
[797b704]1140 int addrlen;
1141 ip_netif_t *netif;
[4e5c7ba]1142 ip_route_t *route;
[797b704]1143 struct sockaddr *addr;
1144 struct sockaddr_in *address_in;
1145 in_addr_t *dest;
1146 in_addr_t *src;
1147 int phone;
1148 int rc;
[e8199d77]1149
[797b704]1150 // addresses in the host byte order
1151 // should be the next hop address or the target destination address
1152 addrlen = packet_get_addr(packet, NULL, (uint8_t **) &addr);
1153 if (addrlen < 0)
1154 return ip_release_and_return(packet, addrlen);
1155 if ((size_t) addrlen < sizeof(struct sockaddr))
1156 return ip_release_and_return(packet, EINVAL);
[21580dd]1157
[797b704]1158 switch (addr->sa_family) {
1159 case AF_INET:
1160 if (addrlen != sizeof(struct sockaddr_in))
1161 return ip_release_and_return(packet, EINVAL);
1162 address_in = (struct sockaddr_in *) addr;
1163 dest = &address_in->sin_addr;
1164 if (!dest->s_addr)
1165 dest->s_addr = IPV4_LOCALHOST_ADDRESS;
1166 break;
1167 case AF_INET6:
1168 default:
1169 return ip_release_and_return(packet, EAFNOSUPPORT);
1170 }
[e8199d77]1171
1172 netif = NULL;
1173 route = NULL;
1174 fibril_rwlock_read_lock(&ip_globals.netifs_lock);
1175
1176 // device specified?
1177 if (device_id > 0) {
1178 netif = ip_netifs_find(&ip_globals.netifs, device_id);
[774e6d1a]1179 route = ip_netif_find_route(netif, *dest);
[e8199d77]1180 if (netif && !route && (ip_globals.gateway.netif == netif))
1181 route = &ip_globals.gateway;
1182 }
1183
1184 if (!route) {
1185 route = ip_find_route(*dest);
1186 netif = route ? route->netif : NULL;
1187 }
1188 if (!netif || !route) {
1189 fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
1190 phone = ip_prepare_icmp_and_get_phone(error, packet, NULL);
1191 if (phone >= 0) {
1192 // unreachable ICMP if no routing
1193 icmp_destination_unreachable_msg(phone,
1194 ICMP_NET_UNREACH, 0, packet);
1195 }
1196 return ENOENT;
1197 }
1198
1199 if (error) {
1200 // do not send for broadcast, anycast packets or network
1201 // broadcast
1202 if (!dest->s_addr || !(~dest->s_addr) ||
1203 !(~((dest->s_addr & ~route->netmask.s_addr) |
1204 route->netmask.s_addr)) ||
1205 (!(dest->s_addr & ~route->netmask.s_addr))) {
1206 return ip_release_and_return(packet, EINVAL);
1207 }
1208 }
[774e6d1a]1209
[e8199d77]1210 // if the local host is the destination
1211 if ((route->address.s_addr == dest->s_addr) &&
1212 (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) {
1213 // find the loopback device to deliver
1214 dest->s_addr = IPV4_LOCALHOST_ADDRESS;
1215 route = ip_find_route(*dest);
1216 netif = route ? route->netif : NULL;
1217 if (!netif || !route) {
1218 fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
1219 phone = ip_prepare_icmp_and_get_phone(error, packet,
1220 NULL);
1221 if (phone >= 0) {
1222 // unreachable ICMP if no routing
1223 icmp_destination_unreachable_msg(phone,
1224 ICMP_HOST_UNREACH, 0, packet);
[21580dd]1225 }
[e8199d77]1226 return ENOENT;
1227 }
1228 }
1229
1230 src = ip_netif_address(netif);
1231 if (!src) {
1232 fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
1233 return ip_release_and_return(packet, ENOENT);
1234 }
1235
[02314f8]1236 rc = ip_send_route(packet, netif, route, src, *dest, error);
[e8199d77]1237 fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
1238
[02314f8]1239 return rc;
[e8199d77]1240}
1241
[797b704]1242/** Updates the device state.
[e8199d77]1243 *
1244 * @param[in] device_id The device identifier.
[797b704]1245 * @param[in] state The new state value.
[1bfd3d3]1246 * @return EOK on success.
[797b704]1247 * @return ENOENT if device is not found.
[e8199d77]1248 */
[797b704]1249static int ip_device_state_message(device_id_t device_id, device_state_t state)
[e8199d77]1250{
[4e5c7ba]1251 ip_netif_t *netif;
[e8199d77]1252
[797b704]1253 fibril_rwlock_write_lock(&ip_globals.netifs_lock);
1254 // find the device
1255 netif = ip_netifs_find(&ip_globals.netifs, device_id);
1256 if (!netif) {
1257 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1258 return ENOENT;
[e8199d77]1259 }
[797b704]1260 netif->state = state;
1261 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1262
1263 printf("%s: Device %d changed state to %d\n", NAME, device_id, state);
[e8199d77]1264
1265 return EOK;
1266}
1267
1268/** Returns the packet destination address from the IP header.
1269 *
1270 * @param[in] header The packet IP header to be read.
[1bfd3d3]1271 * @return The packet destination address.
[e8199d77]1272 */
[88a1bb9]1273static in_addr_t ip_get_destination(ip_header_t *header)
[e8199d77]1274{
1275 in_addr_t destination;
1276
1277 // TODO search set ipopt route?
1278 destination.s_addr = header->destination_address;
1279 return destination;
1280}
1281
1282/** Delivers the packet to the local host.
1283 *
1284 * The packet is either passed to another module or released on error.
1285 * The ICMP_PROT_UNREACH error notification may be sent if the protocol is not
1286 * found.
1287 *
1288 * @param[in] device_id The source device identifier.
1289 * @param[in] packet The packet to be delivered.
1290 * @param[in] header The first packet IP header. May be NULL.
1291 * @param[in] error The packet error service.
[1bfd3d3]1292 * @return EOK on success.
1293 * @return ENOTSUP if the packet is a fragment.
1294 * @return EAFNOSUPPORT if the address family is not supported.
1295 * @return ENOENT if the target protocol is not found.
1296 * @return Other error codes as defined for the packet_set_addr()
[e8199d77]1297 * function.
[1bfd3d3]1298 * @return Other error codes as defined for the packet_trim()
[e8199d77]1299 * function.
[1bfd3d3]1300 * @return Other error codes as defined for the protocol specific
[e8199d77]1301 * tl_received_msg() function.
1302 */
[797b704]1303static int ip_deliver_local(device_id_t device_id, packet_t *packet,
1304 ip_header_t *header, services_t error)
[e8199d77]1305{
[4e5c7ba]1306 ip_proto_t *proto;
[e8199d77]1307 int phone;
1308 services_t service;
1309 tl_received_msg_t received_msg;
1310 struct sockaddr *src;
1311 struct sockaddr *dest;
1312 struct sockaddr_in src_in;
1313 struct sockaddr_in dest_in;
1314 socklen_t addrlen;
[02314f8]1315 int rc;
[e8199d77]1316
1317 if ((header->flags & IPFLAG_MORE_FRAGMENTS) ||
1318 IP_FRAGMENT_OFFSET(header)) {
1319 // TODO fragmented
1320 return ENOTSUP;
1321 }
1322
1323 switch (header->version) {
1324 case IPVERSION:
1325 addrlen = sizeof(src_in);
1326 bzero(&src_in, addrlen);
1327 src_in.sin_family = AF_INET;
1328 memcpy(&dest_in, &src_in, addrlen);
1329 memcpy(&src_in.sin_addr.s_addr, &header->source_address,
1330 sizeof(header->source_address));
1331 memcpy(&dest_in.sin_addr.s_addr, &header->destination_address,
1332 sizeof(header->destination_address));
1333 src = (struct sockaddr *) &src_in;
1334 dest = (struct sockaddr *) &dest_in;
1335 break;
1336
1337 default:
1338 return ip_release_and_return(packet, EAFNOSUPPORT);
1339 }
1340
[02314f8]1341 rc = packet_set_addr(packet, (uint8_t *) src, (uint8_t *) dest,
1342 addrlen);
1343 if (rc != EOK)
1344 return ip_release_and_return(packet, rc);
[e8199d77]1345
1346 // trim padding if present
1347 if (!error &&
1348 (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) {
[02314f8]1349 rc = packet_trim(packet, 0,
1350 packet_get_data_length(packet) - IP_TOTAL_LENGTH(header));
1351 if (rc != EOK)
1352 return ip_release_and_return(packet, rc);
[e8199d77]1353 }
1354
1355 fibril_rwlock_read_lock(&ip_globals.protos_lock);
1356
1357 proto = ip_protos_find(&ip_globals.protos, header->protocol);
1358 if (!proto) {
1359 fibril_rwlock_read_unlock(&ip_globals.protos_lock);
1360 phone = ip_prepare_icmp_and_get_phone(error, packet, header);
1361 if (phone >= 0) {
1362 // unreachable ICMP
1363 icmp_destination_unreachable_msg(phone,
1364 ICMP_PROT_UNREACH, 0, packet);
[21580dd]1365 }
[e8199d77]1366 return ENOENT;
[21580dd]1367 }
1368
[e8199d77]1369 if (proto->received_msg) {
1370 service = proto->service;
1371 received_msg = proto->received_msg;
1372 fibril_rwlock_read_unlock(&ip_globals.protos_lock);
[02314f8]1373 rc = received_msg(device_id, packet, service, error);
[e8199d77]1374 } else {
[02314f8]1375 rc = tl_received_msg(proto->phone, device_id, packet,
[e8199d77]1376 proto->service, error);
1377 fibril_rwlock_read_unlock(&ip_globals.protos_lock);
1378 }
[21580dd]1379
[02314f8]1380 return rc;
[21580dd]1381}
1382
[e8199d77]1383/** Processes the received packet.
1384 *
1385 * The packet is either passed to another module or released on error.
1386 *
1387 * The ICMP_PARAM_POINTER error notification may be sent if the checksum is
1388 * invalid.
1389 * The ICMP_EXC_TTL error notification may be sent if the TTL is less than two.
1390 * The ICMP_HOST_UNREACH error notification may be sent if no route was found.
1391 * The ICMP_HOST_UNREACH error notification may be sent if the packet is for
1392 * another host and the routing is disabled.
1393 *
1394 * @param[in] device_id The source device identifier.
1395 * @param[in] packet The received packet to be processed.
[1bfd3d3]1396 * @return EOK on success.
1397 * @return EINVAL if the TTL is less than two.
1398 * @return EINVAL if the checksum is invalid.
1399 * @return EAFNOSUPPORT if the address family is not supported.
1400 * @return ENOENT if no route was found.
1401 * @return ENOENT if the packet is for another host and the routing
[e8199d77]1402 * is disabled.
1403 */
[797b704]1404static int ip_process_packet(device_id_t device_id, packet_t *packet)
[e8199d77]1405{
[88a1bb9]1406 ip_header_t *header;
[aadf01e]1407 in_addr_t dest;
[4e5c7ba]1408 ip_route_t *route;
[aadf01e]1409 int phone;
[e8199d77]1410 struct sockaddr *addr;
[aadf01e]1411 struct sockaddr_in addr_in;
1412 socklen_t addrlen;
[02314f8]1413 int rc;
[774e6d1a]1414
[88a1bb9]1415 header = (ip_header_t *) packet_get_data(packet);
[e8199d77]1416 if (!header)
[aadf01e]1417 return ip_release_and_return(packet, ENOMEM);
[e8199d77]1418
[21580dd]1419 // checksum
[e8199d77]1420 if ((header->header_checksum) &&
1421 (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) {
[aadf01e]1422 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
[e8199d77]1423 if (phone >= 0) {
[21580dd]1424 // checksum error ICMP
[e8199d77]1425 icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER,
1426 ((size_t) ((void *) &header->header_checksum)) -
1427 ((size_t) ((void *) header)), packet);
[21580dd]1428 }
1429 return EINVAL;
1430 }
[e8199d77]1431
1432 if (header->ttl <= 1) {
[aadf01e]1433 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
[e8199d77]1434 if (phone >= 0) {
1435 // ttl exceeded ICMP
[aadf01e]1436 icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet);
[21580dd]1437 }
1438 return EINVAL;
1439 }
[774e6d1a]1440
[21580dd]1441 // process ipopt and get destination
[aadf01e]1442 dest = ip_get_destination(header);
[e8199d77]1443
[21580dd]1444 // set the addrination address
[e8199d77]1445 switch (header->version) {
1446 case IPVERSION:
1447 addrlen = sizeof(addr_in);
1448 bzero(&addr_in, addrlen);
1449 addr_in.sin_family = AF_INET;
1450 memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest));
1451 addr = (struct sockaddr *) &addr_in;
1452 break;
1453
1454 default:
1455 return ip_release_and_return(packet, EAFNOSUPPORT);
[21580dd]1456 }
[e8199d77]1457
[02314f8]1458 rc = packet_set_addr(packet, NULL, (uint8_t *) &addr, addrlen);
1459 if (rc != EOK)
1460 return rc;
[774e6d1a]1461
[aadf01e]1462 route = ip_find_route(dest);
[e8199d77]1463 if (!route) {
[aadf01e]1464 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
[e8199d77]1465 if (phone >= 0) {
[21580dd]1466 // unreachable ICMP
[e8199d77]1467 icmp_destination_unreachable_msg(phone,
1468 ICMP_HOST_UNREACH, 0, packet);
[21580dd]1469 }
1470 return ENOENT;
1471 }
[e8199d77]1472
1473 if (route->address.s_addr == dest.s_addr) {
[21580dd]1474 // local delivery
[aadf01e]1475 return ip_deliver_local(device_id, packet, header, 0);
[21580dd]1476 }
[e8199d77]1477
1478 if (route->netif->routing) {
1479 header->ttl--;
1480 return ip_send_route(packet, route->netif, route, NULL, dest,
1481 0);
1482 }
1483
1484 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
1485 if (phone >= 0) {
1486 // unreachable ICMP if no routing
1487 icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0,
1488 packet);
1489 }
1490
1491 return ENOENT;
1492}
1493
[797b704]1494/** Returns the device packet dimensions for sending.
1495 *
1496 * @param[in] phone The service module phone.
1497 * @param[in] message The service specific message.
1498 * @param[in] device_id The device identifier.
1499 * @param[out] addr_len The minimum reserved address length.
1500 * @param[out] prefix The minimum reserved prefix size.
1501 * @param[out] content The maximum content size.
1502 * @param[out] suffix The minimum reserved suffix size.
1503 * @return EOK on success.
1504 */
1505static int ip_packet_size_message(device_id_t device_id, size_t *addr_len,
1506 size_t *prefix, size_t *content, size_t *suffix)
1507{
1508 ip_netif_t *netif;
1509 int index;
1510
1511 if (!addr_len || !prefix || !content || !suffix)
1512 return EBADMEM;
1513
1514 *content = IP_MAX_CONTENT - IP_PREFIX;
1515 fibril_rwlock_read_lock(&ip_globals.netifs_lock);
1516 if (device_id < 0) {
1517 *addr_len = IP_ADDR;
1518 *prefix = 0;
1519 *suffix = 0;
1520
1521 for (index = ip_netifs_count(&ip_globals.netifs) - 1;
1522 index >= 0; index--) {
1523 netif = ip_netifs_get_index(&ip_globals.netifs, index);
1524 if (!netif)
1525 continue;
1526
1527 if (netif->packet_dimension.addr_len > *addr_len)
1528 *addr_len = netif->packet_dimension.addr_len;
1529
1530 if (netif->packet_dimension.prefix > *prefix)
1531 *prefix = netif->packet_dimension.prefix;
1532
1533 if (netif->packet_dimension.suffix > *suffix)
1534 *suffix = netif->packet_dimension.suffix;
1535 }
1536
1537 *prefix = *prefix + IP_PREFIX;
1538 *suffix = *suffix + IP_SUFFIX;
1539 } else {
1540 netif = ip_netifs_find(&ip_globals.netifs, device_id);
1541 if (!netif) {
1542 fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
1543 return ENOENT;
1544 }
1545
1546 *addr_len = (netif->packet_dimension.addr_len > IP_ADDR) ?
1547 netif->packet_dimension.addr_len : IP_ADDR;
1548 *prefix = netif->packet_dimension.prefix + IP_PREFIX;
1549 *suffix = netif->packet_dimension.suffix + IP_SUFFIX;
1550 }
1551 fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
1552
1553 return EOK;
1554}
1555
1556/** Updates the device content length according to the new MTU value.
1557 *
1558 * @param[in] device_id The device identifier.
1559 * @param[in] mtu The new mtu value.
1560 * @return EOK on success.
1561 * @return ENOENT if device is not found.
1562 */
1563static int ip_mtu_changed_message(device_id_t device_id, size_t mtu)
1564{
1565 ip_netif_t *netif;
1566
1567 fibril_rwlock_write_lock(&ip_globals.netifs_lock);
1568 netif = ip_netifs_find(&ip_globals.netifs, device_id);
1569 if (!netif) {
1570 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1571 return ENOENT;
1572 }
1573 netif->packet_dimension.content = mtu;
1574 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1575
1576 printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu);
1577
1578 return EOK;
1579}
1580
1581/** Process IPC messages from the registered device driver modules
1582 *
1583 * @param[in] iid Message identifier.
1584 * @param[in,out] icall Message parameters.
1585 *
1586 */
1587static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall)
1588{
1589 packet_t *packet;
1590 int rc;
1591
1592 while (true) {
1593 switch (IPC_GET_IMETHOD(*icall)) {
1594 case NET_IL_DEVICE_STATE:
1595 rc = ip_device_state_message(IPC_GET_DEVICE(*icall),
1596 IPC_GET_STATE(*icall));
[ffa2c8ef]1597 async_answer_0(iid, (sysarg_t) rc);
[797b704]1598 break;
1599
1600 case NET_IL_RECEIVED:
1601 rc = packet_translate_remote(ip_globals.net_phone, &packet,
1602 IPC_GET_PACKET(*icall));
1603 if (rc == EOK) {
1604 do {
1605 packet_t *next = pq_detach(packet);
1606 ip_process_packet(IPC_GET_DEVICE(*icall), packet);
1607 packet = next;
1608 } while (packet);
1609 }
1610
[ffa2c8ef]1611 async_answer_0(iid, (sysarg_t) rc);
[797b704]1612 break;
1613
1614 case NET_IL_MTU_CHANGED:
1615 rc = ip_mtu_changed_message(IPC_GET_DEVICE(*icall),
1616 IPC_GET_MTU(*icall));
[ffa2c8ef]1617 async_answer_0(iid, (sysarg_t) rc);
[797b704]1618 break;
1619
1620 default:
[ffa2c8ef]1621 async_answer_0(iid, (sysarg_t) ENOTSUP);
[797b704]1622 }
1623
1624 iid = async_get_call(icall);
1625 }
1626}
1627
1628/** Registers the transport layer protocol.
1629 *
1630 * The traffic of this protocol will be supplied using either the receive
1631 * function or IPC message.
1632 *
1633 * @param[in] protocol The transport layer module protocol.
1634 * @param[in] service The transport layer module service.
1635 * @param[in] phone The transport layer module phone.
1636 * @param[in] received_msg The receiving function.
1637 * @return EOK on success.
1638 * @return EINVAL if the protocol parameter and/or the service
1639 * parameter is zero.
1640 * @return EINVAL if the phone parameter is not a positive number
1641 * and the tl_receive_msg is NULL.
1642 * @return ENOMEM if there is not enough memory left.
1643 */
1644static int
1645ip_register(int protocol, services_t service, int phone,
1646 tl_received_msg_t received_msg)
1647{
1648 ip_proto_t *proto;
1649 int index;
1650
1651 if (!protocol || !service || ((phone < 0) && !received_msg))
1652 return EINVAL;
1653
1654 proto = (ip_proto_t *) malloc(sizeof(ip_protos_t));
1655 if (!proto)
1656 return ENOMEM;
1657
1658 proto->protocol = protocol;
1659 proto->service = service;
1660 proto->phone = phone;
1661 proto->received_msg = received_msg;
1662
1663 fibril_rwlock_write_lock(&ip_globals.protos_lock);
1664 index = ip_protos_add(&ip_globals.protos, proto->protocol, proto);
1665 if (index < 0) {
1666 fibril_rwlock_write_unlock(&ip_globals.protos_lock);
1667 free(proto);
1668 return index;
1669 }
1670 fibril_rwlock_write_unlock(&ip_globals.protos_lock);
1671
1672 printf("%s: Protocol registered (protocol: %d, phone: %d)\n",
1673 NAME, proto->protocol, proto->phone);
1674
1675 return EOK;
1676}
1677
1678
[e8199d77]1679static int
1680ip_add_route_req_local(int ip_phone, device_id_t device_id, in_addr_t address,
1681 in_addr_t netmask, in_addr_t gateway)
1682{
[4e5c7ba]1683 ip_route_t *route;
1684 ip_netif_t *netif;
[e8199d77]1685 int index;
1686
1687 fibril_rwlock_write_lock(&ip_globals.netifs_lock);
1688
1689 netif = ip_netifs_find(&ip_globals.netifs, device_id);
1690 if (!netif) {
1691 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1692 return ENOENT;
1693 }
1694
[4e5c7ba]1695 route = (ip_route_t *) malloc(sizeof(ip_route_t));
[e8199d77]1696 if (!route) {
1697 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1698 return ENOMEM;
1699 }
1700
1701 route->address.s_addr = address.s_addr;
1702 route->netmask.s_addr = netmask.s_addr;
1703 route->gateway.s_addr = gateway.s_addr;
1704 route->netif = netif;
1705 index = ip_routes_add(&netif->routes, route);
1706 if (index < 0)
1707 free(route);
1708
1709 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1710
1711 return index;
1712}
1713
1714static int
1715ip_set_gateway_req_local(int ip_phone, device_id_t device_id, in_addr_t gateway)
1716{
[4e5c7ba]1717 ip_netif_t *netif;
[e8199d77]1718
1719 fibril_rwlock_write_lock(&ip_globals.netifs_lock);
1720
1721 netif = ip_netifs_find(&ip_globals.netifs, device_id);
1722 if (!netif) {
1723 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1724 return ENOENT;
1725 }
1726
1727 ip_globals.gateway.address.s_addr = 0;
1728 ip_globals.gateway.netmask.s_addr = 0;
1729 ip_globals.gateway.gateway.s_addr = gateway.s_addr;
1730 ip_globals.gateway.netif = netif;
1731
1732 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
1733
1734 return EOK;
[21580dd]1735}
1736
[14f1db0]1737/** Notify the IP module about the received error notification packet.
1738 *
[e8199d77]1739 * @param[in] ip_phone The IP module phone used for (semi)remote calls.
1740 * @param[in] device_id The device identifier.
1741 * @param[in] packet The received packet or the received packet queue.
1742 * @param[in] target The target internetwork module service to be
1743 * delivered to.
1744 * @param[in] error The packet error reporting service. Prefixes the
1745 * received packet.
1746 * @return EOK on success.
[14f1db0]1747 *
1748 */
[e8199d77]1749static int
1750ip_received_error_msg_local(int ip_phone, device_id_t device_id,
[46d4d9f]1751 packet_t *packet, services_t target, services_t error)
[e8199d77]1752{
1753 uint8_t *data;
[aadf01e]1754 int offset;
1755 icmp_type_t type;
1756 icmp_code_t code;
[4e5c7ba]1757 ip_netif_t *netif;
[aadf01e]1758 measured_string_t address;
[4e5c7ba]1759 ip_route_t *route;
[88a1bb9]1760 ip_header_t *header;
[aadf01e]1761
[e8199d77]1762 switch (error) {
1763 case SERVICE_ICMP:
1764 offset = icmp_client_process_packet(packet, &type, &code, NULL,
1765 NULL);
1766 if (offset < 0)
1767 return ip_release_and_return(packet, ENOMEM);
[21580dd]1768
[e8199d77]1769 data = packet_get_data(packet);
[88a1bb9]1770 header = (ip_header_t *)(data + offset);
[21580dd]1771
[e8199d77]1772 // destination host unreachable?
1773 if ((type != ICMP_DEST_UNREACH) ||
1774 (code != ICMP_HOST_UNREACH)) {
1775 // no, something else
1776 break;
[21580dd]1777 }
1778
[e8199d77]1779 fibril_rwlock_read_lock(&ip_globals.netifs_lock);
[21580dd]1780
[e8199d77]1781 netif = ip_netifs_find(&ip_globals.netifs, device_id);
1782 if (!netif || !netif->arp) {
1783 fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
[21580dd]1784 break;
[e8199d77]1785 }
[21580dd]1786
[e8199d77]1787 route = ip_routes_get_index(&netif->routes, 0);
1788
1789 // from the same network?
1790 if (route && ((route->address.s_addr & route->netmask.s_addr) ==
1791 (header->destination_address & route->netmask.s_addr))) {
1792 // clear the ARP mapping if any
[61bfc370]1793 address.value = (uint8_t *) &header->destination_address;
[7837101]1794 address.length = sizeof(header->destination_address);
[e8199d77]1795 arp_clear_address_req(netif->arp->phone,
1796 netif->device_id, SERVICE_IP, &address);
1797 }
[21580dd]1798
[e8199d77]1799 fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
1800 break;
[21580dd]1801
[e8199d77]1802 default:
1803 return ip_release_and_return(packet, ENOTSUP);
[21580dd]1804 }
1805
[e8199d77]1806 return ip_deliver_local(device_id, packet, header, error);
[21580dd]1807}
1808
[e8199d77]1809static int
1810ip_get_route_req_local(int ip_phone, ip_protocol_t protocol,
1811 const struct sockaddr *destination, socklen_t addrlen,
1812 device_id_t *device_id, void **header, size_t *headerlen)
1813{
1814 struct sockaddr_in *address_in;
1815 in_addr_t *dest;
1816 in_addr_t *src;
[4e5c7ba]1817 ip_route_t *route;
[88a1bb9]1818 ipv4_pseudo_header_t *header_in;
[aadf01e]1819
[e8199d77]1820 if (!destination || (addrlen <= 0))
[21580dd]1821 return EINVAL;
[e8199d77]1822
1823 if (!device_id || !header || !headerlen)
[aadf01e]1824 return EBADMEM;
[e8199d77]1825
1826 if ((size_t) addrlen < sizeof(struct sockaddr))
[aadf01e]1827 return EINVAL;
[e8199d77]1828
1829 switch (destination->sa_family) {
1830 case AF_INET:
1831 if (addrlen != sizeof(struct sockaddr_in))
1832 return EINVAL;
1833 address_in = (struct sockaddr_in *) destination;
1834 dest = &address_in->sin_addr;
1835 if (!dest->s_addr)
1836 dest->s_addr = IPV4_LOCALHOST_ADDRESS;
1837 break;
1838
1839 case AF_INET6:
1840 default:
1841 return EAFNOSUPPORT;
[aadf01e]1842 }
[e8199d77]1843
[aadf01e]1844 fibril_rwlock_read_lock(&ip_globals.lock);
1845 route = ip_find_route(*dest);
[836dd794]1846 // if the local host is the destination
[e8199d77]1847 if (route && (route->address.s_addr == dest->s_addr) &&
1848 (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) {
[836dd794]1849 // find the loopback device to deliver
1850 dest->s_addr = IPV4_LOCALHOST_ADDRESS;
1851 route = ip_find_route(*dest);
1852 }
[e8199d77]1853
1854 if (!route || !route->netif) {
[aadf01e]1855 fibril_rwlock_read_unlock(&ip_globals.lock);
[21580dd]1856 return ENOENT;
1857 }
[e8199d77]1858
[aadf01e]1859 *device_id = route->netif->device_id;
1860 src = ip_netif_address(route->netif);
1861 fibril_rwlock_read_unlock(&ip_globals.lock);
[e8199d77]1862
[aadf01e]1863 *headerlen = sizeof(*header_in);
[88a1bb9]1864 header_in = (ipv4_pseudo_header_t *) malloc(*headerlen);
[e8199d77]1865 if (!header_in)
[aadf01e]1866 return ENOMEM;
[e8199d77]1867
1868 bzero(header_in, *headerlen);
[21580dd]1869 header_in->destination_address = dest->s_addr;
1870 header_in->source_address = src->s_addr;
1871 header_in->protocol = protocol;
1872 header_in->data_length = 0;
[14f1db0]1873 *header = header_in;
[e8199d77]1874
[21580dd]1875 return EOK;
1876}
1877
[e8199d77]1878/** Processes the IP message.
1879 *
1880 * @param[in] callid The message identifier.
1881 * @param[in] call The message parameters.
1882 * @param[out] answer The message answer parameters.
1883 * @param[out] answer_count The last parameter for the actual answer in the
1884 * answer parameter.
[1bfd3d3]1885 * @return EOK on success.
1886 * @return ENOTSUP if the message is not known.
[849ed54]1887 *
[e8199d77]1888 * @see ip_interface.h
[797b704]1889 * @see il_remote.h
[e8199d77]1890 * @see IS_NET_IP_MESSAGE()
1891 */
[797b704]1892int il_module_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
[774e6d1a]1893 size_t *answer_count)
[e8199d77]1894{
[46d4d9f]1895 packet_t *packet;
[e8199d77]1896 struct sockaddr *addr;
[797b704]1897 void *header;
1898 size_t headerlen;
[e8199d77]1899 size_t addrlen;
1900 size_t prefix;
1901 size_t suffix;
1902 size_t content;
1903 device_id_t device_id;
[02314f8]1904 int rc;
[e8199d77]1905
1906 *answer_count = 0;
[228e490]1907 switch (IPC_GET_IMETHOD(*call)) {
[e8199d77]1908 case IPC_M_PHONE_HUNGUP:
1909 return EOK;
1910
1911 case IPC_M_CONNECT_TO_ME:
[774e6d1a]1912 return ip_register(IL_GET_PROTO(*call), IL_GET_SERVICE(*call),
1913 IPC_GET_PHONE(*call), NULL);
[e8199d77]1914
[797b704]1915 case NET_IP_DEVICE:
[774e6d1a]1916 return ip_device_req_local(0, IPC_GET_DEVICE(*call),
1917 IPC_GET_SERVICE(*call));
[e8199d77]1918
1919 case NET_IP_RECEIVED_ERROR:
[02314f8]1920 rc = packet_translate_remote(ip_globals.net_phone, &packet,
[774e6d1a]1921 IPC_GET_PACKET(*call));
[02314f8]1922 if (rc != EOK)
1923 return rc;
[774e6d1a]1924 return ip_received_error_msg_local(0, IPC_GET_DEVICE(*call),
1925 packet, IPC_GET_TARGET(*call), IPC_GET_ERROR(*call));
[e8199d77]1926
1927 case NET_IP_ADD_ROUTE:
[774e6d1a]1928 return ip_add_route_req_local(0, IPC_GET_DEVICE(*call),
1929 IP_GET_ADDRESS(*call), IP_GET_NETMASK(*call),
1930 IP_GET_GATEWAY(*call));
[e8199d77]1931
1932 case NET_IP_SET_GATEWAY:
[774e6d1a]1933 return ip_set_gateway_req_local(0, IPC_GET_DEVICE(*call),
1934 IP_GET_GATEWAY(*call));
[e8199d77]1935
1936 case NET_IP_GET_ROUTE:
[7880d58]1937 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0,
1938 &addrlen);
[02314f8]1939 if (rc != EOK)
1940 return rc;
1941
[774e6d1a]1942 rc = ip_get_route_req_local(0, IP_GET_PROTOCOL(*call), addr,
[02314f8]1943 (socklen_t) addrlen, &device_id, &header, &headerlen);
1944 if (rc != EOK)
1945 return rc;
1946
[774e6d1a]1947 IPC_SET_DEVICE(*answer, device_id);
1948 IP_SET_HEADERLEN(*answer, headerlen);
[e8199d77]1949
1950 *answer_count = 2;
[02314f8]1951
1952 rc = data_reply(&headerlen, sizeof(headerlen));
1953 if (rc == EOK)
1954 rc = data_reply(header, headerlen);
[e8199d77]1955
1956 free(header);
[02314f8]1957 return rc;
[e8199d77]1958
[797b704]1959 case NET_IP_PACKET_SPACE:
[774e6d1a]1960 rc = ip_packet_size_message(IPC_GET_DEVICE(*call), &addrlen,
[02314f8]1961 &prefix, &content, &suffix);
1962 if (rc != EOK)
1963 return rc;
1964
[774e6d1a]1965 IPC_SET_ADDR(*answer, addrlen);
1966 IPC_SET_PREFIX(*answer, prefix);
1967 IPC_SET_CONTENT(*answer, content);
1968 IPC_SET_SUFFIX(*answer, suffix);
[e8199d77]1969 *answer_count = 4;
1970 return EOK;
1971
[797b704]1972 case NET_IP_SEND:
1973 rc = packet_translate_remote(ip_globals.net_phone, &packet,
1974 IPC_GET_PACKET(*call));
1975 if (rc != EOK)
1976 return rc;
1977
1978 return ip_send_msg_local(0, IPC_GET_DEVICE(*call), packet, 0,
1979 IPC_GET_ERROR(*call));
[e8199d77]1980 }
1981
1982 return ENOTSUP;
1983}
1984
[849ed54]1985int main(int argc, char *argv[])
1986{
1987 /* Start the module */
[797b704]1988 return il_module_start(SERVICE_IP);
[849ed54]1989}
1990
[21580dd]1991/** @}
1992 */
Note: See TracBrowser for help on using the repository browser.