alias_local.h revision 162674
1/*-
2 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/netinet/libalias/alias_local.h 162674 2006-09-26 23:26:53Z piso $
27 */
28
29/*
30 * Alias_local.h contains the function prototypes for alias.c,
31 * alias_db.c, alias_util.c and alias_ftp.c, alias_irc.c (as well
32 * as any future add-ons).  It also includes macros, globals and
33 * struct definitions shared by more than one alias*.c file.
34 *
35 * This include file is intended to be used only within the aliasing
36 * software.  Outside world interfaces are defined in alias.h
37 *
38 * This software is placed into the public domain with no restrictions
39 * on its distribution.
40 *
41 * Initial version:  August, 1996  (cjm)
42 *
43 * <updated several times by original author and Eivind Eklund>
44 */
45
46#ifndef _ALIAS_LOCAL_H_
47#define	_ALIAS_LOCAL_H_
48
49#include <sys/types.h>
50#include <sys/sysctl.h>
51
52#ifdef _KERNEL
53#include <sys/malloc.h>
54#include <sys/lock.h>
55/* XXX: LibAliasSetTarget() uses this constant. */
56#define	INADDR_NONE	0xffffffff
57#endif
58
59/* Sizes of input and output link tables */
60#define LINK_TABLE_OUT_SIZE         101
61#define LINK_TABLE_IN_SIZE         4001
62
63struct proxy_entry;
64
65struct libalias {
66	LIST_ENTRY(libalias) instancelist;
67
68	int		packetAliasMode;	/* Mode flags                      */
69	/* - documented in alias.h  */
70
71	struct in_addr	aliasAddress;	/* Address written onto source     */
72	/* field of IP packet.           */
73
74	struct in_addr	targetAddress;	/* IP address incoming packets     */
75	/* are sent to if no aliasing    */
76	/* link already exists           */
77
78	struct in_addr	nullAddress;	/* Used as a dummy parameter for   */
79	/* some function calls           */
80
81			LIST_HEAD     (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
82	/* Lookup table of pointers to     */
83	/* chains of link records. Each  */
84
85			LIST_HEAD     (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
86	/* link record is doubly indexed */
87	/* into input and output lookup  */
88	/* tables.                       */
89
90	/* Link statistics                 */
91	int		icmpLinkCount;
92	int		udpLinkCount;
93	int		tcpLinkCount;
94	int		pptpLinkCount;
95	int		protoLinkCount;
96	int		fragmentIdLinkCount;
97	int		fragmentPtrLinkCount;
98	int		sockCount;
99
100	int		cleanupIndex;	/* Index to chain of link table    */
101	/* being inspected for old links   */
102
103	int		timeStamp;	/* System time in seconds for      */
104	/* current packet                  */
105
106	int		lastCleanupTime;	/* Last time
107						 * IncrementalCleanup()  */
108	/* was called                      */
109
110	int		houseKeepingResidual;	/* used by HouseKeeping()          */
111
112	int		deleteAllLinks;	/* If equal to zero, DeleteLink()  */
113	/* will not remove permanent links */
114
115	/* log descriptor        */
116#ifdef  _KERNEL
117	char           *logDesc;
118#else
119	FILE           *logDesc;
120#endif
121	/* statistics monitoring */
122
123	int		newDefaultLink;	/* Indicates if a new aliasing     */
124	/* link has been created after a   */
125	/* call to PacketAliasIn/Out().    */
126
127#ifndef NO_FW_PUNCH
128	int		fireWallFD;	/* File descriptor to be able to   */
129	/* control firewall.  Opened by    */
130	/* PacketAliasSetMode on first     */
131	/* setting the PKT_ALIAS_PUNCH_FW  */
132	/* flag.                           */
133	int		fireWallBaseNum;	/* The first firewall entry
134						 * free for our use */
135	int		fireWallNumNums;	/* How many entries can we
136						 * use? */
137	int		fireWallActiveNum;	/* Which entry did we last
138						 * use? */
139	char           *fireWallField;	/* bool array for entries */
140#endif
141
142	unsigned int	skinnyPort;	/* TCP port used by the Skinny     */
143	/* protocol.                       */
144
145	struct proxy_entry *proxyList;
146
147	struct in_addr	true_addr;	/* in network byte order. */
148	u_short		true_port;	/* in host byte order. */
149
150};
151
152/* Macros */
153
154/*
155 * The following macro is used to update an
156 * internet checksum.  "delta" is a 32-bit
157 * accumulation of all the changes to the
158 * checksum (adding in new 16-bit words and
159 * subtracting out old words), and "cksum"
160 * is the checksum value to be updated.
161 */
162#define	ADJUST_CHECKSUM(acc, cksum) \
163	do { \
164		acc += cksum; \
165		if (acc < 0) { \
166			acc = -acc; \
167			acc = (acc >> 16) + (acc & 0xffff); \
168			acc += acc >> 16; \
169			cksum = (u_short) ~acc; \
170		} else { \
171			acc = (acc >> 16) + (acc & 0xffff); \
172			acc += acc >> 16; \
173			cksum = (u_short) acc; \
174		} \
175	} while (0)
176
177
178/* Prototypes */
179
180/*
181 * We do not calculate TCP checksums when libalias is a kernel
182 * module, since it has no idea about checksum offloading.
183 * If TCP data has changed, then we just set checksum to zero,
184 * and caller must recalculate it himself.
185 * In case if libalias will edit UDP data, the same approach
186 * should be used.
187 */
188#ifndef _KERNEL
189u_short		IpChecksum(struct ip *_pip);
190u_short		TcpChecksum(struct ip *_pip);
191#endif
192void
193DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
194
195/* Internal data access */
196struct alias_link *
197FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
198    u_short _id_alias, int _create);
199struct alias_link *
200FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
201    u_short _id, int _create);
202struct alias_link *
203FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
204    u_short _ip_id);
205struct alias_link *
206FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
207    u_short _ip_id);
208struct alias_link *
209		AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
210struct alias_link *
211		FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
212struct alias_link *
213FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
214    u_char _proto);
215struct alias_link *
216FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
217    u_char _proto);
218struct alias_link *
219FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
220    u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
221struct alias_link *
222FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
223    u_short _src_port, u_short _dst_port, u_char _proto, int _create);
224struct alias_link *
225AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
226    struct in_addr _alias_addr, u_int16_t _src_call_id);
227struct alias_link *
228FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
229    struct in_addr _dst_addr, u_int16_t _src_call_id);
230struct alias_link *
231FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
232    struct in_addr _alias_addr, u_int16_t _dst_call_id);
233struct alias_link *
234FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
235    struct in_addr _dst_addr, u_int16_t _dst_call_id);
236struct alias_link *
237FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
238    struct in_addr _alias_addr, u_int16_t _alias_call_id);
239struct alias_link *
240FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
241    u_short _src_port, u_short _alias_port, u_char _proto);
242struct in_addr
243		FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
244struct in_addr
245		FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
246
247/* External data access/modification */
248int
249FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
250    u_short _src_port, u_short _dst_port, u_short _port_count,
251    u_char _proto, u_char _align);
252void		GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
253void		SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
254void		GetFragmentPtr(struct alias_link *_lnk, char **_fptr);
255void		SetFragmentPtr(struct alias_link *_lnk, char *fptr);
256void		SetStateIn(struct alias_link *_lnk, int _state);
257void		SetStateOut(struct alias_link *_lnk, int _state);
258int		GetStateIn (struct alias_link *_lnk);
259int		GetStateOut(struct alias_link *_lnk);
260struct in_addr
261		GetOriginalAddress(struct alias_link *_lnk);
262struct in_addr
263		GetDestAddress(struct alias_link *_lnk);
264struct in_addr
265		GetAliasAddress(struct alias_link *_lnk);
266struct in_addr
267		GetDefaultAliasAddress(struct libalias *la);
268void		SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
269u_short		GetOriginalPort(struct alias_link *_lnk);
270u_short		GetAliasPort(struct alias_link *_lnk);
271struct in_addr
272		GetProxyAddress(struct alias_link *_lnk);
273void		SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
274u_short		GetProxyPort(struct alias_link *_lnk);
275void		SetProxyPort(struct alias_link *_lnk, u_short _port);
276void		SetAckModified(struct alias_link *_lnk);
277int		GetAckModified(struct alias_link *_lnk);
278int		GetDeltaAckIn(struct ip *_pip, struct alias_link *_lnk);
279int		GetDeltaSeqOut(struct ip *_pip, struct alias_link *_lnk);
280void		AddSeq    (struct ip *_pip, struct alias_link *_lnk, int _delta);
281void		SetExpire (struct alias_link *_lnk, int _expire);
282void		ClearCheckNewLink(struct libalias *la);
283void		SetProtocolFlags(struct alias_link *_lnk, int _pflags);
284int		GetProtocolFlags(struct alias_link *_lnk);
285void		SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
286
287#ifndef NO_FW_PUNCH
288void		PunchFWHole(struct alias_link *_lnk);
289
290#endif
291
292/* Housekeeping function */
293void		HouseKeeping(struct libalias *);
294
295/* Tcp specfic routines */
296/* lint -save -library Suppress flexelint warnings */
297
298/* Transparent proxy routines */
299int
300ProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
301    u_short * _proxy_server_port);
302void
303ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
304    int _maxpacketsize, int _proxy_type);
305
306enum alias_tcp_state {
307	ALIAS_TCP_STATE_NOT_CONNECTED,
308	ALIAS_TCP_STATE_CONNECTED,
309	ALIAS_TCP_STATE_DISCONNECTED
310};
311
312#if defined(_NETINET_IP_H_)
313static __inline void *
314ip_next(struct ip *iphdr)
315{
316	char *p = (char *)iphdr;
317	return (&p[iphdr->ip_hl * 4]);
318}
319#endif
320
321#if defined(_NETINET_TCP_H_)
322static __inline void *
323tcp_next(struct tcphdr *tcphdr)
324{
325	char *p = (char *)tcphdr;
326	return (&p[tcphdr->th_off * 4]);
327}
328#endif
329
330#if defined(_NETINET_UDP_H_)
331static __inline void *
332udp_next(struct udphdr *udphdr)
333{
334	return ((void *)(udphdr + 1));
335}
336#endif
337
338#endif				/* !_ALIAS_LOCAL_H_ */
339