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