2 * Point-to-Point Tunneling Protocol for Linux
4 * Authors: Kozlov D. (xeb@mail.ru)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/string.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <linux/netdevice.h>
19 #include <linux/net.h>
20 #include <linux/skbuff.h>
21 #include <linux/init.h>
22 #include <linux/ppp_channel.h>
23 #include <linux/ppp_defs.h>
24 #include <linux/if_pppox.h>
25 #include <linux/if_ppp.h>
26 #include <linux/notifier.h>
27 #include <linux/file.h>
30 #include <linux/netfilter.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/version.h>
33 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
34 #include <asm/semaphore.h>
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
38 #include <asm/bitops.h>
42 #include <net/protocol.h>
45 #include <net/route.h>
47 #include <asm/uaccess.h>
56 #define PPTP_DRIVER_VERSION "0.8.5"
58 static int log_level
=0;
59 static int log_packets
=10;
61 #define MAX_CALLID 65535
62 #define PPP_LCP_ECHOREQ 0x09
63 #define PPP_LCP_ECHOREP 0x0A
65 static unsigned long *callid_bitmap
=NULL
;
66 static struct pppox_sock
**callid_sock
=NULL
;
68 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
70 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
71 #define INIT_TIMER(_timer,_routine,_data) \
73 (_timer)->function=_routine; \
74 (_timer)->data=_data; \
78 static inline void *kzalloc(size_t size
,int gfp
)
80 void *p
=kmalloc(size
,gfp
);
86 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,20)
87 static inline void nf_reset(struct sk_buff
*skb
)
89 #ifdef CONFIG_NETFILTER
90 nf_conntrack_put(skb
->nfct
);
92 #ifdef CONFIG_NETFILTER_DEBUG
100 #if 0 // already defined for mips32
102 * __ffs - find first bit in word.
103 * @word: The word to search
105 * Undefined if no bit exists, so code should check against 0 first.
107 static inline unsigned long __ffs(unsigned long word
)
111 #if BITS_PER_LONG == 64
112 if ((word
& 0xffffffff) == 0) {
117 if ((word
& 0xffff) == 0) {
121 if ((word
& 0xff) == 0) {
125 if ((word
& 0xf) == 0) {
129 if ((word
& 0x3) == 0) {
133 if ((word
& 0x1) == 0)
138 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
140 * Find the next set bit in a memory region.
142 static unsigned long find_next_bit(const unsigned long *addr
, unsigned long size
,
143 unsigned long offset
)
145 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
146 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
152 offset
%= BITS_PER_LONG
;
155 tmp
&= (~0UL << offset
);
156 if (size
< BITS_PER_LONG
)
160 size
-= BITS_PER_LONG
;
161 result
+= BITS_PER_LONG
;
163 while (size
& ~(BITS_PER_LONG
-1)) {
166 result
+= BITS_PER_LONG
;
167 size
-= BITS_PER_LONG
;
174 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
175 if (tmp
== 0UL) /* Are any bits set? */
176 return result
+ size
; /* Nope. */
178 return result
+ __ffs(tmp
);
183 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
184 static rwlock_t chan_lock
=RW_LOCK_UNLOCKED
;
185 #define SK_STATE(sk) (sk)->state
187 static DECLARE_MUTEX(chan_lock
);
188 #define SK_STATE(sk) (sk)->sk_state
191 static int pptp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
);
192 static int pptp_ppp_ioctl(struct ppp_channel
*chan
, unsigned int cmd
,
194 static int pptp_rcv_core(struct sock
*sk
,struct sk_buff
*skb
);
196 static struct ppp_channel_ops pptp_chan_ops
= {
197 .start_xmit
= pptp_xmit
,
198 .ioctl
=pptp_ppp_ioctl
,
202 #define MISSING_WINDOW 20
203 #define WRAPPED( curseq, lastseq) \
204 ((((curseq) & 0xffffff00) == 0) && \
205 (((lastseq) & 0xffffff00 ) == 0xffffff00))
207 /* gre header structure: -------------------------------------------- */
209 #define PPTP_GRE_PROTO 0x880B
210 #define PPTP_GRE_VER 0x1
212 #define PPTP_GRE_FLAG_C 0x80
213 #define PPTP_GRE_FLAG_R 0x40
214 #define PPTP_GRE_FLAG_K 0x20
215 #define PPTP_GRE_FLAG_S 0x10
216 #define PPTP_GRE_FLAG_A 0x80
218 #define PPTP_GRE_IS_C(f) ((f)&PPTP_GRE_FLAG_C)
219 #define PPTP_GRE_IS_R(f) ((f)&PPTP_GRE_FLAG_R)
220 #define PPTP_GRE_IS_K(f) ((f)&PPTP_GRE_FLAG_K)
221 #define PPTP_GRE_IS_S(f) ((f)&PPTP_GRE_FLAG_S)
222 #define PPTP_GRE_IS_A(f) ((f)&PPTP_GRE_FLAG_A)
224 struct pptp_gre_header
{
225 u8 flags
; /* bitfield */
226 u8 ver
; /* should be PPTP_GRE_VER (enhanced GRE) */
227 u16 protocol
; /* should be PPTP_GRE_PROTO (ppp-encaps) */
228 u16 payload_len
; /* size of ppp payload, not inc. gre header */
229 u16 call_id
; /* peer's call_id for this session */
230 u32 seq
; /* sequence number. Present if S==1 */
231 u32 ack
; /* seq number of highest packet recieved by */
232 /* sender in this session */
234 #define PPTP_HEADER_OVERHEAD (2+sizeof(struct pptp_gre_header))
236 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
237 static struct pppox_sock
* lookup_chan(u16 call_id
, u32 s_addr
)
239 static struct pppox_sock
* lookup_chan(u16 call_id
, __be32 s_addr
)
242 struct pppox_sock
*sock
;
243 struct pptp_opt
*opt
;
245 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
248 read_lock(&chan_lock
);
250 sock
= rcu_dereference(callid_sock
[call_id
]);
252 opt
=&sock
->proto
.pptp
;
253 if (opt
->dst_addr
.sin_addr
.s_addr
!=s_addr
) sock
=NULL
;
254 else sock_hold(sk_pppox(sock
));
256 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
259 read_unlock(&chan_lock
);
265 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
266 static int lookup_chan_dst(u16 call_id
, u32 d_addr
)
268 static int lookup_chan_dst(u16 call_id
, __be32 d_addr
)
271 struct pppox_sock
*sock
;
272 struct pptp_opt
*opt
;
275 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
278 read_lock(&chan_lock
);
280 for(i
=find_next_bit(callid_bitmap
,MAX_CALLID
,1); i
<MAX_CALLID
; i
=find_next_bit(callid_bitmap
,MAX_CALLID
,i
+1)){
282 opt
=&sock
->proto
.pptp
;
283 if (opt
->dst_addr
.call_id
==call_id
&& opt
->dst_addr
.sin_addr
.s_addr
==d_addr
) break;
285 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
288 read_unlock(&chan_lock
);
294 static int add_chan(struct pppox_sock
*sock
)
296 static int call_id
=0;
299 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
303 write_lock_bh(&chan_lock
);
306 if (!sock
->proto
.pptp
.src_addr
.call_id
)
308 call_id
=find_next_zero_bit(callid_bitmap
,MAX_CALLID
,call_id
+1);
309 if (call_id
==MAX_CALLID
)
310 call_id
=find_next_zero_bit(callid_bitmap
,MAX_CALLID
,1);
311 sock
->proto
.pptp
.src_addr
.call_id
=call_id
;
313 else if (test_bit(sock
->proto
.pptp
.src_addr
.call_id
,callid_bitmap
))
316 set_bit(sock
->proto
.pptp
.src_addr
.call_id
,callid_bitmap
);
317 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
318 rcu_assign_pointer(callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
],sock
);
320 callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
]=sock
;
325 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
328 write_unlock_bh(&chan_lock
);
334 static void del_chan(struct pppox_sock
*sock
)
336 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
340 write_lock_bh(&chan_lock
);
342 clear_bit(sock
->proto
.pptp
.src_addr
.call_id
,callid_bitmap
);
343 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
344 rcu_assign_pointer(callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
],NULL
);
347 callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
]=NULL
;
348 write_unlock_bh(&chan_lock
);
352 static int pptp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
354 struct sock
*sk
= (struct sock
*) chan
->private;
355 struct pppox_sock
*po
= pppox_sk(sk
);
356 struct pptp_opt
*opt
=&po
->proto
.pptp
;
357 struct pptp_gre_header
*hdr
;
358 unsigned int header_len
=sizeof(*hdr
);
366 struct rtable
*rt
; /* Route to the other host */
367 struct net_device
*tdev
; /* Device to other host */
368 struct iphdr
*iph
; /* Our new IP header */
369 int max_headroom
; /* The extra header space needed */
371 if (SK_STATE(sk_pppox(po
)) & PPPOX_DEAD
)
374 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
376 struct rt_key key
= {
377 .dst
=opt
->dst_addr
.sin_addr
.s_addr
,
378 .src
=opt
->src_addr
.sin_addr
.s_addr
,
381 if ((err
=ip_route_output_key(&rt
, &key
))) {
387 struct flowi fl
= { .oif
= 0,
389 { .daddr
= opt
->dst_addr
.sin_addr
.s_addr
,
390 .saddr
= opt
->src_addr
.sin_addr
.s_addr
,
391 .tos
= RT_TOS(0) } },
392 .proto
= IPPROTO_GRE
};
393 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
394 if ((err
=ip_route_output_key(&rt
, &fl
))) {
396 if ((err
=ip_route_output_key(&init_net
,&rt
, &fl
))) {
402 tdev
= rt
->u
.dst
.dev
;
404 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
405 max_headroom
= ((tdev
->hard_header_len
+15)&~15) + sizeof(*iph
)+sizeof(*hdr
)+2;
407 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(*iph
)+sizeof(*hdr
)+2;
411 if (skb_headroom(skb
) < max_headroom
|| skb_shared(skb
)||
412 (skb_cloned(skb
) && !skb_clone_writable(skb
, 0))) {
413 struct sk_buff
*new_skb
= skb_realloc_headroom(skb
, max_headroom
);
419 skb_set_owner_w(new_skb
, skb
->sk
);
425 islcp
=((data
[0] << 8) + data
[1])== PPP_LCP
&& 1 <= data
[2] && data
[2] <= 7;
427 /* compress protocol field */
428 if ((opt
->ppp_flags
& SC_COMP_PROT
) && data
[0]==0 && !islcp
)
432 * Put in the address/control bytes if necessary
434 if ((opt
->ppp_flags
& SC_COMP_AC
) == 0 || islcp
) {
435 data
=skb_push(skb
,2);
436 data
[0]=PPP_ALLSTATIONS
;
442 seq_recv
= opt
->seq_recv
;
444 if (opt
->ack_sent
== seq_recv
) header_len
-=sizeof(hdr
->ack
);
446 // Push down and install GRE header
447 skb_push(skb
,header_len
);
448 hdr
=(struct pptp_gre_header
*)(skb
->data
);
450 hdr
->flags
= PPTP_GRE_FLAG_K
;
451 hdr
->ver
= PPTP_GRE_VER
;
452 hdr
->protocol
= htons(PPTP_GRE_PROTO
);
453 hdr
->call_id
= htons(opt
->dst_addr
.call_id
);
455 hdr
->flags
|= PPTP_GRE_FLAG_S
;
456 hdr
->seq
= htonl(++opt
->seq_sent
);
458 if (log_level
>=3 && opt
->seq_sent
<=log_packets
)
459 printk(KERN_INFO
"PPTP[%i]: send packet: seq=%i",opt
->src_addr
.call_id
,opt
->seq_sent
);
461 if (opt
->ack_sent
!= seq_recv
) {
462 /* send ack with this message */
463 hdr
->ver
|= PPTP_GRE_FLAG_A
;
464 hdr
->ack
= htonl(seq_recv
);
465 opt
->ack_sent
= seq_recv
;
467 if (log_level
>=3 && opt
->seq_sent
<=log_packets
)
468 printk(" ack=%i",seq_recv
);
471 hdr
->payload_len
= htons(len
);
473 if (log_level
>=3 && opt
->seq_sent
<=log_packets
)
478 * Push down and install the IP header.
481 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
482 skb_reset_transport_header(skb
);
483 skb_push(skb
, sizeof(*iph
));
484 skb_reset_network_header(skb
);
485 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
486 skb
->transport_header
= skb
->network_header
;
487 skb_push(skb
, sizeof(*iph
));
488 skb_reset_network_header(skb
);
490 skb
->h
.raw
= skb
->nh
.raw
;
491 skb
->nh
.raw
= skb_push(skb
, sizeof(*iph
));
493 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
494 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
495 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
499 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
505 iph
->ihl
= sizeof(struct iphdr
) >> 2;
506 if (ip_dont_fragment(sk
, &rt
->u
.dst
))
507 iph
->frag_off
= htons(IP_DF
);
510 iph
->protocol
= IPPROTO_GRE
;
512 iph
->daddr
= rt
->rt_dst
;
513 iph
->saddr
= rt
->rt_src
;
514 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
515 iph
->ttl
= sk
->protinfo
.af_inet
.ttl
;
517 iph
->ttl
= dst_metric(&rt
->u
.dst
, RTAX_HOPLIMIT
);
519 iph
->tot_len
= htons(skb
->len
);
521 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
523 skb_dst_set(skb
,&rt
->u
.dst
);
525 dst_release(skb
->dst
);
526 skb
->dst
= &rt
->u
.dst
;
531 skb
->ip_summed
= CHECKSUM_NONE
;
532 ip_select_ident(iph
, &rt
->u
.dst
, NULL
);
535 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
536 err
= NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->u
.dst
.dev
, ip_send
);
537 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
538 err
= NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->u
.dst
.dev
, dst_output
);
540 err
= ip_local_out(skb
);
547 static int pptp_rcv_core(struct sock
*sk
,struct sk_buff
*skb
)
549 struct pppox_sock
*po
= pppox_sk(sk
);
550 struct pptp_opt
*opt
=&po
->proto
.pptp
;
551 int headersize
,payload_len
,seq
;
553 struct pptp_gre_header
*header
;
555 if (!(SK_STATE(sk
) & PPPOX_CONNECTED
)) {
556 if (sock_queue_rcv_skb(sk
, skb
))
558 return NET_RX_SUCCESS
;
561 header
= (struct pptp_gre_header
*)(skb
->data
);
563 /* test if acknowledgement present */
564 if (PPTP_GRE_IS_A(header
->ver
)){
565 u32 ack
= (PPTP_GRE_IS_S(header
->flags
))?
566 header
->ack
:header
->seq
; /* ack in different place if S = 0 */
570 if (ack
> opt
->ack_recv
) opt
->ack_recv
= ack
;
571 /* also handle sequence number wrap-around */
572 if (WRAPPED(ack
,opt
->ack_recv
)) opt
->ack_recv
= ack
;
575 /* test if payload present */
576 if (!PPTP_GRE_IS_S(header
->flags
)){
580 headersize
= sizeof(*header
);
581 payload_len
= ntohs(header
->payload_len
);
582 seq
= ntohl(header
->seq
);
584 /* no ack present? */
585 if (!PPTP_GRE_IS_A(header
->ver
)) headersize
-= sizeof(header
->ack
);
586 /* check for incomplete packet (length smaller than expected) */
587 if (skb
->len
- headersize
< payload_len
){
590 printk(KERN_INFO
"PPTP: discarding truncated packet (expected %d, got %d bytes)\n",
591 payload_len
, skb
->len
- headersize
);
596 payload
=skb
->data
+headersize
;
597 /* check for expected sequence number */
598 if ( seq
< opt
->seq_recv
+ 1 || WRAPPED(opt
->seq_recv
, seq
) ){
599 if ( (payload
[0] == PPP_ALLSTATIONS
) && (payload
[1] == PPP_UI
) &&
600 (PPP_PROTOCOL(payload
) == PPP_LCP
) &&
601 ((payload
[4] == PPP_LCP_ECHOREQ
) || (payload
[4] == PPP_LCP_ECHOREP
)) ){
604 printk(KERN_INFO
"PPTP[%i]: allowing old LCP Echo packet %d (expecting %d)\n", opt
->src_addr
.call_id
,
605 seq
, opt
->seq_recv
+ 1);
611 printk(KERN_INFO
"PPTP[%i]: discarding duplicate or old packet %d (expecting %d)\n",opt
->src_addr
.call_id
,
612 seq
, opt
->seq_recv
+ 1);
618 if ( log_level
>= 3 && opt
->seq_sent
<=log_packets
)
619 printk(KERN_INFO
"PPTP[%i]: accepting packet %d size=%i (%02x %02x %02x %02x %02x %02x)\n",opt
->src_addr
.call_id
, seq
,payload_len
,
628 skb_pull(skb
,headersize
);
630 if (payload
[0] == PPP_ALLSTATIONS
&& payload
[1] == PPP_UI
){
631 /* chop off address/control */
637 if ((*skb
->data
) & 1){
638 /* protocol is compressed */
639 skb_push(skb
, 1)[0] = 0;
642 skb
->ip_summed
=CHECKSUM_NONE
;
643 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,21)
644 skb_set_network_header(skb
,skb
->head
-skb
->data
);
646 ppp_input(&po
->chan
,skb
);
648 return NET_RX_SUCCESS
;
655 static int pptp_rcv(struct sk_buff
*skb
)
657 struct pppox_sock
*po
;
658 struct pptp_gre_header
*header
;
660 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
665 if (skb
->pkt_type
!= PACKET_HOST
)
668 /*if (!pskb_may_pull(skb, 12))
671 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
677 header
= (struct pptp_gre_header
*)skb
->data
;
679 if ( /* version should be 1 */
680 ((header
->ver
& 0x7F) != PPTP_GRE_VER
) ||
681 /* PPTP-GRE protocol for PPTP */
682 (ntohs(header
->protocol
) != PPTP_GRE_PROTO
)||
683 /* flag C should be clear */
684 PPTP_GRE_IS_C(header
->flags
) ||
685 /* flag R should be clear */
686 PPTP_GRE_IS_R(header
->flags
) ||
687 /* flag K should be set */
688 (!PPTP_GRE_IS_K(header
->flags
)) ||
689 /* routing and recursion ctrl = 0 */
690 ((header
->flags
&0xF) != 0)){
691 /* if invalid, discard this packet */
693 printk(KERN_INFO
"PPTP: Discarding GRE: %X %X %X %X %X %X\n",
694 header
->ver
&0x7F, ntohs(header
->protocol
),
695 PPTP_GRE_IS_C(header
->flags
),
696 PPTP_GRE_IS_R(header
->flags
),
697 PPTP_GRE_IS_K(header
->flags
),
698 header
->flags
& 0xF);
703 if ((po
=lookup_chan(htons(header
->call_id
),iph
->saddr
))) {
704 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
706 skb_dst_set(skb
,NULL
);
708 dst_release(skb
->dst
);
712 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
715 /* Socket state is unknown, must put skb into backlog. */
716 if (sk
->lock
.users
!= 0) {
717 sk_add_backlog(sk
, skb
);
718 ret
= NET_RX_SUCCESS
;
720 ret
= pptp_rcv_core(sk
, skb
);
726 #else /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
728 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,19)
729 return sk_receive_skb(sk_pppox(po
), skb
);
731 return sk_receive_skb(sk_pppox(po
), skb
, 0);
734 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
738 printk(KERN_INFO
"PPTP: Discarding packet from unknown call_id %i\n",htons(header
->call_id
));
747 static int pptp_bind(struct socket
*sock
,struct sockaddr
*uservaddr
,int sockaddr_len
)
749 struct sock
*sk
= sock
->sk
;
750 struct sockaddr_pppox
*sp
= (struct sockaddr_pppox
*) uservaddr
;
751 struct pppox_sock
*po
= pppox_sk(sk
);
752 struct pptp_opt
*opt
=&po
->proto
.pptp
;
757 printk(KERN_INFO
"PPTP: bind: addr=%X call_id=%i\n",sp
->sa_addr
.pptp
.sin_addr
.s_addr
,
758 sp
->sa_addr
.pptp
.call_id
);
762 opt
->src_addr
=sp
->sa_addr
.pptp
;
770 printk(KERN_INFO
"PPTP: using call_id %i\n",opt
->src_addr
.call_id
);
777 static int pptp_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
778 int sockaddr_len
, int flags
)
780 struct sock
*sk
= sock
->sk
;
781 struct sockaddr_pppox
*sp
= (struct sockaddr_pppox
*) uservaddr
;
782 struct pppox_sock
*po
= pppox_sk(sk
);
783 struct pptp_opt
*opt
= &po
->proto
.pptp
;
784 struct rtable
*rt
; /* Route to the other host */
787 if (sp
->sa_protocol
!= PX_PROTO_PPTP
)
792 printk(KERN_INFO
"PPTP[%i]: connect: addr=%X call_id=%i\n",opt
->src_addr
.call_id
,
793 sp
->sa_addr
.pptp
.sin_addr
.s_addr
,sp
->sa_addr
.pptp
.call_id
);
796 if (lookup_chan_dst(sp
->sa_addr
.pptp
.call_id
,sp
->sa_addr
.pptp
.sin_addr
.s_addr
))
800 /* Check for already bound sockets */
801 if (SK_STATE(sk
) & PPPOX_CONNECTED
){
806 /* Check for already disconnected sockets, on attempts to disconnect */
807 if (SK_STATE(sk
) & PPPOX_DEAD
){
812 if (!opt
->src_addr
.sin_addr
.s_addr
|| !sp
->sa_addr
.pptp
.sin_addr
.s_addr
){
818 po
->chan
.ops
=&pptp_chan_ops
;
820 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
822 struct rt_key key
= {
823 .dst
=opt
->dst_addr
.sin_addr
.s_addr
,
824 .src
=opt
->src_addr
.sin_addr
.s_addr
,
827 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
828 if (ip_route_output_key(&rt
, &key
)) {
830 if (ip_route_output_key(&init_net
, &rt
, &key
)) {
832 error
= -EHOSTUNREACH
;
840 { .daddr
= opt
->dst_addr
.sin_addr
.s_addr
,
841 .saddr
= opt
->src_addr
.sin_addr
.s_addr
,
842 .tos
= RT_CONN_FLAGS(sk
) } },
843 .proto
= IPPROTO_GRE
};
844 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
845 security_sk_classify_flow(sk
, &fl
);
847 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
848 if (ip_route_output_key(&rt
, &fl
)){
850 if (ip_route_output_key(&init_net
, &rt
, &fl
)){
852 error
= -EHOSTUNREACH
;
855 sk_setup_caps(sk
, &rt
->u
.dst
);
858 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
859 po
->chan
.mtu
=PPP_MTU
;
861 po
->chan
.mtu
=dst_mtu(&rt
->u
.dst
);
862 if (!po
->chan
.mtu
) po
->chan
.mtu
=PPP_MTU
;
865 po
->chan
.mtu
-=PPTP_HEADER_OVERHEAD
;
867 po
->chan
.hdrlen
=2+sizeof(struct pptp_gre_header
);
868 error
= ppp_register_channel(&po
->chan
);
870 printk(KERN_ERR
"PPTP: failed to register PPP channel (%d)\n",error
);
874 opt
->dst_addr
=sp
->sa_addr
.pptp
;
875 SK_STATE(sk
) = PPPOX_CONNECTED
;
882 static int pptp_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
883 int *usockaddr_len
, int peer
)
885 int len
= sizeof(struct sockaddr_pppox
);
886 struct sockaddr_pppox sp
;
888 sp
.sa_family
= AF_PPPOX
;
889 sp
.sa_protocol
= PX_PROTO_PPTP
;
890 sp
.sa_addr
.pptp
=pppox_sk(sock
->sk
)->proto
.pptp
.src_addr
;
892 memcpy(uaddr
, &sp
, len
);
894 *usockaddr_len
= len
;
899 static int pptp_release(struct socket
*sock
)
901 struct sock
*sk
= sock
->sk
;
902 struct pppox_sock
*po
;
903 struct pptp_opt
*opt
;
911 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
914 if (sock_flag(sk
, SOCK_DEAD
))
925 pppox_unbind_sock(sk
);
926 SK_STATE(sk
) = PPPOX_DEAD
;
930 printk(KERN_INFO
"PPTP[%i]: release\n",opt
->src_addr
.call_id
);
943 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
944 static struct proto pptp_sk_proto
= {
946 .owner
= THIS_MODULE
,
947 .obj_size
= sizeof(struct pppox_sock
),
951 static struct proto_ops pptp_ops
= {
953 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
954 .owner
= THIS_MODULE
,
956 .release
= pptp_release
,
958 .connect
= pptp_connect
,
959 .socketpair
= sock_no_socketpair
,
960 .accept
= sock_no_accept
,
961 .getname
= pptp_getname
,
962 .poll
= sock_no_poll
,
963 .listen
= sock_no_listen
,
964 .shutdown
= sock_no_shutdown
,
965 .setsockopt
= sock_no_setsockopt
,
966 .getsockopt
= sock_no_getsockopt
,
967 .sendmsg
= sock_no_sendmsg
,
968 .recvmsg
= sock_no_recvmsg
,
969 .mmap
= sock_no_mmap
,
970 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
971 .ioctl
= pppox_ioctl
,
976 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
977 static void pptp_sock_destruct(struct sock
*sk
)
979 skb_queue_purge(&sk
->receive_queue
);
980 if (!(SK_STATE(sk
) & PPPOX_DEAD
)) {
981 del_chan(pppox_sk(sk
));
982 pppox_unbind_sock(sk
);
984 if (sk
->protinfo
.destruct_hook
)
985 kfree(sk
->protinfo
.destruct_hook
);
990 static int pptp_create(struct socket
*sock
)
994 struct pppox_sock
*po
;
995 struct pptp_opt
*opt
;
999 sk
= sk_alloc(PF_PPPOX
, GFP_KERNEL
, 1);
1003 sock_init_data(sock
, sk
);
1005 sock
->state
= SS_UNCONNECTED
;
1006 sock
->ops
= &pptp_ops
;
1008 //sk->sk_backlog_rcv = pppoe_rcv_core;
1009 sk
->state
= PPPOX_NONE
;
1010 sk
->type
= SOCK_STREAM
;
1011 sk
->family
= PF_PPPOX
;
1012 sk
->protocol
= PX_PROTO_PPTP
;
1014 sk
->protinfo
.pppox
=kzalloc(sizeof(struct pppox_sock
),GFP_KERNEL
);
1015 sk
->destruct
=pptp_sock_destruct
;
1016 sk
->protinfo
.destruct_hook
=sk
->protinfo
.pppox
;
1020 opt
=&po
->proto
.pptp
;
1022 opt
->seq_sent
=0; opt
->seq_recv
=0;
1023 opt
->ack_recv
=0; opt
->ack_sent
=0;
1030 static void pptp_sock_destruct(struct sock
*sk
)
1032 if (!(SK_STATE(sk
) & PPPOX_DEAD
)){
1033 del_chan(pppox_sk(sk
));
1034 pppox_unbind_sock(sk
);
1036 skb_queue_purge(&sk
->sk_receive_queue
);
1038 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1039 static int pptp_create(struct socket
*sock
)
1041 static int pptp_create(struct net
*net
, struct socket
*sock
)
1044 int error
= -ENOMEM
;
1046 struct pppox_sock
*po
;
1047 struct pptp_opt
*opt
;
1049 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1050 sk
= sk_alloc(PF_PPPOX
, GFP_KERNEL
, &pptp_sk_proto
, 1);
1052 sk
= sk_alloc(net
,PF_PPPOX
, GFP_KERNEL
, &pptp_sk_proto
);
1057 sock_init_data(sock
, sk
);
1059 sock
->state
= SS_UNCONNECTED
;
1060 sock
->ops
= &pptp_ops
;
1062 sk
->sk_backlog_rcv
= pptp_rcv_core
;
1063 sk
->sk_state
= PPPOX_NONE
;
1064 sk
->sk_type
= SOCK_STREAM
;
1065 sk
->sk_family
= PF_PPPOX
;
1066 sk
->sk_protocol
= PX_PROTO_PPTP
;
1067 sk
->sk_destruct
= pptp_sock_destruct
;
1070 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1073 opt
=&po
->proto
.pptp
;
1075 opt
->seq_sent
=0; opt
->seq_recv
=0;
1076 opt
->ack_recv
=0; opt
->ack_sent
=0;
1085 static int pptp_ppp_ioctl(struct ppp_channel
*chan
, unsigned int cmd
,
1088 struct sock
*sk
= (struct sock
*) chan
->private;
1089 struct pppox_sock
*po
= pppox_sk(sk
);
1090 struct pptp_opt
*opt
=&po
->proto
.pptp
;
1091 void __user
*argp
= (void __user
*)arg
;
1092 int __user
*p
= argp
;
1098 val
= opt
->ppp_flags
;
1099 if (put_user(val
, p
))
1104 if (get_user(val
, p
))
1106 opt
->ppp_flags
= val
& ~SC_RCV_BITS
;
1117 static struct pppox_proto pppox_pptp_proto
= {
1118 .create
= pptp_create
,
1119 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
1120 .owner
= THIS_MODULE
,
1125 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1126 static struct inet_protocol net_pptp_protocol
= {
1127 .handler
= pptp_rcv
,
1128 //.err_handler = pptp_err,
1129 .protocol
= IPPROTO_GRE
,
1134 static struct gre_protocol gre_pptp_protocol
= {
1136 static struct net_protocol net_pptp_protocol
= {
1138 .handler
= pptp_rcv
,
1139 //.err_handler = pptp_err,
1143 static int __init
pptp_init_module(void)
1146 printk(KERN_INFO
"PPTP driver version " PPTP_DRIVER_VERSION
"\n");
1148 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1149 inet_add_protocol(&net_pptp_protocol
);
1152 if (gre_add_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
) < 0) {
1154 if (inet_add_protocol(&net_pptp_protocol
, IPPROTO_GRE
) < 0) {
1156 printk(KERN_INFO
"PPTP: can't add protocol\n");
1161 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1162 err
= proto_register(&pptp_sk_proto
, 0);
1164 printk(KERN_INFO
"PPTP: can't register sk_proto\n");
1165 goto out_inet_del_protocol
;
1169 err
= register_pppox_proto(PX_PROTO_PPTP
, &pppox_pptp_proto
);
1171 printk(KERN_INFO
"PPTP: can't register pppox_proto\n");
1172 goto out_unregister_sk_proto
;
1176 //assuming PAGESIZE is 4096 bytes
1177 callid_bitmap
=(unsigned long*)__get_free_pages(GFP_KERNEL
,1);
1178 memset(callid_bitmap
,0,PAGE_SIZE
<<1);
1180 #if (BITS_PER_LONG == 32)
1181 callid_sock
=(struct pppox_sock
**)__get_free_pages(GFP_KERNEL
,6);
1182 memset(callid_sock
,0,PAGE_SIZE
<<6);
1183 #elif (BITS_PER_LONG == 64)
1184 callid_sock
=(struct pppox_sock
**)__get_free_pages(GFP_KERNEL
,7);
1185 memset(callid_sock
,0,PAGE_SIZE
<<7);
1187 #error unknown size of LONG
1192 out_unregister_sk_proto
:
1193 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1194 proto_unregister(&pptp_sk_proto
);
1197 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1198 out_inet_del_protocol
:
1201 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1202 inet_del_protocol(&net_pptp_protocol
);
1205 gre_del_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
);
1207 inet_del_protocol(&net_pptp_protocol
, IPPROTO_GRE
);
1213 static void __exit
pptp_exit_module(void)
1215 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1216 flush_scheduled_tasks();
1218 flush_scheduled_work();
1221 unregister_pppox_proto(PX_PROTO_PPTP
);
1222 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1223 inet_del_protocol(&net_pptp_protocol
);
1225 proto_unregister(&pptp_sk_proto
);
1227 gre_del_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
);
1229 inet_del_protocol(&net_pptp_protocol
, IPPROTO_GRE
);
1232 if (callid_bitmap
) free_pages((unsigned long)callid_bitmap
,1);
1234 #if (BITS_PER_LONG == 32)
1235 free_pages((unsigned long)callid_sock
,6);
1236 #elif (BITS_PER_LONG == 64)
1237 free_pages((unsigned long)callid_sock
,7);
1242 module_init(pptp_init_module
);
1243 module_exit(pptp_exit_module
);
1245 MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol for Linux");
1246 MODULE_AUTHOR("Kozlov D. (xeb@mail.ru)");
1247 MODULE_LICENSE("GPL");
1249 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1250 MODULE_PARM(log_level
,"i");
1251 MODULE_PARM(log_packets
,"i");
1253 module_param(log_level
,int,0);
1254 module_param(log_packets
,int,0);
1256 MODULE_PARM_DESC(log_level
,"Logging level (default=0)");