1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/rculist.h>
13 #include <linux/skbuff.h>
14 #include <linux/netlink.h>
15 #include <linux/netfilter.h>
16 #include <linux/static_key.h>
17 #include <linux/netfilter/nfnetlink.h>
18 #include <linux/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
20 #include <net/netfilter/nf_tables.h>
21 #include <net/netfilter/nf_log.h>
22 #include <net/netfilter/nft_meta.h>
24 static noinline void __nft_trace_packet(struct nft_traceinfo *info,
25 const struct nft_chain *chain,
26 enum nft_trace_types type)
28 if (!info->trace || !info->nf_trace)
34 nft_trace_notify(info);
37 static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
38 struct nft_traceinfo *info,
39 const struct nft_chain *chain,
40 const struct nft_rule_dp *rule,
41 enum nft_trace_types type)
43 if (static_branch_unlikely(&nft_trace_enabled)) {
44 info->nf_trace = pkt->skb->nf_trace;
46 __nft_trace_packet(info, chain, type);
50 static inline void nft_trace_copy_nftrace(const struct nft_pktinfo *pkt,
51 struct nft_traceinfo *info)
53 if (static_branch_unlikely(&nft_trace_enabled)) {
55 info->nf_trace = pkt->skb->nf_trace;
59 static void nft_bitwise_fast_eval(const struct nft_expr *expr,
60 struct nft_regs *regs)
62 const struct nft_bitwise_fast_expr *priv = nft_expr_priv(expr);
63 u32 *src = ®s->data[priv->sreg];
64 u32 *dst = ®s->data[priv->dreg];
66 *dst = (*src & priv->mask) ^ priv->xor;
69 static void nft_cmp_fast_eval(const struct nft_expr *expr,
70 struct nft_regs *regs)
72 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
74 if (((regs->data[priv->sreg] & priv->mask) == priv->data) ^ priv->inv)
76 regs->verdict.code = NFT_BREAK;
79 static void nft_cmp16_fast_eval(const struct nft_expr *expr,
80 struct nft_regs *regs)
82 const struct nft_cmp16_fast_expr *priv = nft_expr_priv(expr);
83 const u64 *reg_data = (const u64 *)®s->data[priv->sreg];
84 const u64 *mask = (const u64 *)&priv->mask;
85 const u64 *data = (const u64 *)&priv->data;
87 if (((reg_data[0] & mask[0]) == data[0] &&
88 ((reg_data[1] & mask[1]) == data[1])) ^ priv->inv)
90 regs->verdict.code = NFT_BREAK;
93 static noinline void __nft_trace_verdict(struct nft_traceinfo *info,
94 const struct nft_chain *chain,
95 const struct nft_regs *regs)
97 enum nft_trace_types type;
99 switch (regs->verdict.code) {
102 type = NFT_TRACETYPE_RETURN;
105 type = NFT_TRACETYPE_RULE;
106 /* can't access skb->nf_trace; use copy */
109 type = NFT_TRACETYPE_RULE;
112 info->nf_trace = info->pkt->skb->nf_trace;
116 __nft_trace_packet(info, chain, type);
119 static inline void nft_trace_verdict(struct nft_traceinfo *info,
120 const struct nft_chain *chain,
121 const struct nft_rule_dp *rule,
122 const struct nft_regs *regs)
124 if (static_branch_unlikely(&nft_trace_enabled)) {
126 __nft_trace_verdict(info, chain, regs);
130 static bool nft_payload_fast_eval(const struct nft_expr *expr,
131 struct nft_regs *regs,
132 const struct nft_pktinfo *pkt)
134 const struct nft_payload *priv = nft_expr_priv(expr);
135 const struct sk_buff *skb = pkt->skb;
136 u32 *dest = ®s->data[priv->dreg];
139 if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
140 ptr = skb_network_header(skb);
142 if (!(pkt->flags & NFT_PKTINFO_L4PROTO))
144 ptr = skb->data + nft_thoff(pkt);
149 if (unlikely(ptr + priv->len > skb_tail_pointer(skb)))
154 *(u16 *)dest = *(u16 *)ptr;
155 else if (priv->len == 4)
156 *(u32 *)dest = *(u32 *)ptr;
158 *(u8 *)dest = *(u8 *)ptr;
162 DEFINE_STATIC_KEY_FALSE(nft_counters_enabled);
164 static noinline void nft_update_chain_stats(const struct nft_chain *chain,
165 const struct nft_pktinfo *pkt)
167 struct nft_base_chain *base_chain;
168 struct nft_stats __percpu *pstats;
169 struct nft_stats *stats;
171 base_chain = nft_base_chain(chain);
173 pstats = READ_ONCE(base_chain->stats);
176 stats = this_cpu_ptr(pstats);
177 u64_stats_update_begin(&stats->syncp);
179 stats->bytes += pkt->skb->len;
180 u64_stats_update_end(&stats->syncp);
185 struct nft_jumpstack {
186 const struct nft_chain *chain;
187 const struct nft_rule_dp *rule;
188 const struct nft_rule_dp *last_rule;
191 static void expr_call_ops_eval(const struct nft_expr *expr,
192 struct nft_regs *regs,
193 struct nft_pktinfo *pkt)
195 #ifdef CONFIG_RETPOLINE
196 unsigned long e = (unsigned long)expr->ops->eval;
198 do { if ((e) == (unsigned long)(fun)) \
199 return fun(expr, regs, pkt); } while (0)
201 X(e, nft_payload_eval);
203 X(e, nft_counter_eval);
204 X(e, nft_meta_get_eval);
205 X(e, nft_lookup_eval);
206 X(e, nft_range_eval);
207 X(e, nft_immediate_eval);
208 X(e, nft_byteorder_eval);
209 X(e, nft_dynset_eval);
210 X(e, nft_rt_get_eval);
211 X(e, nft_bitwise_eval);
213 #endif /* CONFIG_RETPOLINE */
214 expr->ops->eval(expr, regs, pkt);
217 #define nft_rule_expr_first(rule) (struct nft_expr *)&rule->data[0]
218 #define nft_rule_expr_next(expr) ((void *)expr) + expr->ops->size
219 #define nft_rule_expr_last(rule) (struct nft_expr *)&rule->data[rule->dlen]
220 #define nft_rule_next(rule) (void *)rule + sizeof(*rule) + rule->dlen
222 #define nft_rule_dp_for_each_expr(expr, last, rule) \
223 for ((expr) = nft_rule_expr_first(rule), (last) = nft_rule_expr_last(rule); \
225 (expr) = nft_rule_expr_next(expr))
228 nft_do_chain(struct nft_pktinfo *pkt, void *priv)
230 const struct nft_chain *chain = priv, *basechain = chain;
231 const struct nft_rule_dp *rule, *last_rule;
232 const struct net *net = nft_net(pkt);
233 const struct nft_expr *expr, *last;
234 struct nft_regs regs = {};
235 unsigned int stackptr = 0;
236 struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
237 bool genbit = READ_ONCE(net->nft.gencursor);
238 struct nft_rule_blob *blob;
239 struct nft_traceinfo info;
242 if (static_branch_unlikely(&nft_trace_enabled))
243 nft_trace_init(&info, pkt, ®s.verdict, basechain);
246 blob = rcu_dereference(chain->blob_gen_1);
248 blob = rcu_dereference(chain->blob_gen_0);
250 rule = (struct nft_rule_dp *)blob->data;
251 last_rule = (void *)blob->data + blob->size;
253 regs.verdict.code = NFT_CONTINUE;
254 for (; rule < last_rule; rule = nft_rule_next(rule)) {
255 nft_rule_dp_for_each_expr(expr, last, rule) {
256 if (expr->ops == &nft_cmp_fast_ops)
257 nft_cmp_fast_eval(expr, ®s);
258 else if (expr->ops == &nft_cmp16_fast_ops)
259 nft_cmp16_fast_eval(expr, ®s);
260 else if (expr->ops == &nft_bitwise_fast_ops)
261 nft_bitwise_fast_eval(expr, ®s);
262 else if (expr->ops != &nft_payload_fast_ops ||
263 !nft_payload_fast_eval(expr, ®s, pkt))
264 expr_call_ops_eval(expr, ®s, pkt);
266 if (regs.verdict.code != NFT_CONTINUE)
270 switch (regs.verdict.code) {
272 regs.verdict.code = NFT_CONTINUE;
273 nft_trace_copy_nftrace(pkt, &info);
276 nft_trace_packet(pkt, &info, chain, rule,
283 nft_trace_verdict(&info, chain, rule, ®s);
285 switch (regs.verdict.code & NF_VERDICT_MASK) {
290 return regs.verdict.code;
293 switch (regs.verdict.code) {
295 if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
297 jumpstack[stackptr].chain = chain;
298 jumpstack[stackptr].rule = nft_rule_next(rule);
299 jumpstack[stackptr].last_rule = last_rule;
303 chain = regs.verdict.chain;
314 chain = jumpstack[stackptr].chain;
315 rule = jumpstack[stackptr].rule;
316 last_rule = jumpstack[stackptr].last_rule;
320 nft_trace_packet(pkt, &info, basechain, NULL, NFT_TRACETYPE_POLICY);
322 if (static_branch_unlikely(&nft_counters_enabled))
323 nft_update_chain_stats(basechain, pkt);
325 return nft_base_chain(basechain)->policy;
327 EXPORT_SYMBOL_GPL(nft_do_chain);
329 static struct nft_expr_type *nft_basic_types[] = {
345 static struct nft_object_type *nft_basic_objects[] = {
346 #ifdef CONFIG_NETWORK_SECMARK
347 &nft_secmark_obj_type,
349 &nft_counter_obj_type,
352 int __init nf_tables_core_module_init(void)
356 nft_counter_init_seqcount();
358 for (i = 0; i < ARRAY_SIZE(nft_basic_objects); i++) {
359 err = nft_register_obj(nft_basic_objects[i]);
364 for (j = 0; j < ARRAY_SIZE(nft_basic_types); j++) {
365 err = nft_register_expr(nft_basic_types[j]);
374 nft_unregister_expr(nft_basic_types[j]);
377 nft_unregister_obj(nft_basic_objects[i]);
382 void nf_tables_core_module_exit(void)
386 i = ARRAY_SIZE(nft_basic_types);
388 nft_unregister_expr(nft_basic_types[i]);
390 i = ARRAY_SIZE(nft_basic_objects);
392 nft_unregister_obj(nft_basic_objects[i]);