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