dm: fix dm_wq_work() to only use __split_and_process_bio() if appropriate
[linux-2.6-block.git] / net / netfilter / nf_tables_core.c
CommitLineData
96518518
PM
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 *
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.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
a81b2ce8 11#include <linux/kernel.h>
96518518
PM
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>
e639f7ab 19#include <linux/static_key.h>
96518518
PM
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>
b5bc89bf 24#include <net/netfilter/nf_log.h>
96518518 25
33d5a7b1
FW
26static noinline void __nft_trace_packet(struct nft_traceinfo *info,
27 const struct nft_chain *chain,
e65eebec 28 enum nft_trace_types type)
01ef16c2 29{
33d5a7b1
FW
30 const struct nft_pktinfo *pkt = info->pkt;
31
e639f7ab 32 if (!info->trace || !pkt->skb->nf_trace)
33d5a7b1
FW
33 return;
34
35 info->chain = chain;
36 info->type = type;
37
38 nft_trace_notify(info);
01ef16c2
PM
39}
40
33d5a7b1 41static inline void nft_trace_packet(struct nft_traceinfo *info,
01ef16c2 42 const struct nft_chain *chain,
33d5a7b1 43 const struct nft_rule *rule,
33d5a7b1 44 enum nft_trace_types type)
01ef16c2 45{
e639f7ab 46 if (static_branch_unlikely(&nft_trace_enabled)) {
33d5a7b1 47 info->rule = rule;
e65eebec 48 __nft_trace_packet(info, chain, type);
33d5a7b1 49 }
01ef16c2
PM
50}
51
cb7dbfd0 52static void nft_cmp_fast_eval(const struct nft_expr *expr,
a55e22e9 53 struct nft_regs *regs)
cb7dbfd0
PM
54{
55 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
b855d416 56 u32 mask = nft_cmp_fast_mask(priv->len);
cb7dbfd0 57
49499c3e 58 if ((regs->data[priv->sreg] & mask) == priv->data)
cb7dbfd0 59 return;
a55e22e9 60 regs->verdict.code = NFT_BREAK;
cb7dbfd0
PM
61}
62
c29b72e0 63static bool nft_payload_fast_eval(const struct nft_expr *expr,
a55e22e9 64 struct nft_regs *regs,
c29b72e0
PM
65 const struct nft_pktinfo *pkt)
66{
67 const struct nft_payload *priv = nft_expr_priv(expr);
68 const struct sk_buff *skb = pkt->skb;
49499c3e 69 u32 *dest = &regs->data[priv->dreg];
c29b72e0
PM
70 unsigned char *ptr;
71
72 if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
73 ptr = skb_network_header(skb);
a20877b5
LZ
74 else {
75 if (!pkt->tprot_set)
76 return false;
c54032e0 77 ptr = skb_network_header(skb) + pkt->xt.thoff;
a20877b5 78 }
c29b72e0
PM
79
80 ptr += priv->offset;
81
8dc3c2b8 82 if (unlikely(ptr + priv->len > skb_tail_pointer(skb)))
c29b72e0
PM
83 return false;
84
49499c3e 85 *dest = 0;
c29b72e0 86 if (priv->len == 2)
fad136ea 87 *(u16 *)dest = *(u16 *)ptr;
c29b72e0 88 else if (priv->len == 4)
fad136ea 89 *(u32 *)dest = *(u32 *)ptr;
c29b72e0 90 else
fad136ea 91 *(u8 *)dest = *(u8 *)ptr;
c29b72e0
PM
92 return true;
93}
94
9f08ea84
PNA
95DEFINE_STATIC_KEY_FALSE(nft_counters_enabled);
96
97static noinline void nft_update_chain_stats(const struct nft_chain *chain,
98 const struct nft_pktinfo *pkt)
99{
00924094 100 struct nft_base_chain *base_chain;
9f08ea84
PNA
101 struct nft_stats *stats;
102
00924094 103 base_chain = nft_base_chain(chain);
4c05ec47 104 if (!rcu_access_pointer(base_chain->stats))
00924094
FW
105 return;
106
ad9d9e85 107 local_bh_disable();
00924094
FW
108 stats = this_cpu_ptr(rcu_dereference(base_chain->stats));
109 if (stats) {
00924094
FW
110 u64_stats_update_begin(&stats->syncp);
111 stats->pkts++;
112 stats->bytes += pkt->skb->len;
113 u64_stats_update_end(&stats->syncp);
00924094 114 }
ad9d9e85 115 local_bh_enable();
9f08ea84
PNA
116}
117
0ca743a5
PNA
118struct nft_jumpstack {
119 const struct nft_chain *chain;
0cbc06b3 120 struct nft_rule *const *rules;
0ca743a5
PNA
121};
122
222440b4
FW
123static void expr_call_ops_eval(const struct nft_expr *expr,
124 struct nft_regs *regs,
125 struct nft_pktinfo *pkt)
126{
127 unsigned long e = (unsigned long)expr->ops->eval;
128
129 if (e == (unsigned long)nft_meta_get_eval)
130 nft_meta_get_eval(expr, regs, pkt);
131 else if (e == (unsigned long)nft_lookup_eval)
132 nft_lookup_eval(expr, regs, pkt);
133 else
134 expr->ops->eval(expr, regs, pkt);
135}
136
0ca743a5 137unsigned int
06198b34 138nft_do_chain(struct nft_pktinfo *pkt, void *priv)
96518518 139{
06198b34 140 const struct nft_chain *chain = priv, *basechain = chain;
0e5a1c7e 141 const struct net *net = nft_net(pkt);
0cbc06b3 142 struct nft_rule *const *rules;
96518518
PM
143 const struct nft_rule *rule;
144 const struct nft_expr *expr, *last;
a55e22e9 145 struct nft_regs regs;
96518518 146 unsigned int stackptr = 0;
0ca743a5 147 struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
0cbc06b3 148 bool genbit = READ_ONCE(net->nft.gencursor);
33d5a7b1 149 struct nft_traceinfo info;
96518518 150
e639f7ab
FW
151 info.trace = false;
152 if (static_branch_unlikely(&nft_trace_enabled))
153 nft_trace_init(&info, pkt, &regs.verdict, basechain);
96518518 154do_chain:
0cbc06b3
FW
155 if (genbit)
156 rules = rcu_dereference(chain->rules_gen_1);
157 else
158 rules = rcu_dereference(chain->rules_gen_0);
159
96518518 160next_rule:
0cbc06b3 161 rule = *rules;
a55e22e9 162 regs.verdict.code = NFT_CONTINUE;
0cbc06b3
FW
163 for (; *rules ; rules++) {
164 rule = *rules;
96518518 165 nft_rule_for_each_expr(expr, last, rule) {
cb7dbfd0 166 if (expr->ops == &nft_cmp_fast_ops)
a55e22e9 167 nft_cmp_fast_eval(expr, &regs);
c29b72e0 168 else if (expr->ops != &nft_payload_fast_ops ||
a55e22e9 169 !nft_payload_fast_eval(expr, &regs, pkt))
222440b4 170 expr_call_ops_eval(expr, &regs, pkt);
cb7dbfd0 171
a55e22e9 172 if (regs.verdict.code != NFT_CONTINUE)
96518518
PM
173 break;
174 }
175
a55e22e9 176 switch (regs.verdict.code) {
96518518 177 case NFT_BREAK:
a55e22e9 178 regs.verdict.code = NFT_CONTINUE;
3b084e99 179 continue;
96518518 180 case NFT_CONTINUE:
33d5a7b1 181 nft_trace_packet(&info, chain, rule,
e65eebec 182 NFT_TRACETYPE_RULE);
96518518
PM
183 continue;
184 }
185 break;
186 }
187
a55e22e9 188 switch (regs.verdict.code & NF_VERDICT_MASK) {
96518518
PM
189 case NF_ACCEPT:
190 case NF_DROP:
191 case NF_QUEUE:
5efa0fc6 192 case NF_STOLEN:
33d5a7b1 193 nft_trace_packet(&info, chain, rule,
e65eebec 194 NFT_TRACETYPE_RULE);
a55e22e9 195 return regs.verdict.code;
e569bdab
EL
196 }
197
a55e22e9 198 switch (regs.verdict.code) {
96518518 199 case NFT_JUMP:
adc972c5
TY
200 if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
201 return NF_DROP;
96518518 202 jumpstack[stackptr].chain = chain;
0cbc06b3 203 jumpstack[stackptr].rules = rules + 1;
96518518 204 stackptr++;
354bf5a0 205 /* fall through */
96518518 206 case NFT_GOTO:
33d5a7b1 207 nft_trace_packet(&info, chain, rule,
e65eebec 208 NFT_TRACETYPE_RULE);
7b9d5ef9 209
a55e22e9 210 chain = regs.verdict.chain;
96518518 211 goto do_chain;
354bf5a0 212 case NFT_CONTINUE:
354bf5a0 213 /* fall through */
96518518 214 case NFT_RETURN:
33d5a7b1 215 nft_trace_packet(&info, chain, rule,
e65eebec 216 NFT_TRACETYPE_RETURN);
7e9bc10d 217 break;
96518518
PM
218 default:
219 WARN_ON(1);
220 }
221
222 if (stackptr > 0) {
223 stackptr--;
224 chain = jumpstack[stackptr].chain;
0cbc06b3 225 rules = jumpstack[stackptr].rules;
96518518
PM
226 goto next_rule;
227 }
228
e65eebec 229 nft_trace_packet(&info, basechain, NULL, NFT_TRACETYPE_POLICY);
5467a512 230
9f08ea84
PNA
231 if (static_branch_unlikely(&nft_counters_enabled))
232 nft_update_chain_stats(basechain, pkt);
b5bc89bf 233
5467a512 234 return nft_base_chain(basechain)->policy;
96518518 235}
3876d22d 236EXPORT_SYMBOL_GPL(nft_do_chain);
96518518 237
4e24877e
LZ
238static struct nft_expr_type *nft_basic_types[] = {
239 &nft_imm_type,
240 &nft_cmp_type,
241 &nft_lookup_type,
242 &nft_bitwise_type,
243 &nft_byteorder_type,
244 &nft_payload_type,
245 &nft_dynset_type,
246 &nft_range_type,
8a22543c 247 &nft_meta_type,
ae1bc6a9 248 &nft_rt_type,
d0103158 249 &nft_exthdr_type,
4e24877e
LZ
250};
251
fb961945
CG
252static struct nft_object_type *nft_basic_objects[] = {
253#ifdef CONFIG_NETWORK_SECMARK
254 &nft_secmark_obj_type,
255#endif
256};
257
96518518
PM
258int __init nf_tables_core_module_init(void)
259{
fb961945
CG
260 int err, i, j = 0;
261
262 for (i = 0; i < ARRAY_SIZE(nft_basic_objects); i++) {
263 err = nft_register_obj(nft_basic_objects[i]);
264 if (err)
265 goto err;
266 }
96518518 267
fb961945
CG
268 for (j = 0; j < ARRAY_SIZE(nft_basic_types); j++) {
269 err = nft_register_expr(nft_basic_types[j]);
4e24877e
LZ
270 if (err)
271 goto err;
272 }
96518518 273
0f3cd9b3 274 return 0;
4e24877e
LZ
275
276err:
fb961945
CG
277 while (j-- > 0)
278 nft_unregister_expr(nft_basic_types[j]);
279
4e24877e 280 while (i-- > 0)
fb961945
CG
281 nft_unregister_obj(nft_basic_objects[i]);
282
96518518
PM
283 return err;
284}
285
286void nf_tables_core_module_exit(void)
287{
4e24877e
LZ
288 int i;
289
290 i = ARRAY_SIZE(nft_basic_types);
291 while (i-- > 0)
292 nft_unregister_expr(nft_basic_types[i]);
fb961945
CG
293
294 i = ARRAY_SIZE(nft_basic_objects);
295 while (i-- > 0)
296 nft_unregister_obj(nft_basic_objects[i]);
96518518 297}