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
|
/* types and functions used throughout filtergen
*
* Copyright (c) 2002 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
*/
#ifndef _FK_FILTER_H
#define _FK_FILTER_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef __GNUC__
#define _PRINTF_ATTR(s, e) __attribute__((__format__(__printf__, (s), (e))))
#else
#define _PRINTF_ATTR(s, e)
#endif
/*
* If it doesn't begin with "F_", it's a simple token, not an
* filter type
*/
enum filtertype {
F_EOF = 0, // Same as YYEOF
F_DIRECTION,
F_TARGET,
F_SOURCE,
F_DEST,
F_SPORT,
F_DPORT,
F_ICMPTYPE,
F_PROTO,
F_NEG,
F_SIBLIST,
F_SUBGROUP,
F_LOG,
F_RTYPE,
F_ONEWAY,
/* this must be the last real filter type */
F_FILTER_MAX,
/* parser use only */
INPUT,
OUTPUT,
/* lex uses "ACCEPT" and "REJECT" */
T_ACCEPT,
DROP,
T_REJECT,
MASQ,
REDIRECT,
OPENBRACE,
CLOSEBRACE,
OPENBRACKET,
CLOSEBRACKET,
SEMICOLON,
STRING,
INCLUDE,
LOCALONLY,
ROUTEDONLY, /* for F_RTYPE */
TEXT, /* for F_LOG */
};
/* Structure;s which appear in both the parse tree and the output rule */
struct proto_spec {
int num;
char *name;
};
struct addr_spec {
sa_family_t family;
union {
struct {
struct in_addr addr, mask;
} inet;
struct {
struct in6_addr addr, mask;
} inet6;
} u;
char *addrstr, *maskstr;
};
struct port_spec {
int min, max;
char *minstr, *maxstr;
};
/* This is basically just a parse tree */
struct filter {
enum filtertype type;
union {
struct {
enum filtertype direction;
char *iface;
} ifinfo;
enum filtertype target;
char *logmsg;
enum filtertype rtype;
struct addr_spec addrs;
struct port_spec ports;
char *icmp;
struct proto_spec proto;
struct filter *neg;
struct filter *sib;
struct {
char *name;
struct filter *list;
} sub;
} u;
struct filter *child, *next;
/* infernal use only */
int negate;
};
struct filtergen_opts {
sa_family_t family;
};
/* from filter.c */
typedef struct filter *filter_tctor(enum filtertype);
filter_tctor new_filter_target, new_filter_rtype;
struct filter *new_filter_neg(struct filter *sub);
struct filter *new_filter_sibs(struct filter *list);
struct filter *new_filter_subgroup(char *name, struct filter *list);
typedef struct filter *filter_ctor(enum filtertype, const char *);
filter_ctor new_filter_device, new_filter_ports, new_filter_icmp,
new_filter_proto, new_filter_log;
struct filter *new_filter_host(enum filtertype, const char *, sa_family_t);
struct filter *new_filter_oneway(void);
/* filter manipulations */
void filter_unroll(struct filter **f);
void filter_nogroup(struct filter *f);
void filter_noneg(struct filter **f);
/* from generated lexer and parer in filterlex.l */
int filter_fopen(const char *filename);
struct filter *filter_parse_list(struct filtergen_opts *o);
/* from gen.c */
#define ESET(e, f) (e->whats_set & (1 << F_##f))
struct filterent {
/* Either direction+iface or groupname must be set */
enum filtertype direction;
char *iface;
char *groupname;
/* One of these must be set */
enum filtertype target;
char *subgroup;
/* These may or may not be set */
int whats_set : F_FILTER_MAX;
int whats_negated : F_FILTER_MAX;
struct addr_spec srcaddr, dstaddr;
enum filtertype rtype;
struct proto_spec proto;
char *logmsg;
int oneway;
/* We need this not to be a union, for error-checking reasons */
struct {
struct {
struct port_spec src, dst;
} ports;
char *icmp;
} u;
};
struct fg_misc {
int flags;
void *misc;
};
typedef int fg_cb_rule(const struct filterent *ent, struct fg_misc *misc);
typedef int fg_cb_group(const char *name);
typedef struct {
fg_cb_rule *rule;
fg_cb_group *group;
} fg_callback;
int filtergen_cprod(struct filter *filter, fg_callback *cb,
struct fg_misc *misc);
/* fg-util.c */
char *strapp(char *s, const char *n);
#define strapp2(s, n1, n2) strapp(strapp(s, n1), n2)
int str_to_int(const char *s, int *i);
char *int_to_str_dup(int i);
/* various drivers */
typedef int filtergen(struct filter *filter, int flags);
filtergen fg_iptables, fg_ip6tables, fg_iptrestore, fg_ip6trestore, fg_ipchains,
fg_ipfilter, fg_cisco;
typedef int filter_flush(enum filtertype policy);
filter_flush flush_iptables, flush_ip6tables, flush_iptrestore,
flush_ip6trestore, flush_ipchains;
/* ("flags" arguments) */
enum flags {
FF_NOSKEL = (1 << 0), /* omit any "skeleton" rules */
FF_LSTATE = (1 << 1), /* lightweight state matching */
FF_LOCAL = (1 << 2), /* assume packets are local only */
FF_ROUTE = (1 << 3), /* assume packets are forwarded */
FF_NORESOLVE = (1 << 4), /* don't resolve hostnames, ports, or services */
FF_FLUSH = (1 << 5), /* just flush the ruleset instead */
};
/* filtergen.c */
int oputs(const char *s);
int oprintf(const char *fmt, ...) _PRINTF_ATTR(1, 2);
#endif /* _FK_FILTER_H */
|