ebt_redirect.c 1.88 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
/*
 *  ebt_redirect
 *
 *	Authors:
 *	Bart De Schuymer <bdschuym@pandora.be>
 *
 *  April, 2002
 *
 */

11
#include <linux/netfilter.h>
Linus Torvalds's avatar
Linus Torvalds committed
12
13
14
15
16
17
#include <linux/netfilter_bridge/ebtables.h>
#include <linux/netfilter_bridge/ebt_redirect.h>
#include <linux/module.h>
#include <net/sock.h>
#include "../br_private.h"

18
static int ebt_target_redirect(struct sk_buff *skb, unsigned int hooknr,
Linus Torvalds's avatar
Linus Torvalds committed
19
20
21
   const struct net_device *in, const struct net_device *out,
   const void *data, unsigned int datalen)
{
22
	const struct ebt_redirect_info *info = data;
Linus Torvalds's avatar
Linus Torvalds committed
23

24
	if (!skb_make_writable(skb, 0))
25
		return NF_DROP;
Linus Torvalds's avatar
Linus Torvalds committed
26
27

	if (hooknr != NF_BR_BROUTING)
28
		memcpy(eth_hdr(skb)->h_dest,
Linus Torvalds's avatar
Linus Torvalds committed
29
30
		       in->br_port->br->dev->dev_addr, ETH_ALEN);
	else
31
32
		memcpy(eth_hdr(skb)->h_dest, in->dev_addr, ETH_ALEN);
	skb->pkt_type = PACKET_HOST;
Linus Torvalds's avatar
Linus Torvalds committed
33
34
35
36
37
38
	return info->target;
}

static int ebt_target_redirect_check(const char *tablename, unsigned int hookmask,
   const struct ebt_entry *e, void *data, unsigned int datalen)
{
39
	const struct ebt_redirect_info *info = data;
Linus Torvalds's avatar
Linus Torvalds committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53

	if (datalen != EBT_ALIGN(sizeof(struct ebt_redirect_info)))
		return -EINVAL;
	if (BASE_CHAIN && info->target == EBT_RETURN)
		return -EINVAL;
	CLEAR_BASE_CHAIN_BIT;
	if ( (strcmp(tablename, "nat") || hookmask & ~(1 << NF_BR_PRE_ROUTING)) &&
	     (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
		return -EINVAL;
	if (INVALID_TARGET)
		return -EINVAL;
	return 0;
}

54
static struct ebt_target redirect_target __read_mostly = {
Linus Torvalds's avatar
Linus Torvalds committed
55
56
57
58
59
60
	.name		= EBT_REDIRECT_TARGET,
	.target		= ebt_target_redirect,
	.check		= ebt_target_redirect_check,
	.me		= THIS_MODULE,
};

61
static int __init ebt_redirect_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
62
63
64
65
{
	return ebt_register_target(&redirect_target);
}

66
static void __exit ebt_redirect_fini(void)
Linus Torvalds's avatar
Linus Torvalds committed
67
68
69
70
{
	ebt_unregister_target(&redirect_target);
}

71
72
module_init(ebt_redirect_init);
module_exit(ebt_redirect_fini);
73
MODULE_DESCRIPTION("Ebtables: Packet redirection to localhost");
Linus Torvalds's avatar
Linus Torvalds committed
74
MODULE_LICENSE("GPL");