Module Name:    src
Committed By:   martin
Date:           Sun Mar  7 19:04:31 UTC 2021

Modified Files:
        src/sys/netinet [netbsd-9]: ip6.h ip_input.c tcp_subr.c
        src/sys/netinet6 [netbsd-9]: ip6_id.c ip6_var.h

Log Message:
Pull up following revision(s) (requested by christos in ticket #1226):

        sys/netinet6/ip6_id.c: revision 1.19-1.21
        sys/netinet6/ip6_var.h: revision 1.88
        sys/netinet/ip_input.c: revision 1.400
        sys/netinet/tcp_subr.c: revision 1.285
        sys/netinet/ip6.h: revision 1.30

netinet: Enable random IP fragment ids by default (from riastradh)

netinet: Enable RFC 1948 pseudorandom TCP ISS selection by default.
(from riastradh)

netinet6: Mark randomid unused.

Will make merging and bisection easier if anything goes wrong with
flow label or fragment id randomization changes.
(from riastradh)

netinet/netinet6: Add necessary includes to make these standalone.
(from riastradh)

Replace randomid() by cprng_fast32()


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.25.6.1 src/sys/netinet/ip6.h
cvs rdiff -u -r1.389.2.2 -r1.389.2.3 src/sys/netinet/ip_input.c
cvs rdiff -u -r1.282.4.1 -r1.282.4.2 src/sys/netinet/tcp_subr.c
cvs rdiff -u -r1.18 -r1.18.22.1 src/sys/netinet6/ip6_id.c
cvs rdiff -u -r1.82 -r1.82.2.1 src/sys/netinet6/ip6_var.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/netinet/ip6.h
diff -u src/sys/netinet/ip6.h:1.25 src/sys/netinet/ip6.h:1.25.6.1
--- src/sys/netinet/ip6.h:1.25	Fri May 18 18:52:17 2018
+++ src/sys/netinet/ip6.h	Sun Mar  7 19:04:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip6.h,v 1.25 2018/05/18 18:52:17 maxv Exp $	*/
+/*	$NetBSD: ip6.h,v 1.25.6.1 2021/03/07 19:04:31 martin Exp $	*/
 /*	$KAME: ip6.h,v 1.45 2003/06/05 04:46:38 keiichi Exp $	*/
 
 /*
@@ -64,6 +64,11 @@
 #ifndef _NETINET_IP6_H_
 #define _NETINET_IP6_H_
 
+#include <sys/types.h>
+#include <sys/endian.h>
+
+#include <netinet/in.h>
+
 /*
  * Definition for internet protocol version 6.
  * RFC 2460

Index: src/sys/netinet/ip_input.c
diff -u src/sys/netinet/ip_input.c:1.389.2.2 src/sys/netinet/ip_input.c:1.389.2.3
--- src/sys/netinet/ip_input.c:1.389.2.2	Tue Sep 24 03:10:35 2019
+++ src/sys/netinet/ip_input.c	Sun Mar  7 19:04:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip_input.c,v 1.389.2.2 2019/09/24 03:10:35 martin Exp $	*/
+/*	$NetBSD: ip_input.c,v 1.389.2.3 2021/03/07 19:04:31 martin Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.389.2.2 2019/09/24 03:10:35 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.389.2.3 2021/03/07 19:04:31 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -168,7 +168,7 @@ int ip_directedbcast = 0;
 int ip_allowsrcrt = 0;
 int ip_mtudisc = 1;
 int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
-int ip_do_randomid = 0;
+int ip_do_randomid = 1;
 
 /*
  * XXX - Setting ip_checkinterface mostly implements the receive side of

Index: src/sys/netinet/tcp_subr.c
diff -u src/sys/netinet/tcp_subr.c:1.282.4.1 src/sys/netinet/tcp_subr.c:1.282.4.2
--- src/sys/netinet/tcp_subr.c:1.282.4.1	Tue Sep 10 16:19:00 2019
+++ src/sys/netinet/tcp_subr.c	Sun Mar  7 19:04:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: tcp_subr.c,v 1.282.4.1 2019/09/10 16:19:00 martin Exp $	*/
+/*	$NetBSD: tcp_subr.c,v 1.282.4.2 2021/03/07 19:04:31 martin Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tcp_subr.c,v 1.282.4.1 2019/09/10 16:19:00 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tcp_subr.c,v 1.282.4.2 2021/03/07 19:04:31 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -163,7 +163,7 @@ int 	tcp_mssdflt = TCP_MSS;
 int	tcp_minmss = TCP_MINMSS;
 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
 int	tcp_do_rfc1323 = 1;	/* window scaling / timestamps (obsolete) */
-int	tcp_do_rfc1948 = 0;	/* ISS by cryptographic hash */
+int	tcp_do_rfc1948 = 1;	/* ISS by cryptographic hash */
 int	tcp_do_sack = 1;	/* selective acknowledgement */
 int	tcp_do_win_scale = 1;	/* RFC1323 window scaling */
 int	tcp_do_timestamps = 1;	/* RFC1323 timestamps */

Index: src/sys/netinet6/ip6_id.c
diff -u src/sys/netinet6/ip6_id.c:1.18 src/sys/netinet6/ip6_id.c:1.18.22.1
--- src/sys/netinet6/ip6_id.c:1.18	Fri Aug  7 08:11:33 2015
+++ src/sys/netinet6/ip6_id.c	Sun Mar  7 19:04:31 2021
@@ -1,9 +1,7 @@
-/*	$NetBSD: ip6_id.c,v 1.18 2015/08/07 08:11:33 ozaki-r Exp $	*/
-/*	$KAME: ip6_id.c,v 1.8 2003/09/06 13:41:06 itojun Exp $	*/
-/*	$OpenBSD: ip_id.c,v 1.6 2002/03/15 18:19:52 millert Exp $	*/
+/*	$NetBSD: ip6_id.c,v 1.18.22.1 2021/03/07 19:04:31 martin Exp $	*/
 
-/*
- * Copyright (C) 2003 WIDE Project.
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -14,241 +12,38 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the project nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
  *
- * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/*
- * Copyright 1998 Niels Provos <pro...@citi.umich.edu>
- * All rights reserved.
- *
- * Theo de Raadt <dera...@openbsd.org> came up with the idea of using
- * such a mathematical system to generate more random (yet non-repeating)
- * ids to solve the resolver/named problem.  But Niels designed the
- * actual system based on the constraints.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/*
- * seed = random (bits - 1) bit
- * n = prime, g0 = generator to n,
- * j = random so that gcd(j,n-1) == 1
- * g = g0^j mod n will be a generator again.
- *
- * X[0] = random seed.
- * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
- * with a = 7^(even random) mod m,
- *      b = random with gcd(b,m) == 1
- *      m = constant and a maximal period of m-1.
- *
- * The transaction id is determined by:
- * id[n] = seed xor (g^X[n] mod n)
- *
- * Effectively the id is restricted to the lower (bits - 1) bits, thus
- * yielding two different cycles by toggling the msb on and off.
- * This avoids reuse issues caused by reseeding.
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip6_id.c,v 1.18 2015/08/07 08:11:33 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip6_id.c,v 1.18.22.1 2021/03/07 19:04:31 martin Exp $");
 
-#include <sys/param.h>
+#include <sys/types.h>
 #include <sys/cprng.h>
 
-#include <lib/libkern/libkern.h>
-
-#include <net/if.h>
-#include <netinet/in.h>
-#include <netinet/ip6.h>
 #include <netinet6/ip6_var.h>
 
-struct randomtab {
-	const int	ru_bits; /* resulting bits */
-	const long	ru_out;	/* Time after wich will be reseeded */
-	const u_int32_t ru_max;	/* Uniq cycle, avoid blackjack prediction */
-	const u_int32_t ru_gen;	/* Starting generator */
-	const u_int32_t ru_n;	/* ru_n: prime, ru_n - 1: product of pfacts[] */
-	const u_int32_t ru_agen; /* determine ru_a as ru_agen^(2*rand) */
-	const u_int32_t ru_m;	/* ru_m = 2^x*3^y */
-	const u_int32_t ru_pfacts[4];	/* factors of ru_n */
-
-	u_int32_t ru_counter;
-	u_int32_t ru_msb;
-
-	u_int32_t ru_x;
-	u_int32_t ru_seed, ru_seed2;
-	u_int32_t ru_a, ru_b;
-	u_int32_t ru_g;
-	long ru_reseed;
-};
-
-static struct randomtab randomtab_32 = {
-	.ru_bits = 32,		/* resulting bits */
-	.ru_out = 180,		/* Time after wich will be reseeded */
-	.ru_max = 1000000000,	/* Uniq cycle, avoid blackjack prediction */
-	.ru_gen = 2,		/* Starting generator */
-	.ru_n = 2147483629,	/* RU_N-1 = 2^2*3^2*59652323 */
-	.ru_agen = 7,		/* determine ru_a as RU_AGEN^(2*rand) */
-	.ru_m = 1836660096,	/* RU_M = 2^7*3^15 - don't change */
-	.ru_pfacts = { 2, 3, 59652323, 0 },	/* factors of ru_n */
-};
-
-static struct randomtab randomtab_20 = {
-	.ru_bits = 20,		/* resulting bits */
-	.ru_out = 180,		/* Time after wich will be reseeded */
-	.ru_max = 200000,	/* Uniq cycle, avoid blackjack prediction */
-	.ru_gen = 2,		/* Starting generator */
-	.ru_n = 524269,		/* RU_N-1 = 2^2*3^2*14563 */
-	.ru_agen = 7,		/* determine ru_a as RU_AGEN^(2*rand) */
-	.ru_m = 279936,		/* RU_M = 2^7*3^7 - don't change */
-	.ru_pfacts = { 2, 3, 14563, 0 },	/* factors of ru_n */
-};
-
-static u_int32_t pmod(u_int32_t, u_int32_t, u_int32_t);
-static void initid(struct randomtab *);
-static u_int32_t randomid(struct randomtab *);
-
-/*
- * Do a fast modular exponation, returned value will be in the range
- * of 0 - (mod-1)
- */
-
-static u_int32_t
-pmod(u_int32_t gen, u_int32_t expo, u_int32_t mod)
-{
-	u_int64_t s, t, u;
-
-	s = 1;
-	t = gen;
-	u = expo;
-
-	while (u) {
-		if (u & 1)
-			s = (s * t) % mod;
-		u >>= 1;
-		t = (t * t) % mod;
-	}
-	return (s);
-}
-
-/*
- * Initalizes the seed and chooses a suitable generator. Also toggles
- * the msb flag. The msb flag is used to generate two distinct
- * cycles of random numbers and thus avoiding reuse of ids.
- *
- * This function is called from id_randomid() when needed, an
- * application does not have to worry about it.
- */
-static void
-initid(struct randomtab *p)
-{
-	u_int32_t j, i;
-	int noprime = 1;
-
-	p->ru_x = cprng_fast32() % p->ru_m;
-
-	/* (bits - 1) bits of random seed */
-	p->ru_seed = cprng_fast32() & (~0U >> (32 - p->ru_bits + 1));
-	p->ru_seed2 = cprng_fast32() & (~0U >> (32 - p->ru_bits + 1));
-
-	/* Determine the LCG we use */
-	p->ru_b = (cprng_fast32() & (~0U >> (32 - p->ru_bits))) | 1;
-	p->ru_a = pmod(p->ru_agen,
-	    (cprng_fast32() & (~0U >> (32 - p->ru_bits))) & (~1U), p->ru_m);
-	while (p->ru_b % 3 == 0)
-		p->ru_b += 2;
-
-	j = cprng_fast32() % p->ru_n;
-
-	/*
-	 * Do a fast gcd(j, RU_N - 1), so we can find a j with
-	 * gcd(j, RU_N - 1) == 1, giving a new generator for
-	 * RU_GEN^j mod RU_N
-	 */
-	while (noprime) {
-		for (i = 0; p->ru_pfacts[i] > 0; i++)
-			if (j % p->ru_pfacts[i] == 0)
-				break;
-
-		if (p->ru_pfacts[i] == 0)
-			noprime = 0;
-		else
-			j = (j + 1) % p->ru_n;
-	}
-
-	p->ru_g = pmod(p->ru_gen, j, p->ru_n);
-	p->ru_counter = 0;
-
-	p->ru_reseed = time_uptime + p->ru_out;
-	p->ru_msb = p->ru_msb ? 0 : (1U << (p->ru_bits - 1));
-}
-
-static u_int32_t
-randomid(struct randomtab *p)
-{
-	int i, n;
-
-	if (p->ru_counter >= p->ru_max || time_uptime > p->ru_reseed)
-		initid(p);
-
-	/* Skip a random number of ids */
-	n = cprng_fast32() & 0x3;
-	if (p->ru_counter + n >= p->ru_max)
-		initid(p);
-
-	for (i = 0; i <= n; i++) {
-		/* Linear Congruential Generator */
-		p->ru_x = ((u_int64_t)p->ru_a * p->ru_x + p->ru_b) % p->ru_m;
-	}
-
-	p->ru_counter += i;
-
-	return (p->ru_seed ^ pmod(p->ru_g, p->ru_seed2 + p->ru_x, p->ru_n)) |
-	    p->ru_msb;
-}
-
-u_int32_t
+uint32_t
 ip6_randomid(void)
 {
 
-	return randomid(&randomtab_32);
+	return cprng_fast32();
 }
 
