secure_seq.c 4.9 KB
Newer Older
1 2 3 4
/*
 * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 */

5 6 7 8 9 10 11 12 13
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cryptohash.h>
#include <linux/module.h>
#include <linux/cache.h>
#include <linux/random.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/string.h>
14
#include <linux/net.h>
15
#include <linux/siphash.h>
16 17
#include <net/secure_seq.h>

18
#if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
19
#include <linux/in6.h>
20
#include <net/tcp.h>
21

22
static siphash_key_t net_secret __read_mostly;
23
static siphash_key_t ts_secret __read_mostly;
24

25
static __always_inline void net_secret_init(void)
26
{
27
	net_get_random_once(&net_secret, sizeof(net_secret));
28
}
29 30 31 32 33

static __always_inline void ts_secret_init(void)
{
	net_get_random_once(&ts_secret, sizeof(ts_secret));
}
34
#endif
35

36
#ifdef CONFIG_INET
37 38 39 40 41 42 43 44 45 46 47 48
static u32 seq_scale(u32 seq)
{
	/*
	 *	As close as possible to RFC 793, which
	 *	suggests using a 250 kHz clock.
	 *	Further reading shows this assumes 2 Mb/s networks.
	 *	For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
	 *	For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
	 *	we also need to limit the resolution so that the u32 seq
	 *	overlaps less than one time per MSL (2 minutes).
	 *	Choosing a clock of 64 ns period is OK. (period of 274 s)
	 */
49
	return seq + (ktime_get_real_ns() >> 6);
50
}
51
#endif
52

E
Eric Dumazet 已提交
53
#if IS_ENABLED(CONFIG_IPV6)
54 55
u32 secure_tcpv6_ts_off(const struct net *net,
			const __be32 *saddr, const __be32 *daddr)
56 57 58 59 60 61 62 63 64
{
	const struct {
		struct in6_addr saddr;
		struct in6_addr daddr;
	} __aligned(SIPHASH_ALIGNMENT) combined = {
		.saddr = *(struct in6_addr *)saddr,
		.daddr = *(struct in6_addr *)daddr,
	};

65
	if (net->ipv4.sysctl_tcp_timestamps != 1)
66 67
		return 0;

68
	ts_secret_init();
69 70 71
	return siphash(&combined, offsetofend(typeof(combined), daddr),
		       &ts_secret);
}
72
EXPORT_SYMBOL(secure_tcpv6_ts_off);
73

74 75
u32 secure_tcpv6_seq(const __be32 *saddr, const __be32 *daddr,
		     __be16 sport, __be16 dport)
76
{
77 78 79 80 81 82 83 84 85 86 87
	const struct {
		struct in6_addr saddr;
		struct in6_addr daddr;
		__be16 sport;
		__be16 dport;
	} __aligned(SIPHASH_ALIGNMENT) combined = {
		.saddr = *(struct in6_addr *)saddr,
		.daddr = *(struct in6_addr *)daddr,
		.sport = sport,
		.dport = dport
	};
88 89
	u32 hash;

90
	net_secret_init();
91 92 93
	hash = siphash(&combined, offsetofend(typeof(combined), dport),
		       &net_secret);
	return seq_scale(hash);
94
}
95
EXPORT_SYMBOL(secure_tcpv6_seq);
96 97 98 99

u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
			       __be16 dport)
{
100 101 102 103 104 105 106 107 108
	const struct {
		struct in6_addr saddr;
		struct in6_addr daddr;
		__be16 dport;
	} __aligned(SIPHASH_ALIGNMENT) combined = {
		.saddr = *(struct in6_addr *)saddr,
		.daddr = *(struct in6_addr *)daddr,
		.dport = dport
	};
109
	net_secret_init();
110 111
	return siphash(&combined, offsetofend(typeof(combined), dport),
		       &net_secret);
112
}
113
EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
114 115 116
#endif

#ifdef CONFIG_INET
117
u32 secure_tcp_ts_off(const struct net *net, __be32 saddr, __be32 daddr)
118
{
119
	if (net->ipv4.sysctl_tcp_timestamps != 1)
120 121
		return 0;

122
	ts_secret_init();
123 124 125
	return siphash_2u32((__force u32)saddr, (__force u32)daddr,
			    &ts_secret);
}
126

127
/* secure_tcp_seq_and_tsoff(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
128 129 130 131
 * but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
 * it would be easy enough to have the former function use siphash_4u32, passing
 * the arguments as separate u32.
 */
132 133
u32 secure_tcp_seq(__be32 saddr, __be32 daddr,
		   __be16 sport, __be16 dport)
134
{
135 136
	u32 hash;

137
	net_secret_init();
138 139 140 141
	hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
			    (__force u32)sport << 16 | (__force u32)dport,
			    &net_secret);
	return seq_scale(hash);
142 143 144 145
}

u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
{
146
	net_secret_init();
147 148
	return siphash_3u32((__force u32)saddr, (__force u32)daddr,
			    (__force u16)dport, &net_secret);
149 150 151 152
}
EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
#endif

I
Igor Maravić 已提交
153
#if IS_ENABLED(CONFIG_IP_DCCP)
154 155 156 157
u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
				__be16 sport, __be16 dport)
{
	u64 seq;
158
	net_secret_init();
E
Eric Dumazet 已提交
159 160 161
	seq = siphash_3u32((__force u32)saddr, (__force u32)daddr,
			   (__force u32)sport << 16 | (__force u32)dport,
			   &net_secret);
162
	seq += ktime_get_real_ns();
163 164 165 166 167
	seq &= (1ull << 48) - 1;
	return seq;
}
EXPORT_SYMBOL(secure_dccp_sequence_number);

E
Eric Dumazet 已提交
168
#if IS_ENABLED(CONFIG_IPV6)
169 170 171
u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
				  __be16 sport, __be16 dport)
{
172 173 174 175 176 177 178 179 180 181 182
	const struct {
		struct in6_addr saddr;
		struct in6_addr daddr;
		__be16 sport;
		__be16 dport;
	} __aligned(SIPHASH_ALIGNMENT) combined = {
		.saddr = *(struct in6_addr *)saddr,
		.daddr = *(struct in6_addr *)daddr,
		.sport = sport,
		.dport = dport
	};
183
	u64 seq;
184
	net_secret_init();
185 186
	seq = siphash(&combined, offsetofend(typeof(combined), dport),
		      &net_secret);
187
	seq += ktime_get_real_ns();
188 189 190 191 192 193
	seq &= (1ull << 48) - 1;
	return seq;
}
EXPORT_SYMBOL(secure_dccpv6_sequence_number);
#endif
#endif