Home | History | Annotate | Line # | Download | only in npfctl
npfctl.h revision 1.11.2.11
      1 /*	$NetBSD: npfctl.h,v 1.11.2.11 2012/12/16 19:41:37 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 #include <assert.h>
     36 #include <util.h>
     37 #undef ECHO /* XXX util.h */
     38 
     39 #include <net/npf_ncode.h>
     40 #include <net/npf.h>
     41 
     42 #define	_NPF_PRIVATE
     43 #include <npf.h>
     44 
     45 #include "npf_var.h"
     46 
     47 #define	NPF_DEV_PATH	"/dev/npf"
     48 #define	NPF_CONF_PATH	"/etc/npf.conf"
     49 #define	NPF_SESSDB_PATH	"/var/db/npf_sessions.db"
     50 
     51 typedef struct fam_addr_mask {
     52 	sa_family_t	fam_family;
     53 	npf_addr_t	fam_addr;
     54 	npf_netmask_t	fam_mask;
     55 	unsigned long	fam_ifindex;
     56 } fam_addr_mask_t;
     57 
     58 typedef struct ifnet_addr {
     59 	unsigned long	ifna_index;
     60 	sa_family_t	ifna_family;
     61 	npfvar_t *	ifna_filter;
     62 	npfvar_t *	ifna_addrs;
     63 } ifnet_addr_t;
     64 
     65 typedef struct port_range {
     66 	in_port_t	pr_start;
     67 	in_port_t	pr_end;
     68 } port_range_t;
     69 
     70 typedef struct addr_port {
     71 	npfvar_t *	ap_netaddr;
     72 	npfvar_t *	ap_portrange;
     73 } addr_port_t;
     74 
     75 typedef struct filt_opts {
     76 	addr_port_t	fo_from;
     77 	addr_port_t	fo_to;
     78 } filt_opts_t;
     79 
     80 typedef struct opt_proto {
     81 	int		op_proto;
     82 	npfvar_t *	op_opts;
     83 } opt_proto_t;
     84 
     85 typedef struct rule_group {
     86 	const char *	rg_name;
     87 	uint32_t	rg_attr;
     88 	u_int		rg_ifnum;
     89 } rule_group_t;
     90 
     91 typedef struct proc_call {
     92 	const char *	pc_name;
     93 	npfvar_t *	pc_opts;
     94 } proc_call_t;
     95 
     96 typedef struct proc_param {
     97 	const char *	pp_param;
     98 	const char *	pp_value;
     99 } proc_param_t;
    100 
    101 void		yyerror(const char *, ...) __printflike(1, 2) __dead;
    102 
    103 void		npfctl_print_error(const nl_error_t *);
    104 char *		npfctl_print_addrmask(int, npf_addr_t *, npf_netmask_t);
    105 bool		npfctl_table_exists_p(const char *);
    106 int		npfctl_protono(const char *);
    107 in_port_t	npfctl_portno(const char *);
    108 uint8_t		npfctl_icmpcode(int, uint8_t, const char *);
    109 uint8_t		npfctl_icmptype(int, const char *);
    110 unsigned long   npfctl_find_ifindex(const char *);
    111 npfvar_t *	npfctl_parse_ifnet(const char *, const int);
    112 npfvar_t *	npfctl_parse_tcpflag(const char *);
    113 npfvar_t *	npfctl_parse_table_id(const char *);
    114 npfvar_t * 	npfctl_parse_icmp(int, int, int);
    115 npfvar_t *	npfctl_parse_port_range(in_port_t, in_port_t);
    116 npfvar_t *	npfctl_parse_port_range_variable(const char *);
    117 npfvar_t *	npfctl_parse_fam_addr_mask(const char *, const char *,
    118 		    unsigned long *);
    119 bool		npfctl_parse_cidr(char *, fam_addr_mask_t *, int *);
    120 
    121 /*
    122  * NPF extension loading.
    123  */
    124 
    125 typedef struct npf_extmod npf_extmod_t;
    126 
    127 npf_extmod_t *	npf_extmod_get(const char *, nl_ext_t **);
    128 int		npf_extmod_param(npf_extmod_t *, nl_ext_t *,
    129 		    const char *, const char *);
    130 
    131 /*
    132  * N-code generation interface.
    133  */
    134 
    135 typedef struct nc_ctx nc_ctx_t;
    136 
    137 #define	NC_MATCH_DST		0x01
    138 #define	NC_MATCH_SRC		0x02
    139 
    140 #define	NC_MATCH_TCP		0x04
    141 #define	NC_MATCH_UDP		0x08
    142 #define	NC_MATCH_ICMP		0x10
    143 #define	NC_MATCH_ICMP6		0x20
    144 
    145 nc_ctx_t *	npfctl_ncgen_create(void);
    146 void *		npfctl_ncgen_complete(nc_ctx_t *, size_t *);
    147 void		npfctl_ncgen_print(const void *, size_t);
    148 
    149 void		npfctl_ncgen_group(nc_ctx_t *);
    150 void		npfctl_ncgen_endgroup(nc_ctx_t *);
    151 
    152 void		npfctl_gennc_v4cidr(nc_ctx_t *, int, const npf_addr_t *,
    153 		    const npf_netmask_t);
    154 void		npfctl_gennc_v6cidr(nc_ctx_t *, int, const npf_addr_t *,
    155 		    const npf_netmask_t);
    156 void		npfctl_gennc_ports(nc_ctx_t *, int, in_port_t, in_port_t);
    157 void		npfctl_gennc_icmp(nc_ctx_t *, int, int);
    158 void		npfctl_gennc_icmp6(nc_ctx_t *, int, int);
    159 void		npfctl_gennc_tbl(nc_ctx_t *, int, u_int);
    160 void		npfctl_gennc_tcpfl(nc_ctx_t *, uint8_t, uint8_t);
    161 void		npfctl_gennc_proto(nc_ctx_t *ctx, uint8_t, uint8_t);
    162 
    163 /*
    164  * N-code disassembler.
    165  */
    166 
    167 typedef struct nc_inf nc_inf_t;
    168 
    169 nc_inf_t *	npfctl_ncode_disinf(FILE *);
    170 int		npfctl_ncode_disassemble(nc_inf_t *, const void *, size_t);
    171 
    172 /*
    173  * Configuration building interface.
    174  */
    175 
    176 #define	NPFCTL_NAT_DYNAMIC	1
    177 #define	NPFCTL_NAT_STATIC	2
    178 
    179 void		npfctl_config_init(bool);
    180 int		npfctl_config_send(int, const char *);
    181 nl_config_t *	npfctl_config_ref(void);
    182 int		npfctl_config_show(int);
    183 unsigned long	npfctl_debug_addif(const char *);
    184 
    185 void		npfctl_build_rproc(const char *, npfvar_t *);
    186 void		npfctl_build_group(const char *, int, u_int);
    187 void		npfctl_build_rule(int, u_int, sa_family_t,
    188 		    const opt_proto_t *, const filt_opts_t *, const char *);
    189 void		npfctl_build_natseg(int, int, u_int, const addr_port_t *,
    190 		    const addr_port_t *, const filt_opts_t *);
    191 void		npfctl_build_table(const char *, u_int, const char *);
    192 
    193 #endif
    194