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
70 #define GET_ALIAS_PORT -1
71 #define GET_ALIAS_ID GET_ALIAS_PORT
76 LIST_ENTRY(libalias) instancelist;
78 int packetAliasMode; /* Mode flags */
79 /* - documented in alias.h */
81 struct in_addr aliasAddress; /* Address written onto source */
82 /* field of IP packet. */
84 struct in_addr targetAddress; /* IP address incoming packets */
85 /* are sent to if no aliasing */
86 /* link already exists */
88 struct in_addr nullAddress; /* Used as a dummy parameter for */
89 /* some function calls */
91 LIST_HEAD (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
92 /* Lookup table of pointers to */
93 /* chains of link records. Each */
95 LIST_HEAD (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
96 /* link record is doubly indexed */
97 /* into input and output lookup */
100 /* Link statistics */
106 int fragmentIdLinkCount;
107 int fragmentPtrLinkCount;
110 int cleanupIndex; /* Index to chain of link table */
111 /* being inspected for old links */
113 int timeStamp; /* System time in seconds for */
116 int lastCleanupTime; /* Last time
117 * IncrementalCleanup() */
120 int deleteAllLinks; /* If equal to zero, DeleteLink() */
121 /* will not remove permanent links */
129 /* statistics monitoring */
131 int newDefaultLink; /* Indicates if a new aliasing */
132 /* link has been created after a */
133 /* call to PacketAliasIn/Out(). */
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 */
141 int fireWallBaseNum; /* The first firewall entry
142 * free for our use */
143 int fireWallNumNums; /* How many entries can we
145 int fireWallActiveNum; /* Which entry did we last
147 char *fireWallField; /* bool array for entries */
150 unsigned int skinnyPort; /* TCP port used by the Skinny */
153 struct proxy_entry *proxyList;
155 struct in_addr true_addr; /* in network byte order. */
156 u_short true_port; /* in host byte order. */
162 /* counts associations that have progressed to UP and not yet removed */
165 /* timing queue for keeping track of association timeouts */
166 struct sctp_nat_timer sctpNatTimer;
168 /* size of hash table used in this instance */
169 u_int sctpNatTableSize;
172 * local look up table sorted by l_vtag/l_port
174 LIST_HEAD(sctpNatTableL, sctp_nat_assoc) *sctpTableLocal;
176 * global look up table sorted by g_vtag/g_port
178 LIST_HEAD(sctpNatTableG, sctp_nat_assoc) *sctpTableGlobal;
181 * avoid races in libalias: every public function has to use it.
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)
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)
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.
212 #define ADJUST_CHECKSUM(acc, cksum) \
217 acc = (acc >> 16) + (acc & 0xffff); \
219 cksum = (u_short) ~acc; \
221 acc = (acc >> 16) + (acc & 0xffff); \
223 cksum = (u_short) acc; \
231 * SctpFunction prototypes
234 void AliasSctpInit(struct libalias *la);
235 void AliasSctpTerm(struct libalias *la);
236 int SctpAlias(struct libalias *la, struct ip *ip, int direction);
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
247 u_short IpChecksum(struct ip *_pip);
248 u_short TcpChecksum(struct ip *_pip);
251 DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
253 /* Internal data access */
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);
259 FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
260 u_short _id_alias, int _create);
262 FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
263 u_short _id, int _create);
265 FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
268 FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
271 AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
273 FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
275 FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
278 FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
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);
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);
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);
290 FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
291 struct in_addr _dst_addr, u_int16_t _src_call_id);
293 FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
294 struct in_addr _alias_addr, u_int16_t _dst_call_id);
296 FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
297 struct in_addr _dst_addr, u_int16_t _dst_call_id);
299 FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
300 struct in_addr _alias_addr, u_int16_t _alias_call_id);
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);
305 FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
307 FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
309 FindSctpRedirectAddress(struct libalias *la, struct sctp_nat_msg *sm);
311 /* External data access/modification */
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);
325 GetOriginalAddress(struct alias_link *_lnk);
327 GetDestAddress(struct alias_link *_lnk);
329 GetAliasAddress(struct alias_link *_lnk);
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);
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);
353 void PunchFWHole(struct alias_link *_lnk);
357 /* Housekeeping function */
358 void HouseKeeping(struct libalias *);
360 /* Tcp specfic routines */
361 /* lint -save -library Suppress flexelint warnings */
363 /* Transparent proxy routines */
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);
369 ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
370 int _maxpacketsize, int _proxy_type);
372 enum alias_tcp_state {
373 ALIAS_TCP_STATE_NOT_CONNECTED,
374 ALIAS_TCP_STATE_CONNECTED,
375 ALIAS_TCP_STATE_DISCONNECTED
378 #if defined(_NETINET_IP_H_)
379 static __inline void *
380 ip_next(struct ip *iphdr)
382 char *p = (char *)iphdr;
383 return (&p[iphdr->ip_hl * 4]);
387 #if defined(_NETINET_TCP_H_)
388 static __inline void *
389 tcp_next(struct tcphdr *tcphdr)
391 char *p = (char *)tcphdr;
392 return (&p[tcphdr->th_off * 4]);
396 #if defined(_NETINET_UDP_H_)
397 static __inline void *
398 udp_next(struct udphdr *udphdr)
400 return ((void *)(udphdr + 1));
404 #endif /* !_ALIAS_LOCAL_H_ */