vgaarb: use bridges to control VGA routing where possible.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / pptp.c
blob51dfcf8023c7c2bd7bf11ddd8338d8e266366d02
1 /*
2 * Point-to-Point Tunneling Protocol for Linux
4 * Authors: Dmitry Kozlov <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/vmalloc.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>
34 #include <linux/rcupdate.h>
35 #include <linux/spinlock.h>
37 #include <net/sock.h>
38 #include <net/protocol.h>
39 #include <net/ip.h>
40 #include <net/icmp.h>
41 #include <net/route.h>
42 #include <net/gre.h>
44 #include <linux/uaccess.h>
46 #define PPTP_DRIVER_VERSION "0.8.5"
48 #define MAX_CALLID 65535
50 static DECLARE_BITMAP(callid_bitmap, MAX_CALLID + 1);
51 static struct pppox_sock **callid_sock;
53 static DEFINE_SPINLOCK(chan_lock);
55 static struct proto pptp_sk_proto __read_mostly;
56 static const struct ppp_channel_ops pptp_chan_ops;
57 static const struct proto_ops pptp_ops;
59 #define PPP_LCP_ECHOREQ 0x09
60 #define PPP_LCP_ECHOREP 0x0A
61 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
63 #define MISSING_WINDOW 20
64 #define WRAPPED(curseq, lastseq)\
65 ((((curseq) & 0xffffff00) == 0) &&\
66 (((lastseq) & 0xffffff00) == 0xffffff00))
68 #define PPTP_GRE_PROTO 0x880B
69 #define PPTP_GRE_VER 0x1
71 #define PPTP_GRE_FLAG_C 0x80
72 #define PPTP_GRE_FLAG_R 0x40
73 #define PPTP_GRE_FLAG_K 0x20
74 #define PPTP_GRE_FLAG_S 0x10
75 #define PPTP_GRE_FLAG_A 0x80
77 #define PPTP_GRE_IS_C(f) ((f)&PPTP_GRE_FLAG_C)
78 #define PPTP_GRE_IS_R(f) ((f)&PPTP_GRE_FLAG_R)
79 #define PPTP_GRE_IS_K(f) ((f)&PPTP_GRE_FLAG_K)
80 #define PPTP_GRE_IS_S(f) ((f)&PPTP_GRE_FLAG_S)
81 #define PPTP_GRE_IS_A(f) ((f)&PPTP_GRE_FLAG_A)
83 #define PPTP_HEADER_OVERHEAD (2+sizeof(struct pptp_gre_header))
84 struct pptp_gre_header {
85 u8 flags;
86 u8 ver;
87 u16 protocol;
88 u16 payload_len;
89 u16 call_id;
90 u32 seq;
91 u32 ack;
92 } __packed;
94 static struct pppox_sock *lookup_chan(u16 call_id, __be32 s_addr)
96 struct pppox_sock *sock;
97 struct pptp_opt *opt;
99 rcu_read_lock();
100 sock = rcu_dereference(callid_sock[call_id]);
101 if (sock) {
102 opt = &sock->proto.pptp;
103 if (opt->dst_addr.sin_addr.s_addr != s_addr)
104 sock = NULL;
105 else
106 sock_hold(sk_pppox(sock));
108 rcu_read_unlock();
110 return sock;
113 static int lookup_chan_dst(u16 call_id, __be32 d_addr)
115 struct pppox_sock *sock;
116 struct pptp_opt *opt;
117 int i;
119 rcu_read_lock();
120 for (i = find_next_bit(callid_bitmap, MAX_CALLID, 1); i < MAX_CALLID;
121 i = find_next_bit(callid_bitmap, MAX_CALLID, i + 1)) {
122 sock = rcu_dereference(callid_sock[i]);
123 if (!sock)
124 continue;
125 opt = &sock->proto.pptp;
126 if (opt->dst_addr.call_id == call_id &&
127 opt->dst_addr.sin_addr.s_addr == d_addr)
128 break;
130 rcu_read_unlock();
132 return i < MAX_CALLID;
135 static int add_chan(struct pppox_sock *sock)
137 static int call_id;
139 spin_lock(&chan_lock);
140 if (!sock->proto.pptp.src_addr.call_id) {
141 call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, call_id + 1);
142 if (call_id == MAX_CALLID) {
143 call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, 1);
144 if (call_id == MAX_CALLID)
145 goto out_err;
147 sock->proto.pptp.src_addr.call_id = call_id;
148 } else if (test_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap))
149 goto out_err;
151 set_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap);
152 rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id], sock);
153 spin_unlock(&chan_lock);
155 return 0;
157 out_err:
158 spin_unlock(&chan_lock);
159 return -1;
162 static void del_chan(struct pppox_sock *sock)
164 spin_lock(&chan_lock);
165 clear_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap);
166 rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id], NULL);
167 spin_unlock(&chan_lock);
168 synchronize_rcu();
171 static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
173 struct sock *sk = (struct sock *) chan->private;
174 struct pppox_sock *po = pppox_sk(sk);
175 struct pptp_opt *opt = &po->proto.pptp;
176 struct pptp_gre_header *hdr;
177 unsigned int header_len = sizeof(*hdr);
178 int islcp;
179 int len;
180 unsigned char *data;
181 __u32 seq_recv;
184 struct rtable *rt;
185 struct net_device *tdev;
186 struct iphdr *iph;
187 int max_headroom;
189 if (sk_pppox(po)->sk_state & PPPOX_DEAD)
190 goto tx_error;
192 rt = ip_route_output_ports(&init_net, NULL,
193 opt->dst_addr.sin_addr.s_addr,
194 opt->src_addr.sin_addr.s_addr,
195 0, 0, IPPROTO_GRE,
196 RT_TOS(0), 0);
197 if (IS_ERR(rt))
198 goto tx_error;
200 tdev = rt->dst.dev;
202 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph) + sizeof(*hdr) + 2;
204 if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
205 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
206 if (!new_skb) {
207 ip_rt_put(rt);
208 goto tx_error;
210 if (skb->sk)
211 skb_set_owner_w(new_skb, skb->sk);
212 kfree_skb(skb);
213 skb = new_skb;
216 data = skb->data;
217 islcp = ((data[0] << 8) + data[1]) == PPP_LCP && 1 <= data[2] && data[2] <= 7;
219 /* compress protocol field */
220 if ((opt->ppp_flags & SC_COMP_PROT) && data[0] == 0 && !islcp)
221 skb_pull(skb, 1);
223 /* Put in the address/control bytes if necessary */
224 if ((opt->ppp_flags & SC_COMP_AC) == 0 || islcp) {
225 data = skb_push(skb, 2);
226 data[0] = PPP_ALLSTATIONS;
227 data[1] = PPP_UI;
230 len = skb->len;
232 seq_recv = opt->seq_recv;
234 if (opt->ack_sent == seq_recv)
235 header_len -= sizeof(hdr->ack);
237 /* Push down and install GRE header */
238 skb_push(skb, header_len);
239 hdr = (struct pptp_gre_header *)(skb->data);
241 hdr->flags = PPTP_GRE_FLAG_K;
242 hdr->ver = PPTP_GRE_VER;
243 hdr->protocol = htons(PPTP_GRE_PROTO);
244 hdr->call_id = htons(opt->dst_addr.call_id);
246 hdr->flags |= PPTP_GRE_FLAG_S;
247 hdr->seq = htonl(++opt->seq_sent);
248 if (opt->ack_sent != seq_recv) {
249 /* send ack with this message */
250 hdr->ver |= PPTP_GRE_FLAG_A;
251 hdr->ack = htonl(seq_recv);
252 opt->ack_sent = seq_recv;
254 hdr->payload_len = htons(len);
256 /* Push down and install the IP header. */
258 skb_reset_transport_header(skb);
259 skb_push(skb, sizeof(*iph));
260 skb_reset_network_header(skb);
261 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
262 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
264 iph = ip_hdr(skb);
265 iph->version = 4;
266 iph->ihl = sizeof(struct iphdr) >> 2;
267 if (ip_dont_fragment(sk, &rt->dst))
268 iph->frag_off = htons(IP_DF);
269 else
270 iph->frag_off = 0;
271 iph->protocol = IPPROTO_GRE;
272 iph->tos = 0;
273 iph->daddr = rt->rt_dst;
274 iph->saddr = rt->rt_src;
275 iph->ttl = ip4_dst_hoplimit(&rt->dst);
276 iph->tot_len = htons(skb->len);
278 skb_dst_drop(skb);
279 skb_dst_set(skb, &rt->dst);
281 nf_reset(skb);
283 skb->ip_summed = CHECKSUM_NONE;
284 ip_select_ident(iph, &rt->dst, NULL);
285 ip_send_check(iph);
287 ip_local_out(skb);
289 tx_error:
290 return 1;
293 static int pptp_rcv_core(struct sock *sk, struct sk_buff *skb)
295 struct pppox_sock *po = pppox_sk(sk);
296 struct pptp_opt *opt = &po->proto.pptp;
297 int headersize, payload_len, seq;
298 __u8 *payload;
299 struct pptp_gre_header *header;
301 if (!(sk->sk_state & PPPOX_CONNECTED)) {
302 if (sock_queue_rcv_skb(sk, skb))
303 goto drop;
304 return NET_RX_SUCCESS;
307 header = (struct pptp_gre_header *)(skb->data);
309 /* test if acknowledgement present */
310 if (PPTP_GRE_IS_A(header->ver)) {
311 __u32 ack = (PPTP_GRE_IS_S(header->flags)) ?
312 header->ack : header->seq; /* ack in different place if S = 0 */
314 ack = ntohl(ack);
316 if (ack > opt->ack_recv)
317 opt->ack_recv = ack;
318 /* also handle sequence number wrap-around */
319 if (WRAPPED(ack, opt->ack_recv))
320 opt->ack_recv = ack;
323 /* test if payload present */
324 if (!PPTP_GRE_IS_S(header->flags))
325 goto drop;
327 headersize = sizeof(*header);
328 payload_len = ntohs(header->payload_len);
329 seq = ntohl(header->seq);
331 /* no ack present? */
332 if (!PPTP_GRE_IS_A(header->ver))
333 headersize -= sizeof(header->ack);
334 /* check for incomplete packet (length smaller than expected) */
335 if (skb->len - headersize < payload_len)
336 goto drop;
338 payload = skb->data + headersize;
339 /* check for expected sequence number */
340 if (seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq)) {
341 if ((payload[0] == PPP_ALLSTATIONS) && (payload[1] == PPP_UI) &&
342 (PPP_PROTOCOL(payload) == PPP_LCP) &&
343 ((payload[4] == PPP_LCP_ECHOREQ) || (payload[4] == PPP_LCP_ECHOREP)))
344 goto allow_packet;
345 } else {
346 opt->seq_recv = seq;
347 allow_packet:
348 skb_pull(skb, headersize);
350 if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI) {
351 /* chop off address/control */
352 if (skb->len < 3)
353 goto drop;
354 skb_pull(skb, 2);
357 if ((*skb->data) & 1) {
358 /* protocol is compressed */
359 skb_push(skb, 1)[0] = 0;
362 skb->ip_summed = CHECKSUM_NONE;
363 skb_set_network_header(skb, skb->head-skb->data);
364 ppp_input(&po->chan, skb);
366 return NET_RX_SUCCESS;
368 drop:
369 kfree_skb(skb);
370 return NET_RX_DROP;
373 static int pptp_rcv(struct sk_buff *skb)
375 struct pppox_sock *po;
376 struct pptp_gre_header *header;
377 struct iphdr *iph;
379 if (skb->pkt_type != PACKET_HOST)
380 goto drop;
382 if (!pskb_may_pull(skb, 12))
383 goto drop;
385 iph = ip_hdr(skb);
387 header = (struct pptp_gre_header *)skb->data;
389 if (ntohs(header->protocol) != PPTP_GRE_PROTO || /* PPTP-GRE protocol for PPTP */
390 PPTP_GRE_IS_C(header->flags) || /* flag C should be clear */
391 PPTP_GRE_IS_R(header->flags) || /* flag R should be clear */
392 !PPTP_GRE_IS_K(header->flags) || /* flag K should be set */
393 (header->flags&0xF) != 0) /* routing and recursion ctrl = 0 */
394 /* if invalid, discard this packet */
395 goto drop;
397 po = lookup_chan(htons(header->call_id), iph->saddr);
398 if (po) {
399 skb_dst_drop(skb);
400 nf_reset(skb);
401 return sk_receive_skb(sk_pppox(po), skb, 0);
403 drop:
404 kfree_skb(skb);
405 return NET_RX_DROP;
408 static int pptp_bind(struct socket *sock, struct sockaddr *uservaddr,
409 int sockaddr_len)
411 struct sock *sk = sock->sk;
412 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
413 struct pppox_sock *po = pppox_sk(sk);
414 struct pptp_opt *opt = &po->proto.pptp;
415 int error = 0;
417 lock_sock(sk);
419 opt->src_addr = sp->sa_addr.pptp;
420 if (add_chan(po)) {
421 release_sock(sk);
422 error = -EBUSY;
425 release_sock(sk);
426 return error;
429 static int pptp_connect(struct socket *sock, struct sockaddr *uservaddr,
430 int sockaddr_len, int flags)
432 struct sock *sk = sock->sk;
433 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
434 struct pppox_sock *po = pppox_sk(sk);
435 struct pptp_opt *opt = &po->proto.pptp;
436 struct rtable *rt;
437 int error = 0;
439 if (sp->sa_protocol != PX_PROTO_PPTP)
440 return -EINVAL;
442 if (lookup_chan_dst(sp->sa_addr.pptp.call_id, sp->sa_addr.pptp.sin_addr.s_addr))
443 return -EALREADY;
445 lock_sock(sk);
446 /* Check for already bound sockets */
447 if (sk->sk_state & PPPOX_CONNECTED) {
448 error = -EBUSY;
449 goto end;
452 /* Check for already disconnected sockets, on attempts to disconnect */
453 if (sk->sk_state & PPPOX_DEAD) {
454 error = -EALREADY;
455 goto end;
458 if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr) {
459 error = -EINVAL;
460 goto end;
463 po->chan.private = sk;
464 po->chan.ops = &pptp_chan_ops;
466 rt = ip_route_output_ports(&init_net, sk,
467 opt->dst_addr.sin_addr.s_addr,
468 opt->src_addr.sin_addr.s_addr,
469 0, 0,
470 IPPROTO_GRE, RT_CONN_FLAGS(sk), 0);
471 if (IS_ERR(rt)) {
472 error = -EHOSTUNREACH;
473 goto end;
475 sk_setup_caps(sk, &rt->dst);
477 po->chan.mtu = dst_mtu(&rt->dst);
478 if (!po->chan.mtu)
479 po->chan.mtu = PPP_MTU;
480 ip_rt_put(rt);
481 po->chan.mtu -= PPTP_HEADER_OVERHEAD;
483 po->chan.hdrlen = 2 + sizeof(struct pptp_gre_header);
484 error = ppp_register_channel(&po->chan);
485 if (error) {
486 pr_err("PPTP: failed to register PPP channel (%d)\n", error);
487 goto end;
490 opt->dst_addr = sp->sa_addr.pptp;
491 sk->sk_state = PPPOX_CONNECTED;
493 end:
494 release_sock(sk);
495 return error;
498 static int pptp_getname(struct socket *sock, struct sockaddr *uaddr,
499 int *usockaddr_len, int peer)
501 int len = sizeof(struct sockaddr_pppox);
502 struct sockaddr_pppox sp;
504 sp.sa_family = AF_PPPOX;
505 sp.sa_protocol = PX_PROTO_PPTP;
506 sp.sa_addr.pptp = pppox_sk(sock->sk)->proto.pptp.src_addr;
508 memcpy(uaddr, &sp, len);
510 *usockaddr_len = len;
512 return 0;
515 static int pptp_release(struct socket *sock)
517 struct sock *sk = sock->sk;
518 struct pppox_sock *po;
519 struct pptp_opt *opt;
520 int error = 0;
522 if (!sk)
523 return 0;
525 lock_sock(sk);
527 if (sock_flag(sk, SOCK_DEAD)) {
528 release_sock(sk);
529 return -EBADF;
532 po = pppox_sk(sk);
533 opt = &po->proto.pptp;
534 del_chan(po);
536 pppox_unbind_sock(sk);
537 sk->sk_state = PPPOX_DEAD;
539 sock_orphan(sk);
540 sock->sk = NULL;
542 release_sock(sk);
543 sock_put(sk);
545 return error;
548 static void pptp_sock_destruct(struct sock *sk)
550 if (!(sk->sk_state & PPPOX_DEAD)) {
551 del_chan(pppox_sk(sk));
552 pppox_unbind_sock(sk);
554 skb_queue_purge(&sk->sk_receive_queue);
557 static int pptp_create(struct net *net, struct socket *sock)
559 int error = -ENOMEM;
560 struct sock *sk;
561 struct pppox_sock *po;
562 struct pptp_opt *opt;
564 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pptp_sk_proto);
565 if (!sk)
566 goto out;
568 sock_init_data(sock, sk);
570 sock->state = SS_UNCONNECTED;
571 sock->ops = &pptp_ops;
573 sk->sk_backlog_rcv = pptp_rcv_core;
574 sk->sk_state = PPPOX_NONE;
575 sk->sk_type = SOCK_STREAM;
576 sk->sk_family = PF_PPPOX;
577 sk->sk_protocol = PX_PROTO_PPTP;
578 sk->sk_destruct = pptp_sock_destruct;
580 po = pppox_sk(sk);
581 opt = &po->proto.pptp;
583 opt->seq_sent = 0; opt->seq_recv = 0;
584 opt->ack_recv = 0; opt->ack_sent = 0;
586 error = 0;
587 out:
588 return error;
591 static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
592 unsigned long arg)
594 struct sock *sk = (struct sock *) chan->private;
595 struct pppox_sock *po = pppox_sk(sk);
596 struct pptp_opt *opt = &po->proto.pptp;
597 void __user *argp = (void __user *)arg;
598 int __user *p = argp;
599 int err, val;
601 err = -EFAULT;
602 switch (cmd) {
603 case PPPIOCGFLAGS:
604 val = opt->ppp_flags;
605 if (put_user(val, p))
606 break;
607 err = 0;
608 break;
609 case PPPIOCSFLAGS:
610 if (get_user(val, p))
611 break;
612 opt->ppp_flags = val & ~SC_RCV_BITS;
613 err = 0;
614 break;
615 default:
616 err = -ENOTTY;
619 return err;
622 static const struct ppp_channel_ops pptp_chan_ops = {
623 .start_xmit = pptp_xmit,
624 .ioctl = pptp_ppp_ioctl,
627 static struct proto pptp_sk_proto __read_mostly = {
628 .name = "PPTP",
629 .owner = THIS_MODULE,
630 .obj_size = sizeof(struct pppox_sock),
633 static const struct proto_ops pptp_ops = {
634 .family = AF_PPPOX,
635 .owner = THIS_MODULE,
636 .release = pptp_release,
637 .bind = pptp_bind,
638 .connect = pptp_connect,
639 .socketpair = sock_no_socketpair,
640 .accept = sock_no_accept,
641 .getname = pptp_getname,
642 .poll = sock_no_poll,
643 .listen = sock_no_listen,
644 .shutdown = sock_no_shutdown,
645 .setsockopt = sock_no_setsockopt,
646 .getsockopt = sock_no_getsockopt,
647 .sendmsg = sock_no_sendmsg,
648 .recvmsg = sock_no_recvmsg,
649 .mmap = sock_no_mmap,
650 .ioctl = pppox_ioctl,
653 static const struct pppox_proto pppox_pptp_proto = {
654 .create = pptp_create,
655 .owner = THIS_MODULE,
658 static const struct gre_protocol gre_pptp_protocol = {
659 .handler = pptp_rcv,
662 static int __init pptp_init_module(void)
664 int err = 0;
665 pr_info("PPTP driver version " PPTP_DRIVER_VERSION "\n");
667 callid_sock = vzalloc((MAX_CALLID + 1) * sizeof(void *));
668 if (!callid_sock) {
669 pr_err("PPTP: cann't allocate memory\n");
670 return -ENOMEM;
673 err = gre_add_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
674 if (err) {
675 pr_err("PPTP: can't add gre protocol\n");
676 goto out_mem_free;
679 err = proto_register(&pptp_sk_proto, 0);
680 if (err) {
681 pr_err("PPTP: can't register sk_proto\n");
682 goto out_gre_del_protocol;
685 err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto);
686 if (err) {
687 pr_err("PPTP: can't register pppox_proto\n");
688 goto out_unregister_sk_proto;
691 return 0;
693 out_unregister_sk_proto:
694 proto_unregister(&pptp_sk_proto);
695 out_gre_del_protocol:
696 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
697 out_mem_free:
698 vfree(callid_sock);
700 return err;
703 static void __exit pptp_exit_module(void)
705 unregister_pppox_proto(PX_PROTO_PPTP);
706 proto_unregister(&pptp_sk_proto);
707 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
708 vfree(callid_sock);
711 module_init(pptp_init_module);
712 module_exit(pptp_exit_module);
714 MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol");
715 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
716 MODULE_LICENSE("GPL");