xref: /freebsd/sys/netinet/libalias/alias.h (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 #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 
85 /* OLD API */
86 
87 /* Initialization and control functions. */
88 void		PacketAliasInit(void);
89 void		PacketAliasSetAddress(struct in_addr _addr);
90 void		PacketAliasSetFWBase(unsigned int _base, unsigned int _num);
91 void		PacketAliasSetSkinnyPort(unsigned int _port);
92 unsigned int
93 		PacketAliasSetMode(unsigned int _flags, unsigned int _mask);
94 void		PacketAliasUninit(void);
95 
96 /* Packet Handling functions. */
97 int		PacketAliasIn(char *_ptr, int _maxpacketsize);
98 int		PacketAliasOut(char *_ptr, int _maxpacketsize);
99 int		PacketUnaliasOut(char *_ptr, int _maxpacketsize);
100 
101 /* Port and address redirection functions. */
102 
103 
104 int
105 PacketAliasAddServer(struct alias_link *_lnk,
106     struct in_addr _addr, unsigned short _port);
107 struct alias_link *
108 PacketAliasRedirectAddr(struct in_addr _src_addr,
109     struct in_addr _alias_addr);
110 int		PacketAliasRedirectDynamic(struct alias_link *_lnk);
111 void		PacketAliasRedirectDelete(struct alias_link *_lnk);
112 struct alias_link *
113 PacketAliasRedirectPort(struct in_addr _src_addr,
114     unsigned short _src_port, struct in_addr _dst_addr,
115     unsigned short _dst_port, struct in_addr _alias_addr,
116     unsigned short _alias_port, unsigned char _proto);
117 struct alias_link *
118 PacketAliasRedirectProto(struct in_addr _src_addr,
119     struct in_addr _dst_addr, struct in_addr _alias_addr,
120     unsigned char _proto);
121 
122 /* Fragment Handling functions. */
123 void		PacketAliasFragmentIn(char *_ptr, char *_ptr_fragment);
124 char           *PacketAliasGetFragment(char *_ptr);
125 int		PacketAliasSaveFragment(char *_ptr);
126 
127 /* Miscellaneous functions. */
128 int		PacketAliasCheckNewLink(void);
129 unsigned short
130 		PacketAliasInternetChecksum(unsigned short *_ptr, int _nbytes);
131 void		PacketAliasSetTarget(struct in_addr _target_addr);
132 
133 /* Transparent proxying routines. */
134 int		PacketAliasProxyRule(const char *_cmd);
135 
136 /* NEW API */
137 
138 /* Initialization and control functions. */
139 struct libalias *LibAliasInit(struct libalias *);
140 void		LibAliasSetAddress(struct libalias *, struct in_addr _addr);
141 void		LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num);
142 void		LibAliasSetSkinnyPort(struct libalias *, unsigned int _port);
143 unsigned int
144 		LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask);
145 void		LibAliasUninit(struct libalias *);
146 
147 /* Packet Handling functions. */
148 int		LibAliasIn (struct libalias *, char *_ptr, int _maxpacketsize);
149 int		LibAliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
150 int		LibAliasOutTry(struct libalias *, char *_ptr, int _maxpacketsize, int _create);
151 int		LibAliasUnaliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
152 
153 /* Port and address redirection functions. */
154 
155 int
156 LibAliasAddServer(struct libalias *, struct alias_link *_lnk,
157     struct in_addr _addr, unsigned short _port);
158 struct alias_link *
159 LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
160     struct in_addr _alias_addr);
161 int		LibAliasRedirectDynamic(struct libalias *, struct alias_link *_lnk);
162 void		LibAliasRedirectDelete(struct libalias *, struct alias_link *_lnk);
163 struct alias_link *
164 LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
165     unsigned short _src_port, struct in_addr _dst_addr,
166     unsigned short _dst_port, struct in_addr _alias_addr,
167     unsigned short _alias_port, unsigned char _proto);
168 struct alias_link *
169 LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
170     struct in_addr _dst_addr, struct in_addr _alias_addr,
171     unsigned char _proto);
172 
173 /* Fragment Handling functions. */
174 void		LibAliasFragmentIn(struct libalias *, char *_ptr, char *_ptr_fragment);
175 char           *LibAliasGetFragment(struct libalias *, char *_ptr);
176 int		LibAliasSaveFragment(struct libalias *, char *_ptr);
177 
178 /* Miscellaneous functions. */
179 int		LibAliasCheckNewLink(struct libalias *);
180 unsigned short
181 		LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes);
182 void		LibAliasSetTarget(struct libalias *, struct in_addr _target_addr);
183 
184 /* Transparent proxying routines. */
185 int		LibAliasProxyRule(struct libalias *, const char *_cmd);
186 
187 /* Module handling API */
188 int             LibAliasLoadModule(char *);
189 int             LibAliasUnLoadAllModule(void);
190 int             LibAliasRefreshModules(void);
191 
192 /* Mbuf helper function. */
193 struct mbuf    *m_megapullup(struct mbuf *, int);
194 
195 /*
196  * Mode flags and other constants.
197  */
198 
199 
200 /* Mode flags, set using PacketAliasSetMode() */
201 
202 /*
203  * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log
204  * every time a link is created or deleted.  This is useful for debugging.
205  */
206 #define	PKT_ALIAS_LOG			0x01
207 
208 /*
209  * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp,
210  * telnet or web servers will be prevented by the aliasing mechanism.
211  */
212 #define	PKT_ALIAS_DENY_INCOMING		0x02
213 
214 /*
215  * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the
216  * same port as they originated on.  This allows e.g. rsh to work *99% of the
217  * time*, but _not_ 100% (it will be slightly flakey instead of not working
218  * at all).  This mode bit is set by PacketAliasInit(), so it is a default
219  * mode of operation.
220  */
221 #define	PKT_ALIAS_SAME_PORTS		0x04
222 
223 /*
224  * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g.
225  * destination port and/or address is zero), the packet aliasing engine will
226  * attempt to allocate a socket for the aliasing port it chooses.  This will
227  * avoid interference with the host machine.  Fully specified links do not
228  * require this.  This bit is set after a call to PacketAliasInit(), so it is
229  * a default mode of operation.
230  */
231 #ifndef	NO_USE_SOCKETS
232 #define	PKT_ALIAS_USE_SOCKETS		0x08
233 #endif
234 /*-
235  * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with
236  * unregistered source addresses will be aliased.  Private
237  * addresses are those in the following ranges:
238  *
239  *		10.0.0.0     ->   10.255.255.255
240  *		172.16.0.0   ->   172.31.255.255
241  *		192.168.0.0  ->   192.168.255.255
242  */
243 #define	PKT_ALIAS_UNREGISTERED_ONLY	0x10
244 
245 /*
246  * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic
247  * aliasing links will be reset whenever PacketAliasSetAddress() changes the
248  * default aliasing address.  If the default aliasing address is left
249  * unchanged by this function call, then the table of dynamic aliasing links
250  * will be left intact.  This bit is set after a call to PacketAliasInit().
251  */
252 #define	PKT_ALIAS_RESET_ON_ADDR_CHANGE	0x20
253 
254 #ifndef NO_FW_PUNCH
255 /*
256  * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will
257  * create a 'hole' in the firewall to allow the transfers to work.  The
258  * ipfw rule number that the hole is created with is controlled by
259  * PacketAliasSetFWBase().  The hole will be attached to that
260  * particular alias_link, so when the link goes away the hole is deleted.
261  */
262 #define	PKT_ALIAS_PUNCH_FW		0x100
263 #endif
264 
265 /*
266  * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only
267  * transparent proxying is performed.
268  */
269 #define	PKT_ALIAS_PROXY_ONLY		0x40
270 
271 /*
272  * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and
273  * PacketAliasOut() are reversed.
274  */
275 #define	PKT_ALIAS_REVERSE		0x80
276 
277 /* Function return codes. */
278 #define	PKT_ALIAS_ERROR			-1
279 #define	PKT_ALIAS_OK			1
280 #define	PKT_ALIAS_IGNORED		2
281 #define	PKT_ALIAS_UNRESOLVED_FRAGMENT	3
282 #define	PKT_ALIAS_FOUND_HEADER_FRAGMENT	4
283 
284 #endif				/* !_ALIAS_H_ */
285 
286 /* lint -restore */
287