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 : : /** Minimum interval between two log events for this rule, in nanoseconds.
63 : : * Zero means no rate limiting: every match is logged. */
64 : : uint64_t log_rate_ns;
65 : :
66 : : /** Mark to set to the packet's `sk_buff`. Only support for some hooks.
67 : : * The leftmost 32 bits are set to 1 if a mark is defined, or 0 otherwise.
68 : : * See `bf_rule_mark_is_set`. */
69 : : uint64_t mark;
70 : :
71 : : /** If set, the rule records matched elements count, and `bf_rule::counters`
72 : : * is valid. */
73 : : uint8_t has_counters:1;
74 : :
75 : : /** Element and size counters for matched traffic. Only valid if
76 : : * `has_counters == 1`. This field is used when reading a chain, not when
77 : : * creating it. This field is not serialized.
78 : : *
79 : : * @todo Use this field to allow users to initialize the rule's counters
80 : : * to a specific value. */
81 : : struct bf_counter counters;
82 : :
83 : : /** If true, skip this rule during flag calculation and code generation. */
84 : : bool disabled;
85 : :
86 : : enum bf_verdict verdict;
87 : :
88 : : /** Target interface index for REDIRECT verdict. 0 if not set. */
89 : : uint32_t redirect_ifindex;
90 : : /** Direction for REDIRECT verdict. */
91 : : enum bf_redirect_dir redirect_dir;
92 : : };
93 : :
94 : : static_assert(
95 : : _BF_LOG_OPT_MAX < 8,
96 : : "bf_log_opt has more than 8 values, it won't fit in bf_rule.log's 8 bits");
97 : :
98 : : /**
99 : : * Allocated and initialise a new rule.
100 : : *
101 : : * On failure, @p rule is left unchanged.
102 : : *
103 : : * @param rule On success, points to the allocated rule. Must be non NULL.
104 : : * @return 0 on success, or negative errno value on error.
105 : : */
106 : : int bf_rule_new(struct bf_rule **rule);
107 : :
108 : : /**
109 : : * @brief Allocate and initialize a new rule from serialized data.
110 : : *
111 : : * @param rule Rule object to allocate and initialize from the serialized
112 : : * data. The caller will own the object. On failure, `*rule` is
113 : : * unchanged. Can't be NULL.
114 : : * @param node Node containing the serialized rule. Can't be NULL.
115 : : * @return 0 on success, or a negative errno value on failure.
116 : : */
117 : : int bf_rule_new_from_pack(struct bf_rule **rule, bf_rpack_node_t node);
118 : :
119 : : /**
120 : : * Free a rule.
121 : : *
122 : : * Free @p rule and set it to NULL. If @p rule is NULL, nothing is done.
123 : : *
124 : : * @param rule Rule to free. Must be non-NULL.
125 : : */
126 : : void bf_rule_free(struct bf_rule **rule);
127 : :
128 : : /**
129 : : * @brief Serialize a rule.
130 : : *
131 : : * @param rule Rule to serialize. Can't be NULL.
132 : : * @param pack `bf_wpack_t` object to serialize the matcher rule. Can't be NULL.
133 : : * @return 0 on success, or a negative error value on failure.
134 : : */
135 : : int bf_rule_pack(const struct bf_rule *rule, bf_wpack_t *pack);
136 : :
137 : : /**
138 : : * Dump a rule.
139 : : *
140 : : * @param rule Rule to dump. Must not be NULL.
141 : : * @param prefix Prefix for each printed line.
142 : : */
143 : : void bf_rule_dump(const struct bf_rule *rule, prefix_t *prefix);
144 : :
145 : : /**
146 : : * Create a new matcher and add it to the rule.
147 : : *
148 : : * @param rule Rule to add the matcher to. Can't be NULL.
149 : : * @param type Matcher type.
150 : : * @param op Comparison operator.
151 : : * @param payload Payload of the matcher, its content and size depends on @p
152 : : * type . Can be NULL but only if @p payload_len is 0, in which case
153 : : * there is no payload.
154 : : * @param payload_len Length of the payload.
155 : : * @param negate If true, negate the matcher's comparison.
156 : : * @return 0 on success, or negative errno value on failure.
157 : : */
158 : : int bf_rule_add_matcher(struct bf_rule *rule, enum bf_matcher_type type,
159 : : enum bf_matcher_op op, const void *payload,
160 : : size_t payload_len, bool negate);
161 : :
162 : : static inline void bf_rule_mark_set(struct bf_rule *rule, uint32_t mark)
163 : : {
164 : : assert(rule);
165 : :
166 : 13 : rule->mark = ~BF_RULE_MARK_MASK | mark;
167 : : }
168 : :
169 : : static inline uint32_t bf_rule_mark_get(const struct bf_rule *rule)
170 : : {
171 : : assert(rule);
172 : :
173 : 10 : return rule->mark & BF_RULE_MARK_MASK;
174 : : }
175 : :
176 : : static inline bool bf_rule_mark_is_set(const struct bf_rule *rule)
177 : : {
178 : : assert(rule);
179 : :
180 [ + + ]: 10667 : return rule->mark >> 32;
181 : : }
182 : :
183 : : static inline void bf_rule_set_redirect(struct bf_rule *rule, uint32_t ifindex,
184 : : enum bf_redirect_dir dir)
185 : : {
186 : : assert(rule);
187 : :
188 : 13 : rule->redirect_ifindex = ifindex;
189 : 13 : rule->redirect_dir = dir;
190 : : }
191 : :
192 : : static inline bool bf_rule_has_redirect(const struct bf_rule *rule)
193 : : {
194 : : assert(rule);
195 : :
196 [ - + ]: 14 : return rule->redirect_ifindex != 0;
197 : : }
|