alias_local.h revision 131699
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 131699 2004-07-06 12:13:28Z des $
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
51124621Sphk/* Sizes of input and output link tables */
52124621Sphk#define LINK_TABLE_OUT_SIZE         101
53124621Sphk#define LINK_TABLE_IN_SIZE         4001
54124621Sphk
55124621Sphkstruct proxy_entry;
56124621Sphk
57124621Sphkstruct libalias {
58127094Sdes	LIST_ENTRY(libalias) instancelist;
59124621Sphk
60127094Sdes	int		packetAliasMode;	/* Mode flags                      */
61127094Sdes	/* - documented in alias.h  */
62124621Sphk
63127094Sdes	struct in_addr	aliasAddress;	/* Address written onto source     */
64127094Sdes	/* field of IP packet.           */
65124621Sphk
66127094Sdes	struct in_addr	targetAddress;	/* IP address incoming packets     */
67127094Sdes	/* are sent to if no aliasing    */
68127094Sdes	/* link already exists           */
69124621Sphk
70127094Sdes	struct in_addr	nullAddress;	/* Used as a dummy parameter for   */
71127094Sdes	/* some function calls           */
72124621Sphk
73127094Sdes			LIST_HEAD     (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
74127094Sdes	/* Lookup table of pointers to     */
75127094Sdes	/* chains of link records. Each  */
76124621Sphk
77127094Sdes			LIST_HEAD     (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
78127094Sdes	/* link record is doubly indexed */
79127094Sdes	/* into input and output lookup  */
80127094Sdes	/* tables.                       */
81124621Sphk
82127094Sdes	/* Link statistics                 */
83127094Sdes	int		icmpLinkCount;
84127094Sdes	int		udpLinkCount;
85127094Sdes	int		tcpLinkCount;
86127094Sdes	int		pptpLinkCount;
87127094Sdes	int		protoLinkCount;
88127094Sdes	int		fragmentIdLinkCount;
89127094Sdes	int		fragmentPtrLinkCount;
90127094Sdes	int		sockCount;
91124621Sphk
92127094Sdes	int		cleanupIndex;	/* Index to chain of link table    */
93127094Sdes	/* being inspected for old links   */
94124621Sphk
95127094Sdes	int		timeStamp;	/* System time in seconds for      */
96127094Sdes	/* current packet                  */
97124621Sphk
98127094Sdes	int		lastCleanupTime;	/* Last time
99127094Sdes						 * IncrementalCleanup()  */
100127094Sdes	/* was called                      */
101124621Sphk
102127094Sdes	int		houseKeepingResidual;	/* used by HouseKeeping()          */
103124621Sphk
104127094Sdes	int		deleteAllLinks;	/* If equal to zero, DeleteLink()  */
105127094Sdes	/* will not remove permanent links */
106124621Sphk
107127094Sdes	FILE           *monitorFile;	/* File descriptor for link        */
108127094Sdes	/* statistics monitoring file      */
109124621Sphk
110127094Sdes	int		newDefaultLink;	/* Indicates if a new aliasing     */
111127094Sdes	/* link has been created after a   */
112127094Sdes	/* call to PacketAliasIn/Out().    */
113124621Sphk
114124621Sphk#ifndef NO_FW_PUNCH
115127094Sdes	int		fireWallFD;	/* File descriptor to be able to   */
116127094Sdes	/* control firewall.  Opened by    */
117127094Sdes	/* PacketAliasSetMode on first     */
118127094Sdes	/* setting the PKT_ALIAS_PUNCH_FW  */
119127094Sdes	/* flag.                           */
120127094Sdes	int		fireWallBaseNum;	/* The first firewall entry
121127094Sdes						 * free for our use */
122127094Sdes	int		fireWallNumNums;	/* How many entries can we
123127094Sdes						 * use? */
124127094Sdes	int		fireWallActiveNum;	/* Which entry did we last
125127094Sdes						 * use? */
126127094Sdes	char           *fireWallField;	/* bool array for entries */
127124621Sphk#endif
128124621Sphk
129127094Sdes	unsigned int	skinnyPort;	/* TCP port used by the Skinny     */
130127094Sdes	/* protocol.                       */
131124621Sphk
132124621Sphk	struct proxy_entry *proxyList;
133124621Sphk
134127094Sdes	struct in_addr	true_addr;	/* in network byte order. */
135127094Sdes	u_short		true_port;	/* in host byte order. */
136124621Sphk
137124621Sphk};
138124621Sphk
13974778Sbrian/* Macros */
14026026Sbrian
14144307Sbrian/*
14274778Sbrian * The following macro is used to update an
14374778Sbrian * internet checksum.  "delta" is a 32-bit
14474778Sbrian * accumulation of all the changes to the
14574778Sbrian * checksum (adding in new 16-bit words and
14674778Sbrian * subtracting out old words), and "cksum"
14774778Sbrian * is the checksum value to be updated.
14844307Sbrian */
14974778Sbrian#define	ADJUST_CHECKSUM(acc, cksum) \
15074778Sbrian	do { \
15174778Sbrian		acc += cksum; \
15274778Sbrian		if (acc < 0) { \
15374778Sbrian			acc = -acc; \
15474778Sbrian			acc = (acc >> 16) + (acc & 0xffff); \
15574778Sbrian			acc += acc >> 16; \
15674778Sbrian			cksum = (u_short) ~acc; \
15774778Sbrian		} else { \
15874778Sbrian			acc = (acc >> 16) + (acc & 0xffff); \
15974778Sbrian			acc += acc >> 16; \
16074778Sbrian			cksum = (u_short) acc; \
16174778Sbrian		} \
16274778Sbrian	} while (0)
16344307Sbrian
16444307Sbrian
16574778Sbrian/* Prototypes */
16626026Sbrian
16726026Sbrian/* General utilities */
168127094Sdesu_short		IpChecksum(struct ip *_pip);
169127094Sdesu_short		TcpChecksum(struct ip *_pip);
170127094Sdesvoid
171127689SdesDifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
17226026Sbrian
17326026Sbrian/* Internal data access */
17426026Sbrianstruct alias_link *
175127094SdesFindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
176127094Sdes    u_short _id_alias, int _create);
17726026Sbrianstruct alias_link *
178127094SdesFindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
179127094Sdes    u_short _id, int _create);
18026026Sbrianstruct alias_link *
181127094SdesFindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
182127094Sdes    u_short _ip_id);
18326026Sbrianstruct alias_link *
184127094SdesFindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
185127094Sdes    u_short _ip_id);
18626026Sbrianstruct alias_link *
187127094Sdes		AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
18826026Sbrianstruct alias_link *
189127094Sdes		FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
19026026Sbrianstruct alias_link *
191127094SdesFindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
192127094Sdes    u_char _proto);
19359356Srustruct alias_link *
194127094SdesFindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
195127094Sdes    u_char _proto);
19659356Srustruct alias_link *
197127094SdesFindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
198127094Sdes    u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
19926026Sbrianstruct alias_link *
200127094SdesFindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
201127094Sdes    u_short _src_port, u_short _dst_port, u_char _proto, int _create);
20261861Srustruct alias_link *
203127094SdesAddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
204127094Sdes    struct in_addr _alias_addr, u_int16_t _src_call_id);
20561861Srustruct alias_link *
206127094SdesFindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
207127094Sdes    struct in_addr _dst_addr, u_int16_t _src_call_id);
20863899Sarchiestruct alias_link *
209127094SdesFindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
210127094Sdes    struct in_addr _alias_addr, u_int16_t _dst_call_id);
21167966Srustruct alias_link *
212127094SdesFindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
213127094Sdes    struct in_addr _dst_addr, u_int16_t _dst_call_id);
21467966Srustruct alias_link *
215127094SdesFindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
216127094Sdes    struct in_addr _alias_addr, u_int16_t _alias_call_id);
21767966Srustruct alias_link *
218127094SdesFindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
219127094Sdes    u_short _src_port, u_short _alias_port, u_char _proto);
22026026Sbrianstruct in_addr
221127094Sdes		FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
22226026Sbrianstruct in_addr
223127094Sdes		FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
22426026Sbrian
22526026Sbrian/* External data access/modification */
226127094Sdesint
227127094SdesFindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
228127094Sdes    u_short _src_port, u_short _dst_port, u_short _port_count,
229127094Sdes    u_char _proto, u_char _align);
230131614Sdesvoid		GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
231131614Sdesvoid		SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
232131614Sdesvoid		GetFragmentPtr(struct alias_link *_lnk, char **_fptr);
233131614Sdesvoid		SetFragmentPtr(struct alias_link *_lnk, char *fptr);
234131614Sdesvoid		SetStateIn(struct alias_link *_lnk, int _state);
235131614Sdesvoid		SetStateOut(struct alias_link *_lnk, int _state);
236131614Sdesint		GetStateIn (struct alias_link *_lnk);
237131614Sdesint		GetStateOut(struct alias_link *_lnk);
23874778Sbrianstruct in_addr
239131614Sdes		GetOriginalAddress(struct alias_link *_lnk);
24074778Sbrianstruct in_addr
241131614Sdes		GetDestAddress(struct alias_link *_lnk);
24274778Sbrianstruct in_addr
243131614Sdes		GetAliasAddress(struct alias_link *_lnk);
24474778Sbrianstruct in_addr
245127094Sdes		GetDefaultAliasAddress(struct libalias *la);
246127094Sdesvoid		SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
247131614Sdesu_short		GetOriginalPort(struct alias_link *_lnk);
248131614Sdesu_short		GetAliasPort(struct alias_link *_lnk);
24974778Sbrianstruct in_addr
250131614Sdes		GetProxyAddress(struct alias_link *_lnk);
251131614Sdesvoid		SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
252131614Sdesu_short		GetProxyPort(struct alias_link *_lnk);
253131614Sdesvoid		SetProxyPort(struct alias_link *_lnk, u_short _port);
254131614Sdesvoid		SetAckModified(struct alias_link *_lnk);
255131614Sdesint		GetAckModified(struct alias_link *_lnk);
256131614Sdesint		GetDeltaAckIn(struct ip *_pip, struct alias_link *_lnk);
257131614Sdesint		GetDeltaSeqOut(struct ip *_pip, struct alias_link *_lnk);
258131614Sdesvoid		AddSeq    (struct ip *_pip, struct alias_link *_lnk, int _delta);
259131614Sdesvoid		SetExpire (struct alias_link *_lnk, int _expire);
260127094Sdesvoid		ClearCheckNewLink(struct libalias *la);
261131614Sdesvoid		SetProtocolFlags(struct alias_link *_lnk, int _pflags);
262131614Sdesint		GetProtocolFlags(struct alias_link *_lnk);
263131614Sdesvoid		SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
264127094Sdes
26536711Sbrian#ifndef NO_FW_PUNCH
266131614Sdesvoid		PunchFWHole(struct alias_link *_lnk);
267127094Sdes
26836711Sbrian#endif
26926026Sbrian
27026026Sbrian/* Housekeeping function */
271127094Sdesvoid		HouseKeeping(struct libalias *);
27226026Sbrian
27326026Sbrian/* Tcp specfic routines */
27474778Sbrian/* lint -save -library Suppress flexelint warnings */
27544307Sbrian
27644307Sbrian/* FTP routines */
277127094Sdesvoid
278131614SdesAliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
279127094Sdes    int _maxpacketsize);
28044307Sbrian
28144307Sbrian/* IRC routines */
282127094Sdesvoid
283131614SdesAliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
284127094Sdes    int _maxsize);
28544307Sbrian
28663899Sarchie/* RTSP routines */
287127094Sdesvoid
288131614SdesAliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
289127094Sdes    int _maxpacketsize);
29063899Sarchie
29161861Sru/* PPTP routines */
292131614Sdesvoid		AliasHandlePptpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
293131614Sdesvoid		AliasHandlePptpIn(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
294127094Sdesint		AliasHandlePptpGreOut(struct libalias *la, struct ip *_pip);
295127094Sdesint		AliasHandlePptpGreIn(struct libalias *la, struct ip *_pip);
29661861Sru
29744307Sbrian/* NetBIOS routines */
298127094Sdesint
299131614SdesAliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
300127094Sdes    struct in_addr *_alias_address, u_short _alias_port);
301127094Sdesint
302131614SdesAliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_lnk,
303127094Sdes    struct in_addr *_alias_address, u_short * _alias_port,
304127094Sdes    struct in_addr *_original_address, u_short * _original_port);
30544307Sbrian
30644307Sbrian/* CUSeeMe routines */
307131614Sdesvoid		AliasHandleCUSeeMeOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
308127094Sdesvoid		AliasHandleCUSeeMeIn(struct libalias *la, struct ip *_pip, struct in_addr _original_addr);
30927864Sbrian
310120372Smarcus/* Skinny routines */
311131614Sdesvoid		AliasHandleSkinny(struct libalias *la, struct ip *_pip, struct alias_link *_lnk);
312120372Smarcus
31344307Sbrian/* Transparent proxy routines */
314127094Sdesint
315127094SdesProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr,
316127094Sdes    u_short * _proxy_server_port);
317127094Sdesvoid
318131614SdesProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
319127094Sdes    int _maxpacketsize, int _proxy_type);
32036321Samurai
32132377Seivindenum alias_tcp_state {
32274778Sbrian	ALIAS_TCP_STATE_NOT_CONNECTED,
32374778Sbrian	ALIAS_TCP_STATE_CONNECTED,
32474778Sbrian	ALIAS_TCP_STATE_DISCONNECTED
32532377Seivind};
32674778Sbrian
327131699Sdes#if defined(_NETINET_IP_H_)
328131699Sdesstatic __inline void *
329131699Sdesip_next(struct ip *iphdr)
330131699Sdes{
331131699Sdes	char *p = (char *)iphdr;
332131699Sdes	return (&p[iphdr->ip_hl * 4]);
333131699Sdes}
334131699Sdes#endif
335131699Sdes
336131699Sdes#if defined(_NETINET_TCP_H_)
337131699Sdesstatic __inline void *
338131699Sdestcp_next(struct tcphdr *tcphdr)
339131699Sdes{
340131699Sdes	char *p = (char *)tcphdr;
341131699Sdes	return (&p[tcphdr->th_off * 4]);
342131699Sdes}
343131699Sdes#endif
344131699Sdes
345131699Sdes#if defined(_NETINET_UDP_H_)
346131699Sdesstatic __inline void *
347131699Sdesudp_next(struct udphdr *udphdr)
348131699Sdes{
349131699Sdes	return ((void *)(udphdr + 1));
350131699Sdes}
351131699Sdes#endif
352131699Sdes
35326026Sbrian/*lint -restore */
35474778Sbrian
355127094Sdes#endif				/* !_ALIAS_LOCAL_H_ */
356