2017-01-08 12:54:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2011-08-04 03:50:44 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/cryptohash.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/cache.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/hrtimer.h>
|
|
|
|
#include <linux/ktime.h>
|
|
|
|
#include <linux/string.h>
|
2013-10-19 19:48:59 +00:00
|
|
|
#include <linux/net.h>
|
2017-01-08 12:54:02 +00:00
|
|
|
#include <linux/siphash.h>
|
2011-08-04 03:50:44 +00:00
|
|
|
#include <net/secure_seq.h>
|
|
|
|
|
2013-10-05 20:56:59 +00:00
|
|
|
#if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
|
2017-01-08 12:54:02 +00:00
|
|
|
#include <linux/in6.h>
|
2016-12-01 10:32:07 +00:00
|
|
|
#include <net/tcp.h>
|
2011-08-04 03:50:44 +00:00
|
|
|
|
2017-01-08 12:54:02 +00:00
|
|
|
static siphash_key_t net_secret __read_mostly;
|
2013-09-24 13:19:57 +00:00
|
|
|
|
2013-10-23 06:44:50 +00:00
|
|
|
static __always_inline void net_secret_init(void)
|
2011-08-04 03:50:44 +00:00
|
|
|
{
|
2017-01-08 12:54:02 +00:00
|
|
|
net_get_random_once(&net_secret, sizeof(net_secret));
|
2011-08-04 03:50:44 +00:00
|
|
|
}
|
2013-10-05 20:56:59 +00:00
|
|
|
#endif
|
2011-08-04 03:50:44 +00:00
|
|
|
|
2011-12-06 08:04:40 +00:00
|
|
|
#ifdef CONFIG_INET
|
2011-08-04 03:50:44 +00:00
|
|
|
static u32 seq_scale(u32 seq)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* As close as possible to RFC 793, which
|
|
|
|
* suggests using a 250 kHz clock.
|
|
|
|
* Further reading shows this assumes 2 Mb/s networks.
|
|
|
|
* For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.
|
|
|
|
* For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but
|
|
|
|
* we also need to limit the resolution so that the u32 seq
|
|
|
|
* overlaps less than one time per MSL (2 minutes).
|
|
|
|
* Choosing a clock of 64 ns period is OK. (period of 274 s)
|
|
|
|
*/
|
2014-08-23 01:32:09 +00:00
|
|
|
return seq + (ktime_get_real_ns() >> 6);
|
2011-08-04 03:50:44 +00:00
|
|
|
}
|
2011-12-06 08:04:40 +00:00
|
|
|
#endif
|
2011-08-04 03:50:44 +00:00
|
|
|
|
2011-12-10 09:48:31 +00:00
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
2016-12-01 10:32:06 +00:00
|
|
|
u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
|
|
|
|
__be16 sport, __be16 dport, u32 *tsoff)
|
2011-08-04 03:50:44 +00:00
|
|
|
{
|
2017-01-08 12:54:02 +00:00
|
|
|
const struct {
|
|
|
|
struct in6_addr saddr;
|
|
|
|
struct in6_addr daddr;
|
|
|
|
__be16 sport;
|
|
|
|
__be16 dport;
|
|
|
|
} __aligned(SIPHASH_ALIGNMENT) combined = {
|
|
|
|
.saddr = *(struct in6_addr *)saddr,
|
|
|
|
.daddr = *(struct in6_addr *)daddr,
|
|
|
|
.sport = sport,
|
|
|
|
.dport = dport
|
|
|
|
};
|
|
|
|
u64 hash;
|
2013-09-24 13:19:57 +00:00
|
|
|
net_secret_init();
|
2017-01-08 12:54:02 +00:00
|
|
|
hash = siphash(&combined, offsetofend(typeof(combined), dport),
|
|
|
|
&net_secret);
|
|
|
|
*tsoff = sysctl_tcp_timestamps == 1 ? (hash >> 32) : 0;
|
|
|
|
return seq_scale(hash);
|
2011-08-04 03:50:44 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(secure_tcpv6_sequence_number);
|
|
|
|
|
|
|
|
u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
|
|
|
|
__be16 dport)
|
|
|
|
{
|
2017-01-08 12:54:02 +00:00
|
|
|
const struct {
|
|
|
|
struct in6_addr saddr;
|
|
|
|
struct in6_addr daddr;
|
|
|
|
__be16 dport;
|
|
|
|
} __aligned(SIPHASH_ALIGNMENT) combined = {
|
|
|
|
.saddr = *(struct in6_addr *)saddr,
|
|
|
|
.daddr = *(struct in6_addr *)daddr,
|
|
|
|
.dport = dport
|
|
|
|
};
|
2013-09-24 13:19:57 +00:00
|
|
|
net_secret_init();
|
2017-01-08 12:54:02 +00:00
|
|
|
return siphash(&combined, offsetofend(typeof(combined), dport),
|
|
|
|
&net_secret);
|
2011-08-04 03:50:44 +00:00
|
|
|
}
|
2012-08-26 17:14:12 +00:00
|
|
|
EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
|
2011-08-04 03:50:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_INET
|
|
|
|
|
2017-01-08 12:54:02 +00:00
|
|
|
/* secure_tcp_sequence_number(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
|
|
|
|
* but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
|
|
|
|
* it would be easy enough to have the former function use siphash_4u32, passing
|
|
|
|
* the arguments as separate u32.
|
|
|
|
*/
|
|
|
|
|
2016-12-01 10:32:06 +00:00
|
|
|
u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
|
|
|
|
__be16 sport, __be16 dport, u32 *tsoff)
|
2011-08-04 03:50:44 +00:00
|
|
|
{
|
2017-01-08 12:54:02 +00:00
|
|
|
u64 hash;
|
2013-09-24 13:19:57 +00:00
|
|
|
net_secret_init();
|
2017-01-08 12:54:02 +00:00
|
|
|
hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
|
|
|
|
(__force u32)sport << 16 | (__force u32)dport,
|
|
|
|
&net_secret);
|
|
|
|
*tsoff = sysctl_tcp_timestamps == 1 ? (hash >> 32) : 0;
|
|
|
|
return seq_scale(hash);
|
2011-08-04 03:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
|
|
|
|
{
|
2013-09-24 13:19:57 +00:00
|
|
|
net_secret_init();
|
2017-01-08 12:54:02 +00:00
|
|
|
return siphash_3u32((__force u32)saddr, (__force u32)daddr,
|
|
|
|
(__force u16)dport, &net_secret);
|
2011-08-04 03:50:44 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
|
|
|
|
#endif
|
|
|
|
|
2011-12-12 02:58:22 +00:00
|
|
|
#if IS_ENABLED(CONFIG_IP_DCCP)
|
2011-08-04 03:50:44 +00:00
|
|
|
u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
|
|
|
|
__be16 sport, __be16 dport)
|
|
|
|
{
|
|
|
|
u64 seq;
|
2013-09-24 13:19:57 +00:00
|
|
|
net_secret_init();
|
2017-01-12 02:10:37 +00:00
|
|
|
seq = siphash_3u32((__force u32)saddr, (__force u32)daddr,
|
|
|
|
(__force u32)sport << 16 | (__force u32)dport,
|
|
|
|
&net_secret);
|
2014-08-23 01:32:09 +00:00
|
|
|
seq += ktime_get_real_ns();
|
2011-08-04 03:50:44 +00:00
|
|
|
seq &= (1ull << 48) - 1;
|
|
|
|
return seq;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(secure_dccp_sequence_number);
|
|
|
|
|
2011-12-10 09:48:31 +00:00
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
2011-08-04 03:50:44 +00:00
|
|
|
u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
|
|
|
|
__be16 sport, __be16 dport)
|
|
|
|
{
|
2017-01-08 12:54:02 +00:00
|
|
|
const struct {
|
|
|
|
struct in6_addr saddr;
|
|
|
|
struct in6_addr daddr;
|
|
|
|
__be16 sport;
|
|
|
|
__be16 dport;
|
|
|
|
} __aligned(SIPHASH_ALIGNMENT) combined = {
|
|
|
|
.saddr = *(struct in6_addr *)saddr,
|
|
|
|
.daddr = *(struct in6_addr *)daddr,
|
|
|
|
.sport = sport,
|
|
|
|
.dport = dport
|
|
|
|
};
|
2011-08-04 03:50:44 +00:00
|
|
|
u64 seq;
|
2013-09-24 13:19:57 +00:00
|
|
|
net_secret_init();
|
2017-01-08 12:54:02 +00:00
|
|
|
seq = siphash(&combined, offsetofend(typeof(combined), dport),
|
|
|
|
&net_secret);
|
2014-08-23 01:32:09 +00:00
|
|
|
seq += ktime_get_real_ns();
|
2011-08-04 03:50:44 +00:00
|
|
|
seq &= (1ull << 48) - 1;
|
|
|
|
return seq;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(secure_dccpv6_sequence_number);
|
|
|
|
#endif
|
|
|
|
#endif
|