Changeset 357b5f5 in mainline for uspace/srv/net/il/ip/ip.c


Ignore:
Timestamp:
2011-01-23T20:09:13Z (15 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fdb9982c
Parents:
cead2aa (diff), 7e36c8d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/il/ip/ip.c

    rcead2aa r357b5f5  
    3535 * @see arp.h
    3636 */
    37 
    38 #include "ip.h"
    39 #include "ip_module.h"
    4037
    4138#include <async.h>
     
    5249#include <sys/types.h>
    5350#include <byteorder.h>
     51#include "ip.h"
    5452
    5553#include <adt/measured_strings.h>
     
    6967#include <net_checksum.h>
    7068#include <icmp_client.h>
    71 #include <icmp_interface.h>
    72 #include <il_interface.h>
     69#include <icmp_remote.h>
    7370#include <ip_client.h>
    7471#include <ip_interface.h>
    7572#include <ip_header.h>
    7673#include <net_interface.h>
    77 #include <nil_interface.h>
    78 #include <tl_interface.h>
     74#include <nil_remote.h>
     75#include <tl_remote.h>
    7976#include <packet_remote.h>
    80 #include <il_local.h>
     77#include <il_remote.h>
     78#include <il_skel.h>
    8179
    8280/** IP module name. */
     
    122120INT_MAP_IMPLEMENT(ip_protos, ip_proto_t);
    123121GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t);
     122
     123static void ip_receiver(ipc_callid_t, ipc_call_t *);
    124124
    125125/** Releases the packet and returns the result.
     
    244244}
    245245
    246 /** Initializes the IP module.
    247  *
    248  * @param[in] client_connection The client connection processing function. The
    249  *                      module skeleton propagates its own one.
    250  * @return              EOK on success.
    251  * @return              ENOMEM if there is not enough memory left.
    252  */
    253 int ip_initialize(async_client_conn_t client_connection)
    254 {
    255         int rc;
    256 
     246int il_initialize(int net_phone)
     247{
    257248        fibril_rwlock_initialize(&ip_globals.lock);
    258249        fibril_rwlock_write_lock(&ip_globals.lock);
    259250        fibril_rwlock_initialize(&ip_globals.protos_lock);
    260251        fibril_rwlock_initialize(&ip_globals.netifs_lock);
     252       
     253        ip_globals.net_phone = net_phone;
    261254        ip_globals.packet_counter = 0;
    262255        ip_globals.gateway.address.s_addr = 0;
     
    264257        ip_globals.gateway.gateway.s_addr = 0;
    265258        ip_globals.gateway.netif = NULL;
    266         ip_globals.client_connection = client_connection;
    267        
    268         rc = ip_netifs_initialize(&ip_globals.netifs);
     259       
     260        int rc = ip_netifs_initialize(&ip_globals.netifs);
    269261        if (rc != EOK)
    270262                goto out;
     
    275267        if (rc != EOK)
    276268                goto out;
    277         rc = add_module(NULL, &ip_globals.modules, ARP_NAME, ARP_FILENAME,
    278             SERVICE_ARP, 0, arp_connect_module);
     269        rc = add_module(NULL, &ip_globals.modules, (uint8_t *) ARP_NAME,
     270            (uint8_t *) ARP_FILENAME, SERVICE_ARP, 0, arp_connect_module);
    279271
    280272out:
     
    312304        measured_string_t names[] = {
    313305                {
    314                         (char *) "IPV",
     306                        (uint8_t *) "IPV",
    315307                        3
    316308                },
    317309                {
    318                         (char *) "IP_CONFIG",
     310                        (uint8_t *) "IP_CONFIG",
    319311                        9
    320312                },
    321313                {
    322                         (char *) "IP_ADDR",
     314                        (uint8_t *) "IP_ADDR",
    323315                        7
    324316                },
    325317                {
    326                         (char *) "IP_NETMASK",
     318                        (uint8_t *) "IP_NETMASK",
    327319                        10
    328320                },
    329321                {
    330                         (char *) "IP_GATEWAY",
     322                        (uint8_t *) "IP_GATEWAY",
    331323                        10
    332324                },
    333325                {
    334                         (char *) "IP_BROADCAST",
     326                        (uint8_t *) "IP_BROADCAST",
    335327                        12
    336328                },
    337329                {
    338                         (char *) "ARP",
     330                        (uint8_t *) "ARP",
    339331                        3
    340332                },
    341333                {
    342                         (char *) "IP_ROUTING",
     334                        (uint8_t *) "IP_ROUTING",
    343335                        10
    344336                }
     
    346338        measured_string_t *configuration;
    347339        size_t count = sizeof(names) / sizeof(measured_string_t);
    348         char *data;
     340        uint8_t *data;
    349341        measured_string_t address;
    350342        ip_route_t *route;
     
    368360        if (configuration) {
    369361                if (configuration[0].value)
    370                         ip_netif->ipv = strtol(configuration[0].value, NULL, 0);
    371 
    372                 ip_netif->dhcp = !str_lcmp(configuration[1].value, "dhcp",
     362                        ip_netif->ipv = strtol((char *) configuration[0].value, NULL, 0);
     363               
     364                ip_netif->dhcp = !str_lcmp((char *) configuration[1].value, "dhcp",
    373365                    configuration[1].length);
    374366               
     
    394386                        }
    395387                       
    396                         if ((inet_pton(AF_INET, configuration[2].value,
     388                        if ((inet_pton(AF_INET, (char *) configuration[2].value,
    397389                            (uint8_t *) &route->address.s_addr) != EOK) ||
    398                             (inet_pton(AF_INET, configuration[3].value,
     390                            (inet_pton(AF_INET, (char *) configuration[3].value,
    399391                            (uint8_t *) &route->netmask.s_addr) != EOK) ||
    400                             (inet_pton(AF_INET, configuration[4].value,
     392                            (inet_pton(AF_INET, (char *) configuration[4].value,
    401393                            (uint8_t *) &gateway.s_addr) == EINVAL) ||
    402                             (inet_pton(AF_INET, configuration[5].value,
     394                            (inet_pton(AF_INET, (char *) configuration[5].value,
    403395                            (uint8_t *) &ip_netif->broadcast.s_addr) == EINVAL))
    404396                            {
     
    430422        // binds the netif service which also initializes the device
    431423        ip_netif->phone = nil_bind_service(ip_netif->service,
    432             (ipcarg_t) ip_netif->device_id, SERVICE_IP,
    433             ip_globals.client_connection);
     424            (sysarg_t) ip_netif->device_id, SERVICE_IP,
     425            ip_receiver);
    434426        if (ip_netif->phone < 0) {
    435427                printf("Failed to contact the nil service %d\n",
     
    441433        if (ip_netif->arp) {
    442434                if (route) {
    443                         address.value = (char *) &route->address.s_addr;
    444                         address.length = CONVERT_SIZE(in_addr_t, char, 1);
     435                        address.value = (uint8_t *) &route->address.s_addr;
     436                        address.length = sizeof(in_addr_t);
    445437                       
    446438                        rc = arp_device_req(ip_netif->arp->phone,
     
    477469                ip_globals.gateway.gateway.s_addr = gateway.s_addr;
    478470                ip_globals.gateway.netif = ip_netif;
     471               
     472                char defgateway[INET_ADDRSTRLEN];
     473                inet_ntop(AF_INET, (uint8_t *) &gateway.s_addr,
     474                    defgateway, INET_ADDRSTRLEN);
     475                printf("%s: Default gateway (%s)\n", NAME, defgateway);
    479476        }
    480477
     
    482479}
    483480
    484 /** Updates the device content length according to the new MTU value.
    485  *
    486  * @param[in] device_id The device identifier.
    487  * @param[in] mtu       The new mtu value.
    488  * @return              EOK on success.
    489  * @return              ENOENT if device is not found.
    490  */
    491 static int ip_mtu_changed_message(device_id_t device_id, size_t mtu)
    492 {
     481static int ip_device_req_local(int il_phone, device_id_t device_id,
     482    services_t netif)
     483{
     484        ip_netif_t *ip_netif;
     485        ip_route_t *route;
     486        int index;
     487        int rc;
     488
     489        ip_netif = (ip_netif_t *) malloc(sizeof(ip_netif_t));
     490        if (!ip_netif)
     491                return ENOMEM;
     492
     493        rc = ip_routes_initialize(&ip_netif->routes);
     494        if (rc != EOK) {
     495                free(ip_netif);
     496                return rc;
     497        }
     498
     499        ip_netif->device_id = device_id;
     500        ip_netif->service = netif;
     501        ip_netif->state = NETIF_STOPPED;
     502
     503        fibril_rwlock_write_lock(&ip_globals.netifs_lock);
     504
     505        rc = ip_netif_initialize(ip_netif);
     506        if (rc != EOK) {
     507                fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
     508                ip_routes_destroy(&ip_netif->routes);
     509                free(ip_netif);
     510                return rc;
     511        }
     512        if (ip_netif->arp)
     513                ip_netif->arp->usage++;
     514
     515        // print the settings
     516        printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n",
     517            NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv,
     518            ip_netif->dhcp ? "dhcp" : "static");
     519       
     520        // TODO ipv6 addresses
     521       
     522        char address[INET_ADDRSTRLEN];
     523        char netmask[INET_ADDRSTRLEN];
     524        char gateway[INET_ADDRSTRLEN];
     525       
     526        for (index = 0; index < ip_routes_count(&ip_netif->routes); index++) {
     527                route = ip_routes_get_index(&ip_netif->routes, index);
     528                if (route) {
     529                        inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr,
     530                            address, INET_ADDRSTRLEN);
     531                        inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr,
     532                            netmask, INET_ADDRSTRLEN);
     533                        inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr,
     534                            gateway, INET_ADDRSTRLEN);
     535                        printf("%s: Route %d (address: %s, netmask: %s, "
     536                            "gateway: %s)\n", NAME, index, address, netmask,
     537                            gateway);
     538                }
     539        }
     540       
     541        inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address,
     542            INET_ADDRSTRLEN);
     543        fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
     544
     545        printf("%s: Broadcast (%s)\n", NAME, address);
     546
     547        return EOK;
     548}
     549
     550/** Searches the network interfaces if there is a suitable route.
     551 *
     552 * @param[in] netif     The network interface to be searched for routes. May be
     553 *                      NULL.
     554 * @param[in] destination The destination address.
     555 * @return              The found route.
     556 * @return              NULL if no route was found.
     557 */
     558static ip_route_t *ip_netif_find_route(ip_netif_t *netif,
     559    in_addr_t destination)
     560{
     561        int index;
     562        ip_route_t *route;
     563       
     564        if (!netif)
     565                return NULL;
     566       
     567        /* Start with the first one (the direct route) */
     568        for (index = 0; index < ip_routes_count(&netif->routes); index++) {
     569                route = ip_routes_get_index(&netif->routes, index);
     570                if ((route) &&
     571                    ((route->address.s_addr & route->netmask.s_addr) ==
     572                    (destination.s_addr & route->netmask.s_addr)))
     573                        return route;
     574        }
     575
     576        return NULL;
     577}
     578
     579/** Searches all network interfaces if there is a suitable route.
     580 *
     581 * @param[in] destination The destination address.
     582 * @return              The found route.
     583 * @return              NULL if no route was found.
     584 */
     585static ip_route_t *ip_find_route(in_addr_t destination) {
     586        int index;
     587        ip_route_t *route;
    493588        ip_netif_t *netif;
    494589
    495         fibril_rwlock_write_lock(&ip_globals.netifs_lock);
    496         netif = ip_netifs_find(&ip_globals.netifs, device_id);
    497         if (!netif) {
    498                 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    499                 return ENOENT;
    500         }
    501         netif->packet_dimension.content = mtu;
    502         fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    503 
    504         printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu);
    505 
    506         return EOK;
    507 }
    508 
    509 /** Updates the device state.
    510  *
    511  * @param[in] device_id The device identifier.
    512  * @param[in] state     The new state value.
    513  * @return              EOK on success.
    514  * @return              ENOENT if device is not found.
    515  */
    516 static int ip_device_state_message(device_id_t device_id, device_state_t state)
    517 {
    518         ip_netif_t *netif;
    519 
    520         fibril_rwlock_write_lock(&ip_globals.netifs_lock);
    521         // find the device
    522         netif = ip_netifs_find(&ip_globals.netifs, device_id);
    523         if (!netif) {
    524                 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    525                 return ENOENT;
    526         }
    527         netif->state = state;
    528         fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    529 
    530         printf("%s: Device %d changed state to %d\n", NAME, device_id, state);
    531 
    532         return EOK;
    533 }
    534 
    535 
    536 /** Prefixes a middle fragment header based on the last fragment header to the
    537  * packet.
    538  *
    539  * @param[in] packet    The packet to be prefixed.
    540  * @param[in] last      The last header to be copied.
    541  * @return              The prefixed middle header.
    542  * @return              NULL on error.
    543  */
    544 static ip_header_t *
    545 ip_create_middle_header(packet_t *packet, ip_header_t *last)
    546 {
    547         ip_header_t *middle;
    548 
    549         middle = (ip_header_t *) packet_suffix(packet, IP_HEADER_LENGTH(last));
    550         if (!middle)
    551                 return NULL;
    552         memcpy(middle, last, IP_HEADER_LENGTH(last));
    553         middle->flags |= IPFLAG_MORE_FRAGMENTS;
    554         return middle;
     590        // start with the last netif - the newest one
     591        index = ip_netifs_count(&ip_globals.netifs) - 1;
     592        while (index >= 0) {
     593                netif = ip_netifs_get_index(&ip_globals.netifs, index);
     594                if (netif && (netif->state == NETIF_ACTIVE)) {
     595                        route = ip_netif_find_route(netif, destination);
     596                        if (route)
     597                                return route;
     598                }
     599                index--;
     600        }
     601
     602        return &ip_globals.gateway;
     603}
     604
     605/** Returns the network interface's IP address.
     606 *
     607 * @param[in] netif     The network interface.
     608 * @return              The IP address.
     609 * @return              NULL if no IP address was found.
     610 */
     611static in_addr_t *ip_netif_address(ip_netif_t *netif)
     612{
     613        ip_route_t *route;
     614
     615        route = ip_routes_get_index(&netif->routes, 0);
     616        return route ? &route->address : NULL;
    555617}
    556618
     
    621683 *                      function.
    622684 */
    623 static int
    624 ip_prepare_packet(in_addr_t *source, in_addr_t dest, packet_t *packet,
    625     measured_string_t *destination)
     685static int ip_prepare_packet(in_addr_t *source, in_addr_t dest,
     686    packet_t *packet, measured_string_t *destination)
    626687{
    627688        size_t length;
     
    639700        if (destination) {
    640701                rc = packet_set_addr(packet, NULL, (uint8_t *) destination->value,
    641                     CONVERT_SIZE(char, uint8_t, destination->length));
     702                    destination->length);
    642703        } else {
    643704                rc = packet_set_addr(packet, NULL, NULL, 0);
     
    687748                                rc = packet_set_addr(next, NULL,
    688749                                    (uint8_t *) destination->value,
    689                                     CONVERT_SIZE(char, uint8_t,
    690                                     destination->length));
     750                                    destination->length);
    691751                                if (rc != EOK) {
    692752                                        free(last_header);
     
    718778                        rc = packet_set_addr(next, NULL,
    719779                            (uint8_t *) destination->value,
    720                             CONVERT_SIZE(char, uint8_t, destination->length));
     780                            destination->length);
    721781                        if (rc != EOK) {
    722782                                free(last_header);
     
    753813 *                      function.
    754814 */
    755 static int
    756 ip_fragment_packet_data(packet_t *packet, packet_t *new_packet,
     815static int ip_fragment_packet_data(packet_t *packet, packet_t *new_packet,
    757816    ip_header_t *header, ip_header_t *new_header, size_t length,
    758817    const struct sockaddr *src, const struct sockaddr *dest, socklen_t addrlen)
     
    788847
    789848        return pq_insert_after(packet, new_packet);
     849}
     850
     851/** Prefixes a middle fragment header based on the last fragment header to the
     852 * packet.
     853 *
     854 * @param[in] packet    The packet to be prefixed.
     855 * @param[in] last      The last header to be copied.
     856 * @return              The prefixed middle header.
     857 * @return              NULL on error.
     858 */
     859static ip_header_t *ip_create_middle_header(packet_t *packet,
     860    ip_header_t *last)
     861{
     862        ip_header_t *middle;
     863
     864        middle = (ip_header_t *) packet_suffix(packet, IP_HEADER_LENGTH(last));
     865        if (!middle)
     866                return NULL;
     867        memcpy(middle, last, IP_HEADER_LENGTH(last));
     868        middle->flags |= IPFLAG_MORE_FRAGMENTS;
     869        return middle;
    790870}
    791871
     
    9921072 *                      function.
    9931073 */
    994 static int
    995 ip_send_route(packet_t *packet, ip_netif_t *netif, ip_route_t *route,
    996     in_addr_t *src, in_addr_t dest, services_t error)
     1074static int ip_send_route(packet_t *packet, ip_netif_t *netif,
     1075    ip_route_t *route, in_addr_t *src, in_addr_t dest, services_t error)
    9971076{
    9981077        measured_string_t destination;
    9991078        measured_string_t *translation;
    1000         char *data;
     1079        uint8_t *data;
    10011080        int phone;
    10021081        int rc;
     
    10051084        if (netif->arp && (route->address.s_addr != dest.s_addr)) {
    10061085                destination.value = route->gateway.s_addr ?
    1007                     (char *) &route->gateway.s_addr : (char *) &dest.s_addr;
    1008                 destination.length = CONVERT_SIZE(dest.s_addr, char, 1);
     1086                    (uint8_t *) &route->gateway.s_addr : (uint8_t *) &dest.s_addr;
     1087                destination.length = sizeof(dest.s_addr);
    10091088
    10101089                rc = arp_translate_req(netif->arp->phone, netif->device_id,
     
    10571136}
    10581137
    1059 /** Searches the network interfaces if there is a suitable route.
    1060  *
    1061  * @param[in] netif     The network interface to be searched for routes. May be
    1062  *                      NULL.
    1063  * @param[in] destination The destination address.
    1064  * @return              The found route.
    1065  * @return              NULL if no route was found.
    1066  */
    1067 static ip_route_t *
    1068 ip_netif_find_route(ip_netif_t *netif, in_addr_t destination)
    1069 {
    1070         int index;
    1071         ip_route_t *route;
    1072 
    1073         if (!netif)
    1074                 return NULL;
    1075 
    1076         // start with the first one - the direct route
    1077         for (index = 0; index < ip_routes_count(&netif->routes); index++) {
    1078                 route = ip_routes_get_index(&netif->routes, index);
    1079                 if (route &&
    1080                     ((route->address.s_addr & route->netmask.s_addr) ==
    1081                     (destination.s_addr & route->netmask.s_addr))) {
    1082                         return route;
    1083                 }
    1084         }
    1085 
    1086         return NULL;
    1087 }
    1088 
    1089 /** Searches all network interfaces if there is a suitable route.
    1090  *
    1091  * @param[in] destination The destination address.
    1092  * @return              The found route.
    1093  * @return              NULL if no route was found.
    1094  */
    1095 static ip_route_t *ip_find_route(in_addr_t destination) {
    1096         int index;
    1097         ip_route_t *route;
    1098         ip_netif_t *netif;
    1099 
    1100         // start with the last netif - the newest one
    1101         index = ip_netifs_count(&ip_globals.netifs) - 1;
    1102         while (index >= 0) {
    1103                 netif = ip_netifs_get_index(&ip_globals.netifs, index);
    1104                 if (netif && (netif->state == NETIF_ACTIVE)) {
    1105                         route = ip_netif_find_route(netif, destination);
    1106                         if (route)
    1107                                 return route;
    1108                 }
    1109                 index--;
    1110         }
    1111 
    1112         return &ip_globals.gateway;
    1113 }
    1114 
    1115 /** Returns the network interface's IP address.
    1116  *
    1117  * @param[in] netif     The network interface.
    1118  * @return              The IP address.
    1119  * @return              NULL if no IP address was found.
    1120  */
    1121 static in_addr_t *ip_netif_address(ip_netif_t *netif)
    1122 {
    1123         ip_route_t *route;
    1124 
    1125         route = ip_routes_get_index(&netif->routes, 0);
    1126         return route ? &route->address : NULL;
    1127 }
    1128 
    1129 /** Registers the transport layer protocol.
    1130  *
    1131  * The traffic of this protocol will be supplied using either the receive
    1132  * function or IPC message.
    1133  *
    1134  * @param[in] protocol  The transport layer module protocol.
    1135  * @param[in] service   The transport layer module service.
    1136  * @param[in] phone     The transport layer module phone.
    1137  * @param[in] received_msg The receiving function.
    1138  * @return              EOK on success.
    1139  * @return              EINVAL if the protocol parameter and/or the service
    1140  *                      parameter is zero.
    1141  * @return              EINVAL if the phone parameter is not a positive number
    1142  *                      and the tl_receive_msg is NULL.
    1143  * @return              ENOMEM if there is not enough memory left.
    1144  */
    1145 static int
    1146 ip_register(int protocol, services_t service, int phone,
    1147     tl_received_msg_t received_msg)
    1148 {
    1149         ip_proto_t *proto;
    1150         int index;
    1151 
    1152         if (!protocol || !service || ((phone < 0) && !received_msg))
    1153                 return EINVAL;
    1154 
    1155         proto = (ip_proto_t *) malloc(sizeof(ip_protos_t));
    1156         if (!proto)
    1157                 return ENOMEM;
    1158 
    1159         proto->protocol = protocol;
    1160         proto->service = service;
    1161         proto->phone = phone;
    1162         proto->received_msg = received_msg;
    1163 
    1164         fibril_rwlock_write_lock(&ip_globals.protos_lock);
    1165         index = ip_protos_add(&ip_globals.protos, proto->protocol, proto);
    1166         if (index < 0) {
    1167                 fibril_rwlock_write_unlock(&ip_globals.protos_lock);
    1168                 free(proto);
    1169                 return index;
    1170         }
    1171         fibril_rwlock_write_unlock(&ip_globals.protos_lock);
    1172 
    1173         printf("%s: Protocol registered (protocol: %d, phone: %d)\n",
    1174             NAME, proto->protocol, proto->phone);
    1175 
    1176         return EOK;
    1177 }
    1178 
    1179 static int
    1180 ip_device_req_local(int il_phone, device_id_t device_id, services_t netif)
    1181 {
    1182         ip_netif_t *ip_netif;
    1183         ip_route_t *route;
    1184         int index;
    1185         int rc;
    1186 
    1187         ip_netif = (ip_netif_t *) malloc(sizeof(ip_netif_t));
    1188         if (!ip_netif)
    1189                 return ENOMEM;
    1190 
    1191         rc = ip_routes_initialize(&ip_netif->routes);
    1192         if (rc != EOK) {
    1193                 free(ip_netif);
    1194                 return rc;
    1195         }
    1196 
    1197         ip_netif->device_id = device_id;
    1198         ip_netif->service = netif;
    1199         ip_netif->state = NETIF_STOPPED;
    1200 
    1201         fibril_rwlock_write_lock(&ip_globals.netifs_lock);
    1202 
    1203         rc = ip_netif_initialize(ip_netif);
    1204         if (rc != EOK) {
    1205                 fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    1206                 ip_routes_destroy(&ip_netif->routes);
    1207                 free(ip_netif);
    1208                 return rc;
    1209         }
    1210         if (ip_netif->arp)
    1211                 ip_netif->arp->usage++;
    1212 
    1213         // print the settings
    1214         printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n",
    1215             NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv,
    1216             ip_netif->dhcp ? "dhcp" : "static");
    1217        
    1218         // TODO ipv6 addresses
    1219        
    1220         char address[INET_ADDRSTRLEN];
    1221         char netmask[INET_ADDRSTRLEN];
    1222         char gateway[INET_ADDRSTRLEN];
    1223        
    1224         for (index = 0; index < ip_routes_count(&ip_netif->routes); index++) {
    1225                 route = ip_routes_get_index(&ip_netif->routes, index);
    1226                 if (route) {
    1227                         inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr,
    1228                             address, INET_ADDRSTRLEN);
    1229                         inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr,
    1230                             netmask, INET_ADDRSTRLEN);
    1231                         inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr,
    1232                             gateway, INET_ADDRSTRLEN);
    1233                         printf("%s: Route %d (address: %s, netmask: %s, "
    1234                             "gateway: %s)\n", NAME, index, address, netmask,
    1235                             gateway);
    1236                 }
    1237         }
    1238        
    1239         inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address,
    1240             INET_ADDRSTRLEN);
    1241         fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    1242 
    1243         printf("%s: Broadcast (%s)\n", NAME, address);
    1244 
    1245         return EOK;
    1246 }
    1247 
    1248 static int
    1249 ip_send_msg_local(int il_phone, device_id_t device_id, packet_t *packet,
    1250     services_t sender, services_t error)
     1138static int ip_send_msg_local(int il_phone, device_id_t device_id,
     1139    packet_t *packet, services_t sender, services_t error)
    12511140{
    12521141        int addrlen;
     
    12891178        if (device_id > 0) {
    12901179                netif = ip_netifs_find(&ip_globals.netifs, device_id);
    1291                 route = ip_netif_find_route(netif, * dest);
     1180                route = ip_netif_find_route(netif, *dest);
    12921181                if (netif && !route && (ip_globals.gateway.netif == netif))
    12931182                        route = &ip_globals.gateway;
     
    13191208                }
    13201209        }
    1321 
     1210       
    13221211        // if the local host is the destination
    13231212        if ((route->address.s_addr == dest->s_addr) &&
     
    13521241}
    13531242
     1243/** Updates the device state.
     1244 *
     1245 * @param[in] device_id The device identifier.
     1246 * @param[in] state     The new state value.
     1247 * @return              EOK on success.
     1248 * @return              ENOENT if device is not found.
     1249 */
     1250static int ip_device_state_message(device_id_t device_id, device_state_t state)
     1251{
     1252        ip_netif_t *netif;
     1253
     1254        fibril_rwlock_write_lock(&ip_globals.netifs_lock);
     1255        // find the device
     1256        netif = ip_netifs_find(&ip_globals.netifs, device_id);
     1257        if (!netif) {
     1258                fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
     1259                return ENOENT;
     1260        }
     1261        netif->state = state;
     1262        fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
     1263
     1264        printf("%s: Device %d changed state to %d\n", NAME, device_id, state);
     1265
     1266        return EOK;
     1267}
     1268
     1269/** Returns the packet destination address from the IP header.
     1270 *
     1271 * @param[in] header    The packet IP header to be read.
     1272 * @return              The packet destination address.
     1273 */
     1274static in_addr_t ip_get_destination(ip_header_t *header)
     1275{
     1276        in_addr_t destination;
     1277
     1278        // TODO search set ipopt route?
     1279        destination.s_addr = header->destination_address;
     1280        return destination;
     1281}
     1282
     1283/** Delivers the packet to the local host.
     1284 *
     1285 * The packet is either passed to another module or released on error.
     1286 * The ICMP_PROT_UNREACH error notification may be sent if the protocol is not
     1287 * found.
     1288 *
     1289 * @param[in] device_id The source device identifier.
     1290 * @param[in] packet    The packet to be delivered.
     1291 * @param[in] header    The first packet IP header. May be NULL.
     1292 * @param[in] error     The packet error service.
     1293 * @return              EOK on success.
     1294 * @return              ENOTSUP if the packet is a fragment.
     1295 * @return              EAFNOSUPPORT if the address family is not supported.
     1296 * @return              ENOENT if the target protocol is not found.
     1297 * @return              Other error codes as defined for the packet_set_addr()
     1298 *                      function.
     1299 * @return              Other error codes as defined for the packet_trim()
     1300 *                      function.
     1301 * @return              Other error codes as defined for the protocol specific
     1302 *                      tl_received_msg() function.
     1303 */
     1304static int ip_deliver_local(device_id_t device_id, packet_t *packet,
     1305    ip_header_t *header, services_t error)
     1306{
     1307        ip_proto_t *proto;
     1308        int phone;
     1309        services_t service;
     1310        tl_received_msg_t received_msg;
     1311        struct sockaddr *src;
     1312        struct sockaddr *dest;
     1313        struct sockaddr_in src_in;
     1314        struct sockaddr_in dest_in;
     1315        socklen_t addrlen;
     1316        int rc;
     1317
     1318        if ((header->flags & IPFLAG_MORE_FRAGMENTS) ||
     1319            IP_FRAGMENT_OFFSET(header)) {
     1320                // TODO fragmented
     1321                return ENOTSUP;
     1322        }
     1323       
     1324        switch (header->version) {
     1325        case IPVERSION:
     1326                addrlen = sizeof(src_in);
     1327                bzero(&src_in, addrlen);
     1328                src_in.sin_family = AF_INET;
     1329                memcpy(&dest_in, &src_in, addrlen);
     1330                memcpy(&src_in.sin_addr.s_addr, &header->source_address,
     1331                    sizeof(header->source_address));
     1332                memcpy(&dest_in.sin_addr.s_addr, &header->destination_address,
     1333                    sizeof(header->destination_address));
     1334                src = (struct sockaddr *) &src_in;
     1335                dest = (struct sockaddr *) &dest_in;
     1336                break;
     1337
     1338        default:
     1339                return ip_release_and_return(packet, EAFNOSUPPORT);
     1340        }
     1341
     1342        rc = packet_set_addr(packet, (uint8_t *) src, (uint8_t *) dest,
     1343            addrlen);
     1344        if (rc != EOK)
     1345                return ip_release_and_return(packet, rc);
     1346
     1347        // trim padding if present
     1348        if (!error &&
     1349            (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) {
     1350                rc = packet_trim(packet, 0,
     1351                    packet_get_data_length(packet) - IP_TOTAL_LENGTH(header));
     1352                if (rc != EOK)
     1353                        return ip_release_and_return(packet, rc);
     1354        }
     1355
     1356        fibril_rwlock_read_lock(&ip_globals.protos_lock);
     1357
     1358        proto = ip_protos_find(&ip_globals.protos, header->protocol);
     1359        if (!proto) {
     1360                fibril_rwlock_read_unlock(&ip_globals.protos_lock);
     1361                phone = ip_prepare_icmp_and_get_phone(error, packet, header);
     1362                if (phone >= 0) {
     1363                        // unreachable ICMP
     1364                        icmp_destination_unreachable_msg(phone,
     1365                            ICMP_PROT_UNREACH, 0, packet);
     1366                }
     1367                return ENOENT;
     1368        }
     1369
     1370        if (proto->received_msg) {
     1371                service = proto->service;
     1372                received_msg = proto->received_msg;
     1373                fibril_rwlock_read_unlock(&ip_globals.protos_lock);
     1374                rc = received_msg(device_id, packet, service, error);
     1375        } else {
     1376                rc = tl_received_msg(proto->phone, device_id, packet,
     1377                    proto->service, error);
     1378                fibril_rwlock_read_unlock(&ip_globals.protos_lock);
     1379        }
     1380
     1381        return rc;
     1382}
     1383
     1384/** Processes the received packet.
     1385 *
     1386 * The packet is either passed to another module or released on error.
     1387 *
     1388 * The ICMP_PARAM_POINTER error notification may be sent if the checksum is
     1389 * invalid.
     1390 * The ICMP_EXC_TTL error notification may be sent if the TTL is less than two.
     1391 * The ICMP_HOST_UNREACH error notification may be sent if no route was found.
     1392 * The ICMP_HOST_UNREACH error notification may be sent if the packet is for
     1393 * another host and the routing is disabled.
     1394 *
     1395 * @param[in] device_id The source device identifier.
     1396 * @param[in] packet    The received packet to be processed.
     1397 * @return              EOK on success.
     1398 * @return              EINVAL if the TTL is less than two.
     1399 * @return              EINVAL if the checksum is invalid.
     1400 * @return              EAFNOSUPPORT if the address family is not supported.
     1401 * @return              ENOENT if no route was found.
     1402 * @return              ENOENT if the packet is for another host and the routing
     1403 *                      is disabled.
     1404 */
     1405static int ip_process_packet(device_id_t device_id, packet_t *packet)
     1406{
     1407        ip_header_t *header;
     1408        in_addr_t dest;
     1409        ip_route_t *route;
     1410        int phone;
     1411        struct sockaddr *addr;
     1412        struct sockaddr_in addr_in;
     1413        socklen_t addrlen;
     1414        int rc;
     1415       
     1416        header = (ip_header_t *) packet_get_data(packet);
     1417        if (!header)
     1418                return ip_release_and_return(packet, ENOMEM);
     1419
     1420        // checksum
     1421        if ((header->header_checksum) &&
     1422            (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) {
     1423                phone = ip_prepare_icmp_and_get_phone(0, packet, header);
     1424                if (phone >= 0) {
     1425                        // checksum error ICMP
     1426                        icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER,
     1427                            ((size_t) ((void *) &header->header_checksum)) -
     1428                            ((size_t) ((void *) header)), packet);
     1429                }
     1430                return EINVAL;
     1431        }
     1432
     1433        if (header->ttl <= 1) {
     1434                phone = ip_prepare_icmp_and_get_phone(0, packet, header);
     1435                if (phone >= 0) {
     1436                        // ttl exceeded ICMP
     1437                        icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet);
     1438                }
     1439                return EINVAL;
     1440        }
     1441       
     1442        // process ipopt and get destination
     1443        dest = ip_get_destination(header);
     1444
     1445        // set the addrination address
     1446        switch (header->version) {
     1447        case IPVERSION:
     1448                addrlen = sizeof(addr_in);
     1449                bzero(&addr_in, addrlen);
     1450                addr_in.sin_family = AF_INET;
     1451                memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest));
     1452                addr = (struct sockaddr *) &addr_in;
     1453                break;
     1454
     1455        default:
     1456                return ip_release_and_return(packet, EAFNOSUPPORT);
     1457        }
     1458
     1459        rc = packet_set_addr(packet, NULL, (uint8_t *) &addr, addrlen);
     1460        if (rc != EOK)
     1461                return rc;
     1462       
     1463        route = ip_find_route(dest);
     1464        if (!route) {
     1465                phone = ip_prepare_icmp_and_get_phone(0, packet, header);
     1466                if (phone >= 0) {
     1467                        // unreachable ICMP
     1468                        icmp_destination_unreachable_msg(phone,
     1469                            ICMP_HOST_UNREACH, 0, packet);
     1470                }
     1471                return ENOENT;
     1472        }
     1473
     1474        if (route->address.s_addr == dest.s_addr) {
     1475                // local delivery
     1476                return ip_deliver_local(device_id, packet, header, 0);
     1477        }
     1478
     1479        if (route->netif->routing) {
     1480                header->ttl--;
     1481                return ip_send_route(packet, route->netif, route, NULL, dest,
     1482                    0);
     1483        }
     1484
     1485        phone = ip_prepare_icmp_and_get_phone(0, packet, header);
     1486        if (phone >= 0) {
     1487                // unreachable ICMP if no routing
     1488                icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0,
     1489                    packet);
     1490        }
     1491       
     1492        return ENOENT;
     1493}
     1494
    13541495/** Returns the device packet dimensions for sending.
    13551496 *
     
    13631504 * @return              EOK on success.
    13641505 */
    1365 static int
    1366 ip_packet_size_message(device_id_t device_id, size_t *addr_len, size_t *prefix,
    1367     size_t *content, size_t *suffix)
     1506static int ip_packet_size_message(device_id_t device_id, size_t *addr_len,
     1507    size_t *prefix, size_t *content, size_t *suffix)
    13681508{
    13691509        ip_netif_t *netif;
     
    14151555}
    14161556
    1417 /** Returns the packet destination address from the IP header.
    1418  *
    1419  * @param[in] header    The packet IP header to be read.
    1420  * @return              The packet destination address.
    1421  */
    1422 static in_addr_t ip_get_destination(ip_header_t *header)
    1423 {
    1424         in_addr_t destination;
    1425 
    1426         // TODO search set ipopt route?
    1427         destination.s_addr = header->destination_address;
    1428         return destination;
    1429 }
    1430 
    1431 /** Delivers the packet to the local host.
    1432  *
    1433  * The packet is either passed to another module or released on error.
    1434  * The ICMP_PROT_UNREACH error notification may be sent if the protocol is not
    1435  * found.
    1436  *
    1437  * @param[in] device_id The source device identifier.
    1438  * @param[in] packet    The packet to be delivered.
    1439  * @param[in] header    The first packet IP header. May be NULL.
    1440  * @param[in] error     The packet error service.
     1557/** Updates the device content length according to the new MTU value.
     1558 *
     1559 * @param[in] device_id The device identifier.
     1560 * @param[in] mtu       The new mtu value.
    14411561 * @return              EOK on success.
    1442  * @return              ENOTSUP if the packet is a fragment.
    1443  * @return              EAFNOSUPPORT if the address family is not supported.
    1444  * @return              ENOENT if the target protocol is not found.
    1445  * @return              Other error codes as defined for the packet_set_addr()
    1446  *                      function.
    1447  * @return              Other error codes as defined for the packet_trim()
    1448  *                      function.
    1449  * @return              Other error codes as defined for the protocol specific
    1450  *                      tl_received_msg() function.
     1562 * @return              ENOENT if device is not found.
     1563 */
     1564static int ip_mtu_changed_message(device_id_t device_id, size_t mtu)
     1565{
     1566        ip_netif_t *netif;
     1567
     1568        fibril_rwlock_write_lock(&ip_globals.netifs_lock);
     1569        netif = ip_netifs_find(&ip_globals.netifs, device_id);
     1570        if (!netif) {
     1571                fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
     1572                return ENOENT;
     1573        }
     1574        netif->packet_dimension.content = mtu;
     1575        fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
     1576
     1577        printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu);
     1578
     1579        return EOK;
     1580}
     1581
     1582/** Process IPC messages from the registered device driver modules
     1583 *
     1584 * @param[in]     iid   Message identifier.
     1585 * @param[in,out] icall Message parameters.
     1586 *
     1587 */
     1588static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall)
     1589{
     1590        packet_t *packet;
     1591        int rc;
     1592       
     1593        while (true) {
     1594                switch (IPC_GET_IMETHOD(*icall)) {
     1595                case NET_IL_DEVICE_STATE:
     1596                        rc = ip_device_state_message(IPC_GET_DEVICE(*icall),
     1597                            IPC_GET_STATE(*icall));
     1598                        ipc_answer_0(iid, (sysarg_t) rc);
     1599                        break;
     1600               
     1601                case NET_IL_RECEIVED:
     1602                        rc = packet_translate_remote(ip_globals.net_phone, &packet,
     1603                            IPC_GET_PACKET(*icall));
     1604                        if (rc == EOK) {
     1605                                do {
     1606                                        packet_t *next = pq_detach(packet);
     1607                                        ip_process_packet(IPC_GET_DEVICE(*icall), packet);
     1608                                        packet = next;
     1609                                } while (packet);
     1610                        }
     1611                       
     1612                        ipc_answer_0(iid, (sysarg_t) rc);
     1613                        break;
     1614               
     1615                case NET_IL_MTU_CHANGED:
     1616                        rc = ip_mtu_changed_message(IPC_GET_DEVICE(*icall),
     1617                            IPC_GET_MTU(*icall));
     1618                        ipc_answer_0(iid, (sysarg_t) rc);
     1619                        break;
     1620               
     1621                default:
     1622                        ipc_answer_0(iid, (sysarg_t) ENOTSUP);
     1623                }
     1624               
     1625                iid = async_get_call(icall);
     1626        }
     1627}
     1628
     1629/** Registers the transport layer protocol.
     1630 *
     1631 * The traffic of this protocol will be supplied using either the receive
     1632 * function or IPC message.
     1633 *
     1634 * @param[in] protocol  The transport layer module protocol.
     1635 * @param[in] service   The transport layer module service.
     1636 * @param[in] phone     The transport layer module phone.
     1637 * @param[in] received_msg The receiving function.
     1638 * @return              EOK on success.
     1639 * @return              EINVAL if the protocol parameter and/or the service
     1640 *                      parameter is zero.
     1641 * @return              EINVAL if the phone parameter is not a positive number
     1642 *                      and the tl_receive_msg is NULL.
     1643 * @return              ENOMEM if there is not enough memory left.
    14511644 */
    14521645static int
    1453 ip_deliver_local(device_id_t device_id, packet_t *packet, ip_header_t *header,
    1454     services_t error)
     1646ip_register(int protocol, services_t service, int phone,
     1647    tl_received_msg_t received_msg)
    14551648{
    14561649        ip_proto_t *proto;
    1457         int phone;
    1458         services_t service;
    1459         tl_received_msg_t received_msg;
    1460         struct sockaddr *src;
    1461         struct sockaddr *dest;
    1462         struct sockaddr_in src_in;
    1463         struct sockaddr_in dest_in;
    1464         socklen_t addrlen;
    1465         int rc;
    1466 
    1467         if ((header->flags & IPFLAG_MORE_FRAGMENTS) ||
    1468             IP_FRAGMENT_OFFSET(header)) {
    1469                 // TODO fragmented
    1470                 return ENOTSUP;
    1471         }
    1472        
    1473         switch (header->version) {
    1474         case IPVERSION:
    1475                 addrlen = sizeof(src_in);
    1476                 bzero(&src_in, addrlen);
    1477                 src_in.sin_family = AF_INET;
    1478                 memcpy(&dest_in, &src_in, addrlen);
    1479                 memcpy(&src_in.sin_addr.s_addr, &header->source_address,
    1480                     sizeof(header->source_address));
    1481                 memcpy(&dest_in.sin_addr.s_addr, &header->destination_address,
    1482                     sizeof(header->destination_address));
    1483                 src = (struct sockaddr *) &src_in;
    1484                 dest = (struct sockaddr *) &dest_in;
    1485                 break;
    1486 
    1487         default:
    1488                 return ip_release_and_return(packet, EAFNOSUPPORT);
    1489         }
    1490 
    1491         rc = packet_set_addr(packet, (uint8_t *) src, (uint8_t *) dest,
    1492             addrlen);
    1493         if (rc != EOK)
    1494                 return ip_release_and_return(packet, rc);
    1495 
    1496         // trim padding if present
    1497         if (!error &&
    1498             (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) {
    1499                 rc = packet_trim(packet, 0,
    1500                     packet_get_data_length(packet) - IP_TOTAL_LENGTH(header));
    1501                 if (rc != EOK)
    1502                         return ip_release_and_return(packet, rc);
    1503         }
    1504 
    1505         fibril_rwlock_read_lock(&ip_globals.protos_lock);
    1506 
    1507         proto = ip_protos_find(&ip_globals.protos, header->protocol);
    1508         if (!proto) {
    1509                 fibril_rwlock_read_unlock(&ip_globals.protos_lock);
    1510                 phone = ip_prepare_icmp_and_get_phone(error, packet, header);
    1511                 if (phone >= 0) {
    1512                         // unreachable ICMP
    1513                         icmp_destination_unreachable_msg(phone,
    1514                             ICMP_PROT_UNREACH, 0, packet);
    1515                 }
    1516                 return ENOENT;
    1517         }
    1518 
    1519         if (proto->received_msg) {
    1520                 service = proto->service;
    1521                 received_msg = proto->received_msg;
    1522                 fibril_rwlock_read_unlock(&ip_globals.protos_lock);
    1523                 rc = received_msg(device_id, packet, service, error);
    1524         } else {
    1525                 rc = tl_received_msg(proto->phone, device_id, packet,
    1526                     proto->service, error);
    1527                 fibril_rwlock_read_unlock(&ip_globals.protos_lock);
    1528         }
    1529 
    1530         return rc;
    1531 }
    1532 
    1533 /** Processes the received packet.
    1534  *
    1535  * The packet is either passed to another module or released on error.
    1536  *
    1537  * The ICMP_PARAM_POINTER error notification may be sent if the checksum is
    1538  * invalid.
    1539  * The ICMP_EXC_TTL error notification may be sent if the TTL is less than two.
    1540  * The ICMP_HOST_UNREACH error notification may be sent if no route was found.
    1541  * The ICMP_HOST_UNREACH error notification may be sent if the packet is for
    1542  * another host and the routing is disabled.
    1543  *
    1544  * @param[in] device_id The source device identifier.
    1545  * @param[in] packet    The received packet to be processed.
    1546  * @return              EOK on success.
    1547  * @return              EINVAL if the TTL is less than two.
    1548  * @return              EINVAL if the checksum is invalid.
    1549  * @return              EAFNOSUPPORT if the address family is not supported.
    1550  * @return              ENOENT if no route was found.
    1551  * @return              ENOENT if the packet is for another host and the routing
    1552  *                      is disabled.
    1553  */
    1554 static int
    1555 ip_process_packet(device_id_t device_id, packet_t *packet)
    1556 {
    1557         ip_header_t *header;
    1558         in_addr_t dest;
    1559         ip_route_t *route;
    1560         int phone;
    1561         struct sockaddr *addr;
    1562         struct sockaddr_in addr_in;
    1563         socklen_t addrlen;
    1564         int rc;
    1565 
    1566         header = (ip_header_t *) packet_get_data(packet);
    1567         if (!header)
    1568                 return ip_release_and_return(packet, ENOMEM);
    1569 
    1570         // checksum
    1571         if ((header->header_checksum) &&
    1572             (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) {
    1573                 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    1574                 if (phone >= 0) {
    1575                         // checksum error ICMP
    1576                         icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER,
    1577                             ((size_t) ((void *) &header->header_checksum)) -
    1578                             ((size_t) ((void *) header)), packet);
    1579                 }
     1650        int index;
     1651
     1652        if (!protocol || !service || ((phone < 0) && !received_msg))
    15801653                return EINVAL;
    1581         }
    1582 
    1583         if (header->ttl <= 1) {
    1584                 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    1585                 if (phone >= 0) {
    1586                         // ttl exceeded ICMP
    1587                         icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet);
    1588                 }
    1589                 return EINVAL;
    1590         }
    1591 
    1592         // process ipopt and get destination
    1593         dest = ip_get_destination(header);
    1594 
    1595         // set the addrination address
    1596         switch (header->version) {
    1597         case IPVERSION:
    1598                 addrlen = sizeof(addr_in);
    1599                 bzero(&addr_in, addrlen);
    1600                 addr_in.sin_family = AF_INET;
    1601                 memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest));
    1602                 addr = (struct sockaddr *) &addr_in;
    1603                 break;
    1604 
    1605         default:
    1606                 return ip_release_and_return(packet, EAFNOSUPPORT);
    1607         }
    1608 
    1609         rc = packet_set_addr(packet, NULL, (uint8_t *) &addr, addrlen);
    1610         if (rc != EOK)
    1611                 return rc;
    1612 
    1613         route = ip_find_route(dest);
    1614         if (!route) {
    1615                 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    1616                 if (phone >= 0) {
    1617                         // unreachable ICMP
    1618                         icmp_destination_unreachable_msg(phone,
    1619                             ICMP_HOST_UNREACH, 0, packet);
    1620                 }
    1621                 return ENOENT;
    1622         }
    1623 
    1624         if (route->address.s_addr == dest.s_addr) {
    1625                 // local delivery
    1626                 return ip_deliver_local(device_id, packet, header, 0);
    1627         }
    1628 
    1629         if (route->netif->routing) {
    1630                 header->ttl--;
    1631                 return ip_send_route(packet, route->netif, route, NULL, dest,
    1632                     0);
    1633         }
    1634 
    1635         phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    1636         if (phone >= 0) {
    1637                 // unreachable ICMP if no routing
    1638                 icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0,
    1639                     packet);
    1640         }
    1641        
    1642         return ENOENT;
    1643 }
     1654
     1655        proto = (ip_proto_t *) malloc(sizeof(ip_protos_t));
     1656        if (!proto)
     1657                return ENOMEM;
     1658
     1659        proto->protocol = protocol;
     1660        proto->service = service;
     1661        proto->phone = phone;
     1662        proto->received_msg = received_msg;
     1663
     1664        fibril_rwlock_write_lock(&ip_globals.protos_lock);
     1665        index = ip_protos_add(&ip_globals.protos, proto->protocol, proto);
     1666        if (index < 0) {
     1667                fibril_rwlock_write_unlock(&ip_globals.protos_lock);
     1668                free(proto);
     1669                return index;
     1670        }
     1671        fibril_rwlock_write_unlock(&ip_globals.protos_lock);
     1672
     1673        printf("%s: Protocol registered (protocol: %d, phone: %d)\n",
     1674            NAME, proto->protocol, proto->phone);
     1675
     1676        return EOK;
     1677}
     1678
    16441679
    16451680static int
     
    17571792                    (header->destination_address & route->netmask.s_addr))) {
    17581793                        // clear the ARP mapping if any
    1759                         address.value = (char *) &header->destination_address;
    1760                         address.length = CONVERT_SIZE(uint8_t, char,
    1761                             sizeof(header->destination_address));
     1794                        address.value = (uint8_t *) &header->destination_address;
     1795                        address.length = sizeof(header->destination_address);
    17621796                        arp_clear_address_req(netif->arp->phone,
    17631797                            netif->device_id, SERVICE_IP, &address);
     
    18431877}
    18441878
    1845 /** Processes the received IP packet or the packet queue one by one.
    1846  *
    1847  * The packet is either passed to another module or released on error.
    1848  *
    1849  * @param[in] device_id The source device identifier.
    1850  * @param[in,out] packet The received packet.
    1851  * @return              EOK on success and the packet is no longer needed.
    1852  * @return              EINVAL if the packet is too small to carry the IP
    1853  *                      packet.
    1854  * @return              EINVAL if the received address lengths differs from the
    1855  *                      registered values.
    1856  * @return              ENOENT if the device is not found in the cache.
    1857  * @return              ENOENT if the protocol for the device is not found in
    1858  *                      the cache.
    1859  * @return              ENOMEM if there is not enough memory left.
    1860  */
    1861 static int ip_receive_message(device_id_t device_id, packet_t *packet)
    1862 {
    1863         packet_t *next;
    1864 
    1865         do {
    1866                 next = pq_detach(packet);
    1867                 ip_process_packet(device_id, packet);
    1868                 packet = next;
    1869         } while (packet);
    1870 
    1871         return EOK;
    1872 }
    1873 
    18741879/** Processes the IP message.
    18751880 *
     
    18831888 *
    18841889 * @see ip_interface.h
    1885  * @see il_interface.h
     1890 * @see il_remote.h
    18861891 * @see IS_NET_IP_MESSAGE()
    18871892 */
    1888 int
    1889 ip_message_standalone(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
    1890     int *answer_count)
     1893int il_module_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
     1894    size_t *answer_count)
    18911895{
    18921896        packet_t *packet;
    18931897        struct sockaddr *addr;
     1898        void *header;
     1899        size_t headerlen;
    18941900        size_t addrlen;
    18951901        size_t prefix;
    18961902        size_t suffix;
    18971903        size_t content;
    1898         void *header;
    1899         size_t headerlen;
    19001904        device_id_t device_id;
    19011905        int rc;
    19021906       
    19031907        *answer_count = 0;
    1904         switch (IPC_GET_METHOD(*call)) {
     1908        switch (IPC_GET_IMETHOD(*call)) {
    19051909        case IPC_M_PHONE_HUNGUP:
    19061910                return EOK;
    19071911       
    19081912        case IPC_M_CONNECT_TO_ME:
    1909                 return ip_register(IL_GET_PROTO(call), IL_GET_SERVICE(call),
    1910                     IPC_GET_PHONE(call), NULL);
    1911        
    1912         case NET_IL_DEVICE:
    1913                 return ip_device_req_local(0, IPC_GET_DEVICE(call),
    1914                     IPC_GET_SERVICE(call));
    1915        
    1916         case NET_IL_SEND:
     1913                return ip_register(IL_GET_PROTO(*call), IL_GET_SERVICE(*call),
     1914                    IPC_GET_PHONE(*call), NULL);
     1915       
     1916        case NET_IP_DEVICE:
     1917                return ip_device_req_local(0, IPC_GET_DEVICE(*call),
     1918                    IPC_GET_SERVICE(*call));
     1919       
     1920        case NET_IP_RECEIVED_ERROR:
    19171921                rc = packet_translate_remote(ip_globals.net_phone, &packet,
    1918                     IPC_GET_PACKET(call));
     1922                    IPC_GET_PACKET(*call));
    19191923                if (rc != EOK)
    19201924                        return rc;
    1921                 return ip_send_msg_local(0, IPC_GET_DEVICE(call), packet, 0,
    1922                     IPC_GET_ERROR(call));
    1923        
    1924         case NET_IL_DEVICE_STATE:
    1925                 return ip_device_state_message(IPC_GET_DEVICE(call),
    1926                     IPC_GET_STATE(call));
    1927        
    1928         case NET_IL_RECEIVED:
    1929                 rc = packet_translate_remote(ip_globals.net_phone, &packet,
    1930                     IPC_GET_PACKET(call));
    1931                 if (rc != EOK)
    1932                         return rc;
    1933                 return ip_receive_message(IPC_GET_DEVICE(call), packet);
    1934        
    1935         case NET_IP_RECEIVED_ERROR:
    1936                 rc = packet_translate_remote(ip_globals.net_phone, &packet,
    1937                     IPC_GET_PACKET(call));
    1938                 if (rc != EOK)
    1939                         return rc;
    1940                 return ip_received_error_msg_local(0, IPC_GET_DEVICE(call),
    1941                     packet, IPC_GET_TARGET(call), IPC_GET_ERROR(call));
     1925                return ip_received_error_msg_local(0, IPC_GET_DEVICE(*call),
     1926                    packet, IPC_GET_TARGET(*call), IPC_GET_ERROR(*call));
    19421927       
    19431928        case NET_IP_ADD_ROUTE:
    1944                 return ip_add_route_req_local(0, IPC_GET_DEVICE(call),
    1945                     IP_GET_ADDRESS(call), IP_GET_NETMASK(call),
    1946                     IP_GET_GATEWAY(call));
     1929                return ip_add_route_req_local(0, IPC_GET_DEVICE(*call),
     1930                    IP_GET_ADDRESS(*call), IP_GET_NETMASK(*call),
     1931                    IP_GET_GATEWAY(*call));
    19471932
    19481933        case NET_IP_SET_GATEWAY:
    1949                 return ip_set_gateway_req_local(0, IPC_GET_DEVICE(call),
    1950                     IP_GET_GATEWAY(call));
     1934                return ip_set_gateway_req_local(0, IPC_GET_DEVICE(*call),
     1935                    IP_GET_GATEWAY(*call));
    19511936
    19521937        case NET_IP_GET_ROUTE:
    1953                 rc = data_receive((void **) &addr, &addrlen);
     1938                rc = async_data_write_accept((void **) &addr, false, 0, 0, 0,
     1939                    &addrlen);
    19541940                if (rc != EOK)
    19551941                        return rc;
    19561942               
    1957                 rc = ip_get_route_req_local(0, IP_GET_PROTOCOL(call), addr,
     1943                rc = ip_get_route_req_local(0, IP_GET_PROTOCOL(*call), addr,
    19581944                    (socklen_t) addrlen, &device_id, &header, &headerlen);
    19591945                if (rc != EOK)
    19601946                        return rc;
    19611947               
    1962                 IPC_SET_DEVICE(answer, device_id);
    1963                 IP_SET_HEADERLEN(answer, headerlen);
     1948                IPC_SET_DEVICE(*answer, device_id);
     1949                IP_SET_HEADERLEN(*answer, headerlen);
    19641950               
    19651951                *answer_count = 2;
     
    19721958                return rc;
    19731959       
    1974         case NET_IL_PACKET_SPACE:
    1975                 rc = ip_packet_size_message(IPC_GET_DEVICE(call), &addrlen,
     1960        case NET_IP_PACKET_SPACE:
     1961                rc = ip_packet_size_message(IPC_GET_DEVICE(*call), &addrlen,
    19761962                    &prefix, &content, &suffix);
    19771963                if (rc != EOK)
    19781964                        return rc;
    19791965               
    1980                 IPC_SET_ADDR(answer, addrlen);
    1981                 IPC_SET_PREFIX(answer, prefix);
    1982                 IPC_SET_CONTENT(answer, content);
    1983                 IPC_SET_SUFFIX(answer, suffix);
     1966                IPC_SET_ADDR(*answer, addrlen);
     1967                IPC_SET_PREFIX(*answer, prefix);
     1968                IPC_SET_CONTENT(*answer, content);
     1969                IPC_SET_SUFFIX(*answer, suffix);
    19841970                *answer_count = 4;
    19851971                return EOK;
    19861972       
    1987         case NET_IL_MTU_CHANGED:
    1988                 return ip_mtu_changed_message(IPC_GET_DEVICE(call),
    1989                     IPC_GET_MTU(call));
     1973        case NET_IP_SEND:
     1974                rc = packet_translate_remote(ip_globals.net_phone, &packet,
     1975                    IPC_GET_PACKET(*call));
     1976                if (rc != EOK)
     1977                        return rc;
     1978               
     1979                return ip_send_msg_local(0, IPC_GET_DEVICE(*call), packet, 0,
     1980                    IPC_GET_ERROR(*call));
    19901981        }
    19911982       
     
    19931984}
    19941985
    1995 /** Default thread for new connections.
    1996  *
    1997  * @param[in] iid       The initial message identifier.
    1998  * @param[in] icall     The initial message call structure.
    1999  */
    2000 static void il_client_connection(ipc_callid_t iid, ipc_call_t *icall)
    2001 {
    2002         /*
    2003          * Accept the connection
    2004          *  - Answer the first IPC_M_CONNECT_ME_TO call.
    2005          */
    2006         ipc_answer_0(iid, EOK);
    2007        
    2008         while (true) {
    2009                 ipc_call_t answer;
    2010                 int answer_count;
    2011                
    2012                 /* Clear the answer structure */
    2013                 refresh_answer(&answer, &answer_count);
    2014                
    2015                 /* Fetch the next message */
    2016                 ipc_call_t call;
    2017                 ipc_callid_t callid = async_get_call(&call);
    2018                
    2019                 /* Process the message */
    2020                 int res = il_module_message_standalone(callid, &call, &answer,
    2021                     &answer_count);
    2022                
    2023                 /*
    2024                  * End if told to either by the message or the processing
    2025                  * result.
    2026                  */
    2027                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
    2028                     (res == EHANGUP)) {
    2029                         return;
    2030                 }
    2031                
    2032                 /* Answer the message */
    2033                 answer_call(callid, res, &answer, answer_count);
    2034         }
    2035 }
    2036 
    2037 /** Starts the module.
    2038  *
    2039  * @return EOK on success.
    2040  * @return Other error codes as defined for each specific module start function.
    2041  */
    20421986int main(int argc, char *argv[])
    20431987{
    2044         int rc;
    2045        
    20461988        /* Start the module */
    2047         rc = il_module_start_standalone(il_client_connection);
    2048         return rc;
     1989        return il_module_start(SERVICE_IP);
    20491990}
    20501991
Note: See TracChangeset for help on using the changeset viewer.