alias.h revision 145926
191094Sdes/* lint -save -library Flexelint comment for external headers */
2115619Sdes
3228690Sdes/*-
491094Sdes * Copyright (c) 2001 Charles Mott <cm@linktel.net>
591094Sdes * All rights reserved.
691094Sdes *
799158Sdes * Redistribution and use in source and binary forms, with or without
899158Sdes * modification, are permitted provided that the following conditions
999158Sdes * are met:
1091094Sdes * 1. Redistributions of source code must retain the above copyright
1191094Sdes *    notice, this list of conditions and the following disclaimer.
1291094Sdes * 2. Redistributions in binary form must reproduce the above copyright
1391094Sdes *    notice, this list of conditions and the following disclaimer in the
1491094Sdes *    documentation and/or other materials provided with the distribution.
1591094Sdes *
1691094Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1791094Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1891094Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1991094Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2091094Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2191094Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2291094Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2391094Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2491094Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2591094Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2691094Sdes * SUCH DAMAGE.
2791094Sdes *
2891094Sdes * $FreeBSD: head/sys/netinet/libalias/alias.h 145926 2005-05-05 20:25:12Z glebius $
2991094Sdes */
3091094Sdes
3191094Sdes/*-
3291094Sdes * Alias.h defines the outside world interfaces for the packet aliasing
3391094Sdes * software.
3491094Sdes *
35255376Sdes * This software is placed into the public domain with no restrictions on its
3691094Sdes * distribution.
3791094Sdes */
38228690Sdes
39228690Sdes#ifndef _ALIAS_H_
40228690Sdes#define	_ALIAS_H_
41228690Sdes
4291094Sdes/*
4391094Sdes * The external interface to libalias, the packet aliasing engine.
4491094Sdes *
4591094Sdes * There are two sets of functions:
4691094Sdes *
4791094Sdes * PacketAlias*() the old API which doesn't take an instance pointer
4891094Sdes * and therefore can only have one packet engine at a time.
4991094Sdes *
5091094Sdes * LibAlias*() the new API which takes as first argument a pointer to
5191094Sdes * the instance of the packet aliasing engine.
5291094Sdes *
5391094Sdes * The functions otherwise correspond to each other one for one, except
5491094Sdes * for the LibAliasUnaliasOut()/PacketUnaliasOut() function which were
5591094Sdes * were misnamed in the old API.
56114536Sdes */
5791094Sdes
5891094Sdes/*
5991094Sdes * The instance structure
60110558Sdes */
6191094Sdesstruct libalias;
6291094Sdes
63110558Sdes/*
6491094Sdes * An anonymous structure, a pointer to which is returned from
65108794Sdes * PacketAliasRedirectAddr(), PacketAliasRedirectPort() or
6691094Sdes * PacketAliasRedirectProto(), passed to PacketAliasAddServer(),
67108794Sdes * and freed by PacketAliasRedirectDelete().
68115619Sdes */
69108794Sdesstruct alias_link;
70110558Sdes
71110558Sdes
72110558Sdes/* OLD API */
73110558Sdes
74110558Sdes/* Initialization and control functions. */
75110558Sdesvoid		PacketAliasInit(void);
76110558Sdesvoid		PacketAliasSetAddress(struct in_addr _addr);
7791094Sdesvoid		PacketAliasSetFWBase(unsigned int _base, unsigned int _num);
7891100Sdesvoid		PacketAliasSetSkinnyPort(unsigned int _port);
7991100Sdesunsigned int
8091100Sdes		PacketAliasSetMode(unsigned int _flags, unsigned int _mask);
8191100Sdesvoid		PacketAliasUninit(void);
8291100Sdes
8391100Sdes/* Packet Handling functions. */
8491100Sdesint		PacketAliasIn(char *_ptr, int _maxpacketsize);
8591100Sdesint		PacketAliasOut(char *_ptr, int _maxpacketsize);
8691100Sdesint		PacketUnaliasOut(char *_ptr, int _maxpacketsize);
8791100Sdes
88/* Port and address redirection functions. */
89
90
91int
92PacketAliasAddServer(struct alias_link *_lnk,
93    struct in_addr _addr, unsigned short _port);
94struct alias_link *
95PacketAliasRedirectAddr(struct in_addr _src_addr,
96    struct in_addr _alias_addr);
97int		PacketAliasRedirectDynamic(struct alias_link *_lnk);
98void		PacketAliasRedirectDelete(struct alias_link *_lnk);
99struct alias_link *
100PacketAliasRedirectPort(struct in_addr _src_addr,
101    unsigned short _src_port, struct in_addr _dst_addr,
102    unsigned short _dst_port, struct in_addr _alias_addr,
103    unsigned short _alias_port, unsigned char _proto);
104struct alias_link *
105PacketAliasRedirectProto(struct in_addr _src_addr,
106    struct in_addr _dst_addr, struct in_addr _alias_addr,
107    unsigned char _proto);
108
109/* Fragment Handling functions. */
110void		PacketAliasFragmentIn(char *_ptr, char *_ptr_fragment);
111char           *PacketAliasGetFragment(char *_ptr);
112int		PacketAliasSaveFragment(char *_ptr);
113
114/* Miscellaneous functions. */
115int		PacketAliasCheckNewLink(void);
116unsigned short
117		PacketAliasInternetChecksum(unsigned short *_ptr, int _nbytes);
118void		PacketAliasSetTarget(struct in_addr _target_addr);
119
120/* Transparent proxying routines. */
121int		PacketAliasProxyRule(const char *_cmd);
122
123/* NEW API */
124
125/* Initialization and control functions. */
126struct libalias *LibAliasInit(struct libalias *);
127void		LibAliasSetAddress(struct libalias *, struct in_addr _addr);
128void		LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num);
129void		LibAliasSetSkinnyPort(struct libalias *, unsigned int _port);
130unsigned int
131		LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask);
132void		LibAliasUninit(struct libalias *);
133
134/* Packet Handling functions. */
135int		LibAliasIn (struct libalias *, char *_ptr, int _maxpacketsize);
136int		LibAliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
137int		LibAliasOutTry(struct libalias *, char *_ptr, int _maxpacketsize, int _create);
138int		LibAliasUnaliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
139
140/* Port and address redirection functions. */
141
142int
143LibAliasAddServer(struct libalias *, struct alias_link *_lnk,
144    struct in_addr _addr, unsigned short _port);
145struct alias_link *
146LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
147    struct in_addr _alias_addr);
148int		LibAliasRedirectDynamic(struct libalias *, struct alias_link *_lnk);
149void		LibAliasRedirectDelete(struct libalias *, struct alias_link *_lnk);
150struct alias_link *
151LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
152    unsigned short _src_port, struct in_addr _dst_addr,
153    unsigned short _dst_port, struct in_addr _alias_addr,
154    unsigned short _alias_port, unsigned char _proto);
155struct alias_link *
156LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
157    struct in_addr _dst_addr, struct in_addr _alias_addr,
158    unsigned char _proto);
159
160/* Fragment Handling functions. */
161void		LibAliasFragmentIn(struct libalias *, char *_ptr, char *_ptr_fragment);
162char           *LibAliasGetFragment(struct libalias *, char *_ptr);
163int		LibAliasSaveFragment(struct libalias *, char *_ptr);
164
165/* Miscellaneous functions. */
166int		LibAliasCheckNewLink(struct libalias *);
167unsigned short
168		LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes);
169void		LibAliasSetTarget(struct libalias *, struct in_addr _target_addr);
170
171/* Transparent proxying routines. */
172int		LibAliasProxyRule(struct libalias *, const char *_cmd);
173
174
175/*
176 * Mode flags and other constants.
177 */
178
179
180/* Mode flags, set using PacketAliasSetMode() */
181
182/*
183 * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log
184 * every time a link is created or deleted.  This is useful for debugging.
185 */
186#ifndef	NO_LOGGING
187#define	PKT_ALIAS_LOG			0x01
188#endif
189
190/*
191 * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp,
192 * telnet or web servers will be prevented by the aliasing mechanism.
193 */
194#define	PKT_ALIAS_DENY_INCOMING		0x02
195
196/*
197 * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the
198 * same port as they originated on.  This allows e.g. rsh to work *99% of the
199 * time*, but _not_ 100% (it will be slightly flakey instead of not working
200 * at all).  This mode bit is set by PacketAliasInit(), so it is a default
201 * mode of operation.
202 */
203#define	PKT_ALIAS_SAME_PORTS		0x04
204
205/*
206 * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g.
207 * destination port and/or address is zero), the packet aliasing engine will
208 * attempt to allocate a socket for the aliasing port it chooses.  This will
209 * avoid interference with the host machine.  Fully specified links do not
210 * require this.  This bit is set after a call to PacketAliasInit(), so it is
211 * a default mode of operation.
212 */
213#ifndef	NO_USE_SOCKETS
214#define	PKT_ALIAS_USE_SOCKETS		0x08
215#endif
216/*-
217 * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with
218 * unregistered source addresses will be aliased.  Private
219 * addresses are those in the following ranges:
220 *
221 *		10.0.0.0     ->   10.255.255.255
222 *		172.16.0.0   ->   172.31.255.255
223 *		192.168.0.0  ->   192.168.255.255
224 */
225#define	PKT_ALIAS_UNREGISTERED_ONLY	0x10
226
227/*
228 * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic
229 * aliasing links will be reset whenever PacketAliasSetAddress() changes the
230 * default aliasing address.  If the default aliasing address is left
231 * unchanged by this function call, then the table of dynamic aliasing links
232 * will be left intact.  This bit is set after a call to PacketAliasInit().
233 */
234#define	PKT_ALIAS_RESET_ON_ADDR_CHANGE	0x20
235
236#ifndef NO_FW_PUNCH
237/*
238 * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will
239 * create a 'hole' in the firewall to allow the transfers to work.  The
240 * ipfw rule number that the hole is created with is controlled by
241 * PacketAliasSetFWBase().  The hole will be attached to that
242 * particular alias_link, so when the link goes away the hole is deleted.
243 */
244#define	PKT_ALIAS_PUNCH_FW		0x100
245#endif
246
247/*
248 * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only
249 * transparent proxying is performed.
250 */
251#define	PKT_ALIAS_PROXY_ONLY		0x40
252
253/*
254 * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and
255 * PacketAliasOut() are reversed.
256 */
257#define	PKT_ALIAS_REVERSE		0x80
258
259/* Function return codes. */
260#define	PKT_ALIAS_ERROR			-1
261#define	PKT_ALIAS_OK			1
262#define	PKT_ALIAS_IGNORED		2
263#define	PKT_ALIAS_UNRESOLVED_FRAGMENT	3
264#define	PKT_ALIAS_FOUND_HEADER_FRAGMENT	4
265
266#endif				/* !_ALIAS_H_ */
267
268/* lint -restore */
269