BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file udp_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 UDP protocol.
32 */
33  
34 #ifndef BADVPN_MISC_UDP_PROTO_H
35 #define BADVPN_MISC_UDP_PROTO_H
36  
37 #include <stdint.h>
38  
39 #include <misc/debug.h>
40 #include <misc/byteorder.h>
41 #include <misc/ipv4_proto.h>
42 #include <misc/ipv6_proto.h>
43 #include <misc/read_write_int.h>
44  
45 B_START_PACKED
46 struct udp_header {
47 uint16_t source_port;
48 uint16_t dest_port;
49 uint16_t length;
50 uint16_t checksum;
51 } B_PACKED;
52 B_END_PACKED
53  
54 static uint32_t udp_checksum_summer (const char *data, uint16_t len)
55 {
56 ASSERT(len % 2 == 0)
57  
58 uint32_t t = 0;
59  
60 for (uint16_t i = 0; i < len / 2; i++) {
61 t += badvpn_read_be16(data + 2 * i);
62 }
63  
64 return t;
65 }
66  
67 static uint16_t udp_checksum (const struct udp_header *header, const uint8_t *payload, uint16_t payload_len, uint32_t source_addr, uint32_t dest_addr)
68 {
69 uint32_t t = 0;
70  
71 t += udp_checksum_summer((char *)&source_addr, sizeof(source_addr));
72 t += udp_checksum_summer((char *)&dest_addr, sizeof(dest_addr));
73  
74 uint16_t x;
75 x = hton16(IPV4_PROTOCOL_UDP);
76 t += udp_checksum_summer((char *)&x, sizeof(x));
77 x = hton16(sizeof(*header) + payload_len);
78 t += udp_checksum_summer((char *)&x, sizeof(x));
79  
80 t += udp_checksum_summer((const char *)header, sizeof(*header));
81  
82 if (payload_len % 2 == 0) {
83 t += udp_checksum_summer((const char *)payload, payload_len);
84 } else {
85 t += udp_checksum_summer((const char *)payload, payload_len - 1);
86  
87 x = hton16(((uint16_t)payload[payload_len - 1]) << 8);
88 t += udp_checksum_summer((char *)&x, sizeof(x));
89 }
90  
91 while (t >> 16) {
92 t = (t & 0xFFFF) + (t >> 16);
93 }
94  
95 t = ~t;
96  
97 if (t == 0) {
98 t = UINT16_MAX;
99 }
100  
101 return hton16(t);
102 }
103  
104 static uint16_t udp_ip6_checksum (const struct udp_header *header, const uint8_t *payload, uint16_t payload_len, const uint8_t *source_addr, const uint8_t *dest_addr)
105 {
106 uint32_t t = 0;
107  
108 t += udp_checksum_summer((const char *)source_addr, 16);
109 t += udp_checksum_summer((const char *)dest_addr, 16);
110  
111 uint32_t x;
112 x = hton32(sizeof(*header) + payload_len);
113 t += udp_checksum_summer((char *)&x, sizeof(x));
114 x = hton32(IPV6_NEXT_UDP);
115 t += udp_checksum_summer((char *)&x, sizeof(x));
116  
117 t += udp_checksum_summer((const char *)header, sizeof(*header));
118  
119 if (payload_len % 2 == 0) {
120 t += udp_checksum_summer((const char *)payload, payload_len);
121 } else {
122 t += udp_checksum_summer((const char *)payload, payload_len - 1);
123  
124 uint16_t y;
125 y = hton16(((uint16_t)payload[payload_len - 1]) << 8);
126 t += udp_checksum_summer((char *)&y, sizeof(y));
127 }
128  
129 while (t >> 16) {
130 t = (t & 0xFFFF) + (t >> 16);
131 }
132  
133 t = ~t;
134  
135 if (t == 0) {
136 t = UINT16_MAX;
137 }
138  
139 return hton16(t);
140 }
141  
142 static int udp_check (const uint8_t *data, int data_len, struct udp_header *out_header, uint8_t **out_payload, int *out_payload_len)
143 {
144 ASSERT(data_len >= 0)
145 ASSERT(out_header)
146 ASSERT(out_payload)
147 ASSERT(out_payload_len)
148  
149 // parse UDP header
150 if (data_len < sizeof(struct udp_header)) {
151 return 0;
152 }
153 memcpy(out_header, data, sizeof(*out_header));
154 data += sizeof(*out_header);
155 data_len -= sizeof(*out_header);
156  
157 // verify UDP payload
158 int udp_length = ntoh16(out_header->length);
159 if (udp_length < sizeof(*out_header)) {
160 return 0;
161 }
162 if (udp_length > sizeof(*out_header) + data_len) {
163 return 0;
164 }
165  
166 // ignore stray data
167 data_len = udp_length - sizeof(*out_header);
168  
169 *out_payload = (uint8_t *)data;
170 *out_payload_len = data_len;
171 return 1;
172 }
173  
174 #endif