Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[pandora-kernel.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
11 #include <linux/netfilter.h>
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>
16 #include <linux/if_arp.h>
17 #include <net/arp.h>
18
19 static 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         struct ebt_nat_info *info = (struct ebt_nat_info *) data;
24
25         if (skb_make_writable(skb, 0))
26                 return NF_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                 struct arphdr _ah, *ap;
32
33                 ap = skb_header_pointer(skb, 0, sizeof(_ah), &_ah);
34                 if (ap == NULL)
35                         return EBT_DROP;
36                 if (ap->ar_hln != ETH_ALEN)
37                         goto out;
38                 if (skb_store_bits(skb, sizeof(_ah), info->mac,ETH_ALEN))
39                         return EBT_DROP;
40         }
41 out:
42         return info->target | ~EBT_VERDICT_BITS;
43 }
44
45 static int ebt_target_snat_check(const char *tablename, unsigned int hookmask,
46    const struct ebt_entry *e, void *data, unsigned int datalen)
47 {
48         struct ebt_nat_info *info = (struct ebt_nat_info *) data;
49         int tmp;
50
51         if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
52                 return -EINVAL;
53         tmp = info->target | ~EBT_VERDICT_BITS;
54         if (BASE_CHAIN && tmp == EBT_RETURN)
55                 return -EINVAL;
56         CLEAR_BASE_CHAIN_BIT;
57         if (strcmp(tablename, "nat"))
58                 return -EINVAL;
59         if (hookmask & ~(1 << NF_BR_POST_ROUTING))
60                 return -EINVAL;
61
62         if (tmp < -NUM_STANDARD_TARGETS || tmp >= 0)
63                 return -EINVAL;
64         tmp = info->target | EBT_VERDICT_BITS;
65         if ((tmp & ~NAT_ARP_BIT) != ~NAT_ARP_BIT)
66                 return -EINVAL;
67         return 0;
68 }
69
70 static struct ebt_target snat =
71 {
72         .name           = EBT_SNAT_TARGET,
73         .target         = ebt_target_snat,
74         .check          = ebt_target_snat_check,
75         .me             = THIS_MODULE,
76 };
77
78 static int __init ebt_snat_init(void)
79 {
80         return ebt_register_target(&snat);
81 }
82
83 static void __exit ebt_snat_fini(void)
84 {
85         ebt_unregister_target(&snat);
86 }
87
88 module_init(ebt_snat_init);
89 module_exit(ebt_snat_fini);
90 MODULE_LICENSE("GPL");