xref: /freebsd/sys/netinet/libalias/alias_pptp.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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$
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 GRE alias link is created.
49    The GRE 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 
83 struct 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 
111 enum {
112   PPTP_StartCtrlConnRequest = 1,
113   PPTP_StartCtrlConnReply = 2,
114   PPTP_StopCtrlConnRequest = 3,
115   PPTP_StopCtrlConnReply = 4,
116   PPTP_EchoRequest = 5,
117   PPTP_statoReply = 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 
145 static PptpCallId AliasVerifyPptp(struct ip *, u_int16_t *);
146 
147 int
148 PptpGetCallID(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 
163 void 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 
171 void
172 AliasHandlePptpOut(struct ip *pip,	    /* IP packet to examine/patch */
173                    struct alias_link *link) /* The PPTP control link */
174 {
175     struct alias_link   *gre_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     if ((ctl_type >= PPTP_OutCallRequest) &&
186        (ctl_type <= PPTP_CallDiscNotify)) {
187 
188       /* Establish GRE link for address and Call ID found in PPTP Control Msg */
189       gre_link = FindPptpOut(GetOriginalAddress(link), GetDestAddress(link),
190                              cptr->cid1);
191 
192       if (gre_link != NULL) {
193 	/* alias the Call Id */
194 	cptr->cid1 = GetAliasPort(gre_link);
195 
196 	/* Compute TCP checksum for revised packet */
197 	tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
198 	tc->th_sum = 0;
199 	tc->th_sum = TcpChecksum(pip);
200       }
201     }
202 }
203 
204 void
205 AliasHandlePptpIn(struct ip *pip,	   /* IP packet to examine/patch */
206                   struct alias_link *link) /* The PPTP control link */
207 {
208     struct alias_link   *gre_link;
209     PptpCallId    	cptr;
210     u_int16_t     	*pcall_id;
211     u_int16_t           ctl_type;           /* control message type */
212     struct tcphdr 	*tc;
213 
214     /* Verify valid PPTP control message */
215     if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL)
216       return;
217 
218     /* Modify certain PPTP messages */
219     switch (ctl_type)
220     {
221     case PPTP_InCallConn:
222     case PPTP_WanErrorNotify:
223     case PPTP_SetLinkInfo:
224       pcall_id = &cptr->cid1;
225       break;
226     case PPTP_OutCallReply:
227     case PPTP_InCallReply:
228       pcall_id = &cptr->cid2;
229       break;
230     default:
231       return;
232       break;
233     }
234 
235     /* Find GRE link for address and Call ID found in PPTP Control Msg */
236     gre_link = FindPptpIn(GetDestAddress(link), GetAliasAddress(link),
237 	                  *pcall_id);
238 
239     if (gre_link != NULL) {
240       /* alias the Call Id */
241       *pcall_id = GetOriginalPort(gre_link);
242 
243       /* Compute TCP checksum for modified packet */
244       tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
245       tc->th_sum = 0;
246       tc->th_sum = TcpChecksum(pip);
247     }
248 }
249 
250 PptpCallId
251 AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch */
252 {
253     int           	hlen, tlen, dlen;
254     PptpMsgHead   	hptr;
255     struct tcphdr 	*tc;
256 
257     /* Calculate some lengths */
258     tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
259     hlen = (pip->ip_hl + tc->th_off) << 2;
260     tlen = ntohs(pip->ip_len);
261     dlen = tlen - hlen;
262 
263     /* Verify data length */
264     if (dlen < (sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds)))
265       return(NULL);
266 
267     /* Move up to PPTP message header */
268     hptr = (PptpMsgHead)(((char *) pip) + hlen);
269 
270     /* Return the control message type */
271     *ptype = ntohs(hptr->type);
272 
273     /* Verify PPTP Control Message */
274     if ((ntohs(hptr->msgType) != PPTP_CTRL_MSG_TYPE) ||
275         (ntohl(hptr->magic) != PPTP_MAGIC))
276       return(NULL);
277     else
278       return((PptpCallId)(((char *)hptr) + sizeof(struct pptpMsgHead)));
279 }
280