1 /* $NetBSD: ip4_frag_1.c,v 1.2 2010/10/03 19:41:25 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software developed for The NetBSD Foundation
8 * by Mindaugas Rasiukevicius <rmind at NetBSD org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Test various cases of IPv4 reassembly.
34 */
35
36 #include <sys/cdefs.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stddef.h>
42 #include <unistd.h>
43 #include <netdb.h>
44 #include <assert.h>
45 #include <err.h>
46
47 #include <sys/socket.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/in.h>
50 #include <netinet/ip.h>
51 #include <netinet/udp.h>
52 #include <netinet/ip_icmp.h>
53 #include <arpa/inet.h>
54
55 #define IP_SIZE sizeof(struct ip)
56 #define ICMP_SIZE offsetof(struct icmp, icmp_ip)
57
58 static void *
xalloc(size_t sz)59 xalloc(size_t sz)
60 {
61 void *ptr = calloc(1, sz);
62
63 if (ptr == NULL) {
64 err(EXIT_FAILURE, "calloc");
65 }
66 return ptr;
67 }
68
69 static int
create_socket(void)70 create_socket(void)
71 {
72 int s, val = 1;
73
74 s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
75 if (s == -1) {
76 err(EXIT_FAILURE, "socket");
77 }
78 if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, &val, sizeof(val)) == -1) {
79 err(EXIT_FAILURE, "setsockopt");
80 }
81 return s;
82 }
83
84 void *
create_ip_packet(in_addr_t target,int foff,size_t addlen)85 create_ip_packet(in_addr_t target, int foff, size_t addlen)
86 {
87 struct ip *ip;
88 unsigned int n;
89 uint8_t *data;
90 int first;
91
92 /* Header at zero offset - first fragment. */
93 first = ((foff & IP_MF) == 0);
94 if (first) {
95 assert(addlen >= ICMP_SIZE);
96 addlen -= ICMP_SIZE;
97 }
98 ip = xalloc(IP_SIZE + addlen);
99
100 /*
101 * IP header.
102 * Note: ip_len and ip_off shall be in host byte order.
103 */
104 ip->ip_v = IPVERSION;
105 ip->ip_hl = 5;
106 ip->ip_tos = 0;
107 ip->ip_len = (5 << 2) + addlen;
108 ip->ip_id = htons(1);
109 ip->ip_off = foff;
110 ip->ip_ttl = 64;
111 ip->ip_p = IPPROTO_ICMP;
112 ip->ip_sum = 0;
113
114 ip->ip_src.s_addr = INADDR_ANY;
115 ip->ip_dst.s_addr = target;
116
117 /* Fill in some data. */
118 data = (void *)(ip + 1);
119 if (first) {
120 struct icmp *icmp = (void *)data;
121 icmp->icmp_type = ICMP_ECHO;
122 icmp->icmp_code = 0;
123 icmp->icmp_cksum = 0;
124 n = ICMP_SIZE;
125 } else {
126 n = 0;
127 }
128 for (data += n; n < addlen; n++) {
129 *data = (0xf & n);
130 data++;
131 }
132 return (void *)ip;
133 }
134
135 void
send_packet(int s,void * p,in_addr_t target)136 send_packet(int s, void *p, in_addr_t target)
137 {
138 struct ip *ip = (struct ip *)p;
139 struct sockaddr_in addr;
140 ssize_t ret;
141
142 memset(&addr, 0, sizeof(addr));
143 addr.sin_family = AF_INET;
144 addr.sin_addr.s_addr = target;
145
146 /* Note: total length was set host byte-order. */
147 ret = sendto(s, p, ip->ip_len, 0,
148 (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
149 if (ret == -1) {
150 err(EXIT_FAILURE, "sendto");
151 }
152 free(p);
153 }
154
155 void
test_case_0(int sock,in_addr_t target)156 test_case_0(int sock, in_addr_t target)
157 {
158 void *p;
159
160 /*
161 * Case 0: normal reassembly.
162 * => IP_STAT_FRAGMENTS
163 */
164
165 p = create_ip_packet(target, 0 | IP_MF, 1480);
166 send_packet(sock, p, target);
167
168 p = create_ip_packet(target, 185, 1480);
169 send_packet(sock, p, target);
170 }
171
172 void
test_case_1(int sock,in_addr_t target)173 test_case_1(int sock, in_addr_t target)
174 {
175 void *p;
176
177 /*
178 * Case 1: ip_len of the first fragment and ip_off + hlen of other
179 * fragments should be at least 68 (IP_MINFRAGSIZE - 1) bytes.
180 * => IP_STAT_BADFRAGS (malformed)
181 */
182
183 /* 20 + 48 == 68, OK */
184 p = create_ip_packet(target, 0 | IP_MF, 48);
185 send_packet(sock, p, target);
186
187 /* 20 + (5 * 8) = 60 < 68, ERR */
188 p = create_ip_packet(target, 6 - 1, 8);
189 send_packet(sock, p, target);
190 }
191
192 void
test_case_2(int sock,in_addr_t target)193 test_case_2(int sock, in_addr_t target)
194 {
195 void *p;
196 struct ip *ip;
197
198 /*
199 * Case 2: mismatch of TOS.
200 * => IP_STAT_BADFRAGS (malformed)
201 */
202
203 p = create_ip_packet(target, 0 | IP_MF, 48);
204 ip = (struct ip *)p;
205 ip->ip_tos = 123;
206 send_packet(sock, p, target);
207
208 p = create_ip_packet(target, 6, 48);
209 send_packet(sock, p, target);
210 }
211
212 void
test_case_3(int sock,in_addr_t target)213 test_case_3(int sock, in_addr_t target)
214 {
215 void *p;
216
217 /*
218 * Case 3: data length is not multiple of 8 bytes.
219 * => IP_STAT_BADFRAGS (malformed)
220 */
221
222 p = create_ip_packet(target, 0 | IP_MF, 48);
223 send_packet(sock, p, target);
224
225 /* (48 + 1) & 0x7 != 0, ERR */
226 p = create_ip_packet(target, 6, 48 + 1);
227 send_packet(sock, p, target);
228 }
229
230 void
test_case_4(int sock,in_addr_t target)231 test_case_4(int sock, in_addr_t target)
232 {
233 void *p;
234
235 /*
236 * Case 4: fragment covering previous (duplicate).
237 * => IP_STAT_FRAGDROPPED (dup, out of space)
238 */
239
240 p = create_ip_packet(target, 0 | IP_MF, 48);
241 send_packet(sock, p, target);
242
243 p = create_ip_packet(target, 6 | IP_MF, 96);
244 send_packet(sock, p, target);
245
246 p = create_ip_packet(target, 6, 48);
247 send_packet(sock, p, target);
248 }
249
250 void
test_case_5(int sock,in_addr_t target)251 test_case_5(int sock, in_addr_t target)
252 {
253 void *p;
254 int cnt, left, off;
255
256 /*
257 * Case 5: construct packet larger than IP_MAXPACKET.
258 * => IP_STAT_TOOLONG (with length > max ip packet size)
259 */
260
261 /*
262 * Assumptions:
263 * - 1500 MTU. Minus IP header - 1480.
264 * - Bytes left: 65535 - (1480 * 44) = 415.
265 */
266 cnt = IP_MAXPACKET / 1480;
267 left = IP_MAXPACKET - (1480 * cnt);
268 left = (left / 8) * 8;
269
270 for (off = 0; cnt != 0; cnt--) {
271 p = create_ip_packet(target, off | IP_MF, 1480);
272 send_packet(sock, p, target);
273 off += (1480 >> 3);
274 }
275 /* Add 8 bytes and thus cross IP_MAXPACKET limit. */
276 p = create_ip_packet(target, off, left + 8);
277 send_packet(sock, p, target);
278 }
279
280 void
test_case_6(int sock,in_addr_t target)281 test_case_6(int sock, in_addr_t target)
282 {
283 struct ip *ip;
284 void *p;
285
286 /*
287 * Case 6: cause a fragment timeout.
288 * => IP_STAT_FRAGTIMEOUT (fragments dropped after timeout)
289 */
290
291 p = create_ip_packet(target, 0 | IP_MF, 48);
292 ip = (struct ip *)p;
293 ip->ip_id = htons(321);
294 send_packet(sock, p, target);
295 /*
296 * Missing non-MF packet - timeout.
297 */
298 }
299
300 int
main(int argc,char ** argv)301 main(int argc, char **argv)
302 {
303 struct hostent *he;
304 in_addr_t target;
305 int sock;
306
307 if (argc < 2) {
308 printf("%s: <target host>\n", argv[0]);
309 exit(EXIT_SUCCESS);
310 }
311
312 he = gethostbyname(argv[1]);
313 if (he == NULL) {
314 err(EXIT_FAILURE, "gethostbyname");
315 }
316 memcpy(&target, he->h_addr, sizeof(target));
317
318 sock = create_socket();
319
320 test_case_0(sock, target);
321 test_case_1(sock, target);
322 test_case_2(sock, target);
323 test_case_3(sock, target);
324 test_case_4(sock, target);
325 test_case_5(sock, target);
326 test_case_6(sock, target);
327
328 close(sock);
329
330 return 0;
331 }
332