Skip to content

Commit 8d54dfc

Browse files
nvmmaxKernel Patches Daemon
authored andcommitted
bpf: Add helpers to issue and check SYN cookies in XDP
The new helpers bpf_tcp_raw_{gen,check}_syncookie_ipv{4,6} allow an XDP program to generate SYN cookies in response to TCP SYN packets and to check those cookies upon receiving the first ACK packet (the final packet of the TCP handshake). Unlike bpf_tcp_{gen,check}_syncookie these new helpers don't need a listening socket on the local machine, which allows to use them together with synproxy to accelerate SYN cookie generation. Signed-off-by: Maxim Mikityanskiy <maximmi@nvidia.com> Reviewed-by: Tariq Toukan <tariqt@nvidia.com>
1 parent 1587bbd commit 8d54dfc

File tree

6 files changed

+281
-1
lines changed

6 files changed

+281
-1
lines changed

include/net/tcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
432432
struct tcphdr *th, u32 *cookie);
433433
u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
434434
struct tcphdr *th, u32 *cookie);
435+
u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss);
435436
u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
436437
const struct tcp_request_sock_ops *af_ops,
437438
struct sock *sk, struct tcphdr *th);

include/uapi/linux/bpf.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5145,6 +5145,80 @@ union bpf_attr {
51455145
* The **hash_algo** is returned on success,
51465146
* **-EOPNOTSUP** if the hash calculation failed or **-EINVAL** if
51475147
* invalid arguments are passed.
5148+
*
5149+
* s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
5150+
* Description
5151+
* Try to issue a SYN cookie for the packet with corresponding
5152+
* IPv4/TCP headers, *iph* and *th*, without depending on a
5153+
* listening socket.
5154+
*
5155+
* *iph* points to the IPv4 header.
5156+
*
5157+
* *th* points to the start of the TCP header, while *th_len*
5158+
* contains the length of the TCP header (at least
5159+
* **sizeof**\ (**struct tcphdr**)).
5160+
* Return
5161+
* On success, lower 32 bits hold the generated SYN cookie in
5162+
* followed by 16 bits which hold the MSS value for that cookie,
5163+
* and the top 16 bits are unused.
5164+
*
5165+
* On failure, the returned value is one of the following:
5166+
*
5167+
* **-EINVAL** if *th_len* is invalid.
5168+
*
5169+
* s64 bpf_tcp_raw_gen_syncookie_ipv6(struct ipv6hdr *iph, struct tcphdr *th, u32 th_len)
5170+
* Description
5171+
* Try to issue a SYN cookie for the packet with corresponding
5172+
* IPv6/TCP headers, *iph* and *th*, without depending on a
5173+
* listening socket.
5174+
*
5175+
* *iph* points to the IPv6 header.
5176+
*
5177+
* *th* points to the start of the TCP header, while *th_len*
5178+
* contains the length of the TCP header (at least
5179+
* **sizeof**\ (**struct tcphdr**)).
5180+
* Return
5181+
* On success, lower 32 bits hold the generated SYN cookie in
5182+
* followed by 16 bits which hold the MSS value for that cookie,
5183+
* and the top 16 bits are unused.
5184+
*
5185+
* On failure, the returned value is one of the following:
5186+
*
5187+
* **-EINVAL** if *th_len* is invalid.
5188+
*
5189+
* **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin.
5190+
*
5191+
* int bpf_tcp_raw_check_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th)
5192+
* Description
5193+
* Check whether *iph* and *th* contain a valid SYN cookie ACK
5194+
* without depending on a listening socket.
5195+
*
5196+
* *iph* points to the IPv4 header.
5197+
*
5198+
* *th* points to the TCP header.
5199+
* Return
5200+
* 0 if *iph* and *th* are a valid SYN cookie ACK.
5201+
*
5202+
* On failure, the returned value is one of the following:
5203+
*
5204+
* **-EACCES** if the SYN cookie is not valid.
5205+
*
5206+
* int bpf_tcp_raw_check_syncookie_ipv6(struct ipv6hdr *iph, struct tcphdr *th)
5207+
* Description
5208+
* Check whether *iph* and *th* contain a valid SYN cookie ACK
5209+
* without depending on a listening socket.
5210+
*
5211+
* *iph* points to the IPv6 header.
5212+
*
5213+
* *th* points to the TCP header.
5214+
* Return
5215+
* 0 if *iph* and *th* are a valid SYN cookie ACK.
5216+
*
5217+
* On failure, the returned value is one of the following:
5218+
*
5219+
* **-EACCES** if the SYN cookie is not valid.
5220+
*
5221+
* **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin.
51485222
*/
51495223
#define __BPF_FUNC_MAPPER(FN) \
51505224
FN(unspec), \
@@ -5341,6 +5415,10 @@ union bpf_attr {
53415415
FN(copy_from_user_task), \
53425416
FN(skb_set_tstamp), \
53435417
FN(ima_file_hash), \
5418+
FN(tcp_raw_gen_syncookie_ipv4), \
5419+
FN(tcp_raw_gen_syncookie_ipv6), \
5420+
FN(tcp_raw_check_syncookie_ipv4), \
5421+
FN(tcp_raw_check_syncookie_ipv6), \
53445422
/* */
53455423

53465424
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

net/core/filter.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7434,6 +7434,114 @@ static const struct bpf_func_proto bpf_skb_set_tstamp_proto = {
74347434
.arg3_type = ARG_ANYTHING,
74357435
};
74367436

7437+
#ifdef CONFIG_SYN_COOKIES
7438+
BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struct iphdr *, iph,
7439+
struct tcphdr *, th, u32, th_len)
7440+
{
7441+
u32 cookie;
7442+
u16 mss;
7443+
7444+
if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7445+
return -EINVAL;
7446+
7447+
mss = tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT;
7448+
cookie = __cookie_v4_init_sequence(iph, th, &mss);
7449+
7450+
return cookie | ((u64)mss << 32);
7451+
}
7452+
7453+
static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv4_proto = {
7454+
.func= bpf_tcp_raw_gen_syncookie_ipv4,
7455+
.gpl_only= true, /* __cookie_v4_init_sequence() is GPL */
7456+
.pkt_access= true,
7457+
.ret_type= RET_INTEGER,
7458+
.arg1_type= ARG_PTR_TO_MEM,
7459+
.arg1_size= sizeof(struct iphdr),
7460+
.arg2_type= ARG_PTR_TO_MEM,
7461+
.arg3_type= ARG_CONST_SIZE,
7462+
};
7463+
7464+
BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struct ipv6hdr *, iph,
7465+
struct tcphdr *, th, u32, th_len)
7466+
{
7467+
#if IS_BUILTIN(CONFIG_IPV6)
7468+
const u16 mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
7469+
sizeof(struct ipv6hdr);
7470+
u32 cookie;
7471+
u16 mss;
7472+
7473+
if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7474+
return -EINVAL;
7475+
7476+
mss = tcp_parse_mss_option(th, 0) ?: mss_clamp;
7477+
cookie = __cookie_v6_init_sequence(iph, th, &mss);
7478+
7479+
return cookie | ((u64)mss << 32);
7480+
#else
7481+
return -EPROTONOSUPPORT;
7482+
#endif
7483+
}
7484+
7485+
static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = {
7486+
.func= bpf_tcp_raw_gen_syncookie_ipv6,
7487+
.gpl_only= true, /* __cookie_v6_init_sequence() is GPL */
7488+
.pkt_access= true,
7489+
.ret_type= RET_INTEGER,
7490+
.arg1_type= ARG_PTR_TO_MEM,
7491+
.arg1_size= sizeof(struct ipv6hdr),
7492+
.arg2_type= ARG_PTR_TO_MEM,
7493+
.arg3_type= ARG_CONST_SIZE,
7494+
};
7495+
7496+
BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4, struct iphdr *, iph,
7497+
struct tcphdr *, th)
7498+
{
7499+
u32 cookie = ntohl(th->ack_seq) - 1;
7500+
7501+
if (__cookie_v4_check(iph, th, cookie) > 0)
7502+
return 0;
7503+
7504+
return -EACCES;
7505+
}
7506+
7507+
static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv4_proto = {
7508+
.func= bpf_tcp_raw_check_syncookie_ipv4,
7509+
.gpl_only= true, /* __cookie_v4_check is GPL */
7510+
.pkt_access= true,
7511+
.ret_type= RET_INTEGER,
7512+
.arg1_type= ARG_PTR_TO_MEM,
7513+
.arg1_size= sizeof(struct iphdr),
7514+
.arg2_type= ARG_PTR_TO_MEM,
7515+
.arg2_size= sizeof(struct tcphdr),
7516+
};
7517+
7518+
BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6, struct ipv6hdr *, iph,
7519+
struct tcphdr *, th)
7520+
{
7521+
#if IS_BUILTIN(CONFIG_IPV6)
7522+
u32 cookie = ntohl(th->ack_seq) - 1;
7523+
7524+
if (__cookie_v6_check(iph, th, cookie) > 0)
7525+
return 0;
7526+
7527+
return -EACCES;
7528+
#else
7529+
return -EPROTONOSUPPORT;
7530+
#endif
7531+
}
7532+
7533+
static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv6_proto = {
7534+
.func= bpf_tcp_raw_check_syncookie_ipv6,
7535+
.gpl_only= true, /* __cookie_v6_check is GPL */
7536+
.pkt_access= true,
7537+
.ret_type= RET_INTEGER,
7538+
.arg1_type= ARG_PTR_TO_MEM,
7539+
.arg1_size= sizeof(struct ipv6hdr),
7540+
.arg2_type= ARG_PTR_TO_MEM,
7541+
.arg2_size= sizeof(struct tcphdr),
7542+
};
7543+
#endif /* CONFIG_SYN_COOKIES */
7544+
74377545
#endif /* CONFIG_INET */
74387546

