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: src/sys/netinet/libalias/alias_pptp.c,v 1.15.6.1 2008/11/25 02:59:29 kensmith Exp $");
44 #include <sys/param.h>
45 #include <sys/limits.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
51 #include <sys/types.h>
55 #include <netinet/tcp.h>
58 #include <netinet/libalias/alias.h>
59 #include <netinet/libalias/alias_local.h>
60 #include <netinet/libalias/alias_mod.h>
63 #include "alias_local.h"
64 #include "alias_mod.h"
67 #define PPTP_CONTROL_PORT_NUMBER 1723
70 AliasHandlePptpOut(struct libalias
*, struct ip
*, struct alias_link
*);
73 AliasHandlePptpIn(struct libalias
*, struct ip
*, struct alias_link
*);
76 AliasHandlePptpGreOut(struct libalias
*, struct ip
*);
79 AliasHandlePptpGreIn(struct libalias
*, struct ip
*);
82 fingerprint(struct libalias
*la
, struct ip
*pip
, struct alias_data
*ah
)
85 if (ah
->dport
== NULL
|| ah
->sport
== NULL
|| ah
->lnk
== NULL
)
87 if (ntohs(*ah
->dport
) == PPTP_CONTROL_PORT_NUMBER
88 || ntohs(*ah
->sport
) == PPTP_CONTROL_PORT_NUMBER
)
94 fingerprintgre(struct libalias
*la
, struct ip
*pip
, struct alias_data
*ah
)
101 protohandlerin(struct libalias
*la
, struct ip
*pip
, struct alias_data
*ah
)
104 AliasHandlePptpIn(la
, pip
, ah
->lnk
);
109 protohandlerout(struct libalias
*la
, struct ip
*pip
, struct alias_data
*ah
)
112 AliasHandlePptpOut(la
, pip
, ah
->lnk
);
117 protohandlergrein(struct libalias
*la
, struct ip
*pip
, struct alias_data
*ah
)
120 if (la
->packetAliasMode
& PKT_ALIAS_PROXY_ONLY
||
121 AliasHandlePptpGreIn(la
, pip
) == 0)
127 protohandlergreout(struct libalias
*la
, struct ip
*pip
, struct alias_data
*ah
)
130 if (AliasHandlePptpGreOut(la
, pip
) == 0)
135 /* Kernel module definition. */
136 struct proto_handler handlers
[] = {
141 .fingerprint
= &fingerprint
,
142 .protohandler
= &protohandlerin
148 .fingerprint
= &fingerprint
,
149 .protohandler
= &protohandlerout
152 * WATCH OUT!!! these 2 handlers NEED a priority of INT_MAX (highest possible)
153 * cause they will ALWAYS process packets, so they must be the last one
154 * in chain: look fingerprintgre() above.
160 .fingerprint
= &fingerprintgre
,
161 .protohandler
= &protohandlergrein
167 .fingerprint
= &fingerprintgre
,
168 .protohandler
= &protohandlergreout
173 mod_handler(module_t mod
, int type
, void *data
)
180 LibAliasAttachHandlers(handlers
);
184 LibAliasDetachHandlers(handlers
);
195 moduledata_t alias_mod
= {
196 "alias_pptp", mod_handler
, NULL
200 DECLARE_MODULE(alias_pptp
, alias_mod
, SI_SUB_DRIVERS
, SI_ORDER_SECOND
);
201 MODULE_VERSION(alias_pptp
, 1);
202 MODULE_DEPEND(alias_pptp
, libalias
, 1, 1, 1);
206 Alias_pptp.c performs special processing for PPTP sessions under TCP.
207 Specifically, watch PPTP control messages and alias the Call ID or the
208 Peer's Call ID in the appropriate messages. Note, PPTP requires
209 "de-aliasing" of incoming packets, this is different than any other
210 TCP applications that are currently (ie. FTP, IRC and RTSP) aliased.
212 For Call IDs encountered for the first time, a PPTP alias link is created.
213 The PPTP alias link uses the Call ID in place of the original port number.
214 An alias Call ID is created.
216 For this routine to work, the PPTP control messages must fit entirely
217 into a single TCP packet. This is typically the case, but is not
218 required by the spec.
220 Unlike some of the other TCP applications that are aliased (ie. FTP,
221 IRC and RTSP), the PPTP control messages that need to be aliased are
222 guaranteed to remain the same length. The aliased Call ID is a fixed
227 Initial version: May, 2000 (eds)
235 struct grehdr
{ /* Enhanced GRE header. */
236 u_int16_t gh_flags
; /* Flags. */
237 u_int16_t gh_protocol
; /* Protocol type. */
238 u_int16_t gh_length
; /* Payload length. */
239 u_int16_t gh_call_id
; /* Call ID. */
240 u_int32_t gh_seq_no
; /* Sequence number (optional). */
241 u_int32_t gh_ack_no
; /* Acknowledgment number
244 typedef struct grehdr GreHdr
;
246 /* The PPTP protocol ID used in the GRE 'proto' field. */
247 #define PPTP_GRE_PROTO 0x880b
249 /* Bits that must be set a certain way in all PPTP/GRE packets. */
250 #define PPTP_INIT_VALUE ((0x2001 << 16) | PPTP_GRE_PROTO)
251 #define PPTP_INIT_MASK 0xef7fffff
253 #define PPTP_MAGIC 0x1a2b3c4d
254 #define PPTP_CTRL_MSG_TYPE 1
257 PPTP_StartCtrlConnRequest
= 1,
258 PPTP_StartCtrlConnReply
= 2,
259 PPTP_StopCtrlConnRequest
= 3,
260 PPTP_StopCtrlConnReply
= 4,
261 PPTP_EchoRequest
= 5,
263 PPTP_OutCallRequest
= 7,
264 PPTP_OutCallReply
= 8,
265 PPTP_InCallRequest
= 9,
266 PPTP_InCallReply
= 10,
267 PPTP_InCallConn
= 11,
268 PPTP_CallClearRequest
= 12,
269 PPTP_CallDiscNotify
= 13,
270 PPTP_WanErrorNotify
= 14,
271 PPTP_SetLinkInfo
= 15
274 /* Message structures */
276 u_int16_t length
; /* total length */
277 u_int16_t msgType
;/* PPTP message type */
278 u_int32_t magic
; /* magic cookie */
279 u_int16_t type
; /* control message type */
280 u_int16_t resv0
; /* reserved */
282 typedef struct pptpMsgHead
*PptpMsgHead
;
285 u_int8_t resCode
;/* Result Code */
286 u_int8_t errCode
;/* Error Code */
288 typedef struct pptpCodes
*PptpCode
;
291 u_int16_t cid1
; /* Call ID field #1 */
292 u_int16_t cid2
; /* Call ID field #2 */
294 typedef struct pptpCallIds
*PptpCallId
;
296 static PptpCallId
AliasVerifyPptp(struct ip
*, u_int16_t
*);
300 AliasHandlePptpOut(struct libalias
*la
,
301 struct ip
*pip
, /* IP packet to examine/patch */
302 struct alias_link
*lnk
)
303 { /* The PPTP control link */
304 struct alias_link
*pptp_lnk
;
307 u_int16_t ctl_type
; /* control message type */
310 /* Verify valid PPTP control message */
311 if ((cptr
= AliasVerifyPptp(pip
, &ctl_type
)) == NULL
)
314 /* Modify certain PPTP messages */
316 case PPTP_OutCallRequest
:
317 case PPTP_OutCallReply
:
318 case PPTP_InCallRequest
:
319 case PPTP_InCallReply
:
321 * Establish PPTP link for address and Call ID found in
324 pptp_lnk
= AddPptp(la
, GetOriginalAddress(lnk
), GetDestAddress(lnk
),
325 GetAliasAddress(lnk
), cptr
->cid1
);
327 case PPTP_CallClearRequest
:
328 case PPTP_CallDiscNotify
:
330 * Find PPTP link for address and Call ID found in control
333 pptp_lnk
= FindPptpOutByCallId(la
, GetOriginalAddress(lnk
),
341 if (pptp_lnk
!= NULL
) {
342 int accumulate
= cptr
->cid1
;
344 /* alias the Call Id */
345 cptr
->cid1
= GetAliasPort(pptp_lnk
);
347 /* Compute TCP checksum for revised packet */
348 tc
= (struct tcphdr
*)ip_next(pip
);
349 accumulate
-= cptr
->cid1
;
350 ADJUST_CHECKSUM(accumulate
, tc
->th_sum
);
353 case PPTP_OutCallReply
:
354 case PPTP_InCallReply
:
355 codes
= (PptpCode
) (cptr
+ 1);
356 if (codes
->resCode
== 1) /* Connection
358 SetDestCallId(pptp_lnk
, /* note the Peer's Call
362 SetExpire(pptp_lnk
, 0); /* Connection refused. */
364 case PPTP_CallDiscNotify
: /* Connection closed. */
365 SetExpire(pptp_lnk
, 0);
372 AliasHandlePptpIn(struct libalias
*la
,
373 struct ip
*pip
, /* IP packet to examine/patch */
374 struct alias_link
*lnk
)
375 { /* The PPTP control link */
376 struct alias_link
*pptp_lnk
;
379 u_int16_t ctl_type
; /* control message type */
382 /* Verify valid PPTP control message */
383 if ((cptr
= AliasVerifyPptp(pip
, &ctl_type
)) == NULL
)
386 /* Modify certain PPTP messages */
388 case PPTP_InCallConn
:
389 case PPTP_WanErrorNotify
:
390 case PPTP_SetLinkInfo
:
391 pcall_id
= &cptr
->cid1
;
393 case PPTP_OutCallReply
:
394 case PPTP_InCallReply
:
395 pcall_id
= &cptr
->cid2
;
397 case PPTP_CallDiscNotify
: /* Connection closed. */
398 pptp_lnk
= FindPptpInByCallId(la
, GetDestAddress(lnk
),
399 GetAliasAddress(lnk
),
401 if (pptp_lnk
!= NULL
)
402 SetExpire(pptp_lnk
, 0);
408 /* Find PPTP link for address and Call ID found in PPTP Control Msg */
409 pptp_lnk
= FindPptpInByPeerCallId(la
, GetDestAddress(lnk
),
410 GetAliasAddress(lnk
),
413 if (pptp_lnk
!= NULL
) {
414 int accumulate
= *pcall_id
;
416 /* De-alias the Peer's Call Id. */
417 *pcall_id
= GetOriginalPort(pptp_lnk
);
419 /* Compute TCP checksum for modified packet */
420 tc
= (struct tcphdr
*)ip_next(pip
);
421 accumulate
-= *pcall_id
;
422 ADJUST_CHECKSUM(accumulate
, tc
->th_sum
);
424 if (ctl_type
== PPTP_OutCallReply
|| ctl_type
== PPTP_InCallReply
) {
425 PptpCode codes
= (PptpCode
) (cptr
+ 1);
427 if (codes
->resCode
== 1) /* Connection
429 SetDestCallId(pptp_lnk
, /* note the Call ID. */
432 SetExpire(pptp_lnk
, 0); /* Connection refused. */
438 AliasVerifyPptp(struct ip
*pip
, u_int16_t
* ptype
)
439 { /* IP packet to examine/patch */
440 int hlen
, tlen
, dlen
;
444 /* Calculate some lengths */
445 tc
= (struct tcphdr
*)ip_next(pip
);
446 hlen
= (pip
->ip_hl
+ tc
->th_off
) << 2;
447 tlen
= ntohs(pip
->ip_len
);
450 /* Verify data length */
451 if (dlen
< (int)(sizeof(struct pptpMsgHead
) + sizeof(struct pptpCallIds
)))
454 /* Move up to PPTP message header */
455 hptr
= (PptpMsgHead
) tcp_next(tc
);
457 /* Return the control message type */
458 *ptype
= ntohs(hptr
->type
);
460 /* Verify PPTP Control Message */
461 if ((ntohs(hptr
->msgType
) != PPTP_CTRL_MSG_TYPE
) ||
462 (ntohl(hptr
->magic
) != PPTP_MAGIC
))
465 /* Verify data length. */
466 if ((*ptype
== PPTP_OutCallReply
|| *ptype
== PPTP_InCallReply
) &&
467 (dlen
< (int)(sizeof(struct pptpMsgHead
) + sizeof(struct pptpCallIds
) +
468 sizeof(struct pptpCodes
))))
471 return (PptpCallId
) (hptr
+ 1);
475 AliasHandlePptpGreOut(struct libalias
*la
, struct ip
*pip
)
478 struct alias_link
*lnk
;
480 gr
= (GreHdr
*) ip_next(pip
);
482 /* Check GRE header bits. */
483 if ((ntohl(*((u_int32_t
*) gr
)) & PPTP_INIT_MASK
) != PPTP_INIT_VALUE
)
486 lnk
= FindPptpOutByPeerCallId(la
, pip
->ip_src
, pip
->ip_dst
, gr
->gh_call_id
);
488 struct in_addr alias_addr
= GetAliasAddress(lnk
);
490 /* Change source IP address. */
491 DifferentialChecksum(&pip
->ip_sum
,
492 &alias_addr
, &pip
->ip_src
, 2);
493 pip
->ip_src
= alias_addr
;
499 AliasHandlePptpGreIn(struct libalias
*la
, struct ip
*pip
)
502 struct alias_link
*lnk
;
504 gr
= (GreHdr
*) ip_next(pip
);
506 /* Check GRE header bits. */
507 if ((ntohl(*((u_int32_t
*) gr
)) & PPTP_INIT_MASK
) != PPTP_INIT_VALUE
)
510 lnk
= FindPptpInByPeerCallId(la
, pip
->ip_src
, pip
->ip_dst
, gr
->gh_call_id
);
512 struct in_addr src_addr
= GetOriginalAddress(lnk
);
514 /* De-alias the Peer's Call Id. */
515 gr
->gh_call_id
= GetOriginalPort(lnk
);
517 /* Restore original IP address. */
518 DifferentialChecksum(&pip
->ip_sum
,
519 &src_addr
, &pip
->ip_dst
, 2);
520 pip
->ip_dst
= src_addr
;