Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-only */
2 : /*
3 : * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #include "bpfilter/cgen/tc.h"
7 :
8 : #include <linux/bpf.h>
9 : #include <linux/bpf_common.h>
10 : #include <linux/pkt_cls.h>
11 :
12 : #include <stddef.h>
13 : #include <stdint.h>
14 :
15 : #include "bpfilter/cgen/cgen.h"
16 : #include "bpfilter/cgen/program.h"
17 : #include "bpfilter/cgen/stub.h"
18 : #include "core/btf.h"
19 : #include "core/flavor.h"
20 : #include "core/helper.h"
21 : #include "core/verdict.h"
22 :
23 : #include "external/filter.h"
24 :
25 0 : static int _bf_tc_gen_inline_prologue(struct bf_program *program)
26 : {
27 : int r;
28 :
29 0 : bf_assert(program);
30 :
31 : // Copy the packet size into the runtime context
32 0 : EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
33 : offsetof(struct __sk_buff, len)));
34 0 : EMIT(program,
35 : BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_3, BF_PROG_CTX_OFF(pkt_size)));
36 :
37 : /** The @c __sk_buff structure contains two fields related to the interface
38 : * index: @c ingress_ifindex and @c ifindex . @c ingress_ifindex is the
39 : * interface index the packet has been received on. However, we use
40 : * @c ifindex which is the interface index the packet is processed by: if
41 : * a packet is redirected locally from interface #1 to interface #2, then
42 : * @c ingress_ifindex will contain @c 1 but @c ifindex will contains @c 2 .
43 : * For egress, only @c ifindex is used.
44 : */
45 0 : if ((r = bf_btf_get_field_off("__sk_buff", "ifindex")) < 0)
46 : return r;
47 0 : EMIT(program, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, r));
48 0 : EMIT(program,
49 : BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, BF_PROG_CTX_OFF(ifindex)));
50 :
51 0 : r = bf_stub_make_ctx_skb_dynptr(program, BPF_REG_1);
52 0 : if (r)
53 : return r;
54 :
55 0 : r = bf_stub_parse_l2_ethhdr(program);
56 0 : if (r)
57 : return r;
58 :
59 0 : r = bf_stub_parse_l3_hdr(program);
60 0 : if (r)
61 : return r;
62 :
63 0 : r = bf_stub_parse_l4_hdr(program);
64 : if (r)
65 : return r;
66 :
67 : return 0;
68 : }
69 :
70 0 : static int _bf_tc_gen_inline_epilogue(struct bf_program *program)
71 : {
72 : UNUSED(program);
73 :
74 0 : return 0;
75 : }
76 :
77 : /**
78 : * Convert a standard verdict into a return value.
79 : *
80 : * @param verdict Verdict to convert. Must be valid.
81 : * @return TC return code corresponding to the verdict, as an integer.
82 : */
83 0 : static int _bf_tc_get_verdict(enum bf_verdict verdict)
84 : {
85 0 : bf_assert(0 <= verdict && verdict < _BF_TERMINAL_VERDICT_MAX);
86 :
87 : static const int verdicts[] = {
88 : [BF_VERDICT_ACCEPT] = TC_ACT_OK,
89 : [BF_VERDICT_DROP] = TC_ACT_SHOT,
90 : };
91 :
92 : static_assert(ARRAY_SIZE(verdicts) == _BF_TERMINAL_VERDICT_MAX);
93 :
94 0 : return verdicts[verdict];
95 : }
96 :
97 : const struct bf_flavor_ops bf_flavor_ops_tc = {
98 : .gen_inline_prologue = _bf_tc_gen_inline_prologue,
99 : .gen_inline_epilogue = _bf_tc_gen_inline_epilogue,
100 : .get_verdict = _bf_tc_get_verdict,
101 : };
|