Ignore:
File:
1 edited

Legend:

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

    rffa2c8ef r6b82009  
    120120GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t);
    121121
    122 static void ip_receiver(ipc_callid_t, ipc_call_t *);
    123 
    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.
     122static void ip_receiver(ipc_callid_t, ipc_call_t *, void *);
     123
     124/** Release the packet and returns the result.
     125 *
     126 * @param[in] packet Packet queue to be released.
     127 * @param[in] result Result to be returned.
     128 *
     129 * @return Result parameter.
     130 *
    129131 */
    130132static int ip_release_and_return(packet_t *packet, int result)
    131133{
    132         pq_release_remote(ip_globals.net_phone, packet_get_id(packet));
     134        pq_release_remote(ip_globals.net_sess, packet_get_id(packet));
    133135        return result;
    134136}
    135137
    136 /** Returns the ICMP phone.
    137  *
    138  * Searches the registered protocols.
    139  *
    140  * @return              The found ICMP phone.
    141  * @return              ENOENT if the ICMP is not registered.
    142  */
    143 static int ip_get_icmp_phone(void)
    144 {
    145         ip_proto_t *proto;
    146         int phone;
    147 
     138/** Return the ICMP session.
     139 *
     140 * Search the registered protocols.
     141 *
     142 * @return Found ICMP session.
     143 * @return NULL if the ICMP is not registered.
     144 *
     145 */
     146static async_sess_t *ip_get_icmp_session(void)
     147{
    148148        fibril_rwlock_read_lock(&ip_globals.protos_lock);
    149         proto = ip_protos_find(&ip_globals.protos, IPPROTO_ICMP);
    150         phone = proto ? proto->phone : ENOENT;
     149        ip_proto_t *proto = ip_protos_find(&ip_globals.protos, IPPROTO_ICMP);
     150        async_sess_t *sess = proto ? proto->sess : NULL;
    151151        fibril_rwlock_read_unlock(&ip_globals.protos_lock);
    152         return phone;
     152       
     153        return sess;
    153154}
    154155
     
    176177        socklen_t addrlen;
    177178
    178         // detach the first packet and release the others
     179        /* Detach the first packet and release the others */
    179180        next = pq_detach(packet);
    180181        if (next)
    181                 pq_release_remote(ip_globals.net_phone, packet_get_id(next));
     182                pq_release_remote(ip_globals.net_sess, packet_get_id(next));
    182183
    183184        if (!header) {
     
    185186                        return ENOMEM;
    186187
    187                 // get header
     188                /* Get header */
    188189                header = (ip_header_t *) packet_get_data(packet);
    189190                if (!header)
     
    192193        }
    193194
    194         // only for the first fragment
     195        /* Only for the first fragment */
    195196        if (IP_FRAGMENT_OFFSET(header))
    196197                return EINVAL;
    197198
    198         // not for the ICMP protocol
     199        /* Not for the ICMP protocol */
    199200        if (header->protocol == IPPROTO_ICMP)
    200201                return EPERM;
    201202
    202         // set the destination address
    203         switch (header->version) {
     203        /* Set the destination address */
     204        switch (GET_IP_HEADER_VERSION(header)) {
    204205        case IPVERSION:
    205206                addrlen = sizeof(dest_in);
     
    218219}
    219220
    220 /** Prepares the ICMP notification packet.
    221  *
    222  * Releases additional packets and keeps only the first one.
     221/** Prepare the ICMP notification packet.
     222 *
     223 * Release additional packets and keep only the first one.
    223224 * All packets are released on error.
    224225 *
    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.
    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.
    232  */
    233 static int
    234 ip_prepare_icmp_and_get_phone(services_t error, packet_t *packet,
    235     ip_header_t *header)
    236 {
    237         int phone;
    238 
    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 }
    244 
    245 int il_initialize(int net_phone)
     226 * @param[in] error  Packet error service.
     227 * @param[in] packet Packet or the packet queue to be reported as faulty.
     228 * @param[in] header First packet IP header. May be NULL.
     229 *
     230 * @return Found ICMP session.
     231 * @return NULL if the error parameter is set.
     232 * @return NULL if the ICMP session is not found.
     233 * @return NULL if the ip_prepare_icmp() fails.
     234 *
     235 */
     236static async_sess_t *ip_prepare_icmp_and_get_session(services_t error,
     237    packet_t *packet, ip_header_t *header)
     238{
     239        async_sess_t *sess = ip_get_icmp_session();
     240       
     241        if ((error) || (!sess) || (ip_prepare_icmp(packet, header))) {
     242                pq_release_remote(ip_globals.net_sess, packet_get_id(packet));
     243                return NULL;
     244        }
     245       
     246        return sess;
     247}
     248
     249int il_initialize(async_sess_t *net_sess)
    246250{
    247251        fibril_rwlock_initialize(&ip_globals.lock);
     
    250254        fibril_rwlock_initialize(&ip_globals.netifs_lock);
    251255       
    252         ip_globals.net_phone = net_phone;
     256        ip_globals.net_sess = net_sess;
    253257        ip_globals.packet_counter = 0;
    254258        ip_globals.gateway.address.s_addr = 0;
     
    351355        configuration = &names[0];
    352356
    353         // get configuration
    354         rc = net_get_device_conf_req(ip_globals.net_phone, ip_netif->device_id,
     357        /* Get configuration */
     358        rc = net_get_device_conf_req(ip_globals.net_sess, ip_netif->device_id,
    355359            &configuration, count, &data);
    356360        if (rc != EOK)
     
    419423        }
    420424
    421         // binds the netif service which also initializes the device
    422         ip_netif->phone = nil_bind_service(ip_netif->service,
     425        /* Bind netif service which also initializes the device */
     426        ip_netif->sess = nil_bind_service(ip_netif->service,
    423427            (sysarg_t) ip_netif->device_id, SERVICE_IP,
    424428            ip_receiver);
    425         if (ip_netif->phone < 0) {
     429        if (ip_netif->sess == NULL) {
    426430                printf("Failed to contact the nil service %d\n",
    427431                    ip_netif->service);
    428                 return ip_netif->phone;
    429         }
    430 
    431         // has to be after the device netif module initialization
     432                return ENOENT;
     433        }
     434
     435        /* Has to be after the device netif module initialization */
    432436        if (ip_netif->arp) {
    433437                if (route) {
     
    435439                        address.length = sizeof(in_addr_t);
    436440                       
    437                         rc = arp_device_req(ip_netif->arp->phone,
     441                        rc = arp_device_req(ip_netif->arp->sess,
    438442                            ip_netif->device_id, SERVICE_IP, ip_netif->service,
    439443                            &address);
     
    445449        }
    446450
    447         // get packet dimensions
    448         rc = nil_packet_size_req(ip_netif->phone, ip_netif->device_id,
     451        /* Get packet dimensions */
     452        rc = nil_packet_size_req(ip_netif->sess, ip_netif->device_id,
    449453            &ip_netif->packet_dimension);
    450454        if (rc != EOK)
     
    463467       
    464468        if (gateway.s_addr) {
    465                 // the default gateway
     469                /* The default gateway */
    466470                ip_globals.gateway.address.s_addr = 0;
    467471                ip_globals.gateway.netmask.s_addr = 0;
     
    478482}
    479483
    480 static int ip_device_req_local(int il_phone, device_id_t device_id,
    481     services_t netif)
     484static int ip_device_req_local(device_id_t device_id, services_t netif)
    482485{
    483486        ip_netif_t *ip_netif;
     
    505508        if (rc != EOK) {
    506509                fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    507                 ip_routes_destroy(&ip_netif->routes);
     510                ip_routes_destroy(&ip_netif->routes, free);
    508511                free(ip_netif);
    509512                return rc;
     
    512515                ip_netif->arp->usage++;
    513516
    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        /* Print the settings */
     518        printf("%s: Device registered (id: %d, ipv: %d, conf: %s)\n",
     519            NAME, ip_netif->device_id, ip_netif->ipv,
    517520            ip_netif->dhcp ? "dhcp" : "static");
    518521       
     
    587590        ip_netif_t *netif;
    588591
    589         // start with the last netif - the newest one
     592        /* Start with the last netif - the newest one */
    590593        index = ip_netifs_count(&ip_globals.netifs) - 1;
    591594        while (index >= 0) {
     
    629632        size_t length;
    630633
    631         // copy first itself
     634        /* Copy first itself */
    632635        memcpy(last, first, sizeof(ip_header_t));
    633636        length = sizeof(ip_header_t);
    634637        next = sizeof(ip_header_t);
    635638
    636         // process all ip options
    637         while (next < first->header_length) {
     639        /* Process all IP options */
     640        while (next < GET_IP_HEADER_LENGTH(first)) {
    638641                option = (ip_option_t *) (((uint8_t *) first) + next);
    639                 // skip end or noop
     642                /* Skip end or noop */
    640643                if ((option->type == IPOPT_END) ||
    641644                    (option->type == IPOPT_NOOP)) {
    642645                        next++;
    643646                } else {
    644                         // copy if told so or skip
     647                        /* Copy if told so or skip */
    645648                        if (IPOPT_COPIED(option->type)) {
    646649                                memcpy(((uint8_t *) last) + length,
     
    648651                                length += option->length;
    649652                        }
    650                         // next option
     653                        /* Next option */
    651654                        next += option->length;
    652655                }
    653656        }
    654657
    655         // align 4 byte boundary
     658        /* Align 4 byte boundary */
    656659        if (length % 4) {
    657660                bzero(((uint8_t *) last) + length, 4 - (length % 4));
    658                 last->header_length = length / 4 + 1;
     661                SET_IP_HEADER_LENGTH(last, (length / 4 + 1));
    659662        } else {
    660                 last->header_length = length / 4;
     663                SET_IP_HEADER_LENGTH(last, (length / 4));
    661664        }
    662665
     
    706709                return rc;
    707710       
    708         header->version = IPV4;
    709         header->fragment_offset_high = 0;
     711        SET_IP_HEADER_VERSION(header, IPV4);
     712        SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(header, 0);
    710713        header->fragment_offset_low = 0;
    711714        header->header_checksum = 0;
     
    735738                        memcpy(middle_header, last_header,
    736739                            IP_HEADER_LENGTH(last_header));
    737                         header->flags |= IPFLAG_MORE_FRAGMENTS;
     740                        SET_IP_HEADER_FLAGS(header,
     741                            (GET_IP_HEADER_FLAGS(header) | IPFLAG_MORE_FRAGMENTS));
    738742                        middle_header->total_length =
    739743                            htons(packet_get_data_length(next));
    740                         middle_header->fragment_offset_high =
    741                             IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length);
     744                        SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(middle_header,
     745                            IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length));
    742746                        middle_header->fragment_offset_low =
    743747                            IP_COMPUTE_FRAGMENT_OFFSET_LOW(length);
     
    768772                middle_header->total_length =
    769773                    htons(packet_get_data_length(next));
    770                 middle_header->fragment_offset_high =
    771                     IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length);
     774                SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(middle_header,
     775                    IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length));
    772776                middle_header->fragment_offset_low =
    773777                    IP_COMPUTE_FRAGMENT_OFFSET_LOW(length);
     
    785789                length += packet_get_data_length(next);
    786790                free(last_header);
    787                 header->flags |= IPFLAG_MORE_FRAGMENTS;
     791                SET_IP_HEADER_FLAGS(header,
     792                    (GET_IP_HEADER_FLAGS(header) | IPFLAG_MORE_FRAGMENTS));
    788793        }
    789794
    790795        header->total_length = htons(length);
    791         // unnecessary for all protocols
     796        /* Unnecessary for all protocols */
    792797        header->header_checksum = IP_HEADER_CHECKSUM(header);
    793798
     
    834839        new_header->total_length = htons(IP_HEADER_LENGTH(new_header) + length);
    835840        offset = IP_FRAGMENT_OFFSET(header) + IP_HEADER_DATA_LENGTH(header);
    836         new_header->fragment_offset_high =
    837             IP_COMPUTE_FRAGMENT_OFFSET_HIGH(offset);
     841        SET_IP_HEADER_FRAGMENT_OFFSET_HIGH(new_header,
     842            IP_COMPUTE_FRAGMENT_OFFSET_HIGH(offset));
    838843        new_header->fragment_offset_low =
    839844            IP_COMPUTE_FRAGMENT_OFFSET_LOW(offset);
     
    865870                return NULL;
    866871        memcpy(middle, last, IP_HEADER_LENGTH(last));
    867         middle->flags |= IPFLAG_MORE_FRAGMENTS;
     872        SET_IP_HEADER_FLAGS(middle,
     873            (GET_IP_HEADER_FLAGS(middle) | IPFLAG_MORE_FRAGMENTS));
    868874        return middle;
    869875}
     
    916922                return ENOMEM;
    917923
    918         // get header
     924        /* Get header */
    919925        header = (ip_header_t *) packet_get_data(packet);
    920926        if (!header)
    921927                return EINVAL;
    922928
    923         // fragmentation forbidden?
    924         if(header->flags & IPFLAG_DONT_FRAGMENT)
     929        /* Fragmentation forbidden? */
     930        if(GET_IP_HEADER_FLAGS(header) & IPFLAG_DONT_FRAGMENT)
    925931                return EPERM;
    926932
    927         // create the last fragment
    928         new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, length,
     933        /* Create the last fragment */
     934        new_packet = packet_get_4_remote(ip_globals.net_sess, prefix, length,
    929935            suffix, ((addrlen > addr_len) ? addrlen : addr_len));
    930936        if (!new_packet)
    931937                return ENOMEM;
    932938
    933         // allocate as much as originally
     939        /* Allocate as much as originally */
    934940        last_header = (ip_header_t *) packet_suffix(new_packet,
    935941            IP_HEADER_LENGTH(header));
     
    939945        ip_create_last_header(last_header, header);
    940946
    941         // trim the unused space
     947        /* Trim the unused space */
    942948        rc = packet_trim(new_packet, 0,
    943949            IP_HEADER_LENGTH(header) - IP_HEADER_LENGTH(last_header));
     
    945951                return ip_release_and_return(packet, rc);
    946952
    947         // biggest multiple of 8 lower than content
     953        /* Greatest multiple of 8 lower than content */
    948954        // TODO even fragmentation?
    949955        length = length & ~0x7;
     
    957963                return ip_release_and_return(packet, rc);
    958964
    959         // mark the first as fragmented
    960         header->flags |= IPFLAG_MORE_FRAGMENTS;
    961 
    962         // create middle framgents
     965        /* Mark the first as fragmented */
     966        SET_IP_HEADER_FLAGS(header,
     967            (GET_IP_HEADER_FLAGS(header) | IPFLAG_MORE_FRAGMENTS));
     968
     969        /* Create middle fragments */
    963970        while (IP_TOTAL_LENGTH(header) > length) {
    964                 new_packet = packet_get_4_remote(ip_globals.net_phone, prefix,
     971                new_packet = packet_get_4_remote(ip_globals.net_sess, prefix,
    965972                    length, suffix,
    966973                    ((addrlen >= addr_len) ? addrlen : addr_len));
     
    981988        }
    982989
    983         // finish the first fragment
     990        /* Finish the first fragment */
    984991        header->header_checksum = IP_HEADER_CHECKSUM(header);
    985992
     
    987994}
    988995
    989 /** Checks the packet queue lengths and fragments the packets if needed.
     996/** Check the packet queue lengths and fragments the packets if needed.
    990997 *
    991998 * The ICMP_FRAG_NEEDED error notification may be sent if the packet needs to
    992999 * be fragmented and the fragmentation is not allowed.
    9931000 *
    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.
    1000  * @return              The packet or the packet queue of the allowed length.
    1001  * @return              NULL if there are no packets left.
    1002  */
    1003 static packet_t *
    1004 ip_split_packet(packet_t *packet, size_t prefix, size_t content, size_t suffix,
    1005     socklen_t addr_len, services_t error)
     1001 * @param[in,out] packet   Packet or the packet queue to be checked.
     1002 * @param[in]     prefix   Minimum prefix size.
     1003 * @param[in]     content  Maximum content size.
     1004 * @param[in]     suffix   Minimum suffix size.
     1005 * @param[in]     addr_len Minimum address length.
     1006 * @param[in]     error    Error module service.
     1007 *
     1008 * @return The packet or the packet queue of the allowed length.
     1009 * @return NULL if there are no packets left.
     1010 *
     1011 */
     1012static packet_t *ip_split_packet(packet_t *packet, size_t prefix, size_t content,
     1013    size_t suffix, socklen_t addr_len, services_t error)
    10061014{
    10071015        size_t length;
     
    10091017        packet_t *new_packet;
    10101018        int result;
    1011         int phone;
    1012 
     1019        async_sess_t *sess;
     1020       
    10131021        next = packet;
    1014         // check all packets
     1022        /* Check all packets */
    10151023        while (next) {
    10161024                length = packet_get_data_length(next);
     
    10211029                }
    10221030
    1023                 // too long
     1031                /* Too long */
    10241032                result = ip_fragment_packet(next, content, prefix,
    10251033                    suffix, addr_len);
     
    10271035                        new_packet = pq_detach(next);
    10281036                        if (next == packet) {
    1029                                 // the new first packet of the queue
     1037                                /* The new first packet of the queue */
    10301038                                packet = new_packet;
    10311039                        }
    1032                         // fragmentation needed?
     1040                        /* Fragmentation needed? */
    10331041                        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,
     1042                                sess = ip_prepare_icmp_and_get_session(error, next, NULL);
     1043                                if (sess) {
     1044                                        /* Fragmentation necessary ICMP */
     1045                                        icmp_destination_unreachable_msg(sess,
    10391046                                            ICMP_FRAG_NEEDED, content, next);
    10401047                                }
    10411048                        } else {
    1042                                 pq_release_remote(ip_globals.net_phone,
     1049                                pq_release_remote(ip_globals.net_sess,
    10431050                                    packet_get_id(next));
    10441051                        }
     
    10541061}
    10551062
    1056 /** Sends the packet or the packet queue via the specified route.
     1063/** Send the packet or the packet queue via the specified route.
    10571064 *
    10581065 * The ICMP_HOST_UNREACH error notification may be sent if route hardware
    10591066 * destination address is found.
    10601067 *
    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.
    1067  * @return              EOK on success.
    1068  * @return              Other error codes as defined for the arp_translate_req()
    1069  *                      function.
    1070  * @return              Other error codes as defined for the ip_prepare_packet()
    1071  *                      function.
     1068 * @param[in,out] packet Packet to be sent.
     1069 * @param[in]     netif  Target network interface.
     1070 * @param[in]     route  Target route.
     1071 * @param[in]     src    Source address.
     1072 * @param[in]     dest   Destination address.
     1073 * @param[in]     error  Error module service.
     1074 *
     1075 * @return EOK on success.
     1076 * @return Other error codes as defined for arp_translate_req().
     1077 * @return Other error codes as defined for ip_prepare_packet().
     1078 *
    10721079 */
    10731080static int ip_send_route(packet_t *packet, ip_netif_t *netif,
     
    10771084        measured_string_t *translation;
    10781085        uint8_t *data;
    1079         int phone;
     1086        async_sess_t *sess;
    10801087        int rc;
    10811088
    1082         // get destination hardware address
     1089        /* Get destination hardware address */
    10831090        if (netif->arp && (route->address.s_addr != dest.s_addr)) {
    10841091                destination.value = route->gateway.s_addr ?
     
    10861093                destination.length = sizeof(dest.s_addr);
    10871094
    1088                 rc = arp_translate_req(netif->arp->phone, netif->device_id,
     1095                rc = arp_translate_req(netif->arp->sess, netif->device_id,
    10891096                    SERVICE_IP, &destination, &translation, &data);
    10901097                if (rc != EOK) {
    1091                         pq_release_remote(ip_globals.net_phone,
     1098                        pq_release_remote(ip_globals.net_sess,
    10921099                            packet_get_id(packet));
    10931100                        return rc;
     
    10991106                                free(data);
    11001107                        }
    1101                         phone = ip_prepare_icmp_and_get_phone(error, packet,
     1108                        sess = ip_prepare_icmp_and_get_session(error, packet,
    11021109                            NULL);
    1103                         if (phone >= 0) {
    1104                                 // unreachable ICMP if no routing
    1105                                 icmp_destination_unreachable_msg(phone,
     1110                        if (sess) {
     1111                                /* Unreachable ICMP if no routing */
     1112                                icmp_destination_unreachable_msg(sess,
    11061113                                    ICMP_HOST_UNREACH, 0, packet);
    11071114                        }
     
    11151122        rc = ip_prepare_packet(src, dest, packet, translation);
    11161123        if (rc != EOK) {
    1117                 pq_release_remote(ip_globals.net_phone, packet_get_id(packet));
     1124                pq_release_remote(ip_globals.net_sess, packet_get_id(packet));
    11181125        } else {
    11191126                packet = ip_split_packet(packet, netif->packet_dimension.prefix,
     
    11221129                    netif->packet_dimension.addr_len, error);
    11231130                if (packet) {
    1124                         nil_send_msg(netif->phone, netif->device_id, packet,
     1131                        nil_send_msg(netif->sess, netif->device_id, packet,
    11251132                            SERVICE_IP);
    11261133                }
     
    11351142}
    11361143
    1137 static int ip_send_msg_local(int il_phone, device_id_t device_id,
    1138     packet_t *packet, services_t sender, services_t error)
     1144static int ip_send_msg_local(device_id_t device_id, packet_t *packet,
     1145    services_t sender, services_t error)
    11391146{
    11401147        int addrlen;
     
    11451152        in_addr_t *dest;
    11461153        in_addr_t *src;
    1147         int phone;
     1154        async_sess_t *sess;
    11481155        int rc;
    11491156
    1150         // addresses in the host byte order
    1151         // should be the next hop address or the target destination address
     1157        /*
     1158         * Addresses in the host byte order
     1159         * Should be the next hop address or the target destination address
     1160         */
    11521161        addrlen = packet_get_addr(packet, NULL, (uint8_t **) &addr);
    11531162        if (addrlen < 0)
     
    11741183        fibril_rwlock_read_lock(&ip_globals.netifs_lock);
    11751184
    1176         // device specified?
     1185        /* Device specified? */
    11771186        if (device_id > 0) {
    11781187                netif = ip_netifs_find(&ip_globals.netifs, device_id);
     
    11881197        if (!netif || !route) {
    11891198                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,
     1199                sess = ip_prepare_icmp_and_get_session(error, packet, NULL);
     1200                if (sess) {
     1201                        /* Unreachable ICMP if no routing */
     1202                        icmp_destination_unreachable_msg(sess,
    11941203                            ICMP_NET_UNREACH, 0, packet);
    11951204                }
     
    11981207
    11991208        if (error) {
    1200                 // do not send for broadcast, anycast packets or network
    1201                 // broadcast
     1209                /*
     1210                 * Do not send for broadcast, anycast packets or network
     1211                 * broadcast.
     1212                 */
    12021213                if (!dest->s_addr || !(~dest->s_addr) ||
    12031214                    !(~((dest->s_addr & ~route->netmask.s_addr) |
     
    12081219        }
    12091220       
    1210         // if the local host is the destination
     1221        /* If the local host is the destination */
    12111222        if ((route->address.s_addr == dest->s_addr) &&
    12121223            (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) {
    1213                 // find the loopback device to deliver
     1224                /* Find the loopback device to deliver */
    12141225                dest->s_addr = IPV4_LOCALHOST_ADDRESS;
    12151226                route = ip_find_route(*dest);
     
    12171228                if (!netif || !route) {
    12181229                        fibril_rwlock_read_unlock(&ip_globals.netifs_lock);
    1219                         phone = ip_prepare_icmp_and_get_phone(error, packet,
     1230                        sess = ip_prepare_icmp_and_get_session(error, packet,
    12201231                            NULL);
    1221                         if (phone >= 0) {
    1222                                 // unreachable ICMP if no routing
    1223                                 icmp_destination_unreachable_msg(phone,
     1232                        if (sess) {
     1233                                /* Unreachable ICMP if no routing */
     1234                                icmp_destination_unreachable_msg(sess,
    12241235                                    ICMP_HOST_UNREACH, 0, packet);
    12251236                        }
     
    12521263
    12531264        fibril_rwlock_write_lock(&ip_globals.netifs_lock);
    1254         // find the device
     1265        /* Find the device */
    12551266        netif = ip_netifs_find(&ip_globals.netifs, device_id);
    12561267        if (!netif) {
     
    13051316{
    13061317        ip_proto_t *proto;
    1307         int phone;
     1318        async_sess_t *sess;
    13081319        services_t service;
    13091320        tl_received_msg_t received_msg;
     
    13151326        int rc;
    13161327
    1317         if ((header->flags & IPFLAG_MORE_FRAGMENTS) ||
     1328        if ((GET_IP_HEADER_FLAGS(header) & IPFLAG_MORE_FRAGMENTS) ||
    13181329            IP_FRAGMENT_OFFSET(header)) {
    13191330                // TODO fragmented
     
    13211332        }
    13221333       
    1323         switch (header->version) {
     1334        switch (GET_IP_HEADER_VERSION(header)) {
    13241335        case IPVERSION:
    13251336                addrlen = sizeof(src_in);
     
    13441355                return ip_release_and_return(packet, rc);
    13451356
    1346         // trim padding if present
     1357        /* Trim padding if present */
    13471358        if (!error &&
    13481359            (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) {
     
    13581369        if (!proto) {
    13591370                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,
     1371                sess = ip_prepare_icmp_and_get_session(error, packet, header);
     1372                if (sess) {
     1373                        /* Unreachable ICMP */
     1374                        icmp_destination_unreachable_msg(sess,
    13641375                            ICMP_PROT_UNREACH, 0, packet);
    13651376                }
     
    13731384                rc = received_msg(device_id, packet, service, error);
    13741385        } else {
    1375                 rc = tl_received_msg(proto->phone, device_id, packet,
     1386                rc = tl_received_msg(proto->sess, device_id, packet,
    13761387                    proto->service, error);
    13771388                fibril_rwlock_read_unlock(&ip_globals.protos_lock);
     
    14071418        in_addr_t dest;
    14081419        ip_route_t *route;
    1409         int phone;
     1420        async_sess_t *sess;
    14101421        struct sockaddr *addr;
    14111422        struct sockaddr_in addr_in;
     
    14171428                return ip_release_and_return(packet, ENOMEM);
    14181429
    1419         // checksum
     1430        /* Checksum */
    14201431        if ((header->header_checksum) &&
    14211432            (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) {
    1422                 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    1423                 if (phone >= 0) {
    1424                         // checksum error ICMP
    1425                         icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER,
     1433                sess = ip_prepare_icmp_and_get_session(0, packet, header);
     1434                if (sess) {
     1435                        /* Checksum error ICMP */
     1436                        icmp_parameter_problem_msg(sess, ICMP_PARAM_POINTER,
    14261437                            ((size_t) ((void *) &header->header_checksum)) -
    14271438                            ((size_t) ((void *) header)), packet);
     
    14311442
    14321443        if (header->ttl <= 1) {
    1433                 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    1434                 if (phone >= 0) {
    1435                         // ttl exceeded ICMP
    1436                         icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet);
     1444                sess = ip_prepare_icmp_and_get_session(0, packet, header);
     1445                if (sess) {
     1446                        /* TTL exceeded ICMP */
     1447                        icmp_time_exceeded_msg(sess, ICMP_EXC_TTL, packet);
    14371448                }
    14381449                return EINVAL;
    14391450        }
    14401451       
    1441         // process ipopt and get destination
     1452        /* Process ipopt and get destination */
    14421453        dest = ip_get_destination(header);
    14431454
    1444         // set the addrination address
    1445         switch (header->version) {
     1455        /* Set the destination address */
     1456        switch (GET_IP_HEADER_VERSION(header)) {
    14461457        case IPVERSION:
    14471458                addrlen = sizeof(addr_in);
     
    14621473        route = ip_find_route(dest);
    14631474        if (!route) {
    1464                 phone = ip_prepare_icmp_and_get_phone(0, packet, header);
    1465                 if (phone >= 0) {
    1466                         // unreachable ICMP
    1467                         icmp_destination_unreachable_msg(phone,
     1475                sess = ip_prepare_icmp_and_get_session(0, packet, header);
     1476                if (sess) {
     1477                        /* Unreachable ICMP */
     1478                        icmp_destination_unreachable_msg(sess,
    14681479                            ICMP_HOST_UNREACH, 0, packet);
    14691480                }
     
    14721483
    14731484        if (route->address.s_addr == dest.s_addr) {
    1474                 // local delivery
     1485                /* Local delivery */
    14751486                return ip_deliver_local(device_id, packet, header, 0);
    14761487        }
     
    14821493        }
    14831494
    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,
     1495        sess = ip_prepare_icmp_and_get_session(0, packet, header);
     1496        if (sess) {
     1497                /* Unreachable ICMP if no routing */
     1498                icmp_destination_unreachable_msg(sess, ICMP_HOST_UNREACH, 0,
    14881499                    packet);
    14891500        }
     
    14921503}
    14931504
    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.
     1505/** Return the device packet dimensions for sending.
     1506 *
     1507 * @param[in]  device_id Device identifier.
     1508 * @param[out] addr_len  Minimum reserved address length.
     1509 * @param[out] prefix    Minimum reserved prefix size.
     1510 * @param[out] content   Maximum content size.
     1511 * @param[out] suffix    Minimum reserved suffix size.
     1512 *
     1513 * @return EOK on success.
     1514 *
    15041515 */
    15051516static int ip_packet_size_message(device_id_t device_id, size_t *addr_len,
     
    15831594 * @param[in]     iid   Message identifier.
    15841595 * @param[in,out] icall Message parameters.
    1585  *
    1586  */
    1587 static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall)
     1596 * @param[in]     arg   Local argument.
     1597 *
     1598 */
     1599static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    15881600{
    15891601        packet_t *packet;
     
    15991611               
    16001612                case NET_IL_RECEIVED:
    1601                         rc = packet_translate_remote(ip_globals.net_phone, &packet,
     1613                        rc = packet_translate_remote(ip_globals.net_sess, &packet,
    16021614                            IPC_GET_PACKET(*icall));
    16031615                        if (rc == EOK) {
     
    16261638}
    16271639
    1628 /** Registers the transport layer protocol.
     1640/** Register the transport layer protocol.
    16291641 *
    16301642 * The traffic of this protocol will be supplied using either the receive
    16311643 * function or IPC message.
    16321644 *
    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  */
    1644 static int
    1645 ip_register(int protocol, services_t service, int phone,
     1645 * @param[in] protocol     Transport layer module protocol.
     1646 * @param[in] service      Transport layer module service.
     1647 * @param[in] sess         Transport layer module session.
     1648 * @param[in] received_msg Receiving function.
     1649 *
     1650 * @return EOK on success.
     1651 * @return EINVAL if the protocol parameter and/or the service
     1652 *         parameter is zero.
     1653 * @return EINVAL if the phone parameter is not a positive number
     1654 *         and the tl_receive_msg is NULL.
     1655 * @return ENOMEM if there is not enough memory left.
     1656 *
     1657 */
     1658static int ip_register(int protocol, services_t service, async_sess_t *sess,
    16461659    tl_received_msg_t received_msg)
    16471660{
     
    16491662        int index;
    16501663
    1651         if (!protocol || !service || ((phone < 0) && !received_msg))
     1664        if ((!protocol) || (!service) || ((!sess) && (!received_msg)))
    16521665                return EINVAL;
    1653 
     1666       
    16541667        proto = (ip_proto_t *) malloc(sizeof(ip_protos_t));
    16551668        if (!proto)
     
    16581671        proto->protocol = protocol;
    16591672        proto->service = service;
    1660         proto->phone = phone;
     1673        proto->sess = sess;
    16611674        proto->received_msg = received_msg;
    16621675
     
    16701683        fibril_rwlock_write_unlock(&ip_globals.protos_lock);
    16711684
    1672         printf("%s: Protocol registered (protocol: %d, phone: %d)\n",
    1673             NAME, proto->protocol, proto->phone);
     1685        printf("%s: Protocol registered (protocol: %d)\n",
     1686            NAME, proto->protocol);
    16741687
    16751688        return EOK;
    16761689}
    16771690
    1678 
    1679 static int
    1680 ip_add_route_req_local(int ip_phone, device_id_t device_id, in_addr_t address,
     1691static int ip_add_route_req_local(device_id_t device_id, in_addr_t address,
    16811692    in_addr_t netmask, in_addr_t gateway)
    16821693{
     
    17121723}
    17131724
    1714 static int
    1715 ip_set_gateway_req_local(int ip_phone, device_id_t device_id, in_addr_t gateway)
     1725static int ip_set_gateway_req_local(device_id_t device_id, in_addr_t gateway)
    17161726{
    17171727        ip_netif_t *netif;
     
    17371747/** Notify the IP module about the received error notification packet.
    17381748 *
    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.
    1747  *
    1748  */
    1749 static int
    1750 ip_received_error_msg_local(int ip_phone, device_id_t device_id,
     1749 * @param[in] device_id Device identifier.
     1750 * @param[in] packet    Received packet or the received packet queue.
     1751 * @param[in] target    Target internetwork module service to be
     1752 *                      delivered to.
     1753 * @param[in] error     Packet error reporting service. Prefixes the
     1754 *                      received packet.
     1755 *
     1756 * @return EOK on success.
     1757 *
     1758 */
     1759static int ip_received_error_msg_local(device_id_t device_id,
    17511760    packet_t *packet, services_t target, services_t error)
    17521761{
     
    17701779                header = (ip_header_t *)(data + offset);
    17711780
    1772                 // destination host unreachable?
     1781                /* Destination host unreachable? */
    17731782                if ((type != ICMP_DEST_UNREACH) ||
    17741783                    (code != ICMP_HOST_UNREACH)) {
    1775                         // no, something else
     1784                        /* No, something else */
    17761785                        break;
    17771786                }
     
    17871796                route = ip_routes_get_index(&netif->routes, 0);
    17881797
    1789                 // from the same network?
     1798                /* From the same network? */
    17901799                if (route && ((route->address.s_addr & route->netmask.s_addr) ==
    17911800                    (header->destination_address & route->netmask.s_addr))) {
    1792                         // clear the ARP mapping if any
     1801                        /* Clear the ARP mapping if any */
    17931802                        address.value = (uint8_t *) &header->destination_address;
    17941803                        address.length = sizeof(header->destination_address);
    1795                         arp_clear_address_req(netif->arp->phone,
     1804                        arp_clear_address_req(netif->arp->sess,
    17961805                            netif->device_id, SERVICE_IP, &address);
    17971806                }
     
    18071816}
    18081817
    1809 static int
    1810 ip_get_route_req_local(int ip_phone, ip_protocol_t protocol,
     1818static int ip_get_route_req_local(ip_protocol_t protocol,
    18111819    const struct sockaddr *destination, socklen_t addrlen,
    18121820    device_id_t *device_id, void **header, size_t *headerlen)
     
    18441852        fibril_rwlock_read_lock(&ip_globals.lock);
    18451853        route = ip_find_route(*dest);
    1846         // if the local host is the destination
     1854        /* If the local host is the destination */
    18471855        if (route && (route->address.s_addr == dest->s_addr) &&
    18481856            (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) {
    1849                 // find the loopback device to deliver
     1857                /* Find the loopback device to deliver */
    18501858                dest->s_addr = IPV4_LOCALHOST_ADDRESS;
    18511859                route = ip_find_route(*dest);
     
    19051913       
    19061914        *answer_count = 0;
     1915       
     1916        if (!IPC_GET_IMETHOD(*call))
     1917                return EOK;
     1918       
     1919        async_sess_t *callback =
     1920            async_callback_receive_start(EXCHANGE_SERIALIZE, call);
     1921        if (callback)
     1922                return ip_register(IL_GET_PROTO(*call), IL_GET_SERVICE(*call),
     1923                    callback, NULL);
     1924       
    19071925        switch (IPC_GET_IMETHOD(*call)) {
    1908         case IPC_M_PHONE_HUNGUP:
    1909                 return EOK;
    1910        
    1911         case IPC_M_CONNECT_TO_ME:
    1912                 return ip_register(IL_GET_PROTO(*call), IL_GET_SERVICE(*call),
    1913                     IPC_GET_PHONE(*call), NULL);
    1914        
    19151926        case NET_IP_DEVICE:
    1916                 return ip_device_req_local(0, IPC_GET_DEVICE(*call),
     1927                return ip_device_req_local(IPC_GET_DEVICE(*call),
    19171928                    IPC_GET_SERVICE(*call));
    19181929       
    19191930        case NET_IP_RECEIVED_ERROR:
    1920                 rc = packet_translate_remote(ip_globals.net_phone, &packet,
     1931                rc = packet_translate_remote(ip_globals.net_sess, &packet,
    19211932                    IPC_GET_PACKET(*call));
    19221933                if (rc != EOK)
    19231934                        return rc;
    1924                 return ip_received_error_msg_local(0, IPC_GET_DEVICE(*call),
     1935                return ip_received_error_msg_local(IPC_GET_DEVICE(*call),
    19251936                    packet, IPC_GET_TARGET(*call), IPC_GET_ERROR(*call));
    19261937       
    19271938        case NET_IP_ADD_ROUTE:
    1928                 return ip_add_route_req_local(0, IPC_GET_DEVICE(*call),
     1939                return ip_add_route_req_local(IPC_GET_DEVICE(*call),
    19291940                    IP_GET_ADDRESS(*call), IP_GET_NETMASK(*call),
    19301941                    IP_GET_GATEWAY(*call));
    19311942
    19321943        case NET_IP_SET_GATEWAY:
    1933                 return ip_set_gateway_req_local(0, IPC_GET_DEVICE(*call),
     1944                return ip_set_gateway_req_local(IPC_GET_DEVICE(*call),
    19341945                    IP_GET_GATEWAY(*call));
    19351946
     
    19401951                        return rc;
    19411952               
    1942                 rc = ip_get_route_req_local(0, IP_GET_PROTOCOL(*call), addr,
     1953                rc = ip_get_route_req_local(IP_GET_PROTOCOL(*call), addr,
    19431954                    (socklen_t) addrlen, &device_id, &header, &headerlen);
    19441955                if (rc != EOK)
     
    19711982       
    19721983        case NET_IP_SEND:
    1973                 rc = packet_translate_remote(ip_globals.net_phone, &packet,
     1984                rc = packet_translate_remote(ip_globals.net_sess, &packet,
    19741985                    IPC_GET_PACKET(*call));
    19751986                if (rc != EOK)
    19761987                        return rc;
    19771988               
    1978                 return ip_send_msg_local(0, IPC_GET_DEVICE(*call), packet, 0,
     1989                return ip_send_msg_local(IPC_GET_DEVICE(*call), packet, 0,
    19791990                    IPC_GET_ERROR(*call));
    19801991        }
Note: See TracChangeset for help on using the changeset viewer.