1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Cloudflare Ltd.
3 // Copyright (c) 2020 Isovalent, Inc.
9 #include <linux/if_ether.h>
12 #include <linux/ipv6.h>
13 #include <linux/pkt_cls.h>
14 #include <linux/tcp.h>
15 #include <sys/socket.h>
16 #include <bpf/bpf_helpers.h>
17 #include <bpf/bpf_endian.h>
20 #if defined(IPROUTE2_HAVE_LIBBPF)
21 /* Use a new-style map definition. */
23 __uint(type, BPF_MAP_TYPE_SOCKMAP);
26 __uint(pinning, LIBBPF_PIN_BY_NAME);
27 __uint(max_entries, 1);
28 } server_map SEC(".maps");
30 /* Pin map under /sys/fs/bpf/tc/globals/<map name> */
31 #define PIN_GLOBAL_NS 2
33 /* Must match struct bpf_elf_map layout from iproute2 */
42 } server_map SEC("maps") = {
43 .type = BPF_MAP_TYPE_SOCKMAP,
44 .size_key = sizeof(int),
45 .size_value = sizeof(__u64),
47 .pinning = PIN_GLOBAL_NS,
51 char _license[] SEC("license") = "GPL";
53 /* Fill 'tuple' with L3 info, and attempt to find L4. On fail, return NULL. */
54 static inline struct bpf_sock_tuple *
55 get_tuple(struct __sk_buff *skb, bool *ipv4, bool *tcp)
57 void *data_end = (void *)(long)skb->data_end;
58 void *data = (void *)(long)skb->data;
59 struct bpf_sock_tuple *result;
64 eth = (struct ethhdr *)(data);
65 if (eth + 1 > data_end)
68 if (eth->h_proto == bpf_htons(ETH_P_IP)) {
69 struct iphdr *iph = (struct iphdr *)(data + sizeof(*eth));
71 if (iph + 1 > data_end)
74 /* Options are not supported */
76 ihl_len = iph->ihl * 4;
77 proto = iph->protocol;
79 result = (struct bpf_sock_tuple *)&iph->saddr;
80 } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
81 struct ipv6hdr *ip6h = (struct ipv6hdr *)(data + sizeof(*eth));
83 if (ip6h + 1 > data_end)
85 ihl_len = sizeof(*ip6h);
86 proto = ip6h->nexthdr;
88 result = (struct bpf_sock_tuple *)&ip6h->saddr;
90 return (struct bpf_sock_tuple *)data;
93 if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
96 *tcp = (proto == IPPROTO_TCP);
102 handle_udp(struct __sk_buff *skb, struct bpf_sock_tuple *tuple, bool ipv4)
110 tuple_len = ipv4 ? sizeof(tuple->ipv4) : sizeof(tuple->ipv6);
111 if ((void *)tuple + tuple_len > (void *)(long)skb->data_end)
114 sk = bpf_sk_lookup_udp(skb, tuple, tuple_len, BPF_F_CURRENT_NETNS, 0);
118 dport = ipv4 ? tuple->ipv4.dport : tuple->ipv6.dport;
119 if (dport != bpf_htons(4321))
122 sk = bpf_map_lookup_elem(&server_map, &zero);
127 ret = bpf_sk_assign(skb, sk, 0);
133 handle_tcp(struct __sk_buff *skb, struct bpf_sock_tuple *tuple, bool ipv4)
141 tuple_len = ipv4 ? sizeof(tuple->ipv4) : sizeof(tuple->ipv6);
142 if ((void *)tuple + tuple_len > (void *)(long)skb->data_end)
145 sk = bpf_skc_lookup_tcp(skb, tuple, tuple_len, BPF_F_CURRENT_NETNS, 0);
147 if (sk->state != BPF_TCP_LISTEN)
152 dport = ipv4 ? tuple->ipv4.dport : tuple->ipv6.dport;
153 if (dport != bpf_htons(4321))
156 sk = bpf_map_lookup_elem(&server_map, &zero);
160 if (sk->state != BPF_TCP_LISTEN) {
166 ret = bpf_sk_assign(skb, sk, 0);
172 int bpf_sk_assign_test(struct __sk_buff *skb)
174 struct bpf_sock_tuple *tuple;
179 tuple = get_tuple(skb, &ipv4, &tcp);
183 /* Note that the verifier socket return type for bpf_skc_lookup_tcp()
184 * differs from bpf_sk_lookup_udp(), so even though the C-level type is
185 * the same here, if we try to share the implementations they will
186 * fail to verify because we're crossing pointer types.
189 ret = handle_tcp(skb, tuple, ipv4);
191 ret = handle_udp(skb, tuple, ipv4);
193 return ret == 0 ? TC_ACT_OK : TC_ACT_SHOT;