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