Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <getopt.h> |
| 5 | #include <ctype.h> |
| 6 | #include <netinet/ether.h> |
| 7 | #include "../include/ebtables_u.h" |
| 8 | #include "../include/ethernetdb.h" |
| 9 | #include <linux/if_ether.h> |
| 10 | #include <linux/netfilter_bridge/ebt_among.h> |
| 11 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 12 | #define NODEBUG |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 13 | |
| 14 | #define AMONG_DST '1' |
| 15 | #define AMONG_SRC '2' |
| 16 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 17 | static struct option opts[] = { |
| 18 | {"among-dst", required_argument, 0, AMONG_DST}, |
| 19 | {"among-src", required_argument, 0, AMONG_SRC}, |
| 20 | {0} |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | #ifdef DEBUG |
| 24 | static void hexdump(const void *mem, int howmany) |
| 25 | { |
| 26 | printf("\n"); |
| 27 | const unsigned char *p = mem; |
| 28 | int i; |
| 29 | for (i = 0; i < howmany; i++) { |
| 30 | if (i % 32 == 0) { |
| 31 | printf("\n%04x: ", i); |
| 32 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 33 | printf("%2.2x%c", p[i], ". "[i % 4 == 3]); |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 34 | } |
| 35 | printf("\n"); |
| 36 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 37 | #endif /* DEBUG */ |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 38 | |
| 39 | static void print_help() |
| 40 | { |
| 41 | printf( |
| 42 | "`among' options:\n" |
| 43 | "--among-dst list : matches if ether dst is in list\n" |
| 44 | "--among-src list : matches if ether src is in list\n" |
| 45 | "list has form:\n" |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 46 | " xx:xx:xx:xx:xx:xx[=ip.ip.ip.ip],yy:yy:yy:yy:yy:yy[=ip.ip.ip.ip]" |
| 47 | ",...,zz:zz:zz:zz:zz:zz[=ip.ip.ip.ip][,]\n" |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 48 | "Things in brackets are optional.\n" |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 49 | "If you want to allow two (or more) IP addresses to one MAC address, you \n" |
| 50 | "can specify two (or more) pairs witch the same MAC, e.g.\n" |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 51 | " 00:00:00:fa:eb:fe=153.19.120.250,00:00:00:fa:eb:fe=192.168.0.1\n" |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 52 | ); |
| 53 | } |
| 54 | |
| 55 | static void init(struct ebt_entry_match *match) |
| 56 | { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 57 | struct ebt_among_info *amonginfo = |
| 58 | (struct ebt_among_info *) match->data; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 59 | |
| 60 | memset(amonginfo, 0, sizeof(struct ebt_among_info)); |
| 61 | } |
| 62 | |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 63 | static struct ebt_mac_wormhash *new_wormhash(int n) |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 64 | { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 65 | int size = |
| 66 | sizeof(struct ebt_mac_wormhash) + |
| 67 | n * sizeof(struct ebt_mac_wormhash_tuple); |
| 68 | struct ebt_mac_wormhash *result = |
| 69 | (struct ebt_mac_wormhash *) malloc(size); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 70 | memset(result, 0, size); |
| 71 | result->poolsize = n; |
| 72 | return result; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 73 | } |
| 74 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 75 | static void copy_wormhash(struct ebt_mac_wormhash *d, |
| 76 | const struct ebt_mac_wormhash *s) |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 77 | { |
| 78 | int dpoolsize = d->poolsize; |
| 79 | int dsize, ssize, amount; |
| 80 | dsize = ebt_mac_wormhash_size(d); |
| 81 | ssize = ebt_mac_wormhash_size(s); |
| 82 | amount = dsize < ssize ? dsize : ssize; |
| 83 | memcpy(d, s, amount); |
| 84 | d->poolsize = dpoolsize; |
| 85 | } |
| 86 | |
| 87 | /* Returns: |
| 88 | * -1 when '\0' reached |
| 89 | * -2 when `n' bytes read and no delimiter found |
| 90 | * 0 when no less than `n' bytes read and delimiter found |
| 91 | * if `destbuf' is not NULL, it is filled by read bytes and ended with '\0' |
| 92 | * *pp is set on the first byte not copied to `destbuf' |
| 93 | */ |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 94 | static int read_until(const char **pp, const char *delimiters, |
| 95 | char *destbuf, int n) |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 96 | { |
| 97 | int count = 0; |
| 98 | int ret = 0; |
| 99 | char c; |
| 100 | while (1) { |
| 101 | c = **pp; |
| 102 | if (!c) { |
| 103 | ret = -1; |
| 104 | break; |
| 105 | } |
| 106 | if (strchr(delimiters, c)) { |
| 107 | ret = 0; |
| 108 | break; |
| 109 | } |
| 110 | if (count == n) { |
| 111 | ret = -2; |
| 112 | break; |
| 113 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 114 | if (destbuf) |
| 115 | destbuf[count++] = c; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 116 | (*pp)++; |
| 117 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 118 | if (destbuf) |
| 119 | destbuf[count] = 0; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 120 | return ret; |
| 121 | } |
| 122 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 123 | static int fcmp(const void *va, const void *vb) { |
| 124 | const struct ebt_mac_wormhash_tuple *a = va; |
| 125 | const struct ebt_mac_wormhash_tuple *b = vb; |
| 126 | int ca = ((const unsigned char*)a->cmp)[7]; |
| 127 | int cb = ((const unsigned char*)b->cmp)[7]; |
| 128 | return ca - cb; |
| 129 | } |
| 130 | |
| 131 | static void index_table(struct ebt_mac_wormhash *wh) |
| 132 | { |
| 133 | int ipool, itable; |
| 134 | int c; |
| 135 | for (itable = 0; itable <= 256; itable++) { |
| 136 | wh->table[itable] = wh->poolsize; |
| 137 | } |
| 138 | ipool = 0; |
| 139 | itable = 0; |
| 140 | while (1) { |
| 141 | wh->table[itable] = ipool; |
| 142 | c = ((const unsigned char*)wh->pool[ipool].cmp)[7]; |
| 143 | if (itable <= c) { |
| 144 | itable++; |
| 145 | } else { |
| 146 | ipool++; |
| 147 | } |
| 148 | if (ipool > wh->poolsize) |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 153 | static struct ebt_mac_wormhash *create_wormhash(const char *arg) |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 154 | { |
| 155 | const char *pc = arg; |
| 156 | const char *anchor; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 157 | char *endptr; |
| 158 | struct ebt_mac_wormhash *workcopy, *result, *h; |
| 159 | unsigned char mac[6]; |
| 160 | unsigned char ip[4]; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 161 | int nmacs = 0; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 162 | int i; |
| 163 | char token[4]; |
| 164 | if (!(workcopy = new_wormhash(1024))) { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 165 | print_memory(); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 166 | } |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 167 | while (1) { |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 168 | /* remember current position, we'll need it on error */ |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 169 | anchor = pc; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 170 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 171 | /* collect MAC; all its bytes are followed by ':' (colon), |
| 172 | * except for the last one which can be followed by |
| 173 | * ',' (comma), '=' or '\0' */ |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 174 | for (i = 0; i < 5; i++) { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 175 | if (read_until(&pc, ":", token, 2) < 0 |
| 176 | || token[0] == 0) { |
| 177 | print_error("MAC parse error: %.20s", |
| 178 | anchor); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 179 | } |
| 180 | mac[i] = strtol(token, &endptr, 16); |
| 181 | if (*endptr) { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 182 | print_error("MAC parse error: %.20s", |
| 183 | anchor); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 184 | } |
| 185 | pc++; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 186 | } |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 187 | if (read_until(&pc, "=,", token, 2) == -2 || token[0] == 0) { |
| 188 | print_error("MAC parse error: %.20s", anchor); |
| 189 | } |
| 190 | mac[i] = strtol(token, &endptr, 16); |
| 191 | if (*endptr) { |
| 192 | print_error("MAC parse error: %.20s", anchor); |
| 193 | } |
| 194 | if (*pc == '=') { |
| 195 | /* an IP follows the MAC; collect similarly to MAC */ |
| 196 | pc++; |
| 197 | anchor = pc; |
| 198 | for (i = 0; i < 3; i++) { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 199 | if (read_until(&pc, ".", token, 3) < 0 |
| 200 | || token[0] == 0) { |
| 201 | print_error |
| 202 | ("IP parse error: %.20s", |
| 203 | anchor); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 204 | } |
| 205 | ip[i] = strtol(token, &endptr, 10); |
| 206 | if (*endptr) { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 207 | print_error |
| 208 | ("IP parse error: %.20s", |
| 209 | anchor); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 210 | } |
| 211 | pc++; |
| 212 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 213 | if (read_until(&pc, ",", token, 3) == -2 |
| 214 | || token[0] == 0) { |
| 215 | print_error("IP parse error: %.20s", |
| 216 | anchor); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 217 | } |
| 218 | ip[3] = strtol(token, &endptr, 10); |
| 219 | if (*endptr) { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 220 | print_error("IP parse error: %.20s", |
| 221 | anchor); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 222 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 223 | if (*(uint32_t*)ip == 0) { |
| 224 | print_error("Illegal IP 0.0.0.0"); |
| 225 | } |
| 226 | } else { |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 227 | /* no IP, we set it to 0.0.0.0 */ |
| 228 | memset(ip, 0, 4); |
| 229 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 230 | |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 231 | /* we have collected MAC and IP, so we add an entry */ |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 232 | memcpy(((char *) workcopy->pool[nmacs].cmp) + 2, mac, 6); |
| 233 | workcopy->pool[nmacs].ip = *(const uint32_t *) ip; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 234 | nmacs++; |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 235 | |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 236 | /* re-allocate memory if needed */ |
| 237 | if (*pc && nmacs >= workcopy->poolsize) { |
| 238 | if (!(h = new_wormhash(nmacs * 2))) { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 239 | print_memory(); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 240 | } |
| 241 | copy_wormhash(h, workcopy); |
| 242 | free(workcopy); |
| 243 | workcopy = h; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 244 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 245 | |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 246 | /* check if end of string was reached */ |
| 247 | if (!*pc) { |
| 248 | break; |
| 249 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 250 | |
| 251 | /* now `pc' points to comma if we are here; */ |
| 252 | /* increment this to the next char */ |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 253 | /* but first assert :-> */ |
| 254 | if (*pc != ',') { |
| 255 | print_error("Something went wrong; no comma...\n"); |
| 256 | } |
| 257 | pc++; |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 258 | |
| 259 | /* again check if end of string was reached; */ |
| 260 | /* we allow an ending comma */ |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 261 | if (!*pc) { |
| 262 | break; |
| 263 | } |
| 264 | } |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 265 | if (!(result = new_wormhash(nmacs))) { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 266 | print_memory(); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 267 | } |
| 268 | copy_wormhash(result, workcopy); |
| 269 | free(workcopy); |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 270 | qsort(&result->pool, result->poolsize, |
| 271 | sizeof(struct ebt_mac_wormhash_tuple), fcmp); |
| 272 | index_table(result); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 273 | return result; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | #define OPT_DST 0x01 |
| 277 | #define OPT_SRC 0x02 |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 278 | static int parse(int c, char **argv, int argc, |
| 279 | const struct ebt_u_entry *entry, unsigned int *flags, |
| 280 | struct ebt_entry_match **match) |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 281 | { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 282 | struct ebt_among_info *info = |
| 283 | (struct ebt_among_info *) (*match)->data; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 284 | struct ebt_mac_wormhash *wh; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 285 | struct ebt_entry_match *h; |
| 286 | int new_size, old_size; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 287 | |
| 288 | switch (c) { |
| 289 | case AMONG_DST: |
| 290 | case AMONG_SRC: |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 291 | if (check_inverse(optarg)) { |
| 292 | if (c == AMONG_DST) |
| 293 | info->bitmask |= EBT_AMONG_DST_NEG; |
| 294 | else |
| 295 | info->bitmask |= EBT_AMONG_SRC_NEG; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 296 | } |
| 297 | if (optind > argc) |
| 298 | print_error("No MAC list specified\n"); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 299 | wh = create_wormhash(argv[optind - 1]); |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 300 | old_size = sizeof(struct ebt_entry_match) + |
| 301 | (**match).match_size; |
| 302 | h = malloc((new_size = |
| 303 | old_size + ebt_mac_wormhash_size(wh))); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 304 | memcpy(h, *match, old_size); |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 305 | memcpy((char *) h + old_size, wh, |
| 306 | ebt_mac_wormhash_size(wh)); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 307 | h->match_size = new_size - sizeof(struct ebt_entry_match); |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 308 | info = (struct ebt_among_info *) h->data; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 309 | if (c == AMONG_DST) { |
| 310 | check_option(flags, OPT_DST); |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 311 | info->wh_dst_ofs = |
| 312 | old_size - sizeof(struct ebt_entry_match); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 313 | } else { |
| 314 | check_option(flags, OPT_SRC); |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 315 | info->wh_src_ofs = |
| 316 | old_size - sizeof(struct ebt_entry_match); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 317 | } |
| 318 | free(*match); |
| 319 | *match = h; |
| 320 | free(wh); |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 321 | break; |
| 322 | default: |
| 323 | return 0; |
| 324 | } |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | static void final_check(const struct ebt_u_entry *entry, |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 329 | const struct ebt_entry_match *match, |
| 330 | const char *name, unsigned int hookmask, |
| 331 | unsigned int time) |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 332 | { |
| 333 | } |
| 334 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 335 | #ifdef DEBUG |
| 336 | static void wormhash_debug(const struct ebt_mac_wormhash *wh) |
| 337 | { |
| 338 | int i; |
| 339 | printf("poolsize: %d\n", wh->poolsize); |
| 340 | for (i = 0; i <= 256; i++) { |
| 341 | printf("%02x ", wh->table[i]); |
| 342 | if (i % 16 == 15) { |
| 343 | printf("\n"); |
| 344 | } |
| 345 | } |
| 346 | printf("\n"); |
| 347 | } |
| 348 | #endif /* DEBUG */ |
| 349 | |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 350 | static void wormhash_printout(const struct ebt_mac_wormhash *wh) |
| 351 | { |
| 352 | int i; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 353 | unsigned char *ip; |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 354 | for (i = 0; i < wh->poolsize; i++) { |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 355 | const struct ebt_mac_wormhash_tuple *p; |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 356 | p = (const struct ebt_mac_wormhash_tuple *)(&wh->pool[i]); |
| 357 | print_mac(((const char *) &p->cmp[0]) + 2); |
| 358 | if (p->ip) { |
| 359 | ip = (unsigned char *) &p->ip; |
| 360 | printf("=%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 361 | } |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 362 | printf(","); |
| 363 | } |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 364 | printf(" "); |
| 365 | } |
| 366 | |
| 367 | static void print(const struct ebt_u_entry *entry, |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 368 | const struct ebt_entry_match *match) |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 369 | { |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 370 | struct ebt_among_info *info = (struct ebt_among_info *)match->data; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 371 | |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 372 | if (info->wh_dst_ofs) { |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 373 | printf("--among-dst "); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 374 | if (info->bitmask && EBT_AMONG_DST_NEG) { |
| 375 | printf("! "); |
| 376 | } |
| 377 | wormhash_printout(ebt_among_wh_dst(info)); |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 378 | } |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 379 | if (info->wh_src_ofs) { |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 380 | printf("--among-src "); |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 381 | if (info->bitmask && EBT_AMONG_SRC_NEG) { |
| 382 | printf("! "); |
| 383 | } |
| 384 | wormhash_printout(ebt_among_wh_src(info)); |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 388 | static int compare_wh(const struct ebt_mac_wormhash *aw, |
| 389 | const struct ebt_mac_wormhash *bw) |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 390 | { |
| 391 | int as, bs; |
| 392 | as = ebt_mac_wormhash_size(aw); |
| 393 | bs = ebt_mac_wormhash_size(bw); |
| 394 | if (as != bs) |
| 395 | return 0; |
| 396 | if (as && memcmp(aw, bw, as)) |
| 397 | return 0; |
| 398 | return 1; |
| 399 | } |
| 400 | |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 401 | static int compare(const struct ebt_entry_match *m1, |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 402 | const struct ebt_entry_match *m2) |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 403 | { |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 404 | struct ebt_among_info *a = (struct ebt_among_info *) m1->data; |
| 405 | struct ebt_among_info *b = (struct ebt_among_info *) m2->data; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 406 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 407 | if (!compare_wh(ebt_among_wh_dst(a), ebt_among_wh_dst(b))) |
| 408 | return 0; |
| 409 | if (!compare_wh(ebt_among_wh_src(a), ebt_among_wh_src(b))) |
| 410 | return 0; |
| 411 | if (a->bitmask != b->bitmask) |
| 412 | return 0; |
gborowiak | c50ce6a | 2003-09-07 13:16:26 +0000 | [diff] [blame] | 413 | return 1; |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 414 | } |
| 415 | |
gborowiak | 6c6d731 | 2003-09-16 19:26:38 +0000 | [diff] [blame^] | 416 | static struct ebt_u_match among_match = { |
| 417 | .name = EBT_AMONG_MATCH, |
| 418 | .size = sizeof(struct ebt_among_info), |
| 419 | .help = print_help, |
| 420 | .init = init, |
| 421 | .parse = parse, |
| 422 | .final_check = final_check, |
| 423 | .print = print, |
| 424 | .compare = compare, |
| 425 | .extra_ops = opts, |
Bart De Schuymer | 9cc9bfa | 2003-09-02 22:43:25 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | static void _init(void) __attribute__ ((constructor)); |
| 429 | static void _init(void) |
| 430 | { |
| 431 | register_match(&among_match); |
| 432 | } |