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