Branch data Line data Source code
1 : : /* SPDX-License-Identifier: GPL-2.0-only */
2 : : /*
3 : : * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
4 : : */
5 : :
6 : : #pragma once
7 : :
8 : : #include <stdbool.h>
9 : : #include <stddef.h>
10 : : #include <stdint.h>
11 : :
12 : : #include <bpfilter/core/list.h>
13 : : #include <bpfilter/counter.h>
14 : : #include <bpfilter/dump.h>
15 : : #include <bpfilter/matcher.h>
16 : : #include <bpfilter/pack.h>
17 : : #include <bpfilter/runtime.h>
18 : : #include <bpfilter/verdict.h>
19 : :
20 : : #define BF_RULE_MARK_MASK (0x00000000ffffffffULL)
21 : :
22 : : /**
23 : : * @brief Return the string representation of a `bf_log_opt` enumeration value.
24 : : *
25 : : * @param hdr `bf_log_opt` enumeration value.
26 : : * @return A pointer to the C-string representation of `hdr`.
27 : : */
28 : : const char *bf_log_opt_to_str(enum bf_log_opt hdr);
29 : :
30 : : /**
31 : : * @brief Return the `bf_log_opt` enumeration value corresponding to a string.
32 : : *
33 : : * @pre
34 : : * - `str` is a non-NULL pointer to a C-string.
35 : : * - `hdr != NULL`
36 : : * @post
37 : : * - On failure, `hdr` is unchanged.
38 : : *
39 : : * @param str String to get the corresponding `bf_log_opt` enumeration value for.
40 : : * @param hdr On success, contains the `bf_log_opt` enumeration value
41 : : * corresponding to `str`.
42 : : * @return 0 on success, or a negative error value on failure.
43 : : */
44 : : int bf_log_opt_from_str(const char *str, enum bf_log_opt *hdr);
45 : :
46 : : #define _free_bf_rule_ __attribute__((__cleanup__(bf_rule_free)))
47 : :
48 : : /**
49 : : * @struct bf_rule
50 : : *
51 : : * Represents a rule to match against packets.
52 : : *
53 : : * @var bf_rule::index
54 : : * Rule's index. Identifies the rule's within other rules from the same chain.
55 : : */
56 : : struct bf_rule
57 : : {
58 : : uint32_t index;
59 : : bf_list matchers;
60 : : uint8_t log;
61 : :
62 : : /** Mark to set to the packet's `sk_buff`. Only support for some hooks.
63 : : * The leftmost 32 bits are set to 1 if a mark is defined, or 0 otherwise.
64 : : * See `bf_rule_mark_is_set`. */
65 : : uint64_t mark;
66 : :
67 : : /** If set, the rule records matched elements count, and `bf_rule::counters`
68 : : * is valid. */
69 : : uint8_t has_counters:1;
70 : :
71 : : /** Element and size counters for matched traffic. Only valid if
72 : : * `has_counters == 1`. This field is used when reading a chain, not when
73 : : * creating it. This field is not serialized.
74 : : *
75 : : * @todo Use this field to allow users to initialize the rule's counters
76 : : * to a specific value. */
77 : : struct bf_counter counters;
78 : :
79 : : /** If true, skip this rule during flag calculation and code generation. */
80 : : bool disabled;
81 : :
82 : : enum bf_verdict verdict;
83 : :
84 : : /** Target interface index for REDIRECT verdict. 0 if not set. */
85 : : uint32_t redirect_ifindex;
86 : : /** Direction for REDIRECT verdict. */
87 : : enum bf_redirect_dir redirect_dir;
88 : : };
89 : :
90 : : static_assert(
91 : : _BF_LOG_OPT_MAX < 8,
92 : : "bf_log_opt has more than 8 values, it won't fit in bf_rule.log's 8 bits");
93 : :
94 : : /**
95 : : * Allocated and initialise a new rule.
96 : : *
97 : : * On failure, @p rule is left unchanged.
98 : : *
99 : : * @param rule On success, points to the allocated rule. Must be non NULL.
100 : : * @return 0 on success, or negative errno value on error.
101 : : */
102 : : int bf_rule_new(struct bf_rule **rule);
103 : :
104 : : /**
105 : : * @brief Allocate and initialize a new rule from serialized data.
106 : : *
107 : : * @param rule Rule object to allocate and initialize from the serialized
108 : : * data. The caller will own the object. On failure, `*rule` is
109 : : * unchanged. Can't be NULL.
110 : : * @param node Node containing the serialized rule. Can't be NULL.
111 : : * @return 0 on success, or a negative errno value on failure.
112 : : */
113 : : int bf_rule_new_from_pack(struct bf_rule **rule, bf_rpack_node_t node);
114 : :
115 : : /**
116 : : * Free a rule.
117 : : *
118 : : * Free @p rule and set it to NULL. If @p rule is NULL, nothing is done.
119 : : *
120 : : * @param rule Rule to free. Must be non-NULL.
121 : : */
122 : : void bf_rule_free(struct bf_rule **rule);
123 : :
124 : : /**
125 : : * @brief Serialize a rule.
126 : : *
127 : : * @param rule Rule to serialize. Can't be NULL.
128 : : * @param pack `bf_wpack_t` object to serialize the matcher rule. Can't be NULL.
129 : : * @return 0 on success, or a negative error value on failure.
130 : : */
131 : : int bf_rule_pack(const struct bf_rule *rule, bf_wpack_t *pack);
132 : :
133 : : /**
134 : : * Dump a rule.
135 : : *
136 : : * @param rule Rule to dump. Must not be NULL.
137 : : * @param prefix Prefix for each printed line.
138 : : */
139 : : void bf_rule_dump(const struct bf_rule *rule, prefix_t *prefix);
140 : :
141 : : /**
142 : : * Create a new matcher and add it to the rule.
143 : : *
144 : : * @param rule Rule to add the matcher to. Can't be NULL.
145 : : * @param type Matcher type.
146 : : * @param op Comparison operator.
147 : : * @param payload Payload of the matcher, its content and size depends on @p
148 : : * type . Can be NULL but only if @p payload_len is 0, in which case
149 : : * there is no payload.
150 : : * @param payload_len Length of the payload.
151 : : * @param negate If true, negate the matcher's comparison.
152 : : * @return 0 on success, or negative errno value on failure.
153 : : */
154 : : int bf_rule_add_matcher(struct bf_rule *rule, enum bf_matcher_type type,
155 : : enum bf_matcher_op op, const void *payload,
156 : : size_t payload_len, bool negate);
157 : :
158 : : static inline void bf_rule_mark_set(struct bf_rule *rule, uint32_t mark)
159 : : {
160 : : assert(rule);
161 : :
162 : 13 : rule->mark = ~BF_RULE_MARK_MASK | mark;
163 : 13 : }
164 : :
165 : : static inline uint32_t bf_rule_mark_get(const struct bf_rule *rule)
166 : : {
167 : : assert(rule);
168 : :
169 : 10 : return rule->mark & BF_RULE_MARK_MASK;
170 : : }
171 : :
172 : : static inline bool bf_rule_mark_is_set(const struct bf_rule *rule)
173 : : {
174 : : assert(rule);
175 : :
176 [ + + ]: 10653 : return rule->mark >> 32;
177 : : }
178 : :
179 : : static inline void bf_rule_set_redirect(struct bf_rule *rule, uint32_t ifindex,
180 : : enum bf_redirect_dir dir)
181 : : {
182 : : assert(rule);
183 : :
184 : 13 : rule->redirect_ifindex = ifindex;
185 : 13 : rule->redirect_dir = dir;
186 : 13 : }
187 : :
188 : : static inline bool bf_rule_has_redirect(const struct bf_rule *rule)
189 : : {
190 : : assert(rule);
191 : :
192 [ - + ]: 14 : return rule->redirect_ifindex != 0;
193 : : }
|