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 94361021ccSMike Silbersack static u_int16_t *id_array = NULL; 95361021ccSMike Silbersack static bitstr_t *id_bits = NULL; 96361021ccSMike Silbersack static int array_ptr = 0; 97361021ccSMike Silbersack static int array_size = 8192; 98361021ccSMike Silbersack static int random_id_collisions = 0; 99361021ccSMike Silbersack static int random_id_total = 0; 100361021ccSMike Silbersack static struct mtx ip_id_mtx; 10164dddc18SKris Kennaway 1024d77a549SAlfred Perlstein static void ip_initid(void); 103361021ccSMike Silbersack static int sysctl_ip_id_change(SYSCTL_HANDLER_ARGS); 10464dddc18SKris Kennaway 105361021ccSMike Silbersack MTX_SYSINIT(ip_id_mtx, &ip_id_mtx, "ip_id_mtx", MTX_DEF); 10664dddc18SKris Kennaway 107361021ccSMike Silbersack SYSCTL_DECL(_net_inet_ip); 108361021ccSMike Silbersack SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id_period, CTLTYPE_INT|CTLFLAG_RW, 109361021ccSMike Silbersack &array_size, 0, sysctl_ip_id_change, "IU", "IP ID Array size"); 110361021ccSMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_collisions, CTLFLAG_RD, 111361021ccSMike Silbersack &random_id_collisions, 0, "Count of IP ID collisions"); 112361021ccSMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_total, CTLFLAG_RD, 113361021ccSMike Silbersack &random_id_total, 0, "Count of IP IDs created"); 114361021ccSMike Silbersack 115361021ccSMike Silbersack static int 116361021ccSMike Silbersack sysctl_ip_id_change(SYSCTL_HANDLER_ARGS) 11764dddc18SKris Kennaway { 118361021ccSMike Silbersack int error, new; 11964dddc18SKris Kennaway 120361021ccSMike Silbersack new = array_size; 121361021ccSMike Silbersack error = sysctl_handle_int(oidp, &new, 0, req); 122361021ccSMike Silbersack if (error == 0 && req->newptr) { 123361021ccSMike Silbersack if (new >= 512 && new <= 32768) { 124361021ccSMike Silbersack mtx_lock(&ip_id_mtx); 125361021ccSMike Silbersack array_size = new; 126361021ccSMike Silbersack ip_initid(); 127361021ccSMike Silbersack mtx_unlock(&ip_id_mtx); 128361021ccSMike Silbersack } else 129361021ccSMike Silbersack error = EINVAL; 13064dddc18SKris Kennaway } 131361021ccSMike Silbersack return (error); 13264dddc18SKris Kennaway } 13364dddc18SKris Kennaway 13464dddc18SKris Kennaway /* 135361021ccSMike Silbersack * ip_initid() runs with a mutex held and may execute in a network context. 136361021ccSMike Silbersack * As a result, it uses M_NOWAIT. Ideally, we would always do this 137361021ccSMike Silbersack * allocation from the sysctl contact and have it be an invariant that if 138361021ccSMike Silbersack * this random ID allocation mode is selected, the buffers are present. This 139361021ccSMike Silbersack * would also avoid potential network context failures of IP ID generation. 14064dddc18SKris Kennaway */ 14164dddc18SKris Kennaway static void 14264dddc18SKris Kennaway ip_initid(void) 14364dddc18SKris Kennaway { 14464dddc18SKris Kennaway 145361021ccSMike Silbersack mtx_assert(&ip_id_mtx, MA_OWNED); 14664dddc18SKris Kennaway 147361021ccSMike Silbersack if (id_array != NULL) { 148361021ccSMike Silbersack free(id_array, M_IPID); 149361021ccSMike Silbersack free(id_bits, M_IPID); 15064dddc18SKris Kennaway } 151361021ccSMike Silbersack random_id_collisions = 0; 152361021ccSMike Silbersack random_id_total = 0; 153361021ccSMike Silbersack array_ptr = 0; 154361021ccSMike Silbersack id_array = (u_int16_t *) malloc(array_size * sizeof(u_int16_t), 155361021ccSMike Silbersack M_IPID, M_NOWAIT | M_ZERO); 156361021ccSMike Silbersack id_bits = (bitstr_t *) malloc(bitstr_size(65536), M_IPID, 157361021ccSMike Silbersack M_NOWAIT | M_ZERO); 158361021ccSMike Silbersack if (id_array == NULL || id_bits == NULL) { 159361021ccSMike Silbersack /* Neither or both. */ 160361021ccSMike Silbersack if (id_array != NULL) { 161361021ccSMike Silbersack free(id_array, M_IPID); 162361021ccSMike Silbersack id_array = NULL; 163361021ccSMike Silbersack } 164361021ccSMike Silbersack if (id_bits != NULL) { 165361021ccSMike Silbersack free(id_bits, M_IPID); 166361021ccSMike Silbersack id_bits = NULL; 167361021ccSMike Silbersack } 168361021ccSMike Silbersack } 16964dddc18SKris Kennaway } 17064dddc18SKris Kennaway 17164dddc18SKris Kennaway u_int16_t 17264dddc18SKris Kennaway ip_randomid(void) 17364dddc18SKris Kennaway { 174361021ccSMike Silbersack u_int16_t new_id; 17564dddc18SKris Kennaway 176361021ccSMike Silbersack mtx_lock(&ip_id_mtx); 177361021ccSMike Silbersack if (id_array == NULL) 17864dddc18SKris Kennaway ip_initid(); 17964dddc18SKris Kennaway 180361021ccSMike Silbersack /* 181361021ccSMike Silbersack * Fail gracefully; return a fixed id if memory allocation failed; 182361021ccSMike Silbersack * ideally we wouldn't do allocation in this context in order to 183361021ccSMike Silbersack * avoid the possibility of this failure mode. 184361021ccSMike Silbersack */ 185361021ccSMike Silbersack if (id_array == NULL) { 186361021ccSMike Silbersack mtx_unlock(&ip_id_mtx); 187361021ccSMike Silbersack return (1); 188361021ccSMike Silbersack } 18964dddc18SKris Kennaway 190361021ccSMike Silbersack /* 191361021ccSMike Silbersack * To avoid a conflict with the zeros that the array is initially 192361021ccSMike Silbersack * filled with, we never hand out an id of zero. 193361021ccSMike Silbersack */ 194361021ccSMike Silbersack new_id = 0; 195361021ccSMike Silbersack do { 196361021ccSMike Silbersack if (new_id != 0) 197361021ccSMike Silbersack random_id_collisions++; 198361021ccSMike Silbersack arc4rand(&new_id, sizeof(new_id), 0); 199361021ccSMike Silbersack } while (bit_test(id_bits, new_id) || new_id == 0); 200361021ccSMike Silbersack bit_clear(id_bits, id_array[array_ptr]); 201361021ccSMike Silbersack bit_set(id_bits, new_id); 202361021ccSMike Silbersack id_array[array_ptr] = new_id; 203361021ccSMike Silbersack array_ptr++; 204361021ccSMike Silbersack if (array_ptr == array_size) 205361021ccSMike Silbersack array_ptr = 0; 206361021ccSMike Silbersack random_id_total++; 207361021ccSMike Silbersack mtx_unlock(&ip_id_mtx); 208361021ccSMike Silbersack return (new_id); 20964dddc18SKris Kennaway } 210