blob: f36cae785e8217be3e9aed72d5fe7c01939511b6 [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
James Chapman0ad66142010-04-02 06:19:33 +0000536static void pppol2tp_show(struct seq_file *m, void *arg)
537{
538 struct l2tp_session *session = arg;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200539 struct sock *sk;
James Chapman0ad66142010-04-02 06:19:33 +0000540
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200541 sk = pppol2tp_session_get_sock(session);
542 if (sk) {
543 struct pppox_sock *po = pppox_sk(sk);
544
545 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
546 sock_put(sk);
James Chapman0ad66142010-04-02 06:19:33 +0000547 }
548}
James Chapman0ad66142010-04-02 06:19:33 +0000549
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200550static void pppol2tp_session_init(struct l2tp_session *session)
551{
552 struct pppol2tp_session *ps;
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200553
554 session->recv_skb = pppol2tp_recv;
Arnd Bergmannc2ebc252018-08-13 23:43:05 +0200555 if (IS_ENABLED(CONFIG_L2TP_DEBUGFS))
556 session->show = pppol2tp_show;
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200557
558 ps = l2tp_session_priv(session);
559 mutex_init(&ps->sk_lock);
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200560 ps->owner = current->pid;
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200561}
562
Guillaume Naulta4081942018-06-26 18:41:36 +0200563struct l2tp_connect_info {
564 u8 version;
565 int fd;
566 u32 tunnel_id;
567 u32 peer_tunnel_id;
568 u32 session_id;
569 u32 peer_session_id;
570};
571
572static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len,
573 struct l2tp_connect_info *info)
574{
575 switch (sa_len) {
576 case sizeof(struct sockaddr_pppol2tp):
577 {
578 const struct sockaddr_pppol2tp *sa_v2in4 = sa;
579
580 if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP)
581 return -EINVAL;
582
583 info->version = 2;
584 info->fd = sa_v2in4->pppol2tp.fd;
585 info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel;
586 info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel;
587 info->session_id = sa_v2in4->pppol2tp.s_session;
588 info->peer_session_id = sa_v2in4->pppol2tp.d_session;
589
590 break;
591 }
592 case sizeof(struct sockaddr_pppol2tpv3):
593 {
594 const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa;
595
596 if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP)
597 return -EINVAL;
598
599 info->version = 3;
600 info->fd = sa_v3in4->pppol2tp.fd;
601 info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel;
602 info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel;
603 info->session_id = sa_v3in4->pppol2tp.s_session;
604 info->peer_session_id = sa_v3in4->pppol2tp.d_session;
605
606 break;
607 }
608 case sizeof(struct sockaddr_pppol2tpin6):
609 {
610 const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa;
611
612 if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP)
613 return -EINVAL;
614
615 info->version = 2;
616 info->fd = sa_v2in6->pppol2tp.fd;
617 info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel;
618 info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel;
619 info->session_id = sa_v2in6->pppol2tp.s_session;
620 info->peer_session_id = sa_v2in6->pppol2tp.d_session;
621
622 break;
623 }
624 case sizeof(struct sockaddr_pppol2tpv3in6):
625 {
626 const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa;
627
628 if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP)
629 return -EINVAL;
630
631 info->version = 3;
632 info->fd = sa_v3in6->pppol2tp.fd;
633 info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel;
634 info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel;
635 info->session_id = sa_v3in6->pppol2tp.s_session;
636 info->peer_session_id = sa_v3in6->pppol2tp.d_session;
637
638 break;
639 }
640 default:
641 return -EINVAL;
642 }
643
644 return 0;
645}
646
Guillaume Nault789141b2018-08-03 12:38:37 +0200647/* Rough estimation of the maximum payload size a tunnel can transmit without
648 * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence
649 * numbers and no IP option. Not quite accurate, but the result is mostly
650 * unused anyway.
651 */
652static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
653{
654 int mtu;
655
656 mtu = l2tp_tunnel_dst_mtu(tunnel);
657 if (mtu <= PPPOL2TP_HEADER_OVERHEAD)
658 return 1500 - PPPOL2TP_HEADER_OVERHEAD;
659
660 return mtu - PPPOL2TP_HEADER_OVERHEAD;
661}
662
James Chapmanfd558d12010-04-02 06:18:33 +0000663/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
664 */
665static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
666 int sockaddr_len, int flags)
667{
668 struct sock *sk = sock->sk;
James Chapmanfd558d12010-04-02 06:18:33 +0000669 struct pppox_sock *po = pppox_sk(sk);
670 struct l2tp_session *session = NULL;
Guillaume Naulta4081942018-06-26 18:41:36 +0200671 struct l2tp_connect_info info;
James Chapmanfd558d12010-04-02 06:18:33 +0000672 struct l2tp_tunnel *tunnel;
673 struct pppol2tp_session *ps;
James Chapmanfd558d12010-04-02 06:18:33 +0000674 struct l2tp_session_cfg cfg = { 0, };
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200675 bool drop_refcnt = false;
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100676 bool drop_tunnel = false;
Guillaume Naultbda06be2018-06-13 15:09:21 +0200677 bool new_session = false;
678 bool new_tunnel = false;
Guillaume Naulta4081942018-06-26 18:41:36 +0200679 int error;
680
681 error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info);
682 if (error < 0)
683 return error;
James Chapmanfd558d12010-04-02 06:18:33 +0000684
685 lock_sock(sk);
686
James Chapmanfd558d12010-04-02 06:18:33 +0000687 /* Check for already bound sockets */
688 error = -EBUSY;
689 if (sk->sk_state & PPPOX_CONNECTED)
690 goto end;
691
692 /* We don't supporting rebinding anyway */
693 error = -EALREADY;
694 if (sk->sk_user_data)
695 goto end; /* socket is already attached */
696
James Chapmane0d44352010-04-02 06:18:54 +0000697 /* Don't bind if tunnel_id is 0 */
James Chapmanfd558d12010-04-02 06:18:33 +0000698 error = -EINVAL;
Guillaume Naulta4081942018-06-26 18:41:36 +0200699 if (!info.tunnel_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000700 goto end;
701
Guillaume Naulta4081942018-06-26 18:41:36 +0200702 tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id);
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100703 if (tunnel)
704 drop_tunnel = true;
James Chapman309795f2010-04-02 06:19:10 +0000705
James Chapmane0d44352010-04-02 06:18:54 +0000706 /* Special case: create tunnel context if session_id and
707 * peer_session_id is 0. Otherwise look up tunnel using supplied
James Chapmanfd558d12010-04-02 06:18:33 +0000708 * tunnel id.
709 */
Guillaume Naulta4081942018-06-26 18:41:36 +0200710 if (!info.session_id && !info.peer_session_id) {
James Chapman309795f2010-04-02 06:19:10 +0000711 if (tunnel == NULL) {
712 struct l2tp_tunnel_cfg tcfg = {
713 .encap = L2TP_ENCAPTYPE_UDP,
714 .debug = 0,
715 };
Guillaume Nault3e1bc8b2018-06-13 15:09:20 +0200716
717 /* Prevent l2tp_tunnel_register() from trying to set up
718 * a kernel socket.
719 */
Guillaume Naulta4081942018-06-26 18:41:36 +0200720 if (info.fd < 0) {
Guillaume Nault3e1bc8b2018-06-13 15:09:20 +0200721 error = -EBADF;
722 goto end;
723 }
724
Guillaume Naulta4081942018-06-26 18:41:36 +0200725 error = l2tp_tunnel_create(sock_net(sk), info.fd,
726 info.version,
727 info.tunnel_id,
728 info.peer_tunnel_id, &tcfg,
729 &tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000730 if (error < 0)
731 goto end;
Guillaume Nault6b9f3422018-04-10 21:01:12 +0200732
733 l2tp_tunnel_inc_refcount(tunnel);
734 error = l2tp_tunnel_register(tunnel, sock_net(sk),
735 &tcfg);
736 if (error < 0) {
737 kfree(tunnel);
738 goto end;
739 }
740 drop_tunnel = true;
Guillaume Naultbda06be2018-06-13 15:09:21 +0200741 new_tunnel = true;
James Chapman309795f2010-04-02 06:19:10 +0000742 }
James Chapmanfd558d12010-04-02 06:18:33 +0000743 } else {
James Chapmanfd558d12010-04-02 06:18:33 +0000744 /* Error if we can't find the tunnel */
745 error = -ENOENT;
746 if (tunnel == NULL)
747 goto end;
748
749 /* Error if socket is not prepped */
750 if (tunnel->sock == NULL)
751 goto end;
752 }
753
James Chapmanb79585f2012-04-29 21:48:50 +0000754 if (tunnel->peer_tunnel_id == 0)
Guillaume Naulta4081942018-06-26 18:41:36 +0200755 tunnel->peer_tunnel_id = info.peer_tunnel_id;
James Chapmanfd558d12010-04-02 06:18:33 +0000756
Guillaume Nault01e28b92018-08-10 13:21:57 +0200757 session = l2tp_tunnel_get_session(tunnel, info.session_id);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200758 if (session) {
759 drop_refcnt = true;
Guillaume Nault7ac6ab12018-06-13 15:09:19 +0200760
761 if (session->pwtype != L2TP_PWTYPE_PPP) {
762 error = -EPROTOTYPE;
763 goto end;
764 }
765
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200766 ps = l2tp_session_priv(session);
James Chapman309795f2010-04-02 06:19:10 +0000767
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200768 /* Using a pre-existing session is fine as long as it hasn't
769 * been connected yet.
770 */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200771 mutex_lock(&ps->sk_lock);
772 if (rcu_dereference_protected(ps->sk,
Guillaume Nault3d609342018-06-04 18:52:19 +0200773 lockdep_is_held(&ps->sk_lock)) ||
774 ps->__sk) {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200775 mutex_unlock(&ps->sk_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200776 error = -EEXIST;
777 goto end;
778 }
James Chapman309795f2010-04-02 06:19:10 +0000779 } else {
Guillaume Nault90904ff2018-06-13 15:09:18 +0200780 cfg.pw_type = L2TP_PWTYPE_PPP;
James Chapman309795f2010-04-02 06:19:10 +0000781
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200782 session = l2tp_session_create(sizeof(struct pppol2tp_session),
Guillaume Naulta4081942018-06-26 18:41:36 +0200783 tunnel, info.session_id,
784 info.peer_session_id, &cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200785 if (IS_ERR(session)) {
786 error = PTR_ERR(session);
James Chapman309795f2010-04-02 06:19:10 +0000787 goto end;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200788 }
Guillaume Nault3953ae72017-10-27 16:51:50 +0200789
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200790 pppol2tp_session_init(session);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200791 ps = l2tp_session_priv(session);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200792 l2tp_session_inc_refcount(session);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200793
794 mutex_lock(&ps->sk_lock);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200795 error = l2tp_session_register(session, tunnel);
796 if (error < 0) {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200797 mutex_unlock(&ps->sk_lock);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200798 kfree(session);
799 goto end;
800 }
801 drop_refcnt = true;
Guillaume Naultbda06be2018-06-13 15:09:21 +0200802 new_session = true;
James Chapman309795f2010-04-02 06:19:10 +0000803 }
804
James Chapmanfd558d12010-04-02 06:18:33 +0000805 /* Special case: if source & dest session_id == 0x0000, this
806 * socket is being created to manage the tunnel. Just set up
807 * the internal context for use by ioctl() and sockopt()
808 * handlers.
809 */
810 if ((session->session_id == 0) &&
811 (session->peer_session_id == 0)) {
812 error = 0;
813 goto out_no_ppp;
814 }
815
816 /* The only header we need to worry about is the L2TP
817 * header. This size is different depending on whether
818 * sequence numbers are enabled for the data channel.
819 */
820 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
821
822 po->chan.private = sk;
823 po->chan.ops = &pppol2tp_chan_ops;
Guillaume Nault789141b2018-08-03 12:38:37 +0200824 po->chan.mtu = pppol2tp_tunnel_mtu(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +0000825
826 error = ppp_register_net_channel(sock_net(sk), &po->chan);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200827 if (error) {
828 mutex_unlock(&ps->sk_lock);
James Chapmanfd558d12010-04-02 06:18:33 +0000829 goto end;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200830 }
James Chapmanfd558d12010-04-02 06:18:33 +0000831
832out_no_ppp:
833 /* This is how we get the session context from the socket. */
834 sk->sk_user_data = session;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200835 rcu_assign_pointer(ps->sk, sk);
836 mutex_unlock(&ps->sk_lock);
837
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200838 /* Keep the reference we've grabbed on the session: sk doesn't expect
839 * the session to disappear. pppol2tp_session_destruct() is responsible
840 * for dropping it.
841 */
842 drop_refcnt = false;
843
James Chapmanfd558d12010-04-02 06:18:33 +0000844 sk->sk_state = PPPOX_CONNECTED;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000845 l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000846 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000847
848end:
Guillaume Naultbda06be2018-06-13 15:09:21 +0200849 if (error) {
850 if (new_session)
851 l2tp_session_delete(session);
852 if (new_tunnel)
853 l2tp_tunnel_delete(tunnel);
854 }
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200855 if (drop_refcnt)
856 l2tp_session_dec_refcount(session);
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100857 if (drop_tunnel)
858 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +0000859 release_sock(sk);
860
861 return error;
862}
863
James Chapman309795f2010-04-02 06:19:10 +0000864#ifdef CONFIG_L2TP_V3
865
Guillaume Naultf026bc22017-09-01 17:58:51 +0200866/* Called when creating sessions via the netlink interface. */
867static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
868 u32 session_id, u32 peer_session_id,
869 struct l2tp_session_cfg *cfg)
James Chapman309795f2010-04-02 06:19:10 +0000870{
871 int error;
James Chapman309795f2010-04-02 06:19:10 +0000872 struct l2tp_session *session;
James Chapman309795f2010-04-02 06:19:10 +0000873
James Chapman309795f2010-04-02 06:19:10 +0000874 /* Error if tunnel socket is not prepped */
Guillaume Naultf026bc22017-09-01 17:58:51 +0200875 if (!tunnel->sock) {
876 error = -ENOENT;
Guillaume Nault3953ae72017-10-27 16:51:50 +0200877 goto err;
Guillaume Naultf026bc22017-09-01 17:58:51 +0200878 }
James Chapman309795f2010-04-02 06:19:10 +0000879
James Chapman309795f2010-04-02 06:19:10 +0000880 /* Allocate and initialize a new session context. */
James Chapman309795f2010-04-02 06:19:10 +0000881 session = l2tp_session_create(sizeof(struct pppol2tp_session),
882 tunnel, session_id,
883 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200884 if (IS_ERR(session)) {
885 error = PTR_ERR(session);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200886 goto err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200887 }
James Chapman309795f2010-04-02 06:19:10 +0000888
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200889 pppol2tp_session_init(session);
James Chapman309795f2010-04-02 06:19:10 +0000890
Guillaume Nault3953ae72017-10-27 16:51:50 +0200891 error = l2tp_session_register(session, tunnel);
892 if (error < 0)
893 goto err_sess;
James Chapman309795f2010-04-02 06:19:10 +0000894
Guillaume Nault3953ae72017-10-27 16:51:50 +0200895 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000896
Guillaume Nault3953ae72017-10-27 16:51:50 +0200897err_sess:
898 kfree(session);
899err:
James Chapman309795f2010-04-02 06:19:10 +0000900 return error;
901}
902
James Chapman309795f2010-04-02 06:19:10 +0000903#endif /* CONFIG_L2TP_V3 */
904
James Chapmanfd558d12010-04-02 06:18:33 +0000905/* getname() support.
906 */
907static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100908 int peer)
James Chapmanfd558d12010-04-02 06:18:33 +0000909{
James Chapmane0d44352010-04-02 06:18:54 +0000910 int len = 0;
James Chapmanfd558d12010-04-02 06:18:33 +0000911 int error = 0;
912 struct l2tp_session *session;
913 struct l2tp_tunnel *tunnel;
914 struct sock *sk = sock->sk;
915 struct inet_sock *inet;
916 struct pppol2tp_session *pls;
917
918 error = -ENOTCONN;
919 if (sk == NULL)
920 goto end;
Gao Feng56cff472016-08-19 13:36:23 +0800921 if (!(sk->sk_state & PPPOX_CONNECTED))
James Chapmanfd558d12010-04-02 06:18:33 +0000922 goto end;
923
924 error = -EBADF;
925 session = pppol2tp_sock_to_session(sk);
926 if (session == NULL)
927 goto end;
928
929 pls = l2tp_session_priv(session);
Guillaume Nault7198c772017-11-11 06:06:31 +0900930 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000931
Benjamin LaHaisebbdb32c2012-03-20 03:57:54 +0000932 inet = inet_sk(tunnel->sock);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000933 if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
James Chapmane0d44352010-04-02 06:18:54 +0000934 struct sockaddr_pppol2tp sp;
935 len = sizeof(sp);
936 memset(&sp, 0, len);
937 sp.sa_family = AF_PPPOX;
938 sp.sa_protocol = PX_PROTO_OL2TP;
939 sp.pppol2tp.fd = tunnel->fd;
940 sp.pppol2tp.pid = pls->owner;
941 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
942 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
943 sp.pppol2tp.s_session = session->session_id;
944 sp.pppol2tp.d_session = session->peer_session_id;
945 sp.pppol2tp.addr.sin_family = AF_INET;
946 sp.pppol2tp.addr.sin_port = inet->inet_dport;
947 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
948 memcpy(uaddr, &sp, len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000949#if IS_ENABLED(CONFIG_IPV6)
950 } else if ((tunnel->version == 2) &&
951 (tunnel->sock->sk_family == AF_INET6)) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000952 struct sockaddr_pppol2tpin6 sp;
Eric Dumazetefe42082013-10-03 15:42:29 -0700953
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000954 len = sizeof(sp);
955 memset(&sp, 0, len);
956 sp.sa_family = AF_PPPOX;
957 sp.sa_protocol = PX_PROTO_OL2TP;
958 sp.pppol2tp.fd = tunnel->fd;
959 sp.pppol2tp.pid = pls->owner;
960 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
961 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
962 sp.pppol2tp.s_session = session->session_id;
963 sp.pppol2tp.d_session = session->peer_session_id;
964 sp.pppol2tp.addr.sin6_family = AF_INET6;
965 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700966 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
967 sizeof(tunnel->sock->sk_v6_daddr));
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000968 memcpy(uaddr, &sp, len);
969 } else if ((tunnel->version == 3) &&
970 (tunnel->sock->sk_family == AF_INET6)) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000971 struct sockaddr_pppol2tpv3in6 sp;
Eric Dumazetefe42082013-10-03 15:42:29 -0700972
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000973 len = sizeof(sp);
974 memset(&sp, 0, len);
975 sp.sa_family = AF_PPPOX;
976 sp.sa_protocol = PX_PROTO_OL2TP;
977 sp.pppol2tp.fd = tunnel->fd;
978 sp.pppol2tp.pid = pls->owner;
979 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
980 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
981 sp.pppol2tp.s_session = session->session_id;
982 sp.pppol2tp.d_session = session->peer_session_id;
983 sp.pppol2tp.addr.sin6_family = AF_INET6;
984 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700985 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
986 sizeof(tunnel->sock->sk_v6_daddr));
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000987 memcpy(uaddr, &sp, len);
988#endif
James Chapmane0d44352010-04-02 06:18:54 +0000989 } else if (tunnel->version == 3) {
990 struct sockaddr_pppol2tpv3 sp;
991 len = sizeof(sp);
992 memset(&sp, 0, len);
993 sp.sa_family = AF_PPPOX;
994 sp.sa_protocol = PX_PROTO_OL2TP;
995 sp.pppol2tp.fd = tunnel->fd;
996 sp.pppol2tp.pid = pls->owner;
997 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
998 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
999 sp.pppol2tp.s_session = session->session_id;
1000 sp.pppol2tp.d_session = session->peer_session_id;
1001 sp.pppol2tp.addr.sin_family = AF_INET;
1002 sp.pppol2tp.addr.sin_port = inet->inet_dport;
1003 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
1004 memcpy(uaddr, &sp, len);
1005 }
James Chapmanfd558d12010-04-02 06:18:33 +00001006
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001007 error = len;
James Chapmanfd558d12010-04-02 06:18:33 +00001008
James Chapmanfd558d12010-04-02 06:18:33 +00001009 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001010end:
1011 return error;
1012}
1013
1014/****************************************************************************
1015 * ioctl() handlers.
1016 *
1017 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1018 * sockets. However, in order to control kernel tunnel features, we allow
1019 * userspace to create a special "tunnel" PPPoX socket which is used for
1020 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1021 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1022 * calls.
1023 ****************************************************************************/
1024
1025static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
Guillaume Nault7390ed82018-08-10 13:22:02 +02001026 const struct l2tp_stats *stats)
James Chapmanfd558d12010-04-02 06:18:33 +00001027{
Guillaume Nault7390ed82018-08-10 13:22:02 +02001028 memset(dest, 0, sizeof(*dest));
1029
Tom Parkin7b7c0712013-03-19 06:11:22 +00001030 dest->tx_packets = atomic_long_read(&stats->tx_packets);
1031 dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1032 dest->tx_errors = atomic_long_read(&stats->tx_errors);
1033 dest->rx_packets = atomic_long_read(&stats->rx_packets);
1034 dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1035 dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1036 dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1037 dest->rx_errors = atomic_long_read(&stats->rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001038}
1039
Guillaume Nault528534f2018-08-10 13:22:00 +02001040static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
1041 struct l2tp_tunnel *tunnel)
1042{
1043 struct l2tp_session *session;
1044
1045 if (!stats->session_id) {
Guillaume Nault528534f2018-08-10 13:22:00 +02001046 pppol2tp_copy_stats(stats, &tunnel->stats);
1047 return 0;
1048 }
1049
1050 /* If session_id is set, search the corresponding session in the
1051 * context of this tunnel and record the session's statistics.
1052 */
1053 session = l2tp_tunnel_get_session(tunnel, stats->session_id);
1054 if (!session)
1055 return -EBADR;
1056
1057 if (session->pwtype != L2TP_PWTYPE_PPP) {
1058 l2tp_session_dec_refcount(session);
1059 return -EBADR;
1060 }
1061
Guillaume Nault528534f2018-08-10 13:22:00 +02001062 pppol2tp_copy_stats(stats, &session->stats);
1063 l2tp_session_dec_refcount(session);
1064
1065 return 0;
1066}
1067
James Chapmanfd558d12010-04-02 06:18:33 +00001068static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1069 unsigned long arg)
1070{
Guillaume Nault528534f2018-08-10 13:22:00 +02001071 struct pppol2tp_ioc_stats stats;
James Chapmanfd558d12010-04-02 06:18:33 +00001072 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +00001073
Guillaume Nault79e67602018-08-10 13:21:58 +02001074 switch (cmd) {
1075 case PPPIOCGMRU:
1076 case PPPIOCGFLAGS:
1077 session = sock->sk->sk_user_data;
1078 if (!session)
1079 return -ENOTCONN;
James Chapmanfd558d12010-04-02 06:18:33 +00001080
Guillaume Nault79e67602018-08-10 13:21:58 +02001081 /* Not defined for tunnels */
1082 if (!session->session_id && !session->peer_session_id)
1083 return -ENOSYS;
Guillaume Naultbdd02922018-08-10 13:21:58 +02001084
Guillaume Nault79e67602018-08-10 13:21:58 +02001085 if (put_user(0, (int __user *)arg))
1086 return -EFAULT;
1087 break;
1088
1089 case PPPIOCSMRU:
1090 case PPPIOCSFLAGS:
1091 session = sock->sk->sk_user_data;
1092 if (!session)
1093 return -ENOTCONN;
1094
1095 /* Not defined for tunnels */
1096 if (!session->session_id && !session->peer_session_id)
1097 return -ENOSYS;
1098
Jakub Kicinski503c0182019-04-17 13:51:55 -07001099 if (!access_ok((int __user *)arg, sizeof(int)))
Guillaume Nault79e67602018-08-10 13:21:58 +02001100 return -EFAULT;
1101 break;
1102
1103 case PPPIOCGL2TPSTATS:
1104 session = sock->sk->sk_user_data;
1105 if (!session)
1106 return -ENOTCONN;
1107
1108 /* Session 0 represents the parent tunnel */
Guillaume Nault528534f2018-08-10 13:22:00 +02001109 if (!session->session_id && !session->peer_session_id) {
1110 u32 session_id;
1111 int err;
1112
1113 if (copy_from_user(&stats, (void __user *)arg,
1114 sizeof(stats)))
1115 return -EFAULT;
1116
1117 session_id = stats.session_id;
1118 err = pppol2tp_tunnel_copy_stats(&stats,
1119 session->tunnel);
1120 if (err < 0)
1121 return err;
1122
1123 stats.session_id = session_id;
1124 } else {
Guillaume Naultb0e29062018-08-10 13:22:01 +02001125 pppol2tp_copy_stats(&stats, &session->stats);
1126 stats.session_id = session->session_id;
Guillaume Nault528534f2018-08-10 13:22:00 +02001127 }
1128 stats.tunnel_id = session->tunnel->tunnel_id;
1129 stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel);
1130
1131 if (copy_to_user((void __user *)arg, &stats, sizeof(stats)))
1132 return -EFAULT;
Guillaume Nault79e67602018-08-10 13:21:58 +02001133 break;
1134
1135 default:
Guillaume Nault4f5f85e2018-08-10 13:22:03 +02001136 return -ENOIOCTLCMD;
James Chapmanfd558d12010-04-02 06:18:33 +00001137 }
1138
Guillaume Nault79e67602018-08-10 13:21:58 +02001139 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001140}
1141
1142/*****************************************************************************
1143 * setsockopt() / getsockopt() support.
1144 *
1145 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1146 * sockets. In order to control kernel tunnel features, we allow userspace to
1147 * create a special "tunnel" PPPoX socket which is used for control only.
1148 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1149 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1150 *****************************************************************************/
1151
1152/* Tunnel setsockopt() helper.
1153 */
1154static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1155 struct l2tp_tunnel *tunnel,
1156 int optname, int val)
1157{
1158 int err = 0;
1159
1160 switch (optname) {
1161 case PPPOL2TP_SO_DEBUG:
1162 tunnel->debug = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001163 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001164 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001165 break;
1166
1167 default:
1168 err = -ENOPROTOOPT;
1169 break;
1170 }
1171
1172 return err;
1173}
1174
1175/* Session setsockopt helper.
1176 */
1177static int pppol2tp_session_setsockopt(struct sock *sk,
1178 struct l2tp_session *session,
1179 int optname, int val)
1180{
1181 int err = 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001182
1183 switch (optname) {
1184 case PPPOL2TP_SO_RECVSEQ:
1185 if ((val != 0) && (val != 1)) {
1186 err = -EINVAL;
1187 break;
1188 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001189 session->recv_seq = !!val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001190 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001191 "%s: set recv_seq=%d\n",
1192 session->name, session->recv_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001193 break;
1194
1195 case PPPOL2TP_SO_SENDSEQ:
1196 if ((val != 0) && (val != 1)) {
1197 err = -EINVAL;
1198 break;
1199 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001200 session->send_seq = !!val;
James Chapmanfd558d12010-04-02 06:18:33 +00001201 {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001202 struct pppox_sock *po = pppox_sk(sk);
1203
James Chapmanfd558d12010-04-02 06:18:33 +00001204 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1205 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1206 }
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001207 l2tp_session_set_header_len(session, session->tunnel->version);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001208 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001209 "%s: set send_seq=%d\n",
1210 session->name, session->send_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001211 break;
1212
1213 case PPPOL2TP_SO_LNSMODE:
1214 if ((val != 0) && (val != 1)) {
1215 err = -EINVAL;
1216 break;
1217 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001218 session->lns_mode = !!val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001219 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001220 "%s: set lns_mode=%d\n",
1221 session->name, session->lns_mode);
James Chapmanfd558d12010-04-02 06:18:33 +00001222 break;
1223
1224 case PPPOL2TP_SO_DEBUG:
1225 session->debug = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001226 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001227 session->name, session->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001228 break;
1229
1230 case PPPOL2TP_SO_REORDERTO:
1231 session->reorder_timeout = msecs_to_jiffies(val);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001232 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001233 "%s: set reorder_timeout=%d\n",
1234 session->name, session->reorder_timeout);
James Chapmanfd558d12010-04-02 06:18:33 +00001235 break;
1236
1237 default:
1238 err = -ENOPROTOOPT;
1239 break;
1240 }
1241
1242 return err;
1243}
1244
1245/* Main setsockopt() entry point.
1246 * Does API checks, then calls either the tunnel or session setsockopt
1247 * handler, according to whether the PPPoL2TP socket is a for a regular
1248 * session or the special tunnel type.
1249 */
1250static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1251 char __user *optval, unsigned int optlen)
1252{
1253 struct sock *sk = sock->sk;
1254 struct l2tp_session *session;
1255 struct l2tp_tunnel *tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001256 int val;
1257 int err;
1258
1259 if (level != SOL_PPPOL2TP)
Sasha Levin3cf521f2014-07-14 17:02:31 -07001260 return -EINVAL;
James Chapmanfd558d12010-04-02 06:18:33 +00001261
1262 if (optlen < sizeof(int))
1263 return -EINVAL;
1264
1265 if (get_user(val, (int __user *)optval))
1266 return -EFAULT;
1267
1268 err = -ENOTCONN;
1269 if (sk->sk_user_data == NULL)
1270 goto end;
1271
1272 /* Get session context from the socket */
1273 err = -EBADF;
1274 session = pppol2tp_sock_to_session(sk);
1275 if (session == NULL)
1276 goto end;
1277
1278 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1279 */
James Chapmanfd558d12010-04-02 06:18:33 +00001280 if ((session->session_id == 0) &&
1281 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001282 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001283 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
Guillaume Nault7198c772017-11-11 06:06:31 +09001284 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001285 err = pppol2tp_session_setsockopt(sk, session, optname, val);
Guillaume Nault7198c772017-11-11 06:06:31 +09001286 }
James Chapmanfd558d12010-04-02 06:18:33 +00001287
James Chapmanfd558d12010-04-02 06:18:33 +00001288 sock_put(sk);
1289end:
1290 return err;
1291}
1292
1293/* Tunnel getsockopt helper. Called with sock locked.
1294 */
1295static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1296 struct l2tp_tunnel *tunnel,
1297 int optname, int *val)
1298{
1299 int err = 0;
1300
1301 switch (optname) {
1302 case PPPOL2TP_SO_DEBUG:
1303 *val = tunnel->debug;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001304 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001305 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001306 break;
1307
1308 default:
1309 err = -ENOPROTOOPT;
1310 break;
1311 }
1312
1313 return err;
1314}
1315
1316/* Session getsockopt helper. Called with sock locked.
1317 */
1318static int pppol2tp_session_getsockopt(struct sock *sk,
1319 struct l2tp_session *session,
1320 int optname, int *val)
1321{
1322 int err = 0;
1323
1324 switch (optname) {
1325 case PPPOL2TP_SO_RECVSEQ:
1326 *val = session->recv_seq;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001327 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001328 "%s: get recv_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001329 break;
1330
1331 case PPPOL2TP_SO_SENDSEQ:
1332 *val = session->send_seq;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001333 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001334 "%s: get send_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001335 break;
1336
1337 case PPPOL2TP_SO_LNSMODE:
1338 *val = session->lns_mode;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001339 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001340 "%s: get lns_mode=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001341 break;
1342
1343 case PPPOL2TP_SO_DEBUG:
1344 *val = session->debug;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001345 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001346 session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001347 break;
1348
1349 case PPPOL2TP_SO_REORDERTO:
1350 *val = (int) jiffies_to_msecs(session->reorder_timeout);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001351 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001352 "%s: get reorder_timeout=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001353 break;
1354
1355 default:
1356 err = -ENOPROTOOPT;
1357 }
1358
1359 return err;
1360}
1361
1362/* Main getsockopt() entry point.
1363 * Does API checks, then calls either the tunnel or session getsockopt
1364 * handler, according to whether the PPPoX socket is a for a regular session
1365 * or the special tunnel type.
1366 */
Joe Perchese3192692012-06-03 17:41:40 +00001367static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1368 char __user *optval, int __user *optlen)
James Chapmanfd558d12010-04-02 06:18:33 +00001369{
1370 struct sock *sk = sock->sk;
1371 struct l2tp_session *session;
1372 struct l2tp_tunnel *tunnel;
1373 int val, len;
1374 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001375
1376 if (level != SOL_PPPOL2TP)
Sasha Levin3cf521f2014-07-14 17:02:31 -07001377 return -EINVAL;
James Chapmanfd558d12010-04-02 06:18:33 +00001378
Joe Perchese3192692012-06-03 17:41:40 +00001379 if (get_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001380 return -EFAULT;
1381
1382 len = min_t(unsigned int, len, sizeof(int));
1383
1384 if (len < 0)
1385 return -EINVAL;
1386
1387 err = -ENOTCONN;
1388 if (sk->sk_user_data == NULL)
1389 goto end;
1390
1391 /* Get the session context */
1392 err = -EBADF;
1393 session = pppol2tp_sock_to_session(sk);
1394 if (session == NULL)
1395 goto end;
1396
1397 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
James Chapmanfd558d12010-04-02 06:18:33 +00001398 if ((session->session_id == 0) &&
1399 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001400 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001401 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
Guillaume Nault321a52a2017-04-06 18:31:21 +02001402 if (err)
1403 goto end_put_sess;
1404 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001405 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
Guillaume Nault321a52a2017-04-06 18:31:21 +02001406 if (err)
1407 goto end_put_sess;
1408 }
James Chapmanfd558d12010-04-02 06:18:33 +00001409
1410 err = -EFAULT;
Joe Perchese3192692012-06-03 17:41:40 +00001411 if (put_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001412 goto end_put_sess;
1413
1414 if (copy_to_user((void __user *) optval, &val, len))
1415 goto end_put_sess;
1416
1417 err = 0;
1418
1419end_put_sess:
1420 sock_put(sk);
1421end:
1422 return err;
1423}
1424
1425/*****************************************************************************
1426 * /proc filesystem for debug
James Chapmanf7faffa2010-04-02 06:18:49 +00001427 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1428 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
James Chapmanfd558d12010-04-02 06:18:33 +00001429 *****************************************************************************/
1430
1431static unsigned int pppol2tp_net_id;
1432
1433#ifdef CONFIG_PROC_FS
1434
1435struct pppol2tp_seq_data {
1436 struct seq_net_private p;
1437 int tunnel_idx; /* current tunnel */
1438 int session_idx; /* index of session within current tunnel */
1439 struct l2tp_tunnel *tunnel;
1440 struct l2tp_session *session; /* NULL means get next tunnel */
1441};
1442
1443static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1444{
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001445 /* Drop reference taken during previous invocation */
1446 if (pd->tunnel)
1447 l2tp_tunnel_dec_refcount(pd->tunnel);
1448
James Chapmanf7faffa2010-04-02 06:18:49 +00001449 for (;;) {
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001450 pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx);
James Chapmanf7faffa2010-04-02 06:18:49 +00001451 pd->tunnel_idx++;
1452
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001453 /* Only accept L2TPv2 tunnels */
1454 if (!pd->tunnel || pd->tunnel->version == 2)
1455 return;
James Chapmanf7faffa2010-04-02 06:18:49 +00001456
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001457 l2tp_tunnel_dec_refcount(pd->tunnel);
James Chapmanf7faffa2010-04-02 06:18:49 +00001458 }
James Chapmanfd558d12010-04-02 06:18:33 +00001459}
1460
1461static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1462{
Guillaume Nault83494402018-04-25 19:54:14 +02001463 /* Drop reference taken during previous invocation */
1464 if (pd->session)
1465 l2tp_session_dec_refcount(pd->session);
1466
Guillaume Naulta4346212017-10-31 17:36:42 +01001467 pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
James Chapmanfd558d12010-04-02 06:18:33 +00001468 pd->session_idx++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001469
James Chapmanfd558d12010-04-02 06:18:33 +00001470 if (pd->session == NULL) {
1471 pd->session_idx = 0;
1472 pppol2tp_next_tunnel(net, pd);
1473 }
1474}
1475
1476static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1477{
1478 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1479 loff_t pos = *offs;
1480 struct net *net;
1481
1482 if (!pos)
1483 goto out;
1484
1485 BUG_ON(m->private == NULL);
1486 pd = m->private;
1487 net = seq_file_net(m);
1488
1489 if (pd->tunnel == NULL)
1490 pppol2tp_next_tunnel(net, pd);
1491 else
1492 pppol2tp_next_session(net, pd);
1493
1494 /* NULL tunnel and session indicates end of list */
1495 if ((pd->tunnel == NULL) && (pd->session == NULL))
1496 pd = NULL;
1497
1498out:
1499 return pd;
1500}
1501
1502static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1503{
1504 (*pos)++;
1505 return NULL;
1506}
1507
1508static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1509{
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001510 struct pppol2tp_seq_data *pd = v;
1511
1512 if (!pd || pd == SEQ_START_TOKEN)
1513 return;
1514
Guillaume Nault83494402018-04-25 19:54:14 +02001515 /* Drop reference taken by last invocation of pppol2tp_next_session()
1516 * or pppol2tp_next_tunnel().
1517 */
1518 if (pd->session) {
1519 l2tp_session_dec_refcount(pd->session);
1520 pd->session = NULL;
1521 }
Guillaume Nault5411b6182018-04-19 16:20:48 +02001522 if (pd->tunnel) {
Guillaume Nault0e0c3fe2018-04-12 20:50:34 +02001523 l2tp_tunnel_dec_refcount(pd->tunnel);
Guillaume Nault5411b6182018-04-19 16:20:48 +02001524 pd->tunnel = NULL;
Guillaume Nault5411b6182018-04-19 16:20:48 +02001525 }
James Chapmanfd558d12010-04-02 06:18:33 +00001526}
1527
1528static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1529{
1530 struct l2tp_tunnel *tunnel = v;
1531
1532 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1533 tunnel->name,
1534 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001535 refcount_read(&tunnel->ref_count) - 1);
Tom Parkin7b7c0712013-03-19 06:11:22 +00001536 seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001537 tunnel->debug,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001538 atomic_long_read(&tunnel->stats.tx_packets),
1539 atomic_long_read(&tunnel->stats.tx_bytes),
1540 atomic_long_read(&tunnel->stats.tx_errors),
1541 atomic_long_read(&tunnel->stats.rx_packets),
1542 atomic_long_read(&tunnel->stats.rx_bytes),
1543 atomic_long_read(&tunnel->stats.rx_errors));
James Chapmanfd558d12010-04-02 06:18:33 +00001544}
1545
1546static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1547{
1548 struct l2tp_session *session = v;
1549 struct l2tp_tunnel *tunnel = session->tunnel;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001550 unsigned char state;
1551 char user_data_ok;
1552 struct sock *sk;
James Chapmanfd558d12010-04-02 06:18:33 +00001553 u32 ip = 0;
1554 u16 port = 0;
1555
1556 if (tunnel->sock) {
1557 struct inet_sock *inet = inet_sk(tunnel->sock);
1558 ip = ntohl(inet->inet_saddr);
1559 port = ntohs(inet->inet_sport);
1560 }
1561
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001562 sk = pppol2tp_session_get_sock(session);
1563 if (sk) {
1564 state = sk->sk_state;
1565 user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1566 } else {
1567 state = 0;
1568 user_data_ok = 'N';
1569 }
1570
James Chapmanfd558d12010-04-02 06:18:33 +00001571 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1572 "%04X/%04X %d %c\n",
1573 session->name, ip, port,
1574 tunnel->tunnel_id,
1575 session->session_id,
1576 tunnel->peer_tunnel_id,
1577 session->peer_session_id,
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001578 state, user_data_ok);
Guillaume Nault789141b2018-08-03 12:38:37 +02001579 seq_printf(m, " 0/0/%c/%c/%s %08x %u\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001580 session->recv_seq ? 'R' : '-',
1581 session->send_seq ? 'S' : '-',
1582 session->lns_mode ? "LNS" : "LAC",
1583 session->debug,
1584 jiffies_to_msecs(session->reorder_timeout));
Tom Parkin7b7c0712013-03-19 06:11:22 +00001585 seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001586 session->nr, session->ns,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001587 atomic_long_read(&session->stats.tx_packets),
1588 atomic_long_read(&session->stats.tx_bytes),
1589 atomic_long_read(&session->stats.tx_errors),
1590 atomic_long_read(&session->stats.rx_packets),
1591 atomic_long_read(&session->stats.rx_bytes),
1592 atomic_long_read(&session->stats.rx_errors));
James Chapman93454712010-04-02 06:18:44 +00001593
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001594 if (sk) {
1595 struct pppox_sock *po = pppox_sk(sk);
1596
James Chapman93454712010-04-02 06:18:44 +00001597 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001598 sock_put(sk);
1599 }
James Chapmanfd558d12010-04-02 06:18:33 +00001600}
1601
1602static int pppol2tp_seq_show(struct seq_file *m, void *v)
1603{
1604 struct pppol2tp_seq_data *pd = v;
1605
1606 /* display header on line 1 */
1607 if (v == SEQ_START_TOKEN) {
1608 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1609 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1610 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1611 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1612 "dest-tid/sid state user-data-ok\n");
1613 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1614 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1615 goto out;
1616 }
1617
Guillaume Nault83494402018-04-25 19:54:14 +02001618 if (!pd->session)
James Chapmanfd558d12010-04-02 06:18:33 +00001619 pppol2tp_seq_tunnel_show(m, pd->tunnel);
Guillaume Nault83494402018-04-25 19:54:14 +02001620 else
James Chapmanfd558d12010-04-02 06:18:33 +00001621 pppol2tp_seq_session_show(m, pd->session);
1622
1623out:
1624 return 0;
1625}
1626
1627static const struct seq_operations pppol2tp_seq_ops = {
1628 .start = pppol2tp_seq_start,
1629 .next = pppol2tp_seq_next,
1630 .stop = pppol2tp_seq_stop,
1631 .show = pppol2tp_seq_show,
1632};
James Chapmanfd558d12010-04-02 06:18:33 +00001633#endif /* CONFIG_PROC_FS */
1634
1635/*****************************************************************************
1636 * Network namespace
1637 *****************************************************************************/
1638
1639static __net_init int pppol2tp_init_net(struct net *net)
1640{
1641 struct proc_dir_entry *pde;
1642 int err = 0;
1643
Christoph Hellwigc3506372018-04-10 19:42:55 +02001644 pde = proc_create_net("pppol2tp", 0444, net->proc_net,
1645 &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data));
James Chapmanfd558d12010-04-02 06:18:33 +00001646 if (!pde) {
1647 err = -ENOMEM;
1648 goto out;
1649 }
1650
1651out:
1652 return err;
1653}
1654
1655static __net_exit void pppol2tp_exit_net(struct net *net)
1656{
Gao fengece31ff2013-02-18 01:34:56 +00001657 remove_proc_entry("pppol2tp", net->proc_net);
James Chapmanfd558d12010-04-02 06:18:33 +00001658}
1659
1660static struct pernet_operations pppol2tp_net_ops = {
1661 .init = pppol2tp_init_net,
1662 .exit = pppol2tp_exit_net,
1663 .id = &pppol2tp_net_id,
1664};
1665
1666/*****************************************************************************
1667 * Init and cleanup
1668 *****************************************************************************/
1669
1670static const struct proto_ops pppol2tp_ops = {
1671 .family = AF_PPPOX,
1672 .owner = THIS_MODULE,
1673 .release = pppol2tp_release,
1674 .bind = sock_no_bind,
1675 .connect = pppol2tp_connect,
1676 .socketpair = sock_no_socketpair,
1677 .accept = sock_no_accept,
1678 .getname = pppol2tp_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001679 .poll = datagram_poll,
James Chapmanfd558d12010-04-02 06:18:33 +00001680 .listen = sock_no_listen,
1681 .shutdown = sock_no_shutdown,
1682 .setsockopt = pppol2tp_setsockopt,
1683 .getsockopt = pppol2tp_getsockopt,
1684 .sendmsg = pppol2tp_sendmsg,
1685 .recvmsg = pppol2tp_recvmsg,
1686 .mmap = sock_no_mmap,
1687 .ioctl = pppox_ioctl,
1688};
1689
Eric Dumazet756e64a2010-09-21 06:43:54 +00001690static const struct pppox_proto pppol2tp_proto = {
James Chapmanfd558d12010-04-02 06:18:33 +00001691 .create = pppol2tp_create,
Wei Yongjune1558a92013-07-02 09:02:07 +08001692 .ioctl = pppol2tp_ioctl,
1693 .owner = THIS_MODULE,
James Chapmanfd558d12010-04-02 06:18:33 +00001694};
1695
James Chapman309795f2010-04-02 06:19:10 +00001696#ifdef CONFIG_L2TP_V3
1697
1698static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1699 .session_create = pppol2tp_session_create,
Tom Parkincf2f5c82013-03-19 06:11:21 +00001700 .session_delete = l2tp_session_delete,
James Chapman309795f2010-04-02 06:19:10 +00001701};
1702
1703#endif /* CONFIG_L2TP_V3 */
1704
James Chapmanfd558d12010-04-02 06:18:33 +00001705static int __init pppol2tp_init(void)
1706{
1707 int err;
1708
1709 err = register_pernet_device(&pppol2tp_net_ops);
1710 if (err)
1711 goto out;
1712
1713 err = proto_register(&pppol2tp_sk_proto, 0);
1714 if (err)
1715 goto out_unregister_pppol2tp_pernet;
1716
1717 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1718 if (err)
1719 goto out_unregister_pppol2tp_proto;
1720
James Chapman309795f2010-04-02 06:19:10 +00001721#ifdef CONFIG_L2TP_V3
1722 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1723 if (err)
1724 goto out_unregister_pppox;
1725#endif
1726
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001727 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001728
1729out:
1730 return err;
James Chapman309795f2010-04-02 06:19:10 +00001731
1732#ifdef CONFIG_L2TP_V3
1733out_unregister_pppox:
1734 unregister_pppox_proto(PX_PROTO_OL2TP);
1735#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001736out_unregister_pppol2tp_proto:
1737 proto_unregister(&pppol2tp_sk_proto);
1738out_unregister_pppol2tp_pernet:
1739 unregister_pernet_device(&pppol2tp_net_ops);
1740 goto out;
1741}
1742
1743static void __exit pppol2tp_exit(void)
1744{
James Chapman309795f2010-04-02 06:19:10 +00001745#ifdef CONFIG_L2TP_V3
1746 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1747#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001748 unregister_pppox_proto(PX_PROTO_OL2TP);
1749 proto_unregister(&pppol2tp_sk_proto);
1750 unregister_pernet_device(&pppol2tp_net_ops);
1751}
1752
1753module_init(pppol2tp_init);
1754module_exit(pppol2tp_exit);
1755
1756MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1757MODULE_DESCRIPTION("PPP over L2TP over UDP");
1758MODULE_LICENSE("GPL");
1759MODULE_VERSION(PPPOL2TP_DRV_VERSION);
Guillaume Nault681b4d82015-12-02 16:27:39 +01001760MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
Guillaume Nault249ee812017-04-03 13:23:15 +02001761MODULE_ALIAS_L2TP_PWTYPE(7);