npfctl.h revision 1.10 1 /* $NetBSD: npfctl.h,v 1.10 2012/01/15 00:49:48 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
36 #include <net/npf_ncode.h>
37 #include <net/npf.h>
38
39 #define _NPF_PRIVATE
40 #include <npf.h>
41
42 #include "npf_var.h"
43
44 #define NPF_DEV_PATH "/dev/npf"
45 #define NPF_CONF_PATH "/etc/npf.conf"
46 #define NPF_SESSDB_PATH "/var/db/npf_sessions.db"
47
48 typedef struct fam_addr_mask {
49 sa_family_t fam_family;
50 npf_addr_t fam_addr;
51 npf_netmask_t fam_mask;
52 npfvar_t * fam_interface;
53 } fam_addr_mask_t;
54
55 typedef struct port_range {
56 in_port_t pr_start;
57 in_port_t pr_end;
58 } port_range_t;
59
60 typedef struct filt_opts {
61 npfvar_t * fo_from;
62 npfvar_t * fo_from_port_range;
63 npfvar_t * fo_to;
64 npfvar_t * fo_to_port_range;
65 } filt_opts_t;
66
67 typedef struct opt_proto {
68 int op_proto;
69 npfvar_t * op_opts;
70 } opt_proto_t;
71
72 typedef struct rule_group {
73 const char * rg_name;
74 int rg_attr;
75 u_int rg_ifnum;
76 } rule_group_t;
77
78 typedef struct proc_op {
79 const char * po_name;
80 npfvar_t * po_opts;
81 } proc_op_t;
82
83 typedef struct module_arg {
84 const char * ma_name;
85 npfvar_t * ma_opts;
86 } module_arg_t;
87
88 void yyerror(const char *, ...) __printflike(1, 2) __dead;
89 void * zalloc(size_t);
90 void * xrealloc(void *, size_t);
91 char * xstrdup(const char *);
92 char * xstrndup(const char *, size_t);
93
94 bool npfctl_table_exists_p(const char *);
95 in_port_t npfctl_portno(const char *);
96 uint8_t npfctl_icmpcode(uint8_t, const char *);
97 uint8_t npfctl_icmptype(const char *);
98 unsigned long npfctl_find_ifindex(const char *);
99 npfvar_t * npfctl_parse_tcpflag(const char *);
100 npfvar_t * npfctl_parse_table_id(const char *);
101 npfvar_t * npfctl_parse_icmpcode(const char *);
102 npfvar_t * npfctl_parse_icmp(uint8_t, uint8_t);
103 npfvar_t * npfctl_parse_iface(const char *);
104 npfvar_t * npfctl_parse_port_range(in_port_t, in_port_t);
105 npfvar_t * npfctl_parse_fam_addr_mask(const char *, const char *,
106 unsigned long *);
107 fam_addr_mask_t *npfctl_parse_cidr(char *);
108
109 /*
110 * N-code generation interface.
111 */
112
113 typedef struct nc_ctx nc_ctx_t;
114
115 #define NC_MATCH_DST 0x01
116 #define NC_MATCH_SRC 0x02
117
118 #define NC_MATCH_TCP 0x04
119 #define NC_MATCH_UDP 0x08
120
121 nc_ctx_t * npfctl_ncgen_create(void);
122 void * npfctl_ncgen_complete(nc_ctx_t *, size_t *);
123 void npfctl_ncgen_print(const void *, size_t);
124
125 void npfctl_ncgen_group(nc_ctx_t *);
126 void npfctl_ncgen_endgroup(nc_ctx_t *);
127
128 void npfctl_gennc_v4cidr(nc_ctx_t *, int, const npf_addr_t *,
129 const npf_netmask_t);
130 void npfctl_gennc_v6cidr(nc_ctx_t *, int, const npf_addr_t *,
131 const npf_netmask_t);
132 void npfctl_gennc_ports(nc_ctx_t *, int, in_port_t, in_port_t);
133 void npfctl_gennc_icmp(nc_ctx_t *, int, int);
134 void npfctl_gennc_tbl(nc_ctx_t *, int, u_int);
135 void npfctl_gennc_tcpfl(nc_ctx_t *, uint8_t, uint8_t);
136
137 /*
138 * Configuration building interface.
139 */
140
141 #define NPFCTL_RDR 1
142 #define NPFCTL_BINAT 2
143 #define NPFCTL_NAT 3
144
145 void npfctl_config_init(bool);
146 int npfctl_config_send(int);
147
148 void npfctl_build_rproc(const char *, npfvar_t *);
149 void npfctl_build_group(const char *, int, u_int);
150 void npfctl_build_rule(int, u_int, sa_family_t,
151 const opt_proto_t *, const filt_opts_t *, const char *);
152 void npfctl_build_nat(int, u_int, const filt_opts_t *,
153 npfvar_t *, npfvar_t *);
154 void npfctl_build_table(const char *, u_int, const char *);
155
156 #endif
157