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