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> |
| 6 | #include <linux/netfilter_bridge/ebtables.h> |
| 7 | #include <getopt.h> |
| 8 | #include "../include/ebtables_u.h" |
| 9 | #include <linux/netfilter_bridge/ebt_arp.h> |
| 10 | |
| 11 | #define ARP_OPCODE '1' |
| 12 | #define ARP_HTYPE '2' |
| 13 | #define ARP_PTYPE '3' |
| 14 | #define ARP_IP_S '4' |
| 15 | #define ARP_IP_D '5' |
| 16 | static struct option opts[] = |
| 17 | { |
| 18 | { "arp-opcode" , required_argument, 0, ARP_OPCODE }, |
| 19 | { "arp-op" , required_argument, 0, ARP_OPCODE }, |
| 20 | { "arp-htype" , required_argument, 0, ARP_HTYPE }, |
| 21 | { "arp-ptype" , required_argument, 0, ARP_PTYPE }, |
| 22 | { "arp-ip-src" , required_argument, 0, ARP_IP_S }, |
| 23 | { "arp-ip-dst" , required_argument, 0, ARP_IP_D }, |
| 24 | { 0 } |
| 25 | }; |
| 26 | |
| 27 | // a few names |
| 28 | static char *opcodes[] = |
| 29 | { |
| 30 | "Request", |
| 31 | "Reply", |
| 32 | "Request Reverse", |
| 33 | "Reply Reverse", |
| 34 | "DRARP Request", |
| 35 | "DRARP Reply", |
| 36 | "DRARP Error", |
| 37 | "InARP Request", |
| 38 | "ARP NAK", |
| 39 | "" |
| 40 | }; |
| 41 | |
| 42 | static void print_help() |
| 43 | { |
| 44 | int i = 0; |
| 45 | |
| 46 | printf( |
| 47 | "arp options:\n" |
| 48 | "--arp-opcode opcode : ARP opcode (integer or string)\n" |
| 49 | "--arp-htype type : ARP hardware type (integer or string)\n" |
| 50 | "--arp-ptype type : ARP protocol type (hexadecimal or string)\n" |
| 51 | "--arp-ip-src [!] address[/mask]: ARP ip source specification\n" |
| 52 | "--arp-ip-dst [!] address[/mask]: ARP ip target specification\n" |
| 53 | " opcode strings: \n"); |
| 54 | while (strcmp(opcodes[i], "")) { |
| 55 | printf("%d = %s\n", i + 1, opcodes[i]); |
| 56 | i++; |
| 57 | } |
| 58 | printf( |
| 59 | " hardware type string: \n 1 = Ethernet\n" |
| 60 | " protocol type string: \n 0x0800 = IPv4\n"); |
| 61 | } |
| 62 | |
| 63 | static void init(struct ebt_entry_match *match) |
| 64 | { |
| 65 | struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)match->data; |
| 66 | |
| 67 | arpinfo->invflags = 0; |
| 68 | arpinfo->bitmask = 0; |
| 69 | } |
| 70 | |
| 71 | // defined in ebt_ip.c |
| 72 | void parse_ip_address(char *address, __u32 *addr, __u32 *msk); |
| 73 | |
| 74 | #define OPT_OPCODE 0x01 |
| 75 | #define OPT_HTYPE 0x02 |
| 76 | #define OPT_PTYPE 0x04 |
| 77 | #define OPT_IP_S 0x08 |
| 78 | #define OPT_IP_D 0x10 |
| 79 | static int parse(int c, char **argv, int argc, |
| 80 | const struct ebt_u_entry *entry, unsigned int *flags, |
| 81 | struct ebt_entry_match **match) |
| 82 | { |
| 83 | struct ebt_arp_info *arpinfo = (struct ebt_arp_info *)(*match)->data; |
| 84 | int i; |
| 85 | char *end; |
| 86 | __u32 *addr; |
| 87 | __u32 *mask; |
| 88 | |
| 89 | switch (c) { |
| 90 | case ARP_OPCODE: |
| 91 | check_option(flags, OPT_OPCODE); |
| 92 | if (check_inverse(optarg)) |
| 93 | arpinfo->invflags |= EBT_ARP_OPCODE; |
| 94 | |
| 95 | if (optind > argc) |
| 96 | print_error("Missing arp opcode argument"); |
| 97 | i = strtol(argv[optind - 1], &end, 10); |
| 98 | if (i < 0 || i >= (0x1 << 16) || *end !='\0') { |
| 99 | i = 0; |
| 100 | while (strcmp(opcodes[i], "")) { |
| 101 | if (!strcasecmp(opcodes[i], optarg)) |
| 102 | break; |
| 103 | i++; |
| 104 | } |
| 105 | if (!strcmp(opcodes[i], "")) |
| 106 | print_error("Problem with specified " |
| 107 | "arp opcode"); |
| 108 | } |
| 109 | arpinfo->opcode = htons(i); |
| 110 | arpinfo->bitmask |= EBT_ARP_OPCODE; |
| 111 | break; |
| 112 | |
| 113 | case ARP_HTYPE: |
| 114 | check_option(flags, OPT_HTYPE); |
| 115 | if (check_inverse(optarg)) |
| 116 | arpinfo->invflags |= EBT_ARP_HTYPE; |
| 117 | |
| 118 | if (optind > argc) |
| 119 | print_error("Missing arp hardware type argument"); |
| 120 | i = strtol(argv[optind - 1], &end, 10); |
| 121 | if (i < 0 || i >= (0x1 << 16) || *end !='\0') { |
| 122 | if (!strcasecmp("Ethernet", argv[optind - 1])) |
| 123 | i = 1; |
| 124 | else |
| 125 | print_error("Problem with specified arp " |
| 126 | "hardware type"); |
| 127 | } |
| 128 | arpinfo->htype = htons(i); |
| 129 | arpinfo->bitmask |= EBT_ARP_HTYPE; |
| 130 | break; |
| 131 | |
| 132 | case ARP_PTYPE: |
| 133 | check_option(flags, OPT_PTYPE); |
| 134 | if (check_inverse(optarg)) |
| 135 | arpinfo->invflags |= EBT_ARP_PTYPE; |
| 136 | |
| 137 | if (optind > argc) |
| 138 | print_error("Missing arp protocol type argument"); |
| 139 | i = strtol(argv[optind - 1], &end, 16); |
| 140 | if (i < 0 || i >= (0x1 << 16) || *end !='\0') { |
| 141 | if (!strcasecmp("IPv4", argv[optind - 1])) |
| 142 | i = 0x0800; |
| 143 | else |
| 144 | print_error("Problem with specified arp " |
| 145 | "protocol type"); |
| 146 | } |
| 147 | arpinfo->ptype = htons(i); |
| 148 | arpinfo->bitmask |= EBT_ARP_PTYPE; |
| 149 | break; |
| 150 | |
| 151 | case ARP_IP_S: |
| 152 | case ARP_IP_D: |
| 153 | if (c == ARP_IP_S) { |
| 154 | check_option(flags, OPT_IP_S); |
| 155 | addr = &arpinfo->saddr; |
| 156 | mask = &arpinfo->smsk; |
| 157 | arpinfo->bitmask |= EBT_ARP_SRC_IP; |
| 158 | } else { |
| 159 | check_option(flags, OPT_IP_D); |
| 160 | addr = &arpinfo->daddr; |
| 161 | mask = &arpinfo->dmsk; |
| 162 | arpinfo->bitmask |= EBT_ARP_DST_IP; |
| 163 | } |
| 164 | if (check_inverse(optarg)) { |
| 165 | if (c == ARP_IP_S) |
| 166 | arpinfo->invflags |= EBT_ARP_SRC_IP; |
| 167 | else |
| 168 | arpinfo->invflags |= EBT_ARP_DST_IP; |
| 169 | } |
| 170 | if (optind > argc) |
| 171 | print_error("Missing ip address argument"); |
| 172 | parse_ip_address(argv[optind - 1], addr, mask); |
| 173 | break; |
| 174 | default: |
| 175 | return 0; |
| 176 | } |
| 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | static void final_check(const struct ebt_u_entry *entry, |
| 181 | const struct ebt_entry_match *match, const char *name, unsigned int hook) |
| 182 | { |
| 183 | if (entry->bitmask & EBT_NOPROTO || entry->bitmask & EBT_802_3 || |
| 184 | (entry->ethproto != ETH_P_ARP && entry->ethproto != ETH_P_RARP)) |
| 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) { |
| 198 | printf("arp opcode: "); |
| 199 | if (arpinfo->invflags & EBT_ARP_OPCODE) |
| 200 | printf("! "); |
| 201 | printf("%d ", ntohs(arpinfo->opcode)); |
| 202 | } |
| 203 | if (arpinfo->bitmask & EBT_ARP_HTYPE) { |
| 204 | printf("arp htype: "); |
| 205 | if (arpinfo->invflags & EBT_ARP_HTYPE) |
| 206 | printf("! "); |
| 207 | printf("%d ", ntohs(arpinfo->htype)); |
| 208 | } |
| 209 | if (arpinfo->bitmask & EBT_ARP_PTYPE) { |
| 210 | printf("arp ptype: "); |
| 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) { |
| 216 | printf("arp src IP "); |
| 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) ? "" : "."); |
| 222 | printf("%s, ", mask_to_dotted(arpinfo->smsk)); |
| 223 | } |
| 224 | if (arpinfo->bitmask & EBT_ARP_DST_IP) { |
| 225 | printf("arp dst IP "); |
| 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) ? "" : "."); |
| 231 | printf("%s, ", mask_to_dotted(arpinfo->dmsk)); |
| 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 | } |