Root/
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/module.h> |
12 | #include <linux/init.h> |
13 | #include <linux/list.h> |
14 | #include <linux/rculist.h> |
15 | #include <linux/skbuff.h> |
16 | #include <linux/netlink.h> |
17 | #include <linux/netfilter.h> |
18 | #include <linux/netfilter/nfnetlink.h> |
19 | #include <linux/netfilter/nf_tables.h> |
20 | #include <net/netfilter/nf_tables_core.h> |
21 | #include <net/netfilter/nf_tables.h> |
22 | #include <net/netfilter/nf_log.h> |
23 | |
24 | static void nft_cmp_fast_eval(const struct nft_expr *expr, |
25 | struct nft_data data[NFT_REG_MAX + 1]) |
26 | { |
27 | const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr); |
28 | u32 mask; |
29 | |
30 | mask = ~0U >> (sizeof(priv->data) * BITS_PER_BYTE - priv->len); |
31 | if ((data[priv->sreg].data[0] & mask) == priv->data) |
32 | return; |
33 | data[NFT_REG_VERDICT].verdict = NFT_BREAK; |
34 | } |
35 | |
36 | static bool nft_payload_fast_eval(const struct nft_expr *expr, |
37 | struct nft_data data[NFT_REG_MAX + 1], |
38 | const struct nft_pktinfo *pkt) |
39 | { |
40 | const struct nft_payload *priv = nft_expr_priv(expr); |
41 | const struct sk_buff *skb = pkt->skb; |
42 | struct nft_data *dest = &data[priv->dreg]; |
43 | unsigned char *ptr; |
44 | |
45 | if (priv->base == NFT_PAYLOAD_NETWORK_HEADER) |
46 | ptr = skb_network_header(skb); |
47 | else |
48 | ptr = skb_network_header(skb) + pkt->xt.thoff; |
49 | |
50 | ptr += priv->offset; |
51 | |
52 | if (unlikely(ptr + priv->len >= skb_tail_pointer(skb))) |
53 | return false; |
54 | |
55 | if (priv->len == 2) |
56 | *(u16 *)dest->data = *(u16 *)ptr; |
57 | else if (priv->len == 4) |
58 | *(u32 *)dest->data = *(u32 *)ptr; |
59 | else |
60 | *(u8 *)dest->data = *(u8 *)ptr; |
61 | return true; |
62 | } |
63 | |
64 | struct nft_jumpstack { |
65 | const struct nft_chain *chain; |
66 | const struct nft_rule *rule; |
67 | int rulenum; |
68 | }; |
69 | |
70 | static inline void |
71 | nft_chain_stats(const struct nft_chain *this, const struct nft_pktinfo *pkt, |
72 | struct nft_jumpstack *jumpstack, unsigned int stackptr) |
73 | { |
74 | struct nft_stats __percpu *stats; |
75 | const struct nft_chain *chain = stackptr ? jumpstack[0].chain : this; |
76 | |
77 | rcu_read_lock_bh(); |
78 | stats = rcu_dereference(nft_base_chain(chain)->stats); |
79 | __this_cpu_inc(stats->pkts); |
80 | __this_cpu_add(stats->bytes, pkt->skb->len); |
81 | rcu_read_unlock_bh(); |
82 | } |
83 | |
84 | enum nft_trace { |
85 | NFT_TRACE_RULE, |
86 | NFT_TRACE_RETURN, |
87 | NFT_TRACE_POLICY, |
88 | }; |
89 | |
90 | static const char *const comments[] = { |
91 | [NFT_TRACE_RULE] = "rule", |
92 | [NFT_TRACE_RETURN] = "return", |
93 | [NFT_TRACE_POLICY] = "policy", |
94 | }; |
95 | |
96 | static struct nf_loginfo trace_loginfo = { |
97 | .type = NF_LOG_TYPE_LOG, |
98 | .u = { |
99 | .log = { |
100 | .level = 4, |
101 | .logflags = NF_LOG_MASK, |
102 | }, |
103 | }, |
104 | }; |
105 | |
106 | static inline void nft_trace_packet(const struct nft_pktinfo *pkt, |
107 | const struct nft_chain *chain, |
108 | int rulenum, enum nft_trace type) |
109 | { |
110 | struct net *net = dev_net(pkt->in ? pkt->in : pkt->out); |
111 | |
112 | nf_log_packet(net, pkt->xt.family, pkt->hooknum, pkt->skb, pkt->in, |
113 | pkt->out, &trace_loginfo, "TRACE: %s:%s:%s:%u ", |
114 | chain->table->name, chain->name, comments[type], |
115 | rulenum); |
116 | } |
117 | |
118 | unsigned int |
119 | nft_do_chain_pktinfo(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops) |
120 | { |
121 | const struct nft_chain *chain = ops->priv; |
122 | const struct nft_rule *rule; |
123 | const struct nft_expr *expr, *last; |
124 | struct nft_data data[NFT_REG_MAX + 1]; |
125 | unsigned int stackptr = 0; |
126 | struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE]; |
127 | int rulenum = 0; |
128 | /* |
129 | * Cache cursor to avoid problems in case that the cursor is updated |
130 | * while traversing the ruleset. |
131 | */ |
132 | unsigned int gencursor = ACCESS_ONCE(chain->net->nft.gencursor); |
133 | |
134 | do_chain: |
135 | rule = list_entry(&chain->rules, struct nft_rule, list); |
136 | next_rule: |
137 | data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; |
138 | list_for_each_entry_continue_rcu(rule, &chain->rules, list) { |
139 | |
140 | /* This rule is not active, skip. */ |
141 | if (unlikely(rule->genmask & (1 << gencursor))) |
142 | continue; |
143 | |
144 | rulenum++; |
145 | |
146 | nft_rule_for_each_expr(expr, last, rule) { |
147 | if (expr->ops == &nft_cmp_fast_ops) |
148 | nft_cmp_fast_eval(expr, data); |
149 | else if (expr->ops != &nft_payload_fast_ops || |
150 | !nft_payload_fast_eval(expr, data, pkt)) |
151 | expr->ops->eval(expr, data, pkt); |
152 | |
153 | if (data[NFT_REG_VERDICT].verdict != NFT_CONTINUE) |
154 | break; |
155 | } |
156 | |
157 | switch (data[NFT_REG_VERDICT].verdict) { |
158 | case NFT_BREAK: |
159 | data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; |
160 | /* fall through */ |
161 | case NFT_CONTINUE: |
162 | continue; |
163 | } |
164 | break; |
165 | } |
166 | |
167 | switch (data[NFT_REG_VERDICT].verdict) { |
168 | case NF_ACCEPT: |
169 | case NF_DROP: |
170 | case NF_QUEUE: |
171 | if (unlikely(pkt->skb->nf_trace)) |
172 | nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE); |
173 | |
174 | return data[NFT_REG_VERDICT].verdict; |
175 | case NFT_JUMP: |
176 | if (unlikely(pkt->skb->nf_trace)) |
177 | nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE); |
178 | |
179 | BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE); |
180 | jumpstack[stackptr].chain = chain; |
181 | jumpstack[stackptr].rule = rule; |
182 | jumpstack[stackptr].rulenum = rulenum; |
183 | stackptr++; |
184 | /* fall through */ |
185 | case NFT_GOTO: |
186 | chain = data[NFT_REG_VERDICT].chain; |
187 | goto do_chain; |
188 | case NFT_RETURN: |
189 | if (unlikely(pkt->skb->nf_trace)) |
190 | nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN); |
191 | |
192 | /* fall through */ |
193 | case NFT_CONTINUE: |
194 | break; |
195 | default: |
196 | WARN_ON(1); |
197 | } |
198 | |
199 | if (stackptr > 0) { |
200 | if (unlikely(pkt->skb->nf_trace)) |
201 | nft_trace_packet(pkt, chain, ++rulenum, NFT_TRACE_RETURN); |
202 | |
203 | stackptr--; |
204 | chain = jumpstack[stackptr].chain; |
205 | rule = jumpstack[stackptr].rule; |
206 | rulenum = jumpstack[stackptr].rulenum; |
207 | goto next_rule; |
208 | } |
209 | nft_chain_stats(chain, pkt, jumpstack, stackptr); |
210 | |
211 | if (unlikely(pkt->skb->nf_trace)) |
212 | nft_trace_packet(pkt, chain, ++rulenum, NFT_TRACE_POLICY); |
213 | |
214 | return nft_base_chain(chain)->policy; |
215 | } |
216 | EXPORT_SYMBOL_GPL(nft_do_chain_pktinfo); |
217 | |
218 | int __init nf_tables_core_module_init(void) |
219 | { |
220 | int err; |
221 | |
222 | err = nft_immediate_module_init(); |
223 | if (err < 0) |
224 | goto err1; |
225 | |
226 | err = nft_cmp_module_init(); |
227 | if (err < 0) |
228 | goto err2; |
229 | |
230 | err = nft_lookup_module_init(); |
231 | if (err < 0) |
232 | goto err3; |
233 | |
234 | err = nft_bitwise_module_init(); |
235 | if (err < 0) |
236 | goto err4; |
237 | |
238 | err = nft_byteorder_module_init(); |
239 | if (err < 0) |
240 | goto err5; |
241 | |
242 | err = nft_payload_module_init(); |
243 | if (err < 0) |
244 | goto err6; |
245 | |
246 | return 0; |
247 | |
248 | err6: |
249 | nft_byteorder_module_exit(); |
250 | err5: |
251 | nft_bitwise_module_exit(); |
252 | err4: |
253 | nft_lookup_module_exit(); |
254 | err3: |
255 | nft_cmp_module_exit(); |
256 | err2: |
257 | nft_immediate_module_exit(); |
258 | err1: |
259 | return err; |
260 | } |
261 | |
262 | void nf_tables_core_module_exit(void) |
263 | { |
264 | nft_payload_module_exit(); |
265 | nft_byteorder_module_exit(); |
266 | nft_bitwise_module_exit(); |
267 | nft_lookup_module_exit(); |
268 | nft_cmp_module_exit(); |
269 | nft_immediate_module_exit(); |
270 | } |
271 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9