Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ebt_redirect |
| 3 | * |
| 4 | * Authors: |
| 5 | * Bart De Schuymer <bdschuym@pandora.be> |
| 6 | * |
| 7 | * April, 2002 |
| 8 | * |
| 9 | */ |
| 10 | |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 11 | #include <linux/netfilter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #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 Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 18 | static int ebt_target_redirect(struct sk_buff *skb, unsigned int hooknr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | const struct net_device *in, const struct net_device *out, |
| 20 | const void *data, unsigned int datalen) |
| 21 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 22 | const struct ebt_redirect_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Joonwoo Park | eb1197b | 2008-02-19 17:18:47 -0800 | [diff] [blame] | 24 | if (!skb_make_writable(skb, 0)) |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 25 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | if (hooknr != NF_BR_BROUTING) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 28 | memcpy(eth_hdr(skb)->h_dest, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | in->br_port->br->dev->dev_addr, ETH_ALEN); |
| 30 | else |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 31 | memcpy(eth_hdr(skb)->h_dest, in->dev_addr, ETH_ALEN); |
| 32 | skb->pkt_type = PACKET_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | return info->target; |
| 34 | } |
| 35 | |
| 36 | static int ebt_target_redirect_check(const char *tablename, unsigned int hookmask, |
| 37 | const struct ebt_entry *e, void *data, unsigned int datalen) |
| 38 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 39 | const struct ebt_redirect_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 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 Engelhardt | 30083c9 | 2008-01-31 04:00:59 -0800 | [diff] [blame] | 54 | static struct ebt_target redirect_target __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | .name = EBT_REDIRECT_TARGET, |
| 56 | .target = ebt_target_redirect, |
| 57 | .check = ebt_target_redirect_check, |
| 58 | .me = THIS_MODULE, |
| 59 | }; |
| 60 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 61 | static int __init ebt_redirect_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | return ebt_register_target(&redirect_target); |
| 64 | } |
| 65 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 66 | static void __exit ebt_redirect_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | ebt_unregister_target(&redirect_target); |
| 69 | } |
| 70 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 71 | module_init(ebt_redirect_init); |
| 72 | module_exit(ebt_redirect_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 73 | MODULE_DESCRIPTION("Ebtables: Packet redirection to localhost"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | MODULE_LICENSE("GPL"); |