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