blob: cd43d02484e4c8ec6f12e69e1783e47b16a46e05 [file] [log] [blame]
James Chapmanfd558d12010-04-02 06:18:33 +00001/*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
3 *
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
6 *
7 * Version: 2.0.0
8 *
9 * Authors: James Chapman (jchapman@katalix.com)
10 *
11 * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
12 *
13 * License:
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 */
20
21/* This driver handles only L2TP data frames; control frames are handled by a
22 * userspace application.
23 *
24 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
25 * attaches it to a bound UDP socket with local tunnel_id / session_id and
26 * peer tunnel_id / session_id set. Data can then be sent or received using
27 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
28 * can be read or modified using ioctl() or [gs]etsockopt() calls.
29 *
30 * When a PPPoL2TP socket is connected with local and peer session_id values
31 * zero, the socket is treated as a special tunnel management socket.
32 *
33 * Here's example userspace code to create a socket for sending/receiving data
34 * over an L2TP session:-
35 *
36 * struct sockaddr_pppol2tp sax;
37 * int fd;
38 * int session_fd;
39 *
40 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
41 *
42 * sax.sa_family = AF_PPPOX;
43 * sax.sa_protocol = PX_PROTO_OL2TP;
44 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
45 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
46 * sax.pppol2tp.addr.sin_port = addr->sin_port;
47 * sax.pppol2tp.addr.sin_family = AF_INET;
48 * sax.pppol2tp.s_tunnel = tunnel_id;
49 * sax.pppol2tp.s_session = session_id;
50 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
51 * sax.pppol2tp.d_session = peer_session_id;
52 *
53 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
54 *
55 * A pppd plugin that allows PPP traffic to be carried over L2TP using
56 * this driver is available from the OpenL2TP project at
57 * http://openl2tp.sourceforge.net.
58 */
59
Joe Perchesa4ca44f2012-05-16 09:55:56 +000060#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
61
James Chapmanfd558d12010-04-02 06:18:33 +000062#include <linux/module.h>
63#include <linux/string.h>
64#include <linux/list.h>
65#include <linux/uaccess.h>
66
67#include <linux/kernel.h>
68#include <linux/spinlock.h>
69#include <linux/kthread.h>
70#include <linux/sched.h>
71#include <linux/slab.h>
72#include <linux/errno.h>
73#include <linux/jiffies.h>
74
75#include <linux/netdevice.h>
76#include <linux/net.h>
77#include <linux/inetdevice.h>
78#include <linux/skbuff.h>
79#include <linux/init.h>
80#include <linux/ip.h>
81#include <linux/udp.h>
82#include <linux/if_pppox.h>
83#include <linux/if_pppol2tp.h>
84#include <net/sock.h>
85#include <linux/ppp_channel.h>
86#include <linux/ppp_defs.h>
Paul Mackerras4b32da2b2012-03-04 12:56:55 +000087#include <linux/ppp-ioctl.h>
James Chapmanfd558d12010-04-02 06:18:33 +000088#include <linux/file.h>
89#include <linux/hash.h>
90#include <linux/sort.h>
91#include <linux/proc_fs.h>
James Chapman309795f2010-04-02 06:19:10 +000092#include <linux/l2tp.h>
James Chapmanfd558d12010-04-02 06:18:33 +000093#include <linux/nsproxy.h>
94#include <net/net_namespace.h>
95#include <net/netns/generic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000096#include <net/ip.h>
97#include <net/udp.h>
Tom Parkincf2f5c82013-03-19 06:11:21 +000098#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +000099
100#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -0700101#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +0000102
103#include "l2tp_core.h"
104
105#define PPPOL2TP_DRV_VERSION "V2.0"
106
107/* Space for UDP, L2TP and PPP headers */
108#define PPPOL2TP_HEADER_OVERHEAD 40
109
James Chapmanfd558d12010-04-02 06:18:33 +0000110/* Number of bytes to build transmit L2TP headers.
111 * Unfortunately the size is different depending on whether sequence numbers
112 * are enabled.
113 */
114#define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
115#define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
116
117/* Private data of each session. This data lives at the end of struct
118 * l2tp_session, referenced via session->priv[].
119 */
120struct pppol2tp_session {
121 int owner; /* pid that opened the socket */
122
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200123 struct mutex sk_lock; /* Protects .sk */
124 struct sock __rcu *sk; /* Pointer to the session
James Chapmanfd558d12010-04-02 06:18:33 +0000125 * PPPoX socket */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200126 struct sock *__sk; /* Copy of .sk, for cleanup */
127 struct rcu_head rcu; /* For asynchronous release */
James Chapmanfd558d12010-04-02 06:18:33 +0000128};
129
130static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
131
stephen hemmingerd7100da2010-08-04 07:34:36 +0000132static const struct ppp_channel_ops pppol2tp_chan_ops = {
133 .start_xmit = pppol2tp_xmit,
134};
135
James Chapmanfd558d12010-04-02 06:18:33 +0000136static const struct proto_ops pppol2tp_ops;
137
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200138/* Retrieves the pppol2tp socket associated to a session.
139 * A reference is held on the returned socket, so this function must be paired
140 * with sock_put().
141 */
142static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
143{
144 struct pppol2tp_session *ps = l2tp_session_priv(session);
145 struct sock *sk;
146
147 rcu_read_lock();
148 sk = rcu_dereference(ps->sk);
149 if (sk)
150 sock_hold(sk);
151 rcu_read_unlock();
152
153 return sk;
154}
155
James Chapmanfd558d12010-04-02 06:18:33 +0000156/* Helpers to obtain tunnel/session contexts from sockets.
157 */
158static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
159{
160 struct l2tp_session *session;
161
162 if (sk == NULL)
163 return NULL;
164
165 sock_hold(sk);
166 session = (struct l2tp_session *)(sk->sk_user_data);
167 if (session == NULL) {
168 sock_put(sk);
169 goto out;
170 }
171
172 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
173
174out:
175 return session;
176}
177
178/*****************************************************************************
179 * Receive data handling
180 *****************************************************************************/
181
James Chapmanfd558d12010-04-02 06:18:33 +0000182/* Receive message. This is the recvmsg for the PPPoL2TP socket.
183 */
Ying Xue1b784142015-03-02 15:37:48 +0800184static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
185 size_t len, int flags)
James Chapmanfd558d12010-04-02 06:18:33 +0000186{
187 int err;
188 struct sk_buff *skb;
189 struct sock *sk = sock->sk;
190
191 err = -EIO;
192 if (sk->sk_state & PPPOX_BOUND)
193 goto end;
194
James Chapmanfd558d12010-04-02 06:18:33 +0000195 err = 0;
196 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
197 flags & MSG_DONTWAIT, &err);
198 if (!skb)
199 goto end;
200
201 if (len > skb->len)
202 len = skb->len;
203 else if (len < skb->len)
204 msg->msg_flags |= MSG_TRUNC;
205
David S. Miller51f3d022014-11-05 16:46:40 -0500206 err = skb_copy_datagram_msg(skb, 0, msg, len);
James Chapmanfd558d12010-04-02 06:18:33 +0000207 if (likely(err == 0))
208 err = len;
209
210 kfree_skb(skb);
211end:
212 return err;
213}
214
215static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
216{
217 struct pppol2tp_session *ps = l2tp_session_priv(session);
218 struct sock *sk = NULL;
219
220 /* If the socket is bound, send it in to PPP's input queue. Otherwise
221 * queue it on the session socket.
222 */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200223 rcu_read_lock();
224 sk = rcu_dereference(ps->sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000225 if (sk == NULL)
226 goto no_sock;
227
Guillaume Nault2b139e62018-07-25 14:53:33 +0200228 /* If the first two bytes are 0xFF03, consider that it is the PPP's
229 * Address and Control fields and skip them. The L2TP module has always
230 * worked this way, although, in theory, the use of these fields should
231 * be negociated and handled at the PPP layer. These fields are
232 * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
233 * Information command with Poll/Final bit set to zero (RFC 1662).
234 */
235 if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
236 skb->data[1] == PPP_UI)
237 skb_pull(skb, 2);
238
James Chapmanfd558d12010-04-02 06:18:33 +0000239 if (sk->sk_state & PPPOX_BOUND) {
240 struct pppox_sock *po;
Guillaume Nault98f40b32015-12-29 13:06:59 +0100241
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000242 l2tp_dbg(session, L2TP_MSG_DATA,
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000243 "%s: recv %d byte data frame, passing to ppp\n",
244 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000245
James Chapmanfd558d12010-04-02 06:18:33 +0000246 po = pppox_sk(sk);
247 ppp_input(&po->chan, skb);
248 } else {
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000249 l2tp_dbg(session, L2TP_MSG_DATA,
Guillaume Nault9e9cb622014-03-06 11:15:10 +0100250 "%s: recv %d byte data frame, passing to L2TP socket\n",
251 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000252
Guillaume Nault9e9cb622014-03-06 11:15:10 +0100253 if (sock_queue_rcv_skb(sk, skb) < 0) {
254 atomic_long_inc(&session->stats.rx_errors);
255 kfree_skb(skb);
256 }
James Chapmanfd558d12010-04-02 06:18:33 +0000257 }
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200258 rcu_read_unlock();
James Chapmanfd558d12010-04-02 06:18:33 +0000259
260 return;
261
262no_sock:
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200263 rcu_read_unlock();
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000264 l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000265 kfree_skb(skb);
266}
267
James Chapmanfd558d12010-04-02 06:18:33 +0000268/************************************************************************
269 * Transmit handling
270 ***********************************************************************/
271
James Chapmanfd558d12010-04-02 06:18:33 +0000272/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
273 * when a user application does a sendmsg() on the session socket. L2TP and
274 * PPP headers must be inserted into the user's data.
275 */
Ying Xue1b784142015-03-02 15:37:48 +0800276static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
James Chapmanfd558d12010-04-02 06:18:33 +0000277 size_t total_len)
278{
James Chapmanfd558d12010-04-02 06:18:33 +0000279 struct sock *sk = sock->sk;
280 struct sk_buff *skb;
281 int error;
282 struct l2tp_session *session;
283 struct l2tp_tunnel *tunnel;
James Chapman0d767512010-04-02 06:19:00 +0000284 int uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +0000285
286 error = -ENOTCONN;
287 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
288 goto error;
289
290 /* Get session and tunnel contexts */
291 error = -EBADF;
292 session = pppol2tp_sock_to_session(sk);
293 if (session == NULL)
294 goto error;
295
Guillaume Nault7198c772017-11-11 06:06:31 +0900296 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000297
James Chapman0d767512010-04-02 06:19:00 +0000298 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
299
James Chapmanfd558d12010-04-02 06:18:33 +0000300 /* Allocate a socket buffer */
301 error = -ENOMEM;
302 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +0000303 uhlen + session->hdr_len +
Gao Feng54c151d2016-08-22 22:50:02 +0800304 2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
James Chapmanfd558d12010-04-02 06:18:33 +0000305 0, GFP_KERNEL);
306 if (!skb)
Guillaume Nault7198c772017-11-11 06:06:31 +0900307 goto error_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000308
309 /* Reserve space for headers. */
310 skb_reserve(skb, NET_SKB_PAD);
311 skb_reset_network_header(skb);
312 skb_reserve(skb, sizeof(struct iphdr));
313 skb_reset_transport_header(skb);
James Chapman0d767512010-04-02 06:19:00 +0000314 skb_reserve(skb, uhlen);
James Chapmanfd558d12010-04-02 06:18:33 +0000315
316 /* Add PPP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800317 skb->data[0] = PPP_ALLSTATIONS;
318 skb->data[1] = PPP_UI;
James Chapmanfd558d12010-04-02 06:18:33 +0000319 skb_put(skb, 2);
320
321 /* Copy user data into skb */
Al Viro6ce8e9c2014-04-06 21:25:44 -0400322 error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000323 if (error < 0) {
324 kfree_skb(skb);
Guillaume Nault7198c772017-11-11 06:06:31 +0900325 goto error_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000326 }
James Chapmanfd558d12010-04-02 06:18:33 +0000327
Eric Dumazet455cc322013-10-10 06:30:09 -0700328 local_bh_disable();
James Chapmanfd558d12010-04-02 06:18:33 +0000329 l2tp_xmit_skb(session, skb, session->hdr_len);
Eric Dumazet455cc322013-10-10 06:30:09 -0700330 local_bh_enable();
James Chapmanfd558d12010-04-02 06:18:33 +0000331
Guillaume Nault8b82547e33e2013-03-01 05:02:02 +0000332 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000333
Guillaume Naulta6f79d02013-06-12 16:07:36 +0200334 return total_len;
James Chapmanfd558d12010-04-02 06:18:33 +0000335
James Chapmanfd558d12010-04-02 06:18:33 +0000336error_put_sess:
337 sock_put(sk);
338error:
339 return error;
340}
341
342/* Transmit function called by generic PPP driver. Sends PPP frame
343 * over PPPoL2TP socket.
344 *
345 * This is almost the same as pppol2tp_sendmsg(), but rather than
346 * being called with a msghdr from userspace, it is called with a skb
347 * from the kernel.
348 *
349 * The supplied skb from ppp doesn't have enough headroom for the
350 * insertion of L2TP, UDP and IP headers so we need to allocate more
351 * headroom in the skb. This will create a cloned skb. But we must be
352 * careful in the error case because the caller will expect to free
353 * the skb it supplied, not our cloned skb. So we take care to always
354 * leave the original skb unfreed if we return an error.
355 */
356static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
357{
James Chapmanfd558d12010-04-02 06:18:33 +0000358 struct sock *sk = (struct sock *) chan->private;
James Chapmanfd558d12010-04-02 06:18:33 +0000359 struct l2tp_session *session;
360 struct l2tp_tunnel *tunnel;
Eric Dumazet09df57c2011-10-07 05:45:57 +0000361 int uhlen, headroom;
James Chapmanfd558d12010-04-02 06:18:33 +0000362
363 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
364 goto abort;
365
366 /* Get session and tunnel contexts from the socket */
367 session = pppol2tp_sock_to_session(sk);
368 if (session == NULL)
369 goto abort;
370
Guillaume Nault7198c772017-11-11 06:06:31 +0900371 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000372
Eric Dumazet09df57c2011-10-07 05:45:57 +0000373 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
374 headroom = NET_SKB_PAD +
375 sizeof(struct iphdr) + /* IP header */
376 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
377 session->hdr_len + /* L2TP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800378 2; /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
Eric Dumazet09df57c2011-10-07 05:45:57 +0000379 if (skb_cow_head(skb, headroom))
Guillaume Nault7198c772017-11-11 06:06:31 +0900380 goto abort_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000381
James Chapmanfd558d12010-04-02 06:18:33 +0000382 /* Setup PPP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800383 __skb_push(skb, 2);
384 skb->data[0] = PPP_ALLSTATIONS;
385 skb->data[1] = PPP_UI;
James Chapmanfd558d12010-04-02 06:18:33 +0000386
Eric Dumazet455cc322013-10-10 06:30:09 -0700387 local_bh_disable();
James Chapmane0d44352010-04-02 06:18:54 +0000388 l2tp_xmit_skb(session, skb, session->hdr_len);
Eric Dumazet455cc322013-10-10 06:30:09 -0700389 local_bh_enable();
James Chapmanfd558d12010-04-02 06:18:33 +0000390
James Chapmanfd558d12010-04-02 06:18:33 +0000391 sock_put(sk);
Guillaume Nault7198c772017-11-11 06:06:31 +0900392
James Chapmanfd558d12010-04-02 06:18:33 +0000393 return 1;
394
James Chapmanfd558d12010-04-02 06:18:33 +0000395abort_put_sess:
396 sock_put(sk);
397abort:
398 /* Free the original skb */
399 kfree_skb(skb);
400 return 1;
401}
402
403/*****************************************************************************
404 * Session (and tunnel control) socket create/destroy.
405 *****************************************************************************/
406
James Chapmand02ba2a2018-02-23 17:45:46 +0000407static void pppol2tp_put_sk(struct rcu_head *head)
408{
409 struct pppol2tp_session *ps;
410
411 ps = container_of(head, typeof(*ps), rcu);
412 sock_put(ps->__sk);
413}
414
James Chapmanfd558d12010-04-02 06:18:33 +0000415/* Really kill the session socket. (Called from sock_put() if
416 * refcnt == 0.)
417 */
418static void pppol2tp_session_destruct(struct sock *sk)
419{
Tom Parkinf6e16b22013-03-19 06:11:23 +0000420 struct l2tp_session *session = sk->sk_user_data;
Guillaume Naulte91793b2017-03-29 08:45:29 +0200421
422 skb_queue_purge(&sk->sk_receive_queue);
423 skb_queue_purge(&sk->sk_write_queue);
424
Tom Parkinf6e16b22013-03-19 06:11:23 +0000425 if (session) {
James Chapmanfd558d12010-04-02 06:18:33 +0000426 sk->sk_user_data = NULL;
427 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
428 l2tp_session_dec_refcount(session);
429 }
James Chapmanfd558d12010-04-02 06:18:33 +0000430}
431
432/* Called when the PPPoX socket (session) is closed.
433 */
434static int pppol2tp_release(struct socket *sock)
435{
436 struct sock *sk = sock->sk;
437 struct l2tp_session *session;
438 int error;
439
440 if (!sk)
441 return 0;
442
443 error = -EBADF;
444 lock_sock(sk);
445 if (sock_flag(sk, SOCK_DEAD) != 0)
446 goto error;
447
448 pppox_unbind_sock(sk);
449
450 /* Signal the death of the socket. */
451 sk->sk_state = PPPOX_DEAD;
452 sock_orphan(sk);
453 sock->sk = NULL;
454
455 session = pppol2tp_sock_to_session(sk);
James Chapmand02ba2a2018-02-23 17:45:46 +0000456 if (session) {
Guillaume Nault3d609342018-06-04 18:52:19 +0200457 struct pppol2tp_session *ps;
458
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200459 l2tp_session_delete(session);
Guillaume Nault3d609342018-06-04 18:52:19 +0200460
461 ps = l2tp_session_priv(session);
462 mutex_lock(&ps->sk_lock);
463 ps->__sk = rcu_dereference_protected(ps->sk,
464 lockdep_is_held(&ps->sk_lock));
465 RCU_INIT_POINTER(ps->sk, NULL);
466 mutex_unlock(&ps->sk_lock);
467 call_rcu(&ps->rcu, pppol2tp_put_sk);
468
469 /* Rely on the sock_put() call at the end of the function for
470 * dropping the reference held by pppol2tp_sock_to_session().
471 * The last reference will be dropped by pppol2tp_put_sk().
472 */
James Chapmanfd558d12010-04-02 06:18:33 +0000473 }
James Chapmand02ba2a2018-02-23 17:45:46 +0000474
James Chapmanfd558d12010-04-02 06:18:33 +0000475 release_sock(sk);
476
477 /* This will delete the session context via
478 * pppol2tp_session_destruct() if the socket's refcnt drops to
479 * zero.
480 */
481 sock_put(sk);
482
483 return 0;
484
485error:
486 release_sock(sk);
487 return error;
488}
489
490static struct proto pppol2tp_sk_proto = {
491 .name = "PPPOL2TP",
492 .owner = THIS_MODULE,
493 .obj_size = sizeof(struct pppox_sock),
494};
495
496static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
497{
498 int rc;
499
500 rc = l2tp_udp_encap_recv(sk, skb);
501 if (rc)
502 kfree_skb(skb);
503
504 return NET_RX_SUCCESS;
505}
506
507/* socket() handler. Initialize a new struct sock.
508 */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500509static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
James Chapmanfd558d12010-04-02 06:18:33 +0000510{
511 int error = -ENOMEM;
512 struct sock *sk;
513
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500514 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
James Chapmanfd558d12010-04-02 06:18:33 +0000515 if (!sk)
516 goto out;
517
518 sock_init_data(sock, sk);
519
520 sock->state = SS_UNCONNECTED;
521 sock->ops = &pppol2tp_ops;
522
523 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
524 sk->sk_protocol = PX_PROTO_OL2TP;
525 sk->sk_family = PF_PPPOX;
526 sk->sk_state = PPPOX_NONE;
527 sk->sk_type = SOCK_STREAM;
528 sk->sk_destruct = pppol2tp_session_destruct;
529
530 error = 0;
531
532out:
533 return error;
534}
535
Javier Martinez Canillas9dd79942016-09-09 08:43:17 -0400536#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
James Chapman0ad66142010-04-02 06:19:33 +0000537static void pppol2tp_show(struct seq_file *m, void *arg)
538{
539 struct l2tp_session *session = arg;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200540 struct sock *sk;
James Chapman0ad66142010-04-02 06:19:33 +0000541
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200542 sk = pppol2tp_session_get_sock(session);
543 if (sk) {
544 struct pppox_sock *po = pppox_sk(sk);
545
546 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
547 sock_put(sk);
James Chapman0ad66142010-04-02 06:19:33 +0000548 }
549}
550#endif
551
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200552static void pppol2tp_session_init(struct l2tp_session *session)
553{
554 struct pppol2tp_session *ps;
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200555
556 session->recv_skb = pppol2tp_recv;
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200557#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
558 session->show = pppol2tp_show;
559#endif
560
561 ps = l2tp_session_priv(session);
562 mutex_init(&ps->sk_lock);
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200563 ps->owner = current->pid;
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200564}
565
Guillaume Naulta4081942018-06-26 18:41:36 +0200566struct l2tp_connect_info {
567 u8 version;
568 int fd;
569 u32 tunnel_id;
570 u32 peer_tunnel_id;
571 u32 session_id;
572 u32 peer_session_id;
573};
574
575static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
576 struct l2tp_connect_info *info)
577{
578 switch (sa_len) {
579 case sizeof(struct sockaddr_pppol2tp):
580 {
581 const struct sockaddr_pppol2tp *sa_v2in4 = sa;
582
583 if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
584 return -EINVAL;
585
586 info->version = 2;
587 info->fd = sa_v2in4->pppol2tp.fd;
588 info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
589 info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
590 info->session_id = sa_v2in4->pppol2tp.s_session;
591 info->peer_session_id = sa_v2in4->pppol2tp.d_session;
592
593 break;
594 }
595 case sizeof(struct sockaddr_pppol2tpv3):
596 {
597 const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
598
599 if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
600 return -EINVAL;
601
602 info->version = 3;
603 info->fd = sa_v3in4->pppol2tp.fd;
604 info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
605 info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
606 info->session_id = sa_v3in4->pppol2tp.s_session;
607 info->peer_session_id = sa_v3in4->pppol2tp.d_session;
608
609 break;
610 }
611 case sizeof(struct sockaddr_pppol2tpin6):
612 {
613 const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
614
615 if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
616 return -EINVAL;
617
618 info->version = 2;
619 info->fd = sa_v2in6->pppol2tp.fd;
620 info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
621 info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
622 info->session_id = sa_v2in6->pppol2tp.s_session;
623 info->peer_session_id = sa_v2in6->pppol2tp.d_session;
624
625 break;
626 }
627 case sizeof(struct sockaddr_pppol2tpv3in6):
628 {
629 const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
630
631 if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
632 return -EINVAL;
633
634 info->version = 3;
635 info->fd = sa_v3in6->pppol2tp.fd;
636 info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
637 info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
638 info->session_id = sa_v3in6->pppol2tp.s_session;
639 info->peer_session_id = sa_v3in6->pppol2tp.d_session;
640
641 break;
642 }
643 default:
644 return -EINVAL;
645 }
646
647 return 0;
648}
649
Guillaume Nault789141b2018-08-03 12:38:37 +0200650/* Rough estimation of the maximum payload size a tunnel can transmit without
651 * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
652 * numbers and no IP option. Not quite accurate, but the result is mostly
653 * unused anyway.
654 */
655static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
656{
657 int mtu;
658
659 mtu = l2tp_tunnel_dst_mtu(tunnel);
660 if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
661 return 1500 - PPPOL2TP_HEADER_OVERHEAD;
662
663 return mtu - PPPOL2TP_HEADER_OVERHEAD;
664}
665
James Chapmanfd558d12010-04-02 06:18:33 +0000666/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
667 */
668static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
669 int sockaddr_len, int flags)
670{
671 struct sock *sk = sock->sk;
James Chapmanfd558d12010-04-02 06:18:33 +0000672 struct pppox_sock *po = pppox_sk(sk);
673 struct l2tp_session *session = NULL;
Guillaume Naulta4081942018-06-26 18:41:36 +0200674 struct l2tp_connect_info info;
James Chapmanfd558d12010-04-02 06:18:33 +0000675 struct l2tp_tunnel *tunnel;
676 struct pppol2tp_session *ps;
James Chapmanfd558d12010-04-02 06:18:33 +0000677 struct l2tp_session_cfg cfg = { 0, };
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200678 bool drop_refcnt = false;
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100679 bool drop_tunnel = false;
Guillaume Naultbda06be2018-06-13 15:09:21 +0200680 bool new_session = false;
681 bool new_tunnel = false;
Guillaume Naulta4081942018-06-26 18:41:36 +0200682 int error;
683
684 error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
685 if (error < 0)
686 return error;
James Chapmanfd558d12010-04-02 06:18:33 +0000687
688 lock_sock(sk);
689
James Chapmanfd558d12010-04-02 06:18:33 +0000690 /* Check for already bound sockets */
691 error = -EBUSY;
692 if (sk->sk_state & PPPOX_CONNECTED)
693 goto end;
694
695 /* We don't supporting rebinding anyway */
696 error = -EALREADY;
697 if (sk->sk_user_data)
698 goto end; /* socket is already attached */
699
James Chapmane0d44352010-04-02 06:18:54 +0000700 /* Don't bind if tunnel_id is 0 */
James Chapmanfd558d12010-04-02 06:18:33 +0000701 error = -EINVAL;
Guillaume Naulta4081942018-06-26 18:41:36 +0200702 if (!info.tunnel_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000703 goto end;
704
Guillaume Naulta4081942018-06-26 18:41:36 +0200705 tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100706 if (tunnel)
707 drop_tunnel = true;
James Chapman309795f2010-04-02 06:19:10 +0000708
James Chapmane0d44352010-04-02 06:18:54 +0000709 /* Special case: create tunnel context if session_id and
710 * peer_session_id is 0. Otherwise look up tunnel using supplied
James Chapmanfd558d12010-04-02 06:18:33 +0000711 * tunnel id.
712 */
Guillaume Naulta4081942018-06-26 18:41:36 +0200713 if (!info.session_id && !info.peer_session_id) {
James Chapman309795f2010-04-02 06:19:10 +0000714 if (tunnel == NULL) {
715 struct l2tp_tunnel_cfg tcfg = {
716 .encap = L2TP_ENCAPTYPE_UDP,
717 .debug = 0,
718 };
Guillaume Nault3e1bc8b2018-06-13 15:09:20 +0200719
720 /* Prevent l2tp_tunnel_register() from trying to set up
721 * a kernel socket.
722 */
Guillaume Naulta4081942018-06-26 18:41:36 +0200723 if (info.fd < 0) {
Guillaume Nault3e1bc8b2018-06-13 15:09:20 +0200724 error = -EBADF;
725 goto end;
726 }
727
Guillaume Naulta4081942018-06-26 18:41:36 +0200728 error = l2tp_tunnel_create(sock_net(sk), info.fd,
729 info.version,
730 info.tunnel_id,
731 info.peer_tunnel_id, &tcfg,
732 &tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000733 if (error < 0)
734 goto end;
Guillaume Nault6b9f3422018-04-10 21:01:12 +0200735
736 l2tp_tunnel_inc_refcount(tunnel);
737 error = l2tp_tunnel_register(tunnel, sock_net(sk),
738 &tcfg);
739 if (error < 0) {
740 kfree(tunnel);
741 goto end;
742 }
743 drop_tunnel = true;
Guillaume Naultbda06be2018-06-13 15:09:21 +0200744 new_tunnel = true;
James Chapman309795f2010-04-02 06:19:10 +0000745 }
James Chapmanfd558d12010-04-02 06:18:33 +0000746 } else {
James Chapmanfd558d12010-04-02 06:18:33 +0000747 /* Error if we can't find the tunnel */
748 error = -ENOENT;
749 if (tunnel == NULL)
750 goto end;
751
752 /* Error if socket is not prepped */
753 if (tunnel->sock == NULL)
754 goto end;
755 }
756
James Chapmanb79585f2012-04-29 21:48:50 +0000757 if (tunnel->peer_tunnel_id == 0)
Guillaume Naulta4081942018-06-26 18:41:36 +0200758 tunnel->peer_tunnel_id = info.peer_tunnel_id;
James Chapmanfd558d12010-04-02 06:18:33 +0000759
Guillaume Nault01e28b92018-08-10 13:21:57 +0200760 session = l2tp_tunnel_get_session(tunnel, info.session_id);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200761 if (session) {
762 drop_refcnt = true;
Guillaume Nault7ac6ab12018-06-13 15:09:19 +0200763
764 if (session->pwtype != L2TP_PWTYPE_PPP) {
765 error = -EPROTOTYPE;
766 goto end;
767 }
768
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200769 ps = l2tp_session_priv(session);
James Chapman309795f2010-04-02 06:19:10 +0000770
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200771 /* Using a pre-existing session is fine as long as it hasn't
772 * been connected yet.
773 */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200774 mutex_lock(&ps->sk_lock);
775 if (rcu_dereference_protected(ps->sk,
Guillaume Nault3d609342018-06-04 18:52:19 +0200776 lockdep_is_held(&ps->sk_lock)) ||
777 ps->__sk) {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200778 mutex_unlock(&ps->sk_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200779 error = -EEXIST;
780 goto end;
781 }
James Chapman309795f2010-04-02 06:19:10 +0000782 } else {
Guillaume Nault90904ff2018-06-13 15:09:18 +0200783 cfg.pw_type = L2TP_PWTYPE_PPP;
James Chapman309795f2010-04-02 06:19:10 +0000784
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200785 session = l2tp_session_create(sizeof(struct pppol2tp_session),
Guillaume Naulta4081942018-06-26 18:41:36 +0200786 tunnel, info.session_id,
787 info.peer_session_id, &cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200788 if (IS_ERR(session)) {
789 error = PTR_ERR(session);
James Chapman309795f2010-04-02 06:19:10 +0000790 goto end;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200791 }
Guillaume Nault3953ae72017-10-27 16:51:50 +0200792
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200793 pppol2tp_session_init(session);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200794 ps = l2tp_session_priv(session);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200795 l2tp_session_inc_refcount(session);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200796
797 mutex_lock(&ps->sk_lock);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200798 error = l2tp_session_register(session, tunnel);
799 if (error < 0) {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200800 mutex_unlock(&ps->sk_lock);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200801 kfree(session);
802 goto end;
803 }
804 drop_refcnt = true;
Guillaume Naultbda06be2018-06-13 15:09:21 +0200805 new_session = true;
James Chapman309795f2010-04-02 06:19:10 +0000806 }
807
James Chapmanfd558d12010-04-02 06:18:33 +0000808 /* Special case: if source & dest session_id == 0x0000, this
809 * socket is being created to manage the tunnel. Just set up
810 * the internal context for use by ioctl() and sockopt()
811 * handlers.
812 */
813 if ((session->session_id == 0) &&
814 (session->peer_session_id == 0)) {
815 error = 0;
816 goto out_no_ppp;
817 }
818
819 /* The only header we need to worry about is the L2TP
820 * header. This size is different depending on whether
821 * sequence numbers are enabled for the data channel.
822 */
823 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
824
825 po->chan.private = sk;
826 po->chan.ops = &pppol2tp_chan_ops;
Guillaume Nault789141b2018-08-03 12:38:37 +0200827 po->chan.mtu = pppol2tp_tunnel_mtu(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +0000828
829 error = ppp_register_net_channel(sock_net(sk), &po->chan);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200830 if (error) {
831 mutex_unlock(&ps->sk_lock);
James Chapmanfd558d12010-04-02 06:18:33 +0000832 goto end;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200833 }
James Chapmanfd558d12010-04-02 06:18:33 +0000834
835out_no_ppp:
836 /* This is how we get the session context from the socket. */
837 sk->sk_user_data = session;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200838 rcu_assign_pointer(ps->sk, sk);
839 mutex_unlock(&ps->sk_lock);
840
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200841 /* Keep the reference we've grabbed on the session: sk doesn't expect
842 * the session to disappear. pppol2tp_session_destruct() is responsible
843 * for dropping it.
844 */
845 drop_refcnt = false;
846
James Chapmanfd558d12010-04-02 06:18:33 +0000847 sk->sk_state = PPPOX_CONNECTED;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000848 l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000849 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000850
851end:
Guillaume Naultbda06be2018-06-13 15:09:21 +0200852 if (error) {
853 if (new_session)
854 l2tp_session_delete(session);
855 if (new_tunnel)
856 l2tp_tunnel_delete(tunnel);
857 }
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200858 if (drop_refcnt)
859 l2tp_session_dec_refcount(session);
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100860 if (drop_tunnel)
861 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +0000862 release_sock(sk);
863
864 return error;
865}
866
James Chapman309795f2010-04-02 06:19:10 +0000867#ifdef CONFIG_L2TP_V3
868
Guillaume Naultf026bc22017-09-01 17:58:51 +0200869/* Called when creating sessions via the netlink interface. */
870static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
871 u32 session_id, u32 peer_session_id,
872 struct l2tp_session_cfg *cfg)
James Chapman309795f2010-04-02 06:19:10 +0000873{
874 int error;
James Chapman309795f2010-04-02 06:19:10 +0000875 struct l2tp_session *session;
James Chapman309795f2010-04-02 06:19:10 +0000876
James Chapman309795f2010-04-02 06:19:10 +0000877 /* Error if tunnel socket is not prepped */
Guillaume Naultf026bc22017-09-01 17:58:51 +0200878 if (!tunnel->sock) {
879 error = -ENOENT;
Guillaume Nault3953ae72017-10-27 16:51:50 +0200880 goto err;
Guillaume Naultf026bc22017-09-01 17:58:51 +0200881 }
James Chapman309795f2010-04-02 06:19:10 +0000882
James Chapman309795f2010-04-02 06:19:10 +0000883 /* Allocate and initialize a new session context. */
James Chapman309795f2010-04-02 06:19:10 +0000884 session = l2tp_session_create(sizeof(struct pppol2tp_session),
885 tunnel, session_id,
886 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200887 if (IS_ERR(session)) {
888 error = PTR_ERR(session);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200889 goto err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200890 }
James Chapman309795f2010-04-02 06:19:10 +0000891
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200892 pppol2tp_session_init(session);
James Chapman309795f2010-04-02 06:19:10 +0000893
Guillaume Nault3953ae72017-10-27 16:51:50 +0200894 error = l2tp_session_register(session, tunnel);
895 if (error < 0)
896 goto err_sess;
James Chapman309795f2010-04-02 06:19:10 +0000897
Guillaume Nault3953ae72017-10-27 16:51:50 +0200898 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000899
Guillaume Nault3953ae72017-10-27 16:51:50 +0200900err_sess:
901 kfree(session);
902err:
James Chapman309795f2010-04-02 06:19:10 +0000903 return error;
904}
905
James Chapman309795f2010-04-02 06:19:10 +0000906#endif /* CONFIG_L2TP_V3 */
907
James Chapmanfd558d12010-04-02 06:18:33 +0000908/* getname() support.
909 */
910static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100911 int peer)
James Chapmanfd558d12010-04-02 06:18:33 +0000912{
James Chapmane0d44352010-04-02 06:18:54 +0000913 int len = 0;
James Chapmanfd558d12010-04-02 06:18:33 +0000914 int error = 0;
915 struct l2tp_session *session;
916 struct l2tp_tunnel *tunnel;
917 struct sock *sk = sock->sk;
918 struct inet_sock *inet;
919 struct pppol2tp_session *pls;
920
921 error = -ENOTCONN;
922 if (sk == NULL)
923 goto end;
Gao Feng56cff472016-08-19 13:36:23 +0800924 if (!(sk->sk_state & PPPOX_CONNECTED))
James Chapmanfd558d12010-04-02 06:18:33 +0000925 goto end;
926
927 error = -EBADF;
928 session = pppol2tp_sock_to_session(sk);
929 if (session == NULL)
930 goto end;
931
932 pls = l2tp_session_priv(session);
Guillaume Nault7198c772017-11-11 06:06:31 +0900933 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000934
Benjamin LaHaisebbdb32c2012-03-20 03:57:54 +0000935 inet = inet_sk(tunnel->sock);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000936 if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
James Chapmane0d44352010-04-02 06:18:54 +0000937 struct sockaddr_pppol2tp sp;
938 len = sizeof(sp);
939 memset(&sp, 0, len);
940 sp.sa_family = AF_PPPOX;
941 sp.sa_protocol = PX_PROTO_OL2TP;
942 sp.pppol2tp.fd = tunnel->fd;
943 sp.pppol2tp.pid = pls->owner;
944 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
945 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
946 sp.pppol2tp.s_session = session->session_id;
947 sp.pppol2tp.d_session = session->peer_session_id;
948 sp.pppol2tp.addr.sin_family = AF_INET;
949 sp.pppol2tp.addr.sin_port = inet->inet_dport;
950 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
951 memcpy(uaddr, &sp, len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000952#if IS_ENABLED(CONFIG_IPV6)
953 } else if ((tunnel->version == 2) &&
954 (tunnel->sock->sk_family == AF_INET6)) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000955 struct sockaddr_pppol2tpin6 sp;
Eric Dumazetefe42082013-10-03 15:42:29 -0700956
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000957 len = sizeof(sp);
958 memset(&sp, 0, len);
959 sp.sa_family = AF_PPPOX;
960 sp.sa_protocol = PX_PROTO_OL2TP;
961 sp.pppol2tp.fd = tunnel->fd;
962 sp.pppol2tp.pid = pls->owner;
963 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
964 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
965 sp.pppol2tp.s_session = session->session_id;
966 sp.pppol2tp.d_session = session->peer_session_id;
967 sp.pppol2tp.addr.sin6_family = AF_INET6;
968 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700969 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
970 sizeof(tunnel->sock->sk_v6_daddr));
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000971 memcpy(uaddr, &sp, len);
972 } else if ((tunnel->version == 3) &&
973 (tunnel->sock->sk_family == AF_INET6)) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000974 struct sockaddr_pppol2tpv3in6 sp;
Eric Dumazetefe42082013-10-03 15:42:29 -0700975
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000976 len = sizeof(sp);
977 memset(&sp, 0, len);
978 sp.sa_family = AF_PPPOX;
979 sp.sa_protocol = PX_PROTO_OL2TP;
980 sp.pppol2tp.fd = tunnel->fd;
981 sp.pppol2tp.pid = pls->owner;
982 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
983 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
984 sp.pppol2tp.s_session = session->session_id;
985 sp.pppol2tp.d_session = session->peer_session_id;
986 sp.pppol2tp.addr.sin6_family = AF_INET6;
987 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700988 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
989 sizeof(tunnel->sock->sk_v6_daddr));
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000990 memcpy(uaddr, &sp, len);
991#endif
James Chapmane0d44352010-04-02 06:18:54 +0000992 } else if (tunnel->version == 3) {
993 struct sockaddr_pppol2tpv3 sp;
994 len = sizeof(sp);
995 memset(&sp, 0, len);
996 sp.sa_family = AF_PPPOX;
997 sp.sa_protocol = PX_PROTO_OL2TP;
998 sp.pppol2tp.fd = tunnel->fd;
999 sp.pppol2tp.pid = pls->owner;
1000 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
1001 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
1002 sp.pppol2tp.s_session = session->session_id;
1003 sp.pppol2tp.d_session = session->peer_session_id;
1004 sp.pppol2tp.addr.sin_family = AF_INET;
1005 sp.pppol2tp.addr.sin_port = inet->inet_dport;
1006 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
1007 memcpy(uaddr, &sp, len);
1008 }
James Chapmanfd558d12010-04-02 06:18:33 +00001009
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001010 error = len;
James Chapmanfd558d12010-04-02 06:18:33 +00001011
James Chapmanfd558d12010-04-02 06:18:33 +00001012 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001013end:
1014 return error;
1015}
1016
1017/****************************************************************************
1018 * ioctl() handlers.
1019 *
1020 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1021 * sockets. However, in order to control kernel tunnel features, we allow
1022 * userspace to create a special "tunnel" PPPoX socket which is used for
1023 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1024 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1025 * calls.
1026 ****************************************************************************/
1027
1028static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1029 struct l2tp_stats *stats)
1030{
Tom Parkin7b7c0712013-03-19 06:11:22 +00001031 dest->tx_packets = atomic_long_read(&stats->tx_packets);
1032 dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1033 dest->tx_errors = atomic_long_read(&stats->tx_errors);
1034 dest->rx_packets = atomic_long_read(&stats->rx_packets);
1035 dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1036 dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1037 dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1038 dest->rx_errors = atomic_long_read(&stats->rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001039}
1040
1041/* Session ioctl helper.
1042 */
1043static int pppol2tp_session_ioctl(struct l2tp_session *session,
1044 unsigned int cmd, unsigned long arg)
1045{
James Chapmanfd558d12010-04-02 06:18:33 +00001046 int err = 0;
1047 struct sock *sk;
1048 int val = (int) arg;
James Chapmanfd558d12010-04-02 06:18:33 +00001049 struct l2tp_tunnel *tunnel = session->tunnel;
1050 struct pppol2tp_ioc_stats stats;
1051
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001052 l2tp_dbg(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001053 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1054 session->name, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001055
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001056 sk = pppol2tp_session_get_sock(session);
Guillaume Nault5903f592017-10-13 19:22:35 +02001057 if (!sk)
1058 return -EBADR;
1059
James Chapmanfd558d12010-04-02 06:18:33 +00001060 switch (cmd) {
James Chapmanfd558d12010-04-02 06:18:33 +00001061 case PPPIOCGMRU:
James Chapmanfd558d12010-04-02 06:18:33 +00001062 case PPPIOCGFLAGS:
1063 err = -EFAULT;
Guillaume Nault1998b5e2018-07-27 10:59:59 +02001064 if (put_user(0, (int __user *)arg))
James Chapmanfd558d12010-04-02 06:18:33 +00001065 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001066 err = 0;
1067 break;
1068
Guillaume Nault92ea4a72018-07-27 11:00:00 +02001069 case PPPIOCSMRU:
James Chapmanfd558d12010-04-02 06:18:33 +00001070 case PPPIOCSFLAGS:
1071 err = -EFAULT;
Guillaume Nault1998b5e2018-07-27 10:59:59 +02001072 if (get_user(val, (int __user *)arg))
James Chapmanfd558d12010-04-02 06:18:33 +00001073 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001074 err = 0;
1075 break;
1076
1077 case PPPIOCGL2TPSTATS:
1078 err = -ENXIO;
1079 if (!(sk->sk_state & PPPOX_CONNECTED))
1080 break;
1081
1082 memset(&stats, 0, sizeof(stats));
1083 stats.tunnel_id = tunnel->tunnel_id;
1084 stats.session_id = session->session_id;
1085 pppol2tp_copy_stats(&stats, &session->stats);
1086 if (copy_to_user((void __user *) arg, &stats,
1087 sizeof(stats)))
1088 break;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001089 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001090 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001091 err = 0;
1092 break;
1093
1094 default:
1095 err = -ENOSYS;
1096 break;
1097 }
1098
1099 sock_put(sk);
1100
1101 return err;
1102}
1103
1104/* Tunnel ioctl helper.
1105 *
1106 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1107 * specifies a session_id, the session ioctl handler is called. This allows an
1108 * application to retrieve session stats via a tunnel socket.
1109 */
1110static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1111 unsigned int cmd, unsigned long arg)
1112{
1113 int err = 0;
1114 struct sock *sk;
1115 struct pppol2tp_ioc_stats stats;
1116
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001117 l2tp_dbg(tunnel, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001118 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1119 tunnel->name, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001120
1121 sk = tunnel->sock;
1122 sock_hold(sk);
1123
1124 switch (cmd) {
1125 case PPPIOCGL2TPSTATS:
1126 err = -ENXIO;
1127 if (!(sk->sk_state & PPPOX_CONNECTED))
1128 break;
1129
1130 if (copy_from_user(&stats, (void __user *) arg,
1131 sizeof(stats))) {
1132 err = -EFAULT;
1133 break;
1134 }
1135 if (stats.session_id != 0) {
1136 /* resend to session ioctl handler */
Guillaume Nault01e28b92018-08-10 13:21:57 +02001137 struct l2tp_session *session;
Guillaume Nault57377d62017-03-31 13:02:26 +02001138
Guillaume Nault01e28b92018-08-10 13:21:57 +02001139 session = l2tp_tunnel_get_session(tunnel,
1140 stats.session_id);
Guillaume Naultf664e37d2018-08-03 17:00:11 +02001141 if (!session) {
James Chapmanfd558d12010-04-02 06:18:33 +00001142 err = -EBADR;
Guillaume Naultf664e37d2018-08-03 17:00:11 +02001143 break;
Guillaume Nault57377d62017-03-31 13:02:26 +02001144 }
Guillaume Naultf664e37d2018-08-03 17:00:11 +02001145 if (session->pwtype != L2TP_PWTYPE_PPP) {
1146 l2tp_session_dec_refcount(session);
1147 err = -EBADR;
1148 break;
1149 }
1150
1151 err = pppol2tp_session_ioctl(session, cmd, arg);
1152 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001153 break;
1154 }
Guillaume Naultd6a61ec2018-08-10 13:21:55 +02001155 stats.using_ipsec = l2tp_tunnel_uses_xfrm(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001156 pppol2tp_copy_stats(&stats, &tunnel->stats);
1157 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1158 err = -EFAULT;
1159 break;
1160 }
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001161 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001162 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001163 err = 0;
1164 break;
1165
1166 default:
1167 err = -ENOSYS;
1168 break;
1169 }
1170
1171 sock_put(sk);
1172
1173 return err;
1174}
1175
1176/* Main ioctl() handler.
1177 * Dispatch to tunnel or session helpers depending on the socket.
1178 */
1179static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1180 unsigned long arg)
1181{
1182 struct sock *sk = sock->sk;
1183 struct l2tp_session *session;
1184 struct l2tp_tunnel *tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001185 int err;
1186
1187 if (!sk)
1188 return 0;
1189
1190 err = -EBADF;
1191 if (sock_flag(sk, SOCK_DEAD) != 0)
1192 goto end;
1193
1194 err = -ENOTCONN;
1195 if ((sk->sk_user_data == NULL) ||
1196 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1197 goto end;
1198
1199 /* Get session context from the socket */
1200 err = -EBADF;
1201 session = pppol2tp_sock_to_session(sk);
1202 if (session == NULL)
1203 goto end;
1204
1205 /* Special case: if session's session_id is zero, treat ioctl as a
1206 * tunnel ioctl
1207 */
James Chapmanfd558d12010-04-02 06:18:33 +00001208 if ((session->session_id == 0) &&
1209 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001210 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001211 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001212 goto end_put_sess;
1213 }
1214
1215 err = pppol2tp_session_ioctl(session, cmd, arg);
1216
1217end_put_sess:
1218 sock_put(sk);
1219end:
1220 return err;
1221}
1222
1223/*****************************************************************************
1224 * setsockopt() / getsockopt() support.
1225 *
1226 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1227 * sockets. In order to control kernel tunnel features, we allow userspace to
1228 * create a special "tunnel" PPPoX socket which is used for control only.
1229 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1230 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1231 *****************************************************************************/
1232
1233/* Tunnel setsockopt() helper.
1234 */
1235static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1236 struct l2tp_tunnel *tunnel,
1237 int optname, int val)
1238{
1239 int err = 0;
1240
1241 switch (optname) {
1242 case PPPOL2TP_SO_DEBUG:
1243 tunnel->debug = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001244 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001245 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001246 break;
1247
1248 default:
1249 err = -ENOPROTOOPT;
1250 break;
1251 }
1252
1253 return err;
1254}
1255
1256/* Session setsockopt helper.
1257 */
1258static int pppol2tp_session_setsockopt(struct sock *sk,
1259 struct l2tp_session *session,
1260 int optname, int val)
1261{
1262 int err = 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001263
1264 switch (optname) {
1265 case PPPOL2TP_SO_RECVSEQ:
1266 if ((val != 0) && (val != 1)) {
1267 err = -EINVAL;
1268 break;
1269 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001270 session->recv_seq = !!val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001271 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001272 "%s: set recv_seq=%d\n",
1273 session->name, session->recv_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001274 break;
1275
1276 case PPPOL2TP_SO_SENDSEQ:
1277 if ((val != 0) && (val != 1)) {
1278 err = -EINVAL;
1279 break;
1280 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001281 session->send_seq = !!val;
James Chapmanfd558d12010-04-02 06:18:33 +00001282 {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001283 struct pppox_sock *po = pppox_sk(sk);
1284
James Chapmanfd558d12010-04-02 06:18:33 +00001285 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1286 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1287 }
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001288 l2tp_session_set_header_len(session, session->tunnel->version);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001289 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001290 "%s: set send_seq=%d\n",
1291 session->name, session->send_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001292 break;
1293
1294 case PPPOL2TP_SO_LNSMODE:
1295 if ((val != 0) && (val != 1)) {
1296 err = -EINVAL;
1297 break;
1298 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001299 session->lns_mode = !!val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001300 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001301 "%s: set lns_mode=%d\n",
1302 session->name, session->lns_mode);
James Chapmanfd558d12010-04-02 06:18:33 +00001303 break;
1304
1305 case PPPOL2TP_SO_DEBUG:
1306 session->debug = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001307 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001308 session->name, session->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001309 break;
1310
1311 case PPPOL2TP_SO_REORDERTO:
1312 session->reorder_timeout = msecs_to_jiffies(val);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001313 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001314 "%s: set reorder_timeout=%d\n",
1315 session->name, session->reorder_timeout);
James Chapmanfd558d12010-04-02 06:18:33 +00001316 break;
1317
1318 default:
1319 err = -ENOPROTOOPT;
1320 break;
1321 }
1322
1323 return err;
1324}
1325
1326/* Main setsockopt() entry point.
1327 * Does API checks, then calls either the tunnel or session setsockopt
1328 * handler, according to whether the PPPoL2TP socket is a for a regular
1329 * session or the special tunnel type.
1330 */
1331static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1332 char __user *optval, unsigned int optlen)
1333{
1334 struct sock *sk = sock->sk;
1335 struct l2tp_session *session;
1336 struct l2tp_tunnel *tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001337 int val;
1338 int err;
1339
1340 if (level != SOL_PPPOL2TP)
Sasha Levin3cf521f2014-07-14 17:02:31 -07001341 return -EINVAL;
James Chapmanfd558d12010-04-02 06:18:33 +00001342
1343 if (optlen < sizeof(int))
1344 return -EINVAL;
1345
1346 if (get_user(val, (int __user *)optval))
1347 return -EFAULT;
1348
1349 err = -ENOTCONN;
1350 if (sk->sk_user_data == NULL)
1351 goto end;
1352
1353 /* Get session context from the socket */
1354 err = -EBADF;
1355 session = pppol2tp_sock_to_session(sk);
1356 if (session == NULL)
1357 goto end;
1358
1359 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1360 */
James Chapmanfd558d12010-04-02 06:18:33 +00001361 if ((session->session_id == 0) &&
1362 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001363 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001364 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
Guillaume Nault7198c772017-11-11 06:06:31 +09001365 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001366 err = pppol2tp_session_setsockopt(sk, session, optname, val);
Guillaume Nault7198c772017-11-11 06:06:31 +09001367 }
James Chapmanfd558d12010-04-02 06:18:33 +00001368
James Chapmanfd558d12010-04-02 06:18:33 +00001369 sock_put(sk);
1370end:
1371 return err;
1372}
1373
1374/* Tunnel getsockopt helper. Called with sock locked.
1375 */
1376static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1377 struct l2tp_tunnel *tunnel,
1378 int optname, int *val)
1379{
1380 int err = 0;
1381
1382 switch (optname) {
1383 case PPPOL2TP_SO_DEBUG:
1384 *val = tunnel->debug;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001385 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001386 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001387 break;
1388
1389 default:
1390 err = -ENOPROTOOPT;
1391 break;
1392 }
1393
1394 return err;
1395}
1396
1397/* Session getsockopt helper. Called with sock locked.
1398 */
1399static int pppol2tp_session_getsockopt(struct sock *sk,
1400 struct l2tp_session *session,
1401 int optname, int *val)
1402{
1403 int err = 0;
1404
1405 switch (optname) {
1406 case PPPOL2TP_SO_RECVSEQ:
1407 *val = session->recv_seq;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001408 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001409 "%s: get recv_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001410 break;
1411
1412 case PPPOL2TP_SO_SENDSEQ:
1413 *val = session->send_seq;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001414 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001415 "%s: get send_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001416 break;
1417
1418 case PPPOL2TP_SO_LNSMODE:
1419 *val = session->lns_mode;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001420 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001421 "%s: get lns_mode=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001422 break;
1423
1424 case PPPOL2TP_SO_DEBUG:
1425 *val = session->debug;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001426 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001427 session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001428 break;
1429
1430 case PPPOL2TP_SO_REORDERTO:
1431 *val = (int) jiffies_to_msecs(session->reorder_timeout);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001432 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001433 "%s: get reorder_timeout=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001434 break;
1435
1436 default:
1437 err = -ENOPROTOOPT;
1438 }
1439
1440 return err;
1441}
1442
1443/* Main getsockopt() entry point.
1444 * Does API checks, then calls either the tunnel or session getsockopt
1445 * handler, according to whether the PPPoX socket is a for a regular session
1446 * or the special tunnel type.
1447 */
Joe Perchese3192692012-06-03 17:41:40 +00001448static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1449 char __user *optval, int __user *optlen)
James Chapmanfd558d12010-04-02 06:18:33 +00001450{
1451 struct sock *sk = sock->sk;
1452 struct l2tp_session *session;
1453 struct l2tp_tunnel *tunnel;
1454 int val, len;
1455 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001456
1457 if (level != SOL_PPPOL2TP)
Sasha Levin3cf521f2014-07-14 17:02:31 -07001458 return -EINVAL;
James Chapmanfd558d12010-04-02 06:18:33 +00001459
Joe Perchese3192692012-06-03 17:41:40 +00001460 if (get_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001461 return -EFAULT;
1462
1463 len = min_t(unsigned int, len, sizeof(int));
1464
1465 if (len < 0)
1466 return -EINVAL;
1467
1468 err = -ENOTCONN;
1469 if (sk->sk_user_data == NULL)
1470 goto end;
1471
1472 /* Get the session context */
1473 err = -EBADF;
1474 session = pppol2tp_sock_to_session(sk);
1475 if (session == NULL)
1476 goto end;
1477
1478 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
James Chapmanfd558d12010-04-02 06:18:33 +00001479 if ((session->session_id == 0) &&
1480 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001481 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001482 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
Guillaume Nault321a52a2017-04-06 18:31:21 +02001483 if (err)
1484 goto end_put_sess;
1485 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001486 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
Guillaume Nault321a52a2017-04-06 18:31:21 +02001487 if (err)
1488 goto end_put_sess;
1489 }
James Chapmanfd558d12010-04-02 06:18:33 +00001490
1491 err = -EFAULT;
Joe Perchese3192692012-06-03 17:41:40 +00001492 if (put_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001493 goto end_put_sess;
1494
1495 if (copy_to_user((void __user *) optval, &val, len))
1496 goto end_put_sess;
1497
1498 err = 0;
1499
1500end_put_sess:
1501 sock_put(sk);
1502end:
1503 return err;
1504}
1505
1506/*****************************************************************************
1507 * /proc filesystem for debug
James Chapmanf7faffa2010-04-02 06:18:49 +00001508 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1509 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
James Chapmanfd558d12010-04-02 06:18:33 +00001510 *****************************************************************************/
1511
1512static unsigned int pppol2tp_net_id;
1513
1514#ifdef CONFIG_PROC_FS
1515
1516struct pppol2tp_seq_data {
1517 struct seq_net_private p;
1518 int tunnel_idx; /* current tunnel */
1519 int session_idx; /* index of session within current tunnel */
1520 struct l2tp_tunnel *tunnel;
1521 struct l2tp_session *session; /* NULL means get next tunnel */
1522};
1523
1524static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1525{
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001526 /* Drop reference taken during previous invocation */
1527 if (pd->tunnel)
1528 l2tp_tunnel_dec_refcount(pd->tunnel);
1529
James Chapmanf7faffa2010-04-02 06:18:49 +00001530 for (;;) {
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001531 pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
James Chapmanf7faffa2010-04-02 06:18:49 +00001532 pd->tunnel_idx++;
1533
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001534 /* Only accept L2TPv2 tunnels */
1535 if (!pd->tunnel || pd->tunnel->version == 2)
1536 return;
James Chapmanf7faffa2010-04-02 06:18:49 +00001537
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001538 l2tp_tunnel_dec_refcount(pd->tunnel);
James Chapmanf7faffa2010-04-02 06:18:49 +00001539 }
James Chapmanfd558d12010-04-02 06:18:33 +00001540}
1541
1542static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1543{
Guillaume Nault83494402018-04-25 19:54:14 +02001544 /* Drop reference taken during previous invocation */
1545 if (pd->session)
1546 l2tp_session_dec_refcount(pd->session);
1547
Guillaume Naulta4346212017-10-31 17:36:42 +01001548 pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
James Chapmanfd558d12010-04-02 06:18:33 +00001549 pd->session_idx++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001550
James Chapmanfd558d12010-04-02 06:18:33 +00001551 if (pd->session == NULL) {
1552 pd->session_idx = 0;
1553 pppol2tp_next_tunnel(net, pd);
1554 }
1555}
1556
1557static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1558{
1559 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1560 loff_t pos = *offs;
1561 struct net *net;
1562
1563 if (!pos)
1564 goto out;
1565
1566 BUG_ON(m->private == NULL);
1567 pd = m->private;
1568 net = seq_file_net(m);
1569
1570 if (pd->tunnel == NULL)
1571 pppol2tp_next_tunnel(net, pd);
1572 else
1573 pppol2tp_next_session(net, pd);
1574
1575 /* NULL tunnel and session indicates end of list */
1576 if ((pd->tunnel == NULL) && (pd->session == NULL))
1577 pd = NULL;
1578
1579out:
1580 return pd;
1581}
1582
1583static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1584{
1585 (*pos)++;
1586 return NULL;
1587}
1588
1589static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1590{
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001591 struct pppol2tp_seq_data *pd = v;
1592
1593 if (!pd || pd == SEQ_START_TOKEN)
1594 return;
1595
Guillaume Nault83494402018-04-25 19:54:14 +02001596 /* Drop reference taken by last invocation of pppol2tp_next_session()
1597 * or pppol2tp_next_tunnel().
1598 */
1599 if (pd->session) {
1600 l2tp_session_dec_refcount(pd->session);
1601 pd->session = NULL;
1602 }
Guillaume Nault5411b6182018-04-19 16:20:48 +02001603 if (pd->tunnel) {
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001604 l2tp_tunnel_dec_refcount(pd->tunnel);
Guillaume Nault5411b6182018-04-19 16:20:48 +02001605 pd->tunnel = NULL;
Guillaume Nault5411b6182018-04-19 16:20:48 +02001606 }
James Chapmanfd558d12010-04-02 06:18:33 +00001607}
1608
1609static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1610{
1611 struct l2tp_tunnel *tunnel = v;
1612
1613 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1614 tunnel->name,
1615 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001616 refcount_read(&tunnel->ref_count) - 1);
Tom Parkin7b7c0712013-03-19 06:11:22 +00001617 seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001618 tunnel->debug,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001619 atomic_long_read(&tunnel->stats.tx_packets),
1620 atomic_long_read(&tunnel->stats.tx_bytes),
1621 atomic_long_read(&tunnel->stats.tx_errors),
1622 atomic_long_read(&tunnel->stats.rx_packets),
1623 atomic_long_read(&tunnel->stats.rx_bytes),
1624 atomic_long_read(&tunnel->stats.rx_errors));
James Chapmanfd558d12010-04-02 06:18:33 +00001625}
1626
1627static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1628{
1629 struct l2tp_session *session = v;
1630 struct l2tp_tunnel *tunnel = session->tunnel;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001631 unsigned char state;
1632 char user_data_ok;
1633 struct sock *sk;
James Chapmanfd558d12010-04-02 06:18:33 +00001634 u32 ip = 0;
1635 u16 port = 0;
1636
1637 if (tunnel->sock) {
1638 struct inet_sock *inet = inet_sk(tunnel->sock);
1639 ip = ntohl(inet->inet_saddr);
1640 port = ntohs(inet->inet_sport);
1641 }
1642
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001643 sk = pppol2tp_session_get_sock(session);
1644 if (sk) {
1645 state = sk->sk_state;
1646 user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1647 } else {
1648 state = 0;
1649 user_data_ok = 'N';
1650 }
1651
James Chapmanfd558d12010-04-02 06:18:33 +00001652 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1653 "%04X/%04X %d %c\n",
1654 session->name, ip, port,
1655 tunnel->tunnel_id,
1656 session->session_id,
1657 tunnel->peer_tunnel_id,
1658 session->peer_session_id,
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001659 state, user_data_ok);
Guillaume Nault789141b2018-08-03 12:38:37 +02001660 seq_printf(m, " 0/0/%c/%c/%s %08x %u\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001661 session->recv_seq ? 'R' : '-',
1662 session->send_seq ? 'S' : '-',
1663 session->lns_mode ? "LNS" : "LAC",
1664 session->debug,
1665 jiffies_to_msecs(session->reorder_timeout));
Tom Parkin7b7c0712013-03-19 06:11:22 +00001666 seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001667 session->nr, session->ns,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001668 atomic_long_read(&session->stats.tx_packets),
1669 atomic_long_read(&session->stats.tx_bytes),
1670 atomic_long_read(&session->stats.tx_errors),
1671 atomic_long_read(&session->stats.rx_packets),
1672 atomic_long_read(&session->stats.rx_bytes),
1673 atomic_long_read(&session->stats.rx_errors));
James Chapman93454712010-04-02 06:18:44 +00001674
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001675 if (sk) {
1676 struct pppox_sock *po = pppox_sk(sk);
1677
James Chapman93454712010-04-02 06:18:44 +00001678 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001679 sock_put(sk);
1680 }
James Chapmanfd558d12010-04-02 06:18:33 +00001681}
1682
1683static int pppol2tp_seq_show(struct seq_file *m, void *v)
1684{
1685 struct pppol2tp_seq_data *pd = v;
1686
1687 /* display header on line 1 */
1688 if (v == SEQ_START_TOKEN) {
1689 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1690 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1691 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1692 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1693 "dest-tid/sid state user-data-ok\n");
1694 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1695 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1696 goto out;
1697 }
1698
Guillaume Nault83494402018-04-25 19:54:14 +02001699 if (!pd->session)
James Chapmanfd558d12010-04-02 06:18:33 +00001700 pppol2tp_seq_tunnel_show(m, pd->tunnel);
Guillaume Nault83494402018-04-25 19:54:14 +02001701 else
James Chapmanfd558d12010-04-02 06:18:33 +00001702 pppol2tp_seq_session_show(m, pd->session);
1703
1704out:
1705 return 0;
1706}
1707
1708static const struct seq_operations pppol2tp_seq_ops = {
1709 .start = pppol2tp_seq_start,
1710 .next = pppol2tp_seq_next,
1711 .stop = pppol2tp_seq_stop,
1712 .show = pppol2tp_seq_show,
1713};
James Chapmanfd558d12010-04-02 06:18:33 +00001714#endif /* CONFIG_PROC_FS */
1715
1716/*****************************************************************************
1717 * Network namespace
1718 *****************************************************************************/
1719
1720static __net_init int pppol2tp_init_net(struct net *net)
1721{
1722 struct proc_dir_entry *pde;
1723 int err = 0;
1724
Christoph Hellwigc3506372018-04-10 19:42:55 +02001725 pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1726 &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
James Chapmanfd558d12010-04-02 06:18:33 +00001727 if (!pde) {
1728 err = -ENOMEM;
1729 goto out;
1730 }
1731
1732out:
1733 return err;
1734}
1735
1736static __net_exit void pppol2tp_exit_net(struct net *net)
1737{
Gao fengece31ff2013-02-18 01:34:56 +00001738 remove_proc_entry("pppol2tp", net->proc_net);
James Chapmanfd558d12010-04-02 06:18:33 +00001739}
1740
1741static struct pernet_operations pppol2tp_net_ops = {
1742 .init = pppol2tp_init_net,
1743 .exit = pppol2tp_exit_net,
1744 .id = &pppol2tp_net_id,
1745};
1746
1747/*****************************************************************************
1748 * Init and cleanup
1749 *****************************************************************************/
1750
1751static const struct proto_ops pppol2tp_ops = {
1752 .family = AF_PPPOX,
1753 .owner = THIS_MODULE,
1754 .release = pppol2tp_release,
1755 .bind = sock_no_bind,
1756 .connect = pppol2tp_connect,
1757 .socketpair = sock_no_socketpair,
1758 .accept = sock_no_accept,
1759 .getname = pppol2tp_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001760 .poll = datagram_poll,
James Chapmanfd558d12010-04-02 06:18:33 +00001761 .listen = sock_no_listen,
1762 .shutdown = sock_no_shutdown,
1763 .setsockopt = pppol2tp_setsockopt,
1764 .getsockopt = pppol2tp_getsockopt,
1765 .sendmsg = pppol2tp_sendmsg,
1766 .recvmsg = pppol2tp_recvmsg,
1767 .mmap = sock_no_mmap,
1768 .ioctl = pppox_ioctl,
1769};
1770
Eric Dumazet756e64a2010-09-21 06:43:54 +00001771static const struct pppox_proto pppol2tp_proto = {
James Chapmanfd558d12010-04-02 06:18:33 +00001772 .create = pppol2tp_create,
Wei Yongjune1558a92013-07-02 09:02:07 +08001773 .ioctl = pppol2tp_ioctl,
1774 .owner = THIS_MODULE,
James Chapmanfd558d12010-04-02 06:18:33 +00001775};
1776
James Chapman309795f2010-04-02 06:19:10 +00001777#ifdef CONFIG_L2TP_V3
1778
1779static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1780 .session_create = pppol2tp_session_create,
Tom Parkincf2f5c82013-03-19 06:11:21 +00001781 .session_delete = l2tp_session_delete,
James Chapman309795f2010-04-02 06:19:10 +00001782};
1783
1784#endif /* CONFIG_L2TP_V3 */
1785
James Chapmanfd558d12010-04-02 06:18:33 +00001786static int __init pppol2tp_init(void)
1787{
1788 int err;
1789
1790 err = register_pernet_device(&pppol2tp_net_ops);
1791 if (err)
1792 goto out;
1793
1794 err = proto_register(&pppol2tp_sk_proto, 0);
1795 if (err)
1796 goto out_unregister_pppol2tp_pernet;
1797
1798 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1799 if (err)
1800 goto out_unregister_pppol2tp_proto;
1801
James Chapman309795f2010-04-02 06:19:10 +00001802#ifdef CONFIG_L2TP_V3
1803 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1804 if (err)
1805 goto out_unregister_pppox;
1806#endif
1807
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001808 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001809
1810out:
1811 return err;
James Chapman309795f2010-04-02 06:19:10 +00001812
1813#ifdef CONFIG_L2TP_V3
1814out_unregister_pppox:
1815 unregister_pppox_proto(PX_PROTO_OL2TP);
1816#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001817out_unregister_pppol2tp_proto:
1818 proto_unregister(&pppol2tp_sk_proto);
1819out_unregister_pppol2tp_pernet:
1820 unregister_pernet_device(&pppol2tp_net_ops);
1821 goto out;
1822}
1823
1824static void __exit pppol2tp_exit(void)
1825{
James Chapman309795f2010-04-02 06:19:10 +00001826#ifdef CONFIG_L2TP_V3
1827 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1828#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001829 unregister_pppox_proto(PX_PROTO_OL2TP);
1830 proto_unregister(&pppol2tp_sk_proto);
1831 unregister_pernet_device(&pppol2tp_net_ops);
1832}
1833
1834module_init(pppol2tp_init);
1835module_exit(pppol2tp_exit);
1836
1837MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1838MODULE_DESCRIPTION("PPP over L2TP over UDP");
1839MODULE_LICENSE("GPL");
1840MODULE_VERSION(PPPOL2TP_DRV_VERSION);
Guillaume Nault681b4d82015-12-02 16:27:39 +01001841MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
Guillaume Nault249ee812017-04-03 13:23:15 +02001842MODULE_ALIAS_L2TP_PWTYPE(7);