tcp_rndiss.c revision 1.2.2.2 1 1.2.2.2 wrstuden /* $OpenBSD: tcp_subr.c,v 1.98 2007/06/25 12:17:43 markus Exp $ */
2 1.2.2.2 wrstuden /* $NetBSD: tcp_rndiss.c,v 1.2.2.2 2008/06/24 05:01:07 wrstuden Exp $ */
3 1.2.2.2 wrstuden
4 1.2.2.2 wrstuden /*
5 1.2.2.2 wrstuden * Copyright (c) 1982, 1986, 1988, 1990, 1993
6 1.2.2.2 wrstuden * The Regents of the University of California. All rights reserved.
7 1.2.2.2 wrstuden *
8 1.2.2.2 wrstuden * Redistribution and use in source and binary forms, with or without
9 1.2.2.2 wrstuden * modification, are permitted provided that the following conditions
10 1.2.2.2 wrstuden * are met:
11 1.2.2.2 wrstuden * 1. Redistributions of source code must retain the above copyright
12 1.2.2.2 wrstuden * notice, this list of conditions and the following disclaimer.
13 1.2.2.2 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.2.2 wrstuden * notice, this list of conditions and the following disclaimer in the
15 1.2.2.2 wrstuden * documentation and/or other materials provided with the distribution.
16 1.2.2.2 wrstuden * 3. Neither the name of the University nor the names of its contributors
17 1.2.2.2 wrstuden * may be used to endorse or promote products derived from this software
18 1.2.2.2 wrstuden * without specific prior written permission.
19 1.2.2.2 wrstuden *
20 1.2.2.2 wrstuden * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 1.2.2.2 wrstuden * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2.2.2 wrstuden * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2.2.2 wrstuden * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 1.2.2.2 wrstuden * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2.2.2 wrstuden * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2.2.2 wrstuden * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2.2.2 wrstuden * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2.2.2 wrstuden * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2.2.2 wrstuden * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2.2.2 wrstuden * SUCH DAMAGE.
31 1.2.2.2 wrstuden *
32 1.2.2.2 wrstuden * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
33 1.2.2.2 wrstuden *
34 1.2.2.2 wrstuden * NRL grants permission for redistribution and use in source and binary
35 1.2.2.2 wrstuden * forms, with or without modification, of the software and documentation
36 1.2.2.2 wrstuden * created at NRL provided that the following conditions are met:
37 1.2.2.2 wrstuden *
38 1.2.2.2 wrstuden * 1. Redistributions of source code must retain the above copyright
39 1.2.2.2 wrstuden * notice, this list of conditions and the following disclaimer.
40 1.2.2.2 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
41 1.2.2.2 wrstuden * notice, this list of conditions and the following disclaimer in the
42 1.2.2.2 wrstuden * documentation and/or other materials provided with the distribution.
43 1.2.2.2 wrstuden * 3. All advertising materials mentioning features or use of this software
44 1.2.2.2 wrstuden * must display the following acknowledgements:
45 1.2.2.2 wrstuden * This product includes software developed by the University of
46 1.2.2.2 wrstuden * California, Berkeley and its contributors.
47 1.2.2.2 wrstuden * This product includes software developed at the Information
48 1.2.2.2 wrstuden * Technology Division, US Naval Research Laboratory.
49 1.2.2.2 wrstuden * 4. Neither the name of the NRL nor the names of its contributors
50 1.2.2.2 wrstuden * may be used to endorse or promote products derived from this software
51 1.2.2.2 wrstuden * without specific prior written permission.
52 1.2.2.2 wrstuden *
53 1.2.2.2 wrstuden * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
54 1.2.2.2 wrstuden * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 1.2.2.2 wrstuden * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56 1.2.2.2 wrstuden * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
57 1.2.2.2 wrstuden * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58 1.2.2.2 wrstuden * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 1.2.2.2 wrstuden * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60 1.2.2.2 wrstuden * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61 1.2.2.2 wrstuden * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62 1.2.2.2 wrstuden * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63 1.2.2.2 wrstuden * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 1.2.2.2 wrstuden *
65 1.2.2.2 wrstuden * The views and conclusions contained in the software and documentation
66 1.2.2.2 wrstuden * are those of the authors and should not be interpreted as representing
67 1.2.2.2 wrstuden * official policies, either expressed or implied, of the US Naval
68 1.2.2.2 wrstuden * Research Laboratory (NRL).
69 1.2.2.2 wrstuden */
70 1.2.2.2 wrstuden
71 1.2.2.2 wrstuden #include <sys/cdefs.h>
72 1.2.2.2 wrstuden __KERNEL_RCSID(0, "$NetBSD: tcp_rndiss.c,v 1.2.2.2 2008/06/24 05:01:07 wrstuden Exp $");
73 1.2.2.2 wrstuden
74 1.2.2.2 wrstuden #include <sys/param.h>
75 1.2.2.2 wrstuden #include <sys/rnd.h>
76 1.2.2.2 wrstuden
77 1.2.2.2 wrstuden #include <netinet/tcp.h>
78 1.2.2.2 wrstuden #include <netinet/tcp_seq.h>
79 1.2.2.2 wrstuden #include <netinet/tcp_rndiss.h>
80 1.2.2.2 wrstuden
81 1.2.2.2 wrstuden #define TCP_RNDISS_ROUNDS 16
82 1.2.2.2 wrstuden #define TCP_RNDISS_OUT 7200
83 1.2.2.2 wrstuden #define TCP_RNDISS_MAX 30000
84 1.2.2.2 wrstuden
85 1.2.2.2 wrstuden u_int8_t tcp_rndiss_sbox[128];
86 1.2.2.2 wrstuden u_int16_t tcp_rndiss_msb;
87 1.2.2.2 wrstuden u_int16_t tcp_rndiss_cnt;
88 1.2.2.2 wrstuden long tcp_rndiss_reseed;
89 1.2.2.2 wrstuden
90 1.2.2.2 wrstuden u_int16_t
91 1.2.2.2 wrstuden tcp_rndiss_encrypt(u_int16_t val)
92 1.2.2.2 wrstuden {
93 1.2.2.2 wrstuden u_int16_t sum = 0, i;
94 1.2.2.2 wrstuden
95 1.2.2.2 wrstuden for (i = 0; i < TCP_RNDISS_ROUNDS; i++) {
96 1.2.2.2 wrstuden sum += 0x79b9;
97 1.2.2.2 wrstuden val ^= ((u_int16_t)tcp_rndiss_sbox[(val^sum) & 0x7f]) << 7;
98 1.2.2.2 wrstuden val = ((val & 0xff) << 7) | (val >> 8);
99 1.2.2.2 wrstuden }
100 1.2.2.2 wrstuden
101 1.2.2.2 wrstuden return val;
102 1.2.2.2 wrstuden }
103 1.2.2.2 wrstuden
104 1.2.2.2 wrstuden void
105 1.2.2.2 wrstuden tcp_rndiss_init(void)
106 1.2.2.2 wrstuden {
107 1.2.2.2 wrstuden rnd_extract_data(tcp_rndiss_sbox, sizeof(tcp_rndiss_sbox),
108 1.2.2.2 wrstuden RND_EXTRACT_ANY);
109 1.2.2.2 wrstuden
110 1.2.2.2 wrstuden tcp_rndiss_reseed = time_second + TCP_RNDISS_OUT;
111 1.2.2.2 wrstuden tcp_rndiss_msb = tcp_rndiss_msb == 0x8000 ? 0 : 0x8000;
112 1.2.2.2 wrstuden tcp_rndiss_cnt = 0;
113 1.2.2.2 wrstuden }
114 1.2.2.2 wrstuden
115 1.2.2.2 wrstuden tcp_seq
116 1.2.2.2 wrstuden tcp_rndiss_next(void)
117 1.2.2.2 wrstuden {
118 1.2.2.2 wrstuden if (tcp_rndiss_cnt >= TCP_RNDISS_MAX ||
119 1.2.2.2 wrstuden time_second > tcp_rndiss_reseed)
120 1.2.2.2 wrstuden tcp_rndiss_init();
121 1.2.2.2 wrstuden
122 1.2.2.2 wrstuden /* (arc4random() & 0x7fff) ensures a 32768 byte gap between ISS */
123 1.2.2.2 wrstuden return ((tcp_rndiss_encrypt(tcp_rndiss_cnt++) | tcp_rndiss_msb) <<16) |
124 1.2.2.2 wrstuden (arc4random() & 0x7fff);
125 1.2.2.2 wrstuden }
126