blob: 758f140b6bedc51669fed973b39ee317c2bf1570 [file] [log] [blame]
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +01001/*
2 * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
3 */
4
David S. Miller6e5714e2011-08-03 20:50:44 -07005#include <linux/kernel.h>
6#include <linux/init.h>
7#include <linux/cryptohash.h>
8#include <linux/module.h>
9#include <linux/cache.h>
10#include <linux/random.h>
11#include <linux/hrtimer.h>
12#include <linux/ktime.h>
13#include <linux/string.h>
Hannes Frederic Sowae34c9a62013-10-19 21:48:59 +020014#include <linux/net.h>
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010015#include <linux/siphash.h>
David S. Miller6e5714e2011-08-03 20:50:44 -070016#include <net/secure_seq.h>
17
Fabio Estevamcb03db92013-10-05 17:56:59 -030018#if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010019#include <linux/in6.h>
Florian Westphal25429d72016-12-01 11:32:07 +010020#include <net/tcp.h>
David S. Miller6e5714e2011-08-03 20:50:44 -070021
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010022static siphash_key_t net_secret __read_mostly;
Eric Dumazet9a3bab62013-09-24 06:19:57 -070023
Hannes Frederic Sowa34d92d52013-10-23 08:44:50 +020024static __always_inline void net_secret_init(void)
David S. Miller6e5714e2011-08-03 20:50:44 -070025{
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010026 net_get_random_once(&net_secret, sizeof(net_secret));
David S. Miller6e5714e2011-08-03 20:50:44 -070027}
Fabio Estevamcb03db92013-10-05 17:56:59 -030028#endif
David S. Miller6e5714e2011-08-03 20:50:44 -070029
Stephen Boyd681090902011-12-06 08:04:40 +000030#ifdef CONFIG_INET
David S. Miller6e5714e2011-08-03 20:50:44 -070031static u32 seq_scale(u32 seq)
32{
33 /*
34 * As close as possible to RFC 793, which
35 * suggests using a 250 kHz clock.
36 * Further reading shows this assumes 2 Mb/s networks.
37 * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
38 * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
39 * we also need to limit the resolution so that the u32 seq
40 * overlaps less than one time per MSL (2 minutes).
41 * Choosing a clock of 64 ns period is OK. (period of 274 s)
42 */
Eric Dumazetd2de8752014-08-22 18:32:09 -070043 return seq + (ktime_get_real_ns() >> 6);
David S. Miller6e5714e2011-08-03 20:50:44 -070044}
Stephen Boyd681090902011-12-06 08:04:40 +000045#endif
David S. Miller6e5714e2011-08-03 20:50:44 -070046
Eric Dumazetdfd56b82011-12-10 09:48:31 +000047#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal95a22ca2016-12-01 11:32:06 +010048u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
49 __be16 sport, __be16 dport, u32 *tsoff)
David S. Miller6e5714e2011-08-03 20:50:44 -070050{
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010051 const struct {
52 struct in6_addr saddr;
53 struct in6_addr daddr;
54 __be16 sport;
55 __be16 dport;
56 } __aligned(SIPHASH_ALIGNMENT) combined = {
57 .saddr = *(struct in6_addr *)saddr,
58 .daddr = *(struct in6_addr *)daddr,
59 .sport = sport,
60 .dport = dport
61 };
62 u64 hash;
Eric Dumazet9a3bab62013-09-24 06:19:57 -070063 net_secret_init();
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010064 hash = siphash(&combined, offsetofend(typeof(combined), dport),
65 &net_secret);
66 *tsoff = sysctl_tcp_timestamps == 1 ? (hash >> 32) : 0;
67 return seq_scale(hash);
David S. Miller6e5714e2011-08-03 20:50:44 -070068}
69EXPORT_SYMBOL(secure_tcpv6_sequence_number);
70
71u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
72 __be16 dport)
73{
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010074 const struct {
75 struct in6_addr saddr;
76 struct in6_addr daddr;
77 __be16 dport;
78 } __aligned(SIPHASH_ALIGNMENT) combined = {
79 .saddr = *(struct in6_addr *)saddr,
80 .daddr = *(struct in6_addr *)daddr,
81 .dport = dport
82 };
Eric Dumazet9a3bab62013-09-24 06:19:57 -070083 net_secret_init();
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010084 return siphash(&combined, offsetofend(typeof(combined), dport),
85 &net_secret);
David S. Miller6e5714e2011-08-03 20:50:44 -070086}
Patrick McHardy58a317f2012-08-26 19:14:12 +020087EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
David S. Miller6e5714e2011-08-03 20:50:44 -070088#endif
89
90#ifdef CONFIG_INET
David S. Miller6e5714e2011-08-03 20:50:44 -070091
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +010092/* secure_tcp_sequence_number(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
93 * but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
94 * it would be easy enough to have the former function use siphash_4u32, passing
95 * the arguments as separate u32.
96 */
97
Florian Westphal95a22ca2016-12-01 11:32:06 +010098u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
99 __be16 sport, __be16 dport, u32 *tsoff)
David S. Miller6e5714e2011-08-03 20:50:44 -0700100{
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +0100101 u64 hash;
Eric Dumazet9a3bab62013-09-24 06:19:57 -0700102 net_secret_init();
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +0100103 hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
104 (__force u32)sport << 16 | (__force u32)dport,
105 &net_secret);
106 *tsoff = sysctl_tcp_timestamps == 1 ? (hash >> 32) : 0;
107 return seq_scale(hash);
David S. Miller6e5714e2011-08-03 20:50:44 -0700108}
109
110u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
111{
Eric Dumazet9a3bab62013-09-24 06:19:57 -0700112 net_secret_init();
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +0100113 return siphash_3u32((__force u32)saddr, (__force u32)daddr,
114 (__force u16)dport, &net_secret);
David S. Miller6e5714e2011-08-03 20:50:44 -0700115}
116EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
117#endif
118
Igor Maravića3bf7ae2011-12-12 02:58:22 +0000119#if IS_ENABLED(CONFIG_IP_DCCP)
David S. Miller6e5714e2011-08-03 20:50:44 -0700120u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
121 __be16 sport, __be16 dport)
122{
David S. Miller6e5714e2011-08-03 20:50:44 -0700123 u64 seq;
Eric Dumazet9a3bab62013-09-24 06:19:57 -0700124 net_secret_init();
Eric Dumazetc1ce1562017-01-11 18:10:37 -0800125 seq = siphash_3u32((__force u32)saddr, (__force u32)daddr,
126 (__force u32)sport << 16 | (__force u32)dport,
127 &net_secret);
Eric Dumazetd2de8752014-08-22 18:32:09 -0700128 seq += ktime_get_real_ns();
David S. Miller6e5714e2011-08-03 20:50:44 -0700129 seq &= (1ull << 48) - 1;
David S. Miller6e5714e2011-08-03 20:50:44 -0700130 return seq;
131}
132EXPORT_SYMBOL(secure_dccp_sequence_number);
133
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000134#if IS_ENABLED(CONFIG_IPV6)
David S. Miller6e5714e2011-08-03 20:50:44 -0700135u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
136 __be16 sport, __be16 dport)
137{
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +0100138 const struct {
139 struct in6_addr saddr;
140 struct in6_addr daddr;
141 __be16 sport;
142 __be16 dport;
143 } __aligned(SIPHASH_ALIGNMENT) combined = {
144 .saddr = *(struct in6_addr *)saddr,
145 .daddr = *(struct in6_addr *)daddr,
146 .sport = sport,
147 .dport = dport
148 };
David S. Miller6e5714e2011-08-03 20:50:44 -0700149 u64 seq;
Eric Dumazet9a3bab62013-09-24 06:19:57 -0700150 net_secret_init();
Jason A. Donenfeld7cd23e52017-01-08 13:54:02 +0100151 seq = siphash(&combined, offsetofend(typeof(combined), dport),
152 &net_secret);
Eric Dumazetd2de8752014-08-22 18:32:09 -0700153 seq += ktime_get_real_ns();
David S. Miller6e5714e2011-08-03 20:50:44 -0700154 seq &= (1ull << 48) - 1;
David S. Miller6e5714e2011-08-03 20:50:44 -0700155 return seq;
156}
157EXPORT_SYMBOL(secure_dccpv6_sequence_number);
158#endif
159#endif