alias_local.h revision 145925
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 145925 2005-05-05 20:22:09Z glebius $
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/queue.h>
50
51/* Sizes of input and output link tables */
52#define LINK_TABLE_OUT_SIZE         101
53#define LINK_TABLE_IN_SIZE         4001
54
55struct proxy_entry;
56
57struct libalias {
58	LIST_ENTRY(libalias) instancelist;
59
60	int		packetAliasMode;	/* Mode flags                      */
61	/* - documented in alias.h  */
62
63	struct in_addr	aliasAddress;	/* Address written onto source     */
64	/* field of IP packet.           */
65
66	struct in_addr	targetAddress;	/* IP address incoming packets     */
67	/* are sent to if no aliasing    */
68	/* link already exists           */
69
70	struct in_addr	nullAddress;	/* Used as a dummy parameter for   */
71	/* some function calls           */
72
73			LIST_HEAD     (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
74	/* Lookup table of pointers to     */
75	/* chains of link records. Each  */
76
77			LIST_HEAD     (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
78	/* link record is doubly indexed */
79	/* into input and output lookup  */
80	/* tables.                       */
81
82	/* Link statistics                 */
83	int		icmpLinkCount;
84	int		udpLinkCount;
85	int		tcpLinkCount;
86	int		pptpLinkCount;
87	int		protoLinkCount;
88	int		fragmentIdLinkCount;
89	int		fragmentPtrLinkCount;
90	int		sockCount;
91
92	int		cleanupIndex;	/* Index to chain of link table    */
93	/* being inspected for old links   */
94
95	int		timeStamp;	/* System time in seconds for      */
96	/* current packet                  */
97
98	int		lastCleanupTime;	/* Last time
99						 * IncrementalCleanup()  */
100	/* was called                      */
101
102	int		houseKeepingResidual;	/* used by HouseKeeping()          */
103
104	int		deleteAllLinks;	/* If equal to zero, DeleteLink()  */
105	/* will not remove permanent links */
106#ifndef	NO_LOGGING
107	FILE           *monitorFile;	/* File descriptor for link        */
108#endif
109	/* statistics monitoring file      */
110
111	int		newDefaultLink;	/* Indicates if a new aliasing     */
112	/* link has been created after a   */
113	/* call to PacketAliasIn/Out().    */
114
115#ifndef NO_FW_PUNCH
116	int		fireWallFD;	/* File descriptor to be able to   */
117	/* control firewall.  Opened by    */
118	/* PacketAliasSetMode on first     */
119	/* setting the PKT_ALIAS_PUNCH_FW  */
120	/* flag.                           */
121	int		fireWallBaseNum;	/* The first firewall entry
122						 * free for our use */
123	int		fireWallNumNums;	/* How many entries can we
124						 * use? */
125	int		fireWallActiveNum;	/* Which entry did we last
126						 * use? */
127	char           *fireWallField;	/* bool array for entries */
128#endif
129
130	unsigned int	skinnyPort;	/* TCP port used by the Skinny     */
131	/* protocol.                       */
132
133	struct proxy_entry *proxyList;
134
135	struct in_addr	true_addr;	/* in network byte order. */
136	u_short		true_port;	/* in host byte order. */
137
138};
139
140/* Macros */
141
142/*
143 * The following macro is used to update an
144 * internet checksum.  "delta" is a 32-bit
145 * accumulation of all the changes to the
146 * checksum (adding in new 16-bit words and
147 * subtracting out old words), and "cksum"
148 * is the checksum value to be updated.
149 */
150#define	ADJUST_CHECKSUM(acc, cksum) \
151	do { \
152		acc += cksum; \
153		if (acc < 0) { \
154			acc = -acc; \
155			acc = (acc >> 16) + (acc & 0xffff); \
156			acc += acc >> 16; \
157			cksum = (u_short) ~acc; \
158		} else { \
159			acc = (acc >> 16) + (acc & 0xffff); \
160			acc += acc >> 16; \
161			cksum = (u_short) acc; \
162		} \
163	} while (0)
164
165
166/* Prototypes */
167
168/* General utilities */
169u_short		IpChecksum(struct ip *_pip);
170u_short		TcpChecksum(struct ip *_pip);
171void
172DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
173
174/* Internal data access */
175struct alias_link *
176FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
177    u_short _id_alias, int _create);
178struct alias_link *
179FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
180    u_short _id, int _create);
181struct alias_link *
182FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
183    u_short _ip_id);
184struct alias_link *
185FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
186    u_short _ip_id);
187struct alias_link *
188		AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
189struct alias_link *
190		FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
191struct alias_link *
192FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
193    u_char _proto);
194struct alias_link *
195FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
196    u_char _proto);
197struct alias_link *
198FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
199    u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
200struct alias_link *
201FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
202    u_short _src_port, u_short _dst_port, u_char _proto, int _create);
203struct alias_link *
204AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
205    struct in_addr _alias_addr, u_int16_t _src_call_id);
206struct alias_link *
207FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
208    struct in_addr _dst_addr, u_int16_t _src_call_id);
209struct alias_link *
210FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
211    struct in_addr _alias_addr, u_int16_t _dst_call_id);
212struct alias_link *
213FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
214    struct in_addr _dst_addr, u_int16_t _dst_call_id);
215struct alias_link *
216FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
217    struct in_addr _alias_addr, u_int16_t _alias_call_id);
218struct alias_link *
219FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
220    u_short _src_port, u_short _alias_port, u_char _proto);
221struct in_addr
222		FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
223struct in_addr
224		FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
225
226/* External data access/modification */
227int
228FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
229    u_short _src_port, u_short _dst_port, u_short _port_count,
230    u_char _proto, u_char _align);
231void		GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
232void		SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
233void		GetFragmentPtr(struct alias_link *_lnk, char **_fptr);
234void		SetFragmentPtr(struct alias_link *_lnk, char *fptr);
235void		SetStateIn(struct alias_link *_lnk, int _state);
236void		SetStateOut(struct alias_link *_lnk, int _state);
237int		GetStateIn (struct alias_link *_lnk);
238int		GetStateOut(struct alias_link *_lnk);
239struct in_addr
240		GetOriginalAddress(struct alias_link *_lnk);
241struct in_addr
242		GetDestAddress(struct alias_link *_lnk);
243struct in_addr
244		GetAliasAddress(struct alias_link *_lnk);
245struct in_addr
246		GetDefaultAliasAddress(struct libalias *la);
247void		SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
248u_short		GetOriginalPort(struct alias_link *_lnk);
249u_short		GetAliasPort(struct alias_link *_lnk);
250struct in_addr
251		GetProxyAddress(struct alias_link *_lnk);
252void		SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
253u_short		GetProxyPort(struct alias_link *_lnk);
254void		SetProxyPort(struct alias_link *_lnk, u_short _port);
255void		SetAckModified(struct alias_link *_lnk);
256int		GetAckModified(struct alias_link *_lnk);
257int		GetDeltaAckIn(struct ip *_pip, struct alias_link *_lnk);
258int		GetDeltaSeqOut(struct ip *_pip, struct alias_link *_lnk);
259void		AddSeq    (struct ip *_pip, struct alias_link *_lnk, int _delta);
260void		SetExpire (struct alias_link *_lnk, int _expire);
261void		ClearCheckNewLink(struct libalias *la);
262void		SetProtocolFlags(struct alias_link *_lnk, int _pflags);
263int		GetProtocolFlags(struct alias_link *_lnk);
264void		SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
265
266#ifndef NO_FW_PUNCH
267void		PunchFWHole(struct alias_link *_lnk);
268
269#endif
270
271/* Housekeeping function */
272void		HouseKeeping(struct libalias *);
273
274/* Tcp specfic routines */
275/* lint -save -library Suppress flexelint warnings */
276
277/* FTP routines */
278void
279AliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
280    int _maxpacketsize);
281
282/* IRC routines */
283void
284AliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
285    int _maxsize);
286
287/* RTSP routines */
288void
289AliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
290    int _maxpacketsize);
291
292/* PPTP routines */
293void		AliasHandlePptpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
294void		AliasHandlePptpIn(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
295int		AliasHandlePptpGreOut(struct libalias *la, struct ip *_pip);
296int		AliasHandlePptpGreIn(struct libalias *la, struct ip *_pip);
297
298/* NetBIOS routines */
299int
300AliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
301    struct in_addr *_alias_address, u_short _alias_port);
302int
303AliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
304    struct in_addr *_alias_address, u_short * _alias_port,
305    struct in_addr *_original_address, u_short * _original_port);
306
307/* CUSeeMe routines */
308void		AliasHandleCUSeeMeOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
309void		AliasHandleCUSeeMeIn(struct libalias *la, struct ip *_pip, struct in_addr _original_addr);
310
311/* Skinny routines */
312void		AliasHandleSkinny(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
313
314/* Transparent proxy routines */
315int
316ProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
317    u_short * _proxy_server_port);
318void
319ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
320    int _maxpacketsize, int _proxy_type);
321
322enum alias_tcp_state {
323	ALIAS_TCP_STATE_NOT_CONNECTED,
324	ALIAS_TCP_STATE_CONNECTED,
325	ALIAS_TCP_STATE_DISCONNECTED
326};
327
328#if defined(_NETINET_IP_H_)
329static __inline void *
330ip_next(struct ip *iphdr)
331{
332	char *p = (char *)iphdr;
333	return (&p[iphdr->ip_hl * 4]);
334}
335#endif
336
337#if defined(_NETINET_TCP_H_)
338static __inline void *
339tcp_next(struct tcphdr *tcphdr)
340{
341	char *p = (char *)tcphdr;
342	return (&p[tcphdr->th_off * 4]);
343}
344#endif
345
346#if defined(_NETINET_UDP_H_)
347static __inline void *
348udp_next(struct udphdr *udphdr)
349{
350	return ((void *)(udphdr + 1));
351}
352#endif
353
354/*lint -restore */
355
356#endif				/* !_ALIAS_LOCAL_H_ */
357