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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [bsd_tcpip/] [v2_0/] [src/] [sys/] [netinet/] [ip_id.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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