Changeset 8b655705 in mainline for uspace/srv/net/il/ip/ip.c


Ignore:
Timestamp:
2011-04-15T19:38:07Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9dd730d1
Parents:
6b9e85b (diff), b2fb47f (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

    r6b9e85b r8b655705  
    4141#include <stdio.h>
    4242#include <str.h>
    43 #include <ipc/ipc.h>
    4443#include <ipc/services.h>
    4544#include <ipc/net.h>
     
    6766#include <net_checksum.h>
    6867#include <icmp_client.h>
    69 #include <icmp_interface.h>
     68#include <icmp_remote.h>
    7069#include <ip_client.h>
    7170#include <ip_interface.h>
     
    177176        socklen_t addrlen;
    178177
    179         // detach the first packet and release the others
     178        /* Detach the first packet and release the others */
    180179        next = pq_detach(packet);
    181180        if (next)
     
    186185                        return ENOMEM;
    187186
    188                 // get header
     187                /* Get header */
    189188                header = (ip_header_t *) packet_get_data(packet);
    190189                if (!header)
     
    193192        }
    194193
    195         // only for the first fragment
     194        /* Only for the first fragment */
    196195        if (IP_FRAGMENT_OFFSET(header))
    197196                return EINVAL;
    198197
    199         // not for the ICMP protocol
     198        /* Not for the ICMP protocol */
    200199        if (header->protocol == IPPROTO_ICMP)
    201200                return EPERM;
    202201
    203         // set the destination address
     202        /* Set the destination address */
    204203        switch (header->version) {
    205204        case IPVERSION:
     
    352351        configuration = &names[0];
    353352
    354         // get configuration
     353        /* Get configuration */
    355354        rc = net_get_device_conf_req(ip_globals.net_phone, ip_netif->device_id,
    356355            &configuration, count, &data);
     
    420419        }
    421420
    422         // binds the netif service which also initializes the device
     421        /* Bind netif service which also initializes the device */
    423422        ip_netif->phone = nil_bind_service(ip_netif->service,
    424423            (sysarg_t) ip_netif->device_id, SERVICE_IP,
     
    430429        }
    431430
    432         // has to be after the device netif module initialization
     431        /* Has to be after the device netif module initialization */
    433432        if (ip_netif->arp) {
    434433                if (route) {
     
    446445        }
    447446
    448         // get packet dimensions
     447        /* Get packet dimensions */
    449448        rc = nil_packet_size_req(ip_netif->phone, ip_netif->device_id,
    450449            &ip_netif->packet_dimension);
     
    464463       
    465464        if (gateway.s_addr) {
    466                 // the default gateway
     465                /* The default gateway */
    467466                ip_globals.gateway.address.s_addr = 0;
    468467                ip_globals.gateway.netmask.s_addr = 0;
     
    506505        if (rc != EOK) {
    507506                fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    508                 ip_routes_destroy(&ip_netif->routes);
     507                ip_routes_destroy(&ip_netif->routes, free);
    509508                free(ip_netif);
    510509                return rc;
     
    513512                ip_netif->arp->usage++;
    514513
    515         // print the settings
     514        /* Print the settings */
    516515        printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n",
    517516            NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv,
     
    588587        ip_netif_t *netif;
    589588
    590         // start with the last netif - the newest one
     589        /* Start with the last netif - the newest one */
    591590        index = ip_netifs_count(&ip_globals.netifs) - 1;
    592591        while (index >= 0) {
     
    630629        size_t length;
    631630
    632         // copy first itself
     631        /* Copy first itself */
    633632        memcpy(last, first, sizeof(ip_header_t));
    634633        length = sizeof(ip_header_t);
    635634        next = sizeof(ip_header_t);
    636635
    637         // process all ip options
     636        /* Process all IP options */
    638637        while (next < first->header_length) {
    639638                option = (ip_option_t *) (((uint8_t *) first) + next);
    640                 // skip end or noop
     639                /* Skip end or noop */
    641640                if ((option->type == IPOPT_END) ||
    642641                    (option->type == IPOPT_NOOP)) {
    643642                        next++;
    644643                } else {
    645                         // copy if told so or skip
     644                        /* Copy if told so or skip */
    646645                        if (IPOPT_COPIED(option->type)) {
    647646                                memcpy(((uint8_t *) last) + length,
     
    649648                                length += option->length;
    650649                        }
    651                         // next option
     650                        /* Next option */
    652651                        next += option->length;
    653652                }
    654653        }
    655654
    656         // align 4 byte boundary
     655        /* Align 4 byte boundary */
    657656        if (length % 4) {
    658657                bzero(((uint8_t *) last) + length, 4 - (length % 4));
     
    790789
    791790        header->total_length = htons(length);
    792         // unnecessary for all protocols
     791        /* Unnecessary for all protocols */
    793792        header->header_checksum = IP_HEADER_CHECKSUM(header);
    794793
     
    917916                return ENOMEM;
    918917
    919         // get header
     918        /* Get header */
    920919        header = (ip_header_t *) packet_get_data(packet);
    921920        if (!header)
    922921                return EINVAL;
    923922
    924         // fragmentation forbidden?
     923        /* Fragmentation forbidden? */
    925924        if(header->flags & IPFLAG_DONT_FRAGMENT)
    926925                return EPERM;
    927926
    928         // create the last fragment
     927        /* Create the last fragment */
    929928        new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, length,
    930929            suffix, ((addrlen > addr_len) ? addrlen : addr_len));
     
    932931                return ENOMEM;
    933932
    934         // allocate as much as originally
     933        /* Allocate as much as originally */
    935934        last_header = (ip_header_t *) packet_suffix(new_packet,
    936935            IP_HEADER_LENGTH(header));
     
    940939        ip_create_last_header(last_header, header);
    941940
    942         // trim the unused space
     941        /* Trim the unused space */
    943942        rc = packet_trim(new_packet, 0,
    944943            IP_HEADER_LENGTH(header) - IP_HEADER_LENGTH(last_header));
     
    946945                return ip_release_and_return(packet, rc);
    947946
    948         // biggest multiple of 8 lower than content
     947        /* Greatest multiple of 8 lower than content */
    949948        // TODO even fragmentation?
    950949        length = length & ~0x7;
     
    958957                return ip_release_and_return(packet, rc);
    959958
    960         // mark the first as fragmented
     959        /* Mark the first as fragmented */
    961960        header->flags |= IPFLAG_MORE_FRAGMENTS;
    962961
    963         // create middle framgents
     962        /* Create middle fragments */
    964963        while (IP_TOTAL_LENGTH(header) > length) {
    965964                new_packet = packet_get_4_remote(ip_globals.net_phone, prefix,
     
    982981        }
    983982
    984         // finish the first fragment
     983        /* Finish the first fragment */
    985984        header->header_checksum = IP_HEADER_CHECKSUM(header);
    986985
     
    10131012
    10141013        next = packet;
    1015         // check all packets
     1014        /* Check all packets */
    10161015        while (next) {
    10171016                length = packet_get_data_length(next);
     
    10221021                }
    10231022
    1024                 // too long
     1023                /* Too long */
    10251024                result = ip_fragment_packet(next, content, prefix,
    10261025                    suffix, addr_len);
     
    10281027                        new_packet = pq_detach(next);
    10291028                        if (next == packet) {
    1030                                 // the new first packet of the queue
     1029                                /* The new first packet of the queue */
    10311030                                packet = new_packet;
    10321031                        }
    1033                         // fragmentation needed?
     1032                        /* Fragmentation needed? */
    10341033                        if (result == EPERM) {
    10351034                                phone = ip_prepare_icmp_and_get_phone(
    10361035                                    error, next, NULL);
    10371036                                if (phone >= 0) {
    1038                                         // fragmentation necessary ICMP
     1037                                        /* Fragmentation necessary ICMP */
    10391038                                        icmp_destination_unreachable_msg(phone,
    10401039                                            ICMP_FRAG_NEEDED, content, next);
     
    10811080        int rc;
    10821081
    1083         // get destination hardware address
     1082        /* Get destination hardware address */
    10841083        if (netif->arp && (route->address.s_addr != dest.s_addr)) {
    10851084                destination.value = route->gateway.s_addr ?
     
    11031102                            NULL);
    11041103                        if (phone >= 0) {
    1105                                 // unreachable ICMP if no routing
     1104                                /* Unreachable ICMP if no routing */
    11061105                                icmp_destination_unreachable_msg(phone,
    11071106                                    ICMP_HOST_UNREACH, 0, packet);
     
    11491148        int rc;
    11501149
    1151         // addresses in the host byte order
    1152         // should be the next hop address or the target destination address
     1150        /*
     1151         * Addresses in the host byte order
     1152         * Should be the next hop address or the target destination address
     1153         */
    11531154        addrlen = packet_get_addr(packet, NULL, (uint8_t **) &addr);
    11541155        if (addrlen < 0)
     
    11751176        fibril_rwlock_read_lock(&ip_globals.netifs_lock);
    11761177
    1177         // device specified?
     1178        /* Device specified? */
    11781179        if (device_id > 0) {
    11791180                netif = ip_netifs_find(&ip_globals.netifs, device_id);
     
    11911192                phone = ip_prepare_icmp_and_get_phone(error, packet, NULL);
    11921193                if (phone >= 0) {
    1193                         // unreachable ICMP if no routing
     1194                        /* Unreachable ICMP if no routing */
    11941195                        icmp_destination_unreachable_msg(phone,
    11951196                            ICMP_NET_UNREACH, 0, packet);
     
    11991200
    12001201        if (error) {
    1201                 // do not send for broadcast, anycast packets or network
    1202                 // broadcast
     1202                /*
     1203                 * Do not send for broadcast, anycast packets or network
     1204                 * broadcast.
     1205                 */
    12031206                if (!dest->s_addr || !(~dest->s_addr) ||
    12041207                    !(~((dest->s_addr & ~route->netmask.s_addr) |
     
    12091212        }
    12101213       
    1211         // if the local host is the destination
     1214        /* If the local host is the destination */
    12121215        if ((route->address.s_addr == dest->s_addr) &&
    12131216            (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) {
    1214                 // find the loopback device to deliver
     1217                /* Find the loopback device to deliver */
    12151218                dest->s_addr = IPV4_LOCALHOST_ADDRESS;
    12161219                route = ip_find_route(*dest);
     
    12211224                            NULL);
    12221225                        if (phone >= 0) {
    1223                                 // unreachable ICMP if no routing
     1226                                /* Unreachable ICMP if no routing */
    12241227                                icmp_destination_unreachable_msg(phone,
    12251228                                    ICMP_HOST_UNREACH, 0, packet);
     
    12531256
    12541257        fibril_rwlock_write_lock(&ip_globals.netifs_lock);
    1255         // find the device
     1258        /* Find the device */
    12561259        netif = ip_netifs_find(&ip_globals.netifs, device_id);
    12571260        if (!netif) {
     
    13451348                return ip_release_and_return(packet, rc);
    13461349
    1347         // trim padding if present
     1350        /* Trim padding if present */
    13481351        if (!error &&
    13491352            (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) {
     
    13611364                phone = ip_prepare_icmp_and_get_phone(error, packet, header);
    13621365                if (phone >= 0) {
    1363                         // unreachable ICMP
     1366                        /* Unreachable ICMP */
    13641367                        icmp_destination_unreachable_msg(phone,
    13651368                            ICMP_PROT_UNREACH, 0, packet);
     
    14181421                return ip_release_and_return(packet, ENOMEM);
    14191422
    1420         // checksum
     1423        /* Checksum */
    14211424        if ((header->header_checksum) &&
    14221425            (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) {
    14231426                phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    14241427                if (phone >= 0) {
    1425                         // checksum error ICMP
     1428                        /* Checksum error ICMP */
    14261429                        icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER,
    14271430                            ((size_t) ((void *) &header->header_checksum)) -
     
    14341437                phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    14351438                if (phone >= 0) {
    1436                         // ttl exceeded ICMP
     1439                        /* TTL exceeded ICMP */
    14371440                        icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet);
    14381441                }
     
    14401443        }
    14411444       
    1442         // process ipopt and get destination
     1445        /* Process ipopt and get destination */
    14431446        dest = ip_get_destination(header);
    14441447
    1445         // set the addrination address
     1448        /* Set the destination address */
    14461449        switch (header->version) {
    14471450        case IPVERSION:
     
    14651468                phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    14661469                if (phone >= 0) {
    1467                         // unreachable ICMP
     1470                        /* Unreachable ICMP */
    14681471                        icmp_destination_unreachable_msg(phone,
    14691472                            ICMP_HOST_UNREACH, 0, packet);
     
    14731476
    14741477        if (route->address.s_addr == dest.s_addr) {
    1475                 // local delivery
     1478                /* Local delivery */
    14761479                return ip_deliver_local(device_id, packet, header, 0);
    14771480        }
     
    14851488        phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    14861489        if (phone >= 0) {
    1487                 // unreachable ICMP if no routing
     1490                /* Unreachable ICMP if no routing */
    14881491                icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0,
    14891492                    packet);
     
    15961599                        rc = ip_device_state_message(IPC_GET_DEVICE(*icall),
    15971600                            IPC_GET_STATE(*icall));
    1598                         ipc_answer_0(iid, (sysarg_t) rc);
     1601                        async_answer_0(iid, (sysarg_t) rc);
    15991602                        break;
    16001603               
     
    16101613                        }
    16111614                       
    1612                         ipc_answer_0(iid, (sysarg_t) rc);
     1615                        async_answer_0(iid, (sysarg_t) rc);
    16131616                        break;
    16141617               
     
    16161619                        rc = ip_mtu_changed_message(IPC_GET_DEVICE(*icall),
    16171620                            IPC_GET_MTU(*icall));
    1618                         ipc_answer_0(iid, (sysarg_t) rc);
     1621                        async_answer_0(iid, (sysarg_t) rc);
    16191622                        break;
    16201623               
    16211624                default:
    1622                         ipc_answer_0(iid, (sysarg_t) ENOTSUP);
     1625                        async_answer_0(iid, (sysarg_t) ENOTSUP);
    16231626                }
    16241627               
     
    17711774                header = (ip_header_t *)(data + offset);
    17721775
    1773                 // destination host unreachable?
     1776                /* Destination host unreachable? */
    17741777                if ((type != ICMP_DEST_UNREACH) ||
    17751778                    (code != ICMP_HOST_UNREACH)) {
    1776                         // no, something else
     1779                        /* No, something else */
    17771780                        break;
    17781781                }
     
    17881791                route = ip_routes_get_index(&netif->routes, 0);
    17891792
    1790                 // from the same network?
     1793                /* From the same network? */
    17911794                if (route && ((route->address.s_addr & route->netmask.s_addr) ==
    17921795                    (header->destination_address & route->netmask.s_addr))) {
    1793                         // clear the ARP mapping if any
     1796                        /* Clear the ARP mapping if any */
    17941797                        address.value = (uint8_t *) &header->destination_address;
    17951798                        address.length = sizeof(header->destination_address);
     
    18451848        fibril_rwlock_read_lock(&ip_globals.lock);
    18461849        route = ip_find_route(*dest);
    1847         // if the local host is the destination
     1850        /* If the local host is the destination */
    18481851        if (route && (route->address.s_addr == dest->s_addr) &&
    18491852            (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) {
    1850                 // find the loopback device to deliver
     1853                /* Find the loopback device to deliver */
    18511854                dest->s_addr = IPV4_LOCALHOST_ADDRESS;
    18521855                route = ip_find_route(*dest);
Note: See TracChangeset for help on using the changeset viewer.