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