2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/rculist.h>
16 #include <linux/skbuff.h>
17 #include <linux/netlink.h>
18 #include <linux/netfilter.h>
19 #include <linux/static_key.h>
20 #include <linux/netfilter/nfnetlink.h>
21 #include <linux/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_tables_core.h>
23 #include <net/netfilter/nf_tables.h>
24 #include <net/netfilter/nf_log.h>
26 static const char *const comments[__NFT_TRACETYPE_MAX] = {
27 [NFT_TRACETYPE_POLICY] = "policy",
28 [NFT_TRACETYPE_RETURN] = "return",
29 [NFT_TRACETYPE_RULE] = "rule",
32 static const struct nf_loginfo trace_loginfo = {
33 .type = NF_LOG_TYPE_LOG,
36 .level = LOGLEVEL_WARNING,
37 .logflags = NF_LOG_DEFAULT_MASK,
42 static noinline void __nft_trace_packet(struct nft_traceinfo *info,
43 const struct nft_chain *chain,
44 enum nft_trace_types type)
46 const struct nft_pktinfo *pkt = info->pkt;
48 if (!info->trace || !pkt->skb->nf_trace)
54 nft_trace_notify(info);
57 static inline void nft_trace_packet(struct nft_traceinfo *info,
58 const struct nft_chain *chain,
59 const struct nft_rule *rule,
60 enum nft_trace_types type)
62 if (static_branch_unlikely(&nft_trace_enabled)) {
64 __nft_trace_packet(info, chain, type);
68 static void nft_cmp_fast_eval(const struct nft_expr *expr,
69 struct nft_regs *regs)
71 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
72 u32 mask = nft_cmp_fast_mask(priv->len);
74 if ((regs->data[priv->sreg] & mask) == priv->data)
76 regs->verdict.code = NFT_BREAK;
79 static bool nft_payload_fast_eval(const struct nft_expr *expr,
80 struct nft_regs *regs,
81 const struct nft_pktinfo *pkt)
83 const struct nft_payload *priv = nft_expr_priv(expr);
84 const struct sk_buff *skb = pkt->skb;
85 u32 *dest = ®s->data[priv->dreg];
88 if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
89 ptr = skb_network_header(skb);
93 ptr = skb_network_header(skb) + pkt->xt.thoff;
98 if (unlikely(ptr + priv->len > skb_tail_pointer(skb)))
103 *(u16 *)dest = *(u16 *)ptr;
104 else if (priv->len == 4)
105 *(u32 *)dest = *(u32 *)ptr;
107 *(u8 *)dest = *(u8 *)ptr;
111 DEFINE_STATIC_KEY_FALSE(nft_counters_enabled);
113 static noinline void nft_update_chain_stats(const struct nft_chain *chain,
114 const struct nft_pktinfo *pkt)
116 struct nft_base_chain *base_chain;
117 struct nft_stats *stats;
119 base_chain = nft_base_chain(chain);
120 if (!base_chain->stats)
123 stats = this_cpu_ptr(rcu_dereference(base_chain->stats));
126 u64_stats_update_begin(&stats->syncp);
128 stats->bytes += pkt->skb->len;
129 u64_stats_update_end(&stats->syncp);
134 struct nft_jumpstack {
135 const struct nft_chain *chain;
136 const struct nft_rule *rule;
140 nft_do_chain(struct nft_pktinfo *pkt, void *priv)
142 const struct nft_chain *chain = priv, *basechain = chain;
143 const struct net *net = nft_net(pkt);
144 const struct nft_rule *rule;
145 const struct nft_expr *expr, *last;
146 struct nft_regs regs;
147 unsigned int stackptr = 0;
148 struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
149 unsigned int gencursor = nft_genmask_cur(net);
150 struct nft_traceinfo info;
153 if (static_branch_unlikely(&nft_trace_enabled))
154 nft_trace_init(&info, pkt, ®s.verdict, basechain);
156 rule = list_entry(&chain->rules, struct nft_rule, list);
158 regs.verdict.code = NFT_CONTINUE;
159 list_for_each_entry_continue_rcu(rule, &chain->rules, list) {
161 /* This rule is not active, skip. */
162 if (unlikely(rule->genmask & gencursor))
165 nft_rule_for_each_expr(expr, last, rule) {
166 if (expr->ops == &nft_cmp_fast_ops)
167 nft_cmp_fast_eval(expr, ®s);
168 else if (expr->ops != &nft_payload_fast_ops ||
169 !nft_payload_fast_eval(expr, ®s, pkt))
170 expr->ops->eval(expr, ®s, pkt);
172 if (regs.verdict.code != NFT_CONTINUE)
176 switch (regs.verdict.code) {
178 regs.verdict.code = NFT_CONTINUE;
181 nft_trace_packet(&info, chain, rule,
188 switch (regs.verdict.code & NF_VERDICT_MASK) {
193 nft_trace_packet(&info, chain, rule,
195 return regs.verdict.code;
198 switch (regs.verdict.code) {
200 BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE);
201 jumpstack[stackptr].chain = chain;
202 jumpstack[stackptr].rule = rule;
206 nft_trace_packet(&info, chain, rule,
209 chain = regs.verdict.chain;
214 nft_trace_packet(&info, chain, rule,
215 NFT_TRACETYPE_RETURN);
223 chain = jumpstack[stackptr].chain;
224 rule = jumpstack[stackptr].rule;
228 nft_trace_packet(&info, basechain, NULL, NFT_TRACETYPE_POLICY);
230 if (static_branch_unlikely(&nft_counters_enabled))
231 nft_update_chain_stats(basechain, pkt);
233 return nft_base_chain(basechain)->policy;
235 EXPORT_SYMBOL_GPL(nft_do_chain);
237 static struct nft_expr_type *nft_basic_types[] = {
251 int __init nf_tables_core_module_init(void)
255 for (i = 0; i < ARRAY_SIZE(nft_basic_types); i++) {
256 err = nft_register_expr(nft_basic_types[i]);
265 nft_unregister_expr(nft_basic_types[i]);
269 void nf_tables_core_module_exit(void)
273 i = ARRAY_SIZE(nft_basic_types);
275 nft_unregister_expr(nft_basic_types[i]);