xref: /freebsd/sys/netinet/libalias/alias_db.h (revision 61bf830cbb260c2a046cb44421d319184393e028)
1f70c98a2SLutz Donnerhacke /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3f70c98a2SLutz Donnerhacke  *
4f70c98a2SLutz Donnerhacke  * Copyright (c) 2001 Charles Mott <cm@linktel.net>
5f70c98a2SLutz Donnerhacke  * All rights reserved.
6f70c98a2SLutz Donnerhacke  *
7f70c98a2SLutz Donnerhacke  * Redistribution and use in source and binary forms, with or without
8f70c98a2SLutz Donnerhacke  * modification, are permitted provided that the following conditions
9f70c98a2SLutz Donnerhacke  * are met:
10f70c98a2SLutz Donnerhacke  * 1. Redistributions of source code must retain the above copyright
11f70c98a2SLutz Donnerhacke  *    notice, this list of conditions and the following disclaimer.
12f70c98a2SLutz Donnerhacke  * 2. Redistributions in binary form must reproduce the above copyright
13f70c98a2SLutz Donnerhacke  *    notice, this list of conditions and the following disclaimer in the
14f70c98a2SLutz Donnerhacke  *    documentation and/or other materials provided with the distribution.
15f70c98a2SLutz Donnerhacke  *
16f70c98a2SLutz Donnerhacke  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f70c98a2SLutz Donnerhacke  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f70c98a2SLutz Donnerhacke  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f70c98a2SLutz Donnerhacke  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f70c98a2SLutz Donnerhacke  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f70c98a2SLutz Donnerhacke  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f70c98a2SLutz Donnerhacke  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f70c98a2SLutz Donnerhacke  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f70c98a2SLutz Donnerhacke  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f70c98a2SLutz Donnerhacke  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f70c98a2SLutz Donnerhacke  * SUCH DAMAGE.
27f70c98a2SLutz Donnerhacke  */
28f70c98a2SLutz Donnerhacke 
29f70c98a2SLutz Donnerhacke /*
30f70c98a2SLutz Donnerhacke     Alias_db.c encapsulates all data structures used for storing
31f70c98a2SLutz Donnerhacke     packet aliasing data.  Other parts of the aliasing software
32f70c98a2SLutz Donnerhacke     access data through functions provided in this file.
33f70c98a2SLutz Donnerhacke 
34f70c98a2SLutz Donnerhacke     Data storage is based on the notion of a "link", which is
35f70c98a2SLutz Donnerhacke     established for ICMP echo/reply packets, UDP datagrams and
36f70c98a2SLutz Donnerhacke     TCP stream connections.  A link stores the original source
37f70c98a2SLutz Donnerhacke     and destination addresses.  For UDP and TCP, it also stores
38f70c98a2SLutz Donnerhacke     source and destination port numbers, as well as an alias
39f70c98a2SLutz Donnerhacke     port number.  Links are also used to store information about
40f70c98a2SLutz Donnerhacke     fragments.
41f70c98a2SLutz Donnerhacke 
42f70c98a2SLutz Donnerhacke     There is a facility for sweeping through and deleting old
43f70c98a2SLutz Donnerhacke     links as new packets are sent through.  A simple timeout is
44f70c98a2SLutz Donnerhacke     used for ICMP and UDP links.  TCP links are left alone unless
45f70c98a2SLutz Donnerhacke     there is an incomplete connection, in which case the link
46f70c98a2SLutz Donnerhacke     can be deleted after a certain amount of time.
47f70c98a2SLutz Donnerhacke 
48f70c98a2SLutz Donnerhacke     Initial version: August, 1996  (cjm)
49f70c98a2SLutz Donnerhacke 
50f70c98a2SLutz Donnerhacke     Version 1.4: September 16, 1996 (cjm)
51f70c98a2SLutz Donnerhacke 	Facility for handling incoming links added.
52f70c98a2SLutz Donnerhacke 
53f70c98a2SLutz Donnerhacke     Version 1.6: September 18, 1996 (cjm)
54f70c98a2SLutz Donnerhacke 	ICMP data handling simplified.
55f70c98a2SLutz Donnerhacke 
56f70c98a2SLutz Donnerhacke     Version 1.7: January 9, 1997 (cjm)
57f70c98a2SLutz Donnerhacke 	Fragment handling simplified.
58f70c98a2SLutz Donnerhacke 	Saves pointers for unresolved fragments.
59f70c98a2SLutz Donnerhacke 	Permits links for unspecified remote ports
60f70c98a2SLutz Donnerhacke 	  or unspecified remote addresses.
61f70c98a2SLutz Donnerhacke 	Fixed bug which did not properly zero port
62f70c98a2SLutz Donnerhacke 	  table entries after a link was deleted.
63f70c98a2SLutz Donnerhacke 	Cleaned up some obsolete comments.
64f70c98a2SLutz Donnerhacke 
65f70c98a2SLutz Donnerhacke     Version 1.8: January 14, 1997 (cjm)
66f70c98a2SLutz Donnerhacke 	Fixed data type error in StartPoint().
67f70c98a2SLutz Donnerhacke 	(This error did not exist prior to v1.7
68f70c98a2SLutz Donnerhacke 	and was discovered and fixed by Ari Suutari)
69f70c98a2SLutz Donnerhacke 
70f70c98a2SLutz Donnerhacke     Version 1.9: February 1, 1997
71f70c98a2SLutz Donnerhacke 	Optionally, connections initiated from packet aliasing host
72f70c98a2SLutz Donnerhacke 	machine will will not have their port number aliased unless it
73f70c98a2SLutz Donnerhacke 	conflicts with an aliasing port already being used. (cjm)
74f70c98a2SLutz Donnerhacke 
75f70c98a2SLutz Donnerhacke 	All options earlier being #ifdef'ed are now available through
76f70c98a2SLutz Donnerhacke 	a new interface, SetPacketAliasMode().  This allows run time
77f70c98a2SLutz Donnerhacke 	control (which is now available in PPP+pktAlias through the
78f70c98a2SLutz Donnerhacke 	'alias' keyword). (ee)
79f70c98a2SLutz Donnerhacke 
80f70c98a2SLutz Donnerhacke 	Added ability to create an alias port without
81f70c98a2SLutz Donnerhacke 	either destination address or port specified.
82f70c98a2SLutz Donnerhacke 	port type = ALIAS_PORT_UNKNOWN_DEST_ALL (ee)
83f70c98a2SLutz Donnerhacke 
84f70c98a2SLutz Donnerhacke 	Removed K&R style function headers
85f70c98a2SLutz Donnerhacke 	and general cleanup. (ee)
86f70c98a2SLutz Donnerhacke 
87f70c98a2SLutz Donnerhacke 	Added packetAliasMode to replace compiler #defines's (ee)
88f70c98a2SLutz Donnerhacke 
89f70c98a2SLutz Donnerhacke 	Allocates sockets for partially specified
90f70c98a2SLutz Donnerhacke 	ports if ALIAS_USE_SOCKETS defined. (cjm)
91f70c98a2SLutz Donnerhacke 
92f70c98a2SLutz Donnerhacke     Version 2.0: March, 1997
93f70c98a2SLutz Donnerhacke 	SetAliasAddress() will now clean up alias links
94f70c98a2SLutz Donnerhacke 	if the aliasing address is changed. (cjm)
95f70c98a2SLutz Donnerhacke 
96f70c98a2SLutz Donnerhacke 	PacketAliasPermanentLink() function added to support permanent
97f70c98a2SLutz Donnerhacke 	links.  (J. Fortes suggested the need for this.)
98f70c98a2SLutz Donnerhacke 	Examples:
99f70c98a2SLutz Donnerhacke 
100f70c98a2SLutz Donnerhacke 	(192.168.0.1, port 23)  <-> alias port 6002, unknown dest addr/port
101f70c98a2SLutz Donnerhacke 
102f70c98a2SLutz Donnerhacke 	(192.168.0.2, port 21)  <-> alias port 3604, known dest addr
103f70c98a2SLutz Donnerhacke 						     unknown dest port
104f70c98a2SLutz Donnerhacke 
105f70c98a2SLutz Donnerhacke 	These permanent links allow for incoming connections to
106f70c98a2SLutz Donnerhacke 	machines on the local network.  They can be given with a
107f70c98a2SLutz Donnerhacke 	user-chosen amount of specificity, with increasing specificity
108f70c98a2SLutz Donnerhacke 	meaning more security. (cjm)
109f70c98a2SLutz Donnerhacke 
110f70c98a2SLutz Donnerhacke 	Quite a bit of rework to the basic engine.  The portTable[]
111f70c98a2SLutz Donnerhacke 	array, which kept track of which ports were in use was replaced
112f70c98a2SLutz Donnerhacke 	by a table/linked list structure. (cjm)
113f70c98a2SLutz Donnerhacke 
114f70c98a2SLutz Donnerhacke 	SetExpire() function added. (cjm)
115f70c98a2SLutz Donnerhacke 
116f70c98a2SLutz Donnerhacke 	DeleteLink() no longer frees memory association with a pointer
117f70c98a2SLutz Donnerhacke 	to a fragment (this bug was first recognized by E. Eklund in
118f70c98a2SLutz Donnerhacke 	v1.9).
119f70c98a2SLutz Donnerhacke 
120f70c98a2SLutz Donnerhacke     Version 2.1: May, 1997 (cjm)
121f70c98a2SLutz Donnerhacke 	Packet aliasing engine reworked so that it can handle
122f70c98a2SLutz Donnerhacke 	multiple external addresses rather than just a single
123f70c98a2SLutz Donnerhacke 	host address.
124f70c98a2SLutz Donnerhacke 
125f70c98a2SLutz Donnerhacke 	PacketAliasRedirectPort() and PacketAliasRedirectAddr()
126f70c98a2SLutz Donnerhacke 	added to the API.  The first function is a more generalized
127f70c98a2SLutz Donnerhacke 	version of PacketAliasPermanentLink().  The second function
128f70c98a2SLutz Donnerhacke 	implements static network address translation.
129f70c98a2SLutz Donnerhacke 
130f70c98a2SLutz Donnerhacke     Version 3.2: July, 2000 (salander and satoh)
131f70c98a2SLutz Donnerhacke 	Added FindNewPortGroup to get contiguous range of port values.
132f70c98a2SLutz Donnerhacke 
133f70c98a2SLutz Donnerhacke 	Added QueryUdpTcpIn and QueryUdpTcpOut to look for an aliasing
134f70c98a2SLutz Donnerhacke 	link but not actually add one.
135f70c98a2SLutz Donnerhacke 
136f70c98a2SLutz Donnerhacke 	Added FindRtspOut, which is closely derived from FindUdpTcpOut,
137f70c98a2SLutz Donnerhacke 	except that the alias port (from FindNewPortGroup) is provided
138f70c98a2SLutz Donnerhacke 	as input.
139f70c98a2SLutz Donnerhacke 
140f70c98a2SLutz Donnerhacke     See HISTORY file for additional revisions.
141f70c98a2SLutz Donnerhacke */
142f70c98a2SLutz Donnerhacke 
143f70c98a2SLutz Donnerhacke #ifndef _ALIAS_DB_H_
144f70c98a2SLutz Donnerhacke #define _ALIAS_DB_H_
145f70c98a2SLutz Donnerhacke 
146f70c98a2SLutz Donnerhacke 
147f70c98a2SLutz Donnerhacke /*
148f70c98a2SLutz Donnerhacke    Constants (note: constants are also defined
149f70c98a2SLutz Donnerhacke 	      near relevant functions or structs)
150f70c98a2SLutz Donnerhacke */
151f70c98a2SLutz Donnerhacke 
152f70c98a2SLutz Donnerhacke /* Timeouts (in seconds) for different link types */
153f70c98a2SLutz Donnerhacke #define ICMP_EXPIRE_TIME             60
154f70c98a2SLutz Donnerhacke #define UDP_EXPIRE_TIME              60
155f70c98a2SLutz Donnerhacke #define PROTO_EXPIRE_TIME            60
156f70c98a2SLutz Donnerhacke #define FRAGMENT_ID_EXPIRE_TIME      10
157f70c98a2SLutz Donnerhacke #define FRAGMENT_PTR_EXPIRE_TIME     30
158f70c98a2SLutz Donnerhacke 
159f70c98a2SLutz Donnerhacke /* TCP link expire time for different cases */
160f70c98a2SLutz Donnerhacke /* When the link has been used and closed - minimal grace time to
161f70c98a2SLutz Donnerhacke    allow ACKs and potential re-connect in FTP (XXX - is this allowed?)  */
162f70c98a2SLutz Donnerhacke #ifndef TCP_EXPIRE_DEAD
163f70c98a2SLutz Donnerhacke #define TCP_EXPIRE_DEAD           10
164f70c98a2SLutz Donnerhacke #endif
165f70c98a2SLutz Donnerhacke 
166f70c98a2SLutz Donnerhacke /* When the link has been used and closed on one side - the other side
167f70c98a2SLutz Donnerhacke    is allowed to still send data */
168f70c98a2SLutz Donnerhacke #ifndef TCP_EXPIRE_SINGLEDEAD
169f70c98a2SLutz Donnerhacke #define TCP_EXPIRE_SINGLEDEAD     90
170f70c98a2SLutz Donnerhacke #endif
171f70c98a2SLutz Donnerhacke 
172f70c98a2SLutz Donnerhacke /* When the link isn't yet up */
173f70c98a2SLutz Donnerhacke #ifndef TCP_EXPIRE_INITIAL
174f70c98a2SLutz Donnerhacke #define TCP_EXPIRE_INITIAL       300
175f70c98a2SLutz Donnerhacke #endif
176f70c98a2SLutz Donnerhacke 
177f70c98a2SLutz Donnerhacke /* When the link is up */
178f70c98a2SLutz Donnerhacke #ifndef TCP_EXPIRE_CONNECTED
179f70c98a2SLutz Donnerhacke #define TCP_EXPIRE_CONNECTED   86400
180f70c98a2SLutz Donnerhacke #endif
181f70c98a2SLutz Donnerhacke 
182f70c98a2SLutz Donnerhacke /* Dummy port number codes used for FindLinkIn/Out() and AddLink().
183f70c98a2SLutz Donnerhacke    These constants can be anything except zero, which indicates an
184f70c98a2SLutz Donnerhacke    unknown port number. */
185f70c98a2SLutz Donnerhacke 
186f70c98a2SLutz Donnerhacke #define NO_DEST_PORT     1
187f70c98a2SLutz Donnerhacke #define NO_SRC_PORT      1
188f70c98a2SLutz Donnerhacke 
189f70c98a2SLutz Donnerhacke /* Matches any/unknown address in FindLinkIn/Out() and AddLink(). */
190f70c98a2SLutz Donnerhacke static struct in_addr const ANY_ADDR = { INADDR_ANY };
191f70c98a2SLutz Donnerhacke 
192f70c98a2SLutz Donnerhacke /* Data Structures
193f70c98a2SLutz Donnerhacke 
194f70c98a2SLutz Donnerhacke     The fundamental data structure used in this program is
195f70c98a2SLutz Donnerhacke     "struct alias_link".  Whenever a TCP connection is made,
196f70c98a2SLutz Donnerhacke     a UDP datagram is sent out, or an ICMP echo request is made,
197f70c98a2SLutz Donnerhacke     a link record is made (if it has not already been created).
198f70c98a2SLutz Donnerhacke     The link record is identified by the source address/port
199f70c98a2SLutz Donnerhacke     and the destination address/port. In the case of an ICMP
200f70c98a2SLutz Donnerhacke     echo request, the source port is treated as being equivalent
201f70c98a2SLutz Donnerhacke     with the 16-bit ID number of the ICMP packet.
202f70c98a2SLutz Donnerhacke 
203f70c98a2SLutz Donnerhacke     The link record also can store some auxiliary data.  For
204f70c98a2SLutz Donnerhacke     TCP connections that have had sequence and acknowledgment
205f70c98a2SLutz Donnerhacke     modifications, data space is available to track these changes.
206f70c98a2SLutz Donnerhacke     A state field is used to keep track in changes to the TCP
207f70c98a2SLutz Donnerhacke     connection state.  ID numbers of fragments can also be
208f70c98a2SLutz Donnerhacke     stored in the auxiliary space.  Pointers to unresolved
209f70c98a2SLutz Donnerhacke     fragments can also be stored.
210f70c98a2SLutz Donnerhacke 
211*61bf830cSDamjan Jovanovic     The link records support several independent chainings.  Lookup
212f70c98a2SLutz Donnerhacke     tables for input and out tables hold the initial pointers
213f70c98a2SLutz Donnerhacke     the link chains.  On input, the lookup table indexes on alias
214f70c98a2SLutz Donnerhacke     port and link type.  On output, the lookup table indexes on
215f70c98a2SLutz Donnerhacke     source address, destination address, source port, destination
216*61bf830cSDamjan Jovanovic     port and link type. A internal_endpoint table is used for
217*61bf830cSDamjan Jovanovic     endpoint-independent mapping, and indexes on source address,
218*61bf830cSDamjan Jovanovic     source port and link type.
219f70c98a2SLutz Donnerhacke */
220f70c98a2SLutz Donnerhacke 
221f70c98a2SLutz Donnerhacke /* used to save changes to ACK/sequence numbers */
222f70c98a2SLutz Donnerhacke struct ack_data_record {
223f70c98a2SLutz Donnerhacke 	u_long		ack_old;
224f70c98a2SLutz Donnerhacke 	u_long		ack_new;
225f70c98a2SLutz Donnerhacke 	int		delta;
226f70c98a2SLutz Donnerhacke 	int		active;
227f70c98a2SLutz Donnerhacke };
228f70c98a2SLutz Donnerhacke 
229f70c98a2SLutz Donnerhacke /* Information about TCP connection */
230f70c98a2SLutz Donnerhacke struct tcp_state {
231f70c98a2SLutz Donnerhacke 	int		in;	/* State for outside -> inside */
232f70c98a2SLutz Donnerhacke 	int		out;	/* State for inside  -> outside */
233f70c98a2SLutz Donnerhacke 	int		index;	/* Index to ACK data array */
234f70c98a2SLutz Donnerhacke 	/* Indicates whether ACK and sequence numbers been modified */
235f70c98a2SLutz Donnerhacke 	int		ack_modified;
236f70c98a2SLutz Donnerhacke };
237f70c98a2SLutz Donnerhacke 
238f70c98a2SLutz Donnerhacke /* Number of distinct ACK number changes
239f70c98a2SLutz Donnerhacke  * saved for a modified TCP stream */
240f70c98a2SLutz Donnerhacke #define N_LINK_TCP_DATA   3
241f70c98a2SLutz Donnerhacke struct tcp_dat {
242f70c98a2SLutz Donnerhacke 	struct tcp_state state;
243f70c98a2SLutz Donnerhacke 	struct ack_data_record ack[N_LINK_TCP_DATA];
244f70c98a2SLutz Donnerhacke 	/* Which firewall record is used for this hole? */
245f70c98a2SLutz Donnerhacke 	int		fwhole;
246f70c98a2SLutz Donnerhacke };
247f70c98a2SLutz Donnerhacke 
248f70c98a2SLutz Donnerhacke /* LSNAT server pool (circular list) */
249f70c98a2SLutz Donnerhacke struct server {
250f70c98a2SLutz Donnerhacke 	struct in_addr	addr;
251f70c98a2SLutz Donnerhacke 	u_short		port;
252f70c98a2SLutz Donnerhacke 	struct server  *next;
253f70c98a2SLutz Donnerhacke };
254f70c98a2SLutz Donnerhacke 
255f70c98a2SLutz Donnerhacke /* Main data structure */
256f70c98a2SLutz Donnerhacke struct alias_link {
257f70c98a2SLutz Donnerhacke 	struct libalias *la;
258f70c98a2SLutz Donnerhacke 	/* Address and port information */
259f70c98a2SLutz Donnerhacke 	struct in_addr	src_addr;
260f70c98a2SLutz Donnerhacke 	struct in_addr	dst_addr;
261f70c98a2SLutz Donnerhacke 	struct in_addr	alias_addr;
262f70c98a2SLutz Donnerhacke 	struct in_addr	proxy_addr;
263f70c98a2SLutz Donnerhacke 	u_short		src_port;
264f70c98a2SLutz Donnerhacke 	u_short		dst_port;
265f70c98a2SLutz Donnerhacke 	u_short		alias_port;
266f70c98a2SLutz Donnerhacke 	u_short		proxy_port;
267f70c98a2SLutz Donnerhacke 	struct server  *server;
268f70c98a2SLutz Donnerhacke 	/* Type of link: TCP, UDP, ICMP, proto, frag */
269f70c98a2SLutz Donnerhacke 	int		link_type;
270f70c98a2SLutz Donnerhacke /* values for link_type */
271f70c98a2SLutz Donnerhacke #define LINK_ICMP                     IPPROTO_ICMP
272f70c98a2SLutz Donnerhacke #define LINK_UDP                      IPPROTO_UDP
273f70c98a2SLutz Donnerhacke #define LINK_TCP                      IPPROTO_TCP
274f70c98a2SLutz Donnerhacke #define LINK_FRAGMENT_ID              (IPPROTO_MAX + 1)
275f70c98a2SLutz Donnerhacke #define LINK_FRAGMENT_PTR             (IPPROTO_MAX + 2)
276f70c98a2SLutz Donnerhacke #define LINK_ADDR                     (IPPROTO_MAX + 3)
277f70c98a2SLutz Donnerhacke #define LINK_PPTP                     (IPPROTO_MAX + 4)
278f70c98a2SLutz Donnerhacke 
279f70c98a2SLutz Donnerhacke 	int		flags;	/* indicates special characteristics */
280f70c98a2SLutz Donnerhacke 	int		pflags;	/* protocol-specific flags */
281f70c98a2SLutz Donnerhacke /* flag bits */
282f70c98a2SLutz Donnerhacke #define LINK_UNKNOWN_DEST_PORT     0x01
283f70c98a2SLutz Donnerhacke #define LINK_UNKNOWN_DEST_ADDR     0x02
284f70c98a2SLutz Donnerhacke #define LINK_PERMANENT             0x04
285f70c98a2SLutz Donnerhacke #define LINK_PARTIALLY_SPECIFIED   0x03	/* logical-or of first two bits */
286f70c98a2SLutz Donnerhacke #define LINK_UNFIREWALLED          0x08
287f70c98a2SLutz Donnerhacke 
288f70c98a2SLutz Donnerhacke 	int		timestamp;	/* Time link was last accessed */
289f70c98a2SLutz Donnerhacke #ifndef NO_USE_SOCKETS
290f70c98a2SLutz Donnerhacke 	int		sockfd;		/* socket descriptor */
291f70c98a2SLutz Donnerhacke #endif
292f70c98a2SLutz Donnerhacke 	/* Linked list of pointers for input and output lookup tables  */
293f70c98a2SLutz Donnerhacke 	union {
294f70c98a2SLutz Donnerhacke 		struct {
295f70c98a2SLutz Donnerhacke 			SPLAY_ENTRY(alias_link) out;
296f70c98a2SLutz Donnerhacke 			LIST_ENTRY (alias_link) in;
297*61bf830cSDamjan Jovanovic 			SPLAY_ENTRY(alias_link) internal_endpoint;
298f70c98a2SLutz Donnerhacke 		} all;
299f70c98a2SLutz Donnerhacke 		struct {
300f70c98a2SLutz Donnerhacke 			LIST_ENTRY (alias_link) list;
301f70c98a2SLutz Donnerhacke 		} pptp;
302f70c98a2SLutz Donnerhacke 	};
303f70c98a2SLutz Donnerhacke 	struct {
304f70c98a2SLutz Donnerhacke 		TAILQ_ENTRY(alias_link) list;
305f70c98a2SLutz Donnerhacke 		int	time;	/* Expire time for link */
306f70c98a2SLutz Donnerhacke 	} expire;
307f70c98a2SLutz Donnerhacke 	/* Auxiliary data */
308f70c98a2SLutz Donnerhacke 	union {
309f70c98a2SLutz Donnerhacke 		char           *frag_ptr;
310f70c98a2SLutz Donnerhacke 		struct in_addr	frag_addr;
311f70c98a2SLutz Donnerhacke 		struct tcp_dat *tcp;
312f70c98a2SLutz Donnerhacke 	} data;
313f70c98a2SLutz Donnerhacke };
314f70c98a2SLutz Donnerhacke 
315f70c98a2SLutz Donnerhacke /* Clean up procedure. */
316f70c98a2SLutz Donnerhacke static void finishoff(void);
317f70c98a2SLutz Donnerhacke 
318f70c98a2SLutz Donnerhacke /* Internal utility routines (used only in alias_db.c)
319f70c98a2SLutz Donnerhacke 
320f70c98a2SLutz Donnerhacke Lookup table starting points:
321f70c98a2SLutz Donnerhacke     StartPointIn()           -- link table initial search point for
322f70c98a2SLutz Donnerhacke 				incoming packets
323f70c98a2SLutz Donnerhacke     StartPointOut()          -- link table initial search point for
324f70c98a2SLutz Donnerhacke 				outgoing packets
325f70c98a2SLutz Donnerhacke 
326f70c98a2SLutz Donnerhacke Miscellaneous:
327f70c98a2SLutz Donnerhacke     SeqDiff()                -- difference between two TCP sequences
328f70c98a2SLutz Donnerhacke     ShowAliasStats()         -- send alias statistics to a monitor file
329f70c98a2SLutz Donnerhacke */
330f70c98a2SLutz Donnerhacke 
331f70c98a2SLutz Donnerhacke /* Local prototypes */
332f70c98a2SLutz Donnerhacke static struct group_in *
333f70c98a2SLutz Donnerhacke StartPointIn(struct libalias *, struct in_addr, u_short, int, int);
334f70c98a2SLutz Donnerhacke static int	SeqDiff(u_long, u_long);
335f70c98a2SLutz Donnerhacke 
336f70c98a2SLutz Donnerhacke #ifndef NO_FW_PUNCH
337f70c98a2SLutz Donnerhacke /* Firewall control */
338f70c98a2SLutz Donnerhacke static void	InitPunchFW(struct libalias *);
339f70c98a2SLutz Donnerhacke static void	UninitPunchFW(struct libalias *);
340f70c98a2SLutz Donnerhacke static void	ClearFWHole(struct alias_link *);
341f70c98a2SLutz Donnerhacke 
342f70c98a2SLutz Donnerhacke #endif
343f70c98a2SLutz Donnerhacke 
344f70c98a2SLutz Donnerhacke /* Log file control */
345f70c98a2SLutz Donnerhacke static void	ShowAliasStats(struct libalias *);
346f70c98a2SLutz Donnerhacke static int	InitPacketAliasLog(struct libalias *);
347f70c98a2SLutz Donnerhacke static void	UninitPacketAliasLog(struct libalias *);
348f70c98a2SLutz Donnerhacke 
349f70c98a2SLutz Donnerhacke void		SctpShowAliasStats(struct libalias *la);
350f70c98a2SLutz Donnerhacke 
351f70c98a2SLutz Donnerhacke 
352f70c98a2SLutz Donnerhacke /* Splay handling */
353f70c98a2SLutz Donnerhacke static inline int
cmp_out(struct alias_link * a,struct alias_link * b)354f70c98a2SLutz Donnerhacke cmp_out(struct alias_link *a, struct alias_link *b) {
355f70c98a2SLutz Donnerhacke 	int i = a->src_port - b->src_port;
356f70c98a2SLutz Donnerhacke 	if (i != 0) return (i);
35725392facSLutz Donnerhacke 	if (a->src_addr.s_addr > b->src_addr.s_addr) return (1);
35825392facSLutz Donnerhacke 	if (a->src_addr.s_addr < b->src_addr.s_addr) return (-1);
35925392facSLutz Donnerhacke 	if (a->dst_addr.s_addr > b->dst_addr.s_addr) return (1);
36025392facSLutz Donnerhacke 	if (a->dst_addr.s_addr < b->dst_addr.s_addr) return (-1);
361f70c98a2SLutz Donnerhacke 	i = a->dst_port - b->dst_port;
362f70c98a2SLutz Donnerhacke 	if (i != 0) return (i);
363f70c98a2SLutz Donnerhacke 	i = a->link_type - b->link_type;
364f70c98a2SLutz Donnerhacke 	return (i);
365f70c98a2SLutz Donnerhacke }
366f70c98a2SLutz Donnerhacke SPLAY_PROTOTYPE(splay_out, alias_link, all.out, cmp_out);
367f70c98a2SLutz Donnerhacke 
368f70c98a2SLutz Donnerhacke static inline int
cmp_in(struct group_in * a,struct group_in * b)369f70c98a2SLutz Donnerhacke cmp_in(struct group_in *a, struct group_in *b) {
370f70c98a2SLutz Donnerhacke 	int i = a->alias_port - b->alias_port;
371f70c98a2SLutz Donnerhacke 	if (i != 0) return (i);
372f70c98a2SLutz Donnerhacke 	i = a->link_type - b->link_type;
373f70c98a2SLutz Donnerhacke 	if (i != 0) return (i);
37425392facSLutz Donnerhacke 	if (a->alias_addr.s_addr > b->alias_addr.s_addr) return (1);
37525392facSLutz Donnerhacke 	if (a->alias_addr.s_addr < b->alias_addr.s_addr) return (-1);
37625392facSLutz Donnerhacke 	return (0);
377f70c98a2SLutz Donnerhacke }
378f70c98a2SLutz Donnerhacke SPLAY_PROTOTYPE(splay_in, group_in, in, cmp_in);
379f70c98a2SLutz Donnerhacke 
380*61bf830cSDamjan Jovanovic static inline int
cmp_internal_endpoint(struct alias_link * a,struct alias_link * b)381*61bf830cSDamjan Jovanovic cmp_internal_endpoint(struct alias_link *a, struct alias_link *b) {
382*61bf830cSDamjan Jovanovic 	int i = a->link_type - b->link_type;
383*61bf830cSDamjan Jovanovic 	if (i != 0) return (i);
384*61bf830cSDamjan Jovanovic 	if (a->src_addr.s_addr > b->src_addr.s_addr) return (1);
385*61bf830cSDamjan Jovanovic 	if (a->src_addr.s_addr < b->src_addr.s_addr) return (-1);
386*61bf830cSDamjan Jovanovic 	i = a->src_port - b->src_port;
387*61bf830cSDamjan Jovanovic 	return (i);
388*61bf830cSDamjan Jovanovic }
389*61bf830cSDamjan Jovanovic SPLAY_PROTOTYPE(splay_internal_endpoint, alias_link, all.internal_endpoint,
390*61bf830cSDamjan Jovanovic     cmp_internal_endpoint);
391*61bf830cSDamjan Jovanovic 
392f70c98a2SLutz Donnerhacke /* Internal routines for finding, deleting and adding links
393f70c98a2SLutz Donnerhacke 
394f70c98a2SLutz Donnerhacke Port Allocation:
395f70c98a2SLutz Donnerhacke     GetNewPort()                 -- find and reserve new alias port number
396f70c98a2SLutz Donnerhacke     GetSocket()                  -- try to allocate a socket for a given port
397f70c98a2SLutz Donnerhacke 
398f70c98a2SLutz Donnerhacke Link creation and deletion:
399f70c98a2SLutz Donnerhacke     CleanupAliasData()           - remove all link chains from lookup table
400f70c98a2SLutz Donnerhacke     CleanupLink()                - look for a stale link
401f70c98a2SLutz Donnerhacke     DeleteLink()                 - remove link
402f70c98a2SLutz Donnerhacke     AddLink()                    - add link
403f70c98a2SLutz Donnerhacke     ReLink()                     - change link
404f70c98a2SLutz Donnerhacke 
405f70c98a2SLutz Donnerhacke Link search:
406f70c98a2SLutz Donnerhacke     FindLinkOut()                - find link for outgoing packets
407f70c98a2SLutz Donnerhacke     FindLinkIn()                 - find link for incoming packets
408*61bf830cSDamjan Jovanovic     FindLinkByInternalEndpoint() - find link by a packet's internal endpoint
409f70c98a2SLutz Donnerhacke 
410f70c98a2SLutz Donnerhacke Port search:
411f70c98a2SLutz Donnerhacke     FindNewPortGroup()           - find an available group of ports
412f70c98a2SLutz Donnerhacke */
413f70c98a2SLutz Donnerhacke 
414f70c98a2SLutz Donnerhacke /* Local prototypes */
415f70c98a2SLutz Donnerhacke static int	GetNewPort(struct libalias *, struct alias_link *, int);
416f70c98a2SLutz Donnerhacke #ifndef NO_USE_SOCKETS
417f70c98a2SLutz Donnerhacke static u_short	GetSocket(struct libalias *, u_short, int *, int);
418f70c98a2SLutz Donnerhacke #endif
419f70c98a2SLutz Donnerhacke static void	CleanupAliasData(struct libalias *, int);
420f70c98a2SLutz Donnerhacke static void	CleanupLink(struct libalias *, struct alias_link **, int);
421f70c98a2SLutz Donnerhacke static void	DeleteLink(struct alias_link **, int);
422f70c98a2SLutz Donnerhacke static struct alias_link *
423f70c98a2SLutz Donnerhacke UseLink(struct libalias *, struct alias_link *);
424f70c98a2SLutz Donnerhacke 
425f70c98a2SLutz Donnerhacke static struct alias_link *
426f70c98a2SLutz Donnerhacke ReLink(struct alias_link *,
427f70c98a2SLutz Donnerhacke     struct in_addr, struct in_addr, struct in_addr,
428f70c98a2SLutz Donnerhacke     u_short, u_short, int, int, int);
429f70c98a2SLutz Donnerhacke 
430f70c98a2SLutz Donnerhacke static struct alias_link *
431f70c98a2SLutz Donnerhacke FindLinkOut(struct libalias *, struct in_addr, struct in_addr, u_short, u_short, int, int);
432f70c98a2SLutz Donnerhacke 
433f70c98a2SLutz Donnerhacke static struct alias_link *
434f70c98a2SLutz Donnerhacke FindLinkIn(struct libalias *, struct in_addr, struct in_addr, u_short, u_short, int, int);
435f70c98a2SLutz Donnerhacke 
436*61bf830cSDamjan Jovanovic static struct alias_link *
437*61bf830cSDamjan Jovanovic FindLinkByInternalEndpoint(struct libalias *, struct in_addr, u_short, int);
438*61bf830cSDamjan Jovanovic 
439f70c98a2SLutz Donnerhacke static u_short _RandomPort(struct libalias *la);
440f70c98a2SLutz Donnerhacke 
441f70c98a2SLutz Donnerhacke #define GET_NEW_PORT_MAX_ATTEMPTS       20
442f70c98a2SLutz Donnerhacke 
443f70c98a2SLutz Donnerhacke 
444f70c98a2SLutz Donnerhacke #ifndef NO_FW_PUNCH
445f70c98a2SLutz Donnerhacke 
446f70c98a2SLutz Donnerhacke static void ClearAllFWHoles(struct libalias *la);
447f70c98a2SLutz Donnerhacke 
448f70c98a2SLutz Donnerhacke #define fw_setfield(la, field, num)			\
449f70c98a2SLutz Donnerhacke do {						\
450f70c98a2SLutz Donnerhacke     (field)[(num) - la->fireWallBaseNum] = 1;		\
451f70c98a2SLutz Donnerhacke } /*lint -save -e717 */ while(0)/* lint -restore */
452f70c98a2SLutz Donnerhacke 
453f70c98a2SLutz Donnerhacke #define fw_clrfield(la, field, num)			\
454f70c98a2SLutz Donnerhacke do {							\
455f70c98a2SLutz Donnerhacke     (field)[(num) - la->fireWallBaseNum] = 0;		\
456f70c98a2SLutz Donnerhacke } /*lint -save -e717 */ while(0)/* lint -restore */
457f70c98a2SLutz Donnerhacke 
458f70c98a2SLutz Donnerhacke #define fw_tstfield(la, field, num) ((field)[(num) - la->fireWallBaseNum])
459f70c98a2SLutz Donnerhacke 
460f70c98a2SLutz Donnerhacke #endif /* !NO_FW_PUNCH */
461f70c98a2SLutz Donnerhacke 
462f70c98a2SLutz Donnerhacke #endif /* _ALIAS_DB_H_ */
463