xref: /freebsd/sys/netinet/libalias/alias.h (revision 61bf830cbb260c2a046cb44421d319184393e028)
1 /* lint -save -library Flexelint comment for external headers */
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2001 Charles Mott <cm@linktel.net>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * Alias.h defines the outside world interfaces for the packet aliasing
33  * software.
34  *
35  * This software is placed into the public domain with no restrictions on its
36  * distribution.
37  */
38 
39 #ifndef _ALIAS_H_
40 #define	_ALIAS_H_
41 
42 #include <netinet/in_systm.h>
43 #include <netinet/in.h>
44 #include <netinet/ip.h>
45 
46 #define LIBALIAS_BUF_SIZE 128
47 #ifdef _KERNEL
48 /*
49  * The kernel version of libalias does not support these features.
50  */
51 #define	NO_FW_PUNCH
52 #define	NO_USE_SOCKETS
53 #endif
54 
55 /*
56  * The external interface to libalias, the packet aliasing engine.
57  *
58  * There are two sets of functions:
59  *
60  * PacketAlias*() the old API which doesn't take an instance pointer
61  * and therefore can only have one packet engine at a time.
62  *
63  * LibAlias*() the new API which takes as first argument a pointer to
64  * the instance of the packet aliasing engine.
65  *
66  * The functions otherwise correspond to each other one for one, except
67  * for the LibAliasUnaliasOut()/PacketUnaliasOut() function which were
68  * were misnamed in the old API.
69  */
70 
71 /*
72  * The instance structure
73  */
74 struct libalias;
75 
76 /*
77  * An anonymous structure, a pointer to which is returned from
78  * PacketAliasRedirectAddr(), PacketAliasRedirectPort() or
79  * PacketAliasRedirectProto(), passed to PacketAliasAddServer(),
80  * and freed by PacketAliasRedirectDelete().
81  */
82 struct alias_link;
83 
84 /* Initialization and control functions. */
85 struct libalias *LibAliasInit(struct libalias *);
86 void		LibAliasSetAddress(struct libalias *, struct in_addr _addr);
87 void		LibAliasSetAliasPortRange(struct libalias *la, u_short port_low, u_short port_hi);
88 void		LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num);
89 void		LibAliasSetSkinnyPort(struct libalias *, unsigned int _port);
90 unsigned int	LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask);
91 void		LibAliasUninit(struct libalias *);
92 
93 /* Packet Handling functions. */
94 int		LibAliasIn (struct libalias *, void *_ptr, int _maxpacketsize);
95 int		LibAliasOut(struct libalias *, void *_ptr, int _maxpacketsize);
96 int		LibAliasOutTry(struct libalias *, void *_ptr, int _maxpacketsize, int _create);
97 int		LibAliasUnaliasOut(struct libalias *, void *_ptr, int _maxpacketsize);
98 
99 /* Port and address redirection functions. */
100 
101 int		LibAliasAddServer(struct libalias *, struct alias_link *_lnk,
102 		    struct in_addr _addr, unsigned short _port);
103 struct alias_link * LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
104 		    struct in_addr _alias_addr);
105 int		LibAliasRedirectDynamic(struct libalias *, struct alias_link *_lnk);
106 void		LibAliasRedirectDelete(struct libalias *, struct alias_link *_lnk);
107 struct alias_link * LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
108 		    unsigned short _src_port, struct in_addr _dst_addr,
109 		    unsigned short _dst_port, struct in_addr _alias_addr,
110 		    unsigned short _alias_port, unsigned char _proto);
111 struct alias_link * LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
112 		    struct in_addr _dst_addr, struct in_addr _alias_addr,
113 		    unsigned char _proto);
114 
115 /* Fragment Handling functions. */
116 void		LibAliasFragmentIn(struct libalias *, void *_ptr, void *_ptr_fragment);
117 void	       *LibAliasGetFragment(struct libalias *, void *_ptr);
118 int		LibAliasSaveFragment(struct libalias *, void *_ptr);
119 
120 /* Miscellaneous functions. */
121 unsigned short	LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes);
122 void		LibAliasSetTarget(struct libalias *, struct in_addr _target_addr);
123 
124 /* Transparent proxying routines. */
125 int		LibAliasProxyRule(struct libalias *, const char *_cmd);
126 
127 /* Module handling API */
128 int		LibAliasLoadModule(char *);
129 int		LibAliasUnLoadAllModule(void);
130 int		LibAliasRefreshModules(void);
131 
132 /* Mbuf helper function. */
133 struct mbuf    *m_megapullup(struct mbuf *, int);
134 
135 /*
136  * Mode flags and other constants.
137  */
138 
139 /* Mode flags, set using PacketAliasSetMode() */
140 
141 /*
142  * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log
143  * every time a link is created or deleted.  This is useful for debugging.
144  */
145 #define	PKT_ALIAS_LOG			0x01
146 
147 /*
148  * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp,
149  * telnet or web servers will be prevented by the aliasing mechanism.
150  */
151 #define	PKT_ALIAS_DENY_INCOMING		0x02
152 
153 /*
154  * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the
155  * same port as they originated on.  This allows e.g. rsh to work *99% of the
156  * time*, but _not_ 100% (it will be slightly flakey instead of not working
157  * at all).  This mode bit is set by PacketAliasInit(), so it is a default
158  * mode of operation.
159  */
160 #define	PKT_ALIAS_SAME_PORTS		0x04
161 
162 /*
163  * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g.
164  * destination port and/or address is zero), the packet aliasing engine will
165  * attempt to allocate a socket for the aliasing port it chooses.  This will
166  * avoid interference with the host machine.  Fully specified links do not
167  * require this.  This bit is set after a call to PacketAliasInit(), so it is
168  * a default mode of operation.
169  */
170 #ifndef NO_USE_SOCKETS
171 #define	PKT_ALIAS_USE_SOCKETS		0x08
172 #endif
173 /*-
174  * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with
175  * unregistered source addresses will be aliased.  Private
176  * addresses are those in the following ranges:
177  *
178  *		10.0.0.0     ->   10.255.255.255
179  *		172.16.0.0   ->   172.31.255.255
180  *		192.168.0.0  ->   192.168.255.255
181  */
182 #define	PKT_ALIAS_UNREGISTERED_ONLY	0x10
183 
184 /*
185  * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic
186  * aliasing links will be reset whenever PacketAliasSetAddress() changes the
187  * default aliasing address.  If the default aliasing address is left
188  * unchanged by this function call, then the table of dynamic aliasing links
189  * will be left intact.  This bit is set after a call to PacketAliasInit().
190  */
191 #define	PKT_ALIAS_RESET_ON_ADDR_CHANGE	0x20
192 
193 /*
194  * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only
195  * transparent proxying is performed.
196  */
197 #define	PKT_ALIAS_PROXY_ONLY		0x40
198 
199 /*
200  * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and
201  * PacketAliasOut() are reversed.
202  */
203 #define	PKT_ALIAS_REVERSE		0x80
204 
205 #ifndef NO_FW_PUNCH
206 /*
207  * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will
208  * create a 'hole' in the firewall to allow the transfers to work.  The
209  * ipfw rule number that the hole is created with is controlled by
210  * PacketAliasSetFWBase().  The hole will be attached to that
211  * particular alias_link, so when the link goes away the hole is deleted.
212  */
213 #define	PKT_ALIAS_PUNCH_FW		0x100
214 #endif
215 
216 /*
217  * If PKT_ALIAS_SKIP_GLOBAL is set, nat instance is not checked for matching
218  * states in 'ipfw nat global' rule.
219  */
220 #define	PKT_ALIAS_SKIP_GLOBAL		0x200
221 
222 /*
223  * Like PKT_ALIAS_UNREGISTERED_ONLY, but includes the RFC 6598
224  * (Carrier Grade NAT) address range as follows:
225  *
226  *		100.64.0.0   ->   100.127.255.255
227  */
228 #define	PKT_ALIAS_UNREGISTERED_CGN	0x400
229 
230 /*
231  * When this bit is set, UDP uses endpoint-independent mapping (EIM), as per
232  * RFC 4787 ("full cone" NAT of RFC 3489). All packets from the same internal
233  * address:port are mapped to the same NAT address:port, regardless of their
234  * destination address:port. If filtering rules allow, and if
235  * PKT_ALIAS_DENY_INCOMING is unset, any other external address:port can also
236  * send to the internal address:port through its mapped NAT address:port. This
237  * is more compatible with applications, and can reduce the need for port
238  * forwarding, but less scalable as each NAT address:port can only be
239  * concurrently used by at most one internal address:port.
240  *
241  * When this bit is unset, UDP packets use endpoint-dependent mapping (EDM)
242  * ("symmetric" NAT). Each connection from a particular internal address:port
243  * to different external addresses:ports is mapped to a random and
244  * unpredictable NAT address:port. Two appplications behind EDM NATs can only
245  * connect to each other by port forwarding on the NAT, or tunnelling through
246  * an in-between server.
247  */
248 #define PKT_ALIAS_UDP_EIM		0x800
249 
250 /* Function return codes. */
251 #define	PKT_ALIAS_ERROR			-1
252 #define	PKT_ALIAS_OK			1
253 #define	PKT_ALIAS_IGNORED		2
254 #define	PKT_ALIAS_UNRESOLVED_FRAGMENT	3
255 #define	PKT_ALIAS_FOUND_HEADER_FRAGMENT	4
256 
257 #endif				/* !_ALIAS_H_ */
258 
259 /* lint -restore */
260