~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nf_tables_core.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 __percpu *pstats;
102         struct nft_stats *stats;
103 
104         base_chain = nft_base_chain(chain);
105 
106         rcu_read_lock();
107         pstats = READ_ONCE(base_chain->stats);
108         if (pstats) {
109                 local_bh_disable();
110                 stats = this_cpu_ptr(pstats);
111                 u64_stats_update_begin(&stats->syncp);
112                 stats->pkts++;
113                 stats->bytes += pkt->skb->len;
114                 u64_stats_update_end(&stats->syncp);
115                 local_bh_enable();
116         }
117         rcu_read_unlock();
118 }
119 
120 struct nft_jumpstack {
121         const struct nft_chain  *chain;
122         struct nft_rule *const *rules;
123 };
124 
125 static void expr_call_ops_eval(const struct nft_expr *expr,
126                                struct nft_regs *regs,
127                                struct nft_pktinfo *pkt)
128 {
129 #ifdef CONFIG_RETPOLINE
130         unsigned long e = (unsigned long)expr->ops->eval;
131 #define X(e, fun) \
132         do { if ((e) == (unsigned long)(fun)) \
133                 return fun(expr, regs, pkt); } while (0)
134 
135         X(e, nft_payload_eval);
136         X(e, nft_cmp_eval);
137         X(e, nft_meta_get_eval);
138         X(e, nft_lookup_eval);
139         X(e, nft_range_eval);
140         X(e, nft_immediate_eval);
141         X(e, nft_byteorder_eval);
142         X(e, nft_dynset_eval);
143         X(e, nft_rt_get_eval);
144         X(e, nft_bitwise_eval);
145 #undef  X
146 #endif /* CONFIG_RETPOLINE */
147         expr->ops->eval(expr, regs, pkt);
148 }
149 
150 unsigned int
151 nft_do_chain(struct nft_pktinfo *pkt, void *priv)
152 {
153         const struct nft_chain *chain = priv, *basechain = chain;
154         const struct net *net = nft_net(pkt);
155         struct nft_rule *const *rules;
156         const struct nft_rule *rule;
157         const struct nft_expr *expr, *last;
158         struct nft_regs regs;
159         unsigned int stackptr = 0;
160         struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
161         bool genbit = READ_ONCE(net->nft.gencursor);
162         struct nft_traceinfo info;
163 
164         info.trace = false;
165         if (static_branch_unlikely(&nft_trace_enabled))
166                 nft_trace_init(&info, pkt, &regs.verdict, basechain);
167 do_chain:
168         if (genbit)
169                 rules = rcu_dereference(chain->rules_gen_1);
170         else
171                 rules = rcu_dereference(chain->rules_gen_0);
172 
173 next_rule:
174         rule = *rules;
175         regs.verdict.code = NFT_CONTINUE;
176         for (; *rules ; rules++) {
177                 rule = *rules;
178                 nft_rule_for_each_expr(expr, last, rule) {
179                         if (expr->ops == &nft_cmp_fast_ops)
180                                 nft_cmp_fast_eval(expr, &regs);
181                         else if (expr->ops != &nft_payload_fast_ops ||
182                                  !nft_payload_fast_eval(expr, &regs, pkt))
183                                 expr_call_ops_eval(expr, &regs, pkt);
184 
185                         if (regs.verdict.code != NFT_CONTINUE)
186                                 break;
187                 }
188 
189                 switch (regs.verdict.code) {
190                 case NFT_BREAK:
191                         regs.verdict.code = NFT_CONTINUE;
192                         continue;
193                 case NFT_CONTINUE:
194                         nft_trace_packet(&info, chain, rule,
195                                          NFT_TRACETYPE_RULE);
196                         continue;
197                 }
198                 break;
199         }
200 
201         switch (regs.verdict.code & NF_VERDICT_MASK) {
202         case NF_ACCEPT:
203         case NF_DROP:
204         case NF_QUEUE:
205         case NF_STOLEN:
206                 nft_trace_packet(&info, chain, rule,
207                                  NFT_TRACETYPE_RULE);
208                 return regs.verdict.code;
209         }
210 
211         switch (regs.verdict.code) {
212         case NFT_JUMP:
213                 if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
214                         return NF_DROP;
215                 jumpstack[stackptr].chain = chain;
216                 jumpstack[stackptr].rules = rules + 1;
217                 stackptr++;
218                 /* fall through */
219         case NFT_GOTO:
220                 nft_trace_packet(&info, chain, rule,
221                                  NFT_TRACETYPE_RULE);
222 
223                 chain = regs.verdict.chain;
224                 goto do_chain;
225         case NFT_CONTINUE:
226         case NFT_RETURN:
227                 nft_trace_packet(&info, chain, rule,
228                                  NFT_TRACETYPE_RETURN);
229                 break;
230         default:
231                 WARN_ON(1);
232         }
233 
234         if (stackptr > 0) {
235                 stackptr--;
236                 chain = jumpstack[stackptr].chain;
237                 rules = jumpstack[stackptr].rules;
238                 goto next_rule;
239         }
240 
241         nft_trace_packet(&info, basechain, NULL, NFT_TRACETYPE_POLICY);
242 
243         if (static_branch_unlikely(&nft_counters_enabled))
244                 nft_update_chain_stats(basechain, pkt);
245 
246         return nft_base_chain(basechain)->policy;
247 }
248 EXPORT_SYMBOL_GPL(nft_do_chain);
249 
250 static struct nft_expr_type *nft_basic_types[] = {
251         &nft_imm_type,
252         &nft_cmp_type,
253         &nft_lookup_type,
254         &nft_bitwise_type,
255         &nft_byteorder_type,
256         &nft_payload_type,
257         &nft_dynset_type,
258         &nft_range_type,
259         &nft_meta_type,
260         &nft_rt_type,
261         &nft_exthdr_type,
262 };
263 
264 static struct nft_object_type *nft_basic_objects[] = {
265 #ifdef CONFIG_NETWORK_SECMARK
266         &nft_secmark_obj_type,
267 #endif
268 };
269 
270 int __init nf_tables_core_module_init(void)
271 {
272         int err, i, j = 0;
273 
274         for (i = 0; i < ARRAY_SIZE(nft_basic_objects); i++) {
275                 err = nft_register_obj(nft_basic_objects[i]);
276                 if (err)
277                         goto err;
278         }
279 
280         for (j = 0; j < ARRAY_SIZE(nft_basic_types); j++) {
281                 err = nft_register_expr(nft_basic_types[j]);
282                 if (err)
283                         goto err;
284         }
285 
286         return 0;
287 
288 err:
289         while (j-- > 0)
290                 nft_unregister_expr(nft_basic_types[j]);
291 
292         while (i-- > 0)
293                 nft_unregister_obj(nft_basic_objects[i]);
294 
295         return err;
296 }
297 
298 void nf_tables_core_module_exit(void)
299 {
300         int i;
301 
302         i = ARRAY_SIZE(nft_basic_types);
303         while (i-- > 0)
304                 nft_unregister_expr(nft_basic_types[i]);
305 
306         i = ARRAY_SIZE(nft_basic_objects);
307         while (i-- > 0)
308                 nft_unregister_obj(nft_basic_objects[i]);
309 }
310 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp