1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/cryptohash.h>
9 #include <linux/module.h>
10 #include <linux/cache.h>
11 #include <linux/random.h>
12 #include <linux/hrtimer.h>
13 #include <linux/ktime.h>
14 #include <linux/string.h>
15 #include <linux/net.h>
16 #include <linux/siphash.h>
17 #include <net/secure_seq.h>
19 #if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
20 #include <linux/in6.h>
23 static siphash_key_t net_secret __read_mostly;
24 static siphash_key_t ts_secret __read_mostly;
26 #define EPHEMERAL_PORT_SHUFFLE_PERIOD (10 * HZ)
28 static __always_inline void net_secret_init(void)
30 net_get_random_once(&net_secret, sizeof(net_secret));
33 static __always_inline void ts_secret_init(void)
35 net_get_random_once(&ts_secret, sizeof(ts_secret));
40 static u32 seq_scale(u32 seq)
43 * As close as possible to RFC 793, which
44 * suggests using a 250 kHz clock.
45 * Further reading shows this assumes 2 Mb/s networks.
46 * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
47 * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
48 * we also need to limit the resolution so that the u32 seq
49 * overlaps less than one time per MSL (2 minutes).
50 * Choosing a clock of 64 ns period is OK. (period of 274 s)
52 return seq + (ktime_get_real_ns() >> 6);
56 #if IS_ENABLED(CONFIG_IPV6)
57 u32 secure_tcpv6_ts_off(const struct net *net,
58 const __be32 *saddr, const __be32 *daddr)
61 struct in6_addr saddr;
62 struct in6_addr daddr;
63 } __aligned(SIPHASH_ALIGNMENT) combined = {
64 .saddr = *(struct in6_addr *)saddr,
65 .daddr = *(struct in6_addr *)daddr,
68 if (net->ipv4.sysctl_tcp_timestamps != 1)
72 return siphash(&combined, offsetofend(typeof(combined), daddr),
75 EXPORT_SYMBOL(secure_tcpv6_ts_off);
77 u32 secure_tcpv6_seq(const __be32 *saddr, const __be32 *daddr,
78 __be16 sport, __be16 dport)
81 struct in6_addr saddr;
82 struct in6_addr daddr;
85 } __aligned(SIPHASH_ALIGNMENT) combined = {
86 .saddr = *(struct in6_addr *)saddr,
87 .daddr = *(struct in6_addr *)daddr,
94 hash = siphash(&combined, offsetofend(typeof(combined), dport),
96 return seq_scale(hash);
98 EXPORT_SYMBOL(secure_tcpv6_seq);
100 u64 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
104 struct in6_addr saddr;
105 struct in6_addr daddr;
106 unsigned int timeseed;
108 } __aligned(SIPHASH_ALIGNMENT) combined = {
109 .saddr = *(struct in6_addr *)saddr,
110 .daddr = *(struct in6_addr *)daddr,
111 .timeseed = jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
115 return siphash(&combined, offsetofend(typeof(combined), dport),
118 EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
122 u32 secure_tcp_ts_off(const struct net *net, __be32 saddr, __be32 daddr)
124 if (net->ipv4.sysctl_tcp_timestamps != 1)
128 return siphash_2u32((__force u32)saddr, (__force u32)daddr,
132 /* secure_tcp_seq_and_tsoff(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
133 * but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
134 * it would be easy enough to have the former function use siphash_4u32, passing
135 * the arguments as separate u32.
137 u32 secure_tcp_seq(__be32 saddr, __be32 daddr,
138 __be16 sport, __be16 dport)
143 hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
144 (__force u32)sport << 16 | (__force u32)dport,
146 return seq_scale(hash);
148 EXPORT_SYMBOL_GPL(secure_tcp_seq);
150 u64 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
153 return siphash_4u32((__force u32)saddr, (__force u32)daddr,
155 jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
158 EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
161 #if IS_ENABLED(CONFIG_IP_DCCP)
162 u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
163 __be16 sport, __be16 dport)
167 seq = siphash_3u32((__force u32)saddr, (__force u32)daddr,
168 (__force u32)sport << 16 | (__force u32)dport,
170 seq += ktime_get_real_ns();
171 seq &= (1ull << 48) - 1;
174 EXPORT_SYMBOL(secure_dccp_sequence_number);
176 #if IS_ENABLED(CONFIG_IPV6)
177 u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
178 __be16 sport, __be16 dport)
181 struct in6_addr saddr;
182 struct in6_addr daddr;
185 } __aligned(SIPHASH_ALIGNMENT) combined = {
186 .saddr = *(struct in6_addr *)saddr,
187 .daddr = *(struct in6_addr *)daddr,
193 seq = siphash(&combined, offsetofend(typeof(combined), dport),
195 seq += ktime_get_real_ns();
196 seq &= (1ull << 48) - 1;
199 EXPORT_SYMBOL(secure_dccpv6_sequence_number);