2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 2000 Whistle Communications, Inc.
27 * All rights reserved.
29 * Subject to the following obligations and disclaimer of warranty, use and
30 * redistribution of this software, in source or object code forms, with or
31 * without modifications are expressly permitted by Whistle Communications;
32 * provided, however, that:
33 * 1. Any and all reproductions of the source or object code must include the
34 * copyright notice above and the following disclaimer of warranties; and
35 * 2. No rights are granted, in any manner or form, to use Whistle
36 * Communications, Inc. trademarks, including the mark "WHISTLE
37 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
38 * such appears in the above copyright notice or in the software.
40 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
41 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
42 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
43 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
44 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
45 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
46 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
47 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
48 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
49 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
50 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
51 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
58 * Author: Erik Salander <erik@whistle.com>
61 * $FreeBSD: src/lib/libalias/alias_pptp.c,v 1.1.2.4 2001/08/01 09:52:27 obrien Exp $
65 Alias_pptp.c performs special processing for PPTP sessions under TCP.
66 Specifically, watch PPTP control messages and alias the Call ID or the
67 Peer's Call ID in the appropriate messages. Note, PPTP requires
68 "de-aliasing" of incoming packets, this is different than any other
69 TCP applications that are currently (ie. FTP, IRC and RTSP) aliased.
71 For Call IDs encountered for the first time, a PPTP alias link is created.
72 The PPTP alias link uses the Call ID in place of the original port number.
73 An alias Call ID is created.
75 For this routine to work, the PPTP control messages must fit entirely
76 into a single TCP packet. This is typically the case, but is not
79 Unlike some of the other TCP applications that are aliased (ie. FTP,
80 IRC and RTSP), the PPTP control messages that need to be aliased are
81 guaranteed to remain the same length. The aliased Call ID is a fixed
86 Initial version: May, 2000 (eds)
91 #include <sys/types.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in.h>
94 #include <netinet/ip.h>
95 #include <netinet/tcp.h>
99 #include "alias_local.h"
105 struct grehdr
/* Enhanced GRE header. */
107 u_int16_t gh_flags
; /* Flags. */
108 u_int16_t gh_protocol
; /* Protocol type. */
109 u_int16_t gh_length
; /* Payload length. */
110 u_int16_t gh_call_id
; /* Call ID. */
111 u_int32_t gh_seq_no
; /* Sequence number (optional). */
112 u_int32_t gh_ack_no
; /* Acknowledgment number (optional). */
114 typedef struct grehdr GreHdr
;
116 /* The PPTP protocol ID used in the GRE 'proto' field. */
117 #define PPTP_GRE_PROTO 0x880b
119 /* Bits that must be set a certain way in all PPTP/GRE packets. */
120 #define PPTP_INIT_VALUE ((0x2001 << 16) | PPTP_GRE_PROTO)
121 #define PPTP_INIT_MASK 0xef7fffff
123 #define PPTP_MAGIC 0x1a2b3c4d
124 #define PPTP_CTRL_MSG_TYPE 1
127 PPTP_StartCtrlConnRequest
= 1,
128 PPTP_StartCtrlConnReply
= 2,
129 PPTP_StopCtrlConnRequest
= 3,
130 PPTP_StopCtrlConnReply
= 4,
131 PPTP_EchoRequest
= 5,
133 PPTP_OutCallRequest
= 7,
134 PPTP_OutCallReply
= 8,
135 PPTP_InCallRequest
= 9,
136 PPTP_InCallReply
= 10,
137 PPTP_InCallConn
= 11,
138 PPTP_CallClearRequest
= 12,
139 PPTP_CallDiscNotify
= 13,
140 PPTP_WanErrorNotify
= 14,
141 PPTP_SetLinkInfo
= 15
144 /* Message structures */
146 u_int16_t length
; /* total length */
147 u_int16_t msgType
; /* PPTP message type */
148 u_int32_t magic
; /* magic cookie */
149 u_int16_t type
; /* control message type */
150 u_int16_t resv0
; /* reserved */
152 typedef struct pptpMsgHead
*PptpMsgHead
;
155 u_int8_t resCode
; /* Result Code */
156 u_int8_t errCode
; /* Error Code */
158 typedef struct pptpCodes
*PptpCode
;
161 u_int16_t cid1
; /* Call ID field #1 */
162 u_int16_t cid2
; /* Call ID field #2 */
164 typedef struct pptpCallIds
*PptpCallId
;
166 static PptpCallId
AliasVerifyPptp(struct ip
*, u_int16_t
*);
170 AliasHandlePptpOut(struct ip
*pip
, /* IP packet to examine/patch */
171 struct alias_link
*link
) /* The PPTP control link */
173 struct alias_link
*pptp_link
;
176 u_int16_t ctl_type
; /* control message type */
179 /* Verify valid PPTP control message */
180 if ((cptr
= AliasVerifyPptp(pip
, &ctl_type
)) == NULL
)
183 /* Modify certain PPTP messages */
185 case PPTP_OutCallRequest
:
186 case PPTP_OutCallReply
:
187 case PPTP_InCallRequest
:
188 case PPTP_InCallReply
:
189 /* Establish PPTP link for address and Call ID found in control message. */
190 pptp_link
= AddPptp(GetOriginalAddress(link
), GetDestAddress(link
),
191 GetAliasAddress(link
), cptr
->cid1
);
193 case PPTP_CallClearRequest
:
194 case PPTP_CallDiscNotify
:
195 /* Find PPTP link for address and Call ID found in control message. */
196 pptp_link
= FindPptpOutByCallId(GetOriginalAddress(link
),
197 GetDestAddress(link
),
204 if (pptp_link
!= NULL
) {
205 int accumulate
= cptr
->cid1
;
207 /* alias the Call Id */
208 cptr
->cid1
= GetAliasPort(pptp_link
);
210 /* Compute TCP checksum for revised packet */
211 tc
= (struct tcphdr
*) ((char *) pip
+ (pip
->ip_hl
<< 2));
212 accumulate
-= cptr
->cid1
;
213 ADJUST_CHECKSUM(accumulate
, tc
->th_sum
);
216 case PPTP_OutCallReply
:
217 case PPTP_InCallReply
:
218 codes
= (PptpCode
)(cptr
+ 1);
219 if (codes
->resCode
== 1) /* Connection established, */
220 SetDestCallId(pptp_link
, /* note the Peer's Call ID. */
223 SetExpire(pptp_link
, 0); /* Connection refused. */
225 case PPTP_CallDiscNotify
: /* Connection closed. */
226 SetExpire(pptp_link
, 0);
233 AliasHandlePptpIn(struct ip
*pip
, /* IP packet to examine/patch */
234 struct alias_link
*link
) /* The PPTP control link */
236 struct alias_link
*pptp_link
;
239 u_int16_t ctl_type
; /* control message type */
242 /* Verify valid PPTP control message */
243 if ((cptr
= AliasVerifyPptp(pip
, &ctl_type
)) == NULL
)
246 /* Modify certain PPTP messages */
249 case PPTP_InCallConn
:
250 case PPTP_WanErrorNotify
:
251 case PPTP_SetLinkInfo
:
252 pcall_id
= &cptr
->cid1
;
254 case PPTP_OutCallReply
:
255 case PPTP_InCallReply
:
256 pcall_id
= &cptr
->cid2
;
258 case PPTP_CallDiscNotify
: /* Connection closed. */
259 pptp_link
= FindPptpInByCallId(GetDestAddress(link
),
260 GetAliasAddress(link
),
262 if (pptp_link
!= NULL
)
263 SetExpire(pptp_link
, 0);
269 /* Find PPTP link for address and Call ID found in PPTP Control Msg */
270 pptp_link
= FindPptpInByPeerCallId(GetDestAddress(link
),
271 GetAliasAddress(link
),
274 if (pptp_link
!= NULL
) {
275 int accumulate
= *pcall_id
;
277 /* De-alias the Peer's Call Id. */
278 *pcall_id
= GetOriginalPort(pptp_link
);
280 /* Compute TCP checksum for modified packet */
281 tc
= (struct tcphdr
*) ((char *) pip
+ (pip
->ip_hl
<< 2));
282 accumulate
-= *pcall_id
;
283 ADJUST_CHECKSUM(accumulate
, tc
->th_sum
);
285 if (ctl_type
== PPTP_OutCallReply
|| ctl_type
== PPTP_InCallReply
) {
286 PptpCode codes
= (PptpCode
)(cptr
+ 1);
288 if (codes
->resCode
== 1) /* Connection established, */
289 SetDestCallId(pptp_link
, /* note the Call ID. */
292 SetExpire(pptp_link
, 0); /* Connection refused. */
298 AliasVerifyPptp(struct ip
*pip
, u_int16_t
*ptype
) /* IP packet to examine/patch */
300 int hlen
, tlen
, dlen
;
304 /* Calculate some lengths */
305 tc
= (struct tcphdr
*) ((char *) pip
+ (pip
->ip_hl
<< 2));
306 hlen
= (pip
->ip_hl
+ tc
->th_off
) << 2;
307 tlen
= ntohs(pip
->ip_len
);
310 /* Verify data length */
311 if (dlen
< (sizeof(struct pptpMsgHead
) + sizeof(struct pptpCallIds
)))
314 /* Move up to PPTP message header */
315 hptr
= (PptpMsgHead
)(((char *) pip
) + hlen
);
317 /* Return the control message type */
318 *ptype
= ntohs(hptr
->type
);
320 /* Verify PPTP Control Message */
321 if ((ntohs(hptr
->msgType
) != PPTP_CTRL_MSG_TYPE
) ||
322 (ntohl(hptr
->magic
) != PPTP_MAGIC
))
325 /* Verify data length. */
326 if ((*ptype
== PPTP_OutCallReply
|| *ptype
== PPTP_InCallReply
) &&
327 (dlen
< sizeof(struct pptpMsgHead
) + sizeof(struct pptpCallIds
) +
328 sizeof(struct pptpCodes
)))
331 return (PptpCallId
)(hptr
+ 1);
336 AliasHandlePptpGreOut(struct ip
*pip
)
339 struct alias_link
*link
;
341 gr
= (GreHdr
*)((char *)pip
+ (pip
->ip_hl
<< 2));
343 /* Check GRE header bits. */
344 if ((ntohl(*((u_int32_t
*)gr
)) & PPTP_INIT_MASK
) != PPTP_INIT_VALUE
)
347 link
= FindPptpOutByPeerCallId(pip
->ip_src
, pip
->ip_dst
, gr
->gh_call_id
);
349 struct in_addr alias_addr
= GetAliasAddress(link
);
351 /* Change source IP address. */
352 DifferentialChecksum(&pip
->ip_sum
,
353 (u_short
*)&alias_addr
,
354 (u_short
*)&pip
->ip_src
,
356 pip
->ip_src
= alias_addr
;
364 AliasHandlePptpGreIn(struct ip
*pip
)
367 struct alias_link
*link
;
369 gr
= (GreHdr
*)((char *)pip
+ (pip
->ip_hl
<< 2));
371 /* Check GRE header bits. */
372 if ((ntohl(*((u_int32_t
*)gr
)) & PPTP_INIT_MASK
) != PPTP_INIT_VALUE
)
375 link
= FindPptpInByPeerCallId(pip
->ip_src
, pip
->ip_dst
, gr
->gh_call_id
);
377 struct in_addr src_addr
= GetOriginalAddress(link
);
379 /* De-alias the Peer's Call Id. */
380 gr
->gh_call_id
= GetOriginalPort(link
);
382 /* Restore original IP address. */
383 DifferentialChecksum(&pip
->ip_sum
,
384 (u_short
*)&src_addr
,
385 (u_short
*)&pip
->ip_dst
,
387 pip
->ip_dst
= src_addr
;