2 * Generic address resultion entity
6 * net_ratelimit Andi Kleen
7 * in{4,6}_pton YOSHIFUJI Hideaki, Copyright (C)2006 USAGI/WIDE Project
9 * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
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.
17 #include <linux/module.h>
18 #include <linux/jiffies.h>
19 #include <linux/kernel.h>
20 #include <linux/inet.h>
22 #include <linux/net.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <linux/random.h>
26 #include <linux/percpu.h>
27 #include <linux/init.h>
29 #include <asm/byteorder.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
34 This is a maximally equidistributed combined Tausworthe generator
35 based on code from GNU Scientific Library 1.5 (30 Jun 2004)
37 x_n = (s1_n ^ s2_n ^ s3_n)
39 s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
40 s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
41 s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
43 The period of this generator is about 2^88.
45 From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
46 Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
48 This is available on the net from L'Ecuyer's home page,
50 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
51 ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
53 There is an erratum in the paper "Tables of Maximally
54 Equidistributed Combined LFSR Generators", Mathematics of
55 Computation, 68, 225 (1999), 261--269:
56 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
58 ... the k_j most significant bits of z_j must be non-
59 zero, for each j. (Note: this restriction also applies to the
60 computer code given in [4], but was mistakenly not mentioned in
63 This affects the seeding procedure by imposing the requirement
64 s1 > 1, s2 > 7, s3 > 15.
71 static DEFINE_PER_CPU(struct nrnd_state, net_rand_state);
73 static u32 __net_random(struct nrnd_state *state)
75 #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
77 state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
78 state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
79 state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
81 return (state->s1 ^ state->s2 ^ state->s3);
84 static void __net_srandom(struct nrnd_state *state, unsigned long s)
87 s = 1; /* default seed is 1 */
89 #define LCG(n) (69069 * n)
91 state->s2 = LCG(state->s1);
92 state->s3 = LCG(state->s2);
104 unsigned long net_random(void)
107 struct nrnd_state *state = &get_cpu_var(net_rand_state);
108 r = __net_random(state);
114 void net_srandom(unsigned long entropy)
116 struct nrnd_state *state = &get_cpu_var(net_rand_state);
117 __net_srandom(state, state->s1^entropy);
121 void __init net_random_init(void)
125 for_each_possible_cpu(i) {
126 struct nrnd_state *state = &per_cpu(net_rand_state,i);
127 __net_srandom(state, i+jiffies);
131 static int net_random_reseed(void)
136 for_each_possible_cpu(i) {
137 struct nrnd_state *state = &per_cpu(net_rand_state,i);
139 get_random_bytes(&seed, sizeof(seed));
140 __net_srandom(state, seed);
144 late_initcall(net_random_reseed);
146 int net_msg_cost = 5*HZ;
147 int net_msg_burst = 10;
150 * All net warning printk()s should be guarded by this function.
152 int net_ratelimit(void)
154 return __printk_ratelimit(net_msg_cost, net_msg_burst);
157 EXPORT_SYMBOL(net_random);
158 EXPORT_SYMBOL(net_ratelimit);
159 EXPORT_SYMBOL(net_srandom);
162 * Convert an ASCII string to binary IP.
163 * This is outside of net/ipv4/ because various code that uses IP addresses
164 * is otherwise not dependent on the TCP/IP stack.
167 __be32 in_aton(const char *str)
174 for (i = 0; i < 4; i++)
180 while (*str != '\0' && *str != '.' && *str != '\n')
194 EXPORT_SYMBOL(in_aton);
196 #define IN6PTON_XDIGIT 0x00010000
197 #define IN6PTON_DIGIT 0x00020000
198 #define IN6PTON_COLON_MASK 0x00700000
199 #define IN6PTON_COLON_1 0x00100000 /* single : requested */
200 #define IN6PTON_COLON_2 0x00200000 /* second : requested */
201 #define IN6PTON_COLON_1_2 0x00400000 /* :: requested */
202 #define IN6PTON_DOT 0x00800000 /* . */
203 #define IN6PTON_DELIM 0x10000000
204 #define IN6PTON_NULL 0x20000000 /* first/tail */
205 #define IN6PTON_UNKNOWN 0x40000000
207 static inline int digit2bin(char c, char delim)
209 if (c == delim || c == '\0')
210 return IN6PTON_DELIM;
213 if (c >= '0' && c <= '9')
214 return (IN6PTON_DIGIT | (c - '0'));
215 return IN6PTON_UNKNOWN;
218 static inline int xdigit2bin(char c, char delim)
220 if (c == delim || c == '\0')
221 return IN6PTON_DELIM;
223 return IN6PTON_COLON_MASK;
226 if (c >= '0' && c <= '9')
227 return (IN6PTON_XDIGIT | IN6PTON_DIGIT| (c - '0'));
228 if (c >= 'a' && c <= 'f')
229 return (IN6PTON_XDIGIT | (c - 'a' + 10));
230 if (c >= 'A' && c <= 'F')
231 return (IN6PTON_XDIGIT | (c - 'A' + 10));
232 return IN6PTON_UNKNOWN;
235 int in4_pton(const char *src, int srclen,
237 char delim, const char **end)
247 srclen = strlen(src);
253 c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
254 if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM))) {
257 if (c & (IN6PTON_DOT | IN6PTON_DELIM)) {
263 if (c & IN6PTON_DELIM) {
271 if ((w & 0xffff) > 255) {
281 memcpy(dst, dbuf, sizeof(dbuf));
288 EXPORT_SYMBOL(in4_pton);
290 int in6_pton(const char *src, int srclen,
292 char delim, const char **end)
294 const char *s, *tok = NULL;
299 int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
302 memset(dbuf, 0, sizeof(dbuf));
307 srclen = strlen(src);
312 c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
315 if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
316 /* process one 16-bit word */
317 if (!(state & IN6PTON_NULL)) {
318 *d++ = (w >> 8) & 0xff;
322 if (c & IN6PTON_DELIM) {
323 /* We've processed last word */
328 * COLON_2 => XDIGIT|DELIM
329 * COLON_1_2 => COLON_2
331 switch (state & IN6PTON_COLON_MASK) {
332 case IN6PTON_COLON_2:
334 state = IN6PTON_XDIGIT | IN6PTON_DELIM;
335 if (dc - dbuf >= sizeof(dbuf))
336 state |= IN6PTON_NULL;
338 case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
339 state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
341 case IN6PTON_COLON_1:
342 state = IN6PTON_XDIGIT;
344 case IN6PTON_COLON_1_2:
345 state = IN6PTON_COLON_2;
354 if (c & IN6PTON_DOT) {
355 ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
363 w = (w << 4) | (0xff & c);
364 state = IN6PTON_COLON_1 | IN6PTON_DELIM;
366 state |= IN6PTON_XDIGIT;
368 if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
369 state |= IN6PTON_COLON_1_2;
370 state &= ~IN6PTON_DELIM;
372 if (d + 2 >= dbuf + sizeof(dbuf)) {
373 state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
376 if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
377 d + 4 == dbuf + sizeof(dbuf)) {
378 state |= IN6PTON_DOT;
380 if (d >= dbuf + sizeof(dbuf)) {
381 state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
392 while(i >= dc - dbuf)
397 memcpy(dst, dbuf, sizeof(dbuf));
406 EXPORT_SYMBOL(in6_pton);