blob: 8d9d05edd2eb1e66024311c62c6b7679c52bdc7d [file] [log] [blame]
David S. Miller6e5714e2011-08-03 20:50:44 -07001#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
11#include <net/secure_seq.h>
12
Fabio Estevamb9396c42013-10-05 17:56:59 -030013#if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
Eric Dumazetbdf831a2013-09-24 06:19:57 -070014#define NET_SECRET_SIZE (MD5_MESSAGE_BYTES / 4)
David S. Miller6e5714e2011-08-03 20:50:44 -070015
Eric Dumazetbdf831a2013-09-24 06:19:57 -070016static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned;
17
18static void net_secret_init(void)
David S. Miller6e5714e2011-08-03 20:50:44 -070019{
Eric Dumazetbdf831a2013-09-24 06:19:57 -070020 u32 tmp;
21 int i;
22
23 if (likely(net_secret[0]))
24 return;
25
26 for (i = NET_SECRET_SIZE; i > 0;) {
27 do {
28 get_random_bytes(&tmp, sizeof(tmp));
29 } while (!tmp);
30 cmpxchg(&net_secret[--i], 0, tmp);
31 }
David S. Miller6e5714e2011-08-03 20:50:44 -070032}
Fabio Estevamb9396c42013-10-05 17:56:59 -030033#endif
David S. Miller6e5714e2011-08-03 20:50:44 -070034
Stephen Boyd681090902011-12-06 08:04:40 +000035#ifdef CONFIG_INET
David S. Miller6e5714e2011-08-03 20:50:44 -070036static u32 seq_scale(u32 seq)
37{
38 /*
39 * As close as possible to RFC 793, which
40 * suggests using a 250 kHz clock.
41 * Further reading shows this assumes 2 Mb/s networks.
42 * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
43 * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
44 * we also need to limit the resolution so that the u32 seq
45 * overlaps less than one time per MSL (2 minutes).
46 * Choosing a clock of 64 ns period is OK. (period of 274 s)
47 */
48 return seq + (ktime_to_ns(ktime_get_real()) >> 6);
49}
Stephen Boyd681090902011-12-06 08:04:40 +000050#endif
David S. Miller6e5714e2011-08-03 20:50:44 -070051
Eric Dumazetdfd56b82011-12-10 09:48:31 +000052#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetcf533ea2011-10-21 05:22:42 -040053__u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
David S. Miller6e5714e2011-08-03 20:50:44 -070054 __be16 sport, __be16 dport)
55{
56 u32 secret[MD5_MESSAGE_BYTES / 4];
57 u32 hash[MD5_DIGEST_WORDS];
58 u32 i;
59
Eric Dumazetbdf831a2013-09-24 06:19:57 -070060 net_secret_init();
David S. Miller6e5714e2011-08-03 20:50:44 -070061 memcpy(hash, saddr, 16);
62 for (i = 0; i < 4; i++)
Eric Dumazet747465e2012-01-16 19:27:39 +000063 secret[i] = net_secret[i] + (__force u32)daddr[i];
David S. Miller6e5714e2011-08-03 20:50:44 -070064 secret[4] = net_secret[4] +
65 (((__force u16)sport << 16) + (__force u16)dport);
66 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
67 secret[i] = net_secret[i];
68
69 md5_transform(hash, secret);
70
71 return seq_scale(hash[0]);
72}
73EXPORT_SYMBOL(secure_tcpv6_sequence_number);
74
75u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
76 __be16 dport)
77{
78 u32 secret[MD5_MESSAGE_BYTES / 4];
79 u32 hash[MD5_DIGEST_WORDS];
80 u32 i;
81
Eric Dumazetbdf831a2013-09-24 06:19:57 -070082 net_secret_init();
David S. Miller6e5714e2011-08-03 20:50:44 -070083 memcpy(hash, saddr, 16);
84 for (i = 0; i < 4; i++)
85 secret[i] = net_secret[i] + (__force u32) daddr[i];
86 secret[4] = net_secret[4] + (__force u32)dport;
87 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
88 secret[i] = net_secret[i];
89
90 md5_transform(hash, secret);
91
92 return hash[0];
93}
Patrick McHardy58a317f2012-08-26 19:14:12 +020094EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
David S. Miller6e5714e2011-08-03 20:50:44 -070095#endif
96
97#ifdef CONFIG_INET
98__u32 secure_ip_id(__be32 daddr)
99{
100 u32 hash[MD5_DIGEST_WORDS];
101
Eric Dumazetbdf831a2013-09-24 06:19:57 -0700102 net_secret_init();
David S. Miller6e5714e2011-08-03 20:50:44 -0700103 hash[0] = (__force __u32) daddr;
104 hash[1] = net_secret[13];
105 hash[2] = net_secret[14];
106 hash[3] = net_secret[15];
107
108 md5_transform(hash, net_secret);
109
110 return hash[0];
111}
112
113__u32 secure_ipv6_id(const __be32 daddr[4])
114{
115 __u32 hash[4];
116
Eric Dumazetbdf831a2013-09-24 06:19:57 -0700117 net_secret_init();
David S. Miller6e5714e2011-08-03 20:50:44 -0700118 memcpy(hash, daddr, 16);
119 md5_transform(hash, net_secret);
120
121 return hash[0];
122}
123
124__u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
125 __be16 sport, __be16 dport)
126{
127 u32 hash[MD5_DIGEST_WORDS];
128
Eric Dumazetbdf831a2013-09-24 06:19:57 -0700129 net_secret_init();
David S. Miller6e5714e2011-08-03 20:50:44 -0700130 hash[0] = (__force u32)saddr;
131 hash[1] = (__force u32)daddr;
132 hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
133 hash[3] = net_secret[15];
134
135 md5_transform(hash, net_secret);
136
137 return seq_scale(hash[0]);
138}
139
140u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
141{
142 u32 hash[MD5_DIGEST_WORDS];
143
Eric Dumazetbdf831a2013-09-24 06:19:57 -0700144 net_secret_init();
David S. Miller6e5714e2011-08-03 20:50:44 -0700145 hash[0] = (__force u32)saddr;
146 hash[1] = (__force u32)daddr;
147 hash[2] = (__force u32)dport ^ net_secret[14];
148 hash[3] = net_secret[15];
149
150 md5_transform(hash, net_secret);
151
152 return hash[0];
153}
154EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
155#endif
156
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000157#if IS_ENABLED(CONFIG_IP_DCCP)
David S. Miller6e5714e2011-08-03 20:50:44 -0700158u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
159 __be16 sport, __be16 dport)
160{
161 u32 hash[MD5_DIGEST_WORDS];
162 u64 seq;
163
Eric Dumazetbdf831a2013-09-24 06:19:57 -0700164 net_secret_init();
David S. Miller6e5714e2011-08-03 20:50:44 -0700165 hash[0] = (__force u32)saddr;
166 hash[1] = (__force u32)daddr;
167 hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
168 hash[3] = net_secret[15];
169
170 md5_transform(hash, net_secret);
171
172 seq = hash[0] | (((u64)hash[1]) << 32);
173 seq += ktime_to_ns(ktime_get_real());
174 seq &= (1ull << 48) - 1;
175
176 return seq;
177}
178EXPORT_SYMBOL(secure_dccp_sequence_number);
179
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000180#if IS_ENABLED(CONFIG_IPV6)
David S. Miller6e5714e2011-08-03 20:50:44 -0700181u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
182 __be16 sport, __be16 dport)
183{
184 u32 secret[MD5_MESSAGE_BYTES / 4];
185 u32 hash[MD5_DIGEST_WORDS];
186 u64 seq;
187 u32 i;
188
Eric Dumazetbdf831a2013-09-24 06:19:57 -0700189 net_secret_init();
David S. Miller6e5714e2011-08-03 20:50:44 -0700190 memcpy(hash, saddr, 16);
191 for (i = 0; i < 4; i++)
192 secret[i] = net_secret[i] + daddr[i];
193 secret[4] = net_secret[4] +
194 (((__force u16)sport << 16) + (__force u16)dport);
195 for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
196 secret[i] = net_secret[i];
197
198 md5_transform(hash, secret);
199
200 seq = hash[0] | (((u64)hash[1]) << 32);
201 seq += ktime_to_ns(ktime_get_real());
202 seq &= (1ull << 48) - 1;
203
204 return seq;
205}
206EXPORT_SYMBOL(secure_dccpv6_sequence_number);
207#endif
208#endif