Home | History | Annotate | Line # | Download | only in frag
ip4_frag_1.c revision 1.1
      1 /*	$NetBSD: ip4_frag_1.c,v 1.1 2010/07/13 22:13:18 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 *
     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
     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 *
     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[n] = (0xf & n);
    130 	}
    131 	return (void *)ip;
    132 }
    133 
    134 void
    135 send_packet(int s, void *p, in_addr_t target)
    136 {
    137 	struct ip *ip = (struct ip *)p;
    138 	struct sockaddr_in addr;
    139 	ssize_t ret;
    140 
    141 	memset(&addr, 0, sizeof(addr));
    142 	addr.sin_family = AF_INET;
    143 	addr.sin_addr.s_addr = target;
    144 
    145 	/* Note: total length was set host byte-order. */
    146 	ret = sendto(s, p, ip->ip_len, 0,
    147 	    (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
    148 	if (ret == -1) {
    149 		err(EXIT_FAILURE, "sendto");
    150 	}
    151 	free(p);
    152 }
    153 
    154 void
    155 test_case_0(int sock, in_addr_t target)
    156 {
    157 	void *p;
    158 
    159 	/*
    160 	 * Case 0: normal reassembly.
    161 	 * => IP_STAT_FRAGMENTS
    162 	 */
    163 
    164 	p = create_ip_packet(target, 0 | IP_MF, 1480);
    165 	send_packet(sock, p, target);
    166 
    167 	p = create_ip_packet(target, 185, 1480);
    168 	send_packet(sock, p, target);
    169 }
    170 
    171 void
    172 test_case_1(int sock, in_addr_t target)
    173 {
    174 	void *p;
    175 
    176 	/*
    177 	 * Case 1: ip_len of the first fragment and ip_off + hlen of other
    178 	 * fragments should be at least 68 (IP_MINFRAGSIZE - 1) bytes.
    179 	 * => IP_STAT_BADFRAGS (malformed)
    180 	 */
    181 
    182 	/* 20 + 48 == 68, OK */
    183 	p = create_ip_packet(target, 0 | IP_MF, 48);
    184 	send_packet(sock, p, target);
    185 
    186 	/* 20 + (5 * 8) = 60 < 68, ERR */
    187 	p = create_ip_packet(target, 6 - 1, 8);
    188 	send_packet(sock, p, target);
    189 }
    190 
    191 void
    192 test_case_2(int sock, in_addr_t target)
    193 {
    194 	void *p;
    195 	struct ip *ip;
    196 
    197 	/*
    198 	 * Case 2: mismatch of TOS.
    199 	 * => IP_STAT_BADFRAGS (malformed)
    200 	 */
    201 
    202 	p = create_ip_packet(target, 0 | IP_MF, 48);
    203 	ip = (struct ip *)p;
    204 	ip->ip_tos = 123;
    205 	send_packet(sock, p, target);
    206 
    207 	p = create_ip_packet(target, 6, 48);
    208 	send_packet(sock, p, target);
    209 }
    210 
    211 void
    212 test_case_3(int sock, in_addr_t target)
    213 {
    214 	void *p;
    215 
    216 	/*
    217 	 * Case 3: data length is not multiple of 8 bytes.
    218 	 * => IP_STAT_BADFRAGS (malformed)
    219 	 */
    220 
    221 	p = create_ip_packet(target, 0 | IP_MF, 48);
    222 	send_packet(sock, p, target);
    223 
    224 	/* (48 + 1) & 0x7 != 0, ERR */
    225 	p = create_ip_packet(target, 6, 48 + 1);
    226 	send_packet(sock, p, target);
    227 }
    228 
    229 void
    230 test_case_4(int sock, in_addr_t target)
    231 {
    232 	void *p;
    233 
    234 	/*
    235 	 * Case 4: fragment covering previous (duplicate).
    236 	 * => IP_STAT_FRAGDROPPED (dup, out of space)
    237 	 */
    238 
    239 	p = create_ip_packet(target, 0 | IP_MF, 48);
    240 	send_packet(sock, p, target);
    241 
    242 	p = create_ip_packet(target, 6 | IP_MF, 96);
    243 	send_packet(sock, p, target);
    244 
    245 	p = create_ip_packet(target, 6, 48);
    246 	send_packet(sock, p, target);
    247 }
    248 
    249 void
    250 test_case_5(int sock, in_addr_t target)
    251 {
    252 	void *p;
    253 	int cnt, left, off;
    254 
    255 	/*
    256 	 * Case 5: construct packet larger than IP_MAXPACKET.
    257 	 * => IP_STAT_TOOLONG (with length > max ip packet size)
    258 	 */
    259 
    260 	/*
    261 	 * Assumptions:
    262 	 * - 1500 MTU.  Minus IP header - 1480.
    263 	 * - Bytes left: 65535 - (1480 * 44) = 415.
    264 	 */
    265 	cnt = IP_MAXPACKET / 1480;
    266 	left = IP_MAXPACKET - (1480 * cnt);
    267 	left = (left / 8) * 8;
    268 
    269 	for (off = 0; cnt != 0; cnt--) {
    270 		p = create_ip_packet(target, off | IP_MF, 1480);
    271 		send_packet(sock, p, target);
    272 		off += (1480 >> 3);
    273 	}
    274 	/* Add 8 bytes and thus cross IP_MAXPACKET limit. */
    275 	p = create_ip_packet(target, off, left + 8);
    276 	send_packet(sock, p, target);
    277 }
    278 
    279 void
    280 test_case_6(int sock, in_addr_t target)
    281 {
    282 	struct ip *ip;
    283 	void *p;
    284 
    285 	/*
    286 	 * Case 6: cause a fragment timeout.
    287 	 * => IP_STAT_FRAGTIMEOUT (fragments dropped after timeout)
    288 	 */
    289 
    290 	p = create_ip_packet(target, 0 | IP_MF, 48);
    291 	ip = (struct ip *)p;
    292 	ip->ip_id = htons(321);
    293 	send_packet(sock, p, target);
    294 	/*
    295 	 * Missing non-MF packet - timeout.
    296 	 */
    297 }
    298 
    299 int
    300 main(int argc, char **argv)
    301 {
    302 	struct hostent *he;
    303 	in_addr_t target;
    304 	int sock;
    305 
    306 	if (argc < 2) {
    307 		printf("%s: <target host>\n", argv[0]);
    308 		exit(EXIT_SUCCESS);
    309 	}
    310 
    311 	he = gethostbyname(argv[1]);
    312 	if (he == NULL) {
    313 		err(EXIT_FAILURE, "gethostbyname");
    314 	}
    315 	memcpy(&target, he->h_addr, sizeof(target));
    316 
    317 	sock = create_socket();
    318 
    319 	test_case_0(sock, target);
    320 	test_case_1(sock, target);
    321 	test_case_2(sock, target);
    322 	test_case_3(sock, target);
    323 	test_case_4(sock, target);
    324 	test_case_5(sock, target);
    325 	test_case_6(sock, target);
    326 
    327 	close(sock);
    328 
    329 	return 0;
    330 }
    331