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