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