Home | History | Annotate | Line # | Download | only in npfctl
npfctl.h revision 1.19
      1 /*	$NetBSD: npfctl.h,v 1.19 2012/08/12 03:35:13 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 #include <assert.h>
     36 
     37 #include <net/npf_ncode.h>
     38 #include <net/npf.h>
     39 
     40 #define	_NPF_PRIVATE
     41 #include <npf.h>
     42 
     43 #include "npf_var.h"
     44 
     45 #define	NPF_DEV_PATH	"/dev/npf"
     46 #define	NPF_CONF_PATH	"/etc/npf.conf"
     47 #define	NPF_SESSDB_PATH	"/var/db/npf_sessions.db"
     48 
     49 typedef struct fam_addr_mask {
     50 	sa_family_t	fam_family;
     51 	npf_addr_t	fam_addr;
     52 	npf_netmask_t	fam_mask;
     53 	npfvar_t *	fam_interface;
     54 } fam_addr_mask_t;
     55 
     56 typedef struct port_range {
     57 	in_port_t	pr_start;
     58 	in_port_t	pr_end;
     59 } port_range_t;
     60 
     61 typedef struct addr_port {
     62 	npfvar_t *	ap_netaddr;
     63 	npfvar_t *	ap_portrange;
     64 } addr_port_t;
     65 
     66 typedef struct filt_opts {
     67 	addr_port_t	fo_from;
     68 	addr_port_t	fo_to;
     69 } filt_opts_t;
     70 
     71 typedef struct opt_proto {
     72 	int		op_proto;
     73 	npfvar_t *	op_opts;
     74 } opt_proto_t;
     75 
     76 typedef struct rule_group {
     77 	const char *	rg_name;
     78 	uint32_t	rg_attr;
     79 	u_int		rg_ifnum;
     80 } rule_group_t;
     81 
     82 typedef struct proc_op {
     83 	const char *	po_name;
     84 	npfvar_t *	po_opts;
     85 } proc_op_t;
     86 
     87 typedef struct module_arg {
     88 	const char *	ma_name;
     89 	npfvar_t *	ma_opts;
     90 } module_arg_t;
     91 
     92 void		yyerror(const char *, ...) __printflike(1, 2) __dead;
     93 void *		zalloc(size_t);
     94 void *		xrealloc(void *, size_t);
     95 char *		xstrdup(const char *);
     96 char *		xstrndup(const char *, size_t);
     97 
     98 void		npfctl_print_error(const nl_error_t *);
     99 bool		npfctl_table_exists_p(const char *);
    100 int		npfctl_protono(const char *);
    101 in_port_t	npfctl_portno(const char *);
    102 uint8_t		npfctl_icmpcode(int, uint8_t, const char *);
    103 uint8_t		npfctl_icmptype(int, const char *);
    104 unsigned long   npfctl_find_ifindex(const char *);
    105 npfvar_t *	npfctl_parse_tcpflag(const char *);
    106 npfvar_t *	npfctl_parse_table_id(const char *);
    107 npfvar_t * 	npfctl_parse_icmp(int, 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 bool		npfctl_parse_cidr(char *, fam_addr_mask_t *, int *);
    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 #define	NC_MATCH_ICMP6		0x20
    128 
    129 nc_ctx_t *	npfctl_ncgen_create(void);
    130 void *		npfctl_ncgen_complete(nc_ctx_t *, size_t *);
    131 void		npfctl_ncgen_print(const void *, size_t);
    132 
    133 void		npfctl_ncgen_group(nc_ctx_t *);
    134 void		npfctl_ncgen_endgroup(nc_ctx_t *);
    135 
    136 void		npfctl_gennc_v4cidr(nc_ctx_t *, int, const npf_addr_t *,
    137 		    const npf_netmask_t);
    138 void		npfctl_gennc_v6cidr(nc_ctx_t *, int, const npf_addr_t *,
    139 		    const npf_netmask_t);
    140 void		npfctl_gennc_ports(nc_ctx_t *, int, in_port_t, in_port_t);
    141 void		npfctl_gennc_icmp(nc_ctx_t *, int, int);
    142 void		npfctl_gennc_icmp6(nc_ctx_t *, int, int);
    143 void		npfctl_gennc_tbl(nc_ctx_t *, int, u_int);
    144 void		npfctl_gennc_tcpfl(nc_ctx_t *, uint8_t, uint8_t);
    145 void		npfctl_gennc_proto(nc_ctx_t *ctx, uint8_t, uint8_t);
    146 
    147 /*
    148  * N-code disassembler.
    149  */
    150 
    151 typedef struct nc_inf nc_inf_t;
    152 
    153 nc_inf_t *	npfctl_ncode_disinf(FILE *);
    154 int		npfctl_ncode_disassemble(nc_inf_t *, const void *, size_t);
    155 
    156 /*
    157  * Configuration building interface.
    158  */
    159 
    160 #define	NPFCTL_NAT_DYNAMIC	1
    161 #define	NPFCTL_NAT_STATIC	2
    162 
    163 void		npfctl_config_init(bool);
    164 int		npfctl_config_send(int, const char *);
    165 int		npfctl_config_show(int);
    166 unsigned long	npfctl_debug_addif(const char *);
    167 
    168 void		npfctl_build_rproc(const char *, npfvar_t *);
    169 void		npfctl_build_group(const char *, int, u_int);
    170 void		npfctl_build_rule(int, u_int, sa_family_t,
    171 		    const opt_proto_t *, const filt_opts_t *, const char *);
    172 void		npfctl_build_natseg(int, int, u_int, const addr_port_t *,
    173 		    const addr_port_t *, const filt_opts_t *);
    174 void		npfctl_build_table(const char *, u_int, const char *);
    175 
    176 #endif
    177