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