2 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.
35 * This include file is intended to be used only within the aliasing
36 * software. Outside world interfaces are defined in alias.h
38 * This software is placed into the public domain with no restrictions
39 * on its distribution.
41 * Initial version: August, 1996 (cjm)
43 * <updated several times by original author and Eivind Eklund>
46 #ifndef _ALIAS_LOCAL_H_
47 #define _ALIAS_LOCAL_H_
49 #include <sys/types.h>
50 #include <sys/sysctl.h>
53 #include <sys/malloc.h>
54 #include <sys/param.h>
56 #include <sys/mutex.h>
58 /* XXX: LibAliasSetTarget() uses this constant. */
59 #define INADDR_NONE 0xffffffff
61 #include <netinet/libalias/alias_sctp.h>
63 #include "alias_sctp.h"
66 /* Sizes of input and output link tables */
67 #define LINK_TABLE_OUT_SIZE 4001
68 #define LINK_TABLE_IN_SIZE 4001
73 LIST_ENTRY(libalias) instancelist;
75 int packetAliasMode; /* Mode flags */
76 /* - documented in alias.h */
78 struct in_addr aliasAddress; /* Address written onto source */
79 /* field of IP packet. */
81 struct in_addr targetAddress; /* IP address incoming packets */
82 /* are sent to if no aliasing */
83 /* link already exists */
85 struct in_addr nullAddress; /* Used as a dummy parameter for */
86 /* some function calls */
88 LIST_HEAD (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
89 /* Lookup table of pointers to */
90 /* chains of link records. Each */
92 LIST_HEAD (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
93 /* link record is doubly indexed */
94 /* into input and output lookup */
103 int fragmentIdLinkCount;
104 int fragmentPtrLinkCount;
107 int cleanupIndex; /* Index to chain of link table */
108 /* being inspected for old links */
110 int timeStamp; /* System time in seconds for */
113 int lastCleanupTime; /* Last time
114 * IncrementalCleanup() */
117 int deleteAllLinks; /* If equal to zero, DeleteLink() */
118 /* will not remove permanent links */
126 /* statistics monitoring */
128 int newDefaultLink; /* Indicates if a new aliasing */
129 /* link has been created after a */
130 /* call to PacketAliasIn/Out(). */
133 int fireWallFD; /* File descriptor to be able to */
134 /* control firewall. Opened by */
135 /* PacketAliasSetMode on first */
136 /* setting the PKT_ALIAS_PUNCH_FW */
138 int fireWallBaseNum; /* The first firewall entry
139 * free for our use */
140 int fireWallNumNums; /* How many entries can we
142 int fireWallActiveNum; /* Which entry did we last
144 char *fireWallField; /* bool array for entries */
147 unsigned int skinnyPort; /* TCP port used by the Skinny */
150 struct proxy_entry *proxyList;
152 struct in_addr true_addr; /* in network byte order. */
153 u_short true_port; /* in host byte order. */
159 /* counts associations that have progressed to UP and not yet removed */
162 /* timing queue for keeping track of association timeouts */
163 struct sctp_nat_timer sctpNatTimer;
165 /* size of hash table used in this instance */
166 u_int sctpNatTableSize;
169 * local look up table sorted by l_vtag/l_port
171 LIST_HEAD(sctpNatTableL, sctp_nat_assoc) *sctpTableLocal;
173 * global look up table sorted by g_vtag/g_port
175 LIST_HEAD(sctpNatTableG, sctp_nat_assoc) *sctpTableGlobal;
178 * avoid races in libalias: every public function has to use it.
187 #define LIBALIAS_LOCK_INIT(l) \
188 mtx_init(&l->mutex, "per-instance libalias mutex", NULL, MTX_DEF)
189 #define LIBALIAS_LOCK_ASSERT(l) mtx_assert(&l->mutex, MA_OWNED)
190 #define LIBALIAS_LOCK(l) mtx_lock(&l->mutex)
191 #define LIBALIAS_UNLOCK(l) mtx_unlock(&l->mutex)
192 #define LIBALIAS_LOCK_DESTROY(l) mtx_destroy(&l->mutex)
194 #define LIBALIAS_LOCK_INIT(l)
195 #define LIBALIAS_LOCK_ASSERT(l)
196 #define LIBALIAS_LOCK(l)
197 #define LIBALIAS_UNLOCK(l)
198 #define LIBALIAS_LOCK_DESTROY(l)
202 * The following macro is used to update an
203 * internet checksum. "delta" is a 32-bit
204 * accumulation of all the changes to the
205 * checksum (adding in new 16-bit words and
206 * subtracting out old words), and "cksum"
207 * is the checksum value to be updated.
209 #define ADJUST_CHECKSUM(acc, cksum) \
214 acc = (acc >> 16) + (acc & 0xffff); \
216 cksum = (u_short) ~acc; \
218 acc = (acc >> 16) + (acc & 0xffff); \
220 cksum = (u_short) acc; \
228 * SctpFunction prototypes
231 void AliasSctpInit(struct libalias *la);
232 void AliasSctpTerm(struct libalias *la);
233 int SctpAlias(struct libalias *la, struct ip *ip, int direction);
236 * We do not calculate TCP checksums when libalias is a kernel
237 * module, since it has no idea about checksum offloading.
238 * If TCP data has changed, then we just set checksum to zero,
239 * and caller must recalculate it himself.
240 * In case if libalias will edit UDP data, the same approach
244 u_short IpChecksum(struct ip *_pip);
245 u_short TcpChecksum(struct ip *_pip);
248 DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
250 /* Internal data access */
252 FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
253 u_short _id_alias, int _create);
255 FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
256 u_short _id, int _create);
258 FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
261 FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
264 AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
266 FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
268 FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
271 FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
274 FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
275 u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
277 FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
278 u_short _src_port, u_short _dst_port, u_char _proto, int _create);
280 AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
281 struct in_addr _alias_addr, u_int16_t _src_call_id);
283 FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
284 struct in_addr _dst_addr, u_int16_t _src_call_id);
286 FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
287 struct in_addr _alias_addr, u_int16_t _dst_call_id);
289 FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
290 struct in_addr _dst_addr, u_int16_t _dst_call_id);
292 FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
293 struct in_addr _alias_addr, u_int16_t _alias_call_id);
295 FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
296 u_short _src_port, u_short _alias_port, u_char _proto);
298 FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
300 FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
302 FindSctpRedirectAddress(struct libalias *la, struct sctp_nat_msg *sm);
304 /* External data access/modification */
306 FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
307 u_short _src_port, u_short _dst_port, u_short _port_count,
308 u_char _proto, u_char _align);
309 void GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
310 void SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
311 void GetFragmentPtr(struct alias_link *_lnk, char **_fptr);
312 void SetFragmentPtr(struct alias_link *_lnk, char *fptr);
313 void SetStateIn(struct alias_link *_lnk, int _state);
314 void SetStateOut(struct alias_link *_lnk, int _state);
315 int GetStateIn (struct alias_link *_lnk);
316 int GetStateOut(struct alias_link *_lnk);
318 GetOriginalAddress(struct alias_link *_lnk);
320 GetDestAddress(struct alias_link *_lnk);
322 GetAliasAddress(struct alias_link *_lnk);
324 GetDefaultAliasAddress(struct libalias *la);
325 void SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
326 u_short GetOriginalPort(struct alias_link *_lnk);
327 u_short GetAliasPort(struct alias_link *_lnk);
329 GetProxyAddress(struct alias_link *_lnk);
330 void SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
331 u_short GetProxyPort(struct alias_link *_lnk);
332 void SetProxyPort(struct alias_link *_lnk, u_short _port);
333 void SetAckModified(struct alias_link *_lnk);
334 int GetAckModified(struct alias_link *_lnk);
335 int GetDeltaAckIn(u_long, struct alias_link *_lnk);
336 int GetDeltaSeqOut(u_long, struct alias_link *lnk);
337 void AddSeq(struct alias_link *lnk, int delta, u_int ip_hl,
338 u_short ip_len, u_long th_seq, u_int th_off);
339 void SetExpire (struct alias_link *_lnk, int _expire);
340 void ClearCheckNewLink(struct libalias *la);
341 void SetProtocolFlags(struct alias_link *_lnk, int _pflags);
342 int GetProtocolFlags(struct alias_link *_lnk);
343 void SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
346 void PunchFWHole(struct alias_link *_lnk);
350 /* Housekeeping function */
351 void HouseKeeping(struct libalias *);
353 /* Tcp specfic routines */
354 /* lint -save -library Suppress flexelint warnings */
356 /* Transparent proxy routines */
358 ProxyCheck(struct libalias *la, struct in_addr *proxy_server_addr,
359 u_short * proxy_server_port, struct in_addr src_addr,
360 struct in_addr dst_addr, u_short dst_port, u_char ip_p);
362 ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
363 int _maxpacketsize, int _proxy_type);
365 enum alias_tcp_state {
366 ALIAS_TCP_STATE_NOT_CONNECTED,
367 ALIAS_TCP_STATE_CONNECTED,
368 ALIAS_TCP_STATE_DISCONNECTED
371 #if defined(_NETINET_IP_H_)
372 static __inline void *
373 ip_next(struct ip *iphdr)
375 char *p = (char *)iphdr;
376 return (&p[iphdr->ip_hl * 4]);
380 #if defined(_NETINET_TCP_H_)
381 static __inline void *
382 tcp_next(struct tcphdr *tcphdr)
384 char *p = (char *)tcphdr;
385 return (&p[tcphdr->th_off * 4]);
389 #if defined(_NETINET_UDP_H_)
390 static __inline void *
391 udp_next(struct udphdr *udphdr)
393 return ((void *)(udphdr + 1));
397 #endif /* !_ALIAS_LOCAL_H_ */