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