]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/l2tp/l2tp_ppp.c
l2tp: Add L2TPv3 protocol support
[mirror_ubuntu-bionic-kernel.git] / net / l2tp / l2tp_ppp.c
CommitLineData
fd558d18
JC
1/*****************************************************************************
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
60#include <linux/module.h>
61#include <linux/string.h>
62#include <linux/list.h>
63#include <linux/uaccess.h>
64
65#include <linux/kernel.h>
66#include <linux/spinlock.h>
67#include <linux/kthread.h>
68#include <linux/sched.h>
69#include <linux/slab.h>
70#include <linux/errno.h>
71#include <linux/jiffies.h>
72
73#include <linux/netdevice.h>
74#include <linux/net.h>
75#include <linux/inetdevice.h>
76#include <linux/skbuff.h>
77#include <linux/init.h>
78#include <linux/ip.h>
79#include <linux/udp.h>
80#include <linux/if_pppox.h>
81#include <linux/if_pppol2tp.h>
82#include <net/sock.h>
83#include <linux/ppp_channel.h>
84#include <linux/ppp_defs.h>
85#include <linux/if_ppp.h>
86#include <linux/file.h>
87#include <linux/hash.h>
88#include <linux/sort.h>
89#include <linux/proc_fs.h>
90#include <linux/nsproxy.h>
91#include <net/net_namespace.h>
92#include <net/netns/generic.h>
93#include <net/dst.h>
94#include <net/ip.h>
95#include <net/udp.h>
96#include <net/xfrm.h>
97
98#include <asm/byteorder.h>
99#include <asm/atomic.h>
100
101#include "l2tp_core.h"
102
103#define PPPOL2TP_DRV_VERSION "V2.0"
104
105/* Space for UDP, L2TP and PPP headers */
106#define PPPOL2TP_HEADER_OVERHEAD 40
107
108#define PRINTK(_mask, _type, _lvl, _fmt, args...) \
109 do { \
110 if ((_mask) & (_type)) \
111 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
112 } while (0)
113
114/* Number of bytes to build transmit L2TP headers.
115 * Unfortunately the size is different depending on whether sequence numbers
116 * are enabled.
117 */
118#define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
119#define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
120
121/* Private data of each session. This data lives at the end of struct
122 * l2tp_session, referenced via session->priv[].
123 */
124struct pppol2tp_session {
125 int owner; /* pid that opened the socket */
126
127 struct sock *sock; /* Pointer to the session
128 * PPPoX socket */
129 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
130 * socket */
131 int flags; /* accessed by PPPIOCGFLAGS.
132 * Unused. */
133};
134
135static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
136
137static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL };
138static const struct proto_ops pppol2tp_ops;
139
140/* Helpers to obtain tunnel/session contexts from sockets.
141 */
142static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
143{
144 struct l2tp_session *session;
145
146 if (sk == NULL)
147 return NULL;
148
149 sock_hold(sk);
150 session = (struct l2tp_session *)(sk->sk_user_data);
151 if (session == NULL) {
152 sock_put(sk);
153 goto out;
154 }
155
156 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
157
158out:
159 return session;
160}
161
162/*****************************************************************************
163 * Receive data handling
164 *****************************************************************************/
165
166static int pppol2tp_recv_payload_hook(struct sk_buff *skb)
167{
168 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
169 * don't send the PPP header (PPP header compression enabled), but
170 * other clients can include the header. So we cope with both cases
171 * here. The PPP header is always FF03 when using L2TP.
172 *
173 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
174 * the field may be unaligned.
175 */
176 if (!pskb_may_pull(skb, 2))
177 return 1;
178
179 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
180 skb_pull(skb, 2);
181
182 return 0;
183}
184
185/* Receive message. This is the recvmsg for the PPPoL2TP socket.
186 */
187static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
188 struct msghdr *msg, size_t len,
189 int flags)
190{
191 int err;
192 struct sk_buff *skb;
193 struct sock *sk = sock->sk;
194
195 err = -EIO;
196 if (sk->sk_state & PPPOX_BOUND)
197 goto end;
198
199 msg->msg_namelen = 0;
200
201 err = 0;
202 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
203 flags & MSG_DONTWAIT, &err);
204 if (!skb)
205 goto end;
206
207 if (len > skb->len)
208 len = skb->len;
209 else if (len < skb->len)
210 msg->msg_flags |= MSG_TRUNC;
211
212 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
213 if (likely(err == 0))
214 err = len;
215
216 kfree_skb(skb);
217end:
218 return err;
219}
220
221static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
222{
223 struct pppol2tp_session *ps = l2tp_session_priv(session);
224 struct sock *sk = NULL;
225
226 /* If the socket is bound, send it in to PPP's input queue. Otherwise
227 * queue it on the session socket.
228 */
229 sk = ps->sock;
230 if (sk == NULL)
231 goto no_sock;
232
233 if (sk->sk_state & PPPOX_BOUND) {
234 struct pppox_sock *po;
235 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
236 "%s: recv %d byte data frame, passing to ppp\n",
237 session->name, data_len);
238
239 /* We need to forget all info related to the L2TP packet
240 * gathered in the skb as we are going to reuse the same
241 * skb for the inner packet.
242 * Namely we need to:
243 * - reset xfrm (IPSec) information as it applies to
244 * the outer L2TP packet and not to the inner one
245 * - release the dst to force a route lookup on the inner
246 * IP packet since skb->dst currently points to the dst
247 * of the UDP tunnel
248 * - reset netfilter information as it doesn't apply
249 * to the inner packet either
250 */
251 secpath_reset(skb);
252 skb_dst_drop(skb);
253 nf_reset(skb);
254
255 po = pppox_sk(sk);
256 ppp_input(&po->chan, skb);
257 } else {
258 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
259 "%s: socket not bound\n", session->name);
260
261 /* Not bound. Nothing we can do, so discard. */
262 session->stats.rx_errors++;
263 kfree_skb(skb);
264 }
265
266 return;
267
268no_sock:
269 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
270 "%s: no socket\n", session->name);
271 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
294/* Tell how big L2TP headers are for a particular session. This
295 * depends on whether sequence numbers are being used.
296 */
297static inline int pppol2tp_l2tp_header_len(struct l2tp_session *session)
298{
299 if (session->send_seq)
300 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
301
302 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
303}
304
305/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
306 * when a user application does a sendmsg() on the session socket. L2TP and
307 * PPP headers must be inserted into the user's data.
308 */
309static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
310 size_t total_len)
311{
312 static const unsigned char ppph[2] = { 0xff, 0x03 };
313 struct sock *sk = sock->sk;
314 struct sk_buff *skb;
315 int error;
316 struct l2tp_session *session;
317 struct l2tp_tunnel *tunnel;
318 struct pppol2tp_session *ps;
319
320 error = -ENOTCONN;
321 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
322 goto error;
323
324 /* Get session and tunnel contexts */
325 error = -EBADF;
326 session = pppol2tp_sock_to_session(sk);
327 if (session == NULL)
328 goto error;
329
330 ps = l2tp_session_priv(session);
331 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
332 if (tunnel == NULL)
333 goto error_put_sess;
334
335 /* Allocate a socket buffer */
336 error = -ENOMEM;
337 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
338 sizeof(struct udphdr) + session->hdr_len +
339 sizeof(ppph) + total_len,
340 0, GFP_KERNEL);
341 if (!skb)
342 goto error_put_sess_tun;
343
344 /* Reserve space for headers. */
345 skb_reserve(skb, NET_SKB_PAD);
346 skb_reset_network_header(skb);
347 skb_reserve(skb, sizeof(struct iphdr));
348 skb_reset_transport_header(skb);
349 skb_reserve(skb, sizeof(struct udphdr));
350
351 /* Add PPP header */
352 skb->data[0] = ppph[0];
353 skb->data[1] = ppph[1];
354 skb_put(skb, 2);
355
356 /* Copy user data into skb */
357 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
358 if (error < 0) {
359 kfree_skb(skb);
360 goto error_put_sess_tun;
361 }
362 skb_put(skb, total_len);
363
364 l2tp_xmit_skb(session, skb, session->hdr_len);
365
366 sock_put(ps->tunnel_sock);
367
368 return error;
369
370error_put_sess_tun:
371 sock_put(ps->tunnel_sock);
372error_put_sess:
373 sock_put(sk);
374error:
375 return error;
376}
377
378/* Transmit function called by generic PPP driver. Sends PPP frame
379 * over PPPoL2TP socket.
380 *
381 * This is almost the same as pppol2tp_sendmsg(), but rather than
382 * being called with a msghdr from userspace, it is called with a skb
383 * from the kernel.
384 *
385 * The supplied skb from ppp doesn't have enough headroom for the
386 * insertion of L2TP, UDP and IP headers so we need to allocate more
387 * headroom in the skb. This will create a cloned skb. But we must be
388 * careful in the error case because the caller will expect to free
389 * the skb it supplied, not our cloned skb. So we take care to always
390 * leave the original skb unfreed if we return an error.
391 */
392static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
393{
394 static const u8 ppph[2] = { 0xff, 0x03 };
395 struct sock *sk = (struct sock *) chan->private;
396 struct sock *sk_tun;
397 int hdr_len;
398 struct l2tp_session *session;
399 struct l2tp_tunnel *tunnel;
400 struct pppol2tp_session *ps;
401 int old_headroom;
402 int new_headroom;
403
404 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
405 goto abort;
406
407 /* Get session and tunnel contexts from the socket */
408 session = pppol2tp_sock_to_session(sk);
409 if (session == NULL)
410 goto abort;
411
412 ps = l2tp_session_priv(session);
413 sk_tun = ps->tunnel_sock;
414 if (sk_tun == NULL)
415 goto abort_put_sess;
416 tunnel = l2tp_sock_to_tunnel(sk_tun);
417 if (tunnel == NULL)
418 goto abort_put_sess;
419
420 /* What header length is configured for this session? */
421 hdr_len = pppol2tp_l2tp_header_len(session);
422
423 old_headroom = skb_headroom(skb);
424 if (skb_cow_head(skb, sizeof(ppph)))
425 goto abort_put_sess_tun;
426
427 new_headroom = skb_headroom(skb);
428 skb->truesize += new_headroom - old_headroom;
429
430 /* Setup PPP header */
431 __skb_push(skb, sizeof(ppph));
432 skb->data[0] = ppph[0];
433 skb->data[1] = ppph[1];
434
435 l2tp_xmit_skb(session, skb, hdr_len);
436
437 sock_put(sk_tun);
438 sock_put(sk);
439 return 1;
440
441abort_put_sess_tun:
442 sock_put(sk_tun);
443abort_put_sess:
444 sock_put(sk);
445abort:
446 /* Free the original skb */
447 kfree_skb(skb);
448 return 1;
449}
450
451/*****************************************************************************
452 * Session (and tunnel control) socket create/destroy.
453 *****************************************************************************/
454
455/* Called by l2tp_core when a session socket is being closed.
456 */
457static void pppol2tp_session_close(struct l2tp_session *session)
458{
459 struct pppol2tp_session *ps = l2tp_session_priv(session);
460 struct sock *sk = ps->sock;
461 struct sk_buff *skb;
462
463 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
464
465 if (session->session_id == 0)
466 goto out;
467
468 if (sk != NULL) {
469 lock_sock(sk);
470
471 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
472 pppox_unbind_sock(sk);
473 sk->sk_state = PPPOX_DEAD;
474 sk->sk_state_change(sk);
475 }
476
477 /* Purge any queued data */
478 skb_queue_purge(&sk->sk_receive_queue);
479 skb_queue_purge(&sk->sk_write_queue);
480 while ((skb = skb_dequeue(&session->reorder_q))) {
481 kfree_skb(skb);
482 sock_put(sk);
483 }
484
485 release_sock(sk);
486 }
487
488out:
489 return;
490}
491
492/* Really kill the session socket. (Called from sock_put() if
493 * refcnt == 0.)
494 */
495static void pppol2tp_session_destruct(struct sock *sk)
496{
497 struct l2tp_session *session;
498
499 if (sk->sk_user_data != NULL) {
500 session = sk->sk_user_data;
501 if (session == NULL)
502 goto out;
503
504 sk->sk_user_data = NULL;
505 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
506 l2tp_session_dec_refcount(session);
507 }
508
509out:
510 return;
511}
512
513/* Called when the PPPoX socket (session) is closed.
514 */
515static int pppol2tp_release(struct socket *sock)
516{
517 struct sock *sk = sock->sk;
518 struct l2tp_session *session;
519 int error;
520
521 if (!sk)
522 return 0;
523
524 error = -EBADF;
525 lock_sock(sk);
526 if (sock_flag(sk, SOCK_DEAD) != 0)
527 goto error;
528
529 pppox_unbind_sock(sk);
530
531 /* Signal the death of the socket. */
532 sk->sk_state = PPPOX_DEAD;
533 sock_orphan(sk);
534 sock->sk = NULL;
535
536 session = pppol2tp_sock_to_session(sk);
537
538 /* Purge any queued data */
539 skb_queue_purge(&sk->sk_receive_queue);
540 skb_queue_purge(&sk->sk_write_queue);
541 if (session != NULL) {
542 struct sk_buff *skb;
543 while ((skb = skb_dequeue(&session->reorder_q))) {
544 kfree_skb(skb);
545 sock_put(sk);
546 }
547 sock_put(sk);
548 }
549
550 release_sock(sk);
551
552 /* This will delete the session context via
553 * pppol2tp_session_destruct() if the socket's refcnt drops to
554 * zero.
555 */
556 sock_put(sk);
557
558 return 0;
559
560error:
561 release_sock(sk);
562 return error;
563}
564
565static struct proto pppol2tp_sk_proto = {
566 .name = "PPPOL2TP",
567 .owner = THIS_MODULE,
568 .obj_size = sizeof(struct pppox_sock),
569};
570
571static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
572{
573 int rc;
574
575 rc = l2tp_udp_encap_recv(sk, skb);
576 if (rc)
577 kfree_skb(skb);
578
579 return NET_RX_SUCCESS;
580}
581
582/* socket() handler. Initialize a new struct sock.
583 */
584static int pppol2tp_create(struct net *net, struct socket *sock)
585{
586 int error = -ENOMEM;
587 struct sock *sk;
588
589 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
590 if (!sk)
591 goto out;
592
593 sock_init_data(sock, sk);
594
595 sock->state = SS_UNCONNECTED;
596 sock->ops = &pppol2tp_ops;
597
598 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
599 sk->sk_protocol = PX_PROTO_OL2TP;
600 sk->sk_family = PF_PPPOX;
601 sk->sk_state = PPPOX_NONE;
602 sk->sk_type = SOCK_STREAM;
603 sk->sk_destruct = pppol2tp_session_destruct;
604
605 error = 0;
606
607out:
608 return error;
609}
610
611/* 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;
622 struct dst_entry *dst;
623 struct l2tp_session_cfg cfg = { 0, };
624 int error = 0;
625
626 lock_sock(sk);
627
628 error = -EINVAL;
629 if (sp->sa_protocol != PX_PROTO_OL2TP)
630 goto end;
631
632 /* Check for already bound sockets */
633 error = -EBUSY;
634 if (sk->sk_state & PPPOX_CONNECTED)
635 goto end;
636
637 /* We don't supporting rebinding anyway */
638 error = -EALREADY;
639 if (sk->sk_user_data)
640 goto end; /* socket is already attached */
641
642 /* Don't bind if s_tunnel is 0 */
643 error = -EINVAL;
644 if (sp->pppol2tp.s_tunnel == 0)
645 goto end;
646
647 /* Special case: create tunnel context if s_session and
648 * d_session is 0. Otherwise look up tunnel using supplied
649 * tunnel id.
650 */
651 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
652 error = l2tp_tunnel_create(sock_net(sk), sp->pppol2tp.fd, 2, sp->pppol2tp.s_tunnel, sp->pppol2tp.d_tunnel, NULL, &tunnel);
653 if (error < 0)
654 goto end;
655 } else {
656 tunnel = l2tp_tunnel_find(sock_net(sk), sp->pppol2tp.s_tunnel);
657
658 /* Error if we can't find the tunnel */
659 error = -ENOENT;
660 if (tunnel == NULL)
661 goto end;
662
663 /* Error if socket is not prepped */
664 if (tunnel->sock == NULL)
665 goto end;
666 }
667
668 if (tunnel->recv_payload_hook == NULL)
669 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
670
671 /* Check that this session doesn't already exist */
672 error = -EEXIST;
f7faffa3 673 session = l2tp_session_find(sock_net(sk), tunnel, sp->pppol2tp.s_session);
fd558d18
JC
674 if (session != NULL)
675 goto end;
676
677 /* Default MTU must allow space for UDP/L2TP/PPP
678 * headers.
679 */
680 cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
fd558d18
JC
681 cfg.debug = tunnel->debug;
682
683 /* Allocate and initialize a new session context. */
684 session = l2tp_session_create(sizeof(struct pppol2tp_session),
685 tunnel, sp->pppol2tp.s_session,
686 sp->pppol2tp.d_session, &cfg);
687 if (session == NULL) {
688 error = -ENOMEM;
689 goto end;
690 }
691
692 ps = l2tp_session_priv(session);
693 ps->owner = current->pid;
694 ps->sock = sk;
695 ps->tunnel_sock = tunnel->sock;
696
697 session->recv_skb = pppol2tp_recv;
698 session->session_close = pppol2tp_session_close;
699
700 /* We need to know each time a skb is dropped from the reorder
701 * queue.
702 */
703 session->ref = pppol2tp_session_sock_hold;
704 session->deref = pppol2tp_session_sock_put;
705
706 /* If PMTU discovery was enabled, use the MTU that was discovered */
707 dst = sk_dst_get(sk);
708 if (dst != NULL) {
709 u32 pmtu = dst_mtu(__sk_dst_get(sk));
710 if (pmtu != 0)
711 session->mtu = session->mru = pmtu -
712 PPPOL2TP_HEADER_OVERHEAD;
713 dst_release(dst);
714 }
715
716 /* Special case: if source & dest session_id == 0x0000, this
717 * socket is being created to manage the tunnel. Just set up
718 * the internal context for use by ioctl() and sockopt()
719 * handlers.
720 */
721 if ((session->session_id == 0) &&
722 (session->peer_session_id == 0)) {
723 error = 0;
724 goto out_no_ppp;
725 }
726
727 /* The only header we need to worry about is the L2TP
728 * header. This size is different depending on whether
729 * sequence numbers are enabled for the data channel.
730 */
731 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
732
733 po->chan.private = sk;
734 po->chan.ops = &pppol2tp_chan_ops;
735 po->chan.mtu = session->mtu;
736
737 error = ppp_register_net_channel(sock_net(sk), &po->chan);
738 if (error)
739 goto end;
740
741out_no_ppp:
742 /* This is how we get the session context from the socket. */
743 sk->sk_user_data = session;
744 sk->sk_state = PPPOX_CONNECTED;
745 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
746 "%s: created\n", session->name);
747
748end:
749 release_sock(sk);
750
751 return error;
752}
753
754/* getname() support.
755 */
756static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
757 int *usockaddr_len, int peer)
758{
759 int len = sizeof(struct sockaddr_pppol2tp);
760 struct sockaddr_pppol2tp sp;
761 int error = 0;
762 struct l2tp_session *session;
763 struct l2tp_tunnel *tunnel;
764 struct sock *sk = sock->sk;
765 struct inet_sock *inet;
766 struct pppol2tp_session *pls;
767
768 error = -ENOTCONN;
769 if (sk == NULL)
770 goto end;
771 if (sk->sk_state != PPPOX_CONNECTED)
772 goto end;
773
774 error = -EBADF;
775 session = pppol2tp_sock_to_session(sk);
776 if (session == NULL)
777 goto end;
778
779 pls = l2tp_session_priv(session);
780 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
781 if (tunnel == NULL) {
782 error = -EBADF;
783 goto end_put_sess;
784 }
785
786 memset(&sp, 0, len);
787 sp.sa_family = AF_PPPOX;
788 sp.sa_protocol = PX_PROTO_OL2TP;
789 sp.pppol2tp.fd = tunnel->fd;
790 sp.pppol2tp.pid = pls->owner;
791 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
792 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
793 sp.pppol2tp.s_session = session->session_id;
794 sp.pppol2tp.d_session = session->peer_session_id;
795 inet = inet_sk(sk);
796 sp.pppol2tp.addr.sin_family = AF_INET;
797 sp.pppol2tp.addr.sin_port = inet->inet_dport;
798 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
799
800 memcpy(uaddr, &sp, len);
801
802 *usockaddr_len = len;
803
804 sock_put(pls->tunnel_sock);
805end_put_sess:
806 sock_put(sk);
807 error = 0;
808
809end:
810 return error;
811}
812
813/****************************************************************************
814 * ioctl() handlers.
815 *
816 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
817 * sockets. However, in order to control kernel tunnel features, we allow
818 * userspace to create a special "tunnel" PPPoX socket which is used for
819 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
820 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
821 * calls.
822 ****************************************************************************/
823
824static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
825 struct l2tp_stats *stats)
826{
827 dest->tx_packets = stats->tx_packets;
828 dest->tx_bytes = stats->tx_bytes;
829 dest->tx_errors = stats->tx_errors;
830 dest->rx_packets = stats->rx_packets;
831 dest->rx_bytes = stats->rx_bytes;
832 dest->rx_seq_discards = stats->rx_seq_discards;
833 dest->rx_oos_packets = stats->rx_oos_packets;
834 dest->rx_errors = stats->rx_errors;
835}
836
837/* Session ioctl helper.
838 */
839static int pppol2tp_session_ioctl(struct l2tp_session *session,
840 unsigned int cmd, unsigned long arg)
841{
842 struct ifreq ifr;
843 int err = 0;
844 struct sock *sk;
845 int val = (int) arg;
846 struct pppol2tp_session *ps = l2tp_session_priv(session);
847 struct l2tp_tunnel *tunnel = session->tunnel;
848 struct pppol2tp_ioc_stats stats;
849
850 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
851 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
852 session->name, cmd, arg);
853
854 sk = ps->sock;
855 sock_hold(sk);
856
857 switch (cmd) {
858 case SIOCGIFMTU:
859 err = -ENXIO;
860 if (!(sk->sk_state & PPPOX_CONNECTED))
861 break;
862
863 err = -EFAULT;
864 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
865 break;
866 ifr.ifr_mtu = session->mtu;
867 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
868 break;
869
870 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
871 "%s: get mtu=%d\n", session->name, session->mtu);
872 err = 0;
873 break;
874
875 case SIOCSIFMTU:
876 err = -ENXIO;
877 if (!(sk->sk_state & PPPOX_CONNECTED))
878 break;
879
880 err = -EFAULT;
881 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
882 break;
883
884 session->mtu = ifr.ifr_mtu;
885
886 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
887 "%s: set mtu=%d\n", session->name, session->mtu);
888 err = 0;
889 break;
890
891 case PPPIOCGMRU:
892 err = -ENXIO;
893 if (!(sk->sk_state & PPPOX_CONNECTED))
894 break;
895
896 err = -EFAULT;
897 if (put_user(session->mru, (int __user *) arg))
898 break;
899
900 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
901 "%s: get mru=%d\n", session->name, session->mru);
902 err = 0;
903 break;
904
905 case PPPIOCSMRU:
906 err = -ENXIO;
907 if (!(sk->sk_state & PPPOX_CONNECTED))
908 break;
909
910 err = -EFAULT;
911 if (get_user(val, (int __user *) arg))
912 break;
913
914 session->mru = val;
915 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
916 "%s: set mru=%d\n", session->name, session->mru);
917 err = 0;
918 break;
919
920 case PPPIOCGFLAGS:
921 err = -EFAULT;
922 if (put_user(ps->flags, (int __user *) arg))
923 break;
924
925 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
926 "%s: get flags=%d\n", session->name, ps->flags);
927 err = 0;
928 break;
929
930 case PPPIOCSFLAGS:
931 err = -EFAULT;
932 if (get_user(val, (int __user *) arg))
933 break;
934 ps->flags = val;
935 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
936 "%s: set flags=%d\n", session->name, ps->flags);
937 err = 0;
938 break;
939
940 case PPPIOCGL2TPSTATS:
941 err = -ENXIO;
942 if (!(sk->sk_state & PPPOX_CONNECTED))
943 break;
944
945 memset(&stats, 0, sizeof(stats));
946 stats.tunnel_id = tunnel->tunnel_id;
947 stats.session_id = session->session_id;
948 pppol2tp_copy_stats(&stats, &session->stats);
949 if (copy_to_user((void __user *) arg, &stats,
950 sizeof(stats)))
951 break;
952 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
953 "%s: get L2TP stats\n", session->name);
954 err = 0;
955 break;
956
957 default:
958 err = -ENOSYS;
959 break;
960 }
961
962 sock_put(sk);
963
964 return err;
965}
966
967/* Tunnel ioctl helper.
968 *
969 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
970 * specifies a session_id, the session ioctl handler is called. This allows an
971 * application to retrieve session stats via a tunnel socket.
972 */
973static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
974 unsigned int cmd, unsigned long arg)
975{
976 int err = 0;
977 struct sock *sk;
978 struct pppol2tp_ioc_stats stats;
979
980 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
981 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
982 tunnel->name, cmd, arg);
983
984 sk = tunnel->sock;
985 sock_hold(sk);
986
987 switch (cmd) {
988 case PPPIOCGL2TPSTATS:
989 err = -ENXIO;
990 if (!(sk->sk_state & PPPOX_CONNECTED))
991 break;
992
993 if (copy_from_user(&stats, (void __user *) arg,
994 sizeof(stats))) {
995 err = -EFAULT;
996 break;
997 }
998 if (stats.session_id != 0) {
999 /* resend to session ioctl handler */
1000 struct l2tp_session *session =
f7faffa3 1001 l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
fd558d18
JC
1002 if (session != NULL)
1003 err = pppol2tp_session_ioctl(session, cmd, arg);
1004 else
1005 err = -EBADR;
1006 break;
1007 }
1008#ifdef CONFIG_XFRM
1009 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1010#endif
1011 pppol2tp_copy_stats(&stats, &tunnel->stats);
1012 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1013 err = -EFAULT;
1014 break;
1015 }
1016 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1017 "%s: get L2TP stats\n", tunnel->name);
1018 err = 0;
1019 break;
1020
1021 default:
1022 err = -ENOSYS;
1023 break;
1024 }
1025
1026 sock_put(sk);
1027
1028 return err;
1029}
1030
1031/* Main ioctl() handler.
1032 * Dispatch to tunnel or session helpers depending on the socket.
1033 */
1034static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1035 unsigned long arg)
1036{
1037 struct sock *sk = sock->sk;
1038 struct l2tp_session *session;
1039 struct l2tp_tunnel *tunnel;
1040 struct pppol2tp_session *ps;
1041 int err;
1042
1043 if (!sk)
1044 return 0;
1045
1046 err = -EBADF;
1047 if (sock_flag(sk, SOCK_DEAD) != 0)
1048 goto end;
1049
1050 err = -ENOTCONN;
1051 if ((sk->sk_user_data == NULL) ||
1052 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1053 goto end;
1054
1055 /* Get session context from the socket */
1056 err = -EBADF;
1057 session = pppol2tp_sock_to_session(sk);
1058 if (session == NULL)
1059 goto end;
1060
1061 /* Special case: if session's session_id is zero, treat ioctl as a
1062 * tunnel ioctl
1063 */
1064 ps = l2tp_session_priv(session);
1065 if ((session->session_id == 0) &&
1066 (session->peer_session_id == 0)) {
1067 err = -EBADF;
1068 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1069 if (tunnel == NULL)
1070 goto end_put_sess;
1071
1072 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1073 sock_put(ps->tunnel_sock);
1074 goto end_put_sess;
1075 }
1076
1077 err = pppol2tp_session_ioctl(session, cmd, arg);
1078
1079end_put_sess:
1080 sock_put(sk);
1081end:
1082 return err;
1083}
1084
1085/*****************************************************************************
1086 * setsockopt() / getsockopt() support.
1087 *
1088 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1089 * sockets. In order to control kernel tunnel features, we allow userspace to
1090 * create a special "tunnel" PPPoX socket which is used for control only.
1091 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1092 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1093 *****************************************************************************/
1094
1095/* Tunnel setsockopt() helper.
1096 */
1097static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1098 struct l2tp_tunnel *tunnel,
1099 int optname, int val)
1100{
1101 int err = 0;
1102
1103 switch (optname) {
1104 case PPPOL2TP_SO_DEBUG:
1105 tunnel->debug = val;
1106 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1107 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1108 break;
1109
1110 default:
1111 err = -ENOPROTOOPT;
1112 break;
1113 }
1114
1115 return err;
1116}
1117
1118/* Session setsockopt helper.
1119 */
1120static int pppol2tp_session_setsockopt(struct sock *sk,
1121 struct l2tp_session *session,
1122 int optname, int val)
1123{
1124 int err = 0;
1125 struct pppol2tp_session *ps = l2tp_session_priv(session);
1126
1127 switch (optname) {
1128 case PPPOL2TP_SO_RECVSEQ:
1129 if ((val != 0) && (val != 1)) {
1130 err = -EINVAL;
1131 break;
1132 }
1133 session->recv_seq = val ? -1 : 0;
1134 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1135 "%s: set recv_seq=%d\n", session->name, session->recv_seq);
1136 break;
1137
1138 case PPPOL2TP_SO_SENDSEQ:
1139 if ((val != 0) && (val != 1)) {
1140 err = -EINVAL;
1141 break;
1142 }
1143 session->send_seq = val ? -1 : 0;
1144 {
1145 struct sock *ssk = ps->sock;
1146 struct pppox_sock *po = pppox_sk(ssk);
1147 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1148 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1149 }
1150 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1151 "%s: set send_seq=%d\n", session->name, session->send_seq);
1152 break;
1153
1154 case PPPOL2TP_SO_LNSMODE:
1155 if ((val != 0) && (val != 1)) {
1156 err = -EINVAL;
1157 break;
1158 }
1159 session->lns_mode = val ? -1 : 0;
1160 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1161 "%s: set lns_mode=%d\n", session->name, session->lns_mode);
1162 break;
1163
1164 case PPPOL2TP_SO_DEBUG:
1165 session->debug = val;
1166 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1167 "%s: set debug=%x\n", session->name, session->debug);
1168 break;
1169
1170 case PPPOL2TP_SO_REORDERTO:
1171 session->reorder_timeout = msecs_to_jiffies(val);
1172 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1173 "%s: set reorder_timeout=%d\n", session->name, session->reorder_timeout);
1174 break;
1175
1176 default:
1177 err = -ENOPROTOOPT;
1178 break;
1179 }
1180
1181 return err;
1182}
1183
1184/* Main setsockopt() entry point.
1185 * Does API checks, then calls either the tunnel or session setsockopt
1186 * handler, according to whether the PPPoL2TP socket is a for a regular
1187 * session or the special tunnel type.
1188 */
1189static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1190 char __user *optval, unsigned int optlen)
1191{
1192 struct sock *sk = sock->sk;
1193 struct l2tp_session *session;
1194 struct l2tp_tunnel *tunnel;
1195 struct pppol2tp_session *ps;
1196 int val;
1197 int err;
1198
1199 if (level != SOL_PPPOL2TP)
1200 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1201
1202 if (optlen < sizeof(int))
1203 return -EINVAL;
1204
1205 if (get_user(val, (int __user *)optval))
1206 return -EFAULT;
1207
1208 err = -ENOTCONN;
1209 if (sk->sk_user_data == NULL)
1210 goto end;
1211
1212 /* Get session context from the socket */
1213 err = -EBADF;
1214 session = pppol2tp_sock_to_session(sk);
1215 if (session == NULL)
1216 goto end;
1217
1218 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1219 */
1220 ps = l2tp_session_priv(session);
1221 if ((session->session_id == 0) &&
1222 (session->peer_session_id == 0)) {
1223 err = -EBADF;
1224 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1225 if (tunnel == NULL)
1226 goto end_put_sess;
1227
1228 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1229 sock_put(ps->tunnel_sock);
1230 } else
1231 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1232
1233 err = 0;
1234
1235end_put_sess:
1236 sock_put(sk);
1237end:
1238 return err;
1239}
1240
1241/* Tunnel getsockopt helper. Called with sock locked.
1242 */
1243static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1244 struct l2tp_tunnel *tunnel,
1245 int optname, int *val)
1246{
1247 int err = 0;
1248
1249 switch (optname) {
1250 case PPPOL2TP_SO_DEBUG:
1251 *val = tunnel->debug;
1252 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1253 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
1254 break;
1255
1256 default:
1257 err = -ENOPROTOOPT;
1258 break;
1259 }
1260
1261 return err;
1262}
1263
1264/* Session getsockopt helper. Called with sock locked.
1265 */
1266static int pppol2tp_session_getsockopt(struct sock *sk,
1267 struct l2tp_session *session,
1268 int optname, int *val)
1269{
1270 int err = 0;
1271
1272 switch (optname) {
1273 case PPPOL2TP_SO_RECVSEQ:
1274 *val = session->recv_seq;
1275 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1276 "%s: get recv_seq=%d\n", session->name, *val);
1277 break;
1278
1279 case PPPOL2TP_SO_SENDSEQ:
1280 *val = session->send_seq;
1281 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1282 "%s: get send_seq=%d\n", session->name, *val);
1283 break;
1284
1285 case PPPOL2TP_SO_LNSMODE:
1286 *val = session->lns_mode;
1287 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1288 "%s: get lns_mode=%d\n", session->name, *val);
1289 break;
1290
1291 case PPPOL2TP_SO_DEBUG:
1292 *val = session->debug;
1293 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1294 "%s: get debug=%d\n", session->name, *val);
1295 break;
1296
1297 case PPPOL2TP_SO_REORDERTO:
1298 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1299 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1300 "%s: get reorder_timeout=%d\n", session->name, *val);
1301 break;
1302
1303 default:
1304 err = -ENOPROTOOPT;
1305 }
1306
1307 return err;
1308}
1309
1310/* Main getsockopt() entry point.
1311 * Does API checks, then calls either the tunnel or session getsockopt
1312 * handler, according to whether the PPPoX socket is a for a regular session
1313 * or the special tunnel type.
1314 */
1315static int pppol2tp_getsockopt(struct socket *sock, int level,
1316 int optname, char __user *optval, int __user *optlen)
1317{
1318 struct sock *sk = sock->sk;
1319 struct l2tp_session *session;
1320 struct l2tp_tunnel *tunnel;
1321 int val, len;
1322 int err;
1323 struct pppol2tp_session *ps;
1324
1325 if (level != SOL_PPPOL2TP)
1326 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1327
1328 if (get_user(len, (int __user *) optlen))
1329 return -EFAULT;
1330
1331 len = min_t(unsigned int, len, sizeof(int));
1332
1333 if (len < 0)
1334 return -EINVAL;
1335
1336 err = -ENOTCONN;
1337 if (sk->sk_user_data == NULL)
1338 goto end;
1339
1340 /* Get the session context */
1341 err = -EBADF;
1342 session = pppol2tp_sock_to_session(sk);
1343 if (session == NULL)
1344 goto end;
1345
1346 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1347 ps = l2tp_session_priv(session);
1348 if ((session->session_id == 0) &&
1349 (session->peer_session_id == 0)) {
1350 err = -EBADF;
1351 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1352 if (tunnel == NULL)
1353 goto end_put_sess;
1354
1355 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1356 sock_put(ps->tunnel_sock);
1357 } else
1358 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1359
1360 err = -EFAULT;
1361 if (put_user(len, (int __user *) optlen))
1362 goto end_put_sess;
1363
1364 if (copy_to_user((void __user *) optval, &val, len))
1365 goto end_put_sess;
1366
1367 err = 0;
1368
1369end_put_sess:
1370 sock_put(sk);
1371end:
1372 return err;
1373}
1374
1375/*****************************************************************************
1376 * /proc filesystem for debug
f7faffa3
JC
1377 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1378 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
fd558d18
JC
1379 *****************************************************************************/
1380
1381static unsigned int pppol2tp_net_id;
1382
1383#ifdef CONFIG_PROC_FS
1384
1385struct pppol2tp_seq_data {
1386 struct seq_net_private p;
1387 int tunnel_idx; /* current tunnel */
1388 int session_idx; /* index of session within current tunnel */
1389 struct l2tp_tunnel *tunnel;
1390 struct l2tp_session *session; /* NULL means get next tunnel */
1391};
1392
1393static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1394{
f7faffa3
JC
1395 for (;;) {
1396 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1397 pd->tunnel_idx++;
1398
1399 if (pd->tunnel == NULL)
1400 break;
1401
1402 /* Ignore L2TPv3 tunnels */
1403 if (pd->tunnel->version < 3)
1404 break;
1405 }
fd558d18
JC
1406}
1407
1408static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1409{
1410 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1411 pd->session_idx++;
f7faffa3 1412
fd558d18
JC
1413 if (pd->session == NULL) {
1414 pd->session_idx = 0;
1415 pppol2tp_next_tunnel(net, pd);
1416 }
1417}
1418
1419static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1420{
1421 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1422 loff_t pos = *offs;
1423 struct net *net;
1424
1425 if (!pos)
1426 goto out;
1427
1428 BUG_ON(m->private == NULL);
1429 pd = m->private;
1430 net = seq_file_net(m);
1431
1432 if (pd->tunnel == NULL)
1433 pppol2tp_next_tunnel(net, pd);
1434 else
1435 pppol2tp_next_session(net, pd);
1436
1437 /* NULL tunnel and session indicates end of list */
1438 if ((pd->tunnel == NULL) && (pd->session == NULL))
1439 pd = NULL;
1440
1441out:
1442 return pd;
1443}
1444
1445static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1446{
1447 (*pos)++;
1448 return NULL;
1449}
1450
1451static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1452{
1453 /* nothing to do */
1454}
1455
1456static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1457{
1458 struct l2tp_tunnel *tunnel = v;
1459
1460 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1461 tunnel->name,
1462 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1463 atomic_read(&tunnel->ref_count) - 1);
1464 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1465 tunnel->debug,
1466 (unsigned long long)tunnel->stats.tx_packets,
1467 (unsigned long long)tunnel->stats.tx_bytes,
1468 (unsigned long long)tunnel->stats.tx_errors,
1469 (unsigned long long)tunnel->stats.rx_packets,
1470 (unsigned long long)tunnel->stats.rx_bytes,
1471 (unsigned long long)tunnel->stats.rx_errors);
1472}
1473
1474static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1475{
1476 struct l2tp_session *session = v;
1477 struct l2tp_tunnel *tunnel = session->tunnel;
1478 struct pppol2tp_session *ps = l2tp_session_priv(session);
9345471b 1479 struct pppox_sock *po = pppox_sk(ps->sock);
fd558d18
JC
1480 u32 ip = 0;
1481 u16 port = 0;
1482
1483 if (tunnel->sock) {
1484 struct inet_sock *inet = inet_sk(tunnel->sock);
1485 ip = ntohl(inet->inet_saddr);
1486 port = ntohs(inet->inet_sport);
1487 }
1488
1489 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1490 "%04X/%04X %d %c\n",
1491 session->name, ip, port,
1492 tunnel->tunnel_id,
1493 session->session_id,
1494 tunnel->peer_tunnel_id,
1495 session->peer_session_id,
1496 ps->sock->sk_state,
1497 (session == ps->sock->sk_user_data) ?
1498 'Y' : 'N');
1499 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1500 session->mtu, session->mru,
1501 session->recv_seq ? 'R' : '-',
1502 session->send_seq ? 'S' : '-',
1503 session->lns_mode ? "LNS" : "LAC",
1504 session->debug,
1505 jiffies_to_msecs(session->reorder_timeout));
1506 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1507 session->nr, session->ns,
1508 (unsigned long long)session->stats.tx_packets,
1509 (unsigned long long)session->stats.tx_bytes,
1510 (unsigned long long)session->stats.tx_errors,
1511 (unsigned long long)session->stats.rx_packets,
1512 (unsigned long long)session->stats.rx_bytes,
1513 (unsigned long long)session->stats.rx_errors);
9345471b
JC
1514
1515 if (po)
1516 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
fd558d18
JC
1517}
1518
1519static int pppol2tp_seq_show(struct seq_file *m, void *v)
1520{
1521 struct pppol2tp_seq_data *pd = v;
1522
1523 /* display header on line 1 */
1524 if (v == SEQ_START_TOKEN) {
1525 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1526 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1527 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1528 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1529 "dest-tid/sid state user-data-ok\n");
1530 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1531 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1532 goto out;
1533 }
1534
1535 /* Show the tunnel or session context.
1536 */
1537 if (pd->session == NULL)
1538 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1539 else
1540 pppol2tp_seq_session_show(m, pd->session);
1541
1542out:
1543 return 0;
1544}
1545
1546static const struct seq_operations pppol2tp_seq_ops = {
1547 .start = pppol2tp_seq_start,
1548 .next = pppol2tp_seq_next,
1549 .stop = pppol2tp_seq_stop,
1550 .show = pppol2tp_seq_show,
1551};
1552
1553/* Called when our /proc file is opened. We allocate data for use when
1554 * iterating our tunnel / session contexts and store it in the private
1555 * data of the seq_file.
1556 */
1557static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1558{
1559 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1560 sizeof(struct pppol2tp_seq_data));
1561}
1562
1563static const struct file_operations pppol2tp_proc_fops = {
1564 .owner = THIS_MODULE,
1565 .open = pppol2tp_proc_open,
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .release = seq_release_net,
1569};
1570
1571#endif /* CONFIG_PROC_FS */
1572
1573/*****************************************************************************
1574 * Network namespace
1575 *****************************************************************************/
1576
1577static __net_init int pppol2tp_init_net(struct net *net)
1578{
1579 struct proc_dir_entry *pde;
1580 int err = 0;
1581
1582 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1583 if (!pde) {
1584 err = -ENOMEM;
1585 goto out;
1586 }
1587
1588out:
1589 return err;
1590}
1591
1592static __net_exit void pppol2tp_exit_net(struct net *net)
1593{
1594 proc_net_remove(net, "pppol2tp");
1595}
1596
1597static struct pernet_operations pppol2tp_net_ops = {
1598 .init = pppol2tp_init_net,
1599 .exit = pppol2tp_exit_net,
1600 .id = &pppol2tp_net_id,
1601};
1602
1603/*****************************************************************************
1604 * Init and cleanup
1605 *****************************************************************************/
1606
1607static const struct proto_ops pppol2tp_ops = {
1608 .family = AF_PPPOX,
1609 .owner = THIS_MODULE,
1610 .release = pppol2tp_release,
1611 .bind = sock_no_bind,
1612 .connect = pppol2tp_connect,
1613 .socketpair = sock_no_socketpair,
1614 .accept = sock_no_accept,
1615 .getname = pppol2tp_getname,
1616 .poll = datagram_poll,
1617 .listen = sock_no_listen,
1618 .shutdown = sock_no_shutdown,
1619 .setsockopt = pppol2tp_setsockopt,
1620 .getsockopt = pppol2tp_getsockopt,
1621 .sendmsg = pppol2tp_sendmsg,
1622 .recvmsg = pppol2tp_recvmsg,
1623 .mmap = sock_no_mmap,
1624 .ioctl = pppox_ioctl,
1625};
1626
1627static struct pppox_proto pppol2tp_proto = {
1628 .create = pppol2tp_create,
1629 .ioctl = pppol2tp_ioctl
1630};
1631
1632static int __init pppol2tp_init(void)
1633{
1634 int err;
1635
1636 err = register_pernet_device(&pppol2tp_net_ops);
1637 if (err)
1638 goto out;
1639
1640 err = proto_register(&pppol2tp_sk_proto, 0);
1641 if (err)
1642 goto out_unregister_pppol2tp_pernet;
1643
1644 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1645 if (err)
1646 goto out_unregister_pppol2tp_proto;
1647
1648 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
1649 PPPOL2TP_DRV_VERSION);
1650
1651out:
1652 return err;
1653out_unregister_pppol2tp_proto:
1654 proto_unregister(&pppol2tp_sk_proto);
1655out_unregister_pppol2tp_pernet:
1656 unregister_pernet_device(&pppol2tp_net_ops);
1657 goto out;
1658}
1659
1660static void __exit pppol2tp_exit(void)
1661{
1662 unregister_pppox_proto(PX_PROTO_OL2TP);
1663 proto_unregister(&pppol2tp_sk_proto);
1664 unregister_pernet_device(&pppol2tp_net_ops);
1665}
1666
1667module_init(pppol2tp_init);
1668module_exit(pppol2tp_exit);
1669
1670MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1671MODULE_DESCRIPTION("PPP over L2TP over UDP");
1672MODULE_LICENSE("GPL");
1673MODULE_VERSION(PPPOL2TP_DRV_VERSION);