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> |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 4 | #include <getopt.h> |
| 5 | #include "../include/ebtables_u.h" |
Bart De Schuymer | c1939b1 | 2002-11-20 19:41:54 +0000 | [diff] [blame] | 6 | #include "../include/ethernetdb.h" |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 7 | #include <linux/netfilter_bridge/ebt_arp.h> |
| 8 | |
| 9 | #define ARP_OPCODE '1' |
| 10 | #define ARP_HTYPE '2' |
| 11 | #define ARP_PTYPE '3' |
| 12 | #define ARP_IP_S '4' |
| 13 | #define ARP_IP_D '5' |
| 14 | static struct option opts[] = |
| 15 | { |
| 16 | { "arp-opcode" , required_argument, 0, ARP_OPCODE }, |
| 17 | { "arp-op" , required_argument, 0, ARP_OPCODE }, |
| 18 | { "arp-htype" , required_argument, 0, ARP_HTYPE }, |
| 19 | { "arp-ptype" , required_argument, 0, ARP_PTYPE }, |
| 20 | { "arp-ip-src" , required_argument, 0, ARP_IP_S }, |
| 21 | { "arp-ip-dst" , required_argument, 0, ARP_IP_D }, |
| 22 | { 0 } |
| 23 | }; |
| 24 | |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 25 | #define NUMOPCODES 9 |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 26 | // a few names |
| 27 | static char *opcodes[] = |
| 28 | { |
| 29 | "Request", |
| 30 | "Reply", |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 31 | "Request_Reverse", |
| 32 | "Reply_Reverse", |
| 33 | "DRARP_Request", |
| 34 | "DRARP_Reply", |
| 35 | "DRARP_Error", |
| 36 | "InARP_Request", |
| 37 | "ARP_NAK", |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static void print_help() |
| 41 | { |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 42 | int i; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 43 | |
| 44 | printf( |
| 45 | "arp options:\n" |
| 46 | "--arp-opcode opcode : ARP opcode (integer or string)\n" |
| 47 | "--arp-htype type : ARP hardware type (integer or string)\n" |
| 48 | "--arp-ptype type : ARP protocol type (hexadecimal or string)\n" |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 49 | "--arp-ip-src [!] address[/mask]: ARP IP source specification\n" |
| 50 | "--arp-ip-dst [!] address[/mask]: ARP IP target specification\n" |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 51 | " opcode strings: \n"); |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 52 | for (i = 0; i < NUMOPCODES; i++) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 53 | printf("%d = %s\n", i + 1, opcodes[i]); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 54 | printf( |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 55 | " hardware type string: 1 = Ethernet\n" |
| 56 | " protocol type string: see /etc/ethertypes\n"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void init(struct ebt_entry_match *match) |
| 60 | { |
| 61 | struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)match->data; |
| 62 | |
| 63 | arpinfo->invflags = 0; |
| 64 | arpinfo->bitmask = 0; |
| 65 | } |
| 66 | |
| 67 | // defined in ebt_ip.c |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 68 | void parse_ip_address(char *address, uint32_t *addr, uint32_t *msk); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 69 | |
| 70 | #define OPT_OPCODE 0x01 |
| 71 | #define OPT_HTYPE 0x02 |
| 72 | #define OPT_PTYPE 0x04 |
| 73 | #define OPT_IP_S 0x08 |
| 74 | #define OPT_IP_D 0x10 |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 75 | static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry, |
| 76 | unsigned int *flags, struct ebt_entry_match **match) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 77 | { |
| 78 | struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)(*match)->data; |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 79 | long int i; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 80 | char *end; |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 81 | uint32_t *addr; |
| 82 | uint32_t *mask; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 83 | |
| 84 | switch (c) { |
| 85 | case ARP_OPCODE: |
| 86 | check_option(flags, OPT_OPCODE); |
| 87 | if (check_inverse(optarg)) |
| 88 | arpinfo->invflags |= EBT_ARP_OPCODE; |
| 89 | |
| 90 | if (optind > argc) |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 91 | print_error("Missing ARP opcode argument"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 92 | i = strtol(argv[optind - 1], &end, 10); |
| 93 | if (i < 0 || i >= (0x1 << 16) || *end !='\0') { |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 94 | for (i = 0; i < NUMOPCODES; i++) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 95 | if (!strcasecmp(opcodes[i], optarg)) |
| 96 | break; |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 97 | if (i == NUMOPCODES) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 98 | print_error("Problem with specified " |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 99 | "ARP opcode"); |
| 100 | i++; |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 101 | } |
| 102 | arpinfo->opcode = htons(i); |
| 103 | arpinfo->bitmask |= EBT_ARP_OPCODE; |
| 104 | break; |
| 105 | |
| 106 | case ARP_HTYPE: |
| 107 | check_option(flags, OPT_HTYPE); |
| 108 | if (check_inverse(optarg)) |
| 109 | arpinfo->invflags |= EBT_ARP_HTYPE; |
| 110 | |
| 111 | if (optind > argc) |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 112 | print_error("Missing ARP hardware type argument"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 113 | i = strtol(argv[optind - 1], &end, 10); |
| 114 | if (i < 0 || i >= (0x1 << 16) || *end !='\0') { |
| 115 | if (!strcasecmp("Ethernet", argv[optind - 1])) |
| 116 | i = 1; |
| 117 | else |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 118 | print_error("Problem with specified ARP " |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 119 | "hardware type"); |
| 120 | } |
| 121 | arpinfo->htype = htons(i); |
| 122 | arpinfo->bitmask |= EBT_ARP_HTYPE; |
| 123 | break; |
| 124 | |
| 125 | case ARP_PTYPE: |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 126 | { |
| 127 | uint16_t proto; |
| 128 | |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 129 | check_option(flags, OPT_PTYPE); |
| 130 | if (check_inverse(optarg)) |
| 131 | arpinfo->invflags |= EBT_ARP_PTYPE; |
| 132 | |
| 133 | if (optind > argc) |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 134 | print_error("Missing ARP protocol type argument"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 135 | i = strtol(argv[optind - 1], &end, 16); |
| 136 | if (i < 0 || i >= (0x1 << 16) || *end !='\0') { |
Bart De Schuymer | c1939b1 | 2002-11-20 19:41:54 +0000 | [diff] [blame] | 137 | struct ethertypeent *ent; |
| 138 | |
| 139 | ent = getethertypebyname(argv[optind - 1]); |
| 140 | if (!ent) |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 141 | print_error("Problem with specified ARP " |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 142 | "protocol type"); |
Bart De Schuymer | c1939b1 | 2002-11-20 19:41:54 +0000 | [diff] [blame] | 143 | proto = ent->e_ethertype; |
| 144 | |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 145 | } else |
| 146 | proto = i; |
| 147 | arpinfo->ptype = htons(proto); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 148 | arpinfo->bitmask |= EBT_ARP_PTYPE; |
| 149 | break; |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 150 | } |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 151 | |
| 152 | case ARP_IP_S: |
| 153 | case ARP_IP_D: |
| 154 | if (c == ARP_IP_S) { |
| 155 | check_option(flags, OPT_IP_S); |
| 156 | addr = &arpinfo->saddr; |
| 157 | mask = &arpinfo->smsk; |
| 158 | arpinfo->bitmask |= EBT_ARP_SRC_IP; |
| 159 | } else { |
| 160 | check_option(flags, OPT_IP_D); |
| 161 | addr = &arpinfo->daddr; |
| 162 | mask = &arpinfo->dmsk; |
| 163 | arpinfo->bitmask |= EBT_ARP_DST_IP; |
| 164 | } |
| 165 | if (check_inverse(optarg)) { |
| 166 | if (c == ARP_IP_S) |
| 167 | arpinfo->invflags |= EBT_ARP_SRC_IP; |
| 168 | else |
| 169 | arpinfo->invflags |= EBT_ARP_DST_IP; |
| 170 | } |
| 171 | if (optind > argc) |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 172 | print_error("Missing ARP IP address argument"); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 173 | parse_ip_address(argv[optind - 1], addr, mask); |
| 174 | break; |
| 175 | default: |
| 176 | return 0; |
| 177 | } |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | static void final_check(const struct ebt_u_entry *entry, |
Bart De Schuymer | 7b9aaeb | 2002-06-23 20:38:34 +0000 | [diff] [blame] | 182 | const struct ebt_entry_match *match, const char *name, |
Bart De Schuymer | c9b5293 | 2002-08-24 13:26:34 +0000 | [diff] [blame] | 183 | unsigned int hookmask, unsigned int time) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 184 | { |
Bart De Schuymer | 4057319 | 2002-08-29 16:48:36 +0000 | [diff] [blame] | 185 | if ((entry->ethproto != ETH_P_ARP && entry->ethproto != ETH_P_RARP) || |
Bart De Schuymer | b2632c5 | 2002-08-09 18:57:05 +0000 | [diff] [blame] | 186 | entry->invflags & EBT_IPROTO) |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 187 | print_error("For (R)ARP filtering the protocol must be " |
| 188 | "specified as ARP or RARP"); |
| 189 | } |
| 190 | |
| 191 | // defined in the ebt_ip.c |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 192 | char *mask_to_dotted(uint32_t mask); |
| 193 | |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 194 | static void print(const struct ebt_u_entry *entry, |
| 195 | const struct ebt_entry_match *match) |
| 196 | { |
| 197 | struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)match->data; |
| 198 | int i; |
| 199 | |
| 200 | if (arpinfo->bitmask & EBT_ARP_OPCODE) { |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 201 | int opcode = ntohs(arpinfo->opcode); |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 202 | printf("--arp-op "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 203 | if (arpinfo->invflags & EBT_ARP_OPCODE) |
| 204 | printf("! "); |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 205 | if (opcode > 0 && opcode <= NUMOPCODES) |
| 206 | printf("%s ", opcodes[opcode - 1]); |
| 207 | else |
| 208 | printf("%d ", opcode); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 209 | } |
| 210 | if (arpinfo->bitmask & EBT_ARP_HTYPE) { |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 211 | printf("--arp-htype "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 212 | if (arpinfo->invflags & EBT_ARP_HTYPE) |
| 213 | printf("! "); |
| 214 | printf("%d ", ntohs(arpinfo->htype)); |
| 215 | } |
| 216 | if (arpinfo->bitmask & EBT_ARP_PTYPE) { |
Bart De Schuymer | c1939b1 | 2002-11-20 19:41:54 +0000 | [diff] [blame] | 217 | struct ethertypeent *ent; |
| 218 | |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 219 | printf("--arp-ptype "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 220 | if (arpinfo->invflags & EBT_ARP_PTYPE) |
| 221 | printf("! "); |
Bart De Schuymer | c1939b1 | 2002-11-20 19:41:54 +0000 | [diff] [blame] | 222 | ent = getethertypebynumber(ntohs(arpinfo->ptype)); |
| 223 | if (!ent) |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 224 | printf("0x%x ", ntohs(arpinfo->ptype)); |
| 225 | else |
Bart De Schuymer | c1939b1 | 2002-11-20 19:41:54 +0000 | [diff] [blame] | 226 | printf("%s ", ent->e_name); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 227 | } |
| 228 | if (arpinfo->bitmask & EBT_ARP_SRC_IP) { |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 229 | printf("--arp-ip-src "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 230 | if (arpinfo->invflags & EBT_ARP_SRC_IP) |
| 231 | printf("! "); |
| 232 | for (i = 0; i < 4; i++) |
| 233 | printf("%d%s", ((unsigned char *)&arpinfo->saddr)[i], |
| 234 | (i == 3) ? "" : "."); |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 235 | printf("%s ", mask_to_dotted(arpinfo->smsk)); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 236 | } |
| 237 | if (arpinfo->bitmask & EBT_ARP_DST_IP) { |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 238 | printf("--arp-ip-dst "); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 239 | if (arpinfo->invflags & EBT_ARP_DST_IP) |
| 240 | printf("! "); |
| 241 | for (i = 0; i < 4; i++) |
| 242 | printf("%d%s", ((unsigned char *)&arpinfo->daddr)[i], |
| 243 | (i == 3) ? "" : "."); |
Bart De Schuymer | 41e8a19 | 2002-06-23 08:03:12 +0000 | [diff] [blame] | 244 | printf("%s ", mask_to_dotted(arpinfo->dmsk)); |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | static int compare(const struct ebt_entry_match *m1, |
| 249 | const struct ebt_entry_match *m2) |
| 250 | { |
| 251 | struct ebt_arp_info *arpinfo1 = (struct ebt_arp_info *)m1->data; |
| 252 | struct ebt_arp_info *arpinfo2 = (struct ebt_arp_info *)m2->data; |
| 253 | |
| 254 | if (arpinfo1->bitmask != arpinfo2->bitmask) |
| 255 | return 0; |
| 256 | if (arpinfo1->invflags != arpinfo2->invflags) |
| 257 | return 0; |
| 258 | if (arpinfo1->bitmask & EBT_ARP_OPCODE) { |
| 259 | if (arpinfo1->opcode != arpinfo2->opcode) |
| 260 | return 0; |
| 261 | } |
| 262 | if (arpinfo1->bitmask & EBT_ARP_HTYPE) { |
| 263 | if (arpinfo1->htype != arpinfo2->htype) |
| 264 | return 0; |
| 265 | } |
| 266 | if (arpinfo1->bitmask & EBT_ARP_PTYPE) { |
| 267 | if (arpinfo1->ptype != arpinfo2->ptype) |
| 268 | return 0; |
| 269 | } |
| 270 | if (arpinfo1->bitmask & EBT_ARP_SRC_IP) { |
| 271 | if (arpinfo1->saddr != arpinfo2->saddr) |
| 272 | return 0; |
| 273 | if (arpinfo1->smsk != arpinfo2->smsk) |
| 274 | return 0; |
| 275 | } |
| 276 | if (arpinfo1->bitmask & EBT_ARP_DST_IP) { |
| 277 | if (arpinfo1->daddr != arpinfo2->daddr) |
| 278 | return 0; |
| 279 | if (arpinfo1->dmsk != arpinfo2->dmsk) |
| 280 | return 0; |
| 281 | } |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | static struct ebt_u_match arp_match = |
| 286 | { |
| 287 | EBT_ARP_MATCH, |
| 288 | sizeof(struct ebt_arp_info), |
| 289 | print_help, |
| 290 | init, |
| 291 | parse, |
| 292 | final_check, |
| 293 | print, |
| 294 | compare, |
Bart De Schuymer | 9cfd654 | 2002-08-13 16:08:08 +0000 | [diff] [blame] | 295 | opts |
Bart De Schuymer | 1abc55d | 2002-06-01 19:23:47 +0000 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | static void _init(void) __attribute__ ((constructor)); |
| 299 | static void _init(void) |
| 300 | { |
| 301 | register_match(&arp_match); |
| 302 | } |