1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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_local.h contains the function prototypes for alias.c,
33 * alias_db.c, alias_util.c and alias_ftp.c, alias_irc.c (as well
34 * as any future add-ons).  It also includes macros, globals and
35 * struct definitions shared by more than one alias*.c file.
36 *
37 * This include file is intended to be used only within the aliasing
38 * software.  Outside world interfaces are defined in alias.h
39 *
40 * This software is placed into the public domain with no restrictions
41 * on its distribution.
42 *
43 * Initial version:  August, 1996  (cjm)
44 *
45 * <updated several times by original author and Eivind Eklund>
46 */
47
48#ifndef _ALIAS_LOCAL_H_
49#define	_ALIAS_LOCAL_H_
50
51#include <sys/types.h>
52#include <sys/sysctl.h>
53
54#ifdef _KERNEL
55#include <sys/malloc.h>
56#include <sys/param.h>
57#include <sys/lock.h>
58#include <sys/mutex.h>
59
60/* XXX: LibAliasSetTarget() uses this constant. */
61#define	INADDR_NONE	0xffffffff
62
63#include <netinet/libalias/alias_sctp.h>
64#else
65#include "alias_sctp.h"
66#endif
67
68/* Sizes of input and output link tables */
69#define LINK_TABLE_OUT_SIZE	4001
70#define LINK_TABLE_IN_SIZE	4001
71
72#define	GET_ALIAS_PORT		-1
73#define	GET_ALIAS_ID		GET_ALIAS_PORT
74
75#ifdef _KERNEL
76#define INET_NTOA_BUF(buf) (buf)
77#else
78#define INET_NTOA_BUF(buf) (buf), sizeof(buf)
79#endif
80
81struct proxy_entry;
82
83struct libalias {
84	LIST_ENTRY(libalias) instancelist;
85	/* Mode flags documented in alias.h */
86	int		packetAliasMode;
87	/* Address written onto source field of IP packet. */
88	struct in_addr	aliasAddress;
89	/* IP address incoming packets are sent to
90	 * if no aliasing link already exists */
91	struct in_addr	targetAddress;
92	/* Lookup table of pointers to chains of link records.
93	 * Each link record is doubly indexed into input and
94	 * output lookup tables. */
95	LIST_HEAD     (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
96	LIST_HEAD     (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
97	/* Link statistics */
98	int		icmpLinkCount;
99	int		udpLinkCount;
100	int		tcpLinkCount;
101	int		pptpLinkCount;
102	int		protoLinkCount;
103	int		fragmentIdLinkCount;
104	int		fragmentPtrLinkCount;
105	int		sockCount;
106	/* Index to chain of link table being inspected for old links   */
107	int		cleanupIndex;
108	/* System time in seconds for current packet */
109	int		timeStamp;
110	/* Last time IncrementalCleanup() was called */
111	int		lastCleanupTime;
112	/* If equal to zero, DeleteLink()
113	 * will not remove permanent links */
114	int		deleteAllLinks;
115	/* log descriptor */
116#ifdef _KERNEL
117	char	       *logDesc;
118#else
119	FILE	       *logDesc;
120#endif
121
122#ifndef NO_FW_PUNCH
123	/* File descriptor to be able to control firewall.
124	 * Opened by PacketAliasSetMode on first setting
125	 * the PKT_ALIAS_PUNCH_FW flag. */
126	int		fireWallFD;
127	/* The first firewall entry free for our use */
128	int		fireWallBaseNum;
129	/* How many entries can we use? */
130	int		fireWallNumNums;
131	/* Which entry did we last use? */
132	int		fireWallActiveNum;
133	/* bool array for entries */
134	char	       *fireWallField;
135#endif
136	/* TCP port used by the Skinny protocol. */
137	unsigned int	skinnyPort;
138
139	struct proxy_entry *proxyList;
140
141	struct in_addr	true_addr;	/* in network byte order. */
142	u_short		true_port;	/* in host byte order. */
143
144	/* Port ranges for aliasing. */
145	u_short		aliasPortLower;
146	u_short		aliasPortLength;
147
148	/*
149	 * sctp code support
150	 */
151
152	/* counts associations that have progressed to UP and not yet removed */
153	int		sctpLinkCount;
154#ifdef _KERNEL
155	/* timing queue for keeping track of association timeouts */
156	struct sctp_nat_timer sctpNatTimer;
157	/* size of hash table used in this instance */
158	u_int sctpNatTableSize;
159	/* local look up table sorted by l_vtag/l_port */
160	LIST_HEAD(sctpNatTableL, sctp_nat_assoc) *sctpTableLocal;
161	/* global look up table sorted by g_vtag/g_port */
162	LIST_HEAD(sctpNatTableG, sctp_nat_assoc) *sctpTableGlobal;
163
164	/* avoid races in libalias: every public function has to use it. */
165	struct mtx mutex;
166#endif
167};
168
169/* Macros */
170
171#ifdef _KERNEL
172#define LIBALIAS_LOCK_INIT(l) \
173	mtx_init(&l->mutex, "per-instance libalias mutex", NULL, MTX_DEF)
174#define LIBALIAS_LOCK_ASSERT(l) mtx_assert(&l->mutex, MA_OWNED)
175#define LIBALIAS_LOCK(l) mtx_lock(&l->mutex)
176#define LIBALIAS_UNLOCK(l) mtx_unlock(&l->mutex)
177#define LIBALIAS_LOCK_DESTROY(l)	mtx_destroy(&l->mutex)
178#else
179#define LIBALIAS_LOCK_INIT(l)
180#define LIBALIAS_LOCK_ASSERT(l)
181#define LIBALIAS_LOCK(l)
182#define LIBALIAS_UNLOCK(l)
183#define LIBALIAS_LOCK_DESTROY(l)
184#endif
185
186/*
187 * The following macro is used to update an
188 * internet checksum.  "delta" is a 32-bit
189 * accumulation of all the changes to the
190 * checksum (adding in new 16-bit words and
191 * subtracting out old words), and "cksum"
192 * is the checksum value to be updated.
193 */
194#define	ADJUST_CHECKSUM(acc, cksum) \
195	do { \
196		acc += cksum; \
197		if (acc < 0) { \
198			acc = -acc; \
199			acc = (acc >> 16) + (acc & 0xffff); \
200			acc += acc >> 16; \
201			cksum = (u_short) ~acc; \
202		} else { \
203			acc = (acc >> 16) + (acc & 0xffff); \
204			acc += acc >> 16; \
205			cksum = (u_short) acc; \
206		} \
207	} while (0)
208
209/* Prototypes */
210
211/*
212 * SctpFunction prototypes
213 *
214 */
215void AliasSctpInit(struct libalias *la);
216void AliasSctpTerm(struct libalias *la);
217int SctpAlias(struct libalias *la, struct ip *ip, int direction);
218
219/*
220 * We do not calculate TCP checksums when libalias is a kernel
221 * module, since it has no idea about checksum offloading.
222 * If TCP data has changed, then we just set checksum to zero,
223 * and caller must recalculate it himself.
224 * In case if libalias will edit UDP data, the same approach
225 * should be used.
226 */
227#ifndef _KERNEL
228u_short		IpChecksum(struct ip *_pip);
229u_short		TcpChecksum(struct ip *_pip);
230#endif
231void
232DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
233
234/* Internal data access */
235struct alias_link *
236AddLink(struct libalias *la, struct in_addr src_addr, struct in_addr dst_addr,
237    struct in_addr alias_addr, u_short src_port, u_short dst_port,
238    int alias_param, int link_type);
239struct alias_link *
240FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
241    u_short _id_alias, int _create);
242struct alias_link *
243FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
244    u_short _id, int _create);
245struct alias_link *
246FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
247    u_short _ip_id);
248struct alias_link *
249FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
250    u_short _ip_id);
251struct alias_link *
252AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
253struct alias_link *
254FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
255struct alias_link *
256FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
257    u_char _proto);
258struct alias_link *
259FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
260    u_char _proto);
261struct alias_link *
262FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
263    u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
264struct alias_link *
265FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
266    u_short _src_port, u_short _dst_port, u_char _proto, int _create);
267struct alias_link *
268AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
269    struct in_addr _alias_addr, u_int16_t _src_call_id);
270struct alias_link *
271FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
272    struct in_addr _dst_addr, u_int16_t _src_call_id);
273struct alias_link *
274FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
275    struct in_addr _alias_addr, u_int16_t _dst_call_id);
276struct alias_link *
277FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
278    struct in_addr _dst_addr, u_int16_t _dst_call_id);
279struct alias_link *
280FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
281    struct in_addr _alias_addr, u_int16_t _alias_call_id);
282struct alias_link *
283FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
284    u_short _src_port, u_short _alias_port, u_char _proto);
285struct in_addr
286FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
287struct in_addr
288FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
289struct in_addr
290FindSctpRedirectAddress(struct libalias *la,  struct sctp_nat_msg *sm);
291
292/* External data access/modification */
293int		FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr,
294		    struct in_addr _alias_addr, u_short _src_port,
295		    u_short _dst_port, u_short _port_count, u_char _proto,
296		    u_char _align);
297void		GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
298void		SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
299void		GetFragmentPtr(struct alias_link *_lnk, void **_fptr);
300void		SetFragmentPtr(struct alias_link *_lnk, void *fptr);
301void		SetStateIn(struct alias_link *_lnk, int _state);
302void		SetStateOut(struct alias_link *_lnk, int _state);
303int		GetStateIn (struct alias_link *_lnk);
304int		GetStateOut(struct alias_link *_lnk);
305struct in_addr	GetOriginalAddress(struct alias_link *_lnk);
306struct in_addr	GetDestAddress(struct alias_link *_lnk);
307struct in_addr	GetAliasAddress(struct alias_link *_lnk);
308struct in_addr	GetDefaultAliasAddress(struct libalias *la);
309void		SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
310u_short		GetOriginalPort(struct alias_link *_lnk);
311u_short		GetAliasPort(struct alias_link *_lnk);
312struct in_addr	GetProxyAddress(struct alias_link *_lnk);
313void		SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
314u_short		GetProxyPort(struct alias_link *_lnk);
315void		SetProxyPort(struct alias_link *_lnk, u_short _port);
316void		SetAckModified(struct alias_link *_lnk);
317int		GetAckModified(struct alias_link *_lnk);
318int		GetDeltaAckIn(u_long, struct alias_link *_lnk);
319int		GetDeltaSeqOut(u_long, struct alias_link *lnk);
320void		AddSeq(struct alias_link *lnk, int delta, u_int ip_hl,
321		    u_short ip_len, u_long th_seq, u_int th_off);
322void		SetExpire (struct alias_link *_lnk, int _expire);
323void		SetProtocolFlags(struct alias_link *_lnk, int _pflags);
324int		GetProtocolFlags(struct alias_link *_lnk);
325void		SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
326
327#ifndef NO_FW_PUNCH
328void		PunchFWHole(struct alias_link *_lnk);
329
330#endif
331
332/* Housekeeping function */
333void		HouseKeeping(struct libalias *);
334
335/* Transparent proxy routines */
336int
337ProxyCheck(struct libalias *la, struct in_addr *proxy_server_addr,
338    u_short * proxy_server_port, struct in_addr src_addr,
339    struct in_addr dst_addr, u_short dst_port, u_char ip_p);
340void
341ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
342    int _maxpacketsize, int _proxy_type);
343
344/* Tcp specific routines */
345/* lint -save -library Suppress flexelint warnings */
346
347enum alias_tcp_state {
348	ALIAS_TCP_STATE_NOT_CONNECTED,
349	ALIAS_TCP_STATE_CONNECTED,
350	ALIAS_TCP_STATE_DISCONNECTED
351};
352
353#if defined(_NETINET_IP_H_)
354static __inline void *
355ip_next(struct ip *iphdr)
356{
357	char *p = (char *)iphdr;
358	return (&p[iphdr->ip_hl * 4]);
359}
360#endif
361
362#if defined(_NETINET_TCP_H_)
363static __inline void *
364tcp_next(struct tcphdr *tcphdr)
365{
366	char *p = (char *)tcphdr;
367	return (&p[tcphdr->th_off * 4]);
368}
369#endif
370
371#if defined(_NETINET_UDP_H_)
372static __inline void *
373udp_next(struct udphdr *udphdr)
374{
375	return ((void *)(udphdr + 1));
376}
377#endif
378
379#endif				/* !_ALIAS_LOCAL_H_ */
380