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