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