1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Point-to-Point Tunneling Protocol for Linux
8 #include <linux/string.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/errno.h>
13 #include <linux/netdevice.h>
14 #include <linux/net.h>
15 #include <linux/skbuff.h>
16 #include <linux/vmalloc.h>
17 #include <linux/init.h>
18 #include <linux/ppp_channel.h>
19 #include <linux/ppp_defs.h>
20 #include <linux/if_pppox.h>
21 #include <linux/ppp-ioctl.h>
22 #include <linux/notifier.h>
23 #include <linux/file.h>
26 #include <linux/rcupdate.h>
27 #include <linux/security.h>
28 #include <linux/spinlock.h>
31 #include <net/protocol.h>
34 #include <net/route.h>
38 #include <linux/uaccess.h>
40 #define PPTP_DRIVER_VERSION "0.8.5"
42 #define MAX_CALLID 65535
44 static DECLARE_BITMAP(callid_bitmap, MAX_CALLID + 1);
45 static struct pppox_sock __rcu **callid_sock;
47 static DEFINE_SPINLOCK(chan_lock);
49 static struct proto pptp_sk_proto __read_mostly;
50 static const struct ppp_channel_ops pptp_chan_ops;
51 static const struct proto_ops pptp_ops;
53 static struct pppox_sock *lookup_chan(u16 call_id, __be32 s_addr)
55 struct pppox_sock *sock;
59 sock = rcu_dereference(callid_sock[call_id]);
61 opt = &sock->proto.pptp;
62 if (opt->dst_addr.sin_addr.s_addr != s_addr)
65 sock_hold(sk_pppox(sock));
72 static int lookup_chan_dst(u16 call_id, __be32 d_addr)
74 struct pppox_sock *sock;
80 for_each_set_bit_from(i, callid_bitmap, MAX_CALLID) {
81 sock = rcu_dereference(callid_sock[i]);
84 opt = &sock->proto.pptp;
85 if (opt->dst_addr.call_id == call_id &&
86 opt->dst_addr.sin_addr.s_addr == d_addr)
91 return i < MAX_CALLID;
94 static int add_chan(struct pppox_sock *sock,
99 spin_lock(&chan_lock);
101 call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, call_id + 1);
102 if (call_id == MAX_CALLID) {
103 call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, 1);
104 if (call_id == MAX_CALLID)
107 sa->call_id = call_id;
108 } else if (test_bit(sa->call_id, callid_bitmap)) {
112 sock->proto.pptp.src_addr = *sa;
113 set_bit(sa->call_id, callid_bitmap);
114 rcu_assign_pointer(callid_sock[sa->call_id], sock);
115 spin_unlock(&chan_lock);
120 spin_unlock(&chan_lock);
124 static void del_chan(struct pppox_sock *sock)
126 spin_lock(&chan_lock);
127 clear_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap);
128 RCU_INIT_POINTER(callid_sock[sock->proto.pptp.src_addr.call_id], NULL);
129 spin_unlock(&chan_lock);
132 static struct rtable *pptp_route_output(const struct pppox_sock *po,
135 const struct sock *sk = &po->sk;
139 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark, 0,
140 RT_SCOPE_UNIVERSE, IPPROTO_GRE, 0,
141 po->proto.pptp.dst_addr.sin_addr.s_addr,
142 po->proto.pptp.src_addr.sin_addr.s_addr,
143 0, 0, sock_net_uid(net, sk));
144 security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4));
146 return ip_route_output_flow(net, fl4, sk);
149 static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
151 struct sock *sk = chan->private;
152 struct pppox_sock *po = pppox_sk(sk);
153 struct net *net = sock_net(sk);
154 struct pptp_opt *opt = &po->proto.pptp;
155 struct pptp_gre_header *hdr;
156 unsigned int header_len = sizeof(*hdr);
165 struct net_device *tdev;
169 if (sk_pppox(po)->sk_state & PPPOX_DEAD)
172 rt = pptp_route_output(po, &fl4);
178 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph) + sizeof(*hdr) + 2;
180 if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
181 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
187 skb_set_owner_w(new_skb, skb->sk);
193 islcp = ((data[0] << 8) + data[1]) == PPP_LCP && 1 <= data[2] && data[2] <= 7;
195 /* compress protocol field */
196 if ((opt->ppp_flags & SC_COMP_PROT) && data[0] == 0 && !islcp)
199 /* Put in the address/control bytes if necessary */
200 if ((opt->ppp_flags & SC_COMP_AC) == 0 || islcp) {
201 data = skb_push(skb, 2);
202 data[0] = PPP_ALLSTATIONS;
208 seq_recv = opt->seq_recv;
210 if (opt->ack_sent == seq_recv)
211 header_len -= sizeof(hdr->ack);
213 /* Push down and install GRE header */
214 skb_push(skb, header_len);
215 hdr = (struct pptp_gre_header *)(skb->data);
217 hdr->gre_hd.flags = GRE_KEY | GRE_VERSION_1 | GRE_SEQ;
218 hdr->gre_hd.protocol = GRE_PROTO_PPP;
219 hdr->call_id = htons(opt->dst_addr.call_id);
221 hdr->seq = htonl(++opt->seq_sent);
222 if (opt->ack_sent != seq_recv) {
223 /* send ack with this message */
224 hdr->gre_hd.flags |= GRE_ACK;
225 hdr->ack = htonl(seq_recv);
226 opt->ack_sent = seq_recv;
228 hdr->payload_len = htons(len);
230 /* Push down and install the IP header. */
232 skb_reset_transport_header(skb);
233 skb_push(skb, sizeof(*iph));
234 skb_reset_network_header(skb);
235 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
236 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
240 iph->ihl = sizeof(struct iphdr) >> 2;
241 if (ip_dont_fragment(sk, &rt->dst))
242 iph->frag_off = htons(IP_DF);
245 iph->protocol = IPPROTO_GRE;
247 iph->daddr = fl4.daddr;
248 iph->saddr = fl4.saddr;
249 iph->ttl = ip4_dst_hoplimit(&rt->dst);
250 iph->tot_len = htons(skb->len);
253 skb_dst_set(skb, &rt->dst);
257 skb->ip_summed = CHECKSUM_NONE;
258 ip_select_ident(net, skb, NULL);
261 ip_local_out(net, skb->sk, skb);
269 static int pptp_rcv_core(struct sock *sk, struct sk_buff *skb)
271 struct pppox_sock *po = pppox_sk(sk);
272 struct pptp_opt *opt = &po->proto.pptp;
273 int headersize, payload_len, seq;
275 struct pptp_gre_header *header;
277 if (!(sk->sk_state & PPPOX_CONNECTED)) {
278 if (sock_queue_rcv_skb(sk, skb))
280 return NET_RX_SUCCESS;
283 header = (struct pptp_gre_header *)(skb->data);
284 headersize = sizeof(*header);
286 /* test if acknowledgement present */
287 if (GRE_IS_ACK(header->gre_hd.flags)) {
290 if (!pskb_may_pull(skb, headersize))
292 header = (struct pptp_gre_header *)(skb->data);
294 /* ack in different place if S = 0 */
295 ack = GRE_IS_SEQ(header->gre_hd.flags) ? ntohl(header->ack) :
297 if (ack > opt->ack_recv)
299 /* also handle sequence number wrap-around */
300 if (WRAPPED(ack, opt->ack_recv))
303 headersize -= sizeof(header->ack);
305 /* test if payload present */
306 if (!GRE_IS_SEQ(header->gre_hd.flags))
309 payload_len = ntohs(header->payload_len);
310 seq = ntohl(header->seq);
312 /* check for incomplete packet (length smaller than expected) */
313 if (!pskb_may_pull(skb, headersize + payload_len))
316 payload = skb->data + headersize;
317 /* check for expected sequence number */
318 if (seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq)) {
319 if ((payload[0] == PPP_ALLSTATIONS) && (payload[1] == PPP_UI) &&
320 (PPP_PROTOCOL(payload) == PPP_LCP) &&
321 ((payload[4] == PPP_LCP_ECHOREQ) || (payload[4] == PPP_LCP_ECHOREP)))
326 skb_pull(skb, headersize);
328 if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI) {
329 /* chop off address/control */
335 skb->ip_summed = CHECKSUM_NONE;
336 skb_set_network_header(skb, skb->head-skb->data);
337 ppp_input(&po->chan, skb);
339 return NET_RX_SUCCESS;
346 static int pptp_rcv(struct sk_buff *skb)
348 struct pppox_sock *po;
349 struct pptp_gre_header *header;
352 if (skb->pkt_type != PACKET_HOST)
355 if (!pskb_may_pull(skb, 12))
360 header = (struct pptp_gre_header *)skb->data;
362 if (header->gre_hd.protocol != GRE_PROTO_PPP || /* PPTP-GRE protocol for PPTP */
363 GRE_IS_CSUM(header->gre_hd.flags) || /* flag CSUM should be clear */
364 GRE_IS_ROUTING(header->gre_hd.flags) || /* flag ROUTING should be clear */
365 !GRE_IS_KEY(header->gre_hd.flags) || /* flag KEY should be set */
366 (header->gre_hd.flags & GRE_FLAGS)) /* flag Recursion Ctrl should be clear */
367 /* if invalid, discard this packet */
370 po = lookup_chan(ntohs(header->call_id), iph->saddr);
374 return sk_receive_skb(sk_pppox(po), skb, 0);
381 static int pptp_bind(struct socket *sock, struct sockaddr *uservaddr,
384 struct sock *sk = sock->sk;
385 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
386 struct pppox_sock *po = pppox_sk(sk);
389 if (sockaddr_len < sizeof(struct sockaddr_pppox))
394 if (sk->sk_state & PPPOX_DEAD) {
399 if (sk->sk_state & PPPOX_BOUND) {
404 if (add_chan(po, &sp->sa_addr.pptp))
407 sk->sk_state |= PPPOX_BOUND;
414 static int pptp_connect(struct socket *sock, struct sockaddr *uservaddr,
415 int sockaddr_len, int flags)
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;
425 if (sockaddr_len < sizeof(struct sockaddr_pppox))
428 if (sp->sa_protocol != PX_PROTO_PPTP)
431 if (lookup_chan_dst(sp->sa_addr.pptp.call_id, sp->sa_addr.pptp.sin_addr.s_addr))
435 /* Check for already bound sockets */
436 if (sk->sk_state & PPPOX_CONNECTED) {
441 /* Check for already disconnected sockets, on attempts to disconnect */
442 if (sk->sk_state & PPPOX_DEAD) {
447 if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr) {
452 po->chan.private = sk;
453 po->chan.ops = &pptp_chan_ops;
455 rt = pptp_route_output(po, &fl4);
457 error = -EHOSTUNREACH;
460 sk_setup_caps(sk, &rt->dst);
462 po->chan.mtu = dst_mtu(&rt->dst);
464 po->chan.mtu = PPP_MRU;
465 po->chan.mtu -= PPTP_HEADER_OVERHEAD;
467 po->chan.hdrlen = 2 + sizeof(struct pptp_gre_header);
468 error = ppp_register_channel(&po->chan);
470 pr_err("PPTP: failed to register PPP channel (%d)\n", error);
474 opt->dst_addr = sp->sa_addr.pptp;
475 sk->sk_state |= PPPOX_CONNECTED;
482 static int pptp_getname(struct socket *sock, struct sockaddr *uaddr,
485 int len = sizeof(struct sockaddr_pppox);
486 struct sockaddr_pppox sp;
488 memset(&sp.sa_addr, 0, sizeof(sp.sa_addr));
490 sp.sa_family = AF_PPPOX;
491 sp.sa_protocol = PX_PROTO_PPTP;
492 sp.sa_addr.pptp = pppox_sk(sock->sk)->proto.pptp.src_addr;
494 memcpy(uaddr, &sp, len);
499 static int pptp_release(struct socket *sock)
501 struct sock *sk = sock->sk;
502 struct pppox_sock *po;
510 if (sock_flag(sk, SOCK_DEAD)) {
519 pppox_unbind_sock(sk);
520 sk->sk_state = PPPOX_DEAD;
531 static void pptp_sock_destruct(struct sock *sk)
533 if (!(sk->sk_state & PPPOX_DEAD)) {
534 del_chan(pppox_sk(sk));
535 pppox_unbind_sock(sk);
537 skb_queue_purge(&sk->sk_receive_queue);
538 dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1));
541 static int pptp_create(struct net *net, struct socket *sock, int kern)
545 struct pppox_sock *po;
546 struct pptp_opt *opt;
548 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pptp_sk_proto, kern);
552 sock_init_data(sock, sk);
554 sock->state = SS_UNCONNECTED;
555 sock->ops = &pptp_ops;
557 sk->sk_backlog_rcv = pptp_rcv_core;
558 sk->sk_state = PPPOX_NONE;
559 sk->sk_type = SOCK_STREAM;
560 sk->sk_family = PF_PPPOX;
561 sk->sk_protocol = PX_PROTO_PPTP;
562 sk->sk_destruct = pptp_sock_destruct;
565 opt = &po->proto.pptp;
567 opt->seq_sent = 0; opt->seq_recv = 0xffffffff;
568 opt->ack_recv = 0; opt->ack_sent = 0xffffffff;
575 static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
578 struct sock *sk = chan->private;
579 struct pppox_sock *po = pppox_sk(sk);
580 struct pptp_opt *opt = &po->proto.pptp;
581 void __user *argp = (void __user *)arg;
582 int __user *p = argp;
588 val = opt->ppp_flags;
589 if (put_user(val, p))
594 if (get_user(val, p))
596 opt->ppp_flags = val & ~SC_RCV_BITS;
606 static const struct ppp_channel_ops pptp_chan_ops = {
607 .start_xmit = pptp_xmit,
608 .ioctl = pptp_ppp_ioctl,
611 static struct proto pptp_sk_proto __read_mostly = {
613 .owner = THIS_MODULE,
614 .obj_size = sizeof(struct pppox_sock),
617 static const struct proto_ops pptp_ops = {
619 .owner = THIS_MODULE,
620 .release = pptp_release,
622 .connect = pptp_connect,
623 .socketpair = sock_no_socketpair,
624 .accept = sock_no_accept,
625 .getname = pptp_getname,
626 .listen = sock_no_listen,
627 .shutdown = sock_no_shutdown,
628 .sendmsg = sock_no_sendmsg,
629 .recvmsg = sock_no_recvmsg,
630 .mmap = sock_no_mmap,
631 .ioctl = pppox_ioctl,
633 .compat_ioctl = pppox_compat_ioctl,
637 static const struct pppox_proto pppox_pptp_proto = {
638 .create = pptp_create,
639 .owner = THIS_MODULE,
642 static const struct gre_protocol gre_pptp_protocol = {
646 static int __init pptp_init_module(void)
649 pr_info("PPTP driver version " PPTP_DRIVER_VERSION "\n");
651 callid_sock = vzalloc(array_size(sizeof(void *), (MAX_CALLID + 1)));
655 err = gre_add_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
657 pr_err("PPTP: can't add gre protocol\n");
661 err = proto_register(&pptp_sk_proto, 0);
663 pr_err("PPTP: can't register sk_proto\n");
664 goto out_gre_del_protocol;
667 err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto);
669 pr_err("PPTP: can't register pppox_proto\n");
670 goto out_unregister_sk_proto;
675 out_unregister_sk_proto:
676 proto_unregister(&pptp_sk_proto);
677 out_gre_del_protocol:
678 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
685 static void __exit pptp_exit_module(void)
687 unregister_pppox_proto(PX_PROTO_PPTP);
688 proto_unregister(&pptp_sk_proto);
689 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
693 module_init(pptp_init_module);
694 module_exit(pptp_exit_module);
696 MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol");
698 MODULE_LICENSE("GPL");
699 MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_PPTP);