summaryrefslogtreecommitdiffstats
path: root/extensions/libebt_arp.c
blob: 50ce32be5101486a7c0abff45d6434ee950c1393 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* ebt_arp
 *
 * Authors:
 * Bart De Schuymer <bdschuym@pandora.be>
 * Tim Gardner <timg@tpi.com>
 *
 * April, 2002
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <xtables.h>
#include <netinet/ether.h>

#include <xtables.h>
#include <net/if_arp.h>
#include <linux/netfilter_bridge/ebt_arp.h>
#include "iptables/nft.h"
#include "iptables/nft-bridge.h"

/* values must correspond with EBT_ARP_* bit positions */
enum {
	O_OPCODE = 0,
	O_HTYPE,
	O_PTYPE,
	O_SRC_IP,
	O_DST_IP,
	O_SRC_MAC,
	O_DST_MAC,
	O_GRAT,
};

static const struct xt_option_entry brarp_opts[] = {
#define ENTRY(n, i, t) { .name = n, .id = i, .type = t, .flags = XTOPT_INVERT }
	ENTRY("arp-opcode",     O_OPCODE,  XTTYPE_STRING),
	ENTRY("arp-op",         O_OPCODE,  XTTYPE_STRING),
	ENTRY("arp-htype",      O_HTYPE,   XTTYPE_STRING),
	ENTRY("arp-ptype",      O_PTYPE,   XTTYPE_STRING),
	ENTRY("arp-ip-src",     O_SRC_IP,  XTTYPE_HOSTMASK),
	ENTRY("arp-ip-dst",     O_DST_IP,  XTTYPE_HOSTMASK),
	ENTRY("arp-mac-src",    O_SRC_MAC, XTTYPE_ETHERMACMASK),
	ENTRY("arp-mac-dst",    O_DST_MAC, XTTYPE_ETHERMACMASK),
	ENTRY("arp-gratuitous", O_GRAT,    XTTYPE_NONE),
#undef ENTRY
	XTOPT_TABLEEND
};

/* a few names */
static char *opcodes[] =
{
	"Request",
	"Reply",
	"Request_Reverse",
	"Reply_Reverse",
	"DRARP_Request",
	"DRARP_Reply",
	"DRARP_Error",
	"InARP_Request",
	"ARP_NAK",
};

static void brarp_print_help(void)
{
	int i;

	printf(
"arp options:\n"
"[!] --arp-opcode  opcode        : ARP opcode (integer or string)\n"
"[!] --arp-htype   type          : ARP hardware type (integer or string)\n"
"[!] --arp-ptype   type          : ARP protocol type (hexadecimal or string)\n"
"[!] --arp-ip-src  address[/mask]: ARP IP source specification\n"
"[!] --arp-ip-dst  address[/mask]: ARP IP target specification\n"
"[!] --arp-mac-src address[/mask]: ARP MAC source specification\n"
"[!] --arp-mac-dst address[/mask]: ARP MAC target specification\n"
"[!] --arp-gratuitous            : ARP gratuitous packet\n"
" opcode strings: \n");
	for (i = 0; i < ARRAY_SIZE(opcodes); i++)
		printf(" %d = %s\n", i + 1, opcodes[i]);
	printf(
" hardware type string: 1 = Ethernet\n"
" protocol type string: see "XT_PATH_ETHERTYPES"\n");
}

