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 const struct nft_pktinfo *pkt = info->pkt;
30 if (!info->trace || !pkt->skb->nf_trace)
36 nft_trace_notify(info);
39 static inline void nft_trace_packet(struct nft_traceinfo *info,
40 const struct nft_chain *chain,
41 const struct nft_rule_dp *rule,
42 enum nft_trace_types type)
44 if (static_branch_unlikely(&nft_trace_enabled)) {
46 __nft_trace_packet(info, chain, type);
50 static void nft_bitwise_fast_eval(const struct nft_expr *expr,
51 struct nft_regs *regs)
53 const struct nft_bitwise_fast_expr *priv = nft_expr_priv(expr);
54 u32 *src = ®s->data[priv->sreg];
55 u32 *dst = ®s->data[priv->dreg];
57 *dst = (*src & priv->mask) ^ priv->xor;
60 static void nft_cmp_fast_eval(const struct nft_expr *expr,
61 struct nft_regs *regs)
63 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
65 if (((regs->data[priv->sreg] & priv->mask) == priv->data) ^ priv->inv)
67 regs->verdict.code = NFT_BREAK;
70 static void nft_cmp16_fast_eval(const struct nft_expr *expr,
71 struct nft_regs *regs)
73 const struct nft_cmp16_fast_expr *priv = nft_expr_priv(expr);
74 const u64 *reg_data = (const u64 *)®s->data[priv->sreg];
75 const u64 *mask = (const u64 *)&priv->mask;
76 const u64 *data = (const u64 *)&priv->data;
78 if (((reg_data[0] & mask[0]) == data[0] &&
79 ((reg_data[1] & mask[1]) == data[1])) ^ priv->inv)
81 regs->verdict.code = NFT_BREAK;
84 static noinline void __nft_trace_verdict(struct nft_traceinfo *info,
85 const struct nft_chain *chain,
86 const struct nft_regs *regs)
88 enum nft_trace_types type;
90 switch (regs->verdict.code) {
93 type = NFT_TRACETYPE_RETURN;
96 type = NFT_TRACETYPE_RULE;
100 __nft_trace_packet(info, chain, type);
103 static inline void nft_trace_verdict(struct nft_traceinfo *info,
104 const struct nft_chain *chain,
105 const struct nft_rule_dp *rule,
106 const struct nft_regs *regs)
108 if (static_branch_unlikely(&nft_trace_enabled)) {
110 __nft_trace_verdict(info, chain, regs);
114 static bool nft_payload_fast_eval(const struct nft_expr *expr,
115 struct nft_regs *regs,
116 const struct nft_pktinfo *pkt)
118 const struct nft_payload *priv = nft_expr_priv(expr);
119 const struct sk_buff *skb = pkt->skb;
120 u32 *dest = ®s->data[priv->dreg];
123 if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
124 ptr = skb_network_header(skb);
126 if (!(pkt->flags & NFT_PKTINFO_L4PROTO))
128 ptr = skb_network_header(skb) + nft_thoff(pkt);
133 if (unlikely(ptr + priv->len > skb_tail_pointer(skb)))
138 *(u16 *)dest = *(u16 *)ptr;
139 else if (priv->len == 4)
140 *(u32 *)dest = *(u32 *)ptr;
142 *(u8 *)dest = *(u8 *)ptr;
146 DEFINE_STATIC_KEY_FALSE(nft_counters_enabled);
148 static noinline void nft_update_chain_stats(const struct nft_chain *chain,
149 const struct nft_pktinfo *pkt)
151 struct nft_base_chain *base_chain;
152 struct nft_stats __percpu *pstats;
153 struct nft_stats *stats;
155 base_chain = nft_base_chain(chain);
157 pstats = READ_ONCE(base_chain->stats);
160 stats = this_cpu_ptr(pstats);
161 u64_stats_update_begin(&stats->syncp);
163 stats->bytes += pkt->skb->len;
164 u64_stats_update_end(&stats->syncp);
169 struct nft_jumpstack {
170 const struct nft_chain *chain;
171 const struct nft_rule_dp *rule;
172 const struct nft_rule_dp *last_rule;
175 static void expr_call_ops_eval(const struct nft_expr *expr,
176 struct nft_regs *regs,
177 struct nft_pktinfo *pkt)
179 #ifdef CONFIG_RETPOLINE
180 unsigned long e = (unsigned long)expr->ops->eval;
182 do { if ((e) == (unsigned long)(fun)) \
183 return fun(expr, regs, pkt); } while (0)
185 X(e, nft_payload_eval);
187 X(e, nft_counter_eval);
188 X(e, nft_meta_get_eval);
189 X(e, nft_lookup_eval);
190 X(e, nft_range_eval);
191 X(e, nft_immediate_eval);
192 X(e, nft_byteorder_eval);
193 X(e, nft_dynset_eval);
194 X(e, nft_rt_get_eval);
195 X(e, nft_bitwise_eval);
197 #endif /* CONFIG_RETPOLINE */
198 expr->ops->eval(expr, regs, pkt);
201 #define nft_rule_expr_first(rule) (struct nft_expr *)&rule->data[0]
202 #define nft_rule_expr_next(expr) ((void *)expr) + expr->ops->size
203 #define nft_rule_expr_last(rule) (struct nft_expr *)&rule->data[rule->dlen]
204 #define nft_rule_next(rule) (void *)rule + sizeof(*rule) + rule->dlen
206 #define nft_rule_dp_for_each_expr(expr, last, rule) \
207 for ((expr) = nft_rule_expr_first(rule), (last) = nft_rule_expr_last(rule); \
209 (expr) = nft_rule_expr_next(expr))
212 nft_do_chain(struct nft_pktinfo *pkt, void *priv)
214 const struct nft_chain *chain = priv, *basechain = chain;
215 const struct nft_rule_dp *rule, *last_rule;
216 const struct net *net = nft_net(pkt);
217 const struct nft_expr *expr, *last;
218 struct nft_regs regs = {};
219 unsigned int stackptr = 0;
220 struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
221 bool genbit = READ_ONCE(net->nft.gencursor);
222 struct nft_rule_blob *blob;
223 struct nft_traceinfo info;
226 if (static_branch_unlikely(&nft_trace_enabled))
227 nft_trace_init(&info, pkt, ®s.verdict, basechain);
230 blob = rcu_dereference(chain->blob_gen_1);
232 blob = rcu_dereference(chain->blob_gen_0);
234 rule = (struct nft_rule_dp *)blob->data;
235 last_rule = (void *)blob->data + blob->size;
237 regs.verdict.code = NFT_CONTINUE;
238 for (; rule < last_rule; rule = nft_rule_next(rule)) {
239 nft_rule_dp_for_each_expr(expr, last, rule) {
240 if (expr->ops == &nft_cmp_fast_ops)
241 nft_cmp_fast_eval(expr, ®s);
242 else if (expr->ops == &nft_cmp16_fast_ops)
243 nft_cmp16_fast_eval(expr, ®s);
244 else if (expr->ops == &nft_bitwise_fast_ops)
245 nft_bitwise_fast_eval(expr, ®s);
246 else if (expr->ops != &nft_payload_fast_ops ||
247 !nft_payload_fast_eval(expr, ®s, pkt))
248 expr_call_ops_eval(expr, ®s, pkt);
250 if (regs.verdict.code != NFT_CONTINUE)
254 switch (regs.verdict.code) {
256 regs.verdict.code = NFT_CONTINUE;
259 nft_trace_packet(&info, chain, rule,
266 nft_trace_verdict(&info, chain, rule, ®s);
268 switch (regs.verdict.code & NF_VERDICT_MASK) {
273 return regs.verdict.code;
276 switch (regs.verdict.code) {
278 if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
280 jumpstack[stackptr].chain = chain;
281 jumpstack[stackptr].rule = nft_rule_next(rule);
282 jumpstack[stackptr].last_rule = last_rule;
286 chain = regs.verdict.chain;
297 chain = jumpstack[stackptr].chain;
298 rule = jumpstack[stackptr].rule;
299 last_rule = jumpstack[stackptr].last_rule;
303 nft_trace_packet(&info, basechain, NULL, NFT_TRACETYPE_POLICY);
305 if (static_branch_unlikely(&nft_counters_enabled))
306 nft_update_chain_stats(basechain, pkt);
308 return nft_base_chain(basechain)->policy;
310 EXPORT_SYMBOL_GPL(nft_do_chain);
312 static struct nft_expr_type *nft_basic_types[] = {
328 static struct nft_object_type *nft_basic_objects[] = {
329 #ifdef CONFIG_NETWORK_SECMARK
330 &nft_secmark_obj_type,
332 &nft_counter_obj_type,
335 int __init nf_tables_core_module_init(void)
339 nft_counter_init_seqcount();
341 for (i = 0; i < ARRAY_SIZE(nft_basic_objects); i++) {
342 err = nft_register_obj(nft_basic_objects[i]);
347 for (j = 0; j < ARRAY_SIZE(nft_basic_types); j++) {
348 err = nft_register_expr(nft_basic_types[j]);
357 nft_unregister_expr(nft_basic_types[j]);
360 nft_unregister_obj(nft_basic_objects[i]);
365 void nf_tables_core_module_exit(void)
369 i = ARRAY_SIZE(nft_basic_types);
371 nft_unregister_expr(nft_basic_types[i]);
373 i = ARRAY_SIZE(nft_basic_objects);
375 nft_unregister_obj(nft_basic_objects[i]);