Home | History | Annotate | Line # | Download | only in npfctl
npfctl.h revision 1.16
      1 /*	$NetBSD: npfctl.h,v 1.16 2012/07/01 23:21:07 rmind 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 addr_port {
     61 	npfvar_t *	ap_netaddr;
     62 	npfvar_t *	ap_portrange;
     63 } addr_port_t;
     64 
     65 typedef struct filt_opts {
     66 	addr_port_t	fo_from;
     67 	addr_port_t	fo_to;
     68 } filt_opts_t;
     69 
     70 typedef struct opt_proto {
     71 	int		op_proto;
     72 	npfvar_t *	op_opts;
     73 } opt_proto_t;
     74 
     75 typedef struct rule_group {
     76 	const char *	rg_name;
     77 	uint32_t	rg_attr;
     78 	u_int		rg_ifnum;
     79 } rule_group_t;
     80 
     81 typedef struct proc_op {
     82 	const char *	po_name;
     83 	npfvar_t *	po_opts;
     84 } proc_op_t;
     85 
     86 typedef struct module_arg {
     87 	const char *	ma_name;
     88 	npfvar_t *	ma_opts;
     89 } module_arg_t;
     90 
     91 void		yyerror(const char *, ...) __printflike(1, 2) __dead;
     92 void *		zalloc(size_t);
     93 void *		xrealloc(void *, size_t);
     94 char *		xstrdup(const char *);
     95 char *		xstrndup(const char *, size_t);
     96 
     97 void		npfctl_print_error(const nl_error_t *);
     98 bool		npfctl_table_exists_p(const char *);
     99 int		npfctl_protono(const char *);
    100 in_port_t	npfctl_portno(const char *);
    101 uint8_t		npfctl_icmpcode(uint8_t, const char *);
    102 uint8_t		npfctl_icmptype(const char *);
    103 unsigned long   npfctl_find_ifindex(const char *);
    104 npfvar_t *	npfctl_parse_tcpflag(const char *);
    105 npfvar_t *	npfctl_parse_table_id(const char *);
    106 npfvar_t * 	npfctl_parse_icmpcode(const char *);
    107 npfvar_t * 	npfctl_parse_icmp(int, int);
    108 npfvar_t *	npfctl_parse_iface(const char *);
    109 npfvar_t *	npfctl_parse_port_range(in_port_t, in_port_t);
    110 npfvar_t *	npfctl_parse_port_range_variable(const char *);
    111 npfvar_t *	npfctl_parse_fam_addr_mask(const char *, const char *,
    112 		    unsigned long *);
    113 fam_addr_mask_t *npfctl_parse_cidr(char *);
    114 
    115 /*
    116  * N-code generation interface.
    117  */
    118 
    119 typedef struct nc_ctx nc_ctx_t;
    120 
    121 #define	NC_MATCH_DST		0x01
    122 #define	NC_MATCH_SRC		0x02
    123 
    124 #define	NC_MATCH_TCP		0x04
    125 #define	NC_MATCH_UDP		0x08
    126 #define	NC_MATCH_ICMP		0x10
    127 
    128 nc_ctx_t *	npfctl_ncgen_create(void);
    129 void *		npfctl_ncgen_complete(nc_ctx_t *, size_t *);
    130 void		npfctl_ncgen_print(const void *, size_t);
    131 
    132 void		npfctl_ncgen_group(nc_ctx_t *);
    133 void		npfctl_ncgen_endgroup(nc_ctx_t *);
    134 
    135 void		npfctl_gennc_v4cidr(nc_ctx_t *, int, const npf_addr_t *,
    136 		    const npf_netmask_t);
    137 void		npfctl_gennc_v6cidr(nc_ctx_t *, int, const npf_addr_t *,
    138 		    const npf_netmask_t);
    139 void		npfctl_gennc_ports(nc_ctx_t *, int, in_port_t, in_port_t);
    140 void		npfctl_gennc_icmp(nc_ctx_t *, int, int);
    141 void		npfctl_gennc_tbl(nc_ctx_t *, int, u_int);
    142 void		npfctl_gennc_tcpfl(nc_ctx_t *, uint8_t, uint8_t);
    143 void		npfctl_gennc_proto(nc_ctx_t *ctx, uint8_t, uint8_t);
    144 
    145 /*
    146  * N-code disassembler.
    147  */
    148 
    149 typedef struct nc_inf nc_inf_t;
    150 
    151 nc_inf_t *	npfctl_ncode_disinf(FILE *);
    152 int		npfctl_ncode_disassemble(nc_inf_t *, const void *, size_t);
    153 
    154 /*
    155  * Configuration building interface.
    156  */
    157 
    158 #define	NPFCTL_NAT_DYNAMIC	1
    159 #define	NPFCTL_NAT_STATIC	2
    160 
    161 void		npfctl_config_init(bool);
    162 int		npfctl_config_send(int);
    163 int		npfctl_config_show(int);
    164 
    165 void		npfctl_build_rproc(const char *, npfvar_t *);
    166 void		npfctl_build_group(const char *, int, u_int);
    167 void		npfctl_build_rule(int, u_int, sa_family_t,
    168 		    const opt_proto_t *, const filt_opts_t *, const char *);
    169 void		npfctl_build_nat(int, int, u_int, const addr_port_t *,
    170 		    const addr_port_t *, const filt_opts_t *);
    171 void		npfctl_build_table(const char *, u_int, const char *);
    172 
    173 #endif
    174