blob: 637a341c1e2d1a466c68efcb50d7fc201198a15b [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
125 struct sock *sock; /* Pointer to the session
126 * PPPoX socket */
127 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
128 * socket */
129 int flags; /* accessed by PPPIOCGFLAGS.
130 * Unused. */
131};
132
133static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
134
stephen hemmingerd7100da2010-08-04 07:34:36 +0000135static const struct ppp_channel_ops pppol2tp_chan_ops = {
136 .start_xmit = pppol2tp_xmit,
137};
138
James Chapmanfd558d12010-04-02 06:18:33 +0000139static const struct proto_ops pppol2tp_ops;
140
141/* Helpers to obtain tunnel/session contexts from sockets.
142 */
143static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
144{
145 struct l2tp_session *session;
146
147 if (sk == NULL)
148 return NULL;
149
150 sock_hold(sk);
151 session = (struct l2tp_session *)(sk->sk_user_data);
152 if (session == NULL) {
153 sock_put(sk);
154 goto out;
155 }
156
157 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
158
159out:
160 return session;
161}
162
163/*****************************************************************************
164 * Receive data handling
165 *****************************************************************************/
166
167static int pppol2tp_recv_payload_hook(struct sk_buff *skb)
168{
169 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
170 * don't send the PPP header (PPP header compression enabled), but
171 * other clients can include the header. So we cope with both cases
172 * here. The PPP header is always FF03 when using L2TP.
173 *
174 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
175 * the field may be unaligned.
176 */
177 if (!pskb_may_pull(skb, 2))
178 return 1;
179
180 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
181 skb_pull(skb, 2);
182
183 return 0;
184}
185
186/* Receive message. This is the recvmsg for the PPPoL2TP socket.
187 */
188static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
189 struct msghdr *msg, size_t len,
190 int flags)
191{
192 int err;
193 struct sk_buff *skb;
194 struct sock *sk = sock->sk;
195
196 err = -EIO;
197 if (sk->sk_state & PPPOX_BOUND)
198 goto end;
199
200 msg->msg_namelen = 0;
201
202 err = 0;
203 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
204 flags & MSG_DONTWAIT, &err);
205 if (!skb)
206 goto end;
207
208 if (len > skb->len)
209 len = skb->len;
210 else if (len < skb->len)
211 msg->msg_flags |= MSG_TRUNC;
212
213 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
214 if (likely(err == 0))
215 err = len;
216
217 kfree_skb(skb);
218end:
219 return err;
220}
221
222static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
223{
224 struct pppol2tp_session *ps = l2tp_session_priv(session);
225 struct sock *sk = NULL;
226
227 /* If the socket is bound, send it in to PPP's input queue. Otherwise
228 * queue it on the session socket.
229 */
230 sk = ps->sock;
231 if (sk == NULL)
232 goto no_sock;
233
234 if (sk->sk_state & PPPOX_BOUND) {
235 struct pppox_sock *po;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000236 l2tp_dbg(session, PPPOL2TP_MSG_DATA,
237 "%s: recv %d byte data frame, passing to ppp\n",
238 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000239
240 /* We need to forget all info related to the L2TP packet
241 * gathered in the skb as we are going to reuse the same
242 * skb for the inner packet.
243 * Namely we need to:
244 * - reset xfrm (IPSec) information as it applies to
245 * the outer L2TP packet and not to the inner one
246 * - release the dst to force a route lookup on the inner
247 * IP packet since skb->dst currently points to the dst
248 * of the UDP tunnel
249 * - reset netfilter information as it doesn't apply
250 * to the inner packet either
251 */
252 secpath_reset(skb);
253 skb_dst_drop(skb);
254 nf_reset(skb);
255
256 po = pppox_sk(sk);
257 ppp_input(&po->chan, skb);
258 } else {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000259 l2tp_info(session, PPPOL2TP_MSG_DATA, "%s: socket not bound\n",
260 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000261
262 /* Not bound. Nothing we can do, so discard. */
Tom Parkin7b7c0712013-03-19 06:11:22 +0000263 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000264 kfree_skb(skb);
265 }
266
267 return;
268
269no_sock:
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000270 l2tp_info(session, PPPOL2TP_MSG_DATA, "%s: no socket\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000271 kfree_skb(skb);
272}
273
274static void pppol2tp_session_sock_hold(struct l2tp_session *session)
275{
276 struct pppol2tp_session *ps = l2tp_session_priv(session);
277
278 if (ps->sock)
279 sock_hold(ps->sock);
280}
281
282static void pppol2tp_session_sock_put(struct l2tp_session *session)
283{
284 struct pppol2tp_session *ps = l2tp_session_priv(session);
285
286 if (ps->sock)
287 sock_put(ps->sock);
288}
289
290/************************************************************************
291 * Transmit handling
292 ***********************************************************************/
293
James Chapmanfd558d12010-04-02 06:18:33 +0000294/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
295 * when a user application does a sendmsg() on the session socket. L2TP and
296 * PPP headers must be inserted into the user's data.
297 */
298static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
299 size_t total_len)
300{
301 static const unsigned char ppph[2] = { 0xff, 0x03 };
302 struct sock *sk = sock->sk;
303 struct sk_buff *skb;
304 int error;
305 struct l2tp_session *session;
306 struct l2tp_tunnel *tunnel;
307 struct pppol2tp_session *ps;
James Chapman0d767512010-04-02 06:19:00 +0000308 int uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +0000309
310 error = -ENOTCONN;
311 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
312 goto error;
313
314 /* Get session and tunnel contexts */
315 error = -EBADF;
316 session = pppol2tp_sock_to_session(sk);
317 if (session == NULL)
318 goto error;
319
320 ps = l2tp_session_priv(session);
321 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
322 if (tunnel == NULL)
323 goto error_put_sess;
324
James Chapman0d767512010-04-02 06:19:00 +0000325 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
326
James Chapmanfd558d12010-04-02 06:18:33 +0000327 /* Allocate a socket buffer */
328 error = -ENOMEM;
329 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +0000330 uhlen + session->hdr_len +
James Chapmanfd558d12010-04-02 06:18:33 +0000331 sizeof(ppph) + total_len,
332 0, GFP_KERNEL);
333 if (!skb)
334 goto error_put_sess_tun;
335
336 /* Reserve space for headers. */
337 skb_reserve(skb, NET_SKB_PAD);
338 skb_reset_network_header(skb);
339 skb_reserve(skb, sizeof(struct iphdr));
340 skb_reset_transport_header(skb);
James Chapman0d767512010-04-02 06:19:00 +0000341 skb_reserve(skb, uhlen);
James Chapmanfd558d12010-04-02 06:18:33 +0000342
343 /* Add PPP header */
344 skb->data[0] = ppph[0];
345 skb->data[1] = ppph[1];
346 skb_put(skb, 2);
347
348 /* Copy user data into skb */
349 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
350 if (error < 0) {
351 kfree_skb(skb);
352 goto error_put_sess_tun;
353 }
354 skb_put(skb, total_len);
355
356 l2tp_xmit_skb(session, skb, session->hdr_len);
357
358 sock_put(ps->tunnel_sock);
Guillaume Nault8b82547e33e2013-03-01 05:02:02 +0000359 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000360
361 return error;
362
363error_put_sess_tun:
364 sock_put(ps->tunnel_sock);
365error_put_sess:
366 sock_put(sk);
367error:
368 return error;
369}
370
371/* Transmit function called by generic PPP driver. Sends PPP frame
372 * over PPPoL2TP socket.
373 *
374 * This is almost the same as pppol2tp_sendmsg(), but rather than
375 * being called with a msghdr from userspace, it is called with a skb
376 * from the kernel.
377 *
378 * The supplied skb from ppp doesn't have enough headroom for the
379 * insertion of L2TP, UDP and IP headers so we need to allocate more
380 * headroom in the skb. This will create a cloned skb. But we must be
381 * careful in the error case because the caller will expect to free
382 * the skb it supplied, not our cloned skb. So we take care to always
383 * leave the original skb unfreed if we return an error.
384 */
385static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
386{
387 static const u8 ppph[2] = { 0xff, 0x03 };
388 struct sock *sk = (struct sock *) chan->private;
389 struct sock *sk_tun;
James Chapmanfd558d12010-04-02 06:18:33 +0000390 struct l2tp_session *session;
391 struct l2tp_tunnel *tunnel;
392 struct pppol2tp_session *ps;
Eric Dumazet09df57c2011-10-07 05:45:57 +0000393 int uhlen, headroom;
James Chapmanfd558d12010-04-02 06:18:33 +0000394
395 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
396 goto abort;
397
398 /* Get session and tunnel contexts from the socket */
399 session = pppol2tp_sock_to_session(sk);
400 if (session == NULL)
401 goto abort;
402
403 ps = l2tp_session_priv(session);
404 sk_tun = ps->tunnel_sock;
405 if (sk_tun == NULL)
406 goto abort_put_sess;
407 tunnel = l2tp_sock_to_tunnel(sk_tun);
408 if (tunnel == NULL)
409 goto abort_put_sess;
410
Eric Dumazet09df57c2011-10-07 05:45:57 +0000411 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
412 headroom = NET_SKB_PAD +
413 sizeof(struct iphdr) + /* IP header */
414 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
415 session->hdr_len + /* L2TP header */
416 sizeof(ppph); /* PPP header */
417 if (skb_cow_head(skb, headroom))
James Chapmanfd558d12010-04-02 06:18:33 +0000418 goto abort_put_sess_tun;
419
James Chapmanfd558d12010-04-02 06:18:33 +0000420 /* Setup PPP header */
421 __skb_push(skb, sizeof(ppph));
422 skb->data[0] = ppph[0];
423 skb->data[1] = ppph[1];
424
James Chapmane0d44352010-04-02 06:18:54 +0000425 l2tp_xmit_skb(session, skb, session->hdr_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000426
427 sock_put(sk_tun);
428 sock_put(sk);
429 return 1;
430
431abort_put_sess_tun:
432 sock_put(sk_tun);
433abort_put_sess:
434 sock_put(sk);
435abort:
436 /* Free the original skb */
437 kfree_skb(skb);
438 return 1;
439}
440
441/*****************************************************************************
442 * Session (and tunnel control) socket create/destroy.
443 *****************************************************************************/
444
445/* Called by l2tp_core when a session socket is being closed.
446 */
447static void pppol2tp_session_close(struct l2tp_session *session)
448{
449 struct pppol2tp_session *ps = l2tp_session_priv(session);
450 struct sock *sk = ps->sock;
Tom Parkincf2f5c82013-03-19 06:11:21 +0000451 struct socket *sock = sk->sk_socket;
James Chapmanfd558d12010-04-02 06:18:33 +0000452
453 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
454
James Chapmanfd558d12010-04-02 06:18:33 +0000455
Tom Parkincf2f5c82013-03-19 06:11:21 +0000456 if (sock) {
457 inet_shutdown(sock, 2);
458 /* Don't let the session go away before our socket does */
459 l2tp_session_inc_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000460 }
James Chapmanfd558d12010-04-02 06:18:33 +0000461 return;
462}
463
464/* Really kill the session socket. (Called from sock_put() if
465 * refcnt == 0.)
466 */
467static void pppol2tp_session_destruct(struct sock *sk)
468{
Tom Parkinf6e16b22013-03-19 06:11:23 +0000469 struct l2tp_session *session = sk->sk_user_data;
470 if (session) {
James Chapmanfd558d12010-04-02 06:18:33 +0000471 sk->sk_user_data = NULL;
472 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
473 l2tp_session_dec_refcount(session);
474 }
James Chapmanfd558d12010-04-02 06:18:33 +0000475 return;
476}
477
478/* Called when the PPPoX socket (session) is closed.
479 */
480static int pppol2tp_release(struct socket *sock)
481{
482 struct sock *sk = sock->sk;
483 struct l2tp_session *session;
484 int error;
485
486 if (!sk)
487 return 0;
488
489 error = -EBADF;
490 lock_sock(sk);
491 if (sock_flag(sk, SOCK_DEAD) != 0)
492 goto error;
493
494 pppox_unbind_sock(sk);
495
496 /* Signal the death of the socket. */
497 sk->sk_state = PPPOX_DEAD;
498 sock_orphan(sk);
499 sock->sk = NULL;
500
501 session = pppol2tp_sock_to_session(sk);
502
503 /* Purge any queued data */
James Chapmanfd558d12010-04-02 06:18:33 +0000504 if (session != NULL) {
Tom Parkinf6e16b22013-03-19 06:11:23 +0000505 __l2tp_session_unhash(session);
Tom Parkincf2f5c82013-03-19 06:11:21 +0000506 l2tp_session_queue_purge(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000507 sock_put(sk);
508 }
Tom Parkincf2f5c82013-03-19 06:11:21 +0000509 skb_queue_purge(&sk->sk_receive_queue);
510 skb_queue_purge(&sk->sk_write_queue);
James Chapmanfd558d12010-04-02 06:18:33 +0000511
512 release_sock(sk);
513
514 /* This will delete the session context via
515 * pppol2tp_session_destruct() if the socket's refcnt drops to
516 * zero.
517 */
518 sock_put(sk);
519
520 return 0;
521
522error:
523 release_sock(sk);
524 return error;
525}
526
527static struct proto pppol2tp_sk_proto = {
528 .name = "PPPOL2TP",
529 .owner = THIS_MODULE,
530 .obj_size = sizeof(struct pppox_sock),
531};
532
533static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
534{
535 int rc;
536
537 rc = l2tp_udp_encap_recv(sk, skb);
538 if (rc)
539 kfree_skb(skb);
540
541 return NET_RX_SUCCESS;
542}
543
544/* socket() handler. Initialize a new struct sock.
545 */
546static int pppol2tp_create(struct net *net, struct socket *sock)
547{
548 int error = -ENOMEM;
549 struct sock *sk;
550
551 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
552 if (!sk)
553 goto out;
554
555 sock_init_data(sock, sk);
556
557 sock->state = SS_UNCONNECTED;
558 sock->ops = &pppol2tp_ops;
559
560 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
561 sk->sk_protocol = PX_PROTO_OL2TP;
562 sk->sk_family = PF_PPPOX;
563 sk->sk_state = PPPOX_NONE;
564 sk->sk_type = SOCK_STREAM;
565 sk->sk_destruct = pppol2tp_session_destruct;
566
567 error = 0;
568
569out:
570 return error;
571}
572
David S. Millerf66ef2d2010-04-03 15:01:37 -0700573#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
James Chapman0ad66142010-04-02 06:19:33 +0000574static void pppol2tp_show(struct seq_file *m, void *arg)
575{
576 struct l2tp_session *session = arg;
577 struct pppol2tp_session *ps = l2tp_session_priv(session);
578
579 if (ps) {
580 struct pppox_sock *po = pppox_sk(ps->sock);
581 if (po)
582 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
583 }
584}
585#endif
586
James Chapmanfd558d12010-04-02 06:18:33 +0000587/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
588 */
589static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
590 int sockaddr_len, int flags)
591{
592 struct sock *sk = sock->sk;
593 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
594 struct pppox_sock *po = pppox_sk(sk);
595 struct l2tp_session *session = NULL;
596 struct l2tp_tunnel *tunnel;
597 struct pppol2tp_session *ps;
598 struct dst_entry *dst;
599 struct l2tp_session_cfg cfg = { 0, };
600 int error = 0;
James Chapmane0d44352010-04-02 06:18:54 +0000601 u32 tunnel_id, peer_tunnel_id;
602 u32 session_id, peer_session_id;
603 int ver = 2;
604 int fd;
James Chapmanfd558d12010-04-02 06:18:33 +0000605
606 lock_sock(sk);
607
608 error = -EINVAL;
609 if (sp->sa_protocol != PX_PROTO_OL2TP)
610 goto end;
611
612 /* Check for already bound sockets */
613 error = -EBUSY;
614 if (sk->sk_state & PPPOX_CONNECTED)
615 goto end;
616
617 /* We don't supporting rebinding anyway */
618 error = -EALREADY;
619 if (sk->sk_user_data)
620 goto end; /* socket is already attached */
621
James Chapmanb79585f2012-04-29 21:48:50 +0000622 /* Get params from socket address. Handle L2TPv2 and L2TPv3.
623 * This is nasty because there are different sockaddr_pppol2tp
624 * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use
625 * the sockaddr size to determine which structure the caller
626 * is using.
627 */
628 peer_tunnel_id = 0;
James Chapmane0d44352010-04-02 06:18:54 +0000629 if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
630 fd = sp->pppol2tp.fd;
631 tunnel_id = sp->pppol2tp.s_tunnel;
632 peer_tunnel_id = sp->pppol2tp.d_tunnel;
633 session_id = sp->pppol2tp.s_session;
634 peer_session_id = sp->pppol2tp.d_session;
635 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
James Chapmanb79585f2012-04-29 21:48:50 +0000636 struct sockaddr_pppol2tpv3 *sp3 =
637 (struct sockaddr_pppol2tpv3 *) sp;
James Chapmane0d44352010-04-02 06:18:54 +0000638 ver = 3;
639 fd = sp3->pppol2tp.fd;
640 tunnel_id = sp3->pppol2tp.s_tunnel;
641 peer_tunnel_id = sp3->pppol2tp.d_tunnel;
642 session_id = sp3->pppol2tp.s_session;
643 peer_session_id = sp3->pppol2tp.d_session;
James Chapmanb79585f2012-04-29 21:48:50 +0000644 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) {
645 struct sockaddr_pppol2tpin6 *sp6 =
646 (struct sockaddr_pppol2tpin6 *) sp;
647 fd = sp6->pppol2tp.fd;
648 tunnel_id = sp6->pppol2tp.s_tunnel;
649 peer_tunnel_id = sp6->pppol2tp.d_tunnel;
650 session_id = sp6->pppol2tp.s_session;
651 peer_session_id = sp6->pppol2tp.d_session;
652 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) {
653 struct sockaddr_pppol2tpv3in6 *sp6 =
654 (struct sockaddr_pppol2tpv3in6 *) sp;
655 ver = 3;
656 fd = sp6->pppol2tp.fd;
657 tunnel_id = sp6->pppol2tp.s_tunnel;
658 peer_tunnel_id = sp6->pppol2tp.d_tunnel;
659 session_id = sp6->pppol2tp.s_session;
660 peer_session_id = sp6->pppol2tp.d_session;
James Chapmane0d44352010-04-02 06:18:54 +0000661 } else {
662 error = -EINVAL;
663 goto end; /* bad socket address */
664 }
665
666 /* Don't bind if tunnel_id is 0 */
James Chapmanfd558d12010-04-02 06:18:33 +0000667 error = -EINVAL;
James Chapmane0d44352010-04-02 06:18:54 +0000668 if (tunnel_id == 0)
James Chapmanfd558d12010-04-02 06:18:33 +0000669 goto end;
670
James Chapman309795f2010-04-02 06:19:10 +0000671 tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id);
672
James Chapmane0d44352010-04-02 06:18:54 +0000673 /* Special case: create tunnel context if session_id and
674 * peer_session_id is 0. Otherwise look up tunnel using supplied
James Chapmanfd558d12010-04-02 06:18:33 +0000675 * tunnel id.
676 */
James Chapmane0d44352010-04-02 06:18:54 +0000677 if ((session_id == 0) && (peer_session_id == 0)) {
James Chapman309795f2010-04-02 06:19:10 +0000678 if (tunnel == NULL) {
679 struct l2tp_tunnel_cfg tcfg = {
680 .encap = L2TP_ENCAPTYPE_UDP,
681 .debug = 0,
682 };
683 error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
684 if (error < 0)
685 goto end;
686 }
James Chapmanfd558d12010-04-02 06:18:33 +0000687 } else {
James Chapmanfd558d12010-04-02 06:18:33 +0000688 /* Error if we can't find the tunnel */
689 error = -ENOENT;
690 if (tunnel == NULL)
691 goto end;
692
693 /* Error if socket is not prepped */
694 if (tunnel->sock == NULL)
695 goto end;
696 }
697
698 if (tunnel->recv_payload_hook == NULL)
699 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
700
James Chapmanb79585f2012-04-29 21:48:50 +0000701 if (tunnel->peer_tunnel_id == 0)
702 tunnel->peer_tunnel_id = peer_tunnel_id;
James Chapmanfd558d12010-04-02 06:18:33 +0000703
James Chapman309795f2010-04-02 06:19:10 +0000704 /* Create session if it doesn't already exist. We handle the
705 * case where a session was previously created by the netlink
706 * interface by checking that the session doesn't already have
707 * a socket and its tunnel socket are what we expect. If any
708 * of those checks fail, return EEXIST to the caller.
709 */
710 session = l2tp_session_find(sock_net(sk), tunnel, session_id);
711 if (session == NULL) {
712 /* Default MTU must allow space for UDP/L2TP/PPP
713 * headers.
714 */
715 cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
716
717 /* Allocate and initialize a new session context. */
718 session = l2tp_session_create(sizeof(struct pppol2tp_session),
719 tunnel, session_id,
720 peer_session_id, &cfg);
721 if (session == NULL) {
722 error = -ENOMEM;
723 goto end;
724 }
725 } else {
726 ps = l2tp_session_priv(session);
727 error = -EEXIST;
728 if (ps->sock != NULL)
729 goto end;
730
731 /* consistency checks */
732 if (ps->tunnel_sock != tunnel->sock)
733 goto end;
734 }
735
736 /* Associate session with its PPPoL2TP socket */
James Chapmanfd558d12010-04-02 06:18:33 +0000737 ps = l2tp_session_priv(session);
738 ps->owner = current->pid;
739 ps->sock = sk;
740 ps->tunnel_sock = tunnel->sock;
741
742 session->recv_skb = pppol2tp_recv;
743 session->session_close = pppol2tp_session_close;
David S. Millerf66ef2d2010-04-03 15:01:37 -0700744#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
James Chapman0ad66142010-04-02 06:19:33 +0000745 session->show = pppol2tp_show;
746#endif
James Chapmanfd558d12010-04-02 06:18:33 +0000747
748 /* We need to know each time a skb is dropped from the reorder
749 * queue.
750 */
751 session->ref = pppol2tp_session_sock_hold;
752 session->deref = pppol2tp_session_sock_put;
753
754 /* If PMTU discovery was enabled, use the MTU that was discovered */
755 dst = sk_dst_get(sk);
756 if (dst != NULL) {
757 u32 pmtu = dst_mtu(__sk_dst_get(sk));
758 if (pmtu != 0)
759 session->mtu = session->mru = pmtu -
760 PPPOL2TP_HEADER_OVERHEAD;
761 dst_release(dst);
762 }
763
764 /* Special case: if source & dest session_id == 0x0000, this
765 * socket is being created to manage the tunnel. Just set up
766 * the internal context for use by ioctl() and sockopt()
767 * handlers.
768 */
769 if ((session->session_id == 0) &&
770 (session->peer_session_id == 0)) {
771 error = 0;
772 goto out_no_ppp;
773 }
774
775 /* The only header we need to worry about is the L2TP
776 * header. This size is different depending on whether
777 * sequence numbers are enabled for the data channel.
778 */
779 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
780
781 po->chan.private = sk;
782 po->chan.ops = &pppol2tp_chan_ops;
783 po->chan.mtu = session->mtu;
784
785 error = ppp_register_net_channel(sock_net(sk), &po->chan);
786 if (error)
787 goto end;
788
789out_no_ppp:
790 /* This is how we get the session context from the socket. */
791 sk->sk_user_data = session;
792 sk->sk_state = PPPOX_CONNECTED;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000793 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
794 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000795
796end:
797 release_sock(sk);
798
799 return error;
800}
801
James Chapman309795f2010-04-02 06:19:10 +0000802#ifdef CONFIG_L2TP_V3
803
804/* Called when creating sessions via the netlink interface.
805 */
806static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
807{
808 int error;
809 struct l2tp_tunnel *tunnel;
810 struct l2tp_session *session;
811 struct pppol2tp_session *ps;
812
813 tunnel = l2tp_tunnel_find(net, tunnel_id);
814
815 /* Error if we can't find the tunnel */
816 error = -ENOENT;
817 if (tunnel == NULL)
818 goto out;
819
820 /* Error if tunnel socket is not prepped */
821 if (tunnel->sock == NULL)
822 goto out;
823
824 /* Check that this session doesn't already exist */
825 error = -EEXIST;
826 session = l2tp_session_find(net, tunnel, session_id);
827 if (session != NULL)
828 goto out;
829
830 /* Default MTU values. */
831 if (cfg->mtu == 0)
832 cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
833 if (cfg->mru == 0)
834 cfg->mru = cfg->mtu;
835
836 /* Allocate and initialize a new session context. */
837 error = -ENOMEM;
838 session = l2tp_session_create(sizeof(struct pppol2tp_session),
839 tunnel, session_id,
840 peer_session_id, cfg);
841 if (session == NULL)
842 goto out;
843
844 ps = l2tp_session_priv(session);
845 ps->tunnel_sock = tunnel->sock;
846
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000847 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
848 session->name);
James Chapman309795f2010-04-02 06:19:10 +0000849
850 error = 0;
851
852out:
853 return error;
854}
855
James Chapman309795f2010-04-02 06:19:10 +0000856#endif /* CONFIG_L2TP_V3 */
857
James Chapmanfd558d12010-04-02 06:18:33 +0000858/* getname() support.
859 */
860static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
861 int *usockaddr_len, int peer)
862{
James Chapmane0d44352010-04-02 06:18:54 +0000863 int len = 0;
James Chapmanfd558d12010-04-02 06:18:33 +0000864 int error = 0;
865 struct l2tp_session *session;
866 struct l2tp_tunnel *tunnel;
867 struct sock *sk = sock->sk;
868 struct inet_sock *inet;
869 struct pppol2tp_session *pls;
870
871 error = -ENOTCONN;
872 if (sk == NULL)
873 goto end;
874 if (sk->sk_state != PPPOX_CONNECTED)
875 goto end;
876
877 error = -EBADF;
878 session = pppol2tp_sock_to_session(sk);
879 if (session == NULL)
880 goto end;
881
882 pls = l2tp_session_priv(session);
883 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
884 if (tunnel == NULL) {
885 error = -EBADF;
886 goto end_put_sess;
887 }
888
Benjamin LaHaisebbdb32c2012-03-20 03:57:54 +0000889 inet = inet_sk(tunnel->sock);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000890 if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
James Chapmane0d44352010-04-02 06:18:54 +0000891 struct sockaddr_pppol2tp sp;
892 len = sizeof(sp);
893 memset(&sp, 0, len);
894 sp.sa_family = AF_PPPOX;
895 sp.sa_protocol = PX_PROTO_OL2TP;
896 sp.pppol2tp.fd = tunnel->fd;
897 sp.pppol2tp.pid = pls->owner;
898 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
899 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
900 sp.pppol2tp.s_session = session->session_id;
901 sp.pppol2tp.d_session = session->peer_session_id;
902 sp.pppol2tp.addr.sin_family = AF_INET;
903 sp.pppol2tp.addr.sin_port = inet->inet_dport;
904 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
905 memcpy(uaddr, &sp, len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +0000906#if IS_ENABLED(CONFIG_IPV6)
907 } else if ((tunnel->version == 2) &&
908 (tunnel->sock->sk_family == AF_INET6)) {
909 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
910 struct sockaddr_pppol2tpin6 sp;
911 len = sizeof(sp);
912 memset(&sp, 0, len);
913 sp.sa_family = AF_PPPOX;
914 sp.sa_protocol = PX_PROTO_OL2TP;
915 sp.pppol2tp.fd = tunnel->fd;
916 sp.pppol2tp.pid = pls->owner;
917 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
918 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
919 sp.pppol2tp.s_session = session->session_id;
920 sp.pppol2tp.d_session = session->peer_session_id;
921 sp.pppol2tp.addr.sin6_family = AF_INET6;
922 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
923 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr,
924 sizeof(np->daddr));
925 memcpy(uaddr, &sp, len);
926 } else if ((tunnel->version == 3) &&
927 (tunnel->sock->sk_family == AF_INET6)) {
928 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
929 struct sockaddr_pppol2tpv3in6 sp;
930 len = sizeof(sp);
931 memset(&sp, 0, len);
932 sp.sa_family = AF_PPPOX;
933 sp.sa_protocol = PX_PROTO_OL2TP;
934 sp.pppol2tp.fd = tunnel->fd;
935 sp.pppol2tp.pid = pls->owner;
936 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
937 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
938 sp.pppol2tp.s_session = session->session_id;
939 sp.pppol2tp.d_session = session->peer_session_id;
940 sp.pppol2tp.addr.sin6_family = AF_INET6;
941 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
942 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr,
943 sizeof(np->daddr));
944 memcpy(uaddr, &sp, len);
945#endif
James Chapmane0d44352010-04-02 06:18:54 +0000946 } else if (tunnel->version == 3) {
947 struct sockaddr_pppol2tpv3 sp;
948 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.sin_family = AF_INET;
959 sp.pppol2tp.addr.sin_port = inet->inet_dport;
960 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
961 memcpy(uaddr, &sp, len);
962 }
James Chapmanfd558d12010-04-02 06:18:33 +0000963
964 *usockaddr_len = len;
965
966 sock_put(pls->tunnel_sock);
967end_put_sess:
968 sock_put(sk);
969 error = 0;
970
971end:
972 return error;
973}
974
975/****************************************************************************
976 * ioctl() handlers.
977 *
978 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
979 * sockets. However, in order to control kernel tunnel features, we allow
980 * userspace to create a special "tunnel" PPPoX socket which is used for
981 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
982 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
983 * calls.
984 ****************************************************************************/
985
986static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
987 struct l2tp_stats *stats)
988{
Tom Parkin7b7c0712013-03-19 06:11:22 +0000989 dest->tx_packets = atomic_long_read(&stats->tx_packets);
990 dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
991 dest->tx_errors = atomic_long_read(&stats->tx_errors);
992 dest->rx_packets = atomic_long_read(&stats->rx_packets);
993 dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
994 dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
995 dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
996 dest->rx_errors = atomic_long_read(&stats->rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000997}
998
999/* Session ioctl helper.
1000 */
1001static int pppol2tp_session_ioctl(struct l2tp_session *session,
1002 unsigned int cmd, unsigned long arg)
1003{
1004 struct ifreq ifr;
1005 int err = 0;
1006 struct sock *sk;
1007 int val = (int) arg;
1008 struct pppol2tp_session *ps = l2tp_session_priv(session);
1009 struct l2tp_tunnel *tunnel = session->tunnel;
1010 struct pppol2tp_ioc_stats stats;
1011
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001012 l2tp_dbg(session, PPPOL2TP_MSG_CONTROL,
1013 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1014 session->name, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001015
1016 sk = ps->sock;
1017 sock_hold(sk);
1018
1019 switch (cmd) {
1020 case SIOCGIFMTU:
1021 err = -ENXIO;
1022 if (!(sk->sk_state & PPPOX_CONNECTED))
1023 break;
1024
1025 err = -EFAULT;
1026 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1027 break;
1028 ifr.ifr_mtu = session->mtu;
1029 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1030 break;
1031
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001032 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mtu=%d\n",
1033 session->name, session->mtu);
James Chapmanfd558d12010-04-02 06:18:33 +00001034 err = 0;
1035 break;
1036
1037 case SIOCSIFMTU:
1038 err = -ENXIO;
1039 if (!(sk->sk_state & PPPOX_CONNECTED))
1040 break;
1041
1042 err = -EFAULT;
1043 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1044 break;
1045
1046 session->mtu = ifr.ifr_mtu;
1047
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001048 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mtu=%d\n",
1049 session->name, session->mtu);
James Chapmanfd558d12010-04-02 06:18:33 +00001050 err = 0;
1051 break;
1052
1053 case PPPIOCGMRU:
1054 err = -ENXIO;
1055 if (!(sk->sk_state & PPPOX_CONNECTED))
1056 break;
1057
1058 err = -EFAULT;
1059 if (put_user(session->mru, (int __user *) arg))
1060 break;
1061
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001062 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mru=%d\n",
1063 session->name, session->mru);
James Chapmanfd558d12010-04-02 06:18:33 +00001064 err = 0;
1065 break;
1066
1067 case PPPIOCSMRU:
1068 err = -ENXIO;
1069 if (!(sk->sk_state & PPPOX_CONNECTED))
1070 break;
1071
1072 err = -EFAULT;
1073 if (get_user(val, (int __user *) arg))
1074 break;
1075
1076 session->mru = val;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001077 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mru=%d\n",
1078 session->name, session->mru);
James Chapmanfd558d12010-04-02 06:18:33 +00001079 err = 0;
1080 break;
1081
1082 case PPPIOCGFLAGS:
1083 err = -EFAULT;
1084 if (put_user(ps->flags, (int __user *) arg))
1085 break;
1086
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001087 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get flags=%d\n",
1088 session->name, ps->flags);
James Chapmanfd558d12010-04-02 06:18:33 +00001089 err = 0;
1090 break;
1091
1092 case PPPIOCSFLAGS:
1093 err = -EFAULT;
1094 if (get_user(val, (int __user *) arg))
1095 break;
1096 ps->flags = val;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001097 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set flags=%d\n",
1098 session->name, ps->flags);
James Chapmanfd558d12010-04-02 06:18:33 +00001099 err = 0;
1100 break;
1101
1102 case PPPIOCGL2TPSTATS:
1103 err = -ENXIO;
1104 if (!(sk->sk_state & PPPOX_CONNECTED))
1105 break;
1106
1107 memset(&stats, 0, sizeof(stats));
1108 stats.tunnel_id = tunnel->tunnel_id;
1109 stats.session_id = session->session_id;
1110 pppol2tp_copy_stats(&stats, &session->stats);
1111 if (copy_to_user((void __user *) arg, &stats,
1112 sizeof(stats)))
1113 break;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001114 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1115 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001116 err = 0;
1117 break;
1118
1119 default:
1120 err = -ENOSYS;
1121 break;
1122 }
1123
1124 sock_put(sk);
1125
1126 return err;
1127}
1128
1129/* Tunnel ioctl helper.
1130 *
1131 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1132 * specifies a session_id, the session ioctl handler is called. This allows an
1133 * application to retrieve session stats via a tunnel socket.
1134 */
1135static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1136 unsigned int cmd, unsigned long arg)
1137{
1138 int err = 0;
1139 struct sock *sk;
1140 struct pppol2tp_ioc_stats stats;
1141
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001142 l2tp_dbg(tunnel, PPPOL2TP_MSG_CONTROL,
1143 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1144 tunnel->name, cmd, arg);
James Chapmanfd558d12010-04-02 06:18:33 +00001145
1146 sk = tunnel->sock;
1147 sock_hold(sk);
1148
1149 switch (cmd) {
1150 case PPPIOCGL2TPSTATS:
1151 err = -ENXIO;
1152 if (!(sk->sk_state & PPPOX_CONNECTED))
1153 break;
1154
1155 if (copy_from_user(&stats, (void __user *) arg,
1156 sizeof(stats))) {
1157 err = -EFAULT;
1158 break;
1159 }
1160 if (stats.session_id != 0) {
1161 /* resend to session ioctl handler */
1162 struct l2tp_session *session =
James Chapmanf7faffa2010-04-02 06:18:49 +00001163 l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
James Chapmanfd558d12010-04-02 06:18:33 +00001164 if (session != NULL)
1165 err = pppol2tp_session_ioctl(session, cmd, arg);
1166 else
1167 err = -EBADR;
1168 break;
1169 }
1170#ifdef CONFIG_XFRM
1171 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1172#endif
1173 pppol2tp_copy_stats(&stats, &tunnel->stats);
1174 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1175 err = -EFAULT;
1176 break;
1177 }
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001178 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
1179 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001180 err = 0;
1181 break;
1182
1183 default:
1184 err = -ENOSYS;
1185 break;
1186 }
1187
1188 sock_put(sk);
1189
1190 return err;
1191}
1192
1193/* Main ioctl() handler.
1194 * Dispatch to tunnel or session helpers depending on the socket.
1195 */
1196static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1197 unsigned long arg)
1198{
1199 struct sock *sk = sock->sk;
1200 struct l2tp_session *session;
1201 struct l2tp_tunnel *tunnel;
1202 struct pppol2tp_session *ps;
1203 int err;
1204
1205 if (!sk)
1206 return 0;
1207
1208 err = -EBADF;
1209 if (sock_flag(sk, SOCK_DEAD) != 0)
1210 goto end;
1211
1212 err = -ENOTCONN;
1213 if ((sk->sk_user_data == NULL) ||
1214 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1215 goto end;
1216
1217 /* Get session context from the socket */
1218 err = -EBADF;
1219 session = pppol2tp_sock_to_session(sk);
1220 if (session == NULL)
1221 goto end;
1222
1223 /* Special case: if session's session_id is zero, treat ioctl as a
1224 * tunnel ioctl
1225 */
1226 ps = l2tp_session_priv(session);
1227 if ((session->session_id == 0) &&
1228 (session->peer_session_id == 0)) {
1229 err = -EBADF;
1230 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1231 if (tunnel == NULL)
1232 goto end_put_sess;
1233
1234 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1235 sock_put(ps->tunnel_sock);
1236 goto end_put_sess;
1237 }
1238
1239 err = pppol2tp_session_ioctl(session, cmd, arg);
1240
1241end_put_sess:
1242 sock_put(sk);
1243end:
1244 return err;
1245}
1246
1247/*****************************************************************************
1248 * setsockopt() / getsockopt() support.
1249 *
1250 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1251 * sockets. In order to control kernel tunnel features, we allow userspace to
1252 * create a special "tunnel" PPPoX socket which is used for control only.
1253 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1254 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1255 *****************************************************************************/
1256
1257/* Tunnel setsockopt() helper.
1258 */
1259static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1260 struct l2tp_tunnel *tunnel,
1261 int optname, int val)
1262{
1263 int err = 0;
1264
1265 switch (optname) {
1266 case PPPOL2TP_SO_DEBUG:
1267 tunnel->debug = val;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001268 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
1269 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001270 break;
1271
1272 default:
1273 err = -ENOPROTOOPT;
1274 break;
1275 }
1276
1277 return err;
1278}
1279
1280/* Session setsockopt helper.
1281 */
1282static int pppol2tp_session_setsockopt(struct sock *sk,
1283 struct l2tp_session *session,
1284 int optname, int val)
1285{
1286 int err = 0;
1287 struct pppol2tp_session *ps = l2tp_session_priv(session);
1288
1289 switch (optname) {
1290 case PPPOL2TP_SO_RECVSEQ:
1291 if ((val != 0) && (val != 1)) {
1292 err = -EINVAL;
1293 break;
1294 }
1295 session->recv_seq = val ? -1 : 0;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001296 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1297 "%s: set recv_seq=%d\n",
1298 session->name, session->recv_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001299 break;
1300
1301 case PPPOL2TP_SO_SENDSEQ:
1302 if ((val != 0) && (val != 1)) {
1303 err = -EINVAL;
1304 break;
1305 }
1306 session->send_seq = val ? -1 : 0;
1307 {
1308 struct sock *ssk = ps->sock;
1309 struct pppox_sock *po = pppox_sk(ssk);
1310 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1311 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1312 }
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001313 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1314 "%s: set send_seq=%d\n",
1315 session->name, session->send_seq);
James Chapmanfd558d12010-04-02 06:18:33 +00001316 break;
1317
1318 case PPPOL2TP_SO_LNSMODE:
1319 if ((val != 0) && (val != 1)) {
1320 err = -EINVAL;
1321 break;
1322 }
1323 session->lns_mode = val ? -1 : 0;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001324 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1325 "%s: set lns_mode=%d\n",
1326 session->name, session->lns_mode);
James Chapmanfd558d12010-04-02 06:18:33 +00001327 break;
1328
1329 case PPPOL2TP_SO_DEBUG:
1330 session->debug = val;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001331 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
1332 session->name, session->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001333 break;
1334
1335 case PPPOL2TP_SO_REORDERTO:
1336 session->reorder_timeout = msecs_to_jiffies(val);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001337 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1338 "%s: set reorder_timeout=%d\n",
1339 session->name, session->reorder_timeout);
James Chapmanfd558d12010-04-02 06:18:33 +00001340 break;
1341
1342 default:
1343 err = -ENOPROTOOPT;
1344 break;
1345 }
1346
1347 return err;
1348}
1349
1350/* Main setsockopt() entry point.
1351 * Does API checks, then calls either the tunnel or session setsockopt
1352 * handler, according to whether the PPPoL2TP socket is a for a regular
1353 * session or the special tunnel type.
1354 */
1355static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1356 char __user *optval, unsigned int optlen)
1357{
1358 struct sock *sk = sock->sk;
1359 struct l2tp_session *session;
1360 struct l2tp_tunnel *tunnel;
1361 struct pppol2tp_session *ps;
1362 int val;
1363 int err;
1364
1365 if (level != SOL_PPPOL2TP)
1366 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1367
1368 if (optlen < sizeof(int))
1369 return -EINVAL;
1370
1371 if (get_user(val, (int __user *)optval))
1372 return -EFAULT;
1373
1374 err = -ENOTCONN;
1375 if (sk->sk_user_data == NULL)
1376 goto end;
1377
1378 /* Get session context from the socket */
1379 err = -EBADF;
1380 session = pppol2tp_sock_to_session(sk);
1381 if (session == NULL)
1382 goto end;
1383
1384 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1385 */
1386 ps = l2tp_session_priv(session);
1387 if ((session->session_id == 0) &&
1388 (session->peer_session_id == 0)) {
1389 err = -EBADF;
1390 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1391 if (tunnel == NULL)
1392 goto end_put_sess;
1393
1394 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1395 sock_put(ps->tunnel_sock);
1396 } else
1397 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1398
1399 err = 0;
1400
1401end_put_sess:
1402 sock_put(sk);
1403end:
1404 return err;
1405}
1406
1407/* Tunnel getsockopt helper. Called with sock locked.
1408 */
1409static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1410 struct l2tp_tunnel *tunnel,
1411 int optname, int *val)
1412{
1413 int err = 0;
1414
1415 switch (optname) {
1416 case PPPOL2TP_SO_DEBUG:
1417 *val = tunnel->debug;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001418 l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get debug=%x\n",
1419 tunnel->name, tunnel->debug);
James Chapmanfd558d12010-04-02 06:18:33 +00001420 break;
1421
1422 default:
1423 err = -ENOPROTOOPT;
1424 break;
1425 }
1426
1427 return err;
1428}
1429
1430/* Session getsockopt helper. Called with sock locked.
1431 */
1432static int pppol2tp_session_getsockopt(struct sock *sk,
1433 struct l2tp_session *session,
1434 int optname, int *val)
1435{
1436 int err = 0;
1437
1438 switch (optname) {
1439 case PPPOL2TP_SO_RECVSEQ:
1440 *val = session->recv_seq;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001441 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1442 "%s: get recv_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001443 break;
1444
1445 case PPPOL2TP_SO_SENDSEQ:
1446 *val = session->send_seq;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001447 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1448 "%s: get send_seq=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001449 break;
1450
1451 case PPPOL2TP_SO_LNSMODE:
1452 *val = session->lns_mode;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001453 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1454 "%s: get lns_mode=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001455 break;
1456
1457 case PPPOL2TP_SO_DEBUG:
1458 *val = session->debug;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001459 l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get debug=%d\n",
1460 session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001461 break;
1462
1463 case PPPOL2TP_SO_REORDERTO:
1464 *val = (int) jiffies_to_msecs(session->reorder_timeout);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001465 l2tp_info(session, PPPOL2TP_MSG_CONTROL,
1466 "%s: get reorder_timeout=%d\n", session->name, *val);
James Chapmanfd558d12010-04-02 06:18:33 +00001467 break;
1468
1469 default:
1470 err = -ENOPROTOOPT;
1471 }
1472
1473 return err;
1474}
1475
1476/* Main getsockopt() entry point.
1477 * Does API checks, then calls either the tunnel or session getsockopt
1478 * handler, according to whether the PPPoX socket is a for a regular session
1479 * or the special tunnel type.
1480 */
Joe Perchese3192692012-06-03 17:41:40 +00001481static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
1482 char __user *optval, int __user *optlen)
James Chapmanfd558d12010-04-02 06:18:33 +00001483{
1484 struct sock *sk = sock->sk;
1485 struct l2tp_session *session;
1486 struct l2tp_tunnel *tunnel;
1487 int val, len;
1488 int err;
1489 struct pppol2tp_session *ps;
1490
1491 if (level != SOL_PPPOL2TP)
1492 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1493
Joe Perchese3192692012-06-03 17:41:40 +00001494 if (get_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001495 return -EFAULT;
1496
1497 len = min_t(unsigned int, len, sizeof(int));
1498
1499 if (len < 0)
1500 return -EINVAL;
1501
1502 err = -ENOTCONN;
1503 if (sk->sk_user_data == NULL)
1504 goto end;
1505
1506 /* Get the session context */
1507 err = -EBADF;
1508 session = pppol2tp_sock_to_session(sk);
1509 if (session == NULL)
1510 goto end;
1511
1512 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1513 ps = l2tp_session_priv(session);
1514 if ((session->session_id == 0) &&
1515 (session->peer_session_id == 0)) {
1516 err = -EBADF;
1517 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1518 if (tunnel == NULL)
1519 goto end_put_sess;
1520
1521 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1522 sock_put(ps->tunnel_sock);
1523 } else
1524 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1525
1526 err = -EFAULT;
Joe Perchese3192692012-06-03 17:41:40 +00001527 if (put_user(len, optlen))
James Chapmanfd558d12010-04-02 06:18:33 +00001528 goto end_put_sess;
1529
1530 if (copy_to_user((void __user *) optval, &val, len))
1531 goto end_put_sess;
1532
1533 err = 0;
1534
1535end_put_sess:
1536 sock_put(sk);
1537end:
1538 return err;
1539}
1540
1541/*****************************************************************************
1542 * /proc filesystem for debug
James Chapmanf7faffa2010-04-02 06:18:49 +00001543 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1544 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
James Chapmanfd558d12010-04-02 06:18:33 +00001545 *****************************************************************************/
1546
1547static unsigned int pppol2tp_net_id;
1548
1549#ifdef CONFIG_PROC_FS
1550
1551struct pppol2tp_seq_data {
1552 struct seq_net_private p;
1553 int tunnel_idx; /* current tunnel */
1554 int session_idx; /* index of session within current tunnel */
1555 struct l2tp_tunnel *tunnel;
1556 struct l2tp_session *session; /* NULL means get next tunnel */
1557};
1558
1559static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1560{
James Chapmanf7faffa2010-04-02 06:18:49 +00001561 for (;;) {
1562 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1563 pd->tunnel_idx++;
1564
1565 if (pd->tunnel == NULL)
1566 break;
1567
1568 /* Ignore L2TPv3 tunnels */
1569 if (pd->tunnel->version < 3)
1570 break;
1571 }
James Chapmanfd558d12010-04-02 06:18:33 +00001572}
1573
1574static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1575{
1576 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1577 pd->session_idx++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001578
James Chapmanfd558d12010-04-02 06:18:33 +00001579 if (pd->session == NULL) {
1580 pd->session_idx = 0;
1581 pppol2tp_next_tunnel(net, pd);
1582 }
1583}
1584
1585static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1586{
1587 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1588 loff_t pos = *offs;
1589 struct net *net;
1590
1591 if (!pos)
1592 goto out;
1593
1594 BUG_ON(m->private == NULL);
1595 pd = m->private;
1596 net = seq_file_net(m);
1597
1598 if (pd->tunnel == NULL)
1599 pppol2tp_next_tunnel(net, pd);
1600 else
1601 pppol2tp_next_session(net, pd);
1602
1603 /* NULL tunnel and session indicates end of list */
1604 if ((pd->tunnel == NULL) && (pd->session == NULL))
1605 pd = NULL;
1606
1607out:
1608 return pd;
1609}
1610
1611static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1612{
1613 (*pos)++;
1614 return NULL;
1615}
1616
1617static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1618{
1619 /* nothing to do */
1620}
1621
1622static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1623{
1624 struct l2tp_tunnel *tunnel = v;
1625
1626 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1627 tunnel->name,
1628 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1629 atomic_read(&tunnel->ref_count) - 1);
Tom Parkin7b7c0712013-03-19 06:11:22 +00001630 seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001631 tunnel->debug,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001632 atomic_long_read(&tunnel->stats.tx_packets),
1633 atomic_long_read(&tunnel->stats.tx_bytes),
1634 atomic_long_read(&tunnel->stats.tx_errors),
1635 atomic_long_read(&tunnel->stats.rx_packets),
1636 atomic_long_read(&tunnel->stats.rx_bytes),
1637 atomic_long_read(&tunnel->stats.rx_errors));
James Chapmanfd558d12010-04-02 06:18:33 +00001638}
1639
1640static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1641{
1642 struct l2tp_session *session = v;
1643 struct l2tp_tunnel *tunnel = session->tunnel;
1644 struct pppol2tp_session *ps = l2tp_session_priv(session);
James Chapman93454712010-04-02 06:18:44 +00001645 struct pppox_sock *po = pppox_sk(ps->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001646 u32 ip = 0;
1647 u16 port = 0;
1648
1649 if (tunnel->sock) {
1650 struct inet_sock *inet = inet_sk(tunnel->sock);
1651 ip = ntohl(inet->inet_saddr);
1652 port = ntohs(inet->inet_sport);
1653 }
1654
1655 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1656 "%04X/%04X %d %c\n",
1657 session->name, ip, port,
1658 tunnel->tunnel_id,
1659 session->session_id,
1660 tunnel->peer_tunnel_id,
1661 session->peer_session_id,
1662 ps->sock->sk_state,
1663 (session == ps->sock->sk_user_data) ?
1664 'Y' : 'N');
1665 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1666 session->mtu, session->mru,
1667 session->recv_seq ? 'R' : '-',
1668 session->send_seq ? 'S' : '-',
1669 session->lns_mode ? "LNS" : "LAC",
1670 session->debug,
1671 jiffies_to_msecs(session->reorder_timeout));
Tom Parkin7b7c0712013-03-19 06:11:22 +00001672 seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
James Chapmanfd558d12010-04-02 06:18:33 +00001673 session->nr, session->ns,
Tom Parkin7b7c0712013-03-19 06:11:22 +00001674 atomic_long_read(&session->stats.tx_packets),
1675 atomic_long_read(&session->stats.tx_bytes),
1676 atomic_long_read(&session->stats.tx_errors),
1677 atomic_long_read(&session->stats.rx_packets),
1678 atomic_long_read(&session->stats.rx_bytes),
1679 atomic_long_read(&session->stats.rx_errors));
James Chapman93454712010-04-02 06:18:44 +00001680
1681 if (po)
1682 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
James Chapmanfd558d12010-04-02 06:18:33 +00001683}
1684
1685static int pppol2tp_seq_show(struct seq_file *m, void *v)
1686{
1687 struct pppol2tp_seq_data *pd = v;
1688
1689 /* display header on line 1 */
1690 if (v == SEQ_START_TOKEN) {
1691 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1692 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1693 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1694 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1695 "dest-tid/sid state user-data-ok\n");
1696 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1697 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1698 goto out;
1699 }
1700
1701 /* Show the tunnel or session context.
1702 */
1703 if (pd->session == NULL)
1704 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1705 else
1706 pppol2tp_seq_session_show(m, pd->session);
1707
1708out:
1709 return 0;
1710}
1711
1712static const struct seq_operations pppol2tp_seq_ops = {
1713 .start = pppol2tp_seq_start,
1714 .next = pppol2tp_seq_next,
1715 .stop = pppol2tp_seq_stop,
1716 .show = pppol2tp_seq_show,
1717};
1718
1719/* Called when our /proc file is opened. We allocate data for use when
1720 * iterating our tunnel / session contexts and store it in the private
1721 * data of the seq_file.
1722 */
1723static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1724{
1725 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1726 sizeof(struct pppol2tp_seq_data));
1727}
1728
1729static const struct file_operations pppol2tp_proc_fops = {
1730 .owner = THIS_MODULE,
1731 .open = pppol2tp_proc_open,
1732 .read = seq_read,
1733 .llseek = seq_lseek,
1734 .release = seq_release_net,
1735};
1736
1737#endif /* CONFIG_PROC_FS */
1738
1739/*****************************************************************************
1740 * Network namespace
1741 *****************************************************************************/
1742
1743static __net_init int pppol2tp_init_net(struct net *net)
1744{
1745 struct proc_dir_entry *pde;
1746 int err = 0;
1747
Gao fengd4beaa62013-02-18 01:34:54 +00001748 pde = proc_create("pppol2tp", S_IRUGO, net->proc_net,
1749 &pppol2tp_proc_fops);
James Chapmanfd558d12010-04-02 06:18:33 +00001750 if (!pde) {
1751 err = -ENOMEM;
1752 goto out;
1753 }
1754
1755out:
1756 return err;
1757}
1758
1759static __net_exit void pppol2tp_exit_net(struct net *net)
1760{
Gao fengece31ff2013-02-18 01:34:56 +00001761 remove_proc_entry("pppol2tp", net->proc_net);
James Chapmanfd558d12010-04-02 06:18:33 +00001762}
1763
1764static struct pernet_operations pppol2tp_net_ops = {
1765 .init = pppol2tp_init_net,
1766 .exit = pppol2tp_exit_net,
1767 .id = &pppol2tp_net_id,
1768};
1769
1770/*****************************************************************************
1771 * Init and cleanup
1772 *****************************************************************************/
1773
1774static const struct proto_ops pppol2tp_ops = {
1775 .family = AF_PPPOX,
1776 .owner = THIS_MODULE,
1777 .release = pppol2tp_release,
1778 .bind = sock_no_bind,
1779 .connect = pppol2tp_connect,
1780 .socketpair = sock_no_socketpair,
1781 .accept = sock_no_accept,
1782 .getname = pppol2tp_getname,
1783 .poll = datagram_poll,
1784 .listen = sock_no_listen,
1785 .shutdown = sock_no_shutdown,
1786 .setsockopt = pppol2tp_setsockopt,
1787 .getsockopt = pppol2tp_getsockopt,
1788 .sendmsg = pppol2tp_sendmsg,
1789 .recvmsg = pppol2tp_recvmsg,
1790 .mmap = sock_no_mmap,
1791 .ioctl = pppox_ioctl,
1792};
1793
Eric Dumazet756e64a2010-09-21 06:43:54 +00001794static const struct pppox_proto pppol2tp_proto = {
James Chapmanfd558d12010-04-02 06:18:33 +00001795 .create = pppol2tp_create,
1796 .ioctl = pppol2tp_ioctl
1797};
1798
James Chapman309795f2010-04-02 06:19:10 +00001799#ifdef CONFIG_L2TP_V3
1800
1801static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1802 .session_create = pppol2tp_session_create,
Tom Parkincf2f5c82013-03-19 06:11:21 +00001803 .session_delete = l2tp_session_delete,
James Chapman309795f2010-04-02 06:19:10 +00001804};
1805
1806#endif /* CONFIG_L2TP_V3 */
1807
James Chapmanfd558d12010-04-02 06:18:33 +00001808static int __init pppol2tp_init(void)
1809{
1810 int err;
1811
1812 err = register_pernet_device(&pppol2tp_net_ops);
1813 if (err)
1814 goto out;
1815
1816 err = proto_register(&pppol2tp_sk_proto, 0);
1817 if (err)
1818 goto out_unregister_pppol2tp_pernet;
1819
1820 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1821 if (err)
1822 goto out_unregister_pppol2tp_proto;
1823
James Chapman309795f2010-04-02 06:19:10 +00001824#ifdef CONFIG_L2TP_V3
1825 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1826 if (err)
1827 goto out_unregister_pppox;
1828#endif
1829
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001830 pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001831
1832out:
1833 return err;
James Chapman309795f2010-04-02 06:19:10 +00001834
1835#ifdef CONFIG_L2TP_V3
1836out_unregister_pppox:
1837 unregister_pppox_proto(PX_PROTO_OL2TP);
1838#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001839out_unregister_pppol2tp_proto:
1840 proto_unregister(&pppol2tp_sk_proto);
1841out_unregister_pppol2tp_pernet:
1842 unregister_pernet_device(&pppol2tp_net_ops);
1843 goto out;
1844}
1845
1846static void __exit pppol2tp_exit(void)
1847{
James Chapman309795f2010-04-02 06:19:10 +00001848#ifdef CONFIG_L2TP_V3
1849 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1850#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001851 unregister_pppox_proto(PX_PROTO_OL2TP);
1852 proto_unregister(&pppol2tp_sk_proto);
1853 unregister_pernet_device(&pppol2tp_net_ops);
1854}
1855
1856module_init(pppol2tp_init);
1857module_exit(pppol2tp_exit);
1858
1859MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1860MODULE_DESCRIPTION("PPP over L2TP over UDP");
1861MODULE_LICENSE("GPL");
1862MODULE_VERSION(PPPOL2TP_DRV_VERSION);
Benjamin LaHaise9395a092012-03-20 14:01:21 +00001863MODULE_ALIAS("pppox-proto-" __stringify(PX_PROTO_OL2TP));