@@ -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
74397547bool 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)
78467954return & bpf_tcp_check_syncookie_proto ;
78477955case BPF_FUNC_tcp_gen_syncookie :
78487956return & 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
78507968default :
78517969return bpf_sk_base_func_proto (func_id );
0 commit comments