Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ebt_snat |
| 3 | * |
| 4 | * Authors: |
| 5 | * Bart De Schuymer <bdschuym@pandora.be> |
| 6 | * |
| 7 | * June, 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_nat.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <net/sock.h> |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 16 | #include <linux/if_arp.h> |
| 17 | #include <net/arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 19 | static int ebt_target_snat(struct sk_buff *skb, unsigned int hooknr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | const struct net_device *in, const struct net_device *out, |
| 21 | const void *data, unsigned int datalen) |
| 22 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame^] | 23 | const struct ebt_nat_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 25 | if (skb_make_writable(skb, 0)) |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 26 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 28 | memcpy(eth_hdr(skb)->h_source, info->mac, ETH_ALEN); |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 29 | if (!(info->target & NAT_ARP_BIT) && |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 30 | eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame^] | 31 | const struct arphdr *ap; |
| 32 | struct arphdr _ah; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 33 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 34 | ap = skb_header_pointer(skb, 0, sizeof(_ah), &_ah); |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 35 | if (ap == NULL) |
| 36 | return EBT_DROP; |
| 37 | if (ap->ar_hln != ETH_ALEN) |
| 38 | goto out; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 39 | if (skb_store_bits(skb, sizeof(_ah), info->mac,ETH_ALEN)) |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 40 | return EBT_DROP; |
| 41 | } |
| 42 | out: |
| 43 | return info->target | ~EBT_VERDICT_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static int ebt_target_snat_check(const char *tablename, unsigned int hookmask, |
| 47 | const struct ebt_entry *e, void *data, unsigned int datalen) |
| 48 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame^] | 49 | const struct ebt_nat_info *info = data; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 50 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info))) |
| 53 | return -EINVAL; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 54 | tmp = info->target | ~EBT_VERDICT_BITS; |
| 55 | if (BASE_CHAIN && tmp == EBT_RETURN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return -EINVAL; |
| 57 | CLEAR_BASE_CHAIN_BIT; |
| 58 | if (strcmp(tablename, "nat")) |
| 59 | return -EINVAL; |
| 60 | if (hookmask & ~(1 << NF_BR_POST_ROUTING)) |
| 61 | return -EINVAL; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 62 | |
| 63 | if (tmp < -NUM_STANDARD_TARGETS || tmp >= 0) |
| 64 | return -EINVAL; |
| 65 | tmp = info->target | EBT_VERDICT_BITS; |
| 66 | if ((tmp & ~NAT_ARP_BIT) != ~NAT_ARP_BIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return -EINVAL; |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static struct ebt_target snat = |
| 72 | { |
| 73 | .name = EBT_SNAT_TARGET, |
| 74 | .target = ebt_target_snat, |
| 75 | .check = ebt_target_snat_check, |
| 76 | .me = THIS_MODULE, |
| 77 | }; |
| 78 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 79 | static int __init ebt_snat_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | return ebt_register_target(&snat); |
| 82 | } |
| 83 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 84 | static void __exit ebt_snat_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | ebt_unregister_target(&snat); |
| 87 | } |
| 88 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 89 | module_init(ebt_snat_init); |
| 90 | module_exit(ebt_snat_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | MODULE_LICENSE("GPL"); |