GNU Linux-libre 5.4.207-gnu1
[releases.git] / net / core / secure_seq.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5
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>
18
19 #if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
20 #include <linux/in6.h>
21 #include <net/tcp.h>
22
23 static siphash_key_t net_secret __read_mostly;
24 static siphash_key_t ts_secret __read_mostly;
25
26 #define EPHEMERAL_PORT_SHUFFLE_PERIOD (10 * HZ)
27
28 static __always_inline void net_secret_init(void)
29 {
30         net_get_random_once(&net_secret, sizeof(net_secret));
31 }
32
33 static __always_inline void ts_secret_init(void)
34 {
35         net_get_random_once(&ts_secret, sizeof(ts_secret));
36 }
37 #endif
38
39 #ifdef CONFIG_INET
40 static u32 seq_scale(u32 seq)
41 {
42         /*
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)
51          */
52         return seq + (ktime_get_real_ns() >> 6);
53 }
54 #endif
55
56 #if IS_ENABLED(CONFIG_IPV6)
57 u32 secure_tcpv6_ts_off(const struct net *net,
58                         const __be32 *saddr, const __be32 *daddr)
59 {
60         const struct {
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,
66         };
67
68         if (net->ipv4.sysctl_tcp_timestamps != 1)
69                 return 0;
70
71         ts_secret_init();
72         return siphash(&combined, offsetofend(typeof(combined), daddr),
73                        &ts_secret);
74 }
75 EXPORT_SYMBOL(secure_tcpv6_ts_off);
76
77 u32 secure_tcpv6_seq(const __be32 *saddr, const __be32 *daddr,
78                      __be16 sport, __be16 dport)
79 {
80         const struct {
81                 struct in6_addr saddr;
82                 struct in6_addr daddr;
83                 __be16 sport;
84                 __be16 dport;
85         } __aligned(SIPHASH_ALIGNMENT) combined = {
86                 .saddr = *(struct in6_addr *)saddr,
87                 .daddr = *(struct in6_addr *)daddr,
88                 .sport = sport,
89                 .dport = dport
90         };
91         u32 hash;
92
93         net_secret_init();
94         hash = siphash(&combined, offsetofend(typeof(combined), dport),
95                        &net_secret);
96         return seq_scale(hash);
97 }
98 EXPORT_SYMBOL(secure_tcpv6_seq);
99
100 u64 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
101                                __be16 dport)
102 {
103         const struct {
104                 struct in6_addr saddr;
105                 struct in6_addr daddr;
106                 unsigned int timeseed;
107                 __be16 dport;
108         } __aligned(SIPHASH_ALIGNMENT) combined = {
109                 .saddr = *(struct in6_addr *)saddr,
110                 .daddr = *(struct in6_addr *)daddr,
111                 .timeseed = jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
112                 .dport = dport,
113         };
114         net_secret_init();
115         return siphash(&combined, offsetofend(typeof(combined), dport),
116                        &net_secret);
117 }
118 EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
119 #endif
120
121 #ifdef CONFIG_INET
122 u32 secure_tcp_ts_off(const struct net *net, __be32 saddr, __be32 daddr)
123 {
124         if (net->ipv4.sysctl_tcp_timestamps != 1)
125                 return 0;
126
127         ts_secret_init();
128         return siphash_2u32((__force u32)saddr, (__force u32)daddr,
129                             &ts_secret);
130 }
131
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.
136  */
137 u32 secure_tcp_seq(__be32 saddr, __be32 daddr,
138                    __be16 sport, __be16 dport)
139 {
140         u32 hash;
141
142         net_secret_init();
143         hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
144                             (__force u32)sport << 16 | (__force u32)dport,
145                             &net_secret);
146         return seq_scale(hash);
147 }
148 EXPORT_SYMBOL_GPL(secure_tcp_seq);
149
150 u64 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
151 {
152         net_secret_init();
153         return siphash_4u32((__force u32)saddr, (__force u32)daddr,
154                             (__force u16)dport,
155                             jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
156                             &net_secret);
157 }
158 EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
159 #endif
160
161 #if IS_ENABLED(CONFIG_IP_DCCP)
162 u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
163                                 __be16 sport, __be16 dport)
164 {
165         u64 seq;
166         net_secret_init();
167         seq = siphash_3u32((__force u32)saddr, (__force u32)daddr,
168                            (__force u32)sport << 16 | (__force u32)dport,
169                            &net_secret);
170         seq += ktime_get_real_ns();
171         seq &= (1ull << 48) - 1;
172         return seq;
173 }
174 EXPORT_SYMBOL(secure_dccp_sequence_number);
175
176 #if IS_ENABLED(CONFIG_IPV6)
177 u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
178                                   __be16 sport, __be16 dport)
179 {
180         const struct {
181                 struct in6_addr saddr;
182                 struct in6_addr daddr;
183                 __be16 sport;
184                 __be16 dport;
185         } __aligned(SIPHASH_ALIGNMENT) combined = {
186                 .saddr = *(struct in6_addr *)saddr,
187                 .daddr = *(struct in6_addr *)daddr,
188                 .sport = sport,
189                 .dport = dport
190         };
191         u64 seq;
192         net_secret_init();
193         seq = siphash(&combined, offsetofend(typeof(combined), dport),
194                       &net_secret);
195         seq += ktime_get_real_ns();
196         seq &= (1ull << 48) - 1;
197         return seq;
198 }
199 EXPORT_SYMBOL(secure_dccpv6_sequence_number);
200 #endif
201 #endif