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