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