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/queue.h> 50 51 /* Sizes of input and output link tables */ 52 #define LINK_TABLE_OUT_SIZE 101 53 #define LINK_TABLE_IN_SIZE 4001 54 55 struct proxy_entry; 56 57 struct libalias { 58 LIST_ENTRY(libalias) instancelist; 59 60 int packetAliasMode; /* Mode flags */ 61 /* - documented in alias.h */ 62 63 struct in_addr aliasAddress; /* Address written onto source */ 64 /* field of IP packet. */ 65 66 struct in_addr targetAddress; /* IP address incoming packets */ 67 /* are sent to if no aliasing */ 68 /* link already exists */ 69 70 struct in_addr nullAddress; /* Used as a dummy parameter for */ 71 /* some function calls */ 72 73 LIST_HEAD (, alias_link) linkTableOut[LINK_TABLE_OUT_SIZE]; 74 /* Lookup table of pointers to */ 75 /* chains of link records. Each */ 76 77 LIST_HEAD (, alias_link) linkTableIn[LINK_TABLE_IN_SIZE]; 78 /* link record is doubly indexed */ 79 /* into input and output lookup */ 80 /* tables. */ 81 82 /* Link statistics */ 83 int icmpLinkCount; 84 int udpLinkCount; 85 int tcpLinkCount; 86 int pptpLinkCount; 87 int protoLinkCount; 88 int fragmentIdLinkCount; 89 int fragmentPtrLinkCount; 90 int sockCount; 91 92 int cleanupIndex; /* Index to chain of link table */ 93 /* being inspected for old links */ 94 95 int timeStamp; /* System time in seconds for */ 96 /* current packet */ 97 98 int lastCleanupTime; /* Last time 99 * IncrementalCleanup() */ 100 /* was called */ 101 102 int houseKeepingResidual; /* used by HouseKeeping() */ 103 104 int deleteAllLinks; /* If equal to zero, DeleteLink() */ 105 /* will not remove permanent links */ 106 107 FILE *monitorFile; /* File descriptor for link */ 108 /* statistics monitoring file */ 109 110 int newDefaultLink; /* Indicates if a new aliasing */ 111 /* link has been created after a */ 112 /* call to PacketAliasIn/Out(). */ 113 114 #ifndef NO_FW_PUNCH 115 int fireWallFD; /* File descriptor to be able to */ 116 /* control firewall. Opened by */ 117 /* PacketAliasSetMode on first */ 118 /* setting the PKT_ALIAS_PUNCH_FW */ 119 /* flag. */ 120 int fireWallBaseNum; /* The first firewall entry 121 * free for our use */ 122 int fireWallNumNums; /* How many entries can we 123 * use? */ 124 int fireWallActiveNum; /* Which entry did we last 125 * use? */ 126 char *fireWallField; /* bool array for entries */ 127 #endif 128 129 unsigned int skinnyPort; /* TCP port used by the Skinny */ 130 /* protocol. */ 131 132 struct proxy_entry *proxyList; 133 134 struct in_addr true_addr; /* in network byte order. */ 135 u_short true_port; /* in host byte order. */ 136 137 }; 138 139 /* Macros */ 140 141 /* 142 * The following macro is used to update an 143 * internet checksum. "delta" is a 32-bit 144 * accumulation of all the changes to the 145 * checksum (adding in new 16-bit words and 146 * subtracting out old words), and "cksum" 147 * is the checksum value to be updated. 148 */ 149 #define ADJUST_CHECKSUM(acc, cksum) \ 150 do { \ 151 acc += cksum; \ 152 if (acc < 0) { \ 153 acc = -acc; \ 154 acc = (acc >> 16) + (acc & 0xffff); \ 155 acc += acc >> 16; \ 156 cksum = (u_short) ~acc; \ 157 } else { \ 158 acc = (acc >> 16) + (acc & 0xffff); \ 159 acc += acc >> 16; \ 160 cksum = (u_short) acc; \ 161 } \ 162 } while (0) 163 164 165 /* Prototypes */ 166 167 /* General utilities */ 168 u_short IpChecksum(struct ip *_pip); 169 u_short TcpChecksum(struct ip *_pip); 170 void 171 DifferentialChecksum(u_short * _cksum, void * _new, void * _old, int _n); 172 173 /* Internal data access */ 174 struct alias_link * 175 FindIcmpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr, 176 u_short _id_alias, int _create); 177 struct alias_link * 178 FindIcmpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr, 179 u_short _id, int _create); 180 struct alias_link * 181 FindFragmentIn1(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr, 182 u_short _ip_id); 183 struct alias_link * 184 FindFragmentIn2(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr, 185 u_short _ip_id); 186 struct alias_link * 187 AddFragmentPtrLink(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id); 188 struct alias_link * 189 FindFragmentPtr(struct libalias *la, struct in_addr _dst_addr, u_short _ip_id); 190 struct alias_link * 191 FindProtoIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr, 192 u_char _proto); 193 struct alias_link * 194 FindProtoOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr, 195 u_char _proto); 196 struct alias_link * 197 FindUdpTcpIn(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr, 198 u_short _dst_port, u_short _alias_port, u_char _proto, int _create); 199 struct alias_link * 200 FindUdpTcpOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr, 201 u_short _src_port, u_short _dst_port, u_char _proto, int _create); 202 struct alias_link * 203 AddPptp(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr, 204 struct in_addr _alias_addr, u_int16_t _src_call_id); 205 struct alias_link * 206 FindPptpOutByCallId(struct libalias *la, struct in_addr _src_addr, 207 struct in_addr _dst_addr, u_int16_t _src_call_id); 208 struct alias_link * 209 FindPptpInByCallId(struct libalias *la, struct in_addr _dst_addr, 210 struct in_addr _alias_addr, u_int16_t _dst_call_id); 211 struct alias_link * 212 FindPptpOutByPeerCallId(struct libalias *la, struct in_addr _src_addr, 213 struct in_addr _dst_addr, u_int16_t _dst_call_id); 214 struct alias_link * 215 FindPptpInByPeerCallId(struct libalias *la, struct in_addr _dst_addr, 216 struct in_addr _alias_addr, u_int16_t _alias_call_id); 217 struct alias_link * 218 FindRtspOut(struct libalias *la, struct in_addr _src_addr, struct in_addr _dst_addr, 219 u_short _src_port, u_short _alias_port, u_char _proto); 220 struct in_addr 221 FindOriginalAddress(struct libalias *la, struct in_addr _alias_addr); 222 struct in_addr 223 FindAliasAddress(struct libalias *la, struct in_addr _original_addr); 224 225 /* External data access/modification */ 226 int 227 FindNewPortGroup(struct libalias *la, struct in_addr _dst_addr, struct in_addr _alias_addr, 228 u_short _src_port, u_short _dst_port, u_short _port_count, 229 u_char _proto, u_char _align); 230 void GetFragmentAddr(struct alias_link *_lnk, struct in_addr *_src_addr); 231 void SetFragmentAddr(struct alias_link *_lnk, struct in_addr _src_addr); 232 void GetFragmentPtr(struct alias_link *_lnk, char **_fptr); 233 void SetFragmentPtr(struct alias_link *_lnk, char *fptr); 234 void SetStateIn(struct alias_link *_lnk, int _state); 235 void SetStateOut(struct alias_link *_lnk, int _state); 236 int GetStateIn (struct alias_link *_lnk); 237 int GetStateOut(struct alias_link *_lnk); 238 struct in_addr 239 GetOriginalAddress(struct alias_link *_lnk); 240 struct in_addr 241 GetDestAddress(struct alias_link *_lnk); 242 struct in_addr 243 GetAliasAddress(struct alias_link *_lnk); 244 struct in_addr 245 GetDefaultAliasAddress(struct libalias *la); 246 void SetDefaultAliasAddress(struct libalias *la, struct in_addr _alias_addr); 247 u_short GetOriginalPort(struct alias_link *_lnk); 248 u_short GetAliasPort(struct alias_link *_lnk); 249 struct in_addr 250 GetProxyAddress(struct alias_link *_lnk); 251 void SetProxyAddress(struct alias_link *_lnk, struct in_addr _addr); 252 u_short GetProxyPort(struct alias_link *_lnk); 253 void SetProxyPort(struct alias_link *_lnk, u_short _port); 254 void SetAckModified(struct alias_link *_lnk); 255 int GetAckModified(struct alias_link *_lnk); 256 int GetDeltaAckIn(struct ip *_pip, struct alias_link *_lnk); 257 int GetDeltaSeqOut(struct ip *_pip, struct alias_link *_lnk); 258 void AddSeq (struct ip *_pip, struct alias_link *_lnk, int _delta); 259 void SetExpire (struct alias_link *_lnk, int _expire); 260 void ClearCheckNewLink(struct libalias *la); 261 void SetProtocolFlags(struct alias_link *_lnk, int _pflags); 262 int GetProtocolFlags(struct alias_link *_lnk); 263 void SetDestCallId(struct alias_link *_lnk, u_int16_t _cid); 264 265 #ifndef NO_FW_PUNCH 266 void PunchFWHole(struct alias_link *_lnk); 267 268 #endif 269 270 /* Housekeeping function */ 271 void HouseKeeping(struct libalias *); 272 273 /* Tcp specfic routines */ 274 /* lint -save -library Suppress flexelint warnings */ 275 276 /* FTP routines */ 277 void 278 AliasHandleFtpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk, 279 int _maxpacketsize); 280 281 /* IRC routines */ 282 void 283 AliasHandleIrcOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk, 284 int _maxsize); 285 286 /* RTSP routines */ 287 void 288 AliasHandleRtspOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk, 289 int _maxpacketsize); 290 291 /* PPTP routines */ 292 void AliasHandlePptpOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk); 293 void AliasHandlePptpIn(struct libalias *la, struct ip *_pip, struct alias_link *_lnk); 294 int AliasHandlePptpGreOut(struct libalias *la, struct ip *_pip); 295 int AliasHandlePptpGreIn(struct libalias *la, struct ip *_pip); 296 297 /* NetBIOS routines */ 298 int 299 AliasHandleUdpNbt(struct libalias *la, struct ip *_pip, struct alias_link *_lnk, 300 struct in_addr *_alias_address, u_short _alias_port); 301 int 302 AliasHandleUdpNbtNS(struct libalias *la, struct ip *_pip, struct alias_link *_lnk, 303 struct in_addr *_alias_address, u_short * _alias_port, 304 struct in_addr *_original_address, u_short * _original_port); 305 306 /* CUSeeMe routines */ 307 void AliasHandleCUSeeMeOut(struct libalias *la, struct ip *_pip, struct alias_link *_lnk); 308 void AliasHandleCUSeeMeIn(struct libalias *la, struct ip *_pip, struct in_addr _original_addr); 309 310 /* Skinny routines */ 311 void AliasHandleSkinny(struct libalias *la, struct ip *_pip, struct alias_link *_lnk); 312 313 /* Transparent proxy routines */ 314 int 315 ProxyCheck(struct libalias *la, struct ip *_pip, struct in_addr *_proxy_server_addr, 316 u_short * _proxy_server_port); 317 void 318 ProxyModify(struct libalias *la, struct alias_link *_lnk, struct ip *_pip, 319 int _maxpacketsize, int _proxy_type); 320 321 enum alias_tcp_state { 322 ALIAS_TCP_STATE_NOT_CONNECTED, 323 ALIAS_TCP_STATE_CONNECTED, 324 ALIAS_TCP_STATE_DISCONNECTED 325 }; 326 327 #if defined(_NETINET_IP_H_) 328 static __inline void * 329 ip_next(struct ip *iphdr) 330 { 331 char *p = (char *)iphdr; 332 return (&p[iphdr->ip_hl * 4]); 333 } 334 #endif 335 336 #if defined(_NETINET_TCP_H_) 337 static __inline void * 338 tcp_next(struct tcphdr *tcphdr) 339 { 340 char *p = (char *)tcphdr; 341 return (&p[tcphdr->th_off * 4]); 342 } 343 #endif 344 345 #if defined(_NETINET_UDP_H_) 346 static __inline void * 347 udp_next(struct udphdr *udphdr) 348 { 349 return ((void *)(udphdr + 1)); 350 } 351 #endif 352 353 /*lint -restore */ 354 355 #endif /* !_ALIAS_LOCAL_H_ */ 356