Subversion Repositories HelenOS

Rev

Rev 4721 | Rev 4728 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4721 Rev 4722
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup ip
29
/** @addtogroup ip
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  IP client interface implementation.
34
 *  IP client interface implementation.
35
 *  @see ip_client.h
35
 *  @see ip_client.h
36
 */
36
 */
37
 
37
 
38
#include <errno.h>
38
#include <errno.h>
39
 
39
 
40
#include <sys/types.h>
40
#include <sys/types.h>
41
 
41
 
42
#include "../../include/ip_client.h"
42
#include "../../include/ip_client.h"
-
 
43
#include "../../include/socket_errno.h"
43
 
44
 
44
#include "../../structures/packet/packet.h"
45
#include "../../structures/packet/packet.h"
45
#include "../../structures/packet/packet_client.h"
46
#include "../../structures/packet/packet_client.h"
46
 
47
 
47
#include "ip_header.h"
48
#include "ip_header.h"
48
 
49
 
49
int ip_client_prepare_packet( packet_t packet, ip_protocol_t protocol, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, size_t ipopt_length ){
50
int ip_client_prepare_packet( packet_t packet, ip_protocol_t protocol, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, size_t ipopt_length ){
50
    ip_header_ref   header;
51
    ip_header_ref   header;
51
    uint8_t *       data;
52
    uint8_t *       data;
52
    size_t          padding;
53
    size_t          padding;
53
 
54
 
54
    padding =  ipopt_length % 4;
55
    padding =  ipopt_length % 4;
55
    if( padding ){
56
    if( padding ){
56
        padding = 4 - padding;
57
        padding = 4 - padding;
57
        ipopt_length += padding;
58
        ipopt_length += padding;
58
    }
59
    }
59
    data = ( uint8_t * ) packet_prefix( packet, sizeof( ip_header_t ) + padding );
60
    data = ( uint8_t * ) packet_prefix( packet, sizeof( ip_header_t ) + padding );
60
    if( ! data ) return ENOMEM;
61
    if( ! data ) return ENOMEM;
61
    while( padding -- ) data[ sizeof( ip_header_t ) + padding ] = IPOPT_NOOP;
62
    while( padding -- ) data[ sizeof( ip_header_t ) + padding ] = IPOPT_NOOP;
62
    header = ( ip_header_ref ) data;
63
    header = ( ip_header_ref ) data;
63
    header->ihl = ( uint8_t ) ( sizeof( ip_header_t ) + ipopt_length ) / 4;
64
    header->ihl = ( uint8_t ) ( sizeof( ip_header_t ) + ipopt_length ) / 4;
64
    header->ttl = ttl ? (( ttl <= MAXTTL ) ? ttl : MAXTTL ) : IPDEFTTL;
65
    header->ttl = ttl ? (( ttl <= MAXTTL ) ? ttl : MAXTTL ) : IPDEFTTL;
65
    header->tos = tos;
66
    header->tos = tos;
66
    header->protocol = protocol;
67
    header->protocol = protocol;
67
    if( dont_fragment ) header->flags = IPFLAG_DONT_FRAGMENT;
68
    if( dont_fragment ) header->flags = IPFLAG_DONT_FRAGMENT;
68
    return EOK;
69
    return EOK;
69
}
70
}
70
 
71
 
71
int ip_client_process_packet( packet_t packet, ip_protocol_t * protocol, ip_ttl_t * ttl, ip_tos_t * tos, int * dont_fragment, size_t * ipopt_length ){
72
int ip_client_process_packet( packet_t packet, ip_protocol_t * protocol, ip_ttl_t * ttl, ip_tos_t * tos, int * dont_fragment, size_t * ipopt_length ){
72
    ip_header_ref   header;
73
    ip_header_ref   header;
73
 
74
 
74
    header = ( ip_header_ref ) packet_get_data( packet );
75
    header = ( ip_header_ref ) packet_get_data( packet );
75
    if(( ! header )
76
    if(( ! header )
76
    || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){
77
    || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){
77
        return ENOMEM;
78
        return ENOMEM;
78
    }
79
    }
79
    if( protocol ) * protocol = header->protocol;
80
    if( protocol ) * protocol = header->protocol;
80
    if( ttl ) * ttl = header->ttl;
81
    if( ttl ) * ttl = header->ttl;
81
    if( tos ) * tos = header->tos;
82
    if( tos ) * tos = header->tos;
82
    if( dont_fragment ) * dont_fragment = header->flags & IPFLAG_DONT_FRAGMENT;
83
    if( dont_fragment ) * dont_fragment = header->flags & IPFLAG_DONT_FRAGMENT;
83
    if( ipopt_length ){
84
    if( ipopt_length ){
84
        * ipopt_length = header->ihl * 4 - sizeof( ip_header_t );
85
        * ipopt_length = header->ihl * 4 - sizeof( ip_header_t );
85
        return sizeof( ip_header_t );
86
        return sizeof( ip_header_t );
86
    }else{
87
    }else{
87
        return header->ihl * 4;
88
        return header->ihl * 4;
88
    }
89
    }
89
}
90
}
90
 
