4 * Copyright (c) 2000 Whistle Communications, Inc.
7 * Subject to the following obligations and disclaimer of warranty, use and
8 * redistribution of this software, in source or object code forms, with or
9 * without modifications are expressly permitted by Whistle Communications;
10 * provided, however, that:
11 * 1. Any and all reproductions of the source or object code must include the
12 * copyright notice above and the following disclaimer of warranties; and
13 * 2. No rights are granted, in any manner or form, to use Whistle
14 * Communications, Inc. trademarks, including the mark "WHISTLE
15 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16 * such appears in the above copyright notice or in the software.
18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * Author: Erik Salander <erik@whistle.com>
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 Alias_pptp.c performs special processing for PPTP sessions under TCP.
44 Specifically, watch PPTP control messages and alias the Call ID or the
45 Peer's Call ID in the appropriate messages. Note, PPTP requires
46 "de-aliasing" of incoming packets, this is different than any other
47 TCP applications that are currently (ie. FTP, IRC and RTSP) aliased.
49 For Call IDs encountered for the first time, a PPTP alias link is created.
50 The PPTP alias link uses the Call ID in place of the original port number.
51 An alias Call ID is created.
53 For this routine to work, the PPTP control messages must fit entirely
54 into a single TCP packet. This is typically the case, but is not
57 Unlike some of the other TCP applications that are aliased (ie. FTP,
58 IRC and RTSP), the PPTP control messages that need to be aliased are
59 guaranteed to remain the same length. The aliased Call ID is a fixed
64 Initial version: May, 2000 (eds)
70 #include <sys/param.h>
72 #include <sys/types.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in.h>
78 #include <netinet/ip.h>
79 #include <netinet/tcp.h>
82 #include <netinet/libalias/alias.h>
83 #include <netinet/libalias/alias_local.h>
85 #include "alias_local.h"
92 struct grehdr { /* Enhanced GRE header. */
93 u_int16_t gh_flags; /* Flags. */
94 u_int16_t gh_protocol; /* Protocol type. */
95 u_int16_t gh_length; /* Payload length. */
96 u_int16_t gh_call_id; /* Call ID. */
97 u_int32_t gh_seq_no; /* Sequence number (optional). */
98 u_int32_t gh_ack_no; /* Acknowledgment number
101 typedef struct grehdr GreHdr;
103 /* The PPTP protocol ID used in the GRE 'proto' field. */
104 #define PPTP_GRE_PROTO 0x880b
106 /* Bits that must be set a certain way in all PPTP/GRE packets. */
107 #define PPTP_INIT_VALUE ((0x2001 << 16) | PPTP_GRE_PROTO)
108 #define PPTP_INIT_MASK 0xef7fffff
110 #define PPTP_MAGIC 0x1a2b3c4d
111 #define PPTP_CTRL_MSG_TYPE 1
114 PPTP_StartCtrlConnRequest = 1,
115 PPTP_StartCtrlConnReply = 2,
116 PPTP_StopCtrlConnRequest = 3,
117 PPTP_StopCtrlConnReply = 4,
118 PPTP_EchoRequest = 5,
120 PPTP_OutCallRequest = 7,
121 PPTP_OutCallReply = 8,
122 PPTP_InCallRequest = 9,
123 PPTP_InCallReply = 10,
124 PPTP_InCallConn = 11,
125 PPTP_CallClearRequest = 12,
126 PPTP_CallDiscNotify = 13,
127 PPTP_WanErrorNotify = 14,
128 PPTP_SetLinkInfo = 15
131 /* Message structures */
133 u_int16_t length; /* total length */
134 u_int16_t msgType;/* PPTP message type */
135 u_int32_t magic; /* magic cookie */
136 u_int16_t type; /* control message type */
137 u_int16_t resv0; /* reserved */
139 typedef struct pptpMsgHead *PptpMsgHead;
142 u_int8_t resCode;/* Result Code */
143 u_int8_t errCode;/* Error Code */
145 typedef struct pptpCodes *PptpCode;
148 u_int16_t cid1; /* Call ID field #1 */
149 u_int16_t cid2; /* Call ID field #2 */
151 typedef struct pptpCallIds *PptpCallId;
153 static PptpCallId AliasVerifyPptp(struct ip *, u_int16_t *);
157 AliasHandlePptpOut(struct libalias *la,
158 struct ip *pip, /* IP packet to examine/patch */
159 struct alias_link *lnk)
160 { /* The PPTP control link */
161 struct alias_link *pptp_lnk;
164 u_int16_t ctl_type; /* control message type */
167 /* Verify valid PPTP control message */
168 if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL)
171 /* Modify certain PPTP messages */
173 case PPTP_OutCallRequest:
174 case PPTP_OutCallReply:
175 case PPTP_InCallRequest:
176 case PPTP_InCallReply:
178 * Establish PPTP link for address and Call ID found in
181 pptp_lnk = AddPptp(la, GetOriginalAddress(lnk), GetDestAddress(lnk),
182 GetAliasAddress(lnk), cptr->cid1);
184 case PPTP_CallClearRequest:
185 case PPTP_CallDiscNotify:
187 * Find PPTP link for address and Call ID found in control
190 pptp_lnk = FindPptpOutByCallId(la, GetOriginalAddress(lnk),
198 if (pptp_lnk != NULL) {
199 int accumulate = cptr->cid1;
201 /* alias the Call Id */
202 cptr->cid1 = GetAliasPort(pptp_lnk);
204 /* Compute TCP checksum for revised packet */
205 tc = (struct tcphdr *)ip_next(pip);
206 accumulate -= cptr->cid1;
207 ADJUST_CHECKSUM(accumulate, tc->th_sum);
210 case PPTP_OutCallReply:
211 case PPTP_InCallReply:
212 codes = (PptpCode) (cptr + 1);
213 if (codes->resCode == 1) /* Connection
215 SetDestCallId(pptp_lnk, /* note the Peer's Call
219 SetExpire(pptp_lnk, 0); /* Connection refused. */
221 case PPTP_CallDiscNotify: /* Connection closed. */
222 SetExpire(pptp_lnk, 0);
229 AliasHandlePptpIn(struct libalias *la,
230 struct ip *pip, /* IP packet to examine/patch */
231 struct alias_link *lnk)
232 { /* The PPTP control link */
233 struct alias_link *pptp_lnk;
236 u_int16_t ctl_type; /* control message type */
239 /* Verify valid PPTP control message */
240 if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL)
243 /* Modify certain PPTP messages */
245 case PPTP_InCallConn:
246 case PPTP_WanErrorNotify:
247 case PPTP_SetLinkInfo:
248 pcall_id = &cptr->cid1;
250 case PPTP_OutCallReply:
251 case PPTP_InCallReply:
252 pcall_id = &cptr->cid2;
254 case PPTP_CallDiscNotify: /* Connection closed. */
255 pptp_lnk = FindPptpInByCallId(la, GetDestAddress(lnk),
256 GetAliasAddress(lnk),
258 if (pptp_lnk != NULL)
259 SetExpire(pptp_lnk, 0);
265 /* Find PPTP link for address and Call ID found in PPTP Control Msg */
266 pptp_lnk = FindPptpInByPeerCallId(la, GetDestAddress(lnk),
267 GetAliasAddress(lnk),
270 if (pptp_lnk != NULL) {
271 int accumulate = *pcall_id;
273 /* De-alias the Peer's Call Id. */
274 *pcall_id = GetOriginalPort(pptp_lnk);
276 /* Compute TCP checksum for modified packet */
277 tc = (struct tcphdr *)ip_next(pip);
278 accumulate -= *pcall_id;
279 ADJUST_CHECKSUM(accumulate, tc->th_sum);
281 if (ctl_type == PPTP_OutCallReply || ctl_type == PPTP_InCallReply) {
282 PptpCode codes = (PptpCode) (cptr + 1);
284 if (codes->resCode == 1) /* Connection
286 SetDestCallId(pptp_lnk, /* note the Call ID. */
289 SetExpire(pptp_lnk, 0); /* Connection refused. */
295 AliasVerifyPptp(struct ip *pip, u_int16_t * ptype)
296 { /* IP packet to examine/patch */
297 int hlen, tlen, dlen;
301 /* Calculate some lengths */
302 tc = (struct tcphdr *)ip_next(pip);
303 hlen = (pip->ip_hl + tc->th_off) << 2;
304 tlen = ntohs(pip->ip_len);
307 /* Verify data length */
308 if (dlen < (int)(sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds)))
311 /* Move up to PPTP message header */
312 hptr = (PptpMsgHead) tcp_next(tc);
314 /* Return the control message type */
315 *ptype = ntohs(hptr->type);
317 /* Verify PPTP Control Message */
318 if ((ntohs(hptr->msgType) != PPTP_CTRL_MSG_TYPE) ||
319 (ntohl(hptr->magic) != PPTP_MAGIC))
322 /* Verify data length. */
323 if ((*ptype == PPTP_OutCallReply || *ptype == PPTP_InCallReply) &&
324 (dlen < (int)(sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds) +
325 sizeof(struct pptpCodes))))
328 return (PptpCallId) (hptr + 1);
333 AliasHandlePptpGreOut(struct libalias *la, struct ip *pip)
336 struct alias_link *lnk;
338 gr = (GreHdr *) ip_next(pip);
340 /* Check GRE header bits. */
341 if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
344 lnk = FindPptpOutByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id);
346 struct in_addr alias_addr = GetAliasAddress(lnk);
348 /* Change source IP address. */
349 DifferentialChecksum(&pip->ip_sum,
350 &alias_addr, &pip->ip_src, 2);
351 pip->ip_src = alias_addr;
358 AliasHandlePptpGreIn(struct libalias *la, struct ip *pip)
361 struct alias_link *lnk;
363 gr = (GreHdr *) ip_next(pip);
365 /* Check GRE header bits. */
366 if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
369 lnk = FindPptpInByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id);
371 struct in_addr src_addr = GetOriginalAddress(lnk);
373 /* De-alias the Peer's Call Id. */
374 gr->gh_call_id = GetOriginalPort(lnk);
376 /* Restore original IP address. */
377 DifferentialChecksum(&pip->ip_sum,
378 &src_addr, &pip->ip_dst, 2);
379 pip->ip_dst = src_addr;