1/*
2 *  ebt_redirect
3 *
4 *	Authors:
5 *	Bart De Schuymer <bdschuym@pandora.be>
6 *
7 *  April, 2002
8 *
9 */
10#include <linux/module.h>
11#include <net/sock.h>
12#include "../br_private.h"
13#include <linux/netfilter.h>
14#include <linux/netfilter/x_tables.h>
15#include <linux/netfilter_bridge/ebtables.h>
16#include <linux/netfilter_bridge/ebt_redirect.h>
17
18static unsigned int
19ebt_redirect_tg(struct sk_buff *skb, const struct xt_action_param *par)
20{
21	const struct ebt_redirect_info *info = par->targinfo;
22
23	if (!skb_make_writable(skb, 0))
24		return EBT_DROP;
25
26	if (par->hooknum != NF_BR_BROUTING)
27		/* rcu_read_lock()ed by nf_hook_slow */
28		ether_addr_copy(eth_hdr(skb)->h_dest,
29				br_port_get_rcu(par->in)->br->dev->dev_addr);
30	else
31		ether_addr_copy(eth_hdr(skb)->h_dest, par->in->dev_addr);
32	skb->pkt_type = PACKET_HOST;
33	return info->target;
34}
35
36static int ebt_redirect_tg_check(const struct xt_tgchk_param *par)
37{
38	const struct ebt_redirect_info *info = par->targinfo;
39	unsigned int hook_mask;
40
41	if (BASE_CHAIN && info->target == EBT_RETURN)
42		return -EINVAL;
43
44	hook_mask = par->hook_mask & ~(1 << NF_BR_NUMHOOKS);
45	if ((strcmp(par->table, "nat") != 0 ||
46	    hook_mask & ~(1 << NF_BR_PRE_ROUTING)) &&
47	    (strcmp(par->table, "broute") != 0 ||
48	    hook_mask & ~(1 << NF_BR_BROUTING)))
49		return -EINVAL;
50	if (INVALID_TARGET)
51		return -EINVAL;
52	return 0;
53}
54
55static struct xt_target ebt_redirect_tg_reg __read_mostly = {
56	.name		= "redirect",
57	.revision	= 0,
58	.family		= NFPROTO_BRIDGE,
59	.hooks		= (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_PRE_ROUTING) |
60			  (1 << NF_BR_BROUTING),
61	.target		= ebt_redirect_tg,
62	.checkentry	= ebt_redirect_tg_check,
63	.targetsize	= sizeof(struct ebt_redirect_info),
64	.me		= THIS_MODULE,
65};
66
67static int __init ebt_redirect_init(void)
68{
69	return xt_register_target(&ebt_redirect_tg_reg);
70}
71
72static void __exit ebt_redirect_fini(void)
73{
74	xt_unregister_target(&ebt_redirect_tg_reg);
75}
76
77module_init(ebt_redirect_init);
78module_exit(ebt_redirect_fini);
79MODULE_DESCRIPTION("Ebtables: Packet redirection to localhost");
80MODULE_LICENSE("GPL");
81