Home | History | Annotate | Line # | Download | only in pfctl
pfctl_parser.h revision 1.1.1.2
      1 /*	$OpenBSD: pfctl_parser.h,v 1.77 2004/07/16 23:44:25 frantzen Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001 Daniel Hartmeier
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  *
     11  *    - Redistributions of source code must retain the above copyright
     12  *      notice, this list of conditions and the following disclaimer.
     13  *    - Redistributions in binary form must reproduce the above
     14  *      copyright notice, this list of conditions and the following
     15  *      disclaimer in the documentation and/or other materials provided
     16  *      with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  */
     32 
     33 #ifndef _PFCTL_PARSER_H_
     34 #define _PFCTL_PARSER_H_
     35 
     36 #define PF_OSFP_FILE		"/etc/pf.os"
     37 
     38 #define PF_OPT_DISABLE		0x0001
     39 #define PF_OPT_ENABLE		0x0002
     40 #define PF_OPT_VERBOSE		0x0004
     41 #define PF_OPT_NOACTION		0x0008
     42 #define PF_OPT_QUIET		0x0010
     43 #define PF_OPT_CLRRULECTRS	0x0020
     44 #define PF_OPT_USEDNS		0x0040
     45 #define PF_OPT_VERBOSE2		0x0080
     46 #define PF_OPT_DUMMYACTION	0x0100
     47 #define PF_OPT_DEBUG		0x0200
     48 #define PF_OPT_SHOWALL		0x0400
     49 #define PF_OPT_OPTIMIZE		0x0800
     50 #define PF_OPT_OPTIMIZE_PROFILE	0x1000
     51 
     52 #define PF_TH_ALL		0xFF
     53 
     54 #define PF_NAT_PROXY_PORT_LOW	50001
     55 #define PF_NAT_PROXY_PORT_HIGH	65535
     56 
     57 #define FCNT_NAMES { \
     58 	"searches", \
     59 	"inserts", \
     60 	"removals", \
     61 	NULL \
     62 }
     63 
     64 struct pfr_buffer;	/* forward definition */
     65 struct pf_opt_rule;
     66 TAILQ_HEAD(pf_opt_queue, pf_opt_rule);
     67 
     68 
     69 struct pfctl {
     70 	int dev;
     71 	int opts;
     72 	int loadopt;
     73 	u_int32_t tticket;		/* table ticket */
     74 	int tdirty;			/* kernel dirty */
     75 	u_int32_t rule_nr;
     76 	struct pfioc_pooladdr paddr;
     77 	struct pfioc_altq *paltq;
     78 	struct pfioc_queue *pqueue;
     79 	struct pfr_buffer *trans;
     80 	const char *anchor;
     81 	const char *ruleset;
     82 	struct pf_opt_queue opt_queue;
     83 };
     84 
     85 struct node_if {
     86 	char			 ifname[IFNAMSIZ];
     87 	u_int8_t		 not;
     88 	u_int8_t		 dynamic; /* antispoof */
     89 	u_int			 ifa_flags;
     90 	struct node_if		*next;
     91 	struct node_if		*tail;
     92 };
     93 
     94 struct node_host {
     95 	struct pf_addr_wrap	 addr;
     96 	struct pf_addr		 bcast;
     97 	struct pf_addr		 peer;
     98 	sa_family_t		 af;
     99 	u_int8_t		 not;
    100 	u_int32_t		 ifindex;	/* link-local IPv6 addrs */
    101 	char			*ifname;
    102 	u_int			 ifa_flags;
    103 	struct node_host	*next;
    104 	struct node_host	*tail;
    105 };
    106 /* special flags used by ifa_exists */
    107 #define PF_IFA_FLAG_GROUP	0x10000
    108 #define PF_IFA_FLAG_DYNAMIC	0x20000
    109 #define PF_IFA_FLAG_CLONABLE	0x40000
    110 
    111 struct node_os {
    112 	char			*os;
    113 	pf_osfp_t		 fingerprint;
    114 	struct node_os		*next;
    115 	struct node_os		*tail;
    116 };
    117 
    118 struct node_queue_bw {
    119 	u_int32_t	bw_absolute;
    120 	u_int16_t	bw_percent;
    121 };
    122 
    123 struct node_hfsc_sc {
    124 	struct node_queue_bw	m1;	/* slope of 1st segment; bps */
    125 	u_int			d;	/* x-projection of m1; msec */
    126 	struct node_queue_bw	m2;	/* slope of 2nd segment; bps */
    127 	u_int8_t		used;
    128 };
    129 
    130 struct node_hfsc_opts {
    131 	struct node_hfsc_sc	realtime;
    132 	struct node_hfsc_sc	linkshare;
    133 	struct node_hfsc_sc	upperlimit;
    134 	int			flags;
    135 };
    136 
    137 struct node_queue_opt {
    138 	int			 qtype;
    139 	union {
    140 		struct cbq_opts		cbq_opts;
    141 		struct priq_opts	priq_opts;
    142 		struct node_hfsc_opts	hfsc_opts;
    143 	}			 data;
    144 };
    145 
    146 SIMPLEQ_HEAD(node_tinithead, node_tinit);
    147 struct node_tinit {	/* table initializer */
    148 	SIMPLEQ_ENTRY(node_tinit)	 entries;
    149 	struct node_host		*host;
    150 	char				*file;
    151 };
    152 
    153 
    154 /* optimizer created tables */
    155 struct pf_opt_tbl {
    156 	char			 pt_name[PF_TABLE_NAME_SIZE];
    157 	int			 pt_rulecount;
    158 	int			 pt_generated;
    159 	struct node_tinithead	 pt_nodes;
    160 	struct pfr_buffer	*pt_buf;
    161 };
    162 #define PF_OPT_TABLE_PREFIX	"__automatic_"
    163 
    164 /* optimizer pf_rule container */
    165 struct pf_opt_rule {
    166 	struct pf_rule		 por_rule;
    167 	struct pf_opt_tbl	*por_src_tbl;
    168 	struct pf_opt_tbl	*por_dst_tbl;
    169 	char			 por_anchor[MAXPATHLEN];
    170 	u_int64_t		 por_profile_count;
    171 	TAILQ_ENTRY(pf_opt_rule) por_entry;
    172 	TAILQ_ENTRY(pf_opt_rule) por_skip_entry[PF_SKIP_COUNT];
    173 };
    174 
    175 
    176 int	pfctl_rules(int, char *, int, char *, struct pfr_buffer *);
    177 int	pfctl_optimize_rules(struct pfctl *);
    178 
    179 int	pfctl_add_rule(struct pfctl *, struct pf_rule *, const char *);
    180 int	pfctl_add_altq(struct pfctl *, struct pf_altq *);
    181 int	pfctl_add_pool(struct pfctl *, struct pf_pool *, sa_family_t);
    182 void	pfctl_clear_pool(struct pf_pool *);
    183 
    184 int	pfctl_set_timeout(struct pfctl *, const char *, int, int);
    185 int	pfctl_set_optimization(struct pfctl *, const char *);
    186 int	pfctl_set_limit(struct pfctl *, const char *, unsigned int);
    187 int	pfctl_set_logif(struct pfctl *, char *);
    188 int	pfctl_set_hostid(struct pfctl *, u_int32_t);
    189 int	pfctl_set_debug(struct pfctl *, char *);
    190 
    191 int	parse_rules(FILE *, struct pfctl *);
    192 int	parse_flags(char *);
    193 int	pfctl_load_anchors(int, int, struct pfr_buffer *);
    194 
    195 void	print_pool(struct pf_pool *, u_int16_t, u_int16_t, sa_family_t, int);
    196 void	print_src_node(struct pf_src_node *, int);
    197 void	print_rule(struct pf_rule *, const char *, int);
    198 void	print_tabledef(const char *, int, int, struct node_tinithead *);
    199 void	print_status(struct pf_status *, int);
    200 
    201 int	eval_pfaltq(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
    202 	    struct node_queue_opt *);
    203 int	eval_pfqueue(struct pfctl *, struct pf_altq *, struct node_queue_bw *,
    204 	    struct node_queue_opt *);
    205 
    206 void	 print_altq(const struct pf_altq *, unsigned, struct node_queue_bw *,
    207 	    struct node_queue_opt *);
    208 void	 print_queue(const struct pf_altq *, unsigned, struct node_queue_bw *,
    209 	    int, struct node_queue_opt *);
    210 
    211 int	pfctl_define_table(char *, int, int, const char *, struct pfr_buffer *,
    212 	    u_int32_t);
    213 
    214 void		 pfctl_clear_fingerprints(int, int);
    215 int		 pfctl_file_fingerprints(int, int, const char *);
    216 pf_osfp_t	 pfctl_get_fingerprint(const char *);
    217 int		 pfctl_load_fingerprints(int, int);
    218 char		*pfctl_lookup_fingerprint(pf_osfp_t, char *, size_t);
    219 void		 pfctl_show_fingerprints(int);
    220 
    221 
    222 struct icmptypeent {
    223 	const char *name;
    224 	u_int8_t type;
    225 };
    226 
    227 struct icmpcodeent {
    228 	const char *name;
    229 	u_int8_t type;
    230 	u_int8_t code;
    231 };
    232 
    233 const struct icmptypeent *geticmptypebynumber(u_int8_t, u_int8_t);
    234 const struct icmptypeent *geticmptypebyname(char *, u_int8_t);
    235 const struct icmpcodeent *geticmpcodebynumber(u_int8_t, u_int8_t, u_int8_t);
    236 const struct icmpcodeent *geticmpcodebyname(u_long, char *, u_int8_t);
    237 
    238 struct pf_timeout {
    239 	const char	*name;
    240 	int		 timeout;
    241 };
    242 
    243 #define PFCTL_FLAG_FILTER	0x02
    244 #define PFCTL_FLAG_NAT		0x04
    245 #define PFCTL_FLAG_OPTION	0x08
    246 #define PFCTL_FLAG_ALTQ		0x10
    247 #define PFCTL_FLAG_TABLE	0x20
    248 
    249 extern const struct pf_timeout pf_timeouts[];
    250 
    251 void			 set_ipmask(struct node_host *, u_int8_t);
    252 int			 check_netmask(struct node_host *, sa_family_t);
    253 int			 unmask(struct pf_addr *, sa_family_t);
    254 void			 ifa_load(void);
    255 struct node_host	*ifa_exists(const char *, int);
    256 struct node_host	*ifa_lookup(const char *, int);
    257 struct node_host	*host(const char *);
    258 
    259 int			 append_addr(struct pfr_buffer *, char *, int);
    260 int			 append_addr_host(struct pfr_buffer *,
    261 			    struct node_host *, int, int);
    262 
    263 #endif /* _PFCTL_PARSER_H_ */
    264