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_
51 #include <sys/types.h>
52 #include <sys/sysctl.h>
55 #include <sys/malloc.h>
56 #include <sys/param.h>
58 #include <sys/mutex.h>
60 /* XXX: LibAliasSetTarget() uses this constant. */
61 #define INADDR_NONE 0xffffffff
63 #include <netinet/libalias/alias_sctp.h>
65 #include "alias_sctp.h"
68 /* Sizes of input and output link tables */
69 #define LINK_TABLE_OUT_SIZE 4001
70 #define LINK_TABLE_IN_SIZE 4001
72 #define GET_ALIAS_PORT -1
73 #define GET_ALIAS_ID GET_ALIAS_PORT
76 #define INET_NTOA_BUF(buf) (buf)
78 #define INET_NTOA_BUF(buf) (buf), sizeof(buf)
84 LIST_ENTRY(libalias) instancelist;
86 int packetAliasMode; /* Mode flags */
87 /* - documented in alias.h */
89 struct in_addr aliasAddress; /* Address written onto source */
90 /* field of IP packet. */
92 struct in_addr targetAddress; /* IP address incoming packets */
93 /* are sent to if no aliasing */
94 /* link already exists */
96 struct in_addr nullAddress; /* Used as a dummy parameter for */
97 /* some function calls */
99 LIST_HEAD (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE];
100 /* Lookup table of pointers to */
101 /* chains of link records. Each */
103 LIST_HEAD (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE];
104 /* link record is doubly indexed */
105 /* into input and output lookup */
108 /* Link statistics */
114 int fragmentIdLinkCount;
115 int fragmentPtrLinkCount;
118 int cleanupIndex; /* Index to chain of link table */
119 /* being inspected for old links */
121 int timeStamp; /* System time in seconds for */
124 int lastCleanupTime; /* Last time
125 * IncrementalCleanup() */
128 int deleteAllLinks; /* If equal to zero, DeleteLink() */
129 /* will not remove permanent links */
137 /* statistics monitoring */
139 int newDefaultLink; /* Indicates if a new aliasing */
140 /* link has been created after a */
141 /* call to PacketAliasIn/Out(). */
144 int fireWallFD; /* File descriptor to be able to */
145 /* control firewall. Opened by */
146 /* PacketAliasSetMode on first */
147 /* setting the PKT_ALIAS_PUNCH_FW */
149 int fireWallBaseNum; /* The first firewall entry
150 * free for our use */
151 int fireWallNumNums; /* How many entries can we
153 int fireWallActiveNum; /* Which entry did we last
155 char *fireWallField; /* bool array for entries */
158 unsigned int skinnyPort; /* TCP port used by the Skinny */
161 struct proxy_entry *proxyList;
163 struct in_addr true_addr; /* in network byte order. */
164 u_short true_port; /* in host byte order. */
166 /* Port ranges for aliasing. */
167 u_short aliasPortLower;
168 u_short aliasPortLength;
174 /* counts associations that have progressed to UP and not yet removed */
177 /* timing queue for keeping track of association timeouts */
178 struct sctp_nat_timer sctpNatTimer;
180 /* size of hash table used in this instance */
181 u_int sctpNatTableSize;
184 * local look up table sorted by l_vtag/l_port
186 LIST_HEAD(sctpNatTableL, sctp_nat_assoc) *sctpTableLocal;
188 * global look up table sorted by g_vtag/g_port
190 LIST_HEAD(sctpNatTableG, sctp_nat_assoc) *sctpTableGlobal;
193 * avoid races in libalias: every public function has to use it.
202 #define LIBALIAS_LOCK_INIT(l) \
203 mtx_init(&l->mutex, "per-instance libalias mutex", NULL, MTX_DEF)
204 #define LIBALIAS_LOCK_ASSERT(l) mtx_assert(&l->mutex, MA_OWNED)
205 #define LIBALIAS_LOCK(l) mtx_lock(&l->mutex)
206 #define LIBALIAS_UNLOCK(l) mtx_unlock(&l->mutex)
207 #define LIBALIAS_LOCK_DESTROY(l) mtx_destroy(&l->mutex)
209 #define LIBALIAS_LOCK_INIT(l)
210 #define LIBALIAS_LOCK_ASSERT(l)
211 #define LIBALIAS_LOCK(l)
212 #define LIBALIAS_UNLOCK(l)
213 #define LIBALIAS_LOCK_DESTROY(l)
217 * The following macro is used to update an
218 * internet checksum. "delta" is a 32-bit
219 * accumulation of all the changes to the
220 * checksum (adding in new 16-bit words and
221 * subtracting out old words), and "cksum"
222 * is the checksum value to be updated.
224 #define ADJUST_CHECKSUM(acc, cksum) \
229 acc = (acc >> 16) + (acc & 0xffff); \
231 cksum = (u_short) ~acc; \
233 acc = (acc >> 16) + (acc & 0xffff); \
235 cksum = (u_short) acc; \
243 * SctpFunction prototypes
246 void AliasSctpInit(struct libalias *la);
247 void AliasSctpTerm(struct libalias *la);
248 int SctpAlias(struct libalias *la, struct ip *ip, int direction);
251 * We do not calculate TCP checksums when libalias is a kernel
252 * module, since it has no idea about checksum offloading.
253 * If TCP data has changed, then we just set checksum to zero,
254 * and caller must recalculate it himself.
255 * In case if libalias will edit UDP data, the same approach
259 u_short IpChecksum(struct ip *_pip);
260 u_short TcpChecksum(struct ip *_pip);
263 DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n);
265 /* Internal data access */
267 AddLink(struct libalias *la, struct in_addr src_addr, struct in_addr dst_addr,
268 struct in_addr alias_addr, u_short src_port, u_short dst_port,
269 int alias_param, int link_type);
271 FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
272 u_short _id_alias, int _create);
274 FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
275 u_short _id, int _create);
277 FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
280 FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
283 AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
285 FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id);
287 FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
290 FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
293 FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
294 u_short _dst_port, u_short _alias_port, u_char _proto, int _create);
296 FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
297 u_short _src_port, u_short _dst_port, u_char _proto, int _create);
299 AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
300 struct in_addr _alias_addr, u_int16_t _src_call_id);
302 FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr,
303 struct in_addr _dst_addr, u_int16_t _src_call_id);
305 FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr,
306 struct in_addr _alias_addr, u_int16_t _dst_call_id);
308 FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr,
309 struct in_addr _dst_addr, u_int16_t _dst_call_id);
311 FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr,
312 struct in_addr _alias_addr, u_int16_t _alias_call_id);
314 FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr,
315 u_short _src_port, u_short _alias_port, u_char _proto);
317 FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr);
319 FindAliasAddress(struct libalias *la, struct in_addr _original_addr);
321 FindSctpRedirectAddress(struct libalias *la, struct sctp_nat_msg *sm);
323 /* External data access/modification */
325 FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr,
326 u_short _src_port, u_short _dst_port, u_short _port_count,
327 u_char _proto, u_char _align);
328 void GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr);
329 void SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr);
330 void GetFragmentPtr(struct alias_link *_lnk, char **_fptr);
331 void SetFragmentPtr(struct alias_link *_lnk, char *fptr);
332 void SetStateIn(struct alias_link *_lnk, int _state);
333 void SetStateOut(struct alias_link *_lnk, int _state);
334 int GetStateIn (struct alias_link *_lnk);
335 int GetStateOut(struct alias_link *_lnk);
337 GetOriginalAddress(struct alias_link *_lnk);
339 GetDestAddress(struct alias_link *_lnk);
341 GetAliasAddress(struct alias_link *_lnk);
343 GetDefaultAliasAddress(struct libalias *la);
344 void SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr);
345 u_short GetOriginalPort(struct alias_link *_lnk);
346 u_short GetAliasPort(struct alias_link *_lnk);
348 GetProxyAddress(struct alias_link *_lnk);
349 void SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr);
350 u_short GetProxyPort(struct alias_link *_lnk);
351 void SetProxyPort(struct alias_link *_lnk, u_short _port);
352 void SetAckModified(struct alias_link *_lnk);
353 int GetAckModified(struct alias_link *_lnk);
354 int GetDeltaAckIn(u_long, struct alias_link *_lnk);
355 int GetDeltaSeqOut(u_long, struct alias_link *lnk);
356 void AddSeq(struct alias_link *lnk, int delta, u_int ip_hl,
357 u_short ip_len, u_long th_seq, u_int th_off);
358 void SetExpire (struct alias_link *_lnk, int _expire);
359 void ClearCheckNewLink(struct libalias *la);
360 void SetProtocolFlags(struct alias_link *_lnk, int _pflags);
361 int GetProtocolFlags(struct alias_link *_lnk);
362 void SetDestCallId(struct alias_link *_lnk, u_int16_t _cid);
365 void PunchFWHole(struct alias_link *_lnk);
369 /* Housekeeping function */
370 void HouseKeeping(struct libalias *);
372 /* Tcp specific routines */
373 /* lint -save -library Suppress flexelint warnings */
375 /* Transparent proxy routines */
377 ProxyCheck(struct libalias *la, struct in_addr *proxy_server_addr,
378 u_short * proxy_server_port, struct in_addr src_addr,
379 struct in_addr dst_addr, u_short dst_port, u_char ip_p);
381 ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip,
382 int _maxpacketsize, int _proxy_type);
384 enum alias_tcp_state {
385 ALIAS_TCP_STATE_NOT_CONNECTED,
386 ALIAS_TCP_STATE_CONNECTED,
387 ALIAS_TCP_STATE_DISCONNECTED
390 #if defined(_NETINET_IP_H_)
391 static __inline void *
392 ip_next(struct ip *iphdr)
394 char *p = (char *)iphdr;
395 return (&p[iphdr->ip_hl * 4]);
399 #if defined(_NETINET_TCP_H_)
400 static __inline void *
401 tcp_next(struct tcphdr *tcphdr)
403 char *p = (char *)tcphdr;
404 return (&p[tcphdr->th_off * 4]);
408 #if defined(_NETINET_UDP_H_)
409 static __inline void *
410 udp_next(struct udphdr *udphdr)
412 return ((void *)(udphdr + 1));
416 #endif /* !_ALIAS_LOCAL_H_ */