fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.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_bridge/ebtables.h>
12 #include <linux/netfilter_bridge/ebt_nat.h>
13 #include <linux/module.h>
14 #include <net/sock.h>
15 #include <linux/if_arp.h>
16 #include <net/arp.h>
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);
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;
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;
56         int tmp;
57
58         if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
59                 return -EINVAL;
60         tmp = info->target | ~EBT_VERDICT_BITS;
61         if (BASE_CHAIN && tmp == EBT_RETURN)
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;
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)
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
85 static int __init ebt_snat_init(void)
86 {
87         return ebt_register_target(&snat);
88 }
89
90 static void __exit ebt_snat_fini(void)
91 {
92         ebt_unregister_target(&snat);
93 }
94
95 module_init(ebt_snat_init);
96 module_exit(ebt_snat_fini);
97 MODULE_LICENSE("GPL");