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