The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/ip6_id.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: ip6_id.c,v 1.13 2004/03/23 05:31:54 itojun Exp $       */
    2 /*      $KAME: ip6_id.c,v 1.8 2003/09/06 13:41:06 itojun Exp $  */
    3 /*      $OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $       */
    4 
    5 /*
    6  * Copyright (C) 2003 WIDE Project.
    7  * All rights reserved.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 3. Neither the name of the project nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  */
   33 
   34 /*
   35  * Copyright 1998 Niels Provos <provos@citi.umich.edu>
   36  * All rights reserved.
   37  *
   38  * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
   39  * such a mathematical system to generate more random (yet non-repeating)
   40  * ids to solve the resolver/named problem.  But Niels designed the
   41  * actual system based on the constraints.
   42  *
   43  * Redistribution and use in source and binary forms, with or without
   44  * modification, are permitted provided that the following conditions
   45  * are met:
   46  * 1. Redistributions of source code must retain the above copyright
   47  *    notice, this list of conditions and the following disclaimer.
   48  * 2. Redistributions in binary form must reproduce the above copyright
   49  *    notice, this list of conditions and the following disclaimer in the
   50  *    documentation and/or other materials provided with the distribution.
   51  *
   52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   62  */
   63 
   64 /*
   65  * seed = random (bits - 1) bit
   66  * n = prime, g0 = generator to n,
   67  * j = random so that gcd(j,n-1) == 1
   68  * g = g0^j mod n will be a generator again.
   69  *
   70  * X[0] = random seed.
   71  * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
   72  * with a = 7^(even random) mod m,
   73  *      b = random with gcd(b,m) == 1
   74  *      m = constant and a maximal period of m-1.
   75  *
   76  * The transaction id is determined by:
   77  * id[n] = seed xor (g^X[n] mod n)
   78  *
   79  * Effectively the id is restricted to the lower (bits - 1) bits, thus
   80  * yielding two different cycles by toggling the msb on and off.
   81  * This avoids reuse issues caused by reseeding.
   82  */
   83 
   84 #include <sys/cdefs.h>
   85 __KERNEL_RCSID(0, "$NetBSD: ip6_id.c,v 1.13 2004/03/23 05:31:54 itojun Exp $");
   86 
   87 #include <sys/types.h>
   88 #include <sys/param.h>
   89 #include <sys/kernel.h>
   90 #include <lib/libkern/libkern.h>
   91 
   92 #include <net/if.h>
   93 #include <netinet/in.h>
   94 #include <netinet/ip6.h>
   95 #include <netinet6/ip6_var.h>
   96 
   97 struct randomtab {
   98         const int       ru_bits; /* resulting bits */
   99         const long      ru_out; /* Time after wich will be reseeded */
  100         const u_int32_t ru_max; /* Uniq cycle, avoid blackjack prediction */
  101         const u_int32_t ru_gen; /* Starting generator */
  102         const u_int32_t ru_n;   /* ru_n: prime, ru_n - 1: product of pfacts[] */
  103         const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */
  104         const u_int32_t ru_m;   /* ru_m = 2^x*3^y */
  105         const u_int32_t pfacts[4];      /* factors of ru_n */
  106 
  107         u_int32_t ru_counter;
  108         u_int32_t ru_msb;
  109 
  110         u_int32_t ru_x;
  111         u_int32_t ru_seed, ru_seed2;
  112         u_int32_t ru_a, ru_b;
  113         u_int32_t ru_g;
  114         long ru_reseed;
  115 };
  116 
  117 static struct randomtab randomtab_32 = {
  118         32,                     /* resulting bits */
  119         180,                    /* Time after wich will be reseeded */
  120         1000000000,             /* Uniq cycle, avoid blackjack prediction */
  121         2,                      /* Starting generator */
  122         2147483629,             /* RU_N-1 = 2^2*3^2*59652323 */
  123         7,                      /* determine ru_a as RU_AGEN^(2*rand) */
  124         1836660096,             /* RU_M = 2^7*3^15 - don't change */
  125         { 2, 3, 59652323, 0 },  /* factors of ru_n */
  126 };
  127 
  128 static struct randomtab randomtab_20 = {
  129         20,                     /* resulting bits */
  130         180,                    /* Time after wich will be reseeded */
  131         200000,                 /* Uniq cycle, avoid blackjack prediction */
  132         2,                      /* Starting generator */
  133         524269,                 /* RU_N-1 = 2^2*3^2*14563 */
  134         7,                      /* determine ru_a as RU_AGEN^(2*rand) */
  135         279936,                 /* RU_M = 2^7*3^7 - don't change */
  136         { 2, 3, 14563, 0 },     /* factors of ru_n */
  137 };
  138 
  139 static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
  140 static void initid(struct randomtab *);
  141 static u_int32_t randomid(struct randomtab *);
  142 
  143 /*
  144  * Do a fast modular exponation, returned value will be in the range
  145  * of 0 - (mod-1)
  146  */
  147 
  148 static u_int32_t
  149 pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod)
  150 {
  151         u_int64_t s, t, u;
  152 
  153         s = 1;
  154         t = gen;
  155         u = expo;
  156 
  157         while (u) {
  158                 if (u & 1)
  159                         s = (s * t) % mod;
  160                 u >>= 1;
  161                 t = (t * t) % mod;
  162         }
  163         return (s);
  164 }
  165 
  166 /*
  167  * Initalizes the seed and chooses a suitable generator. Also toggles
  168  * the msb flag. The msb flag is used to generate two distinct
  169  * cycles of random numbers and thus avoiding reuse of ids.
  170  *
  171  * This function is called from id_randomid() when needed, an
  172  * application does not have to worry about it.
  173  */
  174 static void
  175 initid(struct randomtab *p)
  176 {
  177         u_int32_t j, i;
  178         int noprime = 1;
  179 
  180         p->ru_x = arc4random() % p->ru_m;
  181 
  182         /* (bits - 1) bits of random seed */
  183         p->ru_seed = arc4random() & (~0U >> (32 - p->ru_bits + 1));
  184         p->ru_seed2 = arc4random() & (~0U >> (32 - p->ru_bits + 1));
  185 
  186         /* Determine the LCG we use */
  187         p->ru_b = (arc4random() & (~0U >> (32 - p->ru_bits))) | 1;
  188         p->ru_a = pmod(p->ru_agen,
  189             (arc4random() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
  190         while (p->ru_b % 3 == 0)
  191                 p->ru_b += 2;
  192 
  193         j = arc4random() % p->ru_n;
  194 
  195         /*
  196          * Do a fast gcd(j, RU_N - 1), so we can find a j with
  197          * gcd(j, RU_N - 1) == 1, giving a new generator for
  198          * RU_GEN^j mod RU_N
  199          */
  200         while (noprime) {
  201                 for (i = 0; p->pfacts[i] > 0; i++)
  202                         if (j % p->pfacts[i] == 0)
  203                                 break;
  204 
  205                 if (p->pfacts[i] == 0)
  206                         noprime = 0;
  207                 else
  208                         j = (j + 1) % p->ru_n;
  209         }
  210 
  211         p->ru_g = pmod(p->ru_gen, j, p->ru_n);
  212         p->ru_counter = 0;
  213 
  214         p->ru_reseed = time.tv_sec + p->ru_out;
  215         p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
  216 }
  217 
  218 static u_int32_t
  219 randomid(struct randomtab *p)
  220 {
  221         int i, n;
  222 
  223         if (p->ru_counter >= p->ru_max || time.tv_sec > p->ru_reseed)
  224                 initid(p);
  225 
  226         /* Skip a random number of ids */
  227         n = arc4random() & 0x3;
  228         if (p->ru_counter + n >= p->ru_max)
  229                 initid(p);
  230 
  231         for (i = 0; i <= n; i++) {
  232                 /* Linear Congruential Generator */
  233                 p->ru_x = ((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;
  234         }
  235 
  236         p->ru_counter += i;
  237 
  238         return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 + p->ru_x, p->ru_n)) |
  239             p->ru_msb;
  240 }
  241 
  242 u_int32_t
  243 ip6_randomid(void)
  244 {
  245 
  246         return randomid(&randomtab_32);
  247 }
  248 
  249 u_int32_t
  250 ip6_randomflowlabel(void)
  251 {
  252 
  253         return randomid(&randomtab_20) & 0xfffff;
  254 }

Cache object: 2eb6f356715f5923990ae64477d0000c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.