summaryrefslogtreecommitdiffstats
path: root/kernel/linux2.5/net/bridge/netfilter/ebt_arp.c
blob: 0d1650b28a06cfe61ffbb937857437e16e3d4591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 *  ebt_arp
 *
 *	Authors:
 *	Bart De Schuymer <bdschuym@pandora.be>
 *	Tim Gardner <timg@tpi.com>
 *
 *  April, 2002
 *
 */

#include <linux/netfilter_bridge/ebtables.h>
#include <linux/netfilter_bridge/ebt_arp.h>
#include <linux/if_arp.h>
#include <linux/module.h>
#include <linux/if_ether.h>

static int ebt_filter_arp(const struct sk_buff *skb, const struct net_device *in,
   const struct net_device *out, const void *data, unsigned int datalen)
{
	struct ebt_arp_info *info = (struct ebt_arp_info *)data;

	if (info->bitmask & EBT_ARP_OPCODE && FWINV(info->opcode !=
	   ((*skb).nh.arph)->ar_op, EBT_ARP_OPCODE))
		return EBT_NOMATCH;
	if (info->bitmask & EBT_ARP_HTYPE && FWINV(info->htype !=
	   ((*skb).nh.arph)->ar_hrd, EBT_ARP_HTYPE))
		return EBT_NOMATCH;
	if (info->bitmask & EBT_ARP_PTYPE && FWINV(info->ptype !=
	   ((*skb).nh.arph)->ar_pro, EBT_ARP_PTYPE))
		return EBT_NOMATCH;

	if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP))
	{
		uint32_t arp_len = sizeof(struct arphdr) +
		   (2 * (((*skb).nh.arph)->ar_hln)) +
		   (2 * (((*skb).nh.arph)->ar_pln));
		uint32_t dst;
		uint32_t src;

		/* Make sure the packet is long enough */
		if ((((*skb).nh.raw) + arp_len) > (*skb).tail)
			return EBT_NOMATCH;
		/* IPv4 addresses are always 4 bytes */
		if (((*skb).nh.arph)->ar_pln != sizeof(uint32_t))
			return EBT_NOMATCH;

		if (info->bitmask & EBT_ARP_SRC_IP) {
			memcpy(&src, ((*skb).nh.raw) + sizeof(struct arphdr) +
			   ((*skb).nh.arph)->ar_hln, sizeof(uint32_t));
			if (FWINV(info->saddr != (src & info->smsk),
			   EBT_ARP_SRC_IP))
				return EBT_NOMATCH;
		}

		if (info->bitmask & EBT_ARP_DST_IP) {
			memcpy(&dst, ((*skb).nh.raw)+sizeof(struct arphdr) +
			   (2*(((*skb).nh.arph)->ar_hln)) +
			   (((*skb).nh.arph)->ar_pln), sizeof(uint32_t));
			if (FWINV(info->daddr != (dst & info->dmsk),
			   EBT_ARP_DST_IP))
				return EBT_NOMATCH;
		}
	}

	if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC))
	{
		uint32_t arp_len = sizeof(struct arphdr) +
		   (2 * (((*skb).nh.arph)->ar_hln)) +
		   (2 * (((*skb).nh.arph)->ar_pln));
		unsigned char dst[ETH_ALEN];
		unsigned char src[ETH_ALEN];

		/* Make sure the packet is long enough */
		if ((((*skb).nh.raw) + arp_len) > (*skb).tail)
			return EBT_NOMATCH;
		/* MAC addresses are 6 bytes */
		if (((*skb).nh.arph)->ar_hln != ETH_ALEN)
			return EBT_NOMATCH;
		if (info->bitmask & EBT_ARP_SRC_MAC) {
			uint8_t verdict, i;
			memcpy(&src, ((*skb).nh.raw) +
					sizeof(struct arphdr),
					ETH_ALEN);
			verdict = 0;
			for (i = 0; i < 6; i++)
				verdict |= (src[i] ^ info->smaddr[i]) &
				       info->smmsk[i];
			if (FWINV(verdict != 0, EBT_ARP_SRC_MAC))
				return EBT_NOMATCH;
		}

		if (info->bitmask & EBT_ARP_DST_MAC) {
			uint8_t verdict, i;
			memcpy(&dst, ((*skb).nh.raw) +
					sizeof(struct arphdr) +
			   		(((*skb).nh.arph)->ar_hln) +
			   		(((*skb).nh.arph)->ar_pln),
					ETH_ALEN);
			verdict = 0;
			for (i = 0; i < 6; i++)
				verdict |= (dst[i] ^ info->dmaddr[i]) &
					info->dmmsk[i];
			if (FWINV(verdict != 0, EBT_ARP_DST_MAC))
				return EBT_NOMATCH;
		}
	}

	return EBT_MATCH;
}

static int ebt_arp_check(const char *tablename, unsigned int hookmask,
   const struct ebt_entry *e, void *data, unsigned int datalen)
{
	struct ebt_arp_info *info = (struct ebt_arp_info *)data;

	if (datalen != sizeof(struct ebt_arp_info))
		return -EINVAL;
	if ((e->ethproto != __constant_htons(ETH_P_ARP) &&
	   e->ethproto != __constant_htons(ETH_P_RARP)) ||
	   e->invflags & EBT_IPROTO)
		return -EINVAL;
	if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
		return -EINVAL;
	return 0;
}

static struct ebt_match filter_arp =
{
	.name		= EBT_ARP_MATCH,
	.match		= ebt_filter_arp,
	.check		= ebt_arp_check,
	.me		= THIS_MODULE,
};

static int __init init(void)
{
	return ebt_register_match(&filter_arp);
}

static void __exit fini(void)
{
	ebt_unregister_match(&filter_arp);
}

module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");