2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Alias_local.h contains the function prototypes for alias.c,
33 * alias_db.c, alias_util.c and alias_ftp.c, alias_irc.c (as well
34 * as any future add-ons). It also includes macros, globals and
35 * struct definitions shared by more than one alias*.c file.
37 * This include file is intended to be used only within the aliasing
38 * software. Outside world interfaces are defined in alias.h
40 * This software is placed into the public domain with no restrictions
41 * on its distribution.
43 * Initial version: August, 1996 (cjm)
45 * <updated several times by original author and Eivind Eklund>
48 #ifndef _ALIAS_LOCAL_H_
49 #define _ALIAS_LOCAL_H_
52 #include <sys/types.h>
53 #include <sys/sysctl.h>
56 #include <sys/malloc.h>
57 #include <sys/param.h>
59 #include <sys/mutex.h>
61 /* XXX: LibAliasSetTarget() uses this constant. */
62 #define INADDR_NONE 0xffffffff
64 #include <netinet/libalias/alias_sctp.h>
66 #include "alias_sctp.h"
69 /* Sizes of input and output link tables */
70 #define GET_ALIAS_PORT -1
71 #define GET_ALIAS_ID GET_ALIAS_PORT
74 #define INET_NTOA_BUF(buf) (buf)
76 #define INET_NTOA_BUF(buf) (buf), sizeof(buf)
82 struct in_addr alias_addr;
85 SPLAY_ENTRY(group_in) in;
86 LIST_HEAD(, alias_link) full, partial;
90 LIST_ENTRY(libalias) instancelist;
91 /* Mode flags documented in alias.h */
93 /* Address written onto source field of IP packet. */
94 struct in_addr aliasAddress;
95 /* IP address incoming packets are sent to
96 * if no aliasing link already exists */
97 struct in_addr targetAddress;
98 /* Lookup table of pointers to chains of link records.
99 * Each link record is doubly indexed into input and
100 * output lookup tables. */
101 SPLAY_HEAD(splay_out, alias_link) linkSplayOut;
102 SPLAY_HEAD(splay_in, group_in) linkSplayIn;
103 LIST_HEAD (, alias_link) pptpList;
105 TAILQ_HEAD (, alias_link) checkExpire;
106 /* Link statistics */
107 unsigned int icmpLinkCount;
108 unsigned int udpLinkCount;
109 unsigned int tcpLinkCount;
110 unsigned int pptpLinkCount;
111 unsigned int protoLinkCount;
112 unsigned int fragmentIdLinkCount;
113 unsigned int fragmentPtrLinkCount;
114 unsigned int sockCount;
123 /* File descriptor to be able to control firewall.
124 * Opened by PacketAliasSetMode on first setting
125 * the PKT_ALIAS_PUNCH_FW flag. */
127 /* The first firewall entry free for our use */
129 /* How many entries can we use? */
131 /* Which entry did we last use? */
132 int fireWallActiveNum;
133 /* bool array for entries */
136 /* TCP port used by the Skinny protocol. */
137 unsigned int skinnyPort;
139 struct proxy_entry *proxyList;
141 struct in_addr true_addr; /* in network byte order. */
142 u_short true_port; /* in host byte order. */
144 /* Port ranges for aliasing. */
145 u_short aliasPortLower;
146 u_short aliasPortLength;
152 /* counts associations that have progressed to UP and not yet removed */
155 /* timing queue for keeping track of association timeouts */
156 struct sctp_nat_timer sctpNatTimer;
157 /* size of hash table used in this instance */
158 u_int sctpNatTableSize;
159 /* local look up table sorted by l_vtag/l_port */
160 LIST_HEAD(sctpNatTableL, sctp_nat_assoc) *sctpTableLocal;
161 /* global look up table sorted by g_vtag/g_port */
162 LIST_HEAD(sctpNatTableG, sctp_nat_assoc) *sctpTableGlobal;
164 /* avoid races in libalias: every public function has to use it. */
172 #define LIBALIAS_LOCK_INIT(l) \
173 mtx_init(&l->mutex, "per-instance libalias mutex", NULL, MTX_DEF)
174 #define LIBALIAS_LOCK_ASSERT(l) mtx_assert(&l->mutex, MA_OWNED)
175 #define LIBALIAS_LOCK(l) mtx_lock(&l->mutex)
176 #define LIBALIAS_UNLOCK(l) mtx_unlock(&l->mutex)
177 #define LIBALIAS_LOCK_DESTROY(l) mtx_destroy(&l->mutex)
179 #define LIBALIAS_LOCK_INIT(l)
180 #define LIBALIAS_LOCK_ASSERT(l)
181 #define LIBALIAS_LOCK(l)
182 #define LIBALIAS_UNLOCK(l)
183 #define LIBALIAS_LOCK_DESTROY(l)
187 * The following macro is used to update an
188 * internet checksum. "delta" is a 32-bit
189 * accumulation of all the changes to the
190 * checksum (adding in new 16-bit words and
191 * subtracting out old words), and "cksum"
192 * is the checksum value to be updated.
194 #define ADJUST_CHECKSUM(acc, cksum) \
199 acc = (acc >> 16) + (acc & 0xffff); \
201 cksum = (u_short) ~acc; \
203 acc = (acc >> 16) + (acc & 0xffff); \
205 cksum = (u_short) acc; \
211 /* System time in seconds for current packet */
212 extern int LibAliasTime;
215 * SctpFunction prototypes
218 void AliasSctpInit(struct libalias *la);
219 void AliasSctpTerm(struct libalias *la);
220 int SctpAlias(struct libalias *la, struct ip *ip, int direction);
223 * We do not calculate TCP checksums when libalias is a kernel
224 * module, since it has no idea about checksum offloading.
225 * If TCP data has changed, then we just set checksum to zero,
226 * and caller must recalculate it himself.
227 * In case if libalias will edit UDP data, the same approach
231 u_short IpChecksum(struct ip *_pip);
232 u_short TcpChecksum(struct ip *_pip);
235 DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
237 /* Internal data access */
239 AddLink(struct libalias *la, struct in_addr src_addr, struct in_addr dst_addr,
240 struct in_addr alias_addr, u_short src_port, u_short dst_port,
241 int alias_param, int link_type);
243 FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
244 u_short _id_alias, int _create);
246 FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
247 u_short _id, int _create);
249 FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
252 FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
255 AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
257 FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
259 FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
262 FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
265 FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
266 u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
268 FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
269 u_short _src_port, u_short _dst_port, u_char _proto, int _create);
271 AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
272 struct in_addr _alias_addr, u_int16_t _src_call_id);
274 FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
275 struct in_addr _dst_addr, u_int16_t _src_call_id);
277 FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
278 struct in_addr _alias_addr, u_int16_t _dst_call_id);
280 FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
281 struct in_addr _dst_addr, u_int16_t _dst_call_id);
283 FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
284 struct in_addr _alias_addr, u_int16_t _alias_call_id);
286 FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
287 u_short _src_port, u_short _alias_port, u_char _proto);
289 FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
291 FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
293 FindSctpRedirectAddress(struct libalias *la, struct sctp_nat_msg *sm);
295 /* External data access/modification */
296 int FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr,
297 struct in_addr _alias_addr, u_short _src_port,
298 u_short _dst_port, u_short _port_count, u_char _proto,
300 void GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
301 void SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
302 void GetFragmentPtr(struct alias_link *_lnk, void **_fptr);
303 void SetFragmentPtr(struct alias_link *_lnk, void *fptr);
304 void SetStateIn(struct alias_link *_lnk, int _state);
305 void SetStateOut(struct alias_link *_lnk, int _state);
306 int GetStateIn (struct alias_link *_lnk);
307 int GetStateOut(struct alias_link *_lnk);
308 struct in_addr GetOriginalAddress(struct alias_link *_lnk);
309 struct in_addr GetDestAddress(struct alias_link *_lnk);
310 struct in_addr GetAliasAddress(struct alias_link *_lnk);
311 struct in_addr GetDefaultAliasAddress(struct libalias *la);
312 void SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
313 u_short GetOriginalPort(struct alias_link *_lnk);
314 u_short GetAliasPort(struct alias_link *_lnk);
315 struct in_addr GetProxyAddress(struct alias_link *_lnk);
316 void SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
317 u_short GetProxyPort(struct alias_link *_lnk);
318 void SetProxyPort(struct alias_link *_lnk, u_short _port);
319 void SetAckModified(struct alias_link *_lnk);
320 int GetAckModified(struct alias_link *_lnk);
321 int GetDeltaAckIn(u_long, struct alias_link *_lnk);
322 int GetDeltaSeqOut(u_long, struct alias_link *lnk);
323 void AddSeq(struct alias_link *lnk, int delta, u_int ip_hl,
324 u_short ip_len, u_long th_seq, u_int th_off);
325 void SetExpire (struct alias_link *_lnk, int _expire);
326 void SetProtocolFlags(struct alias_link *_lnk, int _pflags);
327 int GetProtocolFlags(struct alias_link *_lnk);
328 void SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
331 void PunchFWHole(struct alias_link *_lnk);
335 /* Housekeeping function */
336 void HouseKeeping(struct libalias *);
338 /* Transparent proxy routines */
340 ProxyCheck(struct libalias *la, struct in_addr *proxy_server_addr,
341 u_short * proxy_server_port, struct in_addr src_addr,
342 struct in_addr dst_addr, u_short dst_port, u_char ip_p);
344 ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
345 int _maxpacketsize, int _proxy_type);
347 /* Tcp specific routines */
348 /* lint -save -library Suppress flexelint warnings */
350 enum alias_tcp_state {
351 ALIAS_TCP_STATE_NOT_CONNECTED,
352 ALIAS_TCP_STATE_CONNECTED,
353 ALIAS_TCP_STATE_DISCONNECTED
356 #if defined(_NETINET_IP_H_)
357 static __inline void *
358 ip_next(struct ip *iphdr)
360 char *p = (char *)iphdr;
361 return (&p[iphdr->ip_hl * 4]);
365 #if defined(_NETINET_TCP_H_)
366 static __inline void *
367 tcp_next(struct tcphdr *tcphdr)
369 char *p = (char *)tcphdr;
370 return (&p[tcphdr->th_off * 4]);
374 #if defined(_NETINET_UDP_H_)
375 static __inline void *
376 udp_next(struct udphdr *udphdr)
378 return ((void *)(udphdr + 1));
382 #endif /* !_ALIAS_LOCAL_H_ */