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