BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file ipv4_proto.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Definitions for the IPv4 protocol. |
||
32 | */ |
||
33 | |||
34 | #ifndef BADVPN_MISC_IPV4_PROTO_H |
||
35 | #define BADVPN_MISC_IPV4_PROTO_H |
||
36 | |||
37 | #include <stdint.h> |
||
38 | #include <string.h> |
||
39 | |||
40 | #include <misc/debug.h> |
||
41 | #include <misc/byteorder.h> |
||
42 | #include <misc/packed.h> |
||
43 | #include <misc/read_write_int.h> |
||
44 | |||
45 | #define IPV4_PROTOCOL_IGMP 2 |
||
46 | #define IPV4_PROTOCOL_UDP 17 |
||
47 | |||
48 | B_START_PACKED |
||
49 | struct ipv4_header { |
||
50 | uint8_t version4_ihl4; |
||
51 | uint8_t ds; |
||
52 | uint16_t total_length; |
||
53 | // |
||
54 | uint16_t identification; |
||
55 | uint16_t flags3_fragmentoffset13; |
||
56 | // |
||
57 | uint8_t ttl; |
||
58 | uint8_t protocol; |
||
59 | uint16_t checksum; |
||
60 | // |
||
61 | uint32_t source_address; |
||
62 | // |
||
63 | uint32_t destination_address; |
||
64 | } B_PACKED; |
||
65 | B_END_PACKED |
||
66 | |||
67 | #define IPV4_GET_VERSION(_header) (((_header).version4_ihl4&0xF0)>>4) |
||
68 | #define IPV4_GET_IHL(_header) (((_header).version4_ihl4&0x0F)>>0) |
||
69 | |||
70 | #define IPV4_MAKE_VERSION_IHL(size) (((size)/4) + (4 << 4)) |
||
71 | |||
72 | static uint16_t ipv4_checksum (const struct ipv4_header *header, const char *extra, uint16_t extra_len) |
||
73 | { |
||
74 | ASSERT(extra_len % 2 == 0) |
||
75 | ASSERT(extra_len == 0 || extra) |
||
76 | |||
77 | uint32_t t = 0; |
||
78 | |||
79 | for (uint16_t i = 0; i < sizeof(*header) / 2; i++) { |
||
80 | t += badvpn_read_be16((const char *)header + 2 * i); |
||
81 | } |
||
82 | |||
83 | for (uint16_t i = 0; i < extra_len / 2; i++) { |
||
84 | t += badvpn_read_be16((const char *)extra + 2 * i); |
||
85 | } |
||
86 | |||
87 | while (t >> 16) { |
||
88 | t = (t & 0xFFFF) + (t >> 16); |
||
89 | } |
||
90 | |||
91 | return hton16(~t); |
||
92 | } |
||
93 | |||
94 | static int ipv4_check (uint8_t *data, int data_len, struct ipv4_header *out_header, uint8_t **out_payload, int *out_payload_len) |
||
95 | { |
||
96 | ASSERT(data_len >= 0) |
||
97 | ASSERT(out_header) |
||
98 | ASSERT(out_payload) |
||
99 | ASSERT(out_payload_len) |
||
100 | |||
101 | // check base header |
||
102 | if (data_len < sizeof(struct ipv4_header)) { |
||
103 | return 0; |
||
104 | } |
||
105 | memcpy(out_header, data, sizeof(*out_header)); |
||
106 | |||
107 | // check version |
||
108 | if (IPV4_GET_VERSION(*out_header) != 4) { |
||
109 | return 0; |
||
110 | } |
||
111 | |||
112 | // check options |
||
113 | uint16_t header_len = IPV4_GET_IHL(*out_header) * 4; |
||
114 | if (header_len < sizeof(struct ipv4_header)) { |
||
115 | return 0; |
||
116 | } |
||
117 | if (header_len > data_len) { |
||
118 | return 0; |
||
119 | } |
||
120 | |||
121 | // check total length |
||
122 | uint16_t total_length = ntoh16(out_header->total_length); |
||
123 | if (total_length < header_len) { |
||
124 | return 0; |
||
125 | } |
||
126 | if (total_length > data_len) { |
||
127 | return 0; |
||
128 | } |
||
129 | |||
130 | // check checksum |
||
131 | uint16_t checksum_in_packet = out_header->checksum; |
||
132 | out_header->checksum = hton16(0); |
||
133 | uint16_t checksum_computed = ipv4_checksum(out_header, (char *)data + sizeof(*out_header), header_len - sizeof(*out_header)); |
||
134 | out_header->checksum = checksum_in_packet; |
||
135 | if (checksum_in_packet != checksum_computed) { |
||
136 | return 0; |
||
137 | } |
||
138 | |||
139 | *out_payload = data + header_len; |
||
140 | *out_payload_len = total_length - header_len; |
||
141 | |||
142 | return 1; |
||
143 | } |
||
144 | |||
145 | #endif |