[S390] Remove error checking from copy_oldmem_page()
[linux/fpc-iii.git] / drivers / net / ppp / pptp.c
blob89f829f5f7257fcacd54902d3963520ab5b47525
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/rcupdate.h>
34 #include <linux/spinlock.h>
36 #include <net/sock.h>
37 #include <net/protocol.h>
38 #include <net/ip.h>
39 #include <net/icmp.h>
40 #include <net/route.h>
41 #include <net/gre.h>
43 #include <linux/uaccess.h>
45 #define PPTP_DRIVER_VERSION "0.8.5"
47 #define MAX_CALLID 65535
49 static DECLARE_BITMAP(callid_bitmap, MAX_CALLID + 1);
50 static struct pppox_sock **callid_sock;
52 static DEFINE_SPINLOCK(chan_lock);
54 static struct proto pptp_sk_proto __read_mostly;
55 static const struct ppp_channel_ops pptp_chan_ops;
56 static const struct proto_ops pptp_ops;
58 #define PPP_LCP_ECHOREQ 0x09
59 #define PPP_LCP_ECHOREP 0x0A
60 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
62 #define MISSING_WINDOW 20
63 #define WRAPPED(curseq, lastseq)\
64 ((((curseq) & 0xffffff00) == 0) &&\
65 (((lastseq) & 0xffffff00) == 0xffffff00))
67 #define PPTP_GRE_PROTO 0x880B
68 #define PPTP_GRE_VER 0x1
70 #define PPTP_GRE_FLAG_C 0x80
71 #define PPTP_GRE_FLAG_R 0x40
72 #define PPTP_GRE_FLAG_K 0x20
73 #define PPTP_GRE_FLAG_S 0x10
74 #define PPTP_GRE_FLAG_A 0x80
76 #define PPTP_GRE_IS_C(f) ((f)&PPTP_GRE_FLAG_C)
77 #define PPTP_GRE_IS_R(f) ((f)&PPTP_GRE_FLAG_R)
78 #define PPTP_GRE_IS_K(f) ((f)&PPTP_GRE_FLAG_K)
79 #define PPTP_GRE_IS_S(f) ((f)&PPTP_GRE_FLAG_S)
80 #define PPTP_GRE_IS_A(f) ((f)&PPTP_GRE_FLAG_A)
82 #define PPTP_HEADER_OVERHEAD (2+sizeof(struct pptp_gre_header))
83 struct pptp_gre_header {
84 u8 flags;
85 u8 ver;
86 u16 protocol;
87 u16 payload_len;
88 u16 call_id;
89 u32 seq;
90 u32 ack;
91 } __packed;
93 static struct pppox_sock *lookup_chan(u16 call_id, __be32 s_addr)
95 struct pppox_sock *sock;
96 struct pptp_opt *opt;
98 rcu_read_lock();
99 sock = rcu_dereference(callid_sock[call_id]);
100 if (sock) {
101 opt = &sock->proto.pptp;
102 if (opt->dst_addr.sin_addr.s_addr != s_addr)
103 sock = NULL;
104 else
105 sock_hold(sk_pppox(sock));
107 rcu_read_unlock();
109 return sock;
112 static int lookup_chan_dst(u16 call_id, __be32 d_addr)
114 struct pppox_sock *sock;
115 struct pptp_opt *opt;
116 int i;
118 rcu_read_lock();
119 for (i = find_next_bit(callid_bitmap, MAX_CALLID, 1); i < MAX_CALLID;
120 i = find_next_bit(callid_bitmap, MAX_CALLID, i + 1)) {
121 sock = rcu_dereference(callid_sock[i]);
122 if (!sock)
123 continue;
124 opt = &sock->proto.pptp;
125 if (opt->dst_addr.call_id == call_id &&
126 opt->dst_addr.sin_addr.s_addr == d_addr)
127 break;
129 rcu_read_unlock();
131 return i < MAX_CALLID;
134 static int add_chan(struct pppox_sock *sock)
136 static int call_id;
138 spin_lock(&chan_lock);
139 if (!sock->proto.pptp.src_addr.call_id) {
140 call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, call_id + 1);
141 if (call_id == MAX_CALLID) {
142 call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, 1);
143 if (call_id == MAX_CALLID)
144 goto out_err;
146 sock->proto.pptp.src_addr.call_id = call_id;
147 } else if (test_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap))
148 goto out_err;
150 set_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap);
151 rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id], sock);
152 spin_unlock(&chan_lock);
154 return 0;
156 out_err:
157 spin_unlock(&chan_lock);
158 return -1;
161 static void del_chan(struct pppox_sock *sock)
163 spin_lock(&chan_lock);
164 clear_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap);
165 rcu_assign_pointer(callid_sock[sock->proto.pptp.src_addr.call_id], NULL);
166 spin_unlock(&chan_lock);
167 synchronize_rcu();
170 static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
172 struct sock *sk = (struct sock *) chan->private;
173 struct pppox_sock *po = pppox_sk(sk);
174 struct pptp_opt *opt = &po->proto.pptp;
175 struct pptp_gre_header *hdr;
176 unsigned int header_len = sizeof(*hdr);
177 struct flowi4 fl4;
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, &fl4, 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 = fl4.daddr;
274 iph->saddr = fl4.saddr;
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);
288 return 1;
290 tx_error:
291 kfree_skb(skb);
292 return 1;
295 static int pptp_rcv_core(struct sock *sk, struct sk_buff *skb)
297 struct pppox_sock *po = pppox_sk(sk);
298 struct pptp_opt *opt = &po->proto.pptp;
299 int headersize, payload_len, seq;
300 __u8 *payload;
301 struct pptp_gre_header *header;
303 if (!(sk->sk_state & PPPOX_CONNECTED)) {
304 if (sock_queue_rcv_skb(sk, skb))
305 goto drop;
306 return NET_RX_SUCCESS;
309 header = (struct pptp_gre_header *)(skb->data);
310 headersize = sizeof(*header);
312 /* test if acknowledgement present */
313 if (PPTP_GRE_IS_A(header->ver)) {
314 __u32 ack;
316 if (!pskb_may_pull(skb, headersize))
317 goto drop;
318 header = (struct pptp_gre_header *)(skb->data);
320 /* ack in different place if S = 0 */
321 ack = PPTP_GRE_IS_S(header->flags) ? header->ack : header->seq;
323 ack = ntohl(ack);
325 if (ack > opt->ack_recv)
326 opt->ack_recv = ack;
327 /* also handle sequence number wrap-around */
328 if (WRAPPED(ack, opt->ack_recv))
329 opt->ack_recv = ack;
330 } else {
331 headersize -= sizeof(header->ack);
333 /* test if payload present */
334 if (!PPTP_GRE_IS_S(header->flags))
335 goto drop;
337 payload_len = ntohs(header->payload_len);
338 seq = ntohl(header->seq);
340 /* check for incomplete packet (length smaller than expected) */
341 if (!pskb_may_pull(skb, headersize + payload_len))
342 goto drop;
344 payload = skb->data + headersize;
345 /* check for expected sequence number */
346 if (seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq)) {
347 if ((payload[0] == PPP_ALLSTATIONS) && (payload[1] == PPP_UI) &&
348 (PPP_PROTOCOL(payload) == PPP_LCP) &&
349 ((payload[4] == PPP_LCP_ECHOREQ) || (payload[4] == PPP_LCP_ECHOREP)))
350 goto allow_packet;
351 } else {
352 opt->seq_recv = seq;
353 allow_packet:
354 skb_pull(skb, headersize);
356 if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI) {
357 /* chop off address/control */
358 if (skb->len < 3)
359 goto drop;
360 skb_pull(skb, 2);
363 if ((*skb->data) & 1) {
364 /* protocol is compressed */
365 skb_push(skb, 1)[0] = 0;
368 skb->ip_summed = CHECKSUM_NONE;
369 skb_set_network_header(skb, skb->head-skb->data);
370 ppp_input(&po->chan, skb);
372 return NET_RX_SUCCESS;
374 drop:
375 kfree_skb(skb);
376 return NET_RX_DROP;
379 static int pptp_rcv(struct sk_buff *skb)
381 struct pppox_sock *po;
382 struct pptp_gre_header *header;
383 struct iphdr *iph;
385 if (skb->pkt_type != PACKET_HOST)
386 goto drop;
388 if (!pskb_may_pull(skb, 12))
389 goto drop;
391 iph = ip_hdr(skb);
393 header = (struct pptp_gre_header *)skb->data;
395 if (ntohs(header->protocol) != PPTP_GRE_PROTO || /* PPTP-GRE protocol for PPTP */
396 PPTP_GRE_IS_C(header->flags) || /* flag C should be clear */
397 PPTP_GRE_IS_R(header->flags) || /* flag R should be clear */
398 !PPTP_GRE_IS_K(header->flags) || /* flag K should be set */
399 (header->flags&0xF) != 0) /* routing and recursion ctrl = 0 */
400 /* if invalid, discard this packet */
401 goto drop;
403 po = lookup_chan(htons(header->call_id), iph->saddr);
404 if (po) {
405 skb_dst_drop(skb);
406 nf_reset(skb);
407 return sk_receive_skb(sk_pppox(po), skb, 0);
409 drop:
410 kfree_skb(skb);
411 return NET_RX_DROP;
414 static int pptp_bind(struct socket *sock, struct sockaddr *uservaddr,
415 int sockaddr_len)
417 struct sock *sk = sock->sk;
418 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
419 struct pppox_sock *po = pppox_sk(sk);
420 struct pptp_opt *opt = &po->proto.pptp;
421 int error = 0;
423 lock_sock(sk);
425 opt->src_addr = sp->sa_addr.pptp;
426 if (add_chan(po)) {
427 release_sock(sk);
428 error = -EBUSY;
431 release_sock(sk);
432 return error;
435 static int pptp_connect(struct socket *sock, struct sockaddr *uservaddr,
436 int sockaddr_len, int flags)
438 struct sock *sk = sock->sk;
439 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
440 struct pppox_sock *po = pppox_sk(sk);
441 struct pptp_opt *opt = &po->proto.pptp;
442 struct rtable *rt;
443 struct flowi4 fl4;
444 int error = 0;
446 if (sp->sa_protocol != PX_PROTO_PPTP)
447 return -EINVAL;
449 if (lookup_chan_dst(sp->sa_addr.pptp.call_id, sp->sa_addr.pptp.sin_addr.s_addr))
450 return -EALREADY;
452 lock_sock(sk);
453 /* Check for already bound sockets */
454 if (sk->sk_state & PPPOX_CONNECTED) {
455 error = -EBUSY;
456 goto end;
459 /* Check for already disconnected sockets, on attempts to disconnect */
460 if (sk->sk_state & PPPOX_DEAD) {
461 error = -EALREADY;
462 goto end;
465 if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr) {
466 error = -EINVAL;
467 goto end;
470 po->chan.private = sk;
471 po->chan.ops = &pptp_chan_ops;
473 rt = ip_route_output_ports(&init_net, &fl4, sk,
474 opt->dst_addr.sin_addr.s_addr,
475 opt->src_addr.sin_addr.s_addr,
476 0, 0,
477 IPPROTO_GRE, RT_CONN_FLAGS(sk), 0);
478 if (IS_ERR(rt)) {
479 error = -EHOSTUNREACH;
480 goto end;
482 sk_setup_caps(sk, &rt->dst);
484 po->chan.mtu = dst_mtu(&rt->dst);
485 if (!po->chan.mtu)
486 po->chan.mtu = PPP_MTU;
487 ip_rt_put(rt);
488 po->chan.mtu -= PPTP_HEADER_OVERHEAD;
490 po->chan.hdrlen = 2 + sizeof(struct pptp_gre_header);
491 error = ppp_register_channel(&po->chan);
492 if (error) {
493 pr_err("PPTP: failed to register PPP channel (%d)\n", error);
494 goto end;
497 opt->dst_addr = sp->sa_addr.pptp;
498 sk->sk_state = PPPOX_CONNECTED;
500 end:
501 release_sock(sk);
502 return error;
505 static int pptp_getname(struct socket *sock, struct sockaddr *uaddr,
506 int *usockaddr_len, int peer)
508 int len = sizeof(struct sockaddr_pppox);
509 struct sockaddr_pppox sp;
511 sp.sa_family = AF_PPPOX;
512 sp.sa_protocol = PX_PROTO_PPTP;
513 sp.sa_addr.pptp = pppox_sk(sock->sk)->proto.pptp.src_addr;
515 memcpy(uaddr, &sp, len);
517 *usockaddr_len = len;
519 return 0;
522 static int pptp_release(struct socket *sock)
524 struct sock *sk = sock->sk;
525 struct pppox_sock *po;
526 struct pptp_opt *opt;
527 int error = 0;
529 if (!sk)
530 return 0;
532 lock_sock(sk);
534 if (sock_flag(sk, SOCK_DEAD)) {
535 release_sock(sk);
536 return -EBADF;
539 po = pppox_sk(sk);
540 opt = &po->proto.pptp;
541 del_chan(po);
543 pppox_unbind_sock(sk);
544 sk->sk_state = PPPOX_DEAD;
546 sock_orphan(sk);
547 sock->sk = NULL;
549 release_sock(sk);
550 sock_put(sk);
552 return error;
555 static void pptp_sock_destruct(struct sock *sk)
557 if (!(sk->sk_state & PPPOX_DEAD)) {
558 del_chan(pppox_sk(sk));
559 pppox_unbind_sock(sk);
561 skb_queue_purge(&sk->sk_receive_queue);
564 static int pptp_create(struct net *net, struct socket *sock)
566 int error = -ENOMEM;
567 struct sock *sk;
568 struct pppox_sock *po;
569 struct pptp_opt *opt;
571 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pptp_sk_proto);
572 if (!sk)
573 goto out;
575 sock_init_data(sock, sk);
577 sock->state = SS_UNCONNECTED;
578 sock->ops = &pptp_ops;
580 sk->sk_backlog_rcv = pptp_rcv_core;
581 sk->sk_state = PPPOX_NONE;
582 sk->sk_type = SOCK_STREAM;
583 sk->sk_family = PF_PPPOX;
584 sk->sk_protocol = PX_PROTO_PPTP;
585 sk->sk_destruct = pptp_sock_destruct;
587 po = pppox_sk(sk);
588 opt = &po->proto.pptp;
590 opt->seq_sent = 0; opt->seq_recv = 0;
591 opt->ack_recv = 0; opt->ack_sent = 0;
593 error = 0;
594 out:
595 return error;
598 static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
599 unsigned long arg)
601 struct sock *sk = (struct sock *) chan->private;
602 struct pppox_sock *po = pppox_sk(sk);
603 struct pptp_opt *opt = &po->proto.pptp;
604 void __user *argp = (void __user *)arg;
605 int __user *p = argp;
606 int err, val;
608 err = -EFAULT;
609 switch (cmd) {
610 case PPPIOCGFLAGS:
611 val = opt->ppp_flags;
612 if (put_user(val, p))
613 break;
614 err = 0;
615 break;
616 case PPPIOCSFLAGS:
617 if (get_user(val, p))
618 break;
619 opt->ppp_flags = val & ~SC_RCV_BITS;
620 err = 0;
621 break;
622 default:
623 err = -ENOTTY;
626 return err;
629 static const struct ppp_channel_ops pptp_chan_ops = {
630 .start_xmit = pptp_xmit,
631 .ioctl = pptp_ppp_ioctl,
634 static struct proto pptp_sk_proto __read_mostly = {
635 .name = "PPTP",
636 .owner = THIS_MODULE,
637 .obj_size = sizeof(struct pppox_sock),
640 static const struct proto_ops pptp_ops = {
641 .family = AF_PPPOX,
642 .owner = THIS_MODULE,
643 .release = pptp_release,
644 .bind = pptp_bind,
645 .connect = pptp_connect,
646 .socketpair = sock_no_socketpair,
647 .accept = sock_no_accept,
648 .getname = pptp_getname,
649 .poll = sock_no_poll,
650 .listen = sock_no_listen,
651 .shutdown = sock_no_shutdown,
652 .setsockopt = sock_no_setsockopt,
653 .getsockopt = sock_no_getsockopt,
654 .sendmsg = sock_no_sendmsg,
655 .recvmsg = sock_no_recvmsg,
656 .mmap = sock_no_mmap,
657 .ioctl = pppox_ioctl,
660 static const struct pppox_proto pppox_pptp_proto = {
661 .create = pptp_create,
662 .owner = THIS_MODULE,
665 static const struct gre_protocol gre_pptp_protocol = {
666 .handler = pptp_rcv,
669 static int __init pptp_init_module(void)
671 int err = 0;
672 pr_info("PPTP driver version " PPTP_DRIVER_VERSION "\n");
674 callid_sock = vzalloc((MAX_CALLID + 1) * sizeof(void *));
675 if (!callid_sock) {
676 pr_err("PPTP: cann't allocate memory\n");
677 return -ENOMEM;
680 err = gre_add_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
681 if (err) {
682 pr_err("PPTP: can't add gre protocol\n");
683 goto out_mem_free;
686 err = proto_register(&pptp_sk_proto, 0);
687 if (err) {
688 pr_err("PPTP: can't register sk_proto\n");
689 goto out_gre_del_protocol;
692 err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto);
693 if (err) {
694 pr_err("PPTP: can't register pppox_proto\n");
695 goto out_unregister_sk_proto;
698 return 0;
700 out_unregister_sk_proto:
701 proto_unregister(&pptp_sk_proto);
702 out_gre_del_protocol:
703 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
704 out_mem_free:
705 vfree(callid_sock);
707 return err;
710 static void __exit pptp_exit_module(void)
712 unregister_pppox_proto(PX_PROTO_PPTP);
713 proto_unregister(&pptp_sk_proto);
714 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
715 vfree(callid_sock);
718 module_init(pptp_init_module);
719 module_exit(pptp_exit_module);
721 MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol");
722 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
723 MODULE_LICENSE("GPL");