• Kuniyuki Iwashima's avatar
    bpf: tcp: Support arbitrary SYN Cookie. · e472f888
    Kuniyuki Iwashima authored
    This patch adds a new kfunc available at TC hook to support arbitrary
    SYN Cookie.
    
    The basic usage is as follows:
    
        struct bpf_tcp_req_attrs attrs = {
            .mss = mss,
            .wscale_ok = wscale_ok,
            .rcv_wscale = rcv_wscale, /* Server's WScale < 15 */
            .snd_wscale = snd_wscale, /* Client's WScale < 15 */
            .tstamp_ok = tstamp_ok,
            .rcv_tsval = tsval,
            .rcv_tsecr = tsecr, /* Server's Initial TSval */
            .usec_ts_ok = usec_ts_ok,
            .sack_ok = sack_ok,
            .ecn_ok = ecn_ok,
        }
    
        skc = bpf_skc_lookup_tcp(...);
        sk = (struct sock *)bpf_skc_to_tcp_sock(skc);
        bpf_sk_assign_tcp_reqsk(skb, sk, attrs, sizeof(attrs));
        bpf_sk_release(skc);
    
    bpf_sk_assign_tcp_reqsk() takes skb, a listener sk, and struct
    bpf_tcp_req_attrs and allocates reqsk and configures it.  Then,
    bpf_sk_assign_tcp_reqsk() links reqsk with skb and the listener.
    
    The notable thing here is that we do not hold refcnt for both reqsk
    and listener.  To differentiate that, we mark reqsk->syncookie, which
    is only used in TX for now.  So, if reqsk->syncookie is 1 in RX, it
    means that the reqsk is allocated by kfunc.
    
    When skb is freed, sock_pfree() checks if reqsk->syncookie is 1,
    and in that case, we set NULL to reqsk->rsk_listener before calling
    reqsk_free() as reqsk does not hold a refcnt of the listener.
    
    When the TCP stack looks up a socket from the skb, we steal the
    listener from the reqsk in skb_steal_sock() and create a full sk
    in cookie_v[46]_check().
    
    The refcnt of reqsk will finally be set to 1 in tcp_get_cookie_sock()
    after creating a full sk.
    
    Note that we can extend struct bpf_tcp_req_attrs in the future when
    we add a new attribute that is determined in 3WHS.
    Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
    Link: https://lore.kernel.org/r/20240115205514.68364-6-kuniyu@amazon.comSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    e472f888
tcp.h 84.8 KB