static void brarp_parse(struct xt_option_call *cb)
{
	struct ebt_arp_info *arpinfo = cb->data;
	struct xt_ethertypeent *ent;
	long int i;
	char *end;


	xtables_option_parse(cb);

	arpinfo->bitmask |= 1 << cb->entry->id;
	if (cb->invert)
		arpinfo->invflags |= 1 << cb->entry->id;

	switch (cb->entry->id) {
	case O_OPCODE:
		i = strtol(cb->arg, &end, 10);
		if (i < 0 || i >= (0x1 << 16) || *end !='\0') {
			for (i = 0; i < ARRAY_SIZE(opcodes); i++)
				if (!strcasecmp(opcodes[i], cb->arg))
					break;
			if (i == ARRAY_SIZE(opcodes))
				xtables_error(PARAMETER_PROBLEM,
					      "Problem with specified ARP opcode");
			i++;
		}
		arpinfo->opcode = htons(i);
		break;
	case O_HTYPE:
		i = strtol(cb->arg, &end, 10);
		if (i < 0 || i >= (0x1 << 16) || *end !='\0') {
			if (!strcasecmp("Ethernet", cb->arg))
				i = 1;
			else
				xtables_error(PARAMETER_PROBLEM,
					      "Problem with specified ARP hardware type");
		}
		arpinfo->htype = htons(i);
		break;
	case O_PTYPE:
		i = strtol(cb->arg, &end, 16);
		if (i >= 0 && i < (0x1 << 16) && *end == '\0') {
			arpinfo->ptype = htons(i);
			break;
		}
		ent = xtables_getethertypebyname(cb->arg);
		if (!ent)
			xtables_error(PARAMETER_PROBLEM,
				      "Problem with specified ARP protocol type");
		arpinfo->ptype = htons(ent->e_ethertype);
		break;
	case O_SRC_IP:
		arpinfo->saddr = cb->val.haddr.ip & cb->val.hmask.ip;
		arpinfo->smsk = cb->val.hmask.ip;
		break;
	case O_DST_IP:
		arpinfo->daddr = cb->val.haddr.ip & cb->val.hmask.ip;
		arpinfo->dmsk = cb->val.hmask.ip;
		break;
	case O_SRC_MAC:
		memcpy(arpinfo->smaddr, cb->val.ethermac, ETH_ALEN);
		memcpy(arpinfo->smmsk, cb->val.ethermacmask, ETH_ALEN);
		break;
	case O_DST_MAC:
		memcpy(arpinfo->dmaddr, cb->val.ethermac, ETH_ALEN);
		memcpy(arpinfo->dmmsk, cb->val.ethermacmask, ETH_ALEN);
		break;
	}
}

static void brarp_print(const void *ip, const struct xt_entry_match *match, int numeric)
{
	const struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)match->data;

	if (arpinfo->bitmask & EBT_ARP_OPCODE) {
		int opcode = ntohs(arpinfo->opcode);

		if (arpinfo->invflags & EBT_ARP_OPCODE)
			printf("! ");
		printf("--arp-op ");
		if (opcode > 0 && opcode <= ARRAY_SIZE(opcodes))
			printf("%s ", opcodes[opcode - 1]);
		else
			printf("%d ", opcode);
	}
	if (arpinfo->bitmask & EBT_ARP_HTYPE) {
		if (arpinfo->invflags & EBT_ARP_HTYPE)
			printf("! ");
		printf("--arp-htype %d ", ntohs(arpinfo->htype));
	}
	if (arpinfo->bitmask & EBT_ARP_PTYPE) {
		if (arpinfo->invflags & EBT_ARP_PTYPE)
			printf("! ");
		printf("--arp-ptype 0x%x ", ntohs(arpinfo->ptype));
	}
	if (arpinfo->bitmask & EBT_ARP_SRC_IP) {
		if (arpinfo->invflags & EBT_ARP_SRC_IP)
			printf("! ");
		printf("--arp-ip-src %s%s ",
		       xtables_ipaddr_to_numeric((void *)&arpinfo->saddr),
		       xtables_ipmask_to_numeric((void *)&arpinfo->smsk));
	}
	if (arpinfo->bitmask & EBT_ARP_DST_IP) {
		if (arpinfo->invflags & EBT_ARP_DST_IP)
			printf("! ");
		printf("--arp-ip-dst %s%s ",
		       xtables_ipaddr_to_numeric((void *)&arpinfo->daddr),
		       xtables_ipmask_to_numeric((void *)&arpinfo->dmsk));
	}
	if (arpinfo->bitmask & EBT_ARP_SRC_MAC) {
		if (arpinfo->invflags & EBT_ARP_SRC_MAC)
			printf("! ");
		printf("--arp-mac-src ");
		xtables_print_mac_and_mask(arpinfo->smaddr, arpinfo->smmsk);
		printf(" ");
	}
	if (arpinfo->bitmask & EBT_ARP_DST_MAC) {
		if (arpinfo->invflags & EBT_ARP_DST_MAC)
			printf("! ");
		printf("--arp-mac-dst ");
		xtables_print_mac_and_mask(arpinfo->dmaddr, arpinfo->dmmsk);
		printf(" ");
	}
	if (arpinfo->bitmask & EBT_ARP_GRAT) {
		if (arpinfo->invflags & EBT_ARP_GRAT)
			printf("! ");
		printf("--arp-gratuitous ");
	}
}

static struct xtables_match brarp_match = {
	.name		= "arp",
	.version	= XTABLES_VERSION,
	.family		= NFPROTO_BRIDGE,
	.size		= XT_ALIGN(sizeof(struct ebt_arp_info)),
	.userspacesize	= XT_ALIGN(sizeof(struct ebt_arp_info)),
	.help		= brarp_print_help,
	.x6_parse	= brarp_parse,
	.print		= brarp_print,
	.x6_options	= brarp_opts,
};

void _init(void)
{
	xtables_register_match(&brarp_match);
}