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> | |
e639f7ab | 19 | #include <linux/static_key.h> |
96518518 PM |
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> | |
b5bc89bf | 24 | #include <net/netfilter/nf_log.h> |
96518518 | 25 | |
9fb0b519 FW |
26 | static const char *const comments[__NFT_TRACETYPE_MAX] = { |
27 | [NFT_TRACETYPE_POLICY] = "policy", | |
28 | [NFT_TRACETYPE_RETURN] = "return", | |
29 | [NFT_TRACETYPE_RULE] = "rule", | |
01ef16c2 PM |
30 | }; |
31 | ||
549d2d41 | 32 | static const struct nf_loginfo trace_loginfo = { |
01ef16c2 PM |
33 | .type = NF_LOG_TYPE_LOG, |
34 | .u = { | |
35 | .log = { | |
a81b2ce8 | 36 | .level = LOGLEVEL_WARNING, |
ff107d27 | 37 | .logflags = NF_LOG_DEFAULT_MASK, |
01ef16c2 PM |
38 | }, |
39 | }, | |
40 | }; | |
41 | ||
33d5a7b1 FW |
42 | static noinline void __nft_trace_packet(struct nft_traceinfo *info, |
43 | const struct nft_chain *chain, | |
9fb0b519 | 44 | int rulenum, enum nft_trace_types type) |
01ef16c2 | 45 | { |
33d5a7b1 FW |
46 | const struct nft_pktinfo *pkt = info->pkt; |
47 | ||
e639f7ab | 48 | if (!info->trace || !pkt->skb->nf_trace) |
33d5a7b1 FW |
49 | return; |
50 | ||
51 | info->chain = chain; | |
52 | info->type = type; | |
53 | ||
54 | nft_trace_notify(info); | |
55 | ||
0e5a1c7e PNA |
56 | nf_log_trace(nft_net(pkt), nft_pf(pkt), nft_hook(pkt), pkt->skb, |
57 | nft_in(pkt), nft_out(pkt), &trace_loginfo, | |
58 | "TRACE: %s:%s:%s:%u ", | |
59 | chain->table->name, chain->name, comments[type], rulenum); | |
01ef16c2 PM |
60 | } |
61 | ||
33d5a7b1 | 62 | static inline void nft_trace_packet(struct nft_traceinfo *info, |
01ef16c2 | 63 | const struct nft_chain *chain, |
33d5a7b1 FW |
64 | const struct nft_rule *rule, |
65 | int rulenum, | |
66 | enum nft_trace_types type) | |
01ef16c2 | 67 | { |
e639f7ab | 68 | if (static_branch_unlikely(&nft_trace_enabled)) { |
33d5a7b1 FW |
69 | info->rule = rule; |
70 | __nft_trace_packet(info, chain, rulenum, type); | |
71 | } | |
01ef16c2 PM |
72 | } |
73 | ||
cb7dbfd0 | 74 | static void nft_cmp_fast_eval(const struct nft_expr *expr, |
a55e22e9 | 75 | struct nft_regs *regs) |
cb7dbfd0 PM |
76 | { |
77 | const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr); | |
b855d416 | 78 | u32 mask = nft_cmp_fast_mask(priv->len); |
cb7dbfd0 | 79 | |
49499c3e | 80 | if ((regs->data[priv->sreg] & mask) == priv->data) |
cb7dbfd0 | 81 | return; |
a55e22e9 | 82 | regs->verdict.code = NFT_BREAK; |
cb7dbfd0 PM |
83 | } |
84 | ||
c29b72e0 | 85 | static bool nft_payload_fast_eval(const struct nft_expr *expr, |
a55e22e9 | 86 | struct nft_regs *regs, |
c29b72e0 PM |
87 | const struct nft_pktinfo *pkt) |
88 | { | |
89 | const struct nft_payload *priv = nft_expr_priv(expr); | |
90 | const struct sk_buff *skb = pkt->skb; | |
49499c3e | 91 | u32 *dest = ®s->data[priv->dreg]; |
c29b72e0 PM |
92 | unsigned char *ptr; |
93 | ||
94 | if (priv->base == NFT_PAYLOAD_NETWORK_HEADER) | |
95 | ptr = skb_network_header(skb); | |
a20877b5 LZ |
96 | else { |
97 | if (!pkt->tprot_set) | |
98 | return false; | |
c54032e0 | 99 | ptr = skb_network_header(skb) + pkt->xt.thoff; |
a20877b5 | 100 | } |
c29b72e0 PM |
101 | |
102 | ptr += priv->offset; | |
103 | ||
8dc3c2b8 | 104 | if (unlikely(ptr + priv->len > skb_tail_pointer(skb))) |
c29b72e0 PM |
105 | return false; |
106 | ||
49499c3e | 107 | *dest = 0; |
c29b72e0 | 108 | if (priv->len == 2) |
fad136ea | 109 | *(u16 *)dest = *(u16 *)ptr; |
c29b72e0 | 110 | else if (priv->len == 4) |
fad136ea | 111 | *(u32 *)dest = *(u32 *)ptr; |
c29b72e0 | 112 | else |
fad136ea | 113 | *(u8 *)dest = *(u8 *)ptr; |
c29b72e0 PM |
114 | return true; |
115 | } | |
116 | ||
9f08ea84 PNA |
117 | DEFINE_STATIC_KEY_FALSE(nft_counters_enabled); |
118 | ||
119 | static noinline void nft_update_chain_stats(const struct nft_chain *chain, | |
120 | const struct nft_pktinfo *pkt) | |
121 | { | |
122 | struct nft_stats *stats; | |
123 | ||
124 | local_bh_disable(); | |
125 | stats = this_cpu_ptr(rcu_dereference(nft_base_chain(chain)->stats)); | |
126 | u64_stats_update_begin(&stats->syncp); | |
127 | stats->pkts++; | |
128 | stats->bytes += pkt->skb->len; | |
129 | u64_stats_update_end(&stats->syncp); | |
130 | local_bh_enable(); | |
131 | } | |
132 | ||
0ca743a5 PNA |
133 | struct nft_jumpstack { |
134 | const struct nft_chain *chain; | |
135 | const struct nft_rule *rule; | |
b5bc89bf | 136 | int rulenum; |
0ca743a5 PNA |
137 | }; |
138 | ||
0ca743a5 | 139 | unsigned int |
06198b34 | 140 | nft_do_chain(struct nft_pktinfo *pkt, void *priv) |
96518518 | 141 | { |
06198b34 | 142 | const struct nft_chain *chain = priv, *basechain = chain; |
0e5a1c7e | 143 | const struct net *net = nft_net(pkt); |
96518518 PM |
144 | const struct nft_rule *rule; |
145 | const struct nft_expr *expr, *last; | |
a55e22e9 | 146 | struct nft_regs regs; |
96518518 | 147 | unsigned int stackptr = 0; |
0ca743a5 | 148 | struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE]; |
d088be80 | 149 | int rulenum; |
ea4bd995 | 150 | unsigned int gencursor = nft_genmask_cur(net); |
33d5a7b1 | 151 | struct nft_traceinfo info; |
96518518 | 152 | |
e639f7ab FW |
153 | info.trace = false; |
154 | if (static_branch_unlikely(&nft_trace_enabled)) | |
155 | nft_trace_init(&info, pkt, ®s.verdict, basechain); | |
96518518 | 156 | do_chain: |
d088be80 | 157 | rulenum = 0; |
96518518 PM |
158 | rule = list_entry(&chain->rules, struct nft_rule, list); |
159 | next_rule: | |
a55e22e9 | 160 | regs.verdict.code = NFT_CONTINUE; |
96518518 | 161 | list_for_each_entry_continue_rcu(rule, &chain->rules, list) { |
0628b123 PNA |
162 | |
163 | /* This rule is not active, skip. */ | |
8fff1722 | 164 | if (unlikely(rule->genmask & gencursor)) |
0628b123 PNA |
165 | continue; |
166 | ||
b5bc89bf PNA |
167 | rulenum++; |
168 | ||
96518518 | 169 | nft_rule_for_each_expr(expr, last, rule) { |
cb7dbfd0 | 170 | if (expr->ops == &nft_cmp_fast_ops) |
a55e22e9 | 171 | nft_cmp_fast_eval(expr, ®s); |
c29b72e0 | 172 | else if (expr->ops != &nft_payload_fast_ops || |
a55e22e9 PM |
173 | !nft_payload_fast_eval(expr, ®s, pkt)) |
174 | expr->ops->eval(expr, ®s, pkt); | |
cb7dbfd0 | 175 | |
a55e22e9 | 176 | if (regs.verdict.code != NFT_CONTINUE) |
96518518 PM |
177 | break; |
178 | } | |
179 | ||
a55e22e9 | 180 | switch (regs.verdict.code) { |
96518518 | 181 | case NFT_BREAK: |
a55e22e9 | 182 | regs.verdict.code = NFT_CONTINUE; |
3b084e99 | 183 | continue; |
96518518 | 184 | case NFT_CONTINUE: |
33d5a7b1 FW |
185 | nft_trace_packet(&info, chain, rule, |
186 | rulenum, NFT_TRACETYPE_RULE); | |
96518518 PM |
187 | continue; |
188 | } | |
189 | break; | |
190 | } | |
191 | ||
a55e22e9 | 192 | switch (regs.verdict.code & NF_VERDICT_MASK) { |
96518518 PM |
193 | case NF_ACCEPT: |
194 | case NF_DROP: | |
195 | case NF_QUEUE: | |
5efa0fc6 | 196 | case NF_STOLEN: |
33d5a7b1 FW |
197 | nft_trace_packet(&info, chain, rule, |
198 | rulenum, NFT_TRACETYPE_RULE); | |
a55e22e9 | 199 | return regs.verdict.code; |
e569bdab EL |
200 | } |
201 | ||
a55e22e9 | 202 | switch (regs.verdict.code) { |
96518518 PM |
203 | case NFT_JUMP: |
204 | BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE); | |
205 | jumpstack[stackptr].chain = chain; | |
206 | jumpstack[stackptr].rule = rule; | |
b5bc89bf | 207 | jumpstack[stackptr].rulenum = rulenum; |
96518518 | 208 | stackptr++; |
354bf5a0 | 209 | /* fall through */ |
96518518 | 210 | case NFT_GOTO: |
33d5a7b1 FW |
211 | nft_trace_packet(&info, chain, rule, |
212 | rulenum, NFT_TRACETYPE_RULE); | |
7b9d5ef9 | 213 | |
a55e22e9 | 214 | chain = regs.verdict.chain; |
96518518 | 215 | goto do_chain; |
354bf5a0 PM |
216 | case NFT_CONTINUE: |
217 | rulenum++; | |
218 | /* fall through */ | |
96518518 | 219 | case NFT_RETURN: |
33d5a7b1 FW |
220 | nft_trace_packet(&info, chain, rule, |
221 | rulenum, NFT_TRACETYPE_RETURN); | |
7e9bc10d | 222 | break; |
96518518 PM |
223 | default: |
224 | WARN_ON(1); | |
225 | } | |
226 | ||
227 | if (stackptr > 0) { | |
228 | stackptr--; | |
229 | chain = jumpstack[stackptr].chain; | |
230 | rule = jumpstack[stackptr].rule; | |
b5bc89bf | 231 | rulenum = jumpstack[stackptr].rulenum; |
96518518 PM |
232 | goto next_rule; |
233 | } | |
234 | ||
33d5a7b1 FW |
235 | nft_trace_packet(&info, basechain, NULL, -1, |
236 | NFT_TRACETYPE_POLICY); | |
5467a512 | 237 | |
9f08ea84 PNA |
238 | if (static_branch_unlikely(&nft_counters_enabled)) |
239 | nft_update_chain_stats(basechain, pkt); | |
b5bc89bf | 240 | |
5467a512 | 241 | return nft_base_chain(basechain)->policy; |
96518518 | 242 | } |
3876d22d | 243 | EXPORT_SYMBOL_GPL(nft_do_chain); |
96518518 | 244 | |
4e24877e LZ |
245 | static struct nft_expr_type *nft_basic_types[] = { |
246 | &nft_imm_type, | |
247 | &nft_cmp_type, | |
248 | &nft_lookup_type, | |
249 | &nft_bitwise_type, | |
250 | &nft_byteorder_type, | |
251 | &nft_payload_type, | |
252 | &nft_dynset_type, | |
253 | &nft_range_type, | |
254 | }; | |
255 | ||
96518518 PM |
256 | int __init nf_tables_core_module_init(void) |
257 | { | |
4e24877e | 258 | int err, i; |
96518518 | 259 | |
4e24877e LZ |
260 | for (i = 0; i < ARRAY_SIZE(nft_basic_types); i++) { |
261 | err = nft_register_expr(nft_basic_types[i]); | |
262 | if (err) | |
263 | goto err; | |
264 | } | |
96518518 | 265 | |
0f3cd9b3 | 266 | return 0; |
4e24877e LZ |
267 | |
268 | err: | |
269 | while (i-- > 0) | |
270 | nft_unregister_expr(nft_basic_types[i]); | |
96518518 PM |
271 | return err; |
272 | } | |
273 | ||
274 | void nf_tables_core_module_exit(void) | |
275 | { | |
4e24877e LZ |
276 | int i; |
277 | ||
278 | i = ARRAY_SIZE(nft_basic_types); | |
279 | while (i-- > 0) | |
280 | nft_unregister_expr(nft_basic_types[i]); | |
96518518 | 281 | } |