1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Cloudflare Ltd.
3 // Copyright (c) 2020 Isovalent, Inc.
4
5 #include <stddef.h>
6 #include <stdbool.h>
7 #include <string.h>
8 #include <linux/bpf.h>
9 #include <linux/if_ether.h>
10 #include <linux/in.h>
11 #include <linux/ip.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>
18
19 #if defined(IPROUTE2_HAVE_LIBBPF)
20 /* Use a new-style map definition. */
21 struct {
22 __uint(type, BPF_MAP_TYPE_SOCKMAP);
23 __type(key, int);
24 __type(value, __u64);
25 __uint(pinning, LIBBPF_PIN_BY_NAME);
26 __uint(max_entries, 1);
27 } server_map SEC(".maps");
28 #else
29 /* Pin map under /sys/fs/bpf/tc/globals/<map name> */
30 #define PIN_GLOBAL_NS 2
31
32 /* Must match struct bpf_elf_map layout from iproute2 */
33 struct {
34 __u32 type;
35 __u32 size_key;
36 __u32 size_value;
37 __u32 max_elem;
38 __u32 flags;
39 __u32 id;
40 __u32 pinning;
41 } server_map SEC("maps") = {
42 .type = BPF_MAP_TYPE_SOCKMAP,
43 .size_key = sizeof(int),
44 .size_value = sizeof(__u64),
45 .max_elem = 1,
46 .pinning = PIN_GLOBAL_NS,
47 };
48 #endif
49
50 char _license[] SEC("license") = "GPL";
51
52 /* Fill 'tuple' with L3 info, and attempt to find L4. On fail, return NULL. */
53 static inline struct bpf_sock_tuple *
get_tuple(struct __sk_buff * skb,bool * ipv4,bool * tcp)54 get_tuple(struct __sk_buff *skb, bool *ipv4, bool *tcp)
55 {
56 void *data_end = (void *)(long)skb->data_end;
57 void *data = (void *)(long)skb->data;
58 struct bpf_sock_tuple *result;
59 struct ethhdr *eth;
60 __u64 tuple_len;
61 __u8 proto = 0;
62 __u64 ihl_len;
63
64 eth = (struct ethhdr *)(data);
65 if (eth + 1 > data_end)
66 return NULL;
67
68 if (eth->h_proto == bpf_htons(ETH_P_IP)) {
69 struct iphdr *iph = (struct iphdr *)(data + sizeof(*eth));
70
71 if (iph + 1 > data_end)
72 return NULL;
73 if (iph->ihl != 5)
74 /* Options are not supported */
75 return NULL;
76 ihl_len = iph->ihl * 4;
77 proto = iph->protocol;
78 *ipv4 = true;
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));
82
83 if (ip6h + 1 > data_end)
84 return NULL;
85 ihl_len = sizeof(*ip6h);
86 proto = ip6h->nexthdr;
87 *ipv4 = false;
88 result = (struct bpf_sock_tuple *)&ip6h->saddr;
89 } else {
90 return (struct bpf_sock_tuple *)data;
91 }
92
93 if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
94 return NULL;
95
96 *tcp = (proto == IPPROTO_TCP);
97 return result;
98 }
99
100 static inline int
handle_udp(struct __sk_buff * skb,struct bpf_sock_tuple * tuple,bool ipv4)101 handle_udp(struct __sk_buff *skb, struct bpf_sock_tuple *tuple, bool ipv4)
102 {
103 struct bpf_sock *sk;
104 const int zero = 0;
105 size_t tuple_len;
106 __be16 dport;
107 int ret;
108
109 tuple_len = ipv4 ? sizeof(tuple->ipv4) : sizeof(tuple->ipv6);
110 if ((void *)tuple + tuple_len > (void *)(long)skb->data_end)
111 return TC_ACT_SHOT;
112
113 sk = bpf_sk_lookup_udp(skb, tuple, tuple_len, BPF_F_CURRENT_NETNS, 0);
114 if (sk)
115 goto assign;
116
117 dport = ipv4 ? tuple->ipv4.dport : tuple->ipv6.dport;
118 if (dport != bpf_htons(4321))
119 return TC_ACT_OK;
120
121 sk = bpf_map_lookup_elem(&server_map, &zero);
122 if (!sk)
123 return TC_ACT_SHOT;
124
125 assign:
126 ret = bpf_sk_assign(skb, sk, 0);
127 bpf_sk_release(sk);
128 return ret;
129 }
130
131 static inline int
handle_tcp(struct __sk_buff * skb,struct bpf_sock_tuple * tuple,bool ipv4)132 handle_tcp(struct __sk_buff *skb, struct bpf_sock_tuple *tuple, bool ipv4)
133 {
134 struct bpf_sock *sk;
135 const int zero = 0;
136 size_t tuple_len;
137 __be16 dport;
138 int ret;
139
140 tuple_len = ipv4 ? sizeof(tuple->ipv4) : sizeof(tuple->ipv6);
141 if ((void *)tuple + tuple_len > (void *)(long)skb->data_end)
142 return TC_ACT_SHOT;
143
144 sk = bpf_skc_lookup_tcp(skb, tuple, tuple_len, BPF_F_CURRENT_NETNS, 0);
145 if (sk) {
146 if (sk->state != BPF_TCP_LISTEN)
147 goto assign;
148 bpf_sk_release(sk);
149 }
150
151 dport = ipv4 ? tuple->ipv4.dport : tuple->ipv6.dport;
152 if (dport != bpf_htons(4321))
153 return TC_ACT_OK;
154
155 sk = bpf_map_lookup_elem(&server_map, &zero);
156 if (!sk)
157 return TC_ACT_SHOT;
158
159 if (sk->state != BPF_TCP_LISTEN) {
160 bpf_sk_release(sk);
161 return TC_ACT_SHOT;
162 }
163
164 assign:
165 ret = bpf_sk_assign(skb, sk, 0);
166 bpf_sk_release(sk);
167 return ret;
168 }
169
170 SEC("tc")
bpf_sk_assign_test(struct __sk_buff * skb)171 int bpf_sk_assign_test(struct __sk_buff *skb)
172 {
173 struct bpf_sock_tuple *tuple;
174 bool ipv4 = false;
175 bool tcp = false;
176 int tuple_len;
177 int ret = 0;
178
179 tuple = get_tuple(skb, &ipv4, &tcp);
180 if (!tuple)
181 return TC_ACT_SHOT;
182
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.
187 */
188 if (tcp)
189 ret = handle_tcp(skb, tuple, ipv4);
190 else
191 ret = handle_udp(skb, tuple, ipv4);
192
193 return ret == 0 ? TC_ACT_OK : TC_ACT_SHOT;
194 }
195