nf_tables_core.c 6.5 KB
Newer Older
P
Patrick McHardy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Development of this code funded by Astaro AG (http://www.astaro.com/)
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/rculist.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nf_tables.h>
#include <net/netfilter/nf_tables_core.h>
#include <net/netfilter/nf_tables.h>
22
#include <net/netfilter/nf_log.h>
P
Patrick McHardy 已提交
23

24 25 26 27
static void nft_cmp_fast_eval(const struct nft_expr *expr,
			      struct nft_data data[NFT_REG_MAX + 1])
{
	const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
28
	u32 mask = nft_cmp_fast_mask(priv->len);
29 30 31 32 33 34

	if ((data[priv->sreg].data[0] & mask) == priv->data)
		return;
	data[NFT_REG_VERDICT].verdict = NFT_BREAK;
}

35 36 37 38 39 40 41 42 43 44 45 46
static bool nft_payload_fast_eval(const struct nft_expr *expr,
				  struct nft_data data[NFT_REG_MAX + 1],
				  const struct nft_pktinfo *pkt)
{
	const struct nft_payload *priv = nft_expr_priv(expr);
	const struct sk_buff *skb = pkt->skb;
	struct nft_data *dest = &data[priv->dreg];
	unsigned char *ptr;

	if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
		ptr = skb_network_header(skb);
	else
47
		ptr = skb_network_header(skb) + pkt->xt.thoff;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

	ptr += priv->offset;

	if (unlikely(ptr + priv->len >= skb_tail_pointer(skb)))
		return false;

	if (priv->len == 2)
		*(u16 *)dest->data = *(u16 *)ptr;
	else if (priv->len == 4)
		*(u32 *)dest->data = *(u32 *)ptr;
	else
		*(u8 *)dest->data = *(u8 *)ptr;
	return true;
}

63 64 65
struct nft_jumpstack {
	const struct nft_chain	*chain;
	const struct nft_rule	*rule;
66
	int			rulenum;
67 68
};

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
enum nft_trace {
	NFT_TRACE_RULE,
	NFT_TRACE_RETURN,
	NFT_TRACE_POLICY,
};

static const char *const comments[] = {
	[NFT_TRACE_RULE]	= "rule",
	[NFT_TRACE_RETURN]	= "return",
	[NFT_TRACE_POLICY]	= "policy",
};

static struct nf_loginfo trace_loginfo = {
	.type = NF_LOG_TYPE_LOG,
	.u = {
		.log = {
			.level = 4,
			.logflags = NF_LOG_MASK,
	        },
	},
};

91 92 93
static void nft_trace_packet(const struct nft_pktinfo *pkt,
			     const struct nft_chain *chain,
			     int rulenum, enum nft_trace type)
94 95 96
{
	struct net *net = dev_net(pkt->in ? pkt->in : pkt->out);

97
	nf_log_packet(net, pkt->xt.family, pkt->ops->hooknum, pkt->skb, pkt->in,
98 99 100 101 102
		      pkt->out, &trace_loginfo, "TRACE: %s:%s:%s:%u ",
		      chain->table->name, chain->name, comments[type],
		      rulenum);
}

103
unsigned int
104
nft_do_chain(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops)
P
Patrick McHardy 已提交
105
{
106
	const struct nft_chain *chain = ops->priv, *basechain = chain;
P
Patrick McHardy 已提交
107 108 109 110
	const struct nft_rule *rule;
	const struct nft_expr *expr, *last;
	struct nft_data data[NFT_REG_MAX + 1];
	unsigned int stackptr = 0;
111
	struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
112
	struct nft_stats *stats;
113
	int rulenum;
114 115 116 117 118
	/*
	 * Cache cursor to avoid problems in case that the cursor is updated
	 * while traversing the ruleset.
	 */
	unsigned int gencursor = ACCESS_ONCE(chain->net->nft.gencursor);
P
Patrick McHardy 已提交
119 120

do_chain:
121
	rulenum = 0;
P
Patrick McHardy 已提交
122 123 124 125
	rule = list_entry(&chain->rules, struct nft_rule, list);
next_rule:
	data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
	list_for_each_entry_continue_rcu(rule, &chain->rules, list) {
126 127 128 129 130

		/* This rule is not active, skip. */
		if (unlikely(rule->genmask & (1 << gencursor)))
			continue;

131 132
		rulenum++;

P
Patrick McHardy 已提交
133
		nft_rule_for_each_expr(expr, last, rule) {
134 135
			if (expr->ops == &nft_cmp_fast_ops)
				nft_cmp_fast_eval(expr, data);
136
			else if (expr->ops != &nft_payload_fast_ops ||
137 138
				 !nft_payload_fast_eval(expr, data, pkt))
				expr->ops->eval(expr, data, pkt);
139

P
Patrick McHardy 已提交
140 141 142 143 144 145 146
			if (data[NFT_REG_VERDICT].verdict != NFT_CONTINUE)
				break;
		}

		switch (data[NFT_REG_VERDICT].verdict) {
		case NFT_BREAK:
			data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
147
			continue;
P
Patrick McHardy 已提交
148
		case NFT_CONTINUE:
149 150
			if (unlikely(pkt->skb->nf_trace))
				nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
P
Patrick McHardy 已提交
151 152 153 154 155
			continue;
		}
		break;
	}

156
	switch (data[NFT_REG_VERDICT].verdict & NF_VERDICT_MASK) {
P
Patrick McHardy 已提交
157 158 159
	case NF_ACCEPT:
	case NF_DROP:
	case NF_QUEUE:
160 161 162
		if (unlikely(pkt->skb->nf_trace))
			nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);

P
Patrick McHardy 已提交
163
		return data[NFT_REG_VERDICT].verdict;
164 165 166
	}

	switch (data[NFT_REG_VERDICT].verdict) {
P
Patrick McHardy 已提交
167
	case NFT_JUMP:
168 169 170
		if (unlikely(pkt->skb->nf_trace))
			nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);

P
Patrick McHardy 已提交
171 172 173
		BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE);
		jumpstack[stackptr].chain = chain;
		jumpstack[stackptr].rule  = rule;
