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