Home | History | Annotate | Line # | Download | only in npfctl
npfctl.h revision 1.30
      1 /*	$NetBSD: npfctl.h,v 1.30 2013/09/19 01:04:45 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This material is based upon work partially supported by The
      8  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _NPFCTL_H_
     33 #define _NPFCTL_H_
     34 
     35 #include <stdio.h>
     36 #include <stdbool.h>
     37 #include <inttypes.h>
     38 #include <assert.h>
     39 #include <util.h>
     40 
     41 #define	NPF_BPFCOP
     42 #include <net/npf_ncode.h>
     43 #include <net/npf.h>
     44 
     45 #define	_NPF_PRIVATE
     46 #include <npf.h>
     47 
     48 #include "npf_var.h"
     49 
     50 #define	NPF_DEV_PATH	"/dev/npf"
     51 #define	NPF_CONF_PATH	"/etc/npf.conf"
     52 #define	NPF_SESSDB_PATH	"/var/db/npf_sessions.db"
     53 
     54 typedef struct fam_addr_mask {
     55 	sa_family_t	fam_family;
     56 	npf_addr_t	fam_addr;
     57 	npf_netmask_t	fam_mask;
     58 	unsigned long	fam_ifindex;
     59 } fam_addr_mask_t;
     60 
     61 typedef struct ifnet_addr {
     62 	unsigned long	ifna_index;
     63 	sa_family_t	ifna_family;
     64 	npfvar_t *	ifna_filter;
     65 	npfvar_t *	ifna_addrs;
     66 } ifnet_addr_t;
     67 
     68 typedef struct port_range {
     69 	in_port_t	pr_start;
     70 	in_port_t	pr_end;
     71 } port_range_t;
     72 
     73 typedef struct addr_port {
     74 	npfvar_t *	ap_netaddr;
     75 	npfvar_t *	ap_portrange;
     76 } addr_port_t;
     77 
     78 typedef struct filt_opts {
     79 	addr_port_t	fo_from;
     80 	addr_port_t	fo_to;
     81 } filt_opts_t;
     82 
     83 typedef struct opt_proto {
     84 	int		op_proto;
     85 	npfvar_t *	op_opts;
     86 } opt_proto_t;
     87 
     88 typedef struct rule_group {
     89 	const char *	rg_name;
     90 	uint32_t	rg_attr;
     91 	u_int		rg_ifnum;
     92 	bool		rg_default;
     93 } rule_group_t;
     94 
     95 typedef struct proc_call {
     96 	const char *	pc_name;
     97 	npfvar_t *	pc_opts;
     98 } proc_call_t;
     99 
    100 typedef struct proc_param {
    101 	const char *	pp_param;
    102 	const char *	pp_value;
    103 } proc_param_t;
    104 
    105 enum { NPFCTL_PARSE_FILE, NPFCTL_PARSE_STRING };
    106 
    107 bool		join(char *, size_t, int, char **, const char *);
    108 void		yyerror(const char *, ...) __printflike(1, 2) __dead;
    109 void		npfctl_parse_file(const char *);
    110 void		npfctl_parse_string(const char *);
    111 
    112 void		npfctl_print_error(const nl_error_t *);
    113 char *		npfctl_print_addrmask(int, const npf_addr_t *, npf_netmask_t);
    114 bool		npfctl_table_exists_p(const char *);
    115 int		npfctl_protono(const char *);
    116 in_port_t	npfctl_portno(const char *);
    117 uint8_t		npfctl_icmpcode(int, uint8_t, const char *);
    118 uint8_t		npfctl_icmptype(int, const char *);
    119 unsigned long   npfctl_find_ifindex(const char *);
    120 npfvar_t *	npfctl_parse_ifnet(const char *, const int);
    121 npfvar_t *	npfctl_parse_tcpflag(const char *);
    122 npfvar_t *	npfctl_parse_table_id(const char *);
    123 npfvar_t * 	npfctl_parse_icmp(int, int, int);
    124 npfvar_t *	npfctl_parse_port_range(in_port_t, in_port_t);
    125 npfvar_t *	npfctl_parse_port_range_variable(const char *);
    126 npfvar_t *	npfctl_parse_fam_addr_mask(const char *, const char *,
    127 		    unsigned long *);
    128 bool		npfctl_parse_cidr(char *, fam_addr_mask_t *, int *);
    129 
    130 /*
    131  * NPF extension loading.
    132  */
    133 
    134 typedef struct npf_extmod npf_extmod_t;
    135 
    136 npf_extmod_t *	npf_extmod_get(const char *, nl_ext_t **);
    137 int		npf_extmod_param(npf_extmod_t *, nl_ext_t *,
    138 		    const char *, const char *);
    139 
    140 /*
    141  * BFF byte-code generation interface.
    142  */
    143 
    144 #define	NPFCTL_USE_BPF	1
    145 
    146 typedef struct npf_bpf npf_bpf_t;
    147 
    148 #define	MATCH_DST	0x01
    149 #define	MATCH_SRC	0x02
    150 
    151 enum {
    152 	BM_IPVER,
    153 	BM_PROTO,
    154 	BM_SRC_CIDR,
    155 	BM_SRC_TABLE,
    156 	BM_DST_CIDR,
    157 	BM_DST_TABLE,
    158 	BM_SRC_PORTS,
    159 	BM_DST_PORTS,
    160 	BM_TCPFL,
    161 	BM_ICMP_TYPE,
    162 	BM_ICMP_CODE,
    163 };
    164 
    165 npf_bpf_t *npfctl_bpf_create(void);
    166 struct bpf_program *npfctl_bpf_complete(npf_bpf_t *);
    167 const void *npfctl_bpf_bmarks(npf_bpf_t *, size_t *);
    168 void	npfctl_bpf_destroy(npf_bpf_t *);
    169 
    170 void	npfctl_bpf_group(npf_bpf_t *);
    171 void	npfctl_bpf_endgroup(npf_bpf_t *);
    172 
    173 void	npfctl_bpf_proto(npf_bpf_t *, sa_family_t, int);
    174 void	npfctl_bpf_cidr(npf_bpf_t *, u_int, sa_family_t,
    175 	    const npf_addr_t *, const npf_netmask_t);
    176 void	npfctl_bpf_ports(npf_bpf_t *, u_int, in_port_t, in_port_t);
    177 void	npfctl_bpf_tcpfl(npf_bpf_t *, uint8_t, uint8_t);
    178 void	npfctl_bpf_icmp(npf_bpf_t *, int, int);
    179 void	npfctl_bpf_table(npf_bpf_t *, u_int, u_int);
    180 
    181 /*
    182  * N-code generation interface.
    183  */
    184 
    185 typedef struct nc_ctx nc_ctx_t;
    186 
    187 #define	NC_MATCH_DST		0x01
    188 #define	NC_MATCH_SRC		0x02
    189 
    190 #define	NC_MATCH_TCP		0x04
    191 #define	NC_MATCH_UDP		0x08
    192 #define	NC_MATCH_ICMP		0x10
    193 #define	NC_MATCH_ICMP6		0x20
    194 
    195 nc_ctx_t *	npfctl_ncgen_create(void);
    196 void *		npfctl_ncgen_complete(nc_ctx_t *, size_t *);
    197 void		npfctl_ncgen_print(const void *, size_t);
    198 
    199 void		npfctl_ncgen_group(nc_ctx_t *);
    200 void		npfctl_ncgen_endgroup(nc_ctx_t *);
    201 
    202 void		npfctl_gennc_v4cidr(nc_ctx_t *, int, const npf_addr_t *,
    203 		    const npf_netmask_t);
    204 void		npfctl_gennc_v6cidr(nc_ctx_t *, int, const npf_addr_t *,
    205 		    const npf_netmask_t);
    206 void		npfctl_gennc_ports(nc_ctx_t *, int, in_port_t, in_port_t);
    207 void		npfctl_gennc_icmp(nc_ctx_t *, int, int);
    208 void		npfctl_gennc_icmp6(nc_ctx_t *, int, int);
    209 void		npfctl_gennc_tbl(nc_ctx_t *, int, u_int);
    210 void		npfctl_gennc_tcpfl(nc_ctx_t *, uint8_t, uint8_t);
    211 void		npfctl_gennc_proto(nc_ctx_t *ctx, uint8_t, uint8_t);
    212 
    213 /*
    214  * N-code disassembler.
    215  */
    216 
    217 typedef struct nc_inf nc_inf_t;
    218 
    219 nc_inf_t *	npfctl_ncode_disinf(FILE *);
    220 int		npfctl_ncode_disassemble(nc_inf_t *, const void *, size_t);
    221 
    222 /*
    223  * Configuration building interface.
    224  */
    225 
    226 #define	NPFCTL_NAT_DYNAMIC	1
    227 #define	NPFCTL_NAT_STATIC	2
    228 
    229 void		npfctl_config_init(bool);
    230 int		npfctl_config_send(int, const char *);
    231 nl_config_t *	npfctl_config_ref(void);
    232 int		npfctl_config_show(int);
    233 int		npfctl_ruleset_show(int, const char *);
    234 
    235 nl_rule_t *	npfctl_rule_ref(void);
    236 unsigned long	npfctl_debug_addif(const char *);
    237 
    238 void		npfctl_build_alg(const char *);
    239 void		npfctl_build_rproc(const char *, npfvar_t *);
    240 void		npfctl_build_group(const char *, int, u_int, bool);
    241 void		npfctl_build_group_end(void);
    242 void		npfctl_build_rule(uint32_t, u_int, sa_family_t,
    243 		    const opt_proto_t *, const filt_opts_t *, const char *);
    244 void		npfctl_build_natseg(int, int, u_int, const addr_port_t *,
    245 		    const addr_port_t *, const filt_opts_t *);
    246 void		npfctl_build_maprset(const char *, int, u_int);
    247 void		npfctl_build_table(const char *, u_int, const char *);
    248 
    249 #endif
    250