1 /*
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>
26 #include <linux/ppp-ioctl.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>
35 
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>
42 
43 #include <linux/uaccess.h>
44 
45 #define PPTP_DRIVER_VERSION "0.8.5"
46 
47 #define MAX_CALLID 65535
48 
49 static DECLARE_BITMAP(callid_bitmap, MAX_CALLID + 1);
50 static struct pppox_sock __rcu **callid_sock;
51 
52 static DEFINE_SPINLOCK(chan_lock);
53 
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;
57 
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)
61 
62 #define MISSING_WINDOW 20
63 #define WRAPPED(curseq, lastseq)\
64 	((((curseq) & 0xffffff00) == 0) &&\
65 	(((lastseq) & 0xffffff00) == 0xffffff00))
66 
67 #define PPTP_GRE_PROTO  0x880B
68 #define PPTP_GRE_VER    0x1
69 
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
75 
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)
81 
82 #define PPTP_HEADER_OVERHEAD (2+sizeof(struct pptp_gre_header))
83 struct pptp_gre_header {
84 	u8  flags;
85 	u8  ver;
86 	__be16 protocol;
87 	__be16 payload_len;
88 	__be16 call_id;
89 	__be32 seq;
90 	__be32 ack;
91 } __packed;
92 
lookup_chan(u16 call_id,__be32 s_addr)93 static struct pppox_sock *lookup_chan(u16 call_id, __be32 s_addr)
94 {
95 	struct pppox_sock *sock;
96 	struct pptp_opt *opt;
97 
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));
106 	}
107 	rcu_read_unlock();
108 
109 	return sock;
110 }
111 
lookup_chan_dst(u16 call_id,__be32 d_addr)112 static int lookup_chan_dst(u16 call_id, __be32 d_addr)
113 {
114 	struct pppox_sock *sock;
115 	struct pptp_opt *opt;
116 	int i;
117 
118 	rcu_read_lock();
119 	i = 1;
120 	for_each_set_bit_from(i, callid_bitmap, MAX_CALLID) {
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;
128 	}
129 	rcu_read_unlock();
130 
131 	return i < MAX_CALLID;
132 }
133 
add_chan(struct pppox_sock * sock,struct pptp_addr * sa)134 static int add_chan(struct pppox_sock *sock,
135 		    struct pptp_addr *sa)
136 {
137 	static int call_id;
138 
139 	spin_lock(&chan_lock);
140 	if (!sa->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;
146 		}
147 		sa->call_id = call_id;
148 	} else if (test_bit(sa->call_id, callid_bitmap)) {
149 		goto out_err;
150 	}
151 
152 	sock->proto.pptp.src_addr = *sa;
153 	set_bit(sa->call_id, callid_bitmap);
154 	rcu_assign_pointer(callid_sock[sa->call_id], sock);
155 	spin_unlock(&chan_lock);
156 
157 	return 0;
158 
159 out_err:
160 	spin_unlock(&chan_lock);
161 	return -1;
162 }
163 
del_chan(struct pppox_sock * sock)164 static void del_chan(struct pppox_sock *sock)
165 {
166 	spin_lock(&chan_lock);
167 	clear_bit(sock->proto.pptp.src_addr.call_id, callid_bitmap);
168 	RCU_INIT_POINTER(callid_sock[sock->proto.pptp.src_addr.call_id], NULL);
169 	spin_unlock(&chan_lock);
170 	synchronize_rcu();
171 }
172 
pptp_xmit(struct ppp_channel * chan,struct sk_buff * skb)173 static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
174 {
175 	struct sock *sk = (struct sock *) chan->private;
176 	struct pppox_sock *po = pppox_sk(sk);
177 	struct pptp_opt *opt = &po->proto.pptp;
178 	struct pptp_gre_header *hdr;
179 	unsigned int header_len = sizeof(*hdr);
180 	struct flowi4 fl4;
181 	int islcp;
182 	int len;
183 	unsigned char *data;
184 	__u32 seq_recv;
185 
186 
187 	struct rtable *rt;
188 	struct net_device *tdev;
189 	struct iphdr  *iph;
190 	int    max_headroom;
191 
192 	if (sk_pppox(po)->sk_state & PPPOX_DEAD)
193 		goto tx_error;
194 
195 	rt = ip_route_output_ports(sock_net(sk), &fl4, NULL,
196 				   opt->dst_addr.sin_addr.s_addr,
197 				   opt->src_addr.sin_addr.s_addr,
198 				   0, 0, IPPROTO_GRE,
199 				   RT_TOS(0), 0);
200 	if (IS_ERR(rt))
201 		goto tx_error;
202 
203 	tdev = rt->dst.dev;
204 
205 	max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(*iph) + sizeof(*hdr) + 2;
206 
207 	if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
208 		struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
209 		if (!new_skb) {
210 			ip_rt_put(rt);
211 			goto tx_error;
212 		}
213 		if (skb->sk)
214 			skb_set_owner_w(new_skb, skb->sk);
215 		consume_skb(skb);
216 		skb = new_skb;
217 	}
218 
219 	data = skb->data;
220 	islcp = ((data[0] << 8) + data[1]) == PPP_LCP && 1 <= data[2] && data[2] <= 7;
221 
222 	/* compress protocol field */
223 	if ((opt->ppp_flags & SC_COMP_PROT) && data[0] == 0 && !islcp)
224 		skb_pull(skb, 1);
225 
226 	/* Put in the address/control bytes if necessary */
227 	if ((opt->ppp_flags & SC_COMP_AC) == 0 || islcp) {
228 		data = skb_push(skb, 2);
229 		data[0] = PPP_ALLSTATIONS;
230 		data[1] = PPP_UI;
231 	}
232 
233 	len = skb->len;
234 
235 	seq_recv = opt->seq_recv;
236 
237 	if (opt->ack_sent == seq_recv)
238 		header_len -= sizeof(hdr->ack);
239 
240 	/* Push down and install GRE header */
241 	skb_push(skb, header_len);
242 	hdr = (struct pptp_gre_header *)(skb->data);
243 
244 	hdr->flags       = PPTP_GRE_FLAG_K;
245 	hdr->ver         = PPTP_GRE_VER;
246 	hdr->protocol    = htons(PPTP_GRE_PROTO);
247 	hdr->call_id     = htons(opt->dst_addr.call_id);
248 
249 	hdr->flags      |= PPTP_GRE_FLAG_S;
250 	hdr->seq         = htonl(++opt->seq_sent);
251 	if (opt->ack_sent != seq_recv)	{
252 		/* send ack with this message */
253 		hdr->ver |= PPTP_GRE_FLAG_A;
254 		hdr->ack  = htonl(seq_recv);
255 		opt->ack_sent = seq_recv;
256 	}
257 	hdr->payload_len = htons(len);
258 
259 	/*	Push down and install the IP header. */
260 
261 	skb_reset_transport_header(skb);
262 	skb_push(skb, sizeof(*iph));
263 	skb_reset_network_header(skb);
264 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
265 	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
266 
267 	iph =	ip_hdr(skb);
268 	iph->version =	4;
269 	iph->ihl =	sizeof(struct iphdr) >> 2;
270 	if (ip_dont_fragment(sk, &rt->dst))
271 		iph->frag_off	=	htons(IP_DF);
272 	else
273 		iph->frag_off	=	0;
274 	iph->protocol = IPPROTO_GRE;
275 	iph->tos      = 0;
276 	iph->daddr    = fl4.daddr;
277 	iph->saddr    = fl4.saddr;
278 	iph->ttl      = ip4_dst_hoplimit(&rt->dst);
279 	iph->tot_len  = htons(skb->len);
280 
281 	skb_dst_drop(skb);
282 	skb_dst_set(skb, &rt->dst);
283 
284 	nf_reset(skb);
285 
286 	skb->ip_summed = CHECKSUM_NONE;
287 	ip_select_ident(sock_net(sk), skb, NULL);
288 	ip_send_check(iph);
289 
290 	ip_local_out(skb);
291 	return 1;
292 
293 tx_error:
294 	kfree_skb(skb);
295 	return 1;
296 }
297 
pptp_rcv_core(struct sock * sk,struct sk_buff * skb)298 static int pptp_rcv_core(struct sock *sk, struct sk_buff *skb)
299 {
300 	struct pppox_sock *po = pppox_sk(sk);
301 	struct pptp_opt *opt = &po->proto.pptp;
302 	int headersize, payload_len, seq;
303 	__u8 *payload;
304 	struct pptp_gre_header *header;
305 
306 	if (!(sk->sk_state & PPPOX_CONNECTED)) {
307 		if (sock_queue_rcv_skb(sk, skb))
308 			goto drop;
309 		return NET_RX_SUCCESS;
310 	}
311 
312 	header = (struct pptp_gre_header *)(skb->data);
313 	headersize  = sizeof(*header);
314 
315 	/* test if acknowledgement present */
316 	if (PPTP_GRE_IS_A(header->ver)) {
317 		__u32 ack;
318 
319 		if (!pskb_may_pull(skb, headersize))
320 			goto drop;
321 		header = (struct pptp_gre_header *)(skb->data);
322 
323 		/* ack in different place if S = 0 */
324 		ack = PPTP_GRE_IS_S(header->flags) ? header->ack : header->seq;
325 
326 		ack = ntohl(ack);
327 
328 		if (ack > opt->ack_recv)
329 			opt->ack_recv = ack;
330 		/* also handle sequence number wrap-around  */
331 		if (WRAPPED(ack, opt->ack_recv))
332 			opt->ack_recv = ack;
333 	} else {
334 		headersize -= sizeof(header->ack);
335 	}
336 	/* test if payload present */
337 	if (!PPTP_GRE_IS_S(header->flags))
338 		goto drop;
339 
340 	payload_len = ntohs(header->payload_len);
341 	seq         = ntohl(header->seq);
342 
343 	/* check for incomplete packet (length smaller than expected) */
344 	if (!pskb_may_pull(skb, headersize + payload_len))
345 		goto drop;
346 
347 	payload = skb->data + headersize;
348 	/* check for expected sequence number */
349 	if (seq < opt->seq_recv + 1 || WRAPPED(opt->seq_recv, seq)) {
350 		if ((payload[0] == PPP_ALLSTATIONS) && (payload[1] == PPP_UI) &&
351 				(PPP_PROTOCOL(payload) == PPP_LCP) &&
352 				((payload[4] == PPP_LCP_ECHOREQ) || (payload[4] == PPP_LCP_ECHOREP)))
353 			goto allow_packet;
354 	} else {
355 		opt->seq_recv = seq;
356 allow_packet:
357 		skb_pull(skb, headersize);
358 
359 		if (payload[0] == PPP_ALLSTATIONS && payload[1] == PPP_UI) {
360 			/* chop off address/control */
361 			if (skb->len < 3)
362 				goto drop;
363 			skb_pull(skb, 2);
364 		}
365 
366 		if ((*skb->data) & 1) {
367 			/* protocol is compressed */
368 			skb_push(skb, 1)[0] = 0;
369 		}
370 
371 		skb->ip_summed = CHECKSUM_NONE;
372 		skb_set_network_header(skb, skb->head-skb->data);
373 		ppp_input(&po->chan, skb);
374 
375 		return NET_RX_SUCCESS;
376 	}
377 drop:
378 	kfree_skb(skb);
379 	return NET_RX_DROP;
380 }
381 
pptp_rcv(struct sk_buff * skb)382 static int pptp_rcv(struct sk_buff *skb)
383 {
384 	struct pppox_sock *po;
385 	struct pptp_gre_header *header;
386 	struct iphdr *iph;
387 
388 	if (skb->pkt_type != PACKET_HOST)
389 		goto drop;
390 
391 	if (!pskb_may_pull(skb, 12))
392 		goto drop;
393 
394 	iph = ip_hdr(skb);
395 
396 	header = (struct pptp_gre_header *)skb->data;
397 
398 	if (ntohs(header->protocol) != PPTP_GRE_PROTO || /* PPTP-GRE protocol for PPTP */
399 		PPTP_GRE_IS_C(header->flags) ||                /* flag C should be clear */
400 		PPTP_GRE_IS_R(header->flags) ||                /* flag R should be clear */
401 		!PPTP_GRE_IS_K(header->flags) ||               /* flag K should be set */
402 		(header->flags&0xF) != 0)                      /* routing and recursion ctrl = 0 */
403 		/* if invalid, discard this packet */
404 		goto drop;
405 
406 	po = lookup_chan(htons(header->call_id), iph->saddr);
407 	if (po) {
408 		skb_dst_drop(skb);
409 		nf_reset(skb);
410 		return sk_receive_skb(sk_pppox(po), skb, 0);
411 	}
412 drop:
413 	kfree_skb(skb);
414 	return NET_RX_DROP;
415 }
416 
pptp_bind(struct socket * sock,struct sockaddr * uservaddr,int sockaddr_len)417 static int pptp_bind(struct socket *sock, struct sockaddr *uservaddr,
418 	int sockaddr_len)
419 {
420 	struct sock *sk = sock->sk;
421 	struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
422 	struct pppox_sock *po = pppox_sk(sk);
423 	int error = 0;
424 
425 	if (sockaddr_len < sizeof(struct sockaddr_pppox))
426 		return -EINVAL;
427 
428 	lock_sock(sk);
429 
430 	if (sk->sk_state & PPPOX_DEAD) {
431 		error = -EALREADY;
432 		goto out;
433 	}
434 
435 	if (sk->sk_state & PPPOX_BOUND) {
436 		error = -EBUSY;
437 		goto out;
438 	}
439 
440 	if (add_chan(po, &sp->sa_addr.pptp))
441 		error = -EBUSY;
442 	else
443 		sk->sk_state |= PPPOX_BOUND;
444 
445 out:
446 	release_sock(sk);
447 	return error;
448 }
449 
pptp_connect(struct socket * sock,struct sockaddr * uservaddr,int sockaddr_len,int flags)450 static int pptp_connect(struct socket *sock, struct sockaddr *uservaddr,
451 	int sockaddr_len, int flags)
452 {
453 	struct sock *sk = sock->sk;
454 	struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
455 	struct pppox_sock *po = pppox_sk(sk);
456 	struct pptp_opt *opt = &po->proto.pptp;
457 	struct rtable *rt;
458 	struct flowi4 fl4;
459 	int error = 0;
460 
461 	if (sockaddr_len < sizeof(struct sockaddr_pppox))
462 		return -EINVAL;
463 
464 	if (sp->sa_protocol != PX_PROTO_PPTP)
465 		return -EINVAL;
466 
467 	if (lookup_chan_dst(sp->sa_addr.pptp.call_id, sp->sa_addr.pptp.sin_addr.s_addr))
468 		return -EALREADY;
469 
470 	lock_sock(sk);
471 	/* Check for already bound sockets */
472 	if (sk->sk_state & PPPOX_CONNECTED) {
473 		error = -EBUSY;
474 		goto end;
475 	}
476 
477 	/* Check for already disconnected sockets, on attempts to disconnect */
478 	if (sk->sk_state & PPPOX_DEAD) {
479 		error = -EALREADY;
480 		goto end;
481 	}
482 
483 	if (!opt->src_addr.sin_addr.s_addr || !sp->sa_addr.pptp.sin_addr.s_addr) {
484 		error = -EINVAL;
485 		goto end;
486 	}
487 
488 	po->chan.private = sk;
489 	po->chan.ops = &pptp_chan_ops;
490 
491 	rt = ip_route_output_ports(sock_net(sk), &fl4, sk,
492 				   opt->dst_addr.sin_addr.s_addr,
493 				   opt->src_addr.sin_addr.s_addr,
494 				   0, 0,
495 				   IPPROTO_GRE, RT_CONN_FLAGS(sk), 0);
496 	if (IS_ERR(rt)) {
497 		error = -EHOSTUNREACH;
498 		goto end;
499 	}
500 	sk_setup_caps(sk, &rt->dst);
501 
502 	po->chan.mtu = dst_mtu(&rt->dst);
503 	if (!po->chan.mtu)
504 		po->chan.mtu = PPP_MRU;
505 	ip_rt_put(rt);
506 	po->chan.mtu -= PPTP_HEADER_OVERHEAD;
507 
508 	po->chan.hdrlen = 2 + sizeof(struct pptp_gre_header);
509 	error = ppp_register_channel(&po->chan);
510 	if (error) {
511 		pr_err("PPTP: failed to register PPP channel (%d)\n", error);
512 		goto end;
513 	}
514 
515 	opt->dst_addr = sp->sa_addr.pptp;
516 	sk->sk_state |= PPPOX_CONNECTED;
517 
518  end:
519 	release_sock(sk);
520 	return error;
521 }
522 
pptp_getname(struct socket * sock,struct sockaddr * uaddr,int * usockaddr_len,int peer)523 static int pptp_getname(struct socket *sock, struct sockaddr *uaddr,
524 	int *usockaddr_len, int peer)
525 {
526 	int len = sizeof(struct sockaddr_pppox);
527 	struct sockaddr_pppox sp;
528 
529 	memset(&sp.sa_addr, 0, sizeof(sp.sa_addr));
530 
531 	sp.sa_family    = AF_PPPOX;
532 	sp.sa_protocol  = PX_PROTO_PPTP;
533 	sp.sa_addr.pptp = pppox_sk(sock->sk)->proto.pptp.src_addr;
534 
535 	memcpy(uaddr, &sp, len);
536 
537 	*usockaddr_len = len;
538 
539 	return 0;
540 }
541 
pptp_release(struct socket * sock)542 static int pptp_release(struct socket *sock)
543 {
544 	struct sock *sk = sock->sk;
545 	struct pppox_sock *po;
546 	struct pptp_opt *opt;
547 	int error = 0;
548 
549 	if (!sk)
550 		return 0;
551 
552 	lock_sock(sk);
553 
554 	if (sock_flag(sk, SOCK_DEAD)) {
555 		release_sock(sk);
556 		return -EBADF;
557 	}
558 
559 	po = pppox_sk(sk);
560 	opt = &po->proto.pptp;
561 	del_chan(po);
562 
563 	pppox_unbind_sock(sk);
564 	sk->sk_state = PPPOX_DEAD;
565 
566 	sock_orphan(sk);
567 	sock->sk = NULL;
568 
569 	release_sock(sk);
570 	sock_put(sk);
571 
572 	return error;
573 }
574 
pptp_sock_destruct(struct sock * sk)575 static void pptp_sock_destruct(struct sock *sk)
576 {
577 	if (!(sk->sk_state & PPPOX_DEAD)) {
578 		del_chan(pppox_sk(sk));
579 		pppox_unbind_sock(sk);
580 	}
581 	skb_queue_purge(&sk->sk_receive_queue);
582 }
583 
pptp_create(struct net * net,struct socket * sock)584 static int pptp_create(struct net *net, struct socket *sock)
585 {
586 	int error = -ENOMEM;
587 	struct sock *sk;
588 	struct pppox_sock *po;
589 	struct pptp_opt *opt;
590 
591 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pptp_sk_proto);
592 	if (!sk)
593 		goto out;
594 
595 	sock_init_data(sock, sk);
596 
597 	sock->state = SS_UNCONNECTED;
598 	sock->ops   = &pptp_ops;
599 
600 	sk->sk_backlog_rcv = pptp_rcv_core;
601 	sk->sk_state       = PPPOX_NONE;
602 	sk->sk_type        = SOCK_STREAM;
603 	sk->sk_family      = PF_PPPOX;
604 	sk->sk_protocol    = PX_PROTO_PPTP;
605 	sk->sk_destruct    = pptp_sock_destruct;
606 
607 	po = pppox_sk(sk);
608 	opt = &po->proto.pptp;
609 
610 	opt->seq_sent = 0; opt->seq_recv = 0xffffffff;
611 	opt->ack_recv = 0; opt->ack_sent = 0xffffffff;
612 
613 	error = 0;
614 out:
615 	return error;
616 }
617 
pptp_ppp_ioctl(struct ppp_channel * chan,unsigned int cmd,unsigned long arg)618 static int pptp_ppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
619 	unsigned long arg)
620 {
621 	struct sock *sk = (struct sock *) chan->private;
622 	struct pppox_sock *po = pppox_sk(sk);
623 	struct pptp_opt *opt = &po->proto.pptp;
624 	void __user *argp = (void __user *)arg;
625 	int __user *p = argp;
626 	int err, val;
627 
628 	err = -EFAULT;
629 	switch (cmd) {
630 	case PPPIOCGFLAGS:
631 		val = opt->ppp_flags;
632 		if (put_user(val, p))
633 			break;
634 		err = 0;
635 		break;
636 	case PPPIOCSFLAGS:
637 		if (get_user(val, p))
638 			break;
639 		opt->ppp_flags = val & ~SC_RCV_BITS;
640 		err = 0;
641 		break;
642 	default:
643 		err = -ENOTTY;
644 	}
645 
646 	return err;
647 }
648 
649 static const struct ppp_channel_ops pptp_chan_ops = {
650 	.start_xmit = pptp_xmit,
651 	.ioctl      = pptp_ppp_ioctl,
652 };
653 
654 static struct proto pptp_sk_proto __read_mostly = {
655 	.name     = "PPTP",
656 	.owner    = THIS_MODULE,
657 	.obj_size = sizeof(struct pppox_sock),
658 };
659 
660 static const struct proto_ops pptp_ops = {
661 	.family     = AF_PPPOX,
662 	.owner      = THIS_MODULE,
663 	.release    = pptp_release,
664 	.bind       = pptp_bind,
665 	.connect    = pptp_connect,
666 	.socketpair = sock_no_socketpair,
667 	.accept     = sock_no_accept,
668 	.getname    = pptp_getname,
669 	.poll       = sock_no_poll,
670 	.listen     = sock_no_listen,
671 	.shutdown   = sock_no_shutdown,
672 	.setsockopt = sock_no_setsockopt,
673 	.getsockopt = sock_no_getsockopt,
674 	.sendmsg    = sock_no_sendmsg,
675 	.recvmsg    = sock_no_recvmsg,
676 	.mmap       = sock_no_mmap,
677 	.ioctl      = pppox_ioctl,
678 };
679 
680 static const struct pppox_proto pppox_pptp_proto = {
681 	.create = pptp_create,
682 	.owner  = THIS_MODULE,
683 };
684 
685 static const struct gre_protocol gre_pptp_protocol = {
686 	.handler = pptp_rcv,
687 };
688 
pptp_init_module(void)689 static int __init pptp_init_module(void)
690 {
691 	int err = 0;
692 	pr_info("PPTP driver version " PPTP_DRIVER_VERSION "\n");
693 
694 	callid_sock = vzalloc((MAX_CALLID + 1) * sizeof(void *));
695 	if (!callid_sock)
696 		return -ENOMEM;
697 
698 	err = gre_add_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
699 	if (err) {
700 		pr_err("PPTP: can't add gre protocol\n");
701 		goto out_mem_free;
702 	}
703 
704 	err = proto_register(&pptp_sk_proto, 0);
705 	if (err) {
706 		pr_err("PPTP: can't register sk_proto\n");
707 		goto out_gre_del_protocol;
708 	}
709 
710 	err = register_pppox_proto(PX_PROTO_PPTP, &pppox_pptp_proto);
711 	if (err) {
712 		pr_err("PPTP: can't register pppox_proto\n");
713 		goto out_unregister_sk_proto;
714 	}
715 
716 	return 0;
717 
718 out_unregister_sk_proto:
719 	proto_unregister(&pptp_sk_proto);
720 out_gre_del_protocol:
721 	gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
722 out_mem_free:
723 	vfree(callid_sock);
724 
725 	return err;
726 }
727 
pptp_exit_module(void)728 static void __exit pptp_exit_module(void)
729 {
730 	unregister_pppox_proto(PX_PROTO_PPTP);
731 	proto_unregister(&pptp_sk_proto);
732 	gre_del_protocol(&gre_pptp_protocol, GREPROTO_PPTP);
733 	vfree(callid_sock);
734 }
735 
736 module_init(pptp_init_module);
737 module_exit(pptp_exit_module);
738 
739 MODULE_DESCRIPTION("Point-to-Point Tunneling Protocol");
740 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
741 MODULE_LICENSE("GPL");
742