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