blob: 50c60550f295daadfb5d2bb15cc8c3f986004fc4 [file] [log] [blame]
Dmitry Kozlov00959ad2010-08-21 23:05:39 -07001/*
2 * Point-to-Point Tunneling Protocol for Linux
3 *
4 * Authors: Dmitry Kozlov <xeb@mail.ru>
5 *
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.
10 *
11 */
12
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>
Paul Mackerrasbf7daeb2012-03-04 12:56:04 +000026#include <linux/ppp-ioctl.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070027#include <linux/notifier.h>
28#include <linux/file.h>
29#include <linux/in.h>
30#include <linux/ip.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070031#include <linux/rcupdate.h>
32#include <linux/spinlock.h>
33
34#include <net/sock.h>
35#include <net/protocol.h>
36#include <net/ip.h>
37#include <net/icmp.h>
38#include <net/route.h>
39#include <net/gre.h>
Gao Fengab10dcc2016-08-09 12:38:24 +080040#include <net/pptp.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070041
42#include <linux/uaccess.h>
43
44#define PPTP_DRIVER_VERSION "0.8.5"
45
46#define MAX_CALLID 65535
47
48static DECLARE_BITMAP(callid_bitmap, MAX_CALLID + 1);
stephen hemminger469230d2013-08-08 10:27:14 -070049static struct pppox_sock __rcu **callid_sock;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070050
51static DEFINE_SPINLOCK(chan_lock);
52
53static struct proto pptp_sk_proto __read_mostly;
Eric Dumazet756e64a2010-09-21 06:43:54 +000054static const struct ppp_channel_ops pptp_chan_ops;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070055static const struct proto_ops pptp_ops;
56
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070057static struct pppox_sock *lookup_chan(u16 call_id, __be32 s_addr)
58{
59 struct pppox_sock *sock;
60 struct pptp_opt *opt;
61
62 rcu_read_lock();
63 sock = rcu_dereference(callid_sock[call_id]);
64 if (sock) {
65 opt = &sock->proto.pptp;
66 if (opt->dst_addr.sin_addr.s_addr != s_addr)
67 sock = NULL;
68 else
69 sock_hold(sk_pppox(sock));
70 }
71 rcu_read_unlock();
72
73 return sock;
74}
75
76static int lookup_chan_dst(u16 call_id, __be32 d_addr)
77{
78 struct pppox_sock *sock;
79 struct pptp_opt *opt;
80 int i;
81
82 rcu_read_lock();
Akinobu Mita99427742012-04-02 22:47:16 +000083 i = 1;
84 for_each_set_bit_from(i, callid_bitmap, MAX_CALLID) {
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070085 sock = rcu_dereference(callid_sock[i]);
86 if (!sock)
87 continue;
88 opt = &sock->proto.pptp;
89 if (opt->dst_addr.call_id == call_id &&
90 opt->dst_addr.sin_addr.s_addr == d_addr)
91 break;
92 }
93 rcu_read_unlock();
94
95 return i < MAX_CALLID;
96}
97
Hannes Frederic Sowa9a368af2016-01-22 01:39:43 +010098static int add_chan(struct pppox_sock *sock,
99 struct pptp_addr *sa)
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700100{
101 static int call_id;
102
103 spin_lock(&chan_lock);
Hannes Frederic Sowa9a368af2016-01-22 01:39:43 +0100104 if (!sa->call_id) {
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700105 call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, call_id + 1);
106 if (call_id == MAX_CALLID) {
107 call_id = find_next_zero_bit(callid_bitmap, MAX_CALLID, 1);
108 if (call_id == MAX_CALLID)
109 goto out_err;
110 }
Hannes Frederic Sowa9a368af2016-01-22 01:39:43 +0100111 sa->call_id = call_id;
112 } else if (test_bit(sa->call_id, callid_bitmap)) {
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700113 goto out_err;
Hannes Frederic Sowa9a368af2016-01-22 01:39:43 +0100114 }
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700115
Hannes Frederic Sowa9a368af2016-01-22 01:39:43 +0100116 sock->proto.pptp.src_addr = *sa;
117 set_bit(sa->call_id, callid_bitmap);
118 rcu_assign_pointer(callid_sock[sa->call_id], sock);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700119 spin_unlock(&chan_lock);
120
121 return 0;
122
123out_err:
124 spin_unlock(&chan_lock);
125 return -1;
126}
127
128static void del_chan(struct pppox_sock *sock)
129{
130 spin_lock(&chan_lock);
131 clear_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000132 RCU_INIT_POINTER(callid_sock[sock->proto.pptp.src_addr.call_id], NULL);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700133 spin_unlock(&chan_lock);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700134}
135
136static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
137{
138 struct sock *sk = (struct sock *) chan->private;
139 struct pppox_sock *po = pppox_sk(sk);
Eric W. Biedermana7093fe2015-10-07 16:48:43 -0500140 struct net *net = sock_net(sk);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700141 struct pptp_opt *opt = &po->proto.pptp;
142 struct pptp_gre_header *hdr;
143 unsigned int header_len = sizeof(*hdr);
David S. Miller31e4543d2011-05-03 20:25:42 -0700144 struct flowi4 fl4;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700145 int islcp;
146 int len;
147 unsigned char *data;
148 __u32 seq_recv;
149
150
151 struct rtable *rt;
152 struct net_device *tdev;
153 struct iphdr *iph;
154 int max_headroom;
155
156 if (sk_pppox(po)->sk_state & PPPOX_DEAD)
157 goto tx_error;
158
Eric W. Biedermana7093fe2015-10-07 16:48:43 -0500159 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500160 opt->dst_addr.sin_addr.s_addr,
161 opt->src_addr.sin_addr.s_addr,
162 0, 0, IPPROTO_GRE,
163 RT_TOS(0), 0);
164 if (IS_ERR(rt))
165 goto tx_error;
166
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700167 tdev = rt->dst.dev;
168
169 max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph) + sizeof(*hdr) + 2;
170
171 if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
172 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
173 if (!new_skb) {
174 ip_rt_put(rt);
175 goto tx_error;
176 }
177 if (skb->sk)
178 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet968d7012012-05-18 20:23:00 +0000179 consume_skb(skb);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700180 skb = new_skb;
181 }
182
183 data = skb->data;
184 islcp = ((data[0] << 8) + data[1]) == PPP_LCP && 1 <= data[2] && data[2] <= 7;
185
186 /* compress protocol field */
187 if ((opt->ppp_flags & SC_COMP_PROT) && data[0] == 0 && !islcp)
188 skb_pull(skb, 1);
189
190 /* Put in the address/control bytes if necessary */
191 if ((opt->ppp_flags & SC_COMP_AC) == 0 || islcp) {
192 data = skb_push(skb, 2);
193 data[0] = PPP_ALLSTATIONS;
194 data[1] = PPP_UI;
195 }
196
197 len = skb->len;
198
199 seq_recv = opt->seq_recv;
200
201 if (opt->ack_sent == seq_recv)
202 header_len -= sizeof(hdr->ack);
203
204 /* Push down and install GRE header */
205 skb_push(skb, header_len);
206 hdr = (struct pptp_gre_header *)(skb->data);
207
Gao Feng03459342016-08-13 00:30:48 +0800208 hdr->gre_hd.flags = GRE_KEY | GRE_VERSION_1 | GRE_SEQ;
209 hdr->gre_hd.protocol = GRE_PROTO_PPP;
210 hdr->call_id = htons(opt->dst_addr.call_id);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700211
Gao Feng03459342016-08-13 00:30:48 +0800212 hdr->seq = htonl(++opt->seq_sent);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700213 if (opt->ack_sent != seq_recv) {
214 /* send ack with this message */
Gao Feng03459342016-08-13 00:30:48 +0800215 hdr->gre_hd.flags |= GRE_ACK;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700216 hdr->ack = htonl(seq_recv);
217 opt->ack_sent = seq_recv;
218 }
219 hdr->payload_len = htons(len);
220
221 /* Push down and install the IP header. */
222
223 skb_reset_transport_header(skb);
224 skb_push(skb, sizeof(*iph));
225 skb_reset_network_header(skb);
226 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
227 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
228
229 iph = ip_hdr(skb);
230 iph->version = 4;
231 iph->ihl = sizeof(struct iphdr) >> 2;
232 if (ip_dont_fragment(sk, &rt->dst))
233 iph->frag_off = htons(IP_DF);
234 else
235 iph->frag_off = 0;
236 iph->protocol = IPPROTO_GRE;
237 iph->tos = 0;
David S. Miller28c90da2011-05-03 20:41:42 -0700238 iph->daddr = fl4.daddr;
239 iph->saddr = fl4.saddr;
David S. Miller323e1262010-12-12 21:55:08 -0800240 iph->ttl = ip4_dst_hoplimit(&rt->dst);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700241 iph->tot_len = htons(skb->len);
242
243 skb_dst_drop(skb);
244 skb_dst_set(skb, &rt->dst);
245
246 nf_reset(skb);
247
248 skb->ip_summed = CHECKSUM_NONE;
Eric W. Biedermana7093fe2015-10-07 16:48:43 -0500249 ip_select_ident(net, skb, NULL);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700250 ip_send_check(iph);
251
Eric W. Biederman33224b12015-10-07 16:48:46 -0500252 ip_local_out(net, skb->sk, skb);
Eric Dumazet8bae8bd2011-10-17 17:01:47 +0000253 return 1;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700254
255tx_error:
Eric Dumazet8bae8bd2011-10-17 17:01:47 +0000256 kfree_skb(skb);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700257 return 1;
258}
259
260static int pptp_rcv_core(struct sock *sk, struct sk_buff *skb)
261{
262 struct pppox_sock *po = pppox_sk(sk);
263 struct pptp_opt *opt = &po->proto.pptp;
264 int headersize, payload_len, seq;
265 __u8 *payload;
266 struct pptp_gre_header *header;
267
268 if (!(sk->sk_state & PPPOX_CONNECTED)) {
269 if (sock_queue_rcv_skb(sk, skb))
270 goto drop;
271 return NET_RX_SUCCESS;
272 }
273
274 header = (struct pptp_gre_header *)(skb->data);
Eric Dumazet4ea27392011-10-17 17:59:53 +0000275 headersize = sizeof(*header);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700276
277 /* test if acknowledgement present */
Gao Feng03459342016-08-13 00:30:48 +0800278 if (GRE_IS_ACK(header->gre_hd.flags)) {
Eric Dumazet4ea27392011-10-17 17:59:53 +0000279 __u32 ack;
280
281 if (!pskb_may_pull(skb, headersize))
282 goto drop;
283 header = (struct pptp_gre_header *)(skb->data);
284
285 /* ack in different place if S = 0 */
Gao Feng03459342016-08-13 00:30:48 +0800286 ack = GRE_IS_SEQ(header->gre_hd.flags) ? header->ack : header->seq;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700287
288 ack = ntohl(ack);
289
290 if (ack > opt->ack_recv)
291 opt->ack_recv = ack;
292 /* also handle sequence number wrap-around */
293 if (WRAPPED(ack, opt->ack_recv))
294 opt->ack_recv = ack;
Eric Dumazet4ea27392011-10-17 17:59:53 +0000295 } else {
296 headersize -= sizeof(header->ack);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700297 }
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700298 /* test if payload present */
Gao Feng03459342016-08-13 00:30:48 +0800299 if (!GRE_IS_SEQ(header->gre_hd.flags))
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700300 goto drop;
301
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700302 payload_len = ntohs(header->payload_len);
303 seq = ntohl(header->seq);
304
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700305 /* check for incomplete packet (length smaller than expected) */
Eric Dumazet4ea27392011-10-17 17:59:53 +0000306 if (!pskb_may_pull(skb, headersize + payload_len))
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700307 goto drop;
308
309 payload = skb->data + headersize;
310 /* check for expected sequence number */
311 if (seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq)) {
312 if ((payload[0] == PPP_ALLSTATIONS) && (payload[1] == PPP_UI) &&
313 (PPP_PROTOCOL(payload) == PPP_LCP) &&
314 ((payload[4] == PPP_LCP_ECHOREQ) || (payload[4] == PPP_LCP_ECHOREP)))
315 goto allow_packet;
316 } else {
317 opt->seq_recv = seq;
318allow_packet:
319 skb_pull(skb, headersize);
320
321 if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI) {
322 /* chop off address/control */
323 if (skb->len < 3)
324 goto drop;
325 skb_pull(skb, 2);
326 }
327
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700328 skb->ip_summed = CHECKSUM_NONE;
329 skb_set_network_header(skb, skb->head-skb->data);
330 ppp_input(&po->chan, skb);
331
332 return NET_RX_SUCCESS;
333 }
334drop:
335 kfree_skb(skb);
336 return NET_RX_DROP;
337}
338
339static int pptp_rcv(struct sk_buff *skb)
340{
341 struct pppox_sock *po;
342 struct pptp_gre_header *header;
343 struct iphdr *iph;
344
345 if (skb->pkt_type != PACKET_HOST)
346 goto drop;
347
348 if (!pskb_may_pull(skb, 12))
349 goto drop;
350
351 iph = ip_hdr(skb);
352
353 header = (struct pptp_gre_header *)skb->data;
354
Gao Feng03459342016-08-13 00:30:48 +0800355 if (header->gre_hd.protocol != GRE_PROTO_PPP || /* PPTP-GRE protocol for PPTP */
356 GRE_IS_CSUM(header->gre_hd.flags) || /* flag CSUM should be clear */
357 GRE_IS_ROUTING(header->gre_hd.flags) || /* flag ROUTING should be clear */
358 !GRE_IS_KEY(header->gre_hd.flags) || /* flag KEY should be set */
359 (header->gre_hd.flags & GRE_FLAGS)) /* flag Recursion Ctrl should be clear */
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700360 /* if invalid, discard this packet */
361 goto drop;
362
363 po = lookup_chan(htons(header->call_id), iph->saddr);
364 if (po) {
365 skb_dst_drop(skb);
366 nf_reset(skb);
367 return sk_receive_skb(sk_pppox(po), skb, 0);
368 }
369drop:
370 kfree_skb(skb);
371 return NET_RX_DROP;
372}
373
374static int pptp_bind(struct socket *sock, struct sockaddr *uservaddr,
375 int sockaddr_len)
376{
377 struct sock *sk = sock->sk;
378 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
379 struct pppox_sock *po = pppox_sk(sk);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700380 int error = 0;
381
WANG Cong09ccfd22015-12-14 13:48:36 -0800382 if (sockaddr_len < sizeof(struct sockaddr_pppox))
383 return -EINVAL;
384
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700385 lock_sock(sk);
386
Hannes Frederic Sowa9a368af2016-01-22 01:39:43 +0100387 if (sk->sk_state & PPPOX_DEAD) {
388 error = -EALREADY;
389 goto out;
390 }
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700391
Hannes Frederic Sowa9a368af2016-01-22 01:39:43 +0100392 if (sk->sk_state & PPPOX_BOUND) {
393 error = -EBUSY;
394 goto out;
395 }
396
397 if (add_chan(po, &sp->sa_addr.pptp))
398 error = -EBUSY;
399 else
400 sk->sk_state |= PPPOX_BOUND;
401
402out:
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700403 release_sock(sk);
404 return error;
405}
406
407static int pptp_connect(struct socket *sock, struct sockaddr *uservaddr,
408 int sockaddr_len, int flags)
409{
410 struct sock *sk = sock->sk;
411 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
412 struct pppox_sock *po = pppox_sk(sk);
413 struct pptp_opt *opt = &po->proto.pptp;
414 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -0700415 struct flowi4 fl4;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700416 int error = 0;
417
WANG Cong09ccfd22015-12-14 13:48:36 -0800418 if (sockaddr_len < sizeof(struct sockaddr_pppox))
419 return -EINVAL;
420
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700421 if (sp->sa_protocol != PX_PROTO_PPTP)
422 return -EINVAL;
423
424 if (lookup_chan_dst(sp->sa_addr.pptp.call_id, sp->sa_addr.pptp.sin_addr.s_addr))
425 return -EALREADY;
426
427 lock_sock(sk);
428 /* Check for already bound sockets */
429 if (sk->sk_state & PPPOX_CONNECTED) {
430 error = -EBUSY;
431 goto end;
432 }
433
434 /* Check for already disconnected sockets, on attempts to disconnect */
435 if (sk->sk_state & PPPOX_DEAD) {
436 error = -EALREADY;
437 goto end;
438 }
439
440 if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr) {
441 error = -EINVAL;
442 goto end;
443 }
444
445 po->chan.private = sk;
446 po->chan.ops = &pptp_chan_ops;
447
Gao feng08252b32012-08-07 00:23:11 +0000448 rt = ip_route_output_ports(sock_net(sk), &fl4, sk,
David S. Miller78fbfd82011-03-12 00:00:52 -0500449 opt->dst_addr.sin_addr.s_addr,
450 opt->src_addr.sin_addr.s_addr,
451 0, 0,
452 IPPROTO_GRE, RT_CONN_FLAGS(sk), 0);
453 if (IS_ERR(rt)) {
454 error = -EHOSTUNREACH;
455 goto end;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700456 }
David S. Miller78fbfd82011-03-12 00:00:52 -0500457 sk_setup_caps(sk, &rt->dst);
458
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700459 po->chan.mtu = dst_mtu(&rt->dst);
460 if (!po->chan.mtu)
Paul Mackerrasbf7daeb2012-03-04 12:56:04 +0000461 po->chan.mtu = PPP_MRU;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700462 po->chan.mtu -= PPTP_HEADER_OVERHEAD;
463
464 po->chan.hdrlen = 2 + sizeof(struct pptp_gre_header);
465 error = ppp_register_channel(&po->chan);
466 if (error) {
467 pr_err("PPTP: failed to register PPP channel (%d)\n", error);
468 goto end;
469 }
470
471 opt->dst_addr = sp->sa_addr.pptp;
Hannes Frederic Sowa9a368af2016-01-22 01:39:43 +0100472 sk->sk_state |= PPPOX_CONNECTED;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700473
474 end:
475 release_sock(sk);
476 return error;
477}
478
479static int pptp_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100480 int peer)
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700481{
482 int len = sizeof(struct sockaddr_pppox);
483 struct sockaddr_pppox sp;
484
Mathias Krausea5f6fc22014-11-19 18:05:26 +0100485 memset(&sp.sa_addr, 0, sizeof(sp.sa_addr));
486
487 sp.sa_family = AF_PPPOX;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700488 sp.sa_protocol = PX_PROTO_PPTP;
489 sp.sa_addr.pptp = pppox_sk(sock->sk)->proto.pptp.src_addr;
490
491 memcpy(uaddr, &sp, len);
492
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100493 return len;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700494}
495
496static int pptp_release(struct socket *sock)
497{
498 struct sock *sk = sock->sk;
499 struct pppox_sock *po;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700500 int error = 0;
501
502 if (!sk)
503 return 0;
504
505 lock_sock(sk);
506
507 if (sock_flag(sk, SOCK_DEAD)) {
508 release_sock(sk);
509 return -EBADF;
510 }
511
512 po = pppox_sk(sk);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700513 del_chan(po);
Gao Fengddab8282017-07-31 18:07:38 +0800514 synchronize_rcu();
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700515
516 pppox_unbind_sock(sk);
517 sk->sk_state = PPPOX_DEAD;
518
519 sock_orphan(sk);
520 sock->sk = NULL;
521
522 release_sock(sk);
523 sock_put(sk);
524
525 return error;
526}
527
528static void pptp_sock_destruct(struct sock *sk)
529{
530 if (!(sk->sk_state & PPPOX_DEAD)) {
531 del_chan(pppox_sk(sk));
532 pppox_unbind_sock(sk);
533 }
534 skb_queue_purge(&sk->sk_receive_queue);
Xin Long9417d812019-03-13 17:00:48 +0800535 dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1));
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700536}
537
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500538static int pptp_create(struct net *net, struct socket *sock, int kern)
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700539{
540 int error = -ENOMEM;
541 struct sock *sk;
542 struct pppox_sock *po;
543 struct pptp_opt *opt;
544
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500545 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pptp_sk_proto, kern);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700546 if (!sk)
547 goto out;
548
549 sock_init_data(sock, sk);
550
551 sock->state = SS_UNCONNECTED;
552 sock->ops = &pptp_ops;
553
554 sk->sk_backlog_rcv = pptp_rcv_core;
555 sk->sk_state = PPPOX_NONE;
556 sk->sk_type = SOCK_STREAM;
557 sk->sk_family = PF_PPPOX;
558 sk->sk_protocol = PX_PROTO_PPTP;
559 sk->sk_destruct = pptp_sock_destruct;
560
561 po = pppox_sk(sk);
562 opt = &po->proto.pptp;
563
Bradley Peterson91dce7d2012-01-12 10:39:16 +0000564 opt->seq_sent = 0; opt->seq_recv = 0xffffffff;
565 opt->ack_recv = 0; opt->ack_sent = 0xffffffff;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700566
567 error = 0;
568out:
569 return error;
570}
571
572static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
573 unsigned long arg)
574{
575 struct sock *sk = (struct sock *) chan->private;
576 struct pppox_sock *po = pppox_sk(sk);
577 struct pptp_opt *opt = &po->proto.pptp;
578 void __user *argp = (void __user *)arg;
579 int __user *p = argp;
580 int err, val;
581
582 err = -EFAULT;
583 switch (cmd) {
584 case PPPIOCGFLAGS:
585 val = opt->ppp_flags;
586 if (put_user(val, p))
587 break;
588 err = 0;
589 break;
590 case PPPIOCSFLAGS:
591 if (get_user(val, p))
592 break;
593 opt->ppp_flags = val & ~SC_RCV_BITS;
594 err = 0;
595 break;
596 default:
597 err = -ENOTTY;
598 }
599
600 return err;
601}
602
Eric Dumazet756e64a2010-09-21 06:43:54 +0000603static const struct ppp_channel_ops pptp_chan_ops = {
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700604 .start_xmit = pptp_xmit,
605 .ioctl = pptp_ppp_ioctl,
606};
607
608static struct proto pptp_sk_proto __read_mostly = {
609 .name = "PPTP",
610 .owner = THIS_MODULE,
611 .obj_size = sizeof(struct pppox_sock),
612};
613
614static const struct proto_ops pptp_ops = {
615 .family = AF_PPPOX,
616 .owner = THIS_MODULE,
617 .release = pptp_release,
618 .bind = pptp_bind,
619 .connect = pptp_connect,
620 .socketpair = sock_no_socketpair,
621 .accept = sock_no_accept,
622 .getname = pptp_getname,
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700623 .listen = sock_no_listen,
624 .shutdown = sock_no_shutdown,
625 .setsockopt = sock_no_setsockopt,
626 .getsockopt = sock_no_getsockopt,
627 .sendmsg = sock_no_sendmsg,
628 .recvmsg = sock_no_recvmsg,
629 .mmap = sock_no_mmap,
630 .ioctl = pppox_ioctl,
631};
632
Eric Dumazet756e64a2010-09-21 06:43:54 +0000633static const struct pppox_proto pppox_pptp_proto = {
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700634 .create = pptp_create,
635 .owner = THIS_MODULE,
636};
637
Eric Dumazet756e64a2010-09-21 06:43:54 +0000638static const struct gre_protocol gre_pptp_protocol = {
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700639 .handler = pptp_rcv,
640};
641
642static int __init pptp_init_module(void)
643{
644 int err = 0;
645 pr_info("PPTP driver version " PPTP_DRIVER_VERSION "\n");
646
Kees Cookfad953c2018-06-12 14:27:37 -0700647 callid_sock = vzalloc(array_size(sizeof(void *), (MAX_CALLID + 1)));
Joe Perchese404dec2012-01-29 12:56:23 +0000648 if (!callid_sock)
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700649 return -ENOMEM;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700650
651 err = gre_add_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
652 if (err) {
653 pr_err("PPTP: can't add gre protocol\n");
654 goto out_mem_free;
655 }
656
657 err = proto_register(&pptp_sk_proto, 0);
658 if (err) {
659 pr_err("PPTP: can't register sk_proto\n");
660 goto out_gre_del_protocol;
661 }
662
663 err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto);
664 if (err) {
665 pr_err("PPTP: can't register pppox_proto\n");
666 goto out_unregister_sk_proto;
667 }
668
669 return 0;
670
671out_unregister_sk_proto:
672 proto_unregister(&pptp_sk_proto);
673out_gre_del_protocol:
674 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
675out_mem_free:
676 vfree(callid_sock);
677
678 return err;
679}
680
681static void __exit pptp_exit_module(void)
682{
683 unregister_pppox_proto(PX_PROTO_PPTP);
684 proto_unregister(&pptp_sk_proto);
685 gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
686 vfree(callid_sock);
687}
688
689module_init(pptp_init_module);
690module_exit(pptp_exit_module);
691
692MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol");
693MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
694MODULE_LICENSE("GPL");
Guillaume Nault681b4d82015-12-02 16:27:39 +0100695MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_PPTP);