Commit 7b0f570f authored by Kuniyuki Iwashima's avatar Kuniyuki Iwashima Committed by Jakub Kicinski

tcp: Move TCP-AO bits from cookie_v[46]_check() to tcp_ao_syncookie().

We initialise treq->af_specific in cookie_tcp_reqsk_alloc() so that
we can look up a key later in tcp_create_openreq_child().

Initially, that change was added for MD5 by commit ba5a4fdd ("tcp:
make sure treq->af_specific is initialized"), but it has not been used
since commit d0f2b7a9 ("tcp: Disable header prediction for MD5
flow.").

Now, treq->af_specific is used only by TCP-AO, so, we can move that
initialisation into tcp_ao_syncookie().

In addition to that, l3index in cookie_v[46]_check() is only used for
tcp_ao_syncookie(), so let's move it as well.

While at it, we move down tcp_ao_syncookie() in cookie_v4_check() so
that it will be called after security_inet_conn_request() to make
functions order consistent with cookie_v6_check().
Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: default avatarSimon Horman <horms@kernel.org>
Reviewed-by: default avatarEric Dumazet <edumazet@google.com>
Link: https://lore.kernel.org/r/20231129022924.96156-7-kuniyu@amazon.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent efce3d1f
...@@ -494,7 +494,6 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb, ...@@ -494,7 +494,6 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th); int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th);
struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb); struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops, struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
const struct tcp_request_sock_ops *af_ops,
struct sock *sk, struct sk_buff *skb); struct sock *sk, struct sk_buff *skb);
#ifdef CONFIG_SYN_COOKIES #ifdef CONFIG_SYN_COOKIES
......
...@@ -265,8 +265,7 @@ void tcp_ao_established(struct sock *sk); ...@@ -265,8 +265,7 @@ 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);
void tcp_ao_connect_init(struct sock *sk); void tcp_ao_connect_init(struct sock *sk);
void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
struct tcp_request_sock *treq, struct request_sock *req, unsigned short int family);
unsigned short int family, int l3index);
#else /* CONFIG_TCP_AO */ #else /* CONFIG_TCP_AO */
static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb, static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
...@@ -277,8 +276,7 @@ static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb, ...@@ -277,8 +276,7 @@ static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
} }
static inline void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, static inline void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
struct tcp_request_sock *treq, struct request_sock *req, unsigned short int family)
unsigned short int family, int l3index)
{ {
} }
......
...@@ -286,9 +286,7 @@ bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt, ...@@ -286,9 +286,7 @@ bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt,
EXPORT_SYMBOL(cookie_ecn_ok); EXPORT_SYMBOL(cookie_ecn_ok);
struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops, struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
const struct tcp_request_sock_ops *af_ops, struct sock *sk, struct sk_buff *skb)
struct sock *sk,
struct sk_buff *skb)
{ {
struct tcp_request_sock *treq; struct tcp_request_sock *treq;
struct request_sock *req; struct request_sock *req;
...@@ -303,9 +301,6 @@ struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops, ...@@ -303,9 +301,6 @@ struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
treq = tcp_rsk(req); treq = tcp_rsk(req);
/* treq->af_specific might be used to perform TCP_MD5 lookup */
treq->af_specific = af_ops;
treq->syn_tos = TCP_SKB_CB(skb)->ip_dsfield; treq->syn_tos = TCP_SKB_CB(skb)->ip_dsfield;
treq->req_usec_ts = false; treq->req_usec_ts = false;
...@@ -345,7 +340,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ...@@ -345,7 +340,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
struct rtable *rt; struct rtable *rt;
__u8 rcv_wscale; __u8 rcv_wscale;
u32 tsoff = 0; u32 tsoff = 0;
int l3index;
if (!READ_ONCE(net->ipv4.sysctl_tcp_syncookies) || if (!READ_ONCE(net->ipv4.sysctl_tcp_syncookies) ||
!th->ack || th->rst) !th->ack || th->rst)
...@@ -376,8 +370,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ...@@ -376,8 +370,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
if (!cookie_timestamp_decode(net, &tcp_opt)) if (!cookie_timestamp_decode(net, &tcp_opt))
goto out; goto out;
req = cookie_tcp_reqsk_alloc(&tcp_request_sock_ops, req = cookie_tcp_reqsk_alloc(&tcp_request_sock_ops, sk, skb);
&tcp_request_sock_ipv4_ops, sk, skb);
if (!req) if (!req)
goto out_drop; goto out_drop;
...@@ -406,9 +399,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ...@@ -406,9 +399,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
ireq->ir_iif = inet_request_bound_dev_if(sk, skb); ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
l3index = l3mdev_master_ifindex_by_index(net, ireq->ir_iif);
tcp_ao_syncookie(sk, skb, treq, AF_INET, l3index);
/* We throwed the options of the initial SYN away, so we hope /* We throwed the options of the initial SYN away, so we hope
* the ACK carries the same options again (see RFC1122 4.2.3.8) * the ACK carries the same options again (see RFC1122 4.2.3.8)
*/ */
...@@ -417,6 +407,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb) ...@@ -417,6 +407,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
if (security_inet_conn_request(sk, skb, req)) if (security_inet_conn_request(sk, skb, req))
goto out_free; goto out_free;
tcp_ao_syncookie(sk, skb, req, AF_INET);
req->num_retrans = 0; req->num_retrans = 0;
/* /*
......
...@@ -844,18 +844,30 @@ static struct tcp_ao_key *tcp_ao_inbound_lookup(unsigned short int family, ...@@ -844,18 +844,30 @@ static struct tcp_ao_key *tcp_ao_inbound_lookup(unsigned short int family,
} }
void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb, void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
struct tcp_request_sock *treq, struct request_sock *req, unsigned short int family)
unsigned short int family, int l3index)
{ {
struct tcp_request_sock *treq = tcp_rsk(req);
const struct tcphdr *th = tcp_hdr(skb); const struct tcphdr *th = tcp_hdr(skb);
const struct tcp_ao_hdr *aoh; const struct tcp_ao_hdr *aoh;
struct tcp_ao_key *key; struct tcp_ao_key *key;
int l3index;
/* treq->af_specific is used to perform TCP_AO lookup
* in tcp_create_openreq_child().
*/
#if IS_ENABLED(CONFIG_IPV6)
if (family == AF_INET6)
treq->af_specific = &tcp_request_sock_ipv6_ops;
else
#endif
treq->af_specific = &tcp_request_sock_ipv4_ops;
treq->maclen = 0; treq->maclen = 0;
if (tcp_parse_auth_options(th, NULL, &aoh) || !aoh) if (tcp_parse_auth_options(th, NULL, &aoh) || !aoh)
return; return;
l3index = l3mdev_master_ifindex_by_index(sock_net(sk), inet_rsk(req)->ir_iif);
key = tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index); key = tcp_ao_inbound_lookup(family, sk, skb, -1, aoh->keyid, l3index);
if (!key) if (!key)
/* Key not found, continue without TCP-AO */ /* Key not found, continue without TCP-AO */
......
...@@ -142,7 +142,6 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ...@@ -142,7 +142,6 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
int full_space, mss; int full_space, mss;
__u8 rcv_wscale; __u8 rcv_wscale;
u32 tsoff = 0; u32 tsoff = 0;
int l3index;
if (!READ_ONCE(net->ipv4.sysctl_tcp_syncookies) || if (!READ_ONCE(net->ipv4.sysctl_tcp_syncookies) ||
!th->ack || th->rst) !th->ack || th->rst)
...@@ -173,8 +172,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ...@@ -173,8 +172,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
if (!cookie_timestamp_decode(net, &tcp_opt)) if (!cookie_timestamp_decode(net, &tcp_opt))
goto out; goto out;
req = cookie_tcp_reqsk_alloc(&tcp6_request_sock_ops, req = cookie_tcp_reqsk_alloc(&tcp6_request_sock_ops, sk, skb);
&tcp_request_sock_ipv6_ops, sk, skb);
if (!req) if (!req)
goto out_drop; goto out_drop;
...@@ -218,8 +216,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) ...@@ -218,8 +216,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
treq->ts_off = tsoff; treq->ts_off = tsoff;
treq->txhash = net_tx_rndhash(); treq->txhash = net_tx_rndhash();
l3index = l3mdev_master_ifindex_by_index(net, ireq->ir_iif); tcp_ao_syncookie(sk, skb, req, AF_INET6);
tcp_ao_syncookie(sk, skb, treq, AF_INET6, l3index);
if (IS_ENABLED(CONFIG_SMC)) if (IS_ENABLED(CONFIG_SMC))
ireq->smc_ok = 0; ireq->smc_ok = 0;
......
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