xref: /freebsd/sys/netinet/ip_id.c (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
1c398230bSWarner Losh /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
4361021ccSMike Silbersack  * Copyright (c) 2008 Michael J. Silbersack.
564dddc18SKris Kennaway  * All rights reserved.
664dddc18SKris Kennaway  *
764dddc18SKris Kennaway  * Redistribution and use in source and binary forms, with or without
864dddc18SKris Kennaway  * modification, are permitted provided that the following conditions
964dddc18SKris Kennaway  * are met:
1064dddc18SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
11361021ccSMike Silbersack  *    notice unmodified, this list of conditions, and the following
12361021ccSMike Silbersack  *    disclaimer.
1364dddc18SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
1464dddc18SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
1564dddc18SKris Kennaway  *    documentation and/or other materials provided with the distribution.
1664dddc18SKris Kennaway  *
1764dddc18SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1864dddc18SKris Kennaway  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1964dddc18SKris Kennaway  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2064dddc18SKris Kennaway  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2164dddc18SKris Kennaway  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2264dddc18SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2364dddc18SKris Kennaway  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2464dddc18SKris Kennaway  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2564dddc18SKris Kennaway  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2664dddc18SKris Kennaway  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2764dddc18SKris Kennaway  */
2864dddc18SKris Kennaway 
294b421e2dSMike Silbersack #include <sys/cdefs.h>
304b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
314b421e2dSMike Silbersack 
32361021ccSMike Silbersack /*
33361021ccSMike Silbersack  * IP ID generation is a fascinating topic.
34361021ccSMike Silbersack  *
35361021ccSMike Silbersack  * In order to avoid ID collisions during packet reassembly, common sense
36361021ccSMike Silbersack  * dictates that the period between reuse of IDs be as large as possible.
37361021ccSMike Silbersack  * This leads to the classic implementation of a system-wide counter, thereby
38361021ccSMike Silbersack  * ensuring that IDs repeat only once every 2^16 packets.
39361021ccSMike Silbersack  *
40361021ccSMike Silbersack  * Subsequent security researchers have pointed out that using a global
41361021ccSMike Silbersack  * counter makes ID values predictable.  This predictability allows traffic
42361021ccSMike Silbersack  * analysis, idle scanning, and even packet injection in specific cases.
43361021ccSMike Silbersack  * These results suggest that IP IDs should be as random as possible.
44361021ccSMike Silbersack  *
45361021ccSMike Silbersack  * The "searchable queues" algorithm used in this IP ID implementation was
46361021ccSMike Silbersack  * proposed by Amit Klein.  It is a compromise between the above two
47361021ccSMike Silbersack  * viewpoints that has provable behavior that can be tuned to the user's
48361021ccSMike Silbersack  * requirements.
49361021ccSMike Silbersack  *
50361021ccSMike Silbersack  * The basic concept is that we supplement a standard random number generator
51361021ccSMike Silbersack  * with a queue of the last L IDs that we have handed out to ensure that all
52361021ccSMike Silbersack  * IDs have a period of at least L.
53361021ccSMike Silbersack  *
54361021ccSMike Silbersack  * To efficiently implement this idea, we keep two data structures: a
55361021ccSMike Silbersack  * circular array of IDs of size L and a bitstring of 65536 bits.
56361021ccSMike Silbersack  *
57361021ccSMike Silbersack  * To start, we ask the RNG for a new ID.  A quick index into the bitstring
58361021ccSMike Silbersack  * is used to determine if this is a recently used value.  The process is
59361021ccSMike Silbersack  * repeated until a value is returned that is not in the bitstring.
60361021ccSMike Silbersack  *
61361021ccSMike Silbersack  * Having found a usable ID, we remove the ID stored at the current position
62361021ccSMike Silbersack  * in the queue from the bitstring and replace it with our new ID.  Our new
63361021ccSMike Silbersack  * ID is then added to the bitstring and the queue pointer is incremented.
64361021ccSMike Silbersack  *
65361021ccSMike Silbersack  * The lower limit of 512 was chosen because there doesn't seem to be much
66361021ccSMike Silbersack  * point to having a smaller value.  The upper limit of 32768 was chosen for
67361021ccSMike Silbersack  * two reasons.  First, every step above 32768 decreases the entropy.  Taken
68361021ccSMike Silbersack  * to an extreme, 65533 would offer 1 bit of entropy.  Second, the number of
69361021ccSMike Silbersack  * attempts it takes the algorithm to find an unused ID drastically
70361021ccSMike Silbersack  * increases, killing performance.  The default value of 8192 was chosen
71361021ccSMike Silbersack  * because it provides a good tradeoff between randomness and non-repetition.
72361021ccSMike Silbersack  *
73361021ccSMike Silbersack  * With L=8192, the queue will use 16K of memory.  The bitstring always
74361021ccSMike Silbersack  * uses 8K of memory.  No memory is allocated until the use of random ids is
75361021ccSMike Silbersack  * enabled.
76361021ccSMike Silbersack  */
77361021ccSMike Silbersack 
7864dddc18SKris Kennaway #include <sys/param.h>
796d947416SGleb Smirnoff #include <sys/systm.h>
806d947416SGleb Smirnoff #include <sys/counter.h>
811d549750SBjoern A. Zeeb #include <sys/kernel.h>
826d947416SGleb Smirnoff #include <sys/malloc.h>
83361021ccSMike Silbersack #include <sys/lock.h>
84361021ccSMike Silbersack #include <sys/mutex.h>
8564dddc18SKris Kennaway #include <sys/random.h>
866d947416SGleb Smirnoff #include <sys/smp.h>
87361021ccSMike Silbersack #include <sys/sysctl.h>
88513635bfSGleb Smirnoff #include <sys/bitstring.h>
89513635bfSGleb Smirnoff 
90513635bfSGleb Smirnoff #include <net/vnet.h>
91513635bfSGleb Smirnoff 
92361021ccSMike Silbersack #include <netinet/in.h>
936d947416SGleb Smirnoff #include <netinet/ip.h>
94361021ccSMike Silbersack #include <netinet/ip_var.h>
9564dddc18SKris Kennaway 
966d947416SGleb Smirnoff /*
976d947416SGleb Smirnoff  * By default we generate IP ID only for non-atomic datagrams, as
986d947416SGleb Smirnoff  * suggested by RFC6864.  We use per-CPU counter for that, or if
996d947416SGleb Smirnoff  * user wants to, we can turn on random ID generation.
1006d947416SGleb Smirnoff  */
1015f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip_rfc6864) = 1;
1025f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip_do_randomid) = 0;
1036d947416SGleb Smirnoff #define	V_ip_rfc6864		VNET(ip_rfc6864)
1046d947416SGleb Smirnoff #define	V_ip_do_randomid	VNET(ip_do_randomid)
10564dddc18SKris Kennaway 
1066d947416SGleb Smirnoff /*
1076d947416SGleb Smirnoff  * Random ID state engine.
1086d947416SGleb Smirnoff  */
1096d947416SGleb Smirnoff static MALLOC_DEFINE(M_IPID, "ipid", "randomized ip id state");
1105f901c92SAndrew Turner VNET_DEFINE_STATIC(uint16_t *, id_array);
1115f901c92SAndrew Turner VNET_DEFINE_STATIC(bitstr_t *, id_bits);
1125f901c92SAndrew Turner VNET_DEFINE_STATIC(int, array_ptr);
1135f901c92SAndrew Turner VNET_DEFINE_STATIC(int, array_size);
1145f901c92SAndrew Turner VNET_DEFINE_STATIC(int, random_id_collisions);
1155f901c92SAndrew Turner VNET_DEFINE_STATIC(int, random_id_total);
1165f901c92SAndrew Turner VNET_DEFINE_STATIC(struct mtx, ip_id_mtx);
117513635bfSGleb Smirnoff #define	V_id_array	VNET(id_array)
118513635bfSGleb Smirnoff #define	V_id_bits	VNET(id_bits)
119513635bfSGleb Smirnoff #define	V_array_ptr	VNET(array_ptr)
120513635bfSGleb Smirnoff #define	V_array_size	VNET(array_size)
121513635bfSGleb Smirnoff #define	V_random_id_collisions	VNET(random_id_collisions)
122513635bfSGleb Smirnoff #define	V_random_id_total	VNET(random_id_total)
123513635bfSGleb Smirnoff #define	V_ip_id_mtx	VNET(ip_id_mtx)
12464dddc18SKris Kennaway 
1256d947416SGleb Smirnoff /*
1266d947416SGleb Smirnoff  * Non-random ID state engine is simply a per-cpu counter.
1276d947416SGleb Smirnoff  */
1285f901c92SAndrew Turner VNET_DEFINE_STATIC(counter_u64_t, ip_id);
1296d947416SGleb Smirnoff #define	V_ip_id		VNET(ip_id)
1306d947416SGleb Smirnoff 
1316d947416SGleb Smirnoff static int	sysctl_ip_randomid(SYSCTL_HANDLER_ARGS);
132361021ccSMike Silbersack static int	sysctl_ip_id_change(SYSCTL_HANDLER_ARGS);
1336d947416SGleb Smirnoff static void	ip_initid(int);
1346d947416SGleb Smirnoff static uint16_t ip_randomid(void);
135513635bfSGleb Smirnoff static void	ipid_sysinit(void);
136513635bfSGleb Smirnoff static void	ipid_sysuninit(void);
13764dddc18SKris Kennaway 
1384b79449eSBjoern A. Zeeb SYSCTL_DECL(_net_inet_ip);
1396d947416SGleb Smirnoff SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id,
140*7029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_MPSAFE,
1416d947416SGleb Smirnoff     &VNET_NAME(ip_do_randomid), 0, sysctl_ip_randomid, "IU",
1426d947416SGleb Smirnoff     "Assign random ip_id values");
1436d947416SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, rfc6864, CTLFLAG_VNET | CTLFLAG_RW,
1446d947416SGleb Smirnoff     &VNET_NAME(ip_rfc6864), 0,
1456d947416SGleb Smirnoff     "Use constant IP ID for atomic datagrams");
146513635bfSGleb Smirnoff SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id_period,
147*7029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET | CTLFLAG_MPSAFE,
148513635bfSGleb Smirnoff     &VNET_NAME(array_size), 0, sysctl_ip_id_change, "IU", "IP ID Array size");
149513635bfSGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_collisions,
150513635bfSGleb Smirnoff     CTLFLAG_RD | CTLFLAG_VNET,
151513635bfSGleb Smirnoff     &VNET_NAME(random_id_collisions), 0, "Count of IP ID collisions");
152513635bfSGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_total, CTLFLAG_RD | CTLFLAG_VNET,
153513635bfSGleb Smirnoff     &VNET_NAME(random_id_total), 0, "Count of IP IDs created");
154361021ccSMike Silbersack 
155361021ccSMike Silbersack static int
1566d947416SGleb Smirnoff sysctl_ip_randomid(SYSCTL_HANDLER_ARGS)
1576d947416SGleb Smirnoff {
1586d947416SGleb Smirnoff 	int error, new;
1596d947416SGleb Smirnoff 
1606d947416SGleb Smirnoff 	new = V_ip_do_randomid;
1616d947416SGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
1626d947416SGleb Smirnoff 	if (error || req->newptr == NULL)
1636d947416SGleb Smirnoff 		return (error);
1646d947416SGleb Smirnoff 	if (new != 0 && new != 1)
1656d947416SGleb Smirnoff 		return (EINVAL);
1666d947416SGleb Smirnoff 	if (new == V_ip_do_randomid)
1676d947416SGleb Smirnoff 		return (0);
1686d947416SGleb Smirnoff 	if (new == 1 && V_ip_do_randomid == 0)
1696d947416SGleb Smirnoff 		ip_initid(8192);
1706d947416SGleb Smirnoff 	/* We don't free memory when turning random ID off, due to race. */
1716d947416SGleb Smirnoff 	V_ip_do_randomid = new;
1726d947416SGleb Smirnoff 	return (0);
1736d947416SGleb Smirnoff }
1746d947416SGleb Smirnoff 
1756d947416SGleb Smirnoff static int
176361021ccSMike Silbersack sysctl_ip_id_change(SYSCTL_HANDLER_ARGS)
17764dddc18SKris Kennaway {
178361021ccSMike Silbersack 	int error, new;
17964dddc18SKris Kennaway 
180513635bfSGleb Smirnoff 	new = V_array_size;
181361021ccSMike Silbersack 	error = sysctl_handle_int(oidp, &new, 0, req);
182361021ccSMike Silbersack 	if (error == 0 && req->newptr) {
1831f08c947SGleb Smirnoff 		if (new >= 512 && new <= 32768)
1841f08c947SGleb Smirnoff 			ip_initid(new);
1851f08c947SGleb Smirnoff 		else
186361021ccSMike Silbersack 			error = EINVAL;
18764dddc18SKris Kennaway 	}
188361021ccSMike Silbersack 	return (error);
18964dddc18SKris Kennaway }
19064dddc18SKris Kennaway 
19164dddc18SKris Kennaway static void
1921f08c947SGleb Smirnoff ip_initid(int new_size)
19364dddc18SKris Kennaway {
1941f08c947SGleb Smirnoff 	uint16_t *new_array;
1951f08c947SGleb Smirnoff 	bitstr_t *new_bits;
19664dddc18SKris Kennaway 
1971f08c947SGleb Smirnoff 	new_array = malloc(new_size * sizeof(uint16_t), M_IPID,
1981f08c947SGleb Smirnoff 	    M_WAITOK | M_ZERO);
1991f08c947SGleb Smirnoff 	new_bits = malloc(bitstr_size(65536), M_IPID, M_WAITOK | M_ZERO);
20064dddc18SKris Kennaway 
201513635bfSGleb Smirnoff 	mtx_lock(&V_ip_id_mtx);
202513635bfSGleb Smirnoff 	if (V_id_array != NULL) {
203513635bfSGleb Smirnoff 		free(V_id_array, M_IPID);
204513635bfSGleb Smirnoff 		free(V_id_bits, M_IPID);
2051f08c947SGleb Smirnoff 	}
206513635bfSGleb Smirnoff 	V_id_array = new_array;
207513635bfSGleb Smirnoff 	V_id_bits = new_bits;
208513635bfSGleb Smirnoff 	V_array_size = new_size;
209513635bfSGleb Smirnoff 	V_array_ptr = 0;
210513635bfSGleb Smirnoff 	V_random_id_collisions = 0;
211513635bfSGleb Smirnoff 	V_random_id_total = 0;
212513635bfSGleb Smirnoff 	mtx_unlock(&V_ip_id_mtx);
213361021ccSMike Silbersack }
21464dddc18SKris Kennaway 
2156d947416SGleb Smirnoff static uint16_t
2161f08c947SGleb Smirnoff ip_randomid(void)
2171f08c947SGleb Smirnoff {
2181f08c947SGleb Smirnoff 	uint16_t new_id;
2191f08c947SGleb Smirnoff 
220513635bfSGleb Smirnoff 	mtx_lock(&V_ip_id_mtx);
221361021ccSMike Silbersack 	/*
222361021ccSMike Silbersack 	 * To avoid a conflict with the zeros that the array is initially
223361021ccSMike Silbersack 	 * filled with, we never hand out an id of zero.
224361021ccSMike Silbersack 	 */
225361021ccSMike Silbersack 	new_id = 0;
226361021ccSMike Silbersack 	do {
227361021ccSMike Silbersack 		if (new_id != 0)
228513635bfSGleb Smirnoff 			V_random_id_collisions++;
229361021ccSMike Silbersack 		arc4rand(&new_id, sizeof(new_id), 0);
230513635bfSGleb Smirnoff 	} while (bit_test(V_id_bits, new_id) || new_id == 0);
231513635bfSGleb Smirnoff 	bit_clear(V_id_bits, V_id_array[V_array_ptr]);
232513635bfSGleb Smirnoff 	bit_set(V_id_bits, new_id);
233513635bfSGleb Smirnoff 	V_id_array[V_array_ptr] = new_id;
234513635bfSGleb Smirnoff 	V_array_ptr++;
235513635bfSGleb Smirnoff 	if (V_array_ptr == V_array_size)
236513635bfSGleb Smirnoff 		V_array_ptr = 0;
237513635bfSGleb Smirnoff 	V_random_id_total++;
238513635bfSGleb Smirnoff 	mtx_unlock(&V_ip_id_mtx);
239361021ccSMike Silbersack 	return (new_id);
24064dddc18SKris Kennaway }
2411f08c947SGleb Smirnoff 
2426d947416SGleb Smirnoff void
2436d947416SGleb Smirnoff ip_fillid(struct ip *ip)
2446d947416SGleb Smirnoff {
2456d947416SGleb Smirnoff 
2466d947416SGleb Smirnoff 	/*
2476d947416SGleb Smirnoff 	 * Per RFC6864 Section 4
2486d947416SGleb Smirnoff 	 *
2496d947416SGleb Smirnoff 	 * o  Atomic datagrams: (DF==1) && (MF==0) && (frag_offset==0)
2506d947416SGleb Smirnoff 	 * o  Non-atomic datagrams: (DF==0) || (MF==1) || (frag_offset>0)
2516d947416SGleb Smirnoff 	 */
2526d947416SGleb Smirnoff 	if (V_ip_rfc6864 && (ip->ip_off & htons(IP_DF)) == htons(IP_DF))
2536d947416SGleb Smirnoff 		ip->ip_id = 0;
2546d947416SGleb Smirnoff 	else if (V_ip_do_randomid)
2556d947416SGleb Smirnoff 		ip->ip_id = ip_randomid();
2566d947416SGleb Smirnoff 	else {
2576d947416SGleb Smirnoff 		counter_u64_add(V_ip_id, 1);
2587a742e37SGleb Smirnoff 		/*
2597a742e37SGleb Smirnoff 		 * There are two issues about this trick, to be kept in mind.
2607a742e37SGleb Smirnoff 		 * 1) We can migrate between counter_u64_add() and next
2617a742e37SGleb Smirnoff 		 *    line, and grab counter from other CPU, resulting in too
2627a742e37SGleb Smirnoff 		 *    quick ID reuse. This is tolerable in our particular case,
2637a742e37SGleb Smirnoff 		 *    since probability of such event is much lower then reuse
2647a742e37SGleb Smirnoff 		 *    of ID due to legitimate overflow, that at modern Internet
2657a742e37SGleb Smirnoff 		 *    speeds happens all the time.
2667a742e37SGleb Smirnoff 		 * 2) We are relying on the fact that counter(9) is based on
2677a742e37SGleb Smirnoff 		 *    UMA_ZONE_PCPU uma(9) zone. We also take only last
2687a742e37SGleb Smirnoff 		 *    sixteen bits of a counter, so we don't care about the
2697a742e37SGleb Smirnoff 		 *    fact that machines with 32-bit word update their counters
2707a742e37SGleb Smirnoff 		 *    not atomically.
2717a742e37SGleb Smirnoff 		 */
2726d947416SGleb Smirnoff 		ip->ip_id = htons((*(uint64_t *)zpcpu_get(V_ip_id)) & 0xffff);
2736d947416SGleb Smirnoff 	}
2746d947416SGleb Smirnoff }
2756d947416SGleb Smirnoff 
2761f08c947SGleb Smirnoff static void
277513635bfSGleb Smirnoff ipid_sysinit(void)
2781f08c947SGleb Smirnoff {
27996c85efbSNathan Whitehorn 	int i;
2801f08c947SGleb Smirnoff 
281513635bfSGleb Smirnoff 	mtx_init(&V_ip_id_mtx, "ip_id_mtx", NULL, MTX_DEF);
2826d947416SGleb Smirnoff 	V_ip_id = counter_u64_alloc(M_WAITOK);
28396c85efbSNathan Whitehorn 
28496c85efbSNathan Whitehorn 	CPU_FOREACH(i)
2856d947416SGleb Smirnoff 		arc4rand(zpcpu_get_cpu(V_ip_id, i), sizeof(uint64_t), 0);
2861f08c947SGleb Smirnoff }
287513635bfSGleb Smirnoff VNET_SYSINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipid_sysinit, NULL);
288513635bfSGleb Smirnoff 
289513635bfSGleb Smirnoff static void
290513635bfSGleb Smirnoff ipid_sysuninit(void)
291513635bfSGleb Smirnoff {
292513635bfSGleb Smirnoff 
2936d947416SGleb Smirnoff 	if (V_id_array != NULL) {
294513635bfSGleb Smirnoff 		free(V_id_array, M_IPID);
295513635bfSGleb Smirnoff 		free(V_id_bits, M_IPID);
296513635bfSGleb Smirnoff 	}
2976d947416SGleb Smirnoff 	counter_u64_free(V_ip_id);
298a6c96fc2SBjoern A. Zeeb 	mtx_destroy(&V_ip_id_mtx);
2996d947416SGleb Smirnoff }
30089856f7eSBjoern A. Zeeb VNET_SYSUNINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ipid_sysuninit, NULL);
301