aboutsummaryrefslogtreecommitdiffstats
path: root/net/l2tp/l2tp_ppp.c
diff options
context:
space:
mode:
authorJames Chapman <jchapman@katalix.com>2010-04-02 02:18:33 -0400
committerDavid S. Miller <davem@davemloft.net>2010-04-03 17:56:02 -0400
commitfd558d186df2c13a22455373858bae634a4795af (patch)
treeed05523913473b6ba7658c6e8d72a1642cce1851 /net/l2tp/l2tp_ppp.c
parent21b4aaa14329db793832e865f15000c5c0192ac3 (diff)
l2tp: Split pppol2tp patch into separate l2tp and ppp parts
This patch splits the pppol2tp driver into separate L2TP and PPP parts to prepare for L2TPv3 support. In L2TPv3, protocols other than PPP can be carried, so this split creates a common L2TP core that will handle the common L2TP bits which protocol support modules such as PPP will use. Note that the existing pppol2tp module is split into l2tp_core and l2tp_ppp by this change. There are no feature changes here. Internally, however, there are significant changes, mostly to handle the separation of PPP-specific data from the L2TP session and to provide hooks in the core for modules like PPP to access. Signed-off-by: James Chapman <jchapman@katalix.com> Reviewed-by: Randy Dunlap <randy.dunlap@oracle.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/l2tp/l2tp_ppp.c')
-rw-r--r--net/l2tp/l2tp_ppp.c1658
1 files changed, 1658 insertions, 0 deletions
diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c
new file mode 100644
index 000000000000..baac072761aa
--- /dev/null
+++ b/net/l2tp/l2tp_ppp.c
@@ -0,0 +1,1658 @@
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;
673 session = l2tp_session_find(tunnel, sp->pppol2tp.s_session);
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;
681 cfg.hdr_len = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
682 cfg.debug = tunnel->debug;
683
684 /* Allocate and initialize a new session context. */
685 session = l2tp_session_create(sizeof(struct pppol2tp_session),
686 tunnel, sp->pppol2tp.s_session,
687 sp->pppol2tp.d_session, &cfg);
688 if (session == NULL) {
689 error = -ENOMEM;
690 goto end;
691 }
692
693 ps = l2tp_session_priv(session);
694 ps->owner = current->pid;
695 ps->sock = sk;
696 ps->tunnel_sock = tunnel->sock;
697
698 session->recv_skb = pppol2tp_recv;
699 session->session_close = pppol2tp_session_close;
700
701 /* We need to know each time a skb is dropped from the reorder
702 * queue.
703 */
704 session->ref = pppol2tp_session_sock_hold;
705 session->deref = pppol2tp_session_sock_put;
706
707 /* If PMTU discovery was enabled, use the MTU that was discovered */
708 dst = sk_dst_get(sk);
709 if (dst != NULL) {
710 u32 pmtu = dst_mtu(__sk_dst_get(sk));
711 if (pmtu != 0)
712 session->mtu = session->mru = pmtu -
713 PPPOL2TP_HEADER_OVERHEAD;
714 dst_release(dst);
715 }
716
717 /* Special case: if source & dest session_id == 0x0000, this
718 * socket is being created to manage the tunnel. Just set up
719 * the internal context for use by ioctl() and sockopt()
720 * handlers.
721 */
722 if ((session->session_id == 0) &&
723 (session->peer_session_id == 0)) {
724 error = 0;
725 goto out_no_ppp;
726 }
727
728 /* The only header we need to worry about is the L2TP
729 * header. This size is different depending on whether
730 * sequence numbers are enabled for the data channel.
731 */
732 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
733
734 po->chan.private = sk;
735 po->chan.ops = &pppol2tp_chan_ops;
736 po->chan.mtu = session->mtu;
737
738 error = ppp_register_net_channel(sock_net(sk), &po->chan);
739 if (error)
740 goto end;
741
742out_no_ppp:
743 /* This is how we get the session context from the socket. */
744 sk->sk_user_data = session;
745 sk->sk_state = PPPOX_CONNECTED;
746 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
747 "%s: created\n", session->name);
748
749end:
750 release_sock(sk);
751
752 return error;
753}
754
755/* getname() support.
756 */
757static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
758 int *usockaddr_len, int peer)
759{
760 int len = sizeof(struct sockaddr_pppol2tp);
761 struct sockaddr_pppol2tp sp;
762 int error = 0;
763 struct l2tp_session *session;
764 struct l2tp_tunnel *tunnel;
765 struct sock *sk = sock->sk;
766 struct inet_sock *inet;
767 struct pppol2tp_session *pls;
768
769 error = -ENOTCONN;
770 if (sk == NULL)
771 goto end;
772 if (sk->sk_state != PPPOX_CONNECTED)
773 goto end;
774
775 error = -EBADF;
776 session = pppol2tp_sock_to_session(sk);
777 if (session == NULL)
778 goto end;
779
780 pls = l2tp_session_priv(session);
781 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
782 if (tunnel == NULL) {
783 error = -EBADF;
784 goto end_put_sess;
785 }
786
787 memset(&sp, 0, len);
788 sp.sa_family = AF_PPPOX;
789 sp.sa_protocol = PX_PROTO_OL2TP;
790 sp.pppol2tp.fd = tunnel->fd;
791 sp.pppol2tp.pid = pls->owner;
792 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
793 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
794 sp.pppol2tp.s_session = session->session_id;
795 sp.pppol2tp.d_session = session->peer_session_id;
796 inet = inet_sk(sk);
797 sp.pppol2tp.addr.sin_family = AF_INET;
798 sp.pppol2tp.addr.sin_port = inet->inet_dport;
799 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
800
801 memcpy(uaddr, &sp, len);
802
803 *usockaddr_len = len;
804
805 sock_put(pls->tunnel_sock);
806end_put_sess:
807 sock_put(sk);
808 error = 0;
809
810end:
811 return error;
812}
813
814/****************************************************************************
815 * ioctl() handlers.
816 *
817 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
818 * sockets. However, in order to control kernel tunnel features, we allow
819 * userspace to create a special "tunnel" PPPoX socket which is used for
820 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
821 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
822 * calls.
823 ****************************************************************************/
824
825static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
826 struct l2tp_stats *stats)
827{
828 dest->tx_packets = stats->tx_packets;
829 dest->tx_bytes = stats->tx_bytes;
830 dest->tx_errors = stats->tx_errors;
831 dest->rx_packets = stats->rx_packets;
832 dest->rx_bytes = stats->rx_bytes;
833 dest->rx_seq_discards = stats->rx_seq_discards;
834 dest->rx_oos_packets = stats->rx_oos_packets;
835 dest->rx_errors = stats->rx_errors;
836}
837
838/* Session ioctl helper.
839 */
840static int pppol2tp_session_ioctl(struct l2tp_session *session,
841 unsigned int cmd, unsigned long arg)
842{
843 struct ifreq ifr;
844 int err = 0;
845 struct sock *sk;
846 int val = (int) arg;
847 struct pppol2tp_session *ps = l2tp_session_priv(session);
848 struct l2tp_tunnel *tunnel = session->tunnel;
849 struct pppol2tp_ioc_stats stats;
850
851 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
852 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
853 session->name, cmd, arg);
854
855 sk = ps->sock;
856 sock_hold(sk);
857
858 switch (cmd) {
859 case SIOCGIFMTU:
860 err = -ENXIO;
861 if (!(sk->sk_state & PPPOX_CONNECTED))
862 break;
863
864 err = -EFAULT;
865 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
866 break;
867 ifr.ifr_mtu = session->mtu;
868 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
869 break;
870
871 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
872 "%s: get mtu=%d\n", session->name, session->mtu);
873 err = 0;
874 break;
875
876 case SIOCSIFMTU:
877 err = -ENXIO;
878 if (!(sk->sk_state & PPPOX_CONNECTED))
879 break;
880
881 err = -EFAULT;
882 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
883 break;
884
885 session->mtu = ifr.ifr_mtu;
886
887 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
888 "%s: set mtu=%d\n", session->name, session->mtu);
889 err = 0;
890 break;
891
892 case PPPIOCGMRU:
893 err = -ENXIO;
894 if (!(sk->sk_state & PPPOX_CONNECTED))
895 break;
896
897 err = -EFAULT;
898 if (put_user(session->mru, (int __user *) arg))
899 break;
900
901 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
902 "%s: get mru=%d\n", session->name, session->mru);
903 err = 0;
904 break;
905
906 case PPPIOCSMRU:
907 err = -ENXIO;
908 if (!(sk->sk_state & PPPOX_CONNECTED))
909 break;
910
911 err = -EFAULT;
912 if (get_user(val, (int __user *) arg))
913 break;
914
915 session->mru = val;
916 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
917 "%s: set mru=%d\n", session->name, session->mru);
918 err = 0;
919 break;
920
921 case PPPIOCGFLAGS:
922 err = -EFAULT;
923 if (put_user(ps->flags, (int __user *) arg))
924 break;
925
926 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
927 "%s: get flags=%d\n", session->name, ps->flags);
928 err = 0;
929 break;
930
931 case PPPIOCSFLAGS:
932 err = -EFAULT;
933 if (get_user(val, (int __user *) arg))
934 break;
935 ps->flags = val;
936 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
937 "%s: set flags=%d\n", session->name, ps->flags);
938 err = 0;
939 break;
940
941 case PPPIOCGL2TPSTATS:
942 err = -ENXIO;
943 if (!(sk->sk_state & PPPOX_CONNECTED))
944 break;
945
946 memset(&stats, 0, sizeof(stats));
947 stats.tunnel_id = tunnel->tunnel_id;
948 stats.session_id = session->session_id;
949 pppol2tp_copy_stats(&stats, &session->stats);
950 if (copy_to_user((void __user *) arg, &stats,
951 sizeof(stats)))
952 break;
953 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
954 "%s: get L2TP stats\n", session->name);
955 err = 0;
956 break;
957
958 default:
959 err = -ENOSYS;
960 break;
961 }
962
963 sock_put(sk);
964
965 return err;
966}
967
968/* Tunnel ioctl helper.
969 *
970 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
971 * specifies a session_id, the session ioctl handler is called. This allows an
972 * application to retrieve session stats via a tunnel socket.
973 */
974static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
975 unsigned int cmd, unsigned long arg)
976{
977 int err = 0;
978 struct sock *sk;
979 struct pppol2tp_ioc_stats stats;
980
981 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
982 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
983 tunnel->name, cmd, arg);
984
985 sk = tunnel->sock;
986 sock_hold(sk);
987
988 switch (cmd) {
989 case PPPIOCGL2TPSTATS:
990 err = -ENXIO;
991 if (!(sk->sk_state & PPPOX_CONNECTED))
992 break;
993
994 if (copy_from_user(&stats, (void __user *) arg,
995 sizeof(stats))) {
996 err = -EFAULT;
997 break;
998 }
999 if (stats.session_id != 0) {
1000 /* resend to session ioctl handler */
1001 struct l2tp_session *session =
1002 l2tp_session_find(tunnel, stats.session_id);
1003 if (session != NULL)
1004 err = pppol2tp_session_ioctl(session, cmd, arg);
1005 else
1006 err = -EBADR;
1007 break;
1008 }
1009#ifdef CONFIG_XFRM
1010 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1011#endif
1012 pppol2tp_copy_stats(&stats, &tunnel->stats);
1013 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1014 err = -EFAULT;
1015 break;
1016 }
1017 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1018 "%s: get L2TP stats\n", tunnel->name);
1019 err = 0;
1020 break;
1021
1022 default:
1023 err = -ENOSYS;
1024 break;
1025 }
1026
1027 sock_put(sk);
1028
1029 return err;
1030}
1031
1032/* Main ioctl() handler.
1033 * Dispatch to tunnel or session helpers depending on the socket.
1034 */
1035static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1036 unsigned long arg)
1037{
1038 struct sock *sk = sock->sk;
1039 struct l2tp_session *session;
1040 struct l2tp_tunnel *tunnel;
1041 struct pppol2tp_session *ps;
1042 int err;
1043
1044 if (!sk)
1045 return 0;
1046
1047 err = -EBADF;
1048 if (sock_flag(sk, SOCK_DEAD) != 0)
1049 goto end;
1050
1051 err = -ENOTCONN;
1052 if ((sk->sk_user_data == NULL) ||
1053 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1054 goto end;
1055
1056 /* Get session context from the socket */
1057 err = -EBADF;
1058 session = pppol2tp_sock_to_session(sk);
1059 if (session == NULL)
1060 goto end;
1061
1062 /* Special case: if session's session_id is zero, treat ioctl as a
1063 * tunnel ioctl
1064 */
1065 ps = l2tp_session_priv(session);
1066 if ((session->session_id == 0) &&
1067 (session->peer_session_id == 0)) {
1068 err = -EBADF;
1069 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1070 if (tunnel == NULL)
1071 goto end_put_sess;
1072
1073 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1074 sock_put(ps->tunnel_sock);
1075 goto end_put_sess;
1076 }
1077
1078 err = pppol2tp_session_ioctl(session, cmd, arg);
1079
1080end_put_sess:
1081 sock_put(sk);
1082end:
1083 return err;
1084}
1085
1086/*****************************************************************************
1087 * setsockopt() / getsockopt() support.
1088 *
1089 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1090 * sockets. In order to control kernel tunnel features, we allow userspace to
1091 * create a special "tunnel" PPPoX socket which is used for control only.
1092 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1093 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1094 *****************************************************************************/
1095
1096/* Tunnel setsockopt() helper.
1097 */
1098static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1099 struct l2tp_tunnel *tunnel,
1100 int optname, int val)
1101{
1102 int err = 0;
1103
1104 switch (optname) {
1105 case PPPOL2TP_SO_DEBUG:
1106 tunnel->debug = val;
1107 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1108 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1109 break;
1110
1111 default:
1112 err = -ENOPROTOOPT;
1113 break;
1114 }
1115
1116 return err;
1117}
1118
1119/* Session setsockopt helper.
1120 */
1121static int pppol2tp_session_setsockopt(struct sock *sk,
1122 struct l2tp_session *session,
1123 int optname, int val)
1124{
1125 int err = 0;
1126 struct pppol2tp_session *ps = l2tp_session_priv(session);
1127
1128 switch (optname) {
1129 case PPPOL2TP_SO_RECVSEQ:
1130 if ((val != 0) && (val != 1)) {
1131 err = -EINVAL;
1132 break;
1133 }
1134 session->recv_seq = val ? -1 : 0;
1135 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1136 "%s: set recv_seq=%d\n", session->name, session->recv_seq);
1137 break;
1138
1139 case PPPOL2TP_SO_SENDSEQ:
1140 if ((val != 0) && (val != 1)) {
1141 err = -EINVAL;
1142 break;
1143 }
1144 session->send_seq = val ? -1 : 0;
1145 {
1146 struct sock *ssk = ps->sock;
1147 struct pppox_sock *po = pppox_sk(ssk);
1148 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1149 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1150 }
1151 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1152 "%s: set send_seq=%d\n", session->name, session->send_seq);
1153 break;
1154
1155 case PPPOL2TP_SO_LNSMODE:
1156 if ((val != 0) && (val != 1)) {
1157 err = -EINVAL;
1158 break;
1159 }
1160 session->lns_mode = val ? -1 : 0;
1161 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1162 "%s: set lns_mode=%d\n", session->name, session->lns_mode);
1163 break;
1164
1165 case PPPOL2TP_SO_DEBUG:
1166 session->debug = val;
1167 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1168 "%s: set debug=%x\n", session->name, session->debug);
1169 break;
1170
1171 case PPPOL2TP_SO_REORDERTO:
1172 session->reorder_timeout = msecs_to_jiffies(val);
1173 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1174 "%s: set reorder_timeout=%d\n", session->name, session->reorder_timeout);
1175 break;
1176
1177 default:
1178 err = -ENOPROTOOPT;
1179 break;
1180 }
1181
1182 return err;
1183}
1184
1185/* Main setsockopt() entry point.
1186 * Does API checks, then calls either the tunnel or session setsockopt
1187 * handler, according to whether the PPPoL2TP socket is a for a regular
1188 * session or the special tunnel type.
1189 */
1190static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1191 char __user *optval, unsigned int optlen)
1192{
1193 struct sock *sk = sock->sk;
1194 struct l2tp_session *session;
1195 struct l2tp_tunnel *tunnel;
1196 struct pppol2tp_session *ps;
1197 int val;
1198 int err;
1199
1200 if (level != SOL_PPPOL2TP)
1201 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1202
1203 if (optlen < sizeof(int))
1204 return -EINVAL;
1205
1206 if (get_user(val, (int __user *)optval))
1207 return -EFAULT;
1208
1209 err = -ENOTCONN;
1210 if (sk->sk_user_data == NULL)
1211 goto end;
1212
1213 /* Get session context from the socket */
1214 err = -EBADF;
1215 session = pppol2tp_sock_to_session(sk);
1216 if (session == NULL)
1217 goto end;
1218
1219 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1220 */
1221 ps = l2tp_session_priv(session);
1222 if ((session->session_id == 0) &&
1223 (session->peer_session_id == 0)) {
1224 err = -EBADF;
1225 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1226 if (tunnel == NULL)
1227 goto end_put_sess;
1228
1229 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1230 sock_put(ps->tunnel_sock);
1231 } else
1232 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1233
1234 err = 0;
1235
1236end_put_sess:
1237 sock_put(sk);
1238end:
1239 return err;
1240}
1241
1242/* Tunnel getsockopt helper. Called with sock locked.
1243 */
1244static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1245 struct l2tp_tunnel *tunnel,
1246 int optname, int *val)
1247{
1248 int err = 0;
1249
1250 switch (optname) {
1251 case PPPOL2TP_SO_DEBUG:
1252 *val = tunnel->debug;
1253 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1254 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
1255 break;
1256
1257 default:
1258 err = -ENOPROTOOPT;
1259 break;
1260 }
1261
1262 return err;
1263}
1264
1265/* Session getsockopt helper. Called with sock locked.
1266 */
1267static int pppol2tp_session_getsockopt(struct sock *sk,
1268 struct l2tp_session *session,
1269 int optname, int *val)
1270{
1271 int err = 0;
1272
1273 switch (optname) {
1274 case PPPOL2TP_SO_RECVSEQ:
1275 *val = session->recv_seq;
1276 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1277 "%s: get recv_seq=%d\n", session->name, *val);
1278 break;
1279
1280 case PPPOL2TP_SO_SENDSEQ:
1281 *val = session->send_seq;
1282 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1283 "%s: get send_seq=%d\n", session->name, *val);
1284 break;
1285
1286 case PPPOL2TP_SO_LNSMODE:
1287 *val = session->lns_mode;
1288 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1289 "%s: get lns_mode=%d\n", session->name, *val);
1290 break;
1291
1292 case PPPOL2TP_SO_DEBUG:
1293 *val = session->debug;
1294 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1295 "%s: get debug=%d\n", session->name, *val);
1296 break;
1297
1298 case PPPOL2TP_SO_REORDERTO:
1299 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1300 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1301 "%s: get reorder_timeout=%d\n", session->name, *val);
1302 break;
1303
1304 default:
1305 err = -ENOPROTOOPT;
1306 }
1307
1308 return err;
1309}
1310
1311/* Main getsockopt() entry point.
1312 * Does API checks, then calls either the tunnel or session getsockopt
1313 * handler, according to whether the PPPoX socket is a for a regular session
1314 * or the special tunnel type.
1315 */
1316static int pppol2tp_getsockopt(struct socket *sock, int level,
1317 int optname, char __user *optval, int __user *optlen)
1318{
1319 struct sock *sk = sock->sk;
1320 struct l2tp_session *session;
1321 struct l2tp_tunnel *tunnel;
1322 int val, len;
1323 int err;
1324 struct pppol2tp_session *ps;
1325
1326 if (level != SOL_PPPOL2TP)
1327 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1328
1329 if (get_user(len, (int __user *) optlen))
1330 return -EFAULT;
1331
1332 len = min_t(unsigned int, len, sizeof(int));
1333
1334 if (len < 0)
1335 return -EINVAL;
1336
1337 err = -ENOTCONN;
1338 if (sk->sk_user_data == NULL)
1339 goto end;
1340
1341 /* Get the session context */
1342 err = -EBADF;
1343 session = pppol2tp_sock_to_session(sk);
1344 if (session == NULL)
1345 goto end;
1346
1347 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1348 ps = l2tp_session_priv(session);
1349 if ((session->session_id == 0) &&
1350 (session->peer_session_id == 0)) {
1351 err = -EBADF;
1352 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1353 if (tunnel == NULL)
1354 goto end_put_sess;
1355
1356 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1357 sock_put(ps->tunnel_sock);
1358 } else
1359 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1360
1361 err = -EFAULT;
1362 if (put_user(len, (int __user *) optlen))
1363 goto end_put_sess;
1364
1365 if (copy_to_user((void __user *) optval, &val, len))
1366 goto end_put_sess;
1367
1368 err = 0;
1369
1370end_put_sess:
1371 sock_put(sk);
1372end:
1373 return err;
1374}
1375
1376/*****************************************************************************
1377 * /proc filesystem for debug
1378 *****************************************************************************/
1379
1380static unsigned int pppol2tp_net_id;
1381
1382#ifdef CONFIG_PROC_FS
1383
1384struct pppol2tp_seq_data {
1385 struct seq_net_private p;
1386 int tunnel_idx; /* current tunnel */
1387 int session_idx; /* index of session within current tunnel */
1388 struct l2tp_tunnel *tunnel;
1389 struct l2tp_session *session; /* NULL means get next tunnel */
1390};
1391
1392static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1393{
1394 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1395 pd->tunnel_idx++;
1396}
1397
1398static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1399{
1400 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1401 pd->session_idx++;
1402 if (pd->session == NULL) {
1403 pd->session_idx = 0;
1404 pppol2tp_next_tunnel(net, pd);
1405 }
1406}
1407
1408static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1409{
1410 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1411 loff_t pos = *offs;
1412 struct net *net;
1413
1414 if (!pos)
1415 goto out;
1416
1417 BUG_ON(m->private == NULL);
1418 pd = m->private;
1419 net = seq_file_net(m);
1420
1421 if (pd->tunnel == NULL)
1422 pppol2tp_next_tunnel(net, pd);
1423 else
1424 pppol2tp_next_session(net, pd);
1425
1426 /* NULL tunnel and session indicates end of list */
1427 if ((pd->tunnel == NULL) && (pd->session == NULL))
1428 pd = NULL;
1429
1430out:
1431 return pd;
1432}
1433
1434static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1435{
1436 (*pos)++;
1437 return NULL;
1438}
1439
1440static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1441{
1442 /* nothing to do */
1443}
1444
1445static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1446{
1447 struct l2tp_tunnel *tunnel = v;
1448
1449 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1450 tunnel->name,
1451 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1452 atomic_read(&tunnel->ref_count) - 1);
1453 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1454 tunnel->debug,
1455 (unsigned long long)tunnel->stats.tx_packets,
1456 (unsigned long long)tunnel->stats.tx_bytes,
1457 (unsigned long long)tunnel->stats.tx_errors,
1458 (unsigned long long)tunnel->stats.rx_packets,
1459 (unsigned long long)tunnel->stats.rx_bytes,
1460 (unsigned long long)tunnel->stats.rx_errors);
1461}
1462
1463static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1464{
1465 struct l2tp_session *session = v;
1466 struct l2tp_tunnel *tunnel = session->tunnel;
1467 struct pppol2tp_session *ps = l2tp_session_priv(session);
1468 u32 ip = 0;
1469 u16 port = 0;
1470
1471 if (tunnel->sock) {
1472 struct inet_sock *inet = inet_sk(tunnel->sock);
1473 ip = ntohl(inet->inet_saddr);
1474 port = ntohs(inet->inet_sport);
1475 }
1476
1477 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1478 "%04X/%04X %d %c\n",
1479 session->name, ip, port,
1480 tunnel->tunnel_id,
1481 session->session_id,
1482 tunnel->peer_tunnel_id,
1483 session->peer_session_id,
1484 ps->sock->sk_state,
1485 (session == ps->sock->sk_user_data) ?
1486 'Y' : 'N');
1487 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1488 session->mtu, session->mru,
1489 session->recv_seq ? 'R' : '-',
1490 session->send_seq ? 'S' : '-',
1491 session->lns_mode ? "LNS" : "LAC",
1492 session->debug,
1493 jiffies_to_msecs(session->reorder_timeout));
1494 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1495 session->nr, session->ns,
1496 (unsigned long long)session->stats.tx_packets,
1497 (unsigned long long)session->stats.tx_bytes,
1498 (unsigned long long)session->stats.tx_errors,
1499 (unsigned long long)session->stats.rx_packets,
1500 (unsigned long long)session->stats.rx_bytes,
1501 (unsigned long long)session->stats.rx_errors);
1502}
1503
1504static int pppol2tp_seq_show(struct seq_file *m, void *v)
1505{
1506 struct pppol2tp_seq_data *pd = v;
1507
1508 /* display header on line 1 */
1509 if (v == SEQ_START_TOKEN) {
1510 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1511 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1512 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1513 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1514 "dest-tid/sid state user-data-ok\n");
1515 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1516 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1517 goto out;
1518 }
1519
1520 /* Show the tunnel or session context.
1521 */
1522 if (pd->session == NULL)
1523 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1524 else
1525 pppol2tp_seq_session_show(m, pd->session);
1526
1527out:
1528 return 0;
1529}
1530
1531static const struct seq_operations pppol2tp_seq_ops = {
1532 .start = pppol2tp_seq_start,
1533 .next = pppol2tp_seq_next,
1534 .stop = pppol2tp_seq_stop,
1535 .show = pppol2tp_seq_show,
1536};
1537
1538/* Called when our /proc file is opened. We allocate data for use when
1539 * iterating our tunnel / session contexts and store it in the private
1540 * data of the seq_file.
1541 */
1542static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1543{
1544 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1545 sizeof(struct pppol2tp_seq_data));
1546}
1547
1548static const struct file_operations pppol2tp_proc_fops = {
1549 .owner = THIS_MODULE,
1550 .open = pppol2tp_proc_open,
1551 .read = seq_read,
1552 .llseek = seq_lseek,
1553 .release = seq_release_net,
1554};
1555
1556#endif /* CONFIG_PROC_FS */
1557
1558/*****************************************************************************
1559 * Network namespace
1560 *****************************************************************************/
1561
1562static __net_init int pppol2tp_init_net(struct net *net)
1563{
1564 struct proc_dir_entry *pde;
1565 int err = 0;
1566
1567 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1568 if (!pde) {
1569 err = -ENOMEM;
1570 goto out;
1571 }
1572
1573out:
1574 return err;
1575}
1576
1577static __net_exit void pppol2tp_exit_net(struct net *net)
1578{
1579 proc_net_remove(net, "pppol2tp");
1580}
1581
1582static struct pernet_operations pppol2tp_net_ops = {
1583 .init = pppol2tp_init_net,
1584 .exit = pppol2tp_exit_net,
1585 .id = &pppol2tp_net_id,
1586};
1587
1588/*****************************************************************************
1589 * Init and cleanup
1590 *****************************************************************************/
1591
1592static const struct proto_ops pppol2tp_ops = {
1593 .family = AF_PPPOX,
1594 .owner = THIS_MODULE,
1595 .release = pppol2tp_release,
1596 .bind = sock_no_bind,
1597 .connect = pppol2tp_connect,
1598 .socketpair = sock_no_socketpair,
1599 .accept = sock_no_accept,
1600 .getname = pppol2tp_getname,
1601 .poll = datagram_poll,
1602 .listen = sock_no_listen,
1603 .shutdown = sock_no_shutdown,
1604 .setsockopt = pppol2tp_setsockopt,
1605 .getsockopt = pppol2tp_getsockopt,
1606 .sendmsg = pppol2tp_sendmsg,
1607 .recvmsg = pppol2tp_recvmsg,
1608 .mmap = sock_no_mmap,
1609 .ioctl = pppox_ioctl,
1610};
1611
1612static struct pppox_proto pppol2tp_proto = {
1613 .create = pppol2tp_create,
1614 .ioctl = pppol2tp_ioctl
1615};
1616
1617static int __init pppol2tp_init(void)
1618{
1619 int err;
1620
1621 err = register_pernet_device(&pppol2tp_net_ops);
1622 if (err)
1623 goto out;
1624
1625 err = proto_register(&pppol2tp_sk_proto, 0);
1626 if (err)
1627 goto out_unregister_pppol2tp_pernet;
1628
1629 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1630 if (err)
1631 goto out_unregister_pppol2tp_proto;
1632
1633 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
1634 PPPOL2TP_DRV_VERSION);
1635
1636out:
1637 return err;
1638out_unregister_pppol2tp_proto:
1639 proto_unregister(&pppol2tp_sk_proto);
1640out_unregister_pppol2tp_pernet:
1641 unregister_pernet_device(&pppol2tp_net_ops);
1642 goto out;
1643}
1644
1645static void __exit pppol2tp_exit(void)
1646{
1647 unregister_pppox_proto(PX_PROTO_OL2TP);
1648 proto_unregister(&pppol2tp_sk_proto);
1649 unregister_pernet_device(&pppol2tp_net_ops);
1650}
1651
1652module_init(pppol2tp_init);
1653module_exit(pppol2tp_exit);
1654
1655MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1656MODULE_DESCRIPTION("PPP over L2TP over UDP");
1657MODULE_LICENSE("GPL");
1658MODULE_VERSION(PPPOL2TP_DRV_VERSION);