blob: cdb4fa1af7346cddc36070034e4df9da0a904e7e [file] [log] [blame]
Chia-chi Yeh70511fd2009-06-12 01:09:30 +08001/* drivers/net/pppopns.c
2 *
3 * Driver for PPP on PPTP Network Server / PPPoPNS Socket (RFC 2637)
4 *
5 * Copyright (C) 2009 Google, Inc.
Chia-chi Yeh70511fd2009-06-12 01:09:30 +08006 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17/* This driver handles PPTP data packets between a RAW socket and a PPP channel.
18 * The socket is created in the kernel space and connected to the same address
Chia-chi Yehf6cd3752011-04-15 15:22:09 -070019 * of the control socket. Outgoing packets are always sent with sequences but
20 * without acknowledgements. Incoming packets with sequences are reordered
21 * within a sliding window of one second. Currently reordering only happens when
22 * a packet is received. It is done for simplicity since no additional locks or
23 * threads are required. This driver should work on both IPv4 and IPv6. */
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080024
25#include <linux/module.h>
Chia-chi Yehf6cd3752011-04-15 15:22:09 -070026#include <linux/jiffies.h>
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080027#include <linux/workqueue.h>
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080028#include <linux/skbuff.h>
29#include <linux/file.h>
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080030#include <linux/netdevice.h>
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080031#include <linux/net.h>
32#include <linux/ppp_defs.h>
33#include <linux/if.h>
34#include <linux/if_ppp.h>
35#include <linux/if_pppox.h>
36#include <linux/ppp_channel.h>
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080037#include <asm/uaccess.h>
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080038
39#define GRE_HEADER_SIZE 8
40
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080041#define PPTP_GRE_BITS htons(0x2001)
42#define PPTP_GRE_BITS_MASK htons(0xEF7F)
43#define PPTP_GRE_SEQ_BIT htons(0x1000)
44#define PPTP_GRE_ACK_BIT htons(0x0080)
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080045#define PPTP_GRE_TYPE htons(0x880B)
46
47#define PPP_ADDR 0xFF
48#define PPP_CTRL 0x03
49
50struct header {
51 __u16 bits;
52 __u16 type;
53 __u16 length;
54 __u16 call;
55 __u32 sequence;
56} __attribute__((packed));
57
Chia-chi Yehf6cd3752011-04-15 15:22:09 -070058struct meta {
59 __u32 sequence;
60 __u32 timestamp;
61};
62
63static inline struct meta *skb_meta(struct sk_buff *skb)
64{
65 return (struct meta *)skb->cb;
66}
67
68/******************************************************************************/
69
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080070static int pppopns_recv_core(struct sock *sk_raw, struct sk_buff *skb)
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080071{
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080072 struct sock *sk = (struct sock *)sk_raw->sk_user_data;
73 struct pppopns_opt *opt = &pppox_sk(sk)->proto.pns;
Chia-chi Yehf6cd3752011-04-15 15:22:09 -070074 struct meta *meta = skb_meta(skb);
75 __u32 now = jiffies;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080076 struct header *hdr;
77
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080078 /* Skip transport header */
79 skb_pull(skb, skb_transport_header(skb) - skb->data);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080080
Chia-chi Yehf6cd3752011-04-15 15:22:09 -070081 /* Drop the packet if GRE header is missing. */
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080082 if (skb->len < GRE_HEADER_SIZE)
83 goto drop;
Chia-chi Yehf6cd3752011-04-15 15:22:09 -070084 hdr = (struct header *)skb->data;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080085
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080086 /* Check the header. */
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080087 if (hdr->type != PPTP_GRE_TYPE || hdr->call != opt->local ||
88 (hdr->bits & PPTP_GRE_BITS_MASK) != PPTP_GRE_BITS)
89 goto drop;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080090
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080091 /* Skip all fields including optional ones. */
92 if (!skb_pull(skb, GRE_HEADER_SIZE +
93 (hdr->bits & PPTP_GRE_SEQ_BIT ? 4 : 0) +
94 (hdr->bits & PPTP_GRE_ACK_BIT ? 4 : 0)))
95 goto drop;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +080096
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +080097 /* Check the length. */
98 if (skb->len != ntohs(hdr->length))
99 goto drop;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800100
Chia-chi Yehf6cd3752011-04-15 15:22:09 -0700101 /* Check the sequence if it is present. */
102 if (hdr->bits & PPTP_GRE_SEQ_BIT) {
103 meta->sequence = ntohl(hdr->sequence);
104 if ((__s32)(meta->sequence - opt->recv_sequence) < 0)
105 goto drop;
106 }
107
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800108 /* Skip PPP address and control if they are present. */
109 if (skb->len >= 2 && skb->data[0] == PPP_ADDR &&
110 skb->data[1] == PPP_CTRL)
111 skb_pull(skb, 2);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800112
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800113 /* Fix PPP protocol if it is compressed. */
114 if (skb->len >= 1 && skb->data[0] & 1)
115 skb_push(skb, 1)[0] = 0;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800116
Chia-chi Yehf6cd3752011-04-15 15:22:09 -0700117 /* Drop the packet if PPP protocol is missing. */
118 if (skb->len < 2)
119 goto drop;
120
121 /* Perform reordering if sequencing is enabled. */
122 if (hdr->bits & PPTP_GRE_SEQ_BIT) {
123 struct sk_buff *skb1;
124
125 /* Insert the packet into receive queue in order. */
126 skb_set_owner_r(skb, sk);
127 skb_queue_walk(&sk->sk_receive_queue, skb1) {
128 struct meta *meta1 = skb_meta(skb1);
129 __s32 order = meta->sequence - meta1->sequence;
130 if (order == 0)
131 goto drop;
132 if (order < 0) {
133 meta->timestamp = meta1->timestamp;
134 skb_insert(skb1, skb, &sk->sk_receive_queue);
135 skb = NULL;
136 break;
137 }
138 }
139 if (skb) {
140 meta->timestamp = now;
141 skb_queue_tail(&sk->sk_receive_queue, skb);
142 }
143
144 /* Remove packets from receive queue as long as
145 * 1. the receive buffer is full,
146 * 2. they are queued longer than one second, or
147 * 3. there are no missing packets before them. */
148 skb_queue_walk_safe(&sk->sk_receive_queue, skb, skb1) {
149 meta = skb_meta(skb);
150 if (atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
151 now - meta->timestamp < HZ &&
152 meta->sequence != opt->recv_sequence)
153 break;
154 skb_unlink(skb, &sk->sk_receive_queue);
155 opt->recv_sequence = meta->sequence + 1;
156 skb_orphan(skb);
157 ppp_input(&pppox_sk(sk)->chan, skb);
158 }
159 return NET_RX_SUCCESS;
160 }
161
162 /* Flush receive queue if sequencing is disabled. */
163 skb_queue_purge(&sk->sk_receive_queue);
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800164 skb_orphan(skb);
165 ppp_input(&pppox_sk(sk)->chan, skb);
166 return NET_RX_SUCCESS;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800167drop:
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800168 kfree_skb(skb);
169 return NET_RX_DROP;
170}
171
Jon Medhurst30cd6392015-08-19 13:43:16 +0100172static void pppopns_recv(struct sock *sk_raw)
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800173{
174 struct sk_buff *skb;
175 while ((skb = skb_dequeue(&sk_raw->sk_receive_queue))) {
176 sock_hold(sk_raw);
177 sk_receive_skb(sk_raw, skb, 0);
178 }
179}
180
181static struct sk_buff_head delivery_queue;
182
183static void pppopns_xmit_core(struct work_struct *delivery_work)
184{
185 mm_segment_t old_fs = get_fs();
186 struct sk_buff *skb;
187
188 set_fs(KERNEL_DS);
189 while ((skb = skb_dequeue(&delivery_queue))) {
190 struct sock *sk_raw = skb->sk;
191 struct kvec iov = {.iov_base = skb->data, .iov_len = skb->len};
Subash Abhinov Kasiviswanathan149a5c62017-04-28 12:53:04 -0600192 struct msghdr msg = {
193 .msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT,
194 };
Guenter Roeck9079f242016-03-01 09:44:17 -0800195
196 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iov, 1,
197 skb->len);
Amit Pundir85d63882015-12-08 18:26:39 +0530198 sk_raw->sk_prot->sendmsg(sk_raw, &msg, skb->len);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800199 kfree_skb(skb);
200 }
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800201 set_fs(old_fs);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800202}
203
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800204static DECLARE_WORK(delivery_work, pppopns_xmit_core);
205
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800206static int pppopns_xmit(struct ppp_channel *chan, struct sk_buff *skb)
207{
208 struct sock *sk_raw = (struct sock *)chan->private;
209 struct pppopns_opt *opt = &pppox_sk(sk_raw->sk_user_data)->proto.pns;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800210 struct header *hdr;
211 __u16 length;
212
213 /* Install PPP address and control. */
214 skb_push(skb, 2);
215 skb->data[0] = PPP_ADDR;
216 skb->data[1] = PPP_CTRL;
217 length = skb->len;
218
219 /* Install PPTP GRE header. */
220 hdr = (struct header *)skb_push(skb, 12);
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800221 hdr->bits = PPTP_GRE_BITS | PPTP_GRE_SEQ_BIT;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800222 hdr->type = PPTP_GRE_TYPE;
223 hdr->length = htons(length);
224 hdr->call = opt->remote;
Chia-chi Yehf6cd3752011-04-15 15:22:09 -0700225 hdr->sequence = htonl(opt->xmit_sequence);
226 opt->xmit_sequence++;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800227
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800228 /* Now send the packet via the delivery queue. */
229 skb_set_owner_w(skb, sk_raw);
230 skb_queue_tail(&delivery_queue, skb);
231 schedule_work(&delivery_work);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800232 return 1;
233}
234
235/******************************************************************************/
236
237static struct ppp_channel_ops pppopns_channel_ops = {
238 .start_xmit = pppopns_xmit,
239};
240
241static int pppopns_connect(struct socket *sock, struct sockaddr *useraddr,
242 int addrlen, int flags)
243{
244 struct sock *sk = sock->sk;
245 struct pppox_sock *po = pppox_sk(sk);
246 struct sockaddr_pppopns *addr = (struct sockaddr_pppopns *)useraddr;
247 struct sockaddr_storage ss;
248 struct socket *sock_tcp = NULL;
249 struct socket *sock_raw = NULL;
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800250 struct sock *sk_tcp;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800251 struct sock *sk_raw;
252 int error;
253
254 if (addrlen != sizeof(struct sockaddr_pppopns))
255 return -EINVAL;
256
257 lock_sock(sk);
258 error = -EALREADY;
259 if (sk->sk_state != PPPOX_NONE)
260 goto out;
261
262 sock_tcp = sockfd_lookup(addr->tcp_socket, &error);
263 if (!sock_tcp)
264 goto out;
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800265 sk_tcp = sock_tcp->sk;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800266 error = -EPROTONOSUPPORT;
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800267 if (sk_tcp->sk_protocol != IPPROTO_TCP)
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800268 goto out;
269 addrlen = sizeof(struct sockaddr_storage);
270 error = kernel_getpeername(sock_tcp, (struct sockaddr *)&ss, &addrlen);
271 if (error)
272 goto out;
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800273 if (!sk_tcp->sk_bound_dev_if) {
274 struct dst_entry *dst = sk_dst_get(sk_tcp);
275 error = -ENODEV;
276 if (!dst)
277 goto out;
278 sk_tcp->sk_bound_dev_if = dst->dev->ifindex;
279 dst_release(dst);
280 }
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800281
282 error = sock_create(ss.ss_family, SOCK_RAW, IPPROTO_GRE, &sock_raw);
283 if (error)
284 goto out;
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800285 sk_raw = sock_raw->sk;
286 sk_raw->sk_bound_dev_if = sk_tcp->sk_bound_dev_if;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800287 error = kernel_connect(sock_raw, (struct sockaddr *)&ss, addrlen, 0);
288 if (error)
289 goto out;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800290
291 po->chan.hdrlen = 14;
292 po->chan.private = sk_raw;
293 po->chan.ops = &pppopns_channel_ops;
JP Abgrallbda61e52012-09-20 16:34:10 -0700294 po->chan.mtu = PPP_MRU - 80;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800295 po->proto.pns.local = addr->local;
296 po->proto.pns.remote = addr->remote;
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800297 po->proto.pns.data_ready = sk_raw->sk_data_ready;
298 po->proto.pns.backlog_rcv = sk_raw->sk_backlog_rcv;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800299
300 error = ppp_register_channel(&po->chan);
301 if (error)
302 goto out;
303
304 sk->sk_state = PPPOX_CONNECTED;
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800305 lock_sock(sk_raw);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800306 sk_raw->sk_data_ready = pppopns_recv;
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800307 sk_raw->sk_backlog_rcv = pppopns_recv_core;
308 sk_raw->sk_user_data = sk;
309 release_sock(sk_raw);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800310out:
311 if (sock_tcp)
312 sockfd_put(sock_tcp);
313 if (error && sock_raw)
314 sock_release(sock_raw);
315 release_sock(sk);
316 return error;
317}
318
319static int pppopns_release(struct socket *sock)
320{
321 struct sock *sk = sock->sk;
322
323 if (!sk)
324 return 0;
325
326 lock_sock(sk);
327 if (sock_flag(sk, SOCK_DEAD)) {
328 release_sock(sk);
329 return -EBADF;
330 }
331
332 if (sk->sk_state != PPPOX_NONE) {
333 struct sock *sk_raw = (struct sock *)pppox_sk(sk)->chan.private;
334 lock_sock(sk_raw);
Chia-chi Yehf6cd3752011-04-15 15:22:09 -0700335 skb_queue_purge(&sk->sk_receive_queue);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800336 pppox_unbind_sock(sk);
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800337 sk_raw->sk_data_ready = pppox_sk(sk)->proto.pns.data_ready;
338 sk_raw->sk_backlog_rcv = pppox_sk(sk)->proto.pns.backlog_rcv;
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800339 sk_raw->sk_user_data = NULL;
340 release_sock(sk_raw);
341 sock_release(sk_raw->sk_socket);
342 }
343
344 sock_orphan(sk);
345 sock->sk = NULL;
346 release_sock(sk);
347 sock_put(sk);
348 return 0;
349}
350
351/******************************************************************************/
352
353static struct proto pppopns_proto = {
354 .name = "PPPOPNS",
355 .owner = THIS_MODULE,
356 .obj_size = sizeof(struct pppox_sock),
357};
358
359static struct proto_ops pppopns_proto_ops = {
360 .family = PF_PPPOX,
361 .owner = THIS_MODULE,
362 .release = pppopns_release,
363 .bind = sock_no_bind,
364 .connect = pppopns_connect,
365 .socketpair = sock_no_socketpair,
366 .accept = sock_no_accept,
367 .getname = sock_no_getname,
368 .poll = sock_no_poll,
369 .ioctl = pppox_ioctl,
370 .listen = sock_no_listen,
371 .shutdown = sock_no_shutdown,
372 .setsockopt = sock_no_setsockopt,
373 .getsockopt = sock_no_getsockopt,
374 .sendmsg = sock_no_sendmsg,
375 .recvmsg = sock_no_recvmsg,
376 .mmap = sock_no_mmap,
377};
378
Amit Pundira7e707e2015-12-08 12:47:01 +0530379static int pppopns_create(struct net *net, struct socket *sock, int kern)
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800380{
381 struct sock *sk;
382
Amit Pundira7e707e2015-12-08 12:47:01 +0530383 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppopns_proto, kern);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800384 if (!sk)
385 return -ENOMEM;
386
387 sock_init_data(sock, sk);
388 sock->state = SS_UNCONNECTED;
389 sock->ops = &pppopns_proto_ops;
390 sk->sk_protocol = PX_PROTO_OPNS;
391 sk->sk_state = PPPOX_NONE;
392 return 0;
393}
394
395/******************************************************************************/
396
397static struct pppox_proto pppopns_pppox_proto = {
398 .create = pppopns_create,
399 .owner = THIS_MODULE,
400};
401
402static int __init pppopns_init(void)
403{
404 int error;
405
406 error = proto_register(&pppopns_proto, 0);
407 if (error)
408 return error;
409
410 error = register_pppox_proto(PX_PROTO_OPNS, &pppopns_pppox_proto);
411 if (error)
412 proto_unregister(&pppopns_proto);
Chia-chi Yeh32f7ae92009-06-13 02:29:04 +0800413 else
414 skb_queue_head_init(&delivery_queue);
Chia-chi Yeh70511fd2009-06-12 01:09:30 +0800415 return error;
416}
417
418static void __exit pppopns_exit(void)
419{
420 unregister_pppox_proto(PX_PROTO_OPNS);
421 proto_unregister(&pppopns_proto);
422}
423
424module_init(pppopns_init);
425module_exit(pppopns_exit);
426
427MODULE_DESCRIPTION("PPP on PPTP Network Server (PPPoPNS)");
428MODULE_AUTHOR("Chia-chi Yeh <chiachi@android.com>");
429MODULE_LICENSE("GPL");