Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame^] | 1 | /* 802_3 |
| 2 | * |
| 3 | * Author: |
| 4 | * Chris Vitale <csv@bluetail.com> |
| 5 | * |
| 6 | * May 2003 |
| 7 | */ |
| 8 | |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <getopt.h> |
| 13 | #include "../include/ebtables_u.h" |
| 14 | #include "../include/ethernetdb.h" |
| 15 | #include <linux/netfilter_bridge/ebt_802_3.h> |
| 16 | |
| 17 | #define _802_3_SAP '1' |
| 18 | #define _802_3_TYPE '2' |
| 19 | |
| 20 | static struct option opts[] = |
| 21 | { |
| 22 | { "802_3-sap" , required_argument, 0, _802_3_SAP }, |
| 23 | { "802_3-type" , required_argument, 0, _802_3_TYPE }, |
| 24 | { 0 } |
| 25 | }; |
| 26 | |
| 27 | static void print_help() |
| 28 | { |
| 29 | printf( |
| 30 | "802_3 options:\n" |
| 31 | "--802_3-sap [!] protocol : 802.3 DSAP/SSAP- 1 byte value (hex)\n" |
| 32 | " DSAP and SSAP are always the same. One SAP applies to both fields\n" |
| 33 | "--802_3-type [!] protocol : 802.3 SNAP Type- 2 byte value (hex)\n" |
| 34 | " Type implies SAP value 0xaa\n"); |
| 35 | } |
| 36 | |
| 37 | static void init(struct ebt_entry_match *match) |
| 38 | { |
| 39 | struct ebt_802_3_info *info = (struct ebt_802_3_info *)match->data; |
| 40 | |
| 41 | info->invflags = 0; |
| 42 | info->bitmask = 0; |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static int parse(int c, char **argv, int argc, const struct ebt_u_entry *entry, |
| 46 | unsigned int *flags, struct ebt_entry_match **match) |
| 47 | { |
| 48 | struct ebt_802_3_info *info = (struct ebt_802_3_info *) (*match)->data; |
| 49 | unsigned int i; |
| 50 | char *end; |
| 51 | |
| 52 | switch (c) { |
| 53 | case _802_3_SAP: |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame^] | 54 | ebt_check_option2(flags, _802_3_SAP); |
| 55 | if (ebt_check_inverse2(optarg)) |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 56 | info->invflags |= EBT_802_3_SAP; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame^] | 57 | i = strtoul(optarg, &end, 16); |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 58 | if (i > 255 || *end != '\0') |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame^] | 59 | ebt_print_error2("Problem with specified " |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 60 | "sap hex value, %x",i); |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 61 | info->sap = i; /* one byte, so no byte order worries */ |
| 62 | info->bitmask |= EBT_802_3_SAP; |
| 63 | break; |
| 64 | case _802_3_TYPE: |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame^] | 65 | ebt_check_option2(flags, _802_3_TYPE); |
| 66 | if (ebt_check_inverse2(optarg)) |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 67 | info->invflags |= EBT_802_3_TYPE; |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame^] | 68 | i = strtoul(optarg, &end, 16); |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 69 | if (i > 65535 || *end != '\0') { |
Bart De Schuymer | ff58720 | 2005-02-08 20:02:28 +0000 | [diff] [blame^] | 70 | ebt_print_error2("Problem with the specified " |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 71 | "type hex value, %x",i); |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 72 | } |
| 73 | info->type = htons(i); |
| 74 | info->bitmask |= EBT_802_3_TYPE; |
| 75 | break; |
| 76 | default: |
| 77 | return 0; |
| 78 | } |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | static void final_check(const struct ebt_u_entry *entry, |
| 83 | const struct ebt_entry_match *match, const char *name, |
| 84 | unsigned int hookmask, unsigned int time) |
| 85 | { |
| 86 | if (!(entry->bitmask & EBT_802_3)) |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 87 | ebt_print_error("For 802.3 DSAP/SSAP filtering the protocol " |
| 88 | "must be LENGTH"); |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void print(const struct ebt_u_entry *entry, |
| 92 | const struct ebt_entry_match *match) |
| 93 | { |
| 94 | struct ebt_802_3_info *info = (struct ebt_802_3_info *)match->data; |
| 95 | |
| 96 | if (info->bitmask & EBT_802_3_SAP) { |
| 97 | printf("--802_3-sap "); |
| 98 | if (info->invflags & EBT_802_3_SAP) |
| 99 | printf("! "); |
| 100 | printf("0x%.2x ", info->sap); |
| 101 | } |
| 102 | if (info->bitmask & EBT_802_3_TYPE) { |
| 103 | printf("--802_3-type "); |
| 104 | if (info->invflags & EBT_802_3_TYPE) |
| 105 | printf("! "); |
| 106 | printf("0x%.4x ", ntohs(info->type)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static int compare(const struct ebt_entry_match *m1, |
| 111 | const struct ebt_entry_match *m2) |
| 112 | { |
| 113 | struct ebt_802_3_info *info1 = (struct ebt_802_3_info *)m1->data; |
| 114 | struct ebt_802_3_info *info2 = (struct ebt_802_3_info *)m2->data; |
| 115 | |
| 116 | if (info1->bitmask != info2->bitmask) |
| 117 | return 0; |
| 118 | if (info1->invflags != info2->invflags) |
| 119 | return 0; |
| 120 | if (info1->bitmask & EBT_802_3_SAP) { |
| 121 | if (info1->sap != info2->sap) |
| 122 | return 0; |
| 123 | } |
| 124 | if (info1->bitmask & EBT_802_3_TYPE) { |
| 125 | if (info1->type != info2->type) |
| 126 | return 0; |
| 127 | } |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | static struct ebt_u_match _802_3_match = |
| 132 | { |
Bart De Schuymer | 7cf1cca | 2003-08-30 16:20:19 +0000 | [diff] [blame] | 133 | .name = EBT_802_3_MATCH, |
| 134 | .size = sizeof(struct ebt_802_3_info), |
| 135 | .help = print_help, |
| 136 | .init = init, |
| 137 | .parse = parse, |
| 138 | .final_check = final_check, |
| 139 | .print = print, |
| 140 | .compare = compare, |
| 141 | .extra_ops = opts, |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Bart De Schuymer | 64182a3 | 2004-01-21 20:39:54 +0000 | [diff] [blame] | 144 | void _init(void) |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 145 | { |
Bart De Schuymer | 8339ff1 | 2004-01-14 20:05:27 +0000 | [diff] [blame] | 146 | ebt_register_match(&_802_3_match); |
Bart De Schuymer | 8e0e25d | 2003-05-12 17:00:26 +0000 | [diff] [blame] | 147 | } |