sys/dev/disk/dm: Always initialize target's status string
[dragonfly.git] / sys / net / libalias / alias.h
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: src/sys/netinet/libalias/alias.h,v 1.34.6.1 2008/11/25 02:59:29 kensmith Exp $
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
54 MALLOC_DECLARE(M_ALIAS);
55
56 #endif
57
58 /*
59  * The external interface to libalias, the packet aliasing engine.
60  *
61  * There are two sets of functions:
62  *
63  * PacketAlias*() the old API which doesn't take an instance pointer
64  * and therefore can only have one packet engine at a time.
65  *
66  * LibAlias*() the new API which takes as first argument a pointer to
67  * the instance of the packet aliasing engine.
68  *
69  * The functions otherwise correspond to each other one for one, except
70  * for the LibAliasUnaliasOut()/PacketUnaliasOut() function which were
71  * were misnamed in the old API.
72  */
73
74 /*
75  * The instance structure
76  */
77 struct libalias;
78
79 /* Data Structures
80
81         The fundamental data structure used in this program is
82         "struct alias_link".  Whenever a TCP connection is made,
83         a UDP datagram is sent out, or an ICMP echo request is made,
84         a link record is made (if it has not already been created).
85         The link record is identified by the source address/port
86         and the destination address/port. In the case of an ICMP
87         echo request, the source port is treated as being equivalent
88         with the 16-bit ID number of the ICMP packet.
89
90         The link record also can store some auxiliary data.  For
91         TCP connections that have had sequence and acknowledgment
92         modifications, data space is available to track these changes.
93         A state field is used to keep track in changes to the TCP
94         connection state.  ID numbers of fragments can also be
95         stored in the auxiliary space.  Pointers to unresolved
96         fragments can also be stored.
97
98         The link records support two independent chainings.  Lookup
99         tables for input and out tables hold the initial pointers
100         the link chains.  On input, the lookup table indexes on alias
101         port and link type.  On output, the lookup table indexes on
102         source address, destination address, source port, destination
103         port and link type.
104 */
105
106 struct ack_data_record {        /* used to save changes to ACK/sequence
107                                  * numbers */
108         u_long          ack_old;
109         u_long          ack_new;
110         int             delta;
111         int             active;
112 };
113
114 struct tcp_state {              /* Information about TCP connection             */
115         int             in;     /* State for outside -> inside                   */
116         int             out;    /* State for inside  -> outside                 */
117         int             index;  /* Index to ACK data array                               */
118         int             ack_modified;   /* Indicates whether ACK and
119                                          * sequence numbers */
120         /* been modified                                                   */
121 };
122
123 #define N_LINK_TCP_DATA   3     /* Number of distinct ACK number changes
124                                  * saved for a modified TCP stream */
125 struct tcp_dat {
126         struct tcp_state state;
127         struct ack_data_record ack[N_LINK_TCP_DATA];
128         int             fwhole; /* Which firewall record is used for this
129                                  * hole? */
130 };
131
132 struct server {                 /* LSNAT server pool (circular list) */
133         struct in_addr  addr;
134         u_short         port;
135         struct server  *next;
136 };
137
138 struct alias_link {             /* Main data structure */
139         struct libalias *la;
140         struct in_addr  src_addr;       /* Address and port information         */
141         struct in_addr  dst_addr;
142         struct in_addr  alias_addr;
143         struct in_addr  proxy_addr;
144         u_short         src_port;
145         u_short         dst_port;
146         u_short         alias_port;
147         u_short         proxy_port;
148         struct server  *server;
149
150         int             link_type;      /* Type of link: TCP, UDP, ICMP,
151                                          * proto, frag */
152
153 /* values for link_type */
154 #define LINK_ICMP                                        IPPROTO_ICMP
155 #define LINK_UDP                                          IPPROTO_UDP
156 #define LINK_TCP                                          IPPROTO_TCP
157 #define LINK_FRAGMENT_ID                          (IPPROTO_MAX + 1)
158 #define LINK_FRAGMENT_PTR                        (IPPROTO_MAX + 2)
159 #define LINK_ADDR                                        (IPPROTO_MAX + 3)
160 #define LINK_PPTP                                        (IPPROTO_MAX + 4)
161
162         int             flags;  /* indicates special characteristics   */
163         int             pflags; /* protocol-specific flags */
164
165 /* flag bits */
166 #define LINK_UNKNOWN_DEST_PORT   0x01
167 #define LINK_UNKNOWN_DEST_ADDR   0x02
168 #define LINK_PERMANENT                   0x04
169 #define LINK_PARTIALLY_SPECIFIED   0x03 /* logical-or of first two bits */
170 #define LINK_UNFIREWALLED                 0x08
171
172         int             timestamp;      /* Time link was last accessed           */
173         int             expire_time;    /* Expire time for link                         */
174 #ifndef NO_USE_SOCKETS
175         int             sockfd; /* socket descriptor                               */
176 #endif
177                         LIST_ENTRY      (alias_link) list_out;  /* Linked list of
178                                                                  * pointers for  */
179                         LIST_ENTRY      (alias_link) list_in;   /* input and output
180                                                                  * lookup tables  */
181
182         union {                 /* Auxiliary data                                         */
183                 char               *frag_ptr;
184                 struct in_addr  frag_addr;
185                 struct tcp_dat *tcp;
186         }               data;
187 };
188
189 /* OLD API */
190
191 /* Initialization and control functions. */
192 void            PacketAliasInit(void);
193 void            PacketAliasSetAddress(struct in_addr _addr);
194 void            PacketAliasSetFWBase(unsigned int _base, unsigned int _num);
195 void            PacketAliasSetSkinnyPort(unsigned int _port);
196 unsigned int
197                 PacketAliasSetMode(unsigned int _flags, unsigned int _mask);
198 void            PacketAliasUninit(void);
199
200 /* Packet Handling functions. */
201 int             PacketAliasIn(char *_ptr, int _maxpacketsize);
202 int             PacketAliasOut(char *_ptr, int _maxpacketsize);
203 int             PacketUnaliasOut(char *_ptr, int _maxpacketsize);
204
205 /* Port and address redirection functions. */
206
207
208 int
209 PacketAliasAddServer(struct alias_link *_lnk,
210         struct in_addr _addr, unsigned short _port);
211 struct alias_link *
212 PacketAliasRedirectAddr(struct in_addr _src_addr,
213         struct in_addr _alias_addr);
214 int             PacketAliasRedirectDynamic(struct alias_link *_lnk);
215 void            PacketAliasRedirectDelete(struct alias_link *_lnk);
216 struct alias_link *
217 PacketAliasRedirectPort(struct in_addr _src_addr,
218         unsigned short _src_port, struct in_addr _dst_addr,
219         unsigned short _dst_port, struct in_addr _alias_addr,
220         unsigned short _alias_port, unsigned char _proto);
221 struct alias_link *
222 PacketAliasRedirectProto(struct in_addr _src_addr,
223         struct in_addr _dst_addr, struct in_addr _alias_addr,
224         unsigned char _proto);
225
226 /* Fragment Handling functions. */
227 void            PacketAliasFragmentIn(char *_ptr, char *_ptr_fragment);
228 char               *PacketAliasGetFragment(char *_ptr);
229 int             PacketAliasSaveFragment(char *_ptr);
230
231 /* Miscellaneous functions. */
232 int             PacketAliasCheckNewLink(void);
233 unsigned short
234                 PacketAliasInternetChecksum(unsigned short *_ptr, int _nbytes);
235 void            PacketAliasSetTarget(struct in_addr _target_addr);
236
237 /* Transparent proxying routines. */
238 int             PacketAliasProxyRule(const char *_cmd);
239
240 /* NEW API */
241
242 /* Initialization and control functions. */
243 struct libalias *LibAliasInit(struct libalias *);
244 void            LibAliasSetAddress(struct libalias *, struct in_addr _addr);
245 void            LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num);
246 void            LibAliasSetSkinnyPort(struct libalias *, unsigned int _port);
247 unsigned int
248                 LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask);
249 void            LibAliasUninit(struct libalias *);
250
251 /* Packet Handling functions. */
252 int             LibAliasIn (struct libalias *, char *_ptr, int _maxpacketsize);
253 int             LibAliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
254 int             LibAliasOutTry(struct libalias *, char *_ptr, int _maxpacketsize, int _create);
255 int             LibAliasUnaliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
256
257 /* Port and address redirection functions. */
258
259 int
260 LibAliasAddServer(struct libalias *, struct alias_link *_lnk,
261         struct in_addr _addr, unsigned short _port);
262 struct alias_link *
263 LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
264         struct in_addr _alias_addr);
265 int             LibAliasRedirectDynamic(struct libalias *, struct alias_link *_lnk);
266 void            LibAliasRedirectDelete(struct libalias *, struct alias_link *_lnk);
267 struct alias_link *
268 LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
269         unsigned short _src_port, struct in_addr _dst_addr,
270         unsigned short _dst_port, struct in_addr _alias_addr,
271         unsigned short _alias_port, unsigned char _proto);
272 struct alias_link *
273 LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
274         struct in_addr _dst_addr, struct in_addr _alias_addr,
275         unsigned char _proto);
276
277 /* Fragment Handling functions. */
278 void            LibAliasFragmentIn(struct libalias *, char *_ptr, char *_ptr_fragment);
279 char               *LibAliasGetFragment(struct libalias *, char *_ptr);
280 int             LibAliasSaveFragment(struct libalias *, char *_ptr);
281
282 /* Miscellaneous functions. */
283 int             LibAliasCheckNewLink(struct libalias *);
284 unsigned short
285                 LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes);
286 void            LibAliasSetTarget(struct libalias *, struct in_addr _target_addr);
287
288 /* Transparent proxying routines. */
289 int             LibAliasProxyRule(struct libalias *, const char *_cmd);
290
291 /* Module handling API */
292 int                      LibAliasLoadModule(char *);
293 int                      LibAliasUnLoadAllModule(void);
294 int                      LibAliasRefreshModules(void);
295
296 /* Mbuf helper function. */
297 struct mbuf     *m_megapullup(struct mbuf *, int);
298
299 /*
300  * Mode flags and other constants.
301  */
302
303
304 /* Mode flags, set using PacketAliasSetMode() */
305
306 /*
307  * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log
308  * every time a link is created or deleted.  This is useful for debugging.
309  */
310 #define PKT_ALIAS_LOG                   0x01
311
312 /*
313  * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp,
314  * telnet or web servers will be prevented by the aliasing mechanism.
315  */
316 #define PKT_ALIAS_DENY_INCOMING         0x02
317
318 /*
319  * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the
320  * same port as they originated on.  This allows e.g. rsh to work *99% of the
321  * time*, but _not_ 100% (it will be slightly flakey instead of not working
322  * at all).  This mode bit is set by PacketAliasInit(), so it is a default
323  * mode of operation.
324  */
325 #define PKT_ALIAS_SAME_PORTS            0x04
326
327 /*
328  * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g.
329  * destination port and/or address is zero), the packet aliasing engine will
330  * attempt to allocate a socket for the aliasing port it chooses.  This will
331  * avoid interference with the host machine.  Fully specified links do not
332  * require this.  This bit is set after a call to PacketAliasInit(), so it is
333  * a default mode of operation.
334  */
335 #ifndef NO_USE_SOCKETS
336 #define PKT_ALIAS_USE_SOCKETS           0x08
337 #endif
338 /*-
339  * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with
340  * unregistered source addresses will be aliased.  Private
341  * addresses are those in the following ranges:
342  *
343  *              10.0.0.0         ->   10.255.255.255
344  *              172.16.0.0   ->   172.31.255.255
345  *              192.168.0.0  ->   192.168.255.255
346  */
347 #define PKT_ALIAS_UNREGISTERED_ONLY     0x10
348
349 /*
350  * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic
351  * aliasing links will be reset whenever PacketAliasSetAddress() changes the
352  * default aliasing address.  If the default aliasing address is left
353  * unchanged by this function call, then the table of dynamic aliasing links
354  * will be left intact.  This bit is set after a call to PacketAliasInit().
355  */
356 #define PKT_ALIAS_RESET_ON_ADDR_CHANGE  0x20
357
358 #ifndef NO_FW_PUNCH
359 /*
360  * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will
361  * create a 'hole' in the firewall to allow the transfers to work.  The
362  * ipfw rule number that the hole is created with is controlled by
363  * PacketAliasSetFWBase().  The hole will be attached to that
364  * particular alias_link, so when the link goes away the hole is deleted.
365  */
366 #define PKT_ALIAS_PUNCH_FW              0x100
367 #endif
368
369 /*
370  * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only
371  * transparent proxying is performed.
372  */
373 #define PKT_ALIAS_PROXY_ONLY            0x40
374
375 /*
376  * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and
377  * PacketAliasOut() are reversed.
378  */
379 #define PKT_ALIAS_REVERSE               0x80
380
381 /* Function return codes. */
382 #define PKT_ALIAS_ERROR                 -1
383 #define PKT_ALIAS_OK                    1
384 #define PKT_ALIAS_IGNORED               2
385 #define PKT_ALIAS_UNRESOLVED_FRAGMENT   3
386 #define PKT_ALIAS_FOUND_HEADER_FRAGMENT 4
387
388
389
390 #endif                          /* !_ALIAS_H_ */
391
392 /* lint -restore */