xref: /freebsd/sys/netpfil/ipfilter/netinet/ip_pptp_pxy.c (revision 79d23845179a534f533185763cb92032202729a7)
1 /*
2  * Copyright (C) 2012 by Darren Reed.
3  *
4  * Simple PPTP transparent proxy for in-kernel use.  For use with the NAT
5  * code.
6  *
7  * $Id$
8  *
9  */
10 #define	IPF_PPTP_PROXY
11 
12 
13 
14 /*
15  * PPTP proxy
16  */
17 typedef struct pptp_side {
18 	u_32_t		pptps_nexthdr;
19 	u_32_t		pptps_next;
20 	int		pptps_state;
21 	int		pptps_gothdr;
22 	int		pptps_len;
23 	int		pptps_bytes;
24 	char		*pptps_wptr;
25 	char		pptps_buffer[512];
26 } pptp_side_t;
27 
28 typedef struct pptp_pxy {
29 	nat_t		*pptp_nat;
30 	struct ipstate	*pptp_state;
31 	u_short		pptp_call[2];
32 	pptp_side_t	pptp_side[2];
33 	ipnat_t		*pptp_rule;
34 } pptp_pxy_t;
35 
36 typedef	struct pptp_hdr {
37 	u_short		pptph_len;
38 	u_short		pptph_type;
39 	u_32_t		pptph_cookie;
40 } pptp_hdr_t;
41 
42 #define	PPTP_MSGTYPE_CTL	1
43 #define	PPTP_MTCTL_STARTREQ	1
44 #define	PPTP_MTCTL_STARTREP	2
45 #define	PPTP_MTCTL_STOPREQ	3
46 #define	PPTP_MTCTL_STOPREP	4
47 #define	PPTP_MTCTL_ECHOREQ	5
48 #define	PPTP_MTCTL_ECHOREP	6
49 #define	PPTP_MTCTL_OUTREQ	7
50 #define	PPTP_MTCTL_OUTREP	8
51 #define	PPTP_MTCTL_INREQ	9
52 #define	PPTP_MTCTL_INREP	10
53 #define	PPTP_MTCTL_INCONNECT	11
54 #define	PPTP_MTCTL_CLEAR	12
55 #define	PPTP_MTCTL_DISCONNECT	13
56 #define	PPTP_MTCTL_WANERROR	14
57 #define	PPTP_MTCTL_LINKINFO	15
58 
59 
60 void ipf_p_pptp_main_load(void);
61 void ipf_p_pptp_main_unload(void);
62 int ipf_p_pptp_new(void *, fr_info_t *, ap_session_t *, nat_t *);
63 void ipf_p_pptp_del(ipf_main_softc_t *, ap_session_t *);
64 int ipf_p_pptp_inout(void *, fr_info_t *, ap_session_t *, nat_t *);
65 void ipf_p_pptp_donatstate(fr_info_t *, nat_t *, pptp_pxy_t *);
66 int ipf_p_pptp_message(fr_info_t *, nat_t *, pptp_pxy_t *, pptp_side_t *);
67 int ipf_p_pptp_nextmessage(fr_info_t *, nat_t *, pptp_pxy_t *, int);
68 int ipf_p_pptp_mctl(fr_info_t *, nat_t *, pptp_pxy_t *, pptp_side_t *);
69 
70 static	frentry_t	pptpfr;
71 
72 static	int	pptp_proxy_init = 0;
73 static	int	ipf_p_pptp_debug = 0;
74 static	int	ipf_p_pptp_gretimeout = IPF_TTLVAL(120);	/* 2 minutes */
75 
76 
77 /*
78  * PPTP application proxy initialization.
79  */
80 void
ipf_p_pptp_main_load(void)81 ipf_p_pptp_main_load(void)
82 {
83 	bzero((char *)&pptpfr, sizeof(pptpfr));
84 	pptpfr.fr_ref = 1;
85 	pptpfr.fr_age[0] = ipf_p_pptp_gretimeout;
86 	pptpfr.fr_age[1] = ipf_p_pptp_gretimeout;
87 	pptpfr.fr_flags = FR_OUTQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
88 	MUTEX_INIT(&pptpfr.fr_lock, "PPTP proxy rule lock");
89 	pptp_proxy_init = 1;
90 }
91 
92 
93 void
ipf_p_pptp_main_unload(void)94 ipf_p_pptp_main_unload(void)
95 {
96 	if (pptp_proxy_init == 1) {
97 		MUTEX_DESTROY(&pptpfr.fr_lock);
98 		pptp_proxy_init = 0;
99 	}
100 }
101 
102 
103 /*
104  * Setup for a new PPTP proxy.
105  *
106  * NOTE: The printf's are broken up with %s in them to prevent them being
107  * optimised into puts statements on FreeBSD (this doesn't exist in the kernel)
108  */
109 int
ipf_p_pptp_new(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)110 ipf_p_pptp_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
111 {
112 	pptp_pxy_t *pptp;
113 	ipnat_t *ipn;
114 	ipnat_t *np;
115 	int size;
116 	ip_t *ip;
117 
118 	if (fin->fin_v != 4)
119 		return (-1);
120 
121 	ip = fin->fin_ip;
122 	np = nat->nat_ptr;
123 	size = np->in_size;
124 
125 	if (ipf_nat_outlookup(fin, 0, IPPROTO_GRE, nat->nat_osrcip,
126 			  ip->ip_dst) != NULL) {
127 		if (ipf_p_pptp_debug > 0)
128 			printf("ipf_p_pptp_new: GRE session already exists\n");
129 		return (-1);
130 	}
131 
132 	KMALLOC(pptp, pptp_pxy_t *);
133 	if (pptp == NULL) {
134 		if (ipf_p_pptp_debug > 0)
135 			printf("ipf_p_pptp_new: malloc for aps_data failed\n");
136 		return (-1);
137 	}
138 	KMALLOCS(ipn, ipnat_t *, size);
139 	if (ipn == NULL) {
140 		KFREE(pptp);
141 		return (-1);
142 	}
143 
144 	aps->aps_data = pptp;
145 	aps->aps_psiz = sizeof(*pptp);
146 	bzero((char *)pptp, sizeof(*pptp));
147 	bzero((char *)ipn, size);
148 	pptp->pptp_rule = ipn;
149 
150 	/*
151 	 * Create NAT rule against which the tunnel/transport mapping is
152 	 * created.  This is required because the current NAT rule does not
153 	 * describe GRE but TCP instead.
154 	 */
155 	ipn->in_size = size;
156 	ipn->in_ifps[0] = fin->fin_ifp;
157 	ipn->in_apr = NULL;
158 	ipn->in_use = 1;
159 	ipn->in_hits = 1;
160 	ipn->in_ippip = 1;
161 	ipn->in_snip = ntohl(nat->nat_nsrcaddr);
162 	ipn->in_nsrcaddr = fin->fin_saddr;
163 	ipn->in_dnip = ntohl(nat->nat_ndstaddr);
164 	ipn->in_ndstaddr = nat->nat_ndstaddr;
165 	ipn->in_redir = np->in_redir;
166 	ipn->in_osrcaddr = nat->nat_osrcaddr;
167 	ipn->in_odstaddr = nat->nat_odstaddr;
168 	ipn->in_osrcmsk = 0xffffffff;
169 	ipn->in_nsrcmsk = 0xffffffff;
170 	ipn->in_odstmsk = 0xffffffff;
171 	ipn->in_ndstmsk = 0xffffffff;
172 	ipn->in_flags = (np->in_flags | IPN_PROXYRULE);
173 	MUTEX_INIT(&ipn->in_lock, "pptp proxy NAT rule");
174 
175 	ipn->in_namelen = np->in_namelen;
176 	bcopy(np->in_names, ipn->in_ifnames, ipn->in_namelen);
177 	ipn->in_ifnames[0] = np->in_ifnames[0];
178 	ipn->in_ifnames[1] = np->in_ifnames[1];
179 
180 	ipn->in_pr[0] = IPPROTO_GRE;
181 	ipn->in_pr[1] = IPPROTO_GRE;
182 
183 	pptp->pptp_side[0].pptps_wptr = pptp->pptp_side[0].pptps_buffer;
184 	pptp->pptp_side[1].pptps_wptr = pptp->pptp_side[1].pptps_buffer;
185 	return (0);
186 }
187 
188 
189 void
ipf_p_pptp_donatstate(fr_info_t * fin,nat_t * nat,pptp_pxy_t * pptp)190 ipf_p_pptp_donatstate(fr_info_t *fin, nat_t *nat, pptp_pxy_t *pptp)
191 {
192 	ipf_main_softc_t *softc = fin->fin_main_soft;
193 	fr_info_t fi;
194 	grehdr_t gre;
195 	nat_t *nat2;
196 	u_char p;
197 	ip_t *ip;
198 
199 	ip = fin->fin_ip;
200 	p = ip->ip_p;
201 
202 	nat2 = pptp->pptp_nat;
203 	if ((nat2 == NULL) || (pptp->pptp_state == NULL)) {
204 		bcopy((char *)fin, (char *)&fi, sizeof(fi));
205 		bzero((char *)&gre, sizeof(gre));
206 		fi.fin_fi.fi_p = IPPROTO_GRE;
207 		fi.fin_fr = &pptpfr;
208 		if ((nat->nat_dir == NAT_OUTBOUND && fin->fin_out) ||
209 		    (nat->nat_dir == NAT_INBOUND && !fin->fin_out)) {
210 			fi.fin_data[0] = pptp->pptp_call[0];
211 			fi.fin_data[1] = pptp->pptp_call[1];
212 		} else {
213 			fi.fin_data[0] = pptp->pptp_call[1];
214 			fi.fin_data[1] = pptp->pptp_call[0];
215 		}
216 		ip = fin->fin_ip;
217 		ip->ip_p = IPPROTO_GRE;
218 		fi.fin_flx &= ~(FI_TCPUDP|FI_STATE|FI_FRAG);
219 		fi.fin_flx |= FI_IGNORE;
220 		fi.fin_dp = &gre;
221 		gre.gr_flags = htons(1 << 13);
222 
223 		fi.fin_fi.fi_saddr = nat->nat_osrcaddr;
224 		fi.fin_fi.fi_daddr = nat->nat_odstaddr;
225 	}
226 
227 	/*
228 	 * Update NAT timeout/create NAT if missing.
229 	 */
230 	if (nat2 != NULL)
231 		ipf_queueback(softc->ipf_ticks, &nat2->nat_tqe);
232 	else {
233 #ifdef USE_MUTEXES
234 		ipf_nat_softc_t *softn = softc->ipf_nat_soft;
235 #endif
236 
237 		MUTEX_ENTER(&softn->ipf_nat_new);
238 		nat2 = ipf_nat_add(&fi, pptp->pptp_rule, &pptp->pptp_nat,
239 				   NAT_SLAVE, nat->nat_dir);
240 		MUTEX_EXIT(&softn->ipf_nat_new);
241 		if (nat2 != NULL) {
242 			(void) ipf_nat_proto(&fi, nat2, 0);
243 			MUTEX_ENTER(&nat2->nat_lock);
244 			ipf_nat_update(&fi, nat2);
245 			MUTEX_EXIT(&nat2->nat_lock);
246 		}
247 	}
248 
249 	READ_ENTER(&softc->ipf_state);
250 	if (pptp->pptp_state != NULL) {
251 		ipf_queueback(softc->ipf_ticks, &pptp->pptp_state->is_sti);
252 		RWLOCK_EXIT(&softc->ipf_state);
253 	} else {
254 		RWLOCK_EXIT(&softc->ipf_state);
255 		if (nat2 != NULL) {
256 			if (nat->nat_dir == NAT_INBOUND)
257 				fi.fin_fi.fi_daddr = nat2->nat_ndstaddr;
258 			else
259 				fi.fin_fi.fi_saddr = nat2->nat_osrcaddr;
260 		}
261 		fi.fin_ifp = NULL;
262 		(void) ipf_state_add(softc, &fi, &pptp->pptp_state, 0);
263 	}
264 	ip->ip_p = p;
265 	return;
266 }
267 
268 
269 /*
270  * Try and build up the next PPTP message in the TCP stream and if we can
271  * build it up completely (fits in our buffer) then pass it off to the message
272  * parsing function.
273  */
274 int
ipf_p_pptp_nextmessage(fr_info_t * fin,nat_t * nat,pptp_pxy_t * pptp,int rev)275 ipf_p_pptp_nextmessage(fr_info_t *fin, nat_t *nat, pptp_pxy_t *pptp, int rev)
276 {
277 	static const char *funcname = "ipf_p_pptp_nextmessage";
278 	pptp_side_t *pptps;
279 	u_32_t start, end;
280 	pptp_hdr_t *hdr;
281 	tcphdr_t *tcp;
282 	int dlen, off;
283 	u_short len;
284 
285 	tcp = fin->fin_dp;
286 	dlen = fin->fin_dlen - (TCP_OFF(tcp) << 2);
287 	start = ntohl(tcp->th_seq);
288 	pptps = &pptp->pptp_side[rev];
289 	off = (char *)tcp - (char *)fin->fin_ip + (TCP_OFF(tcp) << 2) +
290 	      fin->fin_ipoff;
291 
292 	if (dlen <= 0)
293 		return (0);
294 	/*
295 	 * If the complete data packet is before what we expect to see
296 	 * "next", just ignore it as the chances are we've already seen it.
297 	 * The next if statement following this one really just causes packets
298 	 * ahead of what we've seen to be dropped, implying that something in
299 	 * the middle went missing and we want to see that first.
300 	 */
301 	end = start + dlen;
302 	if (pptps->pptps_next > end && pptps->pptps_next > start)
303 		return (0);
304 
305 	if (pptps->pptps_next != start) {
306 		if (ipf_p_pptp_debug > 5)
307 			printf("%s: next (%x) != start (%x)\n", funcname,
308 				pptps->pptps_next, start);
309 		return (-1);
310 	}
311 
312 	while (dlen > 0) {
313 		off += pptps->pptps_bytes;
314 		if (pptps->pptps_gothdr == 0) {
315 			/*
316 			 * PPTP has an 8 byte header that inclues the cookie.
317 			 * The start of every message should include one and
318 			 * it should match 1a2b3c4d.  Byte order is ignored,
319 			 * deliberately, when printing out the error.
320 			 */
321 			len = MIN(8 - pptps->pptps_bytes, dlen);
322 			COPYDATA(fin->fin_m, off, len, pptps->pptps_wptr);
323 			pptps->pptps_bytes += len;
324 			pptps->pptps_wptr += len;
325 			hdr = (pptp_hdr_t *)pptps->pptps_buffer;
326 			if (pptps->pptps_bytes == 8) {
327 				pptps->pptps_next += 8;
328 				if (ntohl(hdr->pptph_cookie) != 0x1a2b3c4d) {
329 					if (ipf_p_pptp_debug > 1)
330 						printf("%s: bad cookie (%x)\n",
331 						       funcname,
332 						       hdr->pptph_cookie);
333 					return (-1);
334 				}
335 			}
336 			dlen -= len;
337 			off += len;
338 
339 			pptps->pptps_gothdr = 1;
340 			len = ntohs(hdr->pptph_len);
341 			pptps->pptps_len = len;
342 			pptps->pptps_nexthdr += len;
343 
344 			/*
345 			 * If a message is too big for the buffer, just set
346 			 * the fields for the next message to come along.
347 			 * The messages defined in RFC 2637 will not exceed
348 			 * 512 bytes (in total length) so this is likely a
349 			 * bad data packet, anyway.
350 			 */
351 			if (len > sizeof(pptps->pptps_buffer)) {
352 				if (ipf_p_pptp_debug > 3)
353 					printf("%s: message too big (%d)\n",
354 					       funcname, len);
355 				pptps->pptps_next = pptps->pptps_nexthdr;
356 				pptps->pptps_wptr = pptps->pptps_buffer;
357 				pptps->pptps_gothdr = 0;
358 				pptps->pptps_bytes = 0;
359 				pptps->pptps_len = 0;
360 				break;
361 			}
362 		}
363 
364 		len = MIN(pptps->pptps_len - pptps->pptps_bytes, dlen);
365 		COPYDATA(fin->fin_m, off, len, pptps->pptps_wptr);
366 		pptps->pptps_bytes += len;
367 		pptps->pptps_wptr += len;
368 		pptps->pptps_next += len;
369 
370 		if (pptps->pptps_len > pptps->pptps_bytes)
371 			break;
372 
373 		ipf_p_pptp_message(fin, nat, pptp, pptps);
374 		pptps->pptps_wptr = pptps->pptps_buffer;
375 		pptps->pptps_gothdr = 0;
376 		pptps->pptps_bytes = 0;
377 		pptps->pptps_len = 0;
378 
379 		start += len;
380 		dlen -= len;
381 	}
382 
383 	return (0);
384 }
385 
386 
387 /*
388  * handle a complete PPTP message
389  */
390 int
ipf_p_pptp_message(fr_info_t * fin,nat_t * nat,pptp_pxy_t * pptp,pptp_side_t * pptps)391 ipf_p_pptp_message(fr_info_t *fin, nat_t *nat, pptp_pxy_t *pptp,
392 	pptp_side_t *pptps)
393 {
394 	pptp_hdr_t *hdr = (pptp_hdr_t *)pptps->pptps_buffer;
395 
396 	switch (ntohs(hdr->pptph_type))
397 	{
398 	case PPTP_MSGTYPE_CTL :
399 		ipf_p_pptp_mctl(fin, nat, pptp, pptps);
400 		break;
401 
402 	default :
403 		break;
404 	}
405 	return (0);
406 }
407 
408 
409 /*
410  * handle a complete PPTP control message
411  */
412 int
ipf_p_pptp_mctl(fr_info_t * fin,nat_t * nat,pptp_pxy_t * pptp,pptp_side_t * pptps)413 ipf_p_pptp_mctl(fr_info_t *fin, nat_t *nat, pptp_pxy_t *pptp,
414 	pptp_side_t *pptps)
415 {
416 	u_short *buffer = (u_short *)(pptps->pptps_buffer);
417 	pptp_side_t *pptpo;
418 
419 	if (pptps == &pptp->pptp_side[0])
420 		pptpo = &pptp->pptp_side[1];
421 	else
422 		pptpo = &pptp->pptp_side[0];
423 
424 	/*
425 	 * Breakout to handle all the various messages.  Most are just state
426 	 * transition.
427 	 */
428 	switch (ntohs(buffer[4]))
429 	{
430 	case PPTP_MTCTL_STARTREQ :
431 		pptps->pptps_state = PPTP_MTCTL_STARTREQ;
432 		break;
433 	case PPTP_MTCTL_STARTREP :
434 		if (pptpo->pptps_state == PPTP_MTCTL_STARTREQ)
435 			pptps->pptps_state = PPTP_MTCTL_STARTREP;
436 		break;
437 	case PPTP_MTCTL_STOPREQ :
438 		pptps->pptps_state = PPTP_MTCTL_STOPREQ;
439 		break;
440 	case PPTP_MTCTL_STOPREP :
441 		if (pptpo->pptps_state == PPTP_MTCTL_STOPREQ)
442 			pptps->pptps_state = PPTP_MTCTL_STOPREP;
443 		break;
444 	case PPTP_MTCTL_ECHOREQ :
445 		pptps->pptps_state = PPTP_MTCTL_ECHOREQ;
446 		break;
447 	case PPTP_MTCTL_ECHOREP :
448 		if (pptpo->pptps_state == PPTP_MTCTL_ECHOREQ)
449 			pptps->pptps_state = PPTP_MTCTL_ECHOREP;
450 		break;
451 	case PPTP_MTCTL_OUTREQ :
452 		pptps->pptps_state = PPTP_MTCTL_OUTREQ;
453 		break;
454 	case PPTP_MTCTL_OUTREP :
455 		if (pptpo->pptps_state == PPTP_MTCTL_OUTREQ) {
456 			pptps->pptps_state = PPTP_MTCTL_OUTREP;
457 			pptp->pptp_call[0] = buffer[7];
458 			pptp->pptp_call[1] = buffer[6];
459 			ipf_p_pptp_donatstate(fin, nat, pptp);
460 		}
461 		break;
462 	case PPTP_MTCTL_INREQ :
463 		pptps->pptps_state = PPTP_MTCTL_INREQ;
464 		break;
465 	case PPTP_MTCTL_INREP :
466 		if (pptpo->pptps_state == PPTP_MTCTL_INREQ) {
467 			pptps->pptps_state = PPTP_MTCTL_INREP;
468 			pptp->pptp_call[0] = buffer[7];
469 			pptp->pptp_call[1] = buffer[6];
470 			ipf_p_pptp_donatstate(fin, nat, pptp);
471 		}
472 		break;
473 	case PPTP_MTCTL_INCONNECT :
474 		pptps->pptps_state = PPTP_MTCTL_INCONNECT;
475 		break;
476 	case PPTP_MTCTL_CLEAR :
477 		pptps->pptps_state = PPTP_MTCTL_CLEAR;
478 		break;
479 	case PPTP_MTCTL_DISCONNECT :
480 		pptps->pptps_state = PPTP_MTCTL_DISCONNECT;
481 		break;
482 	case PPTP_MTCTL_WANERROR :
483 		pptps->pptps_state = PPTP_MTCTL_WANERROR;
484 		break;
485 	case PPTP_MTCTL_LINKINFO :
486 		pptps->pptps_state = PPTP_MTCTL_LINKINFO;
487 		break;
488 	}
489 
490 	return (0);
491 }
492 
493 
494 /*
495  * For outgoing PPTP packets.  refresh timeouts for NAT & state entries, if
496  * we can.  If they have disappeared, recreate them.
497  */
498 int
ipf_p_pptp_inout(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat)499 ipf_p_pptp_inout(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat)
500 {
501 	pptp_pxy_t *pptp;
502 	tcphdr_t *tcp;
503 	int rev;
504 
505 	if ((fin->fin_out == 1) && (nat->nat_dir == NAT_INBOUND))
506 		rev = 1;
507 	else if ((fin->fin_out == 0) && (nat->nat_dir == NAT_OUTBOUND))
508 		rev = 1;
509 	else
510 		rev = 0;
511 
512 	tcp = (tcphdr_t *)fin->fin_dp;
513 	if ((tcp_get_flags(tcp) & TH_OPENING) == TH_OPENING) {
514 		pptp = (pptp_pxy_t *)aps->aps_data;
515 		pptp->pptp_side[1 - rev].pptps_next = ntohl(tcp->th_ack);
516 		pptp->pptp_side[1 - rev].pptps_nexthdr = ntohl(tcp->th_ack);
517 		pptp->pptp_side[rev].pptps_next = ntohl(tcp->th_seq) + 1;
518 		pptp->pptp_side[rev].pptps_nexthdr = ntohl(tcp->th_seq) + 1;
519 	}
520 	return (ipf_p_pptp_nextmessage(fin, nat, (pptp_pxy_t *)aps->aps_data,
521 				     rev));
522 }
523 
524 
525 /*
526  * clean up after ourselves.
527  */
528 void
ipf_p_pptp_del(ipf_main_softc_t * softc,ap_session_t * aps)529 ipf_p_pptp_del(ipf_main_softc_t *softc, ap_session_t *aps)
530 {
531 	pptp_pxy_t *pptp;
532 
533 	pptp = aps->aps_data;
534 
535 	if (pptp != NULL) {
536 		/*
537 		 * Don't bother changing any of the NAT structure details,
538 		 * *_del() is on a callback from aps_free(), from nat_delete()
539 		 */
540 
541 		READ_ENTER(&softc->ipf_state);
542 		if (pptp->pptp_state != NULL) {
543 			ipf_state_setpending(softc, pptp->pptp_state);
544 		}
545 		RWLOCK_EXIT(&softc->ipf_state);
546 
547 		if (pptp->pptp_nat != NULL)
548 			ipf_nat_setpending(softc, pptp->pptp_nat);
549 		pptp->pptp_rule->in_flags |= IPN_DELETE;
550 		ipf_nat_rule_deref(softc, &pptp->pptp_rule);
551 	}
552 }
553