utils.c 4.5 KB

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