OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [bsd_tcpip/] [current/] [src/] [sys/] [netinet/] [ip_id.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      src/sys/netinet/ip_id.c
4
//
5
//==========================================================================
6
// ####BSDCOPYRIGHTBEGIN####                                    
7
// -------------------------------------------                  
8
// This file is part of eCos, the Embedded Configurable Operating System.
9
//
10
// Portions of this software may have been derived from FreeBSD 
11
// or other sources, and if so are covered by the appropriate copyright
12
// and license included herein.                                 
13
//
14
// Portions created by the Free Software Foundation are         
15
// Copyright (C) 2002 Free Software Foundation, Inc.            
16
// -------------------------------------------                  
17
// ####BSDCOPYRIGHTEND####                                      
18
//==========================================================================
19
 
20
/* $OpenBSD: ip_id.c,v 1.2 1999/08/26 13:37:01 provos Exp $ */
21
 
22
/*
23
 * Copyright 1998 Niels Provos <provos@citi.umich.edu>
24
 * All rights reserved.
25
 *
26
 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
27
 * such a mathematical system to generate more random (yet non-repeating)
28
 * ids to solve the resolver/named problem.  But Niels designed the
29
 * actual system based on the constraints.
30
 *
31
 * Redistribution and use in source and binary forms, with or without
32
 * modification, are permitted provided that the following conditions
33
 * are met:
34
 * 1. Redistributions of source code must retain the above copyright
35
 *    notice, this list of conditions and the following disclaimer.
36
 * 2. Redistributions in binary form must reproduce the above copyright
37
 *    notice, this list of conditions and the following disclaimer in the
38
 *    documentation and/or other materials provided with the distribution.
39
 * 3. All advertising materials mentioning features or use of this software
40
 *    must display the following acknowledgement:
41
 *      This product includes software developed by Niels Provos.
42
 * 4. The name of the author may not be used to endorse or promote products
43
 *    derived from this software without specific prior written permission.
44
 *
45
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55
 *
56
 * $FreeBSD: src/sys/netinet/ip_id.c,v 1.1.2.1 2001/07/19 06:37:26 kris Exp $
57
 */
58
 
59
/*
60
 * seed = random 15bit
61
 * n = prime, g0 = generator to n,
62
 * j = random so that gcd(j,n-1) == 1
63
 * g = g0^j mod n will be a generator again.
64
 *
65
 * X[0] = random seed.
66
 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
67
 * with a = 7^(even random) mod m,
68
 *      b = random with gcd(b,m) == 1
69
 *      m = 31104 and a maximal period of m-1.
70
 *
71
 * The transaction id is determined by:
72
 * id[n] = seed xor (g^X[n] mod n)
73
 *
74
 * Effectivly the id is restricted to the lower 15 bits, thus
75
 * yielding two different cycles by toggling the msb on and off.
76
 * This avoids reuse issues caused by reseeding.
77
 */
78
 
79
#include <sys/param.h>
80
 
81
#ifdef RANDOM_IP_ID
82
#define RU_OUT  180             /* Time after wich will be reseeded */
83
#define RU_MAX  30000           /* Uniq cycle, avoid blackjack prediction */
84
#define RU_GEN  2               /* Starting generator */
85
#define RU_N    32749           /* RU_N-1 = 2*2*3*2729 */
86
#define RU_AGEN 7               /* determine ru_a as RU_AGEN^(2*rand) */
87
#define RU_M    31104           /* RU_M = 2^7*3^5 - don't change */
88
 
89
#define PFAC_N 3
90
const static u_int16_t pfacts[PFAC_N] = {
91
        2,
92
        3,
93
        2729
94
};
95
 
96
static u_int16_t ru_x;
97
static u_int16_t ru_seed, ru_seed2;
98
static u_int16_t ru_a, ru_b;
99
static u_int16_t ru_g;
100
static u_int16_t ru_counter = 0;
101
static u_int16_t ru_msb = 0;
102
static long ru_reseed;
103
static u_int32_t tmp;           /* Storage for unused random */
104
 
105
static u_int16_t pmod __P((u_int16_t, u_int16_t, u_int16_t));
106
static void ip_initid __P((void));
107
u_int16_t ip_randomid __P((void));
108
 
109
/*
110
 * Do a fast modular exponation, returned value will be in the range
111
 * of 0 - (mod-1)
112
 */
113
 
114
#ifdef __STDC__
115
static u_int16_t
116
pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod)
117
#else
118
static u_int16_t
119
pmod(gen, exp, mod)
120
        u_int16_t gen, exp, mod;
