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 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 /* 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. 48 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. 52 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 55 required by the spec. 56 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 60 length field. 61 62 Reference: RFC 2637 63 64 Initial version: May, 2000 (eds) 65 66 */ 67 68 /* Includes */ 69 #include <sys/types.h> 70 #include <netinet/in_systm.h> 71 #include <netinet/in.h> 72 #include <netinet/ip.h> 73 #include <netinet/tcp.h> 74 75 #include <stdio.h> 76 77 #include "alias_local.h" 78 79 /* 80 * PPTP definitions 81 */ 82 83 struct grehdr { /* Enhanced GRE header. */ 84 u_int16_t gh_flags; /* Flags. */ 85 u_int16_t gh_protocol; /* Protocol type. */ 86 u_int16_t gh_length; /* Payload length. */ 87 u_int16_t gh_call_id; /* Call ID. */ 88 u_int32_t gh_seq_no; /* Sequence number (optional). */ 89 u_int32_t gh_ack_no; /* Acknowledgment number 90 * (optional). */ 91 }; 92 typedef struct grehdr GreHdr; 93 94 /* The PPTP protocol ID used in the GRE 'proto' field. */ 95 #define PPTP_GRE_PROTO 0x880b 96 97 /* Bits that must be set a certain way in all PPTP/GRE packets. */ 98 #define PPTP_INIT_VALUE ((0x2001 << 16) | PPTP_GRE_PROTO) 99 #define PPTP_INIT_MASK 0xef7fffff 100 101 #define PPTP_MAGIC 0x1a2b3c4d 102 #define PPTP_CTRL_MSG_TYPE 1 103 104 enum { 105 PPTP_StartCtrlConnRequest = 1, 106 PPTP_StartCtrlConnReply = 2, 107 PPTP_StopCtrlConnRequest = 3, 108 PPTP_StopCtrlConnReply = 4, 109 PPTP_EchoRequest = 5, 110 PPTP_EchoReply = 6, 111 PPTP_OutCallRequest = 7, 112 PPTP_OutCallReply = 8, 113 PPTP_InCallRequest = 9, 114 PPTP_InCallReply = 10, 115 PPTP_InCallConn = 11, 116 PPTP_CallClearRequest = 12, 117 PPTP_CallDiscNotify = 13, 118 PPTP_WanErrorNotify = 14, 119 PPTP_SetLinkInfo = 15 120 }; 121 122 /* Message structures */ 123 struct pptpMsgHead { 124 u_int16_t length; /* total length */ 125 u_int16_t msgType;/* PPTP message type */ 126 u_int32_t magic; /* magic cookie */ 127 u_int16_t type; /* control message type */ 128 u_int16_t resv0; /* reserved */ 129 }; 130 typedef struct pptpMsgHead *PptpMsgHead; 131 132 struct pptpCodes { 133 u_int8_t resCode;/* Result Code */ 134 u_int8_t errCode;/* Error Code */ 135 }; 136 typedef struct pptpCodes *PptpCode; 137 138 struct pptpCallIds { 139 u_int16_t cid1; /* Call ID field #1 */ 140 u_int16_t cid2; /* Call ID field #2 */ 141 }; 142 typedef struct pptpCallIds *PptpCallId; 143 144 static PptpCallId AliasVerifyPptp(struct ip *, u_int16_t *); 145 146 147 void 148 AliasHandlePptpOut(struct libalias *la, 149 struct ip *pip, /* IP packet to examine/patch */ 150 struct alias_link *lnk) 151 { /* The PPTP control link */ 152 struct alias_link *pptp_lnk; 153 PptpCallId cptr; 154 PptpCode codes; 155 u_int16_t ctl_type; /* control message type */ 156 struct tcphdr *tc; 157 158 /* Verify valid PPTP control message */ 159 if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL) 160 return; 161 162 /* Modify certain PPTP messages */ 163 switch (ctl_type) { 164 case PPTP_OutCallRequest: 165 case PPTP_OutCallReply: 166 case PPTP_InCallRequest: 167 case PPTP_InCallReply: 168 /* 169 * Establish PPTP link for address and Call ID found in 170 * control message. 171 */ 172 pptp_lnk = AddPptp(la, GetOriginalAddress(lnk), GetDestAddress(lnk), 173 GetAliasAddress(lnk), cptr->cid1); 174 break; 175 case PPTP_CallClearRequest: 176 case PPTP_CallDiscNotify: 177 /* 178 * Find PPTP link for address and Call ID found in control 179 * message. 180 */ 181 pptp_lnk = FindPptpOutByCallId(la, GetOriginalAddress(lnk), 182 GetDestAddress(lnk), 183 cptr->cid1); 184 break; 185 default: 186 return; 187 } 188 189 if (pptp_lnk != NULL) { 190 int accumulate = cptr->cid1; 191 192 /* alias the Call Id */ 193 cptr->cid1 = GetAliasPort(pptp_lnk); 194 195 /* Compute TCP checksum for revised packet */ 196 tc = (struct tcphdr *)ip_next(pip); 197 accumulate -= cptr->cid1; 198 ADJUST_CHECKSUM(accumulate, tc->th_sum); 199 200 switch (ctl_type) { 201 case PPTP_OutCallReply: 202 case PPTP_InCallReply: 203 codes = (PptpCode) (cptr + 1); 204 if (codes->resCode == 1) /* Connection 205 * established, */ 206 SetDestCallId(pptp_lnk, /* note the Peer's Call 207 * ID. */ 208 cptr->cid2); 209 else 210 SetExpire(pptp_lnk, 0); /* Connection refused. */ 211 break; 212 case PPTP_CallDiscNotify: /* Connection closed. */ 213 SetExpire(pptp_lnk, 0); 214 break; 215 } 216 } 217 } 218 219 void 220 AliasHandlePptpIn(struct libalias *la, 221 struct ip *pip, /* IP packet to examine/patch */ 222 struct alias_link *lnk) 223 { /* The PPTP control link */ 224 struct alias_link *pptp_lnk; 225 PptpCallId cptr; 226 u_int16_t *pcall_id; 227 u_int16_t ctl_type; /* control message type */ 228 struct tcphdr *tc; 229 230 /* Verify valid PPTP control message */ 231 if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL) 232 return; 233 234 /* Modify certain PPTP messages */ 235 switch (ctl_type) { 236 case PPTP_InCallConn: 237 case PPTP_WanErrorNotify: 238 case PPTP_SetLinkInfo: 239 pcall_id = &cptr->cid1; 240 break; 241 case PPTP_OutCallReply: 242 case PPTP_InCallReply: 243 pcall_id = &cptr->cid2; 244 break; 245 case PPTP_CallDiscNotify: /* Connection closed. */ 246 pptp_lnk = FindPptpInByCallId(la, GetDestAddress(lnk), 247 GetAliasAddress(lnk), 248 cptr->cid1); 249 if (pptp_lnk != NULL) 250 SetExpire(pptp_lnk, 0); 251 return; 252 default: 253 return; 254 } 255 256 /* Find PPTP link for address and Call ID found in PPTP Control Msg */ 257 pptp_lnk = FindPptpInByPeerCallId(la, GetDestAddress(lnk), 258 GetAliasAddress(lnk), 259 *pcall_id); 260 261 if (pptp_lnk != NULL) { 262 int accumulate = *pcall_id; 263 264 /* De-alias the Peer's Call Id. */ 265 *pcall_id = GetOriginalPort(pptp_lnk); 266 267 /* Compute TCP checksum for modified packet */ 268 tc = (struct tcphdr *)ip_next(pip); 269 accumulate -= *pcall_id; 270 ADJUST_CHECKSUM(accumulate, tc->th_sum); 271 272 if (ctl_type == PPTP_OutCallReply || ctl_type == PPTP_InCallReply) { 273 PptpCode codes = (PptpCode) (cptr + 1); 274 275 if (codes->resCode == 1) /* Connection 276 * established, */ 277 SetDestCallId(pptp_lnk, /* note the Call ID. */ 278 cptr->cid1); 279 else 280 SetExpire(pptp_lnk, 0); /* Connection refused. */ 281 } 282 } 283 } 284 285 static PptpCallId 286 AliasVerifyPptp(struct ip *pip, u_int16_t * ptype) 287 { /* IP packet to examine/patch */ 288 int hlen, tlen, dlen; 289 PptpMsgHead hptr; 290 struct tcphdr *tc; 291 292 /* Calculate some lengths */ 293 tc = (struct tcphdr *)ip_next(pip); 294 hlen = (pip->ip_hl + tc->th_off) << 2; 295 tlen = ntohs(pip->ip_len); 296 dlen = tlen - hlen; 297 298 /* Verify data length */ 299 if (dlen < (int)(sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds))) 300 return (NULL); 301 302 /* Move up to PPTP message header */ 303 hptr = (PptpMsgHead) ip_next(pip); 304 305 /* Return the control message type */ 306 *ptype = ntohs(hptr->type); 307 308 /* Verify PPTP Control Message */ 309 if ((ntohs(hptr->msgType) != PPTP_CTRL_MSG_TYPE) || 310 (ntohl(hptr->magic) != PPTP_MAGIC)) 311 return (NULL); 312 313 /* Verify data length. */ 314 if ((*ptype == PPTP_OutCallReply || *ptype == PPTP_InCallReply) && 315 (dlen < (int)(sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds) + 316 sizeof(struct pptpCodes)))) 317 return (NULL); 318 else 319 return (PptpCallId) (hptr + 1); 320 } 321 322 323 int 324 AliasHandlePptpGreOut(struct libalias *la, struct ip *pip) 325 { 326 GreHdr *gr; 327 struct alias_link *lnk; 328 329 gr = (GreHdr *) ip_next(pip); 330 331 /* Check GRE header bits. */ 332 if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE) 333 return (-1); 334 335 lnk = FindPptpOutByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id); 336 if (lnk != NULL) { 337 struct in_addr alias_addr = GetAliasAddress(lnk); 338 339 /* Change source IP address. */ 340 DifferentialChecksum(&pip->ip_sum, 341 &alias_addr, &pip->ip_src, 2); 342 pip->ip_src = alias_addr; 343 } 344 return (0); 345 } 346 347 348 int 349 AliasHandlePptpGreIn(struct libalias *la, struct ip *pip) 350 { 351 GreHdr *gr; 352 struct alias_link *lnk; 353 354 gr = (GreHdr *) ip_next(pip); 355 356 /* Check GRE header bits. */ 357 if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE) 358 return (-1); 359 360 lnk = FindPptpInByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id); 361 if (lnk != NULL) { 362 struct in_addr src_addr = GetOriginalAddress(lnk); 363 364 /* De-alias the Peer's Call Id. */ 365 gr->gh_call_id = GetOriginalPort(lnk); 366 367 /* Restore original IP address. */ 368 DifferentialChecksum(&pip->ip_sum, 369 &src_addr, &pip->ip_dst, 2); 370 pip->ip_dst = src_addr; 371 } 372 return (0); 373 } 374