1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
4  * PPTP is a a protocol for creating virtual private networks.
5  * It is a specification defined by Microsoft and some vendors
6  * working with Microsoft.  PPTP is built on top of a modified
7  * version of the Internet Generic Routing Encapsulation Protocol.
8  * GRE is defined in RFC 1701 and RFC 1702.  Documentation of
9  * PPTP can be found in RFC 2637
10  *
11  * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
12  *
13  * Development of this code funded by Astaro AG (http://www.astaro.com/)
14  *
15  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
16  *
17  * Limitations:
18  * 	 - We blindly assume that control connections are always
19  * 	   established in PNS->PAC direction.  This is a violation
20  *	   of RFC 2637
21  * 	 - We can only support one single call within each session
22  * TODO:
23  *	 - testing of incoming PPTP calls
24  */
25 
26 #include <linux/module.h>
27 #include <linux/skbuff.h>
28 #include <linux/in.h>
29 #include <linux/tcp.h>
30 
31 #include <net/netfilter/nf_conntrack.h>
32 #include <net/netfilter/nf_conntrack_core.h>
33 #include <net/netfilter/nf_conntrack_helper.h>
34 #include <net/netfilter/nf_conntrack_zones.h>
35 #include <linux/netfilter/nf_conntrack_proto_gre.h>
36 #include <linux/netfilter/nf_conntrack_pptp.h>
37 
38 #define NF_CT_PPTP_VERSION "3.1"
39 
40 MODULE_LICENSE("GPL");
41 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
42 MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
43 MODULE_ALIAS("ip_conntrack_pptp");
44 MODULE_ALIAS_NFCT_HELPER("pptp");
45 
46 static DEFINE_SPINLOCK(nf_pptp_lock);
47 
48 int
49 (*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
50 			     struct nf_conn *ct, enum ip_conntrack_info ctinfo,
51 			     unsigned int protoff, struct PptpControlHeader *ctlh,
52 			     union pptp_ctrl_union *pptpReq) __read_mostly;
53 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
54 
55 int
56 (*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
57 			    struct nf_conn *ct, enum ip_conntrack_info ctinfo,
58 			    unsigned int protoff, struct PptpControlHeader *ctlh,
59 			    union pptp_ctrl_union *pptpReq) __read_mostly;
60 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
61 
62 void
63 (*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
64 			    struct nf_conntrack_expect *expect_reply)
65 			    __read_mostly;
66 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
67 
68 void
69 (*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
70 			     struct nf_conntrack_expect *exp) __read_mostly;
71 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
72 
73 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
74 /* PptpControlMessageType names */
75 const char *const pptp_msg_name[] = {
76 	"UNKNOWN_MESSAGE",
77 	"START_SESSION_REQUEST",
78 	"START_SESSION_REPLY",
79 	"STOP_SESSION_REQUEST",
80 	"STOP_SESSION_REPLY",
81 	"ECHO_REQUEST",
82 	"ECHO_REPLY",
83 	"OUT_CALL_REQUEST",
84 	"OUT_CALL_REPLY",
85 	"IN_CALL_REQUEST",
86 	"IN_CALL_REPLY",
87 	"IN_CALL_CONNECT",
88 	"CALL_CLEAR_REQUEST",
89 	"CALL_DISCONNECT_NOTIFY",
90 	"WAN_ERROR_NOTIFY",
91 	"SET_LINK_INFO"
92 };
93 EXPORT_SYMBOL(pptp_msg_name);
94 #endif
95 
96 #define SECS *HZ
97 #define MINS * 60 SECS
98 #define HOURS * 60 MINS
99 
100 #define PPTP_GRE_TIMEOUT 		(10 MINS)
101 #define PPTP_GRE_STREAM_TIMEOUT 	(5 HOURS)
102 
103 static void pptp_expectfn(struct nf_conn *ct,
104 			 struct nf_conntrack_expect *exp)
105 {
106 	struct net *net = nf_ct_net(ct);
107 	typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
108 	pr_debug("increasing timeouts\n");
109 
110 	/* increase timeout of GRE data channel conntrack entry */
111 	ct->proto.gre.timeout	     = PPTP_GRE_TIMEOUT;
112 	ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
113 
114 	/* Can you see how rusty this code is, compared with the pre-2.6.11
115 	 * one? That's what happened to my shiny newnat of 2002 ;( -HW */
116 
117 	nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn);
118 	if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
119 		nf_nat_pptp_expectfn(ct, exp);
120 	else {
121 		struct nf_conntrack_tuple inv_t;
122 		struct nf_conntrack_expect *exp_other;
123 
124 		/* obviously this tuple inversion only works until you do NAT */
125 		nf_ct_invert_tuple(&inv_t, &exp->tuple);
126 		pr_debug("trying to unexpect other dir: ");
127 		nf_ct_dump_tuple(&inv_t);
128 
129 		exp_other = nf_ct_expect_find_get(net, nf_ct_zone(ct), &inv_t);
130 		if (exp_other) {
131 			/* delete other expectation.  */
132 			pr_debug("found\n");
133 			nf_ct_unexpect_related(exp_other);
134 			nf_ct_expect_put(exp_other);
135 		} else {
136 			pr_debug("not found\n");
137 		}
138 	}
139 }
140 
141 static int destroy_sibling_or_exp(struct net *net, struct nf_conn *ct,
142 				  const struct nf_conntrack_tuple *t)
143 {
144 	const struct nf_conntrack_tuple_hash *h;
145 	const struct nf_conntrack_zone *zone;
146 	struct nf_conntrack_expect *exp;
147 	struct nf_conn *sibling;
148 
149 	pr_debug("trying to timeout ct or exp for tuple ");
150 	nf_ct_dump_tuple(t);
151 
152 	zone = nf_ct_zone(ct);
153 	h = nf_conntrack_find_get(net, zone, t);
154 	if (h)  {
155 		sibling = nf_ct_tuplehash_to_ctrack(h);
156 		pr_debug("setting timeout of conntrack %p to 0\n", sibling);
157 		sibling->proto.gre.timeout	  = 0;
158 		sibling->proto.gre.stream_timeout = 0;
159 		nf_ct_kill(sibling);
160 		nf_ct_put(sibling);
161 		return 1;
162 	} else {
163 		exp = nf_ct_expect_find_get(net, zone, t);
164 		if (exp) {
165 			pr_debug("unexpect_related of expect %p\n", exp);
166 			nf_ct_unexpect_related(exp);
167 			nf_ct_expect_put(exp);
168 			return 1;
169 		}
170 	}
171 	return 0;
172 }
173 
174 /* timeout GRE data connections */
175 static void pptp_destroy_siblings(struct nf_conn *ct)
176 {
177 	struct net *net = nf_ct_net(ct);
178 	const struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
179 	struct nf_conntrack_tuple t;
180 
181 	nf_ct_gre_keymap_destroy(ct);
182 
183 	/* try original (pns->pac) tuple */
184 	memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
185 	t.dst.protonum = IPPROTO_GRE;
186 	t.src.u.gre.key = ct_pptp_info->pns_call_id;
187 	t.dst.u.gre.key = ct_pptp_info->pac_call_id;
188 	if (!destroy_sibling_or_exp(net, ct, &t))
189 		pr_debug("failed to timeout original pns->pac ct/exp\n");
190 
191 	/* try reply (pac->pns) tuple */
192 	memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
193 	t.dst.protonum = IPPROTO_GRE;
194 	t.src.u.gre.key = ct_pptp_info->pac_call_id;
195 	t.dst.u.gre.key = ct_pptp_info->pns_call_id;
196 	if (!destroy_sibling_or_exp(net, ct, &t))
197 		pr_debug("failed to timeout reply pac->pns ct/exp\n");
198 }
199 
200 /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
201 static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
202 {
203 	struct nf_conntrack_expect *exp_orig, *exp_reply;
204 	enum ip_conntrack_dir dir;
205 	int ret = 1;
206 	typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
207 
208 	exp_orig = nf_ct_expect_alloc(ct);
209 	if (exp_orig == NULL)
210 		goto out;
211 
212 	exp_reply = nf_ct_expect_alloc(ct);
213 	if (exp_reply == NULL)
214 		goto out_put_orig;
215 
216 	/* original direction, PNS->PAC */
217 	dir = IP_CT_DIR_ORIGINAL;
218 	nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
219 			  nf_ct_l3num(ct),
220 			  &ct->tuplehash[dir].tuple.src.u3,
221 			  &ct->tuplehash[dir].tuple.dst.u3,
222 			  IPPROTO_GRE, &peer_callid, &callid);
223 	exp_orig->expectfn = pptp_expectfn;
224 
225 	/* reply direction, PAC->PNS */
226 	dir = IP_CT_DIR_REPLY;
227 	nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
228 			  nf_ct_l3num(ct),
229 			  &ct->tuplehash[dir].tuple.src.u3,
230 			  &ct->tuplehash[dir].tuple.dst.u3,
231 			  IPPROTO_GRE, &callid, &peer_callid);
232 	exp_reply->expectfn = pptp_expectfn;
233 
234 	nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
235 	if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
236 		nf_nat_pptp_exp_gre(exp_orig, exp_reply);
237 	if (nf_ct_expect_related(exp_orig) != 0)
238 		goto out_put_both;
239 	if (nf_ct_expect_related(exp_reply) != 0)
240 		goto out_unexpect_orig;
241 
242 	/* Add GRE keymap entries */
243 	if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
244 		goto out_unexpect_both;
245 	if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
246 		nf_ct_gre_keymap_destroy(ct);
247 		goto out_unexpect_both;
248 	}
249 	ret = 0;
250 
251 out_put_both:
252 	nf_ct_expect_put(exp_reply);
253 out_put_orig:
254 	nf_ct_expect_put(exp_orig);
255 out:
256 	return ret;
257 
258 out_unexpect_both:
259 	nf_ct_unexpect_related(exp_reply);
260 out_unexpect_orig:
261 	nf_ct_unexpect_related(exp_orig);
262 	goto out_put_both;
263 }
264 
265 static int
266 pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff,
267 		 struct PptpControlHeader *ctlh,
268 		 union pptp_ctrl_union *pptpReq,
269 		 unsigned int reqlen,
270 		 struct nf_conn *ct,
271 		 enum ip_conntrack_info ctinfo)
272 {
273 	struct nf_ct_pptp_master *info = nfct_help_data(ct);
274 	u_int16_t msg;
275 	__be16 cid = 0, pcid = 0;
276 	typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
277 
278 	msg = ntohs(ctlh->messageType);
279 	pr_debug("inbound control message %s\n", pptp_msg_name[msg]);
280 
281 	switch (msg) {
282 	case PPTP_START_SESSION_REPLY:
283 		/* server confirms new control session */
284 		if (info->sstate < PPTP_SESSION_REQUESTED)
285 			goto invalid;
286 		if (pptpReq->srep.resultCode == PPTP_START_OK)
287 			info->sstate = PPTP_SESSION_CONFIRMED;
288 		else
289 			info->sstate = PPTP_SESSION_ERROR;
290 		break;
291 
292 	case PPTP_STOP_SESSION_REPLY:
293 		/* server confirms end of control session */
294 		if (info->sstate > PPTP_SESSION_STOPREQ)
295 			goto invalid;
296 		if (pptpReq->strep.resultCode == PPTP_STOP_OK)
297 			info->sstate = PPTP_SESSION_NONE;
298 		else
299 			info->sstate = PPTP_SESSION_ERROR;
300 		break;
301 
302 	case PPTP_OUT_CALL_REPLY:
303 		/* server accepted call, we now expect GRE frames */
304 		if (info->sstate != PPTP_SESSION_CONFIRMED)
305 			goto invalid;
306 		if (info->cstate != PPTP_CALL_OUT_REQ &&
307 		    info->cstate != PPTP_CALL_OUT_CONF)
308 			goto invalid;
309 
310 		cid = pptpReq->ocack.callID;
311 		pcid = pptpReq->ocack.peersCallID;
312 		if (info->pns_call_id != pcid)
313 			goto invalid;
314 		pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
315 			 ntohs(cid), ntohs(pcid));
316 
317 		if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
318 			info->cstate = PPTP_CALL_OUT_CONF;
319 			info->pac_call_id = cid;
320 			exp_gre(ct, cid, pcid);
321 		} else
322 			info->cstate = PPTP_CALL_NONE;
323 		break;
324 
325 	case PPTP_IN_CALL_REQUEST:
326 		/* server tells us about incoming call request */
327 		if (info->sstate != PPTP_SESSION_CONFIRMED)
328 			goto invalid;
329 
330 		cid = pptpReq->icreq.callID;
331 		pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
332 		info->cstate = PPTP_CALL_IN_REQ;
333 		info->pac_call_id = cid;
334 		break;
335 
336 	case PPTP_IN_CALL_CONNECT:
337 		/* server tells us about incoming call established */
338 		if (info->sstate != PPTP_SESSION_CONFIRMED)
339 			goto invalid;
340 		if (info->cstate != PPTP_CALL_IN_REP &&
341 		    info->cstate != PPTP_CALL_IN_CONF)
342 			goto invalid;
343 
344 		pcid = pptpReq->iccon.peersCallID;
345 		cid = info->pac_call_id;
346 
347 		if (info->pns_call_id != pcid)
348 			goto invalid;
349 
350 		pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
351 		info->cstate = PPTP_CALL_IN_CONF;
352 
353 		/* we expect a GRE connection from PAC to PNS */
354 		exp_gre(ct, cid, pcid);
355 		break;
356 
357 	case PPTP_CALL_DISCONNECT_NOTIFY:
358 		/* server confirms disconnect */
359 		cid = pptpReq->disc.callID;
360 		pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
361 		info->cstate = PPTP_CALL_NONE;
362 
363 		/* untrack this call id, unexpect GRE packets */
364 		pptp_destroy_siblings(ct);
365 		break;
366 
367 	case PPTP_WAN_ERROR_NOTIFY:
368 	case PPTP_SET_LINK_INFO:
369 	case PPTP_ECHO_REQUEST:
370 	case PPTP_ECHO_REPLY:
371 		/* I don't have to explain these ;) */
372 		break;
373 
374 	default:
375 		goto invalid;
376 	}
377 
378 	nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
379 	if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
380 		return nf_nat_pptp_inbound(skb, ct, ctinfo,
381 					   protoff, ctlh, pptpReq);
382 	return NF_ACCEPT;
383 
384 invalid:
385 	pr_debug("invalid %s: type=%d cid=%u pcid=%u "
386 		 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
387 		 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
388 		 msg, ntohs(cid), ntohs(pcid),  info->cstate, info->sstate,
389 		 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
390 	return NF_ACCEPT;
391 }
392 
393 static int
394 pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff,
395 		  struct PptpControlHeader *ctlh,
396 		  union pptp_ctrl_union *pptpReq,
397 		  unsigned int reqlen,
398 		  struct nf_conn *ct,
399 		  enum ip_conntrack_info ctinfo)
400 {
401 	struct nf_ct_pptp_master *info = nfct_help_data(ct);
402 	u_int16_t msg;
403 	__be16 cid = 0, pcid = 0;
404 	typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
405 
406 	msg = ntohs(ctlh->messageType);
407 	pr_debug("outbound control message %s\n", pptp_msg_name[msg]);
408 
409 	switch (msg) {
410 	case PPTP_START_SESSION_REQUEST:
411 		/* client requests for new control session */
412 		if (info->sstate != PPTP_SESSION_NONE)
413 			goto invalid;
414 		info->sstate = PPTP_SESSION_REQUESTED;
415 		break;
416 
417 	case PPTP_STOP_SESSION_REQUEST:
418 		/* client requests end of control session */
419 		info->sstate = PPTP_SESSION_STOPREQ;
420 		break;
421 
422 	case PPTP_OUT_CALL_REQUEST:
423 		/* client initiating connection to server */
424 		if (info->sstate != PPTP_SESSION_CONFIRMED)
425 			goto invalid;
426 		info->cstate = PPTP_CALL_OUT_REQ;
427 		/* track PNS call id */
428 		cid = pptpReq->ocreq.callID;
429 		pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
430 		info->pns_call_id = cid;
431 		break;
432 
433 	case PPTP_IN_CALL_REPLY:
434 		/* client answers incoming call */
435 		if (info->cstate != PPTP_CALL_IN_REQ &&
436 		    info->cstate != PPTP_CALL_IN_REP)
437 			goto invalid;
438 
439 		cid = pptpReq->icack.callID;
440 		pcid = pptpReq->icack.peersCallID;
441 		if (info->pac_call_id != pcid)
442 			goto invalid;
443 		pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
444 			 ntohs(cid), ntohs(pcid));
445 
446 		if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
447 			/* part two of the three-way handshake */
448 			info->cstate = PPTP_CALL_IN_REP;
449 			info->pns_call_id = cid;
450 		} else
451 			info->cstate = PPTP_CALL_NONE;
452 		break;
453 
454 	case PPTP_CALL_CLEAR_REQUEST:
455 		/* client requests hangup of call */
456 		if (info->sstate != PPTP_SESSION_CONFIRMED)
457 			goto invalid;
458 		/* FUTURE: iterate over all calls and check if
459 		 * call ID is valid.  We don't do this without newnat,
460 		 * because we only know about last call */
461 		info->cstate = PPTP_CALL_CLEAR_REQ;
462 		break;
463 
464 	case PPTP_SET_LINK_INFO:
465 	case PPTP_ECHO_REQUEST:
466 	case PPTP_ECHO_REPLY:
467 		/* I don't have to explain these ;) */
468 		break;
469 
470 	default:
471 		goto invalid;
472 	}
473 
474 	nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
475 	if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
476 		return nf_nat_pptp_outbound(skb, ct, ctinfo,
477 					    protoff, ctlh, pptpReq);
478 	return NF_ACCEPT;
479 
480 invalid:
481 	pr_debug("invalid %s: type=%d cid=%u pcid=%u "
482 		 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
483 		 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
484 		 msg, ntohs(cid), ntohs(pcid),  info->cstate, info->sstate,
485 		 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
486 	return NF_ACCEPT;
487 }
488 
489 static const unsigned int pptp_msg_size[] = {
490 	[PPTP_START_SESSION_REQUEST]  = sizeof(struct PptpStartSessionRequest),
491 	[PPTP_START_SESSION_REPLY]    = sizeof(struct PptpStartSessionReply),
492 	[PPTP_STOP_SESSION_REQUEST]   = sizeof(struct PptpStopSessionRequest),
493 	[PPTP_STOP_SESSION_REPLY]     = sizeof(struct PptpStopSessionReply),
494 	[PPTP_OUT_CALL_REQUEST]       = sizeof(struct PptpOutCallRequest),
495 	[PPTP_OUT_CALL_REPLY]	      = sizeof(struct PptpOutCallReply),
496 	[PPTP_IN_CALL_REQUEST]	      = sizeof(struct PptpInCallRequest),
497 	[PPTP_IN_CALL_REPLY]	      = sizeof(struct PptpInCallReply),
498 	[PPTP_IN_CALL_CONNECT]	      = sizeof(struct PptpInCallConnected),
499 	[PPTP_CALL_CLEAR_REQUEST]     = sizeof(struct PptpClearCallRequest),
500 	[PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
501 	[PPTP_WAN_ERROR_NOTIFY]	      = sizeof(struct PptpWanErrorNotify),
502 	[PPTP_SET_LINK_INFO]	      = sizeof(struct PptpSetLinkInfo),
503 };
504 
505 /* track caller id inside control connection, call expect_related */
506 static int
507 conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
508 		    struct nf_conn *ct, enum ip_conntrack_info ctinfo)
509 
510 {
511 	int dir = CTINFO2DIR(ctinfo);
512 	const struct nf_ct_pptp_master *info = nfct_help_data(ct);
513 	const struct tcphdr *tcph;
514 	struct tcphdr _tcph;
515 	const struct pptp_pkt_hdr *pptph;
516 	struct pptp_pkt_hdr _pptph;
517 	struct PptpControlHeader _ctlh, *ctlh;
518 	union pptp_ctrl_union _pptpReq, *pptpReq;
519 	unsigned int tcplen = skb->len - protoff;
520 	unsigned int datalen, reqlen, nexthdr_off;
521 	int oldsstate, oldcstate;
522 	int ret;
523 	u_int16_t msg;
524 
525 #if IS_ENABLED(CONFIG_NF_NAT)
526 	if (!nf_ct_is_confirmed(ct) && (ct->status & IPS_NAT_MASK)) {
527 		struct nf_conn_nat *nat = nf_ct_ext_find(ct, NF_CT_EXT_NAT);
528 
529 		if (!nat && !nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC))
530 			return NF_DROP;
531 	}
532 #endif
533 	/* don't do any tracking before tcp handshake complete */
534 	if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
535 		return NF_ACCEPT;
536 
537 	nexthdr_off = protoff;
538 	tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
539 	BUG_ON(!tcph);
540 	nexthdr_off += tcph->doff * 4;
541 	datalen = tcplen - tcph->doff * 4;
542 
543 	pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
544 	if (!pptph) {
545 		pr_debug("no full PPTP header, can't track\n");
546 		return NF_ACCEPT;
547 	}
548 	nexthdr_off += sizeof(_pptph);
549 	datalen -= sizeof(_pptph);
550 
551 	/* if it's not a control message we can't do anything with it */
552 	if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
553 	    ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
554 		pr_debug("not a control packet\n");
555 		return NF_ACCEPT;
556 	}
557 
558 	ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
559 	if (!ctlh)
560 		return NF_ACCEPT;
561 	nexthdr_off += sizeof(_ctlh);
562 	datalen -= sizeof(_ctlh);
563 
564 	reqlen = datalen;
565 	msg = ntohs(ctlh->messageType);
566 	if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
567 		return NF_ACCEPT;
568 	if (reqlen > sizeof(*pptpReq))
569 		reqlen = sizeof(*pptpReq);
570 
571 	pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
572 	if (!pptpReq)
573 		return NF_ACCEPT;
574 
575 	oldsstate = info->sstate;
576 	oldcstate = info->cstate;
577 
578 	spin_lock_bh(&nf_pptp_lock);
579 
580 	/* FIXME: We just blindly assume that the control connection is always
581 	 * established from PNS->PAC.  However, RFC makes no guarantee */
582 	if (dir == IP_CT_DIR_ORIGINAL)
583 		/* client -> server (PNS -> PAC) */
584 		ret = pptp_outbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
585 					ctinfo);
586 	else
587 		/* server -> client (PAC -> PNS) */
588 		ret = pptp_inbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
589 				       ctinfo);
590 	pr_debug("sstate: %d->%d, cstate: %d->%d\n",
591 		 oldsstate, info->sstate, oldcstate, info->cstate);
592 	spin_unlock_bh(&nf_pptp_lock);
593 
594 	return ret;
595 }
596 
597 static const struct nf_conntrack_expect_policy pptp_exp_policy = {
598 	.max_expected	= 2,
599 	.timeout	= 5 * 60,
600 };
601 
602 /* control protocol helper */
603 static struct nf_conntrack_helper pptp __read_mostly = {
604 	.name			= "pptp",
605 	.me			= THIS_MODULE,
606 	.tuple.src.l3num	= AF_INET,
607 	.tuple.src.u.tcp.port	= cpu_to_be16(PPTP_CONTROL_PORT),
608 	.tuple.dst.protonum	= IPPROTO_TCP,
609 	.help			= conntrack_pptp_help,
610 	.destroy		= pptp_destroy_siblings,
611 	.expect_policy		= &pptp_exp_policy,
612 };
613 
614 static int __init nf_conntrack_pptp_init(void)
615 {
616 	NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_pptp_master));
617 
618 	return nf_conntrack_helper_register(&pptp);
619 }
620 
621 static void __exit nf_conntrack_pptp_fini(void)
622 {
623 	nf_conntrack_helper_unregister(&pptp);
624 }
625 
626 module_init(nf_conntrack_pptp_init);
627 module_exit(nf_conntrack_pptp_fini);
628