blob: d0885635a9f4eaf593c7164f71b9b499eb6f594c [file] [log] [blame]
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <sys/socket.h>
5#include <netinet/in.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00006#include <getopt.h>
7#include "../include/ebtables_u.h"
8#include <linux/netfilter_bridge/ebt_arp.h>
9
10#define ARP_OPCODE '1'
11#define ARP_HTYPE '2'
12#define ARP_PTYPE '3'
13#define ARP_IP_S '4'
14#define ARP_IP_D '5'
15static struct option opts[] =
16{
17 { "arp-opcode" , required_argument, 0, ARP_OPCODE },
18 { "arp-op" , required_argument, 0, ARP_OPCODE },
19 { "arp-htype" , required_argument, 0, ARP_HTYPE },
20 { "arp-ptype" , required_argument, 0, ARP_PTYPE },
21 { "arp-ip-src" , required_argument, 0, ARP_IP_S },
22 { "arp-ip-dst" , required_argument, 0, ARP_IP_D },
23 { 0 }
24};
25
26// a few names
27static char *opcodes[] =
28{
29 "Request",
30 "Reply",
31 "Request Reverse",
32 "Reply Reverse",
33 "DRARP Request",
34 "DRARP Reply",
35 "DRARP Error",
36 "InARP Request",
37 "ARP NAK",
38 ""
39};
40
41static void print_help()
42{
43 int i = 0;
44
45 printf(
46"arp options:\n"
47"--arp-opcode opcode : ARP opcode (integer or string)\n"
48"--arp-htype type : ARP hardware type (integer or string)\n"
49"--arp-ptype type : ARP protocol type (hexadecimal or string)\n"
50"--arp-ip-src [!] address[/mask]: ARP ip source specification\n"
51"--arp-ip-dst [!] address[/mask]: ARP ip target specification\n"
52" opcode strings: \n");
53 while (strcmp(opcodes[i], "")) {
54 printf("%d = %s\n", i + 1, opcodes[i]);
55 i++;
56 }
57 printf(
58" hardware type string: \n 1 = Ethernet\n"
59" protocol type string: \n 0x0800 = IPv4\n");
60}
61
62static void init(struct ebt_entry_match *match)
63{
64 struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)match->data;
65
66 arpinfo->invflags = 0;
67 arpinfo->bitmask = 0;
68}
69
70// defined in ebt_ip.c
71void parse_ip_address(char *address, __u32 *addr, __u32 *msk);
72
73#define OPT_OPCODE 0x01
74#define OPT_HTYPE 0x02
75#define OPT_PTYPE 0x04
76#define OPT_IP_S 0x08
77#define OPT_IP_D 0x10
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +000078static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry,
79 unsigned int *flags, struct ebt_entry_match **match)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000080{
81 struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)(*match)->data;
82 int i;
83 char *end;
84 __u32 *addr;
85 __u32 *mask;
86
87 switch (c) {
88 case ARP_OPCODE:
89 check_option(flags, OPT_OPCODE);
90 if (check_inverse(optarg))
91 arpinfo->invflags |= EBT_ARP_OPCODE;
92
93 if (optind > argc)
94 print_error("Missing arp opcode argument");
95 i = strtol(argv[optind - 1], &end, 10);
96 if (i < 0 || i >= (0x1 << 16) || *end !='\0') {
97 i = 0;
98 while (strcmp(opcodes[i], "")) {
99 if (!strcasecmp(opcodes[i], optarg))
100 break;
101 i++;
102 }
103 if (!strcmp(opcodes[i], ""))
104 print_error("Problem with specified "
105 "arp opcode");
106 }
107 arpinfo->opcode = htons(i);
108 arpinfo->bitmask |= EBT_ARP_OPCODE;
109 break;
110
111 case ARP_HTYPE:
112 check_option(flags, OPT_HTYPE);
113 if (check_inverse(optarg))
114 arpinfo->invflags |= EBT_ARP_HTYPE;
115
116 if (optind > argc)
117 print_error("Missing arp hardware type argument");
118 i = strtol(argv[optind - 1], &end, 10);
119 if (i < 0 || i >= (0x1 << 16) || *end !='\0') {
120 if (!strcasecmp("Ethernet", argv[optind - 1]))
121 i = 1;
122 else
123 print_error("Problem with specified arp "
124 "hardware type");
125 }
126 arpinfo->htype = htons(i);
127 arpinfo->bitmask |= EBT_ARP_HTYPE;
128 break;
129
130 case ARP_PTYPE:
131 check_option(flags, OPT_PTYPE);
132 if (check_inverse(optarg))
133 arpinfo->invflags |= EBT_ARP_PTYPE;
134
135 if (optind > argc)
136 print_error("Missing arp protocol type argument");
137 i = strtol(argv[optind - 1], &end, 16);
138 if (i < 0 || i >= (0x1 << 16) || *end !='\0') {
139 if (!strcasecmp("IPv4", argv[optind - 1]))
140 i = 0x0800;
141 else
142 print_error("Problem with specified arp "
143 "protocol type");
144 }
145 arpinfo->ptype = htons(i);
146 arpinfo->bitmask |= EBT_ARP_PTYPE;
147 break;
148
149 case ARP_IP_S:
150 case ARP_IP_D:
151 if (c == ARP_IP_S) {
152 check_option(flags, OPT_IP_S);
153 addr = &arpinfo->saddr;
154 mask = &arpinfo->smsk;
155 arpinfo->bitmask |= EBT_ARP_SRC_IP;
156 } else {
157 check_option(flags, OPT_IP_D);
158 addr = &arpinfo->daddr;
159 mask = &arpinfo->dmsk;
160 arpinfo->bitmask |= EBT_ARP_DST_IP;
161 }
162 if (check_inverse(optarg)) {
163 if (c == ARP_IP_S)
164 arpinfo->invflags |= EBT_ARP_SRC_IP;
165 else
166 arpinfo->invflags |= EBT_ARP_DST_IP;
167 }
168 if (optind > argc)
169 print_error("Missing ip address argument");
170 parse_ip_address(argv[optind - 1], addr, mask);
171 break;
172 default:
173 return 0;
174 }
175 return 1;
176}
177
178static void final_check(const struct ebt_u_entry *entry,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000179 const struct ebt_entry_match *match, const char *name,
180 unsigned int hook_mask, unsigned int time)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000181{
182 if (entry->bitmask & EBT_NOPROTO || entry->bitmask & EBT_802_3 ||
183 (entry->ethproto != ETH_P_ARP && entry->ethproto != ETH_P_RARP))
184 print_error("For (R)ARP filtering the protocol must be "
185 "specified as ARP or RARP");
186}
187
188// defined in the ebt_ip.c
189char *mask_to_dotted(__u32 mask);
190static void print(const struct ebt_u_entry *entry,
191 const struct ebt_entry_match *match)
192{
193 struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)match->data;
194 int i;
195
196 if (arpinfo->bitmask & EBT_ARP_OPCODE) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000197 printf("--arp-op ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000198 if (arpinfo->invflags & EBT_ARP_OPCODE)
199 printf("! ");
200 printf("%d ", ntohs(arpinfo->opcode));
201 }
202 if (arpinfo->bitmask & EBT_ARP_HTYPE) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000203 printf("--arp-htype ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000204 if (arpinfo->invflags & EBT_ARP_HTYPE)
205 printf("! ");
206 printf("%d ", ntohs(arpinfo->htype));
207 }
208 if (arpinfo->bitmask & EBT_ARP_PTYPE) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000209 printf("--arp-ptype ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000210 if (arpinfo->invflags & EBT_ARP_PTYPE)
211 printf("! ");
212 printf("0x%x ", ntohs(arpinfo->ptype));
213 }
214 if (arpinfo->bitmask & EBT_ARP_SRC_IP) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000215 printf("--arp-ip-src ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000216 if (arpinfo->invflags & EBT_ARP_SRC_IP)
217 printf("! ");
218 for (i = 0; i < 4; i++)
219 printf("%d%s", ((unsigned char *)&arpinfo->saddr)[i],
220 (i == 3) ? "" : ".");
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000221 printf("%s ", mask_to_dotted(arpinfo->smsk));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000222 }
223 if (arpinfo->bitmask & EBT_ARP_DST_IP) {
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000224 printf("--arp-ip-dst ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000225 if (arpinfo->invflags & EBT_ARP_DST_IP)
226 printf("! ");
227 for (i = 0; i < 4; i++)
228 printf("%d%s", ((unsigned char *)&arpinfo->daddr)[i],
229 (i == 3) ? "" : ".");
Bart De Schuymer41e8a192002-06-23 08:03:12 +0000230 printf("%s ", mask_to_dotted(arpinfo->dmsk));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000231 }
232}
233
234static int compare(const struct ebt_entry_match *m1,
235 const struct ebt_entry_match *m2)
236{
237 struct ebt_arp_info *arpinfo1 = (struct ebt_arp_info *)m1->data;
238 struct ebt_arp_info *arpinfo2 = (struct ebt_arp_info *)m2->data;
239
240 if (arpinfo1->bitmask != arpinfo2->bitmask)
241 return 0;
242 if (arpinfo1->invflags != arpinfo2->invflags)
243 return 0;
244 if (arpinfo1->bitmask & EBT_ARP_OPCODE) {
245 if (arpinfo1->opcode != arpinfo2->opcode)
246 return 0;
247 }
248 if (arpinfo1->bitmask & EBT_ARP_HTYPE) {
249 if (arpinfo1->htype != arpinfo2->htype)
250 return 0;
251 }
252 if (arpinfo1->bitmask & EBT_ARP_PTYPE) {
253 if (arpinfo1->ptype != arpinfo2->ptype)
254 return 0;
255 }
256 if (arpinfo1->bitmask & EBT_ARP_SRC_IP) {
257 if (arpinfo1->saddr != arpinfo2->saddr)
258 return 0;
259 if (arpinfo1->smsk != arpinfo2->smsk)
260 return 0;
261 }
262 if (arpinfo1->bitmask & EBT_ARP_DST_IP) {
263 if (arpinfo1->daddr != arpinfo2->daddr)
264 return 0;
265 if (arpinfo1->dmsk != arpinfo2->dmsk)
266 return 0;
267 }
268 return 1;
269}
270
271static struct ebt_u_match arp_match =
272{
273 EBT_ARP_MATCH,
274 sizeof(struct ebt_arp_info),
275 print_help,
276 init,
277 parse,
278 final_check,
279 print,
280 compare,
281 opts,
282};
283
284static void _init(void) __attribute__ ((constructor));
285static void _init(void)
286{
287 register_match(&arp_match);
288}