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