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/vmalloc.h>
18 #include <linux/errno.h>
19 #include <linux/netdevice.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/init.h>
23 #include <linux/ppp_channel.h>
24 #include <linux/ppp_defs.h>
25 #include <linux/if_pppox.h>
26 #include <linux/if_ppp.h>
27 #include <linux/notifier.h>
28 #include <linux/file.h>
31 #include <linux/netfilter.h>
32 #include <linux/netfilter_ipv4.h>
33 #include <linux/version.h>
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
36 #include <asm/bitops.h>
40 #include <net/protocol.h>
43 #include <net/route.h>
45 #include <asm/uaccess.h>
48 //#define CONFIG_NET_IPGRE_DEMUX
49 #define HAVE_FIND_NEXT_BIT
50 #define HAVE_SKB_CLONE_WRITABLE
52 #if defined(CONFIG_NET_IPGRE_DEMUX) || defined(CONFIG_NET_IPGRE_DEMUX_MODULE)
56 #define PPTP_DRIVER_VERSION "0.8.5"
59 static int log_level
=0;
60 static int log_packets
=10;
63 #define MAX_CALLID 65535
64 #define PPP_LCP_ECHOREQ 0x09
65 #define PPP_LCP_ECHOREP 0x0A
67 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,20)
68 #define BITS_TO_LONGS(bits) \
69 (((bits)+BITS_PER_LONG-1)/BITS_PER_LONG)
70 #define DECLARE_BITMAP(name,bits) \
71 unsigned long name[BITS_TO_LONGS(bits)]
72 #define CLEAR_BITMAP(name,bits) \
73 memset(name, 0, BITS_TO_LONGS(bits)*sizeof(unsigned long))
76 static DECLARE_BITMAP(callid_bitmap
, MAX_CALLID
+ 1);
77 static struct pppox_sock
**callid_sock
;
79 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
81 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
82 #define INIT_TIMER(_timer,_routine,_data) \
84 (_timer)->function=_routine; \
85 (_timer)->data=_data; \
89 static inline void *kzalloc(size_t size
,int gfp
)
91 void *p
=kmalloc(size
,gfp
);
96 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,20)
97 static inline void nf_reset(struct sk_buff
*skb
)
99 #ifdef CONFIG_NETFILTER
100 nf_conntrack_put(skb
->nfct
);
102 #ifdef CONFIG_NETFILTER_DEBUG
111 #ifndef HAVE_FIND_NEXT_BIT
113 * __ffs - find first bit in word.
114 * @word: The word to search
116 * Undefined if no bit exists, so code should check against 0 first.
118 static inline unsigned long __ffs(unsigned long word
)
122 #if BITS_PER_LONG == 64
123 if ((word
& 0xffffffff) == 0) {
128 if ((word
& 0xffff) == 0) {
132 if ((word
& 0xff) == 0) {
136 if ((word
& 0xf) == 0) {
140 if ((word
& 0x3) == 0) {
144 if ((word
& 0x1) == 0)
149 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
151 * Find the next set bit in a memory region.
153 static unsigned long find_next_bit(const unsigned long *addr
, unsigned long size
,
154 unsigned long offset
)
156 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
157 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
163 offset
%= BITS_PER_LONG
;
166 tmp
&= (~0UL << offset
);
167 if (size
< BITS_PER_LONG
)
171 size
-= BITS_PER_LONG
;
172 result
+= BITS_PER_LONG
;
174 while (size
& ~(BITS_PER_LONG
-1)) {
177 result
+= BITS_PER_LONG
;
178 size
-= BITS_PER_LONG
;
185 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
186 if (tmp
== 0UL) /* Are any bits set? */
187 return result
+ size
; /* Nope. */
189 return result
+ __ffs(tmp
);
194 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
195 static rwlock_t chan_lock
=RW_LOCK_UNLOCKED
;
196 #define SK_STATE(sk) (sk)->state
198 static DEFINE_SPINLOCK(chan_lock
);
199 #define SK_STATE(sk) (sk)->sk_state
202 static int pptp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
);
203 static int pptp_ppp_ioctl(struct ppp_channel
*chan
, unsigned int cmd
,
205 static int pptp_rcv_core(struct sock
*sk
,struct sk_buff
*skb
);
207 static struct ppp_channel_ops pptp_chan_ops
= {
208 .start_xmit
= pptp_xmit
,
209 .ioctl
=pptp_ppp_ioctl
,
213 #define MISSING_WINDOW 20
214 #define WRAPPED( curseq, lastseq) \
215 ((((curseq) & 0xffffff00) == 0) && \
216 (((lastseq) & 0xffffff00 ) == 0xffffff00))
218 /* gre header structure: -------------------------------------------- */
220 #define PPTP_GRE_PROTO 0x880B
221 #define PPTP_GRE_VER 0x1
223 #define PPTP_GRE_FLAG_C 0x80
224 #define PPTP_GRE_FLAG_R 0x40
225 #define PPTP_GRE_FLAG_K 0x20
226 #define PPTP_GRE_FLAG_S 0x10
227 #define PPTP_GRE_FLAG_A 0x80
229 #define PPTP_GRE_IS_C(f) ((f)&PPTP_GRE_FLAG_C)
230 #define PPTP_GRE_IS_R(f) ((f)&PPTP_GRE_FLAG_R)
231 #define PPTP_GRE_IS_K(f) ((f)&PPTP_GRE_FLAG_K)
232 #define PPTP_GRE_IS_S(f) ((f)&PPTP_GRE_FLAG_S)
233 #define PPTP_GRE_IS_A(f) ((f)&PPTP_GRE_FLAG_A)
235 struct pptp_gre_header
{
236 u8 flags
; /* bitfield */
237 u8 ver
; /* should be PPTP_GRE_VER (enhanced GRE) */
238 u16 protocol
; /* should be PPTP_GRE_PROTO (ppp-encaps) */
239 u16 payload_len
; /* size of ppp payload, not inc. gre header */
240 u16 call_id
; /* peer's call_id for this session */
241 u32 seq
; /* sequence number. Present if S==1 */
242 u32 ack
; /* seq number of highest packet recieved by */
243 /* sender in this session */
244 } __attribute__ ((__packed__
));
245 #define PPTP_HEADER_OVERHEAD (2+sizeof(struct pptp_gre_header))
247 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
248 static struct pppox_sock
* lookup_chan(u16 call_id
, u32 s_addr
)
250 static struct pppox_sock
* lookup_chan(u16 call_id
, __be32 s_addr
)
253 struct pppox_sock
*sock
;
254 struct pptp_opt
*opt
;
256 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
258 sock
= rcu_dereference(callid_sock
[call_id
]);
260 read_lock(&chan_lock
);
261 sock
= callid_sock
[call_id
];
264 opt
=&sock
->proto
.pptp
;
265 if (opt
->dst_addr
.sin_addr
.s_addr
!=s_addr
) sock
=NULL
;
266 else sock_hold(sk_pppox(sock
));
268 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
271 read_unlock(&chan_lock
);
277 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
278 static int lookup_chan_dst(u16 call_id
, u32 d_addr
)
280 static int lookup_chan_dst(u16 call_id
, __be32 d_addr
)
283 struct pppox_sock
*sock
;
284 struct pptp_opt
*opt
;
287 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
290 read_lock(&chan_lock
);
292 for(i
= find_next_bit(callid_bitmap
,MAX_CALLID
,1); i
< MAX_CALLID
;
293 i
= find_next_bit(callid_bitmap
, MAX_CALLID
, i
+ 1)){
294 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
295 sock
= rcu_dereference(callid_sock
[i
]);
297 sock
= callid_sock
[i
];
301 opt
= &sock
->proto
.pptp
;
302 if (opt
->dst_addr
.call_id
== call_id
&& opt
->dst_addr
.sin_addr
.s_addr
== d_addr
) break;
304 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
307 read_unlock(&chan_lock
);
313 static int add_chan(struct pppox_sock
*sock
)
315 static int call_id
=0;
318 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
319 spin_lock(&chan_lock
);
321 write_lock_bh(&chan_lock
);
324 if (!sock
->proto
.pptp
.src_addr
.call_id
)
326 call_id
=find_next_zero_bit(callid_bitmap
,MAX_CALLID
,call_id
+1);
327 if (call_id
==MAX_CALLID
)
328 call_id
=find_next_zero_bit(callid_bitmap
,MAX_CALLID
,1);
329 sock
->proto
.pptp
.src_addr
.call_id
=call_id
;
331 else if (test_bit(sock
->proto
.pptp
.src_addr
.call_id
,callid_bitmap
))
334 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
335 rcu_assign_pointer(callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
],sock
);
337 callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
] = sock
;
339 set_bit(sock
->proto
.pptp
.src_addr
.call_id
,callid_bitmap
);
343 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
344 spin_unlock(&chan_lock
);
346 write_unlock_bh(&chan_lock
);
352 static void del_chan(struct pppox_sock
*sock
)
354 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
355 spin_lock(&chan_lock
);
357 write_lock_bh(&chan_lock
);
359 clear_bit(sock
->proto
.pptp
.src_addr
.call_id
,callid_bitmap
);
360 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
361 rcu_assign_pointer(callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
],NULL
);
362 spin_unlock(&chan_lock
);
365 callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
] = NULL
;
366 write_unlock_bh(&chan_lock
);
370 static int pptp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
372 struct sock
*sk
= (struct sock
*) chan
->private;
373 struct pppox_sock
*po
= pppox_sk(sk
);
374 struct pptp_opt
*opt
=&po
->proto
.pptp
;
375 struct pptp_gre_header
*hdr
;
376 unsigned int header_len
=sizeof(*hdr
);
384 struct rtable
*rt
; /* Route to the other host */
385 struct net_device
*tdev
; /* Device to other host */
386 struct iphdr
*iph
; /* Our new IP header */
387 int max_headroom
; /* The extra header space needed */
389 if (SK_STATE(sk_pppox(po
)) & PPPOX_DEAD
)
392 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
394 struct rt_key key
= {
395 .dst
=opt
->dst_addr
.sin_addr
.s_addr
,
396 .src
=opt
->src_addr
.sin_addr
.s_addr
,
399 if ((err
=ip_route_output_key(&rt
, &key
))) {
405 struct flowi fl
= { .oif
= 0,
407 { .daddr
= opt
->dst_addr
.sin_addr
.s_addr
,
408 .saddr
= opt
->src_addr
.sin_addr
.s_addr
,
409 .tos
= RT_TOS(0) } },
410 .proto
= IPPROTO_GRE
};
411 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
412 if ((err
=ip_route_output_key(&rt
, &fl
))) {
414 if ((err
=ip_route_output_key(&init_net
,&rt
, &fl
))) {
422 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
423 max_headroom
= ((tdev
->hard_header_len
+15)&~15) + sizeof(*iph
)+sizeof(*hdr
)+2;
425 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(*iph
)+sizeof(*hdr
)+2;
428 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) && !defined(HAVE_SKB_CLONE_WRITABLE)
429 if (skb_headroom(skb
) < max_headroom
|| skb_cloned(skb
) || skb_shared(skb
)) {
431 if (skb_headroom(skb
) < max_headroom
|| skb_shared(skb
) ||
432 (skb_cloned(skb
) && !skb_clone_writable(skb
,0))) {
434 struct sk_buff
*new_skb
= skb_realloc_headroom(skb
, max_headroom
);
440 skb_set_owner_w(new_skb
, skb
->sk
);
446 islcp
=((data
[0] << 8) + data
[1])== PPP_LCP
&& 1 <= data
[2] && data
[2] <= 7;
448 /* compress protocol field */
449 if ((opt
->ppp_flags
& SC_COMP_PROT
) && data
[0]==0 && !islcp
)
453 * Put in the address/control bytes if necessary
455 if ((opt
->ppp_flags
& SC_COMP_AC
) == 0 || islcp
) {
456 data
=skb_push(skb
,2);
457 data
[0]=PPP_ALLSTATIONS
;
463 seq_recv
= opt
->seq_recv
;
465 if (opt
->ack_sent
== seq_recv
) header_len
-=sizeof(hdr
->ack
);
467 // Push down and install GRE header
468 skb_push(skb
,header_len
);
469 hdr
=(struct pptp_gre_header
*)(skb
->data
);
471 hdr
->flags
= PPTP_GRE_FLAG_K
;
472 hdr
->ver
= PPTP_GRE_VER
;
473 hdr
->protocol
= htons(PPTP_GRE_PROTO
);
474 hdr
->call_id
= htons(opt
->dst_addr
.call_id
);
476 hdr
->flags
|= PPTP_GRE_FLAG_S
;
477 hdr
->seq
= htonl(++opt
->seq_sent
);
479 if (log_level
>=3 && opt
->seq_sent
<=log_packets
)
480 printk(KERN_INFO
"PPTP[%i]: send packet: seq=%i",opt
->src_addr
.call_id
,opt
->seq_sent
);
482 if (opt
->ack_sent
!= seq_recv
) {
483 /* send ack with this message */
484 hdr
->ver
|= PPTP_GRE_FLAG_A
;
485 hdr
->ack
= htonl(seq_recv
);
486 opt
->ack_sent
= seq_recv
;
488 if (log_level
>=3 && opt
->seq_sent
<=log_packets
)
489 printk(" ack=%i",seq_recv
);
492 hdr
->payload_len
= htons(len
);
494 if (log_level
>=3 && opt
->seq_sent
<=log_packets
)
499 * Push down and install the IP header.
502 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
503 skb_reset_transport_header(skb
);
504 skb_push(skb
, sizeof(*iph
));
505 skb_reset_network_header(skb
);
506 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
507 skb
->transport_header
= skb
->network_header
;
508 skb_push(skb
, sizeof(*iph
));
509 skb_reset_network_header(skb
);
511 skb
->h
.raw
= skb
->nh
.raw
;
512 skb
->nh
.raw
= skb_push(skb
, sizeof(*iph
));
514 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
515 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
516 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
520 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
526 iph
->ihl
= sizeof(struct iphdr
) >> 2;
527 if (ip_dont_fragment(sk
, &rt
->dst
))
528 iph
->frag_off
= htons(IP_DF
);
531 iph
->protocol
= IPPROTO_GRE
;
533 iph
->daddr
= rt
->rt_dst
;
534 iph
->saddr
= rt
->rt_src
;
535 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
536 iph
->ttl
= sk
->protinfo
.af_inet
.ttl
;
538 iph
->ttl
= dst_metric(&rt
->dst
, RTAX_HOPLIMIT
);
540 iph
->tot_len
= htons(skb
->len
);
542 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
544 skb_dst_set(skb
,&rt
->dst
);
546 dst_release(skb
->dst
);
552 skb
->ip_summed
= CHECKSUM_NONE
;
553 ip_select_ident(iph
, &rt
->dst
, NULL
);
556 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
557 err
= NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->dst
.dev
, ip_send
);
558 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
559 err
= NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->dst
.dev
, dst_output
);
561 err
= ip_local_out(skb
);
568 static int pptp_rcv_core(struct sock
*sk
,struct sk_buff
*skb
)
570 struct pppox_sock
*po
= pppox_sk(sk
);
571 struct pptp_opt
*opt
=&po
->proto
.pptp
;
572 int headersize
,payload_len
,seq
;
574 struct pptp_gre_header
*header
;
576 if (!(SK_STATE(sk
) & PPPOX_CONNECTED
)) {
577 if (sock_queue_rcv_skb(sk
, skb
))
579 return NET_RX_SUCCESS
;
582 header
= (struct pptp_gre_header
*)(skb
->data
);
584 /* test if acknowledgement present */
585 if (PPTP_GRE_IS_A(header
->ver
)){
586 u32 ack
= (PPTP_GRE_IS_S(header
->flags
))?
587 header
->ack
:header
->seq
; /* ack in different place if S = 0 */
591 if (ack
> opt
->ack_recv
) opt
->ack_recv
= ack
;
592 /* also handle sequence number wrap-around */
593 if (WRAPPED(ack
,opt
->ack_recv
)) opt
->ack_recv
= ack
;
596 /* test if payload present */
597 if (!PPTP_GRE_IS_S(header
->flags
)){
601 headersize
= sizeof(*header
);
602 payload_len
= ntohs(header
->payload_len
);
603 seq
= ntohl(header
->seq
);
605 /* no ack present? */
606 if (!PPTP_GRE_IS_A(header
->ver
)) headersize
-= sizeof(header
->ack
);
607 /* check for incomplete packet (length smaller than expected) */
608 if (skb
->len
- headersize
< payload_len
){
611 printk(KERN_INFO
"PPTP: discarding truncated packet (expected %d, got %d bytes)\n",
612 payload_len
, skb
->len
- headersize
);
617 payload
=skb
->data
+headersize
;
618 /* check for expected sequence number */
619 if ( seq
< opt
->seq_recv
+ 1 || WRAPPED(opt
->seq_recv
, seq
) ){
620 if ( (payload
[0] == PPP_ALLSTATIONS
) && (payload
[1] == PPP_UI
) &&
621 (PPP_PROTOCOL(payload
) == PPP_LCP
) &&
622 ((payload
[4] == PPP_LCP_ECHOREQ
) || (payload
[4] == PPP_LCP_ECHOREP
)) ){
625 printk(KERN_INFO
"PPTP[%i]: allowing old LCP Echo packet %d (expecting %d)\n", opt
->src_addr
.call_id
,
626 seq
, opt
->seq_recv
+ 1);
632 printk(KERN_INFO
"PPTP[%i]: discarding duplicate or old packet %d (expecting %d)\n",opt
->src_addr
.call_id
,
633 seq
, opt
->seq_recv
+ 1);
639 if ( log_level
>= 3 && opt
->seq_sent
<=log_packets
)
640 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
,
649 skb_pull(skb
,headersize
);
651 if (payload
[0] == PPP_ALLSTATIONS
&& payload
[1] == PPP_UI
){
652 /* chop off address/control */
658 if ((*skb
->data
) & 1){
659 /* protocol is compressed */
660 skb_push(skb
, 1)[0] = 0;
663 skb
->ip_summed
=CHECKSUM_NONE
;
664 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,21)
665 skb_set_network_header(skb
,skb
->head
-skb
->data
);
667 ppp_input(&po
->chan
,skb
);
669 return NET_RX_SUCCESS
;
676 static int pptp_rcv(struct sk_buff
*skb
)
678 struct pppox_sock
*po
;
679 struct pptp_gre_header
*header
;
681 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
686 if (skb
->pkt_type
!= PACKET_HOST
)
689 #if !defined(CONFIG_NET_IPGRE_DEMUX) && !defined(CONFIG_NET_IPGRE_DEMUX_MODULE)
690 if (!pskb_may_pull(skb
, 12))
694 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
700 header
= (struct pptp_gre_header
*)skb
->data
;
702 if ( /* version should be 1 */
703 ((header
->ver
& 0x7F) != PPTP_GRE_VER
) ||
704 /* PPTP-GRE protocol for PPTP */
705 (ntohs(header
->protocol
) != PPTP_GRE_PROTO
)||
706 /* flag C should be clear */
707 PPTP_GRE_IS_C(header
->flags
) ||
708 /* flag R should be clear */
709 PPTP_GRE_IS_R(header
->flags
) ||
710 /* flag K should be set */
711 (!PPTP_GRE_IS_K(header
->flags
)) ||
712 /* routing and recursion ctrl = 0 */
713 ((header
->flags
&0xF) != 0)){
714 /* if invalid, discard this packet */
717 printk(KERN_INFO
"PPTP: Discarding GRE: %X %X %X %X %X %X\n",
718 header
->ver
&0x7F, ntohs(header
->protocol
),
719 PPTP_GRE_IS_C(header
->flags
),
720 PPTP_GRE_IS_R(header
->flags
),
721 PPTP_GRE_IS_K(header
->flags
),
722 header
->flags
& 0xF);
728 if ((po
=lookup_chan(htons(header
->call_id
),iph
->saddr
))) {
729 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
732 dst_release(skb
->dst
);
736 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
739 /* Socket state is unknown, must put skb into backlog. */
740 if (sk
->lock
.users
!= 0) {
741 sk_add_backlog(sk
, skb
);
742 ret
= NET_RX_SUCCESS
;
744 ret
= pptp_rcv_core(sk
, skb
);
750 #else /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
752 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,19)
753 return sk_receive_skb(sk_pppox(po
), skb
);
755 return sk_receive_skb(sk_pppox(po
), skb
, 0);
758 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
762 printk(KERN_INFO
"PPTP: Discarding packet from unknown call_id %i\n",htons(header
->call_id
));
771 static int pptp_bind(struct socket
*sock
,struct sockaddr
*uservaddr
,int sockaddr_len
)
773 struct sock
*sk
= sock
->sk
;
774 struct sockaddr_pppox
*sp
= (struct sockaddr_pppox
*) uservaddr
;
775 struct pppox_sock
*po
= pppox_sk(sk
);
776 struct pptp_opt
*opt
=&po
->proto
.pptp
;
781 printk(KERN_INFO
"PPTP: bind: addr=%X call_id=%i\n",sp
->sa_addr
.pptp
.sin_addr
.s_addr
,
782 sp
->sa_addr
.pptp
.call_id
);
786 opt
->src_addr
=sp
->sa_addr
.pptp
;
791 printk(KERN_INFO
"PPTP: using call_id %i\n",opt
->src_addr
.call_id
);
798 static int pptp_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
799 int sockaddr_len
, int flags
)
801 struct sock
*sk
= sock
->sk
;
802 struct sockaddr_pppox
*sp
= (struct sockaddr_pppox
*) uservaddr
;
803 struct pppox_sock
*po
= pppox_sk(sk
);
804 struct pptp_opt
*opt
= &po
->proto
.pptp
;
805 struct rtable
*rt
; /* Route to the other host */
808 if (sp
->sa_protocol
!= PX_PROTO_PPTP
)
813 printk(KERN_INFO
"PPTP[%i]: connect: addr=%X call_id=%i\n",opt
->src_addr
.call_id
,
814 sp
->sa_addr
.pptp
.sin_addr
.s_addr
,sp
->sa_addr
.pptp
.call_id
);
817 if (lookup_chan_dst(sp
->sa_addr
.pptp
.call_id
,sp
->sa_addr
.pptp
.sin_addr
.s_addr
))
821 /* Check for already bound sockets */
822 if (SK_STATE(sk
) & PPPOX_CONNECTED
){
827 /* Check for already disconnected sockets, on attempts to disconnect */
828 if (SK_STATE(sk
) & PPPOX_DEAD
){
833 if (!opt
->src_addr
.sin_addr
.s_addr
|| !sp
->sa_addr
.pptp
.sin_addr
.s_addr
){
839 po
->chan
.ops
=&pptp_chan_ops
;
841 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
843 struct rt_key key
= {
844 .dst
=opt
->dst_addr
.sin_addr
.s_addr
,
845 .src
=opt
->src_addr
.sin_addr
.s_addr
,
848 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
849 if (ip_route_output_key(&rt
, &key
)) {
851 if (ip_route_output_key(&init_net
, &rt
, &key
)) {
853 error
= -EHOSTUNREACH
;
861 { .daddr
= opt
->dst_addr
.sin_addr
.s_addr
,
862 .saddr
= opt
->src_addr
.sin_addr
.s_addr
,
863 .tos
= RT_CONN_FLAGS(sk
) } },
864 .proto
= IPPROTO_GRE
};
865 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
866 security_sk_classify_flow(sk
, &fl
);
868 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
869 if (ip_route_output_key(&rt
, &fl
)){
871 if (ip_route_output_key(&init_net
, &rt
, &fl
)){
873 error
= -EHOSTUNREACH
;
876 sk_setup_caps(sk
, &rt
->dst
);
879 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
880 po
->chan
.mtu
=PPP_MTU
;
882 po
->chan
.mtu
=dst_mtu(&rt
->dst
);
883 if (!po
->chan
.mtu
) po
->chan
.mtu
=PPP_MTU
;
886 po
->chan
.mtu
-=PPTP_HEADER_OVERHEAD
;
888 po
->chan
.hdrlen
=2+sizeof(struct pptp_gre_header
);
889 po
->chan
.hdrlen
+= LL_MAX_HEADER
+ sizeof(struct iphdr
);
890 error
= ppp_register_channel(&po
->chan
);
892 printk(KERN_ERR
"PPTP: failed to register PPP channel (%d)\n",error
);
896 opt
->dst_addr
=sp
->sa_addr
.pptp
;
897 SK_STATE(sk
) = PPPOX_CONNECTED
;
904 static int pptp_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
905 int *usockaddr_len
, int peer
)
907 int len
= sizeof(struct sockaddr_pppox
);
908 struct sockaddr_pppox sp
;
910 sp
.sa_family
= AF_PPPOX
;
911 sp
.sa_protocol
= PX_PROTO_PPTP
;
912 sp
.sa_addr
.pptp
=pppox_sk(sock
->sk
)->proto
.pptp
.src_addr
;
914 memcpy(uaddr
, &sp
, len
);
916 *usockaddr_len
= len
;
921 static int pptp_release(struct socket
*sock
)
923 struct sock
*sk
= sock
->sk
;
924 struct pppox_sock
*po
;
925 struct pptp_opt
*opt
;
933 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
936 if (sock_flag(sk
, SOCK_DEAD
))
947 pppox_unbind_sock(sk
);
948 SK_STATE(sk
) = PPPOX_DEAD
;
952 printk(KERN_INFO
"PPTP[%i]: release\n",opt
->src_addr
.call_id
);
965 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
966 static struct proto pptp_sk_proto
= {
968 .owner
= THIS_MODULE
,
969 .obj_size
= sizeof(struct pppox_sock
),
973 static struct proto_ops pptp_ops
= {
975 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
976 .owner
= THIS_MODULE
,
978 .release
= pptp_release
,
980 .connect
= pptp_connect
,
981 .socketpair
= sock_no_socketpair
,
982 .accept
= sock_no_accept
,
983 .getname
= pptp_getname
,
984 .poll
= sock_no_poll
,
985 .listen
= sock_no_listen
,
986 .shutdown
= sock_no_shutdown
,
987 .setsockopt
= sock_no_setsockopt
,
988 .getsockopt
= sock_no_getsockopt
,
989 .sendmsg
= sock_no_sendmsg
,
990 .recvmsg
= sock_no_recvmsg
,
991 .mmap
= sock_no_mmap
,
992 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
993 .ioctl
= pppox_ioctl
,
998 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
999 static void pptp_sock_destruct(struct sock
*sk
)
1001 skb_queue_purge(&sk
->receive_queue
);
1002 if (!(SK_STATE(sk
) & PPPOX_DEAD
)) {
1003 del_chan(pppox_sk(sk
));
1004 pppox_unbind_sock(sk
);
1006 if (sk
->protinfo
.destruct_hook
)
1007 kfree(sk
->protinfo
.destruct_hook
);
1012 static int pptp_create(struct socket
*sock
)
1014 int error
= -ENOMEM
;
1016 struct pppox_sock
*po
;
1017 struct pptp_opt
*opt
;
1021 sk
= sk_alloc(PF_PPPOX
, GFP_KERNEL
, 1);
1025 sock_init_data(sock
, sk
);
1027 sock
->state
= SS_UNCONNECTED
;
1028 sock
->ops
= &pptp_ops
;
1030 //sk->sk_backlog_rcv = pppoe_rcv_core;
1031 sk
->state
= PPPOX_NONE
;
1032 sk
->type
= SOCK_STREAM
;
1033 sk
->family
= PF_PPPOX
;
1034 sk
->protocol
= PX_PROTO_PPTP
;
1036 sk
->protinfo
.pppox
=kzalloc(sizeof(struct pppox_sock
),GFP_KERNEL
);
1037 sk
->destruct
=pptp_sock_destruct
;
1038 sk
->protinfo
.destruct_hook
=sk
->protinfo
.pppox
;
1042 opt
=&po
->proto
.pptp
;
1044 opt
->seq_sent
=0; opt
->seq_recv
=0;
1045 opt
->ack_recv
=0; opt
->ack_sent
=0;
1052 static void pptp_sock_destruct(struct sock
*sk
)
1054 if (!(SK_STATE(sk
) & PPPOX_DEAD
)){
1055 del_chan(pppox_sk(sk
));
1056 pppox_unbind_sock(sk
);
1058 skb_queue_purge(&sk
->sk_receive_queue
);
1060 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1061 static int pptp_create(struct socket
*sock
)
1063 static int pptp_create(struct net
*net
, struct socket
*sock
)
1066 int error
= -ENOMEM
;
1068 struct pppox_sock
*po
;
1069 struct pptp_opt
*opt
;
1071 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1072 sk
= sk_alloc(PF_PPPOX
, GFP_KERNEL
, &pptp_sk_proto
, 1);
1074 sk
= sk_alloc(net
,PF_PPPOX
, GFP_KERNEL
, &pptp_sk_proto
);
1079 sock_init_data(sock
, sk
);
1081 sock
->state
= SS_UNCONNECTED
;
1082 sock
->ops
= &pptp_ops
;
1084 sk
->sk_backlog_rcv
= pptp_rcv_core
;
1085 sk
->sk_state
= PPPOX_NONE
;
1086 sk
->sk_type
= SOCK_STREAM
;
1087 sk
->sk_family
= PF_PPPOX
;
1088 sk
->sk_protocol
= PX_PROTO_PPTP
;
1089 sk
->sk_destruct
= pptp_sock_destruct
;
1092 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1095 opt
=&po
->proto
.pptp
;
1097 opt
->seq_sent
= 0; opt
->seq_recv
= 0xffffffff;
1098 opt
->ack_recv
= 0; opt
->ack_sent
= 0xffffffff;
1107 static int pptp_ppp_ioctl(struct ppp_channel
*chan
, unsigned int cmd
,
1110 struct sock
*sk
= (struct sock
*) chan
->private;
1111 struct pppox_sock
*po
= pppox_sk(sk
);
1112 struct pptp_opt
*opt
=&po
->proto
.pptp
;
1113 void __user
*argp
= (void __user
*)arg
;
1114 int __user
*p
= argp
;
1120 val
= opt
->ppp_flags
;
1121 if (put_user(val
, p
))
1126 if (get_user(val
, p
))
1128 opt
->ppp_flags
= val
& ~SC_RCV_BITS
;
1139 static struct pppox_proto pppox_pptp_proto
= {
1140 .create
= pptp_create
,
1141 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
1142 .owner
= THIS_MODULE
,
1146 #if defined(CONFIG_NET_IPGRE_DEMUX) || defined(CONFIG_NET_IPGRE_DEMUX_MODULE)
1147 static struct gre_protocol gre_pptp_protocol
= {
1148 .handler
= pptp_rcv
,
1150 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1151 static struct inet_protocol net_pptp_protocol
= {
1152 .handler
= pptp_rcv
,
1153 .protocol
= IPPROTO_GRE
,
1157 static struct net_protocol net_pptp_protocol
= {
1158 .handler
= pptp_rcv
,
1162 static int __init
pptp_init_module(void)
1165 printk(KERN_INFO
"PPTP driver version " PPTP_DRIVER_VERSION
"\n");
1166 printk("=== PPTP init ===\n");
1167 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1168 callid_sock
= __vmalloc((MAX_CALLID
+ 1) * sizeof(void *),
1169 GFP_KERNEL
| __GFP_ZERO
, PAGE_KERNEL
);
1171 callid_sock
= __vmalloc((MAX_CALLID
+ 1) * sizeof(void *),
1172 GFP_KERNEL
, PAGE_KERNEL
);
1173 memset(callid_sock
, 0, (MAX_CALLID
+ 1) * sizeof(void *));
1176 printk(KERN_ERR
"PPTP: cann't allocate memory\n");
1180 #if defined(CONFIG_NET_IPGRE_DEMUX) || defined(CONFIG_NET_IPGRE_DEMUX_MODULE)
1181 if (gre_add_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
) < 0) {
1182 printk(KERN_INFO
"PPTP: can't add protocol\n");
1185 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1186 inet_add_protocol(&net_pptp_protocol
);
1188 if (inet_add_protocol(&net_pptp_protocol
, IPPROTO_GRE
) < 0) {
1189 printk(KERN_INFO
"PPTP: can't add protocol\n");
1194 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1195 err
= proto_register(&pptp_sk_proto
, 0);
1197 printk(KERN_INFO
"PPTP: can't register sk_proto\n");
1198 goto out_inet_del_protocol
;
1202 err
= register_pppox_proto(PX_PROTO_PPTP
, &pppox_pptp_proto
);
1204 printk(KERN_INFO
"PPTP: can't register pppox_proto\n");
1205 goto out_unregister_sk_proto
;
1209 out_unregister_sk_proto
:
1210 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1211 proto_unregister(&pptp_sk_proto
);
1214 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1215 out_inet_del_protocol
:
1218 #if defined(CONFIG_NET_IPGRE_DEMUX) || defined(CONFIG_NET_IPGRE_DEMUX_MODULE)
1219 gre_del_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
);
1220 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1221 inet_del_protocol(&net_pptp_protocol
);
1223 inet_del_protocol(&net_pptp_protocol
, IPPROTO_GRE
);
1231 static void __exit
pptp_exit_module(void)
1233 unregister_pppox_proto(PX_PROTO_PPTP
);
1234 #if defined(CONFIG_NET_IPGRE_DEMUX) || defined(CONFIG_NET_IPGRE_DEMUX_MODULE)
1235 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1236 proto_unregister(&pptp_sk_proto
);
1238 gre_del_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
);
1239 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1240 inet_del_protocol(&net_pptp_protocol
);
1242 proto_unregister(&pptp_sk_proto
);
1243 inet_del_protocol(&net_pptp_protocol
, IPPROTO_GRE
);
1248 module_init(pptp_init_module
);
1249 module_exit(pptp_exit_module
);
1251 MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol for Linux");
1252 MODULE_AUTHOR("Kozlov D. (xeb@mail.ru)");
1253 MODULE_LICENSE("GPL");
1256 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1257 MODULE_PARM(log_level
,"i");
1258 MODULE_PARM(log_packets
,"i");
1260 module_param(log_level
,int,0);
1261 module_param(log_packets
,int,0);
1263 MODULE_PARM_DESC(log_level
,"Logging level (default=0)");