-u_int32_t
+uint32_t
 ip6_randomflowlabel(void)
 {
 
-	return randomid(&randomtab_20) & 0xfffff;
+	return cprng_fast32() & 0xfffff;
 }

Index: src/sys/netinet6/ip6_var.h
diff -u src/sys/netinet6/ip6_var.h:1.82 src/sys/netinet6/ip6_var.h:1.82.2.1
--- src/sys/netinet6/ip6_var.h:1.82	Mon May 13 07:47:59 2019
+++ src/sys/netinet6/ip6_var.h	Sun Mar  7 19:04:31 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ip6_var.h,v 1.82 2019/05/13 07:47:59 ozaki-r Exp $	*/
+/*	$NetBSD: ip6_var.h,v 1.82.2.1 2021/03/07 19:04:31 martin Exp $	*/
 /*	$KAME: ip6_var.h,v 1.33 2000/06/11 14:59:20 jinmei Exp $	*/
 
 /*
@@ -64,9 +64,16 @@
 #ifndef _NETINET6_IP6_VAR_H_
 #define _NETINET6_IP6_VAR_H_
 
+#include <sys/types.h>
+#include <sys/queue.h>
 #include <sys/socketvar.h>
+
+#include <net/if.h>
 #include <net/route.h>
 
+#include <netinet/in.h>
+#include <netinet/ip6.h>
+
 struct	ip6_moptions {
 	if_index_t im6o_multicast_if_index; /* I/F for outgoing multicasts */
 	u_char	im6o_multicast_hlim;	/* hoplimit for outgoing multicasts */
@@ -212,6 +219,9 @@ struct ip6flow {
 };
 
 #ifdef _KERNEL
+
+#include <sys/protosw.h>
+
 /*
  * Auxiliary attributes of incoming IPv6 packets, which is initialized when we
  * come into ip6_input().

Reply via email to