xref: /freebsd/sys/netinet/ip_id.c (revision 1f08c9479f919ae10ac122be258c5abe84f368ac)
164dddc18SKris Kennaway 
2c398230bSWarner Losh /*-
3361021ccSMike Silbersack  * Copyright (c) 2008 Michael J. Silbersack.
464dddc18SKris Kennaway  * All rights reserved.
564dddc18SKris Kennaway  *
664dddc18SKris Kennaway  * Redistribution and use in source and binary forms, with or without
764dddc18SKris Kennaway  * modification, are permitted provided that the following conditions
864dddc18SKris Kennaway  * are met:
964dddc18SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
10361021ccSMike Silbersack  *    notice unmodified, this list of conditions, and the following
11361021ccSMike Silbersack  *    disclaimer.
1264dddc18SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
1364dddc18SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
1464dddc18SKris Kennaway  *    documentation and/or other materials provided with the distribution.
1564dddc18SKris Kennaway  *
1664dddc18SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1764dddc18SKris Kennaway  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1864dddc18SKris Kennaway  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1964dddc18SKris Kennaway  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2064dddc18SKris Kennaway  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2164dddc18SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2264dddc18SKris Kennaway  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2364dddc18SKris Kennaway  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2464dddc18SKris Kennaway  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2564dddc18SKris Kennaway  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2664dddc18SKris Kennaway  */
2764dddc18SKris Kennaway 
284b421e2dSMike Silbersack #include <sys/cdefs.h>
294b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
304b421e2dSMike Silbersack 
31361021ccSMike Silbersack /*
32361021ccSMike Silbersack  * IP ID generation is a fascinating topic.
33361021ccSMike Silbersack  *
34361021ccSMike Silbersack  * In order to avoid ID collisions during packet reassembly, common sense
35361021ccSMike Silbersack  * dictates that the period between reuse of IDs be as large as possible.
36361021ccSMike Silbersack  * This leads to the classic implementation of a system-wide counter, thereby
37361021ccSMike Silbersack  * ensuring that IDs repeat only once every 2^16 packets.
38361021ccSMike Silbersack  *
39361021ccSMike Silbersack  * Subsequent security researchers have pointed out that using a global
40361021ccSMike Silbersack  * counter makes ID values predictable.  This predictability allows traffic
41361021ccSMike Silbersack  * analysis, idle scanning, and even packet injection in specific cases.
42361021ccSMike Silbersack  * These results suggest that IP IDs should be as random as possible.
43361021ccSMike Silbersack  *
44361021ccSMike Silbersack  * The "searchable queues" algorithm used in this IP ID implementation was
45361021ccSMike Silbersack  * proposed by Amit Klein.  It is a compromise between the above two
46361021ccSMike Silbersack  * viewpoints that has provable behavior that can be tuned to the user's
47361021ccSMike Silbersack  * requirements.
48361021ccSMike Silbersack  *
49361021ccSMike Silbersack  * The basic concept is that we supplement a standard random number generator
50361021ccSMike Silbersack  * with a queue of the last L IDs that we have handed out to ensure that all
51361021ccSMike Silbersack  * IDs have a period of at least L.
52361021ccSMike Silbersack  *
53361021ccSMike Silbersack  * To efficiently implement this idea, we keep two data structures: a
54361021ccSMike Silbersack  * circular array of IDs of size L and a bitstring of 65536 bits.
55361021ccSMike Silbersack  *
56361021ccSMike Silbersack  * To start, we ask the RNG for a new ID.  A quick index into the bitstring
57361021ccSMike Silbersack  * is used to determine if this is a recently used value.  The process is
58361021ccSMike Silbersack  * repeated until a value is returned that is not in the bitstring.
59361021ccSMike Silbersack  *
60361021ccSMike Silbersack  * Having found a usable ID, we remove the ID stored at the current position
61361021ccSMike Silbersack  * in the queue from the bitstring and replace it with our new ID.  Our new
62361021ccSMike Silbersack  * ID is then added to the bitstring and the queue pointer is incremented.
63361021ccSMike Silbersack  *
64361021ccSMike Silbersack  * The lower limit of 512 was chosen because there doesn't seem to be much
65361021ccSMike Silbersack  * point to having a smaller value.  The upper limit of 32768 was chosen for
66361021ccSMike Silbersack  * two reasons.  First, every step above 32768 decreases the entropy.  Taken
67361021ccSMike Silbersack  * to an extreme, 65533 would offer 1 bit of entropy.  Second, the number of
68361021ccSMike Silbersack  * attempts it takes the algorithm to find an unused ID drastically
69361021ccSMike Silbersack  * increases, killing performance.  The default value of 8192 was chosen
70361021ccSMike Silbersack  * because it provides a good tradeoff between randomness and non-repetition.
71361021ccSMike Silbersack  *
72361021ccSMike Silbersack  * With L=8192, the queue will use 16K of memory.  The bitstring always
73361021ccSMike Silbersack  * uses 8K of memory.  No memory is allocated until the use of random ids is
74361021ccSMike Silbersack  * enabled.
75361021ccSMike Silbersack  */
76361021ccSMike Silbersack 
77361021ccSMike Silbersack #include <sys/types.h>
78361021ccSMike Silbersack #include <sys/malloc.h>
7964dddc18SKris Kennaway #include <sys/param.h>
8064dddc18SKris Kennaway #include <sys/time.h>
8164dddc18SKris Kennaway #include <sys/kernel.h>
82361021ccSMike Silbersack #include <sys/libkern.h>
83361021ccSMike Silbersack #include <sys/lock.h>
84361021ccSMike Silbersack #include <sys/mutex.h>
8564dddc18SKris Kennaway #include <sys/random.h>
86361021ccSMike Silbersack #include <sys/systm.h>
87361021ccSMike Silbersack #include <sys/sysctl.h>
88361021ccSMike Silbersack #include <netinet/in.h>
89361021ccSMike Silbersack #include <netinet/ip_var.h>
90361021ccSMike Silbersack #include <sys/bitstring.h>
9164dddc18SKris Kennaway 
92361021ccSMike Silbersack static MALLOC_DEFINE(M_IPID, "ipid", "randomized ip id state");
9364dddc18SKris Kennaway 
94*1f08c947SGleb Smirnoff static uint16_t 	*id_array;
95*1f08c947SGleb Smirnoff static bitstr_t		*id_bits;
96*1f08c947SGleb Smirnoff static int		 array_ptr;
97*1f08c947SGleb Smirnoff static int		 array_size;
98*1f08c947SGleb Smirnoff static int		 random_id_collisions;
99*1f08c947SGleb Smirnoff static int		 random_id_total;
100f89d4c3aSAndre Oppermann static struct mtx	 ip_id_mtx;
10164dddc18SKris Kennaway 
102*1f08c947SGleb Smirnoff static void	ip_initid(int);
103361021ccSMike Silbersack static int	sysctl_ip_id_change(SYSCTL_HANDLER_ARGS);
104*1f08c947SGleb Smirnoff static void	ip_sysinitid(void);
10564dddc18SKris Kennaway 
1064b79449eSBjoern A. Zeeb SYSCTL_DECL(_net_inet_ip);
107361021ccSMike Silbersack SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id_period, CTLTYPE_INT|CTLFLAG_RW,
108361021ccSMike Silbersack     &array_size, 0, sysctl_ip_id_change, "IU", "IP ID Array size");
109361021ccSMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_collisions, CTLFLAG_RD,
110361021ccSMike Silbersack     &random_id_collisions, 0, "Count of IP ID collisions");
111361021ccSMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_total, CTLFLAG_RD,
112361021ccSMike Silbersack     &random_id_total, 0, "Count of IP IDs created");
113361021ccSMike Silbersack 
114361021ccSMike Silbersack static int
115361021ccSMike Silbersack sysctl_ip_id_change(SYSCTL_HANDLER_ARGS)
11664dddc18SKris Kennaway {
117361021ccSMike Silbersack 	int error, new;
11864dddc18SKris Kennaway 
119361021ccSMike Silbersack 	new = array_size;
120361021ccSMike Silbersack 	error = sysctl_handle_int(oidp, &new, 0, req);
121361021ccSMike Silbersack 	if (error == 0 && req->newptr) {
122*1f08c947SGleb Smirnoff 		if (new >= 512 && new <= 32768)
123*1f08c947SGleb Smirnoff 			ip_initid(new);
124*1f08c947SGleb Smirnoff 		else
125361021ccSMike Silbersack 			error = EINVAL;
12664dddc18SKris Kennaway 	}
127361021ccSMike Silbersack 	return (error);
12864dddc18SKris Kennaway }
12964dddc18SKris Kennaway 
13064dddc18SKris Kennaway static void
131*1f08c947SGleb Smirnoff ip_initid(int new_size)
13264dddc18SKris Kennaway {
133*1f08c947SGleb Smirnoff 	uint16_t *new_array;
134*1f08c947SGleb Smirnoff 	bitstr_t *new_bits;
13564dddc18SKris Kennaway 
136*1f08c947SGleb Smirnoff 	new_array = malloc(new_size * sizeof(uint16_t), M_IPID,
137*1f08c947SGleb Smirnoff 	    M_WAITOK | M_ZERO);
138*1f08c947SGleb Smirnoff 	new_bits = malloc(bitstr_size(65536), M_IPID, M_WAITOK | M_ZERO);
13964dddc18SKris Kennaway 
140361021ccSMike Silbersack 	mtx_lock(&ip_id_mtx);
141*1f08c947SGleb Smirnoff 	if (id_array != NULL) {
142*1f08c947SGleb Smirnoff 		free(id_array, M_IPID);
143*1f08c947SGleb Smirnoff 		free(id_bits, M_IPID);
144*1f08c947SGleb Smirnoff 	}
145*1f08c947SGleb Smirnoff 	id_array = new_array;
146*1f08c947SGleb Smirnoff 	id_bits = new_bits;
147*1f08c947SGleb Smirnoff 	array_size = new_size;
148*1f08c947SGleb Smirnoff 	array_ptr = 0;
149*1f08c947SGleb Smirnoff 	random_id_collisions = 0;
150*1f08c947SGleb Smirnoff 	random_id_total = 0;
151361021ccSMike Silbersack 	mtx_unlock(&ip_id_mtx);
152361021ccSMike Silbersack }
15364dddc18SKris Kennaway 
154*1f08c947SGleb Smirnoff uint16_t
155*1f08c947SGleb Smirnoff ip_randomid(void)
156*1f08c947SGleb Smirnoff {
157*1f08c947SGleb Smirnoff 	uint16_t new_id;
158*1f08c947SGleb Smirnoff 
159*1f08c947SGleb Smirnoff 	mtx_lock(&ip_id_mtx);
160361021ccSMike Silbersack 	/*
161361021ccSMike Silbersack 	 * To avoid a conflict with the zeros that the array is initially
162361021ccSMike Silbersack 	 * filled with, we never hand out an id of zero.
163361021ccSMike Silbersack 	 */
164361021ccSMike Silbersack 	new_id = 0;
165361021ccSMike Silbersack 	do {
166361021ccSMike Silbersack 		if (new_id != 0)
167361021ccSMike Silbersack 			random_id_collisions++;
168361021ccSMike Silbersack 		arc4rand(&new_id, sizeof(new_id), 0);
169361021ccSMike Silbersack 	} while (bit_test(id_bits, new_id) || new_id == 0);
170361021ccSMike Silbersack 	bit_clear(id_bits, id_array[array_ptr]);
171361021ccSMike Silbersack 	bit_set(id_bits, new_id);
172361021ccSMike Silbersack 	id_array[array_ptr] = new_id;
173361021ccSMike Silbersack 	array_ptr++;
174361021ccSMike Silbersack 	if (array_ptr == array_size)
175361021ccSMike Silbersack 		array_ptr = 0;
176361021ccSMike Silbersack 	random_id_total++;
177361021ccSMike Silbersack 	mtx_unlock(&ip_id_mtx);
178361021ccSMike Silbersack 	return (new_id);
17964dddc18SKris Kennaway }
180*1f08c947SGleb Smirnoff 
181*1f08c947SGleb Smirnoff static void
182*1f08c947SGleb Smirnoff ip_sysinitid(void)
183*1f08c947SGleb Smirnoff {
184*1f08c947SGleb Smirnoff 
185*1f08c947SGleb Smirnoff 	mtx_init(&ip_id_mtx, "ip_id_mtx", NULL, MTX_DEF);
186*1f08c947SGleb Smirnoff 	ip_initid(8192);
187*1f08c947SGleb Smirnoff }
188*1f08c947SGleb Smirnoff SYSINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ip_sysinitid, NULL);
189