• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/accel-pptpd/kernel/patch/
1diff -uprN linux-2.6.18.orig/drivers/net/Kconfig linux-2.6.18.pptp/drivers/net/Kconfig
2--- linux-2.6.18.orig/drivers/net/Kconfig	2006-10-30 13:20:24.000000000 +0300
3+++ linux-2.6.18.pptp/drivers/net/Kconfig	2006-10-30 13:21:45.000000000 +0300
4@@ -2687,6 +2687,15 @@ config PPPOE
5 	  which contains instruction on how to use this driver (under 
6 	  the heading "Kernel mode PPPoE").
7 
8+config PPTP
9+	tristate "PPTP (Point-to-Point Tunneling Protocol) (EXPERIMENTAL)"
10+	depends on EXPERIMENTAL && PPP
11+	help
12+	  Support for Microsoft Point-to-Point Tunneling Protocol.
13+
14+	  See http://accel-pptp.sourceforge.net/ for information on 
15+	  configuring PPTP clients and servers to utilize this driver.
16+
17 config PPPOATM
18 	tristate "PPP over ATM"
19 	depends on ATM && PPP
20diff -uprN linux-2.6.18.orig/drivers/net/Makefile linux-2.6.18.pptp/drivers/net/Makefile
21--- linux-2.6.18.orig/drivers/net/Makefile	2006-10-30 13:20:23.000000000 +0300
22+++ linux-2.6.18.pptp/drivers/net/Makefile	2006-10-30 13:21:45.000000000 +0300
23@@ -121,6 +121,7 @@ obj-$(CONFIG_PPP_DEFLATE) += ppp_deflate
24 obj-$(CONFIG_PPP_BSDCOMP) += bsd_comp.o
25 obj-$(CONFIG_PPP_MPPE) += ppp_mppe.o
26 obj-$(CONFIG_PPPOE) += pppox.o pppoe.o
27+obj-$(CONFIG_PPTP) += pppox.o pptp.o
28 
29 obj-$(CONFIG_SLIP) += slip.o
30 ifeq ($(CONFIG_SLIP_COMPRESSED),y)
31diff -uprN linux-2.6.18.orig/drivers/net/pptp.c linux-2.6.18.pptp/drivers/net/pptp.c
32--- linux-2.6.18.orig/drivers/net/pptp.c	1970-01-01 03:00:00.000000000 +0300
33+++ linux-2.6.18.pptp/drivers/net/pptp.c	2006-10-30 13:31:37.000000000 +0300
34@@ -0,0 +1,941 @@
35+/*
36+ *  Point-to-Point Tunneling Protocol for Linux
37+ *
38+ *	Authors: Kozlov D. (xeb@mail.ru)
39+ *
40+ *	This program is free software; you can redistribute it and/or
41+ *	modify it under the terms of the GNU General Public License
42+ *	as published by the Free Software Foundation; either version
43+ *	2 of the License, or (at your option) any later version.
44+ *
45+ */
46+
47+#include <linux/string.h>
48+#include <linux/module.h>
49+#include <linux/kernel.h>
50+#include <linux/slab.h>
51+#include <linux/errno.h>
52+#include <linux/netdevice.h>
53+#include <linux/net.h>
54+#include <linux/skbuff.h>
55+#include <linux/init.h>
56+#include <linux/ppp_channel.h>
57+#include <linux/ppp_defs.h>
58+#include <linux/if_ppp.h>
59+#include <linux/if_pppox.h>
60+#include <linux/notifier.h>
61+#include <linux/file.h>
62+#include <linux/proc_fs.h>
63+#include <linux/in.h>
64+#include <linux/ip.h>
65+#include <linux/netfilter.h>
66+#include <linux/netfilter_ipv4.h>
67+#include <linux/workqueue.h>
68+#include <linux/version.h>
69+
70+#include <net/sock.h>
71+#include <net/protocol.h>
72+#include <net/ip.h>
73+#include <net/icmp.h>
74+#include <net/route.h>
75+
76+#include <asm/uaccess.h>
77+
78+#define PPTP_DRIVER_VERSION "0.7"
79+
80+MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol for Linux");
81+MODULE_AUTHOR("Kozlov D. (xeb@mail.ru)");
82+MODULE_LICENSE("GPL");
83+
84+static int log_level=0;
85+static int min_window=5;
86+static int max_window=100;
87+module_param(min_window,int,5);
88+MODULE_PARM_DESC(min_window,"Minimum sliding window size (default=3)");
89+module_param(max_window,int,100);
90+MODULE_PARM_DESC(max_window,"Maximum sliding window size (default=100)");
91+module_param(log_level,int,0);
92+
93+static struct proc_dir_entry* proc_dir;
94+
95+#define HASH_SIZE  16
96+#define HASH(addr) ((addr^(addr>>4))&0xF)
97+static DEFINE_RWLOCK(chan_lock);
98+static struct pppox_sock *chans[HASH_SIZE];
99+
100+static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
101+static int read_proc(char *page, char **start, off_t off,int count,
102+                     int *eof, void *data);
103+static int __pptp_rcv(struct pppox_sock *po,struct sk_buff *skb,int new);
104+
105+static struct ppp_channel_ops pptp_chan_ops= {
106+	.start_xmit = pptp_xmit,
107+};
108+
109+
110+/* gre header structure: -------------------------------------------- */
111+
112+#define PPTP_GRE_PROTO  0x880B
113+#define PPTP_GRE_VER    0x1
114+
115+#define PPTP_GRE_FLAG_C	0x80
116+#define PPTP_GRE_FLAG_R	0x40
117+#define PPTP_GRE_FLAG_K	0x20
118+#define PPTP_GRE_FLAG_S	0x10
119+#define PPTP_GRE_FLAG_A	0x80
120+
121+#define PPTP_GRE_IS_C(f) ((f)&PPTP_GRE_FLAG_C)
122+#define PPTP_GRE_IS_R(f) ((f)&PPTP_GRE_FLAG_R)
123+#define PPTP_GRE_IS_K(f) ((f)&PPTP_GRE_FLAG_K)
124+#define PPTP_GRE_IS_S(f) ((f)&PPTP_GRE_FLAG_S)
125+#define PPTP_GRE_IS_A(f) ((f)&PPTP_GRE_FLAG_A)
126+
127+struct pptp_gre_header {
128+  u_int8_t flags;		/* bitfield */
129+  u_int8_t ver;			/* should be PPTP_GRE_VER (enhanced GRE) */
130+  u_int16_t protocol;		/* should be PPTP_GRE_PROTO (ppp-encaps) */
131+  u_int16_t payload_len;	/* size of ppp payload, not inc. gre header */
132+  u_int16_t call_id;		/* peer's call_id for this session */
133+  u_int32_t seq;		/* sequence number.  Present if S==1 */
134+  u_int32_t ack;		/* seq number of highest packet recieved by */
135+  				/*  sender in this session */
136+};
137+
138+struct gre_statistics {
139+  /* statistics for GRE receive */
140+  unsigned int rx_accepted;  // data packet accepted
141+  unsigned int rx_lost;      // data packet did not arrive before timeout
142+  unsigned int rx_underwin;  // data packet was under window (arrived too late
143+                             // or duplicate packet)
144+  unsigned int rx_buffered;  // data packet arrived earlier than expected,
145+                             // packet(s) before it were lost or reordered
146+  unsigned int rx_errors;    // OS error on receive
147+  unsigned int rx_truncated; // truncated packet
148+  unsigned int rx_invalid;   // wrong protocol or invalid flags
149+  unsigned int rx_acks;      // acknowledgement only
150+
151+  /* statistics for GRE transmit */
152+  unsigned int tx_sent;      // data packet sent
153+  unsigned int tx_failed;    //
154+  unsigned int tx_acks;      // sent packet with just ACK
155+
156+  __u32 pt_seq;
157+  struct timeval pt_time;
158+  unsigned int rtt;
159+};
160+
161+static struct pppox_sock * lookup_chan(__u16 call_id)
162+{
163+	struct pppox_sock *po;
164+	read_lock_bh(&chan_lock);
165+	for(po=chans[HASH(call_id)]; po; po=po->next)
166+		if (po->proto.pptp.src_addr.call_id==call_id)
167+			break;
168+	read_unlock_bh(&chan_lock);
169+	return po;
170+}
171+
172+static void add_chan(struct pppox_sock *po)
173+{
174+	write_lock_bh(&chan_lock);
175+	po->next=chans[HASH(po->proto.pptp.src_addr.call_id)];
176+	chans[HASH(po->proto.pptp.src_addr.call_id)]=po;
177+	write_unlock_bh(&chan_lock);
178+}
179+
180+static void add_free_chan(struct pppox_sock *po)
181+{
182+	__u16 call_id=1;
183+	struct pppox_sock *p;
184+
185+	write_lock_bh(&chan_lock);
186+	for(call_id=1; call_id<65535; call_id++) {
187+		for(p=chans[HASH(call_id)]; p; p=p->next)
188+			if (p->proto.pptp.src_addr.call_id==call_id)
189+				break;
190+		if (!p){
191+			po->proto.pptp.src_addr.call_id=call_id;
192+			po->next=chans[HASH(call_id)];
193+			chans[HASH(call_id)]=po;
194+			break;
195+		}
196+	}
197+	write_unlock_bh(&chan_lock);
198+}
199+
200+static void del_chan(struct pppox_sock *po)
201+{
202+	struct pppox_sock *p1,*p2;
203+	write_lock_bh(&chan_lock);
204+	for(p2=NULL,p1=chans[HASH(po->proto.pptp.src_addr.call_id)]; p1 && p1!=po;
205+				p2=p1,p1=p1->next);
206+	if (p2) p2->next=p1->next;
207+	else chans[HASH(po->proto.pptp.src_addr.call_id)]=p1->next;
208+	write_unlock_bh(&chan_lock);
209+}
210+
211+static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
212+{
213+	struct sock *sk = (struct sock *) chan->private;
214+	struct pppox_sock *po = pppox_sk(sk);
215+	struct pptp_opt *opt=&po->proto.pptp;
216+	struct pptp_gre_header *hdr;
217+	unsigned int header_len=sizeof(*hdr);
218+	int len=skb?skb->len:0;
219+	int err=0;
220+
221+	struct rtable *rt;     			/* Route to the other host */
222+	struct net_device *tdev;			/* Device to other host */
223+	struct iphdr  *iph;			/* Our new IP header */
224+	int    max_headroom;			/* The extra header space needed */
225+
226+
227+	if (skb && opt->seq_sent-opt->ack_recv>opt->window){
228+		opt->pause=1;
229+		return 0;
230+	}
231+
232+	{
233+		struct flowi fl = { .oif = 0,
234+				    .nl_u = { .ip4_u =
235+					      { .daddr = opt->dst_addr.sin_addr.s_addr,
236+						.saddr = opt->src_addr.sin_addr.s_addr,
237+						.tos = RT_TOS(0) } },
238+				    .proto = IPPROTO_GRE };
239+		if ((err=ip_route_output_key(&rt, &fl))) {
240+			goto tx_error;
241+		}
242+	}
243+	tdev = rt->u.dst.dev;
244+
245+	max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph)+sizeof(*hdr)+2;
246+
247+	if (!skb){
248+		skb=dev_alloc_skb(max_headroom);
249+		skb_reserve(skb,max_headroom-skb_headroom(skb));
250+	}else if (skb_headroom(skb) < max_headroom ||
251+						skb_cloned(skb) || skb_shared(skb)) {
252+		struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
253+		if (!new_skb) {
254+			ip_rt_put(rt);
255+			goto tx_error;
256+		}
257+		if (skb->sk)
258+			skb_set_owner_w(new_skb, skb->sk);
259+		dev_kfree_skb(skb);
260+		skb = new_skb;
261+	}
262+
263+	if (skb->len){
264+		int islcp;
265+		unsigned char *data=skb->data;
266+		islcp=((data[0] << 8) + data[1])== PPP_LCP && 1 <= data[2] && data[2] <= 7;
267+		if (islcp) {
268+			data=skb_push(skb,2);
269+			data[0]=0xff;
270+			data[1]=0x03;
271+		}
272+	}
273+	len=skb->len;
274+
275+	if (len==0) header_len-=sizeof(hdr->seq);
276+	if (opt->ack_sent == opt->seq_recv) header_len-=sizeof(hdr->ack);
277+
278+	skb->nh.raw = skb_push(skb, sizeof(*iph)+header_len);
279+	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
280+	#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
281+	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
282+			      IPSKB_REROUTED);
283+	#endif
284+	dst_release(skb->dst);
285+	skb->dst = &rt->u.dst;
286+
287+	/*
288+	 *	Push down and install the IP header.
289+	 */
290+
291+	iph 			=	skb->nh.iph;
292+	iph->version		=	4;
293+	iph->ihl		=	sizeof(struct iphdr) >> 2;
294+	iph->frag_off		=	0;//df;
295+	iph->protocol		=	IPPROTO_GRE;
296+	iph->tos		=	0;
297+	iph->daddr		=	rt->rt_dst;
298+	iph->saddr		=	rt->rt_src;
299+	iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
300+
301+	hdr=(struct pptp_gre_header *)(iph+1);
302+	skb->h.raw = (char*)hdr;
303+
304+	hdr->flags       = PPTP_GRE_FLAG_K;
305+	hdr->ver         = PPTP_GRE_VER;
306+	hdr->protocol    = htons(PPTP_GRE_PROTO);
307+	hdr->call_id     = htons(opt->dst_addr.call_id);
308+
309+	if (!len){
310+		hdr->payload_len = 0;
311+		hdr->ver |= PPTP_GRE_FLAG_A;
312+		/* ack is in odd place because S == 0 */
313+		hdr->seq = htonl(opt->seq_recv);
314+		opt->ack_sent = opt->seq_recv;
315+		opt->stat->tx_acks++;
316+	}else {
317+		//if (!opt->seq_sent){
318+		//}
319+
320+		hdr->flags |= PPTP_GRE_FLAG_S;
321+		hdr->seq    = htonl(opt->seq_sent++);
322+		if (log_level>=2)
323+			printk("PPTP: send packet: seq=%i",opt->seq_sent);
324+		if (opt->ack_sent != opt->seq_recv)	{
325+		/* send ack with this message */
326+			hdr->ver |= PPTP_GRE_FLAG_A;
327+			hdr->ack  = htonl(opt->seq_recv);
328+			opt->ack_sent = opt->seq_recv;
329+			if (log_level>=2)
330+				printk(" ack=%i",opt->seq_recv);
331+		}
332+		hdr->payload_len = htons(len);
333+		if (log_level>=2)
334+			printk("\n");
335+	}
336+
337+	nf_reset(skb);
338+
339+	skb->ip_summed = CHECKSUM_NONE;
340+	iph->tot_len = htons(skb->len);
341+	ip_select_ident(iph, &rt->u.dst, NULL);
342+	ip_send_check(iph);
343+
344+	err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev, dst_output);
345+	if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) {
346+		opt->stat->tx_sent++;
347+		if (!opt->stat->pt_seq){
348+			opt->stat->pt_seq  = opt->seq_sent;
349+			do_gettimeofday(&opt->stat->pt_time);
350+		}
351+	}else goto tx_error;
352+
353+	return 1;
354+
355+tx_error:
356+	opt->stat->tx_failed++;
357+	if (!len) dev_kfree_skb(skb);
358+	return 1;
359+}
360+
361+static void ack_work(struct pppox_sock *po)
362+{
363+	struct pptp_opt *opt=&po->proto.pptp;
364+	if (opt->ack_sent != opt->seq_recv)
365+		pptp_xmit(&po->chan,0);
366+
367+	if (!opt->proc){
368+			char unit[10];
369+			opt->proc=1;
370+			sprintf(unit,"ppp%i",ppp_unit_number(&po->chan));
371+			create_proc_read_entry(unit,0,proc_dir,read_proc,po);
372+	}
373+}
374+
375+static int get_seq(struct sk_buff *skb)
376+{
377+	struct iphdr *iph;
378+	u8 *payload;
379+	struct pptp_gre_header *header;
380+
381+	iph = (struct iphdr*)skb->data;
382+	payload = skb->data + (iph->ihl << 2);
383+	header = (struct pptp_gre_header *)(payload);
384+
385+	return ntohl(header->seq);
386+}
387+static void buf_work(struct pppox_sock *po)
388+{
389+	struct timeval tv1,tv2;
390+	struct sk_buff *skb;
391+	struct pptp_opt *opt=&po->proto.pptp;
392+	unsigned int t;
393+
394+	do_gettimeofday(&tv1);
395+	spin_lock_bh(&opt->skb_buf_lock);
396+	while((skb=skb_dequeue(&opt->skb_buf))){
397+		if (!__pptp_rcv(po,skb,0)){
398+			skb_get_timestamp(skb,&tv2);
399+			t=(tv1.tv_sec-tv2.tv_sec)*1000000+(tv1.tv_usec-tv2.tv_usec);
400+			if (t<opt->stat->rtt){
401+				skb_queue_head(&opt->skb_buf,skb);
402+				schedule_delayed_work(&opt->buf_work,t/100*HZ/10000);
403+				goto exit;
404+			}
405+			t=get_seq(skb)-1;
406+			opt->stat->rx_lost+=t-opt->seq_recv;
407+			opt->seq_recv=t;
408+			__pptp_rcv(po,skb,0);
409+		}
410+	}
411+exit:
412+	spin_unlock_bh(&opt->skb_buf_lock);
413+}
414+
415+
416+#define MISSING_WINDOW 20
417+#define WRAPPED( curseq, lastseq) \
418+    ((((curseq) & 0xffffff00) == 0) && \
419+     (((lastseq) & 0xffffff00 ) == 0xffffff00))
420+static int __pptp_rcv(struct pppox_sock *po,struct sk_buff *skb,int new)
421+{
422+	struct pptp_opt *opt=&po->proto.pptp;
423+	int headersize,payload_len,seq;
424+	__u8 *payload;
425+	struct pptp_gre_header *header;
426+
427+	header = (struct pptp_gre_header *)(skb->data);
428+
429+	if (new){
430+		/* test if acknowledgement present */
431+		if (PPTP_GRE_IS_A(header->ver)){
432+				__u32 ack = (PPTP_GRE_IS_S(header->flags))?
433+						header->ack:header->seq; /* ack in different place if S = 0 */
434+				ack = ntohl( ack);
435+				if (ack > opt->ack_recv) opt->ack_recv = ack;
436+				/* also handle sequence number wrap-around  */
437+				if (WRAPPED(ack,opt->ack_recv)) opt->ack_recv = ack;
438+				if (opt->stat->pt_seq && opt->ack_recv > opt->stat->pt_seq){
439+					struct timeval tv;
440+					unsigned int rtt;
441+					do_gettimeofday(&tv);
442+					rtt = (tv.tv_sec - opt->stat->pt_time.tv_sec)*1000000+
443+						tv.tv_usec-opt->stat->pt_time.tv_usec;
444+					opt->stat->rtt = (opt->stat->rtt + rtt) / 2;
445+					if (opt->stat->rtt>opt->timeout) opt->stat->rtt=opt->timeout;
446+					opt->stat->pt_seq=0;
447+				}
448+				if (opt->pause){
449+					opt->pause=0;
450+					ppp_output_wakeup(&po->chan);
451+				}
452+		}
453+
454+		/* test if payload present */
455+		if (!PPTP_GRE_IS_S(header->flags)){
456+			opt->stat->rx_acks++;
457+			goto drop;
458+		}
459+	}
460+
461+	headersize  = sizeof(*header);
462+	payload_len = ntohs(header->payload_len);
463+	seq         = ntohl(header->seq);
464+
465+	/* no ack present? */
466+	if (!PPTP_GRE_IS_A(header->ver)) headersize -= sizeof(header->ack);
467+	/* check for incomplete packet (length smaller than expected) */
468+	if (skb->len- headersize < payload_len){
469+		if (log_level>=1)
470+			printk("PPTP: discarding truncated packet (expected %d, got %d bytes)\n",
471+						payload_len, skb->len- headersize);
472+		opt->stat->rx_truncated++;
473+		goto drop;
474+	}
475+
476+	payload=skb->data+headersize;
477+	/* check for expected sequence number */
478+	if ((seq == opt->seq_recv + 1) || (!opt->timeout &&
479+			(seq > opt->seq_recv + 1 || WRAPPED(seq, opt->seq_recv)))){
480+		if ( log_level >= 2 )
481+			printk("PPTP: accepting packet %d size=%i (%02x %02x %02x %02x %02x %02x)\n", seq,payload_len,
482+				*(payload +0),
483+				*(payload +1),
484+				*(payload +2),
485+				*(payload +3),
486+				*(payload +4),
487+				*(payload +5));
488+		opt->stat->rx_accepted++;
489+		opt->stat->rx_lost+=seq-(opt->seq_recv + 1);
490+		opt->seq_recv = seq;
491+		schedule_work(&opt->ack_work);
492+
493+		skb_pull(skb,headersize);
494+
495+		if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI){
496+			/* chop off address/control */
497+			if (skb->len < 3)
498+				goto drop;
499+			skb_pull(skb,2);
500+		}
501+
502+		if ((*skb->data) & 1){
503+			/* protocol is compressed */
504+			skb_push(skb, 1)[0] = 0;
505+		}
506+
507+		ppp_input(&po->chan,skb);
508+
509+		return 1;
510+	/* out of order, check if the number is too low and discard the packet.
511+	* (handle sequence number wrap-around, and try to do it right) */
512+	}else if ( seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq) ){
513+		if ( log_level >= 1)
514+			printk("PPTP: discarding duplicate or old packet %d (expecting %d)\n",
515+							seq, opt->seq_recv + 1);
516+		opt->stat->rx_underwin++;
517+	/* sequence number too high, is it reasonably close? */
518+	}else if ( seq < opt->seq_recv + MISSING_WINDOW ||
519+						 WRAPPED(seq, opt->seq_recv + MISSING_WINDOW) ){
520+		opt->stat->rx_buffered++;
521+		if ( log_level >= 1 && new )
522+				printk("PPTP: buffering packet %d (expecting %d, lost or reordered)\n",
523+						seq, opt->seq_recv+1);
524+		return 0;
525+	/* no, packet must be discarded */
526+	}else{
527+		if ( log_level >= 1 )
528+			printk("PPTP: discarding bogus packet %d (expecting %d)\n",
529+							seq, opt->seq_recv + 1);
530+	}
531+drop:
532+	kfree_skb(skb);
533+	return -1;
534+}
535+
536+
537+static int pptp_rcv(struct sk_buff *skb)
538+{
539+	struct pptp_gre_header *header;
540+	struct pppox_sock *po;
541+	struct pptp_opt *opt;
542+
543+	if (!pskb_may_pull(skb, 12))
544+		goto drop;
545+
546+	header = (struct pptp_gre_header *)skb->data;
547+
548+	if (    /* version should be 1 */
549+					((header->ver & 0x7F) != PPTP_GRE_VER) ||
550+					/* PPTP-GRE protocol for PPTP */
551+					(ntohs(header->protocol) != PPTP_GRE_PROTO)||
552+					/* flag C should be clear   */
553+					PPTP_GRE_IS_C(header->flags) ||
554+					/* flag R should be clear   */
555+					PPTP_GRE_IS_R(header->flags) ||
556+					/* flag K should be set     */
557+					(!PPTP_GRE_IS_K(header->flags)) ||
558+					/* routing and recursion ctrl = 0  */
559+					((header->flags&0xF) != 0)){
560+			/* if invalid, discard this packet */
561+		if (log_level>=1)
562+			printk("PPTP: Discarding GRE: %X %X %X %X %X %X\n",
563+							header->ver&0x7F, ntohs(header->protocol),
564+							PPTP_GRE_IS_C(header->flags),
565+							PPTP_GRE_IS_R(header->flags),
566+							PPTP_GRE_IS_K(header->flags),
567+							header->flags & 0xF);
568+		goto drop;
569+	}
570+
571+	dst_release(skb->dst);
572+	skb->dst = NULL;
573+	nf_reset(skb);
574+
575+	if ((po=lookup_chan(htons(header->call_id)))) {
576+		if (!(sk_pppox(po)->sk_state&PPPOX_BOUND))
577+			goto drop;
578+		if (__pptp_rcv(po,skb,1))
579+			buf_work(po);
580+		else{
581+			struct timeval tv;
582+			do_gettimeofday(&tv);
583+			skb_set_timestamp(skb,&tv);
584+			opt=&po->proto.pptp;
585+			spin_lock(&opt->skb_buf_lock);
586+			skb_queue_tail(&opt->skb_buf, skb);
587+			spin_unlock(&opt->skb_buf_lock);
588+			schedule_delayed_work(&opt->buf_work,opt->stat->rtt/100*HZ/10000);
589+		}
590+		goto out;
591+	}else{
592+		if (log_level>=1)
593+			printk("PPTP: Discarding packet from unknown call_id %i\n",header->call_id);
594+		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0);
595+	}
596+
597+drop:
598+	kfree_skb(skb);
599+out:
600+	return 0;
601+}
602+
603+
604+
605+static int proc_output (struct pppox_sock *po,char *buf)
606+{
607+	struct gre_statistics *stat=po->proto.pptp.stat;
608+	char *p=buf;
609+	p+=sprintf(p,"rx accepted  = %d\n",stat->rx_accepted);
610+	p+=sprintf(p,"rx lost      = %d\n",stat->rx_lost);
611+	p+=sprintf(p,"rx under win = %d\n",stat->rx_underwin);
612+	p+=sprintf(p,"rx buffered  = %d\n",stat->rx_buffered);
613+	p+=sprintf(p,"rx invalid   = %d\n",stat->rx_invalid);
614+	p+=sprintf(p,"rx acks      = %d\n",stat->rx_acks);
615+	p+=sprintf(p,"tx sent      = %d\n",stat->tx_sent);
616+	p+=sprintf(p,"tx failed    = %d\n",stat->tx_failed);
617+	p+=sprintf(p,"tx acks      = %d\n",stat->tx_acks);
618+
619+	return p-buf;
620+}
621+static int read_proc(char *page, char **start, off_t off,int count, int *eof, void *data)
622+{
623+	struct pppox_sock *po = data;
624+	int len = proc_output (po,page);
625+	if (len <= off+count) *eof = 1;
626+	*start = page + off;
627+	len -= off;
628+	if (len>count) len = count;
629+	if (len<0) len = 0;
630+	return len;
631+}
632+
633+static int pptp_bind(struct socket *sock,struct sockaddr *uservaddr,int sockaddr_len)
634+{
635+	struct sock *sk = sock->sk;
636+	struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
637+	struct pppox_sock *po = pppox_sk(sk);
638+	struct pptp_opt *opt=&po->proto.pptp;
639+	int error=0;
640+
641+	if (log_level>=1)
642+		printk("PPTP: bind: addr=%X call_id=%i\n",sp->sa_addr.pptp.sin_addr.s_addr,
643+						sp->sa_addr.pptp.call_id);
644+	lock_sock(sk);
645+
646+	opt->src_addr=sp->sa_addr.pptp;
647+	if (sp->sa_addr.pptp.call_id){
648+		if (lookup_chan(sp->sa_addr.pptp.call_id)){
649+			error=-EBUSY;
650+			goto end;
651+		}
652+		add_chan(po);
653+	}else{
654+		add_free_chan(po);
655+		if (!opt->src_addr.call_id)
656+			error=-EBUSY;
657+		if (log_level>=1)
658+			printk("PPTP: using call_id %i\n",opt->src_addr.call_id);
659+	}
660+
661+ end:
662+	release_sock(sk);
663+	return error;
664+}
665+
666+static int pptp_connect(struct socket *sock, struct sockaddr *uservaddr,
667+		  int sockaddr_len, int flags)
668+{
669+	struct sock *sk = sock->sk;
670+	struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
671+	struct pppox_sock *po = pppox_sk(sk);
672+	struct pptp_opt *opt=&po->proto.pptp;
673+	int error=0;
674+
675+	if (log_level>=1)
676+		printk("PPTP: connect: addr=%X call_id=%i\n",
677+						sp->sa_addr.pptp.sin_addr.s_addr,sp->sa_addr.pptp.call_id);
678+
679+	lock_sock(sk);
680+
681+	if (sp->sa_protocol != PX_PROTO_PPTP){
682+		error = -EINVAL;
683+		goto end;
684+	}
685+
686+	/* Check for already bound sockets */
687+	if (sk->sk_state & PPPOX_CONNECTED){
688+		error = -EBUSY;
689+		goto end;
690+	}
691+
692+	/* Check for already disconnected sockets, on attempts to disconnect */
693+	if (sk->sk_state & PPPOX_DEAD){
694+		error = -EALREADY;
695+		goto end;
696+	}
697+
698+	if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr){
699+		error = -EINVAL;
700+		goto end;
701+	}
702+
703+	po->chan.private=sk;
704+	po->chan.ops=&pptp_chan_ops;
705+	po->chan.mtu=PPP_MTU;
706+	po->chan.hdrlen=2+sizeof(struct pptp_gre_header);
707+	error = ppp_register_channel(&po->chan);
708+	if (error){
709+		printk(KERN_ERR "PPTP: failed to register PPP channel (%d)\n",error);
710+		goto end;
711+	}
712+
713+	opt->dst_addr=sp->sa_addr.pptp;
714+	sk->sk_state = PPPOX_CONNECTED;
715+
716+ end:
717+	release_sock(sk);
718+	return error;
719+}
720+
721+static int pptp_getname(struct socket *sock, struct sockaddr *uaddr,
722+		  int *usockaddr_len, int peer)
723+{
724+	int len = sizeof(struct sockaddr_pppox);
725+	struct sockaddr_pppox sp;
726+
727+	sp.sa_family	= AF_PPPOX;
728+	sp.sa_protocol	= PX_PROTO_PPTP;
729+	sp.sa_addr.pptp=pppox_sk(sock->sk)->proto.pptp.src_addr;
730+
731+	memcpy(uaddr, &sp, len);
732+
733+	*usockaddr_len = len;
734+
735+	return 0;
736+}
737+
738+static int pptp_setsockopt(struct socket *sock, int level, int optname,
739+	char* optval, int optlen)
740+{
741+	struct sock *sk = sock->sk;
742+	struct pppox_sock *po = pppox_sk(sk);
743+	struct pptp_opt *opt=&po->proto.pptp;
744+	int val;
745+
746+	if (optlen!=sizeof(int))
747+		return -EINVAL;
748+
749+	if (get_user(val,(int __user*)optval))
750+		return -EFAULT;
751+
752+	switch(optname) {
753+		case PPTP_SO_TIMEOUT:
754+			opt->timeout=val;
755+			break;
756+		case PPTP_SO_WINDOW:
757+			opt->window=val;
758+			break;
759+		default:
760+				return -ENOPROTOOPT;
761+	}
762+
763+	return 0;
764+}
765+
766+static int pptp_getsockopt(struct socket *sock, int level, int optname,
767+	char* optval, int *optlen)
768+{
769+	struct sock *sk = sock->sk;
770+	struct pppox_sock *po = pppox_sk(sk);
771+	struct pptp_opt *opt=&po->proto.pptp;
772+	int len,val;
773+
774+	if (get_user(len,(int __user*)optlen))
775+		return -EFAULT;
776+
777+	if (len<sizeof(int))
778+		return -EINVAL;
779+
780+	switch(optname) {
781+		case PPTP_SO_TIMEOUT:
782+			val=opt->timeout;
783+			break;
784+		case PPTP_SO_WINDOW:
785+			val=opt->window;
786+			break;
787+		default:
788+				return -ENOPROTOOPT;
789+	}
790+
791+	if (put_user(sizeof(int),(int __user*)optlen))
792+		return -EFAULT;
793+
794+	if (put_user(val,(int __user*)optval))
795+		return -EFAULT;
796+
797+	return 0;
798+}
799+
800+static int pptp_release(struct socket *sock)
801+{
802+	struct sock *sk = sock->sk;
803+	struct pppox_sock *po;
804+	struct pptp_opt *opt;
805+	int error = 0;
806+
807+	if (!sk)
808+		return 0;
809+
810+	if (sock_flag(sk, SOCK_DEAD))
811+		return -EBADF;
812+
813+	po = pppox_sk(sk);
814+	opt=&po->proto.pptp;
815+	if (opt->src_addr.sin_addr.s_addr) {
816+		cancel_delayed_work(&opt->buf_work);
817+		flush_scheduled_work();
818+		del_chan(po);
819+		skb_queue_purge(&opt->skb_buf);
820+
821+		if (opt->proc){
822+			char unit[10];
823+			sprintf(unit,"ppp%i",ppp_unit_number(&po->chan));
824+			remove_proc_entry(unit,proc_dir);
825+		}
826+	}
827+
828+	pppox_unbind_sock(sk);
829+
830+	kfree(opt->stat);
831+
832+	/* Signal the death of the socket. */
833+	sk->sk_state = PPPOX_DEAD;
834+
835+	sock_orphan(sk);
836+	sock->sk = NULL;
837+
838+	skb_queue_purge(&sk->sk_receive_queue);
839+	sock_put(sk);
840+
841+	return error;
842+}
843+
844+
845+static struct proto pptp_sk_proto = {
846+	.name	  = "PPTP",
847+	.owner	  = THIS_MODULE,
848+	.obj_size = sizeof(struct pppox_sock),
849+};
850+
851+static const struct proto_ops pptp_ops = {
852+    .family		= AF_PPPOX,
853+    .owner		= THIS_MODULE,
854+    .release		= pptp_release,
855+    .bind		=  pptp_bind,
856+    .connect		= pptp_connect,
857+    .socketpair		= sock_no_socketpair,
858+    .accept		= sock_no_accept,
859+    .getname		= pptp_getname,
860+    .poll		= sock_no_poll,
861+    .listen		= sock_no_listen,
862+    .shutdown		= sock_no_shutdown,
863+    .setsockopt		= pptp_setsockopt,
864+    .getsockopt		= pptp_getsockopt,
865+    .sendmsg		= sock_no_sendmsg,
866+    .recvmsg		= sock_no_recvmsg,
867+    .mmap		= sock_no_mmap,
868+    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
869+    .ioctl		= pppox_ioctl,
870+    #endif
871+};
872+
873+static int pptp_create(struct socket *sock)
874+{
875+	int error = -ENOMEM;
876+	struct sock *sk;
877+	struct pppox_sock *po;
878+	struct pptp_opt *opt;
879+
880+	sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pptp_sk_proto, 1);
881+	if (!sk)
882+		goto out;
883+
884+	sock_init_data(sock, sk);
885+
886+	sock->state = SS_UNCONNECTED;
887+	sock->ops   = &pptp_ops;
888+
889+	//sk->sk_backlog_rcv = pppoe_rcv_core;
890+	sk->sk_state	   = PPPOX_NONE;
891+	sk->sk_type	   = SOCK_STREAM;
892+	sk->sk_family	   = PF_PPPOX;
893+	sk->sk_protocol	   = PX_PROTO_PPTP;
894+
895+	po = pppox_sk(sk);
896+	opt=&po->proto.pptp;
897+
898+	opt->window=min_window;
899+	opt->timeout=0;
900+	opt->seq_sent=0; opt->seq_recv=-1;
901+	opt->ack_recv=0; opt->ack_sent=-1;
902+	skb_queue_head_init(&opt->skb_buf);
903+	opt->skb_buf_lock=SPIN_LOCK_UNLOCKED;
904+	INIT_WORK(&opt->ack_work,(void(*)(void*))ack_work,sk);
905+	INIT_WORK(&opt->buf_work,(void(*)(void*))buf_work,sk);
906+	opt->stat=kzalloc(sizeof(*opt->stat),GFP_KERNEL);
907+
908+	error = 0;
909+out:
910+	return error;
911+}
912+
913+
914+static struct pppox_proto pppox_pptp_proto = {
915+    .create	= pptp_create,
916+    //.ioctl	= pptp_ioctl,
917+    .owner	= THIS_MODULE,
918+};
919+
920+static struct net_protocol net_pptp_protocol = {
921+	.handler	= pptp_rcv,
922+	//.err_handler	=	ipgre_err,
923+};
924+
925+
926+static int pptp_init_module(void)
927+{
928+	int err=0;
929+	printk(KERN_INFO "PPTP driver version " PPTP_DRIVER_VERSION "\n");
930+
931+	if (inet_add_protocol(&net_pptp_protocol, IPPROTO_GRE) < 0) {
932+		printk(KERN_INFO "PPTP: can't add protocol\n");
933+		goto out;
934+	}
935+
936+	err = proto_register(&pptp_sk_proto, 0);
937+	if (err){
938+		printk(KERN_INFO "PPTP: can't register sk_proto\n");
939+		goto out_inet_del_protocol;
940+	}
941+
942+ 	err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto);
943+	if (err){
944+		printk(KERN_INFO "PPTP: can't register pppox_proto\n");
945+		goto out_unregister_sk_proto;
946+	}
947+
948+	proc_dir=proc_mkdir("pptp",NULL);
949+	if (!proc_dir){
950+		printk(KERN_ERR "PPTP: failed to create proc dir\n");
951+	}
952+	//console_verbose();
953+
954+out:
955+	return err;
956+out_unregister_sk_proto:
957+	proto_unregister(&pptp_sk_proto);
958+out_inet_del_protocol:
959+	inet_del_protocol(&net_pptp_protocol, IPPROTO_GRE);
960+	goto out;
961+}
962+
963+static void pptp_exit_module(void)
964+{
965+	unregister_pppox_proto(PX_PROTO_PPTP);
966+	proto_unregister(&pptp_sk_proto);
967+	inet_del_protocol(&net_pptp_protocol, IPPROTO_GRE);
968+
969+	if (proc_dir)
970+		remove_proc_entry("pptp",NULL);
971+}
972+
973+
974+module_init(pptp_init_module);
975+module_exit(pptp_exit_module);
976diff -uprN linux-2.6.18.orig/include/linux/if_pppox.h linux-2.6.18.pptp/include/linux/if_pppox.h
977--- linux-2.6.18.orig/include/linux/if_pppox.h	2006-10-30 13:20:48.000000000 +0300
978+++ linux-2.6.18.pptp/include/linux/if_pppox.h	2006-10-30 11:35:35.000000000 +0300
979@@ -1,6 +1,6 @@
980 /***************************************************************************
981  * Linux PPP over X - Generic PPP transport layer sockets
982- * Linux PPP over Ethernet (PPPoE) Socket Implementation (RFC 2516) 
983+ * Linux PPP over Ethernet (PPPoE) Socket Implementation (RFC 2516)
984  *
985  * This file supplies definitions required by the PPP over Ethernet driver
986  * (pppox.c).  All version information wrt this file is located in pppox.c
987@@ -19,6 +19,7 @@
988 
989 #include <asm/types.h>
990 #include <asm/byteorder.h>
991+#include <linux/in.h>
992 
993 #ifdef  __KERNEL__
994 #include <linux/if_ether.h>
995@@ -36,29 +37,35 @@
996 #define PF_PPPOX	AF_PPPOX
997 #endif /* !(AF_PPPOX) */
998 
999-/************************************************************************ 
1000- * PPPoE addressing definition 
1001- */ 
1002-typedef __u16 sid_t; 
1003-struct pppoe_addr{ 
1004-       sid_t           sid;                    /* Session identifier */ 
1005-       unsigned char   remote[ETH_ALEN];       /* Remote address */ 
1006-       char            dev[IFNAMSIZ];          /* Local device to use */ 
1007-}; 
1008- 
1009-/************************************************************************ 
1010- * Protocols supported by AF_PPPOX 
1011- */ 
1012+/************************************************************************
1013+ * PPPoE addressing definition
1014+ */
1015+typedef __u16 sid_t;
1016+struct pppoe_addr{
1017+       sid_t           sid;                    /* Session identifier */
1018+       unsigned char   remote[ETH_ALEN];       /* Remote address */
1019+       char            dev[IFNAMSIZ];          /* Local device to use */
1020+};
1021+
1022+struct pptp_addr{
1023+       __u16           call_id;
1024+       struct in_addr  sin_addr;
1025+};
1026+/************************************************************************
1027+ * Protocols supported by AF_PPPOX
1028+ */
1029 #define PX_PROTO_OE    0 /* Currently just PPPoE */
1030-#define PX_MAX_PROTO   1	
1031- 
1032-struct sockaddr_pppox { 
1033-       sa_family_t     sa_family;            /* address family, AF_PPPOX */ 
1034-       unsigned int    sa_protocol;          /* protocol identifier */ 
1035-       union{ 
1036-               struct pppoe_addr       pppoe; 
1037-       }sa_addr; 
1038-}__attribute__ ((packed)); 
1039+#define PX_PROTO_PPTP  1
1040+#define PX_MAX_PROTO   2
1041+
1042+struct sockaddr_pppox {
1043+       sa_family_t     sa_family;            /* address family, AF_PPPOX */
1044+       unsigned int    sa_protocol;          /* protocol identifier */
1045+       union{
1046+               struct pppoe_addr       pppoe;
1047+	       			 struct pptp_addr        pptp;
1048+       }sa_addr;
1049+}__attribute__ ((packed));
1050 
1051 
1052 /*********************************************************************
1053@@ -111,6 +118,12 @@ struct pppoe_hdr {
1054 	struct pppoe_tag tag[0];
1055 } __attribute__ ((packed));
1056 
1057+
1058+/* Socket options */
1059+#define PPTP_SO_TIMEOUT 1
1060+#define PPTP_SO_WINDOW  2
1061+
1062+
1063 #ifdef __KERNEL__
1064 struct pppoe_opt {
1065 	struct net_device      *dev;	  /* device associated with socket*/
1066@@ -118,6 +131,21 @@ struct pppoe_opt {
1067 	struct sockaddr_pppox	relay;	  /* what socket data will be
1068 					     relayed to (PPPoE relaying) */
1069 };
1070+struct pptp_opt {
1071+	struct pptp_addr	src_addr;
1072+	struct pptp_addr	dst_addr;
1073+	int timeout;
1074+	int window;
1075+	__u32 ack_sent, ack_recv;
1076+	__u32 seq_sent, seq_recv;
1077+	int pause:1;
1078+	int proc:1;
1079+	spinlock_t skb_buf_lock;
1080+	struct sk_buff_head skb_buf;
1081+	struct work_struct ack_work;  //send ack work
1082+	struct work_struct buf_work; //check bufferd packets work
1083+	struct gre_statistics *stat;
1084+};
1085 
1086 #include <net/sock.h>
1087 
1088@@ -128,6 +156,7 @@ struct pppox_sock {
1089 	struct pppox_sock	*next;	  /* for hash table */
1090 	union {
1091 		struct pppoe_opt pppoe;
1092+		struct pptp_opt pptp;
1093 	} proto;
1094 	unsigned short		num;
1095 };
1096diff -uprN linux-2.6.18.orig/MAINTAINERS linux-2.6.18.pptp/MAINTAINERS
1097--- linux-2.6.18.orig/MAINTAINERS	2006-10-30 13:20:47.000000000 +0300
1098+++ linux-2.6.18.pptp/MAINTAINERS	2006-10-30 13:21:45.000000000 +0300
1099@@ -2325,6 +2325,11 @@ P:	Michal Ostrowski
1100 M:	mostrows@speakeasy.net
1101 S:	Maintained
1102 
1103+PPTP
1104+P:      Dmitry Kozlov
1105+M:      xeb@mail.ru
1106+S:      Maintained
1107+
1108 PREEMPTIBLE KERNEL
1109 P:	Robert Love
1110 M:	rml@tech9.net
1111