utils.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Generic address resultion entity
  3. *
  4. * Authors:
  5. * net_random Alan Cox
  6. * net_ratelimit Andy Kleen
  7. *
  8. * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/string.h>
  20. #include <linux/types.h>
  21. #include <linux/random.h>
  22. #include <linux/percpu.h>
  23. #include <linux/init.h>
  24. #include <asm/byteorder.h>
  25. #include <asm/system.h>
  26. #include <asm/uaccess.h>
  27. /*
  28. This is a maximally equidistributed combined Tausworthe generator
  29. based on code from GNU Scientific Library 1.5 (30 Jun 2004)
  30. x_n = (s1_n ^ s2_n ^ s3_n)
  31. s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
  32. s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
  33. s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
  34. The period of this generator is about 2^88.
  35. From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
  36. Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
  37. This is available on the net from L'Ecuyer's home page,
  38. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  39. ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
  40. There is an erratum in the paper "Tables of Maximally
  41. Equidistributed Combined LFSR Generators", Mathematics of
  42. Computation, 68, 225 (1999), 261--269:
  43. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
  44. ... the k_j most significant bits of z_j must be non-
  45. zero, for each j. (Note: this restriction also applies to the
  46. computer code given in [4], but was mistakenly not mentioned in
  47. that paper.)
  48. This affects the seeding procedure by imposing the requirement
  49. s1 > 1, s2 > 7, s3 > 15.
  50. */
  51. struct nrnd_state {
  52. u32 s1, s2, s3;
  53. };
  54. static DEFINE_PER_CPU(struct nrnd_state, net_rand_state);
  55. static u32 __net_random(struct nrnd_state *state)
  56. {
  57. #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
  58. state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
  59. state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
  60. state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
  61. return (state->s1 ^ state->s2 ^ state->s3);
  62. }
  63. static void __net_srandom(struct nrnd_state *state, unsigned long s)
  64. {
  65. if (s == 0)
  66. s = 1; /* default seed is 1 */
  67. #define LCG(n) (69069 * n)
  68. state->s1 = LCG(s);
  69. state->s2 = LCG(state->s1);
  70. state->s3 = LCG(state->s2);
  71. /* "warm it up" */
  72. __net_random(state);
  73. __net_random(state);
  74. __net_random(state);
  75. __net_random(state);
  76. __net_random(state);
  77. __net_random(state);
  78. }
  79. unsigned long net_random(void)
  80. {
  81. unsigned long r;
  82. struct nrnd_state *state = &get_cpu_var(net_rand_state);
  83. r = __net_random(state);
  84. put_cpu_var(state);
  85. return r;
  86. }
  87. void net_srandom(unsigned long entropy)
  88. {
  89. struct nrnd_state *state = &get_cpu_var(net_rand_state);
  90. __net_srandom(state, state->s1^entropy);
  91. put_cpu_var(state);
  92. }
  93. void __init net_random_init(void)
  94. {
  95. int i;
  96. for (i = 0; i < NR_CPUS; i++) {
  97. struct nrnd_state *state = &per_cpu(net_rand_state,i);
  98. __net_srandom(state, i+jiffies);
  99. }
  100. }
  101. static int net_random_reseed(void)
  102. {
  103. int i;
  104. unsigned long seed[NR_CPUS];
  105. get_random_bytes(seed, sizeof(seed));
  106. for (i = 0; i < NR_CPUS; i++) {
  107. struct nrnd_state *state = &per_cpu(net_rand_state,i);
  108. __net_srandom(state, seed[i]);
  109. }
  110. return 0;
  111. }
  112. late_initcall(net_random_reseed);
  113. int net_msg_cost = 5*HZ;
  114. int net_msg_burst = 10;
  115. /*
  116. * All net warning printk()s should be guarded by this function.
  117. */
  118. int net_ratelimit(void)
  119. {
  120. return __printk_ratelimit(net_msg_cost, net_msg_burst);
  121. }
  122. EXPORT_SYMBOL(net_random);
  123. EXPORT_SYMBOL(net_ratelimit);
  124. EXPORT_SYMBOL(net_srandom);
  125. /*
  126. * Convert an ASCII string to binary IP.
  127. * This is outside of net/ipv4/ because various code that uses IP addresses
  128. * is otherwise not dependent on the TCP/IP stack.
  129. */
  130. __u32 in_aton(const char *str)
  131. {
  132. unsigned long l;
  133. unsigned int val;
  134. int i;
  135. l = 0;
  136. for (i = 0; i < 4; i++)
  137. {
  138. l <<= 8;
  139. if (*str != '\0')
  140. {
  141. val = 0;
  142. while (*str != '\0' && *str != '.')
  143. {
  144. val *= 10;
  145. val += *str - '0';
  146. str++;
  147. }
  148. l |= val;
  149. if (*str != '\0')
  150. str++;
  151. }
  152. }
  153. return(htonl(l));
  154. }
  155. EXPORT_SYMBOL(in_aton);