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