74397547
bool bpf_helper_changes_pkt_data(void *func)
@@ -7846,6 +7954,16 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
78467954
return &bpf_tcp_check_syncookie_proto;
78477955
case BPF_FUNC_tcp_gen_syncookie:
78487956
return &bpf_tcp_gen_syncookie_proto;
7957+
#ifdef CONFIG_SYN_COOKIES
7958+
case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
7959+
return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
7960+
case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
7961+
return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
7962+
case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
7963+
return &bpf_tcp_raw_check_syncookie_ipv4_proto;
7964+
case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
7965+
return &bpf_tcp_raw_check_syncookie_ipv6_proto;
7966+
#endif
78497967
#endif
78507968
default:
78517969
return bpf_sk_base_func_proto(func_id);

net/ipv4/tcp_input.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3963,7 +3963,7 @@ static bool smc_parse_options(const struct tcphdr *th,
39633963
/* Try to parse the MSS option from the TCP header. Return 0 on failure, clamped
39643964
* value on success.
39653965
*/
3966-
static u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss)
3966+
u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss)
39673967
{
39683968
const unsigned char *ptr = (const unsigned char *)(th + 1);
39693969
int length = (th->doff * 4) - sizeof(struct tcphdr);
@@ -4002,6 +4002,7 @@ static u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss)
40024002
}
40034003
return mss;
40044004
}
4005+
EXPORT_SYMBOL_GPL(tcp_parse_mss_option);
40054006

