pptp: accel-pptp kernel driver updates from upstream (git 5 Aug 2010)
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / net / pptp.c
blob7e5a12cb234aa7ee181b73d06c780159f7fc37d7
1 /*
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>
28 #include <linux/in.h>
29 #include <linux/ip.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>
35 #endif
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
38 #include <asm/bitops.h>
39 #endif
41 #include <net/sock.h>
42 #include <net/protocol.h>
43 #include <net/ip.h>
44 #include <net/icmp.h>
45 #include <net/route.h>
47 #include <asm/uaccess.h>
49 #define DEBUG
50 //#define USE_GRE_MOD
52 #ifdef USE_GRE_MOD
53 #include "gre.h"
54 #endif
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) \
72 do { \
73 (_timer)->function=_routine; \
74 (_timer)->data=_data; \
75 init_timer(_timer); \
76 } while (0);
78 static inline void *kzalloc(size_t size,int gfp)
80 void *p=kmalloc(size,gfp);
81 memset(p,0,size);
82 return p;
84 #endif
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);
91 skb->nfct=NULL;
92 #ifdef CONFIG_NETFILTER_DEBUG
93 skb->nf_debug=0;
94 #endif
95 #endif
97 #define __user
98 #endif
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)
109 int num = 0;
111 #if BITS_PER_LONG == 64
112 if ((word & 0xffffffff) == 0) {
113 num += 32;
114 word >>= 32;
116 #endif
117 if ((word & 0xffff) == 0) {
118 num += 16;
119 word >>= 16;
121 if ((word & 0xff) == 0) {
122 num += 8;
123 word >>= 8;
125 if ((word & 0xf) == 0) {
126 num += 4;
127 word >>= 4;
129 if ((word & 0x3) == 0) {
130 num += 2;
131 word >>= 2;
133 if ((word & 0x1) == 0)
134 num += 1;
135 return num;
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);
147 unsigned long tmp;
149 if (offset >= size)
150 return size;
151 size -= result;
152 offset %= BITS_PER_LONG;
153 if (offset) {
154 tmp = *(p++);
155 tmp &= (~0UL << offset);
156 if (size < BITS_PER_LONG)
157 goto found_first;
158 if (tmp)
159 goto found_middle;
160 size -= BITS_PER_LONG;
161 result += BITS_PER_LONG;
163 while (size & ~(BITS_PER_LONG-1)) {
164 if ((tmp = *(p++)))
165 goto found_middle;
166 result += BITS_PER_LONG;
167 size -= BITS_PER_LONG;
169 if (!size)
170 return result;
171 tmp = *p;
173 found_first:
174 tmp &= (~0UL >> (BITS_PER_LONG - size));
175 if (tmp == 0UL) /* Are any bits set? */
176 return result + size; /* Nope. */
177 found_middle:
178 return result + __ffs(tmp);
180 #endif
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
186 #else
187 static DECLARE_MUTEX(chan_lock);
188 #define SK_STATE(sk) (sk)->sk_state
189 #endif
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,
193 unsigned long arg);
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)
238 #else
239 static struct pppox_sock * lookup_chan(u16 call_id, __be32 s_addr)
240 #endif
242 struct pppox_sock *sock;
243 struct pptp_opt *opt;
245 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
246 rcu_read_lock();
247 #else
248 read_lock(&chan_lock);
249 #endif
250 sock = rcu_dereference(callid_sock[call_id]);
251 if (sock) {
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)
257 rcu_read_unlock();
258 #else
259 read_unlock(&chan_lock);
260 #endif
262 return sock;
265 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
266 static int lookup_chan_dst(u16 call_id, u32 d_addr)
267 #else
268 static int lookup_chan_dst(u16 call_id, __be32 d_addr)
269 #endif
271 struct pppox_sock *sock;
272 struct pptp_opt *opt;
273 int i;
275 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
276 down(&chan_lock);
277 #else
278 read_lock(&chan_lock);
279 #endif
280 for(i=find_next_bit(callid_bitmap,MAX_CALLID,1); i<MAX_CALLID; i=find_next_bit(callid_bitmap,MAX_CALLID,i+1)){
281 sock=callid_sock[i];
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)
286 up(&chan_lock);
287 #else
288 read_unlock(&chan_lock);
289 #endif
291 return i<MAX_CALLID;
294 static int add_chan(struct pppox_sock *sock)
296 static int call_id=0;
297 int res=-1;
299 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
300 synchronize_rcu();
301 down(&chan_lock);
302 #else
303 write_lock_bh(&chan_lock);
304 #endif
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))
314 goto exit;
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);
319 #else
320 callid_sock[sock->proto.pptp.src_addr.call_id]=sock;
321 #endif
322 res=0;
324 exit:
325 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
326 up(&chan_lock);
327 #else
328 write_unlock_bh(&chan_lock);
329 #endif
331 return res;
334 static void del_chan(struct pppox_sock *sock)
336 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
337 synchronize_rcu();
338 down(&chan_lock);
339 #else
340 write_lock_bh(&chan_lock);
341 #endif
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);
345 up(&chan_lock);
346 #else
347 callid_sock[sock->proto.pptp.src_addr.call_id]=NULL;
348 write_unlock_bh(&chan_lock);
349 #endif
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);
359 int err=0;
360 int islcp;
361 int len;
362 unsigned char *data;
363 u32 seq_recv;
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)
372 goto tx_error;
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,
379 .tos=RT_TOS(0),
381 if ((err=ip_route_output_key(&rt, &key))) {
382 goto tx_error;
385 #else
387 struct flowi fl = { .oif = 0,
388 .nl_u = { .ip4_u =
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))) {
395 #else
396 if ((err=ip_route_output_key(&init_net,&rt, &fl))) {
397 #endif
398 goto tx_error;
401 #endif
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;
406 #else
407 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph)+sizeof(*hdr)+2;
408 #endif
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);
414 if (!new_skb) {
415 ip_rt_put(rt);
416 goto tx_error;
418 if (skb->sk)
419 skb_set_owner_w(new_skb, skb->sk);
420 kfree_skb(skb);
421 skb = new_skb;
424 data=skb->data;
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)
429 skb_pull(skb,1);
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;
437 data[1]=PPP_UI;
440 len=skb->len;
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);
457 #ifdef DEBUG
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);
460 #endif
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;
466 #ifdef DEBUG
467 if (log_level>=3 && opt->seq_sent<=log_packets)
468 printk(" ack=%i",seq_recv);
469 #endif
471 hdr->payload_len = htons(len);
472 #ifdef DEBUG
473 if (log_level>=3 && opt->seq_sent<=log_packets)
474 printk("\n");
475 #endif
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);
489 #else
490 skb->h.raw = skb->nh.raw;
491 skb->nh.raw = skb_push(skb, sizeof(*iph));
492 #endif
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 |
496 IPSKB_REROUTED);
497 #endif
499 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
500 iph = ip_hdr(skb);
501 #else
502 iph = skb->nh.iph;
503 #endif
504 iph->version = 4;
505 iph->ihl = sizeof(struct iphdr) >> 2;
506 if (ip_dont_fragment(sk, &rt->u.dst))
507 iph->frag_off = htons(IP_DF);
508 else
509 iph->frag_off = 0;
510 iph->protocol = IPPROTO_GRE;
511 iph->tos = 0;
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;
516 #else
517 iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
518 #endif
519 iph->tot_len = htons(skb->len);
521 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
522 skb_dst_drop(skb);
523 skb_dst_set(skb,&rt->u.dst);
524 #else
525 dst_release(skb->dst);
526 skb->dst = &rt->u.dst;
527 #endif
529 nf_reset(skb);
531 skb->ip_summed = CHECKSUM_NONE;
532 ip_select_ident(iph, &rt->u.dst, NULL);
533 ip_send_check(iph);
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);
539 #else
540 err = ip_local_out(skb);
541 #endif
543 tx_error:
544 return 1;
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;
552 u8 *payload;
553 struct pptp_gre_header *header;
555 if (!(SK_STATE(sk) & PPPOX_CONNECTED)) {
556 if (sock_queue_rcv_skb(sk, skb))
557 goto drop;
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 */
568 ack = ntohl( ack);
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)){
577 goto drop;
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){
588 #ifdef DEBUG
589 if (log_level>=1)
590 printk(KERN_INFO"PPTP: discarding truncated packet (expected %d, got %d bytes)\n",
591 payload_len, skb->len - headersize);
592 #endif
593 goto drop;
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)) ){
602 #ifdef DEBUG
603 if ( log_level >= 1)
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);
606 #endif
607 goto allow_packet;
609 #ifdef DEBUG
610 if ( log_level >= 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);
613 #endif
614 }else{
615 opt->seq_recv = seq;
616 allow_packet:
617 #ifdef DEBUG
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,
620 *(payload +0),
621 *(payload +1),
622 *(payload +2),
623 *(payload +3),
624 *(payload +4),
625 *(payload +5));
626 #endif
628 skb_pull(skb,headersize);
630 if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI){
631 /* chop off address/control */
632 if (skb->len < 3)
633 goto drop;
634 skb_pull(skb,2);
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);
645 #endif
646 ppp_input(&po->chan,skb);
648 return NET_RX_SUCCESS;
650 drop:
651 kfree_skb(skb);
652 return NET_RX_DROP;
655 static int pptp_rcv(struct sk_buff *skb)
657 struct pppox_sock *po;
658 struct pptp_gre_header *header;
659 struct iphdr *iph;
660 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
661 int ret;
662 struct sock *sk;
663 #endif
665 if (skb->pkt_type != PACKET_HOST)
666 goto drop;
668 /*if (!pskb_may_pull(skb, 12))
669 goto drop;*/
671 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
672 iph = ip_hdr(skb);
673 #else
674 iph = skb->nh.iph;
675 #endif
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 */
692 if (log_level>=1)
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);
699 goto drop;
703 if ((po=lookup_chan(htons(header->call_id),iph->saddr))) {
704 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
705 skb_dst_drop(skb);
706 skb_dst_set(skb,NULL);
707 #else
708 dst_release(skb->dst);
709 skb->dst = NULL;
710 #endif
711 nf_reset(skb);
712 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
713 sk=sk_pppox(po);
714 bh_lock_sock(sk);
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;
719 } else {
720 ret = pptp_rcv_core(sk, skb);
722 bh_unlock_sock(sk);
723 sock_put(sk);
724 return ret;
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);
730 #else
731 return sk_receive_skb(sk_pppox(po), skb, 0);
732 #endif
734 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
735 }else {
736 #ifdef DEBUG
737 if (log_level>=1)
738 printk(KERN_INFO"PPTP: Discarding packet from unknown call_id %i\n",htons(header->call_id));
739 #endif
742 drop:
743 kfree_skb(skb);
744 return NET_RX_DROP;
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;
753 int error=0;
755 #ifdef DEBUG
756 if (log_level>=1)
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);
759 #endif
760 lock_sock(sk);
762 opt->src_addr=sp->sa_addr.pptp;
763 if (add_chan(po))
765 release_sock(sk);
766 error=-EBUSY;
768 #ifdef DEBUG
769 if (log_level>=1)
770 printk(KERN_INFO"PPTP: using call_id %i\n",opt->src_addr.call_id);
771 #endif
773 release_sock(sk);
774 return error;
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 */
785 int error=0;
787 if (sp->sa_protocol != PX_PROTO_PPTP)
788 return -EINVAL;
790 #ifdef DEBUG
791 if (log_level>=1)
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);
794 #endif
796 if (lookup_chan_dst(sp->sa_addr.pptp.call_id,sp->sa_addr.pptp.sin_addr.s_addr))
797 return -EALREADY;
799 lock_sock(sk);
800 /* Check for already bound sockets */
801 if (SK_STATE(sk) & PPPOX_CONNECTED){
802 error = -EBUSY;
803 goto end;
806 /* Check for already disconnected sockets, on attempts to disconnect */
807 if (SK_STATE(sk) & PPPOX_DEAD){
808 error = -EALREADY;
809 goto end;
812 if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr){
813 error = -EINVAL;
814 goto end;
817 po->chan.private=sk;
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,
825 .tos=RT_TOS(0),
827 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
828 if (ip_route_output_key(&rt, &key)) {
829 #else
830 if (ip_route_output_key(&init_net, &rt, &key)) {
831 #endif
832 error = -EHOSTUNREACH;
833 goto end;
836 #else
838 struct flowi fl = {
839 .nl_u = { .ip4_u =
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);
846 #endif
847 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
848 if (ip_route_output_key(&rt, &fl)){
849 #else
850 if (ip_route_output_key(&init_net, &rt, &fl)){
851 #endif
852 error = -EHOSTUNREACH;
853 goto end;
855 sk_setup_caps(sk, &rt->u.dst);
857 #endif
858 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
859 po->chan.mtu=PPP_MTU;
860 #else
861 po->chan.mtu=dst_mtu(&rt->u.dst);
862 if (!po->chan.mtu) po->chan.mtu=PPP_MTU;
863 #endif
864 ip_rt_put(rt);
865 po->chan.mtu-=PPTP_HEADER_OVERHEAD;
867 po->chan.hdrlen=2+sizeof(struct pptp_gre_header);
868 error = ppp_register_channel(&po->chan);
869 if (error){
870 printk(KERN_ERR "PPTP: failed to register PPP channel (%d)\n",error);
871 goto end;
874 opt->dst_addr=sp->sa_addr.pptp;
875 SK_STATE(sk) = PPPOX_CONNECTED;
877 end:
878 release_sock(sk);
879 return error;
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;
896 return 0;
899 static int pptp_release(struct socket *sock)
901 struct sock *sk = sock->sk;
902 struct pppox_sock *po;
903 struct pptp_opt *opt;
904 int error = 0;
906 if (!sk)
907 return 0;
909 lock_sock(sk);
911 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
912 if (sk->dead)
913 #else
914 if (sock_flag(sk, SOCK_DEAD))
915 #endif
917 release_sock(sk);
918 return -EBADF;
921 po = pppox_sk(sk);
922 opt=&po->proto.pptp;
923 del_chan(po);
925 pppox_unbind_sock(sk);
926 SK_STATE(sk) = PPPOX_DEAD;
928 #ifdef DEBUG
929 if (log_level>=1)
930 printk(KERN_INFO"PPTP[%i]: release\n",opt->src_addr.call_id);
931 #endif
933 sock_orphan(sk);
934 sock->sk = NULL;
936 release_sock(sk);
937 sock_put(sk);
939 return error;
943 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
944 static struct proto pptp_sk_proto = {
945 .name = "PPTP",
946 .owner = THIS_MODULE,
947 .obj_size = sizeof(struct pppox_sock),
949 #endif
951 static struct proto_ops pptp_ops = {
952 .family = AF_PPPOX,
953 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
954 .owner = THIS_MODULE,
955 #endif
956 .release = pptp_release,
957 .bind = pptp_bind,
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,
972 #endif
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);
987 MOD_DEC_USE_COUNT;
990 static int pptp_create(struct socket *sock)
992 int error = -ENOMEM;
993 struct sock *sk;
994 struct pppox_sock *po;
995 struct pptp_opt *opt;
997 MOD_INC_USE_COUNT;
999 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1);
1000 if (!sk)
1001 goto out;
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;
1018 po = pppox_sk(sk);
1019 po->sk=sk;
1020 opt=&po->proto.pptp;
1022 opt->seq_sent=0; opt->seq_recv=0;
1023 opt->ack_recv=0; opt->ack_sent=0;
1025 error = 0;
1026 out:
1027 return error;
1029 #else
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)
1040 #else
1041 static int pptp_create(struct net *net, struct socket *sock)
1042 #endif
1044 int error = -ENOMEM;
1045 struct sock *sk;
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);
1051 #else
1052 sk = sk_alloc(net,PF_PPPOX, GFP_KERNEL, &pptp_sk_proto);
1053 #endif
1054 if (!sk)
1055 goto out;
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;
1069 po = pppox_sk(sk);
1070 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1071 po->sk=sk;
1072 #endif
1073 opt=&po->proto.pptp;
1075 opt->seq_sent=0; opt->seq_recv=0;
1076 opt->ack_recv=0; opt->ack_sent=0;
1078 error = 0;
1079 out:
1080 return error;
1082 #endif
1085 static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
1086 unsigned long arg)
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;
1093 int err, val;
1095 err = -EFAULT;
1096 switch (cmd) {
1097 case PPPIOCGFLAGS:
1098 val = opt->ppp_flags;
1099 if (put_user(val, p))
1100 break;
1101 err = 0;
1102 break;
1103 case PPPIOCSFLAGS:
1104 if (get_user(val, p))
1105 break;
1106 opt->ppp_flags = val & ~SC_RCV_BITS;
1107 err = 0;
1108 break;
1109 default:
1110 err = -ENOTTY;
1113 return err;
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,
1121 #endif
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,
1130 .name = "PPTP",
1132 #else
1133 #ifdef USE_GRE_MOD
1134 static struct gre_protocol gre_pptp_protocol = {
1135 #else
1136 static struct net_protocol net_pptp_protocol = {
1137 #endif
1138 .handler = pptp_rcv,
1139 //.err_handler = pptp_err,
1141 #endif
1143 static int __init pptp_init_module(void)
1145 int err=0;
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);
1150 #else
1151 #ifdef USE_GRE_MOD
1152 if (gre_add_protocol(&gre_pptp_protocol, GREPROTO_PPTP) < 0) {
1153 #else
1154 if (inet_add_protocol(&net_pptp_protocol, IPPROTO_GRE) < 0) {
1155 #endif
1156 printk(KERN_INFO "PPTP: can't add protocol\n");
1157 goto out;
1159 #endif
1161 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1162 err = proto_register(&pptp_sk_proto, 0);
1163 if (err){
1164 printk(KERN_INFO "PPTP: can't register sk_proto\n");
1165 goto out_inet_del_protocol;
1167 #endif
1169 err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto);
1170 if (err){
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);
1186 #else
1187 #error unknown size of LONG
1188 #endif
1190 out:
1191 return err;
1192 out_unregister_sk_proto:
1193 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1194 proto_unregister(&pptp_sk_proto);
1195 #endif
1197 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1198 out_inet_del_protocol:
1199 #endif
1201 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1202 inet_del_protocol(&net_pptp_protocol);
1203 #else
1204 #ifdef USE_GRE_MOD
1205 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
1206 #else
1207 inet_del_protocol(&net_pptp_protocol, IPPROTO_GRE);
1208 #endif
1209 #endif
1210 goto out;
1213 static void __exit pptp_exit_module(void)
1215 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1216 flush_scheduled_tasks();
1217 #else
1218 flush_scheduled_work();
1219 #endif
1221 unregister_pppox_proto(PX_PROTO_PPTP);
1222 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1223 inet_del_protocol(&net_pptp_protocol);
1224 #else
1225 proto_unregister(&pptp_sk_proto);
1226 #ifdef USE_GRE_MOD
1227 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
1228 #else
1229 inet_del_protocol(&net_pptp_protocol, IPPROTO_GRE);
1230 #endif
1231 #endif
1232 if (callid_bitmap) free_pages((unsigned long)callid_bitmap,1);
1233 if (callid_sock) {
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);
1238 #endif
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");
1252 #else
1253 module_param(log_level,int,0);
1254 module_param(log_packets,int,0);
1255 #endif
1256 MODULE_PARM_DESC(log_level,"Logging level (default=0)");