blob: e5b531266541a35c6bb6ca6b9cbbda5015b8472c [file] [log] [blame]
James Chapmanfd558d12010-04-02 06:18:33 +00001/*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
3 *
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
6 *
7 * Version: 2.0.0
8 *
9 * Authors: James Chapman (jchapman@katalix.com)
10 *
11 * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
12 *
13 * License:
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 */
20
21/* This driver handles only L2TP data frames; control frames are handled by a
22 * userspace application.
23 *
24 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
25 * attaches it to a bound UDP socket with local tunnel_id / session_id and
26 * peer tunnel_id / session_id set. Data can then be sent or received using
27 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
28 * can be read or modified using ioctl() or [gs]etsockopt() calls.
29 *
30 * When a PPPoL2TP socket is connected with local and peer session_id values
31 * zero, the socket is treated as a special tunnel management socket.
32 *
33 * Here's example userspace code to create a socket for sending/receiving data
34 * over an L2TP session:-
35 *
36 * struct sockaddr_pppol2tp sax;
37 * int fd;
38 * int session_fd;
39 *
40 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
41 *
42 * sax.sa_family = AF_PPPOX;
43 * sax.sa_protocol = PX_PROTO_OL2TP;
44 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
45 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
46 * sax.pppol2tp.addr.sin_port = addr->sin_port;
47 * sax.pppol2tp.addr.sin_family = AF_INET;
48 * sax.pppol2tp.s_tunnel = tunnel_id;
49 * sax.pppol2tp.s_session = session_id;
50 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
51 * sax.pppol2tp.d_session = peer_session_id;
52 *
53 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
54 *
55 * A pppd plugin that allows PPP traffic to be carried over L2TP using
56 * this driver is available from the OpenL2TP project at
57 * http://openl2tp.sourceforge.net.
58 */
59
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
James Chapmanfd558d12010-04-02 06:18:33 +0000294/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
295 * when a user application does a sendmsg() on the session socket. L2TP and
296 * PPP headers must be inserted into the user's data.
297 */
298static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
299 size_t total_len)
300{
301 static const unsigned char ppph[2] = { 0xff, 0x03 };
302 struct sock *sk = sock->sk;
303 struct sk_buff *skb;
304 int error;
305 struct l2tp_session *session;
306 struct l2tp_tunnel *tunnel;
307 struct pppol2tp_session *ps;
308
309 error = -ENOTCONN;
310 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
311 goto error;
312
313 /* Get session and tunnel contexts */
314 error = -EBADF;
315 session = pppol2tp_sock_to_session(sk);
316 if (session == NULL)
317 goto error;
318
319 ps = l2tp_session_priv(session);
320 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
321 if (tunnel == NULL)
322 goto error_put_sess;
323
324 /* Allocate a socket buffer */
325 error = -ENOMEM;
326 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
327 sizeof(struct udphdr) + session->hdr_len +
328 sizeof(ppph) + total_len,
329 0, GFP_KERNEL);
330 if (!skb)
331 goto error_put_sess_tun;
332
333 /* Reserve space for headers. */
334 skb_reserve(skb, NET_SKB_PAD);
335 skb_reset_network_header(skb);
336 skb_reserve(skb, sizeof(struct iphdr));
337 skb_reset_transport_header(skb);
338 skb_reserve(skb, sizeof(struct udphdr));
339
340 /* Add PPP header */
341 skb->data[0] = ppph[0];
342 skb->data[1] = ppph[1];
343 skb_put(skb, 2);
344
345 /* Copy user data into skb */
346 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
347 if (error < 0) {
348 kfree_skb(skb);
349 goto error_put_sess_tun;
350 }
351 skb_put(skb, total_len);
352
353 l2tp_xmit_skb(session, skb, session->hdr_len);
354
355 sock_put(ps->tunnel_sock);
356
357 return error;
358
359error_put_sess_tun:
360 sock_put(ps->tunnel_sock);
361error_put_sess:
362 sock_put(sk);
363error:
364 return error;
365}
366
367/* Transmit function called by generic PPP driver. Sends PPP frame
368 * over PPPoL2TP socket.
369 *
370 * This is almost the same as pppol2tp_sendmsg(), but rather than
371 * being called with a msghdr from userspace, it is called with a skb
372 * from the kernel.
373 *
374 * The supplied skb from ppp doesn't have enough headroom for the
375 * insertion of L2TP, UDP and IP headers so we need to allocate more
376 * headroom in the skb. This will create a cloned skb. But we must be
377 * careful in the error case because the caller will expect to free
378 * the skb it supplied, not our cloned skb. So we take care to always
379 * leave the original skb unfreed if we return an error.
380 */
381static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
382{
383 static const u8 ppph[2] = { 0xff, 0x03 };
384 struct sock *sk = (struct sock *) chan->private;
385 struct sock *sk_tun;
James Chapmanfd558d12010-04-02 06:18:33 +0000386 struct l2tp_session *session;
387 struct l2tp_tunnel *tunnel;
388 struct pppol2tp_session *ps;
389 int old_headroom;
390 int new_headroom;
391
392 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
393 goto abort;
394
395 /* Get session and tunnel contexts from the socket */
396 session = pppol2tp_sock_to_session(sk);
397 if (session == NULL)
398 goto abort;
399
400 ps = l2tp_session_priv(session);
401 sk_tun = ps->tunnel_sock;
402 if (sk_tun == NULL)
403 goto abort_put_sess;
404 tunnel = l2tp_sock_to_tunnel(sk_tun);
405 if (tunnel == NULL)
406 goto abort_put_sess;
407
James Chapmanfd558d12010-04-02 06:18:33 +0000408 old_headroom = skb_headroom(skb);
409 if (skb_cow_head(skb, sizeof(ppph)))
410 goto abort_put_sess_tun;
411
412 new_headroom = skb_headroom(skb);
413 skb->truesize += new_headroom - old_headroom;
414
415 /* Setup PPP header */
416 __skb_push(skb, sizeof(ppph));
417 skb->data[0] = ppph[0];
418 skb->data[1] = ppph[1];
419
James Chapmane0d44352010-04-02 06:18:54 +0000420 l2tp_xmit_skb(session, skb, session->hdr_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000421
422 sock_put(sk_tun);
423 sock_put(sk);
424 return 1;
425
426abort_put_sess_tun:
427 sock_put(sk_tun);
428abort_put_sess:
429 sock_put(sk);
430abort:
431 /* Free the original skb */
432 kfree_skb(skb);
433 return 1;
434}
435
436/*****************************************************************************
437 * Session (and tunnel control) socket create/destroy.
438 *****************************************************************************/
439
440/* Called by l2tp_core when a session socket is being closed.
441 */
442static void pppol2tp_session_close(struct l2tp_session *session)
443{
444 struct pppol2tp_session *ps = l2tp_session_priv(session);
445 struct sock *sk = ps->sock;
446 struct sk_buff *skb;
447
448 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
449
450 if (session->session_id == 0)
451 goto out;
452
453 if (sk != NULL) {
454 lock_sock(sk);
455
456 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
457 pppox_unbind_sock(sk);
458 sk->sk_state = PPPOX_DEAD;
459 sk->sk_state_change(sk);
460 }
461
462 /* Purge any queued data */
463 skb_queue_purge(&sk->sk_receive_queue);
464 skb_queue_purge(&sk->sk_write_queue);
465 while ((skb = skb_dequeue(&session->reorder_q))) {
466 kfree_skb(skb);
467 sock_put(sk);
468 }
469
470 release_sock(sk);
471 }
472
473out:
474 return;
475}
476
477/* Really kill the session socket. (Called from sock_put() if
478 * refcnt == 0.)
479 */
480static void pppol2tp_session_destruct(struct sock *sk)
481{
482 struct l2tp_session *session;
483
484 if (sk->sk_user_data != NULL) {
485 session = sk->sk_user_data;
486 if (session == NULL)
487 goto out;
488
489 sk->sk_user_data = NULL;
490 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
491 l2tp_session_dec_refcount(session);
492 }
493
494out:
495 return;
496}
497
498/* Called when the PPPoX socket (session) is closed.
499 */
500static int pppol2tp_release(struct socket *sock)
501{
502 struct sock *sk = sock->sk;
503 struct l2tp_session *session;
504 int error;
505
506 if (!sk)
507 return 0;
508
509 error = -EBADF;
510 lock_sock(sk);
511 if (sock_flag(sk, SOCK_DEAD) != 0)
512 goto error;
513
514 pppox_unbind_sock(sk);
515
516 /* Signal the death of the socket. */
517 sk->sk_state = PPPOX_DEAD;
518 sock_orphan(sk);
519 sock->sk = NULL;
520
521 session = pppol2tp_sock_to_session(sk);
522
523 /* Purge any queued data */
524 skb_queue_purge(&sk->sk_receive_queue);
525 skb_queue_purge(&sk->sk_write_queue);
526 if (session != NULL) {
527 struct sk_buff *skb;
528 while ((skb = skb_dequeue(&session->reorder_q))) {
529 kfree_skb(skb);
530 sock_put(sk);
531 }
532 sock_put(sk);
533 }
534
535 release_sock(sk);
536
537 /* This will delete the session context via
538 * pppol2tp_session_destruct() if the socket's refcnt drops to
539 * zero.
540 */
541 sock_put(sk);
542
543 return 0;
544
545error:
546 release_sock(sk);
547 return error;
548}
549
550static struct proto pppol2tp_sk_proto = {
551 .name = "PPPOL2TP",
552 .owner = THIS_MODULE,
553 .obj_size = sizeof(struct pppox_sock),
554};
555
556static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
557{
558 int rc;
559
560 rc = l2tp_udp_encap_recv(sk, skb);
561 if (rc)
562 kfree_skb(skb);
563
564 return NET_RX_SUCCESS;
565}
566
567/* socket() handler. Initialize a new struct sock.
568 */
569static int pppol2tp_create(struct net *net, struct socket *sock)
570{
571 int error = -ENOMEM;
572 struct sock *sk;
573
574 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
575 if (!sk)
576 goto out;
577
578 sock_init_data(sock, sk);
579
580 sock->state = SS_UNCONNECTED;
581 sock->ops = &pppol2tp_ops;
582
583 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
584 sk->sk_protocol = PX_PROTO_OL2TP;
585 sk->sk_family = PF_PPPOX;
586 sk->sk_state = PPPOX_NONE;
587 sk->sk_type = SOCK_STREAM;
588 sk->sk_destruct = pppol2tp_session_destruct;
589
590 error = 0;
591
592out:
593 return error;
594}
595
596/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
597 */
598static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
599 int sockaddr_len, int flags)
600{
601 struct sock *sk = sock->sk;
602 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
James Chapmane0d44352010-04-02 06:18:54 +0000603 struct sockaddr_pppol2tpv3 *sp3 = (struct sockaddr_pppol2tpv3 *) uservaddr;
James Chapmanfd558d12010-04-02 06:18:33 +0000604 struct pppox_sock *po = pppox_sk(sk);
605 struct l2tp_session *session = NULL;
606 struct l2tp_tunnel *tunnel;
607 struct pppol2tp_session *ps;
608 struct dst_entry *dst;
609 struct l2tp_session_cfg cfg = { 0, };
610 int error = 0;
James Chapmane0d44352010-04-02 06:18:54 +0000611 u32 tunnel_id, peer_tunnel_id;
612 u32 session_id, peer_session_id;
613 int ver = 2;
614 int fd;
James Chapmanfd558d12010-04-02 06:18:33 +0000615
616 lock_sock(sk);
617
618 error = -EINVAL;
619 if (sp->sa_protocol != PX_PROTO_OL2TP)
620 goto end;
621
622 /* Check for already bound sockets */
623 error = -EBUSY;
624 if (sk->sk_state & PPPOX_CONNECTED)
625 goto end;
626
627 /* We don't supporting rebinding anyway */
628 error = -EALREADY;
629 if (sk->sk_user_data)
630 goto end; /* socket is already attached */
631
James Chapmane0d44352010-04-02 06:18:54 +0000632 /* Get params from socket address. Handle L2TPv2 and L2TPv3 */
633 if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
634 fd = sp->pppol2tp.fd;
635 tunnel_id = sp->pppol2tp.s_tunnel;
636 peer_tunnel_id = sp->pppol2tp.d_tunnel;
637 session_id = sp->pppol2tp.s_session;
638 peer_session_id = sp->pppol2tp.d_session;
639 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
640 ver = 3;
641 fd = sp3->pppol2tp.fd;
642 tunnel_id = sp3->pppol2tp.s_tunnel;
643 peer_tunnel_id = sp3->pppol2tp.d_tunnel;
644 session_id = sp3->pppol2tp.s_session;
645 peer_session_id = sp3->pppol2tp.d_session;
646 } else {
647 error = -EINVAL;
648 goto end; /* bad socket address */
649 }
650
651 /* Don't bind if tunnel_id is 0 */
James Chapmanfd558d12010-04-02 06:18:33 +0000652 error = -EINVAL;
James Chapmane0d44352010-04-02 06:18:54 +0000653 if (tunnel_id == 0)
James Chapmanfd558d12010-04-02 06:18:33 +0000654 goto end;
655
James Chapmane0d44352010-04-02 06:18:54 +0000656 /* Special case: create tunnel context if session_id and
657 * peer_session_id is 0. Otherwise look up tunnel using supplied
James Chapmanfd558d12010-04-02 06:18:33 +0000658 * tunnel id.
659 */
James Chapmane0d44352010-04-02 06:18:54 +0000660 if ((session_id == 0) && (peer_session_id == 0)) {
661 error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, NULL, &tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +0000662 if (error < 0)
663 goto end;
664 } else {
James Chapmane0d44352010-04-02 06:18:54 +0000665 tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000666
667 /* Error if we can't find the tunnel */
668 error = -ENOENT;
669 if (tunnel == NULL)
670 goto end;
671
672 /* Error if socket is not prepped */
673 if (tunnel->sock == NULL)
674 goto end;
675 }
676
677 if (tunnel->recv_payload_hook == NULL)
678 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
679
680 /* Check that this session doesn't already exist */
681 error = -EEXIST;
James Chapmane0d44352010-04-02 06:18:54 +0000682 session = l2tp_session_find(sock_net(sk), tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000683 if (session != NULL)
684 goto end;
685
James Chapmane0d44352010-04-02 06:18:54 +0000686 /* Default MTU values. */
687 if (cfg.mtu == 0)
688 cfg.mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
689 if (cfg.mru == 0)
690 cfg.mru = cfg.mtu;
James Chapmanfd558d12010-04-02 06:18:33 +0000691 cfg.debug = tunnel->debug;
692
693 /* Allocate and initialize a new session context. */
694 session = l2tp_session_create(sizeof(struct pppol2tp_session),
James Chapmane0d44352010-04-02 06:18:54 +0000695 tunnel, session_id,
696 peer_session_id, &cfg);
James Chapmanfd558d12010-04-02 06:18:33 +0000697 if (session == NULL) {
698 error = -ENOMEM;
699 goto end;
700 }
701
702 ps = l2tp_session_priv(session);
703 ps->owner = current->pid;
704 ps->sock = sk;
705 ps->tunnel_sock = tunnel->sock;
706
707 session->recv_skb = pppol2tp_recv;
708 session->session_close = pppol2tp_session_close;
709
710 /* We need to know each time a skb is dropped from the reorder
711 * queue.
712 */
713 session->ref = pppol2tp_session_sock_hold;
714 session->deref = pppol2tp_session_sock_put;
715
716 /* If PMTU discovery was enabled, use the MTU that was discovered */
717 dst = sk_dst_get(sk);
718 if (dst != NULL) {
719 u32 pmtu = dst_mtu(__sk_dst_get(sk));
720 if (pmtu != 0)
721 session->mtu = session->mru = pmtu -
722 PPPOL2TP_HEADER_OVERHEAD;
723 dst_release(dst);
724 }
725
726 /* Special case: if source & dest session_id == 0x0000, this
727 * socket is being created to manage the tunnel. Just set up
728 * the internal context for use by ioctl() and sockopt()
729 * handlers.
730 */
731 if ((session->session_id == 0) &&
732 (session->peer_session_id == 0)) {
733 error = 0;
734 goto out_no_ppp;
735 }
736
737 /* The only header we need to worry about is the L2TP
738 * header. This size is different depending on whether
739 * sequence numbers are enabled for the data channel.
740 */
741 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
742
743 po->chan.private = sk;
744 po->chan.ops = &pppol2tp_chan_ops;
745 po->chan.mtu = session->mtu;
746
747 error = ppp_register_net_channel(sock_net(sk), &po->chan);
748 if (error)
749 goto end;
750
751out_no_ppp:
752 /* This is how we get the session context from the socket. */
753 sk->sk_user_data = session;
754 sk->sk_state = PPPOX_CONNECTED;
755 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
756 "%s: created\n", session->name);
757
758end:
759 release_sock(sk);
760
761 return error;
762}
763
764/* getname() support.
765 */
766static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
767 int *usockaddr_len, int peer)
768{
James Chapmane0d44352010-04-02 06:18:54 +0000769 int len = 0;
James Chapmanfd558d12010-04-02 06:18:33 +0000770 int error = 0;
771 struct l2tp_session *session;
772 struct l2tp_tunnel *tunnel;
773 struct sock *sk = sock->sk;
774 struct inet_sock *inet;
775 struct pppol2tp_session *pls;
776
777 error = -ENOTCONN;
778 if (sk == NULL)
779 goto end;
780 if (sk->sk_state != PPPOX_CONNECTED)
781 goto end;
782
783 error = -EBADF;
784 session = pppol2tp_sock_to_session(sk);
785 if (session == NULL)
786 goto end;
787
788 pls = l2tp_session_priv(session);
789 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
790 if (tunnel == NULL) {
791 error = -EBADF;
792 goto end_put_sess;
793 }
794
James Chapmanfd558d12010-04-02 06:18:33 +0000795 inet = inet_sk(sk);
James Chapmane0d44352010-04-02 06:18:54 +0000796 if (tunnel->version == 2) {
797 struct sockaddr_pppol2tp sp;
798 len = sizeof(sp);
799 memset(&sp, 0, len);
800 sp.sa_family = AF_PPPOX;
801 sp.sa_protocol = PX_PROTO_OL2TP;
802 sp.pppol2tp.fd = tunnel->fd;
803 sp.pppol2tp.pid = pls->owner;
804 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
805 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
806 sp.pppol2tp.s_session = session->session_id;
807 sp.pppol2tp.d_session = session->peer_session_id;
808 sp.pppol2tp.addr.sin_family = AF_INET;
809 sp.pppol2tp.addr.sin_port = inet->inet_dport;
810 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
811 memcpy(uaddr, &sp, len);
812 } else if (tunnel->version == 3) {
813 struct sockaddr_pppol2tpv3 sp;
814 len = sizeof(sp);
815 memset(&sp, 0, len);
816 sp.sa_family = AF_PPPOX;
817 sp.sa_protocol = PX_PROTO_OL2TP;
818 sp.pppol2tp.fd = tunnel->fd;
819 sp.pppol2tp.pid = pls->owner;
820 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
821 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
822 sp.pppol2tp.s_session = session->session_id;
823 sp.pppol2tp.d_session = session->peer_session_id;
824 sp.pppol2tp.addr.sin_family = AF_INET;
825 sp.pppol2tp.addr.sin_port = inet->inet_dport;
826 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
827 memcpy(uaddr, &sp, len);
828 }
James Chapmanfd558d12010-04-02 06:18:33 +0000829
830 *usockaddr_len = len;
831
832 sock_put(pls->tunnel_sock);
833end_put_sess:
834 sock_put(sk);
835 error = 0;
836
837end:
838 return error;
839}
840
841/****************************************************************************
842 * ioctl() handlers.
843 *
844 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
845 * sockets. However, in order to control kernel tunnel features, we allow
846 * userspace to create a special "tunnel" PPPoX socket which is used for
847 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
848 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
849 * calls.
850 ****************************************************************************/
851
852static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
853 struct l2tp_stats *stats)
854{
855 dest->tx_packets = stats->tx_packets;
856 dest->tx_bytes = stats->tx_bytes;
857 dest->tx_errors = stats->tx_errors;
858 dest->rx_packets = stats->rx_packets;
859 dest->rx_bytes = stats->rx_bytes;
860 dest->rx_seq_discards = stats->rx_seq_discards;
861 dest->rx_oos_packets = stats->rx_oos_packets;
862 dest->rx_errors = stats->rx_errors;
863}
864
865/* Session ioctl helper.
866 */
867static int pppol2tp_session_ioctl(struct l2tp_session *session,
868 unsigned int cmd, unsigned long arg)
869{
870 struct ifreq ifr;
871 int err = 0;
872 struct sock *sk;
873 int val = (int) arg;
874 struct pppol2tp_session *ps = l2tp_session_priv(session);
875 struct l2tp_tunnel *tunnel = session->tunnel;
876 struct pppol2tp_ioc_stats stats;
877
878 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
879 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
880 session->name, cmd, arg);
881
882 sk = ps->sock;
883 sock_hold(sk);
884
885 switch (cmd) {
886 case SIOCGIFMTU:
887 err = -ENXIO;
888 if (!(sk->sk_state & PPPOX_CONNECTED))
889 break;
890
891 err = -EFAULT;
892 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
893 break;
894 ifr.ifr_mtu = session->mtu;
895 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
896 break;
897
898 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
899 "%s: get mtu=%d\n", session->name, session->mtu);
900 err = 0;
901 break;
902
903 case SIOCSIFMTU:
904 err = -ENXIO;
905 if (!(sk->sk_state & PPPOX_CONNECTED))
906 break;
907
908 err = -EFAULT;
909 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
910 break;
911
912 session->mtu = ifr.ifr_mtu;
913
914 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
915 "%s: set mtu=%d\n", session->name, session->mtu);
916 err = 0;
917 break;
918
919 case PPPIOCGMRU:
920 err = -ENXIO;
921 if (!(sk->sk_state & PPPOX_CONNECTED))
922 break;
923
924 err = -EFAULT;
925 if (put_user(session->mru, (int __user *) arg))
926 break;
927
928 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
929 "%s: get mru=%d\n", session->name, session->mru);
930 err = 0;
931 break;
932
933 case PPPIOCSMRU:
934 err = -ENXIO;
935 if (!(sk->sk_state & PPPOX_CONNECTED))
936 break;
937
938 err = -EFAULT;
939 if (get_user(val, (int __user *) arg))
940 break;
941
942 session->mru = val;
943 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
944 "%s: set mru=%d\n", session->name, session->mru);
945 err = 0;
946 break;
947
948 case PPPIOCGFLAGS:
949 err = -EFAULT;
950 if (put_user(ps->flags, (int __user *) arg))
951 break;
952
953 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
954 "%s: get flags=%d\n", session->name, ps->flags);
955 err = 0;
956 break;
957
958 case PPPIOCSFLAGS:
959 err = -EFAULT;
960 if (get_user(val, (int __user *) arg))
961 break;
962 ps->flags = val;
963 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
964 "%s: set flags=%d\n", session->name, ps->flags);
965 err = 0;
966 break;
967
968 case PPPIOCGL2TPSTATS:
969 err = -ENXIO;
970 if (!(sk->sk_state & PPPOX_CONNECTED))
971 break;
972
973 memset(&stats, 0, sizeof(stats));
974 stats.tunnel_id = tunnel->tunnel_id;
975 stats.session_id = session->session_id;
976 pppol2tp_copy_stats(&stats, &session->stats);
977 if (copy_to_user((void __user *) arg, &stats,
978 sizeof(stats)))
979 break;
980 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
981 "%s: get L2TP stats\n", session->name);
982 err = 0;
983 break;
984
985 default:
986 err = -ENOSYS;
987 break;
988 }
989
990 sock_put(sk);
991
992 return err;
993}
994
995/* Tunnel ioctl helper.
996 *
997 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
998 * specifies a session_id, the session ioctl handler is called. This allows an
999 * application to retrieve session stats via a tunnel socket.
1000 */
1001static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1002 unsigned int cmd, unsigned long arg)
1003{
1004 int err = 0;
1005 struct sock *sk;
1006 struct pppol2tp_ioc_stats stats;
1007
1008 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1009 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1010 tunnel->name, cmd, arg);
1011
1012 sk = tunnel->sock;
1013 sock_hold(sk);
1014
1015 switch (cmd) {
1016 case PPPIOCGL2TPSTATS:
1017 err = -ENXIO;
1018 if (!(sk->sk_state & PPPOX_CONNECTED))
1019 break;
1020
1021 if (copy_from_user(&stats, (void __user *) arg,
1022 sizeof(stats))) {
1023 err = -EFAULT;
1024 break;
1025 }
1026 if (stats.session_id != 0) {
1027 /* resend to session ioctl handler */
1028 struct l2tp_session *session =
James Chapmanf7faffa2010-04-02 06:18:49 +00001029 l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
James Chapmanfd558d12010-04-02 06:18:33 +00001030 if (session != NULL)
1031 err = pppol2tp_session_ioctl(session, cmd, arg);
1032 else
1033 err = -EBADR;
1034 break;
1035 }
1036#ifdef CONFIG_XFRM
1037 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1038#endif
1039 pppol2tp_copy_stats(&stats, &tunnel->stats);
1040 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1041 err = -EFAULT;
1042 break;
1043 }
1044 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1045 "%s: get L2TP stats\n", tunnel->name);
1046 err = 0;
1047 break;
1048
1049 default:
1050 err = -ENOSYS;
1051 break;
1052 }
1053
1054 sock_put(sk);
1055
1056 return err;
1057}
1058
1059/* Main ioctl() handler.
1060 * Dispatch to tunnel or session helpers depending on the socket.
1061 */
1062static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1063 unsigned long arg)
1064{
1065 struct sock *sk = sock->sk;
1066 struct l2tp_session *session;
1067 struct l2tp_tunnel *tunnel;
1068 struct pppol2tp_session *ps;
1069 int err;
1070
1071 if (!sk)
1072 return 0;
1073
1074 err = -EBADF;
1075 if (sock_flag(sk, SOCK_DEAD) != 0)
1076 goto end;
1077
1078 err = -ENOTCONN;
1079 if ((sk->sk_user_data == NULL) ||
1080 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1081 goto end;
1082
1083 /* Get session context from the socket */
1084 err = -EBADF;
1085 session = pppol2tp_sock_to_session(sk);
1086 if (session == NULL)
1087 goto end;
1088
1089 /* Special case: if session's session_id is zero, treat ioctl as a
1090 * tunnel ioctl
1091 */
1092 ps = l2tp_session_priv(session);
1093 if ((session->session_id == 0) &&
1094 (session->peer_session_id == 0)) {
1095 err = -EBADF;
1096 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1097 if (tunnel == NULL)
1098 goto end_put_sess;
1099
1100 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1101 sock_put(ps->tunnel_sock);
1102 goto end_put_sess;
1103 }
1104
1105 err = pppol2tp_session_ioctl(session, cmd, arg);
1106
1107end_put_sess:
1108 sock_put(sk);
1109end:
1110 return err;
1111}
1112
1113/*****************************************************************************
1114 * setsockopt() / getsockopt() support.
1115 *
1116 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1117 * sockets. In order to control kernel tunnel features, we allow userspace to
1118 * create a special "tunnel" PPPoX socket which is used for control only.
1119 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1120 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1121 *****************************************************************************/
1122
1123/* Tunnel setsockopt() helper.
1124 */
1125static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1126 struct l2tp_tunnel *tunnel,
1127 int optname, int val)
1128{
1129 int err = 0;
1130
1131 switch (optname) {
1132 case PPPOL2TP_SO_DEBUG:
1133 tunnel->debug = val;
1134 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1135 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1136 break;
1137
1138 default:
1139 err = -ENOPROTOOPT;
1140 break;
1141 }
1142
1143 return err;
1144}
1145
1146/* Session setsockopt helper.
1147 */
1148static int pppol2tp_session_setsockopt(struct sock *sk,
1149 struct l2tp_session *session,
1150 int optname, int val)
1151{
1152 int err = 0;
1153 struct pppol2tp_session *ps = l2tp_session_priv(session);
1154
1155 switch (optname) {
1156 case PPPOL2TP_SO_RECVSEQ:
1157 if ((val != 0) && (val != 1)) {
1158 err = -EINVAL;
1159 break;
1160 }
1161 session->recv_seq = val ? -1 : 0;
1162 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1163 "%s: set recv_seq=%d\n", session->name, session->recv_seq);
1164 break;
1165
1166 case PPPOL2TP_SO_SENDSEQ:
1167 if ((val != 0) && (val != 1)) {
1168 err = -EINVAL;
1169 break;
1170 }
1171 session->send_seq = val ? -1 : 0;
1172 {
1173 struct sock *ssk = ps->sock;
1174 struct pppox_sock *po = pppox_sk(ssk);
1175 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1176 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1177 }
1178 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1179 "%s: set send_seq=%d\n", session->name, session->send_seq);
1180 break;
1181
1182 case PPPOL2TP_SO_LNSMODE:
1183 if ((val != 0) && (val != 1)) {
1184 err = -EINVAL;
1185 break;
1186 }
1187 session->lns_mode = val ? -1 : 0;
1188 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1189 "%s: set lns_mode=%d\n", session->name, session->lns_mode);
1190 break;
1191
1192 case PPPOL2TP_SO_DEBUG:
1193 session->debug = val;
1194 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1195 "%s: set debug=%x\n", session->name, session->debug);
1196 break;
1197
1198 case PPPOL2TP_SO_REORDERTO:
1199 session->reorder_timeout = msecs_to_jiffies(val);
1200 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1201 "%s: set reorder_timeout=%d\n", session->name, session->reorder_timeout);
1202 break;
1203
1204 default:
1205 err = -ENOPROTOOPT;
1206 break;
1207 }
1208
1209 return err;
1210}
1211
1212/* Main setsockopt() entry point.
1213 * Does API checks, then calls either the tunnel or session setsockopt
1214 * handler, according to whether the PPPoL2TP socket is a for a regular
1215 * session or the special tunnel type.
1216 */
1217static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1218 char __user *optval, unsigned int optlen)
1219{
1220 struct sock *sk = sock->sk;
1221 struct l2tp_session *session;
1222 struct l2tp_tunnel *tunnel;
1223 struct pppol2tp_session *ps;
1224 int val;
1225 int err;
1226
1227 if (level != SOL_PPPOL2TP)
1228 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1229
1230 if (optlen < sizeof(int))
1231 return -EINVAL;
1232
1233 if (get_user(val, (int __user *)optval))
1234 return -EFAULT;
1235
1236 err = -ENOTCONN;
1237 if (sk->sk_user_data == NULL)
1238 goto end;
1239
1240 /* Get session context from the socket */
1241 err = -EBADF;
1242 session = pppol2tp_sock_to_session(sk);
1243 if (session == NULL)
1244 goto end;
1245
1246 /* Special case: if session_id == 0x0000, treat as operation on tunnel
1247 */
1248 ps = l2tp_session_priv(session);
1249 if ((session->session_id == 0) &&
1250 (session->peer_session_id == 0)) {
1251 err = -EBADF;
1252 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1253 if (tunnel == NULL)
1254 goto end_put_sess;
1255
1256 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1257 sock_put(ps->tunnel_sock);
1258 } else
1259 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1260
1261 err = 0;
1262
1263end_put_sess:
1264 sock_put(sk);
1265end:
1266 return err;
1267}
1268
1269/* Tunnel getsockopt helper. Called with sock locked.
1270 */
1271static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1272 struct l2tp_tunnel *tunnel,
1273 int optname, int *val)
1274{
1275 int err = 0;
1276
1277 switch (optname) {
1278 case PPPOL2TP_SO_DEBUG:
1279 *val = tunnel->debug;
1280 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1281 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
1282 break;
1283
1284 default:
1285 err = -ENOPROTOOPT;
1286 break;
1287 }
1288
1289 return err;
1290}
1291
1292/* Session getsockopt helper. Called with sock locked.
1293 */
1294static int pppol2tp_session_getsockopt(struct sock *sk,
1295 struct l2tp_session *session,
1296 int optname, int *val)
1297{
1298 int err = 0;
1299
1300 switch (optname) {
1301 case PPPOL2TP_SO_RECVSEQ:
1302 *val = session->recv_seq;
1303 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1304 "%s: get recv_seq=%d\n", session->name, *val);
1305 break;
1306
1307 case PPPOL2TP_SO_SENDSEQ:
1308 *val = session->send_seq;
1309 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1310 "%s: get send_seq=%d\n", session->name, *val);
1311 break;
1312
1313 case PPPOL2TP_SO_LNSMODE:
1314 *val = session->lns_mode;
1315 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1316 "%s: get lns_mode=%d\n", session->name, *val);
1317 break;
1318
1319 case PPPOL2TP_SO_DEBUG:
1320 *val = session->debug;
1321 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1322 "%s: get debug=%d\n", session->name, *val);
1323 break;
1324
1325 case PPPOL2TP_SO_REORDERTO:
1326 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1327 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1328 "%s: get reorder_timeout=%d\n", session->name, *val);
1329 break;
1330
1331 default:
1332 err = -ENOPROTOOPT;
1333 }
1334
1335 return err;
1336}
1337
1338/* Main getsockopt() entry point.
1339 * Does API checks, then calls either the tunnel or session getsockopt
1340 * handler, according to whether the PPPoX socket is a for a regular session
1341 * or the special tunnel type.
1342 */
1343static int pppol2tp_getsockopt(struct socket *sock, int level,
1344 int optname, char __user *optval, int __user *optlen)
1345{
1346 struct sock *sk = sock->sk;
1347 struct l2tp_session *session;
1348 struct l2tp_tunnel *tunnel;
1349 int val, len;
1350 int err;
1351 struct pppol2tp_session *ps;
1352
1353 if (level != SOL_PPPOL2TP)
1354 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1355
1356 if (get_user(len, (int __user *) optlen))
1357 return -EFAULT;
1358
1359 len = min_t(unsigned int, len, sizeof(int));
1360
1361 if (len < 0)
1362 return -EINVAL;
1363
1364 err = -ENOTCONN;
1365 if (sk->sk_user_data == NULL)
1366 goto end;
1367
1368 /* Get the session context */
1369 err = -EBADF;
1370 session = pppol2tp_sock_to_session(sk);
1371 if (session == NULL)
1372 goto end;
1373
1374 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
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_getsockopt(sk, tunnel, optname, &val);
1384 sock_put(ps->tunnel_sock);
1385 } else
1386 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1387
1388 err = -EFAULT;
1389 if (put_user(len, (int __user *) optlen))
1390 goto end_put_sess;
1391
1392 if (copy_to_user((void __user *) optval, &val, len))
1393 goto end_put_sess;
1394
1395 err = 0;
1396
1397end_put_sess:
1398 sock_put(sk);
1399end:
1400 return err;
1401}
1402
1403/*****************************************************************************
1404 * /proc filesystem for debug
James Chapmanf7faffa2010-04-02 06:18:49 +00001405 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1406 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
James Chapmanfd558d12010-04-02 06:18:33 +00001407 *****************************************************************************/
1408
1409static unsigned int pppol2tp_net_id;
1410
1411#ifdef CONFIG_PROC_FS
1412
1413struct pppol2tp_seq_data {
1414 struct seq_net_private p;
1415 int tunnel_idx; /* current tunnel */
1416 int session_idx; /* index of session within current tunnel */
1417 struct l2tp_tunnel *tunnel;
1418 struct l2tp_session *session; /* NULL means get next tunnel */
1419};
1420
1421static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1422{
James Chapmanf7faffa2010-04-02 06:18:49 +00001423 for (;;) {
1424 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1425 pd->tunnel_idx++;
1426
1427 if (pd->tunnel == NULL)
1428 break;
1429
1430 /* Ignore L2TPv3 tunnels */
1431 if (pd->tunnel->version < 3)
1432 break;
1433 }
James Chapmanfd558d12010-04-02 06:18:33 +00001434}
1435
1436static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1437{
1438 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1439 pd->session_idx++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001440
James Chapmanfd558d12010-04-02 06:18:33 +00001441 if (pd->session == NULL) {
1442 pd->session_idx = 0;
1443 pppol2tp_next_tunnel(net, pd);
1444 }
1445}
1446
1447static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1448{
1449 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1450 loff_t pos = *offs;
1451 struct net *net;
1452
1453 if (!pos)
1454 goto out;
1455
1456 BUG_ON(m->private == NULL);
1457 pd = m->private;
1458 net = seq_file_net(m);
1459
1460 if (pd->tunnel == NULL)
1461 pppol2tp_next_tunnel(net, pd);
1462 else
1463 pppol2tp_next_session(net, pd);
1464
1465 /* NULL tunnel and session indicates end of list */
1466 if ((pd->tunnel == NULL) && (pd->session == NULL))
1467 pd = NULL;
1468
1469out:
1470 return pd;
1471}
1472
1473static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1474{
1475 (*pos)++;
1476 return NULL;
1477}
1478
1479static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1480{
1481 /* nothing to do */
1482}
1483
1484static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1485{
1486 struct l2tp_tunnel *tunnel = v;
1487
1488 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1489 tunnel->name,
1490 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1491 atomic_read(&tunnel->ref_count) - 1);
1492 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1493 tunnel->debug,
1494 (unsigned long long)tunnel->stats.tx_packets,
1495 (unsigned long long)tunnel->stats.tx_bytes,
1496 (unsigned long long)tunnel->stats.tx_errors,
1497 (unsigned long long)tunnel->stats.rx_packets,
1498 (unsigned long long)tunnel->stats.rx_bytes,
1499 (unsigned long long)tunnel->stats.rx_errors);
1500}
1501
1502static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1503{
1504 struct l2tp_session *session = v;
1505 struct l2tp_tunnel *tunnel = session->tunnel;
1506 struct pppol2tp_session *ps = l2tp_session_priv(session);
James Chapman93454712010-04-02 06:18:44 +00001507 struct pppox_sock *po = pppox_sk(ps->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001508 u32 ip = 0;
1509 u16 port = 0;
1510
1511 if (tunnel->sock) {
1512 struct inet_sock *inet = inet_sk(tunnel->sock);
1513 ip = ntohl(inet->inet_saddr);
1514 port = ntohs(inet->inet_sport);
1515 }
1516
1517 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1518 "%04X/%04X %d %c\n",
1519 session->name, ip, port,
1520 tunnel->tunnel_id,
1521 session->session_id,
1522 tunnel->peer_tunnel_id,
1523 session->peer_session_id,
1524 ps->sock->sk_state,
1525 (session == ps->sock->sk_user_data) ?
1526 'Y' : 'N');
1527 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1528 session->mtu, session->mru,
1529 session->recv_seq ? 'R' : '-',
1530 session->send_seq ? 'S' : '-',
1531 session->lns_mode ? "LNS" : "LAC",
1532 session->debug,
1533 jiffies_to_msecs(session->reorder_timeout));
1534 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1535 session->nr, session->ns,
1536 (unsigned long long)session->stats.tx_packets,
1537 (unsigned long long)session->stats.tx_bytes,
1538 (unsigned long long)session->stats.tx_errors,
1539 (unsigned long long)session->stats.rx_packets,
1540 (unsigned long long)session->stats.rx_bytes,
1541 (unsigned long long)session->stats.rx_errors);
James Chapman93454712010-04-02 06:18:44 +00001542
1543 if (po)
1544 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
James Chapmanfd558d12010-04-02 06:18:33 +00001545}
1546
1547static int pppol2tp_seq_show(struct seq_file *m, void *v)
1548{
1549 struct pppol2tp_seq_data *pd = v;
1550
1551 /* display header on line 1 */
1552 if (v == SEQ_START_TOKEN) {
1553 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1554 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1555 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1556 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1557 "dest-tid/sid state user-data-ok\n");
1558 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1559 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1560 goto out;
1561 }
1562
1563 /* Show the tunnel or session context.
1564 */
1565 if (pd->session == NULL)
1566 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1567 else
1568 pppol2tp_seq_session_show(m, pd->session);
1569
1570out:
1571 return 0;
1572}
1573
1574static const struct seq_operations pppol2tp_seq_ops = {
1575 .start = pppol2tp_seq_start,
1576 .next = pppol2tp_seq_next,
1577 .stop = pppol2tp_seq_stop,
1578 .show = pppol2tp_seq_show,
1579};
1580
1581/* Called when our /proc file is opened. We allocate data for use when
1582 * iterating our tunnel / session contexts and store it in the private
1583 * data of the seq_file.
1584 */
1585static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1586{
1587 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1588 sizeof(struct pppol2tp_seq_data));
1589}
1590
1591static const struct file_operations pppol2tp_proc_fops = {
1592 .owner = THIS_MODULE,
1593 .open = pppol2tp_proc_open,
1594 .read = seq_read,
1595 .llseek = seq_lseek,
1596 .release = seq_release_net,
1597};
1598
1599#endif /* CONFIG_PROC_FS */
1600
1601/*****************************************************************************
1602 * Network namespace
1603 *****************************************************************************/
1604
1605static __net_init int pppol2tp_init_net(struct net *net)
1606{
1607 struct proc_dir_entry *pde;
1608 int err = 0;
1609
1610 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1611 if (!pde) {
1612 err = -ENOMEM;
1613 goto out;
1614 }
1615
1616out:
1617 return err;
1618}
1619
1620static __net_exit void pppol2tp_exit_net(struct net *net)
1621{
1622 proc_net_remove(net, "pppol2tp");
1623}
1624
1625static struct pernet_operations pppol2tp_net_ops = {
1626 .init = pppol2tp_init_net,
1627 .exit = pppol2tp_exit_net,
1628 .id = &pppol2tp_net_id,
1629};
1630
1631/*****************************************************************************
1632 * Init and cleanup
1633 *****************************************************************************/
1634
1635static const struct proto_ops pppol2tp_ops = {
1636 .family = AF_PPPOX,
1637 .owner = THIS_MODULE,
1638 .release = pppol2tp_release,
1639 .bind = sock_no_bind,
1640 .connect = pppol2tp_connect,
1641 .socketpair = sock_no_socketpair,
1642 .accept = sock_no_accept,
1643 .getname = pppol2tp_getname,
1644 .poll = datagram_poll,
1645 .listen = sock_no_listen,
1646 .shutdown = sock_no_shutdown,
1647 .setsockopt = pppol2tp_setsockopt,
1648 .getsockopt = pppol2tp_getsockopt,
1649 .sendmsg = pppol2tp_sendmsg,
1650 .recvmsg = pppol2tp_recvmsg,
1651 .mmap = sock_no_mmap,
1652 .ioctl = pppox_ioctl,
1653};
1654
1655static struct pppox_proto pppol2tp_proto = {
1656 .create = pppol2tp_create,
1657 .ioctl = pppol2tp_ioctl
1658};
1659
1660static int __init pppol2tp_init(void)
1661{
1662 int err;
1663
1664 err = register_pernet_device(&pppol2tp_net_ops);
1665 if (err)
1666 goto out;
1667
1668 err = proto_register(&pppol2tp_sk_proto, 0);
1669 if (err)
1670 goto out_unregister_pppol2tp_pernet;
1671
1672 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1673 if (err)
1674 goto out_unregister_pppol2tp_proto;
1675
1676 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
1677 PPPOL2TP_DRV_VERSION);
1678
1679out:
1680 return err;
1681out_unregister_pppol2tp_proto:
1682 proto_unregister(&pppol2tp_sk_proto);
1683out_unregister_pppol2tp_pernet:
1684 unregister_pernet_device(&pppol2tp_net_ops);
1685 goto out;
1686}
1687
1688static void __exit pppol2tp_exit(void)
1689{
1690 unregister_pppox_proto(PX_PROTO_OL2TP);
1691 proto_unregister(&pppol2tp_sk_proto);
1692 unregister_pernet_device(&pppol2tp_net_ops);
1693}
1694
1695module_init(pppol2tp_init);
1696module_exit(pppol2tp_exit);
1697
1698MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1699MODULE_DESCRIPTION("PPP over L2TP over UDP");
1700MODULE_LICENSE("GPL");
1701MODULE_VERSION(PPPOL2TP_DRV_VERSION);