121
#endif
122
{
123
        u_int16_t s, t, u;
124
 
125
        s = 1;
126
        t = gen;
127
        u = exp;
128
 
129
        while (u) {
130
                if (u & 1)
131
                        s = (s*t) % mod;
132
                u >>= 1;
133
                t = (t*t) % mod;
134
        }
135
        return (s);
136
}
137
 
138
/*
139
 * Initalizes the seed and chooses a suitable generator. Also toggles
140
 * the msb flag. The msb flag is used to generate two distinct
141
 * cycles of random numbers and thus avoiding reuse of ids.
142
 *
143
 * This function is called from id_randomid() when needed, an
144
 * application does not have to worry about it.
145
 */
146
static void
147
ip_initid(void)
148
{
149
        u_int16_t j, i;
150
        int noprime = 1;
151
        struct timeval time;
152
 
153
        getmicrotime(&time);
154
        read_random((void *) &tmp, sizeof(tmp));
155
        ru_x = (tmp & 0xFFFF) % RU_M;
156
 
157
        /* 15 bits of random seed */
158
        ru_seed = (tmp >> 16) & 0x7FFF;
159
        read_random((void *) &tmp, sizeof(tmp));
160
        ru_seed2 = tmp & 0x7FFF;
161
 
162
        read_random((void *) &tmp, sizeof(tmp));
163
 
164
        /* Determine the LCG we use */
165
        ru_b = (tmp & 0xfffe) | 1;
166
        ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M);
167
        while (ru_b % 3 == 0)
168
          ru_b += 2;
169
 
170
        read_random((void *) &tmp, sizeof(tmp));
171
        j = tmp % RU_N;
172
        tmp = tmp >> 16;
173
 
174
        /*
175
         * Do a fast gcd(j,RU_N-1), so we can find a j with
176
         * gcd(j, RU_N-1) == 1, giving a new generator for
177
         * RU_GEN^j mod RU_N
178
         */
179
 
180
        while (noprime) {
181
                for (i=0; i<PFAC_N; i++)
182
                        if (j%pfacts[i] == 0)
183
                                break;
184
 
185
                if (i>=PFAC_N)
186
                        noprime = 0;
187
                else
188
                        j = (j+1) % RU_N;
189
        }
190
 
191
        ru_g = pmod(RU_GEN,j,RU_N);
192
        ru_counter = 0;
193
 
194
        ru_reseed = time.tv_sec + RU_OUT;
195
        ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
196
}
197
 
198
u_int16_t
199
ip_randomid(void)
200
{
201
        int i, n;
202
        struct timeval time;
203
 
204
        getmicrotime(&time);
205
        if (ru_counter >= RU_MAX || time.tv_sec > ru_reseed)
206
                ip_initid();
207
 
208
        if (!tmp)
209
                read_random((void *) &tmp, sizeof(tmp));
210
 
211
        /* Skip a random number of ids */
212
        n = tmp & 0x3; tmp = tmp >> 2;
213
        if (ru_counter + n >= RU_MAX)
214
                ip_initid();
215
 
216
        for (i = 0; i <= n; i++)
217
                /* Linear Congruential Generator */
218
                ru_x = (ru_a*ru_x + ru_b) % RU_M;
219
 
220
        ru_counter += i;
221
 
222
        return (ru_seed ^ pmod(ru_g,ru_seed2 ^ ru_x,RU_N)) | ru_msb;
223
}
224
 
225
#endif /* RANDOM_IP_ID */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.