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