Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* module that allows mangling of the arp payload */ |
| 2 | #include <linux/module.h> |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 3 | #include <linux/netfilter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/netfilter_arp/arpt_mangle.h> |
| 5 | #include <net/sock.h> |
| 6 | |
| 7 | MODULE_LICENSE("GPL"); |
| 8 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); |
| 9 | MODULE_DESCRIPTION("arptables arp payload mangle target"); |
| 10 | |
| 11 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 12 | target(struct sk_buff *skb, const struct xt_action_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | { |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 14 | const struct arpt_mangle *mangle = par->targinfo; |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 15 | const struct arphdr *arp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | unsigned char *arpptr; |
| 17 | int pln, hln; |
| 18 | |
Joonwoo Park | eb1197b | 2008-02-19 17:18:47 -0800 | [diff] [blame] | 19 | if (!skb_make_writable(skb, skb->len)) |
Herbert Xu | 2ca7b0a | 2007-10-14 00:39:55 -0700 | [diff] [blame] | 20 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 22 | arp = arp_hdr(skb); |
| 23 | arpptr = skb_network_header(skb) + sizeof(*arp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 || |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 29 | (arpptr + hln > skb_tail_pointer(skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | return NF_DROP; |
| 31 | memcpy(arpptr, mangle->src_devaddr, hln); |
| 32 | } |
| 33 | arpptr += hln; |
| 34 | if (mangle->flags & ARPT_MANGLE_SIP) { |
| 35 | if (ARPT_MANGLE_ADDR_LEN_MAX < pln || |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 36 | (arpptr + pln > skb_tail_pointer(skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | return NF_DROP; |
| 38 | memcpy(arpptr, &mangle->u_s.src_ip, pln); |
| 39 | } |
| 40 | arpptr += pln; |
| 41 | if (mangle->flags & ARPT_MANGLE_TDEV) { |
| 42 | if (ARPT_DEV_ADDR_LEN_MAX < hln || |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 43 | (arpptr + hln > skb_tail_pointer(skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | return NF_DROP; |
| 45 | memcpy(arpptr, mangle->tgt_devaddr, hln); |
| 46 | } |
| 47 | arpptr += hln; |
| 48 | if (mangle->flags & ARPT_MANGLE_TIP) { |
| 49 | if (ARPT_MANGLE_ADDR_LEN_MAX < pln || |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 50 | (arpptr + pln > skb_tail_pointer(skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | return NF_DROP; |
| 52 | memcpy(arpptr, &mangle->u_t.tgt_ip, pln); |
| 53 | } |
| 54 | return mangle->target; |
| 55 | } |
| 56 | |
Jan Engelhardt | 135367b | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 57 | static int checkentry(const struct xt_tgchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 59 | const struct arpt_mangle *mangle = par->targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | if (mangle->flags & ~ARPT_MANGLE_MASK || |
| 62 | !(mangle->flags & ARPT_MANGLE_MASK)) |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 63 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | if (mangle->target != NF_DROP && mangle->target != NF_ACCEPT && |
| 66 | mangle->target != ARPT_CONTINUE) |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 67 | return false; |
| 68 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jan Engelhardt | 95eea85 | 2008-04-14 11:15:43 +0200 | [diff] [blame] | 71 | static struct xt_target arpt_mangle_reg __read_mostly = { |
Patrick McHardy | aa83c1a | 2006-03-20 18:01:28 -0800 | [diff] [blame] | 72 | .name = "mangle", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 73 | .family = NFPROTO_ARP, |
Patrick McHardy | aa83c1a | 2006-03-20 18:01:28 -0800 | [diff] [blame] | 74 | .target = target, |
| 75 | .targetsize = sizeof(struct arpt_mangle), |
| 76 | .checkentry = checkentry, |
| 77 | .me = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 80 | static int __init arpt_mangle_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Jan Engelhardt | 3bb0362 | 2008-04-14 11:15:44 +0200 | [diff] [blame] | 82 | return xt_register_target(&arpt_mangle_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 85 | static void __exit arpt_mangle_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Jan Engelhardt | 3bb0362 | 2008-04-14 11:15:44 +0200 | [diff] [blame] | 87 | xt_unregister_target(&arpt_mangle_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 90 | module_init(arpt_mangle_init); |
| 91 | module_exit(arpt_mangle_fini); |