GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / pptp.c
blob046c874c53c9211a49ce92b168fe9c3362c3254d
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/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>
29 #include <linux/in.h>
30 #include <linux/ip.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>
37 #endif
39 #include <net/sock.h>
40 #include <net/protocol.h>
41 #include <net/ip.h>
42 #include <net/icmp.h>
43 #include <net/route.h>
45 #include <asm/uaccess.h>
47 //#define DEBUG
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)
53 #include <net/gre.h>
54 #endif
56 #define PPTP_DRIVER_VERSION "0.8.5"
58 #ifdef DEBUG
59 static int log_level=0;
60 static int log_packets=10;
61 #endif
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))
74 #endif
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) \
83 do { \
84 (_timer)->function=_routine; \
85 (_timer)->data=_data; \
86 init_timer(_timer); \
87 } while (0);
89 static inline void *kzalloc(size_t size,int gfp)
91 void *p=kmalloc(size,gfp);
92 memset(p,0,size);
93 return p;
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);
101 skb->nfct=NULL;
102 #ifdef CONFIG_NETFILTER_DEBUG
103 skb->nf_debug=0;
104 #endif
105 #endif
107 #define __user
108 #endif
109 #endif
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)
120 int num = 0;
122 #if BITS_PER_LONG == 64
123 if ((word & 0xffffffff) == 0) {
124 num += 32;
125 word >>= 32;
127 #endif
128 if ((word & 0xffff) == 0) {
129 num += 16;
130 word >>= 16;
132 if ((word & 0xff) == 0) {
133 num += 8;
134 word >>= 8;
136 if ((word & 0xf) == 0) {
137 num += 4;
138 word >>= 4;
140 if ((word & 0x3) == 0) {
141 num += 2;
142 word >>= 2;
144 if ((word & 0x1) == 0)
145 num += 1;
146 return num;
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);
158 unsigned long tmp;
160 if (offset >= size)
161 return size;
162 size -= result;
163 offset %= BITS_PER_LONG;
164 if (offset) {
165 tmp = *(p++);
166 tmp &= (~0UL << offset);
167 if (size < BITS_PER_LONG)
168 goto found_first;
169 if (tmp)
170 goto found_middle;
171 size -= BITS_PER_LONG;
172 result += BITS_PER_LONG;
174 while (size & ~(BITS_PER_LONG-1)) {
175 if ((tmp = *(p++)))
176 goto found_middle;
177 result += BITS_PER_LONG;
178 size -= BITS_PER_LONG;
180 if (!size)
181 return result;
182 tmp = *p;
184 found_first:
185 tmp &= (~0UL >> (BITS_PER_LONG - size));
186 if (tmp == 0UL) /* Are any bits set? */
187 return result + size; /* Nope. */
188 found_middle:
189 return result + __ffs(tmp);
191 #endif
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
197 #else
198 static DEFINE_SPINLOCK(chan_lock);
199 #define SK_STATE(sk) (sk)->sk_state
200 #endif
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,
204 unsigned long arg);
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)
249 #else
250 static struct pppox_sock * lookup_chan(u16 call_id, __be32 s_addr)
251 #endif
253 struct pppox_sock *sock;
254 struct pptp_opt *opt;
256 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
257 rcu_read_lock();
258 sock = rcu_dereference(callid_sock[call_id]);
259 #else
260 read_lock(&chan_lock);
261 sock = callid_sock[call_id];
262 #endif
263 if (sock) {
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)
269 rcu_read_unlock();
270 #else
271 read_unlock(&chan_lock);
272 #endif
274 return sock;
277 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
278 static int lookup_chan_dst(u16 call_id, u32 d_addr)
279 #else
280 static int lookup_chan_dst(u16 call_id, __be32 d_addr)
281 #endif
283 struct pppox_sock *sock;
284 struct pptp_opt *opt;
285 int i;
287 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
288 rcu_read_lock();
289 #else
290 read_lock(&chan_lock);
291 #endif
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]);
296 #else
297 sock = callid_sock[i];
298 #endif
299 if (!sock)
300 continue;
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)
305 rcu_read_unlock();
306 #else
307 read_unlock(&chan_lock);
308 #endif
310 return i<MAX_CALLID;
313 static int add_chan(struct pppox_sock *sock)
315 static int call_id=0;
316 int res=-1;
318 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
319 spin_lock(&chan_lock);
320 #else
321 write_lock_bh(&chan_lock);
322 #endif
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))
332 goto exit;
334 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
335 rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id],sock);
336 #else
337 callid_sock[sock->proto.pptp.src_addr.call_id] = sock;
338 #endif
339 set_bit(sock->proto.pptp.src_addr.call_id,callid_bitmap);
340 res=0;
342 exit:
343 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
344 spin_unlock(&chan_lock);
345 #else
346 write_unlock_bh(&chan_lock);
347 #endif
349 return res;
352 static void del_chan(struct pppox_sock *sock)
354 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
355 spin_lock(&chan_lock);
356 #else
357 write_lock_bh(&chan_lock);
358 #endif
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);
363 synchronize_rcu();
364 #else
365 callid_sock[sock->proto.pptp.src_addr.call_id] = NULL;
366 write_unlock_bh(&chan_lock);
367 #endif
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);
377 int err=0;
378 int islcp;
379 int len;
380 unsigned char *data;
381 u32 seq_recv;
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)
390 goto tx_error;
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,
397 .tos=RT_TOS(0),
399 if ((err=ip_route_output_key(&rt, &key))) {
400 goto tx_error;
403 #else
405 struct flowi fl = { .oif = 0,
406 .nl_u = { .ip4_u =
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))) {
413 #else
414 if ((err=ip_route_output_key(&init_net,&rt, &fl))) {
415 #endif
416 goto tx_error;
419 #endif
420 tdev = rt->dst.dev;
422 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
423 max_headroom = ((tdev->hard_header_len+15)&~15) + sizeof(*iph)+sizeof(*hdr)+2;
424 #else
425 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph)+sizeof(*hdr)+2;
426 #endif
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)) {
430 #else
431 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
432 (skb_cloned(skb) && !skb_clone_writable(skb,0))) {
433 #endif
434 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
435 if (!new_skb) {
436 ip_rt_put(rt);
437 goto tx_error;
439 if (skb->sk)
440 skb_set_owner_w(new_skb, skb->sk);
441 kfree_skb(skb);
442 skb = new_skb;
445 data=skb->data;
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)
450 skb_pull(skb,1);
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;
458 data[1]=PPP_UI;
461 len=skb->len;
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);
478 #ifdef DEBUG
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);
481 #endif
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;
487 #ifdef DEBUG
488 if (log_level>=3 && opt->seq_sent<=log_packets)
489 printk(" ack=%i",seq_recv);
490 #endif
492 hdr->payload_len = htons(len);
493 #ifdef DEBUG
494 if (log_level>=3 && opt->seq_sent<=log_packets)
495 printk("\n");
496 #endif
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);
510 #else
511 skb->h.raw = skb->nh.raw;
512 skb->nh.raw = skb_push(skb, sizeof(*iph));
513 #endif
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 |
517 IPSKB_REROUTED);
518 #endif
520 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
521 iph = ip_hdr(skb);
522 #else
523 iph = skb->nh.iph;
524 #endif
525 iph->version = 4;
526 iph->ihl = sizeof(struct iphdr) >> 2;
527 if (ip_dont_fragment(sk, &rt->dst))
528 iph->frag_off = htons(IP_DF);
529 else
530 iph->frag_off = 0;
531 iph->protocol = IPPROTO_GRE;
532 iph->tos = 0;
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;
537 #else
538 iph->ttl = dst_metric(&rt->dst, RTAX_HOPLIMIT);
539 #endif
540 iph->tot_len = htons(skb->len);
542 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
543 skb_dst_drop(skb);
544 skb_dst_set(skb,&rt->dst);
545 #else
546 dst_release(skb->dst);
547 skb->dst = &rt->dst;
548 #endif
550 nf_reset(skb);
552 skb->ip_summed = CHECKSUM_NONE;
553 ip_select_ident(iph, &rt->dst, NULL);
554 ip_send_check(iph);
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);
560 #else
561 err = ip_local_out(skb);
562 #endif
564 tx_error:
565 return 1;
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;
573 u8 *payload;
574 struct pptp_gre_header *header;
576 if (!(SK_STATE(sk) & PPPOX_CONNECTED)) {
577 if (sock_queue_rcv_skb(sk, skb))
578 goto drop;
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 */
589 ack = ntohl( ack);
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)){
598 goto drop;
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){
609 #ifdef DEBUG
610 if (log_level>=1)
611 printk(KERN_INFO"PPTP: discarding truncated packet (expected %d, got %d bytes)\n",
612 payload_len, skb->len - headersize);
613 #endif
614 goto drop;
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)) ){
623 #ifdef DEBUG
624 if ( log_level >= 1)
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);
627 #endif
628 goto allow_packet;
630 #ifdef DEBUG
631 if ( log_level >= 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);
634 #endif
635 }else{
636 opt->seq_recv = seq;
637 allow_packet:
638 #ifdef DEBUG
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,
641 *(payload +0),
642 *(payload +1),
643 *(payload +2),
644 *(payload +3),
645 *(payload +4),
646 *(payload +5));
647 #endif
649 skb_pull(skb,headersize);
651 if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI){
652 /* chop off address/control */
653 if (skb->len < 3)
654 goto drop;
655 skb_pull(skb,2);
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);
666 #endif
667 ppp_input(&po->chan,skb);
669 return NET_RX_SUCCESS;
671 drop:
672 kfree_skb(skb);
673 return NET_RX_DROP;
676 static int pptp_rcv(struct sk_buff *skb)
678 struct pppox_sock *po;
679 struct pptp_gre_header *header;
680 struct iphdr *iph;
681 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
682 int ret;
683 struct sock *sk;
684 #endif
686 if (skb->pkt_type != PACKET_HOST)
687 goto drop;
689 #if !defined(CONFIG_NET_IPGRE_DEMUX) && !defined(CONFIG_NET_IPGRE_DEMUX_MODULE)
690 if (!pskb_may_pull(skb, 12))
691 goto drop;
692 #endif
694 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
695 iph = ip_hdr(skb);
696 #else
697 iph = skb->nh.iph;
698 #endif
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 */
715 #ifdef DEBUG
716 if (log_level>=1)
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);
723 #endif
724 goto drop;
728 if ((po=lookup_chan(htons(header->call_id),iph->saddr))) {
729 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
730 skb_dst_drop(skb);
731 #else
732 dst_release(skb->dst);
733 skb->dst = NULL;
734 #endif
735 nf_reset(skb);
736 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
737 sk=sk_pppox(po);
738 bh_lock_sock(sk);
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;
743 } else {
744 ret = pptp_rcv_core(sk, skb);
746 bh_unlock_sock(sk);
747 sock_put(sk);
748 return ret;
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);
754 #else
755 return sk_receive_skb(sk_pppox(po), skb, 0);
756 #endif
758 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0) */
759 }else {
760 #ifdef DEBUG
761 if (log_level>=1)
762 printk(KERN_INFO"PPTP: Discarding packet from unknown call_id %i\n",htons(header->call_id));
763 #endif
766 drop:
767 kfree_skb(skb);
768 return NET_RX_DROP;
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;
777 int error=0;
779 #ifdef DEBUG
780 if (log_level>=1)
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);
783 #endif
784 lock_sock(sk);
786 opt->src_addr=sp->sa_addr.pptp;
787 if (add_chan(po))
788 error=-EBUSY;
789 #ifdef DEBUG
790 if (log_level>=1)
791 printk(KERN_INFO"PPTP: using call_id %i\n",opt->src_addr.call_id);
792 #endif
794 release_sock(sk);
795 return error;
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 */
806 int error=0;
808 if (sp->sa_protocol != PX_PROTO_PPTP)
809 return -EINVAL;
811 #ifdef DEBUG
812 if (log_level>=1)
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);
815 #endif
817 if (lookup_chan_dst(sp->sa_addr.pptp.call_id,sp->sa_addr.pptp.sin_addr.s_addr))
818 return -EALREADY;
820 lock_sock(sk);
821 /* Check for already bound sockets */
822 if (SK_STATE(sk) & PPPOX_CONNECTED){
823 error = -EBUSY;
824 goto end;
827 /* Check for already disconnected sockets, on attempts to disconnect */
828 if (SK_STATE(sk) & PPPOX_DEAD){
829 error = -EALREADY;
830 goto end;
833 if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr){
834 error = -EINVAL;
835 goto end;
838 po->chan.private=sk;
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,
846 .tos=RT_TOS(0),
848 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
849 if (ip_route_output_key(&rt, &key)) {
850 #else
851 if (ip_route_output_key(&init_net, &rt, &key)) {
852 #endif
853 error = -EHOSTUNREACH;
854 goto end;
857 #else
859 struct flowi fl = {
860 .nl_u = { .ip4_u =
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);
867 #endif
868 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
869 if (ip_route_output_key(&rt, &fl)){
870 #else
871 if (ip_route_output_key(&init_net, &rt, &fl)){
872 #endif
873 error = -EHOSTUNREACH;
874 goto end;
876 sk_setup_caps(sk, &rt->dst);
878 #endif
879 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
880 po->chan.mtu=PPP_MTU;
881 #else
882 po->chan.mtu=dst_mtu(&rt->dst);
883 if (!po->chan.mtu) po->chan.mtu=PPP_MTU;
884 #endif
885 ip_rt_put(rt);
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);
891 if (error){
892 printk(KERN_ERR "PPTP: failed to register PPP channel (%d)\n",error);
893 goto end;
896 opt->dst_addr=sp->sa_addr.pptp;
897 SK_STATE(sk) = PPPOX_CONNECTED;
899 end:
900 release_sock(sk);
901 return error;
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;
918 return 0;
921 static int pptp_release(struct socket *sock)
923 struct sock *sk = sock->sk;
924 struct pppox_sock *po;
925 struct pptp_opt *opt;
926 int error = 0;
928 if (!sk)
929 return 0;
931 lock_sock(sk);
933 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
934 if (sk->dead)
935 #else
936 if (sock_flag(sk, SOCK_DEAD))
937 #endif
939 release_sock(sk);
940 return -EBADF;
943 po = pppox_sk(sk);
944 opt=&po->proto.pptp;
945 del_chan(po);
947 pppox_unbind_sock(sk);
948 SK_STATE(sk) = PPPOX_DEAD;
950 #ifdef DEBUG
951 if (log_level>=1)
952 printk(KERN_INFO"PPTP[%i]: release\n",opt->src_addr.call_id);
953 #endif
955 sock_orphan(sk);
956 sock->sk = NULL;
958 release_sock(sk);
959 sock_put(sk);
961 return error;
965 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
966 static struct proto pptp_sk_proto = {
967 .name = "PPTP",
968 .owner = THIS_MODULE,
969 .obj_size = sizeof(struct pppox_sock),
971 #endif
973 static struct proto_ops pptp_ops = {
974 .family = AF_PPPOX,
975 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
976 .owner = THIS_MODULE,
977 #endif
978 .release = pptp_release,
979 .bind = pptp_bind,
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,
994 #endif
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);
1009 MOD_DEC_USE_COUNT;
1012 static int pptp_create(struct socket *sock)
1014 int error = -ENOMEM;
1015 struct sock *sk;
1016 struct pppox_sock *po;
1017 struct pptp_opt *opt;
1019 MOD_INC_USE_COUNT;
1021 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1);
1022 if (!sk)
1023 goto out;
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;
1040 po = pppox_sk(sk);
1041 po->sk=sk;
1042 opt=&po->proto.pptp;
1044 opt->seq_sent=0; opt->seq_recv=0;
1045 opt->ack_recv=0; opt->ack_sent=0;
1047 error = 0;
1048 out:
1049 return error;
1051 #else
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)
1062 #else
1063 static int pptp_create(struct net *net, struct socket *sock)
1064 #endif
1066 int error = -ENOMEM;
1067 struct sock *sk;
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);
1073 #else
1074 sk = sk_alloc(net,PF_PPPOX, GFP_KERNEL, &pptp_sk_proto);
1075 #endif
1076 if (!sk)
1077 goto out;
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;
1091 po = pppox_sk(sk);
1092 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1093 po->sk=sk;
1094 #endif
1095 opt=&po->proto.pptp;
1097 opt->seq_sent = 0; opt->seq_recv = 0xffffffff;
1098 opt->ack_recv = 0; opt->ack_sent = 0xffffffff;
1100 error = 0;
1101 out:
1102 return error;
1104 #endif
1107 static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
1108 unsigned long arg)
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;
1115 int err, val;
1117 err = -EFAULT;
1118 switch (cmd) {
1119 case PPPIOCGFLAGS:
1120 val = opt->ppp_flags;
1121 if (put_user(val, p))
1122 break;
1123 err = 0;
1124 break;
1125 case PPPIOCSFLAGS:
1126 if (get_user(val, p))
1127 break;
1128 opt->ppp_flags = val & ~SC_RCV_BITS;
1129 err = 0;
1130 break;
1131 default:
1132 err = -ENOTTY;
1135 return err;
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,
1143 #endif
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,
1154 .name = "PPTP",
1156 #else
1157 static struct net_protocol net_pptp_protocol = {
1158 .handler = pptp_rcv,
1160 #endif
1162 static int __init pptp_init_module(void)
1164 int err=0;
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);
1170 #else
1171 callid_sock = __vmalloc((MAX_CALLID + 1) * sizeof(void *),
1172 GFP_KERNEL, PAGE_KERNEL);
1173 memset(callid_sock, 0, (MAX_CALLID + 1) * sizeof(void *));
1174 #endif
1175 if (!callid_sock) {
1176 printk(KERN_ERR "PPTP: cann't allocate memory\n");
1177 return -ENOMEM;
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");
1183 goto out_free_mem;
1185 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1186 inet_add_protocol(&net_pptp_protocol);
1187 #else
1188 if (inet_add_protocol(&net_pptp_protocol, IPPROTO_GRE) < 0) {
1189 printk(KERN_INFO "PPTP: can't add protocol\n");
1190 goto out_free_mem;
1192 #endif
1194 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1195 err = proto_register(&pptp_sk_proto, 0);
1196 if (err){
1197 printk(KERN_INFO "PPTP: can't register sk_proto\n");
1198 goto out_inet_del_protocol;
1200 #endif
1202 err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto);
1203 if (err){
1204 printk(KERN_INFO "PPTP: can't register pppox_proto\n");
1205 goto out_unregister_sk_proto;
1208 return 0;
1209 out_unregister_sk_proto:
1210 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1211 proto_unregister(&pptp_sk_proto);
1212 #endif
1214 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
1215 out_inet_del_protocol:
1216 #endif
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);
1222 #else
1223 inet_del_protocol(&net_pptp_protocol, IPPROTO_GRE);
1224 #endif
1225 out_free_mem:
1226 vfree(callid_sock);
1228 return err;
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);
1237 #endif
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);
1241 #else
1242 proto_unregister(&pptp_sk_proto);
1243 inet_del_protocol(&net_pptp_protocol, IPPROTO_GRE);
1244 #endif
1245 vfree(callid_sock);
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");
1255 #ifdef DEBUG
1256 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1257 MODULE_PARM(log_level,"i");
1258 MODULE_PARM(log_packets,"i");
1259 #else
1260 module_param(log_level,int,0);
1261 module_param(log_packets,int,0);
1262 #endif
1263 MODULE_PARM_DESC(log_level,"Logging level (default=0)");
1264 #endif