alias_pptp.c revision 64334
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 64334 2000-08-07 09:51:04Z 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 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 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_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 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 *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 int accumulate = cptr->cid1; 194 195 /* alias the Call Id */ 196 cptr->cid1 = GetAliasPort(gre_link); 197 198 /* Compute TCP checksum for revised packet */ 199 tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2)); 200 accumulate -= cptr->cid1; 201 ADJUST_CHECKSUM(accumulate, tc->th_sum); 202 } 203 } 204} 205 206void 207AliasHandlePptpIn(struct ip *pip, /* IP packet to examine/patch */ 208 struct alias_link *link) /* The PPTP control link */ 209{ 210 struct alias_link *gre_link; 211 PptpCallId cptr; 212 u_int16_t *pcall_id; 213 u_int16_t ctl_type; /* control message type */ 214 struct tcphdr *tc; 215 216 /* Verify valid PPTP control message */ 217 if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL) 218 return; 219 220 /* Modify certain PPTP messages */ 221 switch (ctl_type) 222 { 223 case PPTP_InCallConn: 224 case PPTP_WanErrorNotify: 225 case PPTP_SetLinkInfo: 226 pcall_id = &cptr->cid1; 227 break; 228 case PPTP_OutCallReply: 229 case PPTP_InCallReply: 230 pcall_id = &cptr->cid2; 231 break; 232 default: 233 return; 234 break; 235 } 236 237 /* Find GRE link for address and Call ID found in PPTP Control Msg */ 238 gre_link = FindPptpIn(GetDestAddress(link), GetAliasAddress(link), 239 *pcall_id); 240 241 if (gre_link != NULL) { 242 int accumulate = *pcall_id; 243 244 /* alias the Call Id */ 245 *pcall_id = GetOriginalPort(gre_link); 246 247 /* Compute TCP checksum for modified packet */ 248 tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2)); 249 accumulate -= *pcall_id; 250 ADJUST_CHECKSUM(accumulate, tc->th_sum); 251 } 252} 253 254PptpCallId 255AliasVerifyPptp(struct ip *pip, u_int16_t *ptype) /* IP packet to examine/patch */ 256{ 257 int hlen, tlen, dlen; 258 PptpMsgHead hptr; 259 struct tcphdr *tc; 260 261 /* Calculate some lengths */ 262 tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2)); 263 hlen = (pip->ip_hl + tc->th_off) << 2; 264 tlen = ntohs(pip->ip_len); 265 dlen = tlen - hlen; 266 267 /* Verify data length */ 268 if (dlen < (sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds))) 269 return(NULL); 270 271 /* Move up to PPTP message header */ 272 hptr = (PptpMsgHead)(((char *) pip) + hlen); 273 274 /* Return the control message type */ 275 *ptype = ntohs(hptr->type); 276 277 /* Verify PPTP Control Message */ 278 if ((ntohs(hptr->msgType) != PPTP_CTRL_MSG_TYPE) || 279 (ntohl(hptr->magic) != PPTP_MAGIC)) 280 return(NULL); 281 else 282 return((PptpCallId)(((char *)hptr) + sizeof(struct pptpMsgHead))); 283} 284