Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / net / ipv4 / netfilter / arpt_mangle.c
bloba5e52a9f0a124e3dd84c164cb5159f120b33071c
1 /* module that allows mangling of the arp payload */
2 #include <linux/module.h>
3 #include <linux/netfilter.h>
4 #include <linux/netfilter_arp/arpt_mangle.h>
5 #include <net/sock.h>
7 MODULE_LICENSE("GPL");
8 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
9 MODULE_DESCRIPTION("arptables arp payload mangle target");
11 static unsigned int
12 target(struct sk_buff *skb, const struct xt_action_param *par)
14 const struct arpt_mangle *mangle = par->targinfo;
15 const struct arphdr *arp;
16 unsigned char *arpptr;
17 int pln, hln;
19 if (!skb_make_writable(skb, skb->len))
20 return NF_DROP;
22 arp = arp_hdr(skb);
23 arpptr = skb_network_header(skb) + sizeof(*arp);
24 pln = arp->ar_pln;
25 hln = arp->ar_hln;
26 /* We assume that pln and hln were checked in the match */
27 if (mangle->flags & ARPT_MANGLE_SDEV) {
28 if (ARPT_DEV_ADDR_LEN_MAX < hln ||
29 (arpptr + hln > skb_tail_pointer(skb)))
30 return NF_DROP;
31 memcpy(arpptr, mangle->src_devaddr, hln);
33 arpptr += hln;
34 if (mangle->flags & ARPT_MANGLE_SIP) {
35 if (ARPT_MANGLE_ADDR_LEN_MAX < pln ||
36 (arpptr + pln > skb_tail_pointer(skb)))
37 return NF_DROP;
38 memcpy(arpptr, &mangle->u_s.src_ip, pln);
40 arpptr += pln;
41 if (mangle->flags & ARPT_MANGLE_TDEV) {
42 if (ARPT_DEV_ADDR_LEN_MAX < hln ||
43 (arpptr + hln > skb_tail_pointer(skb)))
44 return NF_DROP;
45 memcpy(arpptr, mangle->tgt_devaddr, hln);
47 arpptr += hln;
48 if (mangle->flags & ARPT_MANGLE_TIP) {
49 if (ARPT_MANGLE_ADDR_LEN_MAX < pln ||
50 (arpptr + pln > skb_tail_pointer(skb)))
51 return NF_DROP;
52 memcpy(arpptr, &mangle->u_t.tgt_ip, pln);
54 return mangle->target;
57 static int checkentry(const struct xt_tgchk_param *par)
59 const struct arpt_mangle *mangle = par->targinfo;
61 if (mangle->flags & ~ARPT_MANGLE_MASK ||
62 !(mangle->flags & ARPT_MANGLE_MASK))
63 return -EINVAL;
65 if (mangle->target != NF_DROP && mangle->target != NF_ACCEPT &&
66 mangle->target != XT_CONTINUE)
67 return -EINVAL;
68 return 0;
71 static struct xt_target arpt_mangle_reg __read_mostly = {
72 .name = "mangle",
73 .family = NFPROTO_ARP,
74 .target = target,
75 .targetsize = sizeof(struct arpt_mangle),
76 .checkentry = checkentry,
77 .me = THIS_MODULE,
80 static int __init arpt_mangle_init(void)
82 return xt_register_target(&arpt_mangle_reg);
85 static void __exit arpt_mangle_fini(void)
87 xt_unregister_target(&arpt_mangle_reg);
90 module_init(arpt_mangle_init);
91 module_exit(arpt_mangle_fini);