xref: /freebsd/sys/netinet/libalias/alias.h (revision 9608d7e2cd58c1a7fff6562810f2ce519e6ec50a)
1 /* lint -save -library Flexelint comment for external headers */
2 
3 /*-
4  * Copyright (c) 2001 Charles Mott <cm@linktel.net>
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
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 /*
43  * The external interface to libalias, the packet aliasing engine.
44  *
45  * There are two sets of functions:
46  *
47  * PacketAlias*() the old API which doesn't take an instance pointer
48  * and therefore can only have one packet engine at a time.
49  *
50  * LibAlias*() the new API which takes as first argument a pointer to
51  * the instance of the packet aliasing engine.
52  *
53  * The functions otherwise correspond to each other one for one, except
54  * for the LibAliasUnaliasOut()/PacketUnaliasOut() function which were
55  * were misnamed in the old API.
56  */
57 
58 /*
59  * The instance structure
60  */
61 struct libalias;
62 
63 /*
64  * An anonymous structure, a pointer to which is returned from
65  * PacketAliasRedirectAddr(), PacketAliasRedirectPort() or
66  * PacketAliasRedirectProto(), passed to PacketAliasAddServer(),
67  * and freed by PacketAliasRedirectDelete().
68  */
69 struct	alias_link;
70 
71 
72 /* OLD API */
73 
74 /* Initialization and control functions. */
75 void	 PacketAliasInit(void);
76 void	 PacketAliasSetAddress(struct in_addr _addr);
77 void	 PacketAliasSetFWBase(unsigned int _base, unsigned int _num);
78 void	 PacketAliasSetSkinnyPort(unsigned int _port);
79 unsigned int
80 	 PacketAliasSetMode(unsigned int _flags, unsigned int _mask);
81 void	 PacketAliasUninit(void);
82 
83 /* Packet Handling functions. */
84 int	 PacketAliasIn(char *_ptr, int _maxpacketsize);
85 int	 PacketAliasOut(char *_ptr, int _maxpacketsize);
86 int	 PacketUnaliasOut(char *_ptr, int _maxpacketsize);
87 
88 /* Port and address redirection functions. */
89 
90 
91 int	 PacketAliasAddServer(struct alias_link *_link,
92 	    struct in_addr _addr, unsigned short _port);
93 struct alias_link *
94 	 PacketAliasRedirectAddr(struct in_addr _src_addr,
95 	    struct in_addr _alias_addr);
96 int	 PacketAliasRedirectDynamic(struct alias_link *_link);
97 void	 PacketAliasRedirectDelete(struct alias_link *_link);
98 struct alias_link *
99 	 PacketAliasRedirectPort(struct in_addr _src_addr,
100 	    unsigned short _src_port, struct in_addr _dst_addr,
101 	    unsigned short _dst_port, struct in_addr _alias_addr,
102 	    unsigned short _alias_port, unsigned char _proto);
103 struct alias_link *
104 	 PacketAliasRedirectProto(struct in_addr _src_addr,
105 	    struct in_addr _dst_addr, struct in_addr _alias_addr,
106 	    unsigned char _proto);
107 
108 /* Fragment Handling functions. */
109 void	 PacketAliasFragmentIn(char *_ptr, char *_ptr_fragment);
110 char	*PacketAliasGetFragment(char *_ptr);
111 int	 PacketAliasSaveFragment(char *_ptr);
112 
113 /* Miscellaneous functions. */
114 int	 PacketAliasCheckNewLink(void);
115 unsigned short
116 	 PacketAliasInternetChecksum(unsigned short *_ptr, int _nbytes);
117 void	 PacketAliasSetTarget(struct in_addr _target_addr);
118 
119 /* Transparent proxying routines. */
120 int	 PacketAliasProxyRule(const char *_cmd);
121 
122 /* NEW API */
123 
124 /* Initialization and control functions. */
125 struct libalias	*LibAliasInit(struct libalias *);
126 void	 LibAliasSetAddress(struct libalias *, struct in_addr _addr);
127 void	 LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num);
128 void	 LibAliasSetSkinnyPort(struct libalias *, unsigned int _port);
129 unsigned int
130 	 LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask);
131 void	 LibAliasUninit(struct libalias *);
132 
133 /* Packet Handling functions. */
134 int	 LibAliasIn(struct libalias *, char *_ptr, int _maxpacketsize);
135 int	 LibAliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
136 int	 LibAliasUnaliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
137 
138 /* Port and address redirection functions. */
139 
140 int	 LibAliasAddServer(struct libalias *, struct alias_link *_link,
141 	    struct in_addr _addr, unsigned short _port);
142 struct alias_link *
143 	 LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
144 	    struct in_addr _alias_addr);
145 int	 LibAliasRedirectDynamic(struct libalias *, struct alias_link *_link);
146 void	 LibAliasRedirectDelete(struct libalias *, struct alias_link *_link);
147 struct alias_link *
148 	 LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
149 	    unsigned short _src_port, struct in_addr _dst_addr,
150 	    unsigned short _dst_port, struct in_addr _alias_addr,
151 	    unsigned short _alias_port, unsigned char _proto);
152 struct alias_link *
153 	 LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
154 	    struct in_addr _dst_addr, struct in_addr _alias_addr,
155 	    unsigned char _proto);
156 
157 /* Fragment Handling functions. */
158 void	 LibAliasFragmentIn(struct libalias *, char *_ptr, char *_ptr_fragment);
159 char	*LibAliasGetFragment(struct libalias *, char *_ptr);
160 int	 LibAliasSaveFragment(struct libalias *, char *_ptr);
161 
162 /* Miscellaneous functions. */
163 int	 LibAliasCheckNewLink(struct libalias *);
164 unsigned short
165 	 LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes);
166 void	 LibAliasSetTarget(struct libalias *, struct in_addr _target_addr);
167 
168 /* Transparent proxying routines. */
169 int	 LibAliasProxyRule(struct libalias *, const char *_cmd);
170 
171 
172 /*
173  * Mode flags and other constants.
174  */
175 
176 
177 /* Mode flags, set using PacketAliasSetMode() */
178 
179 /*
180  * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log
181  * every time a link is created or deleted.  This is useful for debugging.
182  */
183 #define	PKT_ALIAS_LOG			0x01
184 
185 /*
186  * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp,
187  * telnet or web servers will be prevented by the aliasing mechanism.
188  */
189 #define	PKT_ALIAS_DENY_INCOMING		0x02
190 
191 /*
192  * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the
193  * same port as they originated on.  This allows e.g. rsh to work *99% of the
194  * time*, but _not_ 100% (it will be slightly flakey instead of not working
195  * at all).  This mode bit is set by PacketAliasInit(), so it is a default
196  * mode of operation.
197  */
198 #define	PKT_ALIAS_SAME_PORTS		0x04
199 
200 /*
201  * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g.
202  * destination port and/or address is zero), the packet aliasing engine will
203  * attempt to allocate a socket for the aliasing port it chooses.  This will
204  * avoid interference with the host machine.  Fully specified links do not
205  * require this.  This bit is set after a call to PacketAliasInit(), so it is
206  * a default mode of operation.
207  */
208 #define	PKT_ALIAS_USE_SOCKETS		0x08
209 
210 /*-
211  * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with
212  * unregistered source addresses will be aliased.  Private
213  * addresses are those in the following ranges:
214  *
215  *		10.0.0.0     ->   10.255.255.255
216  *		172.16.0.0   ->   172.31.255.255
217  *		192.168.0.0  ->   192.168.255.255
218  */
219 #define	PKT_ALIAS_UNREGISTERED_ONLY	0x10
220 
221 /*
222  * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic
223  * aliasing links will be reset whenever PacketAliasSetAddress() changes the
224  * default aliasing address.  If the default aliasing address is left
225  * unchanged by this function call, then the table of dynamic aliasing links
226  * will be left intact.  This bit is set after a call to PacketAliasInit().
227  */
228 #define	PKT_ALIAS_RESET_ON_ADDR_CHANGE	0x20
229 
230 #ifndef NO_FW_PUNCH
231 /*
232  * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will
233  * create a 'hole' in the firewall to allow the transfers to work.  The
234  * ipfw rule number that the hole is created with is controlled by
235  * PacketAliasSetFWBase().  The hole will be attached to that
236  * particular alias_link, so when the link goes away the hole is deleted.
237  */
238 #define	PKT_ALIAS_PUNCH_FW		0x100
239 #endif
240 
241 /*
242  * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only
243  * transparent proxying is performed.
244  */
245 #define	PKT_ALIAS_PROXY_ONLY		0x40
246 
247 /*
248  * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and
249  * PacketAliasOut() are reversed.
250  */
251 #define	PKT_ALIAS_REVERSE		0x80
252 
253 /* Function return codes. */
254 #define	PKT_ALIAS_ERROR			-1
255 #define	PKT_ALIAS_OK			1
256 #define	PKT_ALIAS_IGNORED		2
257 #define	PKT_ALIAS_UNRESOLVED_FRAGMENT	3
258 #define	PKT_ALIAS_FOUND_HEADER_FRAGMENT	4
259 
260 #endif /* !_ALIAS_H_ */
261 
262 /* lint -restore */
263