alias_local.h revision 145927
177701Sbrian/*-
285964Sbrian * Copyright (c) 2001 Charles Mott <cm@linktel.net>
377701Sbrian * All rights reserved.
477701Sbrian *
577701Sbrian * Redistribution and use in source and binary forms, with or without
677701Sbrian * modification, are permitted provided that the following conditions
777701Sbrian * are met:
877701Sbrian * 1. Redistributions of source code must retain the above copyright
977701Sbrian *    notice, this list of conditions and the following disclaimer.
1077701Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1177701Sbrian *    notice, this list of conditions and the following disclaimer in the
1277701Sbrian *    documentation and/or other materials provided with the distribution.
1377701Sbrian *
1477701Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1577701Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1677701Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1777701Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1877701Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1977701Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2077701Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2177701Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2277701Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2377701Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2477701Sbrian * SUCH DAMAGE.
2577701Sbrian *
2677701Sbrian * $FreeBSD: head/sys/netinet/libalias/alias_local.h 145927 2005-05-05 21:05:38Z glebius $
2777701Sbrian */
2877701Sbrian
2974778Sbrian/*
3074778Sbrian * Alias_local.h contains the function prototypes for alias.c,
3174778Sbrian * alias_db.c, alias_util.c and alias_ftp.c, alias_irc.c (as well
3274778Sbrian * as any future add-ons).  It also includes macros, globals and
3374778Sbrian * struct definitions shared by more than one alias*.c file.
3474778Sbrian *
3574778Sbrian * This include file is intended to be used only within the aliasing
3674778Sbrian * software.  Outside world interfaces are defined in alias.h
3774778Sbrian *
3874778Sbrian * This software is placed into the public domain with no restrictions
3974778Sbrian * on its distribution.
4074778Sbrian *
4199207Sbrian * Initial version:  August, 1996  (cjm)
4274778Sbrian *
4374778Sbrian * <updated several times by original author and Eivind Eklund>
4474778Sbrian */
4526026Sbrian
4674778Sbrian#ifndef _ALIAS_LOCAL_H_
4774778Sbrian#define	_ALIAS_LOCAL_H_
4844307Sbrian
49124621Sphk#include <sys/queue.h>
50124621Sphk
51145927Sglebius/* Use kernel allocator. */
52145927Sglebius#if defined(_KERNEL) && defined(_SYS_MALLOC_H_)
53145927SglebiusMALLOC_DECLARE(M_ALIAS);
54145927Sglebius#define	malloc(x)	malloc(x, M_ALIAS, M_NOWAIT|M_ZERO)
55145927Sglebius#define	calloc(x, n)	malloc(x*n)
56145927Sglebius#define	free(x)		free(x, M_ALIAS)
57145927Sglebius#endif
58145927Sglebius
59145927Sglebius/* XXX: LibAliasSetTarget() uses this constant. */
60145927Sglebius#ifdef _KERNEL
61145927Sglebius#define	INADDR_NONE	0xffffffff
62145927Sglebius#endif
63145927Sglebius
64124621Sphk/* Sizes of input and output link tables */
65124621Sphk#define LINK_TABLE_OUT_SIZE         101
66124621Sphk#define LINK_TABLE_IN_SIZE         4001
67124621Sphk
68124621Sphkstruct proxy_entry;
69124621Sphk
70124621Sphkstruct libalias {
71127094Sdes	LIST_ENTRY(libalias) instancelist;
72124621Sphk
73127094Sdes	int		packetAliasMode;	/* Mode flags                      */
74127094Sdes	/* - documented in alias.h  */
75124621Sphk
76127094Sdes	struct in_addr	aliasAddress;	/* Address written onto source     */
77127094Sdes	/* field of IP packet.           */
78124621Sphk
79127094Sdes	struct in_addr	targetAddress;	/* IP address incoming packets     */
80127094Sdes	/* are sent to if no aliasing    */
81127094Sdes	/* link already exists           */
82124621Sphk
83127094Sdes	struct in_addr	nullAddress;	/* Used as a dummy parameter for   */
84127094Sdes	/* some function calls           */
85124621Sphk
86127094Sdes			LIST_HEAD     (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
87127094Sdes	/* Lookup table of pointers to     */
88127094Sdes	/* chains of link records. Each  */
89124621Sphk
90127094Sdes			LIST_HEAD     (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
91127094Sdes	/* link record is doubly indexed */
92127094Sdes	/* into input and output lookup  */
93127094Sdes	/* tables.                       */
94124621Sphk
95127094Sdes	/* Link statistics                 */
96127094Sdes	int		icmpLinkCount;
97127094Sdes	int		udpLinkCount;
98127094Sdes	int		tcpLinkCount;
99127094Sdes	int		pptpLinkCount;
100127094Sdes	int		protoLinkCount;
101127094Sdes	int		fragmentIdLinkCount;
102127094Sdes	int		fragmentPtrLinkCount;
103127094Sdes	int		sockCount;
104124621Sphk
105127094Sdes	int		cleanupIndex;	/* Index to chain of link table    */
106127094Sdes	/* being inspected for old links   */
107124621Sphk
108127094Sdes	int		timeStamp;	/* System time in seconds for      */
109127094Sdes	/* current packet                  */
110124621Sphk
111127094Sdes	int		lastCleanupTime;	/* Last time
112127094Sdes						 * IncrementalCleanup()  */
113127094Sdes	/* was called                      */
114124621Sphk
115127094Sdes	int		houseKeepingResidual;	/* used by HouseKeeping()          */
116124621Sphk
117127094Sdes	int		deleteAllLinks;	/* If equal to zero, DeleteLink()  */
118127094Sdes	/* will not remove permanent links */
119145925Sglebius#ifndef	NO_LOGGING
120127094Sdes	FILE           *monitorFile;	/* File descriptor for link        */
121145925Sglebius#endif
122127094Sdes	/* statistics monitoring file      */
123124621Sphk
124127094Sdes	int		newDefaultLink;	/* Indicates if a new aliasing     */
125127094Sdes	/* link has been created after a   */
126127094Sdes	/* call to PacketAliasIn/Out().    */
127124621Sphk
128124621Sphk#ifndef NO_FW_PUNCH
129127094Sdes	int		fireWallFD;	/* File descriptor to be able to   */
130127094Sdes	/* control firewall.  Opened by    */
131127094Sdes	/* PacketAliasSetMode on first     */
132127094Sdes	/* setting the PKT_ALIAS_PUNCH_FW  */
133127094Sdes	/* flag.                           */
134127094Sdes	int		fireWallBaseNum;	/* The first firewall entry
135127094Sdes						 * free for our use */
136127094Sdes	int		fireWallNumNums;	/* How many entries can we
137127094Sdes						 * use? */
138127094Sdes	int		fireWallActiveNum;	/* Which entry did we last
139127094Sdes						 * use? */
140127094Sdes	char           *fireWallField;	/* bool array for entries */
141124621Sphk#endif
142124621Sphk
143127094Sdes	unsigned int	skinnyPort;	/* TCP port used by the Skinny     */
144127094Sdes	/* protocol.                       */
145124621Sphk
146124621Sphk	struct proxy_entry *proxyList;
147124621Sphk
148127094Sdes	struct in_addr	true_addr;	/* in network byte order. */
149127094Sdes	u_short		true_port;	/* in host byte order. */
150124621Sphk
151124621Sphk};
152124621Sphk
15374778Sbrian/* Macros */
15426026Sbrian
15544307Sbrian/*
15674778Sbrian * The following macro is used to update an
15774778Sbrian * internet checksum.  "delta" is a 32-bit
15874778Sbrian * accumulation of all the changes to the
15974778Sbrian * checksum (adding in new 16-bit words and
16074778Sbrian * subtracting out old words), and "cksum"
16174778Sbrian * is the checksum value to be updated.
16244307Sbrian */
16374778Sbrian#define	ADJUST_CHECKSUM(acc, cksum) \
16474778Sbrian	do { \
16574778Sbrian		acc += cksum; \
16674778Sbrian		if (acc < 0) { \
16774778Sbrian			acc = -acc; \
16874778Sbrian			acc = (acc >> 16) + (acc & 0xffff); \
16974778Sbrian			acc += acc >> 16; \
17074778Sbrian			cksum = (u_short) ~acc; \
17174778Sbrian		} else { \
17274778Sbrian			acc = (acc >> 16) + (acc & 0xffff); \
17374778Sbrian			acc += acc >> 16; \
17474778Sbrian			cksum = (u_short) acc; \
17574778Sbrian		} \
17674778Sbrian	} while (0)
17744307Sbrian
17844307Sbrian
17974778Sbrian/* Prototypes */
18026026Sbrian
18126026Sbrian/* General utilities */
182127094Sdesu_short		IpChecksum(struct ip *_pip);
183127094Sdesu_short		TcpChecksum(struct ip *_pip);
184127094Sdesvoid
185127689SdesDifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
18626026Sbrian
18726026Sbrian/* Internal data access */
18826026Sbrianstruct alias_link *
189127094SdesFindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
190127094Sdes    u_short _id_alias, int _create);
19126026Sbrianstruct alias_link *
192127094SdesFindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
193127094Sdes    u_short _id, int _create);
19426026Sbrianstruct alias_link *
195127094SdesFindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
196127094Sdes    u_short _ip_id);
19726026Sbrianstruct alias_link *
198127094SdesFindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
199127094Sdes    u_short _ip_id);
20026026Sbrianstruct alias_link *
201127094Sdes		AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
20226026Sbrianstruct alias_link *
203127094Sdes		FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
20426026Sbrianstruct alias_link *
205127094SdesFindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
206127094Sdes    u_char _proto);
20759356Srustruct alias_link *
208127094SdesFindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
209127094Sdes    u_char _proto);
21059356Srustruct alias_link *
211127094SdesFindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
212127094Sdes    u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
21326026Sbrianstruct alias_link *
214127094SdesFindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
215127094Sdes    u_short _src_port, u_short _dst_port, u_char _proto, int _create);
21661861Srustruct alias_link *
217127094SdesAddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
218127094Sdes    struct in_addr _alias_addr, u_int16_t _src_call_id);
21961861Srustruct alias_link *
220127094SdesFindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
221127094Sdes    struct in_addr _dst_addr, u_int16_t _src_call_id);
22263899Sarchiestruct alias_link *
223127094SdesFindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
224127094Sdes    struct in_addr _alias_addr, u_int16_t _dst_call_id);
22567966Srustruct alias_link *
226127094SdesFindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
227127094Sdes    struct in_addr _dst_addr, u_int16_t _dst_call_id);
22867966Srustruct alias_link *
229127094SdesFindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
230127094Sdes    struct in_addr _alias_addr, u_int16_t _alias_call_id);
23167966Srustruct alias_link *
232127094SdesFindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
233127094Sdes    u_short _src_port, u_short _alias_port, u_char _proto);
23426026Sbrianstruct in_addr
235127094Sdes		FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
23626026Sbrianstruct in_addr
237127094Sdes		FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
23826026Sbrian
23926026Sbrian/* External data access/modification */
240127094Sdesint
241127094SdesFindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
242127094Sdes    u_short _src_port, u_short _dst_port, u_short _port_count,
243127094Sdes    u_char _proto, u_char _align);
244131614Sdesvoid		GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
245131614Sdesvoid		SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
246131614Sdesvoid		GetFragmentPtr(struct alias_link *_lnk, char **_fptr);
247131614Sdesvoid		SetFragmentPtr(struct alias_link *_lnk, char *fptr);
248131614Sdesvoid		SetStateIn(struct alias_link *_lnk, int _state);
249131614Sdesvoid		SetStateOut(struct alias_link *_lnk, int _state);
250131614Sdesint		GetStateIn (struct alias_link *_lnk);
251131614Sdesint		GetStateOut(struct alias_link *_lnk);
25274778Sbrianstruct in_addr
253131614Sdes		GetOriginalAddress(struct alias_link *_lnk);
25474778Sbrianstruct in_addr
255131614Sdes		GetDestAddress(struct alias_link *_lnk);
25674778Sbrianstruct in_addr
257131614Sdes		GetAliasAddress(struct alias_link *_lnk);
25874778Sbrianstruct in_addr
259127094Sdes		GetDefaultAliasAddress(struct libalias *la);
260127094Sdesvoid		SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
261131614Sdesu_short		GetOriginalPort(struct alias_link *_lnk);
262131614Sdesu_short		GetAliasPort(struct alias_link *_lnk);
26374778Sbrianstruct in_addr
264131614Sdes		GetProxyAddress(struct alias_link *_lnk);
265131614Sdesvoid		SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
266131614Sdesu_short		GetProxyPort(struct alias_link *_lnk);
267131614Sdesvoid		SetProxyPort(struct alias_link *_lnk, u_short _port);
268131614Sdesvoid		SetAckModified(struct alias_link *_lnk);
269131614Sdesint		GetAckModified(struct alias_link *_lnk);
270131614Sdesint		GetDeltaAckIn(struct ip *_pip, struct alias_link *_lnk);
271131614Sdesint		GetDeltaSeqOut(struct ip *_pip, struct alias_link *_lnk);
272131614Sdesvoid		AddSeq    (struct ip *_pip, struct alias_link *_lnk, int _delta);
273131614Sdesvoid		SetExpire (struct alias_link *_lnk, int _expire);
274127094Sdesvoid		ClearCheckNewLink(struct libalias *la);
275131614Sdesvoid		SetProtocolFlags(struct alias_link *_lnk, int _pflags);
276131614Sdesint		GetProtocolFlags(struct alias_link *_lnk);
277131614Sdesvoid		SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
278127094Sdes
27936711Sbrian#ifndef NO_FW_PUNCH
280131614Sdesvoid		PunchFWHole(struct alias_link *_lnk);
281127094Sdes
28236711Sbrian#endif
28326026Sbrian
28426026Sbrian/* Housekeeping function */
285127094Sdesvoid		HouseKeeping(struct libalias *);
28626026Sbrian
28726026Sbrian/* Tcp specfic routines */
28874778Sbrian/* lint -save -library Suppress flexelint warnings */
28944307Sbrian
29044307Sbrian/* FTP routines */
291127094Sdesvoid
292131614SdesAliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
293127094Sdes    int _maxpacketsize);
29444307Sbrian
29544307Sbrian/* IRC routines */
296127094Sdesvoid
297131614SdesAliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
298127094Sdes    int _maxsize);
29944307Sbrian
30063899Sarchie/* RTSP routines */
301127094Sdesvoid
302131614SdesAliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
303127094Sdes    int _maxpacketsize);
30463899Sarchie
30561861Sru/* PPTP routines */
306131614Sdesvoid		AliasHandlePptpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
307131614Sdesvoid		AliasHandlePptpIn(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
308127094Sdesint		AliasHandlePptpGreOut(struct libalias *la, struct ip *_pip);
309127094Sdesint		AliasHandlePptpGreIn(struct libalias *la, struct ip *_pip);
31061861Sru
31144307Sbrian/* NetBIOS routines */
312127094Sdesint
313131614SdesAliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
314127094Sdes    struct in_addr *_alias_address, u_short _alias_port);
315127094Sdesint
316131614SdesAliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
317127094Sdes    struct in_addr *_alias_address, u_short * _alias_port,
318127094Sdes    struct in_addr *_original_address, u_short * _original_port);
31944307Sbrian
32044307Sbrian/* CUSeeMe routines */
321131614Sdesvoid		AliasHandleCUSeeMeOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
322127094Sdesvoid		AliasHandleCUSeeMeIn(struct libalias *la, struct ip *_pip, struct in_addr _original_addr);
32327864Sbrian
324120372Smarcus/* Skinny routines */
325131614Sdesvoid		AliasHandleSkinny(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
326120372Smarcus
32744307Sbrian/* Transparent proxy routines */
328127094Sdesint
329127094SdesProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
330127094Sdes    u_short * _proxy_server_port);
331127094Sdesvoid
332131614SdesProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
333127094Sdes    int _maxpacketsize, int _proxy_type);
33436321Samurai
33532377Seivindenum alias_tcp_state {
33674778Sbrian	ALIAS_TCP_STATE_NOT_CONNECTED,
33774778Sbrian	ALIAS_TCP_STATE_CONNECTED,
33874778Sbrian	ALIAS_TCP_STATE_DISCONNECTED
33932377Seivind};
34074778Sbrian
341131699Sdes#if defined(_NETINET_IP_H_)
342131699Sdesstatic __inline void *
343131699Sdesip_next(struct ip *iphdr)
344131699Sdes{
345131699Sdes	char *p = (char *)iphdr;
346131699Sdes	return (&p[iphdr->ip_hl * 4]);
347131699Sdes}
348131699Sdes#endif
349131699Sdes
350131699Sdes#if defined(_NETINET_TCP_H_)
351131699Sdesstatic __inline void *
352131699Sdestcp_next(struct tcphdr *tcphdr)
353131699Sdes{
354131699Sdes	char *p = (char *)tcphdr;
355131699Sdes	return (&p[tcphdr->th_off * 4]);
356131699Sdes}
357131699Sdes#endif
358131699Sdes
359131699Sdes#if defined(_NETINET_UDP_H_)
360131699Sdesstatic __inline void *
361131699Sdesudp_next(struct udphdr *udphdr)
362131699Sdes{
363131699Sdes	return ((void *)(udphdr + 1));
364131699Sdes}
365131699Sdes#endif
366131699Sdes
36726026Sbrian/*lint -restore */
36874778Sbrian
369127094Sdes#endif				/* !_ALIAS_LOCAL_H_ */
370