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