Commit 1e03d32b authored by Dmitry Safonov's avatar Dmitry Safonov Committed by David S. Miller

net/tcp: Add TCP-AO sign to outgoing packets

Using precalculated traffic keys, sign TCP segments as prescribed by
RFC5925. Per RFC, TCP header options are included in sign calculation:
"The TCP header, by default including options, and where the TCP
checksum and TCP-AO MAC fields are set to zero, all in network-
byte order." (5.1.3)

tcp_ao_hash_header() has exclude_options parameter to optionally exclude
TCP header from hash calculation, as described in RFC5925 (9.1), this is
needed for interaction with middleboxes that may change "some TCP
options". This is wired up to AO key flags and setsockopt() later.

Similarly to TCP-MD5 hash TCP segment fragments.

From this moment a user can start sending TCP-AO signed segments with
one of crypto ahash algorithms from supported by Linux kernel. It can
have a user-specified MAC length, to either save TCP option header space
or provide higher protection using a longer signature.
The inbound segments are not yet verified, TCP-AO option is ignored and
they are accepted.
Co-developed-by: default avatarFrancesco Ruggeri <fruggeri@arista.com>
Signed-off-by: default avatarFrancesco Ruggeri <fruggeri@arista.com>
Co-developed-by: default avatarSalam Noureddine <noureddine@arista.com>
Signed-off-by: default avatarSalam Noureddine <noureddine@arista.com>
Signed-off-by: default avatarDmitry Safonov <dima@arista.com>
Acked-by: default avatarDavid Ahern <dsahern@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 7c2ffaf2
...@@ -195,6 +195,7 @@ static_assert((1 << ATO_BITS) > TCP_DELACK_MAX); ...@@ -195,6 +195,7 @@ static_assert((1 << ATO_BITS) > TCP_DELACK_MAX);
#define TCPOPT_SACK 5 /* SACK Block */ #define TCPOPT_SACK 5 /* SACK Block */
#define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */ #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
#define TCPOPT_MD5SIG 19 /* MD5 Signature (RFC2385) */ #define TCPOPT_MD5SIG 19 /* MD5 Signature (RFC2385) */
#define TCPOPT_AO 29 /* Authentication Option (RFC5925) */
#define TCPOPT_MPTCP 30 /* Multipath TCP (RFC6824) */ #define TCPOPT_MPTCP 30 /* Multipath TCP (RFC6824) */
#define TCPOPT_FASTOPEN 34 /* Fast open (RFC7413) */ #define TCPOPT_FASTOPEN 34 /* Fast open (RFC7413) */
#define TCPOPT_EXP 254 /* Experimental */ #define TCPOPT_EXP 254 /* Experimental */
...@@ -2200,6 +2201,9 @@ struct tcp_sock_af_ops { ...@@ -2200,6 +2201,9 @@ struct tcp_sock_af_ops {
int (*ao_calc_key_sk)(struct tcp_ao_key *mkt, u8 *key, int (*ao_calc_key_sk)(struct tcp_ao_key *mkt, u8 *key,
const struct sock *sk, const struct sock *sk,
__be32 sisn, __be32 disn, bool send); __be32 sisn, __be32 disn, bool send);
int (*calc_ao_hash)(char *location, struct tcp_ao_key *ao,
const struct sock *sk, const struct sk_buff *skb,
const u8 *tkey, int hash_offset, u32 sne);
#endif #endif
}; };
...@@ -2253,6 +2257,66 @@ static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops, ...@@ -2253,6 +2257,66 @@ static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
} }
#endif #endif
struct tcp_key {
union {
struct tcp_ao_key *ao_key;
struct tcp_md5sig_key *md5_key;
};
enum {
TCP_KEY_NONE = 0,
TCP_KEY_MD5,
TCP_KEY_AO,
} type;
};
static inline void tcp_get_current_key(const struct sock *sk,
struct tcp_key *out)
{
#if defined(CONFIG_TCP_AO) || defined(CONFIG_TCP_MD5SIG)
const struct tcp_sock *tp = tcp_sk(sk);
#endif
#ifdef CONFIG_TCP_AO
struct tcp_ao_info *ao;
ao = rcu_dereference_protected(tp->ao_info, lockdep_sock_is_held(sk));
if (ao) {
out->ao_key = READ_ONCE(ao->current_key);
out->type = TCP_KEY_AO;
return;
}
#endif
#ifdef CONFIG_TCP_MD5SIG
if (static_branch_unlikely(&tcp_md5_needed.key) &&
rcu_access_pointer(tp->md5sig_info)) {
out->md5_key = tp->af_specific->md5_lookup(sk, sk);
if (out->md5_key) {
out->type = TCP_KEY_MD5;
return;
}
}
#endif
out->type = TCP_KEY_NONE;
}
static inline bool tcp_key_is_md5(const struct tcp_key *key)
{
#ifdef CONFIG_TCP_MD5SIG
if (static_branch_unlikely(&tcp_md5_needed.key) &&
key->type == TCP_KEY_MD5)
return true;
#endif
return false;
}
static inline bool tcp_key_is_ao(const struct tcp_key *key)
{
#ifdef CONFIG_TCP_AO
if (key->type == TCP_KEY_AO)
return true;
#endif
return false;
}
int tcpv4_offload_init(void); int tcpv4_offload_init(void);
void tcp_v4_init(void); void tcp_v4_init(void);
......
...@@ -111,6 +111,13 @@ struct tcp6_ao_context { ...@@ -111,6 +111,13 @@ struct tcp6_ao_context {
struct tcp_sigpool; struct tcp_sigpool;
int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
struct tcp_ao_key *key, struct tcphdr *th,
__u8 *hash_location);
int tcp_ao_hash_skb(unsigned short int family,
char *ao_hash, struct tcp_ao_key *key,
const struct sock *sk, const struct sk_buff *skb,
const u8 *tkey, int hash_offset, u32 sne);
int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family, int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family,
sockptr_t optval, int optlen); sockptr_t optval, int optlen);
int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,
...@@ -126,12 +133,21 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk, ...@@ -126,12 +133,21 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
const struct sock *sk, const struct sock *sk,
__be32 sisn, __be32 disn, bool send); __be32 sisn, __be32 disn, bool send);
int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
const struct sock *sk, const struct sk_buff *skb,
const u8 *tkey, int hash_offset, u32 sne);
/* ipv6 specific functions */ /* ipv6 specific functions */
int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp,
const struct in6_addr *daddr,
const struct in6_addr *saddr, int nbytes);
int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
const struct sock *sk, __be32 sisn, const struct sock *sk, __be32 sisn,
__be32 disn, bool send); __be32 disn, bool send);
struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk,
struct sock *addr_sk, int sndid, int rcvid); struct sock *addr_sk, int sndid, int rcvid);
int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
const struct sock *sk, const struct sk_buff *skb,
const u8 *tkey, int hash_offset, u32 sne);
int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen); int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen);
void tcp_ao_established(struct sock *sk); void tcp_ao_established(struct sock *sk);
void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb); void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb);
...@@ -139,6 +155,13 @@ void tcp_ao_connect_init(struct sock *sk); ...@@ -139,6 +155,13 @@ void tcp_ao_connect_init(struct sock *sk);
#else /* CONFIG_TCP_AO */ #else /* CONFIG_TCP_AO */
static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
struct tcp_ao_key *key, struct tcphdr *th,
__u8 *hash_location)
{
return 0;
}
static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk,
const union tcp_ao_addr *addr, int family, int sndid, int rcvid) const union tcp_ao_addr *addr, int family, int sndid, int rcvid)
{ {
......
...@@ -262,6 +262,171 @@ static int tcp_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key, ...@@ -262,6 +262,171 @@ static int tcp_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
static int tcp_v4_ao_hash_pseudoheader(struct tcp_sigpool *hp,
__be32 daddr, __be32 saddr,
int nbytes)
{
struct tcp4_pseudohdr *bp;
struct scatterlist sg;
bp = hp->scratch;
bp->saddr = saddr;
bp->daddr = daddr;
bp->pad = 0;
bp->protocol = IPPROTO_TCP;
bp->len = cpu_to_be16(nbytes);
sg_init_one(&sg, bp, sizeof(*bp));
ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
return crypto_ahash_update(hp->req);
}
static int tcp_ao_hash_pseudoheader(unsigned short int family,
const struct sock *sk,
const struct sk_buff *skb,
struct tcp_sigpool *hp, int nbytes)
{
const struct tcphdr *th = tcp_hdr(skb);
/* TODO: Can we rely on checksum being zero to mean outbound pkt? */
if (!th->check) {
if (family == AF_INET)
return tcp_v4_ao_hash_pseudoheader(hp, sk->sk_daddr,
sk->sk_rcv_saddr, skb->len);
#if IS_ENABLED(CONFIG_IPV6)
else if (family == AF_INET6)
return tcp_v6_ao_hash_pseudoheader(hp, &sk->sk_v6_daddr,
&sk->sk_v6_rcv_saddr, skb->len);
#endif
else
return -EAFNOSUPPORT;
}
if (family == AF_INET) {
const struct iphdr *iph = ip_hdr(skb);
return tcp_v4_ao_hash_pseudoheader(hp, iph->daddr,
iph->saddr, skb->len);
#if IS_ENABLED(CONFIG_IPV6)
} else if (family == AF_INET6) {
const struct ipv6hdr *iph = ipv6_hdr(skb);
return tcp_v6_ao_hash_pseudoheader(hp, &iph->daddr,
&iph->saddr, skb->len);
#endif
}
return -EAFNOSUPPORT;
}
/* tcp_ao_hash_sne(struct tcp_sigpool *hp)
* @hp - used for hashing
* @sne - sne value
*/
static int tcp_ao_hash_sne(struct tcp_sigpool *hp, u32 sne)
{
struct scatterlist sg;
__be32 *bp;
bp = (__be32 *)hp->scratch;
*bp = htonl(sne);
sg_init_one(&sg, bp, sizeof(*bp));
ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
return crypto_ahash_update(hp->req);
}
static int tcp_ao_hash_header(struct tcp_sigpool *hp,
const struct tcphdr *th,
bool exclude_options, u8 *hash,
int hash_offset, int hash_len)
{
int err, len = th->doff << 2;
struct scatterlist sg;
u8 *hdr = hp->scratch;
/* We are not allowed to change tcphdr, make a local copy */
if (exclude_options) {
len = sizeof(*th) + sizeof(struct tcp_ao_hdr) + hash_len;
memcpy(hdr, th, sizeof(*th));
memcpy(hdr + sizeof(*th),
(u8 *)th + hash_offset - sizeof(struct tcp_ao_hdr),
sizeof(struct tcp_ao_hdr));
memset(hdr + sizeof(*th) + sizeof(struct tcp_ao_hdr),
0, hash_len);
((struct tcphdr *)hdr)->check = 0;
} else {
len = th->doff << 2;
memcpy(hdr, th, len);
/* zero out tcp-ao hash */
((struct tcphdr *)hdr)->check = 0;
memset(hdr + hash_offset, 0, hash_len);
}
sg_init_one(&sg, hdr, len);
ahash_request_set_crypt(hp->req, &sg, NULL, len);
err = crypto_ahash_update(hp->req);
WARN_ON_ONCE(err != 0);
return err;
}
int tcp_ao_hash_skb(unsigned short int family,
char *ao_hash, struct tcp_ao_key *key,
const struct sock *sk, const struct sk_buff *skb,
const u8 *tkey, int hash_offset, u32 sne)
{
const struct tcphdr *th = tcp_hdr(skb);
int tkey_len = tcp_ao_digest_size(key);
struct tcp_sigpool hp;
void *hash_buf = NULL;
hash_buf = kmalloc(tkey_len, GFP_ATOMIC);
if (!hash_buf)
goto clear_hash_noput;
if (tcp_sigpool_start(key->tcp_sigpool_id, &hp))
goto clear_hash_noput;
if (crypto_ahash_setkey(crypto_ahash_reqtfm(hp.req), tkey, tkey_len))
goto clear_hash;
/* For now use sha1 by default. Depends on alg in tcp_ao_key */
if (crypto_ahash_init(hp.req))
goto clear_hash;
if (tcp_ao_hash_sne(&hp, sne))
goto clear_hash;
if (tcp_ao_hash_pseudoheader(family, sk, skb, &hp, skb->len))
goto clear_hash;
if (tcp_ao_hash_header(&hp, th, false,
ao_hash, hash_offset, tcp_ao_maclen(key)))
goto clear_hash;
if (tcp_sigpool_hash_skb_data(&hp, skb, th->doff << 2))
goto clear_hash;
ahash_request_set_crypt(hp.req, NULL, hash_buf, 0);
if (crypto_ahash_final(hp.req))
goto clear_hash;
memcpy(ao_hash, hash_buf, tcp_ao_maclen(key));
tcp_sigpool_end(&hp);
kfree(hash_buf);
return 0;
clear_hash:
tcp_sigpool_end(&hp);
clear_hash_noput:
memset(ao_hash, 0, tcp_ao_maclen(key));
kfree(hash_buf);
return 1;
}
int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
const struct sock *sk, const struct sk_buff *skb,
const u8 *tkey, int hash_offset, u32 sne)
{
return tcp_ao_hash_skb(AF_INET, ao_hash, key, sk, skb,
tkey, hash_offset, sne);
}
struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk, struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
int sndid, int rcvid) int sndid, int rcvid)
{ {
...@@ -270,6 +435,40 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk, ...@@ -270,6 +435,40 @@ struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
return tcp_ao_do_lookup(sk, addr, AF_INET, sndid, rcvid); return tcp_ao_do_lookup(sk, addr, AF_INET, sndid, rcvid);
} }
int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
struct tcp_ao_key *key, struct tcphdr *th,
__u8 *hash_location)
{
struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
struct tcp_sock *tp = tcp_sk(sk);
struct tcp_ao_info *ao;
void *tkey_buf = NULL;
u8 *traffic_key;
ao = rcu_dereference_protected(tcp_sk(sk)->ao_info,
lockdep_sock_is_held(sk));
traffic_key = snd_other_key(key);
if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
__be32 disn;
if (!(tcb->tcp_flags & TCPHDR_ACK)) {
disn = 0;
tkey_buf = kmalloc(tcp_ao_digest_size(key), GFP_ATOMIC);
if (!tkey_buf)
return -ENOMEM;
traffic_key = tkey_buf;
} else {
disn = ao->risn;
}
tp->af_specific->ao_calc_key_sk(key, traffic_key,
sk, ao->lisn, disn, true);
}
tp->af_specific->calc_ao_hash(hash_location, key, sk, skb, traffic_key,
hash_location - (u8 *)th, 0);
kfree(tkey_buf);
return 0;
}
static int tcp_ao_cache_traffic_keys(const struct sock *sk, static int tcp_ao_cache_traffic_keys(const struct sock *sk,
struct tcp_ao_info *ao, struct tcp_ao_info *ao,
struct tcp_ao_key *ao_key) struct tcp_ao_key *ao_key)
......
...@@ -2287,6 +2287,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = { ...@@ -2287,6 +2287,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
#endif #endif
#ifdef CONFIG_TCP_AO #ifdef CONFIG_TCP_AO
.ao_lookup = tcp_v4_ao_lookup, .ao_lookup = tcp_v4_ao_lookup,
.calc_ao_hash = tcp_v4_ao_hash_skb,
.ao_parse = tcp_v4_parse_ao, .ao_parse = tcp_v4_parse_ao,
.ao_calc_key_sk = tcp_v4_ao_calc_key_sk, .ao_calc_key_sk = tcp_v4_ao_calc_key_sk,
#endif #endif
......
...@@ -422,6 +422,7 @@ static inline bool tcp_urg_mode(const struct tcp_sock *tp) ...@@ -422,6 +422,7 @@ static inline bool tcp_urg_mode(const struct tcp_sock *tp)
#define OPTION_FAST_OPEN_COOKIE BIT(8) #define OPTION_FAST_OPEN_COOKIE BIT(8)
#define OPTION_SMC BIT(9) #define OPTION_SMC BIT(9)
#define OPTION_MPTCP BIT(10) #define OPTION_MPTCP BIT(10)
#define OPTION_AO BIT(11)
static void smc_options_write(__be32 *ptr, u16 *options) static void smc_options_write(__be32 *ptr, u16 *options)
{ {
...@@ -614,19 +615,43 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb, ...@@ -614,19 +615,43 @@ static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,
* (but it may well be that other scenarios fail similarly). * (but it may well be that other scenarios fail similarly).
*/ */
static void tcp_options_write(struct tcphdr *th, struct tcp_sock *tp, static void tcp_options_write(struct tcphdr *th, struct tcp_sock *tp,
struct tcp_out_options *opts) struct tcp_out_options *opts,
struct tcp_key *key)
{ {
__be32 *ptr = (__be32 *)(th + 1); __be32 *ptr = (__be32 *)(th + 1);
u16 options = opts->options; /* mungable copy */ u16 options = opts->options; /* mungable copy */
if (unlikely(OPTION_MD5 & options)) { if (tcp_key_is_md5(key)) {
*ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
(TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG); (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
/* overload cookie hash location */ /* overload cookie hash location */
opts->hash_location = (__u8 *)ptr; opts->hash_location = (__u8 *)ptr;
ptr += 4; ptr += 4;
} } else if (tcp_key_is_ao(key)) {
#ifdef CONFIG_TCP_AO
struct tcp_ao_key *rnext_key;
struct tcp_ao_info *ao_info;
u8 maclen;
ao_info = rcu_dereference_check(tp->ao_info,
lockdep_sock_is_held(&tp->inet_conn.icsk_inet.sk));
rnext_key = READ_ONCE(ao_info->rnext_key);
if (WARN_ON_ONCE(!rnext_key))
goto out_ao;
maclen = tcp_ao_maclen(key->ao_key);
*ptr++ = htonl((TCPOPT_AO << 24) |
(tcp_ao_len(key->ao_key) << 16) |
(key->ao_key->sndid << 8) |
(rnext_key->rcvid));
opts->hash_location = (__u8 *)ptr;
ptr += maclen / sizeof(*ptr);
if (unlikely(maclen % sizeof(*ptr))) {
memset(ptr, TCPOPT_NOP, sizeof(*ptr));
ptr++;
}
out_ao:
#endif
}
if (unlikely(opts->mss)) { if (unlikely(opts->mss)) {
*ptr++ = htonl((TCPOPT_MSS << 24) | *ptr++ = htonl((TCPOPT_MSS << 24) |
(TCPOLEN_MSS << 16) | (TCPOLEN_MSS << 16) |
...@@ -767,23 +792,25 @@ static void mptcp_set_option_cond(const struct request_sock *req, ...@@ -767,23 +792,25 @@ static void mptcp_set_option_cond(const struct request_sock *req,
*/ */
static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb, static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
struct tcp_out_options *opts, struct tcp_out_options *opts,
struct tcp_md5sig_key **md5) struct tcp_key *key)
{ {
struct tcp_sock *tp = tcp_sk(sk); struct tcp_sock *tp = tcp_sk(sk);
unsigned int remaining = MAX_TCP_OPTION_SPACE; unsigned int remaining = MAX_TCP_OPTION_SPACE;
struct tcp_fastopen_request *fastopen = tp->fastopen_req; struct tcp_fastopen_request *fastopen = tp->fastopen_req;
bool timestamps;
*md5 = NULL; /* Better than switch (key.type) as it has static branches */
#ifdef CONFIG_TCP_MD5SIG if (tcp_key_is_md5(key)) {
if (static_branch_unlikely(&tcp_md5_needed.key) && timestamps = false;
rcu_access_pointer(tp->md5sig_info)) { opts->options |= OPTION_MD5;
*md5 = tp->af_specific->md5_lookup(sk, sk); remaining -= TCPOLEN_MD5SIG_ALIGNED;
if (*md5) { } else {
opts->options |= OPTION_MD5; timestamps = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps);
remaining -= TCPOLEN_MD5SIG_ALIGNED; if (tcp_key_is_ao(key)) {
opts->options |= OPTION_AO;
remaining -= tcp_ao_len(key->ao_key);
} }
} }
#endif
/* We always get an MSS option. The option bytes which will be seen in /* We always get an MSS option. The option bytes which will be seen in
* normal data packets should timestamps be used, must be in the MSS * normal data packets should timestamps be used, must be in the MSS
...@@ -797,7 +824,7 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb, ...@@ -797,7 +824,7 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
opts->mss = tcp_advertise_mss(sk); opts->mss = tcp_advertise_mss(sk);
remaining -= TCPOLEN_MSS_ALIGNED; remaining -= TCPOLEN_MSS_ALIGNED;
if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps) && !*md5)) { if (likely(timestamps)) {
opts->options |= OPTION_TS; opts->options |= OPTION_TS;
opts->tsval = tcp_skb_timestamp_ts(tp->tcp_usec_ts, skb) + tp->tsoffset; opts->tsval = tcp_skb_timestamp_ts(tp->tcp_usec_ts, skb) + tp->tsoffset;
opts->tsecr = tp->rx_opt.ts_recent; opts->tsecr = tp->rx_opt.ts_recent;
...@@ -922,7 +949,7 @@ static unsigned int tcp_synack_options(const struct sock *sk, ...@@ -922,7 +949,7 @@ static unsigned int tcp_synack_options(const struct sock *sk,
*/ */
static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb, static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb,
struct tcp_out_options *opts, struct tcp_out_options *opts,
struct tcp_md5sig_key **md5) struct tcp_key *key)
{ {
struct tcp_sock *tp = tcp_sk(sk); struct tcp_sock *tp = tcp_sk(sk);
unsigned int size = 0; unsigned int size = 0;
...@@ -930,17 +957,14 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb ...@@ -930,17 +957,14 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb
opts->options = 0; opts->options = 0;
*md5 = NULL; /* Better than switch (key.type) as it has static branches */
#ifdef CONFIG_TCP_MD5SIG if (tcp_key_is_md5(key)) {
if (static_branch_unlikely(&tcp_md5_needed.key) && opts->options |= OPTION_MD5;
rcu_access_pointer(tp->md5sig_info)) { size += TCPOLEN_MD5SIG_ALIGNED;
*md5 = tp->af_specific->md5_lookup(sk, sk); } else if (tcp_key_is_ao(key)) {
if (*md5) { opts->options |= OPTION_AO;
opts->options |= OPTION_MD5; size += tcp_ao_len(key->ao_key);
size += TCPOLEN_MD5SIG_ALIGNED;
}
} }
#endif
if (likely(tp->rx_opt.tstamp_ok)) { if (likely(tp->rx_opt.tstamp_ok)) {
opts->options |= OPTION_TS; opts->options |= OPTION_TS;
...@@ -1245,7 +1269,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, ...@@ -1245,7 +1269,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
struct tcp_out_options opts; struct tcp_out_options opts;
unsigned int tcp_options_size, tcp_header_size; unsigned int tcp_options_size, tcp_header_size;
struct sk_buff *oskb = NULL; struct sk_buff *oskb = NULL;
struct tcp_md5sig_key *md5; struct tcp_key key;
struct tcphdr *th; struct tcphdr *th;
u64 prior_wstamp; u64 prior_wstamp;
int err; int err;
...@@ -1277,11 +1301,11 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, ...@@ -1277,11 +1301,11 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
tcb = TCP_SKB_CB(skb); tcb = TCP_SKB_CB(skb);
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
tcp_get_current_key(sk, &key);
if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) { if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
tcp_options_size = tcp_syn_options(sk, skb, &opts, &md5); tcp_options_size = tcp_syn_options(sk, skb, &opts, &key);
} else { } else {
tcp_options_size = tcp_established_options(sk, skb, &opts, tcp_options_size = tcp_established_options(sk, skb, &opts, &key);
&md5);
/* Force a PSH flag on all (GSO) packets to expedite GRO flush /* Force a PSH flag on all (GSO) packets to expedite GRO flush
* at receiver : This slightly improve GRO performance. * at receiver : This slightly improve GRO performance.
* Note that we do not force the PSH flag for non GSO packets, * Note that we do not force the PSH flag for non GSO packets,
...@@ -1362,16 +1386,25 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, ...@@ -1362,16 +1386,25 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
th->window = htons(min(tp->rcv_wnd, 65535U)); th->window = htons(min(tp->rcv_wnd, 65535U));
} }
tcp_options_write(th, tp, &opts); tcp_options_write(th, tp, &opts, &key);
if (tcp_key_is_md5(&key)) {
#ifdef CONFIG_TCP_MD5SIG #ifdef CONFIG_TCP_MD5SIG
/* Calculate the MD5 hash, as we have all we need now */ /* Calculate the MD5 hash, as we have all we need now */
if (md5) {
sk_gso_disable(sk); sk_gso_disable(sk);
tp->af_specific->calc_md5_hash(opts.hash_location, tp->af_specific->calc_md5_hash(opts.hash_location,
md5, sk, skb); key.md5_key, sk, skb);
}
#endif #endif
} else if (tcp_key_is_ao(&key)) {
int err;
err = tcp_ao_transmit_skb(sk, skb, key.ao_key, th,
opts.hash_location);
if (err) {
kfree_skb_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED);
return -ENOMEM;
}
}
/* BPF prog is the last one writing header option */ /* BPF prog is the last one writing header option */
bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts); bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts);
...@@ -1804,7 +1837,7 @@ unsigned int tcp_current_mss(struct sock *sk) ...@@ -1804,7 +1837,7 @@ unsigned int tcp_current_mss(struct sock *sk)
u32 mss_now; u32 mss_now;
unsigned int header_len; unsigned int header_len;
struct tcp_out_options opts; struct tcp_out_options opts;
struct tcp_md5sig_key *md5; struct tcp_key key;
mss_now = tp->mss_cache; mss_now = tp->mss_cache;
...@@ -1813,8 +1846,8 @@ unsigned int tcp_current_mss(struct sock *sk) ...@@ -1813,8 +1846,8 @@ unsigned int tcp_current_mss(struct sock *sk)
if (mtu != inet_csk(sk)->icsk_pmtu_cookie) if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
mss_now = tcp_sync_mss(sk, mtu); mss_now = tcp_sync_mss(sk, mtu);
} }
tcp_get_current_key(sk, &key);
header_len = tcp_established_options(sk, NULL, &opts, &md5) + header_len = tcp_established_options(sk, NULL, &opts, &key) +
sizeof(struct tcphdr); sizeof(struct tcphdr);
/* The mss_cache is sized based on tp->tcp_header_len, which assumes /* The mss_cache is sized based on tp->tcp_header_len, which assumes
* some common options. If this is an odd packet (because we have SACK * some common options. If this is an odd packet (because we have SACK
...@@ -3613,6 +3646,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst, ...@@ -3613,6 +3646,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
const struct tcp_sock *tp = tcp_sk(sk); const struct tcp_sock *tp = tcp_sk(sk);
struct tcp_md5sig_key *md5 = NULL; struct tcp_md5sig_key *md5 = NULL;
struct tcp_out_options opts; struct tcp_out_options opts;
struct tcp_key key = {};
struct sk_buff *skb; struct sk_buff *skb;
int tcp_header_size; int tcp_header_size;
struct tcphdr *th; struct tcphdr *th;
...@@ -3667,6 +3701,8 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst, ...@@ -3667,6 +3701,8 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
#ifdef CONFIG_TCP_MD5SIG #ifdef CONFIG_TCP_MD5SIG
rcu_read_lock(); rcu_read_lock();
md5 = tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req)); md5 = tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req));
if (md5)
key.type = TCP_KEY_MD5;
#endif #endif
skb_set_hash(skb, READ_ONCE(tcp_rsk(req)->txhash), PKT_HASH_TYPE_L4); skb_set_hash(skb, READ_ONCE(tcp_rsk(req)->txhash), PKT_HASH_TYPE_L4);
/* bpf program will be interested in the tcp_flags */ /* bpf program will be interested in the tcp_flags */
...@@ -3693,7 +3729,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst, ...@@ -3693,7 +3729,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
/* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */ /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
th->window = htons(min(req->rsk_rcv_wnd, 65535U)); th->window = htons(min(req->rsk_rcv_wnd, 65535U));
tcp_options_write(th, NULL, &opts); tcp_options_write(th, NULL, &opts, &key);
th->doff = (tcp_header_size >> 2); th->doff = (tcp_header_size >> 2);
TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS); TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
* Francesco Ruggeri <fruggeri@arista.com> * Francesco Ruggeri <fruggeri@arista.com>
* Salam Noureddine <noureddine@arista.com> * Salam Noureddine <noureddine@arista.com>
*/ */
#include <crypto/hash.h>
#include <linux/tcp.h> #include <linux/tcp.h>
#include <net/tcp.h> #include <net/tcp.h>
...@@ -79,6 +80,33 @@ struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk, ...@@ -79,6 +80,33 @@ struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk,
return tcp_v6_ao_do_lookup(sk, addr, sndid, rcvid); return tcp_v6_ao_do_lookup(sk, addr, sndid, rcvid);
} }
int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp,
const struct in6_addr *daddr,
const struct in6_addr *saddr, int nbytes)
{
struct tcp6_pseudohdr *bp;
struct scatterlist sg;
bp = hp->scratch;
/* 1. TCP pseudo-header (RFC2460) */
bp->saddr = *saddr;
bp->daddr = *daddr;
bp->len = cpu_to_be32(nbytes);
bp->protocol = cpu_to_be32(IPPROTO_TCP);
sg_init_one(&sg, bp, sizeof(*bp));
ahash_request_set_crypt(hp->req, &sg, NULL, sizeof(*bp));
return crypto_ahash_update(hp->req);
}
int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
const struct sock *sk, const struct sk_buff *skb,
const u8 *tkey, int hash_offset, u32 sne)
{
return tcp_ao_hash_skb(AF_INET6, ao_hash, key, sk, skb, tkey,
hash_offset, sne);
}
int tcp_v6_parse_ao(struct sock *sk, int cmd, int tcp_v6_parse_ao(struct sock *sk, int cmd,
sockptr_t optval, int optlen) sockptr_t optval, int optlen)
{ {
......
...@@ -1920,6 +1920,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = { ...@@ -1920,6 +1920,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
#endif #endif
#ifdef CONFIG_TCP_AO #ifdef CONFIG_TCP_AO
.ao_lookup = tcp_v6_ao_lookup, .ao_lookup = tcp_v6_ao_lookup,
.calc_ao_hash = tcp_v6_ao_hash_skb,
.ao_parse = tcp_v6_parse_ao, .ao_parse = tcp_v6_parse_ao,
.ao_calc_key_sk = tcp_v6_ao_calc_key_sk, .ao_calc_key_sk = tcp_v6_ao_calc_key_sk,
#endif #endif
...@@ -1953,6 +1954,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = { ...@@ -1953,6 +1954,7 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
#endif #endif
#ifdef CONFIG_TCP_AO #ifdef CONFIG_TCP_AO
.ao_lookup = tcp_v6_ao_lookup, .ao_lookup = tcp_v6_ao_lookup,
.calc_ao_hash = tcp_v4_ao_hash_skb,
.ao_parse = tcp_v6_parse_ao, .ao_parse = tcp_v6_parse_ao,
#endif #endif
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment