alias_local.h revision 131699
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 131699 2004-07-06 12:13:28Z des $
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
107	FILE           *monitorFile;	/* File descriptor for link        */
108	/* statistics monitoring file      */
109
110	int		newDefaultLink;	/* Indicates if a new aliasing     */
111	/* link has been created after a   */
112	/* call to PacketAliasIn/Out().    */
113
114#ifndef NO_FW_PUNCH
115	int		fireWallFD;	/* File descriptor to be able to   */
116	/* control firewall.  Opened by    */
117	/* PacketAliasSetMode on first     */
118	/* setting the PKT_ALIAS_PUNCH_FW  */
119	/* flag.                           */
120	int		fireWallBaseNum;	/* The first firewall entry
121						 * free for our use */
122	int		fireWallNumNums;	/* How many entries can we
123						 * use? */
124	int		fireWallActiveNum;	/* Which entry did we last
125						 * use? */
126	char           *fireWallField;	/* bool array for entries */
127#endif
128
129	unsigned int	skinnyPort;	/* TCP port used by the Skinny     */
130	/* protocol.                       */
131
132	struct proxy_entry *proxyList;
133
134	struct in_addr	true_addr;	/* in network byte order. */
135	u_short		true_port;	/* in host byte order. */
136
137};
138
139/* Macros */
140
141/*
142 * The following macro is used to update an
143 * internet checksum.  "delta" is a 32-bit
144 * accumulation of all the changes to the
145 * checksum (adding in new 16-bit words and
146 * subtracting out old words), and "cksum"
147 * is the checksum value to be updated.
148 */
149#define	ADJUST_CHECKSUM(acc, cksum) \
150	do { \
151		acc += cksum; \
152		if (acc < 0) { \
153			acc = -acc; \
154			acc = (acc >> 16) + (acc & 0xffff); \
155			acc += acc >> 16; \
156			cksum = (u_short) ~acc; \
157		} else { \
158			acc = (acc >> 16) + (acc & 0xffff); \
159			acc += acc >> 16; \
160			cksum = (u_short) acc; \
161		} \
162	} while (0)
163
164
165/* Prototypes */
166
167/* General utilities */
168u_short		IpChecksum(struct ip *_pip);
169u_short		TcpChecksum(struct ip *_pip);
170void
171DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
172
173/* Internal data access */
174struct alias_link *
175FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
176    u_short _id_alias, int _create);
177struct alias_link *
178FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
179    u_short _id, int _create);
180struct alias_link *
181FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
182    u_short _ip_id);
183struct alias_link *
184FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
185    u_short _ip_id);
186struct alias_link *
187		AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
188struct alias_link *
189		FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
190struct alias_link *
191FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
192    u_char _proto);
193struct alias_link *
194FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
195    u_char _proto);
196struct alias_link *
197FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
198    u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
199struct alias_link *
200FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
201    u_short _src_port, u_short _dst_port, u_char _proto, int _create);
202struct alias_link *
203AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
204    struct in_addr _alias_addr, u_int16_t _src_call_id);
205struct alias_link *
206FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
207    struct in_addr _dst_addr, u_int16_t _src_call_id);
208struct alias_link *
209FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
210    struct in_addr _alias_addr, u_int16_t _dst_call_id);
211struct alias_link *
212FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
213    struct in_addr _dst_addr, u_int16_t _dst_call_id);
214struct alias_link *
215FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
216    struct in_addr _alias_addr, u_int16_t _alias_call_id);
217struct alias_link *
218FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
219    u_short _src_port, u_short _alias_port, u_char _proto);
220struct in_addr
221		FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
222struct in_addr
223		FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
224
225/* External data access/modification */
226int
227FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
228    u_short _src_port, u_short _dst_port, u_short _port_count,
229    u_char _proto, u_char _align);
230void		GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
231void		SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
232void		GetFragmentPtr(struct alias_link *_lnk, char **_fptr);
233void		SetFragmentPtr(struct alias_link *_lnk, char *fptr);
234void		SetStateIn(struct alias_link *_lnk, int _state);
235void		SetStateOut(struct alias_link *_lnk, int _state);
236int		GetStateIn (struct alias_link *_lnk);
237int		GetStateOut(struct alias_link *_lnk);
238struct in_addr
239		GetOriginalAddress(struct alias_link *_lnk);
240struct in_addr
241		GetDestAddress(struct alias_link *_lnk);
242struct in_addr
243		GetAliasAddress(struct alias_link *_lnk);
244struct in_addr
245		GetDefaultAliasAddress(struct libalias *la);
246void		SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
247u_short		GetOriginalPort(struct alias_link *_lnk);
248u_short		GetAliasPort(struct alias_link *_lnk);
249struct in_addr
250		GetProxyAddress(struct alias_link *_lnk);
251void		SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
252u_short		GetProxyPort(struct alias_link *_lnk);
253void		SetProxyPort(struct alias_link *_lnk, u_short _port);
254void		SetAckModified(struct alias_link *_lnk);
255int		GetAckModified(struct alias_link *_lnk);
256int		GetDeltaAckIn(struct ip *_pip, struct alias_link *_lnk);
257int		GetDeltaSeqOut(struct ip *_pip, struct alias_link *_lnk);
258void		AddSeq    (struct ip *_pip, struct alias_link *_lnk, int _delta);
259void		SetExpire (struct alias_link *_lnk, int _expire);
260void		ClearCheckNewLink(struct libalias *la);
261void		SetProtocolFlags(struct alias_link *_lnk, int _pflags);
262int		GetProtocolFlags(struct alias_link *_lnk);
263void		SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
264
265#ifndef NO_FW_PUNCH
266void		PunchFWHole(struct alias_link *_lnk);
267
268#endif
269
270/* Housekeeping function */
271void		HouseKeeping(struct libalias *);
272
273/* Tcp specfic routines */
274/* lint -save -library Suppress flexelint warnings */
275
276/* FTP routines */
277void
278AliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
279    int _maxpacketsize);
280
281/* IRC routines */
282void
283AliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
284    int _maxsize);
285
286/* RTSP routines */
287void
288AliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
289    int _maxpacketsize);
290
291/* PPTP routines */
292void		AliasHandlePptpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
293void		AliasHandlePptpIn(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
294int		AliasHandlePptpGreOut(struct libalias *la, struct ip *_pip);
295int		AliasHandlePptpGreIn(struct libalias *la, struct ip *_pip);
296
297/* NetBIOS routines */
298int
299AliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
300    struct in_addr *_alias_address, u_short _alias_port);
301int
302AliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
303    struct in_addr *_alias_address, u_short * _alias_port,
304    struct in_addr *_original_address, u_short * _original_port);
305
306/* CUSeeMe routines */
307void		AliasHandleCUSeeMeOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
308void		AliasHandleCUSeeMeIn(struct libalias *la, struct ip *_pip, struct in_addr _original_addr);
309
310/* Skinny routines */
311void		AliasHandleSkinny(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
312
313/* Transparent proxy routines */
314int
315ProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
316    u_short * _proxy_server_port);
317void
318ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
319    int _maxpacketsize, int _proxy_type);
320
321enum alias_tcp_state {
322	ALIAS_TCP_STATE_NOT_CONNECTED,
323	ALIAS_TCP_STATE_CONNECTED,
324	ALIAS_TCP_STATE_DISCONNECTED
325};
326
327#if defined(_NETINET_IP_H_)
328static __inline void *
329ip_next(struct ip *iphdr)
330{
331	char *p = (char *)iphdr;
332	return (&p[iphdr->ip_hl * 4]);
333}
334#endif
335
336#if defined(_NETINET_TCP_H_)
337static __inline void *
338tcp_next(struct tcphdr *tcphdr)
339{
340	char *p = (char *)tcphdr;
341	return (&p[tcphdr->th_off * 4]);
342}
343#endif
344
345#if defined(_NETINET_UDP_H_)
346static __inline void *
347udp_next(struct udphdr *udphdr)
348{
349	return ((void *)(udphdr + 1));
350}
351#endif
352
353/*lint -restore */
354
355#endif				/* !_ALIAS_LOCAL_H_ */
356