Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/socket.h> |
| 5 | #include <netinet/in.h> |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 6 | #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' |
| 15 | static 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 |
| 27 | static 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 | |
| 41 | static 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 | |
| 62 | static 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 |
| 71 | void 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 Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 78 | static 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 Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 80 | { |
| 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 | |
| 178 | static void final_check(const struct ebt_u_entry *entry, |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 179 | const struct ebt_entry_match *match, const char *name, |
| 180 | unsigned int hook_mask, unsigned int time) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 181 | { |
| 182 | if (entry->bitmask & EBT_NOPROTO || entry->bitmask & EBT_802_3 || |
Bart De Schuymer | b2632c5 | 2002-08-09 18:57:05 +0000 | [diff] [blame^] | 183 | (entry->ethproto != ETH_P_ARP && entry->ethproto != ETH_P_RARP) || |
| 184 | entry->invflags & EBT_IPROTO) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 185 | print_error("For (R)ARP filtering the protocol must be " |
| 186 | "specified as ARP or RARP"); |
| 187 | } |
| 188 | |
| 189 | // defined in the ebt_ip.c |
| 190 | char *mask_to_dotted(__u32 mask); |
| 191 | static void print(const struct ebt_u_entry *entry, |
| 192 | const struct ebt_entry_match *match) |
| 193 | { |
| 194 | struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)match->data; |
| 195 | int i; |
| 196 | |
| 197 | if (arpinfo->bitmask & EBT_ARP_OPCODE) { |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 198 | printf("--arp-op "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 199 | if (arpinfo->invflags & EBT_ARP_OPCODE) |
| 200 | printf("! "); |
| 201 | printf("%d ", ntohs(arpinfo->opcode)); |
| 202 | } |
| 203 | if (arpinfo->bitmask & EBT_ARP_HTYPE) { |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 204 | printf("--arp-htype "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 205 | if (arpinfo->invflags & EBT_ARP_HTYPE) |
| 206 | printf("! "); |
| 207 | printf("%d ", ntohs(arpinfo->htype)); |
| 208 | } |
| 209 | if (arpinfo->bitmask & EBT_ARP_PTYPE) { |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 210 | printf("--arp-ptype "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 211 | if (arpinfo->invflags & EBT_ARP_PTYPE) |
| 212 | printf("! "); |
| 213 | printf("0x%x ", ntohs(arpinfo->ptype)); |
| 214 | } |
| 215 | if (arpinfo->bitmask & EBT_ARP_SRC_IP) { |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 216 | printf("--arp-ip-src "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 217 | if (arpinfo->invflags & EBT_ARP_SRC_IP) |
| 218 | printf("! "); |
| 219 | for (i = 0; i < 4; i++) |
| 220 | printf("%d%s", ((unsigned char *)&arpinfo->saddr)[i], |
| 221 | (i == 3) ? "" : "."); |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 222 | printf("%s ", mask_to_dotted(arpinfo->smsk)); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 223 | } |
| 224 | if (arpinfo->bitmask & EBT_ARP_DST_IP) { |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 225 | printf("--arp-ip-dst "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 226 | if (arpinfo->invflags & EBT_ARP_DST_IP) |
| 227 | printf("! "); |
| 228 | for (i = 0; i < 4; i++) |
| 229 | printf("%d%s", ((unsigned char *)&arpinfo->daddr)[i], |
| 230 | (i == 3) ? "" : "."); |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 231 | printf("%s ", mask_to_dotted(arpinfo->dmsk)); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | static int compare(const struct ebt_entry_match *m1, |
| 236 | const struct ebt_entry_match *m2) |
| 237 | { |
| 238 | struct ebt_arp_info *arpinfo1 = (struct ebt_arp_info *)m1->data; |
| 239 | struct ebt_arp_info *arpinfo2 = (struct ebt_arp_info *)m2->data; |
| 240 | |
| 241 | if (arpinfo1->bitmask != arpinfo2->bitmask) |
| 242 | return 0; |
| 243 | if (arpinfo1->invflags != arpinfo2->invflags) |
| 244 | return 0; |
| 245 | if (arpinfo1->bitmask & EBT_ARP_OPCODE) { |
| 246 | if (arpinfo1->opcode != arpinfo2->opcode) |
| 247 | return 0; |
| 248 | } |
| 249 | if (arpinfo1->bitmask & EBT_ARP_HTYPE) { |
| 250 | if (arpinfo1->htype != arpinfo2->htype) |
| 251 | return 0; |
| 252 | } |
| 253 | if (arpinfo1->bitmask & EBT_ARP_PTYPE) { |
| 254 | if (arpinfo1->ptype != arpinfo2->ptype) |
| 255 | return 0; |
| 256 | } |
| 257 | if (arpinfo1->bitmask & EBT_ARP_SRC_IP) { |
| 258 | if (arpinfo1->saddr != arpinfo2->saddr) |
| 259 | return 0; |
| 260 | if (arpinfo1->smsk != arpinfo2->smsk) |
| 261 | return 0; |
| 262 | } |
| 263 | if (arpinfo1->bitmask & EBT_ARP_DST_IP) { |
| 264 | if (arpinfo1->daddr != arpinfo2->daddr) |
| 265 | return 0; |
| 266 | if (arpinfo1->dmsk != arpinfo2->dmsk) |
| 267 | return 0; |
| 268 | } |
| 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | static struct ebt_u_match arp_match = |
| 273 | { |
| 274 | EBT_ARP_MATCH, |
| 275 | sizeof(struct ebt_arp_info), |
| 276 | print_help, |
| 277 | init, |
| 278 | parse, |
| 279 | final_check, |
| 280 | print, |
| 281 | compare, |
| 282 | opts, |
| 283 | }; |
| 284 | |
| 285 | static void _init(void) __attribute__ ((constructor)); |
| 286 | static void _init(void) |
| 287 | { |
| 288 | register_match(&arp_match); |
| 289 | } |