Home | History | Annotate | Line # | Download | only in npfctl
npfctl.h revision 1.11.2.1
      1 /*	$NetBSD: npfctl.h,v 1.11.2.1 2012/04/03 17:22:54 riz Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
      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  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef _NPFCTL_H_
     30 #define _NPFCTL_H_
     31 
     32 #include <stdio.h>
     33 #include <stdbool.h>
     34 #include <inttypes.h>
     35 
     36 #include <net/npf_ncode.h>
     37 #include <net/npf.h>
     38 
     39 #define	_NPF_PRIVATE
     40 #include <npf.h>
     41 
     42 #include "npf_var.h"
     43 
     44 #define	NPF_DEV_PATH	"/dev/npf"
     45 #define	NPF_CONF_PATH	"/etc/npf.conf"
     46 #define	NPF_SESSDB_PATH	"/var/db/npf_sessions.db"
     47 
     48 typedef struct fam_addr_mask {
     49 	sa_family_t	fam_family;
     50 	npf_addr_t	fam_addr;
     51 	npf_netmask_t	fam_mask;
     52 	npfvar_t *	fam_interface;
     53 } fam_addr_mask_t;
     54 
     55 typedef struct port_range {
     56 	in_port_t	pr_start;
     57 	in_port_t	pr_end;
     58 } port_range_t;
     59 
     60 typedef struct filt_opts {
     61 	npfvar_t *	fo_from;
     62 	npfvar_t *	fo_from_port_range;
     63 	npfvar_t *	fo_to;
     64 	npfvar_t *	fo_to_port_range;
     65 } filt_opts_t;
     66 
     67 typedef struct opt_proto {
     68 	int		op_proto;
     69 	npfvar_t *	op_opts;
     70 } opt_proto_t;
     71 
     72 typedef struct rule_group {
     73 	const char *	rg_name;
     74 	int		rg_attr;
     75 	u_int		rg_ifnum;
     76 } rule_group_t;
     77 
     78 typedef struct proc_op {
     79 	const char *	po_name;
     80 	npfvar_t *	po_opts;
     81 } proc_op_t;
     82 
     83 typedef struct module_arg {
     84 	const char *	ma_name;
     85 	npfvar_t *	ma_opts;
     86 } module_arg_t;
     87 
     88 void		yyerror(const char *, ...) __printflike(1, 2) __dead;
     89 void *		zalloc(size_t);
     90 void *		xrealloc(void *, size_t);
     91 char *		xstrdup(const char *);
     92 char *		xstrndup(const char *, size_t);
     93 
     94 void		npfctl_print_error(const nl_error_t *);
     95 bool		npfctl_table_exists_p(const char *);
     96 in_port_t	npfctl_portno(const char *);
     97 uint8_t		npfctl_icmpcode(uint8_t, const char *);
     98 uint8_t		npfctl_icmptype(const char *);
     99 unsigned long   npfctl_find_ifindex(const char *);
    100 npfvar_t *	npfctl_parse_tcpflag(const char *);
    101 npfvar_t *	npfctl_parse_table_id(const char *);
    102 npfvar_t * 	npfctl_parse_icmpcode(const char *);
    103 npfvar_t * 	npfctl_parse_icmp(uint8_t, uint8_t);
    104 npfvar_t *	npfctl_parse_iface(const char *);
    105 npfvar_t *	npfctl_parse_port_range(in_port_t, in_port_t);
    106 npfvar_t *	npfctl_parse_fam_addr_mask(const char *, const char *,
    107 		    unsigned long *);
    108 fam_addr_mask_t *npfctl_parse_cidr(char *);
    109 
    110 /*
    111  * N-code generation interface.
    112  */
    113 
    114 typedef struct nc_ctx nc_ctx_t;
    115 
    116 #define	NC_MATCH_DST		0x01
    117 #define	NC_MATCH_SRC		0x02
    118 
    119 #define	NC_MATCH_TCP		0x04
    120 #define	NC_MATCH_UDP		0x08
    121 
    122 nc_ctx_t *	npfctl_ncgen_create(void);
    123 void *		npfctl_ncgen_complete(nc_ctx_t *, size_t *);
    124 void		npfctl_ncgen_print(const void *, size_t);
    125 
    126 void		npfctl_ncgen_group(nc_ctx_t *);
    127 void		npfctl_ncgen_endgroup(nc_ctx_t *);
    128 
    129 void		npfctl_gennc_v4cidr(nc_ctx_t *, int, const npf_addr_t *,
    130 		    const npf_netmask_t);
    131 void		npfctl_gennc_v6cidr(nc_ctx_t *, int, const npf_addr_t *,
    132 		    const npf_netmask_t);
    133 void		npfctl_gennc_ports(nc_ctx_t *, int, in_port_t, in_port_t);
    134 void		npfctl_gennc_icmp(nc_ctx_t *, int, int);
    135 void		npfctl_gennc_tbl(nc_ctx_t *, int, u_int);
    136 void		npfctl_gennc_tcpfl(nc_ctx_t *, uint8_t, uint8_t);
    137 
    138 /*
    139  * Configuration building interface.
    140  */
    141 
    142 #define	NPFCTL_RDR		1
    143 #define	NPFCTL_BINAT		2
    144 #define	NPFCTL_NAT		3
    145 
    146 void		npfctl_config_init(bool);
    147 int		npfctl_config_send(int);
    148 
    149 void		npfctl_build_rproc(const char *, npfvar_t *);
    150 void		npfctl_build_group(const char *, int, u_int);
    151 void		npfctl_build_rule(int, u_int, sa_family_t,
    152 		    const opt_proto_t *, const filt_opts_t *, const char *);
    153 void		npfctl_build_nat(int, u_int, const filt_opts_t *,
    154 		    npfvar_t *, npfvar_t *);
    155 void		npfctl_build_table(const char *, u_int, const char *);
    156 int		npfctl_ncode_disassemble(FILE *, const void *, size_t);
    157 
    158 #endif
    159