91
 
91
size_t ip_client_header_length( packet_t packet ){
92
size_t ip_client_header_length( packet_t packet ){
92
    ip_header_ref   header;
93
    ip_header_ref   header;
93
 
94
 
94
    header = ( ip_header_ref ) packet_get_data( packet );
95
    header = ( ip_header_ref ) packet_get_data( packet );
95
    if(( ! header )
96
    if(( ! header )
96
    || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){
97
    || ( packet_get_data_length( packet ) < sizeof( ip_header_t ))){
97
        return 0;
98
        return 0;
98
    }
99
    }
99
    return header->ihl * 4u;
100
    return header->ihl * 4u;
100
}
101
}
-
 
102
 
-
 
103
int ip_client_set_pseudo_header_data_length( ip_pseudo_header_ref header, size_t headerlen, size_t data_length ){
-
 
104
    ipv4_pseudo_header_ref  header_in;
-
 
105
 
-
 
106
    if( headerlen == sizeof( ipv4_pseudo_header_t )){
-
 
107
        header_in = ( ipv4_pseudo_header_ref ) header;
-
 
108
        header_in->data_length = htons( data_length );
-
 
109
        return EOK;
-
 
110
    }else{
-
 
111
        return EINVAL;
-
 
112
    }
-
 
113
}
-
 
114
 
-
 
115
int ip_client_get_pseudo_header( ip_protocol_t protocol, struct sockaddr * src, socklen_t srclen, struct sockaddr * dest, socklen_t destlen, size_t data_length, ip_pseudo_header_ref * header, size_t * headerlen ){
-
 
116
    ipv4_pseudo_header_ref  header_in;
-
 
117
    struct sockaddr_in *    address_in;
-
 
118
 
-
 
119
    if( !( header && headerlen )) return EBADMEM;
-
 
120
    if( !( src && dest && ( srclen >= sizeof( struct sockaddr )) && ( srclen == destlen ) && ( src->sa_family == dest->sa_family ))) return EINVAL;
-
 
121
    switch( src->sa_family ){
-
 
122
        case AF_INET:
-
 
123
            if( srclen != sizeof( struct sockaddr_in )) return EINVAL;
-
 
124
            * headerlen = sizeof( * header_in );
-
 
125
            header_in = ( ipv4_pseudo_header_ref ) malloc( * headerlen );
-
 
126
            if( ! header_in ) return ENOMEM;
-
 
127
            bzero( header_in, * headerlen );
-
 
128
            address_in = ( struct sockaddr_in * ) dest;
-
 
129
            header_in->destination_address = address_in->sin_addr.s_addr;
-
 
130
            address_in = ( struct sockaddr_in * ) src;
-
 
131
            header_in->source_address = address_in->sin_addr.s_addr;
-
 
132
            header_in->protocol = protocol;
-
 
133
            header_in->data_length = htons( data_length );
-
 
134
            * header = ( ip_pseudo_header_ref ) header_in;
-
 
135
            return EOK;
-
 
136
        // TODO IPv6
-
 
137
/*      case AF_INET6:
-
 
138
            if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
-
 
139
            address_in6 = ( struct sockaddr_in6 * ) addr;
-
 
140
            return EOK;
-
 
141
*/      default:
-
 
142
            return EAFNOSUPPORT;
-
 
143
    }
-
 
144
}
101
 
145
 
102
/** @}
146
/** @}
103
 */
147
 */
104
 
148