]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/core/secure_seq.c
tcp: randomize tcp timestamp offsets for each connection
[mirror_ubuntu-artful-kernel.git] / net / core / secure_seq.c
CommitLineData
6e5714ea
DM
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>
e34c9a69 10#include <linux/net.h>
6e5714ea
DM
11
12#include <net/secure_seq.h>
13
cb03db9d 14#if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
9a3bab6b 15#define NET_SECRET_SIZE (MD5_MESSAGE_BYTES / 4)
6e5714ea 16
9a3bab6b
ED
17static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned;
18
34d92d53 19static __always_inline void net_secret_init(void)
6e5714ea 20{
e34c9a69 21 net_get_random_once(net_secret, sizeof(net_secret));
6e5714ea 22}
cb03db9d 23#endif
6e5714ea 24
68109090 25#ifdef CONFIG_INET
6e5714ea
DM
26static u32 seq_scale(u32 seq)
27{
28 /*
29 * As close as possible to RFC 793, which
30 * suggests using a 250 kHz clock.
31 * Further reading shows this assumes 2 Mb/s networks.
32 * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
33 * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
34 * we also need to limit the resolution so that the u32 seq
35 * overlaps less than one time per MSL (2 minutes).
36 * Choosing a clock of 64 ns period is OK. (period of 274 s)
37 */
d2de875c 38 return seq + (ktime_get_real_ns() >> 6);
6e5714ea 39}
68109090 40#endif
6e5714ea 41
dfd56b8b 42#if IS_ENABLED(CONFIG_IPV6)
95a22cae
FW
43u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
44 __be16 sport, __be16 dport, u32 *tsoff)
6e5714ea
DM
45{
46 u32 secret[MD5_MESSAGE_BYTES / 4];
47 u32 hash[MD5_DIGEST_WORDS];
48 u32 i;
49
9a3bab6b 50 net_secret_init();
6e5714ea
DM
51 memcpy(hash, saddr, 16);
52 for (i = 0; i < 4; i++)
747465ef 53 secret[i] = net_secret[i] + (__force u32)daddr[i];
6e5714ea
DM
54 secret[4] = net_secret[4] +
55 (((__force u16)sport << 16) + (__force u16)dport);
56 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
57 secret[i] = net_secret[i];
58
59 md5_transform(hash, secret);
60
95a22cae 61 *tsoff = hash[1];
6e5714ea
DM
62 return seq_scale(hash[0]);
63}
64EXPORT_SYMBOL(secure_tcpv6_sequence_number);
65
66u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
67 __be16 dport)
68{
69 u32 secret[MD5_MESSAGE_BYTES / 4];
70 u32 hash[MD5_DIGEST_WORDS];
71 u32 i;
72
9a3bab6b 73 net_secret_init();
6e5714ea
DM
74 memcpy(hash, saddr, 16);
75 for (i = 0; i < 4; i++)
76 secret[i] = net_secret[i] + (__force u32) daddr[i];
77 secret[4] = net_secret[4] + (__force u32)dport;
78 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
79 secret[i] = net_secret[i];
80
81 md5_transform(hash, secret);
82
83 return hash[0];
84}
58a317f1 85EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
6e5714ea
DM
86#endif
87
88#ifdef CONFIG_INET
6e5714ea 89
95a22cae
FW
90u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
91 __be16 sport, __be16 dport, u32 *tsoff)
6e5714ea
DM
92{
93 u32 hash[MD5_DIGEST_WORDS];
94
9a3bab6b 95 net_secret_init();
6e5714ea
DM
96 hash[0] = (__force u32)saddr;
97 hash[1] = (__force u32)daddr;
98 hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
99 hash[3] = net_secret[15];
100
101 md5_transform(hash, net_secret);
102
95a22cae 103 *tsoff = hash[1];
6e5714ea
DM
104 return seq_scale(hash[0]);
105}
106
107u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
108{
109 u32 hash[MD5_DIGEST_WORDS];
110
9a3bab6b 111 net_secret_init();
6e5714ea
DM
112 hash[0] = (__force u32)saddr;
113 hash[1] = (__force u32)daddr;
114 hash[2] = (__force u32)dport ^ net_secret[14];
115 hash[3] = net_secret[15];
116
117 md5_transform(hash, net_secret);
118
119 return hash[0];
120}
121EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
122#endif
123
a3bf7ae9 124#if IS_ENABLED(CONFIG_IP_DCCP)
6e5714ea
DM
125u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
126 __be16 sport, __be16 dport)
127{
128 u32 hash[MD5_DIGEST_WORDS];
129 u64 seq;
130
9a3bab6b 131 net_secret_init();
6e5714ea
DM
132 hash[0] = (__force u32)saddr;
133 hash[1] = (__force u32)daddr;
134 hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
135 hash[3] = net_secret[15];
136
137 md5_transform(hash, net_secret);
138
139 seq = hash[0] | (((u64)hash[1]) << 32);
d2de875c 140 seq += ktime_get_real_ns();
6e5714ea
DM
141 seq &= (1ull << 48) - 1;
142
143 return seq;
144}
145EXPORT_SYMBOL(secure_dccp_sequence_number);
146
dfd56b8b 147#if IS_ENABLED(CONFIG_IPV6)
6e5714ea
DM
148u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
149 __be16 sport, __be16 dport)
150{
151 u32 secret[MD5_MESSAGE_BYTES / 4];
152 u32 hash[MD5_DIGEST_WORDS];
153 u64 seq;
154 u32 i;
155
9a3bab6b 156 net_secret_init();
6e5714ea
DM
157 memcpy(hash, saddr, 16);
158 for (i = 0; i < 4; i++)
68319052 159 secret[i] = net_secret[i] + (__force u32)daddr[i];
6e5714ea
DM
160 secret[4] = net_secret[4] +
161 (((__force u16)sport << 16) + (__force u16)dport);
162 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
163 secret[i] = net_secret[i];
164
165 md5_transform(hash, secret);
166
167 seq = hash[0] | (((u64)hash[1]) << 32);
d2de875c 168 seq += ktime_get_real_ns();
6e5714ea
DM
169 seq &= (1ull << 48) - 1;
170
171 return seq;
172}
173EXPORT_SYMBOL(secure_dccpv6_sequence_number);
174#endif
175#endif