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$");
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 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 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 *);
299 AliasHandlePptpOut(struct libalias *la,
300 struct ip *pip, /* IP packet to examine/patch */
301 struct alias_link *lnk)
302 { /* The PPTP control link */
303 struct alias_link *pptp_lnk;
306 u_int16_t ctl_type; /* control message type */
309 /* Verify valid PPTP control message */
310 if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL)
313 /* Modify certain PPTP messages */
315 case PPTP_OutCallRequest:
316 case PPTP_OutCallReply:
317 case PPTP_InCallRequest:
318 case PPTP_InCallReply:
320 * Establish PPTP link for address and Call ID found in
323 pptp_lnk = AddPptp(la, GetOriginalAddress(lnk), GetDestAddress(lnk),
324 GetAliasAddress(lnk), cptr->cid1);
326 case PPTP_CallClearRequest:
327 case PPTP_CallDiscNotify:
329 * Find PPTP link for address and Call ID found in control
332 pptp_lnk = FindPptpOutByCallId(la, GetOriginalAddress(lnk),
340 if (pptp_lnk != NULL) {
341 int accumulate = cptr->cid1;
343 /* alias the Call Id */
344 cptr->cid1 = GetAliasPort(pptp_lnk);
346 /* Compute TCP checksum for revised packet */
347 tc = (struct tcphdr *)ip_next(pip);
348 accumulate -= cptr->cid1;
349 ADJUST_CHECKSUM(accumulate, tc->th_sum);
352 case PPTP_OutCallReply:
353 case PPTP_InCallReply:
354 codes = (PptpCode) (cptr + 1);
355 if (codes->resCode == 1) /* Connection
357 SetDestCallId(pptp_lnk, /* note the Peer's Call
361 SetExpire(pptp_lnk, 0); /* Connection refused. */
363 case PPTP_CallDiscNotify: /* Connection closed. */
364 SetExpire(pptp_lnk, 0);
371 AliasHandlePptpIn(struct libalias *la,
372 struct ip *pip, /* IP packet to examine/patch */
373 struct alias_link *lnk)
374 { /* The PPTP control link */
375 struct alias_link *pptp_lnk;
378 u_int16_t ctl_type; /* control message type */
381 /* Verify valid PPTP control message */
382 if ((cptr = AliasVerifyPptp(pip, &ctl_type)) == NULL)
385 /* Modify certain PPTP messages */
387 case PPTP_InCallConn:
388 case PPTP_WanErrorNotify:
389 case PPTP_SetLinkInfo:
390 pcall_id = &cptr->cid1;
392 case PPTP_OutCallReply:
393 case PPTP_InCallReply:
394 pcall_id = &cptr->cid2;
396 case PPTP_CallDiscNotify: /* Connection closed. */
397 pptp_lnk = FindPptpInByCallId(la, GetDestAddress(lnk),
398 GetAliasAddress(lnk),
400 if (pptp_lnk != NULL)
401 SetExpire(pptp_lnk, 0);
407 /* Find PPTP link for address and Call ID found in PPTP Control Msg */
408 pptp_lnk = FindPptpInByPeerCallId(la, GetDestAddress(lnk),
409 GetAliasAddress(lnk),
412 if (pptp_lnk != NULL) {
413 int accumulate = *pcall_id;
415 /* De-alias the Peer's Call Id. */
416 *pcall_id = GetOriginalPort(pptp_lnk);
418 /* Compute TCP checksum for modified packet */
419 tc = (struct tcphdr *)ip_next(pip);
420 accumulate -= *pcall_id;
421 ADJUST_CHECKSUM(accumulate, tc->th_sum);
423 if (ctl_type == PPTP_OutCallReply || ctl_type == PPTP_InCallReply) {
424 PptpCode codes = (PptpCode) (cptr + 1);
426 if (codes->resCode == 1) /* Connection
428 SetDestCallId(pptp_lnk, /* note the Call ID. */
431 SetExpire(pptp_lnk, 0); /* Connection refused. */
437 AliasVerifyPptp(struct ip *pip, u_int16_t * ptype)
438 { /* IP packet to examine/patch */
439 int hlen, tlen, dlen;
443 /* Calculate some lengths */
444 tc = (struct tcphdr *)ip_next(pip);
445 hlen = (pip->ip_hl + tc->th_off) << 2;
446 tlen = ntohs(pip->ip_len);
449 /* Verify data length */
450 if (dlen < (int)(sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds)))
453 /* Move up to PPTP message header */
454 hptr = (PptpMsgHead) tcp_next(tc);
456 /* Return the control message type */
457 *ptype = ntohs(hptr->type);
459 /* Verify PPTP Control Message */
460 if ((ntohs(hptr->msgType) != PPTP_CTRL_MSG_TYPE) ||
461 (ntohl(hptr->magic) != PPTP_MAGIC))
464 /* Verify data length. */
465 if ((*ptype == PPTP_OutCallReply || *ptype == PPTP_InCallReply) &&
466 (dlen < (int)(sizeof(struct pptpMsgHead) + sizeof(struct pptpCallIds) +
467 sizeof(struct pptpCodes))))
470 return (PptpCallId) (hptr + 1);
474 AliasHandlePptpGreOut(struct libalias *la, struct ip *pip)
477 struct alias_link *lnk;
479 gr = (GreHdr *) ip_next(pip);
481 /* Check GRE header bits. */
482 if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
485 lnk = FindPptpOutByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id);
487 struct in_addr alias_addr = GetAliasAddress(lnk);
489 /* Change source IP address. */
490 DifferentialChecksum(&pip->ip_sum,
491 &alias_addr, &pip->ip_src, 2);
492 pip->ip_src = alias_addr;
498 AliasHandlePptpGreIn(struct libalias *la, struct ip *pip)
501 struct alias_link *lnk;
503 gr = (GreHdr *) ip_next(pip);
505 /* Check GRE header bits. */
506 if ((ntohl(*((u_int32_t *) gr)) & PPTP_INIT_MASK) != PPTP_INIT_VALUE)
509 lnk = FindPptpInByPeerCallId(la, pip->ip_src, pip->ip_dst, gr->gh_call_id);
511 struct in_addr src_addr = GetOriginalAddress(lnk);
513 /* De-alias the Peer's Call Id. */
514 gr->gh_call_id = GetOriginalPort(lnk);
516 /* Restore original IP address. */
517 DifferentialChecksum(&pip->ip_sum,
518 &src_addr, &pip->ip_dst, 2);
519 pip->ip_dst = src_addr;