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