Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2020 Cloudflare
0003 #include "vmlinux.h"
0004 #include <bpf/bpf_helpers.h>
0005 
0006 struct {
0007     __uint(type, BPF_MAP_TYPE_SOCKMAP);
0008     __uint(max_entries, 1);
0009     __type(key, __u32);
0010     __type(value, __u64);
0011 } src SEC(".maps");
0012 
0013 struct {
0014     __uint(type, BPF_MAP_TYPE_SOCKMAP);
0015     __uint(max_entries, 1);
0016     __type(key, __u32);
0017     __type(value, __u64);
0018 } dst_sock_map SEC(".maps");
0019 
0020 struct {
0021     __uint(type, BPF_MAP_TYPE_SOCKHASH);
0022     __uint(max_entries, 1);
0023     __type(key, __u32);
0024     __type(value, __u64);
0025 } dst_sock_hash SEC(".maps");
0026 
0027 SEC("tc")
0028 int copy_sock_map(void *ctx)
0029 {
0030     struct bpf_sock *sk;
0031     bool failed = false;
0032     __u32 key = 0;
0033 
0034     sk = bpf_map_lookup_elem(&src, &key);
0035     if (!sk)
0036         return SK_DROP;
0037 
0038     if (bpf_map_update_elem(&dst_sock_map, &key, sk, 0))
0039         failed = true;
0040 
0041     if (bpf_map_update_elem(&dst_sock_hash, &key, sk, 0))
0042         failed = true;
0043 
0044     bpf_sk_release(sk);
0045     return failed ? SK_DROP : SK_PASS;
0046 }
0047 
0048 char _license[] SEC("license") = "GPL";