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