blob: 44cac66284a503cd7e0eff4fc150590926bd663a [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>
96#include <net/dst.h>
97#include <net/ip.h>
98#include <net/udp.h>
99#include <net/xfrm.h>
Tom Parkincf2f5c82013-03-19 06:11:21 +0000100#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +0000101
102#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -0700103#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +0000104
105#include "l2tp_core.h"
106
107#define PPPOL2TP_DRV_VERSION "V2.0"
108
109/* Space for UDP, L2TP and PPP headers */
110#define PPPOL2TP_HEADER_OVERHEAD 40
111
James Chapmanfd558d12010-04-02 06:18:33 +0000112/* Number of bytes to build transmit L2TP headers.
113 * Unfortunately the size is different depending on whether sequence numbers
114 * are enabled.
115 */
116#define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
117#define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
118
119/* Private data of each session. This data lives at the end of struct
120 * l2tp_session, referenced via session->priv[].
121 */
122struct pppol2tp_session {
123 int owner; /* pid that opened the socket */
124
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200125 struct mutex sk_lock; /* Protects .sk */
126 struct sock __rcu *sk; /* Pointer to the session
James Chapmanfd558d12010-04-02 06:18:33 +0000127 * PPPoX socket */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200128 struct sock *__sk; /* Copy of .sk, for cleanup */
129 struct rcu_head rcu; /* For asynchronous release */
James Chapmanfd558d12010-04-02 06:18:33 +0000130};
131
132static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
133
stephen hemmingerd7100da2010-08-04 07:34:36 +0000134static const struct ppp_channel_ops pppol2tp_chan_ops = {
135 .start_xmit = pppol2tp_xmit,
136};
137
James Chapmanfd558d12010-04-02 06:18:33 +0000138static const struct proto_ops pppol2tp_ops;
139
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200140/* Retrieves the pppol2tp socket associated to a session.
141 * A reference is held on the returned socket, so this function must be paired
142 * with sock_put().
143 */
144static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
145{
146 struct pppol2tp_session *ps = l2tp_session_priv(session);
147 struct sock *sk;
148
149 rcu_read_lock();
150 sk = rcu_dereference(ps->sk);
151 if (sk)
152 sock_hold(sk);
153 rcu_read_unlock();
154
155 return sk;
156}
157
James Chapmanfd558d12010-04-02 06:18:33 +0000158/* Helpers to obtain tunnel/session contexts from sockets.
159 */
160static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
161{
162 struct l2tp_session *session;
163
164 if (sk == NULL)
165 return NULL;
166
167 sock_hold(sk);
168 session = (struct l2tp_session *)(sk->sk_user_data);
169 if (session == NULL) {
170 sock_put(sk);
171 goto out;
172 }
173
174 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
175
176out:
177 return session;
178}
179
180/*****************************************************************************
181 * Receive data handling
182 *****************************************************************************/
183
James Chapmanfd558d12010-04-02 06:18:33 +0000184/* Receive message. This is the recvmsg for the PPPoL2TP socket.
185 */
Ying Xue1b784142015-03-02 15:37:48 +0800186static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
187 size_t len, int flags)
James Chapmanfd558d12010-04-02 06:18:33 +0000188{
189 int err;
190 struct sk_buff *skb;
191 struct sock *sk = sock->sk;
192
193 err = -EIO;
194 if (sk->sk_state & PPPOX_BOUND)
195 goto end;
196
James Chapmanfd558d12010-04-02 06:18:33 +0000197 err = 0;
198 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
199 flags & MSG_DONTWAIT, &err);
200 if (!skb)
201 goto end;
202
203 if (len > skb->len)
204 len = skb->len;
205 else if (len < skb->len)
206 msg->msg_flags |= MSG_TRUNC;
207
David S. Miller51f3d022014-11-05 16:46:40 -0500208 err = skb_copy_datagram_msg(skb, 0, msg, len);
James Chapmanfd558d12010-04-02 06:18:33 +0000209 if (likely(err == 0))
210 err = len;
211
212 kfree_skb(skb);
213end:
214 return err;
215}
216
217static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
218{
219 struct pppol2tp_session *ps = l2tp_session_priv(session);
220 struct sock *sk = NULL;
221
222 /* If the socket is bound, send it in to PPP's input queue. Otherwise
223 * queue it on the session socket.
224 */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200225 rcu_read_lock();
226 sk = rcu_dereference(ps->sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000227 if (sk == NULL)
228 goto no_sock;
229
Guillaume Nault2b139e62018-07-25 14:53:33 +0200230 /* If the first two bytes are 0xFF03, consider that it is the PPP's
231 * Address and Control fields and skip them. The L2TP module has always
232 * worked this way, although, in theory, the use of these fields should
233 * be negociated and handled at the PPP layer. These fields are
234 * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered
235 * Information command with Poll/Final bit set to zero (RFC 1662).
236 */
237 if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS &&
238 skb->data[1] == PPP_UI)
239 skb_pull(skb, 2);
240
James Chapmanfd558d12010-04-02 06:18:33 +0000241 if (sk->sk_state & PPPOX_BOUND) {
242 struct pppox_sock *po;
Guillaume Nault98f40b32015-12-29 13:06:59 +0100243
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000244 l2tp_dbg(session, L2TP_MSG_DATA,
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000245 "%s: recv %d byte data frame, passing to ppp\n",
246 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000247
James Chapmanfd558d12010-04-02 06:18:33 +0000248 po = pppox_sk(sk);
249 ppp_input(&po->chan, skb);
250 } else {
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000251 l2tp_dbg(session, L2TP_MSG_DATA,
Guillaume Nault9e9cb622014-03-06 11:15:10 +0100252 "%s: recv %d byte data frame, passing to L2TP socket\n",
253 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000254
Guillaume Nault9e9cb622014-03-06 11:15:10 +0100255 if (sock_queue_rcv_skb(sk, skb) < 0) {
256 atomic_long_inc(&session->stats.rx_errors);
257 kfree_skb(skb);
258 }
James Chapmanfd558d12010-04-02 06:18:33 +0000259 }
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200260 rcu_read_unlock();
James Chapmanfd558d12010-04-02 06:18:33 +0000261
262 return;
263
264no_sock:
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200265 rcu_read_unlock();
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000266 l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000267 kfree_skb(skb);
268}
269
James Chapmanfd558d12010-04-02 06:18:33 +0000270/************************************************************************
271 * Transmit handling
272 ***********************************************************************/
273
James Chapmanfd558d12010-04-02 06:18:33 +0000274/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
275 * when a user application does a sendmsg() on the session socket. L2TP and
276 * PPP headers must be inserted into the user's data.
277 */
Ying Xue1b784142015-03-02 15:37:48 +0800278static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
James Chapmanfd558d12010-04-02 06:18:33 +0000279 size_t total_len)
280{
James Chapmanfd558d12010-04-02 06:18:33 +0000281 struct sock *sk = sock->sk;
282 struct sk_buff *skb;
283 int error;
284 struct l2tp_session *session;
285 struct l2tp_tunnel *tunnel;
James Chapman0d767512010-04-02 06:19:00 +0000286 int uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +0000287
288 error = -ENOTCONN;
289 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
290 goto error;
291
292 /* Get session and tunnel contexts */
293 error = -EBADF;
294 session = pppol2tp_sock_to_session(sk);
295 if (session == NULL)
296 goto error;
297
Guillaume Nault7198c772017-11-11 06:06:31 +0900298 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000299
James Chapman0d767512010-04-02 06:19:00 +0000300 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
301
James Chapmanfd558d12010-04-02 06:18:33 +0000302 /* Allocate a socket buffer */
303 error = -ENOMEM;
304 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +0000305 uhlen + session->hdr_len +
Gao Feng54c151d2016-08-22 22:50:02 +0800306 2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
James Chapmanfd558d12010-04-02 06:18:33 +0000307 0, GFP_KERNEL);
308 if (!skb)
Guillaume Nault7198c772017-11-11 06:06:31 +0900309 goto error_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000310
311 /* Reserve space for headers. */
312 skb_reserve(skb, NET_SKB_PAD);
313 skb_reset_network_header(skb);
314 skb_reserve(skb, sizeof(struct iphdr));
315 skb_reset_transport_header(skb);
James Chapman0d767512010-04-02 06:19:00 +0000316 skb_reserve(skb, uhlen);
James Chapmanfd558d12010-04-02 06:18:33 +0000317
318 /* Add PPP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800319 skb->data[0] = PPP_ALLSTATIONS;
320 skb->data[1] = PPP_UI;
James Chapmanfd558d12010-04-02 06:18:33 +0000321 skb_put(skb, 2);
322
323 /* Copy user data into skb */
Al Viro6ce8e9c2014-04-06 21:25:44 -0400324 error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000325 if (error < 0) {
326 kfree_skb(skb);
Guillaume Nault7198c772017-11-11 06:06:31 +0900327 goto error_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000328 }
James Chapmanfd558d12010-04-02 06:18:33 +0000329
Eric Dumazet455cc322013-10-10 06:30:09 -0700330 local_bh_disable();
James Chapmanfd558d12010-04-02 06:18:33 +0000331 l2tp_xmit_skb(session, skb, session->hdr_len);
Eric Dumazet455cc322013-10-10 06:30:09 -0700332 local_bh_enable();
James Chapmanfd558d12010-04-02 06:18:33 +0000333
Guillaume Nault8b82547e33e2013-03-01 05:02:02 +0000334 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000335
Guillaume Naulta6f79d02013-06-12 16:07:36 +0200336 return total_len;
James Chapmanfd558d12010-04-02 06:18:33 +0000337
James Chapmanfd558d12010-04-02 06:18:33 +0000338error_put_sess:
339 sock_put(sk);
340error:
341 return error;
342}
343
344/* Transmit function called by generic PPP driver. Sends PPP frame
345 * over PPPoL2TP socket.
346 *
347 * This is almost the same as pppol2tp_sendmsg(), but rather than
348 * being called with a msghdr from userspace, it is called with a skb
349 * from the kernel.
350 *
351 * The supplied skb from ppp doesn't have enough headroom for the
352 * insertion of L2TP, UDP and IP headers so we need to allocate more
353 * headroom in the skb. This will create a cloned skb. But we must be
354 * careful in the error case because the caller will expect to free
355 * the skb it supplied, not our cloned skb. So we take care to always
356 * leave the original skb unfreed if we return an error.
357 */
358static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
359{
James Chapmanfd558d12010-04-02 06:18:33 +0000360 struct sock *sk = (struct sock *) chan->private;
James Chapmanfd558d12010-04-02 06:18:33 +0000361 struct l2tp_session *session;
362 struct l2tp_tunnel *tunnel;
Eric Dumazet09df57c2011-10-07 05:45:57 +0000363 int uhlen, headroom;
James Chapmanfd558d12010-04-02 06:18:33 +0000364
365 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
366 goto abort;
367
368 /* Get session and tunnel contexts from the socket */
369 session = pppol2tp_sock_to_session(sk);
370 if (session == NULL)
371 goto abort;
372
Guillaume Nault7198c772017-11-11 06:06:31 +0900373 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000374
Eric Dumazet09df57c2011-10-07 05:45:57 +0000375 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
376 headroom = NET_SKB_PAD +
377 sizeof(struct iphdr) + /* IP header */
378 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
379 session->hdr_len + /* L2TP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800380 2; /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
Eric Dumazet09df57c2011-10-07 05:45:57 +0000381 if (skb_cow_head(skb, headroom))
Guillaume Nault7198c772017-11-11 06:06:31 +0900382 goto abort_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000383
James Chapmanfd558d12010-04-02 06:18:33 +0000384 /* Setup PPP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800385 __skb_push(skb, 2);
386 skb->data[0] = PPP_ALLSTATIONS;
387 skb->data[1] = PPP_UI;
James Chapmanfd558d12010-04-02 06:18:33 +0000388
Eric Dumazet455cc322013-10-10 06:30:09 -0700389 local_bh_disable();
James Chapmane0d44352010-04-02 06:18:54 +0000390 l2tp_xmit_skb(session, skb, session->hdr_len);
Eric Dumazet455cc322013-10-10 06:30:09 -0700391 local_bh_enable();
James Chapmanfd558d12010-04-02 06:18:33 +0000392
James Chapmanfd558d12010-04-02 06:18:33 +0000393 sock_put(sk);
Guillaume Nault7198c772017-11-11 06:06:31 +0900394
James Chapmanfd558d12010-04-02 06:18:33 +0000395 return 1;
396
James Chapmanfd558d12010-04-02 06:18:33 +0000397abort_put_sess:
398 sock_put(sk);
399abort:
400 /* Free the original skb */
401 kfree_skb(skb);
402 return 1;
403}
404
405/*****************************************************************************
406 * Session (and tunnel control) socket create/destroy.
407 *****************************************************************************/
408
James Chapmand02ba2a2018-02-23 17:45:46 +0000409static void pppol2tp_put_sk(struct rcu_head *head)
410{
411 struct pppol2tp_session *ps;
412
413 ps = container_of(head, typeof(*ps), rcu);
414 sock_put(ps->__sk);
415}
416
James Chapmanfd558d12010-04-02 06:18:33 +0000417/* Really kill the session socket. (Called from sock_put() if
418 * refcnt == 0.)
419 */
420static void pppol2tp_session_destruct(struct sock *sk)
421{
Tom Parkinf6e16b22013-03-19 06:11:23 +0000422 struct l2tp_session *session = sk->sk_user_data;
Guillaume Naulte91793b2017-03-29 08:45:29 +0200423
424 skb_queue_purge(&sk->sk_receive_queue);
425 skb_queue_purge(&sk->sk_write_queue);
426
Tom Parkinf6e16b22013-03-19 06:11:23 +0000427 if (session) {
James Chapmanfd558d12010-04-02 06:18:33 +0000428 sk->sk_user_data = NULL;
429 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
430 l2tp_session_dec_refcount(session);
431 }
James Chapmanfd558d12010-04-02 06:18:33 +0000432}
433
434/* Called when the PPPoX socket (session) is closed.
435 */
436static int pppol2tp_release(struct socket *sock)
437{
438 struct sock *sk = sock->sk;
439 struct l2tp_session *session;
440 int error;
441
442 if (!sk)
443 return 0;
444
445 error = -EBADF;
446 lock_sock(sk);
447 if (sock_flag(sk, SOCK_DEAD) != 0)
448 goto error;
449
450 pppox_unbind_sock(sk);
451
452 /* Signal the death of the socket. */
453 sk->sk_state = PPPOX_DEAD;
454 sock_orphan(sk);
455 sock->sk = NULL;
456
457 session = pppol2tp_sock_to_session(sk);
James Chapmand02ba2a2018-02-23 17:45:46 +0000458 if (session) {
Guillaume Nault3d609342018-06-04 18:52:19 +0200459 struct pppol2tp_session *ps;
460
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200461 l2tp_session_delete(session);
Guillaume Nault3d609342018-06-04 18:52:19 +0200462
463 ps = l2tp_session_priv(session);
464 mutex_lock(&ps->sk_lock);
465 ps->__sk = rcu_dereference_protected(ps->sk,
466 lockdep_is_held(&ps->sk_lock));
467 RCU_INIT_POINTER(ps->sk, NULL);
468 mutex_unlock(&ps->sk_lock);
469 call_rcu(&ps->rcu, pppol2tp_put_sk);
470
471 /* Rely on the sock_put() call at the end of the function for
472 * dropping the reference held by pppol2tp_sock_to_session().
473 * The last reference will be dropped by pppol2tp_put_sk().
474 */
James Chapmanfd558d12010-04-02 06:18:33 +0000475 }
James Chapmand02ba2a2018-02-23 17:45:46 +0000476
James Chapmanfd558d12010-04-02 06:18:33 +0000477 release_sock(sk);
478
479 /* This will delete the session context via
480 * pppol2tp_session_destruct() if the socket's refcnt drops to
481 * zero.
482 */
483 sock_put(sk);
484
485 return 0;
486
487error:
488 release_sock(sk);
489 return error;
490}
491
492static struct proto pppol2tp_sk_proto = {
493 .name = "PPPOL2TP",
494 .owner = THIS_MODULE,
495 .obj_size = sizeof(struct pppox_sock),
496};
497
498static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
499{
500 int rc;
501
502 rc = l2tp_udp_encap_recv(sk, skb);
503 if (rc)
504 kfree_skb(skb);
505
506 return NET_RX_SUCCESS;
507}
508
509/* socket() handler. Initialize a new struct sock.
510 */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500511static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
James Chapmanfd558d12010-04-02 06:18:33 +0000512{
513 int error = -ENOMEM;
514 struct sock *sk;
515
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500516 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
James Chapmanfd558d12010-04-02 06:18:33 +0000517 if (!sk)
518 goto out;
519
520 sock_init_data(sock, sk);
521
522 sock->state = SS_UNCONNECTED;
523 sock->ops = &pppol2tp_ops;
524
525 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
526 sk->sk_protocol = PX_PROTO_OL2TP;
527 sk->sk_family = PF_PPPOX;
528 sk->sk_state = PPPOX_NONE;
529 sk->sk_type = SOCK_STREAM;
530 sk->sk_destruct = pppol2tp_session_destruct;
531
532 error = 0;
533
534out:
535 return error;
536}
537
Javier Martinez Canillas9dd79942016-09-09 08:43:17 -0400538#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
James Chapman0ad66142010-04-02 06:19:33 +0000539static void pppol2tp_show(struct seq_file *m, void *arg)
540{
541 struct l2tp_session *session = arg;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200542 struct sock *sk;
James Chapman0ad66142010-04-02 06:19:33 +0000543
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200544 sk = pppol2tp_session_get_sock(session);
545 if (sk) {
546 struct pppox_sock *po = pppox_sk(sk);
547
548 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
549 sock_put(sk);
James Chapman0ad66142010-04-02 06:19:33 +0000550 }
551}
552#endif
553
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200554static void pppol2tp_session_init(struct l2tp_session *session)
555{
556 struct pppol2tp_session *ps;
557 struct dst_entry *dst;
558
559 session->recv_skb = pppol2tp_recv;
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200560#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
561 session->show = pppol2tp_show;
562#endif
563
564 ps = l2tp_session_priv(session);
565 mutex_init(&ps->sk_lock);
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200566 ps->owner = current->pid;
567
568 /* If PMTU discovery was enabled, use the MTU that was discovered */
569 dst = sk_dst_get(session->tunnel->sock);
570 if (dst) {
571 u32 pmtu = dst_mtu(dst);
572
Guillaume Nault92ea4a72018-07-27 11:00:00 +0200573 if (pmtu)
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200574 session->mtu = pmtu - PPPOL2TP_HEADER_OVERHEAD;
Guillaume Nault92ea4a72018-07-27 11:00:00 +0200575
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200576 dst_release(dst);
577 }
578}
579
Guillaume Naulta4081942018-06-26 18:41:36 +0200580struct l2tp_connect_info {
581 u8 version;
582 int fd;
583 u32 tunnel_id;
584 u32 peer_tunnel_id;
585 u32 session_id;
586 u32 peer_session_id;
587};
588
589static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
590 struct l2tp_connect_info *info)
591{
592 switch (sa_len) {
593 case sizeof(struct sockaddr_pppol2tp):
594 {
595 const struct sockaddr_pppol2tp *sa_v2in4 = sa;
596
597 if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
598 return -EINVAL;
599
600 info->version = 2;
601 info->fd = sa_v2in4->pppol2tp.fd;
602 info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
603 info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
604 info->session_id = sa_v2in4->pppol2tp.s_session;
605 info->peer_session_id = sa_v2in4->pppol2tp.d_session;
606
607 break;
608 }
609 case sizeof(struct sockaddr_pppol2tpv3):
610 {
611 const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
612
613 if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
614 return -EINVAL;
615
616 info->version = 3;
617 info->fd = sa_v3in4->pppol2tp.fd;
618 info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
619 info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
620 info->session_id = sa_v3in4->pppol2tp.s_session;
621 info->peer_session_id = sa_v3in4->pppol2tp.d_session;
622
623 break;
624 }
625 case sizeof(struct sockaddr_pppol2tpin6):
626 {
627 const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
628
629 if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
630 return -EINVAL;
631
632 info->version = 2;
633 info->fd = sa_v2in6->pppol2tp.fd;
634 info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
635 info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
636 info->session_id = sa_v2in6->pppol2tp.s_session;
637 info->peer_session_id = sa_v2in6->pppol2tp.d_session;
638
639 break;
640 }
641 case sizeof(struct sockaddr_pppol2tpv3in6):
642 {
643 const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
644
645 if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
646 return -EINVAL;
647
648 info->version = 3;
649 info->fd = sa_v3in6->pppol2tp.fd;
650 info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
651 info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
652 info->session_id = sa_v3in6->pppol2tp.s_session;
653 info->peer_session_id = sa_v3in6->pppol2tp.d_session;
654
655 break;
656 }
657 default:
658 return -EINVAL;
659 }
660
661 return 0;
662}
663
James Chapmanfd558d12010-04-02 06:18:33 +0000664/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
665 */
666static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
667 int sockaddr_len, int flags)
668{
669 struct sock *sk = sock->sk;
James Chapmanfd558d12010-04-02 06:18:33 +0000670 struct pppox_sock *po = pppox_sk(sk);
671 struct l2tp_session *session = NULL;
Guillaume Naulta4081942018-06-26 18:41:36 +0200672 struct l2tp_connect_info info;
James Chapmanfd558d12010-04-02 06:18:33 +0000673 struct l2tp_tunnel *tunnel;
674 struct pppol2tp_session *ps;
James Chapmanfd558d12010-04-02 06:18:33 +0000675 struct l2tp_session_cfg cfg = { 0, };
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200676 bool drop_refcnt = false;
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100677 bool drop_tunnel = false;
Guillaume Naultbda06be2018-06-13 15:09:21 +0200678 bool new_session = false;
679 bool new_tunnel = false;
Guillaume Naulta4081942018-06-26 18:41:36 +0200680 int error;
681
682 error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
683 if (error < 0)
684 return error;
James Chapmanfd558d12010-04-02 06:18:33 +0000685
686 lock_sock(sk);
687
James Chapmanfd558d12010-04-02 06:18:33 +0000688 /* Check for already bound sockets */
689 error = -EBUSY;
690 if (sk->sk_state & PPPOX_CONNECTED)
691 goto end;
692
693 /* We don't supporting rebinding anyway */
694 error = -EALREADY;
695 if (sk->sk_user_data)
696 goto end; /* socket is already attached */
697
James Chapmane0d44352010-04-02 06:18:54 +0000698 /* Don't bind if tunnel_id is 0 */
James Chapmanfd558d12010-04-02 06:18:33 +0000699 error = -EINVAL;
Guillaume Naulta4081942018-06-26 18:41:36 +0200700 if (!info.tunnel_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000701 goto end;
702
Guillaume Naulta4081942018-06-26 18:41:36 +0200703 tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100704 if (tunnel)
705 drop_tunnel = true;
James Chapman309795f2010-04-02 06:19:10 +0000706
James Chapmane0d44352010-04-02 06:18:54 +0000707 /* Special case: create tunnel context if session_id and
708 * peer_session_id is 0. Otherwise look up tunnel using supplied
James Chapmanfd558d12010-04-02 06:18:33 +0000709 * tunnel id.
710 */
Guillaume Naulta4081942018-06-26 18:41:36 +0200711 if (!info.session_id && !info.peer_session_id) {
James Chapman309795f2010-04-02 06:19:10 +0000712 if (tunnel == NULL) {
713 struct l2tp_tunnel_cfg tcfg = {
714 .encap = L2TP_ENCAPTYPE_UDP,
715 .debug = 0,
716 };
Guillaume Nault3e1bc8b2018-06-13 15:09:20 +0200717
718 /* Prevent l2tp_tunnel_register() from trying to set up
719 * a kernel socket.
720 */
Guillaume Naulta4081942018-06-26 18:41:36 +0200721 if (info.fd < 0) {
Guillaume Nault3e1bc8b2018-06-13 15:09:20 +0200722 error = -EBADF;
723 goto end;
724 }
725
Guillaume Naulta4081942018-06-26 18:41:36 +0200726 error = l2tp_tunnel_create(sock_net(sk), info.fd,
727 info.version,
728 info.tunnel_id,
729 info.peer_tunnel_id, &tcfg,
730 &tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000731 if (error < 0)
732 goto end;
Guillaume Nault6b9f3422018-04-10 21:01:12 +0200733
734 l2tp_tunnel_inc_refcount(tunnel);
735 error = l2tp_tunnel_register(tunnel, sock_net(sk),
736 &tcfg);
737 if (error < 0) {
738 kfree(tunnel);
739 goto end;
740 }
741 drop_tunnel = true;
Guillaume Naultbda06be2018-06-13 15:09:21 +0200742 new_tunnel = true;
James Chapman309795f2010-04-02 06:19:10 +0000743 }
James Chapmanfd558d12010-04-02 06:18:33 +0000744 } else {
James Chapmanfd558d12010-04-02 06:18:33 +0000745 /* Error if we can't find the tunnel */
746 error = -ENOENT;
747 if (tunnel == NULL)
748 goto end;
749
750 /* Error if socket is not prepped */
751 if (tunnel->sock == NULL)
752 goto end;
753 }
754
James Chapmanb79585f2012-04-29 21:48:50 +0000755 if (tunnel->peer_tunnel_id == 0)
Guillaume Naulta4081942018-06-26 18:41:36 +0200756 tunnel->peer_tunnel_id = info.peer_tunnel_id;
James Chapmanfd558d12010-04-02 06:18:33 +0000757
Guillaume Naulta4081942018-06-26 18:41:36 +0200758 session = l2tp_session_get(sock_net(sk), tunnel, info.session_id);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200759 if (session) {
760 drop_refcnt = true;
Guillaume Nault7ac6ab12018-06-13 15:09:19 +0200761
762 if (session->pwtype != L2TP_PWTYPE_PPP) {
763 error = -EPROTOTYPE;
764 goto end;
765 }
766
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200767 ps = l2tp_session_priv(session);
James Chapman309795f2010-04-02 06:19:10 +0000768
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200769 /* Using a pre-existing session is fine as long as it hasn't
770 * been connected yet.
771 */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200772 mutex_lock(&ps->sk_lock);
773 if (rcu_dereference_protected(ps->sk,
Guillaume Nault3d609342018-06-04 18:52:19 +0200774 lockdep_is_held(&ps->sk_lock)) ||
775 ps->__sk) {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200776 mutex_unlock(&ps->sk_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200777 error = -EEXIST;
778 goto end;
779 }
James Chapman309795f2010-04-02 06:19:10 +0000780 } else {
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200781 /* Default MTU must allow space for UDP/L2TP/PPP headers */
782 cfg.mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
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;
827 po->chan.mtu = session->mtu;
828
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 /* Default MTU values. */
884 if (cfg->mtu == 0)
885 cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
James Chapman309795f2010-04-02 06:19:10 +0000886
887 /* Allocate and initialize a new session context. */
James Chapman309795f2010-04-02 06:19:10 +0000888 session = l2tp_session_create(sizeof(struct pppol2tp_session),
889 tunnel, session_id,
890 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200891 if (IS_ERR(session)) {
892 error = PTR_ERR(session);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200893 goto err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200894 }
James Chapman309795f2010-04-02 06:19:10 +0000895
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200896 pppol2tp_session_init(session);
James Chapman309795f2010-04-02 06:19:10 +0000897
Guillaume Nault3953ae72017-10-27 16:51:50 +0200898 error = l2tp_session_register(session, tunnel);
899 if (error < 0)
900 goto err_sess;
James Chapman309795f2010-04-02 06:19:10 +0000901
Guillaume Nault3953ae72017-10-27 16:51:50 +0200902 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000903
Guillaume Nault3953ae72017-10-27 16:51:50 +0200904err_sess:
905 kfree(session);
906err:
James Chapman309795f2010-04-02 06:19:10 +0000907 return error;
908}
909
James Chapman309795f2010-04-02 06:19:10 +0000910#endif /* CONFIG_L2TP_V3 */
911
James Chapmanfd558d12010-04-02 06:18:33 +0000912/* getname() support.
913 */
914static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100915 int peer)
James Chapmanfd558d12010-04-02 06:18:33 +0000916{
James Chapmane0d44352010-04-02 06:18:54 +0000917 int len = 0;
James Chapmanfd558d12010-04-02 06:18:33 +0000918 int error = 0;
919 struct l2tp_session *session;
920 struct l2tp_tunnel *tunnel;
921 struct sock *sk = sock->sk;
922 struct inet_sock *inet;
923 struct pppol2tp_session *pls;
924
925 error = -ENOTCONN;
926 if (sk == NULL)
927 goto end;
Gao Feng56cff472016-08-19 13:36:23 +0800928 if (!(sk->sk_state & PPPOX_CONNECTED))
James Chapmanfd558d12010-04-02 06:18:33 +0000929 goto end;
930
931 error = -EBADF;
932 session = pppol2tp_sock_to_session(sk);
933 if (session == NULL)
934 goto end;
935
936 pls = l2tp_session_priv(session);
Guillaume Nault7198c772017-11-11 06:06:31 +0900937 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000938
Benjamin LaHaisebbdb32c2012-03-20 03:57:54 +0000939 inet = inet_sk(tunnel->sock);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000940 if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
James Chapmane0d44352010-04-02 06:18:54 +0000941 struct sockaddr_pppol2tp sp;
942 len = sizeof(sp);
943 memset(&sp, 0, len);
944 sp.sa_family = AF_PPPOX;
945 sp.sa_protocol = PX_PROTO_OL2TP;
946 sp.pppol2tp.fd = tunnel->fd;
947 sp.pppol2tp.pid = pls->owner;
948 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
949 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
950 sp.pppol2tp.s_session = session->session_id;
951 sp.pppol2tp.d_session = session->peer_session_id;
952 sp.pppol2tp.addr.sin_family = AF_INET;
953 sp.pppol2tp.addr.sin_port = inet->inet_dport;
954 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
955 memcpy(uaddr, &sp, len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000956#if IS_ENABLED(CONFIG_IPV6)
957 } else if ((tunnel->version == 2) &&
958 (tunnel->sock->sk_family == AF_INET6)) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000959 struct sockaddr_pppol2tpin6 sp;
Eric Dumazetefe42082013-10-03 15:42:29 -0700960
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000961 len = sizeof(sp);
962 memset(&sp, 0, len);
963 sp.sa_family = AF_PPPOX;
964 sp.sa_protocol = PX_PROTO_OL2TP;
965 sp.pppol2tp.fd = tunnel->fd;
966 sp.pppol2tp.pid = pls->owner;
967 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
968 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
969 sp.pppol2tp.s_session = session->session_id;
970 sp.pppol2tp.d_session = session->peer_session_id;
971 sp.pppol2tp.addr.sin6_family = AF_INET6;
972 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700973 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
974 sizeof(tunnel->sock->sk_v6_daddr));
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000975 memcpy(uaddr, &sp, len);
976 } else if ((tunnel->version == 3) &&
977 (tunnel->sock->sk_family == AF_INET6)) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000978 struct sockaddr_pppol2tpv3in6 sp;
Eric Dumazetefe42082013-10-03 15:42:29 -0700979
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000980 len = sizeof(sp);
981 memset(&sp, 0, len);
982 sp.sa_family = AF_PPPOX;
983 sp.sa_protocol = PX_PROTO_OL2TP;
984 sp.pppol2tp.fd = tunnel->fd;
985 sp.pppol2tp.pid = pls->owner;
986 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
987 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
988 sp.pppol2tp.s_session = session->session_id;
989 sp.pppol2tp.d_session = session->peer_session_id;
990 sp.pppol2tp.addr.sin6_family = AF_INET6;
991 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700992 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
993 sizeof(tunnel->sock->sk_v6_daddr));
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000994 memcpy(uaddr, &sp, len);
995#endif
James Chapmane0d44352010-04-02 06:18:54 +0000996 } else if (tunnel->version == 3) {
997 struct sockaddr_pppol2tpv3 sp;
998 len = sizeof(sp);
999 memset(&sp, 0, len);
1000 sp.sa_family = AF_PPPOX;
1001 sp.sa_protocol = PX_PROTO_OL2TP;
1002 sp.pppol2tp.fd = tunnel->fd;
1003 sp.pppol2tp.pid = pls->owner;
1004 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
1005 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
1006 sp.pppol2tp.s_session = session->session_id;
1007 sp.pppol2tp.d_session = session->peer_session_id;
1008 sp.pppol2tp.addr.sin_family = AF_INET;
1009 sp.pppol2tp.addr.sin_port = inet->inet_dport;
1010 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
1011 memcpy(uaddr, &sp, len);
1012 }
James Chapmanfd558d12010-04-02 06:18:33 +00001013
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001014 error = len;
James Chapmanfd558d12010-04-02 06:18:33 +00001015
James Chapmanfd558d12010-04-02 06:18:33 +00001016 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001017end:
1018 return error;
1019}
1020
1021/****************************************************************************
1022 * ioctl() handlers.
1023 *
1024 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1025 * sockets. However, in order to control kernel tunnel features, we allow
1026 * userspace to create a special "tunnel" PPPoX socket which is used for
1027 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1028 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1029 * calls.
1030 ****************************************************************************/
1031
1032static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1033 struct l2tp_stats *stats)
1034{
Tom Parkin7b7c0712013-03-19 06:11:22 +00001035 dest->tx_packets = atomic_long_read(&stats->tx_packets);
1036 dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1037 dest->tx_errors = atomic_long_read(&stats->tx_errors);
1038 dest->rx_packets = atomic_long_read(&stats->rx_packets);
1039 dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1040 dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1041 dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1042 dest->rx_errors = atomic_long_read(&stats->rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001043}
1044
1045/* Session ioctl helper.
1046 */
1047static int pppol2tp_session_ioctl(struct l2tp_session *session,
1048 unsigned int cmd, unsigned long arg)
1049{
1050 struct ifreq ifr;
1051 int err = 0;
1052 struct sock *sk;
1053 int val = (int) arg;
James Chapmanfd558d12010-04-02 06:18:33 +00001054 struct l2tp_tunnel *tunnel = session->tunnel;
1055 struct pppol2tp_ioc_stats stats;
1056
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001057 l2tp_dbg(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001058 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1059 session->name, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001060
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001061 sk = pppol2tp_session_get_sock(session);
Guillaume Nault5903f592017-10-13 19:22:35 +02001062 if (!sk)
1063 return -EBADR;
1064
James Chapmanfd558d12010-04-02 06:18:33 +00001065 switch (cmd) {
1066 case SIOCGIFMTU:
1067 err = -ENXIO;
1068 if (!(sk->sk_state & PPPOX_CONNECTED))
1069 break;
1070
1071 err = -EFAULT;
1072 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1073 break;
1074 ifr.ifr_mtu = session->mtu;
1075 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1076 break;
1077
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001078 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mtu=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001079 session->name, session->mtu);
James Chapmanfd558d12010-04-02 06:18:33 +00001080 err = 0;
1081 break;
1082
1083 case SIOCSIFMTU:
1084 err = -ENXIO;
1085 if (!(sk->sk_state & PPPOX_CONNECTED))
1086 break;
1087
1088 err = -EFAULT;
1089 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1090 break;
1091
1092 session->mtu = ifr.ifr_mtu;
1093
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001094 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mtu=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001095 session->name, session->mtu);
James Chapmanfd558d12010-04-02 06:18:33 +00001096 err = 0;
1097 break;
1098
1099 case PPPIOCGMRU:
James Chapmanfd558d12010-04-02 06:18:33 +00001100 case PPPIOCGFLAGS:
1101 err = -EFAULT;
Guillaume Nault1998b5e2018-07-27 10:59:59 +02001102 if (put_user(0, (int __user *)arg))
James Chapmanfd558d12010-04-02 06:18:33 +00001103 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001104 err = 0;
1105 break;
1106
Guillaume Nault92ea4a72018-07-27 11:00:00 +02001107 case PPPIOCSMRU:
James Chapmanfd558d12010-04-02 06:18:33 +00001108 case PPPIOCSFLAGS:
1109 err = -EFAULT;
Guillaume Nault1998b5e2018-07-27 10:59:59 +02001110 if (get_user(val, (int __user *)arg))
James Chapmanfd558d12010-04-02 06:18:33 +00001111 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001112 err = 0;
1113 break;
1114
1115 case PPPIOCGL2TPSTATS:
1116 err = -ENXIO;
1117 if (!(sk->sk_state & PPPOX_CONNECTED))
1118 break;
1119
1120 memset(&stats, 0, sizeof(stats));
1121 stats.tunnel_id = tunnel->tunnel_id;
1122 stats.session_id = session->session_id;
1123 pppol2tp_copy_stats(&stats, &session->stats);
1124 if (copy_to_user((void __user *) arg, &stats,
1125 sizeof(stats)))
1126 break;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001127 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001128 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001129 err = 0;
1130 break;
1131
1132 default:
1133 err = -ENOSYS;
1134 break;
1135 }
1136
1137 sock_put(sk);
1138
1139 return err;
1140}
1141
1142/* Tunnel ioctl helper.
1143 *
1144 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1145 * specifies a session_id, the session ioctl handler is called. This allows an
1146 * application to retrieve session stats via a tunnel socket.
1147 */
1148static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1149 unsigned int cmd, unsigned long arg)
1150{
1151 int err = 0;
1152 struct sock *sk;
1153 struct pppol2tp_ioc_stats stats;
1154
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001155 l2tp_dbg(tunnel, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001156 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1157 tunnel->name, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001158
1159 sk = tunnel->sock;
1160 sock_hold(sk);
1161
1162 switch (cmd) {
1163 case PPPIOCGL2TPSTATS:
1164 err = -ENXIO;
1165 if (!(sk->sk_state & PPPOX_CONNECTED))
1166 break;
1167
1168 if (copy_from_user(&stats, (void __user *) arg,
1169 sizeof(stats))) {
1170 err = -EFAULT;
1171 break;
1172 }
1173 if (stats.session_id != 0) {
1174 /* resend to session ioctl handler */
1175 struct l2tp_session *session =
Guillaume Nault57377d62017-03-31 13:02:26 +02001176 l2tp_session_get(sock_net(sk), tunnel,
Guillaume Naulta4346212017-10-31 17:36:42 +01001177 stats.session_id);
Guillaume Nault57377d62017-03-31 13:02:26 +02001178
Guillaume Naultecd012e2018-06-15 15:39:19 +02001179 if (session && session->pwtype == L2TP_PWTYPE_PPP) {
Guillaume Nault57377d62017-03-31 13:02:26 +02001180 err = pppol2tp_session_ioctl(session, cmd,
1181 arg);
Guillaume Nault57377d62017-03-31 13:02:26 +02001182 l2tp_session_dec_refcount(session);
1183 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001184 err = -EBADR;
Guillaume Nault57377d62017-03-31 13:02:26 +02001185 }
James Chapmanfd558d12010-04-02 06:18:33 +00001186 break;
1187 }
1188#ifdef CONFIG_XFRM
1189 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1190#endif
1191 pppol2tp_copy_stats(&stats, &tunnel->stats);
1192 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1193 err = -EFAULT;
1194 break;
1195 }
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001196 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001197 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001198 err = 0;
1199 break;
1200
1201 default:
1202 err = -ENOSYS;
1203 break;
1204 }
1205
1206 sock_put(sk);
1207
1208 return err;
1209}
1210
1211/* Main ioctl() handler.
1212 * Dispatch to tunnel or session helpers depending on the socket.
1213 */
1214static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1215 unsigned long arg)
1216{
1217 struct sock *sk = sock->sk;
1218 struct l2tp_session *session;
1219 struct l2tp_tunnel *tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001220 int err;
1221
1222 if (!sk)
1223 return 0;
1224
1225 err = -EBADF;
1226 if (sock_flag(sk, SOCK_DEAD) != 0)
1227 goto end;
1228
1229 err = -ENOTCONN;
1230 if ((sk->sk_user_data == NULL) ||
1231 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1232 goto end;
1233
1234 /* Get session context from the socket */
1235 err = -EBADF;
1236 session = pppol2tp_sock_to_session(sk);
1237 if (session == NULL)
1238 goto end;
1239
1240 /* Special case: if session's session_id is zero, treat ioctl as a
1241 * tunnel ioctl
1242 */
James Chapmanfd558d12010-04-02 06:18:33 +00001243 if ((session->session_id == 0) &&
1244 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001245 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001246 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001247 goto end_put_sess;
1248 }
1249
1250 err = pppol2tp_session_ioctl(session, cmd, arg);
1251
1252end_put_sess:
1253 sock_put(sk);
1254end:
1255 return err;
1256}
1257
1258/*****************************************************************************
1259 * setsockopt() / getsockopt() support.
1260 *
1261 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1262 * sockets. In order to control kernel tunnel features, we allow userspace to
1263 * create a special "tunnel" PPPoX socket which is used for control only.
1264 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1265 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1266 *****************************************************************************/
1267
1268/* Tunnel setsockopt() helper.
1269 */
1270static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1271 struct l2tp_tunnel *tunnel,
1272 int optname, int val)
1273{
1274 int err = 0;
1275
1276 switch (optname) {
1277 case PPPOL2TP_SO_DEBUG:
1278 tunnel->debug = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001279 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001280 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001281 break;
1282
1283 default:
1284 err = -ENOPROTOOPT;
1285 break;
1286 }
1287
1288 return err;
1289}
1290
1291/* Session setsockopt helper.
1292 */
1293static int pppol2tp_session_setsockopt(struct sock *sk,
1294 struct l2tp_session *session,
1295 int optname, int val)
1296{
1297 int err = 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001298
1299 switch (optname) {
1300 case PPPOL2TP_SO_RECVSEQ:
1301 if ((val != 0) && (val != 1)) {
1302 err = -EINVAL;
1303 break;
1304 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001305 session->recv_seq = !!val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001306 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001307 "%s: set recv_seq=%d\n",
1308 session->name, session->recv_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001309 break;
1310
1311 case PPPOL2TP_SO_SENDSEQ:
1312 if ((val != 0) && (val != 1)) {
1313 err = -EINVAL;
1314 break;
1315 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001316 session->send_seq = !!val;
James Chapmanfd558d12010-04-02 06:18:33 +00001317 {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001318 struct pppox_sock *po = pppox_sk(sk);
1319
James Chapmanfd558d12010-04-02 06:18:33 +00001320 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1321 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1322 }
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001323 l2tp_session_set_header_len(session, session->tunnel->version);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001324 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001325 "%s: set send_seq=%d\n",
1326 session->name, session->send_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001327 break;
1328
1329 case PPPOL2TP_SO_LNSMODE:
1330 if ((val != 0) && (val != 1)) {
1331 err = -EINVAL;
1332 break;
1333 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001334 session->lns_mode = !!val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001335 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001336 "%s: set lns_mode=%d\n",
1337 session->name, session->lns_mode);
James Chapmanfd558d12010-04-02 06:18:33 +00001338 break;
1339
1340 case PPPOL2TP_SO_DEBUG:
1341 session->debug = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001342 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001343 session->name, session->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001344 break;
1345
1346 case PPPOL2TP_SO_REORDERTO:
1347 session->reorder_timeout = msecs_to_jiffies(val);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001348 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001349 "%s: set reorder_timeout=%d\n",
1350 session->name, session->reorder_timeout);
James Chapmanfd558d12010-04-02 06:18:33 +00001351 break;
1352
1353 default:
1354 err = -ENOPROTOOPT;
1355 break;
1356 }
1357
1358 return err;
1359}
1360
1361/* Main setsockopt() entry point.
1362 * Does API checks, then calls either the tunnel or session setsockopt
1363 * handler, according to whether the PPPoL2TP socket is a for a regular
1364 * session or the special tunnel type.
1365 */
1366static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1367 char __user *optval, unsigned int optlen)
1368{
1369 struct sock *sk = sock->sk;
1370 struct l2tp_session *session;
1371 struct l2tp_tunnel *tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001372 int val;
1373 int err;
1374
1375 if (level != SOL_PPPOL2TP)
Sasha Levin3cf521f2014-07-14 17:02:31 -07001376 return -EINVAL;
James Chapmanfd558d12010-04-02 06:18:33 +00001377
1378 if (optlen < sizeof(int))
1379 return -EINVAL;
1380
1381 if (get_user(val, (int __user *)optval))
1382 return -EFAULT;
1383
1384 err = -ENOTCONN;
1385 if (sk->sk_user_data == NULL)
1386 goto end;
1387
1388 /* Get session context from the socket */
1389 err = -EBADF;
1390 session = pppol2tp_sock_to_session(sk);
1391 if (session == NULL)
1392 goto end;
1393
1394 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1395 */
James Chapmanfd558d12010-04-02 06:18:33 +00001396 if ((session->session_id == 0) &&
1397 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001398 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001399 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
Guillaume Nault7198c772017-11-11 06:06:31 +09001400 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001401 err = pppol2tp_session_setsockopt(sk, session, optname, val);
Guillaume Nault7198c772017-11-11 06:06:31 +09001402 }
James Chapmanfd558d12010-04-02 06:18:33 +00001403
James Chapmanfd558d12010-04-02 06:18:33 +00001404 sock_put(sk);
1405end:
1406 return err;
1407}
1408
1409/* Tunnel getsockopt helper. Called with sock locked.
1410 */
1411static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1412 struct l2tp_tunnel *tunnel,
1413 int optname, int *val)
1414{
1415 int err = 0;
1416
1417 switch (optname) {
1418 case PPPOL2TP_SO_DEBUG:
1419 *val = tunnel->debug;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001420 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001421 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001422 break;
1423
1424 default:
1425 err = -ENOPROTOOPT;
1426 break;
1427 }
1428
1429 return err;
1430}
1431
1432/* Session getsockopt helper. Called with sock locked.
1433 */
1434static int pppol2tp_session_getsockopt(struct sock *sk,
1435 struct l2tp_session *session,
1436 int optname, int *val)
1437{
1438 int err = 0;
1439
1440 switch (optname) {
1441 case PPPOL2TP_SO_RECVSEQ:
1442 *val = session->recv_seq;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001443 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001444 "%s: get recv_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001445 break;
1446
1447 case PPPOL2TP_SO_SENDSEQ:
1448 *val = session->send_seq;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001449 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001450 "%s: get send_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001451 break;
1452
1453 case PPPOL2TP_SO_LNSMODE:
1454 *val = session->lns_mode;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001455 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001456 "%s: get lns_mode=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001457 break;
1458
1459 case PPPOL2TP_SO_DEBUG:
1460 *val = session->debug;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001461 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001462 session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001463 break;
1464
1465 case PPPOL2TP_SO_REORDERTO:
1466 *val = (int) jiffies_to_msecs(session->reorder_timeout);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001467 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001468 "%s: get reorder_timeout=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001469 break;
1470
1471 default:
1472 err = -ENOPROTOOPT;
1473 }
1474
1475 return err;
1476}
1477
1478/* Main getsockopt() entry point.
1479 * Does API checks, then calls either the tunnel or session getsockopt
1480 * handler, according to whether the PPPoX socket is a for a regular session
1481 * or the special tunnel type.
1482 */
Joe Perchese3192692012-06-03 17:41:40 +00001483static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1484 char __user *optval, int __user *optlen)
James Chapmanfd558d12010-04-02 06:18:33 +00001485{
1486 struct sock *sk = sock->sk;
1487 struct l2tp_session *session;
1488 struct l2tp_tunnel *tunnel;
1489 int val, len;
1490 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001491
1492 if (level != SOL_PPPOL2TP)
Sasha Levin3cf521f2014-07-14 17:02:31 -07001493 return -EINVAL;
James Chapmanfd558d12010-04-02 06:18:33 +00001494
Joe Perchese3192692012-06-03 17:41:40 +00001495 if (get_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001496 return -EFAULT;
1497
1498 len = min_t(unsigned int, len, sizeof(int));
1499
1500 if (len < 0)
1501 return -EINVAL;
1502
1503 err = -ENOTCONN;
1504 if (sk->sk_user_data == NULL)
1505 goto end;
1506
1507 /* Get the session context */
1508 err = -EBADF;
1509 session = pppol2tp_sock_to_session(sk);
1510 if (session == NULL)
1511 goto end;
1512
1513 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
James Chapmanfd558d12010-04-02 06:18:33 +00001514 if ((session->session_id == 0) &&
1515 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001516 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001517 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
Guillaume Nault321a52a2017-04-06 18:31:21 +02001518 if (err)
1519 goto end_put_sess;
1520 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001521 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
Guillaume Nault321a52a2017-04-06 18:31:21 +02001522 if (err)
1523 goto end_put_sess;
1524 }
James Chapmanfd558d12010-04-02 06:18:33 +00001525
1526 err = -EFAULT;
Joe Perchese3192692012-06-03 17:41:40 +00001527 if (put_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001528 goto end_put_sess;
1529
1530 if (copy_to_user((void __user *) optval, &val, len))
1531 goto end_put_sess;
1532
1533 err = 0;
1534
1535end_put_sess:
1536 sock_put(sk);
1537end:
1538 return err;
1539}
1540
1541/*****************************************************************************
1542 * /proc filesystem for debug
James Chapmanf7faffa2010-04-02 06:18:49 +00001543 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1544 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
James Chapmanfd558d12010-04-02 06:18:33 +00001545 *****************************************************************************/
1546
1547static unsigned int pppol2tp_net_id;
1548
1549#ifdef CONFIG_PROC_FS
1550
1551struct pppol2tp_seq_data {
1552 struct seq_net_private p;
1553 int tunnel_idx; /* current tunnel */
1554 int session_idx; /* index of session within current tunnel */
1555 struct l2tp_tunnel *tunnel;
1556 struct l2tp_session *session; /* NULL means get next tunnel */
1557};
1558
1559static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1560{
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001561 /* Drop reference taken during previous invocation */
1562 if (pd->tunnel)
1563 l2tp_tunnel_dec_refcount(pd->tunnel);
1564
James Chapmanf7faffa2010-04-02 06:18:49 +00001565 for (;;) {
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001566 pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
James Chapmanf7faffa2010-04-02 06:18:49 +00001567 pd->tunnel_idx++;
1568
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001569 /* Only accept L2TPv2 tunnels */
1570 if (!pd->tunnel || pd->tunnel->version == 2)
1571 return;
James Chapmanf7faffa2010-04-02 06:18:49 +00001572
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001573 l2tp_tunnel_dec_refcount(pd->tunnel);
James Chapmanf7faffa2010-04-02 06:18:49 +00001574 }
James Chapmanfd558d12010-04-02 06:18:33 +00001575}
1576
1577static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1578{
Guillaume Nault83494402018-04-25 19:54:14 +02001579 /* Drop reference taken during previous invocation */
1580 if (pd->session)
1581 l2tp_session_dec_refcount(pd->session);
1582
Guillaume Naulta4346212017-10-31 17:36:42 +01001583 pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
James Chapmanfd558d12010-04-02 06:18:33 +00001584 pd->session_idx++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001585
James Chapmanfd558d12010-04-02 06:18:33 +00001586 if (pd->session == NULL) {
1587 pd->session_idx = 0;
1588 pppol2tp_next_tunnel(net, pd);
1589 }
1590}
1591
1592static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1593{
1594 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1595 loff_t pos = *offs;
1596 struct net *net;
1597
1598 if (!pos)
1599 goto out;
1600
1601 BUG_ON(m->private == NULL);
1602 pd = m->private;
1603 net = seq_file_net(m);
1604
1605 if (pd->tunnel == NULL)
1606 pppol2tp_next_tunnel(net, pd);
1607 else
1608 pppol2tp_next_session(net, pd);
1609
1610 /* NULL tunnel and session indicates end of list */
1611 if ((pd->tunnel == NULL) && (pd->session == NULL))
1612 pd = NULL;
1613
1614out:
1615 return pd;
1616}
1617
1618static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1619{
1620 (*pos)++;
1621 return NULL;
1622}
1623
1624static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1625{
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001626 struct pppol2tp_seq_data *pd = v;
1627
1628 if (!pd || pd == SEQ_START_TOKEN)
1629 return;
1630
Guillaume Nault83494402018-04-25 19:54:14 +02001631 /* Drop reference taken by last invocation of pppol2tp_next_session()
1632 * or pppol2tp_next_tunnel().
1633 */
1634 if (pd->session) {
1635 l2tp_session_dec_refcount(pd->session);
1636 pd->session = NULL;
1637 }
Guillaume Nault5411b6182018-04-19 16:20:48 +02001638 if (pd->tunnel) {
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001639 l2tp_tunnel_dec_refcount(pd->tunnel);
Guillaume Nault5411b6182018-04-19 16:20:48 +02001640 pd->tunnel = NULL;
Guillaume Nault5411b6182018-04-19 16:20:48 +02001641 }
James Chapmanfd558d12010-04-02 06:18:33 +00001642}
1643
1644static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1645{
1646 struct l2tp_tunnel *tunnel = v;
1647
1648 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1649 tunnel->name,
1650 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001651 refcount_read(&tunnel->ref_count) - 1);
Tom Parkin7b7c0712013-03-19 06:11:22 +00001652 seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001653 tunnel->debug,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001654 atomic_long_read(&tunnel->stats.tx_packets),
1655 atomic_long_read(&tunnel->stats.tx_bytes),
1656 atomic_long_read(&tunnel->stats.tx_errors),
1657 atomic_long_read(&tunnel->stats.rx_packets),
1658 atomic_long_read(&tunnel->stats.rx_bytes),
1659 atomic_long_read(&tunnel->stats.rx_errors));
James Chapmanfd558d12010-04-02 06:18:33 +00001660}
1661
1662static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1663{
1664 struct l2tp_session *session = v;
1665 struct l2tp_tunnel *tunnel = session->tunnel;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001666 unsigned char state;
1667 char user_data_ok;
1668 struct sock *sk;
James Chapmanfd558d12010-04-02 06:18:33 +00001669 u32 ip = 0;
1670 u16 port = 0;
1671
1672 if (tunnel->sock) {
1673 struct inet_sock *inet = inet_sk(tunnel->sock);
1674 ip = ntohl(inet->inet_saddr);
1675 port = ntohs(inet->inet_sport);
1676 }
1677
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001678 sk = pppol2tp_session_get_sock(session);
1679 if (sk) {
1680 state = sk->sk_state;
1681 user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1682 } else {
1683 state = 0;
1684 user_data_ok = 'N';
1685 }
1686
James Chapmanfd558d12010-04-02 06:18:33 +00001687 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1688 "%04X/%04X %d %c\n",
1689 session->name, ip, port,
1690 tunnel->tunnel_id,
1691 session->session_id,
1692 tunnel->peer_tunnel_id,
1693 session->peer_session_id,
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001694 state, user_data_ok);
Guillaume Nault92ea4a72018-07-27 11:00:00 +02001695 seq_printf(m, " %d/0/%c/%c/%s %08x %u\n",
1696 session->mtu,
James Chapmanfd558d12010-04-02 06:18:33 +00001697 session->recv_seq ? 'R' : '-',
1698 session->send_seq ? 'S' : '-',
1699 session->lns_mode ? "LNS" : "LAC",
1700 session->debug,
1701 jiffies_to_msecs(session->reorder_timeout));
Tom Parkin7b7c0712013-03-19 06:11:22 +00001702 seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001703 session->nr, session->ns,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001704 atomic_long_read(&session->stats.tx_packets),
1705 atomic_long_read(&session->stats.tx_bytes),
1706 atomic_long_read(&session->stats.tx_errors),
1707 atomic_long_read(&session->stats.rx_packets),
1708 atomic_long_read(&session->stats.rx_bytes),
1709 atomic_long_read(&session->stats.rx_errors));
James Chapman93454712010-04-02 06:18:44 +00001710
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001711 if (sk) {
1712 struct pppox_sock *po = pppox_sk(sk);
1713
James Chapman93454712010-04-02 06:18:44 +00001714 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001715 sock_put(sk);
1716 }
James Chapmanfd558d12010-04-02 06:18:33 +00001717}
1718
1719static int pppol2tp_seq_show(struct seq_file *m, void *v)
1720{
1721 struct pppol2tp_seq_data *pd = v;
1722
1723 /* display header on line 1 */
1724 if (v == SEQ_START_TOKEN) {
1725 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1726 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1727 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1728 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1729 "dest-tid/sid state user-data-ok\n");
1730 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1731 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1732 goto out;
1733 }
1734
Guillaume Nault83494402018-04-25 19:54:14 +02001735 if (!pd->session)
James Chapmanfd558d12010-04-02 06:18:33 +00001736 pppol2tp_seq_tunnel_show(m, pd->tunnel);
Guillaume Nault83494402018-04-25 19:54:14 +02001737 else
James Chapmanfd558d12010-04-02 06:18:33 +00001738 pppol2tp_seq_session_show(m, pd->session);
1739
1740out:
1741 return 0;
1742}
1743
1744static const struct seq_operations pppol2tp_seq_ops = {
1745 .start = pppol2tp_seq_start,
1746 .next = pppol2tp_seq_next,
1747 .stop = pppol2tp_seq_stop,
1748 .show = pppol2tp_seq_show,
1749};
James Chapmanfd558d12010-04-02 06:18:33 +00001750#endif /* CONFIG_PROC_FS */
1751
1752/*****************************************************************************
1753 * Network namespace
1754 *****************************************************************************/
1755
1756static __net_init int pppol2tp_init_net(struct net *net)
1757{
1758 struct proc_dir_entry *pde;
1759 int err = 0;
1760
Christoph Hellwigc3506372018-04-10 19:42:55 +02001761 pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1762 &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
James Chapmanfd558d12010-04-02 06:18:33 +00001763 if (!pde) {
1764 err = -ENOMEM;
1765 goto out;
1766 }
1767
1768out:
1769 return err;
1770}
1771
1772static __net_exit void pppol2tp_exit_net(struct net *net)
1773{
Gao fengece31ff2013-02-18 01:34:56 +00001774 remove_proc_entry("pppol2tp", net->proc_net);
James Chapmanfd558d12010-04-02 06:18:33 +00001775}
1776
1777static struct pernet_operations pppol2tp_net_ops = {
1778 .init = pppol2tp_init_net,
1779 .exit = pppol2tp_exit_net,
1780 .id = &pppol2tp_net_id,
1781};
1782
1783/*****************************************************************************
1784 * Init and cleanup
1785 *****************************************************************************/
1786
1787static const struct proto_ops pppol2tp_ops = {
1788 .family = AF_PPPOX,
1789 .owner = THIS_MODULE,
1790 .release = pppol2tp_release,
1791 .bind = sock_no_bind,
1792 .connect = pppol2tp_connect,
1793 .socketpair = sock_no_socketpair,
1794 .accept = sock_no_accept,
1795 .getname = pppol2tp_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001796 .poll = datagram_poll,
James Chapmanfd558d12010-04-02 06:18:33 +00001797 .listen = sock_no_listen,
1798 .shutdown = sock_no_shutdown,
1799 .setsockopt = pppol2tp_setsockopt,
1800 .getsockopt = pppol2tp_getsockopt,
1801 .sendmsg = pppol2tp_sendmsg,
1802 .recvmsg = pppol2tp_recvmsg,
1803 .mmap = sock_no_mmap,
1804 .ioctl = pppox_ioctl,
1805};
1806
Eric Dumazet756e64a2010-09-21 06:43:54 +00001807static const struct pppox_proto pppol2tp_proto = {
James Chapmanfd558d12010-04-02 06:18:33 +00001808 .create = pppol2tp_create,
Wei Yongjune1558a92013-07-02 09:02:07 +08001809 .ioctl = pppol2tp_ioctl,
1810 .owner = THIS_MODULE,
James Chapmanfd558d12010-04-02 06:18:33 +00001811};
1812
James Chapman309795f2010-04-02 06:19:10 +00001813#ifdef CONFIG_L2TP_V3
1814
1815static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1816 .session_create = pppol2tp_session_create,
Tom Parkincf2f5c82013-03-19 06:11:21 +00001817 .session_delete = l2tp_session_delete,
James Chapman309795f2010-04-02 06:19:10 +00001818};
1819
1820#endif /* CONFIG_L2TP_V3 */
1821
James Chapmanfd558d12010-04-02 06:18:33 +00001822static int __init pppol2tp_init(void)
1823{
1824 int err;
1825
1826 err = register_pernet_device(&pppol2tp_net_ops);
1827 if (err)
1828 goto out;
1829
1830 err = proto_register(&pppol2tp_sk_proto, 0);
1831 if (err)
1832 goto out_unregister_pppol2tp_pernet;
1833
1834 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1835 if (err)
1836 goto out_unregister_pppol2tp_proto;
1837
James Chapman309795f2010-04-02 06:19:10 +00001838#ifdef CONFIG_L2TP_V3
1839 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1840 if (err)
1841 goto out_unregister_pppox;
1842#endif
1843
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001844 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001845
1846out:
1847 return err;
James Chapman309795f2010-04-02 06:19:10 +00001848
1849#ifdef CONFIG_L2TP_V3
1850out_unregister_pppox:
1851 unregister_pppox_proto(PX_PROTO_OL2TP);
1852#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001853out_unregister_pppol2tp_proto:
1854 proto_unregister(&pppol2tp_sk_proto);
1855out_unregister_pppol2tp_pernet:
1856 unregister_pernet_device(&pppol2tp_net_ops);
1857 goto out;
1858}
1859
1860static void __exit pppol2tp_exit(void)
1861{
James Chapman309795f2010-04-02 06:19:10 +00001862#ifdef CONFIG_L2TP_V3
1863 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1864#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001865 unregister_pppox_proto(PX_PROTO_OL2TP);
1866 proto_unregister(&pppol2tp_sk_proto);
1867 unregister_pernet_device(&pppol2tp_net_ops);
1868}
1869
1870module_init(pppol2tp_init);
1871module_exit(pppol2tp_exit);
1872
1873MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1874MODULE_DESCRIPTION("PPP over L2TP over UDP");
1875MODULE_LICENSE("GPL");
1876MODULE_VERSION(PPPOL2TP_DRV_VERSION);
Guillaume Nault681b4d82015-12-02 16:27:39 +01001877MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
Guillaume Nault249ee812017-04-03 13:23:15 +02001878MODULE_ALIAS_L2TP_PWTYPE(7);