1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/cryptohash.h>
4 #include <linux/module.h>
5 #include <linux/cache.h>
6 #include <linux/random.h>
7 #include <linux/hrtimer.h>
8 #include <linux/ktime.h>
9 #include <linux/string.h>
10 #include <linux/net.h>
12 #include <net/secure_seq.h>
14 #define NET_SECRET_SIZE (MD5_MESSAGE_BYTES / 4)
16 static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned;
18 static __always_inline void net_secret_init(void)
20 net_get_random_once(net_secret, sizeof(net_secret));
24 static u32 seq_scale(u32 seq)
27 * As close as possible to RFC 793, which
28 * suggests using a 250 kHz clock.
29 * Further reading shows this assumes 2 Mb/s networks.
30 * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
31 * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
32 * we also need to limit the resolution so that the u32 seq
33 * overlaps less than one time per MSL (2 minutes).
34 * Choosing a clock of 64 ns period is OK. (period of 274 s)
36 return seq + (ktime_to_ns(ktime_get_real()) >> 6);
40 #if IS_ENABLED(CONFIG_IPV6)
41 __u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
42 __be16 sport, __be16 dport)
44 u32 secret[MD5_MESSAGE_BYTES / 4];
45 u32 hash[MD5_DIGEST_WORDS];
49 memcpy(hash, saddr, 16);
50 for (i = 0; i < 4; i++)
51 secret[i] = net_secret[i] + (__force u32)daddr[i];
52 secret[4] = net_secret[4] +
53 (((__force u16)sport << 16) + (__force u16)dport);
54 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
55 secret[i] = net_secret[i];
57 md5_transform(hash, secret);
59 return seq_scale(hash[0]);
61 EXPORT_SYMBOL(secure_tcpv6_sequence_number);
63 u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
66 u32 secret[MD5_MESSAGE_BYTES / 4];
67 u32 hash[MD5_DIGEST_WORDS];
71 memcpy(hash, saddr, 16);
72 for (i = 0; i < 4; i++)
73 secret[i] = net_secret[i] + (__force u32) daddr[i];
74 secret[4] = net_secret[4] + (__force u32)dport;
75 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
76 secret[i] = net_secret[i];
78 md5_transform(hash, secret);
82 EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
86 __u32 secure_ip_id(__be32 daddr)
88 u32 hash[MD5_DIGEST_WORDS];
91 hash[0] = (__force __u32) daddr;
92 hash[1] = net_secret[13];
93 hash[2] = net_secret[14];
94 hash[3] = net_secret[15];
96 md5_transform(hash, net_secret);
101 __u32 secure_ipv6_id(const __be32 daddr[4])
106 memcpy(hash, daddr, 16);
107 md5_transform(hash, net_secret);
112 __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
113 __be16 sport, __be16 dport)
115 u32 hash[MD5_DIGEST_WORDS];
118 hash[0] = (__force u32)saddr;
119 hash[1] = (__force u32)daddr;
120 hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
121 hash[3] = net_secret[15];
123 md5_transform(hash, net_secret);
125 return seq_scale(hash[0]);
128 u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
130 u32 hash[MD5_DIGEST_WORDS];
133 hash[0] = (__force u32)saddr;
134 hash[1] = (__force u32)daddr;
135 hash[2] = (__force u32)dport ^ net_secret[14];
136 hash[3] = net_secret[15];
138 md5_transform(hash, net_secret);
142 EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
145 #if IS_ENABLED(CONFIG_IP_DCCP)
146 u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
147 __be16 sport, __be16 dport)
149 u32 hash[MD5_DIGEST_WORDS];
153 hash[0] = (__force u32)saddr;
154 hash[1] = (__force u32)daddr;
155 hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
156 hash[3] = net_secret[15];
158 md5_transform(hash, net_secret);
160 seq = hash[0] | (((u64)hash[1]) << 32);
161 seq += ktime_to_ns(ktime_get_real());
162 seq &= (1ull << 48) - 1;
166 EXPORT_SYMBOL(secure_dccp_sequence_number);
168 #if IS_ENABLED(CONFIG_IPV6)
169 u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
170 __be16 sport, __be16 dport)
172 u32 secret[MD5_MESSAGE_BYTES / 4];
173 u32 hash[MD5_DIGEST_WORDS];
178 memcpy(hash, saddr, 16);
179 for (i = 0; i < 4; i++)
180 secret[i] = net_secret[i] + daddr[i];
181 secret[4] = net_secret[4] +
182 (((__force u16)sport << 16) + (__force u16)dport);
183 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
184 secret[i] = net_secret[i];
186 md5_transform(hash, secret);
188 seq = hash[0] | (((u64)hash[1]) << 32);
189 seq += ktime_to_ns(ktime_get_real());
190 seq &= (1ull << 48) - 1;
194 EXPORT_SYMBOL(secure_dccpv6_sequence_number);