]>
Commit | Line | Data |
---|---|---|
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> | |
19 | #include <linux/netfilter/nfnetlink.h> | |
20 | #include <linux/netfilter/nf_tables.h> | |
21 | #include <net/netfilter/nf_tables_core.h> | |
22 | #include <net/netfilter/nf_tables.h> | |
b5bc89bf | 23 | #include <net/netfilter/nf_log.h> |
96518518 | 24 | |
01ef16c2 PM |
25 | enum nft_trace { |
26 | NFT_TRACE_RULE, | |
27 | NFT_TRACE_RETURN, | |
28 | NFT_TRACE_POLICY, | |
29 | }; | |
30 | ||
31 | static const char *const comments[] = { | |
32 | [NFT_TRACE_RULE] = "rule", | |
33 | [NFT_TRACE_RETURN] = "return", | |
34 | [NFT_TRACE_POLICY] = "policy", | |
35 | }; | |
36 | ||
37 | static struct nf_loginfo trace_loginfo = { | |
38 | .type = NF_LOG_TYPE_LOG, | |
39 | .u = { | |
40 | .log = { | |
a81b2ce8 | 41 | .level = LOGLEVEL_WARNING, |
01ef16c2 PM |
42 | .logflags = NF_LOG_MASK, |
43 | }, | |
44 | }, | |
45 | }; | |
46 | ||
47 | static void __nft_trace_packet(const struct nft_pktinfo *pkt, | |
48 | const struct nft_chain *chain, | |
49 | int rulenum, enum nft_trace type) | |
50 | { | |
88182a0e | 51 | nf_log_trace(pkt->net, pkt->pf, pkt->hook, pkt->skb, pkt->in, |
fce1528e PNA |
52 | pkt->out, &trace_loginfo, "TRACE: %s:%s:%s:%u ", |
53 | chain->table->name, chain->name, comments[type], | |
54 | rulenum); | |
01ef16c2 PM |
55 | } |
56 | ||
57 | static inline void nft_trace_packet(const struct nft_pktinfo *pkt, | |
58 | const struct nft_chain *chain, | |
59 | int rulenum, enum nft_trace type) | |
60 | { | |
61 | if (unlikely(pkt->skb->nf_trace)) | |
62 | __nft_trace_packet(pkt, chain, rulenum, type); | |
63 | } | |
64 | ||
cb7dbfd0 | 65 | static void nft_cmp_fast_eval(const struct nft_expr *expr, |
a55e22e9 | 66 | struct nft_regs *regs) |
cb7dbfd0 PM |
67 | { |
68 | const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr); | |
b855d416 | 69 | u32 mask = nft_cmp_fast_mask(priv->len); |
cb7dbfd0 | 70 | |
49499c3e | 71 | if ((regs->data[priv->sreg] & mask) == priv->data) |
cb7dbfd0 | 72 | return; |
a55e22e9 | 73 | regs->verdict.code = NFT_BREAK; |
cb7dbfd0 PM |
74 | } |
75 | ||
c29b72e0 | 76 | static bool nft_payload_fast_eval(const struct nft_expr *expr, |
a55e22e9 | 77 | struct nft_regs *regs, |
c29b72e0 PM |
78 | const struct nft_pktinfo *pkt) |
79 | { | |
80 | const struct nft_payload *priv = nft_expr_priv(expr); | |
81 | const struct sk_buff *skb = pkt->skb; | |
49499c3e | 82 | u32 *dest = ®s->data[priv->dreg]; |
c29b72e0 PM |
83 | unsigned char *ptr; |
84 | ||
85 | if (priv->base == NFT_PAYLOAD_NETWORK_HEADER) | |
86 | ptr = skb_network_header(skb); | |
87 | else | |
c54032e0 | 88 | ptr = skb_network_header(skb) + pkt->xt.thoff; |
c29b72e0 PM |
89 | |
90 | ptr += priv->offset; | |
91 | ||
92 | if (unlikely(ptr + priv->len >= skb_tail_pointer(skb))) | |
93 | return false; | |
94 | ||
49499c3e | 95 | *dest = 0; |
c29b72e0 | 96 | if (priv->len == 2) |
fad136ea | 97 | *(u16 *)dest = *(u16 *)ptr; |
c29b72e0 | 98 | else if (priv->len == 4) |
fad136ea | 99 | *(u32 *)dest = *(u32 *)ptr; |
c29b72e0 | 100 | else |
fad136ea | 101 | *(u8 *)dest = *(u8 *)ptr; |
c29b72e0 PM |
102 | return true; |
103 | } | |
104 | ||
0ca743a5 PNA |
105 | struct nft_jumpstack { |
106 | const struct nft_chain *chain; | |
107 | const struct nft_rule *rule; | |
b5bc89bf | 108 | int rulenum; |
0ca743a5 PNA |
109 | }; |
110 | ||
0ca743a5 | 111 | unsigned int |
06198b34 | 112 | nft_do_chain(struct nft_pktinfo *pkt, void *priv) |
96518518 | 113 | { |
06198b34 | 114 | const struct nft_chain *chain = priv, *basechain = chain; |
88182a0e | 115 | const struct net *net = pkt->net; |
96518518 PM |
116 | const struct nft_rule *rule; |
117 | const struct nft_expr *expr, *last; | |
a55e22e9 | 118 | struct nft_regs regs; |
96518518 | 119 | unsigned int stackptr = 0; |
0ca743a5 | 120 | struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE]; |
ce355e20 | 121 | struct nft_stats *stats; |
d088be80 | 122 | int rulenum; |
ea4bd995 | 123 | unsigned int gencursor = nft_genmask_cur(net); |
96518518 PM |
124 | |
125 | do_chain: | |
d088be80 | 126 | rulenum = 0; |
96518518 PM |
127 | rule = list_entry(&chain->rules, struct nft_rule, list); |
128 | next_rule: | |
a55e22e9 | 129 | regs.verdict.code = NFT_CONTINUE; |
96518518 | 130 | list_for_each_entry_continue_rcu(rule, &chain->rules, list) { |
0628b123 PNA |
131 | |
132 | /* This rule is not active, skip. */ | |
133 | if (unlikely(rule->genmask & (1 << gencursor))) | |
134 | continue; | |
135 | ||
b5bc89bf PNA |
136 | rulenum++; |
137 | ||
96518518 | 138 | nft_rule_for_each_expr(expr, last, rule) { |
cb7dbfd0 | 139 | if (expr->ops == &nft_cmp_fast_ops) |
a55e22e9 | 140 | nft_cmp_fast_eval(expr, ®s); |
c29b72e0 | 141 | else if (expr->ops != &nft_payload_fast_ops || |
a55e22e9 PM |
142 | !nft_payload_fast_eval(expr, ®s, pkt)) |
143 | expr->ops->eval(expr, ®s, pkt); | |
cb7dbfd0 | 144 | |
a55e22e9 | 145 | if (regs.verdict.code != NFT_CONTINUE) |
96518518 PM |
146 | break; |
147 | } | |
148 | ||
a55e22e9 | 149 | switch (regs.verdict.code) { |
96518518 | 150 | case NFT_BREAK: |
a55e22e9 | 151 | regs.verdict.code = NFT_CONTINUE; |
3b084e99 | 152 | continue; |
96518518 | 153 | case NFT_CONTINUE: |
01ef16c2 | 154 | nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE); |
96518518 PM |
155 | continue; |
156 | } | |
157 | break; | |
158 | } | |
159 | ||
a55e22e9 | 160 | switch (regs.verdict.code & NF_VERDICT_MASK) { |
96518518 PM |
161 | case NF_ACCEPT: |
162 | case NF_DROP: | |
163 | case NF_QUEUE: | |
01ef16c2 | 164 | nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE); |
a55e22e9 | 165 | return regs.verdict.code; |
e569bdab EL |
166 | } |
167 | ||
a55e22e9 | 168 | switch (regs.verdict.code) { |
96518518 PM |
169 | case NFT_JUMP: |
170 | BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE); | |
171 | jumpstack[stackptr].chain = chain; | |
172 | jumpstack[stackptr].rule = rule; | |
b5bc89bf | 173 | jumpstack[stackptr].rulenum = rulenum; |
96518518 | 174 | stackptr++; |
354bf5a0 | 175 | /* fall through */ |
96518518 | 176 | case NFT_GOTO: |
01ef16c2 | 177 | nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE); |
7b9d5ef9 | 178 | |
a55e22e9 | 179 | chain = regs.verdict.chain; |
96518518 | 180 | goto do_chain; |
354bf5a0 PM |
181 | case NFT_CONTINUE: |
182 | rulenum++; | |
183 | /* fall through */ | |
96518518 | 184 | case NFT_RETURN: |
01ef16c2 | 185 | nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN); |
7e9bc10d | 186 | break; |
96518518 PM |
187 | default: |
188 | WARN_ON(1); | |
189 | } | |
190 | ||
191 | if (stackptr > 0) { | |
192 | stackptr--; | |
193 | chain = jumpstack[stackptr].chain; | |
194 | rule = jumpstack[stackptr].rule; | |
b5bc89bf | 195 | rulenum = jumpstack[stackptr].rulenum; |
96518518 PM |
196 | goto next_rule; |
197 | } | |
198 | ||
01ef16c2 | 199 | nft_trace_packet(pkt, basechain, -1, NFT_TRACE_POLICY); |
5467a512 PNA |
200 | |
201 | rcu_read_lock_bh(); | |
ce355e20 ED |
202 | stats = this_cpu_ptr(rcu_dereference(nft_base_chain(basechain)->stats)); |
203 | u64_stats_update_begin(&stats->syncp); | |
204 | stats->pkts++; | |
205 | stats->bytes += pkt->skb->len; | |
206 | u64_stats_update_end(&stats->syncp); | |
5467a512 | 207 | rcu_read_unlock_bh(); |
b5bc89bf | 208 | |
5467a512 | 209 | return nft_base_chain(basechain)->policy; |
96518518 | 210 | } |
3876d22d | 211 | EXPORT_SYMBOL_GPL(nft_do_chain); |
96518518 PM |
212 | |
213 | int __init nf_tables_core_module_init(void) | |
214 | { | |
215 | int err; | |
216 | ||
217 | err = nft_immediate_module_init(); | |
218 | if (err < 0) | |
219 | goto err1; | |
220 | ||
221 | err = nft_cmp_module_init(); | |
222 | if (err < 0) | |
223 | goto err2; | |
224 | ||
225 | err = nft_lookup_module_init(); | |
226 | if (err < 0) | |
227 | goto err3; | |
228 | ||
229 | err = nft_bitwise_module_init(); | |
230 | if (err < 0) | |
231 | goto err4; | |
232 | ||
233 | err = nft_byteorder_module_init(); | |
234 | if (err < 0) | |
235 | goto err5; | |
236 | ||
237 | err = nft_payload_module_init(); | |
238 | if (err < 0) | |
239 | goto err6; | |
240 | ||
22fe54d5 PM |
241 | err = nft_dynset_module_init(); |
242 | if (err < 0) | |
243 | goto err7; | |
244 | ||
96518518 PM |
245 | return 0; |
246 | ||
22fe54d5 PM |
247 | err7: |
248 | nft_payload_module_exit(); | |
96518518 PM |
249 | err6: |
250 | nft_byteorder_module_exit(); | |
251 | err5: | |
252 | nft_bitwise_module_exit(); | |
253 | err4: | |
254 | nft_lookup_module_exit(); | |
255 | err3: | |
256 | nft_cmp_module_exit(); | |
257 | err2: | |
258 | nft_immediate_module_exit(); | |
259 | err1: | |
260 | return err; | |
261 | } | |
262 | ||
263 | void nf_tables_core_module_exit(void) | |
264 | { | |
22fe54d5 | 265 | nft_dynset_module_exit(); |
96518518 PM |
266 | nft_payload_module_exit(); |
267 | nft_byteorder_module_exit(); | |
268 | nft_bitwise_module_exit(); | |
269 | nft_lookup_module_exit(); | |
270 | nft_cmp_module_exit(); | |
271 | nft_immediate_module_exit(); | |
272 | } |