Skip to content

Instantly share code, notes, and snippets.

@astoycos
Created June 16, 2022 22:26
Show Gist options
  • Select an option

  • Save astoycos/04ead520a4eb1324c8fac4e6d75b6f10 to your computer and use it in GitHub Desktop.

Select an option

Save astoycos/04ead520a4eb1324c8fac4e6d75b6f10 to your computer and use it in GitHub Desktop.
SCTP-Redirect-Try
SEC("cgroup/skb")
int bpf_sockops_sctp_load(struct __sk_buff *skb)
{
    void *data = (void *)(long)skb->data;
    struct eth_hdr *eth = data;
    void *data_end = (void *)(long)skb->data_end;

    __u32 dstAddr;
    int ret;

    // Load from 16 bytes + 4 bytes should be dst IP
    ret = bpf_skb_load_bytes_relative(skb, 16, &dstAddr, 4, BPF_HDR_START_NET);

    const char err_str[] = "Hello, world, from BPF! Saw all Socket With IP\
    : %d and Protocol %x Raw address from load bytes %x";
    // 14 byte eth header 
    // Raw Eth header is ~ 14 bytes
    bpf_trace_printk(err_str, sizeof(err_str), skb->remote_ip4, skb->protocol, dstAddr); //ctx->dst_port);

    //if proto isn't sctp we don't care edit all protocols are 8 
    if (skb->protocol != IPPROTO_SCTP)
    {
        return 1;
    }

    if (data + sizeof(*eth) > data_end)
    {
        return 0;
    }

    struct iphdr *iph = data; //+ sizeof(*eth);

    if (data + sizeof(*eth) + sizeof(*iph) > data_end)
        return 0;

    const char err_str2[] = "Hello, world, from sk_buff BPF! Got Packet Data\
    for REALLY RAW Packet: %x RAW PROTO: %x size of mac %x";

    bpf_trace_printk(err_str2, sizeof(err_str2), iph->daddr, iph->protocol, eth->h_dest[0]);

    //if proto isn't sctp we don't care edit all protocols are 8 
    if (iph->protocol != IPPROTO_SCTP)
    {
        return 1;
    }

    // Fake redirect
    if (iph->daddr == 16843177) { 
        const char err_str3[] = "Rewriting DST Addr to backend";
        bpf_trace_printk(err_str3, sizeof(err_str3));

         __u32 newDstAddr = 34252992;
        ret = bpf_skb_store_bytes(skb, 16,&newDstAddr,sizeof(newDstAddr), 0);
    }

    return 1;

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment