ip_id.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2008 Michael J. Silbersack.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice unmodified, this list of conditions, and the following
  12. * disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. /*
  31. * IP ID generation is a fascinating topic.
  32. *
  33. * In order to avoid ID collisions during packet reassembly, common sense
  34. * dictates that the period between reuse of IDs be as large as possible.
  35. * This leads to the classic implementation of a system-wide counter, thereby
  36. * ensuring that IDs repeat only once every 2^16 packets.
  37. *
  38. * Subsequent security researchers have pointed out that using a global
  39. * counter makes ID values predictable. This predictability allows traffic
  40. * analysis, idle scanning, and even packet injection in specific cases.
  41. * These results suggest that IP IDs should be as random as possible.
  42. *
  43. * The "searchable queues" algorithm used in this IP ID implementation was
  44. * proposed by Amit Klein. It is a compromise between the above two
  45. * viewpoints that has provable behavior that can be tuned to the user's
  46. * requirements.
  47. *
  48. * The basic concept is that we supplement a standard random number generator
  49. * with a queue of the last L IDs that we have handed out to ensure that all
  50. * IDs have a period of at least L.
  51. *
  52. * To efficiently implement this idea, we keep two data structures: a
  53. * circular array of IDs of size L and a bitstring of 65536 bits.
  54. *
  55. * To start, we ask the RNG for a new ID. A quick index into the bitstring
  56. * is used to determine if this is a recently used value. The process is
  57. * repeated until a value is returned that is not in the bitstring.
  58. *
  59. * Having found a usable ID, we remove the ID stored at the current position
  60. * in the queue from the bitstring and replace it with our new ID. Our new
  61. * ID is then added to the bitstring and the queue pointer is incremented.
  62. *
  63. * The lower limit of 512 was chosen because there doesn't seem to be much
  64. * point to having a smaller value. The upper limit of 32768 was chosen for
  65. * two reasons. First, every step above 32768 decreases the entropy. Taken
  66. * to an extreme, 65533 would offer 1 bit of entropy. Second, the number of
  67. * attempts it takes the algorithm to find an unused ID drastically
  68. * increases, killing performance. The default value of 8192 was chosen
  69. * because it provides a good tradeoff between randomness and non-repetition.
  70. *
  71. * With L=8192, the queue will use 16K of memory. The bitstring always
  72. * uses 8K of memory. No memory is allocated until the use of random ids is
  73. * enabled.
  74. */
  75. #include <sys/param.h>
  76. #include <sys/systm.h>
  77. #include <sys/counter.h>
  78. #include <sys/kernel.h>
  79. #include <sys/malloc.h>
  80. #include <sys/lock.h>
  81. #include <sys/mutex.h>
  82. #include <sys/random.h>
  83. #include <sys/smp.h>
  84. #include <sys/sysctl.h>
  85. #include <sys/bitstring.h>
  86. #include <net/vnet.h>
  87. #include <netinet/in.h>
  88. #include <netinet/ip.h>
  89. #include <netinet/ip_var.h>
  90. /*
  91. * By default we generate IP ID only for non-atomic datagrams, as
  92. * suggested by RFC6864. We use per-CPU counter for that, or if
  93. * user wants to, we can turn on random ID generation.
  94. */
  95. VNET_DEFINE_STATIC(int, ip_rfc6864) = 1;
  96. VNET_DEFINE_STATIC(int, ip_do_randomid) = 0;
  97. #define V_ip_rfc6864 VNET(ip_rfc6864)
  98. #define V_ip_do_randomid VNET(ip_do_randomid)
  99. /*
  100. * Random ID state engine.
  101. */
  102. static MALLOC_DEFINE(M_IPID, "ipid", "randomized ip id state");
  103. VNET_DEFINE_STATIC(uint16_t *, id_array);
  104. VNET_DEFINE_STATIC(bitstr_t *, id_bits);
  105. VNET_DEFINE_STATIC(int, array_ptr);
  106. VNET_DEFINE_STATIC(int, array_size);
  107. VNET_DEFINE_STATIC(int, random_id_collisions);
  108. VNET_DEFINE_STATIC(int, random_id_total);
  109. VNET_DEFINE_STATIC(struct mtx, ip_id_mtx);
  110. #define V_id_array VNET(id_array)
  111. #define V_id_bits VNET(id_bits)
  112. #define V_array_ptr VNET(array_ptr)
  113. #define V_array_size VNET(array_size)
  114. #define V_random_id_collisions VNET(random_id_collisions)
  115. #define V_random_id_total VNET(random_id_total)
  116. #define V_ip_id_mtx VNET(ip_id_mtx)
  117. /*
  118. * Non-random ID state engine is simply a per-cpu counter.
  119. */
  120. VNET_DEFINE_STATIC(counter_u64_t, ip_id);
  121. #define V_ip_id VNET(ip_id)
  122. static int sysctl_ip_randomid(SYSCTL_HANDLER_ARGS);
  123. static int sysctl_ip_id_change(SYSCTL_HANDLER_ARGS);
  124. static void ip_initid(int);
  125. static uint16_t ip_randomid(void);
  126. static void ipid_sysinit(void);
  127. static void ipid_sysuninit(void);
  128. SYSCTL_DECL(_net_inet_ip);
  129. SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id,
  130. CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_MPSAFE,
  131. &VNET_NAME(ip_do_randomid), 0, sysctl_ip_randomid, "IU",
  132. "Assign random ip_id values");
  133. SYSCTL_INT(_net_inet_ip, OID_AUTO, rfc6864, CTLFLAG_VNET | CTLFLAG_RW,
  134. &VNET_NAME(ip_rfc6864), 0,
  135. "Use constant IP ID for atomic datagrams");
  136. SYSCTL_PROC(_net_inet_ip, OID_AUTO, random_id_period,
  137. CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_VNET | CTLFLAG_MPSAFE,
  138. &VNET_NAME(array_size), 0, sysctl_ip_id_change, "IU", "IP ID Array size");
  139. SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_collisions,
  140. CTLFLAG_RD | CTLFLAG_VNET,
  141. &VNET_NAME(random_id_collisions), 0, "Count of IP ID collisions");
  142. SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id_total, CTLFLAG_RD | CTLFLAG_VNET,
  143. &VNET_NAME(random_id_total), 0, "Count of IP IDs created");
  144. static int
  145. sysctl_ip_randomid(SYSCTL_HANDLER_ARGS)
  146. {
  147. int error, new;
  148. new = V_ip_do_randomid;
  149. error = sysctl_handle_int(oidp, &new, 0, req);
  150. if (error || req->newptr == NULL)
  151. return (error);
  152. if (new != 0 && new != 1)
  153. return (EINVAL);
  154. if (new == V_ip_do_randomid)
  155. return (0);
  156. if (new == 1 && V_ip_do_randomid == 0)
  157. ip_initid(8192);
  158. /* We don't free memory when turning random ID off, due to race. */
  159. V_ip_do_randomid = new;
  160. return (0);
  161. }
  162. static int
  163. sysctl_ip_id_change(SYSCTL_HANDLER_ARGS)
  164. {
  165. int error, new;
  166. new = V_array_size;
  167. error = sysctl_handle_int(oidp, &new, 0, req);
  168. if (error == 0 && req->newptr) {
  169. if (new >= 512 && new <= 32768)
  170. ip_initid(new);
  171. else
  172. error = EINVAL;
  173. }
  174. return (error);
  175. }
  176. static void
  177. ip_initid(int new_size)
  178. {
  179. uint16_t *new_array;
  180. bitstr_t *new_bits;
  181. new_array = malloc(new_size * sizeof(uint16_t), M_IPID,
  182. M_WAITOK | M_ZERO);
  183. new_bits = malloc(bitstr_size(65536), M_IPID, M_WAITOK | M_ZERO);
  184. mtx_lock(&V_ip_id_mtx);
  185. if (V_id_array != NULL) {
  186. free(V_id_array, M_IPID);
  187. free(V_id_bits, M_IPID);
  188. }
  189. V_id_array = new_array;
  190. V_id_bits = new_bits;
  191. V_array_size = new_size;
  192. V_array_ptr = 0;
  193. V_random_id_collisions = 0;
  194. V_random_id_total = 0;
  195. mtx_unlock(&V_ip_id_mtx);
  196. }
  197. static uint16_t
  198. ip_randomid(void)
  199. {
  200. uint16_t new_id;
  201. mtx_lock(&V_ip_id_mtx);
  202. /*
  203. * To avoid a conflict with the zeros that the array is initially
  204. * filled with, we never hand out an id of zero.
  205. */
  206. new_id = 0;
  207. do {
  208. if (new_id != 0)
  209. V_random_id_collisions++;
  210. arc4rand(&new_id, sizeof(new_id), 0);
  211. } while (bit_test(V_id_bits, new_id) || new_id == 0);
  212. bit_clear(V_id_bits, V_id_array[V_array_ptr]);
  213. bit_set(V_id_bits, new_id);
  214. V_id_array[V_array_ptr] = new_id;
  215. V_array_ptr++;
  216. if (V_array_ptr == V_array_size)
  217. V_array_ptr = 0;
  218. V_random_id_total++;
  219. mtx_unlock(&V_ip_id_mtx);
  220. return (new_id);
  221. }
  222. void
  223. ip_fillid(struct ip *ip)
  224. {
  225. /*
  226. * Per RFC6864 Section 4
  227. *
  228. * o Atomic datagrams: (DF==1) && (MF==0) && (frag_offset==0)
  229. * o Non-atomic datagrams: (DF==0) || (MF==1) || (frag_offset>0)
  230. */
  231. if (V_ip_rfc6864 && (ip->ip_off & htons(IP_DF)) == htons(IP_DF))
  232. ip->ip_id = 0;
  233. else if (V_ip_do_randomid)
  234. ip->ip_id = ip_randomid();
  235. else {
  236. counter_u64_add(V_ip_id, 1);
  237. /*
  238. * There are two issues about this trick, to be kept in mind.
  239. * 1) We can migrate between counter_u64_add() and next
  240. * line, and grab counter from other CPU, resulting in too
  241. * quick ID reuse. This is tolerable in our particular case,
  242. * since probability of such event is much lower then reuse
  243. * of ID due to legitimate overflow, that at modern Internet
  244. * speeds happens all the time.
  245. * 2) We are relying on the fact that counter(9) is based on
  246. * UMA_ZONE_PCPU uma(9) zone. We also take only last
  247. * sixteen bits of a counter, so we don't care about the
  248. * fact that machines with 32-bit word update their counters
  249. * not atomically.
  250. */
  251. ip->ip_id = htons((*(uint64_t *)zpcpu_get(V_ip_id)) & 0xffff);
  252. }
  253. }
  254. static void
  255. ipid_sysinit(void)
  256. {
  257. int i;
  258. mtx_init(&V_ip_id_mtx, "ip_id_mtx", NULL, MTX_DEF);
  259. V_ip_id = counter_u64_alloc(M_WAITOK);
  260. CPU_FOREACH(i)
  261. arc4rand(zpcpu_get_cpu(V_ip_id, i), sizeof(uint64_t), 0);
  262. }
  263. VNET_SYSINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipid_sysinit, NULL);
  264. static void
  265. ipid_sysuninit(void)
  266. {
  267. if (V_id_array != NULL) {
  268. free(V_id_array, M_IPID);
  269. free(V_id_bits, M_IPID);
  270. }
  271. counter_u64_free(V_ip_id);
  272. mtx_destroy(&V_ip_id_mtx);
  273. }
  274. VNET_SYSUNINIT(ip_id, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ipid_sysuninit, NULL);