1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Point-to-Point Tunneling Protocol for Linux
5 * Authors: Dmitry Kozlov <xeb@mail.ru>
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/spinlock.h>
30 #include <net/protocol.h>
33 #include <net/route.h>
37 #include <linux/uaccess.h>
39 #define PPTP_DRIVER_VERSION "0.8.5"
41 #define MAX_CALLID 65535
43 static DECLARE_BITMAP(callid_bitmap
, MAX_CALLID
+ 1);
44 static struct pppox_sock __rcu
**callid_sock
;
46 static DEFINE_SPINLOCK(chan_lock
);
48 static struct proto pptp_sk_proto __read_mostly
;
49 static const struct ppp_channel_ops pptp_chan_ops
;
50 static const struct proto_ops pptp_ops
;
52 static struct pppox_sock
*lookup_chan(u16 call_id
, __be32 s_addr
)
54 struct pppox_sock
*sock
;
58 sock
= rcu_dereference(callid_sock
[call_id
]);
60 opt
= &sock
->proto
.pptp
;
61 if (opt
->dst_addr
.sin_addr
.s_addr
!= s_addr
)
64 sock_hold(sk_pppox(sock
));
71 static int lookup_chan_dst(u16 call_id
, __be32 d_addr
)
73 struct pppox_sock
*sock
;
79 for_each_set_bit_from(i
, callid_bitmap
, MAX_CALLID
) {
80 sock
= rcu_dereference(callid_sock
[i
]);
83 opt
= &sock
->proto
.pptp
;
84 if (opt
->dst_addr
.call_id
== call_id
&&
85 opt
->dst_addr
.sin_addr
.s_addr
== d_addr
)
90 return i
< MAX_CALLID
;
93 static int add_chan(struct pppox_sock
*sock
,
98 spin_lock(&chan_lock
);
100 call_id
= find_next_zero_bit(callid_bitmap
, MAX_CALLID
, call_id
+ 1);
101 if (call_id
== MAX_CALLID
) {
102 call_id
= find_next_zero_bit(callid_bitmap
, MAX_CALLID
, 1);
103 if (call_id
== MAX_CALLID
)
106 sa
->call_id
= call_id
;
107 } else if (test_bit(sa
->call_id
, callid_bitmap
)) {
111 sock
->proto
.pptp
.src_addr
= *sa
;
112 set_bit(sa
->call_id
, callid_bitmap
);
113 rcu_assign_pointer(callid_sock
[sa
->call_id
], sock
);
114 spin_unlock(&chan_lock
);
119 spin_unlock(&chan_lock
);
123 static void del_chan(struct pppox_sock
*sock
)
125 spin_lock(&chan_lock
);
126 clear_bit(sock
->proto
.pptp
.src_addr
.call_id
, callid_bitmap
);
127 RCU_INIT_POINTER(callid_sock
[sock
->proto
.pptp
.src_addr
.call_id
], NULL
);
128 spin_unlock(&chan_lock
);
131 static int pptp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
133 struct sock
*sk
= (struct sock
*) chan
->private;
134 struct pppox_sock
*po
= pppox_sk(sk
);
135 struct net
*net
= sock_net(sk
);
136 struct pptp_opt
*opt
= &po
->proto
.pptp
;
137 struct pptp_gre_header
*hdr
;
138 unsigned int header_len
= sizeof(*hdr
);
147 struct net_device
*tdev
;
151 if (sk_pppox(po
)->sk_state
& PPPOX_DEAD
)
154 rt
= ip_route_output_ports(net
, &fl4
, NULL
,
155 opt
->dst_addr
.sin_addr
.s_addr
,
156 opt
->src_addr
.sin_addr
.s_addr
,
158 RT_TOS(0), sk
->sk_bound_dev_if
);
164 max_headroom
= LL_RESERVED_SPACE(tdev
) + sizeof(*iph
) + sizeof(*hdr
) + 2;
166 if (skb_headroom(skb
) < max_headroom
|| skb_cloned(skb
) || skb_shared(skb
)) {
167 struct sk_buff
*new_skb
= skb_realloc_headroom(skb
, max_headroom
);
173 skb_set_owner_w(new_skb
, skb
->sk
);
179 islcp
= ((data
[0] << 8) + data
[1]) == PPP_LCP
&& 1 <= data
[2] && data
[2] <= 7;
181 /* compress protocol field */
182 if ((opt
->ppp_flags
& SC_COMP_PROT
) && data
[0] == 0 && !islcp
)
185 /* Put in the address/control bytes if necessary */
186 if ((opt
->ppp_flags
& SC_COMP_AC
) == 0 || islcp
) {
187 data
= skb_push(skb
, 2);
188 data
[0] = PPP_ALLSTATIONS
;
194 seq_recv
= opt
->seq_recv
;
196 if (opt
->ack_sent
== seq_recv
)
197 header_len
-= sizeof(hdr
->ack
);
199 /* Push down and install GRE header */
200 skb_push(skb
, header_len
);
201 hdr
= (struct pptp_gre_header
*)(skb
->data
);
203 hdr
->gre_hd
.flags
= GRE_KEY
| GRE_VERSION_1
| GRE_SEQ
;
204 hdr
->gre_hd
.protocol
= GRE_PROTO_PPP
;
205 hdr
->call_id
= htons(opt
->dst_addr
.call_id
);
207 hdr
->seq
= htonl(++opt
->seq_sent
);
208 if (opt
->ack_sent
!= seq_recv
) {
209 /* send ack with this message */
210 hdr
->gre_hd
.flags
|= GRE_ACK
;
211 hdr
->ack
= htonl(seq_recv
);
212 opt
->ack_sent
= seq_recv
;
214 hdr
->payload_len
= htons(len
);
216 /* Push down and install the IP header. */
218 skb_reset_transport_header(skb
);
219 skb_push(skb
, sizeof(*iph
));
220 skb_reset_network_header(skb
);
221 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
222 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
| IPSKB_REROUTED
);
226 iph
->ihl
= sizeof(struct iphdr
) >> 2;
227 if (ip_dont_fragment(sk
, &rt
->dst
))
228 iph
->frag_off
= htons(IP_DF
);
231 iph
->protocol
= IPPROTO_GRE
;
233 iph
->daddr
= fl4
.daddr
;
234 iph
->saddr
= fl4
.saddr
;
235 iph
->ttl
= ip4_dst_hoplimit(&rt
->dst
);
236 iph
->tot_len
= htons(skb
->len
);
239 skb_dst_set(skb
, &rt
->dst
);
243 skb
->ip_summed
= CHECKSUM_NONE
;
244 ip_select_ident(net
, skb
, NULL
);
247 ip_local_out(net
, skb
->sk
, skb
);
255 static int pptp_rcv_core(struct sock
*sk
, struct sk_buff
*skb
)
257 struct pppox_sock
*po
= pppox_sk(sk
);
258 struct pptp_opt
*opt
= &po
->proto
.pptp
;
259 int headersize
, payload_len
, seq
;
261 struct pptp_gre_header
*header
;
263 if (!(sk
->sk_state
& PPPOX_CONNECTED
)) {
264 if (sock_queue_rcv_skb(sk
, skb
))
266 return NET_RX_SUCCESS
;
269 header
= (struct pptp_gre_header
*)(skb
->data
);
270 headersize
= sizeof(*header
);
272 /* test if acknowledgement present */
273 if (GRE_IS_ACK(header
->gre_hd
.flags
)) {
276 if (!pskb_may_pull(skb
, headersize
))
278 header
= (struct pptp_gre_header
*)(skb
->data
);
280 /* ack in different place if S = 0 */
281 ack
= GRE_IS_SEQ(header
->gre_hd
.flags
) ? header
->ack
: header
->seq
;
285 if (ack
> opt
->ack_recv
)
287 /* also handle sequence number wrap-around */
288 if (WRAPPED(ack
, opt
->ack_recv
))
291 headersize
-= sizeof(header
->ack
);
293 /* test if payload present */
294 if (!GRE_IS_SEQ(header
->gre_hd
.flags
))
297 payload_len
= ntohs(header
->payload_len
);
298 seq
= ntohl(header
->seq
);
300 /* check for incomplete packet (length smaller than expected) */
301 if (!pskb_may_pull(skb
, headersize
+ payload_len
))
304 payload
= skb
->data
+ headersize
;
305 /* check for expected sequence number */
306 if (seq
< opt
->seq_recv
+ 1 || WRAPPED(opt
->seq_recv
, seq
)) {
307 if ((payload
[0] == PPP_ALLSTATIONS
) && (payload
[1] == PPP_UI
) &&
308 (PPP_PROTOCOL(payload
) == PPP_LCP
) &&
309 ((payload
[4] == PPP_LCP_ECHOREQ
) || (payload
[4] == PPP_LCP_ECHOREP
)))
314 skb_pull(skb
, headersize
);
316 if (payload
[0] == PPP_ALLSTATIONS
&& payload
[1] == PPP_UI
) {
317 /* chop off address/control */
323 skb
->ip_summed
= CHECKSUM_NONE
;
324 skb_set_network_header(skb
, skb
->head
-skb
->data
);
325 ppp_input(&po
->chan
, skb
);
327 return NET_RX_SUCCESS
;
334 static int pptp_rcv(struct sk_buff
*skb
)
336 struct pppox_sock
*po
;
337 struct pptp_gre_header
*header
;
340 if (skb
->pkt_type
!= PACKET_HOST
)
343 if (!pskb_may_pull(skb
, 12))
348 header
= (struct pptp_gre_header
*)skb
->data
;
350 if (header
->gre_hd
.protocol
!= GRE_PROTO_PPP
|| /* PPTP-GRE protocol for PPTP */
351 GRE_IS_CSUM(header
->gre_hd
.flags
) || /* flag CSUM should be clear */
352 GRE_IS_ROUTING(header
->gre_hd
.flags
) || /* flag ROUTING should be clear */
353 !GRE_IS_KEY(header
->gre_hd
.flags
) || /* flag KEY should be set */
354 (header
->gre_hd
.flags
& GRE_FLAGS
)) /* flag Recursion Ctrl should be clear */
355 /* if invalid, discard this packet */
358 po
= lookup_chan(htons(header
->call_id
), iph
->saddr
);
362 return sk_receive_skb(sk_pppox(po
), skb
, 0);
369 static int pptp_bind(struct socket
*sock
, struct sockaddr
*uservaddr
,
372 struct sock
*sk
= sock
->sk
;
373 struct sockaddr_pppox
*sp
= (struct sockaddr_pppox
*) uservaddr
;
374 struct pppox_sock
*po
= pppox_sk(sk
);
377 if (sockaddr_len
< sizeof(struct sockaddr_pppox
))
382 if (sk
->sk_state
& PPPOX_DEAD
) {
387 if (sk
->sk_state
& PPPOX_BOUND
) {
392 if (add_chan(po
, &sp
->sa_addr
.pptp
))
395 sk
->sk_state
|= PPPOX_BOUND
;
402 static int pptp_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
403 int sockaddr_len
, int flags
)
405 struct sock
*sk
= sock
->sk
;
406 struct sockaddr_pppox
*sp
= (struct sockaddr_pppox
*) uservaddr
;
407 struct pppox_sock
*po
= pppox_sk(sk
);
408 struct pptp_opt
*opt
= &po
->proto
.pptp
;
413 if (sockaddr_len
< sizeof(struct sockaddr_pppox
))
416 if (sp
->sa_protocol
!= PX_PROTO_PPTP
)
419 if (lookup_chan_dst(sp
->sa_addr
.pptp
.call_id
, sp
->sa_addr
.pptp
.sin_addr
.s_addr
))
423 /* Check for already bound sockets */
424 if (sk
->sk_state
& PPPOX_CONNECTED
) {
429 /* Check for already disconnected sockets, on attempts to disconnect */
430 if (sk
->sk_state
& PPPOX_DEAD
) {
435 if (!opt
->src_addr
.sin_addr
.s_addr
|| !sp
->sa_addr
.pptp
.sin_addr
.s_addr
) {
440 po
->chan
.private = sk
;
441 po
->chan
.ops
= &pptp_chan_ops
;
443 rt
= ip_route_output_ports(sock_net(sk
), &fl4
, sk
,
444 opt
->dst_addr
.sin_addr
.s_addr
,
445 opt
->src_addr
.sin_addr
.s_addr
,
447 IPPROTO_GRE
, RT_CONN_FLAGS(sk
),
448 sk
->sk_bound_dev_if
);
450 error
= -EHOSTUNREACH
;
453 sk_setup_caps(sk
, &rt
->dst
);
455 po
->chan
.mtu
= dst_mtu(&rt
->dst
);
457 po
->chan
.mtu
= PPP_MRU
;
458 po
->chan
.mtu
-= PPTP_HEADER_OVERHEAD
;
460 po
->chan
.hdrlen
= 2 + sizeof(struct pptp_gre_header
);
461 error
= ppp_register_channel(&po
->chan
);
463 pr_err("PPTP: failed to register PPP channel (%d)\n", error
);
467 opt
->dst_addr
= sp
->sa_addr
.pptp
;
468 sk
->sk_state
|= PPPOX_CONNECTED
;
475 static int pptp_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
478 int len
= sizeof(struct sockaddr_pppox
);
479 struct sockaddr_pppox sp
;
481 memset(&sp
.sa_addr
, 0, sizeof(sp
.sa_addr
));
483 sp
.sa_family
= AF_PPPOX
;
484 sp
.sa_protocol
= PX_PROTO_PPTP
;
485 sp
.sa_addr
.pptp
= pppox_sk(sock
->sk
)->proto
.pptp
.src_addr
;
487 memcpy(uaddr
, &sp
, len
);
492 static int pptp_release(struct socket
*sock
)
494 struct sock
*sk
= sock
->sk
;
495 struct pppox_sock
*po
;
503 if (sock_flag(sk
, SOCK_DEAD
)) {
512 pppox_unbind_sock(sk
);
513 sk
->sk_state
= PPPOX_DEAD
;
524 static void pptp_sock_destruct(struct sock
*sk
)
526 if (!(sk
->sk_state
& PPPOX_DEAD
)) {
527 del_chan(pppox_sk(sk
));
528 pppox_unbind_sock(sk
);
530 skb_queue_purge(&sk
->sk_receive_queue
);
531 dst_release(rcu_dereference_protected(sk
->sk_dst_cache
, 1));
534 static int pptp_create(struct net
*net
, struct socket
*sock
, int kern
)
538 struct pppox_sock
*po
;
539 struct pptp_opt
*opt
;
541 sk
= sk_alloc(net
, PF_PPPOX
, GFP_KERNEL
, &pptp_sk_proto
, kern
);
545 sock_init_data(sock
, sk
);
547 sock
->state
= SS_UNCONNECTED
;
548 sock
->ops
= &pptp_ops
;
550 sk
->sk_backlog_rcv
= pptp_rcv_core
;
551 sk
->sk_state
= PPPOX_NONE
;
552 sk
->sk_type
= SOCK_STREAM
;
553 sk
->sk_family
= PF_PPPOX
;
554 sk
->sk_protocol
= PX_PROTO_PPTP
;
555 sk
->sk_destruct
= pptp_sock_destruct
;
558 opt
= &po
->proto
.pptp
;
560 opt
->seq_sent
= 0; opt
->seq_recv
= 0xffffffff;
561 opt
->ack_recv
= 0; opt
->ack_sent
= 0xffffffff;
568 static int pptp_ppp_ioctl(struct ppp_channel
*chan
, unsigned int cmd
,
571 struct sock
*sk
= (struct sock
*) chan
->private;
572 struct pppox_sock
*po
= pppox_sk(sk
);
573 struct pptp_opt
*opt
= &po
->proto
.pptp
;
574 void __user
*argp
= (void __user
*)arg
;
575 int __user
*p
= argp
;
581 val
= opt
->ppp_flags
;
582 if (put_user(val
, p
))
587 if (get_user(val
, p
))
589 opt
->ppp_flags
= val
& ~SC_RCV_BITS
;
599 static const struct ppp_channel_ops pptp_chan_ops
= {
600 .start_xmit
= pptp_xmit
,
601 .ioctl
= pptp_ppp_ioctl
,
604 static struct proto pptp_sk_proto __read_mostly
= {
606 .owner
= THIS_MODULE
,
607 .obj_size
= sizeof(struct pppox_sock
),
610 static const struct proto_ops pptp_ops
= {
612 .owner
= THIS_MODULE
,
613 .release
= pptp_release
,
615 .connect
= pptp_connect
,
616 .socketpair
= sock_no_socketpair
,
617 .accept
= sock_no_accept
,
618 .getname
= pptp_getname
,
619 .listen
= sock_no_listen
,
620 .shutdown
= sock_no_shutdown
,
621 .setsockopt
= sock_no_setsockopt
,
622 .getsockopt
= sock_no_getsockopt
,
623 .sendmsg
= sock_no_sendmsg
,
624 .recvmsg
= sock_no_recvmsg
,
625 .mmap
= sock_no_mmap
,
626 .ioctl
= pppox_ioctl
,
628 .compat_ioctl
= pppox_compat_ioctl
,
632 static const struct pppox_proto pppox_pptp_proto
= {
633 .create
= pptp_create
,
634 .owner
= THIS_MODULE
,
637 static const struct gre_protocol gre_pptp_protocol
= {
641 static int __init
pptp_init_module(void)
644 pr_info("PPTP driver version " PPTP_DRIVER_VERSION
"\n");
646 callid_sock
= vzalloc(array_size(sizeof(void *), (MAX_CALLID
+ 1)));
650 err
= gre_add_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
);
652 pr_err("PPTP: can't add gre protocol\n");
656 err
= proto_register(&pptp_sk_proto
, 0);
658 pr_err("PPTP: can't register sk_proto\n");
659 goto out_gre_del_protocol
;
662 err
= register_pppox_proto(PX_PROTO_PPTP
, &pppox_pptp_proto
);
664 pr_err("PPTP: can't register pppox_proto\n");
665 goto out_unregister_sk_proto
;
670 out_unregister_sk_proto
:
671 proto_unregister(&pptp_sk_proto
);
672 out_gre_del_protocol
:
673 gre_del_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
);
680 static void __exit
pptp_exit_module(void)
682 unregister_pppox_proto(PX_PROTO_PPTP
);
683 proto_unregister(&pptp_sk_proto
);
684 gre_del_protocol(&gre_pptp_protocol
, GREPROTO_PPTP
);
688 module_init(pptp_init_module
);
689 module_exit(pptp_exit_module
);
691 MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol");
692 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
693 MODULE_LICENSE("GPL");
694 MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX
, PX_PROTO_PPTP
);