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