blob: 904bc098790d18bdaef974b9f7ab7df5f482aee2 [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 */
Guillaume Nault3bf35eb2013-06-12 16:07:23 +0200353 error = memcpy_fromiovec(skb_put(skb, total_len), m->msg_iov,
354 total_len);
James Chapmanfd558d12010-04-02 06:18:33 +0000355 if (error < 0) {
356 kfree_skb(skb);
357 goto error_put_sess_tun;
358 }
James Chapmanfd558d12010-04-02 06:18:33 +0000359
Eric Dumazet120bc4f2013-10-10 06:30:09 -0700360 local_bh_disable();
James Chapmanfd558d12010-04-02 06:18:33 +0000361 l2tp_xmit_skb(session, skb, session->hdr_len);
Eric Dumazet120bc4f2013-10-10 06:30:09 -0700362 local_bh_enable();
James Chapmanfd558d12010-04-02 06:18:33 +0000363
364 sock_put(ps->tunnel_sock);
Guillaume Nault136d76d2013-03-01 05:02:02 +0000365 sock_put(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000366
Guillaume Nault137b5d82013-06-12 16:07:36 +0200367 return total_len;
James Chapmanfd558d12010-04-02 06:18:33 +0000368
369error_put_sess_tun:
370 sock_put(ps->tunnel_sock);
371error_put_sess:
372 sock_put(sk);
373error:
374 return error;
375}
376
377/* Transmit function called by generic PPP driver. Sends PPP frame
378 * over PPPoL2TP socket.
379 *
380 * This is almost the same as pppol2tp_sendmsg(), but rather than
381 * being called with a msghdr from userspace, it is called with a skb
382 * from the kernel.
383 *
384 * The supplied skb from ppp doesn't have enough headroom for the
385 * insertion of L2TP, UDP and IP headers so we need to allocate more
386 * headroom in the skb. This will create a cloned skb. But we must be
387 * careful in the error case because the caller will expect to free
388 * the skb it supplied, not our cloned skb. So we take care to always
389 * leave the original skb unfreed if we return an error.
390 */
391static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
392{
393 static const u8 ppph[2] = { 0xff, 0x03 };
394 struct sock *sk = (struct sock *) chan->private;
395 struct sock *sk_tun;
James Chapmanfd558d12010-04-02 06:18:33 +0000396 struct l2tp_session *session;
397 struct l2tp_tunnel *tunnel;
398 struct pppol2tp_session *ps;
399 int old_headroom;
400 int new_headroom;
Eric Dumazet09df57c2011-10-07 05:45:57 +0000401 int uhlen, headroom;
James Chapmanfd558d12010-04-02 06:18:33 +0000402
403 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
404 goto abort;
405
406 /* Get session and tunnel contexts from the socket */
407 session = pppol2tp_sock_to_session(sk);
408 if (session == NULL)
409 goto abort;
410
411 ps = l2tp_session_priv(session);
412 sk_tun = ps->tunnel_sock;
413 if (sk_tun == NULL)
414 goto abort_put_sess;
415 tunnel = l2tp_sock_to_tunnel(sk_tun);
416 if (tunnel == NULL)
417 goto abort_put_sess;
418
James Chapmanfd558d12010-04-02 06:18:33 +0000419 old_headroom = skb_headroom(skb);
Eric Dumazet09df57c2011-10-07 05:45:57 +0000420 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
421 headroom = NET_SKB_PAD +
422 sizeof(struct iphdr) + /* IP header */
423 uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
424 session->hdr_len + /* L2TP header */
425 sizeof(ppph); /* PPP header */
426 if (skb_cow_head(skb, headroom))
James Chapmanfd558d12010-04-02 06:18:33 +0000427 goto abort_put_sess_tun;
428
429 new_headroom = skb_headroom(skb);
430 skb->truesize += new_headroom - old_headroom;
431
432 /* Setup PPP header */
433 __skb_push(skb, sizeof(ppph));
434 skb->data[0] = ppph[0];
435 skb->data[1] = ppph[1];
436
Eric Dumazet120bc4f2013-10-10 06:30:09 -0700437 local_bh_disable();
James Chapmane0d44352010-04-02 06:18:54 +0000438 l2tp_xmit_skb(session, skb, session->hdr_len);
Eric Dumazet120bc4f2013-10-10 06:30:09 -0700439 local_bh_enable();
James Chapmanfd558d12010-04-02 06:18:33 +0000440
441 sock_put(sk_tun);
442 sock_put(sk);
443 return 1;
444
445abort_put_sess_tun:
446 sock_put(sk_tun);
447abort_put_sess:
448 sock_put(sk);
449abort:
450 /* Free the original skb */
451 kfree_skb(skb);
452 return 1;
453}
454
455/*****************************************************************************
456 * Session (and tunnel control) socket create/destroy.
457 *****************************************************************************/
458
459/* Called by l2tp_core when a session socket is being closed.
460 */
461static void pppol2tp_session_close(struct l2tp_session *session)
462{
463 struct pppol2tp_session *ps = l2tp_session_priv(session);
464 struct sock *sk = ps->sock;
465 struct sk_buff *skb;
466
467 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
468
469 if (session->session_id == 0)
470 goto out;
471
472 if (sk != NULL) {
473 lock_sock(sk);
474
475 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
476 pppox_unbind_sock(sk);
477 sk->sk_state = PPPOX_DEAD;
478 sk->sk_state_change(sk);
479 }
480
481 /* Purge any queued data */
482 skb_queue_purge(&sk->sk_receive_queue);
483 skb_queue_purge(&sk->sk_write_queue);
484 while ((skb = skb_dequeue(&session->reorder_q))) {
485 kfree_skb(skb);
486 sock_put(sk);
487 }
488
489 release_sock(sk);
490 }
491
492out:
493 return;
494}
495
496/* Really kill the session socket. (Called from sock_put() if
497 * refcnt == 0.)
498 */
499static void pppol2tp_session_destruct(struct sock *sk)
500{
501 struct l2tp_session *session;
502
503 if (sk->sk_user_data != NULL) {
504 session = sk->sk_user_data;
505 if (session == NULL)
506 goto out;
507
508 sk->sk_user_data = NULL;
509 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
510 l2tp_session_dec_refcount(session);
511 }
512
513out:
514 return;
515}
516
517/* Called when the PPPoX socket (session) is closed.
518 */
519static int pppol2tp_release(struct socket *sock)
520{
521 struct sock *sk = sock->sk;
522 struct l2tp_session *session;
523 int error;
524
525 if (!sk)
526 return 0;
527
528 error = -EBADF;
529 lock_sock(sk);
530 if (sock_flag(sk, SOCK_DEAD) != 0)
531 goto error;
532
533 pppox_unbind_sock(sk);
534
535 /* Signal the death of the socket. */
536 sk->sk_state = PPPOX_DEAD;
537 sock_orphan(sk);
538 sock->sk = NULL;
539
540 session = pppol2tp_sock_to_session(sk);
541
542 /* Purge any queued data */
543 skb_queue_purge(&sk->sk_receive_queue);
544 skb_queue_purge(&sk->sk_write_queue);
545 if (session != NULL) {
546 struct sk_buff *skb;
547 while ((skb = skb_dequeue(&session->reorder_q))) {
548 kfree_skb(skb);
549 sock_put(sk);
550 }
551 sock_put(sk);
552 }
553
554 release_sock(sk);
555
556 /* This will delete the session context via
557 * pppol2tp_session_destruct() if the socket's refcnt drops to
558 * zero.
559 */
560 sock_put(sk);
561
562 return 0;
563
564error:
565 release_sock(sk);
566 return error;
567}
568
569static struct proto pppol2tp_sk_proto = {
570 .name = "PPPOL2TP",
571 .owner = THIS_MODULE,
572 .obj_size = sizeof(struct pppox_sock),
573};
574
575static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
576{
577 int rc;
578
579 rc = l2tp_udp_encap_recv(sk, skb);
580 if (rc)
581 kfree_skb(skb);
582
583 return NET_RX_SUCCESS;
584}
585
586/* socket() handler. Initialize a new struct sock.
587 */
588static int pppol2tp_create(struct net *net, struct socket *sock)
589{
590 int error = -ENOMEM;
591 struct sock *sk;
592
593 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto);
594 if (!sk)
595 goto out;
596
597 sock_init_data(sock, sk);
598
599 sock->state = SS_UNCONNECTED;
600 sock->ops = &pppol2tp_ops;
601
602 sk->sk_backlog_rcv = pppol2tp_backlog_recv;
603 sk->sk_protocol = PX_PROTO_OL2TP;
604 sk->sk_family = PF_PPPOX;
605 sk->sk_state = PPPOX_NONE;
606 sk->sk_type = SOCK_STREAM;
607 sk->sk_destruct = pppol2tp_session_destruct;
608
609 error = 0;
610
611out:
612 return error;
613}
614
David S. Millerf66ef2d2010-04-03 15:01:37 -0700615#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
James Chapman0ad66142010-04-02 06:19:33 +0000616static void pppol2tp_show(struct seq_file *m, void *arg)
617{
618 struct l2tp_session *session = arg;
619 struct pppol2tp_session *ps = l2tp_session_priv(session);
620
621 if (ps) {
622 struct pppox_sock *po = pppox_sk(ps->sock);
623 if (po)
624 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
625 }
626}
627#endif
628
James Chapmanfd558d12010-04-02 06:18:33 +0000629/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
630 */
631static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
632 int sockaddr_len, int flags)
633{
634 struct sock *sk = sock->sk;
635 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
James Chapmane0d44352010-04-02 06:18:54 +0000636 struct sockaddr_pppol2tpv3 *sp3 = (struct sockaddr_pppol2tpv3 *) uservaddr;
James Chapmanfd558d12010-04-02 06:18:33 +0000637 struct pppox_sock *po = pppox_sk(sk);
638 struct l2tp_session *session = NULL;
639 struct l2tp_tunnel *tunnel;
640 struct pppol2tp_session *ps;
641 struct dst_entry *dst;
642 struct l2tp_session_cfg cfg = { 0, };
643 int error = 0;
James Chapmane0d44352010-04-02 06:18:54 +0000644 u32 tunnel_id, peer_tunnel_id;
645 u32 session_id, peer_session_id;
646 int ver = 2;
647 int fd;
James Chapmanfd558d12010-04-02 06:18:33 +0000648
649 lock_sock(sk);
650
651 error = -EINVAL;
652 if (sp->sa_protocol != PX_PROTO_OL2TP)
653 goto end;
654
655 /* Check for already bound sockets */
656 error = -EBUSY;
657 if (sk->sk_state & PPPOX_CONNECTED)
658 goto end;
659
660 /* We don't supporting rebinding anyway */
661 error = -EALREADY;
662 if (sk->sk_user_data)
663 goto end; /* socket is already attached */
664
James Chapmane0d44352010-04-02 06:18:54 +0000665 /* Get params from socket address. Handle L2TPv2 and L2TPv3 */
666 if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
667 fd = sp->pppol2tp.fd;
668 tunnel_id = sp->pppol2tp.s_tunnel;
669 peer_tunnel_id = sp->pppol2tp.d_tunnel;
670 session_id = sp->pppol2tp.s_session;
671 peer_session_id = sp->pppol2tp.d_session;
672 } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
673 ver = 3;
674 fd = sp3->pppol2tp.fd;
675 tunnel_id = sp3->pppol2tp.s_tunnel;
676 peer_tunnel_id = sp3->pppol2tp.d_tunnel;
677 session_id = sp3->pppol2tp.s_session;
678 peer_session_id = sp3->pppol2tp.d_session;
679 } else {
680 error = -EINVAL;
681 goto end; /* bad socket address */
682 }
683
684 /* Don't bind if tunnel_id is 0 */
James Chapmanfd558d12010-04-02 06:18:33 +0000685 error = -EINVAL;
James Chapmane0d44352010-04-02 06:18:54 +0000686 if (tunnel_id == 0)
James Chapmanfd558d12010-04-02 06:18:33 +0000687 goto end;
688
James Chapman309795f2010-04-02 06:19:10 +0000689 tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id);
690
James Chapmane0d44352010-04-02 06:18:54 +0000691 /* Special case: create tunnel context if session_id and
692 * peer_session_id is 0. Otherwise look up tunnel using supplied
James Chapmanfd558d12010-04-02 06:18:33 +0000693 * tunnel id.
694 */
James Chapmane0d44352010-04-02 06:18:54 +0000695 if ((session_id == 0) && (peer_session_id == 0)) {
James Chapman309795f2010-04-02 06:19:10 +0000696 if (tunnel == NULL) {
697 struct l2tp_tunnel_cfg tcfg = {
698 .encap = L2TP_ENCAPTYPE_UDP,
699 .debug = 0,
700 };
701 error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
702 if (error < 0)
703 goto end;
704 }
James Chapmanfd558d12010-04-02 06:18:33 +0000705 } else {
James Chapmanfd558d12010-04-02 06:18:33 +0000706 /* Error if we can't find the tunnel */
707 error = -ENOENT;
708 if (tunnel == NULL)
709 goto end;
710
711 /* Error if socket is not prepped */
712 if (tunnel->sock == NULL)
713 goto end;
714 }
715
716 if (tunnel->recv_payload_hook == NULL)
717 tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
718
James Chapman309795f2010-04-02 06:19:10 +0000719 if (tunnel->peer_tunnel_id == 0) {
720 if (ver == 2)
721 tunnel->peer_tunnel_id = sp->pppol2tp.d_tunnel;
722 else
723 tunnel->peer_tunnel_id = sp3->pppol2tp.d_tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000724 }
725
James Chapman309795f2010-04-02 06:19:10 +0000726 /* Create session if it doesn't already exist. We handle the
727 * case where a session was previously created by the netlink
728 * interface by checking that the session doesn't already have
729 * a socket and its tunnel socket are what we expect. If any
730 * of those checks fail, return EEXIST to the caller.
731 */
732 session = l2tp_session_find(sock_net(sk), tunnel, session_id);
733 if (session == NULL) {
734 /* Default MTU must allow space for UDP/L2TP/PPP
735 * headers.
736 */
737 cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
738
739 /* Allocate and initialize a new session context. */
740 session = l2tp_session_create(sizeof(struct pppol2tp_session),
741 tunnel, session_id,
742 peer_session_id, &cfg);
743 if (session == NULL) {
744 error = -ENOMEM;
745 goto end;
746 }
747 } else {
748 ps = l2tp_session_priv(session);
749 error = -EEXIST;
750 if (ps->sock != NULL)
751 goto end;
752
753 /* consistency checks */
754 if (ps->tunnel_sock != tunnel->sock)
755 goto end;
756 }
757
758 /* Associate session with its PPPoL2TP socket */
James Chapmanfd558d12010-04-02 06:18:33 +0000759 ps = l2tp_session_priv(session);
760 ps->owner = current->pid;
761 ps->sock = sk;
762 ps->tunnel_sock = tunnel->sock;
763
764 session->recv_skb = pppol2tp_recv;
765 session->session_close = pppol2tp_session_close;
David S. Millerf66ef2d2010-04-03 15:01:37 -0700766#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
James Chapman0ad66142010-04-02 06:19:33 +0000767 session->show = pppol2tp_show;
768#endif
James Chapmanfd558d12010-04-02 06:18:33 +0000769
770 /* We need to know each time a skb is dropped from the reorder
771 * queue.
772 */
773 session->ref = pppol2tp_session_sock_hold;
774 session->deref = pppol2tp_session_sock_put;
775
776 /* If PMTU discovery was enabled, use the MTU that was discovered */
777 dst = sk_dst_get(sk);
778 if (dst != NULL) {
779 u32 pmtu = dst_mtu(__sk_dst_get(sk));
780 if (pmtu != 0)
781 session->mtu = session->mru = pmtu -
782 PPPOL2TP_HEADER_OVERHEAD;
783 dst_release(dst);
784 }
785
786 /* Special case: if source & dest session_id == 0x0000, this
787 * socket is being created to manage the tunnel. Just set up
788 * the internal context for use by ioctl() and sockopt()
789 * handlers.
790 */
791 if ((session->session_id == 0) &&
792 (session->peer_session_id == 0)) {
793 error = 0;
794 goto out_no_ppp;
795 }
796
797 /* The only header we need to worry about is the L2TP
798 * header. This size is different depending on whether
799 * sequence numbers are enabled for the data channel.
800 */
801 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
802
803 po->chan.private = sk;
804 po->chan.ops = &pppol2tp_chan_ops;
805 po->chan.mtu = session->mtu;
806
807 error = ppp_register_net_channel(sock_net(sk), &po->chan);
808 if (error)
809 goto end;
810
811out_no_ppp:
812 /* This is how we get the session context from the socket. */
813 sk->sk_user_data = session;
814 sk->sk_state = PPPOX_CONNECTED;
815 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
816 "%s: created\n", session->name);
817
818end:
819 release_sock(sk);
820
821 return error;
822}
823
James Chapman309795f2010-04-02 06:19:10 +0000824#ifdef CONFIG_L2TP_V3
825
826/* Called when creating sessions via the netlink interface.
827 */
828static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
829{
830 int error;
831 struct l2tp_tunnel *tunnel;
832 struct l2tp_session *session;
833 struct pppol2tp_session *ps;
834
835 tunnel = l2tp_tunnel_find(net, tunnel_id);
836
837 /* Error if we can't find the tunnel */
838 error = -ENOENT;
839 if (tunnel == NULL)
840 goto out;
841
842 /* Error if tunnel socket is not prepped */
843 if (tunnel->sock == NULL)
844 goto out;
845
846 /* Check that this session doesn't already exist */
847 error = -EEXIST;
848 session = l2tp_session_find(net, tunnel, session_id);
849 if (session != NULL)
850 goto out;
851
852 /* Default MTU values. */
853 if (cfg->mtu == 0)
854 cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
855 if (cfg->mru == 0)
856 cfg->mru = cfg->mtu;
857
858 /* Allocate and initialize a new session context. */
859 error = -ENOMEM;
860 session = l2tp_session_create(sizeof(struct pppol2tp_session),
861 tunnel, session_id,
862 peer_session_id, cfg);
863 if (session == NULL)
864 goto out;
865
866 ps = l2tp_session_priv(session);
867 ps->tunnel_sock = tunnel->sock;
868
869 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
870 "%s: created\n", session->name);
871
872 error = 0;
873
874out:
875 return error;
876}
877
878/* Called when deleting sessions via the netlink interface.
879 */
880static int pppol2tp_session_delete(struct l2tp_session *session)
881{
882 struct pppol2tp_session *ps = l2tp_session_priv(session);
883
884 if (ps->sock == NULL)
885 l2tp_session_dec_refcount(session);
886
887 return 0;
888}
889
890#endif /* CONFIG_L2TP_V3 */
891
James Chapmanfd558d12010-04-02 06:18:33 +0000892/* getname() support.
893 */
894static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
895 int *usockaddr_len, int peer)
896{
James Chapmane0d44352010-04-02 06:18:54 +0000897 int len = 0;
James Chapmanfd558d12010-04-02 06:18:33 +0000898 int error = 0;
899 struct l2tp_session *session;
900 struct l2tp_tunnel *tunnel;
901 struct sock *sk = sock->sk;
902 struct inet_sock *inet;
903 struct pppol2tp_session *pls;
904
905 error = -ENOTCONN;
906 if (sk == NULL)
907 goto end;
908 if (sk->sk_state != PPPOX_CONNECTED)
909 goto end;
910
911 error = -EBADF;
912 session = pppol2tp_sock_to_session(sk);
913 if (session == NULL)
914 goto end;
915
916 pls = l2tp_session_priv(session);
917 tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
918 if (tunnel == NULL) {
919 error = -EBADF;
920 goto end_put_sess;
921 }
922
Benjamin LaHaisebbdb32c2012-03-20 03:57:54 +0000923 inet = inet_sk(tunnel->sock);
James Chapmane0d44352010-04-02 06:18:54 +0000924 if (tunnel->version == 2) {
925 struct sockaddr_pppol2tp sp;
926 len = sizeof(sp);
927 memset(&sp, 0, len);
928 sp.sa_family = AF_PPPOX;
929 sp.sa_protocol = PX_PROTO_OL2TP;
930 sp.pppol2tp.fd = tunnel->fd;
931 sp.pppol2tp.pid = pls->owner;
932 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
933 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
934 sp.pppol2tp.s_session = session->session_id;
935 sp.pppol2tp.d_session = session->peer_session_id;
936 sp.pppol2tp.addr.sin_family = AF_INET;
937 sp.pppol2tp.addr.sin_port = inet->inet_dport;
938 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
939 memcpy(uaddr, &sp, len);
940 } else if (tunnel->version == 3) {
941 struct sockaddr_pppol2tpv3 sp;
942 len = sizeof(sp);
943 memset(&sp, 0, len);
944 sp.sa_family = AF_PPPOX;
945 sp.sa_protocol = PX_PROTO_OL2TP;
946 sp.pppol2tp.fd = tunnel->fd;
947 sp.pppol2tp.pid = pls->owner;
948 sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
949 sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
950 sp.pppol2tp.s_session = session->session_id;
951 sp.pppol2tp.d_session = session->peer_session_id;
952 sp.pppol2tp.addr.sin_family = AF_INET;
953 sp.pppol2tp.addr.sin_port = inet->inet_dport;
954 sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
955 memcpy(uaddr, &sp, len);
956 }
James Chapmanfd558d12010-04-02 06:18:33 +0000957
958 *usockaddr_len = len;
959
960 sock_put(pls->tunnel_sock);
961end_put_sess:
962 sock_put(sk);
963 error = 0;
964
965end:
966 return error;
967}
968
969/****************************************************************************
970 * ioctl() handlers.
971 *
972 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
973 * sockets. However, in order to control kernel tunnel features, we allow
974 * userspace to create a special "tunnel" PPPoX socket which is used for
975 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
976 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
977 * calls.
978 ****************************************************************************/
979
980static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
981 struct l2tp_stats *stats)
982{
983 dest->tx_packets = stats->tx_packets;
984 dest->tx_bytes = stats->tx_bytes;
985 dest->tx_errors = stats->tx_errors;
986 dest->rx_packets = stats->rx_packets;
987 dest->rx_bytes = stats->rx_bytes;
988 dest->rx_seq_discards = stats->rx_seq_discards;
989 dest->rx_oos_packets = stats->rx_oos_packets;
990 dest->rx_errors = stats->rx_errors;
991}
992
993/* Session ioctl helper.
994 */
995static int pppol2tp_session_ioctl(struct l2tp_session *session,
996 unsigned int cmd, unsigned long arg)
997{
998 struct ifreq ifr;
999 int err = 0;
1000 struct sock *sk;
1001 int val = (int) arg;
1002 struct pppol2tp_session *ps = l2tp_session_priv(session);
1003 struct l2tp_tunnel *tunnel = session->tunnel;
1004 struct pppol2tp_ioc_stats stats;
1005
1006 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1007 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1008 session->name, cmd, arg);
1009
1010 sk = ps->sock;
1011 sock_hold(sk);
1012
1013 switch (cmd) {
1014 case SIOCGIFMTU:
1015 err = -ENXIO;
1016 if (!(sk->sk_state & PPPOX_CONNECTED))
1017 break;
1018
1019 err = -EFAULT;
1020 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1021 break;
1022 ifr.ifr_mtu = session->mtu;
1023 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1024 break;
1025
1026 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1027 "%s: get mtu=%d\n", session->name, session->mtu);
1028 err = 0;
1029 break;
1030
1031 case SIOCSIFMTU:
1032 err = -ENXIO;
1033 if (!(sk->sk_state & PPPOX_CONNECTED))
1034 break;
1035
1036 err = -EFAULT;
1037 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1038 break;
1039
1040 session->mtu = ifr.ifr_mtu;
1041
1042 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1043 "%s: set mtu=%d\n", session->name, session->mtu);
1044 err = 0;
1045 break;
1046
1047 case PPPIOCGMRU:
1048 err = -ENXIO;
1049 if (!(sk->sk_state & PPPOX_CONNECTED))
1050 break;
1051
1052 err = -EFAULT;
1053 if (put_user(session->mru, (int __user *) arg))
1054 break;
1055
1056 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1057 "%s: get mru=%d\n", session->name, session->mru);
1058 err = 0;
1059 break;
1060
1061 case PPPIOCSMRU:
1062 err = -ENXIO;
1063 if (!(sk->sk_state & PPPOX_CONNECTED))
1064 break;
1065
1066 err = -EFAULT;
1067 if (get_user(val, (int __user *) arg))
1068 break;
1069
1070 session->mru = val;
1071 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1072 "%s: set mru=%d\n", session->name, session->mru);
1073 err = 0;
1074 break;
1075
1076 case PPPIOCGFLAGS:
1077 err = -EFAULT;
1078 if (put_user(ps->flags, (int __user *) arg))
1079 break;
1080
1081 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1082 "%s: get flags=%d\n", session->name, ps->flags);
1083 err = 0;
1084 break;
1085
1086 case PPPIOCSFLAGS:
1087 err = -EFAULT;
1088 if (get_user(val, (int __user *) arg))
1089 break;
1090 ps->flags = val;
1091 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1092 "%s: set flags=%d\n", session->name, ps->flags);
1093 err = 0;
1094 break;
1095
1096 case PPPIOCGL2TPSTATS:
1097 err = -ENXIO;
1098 if (!(sk->sk_state & PPPOX_CONNECTED))
1099 break;
1100
1101 memset(&stats, 0, sizeof(stats));
1102 stats.tunnel_id = tunnel->tunnel_id;
1103 stats.session_id = session->session_id;
1104 pppol2tp_copy_stats(&stats, &session->stats);
1105 if (copy_to_user((void __user *) arg, &stats,
1106 sizeof(stats)))
1107 break;
1108 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1109 "%s: get L2TP stats\n", session->name);
1110 err = 0;
1111 break;
1112
1113 default:
1114 err = -ENOSYS;
1115 break;
1116 }
1117
1118 sock_put(sk);
1119
1120 return err;
1121}
1122
1123/* Tunnel ioctl helper.
1124 *
1125 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1126 * specifies a session_id, the session ioctl handler is called. This allows an
1127 * application to retrieve session stats via a tunnel socket.
1128 */
1129static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
1130 unsigned int cmd, unsigned long arg)
1131{
1132 int err = 0;
1133 struct sock *sk;
1134 struct pppol2tp_ioc_stats stats;
1135
1136 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1137 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
1138 tunnel->name, cmd, arg);
1139
1140 sk = tunnel->sock;
1141 sock_hold(sk);
1142
1143 switch (cmd) {
1144 case PPPIOCGL2TPSTATS:
1145 err = -ENXIO;
1146 if (!(sk->sk_state & PPPOX_CONNECTED))
1147 break;
1148
1149 if (copy_from_user(&stats, (void __user *) arg,
1150 sizeof(stats))) {
1151 err = -EFAULT;
1152 break;
1153 }
1154 if (stats.session_id != 0) {
1155 /* resend to session ioctl handler */
1156 struct l2tp_session *session =
James Chapmanf7faffa2010-04-02 06:18:49 +00001157 l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
James Chapmanfd558d12010-04-02 06:18:33 +00001158 if (session != NULL)
1159 err = pppol2tp_session_ioctl(session, cmd, arg);
1160 else
1161 err = -EBADR;
1162 break;
1163 }
1164#ifdef CONFIG_XFRM
1165 stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1166#endif
1167 pppol2tp_copy_stats(&stats, &tunnel->stats);
1168 if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
1169 err = -EFAULT;
1170 break;
1171 }
1172 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1173 "%s: get L2TP stats\n", tunnel->name);
1174 err = 0;
1175 break;
1176
1177 default:
1178 err = -ENOSYS;
1179 break;
1180 }
1181
1182 sock_put(sk);
1183
1184 return err;
1185}
1186
1187/* Main ioctl() handler.
1188 * Dispatch to tunnel or session helpers depending on the socket.
1189 */
1190static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1191 unsigned long arg)
1192{
1193 struct sock *sk = sock->sk;
1194 struct l2tp_session *session;
1195 struct l2tp_tunnel *tunnel;
1196 struct pppol2tp_session *ps;
1197 int err;
1198
1199 if (!sk)
1200 return 0;
1201
1202 err = -EBADF;
1203 if (sock_flag(sk, SOCK_DEAD) != 0)
1204 goto end;
1205
1206 err = -ENOTCONN;
1207 if ((sk->sk_user_data == NULL) ||
1208 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1209 goto end;
1210
1211 /* Get session context from the socket */
1212 err = -EBADF;
1213 session = pppol2tp_sock_to_session(sk);
1214 if (session == NULL)
1215 goto end;
1216
1217 /* Special case: if session's session_id is zero, treat ioctl as a
1218 * tunnel ioctl
1219 */
1220 ps = l2tp_session_priv(session);
1221 if ((session->session_id == 0) &&
1222 (session->peer_session_id == 0)) {
1223 err = -EBADF;
1224 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1225 if (tunnel == NULL)
1226 goto end_put_sess;
1227
1228 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1229 sock_put(ps->tunnel_sock);
1230 goto end_put_sess;
1231 }
1232
1233 err = pppol2tp_session_ioctl(session, cmd, arg);
1234
1235end_put_sess:
1236 sock_put(sk);
1237end:
1238 return err;
1239}
1240
1241/*****************************************************************************
1242 * setsockopt() / getsockopt() support.
1243 *
1244 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1245 * sockets. In order to control kernel tunnel features, we allow userspace to
1246 * create a special "tunnel" PPPoX socket which is used for control only.
1247 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1248 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1249 *****************************************************************************/
1250
1251/* Tunnel setsockopt() helper.
1252 */
1253static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1254 struct l2tp_tunnel *tunnel,
1255 int optname, int val)
1256{
1257 int err = 0;
1258
1259 switch (optname) {
1260 case PPPOL2TP_SO_DEBUG:
1261 tunnel->debug = val;
1262 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1263 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1264 break;
1265
1266 default:
1267 err = -ENOPROTOOPT;
1268 break;
1269 }
1270
1271 return err;
1272}
1273
1274/* Session setsockopt helper.
1275 */
1276static int pppol2tp_session_setsockopt(struct sock *sk,
1277 struct l2tp_session *session,
1278 int optname, int val)
1279{
1280 int err = 0;
1281 struct pppol2tp_session *ps = l2tp_session_priv(session);
1282
1283 switch (optname) {
1284 case PPPOL2TP_SO_RECVSEQ:
1285 if ((val != 0) && (val != 1)) {
1286 err = -EINVAL;
1287 break;
1288 }
1289 session->recv_seq = val ? -1 : 0;
1290 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1291 "%s: set recv_seq=%d\n", session->name, session->recv_seq);
1292 break;
1293
1294 case PPPOL2TP_SO_SENDSEQ:
1295 if ((val != 0) && (val != 1)) {
1296 err = -EINVAL;
1297 break;
1298 }
1299 session->send_seq = val ? -1 : 0;
1300 {
1301 struct sock *ssk = ps->sock;
1302 struct pppox_sock *po = pppox_sk(ssk);
1303 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1304 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1305 }
1306 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1307 "%s: set send_seq=%d\n", session->name, session->send_seq);
1308 break;
1309
1310 case PPPOL2TP_SO_LNSMODE:
1311 if ((val != 0) && (val != 1)) {
1312 err = -EINVAL;
1313 break;
1314 }
1315 session->lns_mode = val ? -1 : 0;
1316 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1317 "%s: set lns_mode=%d\n", session->name, session->lns_mode);
1318 break;
1319
1320 case PPPOL2TP_SO_DEBUG:
1321 session->debug = val;
1322 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1323 "%s: set debug=%x\n", session->name, session->debug);
1324 break;
1325
1326 case PPPOL2TP_SO_REORDERTO:
1327 session->reorder_timeout = msecs_to_jiffies(val);
1328 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1329 "%s: set reorder_timeout=%d\n", session->name, session->reorder_timeout);
1330 break;
1331
1332 default:
1333 err = -ENOPROTOOPT;
1334 break;
1335 }
1336
1337 return err;
1338}
1339
1340/* Main setsockopt() entry point.
1341 * Does API checks, then calls either the tunnel or session setsockopt
1342 * handler, according to whether the PPPoL2TP socket is a for a regular
1343 * session or the special tunnel type.
1344 */
1345static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1346 char __user *optval, unsigned int optlen)
1347{
1348 struct sock *sk = sock->sk;
1349 struct l2tp_session *session;
1350 struct l2tp_tunnel *tunnel;
1351 struct pppol2tp_session *ps;
1352 int val;
1353 int err;
1354
1355 if (level != SOL_PPPOL2TP)
1356 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1357
1358 if (optlen < sizeof(int))
1359 return -EINVAL;
1360
1361 if (get_user(val, (int __user *)optval))
1362 return -EFAULT;
1363
1364 err = -ENOTCONN;
1365 if (sk->sk_user_data == NULL)
1366 goto end;
1367
1368 /* Get session context from the socket */
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 */
1376 ps = l2tp_session_priv(session);
1377 if ((session->session_id == 0) &&
1378 (session->peer_session_id == 0)) {
1379 err = -EBADF;
1380 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1381 if (tunnel == NULL)
1382 goto end_put_sess;
1383
1384 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
1385 sock_put(ps->tunnel_sock);
1386 } else
1387 err = pppol2tp_session_setsockopt(sk, session, optname, val);
1388
1389 err = 0;
1390
1391end_put_sess:
1392 sock_put(sk);
1393end:
1394 return err;
1395}
1396
1397/* Tunnel getsockopt helper. Called with sock locked.
1398 */
1399static int pppol2tp_tunnel_getsockopt(struct sock *sk,
1400 struct l2tp_tunnel *tunnel,
1401 int optname, int *val)
1402{
1403 int err = 0;
1404
1405 switch (optname) {
1406 case PPPOL2TP_SO_DEBUG:
1407 *val = tunnel->debug;
1408 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1409 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
1410 break;
1411
1412 default:
1413 err = -ENOPROTOOPT;
1414 break;
1415 }
1416
1417 return err;
1418}
1419
1420/* Session getsockopt helper. Called with sock locked.
1421 */
1422static int pppol2tp_session_getsockopt(struct sock *sk,
1423 struct l2tp_session *session,
1424 int optname, int *val)
1425{
1426 int err = 0;
1427
1428 switch (optname) {
1429 case PPPOL2TP_SO_RECVSEQ:
1430 *val = session->recv_seq;
1431 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1432 "%s: get recv_seq=%d\n", session->name, *val);
1433 break;
1434
1435 case PPPOL2TP_SO_SENDSEQ:
1436 *val = session->send_seq;
1437 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1438 "%s: get send_seq=%d\n", session->name, *val);
1439 break;
1440
1441 case PPPOL2TP_SO_LNSMODE:
1442 *val = session->lns_mode;
1443 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1444 "%s: get lns_mode=%d\n", session->name, *val);
1445 break;
1446
1447 case PPPOL2TP_SO_DEBUG:
1448 *val = session->debug;
1449 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1450 "%s: get debug=%d\n", session->name, *val);
1451 break;
1452
1453 case PPPOL2TP_SO_REORDERTO:
1454 *val = (int) jiffies_to_msecs(session->reorder_timeout);
1455 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1456 "%s: get reorder_timeout=%d\n", session->name, *val);
1457 break;
1458
1459 default:
1460 err = -ENOPROTOOPT;
1461 }
1462
1463 return err;
1464}
1465
1466/* Main getsockopt() entry point.
1467 * Does API checks, then calls either the tunnel or session getsockopt
1468 * handler, according to whether the PPPoX socket is a for a regular session
1469 * or the special tunnel type.
1470 */
1471static int pppol2tp_getsockopt(struct socket *sock, int level,
1472 int optname, char __user *optval, int __user *optlen)
1473{
1474 struct sock *sk = sock->sk;
1475 struct l2tp_session *session;
1476 struct l2tp_tunnel *tunnel;
1477 int val, len;
1478 int err;
1479 struct pppol2tp_session *ps;
1480
1481 if (level != SOL_PPPOL2TP)
1482 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1483
1484 if (get_user(len, (int __user *) optlen))
1485 return -EFAULT;
1486
1487 len = min_t(unsigned int, len, sizeof(int));
1488
1489 if (len < 0)
1490 return -EINVAL;
1491
1492 err = -ENOTCONN;
1493 if (sk->sk_user_data == NULL)
1494 goto end;
1495
1496 /* Get the session context */
1497 err = -EBADF;
1498 session = pppol2tp_sock_to_session(sk);
1499 if (session == NULL)
1500 goto end;
1501
1502 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
1503 ps = l2tp_session_priv(session);
1504 if ((session->session_id == 0) &&
1505 (session->peer_session_id == 0)) {
1506 err = -EBADF;
1507 tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
1508 if (tunnel == NULL)
1509 goto end_put_sess;
1510
1511 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
1512 sock_put(ps->tunnel_sock);
1513 } else
1514 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
1515
1516 err = -EFAULT;
1517 if (put_user(len, (int __user *) optlen))
1518 goto end_put_sess;
1519
1520 if (copy_to_user((void __user *) optval, &val, len))
1521 goto end_put_sess;
1522
1523 err = 0;
1524
1525end_put_sess:
1526 sock_put(sk);
1527end:
1528 return err;
1529}
1530
1531/*****************************************************************************
1532 * /proc filesystem for debug
James Chapmanf7faffa2010-04-02 06:18:49 +00001533 * Since the original pppol2tp driver provided /proc/net/pppol2tp for
1534 * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
James Chapmanfd558d12010-04-02 06:18:33 +00001535 *****************************************************************************/
1536
1537static unsigned int pppol2tp_net_id;
1538
1539#ifdef CONFIG_PROC_FS
1540
1541struct pppol2tp_seq_data {
1542 struct seq_net_private p;
1543 int tunnel_idx; /* current tunnel */
1544 int session_idx; /* index of session within current tunnel */
1545 struct l2tp_tunnel *tunnel;
1546 struct l2tp_session *session; /* NULL means get next tunnel */
1547};
1548
1549static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
1550{
James Chapmanf7faffa2010-04-02 06:18:49 +00001551 for (;;) {
1552 pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
1553 pd->tunnel_idx++;
1554
1555 if (pd->tunnel == NULL)
1556 break;
1557
1558 /* Ignore L2TPv3 tunnels */
1559 if (pd->tunnel->version < 3)
1560 break;
1561 }
James Chapmanfd558d12010-04-02 06:18:33 +00001562}
1563
1564static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
1565{
1566 pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1567 pd->session_idx++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001568
James Chapmanfd558d12010-04-02 06:18:33 +00001569 if (pd->session == NULL) {
1570 pd->session_idx = 0;
1571 pppol2tp_next_tunnel(net, pd);
1572 }
1573}
1574
1575static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
1576{
1577 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
1578 loff_t pos = *offs;
1579 struct net *net;
1580
1581 if (!pos)
1582 goto out;
1583
1584 BUG_ON(m->private == NULL);
1585 pd = m->private;
1586 net = seq_file_net(m);
1587
1588 if (pd->tunnel == NULL)
1589 pppol2tp_next_tunnel(net, pd);
1590 else
1591 pppol2tp_next_session(net, pd);
1592
1593 /* NULL tunnel and session indicates end of list */
1594 if ((pd->tunnel == NULL) && (pd->session == NULL))
1595 pd = NULL;
1596
1597out:
1598 return pd;
1599}
1600
1601static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
1602{
1603 (*pos)++;
1604 return NULL;
1605}
1606
1607static void pppol2tp_seq_stop(struct seq_file *p, void *v)
1608{
1609 /* nothing to do */
1610}
1611
1612static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
1613{
1614 struct l2tp_tunnel *tunnel = v;
1615
1616 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
1617 tunnel->name,
1618 (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1619 atomic_read(&tunnel->ref_count) - 1);
1620 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
1621 tunnel->debug,
1622 (unsigned long long)tunnel->stats.tx_packets,
1623 (unsigned long long)tunnel->stats.tx_bytes,
1624 (unsigned long long)tunnel->stats.tx_errors,
1625 (unsigned long long)tunnel->stats.rx_packets,
1626 (unsigned long long)tunnel->stats.rx_bytes,
1627 (unsigned long long)tunnel->stats.rx_errors);
1628}
1629
1630static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
1631{
1632 struct l2tp_session *session = v;
1633 struct l2tp_tunnel *tunnel = session->tunnel;
1634 struct pppol2tp_session *ps = l2tp_session_priv(session);
James Chapman93454712010-04-02 06:18:44 +00001635 struct pppox_sock *po = pppox_sk(ps->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001636 u32 ip = 0;
1637 u16 port = 0;
1638
1639 if (tunnel->sock) {
1640 struct inet_sock *inet = inet_sk(tunnel->sock);
1641 ip = ntohl(inet->inet_saddr);
1642 port = ntohs(inet->inet_sport);
1643 }
1644
1645 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
1646 "%04X/%04X %d %c\n",
1647 session->name, ip, port,
1648 tunnel->tunnel_id,
1649 session->session_id,
1650 tunnel->peer_tunnel_id,
1651 session->peer_session_id,
1652 ps->sock->sk_state,
1653 (session == ps->sock->sk_user_data) ?
1654 'Y' : 'N');
1655 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
1656 session->mtu, session->mru,
1657 session->recv_seq ? 'R' : '-',
1658 session->send_seq ? 'S' : '-',
1659 session->lns_mode ? "LNS" : "LAC",
1660 session->debug,
1661 jiffies_to_msecs(session->reorder_timeout));
1662 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
1663 session->nr, session->ns,
1664 (unsigned long long)session->stats.tx_packets,
1665 (unsigned long long)session->stats.tx_bytes,
1666 (unsigned long long)session->stats.tx_errors,
1667 (unsigned long long)session->stats.rx_packets,
1668 (unsigned long long)session->stats.rx_bytes,
1669 (unsigned long long)session->stats.rx_errors);
James Chapman93454712010-04-02 06:18:44 +00001670
1671 if (po)
1672 seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
James Chapmanfd558d12010-04-02 06:18:33 +00001673}
1674
1675static int pppol2tp_seq_show(struct seq_file *m, void *v)
1676{
1677 struct pppol2tp_seq_data *pd = v;
1678
1679 /* display header on line 1 */
1680 if (v == SEQ_START_TOKEN) {
1681 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
1682 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
1683 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1684 seq_puts(m, " SESSION name, addr/port src-tid/sid "
1685 "dest-tid/sid state user-data-ok\n");
1686 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
1687 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
1688 goto out;
1689 }
1690
1691 /* Show the tunnel or session context.
1692 */
1693 if (pd->session == NULL)
1694 pppol2tp_seq_tunnel_show(m, pd->tunnel);
1695 else
1696 pppol2tp_seq_session_show(m, pd->session);
1697
1698out:
1699 return 0;
1700}
1701
1702static const struct seq_operations pppol2tp_seq_ops = {
1703 .start = pppol2tp_seq_start,
1704 .next = pppol2tp_seq_next,
1705 .stop = pppol2tp_seq_stop,
1706 .show = pppol2tp_seq_show,
1707};
1708
1709/* Called when our /proc file is opened. We allocate data for use when
1710 * iterating our tunnel / session contexts and store it in the private
1711 * data of the seq_file.
1712 */
1713static int pppol2tp_proc_open(struct inode *inode, struct file *file)
1714{
1715 return seq_open_net(inode, file, &pppol2tp_seq_ops,
1716 sizeof(struct pppol2tp_seq_data));
1717}
1718
1719static const struct file_operations pppol2tp_proc_fops = {
1720 .owner = THIS_MODULE,
1721 .open = pppol2tp_proc_open,
1722 .read = seq_read,
1723 .llseek = seq_lseek,
1724 .release = seq_release_net,
1725};
1726
1727#endif /* CONFIG_PROC_FS */
1728
1729/*****************************************************************************
1730 * Network namespace
1731 *****************************************************************************/
1732
1733static __net_init int pppol2tp_init_net(struct net *net)
1734{
1735 struct proc_dir_entry *pde;
1736 int err = 0;
1737
1738 pde = proc_net_fops_create(net, "pppol2tp", S_IRUGO, &pppol2tp_proc_fops);
1739 if (!pde) {
1740 err = -ENOMEM;
1741 goto out;
1742 }
1743
1744out:
1745 return err;
1746}
1747
1748static __net_exit void pppol2tp_exit_net(struct net *net)
1749{
1750 proc_net_remove(net, "pppol2tp");
1751}
1752
1753static struct pernet_operations pppol2tp_net_ops = {
1754 .init = pppol2tp_init_net,
1755 .exit = pppol2tp_exit_net,
1756 .id = &pppol2tp_net_id,
1757};
1758
1759/*****************************************************************************
1760 * Init and cleanup
1761 *****************************************************************************/
1762
1763static const struct proto_ops pppol2tp_ops = {
1764 .family = AF_PPPOX,
1765 .owner = THIS_MODULE,
1766 .release = pppol2tp_release,
1767 .bind = sock_no_bind,
1768 .connect = pppol2tp_connect,
1769 .socketpair = sock_no_socketpair,
1770 .accept = sock_no_accept,
1771 .getname = pppol2tp_getname,
1772 .poll = datagram_poll,
1773 .listen = sock_no_listen,
1774 .shutdown = sock_no_shutdown,
1775 .setsockopt = pppol2tp_setsockopt,
1776 .getsockopt = pppol2tp_getsockopt,
1777 .sendmsg = pppol2tp_sendmsg,
1778 .recvmsg = pppol2tp_recvmsg,
1779 .mmap = sock_no_mmap,
1780 .ioctl = pppox_ioctl,
1781};
1782
Eric Dumazet756e64a2010-09-21 06:43:54 +00001783static const struct pppox_proto pppol2tp_proto = {
James Chapmanfd558d12010-04-02 06:18:33 +00001784 .create = pppol2tp_create,
Wei Yongjun589acc52013-07-02 09:02:07 +08001785 .ioctl = pppol2tp_ioctl,
1786 .owner = THIS_MODULE,
James Chapmanfd558d12010-04-02 06:18:33 +00001787};
1788
James Chapman309795f2010-04-02 06:19:10 +00001789#ifdef CONFIG_L2TP_V3
1790
1791static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
1792 .session_create = pppol2tp_session_create,
1793 .session_delete = pppol2tp_session_delete,
1794};
1795
1796#endif /* CONFIG_L2TP_V3 */
1797
James Chapmanfd558d12010-04-02 06:18:33 +00001798static int __init pppol2tp_init(void)
1799{
1800 int err;
1801
1802 err = register_pernet_device(&pppol2tp_net_ops);
1803 if (err)
1804 goto out;
1805
1806 err = proto_register(&pppol2tp_sk_proto, 0);
1807 if (err)
1808 goto out_unregister_pppol2tp_pernet;
1809
1810 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
1811 if (err)
1812 goto out_unregister_pppol2tp_proto;
1813
James Chapman309795f2010-04-02 06:19:10 +00001814#ifdef CONFIG_L2TP_V3
1815 err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
1816 if (err)
1817 goto out_unregister_pppox;
1818#endif
1819
James Chapmanfd558d12010-04-02 06:18:33 +00001820 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
1821 PPPOL2TP_DRV_VERSION);
1822
1823out:
1824 return err;
James Chapman309795f2010-04-02 06:19:10 +00001825
1826#ifdef CONFIG_L2TP_V3
1827out_unregister_pppox:
1828 unregister_pppox_proto(PX_PROTO_OL2TP);
1829#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001830out_unregister_pppol2tp_proto:
1831 proto_unregister(&pppol2tp_sk_proto);
1832out_unregister_pppol2tp_pernet:
1833 unregister_pernet_device(&pppol2tp_net_ops);
1834 goto out;
1835}
1836
1837static void __exit pppol2tp_exit(void)
1838{
James Chapman309795f2010-04-02 06:19:10 +00001839#ifdef CONFIG_L2TP_V3
1840 l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
1841#endif
James Chapmanfd558d12010-04-02 06:18:33 +00001842 unregister_pppox_proto(PX_PROTO_OL2TP);
1843 proto_unregister(&pppol2tp_sk_proto);
1844 unregister_pernet_device(&pppol2tp_net_ops);
1845}
1846
1847module_init(pppol2tp_init);
1848module_exit(pppol2tp_exit);
1849
1850MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1851MODULE_DESCRIPTION("PPP over L2TP over UDP");
1852MODULE_LICENSE("GPL");
1853MODULE_VERSION(PPPOL2TP_DRV_VERSION);
Benjamin LaHaise9395a092012-03-20 14:01:21 +00001854MODULE_ALIAS("pppox-proto-" __stringify(PX_PROTO_OL2TP));