ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / bridge / netfilter / ebt_arpreply.c
1 /*
2  *  ebt_arpreply
3  *
4  *      Authors:
5  *      Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
6  *      Bart De Schuymer <bdschuym@pandora.be>
7  *
8  *  August, 2003
9  *
10  */
11
12 #include <linux/netfilter_bridge/ebtables.h>
13 #include <linux/netfilter_bridge/ebt_arpreply.h>
14 #include <linux/if_arp.h>
15 #include <net/arp.h>
16 #include <linux/module.h>
17
18 static int ebt_target_reply(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_arpreply_info *info = (struct ebt_arpreply_info *)data;
23         u32 sip, dip;
24         struct arphdr ah;
25         unsigned char sha[ETH_ALEN];
26         struct sk_buff *skb = *pskb;
27
28         if (skb_copy_bits(skb, 0, &ah, sizeof(ah)))
29                 return EBT_DROP;
30
31         if (ah.ar_op != __constant_htons(ARPOP_REQUEST) || ah.ar_hln != ETH_ALEN
32             || ah.ar_pro != __constant_htons(ETH_P_IP) || ah.ar_pln != 4)
33                 return EBT_CONTINUE;
34
35         if (skb_copy_bits(skb, sizeof(ah), &sha, ETH_ALEN))
36                 return EBT_DROP;
37
38         if (skb_copy_bits(skb, sizeof(ah) + ETH_ALEN, &sip, sizeof(sip)))
39                 return EBT_DROP;
40
41         if (skb_copy_bits(skb, sizeof(ah) + 2 * ETH_ALEN + sizeof(sip),
42             &dip, sizeof(dip)))
43                 return EBT_DROP;
44
45         arp_send(ARPOP_REPLY, ETH_P_ARP, sip, (struct net_device *)in,
46                  dip, sha, info->mac, sha);
47
48         return info->target;
49 }
50
51 static int ebt_target_reply_check(const char *tablename, unsigned int hookmask,
52    const struct ebt_entry *e, void *data, unsigned int datalen)
53 {
54         struct ebt_arpreply_info *info = (struct ebt_arpreply_info *)data;
55
56         if (datalen != EBT_ALIGN(sizeof(struct ebt_arpreply_info)))
57                 return -EINVAL;
58         if (BASE_CHAIN && info->target == EBT_RETURN)
59                 return -EINVAL;
60         if (e->ethproto != __constant_htons(ETH_P_ARP) ||
61             e->invflags & EBT_IPROTO)
62                 return -EINVAL;
63         CLEAR_BASE_CHAIN_BIT;
64         if (strcmp(tablename, "nat") || hookmask & ~(1 << NF_BR_PRE_ROUTING))
65                 return -EINVAL;
66         return 0;
67 }
68
69 static struct ebt_target reply_target =
70 {
71         .name           = EBT_ARPREPLY_TARGET,
72         .target         = ebt_target_reply,
73         .check          = ebt_target_reply_check,
74         .me             = THIS_MODULE,
75 };
76
77 static int __init init(void)
78 {
79         return ebt_register_target(&reply_target);
80 }
81
82 static void __exit fini(void)
83 {
84         ebt_unregister_target(&reply_target);
85 }
86
87 module_init(init);
88 module_exit(fini);
89 MODULE_LICENSE("GPL");