blob: 88b4cb1b7cde310451f64babe64f8881d47e3162 [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 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
131 * socket */
132 int flags; /* accessed by PPPIOCGFLAGS.
133 * Unused. */
134};
135
136static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
137
stephen hemmingerd7100da2010-08-04 07:34:36 +0000138static const struct ppp_channel_ops pppol2tp_chan_ops = {
139 .start_xmit = pppol2tp_xmit,
140};
141
James Chapmanfd558d12010-04-02 06:18:33 +0000142static const struct proto_ops pppol2tp_ops;
143
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200144/* Retrieves the pppol2tp socket associated to a session.
145 * A reference is held on the returned socket, so this function must be paired
146 * with sock_put().
147 */
148static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session)
149{
150 struct pppol2tp_session *ps = l2tp_session_priv(session);
151 struct sock *sk;
152
153 rcu_read_lock();
154 sk = rcu_dereference(ps->sk);
155 if (sk)
156 sock_hold(sk);
157 rcu_read_unlock();
158
159 return sk;
160}
161
James Chapmanfd558d12010-04-02 06:18:33 +0000162/* Helpers to obtain tunnel/session contexts from sockets.
163 */
164static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
165{
166 struct l2tp_session *session;
167
168 if (sk == NULL)
169 return NULL;
170
171 sock_hold(sk);
172 session = (struct l2tp_session *)(sk->sk_user_data);
173 if (session == NULL) {
174 sock_put(sk);
175 goto out;
176 }
177
178 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
179
180out:
181 return session;
182}
183
184/*****************************************************************************
185 * Receive data handling
186 *****************************************************************************/
187
188static int pppol2tp_recv_payload_hook(struct sk_buff *skb)
189{
190 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
191 * don't send the PPP header (PPP header compression enabled), but
192 * other clients can include the header. So we cope with both cases
193 * here. The PPP header is always FF03 when using L2TP.
194 *
195 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
196 * the field may be unaligned.
197 */
198 if (!pskb_may_pull(skb, 2))
199 return 1;
200
Gao Feng54c151d2016-08-22 22:50:02 +0800201 if ((skb->data[0] == PPP_ALLSTATIONS) && (skb->data[1] == PPP_UI))
James Chapmanfd558d12010-04-02 06:18:33 +0000202 skb_pull(skb, 2);
203
204 return 0;
205}
206
207/* Receive message. This is the recvmsg for the PPPoL2TP socket.
208 */
Ying Xue1b784142015-03-02 15:37:48 +0800209static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
210 size_t len, int flags)
James Chapmanfd558d12010-04-02 06:18:33 +0000211{
212 int err;
213 struct sk_buff *skb;
214 struct sock *sk = sock->sk;
215
216 err = -EIO;
217 if (sk->sk_state & PPPOX_BOUND)
218 goto end;
219
James Chapmanfd558d12010-04-02 06:18:33 +0000220 err = 0;
221 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
222 flags & MSG_DONTWAIT, &err);
223 if (!skb)
224 goto end;
225
226 if (len > skb->len)
227 len = skb->len;
228 else if (len < skb->len)
229 msg->msg_flags |= MSG_TRUNC;
230
David S. Miller51f3d022014-11-05 16:46:40 -0500231 err = skb_copy_datagram_msg(skb, 0, msg, len);
James Chapmanfd558d12010-04-02 06:18:33 +0000232 if (likely(err == 0))
233 err = len;
234
235 kfree_skb(skb);
236end:
237 return err;
238}
239
240static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
241{
242 struct pppol2tp_session *ps = l2tp_session_priv(session);
243 struct sock *sk = NULL;
244
245 /* If the socket is bound, send it in to PPP's input queue. Otherwise
246 * queue it on the session socket.
247 */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200248 rcu_read_lock();
249 sk = rcu_dereference(ps->sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000250 if (sk == NULL)
251 goto no_sock;
252
253 if (sk->sk_state & PPPOX_BOUND) {
254 struct pppox_sock *po;
Guillaume Nault98f40b32015-12-29 13:06:59 +0100255
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000256 l2tp_dbg(session, L2TP_MSG_DATA,
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000257 "%s: recv %d byte data frame, passing to ppp\n",
258 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000259
James Chapmanfd558d12010-04-02 06:18:33 +0000260 po = pppox_sk(sk);
261 ppp_input(&po->chan, skb);
262 } else {
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000263 l2tp_dbg(session, L2TP_MSG_DATA,
Guillaume Nault9e9cb622014-03-06 11:15:10 +0100264 "%s: recv %d byte data frame, passing to L2TP socket\n",
265 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000266
Guillaume Nault9e9cb622014-03-06 11:15:10 +0100267 if (sock_queue_rcv_skb(sk, skb) < 0) {
268 atomic_long_inc(&session->stats.rx_errors);
269 kfree_skb(skb);
270 }
James Chapmanfd558d12010-04-02 06:18:33 +0000271 }
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200272 rcu_read_unlock();
James Chapmanfd558d12010-04-02 06:18:33 +0000273
274 return;
275
276no_sock:
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200277 rcu_read_unlock();
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000278 l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000279 kfree_skb(skb);
280}
281
James Chapmanfd558d12010-04-02 06:18:33 +0000282/************************************************************************
283 * Transmit handling
284 ***********************************************************************/
285
James Chapmanfd558d12010-04-02 06:18:33 +0000286/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
287 * when a user application does a sendmsg() on the session socket. L2TP and
288 * PPP headers must be inserted into the user's data.
289 */
Ying Xue1b784142015-03-02 15:37:48 +0800290static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
James Chapmanfd558d12010-04-02 06:18:33 +0000291 size_t total_len)
292{
James Chapmanfd558d12010-04-02 06:18:33 +0000293 struct sock *sk = sock->sk;
294 struct sk_buff *skb;
295 int error;
296 struct l2tp_session *session;
297 struct l2tp_tunnel *tunnel;
James Chapman0d767512010-04-02 06:19:00 +0000298 int uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +0000299
300 error = -ENOTCONN;
301 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
302 goto error;
303
304 /* Get session and tunnel contexts */
305 error = -EBADF;
306 session = pppol2tp_sock_to_session(sk);
307 if (session == NULL)
308 goto error;
309
Guillaume Nault7198c772017-11-11 06:06:31 +0900310 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000311
James Chapman0d767512010-04-02 06:19:00 +0000312 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
313
James Chapmanfd558d12010-04-02 06:18:33 +0000314 /* Allocate a socket buffer */
315 error = -ENOMEM;
316 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +0000317 uhlen + session->hdr_len +
Gao Feng54c151d2016-08-22 22:50:02 +0800318 2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
James Chapmanfd558d12010-04-02 06:18:33 +0000319 0, GFP_KERNEL);
320 if (!skb)
Guillaume Nault7198c772017-11-11 06:06:31 +0900321 goto error_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000322
323 /* Reserve space for headers. */
324 skb_reserve(skb, NET_SKB_PAD);
325 skb_reset_network_header(skb);
326 skb_reserve(skb, sizeof(struct iphdr));
327 skb_reset_transport_header(skb);
James Chapman0d767512010-04-02 06:19:00 +0000328 skb_reserve(skb, uhlen);
James Chapmanfd558d12010-04-02 06:18:33 +0000329
330 /* Add PPP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800331 skb->data[0] = PPP_ALLSTATIONS;
332 skb->data[1] = PPP_UI;
James Chapmanfd558d12010-04-02 06:18:33 +0000333 skb_put(skb, 2);
334
335 /* Copy user data into skb */
Al Viro6ce8e9c2014-04-06 21:25:44 -0400336 error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000337 if (error < 0) {
338 kfree_skb(skb);
Guillaume Nault7198c772017-11-11 06:06:31 +0900339 goto error_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000340 }
James Chapmanfd558d12010-04-02 06:18:33 +0000341
Eric Dumazet455cc322013-10-10 06:30:09 -0700342 local_bh_disable();
James Chapmanfd558d12010-04-02 06:18:33 +0000343 l2tp_xmit_skb(session, skb, session->hdr_len);
Eric Dumazet455cc322013-10-10 06:30:09 -0700344 local_bh_enable();
James Chapmanfd558d12010-04-02 06:18:33 +0000345
Guillaume Nault8b82547e33e2013-03-01 05:02:02 +0000346 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000347
Guillaume Naulta6f79d02013-06-12 16:07:36 +0200348 return total_len;
James Chapmanfd558d12010-04-02 06:18:33 +0000349
James Chapmanfd558d12010-04-02 06:18:33 +0000350error_put_sess:
351 sock_put(sk);
352error:
353 return error;
354}
355
356/* Transmit function called by generic PPP driver. Sends PPP frame
357 * over PPPoL2TP socket.
358 *
359 * This is almost the same as pppol2tp_sendmsg(), but rather than
360 * being called with a msghdr from userspace, it is called with a skb
361 * from the kernel.
362 *
363 * The supplied skb from ppp doesn't have enough headroom for the
364 * insertion of L2TP, UDP and IP headers so we need to allocate more
365 * headroom in the skb. This will create a cloned skb. But we must be
366 * careful in the error case because the caller will expect to free
367 * the skb it supplied, not our cloned skb. So we take care to always
368 * leave the original skb unfreed if we return an error.
369 */
370static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
371{
James Chapmanfd558d12010-04-02 06:18:33 +0000372 struct sock *sk = (struct sock *) chan->private;
James Chapmanfd558d12010-04-02 06:18:33 +0000373 struct l2tp_session *session;
374 struct l2tp_tunnel *tunnel;
Eric Dumazet09df57c2011-10-07 05:45:57 +0000375 int uhlen, headroom;
James Chapmanfd558d12010-04-02 06:18:33 +0000376
377 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
378 goto abort;
379
380 /* Get session and tunnel contexts from the socket */
381 session = pppol2tp_sock_to_session(sk);
382 if (session == NULL)
383 goto abort;
384
Guillaume Nault7198c772017-11-11 06:06:31 +0900385 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000386
Eric Dumazet09df57c2011-10-07 05:45:57 +0000387 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
388 headroom = NET_SKB_PAD +
389 sizeof(struct iphdr) + /* IP header */
390 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
391 session->hdr_len + /* L2TP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800392 2; /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */
Eric Dumazet09df57c2011-10-07 05:45:57 +0000393 if (skb_cow_head(skb, headroom))
Guillaume Nault7198c772017-11-11 06:06:31 +0900394 goto abort_put_sess;
James Chapmanfd558d12010-04-02 06:18:33 +0000395
James Chapmanfd558d12010-04-02 06:18:33 +0000396 /* Setup PPP header */
Gao Feng54c151d2016-08-22 22:50:02 +0800397 __skb_push(skb, 2);
398 skb->data[0] = PPP_ALLSTATIONS;
399 skb->data[1] = PPP_UI;
James Chapmanfd558d12010-04-02 06:18:33 +0000400
Eric Dumazet455cc322013-10-10 06:30:09 -0700401 local_bh_disable();
James Chapmane0d44352010-04-02 06:18:54 +0000402 l2tp_xmit_skb(session, skb, session->hdr_len);
Eric Dumazet455cc322013-10-10 06:30:09 -0700403 local_bh_enable();
James Chapmanfd558d12010-04-02 06:18:33 +0000404
James Chapmanfd558d12010-04-02 06:18:33 +0000405 sock_put(sk);
Guillaume Nault7198c772017-11-11 06:06:31 +0900406
James Chapmanfd558d12010-04-02 06:18:33 +0000407 return 1;
408
James Chapmanfd558d12010-04-02 06:18:33 +0000409abort_put_sess:
410 sock_put(sk);
411abort:
412 /* Free the original skb */
413 kfree_skb(skb);
414 return 1;
415}
416
417/*****************************************************************************
418 * Session (and tunnel control) socket create/destroy.
419 *****************************************************************************/
420
421/* Called by l2tp_core when a session socket is being closed.
422 */
423static void pppol2tp_session_close(struct l2tp_session *session)
424{
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200425 struct sock *sk;
James Chapmanfd558d12010-04-02 06:18:33 +0000426
427 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
428
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200429 sk = pppol2tp_session_get_sock(session);
430 if (sk) {
431 if (sk->sk_socket)
432 inet_shutdown(sk->sk_socket, SEND_SHUTDOWN);
433 sock_put(sk);
434 }
James Chapmanfd558d12010-04-02 06:18:33 +0000435}
436
437/* Really kill the session socket. (Called from sock_put() if
438 * refcnt == 0.)
439 */
440static void pppol2tp_session_destruct(struct sock *sk)
441{
Tom Parkinf6e16b22013-03-19 06:11:23 +0000442 struct l2tp_session *session = sk->sk_user_data;
Guillaume Naulte91793b2017-03-29 08:45:29 +0200443
444 skb_queue_purge(&sk->sk_receive_queue);
445 skb_queue_purge(&sk->sk_write_queue);
446
Tom Parkinf6e16b22013-03-19 06:11:23 +0000447 if (session) {
James Chapmanfd558d12010-04-02 06:18:33 +0000448 sk->sk_user_data = NULL;
449 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
450 l2tp_session_dec_refcount(session);
451 }
James Chapmanfd558d12010-04-02 06:18:33 +0000452}
453
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200454static void pppol2tp_put_sk(struct rcu_head *head)
455{
456 struct pppol2tp_session *ps;
457
458 ps = container_of(head, typeof(*ps), rcu);
459 sock_put(ps->__sk);
460}
461
James Chapmanfd558d12010-04-02 06:18:33 +0000462/* Called when the PPPoX socket (session) is closed.
463 */
464static int pppol2tp_release(struct socket *sock)
465{
466 struct sock *sk = sock->sk;
467 struct l2tp_session *session;
468 int error;
469
470 if (!sk)
471 return 0;
472
473 error = -EBADF;
474 lock_sock(sk);
475 if (sock_flag(sk, SOCK_DEAD) != 0)
476 goto error;
477
478 pppox_unbind_sock(sk);
479
480 /* Signal the death of the socket. */
481 sk->sk_state = PPPOX_DEAD;
482 sock_orphan(sk);
483 sock->sk = NULL;
484
485 session = pppol2tp_sock_to_session(sk);
486
James Chapmanfd558d12010-04-02 06:18:33 +0000487 if (session != NULL) {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200488 struct pppol2tp_session *ps;
489
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200490 l2tp_session_delete(session);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200491
492 ps = l2tp_session_priv(session);
493 mutex_lock(&ps->sk_lock);
494 ps->__sk = rcu_dereference_protected(ps->sk,
495 lockdep_is_held(&ps->sk_lock));
496 RCU_INIT_POINTER(ps->sk, NULL);
497 mutex_unlock(&ps->sk_lock);
498 call_rcu(&ps->rcu, pppol2tp_put_sk);
499
500 /* Rely on the sock_put() call at the end of the function for
501 * dropping the reference held by pppol2tp_sock_to_session().
502 * The last reference will be dropped by pppol2tp_put_sk().
503 */
James Chapmanfd558d12010-04-02 06:18:33 +0000504 }
James Chapmanfd558d12010-04-02 06:18:33 +0000505 release_sock(sk);
506
507 /* This will delete the session context via
508 * pppol2tp_session_destruct() if the socket's refcnt drops to
509 * zero.
510 */
511 sock_put(sk);
512
513 return 0;
514
515error:
516 release_sock(sk);
517 return error;
518}
519
520static struct proto pppol2tp_sk_proto = {
521 .name = "PPPOL2TP",
522 .owner = THIS_MODULE,
523 .obj_size = sizeof(struct pppox_sock),
524};
525
526static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
527{
528 int rc;
529
530 rc = l2tp_udp_encap_recv(sk, skb);
531 if (rc)
532 kfree_skb(skb);
533
534 return NET_RX_SUCCESS;
535}
536
537/* socket() handler. Initialize a new struct sock.
538 */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500539static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
James Chapmanfd558d12010-04-02 06:18:33 +0000540{
541 int error = -ENOMEM;
542 struct sock *sk;
543
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500544 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
James Chapmanfd558d12010-04-02 06:18:33 +0000545 if (!sk)
546 goto out;
547
548 sock_init_data(sock, sk);
549
550 sock->state = SS_UNCONNECTED;
551 sock->ops = &pppol2tp_ops;
552
553 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
554 sk->sk_protocol = PX_PROTO_OL2TP;
555 sk->sk_family = PF_PPPOX;
556 sk->sk_state = PPPOX_NONE;
557 sk->sk_type = SOCK_STREAM;
558 sk->sk_destruct = pppol2tp_session_destruct;
559
560 error = 0;
561
562out:
563 return error;
564}
565
Javier Martinez Canillas9dd79942016-09-09 08:43:17 -0400566#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
James Chapman0ad66142010-04-02 06:19:33 +0000567static void pppol2tp_show(struct seq_file *m, void *arg)
568{
569 struct l2tp_session *session = arg;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200570 struct sock *sk;
James Chapman0ad66142010-04-02 06:19:33 +0000571
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200572 sk = pppol2tp_session_get_sock(session);
573 if (sk) {
574 struct pppox_sock *po = pppox_sk(sk);
575
576 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
577 sock_put(sk);
James Chapman0ad66142010-04-02 06:19:33 +0000578 }
579}
580#endif
581
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200582static void pppol2tp_session_init(struct l2tp_session *session)
583{
584 struct pppol2tp_session *ps;
585 struct dst_entry *dst;
586
587 session->recv_skb = pppol2tp_recv;
588 session->session_close = pppol2tp_session_close;
589#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
590 session->show = pppol2tp_show;
591#endif
592
593 ps = l2tp_session_priv(session);
594 mutex_init(&ps->sk_lock);
595 ps->tunnel_sock = session->tunnel->sock;
596 ps->owner = current->pid;
597
598 /* If PMTU discovery was enabled, use the MTU that was discovered */
599 dst = sk_dst_get(session->tunnel->sock);
600 if (dst) {
601 u32 pmtu = dst_mtu(dst);
602
603 if (pmtu) {
604 session->mtu = pmtu - PPPOL2TP_HEADER_OVERHEAD;
605 session->mru = pmtu - PPPOL2TP_HEADER_OVERHEAD;
606 }
607 dst_release(dst);
608 }
609}
610
James Chapmanfd558d12010-04-02 06:18:33 +0000611/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
612 */
613static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
614 int sockaddr_len, int flags)
615{
616 struct sock *sk = sock->sk;
617 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
618 struct pppox_sock *po = pppox_sk(sk);
619 struct l2tp_session *session = NULL;
620 struct l2tp_tunnel *tunnel;
621 struct pppol2tp_session *ps;
James Chapmanfd558d12010-04-02 06:18:33 +0000622 struct l2tp_session_cfg cfg = { 0, };
623 int error = 0;
James Chapmane0d44352010-04-02 06:18:54 +0000624 u32 tunnel_id, peer_tunnel_id;
625 u32 session_id, peer_session_id;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200626 bool drop_refcnt = false;
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100627 bool drop_tunnel = false;
James Chapmane0d44352010-04-02 06:18:54 +0000628 int ver = 2;
629 int fd;
James Chapmanfd558d12010-04-02 06:18:33 +0000630
631 lock_sock(sk);
632
633 error = -EINVAL;
634 if (sp->sa_protocol != PX_PROTO_OL2TP)
635 goto end;
636
637 /* Check for already bound sockets */
638 error = -EBUSY;
639 if (sk->sk_state & PPPOX_CONNECTED)
640 goto end;
641
642 /* We don't supporting rebinding anyway */
643 error = -EALREADY;
644 if (sk->sk_user_data)
645 goto end; /* socket is already attached */
646
James Chapmanb79585f2012-04-29 21:48:50 +0000647 /* Get params from socket address. Handle L2TPv2 and L2TPv3.
648 * This is nasty because there are different sockaddr_pppol2tp
649 * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use
650 * the sockaddr size to determine which structure the caller
651 * is using.
652 */
653 peer_tunnel_id = 0;
James Chapmane0d44352010-04-02 06:18:54 +0000654 if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
655 fd = sp->pppol2tp.fd;
656 tunnel_id = sp->pppol2tp.s_tunnel;
657 peer_tunnel_id = sp->pppol2tp.d_tunnel;
658 session_id = sp->pppol2tp.s_session;
659 peer_session_id = sp->pppol2tp.d_session;
660 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
James Chapmanb79585f2012-04-29 21:48:50 +0000661 struct sockaddr_pppol2tpv3 *sp3 =
662 (struct sockaddr_pppol2tpv3 *) sp;
James Chapmane0d44352010-04-02 06:18:54 +0000663 ver = 3;
664 fd = sp3->pppol2tp.fd;
665 tunnel_id = sp3->pppol2tp.s_tunnel;
666 peer_tunnel_id = sp3->pppol2tp.d_tunnel;
667 session_id = sp3->pppol2tp.s_session;
668 peer_session_id = sp3->pppol2tp.d_session;
James Chapmanb79585f2012-04-29 21:48:50 +0000669 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) {
670 struct sockaddr_pppol2tpin6 *sp6 =
671 (struct sockaddr_pppol2tpin6 *) sp;
672 fd = sp6->pppol2tp.fd;
673 tunnel_id = sp6->pppol2tp.s_tunnel;
674 peer_tunnel_id = sp6->pppol2tp.d_tunnel;
675 session_id = sp6->pppol2tp.s_session;
676 peer_session_id = sp6->pppol2tp.d_session;
677 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) {
678 struct sockaddr_pppol2tpv3in6 *sp6 =
679 (struct sockaddr_pppol2tpv3in6 *) sp;
680 ver = 3;
681 fd = sp6->pppol2tp.fd;
682 tunnel_id = sp6->pppol2tp.s_tunnel;
683 peer_tunnel_id = sp6->pppol2tp.d_tunnel;
684 session_id = sp6->pppol2tp.s_session;
685 peer_session_id = sp6->pppol2tp.d_session;
James Chapmane0d44352010-04-02 06:18:54 +0000686 } else {
687 error = -EINVAL;
688 goto end; /* bad socket address */
689 }
690
691 /* Don't bind if tunnel_id is 0 */
James Chapmanfd558d12010-04-02 06:18:33 +0000692 error = -EINVAL;
James Chapmane0d44352010-04-02 06:18:54 +0000693 if (tunnel_id == 0)
James Chapmanfd558d12010-04-02 06:18:33 +0000694 goto end;
695
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100696 tunnel = l2tp_tunnel_get(sock_net(sk), tunnel_id);
697 if (tunnel)
698 drop_tunnel = true;
James Chapman309795f2010-04-02 06:19:10 +0000699
James Chapmane0d44352010-04-02 06:18:54 +0000700 /* Special case: create tunnel context if session_id and
701 * peer_session_id is 0. Otherwise look up tunnel using supplied
James Chapmanfd558d12010-04-02 06:18:33 +0000702 * tunnel id.
703 */
James Chapmane0d44352010-04-02 06:18:54 +0000704 if ((session_id == 0) && (peer_session_id == 0)) {
James Chapman309795f2010-04-02 06:19:10 +0000705 if (tunnel == NULL) {
706 struct l2tp_tunnel_cfg tcfg = {
707 .encap = L2TP_ENCAPTYPE_UDP,
708 .debug = 0,
709 };
710 error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
711 if (error < 0)
712 goto end;
713 }
James Chapmanfd558d12010-04-02 06:18:33 +0000714 } else {
James Chapmanfd558d12010-04-02 06:18:33 +0000715 /* Error if we can't find the tunnel */
716 error = -ENOENT;
717 if (tunnel == NULL)
718 goto end;
719
720 /* Error if socket is not prepped */
721 if (tunnel->sock == NULL)
722 goto end;
723 }
724
725 if (tunnel->recv_payload_hook == NULL)
726 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
727
James Chapmanb79585f2012-04-29 21:48:50 +0000728 if (tunnel->peer_tunnel_id == 0)
729 tunnel->peer_tunnel_id = peer_tunnel_id;
James Chapmanfd558d12010-04-02 06:18:33 +0000730
Guillaume Naulta4346212017-10-31 17:36:42 +0100731 session = l2tp_session_get(sock_net(sk), tunnel, session_id);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200732 if (session) {
733 drop_refcnt = true;
734 ps = l2tp_session_priv(session);
James Chapman309795f2010-04-02 06:19:10 +0000735
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200736 /* Using a pre-existing session is fine as long as it hasn't
737 * been connected yet.
738 */
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200739 mutex_lock(&ps->sk_lock);
740 if (rcu_dereference_protected(ps->sk,
741 lockdep_is_held(&ps->sk_lock))) {
742 mutex_unlock(&ps->sk_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200743 error = -EEXIST;
744 goto end;
745 }
746
747 /* consistency checks */
748 if (ps->tunnel_sock != tunnel->sock) {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200749 mutex_unlock(&ps->sk_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200750 error = -EEXIST;
James Chapman309795f2010-04-02 06:19:10 +0000751 goto end;
752 }
753 } else {
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200754 /* Default MTU must allow space for UDP/L2TP/PPP headers */
755 cfg.mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
756 cfg.mru = cfg.mtu;
James Chapman309795f2010-04-02 06:19:10 +0000757
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200758 session = l2tp_session_create(sizeof(struct pppol2tp_session),
759 tunnel, session_id,
760 peer_session_id, &cfg);
761 if (IS_ERR(session)) {
762 error = PTR_ERR(session);
James Chapman309795f2010-04-02 06:19:10 +0000763 goto end;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200764 }
Guillaume Nault3953ae72017-10-27 16:51:50 +0200765
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200766 pppol2tp_session_init(session);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200767 ps = l2tp_session_priv(session);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200768 l2tp_session_inc_refcount(session);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200769
770 mutex_lock(&ps->sk_lock);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200771 error = l2tp_session_register(session, tunnel);
772 if (error < 0) {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200773 mutex_unlock(&ps->sk_lock);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200774 kfree(session);
775 goto end;
776 }
777 drop_refcnt = true;
James Chapman309795f2010-04-02 06:19:10 +0000778 }
779
James Chapmanfd558d12010-04-02 06:18:33 +0000780 /* Special case: if source & dest session_id == 0x0000, this
781 * socket is being created to manage the tunnel. Just set up
782 * the internal context for use by ioctl() and sockopt()
783 * handlers.
784 */
785 if ((session->session_id == 0) &&
786 (session->peer_session_id == 0)) {
787 error = 0;
788 goto out_no_ppp;
789 }
790
791 /* The only header we need to worry about is the L2TP
792 * header. This size is different depending on whether
793 * sequence numbers are enabled for the data channel.
794 */
795 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
796
797 po->chan.private = sk;
798 po->chan.ops = &pppol2tp_chan_ops;
799 po->chan.mtu = session->mtu;
800
801 error = ppp_register_net_channel(sock_net(sk), &po->chan);
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200802 if (error) {
803 mutex_unlock(&ps->sk_lock);
James Chapmanfd558d12010-04-02 06:18:33 +0000804 goto end;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200805 }
James Chapmanfd558d12010-04-02 06:18:33 +0000806
807out_no_ppp:
808 /* This is how we get the session context from the socket. */
809 sk->sk_user_data = session;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +0200810 rcu_assign_pointer(ps->sk, sk);
811 mutex_unlock(&ps->sk_lock);
812
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200813 /* Keep the reference we've grabbed on the session: sk doesn't expect
814 * the session to disappear. pppol2tp_session_destruct() is responsible
815 * for dropping it.
816 */
817 drop_refcnt = false;
818
James Chapmanfd558d12010-04-02 06:18:33 +0000819 sk->sk_state = PPPOX_CONNECTED;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +0000820 l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000821 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000822
823end:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200824 if (drop_refcnt)
825 l2tp_session_dec_refcount(session);
Guillaume Naultf9e56ba2017-10-30 17:58:58 +0100826 if (drop_tunnel)
827 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +0000828 release_sock(sk);
829
830 return error;
831}
832
James Chapman309795f2010-04-02 06:19:10 +0000833#ifdef CONFIG_L2TP_V3
834
Guillaume Naultf026bc22017-09-01 17:58:51 +0200835/* Called when creating sessions via the netlink interface. */
836static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
837 u32 session_id, u32 peer_session_id,
838 struct l2tp_session_cfg *cfg)
James Chapman309795f2010-04-02 06:19:10 +0000839{
840 int error;
James Chapman309795f2010-04-02 06:19:10 +0000841 struct l2tp_session *session;
James Chapman309795f2010-04-02 06:19:10 +0000842
James Chapman309795f2010-04-02 06:19:10 +0000843 /* Error if tunnel socket is not prepped */
Guillaume Naultf026bc22017-09-01 17:58:51 +0200844 if (!tunnel->sock) {
845 error = -ENOENT;
Guillaume Nault3953ae72017-10-27 16:51:50 +0200846 goto err;
Guillaume Naultf026bc22017-09-01 17:58:51 +0200847 }
James Chapman309795f2010-04-02 06:19:10 +0000848
James Chapman309795f2010-04-02 06:19:10 +0000849 /* Default MTU values. */
850 if (cfg->mtu == 0)
851 cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
852 if (cfg->mru == 0)
853 cfg->mru = cfg->mtu;
854
855 /* Allocate and initialize a new session context. */
James Chapman309795f2010-04-02 06:19:10 +0000856 session = l2tp_session_create(sizeof(struct pppol2tp_session),
857 tunnel, session_id,
858 peer_session_id, cfg);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200859 if (IS_ERR(session)) {
860 error = PTR_ERR(session);
Guillaume Nault3953ae72017-10-27 16:51:50 +0200861 goto err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200862 }
James Chapman309795f2010-04-02 06:19:10 +0000863
Guillaume Naultf98be6c2017-10-27 16:51:52 +0200864 pppol2tp_session_init(session);
James Chapman309795f2010-04-02 06:19:10 +0000865
Guillaume Nault3953ae72017-10-27 16:51:50 +0200866 error = l2tp_session_register(session, tunnel);
867 if (error < 0)
868 goto err_sess;
James Chapman309795f2010-04-02 06:19:10 +0000869
Guillaume Nault3953ae72017-10-27 16:51:50 +0200870 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000871
Guillaume Nault3953ae72017-10-27 16:51:50 +0200872err_sess:
873 kfree(session);
874err:
James Chapman309795f2010-04-02 06:19:10 +0000875 return error;
876}
877
James Chapman309795f2010-04-02 06:19:10 +0000878#endif /* CONFIG_L2TP_V3 */
879
James Chapmanfd558d12010-04-02 06:18:33 +0000880/* getname() support.
881 */
882static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
883 int *usockaddr_len, int peer)
884{
James Chapmane0d44352010-04-02 06:18:54 +0000885 int len = 0;
James Chapmanfd558d12010-04-02 06:18:33 +0000886 int error = 0;
887 struct l2tp_session *session;
888 struct l2tp_tunnel *tunnel;
889 struct sock *sk = sock->sk;
890 struct inet_sock *inet;
891 struct pppol2tp_session *pls;
892
893 error = -ENOTCONN;
894 if (sk == NULL)
895 goto end;
Gao Feng56cff472016-08-19 13:36:23 +0800896 if (!(sk->sk_state & PPPOX_CONNECTED))
James Chapmanfd558d12010-04-02 06:18:33 +0000897 goto end;
898
899 error = -EBADF;
900 session = pppol2tp_sock_to_session(sk);
901 if (session == NULL)
902 goto end;
903
904 pls = l2tp_session_priv(session);
Guillaume Nault7198c772017-11-11 06:06:31 +0900905 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000906
Benjamin LaHaisebbdb32c2012-03-20 03:57:54 +0000907 inet = inet_sk(tunnel->sock);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000908 if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
James Chapmane0d44352010-04-02 06:18:54 +0000909 struct sockaddr_pppol2tp sp;
910 len = sizeof(sp);
911 memset(&sp, 0, len);
912 sp.sa_family = AF_PPPOX;
913 sp.sa_protocol = PX_PROTO_OL2TP;
914 sp.pppol2tp.fd = tunnel->fd;
915 sp.pppol2tp.pid = pls->owner;
916 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
917 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
918 sp.pppol2tp.s_session = session->session_id;
919 sp.pppol2tp.d_session = session->peer_session_id;
920 sp.pppol2tp.addr.sin_family = AF_INET;
921 sp.pppol2tp.addr.sin_port = inet->inet_dport;
922 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
923 memcpy(uaddr, &sp, len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000924#if IS_ENABLED(CONFIG_IPV6)
925 } else if ((tunnel->version == 2) &&
926 (tunnel->sock->sk_family == AF_INET6)) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000927 struct sockaddr_pppol2tpin6 sp;
Eric Dumazetefe42082013-10-03 15:42:29 -0700928
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000929 len = sizeof(sp);
930 memset(&sp, 0, len);
931 sp.sa_family = AF_PPPOX;
932 sp.sa_protocol = PX_PROTO_OL2TP;
933 sp.pppol2tp.fd = tunnel->fd;
934 sp.pppol2tp.pid = pls->owner;
935 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
936 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
937 sp.pppol2tp.s_session = session->session_id;
938 sp.pppol2tp.d_session = session->peer_session_id;
939 sp.pppol2tp.addr.sin6_family = AF_INET6;
940 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700941 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
942 sizeof(tunnel->sock->sk_v6_daddr));
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000943 memcpy(uaddr, &sp, len);
944 } else if ((tunnel->version == 3) &&
945 (tunnel->sock->sk_family == AF_INET6)) {
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000946 struct sockaddr_pppol2tpv3in6 sp;
Eric Dumazetefe42082013-10-03 15:42:29 -0700947
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000948 len = sizeof(sp);
949 memset(&sp, 0, len);
950 sp.sa_family = AF_PPPOX;
951 sp.sa_protocol = PX_PROTO_OL2TP;
952 sp.pppol2tp.fd = tunnel->fd;
953 sp.pppol2tp.pid = pls->owner;
954 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
955 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
956 sp.pppol2tp.s_session = session->session_id;
957 sp.pppol2tp.d_session = session->peer_session_id;
958 sp.pppol2tp.addr.sin6_family = AF_INET6;
959 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700960 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
961 sizeof(tunnel->sock->sk_v6_daddr));
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000962 memcpy(uaddr, &sp, len);
963#endif
James Chapmane0d44352010-04-02 06:18:54 +0000964 } else if (tunnel->version == 3) {
965 struct sockaddr_pppol2tpv3 sp;
966 len = sizeof(sp);
967 memset(&sp, 0, len);
968 sp.sa_family = AF_PPPOX;
969 sp.sa_protocol = PX_PROTO_OL2TP;
970 sp.pppol2tp.fd = tunnel->fd;
971 sp.pppol2tp.pid = pls->owner;
972 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
973 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
974 sp.pppol2tp.s_session = session->session_id;
975 sp.pppol2tp.d_session = session->peer_session_id;
976 sp.pppol2tp.addr.sin_family = AF_INET;
977 sp.pppol2tp.addr.sin_port = inet->inet_dport;
978 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
979 memcpy(uaddr, &sp, len);
980 }
James Chapmanfd558d12010-04-02 06:18:33 +0000981
982 *usockaddr_len = len;
phil.turnbull@oracle.com4ac36a42016-07-26 15:14:35 -0400983 error = 0;
James Chapmanfd558d12010-04-02 06:18:33 +0000984
James Chapmanfd558d12010-04-02 06:18:33 +0000985 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000986end:
987 return error;
988}
989
990/****************************************************************************
991 * ioctl() handlers.
992 *
993 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
994 * sockets. However, in order to control kernel tunnel features, we allow
995 * userspace to create a special "tunnel" PPPoX socket which is used for
996 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
997 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
998 * calls.
999 ****************************************************************************/
1000
1001static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
1002 struct l2tp_stats *stats)
1003{
Tom Parkin7b7c0712013-03-19 06:11:22 +00001004 dest->tx_packets = atomic_long_read(&stats->tx_packets);
1005 dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
1006 dest->tx_errors = atomic_long_read(&stats->tx_errors);
1007 dest->rx_packets = atomic_long_read(&stats->rx_packets);
1008 dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
1009 dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
1010 dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
1011 dest->rx_errors = atomic_long_read(&stats->rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001012}
1013
1014/* Session ioctl helper.
1015 */
1016static int pppol2tp_session_ioctl(struct l2tp_session *session,
1017 unsigned int cmd, unsigned long arg)
1018{
1019 struct ifreq ifr;
1020 int err = 0;
1021 struct sock *sk;
1022 int val = (int) arg;
1023 struct pppol2tp_session *ps = l2tp_session_priv(session);
1024 struct l2tp_tunnel *tunnel = session->tunnel;
1025 struct pppol2tp_ioc_stats stats;
1026
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001027 l2tp_dbg(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001028 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1029 session->name, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001030
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001031 sk = pppol2tp_session_get_sock(session);
Guillaume Nault5903f592017-10-13 19:22:35 +02001032 if (!sk)
1033 return -EBADR;
1034
James Chapmanfd558d12010-04-02 06:18:33 +00001035 switch (cmd) {
1036 case SIOCGIFMTU:
1037 err = -ENXIO;
1038 if (!(sk->sk_state & PPPOX_CONNECTED))
1039 break;
1040
1041 err = -EFAULT;
1042 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1043 break;
1044 ifr.ifr_mtu = session->mtu;
1045 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1046 break;
1047
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001048 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mtu=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001049 session->name, session->mtu);
James Chapmanfd558d12010-04-02 06:18:33 +00001050 err = 0;
1051 break;
1052
1053 case SIOCSIFMTU:
1054 err = -ENXIO;
1055 if (!(sk->sk_state & PPPOX_CONNECTED))
1056 break;
1057
1058 err = -EFAULT;
1059 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1060 break;
1061
1062 session->mtu = ifr.ifr_mtu;
1063
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001064 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mtu=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001065 session->name, session->mtu);
James Chapmanfd558d12010-04-02 06:18:33 +00001066 err = 0;
1067 break;
1068
1069 case PPPIOCGMRU:
1070 err = -ENXIO;
1071 if (!(sk->sk_state & PPPOX_CONNECTED))
1072 break;
1073
1074 err = -EFAULT;
1075 if (put_user(session->mru, (int __user *) arg))
1076 break;
1077
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001078 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mru=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001079 session->name, session->mru);
James Chapmanfd558d12010-04-02 06:18:33 +00001080 err = 0;
1081 break;
1082
1083 case PPPIOCSMRU:
1084 err = -ENXIO;
1085 if (!(sk->sk_state & PPPOX_CONNECTED))
1086 break;
1087
1088 err = -EFAULT;
1089 if (get_user(val, (int __user *) arg))
1090 break;
1091
1092 session->mru = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001093 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mru=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001094 session->name, session->mru);
James Chapmanfd558d12010-04-02 06:18:33 +00001095 err = 0;
1096 break;
1097
1098 case PPPIOCGFLAGS:
1099 err = -EFAULT;
1100 if (put_user(ps->flags, (int __user *) arg))
1101 break;
1102
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001103 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get flags=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001104 session->name, ps->flags);
James Chapmanfd558d12010-04-02 06:18:33 +00001105 err = 0;
1106 break;
1107
1108 case PPPIOCSFLAGS:
1109 err = -EFAULT;
1110 if (get_user(val, (int __user *) arg))
1111 break;
1112 ps->flags = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001113 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set flags=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001114 session->name, ps->flags);
James Chapmanfd558d12010-04-02 06:18:33 +00001115 err = 0;
1116 break;
1117
1118 case PPPIOCGL2TPSTATS:
1119 err = -ENXIO;
1120 if (!(sk->sk_state & PPPOX_CONNECTED))
1121 break;
1122
1123 memset(&stats, 0, sizeof(stats));
1124 stats.tunnel_id = tunnel->tunnel_id;
1125 stats.session_id = session->session_id;
1126 pppol2tp_copy_stats(&stats, &session->stats);
1127 if (copy_to_user((void __user *) arg, &stats,
1128 sizeof(stats)))
1129 break;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001130 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001131 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001132 err = 0;
1133 break;
1134
1135 default:
1136 err = -ENOSYS;
1137 break;
1138 }
1139
1140 sock_put(sk);
1141
1142 return err;
1143}
1144
1145/* Tunnel ioctl helper.
1146 *
1147 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1148 * specifies a session_id, the session ioctl handler is called. This allows an
1149 * application to retrieve session stats via a tunnel socket.
1150 */
1151static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1152 unsigned int cmd, unsigned long arg)
1153{
1154 int err = 0;
1155 struct sock *sk;
1156 struct pppol2tp_ioc_stats stats;
1157
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001158 l2tp_dbg(tunnel, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001159 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1160 tunnel->name, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001161
1162 sk = tunnel->sock;
1163 sock_hold(sk);
1164
1165 switch (cmd) {
1166 case PPPIOCGL2TPSTATS:
1167 err = -ENXIO;
1168 if (!(sk->sk_state & PPPOX_CONNECTED))
1169 break;
1170
1171 if (copy_from_user(&stats, (void __user *) arg,
1172 sizeof(stats))) {
1173 err = -EFAULT;
1174 break;
1175 }
1176 if (stats.session_id != 0) {
1177 /* resend to session ioctl handler */
1178 struct l2tp_session *session =
Guillaume Nault57377d62017-03-31 13:02:26 +02001179 l2tp_session_get(sock_net(sk), tunnel,
Guillaume Naulta4346212017-10-31 17:36:42 +01001180 stats.session_id);
Guillaume Nault57377d62017-03-31 13:02:26 +02001181
1182 if (session) {
1183 err = pppol2tp_session_ioctl(session, cmd,
1184 arg);
Guillaume Nault57377d62017-03-31 13:02:26 +02001185 l2tp_session_dec_refcount(session);
1186 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001187 err = -EBADR;
Guillaume Nault57377d62017-03-31 13:02:26 +02001188 }
James Chapmanfd558d12010-04-02 06:18:33 +00001189 break;
1190 }
1191#ifdef CONFIG_XFRM
1192 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1193#endif
1194 pppol2tp_copy_stats(&stats, &tunnel->stats);
1195 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1196 err = -EFAULT;
1197 break;
1198 }
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001199 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get L2TP stats\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001200 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001201 err = 0;
1202 break;
1203
1204 default:
1205 err = -ENOSYS;
1206 break;
1207 }
1208
1209 sock_put(sk);
1210
1211 return err;
1212}
1213
1214/* Main ioctl() handler.
1215 * Dispatch to tunnel or session helpers depending on the socket.
1216 */
1217static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1218 unsigned long arg)
1219{
1220 struct sock *sk = sock->sk;
1221 struct l2tp_session *session;
1222 struct l2tp_tunnel *tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001223 int err;
1224
1225 if (!sk)
1226 return 0;
1227
1228 err = -EBADF;
1229 if (sock_flag(sk, SOCK_DEAD) != 0)
1230 goto end;
1231
1232 err = -ENOTCONN;
1233 if ((sk->sk_user_data == NULL) ||
1234 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1235 goto end;
1236
1237 /* Get session context from the socket */
1238 err = -EBADF;
1239 session = pppol2tp_sock_to_session(sk);
1240 if (session == NULL)
1241 goto end;
1242
1243 /* Special case: if session's session_id is zero, treat ioctl as a
1244 * tunnel ioctl
1245 */
James Chapmanfd558d12010-04-02 06:18:33 +00001246 if ((session->session_id == 0) &&
1247 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001248 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001249 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001250 goto end_put_sess;
1251 }
1252
1253 err = pppol2tp_session_ioctl(session, cmd, arg);
1254
1255end_put_sess:
1256 sock_put(sk);
1257end:
1258 return err;
1259}
1260
1261/*****************************************************************************
1262 * setsockopt() / getsockopt() support.
1263 *
1264 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1265 * sockets. In order to control kernel tunnel features, we allow userspace to
1266 * create a special "tunnel" PPPoX socket which is used for control only.
1267 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1268 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1269 *****************************************************************************/
1270
1271/* Tunnel setsockopt() helper.
1272 */
1273static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1274 struct l2tp_tunnel *tunnel,
1275 int optname, int val)
1276{
1277 int err = 0;
1278
1279 switch (optname) {
1280 case PPPOL2TP_SO_DEBUG:
1281 tunnel->debug = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001282 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001283 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001284 break;
1285
1286 default:
1287 err = -ENOPROTOOPT;
1288 break;
1289 }
1290
1291 return err;
1292}
1293
1294/* Session setsockopt helper.
1295 */
1296static int pppol2tp_session_setsockopt(struct sock *sk,
1297 struct l2tp_session *session,
1298 int optname, int val)
1299{
1300 int err = 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001301
1302 switch (optname) {
1303 case PPPOL2TP_SO_RECVSEQ:
1304 if ((val != 0) && (val != 1)) {
1305 err = -EINVAL;
1306 break;
1307 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001308 session->recv_seq = !!val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001309 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001310 "%s: set recv_seq=%d\n",
1311 session->name, session->recv_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001312 break;
1313
1314 case PPPOL2TP_SO_SENDSEQ:
1315 if ((val != 0) && (val != 1)) {
1316 err = -EINVAL;
1317 break;
1318 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001319 session->send_seq = !!val;
James Chapmanfd558d12010-04-02 06:18:33 +00001320 {
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001321 struct pppox_sock *po = pppox_sk(sk);
1322
James Chapmanfd558d12010-04-02 06:18:33 +00001323 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1324 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1325 }
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001326 l2tp_session_set_header_len(session, session->tunnel->version);
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: set send_seq=%d\n",
1329 session->name, session->send_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001330 break;
1331
1332 case PPPOL2TP_SO_LNSMODE:
1333 if ((val != 0) && (val != 1)) {
1334 err = -EINVAL;
1335 break;
1336 }
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +00001337 session->lns_mode = !!val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001338 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001339 "%s: set lns_mode=%d\n",
1340 session->name, session->lns_mode);
James Chapmanfd558d12010-04-02 06:18:33 +00001341 break;
1342
1343 case PPPOL2TP_SO_DEBUG:
1344 session->debug = val;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001345 l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001346 session->name, session->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001347 break;
1348
1349 case PPPOL2TP_SO_REORDERTO:
1350 session->reorder_timeout = msecs_to_jiffies(val);
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: set reorder_timeout=%d\n",
1353 session->name, session->reorder_timeout);
James Chapmanfd558d12010-04-02 06:18:33 +00001354 break;
1355
1356 default:
1357 err = -ENOPROTOOPT;
1358 break;
1359 }
1360
1361 return err;
1362}
1363
1364/* Main setsockopt() entry point.
1365 * Does API checks, then calls either the tunnel or session setsockopt
1366 * handler, according to whether the PPPoL2TP socket is a for a regular
1367 * session or the special tunnel type.
1368 */
1369static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1370 char __user *optval, unsigned int optlen)
1371{
1372 struct sock *sk = sock->sk;
1373 struct l2tp_session *session;
1374 struct l2tp_tunnel *tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001375 int val;
1376 int err;
1377
1378 if (level != SOL_PPPOL2TP)
Sasha Levin3cf521f2014-07-14 17:02:31 -07001379 return -EINVAL;
James Chapmanfd558d12010-04-02 06:18:33 +00001380
1381 if (optlen < sizeof(int))
1382 return -EINVAL;
1383
1384 if (get_user(val, (int __user *)optval))
1385 return -EFAULT;
1386
1387 err = -ENOTCONN;
1388 if (sk->sk_user_data == NULL)
1389 goto end;
1390
1391 /* Get session context from the socket */
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
1398 */
James Chapmanfd558d12010-04-02 06:18:33 +00001399 if ((session->session_id == 0) &&
1400 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001401 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001402 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
Guillaume Nault7198c772017-11-11 06:06:31 +09001403 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001404 err = pppol2tp_session_setsockopt(sk, session, optname, val);
Guillaume Nault7198c772017-11-11 06:06:31 +09001405 }
James Chapmanfd558d12010-04-02 06:18:33 +00001406
James Chapmanfd558d12010-04-02 06:18:33 +00001407 sock_put(sk);
1408end:
1409 return err;
1410}
1411
1412/* Tunnel getsockopt helper. Called with sock locked.
1413 */
1414static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1415 struct l2tp_tunnel *tunnel,
1416 int optname, int *val)
1417{
1418 int err = 0;
1419
1420 switch (optname) {
1421 case PPPOL2TP_SO_DEBUG:
1422 *val = tunnel->debug;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001423 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001424 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001425 break;
1426
1427 default:
1428 err = -ENOPROTOOPT;
1429 break;
1430 }
1431
1432 return err;
1433}
1434
1435/* Session getsockopt helper. Called with sock locked.
1436 */
1437static int pppol2tp_session_getsockopt(struct sock *sk,
1438 struct l2tp_session *session,
1439 int optname, int *val)
1440{
1441 int err = 0;
1442
1443 switch (optname) {
1444 case PPPOL2TP_SO_RECVSEQ:
1445 *val = session->recv_seq;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001446 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001447 "%s: get recv_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001448 break;
1449
1450 case PPPOL2TP_SO_SENDSEQ:
1451 *val = session->send_seq;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001452 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001453 "%s: get send_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001454 break;
1455
1456 case PPPOL2TP_SO_LNSMODE:
1457 *val = session->lns_mode;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001458 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001459 "%s: get lns_mode=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001460 break;
1461
1462 case PPPOL2TP_SO_DEBUG:
1463 *val = session->debug;
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001464 l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001465 session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001466 break;
1467
1468 case PPPOL2TP_SO_REORDERTO:
1469 *val = (int) jiffies_to_msecs(session->reorder_timeout);
Asbjørn Sloth Tønnesenfba40c62016-12-11 00:18:59 +00001470 l2tp_info(session, L2TP_MSG_CONTROL,
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001471 "%s: get reorder_timeout=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001472 break;
1473
1474 default:
1475 err = -ENOPROTOOPT;
1476 }
1477
1478 return err;
1479}
1480
1481/* Main getsockopt() entry point.
1482 * Does API checks, then calls either the tunnel or session getsockopt
1483 * handler, according to whether the PPPoX socket is a for a regular session
1484 * or the special tunnel type.
1485 */
Joe Perchese3192692012-06-03 17:41:40 +00001486static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1487 char __user *optval, int __user *optlen)
James Chapmanfd558d12010-04-02 06:18:33 +00001488{
1489 struct sock *sk = sock->sk;
1490 struct l2tp_session *session;
1491 struct l2tp_tunnel *tunnel;
1492 int val, len;
1493 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001494
1495 if (level != SOL_PPPOL2TP)
Sasha Levin3cf521f2014-07-14 17:02:31 -07001496 return -EINVAL;
James Chapmanfd558d12010-04-02 06:18:33 +00001497
Joe Perchese3192692012-06-03 17:41:40 +00001498 if (get_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001499 return -EFAULT;
1500
1501 len = min_t(unsigned int, len, sizeof(int));
1502
1503 if (len < 0)
1504 return -EINVAL;
1505
1506 err = -ENOTCONN;
1507 if (sk->sk_user_data == NULL)
1508 goto end;
1509
1510 /* Get the session context */
1511 err = -EBADF;
1512 session = pppol2tp_sock_to_session(sk);
1513 if (session == NULL)
1514 goto end;
1515
1516 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
James Chapmanfd558d12010-04-02 06:18:33 +00001517 if ((session->session_id == 0) &&
1518 (session->peer_session_id == 0)) {
Guillaume Nault7198c772017-11-11 06:06:31 +09001519 tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001520 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
Guillaume Nault321a52a2017-04-06 18:31:21 +02001521 if (err)
1522 goto end_put_sess;
1523 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001524 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
Guillaume Nault321a52a2017-04-06 18:31:21 +02001525 if (err)
1526 goto end_put_sess;
1527 }
James Chapmanfd558d12010-04-02 06:18:33 +00001528
1529 err = -EFAULT;
Joe Perchese3192692012-06-03 17:41:40 +00001530 if (put_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001531 goto end_put_sess;
1532
1533 if (copy_to_user((void __user *) optval, &val, len))
1534 goto end_put_sess;
1535
1536 err = 0;
1537
1538end_put_sess:
1539 sock_put(sk);
1540end:
1541 return err;
1542}
1543
1544/*****************************************************************************
1545 * /proc filesystem for debug
James Chapmanf7faffa2010-04-02 06:18:49 +00001546 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1547 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
James Chapmanfd558d12010-04-02 06:18:33 +00001548 *****************************************************************************/
1549
1550static unsigned int pppol2tp_net_id;
1551
1552#ifdef CONFIG_PROC_FS
1553
1554struct pppol2tp_seq_data {
1555 struct seq_net_private p;
1556 int tunnel_idx; /* current tunnel */
1557 int session_idx; /* index of session within current tunnel */
1558 struct l2tp_tunnel *tunnel;
1559 struct l2tp_session *session; /* NULL means get next tunnel */
1560};
1561
1562static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1563{
James Chapmanf7faffa2010-04-02 06:18:49 +00001564 for (;;) {
1565 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1566 pd->tunnel_idx++;
1567
1568 if (pd->tunnel == NULL)
1569 break;
1570
1571 /* Ignore L2TPv3 tunnels */
1572 if (pd->tunnel->version < 3)
1573 break;
1574 }
James Chapmanfd558d12010-04-02 06:18:33 +00001575}
1576
1577static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1578{
Guillaume Naulta4346212017-10-31 17:36:42 +01001579 pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx);
James Chapmanfd558d12010-04-02 06:18:33 +00001580 pd->session_idx++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001581
James Chapmanfd558d12010-04-02 06:18:33 +00001582 if (pd->session == NULL) {
1583 pd->session_idx = 0;
1584 pppol2tp_next_tunnel(net, pd);
1585 }
1586}
1587
1588static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1589{
1590 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1591 loff_t pos = *offs;
1592 struct net *net;
1593
1594 if (!pos)
1595 goto out;
1596
1597 BUG_ON(m->private == NULL);
1598 pd = m->private;
1599 net = seq_file_net(m);
1600
1601 if (pd->tunnel == NULL)
1602 pppol2tp_next_tunnel(net, pd);
1603 else
1604 pppol2tp_next_session(net, pd);
1605
1606 /* NULL tunnel and session indicates end of list */
1607 if ((pd->tunnel == NULL) && (pd->session == NULL))
1608 pd = NULL;
1609
1610out:
1611 return pd;
1612}
1613
1614static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1615{
1616 (*pos)++;
1617 return NULL;
1618}
1619
1620static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1621{
1622 /* nothing to do */
1623}
1624
1625static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1626{
1627 struct l2tp_tunnel *tunnel = v;
1628
1629 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1630 tunnel->name,
1631 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001632 refcount_read(&tunnel->ref_count) - 1);
Tom Parkin7b7c0712013-03-19 06:11:22 +00001633 seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001634 tunnel->debug,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001635 atomic_long_read(&tunnel->stats.tx_packets),
1636 atomic_long_read(&tunnel->stats.tx_bytes),
1637 atomic_long_read(&tunnel->stats.tx_errors),
1638 atomic_long_read(&tunnel->stats.rx_packets),
1639 atomic_long_read(&tunnel->stats.rx_bytes),
1640 atomic_long_read(&tunnel->stats.rx_errors));
James Chapmanfd558d12010-04-02 06:18:33 +00001641}
1642
1643static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1644{
1645 struct l2tp_session *session = v;
1646 struct l2tp_tunnel *tunnel = session->tunnel;
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001647 unsigned char state;
1648 char user_data_ok;
1649 struct sock *sk;
James Chapmanfd558d12010-04-02 06:18:33 +00001650 u32 ip = 0;
1651 u16 port = 0;
1652
1653 if (tunnel->sock) {
1654 struct inet_sock *inet = inet_sk(tunnel->sock);
1655 ip = ntohl(inet->inet_saddr);
1656 port = ntohs(inet->inet_sport);
1657 }
1658
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001659 sk = pppol2tp_session_get_sock(session);
1660 if (sk) {
1661 state = sk->sk_state;
1662 user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N';
1663 } else {
1664 state = 0;
1665 user_data_ok = 'N';
1666 }
1667
James Chapmanfd558d12010-04-02 06:18:33 +00001668 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1669 "%04X/%04X %d %c\n",
1670 session->name, ip, port,
1671 tunnel->tunnel_id,
1672 session->session_id,
1673 tunnel->peer_tunnel_id,
1674 session->peer_session_id,
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001675 state, user_data_ok);
James Chapmanfd558d12010-04-02 06:18:33 +00001676 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1677 session->mtu, session->mru,
1678 session->recv_seq ? 'R' : '-',
1679 session->send_seq ? 'S' : '-',
1680 session->lns_mode ? "LNS" : "LAC",
1681 session->debug,
1682 jiffies_to_msecs(session->reorder_timeout));
Tom Parkin7b7c0712013-03-19 06:11:22 +00001683 seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001684 session->nr, session->ns,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001685 atomic_long_read(&session->stats.tx_packets),
1686 atomic_long_read(&session->stats.tx_bytes),
1687 atomic_long_read(&session->stats.tx_errors),
1688 atomic_long_read(&session->stats.rx_packets),
1689 atomic_long_read(&session->stats.rx_bytes),
1690 atomic_long_read(&session->stats.rx_errors));
James Chapman93454712010-04-02 06:18:44 +00001691
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001692 if (sk) {
1693 struct pppox_sock *po = pppox_sk(sk);
1694
James Chapman93454712010-04-02 06:18:44 +00001695 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
Guillaume Naultee40fb2e2017-10-27 16:51:52 +02001696 sock_put(sk);
1697 }
James Chapmanfd558d12010-04-02 06:18:33 +00001698}
1699
1700static int pppol2tp_seq_show(struct seq_file *m, void *v)
1701{
1702 struct pppol2tp_seq_data *pd = v;
1703
1704 /* display header on line 1 */
1705 if (v == SEQ_START_TOKEN) {
1706 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1707 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1708 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1709 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1710 "dest-tid/sid state user-data-ok\n");
1711 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1712 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1713 goto out;
1714 }
1715
1716 /* Show the tunnel or session context.
1717 */
Guillaume Naulte08293a2017-04-03 12:03:13 +02001718 if (!pd->session) {
James Chapmanfd558d12010-04-02 06:18:33 +00001719 pppol2tp_seq_tunnel_show(m, pd->tunnel);
Guillaume Naulte08293a2017-04-03 12:03:13 +02001720 } else {
James Chapmanfd558d12010-04-02 06:18:33 +00001721 pppol2tp_seq_session_show(m, pd->session);
Guillaume Naulte08293a2017-04-03 12:03:13 +02001722 l2tp_session_dec_refcount(pd->session);
1723 }
James Chapmanfd558d12010-04-02 06:18:33 +00001724
1725out:
1726 return 0;
1727}
1728
1729static const struct seq_operations pppol2tp_seq_ops = {
1730 .start = pppol2tp_seq_start,
1731 .next = pppol2tp_seq_next,
1732 .stop = pppol2tp_seq_stop,
1733 .show = pppol2tp_seq_show,
1734};
1735
1736/* Called when our /proc file is opened. We allocate data for use when
1737 * iterating our tunnel / session contexts and store it in the private
1738 * data of the seq_file.
1739 */
1740static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1741{
1742 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1743 sizeof(struct pppol2tp_seq_data));
1744}
1745
1746static const struct file_operations pppol2tp_proc_fops = {
1747 .owner = THIS_MODULE,
1748 .open = pppol2tp_proc_open,
1749 .read = seq_read,
1750 .llseek = seq_lseek,
1751 .release = seq_release_net,
1752};
1753
1754#endif /* CONFIG_PROC_FS */
1755
1756/*****************************************************************************
1757 * Network namespace
1758 *****************************************************************************/
1759
1760static __net_init int pppol2tp_init_net(struct net *net)
1761{
1762 struct proc_dir_entry *pde;
1763 int err = 0;
1764
Gao fengd4beaa62013-02-18 01:34:54 +00001765 pde = proc_create("pppol2tp", S_IRUGO, net->proc_net,
1766 &pppol2tp_proc_fops);
James Chapmanfd558d12010-04-02 06:18:33 +00001767 if (!pde) {
1768 err = -ENOMEM;
1769 goto out;
1770 }
1771
1772out:
1773 return err;
1774}
1775
1776static __net_exit void pppol2tp_exit_net(struct net *net)
1777{
Gao fengece31ff2013-02-18 01:34:56 +00001778 remove_proc_entry("pppol2tp", net->proc_net);
James Chapmanfd558d12010-04-02 06:18:33 +00001779}
1780
1781static struct pernet_operations pppol2tp_net_ops = {
1782 .init = pppol2tp_init_net,
1783 .exit = pppol2tp_exit_net,
1784 .id = &pppol2tp_net_id,
1785};
1786
1787/*****************************************************************************
1788 * Init and cleanup
1789 *****************************************************************************/
1790
1791static const struct proto_ops pppol2tp_ops = {
1792 .family = AF_PPPOX,
1793 .owner = THIS_MODULE,
1794 .release = pppol2tp_release,
1795 .bind = sock_no_bind,
1796 .connect = pppol2tp_connect,
1797 .socketpair = sock_no_socketpair,
1798 .accept = sock_no_accept,
1799 .getname = pppol2tp_getname,
1800 .poll = datagram_poll,
1801 .listen = sock_no_listen,
1802 .shutdown = sock_no_shutdown,
1803 .setsockopt = pppol2tp_setsockopt,
1804 .getsockopt = pppol2tp_getsockopt,
1805 .sendmsg = pppol2tp_sendmsg,
1806 .recvmsg = pppol2tp_recvmsg,
1807 .mmap = sock_no_mmap,
1808 .ioctl = pppox_ioctl,
1809};
1810
Eric Dumazet756e64a2010-09-21 06:43:54 +00001811static const struct pppox_proto pppol2tp_proto = {
James Chapmanfd558d12010-04-02 06:18:33 +00001812 .create = pppol2tp_create,
Wei Yongjune1558a92013-07-02 09:02:07 +08001813 .ioctl = pppol2tp_ioctl,
1814 .owner = THIS_MODULE,
James Chapmanfd558d12010-04-02 06:18:33 +00001815};
1816
James Chapman309795f2010-04-02 06:19:10 +00001817#ifdef CONFIG_L2TP_V3
1818
1819static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1820 .session_create = pppol2tp_session_create,
Tom Parkincf2f5c82013-03-19 06:11:21 +00001821 .session_delete = l2tp_session_delete,
James Chapman309795f2010-04-02 06:19:10 +00001822};
1823
1824#endif /* CONFIG_L2TP_V3 */
1825
James Chapmanfd558d12010-04-02 06:18:33 +00001826static int __init pppol2tp_init(void)
1827{
1828 int err;
1829
1830 err = register_pernet_device(&pppol2tp_net_ops);
1831 if (err)
1832 goto out;
1833
1834 err = proto_register(&pppol2tp_sk_proto, 0);
1835 if (err)
1836 goto out_unregister_pppol2tp_pernet;
1837
1838 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1839 if (err)
1840 goto out_unregister_pppol2tp_proto;
1841
James Chapman309795f2010-04-02 06:19:10 +00001842#ifdef CONFIG_L2TP_V3
1843 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1844 if (err)
1845 goto out_unregister_pppox;
1846#endif
1847
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001848 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001849
1850out:
1851 return err;
James Chapman309795f2010-04-02 06:19:10 +00001852
1853#ifdef CONFIG_L2TP_V3
1854out_unregister_pppox:
1855 unregister_pppox_proto(PX_PROTO_OL2TP);
1856#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001857out_unregister_pppol2tp_proto:
1858 proto_unregister(&pppol2tp_sk_proto);
1859out_unregister_pppol2tp_pernet:
1860 unregister_pernet_device(&pppol2tp_net_ops);
1861 goto out;
1862}
1863
1864static void __exit pppol2tp_exit(void)
1865{
James Chapman309795f2010-04-02 06:19:10 +00001866#ifdef CONFIG_L2TP_V3
1867 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1868#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001869 unregister_pppox_proto(PX_PROTO_OL2TP);
1870 proto_unregister(&pppol2tp_sk_proto);
1871 unregister_pernet_device(&pppol2tp_net_ops);
1872}
1873
1874module_init(pppol2tp_init);
1875module_exit(pppol2tp_exit);
1876
1877MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1878MODULE_DESCRIPTION("PPP over L2TP over UDP");
1879MODULE_LICENSE("GPL");
1880MODULE_VERSION(PPPOL2TP_DRV_VERSION);
Guillaume Nault681b4d82015-12-02 16:27:39 +01001881MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP);
Guillaume Nault249ee812017-04-03 13:23:15 +02001882MODULE_ALIAS_L2TP_PWTYPE(7);