]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/bridge/netfilter/ebt_snat.c
0e83de781c0c0a1e5fcf71b1844d6ea61b1c465a
[karo-tx-linux.git] / net / bridge / netfilter / ebt_snat.c
1 /*
2  *  ebt_snat
3  *
4  *      Authors:
5  *      Bart De Schuymer <bdschuym@pandora.be>
6  *
7  *  June, 2002
8  *
9  */
10 #include <linux/module.h>
11 #include <net/sock.h>
12 #include <linux/if_arp.h>
13 #include <net/arp.h>
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>
18
19 static unsigned int ebt_target_snat(struct sk_buff *skb, unsigned int hooknr,
20    const struct net_device *in, const struct net_device *out,
21    const void *data, unsigned int datalen)
22 {
23         const struct ebt_nat_info *info = data;
24
25         if (!skb_make_writable(skb, 0))
26                 return EBT_DROP;
27
28         memcpy(eth_hdr(skb)->h_source, info->mac, ETH_ALEN);
29         if (!(info->target & NAT_ARP_BIT) &&
30             eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
31                 const struct arphdr *ap;
32                 struct arphdr _ah;
33
34                 ap = skb_header_pointer(skb, 0, sizeof(_ah), &_ah);
35                 if (ap == NULL)
36                         return EBT_DROP;
37                 if (ap->ar_hln != ETH_ALEN)
38                         goto out;
39                 if (skb_store_bits(skb, sizeof(_ah), info->mac,ETH_ALEN))
40                         return EBT_DROP;
41         }
42 out:
43         return info->target | ~EBT_VERDICT_BITS;
44 }
45
46 static bool ebt_target_snat_check(const char *tablename, unsigned int hookmask,
47    const struct ebt_entry *e, void *data, unsigned int datalen)
48 {
49         const struct ebt_nat_info *info = data;
50         int tmp;
51
52         tmp = info->target | ~EBT_VERDICT_BITS;
53         if (BASE_CHAIN && tmp == EBT_RETURN)
54                 return false;
55         CLEAR_BASE_CHAIN_BIT;
56         if (strcmp(tablename, "nat"))
57                 return false;
58         if (hookmask & ~(1 << NF_BR_POST_ROUTING))
59                 return false;
60
61         if (tmp < -NUM_STANDARD_TARGETS || tmp >= 0)
62                 return false;
63         tmp = info->target | EBT_VERDICT_BITS;
64         if ((tmp & ~NAT_ARP_BIT) != ~NAT_ARP_BIT)
65                 return false;
66         return true;
67 }
68
69 static struct ebt_target snat __read_mostly = {
70         .name           = EBT_SNAT_TARGET,
71         .revision       = 0,
72         .family         = NFPROTO_BRIDGE,
73         .target         = ebt_target_snat,
74         .check          = ebt_target_snat_check,
75         .targetsize     = XT_ALIGN(sizeof(struct ebt_nat_info)),
76         .me             = THIS_MODULE,
77 };
78
79 static int __init ebt_snat_init(void)
80 {
81         return ebt_register_target(&snat);
82 }
83
84 static void __exit ebt_snat_fini(void)
85 {
86         ebt_unregister_target(&snat);
87 }
88
89 module_init(ebt_snat_init);
90 module_exit(ebt_snat_fini);
91 MODULE_DESCRIPTION("Ebtables: Source MAC address translation");
92 MODULE_LICENSE("GPL");