xref: /freebsd/sys/netgraph/ng_nat.h (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*-
2  * Copyright 2005, Gleb Smirnoff <glebius@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #define NG_NAT_NODE_TYPE    "nat"
30 #define NGM_NAT_COOKIE      1107718711
31 
32 #define	NG_NAT_HOOK_IN	"in"
33 #define	NG_NAT_HOOK_OUT	"out"
34 
35 /* Arguments for NGM_NAT_SET_MODE message */
36 struct ng_nat_mode {
37 	uint32_t	flags;
38 	uint32_t	mask;
39 };
40 
41 /* Keep this in sync with the above structure definition */
42 #define NG_NAT_MODE_INFO {				\
43 	  { "flags",	&ng_parse_uint32_type	},	\
44 	  { "mask",	&ng_parse_uint32_type	},	\
45 	  { NULL }					\
46 }
47 
48 #define NG_NAT_LOG			0x01
49 #define NG_NAT_DENY_INCOMING		0x02
50 #define NG_NAT_SAME_PORTS		0x04
51 #define NG_NAT_UNREGISTERED_ONLY	0x10
52 #define NG_NAT_RESET_ON_ADDR_CHANGE	0x20
53 #define NG_NAT_PROXY_ONLY		0x40
54 #define NG_NAT_REVERSE			0x80
55 
56 #define NG_NAT_DESC_LENGTH	64
57 #define NG_NAT_REDIRPROTO_ADDR	(IPPROTO_MAX + 3) 	/* LibAlias' LINK_ADDR, also unused in in.h */
58 
59 /* Arguments for NGM_NAT_REDIRECT_PORT message */
60 struct ng_nat_redirect_port {
61 	struct in_addr	local_addr;
62 	struct in_addr	alias_addr;
63 	struct in_addr	remote_addr;
64 	uint16_t	local_port;
65 	uint16_t	alias_port;
66 	uint16_t	remote_port;
67 	uint8_t		proto;
68 	char		description[NG_NAT_DESC_LENGTH];
69 };
70 
71 /* Keep this in sync with the above structure definition */
72 #define NG_NAT_REDIRECT_PORT_TYPE_INFO(desctype) {		\
73 	  { "local_addr",	&ng_parse_ipaddr_type	},	\
74 	  { "alias_addr",	&ng_parse_ipaddr_type	},	\
75 	  { "remote_addr",	&ng_parse_ipaddr_type	},	\
76 	  { "local_port",	&ng_parse_uint16_type	},	\
77 	  { "alias_port",	&ng_parse_uint16_type	},	\
78 	  { "remote_port",	&ng_parse_uint16_type	},	\
79 	  { "proto",		&ng_parse_uint8_type	},	\
80 	  { "description",	(desctype)		},	\
81 	  { NULL }						\
82 }
83 
84 /* Arguments for NGM_NAT_REDIRECT_ADDR message */
85 struct ng_nat_redirect_addr {
86 	struct in_addr	local_addr;
87 	struct in_addr	alias_addr;
88 	char		description[NG_NAT_DESC_LENGTH];
89 };
90 
91 /* Keep this in sync with the above structure definition */
92 #define NG_NAT_REDIRECT_ADDR_TYPE_INFO(desctype) {		\
93 	  { "local_addr",	&ng_parse_ipaddr_type	},	\
94 	  { "alias_addr",	&ng_parse_ipaddr_type	},	\
95 	  { "description",	(desctype)		},	\
96 	  { NULL }						\
97 }
98 
99 /* Arguments for NGM_NAT_REDIRECT_PROTO message */
100 struct ng_nat_redirect_proto {
101 	struct in_addr	local_addr;
102 	struct in_addr	alias_addr;
103 	struct in_addr	remote_addr;
104 	uint8_t		proto;
105 	char		description[NG_NAT_DESC_LENGTH];
106 };
107 
108 /* Keep this in sync with the above structure definition */
109 #define NG_NAT_REDIRECT_PROTO_TYPE_INFO(desctype) {		\
110 	  { "local_addr",	&ng_parse_ipaddr_type	},	\
111 	  { "alias_addr",	&ng_parse_ipaddr_type	},	\
112 	  { "remote_addr",	&ng_parse_ipaddr_type	},	\
113 	  { "proto",		&ng_parse_uint8_type	},	\
114 	  { "description",	(desctype)		},	\
115 	  { NULL }						\
116 }
117 
118 /* Arguments for NGM_NAT_ADD_SERVER message */
119 struct ng_nat_add_server {
120 	uint32_t	id;
121 	struct in_addr	addr;
122 	uint16_t	port;
123 };
124 
125 /* Keep this in sync with the above structure definition */
126 #define NG_NAT_ADD_SERVER_TYPE_INFO {				\
127 	  { "id",		&ng_parse_uint32_type	},	\
128 	  { "addr",		&ng_parse_ipaddr_type	},	\
129 	  { "port",		&ng_parse_uint16_type	},	\
130 	  { NULL }						\
131 }
132 
133 /* List entry of array returned in NGM_NAT_LIST_REDIRECTS message */
134 struct ng_nat_listrdrs_entry {
135 	uint32_t	id;		/* Anything except zero */
136 	struct in_addr	local_addr;
137 	struct in_addr	alias_addr;
138 	struct in_addr	remote_addr;
139 	uint16_t	local_port;
140 	uint16_t	alias_port;
141 	uint16_t	remote_port;
142 	uint16_t	proto;		/* Valid proto or NG_NAT_REDIRPROTO_ADDR */
143 	uint16_t	lsnat;		/* LSNAT servers count */
144 	char		description[NG_NAT_DESC_LENGTH];
145 };
146 
147 /* Keep this in sync with the above structure definition */
148 #define NG_NAT_LISTRDRS_ENTRY_TYPE_INFO(desctype) {			\
149 	  { "id",		&ng_parse_uint32_type	},	\
150 	  { "local_addr",	&ng_parse_ipaddr_type	},	\
151 	  { "alias_addr",	&ng_parse_ipaddr_type	},	\
152 	  { "remote_addr",	&ng_parse_ipaddr_type	},	\
153 	  { "local_port",	&ng_parse_uint16_type	},	\
154 	  { "alias_port",	&ng_parse_uint16_type	},	\
155 	  { "remote_port",	&ng_parse_uint16_type	},	\
156 	  { "proto",		&ng_parse_uint16_type	},	\
157 	  { "lsnat",		&ng_parse_uint16_type	},	\
158 	  { "description",	(desctype)		},	\
159 	  { NULL }						\
160 }
161 
162 /* Structure returned by NGM_NAT_LIST_REDIRECTS */
163 struct ng_nat_list_redirects {
164 	uint32_t		total_count;
165 	struct ng_nat_listrdrs_entry redirects[];
166 };
167 
168 /* Keep this in sync with the above structure definition */
169 #define NG_NAT_LIST_REDIRECTS_TYPE_INFO(redirtype) {		\
170 	  { "total_count",	&ng_parse_uint32_type	},	\
171 	  { "redirects",	(redirtype)		},	\
172 	  { NULL }						\
173 }
174 
175 /* Structure returned by NGM_NAT_LIBALIAS_INFO */
176 struct ng_nat_libalias_info {
177 	uint32_t	icmpLinkCount;
178 	uint32_t	udpLinkCount;
179 	uint32_t	tcpLinkCount;
180 	uint32_t	sctpLinkCount;
181 	uint32_t	pptpLinkCount;
182 	uint32_t	protoLinkCount;
183 	uint32_t	fragmentIdLinkCount;
184 	uint32_t	fragmentPtrLinkCount;
185 	uint32_t	sockCount;
186 };
187 
188 /* Keep this in sync with the above structure definition */
189 #define NG_NAT_LIBALIAS_INFO {					\
190 	  { "icmpLinkCount",	&ng_parse_uint32_type	},	\
191 	  { "udpLinkCount",	&ng_parse_uint32_type	},	\
192 	  { "tcpLinkCount",	&ng_parse_uint32_type	},	\
193 	  { "sctpLinkCount",	&ng_parse_uint32_type	},	\
194 	  { "pptpLinkCount",	&ng_parse_uint32_type	},	\
195 	  { "protoLinkCount",	&ng_parse_uint32_type	},	\
196 	  { "fragmentIdLinkCount", &ng_parse_uint32_type },	\
197 	  { "fragmentPtrLinkCount", &ng_parse_uint32_type },	\
198 	  { "sockCount",	&ng_parse_uint32_type	},	\
199 	  { NULL }						\
200 }
201 
202 enum {
203 	NGM_NAT_SET_IPADDR = 1,
204 	NGM_NAT_SET_MODE,
205 	NGM_NAT_SET_TARGET,
206 	NGM_NAT_REDIRECT_PORT,
207 	NGM_NAT_REDIRECT_ADDR,
208 	NGM_NAT_REDIRECT_PROTO,
209 	NGM_NAT_REDIRECT_DYNAMIC,
210 	NGM_NAT_REDIRECT_DELETE,
211 	NGM_NAT_ADD_SERVER,
212 	NGM_NAT_LIST_REDIRECTS,
213 	NGM_NAT_PROXY_RULE,
214 	NGM_NAT_LIBALIAS_INFO,
215 };
216