alias_pptp.c revision 64452
1/*
2 * alias_pptp.c
3 *
4 * Copyright (c) 2000 Whistle Communications, Inc.
5 * All rights reserved.
6 *
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.
17 *
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
34 * OF SUCH DAMAGE.
35 *
36 * Author: Erik Salander <erik@whistle.com>
37 *
38 * $FreeBSD: head/sys/netinet/libalias/alias_pptp.c 64452 2000-08-09 11:25:44Z ru $
39 */
40
41/*
42   Alias_pptp.c performs special processing for PPTP sessions under TCP.
43   Specifically, watch PPTP control messages and alias the Call ID or the
44   Peer's Call ID in the appropriate messages.  Note, PPTP requires
45   "de-aliasing" of incoming packets, this is different than any other
46   TCP applications that are currently (ie. FTP, IRC and RTSP) aliased.
47
48   For Call IDs encountered for the first time, a PPTP alias link is created.
49   The PPTP alias link uses the Call ID in place of the original port number.
50   An alias Call ID is created.
51
52   For this routine to work, the PPTP control messages must fit entirely
53   into a single TCP packet.  This is typically the case, but is not
54   required by the spec.
55
56   Unlike some of the other TCP applications that are aliased (ie. FTP,
57   IRC and RTSP), the PPTP control messages that need to be aliased are
58   guaranteed to remain the same length.  The aliased Call ID is a fixed
59   length field.
60
61   Reference: RFC 2637
62
63   Initial version:  May, 2000 (eds)
64
65*/
66
67/* Includes */
68#include <ctype.h>
69#include <stdio.h>
70#include <string.h>
71#include <sys/types.h>
72#include <netinet/in_systm.h>
73#include <netinet/in.h>
74#include <netinet/ip.h>
75#include <netinet/tcp.h>
76
77#include "alias_local.h"
78
79/*
80 * PPTP definitions
81 */
82
83struct grehdr			/* Enhanced GRE header. */
84{
85    u_char    gh_recursion:3,	/* Recursion control. */
86              gh_ssr_flag:1,	/* Strict source route present. */
87              gh_seq_no_flag:1,	/* Sequence number present. */
88              gh_key_flag:1,	/* Key present. */
89              gh_rt_flag:1,	/* Routing present. */
90              gh_cksum_flag:1;	/* Checksum present. */
91    u_char    gh_version:3,	/* GRE version. */
92              gh_flags:4,	/* Flags. */
93              gh_ack_no_flag:1;	/* Acknowledgment sequence number present. */
94    u_short   gh_protocol;	/* Protocol type. */
95    u_short   gh_length;	/* Payload length. */
96    u_short   gh_call_id;	/* Call ID. */
97    u_int32_t gh_seq_no;	/* Sequence number (optional). */
98    u_int32_t gh_ack_no;	/* Acknowledgment number (optional). */
99};
100
101/* The PPTP protocol ID used in the GRE 'proto' field. */
102#define PPTP_GRE_PROTO          0x880b
103
104/* Bits that must be set a certain way in all PPTP/GRE packets. */
105#define PPTP_INIT_VALUE		((0x2001 << 16) | PPTP_GRE_PROTO)
106#define PPTP_INIT_MASK		0xef7fffff
107
108#define PPTP_MAGIC		0x1a2b3c4d
109#define PPTP_CTRL_MSG_TYPE	1
110
111enum {
112  PPTP_StartCtrlConnRequest = 1,
113  PPTP_StartCtrlConnReply = 2,
114  PPTP_StopCtrlConnRequest = 3,
115  PPTP_StopCtrlConnReply = 4,
116  PPTP_EchoRequest = 5,
117  PPTP_EchoReply = 6,
118  PPTP_OutCallRequest = 7,
119  PPTP_OutCallReply = 8,
120  PPTP_InCallRequest = 9,
121  PPTP_InCallReply = 10,
122  PPTP_InCallConn = 11,
123  PPTP_CallClearRequest = 12,
124  PPTP_CallDiscNotify = 13,
125  PPTP_WanErrorNotify = 14,
126  PPTP_SetLinkInfo = 15,
127};
128
129  /* Message structures */
130  struct pptpMsgHead {
131    u_int16_t   length;         /* total length */
132    u_int16_t   msgType;        /* PPTP message type */
133    u_int32_t   magic;          /* magic cookie */
134    u_int16_t   type;           /* control message type */
135    u_int16_t   resv0;          /* reserved */
136  };
137  typedef struct pptpMsgHead    *PptpMsgHead;
138
139  struct pptpCallIds {
140    u_int16_t   cid1;           /* Call ID field #1 */
141    u_int16_t   cid2;           /* Call ID field #2 */
142  };
143  typedef struct pptpCallIds    *PptpCallId;
144
145static PptpCallId AliasVerifyPptp(struct ip *, u_int16_t *);
146
147int
148PptpGetCallID(struct ip *pip,
149              u_short *call_id)
150{
151    struct grehdr *gr;
152
153    gr = (struct grehdr *)((char *)pip + (pip->ip_hl << 2));
154
155    /* Check GRE header bits. */
156    if ((ntohl(*((u_int32_t *)gr)) & PPTP_INIT_MASK) == PPTP_INIT_VALUE) {
157	*call_id = gr->gh_call_id;
158	return 1;
159    } else
160	return 0;
161};
162
163void PptpSetCallID(struct ip *pip, u_short call_id)
164{
165    struct grehdr *gr;
166
167    gr = (struct grehdr *)((char *)pip + (pip->ip_hl << 2));
168    gr->gh_call_id = call_id;
169};
170
171void
172AliasHandlePptpOut(struct ip *pip,	    /* IP packet to examine/patch */
173                   struct alias_link *link) /* The PPTP control link */
174{
175    struct alias_link   *pptp_link;
176    PptpCallId    	cptr;
177    u_int16_t           ctl_type;           /* control message type */
178    struct tcphdr 	*tc;
179
180    /* Verify valid PPTP control message */
181    if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL)
182      return;
183
184    /* Modify certain PPTP messages */
185    switch (ctl_type) {
186    case PPTP_OutCallRequest:
187    case PPTP_OutCallReply:
188    case PPTP_InCallRequest:
189    case PPTP_InCallReply:
190    case PPTP_CallClearRequest:
191    case PPTP_CallDiscNotify:
192
193      /* Establish PPTP link for address and Call ID found in PPTP Control Msg */
194      pptp_link = FindPptpOut(GetOriginalAddress(link), GetDestAddress(link),
195                              cptr->cid1);
196
197      if (pptp_link != NULL) {
198	int accumulate = cptr->cid1;
199
200	/* alias the Call Id */
201	cptr->cid1 = GetAliasPort(pptp_link);
202
203	/* Compute TCP checksum for revised packet */
204	tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
205	accumulate -= cptr->cid1;
206	ADJUST_CHECKSUM(accumulate, tc->th_sum);
207      }
208      break;
209    default:
210      return;
211    }
212}
213
214void
215AliasHandlePptpIn(struct ip *pip,	   /* IP packet to examine/patch */
216                  struct alias_link *link) /* The PPTP control link */
217{
218    struct alias_link   *pptp_link;
219    PptpCallId    	cptr;
220    u_int16_t     	*pcall_id;
221    u_int16_t           ctl_type;           /* control message type */
222    struct tcphdr 	*tc;
223
224    /* Verify valid PPTP control message */
225    if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL)
226      return;
227
228    /* Modify certain PPTP messages */
229    switch (ctl_type)
230    {
231    case PPTP_InCallConn:
232    case PPTP_WanErrorNotify:
233    case PPTP_SetLinkInfo:
234      pcall_id = &cptr->cid1;
235      break;
236    case PPTP_OutCallReply:
237    case PPTP_InCallReply:
238      pcall_id = &cptr->cid2;
239      break;
240    default:
241      return;
242    }
243
244    /* Find PPTP link for address and Call ID found in PPTP Control Msg */
245    pptp_link = FindPptpIn(GetDestAddress(link), GetAliasAddress(link),
246                           *pcall_id);
247
248    if (pptp_link != NULL) {
249      int accumulate = *pcall_id;
250
251      /* alias the Call Id */
252      *pcall_id = GetOriginalPort(pptp_link);
253
254      /* Compute TCP checksum for modified packet */
255      tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
256      accumulate -= *pcall_id;
257      ADJUST_CHECKSUM(accumulate, tc->th_sum);
258    }
259}
260
261PptpCallId
262AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch */
263{
264    int           	hlen, tlen, dlen;
265    PptpMsgHead   	hptr;
266    struct tcphdr 	*tc;
267
268    /* Calculate some lengths */
269    tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
270    hlen = (pip->ip_hl + tc->th_off) << 2;
271    tlen = ntohs(pip->ip_len);
272    dlen = tlen - hlen;
273
274    /* Verify data length */
275    if (dlen < (sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds)))
276      return(NULL);
277
278    /* Move up to PPTP message header */
279    hptr = (PptpMsgHead)(((char *) pip) + hlen);
280
281    /* Return the control message type */
282    *ptype = ntohs(hptr->type);
283
284    /* Verify PPTP Control Message */
285    if ((ntohs(hptr->msgType) != PPTP_CTRL_MSG_TYPE) ||
286        (ntohl(hptr->magic) != PPTP_MAGIC))
287      return(NULL);
288    else
289      return((PptpCallId)(((char *)hptr) + sizeof(struct pptpMsgHead)));
290}
291