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/netfilter/nfnetlink.h>
20 #include <linux/netfilter/nf_tables.h>
21 #include <net/netfilter/nf_tables_core.h>
22 #include <net/netfilter/nf_tables.h>
23 #include <net/netfilter/nf_log.h>
31 static const char *const comments[] = {
32 [NFT_TRACE_RULE] = "rule",
33 [NFT_TRACE_RETURN] = "return",
34 [NFT_TRACE_POLICY] = "policy",
37 static struct nf_loginfo trace_loginfo = {
38 .type = NF_LOG_TYPE_LOG,
41 .level = LOGLEVEL_WARNING,
42 .logflags = NF_LOG_MASK,
47 static void __nft_trace_packet(const struct nft_pktinfo *pkt,
48 const struct nft_chain *chain,
49 int rulenum, enum nft_trace type)
51 nf_log_trace(pkt->net, pkt->pf, pkt->hook, pkt->skb, pkt->in,
52 pkt->out, &trace_loginfo, "TRACE: %s:%s:%s:%u ",
53 chain->table->name, chain->name, comments[type],
57 static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
58 const struct nft_chain *chain,
59 int rulenum, enum nft_trace type)
61 if (unlikely(pkt->skb->nf_trace))
62 __nft_trace_packet(pkt, chain, rulenum, type);
65 static void nft_cmp_fast_eval(const struct nft_expr *expr,
66 struct nft_regs *regs)
68 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
69 u32 mask = nft_cmp_fast_mask(priv->len);
71 if ((regs->data[priv->sreg] & mask) == priv->data)
73 regs->verdict.code = NFT_BREAK;
76 static bool nft_payload_fast_eval(const struct nft_expr *expr,
77 struct nft_regs *regs,
78 const struct nft_pktinfo *pkt)
80 const struct nft_payload *priv = nft_expr_priv(expr);
81 const struct sk_buff *skb = pkt->skb;
82 u32 *dest = ®s->data[priv->dreg];
85 if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
86 ptr = skb_network_header(skb);
88 ptr = skb_network_header(skb) + pkt->xt.thoff;
92 if (unlikely(ptr + priv->len >= skb_tail_pointer(skb)))
97 *(u16 *)dest = *(u16 *)ptr;
98 else if (priv->len == 4)
99 *(u32 *)dest = *(u32 *)ptr;
101 *(u8 *)dest = *(u8 *)ptr;
105 struct nft_jumpstack {
106 const struct nft_chain *chain;
107 const struct nft_rule *rule;
112 nft_do_chain(struct nft_pktinfo *pkt, void *priv)
114 const struct nft_chain *chain = priv, *basechain = chain;
115 const struct net *net = pkt->net;
116 const struct nft_rule *rule;
117 const struct nft_expr *expr, *last;
118 struct nft_regs regs;
119 unsigned int stackptr = 0;
120 struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
121 struct nft_stats *stats;
123 unsigned int gencursor = nft_genmask_cur(net);
127 rule = list_entry(&chain->rules, struct nft_rule, list);
129 regs.verdict.code = NFT_CONTINUE;
130 list_for_each_entry_continue_rcu(rule, &chain->rules, list) {
132 /* This rule is not active, skip. */
133 if (unlikely(rule->genmask & (1 << gencursor)))
138 nft_rule_for_each_expr(expr, last, rule) {
139 if (expr->ops == &nft_cmp_fast_ops)
140 nft_cmp_fast_eval(expr, ®s);
141 else if (expr->ops != &nft_payload_fast_ops ||
142 !nft_payload_fast_eval(expr, ®s, pkt))
143 expr->ops->eval(expr, ®s, pkt);
145 if (regs.verdict.code != NFT_CONTINUE)
149 switch (regs.verdict.code) {
151 regs.verdict.code = NFT_CONTINUE;
154 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
160 switch (regs.verdict.code & NF_VERDICT_MASK) {
164 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
165 return regs.verdict.code;
168 switch (regs.verdict.code) {
170 BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE);
171 jumpstack[stackptr].chain = chain;
172 jumpstack[stackptr].rule = rule;
173 jumpstack[stackptr].rulenum = rulenum;
177 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
179 chain = regs.verdict.chain;
185 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN);
193 chain = jumpstack[stackptr].chain;
194 rule = jumpstack[stackptr].rule;
195 rulenum = jumpstack[stackptr].rulenum;
199 nft_trace_packet(pkt, basechain, -1, NFT_TRACE_POLICY);
202 stats = this_cpu_ptr(rcu_dereference(nft_base_chain(basechain)->stats));
203 u64_stats_update_begin(&stats->syncp);
205 stats->bytes += pkt->skb->len;
206 u64_stats_update_end(&stats->syncp);
207 rcu_read_unlock_bh();
209 return nft_base_chain(basechain)->policy;
211 EXPORT_SYMBOL_GPL(nft_do_chain);
213 int __init nf_tables_core_module_init(void)
217 err = nft_immediate_module_init();
221 err = nft_cmp_module_init();
225 err = nft_lookup_module_init();
229 err = nft_bitwise_module_init();
233 err = nft_byteorder_module_init();
237 err = nft_payload_module_init();
241 err = nft_dynset_module_init();
248 nft_payload_module_exit();
250 nft_byteorder_module_exit();
252 nft_bitwise_module_exit();
254 nft_lookup_module_exit();
256 nft_cmp_module_exit();
258 nft_immediate_module_exit();
263 void nf_tables_core_module_exit(void)
265 nft_dynset_module_exit();
266 nft_payload_module_exit();
267 nft_byteorder_module_exit();
268 nft_bitwise_module_exit();
269 nft_lookup_module_exit();
270 nft_cmp_module_exit();
271 nft_immediate_module_exit();