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