1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
/* data structure building routines
*
* Copyright (c) 2002,2003 Matthew Kirkwood
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "filter.h"
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
static struct filter *__new_filter(enum filtertype type) {
struct filter *f;
if ((f = calloc(1, sizeof(*f)))) {
f->type = type;
}
return f;
}
struct filter *new_filter_oneway(void) {
return __new_filter(F_ONEWAY);
}
struct filter *new_filter_target(enum filtertype target) {
struct filter *f;
if ((f = __new_filter(F_TARGET))) {
f->u.target = target;
}
return f;
}
struct filter *new_filter_log(enum filtertype type, const char *text) {
struct filter *f;
if ((f = __new_filter(type))) {
f->u.logmsg = text ? strdup(text) : NULL;
}
return f;
}
struct filter *new_filter_rtype(enum filtertype rtype) {
struct filter *f;
if ((f = __new_filter(F_RTYPE))) {
f->u.rtype = rtype;
}
return f;
}
struct filter *new_filter_neg(struct filter *sub) {
struct filter *f;
if ((f = __new_filter(F_NEG))) {
f->u.neg = sub;
}
return f;
}
struct filter *new_filter_sibs(struct filter *list) {
struct filter *f;
if ((f = __new_filter(F_SIBLIST))) {
f->u.sib = list;
}
return f;
}
struct filter *new_filter_subgroup(char *name, struct filter *list) {
struct filter *f;
if ((f = __new_filter(F_SUBGROUP))) {
f->u.sub.name = name;
f->u.sub.list = list;
}
return f;
}
struct filter *new_filter_proto(enum filtertype type, const char *name) {
struct filter *f;
struct protoent *e;
int pn;
if (!str_to_int(name, &pn))
e = getprotobynumber(pn);
else
e = getprotobyname(name);
if (!e) {
fprintf(stderr, "don't know protocol \"%s\"\n", name);
return NULL;
}
if ((f = __new_filter(type))) {
f->u.proto.num = e->p_proto;
f->u.proto.name = strdup(e->p_name);
}
return f;
}
struct filter *new_filter_device(enum filtertype type, const char *iface) {
struct filter *f;
if ((f = __new_filter(F_DIRECTION))) {
f->u.ifinfo.direction = type;
f->u.ifinfo.iface = strdup(iface);
}
return f;
}
struct filter *new_filter_host(enum filtertype type, const char *matchstr,
sa_family_t family) {
struct filter *f;
char *mask;
int i;
if (!(f = __new_filter(type)))
return f;
f->u.addrs.family = family;
f->u.addrs.addrstr = strdup(matchstr);
if ((mask = strchr(f->u.addrs.addrstr, '/'))) {
*mask++ = 0;
switch (family) {
case AF_INET:
if (!str_to_int(mask, &i)) {
/* Netmask like foo/24 */
uint32_t l = 0xffffffff;
if (i < 0 || i > 32) {
fprintf(stderr, "can't parse netmask \"%s\"\n", mask);
return NULL;
}
if (!i)
l = 0;
else {
i = 32 - i;
l >>= i;
l <<= i;
}
f->u.addrs.u.inet.mask.s_addr = htonl(l);
} else {
/* Better be a /255.255.255.0 mask */
if (!inet_aton(mask, &f->u.addrs.u.inet.mask)) {
fprintf(stderr, "can't parse netmask \"%s\"\n", mask);
return NULL;
}
}
f->u.addrs.maskstr = strdup(inet_ntoa(f->u.addrs.u.inet.mask));
break;
case AF_INET6:
if (!str_to_int(mask, &i)) {
/* Netmask like foo/128 */
unsigned char l[16] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char *p = l + 15;
int o;
if (i < 0 || i > 128) {
fprintf(stderr, "can't parse netmask \"%s\"\n", mask);
return NULL;
}
if (i != 0) {
o = 128 - i;
while (o > 8) {
*p = 0x00;
o -= 8;
}
if (!i)
*p = 0x00;
else {
*p >>= o;
*p <<= o;
}
}
memcpy(f->u.addrs.u.inet6.mask.s6_addr, l, sizeof l);
f->u.addrs.maskstr = int_to_str_dup(i);
} else {
fprintf(stderr, "can't parse netmask \"%s\"\n", mask);
return NULL;
}
break;
default:
fprintf(stderr,
"can't parse netmask \"%s\" for invalid address family %d\n",
mask, family);
return NULL;
}
}
return f;
}
struct filter *new_filter_ports(enum filtertype type, const char *matchstr) {
struct filter *f;
struct servent *s;
char *min, *max;
int imin, imax;
min = strdup(matchstr);
if ((max = strchr(min, ':'))) {
*max++ = 0;
max = strdup(max);
}
if (str_to_int(min, &imin)) {
if (!(s = getservbyname(min, NULL)))
imin = -1;
else {
free(min);
min = strdup(s->s_name);
imin = ntohs(s->s_port);
}
}
if (max) {
if (str_to_int(max, &imax)) {
if (!(s = getservbyname(max, NULL)))
imax = -1;
else {
free(max);
max = strdup(s->s_name);
imax = ntohs(s->s_port);
}
}
} else
imax = imin;
if ((f = __new_filter(type))) {
f->u.ports.min = imin;
f->u.ports.max = imax;
f->u.ports.minstr = min;
f->u.ports.maxstr = max;
}
return f;
}
struct filter *new_filter_icmp(enum filtertype type, const char *matchstr) {
struct filter *f;
if ((f = __new_filter(type))) {
f->u.icmp = strdup(matchstr);
}
return f;
}
/*
* These functions DAGify the parsed filter structure.
* This means that we can walk all paths down the DAG
* merely by following ->child and ->next.
*/
static void filter_append(struct filter *f, struct filter *x) {
if (!f)
abort();
if (!x)
return;
/* We have to be paranoid about making loops here */
while ((f->type != F_SIBLIST) && f->child) {
if (f == x)
return;
f = f->child;
}
if (f == x)
return;
if (f->type == F_SIBLIST) {
if (f->child)
abort();
for (f = f->u.sib; f; f = f->next)
filter_append(f, x);
} else
f->child = x;
}
/*
* The easy bit of a cross-product. Basically we ensure that no
* filter node has more than one path out.
* 1. We push sibling->child down to the end of the component
* sub-lists, and
* 2. Ensure that negated entries have only a single component
* hanging off them.
*/
void __filter_unroll(struct filter *f) {
struct filter *c, *s;
if (!f)
return;
/* depth first */
__filter_unroll(c = f->child);
/* check this node */
switch (f->type) {
case F_SIBLIST:
for (s = f->u.sib; s; s = s->next) {
__filter_unroll(s);
filter_append(s, c);
}
f->child = NULL;
break;
case F_SUBGROUP:
__filter_unroll(f->u.sub.list);
break;
case F_NEG:
abort();
default:
break;
}
/* lastly, go sideways */
__filter_unroll(f->next);
}
void __filter_neg_expand(struct filter **f, int neg) {
if (!*f)
return;
__filter_neg_expand(&(*f)->child, neg);
__filter_neg_expand(&(*f)->next, neg);
switch ((*f)->type) {
case F_SIBLIST:
if (neg && (*f)->u.sib && (*f)->u.sib->next) {
fprintf(stderr, "error: can't negate conjunctions\n");
exit(1);
}
__filter_neg_expand(&(*f)->u.sib, neg);
break;
case F_SUBGROUP:
if (neg) {
fprintf(stderr, "error: can't negate subgroups\n");
exit(1);
}
__filter_neg_expand(&(*f)->u.sub.list, neg);
break;
case F_NEG: {
struct filter *c = (*f)->child, *n = (*f)->next;
*f = (*f)->u.neg;
neg = !neg;
__filter_neg_expand(f, neg);
if (c)
filter_append(*f, c);
(*f)->next = n;
break;
}
default:
break;
}
(*f)->negate = neg;
}
/* Move targets to end of each list */
void __filter_targets_to_end(struct filter **f) {
if (!*f)
return;
if (((*f)->type == F_TARGET) && (*f)->child) {
struct filter *c = (*f)->child;
/* Unlink this one */
(*f)->child = NULL;
/* Append ourselves */
filter_append(c, (*f));
/* Tell them upstairs */
*f = c;
} else {
__filter_targets_to_end(&(*f)->child);
__filter_targets_to_end(&(*f)->next);
}
}
void filter_unroll(struct filter **f) {
__filter_neg_expand(f, 0);
__filter_targets_to_end(f);
__filter_unroll(*f);
}
void filter_nogroup(struct filter *f) {
if (!f)
return;
switch (f->type) {
case F_SUBGROUP:
f->u.sib = f->u.sub.list;
f->type = F_SIBLIST;
/* fall through */
case F_SIBLIST:
filter_nogroup(f->u.sib);
break;
default:
;
}
filter_nogroup(f->child);
filter_nogroup(f->next);
}
/* Remove negations by reordering tree:
* NEG(ent)->child,next
* becomes
* ent->next,child,next
*/
void filter_noneg(struct filter **f) { if (f) { };
return;
}
|