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 7764dddc18SKris Kennaway #include <sys/param.h> 786d947416SGleb Smirnoff #include <sys/systm.h> 796d947416SGleb Smirnoff #include <sys/counter.h> 80*1d549750SBjoern A. Zeeb #include <sys/kernel.h> 816d947416SGleb Smirnoff #include <sys/malloc.h> 82361021ccSMike Silbersack #include <sys/lock.h> 83361021ccSMike Silbersack #include <sys/mutex.h> 8464dddc18SKris Kennaway #include <sys/random.h> 856d947416SGleb Smirnoff #include <sys/smp.h> 86361021ccSMike Silbersack #include <sys/sysctl.h> 87513635bfSGleb Smirnoff #include <sys/bitstring.h> 88513635bfSGleb Smirnoff 89513635bfSGleb Smirnoff #include <net/vnet.h> 90513635bfSGleb Smirnoff 91361021ccSMike Silbersack #include <netinet/in.h> 926d947416SGleb Smirnoff #include <netinet/ip.h> 93361021ccSMike Silbersack #include <netinet/ip_var.h> 9464dddc18SKris Kennaway 956d947416SGleb Smirnoff /* 966d947416SGleb Smirnoff * By default we generate IP ID only for non-atomic datagrams, as 976d947416SGleb Smirnoff * suggested by RFC6864. We use per-CPU counter for that, or if 986d947416SGleb Smirnoff * user wants to, we can turn on random ID generation. 996d947416SGleb Smirnoff */ 1006d947416SGleb Smirnoff static VNET_DEFINE(int, ip_rfc6864) = 1; 1016d947416SGleb Smirnoff static VNET_DEFINE(int, ip_do_randomid) = 0; 1026d947416SGleb Smirnoff #define V_ip_rfc6864 VNET(ip_rfc6864) 1036d947416SGleb Smirnoff #define V_ip_do_randomid VNET(ip_do_randomid) 10464dddc18SKris Kennaway 1056d947416SGleb Smirnoff /* 1066d947416SGleb Smirnoff * Random ID state engine. 1076d947416SGleb Smirnoff */ 1086d947416SGleb Smirnoff static MALLOC_DEFINE(M_IPID, "ipid", "randomized ip id state"); 109513635bfSGleb Smirnoff static VNET_DEFINE(uint16_t *, id_array); 110513635bfSGleb Smirnoff static VNET_DEFINE(bitstr_t *, id_bits); 111513635bfSGleb Smirnoff static VNET_DEFINE(int, array_ptr); 112513635bfSGleb Smirnoff static VNET_DEFINE(int, array_size); 113513635bfSGleb Smirnoff static VNET_DEFINE(int, random_id_collisions); 114513635bfSGleb Smirnoff static VNET_DEFINE(int, random_id_total); 115513635bfSGleb Smirnoff static VNET_DEFINE(struct mtx, ip_id_mtx); 116513635bfSGleb Smirnoff #define V_id_array VNET(id_array) 117513635bfSGleb Smirnoff #define V_id_bits VNET(id_bits) 118513635bfSGleb Smirnoff #define V_array_ptr VNET(array_ptr) 119513635bfSGleb Smirnoff #define V_array_size VNET(array_size) 120513635bfSGleb Smirnoff #define V_random_id_collisions VNET(random_id_collisions) 121513635bfSGleb Smirnoff #define V_random_id_total VNET(random_id_total) 122513635bfSGleb Smirnoff #define V_ip_id_mtx VNET(ip_id_mtx) 12364dddc18SKris Kennaway 1246d947416SGleb Smirnoff /* 1256d947416SGleb Smirnoff * Non-random ID state engine is simply a per-cpu counter. 1266d947416SGleb Smirnoff */ 1276d947416SGleb Smirnoff static VNET_DEFINE(counter_u64_t, ip_id); 1286d947416SGleb Smirnoff #define V_ip_id VNET(ip_id) 1296d947416SGleb Smirnoff 1306d947416SGleb Smirnoff static int sysctl_ip_randomid(SYSCTL_HANDLER_ARGS); 131361021ccSMike Silbersack static int sysctl_ip_id_change(SYSCTL_HANDLER_ARGS); 1326d947416SGleb Smirnoff static void ip_initid(int); 1336d947416SGleb Smirnoff static uint16_t ip_randomid(void); 134513635bfSGleb Smirnoff static void ipid_sysinit(void); 135513635bfSGleb Smirnoff static void ipid_sysuninit(void); 13664dddc18SKris Kennaway 1374b79449eSBjoern A. Zeeb SYSCTL_DECL(_net_inet_ip); 1386d947416SGleb Smirnoff SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id, 1396d947416SGleb Smirnoff CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW, 1406d947416SGleb Smirnoff &VNET_NAME(ip_do_randomid), 0, sysctl_ip_randomid, "IU", 1416d947416SGleb Smirnoff "Assign random ip_id values"); 1426d947416SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, rfc6864, CTLFLAG_VNET | CTLFLAG_RW, 1436d947416SGleb Smirnoff &VNET_NAME(ip_rfc6864), 0, 1446d947416SGleb Smirnoff "Use constant IP ID for atomic datagrams"); 145513635bfSGleb Smirnoff SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id_period, 146513635bfSGleb Smirnoff CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET, 147513635bfSGleb Smirnoff &VNET_NAME(array_size), 0, sysctl_ip_id_change, "IU", "IP ID Array size"); 148513635bfSGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_collisions, 149513635bfSGleb Smirnoff CTLFLAG_RD | CTLFLAG_VNET, 150513635bfSGleb Smirnoff &VNET_NAME(random_id_collisions), 0, "Count of IP ID collisions"); 151513635bfSGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_total, CTLFLAG_RD | CTLFLAG_VNET, 152513635bfSGleb Smirnoff &VNET_NAME(random_id_total), 0, "Count of IP IDs created"); 153361021ccSMike Silbersack 154361021ccSMike Silbersack static int 1556d947416SGleb Smirnoff sysctl_ip_randomid(SYSCTL_HANDLER_ARGS) 1566d947416SGleb Smirnoff { 1576d947416SGleb Smirnoff int error, new; 1586d947416SGleb Smirnoff 1596d947416SGleb Smirnoff new = V_ip_do_randomid; 1606d947416SGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 1616d947416SGleb Smirnoff if (error || req->newptr == NULL) 1626d947416SGleb Smirnoff return (error); 1636d947416SGleb Smirnoff if (new != 0 && new != 1) 1646d947416SGleb Smirnoff return (EINVAL); 1656d947416SGleb Smirnoff if (new == V_ip_do_randomid) 1666d947416SGleb Smirnoff return (0); 1676d947416SGleb Smirnoff if (new == 1 && V_ip_do_randomid == 0) 1686d947416SGleb Smirnoff ip_initid(8192); 1696d947416SGleb Smirnoff /* We don't free memory when turning random ID off, due to race. */ 1706d947416SGleb Smirnoff V_ip_do_randomid = new; 1716d947416SGleb Smirnoff return (0); 1726d947416SGleb Smirnoff } 1736d947416SGleb Smirnoff 1746d947416SGleb Smirnoff static int 175361021ccSMike Silbersack sysctl_ip_id_change(SYSCTL_HANDLER_ARGS) 17664dddc18SKris Kennaway { 177361021ccSMike Silbersack int error, new; 17864dddc18SKris Kennaway 179513635bfSGleb Smirnoff new = V_array_size; 180361021ccSMike Silbersack error = sysctl_handle_int(oidp, &new, 0, req); 181361021ccSMike Silbersack if (error == 0 && req->newptr) { 1821f08c947SGleb Smirnoff if (new >= 512 && new <= 32768) 1831f08c947SGleb Smirnoff ip_initid(new); 1841f08c947SGleb Smirnoff else 185361021ccSMike Silbersack error = EINVAL; 18664dddc18SKris Kennaway } 187361021ccSMike Silbersack return (error); 18864dddc18SKris Kennaway } 18964dddc18SKris Kennaway 19064dddc18SKris Kennaway static void 1911f08c947SGleb Smirnoff ip_initid(int new_size) 19264dddc18SKris Kennaway { 1931f08c947SGleb Smirnoff uint16_t *new_array; 1941f08c947SGleb Smirnoff bitstr_t *new_bits; 19564dddc18SKris Kennaway 1961f08c947SGleb Smirnoff new_array = malloc(new_size * sizeof(uint16_t), M_IPID, 1971f08c947SGleb Smirnoff M_WAITOK | M_ZERO); 1981f08c947SGleb Smirnoff new_bits = malloc(bitstr_size(65536), M_IPID, M_WAITOK | M_ZERO); 19964dddc18SKris Kennaway 200513635bfSGleb Smirnoff mtx_lock(&V_ip_id_mtx); 201513635bfSGleb Smirnoff if (V_id_array != NULL) { 202513635bfSGleb Smirnoff free(V_id_array, M_IPID); 203513635bfSGleb Smirnoff free(V_id_bits, M_IPID); 2041f08c947SGleb Smirnoff } 205513635bfSGleb Smirnoff V_id_array = new_array; 206513635bfSGleb Smirnoff V_id_bits = new_bits; 207513635bfSGleb Smirnoff V_array_size = new_size; 208513635bfSGleb Smirnoff V_array_ptr = 0; 209513635bfSGleb Smirnoff V_random_id_collisions = 0; 210513635bfSGleb Smirnoff V_random_id_total = 0; 211513635bfSGleb Smirnoff mtx_unlock(&V_ip_id_mtx); 212361021ccSMike Silbersack } 21364dddc18SKris Kennaway 2146d947416SGleb Smirnoff static uint16_t 2151f08c947SGleb Smirnoff ip_randomid(void) 2161f08c947SGleb Smirnoff { 2171f08c947SGleb Smirnoff uint16_t new_id; 2181f08c947SGleb Smirnoff 219513635bfSGleb Smirnoff mtx_lock(&V_ip_id_mtx); 220361021ccSMike Silbersack /* 221361021ccSMike Silbersack * To avoid a conflict with the zeros that the array is initially 222361021ccSMike Silbersack * filled with, we never hand out an id of zero. 223361021ccSMike Silbersack */ 224361021ccSMike Silbersack new_id = 0; 225361021ccSMike Silbersack do { 226361021ccSMike Silbersack if (new_id != 0) 227513635bfSGleb Smirnoff V_random_id_collisions++; 228361021ccSMike Silbersack arc4rand(&new_id, sizeof(new_id), 0); 229513635bfSGleb Smirnoff } while (bit_test(V_id_bits, new_id) || new_id == 0); 230513635bfSGleb Smirnoff bit_clear(V_id_bits, V_id_array[V_array_ptr]); 231513635bfSGleb Smirnoff bit_set(V_id_bits, new_id); 232513635bfSGleb Smirnoff V_id_array[V_array_ptr] = new_id; 233513635bfSGleb Smirnoff V_array_ptr++; 234513635bfSGleb Smirnoff if (V_array_ptr == V_array_size) 235513635bfSGleb Smirnoff V_array_ptr = 0; 236513635bfSGleb Smirnoff V_random_id_total++; 237513635bfSGleb Smirnoff mtx_unlock(&V_ip_id_mtx); 238361021ccSMike Silbersack return (new_id); 23964dddc18SKris Kennaway } 2401f08c947SGleb Smirnoff 2416d947416SGleb Smirnoff void 2426d947416SGleb Smirnoff ip_fillid(struct ip *ip) 2436d947416SGleb Smirnoff { 2446d947416SGleb Smirnoff 2456d947416SGleb Smirnoff /* 2466d947416SGleb Smirnoff * Per RFC6864 Section 4 2476d947416SGleb Smirnoff * 2486d947416SGleb Smirnoff * o Atomic datagrams: (DF==1) && (MF==0) && (frag_offset==0) 2496d947416SGleb Smirnoff * o Non-atomic datagrams: (DF==0) || (MF==1) || (frag_offset>0) 2506d947416SGleb Smirnoff */ 2516d947416SGleb Smirnoff if (V_ip_rfc6864 && (ip->ip_off & htons(IP_DF)) == htons(IP_DF)) 2526d947416SGleb Smirnoff ip->ip_id = 0; 2536d947416SGleb Smirnoff else if (V_ip_do_randomid) 2546d947416SGleb Smirnoff ip->ip_id = ip_randomid(); 2556d947416SGleb Smirnoff else { 2566d947416SGleb Smirnoff counter_u64_add(V_ip_id, 1); 2576d947416SGleb Smirnoff ip->ip_id = htons((*(uint64_t *)zpcpu_get(V_ip_id)) & 0xffff); 2586d947416SGleb Smirnoff } 2596d947416SGleb Smirnoff } 2606d947416SGleb Smirnoff 2611f08c947SGleb Smirnoff static void 262513635bfSGleb Smirnoff ipid_sysinit(void) 2631f08c947SGleb Smirnoff { 2641f08c947SGleb Smirnoff 265513635bfSGleb Smirnoff mtx_init(&V_ip_id_mtx, "ip_id_mtx", NULL, MTX_DEF); 2666d947416SGleb Smirnoff V_ip_id = counter_u64_alloc(M_WAITOK); 2676d947416SGleb Smirnoff for (int i = 0; i < mp_ncpus; i++) 2686d947416SGleb Smirnoff arc4rand(zpcpu_get_cpu(V_ip_id, i), sizeof(uint64_t), 0); 2691f08c947SGleb Smirnoff } 270513635bfSGleb Smirnoff VNET_SYSINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipid_sysinit, NULL); 271513635bfSGleb Smirnoff 272513635bfSGleb Smirnoff static void 273513635bfSGleb Smirnoff ipid_sysuninit(void) 274513635bfSGleb Smirnoff { 275513635bfSGleb Smirnoff 276513635bfSGleb Smirnoff mtx_destroy(&V_ip_id_mtx); 2776d947416SGleb Smirnoff if (V_id_array != NULL) { 278513635bfSGleb Smirnoff free(V_id_array, M_IPID); 279513635bfSGleb Smirnoff free(V_id_bits, M_IPID); 280513635bfSGleb Smirnoff } 2816d947416SGleb Smirnoff counter_u64_free(V_ip_id); 2826d947416SGleb Smirnoff } 283513635bfSGleb Smirnoff VNET_SYSUNINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipid_sysuninit, NULL); 284