Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Linux Socket Filter - Kernel level socket filtering | |
4 | * | |
bd4cf0ed AS |
5 | * Based on the design of the Berkeley Packet Filter. The new |
6 | * internal format has been designed by PLUMgrid: | |
1da177e4 | 7 | * |
bd4cf0ed AS |
8 | * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com |
9 | * | |
10 | * Authors: | |
11 | * | |
12 | * Jay Schulist <jschlst@samba.org> | |
13 | * Alexei Starovoitov <ast@plumgrid.com> | |
14 | * Daniel Borkmann <dborkman@redhat.com> | |
1da177e4 | 15 | * |
1da177e4 | 16 | * Andi Kleen - Fix a few bad bugs and races. |
4df95ff4 | 17 | * Kris Katterjohn - Added many additional checks in bpf_check_classic() |
1da177e4 LT |
18 | */ |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/types.h> | |
1da177e4 LT |
22 | #include <linux/mm.h> |
23 | #include <linux/fcntl.h> | |
24 | #include <linux/socket.h> | |
91b8270f | 25 | #include <linux/sock_diag.h> |
1da177e4 LT |
26 | #include <linux/in.h> |
27 | #include <linux/inet.h> | |
28 | #include <linux/netdevice.h> | |
29 | #include <linux/if_packet.h> | |
c491680f | 30 | #include <linux/if_arp.h> |
5a0e3ad6 | 31 | #include <linux/gfp.h> |
d74bad4e | 32 | #include <net/inet_common.h> |
1da177e4 LT |
33 | #include <net/ip.h> |
34 | #include <net/protocol.h> | |
4738c1db | 35 | #include <net/netlink.h> |
1da177e4 | 36 | #include <linux/skbuff.h> |
604326b4 | 37 | #include <linux/skmsg.h> |
1da177e4 | 38 | #include <net/sock.h> |
10b89ee4 | 39 | #include <net/flow_dissector.h> |
1da177e4 LT |
40 | #include <linux/errno.h> |
41 | #include <linux/timer.h> | |
7c0f6ba6 | 42 | #include <linux/uaccess.h> |
40daafc8 | 43 | #include <asm/unaligned.h> |
d66f2b91 | 44 | #include <asm/cmpxchg.h> |
1da177e4 | 45 | #include <linux/filter.h> |
86e4ca66 | 46 | #include <linux/ratelimit.h> |
46b325c7 | 47 | #include <linux/seccomp.h> |
f3335031 | 48 | #include <linux/if_vlan.h> |
89aa0758 | 49 | #include <linux/bpf.h> |
af7ec138 | 50 | #include <linux/btf.h> |
d691f9e8 | 51 | #include <net/sch_generic.h> |
8d20aabe | 52 | #include <net/cls_cgroup.h> |
d3aa45ce | 53 | #include <net/dst_metadata.h> |
c46646d0 | 54 | #include <net/dst.h> |
538950a1 | 55 | #include <net/sock_reuseport.h> |
b1d9fc41 | 56 | #include <net/busy_poll.h> |
8c4b4c7e | 57 | #include <net/tcp.h> |
12bed760 | 58 | #include <net/xfrm.h> |
6acc9b43 | 59 | #include <net/udp.h> |
5acaee0a | 60 | #include <linux/bpf_trace.h> |
02671e23 | 61 | #include <net/xdp_sock.h> |
87f5fc7e | 62 | #include <linux/inetdevice.h> |
6acc9b43 JS |
63 | #include <net/inet_hashtables.h> |
64 | #include <net/inet6_hashtables.h> | |
87f5fc7e | 65 | #include <net/ip_fib.h> |
5481d73f | 66 | #include <net/nexthop.h> |
87f5fc7e DA |
67 | #include <net/flow.h> |
68 | #include <net/arp.h> | |
fe94cc29 | 69 | #include <net/ipv6.h> |
6acc9b43 | 70 | #include <net/net_namespace.h> |
fe94cc29 MX |
71 | #include <linux/seg6_local.h> |
72 | #include <net/seg6.h> | |
73 | #include <net/seg6_local.h> | |
52f27877 | 74 | #include <net/lwtunnel.h> |
3616d08b | 75 | #include <net/ipv6_stubs.h> |
6ac99e8f | 76 | #include <net/bpf_sk_storage.h> |
478cfbdf | 77 | #include <net/transp_v6.h> |
c9a0f3b8 | 78 | #include <linux/btf_ids.h> |
1da177e4 | 79 | |
1df8f55a MKL |
80 | static const struct bpf_func_proto * |
81 | bpf_sk_base_func_proto(enum bpf_func_id func_id); | |
82 | ||
b1ea9ff6 | 83 | int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len) |
4d295e54 CH |
84 | { |
85 | if (in_compat_syscall()) { | |
86 | struct compat_sock_fprog f32; | |
87 | ||
88 | if (len != sizeof(f32)) | |
89 | return -EINVAL; | |
b1ea9ff6 | 90 | if (copy_from_sockptr(&f32, src, sizeof(f32))) |
4d295e54 CH |
91 | return -EFAULT; |
92 | memset(dst, 0, sizeof(*dst)); | |
93 | dst->len = f32.len; | |
94 | dst->filter = compat_ptr(f32.filter); | |
95 | } else { | |
96 | if (len != sizeof(*dst)) | |
97 | return -EINVAL; | |
b1ea9ff6 | 98 | if (copy_from_sockptr(dst, src, sizeof(*dst))) |
4d295e54 CH |
99 | return -EFAULT; |
100 | } | |
101 | ||
102 | return 0; | |
103 | } | |
104 | EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user); | |
105 | ||
43db6d65 | 106 | /** |
f4979fce | 107 | * sk_filter_trim_cap - run a packet through a socket filter |
43db6d65 SH |
108 | * @sk: sock associated with &sk_buff |
109 | * @skb: buffer to filter | |
f4979fce | 110 | * @cap: limit on how short the eBPF program may trim the packet |
43db6d65 | 111 | * |
ff936a04 AS |
112 | * Run the eBPF program and then cut skb->data to correct size returned by |
113 | * the program. If pkt_len is 0 we toss packet. If skb->len is smaller | |
43db6d65 | 114 | * than pkt_len we keep whole skb->data. This is the socket level |
ff936a04 | 115 | * wrapper to BPF_PROG_RUN. It returns 0 if the packet should |
43db6d65 SH |
116 | * be accepted or -EPERM if the packet should be tossed. |
117 | * | |
118 | */ | |
f4979fce | 119 | int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap) |
43db6d65 SH |
120 | { |
121 | int err; | |
122 | struct sk_filter *filter; | |
123 | ||
c93bdd0e MG |
124 | /* |
125 | * If the skb was allocated from pfmemalloc reserves, only | |
126 | * allow SOCK_MEMALLOC sockets to use it as this socket is | |
127 | * helping free memory | |
128 | */ | |
8fe809a9 ED |
129 | if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) { |
130 | NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP); | |
c93bdd0e | 131 | return -ENOMEM; |
8fe809a9 | 132 | } |
c11cd3a6 DM |
133 | err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb); |
134 | if (err) | |
135 | return err; | |
136 | ||
43db6d65 SH |
137 | err = security_sock_rcv_skb(sk, skb); |
138 | if (err) | |
139 | return err; | |
140 | ||
80f8f102 ED |
141 | rcu_read_lock(); |
142 | filter = rcu_dereference(sk->sk_filter); | |
43db6d65 | 143 | if (filter) { |
8f917bba WB |
144 | struct sock *save_sk = skb->sk; |
145 | unsigned int pkt_len; | |
146 | ||
147 | skb->sk = sk; | |
148 | pkt_len = bpf_prog_run_save_cb(filter->prog, skb); | |
8f917bba | 149 | skb->sk = save_sk; |
d1f496fd | 150 | err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM; |
43db6d65 | 151 | } |
80f8f102 | 152 | rcu_read_unlock(); |
43db6d65 SH |
153 | |
154 | return err; | |
155 | } | |
f4979fce | 156 | EXPORT_SYMBOL(sk_filter_trim_cap); |
43db6d65 | 157 | |
b390134c | 158 | BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb) |
bd4cf0ed | 159 | { |
f3694e00 | 160 | return skb_get_poff(skb); |
bd4cf0ed AS |
161 | } |
162 | ||
b390134c | 163 | BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x) |
bd4cf0ed | 164 | { |
bd4cf0ed AS |
165 | struct nlattr *nla; |
166 | ||
167 | if (skb_is_nonlinear(skb)) | |
168 | return 0; | |
169 | ||
05ab8f26 MK |
170 | if (skb->len < sizeof(struct nlattr)) |
171 | return 0; | |
172 | ||
30743837 | 173 | if (a > skb->len - sizeof(struct nlattr)) |
bd4cf0ed AS |
174 | return 0; |
175 | ||
30743837 | 176 | nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); |
bd4cf0ed AS |
177 | if (nla) |
178 | return (void *) nla - (void *) skb->data; | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
b390134c | 183 | BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x) |
bd4cf0ed | 184 | { |
bd4cf0ed AS |
185 | struct nlattr *nla; |
186 | ||
187 | if (skb_is_nonlinear(skb)) | |
188 | return 0; | |
189 | ||
05ab8f26 MK |
190 | if (skb->len < sizeof(struct nlattr)) |
191 | return 0; | |
192 | ||
30743837 | 193 | if (a > skb->len - sizeof(struct nlattr)) |
bd4cf0ed AS |
194 | return 0; |
195 | ||
30743837 DB |
196 | nla = (struct nlattr *) &skb->data[a]; |
197 | if (nla->nla_len > skb->len - a) | |
bd4cf0ed AS |
198 | return 0; |
199 | ||
30743837 | 200 | nla = nla_find_nested(nla, x); |
bd4cf0ed AS |
201 | if (nla) |
202 | return (void *) nla - (void *) skb->data; | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
e0cea7ce DB |
207 | BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *, |
208 | data, int, headlen, int, offset) | |
209 | { | |
210 | u8 tmp, *ptr; | |
211 | const int len = sizeof(tmp); | |
212 | ||
213 | if (offset >= 0) { | |
214 | if (headlen - offset >= len) | |
215 | return *(u8 *)(data + offset); | |
216 | if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) | |
217 | return tmp; | |
218 | } else { | |
219 | ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); | |
220 | if (likely(ptr)) | |
221 | return *(u8 *)ptr; | |
222 | } | |
223 | ||
224 | return -EFAULT; | |
225 | } | |
226 | ||
227 | BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb, | |
228 | int, offset) | |
229 | { | |
230 | return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len, | |
231 | offset); | |
232 | } | |
233 | ||
234 | BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *, | |
235 | data, int, headlen, int, offset) | |
236 | { | |
237 | u16 tmp, *ptr; | |
238 | const int len = sizeof(tmp); | |
239 | ||
240 | if (offset >= 0) { | |
241 | if (headlen - offset >= len) | |
242 | return get_unaligned_be16(data + offset); | |
243 | if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) | |
244 | return be16_to_cpu(tmp); | |
245 | } else { | |
246 | ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); | |
247 | if (likely(ptr)) | |
248 | return get_unaligned_be16(ptr); | |
249 | } | |
250 | ||
251 | return -EFAULT; | |
252 | } | |
253 | ||
254 | BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb, | |
255 | int, offset) | |
256 | { | |
257 | return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len, | |
258 | offset); | |
259 | } | |
260 | ||
261 | BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *, | |
262 | data, int, headlen, int, offset) | |
263 | { | |
264 | u32 tmp, *ptr; | |
265 | const int len = sizeof(tmp); | |
266 | ||
267 | if (likely(offset >= 0)) { | |
268 | if (headlen - offset >= len) | |
269 | return get_unaligned_be32(data + offset); | |
270 | if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp))) | |
271 | return be32_to_cpu(tmp); | |
272 | } else { | |
273 | ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len); | |
274 | if (likely(ptr)) | |
275 | return get_unaligned_be32(ptr); | |
276 | } | |
277 | ||
278 | return -EFAULT; | |
279 | } | |
280 | ||
281 | BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb, | |
282 | int, offset) | |
283 | { | |
284 | return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len, | |
285 | offset); | |
286 | } | |
287 | ||
9bac3d6d AS |
288 | static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg, |
289 | struct bpf_insn *insn_buf) | |
290 | { | |
291 | struct bpf_insn *insn = insn_buf; | |
292 | ||
293 | switch (skb_field) { | |
294 | case SKF_AD_MARK: | |
c593642c | 295 | BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4); |
9bac3d6d AS |
296 | |
297 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
298 | offsetof(struct sk_buff, mark)); | |
299 | break; | |
300 | ||
301 | case SKF_AD_PKTTYPE: | |
302 | *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET()); | |
303 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX); | |
304 | #ifdef __BIG_ENDIAN_BITFIELD | |
305 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5); | |
306 | #endif | |
307 | break; | |
308 | ||
309 | case SKF_AD_QUEUE: | |
c593642c | 310 | BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2); |
9bac3d6d AS |
311 | |
312 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
313 | offsetof(struct sk_buff, queue_mapping)); | |
314 | break; | |
c2497395 | 315 | |
c2497395 | 316 | case SKF_AD_VLAN_TAG: |
c593642c | 317 | BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2); |
c2497395 AS |
318 | |
319 | /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */ | |
320 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
321 | offsetof(struct sk_buff, vlan_tci)); | |
9c212255 MM |
322 | break; |
323 | case SKF_AD_VLAN_TAG_PRESENT: | |
324 | *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET()); | |
325 | if (PKT_VLAN_PRESENT_BIT) | |
326 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT); | |
327 | if (PKT_VLAN_PRESENT_BIT < 7) | |
c2497395 | 328 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1); |
c2497395 | 329 | break; |
9bac3d6d AS |
330 | } |
331 | ||
332 | return insn - insn_buf; | |
333 | } | |
334 | ||
bd4cf0ed | 335 | static bool convert_bpf_extensions(struct sock_filter *fp, |
2695fb55 | 336 | struct bpf_insn **insnp) |
bd4cf0ed | 337 | { |
2695fb55 | 338 | struct bpf_insn *insn = *insnp; |
9bac3d6d | 339 | u32 cnt; |
bd4cf0ed AS |
340 | |
341 | switch (fp->k) { | |
342 | case SKF_AD_OFF + SKF_AD_PROTOCOL: | |
c593642c | 343 | BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2); |
0b8c707d DB |
344 | |
345 | /* A = *(u16 *) (CTX + offsetof(protocol)) */ | |
346 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, | |
347 | offsetof(struct sk_buff, protocol)); | |
348 | /* A = ntohs(A) [emitting a nop or swap16] */ | |
349 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); | |
bd4cf0ed AS |
350 | break; |
351 | ||
352 | case SKF_AD_OFF + SKF_AD_PKTTYPE: | |
9bac3d6d AS |
353 | cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn); |
354 | insn += cnt - 1; | |
bd4cf0ed AS |
355 | break; |
356 | ||
357 | case SKF_AD_OFF + SKF_AD_IFINDEX: | |
358 | case SKF_AD_OFF + SKF_AD_HATYPE: | |
c593642c PB |
359 | BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4); |
360 | BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2); | |
f8f6d679 | 361 | |
f035a515 | 362 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), |
f8f6d679 DB |
363 | BPF_REG_TMP, BPF_REG_CTX, |
364 | offsetof(struct sk_buff, dev)); | |
365 | /* if (tmp != 0) goto pc + 1 */ | |
366 | *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); | |
367 | *insn++ = BPF_EXIT_INSN(); | |
368 | if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) | |
369 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, | |
370 | offsetof(struct net_device, ifindex)); | |
371 | else | |
372 | *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, | |
373 | offsetof(struct net_device, type)); | |
bd4cf0ed AS |
374 | break; |
375 | ||
376 | case SKF_AD_OFF + SKF_AD_MARK: | |
9bac3d6d AS |
377 | cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn); |
378 | insn += cnt - 1; | |
bd4cf0ed AS |
379 | break; |
380 | ||
381 | case SKF_AD_OFF + SKF_AD_RXHASH: | |
c593642c | 382 | BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4); |
bd4cf0ed | 383 | |
9739eef1 AS |
384 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, |
385 | offsetof(struct sk_buff, hash)); | |
bd4cf0ed AS |
386 | break; |
387 | ||
388 | case SKF_AD_OFF + SKF_AD_QUEUE: | |
9bac3d6d AS |
389 | cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn); |
390 | insn += cnt - 1; | |
bd4cf0ed AS |
391 | break; |
392 | ||
393 | case SKF_AD_OFF + SKF_AD_VLAN_TAG: | |
c2497395 AS |
394 | cnt = convert_skb_access(SKF_AD_VLAN_TAG, |
395 | BPF_REG_A, BPF_REG_CTX, insn); | |
396 | insn += cnt - 1; | |
397 | break; | |
bd4cf0ed | 398 | |
c2497395 AS |
399 | case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: |
400 | cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, | |
401 | BPF_REG_A, BPF_REG_CTX, insn); | |
402 | insn += cnt - 1; | |
bd4cf0ed AS |
403 | break; |
404 | ||
27cd5452 | 405 | case SKF_AD_OFF + SKF_AD_VLAN_TPID: |
c593642c | 406 | BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2); |
27cd5452 MS |
407 | |
408 | /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */ | |
409 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, | |
410 | offsetof(struct sk_buff, vlan_proto)); | |
411 | /* A = ntohs(A) [emitting a nop or swap16] */ | |
412 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); | |
413 | break; | |
414 | ||
bd4cf0ed AS |
415 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: |
416 | case SKF_AD_OFF + SKF_AD_NLATTR: | |
417 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: | |
418 | case SKF_AD_OFF + SKF_AD_CPU: | |
4cd3675e | 419 | case SKF_AD_OFF + SKF_AD_RANDOM: |
e430f34e | 420 | /* arg1 = CTX */ |
f8f6d679 | 421 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); |
bd4cf0ed | 422 | /* arg2 = A */ |
f8f6d679 | 423 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); |
bd4cf0ed | 424 | /* arg3 = X */ |
f8f6d679 | 425 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); |
e430f34e | 426 | /* Emit call(arg1=CTX, arg2=A, arg3=X) */ |
bd4cf0ed AS |
427 | switch (fp->k) { |
428 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: | |
b390134c | 429 | *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset); |
bd4cf0ed AS |
430 | break; |
431 | case SKF_AD_OFF + SKF_AD_NLATTR: | |
b390134c | 432 | *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr); |
bd4cf0ed AS |
433 | break; |
434 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: | |
b390134c | 435 | *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest); |
bd4cf0ed AS |
436 | break; |
437 | case SKF_AD_OFF + SKF_AD_CPU: | |
b390134c | 438 | *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id); |
bd4cf0ed | 439 | break; |
4cd3675e | 440 | case SKF_AD_OFF + SKF_AD_RANDOM: |
3ad00405 DB |
441 | *insn = BPF_EMIT_CALL(bpf_user_rnd_u32); |
442 | bpf_user_rnd_init_once(); | |
4cd3675e | 443 | break; |
bd4cf0ed AS |
444 | } |
445 | break; | |
446 | ||
447 | case SKF_AD_OFF + SKF_AD_ALU_XOR_X: | |
9739eef1 AS |
448 | /* A ^= X */ |
449 | *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); | |
bd4cf0ed AS |
450 | break; |
451 | ||
452 | default: | |
453 | /* This is just a dummy call to avoid letting the compiler | |
454 | * evict __bpf_call_base() as an optimization. Placed here | |
455 | * where no-one bothers. | |
456 | */ | |
457 | BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); | |
458 | return false; | |
459 | } | |
460 | ||
461 | *insnp = insn; | |
462 | return true; | |
463 | } | |
464 | ||
e0cea7ce DB |
465 | static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp) |
466 | { | |
467 | const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS); | |
468 | int size = bpf_size_to_bytes(BPF_SIZE(fp->code)); | |
469 | bool endian = BPF_SIZE(fp->code) == BPF_H || | |
470 | BPF_SIZE(fp->code) == BPF_W; | |
471 | bool indirect = BPF_MODE(fp->code) == BPF_IND; | |
472 | const int ip_align = NET_IP_ALIGN; | |
473 | struct bpf_insn *insn = *insnp; | |
474 | int offset = fp->k; | |
475 | ||
476 | if (!indirect && | |
477 | ((unaligned_ok && offset >= 0) || | |
478 | (!unaligned_ok && offset >= 0 && | |
479 | offset + ip_align >= 0 && | |
480 | offset + ip_align % size == 0))) { | |
59ee4129 DB |
481 | bool ldx_off_ok = offset <= S16_MAX; |
482 | ||
e0cea7ce | 483 | *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H); |
d8f3e978 DM |
484 | if (offset) |
485 | *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset); | |
59ee4129 DB |
486 | *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP, |
487 | size, 2 + endian + (!ldx_off_ok * 2)); | |
488 | if (ldx_off_ok) { | |
489 | *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, | |
490 | BPF_REG_D, offset); | |
491 | } else { | |
492 | *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D); | |
493 | *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset); | |
494 | *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, | |
495 | BPF_REG_TMP, 0); | |
496 | } | |
e0cea7ce DB |
497 | if (endian) |
498 | *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8); | |
499 | *insn++ = BPF_JMP_A(8); | |
500 | } | |
501 | ||
502 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); | |
503 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D); | |
504 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H); | |
505 | if (!indirect) { | |
506 | *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset); | |
507 | } else { | |
508 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X); | |
509 | if (fp->k) | |
510 | *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset); | |
511 | } | |
512 | ||
513 | switch (BPF_SIZE(fp->code)) { | |
514 | case BPF_B: | |
515 | *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8); | |
516 | break; | |
517 | case BPF_H: | |
518 | *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16); | |
519 | break; | |
520 | case BPF_W: | |
521 | *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32); | |
522 | break; | |
523 | default: | |
524 | return false; | |
525 | } | |
526 | ||
527 | *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2); | |
528 | *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); | |
529 | *insn = BPF_EXIT_INSN(); | |
530 | ||
531 | *insnp = insn; | |
532 | return true; | |
533 | } | |
534 | ||
bd4cf0ed | 535 | /** |
8fb575ca | 536 | * bpf_convert_filter - convert filter program |
bd4cf0ed AS |
537 | * @prog: the user passed filter program |
538 | * @len: the length of the user passed filter program | |
50bbfed9 | 539 | * @new_prog: allocated 'struct bpf_prog' or NULL |
bd4cf0ed | 540 | * @new_len: pointer to store length of converted program |
e0cea7ce | 541 | * @seen_ld_abs: bool whether we've seen ld_abs/ind |
bd4cf0ed | 542 | * |
1f504ec9 TK |
543 | * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn' |
544 | * style extended BPF (eBPF). | |
bd4cf0ed AS |
545 | * Conversion workflow: |
546 | * | |
547 | * 1) First pass for calculating the new program length: | |
e0cea7ce | 548 | * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs) |
bd4cf0ed AS |
549 | * |
550 | * 2) 2nd pass to remap in two passes: 1st pass finds new | |
551 | * jump offsets, 2nd pass remapping: | |
e0cea7ce | 552 | * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs) |
bd4cf0ed | 553 | */ |
d9e12f42 | 554 | static int bpf_convert_filter(struct sock_filter *prog, int len, |
e0cea7ce DB |
555 | struct bpf_prog *new_prog, int *new_len, |
556 | bool *seen_ld_abs) | |
bd4cf0ed | 557 | { |
50bbfed9 AS |
558 | int new_flen = 0, pass = 0, target, i, stack_off; |
559 | struct bpf_insn *new_insn, *first_insn = NULL; | |
bd4cf0ed AS |
560 | struct sock_filter *fp; |
561 | int *addrs = NULL; | |
562 | u8 bpf_src; | |
563 | ||
564 | BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); | |
30743837 | 565 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
bd4cf0ed | 566 | |
6f9a093b | 567 | if (len <= 0 || len > BPF_MAXINSNS) |
bd4cf0ed AS |
568 | return -EINVAL; |
569 | ||
570 | if (new_prog) { | |
50bbfed9 | 571 | first_insn = new_prog->insnsi; |
658da937 DB |
572 | addrs = kcalloc(len, sizeof(*addrs), |
573 | GFP_KERNEL | __GFP_NOWARN); | |
bd4cf0ed AS |
574 | if (!addrs) |
575 | return -ENOMEM; | |
576 | } | |
577 | ||
578 | do_pass: | |
50bbfed9 | 579 | new_insn = first_insn; |
bd4cf0ed AS |
580 | fp = prog; |
581 | ||
8b614aeb | 582 | /* Classic BPF related prologue emission. */ |
50bbfed9 | 583 | if (new_prog) { |
8b614aeb DB |
584 | /* Classic BPF expects A and X to be reset first. These need |
585 | * to be guaranteed to be the first two instructions. | |
586 | */ | |
1d621674 DB |
587 | *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); |
588 | *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X); | |
8b614aeb DB |
589 | |
590 | /* All programs must keep CTX in callee saved BPF_REG_CTX. | |
591 | * In eBPF case it's done by the compiler, here we need to | |
592 | * do this ourself. Initial CTX is present in BPF_REG_ARG1. | |
593 | */ | |
594 | *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); | |
e0cea7ce DB |
595 | if (*seen_ld_abs) { |
596 | /* For packet access in classic BPF, cache skb->data | |
597 | * in callee-saved BPF R8 and skb->len - skb->data_len | |
598 | * (headlen) in BPF R9. Since classic BPF is read-only | |
599 | * on CTX, we only need to cache it once. | |
600 | */ | |
601 | *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), | |
602 | BPF_REG_D, BPF_REG_CTX, | |
603 | offsetof(struct sk_buff, data)); | |
604 | *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX, | |
605 | offsetof(struct sk_buff, len)); | |
606 | *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX, | |
607 | offsetof(struct sk_buff, data_len)); | |
608 | *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP); | |
609 | } | |
8b614aeb DB |
610 | } else { |
611 | new_insn += 3; | |
612 | } | |
bd4cf0ed AS |
613 | |
614 | for (i = 0; i < len; fp++, i++) { | |
e0cea7ce | 615 | struct bpf_insn tmp_insns[32] = { }; |
2695fb55 | 616 | struct bpf_insn *insn = tmp_insns; |
bd4cf0ed AS |
617 | |
618 | if (addrs) | |
50bbfed9 | 619 | addrs[i] = new_insn - first_insn; |
bd4cf0ed AS |
620 | |
621 | switch (fp->code) { | |
622 | /* All arithmetic insns and skb loads map as-is. */ | |
623 | case BPF_ALU | BPF_ADD | BPF_X: | |
624 | case BPF_ALU | BPF_ADD | BPF_K: | |
625 | case BPF_ALU | BPF_SUB | BPF_X: | |
626 | case BPF_ALU | BPF_SUB | BPF_K: | |
627 | case BPF_ALU | BPF_AND | BPF_X: | |
628 | case BPF_ALU | BPF_AND | BPF_K: | |
629 | case BPF_ALU | BPF_OR | BPF_X: | |
630 | case BPF_ALU | BPF_OR | BPF_K: | |
631 | case BPF_ALU | BPF_LSH | BPF_X: | |
632 | case BPF_ALU | BPF_LSH | BPF_K: | |
633 | case BPF_ALU | BPF_RSH | BPF_X: | |
634 | case BPF_ALU | BPF_RSH | BPF_K: | |
635 | case BPF_ALU | BPF_XOR | BPF_X: | |
636 | case BPF_ALU | BPF_XOR | BPF_K: | |
637 | case BPF_ALU | BPF_MUL | BPF_X: | |
638 | case BPF_ALU | BPF_MUL | BPF_K: | |
639 | case BPF_ALU | BPF_DIV | BPF_X: | |
640 | case BPF_ALU | BPF_DIV | BPF_K: | |
641 | case BPF_ALU | BPF_MOD | BPF_X: | |
642 | case BPF_ALU | BPF_MOD | BPF_K: | |
643 | case BPF_ALU | BPF_NEG: | |
644 | case BPF_LD | BPF_ABS | BPF_W: | |
645 | case BPF_LD | BPF_ABS | BPF_H: | |
646 | case BPF_LD | BPF_ABS | BPF_B: | |
647 | case BPF_LD | BPF_IND | BPF_W: | |
648 | case BPF_LD | BPF_IND | BPF_H: | |
649 | case BPF_LD | BPF_IND | BPF_B: | |
650 | /* Check for overloaded BPF extension and | |
651 | * directly convert it if found, otherwise | |
652 | * just move on with mapping. | |
653 | */ | |
654 | if (BPF_CLASS(fp->code) == BPF_LD && | |
655 | BPF_MODE(fp->code) == BPF_ABS && | |
656 | convert_bpf_extensions(fp, &insn)) | |
657 | break; | |
e0cea7ce DB |
658 | if (BPF_CLASS(fp->code) == BPF_LD && |
659 | convert_bpf_ld_abs(fp, &insn)) { | |
660 | *seen_ld_abs = true; | |
661 | break; | |
662 | } | |
bd4cf0ed | 663 | |
68fda450 | 664 | if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) || |
f6b1b3bf | 665 | fp->code == (BPF_ALU | BPF_MOD | BPF_X)) { |
68fda450 | 666 | *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X); |
f6b1b3bf DB |
667 | /* Error with exception code on div/mod by 0. |
668 | * For cBPF programs, this was always return 0. | |
669 | */ | |
670 | *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2); | |
671 | *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); | |
672 | *insn++ = BPF_EXIT_INSN(); | |
673 | } | |
68fda450 | 674 | |
f8f6d679 | 675 | *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k); |
bd4cf0ed AS |
676 | break; |
677 | ||
f8f6d679 DB |
678 | /* Jump transformation cannot use BPF block macros |
679 | * everywhere as offset calculation and target updates | |
680 | * require a bit more work than the rest, i.e. jump | |
681 | * opcodes map as-is, but offsets need adjustment. | |
682 | */ | |
683 | ||
684 | #define BPF_EMIT_JMP \ | |
bd4cf0ed | 685 | do { \ |
050fad7c DB |
686 | const s32 off_min = S16_MIN, off_max = S16_MAX; \ |
687 | s32 off; \ | |
688 | \ | |
bd4cf0ed AS |
689 | if (target >= len || target < 0) \ |
690 | goto err; \ | |
050fad7c | 691 | off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ |
bd4cf0ed | 692 | /* Adjust pc relative offset for 2nd or 3rd insn. */ \ |
050fad7c DB |
693 | off -= insn - tmp_insns; \ |
694 | /* Reject anything not fitting into insn->off. */ \ | |
695 | if (off < off_min || off > off_max) \ | |
696 | goto err; \ | |
697 | insn->off = off; \ | |
bd4cf0ed AS |
698 | } while (0) |
699 | ||
f8f6d679 DB |
700 | case BPF_JMP | BPF_JA: |
701 | target = i + fp->k + 1; | |
702 | insn->code = fp->code; | |
703 | BPF_EMIT_JMP; | |
bd4cf0ed AS |
704 | break; |
705 | ||
706 | case BPF_JMP | BPF_JEQ | BPF_K: | |
707 | case BPF_JMP | BPF_JEQ | BPF_X: | |
708 | case BPF_JMP | BPF_JSET | BPF_K: | |
709 | case BPF_JMP | BPF_JSET | BPF_X: | |
710 | case BPF_JMP | BPF_JGT | BPF_K: | |
711 | case BPF_JMP | BPF_JGT | BPF_X: | |
712 | case BPF_JMP | BPF_JGE | BPF_K: | |
713 | case BPF_JMP | BPF_JGE | BPF_X: | |
714 | if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { | |
715 | /* BPF immediates are signed, zero extend | |
716 | * immediate into tmp register and use it | |
717 | * in compare insn. | |
718 | */ | |
f8f6d679 | 719 | *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); |
bd4cf0ed | 720 | |
e430f34e AS |
721 | insn->dst_reg = BPF_REG_A; |
722 | insn->src_reg = BPF_REG_TMP; | |
bd4cf0ed AS |
723 | bpf_src = BPF_X; |
724 | } else { | |
e430f34e | 725 | insn->dst_reg = BPF_REG_A; |
bd4cf0ed AS |
726 | insn->imm = fp->k; |
727 | bpf_src = BPF_SRC(fp->code); | |
19539ce7 | 728 | insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0; |
1da177e4 | 729 | } |
bd4cf0ed AS |
730 | |
731 | /* Common case where 'jump_false' is next insn. */ | |
732 | if (fp->jf == 0) { | |
733 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; | |
734 | target = i + fp->jt + 1; | |
f8f6d679 | 735 | BPF_EMIT_JMP; |
bd4cf0ed | 736 | break; |
1da177e4 | 737 | } |
bd4cf0ed | 738 | |
92b31a9a DB |
739 | /* Convert some jumps when 'jump_true' is next insn. */ |
740 | if (fp->jt == 0) { | |
741 | switch (BPF_OP(fp->code)) { | |
742 | case BPF_JEQ: | |
743 | insn->code = BPF_JMP | BPF_JNE | bpf_src; | |
744 | break; | |
745 | case BPF_JGT: | |
746 | insn->code = BPF_JMP | BPF_JLE | bpf_src; | |
747 | break; | |
748 | case BPF_JGE: | |
749 | insn->code = BPF_JMP | BPF_JLT | bpf_src; | |
750 | break; | |
751 | default: | |
752 | goto jmp_rest; | |
753 | } | |
754 | ||
bd4cf0ed | 755 | target = i + fp->jf + 1; |
f8f6d679 | 756 | BPF_EMIT_JMP; |
bd4cf0ed | 757 | break; |
0b05b2a4 | 758 | } |
92b31a9a | 759 | jmp_rest: |
bd4cf0ed AS |
760 | /* Other jumps are mapped into two insns: Jxx and JA. */ |
761 | target = i + fp->jt + 1; | |
762 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; | |
f8f6d679 | 763 | BPF_EMIT_JMP; |
bd4cf0ed AS |
764 | insn++; |
765 | ||
766 | insn->code = BPF_JMP | BPF_JA; | |
767 | target = i + fp->jf + 1; | |
f8f6d679 | 768 | BPF_EMIT_JMP; |
bd4cf0ed AS |
769 | break; |
770 | ||
771 | /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ | |
e0cea7ce DB |
772 | case BPF_LDX | BPF_MSH | BPF_B: { |
773 | struct sock_filter tmp = { | |
774 | .code = BPF_LD | BPF_ABS | BPF_B, | |
775 | .k = fp->k, | |
776 | }; | |
777 | ||
778 | *seen_ld_abs = true; | |
779 | ||
780 | /* X = A */ | |
781 | *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); | |
1268e253 | 782 | /* A = BPF_R0 = *(u8 *) (skb->data + K) */ |
e0cea7ce DB |
783 | convert_bpf_ld_abs(&tmp, &insn); |
784 | insn++; | |
9739eef1 | 785 | /* A &= 0xf */ |
f8f6d679 | 786 | *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); |
9739eef1 | 787 | /* A <<= 2 */ |
f8f6d679 | 788 | *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); |
e0cea7ce DB |
789 | /* tmp = X */ |
790 | *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X); | |
9739eef1 | 791 | /* X = A */ |
f8f6d679 | 792 | *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
9739eef1 | 793 | /* A = tmp */ |
f8f6d679 | 794 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); |
bd4cf0ed | 795 | break; |
e0cea7ce | 796 | } |
6205b9cf DB |
797 | /* RET_K is remaped into 2 insns. RET_A case doesn't need an |
798 | * extra mov as BPF_REG_0 is already mapped into BPF_REG_A. | |
799 | */ | |
bd4cf0ed AS |
800 | case BPF_RET | BPF_A: |
801 | case BPF_RET | BPF_K: | |
6205b9cf DB |
802 | if (BPF_RVAL(fp->code) == BPF_K) |
803 | *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0, | |
804 | 0, fp->k); | |
9739eef1 | 805 | *insn = BPF_EXIT_INSN(); |
bd4cf0ed AS |
806 | break; |
807 | ||
808 | /* Store to stack. */ | |
809 | case BPF_ST: | |
810 | case BPF_STX: | |
50bbfed9 | 811 | stack_off = fp->k * 4 + 4; |
f8f6d679 DB |
812 | *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == |
813 | BPF_ST ? BPF_REG_A : BPF_REG_X, | |
50bbfed9 AS |
814 | -stack_off); |
815 | /* check_load_and_stores() verifies that classic BPF can | |
816 | * load from stack only after write, so tracking | |
817 | * stack_depth for ST|STX insns is enough | |
818 | */ | |
819 | if (new_prog && new_prog->aux->stack_depth < stack_off) | |
820 | new_prog->aux->stack_depth = stack_off; | |
bd4cf0ed AS |
821 | break; |
822 | ||
823 | /* Load from stack. */ | |
824 | case BPF_LD | BPF_MEM: | |
825 | case BPF_LDX | BPF_MEM: | |
50bbfed9 | 826 | stack_off = fp->k * 4 + 4; |
f8f6d679 DB |
827 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
828 | BPF_REG_A : BPF_REG_X, BPF_REG_FP, | |
50bbfed9 | 829 | -stack_off); |
bd4cf0ed AS |
830 | break; |
831 | ||
832 | /* A = K or X = K */ | |
833 | case BPF_LD | BPF_IMM: | |
834 | case BPF_LDX | BPF_IMM: | |
f8f6d679 DB |
835 | *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? |
836 | BPF_REG_A : BPF_REG_X, fp->k); | |
bd4cf0ed AS |
837 | break; |
838 | ||
839 | /* X = A */ | |
840 | case BPF_MISC | BPF_TAX: | |
f8f6d679 | 841 | *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); |
bd4cf0ed AS |
842 | break; |
843 | ||
844 | /* A = X */ | |
845 | case BPF_MISC | BPF_TXA: | |
f8f6d679 | 846 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); |
bd4cf0ed AS |
847 | break; |
848 | ||
849 | /* A = skb->len or X = skb->len */ | |
850 | case BPF_LD | BPF_W | BPF_LEN: | |
851 | case BPF_LDX | BPF_W | BPF_LEN: | |
f8f6d679 DB |
852 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? |
853 | BPF_REG_A : BPF_REG_X, BPF_REG_CTX, | |
854 | offsetof(struct sk_buff, len)); | |
bd4cf0ed AS |
855 | break; |
856 | ||
f8f6d679 | 857 | /* Access seccomp_data fields. */ |
bd4cf0ed | 858 | case BPF_LDX | BPF_ABS | BPF_W: |
9739eef1 AS |
859 | /* A = *(u32 *) (ctx + K) */ |
860 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k); | |
bd4cf0ed AS |
861 | break; |
862 | ||
ca9f1fd2 | 863 | /* Unknown instruction. */ |
1da177e4 | 864 | default: |
bd4cf0ed | 865 | goto err; |
1da177e4 | 866 | } |
bd4cf0ed AS |
867 | |
868 | insn++; | |
869 | if (new_prog) | |
870 | memcpy(new_insn, tmp_insns, | |
871 | sizeof(*insn) * (insn - tmp_insns)); | |
bd4cf0ed | 872 | new_insn += insn - tmp_insns; |
1da177e4 LT |
873 | } |
874 | ||
bd4cf0ed AS |
875 | if (!new_prog) { |
876 | /* Only calculating new length. */ | |
50bbfed9 | 877 | *new_len = new_insn - first_insn; |
e0cea7ce DB |
878 | if (*seen_ld_abs) |
879 | *new_len += 4; /* Prologue bits. */ | |
bd4cf0ed AS |
880 | return 0; |
881 | } | |
882 | ||
883 | pass++; | |
50bbfed9 AS |
884 | if (new_flen != new_insn - first_insn) { |
885 | new_flen = new_insn - first_insn; | |
bd4cf0ed AS |
886 | if (pass > 2) |
887 | goto err; | |
bd4cf0ed AS |
888 | goto do_pass; |
889 | } | |
890 | ||
891 | kfree(addrs); | |
892 | BUG_ON(*new_len != new_flen); | |
1da177e4 | 893 | return 0; |
bd4cf0ed AS |
894 | err: |
895 | kfree(addrs); | |
896 | return -EINVAL; | |
1da177e4 LT |
897 | } |
898 | ||
bd4cf0ed | 899 | /* Security: |
bd4cf0ed | 900 | * |
2d5311e4 | 901 | * As we dont want to clear mem[] array for each packet going through |
8ea6e345 | 902 | * __bpf_prog_run(), we check that filter loaded by user never try to read |
2d5311e4 | 903 | * a cell if not previously written, and we check all branches to be sure |
25985edc | 904 | * a malicious user doesn't try to abuse us. |
2d5311e4 | 905 | */ |
ec31a05c | 906 | static int check_load_and_stores(const struct sock_filter *filter, int flen) |
2d5311e4 | 907 | { |
34805931 | 908 | u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ |
2d5311e4 ED |
909 | int pc, ret = 0; |
910 | ||
911 | BUILD_BUG_ON(BPF_MEMWORDS > 16); | |
34805931 | 912 | |
99e72a0f | 913 | masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); |
2d5311e4 ED |
914 | if (!masks) |
915 | return -ENOMEM; | |
34805931 | 916 | |
2d5311e4 ED |
917 | memset(masks, 0xff, flen * sizeof(*masks)); |
918 | ||
919 | for (pc = 0; pc < flen; pc++) { | |
920 | memvalid &= masks[pc]; | |
921 | ||
922 | switch (filter[pc].code) { | |
34805931 DB |
923 | case BPF_ST: |
924 | case BPF_STX: | |
2d5311e4 ED |
925 | memvalid |= (1 << filter[pc].k); |
926 | break; | |
34805931 DB |
927 | case BPF_LD | BPF_MEM: |
928 | case BPF_LDX | BPF_MEM: | |
2d5311e4 ED |
929 | if (!(memvalid & (1 << filter[pc].k))) { |
930 | ret = -EINVAL; | |
931 | goto error; | |
932 | } | |
933 | break; | |
34805931 DB |
934 | case BPF_JMP | BPF_JA: |
935 | /* A jump must set masks on target */ | |
2d5311e4 ED |
936 | masks[pc + 1 + filter[pc].k] &= memvalid; |
937 | memvalid = ~0; | |
938 | break; | |
34805931 DB |
939 | case BPF_JMP | BPF_JEQ | BPF_K: |
940 | case BPF_JMP | BPF_JEQ | BPF_X: | |
941 | case BPF_JMP | BPF_JGE | BPF_K: | |
942 | case BPF_JMP | BPF_JGE | BPF_X: | |
943 | case BPF_JMP | BPF_JGT | BPF_K: | |
944 | case BPF_JMP | BPF_JGT | BPF_X: | |
945 | case BPF_JMP | BPF_JSET | BPF_K: | |
946 | case BPF_JMP | BPF_JSET | BPF_X: | |
947 | /* A jump must set masks on targets */ | |
2d5311e4 ED |
948 | masks[pc + 1 + filter[pc].jt] &= memvalid; |
949 | masks[pc + 1 + filter[pc].jf] &= memvalid; | |
950 | memvalid = ~0; | |
951 | break; | |
952 | } | |
953 | } | |
954 | error: | |
955 | kfree(masks); | |
956 | return ret; | |
957 | } | |
958 | ||
34805931 DB |
959 | static bool chk_code_allowed(u16 code_to_probe) |
960 | { | |
961 | static const bool codes[] = { | |
962 | /* 32 bit ALU operations */ | |
963 | [BPF_ALU | BPF_ADD | BPF_K] = true, | |
964 | [BPF_ALU | BPF_ADD | BPF_X] = true, | |
965 | [BPF_ALU | BPF_SUB | BPF_K] = true, | |
966 | [BPF_ALU | BPF_SUB | BPF_X] = true, | |
967 | [BPF_ALU | BPF_MUL | BPF_K] = true, | |
968 | [BPF_ALU | BPF_MUL | BPF_X] = true, | |
969 | [BPF_ALU | BPF_DIV | BPF_K] = true, | |
970 | [BPF_ALU | BPF_DIV | BPF_X] = true, | |
971 | [BPF_ALU | BPF_MOD | BPF_K] = true, | |
972 | [BPF_ALU | BPF_MOD | BPF_X] = true, | |
973 | [BPF_ALU | BPF_AND | BPF_K] = true, | |
974 | [BPF_ALU | BPF_AND | BPF_X] = true, | |
975 | [BPF_ALU | BPF_OR | BPF_K] = true, | |
976 | [BPF_ALU | BPF_OR | BPF_X] = true, | |
977 | [BPF_ALU | BPF_XOR | BPF_K] = true, | |
978 | [BPF_ALU | BPF_XOR | BPF_X] = true, | |
979 | [BPF_ALU | BPF_LSH | BPF_K] = true, | |
980 | [BPF_ALU | BPF_LSH | BPF_X] = true, | |
981 | [BPF_ALU | BPF_RSH | BPF_K] = true, | |
982 | [BPF_ALU | BPF_RSH | BPF_X] = true, | |
983 | [BPF_ALU | BPF_NEG] = true, | |
984 | /* Load instructions */ | |
985 | [BPF_LD | BPF_W | BPF_ABS] = true, | |
986 | [BPF_LD | BPF_H | BPF_ABS] = true, | |
987 | [BPF_LD | BPF_B | BPF_ABS] = true, | |
988 | [BPF_LD | BPF_W | BPF_LEN] = true, | |
989 | [BPF_LD | BPF_W | BPF_IND] = true, | |
990 | [BPF_LD | BPF_H | BPF_IND] = true, | |
991 | [BPF_LD | BPF_B | BPF_IND] = true, | |
992 | [BPF_LD | BPF_IMM] = true, | |
993 | [BPF_LD | BPF_MEM] = true, | |
994 | [BPF_LDX | BPF_W | BPF_LEN] = true, | |
995 | [BPF_LDX | BPF_B | BPF_MSH] = true, | |
996 | [BPF_LDX | BPF_IMM] = true, | |
997 | [BPF_LDX | BPF_MEM] = true, | |
998 | /* Store instructions */ | |
999 | [BPF_ST] = true, | |
1000 | [BPF_STX] = true, | |
1001 | /* Misc instructions */ | |
1002 | [BPF_MISC | BPF_TAX] = true, | |
1003 | [BPF_MISC | BPF_TXA] = true, | |
1004 | /* Return instructions */ | |
1005 | [BPF_RET | BPF_K] = true, | |
1006 | [BPF_RET | BPF_A] = true, | |
1007 | /* Jump instructions */ | |
1008 | [BPF_JMP | BPF_JA] = true, | |
1009 | [BPF_JMP | BPF_JEQ | BPF_K] = true, | |
1010 | [BPF_JMP | BPF_JEQ | BPF_X] = true, | |
1011 | [BPF_JMP | BPF_JGE | BPF_K] = true, | |
1012 | [BPF_JMP | BPF_JGE | BPF_X] = true, | |
1013 | [BPF_JMP | BPF_JGT | BPF_K] = true, | |
1014 | [BPF_JMP | BPF_JGT | BPF_X] = true, | |
1015 | [BPF_JMP | BPF_JSET | BPF_K] = true, | |
1016 | [BPF_JMP | BPF_JSET | BPF_X] = true, | |
1017 | }; | |
1018 | ||
1019 | if (code_to_probe >= ARRAY_SIZE(codes)) | |
1020 | return false; | |
1021 | ||
1022 | return codes[code_to_probe]; | |
1023 | } | |
1024 | ||
f7bd9e36 DB |
1025 | static bool bpf_check_basics_ok(const struct sock_filter *filter, |
1026 | unsigned int flen) | |
1027 | { | |
1028 | if (filter == NULL) | |
1029 | return false; | |
1030 | if (flen == 0 || flen > BPF_MAXINSNS) | |
1031 | return false; | |
1032 | ||
1033 | return true; | |
1034 | } | |
1035 | ||
1da177e4 | 1036 | /** |
4df95ff4 | 1037 | * bpf_check_classic - verify socket filter code |
1da177e4 LT |
1038 | * @filter: filter to verify |
1039 | * @flen: length of filter | |
1040 | * | |
1041 | * Check the user's filter code. If we let some ugly | |
1042 | * filter code slip through kaboom! The filter must contain | |
93699863 KK |
1043 | * no references or jumps that are out of range, no illegal |
1044 | * instructions, and must end with a RET instruction. | |
1da177e4 | 1045 | * |
7b11f69f KK |
1046 | * All jumps are forward as they are not signed. |
1047 | * | |
1048 | * Returns 0 if the rule set is legal or -EINVAL if not. | |
1da177e4 | 1049 | */ |
d9e12f42 NS |
1050 | static int bpf_check_classic(const struct sock_filter *filter, |
1051 | unsigned int flen) | |
1da177e4 | 1052 | { |
aa1113d9 | 1053 | bool anc_found; |
34805931 | 1054 | int pc; |
1da177e4 | 1055 | |
34805931 | 1056 | /* Check the filter code now */ |
1da177e4 | 1057 | for (pc = 0; pc < flen; pc++) { |
ec31a05c | 1058 | const struct sock_filter *ftest = &filter[pc]; |
93699863 | 1059 | |
34805931 DB |
1060 | /* May we actually operate on this code? */ |
1061 | if (!chk_code_allowed(ftest->code)) | |
cba328fc | 1062 | return -EINVAL; |
34805931 | 1063 | |
93699863 | 1064 | /* Some instructions need special checks */ |
34805931 DB |
1065 | switch (ftest->code) { |
1066 | case BPF_ALU | BPF_DIV | BPF_K: | |
1067 | case BPF_ALU | BPF_MOD | BPF_K: | |
1068 | /* Check for division by zero */ | |
b6069a95 ED |
1069 | if (ftest->k == 0) |
1070 | return -EINVAL; | |
1071 | break; | |
229394e8 RV |
1072 | case BPF_ALU | BPF_LSH | BPF_K: |
1073 | case BPF_ALU | BPF_RSH | BPF_K: | |
1074 | if (ftest->k >= 32) | |
1075 | return -EINVAL; | |
1076 | break; | |
34805931 DB |
1077 | case BPF_LD | BPF_MEM: |
1078 | case BPF_LDX | BPF_MEM: | |
1079 | case BPF_ST: | |
1080 | case BPF_STX: | |
1081 | /* Check for invalid memory addresses */ | |
93699863 KK |
1082 | if (ftest->k >= BPF_MEMWORDS) |
1083 | return -EINVAL; | |
1084 | break; | |
34805931 DB |
1085 | case BPF_JMP | BPF_JA: |
1086 | /* Note, the large ftest->k might cause loops. | |
93699863 KK |
1087 | * Compare this with conditional jumps below, |
1088 | * where offsets are limited. --ANK (981016) | |
1089 | */ | |
34805931 | 1090 | if (ftest->k >= (unsigned int)(flen - pc - 1)) |
93699863 | 1091 | return -EINVAL; |
01f2f3f6 | 1092 | break; |
34805931 DB |
1093 | case BPF_JMP | BPF_JEQ | BPF_K: |
1094 | case BPF_JMP | BPF_JEQ | BPF_X: | |
1095 | case BPF_JMP | BPF_JGE | BPF_K: | |
1096 | case BPF_JMP | BPF_JGE | BPF_X: | |
1097 | case BPF_JMP | BPF_JGT | BPF_K: | |
1098 | case BPF_JMP | BPF_JGT | BPF_X: | |
1099 | case BPF_JMP | BPF_JSET | BPF_K: | |
1100 | case BPF_JMP | BPF_JSET | BPF_X: | |
1101 | /* Both conditionals must be safe */ | |
e35bedf3 | 1102 | if (pc + ftest->jt + 1 >= flen || |
93699863 KK |
1103 | pc + ftest->jf + 1 >= flen) |
1104 | return -EINVAL; | |
cba328fc | 1105 | break; |
34805931 DB |
1106 | case BPF_LD | BPF_W | BPF_ABS: |
1107 | case BPF_LD | BPF_H | BPF_ABS: | |
1108 | case BPF_LD | BPF_B | BPF_ABS: | |
aa1113d9 | 1109 | anc_found = false; |
34805931 DB |
1110 | if (bpf_anc_helper(ftest) & BPF_ANC) |
1111 | anc_found = true; | |
1112 | /* Ancillary operation unknown or unsupported */ | |
aa1113d9 DB |
1113 | if (anc_found == false && ftest->k >= SKF_AD_OFF) |
1114 | return -EINVAL; | |
01f2f3f6 HPP |
1115 | } |
1116 | } | |
93699863 | 1117 | |
34805931 | 1118 | /* Last instruction must be a RET code */ |
01f2f3f6 | 1119 | switch (filter[flen - 1].code) { |
34805931 DB |
1120 | case BPF_RET | BPF_K: |
1121 | case BPF_RET | BPF_A: | |
2d5311e4 | 1122 | return check_load_and_stores(filter, flen); |
cba328fc | 1123 | } |
34805931 | 1124 | |
cba328fc | 1125 | return -EINVAL; |
1da177e4 LT |
1126 | } |
1127 | ||
7ae457c1 AS |
1128 | static int bpf_prog_store_orig_filter(struct bpf_prog *fp, |
1129 | const struct sock_fprog *fprog) | |
a3ea269b | 1130 | { |
009937e7 | 1131 | unsigned int fsize = bpf_classic_proglen(fprog); |
a3ea269b DB |
1132 | struct sock_fprog_kern *fkprog; |
1133 | ||
1134 | fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); | |
1135 | if (!fp->orig_prog) | |
1136 | return -ENOMEM; | |
1137 | ||
1138 | fkprog = fp->orig_prog; | |
1139 | fkprog->len = fprog->len; | |
658da937 DB |
1140 | |
1141 | fkprog->filter = kmemdup(fp->insns, fsize, | |
1142 | GFP_KERNEL | __GFP_NOWARN); | |
a3ea269b DB |
1143 | if (!fkprog->filter) { |
1144 | kfree(fp->orig_prog); | |
1145 | return -ENOMEM; | |
1146 | } | |
1147 | ||
1148 | return 0; | |
1149 | } | |
1150 | ||
7ae457c1 | 1151 | static void bpf_release_orig_filter(struct bpf_prog *fp) |
a3ea269b DB |
1152 | { |
1153 | struct sock_fprog_kern *fprog = fp->orig_prog; | |
1154 | ||
1155 | if (fprog) { | |
1156 | kfree(fprog->filter); | |
1157 | kfree(fprog); | |
1158 | } | |
1159 | } | |
1160 | ||
7ae457c1 AS |
1161 | static void __bpf_prog_release(struct bpf_prog *prog) |
1162 | { | |
24701ece | 1163 | if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { |
89aa0758 AS |
1164 | bpf_prog_put(prog); |
1165 | } else { | |
1166 | bpf_release_orig_filter(prog); | |
1167 | bpf_prog_free(prog); | |
1168 | } | |
7ae457c1 AS |
1169 | } |
1170 | ||
34c5bd66 PN |
1171 | static void __sk_filter_release(struct sk_filter *fp) |
1172 | { | |
7ae457c1 AS |
1173 | __bpf_prog_release(fp->prog); |
1174 | kfree(fp); | |
34c5bd66 PN |
1175 | } |
1176 | ||
47e958ea | 1177 | /** |
46bcf14f | 1178 | * sk_filter_release_rcu - Release a socket filter by rcu_head |
47e958ea PE |
1179 | * @rcu: rcu_head that contains the sk_filter to free |
1180 | */ | |
fbc907f0 | 1181 | static void sk_filter_release_rcu(struct rcu_head *rcu) |
47e958ea PE |
1182 | { |
1183 | struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); | |
1184 | ||
34c5bd66 | 1185 | __sk_filter_release(fp); |
47e958ea | 1186 | } |
fbc907f0 DB |
1187 | |
1188 | /** | |
1189 | * sk_filter_release - release a socket filter | |
1190 | * @fp: filter to remove | |
1191 | * | |
1192 | * Remove a filter from a socket and release its resources. | |
1193 | */ | |
1194 | static void sk_filter_release(struct sk_filter *fp) | |
1195 | { | |
4c355cdf | 1196 | if (refcount_dec_and_test(&fp->refcnt)) |
fbc907f0 DB |
1197 | call_rcu(&fp->rcu, sk_filter_release_rcu); |
1198 | } | |
1199 | ||
1200 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) | |
1201 | { | |
7ae457c1 | 1202 | u32 filter_size = bpf_prog_size(fp->prog->len); |
fbc907f0 | 1203 | |
278571ba AS |
1204 | atomic_sub(filter_size, &sk->sk_omem_alloc); |
1205 | sk_filter_release(fp); | |
fbc907f0 | 1206 | } |
47e958ea | 1207 | |
278571ba AS |
1208 | /* try to charge the socket memory if there is space available |
1209 | * return true on success | |
1210 | */ | |
4c355cdf | 1211 | static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp) |
bd4cf0ed | 1212 | { |
7ae457c1 | 1213 | u32 filter_size = bpf_prog_size(fp->prog->len); |
278571ba AS |
1214 | |
1215 | /* same check as in sock_kmalloc() */ | |
1216 | if (filter_size <= sysctl_optmem_max && | |
1217 | atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { | |
278571ba AS |
1218 | atomic_add(filter_size, &sk->sk_omem_alloc); |
1219 | return true; | |
bd4cf0ed | 1220 | } |
278571ba | 1221 | return false; |
bd4cf0ed AS |
1222 | } |
1223 | ||
4c355cdf RE |
1224 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) |
1225 | { | |
eefca20e ED |
1226 | if (!refcount_inc_not_zero(&fp->refcnt)) |
1227 | return false; | |
1228 | ||
1229 | if (!__sk_filter_charge(sk, fp)) { | |
1230 | sk_filter_release(fp); | |
1231 | return false; | |
1232 | } | |
1233 | return true; | |
4c355cdf RE |
1234 | } |
1235 | ||
7ae457c1 | 1236 | static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) |
bd4cf0ed AS |
1237 | { |
1238 | struct sock_filter *old_prog; | |
7ae457c1 | 1239 | struct bpf_prog *old_fp; |
34805931 | 1240 | int err, new_len, old_len = fp->len; |
e0cea7ce | 1241 | bool seen_ld_abs = false; |
bd4cf0ed AS |
1242 | |
1243 | /* We are free to overwrite insns et al right here as it | |
1244 | * won't be used at this point in time anymore internally | |
1245 | * after the migration to the internal BPF instruction | |
1246 | * representation. | |
1247 | */ | |
1248 | BUILD_BUG_ON(sizeof(struct sock_filter) != | |
2695fb55 | 1249 | sizeof(struct bpf_insn)); |
bd4cf0ed | 1250 | |
bd4cf0ed AS |
1251 | /* Conversion cannot happen on overlapping memory areas, |
1252 | * so we need to keep the user BPF around until the 2nd | |
1253 | * pass. At this time, the user BPF is stored in fp->insns. | |
1254 | */ | |
1255 | old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), | |
658da937 | 1256 | GFP_KERNEL | __GFP_NOWARN); |
bd4cf0ed AS |
1257 | if (!old_prog) { |
1258 | err = -ENOMEM; | |
1259 | goto out_err; | |
1260 | } | |
1261 | ||
1262 | /* 1st pass: calculate the new program length. */ | |
e0cea7ce DB |
1263 | err = bpf_convert_filter(old_prog, old_len, NULL, &new_len, |
1264 | &seen_ld_abs); | |
bd4cf0ed AS |
1265 | if (err) |
1266 | goto out_err_free; | |
1267 | ||
1268 | /* Expand fp for appending the new filter representation. */ | |
1269 | old_fp = fp; | |
60a3b225 | 1270 | fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); |
bd4cf0ed AS |
1271 | if (!fp) { |
1272 | /* The old_fp is still around in case we couldn't | |
1273 | * allocate new memory, so uncharge on that one. | |
1274 | */ | |
1275 | fp = old_fp; | |
1276 | err = -ENOMEM; | |
1277 | goto out_err_free; | |
1278 | } | |
1279 | ||
bd4cf0ed AS |
1280 | fp->len = new_len; |
1281 | ||
2695fb55 | 1282 | /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ |
e0cea7ce DB |
1283 | err = bpf_convert_filter(old_prog, old_len, fp, &new_len, |
1284 | &seen_ld_abs); | |
bd4cf0ed | 1285 | if (err) |
8fb575ca | 1286 | /* 2nd bpf_convert_filter() can fail only if it fails |
bd4cf0ed AS |
1287 | * to allocate memory, remapping must succeed. Note, |
1288 | * that at this time old_fp has already been released | |
278571ba | 1289 | * by krealloc(). |
bd4cf0ed AS |
1290 | */ |
1291 | goto out_err_free; | |
1292 | ||
d1c55ab5 | 1293 | fp = bpf_prog_select_runtime(fp, &err); |
290af866 AS |
1294 | if (err) |
1295 | goto out_err_free; | |
5fe821a9 | 1296 | |
bd4cf0ed AS |
1297 | kfree(old_prog); |
1298 | return fp; | |
1299 | ||
1300 | out_err_free: | |
1301 | kfree(old_prog); | |
1302 | out_err: | |
7ae457c1 | 1303 | __bpf_prog_release(fp); |
bd4cf0ed AS |
1304 | return ERR_PTR(err); |
1305 | } | |
1306 | ||
ac67eb2c DB |
1307 | static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, |
1308 | bpf_aux_classic_check_t trans) | |
302d6637 JP |
1309 | { |
1310 | int err; | |
1311 | ||
bd4cf0ed | 1312 | fp->bpf_func = NULL; |
a91263d5 | 1313 | fp->jited = 0; |
302d6637 | 1314 | |
4df95ff4 | 1315 | err = bpf_check_classic(fp->insns, fp->len); |
418c96ac | 1316 | if (err) { |
7ae457c1 | 1317 | __bpf_prog_release(fp); |
bd4cf0ed | 1318 | return ERR_PTR(err); |
418c96ac | 1319 | } |
302d6637 | 1320 | |
4ae92bc7 NS |
1321 | /* There might be additional checks and transformations |
1322 | * needed on classic filters, f.e. in case of seccomp. | |
1323 | */ | |
1324 | if (trans) { | |
1325 | err = trans(fp->insns, fp->len); | |
1326 | if (err) { | |
1327 | __bpf_prog_release(fp); | |
1328 | return ERR_PTR(err); | |
1329 | } | |
1330 | } | |
1331 | ||
bd4cf0ed AS |
1332 | /* Probe if we can JIT compile the filter and if so, do |
1333 | * the compilation of the filter. | |
1334 | */ | |
302d6637 | 1335 | bpf_jit_compile(fp); |
bd4cf0ed AS |
1336 | |
1337 | /* JIT compiler couldn't process this filter, so do the | |
1338 | * internal BPF translation for the optimized interpreter. | |
1339 | */ | |
5fe821a9 | 1340 | if (!fp->jited) |
7ae457c1 | 1341 | fp = bpf_migrate_filter(fp); |
bd4cf0ed AS |
1342 | |
1343 | return fp; | |
302d6637 JP |
1344 | } |
1345 | ||
1346 | /** | |
7ae457c1 | 1347 | * bpf_prog_create - create an unattached filter |
c6c4b97c | 1348 | * @pfp: the unattached filter that is created |
677a9fd3 | 1349 | * @fprog: the filter program |
302d6637 | 1350 | * |
c6c4b97c | 1351 | * Create a filter independent of any socket. We first run some |
302d6637 JP |
1352 | * sanity checks on it to make sure it does not explode on us later. |
1353 | * If an error occurs or there is insufficient memory for the filter | |
1354 | * a negative errno code is returned. On success the return is zero. | |
1355 | */ | |
7ae457c1 | 1356 | int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog) |
302d6637 | 1357 | { |
009937e7 | 1358 | unsigned int fsize = bpf_classic_proglen(fprog); |
7ae457c1 | 1359 | struct bpf_prog *fp; |
302d6637 JP |
1360 | |
1361 | /* Make sure new filter is there and in the right amounts. */ | |
f7bd9e36 | 1362 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) |
302d6637 JP |
1363 | return -EINVAL; |
1364 | ||
60a3b225 | 1365 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
302d6637 JP |
1366 | if (!fp) |
1367 | return -ENOMEM; | |
a3ea269b | 1368 | |
302d6637 JP |
1369 | memcpy(fp->insns, fprog->filter, fsize); |
1370 | ||
302d6637 | 1371 | fp->len = fprog->len; |
a3ea269b DB |
1372 | /* Since unattached filters are not copied back to user |
1373 | * space through sk_get_filter(), we do not need to hold | |
1374 | * a copy here, and can spare us the work. | |
1375 | */ | |
1376 | fp->orig_prog = NULL; | |
302d6637 | 1377 | |
7ae457c1 | 1378 | /* bpf_prepare_filter() already takes care of freeing |
bd4cf0ed AS |
1379 | * memory in case something goes wrong. |
1380 | */ | |
4ae92bc7 | 1381 | fp = bpf_prepare_filter(fp, NULL); |
bd4cf0ed AS |
1382 | if (IS_ERR(fp)) |
1383 | return PTR_ERR(fp); | |
302d6637 JP |
1384 | |
1385 | *pfp = fp; | |
1386 | return 0; | |
302d6637 | 1387 | } |
7ae457c1 | 1388 | EXPORT_SYMBOL_GPL(bpf_prog_create); |
302d6637 | 1389 | |
ac67eb2c DB |
1390 | /** |
1391 | * bpf_prog_create_from_user - create an unattached filter from user buffer | |
1392 | * @pfp: the unattached filter that is created | |
1393 | * @fprog: the filter program | |
1394 | * @trans: post-classic verifier transformation handler | |
bab18991 | 1395 | * @save_orig: save classic BPF program |
ac67eb2c DB |
1396 | * |
1397 | * This function effectively does the same as bpf_prog_create(), only | |
1398 | * that it builds up its insns buffer from user space provided buffer. | |
1399 | * It also allows for passing a bpf_aux_classic_check_t handler. | |
1400 | */ | |
1401 | int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog, | |
bab18991 | 1402 | bpf_aux_classic_check_t trans, bool save_orig) |
ac67eb2c DB |
1403 | { |
1404 | unsigned int fsize = bpf_classic_proglen(fprog); | |
1405 | struct bpf_prog *fp; | |
bab18991 | 1406 | int err; |
ac67eb2c DB |
1407 | |
1408 | /* Make sure new filter is there and in the right amounts. */ | |
f7bd9e36 | 1409 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) |
ac67eb2c DB |
1410 | return -EINVAL; |
1411 | ||
1412 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); | |
1413 | if (!fp) | |
1414 | return -ENOMEM; | |
1415 | ||
1416 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { | |
1417 | __bpf_prog_free(fp); | |
1418 | return -EFAULT; | |
1419 | } | |
1420 | ||
1421 | fp->len = fprog->len; | |
ac67eb2c DB |
1422 | fp->orig_prog = NULL; |
1423 | ||
bab18991 DB |
1424 | if (save_orig) { |
1425 | err = bpf_prog_store_orig_filter(fp, fprog); | |
1426 | if (err) { | |
1427 | __bpf_prog_free(fp); | |
1428 | return -ENOMEM; | |
1429 | } | |
1430 | } | |
1431 | ||
ac67eb2c DB |
1432 | /* bpf_prepare_filter() already takes care of freeing |
1433 | * memory in case something goes wrong. | |
1434 | */ | |
1435 | fp = bpf_prepare_filter(fp, trans); | |
1436 | if (IS_ERR(fp)) | |
1437 | return PTR_ERR(fp); | |
1438 | ||
1439 | *pfp = fp; | |
1440 | return 0; | |
1441 | } | |
2ea273d7 | 1442 | EXPORT_SYMBOL_GPL(bpf_prog_create_from_user); |
ac67eb2c | 1443 | |
7ae457c1 | 1444 | void bpf_prog_destroy(struct bpf_prog *fp) |
302d6637 | 1445 | { |
7ae457c1 | 1446 | __bpf_prog_release(fp); |
302d6637 | 1447 | } |
7ae457c1 | 1448 | EXPORT_SYMBOL_GPL(bpf_prog_destroy); |
302d6637 | 1449 | |
8ced425e | 1450 | static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) |
49b31e57 DB |
1451 | { |
1452 | struct sk_filter *fp, *old_fp; | |
1453 | ||
1454 | fp = kmalloc(sizeof(*fp), GFP_KERNEL); | |
1455 | if (!fp) | |
1456 | return -ENOMEM; | |
1457 | ||
1458 | fp->prog = prog; | |
49b31e57 | 1459 | |
4c355cdf | 1460 | if (!__sk_filter_charge(sk, fp)) { |
49b31e57 DB |
1461 | kfree(fp); |
1462 | return -ENOMEM; | |
1463 | } | |
4c355cdf | 1464 | refcount_set(&fp->refcnt, 1); |
49b31e57 | 1465 | |
8ced425e HFS |
1466 | old_fp = rcu_dereference_protected(sk->sk_filter, |
1467 | lockdep_sock_is_held(sk)); | |
49b31e57 | 1468 | rcu_assign_pointer(sk->sk_filter, fp); |
8ced425e | 1469 | |
49b31e57 DB |
1470 | if (old_fp) |
1471 | sk_filter_uncharge(sk, old_fp); | |
1472 | ||
1473 | return 0; | |
1474 | } | |
1475 | ||
538950a1 CG |
1476 | static |
1477 | struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk) | |
1da177e4 | 1478 | { |
009937e7 | 1479 | unsigned int fsize = bpf_classic_proglen(fprog); |
7ae457c1 | 1480 | struct bpf_prog *prog; |
1da177e4 LT |
1481 | int err; |
1482 | ||
d59577b6 | 1483 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
538950a1 | 1484 | return ERR_PTR(-EPERM); |
d59577b6 | 1485 | |
1da177e4 | 1486 | /* Make sure new filter is there and in the right amounts. */ |
f7bd9e36 | 1487 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) |
538950a1 | 1488 | return ERR_PTR(-EINVAL); |
1da177e4 | 1489 | |
f7bd9e36 | 1490 | prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); |
7ae457c1 | 1491 | if (!prog) |
538950a1 | 1492 | return ERR_PTR(-ENOMEM); |
a3ea269b | 1493 | |
7ae457c1 | 1494 | if (copy_from_user(prog->insns, fprog->filter, fsize)) { |
c0d1379a | 1495 | __bpf_prog_free(prog); |
538950a1 | 1496 | return ERR_PTR(-EFAULT); |
1da177e4 LT |
1497 | } |
1498 | ||
7ae457c1 | 1499 | prog->len = fprog->len; |
1da177e4 | 1500 | |
7ae457c1 | 1501 | err = bpf_prog_store_orig_filter(prog, fprog); |
a3ea269b | 1502 | if (err) { |
c0d1379a | 1503 | __bpf_prog_free(prog); |
538950a1 | 1504 | return ERR_PTR(-ENOMEM); |
a3ea269b DB |
1505 | } |
1506 | ||
7ae457c1 | 1507 | /* bpf_prepare_filter() already takes care of freeing |
bd4cf0ed AS |
1508 | * memory in case something goes wrong. |
1509 | */ | |
538950a1 CG |
1510 | return bpf_prepare_filter(prog, NULL); |
1511 | } | |
1512 | ||
1513 | /** | |
1514 | * sk_attach_filter - attach a socket filter | |
1515 | * @fprog: the filter program | |
1516 | * @sk: the socket to use | |
1517 | * | |
1518 | * Attach the user's filter code. We first run some sanity checks on | |
1519 | * it to make sure it does not explode on us later. If an error | |
1520 | * occurs or there is insufficient memory for the filter a negative | |
1521 | * errno code is returned. On success the return is zero. | |
1522 | */ | |
8ced425e | 1523 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
538950a1 CG |
1524 | { |
1525 | struct bpf_prog *prog = __get_filter(fprog, sk); | |
1526 | int err; | |
1527 | ||
7ae457c1 AS |
1528 | if (IS_ERR(prog)) |
1529 | return PTR_ERR(prog); | |
1530 | ||
8ced425e | 1531 | err = __sk_attach_prog(prog, sk); |
49b31e57 | 1532 | if (err < 0) { |
7ae457c1 | 1533 | __bpf_prog_release(prog); |
49b31e57 | 1534 | return err; |
278571ba AS |
1535 | } |
1536 | ||
d3904b73 | 1537 | return 0; |
1da177e4 | 1538 | } |
8ced425e | 1539 | EXPORT_SYMBOL_GPL(sk_attach_filter); |
1da177e4 | 1540 | |
538950a1 | 1541 | int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk) |
89aa0758 | 1542 | { |
538950a1 | 1543 | struct bpf_prog *prog = __get_filter(fprog, sk); |
49b31e57 | 1544 | int err; |
89aa0758 | 1545 | |
538950a1 CG |
1546 | if (IS_ERR(prog)) |
1547 | return PTR_ERR(prog); | |
1548 | ||
8217ca65 MKL |
1549 | if (bpf_prog_size(prog->len) > sysctl_optmem_max) |
1550 | err = -ENOMEM; | |
1551 | else | |
1552 | err = reuseport_attach_prog(sk, prog); | |
1553 | ||
1554 | if (err) | |
538950a1 | 1555 | __bpf_prog_release(prog); |
538950a1 | 1556 | |
8217ca65 | 1557 | return err; |
538950a1 CG |
1558 | } |
1559 | ||
1560 | static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk) | |
1561 | { | |
89aa0758 | 1562 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
538950a1 | 1563 | return ERR_PTR(-EPERM); |
89aa0758 | 1564 | |
113214be | 1565 | return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); |
538950a1 CG |
1566 | } |
1567 | ||
1568 | int sk_attach_bpf(u32 ufd, struct sock *sk) | |
1569 | { | |
1570 | struct bpf_prog *prog = __get_bpf(ufd, sk); | |
1571 | int err; | |
1572 | ||
1573 | if (IS_ERR(prog)) | |
1574 | return PTR_ERR(prog); | |
1575 | ||
8ced425e | 1576 | err = __sk_attach_prog(prog, sk); |
49b31e57 | 1577 | if (err < 0) { |
89aa0758 | 1578 | bpf_prog_put(prog); |
49b31e57 | 1579 | return err; |
89aa0758 AS |
1580 | } |
1581 | ||
89aa0758 AS |
1582 | return 0; |
1583 | } | |
1584 | ||
538950a1 CG |
1585 | int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk) |
1586 | { | |
8217ca65 | 1587 | struct bpf_prog *prog; |
538950a1 CG |
1588 | int err; |
1589 | ||
8217ca65 MKL |
1590 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
1591 | return -EPERM; | |
1592 | ||
1593 | prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); | |
45586c70 | 1594 | if (PTR_ERR(prog) == -EINVAL) |
8217ca65 | 1595 | prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT); |
538950a1 CG |
1596 | if (IS_ERR(prog)) |
1597 | return PTR_ERR(prog); | |
1598 | ||
8217ca65 MKL |
1599 | if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) { |
1600 | /* Like other non BPF_PROG_TYPE_SOCKET_FILTER | |
1601 | * bpf prog (e.g. sockmap). It depends on the | |
1602 | * limitation imposed by bpf_prog_load(). | |
1603 | * Hence, sysctl_optmem_max is not checked. | |
1604 | */ | |
1605 | if ((sk->sk_type != SOCK_STREAM && | |
1606 | sk->sk_type != SOCK_DGRAM) || | |
1607 | (sk->sk_protocol != IPPROTO_UDP && | |
1608 | sk->sk_protocol != IPPROTO_TCP) || | |
1609 | (sk->sk_family != AF_INET && | |
1610 | sk->sk_family != AF_INET6)) { | |
1611 | err = -ENOTSUPP; | |
1612 | goto err_prog_put; | |
1613 | } | |
1614 | } else { | |
1615 | /* BPF_PROG_TYPE_SOCKET_FILTER */ | |
1616 | if (bpf_prog_size(prog->len) > sysctl_optmem_max) { | |
1617 | err = -ENOMEM; | |
1618 | goto err_prog_put; | |
1619 | } | |
538950a1 CG |
1620 | } |
1621 | ||
8217ca65 MKL |
1622 | err = reuseport_attach_prog(sk, prog); |
1623 | err_prog_put: | |
1624 | if (err) | |
1625 | bpf_prog_put(prog); | |
1626 | ||
1627 | return err; | |
1628 | } | |
1629 | ||
1630 | void sk_reuseport_prog_free(struct bpf_prog *prog) | |
1631 | { | |
1632 | if (!prog) | |
1633 | return; | |
1634 | ||
1635 | if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) | |
1636 | bpf_prog_put(prog); | |
1637 | else | |
1638 | bpf_prog_destroy(prog); | |
538950a1 CG |
1639 | } |
1640 | ||
21cafc1d DB |
1641 | struct bpf_scratchpad { |
1642 | union { | |
1643 | __be32 diff[MAX_BPF_STACK / sizeof(__be32)]; | |
1644 | u8 buff[MAX_BPF_STACK]; | |
1645 | }; | |
1646 | }; | |
1647 | ||
1648 | static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp); | |
91bc4822 | 1649 | |
5293efe6 DB |
1650 | static inline int __bpf_try_make_writable(struct sk_buff *skb, |
1651 | unsigned int write_len) | |
1652 | { | |
1653 | return skb_ensure_writable(skb, write_len); | |
1654 | } | |
1655 | ||
db58ba45 AS |
1656 | static inline int bpf_try_make_writable(struct sk_buff *skb, |
1657 | unsigned int write_len) | |
1658 | { | |
5293efe6 | 1659 | int err = __bpf_try_make_writable(skb, write_len); |
db58ba45 | 1660 | |
6aaae2b6 | 1661 | bpf_compute_data_pointers(skb); |
db58ba45 AS |
1662 | return err; |
1663 | } | |
1664 | ||
36bbef52 DB |
1665 | static int bpf_try_make_head_writable(struct sk_buff *skb) |
1666 | { | |
1667 | return bpf_try_make_writable(skb, skb_headlen(skb)); | |
1668 | } | |
1669 | ||
a2bfe6bf DB |
1670 | static inline void bpf_push_mac_rcsum(struct sk_buff *skb) |
1671 | { | |
1672 | if (skb_at_tc_ingress(skb)) | |
1673 | skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len); | |
1674 | } | |
1675 | ||
8065694e DB |
1676 | static inline void bpf_pull_mac_rcsum(struct sk_buff *skb) |
1677 | { | |
1678 | if (skb_at_tc_ingress(skb)) | |
1679 | skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len); | |
1680 | } | |
1681 | ||
f3694e00 DB |
1682 | BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset, |
1683 | const void *, from, u32, len, u64, flags) | |
608cd71a | 1684 | { |
608cd71a AS |
1685 | void *ptr; |
1686 | ||
8afd54c8 | 1687 | if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH))) |
781c53bc | 1688 | return -EINVAL; |
0ed661d5 | 1689 | if (unlikely(offset > 0xffff)) |
608cd71a | 1690 | return -EFAULT; |
db58ba45 | 1691 | if (unlikely(bpf_try_make_writable(skb, offset + len))) |
608cd71a AS |
1692 | return -EFAULT; |
1693 | ||
0ed661d5 | 1694 | ptr = skb->data + offset; |
781c53bc | 1695 | if (flags & BPF_F_RECOMPUTE_CSUM) |
479ffccc | 1696 | __skb_postpull_rcsum(skb, ptr, len, offset); |
608cd71a AS |
1697 | |
1698 | memcpy(ptr, from, len); | |
1699 | ||
781c53bc | 1700 | if (flags & BPF_F_RECOMPUTE_CSUM) |
479ffccc | 1701 | __skb_postpush_rcsum(skb, ptr, len, offset); |
8afd54c8 DB |
1702 | if (flags & BPF_F_INVALIDATE_HASH) |
1703 | skb_clear_hash(skb); | |
f8ffad69 | 1704 | |
608cd71a AS |
1705 | return 0; |
1706 | } | |
1707 | ||
577c50aa | 1708 | static const struct bpf_func_proto bpf_skb_store_bytes_proto = { |
608cd71a AS |
1709 | .func = bpf_skb_store_bytes, |
1710 | .gpl_only = false, | |
1711 | .ret_type = RET_INTEGER, | |
1712 | .arg1_type = ARG_PTR_TO_CTX, | |
1713 | .arg2_type = ARG_ANYTHING, | |
39f19ebb AS |
1714 | .arg3_type = ARG_PTR_TO_MEM, |
1715 | .arg4_type = ARG_CONST_SIZE, | |
91bc4822 AS |
1716 | .arg5_type = ARG_ANYTHING, |
1717 | }; | |
1718 | ||
f3694e00 DB |
1719 | BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset, |
1720 | void *, to, u32, len) | |
05c74e5e | 1721 | { |
05c74e5e DB |
1722 | void *ptr; |
1723 | ||
0ed661d5 | 1724 | if (unlikely(offset > 0xffff)) |
074f528e | 1725 | goto err_clear; |
05c74e5e DB |
1726 | |
1727 | ptr = skb_header_pointer(skb, offset, len, to); | |
1728 | if (unlikely(!ptr)) | |
074f528e | 1729 | goto err_clear; |
05c74e5e DB |
1730 | if (ptr != to) |
1731 | memcpy(to, ptr, len); | |
1732 | ||
1733 | return 0; | |
074f528e DB |
1734 | err_clear: |
1735 | memset(to, 0, len); | |
1736 | return -EFAULT; | |
05c74e5e DB |
1737 | } |
1738 | ||
577c50aa | 1739 | static const struct bpf_func_proto bpf_skb_load_bytes_proto = { |
05c74e5e DB |
1740 | .func = bpf_skb_load_bytes, |
1741 | .gpl_only = false, | |
1742 | .ret_type = RET_INTEGER, | |
1743 | .arg1_type = ARG_PTR_TO_CTX, | |
1744 | .arg2_type = ARG_ANYTHING, | |
39f19ebb AS |
1745 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, |
1746 | .arg4_type = ARG_CONST_SIZE, | |
05c74e5e DB |
1747 | }; |
1748 | ||
089b19a9 SF |
1749 | BPF_CALL_4(bpf_flow_dissector_load_bytes, |
1750 | const struct bpf_flow_dissector *, ctx, u32, offset, | |
1751 | void *, to, u32, len) | |
1752 | { | |
1753 | void *ptr; | |
1754 | ||
1755 | if (unlikely(offset > 0xffff)) | |
1756 | goto err_clear; | |
1757 | ||
1758 | if (unlikely(!ctx->skb)) | |
1759 | goto err_clear; | |
1760 | ||
1761 | ptr = skb_header_pointer(ctx->skb, offset, len, to); | |
1762 | if (unlikely(!ptr)) | |
1763 | goto err_clear; | |
1764 | if (ptr != to) | |
1765 | memcpy(to, ptr, len); | |
1766 | ||
1767 | return 0; | |
1768 | err_clear: | |
1769 | memset(to, 0, len); | |
1770 | return -EFAULT; | |
1771 | } | |
1772 | ||
1773 | static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = { | |
1774 | .func = bpf_flow_dissector_load_bytes, | |
1775 | .gpl_only = false, | |
1776 | .ret_type = RET_INTEGER, | |
1777 | .arg1_type = ARG_PTR_TO_CTX, | |
1778 | .arg2_type = ARG_ANYTHING, | |
1779 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, | |
1780 | .arg4_type = ARG_CONST_SIZE, | |
1781 | }; | |
1782 | ||
4e1ec56c DB |
1783 | BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb, |
1784 | u32, offset, void *, to, u32, len, u32, start_header) | |
1785 | { | |
3eee1f75 | 1786 | u8 *end = skb_tail_pointer(skb); |
0f5d82f1 | 1787 | u8 *start, *ptr; |
4e1ec56c | 1788 | |
0f5d82f1 | 1789 | if (unlikely(offset > 0xffff)) |
4e1ec56c DB |
1790 | goto err_clear; |
1791 | ||
1792 | switch (start_header) { | |
1793 | case BPF_HDR_START_MAC: | |
0f5d82f1 YZ |
1794 | if (unlikely(!skb_mac_header_was_set(skb))) |
1795 | goto err_clear; | |
1796 | start = skb_mac_header(skb); | |
4e1ec56c DB |
1797 | break; |
1798 | case BPF_HDR_START_NET: | |
0f5d82f1 | 1799 | start = skb_network_header(skb); |
4e1ec56c DB |
1800 | break; |
1801 | default: | |
1802 | goto err_clear; | |
1803 | } | |
1804 | ||
0f5d82f1 YZ |
1805 | ptr = start + offset; |
1806 | ||
1807 | if (likely(ptr + len <= end)) { | |
4e1ec56c DB |
1808 | memcpy(to, ptr, len); |
1809 | return 0; | |
1810 | } | |
1811 | ||
1812 | err_clear: | |
1813 | memset(to, 0, len); | |
1814 | return -EFAULT; | |
1815 | } | |
1816 | ||
1817 | static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = { | |
1818 | .func = bpf_skb_load_bytes_relative, | |
1819 | .gpl_only = false, | |
1820 | .ret_type = RET_INTEGER, | |
1821 | .arg1_type = ARG_PTR_TO_CTX, | |
1822 | .arg2_type = ARG_ANYTHING, | |
1823 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, | |
1824 | .arg4_type = ARG_CONST_SIZE, | |
1825 | .arg5_type = ARG_ANYTHING, | |
1826 | }; | |
1827 | ||
36bbef52 DB |
1828 | BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len) |
1829 | { | |
1830 | /* Idea is the following: should the needed direct read/write | |
1831 | * test fail during runtime, we can pull in more data and redo | |
1832 | * again, since implicitly, we invalidate previous checks here. | |
1833 | * | |
1834 | * Or, since we know how much we need to make read/writeable, | |
1835 | * this can be done once at the program beginning for direct | |
1836 | * access case. By this we overcome limitations of only current | |
1837 | * headroom being accessible. | |
1838 | */ | |
1839 | return bpf_try_make_writable(skb, len ? : skb_headlen(skb)); | |
1840 | } | |
1841 | ||
1842 | static const struct bpf_func_proto bpf_skb_pull_data_proto = { | |
1843 | .func = bpf_skb_pull_data, | |
1844 | .gpl_only = false, | |
1845 | .ret_type = RET_INTEGER, | |
1846 | .arg1_type = ARG_PTR_TO_CTX, | |
1847 | .arg2_type = ARG_ANYTHING, | |
1848 | }; | |
1849 | ||
46f8bc92 MKL |
1850 | BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk) |
1851 | { | |
46f8bc92 MKL |
1852 | return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL; |
1853 | } | |
1854 | ||
1855 | static const struct bpf_func_proto bpf_sk_fullsock_proto = { | |
1856 | .func = bpf_sk_fullsock, | |
1857 | .gpl_only = false, | |
1858 | .ret_type = RET_PTR_TO_SOCKET_OR_NULL, | |
1859 | .arg1_type = ARG_PTR_TO_SOCK_COMMON, | |
1860 | }; | |
1861 | ||
0ea488ff JF |
1862 | static inline int sk_skb_try_make_writable(struct sk_buff *skb, |
1863 | unsigned int write_len) | |
1864 | { | |
1865 | int err = __bpf_try_make_writable(skb, write_len); | |
1866 | ||
1867 | bpf_compute_data_end_sk_skb(skb); | |
1868 | return err; | |
1869 | } | |
1870 | ||
1871 | BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len) | |
1872 | { | |
1873 | /* Idea is the following: should the needed direct read/write | |
1874 | * test fail during runtime, we can pull in more data and redo | |
1875 | * again, since implicitly, we invalidate previous checks here. | |
1876 | * | |
1877 | * Or, since we know how much we need to make read/writeable, | |
1878 | * this can be done once at the program beginning for direct | |
1879 | * access case. By this we overcome limitations of only current | |
1880 | * headroom being accessible. | |
1881 | */ | |
1882 | return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb)); | |
1883 | } | |
1884 | ||
1885 | static const struct bpf_func_proto sk_skb_pull_data_proto = { | |
1886 | .func = sk_skb_pull_data, | |
1887 | .gpl_only = false, | |
1888 | .ret_type = RET_INTEGER, | |
1889 | .arg1_type = ARG_PTR_TO_CTX, | |
1890 | .arg2_type = ARG_ANYTHING, | |
1891 | }; | |
1892 | ||
f3694e00 DB |
1893 | BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset, |
1894 | u64, from, u64, to, u64, flags) | |
91bc4822 | 1895 | { |
0ed661d5 | 1896 | __sum16 *ptr; |
91bc4822 | 1897 | |
781c53bc DB |
1898 | if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK))) |
1899 | return -EINVAL; | |
0ed661d5 | 1900 | if (unlikely(offset > 0xffff || offset & 1)) |
91bc4822 | 1901 | return -EFAULT; |
0ed661d5 | 1902 | if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) |
91bc4822 AS |
1903 | return -EFAULT; |
1904 | ||
0ed661d5 | 1905 | ptr = (__sum16 *)(skb->data + offset); |
781c53bc | 1906 | switch (flags & BPF_F_HDR_FIELD_MASK) { |
8050c0f0 DB |
1907 | case 0: |
1908 | if (unlikely(from != 0)) | |
1909 | return -EINVAL; | |
1910 | ||
1911 | csum_replace_by_diff(ptr, to); | |
1912 | break; | |
91bc4822 AS |
1913 | case 2: |
1914 | csum_replace2(ptr, from, to); | |
1915 | break; | |
1916 | case 4: | |
1917 | csum_replace4(ptr, from, to); | |
1918 | break; | |
1919 | default: | |
1920 | return -EINVAL; | |
1921 | } | |
1922 | ||
91bc4822 AS |
1923 | return 0; |
1924 | } | |
1925 | ||
577c50aa | 1926 | static const struct bpf_func_proto bpf_l3_csum_replace_proto = { |
91bc4822 AS |
1927 | .func = bpf_l3_csum_replace, |
1928 | .gpl_only = false, | |
1929 | .ret_type = RET_INTEGER, | |
1930 | .arg1_type = ARG_PTR_TO_CTX, | |
1931 | .arg2_type = ARG_ANYTHING, | |
1932 | .arg3_type = ARG_ANYTHING, | |
1933 | .arg4_type = ARG_ANYTHING, | |
1934 | .arg5_type = ARG_ANYTHING, | |
1935 | }; | |
1936 | ||
f3694e00 DB |
1937 | BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset, |
1938 | u64, from, u64, to, u64, flags) | |
91bc4822 | 1939 | { |
781c53bc | 1940 | bool is_pseudo = flags & BPF_F_PSEUDO_HDR; |
2f72959a | 1941 | bool is_mmzero = flags & BPF_F_MARK_MANGLED_0; |
d1b662ad | 1942 | bool do_mforce = flags & BPF_F_MARK_ENFORCE; |
0ed661d5 | 1943 | __sum16 *ptr; |
91bc4822 | 1944 | |
d1b662ad DB |
1945 | if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE | |
1946 | BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK))) | |
781c53bc | 1947 | return -EINVAL; |
0ed661d5 | 1948 | if (unlikely(offset > 0xffff || offset & 1)) |
91bc4822 | 1949 | return -EFAULT; |
0ed661d5 | 1950 | if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr)))) |
91bc4822 AS |
1951 | return -EFAULT; |
1952 | ||
0ed661d5 | 1953 | ptr = (__sum16 *)(skb->data + offset); |
d1b662ad | 1954 | if (is_mmzero && !do_mforce && !*ptr) |
2f72959a | 1955 | return 0; |
91bc4822 | 1956 | |
781c53bc | 1957 | switch (flags & BPF_F_HDR_FIELD_MASK) { |
7d672345 DB |
1958 | case 0: |
1959 | if (unlikely(from != 0)) | |
1960 | return -EINVAL; | |
1961 | ||
1962 | inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo); | |
1963 | break; | |
91bc4822 AS |
1964 | case 2: |
1965 | inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); | |
1966 | break; | |
1967 | case 4: | |
1968 | inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); | |
1969 | break; | |
1970 | default: | |
1971 | return -EINVAL; | |
1972 | } | |
1973 | ||
2f72959a DB |
1974 | if (is_mmzero && !*ptr) |
1975 | *ptr = CSUM_MANGLED_0; | |
91bc4822 AS |
1976 | return 0; |
1977 | } | |
1978 | ||
577c50aa | 1979 | static const struct bpf_func_proto bpf_l4_csum_replace_proto = { |
91bc4822 AS |
1980 | .func = bpf_l4_csum_replace, |
1981 | .gpl_only = false, | |
1982 | .ret_type = RET_INTEGER, | |
1983 | .arg1_type = ARG_PTR_TO_CTX, | |
1984 | .arg2_type = ARG_ANYTHING, | |
1985 | .arg3_type = ARG_ANYTHING, | |
1986 | .arg4_type = ARG_ANYTHING, | |
1987 | .arg5_type = ARG_ANYTHING, | |
608cd71a AS |
1988 | }; |
1989 | ||
f3694e00 DB |
1990 | BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size, |
1991 | __be32 *, to, u32, to_size, __wsum, seed) | |
7d672345 | 1992 | { |
21cafc1d | 1993 | struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); |
f3694e00 | 1994 | u32 diff_size = from_size + to_size; |
7d672345 DB |
1995 | int i, j = 0; |
1996 | ||
1997 | /* This is quite flexible, some examples: | |
1998 | * | |
1999 | * from_size == 0, to_size > 0, seed := csum --> pushing data | |
2000 | * from_size > 0, to_size == 0, seed := csum --> pulling data | |
2001 | * from_size > 0, to_size > 0, seed := 0 --> diffing data | |
2002 | * | |
2003 | * Even for diffing, from_size and to_size don't need to be equal. | |
2004 | */ | |
2005 | if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) || | |
2006 | diff_size > sizeof(sp->diff))) | |
2007 | return -EINVAL; | |
2008 | ||
2009 | for (i = 0; i < from_size / sizeof(__be32); i++, j++) | |
2010 | sp->diff[j] = ~from[i]; | |
2011 | for (i = 0; i < to_size / sizeof(__be32); i++, j++) | |
2012 | sp->diff[j] = to[i]; | |
2013 | ||
2014 | return csum_partial(sp->diff, diff_size, seed); | |
2015 | } | |
2016 | ||
577c50aa | 2017 | static const struct bpf_func_proto bpf_csum_diff_proto = { |
7d672345 DB |
2018 | .func = bpf_csum_diff, |
2019 | .gpl_only = false, | |
36bbef52 | 2020 | .pkt_access = true, |
7d672345 | 2021 | .ret_type = RET_INTEGER, |
db1ac496 | 2022 | .arg1_type = ARG_PTR_TO_MEM_OR_NULL, |
39f19ebb | 2023 | .arg2_type = ARG_CONST_SIZE_OR_ZERO, |
db1ac496 | 2024 | .arg3_type = ARG_PTR_TO_MEM_OR_NULL, |
39f19ebb | 2025 | .arg4_type = ARG_CONST_SIZE_OR_ZERO, |
7d672345 DB |
2026 | .arg5_type = ARG_ANYTHING, |
2027 | }; | |
2028 | ||
36bbef52 DB |
2029 | BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum) |
2030 | { | |
2031 | /* The interface is to be used in combination with bpf_csum_diff() | |
2032 | * for direct packet writes. csum rotation for alignment as well | |
2033 | * as emulating csum_sub() can be done from the eBPF program. | |
2034 | */ | |
2035 | if (skb->ip_summed == CHECKSUM_COMPLETE) | |
2036 | return (skb->csum = csum_add(skb->csum, csum)); | |
2037 | ||
2038 | return -ENOTSUPP; | |
2039 | } | |
2040 | ||
2041 | static const struct bpf_func_proto bpf_csum_update_proto = { | |
2042 | .func = bpf_csum_update, | |
2043 | .gpl_only = false, | |
2044 | .ret_type = RET_INTEGER, | |
2045 | .arg1_type = ARG_PTR_TO_CTX, | |
2046 | .arg2_type = ARG_ANYTHING, | |
2047 | }; | |
2048 | ||
7cdec54f DB |
2049 | BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level) |
2050 | { | |
2051 | /* The interface is to be used in combination with bpf_skb_adjust_room() | |
2052 | * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET | |
2053 | * is passed as flags, for example. | |
2054 | */ | |
2055 | switch (level) { | |
2056 | case BPF_CSUM_LEVEL_INC: | |
2057 | __skb_incr_checksum_unnecessary(skb); | |
2058 | break; | |
2059 | case BPF_CSUM_LEVEL_DEC: | |
2060 | __skb_decr_checksum_unnecessary(skb); | |
2061 | break; | |
2062 | case BPF_CSUM_LEVEL_RESET: | |
2063 | __skb_reset_checksum_unnecessary(skb); | |
2064 | break; | |
2065 | case BPF_CSUM_LEVEL_QUERY: | |
2066 | return skb->ip_summed == CHECKSUM_UNNECESSARY ? | |
2067 | skb->csum_level : -EACCES; | |
2068 | default: | |
2069 | return -EINVAL; | |
2070 | } | |
2071 | ||
2072 | return 0; | |
2073 | } | |
2074 | ||
2075 | static const struct bpf_func_proto bpf_csum_level_proto = { | |
2076 | .func = bpf_csum_level, | |
2077 | .gpl_only = false, | |
2078 | .ret_type = RET_INTEGER, | |
2079 | .arg1_type = ARG_PTR_TO_CTX, | |
2080 | .arg2_type = ARG_ANYTHING, | |
2081 | }; | |
2082 | ||
a70b506e DB |
2083 | static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb) |
2084 | { | |
a70b506e DB |
2085 | return dev_forward_skb(dev, skb); |
2086 | } | |
2087 | ||
4e3264d2 MKL |
2088 | static inline int __bpf_rx_skb_no_mac(struct net_device *dev, |
2089 | struct sk_buff *skb) | |
2090 | { | |
2091 | int ret = ____dev_forward_skb(dev, skb); | |
2092 | ||
2093 | if (likely(!ret)) { | |
2094 | skb->dev = dev; | |
2095 | ret = netif_rx(skb); | |
2096 | } | |
2097 | ||
2098 | return ret; | |
2099 | } | |
2100 | ||
a70b506e DB |
2101 | static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb) |
2102 | { | |
2103 | int ret; | |
2104 | ||
97cdcf37 | 2105 | if (dev_xmit_recursion()) { |
a70b506e DB |
2106 | net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); |
2107 | kfree_skb(skb); | |
2108 | return -ENETDOWN; | |
2109 | } | |
2110 | ||
2111 | skb->dev = dev; | |
5133498f | 2112 | skb->tstamp = 0; |
a70b506e | 2113 | |
97cdcf37 | 2114 | dev_xmit_recursion_inc(); |
a70b506e | 2115 | ret = dev_queue_xmit(skb); |
97cdcf37 | 2116 | dev_xmit_recursion_dec(); |
a70b506e DB |
2117 | |
2118 | return ret; | |
2119 | } | |
2120 | ||
4e3264d2 MKL |
2121 | static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev, |
2122 | u32 flags) | |
2123 | { | |
e7c87bd6 | 2124 | unsigned int mlen = skb_network_offset(skb); |
4e3264d2 | 2125 | |
e7c87bd6 WB |
2126 | if (mlen) { |
2127 | __skb_pull(skb, mlen); | |
4e3264d2 | 2128 | |
e7c87bd6 WB |
2129 | /* At ingress, the mac header has already been pulled once. |
2130 | * At egress, skb_pospull_rcsum has to be done in case that | |
2131 | * the skb is originated from ingress (i.e. a forwarded skb) | |
2132 | * to ensure that rcsum starts at net header. | |
2133 | */ | |
2134 | if (!skb_at_tc_ingress(skb)) | |
2135 | skb_postpull_rcsum(skb, skb_mac_header(skb), mlen); | |
2136 | } | |
4e3264d2 MKL |
2137 | skb_pop_mac_header(skb); |
2138 | skb_reset_mac_len(skb); | |
2139 | return flags & BPF_F_INGRESS ? | |
2140 | __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb); | |
2141 | } | |
2142 | ||
2143 | static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev, | |
2144 | u32 flags) | |
2145 | { | |
3a0af8fd TG |
2146 | /* Verify that a link layer header is carried */ |
2147 | if (unlikely(skb->mac_header >= skb->network_header)) { | |
2148 | kfree_skb(skb); | |
2149 | return -ERANGE; | |
2150 | } | |
2151 | ||
4e3264d2 MKL |
2152 | bpf_push_mac_rcsum(skb); |
2153 | return flags & BPF_F_INGRESS ? | |
2154 | __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb); | |
2155 | } | |
2156 | ||
2157 | static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev, | |
2158 | u32 flags) | |
2159 | { | |
c491680f | 2160 | if (dev_is_mac_header_xmit(dev)) |
4e3264d2 | 2161 | return __bpf_redirect_common(skb, dev, flags); |
c491680f DB |
2162 | else |
2163 | return __bpf_redirect_no_mac(skb, dev, flags); | |
4e3264d2 MKL |
2164 | } |
2165 | ||
b4ab3141 DB |
2166 | #if IS_ENABLED(CONFIG_IPV6) |
2167 | static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb) | |
2168 | { | |
2169 | struct dst_entry *dst = skb_dst(skb); | |
2170 | struct net_device *dev = dst->dev; | |
2171 | u32 hh_len = LL_RESERVED_SPACE(dev); | |
2172 | const struct in6_addr *nexthop; | |
2173 | struct neighbour *neigh; | |
2174 | ||
2175 | if (dev_xmit_recursion()) { | |
2176 | net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); | |
2177 | goto out_drop; | |
2178 | } | |
2179 | ||
2180 | skb->dev = dev; | |
2181 | skb->tstamp = 0; | |
2182 | ||
2183 | if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { | |
2184 | struct sk_buff *skb2; | |
2185 | ||
2186 | skb2 = skb_realloc_headroom(skb, hh_len); | |
2187 | if (unlikely(!skb2)) { | |
2188 | kfree_skb(skb); | |
2189 | return -ENOMEM; | |
2190 | } | |
2191 | if (skb->sk) | |
2192 | skb_set_owner_w(skb2, skb->sk); | |
2193 | consume_skb(skb); | |
2194 | skb = skb2; | |
2195 | } | |
2196 | ||
2197 | rcu_read_lock_bh(); | |
2198 | nexthop = rt6_nexthop(container_of(dst, struct rt6_info, dst), | |
2199 | &ipv6_hdr(skb)->daddr); | |
2200 | neigh = ip_neigh_gw6(dev, nexthop); | |
2201 | if (likely(!IS_ERR(neigh))) { | |
2202 | int ret; | |
2203 | ||
2204 | sock_confirm_neigh(skb, neigh); | |
2205 | dev_xmit_recursion_inc(); | |
2206 | ret = neigh_output(neigh, skb, false); | |
2207 | dev_xmit_recursion_dec(); | |
2208 | rcu_read_unlock_bh(); | |
2209 | return ret; | |
2210 | } | |
2211 | rcu_read_unlock_bh(); | |
2212 | IP6_INC_STATS(dev_net(dst->dev), | |
2213 | ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); | |
2214 | out_drop: | |
2215 | kfree_skb(skb); | |
2216 | return -ENETDOWN; | |
2217 | } | |
2218 | ||
2219 | static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev) | |
2220 | { | |
2221 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); | |
2222 | struct net *net = dev_net(dev); | |
2223 | int err, ret = NET_XMIT_DROP; | |
2224 | struct dst_entry *dst; | |
2225 | struct flowi6 fl6 = { | |
2226 | .flowi6_flags = FLOWI_FLAG_ANYSRC, | |
2227 | .flowi6_mark = skb->mark, | |
2228 | .flowlabel = ip6_flowinfo(ip6h), | |
2229 | .flowi6_oif = dev->ifindex, | |
2230 | .flowi6_proto = ip6h->nexthdr, | |
2231 | .daddr = ip6h->daddr, | |
2232 | .saddr = ip6h->saddr, | |
2233 | }; | |
2234 | ||
2235 | dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL); | |
2236 | if (IS_ERR(dst)) | |
2237 | goto out_drop; | |
2238 | ||
2239 | skb_dst_set(skb, dst); | |
2240 | ||
2241 | err = bpf_out_neigh_v6(net, skb); | |
2242 | if (unlikely(net_xmit_eval(err))) | |
2243 | dev->stats.tx_errors++; | |
2244 | else | |
2245 | ret = NET_XMIT_SUCCESS; | |
2246 | goto out_xmit; | |
2247 | out_drop: | |
2248 | dev->stats.tx_errors++; | |
2249 | kfree_skb(skb); | |
2250 | out_xmit: | |
2251 | return ret; | |
2252 | } | |
2253 | #else | |
2254 | static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev) | |
2255 | { | |
2256 | kfree_skb(skb); | |
2257 | return NET_XMIT_DROP; | |
2258 | } | |
2259 | #endif /* CONFIG_IPV6 */ | |
2260 | ||
2261 | #if IS_ENABLED(CONFIG_INET) | |
2262 | static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb) | |
2263 | { | |
2264 | struct dst_entry *dst = skb_dst(skb); | |
2265 | struct rtable *rt = container_of(dst, struct rtable, dst); | |
2266 | struct net_device *dev = dst->dev; | |
2267 | u32 hh_len = LL_RESERVED_SPACE(dev); | |
2268 | struct neighbour *neigh; | |
2269 | bool is_v6gw = false; | |
2270 | ||
2271 | if (dev_xmit_recursion()) { | |
2272 | net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); | |
2273 | goto out_drop; | |
2274 | } | |
2275 | ||
2276 | skb->dev = dev; | |
2277 | skb->tstamp = 0; | |
2278 | ||
2279 | if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { | |
2280 | struct sk_buff *skb2; | |
2281 | ||
2282 | skb2 = skb_realloc_headroom(skb, hh_len); | |
2283 | if (unlikely(!skb2)) { | |
2284 | kfree_skb(skb); | |
2285 | return -ENOMEM; | |
2286 | } | |
2287 | if (skb->sk) | |
2288 | skb_set_owner_w(skb2, skb->sk); | |
2289 | consume_skb(skb); | |
2290 | skb = skb2; | |
2291 | } | |
2292 | ||
2293 | rcu_read_lock_bh(); | |
2294 | neigh = ip_neigh_for_gw(rt, skb, &is_v6gw); | |
2295 | if (likely(!IS_ERR(neigh))) { | |
2296 | int ret; | |
2297 | ||
2298 | sock_confirm_neigh(skb, neigh); | |
2299 | dev_xmit_recursion_inc(); | |
2300 | ret = neigh_output(neigh, skb, is_v6gw); | |
2301 | dev_xmit_recursion_dec(); | |
2302 | rcu_read_unlock_bh(); | |
2303 | return ret; | |
2304 | } | |
2305 | rcu_read_unlock_bh(); | |
2306 | out_drop: | |
2307 | kfree_skb(skb); | |
2308 | return -ENETDOWN; | |
2309 | } | |
2310 | ||
2311 | static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev) | |
2312 | { | |
2313 | const struct iphdr *ip4h = ip_hdr(skb); | |
2314 | struct net *net = dev_net(dev); | |
2315 | int err, ret = NET_XMIT_DROP; | |
2316 | struct rtable *rt; | |
2317 | struct flowi4 fl4 = { | |
2318 | .flowi4_flags = FLOWI_FLAG_ANYSRC, | |
2319 | .flowi4_mark = skb->mark, | |
2320 | .flowi4_tos = RT_TOS(ip4h->tos), | |
2321 | .flowi4_oif = dev->ifindex, | |
2322 | .flowi4_proto = ip4h->protocol, | |
2323 | .daddr = ip4h->daddr, | |
2324 | .saddr = ip4h->saddr, | |
2325 | }; | |
2326 | ||
2327 | rt = ip_route_output_flow(net, &fl4, NULL); | |
2328 | if (IS_ERR(rt)) | |
2329 | goto out_drop; | |
2330 | if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) { | |
2331 | ip_rt_put(rt); | |
2332 | goto out_drop; | |
2333 | } | |
2334 | ||
2335 | skb_dst_set(skb, &rt->dst); | |
2336 | ||
2337 | err = bpf_out_neigh_v4(net, skb); | |
2338 | if (unlikely(net_xmit_eval(err))) | |
2339 | dev->stats.tx_errors++; | |
2340 | else | |
2341 | ret = NET_XMIT_SUCCESS; | |
2342 | goto out_xmit; | |
2343 | out_drop: | |
2344 | dev->stats.tx_errors++; | |
2345 | kfree_skb(skb); | |
2346 | out_xmit: | |
2347 | return ret; | |
2348 | } | |
2349 | #else | |
2350 | static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev) | |
2351 | { | |
2352 | kfree_skb(skb); | |
2353 | return NET_XMIT_DROP; | |
2354 | } | |
2355 | #endif /* CONFIG_INET */ | |
2356 | ||
2357 | static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev) | |
2358 | { | |
2359 | struct ethhdr *ethh = eth_hdr(skb); | |
2360 | ||
2361 | if (unlikely(skb->mac_header >= skb->network_header)) | |
2362 | goto out; | |
2363 | bpf_push_mac_rcsum(skb); | |
2364 | if (is_multicast_ether_addr(ethh->h_dest)) | |
2365 | goto out; | |
2366 | ||
2367 | skb_pull(skb, sizeof(*ethh)); | |
2368 | skb_unset_mac_header(skb); | |
2369 | skb_reset_network_header(skb); | |
2370 | ||
2371 | if (skb->protocol == htons(ETH_P_IP)) | |
2372 | return __bpf_redirect_neigh_v4(skb, dev); | |
2373 | else if (skb->protocol == htons(ETH_P_IPV6)) | |
2374 | return __bpf_redirect_neigh_v6(skb, dev); | |
2375 | out: | |
2376 | kfree_skb(skb); | |
2377 | return -ENOTSUPP; | |
2378 | } | |
2379 | ||
2380 | /* Internal, non-exposed redirect flags. */ | |
2381 | enum { | |
2382 | BPF_F_NEIGH = (1ULL << 1), | |
2383 | #define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH) | |
2384 | }; | |
2385 | ||
f3694e00 | 2386 | BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags) |
3896d655 | 2387 | { |
3896d655 | 2388 | struct net_device *dev; |
36bbef52 DB |
2389 | struct sk_buff *clone; |
2390 | int ret; | |
3896d655 | 2391 | |
b4ab3141 | 2392 | if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL))) |
781c53bc DB |
2393 | return -EINVAL; |
2394 | ||
3896d655 AS |
2395 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex); |
2396 | if (unlikely(!dev)) | |
2397 | return -EINVAL; | |
2398 | ||
36bbef52 DB |
2399 | clone = skb_clone(skb, GFP_ATOMIC); |
2400 | if (unlikely(!clone)) | |
3896d655 AS |
2401 | return -ENOMEM; |
2402 | ||
36bbef52 DB |
2403 | /* For direct write, we need to keep the invariant that the skbs |
2404 | * we're dealing with need to be uncloned. Should uncloning fail | |
2405 | * here, we need to free the just generated clone to unclone once | |
2406 | * again. | |
2407 | */ | |
2408 | ret = bpf_try_make_head_writable(skb); | |
2409 | if (unlikely(ret)) { | |
2410 | kfree_skb(clone); | |
2411 | return -ENOMEM; | |
2412 | } | |
2413 | ||
4e3264d2 | 2414 | return __bpf_redirect(clone, dev, flags); |
3896d655 AS |
2415 | } |
2416 | ||
577c50aa | 2417 | static const struct bpf_func_proto bpf_clone_redirect_proto = { |
3896d655 AS |
2418 | .func = bpf_clone_redirect, |
2419 | .gpl_only = false, | |
2420 | .ret_type = RET_INTEGER, | |
2421 | .arg1_type = ARG_PTR_TO_CTX, | |
2422 | .arg2_type = ARG_ANYTHING, | |
2423 | .arg3_type = ARG_ANYTHING, | |
2424 | }; | |
2425 | ||
0b19cc0a TM |
2426 | DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info); |
2427 | EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info); | |
781c53bc | 2428 | |
27b29f63 AS |
2429 | int skb_do_redirect(struct sk_buff *skb) |
2430 | { | |
0b19cc0a | 2431 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); |
27b29f63 | 2432 | struct net_device *dev; |
b4ab3141 | 2433 | u32 flags = ri->flags; |
27b29f63 | 2434 | |
4b55cf29 THJ |
2435 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->tgt_index); |
2436 | ri->tgt_index = 0; | |
27b29f63 AS |
2437 | if (unlikely(!dev)) { |
2438 | kfree_skb(skb); | |
2439 | return -EINVAL; | |
2440 | } | |
2441 | ||
b4ab3141 DB |
2442 | return flags & BPF_F_NEIGH ? |
2443 | __bpf_redirect_neigh(skb, dev) : | |
2444 | __bpf_redirect(skb, dev, flags); | |
2445 | } | |
2446 | ||
2447 | BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags) | |
2448 | { | |
2449 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); | |
2450 | ||
2451 | if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL))) | |
2452 | return TC_ACT_SHOT; | |
2453 | ||
2454 | ri->flags = flags; | |
2455 | ri->tgt_index = ifindex; | |
2456 | ||
2457 | return TC_ACT_REDIRECT; | |
27b29f63 AS |
2458 | } |
2459 | ||
577c50aa | 2460 | static const struct bpf_func_proto bpf_redirect_proto = { |
27b29f63 AS |
2461 | .func = bpf_redirect, |
2462 | .gpl_only = false, | |
2463 | .ret_type = RET_INTEGER, | |
2464 | .arg1_type = ARG_ANYTHING, | |
2465 | .arg2_type = ARG_ANYTHING, | |
2466 | }; | |
2467 | ||
b4ab3141 DB |
2468 | BPF_CALL_2(bpf_redirect_neigh, u32, ifindex, u64, flags) |
2469 | { | |
2470 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); | |
2471 | ||
2472 | if (unlikely(flags)) | |
2473 | return TC_ACT_SHOT; | |
2474 | ||
2475 | ri->flags = BPF_F_NEIGH; | |
2476 | ri->tgt_index = ifindex; | |
2477 | ||
2478 | return TC_ACT_REDIRECT; | |
2479 | } | |
2480 | ||
2481 | static const struct bpf_func_proto bpf_redirect_neigh_proto = { | |
2482 | .func = bpf_redirect_neigh, | |
2483 | .gpl_only = false, | |
2484 | .ret_type = RET_INTEGER, | |
2485 | .arg1_type = ARG_ANYTHING, | |
2486 | .arg2_type = ARG_ANYTHING, | |
2487 | }; | |
2488 | ||
604326b4 | 2489 | BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes) |
2a100317 JF |
2490 | { |
2491 | msg->apply_bytes = bytes; | |
2492 | return 0; | |
2493 | } | |
2494 | ||
2495 | static const struct bpf_func_proto bpf_msg_apply_bytes_proto = { | |
2496 | .func = bpf_msg_apply_bytes, | |
2497 | .gpl_only = false, | |
2498 | .ret_type = RET_INTEGER, | |
2499 | .arg1_type = ARG_PTR_TO_CTX, | |
2500 | .arg2_type = ARG_ANYTHING, | |
2501 | }; | |
2502 | ||
604326b4 | 2503 | BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes) |
91843d54 JF |
2504 | { |
2505 | msg->cork_bytes = bytes; | |
2506 | return 0; | |
2507 | } | |
2508 | ||
2509 | static const struct bpf_func_proto bpf_msg_cork_bytes_proto = { | |
2510 | .func = bpf_msg_cork_bytes, | |
2511 | .gpl_only = false, | |
2512 | .ret_type = RET_INTEGER, | |
2513 | .arg1_type = ARG_PTR_TO_CTX, | |
2514 | .arg2_type = ARG_ANYTHING, | |
2515 | }; | |
2516 | ||
604326b4 DB |
2517 | BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start, |
2518 | u32, end, u64, flags) | |
015632bb | 2519 | { |
604326b4 DB |
2520 | u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start; |
2521 | u32 first_sge, last_sge, i, shift, bytes_sg_total; | |
2522 | struct scatterlist *sge; | |
2523 | u8 *raw, *to, *from; | |
015632bb JF |
2524 | struct page *page; |
2525 | ||
2526 | if (unlikely(flags || end <= start)) | |
2527 | return -EINVAL; | |
2528 | ||
2529 | /* First find the starting scatterlist element */ | |
604326b4 | 2530 | i = msg->sg.start; |
015632bb | 2531 | do { |
6562e29c | 2532 | offset += len; |
604326b4 | 2533 | len = sk_msg_elem(msg, i)->length; |
015632bb JF |
2534 | if (start < offset + len) |
2535 | break; | |
604326b4 DB |
2536 | sk_msg_iter_var_next(i); |
2537 | } while (i != msg->sg.end); | |
015632bb JF |
2538 | |
2539 | if (unlikely(start >= offset + len)) | |
2540 | return -EINVAL; | |
2541 | ||
604326b4 | 2542 | first_sge = i; |
5b24109b DB |
2543 | /* The start may point into the sg element so we need to also |
2544 | * account for the headroom. | |
2545 | */ | |
2546 | bytes_sg_total = start - offset + bytes; | |
163ab96b | 2547 | if (!test_bit(i, &msg->sg.copy) && bytes_sg_total <= len) |
015632bb | 2548 | goto out; |
015632bb JF |
2549 | |
2550 | /* At this point we need to linearize multiple scatterlist | |
2551 | * elements or a single shared page. Either way we need to | |
2552 | * copy into a linear buffer exclusively owned by BPF. Then | |
2553 | * place the buffer in the scatterlist and fixup the original | |
2554 | * entries by removing the entries now in the linear buffer | |
2555 | * and shifting the remaining entries. For now we do not try | |
2556 | * to copy partial entries to avoid complexity of running out | |
2557 | * of sg_entry slots. The downside is reading a single byte | |
2558 | * will copy the entire sg entry. | |
2559 | */ | |
2560 | do { | |
604326b4 DB |
2561 | copy += sk_msg_elem(msg, i)->length; |
2562 | sk_msg_iter_var_next(i); | |
5b24109b | 2563 | if (bytes_sg_total <= copy) |
015632bb | 2564 | break; |
604326b4 DB |
2565 | } while (i != msg->sg.end); |
2566 | last_sge = i; | |
015632bb | 2567 | |
5b24109b | 2568 | if (unlikely(bytes_sg_total > copy)) |
015632bb JF |
2569 | return -EINVAL; |
2570 | ||
4c3d795c TD |
2571 | page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP, |
2572 | get_order(copy)); | |
015632bb JF |
2573 | if (unlikely(!page)) |
2574 | return -ENOMEM; | |
015632bb | 2575 | |
604326b4 DB |
2576 | raw = page_address(page); |
2577 | i = first_sge; | |
015632bb | 2578 | do { |
604326b4 DB |
2579 | sge = sk_msg_elem(msg, i); |
2580 | from = sg_virt(sge); | |
2581 | len = sge->length; | |
2582 | to = raw + poffset; | |
015632bb JF |
2583 | |
2584 | memcpy(to, from, len); | |
9db39f4d | 2585 | poffset += len; |
604326b4 DB |
2586 | sge->length = 0; |
2587 | put_page(sg_page(sge)); | |
015632bb | 2588 | |
604326b4 DB |
2589 | sk_msg_iter_var_next(i); |
2590 | } while (i != last_sge); | |
015632bb | 2591 | |
604326b4 | 2592 | sg_set_page(&msg->sg.data[first_sge], page, copy, 0); |
015632bb JF |
2593 | |
2594 | /* To repair sg ring we need to shift entries. If we only | |
2595 | * had a single entry though we can just replace it and | |
2596 | * be done. Otherwise walk the ring and shift the entries. | |
2597 | */ | |
604326b4 DB |
2598 | WARN_ON_ONCE(last_sge == first_sge); |
2599 | shift = last_sge > first_sge ? | |
2600 | last_sge - first_sge - 1 : | |
031097d9 | 2601 | NR_MSG_FRAG_IDS - first_sge + last_sge - 1; |
015632bb JF |
2602 | if (!shift) |
2603 | goto out; | |
2604 | ||
604326b4 DB |
2605 | i = first_sge; |
2606 | sk_msg_iter_var_next(i); | |
015632bb | 2607 | do { |
604326b4 | 2608 | u32 move_from; |
015632bb | 2609 | |
031097d9 JK |
2610 | if (i + shift >= NR_MSG_FRAG_IDS) |
2611 | move_from = i + shift - NR_MSG_FRAG_IDS; | |
015632bb JF |
2612 | else |
2613 | move_from = i + shift; | |
604326b4 | 2614 | if (move_from == msg->sg.end) |
015632bb JF |
2615 | break; |
2616 | ||
604326b4 DB |
2617 | msg->sg.data[i] = msg->sg.data[move_from]; |
2618 | msg->sg.data[move_from].length = 0; | |
2619 | msg->sg.data[move_from].page_link = 0; | |
2620 | msg->sg.data[move_from].offset = 0; | |
2621 | sk_msg_iter_var_next(i); | |
015632bb | 2622 | } while (1); |
604326b4 DB |
2623 | |
2624 | msg->sg.end = msg->sg.end - shift > msg->sg.end ? | |
031097d9 | 2625 | msg->sg.end - shift + NR_MSG_FRAG_IDS : |
604326b4 | 2626 | msg->sg.end - shift; |
015632bb | 2627 | out: |
604326b4 | 2628 | msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset; |
015632bb | 2629 | msg->data_end = msg->data + bytes; |
015632bb JF |
2630 | return 0; |
2631 | } | |
2632 | ||
2633 | static const struct bpf_func_proto bpf_msg_pull_data_proto = { | |
2634 | .func = bpf_msg_pull_data, | |
2635 | .gpl_only = false, | |
2636 | .ret_type = RET_INTEGER, | |
2637 | .arg1_type = ARG_PTR_TO_CTX, | |
2638 | .arg2_type = ARG_ANYTHING, | |
2639 | .arg3_type = ARG_ANYTHING, | |
2640 | .arg4_type = ARG_ANYTHING, | |
2641 | }; | |
2642 | ||
6fff607e JF |
2643 | BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start, |
2644 | u32, len, u64, flags) | |
2645 | { | |
2646 | struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge; | |
6562e29c | 2647 | u32 new, i = 0, l = 0, space, copy = 0, offset = 0; |
6fff607e JF |
2648 | u8 *raw, *to, *from; |
2649 | struct page *page; | |
2650 | ||
2651 | if (unlikely(flags)) | |
2652 | return -EINVAL; | |
2653 | ||
2654 | /* First find the starting scatterlist element */ | |
2655 | i = msg->sg.start; | |
2656 | do { | |
6562e29c | 2657 | offset += l; |
6fff607e JF |
2658 | l = sk_msg_elem(msg, i)->length; |
2659 | ||
2660 | if (start < offset + l) | |
2661 | break; | |
6fff607e JF |
2662 | sk_msg_iter_var_next(i); |
2663 | } while (i != msg->sg.end); | |
2664 | ||
2665 | if (start >= offset + l) | |
2666 | return -EINVAL; | |
2667 | ||
2668 | space = MAX_MSG_FRAGS - sk_msg_elem_used(msg); | |
2669 | ||
2670 | /* If no space available will fallback to copy, we need at | |
2671 | * least one scatterlist elem available to push data into | |
2672 | * when start aligns to the beginning of an element or two | |
2673 | * when it falls inside an element. We handle the start equals | |
2674 | * offset case because its the common case for inserting a | |
2675 | * header. | |
2676 | */ | |
2677 | if (!space || (space == 1 && start != offset)) | |
2678 | copy = msg->sg.data[i].length; | |
2679 | ||
2680 | page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP, | |
2681 | get_order(copy + len)); | |
2682 | if (unlikely(!page)) | |
2683 | return -ENOMEM; | |
2684 | ||
2685 | if (copy) { | |
2686 | int front, back; | |
2687 | ||
2688 | raw = page_address(page); | |
2689 | ||
2690 | psge = sk_msg_elem(msg, i); | |
2691 | front = start - offset; | |
2692 | back = psge->length - front; | |
2693 | from = sg_virt(psge); | |
2694 | ||
2695 | if (front) | |
2696 | memcpy(raw, from, front); | |
2697 | ||
2698 | if (back) { | |
2699 | from += front; | |
2700 | to = raw + front + len; | |
2701 | ||
2702 | memcpy(to, from, back); | |
2703 | } | |
2704 | ||
2705 | put_page(sg_page(psge)); | |
2706 | } else if (start - offset) { | |
2707 | psge = sk_msg_elem(msg, i); | |
2708 | rsge = sk_msg_elem_cpy(msg, i); | |
2709 | ||
2710 | psge->length = start - offset; | |
2711 | rsge.length -= psge->length; | |
2712 | rsge.offset += start; | |
2713 | ||
2714 | sk_msg_iter_var_next(i); | |
2715 | sg_unmark_end(psge); | |
cf21e9ba | 2716 | sg_unmark_end(&rsge); |
6fff607e JF |
2717 | sk_msg_iter_next(msg, end); |
2718 | } | |
2719 | ||
2720 | /* Slot(s) to place newly allocated data */ | |
2721 | new = i; | |
2722 | ||
2723 | /* Shift one or two slots as needed */ | |
2724 | if (!copy) { | |
2725 | sge = sk_msg_elem_cpy(msg, i); | |
2726 | ||
2727 | sk_msg_iter_var_next(i); | |
2728 | sg_unmark_end(&sge); | |
2729 | sk_msg_iter_next(msg, end); | |
2730 | ||
2731 | nsge = sk_msg_elem_cpy(msg, i); | |
2732 | if (rsge.length) { | |
2733 | sk_msg_iter_var_next(i); | |
2734 | nnsge = sk_msg_elem_cpy(msg, i); | |
2735 | } | |
2736 | ||
2737 | while (i != msg->sg.end) { | |
2738 | msg->sg.data[i] = sge; | |
2739 | sge = nsge; | |
2740 | sk_msg_iter_var_next(i); | |
2741 | if (rsge.length) { | |
2742 | nsge = nnsge; | |
2743 | nnsge = sk_msg_elem_cpy(msg, i); | |
2744 | } else { | |
2745 | nsge = sk_msg_elem_cpy(msg, i); | |
2746 | } | |
2747 | } | |
2748 | } | |
2749 | ||
2750 | /* Place newly allocated data buffer */ | |
2751 | sk_mem_charge(msg->sk, len); | |
2752 | msg->sg.size += len; | |
163ab96b | 2753 | __clear_bit(new, &msg->sg.copy); |
6fff607e JF |
2754 | sg_set_page(&msg->sg.data[new], page, len + copy, 0); |
2755 | if (rsge.length) { | |
2756 | get_page(sg_page(&rsge)); | |
2757 | sk_msg_iter_var_next(new); | |
2758 | msg->sg.data[new] = rsge; | |
2759 | } | |
2760 | ||
2761 | sk_msg_compute_data_pointers(msg); | |
2762 | return 0; | |
2763 | } | |
2764 | ||
2765 | static const struct bpf_func_proto bpf_msg_push_data_proto = { | |
2766 | .func = bpf_msg_push_data, | |
2767 | .gpl_only = false, | |
2768 | .ret_type = RET_INTEGER, | |
2769 | .arg1_type = ARG_PTR_TO_CTX, | |
2770 | .arg2_type = ARG_ANYTHING, | |
2771 | .arg3_type = ARG_ANYTHING, | |
2772 | .arg4_type = ARG_ANYTHING, | |
2773 | }; | |
2774 | ||
7246d8ed JF |
2775 | static void sk_msg_shift_left(struct sk_msg *msg, int i) |
2776 | { | |
2777 | int prev; | |
2778 | ||
2779 | do { | |
2780 | prev = i; | |
2781 | sk_msg_iter_var_next(i); | |
2782 | msg->sg.data[prev] = msg->sg.data[i]; | |
2783 | } while (i != msg->sg.end); | |
2784 | ||
2785 | sk_msg_iter_prev(msg, end); | |
2786 | } | |
2787 | ||
2788 | static void sk_msg_shift_right(struct sk_msg *msg, int i) | |
2789 | { | |
2790 | struct scatterlist tmp, sge; | |
2791 | ||
2792 | sk_msg_iter_next(msg, end); | |
2793 | sge = sk_msg_elem_cpy(msg, i); | |
2794 | sk_msg_iter_var_next(i); | |
2795 | tmp = sk_msg_elem_cpy(msg, i); | |
2796 | ||
2797 | while (i != msg->sg.end) { | |
2798 | msg->sg.data[i] = sge; | |
2799 | sk_msg_iter_var_next(i); | |
2800 | sge = tmp; | |
2801 | tmp = sk_msg_elem_cpy(msg, i); | |
2802 | } | |
2803 | } | |
2804 | ||
2805 | BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start, | |
2806 | u32, len, u64, flags) | |
2807 | { | |
6562e29c | 2808 | u32 i = 0, l = 0, space, offset = 0; |
7246d8ed JF |
2809 | u64 last = start + len; |
2810 | int pop; | |
2811 | ||
2812 | if (unlikely(flags)) | |
2813 | return -EINVAL; | |
2814 | ||
2815 | /* First find the starting scatterlist element */ | |
2816 | i = msg->sg.start; | |
2817 | do { | |
6562e29c | 2818 | offset += l; |
7246d8ed JF |
2819 | l = sk_msg_elem(msg, i)->length; |
2820 | ||
2821 | if (start < offset + l) | |
2822 | break; | |
7246d8ed JF |
2823 | sk_msg_iter_var_next(i); |
2824 | } while (i != msg->sg.end); | |
2825 | ||
2826 | /* Bounds checks: start and pop must be inside message */ | |
2827 | if (start >= offset + l || last >= msg->sg.size) | |
2828 | return -EINVAL; | |
2829 | ||
2830 | space = MAX_MSG_FRAGS - sk_msg_elem_used(msg); | |
2831 | ||
2832 | pop = len; | |
2833 | /* --------------| offset | |
2834 | * -| start |-------- len -------| | |
2835 | * | |
2836 | * |----- a ----|-------- pop -------|----- b ----| | |
2837 | * |______________________________________________| length | |
2838 | * | |
2839 | * | |
2840 | * a: region at front of scatter element to save | |
2841 | * b: region at back of scatter element to save when length > A + pop | |
2842 | * pop: region to pop from element, same as input 'pop' here will be | |
2843 | * decremented below per iteration. | |
2844 | * | |
2845 | * Two top-level cases to handle when start != offset, first B is non | |
2846 | * zero and second B is zero corresponding to when a pop includes more | |
2847 | * than one element. | |
2848 | * | |
2849 | * Then if B is non-zero AND there is no space allocate space and | |
2850 | * compact A, B regions into page. If there is space shift ring to | |
2851 | * the rigth free'ing the next element in ring to place B, leaving | |
2852 | * A untouched except to reduce length. | |
2853 | */ | |
2854 | if (start != offset) { | |
2855 | struct scatterlist *nsge, *sge = sk_msg_elem(msg, i); | |
2856 | int a = start; | |
2857 | int b = sge->length - pop - a; | |
2858 | ||
2859 | sk_msg_iter_var_next(i); | |
2860 | ||
2861 | if (pop < sge->length - a) { | |
2862 | if (space) { | |
2863 | sge->length = a; | |
2864 | sk_msg_shift_right(msg, i); | |
2865 | nsge = sk_msg_elem(msg, i); | |
2866 | get_page(sg_page(sge)); | |
2867 | sg_set_page(nsge, | |
2868 | sg_page(sge), | |
2869 | b, sge->offset + pop + a); | |
2870 | } else { | |
2871 | struct page *page, *orig; | |
2872 | u8 *to, *from; | |
2873 | ||
2874 | page = alloc_pages(__GFP_NOWARN | | |
2875 | __GFP_COMP | GFP_ATOMIC, | |
2876 | get_order(a + b)); | |
2877 | if (unlikely(!page)) | |
2878 | return -ENOMEM; | |
2879 | ||
2880 | sge->length = a; | |
2881 | orig = sg_page(sge); | |
2882 | from = sg_virt(sge); | |
2883 | to = page_address(page); | |
2884 | memcpy(to, from, a); | |
2885 | memcpy(to + a, from + a + pop, b); | |
2886 | sg_set_page(sge, page, a + b, 0); | |
2887 | put_page(orig); | |
2888 | } | |
2889 | pop = 0; | |
2890 | } else if (pop >= sge->length - a) { | |
7246d8ed | 2891 | pop -= (sge->length - a); |
3e104c23 | 2892 | sge->length = a; |
7246d8ed JF |
2893 | } |
2894 | } | |
2895 | ||
2896 | /* From above the current layout _must_ be as follows, | |
2897 | * | |
2898 | * -| offset | |
2899 | * -| start | |
2900 | * | |
2901 | * |---- pop ---|---------------- b ------------| | |
2902 | * |____________________________________________| length | |
2903 | * | |
2904 | * Offset and start of the current msg elem are equal because in the | |
2905 | * previous case we handled offset != start and either consumed the | |
2906 | * entire element and advanced to the next element OR pop == 0. | |
2907 | * | |
2908 | * Two cases to handle here are first pop is less than the length | |
2909 | * leaving some remainder b above. Simply adjust the element's layout | |
2910 | * in this case. Or pop >= length of the element so that b = 0. In this | |
2911 | * case advance to next element decrementing pop. | |
2912 | */ | |
2913 | while (pop) { | |
2914 | struct scatterlist *sge = sk_msg_elem(msg, i); | |
2915 | ||
2916 | if (pop < sge->length) { | |
2917 | sge->length -= pop; | |
2918 | sge->offset += pop; | |
2919 | pop = 0; | |
2920 | } else { | |
2921 | pop -= sge->length; | |
2922 | sk_msg_shift_left(msg, i); | |
2923 | } | |
2924 | sk_msg_iter_var_next(i); | |
2925 | } | |
2926 | ||
2927 | sk_mem_uncharge(msg->sk, len - pop); | |
2928 | msg->sg.size -= (len - pop); | |
2929 | sk_msg_compute_data_pointers(msg); | |
2930 | return 0; | |
2931 | } | |
2932 | ||
2933 | static const struct bpf_func_proto bpf_msg_pop_data_proto = { | |
2934 | .func = bpf_msg_pop_data, | |
2935 | .gpl_only = false, | |
2936 | .ret_type = RET_INTEGER, | |
2937 | .arg1_type = ARG_PTR_TO_CTX, | |
2938 | .arg2_type = ARG_ANYTHING, | |
2939 | .arg3_type = ARG_ANYTHING, | |
2940 | .arg4_type = ARG_ANYTHING, | |
2941 | }; | |
2942 | ||
5a52ae4e DB |
2943 | #ifdef CONFIG_CGROUP_NET_CLASSID |
2944 | BPF_CALL_0(bpf_get_cgroup_classid_curr) | |
2945 | { | |
2946 | return __task_get_classid(current); | |
2947 | } | |
2948 | ||
2949 | static const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = { | |
2950 | .func = bpf_get_cgroup_classid_curr, | |
2951 | .gpl_only = false, | |
2952 | .ret_type = RET_INTEGER, | |
2953 | }; | |
b426ce83 DB |
2954 | |
2955 | BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb) | |
2956 | { | |
2957 | struct sock *sk = skb_to_full_sk(skb); | |
2958 | ||
2959 | if (!sk || !sk_fullsock(sk)) | |
2960 | return 0; | |
2961 | ||
2962 | return sock_cgroup_classid(&sk->sk_cgrp_data); | |
2963 | } | |
2964 | ||
2965 | static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = { | |
2966 | .func = bpf_skb_cgroup_classid, | |
2967 | .gpl_only = false, | |
2968 | .ret_type = RET_INTEGER, | |
2969 | .arg1_type = ARG_PTR_TO_CTX, | |
2970 | }; | |
5a52ae4e DB |
2971 | #endif |
2972 | ||
f3694e00 | 2973 | BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb) |
8d20aabe | 2974 | { |
f3694e00 | 2975 | return task_get_classid(skb); |
8d20aabe DB |
2976 | } |
2977 | ||
2978 | static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { | |
2979 | .func = bpf_get_cgroup_classid, | |
2980 | .gpl_only = false, | |
2981 | .ret_type = RET_INTEGER, | |
2982 | .arg1_type = ARG_PTR_TO_CTX, | |
2983 | }; | |
2984 | ||
f3694e00 | 2985 | BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb) |
c46646d0 | 2986 | { |
f3694e00 | 2987 | return dst_tclassid(skb); |
c46646d0 DB |
2988 | } |
2989 | ||
2990 | static const struct bpf_func_proto bpf_get_route_realm_proto = { | |
2991 | .func = bpf_get_route_realm, | |
2992 | .gpl_only = false, | |
2993 | .ret_type = RET_INTEGER, | |
2994 | .arg1_type = ARG_PTR_TO_CTX, | |
2995 | }; | |
2996 | ||
f3694e00 | 2997 | BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb) |
13c5c240 DB |
2998 | { |
2999 | /* If skb_clear_hash() was called due to mangling, we can | |
3000 | * trigger SW recalculation here. Later access to hash | |
3001 | * can then use the inline skb->hash via context directly | |
3002 | * instead of calling this helper again. | |
3003 | */ | |
f3694e00 | 3004 | return skb_get_hash(skb); |
13c5c240 DB |
3005 | } |
3006 | ||
3007 | static const struct bpf_func_proto bpf_get_hash_recalc_proto = { | |
3008 | .func = bpf_get_hash_recalc, | |
3009 | .gpl_only = false, | |
3010 | .ret_type = RET_INTEGER, | |
3011 | .arg1_type = ARG_PTR_TO_CTX, | |
3012 | }; | |
3013 | ||
7a4b28c6 DB |
3014 | BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb) |
3015 | { | |
3016 | /* After all direct packet write, this can be used once for | |
3017 | * triggering a lazy recalc on next skb_get_hash() invocation. | |
3018 | */ | |
3019 | skb_clear_hash(skb); | |
3020 | return 0; | |
3021 | } | |
3022 | ||
3023 | static const struct bpf_func_proto bpf_set_hash_invalid_proto = { | |
3024 | .func = bpf_set_hash_invalid, | |
3025 | .gpl_only = false, | |
3026 | .ret_type = RET_INTEGER, | |
3027 | .arg1_type = ARG_PTR_TO_CTX, | |
3028 | }; | |
3029 | ||
ded092cd DB |
3030 | BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash) |
3031 | { | |
3032 | /* Set user specified hash as L4(+), so that it gets returned | |
3033 | * on skb_get_hash() call unless BPF prog later on triggers a | |
3034 | * skb_clear_hash(). | |
3035 | */ | |
3036 | __skb_set_sw_hash(skb, hash, true); | |
3037 | return 0; | |
3038 | } | |
3039 | ||
3040 | static const struct bpf_func_proto bpf_set_hash_proto = { | |
3041 | .func = bpf_set_hash, | |
3042 | .gpl_only = false, | |
3043 | .ret_type = RET_INTEGER, | |
3044 | .arg1_type = ARG_PTR_TO_CTX, | |
3045 | .arg2_type = ARG_ANYTHING, | |
3046 | }; | |
3047 | ||
f3694e00 DB |
3048 | BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto, |
3049 | u16, vlan_tci) | |
4e10df9a | 3050 | { |
db58ba45 | 3051 | int ret; |
4e10df9a AS |
3052 | |
3053 | if (unlikely(vlan_proto != htons(ETH_P_8021Q) && | |
3054 | vlan_proto != htons(ETH_P_8021AD))) | |
3055 | vlan_proto = htons(ETH_P_8021Q); | |
3056 | ||
8065694e | 3057 | bpf_push_mac_rcsum(skb); |
db58ba45 | 3058 | ret = skb_vlan_push(skb, vlan_proto, vlan_tci); |
8065694e DB |
3059 | bpf_pull_mac_rcsum(skb); |
3060 | ||
6aaae2b6 | 3061 | bpf_compute_data_pointers(skb); |
db58ba45 | 3062 | return ret; |
4e10df9a AS |
3063 | } |
3064 | ||
93731ef0 | 3065 | static const struct bpf_func_proto bpf_skb_vlan_push_proto = { |
4e10df9a AS |
3066 | .func = bpf_skb_vlan_push, |
3067 | .gpl_only = false, | |
3068 | .ret_type = RET_INTEGER, | |
3069 | .arg1_type = ARG_PTR_TO_CTX, | |
3070 | .arg2_type = ARG_ANYTHING, | |
3071 | .arg3_type = ARG_ANYTHING, | |
3072 | }; | |
3073 | ||
f3694e00 | 3074 | BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb) |
4e10df9a | 3075 | { |
db58ba45 | 3076 | int ret; |
4e10df9a | 3077 | |
8065694e | 3078 | bpf_push_mac_rcsum(skb); |
db58ba45 | 3079 | ret = skb_vlan_pop(skb); |
8065694e DB |
3080 | bpf_pull_mac_rcsum(skb); |
3081 | ||
6aaae2b6 | 3082 | bpf_compute_data_pointers(skb); |
db58ba45 | 3083 | return ret; |
4e10df9a AS |
3084 | } |
3085 | ||
93731ef0 | 3086 | static const struct bpf_func_proto bpf_skb_vlan_pop_proto = { |
4e10df9a AS |
3087 | .func = bpf_skb_vlan_pop, |
3088 | .gpl_only = false, | |
3089 | .ret_type = RET_INTEGER, | |
3090 | .arg1_type = ARG_PTR_TO_CTX, | |
3091 | }; | |
3092 | ||
6578171a DB |
3093 | static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len) |
3094 | { | |
3095 | /* Caller already did skb_cow() with len as headroom, | |
3096 | * so no need to do it here. | |
3097 | */ | |
3098 | skb_push(skb, len); | |
3099 | memmove(skb->data, skb->data + len, off); | |
3100 | memset(skb->data + off, 0, len); | |
3101 | ||
3102 | /* No skb_postpush_rcsum(skb, skb->data + off, len) | |
3103 | * needed here as it does not change the skb->csum | |
3104 | * result for checksum complete when summing over | |
3105 | * zeroed blocks. | |
3106 | */ | |
3107 | return 0; | |
3108 | } | |
3109 | ||
3110 | static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len) | |
3111 | { | |
3112 | /* skb_ensure_writable() is not needed here, as we're | |
3113 | * already working on an uncloned skb. | |
3114 | */ | |
3115 | if (unlikely(!pskb_may_pull(skb, off + len))) | |
3116 | return -ENOMEM; | |
3117 | ||
3118 | skb_postpull_rcsum(skb, skb->data + off, len); | |
3119 | memmove(skb->data + len, skb->data, off); | |
3120 | __skb_pull(skb, len); | |
3121 | ||
3122 | return 0; | |
3123 | } | |
3124 | ||
3125 | static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len) | |
3126 | { | |
3127 | bool trans_same = skb->transport_header == skb->network_header; | |
3128 | int ret; | |
3129 | ||
3130 | /* There's no need for __skb_push()/__skb_pull() pair to | |
3131 | * get to the start of the mac header as we're guaranteed | |
3132 | * to always start from here under eBPF. | |
3133 | */ | |
3134 | ret = bpf_skb_generic_push(skb, off, len); | |
3135 | if (likely(!ret)) { | |
3136 | skb->mac_header -= len; | |
3137 | skb->network_header -= len; | |
3138 | if (trans_same) | |
3139 | skb->transport_header = skb->network_header; | |
3140 | } | |
3141 | ||
3142 | return ret; | |
3143 | } | |
3144 | ||
3145 | static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len) | |
3146 | { | |
3147 | bool trans_same = skb->transport_header == skb->network_header; | |
3148 | int ret; | |
3149 | ||
3150 | /* Same here, __skb_push()/__skb_pull() pair not needed. */ | |
3151 | ret = bpf_skb_generic_pop(skb, off, len); | |
3152 | if (likely(!ret)) { | |
3153 | skb->mac_header += len; | |
3154 | skb->network_header += len; | |
3155 | if (trans_same) | |
3156 | skb->transport_header = skb->network_header; | |
3157 | } | |
3158 | ||
3159 | return ret; | |
3160 | } | |
3161 | ||
3162 | static int bpf_skb_proto_4_to_6(struct sk_buff *skb) | |
3163 | { | |
3164 | const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); | |
0daf4349 | 3165 | u32 off = skb_mac_header_len(skb); |
6578171a DB |
3166 | int ret; |
3167 | ||
4c3024de | 3168 | if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) |
d02f51cb DA |
3169 | return -ENOTSUPP; |
3170 | ||
6578171a DB |
3171 | ret = skb_cow(skb, len_diff); |
3172 | if (unlikely(ret < 0)) | |
3173 | return ret; | |
3174 | ||
3175 | ret = bpf_skb_net_hdr_push(skb, off, len_diff); | |
3176 | if (unlikely(ret < 0)) | |
3177 | return ret; | |
3178 | ||
3179 | if (skb_is_gso(skb)) { | |
d02f51cb DA |
3180 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
3181 | ||
880388aa DM |
3182 | /* SKB_GSO_TCPV4 needs to be changed into |
3183 | * SKB_GSO_TCPV6. | |
6578171a | 3184 | */ |
d02f51cb DA |
3185 | if (shinfo->gso_type & SKB_GSO_TCPV4) { |
3186 | shinfo->gso_type &= ~SKB_GSO_TCPV4; | |
3187 | shinfo->gso_type |= SKB_GSO_TCPV6; | |
6578171a DB |
3188 | } |
3189 | ||
3190 | /* Due to IPv6 header, MSS needs to be downgraded. */ | |
d02f51cb | 3191 | skb_decrease_gso_size(shinfo, len_diff); |
6578171a | 3192 | /* Header must be checked, and gso_segs recomputed. */ |
d02f51cb DA |
3193 | shinfo->gso_type |= SKB_GSO_DODGY; |
3194 | shinfo->gso_segs = 0; | |
6578171a DB |
3195 | } |
3196 | ||
3197 | skb->protocol = htons(ETH_P_IPV6); | |
3198 | skb_clear_hash(skb); | |
3199 | ||
3200 | return 0; | |
3201 | } | |
3202 | ||
3203 | static int bpf_skb_proto_6_to_4(struct sk_buff *skb) | |
3204 | { | |
3205 | const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); | |
0daf4349 | 3206 | u32 off = skb_mac_header_len(skb); |
6578171a DB |
3207 | int ret; |
3208 | ||
4c3024de | 3209 | if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) |
d02f51cb DA |
3210 | return -ENOTSUPP; |
3211 | ||
6578171a DB |
3212 | ret = skb_unclone(skb, GFP_ATOMIC); |
3213 | if (unlikely(ret < 0)) | |
3214 | return ret; | |
3215 | ||
3216 | ret = bpf_skb_net_hdr_pop(skb, off, len_diff); | |
3217 | if (unlikely(ret < 0)) | |
3218 | return ret; | |
3219 | ||
3220 | if (skb_is_gso(skb)) { | |
d02f51cb DA |
3221 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
3222 | ||
880388aa DM |
3223 | /* SKB_GSO_TCPV6 needs to be changed into |
3224 | * SKB_GSO_TCPV4. | |
6578171a | 3225 | */ |
d02f51cb DA |
3226 | if (shinfo->gso_type & SKB_GSO_TCPV6) { |
3227 | shinfo->gso_type &= ~SKB_GSO_TCPV6; | |
3228 | shinfo->gso_type |= SKB_GSO_TCPV4; | |
6578171a DB |
3229 | } |
3230 | ||
3231 | /* Due to IPv4 header, MSS can be upgraded. */ | |
d02f51cb | 3232 | skb_increase_gso_size(shinfo, len_diff); |
6578171a | 3233 | /* Header must be checked, and gso_segs recomputed. */ |
d02f51cb DA |
3234 | shinfo->gso_type |= SKB_GSO_DODGY; |
3235 | shinfo->gso_segs = 0; | |
6578171a DB |
3236 | } |
3237 | ||
3238 | skb->protocol = htons(ETH_P_IP); | |
3239 | skb_clear_hash(skb); | |
3240 | ||
3241 | return 0; | |
3242 | } | |
3243 | ||
3244 | static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto) | |
3245 | { | |
3246 | __be16 from_proto = skb->protocol; | |
3247 | ||
3248 | if (from_proto == htons(ETH_P_IP) && | |
3249 | to_proto == htons(ETH_P_IPV6)) | |
3250 | return bpf_skb_proto_4_to_6(skb); | |
3251 | ||
3252 | if (from_proto == htons(ETH_P_IPV6) && | |
3253 | to_proto == htons(ETH_P_IP)) | |
3254 | return bpf_skb_proto_6_to_4(skb); | |
3255 | ||
3256 | return -ENOTSUPP; | |
3257 | } | |
3258 | ||
f3694e00 DB |
3259 | BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto, |
3260 | u64, flags) | |
6578171a | 3261 | { |
6578171a DB |
3262 | int ret; |
3263 | ||
3264 | if (unlikely(flags)) | |
3265 | return -EINVAL; | |
3266 | ||
3267 | /* General idea is that this helper does the basic groundwork | |
3268 | * needed for changing the protocol, and eBPF program fills the | |
3269 | * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace() | |
3270 | * and other helpers, rather than passing a raw buffer here. | |
3271 | * | |
3272 | * The rationale is to keep this minimal and without a need to | |
3273 | * deal with raw packet data. F.e. even if we would pass buffers | |
3274 | * here, the program still needs to call the bpf_lX_csum_replace() | |
3275 | * helpers anyway. Plus, this way we keep also separation of | |
3276 | * concerns, since f.e. bpf_skb_store_bytes() should only take | |
3277 | * care of stores. | |
3278 | * | |
3279 | * Currently, additional options and extension header space are | |
3280 | * not supported, but flags register is reserved so we can adapt | |
3281 | * that. For offloads, we mark packet as dodgy, so that headers | |
3282 | * need to be verified first. | |
3283 | */ | |
3284 | ret = bpf_skb_proto_xlat(skb, proto); | |
6aaae2b6 | 3285 | bpf_compute_data_pointers(skb); |
6578171a DB |
3286 | return ret; |
3287 | } | |
3288 | ||
3289 | static const struct bpf_func_proto bpf_skb_change_proto_proto = { | |
3290 | .func = bpf_skb_change_proto, | |
3291 | .gpl_only = false, | |
3292 | .ret_type = RET_INTEGER, | |
3293 | .arg1_type = ARG_PTR_TO_CTX, | |
3294 | .arg2_type = ARG_ANYTHING, | |
3295 | .arg3_type = ARG_ANYTHING, | |
3296 | }; | |
3297 | ||
f3694e00 | 3298 | BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type) |
d2485c42 | 3299 | { |
d2485c42 | 3300 | /* We only allow a restricted subset to be changed for now. */ |
45c7fffa DB |
3301 | if (unlikely(!skb_pkt_type_ok(skb->pkt_type) || |
3302 | !skb_pkt_type_ok(pkt_type))) | |
d2485c42 DB |
3303 | return -EINVAL; |
3304 | ||
3305 | skb->pkt_type = pkt_type; | |
3306 | return 0; | |
3307 | } | |
3308 | ||
3309 | static const struct bpf_func_proto bpf_skb_change_type_proto = { | |
3310 | .func = bpf_skb_change_type, | |
3311 | .gpl_only = false, | |
3312 | .ret_type = RET_INTEGER, | |
3313 | .arg1_type = ARG_PTR_TO_CTX, | |
3314 | .arg2_type = ARG_ANYTHING, | |
3315 | }; | |
3316 | ||
2be7e212 DB |
3317 | static u32 bpf_skb_net_base_len(const struct sk_buff *skb) |
3318 | { | |
3319 | switch (skb->protocol) { | |
3320 | case htons(ETH_P_IP): | |
3321 | return sizeof(struct iphdr); | |
3322 | case htons(ETH_P_IPV6): | |
3323 | return sizeof(struct ipv6hdr); | |
3324 | default: | |
3325 | return ~0U; | |
3326 | } | |
3327 | } | |
3328 | ||
868d5235 WB |
3329 | #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \ |
3330 | BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) | |
3331 | ||
3332 | #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \ | |
3333 | BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \ | |
3334 | BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \ | |
58dfc900 AM |
3335 | BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \ |
3336 | BPF_F_ADJ_ROOM_ENCAP_L2( \ | |
3337 | BPF_ADJ_ROOM_ENCAP_L2_MASK)) | |
2278f6cc WB |
3338 | |
3339 | static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff, | |
3340 | u64 flags) | |
2be7e212 | 3341 | { |
58dfc900 | 3342 | u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT; |
868d5235 | 3343 | bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK; |
62b31b42 | 3344 | u16 mac_len = 0, inner_net = 0, inner_trans = 0; |
868d5235 | 3345 | unsigned int gso_type = SKB_GSO_DODGY; |
2be7e212 DB |
3346 | int ret; |
3347 | ||
2278f6cc WB |
3348 | if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) { |
3349 | /* udp gso_size delineates datagrams, only allow if fixed */ | |
3350 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) || | |
3351 | !(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) | |
3352 | return -ENOTSUPP; | |
3353 | } | |
d02f51cb | 3354 | |
908adce6 | 3355 | ret = skb_cow_head(skb, len_diff); |
2be7e212 DB |
3356 | if (unlikely(ret < 0)) |
3357 | return ret; | |
3358 | ||
868d5235 WB |
3359 | if (encap) { |
3360 | if (skb->protocol != htons(ETH_P_IP) && | |
3361 | skb->protocol != htons(ETH_P_IPV6)) | |
3362 | return -ENOTSUPP; | |
3363 | ||
3364 | if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 && | |
3365 | flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) | |
3366 | return -EINVAL; | |
3367 | ||
3368 | if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE && | |
3369 | flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) | |
3370 | return -EINVAL; | |
3371 | ||
3372 | if (skb->encapsulation) | |
3373 | return -EALREADY; | |
3374 | ||
3375 | mac_len = skb->network_header - skb->mac_header; | |
3376 | inner_net = skb->network_header; | |
58dfc900 AM |
3377 | if (inner_mac_len > len_diff) |
3378 | return -EINVAL; | |
868d5235 WB |
3379 | inner_trans = skb->transport_header; |
3380 | } | |
3381 | ||
2be7e212 DB |
3382 | ret = bpf_skb_net_hdr_push(skb, off, len_diff); |
3383 | if (unlikely(ret < 0)) | |
3384 | return ret; | |
3385 | ||
868d5235 | 3386 | if (encap) { |
58dfc900 | 3387 | skb->inner_mac_header = inner_net - inner_mac_len; |
868d5235 WB |
3388 | skb->inner_network_header = inner_net; |
3389 | skb->inner_transport_header = inner_trans; | |
3390 | skb_set_inner_protocol(skb, skb->protocol); | |
3391 | ||
3392 | skb->encapsulation = 1; | |
3393 | skb_set_network_header(skb, mac_len); | |
3394 | ||
3395 | if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) | |
3396 | gso_type |= SKB_GSO_UDP_TUNNEL; | |
3397 | else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE) | |
3398 | gso_type |= SKB_GSO_GRE; | |
3399 | else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) | |
3400 | gso_type |= SKB_GSO_IPXIP6; | |
58dfc900 | 3401 | else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4) |
868d5235 WB |
3402 | gso_type |= SKB_GSO_IPXIP4; |
3403 | ||
3404 | if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE || | |
3405 | flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) { | |
3406 | int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ? | |
3407 | sizeof(struct ipv6hdr) : | |
3408 | sizeof(struct iphdr); | |
3409 | ||
3410 | skb_set_transport_header(skb, mac_len + nh_len); | |
3411 | } | |
1b00e0df WB |
3412 | |
3413 | /* Match skb->protocol to new outer l3 protocol */ | |
3414 | if (skb->protocol == htons(ETH_P_IP) && | |
3415 | flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6) | |
3416 | skb->protocol = htons(ETH_P_IPV6); | |
3417 | else if (skb->protocol == htons(ETH_P_IPV6) && | |
3418 | flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4) | |
3419 | skb->protocol = htons(ETH_P_IP); | |
868d5235 WB |
3420 | } |
3421 | ||
2be7e212 | 3422 | if (skb_is_gso(skb)) { |
d02f51cb DA |
3423 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
3424 | ||
2be7e212 | 3425 | /* Due to header grow, MSS needs to be downgraded. */ |
2278f6cc WB |
3426 | if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) |
3427 | skb_decrease_gso_size(shinfo, len_diff); | |
3428 | ||
2be7e212 | 3429 | /* Header must be checked, and gso_segs recomputed. */ |
868d5235 | 3430 | shinfo->gso_type |= gso_type; |
d02f51cb | 3431 | shinfo->gso_segs = 0; |
2be7e212 DB |
3432 | } |
3433 | ||
3434 | return 0; | |
3435 | } | |
3436 | ||
2278f6cc WB |
3437 | static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff, |
3438 | u64 flags) | |
2be7e212 | 3439 | { |
2be7e212 DB |
3440 | int ret; |
3441 | ||
836e66c2 DB |
3442 | if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO | |
3443 | BPF_F_ADJ_ROOM_NO_CSUM_RESET))) | |
43537b8e WB |
3444 | return -EINVAL; |
3445 | ||
2278f6cc WB |
3446 | if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) { |
3447 | /* udp gso_size delineates datagrams, only allow if fixed */ | |
3448 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) || | |
3449 | !(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) | |
3450 | return -ENOTSUPP; | |
3451 | } | |
d02f51cb | 3452 | |
2be7e212 DB |
3453 | ret = skb_unclone(skb, GFP_ATOMIC); |
3454 | if (unlikely(ret < 0)) | |
3455 | return ret; | |
3456 | ||
3457 | ret = bpf_skb_net_hdr_pop(skb, off, len_diff); | |
3458 | if (unlikely(ret < 0)) | |
3459 | return ret; | |
3460 | ||
3461 | if (skb_is_gso(skb)) { | |
d02f51cb DA |
3462 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
3463 | ||
2be7e212 | 3464 | /* Due to header shrink, MSS can be upgraded. */ |
2278f6cc WB |
3465 | if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) |
3466 | skb_increase_gso_size(shinfo, len_diff); | |
3467 | ||
2be7e212 | 3468 | /* Header must be checked, and gso_segs recomputed. */ |
d02f51cb DA |
3469 | shinfo->gso_type |= SKB_GSO_DODGY; |
3470 | shinfo->gso_segs = 0; | |
2be7e212 DB |
3471 | } |
3472 | ||
3473 | return 0; | |
3474 | } | |
3475 | ||
3476 | static u32 __bpf_skb_max_len(const struct sk_buff *skb) | |
3477 | { | |
0c6bc6e5 JF |
3478 | return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len : |
3479 | SKB_MAX_ALLOC; | |
2be7e212 DB |
3480 | } |
3481 | ||
14aa3192 WB |
3482 | BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff, |
3483 | u32, mode, u64, flags) | |
2be7e212 | 3484 | { |
2be7e212 DB |
3485 | u32 len_cur, len_diff_abs = abs(len_diff); |
3486 | u32 len_min = bpf_skb_net_base_len(skb); | |
3487 | u32 len_max = __bpf_skb_max_len(skb); | |
3488 | __be16 proto = skb->protocol; | |
3489 | bool shrink = len_diff < 0; | |
14aa3192 | 3490 | u32 off; |
2be7e212 DB |
3491 | int ret; |
3492 | ||
836e66c2 DB |
3493 | if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK | |
3494 | BPF_F_ADJ_ROOM_NO_CSUM_RESET))) | |
14aa3192 | 3495 | return -EINVAL; |
2be7e212 DB |
3496 | if (unlikely(len_diff_abs > 0xfffU)) |
3497 | return -EFAULT; | |
3498 | if (unlikely(proto != htons(ETH_P_IP) && | |
3499 | proto != htons(ETH_P_IPV6))) | |
3500 | return -ENOTSUPP; | |
3501 | ||
14aa3192 WB |
3502 | off = skb_mac_header_len(skb); |
3503 | switch (mode) { | |
3504 | case BPF_ADJ_ROOM_NET: | |
3505 | off += bpf_skb_net_base_len(skb); | |
3506 | break; | |
3507 | case BPF_ADJ_ROOM_MAC: | |
3508 | break; | |
3509 | default: | |
3510 | return -ENOTSUPP; | |
3511 | } | |
3512 | ||
2be7e212 | 3513 | len_cur = skb->len - skb_network_offset(skb); |
2be7e212 DB |
3514 | if ((shrink && (len_diff_abs >= len_cur || |
3515 | len_cur - len_diff_abs < len_min)) || | |
3516 | (!shrink && (skb->len + len_diff_abs > len_max && | |
3517 | !skb_is_gso(skb)))) | |
3518 | return -ENOTSUPP; | |
3519 | ||
2278f6cc WB |
3520 | ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) : |
3521 | bpf_skb_net_grow(skb, off, len_diff_abs, flags); | |
836e66c2 DB |
3522 | if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET)) |
3523 | __skb_reset_checksum_unnecessary(skb); | |
2be7e212 | 3524 | |
6aaae2b6 | 3525 | bpf_compute_data_pointers(skb); |
e4a6a342 | 3526 | return ret; |
2be7e212 DB |
3527 | } |
3528 | ||
2be7e212 DB |
3529 | static const struct bpf_func_proto bpf_skb_adjust_room_proto = { |
3530 | .func = bpf_skb_adjust_room, | |
3531 | .gpl_only = false, | |
3532 | .ret_type = RET_INTEGER, | |
3533 | .arg1_type = ARG_PTR_TO_CTX, | |
3534 | .arg2_type = ARG_ANYTHING, | |
3535 | .arg3_type = ARG_ANYTHING, | |
3536 | .arg4_type = ARG_ANYTHING, | |
3537 | }; | |
3538 | ||
5293efe6 DB |
3539 | static u32 __bpf_skb_min_len(const struct sk_buff *skb) |
3540 | { | |
3541 | u32 min_len = skb_network_offset(skb); | |
3542 | ||
3543 | if (skb_transport_header_was_set(skb)) | |
3544 | min_len = skb_transport_offset(skb); | |
3545 | if (skb->ip_summed == CHECKSUM_PARTIAL) | |
3546 | min_len = skb_checksum_start_offset(skb) + | |
3547 | skb->csum_offset + sizeof(__sum16); | |
3548 | return min_len; | |
3549 | } | |
3550 | ||
5293efe6 DB |
3551 | static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len) |
3552 | { | |
3553 | unsigned int old_len = skb->len; | |
3554 | int ret; | |
3555 | ||
3556 | ret = __skb_grow_rcsum(skb, new_len); | |
3557 | if (!ret) | |
3558 | memset(skb->data + old_len, 0, new_len - old_len); | |
3559 | return ret; | |
3560 | } | |
3561 | ||
3562 | static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len) | |
3563 | { | |
3564 | return __skb_trim_rcsum(skb, new_len); | |
3565 | } | |
3566 | ||
0ea488ff JF |
3567 | static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len, |
3568 | u64 flags) | |
5293efe6 | 3569 | { |
5293efe6 DB |
3570 | u32 max_len = __bpf_skb_max_len(skb); |
3571 | u32 min_len = __bpf_skb_min_len(skb); | |
5293efe6 DB |
3572 | int ret; |
3573 | ||
3574 | if (unlikely(flags || new_len > max_len || new_len < min_len)) | |
3575 | return -EINVAL; | |
3576 | if (skb->encapsulation) | |
3577 | return -ENOTSUPP; | |
3578 | ||
3579 | /* The basic idea of this helper is that it's performing the | |
3580 | * needed work to either grow or trim an skb, and eBPF program | |
3581 | * rewrites the rest via helpers like bpf_skb_store_bytes(), | |
3582 | * bpf_lX_csum_replace() and others rather than passing a raw | |
3583 | * buffer here. This one is a slow path helper and intended | |
3584 | * for replies with control messages. | |
3585 | * | |
3586 | * Like in bpf_skb_change_proto(), we want to keep this rather | |
3587 | * minimal and without protocol specifics so that we are able | |
3588 | * to separate concerns as in bpf_skb_store_bytes() should only | |
3589 | * be the one responsible for writing buffers. | |
3590 | * | |
3591 | * It's really expected to be a slow path operation here for | |
3592 | * control message replies, so we're implicitly linearizing, | |
3593 | * uncloning and drop offloads from the skb by this. | |
3594 | */ | |
3595 | ret = __bpf_try_make_writable(skb, skb->len); | |
3596 | if (!ret) { | |
3597 | if (new_len > skb->len) | |
3598 | ret = bpf_skb_grow_rcsum(skb, new_len); | |
3599 | else if (new_len < skb->len) | |
3600 | ret = bpf_skb_trim_rcsum(skb, new_len); | |
3601 | if (!ret && skb_is_gso(skb)) | |
3602 | skb_gso_reset(skb); | |
3603 | } | |
0ea488ff JF |
3604 | return ret; |
3605 | } | |
3606 | ||
3607 | BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len, | |
3608 | u64, flags) | |
3609 | { | |
3610 | int ret = __bpf_skb_change_tail(skb, new_len, flags); | |
5293efe6 | 3611 | |
6aaae2b6 | 3612 | bpf_compute_data_pointers(skb); |
5293efe6 DB |
3613 | return ret; |
3614 | } | |
3615 | ||
3616 | static const struct bpf_func_proto bpf_skb_change_tail_proto = { | |
3617 | .func = bpf_skb_change_tail, | |
3618 | .gpl_only = false, | |
3619 | .ret_type = RET_INTEGER, | |
3620 | .arg1_type = ARG_PTR_TO_CTX, | |
3621 | .arg2_type = ARG_ANYTHING, | |
3622 | .arg3_type = ARG_ANYTHING, | |
3623 | }; | |
3624 | ||
0ea488ff | 3625 | BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len, |
3a0af8fd | 3626 | u64, flags) |
0ea488ff JF |
3627 | { |
3628 | int ret = __bpf_skb_change_tail(skb, new_len, flags); | |
3629 | ||
3630 | bpf_compute_data_end_sk_skb(skb); | |
3631 | return ret; | |
3632 | } | |
3633 | ||
3634 | static const struct bpf_func_proto sk_skb_change_tail_proto = { | |
3635 | .func = sk_skb_change_tail, | |
3636 | .gpl_only = false, | |
3637 | .ret_type = RET_INTEGER, | |
3638 | .arg1_type = ARG_PTR_TO_CTX, | |
3639 | .arg2_type = ARG_ANYTHING, | |
3640 | .arg3_type = ARG_ANYTHING, | |
3641 | }; | |
3642 | ||
3643 | static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room, | |
3644 | u64 flags) | |
3a0af8fd TG |
3645 | { |
3646 | u32 max_len = __bpf_skb_max_len(skb); | |
3647 | u32 new_len = skb->len + head_room; | |
3648 | int ret; | |
3649 | ||
3650 | if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) || | |
3651 | new_len < skb->len)) | |
3652 | return -EINVAL; | |
3653 | ||
3654 | ret = skb_cow(skb, head_room); | |
3655 | if (likely(!ret)) { | |
3656 | /* Idea for this helper is that we currently only | |
3657 | * allow to expand on mac header. This means that | |
3658 | * skb->protocol network header, etc, stay as is. | |
3659 | * Compared to bpf_skb_change_tail(), we're more | |
3660 | * flexible due to not needing to linearize or | |
3661 | * reset GSO. Intention for this helper is to be | |
3662 | * used by an L3 skb that needs to push mac header | |
3663 | * for redirection into L2 device. | |
3664 | */ | |
3665 | __skb_push(skb, head_room); | |
3666 | memset(skb->data, 0, head_room); | |
3667 | skb_reset_mac_header(skb); | |
3668 | } | |
3669 | ||
0ea488ff JF |
3670 | return ret; |
3671 | } | |
3672 | ||
3673 | BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room, | |
3674 | u64, flags) | |
3675 | { | |
3676 | int ret = __bpf_skb_change_head(skb, head_room, flags); | |
3677 | ||
6aaae2b6 | 3678 | bpf_compute_data_pointers(skb); |
0ea488ff | 3679 | return ret; |
3a0af8fd TG |
3680 | } |
3681 | ||
3682 | static const struct bpf_func_proto bpf_skb_change_head_proto = { | |
3683 | .func = bpf_skb_change_head, | |
3684 | .gpl_only = false, | |
3685 | .ret_type = RET_INTEGER, | |
3686 | .arg1_type = ARG_PTR_TO_CTX, | |
3687 | .arg2_type = ARG_ANYTHING, | |
3688 | .arg3_type = ARG_ANYTHING, | |
3689 | }; | |
3690 | ||
0ea488ff JF |
3691 | BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room, |
3692 | u64, flags) | |
3693 | { | |
3694 | int ret = __bpf_skb_change_head(skb, head_room, flags); | |
3695 | ||
3696 | bpf_compute_data_end_sk_skb(skb); | |
3697 | return ret; | |
3698 | } | |
3699 | ||
3700 | static const struct bpf_func_proto sk_skb_change_head_proto = { | |
3701 | .func = sk_skb_change_head, | |
3702 | .gpl_only = false, | |
3703 | .ret_type = RET_INTEGER, | |
3704 | .arg1_type = ARG_PTR_TO_CTX, | |
3705 | .arg2_type = ARG_ANYTHING, | |
3706 | .arg3_type = ARG_ANYTHING, | |
3707 | }; | |
de8f3a83 DB |
3708 | static unsigned long xdp_get_metalen(const struct xdp_buff *xdp) |
3709 | { | |
3710 | return xdp_data_meta_unsupported(xdp) ? 0 : | |
3711 | xdp->data - xdp->data_meta; | |
3712 | } | |
3713 | ||
17bedab2 MKL |
3714 | BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset) |
3715 | { | |
6dfb970d | 3716 | void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); |
de8f3a83 | 3717 | unsigned long metalen = xdp_get_metalen(xdp); |
97e19cce | 3718 | void *data_start = xdp_frame_end + metalen; |
17bedab2 MKL |
3719 | void *data = xdp->data + offset; |
3720 | ||
de8f3a83 | 3721 | if (unlikely(data < data_start || |
17bedab2 MKL |
3722 | data > xdp->data_end - ETH_HLEN)) |
3723 | return -EINVAL; | |
3724 | ||
de8f3a83 DB |
3725 | if (metalen) |
3726 | memmove(xdp->data_meta + offset, | |
3727 | xdp->data_meta, metalen); | |
3728 | xdp->data_meta += offset; | |
17bedab2 MKL |
3729 | xdp->data = data; |
3730 | ||
3731 | return 0; | |
3732 | } | |
3733 | ||
3734 | static const struct bpf_func_proto bpf_xdp_adjust_head_proto = { | |
3735 | .func = bpf_xdp_adjust_head, | |
3736 | .gpl_only = false, | |
3737 | .ret_type = RET_INTEGER, | |
3738 | .arg1_type = ARG_PTR_TO_CTX, | |
3739 | .arg2_type = ARG_ANYTHING, | |
3740 | }; | |
3741 | ||
b32cc5b9 NS |
3742 | BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset) |
3743 | { | |
c8741e2b | 3744 | void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */ |
b32cc5b9 NS |
3745 | void *data_end = xdp->data_end + offset; |
3746 | ||
c8741e2b JDB |
3747 | /* Notice that xdp_data_hard_end have reserved some tailroom */ |
3748 | if (unlikely(data_end > data_hard_end)) | |
b32cc5b9 NS |
3749 | return -EINVAL; |
3750 | ||
c8741e2b JDB |
3751 | /* ALL drivers MUST init xdp->frame_sz, chicken check below */ |
3752 | if (unlikely(xdp->frame_sz > PAGE_SIZE)) { | |
3753 | WARN_ONCE(1, "Too BIG xdp->frame_sz = %d\n", xdp->frame_sz); | |
3754 | return -EINVAL; | |
3755 | } | |
3756 | ||
b32cc5b9 NS |
3757 | if (unlikely(data_end < xdp->data + ETH_HLEN)) |
3758 | return -EINVAL; | |
3759 | ||
ddb47d51 JDB |
3760 | /* Clear memory area on grow, can contain uninit kernel memory */ |
3761 | if (offset > 0) | |
3762 | memset(xdp->data_end, 0, offset); | |
3763 | ||
b32cc5b9 NS |
3764 | xdp->data_end = data_end; |
3765 | ||
3766 | return 0; | |
3767 | } | |
3768 | ||
3769 | static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = { | |
3770 | .func = bpf_xdp_adjust_tail, | |
3771 | .gpl_only = false, | |
3772 | .ret_type = RET_INTEGER, | |
3773 | .arg1_type = ARG_PTR_TO_CTX, | |
3774 | .arg2_type = ARG_ANYTHING, | |
3775 | }; | |
3776 | ||
de8f3a83 DB |
3777 | BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset) |
3778 | { | |
97e19cce | 3779 | void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame); |
de8f3a83 DB |
3780 | void *meta = xdp->data_meta + offset; |
3781 | unsigned long metalen = xdp->data - meta; | |
3782 | ||
3783 | if (xdp_data_meta_unsupported(xdp)) | |
3784 | return -ENOTSUPP; | |
97e19cce | 3785 | if (unlikely(meta < xdp_frame_end || |
de8f3a83 DB |
3786 | meta > xdp->data)) |
3787 | return -EINVAL; | |
3788 | if (unlikely((metalen & (sizeof(__u32) - 1)) || | |
3789 | (metalen > 32))) | |
3790 | return -EACCES; | |
3791 | ||
3792 | xdp->data_meta = meta; | |
3793 | ||
3794 | return 0; | |
3795 | } | |
3796 | ||
3797 | static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = { | |
3798 | .func = bpf_xdp_adjust_meta, | |
3799 | .gpl_only = false, | |
3800 | .ret_type = RET_INTEGER, | |
3801 | .arg1_type = ARG_PTR_TO_CTX, | |
3802 | .arg2_type = ARG_ANYTHING, | |
3803 | }; | |
3804 | ||
9c270af3 | 3805 | static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd, |
1170beaa | 3806 | struct bpf_map *map, struct xdp_buff *xdp) |
9c270af3 | 3807 | { |
1b1a251c | 3808 | switch (map->map_type) { |
6f9d451a | 3809 | case BPF_MAP_TYPE_DEVMAP: |
1170beaa BT |
3810 | case BPF_MAP_TYPE_DEVMAP_HASH: |
3811 | return dev_map_enqueue(fwd, xdp, dev_rx); | |
3812 | case BPF_MAP_TYPE_CPUMAP: | |
3813 | return cpu_map_enqueue(fwd, xdp, dev_rx); | |
3814 | case BPF_MAP_TYPE_XSKMAP: | |
3815 | return __xsk_map_redirect(fwd, xdp); | |
1b1a251c | 3816 | default: |
0a29275b | 3817 | return -EBADRQC; |
9c270af3 | 3818 | } |
e4a8e817 | 3819 | return 0; |
814abfab JF |
3820 | } |
3821 | ||
1d233886 | 3822 | void xdp_do_flush(void) |
11393cc9 | 3823 | { |
1d233886 | 3824 | __dev_flush(); |
332f22a6 BT |
3825 | __cpu_map_flush(); |
3826 | __xsk_map_flush(); | |
11393cc9 | 3827 | } |
1d233886 | 3828 | EXPORT_SYMBOL_GPL(xdp_do_flush); |
11393cc9 | 3829 | |
2a68d85f | 3830 | static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index) |
9c270af3 JDB |
3831 | { |
3832 | switch (map->map_type) { | |
3833 | case BPF_MAP_TYPE_DEVMAP: | |
3834 | return __dev_map_lookup_elem(map, index); | |
6f9d451a THJ |
3835 | case BPF_MAP_TYPE_DEVMAP_HASH: |
3836 | return __dev_map_hash_lookup_elem(map, index); | |
9c270af3 JDB |
3837 | case BPF_MAP_TYPE_CPUMAP: |
3838 | return __cpu_map_lookup_elem(map, index); | |
1b1a251c BT |
3839 | case BPF_MAP_TYPE_XSKMAP: |
3840 | return __xsk_map_lookup_elem(map, index); | |
9c270af3 JDB |
3841 | default: |
3842 | return NULL; | |
3843 | } | |
3844 | } | |
3845 | ||
f6069b9a | 3846 | void bpf_clear_redirect_map(struct bpf_map *map) |
7c300131 | 3847 | { |
f6069b9a DB |
3848 | struct bpf_redirect_info *ri; |
3849 | int cpu; | |
3850 | ||
3851 | for_each_possible_cpu(cpu) { | |
3852 | ri = per_cpu_ptr(&bpf_redirect_info, cpu); | |
3853 | /* Avoid polluting remote cacheline due to writes if | |
3854 | * not needed. Once we pass this test, we need the | |
3855 | * cmpxchg() to make sure it hasn't been changed in | |
3856 | * the meantime by remote CPU. | |
3857 | */ | |
3858 | if (unlikely(READ_ONCE(ri->map) == map)) | |
3859 | cmpxchg(&ri->map, map, NULL); | |
3860 | } | |
7c300131 DB |
3861 | } |
3862 | ||
1d233886 THJ |
3863 | int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp, |
3864 | struct bpf_prog *xdp_prog) | |
97f91a7c | 3865 | { |
1d233886 THJ |
3866 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); |
3867 | struct bpf_map *map = READ_ONCE(ri->map); | |
4b55cf29 | 3868 | u32 index = ri->tgt_index; |
43e74c02 | 3869 | void *fwd = ri->tgt_value; |
4c03bdd7 | 3870 | int err; |
97f91a7c | 3871 | |
4b55cf29 | 3872 | ri->tgt_index = 0; |
43e74c02 | 3873 | ri->tgt_value = NULL; |
f6069b9a | 3874 | WRITE_ONCE(ri->map, NULL); |
97f91a7c | 3875 | |
1d233886 THJ |
3876 | if (unlikely(!map)) { |
3877 | fwd = dev_get_by_index_rcu(dev_net(dev), index); | |
3878 | if (unlikely(!fwd)) { | |
3879 | err = -EINVAL; | |
3880 | goto err; | |
3881 | } | |
3882 | ||
3883 | err = dev_xdp_enqueue(fwd, xdp, dev); | |
3884 | } else { | |
3885 | err = __bpf_tx_xdp_map(dev, fwd, map, xdp); | |
3886 | } | |
3887 | ||
f5836ca5 JDB |
3888 | if (unlikely(err)) |
3889 | goto err; | |
3890 | ||
59a30896 | 3891 | _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index); |
f5836ca5 JDB |
3892 | return 0; |
3893 | err: | |
59a30896 | 3894 | _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err); |
97f91a7c JF |
3895 | return err; |
3896 | } | |
814abfab JF |
3897 | EXPORT_SYMBOL_GPL(xdp_do_redirect); |
3898 | ||
c060bc61 XS |
3899 | static int xdp_do_generic_redirect_map(struct net_device *dev, |
3900 | struct sk_buff *skb, | |
02671e23 | 3901 | struct xdp_buff *xdp, |
f6069b9a DB |
3902 | struct bpf_prog *xdp_prog, |
3903 | struct bpf_map *map) | |
6103aa96 | 3904 | { |
0b19cc0a | 3905 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); |
4b55cf29 | 3906 | u32 index = ri->tgt_index; |
43e74c02 | 3907 | void *fwd = ri->tgt_value; |
2facaad6 | 3908 | int err = 0; |
6103aa96 | 3909 | |
4b55cf29 | 3910 | ri->tgt_index = 0; |
43e74c02 | 3911 | ri->tgt_value = NULL; |
f6069b9a | 3912 | WRITE_ONCE(ri->map, NULL); |
96c5508e | 3913 | |
6f9d451a THJ |
3914 | if (map->map_type == BPF_MAP_TYPE_DEVMAP || |
3915 | map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) { | |
6d5fc195 TM |
3916 | struct bpf_dtab_netdev *dst = fwd; |
3917 | ||
3918 | err = dev_map_generic_redirect(dst, skb, xdp_prog); | |
3919 | if (unlikely(err)) | |
9c270af3 | 3920 | goto err; |
02671e23 BT |
3921 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
3922 | struct xdp_sock *xs = fwd; | |
3923 | ||
3924 | err = xsk_generic_rcv(xs, xdp); | |
3925 | if (err) | |
3926 | goto err; | |
3927 | consume_skb(skb); | |
9c270af3 JDB |
3928 | } else { |
3929 | /* TODO: Handle BPF_MAP_TYPE_CPUMAP */ | |
3930 | err = -EBADRQC; | |
f5836ca5 | 3931 | goto err; |
2facaad6 | 3932 | } |
6103aa96 | 3933 | |
9c270af3 JDB |
3934 | _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index); |
3935 | return 0; | |
3936 | err: | |
3937 | _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err); | |
3938 | return err; | |
3939 | } | |
3940 | ||
3941 | int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb, | |
02671e23 | 3942 | struct xdp_buff *xdp, struct bpf_prog *xdp_prog) |
9c270af3 | 3943 | { |
0b19cc0a | 3944 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); |
f6069b9a | 3945 | struct bpf_map *map = READ_ONCE(ri->map); |
4b55cf29 | 3946 | u32 index = ri->tgt_index; |
9c270af3 JDB |
3947 | struct net_device *fwd; |
3948 | int err = 0; | |
3949 | ||
f6069b9a DB |
3950 | if (map) |
3951 | return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, | |
3952 | map); | |
4b55cf29 | 3953 | ri->tgt_index = 0; |
9c270af3 JDB |
3954 | fwd = dev_get_by_index_rcu(dev_net(dev), index); |
3955 | if (unlikely(!fwd)) { | |
3956 | err = -EINVAL; | |
f5836ca5 | 3957 | goto err; |
2facaad6 JDB |
3958 | } |
3959 | ||
d8d7218a TM |
3960 | err = xdp_ok_fwd_dev(fwd, skb->len); |
3961 | if (unlikely(err)) | |
9c270af3 JDB |
3962 | goto err; |
3963 | ||
2facaad6 | 3964 | skb->dev = fwd; |
9c270af3 | 3965 | _trace_xdp_redirect(dev, xdp_prog, index); |
02671e23 | 3966 | generic_xdp_tx(skb, xdp_prog); |
f5836ca5 JDB |
3967 | return 0; |
3968 | err: | |
9c270af3 | 3969 | _trace_xdp_redirect_err(dev, xdp_prog, index, err); |
2facaad6 | 3970 | return err; |
6103aa96 | 3971 | } |
6103aa96 | 3972 | |
814abfab JF |
3973 | BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags) |
3974 | { | |
0b19cc0a | 3975 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); |
814abfab JF |
3976 | |
3977 | if (unlikely(flags)) | |
3978 | return XDP_ABORTED; | |
3979 | ||
814abfab | 3980 | ri->flags = flags; |
4b55cf29 | 3981 | ri->tgt_index = ifindex; |
43e74c02 | 3982 | ri->tgt_value = NULL; |
f6069b9a | 3983 | WRITE_ONCE(ri->map, NULL); |
e4a8e817 | 3984 | |
814abfab JF |
3985 | return XDP_REDIRECT; |
3986 | } | |
3987 | ||
3988 | static const struct bpf_func_proto bpf_xdp_redirect_proto = { | |
3989 | .func = bpf_xdp_redirect, | |
3990 | .gpl_only = false, | |
3991 | .ret_type = RET_INTEGER, | |
3992 | .arg1_type = ARG_ANYTHING, | |
3993 | .arg2_type = ARG_ANYTHING, | |
3994 | }; | |
3995 | ||
f6069b9a DB |
3996 | BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, |
3997 | u64, flags) | |
e4a8e817 | 3998 | { |
0b19cc0a | 3999 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); |
e4a8e817 | 4000 | |
43e74c02 THJ |
4001 | /* Lower bits of the flags are used as return code on lookup failure */ |
4002 | if (unlikely(flags > XDP_TX)) | |
e4a8e817 DB |
4003 | return XDP_ABORTED; |
4004 | ||
43e74c02 THJ |
4005 | ri->tgt_value = __xdp_map_lookup_elem(map, ifindex); |
4006 | if (unlikely(!ri->tgt_value)) { | |
4007 | /* If the lookup fails we want to clear out the state in the | |
4008 | * redirect_info struct completely, so that if an eBPF program | |
4009 | * performs multiple lookups, the last one always takes | |
4010 | * precedence. | |
4011 | */ | |
4012 | WRITE_ONCE(ri->map, NULL); | |
4013 | return flags; | |
4014 | } | |
4015 | ||
e4a8e817 | 4016 | ri->flags = flags; |
4b55cf29 | 4017 | ri->tgt_index = ifindex; |
f6069b9a | 4018 | WRITE_ONCE(ri->map, map); |
e4a8e817 DB |
4019 | |
4020 | return XDP_REDIRECT; | |
4021 | } | |
4022 | ||
4023 | static const struct bpf_func_proto bpf_xdp_redirect_map_proto = { | |
4024 | .func = bpf_xdp_redirect_map, | |
4025 | .gpl_only = false, | |
4026 | .ret_type = RET_INTEGER, | |
4027 | .arg1_type = ARG_CONST_MAP_PTR, | |
4028 | .arg2_type = ARG_ANYTHING, | |
4029 | .arg3_type = ARG_ANYTHING, | |
4030 | }; | |
4031 | ||
555c8a86 | 4032 | static unsigned long bpf_skb_copy(void *dst_buff, const void *skb, |
aa7145c1 | 4033 | unsigned long off, unsigned long len) |
555c8a86 | 4034 | { |
aa7145c1 | 4035 | void *ptr = skb_header_pointer(skb, off, len, dst_buff); |
555c8a86 DB |
4036 | |
4037 | if (unlikely(!ptr)) | |
4038 | return len; | |
4039 | if (ptr != dst_buff) | |
4040 | memcpy(dst_buff, ptr, len); | |
4041 | ||
4042 | return 0; | |
4043 | } | |
4044 | ||
f3694e00 DB |
4045 | BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map, |
4046 | u64, flags, void *, meta, u64, meta_size) | |
555c8a86 | 4047 | { |
555c8a86 | 4048 | u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32; |
555c8a86 DB |
4049 | |
4050 | if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) | |
4051 | return -EINVAL; | |
a7658e1a | 4052 | if (unlikely(!skb || skb_size > skb->len)) |
555c8a86 DB |
4053 | return -EFAULT; |
4054 | ||
4055 | return bpf_event_output(map, flags, meta, meta_size, skb, skb_size, | |
4056 | bpf_skb_copy); | |
4057 | } | |
4058 | ||
4059 | static const struct bpf_func_proto bpf_skb_event_output_proto = { | |
4060 | .func = bpf_skb_event_output, | |
4061 | .gpl_only = true, | |
4062 | .ret_type = RET_INTEGER, | |
4063 | .arg1_type = ARG_PTR_TO_CTX, | |
4064 | .arg2_type = ARG_CONST_MAP_PTR, | |
4065 | .arg3_type = ARG_ANYTHING, | |
39f19ebb | 4066 | .arg4_type = ARG_PTR_TO_MEM, |
1728a4f2 | 4067 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
555c8a86 DB |
4068 | }; |
4069 | ||
9436ef6e | 4070 | BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff) |
c9a0f3b8 | 4071 | |
a7658e1a AS |
4072 | const struct bpf_func_proto bpf_skb_output_proto = { |
4073 | .func = bpf_skb_event_output, | |
4074 | .gpl_only = true, | |
4075 | .ret_type = RET_INTEGER, | |
4076 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
9436ef6e | 4077 | .arg1_btf_id = &bpf_skb_output_btf_ids[0], |
a7658e1a AS |
4078 | .arg2_type = ARG_CONST_MAP_PTR, |
4079 | .arg3_type = ARG_ANYTHING, | |
4080 | .arg4_type = ARG_PTR_TO_MEM, | |
4081 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, | |
a7658e1a AS |
4082 | }; |
4083 | ||
c6c33454 DB |
4084 | static unsigned short bpf_tunnel_key_af(u64 flags) |
4085 | { | |
4086 | return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET; | |
4087 | } | |
4088 | ||
f3694e00 DB |
4089 | BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to, |
4090 | u32, size, u64, flags) | |
d3aa45ce | 4091 | { |
c6c33454 DB |
4092 | const struct ip_tunnel_info *info = skb_tunnel_info(skb); |
4093 | u8 compat[sizeof(struct bpf_tunnel_key)]; | |
074f528e DB |
4094 | void *to_orig = to; |
4095 | int err; | |
d3aa45ce | 4096 | |
074f528e DB |
4097 | if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) { |
4098 | err = -EINVAL; | |
4099 | goto err_clear; | |
4100 | } | |
4101 | if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) { | |
4102 | err = -EPROTO; | |
4103 | goto err_clear; | |
4104 | } | |
c6c33454 | 4105 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { |
074f528e | 4106 | err = -EINVAL; |
c6c33454 | 4107 | switch (size) { |
4018ab18 | 4108 | case offsetof(struct bpf_tunnel_key, tunnel_label): |
c0e760c9 | 4109 | case offsetof(struct bpf_tunnel_key, tunnel_ext): |
4018ab18 | 4110 | goto set_compat; |
c6c33454 DB |
4111 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): |
4112 | /* Fixup deprecated structure layouts here, so we have | |
4113 | * a common path later on. | |
4114 | */ | |
4115 | if (ip_tunnel_info_af(info) != AF_INET) | |
074f528e | 4116 | goto err_clear; |
4018ab18 | 4117 | set_compat: |
c6c33454 DB |
4118 | to = (struct bpf_tunnel_key *)compat; |
4119 | break; | |
4120 | default: | |
074f528e | 4121 | goto err_clear; |
c6c33454 DB |
4122 | } |
4123 | } | |
d3aa45ce AS |
4124 | |
4125 | to->tunnel_id = be64_to_cpu(info->key.tun_id); | |
c6c33454 DB |
4126 | to->tunnel_tos = info->key.tos; |
4127 | to->tunnel_ttl = info->key.ttl; | |
1fbc2e0c | 4128 | to->tunnel_ext = 0; |
c6c33454 | 4129 | |
4018ab18 | 4130 | if (flags & BPF_F_TUNINFO_IPV6) { |
c6c33454 DB |
4131 | memcpy(to->remote_ipv6, &info->key.u.ipv6.src, |
4132 | sizeof(to->remote_ipv6)); | |
4018ab18 DB |
4133 | to->tunnel_label = be32_to_cpu(info->key.label); |
4134 | } else { | |
c6c33454 | 4135 | to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); |
1fbc2e0c DB |
4136 | memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); |
4137 | to->tunnel_label = 0; | |
4018ab18 | 4138 | } |
c6c33454 DB |
4139 | |
4140 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) | |
074f528e | 4141 | memcpy(to_orig, to, size); |
d3aa45ce AS |
4142 | |
4143 | return 0; | |
074f528e DB |
4144 | err_clear: |
4145 | memset(to_orig, 0, size); | |
4146 | return err; | |
d3aa45ce AS |
4147 | } |
4148 | ||
577c50aa | 4149 | static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = { |
d3aa45ce AS |
4150 | .func = bpf_skb_get_tunnel_key, |
4151 | .gpl_only = false, | |
4152 | .ret_type = RET_INTEGER, | |
4153 | .arg1_type = ARG_PTR_TO_CTX, | |
39f19ebb AS |
4154 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
4155 | .arg3_type = ARG_CONST_SIZE, | |
d3aa45ce AS |
4156 | .arg4_type = ARG_ANYTHING, |
4157 | }; | |
4158 | ||
f3694e00 | 4159 | BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size) |
14ca0751 | 4160 | { |
14ca0751 | 4161 | const struct ip_tunnel_info *info = skb_tunnel_info(skb); |
074f528e | 4162 | int err; |
14ca0751 DB |
4163 | |
4164 | if (unlikely(!info || | |
074f528e DB |
4165 | !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) { |
4166 | err = -ENOENT; | |
4167 | goto err_clear; | |
4168 | } | |
4169 | if (unlikely(size < info->options_len)) { | |
4170 | err = -ENOMEM; | |
4171 | goto err_clear; | |
4172 | } | |
14ca0751 DB |
4173 | |
4174 | ip_tunnel_info_opts_get(to, info); | |
074f528e DB |
4175 | if (size > info->options_len) |
4176 | memset(to + info->options_len, 0, size - info->options_len); | |
14ca0751 DB |
4177 | |
4178 | return info->options_len; | |
074f528e DB |
4179 | err_clear: |
4180 | memset(to, 0, size); | |
4181 | return err; | |
14ca0751 DB |
4182 | } |
4183 | ||
4184 | static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = { | |
4185 | .func = bpf_skb_get_tunnel_opt, | |
4186 | .gpl_only = false, | |
4187 | .ret_type = RET_INTEGER, | |
4188 | .arg1_type = ARG_PTR_TO_CTX, | |
39f19ebb AS |
4189 | .arg2_type = ARG_PTR_TO_UNINIT_MEM, |
4190 | .arg3_type = ARG_CONST_SIZE, | |
14ca0751 DB |
4191 | }; |
4192 | ||
d3aa45ce AS |
4193 | static struct metadata_dst __percpu *md_dst; |
4194 | ||
f3694e00 DB |
4195 | BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb, |
4196 | const struct bpf_tunnel_key *, from, u32, size, u64, flags) | |
d3aa45ce | 4197 | { |
d3aa45ce | 4198 | struct metadata_dst *md = this_cpu_ptr(md_dst); |
c6c33454 | 4199 | u8 compat[sizeof(struct bpf_tunnel_key)]; |
d3aa45ce AS |
4200 | struct ip_tunnel_info *info; |
4201 | ||
22080870 | 4202 | if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX | |
77a5196a | 4203 | BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER))) |
d3aa45ce | 4204 | return -EINVAL; |
c6c33454 DB |
4205 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { |
4206 | switch (size) { | |
4018ab18 | 4207 | case offsetof(struct bpf_tunnel_key, tunnel_label): |
c0e760c9 | 4208 | case offsetof(struct bpf_tunnel_key, tunnel_ext): |
c6c33454 DB |
4209 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): |
4210 | /* Fixup deprecated structure layouts here, so we have | |
4211 | * a common path later on. | |
4212 | */ | |
4213 | memcpy(compat, from, size); | |
4214 | memset(compat + size, 0, sizeof(compat) - size); | |
f3694e00 | 4215 | from = (const struct bpf_tunnel_key *) compat; |
c6c33454 DB |
4216 | break; |
4217 | default: | |
4218 | return -EINVAL; | |
4219 | } | |
4220 | } | |
c0e760c9 DB |
4221 | if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) || |
4222 | from->tunnel_ext)) | |
4018ab18 | 4223 | return -EINVAL; |
d3aa45ce AS |
4224 | |
4225 | skb_dst_drop(skb); | |
4226 | dst_hold((struct dst_entry *) md); | |
4227 | skb_dst_set(skb, (struct dst_entry *) md); | |
4228 | ||
4229 | info = &md->u.tun_info; | |
5540fbf4 | 4230 | memset(info, 0, sizeof(*info)); |
d3aa45ce | 4231 | info->mode = IP_TUNNEL_INFO_TX; |
c6c33454 | 4232 | |
db3c6139 | 4233 | info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE; |
22080870 DB |
4234 | if (flags & BPF_F_DONT_FRAGMENT) |
4235 | info->key.tun_flags |= TUNNEL_DONT_FRAGMENT; | |
792f3dd6 WT |
4236 | if (flags & BPF_F_ZERO_CSUM_TX) |
4237 | info->key.tun_flags &= ~TUNNEL_CSUM; | |
77a5196a WT |
4238 | if (flags & BPF_F_SEQ_NUMBER) |
4239 | info->key.tun_flags |= TUNNEL_SEQ; | |
22080870 | 4240 | |
d3aa45ce | 4241 | info->key.tun_id = cpu_to_be64(from->tunnel_id); |
c6c33454 DB |
4242 | info->key.tos = from->tunnel_tos; |
4243 | info->key.ttl = from->tunnel_ttl; | |
4244 | ||
4245 | if (flags & BPF_F_TUNINFO_IPV6) { | |
4246 | info->mode |= IP_TUNNEL_INFO_IPV6; | |
4247 | memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, | |
4248 | sizeof(from->remote_ipv6)); | |
4018ab18 DB |
4249 | info->key.label = cpu_to_be32(from->tunnel_label) & |
4250 | IPV6_FLOWLABEL_MASK; | |
c6c33454 DB |
4251 | } else { |
4252 | info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); | |
4253 | } | |
d3aa45ce AS |
4254 | |
4255 | return 0; | |
4256 | } | |
4257 | ||
577c50aa | 4258 | static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { |
d3aa45ce AS |
4259 | .func = bpf_skb_set_tunnel_key, |
4260 | .gpl_only = false, | |
4261 | .ret_type = RET_INTEGER, | |
4262 | .arg1_type = ARG_PTR_TO_CTX, | |
39f19ebb AS |
4263 | .arg2_type = ARG_PTR_TO_MEM, |
4264 | .arg3_type = ARG_CONST_SIZE, | |
d3aa45ce AS |
4265 | .arg4_type = ARG_ANYTHING, |
4266 | }; | |
4267 | ||
f3694e00 DB |
4268 | BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb, |
4269 | const u8 *, from, u32, size) | |
14ca0751 | 4270 | { |
14ca0751 DB |
4271 | struct ip_tunnel_info *info = skb_tunnel_info(skb); |
4272 | const struct metadata_dst *md = this_cpu_ptr(md_dst); | |
4273 | ||
4274 | if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1)))) | |
4275 | return -EINVAL; | |
fca5fdf6 | 4276 | if (unlikely(size > IP_TUNNEL_OPTS_MAX)) |
14ca0751 DB |
4277 | return -ENOMEM; |
4278 | ||
256c87c1 | 4279 | ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT); |
14ca0751 DB |
4280 | |
4281 | return 0; | |
4282 | } | |
4283 | ||
4284 | static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { | |
4285 | .func = bpf_skb_set_tunnel_opt, | |
4286 | .gpl_only = false, | |
4287 | .ret_type = RET_INTEGER, | |
4288 | .arg1_type = ARG_PTR_TO_CTX, | |
39f19ebb AS |
4289 | .arg2_type = ARG_PTR_TO_MEM, |
4290 | .arg3_type = ARG_CONST_SIZE, | |
14ca0751 DB |
4291 | }; |
4292 | ||
4293 | static const struct bpf_func_proto * | |
4294 | bpf_get_skb_set_tunnel_proto(enum bpf_func_id which) | |
d3aa45ce AS |
4295 | { |
4296 | if (!md_dst) { | |
d66f2b91 JK |
4297 | struct metadata_dst __percpu *tmp; |
4298 | ||
4299 | tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX, | |
4300 | METADATA_IP_TUNNEL, | |
4301 | GFP_KERNEL); | |
4302 | if (!tmp) | |
d3aa45ce | 4303 | return NULL; |
d66f2b91 JK |
4304 | if (cmpxchg(&md_dst, NULL, tmp)) |
4305 | metadata_dst_free_percpu(tmp); | |
d3aa45ce | 4306 | } |
14ca0751 DB |
4307 | |
4308 | switch (which) { | |
4309 | case BPF_FUNC_skb_set_tunnel_key: | |
4310 | return &bpf_skb_set_tunnel_key_proto; | |
4311 | case BPF_FUNC_skb_set_tunnel_opt: | |
4312 | return &bpf_skb_set_tunnel_opt_proto; | |
4313 | default: | |
4314 | return NULL; | |
4315 | } | |
d3aa45ce AS |
4316 | } |
4317 | ||
f3694e00 DB |
4318 | BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map, |
4319 | u32, idx) | |
4a482f34 | 4320 | { |
4a482f34 MKL |
4321 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
4322 | struct cgroup *cgrp; | |
4323 | struct sock *sk; | |
4a482f34 | 4324 | |
2d48c5f9 | 4325 | sk = skb_to_full_sk(skb); |
4a482f34 MKL |
4326 | if (!sk || !sk_fullsock(sk)) |
4327 | return -ENOENT; | |
f3694e00 | 4328 | if (unlikely(idx >= array->map.max_entries)) |
4a482f34 MKL |
4329 | return -E2BIG; |
4330 | ||
f3694e00 | 4331 | cgrp = READ_ONCE(array->ptrs[idx]); |
4a482f34 MKL |
4332 | if (unlikely(!cgrp)) |
4333 | return -EAGAIN; | |
4334 | ||
54fd9c2d | 4335 | return sk_under_cgroup_hierarchy(sk, cgrp); |
4a482f34 MKL |
4336 | } |
4337 | ||
747ea55e DB |
4338 | static const struct bpf_func_proto bpf_skb_under_cgroup_proto = { |
4339 | .func = bpf_skb_under_cgroup, | |
4a482f34 MKL |
4340 | .gpl_only = false, |
4341 | .ret_type = RET_INTEGER, | |
4342 | .arg1_type = ARG_PTR_TO_CTX, | |
4343 | .arg2_type = ARG_CONST_MAP_PTR, | |
4344 | .arg3_type = ARG_ANYTHING, | |
4345 | }; | |
4a482f34 | 4346 | |
cb20b08e | 4347 | #ifdef CONFIG_SOCK_CGROUP_DATA |
f307fa2c AI |
4348 | static inline u64 __bpf_sk_cgroup_id(struct sock *sk) |
4349 | { | |
4350 | struct cgroup *cgrp; | |
4351 | ||
a5fa25ad MKL |
4352 | sk = sk_to_full_sk(sk); |
4353 | if (!sk || !sk_fullsock(sk)) | |
4354 | return 0; | |
4355 | ||
f307fa2c AI |
4356 | cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
4357 | return cgroup_id(cgrp); | |
4358 | } | |
4359 | ||
cb20b08e DB |
4360 | BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb) |
4361 | { | |
a5fa25ad | 4362 | return __bpf_sk_cgroup_id(skb->sk); |
cb20b08e DB |
4363 | } |
4364 | ||
4365 | static const struct bpf_func_proto bpf_skb_cgroup_id_proto = { | |
4366 | .func = bpf_skb_cgroup_id, | |
4367 | .gpl_only = false, | |
4368 | .ret_type = RET_INTEGER, | |
4369 | .arg1_type = ARG_PTR_TO_CTX, | |
4370 | }; | |
77236281 | 4371 | |
f307fa2c AI |
4372 | static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk, |
4373 | int ancestor_level) | |
77236281 | 4374 | { |
77236281 AI |
4375 | struct cgroup *ancestor; |
4376 | struct cgroup *cgrp; | |
4377 | ||
a5fa25ad MKL |
4378 | sk = sk_to_full_sk(sk); |
4379 | if (!sk || !sk_fullsock(sk)) | |
4380 | return 0; | |
4381 | ||
77236281 AI |
4382 | cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); |
4383 | ancestor = cgroup_ancestor(cgrp, ancestor_level); | |
4384 | if (!ancestor) | |
4385 | return 0; | |
4386 | ||
74321038 | 4387 | return cgroup_id(ancestor); |
77236281 AI |
4388 | } |
4389 | ||
f307fa2c AI |
4390 | BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int, |
4391 | ancestor_level) | |
4392 | { | |
a5fa25ad | 4393 | return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level); |
f307fa2c AI |
4394 | } |
4395 | ||
77236281 AI |
4396 | static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = { |
4397 | .func = bpf_skb_ancestor_cgroup_id, | |
4398 | .gpl_only = false, | |
4399 | .ret_type = RET_INTEGER, | |
4400 | .arg1_type = ARG_PTR_TO_CTX, | |
4401 | .arg2_type = ARG_ANYTHING, | |
4402 | }; | |
f307fa2c AI |
4403 | |
4404 | BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk) | |
4405 | { | |
4406 | return __bpf_sk_cgroup_id(sk); | |
4407 | } | |
4408 | ||
4409 | static const struct bpf_func_proto bpf_sk_cgroup_id_proto = { | |
4410 | .func = bpf_sk_cgroup_id, | |
4411 | .gpl_only = false, | |
4412 | .ret_type = RET_INTEGER, | |
a5fa25ad | 4413 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
f307fa2c AI |
4414 | }; |
4415 | ||
4416 | BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level) | |
4417 | { | |
4418 | return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level); | |
4419 | } | |
4420 | ||
4421 | static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = { | |
4422 | .func = bpf_sk_ancestor_cgroup_id, | |
4423 | .gpl_only = false, | |
4424 | .ret_type = RET_INTEGER, | |
a5fa25ad | 4425 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
f307fa2c AI |
4426 | .arg2_type = ARG_ANYTHING, |
4427 | }; | |
cb20b08e DB |
4428 | #endif |
4429 | ||
4de16969 DB |
4430 | static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff, |
4431 | unsigned long off, unsigned long len) | |
4432 | { | |
4433 | memcpy(dst_buff, src_buff + off, len); | |
4434 | return 0; | |
4435 | } | |
4436 | ||
f3694e00 DB |
4437 | BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map, |
4438 | u64, flags, void *, meta, u64, meta_size) | |
4de16969 | 4439 | { |
4de16969 | 4440 | u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32; |
4de16969 DB |
4441 | |
4442 | if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK))) | |
4443 | return -EINVAL; | |
d831ee84 EC |
4444 | if (unlikely(!xdp || |
4445 | xdp_size > (unsigned long)(xdp->data_end - xdp->data))) | |
4de16969 DB |
4446 | return -EFAULT; |
4447 | ||
9c471370 MKL |
4448 | return bpf_event_output(map, flags, meta, meta_size, xdp->data, |
4449 | xdp_size, bpf_xdp_copy); | |
4de16969 DB |
4450 | } |
4451 | ||
4452 | static const struct bpf_func_proto bpf_xdp_event_output_proto = { | |
4453 | .func = bpf_xdp_event_output, | |
4454 | .gpl_only = true, | |
4455 | .ret_type = RET_INTEGER, | |
4456 | .arg1_type = ARG_PTR_TO_CTX, | |
4457 | .arg2_type = ARG_CONST_MAP_PTR, | |
4458 | .arg3_type = ARG_ANYTHING, | |
39f19ebb | 4459 | .arg4_type = ARG_PTR_TO_MEM, |
1728a4f2 | 4460 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, |
4de16969 DB |
4461 | }; |
4462 | ||
9436ef6e | 4463 | BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff) |
c9a0f3b8 | 4464 | |
d831ee84 EC |
4465 | const struct bpf_func_proto bpf_xdp_output_proto = { |
4466 | .func = bpf_xdp_event_output, | |
4467 | .gpl_only = true, | |
4468 | .ret_type = RET_INTEGER, | |
4469 | .arg1_type = ARG_PTR_TO_BTF_ID, | |
9436ef6e | 4470 | .arg1_btf_id = &bpf_xdp_output_btf_ids[0], |
d831ee84 EC |
4471 | .arg2_type = ARG_CONST_MAP_PTR, |
4472 | .arg3_type = ARG_ANYTHING, | |
4473 | .arg4_type = ARG_PTR_TO_MEM, | |
4474 | .arg5_type = ARG_CONST_SIZE_OR_ZERO, | |
d831ee84 EC |
4475 | }; |
4476 | ||
91b8270f CF |
4477 | BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb) |
4478 | { | |
92acdc58 | 4479 | return skb->sk ? __sock_gen_cookie(skb->sk) : 0; |
91b8270f CF |
4480 | } |
4481 | ||
4482 | static const struct bpf_func_proto bpf_get_socket_cookie_proto = { | |
4483 | .func = bpf_get_socket_cookie, | |
4484 | .gpl_only = false, | |
4485 | .ret_type = RET_INTEGER, | |
4486 | .arg1_type = ARG_PTR_TO_CTX, | |
4487 | }; | |
4488 | ||
d692f113 AI |
4489 | BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx) |
4490 | { | |
92acdc58 | 4491 | return __sock_gen_cookie(ctx->sk); |
d692f113 AI |
4492 | } |
4493 | ||
4494 | static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = { | |
4495 | .func = bpf_get_socket_cookie_sock_addr, | |
4496 | .gpl_only = false, | |
4497 | .ret_type = RET_INTEGER, | |
4498 | .arg1_type = ARG_PTR_TO_CTX, | |
4499 | }; | |
4500 | ||
0e53d9e5 DB |
4501 | BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx) |
4502 | { | |
92acdc58 | 4503 | return __sock_gen_cookie(ctx); |
0e53d9e5 DB |
4504 | } |
4505 | ||
4506 | static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { | |
4507 | .func = bpf_get_socket_cookie_sock, | |
4508 | .gpl_only = false, | |
4509 | .ret_type = RET_INTEGER, | |
4510 | .arg1_type = ARG_PTR_TO_CTX, | |
4511 | }; | |
4512 | ||
d692f113 AI |
4513 | BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx) |
4514 | { | |
92acdc58 | 4515 | return __sock_gen_cookie(ctx->sk); |
d692f113 AI |
4516 | } |
4517 | ||
4518 | static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = { | |
4519 | .func = bpf_get_socket_cookie_sock_ops, | |
4520 | .gpl_only = false, | |
4521 | .ret_type = RET_INTEGER, | |
4522 | .arg1_type = ARG_PTR_TO_CTX, | |
4523 | }; | |
4524 | ||
f318903c DB |
4525 | static u64 __bpf_get_netns_cookie(struct sock *sk) |
4526 | { | |
4527 | #ifdef CONFIG_NET_NS | |
92acdc58 | 4528 | return __net_gen_cookie(sk ? sk->sk_net.net : &init_net); |
f318903c DB |
4529 | #else |
4530 | return 0; | |
4531 | #endif | |
4532 | } | |
4533 | ||
4534 | BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx) | |
4535 | { | |
4536 | return __bpf_get_netns_cookie(ctx); | |
4537 | } | |
4538 | ||
4539 | static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = { | |
4540 | .func = bpf_get_netns_cookie_sock, | |
4541 | .gpl_only = false, | |
4542 | .ret_type = RET_INTEGER, | |
4543 | .arg1_type = ARG_PTR_TO_CTX_OR_NULL, | |
4544 | }; | |
4545 | ||
4546 | BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx) | |
4547 | { | |
4548 | return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL); | |
4549 | } | |
4550 | ||
4551 | static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = { | |
4552 | .func = bpf_get_netns_cookie_sock_addr, | |
4553 | .gpl_only = false, | |
4554 | .ret_type = RET_INTEGER, | |
4555 | .arg1_type = ARG_PTR_TO_CTX_OR_NULL, | |
4556 | }; | |
4557 | ||
6acc5c29 CF |
4558 | BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb) |
4559 | { | |
4560 | struct sock *sk = sk_to_full_sk(skb->sk); | |
4561 | kuid_t kuid; | |
4562 | ||
4563 | if (!sk || !sk_fullsock(sk)) | |
4564 | return overflowuid; | |
4565 | kuid = sock_net_uid(sock_net(sk), sk); | |
4566 | return from_kuid_munged(sock_net(sk)->user_ns, kuid); | |
4567 | } | |
4568 | ||
4569 | static const struct bpf_func_proto bpf_get_socket_uid_proto = { | |
4570 | .func = bpf_get_socket_uid, | |
4571 | .gpl_only = false, | |
4572 | .ret_type = RET_INTEGER, | |
4573 | .arg1_type = ARG_PTR_TO_CTX, | |
4574 | }; | |
4575 | ||
beecf11b | 4576 | static int _bpf_setsockopt(struct sock *sk, int level, int optname, |
5cdc744c | 4577 | char *optval, int optlen) |
8c4b4c7e | 4578 | { |
70c58997 | 4579 | char devname[IFNAMSIZ]; |
f9bcf968 | 4580 | int val, valbool; |
70c58997 FF |
4581 | struct net *net; |
4582 | int ifindex; | |
8c4b4c7e | 4583 | int ret = 0; |
8c4b4c7e LB |
4584 | |
4585 | if (!sk_fullsock(sk)) | |
4586 | return -EINVAL; | |
4587 | ||
beecf11b SF |
4588 | sock_owned_by_me(sk); |
4589 | ||
8c4b4c7e | 4590 | if (level == SOL_SOCKET) { |
70c58997 | 4591 | if (optlen != sizeof(int) && optname != SO_BINDTODEVICE) |
8c4b4c7e LB |
4592 | return -EINVAL; |
4593 | val = *((int *)optval); | |
f9bcf968 | 4594 | valbool = val ? 1 : 0; |
8c4b4c7e LB |
4595 | |
4596 | /* Only some socketops are supported */ | |
4597 | switch (optname) { | |
4598 | case SO_RCVBUF: | |
c9e45767 | 4599 | val = min_t(u32, val, sysctl_rmem_max); |
8c4b4c7e | 4600 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
ebb3b78d ED |
4601 | WRITE_ONCE(sk->sk_rcvbuf, |
4602 | max_t(int, val * 2, SOCK_MIN_RCVBUF)); | |
8c4b4c7e LB |
4603 | break; |
4604 | case SO_SNDBUF: | |
c9e45767 | 4605 | val = min_t(u32, val, sysctl_wmem_max); |
8c4b4c7e | 4606 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
e292f05e ED |
4607 | WRITE_ONCE(sk->sk_sndbuf, |
4608 | max_t(int, val * 2, SOCK_MIN_SNDBUF)); | |
8c4b4c7e | 4609 | break; |
76a9ebe8 | 4610 | case SO_MAX_PACING_RATE: /* 32bit version */ |
e224c390 YC |
4611 | if (val != ~0U) |
4612 | cmpxchg(&sk->sk_pacing_status, | |
4613 | SK_PACING_NONE, | |
4614 | SK_PACING_NEEDED); | |
76a9ebe8 | 4615 | sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val; |
8c4b4c7e LB |
4616 | sk->sk_pacing_rate = min(sk->sk_pacing_rate, |
4617 | sk->sk_max_pacing_rate); | |
4618 | break; | |
4619 | case SO_PRIORITY: | |
4620 | sk->sk_priority = val; | |
4621 | break; | |
4622 | case SO_RCVLOWAT: | |
4623 | if (val < 0) | |
4624 | val = INT_MAX; | |
eac66402 | 4625 | WRITE_ONCE(sk->sk_rcvlowat, val ? : 1); |
8c4b4c7e LB |
4626 | break; |
4627 | case SO_MARK: | |
f4924f24 PO |
4628 | if (sk->sk_mark != val) { |
4629 | sk->sk_mark = val; | |
4630 | sk_dst_reset(sk); | |
4631 | } | |
8c4b4c7e | 4632 | break; |
70c58997 | 4633 | case SO_BINDTODEVICE: |
70c58997 FF |
4634 | optlen = min_t(long, optlen, IFNAMSIZ - 1); |
4635 | strncpy(devname, optval, optlen); | |
4636 | devname[optlen] = 0; | |
4637 | ||
4638 | ifindex = 0; | |
4639 | if (devname[0] != '\0') { | |
4640 | struct net_device *dev; | |
4641 | ||
4642 | ret = -ENODEV; | |
4643 | ||
4644 | net = sock_net(sk); | |
4645 | dev = dev_get_by_name(net, devname); | |
4646 | if (!dev) | |
4647 | break; | |
4648 | ifindex = dev->ifindex; | |
4649 | dev_put(dev); | |
4650 | } | |
4651 | ret = sock_bindtoindex(sk, ifindex, false); | |
70c58997 | 4652 | break; |
f9bcf968 DY |
4653 | case SO_KEEPALIVE: |
4654 | if (sk->sk_prot->keepalive) | |
4655 | sk->sk_prot->keepalive(sk, valbool); | |
4656 | sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool); | |
4657 | break; | |
8c4b4c7e LB |
4658 | default: |
4659 | ret = -EINVAL; | |
4660 | } | |
a5192c52 | 4661 | #ifdef CONFIG_INET |
6f5c39fa NS |
4662 | } else if (level == SOL_IP) { |
4663 | if (optlen != sizeof(int) || sk->sk_family != AF_INET) | |
4664 | return -EINVAL; | |
4665 | ||
4666 | val = *((int *)optval); | |
4667 | /* Only some options are supported */ | |
4668 | switch (optname) { | |
4669 | case IP_TOS: | |
4670 | if (val < -1 || val > 0xff) { | |
4671 | ret = -EINVAL; | |
4672 | } else { | |
4673 | struct inet_sock *inet = inet_sk(sk); | |
4674 | ||
4675 | if (val == -1) | |
4676 | val = 0; | |
4677 | inet->tos = val; | |
4678 | } | |
4679 | break; | |
4680 | default: | |
4681 | ret = -EINVAL; | |
4682 | } | |
6f9bd3d7 LB |
4683 | #if IS_ENABLED(CONFIG_IPV6) |
4684 | } else if (level == SOL_IPV6) { | |
4685 | if (optlen != sizeof(int) || sk->sk_family != AF_INET6) | |
4686 | return -EINVAL; | |
4687 | ||
4688 | val = *((int *)optval); | |
4689 | /* Only some options are supported */ | |
4690 | switch (optname) { | |
4691 | case IPV6_TCLASS: | |
4692 | if (val < -1 || val > 0xff) { | |
4693 | ret = -EINVAL; | |
4694 | } else { | |
4695 | struct ipv6_pinfo *np = inet6_sk(sk); | |
4696 | ||
4697 | if (val == -1) | |
4698 | val = 0; | |
4699 | np->tclass = val; | |
4700 | } | |
4701 | break; | |
4702 | default: | |
4703 | ret = -EINVAL; | |
4704 | } | |
4705 | #endif | |
8c4b4c7e LB |
4706 | } else if (level == SOL_TCP && |
4707 | sk->sk_prot->setsockopt == tcp_setsockopt) { | |
91b5b21c LB |
4708 | if (optname == TCP_CONGESTION) { |
4709 | char name[TCP_CA_NAME_MAX]; | |
4710 | ||
4711 | strncpy(name, optval, min_t(long, optlen, | |
4712 | TCP_CA_NAME_MAX-1)); | |
4713 | name[TCP_CA_NAME_MAX-1] = 0; | |
29a94932 | 4714 | ret = tcp_set_congestion_control(sk, name, false, true); |
91b5b21c | 4715 | } else { |
f9bcf968 | 4716 | struct inet_connection_sock *icsk = inet_csk(sk); |
fc747810 | 4717 | struct tcp_sock *tp = tcp_sk(sk); |
2b8ee4f0 | 4718 | unsigned long timeout; |
fc747810 LB |
4719 | |
4720 | if (optlen != sizeof(int)) | |
4721 | return -EINVAL; | |
4722 | ||
4723 | val = *((int *)optval); | |
4724 | /* Only some options are supported */ | |
4725 | switch (optname) { | |
4726 | case TCP_BPF_IW: | |
31aa6503 | 4727 | if (val <= 0 || tp->data_segs_out > tp->syn_data) |
fc747810 LB |
4728 | ret = -EINVAL; |
4729 | else | |
4730 | tp->snd_cwnd = val; | |
4731 | break; | |
13bf9641 LB |
4732 | case TCP_BPF_SNDCWND_CLAMP: |
4733 | if (val <= 0) { | |
4734 | ret = -EINVAL; | |
4735 | } else { | |
4736 | tp->snd_cwnd_clamp = val; | |
4737 | tp->snd_ssthresh = val; | |
4738 | } | |
6d3f06a0 | 4739 | break; |
2b8ee4f0 MKL |
4740 | case TCP_BPF_DELACK_MAX: |
4741 | timeout = usecs_to_jiffies(val); | |
4742 | if (timeout > TCP_DELACK_MAX || | |
4743 | timeout < TCP_TIMEOUT_MIN) | |
4744 | return -EINVAL; | |
4745 | inet_csk(sk)->icsk_delack_max = timeout; | |
4746 | break; | |
ca584ba0 MKL |
4747 | case TCP_BPF_RTO_MIN: |
4748 | timeout = usecs_to_jiffies(val); | |
4749 | if (timeout > TCP_RTO_MIN || | |
4750 | timeout < TCP_TIMEOUT_MIN) | |
4751 | return -EINVAL; | |
4752 | inet_csk(sk)->icsk_rto_min = timeout; | |
4753 | break; | |
1e215300 NS |
4754 | case TCP_SAVE_SYN: |
4755 | if (val < 0 || val > 1) | |
4756 | ret = -EINVAL; | |
4757 | else | |
4758 | tp->save_syn = val; | |
4759 | break; | |
f9bcf968 DY |
4760 | case TCP_KEEPIDLE: |
4761 | ret = tcp_sock_set_keepidle_locked(sk, val); | |
4762 | break; | |
4763 | case TCP_KEEPINTVL: | |
4764 | if (val < 1 || val > MAX_TCP_KEEPINTVL) | |
4765 | ret = -EINVAL; | |
4766 | else | |
4767 | tp->keepalive_intvl = val * HZ; | |
4768 | break; | |
4769 | case TCP_KEEPCNT: | |
4770 | if (val < 1 || val > MAX_TCP_KEEPCNT) | |
4771 | ret = -EINVAL; | |
4772 | else | |
4773 | tp->keepalive_probes = val; | |
4774 | break; | |
4775 | case TCP_SYNCNT: | |
4776 | if (val < 1 || val > MAX_TCP_SYNCNT) | |
4777 | ret = -EINVAL; | |
4778 | else | |
4779 | icsk->icsk_syn_retries = val; | |
4780 | break; | |
4781 | case TCP_USER_TIMEOUT: | |
4782 | if (val < 0) | |
4783 | ret = -EINVAL; | |
4784 | else | |
4785 | icsk->icsk_user_timeout = val; | |
4786 | break; | |
fc747810 LB |
4787 | default: |
4788 | ret = -EINVAL; | |
4789 | } | |
91b5b21c | 4790 | } |
91b5b21c | 4791 | #endif |
8c4b4c7e LB |
4792 | } else { |
4793 | ret = -EINVAL; | |
4794 | } | |
4795 | return ret; | |
4796 | } | |
4797 | ||
beecf11b SF |
4798 | static int _bpf_getsockopt(struct sock *sk, int level, int optname, |
4799 | char *optval, int optlen) | |
cd86d1fd | 4800 | { |
cd86d1fd LB |
4801 | if (!sk_fullsock(sk)) |
4802 | goto err_clear; | |
beecf11b SF |
4803 | |
4804 | sock_owned_by_me(sk); | |
4805 | ||
cd86d1fd LB |
4806 | #ifdef CONFIG_INET |
4807 | if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) { | |
1edb6e03 AR |
4808 | struct inet_connection_sock *icsk; |
4809 | struct tcp_sock *tp; | |
4810 | ||
1e215300 NS |
4811 | switch (optname) { |
4812 | case TCP_CONGESTION: | |
4813 | icsk = inet_csk(sk); | |
cd86d1fd LB |
4814 | |
4815 | if (!icsk->icsk_ca_ops || optlen <= 1) | |
4816 | goto err_clear; | |
4817 | strncpy(optval, icsk->icsk_ca_ops->name, optlen); | |
4818 | optval[optlen - 1] = 0; | |
1e215300 NS |
4819 | break; |
4820 | case TCP_SAVED_SYN: | |
4821 | tp = tcp_sk(sk); | |
4822 | ||
4823 | if (optlen <= 0 || !tp->saved_syn || | |
70a217f1 | 4824 | optlen > tcp_saved_syn_len(tp->saved_syn)) |
1e215300 | 4825 | goto err_clear; |
70a217f1 | 4826 | memcpy(optval, tp->saved_syn->data, optlen); |
1e215300 NS |
4827 | break; |
4828 | default: | |
cd86d1fd LB |
4829 | goto err_clear; |
4830 | } | |
6f5c39fa NS |
4831 | } else if (level == SOL_IP) { |
4832 | struct inet_sock *inet = inet_sk(sk); | |
4833 | ||
4834 | if (optlen != sizeof(int) || sk->sk_family != AF_INET) | |
4835 | goto err_clear; | |
4836 | ||
4837 | /* Only some options are supported */ | |
4838 | switch (optname) { | |
4839 | case IP_TOS: | |
4840 | *((int *)optval) = (int)inet->tos; | |
4841 | break; | |
4842 | default: | |
4843 | goto err_clear; | |
4844 | } | |
6f9bd3d7 LB |
4845 | #if IS_ENABLED(CONFIG_IPV6) |
4846 | } else if (level == SOL_IPV6) { | |
4847 | struct ipv6_pinfo *np = inet6_sk(sk); | |
4848 | ||
4849 | if (optlen != sizeof(int) || sk->sk_family != AF_INET6) | |
4850 | goto err_clear; | |
4851 | ||
4852 | /* Only some options are supported */ | |
4853 | switch (optname) { | |
4854 | case IPV6_TCLASS: | |
4855 | *((int *)optval) = (int)np->tclass; | |
4856 | break; | |
4857 | default: | |
4858 | goto err_clear; | |
4859 | } | |
4860 | #endif | |
cd86d1fd LB |
4861 | } else { |
4862 | goto err_clear; | |
4863 | } | |
aa2bc739 | 4864 | return 0; |
cd86d1fd LB |
4865 | #endif |
4866 | err_clear: | |
4867 | memset(optval, 0, optlen); | |
4868 | return -EINVAL; | |
4869 | } | |
4870 | ||
beecf11b SF |
4871 | BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx, |
4872 | int, level, int, optname, char *, optval, int, optlen) | |
4873 | { | |
5cdc744c | 4874 | return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen); |
beecf11b SF |
4875 | } |
4876 | ||
4877 | static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = { | |
4878 | .func = bpf_sock_addr_setsockopt, | |
4879 | .gpl_only = false, | |
4880 | .ret_type = RET_INTEGER, | |
4881 | .arg1_type = ARG_PTR_TO_CTX, | |
4882 | .arg2_type = ARG_ANYTHING, | |
4883 | .arg3_type = ARG_ANYTHING, | |
4884 | .arg4_type = ARG_PTR_TO_MEM, | |
4885 | .arg5_type = ARG_CONST_SIZE, | |
4886 | }; | |
4887 | ||
4888 | BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx, | |
4889 | int, level, int, optname, char *, optval, int, optlen) | |
4890 | { | |
4891 | return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen); | |
4892 | } | |
4893 | ||
4894 | static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = { | |
4895 | .func = bpf_sock_addr_getsockopt, | |
4896 | .gpl_only = false, | |
4897 | .ret_type = RET_INTEGER, | |
4898 | .arg1_type = ARG_PTR_TO_CTX, | |
4899 | .arg2_type = ARG_ANYTHING, | |
4900 | .arg3_type = ARG_ANYTHING, | |
4901 | .arg4_type = ARG_PTR_TO_UNINIT_MEM, | |
4902 | .arg5_type = ARG_CONST_SIZE, | |
4903 | }; | |
4904 | ||
4905 | BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, | |
4906 | int, level, int, optname, char *, optval, int, optlen) | |
4907 | { | |
5cdc744c | 4908 | return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); |
beecf11b SF |
4909 | } |
4910 | ||
4911 | static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = { | |
4912 | .func = bpf_sock_ops_setsockopt, | |
4913 | .gpl_only = false, | |
4914 | .ret_type = RET_INTEGER, | |
4915 | .arg1_type = ARG_PTR_TO_CTX, | |
4916 | .arg2_type = ARG_ANYTHING, | |
4917 | .arg3_type = ARG_ANYTHING, | |
4918 | .arg4_type = ARG_PTR_TO_MEM, | |
4919 | .arg5_type = ARG_CONST_SIZE, | |
4920 | }; | |
4921 | ||
0813a841 MKL |
4922 | static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock, |
4923 | int optname, const u8 **start) | |
4924 | { | |
4925 | struct sk_buff *syn_skb = bpf_sock->syn_skb; | |
4926 | const u8 *hdr_start; | |
4927 | int ret; | |
4928 | ||
4929 | if (syn_skb) { | |
4930 | /* sk is a request_sock here */ | |
4931 | ||
4932 | if (optname == TCP_BPF_SYN) { | |
4933 | hdr_start = syn_skb->data; | |
4934 | ret = tcp_hdrlen(syn_skb); | |
267cf9fa | 4935 | } else if (optname == TCP_BPF_SYN_IP) { |
0813a841 MKL |
4936 | hdr_start = skb_network_header(syn_skb); |
4937 | ret = skb_network_header_len(syn_skb) + | |
4938 | tcp_hdrlen(syn_skb); | |
267cf9fa MKL |
4939 | } else { |
4940 | /* optname == TCP_BPF_SYN_MAC */ | |
4941 | hdr_start = skb_mac_header(syn_skb); | |
4942 | ret = skb_mac_header_len(syn_skb) + | |
4943 | skb_network_header_len(syn_skb) + | |
4944 | tcp_hdrlen(syn_skb); | |
0813a841 MKL |
4945 | } |
4946 | } else { | |
4947 | struct sock *sk = bpf_sock->sk; | |
4948 | struct saved_syn *saved_syn; | |
4949 | ||
4950 | if (sk->sk_state == TCP_NEW_SYN_RECV) | |
4951 | /* synack retransmit. bpf_sock->syn_skb will | |
4952 | * not be available. It has to resort to | |
4953 | * saved_syn (if it is saved). | |
4954 | */ | |
4955 | saved_syn = inet_reqsk(sk)->saved_syn; | |
4956 | else | |
4957 | saved_syn = tcp_sk(sk)->saved_syn; | |
4958 | ||
4959 | if (!saved_syn) | |
4960 | return -ENOENT; | |
4961 | ||
4962 | if (optname == TCP_BPF_SYN) { | |
4963 | hdr_start = saved_syn->data + | |
267cf9fa | 4964 | saved_syn->mac_hdrlen + |
0813a841 MKL |
4965 | saved_syn->network_hdrlen; |
4966 | ret = saved_syn->tcp_hdrlen; | |
267cf9fa MKL |
4967 | } else if (optname == TCP_BPF_SYN_IP) { |
4968 | hdr_start = saved_syn->data + | |
4969 | saved_syn->mac_hdrlen; | |
4970 | ret = saved_syn->network_hdrlen + | |
4971 | saved_syn->tcp_hdrlen; | |
0813a841 | 4972 | } else { |
267cf9fa MKL |
4973 | /* optname == TCP_BPF_SYN_MAC */ |
4974 | ||
4975 | /* TCP_SAVE_SYN may not have saved the mac hdr */ | |
4976 | if (!saved_syn->mac_hdrlen) | |
4977 | return -ENOENT; | |
4978 | ||
0813a841 | 4979 | hdr_start = saved_syn->data; |
267cf9fa MKL |
4980 | ret = saved_syn->mac_hdrlen + |
4981 | saved_syn->network_hdrlen + | |
0813a841 MKL |
4982 | saved_syn->tcp_hdrlen; |
4983 | } | |
4984 | } | |
4985 | ||
4986 | *start = hdr_start; | |
4987 | return ret; | |
4988 | } | |
4989 | ||
beecf11b SF |
4990 | BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock, |
4991 | int, level, int, optname, char *, optval, int, optlen) | |
4992 | { | |
0813a841 | 4993 | if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP && |
267cf9fa | 4994 | optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) { |
0813a841 MKL |
4995 | int ret, copy_len = 0; |
4996 | const u8 *start; | |
4997 | ||
4998 | ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start); | |
4999 | if (ret > 0) { | |
5000 | copy_len = ret; | |
5001 | if (optlen < copy_len) { | |
5002 | copy_len = optlen; | |
5003 | ret = -ENOSPC; | |
5004 | } | |
5005 | ||
5006 | memcpy(optval, start, copy_len); | |
5007 | } | |
5008 | ||
5009 | /* Zero out unused buffer at the end */ | |
5010 | memset(optval + copy_len, 0, optlen - copy_len); | |
5011 | ||
5012 | return ret; | |
5013 | } | |
5014 | ||
beecf11b SF |
5015 | return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen); |
5016 | } | |
5017 | ||
5018 | static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = { | |
5019 | .func = bpf_sock_ops_getsockopt, | |
cd86d1fd LB |
5020 | .gpl_only = false, |
5021 | .ret_type = RET_INTEGER, | |
5022 | .arg1_type = ARG_PTR_TO_CTX, | |
5023 | .arg2_type = ARG_ANYTHING, | |
5024 | .arg3_type = ARG_ANYTHING, | |
5025 | .arg4_type = ARG_PTR_TO_UNINIT_MEM, | |
5026 | .arg5_type = ARG_CONST_SIZE, | |
5027 | }; | |
5028 | ||
b13d8807 LB |
5029 | BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, |
5030 | int, argval) | |
5031 | { | |
5032 | struct sock *sk = bpf_sock->sk; | |
5033 | int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS; | |
5034 | ||
a7dcdf6e | 5035 | if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk)) |
b13d8807 LB |
5036 | return -EINVAL; |
5037 | ||
725721a6 | 5038 | tcp_sk(sk)->bpf_sock_ops_cb_flags = val; |
b13d8807 LB |
5039 | |
5040 | return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS); | |
b13d8807 LB |
5041 | } |
5042 | ||
5043 | static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = { | |
5044 | .func = bpf_sock_ops_cb_flags_set, | |
5045 | .gpl_only = false, | |
5046 | .ret_type = RET_INTEGER, | |
5047 | .arg1_type = ARG_PTR_TO_CTX, | |
5048 | .arg2_type = ARG_ANYTHING, | |
5049 | }; | |
5050 | ||
d74bad4e AI |
5051 | const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly; |
5052 | EXPORT_SYMBOL_GPL(ipv6_bpf_stub); | |
5053 | ||
5054 | BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr, | |
5055 | int, addr_len) | |
5056 | { | |
5057 | #ifdef CONFIG_INET | |
5058 | struct sock *sk = ctx->sk; | |
8086fbaf | 5059 | u32 flags = BIND_FROM_BPF; |
d74bad4e AI |
5060 | int err; |
5061 | ||
d74bad4e | 5062 | err = -EINVAL; |
ba024f25 TH |
5063 | if (addr_len < offsetofend(struct sockaddr, sa_family)) |
5064 | return err; | |
d74bad4e AI |
5065 | if (addr->sa_family == AF_INET) { |
5066 | if (addr_len < sizeof(struct sockaddr_in)) | |
5067 | return err; | |
8086fbaf SF |
5068 | if (((struct sockaddr_in *)addr)->sin_port == htons(0)) |
5069 | flags |= BIND_FORCE_ADDRESS_NO_PORT; | |
5070 | return __inet_bind(sk, addr, addr_len, flags); | |
d74bad4e AI |
5071 | #if IS_ENABLED(CONFIG_IPV6) |
5072 | } else if (addr->sa_family == AF_INET6) { | |
5073 | if (addr_len < SIN6_LEN_RFC2133) | |
5074 | return err; | |
8086fbaf SF |
5075 | if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0)) |
5076 | flags |= BIND_FORCE_ADDRESS_NO_PORT; | |
d74bad4e AI |
5077 | /* ipv6_bpf_stub cannot be NULL, since it's called from |
5078 | * bpf_cgroup_inet6_connect hook and ipv6 is already loaded | |
5079 | */ | |
8086fbaf | 5080 | return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags); |
d74bad4e AI |
5081 | #endif /* CONFIG_IPV6 */ |
5082 | } | |
5083 | #endif /* CONFIG_INET */ | |
5084 | ||
5085 | return -EAFNOSUPPORT; | |
5086 | } | |
5087 | ||
5088 | static const struct bpf_func_proto bpf_bind_proto = { | |
5089 | .func = bpf_bind, | |
5090 | .gpl_only = false, | |
5091 | .ret_type = RET_INTEGER, | |
5092 | .arg1_type = ARG_PTR_TO_CTX, | |
5093 | .arg2_type = ARG_PTR_TO_MEM, | |
5094 | .arg3_type = ARG_CONST_SIZE, | |
5095 | }; | |
5096 | ||
12bed760 EB |
5097 | #ifdef CONFIG_XFRM |
5098 | BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index, | |
5099 | struct bpf_xfrm_state *, to, u32, size, u64, flags) | |
5100 | { | |
5101 | const struct sec_path *sp = skb_sec_path(skb); | |
5102 | const struct xfrm_state *x; | |
5103 | ||
5104 | if (!sp || unlikely(index >= sp->len || flags)) | |
5105 | goto err_clear; | |
5106 | ||
5107 | x = sp->xvec[index]; | |
5108 | ||
5109 | if (unlikely(size != sizeof(struct bpf_xfrm_state))) | |
5110 | goto err_clear; | |
5111 | ||
5112 | to->reqid = x->props.reqid; | |
5113 | to->spi = x->id.spi; | |
5114 | to->family = x->props.family; | |
1fbc2e0c DB |
5115 | to->ext = 0; |
5116 | ||
12bed760 EB |
5117 | if (to->family == AF_INET6) { |
5118 | memcpy(to->remote_ipv6, x->props.saddr.a6, | |
5119 | sizeof(to->remote_ipv6)); | |
5120 | } else { | |
5121 | to->remote_ipv4 = x->props.saddr.a4; | |
1fbc2e0c | 5122 | memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); |
12bed760 EB |
5123 | } |
5124 | ||
5125 | return 0; | |
5126 | err_clear: | |
5127 | memset(to, 0, size); | |
5128 | return -EINVAL; | |
5129 | } | |
5130 | ||
5131 | static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = { | |
5132 | .func = bpf_skb_get_xfrm_state, | |
5133 | .gpl_only = false, | |
5134 | .ret_type = RET_INTEGER, | |
5135 | .arg1_type = ARG_PTR_TO_CTX, | |
5136 | .arg2_type = ARG_ANYTHING, | |
5137 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, | |
5138 | .arg4_type = ARG_CONST_SIZE, | |
5139 | .arg5_type = ARG_ANYTHING, | |
5140 | }; | |
5141 | #endif | |
5142 | ||
87f5fc7e DA |
5143 | #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6) |
5144 | static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params, | |
5145 | const struct neighbour *neigh, | |
5146 | const struct net_device *dev) | |
5147 | { | |
5148 | memcpy(params->dmac, neigh->ha, ETH_ALEN); | |
5149 | memcpy(params->smac, dev->dev_addr, ETH_ALEN); | |
5150 | params->h_vlan_TCI = 0; | |
5151 | params->h_vlan_proto = 0; | |
4c79579b | 5152 | params->ifindex = dev->ifindex; |
87f5fc7e | 5153 | |
4c79579b | 5154 | return 0; |
87f5fc7e DA |
5155 | } |
5156 | #endif | |
5157 | ||
5158 | #if IS_ENABLED(CONFIG_INET) | |
5159 | static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params, | |
4f74fede | 5160 | u32 flags, bool check_mtu) |
87f5fc7e | 5161 | { |
eba618ab | 5162 | struct fib_nh_common *nhc; |
87f5fc7e DA |
5163 | struct in_device *in_dev; |
5164 | struct neighbour *neigh; | |
5165 | struct net_device *dev; | |
5166 | struct fib_result res; | |
87f5fc7e DA |
5167 | struct flowi4 fl4; |
5168 | int err; | |
4f74fede | 5169 | u32 mtu; |
87f5fc7e DA |
5170 | |
5171 | dev = dev_get_by_index_rcu(net, params->ifindex); | |
5172 | if (unlikely(!dev)) | |
5173 | return -ENODEV; | |
5174 | ||
5175 | /* verify forwarding is enabled on this interface */ | |
5176 | in_dev = __in_dev_get_rcu(dev); | |
5177 | if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev))) | |
4c79579b | 5178 | return BPF_FIB_LKUP_RET_FWD_DISABLED; |
87f5fc7e DA |
5179 | |
5180 | if (flags & BPF_FIB_LOOKUP_OUTPUT) { | |
5181 | fl4.flowi4_iif = 1; | |
5182 | fl4.flowi4_oif = params->ifindex; | |
5183 | } else { | |
5184 | fl4.flowi4_iif = params->ifindex; | |
5185 | fl4.flowi4_oif = 0; | |
5186 | } | |
5187 | fl4.flowi4_tos = params->tos & IPTOS_RT_MASK; | |
5188 | fl4.flowi4_scope = RT_SCOPE_UNIVERSE; | |
5189 | fl4.flowi4_flags = 0; | |
5190 | ||
5191 | fl4.flowi4_proto = params->l4_protocol; | |
5192 | fl4.daddr = params->ipv4_dst; | |
5193 | fl4.saddr = params->ipv4_src; | |
5194 | fl4.fl4_sport = params->sport; | |
5195 | fl4.fl4_dport = params->dport; | |
1869e226 | 5196 | fl4.flowi4_multipath_hash = 0; |
87f5fc7e DA |
5197 | |
5198 | if (flags & BPF_FIB_LOOKUP_DIRECT) { | |
5199 | u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; | |
5200 | struct fib_table *tb; | |
5201 | ||
5202 | tb = fib_get_table(net, tbid); | |
5203 | if (unlikely(!tb)) | |
4c79579b | 5204 | return BPF_FIB_LKUP_RET_NOT_FWDED; |
87f5fc7e DA |
5205 | |
5206 | err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF); | |
5207 | } else { | |
5208 | fl4.flowi4_mark = 0; | |
5209 | fl4.flowi4_secid = 0; | |
5210 | fl4.flowi4_tun_key.tun_id = 0; | |
5211 | fl4.flowi4_uid = sock_net_uid(net, NULL); | |
5212 | ||
5213 | err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF); | |
5214 | } | |
5215 | ||
4c79579b DA |
5216 | if (err) { |
5217 | /* map fib lookup errors to RTN_ type */ | |
5218 | if (err == -EINVAL) | |
5219 | return BPF_FIB_LKUP_RET_BLACKHOLE; | |
5220 | if (err == -EHOSTUNREACH) | |
5221 | return BPF_FIB_LKUP_RET_UNREACHABLE; | |
5222 | if (err == -EACCES) | |
5223 | return BPF_FIB_LKUP_RET_PROHIBIT; | |
5224 | ||
5225 | return BPF_FIB_LKUP_RET_NOT_FWDED; | |
5226 | } | |
5227 | ||
5228 | if (res.type != RTN_UNICAST) | |
5229 | return BPF_FIB_LKUP_RET_NOT_FWDED; | |
87f5fc7e | 5230 | |
5481d73f | 5231 | if (fib_info_num_path(res.fi) > 1) |
87f5fc7e DA |
5232 | fib_select_path(net, &res, &fl4, NULL); |
5233 | ||
4f74fede DA |
5234 | if (check_mtu) { |
5235 | mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst); | |
5236 | if (params->tot_len > mtu) | |
4c79579b | 5237 | return BPF_FIB_LKUP_RET_FRAG_NEEDED; |
4f74fede DA |
5238 | } |
5239 | ||
eba618ab | 5240 | nhc = res.nhc; |
87f5fc7e DA |
5241 | |
5242 | /* do not handle lwt encaps right now */ | |
eba618ab | 5243 | if (nhc->nhc_lwtstate) |
4c79579b | 5244 | return BPF_FIB_LKUP_RET_UNSUPP_LWT; |
87f5fc7e | 5245 | |
eba618ab | 5246 | dev = nhc->nhc_dev; |
87f5fc7e DA |
5247 | |
5248 | params->rt_metric = res.fi->fib_priority; | |
5249 | ||
5250 | /* xdp and cls_bpf programs are run in RCU-bh so | |
5251 | * rcu_read_lock_bh is not needed here | |
5252 | */ | |
6f5f68d0 DA |
5253 | if (likely(nhc->nhc_gw_family != AF_INET6)) { |
5254 | if (nhc->nhc_gw_family) | |
5255 | params->ipv4_dst = nhc->nhc_gw.ipv4; | |
5256 | ||
5257 | neigh = __ipv4_neigh_lookup_noref(dev, | |
5258 | (__force u32)params->ipv4_dst); | |
5259 | } else { | |
5260 | struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst; | |
5261 | ||
5262 | params->family = AF_INET6; | |
5263 | *dst = nhc->nhc_gw.ipv6; | |
5264 | neigh = __ipv6_neigh_lookup_noref_stub(dev, dst); | |
5265 | } | |
5266 | ||
4c79579b DA |
5267 | if (!neigh) |
5268 | return BPF_FIB_LKUP_RET_NO_NEIGH; | |
87f5fc7e | 5269 | |
4c79579b | 5270 | return bpf_fib_set_fwd_params(params, neigh, dev); |
87f5fc7e DA |
5271 | } |
5272 | #endif | |
5273 | ||
5274 | #if IS_ENABLED(CONFIG_IPV6) | |
5275 | static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params, | |
4f74fede | 5276 | u32 flags, bool check_mtu) |
87f5fc7e DA |
5277 | { |
5278 | struct in6_addr *src = (struct in6_addr *) params->ipv6_src; | |
5279 | struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst; | |
e55449e7 | 5280 | struct fib6_result res = {}; |
87f5fc7e DA |
5281 | struct neighbour *neigh; |
5282 | struct net_device *dev; | |
5283 | struct inet6_dev *idev; | |
87f5fc7e DA |
5284 | struct flowi6 fl6; |
5285 | int strict = 0; | |
effda4dd | 5286 | int oif, err; |
4f74fede | 5287 | u32 mtu; |
87f5fc7e DA |
5288 | |
5289 | /* link local addresses are never forwarded */ | |
5290 | if (rt6_need_strict(dst) || rt6_need_strict(src)) | |
4c79579b | 5291 | return BPF_FIB_LKUP_RET_NOT_FWDED; |
87f5fc7e DA |
5292 | |
5293 | dev = dev_get_by_index_rcu(net, params->ifindex); | |
5294 | if (unlikely(!dev)) | |
5295 | return -ENODEV; | |
5296 | ||
5297 | idev = __in6_dev_get_safely(dev); | |
56f0f84e | 5298 | if (unlikely(!idev || !idev->cnf.forwarding)) |
4c79579b | 5299 | return BPF_FIB_LKUP_RET_FWD_DISABLED; |
87f5fc7e DA |
5300 | |
5301 | if (flags & BPF_FIB_LOOKUP_OUTPUT) { | |
5302 | fl6.flowi6_iif = 1; | |
5303 | oif = fl6.flowi6_oif = params->ifindex; | |
5304 | } else { | |
5305 | oif = fl6.flowi6_iif = params->ifindex; | |
5306 | fl6.flowi6_oif = 0; | |
5307 | strict = RT6_LOOKUP_F_HAS_SADDR; | |
5308 | } | |
bd3a08aa | 5309 | fl6.flowlabel = params->flowinfo; |
87f5fc7e DA |
5310 | fl6.flowi6_scope = 0; |
5311 | fl6.flowi6_flags = 0; | |
5312 | fl6.mp_hash = 0; | |
5313 | ||
5314 | fl6.flowi6_proto = params->l4_protocol; | |
5315 | fl6.daddr = *dst; | |
5316 | fl6.saddr = *src; | |
5317 | fl6.fl6_sport = params->sport; | |
5318 | fl6.fl6_dport = params->dport; | |
5319 | ||
5320 | if (flags & BPF_FIB_LOOKUP_DIRECT) { | |
5321 | u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN; | |
5322 | struct fib6_table *tb; | |
5323 | ||
5324 | tb = ipv6_stub->fib6_get_table(net, tbid); | |
5325 | if (unlikely(!tb)) | |
4c79579b | 5326 | return BPF_FIB_LKUP_RET_NOT_FWDED; |
87f5fc7e | 5327 | |
effda4dd DA |
5328 | err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res, |
5329 | strict); | |
87f5fc7e DA |
5330 | } else { |
5331 | fl6.flowi6_mark = 0; | |
5332 | fl6.flowi6_secid = 0; | |
5333 | fl6.flowi6_tun_key.tun_id = 0; | |
5334 | fl6.flowi6_uid = sock_net_uid(net, NULL); | |
5335 | ||
effda4dd | 5336 | err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict); |
87f5fc7e DA |
5337 | } |
5338 | ||
effda4dd | 5339 | if (unlikely(err || IS_ERR_OR_NULL(res.f6i) || |
b1d40991 | 5340 | res.f6i == net->ipv6.fib6_null_entry)) |
4c79579b DA |
5341 | return BPF_FIB_LKUP_RET_NOT_FWDED; |
5342 | ||
7d21fec9 DA |
5343 | switch (res.fib6_type) { |
5344 | /* only unicast is forwarded */ | |
5345 | case RTN_UNICAST: | |
5346 | break; | |
5347 | case RTN_BLACKHOLE: | |
5348 | return BPF_FIB_LKUP_RET_BLACKHOLE; | |
5349 | case RTN_UNREACHABLE: | |
5350 | return BPF_FIB_LKUP_RET_UNREACHABLE; | |
5351 | case RTN_PROHIBIT: | |
5352 | return BPF_FIB_LKUP_RET_PROHIBIT; | |
5353 | default: | |
4c79579b | 5354 | return BPF_FIB_LKUP_RET_NOT_FWDED; |
7d21fec9 | 5355 | } |
87f5fc7e | 5356 | |
b1d40991 DA |
5357 | ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif, |
5358 | fl6.flowi6_oif != 0, NULL, strict); | |
87f5fc7e | 5359 | |
4f74fede | 5360 | if (check_mtu) { |
b748f260 | 5361 | mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src); |
4f74fede | 5362 | if (params->tot_len > mtu) |
4c79579b | 5363 | return BPF_FIB_LKUP_RET_FRAG_NEEDED; |
4f74fede DA |
5364 | } |
5365 | ||
b1d40991 | 5366 | if (res.nh->fib_nh_lws) |
4c79579b | 5367 | return BPF_FIB_LKUP_RET_UNSUPP_LWT; |
87f5fc7e | 5368 | |
b1d40991 DA |
5369 | if (res.nh->fib_nh_gw_family) |
5370 | *dst = res.nh->fib_nh_gw6; | |
87f5fc7e | 5371 | |
b1d40991 DA |
5372 | dev = res.nh->fib_nh_dev; |
5373 | params->rt_metric = res.f6i->fib6_metric; | |
87f5fc7e DA |
5374 | |
5375 | /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is | |
71df5777 | 5376 | * not needed here. |
87f5fc7e | 5377 | */ |
71df5777 | 5378 | neigh = __ipv6_neigh_lookup_noref_stub(dev, dst); |
4c79579b DA |
5379 | if (!neigh) |
5380 | return BPF_FIB_LKUP_RET_NO_NEIGH; | |
87f5fc7e | 5381 | |
4c79579b | 5382 | return bpf_fib_set_fwd_params(params, neigh, dev); |
87f5fc7e DA |
5383 | } |
5384 | #endif | |
5385 | ||
5386 | BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx, | |
5387 | struct bpf_fib_lookup *, params, int, plen, u32, flags) | |
5388 | { | |
5389 | if (plen < sizeof(*params)) | |
5390 | return -EINVAL; | |
5391 | ||
9ce64f19 DA |
5392 | if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT)) |
5393 | return -EINVAL; | |
5394 | ||
87f5fc7e DA |
5395 | switch (params->family) { |
5396 | #if IS_ENABLED(CONFIG_INET) | |
5397 | case AF_INET: | |
5398 | return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params, | |
4f74fede | 5399 | flags, true); |
87f5fc7e DA |
5400 | #endif |
5401 | #if IS_ENABLED(CONFIG_IPV6) | |
5402 | case AF_INET6: | |
5403 | return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params, | |
4f74fede | 5404 | flags, true); |
87f5fc7e DA |
5405 | #endif |
5406 | } | |
bcece5dc | 5407 | return -EAFNOSUPPORT; |
87f5fc7e DA |
5408 | } |
5409 | ||
5410 | static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = { | |
5411 | .func = bpf_xdp_fib_lookup, | |
5412 | .gpl_only = true, | |
5413 | .ret_type = RET_INTEGER, | |
5414 | .arg1_type = ARG_PTR_TO_CTX, | |
5415 | .arg2_type = ARG_PTR_TO_MEM, | |
5416 | .arg3_type = ARG_CONST_SIZE, | |
5417 | .arg4_type = ARG_ANYTHING, | |
5418 | }; | |
5419 | ||
5420 | BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb, | |
5421 | struct bpf_fib_lookup *, params, int, plen, u32, flags) | |
5422 | { | |
4f74fede | 5423 | struct net *net = dev_net(skb->dev); |
4c79579b | 5424 | int rc = -EAFNOSUPPORT; |
4f74fede | 5425 | |
87f5fc7e DA |
5426 | if (plen < sizeof(*params)) |
5427 | return -EINVAL; | |
5428 | ||
9ce64f19 DA |
5429 | if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT)) |
5430 | return -EINVAL; | |
5431 | ||
87f5fc7e DA |
5432 | switch (params->family) { |
5433 | #if IS_ENABLED(CONFIG_INET) | |
5434 | case AF_INET: | |
4c79579b | 5435 | rc = bpf_ipv4_fib_lookup(net, params, flags, false); |
4f74fede | 5436 | break; |
87f5fc7e DA |
5437 | #endif |
5438 | #if IS_ENABLED(CONFIG_IPV6) | |
5439 | case AF_INET6: | |
4c79579b | 5440 | rc = bpf_ipv6_fib_lookup(net, params, flags, false); |
4f74fede | 5441 | break; |
87f5fc7e DA |
5442 | #endif |
5443 | } | |
4f74fede | 5444 | |
4c79579b | 5445 | if (!rc) { |
4f74fede DA |
5446 | struct net_device *dev; |
5447 | ||
4c79579b | 5448 | dev = dev_get_by_index_rcu(net, params->ifindex); |
4f74fede | 5449 | if (!is_skb_forwardable(dev, skb)) |
4c79579b | 5450 | rc = BPF_FIB_LKUP_RET_FRAG_NEEDED; |
4f74fede DA |
5451 | } |
5452 | ||
4c79579b | 5453 | return rc; |
87f5fc7e DA |
5454 | } |
5455 | ||
5456 | static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { | |
5457 | .func = bpf_skb_fib_lookup, | |
5458 | .gpl_only = true, | |
5459 | .ret_type = RET_INTEGER, | |
5460 | .arg1_type = ARG_PTR_TO_CTX, | |
5461 | .arg2_type = ARG_PTR_TO_MEM, | |
5462 | .arg3_type = ARG_CONST_SIZE, | |
5463 | .arg4_type = ARG_ANYTHING, | |
5464 | }; | |
5465 | ||
fe94cc29 MX |
5466 | #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) |
5467 | static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) | |
5468 | { | |
5469 | int err; | |
5470 | struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr; | |
5471 | ||
bb986a50 | 5472 | if (!seg6_validate_srh(srh, len, false)) |
fe94cc29 MX |
5473 | return -EINVAL; |
5474 | ||
5475 | switch (type) { | |
5476 | case BPF_LWT_ENCAP_SEG6_INLINE: | |
5477 | if (skb->protocol != htons(ETH_P_IPV6)) | |
5478 | return -EBADMSG; | |
5479 | ||
5480 | err = seg6_do_srh_inline(skb, srh); | |
5481 | break; | |
5482 | case BPF_LWT_ENCAP_SEG6: | |
5483 | skb_reset_inner_headers(skb); | |
5484 | skb->encapsulation = 1; | |
5485 | err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6); | |
5486 | break; | |
5487 | default: | |
5488 | return -EINVAL; | |
5489 | } | |
5490 | ||
5491 | bpf_compute_data_pointers(skb); | |
5492 | if (err) | |
5493 | return err; | |
5494 | ||
5495 | ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); | |
5496 | skb_set_transport_header(skb, sizeof(struct ipv6hdr)); | |
5497 | ||
5498 | return seg6_lookup_nexthop(skb, NULL, 0); | |
5499 | } | |
5500 | #endif /* CONFIG_IPV6_SEG6_BPF */ | |
5501 | ||
3e0bd37c PO |
5502 | #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) |
5503 | static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, | |
5504 | bool ingress) | |
5505 | { | |
52f27877 | 5506 | return bpf_lwt_push_ip_encap(skb, hdr, len, ingress); |
3e0bd37c PO |
5507 | } |
5508 | #endif | |
5509 | ||
5510 | BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, | |
fe94cc29 MX |
5511 | u32, len) |
5512 | { | |
5513 | switch (type) { | |
5514 | #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) | |
5515 | case BPF_LWT_ENCAP_SEG6: | |
5516 | case BPF_LWT_ENCAP_SEG6_INLINE: | |
5517 | return bpf_push_seg6_encap(skb, type, hdr, len); | |
3e0bd37c PO |
5518 | #endif |
5519 | #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) | |
5520 | case BPF_LWT_ENCAP_IP: | |
5521 | return bpf_push_ip_encap(skb, hdr, len, true /* ingress */); | |
fe94cc29 MX |
5522 | #endif |
5523 | default: | |
5524 | return -EINVAL; | |
5525 | } | |
5526 | } | |
5527 | ||
3e0bd37c PO |
5528 | BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type, |
5529 | void *, hdr, u32, len) | |
5530 | { | |
5531 | switch (type) { | |
5532 | #if IS_ENABLED(CONFIG_LWTUNNEL_BPF) | |
5533 | case BPF_LWT_ENCAP_IP: | |
5534 | return bpf_push_ip_encap(skb, hdr, len, false /* egress */); | |
fe94cc29 MX |
5535 | #endif |
5536 | default: | |
5537 | return -EINVAL; | |
5538 | } | |
5539 | } | |
5540 | ||
3e0bd37c PO |
5541 | static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = { |
5542 | .func = bpf_lwt_in_push_encap, | |
5543 | .gpl_only = false, | |
5544 | .ret_type = RET_INTEGER, | |
5545 | .arg1_type = ARG_PTR_TO_CTX, | |
5546 | .arg2_type = ARG_ANYTHING, | |
5547 | .arg3_type = ARG_PTR_TO_MEM, | |
5548 | .arg4_type = ARG_CONST_SIZE | |
5549 | }; | |
5550 | ||
5551 | static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = { | |
5552 | .func = bpf_lwt_xmit_push_encap, | |
fe94cc29 MX |
5553 | .gpl_only = false, |
5554 | .ret_type = RET_INTEGER, | |
5555 | .arg1_type = ARG_PTR_TO_CTX, | |
5556 | .arg2_type = ARG_ANYTHING, | |
5557 | .arg3_type = ARG_PTR_TO_MEM, | |
5558 | .arg4_type = ARG_CONST_SIZE | |
5559 | }; | |
5560 | ||
61d76980 | 5561 | #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) |
fe94cc29 MX |
5562 | BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset, |
5563 | const void *, from, u32, len) | |
5564 | { | |
fe94cc29 MX |
5565 | struct seg6_bpf_srh_state *srh_state = |
5566 | this_cpu_ptr(&seg6_bpf_srh_states); | |
486cdf21 | 5567 | struct ipv6_sr_hdr *srh = srh_state->srh; |
fe94cc29 | 5568 | void *srh_tlvs, *srh_end, *ptr; |
fe94cc29 MX |
5569 | int srhoff = 0; |
5570 | ||
486cdf21 | 5571 | if (srh == NULL) |
fe94cc29 MX |
5572 | return -EINVAL; |
5573 | ||
fe94cc29 MX |
5574 | srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4)); |
5575 | srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen); | |
5576 | ||
5577 | ptr = skb->data + offset; | |
5578 | if (ptr >= srh_tlvs && ptr + len <= srh_end) | |
486cdf21 | 5579 | srh_state->valid = false; |
fe94cc29 MX |
5580 | else if (ptr < (void *)&srh->flags || |
5581 | ptr + len > (void *)&srh->segments) | |
5582 | return -EFAULT; | |
5583 | ||
5584 | if (unlikely(bpf_try_make_writable(skb, offset + len))) | |
5585 | return -EFAULT; | |
486cdf21 MX |
5586 | if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) |
5587 | return -EINVAL; | |
5588 | srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); | |
fe94cc29 MX |
5589 | |
5590 | memcpy(skb->data + offset, from, len); | |
5591 | return 0; | |
fe94cc29 MX |
5592 | } |
5593 | ||
5594 | static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = { | |
5595 | .func = bpf_lwt_seg6_store_bytes, | |
5596 | .gpl_only = false, | |
5597 | .ret_type = RET_INTEGER, | |
5598 | .arg1_type = ARG_PTR_TO_CTX, | |
5599 | .arg2_type = ARG_ANYTHING, | |
5600 | .arg3_type = ARG_PTR_TO_MEM, | |
5601 | .arg4_type = ARG_CONST_SIZE | |
5602 | }; | |
5603 | ||
486cdf21 | 5604 | static void bpf_update_srh_state(struct sk_buff *skb) |
fe94cc29 | 5605 | { |
fe94cc29 MX |
5606 | struct seg6_bpf_srh_state *srh_state = |
5607 | this_cpu_ptr(&seg6_bpf_srh_states); | |
fe94cc29 | 5608 | int srhoff = 0; |
fe94cc29 | 5609 | |
486cdf21 MX |
5610 | if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) { |
5611 | srh_state->srh = NULL; | |
5612 | } else { | |
5613 | srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); | |
5614 | srh_state->hdrlen = srh_state->srh->hdrlen << 3; | |
5615 | srh_state->valid = true; | |
fe94cc29 | 5616 | } |
486cdf21 MX |
5617 | } |
5618 | ||
5619 | BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb, | |
5620 | u32, action, void *, param, u32, param_len) | |
5621 | { | |
5622 | struct seg6_bpf_srh_state *srh_state = | |
5623 | this_cpu_ptr(&seg6_bpf_srh_states); | |
5624 | int hdroff = 0; | |
5625 | int err; | |
fe94cc29 MX |
5626 | |
5627 | switch (action) { | |
5628 | case SEG6_LOCAL_ACTION_END_X: | |
486cdf21 MX |
5629 | if (!seg6_bpf_has_valid_srh(skb)) |
5630 | return -EBADMSG; | |
fe94cc29 MX |
5631 | if (param_len != sizeof(struct in6_addr)) |
5632 | return -EINVAL; | |
5633 | return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0); | |
5634 | case SEG6_LOCAL_ACTION_END_T: | |
486cdf21 MX |
5635 | if (!seg6_bpf_has_valid_srh(skb)) |
5636 | return -EBADMSG; | |
fe94cc29 MX |
5637 | if (param_len != sizeof(int)) |
5638 | return -EINVAL; | |
5639 | return seg6_lookup_nexthop(skb, NULL, *(int *)param); | |
486cdf21 MX |
5640 | case SEG6_LOCAL_ACTION_END_DT6: |
5641 | if (!seg6_bpf_has_valid_srh(skb)) | |
5642 | return -EBADMSG; | |
fe94cc29 MX |
5643 | if (param_len != sizeof(int)) |
5644 | return -EINVAL; | |
486cdf21 MX |
5645 | |
5646 | if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0) | |
5647 | return -EBADMSG; | |
5648 | if (!pskb_pull(skb, hdroff)) | |
5649 | return -EBADMSG; | |
5650 | ||
5651 | skb_postpull_rcsum(skb, skb_network_header(skb), hdroff); | |
5652 | skb_reset_network_header(skb); | |
5653 | skb_reset_transport_header(skb); | |
5654 | skb->encapsulation = 0; | |
5655 | ||
5656 | bpf_compute_data_pointers(skb); | |
5657 | bpf_update_srh_state(skb); | |
fe94cc29 MX |
5658 | return seg6_lookup_nexthop(skb, NULL, *(int *)param); |
5659 | case SEG6_LOCAL_ACTION_END_B6: | |
486cdf21 MX |
5660 | if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) |
5661 | return -EBADMSG; | |
fe94cc29 MX |
5662 | err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE, |
5663 | param, param_len); | |
5664 | if (!err) | |
486cdf21 MX |
5665 | bpf_update_srh_state(skb); |
5666 | ||
fe94cc29 MX |
5667 | return err; |
5668 | case SEG6_LOCAL_ACTION_END_B6_ENCAP: | |
486cdf21 MX |
5669 | if (srh_state->srh && !seg6_bpf_has_valid_srh(skb)) |
5670 | return -EBADMSG; | |
fe94cc29 MX |
5671 | err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6, |
5672 | param, param_len); | |
5673 | if (!err) | |
486cdf21 MX |
5674 | bpf_update_srh_state(skb); |
5675 | ||
fe94cc29 MX |
5676 | return err; |
5677 | default: | |
5678 | return -EINVAL; | |
5679 | } | |
fe94cc29 MX |
5680 | } |
5681 | ||
5682 | static const struct bpf_func_proto bpf_lwt_seg6_action_proto = { | |
5683 | .func = bpf_lwt_seg6_action, | |
5684 | .gpl_only = false, | |
5685 | .ret_type = RET_INTEGER, | |
5686 | .arg1_type = ARG_PTR_TO_CTX, | |
5687 | .arg2_type = ARG_ANYTHING, | |
5688 | .arg3_type = ARG_PTR_TO_MEM, | |
5689 | .arg4_type = ARG_CONST_SIZE | |
5690 | }; | |
5691 | ||
5692 | BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset, | |
5693 | s32, len) | |
5694 | { | |
fe94cc29 MX |
5695 | struct seg6_bpf_srh_state *srh_state = |
5696 | this_cpu_ptr(&seg6_bpf_srh_states); | |
486cdf21 | 5697 | struct ipv6_sr_hdr *srh = srh_state->srh; |
fe94cc29 | 5698 | void *srh_end, *srh_tlvs, *ptr; |
fe94cc29 MX |
5699 | struct ipv6hdr *hdr; |
5700 | int srhoff = 0; | |
5701 | int ret; | |
5702 | ||
486cdf21 | 5703 | if (unlikely(srh == NULL)) |
fe94cc29 | 5704 | return -EINVAL; |
fe94cc29 MX |
5705 | |
5706 | srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) + | |
5707 | ((srh->first_segment + 1) << 4)); | |
5708 | srh_end = (void *)((unsigned char *)srh + sizeof(*srh) + | |
5709 | srh_state->hdrlen); | |
5710 | ptr = skb->data + offset; | |
5711 | ||
5712 | if (unlikely(ptr < srh_tlvs || ptr > srh_end)) | |
5713 | return -EFAULT; | |
5714 | if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end)) | |
5715 | return -EFAULT; | |
5716 | ||
5717 | if (len > 0) { | |
5718 | ret = skb_cow_head(skb, len); | |
5719 | if (unlikely(ret < 0)) | |
5720 | return ret; | |
5721 | ||
5722 | ret = bpf_skb_net_hdr_push(skb, offset, len); | |
5723 | } else { | |
5724 | ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len); | |
5725 | } | |
5726 | ||
5727 | bpf_compute_data_pointers(skb); | |
5728 | if (unlikely(ret < 0)) | |
5729 | return ret; | |
5730 | ||
5731 | hdr = (struct ipv6hdr *)skb->data; | |
5732 | hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); | |
5733 | ||
486cdf21 MX |
5734 | if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) |
5735 | return -EINVAL; | |
5736 | srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); | |
fe94cc29 | 5737 | srh_state->hdrlen += len; |
486cdf21 | 5738 | srh_state->valid = false; |
fe94cc29 | 5739 | return 0; |
fe94cc29 MX |
5740 | } |
5741 | ||
5742 | static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = { | |
5743 | .func = bpf_lwt_seg6_adjust_srh, | |
5744 | .gpl_only = false, | |
5745 | .ret_type = RET_INTEGER, | |
5746 | .arg1_type = ARG_PTR_TO_CTX, | |
5747 | .arg2_type = ARG_ANYTHING, | |
5748 | .arg3_type = ARG_ANYTHING, | |
5749 | }; | |
61d76980 | 5750 | #endif /* CONFIG_IPV6_SEG6_BPF */ |
fe94cc29 | 5751 | |
df3f94a0 AB |
5752 | #ifdef CONFIG_INET |
5753 | static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple, | |
c8123ead | 5754 | int dif, int sdif, u8 family, u8 proto) |
6acc9b43 | 5755 | { |
6acc9b43 JS |
5756 | bool refcounted = false; |
5757 | struct sock *sk = NULL; | |
5758 | ||
5759 | if (family == AF_INET) { | |
5760 | __be32 src4 = tuple->ipv4.saddr; | |
5761 | __be32 dst4 = tuple->ipv4.daddr; | |
6acc9b43 JS |
5762 | |
5763 | if (proto == IPPROTO_TCP) | |
c8123ead | 5764 | sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0, |
6acc9b43 JS |
5765 | src4, tuple->ipv4.sport, |
5766 | dst4, tuple->ipv4.dport, | |
5767 | dif, sdif, &refcounted); | |
5768 | else | |
5769 | sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport, | |
5770 | dst4, tuple->ipv4.dport, | |
c8123ead | 5771 | dif, sdif, &udp_table, NULL); |
8a615c6b | 5772 | #if IS_ENABLED(CONFIG_IPV6) |
6acc9b43 JS |
5773 | } else { |
5774 | struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr; | |
5775 | struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr; | |
6acc9b43 JS |
5776 | |
5777 | if (proto == IPPROTO_TCP) | |
c8123ead | 5778 | sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0, |
6acc9b43 | 5779 | src6, tuple->ipv6.sport, |
cac6cc2f | 5780 | dst6, ntohs(tuple->ipv6.dport), |
6acc9b43 | 5781 | dif, sdif, &refcounted); |
8a615c6b JS |
5782 | else if (likely(ipv6_bpf_stub)) |
5783 | sk = ipv6_bpf_stub->udp6_lib_lookup(net, | |
5784 | src6, tuple->ipv6.sport, | |
cac6cc2f | 5785 | dst6, tuple->ipv6.dport, |
8a615c6b | 5786 | dif, sdif, |
c8123ead | 5787 | &udp_table, NULL); |
6acc9b43 JS |
5788 | #endif |
5789 | } | |
5790 | ||
5791 | if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) { | |
5792 | WARN_ONCE(1, "Found non-RCU, unreferenced socket!"); | |
5793 | sk = NULL; | |
5794 | } | |
5795 | return sk; | |
5796 | } | |
5797 | ||
edbf8c01 | 5798 | /* bpf_skc_lookup performs the core lookup for different types of sockets, |
6acc9b43 JS |
5799 | * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE. |
5800 | * Returns the socket as an 'unsigned long' to simplify the casting in the | |
5801 | * callers to satisfy BPF_CALL declarations. | |
5802 | */ | |
edbf8c01 LB |
5803 | static struct sock * |
5804 | __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, | |
5805 | struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, | |
5806 | u64 flags) | |
6acc9b43 | 5807 | { |
6acc9b43 JS |
5808 | struct sock *sk = NULL; |
5809 | u8 family = AF_UNSPEC; | |
5810 | struct net *net; | |
c8123ead | 5811 | int sdif; |
6acc9b43 | 5812 | |
9b28ae24 LB |
5813 | if (len == sizeof(tuple->ipv4)) |
5814 | family = AF_INET; | |
5815 | else if (len == sizeof(tuple->ipv6)) | |
5816 | family = AF_INET6; | |
5817 | else | |
5818 | return NULL; | |
5819 | ||
f71c6143 JS |
5820 | if (unlikely(family == AF_UNSPEC || flags || |
5821 | !((s32)netns_id < 0 || netns_id <= S32_MAX))) | |
6acc9b43 JS |
5822 | goto out; |
5823 | ||
c8123ead NH |
5824 | if (family == AF_INET) |
5825 | sdif = inet_sdif(skb); | |
6acc9b43 | 5826 | else |
c8123ead NH |
5827 | sdif = inet6_sdif(skb); |
5828 | ||
f71c6143 JS |
5829 | if ((s32)netns_id < 0) { |
5830 | net = caller_net; | |
4cc1feeb | 5831 | sk = sk_lookup(net, tuple, ifindex, sdif, family, proto); |
f71c6143 | 5832 | } else { |
6acc9b43 JS |
5833 | net = get_net_ns_by_id(caller_net, netns_id); |
5834 | if (unlikely(!net)) | |
5835 | goto out; | |
c8123ead | 5836 | sk = sk_lookup(net, tuple, ifindex, sdif, family, proto); |
6acc9b43 | 5837 | put_net(net); |
6acc9b43 JS |
5838 | } |
5839 | ||
edbf8c01 LB |
5840 | out: |
5841 | return sk; | |
5842 | } | |
5843 | ||
5844 | static struct sock * | |
5845 | __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, | |
5846 | struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, | |
5847 | u64 flags) | |
5848 | { | |
5849 | struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, | |
5850 | ifindex, proto, netns_id, flags); | |
5851 | ||
f7355a6c | 5852 | if (sk) { |
6acc9b43 | 5853 | sk = sk_to_full_sk(sk); |
f7355a6c | 5854 | if (!sk_fullsock(sk)) { |
2e012c74 | 5855 | sock_gen_put(sk); |
f7355a6c MKL |
5856 | return NULL; |
5857 | } | |
5858 | } | |
edbf8c01 LB |
5859 | |
5860 | return sk; | |
6acc9b43 JS |
5861 | } |
5862 | ||
edbf8c01 LB |
5863 | static struct sock * |
5864 | bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, | |
5865 | u8 proto, u64 netns_id, u64 flags) | |
c8123ead NH |
5866 | { |
5867 | struct net *caller_net; | |
5868 | int ifindex; | |
5869 | ||
5870 | if (skb->dev) { | |
5871 | caller_net = dev_net(skb->dev); | |
5872 | ifindex = skb->dev->ifindex; | |
5873 | } else { | |
5874 | caller_net = sock_net(skb->sk); | |
5875 | ifindex = 0; | |
5876 | } | |
5877 | ||
edbf8c01 LB |
5878 | return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, |
5879 | netns_id, flags); | |
c8123ead NH |
5880 | } |
5881 | ||
edbf8c01 LB |
5882 | static struct sock * |
5883 | bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, | |
5884 | u8 proto, u64 netns_id, u64 flags) | |
5885 | { | |
5886 | struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id, | |
5887 | flags); | |
5888 | ||
f7355a6c | 5889 | if (sk) { |
edbf8c01 | 5890 | sk = sk_to_full_sk(sk); |
f7355a6c | 5891 | if (!sk_fullsock(sk)) { |
2e012c74 | 5892 | sock_gen_put(sk); |
f7355a6c MKL |
5893 | return NULL; |
5894 | } | |
5895 | } | |
edbf8c01 LB |
5896 | |
5897 | return sk; | |
5898 | } | |
5899 | ||
5900 | BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb, | |
5901 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) | |
5902 | { | |
5903 | return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP, | |
5904 | netns_id, flags); | |
5905 | } | |
5906 | ||
5907 | static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = { | |
5908 | .func = bpf_skc_lookup_tcp, | |
5909 | .gpl_only = false, | |
5910 | .pkt_access = true, | |
5911 | .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, | |
5912 | .arg1_type = ARG_PTR_TO_CTX, | |
5913 | .arg2_type = ARG_PTR_TO_MEM, | |
5914 | .arg3_type = ARG_CONST_SIZE, | |
5915 | .arg4_type = ARG_ANYTHING, | |
5916 | .arg5_type = ARG_ANYTHING, | |
5917 | }; | |
5918 | ||
6acc9b43 JS |
5919 | BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb, |
5920 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) | |
5921 | { | |
edbf8c01 LB |
5922 | return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, |
5923 | netns_id, flags); | |
6acc9b43 JS |
5924 | } |
5925 | ||
5926 | static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = { | |
5927 | .func = bpf_sk_lookup_tcp, | |
5928 | .gpl_only = false, | |
5929 | .pkt_access = true, | |
5930 | .ret_type = RET_PTR_TO_SOCKET_OR_NULL, | |
5931 | .arg1_type = ARG_PTR_TO_CTX, | |
5932 | .arg2_type = ARG_PTR_TO_MEM, | |
5933 | .arg3_type = ARG_CONST_SIZE, | |
5934 | .arg4_type = ARG_ANYTHING, | |
5935 | .arg5_type = ARG_ANYTHING, | |
5936 | }; | |
5937 | ||
5938 | BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb, | |
5939 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) | |
5940 | { | |
edbf8c01 LB |
5941 | return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, |
5942 | netns_id, flags); | |
6acc9b43 JS |
5943 | } |
5944 | ||
5945 | static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { | |
5946 | .func = bpf_sk_lookup_udp, | |
5947 | .gpl_only = false, | |
5948 | .pkt_access = true, | |
5949 | .ret_type = RET_PTR_TO_SOCKET_OR_NULL, | |
5950 | .arg1_type = ARG_PTR_TO_CTX, | |
5951 | .arg2_type = ARG_PTR_TO_MEM, | |
5952 | .arg3_type = ARG_CONST_SIZE, | |
5953 | .arg4_type = ARG_ANYTHING, | |
5954 | .arg5_type = ARG_ANYTHING, | |
5955 | }; | |
5956 | ||
5957 | BPF_CALL_1(bpf_sk_release, struct sock *, sk) | |
5958 | { | |
a5fa25ad | 5959 | if (sk && sk_is_refcounted(sk)) |
6acc9b43 JS |
5960 | sock_gen_put(sk); |
5961 | return 0; | |
5962 | } | |
5963 | ||
5964 | static const struct bpf_func_proto bpf_sk_release_proto = { | |
5965 | .func = bpf_sk_release, | |
5966 | .gpl_only = false, | |
5967 | .ret_type = RET_INTEGER, | |
a5fa25ad | 5968 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
6acc9b43 | 5969 | }; |
c8123ead NH |
5970 | |
5971 | BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx, | |
5972 | struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) | |
5973 | { | |
5974 | struct net *caller_net = dev_net(ctx->rxq->dev); | |
5975 | int ifindex = ctx->rxq->dev->ifindex; | |
5976 | ||
edbf8c01 LB |
5977 | return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, |
5978 | ifindex, IPPROTO_UDP, netns_id, | |
5979 | flags); | |
c8123ead NH |
5980 | } |
5981 | ||
5982 | static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = { | |
5983 | .func = bpf_xdp_sk_lookup_udp, | |
5984 | .gpl_only = false, | |
5985 | .pkt_access = true, | |
5986 | .ret_type = RET_PTR_TO_SOCKET_OR_NULL, | |
5987 | .arg1_type = ARG_PTR_TO_CTX, | |
5988 | .arg2_type = ARG_PTR_TO_MEM, | |
5989 | .arg3_type = ARG_CONST_SIZE, | |
5990 | .arg4_type = ARG_ANYTHING, | |
5991 | .arg5_type = ARG_ANYTHING, | |
5992 | }; | |
5993 | ||
edbf8c01 LB |
5994 | BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx, |
5995 | struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) | |
5996 | { | |
5997 | struct net *caller_net = dev_net(ctx->rxq->dev); | |
5998 | int ifindex = ctx->rxq->dev->ifindex; | |
5999 | ||
6000 | return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net, | |
6001 | ifindex, IPPROTO_TCP, netns_id, | |
6002 | flags); | |
6003 | } | |
6004 | ||
6005 | static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = { | |
6006 | .func = bpf_xdp_skc_lookup_tcp, | |
6007 | .gpl_only = false, | |
6008 | .pkt_access = true, | |
6009 | .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, | |
6010 | .arg1_type = ARG_PTR_TO_CTX, | |
6011 | .arg2_type = ARG_PTR_TO_MEM, | |
6012 | .arg3_type = ARG_CONST_SIZE, | |
6013 | .arg4_type = ARG_ANYTHING, | |
6014 | .arg5_type = ARG_ANYTHING, | |
6015 | }; | |
6016 | ||
c8123ead NH |
6017 | BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx, |
6018 | struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) | |
6019 | { | |
6020 | struct net *caller_net = dev_net(ctx->rxq->dev); | |
6021 | int ifindex = ctx->rxq->dev->ifindex; | |
6022 | ||
edbf8c01 LB |
6023 | return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, |
6024 | ifindex, IPPROTO_TCP, netns_id, | |
6025 | flags); | |
c8123ead NH |
6026 | } |
6027 | ||
6028 | static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = { | |
6029 | .func = bpf_xdp_sk_lookup_tcp, | |
6030 | .gpl_only = false, | |
6031 | .pkt_access = true, | |
6032 | .ret_type = RET_PTR_TO_SOCKET_OR_NULL, | |
6033 | .arg1_type = ARG_PTR_TO_CTX, | |
6034 | .arg2_type = ARG_PTR_TO_MEM, | |
6035 | .arg3_type = ARG_CONST_SIZE, | |
6036 | .arg4_type = ARG_ANYTHING, | |
6037 | .arg5_type = ARG_ANYTHING, | |
6038 | }; | |
6c49e65e | 6039 | |
edbf8c01 LB |
6040 | BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx, |
6041 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) | |
6042 | { | |
6043 | return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, | |
6044 | sock_net(ctx->sk), 0, | |
6045 | IPPROTO_TCP, netns_id, flags); | |
6046 | } | |
6047 | ||
6048 | static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = { | |
6049 | .func = bpf_sock_addr_skc_lookup_tcp, | |
6050 | .gpl_only = false, | |
6051 | .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, | |
6052 | .arg1_type = ARG_PTR_TO_CTX, | |
6053 | .arg2_type = ARG_PTR_TO_MEM, | |
6054 | .arg3_type = ARG_CONST_SIZE, | |
6055 | .arg4_type = ARG_ANYTHING, | |
6056 | .arg5_type = ARG_ANYTHING, | |
6057 | }; | |
6058 | ||
6c49e65e AI |
6059 | BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx, |
6060 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) | |
6061 | { | |
edbf8c01 LB |
6062 | return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, |
6063 | sock_net(ctx->sk), 0, IPPROTO_TCP, | |
6064 | netns_id, flags); | |
6c49e65e AI |
6065 | } |
6066 | ||
6067 | static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = { | |
6068 | .func = bpf_sock_addr_sk_lookup_tcp, | |
6069 | .gpl_only = false, | |
6070 | .ret_type = RET_PTR_TO_SOCKET_OR_NULL, | |
6071 | .arg1_type = ARG_PTR_TO_CTX, | |
6072 | .arg2_type = ARG_PTR_TO_MEM, | |
6073 | .arg3_type = ARG_CONST_SIZE, | |
6074 | .arg4_type = ARG_ANYTHING, | |
6075 | .arg5_type = ARG_ANYTHING, | |
6076 | }; | |
6077 | ||
6078 | BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx, | |
6079 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) | |
6080 | { | |
edbf8c01 LB |
6081 | return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, |
6082 | sock_net(ctx->sk), 0, IPPROTO_UDP, | |
6083 | netns_id, flags); | |
6c49e65e AI |
6084 | } |
6085 | ||
6086 | static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = { | |
6087 | .func = bpf_sock_addr_sk_lookup_udp, | |
6088 | .gpl_only = false, | |
6089 | .ret_type = RET_PTR_TO_SOCKET_OR_NULL, | |
6090 | .arg1_type = ARG_PTR_TO_CTX, | |
6091 | .arg2_type = ARG_PTR_TO_MEM, | |
6092 | .arg3_type = ARG_CONST_SIZE, | |
6093 | .arg4_type = ARG_ANYTHING, | |
6094 | .arg5_type = ARG_ANYTHING, | |
6095 | }; | |
6096 | ||
655a51e5 MKL |
6097 | bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type, |
6098 | struct bpf_insn_access_aux *info) | |
6099 | { | |
c2cb5e82 SF |
6100 | if (off < 0 || off >= offsetofend(struct bpf_tcp_sock, |
6101 | icsk_retransmits)) | |
655a51e5 MKL |
6102 | return false; |
6103 | ||
6104 | if (off % size != 0) | |
6105 | return false; | |
6106 | ||
6107 | switch (off) { | |
6108 | case offsetof(struct bpf_tcp_sock, bytes_received): | |
6109 | case offsetof(struct bpf_tcp_sock, bytes_acked): | |
6110 | return size == sizeof(__u64); | |
6111 | default: | |
6112 | return size == sizeof(__u32); | |
6113 | } | |
6114 | } | |
6115 | ||
6116 | u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, | |
6117 | const struct bpf_insn *si, | |
6118 | struct bpf_insn *insn_buf, | |
6119 | struct bpf_prog *prog, u32 *target_size) | |
6120 | { | |
6121 | struct bpf_insn *insn = insn_buf; | |
6122 | ||
6123 | #define BPF_TCP_SOCK_GET_COMMON(FIELD) \ | |
6124 | do { \ | |
c593642c PB |
6125 | BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \ |
6126 | sizeof_field(struct bpf_tcp_sock, FIELD)); \ | |
655a51e5 MKL |
6127 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\ |
6128 | si->dst_reg, si->src_reg, \ | |
6129 | offsetof(struct tcp_sock, FIELD)); \ | |
6130 | } while (0) | |
6131 | ||
c2cb5e82 SF |
6132 | #define BPF_INET_SOCK_GET_COMMON(FIELD) \ |
6133 | do { \ | |
c593642c | 6134 | BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \ |
c2cb5e82 | 6135 | FIELD) > \ |
c593642c | 6136 | sizeof_field(struct bpf_tcp_sock, FIELD)); \ |
c2cb5e82 SF |
6137 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ |
6138 | struct inet_connection_sock, \ | |
6139 | FIELD), \ | |
6140 | si->dst_reg, si->src_reg, \ | |
6141 | offsetof( \ | |
6142 | struct inet_connection_sock, \ | |
6143 | FIELD)); \ | |
6144 | } while (0) | |
6145 | ||
655a51e5 MKL |
6146 | if (insn > insn_buf) |
6147 | return insn - insn_buf; | |
6148 | ||
6149 | switch (si->off) { | |
6150 | case offsetof(struct bpf_tcp_sock, rtt_min): | |
c593642c | 6151 | BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) != |
655a51e5 MKL |
6152 | sizeof(struct minmax)); |
6153 | BUILD_BUG_ON(sizeof(struct minmax) < | |
6154 | sizeof(struct minmax_sample)); | |
6155 | ||
6156 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
6157 | offsetof(struct tcp_sock, rtt_min) + | |
6158 | offsetof(struct minmax_sample, v)); | |
6159 | break; | |
2377b81d SF |
6160 | case offsetof(struct bpf_tcp_sock, snd_cwnd): |
6161 | BPF_TCP_SOCK_GET_COMMON(snd_cwnd); | |
6162 | break; | |
6163 | case offsetof(struct bpf_tcp_sock, srtt_us): | |
6164 | BPF_TCP_SOCK_GET_COMMON(srtt_us); | |
6165 | break; | |
6166 | case offsetof(struct bpf_tcp_sock, snd_ssthresh): | |
6167 | BPF_TCP_SOCK_GET_COMMON(snd_ssthresh); | |
6168 | break; | |
6169 | case offsetof(struct bpf_tcp_sock, rcv_nxt): | |
6170 | BPF_TCP_SOCK_GET_COMMON(rcv_nxt); | |
6171 | break; | |
6172 | case offsetof(struct bpf_tcp_sock, snd_nxt): | |
6173 | BPF_TCP_SOCK_GET_COMMON(snd_nxt); | |
6174 | break; | |
6175 | case offsetof(struct bpf_tcp_sock, snd_una): | |
6176 | BPF_TCP_SOCK_GET_COMMON(snd_una); | |
6177 | break; | |
6178 | case offsetof(struct bpf_tcp_sock, mss_cache): | |
6179 | BPF_TCP_SOCK_GET_COMMON(mss_cache); | |
6180 | break; | |
6181 | case offsetof(struct bpf_tcp_sock, ecn_flags): | |
6182 | BPF_TCP_SOCK_GET_COMMON(ecn_flags); | |
6183 | break; | |
6184 | case offsetof(struct bpf_tcp_sock, rate_delivered): | |
6185 | BPF_TCP_SOCK_GET_COMMON(rate_delivered); | |
6186 | break; | |
6187 | case offsetof(struct bpf_tcp_sock, rate_interval_us): | |
6188 | BPF_TCP_SOCK_GET_COMMON(rate_interval_us); | |
6189 | break; | |
6190 | case offsetof(struct bpf_tcp_sock, packets_out): | |
6191 | BPF_TCP_SOCK_GET_COMMON(packets_out); | |
6192 | break; | |
6193 | case offsetof(struct bpf_tcp_sock, retrans_out): | |
6194 | BPF_TCP_SOCK_GET_COMMON(retrans_out); | |
6195 | break; | |
6196 | case offsetof(struct bpf_tcp_sock, total_retrans): | |
6197 | BPF_TCP_SOCK_GET_COMMON(total_retrans); | |
6198 | break; | |
6199 | case offsetof(struct bpf_tcp_sock, segs_in): | |
6200 | BPF_TCP_SOCK_GET_COMMON(segs_in); | |
6201 | break; | |
6202 | case offsetof(struct bpf_tcp_sock, data_segs_in): | |
6203 | BPF_TCP_SOCK_GET_COMMON(data_segs_in); | |
6204 | break; | |
6205 | case offsetof(struct bpf_tcp_sock, segs_out): | |
6206 | BPF_TCP_SOCK_GET_COMMON(segs_out); | |
6207 | break; | |
6208 | case offsetof(struct bpf_tcp_sock, data_segs_out): | |
6209 | BPF_TCP_SOCK_GET_COMMON(data_segs_out); | |
6210 | break; | |
6211 | case offsetof(struct bpf_tcp_sock, lost_out): | |
6212 | BPF_TCP_SOCK_GET_COMMON(lost_out); | |
6213 | break; | |
6214 | case offsetof(struct bpf_tcp_sock, sacked_out): | |
6215 | BPF_TCP_SOCK_GET_COMMON(sacked_out); | |
6216 | break; | |
6217 | case offsetof(struct bpf_tcp_sock, bytes_received): | |
6218 | BPF_TCP_SOCK_GET_COMMON(bytes_received); | |
6219 | break; | |
6220 | case offsetof(struct bpf_tcp_sock, bytes_acked): | |
6221 | BPF_TCP_SOCK_GET_COMMON(bytes_acked); | |
6222 | break; | |
0357746d SF |
6223 | case offsetof(struct bpf_tcp_sock, dsack_dups): |
6224 | BPF_TCP_SOCK_GET_COMMON(dsack_dups); | |
6225 | break; | |
6226 | case offsetof(struct bpf_tcp_sock, delivered): | |
6227 | BPF_TCP_SOCK_GET_COMMON(delivered); | |
6228 | break; | |
6229 | case offsetof(struct bpf_tcp_sock, delivered_ce): | |
6230 | BPF_TCP_SOCK_GET_COMMON(delivered_ce); | |
6231 | break; | |
c2cb5e82 SF |
6232 | case offsetof(struct bpf_tcp_sock, icsk_retransmits): |
6233 | BPF_INET_SOCK_GET_COMMON(icsk_retransmits); | |
6234 | break; | |
655a51e5 MKL |
6235 | } |
6236 | ||
6237 | return insn - insn_buf; | |
6238 | } | |
6239 | ||
6240 | BPF_CALL_1(bpf_tcp_sock, struct sock *, sk) | |
6241 | { | |
655a51e5 MKL |
6242 | if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP) |
6243 | return (unsigned long)sk; | |
6244 | ||
6245 | return (unsigned long)NULL; | |
6246 | } | |
6247 | ||
0d01da6a | 6248 | const struct bpf_func_proto bpf_tcp_sock_proto = { |
655a51e5 MKL |
6249 | .func = bpf_tcp_sock, |
6250 | .gpl_only = false, | |
6251 | .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL, | |
6252 | .arg1_type = ARG_PTR_TO_SOCK_COMMON, | |
6253 | }; | |
6254 | ||
dbafd7dd MKL |
6255 | BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk) |
6256 | { | |
6257 | sk = sk_to_full_sk(sk); | |
6258 | ||
6259 | if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE)) | |
6260 | return (unsigned long)sk; | |
6261 | ||
6262 | return (unsigned long)NULL; | |
6263 | } | |
6264 | ||
6265 | static const struct bpf_func_proto bpf_get_listener_sock_proto = { | |
6266 | .func = bpf_get_listener_sock, | |
6267 | .gpl_only = false, | |
6268 | .ret_type = RET_PTR_TO_SOCKET_OR_NULL, | |
6269 | .arg1_type = ARG_PTR_TO_SOCK_COMMON, | |
6270 | }; | |
6271 | ||
f7c917ba | 6272 | BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb) |
6273 | { | |
6274 | unsigned int iphdr_len; | |
6275 | ||
d7bf2ebe THJ |
6276 | switch (skb_protocol(skb, true)) { |
6277 | case cpu_to_be16(ETH_P_IP): | |
f7c917ba | 6278 | iphdr_len = sizeof(struct iphdr); |
d7bf2ebe THJ |
6279 | break; |
6280 | case cpu_to_be16(ETH_P_IPV6): | |
f7c917ba | 6281 | iphdr_len = sizeof(struct ipv6hdr); |
d7bf2ebe THJ |
6282 | break; |
6283 | default: | |
f7c917ba | 6284 | return 0; |
d7bf2ebe | 6285 | } |
f7c917ba | 6286 | |
6287 | if (skb_headlen(skb) < iphdr_len) | |
6288 | return 0; | |
6289 | ||
6290 | if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len)) | |
6291 | return 0; | |
6292 | ||
6293 | return INET_ECN_set_ce(skb); | |
6294 | } | |
6295 | ||
fada7fdc JL |
6296 | bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type, |
6297 | struct bpf_insn_access_aux *info) | |
6298 | { | |
6299 | if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id)) | |
6300 | return false; | |
6301 | ||
6302 | if (off % size != 0) | |
6303 | return false; | |
6304 | ||
6305 | switch (off) { | |
6306 | default: | |
6307 | return size == sizeof(__u32); | |
6308 | } | |
6309 | } | |
6310 | ||
6311 | u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type, | |
6312 | const struct bpf_insn *si, | |
6313 | struct bpf_insn *insn_buf, | |
6314 | struct bpf_prog *prog, u32 *target_size) | |
6315 | { | |
6316 | struct bpf_insn *insn = insn_buf; | |
6317 | ||
6318 | #define BPF_XDP_SOCK_GET(FIELD) \ | |
6319 | do { \ | |
c593642c PB |
6320 | BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \ |
6321 | sizeof_field(struct bpf_xdp_sock, FIELD)); \ | |
fada7fdc JL |
6322 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\ |
6323 | si->dst_reg, si->src_reg, \ | |
6324 | offsetof(struct xdp_sock, FIELD)); \ | |
6325 | } while (0) | |
6326 | ||
6327 | switch (si->off) { | |
6328 | case offsetof(struct bpf_xdp_sock, queue_id): | |
6329 | BPF_XDP_SOCK_GET(queue_id); | |
6330 | break; | |
6331 | } | |
6332 | ||
6333 | return insn - insn_buf; | |
6334 | } | |
6335 | ||
f7c917ba | 6336 | static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = { |
6337 | .func = bpf_skb_ecn_set_ce, | |
6338 | .gpl_only = false, | |
6339 | .ret_type = RET_INTEGER, | |
6340 | .arg1_type = ARG_PTR_TO_CTX, | |
6341 | }; | |
39904084 LB |
6342 | |
6343 | BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len, | |
6344 | struct tcphdr *, th, u32, th_len) | |
6345 | { | |
6346 | #ifdef CONFIG_SYN_COOKIES | |
6347 | u32 cookie; | |
6348 | int ret; | |
6349 | ||
c0df236e | 6350 | if (unlikely(!sk || th_len < sizeof(*th))) |
39904084 LB |
6351 | return -EINVAL; |
6352 | ||
6353 | /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */ | |
6354 | if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN) | |
6355 | return -EINVAL; | |
6356 | ||
6357 | if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies) | |
6358 | return -EINVAL; | |
6359 | ||
6360 | if (!th->ack || th->rst || th->syn) | |
6361 | return -ENOENT; | |
6362 | ||
6363 | if (tcp_synq_no_recent_overflow(sk)) | |
6364 | return -ENOENT; | |
6365 | ||
6366 | cookie = ntohl(th->ack_seq) - 1; | |
6367 | ||
6368 | switch (sk->sk_family) { | |
6369 | case AF_INET: | |
6370 | if (unlikely(iph_len < sizeof(struct iphdr))) | |
6371 | return -EINVAL; | |
6372 | ||
6373 | ret = __cookie_v4_check((struct iphdr *)iph, th, cookie); | |
6374 | break; | |
6375 | ||
6376 | #if IS_BUILTIN(CONFIG_IPV6) | |
6377 | case AF_INET6: | |
6378 | if (unlikely(iph_len < sizeof(struct ipv6hdr))) | |
6379 | return -EINVAL; | |
6380 | ||
6381 | ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie); | |
6382 | break; | |
6383 | #endif /* CONFIG_IPV6 */ | |
6384 | ||
6385 | default: | |
6386 | return -EPROTONOSUPPORT; | |
6387 | } | |
6388 | ||
6389 | if (ret > 0) | |
6390 | return 0; | |
6391 | ||
6392 | return -ENOENT; | |
6393 | #else | |
6394 | return -ENOTSUPP; | |
6395 | #endif | |
6396 | } | |
6397 | ||
6398 | static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = { | |
6399 | .func = bpf_tcp_check_syncookie, | |
6400 | .gpl_only = true, | |
6401 | .pkt_access = true, | |
6402 | .ret_type = RET_INTEGER, | |
c0df236e | 6403 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
39904084 LB |
6404 | .arg2_type = ARG_PTR_TO_MEM, |
6405 | .arg3_type = ARG_CONST_SIZE, | |
6406 | .arg4_type = ARG_PTR_TO_MEM, | |
6407 | .arg5_type = ARG_CONST_SIZE, | |
6408 | }; | |
6409 | ||
70d66244 PP |
6410 | BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len, |
6411 | struct tcphdr *, th, u32, th_len) | |
6412 | { | |
6413 | #ifdef CONFIG_SYN_COOKIES | |
6414 | u32 cookie; | |
6415 | u16 mss; | |
6416 | ||
c0df236e | 6417 | if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4)) |
70d66244 PP |
6418 | return -EINVAL; |
6419 | ||
6420 | if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN) | |
6421 | return -EINVAL; | |
6422 | ||
6423 | if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies) | |
6424 | return -ENOENT; | |
6425 | ||
6426 | if (!th->syn || th->ack || th->fin || th->rst) | |
6427 | return -EINVAL; | |
6428 | ||
6429 | if (unlikely(iph_len < sizeof(struct iphdr))) | |
6430 | return -EINVAL; | |
6431 | ||
6432 | /* Both struct iphdr and struct ipv6hdr have the version field at the | |
6433 | * same offset so we can cast to the shorter header (struct iphdr). | |
6434 | */ | |
6435 | switch (((struct iphdr *)iph)->version) { | |
6436 | case 4: | |
6437 | if (sk->sk_family == AF_INET6 && sk->sk_ipv6only) | |
6438 | return -EINVAL; | |
6439 | ||
6440 | mss = tcp_v4_get_syncookie(sk, iph, th, &cookie); | |
6441 | break; | |
6442 | ||
6443 | #if IS_BUILTIN(CONFIG_IPV6) | |
6444 | case 6: | |
6445 | if (unlikely(iph_len < sizeof(struct ipv6hdr))) | |
6446 | return -EINVAL; | |
6447 | ||
6448 | if (sk->sk_family != AF_INET6) | |
6449 | return -EINVAL; | |
6450 | ||
6451 | mss = tcp_v6_get_syncookie(sk, iph, th, &cookie); | |
6452 | break; | |
6453 | #endif /* CONFIG_IPV6 */ | |
6454 | ||
6455 | default: | |
6456 | return -EPROTONOSUPPORT; | |
6457 | } | |
0741be35 | 6458 | if (mss == 0) |
70d66244 PP |
6459 | return -ENOENT; |
6460 | ||
6461 | return cookie | ((u64)mss << 32); | |
6462 | #else | |
6463 | return -EOPNOTSUPP; | |
6464 | #endif /* CONFIG_SYN_COOKIES */ | |
6465 | } | |
6466 | ||
6467 | static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = { | |
6468 | .func = bpf_tcp_gen_syncookie, | |
6469 | .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */ | |
6470 | .pkt_access = true, | |
6471 | .ret_type = RET_INTEGER, | |
c0df236e | 6472 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
70d66244 PP |
6473 | .arg2_type = ARG_PTR_TO_MEM, |
6474 | .arg3_type = ARG_CONST_SIZE, | |
6475 | .arg4_type = ARG_PTR_TO_MEM, | |
6476 | .arg5_type = ARG_CONST_SIZE, | |
6477 | }; | |
6478 | ||
cf7fbe66 JS |
6479 | BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags) |
6480 | { | |
27e5203b | 6481 | if (!sk || flags != 0) |
cf7fbe66 JS |
6482 | return -EINVAL; |
6483 | if (!skb_at_tc_ingress(skb)) | |
6484 | return -EOPNOTSUPP; | |
6485 | if (unlikely(dev_net(skb->dev) != sock_net(sk))) | |
6486 | return -ENETUNREACH; | |
8e368dc7 | 6487 | if (unlikely(sk_fullsock(sk) && sk->sk_reuseport)) |
cf7fbe66 | 6488 | return -ESOCKTNOSUPPORT; |
7ae215d2 JS |
6489 | if (sk_is_refcounted(sk) && |
6490 | unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) | |
cf7fbe66 JS |
6491 | return -ENOENT; |
6492 | ||
6493 | skb_orphan(skb); | |
6494 | skb->sk = sk; | |
6495 | skb->destructor = sock_pfree; | |
6496 | ||
6497 | return 0; | |
6498 | } | |
6499 | ||
6500 | static const struct bpf_func_proto bpf_sk_assign_proto = { | |
6501 | .func = bpf_sk_assign, | |
6502 | .gpl_only = false, | |
6503 | .ret_type = RET_INTEGER, | |
6504 | .arg1_type = ARG_PTR_TO_CTX, | |
27e5203b | 6505 | .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
cf7fbe66 JS |
6506 | .arg3_type = ARG_ANYTHING, |
6507 | }; | |
6508 | ||
0813a841 MKL |
6509 | static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend, |
6510 | u8 search_kind, const u8 *magic, | |
6511 | u8 magic_len, bool *eol) | |
6512 | { | |
6513 | u8 kind, kind_len; | |
6514 | ||
6515 | *eol = false; | |
6516 | ||
6517 | while (op < opend) { | |
6518 | kind = op[0]; | |
6519 | ||
6520 | if (kind == TCPOPT_EOL) { | |
6521 | *eol = true; | |
6522 | return ERR_PTR(-ENOMSG); | |
6523 | } else if (kind == TCPOPT_NOP) { | |
6524 | op++; | |
6525 | continue; | |
6526 | } | |
6527 | ||
6528 | if (opend - op < 2 || opend - op < op[1] || op[1] < 2) | |
6529 | /* Something is wrong in the received header. | |
6530 | * Follow the TCP stack's tcp_parse_options() | |
6531 | * and just bail here. | |
6532 | */ | |
6533 | return ERR_PTR(-EFAULT); | |
6534 | ||
6535 | kind_len = op[1]; | |
6536 | if (search_kind == kind) { | |
6537 | if (!magic_len) | |
6538 | return op; | |
6539 | ||
6540 | if (magic_len > kind_len - 2) | |
6541 | return ERR_PTR(-ENOMSG); | |
6542 | ||
6543 | if (!memcmp(&op[2], magic, magic_len)) | |
6544 | return op; | |
6545 | } | |
6546 | ||
6547 | op += kind_len; | |
6548 | } | |
6549 | ||
6550 | return ERR_PTR(-ENOMSG); | |
6551 | } | |
6552 | ||
6553 | BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, | |
6554 | void *, search_res, u32, len, u64, flags) | |
6555 | { | |
6556 | bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN; | |
6557 | const u8 *op, *opend, *magic, *search = search_res; | |
6558 | u8 search_kind, search_len, copy_len, magic_len; | |
6559 | int ret; | |
6560 | ||
6561 | /* 2 byte is the minimal option len except TCPOPT_NOP and | |
6562 | * TCPOPT_EOL which are useless for the bpf prog to learn | |
6563 | * and this helper disallow loading them also. | |
6564 | */ | |
6565 | if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN) | |
6566 | return -EINVAL; | |
6567 | ||
6568 | search_kind = search[0]; | |
6569 | search_len = search[1]; | |
6570 | ||
6571 | if (search_len > len || search_kind == TCPOPT_NOP || | |
6572 | search_kind == TCPOPT_EOL) | |
6573 | return -EINVAL; | |
6574 | ||
6575 | if (search_kind == TCPOPT_EXP || search_kind == 253) { | |
6576 | /* 16 or 32 bit magic. +2 for kind and kind length */ | |
6577 | if (search_len != 4 && search_len != 6) | |
6578 | return -EINVAL; | |
6579 | magic = &search[2]; | |
6580 | magic_len = search_len - 2; | |
6581 | } else { | |
6582 | if (search_len) | |
6583 | return -EINVAL; | |
6584 | magic = NULL; | |
6585 | magic_len = 0; | |
6586 | } | |
6587 | ||
6588 | if (load_syn) { | |
6589 | ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op); | |
6590 | if (ret < 0) | |
6591 | return ret; | |
6592 | ||
6593 | opend = op + ret; | |
6594 | op += sizeof(struct tcphdr); | |
6595 | } else { | |
6596 | if (!bpf_sock->skb || | |
6597 | bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB) | |
6598 | /* This bpf_sock->op cannot call this helper */ | |
6599 | return -EPERM; | |
6600 | ||
6601 | opend = bpf_sock->skb_data_end; | |
6602 | op = bpf_sock->skb->data + sizeof(struct tcphdr); | |
6603 | } | |
6604 | ||
6605 | op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len, | |
6606 | &eol); | |
6607 | if (IS_ERR(op)) | |
6608 | return PTR_ERR(op); | |
6609 | ||
6610 | copy_len = op[1]; | |
6611 | ret = copy_len; | |
6612 | if (copy_len > len) { | |
6613 | ret = -ENOSPC; | |
6614 | copy_len = len; | |
6615 | } | |
6616 | ||
6617 | memcpy(search_res, op, copy_len); | |
6618 | return ret; | |
6619 | } | |
6620 | ||
6621 | static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = { | |
6622 | .func = bpf_sock_ops_load_hdr_opt, | |
6623 | .gpl_only = false, | |
6624 | .ret_type = RET_INTEGER, | |
6625 | .arg1_type = ARG_PTR_TO_CTX, | |
6626 | .arg2_type = ARG_PTR_TO_MEM, | |
6627 | .arg3_type = ARG_CONST_SIZE, | |
6628 | .arg4_type = ARG_ANYTHING, | |
6629 | }; | |
6630 | ||
6631 | BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, | |
6632 | const void *, from, u32, len, u64, flags) | |
6633 | { | |
6634 | u8 new_kind, new_kind_len, magic_len = 0, *opend; | |
6635 | const u8 *op, *new_op, *magic = NULL; | |
6636 | struct sk_buff *skb; | |
6637 | bool eol; | |
6638 | ||
6639 | if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB) | |
6640 | return -EPERM; | |
6641 | ||
6642 | if (len < 2 || flags) | |
6643 | return -EINVAL; | |
6644 | ||
6645 | new_op = from; | |
6646 | new_kind = new_op[0]; | |
6647 | new_kind_len = new_op[1]; | |
6648 | ||
6649 | if (new_kind_len > len || new_kind == TCPOPT_NOP || | |
6650 | new_kind == TCPOPT_EOL) | |
6651 | return -EINVAL; | |
6652 | ||
6653 | if (new_kind_len > bpf_sock->remaining_opt_len) | |
6654 | return -ENOSPC; | |
6655 | ||
6656 | /* 253 is another experimental kind */ | |
6657 | if (new_kind == TCPOPT_EXP || new_kind == 253) { | |
6658 | if (new_kind_len < 4) | |
6659 | return -EINVAL; | |
6660 | /* Match for the 2 byte magic also. | |
6661 | * RFC 6994: the magic could be 2 or 4 bytes. | |
6662 | * Hence, matching by 2 byte only is on the | |
6663 | * conservative side but it is the right | |
6664 | * thing to do for the 'search-for-duplication' | |
6665 | * purpose. | |
6666 | */ | |
6667 | magic = &new_op[2]; | |
6668 | magic_len = 2; | |
6669 | } | |
6670 | ||
6671 | /* Check for duplication */ | |
6672 | skb = bpf_sock->skb; | |
6673 | op = skb->data + sizeof(struct tcphdr); | |
6674 | opend = bpf_sock->skb_data_end; | |
6675 | ||
6676 | op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len, | |
6677 | &eol); | |
6678 | if (!IS_ERR(op)) | |
6679 | return -EEXIST; | |
6680 | ||
6681 | if (PTR_ERR(op) != -ENOMSG) | |
6682 | return PTR_ERR(op); | |
6683 | ||
6684 | if (eol) | |
6685 | /* The option has been ended. Treat it as no more | |
6686 | * header option can be written. | |
6687 | */ | |
6688 | return -ENOSPC; | |
6689 | ||
6690 | /* No duplication found. Store the header option. */ | |
6691 | memcpy(opend, from, new_kind_len); | |
6692 | ||
6693 | bpf_sock->remaining_opt_len -= new_kind_len; | |
6694 | bpf_sock->skb_data_end += new_kind_len; | |
6695 | ||
6696 | return 0; | |
6697 | } | |
6698 | ||
6699 | static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = { | |
6700 | .func = bpf_sock_ops_store_hdr_opt, | |
6701 | .gpl_only = false, | |
6702 | .ret_type = RET_INTEGER, | |
6703 | .arg1_type = ARG_PTR_TO_CTX, | |
6704 | .arg2_type = ARG_PTR_TO_MEM, | |
6705 | .arg3_type = ARG_CONST_SIZE, | |
6706 | .arg4_type = ARG_ANYTHING, | |
6707 | }; | |
6708 | ||
6709 | BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock, | |
6710 | u32, len, u64, flags) | |
6711 | { | |
6712 | if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB) | |
6713 | return -EPERM; | |
6714 | ||
6715 | if (flags || len < 2) | |
6716 | return -EINVAL; | |
6717 | ||
6718 | if (len > bpf_sock->remaining_opt_len) | |
6719 | return -ENOSPC; | |
6720 | ||
6721 | bpf_sock->remaining_opt_len -= len; | |
6722 | ||
6723 | return 0; | |
6724 | } | |
6725 | ||
6726 | static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = { | |
6727 | .func = bpf_sock_ops_reserve_hdr_opt, | |
6728 | .gpl_only = false, | |
6729 | .ret_type = RET_INTEGER, | |
6730 | .arg1_type = ARG_PTR_TO_CTX, | |
6731 | .arg2_type = ARG_ANYTHING, | |
6732 | .arg3_type = ARG_ANYTHING, | |
6733 | }; | |
6734 | ||
df3f94a0 | 6735 | #endif /* CONFIG_INET */ |
6acc9b43 | 6736 | |
fe94cc29 MX |
6737 | bool bpf_helper_changes_pkt_data(void *func) |
6738 | { | |
6739 | if (func == bpf_skb_vlan_push || | |
6740 | func == bpf_skb_vlan_pop || | |
6741 | func == bpf_skb_store_bytes || | |
6742 | func == bpf_skb_change_proto || | |
6743 | func == bpf_skb_change_head || | |
0ea488ff | 6744 | func == sk_skb_change_head || |
fe94cc29 | 6745 | func == bpf_skb_change_tail || |
0ea488ff | 6746 | func == sk_skb_change_tail || |
fe94cc29 MX |
6747 | func == bpf_skb_adjust_room || |
6748 | func == bpf_skb_pull_data || | |
0ea488ff | 6749 | func == sk_skb_pull_data || |
fe94cc29 MX |
6750 | func == bpf_clone_redirect || |
6751 | func == bpf_l3_csum_replace || | |
6752 | func == bpf_l4_csum_replace || | |
6753 | func == bpf_xdp_adjust_head || | |
6754 | func == bpf_xdp_adjust_meta || | |
6755 | func == bpf_msg_pull_data || | |
6fff607e | 6756 | func == bpf_msg_push_data || |
7246d8ed | 6757 | func == bpf_msg_pop_data || |
fe94cc29 | 6758 | func == bpf_xdp_adjust_tail || |
61d76980 | 6759 | #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) |
fe94cc29 MX |
6760 | func == bpf_lwt_seg6_store_bytes || |
6761 | func == bpf_lwt_seg6_adjust_srh || | |
61d76980 | 6762 | func == bpf_lwt_seg6_action || |
0813a841 MKL |
6763 | #endif |
6764 | #ifdef CONFIG_INET | |
6765 | func == bpf_sock_ops_store_hdr_opt || | |
61d76980 | 6766 | #endif |
3e0bd37c PO |
6767 | func == bpf_lwt_in_push_encap || |
6768 | func == bpf_lwt_xmit_push_encap) | |
fe94cc29 MX |
6769 | return true; |
6770 | ||
6771 | return false; | |
6772 | } | |
6773 | ||
6890896b | 6774 | const struct bpf_func_proto bpf_event_output_data_proto __weak; |
f7c6cb1d | 6775 | const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak; |
89aa0758 | 6776 | |
ae2cf1c4 | 6777 | static const struct bpf_func_proto * |
5e43f899 | 6778 | sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
ae2cf1c4 DA |
6779 | { |
6780 | switch (func_id) { | |
6781 | /* inet and inet6 sockets are created in a process | |
6782 | * context so there is always a valid uid/gid | |
6783 | */ | |
6784 | case BPF_FUNC_get_current_uid_gid: | |
6785 | return &bpf_get_current_uid_gid_proto; | |
cd339431 RG |
6786 | case BPF_FUNC_get_local_storage: |
6787 | return &bpf_get_local_storage_proto; | |
0e53d9e5 DB |
6788 | case BPF_FUNC_get_socket_cookie: |
6789 | return &bpf_get_socket_cookie_sock_proto; | |
f318903c DB |
6790 | case BPF_FUNC_get_netns_cookie: |
6791 | return &bpf_get_netns_cookie_sock_proto; | |
fcf752ea DB |
6792 | case BPF_FUNC_perf_event_output: |
6793 | return &bpf_event_output_data_proto; | |
834ebca8 DB |
6794 | case BPF_FUNC_get_current_pid_tgid: |
6795 | return &bpf_get_current_pid_tgid_proto; | |
6796 | case BPF_FUNC_get_current_comm: | |
6797 | return &bpf_get_current_comm_proto; | |
0f09abd1 DB |
6798 | #ifdef CONFIG_CGROUPS |
6799 | case BPF_FUNC_get_current_cgroup_id: | |
6800 | return &bpf_get_current_cgroup_id_proto; | |
6801 | case BPF_FUNC_get_current_ancestor_cgroup_id: | |
6802 | return &bpf_get_current_ancestor_cgroup_id_proto; | |
6803 | #endif | |
5a52ae4e DB |
6804 | #ifdef CONFIG_CGROUP_NET_CLASSID |
6805 | case BPF_FUNC_get_cgroup_classid: | |
6806 | return &bpf_get_cgroup_classid_curr_proto; | |
6807 | #endif | |
f7c6cb1d SF |
6808 | case BPF_FUNC_sk_storage_get: |
6809 | return &bpf_sk_storage_get_cg_sock_proto; | |
ae2cf1c4 DA |
6810 | default: |
6811 | return bpf_base_func_proto(func_id); | |
6812 | } | |
6813 | } | |
6814 | ||
4fbac77d AI |
6815 | static const struct bpf_func_proto * |
6816 | sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
6817 | { | |
6818 | switch (func_id) { | |
6819 | /* inet and inet6 sockets are created in a process | |
6820 | * context so there is always a valid uid/gid | |
6821 | */ | |
6822 | case BPF_FUNC_get_current_uid_gid: | |
6823 | return &bpf_get_current_uid_gid_proto; | |
d74bad4e AI |
6824 | case BPF_FUNC_bind: |
6825 | switch (prog->expected_attach_type) { | |
6826 | case BPF_CGROUP_INET4_CONNECT: | |
6827 | case BPF_CGROUP_INET6_CONNECT: | |
6828 | return &bpf_bind_proto; | |
6829 | default: | |
6830 | return NULL; | |
6831 | } | |
d692f113 AI |
6832 | case BPF_FUNC_get_socket_cookie: |
6833 | return &bpf_get_socket_cookie_sock_addr_proto; | |
f318903c DB |
6834 | case BPF_FUNC_get_netns_cookie: |
6835 | return &bpf_get_netns_cookie_sock_addr_proto; | |
cd339431 RG |
6836 | case BPF_FUNC_get_local_storage: |
6837 | return &bpf_get_local_storage_proto; | |
fcf752ea DB |
6838 | case BPF_FUNC_perf_event_output: |
6839 | return &bpf_event_output_data_proto; | |
834ebca8 DB |
6840 | case BPF_FUNC_get_current_pid_tgid: |
6841 | return &bpf_get_current_pid_tgid_proto; | |
6842 | case BPF_FUNC_get_current_comm: | |
6843 | return &bpf_get_current_comm_proto; | |
0f09abd1 DB |
6844 | #ifdef CONFIG_CGROUPS |
6845 | case BPF_FUNC_get_current_cgroup_id: | |
6846 | return &bpf_get_current_cgroup_id_proto; | |
6847 | case BPF_FUNC_get_current_ancestor_cgroup_id: | |
6848 | return &bpf_get_current_ancestor_cgroup_id_proto; | |
6849 | #endif | |
5a52ae4e DB |
6850 | #ifdef CONFIG_CGROUP_NET_CLASSID |
6851 | case BPF_FUNC_get_cgroup_classid: | |
6852 | return &bpf_get_cgroup_classid_curr_proto; | |
6853 | #endif | |
6c49e65e AI |
6854 | #ifdef CONFIG_INET |
6855 | case BPF_FUNC_sk_lookup_tcp: | |
6856 | return &bpf_sock_addr_sk_lookup_tcp_proto; | |
6857 | case BPF_FUNC_sk_lookup_udp: | |
6858 | return &bpf_sock_addr_sk_lookup_udp_proto; | |
6859 | case BPF_FUNC_sk_release: | |
6860 | return &bpf_sk_release_proto; | |
edbf8c01 LB |
6861 | case BPF_FUNC_skc_lookup_tcp: |
6862 | return &bpf_sock_addr_skc_lookup_tcp_proto; | |
6c49e65e | 6863 | #endif /* CONFIG_INET */ |
fb85c4a7 SF |
6864 | case BPF_FUNC_sk_storage_get: |
6865 | return &bpf_sk_storage_get_proto; | |
6866 | case BPF_FUNC_sk_storage_delete: | |
6867 | return &bpf_sk_storage_delete_proto; | |
beecf11b SF |
6868 | case BPF_FUNC_setsockopt: |
6869 | switch (prog->expected_attach_type) { | |
6870 | case BPF_CGROUP_INET4_CONNECT: | |
6871 | case BPF_CGROUP_INET6_CONNECT: | |
6872 | return &bpf_sock_addr_setsockopt_proto; | |
6873 | default: | |
6874 | return NULL; | |
6875 | } | |
6876 | case BPF_FUNC_getsockopt: | |
6877 | switch (prog->expected_attach_type) { | |
6878 | case BPF_CGROUP_INET4_CONNECT: | |
6879 | case BPF_CGROUP_INET6_CONNECT: | |
6880 | return &bpf_sock_addr_getsockopt_proto; | |
6881 | default: | |
6882 | return NULL; | |
6883 | } | |
4fbac77d | 6884 | default: |
1df8f55a | 6885 | return bpf_sk_base_func_proto(func_id); |
4fbac77d AI |
6886 | } |
6887 | } | |
6888 | ||
2492d3b8 | 6889 | static const struct bpf_func_proto * |
5e43f899 | 6890 | sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
2492d3b8 DB |
6891 | { |
6892 | switch (func_id) { | |
6893 | case BPF_FUNC_skb_load_bytes: | |
6894 | return &bpf_skb_load_bytes_proto; | |
4e1ec56c DB |
6895 | case BPF_FUNC_skb_load_bytes_relative: |
6896 | return &bpf_skb_load_bytes_relative_proto; | |
91b8270f CF |
6897 | case BPF_FUNC_get_socket_cookie: |
6898 | return &bpf_get_socket_cookie_proto; | |
6acc5c29 CF |
6899 | case BPF_FUNC_get_socket_uid: |
6900 | return &bpf_get_socket_uid_proto; | |
7c4b90d7 AZ |
6901 | case BPF_FUNC_perf_event_output: |
6902 | return &bpf_skb_event_output_proto; | |
2492d3b8 | 6903 | default: |
1df8f55a | 6904 | return bpf_sk_base_func_proto(func_id); |
2492d3b8 DB |
6905 | } |
6906 | } | |
6907 | ||
6ac99e8f MKL |
6908 | const struct bpf_func_proto bpf_sk_storage_get_proto __weak; |
6909 | const struct bpf_func_proto bpf_sk_storage_delete_proto __weak; | |
6910 | ||
cd339431 RG |
6911 | static const struct bpf_func_proto * |
6912 | cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
6913 | { | |
6914 | switch (func_id) { | |
6915 | case BPF_FUNC_get_local_storage: | |
6916 | return &bpf_get_local_storage_proto; | |
46f8bc92 MKL |
6917 | case BPF_FUNC_sk_fullsock: |
6918 | return &bpf_sk_fullsock_proto; | |
6ac99e8f MKL |
6919 | case BPF_FUNC_sk_storage_get: |
6920 | return &bpf_sk_storage_get_proto; | |
6921 | case BPF_FUNC_sk_storage_delete: | |
6922 | return &bpf_sk_storage_delete_proto; | |
7c4b90d7 AZ |
6923 | case BPF_FUNC_perf_event_output: |
6924 | return &bpf_skb_event_output_proto; | |
4ecabd55 RG |
6925 | #ifdef CONFIG_SOCK_CGROUP_DATA |
6926 | case BPF_FUNC_skb_cgroup_id: | |
6927 | return &bpf_skb_cgroup_id_proto; | |
06d3e4c9 AI |
6928 | case BPF_FUNC_skb_ancestor_cgroup_id: |
6929 | return &bpf_skb_ancestor_cgroup_id_proto; | |
f307fa2c AI |
6930 | case BPF_FUNC_sk_cgroup_id: |
6931 | return &bpf_sk_cgroup_id_proto; | |
6932 | case BPF_FUNC_sk_ancestor_cgroup_id: | |
6933 | return &bpf_sk_ancestor_cgroup_id_proto; | |
4ecabd55 | 6934 | #endif |
655a51e5 | 6935 | #ifdef CONFIG_INET |
d56c2f95 AI |
6936 | case BPF_FUNC_sk_lookup_tcp: |
6937 | return &bpf_sk_lookup_tcp_proto; | |
6938 | case BPF_FUNC_sk_lookup_udp: | |
6939 | return &bpf_sk_lookup_udp_proto; | |
6940 | case BPF_FUNC_sk_release: | |
6941 | return &bpf_sk_release_proto; | |
6942 | case BPF_FUNC_skc_lookup_tcp: | |
6943 | return &bpf_skc_lookup_tcp_proto; | |
655a51e5 MKL |
6944 | case BPF_FUNC_tcp_sock: |
6945 | return &bpf_tcp_sock_proto; | |
dbafd7dd MKL |
6946 | case BPF_FUNC_get_listener_sock: |
6947 | return &bpf_get_listener_sock_proto; | |
f7c917ba | 6948 | case BPF_FUNC_skb_ecn_set_ce: |
6949 | return &bpf_skb_ecn_set_ce_proto; | |
655a51e5 | 6950 | #endif |
cd339431 RG |
6951 | default: |
6952 | return sk_filter_func_proto(func_id, prog); | |
6953 | } | |
6954 | } | |
6955 | ||
608cd71a | 6956 | static const struct bpf_func_proto * |
5e43f899 | 6957 | tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
608cd71a AS |
6958 | { |
6959 | switch (func_id) { | |
6960 | case BPF_FUNC_skb_store_bytes: | |
6961 | return &bpf_skb_store_bytes_proto; | |
05c74e5e DB |
6962 | case BPF_FUNC_skb_load_bytes: |
6963 | return &bpf_skb_load_bytes_proto; | |
4e1ec56c DB |
6964 | case BPF_FUNC_skb_load_bytes_relative: |
6965 | return &bpf_skb_load_bytes_relative_proto; | |
36bbef52 DB |
6966 | case BPF_FUNC_skb_pull_data: |
6967 | return &bpf_skb_pull_data_proto; | |
7d672345 DB |
6968 | case BPF_FUNC_csum_diff: |
6969 | return &bpf_csum_diff_proto; | |
36bbef52 DB |
6970 | case BPF_FUNC_csum_update: |
6971 | return &bpf_csum_update_proto; | |
7cdec54f DB |
6972 | case BPF_FUNC_csum_level: |
6973 | return &bpf_csum_level_proto; | |
91bc4822 AS |
6974 | case BPF_FUNC_l3_csum_replace: |
6975 | return &bpf_l3_csum_replace_proto; | |
6976 | case BPF_FUNC_l4_csum_replace: | |
6977 | return &bpf_l4_csum_replace_proto; | |
3896d655 AS |
6978 | case BPF_FUNC_clone_redirect: |
6979 | return &bpf_clone_redirect_proto; | |
8d20aabe DB |
6980 | case BPF_FUNC_get_cgroup_classid: |
6981 | return &bpf_get_cgroup_classid_proto; | |
4e10df9a AS |
6982 | case BPF_FUNC_skb_vlan_push: |
6983 | return &bpf_skb_vlan_push_proto; | |
6984 | case BPF_FUNC_skb_vlan_pop: | |
6985 | return &bpf_skb_vlan_pop_proto; | |
6578171a DB |
6986 | case BPF_FUNC_skb_change_proto: |
6987 | return &bpf_skb_change_proto_proto; | |
d2485c42 DB |
6988 | case BPF_FUNC_skb_change_type: |
6989 | return &bpf_skb_change_type_proto; | |
2be7e212 DB |
6990 | case BPF_FUNC_skb_adjust_room: |
6991 | return &bpf_skb_adjust_room_proto; | |
5293efe6 DB |
6992 | case BPF_FUNC_skb_change_tail: |
6993 | return &bpf_skb_change_tail_proto; | |
6f3f65d8 LC |
6994 | case BPF_FUNC_skb_change_head: |
6995 | return &bpf_skb_change_head_proto; | |
d3aa45ce AS |
6996 | case BPF_FUNC_skb_get_tunnel_key: |
6997 | return &bpf_skb_get_tunnel_key_proto; | |
6998 | case BPF_FUNC_skb_set_tunnel_key: | |
14ca0751 DB |
6999 | return bpf_get_skb_set_tunnel_proto(func_id); |
7000 | case BPF_FUNC_skb_get_tunnel_opt: | |
7001 | return &bpf_skb_get_tunnel_opt_proto; | |
7002 | case BPF_FUNC_skb_set_tunnel_opt: | |
7003 | return bpf_get_skb_set_tunnel_proto(func_id); | |
27b29f63 AS |
7004 | case BPF_FUNC_redirect: |
7005 | return &bpf_redirect_proto; | |
b4ab3141 DB |
7006 | case BPF_FUNC_redirect_neigh: |
7007 | return &bpf_redirect_neigh_proto; | |
c46646d0 DB |
7008 | case BPF_FUNC_get_route_realm: |
7009 | return &bpf_get_route_realm_proto; | |
13c5c240 DB |
7010 | case BPF_FUNC_get_hash_recalc: |
7011 | return &bpf_get_hash_recalc_proto; | |
7a4b28c6 DB |
7012 | case BPF_FUNC_set_hash_invalid: |
7013 | return &bpf_set_hash_invalid_proto; | |
ded092cd DB |
7014 | case BPF_FUNC_set_hash: |
7015 | return &bpf_set_hash_proto; | |
bd570ff9 | 7016 | case BPF_FUNC_perf_event_output: |
555c8a86 | 7017 | return &bpf_skb_event_output_proto; |
80b48c44 DB |
7018 | case BPF_FUNC_get_smp_processor_id: |
7019 | return &bpf_get_smp_processor_id_proto; | |
747ea55e DB |
7020 | case BPF_FUNC_skb_under_cgroup: |
7021 | return &bpf_skb_under_cgroup_proto; | |
91b8270f CF |
7022 | case BPF_FUNC_get_socket_cookie: |
7023 | return &bpf_get_socket_cookie_proto; | |
6acc5c29 CF |
7024 | case BPF_FUNC_get_socket_uid: |
7025 | return &bpf_get_socket_uid_proto; | |
cb20b08e DB |
7026 | case BPF_FUNC_fib_lookup: |
7027 | return &bpf_skb_fib_lookup_proto; | |
46f8bc92 MKL |
7028 | case BPF_FUNC_sk_fullsock: |
7029 | return &bpf_sk_fullsock_proto; | |
6ac99e8f MKL |
7030 | case BPF_FUNC_sk_storage_get: |
7031 | return &bpf_sk_storage_get_proto; | |
7032 | case BPF_FUNC_sk_storage_delete: | |
7033 | return &bpf_sk_storage_delete_proto; | |
12bed760 EB |
7034 | #ifdef CONFIG_XFRM |
7035 | case BPF_FUNC_skb_get_xfrm_state: | |
7036 | return &bpf_skb_get_xfrm_state_proto; | |
7037 | #endif | |
b426ce83 DB |
7038 | #ifdef CONFIG_CGROUP_NET_CLASSID |
7039 | case BPF_FUNC_skb_cgroup_classid: | |
7040 | return &bpf_skb_cgroup_classid_proto; | |
7041 | #endif | |
cb20b08e DB |
7042 | #ifdef CONFIG_SOCK_CGROUP_DATA |
7043 | case BPF_FUNC_skb_cgroup_id: | |
7044 | return &bpf_skb_cgroup_id_proto; | |
77236281 AI |
7045 | case BPF_FUNC_skb_ancestor_cgroup_id: |
7046 | return &bpf_skb_ancestor_cgroup_id_proto; | |
cb20b08e | 7047 | #endif |
df3f94a0 | 7048 | #ifdef CONFIG_INET |
6acc9b43 JS |
7049 | case BPF_FUNC_sk_lookup_tcp: |
7050 | return &bpf_sk_lookup_tcp_proto; | |
7051 | case BPF_FUNC_sk_lookup_udp: | |
7052 | return &bpf_sk_lookup_udp_proto; | |
7053 | case BPF_FUNC_sk_release: | |
7054 | return &bpf_sk_release_proto; | |
655a51e5 MKL |
7055 | case BPF_FUNC_tcp_sock: |
7056 | return &bpf_tcp_sock_proto; | |
dbafd7dd MKL |
7057 | case BPF_FUNC_get_listener_sock: |
7058 | return &bpf_get_listener_sock_proto; | |
edbf8c01 LB |
7059 | case BPF_FUNC_skc_lookup_tcp: |
7060 | return &bpf_skc_lookup_tcp_proto; | |
39904084 LB |
7061 | case BPF_FUNC_tcp_check_syncookie: |
7062 | return &bpf_tcp_check_syncookie_proto; | |
315a2029 PO |
7063 | case BPF_FUNC_skb_ecn_set_ce: |
7064 | return &bpf_skb_ecn_set_ce_proto; | |
70d66244 PP |
7065 | case BPF_FUNC_tcp_gen_syncookie: |
7066 | return &bpf_tcp_gen_syncookie_proto; | |
cf7fbe66 JS |
7067 | case BPF_FUNC_sk_assign: |
7068 | return &bpf_sk_assign_proto; | |
df3f94a0 | 7069 | #endif |
608cd71a | 7070 | default: |
1df8f55a | 7071 | return bpf_sk_base_func_proto(func_id); |
608cd71a AS |
7072 | } |
7073 | } | |
7074 | ||
6a773a15 | 7075 | static const struct bpf_func_proto * |
5e43f899 | 7076 | xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
6a773a15 | 7077 | { |
4de16969 DB |
7078 | switch (func_id) { |
7079 | case BPF_FUNC_perf_event_output: | |
7080 | return &bpf_xdp_event_output_proto; | |
669dc4d7 DB |
7081 | case BPF_FUNC_get_smp_processor_id: |
7082 | return &bpf_get_smp_processor_id_proto; | |
205c3807 DB |
7083 | case BPF_FUNC_csum_diff: |
7084 | return &bpf_csum_diff_proto; | |
17bedab2 MKL |
7085 | case BPF_FUNC_xdp_adjust_head: |
7086 | return &bpf_xdp_adjust_head_proto; | |
de8f3a83 DB |
7087 | case BPF_FUNC_xdp_adjust_meta: |
7088 | return &bpf_xdp_adjust_meta_proto; | |
814abfab JF |
7089 | case BPF_FUNC_redirect: |
7090 | return &bpf_xdp_redirect_proto; | |
97f91a7c | 7091 | case BPF_FUNC_redirect_map: |
e4a8e817 | 7092 | return &bpf_xdp_redirect_map_proto; |
b32cc5b9 NS |
7093 | case BPF_FUNC_xdp_adjust_tail: |
7094 | return &bpf_xdp_adjust_tail_proto; | |
87f5fc7e DA |
7095 | case BPF_FUNC_fib_lookup: |
7096 | return &bpf_xdp_fib_lookup_proto; | |
c8123ead NH |
7097 | #ifdef CONFIG_INET |
7098 | case BPF_FUNC_sk_lookup_udp: | |
7099 | return &bpf_xdp_sk_lookup_udp_proto; | |
7100 | case BPF_FUNC_sk_lookup_tcp: | |
7101 | return &bpf_xdp_sk_lookup_tcp_proto; | |
7102 | case BPF_FUNC_sk_release: | |
7103 | return &bpf_sk_release_proto; | |
edbf8c01 LB |
7104 | case BPF_FUNC_skc_lookup_tcp: |
7105 | return &bpf_xdp_skc_lookup_tcp_proto; | |
39904084 LB |
7106 | case BPF_FUNC_tcp_check_syncookie: |
7107 | return &bpf_tcp_check_syncookie_proto; | |
70d66244 PP |
7108 | case BPF_FUNC_tcp_gen_syncookie: |
7109 | return &bpf_tcp_gen_syncookie_proto; | |
c8123ead | 7110 | #endif |
4de16969 | 7111 | default: |
1df8f55a | 7112 | return bpf_sk_base_func_proto(func_id); |
4de16969 | 7113 | } |
6a773a15 BB |
7114 | } |
7115 | ||
604326b4 DB |
7116 | const struct bpf_func_proto bpf_sock_map_update_proto __weak; |
7117 | const struct bpf_func_proto bpf_sock_hash_update_proto __weak; | |
7118 | ||
8c4b4c7e | 7119 | static const struct bpf_func_proto * |
5e43f899 | 7120 | sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
8c4b4c7e LB |
7121 | { |
7122 | switch (func_id) { | |
7123 | case BPF_FUNC_setsockopt: | |
beecf11b | 7124 | return &bpf_sock_ops_setsockopt_proto; |
cd86d1fd | 7125 | case BPF_FUNC_getsockopt: |
beecf11b | 7126 | return &bpf_sock_ops_getsockopt_proto; |
b13d8807 LB |
7127 | case BPF_FUNC_sock_ops_cb_flags_set: |
7128 | return &bpf_sock_ops_cb_flags_set_proto; | |
174a79ff JF |
7129 | case BPF_FUNC_sock_map_update: |
7130 | return &bpf_sock_map_update_proto; | |
81110384 JF |
7131 | case BPF_FUNC_sock_hash_update: |
7132 | return &bpf_sock_hash_update_proto; | |
d692f113 AI |
7133 | case BPF_FUNC_get_socket_cookie: |
7134 | return &bpf_get_socket_cookie_sock_ops_proto; | |
cd339431 RG |
7135 | case BPF_FUNC_get_local_storage: |
7136 | return &bpf_get_local_storage_proto; | |
a5a3a828 | 7137 | case BPF_FUNC_perf_event_output: |
fcf752ea | 7138 | return &bpf_event_output_data_proto; |
1314ef56 SF |
7139 | case BPF_FUNC_sk_storage_get: |
7140 | return &bpf_sk_storage_get_proto; | |
7141 | case BPF_FUNC_sk_storage_delete: | |
7142 | return &bpf_sk_storage_delete_proto; | |
7143 | #ifdef CONFIG_INET | |
0813a841 MKL |
7144 | case BPF_FUNC_load_hdr_opt: |
7145 | return &bpf_sock_ops_load_hdr_opt_proto; | |
7146 | case BPF_FUNC_store_hdr_opt: | |
7147 | return &bpf_sock_ops_store_hdr_opt_proto; | |
7148 | case BPF_FUNC_reserve_hdr_opt: | |
7149 | return &bpf_sock_ops_reserve_hdr_opt_proto; | |
1314ef56 SF |
7150 | case BPF_FUNC_tcp_sock: |
7151 | return &bpf_tcp_sock_proto; | |
7152 | #endif /* CONFIG_INET */ | |
8c4b4c7e | 7153 | default: |
1df8f55a | 7154 | return bpf_sk_base_func_proto(func_id); |
8c4b4c7e LB |
7155 | } |
7156 | } | |
7157 | ||
604326b4 DB |
7158 | const struct bpf_func_proto bpf_msg_redirect_map_proto __weak; |
7159 | const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak; | |
7160 | ||
5e43f899 AI |
7161 | static const struct bpf_func_proto * |
7162 | sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
4f738adb JF |
7163 | { |
7164 | switch (func_id) { | |
7165 | case BPF_FUNC_msg_redirect_map: | |
7166 | return &bpf_msg_redirect_map_proto; | |
81110384 JF |
7167 | case BPF_FUNC_msg_redirect_hash: |
7168 | return &bpf_msg_redirect_hash_proto; | |
2a100317 JF |
7169 | case BPF_FUNC_msg_apply_bytes: |
7170 | return &bpf_msg_apply_bytes_proto; | |
91843d54 JF |
7171 | case BPF_FUNC_msg_cork_bytes: |
7172 | return &bpf_msg_cork_bytes_proto; | |
015632bb JF |
7173 | case BPF_FUNC_msg_pull_data: |
7174 | return &bpf_msg_pull_data_proto; | |
6fff607e JF |
7175 | case BPF_FUNC_msg_push_data: |
7176 | return &bpf_msg_push_data_proto; | |
7246d8ed JF |
7177 | case BPF_FUNC_msg_pop_data: |
7178 | return &bpf_msg_pop_data_proto; | |
abe3cac8 JF |
7179 | case BPF_FUNC_perf_event_output: |
7180 | return &bpf_event_output_data_proto; | |
7181 | case BPF_FUNC_get_current_uid_gid: | |
7182 | return &bpf_get_current_uid_gid_proto; | |
7183 | case BPF_FUNC_get_current_pid_tgid: | |
7184 | return &bpf_get_current_pid_tgid_proto; | |
13d70f5a JF |
7185 | case BPF_FUNC_sk_storage_get: |
7186 | return &bpf_sk_storage_get_proto; | |
7187 | case BPF_FUNC_sk_storage_delete: | |
7188 | return &bpf_sk_storage_delete_proto; | |
abe3cac8 JF |
7189 | #ifdef CONFIG_CGROUPS |
7190 | case BPF_FUNC_get_current_cgroup_id: | |
7191 | return &bpf_get_current_cgroup_id_proto; | |
7192 | case BPF_FUNC_get_current_ancestor_cgroup_id: | |
7193 | return &bpf_get_current_ancestor_cgroup_id_proto; | |
7194 | #endif | |
7195 | #ifdef CONFIG_CGROUP_NET_CLASSID | |
7196 | case BPF_FUNC_get_cgroup_classid: | |
7197 | return &bpf_get_cgroup_classid_curr_proto; | |
7198 | #endif | |
4f738adb | 7199 | default: |
1df8f55a | 7200 | return bpf_sk_base_func_proto(func_id); |
4f738adb JF |
7201 | } |
7202 | } | |
7203 | ||
604326b4 DB |
7204 | const struct bpf_func_proto bpf_sk_redirect_map_proto __weak; |
7205 | const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak; | |
7206 | ||
5e43f899 AI |
7207 | static const struct bpf_func_proto * |
7208 | sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
b005fd18 JF |
7209 | { |
7210 | switch (func_id) { | |
8a31db56 JF |
7211 | case BPF_FUNC_skb_store_bytes: |
7212 | return &bpf_skb_store_bytes_proto; | |
b005fd18 JF |
7213 | case BPF_FUNC_skb_load_bytes: |
7214 | return &bpf_skb_load_bytes_proto; | |
8a31db56 | 7215 | case BPF_FUNC_skb_pull_data: |
0ea488ff | 7216 | return &sk_skb_pull_data_proto; |
8a31db56 | 7217 | case BPF_FUNC_skb_change_tail: |
0ea488ff | 7218 | return &sk_skb_change_tail_proto; |
8a31db56 | 7219 | case BPF_FUNC_skb_change_head: |
0ea488ff | 7220 | return &sk_skb_change_head_proto; |
b005fd18 JF |
7221 | case BPF_FUNC_get_socket_cookie: |
7222 | return &bpf_get_socket_cookie_proto; | |
7223 | case BPF_FUNC_get_socket_uid: | |
7224 | return &bpf_get_socket_uid_proto; | |
174a79ff JF |
7225 | case BPF_FUNC_sk_redirect_map: |
7226 | return &bpf_sk_redirect_map_proto; | |
81110384 JF |
7227 | case BPF_FUNC_sk_redirect_hash: |
7228 | return &bpf_sk_redirect_hash_proto; | |
7c4b90d7 AZ |
7229 | case BPF_FUNC_perf_event_output: |
7230 | return &bpf_skb_event_output_proto; | |
df3f94a0 | 7231 | #ifdef CONFIG_INET |
6acc9b43 JS |
7232 | case BPF_FUNC_sk_lookup_tcp: |
7233 | return &bpf_sk_lookup_tcp_proto; | |
7234 | case BPF_FUNC_sk_lookup_udp: | |
7235 | return &bpf_sk_lookup_udp_proto; | |
7236 | case BPF_FUNC_sk_release: | |
7237 | return &bpf_sk_release_proto; | |
edbf8c01 LB |
7238 | case BPF_FUNC_skc_lookup_tcp: |
7239 | return &bpf_skc_lookup_tcp_proto; | |
df3f94a0 | 7240 | #endif |
b005fd18 | 7241 | default: |
1df8f55a | 7242 | return bpf_sk_base_func_proto(func_id); |
b005fd18 JF |
7243 | } |
7244 | } | |
7245 | ||
d58e468b PP |
7246 | static const struct bpf_func_proto * |
7247 | flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
7248 | { | |
7249 | switch (func_id) { | |
7250 | case BPF_FUNC_skb_load_bytes: | |
089b19a9 | 7251 | return &bpf_flow_dissector_load_bytes_proto; |
d58e468b | 7252 | default: |
1df8f55a | 7253 | return bpf_sk_base_func_proto(func_id); |
d58e468b PP |
7254 | } |
7255 | } | |
7256 | ||
cd3092c7 MX |
7257 | static const struct bpf_func_proto * |
7258 | lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
7259 | { | |
7260 | switch (func_id) { | |
7261 | case BPF_FUNC_skb_load_bytes: | |
7262 | return &bpf_skb_load_bytes_proto; | |
7263 | case BPF_FUNC_skb_pull_data: | |
7264 | return &bpf_skb_pull_data_proto; | |
7265 | case BPF_FUNC_csum_diff: | |
7266 | return &bpf_csum_diff_proto; | |
7267 | case BPF_FUNC_get_cgroup_classid: | |
7268 | return &bpf_get_cgroup_classid_proto; | |
7269 | case BPF_FUNC_get_route_realm: | |
7270 | return &bpf_get_route_realm_proto; | |
7271 | case BPF_FUNC_get_hash_recalc: | |
7272 | return &bpf_get_hash_recalc_proto; | |
7273 | case BPF_FUNC_perf_event_output: | |
7274 | return &bpf_skb_event_output_proto; | |
7275 | case BPF_FUNC_get_smp_processor_id: | |
7276 | return &bpf_get_smp_processor_id_proto; | |
7277 | case BPF_FUNC_skb_under_cgroup: | |
7278 | return &bpf_skb_under_cgroup_proto; | |
7279 | default: | |
1df8f55a | 7280 | return bpf_sk_base_func_proto(func_id); |
cd3092c7 MX |
7281 | } |
7282 | } | |
7283 | ||
7284 | static const struct bpf_func_proto * | |
7285 | lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
7286 | { | |
7287 | switch (func_id) { | |
7288 | case BPF_FUNC_lwt_push_encap: | |
3e0bd37c | 7289 | return &bpf_lwt_in_push_encap_proto; |
cd3092c7 MX |
7290 | default: |
7291 | return lwt_out_func_proto(func_id, prog); | |
7292 | } | |
7293 | } | |
7294 | ||
3a0af8fd | 7295 | static const struct bpf_func_proto * |
5e43f899 | 7296 | lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) |
3a0af8fd TG |
7297 | { |
7298 | switch (func_id) { | |
7299 | case BPF_FUNC_skb_get_tunnel_key: | |
7300 | return &bpf_skb_get_tunnel_key_proto; | |
7301 | case BPF_FUNC_skb_set_tunnel_key: | |
7302 | return bpf_get_skb_set_tunnel_proto(func_id); | |
7303 | case BPF_FUNC_skb_get_tunnel_opt: | |
7304 | return &bpf_skb_get_tunnel_opt_proto; | |
7305 | case BPF_FUNC_skb_set_tunnel_opt: | |
7306 | return bpf_get_skb_set_tunnel_proto(func_id); | |
7307 | case BPF_FUNC_redirect: | |
7308 | return &bpf_redirect_proto; | |
7309 | case BPF_FUNC_clone_redirect: | |
7310 | return &bpf_clone_redirect_proto; | |
7311 | case BPF_FUNC_skb_change_tail: | |
7312 | return &bpf_skb_change_tail_proto; | |
7313 | case BPF_FUNC_skb_change_head: | |
7314 | return &bpf_skb_change_head_proto; | |
7315 | case BPF_FUNC_skb_store_bytes: | |
7316 | return &bpf_skb_store_bytes_proto; | |
7317 | case BPF_FUNC_csum_update: | |
7318 | return &bpf_csum_update_proto; | |
7cdec54f DB |
7319 | case BPF_FUNC_csum_level: |
7320 | return &bpf_csum_level_proto; | |
3a0af8fd TG |
7321 | case BPF_FUNC_l3_csum_replace: |
7322 | return &bpf_l3_csum_replace_proto; | |
7323 | case BPF_FUNC_l4_csum_replace: | |
7324 | return &bpf_l4_csum_replace_proto; | |
7325 | case BPF_FUNC_set_hash_invalid: | |
7326 | return &bpf_set_hash_invalid_proto; | |
3e0bd37c PO |
7327 | case BPF_FUNC_lwt_push_encap: |
7328 | return &bpf_lwt_xmit_push_encap_proto; | |
3a0af8fd | 7329 | default: |
cd3092c7 | 7330 | return lwt_out_func_proto(func_id, prog); |
3a0af8fd TG |
7331 | } |
7332 | } | |
7333 | ||
004d4b27 MX |
7334 | static const struct bpf_func_proto * |
7335 | lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
7336 | { | |
7337 | switch (func_id) { | |
61d76980 | 7338 | #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF) |
004d4b27 MX |
7339 | case BPF_FUNC_lwt_seg6_store_bytes: |
7340 | return &bpf_lwt_seg6_store_bytes_proto; | |
7341 | case BPF_FUNC_lwt_seg6_action: | |
7342 | return &bpf_lwt_seg6_action_proto; | |
7343 | case BPF_FUNC_lwt_seg6_adjust_srh: | |
7344 | return &bpf_lwt_seg6_adjust_srh_proto; | |
61d76980 | 7345 | #endif |
004d4b27 MX |
7346 | default: |
7347 | return lwt_out_func_proto(func_id, prog); | |
3a0af8fd TG |
7348 | } |
7349 | } | |
7350 | ||
f96da094 | 7351 | static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type, |
5e43f899 | 7352 | const struct bpf_prog *prog, |
f96da094 | 7353 | struct bpf_insn_access_aux *info) |
23994631 | 7354 | { |
f96da094 | 7355 | const int size_default = sizeof(__u32); |
23994631 | 7356 | |
9bac3d6d AS |
7357 | if (off < 0 || off >= sizeof(struct __sk_buff)) |
7358 | return false; | |
62c7989b | 7359 | |
4936e352 | 7360 | /* The verifier guarantees that size > 0. */ |
9bac3d6d AS |
7361 | if (off % size != 0) |
7362 | return false; | |
62c7989b DB |
7363 | |
7364 | switch (off) { | |
f96da094 DB |
7365 | case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): |
7366 | if (off + size > offsetofend(struct __sk_buff, cb[4])) | |
62c7989b DB |
7367 | return false; |
7368 | break; | |
8a31db56 JF |
7369 | case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]): |
7370 | case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]): | |
7371 | case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4): | |
7372 | case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4): | |
f96da094 | 7373 | case bpf_ctx_range(struct __sk_buff, data): |
de8f3a83 | 7374 | case bpf_ctx_range(struct __sk_buff, data_meta): |
f96da094 DB |
7375 | case bpf_ctx_range(struct __sk_buff, data_end): |
7376 | if (size != size_default) | |
23994631 | 7377 | return false; |
31fd8581 | 7378 | break; |
b7df9ada | 7379 | case bpf_ctx_range_ptr(struct __sk_buff, flow_keys): |
089b19a9 | 7380 | return false; |
f11216b2 VD |
7381 | case bpf_ctx_range(struct __sk_buff, tstamp): |
7382 | if (size != sizeof(__u64)) | |
7383 | return false; | |
7384 | break; | |
46f8bc92 MKL |
7385 | case offsetof(struct __sk_buff, sk): |
7386 | if (type == BPF_WRITE || size != sizeof(__u64)) | |
7387 | return false; | |
7388 | info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL; | |
7389 | break; | |
31fd8581 | 7390 | default: |
f96da094 | 7391 | /* Only narrow read access allowed for now. */ |
31fd8581 | 7392 | if (type == BPF_WRITE) { |
f96da094 | 7393 | if (size != size_default) |
31fd8581 YS |
7394 | return false; |
7395 | } else { | |
f96da094 DB |
7396 | bpf_ctx_record_field_size(info, size_default); |
7397 | if (!bpf_ctx_narrow_access_ok(off, size, size_default)) | |
23994631 | 7398 | return false; |
31fd8581 | 7399 | } |
62c7989b | 7400 | } |
9bac3d6d AS |
7401 | |
7402 | return true; | |
7403 | } | |
7404 | ||
d691f9e8 | 7405 | static bool sk_filter_is_valid_access(int off, int size, |
19de99f7 | 7406 | enum bpf_access_type type, |
5e43f899 | 7407 | const struct bpf_prog *prog, |
23994631 | 7408 | struct bpf_insn_access_aux *info) |
d691f9e8 | 7409 | { |
db58ba45 | 7410 | switch (off) { |
f96da094 DB |
7411 | case bpf_ctx_range(struct __sk_buff, tc_classid): |
7412 | case bpf_ctx_range(struct __sk_buff, data): | |
de8f3a83 | 7413 | case bpf_ctx_range(struct __sk_buff, data_meta): |
f96da094 | 7414 | case bpf_ctx_range(struct __sk_buff, data_end): |
8a31db56 | 7415 | case bpf_ctx_range_till(struct __sk_buff, family, local_port): |
f11216b2 | 7416 | case bpf_ctx_range(struct __sk_buff, tstamp): |
e3da08d0 | 7417 | case bpf_ctx_range(struct __sk_buff, wire_len): |
045efa82 | 7418 | return false; |
db58ba45 | 7419 | } |
045efa82 | 7420 | |
d691f9e8 AS |
7421 | if (type == BPF_WRITE) { |
7422 | switch (off) { | |
f96da094 | 7423 | case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): |
d691f9e8 AS |
7424 | break; |
7425 | default: | |
7426 | return false; | |
7427 | } | |
7428 | } | |
7429 | ||
5e43f899 | 7430 | return bpf_skb_is_valid_access(off, size, type, prog, info); |
d691f9e8 AS |
7431 | } |
7432 | ||
b39b5f41 SL |
7433 | static bool cg_skb_is_valid_access(int off, int size, |
7434 | enum bpf_access_type type, | |
7435 | const struct bpf_prog *prog, | |
7436 | struct bpf_insn_access_aux *info) | |
7437 | { | |
7438 | switch (off) { | |
7439 | case bpf_ctx_range(struct __sk_buff, tc_classid): | |
7440 | case bpf_ctx_range(struct __sk_buff, data_meta): | |
e3da08d0 | 7441 | case bpf_ctx_range(struct __sk_buff, wire_len): |
b39b5f41 | 7442 | return false; |
ab21c1b5 DB |
7443 | case bpf_ctx_range(struct __sk_buff, data): |
7444 | case bpf_ctx_range(struct __sk_buff, data_end): | |
2c78ee89 | 7445 | if (!bpf_capable()) |
ab21c1b5 DB |
7446 | return false; |
7447 | break; | |
b39b5f41 | 7448 | } |
ab21c1b5 | 7449 | |
b39b5f41 SL |
7450 | if (type == BPF_WRITE) { |
7451 | switch (off) { | |
7452 | case bpf_ctx_range(struct __sk_buff, mark): | |
7453 | case bpf_ctx_range(struct __sk_buff, priority): | |
7454 | case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): | |
7455 | break; | |
f11216b2 | 7456 | case bpf_ctx_range(struct __sk_buff, tstamp): |
2c78ee89 | 7457 | if (!bpf_capable()) |
f11216b2 VD |
7458 | return false; |
7459 | break; | |
b39b5f41 SL |
7460 | default: |
7461 | return false; | |
7462 | } | |
7463 | } | |
7464 | ||
7465 | switch (off) { | |
7466 | case bpf_ctx_range(struct __sk_buff, data): | |
7467 | info->reg_type = PTR_TO_PACKET; | |
7468 | break; | |
7469 | case bpf_ctx_range(struct __sk_buff, data_end): | |
7470 | info->reg_type = PTR_TO_PACKET_END; | |
7471 | break; | |
7472 | } | |
7473 | ||
7474 | return bpf_skb_is_valid_access(off, size, type, prog, info); | |
7475 | } | |
7476 | ||
3a0af8fd TG |
7477 | static bool lwt_is_valid_access(int off, int size, |
7478 | enum bpf_access_type type, | |
5e43f899 | 7479 | const struct bpf_prog *prog, |
23994631 | 7480 | struct bpf_insn_access_aux *info) |
3a0af8fd TG |
7481 | { |
7482 | switch (off) { | |
f96da094 | 7483 | case bpf_ctx_range(struct __sk_buff, tc_classid): |
8a31db56 | 7484 | case bpf_ctx_range_till(struct __sk_buff, family, local_port): |
de8f3a83 | 7485 | case bpf_ctx_range(struct __sk_buff, data_meta): |
f11216b2 | 7486 | case bpf_ctx_range(struct __sk_buff, tstamp): |
e3da08d0 | 7487 | case bpf_ctx_range(struct __sk_buff, wire_len): |
3a0af8fd TG |
7488 | return false; |
7489 | } | |
7490 | ||
7491 | if (type == BPF_WRITE) { | |
7492 | switch (off) { | |
f96da094 DB |
7493 | case bpf_ctx_range(struct __sk_buff, mark): |
7494 | case bpf_ctx_range(struct __sk_buff, priority): | |
7495 | case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): | |
3a0af8fd TG |
7496 | break; |
7497 | default: | |
7498 | return false; | |
7499 | } | |
7500 | } | |
7501 | ||
f96da094 DB |
7502 | switch (off) { |
7503 | case bpf_ctx_range(struct __sk_buff, data): | |
7504 | info->reg_type = PTR_TO_PACKET; | |
7505 | break; | |
7506 | case bpf_ctx_range(struct __sk_buff, data_end): | |
7507 | info->reg_type = PTR_TO_PACKET_END; | |
7508 | break; | |
7509 | } | |
7510 | ||
5e43f899 | 7511 | return bpf_skb_is_valid_access(off, size, type, prog, info); |
3a0af8fd TG |
7512 | } |
7513 | ||
aac3fc32 AI |
7514 | /* Attach type specific accesses */ |
7515 | static bool __sock_filter_check_attach_type(int off, | |
7516 | enum bpf_access_type access_type, | |
7517 | enum bpf_attach_type attach_type) | |
61023658 | 7518 | { |
aac3fc32 AI |
7519 | switch (off) { |
7520 | case offsetof(struct bpf_sock, bound_dev_if): | |
7521 | case offsetof(struct bpf_sock, mark): | |
7522 | case offsetof(struct bpf_sock, priority): | |
7523 | switch (attach_type) { | |
7524 | case BPF_CGROUP_INET_SOCK_CREATE: | |
f5836749 | 7525 | case BPF_CGROUP_INET_SOCK_RELEASE: |
aac3fc32 AI |
7526 | goto full_access; |
7527 | default: | |
7528 | return false; | |
7529 | } | |
7530 | case bpf_ctx_range(struct bpf_sock, src_ip4): | |
7531 | switch (attach_type) { | |
7532 | case BPF_CGROUP_INET4_POST_BIND: | |
7533 | goto read_only; | |
7534 | default: | |
7535 | return false; | |
7536 | } | |
7537 | case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): | |
7538 | switch (attach_type) { | |
7539 | case BPF_CGROUP_INET6_POST_BIND: | |
7540 | goto read_only; | |
7541 | default: | |
7542 | return false; | |
7543 | } | |
7544 | case bpf_ctx_range(struct bpf_sock, src_port): | |
7545 | switch (attach_type) { | |
7546 | case BPF_CGROUP_INET4_POST_BIND: | |
7547 | case BPF_CGROUP_INET6_POST_BIND: | |
7548 | goto read_only; | |
61023658 DA |
7549 | default: |
7550 | return false; | |
7551 | } | |
7552 | } | |
aac3fc32 AI |
7553 | read_only: |
7554 | return access_type == BPF_READ; | |
7555 | full_access: | |
7556 | return true; | |
7557 | } | |
7558 | ||
46f8bc92 MKL |
7559 | bool bpf_sock_common_is_valid_access(int off, int size, |
7560 | enum bpf_access_type type, | |
aac3fc32 AI |
7561 | struct bpf_insn_access_aux *info) |
7562 | { | |
aac3fc32 | 7563 | switch (off) { |
46f8bc92 MKL |
7564 | case bpf_ctx_range_till(struct bpf_sock, type, priority): |
7565 | return false; | |
7566 | default: | |
7567 | return bpf_sock_is_valid_access(off, size, type, info); | |
aac3fc32 | 7568 | } |
aac3fc32 AI |
7569 | } |
7570 | ||
c64b7983 JS |
7571 | bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type, |
7572 | struct bpf_insn_access_aux *info) | |
aac3fc32 | 7573 | { |
aa65d696 MKL |
7574 | const int size_default = sizeof(__u32); |
7575 | ||
aac3fc32 | 7576 | if (off < 0 || off >= sizeof(struct bpf_sock)) |
61023658 | 7577 | return false; |
61023658 DA |
7578 | if (off % size != 0) |
7579 | return false; | |
aa65d696 MKL |
7580 | |
7581 | switch (off) { | |
7582 | case offsetof(struct bpf_sock, state): | |
7583 | case offsetof(struct bpf_sock, family): | |
7584 | case offsetof(struct bpf_sock, type): | |
7585 | case offsetof(struct bpf_sock, protocol): | |
7586 | case offsetof(struct bpf_sock, dst_port): | |
7587 | case offsetof(struct bpf_sock, src_port): | |
c3c16f2e | 7588 | case offsetof(struct bpf_sock, rx_queue_mapping): |
aa65d696 MKL |
7589 | case bpf_ctx_range(struct bpf_sock, src_ip4): |
7590 | case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): | |
7591 | case bpf_ctx_range(struct bpf_sock, dst_ip4): | |
7592 | case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]): | |
7593 | bpf_ctx_record_field_size(info, size_default); | |
7594 | return bpf_ctx_narrow_access_ok(off, size, size_default); | |
7595 | } | |
7596 | ||
7597 | return size == size_default; | |
61023658 DA |
7598 | } |
7599 | ||
c64b7983 JS |
7600 | static bool sock_filter_is_valid_access(int off, int size, |
7601 | enum bpf_access_type type, | |
7602 | const struct bpf_prog *prog, | |
7603 | struct bpf_insn_access_aux *info) | |
7604 | { | |
7605 | if (!bpf_sock_is_valid_access(off, size, type, info)) | |
7606 | return false; | |
7607 | return __sock_filter_check_attach_type(off, type, | |
7608 | prog->expected_attach_type); | |
7609 | } | |
7610 | ||
b09928b9 DB |
7611 | static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write, |
7612 | const struct bpf_prog *prog) | |
7613 | { | |
7614 | /* Neither direct read nor direct write requires any preliminary | |
7615 | * action. | |
7616 | */ | |
7617 | return 0; | |
7618 | } | |
7619 | ||
047b0ecd DB |
7620 | static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write, |
7621 | const struct bpf_prog *prog, int drop_verdict) | |
36bbef52 DB |
7622 | { |
7623 | struct bpf_insn *insn = insn_buf; | |
7624 | ||
7625 | if (!direct_write) | |
7626 | return 0; | |
7627 | ||
7628 | /* if (!skb->cloned) | |
7629 | * goto start; | |
7630 | * | |
7631 | * (Fast-path, otherwise approximation that we might be | |
7632 | * a clone, do the rest in helper.) | |
7633 | */ | |
7634 | *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET()); | |
7635 | *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK); | |
7636 | *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7); | |
7637 | ||
7638 | /* ret = bpf_skb_pull_data(skb, 0); */ | |
7639 | *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); | |
7640 | *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2); | |
7641 | *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, | |
7642 | BPF_FUNC_skb_pull_data); | |
7643 | /* if (!ret) | |
7644 | * goto restore; | |
7645 | * return TC_ACT_SHOT; | |
7646 | */ | |
7647 | *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2); | |
047b0ecd | 7648 | *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict); |
36bbef52 DB |
7649 | *insn++ = BPF_EXIT_INSN(); |
7650 | ||
7651 | /* restore: */ | |
7652 | *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); | |
7653 | /* start: */ | |
7654 | *insn++ = prog->insnsi[0]; | |
7655 | ||
7656 | return insn - insn_buf; | |
7657 | } | |
7658 | ||
e0cea7ce DB |
7659 | static int bpf_gen_ld_abs(const struct bpf_insn *orig, |
7660 | struct bpf_insn *insn_buf) | |
7661 | { | |
7662 | bool indirect = BPF_MODE(orig->code) == BPF_IND; | |
7663 | struct bpf_insn *insn = insn_buf; | |
7664 | ||
e0cea7ce DB |
7665 | if (!indirect) { |
7666 | *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm); | |
7667 | } else { | |
7668 | *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg); | |
7669 | if (orig->imm) | |
7670 | *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm); | |
7671 | } | |
e6a18d36 DB |
7672 | /* We're guaranteed here that CTX is in R6. */ |
7673 | *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX); | |
e0cea7ce DB |
7674 | |
7675 | switch (BPF_SIZE(orig->code)) { | |
7676 | case BPF_B: | |
7677 | *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache); | |
7678 | break; | |
7679 | case BPF_H: | |
7680 | *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache); | |
7681 | break; | |
7682 | case BPF_W: | |
7683 | *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache); | |
7684 | break; | |
7685 | } | |
7686 | ||
7687 | *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2); | |
7688 | *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0); | |
7689 | *insn++ = BPF_EXIT_INSN(); | |
7690 | ||
7691 | return insn - insn_buf; | |
7692 | } | |
7693 | ||
047b0ecd DB |
7694 | static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write, |
7695 | const struct bpf_prog *prog) | |
7696 | { | |
7697 | return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT); | |
7698 | } | |
7699 | ||
d691f9e8 | 7700 | static bool tc_cls_act_is_valid_access(int off, int size, |
19de99f7 | 7701 | enum bpf_access_type type, |
5e43f899 | 7702 | const struct bpf_prog *prog, |
23994631 | 7703 | struct bpf_insn_access_aux *info) |
d691f9e8 AS |
7704 | { |
7705 | if (type == BPF_WRITE) { | |
7706 | switch (off) { | |
f96da094 DB |
7707 | case bpf_ctx_range(struct __sk_buff, mark): |
7708 | case bpf_ctx_range(struct __sk_buff, tc_index): | |
7709 | case bpf_ctx_range(struct __sk_buff, priority): | |
7710 | case bpf_ctx_range(struct __sk_buff, tc_classid): | |
7711 | case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]): | |
f11216b2 | 7712 | case bpf_ctx_range(struct __sk_buff, tstamp): |
74e31ca8 | 7713 | case bpf_ctx_range(struct __sk_buff, queue_mapping): |
d691f9e8 AS |
7714 | break; |
7715 | default: | |
7716 | return false; | |
7717 | } | |
7718 | } | |
19de99f7 | 7719 | |
f96da094 DB |
7720 | switch (off) { |
7721 | case bpf_ctx_range(struct __sk_buff, data): | |
7722 | info->reg_type = PTR_TO_PACKET; | |
7723 | break; | |
de8f3a83 DB |
7724 | case bpf_ctx_range(struct __sk_buff, data_meta): |
7725 | info->reg_type = PTR_TO_PACKET_META; | |
7726 | break; | |
f96da094 DB |
7727 | case bpf_ctx_range(struct __sk_buff, data_end): |
7728 | info->reg_type = PTR_TO_PACKET_END; | |
7729 | break; | |
8a31db56 JF |
7730 | case bpf_ctx_range_till(struct __sk_buff, family, local_port): |
7731 | return false; | |
f96da094 DB |
7732 | } |
7733 | ||
5e43f899 | 7734 | return bpf_skb_is_valid_access(off, size, type, prog, info); |
d691f9e8 AS |
7735 | } |
7736 | ||
1afaf661 | 7737 | static bool __is_valid_xdp_access(int off, int size) |
6a773a15 BB |
7738 | { |
7739 | if (off < 0 || off >= sizeof(struct xdp_md)) | |
7740 | return false; | |
7741 | if (off % size != 0) | |
7742 | return false; | |
6088b582 | 7743 | if (size != sizeof(__u32)) |
6a773a15 BB |
7744 | return false; |
7745 | ||
7746 | return true; | |
7747 | } | |
7748 | ||
7749 | static bool xdp_is_valid_access(int off, int size, | |
7750 | enum bpf_access_type type, | |
5e43f899 | 7751 | const struct bpf_prog *prog, |
23994631 | 7752 | struct bpf_insn_access_aux *info) |
6a773a15 | 7753 | { |
64b59025 DA |
7754 | if (prog->expected_attach_type != BPF_XDP_DEVMAP) { |
7755 | switch (off) { | |
7756 | case offsetof(struct xdp_md, egress_ifindex): | |
7757 | return false; | |
7758 | } | |
7759 | } | |
7760 | ||
0d830032 JK |
7761 | if (type == BPF_WRITE) { |
7762 | if (bpf_prog_is_dev_bound(prog->aux)) { | |
7763 | switch (off) { | |
7764 | case offsetof(struct xdp_md, rx_queue_index): | |
7765 | return __is_valid_xdp_access(off, size); | |
7766 | } | |
7767 | } | |
6a773a15 | 7768 | return false; |
0d830032 | 7769 | } |
6a773a15 BB |
7770 | |
7771 | switch (off) { | |
7772 | case offsetof(struct xdp_md, data): | |
23994631 | 7773 | info->reg_type = PTR_TO_PACKET; |
6a773a15 | 7774 | break; |
de8f3a83 DB |
7775 | case offsetof(struct xdp_md, data_meta): |
7776 | info->reg_type = PTR_TO_PACKET_META; | |
7777 | break; | |
6a773a15 | 7778 | case offsetof(struct xdp_md, data_end): |
23994631 | 7779 | info->reg_type = PTR_TO_PACKET_END; |
6a773a15 BB |
7780 | break; |
7781 | } | |
7782 | ||
1afaf661 | 7783 | return __is_valid_xdp_access(off, size); |
6a773a15 BB |
7784 | } |
7785 | ||
7786 | void bpf_warn_invalid_xdp_action(u32 act) | |
7787 | { | |
9beb8bed DB |
7788 | const u32 act_max = XDP_REDIRECT; |
7789 | ||
7790 | WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n", | |
7791 | act > act_max ? "Illegal" : "Driver unsupported", | |
7792 | act); | |
6a773a15 BB |
7793 | } |
7794 | EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action); | |
7795 | ||
4fbac77d AI |
7796 | static bool sock_addr_is_valid_access(int off, int size, |
7797 | enum bpf_access_type type, | |
7798 | const struct bpf_prog *prog, | |
7799 | struct bpf_insn_access_aux *info) | |
7800 | { | |
7801 | const int size_default = sizeof(__u32); | |
7802 | ||
7803 | if (off < 0 || off >= sizeof(struct bpf_sock_addr)) | |
7804 | return false; | |
7805 | if (off % size != 0) | |
7806 | return false; | |
7807 | ||
7808 | /* Disallow access to IPv6 fields from IPv4 contex and vise | |
7809 | * versa. | |
7810 | */ | |
7811 | switch (off) { | |
7812 | case bpf_ctx_range(struct bpf_sock_addr, user_ip4): | |
7813 | switch (prog->expected_attach_type) { | |
7814 | case BPF_CGROUP_INET4_BIND: | |
d74bad4e | 7815 | case BPF_CGROUP_INET4_CONNECT: |
1b66d253 DB |
7816 | case BPF_CGROUP_INET4_GETPEERNAME: |
7817 | case BPF_CGROUP_INET4_GETSOCKNAME: | |
1cedee13 | 7818 | case BPF_CGROUP_UDP4_SENDMSG: |
983695fa | 7819 | case BPF_CGROUP_UDP4_RECVMSG: |
4fbac77d AI |
7820 | break; |
7821 | default: | |
7822 | return false; | |
7823 | } | |
7824 | break; | |
7825 | case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): | |
7826 | switch (prog->expected_attach_type) { | |
7827 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e | 7828 | case BPF_CGROUP_INET6_CONNECT: |
1b66d253 DB |
7829 | case BPF_CGROUP_INET6_GETPEERNAME: |
7830 | case BPF_CGROUP_INET6_GETSOCKNAME: | |
1cedee13 | 7831 | case BPF_CGROUP_UDP6_SENDMSG: |
983695fa | 7832 | case BPF_CGROUP_UDP6_RECVMSG: |
1cedee13 AI |
7833 | break; |
7834 | default: | |
7835 | return false; | |
7836 | } | |
7837 | break; | |
7838 | case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): | |
7839 | switch (prog->expected_attach_type) { | |
7840 | case BPF_CGROUP_UDP4_SENDMSG: | |
7841 | break; | |
7842 | default: | |
7843 | return false; | |
7844 | } | |
7845 | break; | |
7846 | case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], | |
7847 | msg_src_ip6[3]): | |
7848 | switch (prog->expected_attach_type) { | |
7849 | case BPF_CGROUP_UDP6_SENDMSG: | |
4fbac77d AI |
7850 | break; |
7851 | default: | |
7852 | return false; | |
7853 | } | |
7854 | break; | |
7855 | } | |
7856 | ||
7857 | switch (off) { | |
7858 | case bpf_ctx_range(struct bpf_sock_addr, user_ip4): | |
7859 | case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): | |
1cedee13 AI |
7860 | case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4): |
7861 | case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], | |
7862 | msg_src_ip6[3]): | |
7aebfa1b | 7863 | case bpf_ctx_range(struct bpf_sock_addr, user_port): |
4fbac77d AI |
7864 | if (type == BPF_READ) { |
7865 | bpf_ctx_record_field_size(info, size_default); | |
d4ecfeb1 SF |
7866 | |
7867 | if (bpf_ctx_wide_access_ok(off, size, | |
7868 | struct bpf_sock_addr, | |
7869 | user_ip6)) | |
7870 | return true; | |
7871 | ||
7872 | if (bpf_ctx_wide_access_ok(off, size, | |
7873 | struct bpf_sock_addr, | |
7874 | msg_src_ip6)) | |
7875 | return true; | |
7876 | ||
4fbac77d AI |
7877 | if (!bpf_ctx_narrow_access_ok(off, size, size_default)) |
7878 | return false; | |
7879 | } else { | |
b4399546 SF |
7880 | if (bpf_ctx_wide_access_ok(off, size, |
7881 | struct bpf_sock_addr, | |
7882 | user_ip6)) | |
600c70ba SF |
7883 | return true; |
7884 | ||
b4399546 SF |
7885 | if (bpf_ctx_wide_access_ok(off, size, |
7886 | struct bpf_sock_addr, | |
7887 | msg_src_ip6)) | |
600c70ba SF |
7888 | return true; |
7889 | ||
4fbac77d AI |
7890 | if (size != size_default) |
7891 | return false; | |
7892 | } | |
7893 | break; | |
fb85c4a7 SF |
7894 | case offsetof(struct bpf_sock_addr, sk): |
7895 | if (type != BPF_READ) | |
7896 | return false; | |
7897 | if (size != sizeof(__u64)) | |
7898 | return false; | |
7899 | info->reg_type = PTR_TO_SOCKET; | |
7900 | break; | |
4fbac77d AI |
7901 | default: |
7902 | if (type == BPF_READ) { | |
7903 | if (size != size_default) | |
7904 | return false; | |
7905 | } else { | |
7906 | return false; | |
7907 | } | |
7908 | } | |
7909 | ||
7910 | return true; | |
7911 | } | |
7912 | ||
44f0e430 LB |
7913 | static bool sock_ops_is_valid_access(int off, int size, |
7914 | enum bpf_access_type type, | |
5e43f899 | 7915 | const struct bpf_prog *prog, |
44f0e430 | 7916 | struct bpf_insn_access_aux *info) |
40304b2a | 7917 | { |
44f0e430 LB |
7918 | const int size_default = sizeof(__u32); |
7919 | ||
40304b2a LB |
7920 | if (off < 0 || off >= sizeof(struct bpf_sock_ops)) |
7921 | return false; | |
44f0e430 | 7922 | |
40304b2a LB |
7923 | /* The verifier guarantees that size > 0. */ |
7924 | if (off % size != 0) | |
7925 | return false; | |
40304b2a | 7926 | |
40304b2a LB |
7927 | if (type == BPF_WRITE) { |
7928 | switch (off) { | |
2585cd62 | 7929 | case offsetof(struct bpf_sock_ops, reply): |
6f9bd3d7 | 7930 | case offsetof(struct bpf_sock_ops, sk_txhash): |
44f0e430 LB |
7931 | if (size != size_default) |
7932 | return false; | |
40304b2a LB |
7933 | break; |
7934 | default: | |
7935 | return false; | |
7936 | } | |
44f0e430 LB |
7937 | } else { |
7938 | switch (off) { | |
7939 | case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received, | |
7940 | bytes_acked): | |
7941 | if (size != sizeof(__u64)) | |
7942 | return false; | |
7943 | break; | |
1314ef56 SF |
7944 | case offsetof(struct bpf_sock_ops, sk): |
7945 | if (size != sizeof(__u64)) | |
7946 | return false; | |
7947 | info->reg_type = PTR_TO_SOCKET_OR_NULL; | |
7948 | break; | |
0813a841 MKL |
7949 | case offsetof(struct bpf_sock_ops, skb_data): |
7950 | if (size != sizeof(__u64)) | |
7951 | return false; | |
7952 | info->reg_type = PTR_TO_PACKET; | |
7953 | break; | |
7954 | case offsetof(struct bpf_sock_ops, skb_data_end): | |
7955 | if (size != sizeof(__u64)) | |
7956 | return false; | |
7957 | info->reg_type = PTR_TO_PACKET_END; | |
7958 | break; | |
7959 | case offsetof(struct bpf_sock_ops, skb_tcp_flags): | |
7960 | bpf_ctx_record_field_size(info, size_default); | |
7961 | return bpf_ctx_narrow_access_ok(off, size, | |
7962 | size_default); | |
44f0e430 LB |
7963 | default: |
7964 | if (size != size_default) | |
7965 | return false; | |
7966 | break; | |
7967 | } | |
40304b2a LB |
7968 | } |
7969 | ||
44f0e430 | 7970 | return true; |
40304b2a LB |
7971 | } |
7972 | ||
8a31db56 JF |
7973 | static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write, |
7974 | const struct bpf_prog *prog) | |
7975 | { | |
047b0ecd | 7976 | return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP); |
8a31db56 JF |
7977 | } |
7978 | ||
b005fd18 JF |
7979 | static bool sk_skb_is_valid_access(int off, int size, |
7980 | enum bpf_access_type type, | |
5e43f899 | 7981 | const struct bpf_prog *prog, |
b005fd18 JF |
7982 | struct bpf_insn_access_aux *info) |
7983 | { | |
de8f3a83 DB |
7984 | switch (off) { |
7985 | case bpf_ctx_range(struct __sk_buff, tc_classid): | |
7986 | case bpf_ctx_range(struct __sk_buff, data_meta): | |
f11216b2 | 7987 | case bpf_ctx_range(struct __sk_buff, tstamp): |
e3da08d0 | 7988 | case bpf_ctx_range(struct __sk_buff, wire_len): |
de8f3a83 DB |
7989 | return false; |
7990 | } | |
7991 | ||
8a31db56 JF |
7992 | if (type == BPF_WRITE) { |
7993 | switch (off) { | |
8a31db56 JF |
7994 | case bpf_ctx_range(struct __sk_buff, tc_index): |
7995 | case bpf_ctx_range(struct __sk_buff, priority): | |
7996 | break; | |
7997 | default: | |
7998 | return false; | |
7999 | } | |
8000 | } | |
8001 | ||
b005fd18 | 8002 | switch (off) { |
f7e9cb1e | 8003 | case bpf_ctx_range(struct __sk_buff, mark): |
8a31db56 | 8004 | return false; |
b005fd18 JF |
8005 | case bpf_ctx_range(struct __sk_buff, data): |
8006 | info->reg_type = PTR_TO_PACKET; | |
8007 | break; | |
8008 | case bpf_ctx_range(struct __sk_buff, data_end): | |
8009 | info->reg_type = PTR_TO_PACKET_END; | |
8010 | break; | |
8011 | } | |
8012 | ||
5e43f899 | 8013 | return bpf_skb_is_valid_access(off, size, type, prog, info); |
b005fd18 JF |
8014 | } |
8015 | ||
4f738adb JF |
8016 | static bool sk_msg_is_valid_access(int off, int size, |
8017 | enum bpf_access_type type, | |
5e43f899 | 8018 | const struct bpf_prog *prog, |
4f738adb JF |
8019 | struct bpf_insn_access_aux *info) |
8020 | { | |
8021 | if (type == BPF_WRITE) | |
8022 | return false; | |
8023 | ||
bc1b4f01 JF |
8024 | if (off % size != 0) |
8025 | return false; | |
8026 | ||
4f738adb JF |
8027 | switch (off) { |
8028 | case offsetof(struct sk_msg_md, data): | |
8029 | info->reg_type = PTR_TO_PACKET; | |
303def35 JF |
8030 | if (size != sizeof(__u64)) |
8031 | return false; | |
4f738adb JF |
8032 | break; |
8033 | case offsetof(struct sk_msg_md, data_end): | |
8034 | info->reg_type = PTR_TO_PACKET_END; | |
303def35 JF |
8035 | if (size != sizeof(__u64)) |
8036 | return false; | |
4f738adb | 8037 | break; |
13d70f5a JF |
8038 | case offsetof(struct sk_msg_md, sk): |
8039 | if (size != sizeof(__u64)) | |
8040 | return false; | |
8041 | info->reg_type = PTR_TO_SOCKET; | |
8042 | break; | |
bc1b4f01 JF |
8043 | case bpf_ctx_range(struct sk_msg_md, family): |
8044 | case bpf_ctx_range(struct sk_msg_md, remote_ip4): | |
8045 | case bpf_ctx_range(struct sk_msg_md, local_ip4): | |
8046 | case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]): | |
8047 | case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]): | |
8048 | case bpf_ctx_range(struct sk_msg_md, remote_port): | |
8049 | case bpf_ctx_range(struct sk_msg_md, local_port): | |
8050 | case bpf_ctx_range(struct sk_msg_md, size): | |
303def35 JF |
8051 | if (size != sizeof(__u32)) |
8052 | return false; | |
bc1b4f01 JF |
8053 | break; |
8054 | default: | |
4f738adb | 8055 | return false; |
bc1b4f01 | 8056 | } |
4f738adb JF |
8057 | return true; |
8058 | } | |
8059 | ||
d58e468b PP |
8060 | static bool flow_dissector_is_valid_access(int off, int size, |
8061 | enum bpf_access_type type, | |
8062 | const struct bpf_prog *prog, | |
8063 | struct bpf_insn_access_aux *info) | |
8064 | { | |
089b19a9 SF |
8065 | const int size_default = sizeof(__u32); |
8066 | ||
8067 | if (off < 0 || off >= sizeof(struct __sk_buff)) | |
8068 | return false; | |
8069 | ||
2ee7fba0 SF |
8070 | if (type == BPF_WRITE) |
8071 | return false; | |
d58e468b PP |
8072 | |
8073 | switch (off) { | |
8074 | case bpf_ctx_range(struct __sk_buff, data): | |
089b19a9 SF |
8075 | if (size != size_default) |
8076 | return false; | |
d58e468b | 8077 | info->reg_type = PTR_TO_PACKET; |
089b19a9 | 8078 | return true; |
d58e468b | 8079 | case bpf_ctx_range(struct __sk_buff, data_end): |
089b19a9 SF |
8080 | if (size != size_default) |
8081 | return false; | |
d58e468b | 8082 | info->reg_type = PTR_TO_PACKET_END; |
089b19a9 | 8083 | return true; |
b7df9ada | 8084 | case bpf_ctx_range_ptr(struct __sk_buff, flow_keys): |
089b19a9 SF |
8085 | if (size != sizeof(__u64)) |
8086 | return false; | |
d58e468b | 8087 | info->reg_type = PTR_TO_FLOW_KEYS; |
089b19a9 | 8088 | return true; |
2ee7fba0 | 8089 | default: |
d58e468b PP |
8090 | return false; |
8091 | } | |
089b19a9 | 8092 | } |
d58e468b | 8093 | |
089b19a9 SF |
8094 | static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type, |
8095 | const struct bpf_insn *si, | |
8096 | struct bpf_insn *insn_buf, | |
8097 | struct bpf_prog *prog, | |
8098 | u32 *target_size) | |
8099 | ||
8100 | { | |
8101 | struct bpf_insn *insn = insn_buf; | |
8102 | ||
8103 | switch (si->off) { | |
8104 | case offsetof(struct __sk_buff, data): | |
8105 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data), | |
8106 | si->dst_reg, si->src_reg, | |
8107 | offsetof(struct bpf_flow_dissector, data)); | |
8108 | break; | |
8109 | ||
8110 | case offsetof(struct __sk_buff, data_end): | |
8111 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end), | |
8112 | si->dst_reg, si->src_reg, | |
8113 | offsetof(struct bpf_flow_dissector, data_end)); | |
8114 | break; | |
8115 | ||
8116 | case offsetof(struct __sk_buff, flow_keys): | |
8117 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys), | |
8118 | si->dst_reg, si->src_reg, | |
8119 | offsetof(struct bpf_flow_dissector, flow_keys)); | |
8120 | break; | |
8121 | } | |
8122 | ||
8123 | return insn - insn_buf; | |
d58e468b PP |
8124 | } |
8125 | ||
cf62089b WB |
8126 | static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si, |
8127 | struct bpf_insn *insn) | |
8128 | { | |
8129 | /* si->dst_reg = skb_shinfo(SKB); */ | |
8130 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | |
8131 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end), | |
8132 | BPF_REG_AX, si->src_reg, | |
8133 | offsetof(struct sk_buff, end)); | |
8134 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head), | |
8135 | si->dst_reg, si->src_reg, | |
8136 | offsetof(struct sk_buff, head)); | |
8137 | *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX); | |
8138 | #else | |
8139 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end), | |
8140 | si->dst_reg, si->src_reg, | |
8141 | offsetof(struct sk_buff, end)); | |
8142 | #endif | |
8143 | ||
8144 | return insn; | |
8145 | } | |
8146 | ||
2492d3b8 DB |
8147 | static u32 bpf_convert_ctx_access(enum bpf_access_type type, |
8148 | const struct bpf_insn *si, | |
8149 | struct bpf_insn *insn_buf, | |
f96da094 | 8150 | struct bpf_prog *prog, u32 *target_size) |
9bac3d6d AS |
8151 | { |
8152 | struct bpf_insn *insn = insn_buf; | |
6b8cc1d1 | 8153 | int off; |
9bac3d6d | 8154 | |
6b8cc1d1 | 8155 | switch (si->off) { |
9bac3d6d | 8156 | case offsetof(struct __sk_buff, len): |
6b8cc1d1 | 8157 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, |
f96da094 DB |
8158 | bpf_target_off(struct sk_buff, len, 4, |
8159 | target_size)); | |
9bac3d6d AS |
8160 | break; |
8161 | ||
0b8c707d | 8162 | case offsetof(struct __sk_buff, protocol): |
6b8cc1d1 | 8163 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, |
f96da094 DB |
8164 | bpf_target_off(struct sk_buff, protocol, 2, |
8165 | target_size)); | |
0b8c707d DB |
8166 | break; |
8167 | ||
27cd5452 | 8168 | case offsetof(struct __sk_buff, vlan_proto): |
6b8cc1d1 | 8169 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, |
f96da094 DB |
8170 | bpf_target_off(struct sk_buff, vlan_proto, 2, |
8171 | target_size)); | |
27cd5452 MS |
8172 | break; |
8173 | ||
bcad5718 | 8174 | case offsetof(struct __sk_buff, priority): |
754f1e6a | 8175 | if (type == BPF_WRITE) |
6b8cc1d1 | 8176 | *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, |
f96da094 DB |
8177 | bpf_target_off(struct sk_buff, priority, 4, |
8178 | target_size)); | |
754f1e6a | 8179 | else |
6b8cc1d1 | 8180 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, |
f96da094 DB |
8181 | bpf_target_off(struct sk_buff, priority, 4, |
8182 | target_size)); | |
bcad5718 DB |
8183 | break; |
8184 | ||
37e82c2f | 8185 | case offsetof(struct __sk_buff, ingress_ifindex): |
6b8cc1d1 | 8186 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, |
f96da094 DB |
8187 | bpf_target_off(struct sk_buff, skb_iif, 4, |
8188 | target_size)); | |
37e82c2f AS |
8189 | break; |
8190 | ||
8191 | case offsetof(struct __sk_buff, ifindex): | |
f035a515 | 8192 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), |
6b8cc1d1 | 8193 | si->dst_reg, si->src_reg, |
37e82c2f | 8194 | offsetof(struct sk_buff, dev)); |
6b8cc1d1 DB |
8195 | *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); |
8196 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
f96da094 DB |
8197 | bpf_target_off(struct net_device, ifindex, 4, |
8198 | target_size)); | |
37e82c2f AS |
8199 | break; |
8200 | ||
ba7591d8 | 8201 | case offsetof(struct __sk_buff, hash): |
6b8cc1d1 | 8202 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, |
f96da094 DB |
8203 | bpf_target_off(struct sk_buff, hash, 4, |
8204 | target_size)); | |
ba7591d8 DB |
8205 | break; |
8206 | ||
9bac3d6d | 8207 | case offsetof(struct __sk_buff, mark): |
d691f9e8 | 8208 | if (type == BPF_WRITE) |
6b8cc1d1 | 8209 | *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, |
f96da094 DB |
8210 | bpf_target_off(struct sk_buff, mark, 4, |
8211 | target_size)); | |
d691f9e8 | 8212 | else |
6b8cc1d1 | 8213 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, |
f96da094 DB |
8214 | bpf_target_off(struct sk_buff, mark, 4, |
8215 | target_size)); | |
d691f9e8 | 8216 | break; |
9bac3d6d AS |
8217 | |
8218 | case offsetof(struct __sk_buff, pkt_type): | |
f96da094 DB |
8219 | *target_size = 1; |
8220 | *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg, | |
8221 | PKT_TYPE_OFFSET()); | |
8222 | *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX); | |
8223 | #ifdef __BIG_ENDIAN_BITFIELD | |
8224 | *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5); | |
8225 | #endif | |
8226 | break; | |
9bac3d6d AS |
8227 | |
8228 | case offsetof(struct __sk_buff, queue_mapping): | |
74e31ca8 JDB |
8229 | if (type == BPF_WRITE) { |
8230 | *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1); | |
8231 | *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg, | |
8232 | bpf_target_off(struct sk_buff, | |
8233 | queue_mapping, | |
8234 | 2, target_size)); | |
8235 | } else { | |
8236 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, | |
8237 | bpf_target_off(struct sk_buff, | |
8238 | queue_mapping, | |
8239 | 2, target_size)); | |
8240 | } | |
f96da094 | 8241 | break; |
c2497395 | 8242 | |
c2497395 | 8243 | case offsetof(struct __sk_buff, vlan_present): |
9c212255 MM |
8244 | *target_size = 1; |
8245 | *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg, | |
8246 | PKT_VLAN_PRESENT_OFFSET()); | |
8247 | if (PKT_VLAN_PRESENT_BIT) | |
8248 | *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT); | |
8249 | if (PKT_VLAN_PRESENT_BIT < 7) | |
8250 | *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1); | |
8251 | break; | |
f96da094 | 8252 | |
9c212255 | 8253 | case offsetof(struct __sk_buff, vlan_tci): |
f96da094 DB |
8254 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, |
8255 | bpf_target_off(struct sk_buff, vlan_tci, 2, | |
8256 | target_size)); | |
f96da094 | 8257 | break; |
d691f9e8 AS |
8258 | |
8259 | case offsetof(struct __sk_buff, cb[0]) ... | |
f96da094 | 8260 | offsetofend(struct __sk_buff, cb[4]) - 1: |
c593642c | 8261 | BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20); |
62c7989b DB |
8262 | BUILD_BUG_ON((offsetof(struct sk_buff, cb) + |
8263 | offsetof(struct qdisc_skb_cb, data)) % | |
8264 | sizeof(__u64)); | |
d691f9e8 | 8265 | |
ff936a04 | 8266 | prog->cb_access = 1; |
6b8cc1d1 DB |
8267 | off = si->off; |
8268 | off -= offsetof(struct __sk_buff, cb[0]); | |
8269 | off += offsetof(struct sk_buff, cb); | |
8270 | off += offsetof(struct qdisc_skb_cb, data); | |
d691f9e8 | 8271 | if (type == BPF_WRITE) |
62c7989b | 8272 | *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg, |
6b8cc1d1 | 8273 | si->src_reg, off); |
d691f9e8 | 8274 | else |
62c7989b | 8275 | *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg, |
6b8cc1d1 | 8276 | si->src_reg, off); |
d691f9e8 AS |
8277 | break; |
8278 | ||
045efa82 | 8279 | case offsetof(struct __sk_buff, tc_classid): |
c593642c | 8280 | BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2); |
6b8cc1d1 DB |
8281 | |
8282 | off = si->off; | |
8283 | off -= offsetof(struct __sk_buff, tc_classid); | |
8284 | off += offsetof(struct sk_buff, cb); | |
8285 | off += offsetof(struct qdisc_skb_cb, tc_classid); | |
f96da094 | 8286 | *target_size = 2; |
09c37a2c | 8287 | if (type == BPF_WRITE) |
6b8cc1d1 DB |
8288 | *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, |
8289 | si->src_reg, off); | |
09c37a2c | 8290 | else |
6b8cc1d1 DB |
8291 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, |
8292 | si->src_reg, off); | |
045efa82 DB |
8293 | break; |
8294 | ||
db58ba45 | 8295 | case offsetof(struct __sk_buff, data): |
f035a515 | 8296 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), |
6b8cc1d1 | 8297 | si->dst_reg, si->src_reg, |
db58ba45 AS |
8298 | offsetof(struct sk_buff, data)); |
8299 | break; | |
8300 | ||
de8f3a83 DB |
8301 | case offsetof(struct __sk_buff, data_meta): |
8302 | off = si->off; | |
8303 | off -= offsetof(struct __sk_buff, data_meta); | |
8304 | off += offsetof(struct sk_buff, cb); | |
8305 | off += offsetof(struct bpf_skb_data_end, data_meta); | |
8306 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, | |
8307 | si->src_reg, off); | |
8308 | break; | |
8309 | ||
db58ba45 | 8310 | case offsetof(struct __sk_buff, data_end): |
6b8cc1d1 DB |
8311 | off = si->off; |
8312 | off -= offsetof(struct __sk_buff, data_end); | |
8313 | off += offsetof(struct sk_buff, cb); | |
8314 | off += offsetof(struct bpf_skb_data_end, data_end); | |
8315 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, | |
8316 | si->src_reg, off); | |
db58ba45 AS |
8317 | break; |
8318 | ||
d691f9e8 AS |
8319 | case offsetof(struct __sk_buff, tc_index): |
8320 | #ifdef CONFIG_NET_SCHED | |
d691f9e8 | 8321 | if (type == BPF_WRITE) |
6b8cc1d1 | 8322 | *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg, |
f96da094 DB |
8323 | bpf_target_off(struct sk_buff, tc_index, 2, |
8324 | target_size)); | |
d691f9e8 | 8325 | else |
6b8cc1d1 | 8326 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, |
f96da094 DB |
8327 | bpf_target_off(struct sk_buff, tc_index, 2, |
8328 | target_size)); | |
d691f9e8 | 8329 | #else |
2ed46ce4 | 8330 | *target_size = 2; |
d691f9e8 | 8331 | if (type == BPF_WRITE) |
6b8cc1d1 | 8332 | *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg); |
d691f9e8 | 8333 | else |
6b8cc1d1 | 8334 | *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); |
b1d9fc41 DB |
8335 | #endif |
8336 | break; | |
8337 | ||
8338 | case offsetof(struct __sk_buff, napi_id): | |
8339 | #if defined(CONFIG_NET_RX_BUSY_POLL) | |
b1d9fc41 | 8340 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, |
f96da094 DB |
8341 | bpf_target_off(struct sk_buff, napi_id, 4, |
8342 | target_size)); | |
b1d9fc41 DB |
8343 | *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1); |
8344 | *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); | |
8345 | #else | |
2ed46ce4 | 8346 | *target_size = 4; |
b1d9fc41 | 8347 | *insn++ = BPF_MOV64_IMM(si->dst_reg, 0); |
d691f9e8 | 8348 | #endif |
6b8cc1d1 | 8349 | break; |
8a31db56 | 8350 | case offsetof(struct __sk_buff, family): |
c593642c | 8351 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); |
8a31db56 JF |
8352 | |
8353 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), | |
8354 | si->dst_reg, si->src_reg, | |
8355 | offsetof(struct sk_buff, sk)); | |
8356 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, | |
8357 | bpf_target_off(struct sock_common, | |
8358 | skc_family, | |
8359 | 2, target_size)); | |
8360 | break; | |
8361 | case offsetof(struct __sk_buff, remote_ip4): | |
c593642c | 8362 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); |
8a31db56 JF |
8363 | |
8364 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), | |
8365 | si->dst_reg, si->src_reg, | |
8366 | offsetof(struct sk_buff, sk)); | |
8367 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
8368 | bpf_target_off(struct sock_common, | |
8369 | skc_daddr, | |
8370 | 4, target_size)); | |
8371 | break; | |
8372 | case offsetof(struct __sk_buff, local_ip4): | |
c593642c | 8373 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
8a31db56 JF |
8374 | skc_rcv_saddr) != 4); |
8375 | ||
8376 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), | |
8377 | si->dst_reg, si->src_reg, | |
8378 | offsetof(struct sk_buff, sk)); | |
8379 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
8380 | bpf_target_off(struct sock_common, | |
8381 | skc_rcv_saddr, | |
8382 | 4, target_size)); | |
8383 | break; | |
8384 | case offsetof(struct __sk_buff, remote_ip6[0]) ... | |
8385 | offsetof(struct __sk_buff, remote_ip6[3]): | |
8386 | #if IS_ENABLED(CONFIG_IPV6) | |
c593642c | 8387 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
8a31db56 JF |
8388 | skc_v6_daddr.s6_addr32[0]) != 4); |
8389 | ||
8390 | off = si->off; | |
8391 | off -= offsetof(struct __sk_buff, remote_ip6[0]); | |
8392 | ||
8393 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), | |
8394 | si->dst_reg, si->src_reg, | |
8395 | offsetof(struct sk_buff, sk)); | |
8396 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
8397 | offsetof(struct sock_common, | |
8398 | skc_v6_daddr.s6_addr32[0]) + | |
8399 | off); | |
8400 | #else | |
8401 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
8402 | #endif | |
8403 | break; | |
8404 | case offsetof(struct __sk_buff, local_ip6[0]) ... | |
8405 | offsetof(struct __sk_buff, local_ip6[3]): | |
8406 | #if IS_ENABLED(CONFIG_IPV6) | |
c593642c | 8407 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
8a31db56 JF |
8408 | skc_v6_rcv_saddr.s6_addr32[0]) != 4); |
8409 | ||
8410 | off = si->off; | |
8411 | off -= offsetof(struct __sk_buff, local_ip6[0]); | |
8412 | ||
8413 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), | |
8414 | si->dst_reg, si->src_reg, | |
8415 | offsetof(struct sk_buff, sk)); | |
8416 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
8417 | offsetof(struct sock_common, | |
8418 | skc_v6_rcv_saddr.s6_addr32[0]) + | |
8419 | off); | |
8420 | #else | |
8421 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
8422 | #endif | |
8423 | break; | |
8424 | ||
8425 | case offsetof(struct __sk_buff, remote_port): | |
c593642c | 8426 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); |
8a31db56 JF |
8427 | |
8428 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), | |
8429 | si->dst_reg, si->src_reg, | |
8430 | offsetof(struct sk_buff, sk)); | |
8431 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, | |
8432 | bpf_target_off(struct sock_common, | |
8433 | skc_dport, | |
8434 | 2, target_size)); | |
8435 | #ifndef __BIG_ENDIAN_BITFIELD | |
8436 | *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); | |
8437 | #endif | |
8438 | break; | |
8439 | ||
8440 | case offsetof(struct __sk_buff, local_port): | |
c593642c | 8441 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); |
8a31db56 JF |
8442 | |
8443 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), | |
8444 | si->dst_reg, si->src_reg, | |
8445 | offsetof(struct sk_buff, sk)); | |
8446 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, | |
8447 | bpf_target_off(struct sock_common, | |
8448 | skc_num, 2, target_size)); | |
8449 | break; | |
d58e468b | 8450 | |
f11216b2 | 8451 | case offsetof(struct __sk_buff, tstamp): |
c593642c | 8452 | BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8); |
f11216b2 VD |
8453 | |
8454 | if (type == BPF_WRITE) | |
8455 | *insn++ = BPF_STX_MEM(BPF_DW, | |
8456 | si->dst_reg, si->src_reg, | |
8457 | bpf_target_off(struct sk_buff, | |
8458 | tstamp, 8, | |
8459 | target_size)); | |
8460 | else | |
8461 | *insn++ = BPF_LDX_MEM(BPF_DW, | |
8462 | si->dst_reg, si->src_reg, | |
8463 | bpf_target_off(struct sk_buff, | |
8464 | tstamp, 8, | |
8465 | target_size)); | |
e3da08d0 PP |
8466 | break; |
8467 | ||
d9ff286a | 8468 | case offsetof(struct __sk_buff, gso_segs): |
cf62089b | 8469 | insn = bpf_convert_shinfo_access(si, insn); |
d9ff286a ED |
8470 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs), |
8471 | si->dst_reg, si->dst_reg, | |
8472 | bpf_target_off(struct skb_shared_info, | |
8473 | gso_segs, 2, | |
8474 | target_size)); | |
8475 | break; | |
cf62089b WB |
8476 | case offsetof(struct __sk_buff, gso_size): |
8477 | insn = bpf_convert_shinfo_access(si, insn); | |
8478 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size), | |
8479 | si->dst_reg, si->dst_reg, | |
8480 | bpf_target_off(struct skb_shared_info, | |
8481 | gso_size, 2, | |
8482 | target_size)); | |
8483 | break; | |
e3da08d0 | 8484 | case offsetof(struct __sk_buff, wire_len): |
c593642c | 8485 | BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4); |
e3da08d0 PP |
8486 | |
8487 | off = si->off; | |
8488 | off -= offsetof(struct __sk_buff, wire_len); | |
8489 | off += offsetof(struct sk_buff, cb); | |
8490 | off += offsetof(struct qdisc_skb_cb, pkt_len); | |
8491 | *target_size = 4; | |
8492 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off); | |
46f8bc92 MKL |
8493 | break; |
8494 | ||
8495 | case offsetof(struct __sk_buff, sk): | |
8496 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk), | |
8497 | si->dst_reg, si->src_reg, | |
8498 | offsetof(struct sk_buff, sk)); | |
8499 | break; | |
9bac3d6d AS |
8500 | } |
8501 | ||
8502 | return insn - insn_buf; | |
89aa0758 AS |
8503 | } |
8504 | ||
c64b7983 JS |
8505 | u32 bpf_sock_convert_ctx_access(enum bpf_access_type type, |
8506 | const struct bpf_insn *si, | |
8507 | struct bpf_insn *insn_buf, | |
8508 | struct bpf_prog *prog, u32 *target_size) | |
61023658 DA |
8509 | { |
8510 | struct bpf_insn *insn = insn_buf; | |
aac3fc32 | 8511 | int off; |
61023658 | 8512 | |
6b8cc1d1 | 8513 | switch (si->off) { |
61023658 | 8514 | case offsetof(struct bpf_sock, bound_dev_if): |
c593642c | 8515 | BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4); |
61023658 DA |
8516 | |
8517 | if (type == BPF_WRITE) | |
6b8cc1d1 | 8518 | *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, |
61023658 DA |
8519 | offsetof(struct sock, sk_bound_dev_if)); |
8520 | else | |
6b8cc1d1 | 8521 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, |
61023658 DA |
8522 | offsetof(struct sock, sk_bound_dev_if)); |
8523 | break; | |
aa4c1037 | 8524 | |
482dca93 | 8525 | case offsetof(struct bpf_sock, mark): |
c593642c | 8526 | BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4); |
482dca93 DA |
8527 | |
8528 | if (type == BPF_WRITE) | |
8529 | *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
8530 | offsetof(struct sock, sk_mark)); | |
8531 | else | |
8532 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
8533 | offsetof(struct sock, sk_mark)); | |
8534 | break; | |
8535 | ||
8536 | case offsetof(struct bpf_sock, priority): | |
c593642c | 8537 | BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4); |
482dca93 DA |
8538 | |
8539 | if (type == BPF_WRITE) | |
8540 | *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
8541 | offsetof(struct sock, sk_priority)); | |
8542 | else | |
8543 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
8544 | offsetof(struct sock, sk_priority)); | |
8545 | break; | |
8546 | ||
aa4c1037 | 8547 | case offsetof(struct bpf_sock, family): |
aa65d696 MKL |
8548 | *insn++ = BPF_LDX_MEM( |
8549 | BPF_FIELD_SIZEOF(struct sock_common, skc_family), | |
8550 | si->dst_reg, si->src_reg, | |
8551 | bpf_target_off(struct sock_common, | |
8552 | skc_family, | |
c593642c | 8553 | sizeof_field(struct sock_common, |
aa65d696 MKL |
8554 | skc_family), |
8555 | target_size)); | |
aa4c1037 DA |
8556 | break; |
8557 | ||
8558 | case offsetof(struct bpf_sock, type): | |
bf976514 MM |
8559 | *insn++ = BPF_LDX_MEM( |
8560 | BPF_FIELD_SIZEOF(struct sock, sk_type), | |
8561 | si->dst_reg, si->src_reg, | |
8562 | bpf_target_off(struct sock, sk_type, | |
8563 | sizeof_field(struct sock, sk_type), | |
8564 | target_size)); | |
aa4c1037 DA |
8565 | break; |
8566 | ||
8567 | case offsetof(struct bpf_sock, protocol): | |
bf976514 MM |
8568 | *insn++ = BPF_LDX_MEM( |
8569 | BPF_FIELD_SIZEOF(struct sock, sk_protocol), | |
8570 | si->dst_reg, si->src_reg, | |
8571 | bpf_target_off(struct sock, sk_protocol, | |
8572 | sizeof_field(struct sock, sk_protocol), | |
8573 | target_size)); | |
aa4c1037 | 8574 | break; |
aac3fc32 AI |
8575 | |
8576 | case offsetof(struct bpf_sock, src_ip4): | |
8577 | *insn++ = BPF_LDX_MEM( | |
8578 | BPF_SIZE(si->code), si->dst_reg, si->src_reg, | |
8579 | bpf_target_off(struct sock_common, skc_rcv_saddr, | |
c593642c | 8580 | sizeof_field(struct sock_common, |
aac3fc32 AI |
8581 | skc_rcv_saddr), |
8582 | target_size)); | |
8583 | break; | |
8584 | ||
aa65d696 MKL |
8585 | case offsetof(struct bpf_sock, dst_ip4): |
8586 | *insn++ = BPF_LDX_MEM( | |
8587 | BPF_SIZE(si->code), si->dst_reg, si->src_reg, | |
8588 | bpf_target_off(struct sock_common, skc_daddr, | |
c593642c | 8589 | sizeof_field(struct sock_common, |
aa65d696 MKL |
8590 | skc_daddr), |
8591 | target_size)); | |
8592 | break; | |
8593 | ||
aac3fc32 AI |
8594 | case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]): |
8595 | #if IS_ENABLED(CONFIG_IPV6) | |
8596 | off = si->off; | |
8597 | off -= offsetof(struct bpf_sock, src_ip6[0]); | |
8598 | *insn++ = BPF_LDX_MEM( | |
8599 | BPF_SIZE(si->code), si->dst_reg, si->src_reg, | |
8600 | bpf_target_off( | |
8601 | struct sock_common, | |
8602 | skc_v6_rcv_saddr.s6_addr32[0], | |
c593642c | 8603 | sizeof_field(struct sock_common, |
aac3fc32 AI |
8604 | skc_v6_rcv_saddr.s6_addr32[0]), |
8605 | target_size) + off); | |
8606 | #else | |
8607 | (void)off; | |
8608 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
8609 | #endif | |
8610 | break; | |
8611 | ||
aa65d696 MKL |
8612 | case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]): |
8613 | #if IS_ENABLED(CONFIG_IPV6) | |
8614 | off = si->off; | |
8615 | off -= offsetof(struct bpf_sock, dst_ip6[0]); | |
8616 | *insn++ = BPF_LDX_MEM( | |
8617 | BPF_SIZE(si->code), si->dst_reg, si->src_reg, | |
8618 | bpf_target_off(struct sock_common, | |
8619 | skc_v6_daddr.s6_addr32[0], | |
c593642c | 8620 | sizeof_field(struct sock_common, |
aa65d696 MKL |
8621 | skc_v6_daddr.s6_addr32[0]), |
8622 | target_size) + off); | |
8623 | #else | |
8624 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
8625 | *target_size = 4; | |
8626 | #endif | |
8627 | break; | |
8628 | ||
aac3fc32 AI |
8629 | case offsetof(struct bpf_sock, src_port): |
8630 | *insn++ = BPF_LDX_MEM( | |
8631 | BPF_FIELD_SIZEOF(struct sock_common, skc_num), | |
8632 | si->dst_reg, si->src_reg, | |
8633 | bpf_target_off(struct sock_common, skc_num, | |
c593642c | 8634 | sizeof_field(struct sock_common, |
aac3fc32 AI |
8635 | skc_num), |
8636 | target_size)); | |
8637 | break; | |
aa65d696 MKL |
8638 | |
8639 | case offsetof(struct bpf_sock, dst_port): | |
8640 | *insn++ = BPF_LDX_MEM( | |
8641 | BPF_FIELD_SIZEOF(struct sock_common, skc_dport), | |
8642 | si->dst_reg, si->src_reg, | |
8643 | bpf_target_off(struct sock_common, skc_dport, | |
c593642c | 8644 | sizeof_field(struct sock_common, |
aa65d696 MKL |
8645 | skc_dport), |
8646 | target_size)); | |
8647 | break; | |
8648 | ||
8649 | case offsetof(struct bpf_sock, state): | |
8650 | *insn++ = BPF_LDX_MEM( | |
8651 | BPF_FIELD_SIZEOF(struct sock_common, skc_state), | |
8652 | si->dst_reg, si->src_reg, | |
8653 | bpf_target_off(struct sock_common, skc_state, | |
c593642c | 8654 | sizeof_field(struct sock_common, |
aa65d696 MKL |
8655 | skc_state), |
8656 | target_size)); | |
8657 | break; | |
c3c16f2e AN |
8658 | case offsetof(struct bpf_sock, rx_queue_mapping): |
8659 | #ifdef CONFIG_XPS | |
8660 | *insn++ = BPF_LDX_MEM( | |
8661 | BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping), | |
8662 | si->dst_reg, si->src_reg, | |
8663 | bpf_target_off(struct sock, sk_rx_queue_mapping, | |
8664 | sizeof_field(struct sock, | |
8665 | sk_rx_queue_mapping), | |
8666 | target_size)); | |
8667 | *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING, | |
8668 | 1); | |
8669 | *insn++ = BPF_MOV64_IMM(si->dst_reg, -1); | |
8670 | #else | |
8671 | *insn++ = BPF_MOV64_IMM(si->dst_reg, -1); | |
8672 | *target_size = 2; | |
8673 | #endif | |
8674 | break; | |
61023658 DA |
8675 | } |
8676 | ||
8677 | return insn - insn_buf; | |
8678 | } | |
8679 | ||
6b8cc1d1 DB |
8680 | static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, |
8681 | const struct bpf_insn *si, | |
374fb54e | 8682 | struct bpf_insn *insn_buf, |
f96da094 | 8683 | struct bpf_prog *prog, u32 *target_size) |
374fb54e DB |
8684 | { |
8685 | struct bpf_insn *insn = insn_buf; | |
8686 | ||
6b8cc1d1 | 8687 | switch (si->off) { |
374fb54e | 8688 | case offsetof(struct __sk_buff, ifindex): |
374fb54e | 8689 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev), |
6b8cc1d1 | 8690 | si->dst_reg, si->src_reg, |
374fb54e | 8691 | offsetof(struct sk_buff, dev)); |
6b8cc1d1 | 8692 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, |
f96da094 DB |
8693 | bpf_target_off(struct net_device, ifindex, 4, |
8694 | target_size)); | |
374fb54e DB |
8695 | break; |
8696 | default: | |
f96da094 DB |
8697 | return bpf_convert_ctx_access(type, si, insn_buf, prog, |
8698 | target_size); | |
374fb54e DB |
8699 | } |
8700 | ||
8701 | return insn - insn_buf; | |
8702 | } | |
8703 | ||
6b8cc1d1 DB |
8704 | static u32 xdp_convert_ctx_access(enum bpf_access_type type, |
8705 | const struct bpf_insn *si, | |
6a773a15 | 8706 | struct bpf_insn *insn_buf, |
f96da094 | 8707 | struct bpf_prog *prog, u32 *target_size) |
6a773a15 BB |
8708 | { |
8709 | struct bpf_insn *insn = insn_buf; | |
8710 | ||
6b8cc1d1 | 8711 | switch (si->off) { |
6a773a15 | 8712 | case offsetof(struct xdp_md, data): |
f035a515 | 8713 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data), |
6b8cc1d1 | 8714 | si->dst_reg, si->src_reg, |
6a773a15 BB |
8715 | offsetof(struct xdp_buff, data)); |
8716 | break; | |
de8f3a83 DB |
8717 | case offsetof(struct xdp_md, data_meta): |
8718 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta), | |
8719 | si->dst_reg, si->src_reg, | |
8720 | offsetof(struct xdp_buff, data_meta)); | |
8721 | break; | |
6a773a15 | 8722 | case offsetof(struct xdp_md, data_end): |
f035a515 | 8723 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end), |
6b8cc1d1 | 8724 | si->dst_reg, si->src_reg, |
6a773a15 BB |
8725 | offsetof(struct xdp_buff, data_end)); |
8726 | break; | |
02dd3291 JDB |
8727 | case offsetof(struct xdp_md, ingress_ifindex): |
8728 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), | |
8729 | si->dst_reg, si->src_reg, | |
8730 | offsetof(struct xdp_buff, rxq)); | |
8731 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev), | |
8732 | si->dst_reg, si->dst_reg, | |
8733 | offsetof(struct xdp_rxq_info, dev)); | |
8734 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
daaf24c6 | 8735 | offsetof(struct net_device, ifindex)); |
02dd3291 JDB |
8736 | break; |
8737 | case offsetof(struct xdp_md, rx_queue_index): | |
8738 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), | |
8739 | si->dst_reg, si->src_reg, | |
8740 | offsetof(struct xdp_buff, rxq)); | |
8741 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
daaf24c6 JDB |
8742 | offsetof(struct xdp_rxq_info, |
8743 | queue_index)); | |
02dd3291 | 8744 | break; |
64b59025 DA |
8745 | case offsetof(struct xdp_md, egress_ifindex): |
8746 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq), | |
8747 | si->dst_reg, si->src_reg, | |
8748 | offsetof(struct xdp_buff, txq)); | |
8749 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev), | |
8750 | si->dst_reg, si->dst_reg, | |
8751 | offsetof(struct xdp_txq_info, dev)); | |
8752 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
8753 | offsetof(struct net_device, ifindex)); | |
8754 | break; | |
6a773a15 BB |
8755 | } |
8756 | ||
8757 | return insn - insn_buf; | |
8758 | } | |
8759 | ||
4fbac77d AI |
8760 | /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of |
8761 | * context Structure, F is Field in context structure that contains a pointer | |
8762 | * to Nested Structure of type NS that has the field NF. | |
8763 | * | |
8764 | * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make | |
8765 | * sure that SIZE is not greater than actual size of S.F.NF. | |
8766 | * | |
8767 | * If offset OFF is provided, the load happens from that offset relative to | |
8768 | * offset of NF. | |
8769 | */ | |
8770 | #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \ | |
8771 | do { \ | |
8772 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \ | |
8773 | si->src_reg, offsetof(S, F)); \ | |
8774 | *insn++ = BPF_LDX_MEM( \ | |
8775 | SIZE, si->dst_reg, si->dst_reg, \ | |
c593642c | 8776 | bpf_target_off(NS, NF, sizeof_field(NS, NF), \ |
4fbac77d AI |
8777 | target_size) \ |
8778 | + OFF); \ | |
8779 | } while (0) | |
8780 | ||
8781 | #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \ | |
8782 | SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \ | |
8783 | BPF_FIELD_SIZEOF(NS, NF), 0) | |
8784 | ||
8785 | /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to | |
8786 | * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation. | |
8787 | * | |
4fbac77d AI |
8788 | * In addition it uses Temporary Field TF (member of struct S) as the 3rd |
8789 | * "register" since two registers available in convert_ctx_access are not | |
8790 | * enough: we can't override neither SRC, since it contains value to store, nor | |
8791 | * DST since it contains pointer to context that may be used by later | |
8792 | * instructions. But we need a temporary place to save pointer to nested | |
8793 | * structure whose field we want to store to. | |
8794 | */ | |
600c70ba | 8795 | #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \ |
4fbac77d AI |
8796 | do { \ |
8797 | int tmp_reg = BPF_REG_9; \ | |
8798 | if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \ | |
8799 | --tmp_reg; \ | |
8800 | if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \ | |
8801 | --tmp_reg; \ | |
8802 | *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \ | |
8803 | offsetof(S, TF)); \ | |
8804 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \ | |
8805 | si->dst_reg, offsetof(S, F)); \ | |
600c70ba | 8806 | *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \ |
c593642c | 8807 | bpf_target_off(NS, NF, sizeof_field(NS, NF), \ |
4fbac77d AI |
8808 | target_size) \ |
8809 | + OFF); \ | |
8810 | *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \ | |
8811 | offsetof(S, TF)); \ | |
8812 | } while (0) | |
8813 | ||
8814 | #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \ | |
8815 | TF) \ | |
8816 | do { \ | |
8817 | if (type == BPF_WRITE) { \ | |
600c70ba SF |
8818 | SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \ |
8819 | OFF, TF); \ | |
4fbac77d AI |
8820 | } else { \ |
8821 | SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \ | |
8822 | S, NS, F, NF, SIZE, OFF); \ | |
8823 | } \ | |
8824 | } while (0) | |
8825 | ||
8826 | #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \ | |
8827 | SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \ | |
8828 | S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF) | |
8829 | ||
8830 | static u32 sock_addr_convert_ctx_access(enum bpf_access_type type, | |
8831 | const struct bpf_insn *si, | |
8832 | struct bpf_insn *insn_buf, | |
8833 | struct bpf_prog *prog, u32 *target_size) | |
8834 | { | |
7aebfa1b | 8835 | int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port); |
4fbac77d | 8836 | struct bpf_insn *insn = insn_buf; |
4fbac77d AI |
8837 | |
8838 | switch (si->off) { | |
8839 | case offsetof(struct bpf_sock_addr, user_family): | |
8840 | SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, | |
8841 | struct sockaddr, uaddr, sa_family); | |
8842 | break; | |
8843 | ||
8844 | case offsetof(struct bpf_sock_addr, user_ip4): | |
8845 | SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( | |
8846 | struct bpf_sock_addr_kern, struct sockaddr_in, uaddr, | |
8847 | sin_addr, BPF_SIZE(si->code), 0, tmp_reg); | |
8848 | break; | |
8849 | ||
8850 | case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]): | |
8851 | off = si->off; | |
8852 | off -= offsetof(struct bpf_sock_addr, user_ip6[0]); | |
8853 | SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( | |
8854 | struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr, | |
8855 | sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off, | |
8856 | tmp_reg); | |
8857 | break; | |
8858 | ||
8859 | case offsetof(struct bpf_sock_addr, user_port): | |
8860 | /* To get port we need to know sa_family first and then treat | |
8861 | * sockaddr as either sockaddr_in or sockaddr_in6. | |
8862 | * Though we can simplify since port field has same offset and | |
8863 | * size in both structures. | |
8864 | * Here we check this invariant and use just one of the | |
8865 | * structures if it's true. | |
8866 | */ | |
8867 | BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) != | |
8868 | offsetof(struct sockaddr_in6, sin6_port)); | |
c593642c PB |
8869 | BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) != |
8870 | sizeof_field(struct sockaddr_in6, sin6_port)); | |
7aebfa1b AI |
8871 | /* Account for sin6_port being smaller than user_port. */ |
8872 | port_size = min(port_size, BPF_LDST_BYTES(si)); | |
8873 | SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( | |
8874 | struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr, | |
8875 | sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg); | |
4fbac77d AI |
8876 | break; |
8877 | ||
8878 | case offsetof(struct bpf_sock_addr, family): | |
8879 | SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, | |
8880 | struct sock, sk, sk_family); | |
8881 | break; | |
8882 | ||
8883 | case offsetof(struct bpf_sock_addr, type): | |
bf976514 MM |
8884 | SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, |
8885 | struct sock, sk, sk_type); | |
4fbac77d AI |
8886 | break; |
8887 | ||
8888 | case offsetof(struct bpf_sock_addr, protocol): | |
bf976514 MM |
8889 | SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern, |
8890 | struct sock, sk, sk_protocol); | |
4fbac77d | 8891 | break; |
1cedee13 AI |
8892 | |
8893 | case offsetof(struct bpf_sock_addr, msg_src_ip4): | |
8894 | /* Treat t_ctx as struct in_addr for msg_src_ip4. */ | |
8895 | SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( | |
8896 | struct bpf_sock_addr_kern, struct in_addr, t_ctx, | |
8897 | s_addr, BPF_SIZE(si->code), 0, tmp_reg); | |
8898 | break; | |
8899 | ||
8900 | case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0], | |
8901 | msg_src_ip6[3]): | |
8902 | off = si->off; | |
8903 | off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]); | |
8904 | /* Treat t_ctx as struct in6_addr for msg_src_ip6. */ | |
8905 | SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( | |
8906 | struct bpf_sock_addr_kern, struct in6_addr, t_ctx, | |
8907 | s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg); | |
8908 | break; | |
fb85c4a7 SF |
8909 | case offsetof(struct bpf_sock_addr, sk): |
8910 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk), | |
8911 | si->dst_reg, si->src_reg, | |
8912 | offsetof(struct bpf_sock_addr_kern, sk)); | |
8913 | break; | |
4fbac77d AI |
8914 | } |
8915 | ||
8916 | return insn - insn_buf; | |
8917 | } | |
8918 | ||
40304b2a LB |
8919 | static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, |
8920 | const struct bpf_insn *si, | |
8921 | struct bpf_insn *insn_buf, | |
f96da094 DB |
8922 | struct bpf_prog *prog, |
8923 | u32 *target_size) | |
40304b2a LB |
8924 | { |
8925 | struct bpf_insn *insn = insn_buf; | |
8926 | int off; | |
8927 | ||
9b1f3d6e MKL |
8928 | /* Helper macro for adding read access to tcp_sock or sock fields. */ |
8929 | #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \ | |
8930 | do { \ | |
fd09af01 | 8931 | int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \ |
c593642c PB |
8932 | BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \ |
8933 | sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \ | |
fd09af01 JF |
8934 | if (si->dst_reg == reg || si->src_reg == reg) \ |
8935 | reg--; \ | |
8936 | if (si->dst_reg == reg || si->src_reg == reg) \ | |
8937 | reg--; \ | |
8938 | if (si->dst_reg == si->src_reg) { \ | |
8939 | *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \ | |
8940 | offsetof(struct bpf_sock_ops_kern, \ | |
8941 | temp)); \ | |
8942 | fullsock_reg = reg; \ | |
8943 | jmp += 2; \ | |
8944 | } \ | |
9b1f3d6e MKL |
8945 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ |
8946 | struct bpf_sock_ops_kern, \ | |
8947 | is_fullsock), \ | |
fd09af01 | 8948 | fullsock_reg, si->src_reg, \ |
9b1f3d6e MKL |
8949 | offsetof(struct bpf_sock_ops_kern, \ |
8950 | is_fullsock)); \ | |
fd09af01 JF |
8951 | *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \ |
8952 | if (si->dst_reg == si->src_reg) \ | |
8953 | *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ | |
8954 | offsetof(struct bpf_sock_ops_kern, \ | |
8955 | temp)); \ | |
9b1f3d6e MKL |
8956 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ |
8957 | struct bpf_sock_ops_kern, sk),\ | |
8958 | si->dst_reg, si->src_reg, \ | |
8959 | offsetof(struct bpf_sock_ops_kern, sk));\ | |
8960 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \ | |
8961 | OBJ_FIELD), \ | |
8962 | si->dst_reg, si->dst_reg, \ | |
8963 | offsetof(OBJ, OBJ_FIELD)); \ | |
fd09af01 JF |
8964 | if (si->dst_reg == si->src_reg) { \ |
8965 | *insn++ = BPF_JMP_A(1); \ | |
8966 | *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ | |
8967 | offsetof(struct bpf_sock_ops_kern, \ | |
8968 | temp)); \ | |
8969 | } \ | |
9b1f3d6e MKL |
8970 | } while (0) |
8971 | ||
84f44df6 JF |
8972 | #define SOCK_OPS_GET_SK() \ |
8973 | do { \ | |
8974 | int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \ | |
8975 | if (si->dst_reg == reg || si->src_reg == reg) \ | |
8976 | reg--; \ | |
8977 | if (si->dst_reg == reg || si->src_reg == reg) \ | |
8978 | reg--; \ | |
8979 | if (si->dst_reg == si->src_reg) { \ | |
8980 | *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \ | |
8981 | offsetof(struct bpf_sock_ops_kern, \ | |
8982 | temp)); \ | |
8983 | fullsock_reg = reg; \ | |
8984 | jmp += 2; \ | |
8985 | } \ | |
8986 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ | |
8987 | struct bpf_sock_ops_kern, \ | |
8988 | is_fullsock), \ | |
8989 | fullsock_reg, si->src_reg, \ | |
8990 | offsetof(struct bpf_sock_ops_kern, \ | |
8991 | is_fullsock)); \ | |
8992 | *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \ | |
8993 | if (si->dst_reg == si->src_reg) \ | |
8994 | *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ | |
8995 | offsetof(struct bpf_sock_ops_kern, \ | |
8996 | temp)); \ | |
8997 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ | |
8998 | struct bpf_sock_ops_kern, sk),\ | |
8999 | si->dst_reg, si->src_reg, \ | |
9000 | offsetof(struct bpf_sock_ops_kern, sk));\ | |
9001 | if (si->dst_reg == si->src_reg) { \ | |
9002 | *insn++ = BPF_JMP_A(1); \ | |
9003 | *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \ | |
9004 | offsetof(struct bpf_sock_ops_kern, \ | |
9005 | temp)); \ | |
9006 | } \ | |
9007 | } while (0) | |
9008 | ||
9b1f3d6e MKL |
9009 | #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \ |
9010 | SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock) | |
9011 | ||
9012 | /* Helper macro for adding write access to tcp_sock or sock fields. | |
9013 | * The macro is called with two registers, dst_reg which contains a pointer | |
9014 | * to ctx (context) and src_reg which contains the value that should be | |
9015 | * stored. However, we need an additional register since we cannot overwrite | |
9016 | * dst_reg because it may be used later in the program. | |
9017 | * Instead we "borrow" one of the other register. We first save its value | |
9018 | * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore | |
9019 | * it at the end of the macro. | |
9020 | */ | |
9021 | #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \ | |
9022 | do { \ | |
9023 | int reg = BPF_REG_9; \ | |
c593642c PB |
9024 | BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \ |
9025 | sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \ | |
9b1f3d6e MKL |
9026 | if (si->dst_reg == reg || si->src_reg == reg) \ |
9027 | reg--; \ | |
9028 | if (si->dst_reg == reg || si->src_reg == reg) \ | |
9029 | reg--; \ | |
9030 | *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \ | |
9031 | offsetof(struct bpf_sock_ops_kern, \ | |
9032 | temp)); \ | |
9033 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ | |
9034 | struct bpf_sock_ops_kern, \ | |
9035 | is_fullsock), \ | |
9036 | reg, si->dst_reg, \ | |
9037 | offsetof(struct bpf_sock_ops_kern, \ | |
9038 | is_fullsock)); \ | |
9039 | *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \ | |
9040 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \ | |
9041 | struct bpf_sock_ops_kern, sk),\ | |
9042 | reg, si->dst_reg, \ | |
9043 | offsetof(struct bpf_sock_ops_kern, sk));\ | |
9044 | *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \ | |
9045 | reg, si->src_reg, \ | |
9046 | offsetof(OBJ, OBJ_FIELD)); \ | |
9047 | *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \ | |
9048 | offsetof(struct bpf_sock_ops_kern, \ | |
9049 | temp)); \ | |
9050 | } while (0) | |
9051 | ||
9052 | #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \ | |
9053 | do { \ | |
9054 | if (TYPE == BPF_WRITE) \ | |
9055 | SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \ | |
9056 | else \ | |
9057 | SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \ | |
9058 | } while (0) | |
9059 | ||
9b1f3d6e MKL |
9060 | if (insn > insn_buf) |
9061 | return insn - insn_buf; | |
9062 | ||
40304b2a | 9063 | switch (si->off) { |
c9985d09 MKL |
9064 | case offsetof(struct bpf_sock_ops, op): |
9065 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, | |
9066 | op), | |
9067 | si->dst_reg, si->src_reg, | |
9068 | offsetof(struct bpf_sock_ops_kern, op)); | |
9069 | break; | |
9070 | ||
9071 | case offsetof(struct bpf_sock_ops, replylong[0]) ... | |
40304b2a | 9072 | offsetof(struct bpf_sock_ops, replylong[3]): |
c593642c PB |
9073 | BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) != |
9074 | sizeof_field(struct bpf_sock_ops_kern, reply)); | |
9075 | BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) != | |
9076 | sizeof_field(struct bpf_sock_ops_kern, replylong)); | |
40304b2a | 9077 | off = si->off; |
c9985d09 MKL |
9078 | off -= offsetof(struct bpf_sock_ops, replylong[0]); |
9079 | off += offsetof(struct bpf_sock_ops_kern, replylong[0]); | |
40304b2a LB |
9080 | if (type == BPF_WRITE) |
9081 | *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
9082 | off); | |
9083 | else | |
9084 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
9085 | off); | |
9086 | break; | |
9087 | ||
9088 | case offsetof(struct bpf_sock_ops, family): | |
c593642c | 9089 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); |
40304b2a LB |
9090 | |
9091 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9092 | struct bpf_sock_ops_kern, sk), | |
9093 | si->dst_reg, si->src_reg, | |
9094 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9095 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, | |
9096 | offsetof(struct sock_common, skc_family)); | |
9097 | break; | |
9098 | ||
9099 | case offsetof(struct bpf_sock_ops, remote_ip4): | |
c593642c | 9100 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); |
40304b2a LB |
9101 | |
9102 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9103 | struct bpf_sock_ops_kern, sk), | |
9104 | si->dst_reg, si->src_reg, | |
9105 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9106 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
9107 | offsetof(struct sock_common, skc_daddr)); | |
9108 | break; | |
9109 | ||
9110 | case offsetof(struct bpf_sock_ops, local_ip4): | |
c593642c | 9111 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
303def35 | 9112 | skc_rcv_saddr) != 4); |
40304b2a LB |
9113 | |
9114 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9115 | struct bpf_sock_ops_kern, sk), | |
9116 | si->dst_reg, si->src_reg, | |
9117 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9118 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
9119 | offsetof(struct sock_common, | |
9120 | skc_rcv_saddr)); | |
9121 | break; | |
9122 | ||
9123 | case offsetof(struct bpf_sock_ops, remote_ip6[0]) ... | |
9124 | offsetof(struct bpf_sock_ops, remote_ip6[3]): | |
9125 | #if IS_ENABLED(CONFIG_IPV6) | |
c593642c | 9126 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
40304b2a LB |
9127 | skc_v6_daddr.s6_addr32[0]) != 4); |
9128 | ||
9129 | off = si->off; | |
9130 | off -= offsetof(struct bpf_sock_ops, remote_ip6[0]); | |
9131 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9132 | struct bpf_sock_ops_kern, sk), | |
9133 | si->dst_reg, si->src_reg, | |
9134 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9135 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
9136 | offsetof(struct sock_common, | |
9137 | skc_v6_daddr.s6_addr32[0]) + | |
9138 | off); | |
9139 | #else | |
9140 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
9141 | #endif | |
9142 | break; | |
9143 | ||
9144 | case offsetof(struct bpf_sock_ops, local_ip6[0]) ... | |
9145 | offsetof(struct bpf_sock_ops, local_ip6[3]): | |
9146 | #if IS_ENABLED(CONFIG_IPV6) | |
c593642c | 9147 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
40304b2a LB |
9148 | skc_v6_rcv_saddr.s6_addr32[0]) != 4); |
9149 | ||
9150 | off = si->off; | |
9151 | off -= offsetof(struct bpf_sock_ops, local_ip6[0]); | |
9152 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9153 | struct bpf_sock_ops_kern, sk), | |
9154 | si->dst_reg, si->src_reg, | |
9155 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9156 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
9157 | offsetof(struct sock_common, | |
9158 | skc_v6_rcv_saddr.s6_addr32[0]) + | |
9159 | off); | |
9160 | #else | |
9161 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
9162 | #endif | |
9163 | break; | |
9164 | ||
9165 | case offsetof(struct bpf_sock_ops, remote_port): | |
c593642c | 9166 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); |
40304b2a LB |
9167 | |
9168 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9169 | struct bpf_sock_ops_kern, sk), | |
9170 | si->dst_reg, si->src_reg, | |
9171 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9172 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, | |
9173 | offsetof(struct sock_common, skc_dport)); | |
9174 | #ifndef __BIG_ENDIAN_BITFIELD | |
9175 | *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); | |
9176 | #endif | |
9177 | break; | |
9178 | ||
9179 | case offsetof(struct bpf_sock_ops, local_port): | |
c593642c | 9180 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); |
40304b2a LB |
9181 | |
9182 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9183 | struct bpf_sock_ops_kern, sk), | |
9184 | si->dst_reg, si->src_reg, | |
9185 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9186 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, | |
9187 | offsetof(struct sock_common, skc_num)); | |
9188 | break; | |
f19397a5 LB |
9189 | |
9190 | case offsetof(struct bpf_sock_ops, is_fullsock): | |
9191 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9192 | struct bpf_sock_ops_kern, | |
9193 | is_fullsock), | |
9194 | si->dst_reg, si->src_reg, | |
9195 | offsetof(struct bpf_sock_ops_kern, | |
9196 | is_fullsock)); | |
9197 | break; | |
9198 | ||
44f0e430 | 9199 | case offsetof(struct bpf_sock_ops, state): |
c593642c | 9200 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1); |
44f0e430 LB |
9201 | |
9202 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9203 | struct bpf_sock_ops_kern, sk), | |
9204 | si->dst_reg, si->src_reg, | |
9205 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9206 | *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg, | |
9207 | offsetof(struct sock_common, skc_state)); | |
9208 | break; | |
9209 | ||
9210 | case offsetof(struct bpf_sock_ops, rtt_min): | |
c593642c | 9211 | BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) != |
44f0e430 LB |
9212 | sizeof(struct minmax)); |
9213 | BUILD_BUG_ON(sizeof(struct minmax) < | |
9214 | sizeof(struct minmax_sample)); | |
9215 | ||
9216 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
9217 | struct bpf_sock_ops_kern, sk), | |
9218 | si->dst_reg, si->src_reg, | |
9219 | offsetof(struct bpf_sock_ops_kern, sk)); | |
9220 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, | |
9221 | offsetof(struct tcp_sock, rtt_min) + | |
c593642c | 9222 | sizeof_field(struct minmax_sample, t)); |
44f0e430 LB |
9223 | break; |
9224 | ||
b13d8807 LB |
9225 | case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags): |
9226 | SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags, | |
9227 | struct tcp_sock); | |
9228 | break; | |
44f0e430 | 9229 | |
44f0e430 | 9230 | case offsetof(struct bpf_sock_ops, sk_txhash): |
6f9bd3d7 LB |
9231 | SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash, |
9232 | struct sock, type); | |
44f0e430 | 9233 | break; |
2377b81d SF |
9234 | case offsetof(struct bpf_sock_ops, snd_cwnd): |
9235 | SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd); | |
9236 | break; | |
9237 | case offsetof(struct bpf_sock_ops, srtt_us): | |
9238 | SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us); | |
9239 | break; | |
9240 | case offsetof(struct bpf_sock_ops, snd_ssthresh): | |
9241 | SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh); | |
9242 | break; | |
9243 | case offsetof(struct bpf_sock_ops, rcv_nxt): | |
9244 | SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt); | |
9245 | break; | |
9246 | case offsetof(struct bpf_sock_ops, snd_nxt): | |
9247 | SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt); | |
9248 | break; | |
9249 | case offsetof(struct bpf_sock_ops, snd_una): | |
9250 | SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una); | |
9251 | break; | |
9252 | case offsetof(struct bpf_sock_ops, mss_cache): | |
9253 | SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache); | |
9254 | break; | |
9255 | case offsetof(struct bpf_sock_ops, ecn_flags): | |
9256 | SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags); | |
9257 | break; | |
9258 | case offsetof(struct bpf_sock_ops, rate_delivered): | |
9259 | SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered); | |
9260 | break; | |
9261 | case offsetof(struct bpf_sock_ops, rate_interval_us): | |
9262 | SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us); | |
9263 | break; | |
9264 | case offsetof(struct bpf_sock_ops, packets_out): | |
9265 | SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out); | |
9266 | break; | |
9267 | case offsetof(struct bpf_sock_ops, retrans_out): | |
9268 | SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out); | |
9269 | break; | |
9270 | case offsetof(struct bpf_sock_ops, total_retrans): | |
9271 | SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans); | |
9272 | break; | |
9273 | case offsetof(struct bpf_sock_ops, segs_in): | |
9274 | SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in); | |
9275 | break; | |
9276 | case offsetof(struct bpf_sock_ops, data_segs_in): | |
9277 | SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in); | |
9278 | break; | |
9279 | case offsetof(struct bpf_sock_ops, segs_out): | |
9280 | SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out); | |
9281 | break; | |
9282 | case offsetof(struct bpf_sock_ops, data_segs_out): | |
9283 | SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out); | |
9284 | break; | |
9285 | case offsetof(struct bpf_sock_ops, lost_out): | |
9286 | SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out); | |
9287 | break; | |
9288 | case offsetof(struct bpf_sock_ops, sacked_out): | |
9289 | SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out); | |
9290 | break; | |
9291 | case offsetof(struct bpf_sock_ops, bytes_received): | |
9292 | SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received); | |
9293 | break; | |
9294 | case offsetof(struct bpf_sock_ops, bytes_acked): | |
9295 | SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked); | |
9296 | break; | |
1314ef56 | 9297 | case offsetof(struct bpf_sock_ops, sk): |
84f44df6 | 9298 | SOCK_OPS_GET_SK(); |
1314ef56 | 9299 | break; |
0813a841 MKL |
9300 | case offsetof(struct bpf_sock_ops, skb_data_end): |
9301 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, | |
9302 | skb_data_end), | |
9303 | si->dst_reg, si->src_reg, | |
9304 | offsetof(struct bpf_sock_ops_kern, | |
9305 | skb_data_end)); | |
9306 | break; | |
9307 | case offsetof(struct bpf_sock_ops, skb_data): | |
9308 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, | |
9309 | skb), | |
9310 | si->dst_reg, si->src_reg, | |
9311 | offsetof(struct bpf_sock_ops_kern, | |
9312 | skb)); | |
9313 | *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); | |
9314 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data), | |
9315 | si->dst_reg, si->dst_reg, | |
9316 | offsetof(struct sk_buff, data)); | |
9317 | break; | |
9318 | case offsetof(struct bpf_sock_ops, skb_len): | |
9319 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, | |
9320 | skb), | |
9321 | si->dst_reg, si->src_reg, | |
9322 | offsetof(struct bpf_sock_ops_kern, | |
9323 | skb)); | |
9324 | *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); | |
9325 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len), | |
9326 | si->dst_reg, si->dst_reg, | |
9327 | offsetof(struct sk_buff, len)); | |
9328 | break; | |
9329 | case offsetof(struct bpf_sock_ops, skb_tcp_flags): | |
9330 | off = offsetof(struct sk_buff, cb); | |
9331 | off += offsetof(struct tcp_skb_cb, tcp_flags); | |
9332 | *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags); | |
9333 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern, | |
9334 | skb), | |
9335 | si->dst_reg, si->src_reg, | |
9336 | offsetof(struct bpf_sock_ops_kern, | |
9337 | skb)); | |
9338 | *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); | |
9339 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb, | |
9340 | tcp_flags), | |
9341 | si->dst_reg, si->dst_reg, off); | |
9342 | break; | |
40304b2a LB |
9343 | } |
9344 | return insn - insn_buf; | |
9345 | } | |
9346 | ||
8108a775 JF |
9347 | static u32 sk_skb_convert_ctx_access(enum bpf_access_type type, |
9348 | const struct bpf_insn *si, | |
9349 | struct bpf_insn *insn_buf, | |
9350 | struct bpf_prog *prog, u32 *target_size) | |
9351 | { | |
9352 | struct bpf_insn *insn = insn_buf; | |
9353 | int off; | |
9354 | ||
9355 | switch (si->off) { | |
9356 | case offsetof(struct __sk_buff, data_end): | |
9357 | off = si->off; | |
9358 | off -= offsetof(struct __sk_buff, data_end); | |
9359 | off += offsetof(struct sk_buff, cb); | |
9360 | off += offsetof(struct tcp_skb_cb, bpf.data_end); | |
9361 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, | |
9362 | si->src_reg, off); | |
9363 | break; | |
9364 | default: | |
9365 | return bpf_convert_ctx_access(type, si, insn_buf, prog, | |
9366 | target_size); | |
9367 | } | |
9368 | ||
9369 | return insn - insn_buf; | |
9370 | } | |
9371 | ||
4f738adb JF |
9372 | static u32 sk_msg_convert_ctx_access(enum bpf_access_type type, |
9373 | const struct bpf_insn *si, | |
9374 | struct bpf_insn *insn_buf, | |
9375 | struct bpf_prog *prog, u32 *target_size) | |
9376 | { | |
9377 | struct bpf_insn *insn = insn_buf; | |
720e7f38 | 9378 | #if IS_ENABLED(CONFIG_IPV6) |
303def35 | 9379 | int off; |
720e7f38 | 9380 | #endif |
4f738adb | 9381 | |
7a69c0f2 JF |
9382 | /* convert ctx uses the fact sg element is first in struct */ |
9383 | BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0); | |
9384 | ||
4f738adb JF |
9385 | switch (si->off) { |
9386 | case offsetof(struct sk_msg_md, data): | |
604326b4 | 9387 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data), |
4f738adb | 9388 | si->dst_reg, si->src_reg, |
604326b4 | 9389 | offsetof(struct sk_msg, data)); |
4f738adb JF |
9390 | break; |
9391 | case offsetof(struct sk_msg_md, data_end): | |
604326b4 | 9392 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end), |
4f738adb | 9393 | si->dst_reg, si->src_reg, |
604326b4 | 9394 | offsetof(struct sk_msg, data_end)); |
4f738adb | 9395 | break; |
303def35 | 9396 | case offsetof(struct sk_msg_md, family): |
c593642c | 9397 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2); |
303def35 JF |
9398 | |
9399 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
604326b4 | 9400 | struct sk_msg, sk), |
303def35 | 9401 | si->dst_reg, si->src_reg, |
604326b4 | 9402 | offsetof(struct sk_msg, sk)); |
303def35 JF |
9403 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, |
9404 | offsetof(struct sock_common, skc_family)); | |
9405 | break; | |
9406 | ||
9407 | case offsetof(struct sk_msg_md, remote_ip4): | |
c593642c | 9408 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4); |
303def35 JF |
9409 | |
9410 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
604326b4 | 9411 | struct sk_msg, sk), |
303def35 | 9412 | si->dst_reg, si->src_reg, |
604326b4 | 9413 | offsetof(struct sk_msg, sk)); |
303def35 JF |
9414 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, |
9415 | offsetof(struct sock_common, skc_daddr)); | |
9416 | break; | |
9417 | ||
9418 | case offsetof(struct sk_msg_md, local_ip4): | |
c593642c | 9419 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
303def35 JF |
9420 | skc_rcv_saddr) != 4); |
9421 | ||
9422 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
604326b4 | 9423 | struct sk_msg, sk), |
303def35 | 9424 | si->dst_reg, si->src_reg, |
604326b4 | 9425 | offsetof(struct sk_msg, sk)); |
303def35 JF |
9426 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, |
9427 | offsetof(struct sock_common, | |
9428 | skc_rcv_saddr)); | |
9429 | break; | |
9430 | ||
9431 | case offsetof(struct sk_msg_md, remote_ip6[0]) ... | |
9432 | offsetof(struct sk_msg_md, remote_ip6[3]): | |
9433 | #if IS_ENABLED(CONFIG_IPV6) | |
c593642c | 9434 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
303def35 JF |
9435 | skc_v6_daddr.s6_addr32[0]) != 4); |
9436 | ||
9437 | off = si->off; | |
9438 | off -= offsetof(struct sk_msg_md, remote_ip6[0]); | |
9439 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
604326b4 | 9440 | struct sk_msg, sk), |
303def35 | 9441 | si->dst_reg, si->src_reg, |
604326b4 | 9442 | offsetof(struct sk_msg, sk)); |
303def35 JF |
9443 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, |
9444 | offsetof(struct sock_common, | |
9445 | skc_v6_daddr.s6_addr32[0]) + | |
9446 | off); | |
9447 | #else | |
9448 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
9449 | #endif | |
9450 | break; | |
9451 | ||
9452 | case offsetof(struct sk_msg_md, local_ip6[0]) ... | |
9453 | offsetof(struct sk_msg_md, local_ip6[3]): | |
9454 | #if IS_ENABLED(CONFIG_IPV6) | |
c593642c | 9455 | BUILD_BUG_ON(sizeof_field(struct sock_common, |
303def35 JF |
9456 | skc_v6_rcv_saddr.s6_addr32[0]) != 4); |
9457 | ||
9458 | off = si->off; | |
9459 | off -= offsetof(struct sk_msg_md, local_ip6[0]); | |
9460 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
604326b4 | 9461 | struct sk_msg, sk), |
303def35 | 9462 | si->dst_reg, si->src_reg, |
604326b4 | 9463 | offsetof(struct sk_msg, sk)); |
303def35 JF |
9464 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, |
9465 | offsetof(struct sock_common, | |
9466 | skc_v6_rcv_saddr.s6_addr32[0]) + | |
9467 | off); | |
9468 | #else | |
9469 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
9470 | #endif | |
9471 | break; | |
9472 | ||
9473 | case offsetof(struct sk_msg_md, remote_port): | |
c593642c | 9474 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2); |
303def35 JF |
9475 | |
9476 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
604326b4 | 9477 | struct sk_msg, sk), |
303def35 | 9478 | si->dst_reg, si->src_reg, |
604326b4 | 9479 | offsetof(struct sk_msg, sk)); |
303def35 JF |
9480 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, |
9481 | offsetof(struct sock_common, skc_dport)); | |
9482 | #ifndef __BIG_ENDIAN_BITFIELD | |
9483 | *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16); | |
9484 | #endif | |
9485 | break; | |
9486 | ||
9487 | case offsetof(struct sk_msg_md, local_port): | |
c593642c | 9488 | BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2); |
303def35 JF |
9489 | |
9490 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( | |
604326b4 | 9491 | struct sk_msg, sk), |
303def35 | 9492 | si->dst_reg, si->src_reg, |
604326b4 | 9493 | offsetof(struct sk_msg, sk)); |
303def35 JF |
9494 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg, |
9495 | offsetof(struct sock_common, skc_num)); | |
9496 | break; | |
3bdbd022 JF |
9497 | |
9498 | case offsetof(struct sk_msg_md, size): | |
9499 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size), | |
9500 | si->dst_reg, si->src_reg, | |
9501 | offsetof(struct sk_msg_sg, size)); | |
9502 | break; | |
13d70f5a JF |
9503 | |
9504 | case offsetof(struct sk_msg_md, sk): | |
9505 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk), | |
9506 | si->dst_reg, si->src_reg, | |
9507 | offsetof(struct sk_msg, sk)); | |
9508 | break; | |
4f738adb JF |
9509 | } |
9510 | ||
9511 | return insn - insn_buf; | |
9512 | } | |
9513 | ||
7de16e3a | 9514 | const struct bpf_verifier_ops sk_filter_verifier_ops = { |
4936e352 DB |
9515 | .get_func_proto = sk_filter_func_proto, |
9516 | .is_valid_access = sk_filter_is_valid_access, | |
2492d3b8 | 9517 | .convert_ctx_access = bpf_convert_ctx_access, |
e0cea7ce | 9518 | .gen_ld_abs = bpf_gen_ld_abs, |
89aa0758 AS |
9519 | }; |
9520 | ||
7de16e3a | 9521 | const struct bpf_prog_ops sk_filter_prog_ops = { |
61f3c964 | 9522 | .test_run = bpf_prog_test_run_skb, |
7de16e3a JK |
9523 | }; |
9524 | ||
9525 | const struct bpf_verifier_ops tc_cls_act_verifier_ops = { | |
4936e352 DB |
9526 | .get_func_proto = tc_cls_act_func_proto, |
9527 | .is_valid_access = tc_cls_act_is_valid_access, | |
374fb54e | 9528 | .convert_ctx_access = tc_cls_act_convert_ctx_access, |
36bbef52 | 9529 | .gen_prologue = tc_cls_act_prologue, |
e0cea7ce | 9530 | .gen_ld_abs = bpf_gen_ld_abs, |
7de16e3a JK |
9531 | }; |
9532 | ||
9533 | const struct bpf_prog_ops tc_cls_act_prog_ops = { | |
1cf1cae9 | 9534 | .test_run = bpf_prog_test_run_skb, |
608cd71a AS |
9535 | }; |
9536 | ||
7de16e3a | 9537 | const struct bpf_verifier_ops xdp_verifier_ops = { |
6a773a15 BB |
9538 | .get_func_proto = xdp_func_proto, |
9539 | .is_valid_access = xdp_is_valid_access, | |
9540 | .convert_ctx_access = xdp_convert_ctx_access, | |
b09928b9 | 9541 | .gen_prologue = bpf_noop_prologue, |
7de16e3a JK |
9542 | }; |
9543 | ||
9544 | const struct bpf_prog_ops xdp_prog_ops = { | |
1cf1cae9 | 9545 | .test_run = bpf_prog_test_run_xdp, |
6a773a15 BB |
9546 | }; |
9547 | ||
7de16e3a | 9548 | const struct bpf_verifier_ops cg_skb_verifier_ops = { |
cd339431 | 9549 | .get_func_proto = cg_skb_func_proto, |
b39b5f41 | 9550 | .is_valid_access = cg_skb_is_valid_access, |
2492d3b8 | 9551 | .convert_ctx_access = bpf_convert_ctx_access, |
7de16e3a JK |
9552 | }; |
9553 | ||
9554 | const struct bpf_prog_ops cg_skb_prog_ops = { | |
1cf1cae9 | 9555 | .test_run = bpf_prog_test_run_skb, |
0e33661d DM |
9556 | }; |
9557 | ||
cd3092c7 MX |
9558 | const struct bpf_verifier_ops lwt_in_verifier_ops = { |
9559 | .get_func_proto = lwt_in_func_proto, | |
3a0af8fd | 9560 | .is_valid_access = lwt_is_valid_access, |
2492d3b8 | 9561 | .convert_ctx_access = bpf_convert_ctx_access, |
7de16e3a JK |
9562 | }; |
9563 | ||
cd3092c7 MX |
9564 | const struct bpf_prog_ops lwt_in_prog_ops = { |
9565 | .test_run = bpf_prog_test_run_skb, | |
9566 | }; | |
9567 | ||
9568 | const struct bpf_verifier_ops lwt_out_verifier_ops = { | |
9569 | .get_func_proto = lwt_out_func_proto, | |
3a0af8fd | 9570 | .is_valid_access = lwt_is_valid_access, |
2492d3b8 | 9571 | .convert_ctx_access = bpf_convert_ctx_access, |
7de16e3a JK |
9572 | }; |
9573 | ||
cd3092c7 | 9574 | const struct bpf_prog_ops lwt_out_prog_ops = { |
1cf1cae9 | 9575 | .test_run = bpf_prog_test_run_skb, |
3a0af8fd TG |
9576 | }; |
9577 | ||
7de16e3a | 9578 | const struct bpf_verifier_ops lwt_xmit_verifier_ops = { |
3a0af8fd TG |
9579 | .get_func_proto = lwt_xmit_func_proto, |
9580 | .is_valid_access = lwt_is_valid_access, | |
2492d3b8 | 9581 | .convert_ctx_access = bpf_convert_ctx_access, |
3a0af8fd | 9582 | .gen_prologue = tc_cls_act_prologue, |
7de16e3a JK |
9583 | }; |
9584 | ||
9585 | const struct bpf_prog_ops lwt_xmit_prog_ops = { | |
1cf1cae9 | 9586 | .test_run = bpf_prog_test_run_skb, |
3a0af8fd TG |
9587 | }; |
9588 | ||
004d4b27 MX |
9589 | const struct bpf_verifier_ops lwt_seg6local_verifier_ops = { |
9590 | .get_func_proto = lwt_seg6local_func_proto, | |
9591 | .is_valid_access = lwt_is_valid_access, | |
9592 | .convert_ctx_access = bpf_convert_ctx_access, | |
9593 | }; | |
9594 | ||
9595 | const struct bpf_prog_ops lwt_seg6local_prog_ops = { | |
9596 | .test_run = bpf_prog_test_run_skb, | |
9597 | }; | |
9598 | ||
7de16e3a | 9599 | const struct bpf_verifier_ops cg_sock_verifier_ops = { |
ae2cf1c4 | 9600 | .get_func_proto = sock_filter_func_proto, |
61023658 | 9601 | .is_valid_access = sock_filter_is_valid_access, |
c64b7983 | 9602 | .convert_ctx_access = bpf_sock_convert_ctx_access, |
61023658 DA |
9603 | }; |
9604 | ||
7de16e3a JK |
9605 | const struct bpf_prog_ops cg_sock_prog_ops = { |
9606 | }; | |
9607 | ||
4fbac77d AI |
9608 | const struct bpf_verifier_ops cg_sock_addr_verifier_ops = { |
9609 | .get_func_proto = sock_addr_func_proto, | |
9610 | .is_valid_access = sock_addr_is_valid_access, | |
9611 | .convert_ctx_access = sock_addr_convert_ctx_access, | |
9612 | }; | |
9613 | ||
9614 | const struct bpf_prog_ops cg_sock_addr_prog_ops = { | |
9615 | }; | |
9616 | ||
7de16e3a | 9617 | const struct bpf_verifier_ops sock_ops_verifier_ops = { |
8c4b4c7e | 9618 | .get_func_proto = sock_ops_func_proto, |
40304b2a LB |
9619 | .is_valid_access = sock_ops_is_valid_access, |
9620 | .convert_ctx_access = sock_ops_convert_ctx_access, | |
9621 | }; | |
9622 | ||
7de16e3a JK |
9623 | const struct bpf_prog_ops sock_ops_prog_ops = { |
9624 | }; | |
9625 | ||
9626 | const struct bpf_verifier_ops sk_skb_verifier_ops = { | |
b005fd18 JF |
9627 | .get_func_proto = sk_skb_func_proto, |
9628 | .is_valid_access = sk_skb_is_valid_access, | |
8108a775 | 9629 | .convert_ctx_access = sk_skb_convert_ctx_access, |
8a31db56 | 9630 | .gen_prologue = sk_skb_prologue, |
b005fd18 JF |
9631 | }; |
9632 | ||
7de16e3a JK |
9633 | const struct bpf_prog_ops sk_skb_prog_ops = { |
9634 | }; | |
9635 | ||
4f738adb JF |
9636 | const struct bpf_verifier_ops sk_msg_verifier_ops = { |
9637 | .get_func_proto = sk_msg_func_proto, | |
9638 | .is_valid_access = sk_msg_is_valid_access, | |
9639 | .convert_ctx_access = sk_msg_convert_ctx_access, | |
b09928b9 | 9640 | .gen_prologue = bpf_noop_prologue, |
4f738adb JF |
9641 | }; |
9642 | ||
9643 | const struct bpf_prog_ops sk_msg_prog_ops = { | |
9644 | }; | |
9645 | ||
d58e468b PP |
9646 | const struct bpf_verifier_ops flow_dissector_verifier_ops = { |
9647 | .get_func_proto = flow_dissector_func_proto, | |
9648 | .is_valid_access = flow_dissector_is_valid_access, | |
089b19a9 | 9649 | .convert_ctx_access = flow_dissector_convert_ctx_access, |
d58e468b PP |
9650 | }; |
9651 | ||
9652 | const struct bpf_prog_ops flow_dissector_prog_ops = { | |
b7a1848e | 9653 | .test_run = bpf_prog_test_run_flow_dissector, |
d58e468b PP |
9654 | }; |
9655 | ||
8ced425e | 9656 | int sk_detach_filter(struct sock *sk) |
55b33325 PE |
9657 | { |
9658 | int ret = -ENOENT; | |
9659 | struct sk_filter *filter; | |
9660 | ||
d59577b6 VB |
9661 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) |
9662 | return -EPERM; | |
9663 | ||
8ced425e HFS |
9664 | filter = rcu_dereference_protected(sk->sk_filter, |
9665 | lockdep_sock_is_held(sk)); | |
55b33325 | 9666 | if (filter) { |
a9b3cd7f | 9667 | RCU_INIT_POINTER(sk->sk_filter, NULL); |
46bcf14f | 9668 | sk_filter_uncharge(sk, filter); |
55b33325 PE |
9669 | ret = 0; |
9670 | } | |
a3ea269b | 9671 | |
55b33325 PE |
9672 | return ret; |
9673 | } | |
8ced425e | 9674 | EXPORT_SYMBOL_GPL(sk_detach_filter); |
a8fc9277 | 9675 | |
a3ea269b DB |
9676 | int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, |
9677 | unsigned int len) | |
a8fc9277 | 9678 | { |
a3ea269b | 9679 | struct sock_fprog_kern *fprog; |
a8fc9277 | 9680 | struct sk_filter *filter; |
a3ea269b | 9681 | int ret = 0; |
a8fc9277 PE |
9682 | |
9683 | lock_sock(sk); | |
9684 | filter = rcu_dereference_protected(sk->sk_filter, | |
8ced425e | 9685 | lockdep_sock_is_held(sk)); |
a8fc9277 PE |
9686 | if (!filter) |
9687 | goto out; | |
a3ea269b DB |
9688 | |
9689 | /* We're copying the filter that has been originally attached, | |
93d08b69 DB |
9690 | * so no conversion/decode needed anymore. eBPF programs that |
9691 | * have no original program cannot be dumped through this. | |
a3ea269b | 9692 | */ |
93d08b69 | 9693 | ret = -EACCES; |
7ae457c1 | 9694 | fprog = filter->prog->orig_prog; |
93d08b69 DB |
9695 | if (!fprog) |
9696 | goto out; | |
a3ea269b DB |
9697 | |
9698 | ret = fprog->len; | |
a8fc9277 | 9699 | if (!len) |
a3ea269b | 9700 | /* User space only enquires number of filter blocks. */ |
a8fc9277 | 9701 | goto out; |
a3ea269b | 9702 | |
a8fc9277 | 9703 | ret = -EINVAL; |
a3ea269b | 9704 | if (len < fprog->len) |
a8fc9277 PE |
9705 | goto out; |
9706 | ||
9707 | ret = -EFAULT; | |
009937e7 | 9708 | if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog))) |
a3ea269b | 9709 | goto out; |
a8fc9277 | 9710 | |
a3ea269b DB |
9711 | /* Instead of bytes, the API requests to return the number |
9712 | * of filter blocks. | |
9713 | */ | |
9714 | ret = fprog->len; | |
a8fc9277 PE |
9715 | out: |
9716 | release_sock(sk); | |
9717 | return ret; | |
9718 | } | |
2dbb9b9e MKL |
9719 | |
9720 | #ifdef CONFIG_INET | |
2dbb9b9e MKL |
9721 | static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern, |
9722 | struct sock_reuseport *reuse, | |
9723 | struct sock *sk, struct sk_buff *skb, | |
9724 | u32 hash) | |
9725 | { | |
9726 | reuse_kern->skb = skb; | |
9727 | reuse_kern->sk = sk; | |
9728 | reuse_kern->selected_sk = NULL; | |
9729 | reuse_kern->data_end = skb->data + skb_headlen(skb); | |
9730 | reuse_kern->hash = hash; | |
9731 | reuse_kern->reuseport_id = reuse->reuseport_id; | |
9732 | reuse_kern->bind_inany = reuse->bind_inany; | |
9733 | } | |
9734 | ||
9735 | struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk, | |
9736 | struct bpf_prog *prog, struct sk_buff *skb, | |
9737 | u32 hash) | |
9738 | { | |
9739 | struct sk_reuseport_kern reuse_kern; | |
9740 | enum sk_action action; | |
9741 | ||
9742 | bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash); | |
9743 | action = BPF_PROG_RUN(prog, &reuse_kern); | |
9744 | ||
9745 | if (action == SK_PASS) | |
9746 | return reuse_kern.selected_sk; | |
9747 | else | |
9748 | return ERR_PTR(-ECONNREFUSED); | |
9749 | } | |
9750 | ||
9751 | BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern, | |
9752 | struct bpf_map *, map, void *, key, u32, flags) | |
9753 | { | |
9fed9000 | 9754 | bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY; |
2dbb9b9e MKL |
9755 | struct sock_reuseport *reuse; |
9756 | struct sock *selected_sk; | |
9757 | ||
9758 | selected_sk = map->ops->map_lookup_elem(map, key); | |
9759 | if (!selected_sk) | |
9760 | return -ENOENT; | |
9761 | ||
9762 | reuse = rcu_dereference(selected_sk->sk_reuseport_cb); | |
9fed9000 | 9763 | if (!reuse) { |
64d85290 JS |
9764 | /* Lookup in sock_map can return TCP ESTABLISHED sockets. */ |
9765 | if (sk_is_refcounted(selected_sk)) | |
9766 | sock_put(selected_sk); | |
9767 | ||
9fed9000 JS |
9768 | /* reuseport_array has only sk with non NULL sk_reuseport_cb. |
9769 | * The only (!reuse) case here is - the sk has already been | |
9770 | * unhashed (e.g. by close()), so treat it as -ENOENT. | |
9771 | * | |
9772 | * Other maps (e.g. sock_map) do not provide this guarantee and | |
9773 | * the sk may never be in the reuseport group to begin with. | |
2dbb9b9e | 9774 | */ |
9fed9000 JS |
9775 | return is_sockarray ? -ENOENT : -EINVAL; |
9776 | } | |
2dbb9b9e MKL |
9777 | |
9778 | if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) { | |
035ff358 | 9779 | struct sock *sk = reuse_kern->sk; |
2dbb9b9e | 9780 | |
2dbb9b9e MKL |
9781 | if (sk->sk_protocol != selected_sk->sk_protocol) |
9782 | return -EPROTOTYPE; | |
9783 | else if (sk->sk_family != selected_sk->sk_family) | |
9784 | return -EAFNOSUPPORT; | |
9785 | ||
9786 | /* Catch all. Likely bound to a different sockaddr. */ | |
9787 | return -EBADFD; | |
9788 | } | |
9789 | ||
9790 | reuse_kern->selected_sk = selected_sk; | |
9791 | ||
9792 | return 0; | |
9793 | } | |
9794 | ||
9795 | static const struct bpf_func_proto sk_select_reuseport_proto = { | |
9796 | .func = sk_select_reuseport, | |
9797 | .gpl_only = false, | |
9798 | .ret_type = RET_INTEGER, | |
9799 | .arg1_type = ARG_PTR_TO_CTX, | |
9800 | .arg2_type = ARG_CONST_MAP_PTR, | |
9801 | .arg3_type = ARG_PTR_TO_MAP_KEY, | |
9802 | .arg4_type = ARG_ANYTHING, | |
9803 | }; | |
9804 | ||
9805 | BPF_CALL_4(sk_reuseport_load_bytes, | |
9806 | const struct sk_reuseport_kern *, reuse_kern, u32, offset, | |
9807 | void *, to, u32, len) | |
9808 | { | |
9809 | return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len); | |
9810 | } | |
9811 | ||
9812 | static const struct bpf_func_proto sk_reuseport_load_bytes_proto = { | |
9813 | .func = sk_reuseport_load_bytes, | |
9814 | .gpl_only = false, | |
9815 | .ret_type = RET_INTEGER, | |
9816 | .arg1_type = ARG_PTR_TO_CTX, | |
9817 | .arg2_type = ARG_ANYTHING, | |
9818 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, | |
9819 | .arg4_type = ARG_CONST_SIZE, | |
9820 | }; | |
9821 | ||
9822 | BPF_CALL_5(sk_reuseport_load_bytes_relative, | |
9823 | const struct sk_reuseport_kern *, reuse_kern, u32, offset, | |
9824 | void *, to, u32, len, u32, start_header) | |
9825 | { | |
9826 | return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to, | |
9827 | len, start_header); | |
9828 | } | |
9829 | ||
9830 | static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = { | |
9831 | .func = sk_reuseport_load_bytes_relative, | |
9832 | .gpl_only = false, | |
9833 | .ret_type = RET_INTEGER, | |
9834 | .arg1_type = ARG_PTR_TO_CTX, | |
9835 | .arg2_type = ARG_ANYTHING, | |
9836 | .arg3_type = ARG_PTR_TO_UNINIT_MEM, | |
9837 | .arg4_type = ARG_CONST_SIZE, | |
9838 | .arg5_type = ARG_ANYTHING, | |
9839 | }; | |
9840 | ||
9841 | static const struct bpf_func_proto * | |
9842 | sk_reuseport_func_proto(enum bpf_func_id func_id, | |
9843 | const struct bpf_prog *prog) | |
9844 | { | |
9845 | switch (func_id) { | |
9846 | case BPF_FUNC_sk_select_reuseport: | |
9847 | return &sk_select_reuseport_proto; | |
9848 | case BPF_FUNC_skb_load_bytes: | |
9849 | return &sk_reuseport_load_bytes_proto; | |
9850 | case BPF_FUNC_skb_load_bytes_relative: | |
9851 | return &sk_reuseport_load_bytes_relative_proto; | |
9852 | default: | |
9853 | return bpf_base_func_proto(func_id); | |
9854 | } | |
9855 | } | |
9856 | ||
9857 | static bool | |
9858 | sk_reuseport_is_valid_access(int off, int size, | |
9859 | enum bpf_access_type type, | |
9860 | const struct bpf_prog *prog, | |
9861 | struct bpf_insn_access_aux *info) | |
9862 | { | |
9863 | const u32 size_default = sizeof(__u32); | |
9864 | ||
9865 | if (off < 0 || off >= sizeof(struct sk_reuseport_md) || | |
9866 | off % size || type != BPF_READ) | |
9867 | return false; | |
9868 | ||
9869 | switch (off) { | |
9870 | case offsetof(struct sk_reuseport_md, data): | |
9871 | info->reg_type = PTR_TO_PACKET; | |
9872 | return size == sizeof(__u64); | |
9873 | ||
9874 | case offsetof(struct sk_reuseport_md, data_end): | |
9875 | info->reg_type = PTR_TO_PACKET_END; | |
9876 | return size == sizeof(__u64); | |
9877 | ||
9878 | case offsetof(struct sk_reuseport_md, hash): | |
9879 | return size == size_default; | |
9880 | ||
9881 | /* Fields that allow narrowing */ | |
2c238177 | 9882 | case bpf_ctx_range(struct sk_reuseport_md, eth_protocol): |
c593642c | 9883 | if (size < sizeof_field(struct sk_buff, protocol)) |
2dbb9b9e | 9884 | return false; |
df561f66 | 9885 | fallthrough; |
2c238177 IL |
9886 | case bpf_ctx_range(struct sk_reuseport_md, ip_protocol): |
9887 | case bpf_ctx_range(struct sk_reuseport_md, bind_inany): | |
9888 | case bpf_ctx_range(struct sk_reuseport_md, len): | |
2dbb9b9e MKL |
9889 | bpf_ctx_record_field_size(info, size_default); |
9890 | return bpf_ctx_narrow_access_ok(off, size, size_default); | |
9891 | ||
9892 | default: | |
9893 | return false; | |
9894 | } | |
9895 | } | |
9896 | ||
9897 | #define SK_REUSEPORT_LOAD_FIELD(F) ({ \ | |
9898 | *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \ | |
9899 | si->dst_reg, si->src_reg, \ | |
9900 | bpf_target_off(struct sk_reuseport_kern, F, \ | |
c593642c | 9901 | sizeof_field(struct sk_reuseport_kern, F), \ |
2dbb9b9e MKL |
9902 | target_size)); \ |
9903 | }) | |
9904 | ||
9905 | #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \ | |
9906 | SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \ | |
9907 | struct sk_buff, \ | |
9908 | skb, \ | |
9909 | SKB_FIELD) | |
9910 | ||
bf976514 MM |
9911 | #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \ |
9912 | SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \ | |
9913 | struct sock, \ | |
9914 | sk, \ | |
9915 | SK_FIELD) | |
2dbb9b9e MKL |
9916 | |
9917 | static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type, | |
9918 | const struct bpf_insn *si, | |
9919 | struct bpf_insn *insn_buf, | |
9920 | struct bpf_prog *prog, | |
9921 | u32 *target_size) | |
9922 | { | |
9923 | struct bpf_insn *insn = insn_buf; | |
9924 | ||
9925 | switch (si->off) { | |
9926 | case offsetof(struct sk_reuseport_md, data): | |
9927 | SK_REUSEPORT_LOAD_SKB_FIELD(data); | |
9928 | break; | |
9929 | ||
9930 | case offsetof(struct sk_reuseport_md, len): | |
9931 | SK_REUSEPORT_LOAD_SKB_FIELD(len); | |
9932 | break; | |
9933 | ||
9934 | case offsetof(struct sk_reuseport_md, eth_protocol): | |
9935 | SK_REUSEPORT_LOAD_SKB_FIELD(protocol); | |
9936 | break; | |
9937 | ||
9938 | case offsetof(struct sk_reuseport_md, ip_protocol): | |
bf976514 | 9939 | SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol); |
2dbb9b9e MKL |
9940 | break; |
9941 | ||
9942 | case offsetof(struct sk_reuseport_md, data_end): | |
9943 | SK_REUSEPORT_LOAD_FIELD(data_end); | |
9944 | break; | |
9945 | ||
9946 | case offsetof(struct sk_reuseport_md, hash): | |
9947 | SK_REUSEPORT_LOAD_FIELD(hash); | |
9948 | break; | |
9949 | ||
9950 | case offsetof(struct sk_reuseport_md, bind_inany): | |
9951 | SK_REUSEPORT_LOAD_FIELD(bind_inany); | |
9952 | break; | |
9953 | } | |
9954 | ||
9955 | return insn - insn_buf; | |
9956 | } | |
9957 | ||
9958 | const struct bpf_verifier_ops sk_reuseport_verifier_ops = { | |
9959 | .get_func_proto = sk_reuseport_func_proto, | |
9960 | .is_valid_access = sk_reuseport_is_valid_access, | |
9961 | .convert_ctx_access = sk_reuseport_convert_ctx_access, | |
9962 | }; | |
9963 | ||
9964 | const struct bpf_prog_ops sk_reuseport_prog_ops = { | |
9965 | }; | |
7e6897f9 | 9966 | |
1559b4aa JS |
9967 | DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled); |
9968 | EXPORT_SYMBOL(bpf_sk_lookup_enabled); | |
7e6897f9 | 9969 | |
e9ddbb77 JS |
9970 | BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx, |
9971 | struct sock *, sk, u64, flags) | |
7e6897f9 | 9972 | { |
e9ddbb77 JS |
9973 | if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE | |
9974 | BPF_SK_LOOKUP_F_NO_REUSEPORT))) | |
9975 | return -EINVAL; | |
9976 | if (unlikely(sk && sk_is_refcounted(sk))) | |
9977 | return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */ | |
9978 | if (unlikely(sk && sk->sk_state == TCP_ESTABLISHED)) | |
9979 | return -ESOCKTNOSUPPORT; /* reject connected sockets */ | |
9980 | ||
9981 | /* Check if socket is suitable for packet L3/L4 protocol */ | |
9982 | if (sk && sk->sk_protocol != ctx->protocol) | |
9983 | return -EPROTOTYPE; | |
9984 | if (sk && sk->sk_family != ctx->family && | |
9985 | (sk->sk_family == AF_INET || ipv6_only_sock(sk))) | |
9986 | return -EAFNOSUPPORT; | |
9987 | ||
9988 | if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE)) | |
9989 | return -EEXIST; | |
9990 | ||
9991 | /* Select socket as lookup result */ | |
9992 | ctx->selected_sk = sk; | |
9993 | ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT; | |
9994 | return 0; | |
7e6897f9 | 9995 | } |
af7ec138 | 9996 | |
e9ddbb77 JS |
9997 | static const struct bpf_func_proto bpf_sk_lookup_assign_proto = { |
9998 | .func = bpf_sk_lookup_assign, | |
9999 | .gpl_only = false, | |
10000 | .ret_type = RET_INTEGER, | |
10001 | .arg1_type = ARG_PTR_TO_CTX, | |
10002 | .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL, | |
10003 | .arg3_type = ARG_ANYTHING, | |
af7ec138 YS |
10004 | }; |
10005 | ||
e9ddbb77 JS |
10006 | static const struct bpf_func_proto * |
10007 | sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
10008 | { | |
10009 | switch (func_id) { | |
10010 | case BPF_FUNC_perf_event_output: | |
10011 | return &bpf_event_output_data_proto; | |
10012 | case BPF_FUNC_sk_assign: | |
10013 | return &bpf_sk_lookup_assign_proto; | |
10014 | case BPF_FUNC_sk_release: | |
10015 | return &bpf_sk_release_proto; | |
10016 | default: | |
1df8f55a | 10017 | return bpf_sk_base_func_proto(func_id); |
e9ddbb77 JS |
10018 | } |
10019 | } | |
af7ec138 | 10020 | |
e9ddbb77 JS |
10021 | static bool sk_lookup_is_valid_access(int off, int size, |
10022 | enum bpf_access_type type, | |
10023 | const struct bpf_prog *prog, | |
10024 | struct bpf_insn_access_aux *info) | |
10025 | { | |
10026 | if (off < 0 || off >= sizeof(struct bpf_sk_lookup)) | |
10027 | return false; | |
10028 | if (off % size != 0) | |
10029 | return false; | |
10030 | if (type != BPF_READ) | |
10031 | return false; | |
10032 | ||
10033 | switch (off) { | |
10034 | case offsetof(struct bpf_sk_lookup, sk): | |
10035 | info->reg_type = PTR_TO_SOCKET_OR_NULL; | |
10036 | return size == sizeof(__u64); | |
af7ec138 | 10037 | |
e9ddbb77 JS |
10038 | case bpf_ctx_range(struct bpf_sk_lookup, family): |
10039 | case bpf_ctx_range(struct bpf_sk_lookup, protocol): | |
10040 | case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4): | |
10041 | case bpf_ctx_range(struct bpf_sk_lookup, local_ip4): | |
10042 | case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]): | |
10043 | case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]): | |
10044 | case bpf_ctx_range(struct bpf_sk_lookup, remote_port): | |
10045 | case bpf_ctx_range(struct bpf_sk_lookup, local_port): | |
10046 | bpf_ctx_record_field_size(info, sizeof(__u32)); | |
10047 | return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32)); | |
10048 | ||
10049 | default: | |
10050 | return false; | |
10051 | } | |
10052 | } | |
10053 | ||
10054 | static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type, | |
10055 | const struct bpf_insn *si, | |
10056 | struct bpf_insn *insn_buf, | |
10057 | struct bpf_prog *prog, | |
10058 | u32 *target_size) | |
af7ec138 | 10059 | { |
e9ddbb77 JS |
10060 | struct bpf_insn *insn = insn_buf; |
10061 | ||
10062 | switch (si->off) { | |
10063 | case offsetof(struct bpf_sk_lookup, sk): | |
10064 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, | |
10065 | offsetof(struct bpf_sk_lookup_kern, selected_sk)); | |
10066 | break; | |
af7ec138 | 10067 | |
e9ddbb77 JS |
10068 | case offsetof(struct bpf_sk_lookup, family): |
10069 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, | |
10070 | bpf_target_off(struct bpf_sk_lookup_kern, | |
10071 | family, 2, target_size)); | |
10072 | break; | |
10073 | ||
10074 | case offsetof(struct bpf_sk_lookup, protocol): | |
10075 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, | |
10076 | bpf_target_off(struct bpf_sk_lookup_kern, | |
10077 | protocol, 2, target_size)); | |
10078 | break; | |
10079 | ||
10080 | case offsetof(struct bpf_sk_lookup, remote_ip4): | |
10081 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
10082 | bpf_target_off(struct bpf_sk_lookup_kern, | |
10083 | v4.saddr, 4, target_size)); | |
10084 | break; | |
10085 | ||
10086 | case offsetof(struct bpf_sk_lookup, local_ip4): | |
10087 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, | |
10088 | bpf_target_off(struct bpf_sk_lookup_kern, | |
10089 | v4.daddr, 4, target_size)); | |
10090 | break; | |
10091 | ||
10092 | case bpf_ctx_range_till(struct bpf_sk_lookup, | |
10093 | remote_ip6[0], remote_ip6[3]): { | |
10094 | #if IS_ENABLED(CONFIG_IPV6) | |
10095 | int off = si->off; | |
10096 | ||
10097 | off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]); | |
10098 | off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size); | |
10099 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, | |
10100 | offsetof(struct bpf_sk_lookup_kern, v6.saddr)); | |
10101 | *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); | |
10102 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off); | |
10103 | #else | |
10104 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
10105 | #endif | |
10106 | break; | |
10107 | } | |
10108 | case bpf_ctx_range_till(struct bpf_sk_lookup, | |
10109 | local_ip6[0], local_ip6[3]): { | |
10110 | #if IS_ENABLED(CONFIG_IPV6) | |
10111 | int off = si->off; | |
10112 | ||
10113 | off -= offsetof(struct bpf_sk_lookup, local_ip6[0]); | |
10114 | off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size); | |
10115 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg, | |
10116 | offsetof(struct bpf_sk_lookup_kern, v6.daddr)); | |
10117 | *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1); | |
10118 | *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off); | |
10119 | #else | |
10120 | *insn++ = BPF_MOV32_IMM(si->dst_reg, 0); | |
10121 | #endif | |
10122 | break; | |
af7ec138 | 10123 | } |
e9ddbb77 JS |
10124 | case offsetof(struct bpf_sk_lookup, remote_port): |
10125 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, | |
10126 | bpf_target_off(struct bpf_sk_lookup_kern, | |
10127 | sport, 2, target_size)); | |
10128 | break; | |
10129 | ||
10130 | case offsetof(struct bpf_sk_lookup, local_port): | |
10131 | *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg, | |
10132 | bpf_target_off(struct bpf_sk_lookup_kern, | |
10133 | dport, 2, target_size)); | |
10134 | break; | |
10135 | } | |
10136 | ||
10137 | return insn - insn_buf; | |
af7ec138 | 10138 | } |
e9ddbb77 JS |
10139 | |
10140 | const struct bpf_prog_ops sk_lookup_prog_ops = { | |
10141 | }; | |
10142 | ||
10143 | const struct bpf_verifier_ops sk_lookup_verifier_ops = { | |
10144 | .get_func_proto = sk_lookup_func_proto, | |
10145 | .is_valid_access = sk_lookup_is_valid_access, | |
10146 | .convert_ctx_access = sk_lookup_convert_ctx_access, | |
10147 | }; | |
10148 | ||
2dbb9b9e | 10149 | #endif /* CONFIG_INET */ |
7e6897f9 | 10150 | |
6a64037d | 10151 | DEFINE_BPF_DISPATCHER(xdp) |
7e6897f9 BT |
10152 | |
10153 | void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog) | |
10154 | { | |
6a64037d | 10155 | bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog); |
7e6897f9 | 10156 | } |
af7ec138 | 10157 | |
bc4f0548 | 10158 | #ifdef CONFIG_DEBUG_INFO_BTF |
fce557bc | 10159 | BTF_ID_LIST_GLOBAL(btf_sock_ids) |
bc4f0548 | 10160 | #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type) |
af7ec138 YS |
10161 | BTF_SOCK_TYPE_xxx |
10162 | #undef BTF_SOCK_TYPE | |
bc4f0548 | 10163 | #else |
fce557bc | 10164 | u32 btf_sock_ids[MAX_BTF_SOCK_TYPE]; |
af7ec138 YS |
10165 | #endif |
10166 | ||
af7ec138 YS |
10167 | BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk) |
10168 | { | |
10169 | /* tcp6_sock type is not generated in dwarf and hence btf, | |
10170 | * trigger an explicit type generation here. | |
10171 | */ | |
10172 | BTF_TYPE_EMIT(struct tcp6_sock); | |
8c33dadc | 10173 | if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && |
af7ec138 YS |
10174 | sk->sk_family == AF_INET6) |
10175 | return (unsigned long)sk; | |
10176 | ||
10177 | return (unsigned long)NULL; | |
10178 | } | |
10179 | ||
10180 | const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = { | |
10181 | .func = bpf_skc_to_tcp6_sock, | |
10182 | .gpl_only = false, | |
10183 | .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, | |
1df8f55a | 10184 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
af7ec138 YS |
10185 | .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6], |
10186 | }; | |
478cfbdf YS |
10187 | |
10188 | BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk) | |
10189 | { | |
8c33dadc | 10190 | if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP) |
478cfbdf YS |
10191 | return (unsigned long)sk; |
10192 | ||
10193 | return (unsigned long)NULL; | |
10194 | } | |
10195 | ||
10196 | const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = { | |
10197 | .func = bpf_skc_to_tcp_sock, | |
10198 | .gpl_only = false, | |
10199 | .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, | |
1df8f55a | 10200 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
478cfbdf YS |
10201 | .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP], |
10202 | }; | |
10203 | ||
10204 | BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk) | |
10205 | { | |
6b207d66 | 10206 | #ifdef CONFIG_INET |
8c33dadc | 10207 | if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT) |
478cfbdf | 10208 | return (unsigned long)sk; |
6b207d66 | 10209 | #endif |
478cfbdf YS |
10210 | |
10211 | #if IS_BUILTIN(CONFIG_IPV6) | |
8c33dadc | 10212 | if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT) |
478cfbdf YS |
10213 | return (unsigned long)sk; |
10214 | #endif | |
10215 | ||
10216 | return (unsigned long)NULL; | |
10217 | } | |
10218 | ||
10219 | const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = { | |
10220 | .func = bpf_skc_to_tcp_timewait_sock, | |
10221 | .gpl_only = false, | |
10222 | .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, | |
1df8f55a | 10223 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
478cfbdf YS |
10224 | .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW], |
10225 | }; | |
10226 | ||
10227 | BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk) | |
10228 | { | |
6b207d66 | 10229 | #ifdef CONFIG_INET |
8c33dadc | 10230 | if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV) |
478cfbdf | 10231 | return (unsigned long)sk; |
6b207d66 | 10232 | #endif |
478cfbdf YS |
10233 | |
10234 | #if IS_BUILTIN(CONFIG_IPV6) | |
8c33dadc | 10235 | if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV) |
478cfbdf YS |
10236 | return (unsigned long)sk; |
10237 | #endif | |
10238 | ||
10239 | return (unsigned long)NULL; | |
10240 | } | |
10241 | ||
10242 | const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = { | |
10243 | .func = bpf_skc_to_tcp_request_sock, | |
10244 | .gpl_only = false, | |
10245 | .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, | |
1df8f55a | 10246 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
478cfbdf YS |
10247 | .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ], |
10248 | }; | |
0d4fad3e YS |
10249 | |
10250 | BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk) | |
10251 | { | |
10252 | /* udp6_sock type is not generated in dwarf and hence btf, | |
10253 | * trigger an explicit type generation here. | |
10254 | */ | |
10255 | BTF_TYPE_EMIT(struct udp6_sock); | |
8c33dadc | 10256 | if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP && |
0d4fad3e YS |
10257 | sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6) |
10258 | return (unsigned long)sk; | |
10259 | ||
10260 | return (unsigned long)NULL; | |
10261 | } | |
10262 | ||
10263 | const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = { | |
10264 | .func = bpf_skc_to_udp6_sock, | |
10265 | .gpl_only = false, | |
10266 | .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, | |
1df8f55a | 10267 | .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, |
0d4fad3e YS |
10268 | .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6], |
10269 | }; | |
1df8f55a MKL |
10270 | |
10271 | static const struct bpf_func_proto * | |
10272 | bpf_sk_base_func_proto(enum bpf_func_id func_id) | |
10273 | { | |
10274 | const struct bpf_func_proto *func; | |
10275 | ||
10276 | switch (func_id) { | |
10277 | case BPF_FUNC_skc_to_tcp6_sock: | |
10278 | func = &bpf_skc_to_tcp6_sock_proto; | |
10279 | break; | |
10280 | case BPF_FUNC_skc_to_tcp_sock: | |
10281 | func = &bpf_skc_to_tcp_sock_proto; | |
10282 | break; | |
10283 | case BPF_FUNC_skc_to_tcp_timewait_sock: | |
10284 | func = &bpf_skc_to_tcp_timewait_sock_proto; | |
10285 | break; | |
10286 | case BPF_FUNC_skc_to_tcp_request_sock: | |
10287 | func = &bpf_skc_to_tcp_request_sock_proto; | |
10288 | break; | |
10289 | case BPF_FUNC_skc_to_udp6_sock: | |
10290 | func = &bpf_skc_to_udp6_sock_proto; | |
10291 | break; | |
10292 | default: | |
10293 | return bpf_base_func_proto(func_id); | |
10294 | } | |
10295 | ||
10296 | if (!perfmon_capable()) | |
10297 | return NULL; | |
10298 | ||
10299 | return func; | |
10300 | } |