OSDN Git Service

3fbce3b9c5ec0f51c9841aca36c0e5fc48b233c6
[tomoyo/tomoyo-test1.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 noinline void __nft_trace_packet(struct nft_traceinfo *info,
27                                         const struct nft_chain *chain,
28                                         enum nft_trace_types type)
29 {
30         const struct nft_pktinfo *pkt = info->pkt;
31
32         if (!info->trace || !pkt->skb->nf_trace)
33                 return;
34
35         info->chain = chain;
36         info->type = type;
37
38         nft_trace_notify(info);
39 }
40
41 static inline void nft_trace_packet(struct nft_traceinfo *info,
42                                     const struct nft_chain *chain,
43                                     const struct nft_rule *rule,
44                                     enum nft_trace_types type)
45 {
46         if (static_branch_unlikely(&nft_trace_enabled)) {
47                 info->rule = rule;
48                 __nft_trace_packet(info, chain, type);
49         }
50 }
51
52 static void nft_cmp_fast_eval(const struct nft_expr *expr,
53                               struct nft_regs *regs)
54 {
55         const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
56         u32 mask = nft_cmp_fast_mask(priv->len);
57
58         if ((regs->data[priv->sreg] & mask) == priv->data)
59                 return;
60         regs->verdict.code = NFT_BREAK;
61 }
62
63 static bool nft_payload_fast_eval(const struct nft_expr *expr,
64                                   struct nft_regs *regs,
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;
69         u32 *dest = &regs->data[priv->dreg];
70         unsigned char *ptr;
71
72         if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
73                 ptr = skb_network_header(skb);
74         else {
75                 if (!pkt->tprot_set)
76                         return false;
77                 ptr = skb_network_header(skb) + pkt->xt.thoff;
78         }
79
80         ptr += priv->offset;
81
82         if (unlikely(ptr + priv->len > skb_tail_pointer(skb)))
83                 return false;
84
85         *dest = 0;
86         if (priv->len == 2)
87                 *(u16 *)dest = *(u16 *)ptr;
88         else if (priv->len == 4)
89                 *(u32 *)dest = *(u32 *)ptr;
90         else
91                 *(u8 *)dest = *(u8 *)ptr;
92         return true;
93 }
94
95 DEFINE_STATIC_KEY_FALSE(nft_counters_enabled);
96
97 static noinline void nft_update_chain_stats(const struct nft_chain *chain,
98                                             const struct nft_pktinfo *pkt)
99 {
100         struct nft_base_chain *base_chain;
101         struct nft_stats *stats;
102
103         base_chain = nft_base_chain(chain);
104         if (!base_chain->stats)
105                 return;
106
107         local_bh_disable();
108         stats = this_cpu_ptr(rcu_dereference(base_chain->stats));
109         if (stats) {
110                 u64_stats_update_begin(&stats->syncp);
111                 stats->pkts++;
112                 stats->bytes += pkt->skb->len;
113                 u64_stats_update_end(&stats->syncp);
114         }
115         local_bh_enable();
116 }
117
118 struct nft_jumpstack {
119         const struct nft_chain  *chain;
120         struct nft_rule *const *rules;
121 };
122
123 static 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
137 unsigned int
138 nft_do_chain(struct nft_pktinfo *pkt, void *priv)
139 {
140         const struct nft_chain *chain = priv, *basechain = chain;
141         const struct net *net = nft_net(pkt);
142         struct nft_rule *const *rules;
143         const struct nft_rule *rule;
144         const struct nft_expr *expr, *last;
145         struct nft_regs regs;
146         unsigned int stackptr = 0;
147         struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
148         bool genbit = READ_ONCE(net->nft.gencursor);
149         struct nft_traceinfo info;
150
151         info.trace = false;
152         if (static_branch_unlikely(&nft_trace_enabled))
153                 nft_trace_init(&info, pkt, &regs.verdict, basechain);
154 do_chain:
155         if (genbit)
156                 rules = rcu_dereference(chain->rules_gen_1);
157         else
158                 rules = rcu_dereference(chain->rules_gen_0);
159
160 next_rule:
161         rule = *rules;
162         regs.verdict.code = NFT_CONTINUE;
163         for (; *rules ; rules++) {
164                 rule = *rules;
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_call_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                 if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
201                         return NF_DROP;
202                 jumpstack[stackptr].chain = chain;
203                 jumpstack[stackptr].rules = rules + 1;
204                 stackptr++;
205                 /* fall through */
206         case NFT_GOTO:
207                 nft_trace_packet(&info, chain, rule,
208                                  NFT_TRACETYPE_RULE);
209
210                 chain = regs.verdict.chain;
211                 goto do_chain;
212         case NFT_CONTINUE:
213                 /* fall through */
214         case NFT_RETURN:
215                 nft_trace_packet(&info, chain, rule,
216                                  NFT_TRACETYPE_RETURN);
217                 break;
218         default:
219                 WARN_ON(1);
220         }
221
222         if (stackptr > 0) {
223                 stackptr--;
224                 chain = jumpstack[stackptr].chain;
225                 rules = jumpstack[stackptr].rules;
226                 goto next_rule;
227         }
228
229         nft_trace_packet(&info, basechain, NULL, NFT_TRACETYPE_POLICY);
230
231         if (static_branch_unlikely(&nft_counters_enabled))
232                 nft_update_chain_stats(basechain, pkt);
233
234         return nft_base_chain(basechain)->policy;
235 }
236 EXPORT_SYMBOL_GPL(nft_do_chain);
237
238 static 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,
247         &nft_meta_type,
248         &nft_rt_type,
249         &nft_exthdr_type,
250 };
251
252 static struct nft_object_type *nft_basic_objects[] = {
253 #ifdef CONFIG_NETWORK_SECMARK
254         &nft_secmark_obj_type,
255 #endif
256 };
257
258 int __init nf_tables_core_module_init(void)
259 {
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         }
267
268         for (j = 0; j < ARRAY_SIZE(nft_basic_types); j++) {
269                 err = nft_register_expr(nft_basic_types[j]);
270                 if (err)
271                         goto err;
272         }
273
274         return 0;
275
276 err:
277         while (j-- > 0)
278                 nft_unregister_expr(nft_basic_types[j]);
279
280         while (i-- > 0)
281                 nft_unregister_obj(nft_basic_objects[i]);
282
283         return err;
284 }
285
286 void nf_tables_core_module_exit(void)
287 {
288         int i;
289
290         i = ARRAY_SIZE(nft_basic_types);
291         while (i-- > 0)
292                 nft_unregister_expr(nft_basic_types[i]);
293
294         i = ARRAY_SIZE(nft_basic_objects);
295         while (i-- > 0)
296                 nft_unregister_obj(nft_basic_objects[i]);
297 }