40064007
/* Look for tcp options. Normally only called on SYN and SYNACK packets.
40074008
* But, this can also be called on packets in the established flow when

scripts/bpf_doc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ def __init__(self, parser):
633633
'struct socket',
634634
'struct file',
635635
'struct bpf_timer',
636+
'struct iphdr',
637+
'struct ipv6hdr',
636638
]
637639
known_types = {
638640
'...',
@@ -682,6 +684,8 @@ def __init__(self, parser):
682684
'struct socket',
683685
'struct file',
684686
'struct bpf_timer',
687+
'struct iphdr',
688+
'struct ipv6hdr',
685689
}
686690
mapped_types = {
687691
'u8': '__u8',

tools/include/uapi/linux/bpf.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5145,6 +5145,80 @@ union bpf_attr {
51455145
* The **hash_algo** is returned on success,
51465146
* **-EOPNOTSUP** if the hash calculation failed or **-EINVAL** if
51475147
* invalid arguments are passed.
5148+
*
5149+
* s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
5150+
* Description
5151+
* Try to issue a SYN cookie for the packet with corresponding
5152+
* IPv4/TCP headers, *iph* and *th*, without depending on a
5153+
* listening socket.
5154+
*
5155+
* *iph* points to the IPv4 header.
5156+
*
5157+
* *th* points to the start of the TCP header, while *th_len*
5158+
* contains the length of the TCP header (at least
5159+
* **sizeof**\ (**struct tcphdr**)).
5160+
* Return
5161+
* On success, lower 32 bits hold the generated SYN cookie in
5162+
* followed by 16 bits which hold the MSS value for that cookie,
5163+
* and the top 16 bits are unused.
5164+
*
5165+
* On failure, the returned value is one of the following:
5166+
*
5167+
* **-EINVAL** if *th_len* is invalid.
5168+
*
5169+
* s64 bpf_tcp_raw_gen_syncookie_ipv6(struct ipv6hdr *iph, struct tcphdr *th, u32 th_len)
5170+
* Description
5171+
* Try to issue a SYN cookie for the packet with corresponding
5172+
* IPv6/TCP headers, *iph* and *th*, without depending on a
5173+
* listening socket.
5174+
*
5175+
* *iph* points to the IPv6 header.
5176+
*
5177+
* *th* points to the start of the TCP header, while *th_len*
5178+
* contains the length of the TCP header (at least
5179+
* **sizeof**\ (**struct tcphdr**)).
5180+
* Return
5181+
* On success, lower 32 bits hold the generated SYN cookie in
5182+
* followed by 16 bits which hold the MSS value for that cookie,
5183+
* and the top 16 bits are unused.
5184+
*
5185+
* On failure, the returned value is one of the following:
5186+
*
5187+
* **-EINVAL** if *th_len* is invalid.
5188+
*
5189+
* **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin.
5190+
*
5191+
* int bpf_tcp_raw_check_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th)
5192+
* Description
5193+
* Check whether *iph* and *th* contain a valid SYN cookie ACK
5194+
* without depending on a listening socket.
5195+
*
5196+
* *iph* points to the IPv4 header.
5197+
*
5198+
* *th* points to the TCP header.
5199+
* Return
5200+
* 0 if *iph* and *th* are a valid SYN cookie ACK.
5201+
*
5202+
* On failure, the returned value is one of the following:
5203+
*
5204+
* **-EACCES** if the SYN cookie is not valid.
5205+
*
5206+
* int bpf_tcp_raw_check_syncookie_ipv6(struct ipv6hdr *iph, struct tcphdr *th)
5207+
* Description
5208+
* Check whether *iph* and *th* contain a valid SYN cookie ACK
5209+
* without depending on a listening socket.
5210+
*
5211+
* *iph* points to the IPv6 header.
5212+
*
5213+
* *th* points to the TCP header.
5214+
* Return
5215+
* 0 if *iph* and *th* are a valid SYN cookie ACK.
5216+
*
5217+
* On failure, the returned value is one of the following:
5218+
*
5219+
* **-EACCES** if the SYN cookie is not valid.
5220+
*
5221+
* **-EPROTONOSUPPORT** if CONFIG_IPV6 is not builtin.
51485222
*/
51495223
#define __BPF_FUNC_MAPPER(FN) \
51505224
FN(unspec), \
@@ -5341,6 +5415,10 @@ union bpf_attr {
53415415
FN(copy_from_user_task), \
53425416
FN(skb_set_tstamp), \
53435417
FN(ima_file_hash), \
5418+
FN(tcp_raw_gen_syncookie_ipv4), \
5419+
FN(tcp_raw_gen_syncookie_ipv6), \
5420+
FN(tcp_raw_check_syncookie_ipv4), \
5421+
FN(tcp_raw_check_syncookie_ipv6), \
53445422
/* */
53455423

53465424
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)