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