Skip to content
Permalink
Browse files
tcp: ipv4: Add AO signing for skb-less replies
The code in tcp_v4_send_ack and tcp_v4_send_reset does not allocate a
full skb so special handling is required for tcp-authopt handling.

Signed-off-by: Leonard Crestez <cdleonard@gmail.com>
  • Loading branch information
cdleonard authored and intel-lab-lkp committed Dec 8, 2021
1 parent 8687e69 commit 8d4bf3a9b770cb44bf4e37e794e63f7db7a08cb6
Showing 1 changed file with 79 additions and 3 deletions.
@@ -646,6 +646,46 @@ void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
}
EXPORT_SYMBOL(tcp_v4_send_check);

#ifdef CONFIG_TCP_AUTHOPT
/** tcp_v4_authopt_handle_reply - Insert TCPOPT_AUTHOPT if required
*
* returns number of bytes (always aligned to 4) or zero
*/
static int tcp_v4_authopt_handle_reply(const struct sock *sk,
struct sk_buff *skb,
__be32 *optptr,
struct tcphdr *th)
{
struct tcp_authopt_info *info;
struct tcp_authopt_key_info *key_info;
u8 rnextkeyid;

if (sk->sk_state == TCP_TIME_WAIT)
info = tcp_twsk(sk)->tw_authopt_info;
else
info = tcp_sk(sk)->authopt_info;
if (!info)
return 0;
key_info = __tcp_authopt_select_key(sk, info, sk, &rnextkeyid);
if (!key_info)
return 0;
*optptr = htonl((TCPOPT_AUTHOPT << 24) |
(TCPOLEN_AUTHOPT_OUTPUT << 16) |
(key_info->send_id << 8) |
(rnextkeyid));
/* must update doff before signature computation */
th->doff += TCPOLEN_AUTHOPT_OUTPUT / 4;
tcp_v4_authopt_hash_reply((char *)(optptr + 1),
info,
key_info,
ip_hdr(skb)->daddr,
ip_hdr(skb)->saddr,
th);

return TCPOLEN_AUTHOPT_OUTPUT;
}
#endif

/*
* This routine will send an RST to the other tcp.
*
@@ -661,6 +701,8 @@ EXPORT_SYMBOL(tcp_v4_send_check);

#ifdef CONFIG_TCP_MD5SIG
#define OPTION_BYTES TCPOLEN_MD5SIG_ALIGNED
#elif defined(OPTION_BYTES_TCP_AUTHOPT)
#define OPTION_BYTES TCPOLEN_AUTHOPT_OUTPUT
#else
#define OPTION_BYTES sizeof(__be32)
#endif
@@ -714,8 +756,25 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
arg.iov[0].iov_len = sizeof(rep.th);

net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev);
#ifdef CONFIG_TCP_MD5SIG
#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AUTHOPT)
rcu_read_lock();
#endif
#ifdef CONFIG_TCP_AUTHOPT
/* Unlike TCP-MD5 the signatures for TCP-AO depend on initial sequence
* numbers so we can only handle established and time-wait sockets.
*/
if (tcp_authopt_needed && sk &&
sk->sk_state != TCP_NEW_SYN_RECV &&
sk->sk_state != TCP_LISTEN) {
int tcp_authopt_ret = tcp_v4_authopt_handle_reply(sk, skb, rep.opt, &rep.th);

if (tcp_authopt_ret) {
arg.iov[0].iov_len += tcp_authopt_ret;
goto skip_md5sig;
}
}
#endif
#ifdef CONFIG_TCP_MD5SIG
hash_location = tcp_parse_md5sig_option(th);
if (sk && sk_fullsock(sk)) {
const union tcp_md5_addr *addr;
@@ -757,7 +816,6 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
if (!key)
goto out;


genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
if (genhash || memcmp(hash_location, newhash, 16) != 0)
goto out;
@@ -777,6 +835,9 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
key, ip_hdr(skb)->saddr,
ip_hdr(skb)->daddr, &rep.th);
}
#endif
#ifdef CONFIG_TCP_AUTHOPT
skip_md5sig:
#endif
/* Can't co-exist with TCPMD5, hence check rep.opt[0] */
if (rep.opt[0] == 0) {
@@ -830,8 +891,10 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
__TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
local_bh_enable();

#ifdef CONFIG_TCP_MD5SIG
#if defined(CONFIG_TCP_MD5SIG)
out:
#endif
#if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AUTHOPT)
rcu_read_unlock();
#endif
}
@@ -852,6 +915,8 @@ static void tcp_v4_send_ack(const struct sock *sk,
__be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
#ifdef CONFIG_TCP_MD5SIG
+ (TCPOLEN_MD5SIG_ALIGNED >> 2)
#elif defined(CONFIG_TCP_AUTHOPT)
+ (TCPOLEN_AUTHOPT_OUTPUT >> 2)
#endif
];
} rep;
@@ -883,6 +948,17 @@ static void tcp_v4_send_ack(const struct sock *sk,
rep.th.ack = 1;
rep.th.window = htons(win);

#ifdef CONFIG_TCP_AUTHOPT
if (tcp_authopt_needed) {
int aoret, offset = (tsecr) ? 3 : 0;

aoret = tcp_v4_authopt_handle_reply(sk, skb, &rep.opt[offset], &rep.th);
if (aoret) {
arg.iov[0].iov_len += aoret;
key = NULL;
}
}
#endif
#ifdef CONFIG_TCP_MD5SIG
if (key) {
int offset = (tsecr) ? 3 : 0;

0 comments on commit 8d4bf3a

Please sign in to comment.