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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <net/sock.h> |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 12 | #include <linux/if_arp.h> |
| 13 | #include <net/arp.h> |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 14 | #include <linux/netfilter.h> |
| 15 | #include <linux/netfilter/x_tables.h> |
| 16 | #include <linux/netfilter_bridge/ebtables.h> |
| 17 | #include <linux/netfilter_bridge/ebt_nat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Jan Engelhardt | 0ac6ab1 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 19 | static unsigned 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 | |
Joonwoo Park | eb1197b | 2008-02-19 17:18:47 -0800 | [diff] [blame] | 25 | if (!skb_make_writable(skb, 0)) |
Joonwoo Park | 1b04ab4 | 2008-02-23 20:22:27 -0800 | [diff] [blame] | 26 | return EBT_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 | |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 46 | static bool ebt_target_snat_check(const char *tablename, unsigned int hookmask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 52 | tmp = info->target | ~EBT_VERDICT_BITS; |
| 53 | if (BASE_CHAIN && tmp == EBT_RETURN) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 54 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | CLEAR_BASE_CHAIN_BIT; |
| 56 | if (strcmp(tablename, "nat")) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 57 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | if (hookmask & ~(1 << NF_BR_POST_ROUTING)) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 59 | return false; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 60 | |
| 61 | if (tmp < -NUM_STANDARD_TARGETS || tmp >= 0) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 62 | return false; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 63 | tmp = info->target | EBT_VERDICT_BITS; |
| 64 | if ((tmp & ~NAT_ARP_BIT) != ~NAT_ARP_BIT) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 65 | return false; |
| 66 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Jan Engelhardt | 30083c9 | 2008-01-31 04:00:59 -0800 | [diff] [blame] | 69 | static struct ebt_target snat __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | .name = EBT_SNAT_TARGET, |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame^] | 71 | .revision = 0, |
| 72 | .family = NFPROTO_BRIDGE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | .target = ebt_target_snat, |
| 74 | .check = ebt_target_snat_check, |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 75 | .targetsize = XT_ALIGN(sizeof(struct ebt_nat_info)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 91 | MODULE_DESCRIPTION("Ebtables: Source MAC address translation"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | MODULE_LICENSE("GPL"); |