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> 88*513635bfSGleb Smirnoff #include <sys/bitstring.h> 89*513635bfSGleb Smirnoff 90*513635bfSGleb Smirnoff #include <net/vnet.h> 91*513635bfSGleb Smirnoff 92361021ccSMike Silbersack #include <netinet/in.h> 93361021ccSMike Silbersack #include <netinet/ip_var.h> 9464dddc18SKris Kennaway 95361021ccSMike Silbersack static MALLOC_DEFINE(M_IPID, "ipid", "randomized ip id state"); 9664dddc18SKris Kennaway 97*513635bfSGleb Smirnoff static VNET_DEFINE(uint16_t *, id_array); 98*513635bfSGleb Smirnoff static VNET_DEFINE(bitstr_t *, id_bits); 99*513635bfSGleb Smirnoff static VNET_DEFINE(int, array_ptr); 100*513635bfSGleb Smirnoff static VNET_DEFINE(int, array_size); 101*513635bfSGleb Smirnoff static VNET_DEFINE(int, random_id_collisions); 102*513635bfSGleb Smirnoff static VNET_DEFINE(int, random_id_total); 103*513635bfSGleb Smirnoff static VNET_DEFINE(struct mtx, ip_id_mtx); 104*513635bfSGleb Smirnoff #define V_id_array VNET(id_array) 105*513635bfSGleb Smirnoff #define V_id_bits VNET(id_bits) 106*513635bfSGleb Smirnoff #define V_array_ptr VNET(array_ptr) 107*513635bfSGleb Smirnoff #define V_array_size VNET(array_size) 108*513635bfSGleb Smirnoff #define V_random_id_collisions VNET(random_id_collisions) 109*513635bfSGleb Smirnoff #define V_random_id_total VNET(random_id_total) 110*513635bfSGleb Smirnoff #define V_ip_id_mtx VNET(ip_id_mtx) 11164dddc18SKris Kennaway 1121f08c947SGleb Smirnoff static void ip_initid(int); 113361021ccSMike Silbersack static int sysctl_ip_id_change(SYSCTL_HANDLER_ARGS); 114*513635bfSGleb Smirnoff static void ipid_sysinit(void); 115*513635bfSGleb Smirnoff static void ipid_sysuninit(void); 11664dddc18SKris Kennaway 1174b79449eSBjoern A. Zeeb SYSCTL_DECL(_net_inet_ip); 118*513635bfSGleb Smirnoff SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id_period, 119*513635bfSGleb Smirnoff CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET, 120*513635bfSGleb Smirnoff &VNET_NAME(array_size), 0, sysctl_ip_id_change, "IU", "IP ID Array size"); 121*513635bfSGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_collisions, 122*513635bfSGleb Smirnoff CTLFLAG_RD | CTLFLAG_VNET, 123*513635bfSGleb Smirnoff &VNET_NAME(random_id_collisions), 0, "Count of IP ID collisions"); 124*513635bfSGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_total, CTLFLAG_RD | CTLFLAG_VNET, 125*513635bfSGleb Smirnoff &VNET_NAME(random_id_total), 0, "Count of IP IDs created"); 126361021ccSMike Silbersack 127361021ccSMike Silbersack static int 128361021ccSMike Silbersack sysctl_ip_id_change(SYSCTL_HANDLER_ARGS) 12964dddc18SKris Kennaway { 130361021ccSMike Silbersack int error, new; 13164dddc18SKris Kennaway 132*513635bfSGleb Smirnoff new = V_array_size; 133361021ccSMike Silbersack error = sysctl_handle_int(oidp, &new, 0, req); 134361021ccSMike Silbersack if (error == 0 && req->newptr) { 1351f08c947SGleb Smirnoff if (new >= 512 && new <= 32768) 1361f08c947SGleb Smirnoff ip_initid(new); 1371f08c947SGleb Smirnoff else 138361021ccSMike Silbersack error = EINVAL; 13964dddc18SKris Kennaway } 140361021ccSMike Silbersack return (error); 14164dddc18SKris Kennaway } 14264dddc18SKris Kennaway 14364dddc18SKris Kennaway static void 1441f08c947SGleb Smirnoff ip_initid(int new_size) 14564dddc18SKris Kennaway { 1461f08c947SGleb Smirnoff uint16_t *new_array; 1471f08c947SGleb Smirnoff bitstr_t *new_bits; 14864dddc18SKris Kennaway 1491f08c947SGleb Smirnoff new_array = malloc(new_size * sizeof(uint16_t), M_IPID, 1501f08c947SGleb Smirnoff M_WAITOK | M_ZERO); 1511f08c947SGleb Smirnoff new_bits = malloc(bitstr_size(65536), M_IPID, M_WAITOK | M_ZERO); 15264dddc18SKris Kennaway 153*513635bfSGleb Smirnoff mtx_lock(&V_ip_id_mtx); 154*513635bfSGleb Smirnoff if (V_id_array != NULL) { 155*513635bfSGleb Smirnoff free(V_id_array, M_IPID); 156*513635bfSGleb Smirnoff free(V_id_bits, M_IPID); 1571f08c947SGleb Smirnoff } 158*513635bfSGleb Smirnoff V_id_array = new_array; 159*513635bfSGleb Smirnoff V_id_bits = new_bits; 160*513635bfSGleb Smirnoff V_array_size = new_size; 161*513635bfSGleb Smirnoff V_array_ptr = 0; 162*513635bfSGleb Smirnoff V_random_id_collisions = 0; 163*513635bfSGleb Smirnoff V_random_id_total = 0; 164*513635bfSGleb Smirnoff mtx_unlock(&V_ip_id_mtx); 165361021ccSMike Silbersack } 16664dddc18SKris Kennaway 1671f08c947SGleb Smirnoff uint16_t 1681f08c947SGleb Smirnoff ip_randomid(void) 1691f08c947SGleb Smirnoff { 1701f08c947SGleb Smirnoff uint16_t new_id; 1711f08c947SGleb Smirnoff 172*513635bfSGleb Smirnoff mtx_lock(&V_ip_id_mtx); 173361021ccSMike Silbersack /* 174361021ccSMike Silbersack * To avoid a conflict with the zeros that the array is initially 175361021ccSMike Silbersack * filled with, we never hand out an id of zero. 176361021ccSMike Silbersack */ 177361021ccSMike Silbersack new_id = 0; 178361021ccSMike Silbersack do { 179361021ccSMike Silbersack if (new_id != 0) 180*513635bfSGleb Smirnoff V_random_id_collisions++; 181361021ccSMike Silbersack arc4rand(&new_id, sizeof(new_id), 0); 182*513635bfSGleb Smirnoff } while (bit_test(V_id_bits, new_id) || new_id == 0); 183*513635bfSGleb Smirnoff bit_clear(V_id_bits, V_id_array[V_array_ptr]); 184*513635bfSGleb Smirnoff bit_set(V_id_bits, new_id); 185*513635bfSGleb Smirnoff V_id_array[V_array_ptr] = new_id; 186*513635bfSGleb Smirnoff V_array_ptr++; 187*513635bfSGleb Smirnoff if (V_array_ptr == V_array_size) 188*513635bfSGleb Smirnoff V_array_ptr = 0; 189*513635bfSGleb Smirnoff V_random_id_total++; 190*513635bfSGleb Smirnoff mtx_unlock(&V_ip_id_mtx); 191361021ccSMike Silbersack return (new_id); 19264dddc18SKris Kennaway } 1931f08c947SGleb Smirnoff 1941f08c947SGleb Smirnoff static void 195*513635bfSGleb Smirnoff ipid_sysinit(void) 1961f08c947SGleb Smirnoff { 1971f08c947SGleb Smirnoff 198*513635bfSGleb Smirnoff mtx_init(&V_ip_id_mtx, "ip_id_mtx", NULL, MTX_DEF); 1991f08c947SGleb Smirnoff ip_initid(8192); 2001f08c947SGleb Smirnoff } 201*513635bfSGleb Smirnoff VNET_SYSINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipid_sysinit, NULL); 202*513635bfSGleb Smirnoff 203*513635bfSGleb Smirnoff static void 204*513635bfSGleb Smirnoff ipid_sysuninit(void) 205*513635bfSGleb Smirnoff { 206*513635bfSGleb Smirnoff 207*513635bfSGleb Smirnoff mtx_destroy(&V_ip_id_mtx); 208*513635bfSGleb Smirnoff free(V_id_array, M_IPID); 209*513635bfSGleb Smirnoff free(V_id_bits, M_IPID); 210*513635bfSGleb Smirnoff } 211*513635bfSGleb Smirnoff VNET_SYSUNINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipid_sysuninit, NULL); 212