blob: b8afe850cf1ec1ec77dc4773e173ea96608870dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebt_redirect
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 *
7 * April, 2002
8 *
9 */
10
Herbert Xu2ca7b0a2007-10-14 00:39:55 -070011#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/netfilter_bridge/ebtables.h>
13#include <linux/netfilter_bridge/ebt_redirect.h>
14#include <linux/module.h>
15#include <net/sock.h>
16#include "../br_private.h"
17
Herbert Xu3db05fe2007-10-15 00:53:15 -070018static int ebt_target_redirect(struct sk_buff *skb, unsigned int hooknr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 const struct net_device *in, const struct net_device *out,
20 const void *data, unsigned int datalen)
21{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080022 const struct ebt_redirect_info *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Joonwoo Parkeb1197b2008-02-19 17:18:47 -080024 if (!skb_make_writable(skb, 0))
Joonwoo Park1b04ab42008-02-23 20:22:27 -080025 return EBT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 if (hooknr != NF_BR_BROUTING)
Herbert Xu3db05fe2007-10-15 00:53:15 -070028 memcpy(eth_hdr(skb)->h_dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 in->br_port->br->dev->dev_addr, ETH_ALEN);
30 else
Herbert Xu3db05fe2007-10-15 00:53:15 -070031 memcpy(eth_hdr(skb)->h_dest, in->dev_addr, ETH_ALEN);
32 skb->pkt_type = PACKET_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return info->target;
34}
35
36static int ebt_target_redirect_check(const char *tablename, unsigned int hookmask,
37 const struct ebt_entry *e, void *data, unsigned int datalen)
38{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080039 const struct ebt_redirect_info *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 if (datalen != EBT_ALIGN(sizeof(struct ebt_redirect_info)))
42 return -EINVAL;
43 if (BASE_CHAIN && info->target == EBT_RETURN)
44 return -EINVAL;
45 CLEAR_BASE_CHAIN_BIT;
46 if ( (strcmp(tablename, "nat") || hookmask & ~(1 << NF_BR_PRE_ROUTING)) &&
47 (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
48 return -EINVAL;
49 if (INVALID_TARGET)
50 return -EINVAL;
51 return 0;
52}
53
Jan Engelhardt30083c92008-01-31 04:00:59 -080054static struct ebt_target redirect_target __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .name = EBT_REDIRECT_TARGET,
56 .target = ebt_target_redirect,
57 .check = ebt_target_redirect_check,
58 .me = THIS_MODULE,
59};
60
Andrew Morton65b4b4e2006-03-28 16:37:06 -080061static int __init ebt_redirect_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 return ebt_register_target(&redirect_target);
64}
65
Andrew Morton65b4b4e2006-03-28 16:37:06 -080066static void __exit ebt_redirect_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 ebt_unregister_target(&redirect_target);
69}
70
Andrew Morton65b4b4e2006-03-28 16:37:06 -080071module_init(ebt_redirect_init);
72module_exit(ebt_redirect_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -080073MODULE_DESCRIPTION("Ebtables: Packet redirection to localhost");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_LICENSE("GPL");