]> asedeno.scripts.mit.edu Git - linux.git/blob - net/netfilter/nf_tables_core.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[linux.git] / net / netfilter / nf_tables_core.c
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
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>
25
26 static const char *const comments[__NFT_TRACETYPE_MAX] = {
27         [NFT_TRACETYPE_POLICY]  = "policy",
28         [NFT_TRACETYPE_RETURN]  = "return",
29         [NFT_TRACETYPE_RULE]    = "rule",
30 };
31
32 static const struct nf_loginfo trace_loginfo = {
33         .type = NF_LOG_TYPE_LOG,
34         .u = {
35                 .log = {
36                         .level = LOGLEVEL_WARNING,
37                         .logflags = NF_LOG_DEFAULT_MASK,
38                 },
39         },
40 };
41
42 static noinline void __nft_trace_packet(struct nft_traceinfo *info,
43                                         const struct nft_chain *chain,
44                                         enum nft_trace_types type)
45 {
46         const struct nft_pktinfo *pkt = info->pkt;
47
48         if (!info->trace || !pkt->skb->nf_trace)
49                 return;
50
51         info->chain = chain;
52         info->type = type;
53
54         nft_trace_notify(info);
55 }
56
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)
61 {
62         if (static_branch_unlikely(&nft_trace_enabled)) {
63                 info->rule = rule;
64                 __nft_trace_packet(info, chain, type);
65         }
66 }
67
68 static void nft_cmp_fast_eval(const struct nft_expr *expr,
69                               struct nft_regs *regs)
70 {
71         const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
72         u32 mask = nft_cmp_fast_mask(priv->len);
73
74         if ((regs->data[priv->sreg] & mask) == priv->data)
75                 return;
76         regs->verdict.code = NFT_BREAK;
77 }
78
79 static bool nft_payload_fast_eval(const struct nft_expr *expr,
80                                   struct nft_regs *regs,
81                                   const struct nft_pktinfo *pkt)
82 {
83         const struct nft_payload *priv = nft_expr_priv(expr);
84         const struct sk_buff *skb = pkt->skb;
85         u32 *dest = &regs->data[priv->dreg];
86         unsigned char *ptr;
87
88         if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
89                 ptr = skb_network_header(skb);
90         else {
91                 if (!pkt->tprot_set)
92                         return false;
93                 ptr = skb_network_header(skb) + pkt->xt.thoff;
94         }
95
96         ptr += priv->offset;
97
98         if (unlikely(ptr + priv->len > skb_tail_pointer(skb)))
99                 return false;
100
101         *dest = 0;
102         if (priv->len == 2)
103                 *(u16 *)dest = *(u16 *)ptr;
104         else if (priv->len == 4)
105                 *(u32 *)dest = *(u32 *)ptr;
106         else
107                 *(u8 *)dest = *(u8 *)ptr;
108         return true;
109 }
110
111 DEFINE_STATIC_KEY_FALSE(nft_counters_enabled);
112
113 static noinline void nft_update_chain_stats(const struct nft_chain *chain,
114                                             const struct nft_pktinfo *pkt)
115 {
116         struct nft_base_chain *base_chain;
117         struct nft_stats *stats;
118
119         base_chain = nft_base_chain(chain);
120         if (!base_chain->stats)
121                 return;
122
123         stats = this_cpu_ptr(rcu_dereference(base_chain->stats));
124         if (stats) {
125                 local_bh_disable();
126                 u64_stats_update_begin(&stats->syncp);
127                 stats->pkts++;
128                 stats->bytes += pkt->skb->len;
129                 u64_stats_update_end(&stats->syncp);
130                 local_bh_enable();
131         }
132 }
133
134 struct nft_jumpstack {
135         const struct nft_chain  *chain;
136         const struct nft_rule   *rule;
137 };
138
139 unsigned int
140 nft_do_chain(struct nft_pktinfo *pkt, void *priv)
141 {
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;
151
152         info.trace = false;
153         if (static_branch_unlikely(&nft_trace_enabled))
154                 nft_trace_init(&info, pkt, &regs.verdict, basechain);
155 do_chain:
156         rule = list_entry(&chain->rules, struct nft_rule, list);
157 next_rule:
158         regs.verdict.code = NFT_CONTINUE;
159         list_for_each_entry_continue_rcu(rule, &chain->rules, list) {
160
161                 /* This rule is not active, skip. */
162                 if (unlikely(rule->genmask & gencursor))
163                         continue;
164
165                 nft_rule_for_each_expr(expr, last, rule) {
166                         if (expr->ops == &nft_cmp_fast_ops)
167                                 nft_cmp_fast_eval(expr, &regs);
168                         else if (expr->ops != &nft_payload_fast_ops ||
169                                  !nft_payload_fast_eval(expr, &regs, pkt))
170                                 expr->ops->eval(expr, &regs, pkt);
171
172                         if (regs.verdict.code != NFT_CONTINUE)
173                                 break;
174                 }
175
176                 switch (regs.verdict.code) {
177                 case NFT_BREAK:
178                         regs.verdict.code = NFT_CONTINUE;
179                         continue;
180                 case NFT_CONTINUE:
181                         nft_trace_packet(&info, chain, rule,
182                                          NFT_TRACETYPE_RULE);
183                         continue;
184                 }
185                 break;
186         }
187
188         switch (regs.verdict.code & NF_VERDICT_MASK) {
189         case NF_ACCEPT:
190         case NF_DROP:
191         case NF_QUEUE:
192         case NF_STOLEN:
193                 nft_trace_packet(&info, chain, rule,
194                                  NFT_TRACETYPE_RULE);
195                 return regs.verdict.code;
196         }
197
198         switch (regs.verdict.code) {
199         case NFT_JUMP:
200                 BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE);
201                 jumpstack[stackptr].chain = chain;
202                 jumpstack[stackptr].rule  = rule;
203                 stackptr++;
204                 /* fall through */
205         case NFT_GOTO:
206                 nft_trace_packet(&info, chain, rule,
207                                  NFT_TRACETYPE_RULE);
208
209                 chain = regs.verdict.chain;
210                 goto do_chain;
211         case NFT_CONTINUE:
212                 /* fall through */
213         case NFT_RETURN:
214                 nft_trace_packet(&info, chain, rule,
215                                  NFT_TRACETYPE_RETURN);
216                 break;
217         default:
218                 WARN_ON(1);
219         }
220
221         if (stackptr > 0) {
222                 stackptr--;
223                 chain = jumpstack[stackptr].chain;
224                 rule  = jumpstack[stackptr].rule;
225                 goto next_rule;
226         }
227
228         nft_trace_packet(&info, basechain, NULL, NFT_TRACETYPE_POLICY);
229
230         if (static_branch_unlikely(&nft_counters_enabled))
231                 nft_update_chain_stats(basechain, pkt);
232
233         return nft_base_chain(basechain)->policy;
234 }
235 EXPORT_SYMBOL_GPL(nft_do_chain);
236
237 static struct nft_expr_type *nft_basic_types[] = {
238         &nft_imm_type,
239         &nft_cmp_type,
240         &nft_lookup_type,
241         &nft_bitwise_type,
242         &nft_byteorder_type,
243         &nft_payload_type,
244         &nft_dynset_type,
245         &nft_range_type,
246         &nft_meta_type,
247         &nft_rt_type,
248         &nft_exthdr_type,
249 };
250
251 int __init nf_tables_core_module_init(void)
252 {
253         int err, i;
254
255         for (i = 0; i < ARRAY_SIZE(nft_basic_types); i++) {
256                 err = nft_register_expr(nft_basic_types[i]);
257                 if (err)
258                         goto err;
259         }
260
261         return 0;
262
263 err:
264         while (i-- > 0)
265                 nft_unregister_expr(nft_basic_types[i]);
266         return err;
267 }
268
269 void nf_tables_core_module_exit(void)
270 {
271         int i;
272
273         i = ARRAY_SIZE(nft_basic_types);
274         while (i-- > 0)
275                 nft_unregister_expr(nft_basic_types[i]);
276 }