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 | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 19 | static unsigned int |
| 20 | ebt_snat_tg(struct sk_buff *skb, const struct net_device *in, |
| 21 | const struct net_device *out, unsigned int hook_nr, |
| 22 | const struct xt_target *target, const void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 24 | const struct ebt_nat_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Joonwoo Park | eb1197b | 2008-02-19 17:18:47 -0800 | [diff] [blame] | 26 | if (!skb_make_writable(skb, 0)) |
Joonwoo Park | 1b04ab4 | 2008-02-23 20:22:27 -0800 | [diff] [blame] | 27 | return EBT_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 29 | memcpy(eth_hdr(skb)->h_source, info->mac, ETH_ALEN); |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 30 | if (!(info->target & NAT_ARP_BIT) && |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 31 | eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 32 | const struct arphdr *ap; |
| 33 | struct arphdr _ah; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 34 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 35 | ap = skb_header_pointer(skb, 0, sizeof(_ah), &_ah); |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 36 | if (ap == NULL) |
| 37 | return EBT_DROP; |
| 38 | if (ap->ar_hln != ETH_ALEN) |
| 39 | goto out; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 40 | if (skb_store_bits(skb, sizeof(_ah), info->mac,ETH_ALEN)) |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 41 | return EBT_DROP; |
| 42 | } |
| 43 | out: |
| 44 | return info->target | ~EBT_VERDICT_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 47 | static bool |
| 48 | ebt_snat_tg_check(const char *tablename, const void *e, |
| 49 | const struct xt_target *target, void *data, |
| 50 | unsigned int hookmask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 52 | const struct ebt_nat_info *info = data; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 53 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 55 | tmp = info->target | ~EBT_VERDICT_BITS; |
| 56 | if (BASE_CHAIN && tmp == EBT_RETURN) |
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 | CLEAR_BASE_CHAIN_BIT; |
| 59 | if (strcmp(tablename, "nat")) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 60 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | if (hookmask & ~(1 << NF_BR_POST_ROUTING)) |
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 | |
| 64 | if (tmp < -NUM_STANDARD_TARGETS || tmp >= 0) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 65 | return false; |
Bart De Schuymer | d12cdc3 | 2006-11-29 02:35:40 +0100 | [diff] [blame] | 66 | tmp = info->target | EBT_VERDICT_BITS; |
| 67 | if ((tmp & ~NAT_ARP_BIT) != ~NAT_ARP_BIT) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 68 | return false; |
| 69 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame^] | 72 | static struct xt_target ebt_snat_tg_reg __read_mostly = { |
| 73 | .name = "snat", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 74 | .revision = 0, |
| 75 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 76 | .target = ebt_snat_tg, |
| 77 | .checkentry = ebt_snat_tg_check, |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 78 | .targetsize = XT_ALIGN(sizeof(struct ebt_nat_info)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | .me = THIS_MODULE, |
| 80 | }; |
| 81 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 82 | static int __init ebt_snat_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame^] | 84 | return xt_register_target(&ebt_snat_tg_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 87 | static void __exit ebt_snat_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame^] | 89 | xt_unregister_target(&ebt_snat_tg_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 92 | module_init(ebt_snat_init); |
| 93 | module_exit(ebt_snat_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 94 | MODULE_DESCRIPTION("Ebtables: Source MAC address translation"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | MODULE_LICENSE("GPL"); |