174
		jumpstack[stackptr].rulenum = rulenum;
P
Patrick McHardy 已提交
175
		stackptr++;
176 177
		chain = data[NFT_REG_VERDICT].chain;
		goto do_chain;
P
Patrick McHardy 已提交
178
	case NFT_GOTO:
179 180 181
		if (unlikely(pkt->skb->nf_trace))
			nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);

P
Patrick McHardy 已提交
182 183 184
		chain = data[NFT_REG_VERDICT].chain;
		goto do_chain;
	case NFT_RETURN:
185 186
		if (unlikely(pkt->skb->nf_trace))
			nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN);
187
		break;
P
Patrick McHardy 已提交
188
	case NFT_CONTINUE:
189 190
		if (unlikely(pkt->skb->nf_trace && !(chain->flags & NFT_BASE_CHAIN)))
			nft_trace_packet(pkt, chain, ++rulenum, NFT_TRACE_RETURN);
P
Patrick McHardy 已提交
191 192 193 194 195 196 197 198 199
		break;
	default:
		WARN_ON(1);
	}

	if (stackptr > 0) {
		stackptr--;
		chain = jumpstack[stackptr].chain;
		rule  = jumpstack[stackptr].rule;
200
		rulenum = jumpstack[stackptr].rulenum;
P
Patrick McHardy 已提交
201 202 203
		goto next_rule;
	}

204
	if (unlikely(pkt->skb->nf_trace))
205
		nft_trace_packet(pkt, basechain, -1, NFT_TRACE_POLICY);
206 207

	rcu_read_lock_bh();
208 209 210 211 212
	stats = this_cpu_ptr(rcu_dereference(nft_base_chain(basechain)->stats));
	u64_stats_update_begin(&stats->syncp);
	stats->pkts++;
	stats->bytes += pkt->skb->len;
	u64_stats_update_end(&stats->syncp);
213
	rcu_read_unlock_bh();
214

215
	return nft_base_chain(basechain)->policy;
P
Patrick McHardy 已提交
216
}
217
EXPORT_SYMBOL_GPL(nft_do_chain);
P
Patrick McHardy 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

int __init nf_tables_core_module_init(void)
{
	int err;

	err = nft_immediate_module_init();
	if (err < 0)
		goto err1;

	err = nft_cmp_module_init();
	if (err < 0)
		goto err2;

	err = nft_lookup_module_init();
	if (err < 0)
		goto err3;

	err = nft_bitwise_module_init();
	if (err < 0)
		goto err4;

	err = nft_byteorder_module_init();
	if (err < 0)
		goto err5;

	err = nft_payload_module_init();
	if (err < 0)
		goto err6;

	return 0;

err6:
	nft_byteorder_module_exit();
err5:
	nft_bitwise_module_exit();
err4:
	nft_lookup_module_exit();
err3:
	nft_cmp_module_exit();
err2:
	nft_immediate_module_exit();
err1:
	return err;
}

void nf_tables_core_module_exit(void)
{
	nft_payload_module_exit();
	nft_byteorder_module_exit();
	nft_bitwise_module_exit();
	nft_lookup_module_exit();
	nft_cmp_module_exit();
	nft_immediate_module_exit();
}