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