utils.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Generic address resultion entity
  3. *
  4. * Authors:
  5. * net_random Alan Cox
  6. * net_ratelimit Andy Kleen
  7. * in{4,6}_pton YOSHIFUJI Hideaki, Copyright (C)2006 USAGI/WIDE Project
  8. *
  9. * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/kernel.h>
  19. #include <linux/inet.h>
  20. #include <linux/mm.h>
  21. #include <linux/net.h>
  22. #include <linux/string.h>
  23. #include <linux/types.h>
  24. #include <linux/random.h>
  25. #include <linux/percpu.h>
  26. #include <linux/init.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/system.h>
  29. #include <asm/uaccess.h>
  30. /*
  31. This is a maximally equidistributed combined Tausworthe generator
  32. based on code from GNU Scientific Library 1.5 (30 Jun 2004)
  33. x_n = (s1_n ^ s2_n ^ s3_n)
  34. s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
  35. s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
  36. s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
  37. The period of this generator is about 2^88.
  38. From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
  39. Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
  40. This is available on the net from L'Ecuyer's home page,
  41. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  42. ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
  43. There is an erratum in the paper "Tables of Maximally
  44. Equidistributed Combined LFSR Generators", Mathematics of
  45. Computation, 68, 225 (1999), 261--269:
  46. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
  47. ... the k_j most significant bits of z_j must be non-
  48. zero, for each j. (Note: this restriction also applies to the
  49. computer code given in [4], but was mistakenly not mentioned in
  50. that paper.)
  51. This affects the seeding procedure by imposing the requirement
  52. s1 > 1, s2 > 7, s3 > 15.
  53. */
  54. struct nrnd_state {
  55. u32 s1, s2, s3;
  56. };
  57. static DEFINE_PER_CPU(struct nrnd_state, net_rand_state);
  58. static u32 __net_random(struct nrnd_state *state)
  59. {
  60. #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
  61. state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
  62. state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
  63. state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
  64. return (state->s1 ^ state->s2 ^ state->s3);
  65. }
  66. static void __net_srandom(struct nrnd_state *state, unsigned long s)
  67. {
  68. if (s == 0)
  69. s = 1; /* default seed is 1 */
  70. #define LCG(n) (69069 * n)
  71. state->s1 = LCG(s);
  72. state->s2 = LCG(state->s1);
  73. state->s3 = LCG(state->s2);
  74. /* "warm it up" */
  75. __net_random(state);
  76. __net_random(state);
  77. __net_random(state);
  78. __net_random(state);
  79. __net_random(state);
  80. __net_random(state);
  81. }
  82. unsigned long net_random(void)
  83. {
  84. unsigned long r;
  85. struct nrnd_state *state = &get_cpu_var(net_rand_state);
  86. r = __net_random(state);
  87. put_cpu_var(state);
  88. return r;
  89. }
  90. void net_srandom(unsigned long entropy)
  91. {
  92. struct nrnd_state *state = &get_cpu_var(net_rand_state);
  93. __net_srandom(state, state->s1^entropy);
  94. put_cpu_var(state);
  95. }
  96. void __init net_random_init(void)
  97. {
  98. int i;
  99. for_each_possible_cpu(i) {
  100. struct nrnd_state *state = &per_cpu(net_rand_state,i);
  101. __net_srandom(state, i+jiffies);
  102. }
  103. }
  104. static int net_random_reseed(void)
  105. {
  106. int i;
  107. unsigned long seed;
  108. for_each_possible_cpu(i) {
  109. struct nrnd_state *state = &per_cpu(net_rand_state,i);
  110. get_random_bytes(&seed, sizeof(seed));
  111. __net_srandom(state, seed);
  112. }
  113. return 0;
  114. }
  115. late_initcall(net_random_reseed);
  116. int net_msg_cost = 5*HZ;
  117. int net_msg_burst = 10;
  118. /*
  119. * All net warning printk()s should be guarded by this function.
  120. */
  121. int net_ratelimit(void)
  122. {
  123. return __printk_ratelimit(net_msg_cost, net_msg_burst);
  124. }
  125. EXPORT_SYMBOL(net_random);
  126. EXPORT_SYMBOL(net_ratelimit);
  127. EXPORT_SYMBOL(net_srandom);
  128. /*
  129. * Convert an ASCII string to binary IP.
  130. * This is outside of net/ipv4/ because various code that uses IP addresses
  131. * is otherwise not dependent on the TCP/IP stack.
  132. */
  133. __be32 in_aton(const char *str)
  134. {
  135. unsigned long l;
  136. unsigned int val;
  137. int i;
  138. l = 0;
  139. for (i = 0; i < 4; i++)
  140. {
  141. l <<= 8;
  142. if (*str != '\0')
  143. {
  144. val = 0;
  145. while (*str != '\0' && *str != '.' && *str != '\n')
  146. {
  147. val *= 10;
  148. val += *str - '0';
  149. str++;
  150. }
  151. l |= val;
  152. if (*str != '\0')
  153. str++;
  154. }
  155. }
  156. return(htonl(l));
  157. }
  158. EXPORT_SYMBOL(in_aton);
  159. #define IN6PTON_XDIGIT 0x00010000
  160. #define IN6PTON_DIGIT 0x00020000
  161. #define IN6PTON_COLON_MASK 0x00700000
  162. #define IN6PTON_COLON_1 0x00100000 /* single : requested */
  163. #define IN6PTON_COLON_2 0x00200000 /* second : requested */
  164. #define IN6PTON_COLON_1_2 0x00400000 /* :: requested */
  165. #define IN6PTON_DOT 0x00800000 /* . */
  166. #define IN6PTON_DELIM 0x10000000
  167. #define IN6PTON_NULL 0x20000000 /* first/tail */
  168. #define IN6PTON_UNKNOWN 0x40000000
  169. static inline int digit2bin(char c, char delim)
  170. {
  171. if (c == delim || c == '\0')
  172. return IN6PTON_DELIM;
  173. if (c == '.')
  174. return IN6PTON_DOT;
  175. if (c >= '0' && c <= '9')
  176. return (IN6PTON_DIGIT | (c - '0'));
  177. return IN6PTON_UNKNOWN;
  178. }
  179. static inline int xdigit2bin(char c, char delim)
  180. {
  181. if (c == delim || c == '\0')
  182. return IN6PTON_DELIM;
  183. if (c == ':')
  184. return IN6PTON_COLON_MASK;
  185. if (c == '.')
  186. return IN6PTON_DOT;
  187. if (c >= '0' && c <= '9')
  188. return (IN6PTON_XDIGIT | IN6PTON_DIGIT| (c - '0'));
  189. if (c >= 'a' && c <= 'f')
  190. return (IN6PTON_XDIGIT | (c - 'a' + 10));
  191. if (c >= 'A' && c <= 'F')
  192. return (IN6PTON_XDIGIT | (c - 'A' + 10));
  193. return IN6PTON_UNKNOWN;
  194. }
  195. int in4_pton(const char *src, int srclen,
  196. u8 *dst,
  197. char delim, const char **end)
  198. {
  199. const char *s;
  200. u8 *d;
  201. u8 dbuf[4];
  202. int ret = 0;
  203. int i;
  204. int w = 0;
  205. if (srclen < 0)
  206. srclen = strlen(src);
  207. s = src;
  208. d = dbuf;
  209. i = 0;
  210. while(1) {
  211. int c;
  212. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  213. if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM))) {
  214. goto out;
  215. }
  216. if (c & (IN6PTON_DOT | IN6PTON_DELIM)) {
  217. if (w == 0)
  218. goto out;
  219. *d++ = w & 0xff;
  220. w = 0;
  221. i++;
  222. if (c & IN6PTON_DELIM) {
  223. if (i != 4)
  224. goto out;
  225. break;
  226. }
  227. goto cont;
  228. }
  229. w = (w * 10) + c;
  230. if ((w & 0xffff) > 255) {
  231. goto out;
  232. }
  233. cont:
  234. if (i >= 4)
  235. goto out;
  236. s++;
  237. srclen--;
  238. }
  239. ret = 1;
  240. memcpy(dst, dbuf, sizeof(dbuf));
  241. out:
  242. if (end)
  243. *end = s;
  244. return ret;
  245. }
  246. EXPORT_SYMBOL(in4_pton);
  247. int in6_pton(const char *src, int srclen,
  248. u8 *dst,
  249. char delim, const char **end)
  250. {
  251. const char *s, *tok = NULL;
  252. u8 *d, *dc = NULL;
  253. u8 dbuf[16];
  254. int ret = 0;
  255. int i;
  256. int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
  257. int w = 0;
  258. memset(dbuf, 0, sizeof(dbuf));
  259. s = src;
  260. d = dbuf;
  261. if (srclen < 0)
  262. srclen = strlen(src);
  263. while (1) {
  264. int c;
  265. c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
  266. if (!(c & state))
  267. goto out;
  268. if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
  269. /* process one 16-bit word */
  270. if (!(state & IN6PTON_NULL)) {
  271. *d++ = (w >> 8) & 0xff;
  272. *d++ = w & 0xff;
  273. }
  274. w = 0;
  275. if (c & IN6PTON_DELIM) {
  276. /* We've processed last word */
  277. break;
  278. }
  279. /*
  280. * COLON_1 => XDIGIT
  281. * COLON_2 => XDIGIT|DELIM
  282. * COLON_1_2 => COLON_2
  283. */
  284. switch (state & IN6PTON_COLON_MASK) {
  285. case IN6PTON_COLON_2:
  286. dc = d;
  287. state = IN6PTON_XDIGIT | IN6PTON_DELIM;
  288. if (dc - dbuf >= sizeof(dbuf))
  289. state |= IN6PTON_NULL;
  290. break;
  291. case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
  292. state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
  293. break;
  294. case IN6PTON_COLON_1:
  295. state = IN6PTON_XDIGIT;
  296. break;
  297. case IN6PTON_COLON_1_2:
  298. state = IN6PTON_COLON_2;
  299. break;
  300. default:
  301. state = 0;
  302. }
  303. tok = s + 1;
  304. goto cont;
  305. }
  306. if (c & IN6PTON_DOT) {
  307. ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
  308. if (ret > 0) {
  309. d += 4;
  310. break;
  311. }
  312. goto out;
  313. }
  314. w = (w << 4) | (0xff & c);
  315. state = IN6PTON_COLON_1 | IN6PTON_DELIM;
  316. if (!(w & 0xf000)) {
  317. state |= IN6PTON_XDIGIT;
  318. }
  319. if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
  320. state |= IN6PTON_COLON_1_2;
  321. state &= ~IN6PTON_DELIM;
  322. }
  323. if (d + 2 >= dbuf + sizeof(dbuf)) {
  324. state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
  325. }
  326. cont:
  327. if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
  328. d + 4 == dbuf + sizeof(dbuf)) {
  329. state |= IN6PTON_DOT;
  330. }
  331. if (d >= dbuf + sizeof(dbuf)) {
  332. state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
  333. }
  334. s++;
  335. srclen--;
  336. }
  337. i = 15; d--;
  338. if (dc) {
  339. while(d >= dc)
  340. dst[i--] = *d--;
  341. while(i >= dc - dbuf)
  342. dst[i--] = 0;
  343. while(i >= 0)
  344. dst[i--] = *d--;
  345. } else
  346. memcpy(dst, dbuf, sizeof(dbuf));
  347. ret = 1;
  348. out:
  349. if (end)
  350. *end = s;
  351. return ret;
  352. }
  353. EXPORT_SYMBOL(in6_pton);