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 60a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 61a4ca44faSJoe Perches 62fd558d18SJames Chapman #include <linux/module.h> 63fd558d18SJames Chapman #include <linux/string.h> 64fd558d18SJames Chapman #include <linux/list.h> 65fd558d18SJames Chapman #include <linux/uaccess.h> 66fd558d18SJames Chapman 67fd558d18SJames Chapman #include <linux/kernel.h> 68fd558d18SJames Chapman #include <linux/spinlock.h> 69fd558d18SJames Chapman #include <linux/kthread.h> 70fd558d18SJames Chapman #include <linux/sched.h> 71fd558d18SJames Chapman #include <linux/slab.h> 72fd558d18SJames Chapman #include <linux/errno.h> 73fd558d18SJames Chapman #include <linux/jiffies.h> 74fd558d18SJames Chapman 75fd558d18SJames Chapman #include <linux/netdevice.h> 76fd558d18SJames Chapman #include <linux/net.h> 77fd558d18SJames Chapman #include <linux/inetdevice.h> 78fd558d18SJames Chapman #include <linux/skbuff.h> 79fd558d18SJames Chapman #include <linux/init.h> 80fd558d18SJames Chapman #include <linux/ip.h> 81fd558d18SJames Chapman #include <linux/udp.h> 82fd558d18SJames Chapman #include <linux/if_pppox.h> 83fd558d18SJames Chapman #include <linux/if_pppol2tp.h> 84fd558d18SJames Chapman #include <net/sock.h> 85fd558d18SJames Chapman #include <linux/ppp_channel.h> 86fd558d18SJames Chapman #include <linux/ppp_defs.h> 874b32da2bSPaul Mackerras #include <linux/ppp-ioctl.h> 88fd558d18SJames Chapman #include <linux/file.h> 89fd558d18SJames Chapman #include <linux/hash.h> 90fd558d18SJames Chapman #include <linux/sort.h> 91fd558d18SJames Chapman #include <linux/proc_fs.h> 92309795f4SJames Chapman #include <linux/l2tp.h> 93fd558d18SJames Chapman #include <linux/nsproxy.h> 94fd558d18SJames Chapman #include <net/net_namespace.h> 95fd558d18SJames Chapman #include <net/netns/generic.h> 96fd558d18SJames Chapman #include <net/dst.h> 97fd558d18SJames Chapman #include <net/ip.h> 98fd558d18SJames Chapman #include <net/udp.h> 99fd558d18SJames Chapman #include <net/xfrm.h> 100*cf2f5c88STom Parkin #include <net/inet_common.h> 101fd558d18SJames Chapman 102fd558d18SJames Chapman #include <asm/byteorder.h> 10360063497SArun Sharma #include <linux/atomic.h> 104fd558d18SJames Chapman 105fd558d18SJames Chapman #include "l2tp_core.h" 106fd558d18SJames Chapman 107fd558d18SJames Chapman #define PPPOL2TP_DRV_VERSION "V2.0" 108fd558d18SJames Chapman 109fd558d18SJames Chapman /* Space for UDP, L2TP and PPP headers */ 110fd558d18SJames Chapman #define PPPOL2TP_HEADER_OVERHEAD 40 111fd558d18SJames Chapman 112fd558d18SJames Chapman /* Number of bytes to build transmit L2TP headers. 113fd558d18SJames Chapman * Unfortunately the size is different depending on whether sequence numbers 114fd558d18SJames Chapman * are enabled. 115fd558d18SJames Chapman */ 116fd558d18SJames Chapman #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10 117fd558d18SJames Chapman #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6 118fd558d18SJames Chapman 119fd558d18SJames Chapman /* Private data of each session. This data lives at the end of struct 120fd558d18SJames Chapman * l2tp_session, referenced via session->priv[]. 121fd558d18SJames Chapman */ 122fd558d18SJames Chapman struct pppol2tp_session { 123fd558d18SJames Chapman int owner; /* pid that opened the socket */ 124fd558d18SJames Chapman 125fd558d18SJames Chapman struct sock *sock; /* Pointer to the session 126fd558d18SJames Chapman * PPPoX socket */ 127fd558d18SJames Chapman struct sock *tunnel_sock; /* Pointer to the tunnel UDP 128fd558d18SJames Chapman * socket */ 129fd558d18SJames Chapman int flags; /* accessed by PPPIOCGFLAGS. 130fd558d18SJames Chapman * Unused. */ 131fd558d18SJames Chapman }; 132fd558d18SJames Chapman 133fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb); 134fd558d18SJames Chapman 135d7100da0Sstephen hemminger static const struct ppp_channel_ops pppol2tp_chan_ops = { 136d7100da0Sstephen hemminger .start_xmit = pppol2tp_xmit, 137d7100da0Sstephen hemminger }; 138d7100da0Sstephen hemminger 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; 236a4ca44faSJoe Perches l2tp_dbg(session, PPPOL2TP_MSG_DATA, 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 { 259a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_DATA, "%s: socket not bound\n", 260a4ca44faSJoe Perches 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: 270a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_DATA, "%s: no socket\n", session->name); 271fd558d18SJames Chapman kfree_skb(skb); 272fd558d18SJames Chapman } 273fd558d18SJames Chapman 274fd558d18SJames Chapman static void pppol2tp_session_sock_hold(struct l2tp_session *session) 275fd558d18SJames Chapman { 276fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 277fd558d18SJames Chapman 278fd558d18SJames Chapman if (ps->sock) 279fd558d18SJames Chapman sock_hold(ps->sock); 280fd558d18SJames Chapman } 281fd558d18SJames Chapman 282fd558d18SJames Chapman static void pppol2tp_session_sock_put(struct l2tp_session *session) 283fd558d18SJames Chapman { 284fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 285fd558d18SJames Chapman 286fd558d18SJames Chapman if (ps->sock) 287fd558d18SJames Chapman sock_put(ps->sock); 288fd558d18SJames Chapman } 289fd558d18SJames Chapman 290fd558d18SJames Chapman /************************************************************************ 291fd558d18SJames Chapman * Transmit handling 292fd558d18SJames Chapman ***********************************************************************/ 293fd558d18SJames Chapman 294fd558d18SJames Chapman /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here 295fd558d18SJames Chapman * when a user application does a sendmsg() on the session socket. L2TP and 296fd558d18SJames Chapman * PPP headers must be inserted into the user's data. 297fd558d18SJames Chapman */ 298fd558d18SJames Chapman static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, 299fd558d18SJames Chapman size_t total_len) 300fd558d18SJames Chapman { 301fd558d18SJames Chapman static const unsigned char ppph[2] = { 0xff, 0x03 }; 302fd558d18SJames Chapman struct sock *sk = sock->sk; 303fd558d18SJames Chapman struct sk_buff *skb; 304fd558d18SJames Chapman int error; 305fd558d18SJames Chapman struct l2tp_session *session; 306fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 307fd558d18SJames Chapman struct pppol2tp_session *ps; 3080d76751fSJames Chapman int uhlen; 309fd558d18SJames Chapman 310fd558d18SJames Chapman error = -ENOTCONN; 311fd558d18SJames Chapman if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) 312fd558d18SJames Chapman goto error; 313fd558d18SJames Chapman 314fd558d18SJames Chapman /* Get session and tunnel contexts */ 315fd558d18SJames Chapman error = -EBADF; 316fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 317fd558d18SJames Chapman if (session == NULL) 318fd558d18SJames Chapman goto error; 319fd558d18SJames Chapman 320fd558d18SJames Chapman ps = l2tp_session_priv(session); 321fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock); 322fd558d18SJames Chapman if (tunnel == NULL) 323fd558d18SJames Chapman goto error_put_sess; 324fd558d18SJames Chapman 3250d76751fSJames Chapman uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 3260d76751fSJames Chapman 327fd558d18SJames Chapman /* Allocate a socket buffer */ 328fd558d18SJames Chapman error = -ENOMEM; 329fd558d18SJames Chapman skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) + 3300d76751fSJames Chapman uhlen + session->hdr_len + 331fd558d18SJames Chapman sizeof(ppph) + total_len, 332fd558d18SJames Chapman 0, GFP_KERNEL); 333fd558d18SJames Chapman if (!skb) 334fd558d18SJames Chapman goto error_put_sess_tun; 335fd558d18SJames Chapman 336fd558d18SJames Chapman /* Reserve space for headers. */ 337fd558d18SJames Chapman skb_reserve(skb, NET_SKB_PAD); 338fd558d18SJames Chapman skb_reset_network_header(skb); 339fd558d18SJames Chapman skb_reserve(skb, sizeof(struct iphdr)); 340fd558d18SJames Chapman skb_reset_transport_header(skb); 3410d76751fSJames Chapman skb_reserve(skb, uhlen); 342fd558d18SJames Chapman 343fd558d18SJames Chapman /* Add PPP header */ 344fd558d18SJames Chapman skb->data[0] = ppph[0]; 345fd558d18SJames Chapman skb->data[1] = ppph[1]; 346fd558d18SJames Chapman skb_put(skb, 2); 347fd558d18SJames Chapman 348fd558d18SJames Chapman /* Copy user data into skb */ 349fd558d18SJames Chapman error = memcpy_fromiovec(skb->data, m->msg_iov, total_len); 350fd558d18SJames Chapman if (error < 0) { 351fd558d18SJames Chapman kfree_skb(skb); 352fd558d18SJames Chapman goto error_put_sess_tun; 353fd558d18SJames Chapman } 354fd558d18SJames Chapman skb_put(skb, total_len); 355fd558d18SJames Chapman 356fd558d18SJames Chapman l2tp_xmit_skb(session, skb, session->hdr_len); 357fd558d18SJames Chapman 358fd558d18SJames Chapman sock_put(ps->tunnel_sock); 3598b82547eSGuillaume Nault sock_put(sk); 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; 39309df57caSEric Dumazet int uhlen, headroom; 394fd558d18SJames Chapman 395fd558d18SJames Chapman if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) 396fd558d18SJames Chapman goto abort; 397fd558d18SJames Chapman 398fd558d18SJames Chapman /* Get session and tunnel contexts from the socket */ 399fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 400fd558d18SJames Chapman if (session == NULL) 401fd558d18SJames Chapman goto abort; 402fd558d18SJames Chapman 403fd558d18SJames Chapman ps = l2tp_session_priv(session); 404fd558d18SJames Chapman sk_tun = ps->tunnel_sock; 405fd558d18SJames Chapman if (sk_tun == NULL) 406fd558d18SJames Chapman goto abort_put_sess; 407fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(sk_tun); 408fd558d18SJames Chapman if (tunnel == NULL) 409fd558d18SJames Chapman goto abort_put_sess; 410fd558d18SJames Chapman 41109df57caSEric Dumazet uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 41209df57caSEric Dumazet headroom = NET_SKB_PAD + 41309df57caSEric Dumazet sizeof(struct iphdr) + /* IP header */ 41409df57caSEric Dumazet uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */ 41509df57caSEric Dumazet session->hdr_len + /* L2TP header */ 41609df57caSEric Dumazet sizeof(ppph); /* PPP header */ 41709df57caSEric Dumazet if (skb_cow_head(skb, headroom)) 418fd558d18SJames Chapman goto abort_put_sess_tun; 419fd558d18SJames Chapman 420fd558d18SJames Chapman /* Setup PPP header */ 421fd558d18SJames Chapman __skb_push(skb, sizeof(ppph)); 422fd558d18SJames Chapman skb->data[0] = ppph[0]; 423fd558d18SJames Chapman skb->data[1] = ppph[1]; 424fd558d18SJames Chapman 425e0d4435fSJames Chapman l2tp_xmit_skb(session, skb, session->hdr_len); 426fd558d18SJames Chapman 427fd558d18SJames Chapman sock_put(sk_tun); 428fd558d18SJames Chapman sock_put(sk); 429fd558d18SJames Chapman return 1; 430fd558d18SJames Chapman 431fd558d18SJames Chapman abort_put_sess_tun: 432fd558d18SJames Chapman sock_put(sk_tun); 433fd558d18SJames Chapman abort_put_sess: 434fd558d18SJames Chapman sock_put(sk); 435fd558d18SJames Chapman abort: 436fd558d18SJames Chapman /* Free the original skb */ 437fd558d18SJames Chapman kfree_skb(skb); 438fd558d18SJames Chapman return 1; 439fd558d18SJames Chapman } 440fd558d18SJames Chapman 441fd558d18SJames Chapman /***************************************************************************** 442fd558d18SJames Chapman * Session (and tunnel control) socket create/destroy. 443fd558d18SJames Chapman *****************************************************************************/ 444fd558d18SJames Chapman 445fd558d18SJames Chapman /* Called by l2tp_core when a session socket is being closed. 446fd558d18SJames Chapman */ 447fd558d18SJames Chapman static void pppol2tp_session_close(struct l2tp_session *session) 448fd558d18SJames Chapman { 449fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 450fd558d18SJames Chapman struct sock *sk = ps->sock; 451*cf2f5c88STom Parkin struct socket *sock = sk->sk_socket; 452fd558d18SJames Chapman 453fd558d18SJames Chapman BUG_ON(session->magic != L2TP_SESSION_MAGIC); 454fd558d18SJames Chapman 455fd558d18SJames Chapman 456*cf2f5c88STom Parkin if (sock) { 457*cf2f5c88STom Parkin inet_shutdown(sock, 2); 458*cf2f5c88STom Parkin /* Don't let the session go away before our socket does */ 459*cf2f5c88STom Parkin l2tp_session_inc_refcount(session); 460fd558d18SJames Chapman } 461fd558d18SJames Chapman return; 462fd558d18SJames Chapman } 463fd558d18SJames Chapman 464fd558d18SJames Chapman /* Really kill the session socket. (Called from sock_put() if 465fd558d18SJames Chapman * refcnt == 0.) 466fd558d18SJames Chapman */ 467fd558d18SJames Chapman static void pppol2tp_session_destruct(struct sock *sk) 468fd558d18SJames Chapman { 469fd558d18SJames Chapman struct l2tp_session *session; 470fd558d18SJames Chapman 471fd558d18SJames Chapman if (sk->sk_user_data != NULL) { 472fd558d18SJames Chapman session = sk->sk_user_data; 473fd558d18SJames Chapman if (session == NULL) 474fd558d18SJames Chapman goto out; 475fd558d18SJames Chapman 476fd558d18SJames Chapman sk->sk_user_data = NULL; 477fd558d18SJames Chapman BUG_ON(session->magic != L2TP_SESSION_MAGIC); 478fd558d18SJames Chapman l2tp_session_dec_refcount(session); 479fd558d18SJames Chapman } 480fd558d18SJames Chapman 481fd558d18SJames Chapman out: 482fd558d18SJames Chapman return; 483fd558d18SJames Chapman } 484fd558d18SJames Chapman 485fd558d18SJames Chapman /* Called when the PPPoX socket (session) is closed. 486fd558d18SJames Chapman */ 487fd558d18SJames Chapman static int pppol2tp_release(struct socket *sock) 488fd558d18SJames Chapman { 489fd558d18SJames Chapman struct sock *sk = sock->sk; 490fd558d18SJames Chapman struct l2tp_session *session; 491fd558d18SJames Chapman int error; 492fd558d18SJames Chapman 493fd558d18SJames Chapman if (!sk) 494fd558d18SJames Chapman return 0; 495fd558d18SJames Chapman 496fd558d18SJames Chapman error = -EBADF; 497fd558d18SJames Chapman lock_sock(sk); 498fd558d18SJames Chapman if (sock_flag(sk, SOCK_DEAD) != 0) 499fd558d18SJames Chapman goto error; 500fd558d18SJames Chapman 501fd558d18SJames Chapman pppox_unbind_sock(sk); 502fd558d18SJames Chapman 503fd558d18SJames Chapman /* Signal the death of the socket. */ 504fd558d18SJames Chapman sk->sk_state = PPPOX_DEAD; 505fd558d18SJames Chapman sock_orphan(sk); 506fd558d18SJames Chapman sock->sk = NULL; 507fd558d18SJames Chapman 508fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 509fd558d18SJames Chapman 510fd558d18SJames Chapman /* Purge any queued data */ 511*cf2f5c88STom Parkin if (session != NULL) { 512*cf2f5c88STom Parkin l2tp_session_queue_purge(session); 513*cf2f5c88STom Parkin sock_put(sk); 514*cf2f5c88STom Parkin } 515fd558d18SJames Chapman skb_queue_purge(&sk->sk_receive_queue); 516fd558d18SJames Chapman skb_queue_purge(&sk->sk_write_queue); 517fd558d18SJames Chapman 518fd558d18SJames Chapman release_sock(sk); 519fd558d18SJames Chapman 520fd558d18SJames Chapman /* This will delete the session context via 521fd558d18SJames Chapman * pppol2tp_session_destruct() if the socket's refcnt drops to 522fd558d18SJames Chapman * zero. 523fd558d18SJames Chapman */ 524fd558d18SJames Chapman sock_put(sk); 525fd558d18SJames Chapman 526fd558d18SJames Chapman return 0; 527fd558d18SJames Chapman 528fd558d18SJames Chapman error: 529fd558d18SJames Chapman release_sock(sk); 530fd558d18SJames Chapman return error; 531fd558d18SJames Chapman } 532fd558d18SJames Chapman 533fd558d18SJames Chapman static struct proto pppol2tp_sk_proto = { 534fd558d18SJames Chapman .name = "PPPOL2TP", 535fd558d18SJames Chapman .owner = THIS_MODULE, 536fd558d18SJames Chapman .obj_size = sizeof(struct pppox_sock), 537fd558d18SJames Chapman }; 538fd558d18SJames Chapman 539fd558d18SJames Chapman static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb) 540fd558d18SJames Chapman { 541fd558d18SJames Chapman int rc; 542fd558d18SJames Chapman 543fd558d18SJames Chapman rc = l2tp_udp_encap_recv(sk, skb); 544fd558d18SJames Chapman if (rc) 545fd558d18SJames Chapman kfree_skb(skb); 546fd558d18SJames Chapman 547fd558d18SJames Chapman return NET_RX_SUCCESS; 548fd558d18SJames Chapman } 549fd558d18SJames Chapman 550fd558d18SJames Chapman /* socket() handler. Initialize a new struct sock. 551fd558d18SJames Chapman */ 552fd558d18SJames Chapman static int pppol2tp_create(struct net *net, struct socket *sock) 553fd558d18SJames Chapman { 554fd558d18SJames Chapman int error = -ENOMEM; 555fd558d18SJames Chapman struct sock *sk; 556fd558d18SJames Chapman 557fd558d18SJames Chapman sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto); 558fd558d18SJames Chapman if (!sk) 559fd558d18SJames Chapman goto out; 560fd558d18SJames Chapman 561fd558d18SJames Chapman sock_init_data(sock, sk); 562fd558d18SJames Chapman 563fd558d18SJames Chapman sock->state = SS_UNCONNECTED; 564fd558d18SJames Chapman sock->ops = &pppol2tp_ops; 565fd558d18SJames Chapman 566fd558d18SJames Chapman sk->sk_backlog_rcv = pppol2tp_backlog_recv; 567fd558d18SJames Chapman sk->sk_protocol = PX_PROTO_OL2TP; 568fd558d18SJames Chapman sk->sk_family = PF_PPPOX; 569fd558d18SJames Chapman sk->sk_state = PPPOX_NONE; 570fd558d18SJames Chapman sk->sk_type = SOCK_STREAM; 571fd558d18SJames Chapman sk->sk_destruct = pppol2tp_session_destruct; 572fd558d18SJames Chapman 573fd558d18SJames Chapman error = 0; 574fd558d18SJames Chapman 575fd558d18SJames Chapman out: 576fd558d18SJames Chapman return error; 577fd558d18SJames Chapman } 578fd558d18SJames Chapman 579f66ef2d0SDavid S. Miller #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) 5800ad66140SJames Chapman static void pppol2tp_show(struct seq_file *m, void *arg) 5810ad66140SJames Chapman { 5820ad66140SJames Chapman struct l2tp_session *session = arg; 5830ad66140SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 5840ad66140SJames Chapman 5850ad66140SJames Chapman if (ps) { 5860ad66140SJames Chapman struct pppox_sock *po = pppox_sk(ps->sock); 5870ad66140SJames Chapman if (po) 5880ad66140SJames Chapman seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan)); 5890ad66140SJames Chapman } 5900ad66140SJames Chapman } 5910ad66140SJames Chapman #endif 5920ad66140SJames Chapman 593fd558d18SJames Chapman /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket 594fd558d18SJames Chapman */ 595fd558d18SJames Chapman static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, 596fd558d18SJames Chapman int sockaddr_len, int flags) 597fd558d18SJames Chapman { 598fd558d18SJames Chapman struct sock *sk = sock->sk; 599fd558d18SJames Chapman struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr; 600fd558d18SJames Chapman struct pppox_sock *po = pppox_sk(sk); 601fd558d18SJames Chapman struct l2tp_session *session = NULL; 602fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 603fd558d18SJames Chapman struct pppol2tp_session *ps; 604fd558d18SJames Chapman struct dst_entry *dst; 605fd558d18SJames Chapman struct l2tp_session_cfg cfg = { 0, }; 606fd558d18SJames Chapman int error = 0; 607e0d4435fSJames Chapman u32 tunnel_id, peer_tunnel_id; 608e0d4435fSJames Chapman u32 session_id, peer_session_id; 609e0d4435fSJames Chapman int ver = 2; 610e0d4435fSJames Chapman int fd; 611fd558d18SJames Chapman 612fd558d18SJames Chapman lock_sock(sk); 613fd558d18SJames Chapman 614fd558d18SJames Chapman error = -EINVAL; 615fd558d18SJames Chapman if (sp->sa_protocol != PX_PROTO_OL2TP) 616fd558d18SJames Chapman goto end; 617fd558d18SJames Chapman 618fd558d18SJames Chapman /* Check for already bound sockets */ 619fd558d18SJames Chapman error = -EBUSY; 620fd558d18SJames Chapman if (sk->sk_state & PPPOX_CONNECTED) 621fd558d18SJames Chapman goto end; 622fd558d18SJames Chapman 623fd558d18SJames Chapman /* We don't supporting rebinding anyway */ 624fd558d18SJames Chapman error = -EALREADY; 625fd558d18SJames Chapman if (sk->sk_user_data) 626fd558d18SJames Chapman goto end; /* socket is already attached */ 627fd558d18SJames Chapman 628b79585f5SJames Chapman /* Get params from socket address. Handle L2TPv2 and L2TPv3. 629b79585f5SJames Chapman * This is nasty because there are different sockaddr_pppol2tp 630b79585f5SJames Chapman * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use 631b79585f5SJames Chapman * the sockaddr size to determine which structure the caller 632b79585f5SJames Chapman * is using. 633b79585f5SJames Chapman */ 634b79585f5SJames Chapman peer_tunnel_id = 0; 635e0d4435fSJames Chapman if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) { 636e0d4435fSJames Chapman fd = sp->pppol2tp.fd; 637e0d4435fSJames Chapman tunnel_id = sp->pppol2tp.s_tunnel; 638e0d4435fSJames Chapman peer_tunnel_id = sp->pppol2tp.d_tunnel; 639e0d4435fSJames Chapman session_id = sp->pppol2tp.s_session; 640e0d4435fSJames Chapman peer_session_id = sp->pppol2tp.d_session; 641e0d4435fSJames Chapman } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) { 642b79585f5SJames Chapman struct sockaddr_pppol2tpv3 *sp3 = 643b79585f5SJames Chapman (struct sockaddr_pppol2tpv3 *) sp; 644e0d4435fSJames Chapman ver = 3; 645e0d4435fSJames Chapman fd = sp3->pppol2tp.fd; 646e0d4435fSJames Chapman tunnel_id = sp3->pppol2tp.s_tunnel; 647e0d4435fSJames Chapman peer_tunnel_id = sp3->pppol2tp.d_tunnel; 648e0d4435fSJames Chapman session_id = sp3->pppol2tp.s_session; 649e0d4435fSJames Chapman peer_session_id = sp3->pppol2tp.d_session; 650b79585f5SJames Chapman } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) { 651b79585f5SJames Chapman struct sockaddr_pppol2tpin6 *sp6 = 652b79585f5SJames Chapman (struct sockaddr_pppol2tpin6 *) sp; 653b79585f5SJames Chapman fd = sp6->pppol2tp.fd; 654b79585f5SJames Chapman tunnel_id = sp6->pppol2tp.s_tunnel; 655b79585f5SJames Chapman peer_tunnel_id = sp6->pppol2tp.d_tunnel; 656b79585f5SJames Chapman session_id = sp6->pppol2tp.s_session; 657b79585f5SJames Chapman peer_session_id = sp6->pppol2tp.d_session; 658b79585f5SJames Chapman } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) { 659b79585f5SJames Chapman struct sockaddr_pppol2tpv3in6 *sp6 = 660b79585f5SJames Chapman (struct sockaddr_pppol2tpv3in6 *) sp; 661b79585f5SJames Chapman ver = 3; 662b79585f5SJames Chapman fd = sp6->pppol2tp.fd; 663b79585f5SJames Chapman tunnel_id = sp6->pppol2tp.s_tunnel; 664b79585f5SJames Chapman peer_tunnel_id = sp6->pppol2tp.d_tunnel; 665b79585f5SJames Chapman session_id = sp6->pppol2tp.s_session; 666b79585f5SJames Chapman peer_session_id = sp6->pppol2tp.d_session; 667e0d4435fSJames Chapman } else { 668fd558d18SJames Chapman error = -EINVAL; 669e0d4435fSJames Chapman goto end; /* bad socket address */ 670e0d4435fSJames Chapman } 671e0d4435fSJames Chapman 672e0d4435fSJames Chapman /* Don't bind if tunnel_id is 0 */ 673e0d4435fSJames Chapman error = -EINVAL; 674e0d4435fSJames Chapman if (tunnel_id == 0) 675fd558d18SJames Chapman goto end; 676fd558d18SJames Chapman 677309795f4SJames Chapman tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id); 678309795f4SJames Chapman 679e0d4435fSJames Chapman /* Special case: create tunnel context if session_id and 680e0d4435fSJames Chapman * peer_session_id is 0. Otherwise look up tunnel using supplied 681fd558d18SJames Chapman * tunnel id. 682fd558d18SJames Chapman */ 683e0d4435fSJames Chapman if ((session_id == 0) && (peer_session_id == 0)) { 684309795f4SJames Chapman if (tunnel == NULL) { 685309795f4SJames Chapman struct l2tp_tunnel_cfg tcfg = { 686309795f4SJames Chapman .encap = L2TP_ENCAPTYPE_UDP, 687309795f4SJames Chapman .debug = 0, 688309795f4SJames Chapman }; 689309795f4SJames Chapman error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel); 690fd558d18SJames Chapman if (error < 0) 691fd558d18SJames Chapman goto end; 692309795f4SJames Chapman } 693fd558d18SJames Chapman } else { 694fd558d18SJames Chapman /* Error if we can't find the tunnel */ 695fd558d18SJames Chapman error = -ENOENT; 696fd558d18SJames Chapman if (tunnel == NULL) 697fd558d18SJames Chapman goto end; 698fd558d18SJames Chapman 699fd558d18SJames Chapman /* Error if socket is not prepped */ 700fd558d18SJames Chapman if (tunnel->sock == NULL) 701fd558d18SJames Chapman goto end; 702fd558d18SJames Chapman } 703fd558d18SJames Chapman 704fd558d18SJames Chapman if (tunnel->recv_payload_hook == NULL) 705fd558d18SJames Chapman tunnel->recv_payload_hook = pppol2tp_recv_payload_hook; 706fd558d18SJames Chapman 707b79585f5SJames Chapman if (tunnel->peer_tunnel_id == 0) 708b79585f5SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 709fd558d18SJames Chapman 710309795f4SJames Chapman /* Create session if it doesn't already exist. We handle the 711309795f4SJames Chapman * case where a session was previously created by the netlink 712309795f4SJames Chapman * interface by checking that the session doesn't already have 713309795f4SJames Chapman * a socket and its tunnel socket are what we expect. If any 714309795f4SJames Chapman * of those checks fail, return EEXIST to the caller. 715309795f4SJames Chapman */ 716309795f4SJames Chapman session = l2tp_session_find(sock_net(sk), tunnel, session_id); 717309795f4SJames Chapman if (session == NULL) { 718309795f4SJames Chapman /* Default MTU must allow space for UDP/L2TP/PPP 719309795f4SJames Chapman * headers. 720309795f4SJames Chapman */ 721309795f4SJames Chapman cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD; 722fd558d18SJames Chapman 723fd558d18SJames Chapman /* Allocate and initialize a new session context. */ 724fd558d18SJames Chapman session = l2tp_session_create(sizeof(struct pppol2tp_session), 725e0d4435fSJames Chapman tunnel, session_id, 726e0d4435fSJames Chapman peer_session_id, &cfg); 727fd558d18SJames Chapman if (session == NULL) { 728fd558d18SJames Chapman error = -ENOMEM; 729fd558d18SJames Chapman goto end; 730fd558d18SJames Chapman } 731309795f4SJames Chapman } else { 732309795f4SJames Chapman ps = l2tp_session_priv(session); 733309795f4SJames Chapman error = -EEXIST; 734309795f4SJames Chapman if (ps->sock != NULL) 735309795f4SJames Chapman goto end; 736fd558d18SJames Chapman 737309795f4SJames Chapman /* consistency checks */ 738309795f4SJames Chapman if (ps->tunnel_sock != tunnel->sock) 739309795f4SJames Chapman goto end; 740309795f4SJames Chapman } 741309795f4SJames Chapman 742309795f4SJames Chapman /* Associate session with its PPPoL2TP socket */ 743fd558d18SJames Chapman ps = l2tp_session_priv(session); 744fd558d18SJames Chapman ps->owner = current->pid; 745fd558d18SJames Chapman ps->sock = sk; 746fd558d18SJames Chapman ps->tunnel_sock = tunnel->sock; 747fd558d18SJames Chapman 748fd558d18SJames Chapman session->recv_skb = pppol2tp_recv; 749fd558d18SJames Chapman session->session_close = pppol2tp_session_close; 750f66ef2d0SDavid S. Miller #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) 7510ad66140SJames Chapman session->show = pppol2tp_show; 7520ad66140SJames Chapman #endif 753fd558d18SJames Chapman 754fd558d18SJames Chapman /* We need to know each time a skb is dropped from the reorder 755fd558d18SJames Chapman * queue. 756fd558d18SJames Chapman */ 757fd558d18SJames Chapman session->ref = pppol2tp_session_sock_hold; 758fd558d18SJames Chapman session->deref = pppol2tp_session_sock_put; 759fd558d18SJames Chapman 760fd558d18SJames Chapman /* If PMTU discovery was enabled, use the MTU that was discovered */ 761fd558d18SJames Chapman dst = sk_dst_get(sk); 762fd558d18SJames Chapman if (dst != NULL) { 763fd558d18SJames Chapman u32 pmtu = dst_mtu(__sk_dst_get(sk)); 764fd558d18SJames Chapman if (pmtu != 0) 765fd558d18SJames Chapman session->mtu = session->mru = pmtu - 766fd558d18SJames Chapman PPPOL2TP_HEADER_OVERHEAD; 767fd558d18SJames Chapman dst_release(dst); 768fd558d18SJames Chapman } 769fd558d18SJames Chapman 770fd558d18SJames Chapman /* Special case: if source & dest session_id == 0x0000, this 771fd558d18SJames Chapman * socket is being created to manage the tunnel. Just set up 772fd558d18SJames Chapman * the internal context for use by ioctl() and sockopt() 773fd558d18SJames Chapman * handlers. 774fd558d18SJames Chapman */ 775fd558d18SJames Chapman if ((session->session_id == 0) && 776fd558d18SJames Chapman (session->peer_session_id == 0)) { 777fd558d18SJames Chapman error = 0; 778fd558d18SJames Chapman goto out_no_ppp; 779fd558d18SJames Chapman } 780fd558d18SJames Chapman 781fd558d18SJames Chapman /* The only header we need to worry about is the L2TP 782fd558d18SJames Chapman * header. This size is different depending on whether 783fd558d18SJames Chapman * sequence numbers are enabled for the data channel. 784fd558d18SJames Chapman */ 785fd558d18SJames Chapman po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; 786fd558d18SJames Chapman 787fd558d18SJames Chapman po->chan.private = sk; 788fd558d18SJames Chapman po->chan.ops = &pppol2tp_chan_ops; 789fd558d18SJames Chapman po->chan.mtu = session->mtu; 790fd558d18SJames Chapman 791fd558d18SJames Chapman error = ppp_register_net_channel(sock_net(sk), &po->chan); 792fd558d18SJames Chapman if (error) 793fd558d18SJames Chapman goto end; 794fd558d18SJames Chapman 795fd558d18SJames Chapman out_no_ppp: 796fd558d18SJames Chapman /* This is how we get the session context from the socket. */ 797fd558d18SJames Chapman sk->sk_user_data = session; 798fd558d18SJames Chapman sk->sk_state = PPPOX_CONNECTED; 799a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n", 800a4ca44faSJoe Perches session->name); 801fd558d18SJames Chapman 802fd558d18SJames Chapman end: 803fd558d18SJames Chapman release_sock(sk); 804fd558d18SJames Chapman 805fd558d18SJames Chapman return error; 806fd558d18SJames Chapman } 807fd558d18SJames Chapman 808309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 809309795f4SJames Chapman 810309795f4SJames Chapman /* Called when creating sessions via the netlink interface. 811309795f4SJames Chapman */ 812309795f4SJames Chapman static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) 813309795f4SJames Chapman { 814309795f4SJames Chapman int error; 815309795f4SJames Chapman struct l2tp_tunnel *tunnel; 816309795f4SJames Chapman struct l2tp_session *session; 817309795f4SJames Chapman struct pppol2tp_session *ps; 818309795f4SJames Chapman 819309795f4SJames Chapman tunnel = l2tp_tunnel_find(net, tunnel_id); 820309795f4SJames Chapman 821309795f4SJames Chapman /* Error if we can't find the tunnel */ 822309795f4SJames Chapman error = -ENOENT; 823309795f4SJames Chapman if (tunnel == NULL) 824309795f4SJames Chapman goto out; 825309795f4SJames Chapman 826309795f4SJames Chapman /* Error if tunnel socket is not prepped */ 827309795f4SJames Chapman if (tunnel->sock == NULL) 828309795f4SJames Chapman goto out; 829309795f4SJames Chapman 830309795f4SJames Chapman /* Check that this session doesn't already exist */ 831309795f4SJames Chapman error = -EEXIST; 832309795f4SJames Chapman session = l2tp_session_find(net, tunnel, session_id); 833309795f4SJames Chapman if (session != NULL) 834309795f4SJames Chapman goto out; 835309795f4SJames Chapman 836309795f4SJames Chapman /* Default MTU values. */ 837309795f4SJames Chapman if (cfg->mtu == 0) 838309795f4SJames Chapman cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD; 839309795f4SJames Chapman if (cfg->mru == 0) 840309795f4SJames Chapman cfg->mru = cfg->mtu; 841309795f4SJames Chapman 842309795f4SJames Chapman /* Allocate and initialize a new session context. */ 843309795f4SJames Chapman error = -ENOMEM; 844309795f4SJames Chapman session = l2tp_session_create(sizeof(struct pppol2tp_session), 845309795f4SJames Chapman tunnel, session_id, 846309795f4SJames Chapman peer_session_id, cfg); 847309795f4SJames Chapman if (session == NULL) 848309795f4SJames Chapman goto out; 849309795f4SJames Chapman 850309795f4SJames Chapman ps = l2tp_session_priv(session); 851309795f4SJames Chapman ps->tunnel_sock = tunnel->sock; 852309795f4SJames Chapman 853a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n", 854a4ca44faSJoe Perches session->name); 855309795f4SJames Chapman 856309795f4SJames Chapman error = 0; 857309795f4SJames Chapman 858309795f4SJames Chapman out: 859309795f4SJames Chapman return error; 860309795f4SJames Chapman } 861309795f4SJames Chapman 862309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */ 863309795f4SJames Chapman 864fd558d18SJames Chapman /* getname() support. 865fd558d18SJames Chapman */ 866fd558d18SJames Chapman static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr, 867fd558d18SJames Chapman int *usockaddr_len, int peer) 868fd558d18SJames Chapman { 869e0d4435fSJames Chapman int len = 0; 870fd558d18SJames Chapman int error = 0; 871fd558d18SJames Chapman struct l2tp_session *session; 872fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 873fd558d18SJames Chapman struct sock *sk = sock->sk; 874fd558d18SJames Chapman struct inet_sock *inet; 875fd558d18SJames Chapman struct pppol2tp_session *pls; 876fd558d18SJames Chapman 877fd558d18SJames Chapman error = -ENOTCONN; 878fd558d18SJames Chapman if (sk == NULL) 879fd558d18SJames Chapman goto end; 880fd558d18SJames Chapman if (sk->sk_state != PPPOX_CONNECTED) 881fd558d18SJames Chapman goto end; 882fd558d18SJames Chapman 883fd558d18SJames Chapman error = -EBADF; 884fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 885fd558d18SJames Chapman if (session == NULL) 886fd558d18SJames Chapman goto end; 887fd558d18SJames Chapman 888fd558d18SJames Chapman pls = l2tp_session_priv(session); 889fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock); 890fd558d18SJames Chapman if (tunnel == NULL) { 891fd558d18SJames Chapman error = -EBADF; 892fd558d18SJames Chapman goto end_put_sess; 893fd558d18SJames Chapman } 894fd558d18SJames Chapman 895bbdb32cbSBenjamin LaHaise inet = inet_sk(tunnel->sock); 896d2cf3361SBenjamin LaHaise if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) { 897e0d4435fSJames Chapman struct sockaddr_pppol2tp sp; 898e0d4435fSJames Chapman len = sizeof(sp); 899fd558d18SJames Chapman memset(&sp, 0, len); 900fd558d18SJames Chapman sp.sa_family = AF_PPPOX; 901fd558d18SJames Chapman sp.sa_protocol = PX_PROTO_OL2TP; 902fd558d18SJames Chapman sp.pppol2tp.fd = tunnel->fd; 903fd558d18SJames Chapman sp.pppol2tp.pid = pls->owner; 904fd558d18SJames Chapman sp.pppol2tp.s_tunnel = tunnel->tunnel_id; 905fd558d18SJames Chapman sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; 906fd558d18SJames Chapman sp.pppol2tp.s_session = session->session_id; 907fd558d18SJames Chapman sp.pppol2tp.d_session = session->peer_session_id; 908fd558d18SJames Chapman sp.pppol2tp.addr.sin_family = AF_INET; 909fd558d18SJames Chapman sp.pppol2tp.addr.sin_port = inet->inet_dport; 910fd558d18SJames Chapman sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr; 911fd558d18SJames Chapman memcpy(uaddr, &sp, len); 912d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 913d2cf3361SBenjamin LaHaise } else if ((tunnel->version == 2) && 914d2cf3361SBenjamin LaHaise (tunnel->sock->sk_family == AF_INET6)) { 915d2cf3361SBenjamin LaHaise struct ipv6_pinfo *np = inet6_sk(tunnel->sock); 916d2cf3361SBenjamin LaHaise struct sockaddr_pppol2tpin6 sp; 917d2cf3361SBenjamin LaHaise len = sizeof(sp); 918d2cf3361SBenjamin LaHaise memset(&sp, 0, len); 919d2cf3361SBenjamin LaHaise sp.sa_family = AF_PPPOX; 920d2cf3361SBenjamin LaHaise sp.sa_protocol = PX_PROTO_OL2TP; 921d2cf3361SBenjamin LaHaise sp.pppol2tp.fd = tunnel->fd; 922d2cf3361SBenjamin LaHaise sp.pppol2tp.pid = pls->owner; 923d2cf3361SBenjamin LaHaise sp.pppol2tp.s_tunnel = tunnel->tunnel_id; 924d2cf3361SBenjamin LaHaise sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; 925d2cf3361SBenjamin LaHaise sp.pppol2tp.s_session = session->session_id; 926d2cf3361SBenjamin LaHaise sp.pppol2tp.d_session = session->peer_session_id; 927d2cf3361SBenjamin LaHaise sp.pppol2tp.addr.sin6_family = AF_INET6; 928d2cf3361SBenjamin LaHaise sp.pppol2tp.addr.sin6_port = inet->inet_dport; 929d2cf3361SBenjamin LaHaise memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr, 930d2cf3361SBenjamin LaHaise sizeof(np->daddr)); 931d2cf3361SBenjamin LaHaise memcpy(uaddr, &sp, len); 932d2cf3361SBenjamin LaHaise } else if ((tunnel->version == 3) && 933d2cf3361SBenjamin LaHaise (tunnel->sock->sk_family == AF_INET6)) { 934d2cf3361SBenjamin LaHaise struct ipv6_pinfo *np = inet6_sk(tunnel->sock); 935d2cf3361SBenjamin LaHaise struct sockaddr_pppol2tpv3in6 sp; 936d2cf3361SBenjamin LaHaise len = sizeof(sp); 937d2cf3361SBenjamin LaHaise memset(&sp, 0, len); 938d2cf3361SBenjamin LaHaise sp.sa_family = AF_PPPOX; 939d2cf3361SBenjamin LaHaise sp.sa_protocol = PX_PROTO_OL2TP; 940d2cf3361SBenjamin LaHaise sp.pppol2tp.fd = tunnel->fd; 941d2cf3361SBenjamin LaHaise sp.pppol2tp.pid = pls->owner; 942d2cf3361SBenjamin LaHaise sp.pppol2tp.s_tunnel = tunnel->tunnel_id; 943d2cf3361SBenjamin LaHaise sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; 944d2cf3361SBenjamin LaHaise sp.pppol2tp.s_session = session->session_id; 945d2cf3361SBenjamin LaHaise sp.pppol2tp.d_session = session->peer_session_id; 946d2cf3361SBenjamin LaHaise sp.pppol2tp.addr.sin6_family = AF_INET6; 947d2cf3361SBenjamin LaHaise sp.pppol2tp.addr.sin6_port = inet->inet_dport; 948d2cf3361SBenjamin LaHaise memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr, 949d2cf3361SBenjamin LaHaise sizeof(np->daddr)); 950d2cf3361SBenjamin LaHaise memcpy(uaddr, &sp, len); 951d2cf3361SBenjamin LaHaise #endif 952e0d4435fSJames Chapman } else if (tunnel->version == 3) { 953e0d4435fSJames Chapman struct sockaddr_pppol2tpv3 sp; 954e0d4435fSJames Chapman len = sizeof(sp); 955e0d4435fSJames Chapman memset(&sp, 0, len); 956e0d4435fSJames Chapman sp.sa_family = AF_PPPOX; 957e0d4435fSJames Chapman sp.sa_protocol = PX_PROTO_OL2TP; 958e0d4435fSJames Chapman sp.pppol2tp.fd = tunnel->fd; 959e0d4435fSJames Chapman sp.pppol2tp.pid = pls->owner; 960e0d4435fSJames Chapman sp.pppol2tp.s_tunnel = tunnel->tunnel_id; 961e0d4435fSJames Chapman sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; 962e0d4435fSJames Chapman sp.pppol2tp.s_session = session->session_id; 963e0d4435fSJames Chapman sp.pppol2tp.d_session = session->peer_session_id; 964e0d4435fSJames Chapman sp.pppol2tp.addr.sin_family = AF_INET; 965e0d4435fSJames Chapman sp.pppol2tp.addr.sin_port = inet->inet_dport; 966e0d4435fSJames Chapman sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr; 967e0d4435fSJames Chapman memcpy(uaddr, &sp, len); 968e0d4435fSJames Chapman } 969fd558d18SJames Chapman 970fd558d18SJames Chapman *usockaddr_len = len; 971fd558d18SJames Chapman 972fd558d18SJames Chapman sock_put(pls->tunnel_sock); 973fd558d18SJames Chapman end_put_sess: 974fd558d18SJames Chapman sock_put(sk); 975fd558d18SJames Chapman error = 0; 976fd558d18SJames Chapman 977fd558d18SJames Chapman end: 978fd558d18SJames Chapman return error; 979fd558d18SJames Chapman } 980fd558d18SJames Chapman 981fd558d18SJames Chapman /**************************************************************************** 982fd558d18SJames Chapman * ioctl() handlers. 983fd558d18SJames Chapman * 984fd558d18SJames Chapman * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP 985fd558d18SJames Chapman * sockets. However, in order to control kernel tunnel features, we allow 986fd558d18SJames Chapman * userspace to create a special "tunnel" PPPoX socket which is used for 987fd558d18SJames Chapman * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow 988fd558d18SJames Chapman * the user application to issue L2TP setsockopt(), getsockopt() and ioctl() 989fd558d18SJames Chapman * calls. 990fd558d18SJames Chapman ****************************************************************************/ 991fd558d18SJames Chapman 992fd558d18SJames Chapman static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest, 993fd558d18SJames Chapman struct l2tp_stats *stats) 994fd558d18SJames Chapman { 995fd558d18SJames Chapman dest->tx_packets = stats->tx_packets; 996fd558d18SJames Chapman dest->tx_bytes = stats->tx_bytes; 997fd558d18SJames Chapman dest->tx_errors = stats->tx_errors; 998fd558d18SJames Chapman dest->rx_packets = stats->rx_packets; 999fd558d18SJames Chapman dest->rx_bytes = stats->rx_bytes; 1000fd558d18SJames Chapman dest->rx_seq_discards = stats->rx_seq_discards; 1001fd558d18SJames Chapman dest->rx_oos_packets = stats->rx_oos_packets; 1002fd558d18SJames Chapman dest->rx_errors = stats->rx_errors; 1003fd558d18SJames Chapman } 1004fd558d18SJames Chapman 1005fd558d18SJames Chapman /* Session ioctl helper. 1006fd558d18SJames Chapman */ 1007fd558d18SJames Chapman static int pppol2tp_session_ioctl(struct l2tp_session *session, 1008fd558d18SJames Chapman unsigned int cmd, unsigned long arg) 1009fd558d18SJames Chapman { 1010fd558d18SJames Chapman struct ifreq ifr; 1011fd558d18SJames Chapman int err = 0; 1012fd558d18SJames Chapman struct sock *sk; 1013fd558d18SJames Chapman int val = (int) arg; 1014fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 1015fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1016fd558d18SJames Chapman struct pppol2tp_ioc_stats stats; 1017fd558d18SJames Chapman 1018a4ca44faSJoe Perches l2tp_dbg(session, PPPOL2TP_MSG_CONTROL, 1019fd558d18SJames Chapman "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n", 1020fd558d18SJames Chapman session->name, cmd, arg); 1021fd558d18SJames Chapman 1022fd558d18SJames Chapman sk = ps->sock; 1023fd558d18SJames Chapman sock_hold(sk); 1024fd558d18SJames Chapman 1025fd558d18SJames Chapman switch (cmd) { 1026fd558d18SJames Chapman case SIOCGIFMTU: 1027fd558d18SJames Chapman err = -ENXIO; 1028fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1029fd558d18SJames Chapman break; 1030fd558d18SJames Chapman 1031fd558d18SJames Chapman err = -EFAULT; 1032fd558d18SJames Chapman if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq))) 1033fd558d18SJames Chapman break; 1034fd558d18SJames Chapman ifr.ifr_mtu = session->mtu; 1035fd558d18SJames Chapman if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq))) 1036fd558d18SJames Chapman break; 1037fd558d18SJames Chapman 1038a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mtu=%d\n", 1039a4ca44faSJoe Perches session->name, session->mtu); 1040fd558d18SJames Chapman err = 0; 1041fd558d18SJames Chapman break; 1042fd558d18SJames Chapman 1043fd558d18SJames Chapman case SIOCSIFMTU: 1044fd558d18SJames Chapman err = -ENXIO; 1045fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1046fd558d18SJames Chapman break; 1047fd558d18SJames Chapman 1048fd558d18SJames Chapman err = -EFAULT; 1049fd558d18SJames Chapman if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq))) 1050fd558d18SJames Chapman break; 1051fd558d18SJames Chapman 1052fd558d18SJames Chapman session->mtu = ifr.ifr_mtu; 1053fd558d18SJames Chapman 1054a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mtu=%d\n", 1055a4ca44faSJoe Perches session->name, session->mtu); 1056fd558d18SJames Chapman err = 0; 1057fd558d18SJames Chapman break; 1058fd558d18SJames Chapman 1059fd558d18SJames Chapman case PPPIOCGMRU: 1060fd558d18SJames Chapman err = -ENXIO; 1061fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1062fd558d18SJames Chapman break; 1063fd558d18SJames Chapman 1064fd558d18SJames Chapman err = -EFAULT; 1065fd558d18SJames Chapman if (put_user(session->mru, (int __user *) arg)) 1066fd558d18SJames Chapman break; 1067fd558d18SJames Chapman 1068a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mru=%d\n", 1069a4ca44faSJoe Perches session->name, session->mru); 1070fd558d18SJames Chapman err = 0; 1071fd558d18SJames Chapman break; 1072fd558d18SJames Chapman 1073fd558d18SJames Chapman case PPPIOCSMRU: 1074fd558d18SJames Chapman err = -ENXIO; 1075fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1076fd558d18SJames Chapman break; 1077fd558d18SJames Chapman 1078fd558d18SJames Chapman err = -EFAULT; 1079fd558d18SJames Chapman if (get_user(val, (int __user *) arg)) 1080fd558d18SJames Chapman break; 1081fd558d18SJames Chapman 1082fd558d18SJames Chapman session->mru = val; 1083a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mru=%d\n", 1084a4ca44faSJoe Perches session->name, session->mru); 1085fd558d18SJames Chapman err = 0; 1086fd558d18SJames Chapman break; 1087fd558d18SJames Chapman 1088fd558d18SJames Chapman case PPPIOCGFLAGS: 1089fd558d18SJames Chapman err = -EFAULT; 1090fd558d18SJames Chapman if (put_user(ps->flags, (int __user *) arg)) 1091fd558d18SJames Chapman break; 1092fd558d18SJames Chapman 1093a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get flags=%d\n", 1094a4ca44faSJoe Perches session->name, ps->flags); 1095fd558d18SJames Chapman err = 0; 1096fd558d18SJames Chapman break; 1097fd558d18SJames Chapman 1098fd558d18SJames Chapman case PPPIOCSFLAGS: 1099fd558d18SJames Chapman err = -EFAULT; 1100fd558d18SJames Chapman if (get_user(val, (int __user *) arg)) 1101fd558d18SJames Chapman break; 1102fd558d18SJames Chapman ps->flags = val; 1103a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set flags=%d\n", 1104a4ca44faSJoe Perches session->name, ps->flags); 1105fd558d18SJames Chapman err = 0; 1106fd558d18SJames Chapman break; 1107fd558d18SJames Chapman 1108fd558d18SJames Chapman case PPPIOCGL2TPSTATS: 1109fd558d18SJames Chapman err = -ENXIO; 1110fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1111fd558d18SJames Chapman break; 1112fd558d18SJames Chapman 1113fd558d18SJames Chapman memset(&stats, 0, sizeof(stats)); 1114fd558d18SJames Chapman stats.tunnel_id = tunnel->tunnel_id; 1115fd558d18SJames Chapman stats.session_id = session->session_id; 1116fd558d18SJames Chapman pppol2tp_copy_stats(&stats, &session->stats); 1117fd558d18SJames Chapman if (copy_to_user((void __user *) arg, &stats, 1118fd558d18SJames Chapman sizeof(stats))) 1119fd558d18SJames Chapman break; 1120a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n", 1121a4ca44faSJoe Perches session->name); 1122fd558d18SJames Chapman err = 0; 1123fd558d18SJames Chapman break; 1124fd558d18SJames Chapman 1125fd558d18SJames Chapman default: 1126fd558d18SJames Chapman err = -ENOSYS; 1127fd558d18SJames Chapman break; 1128fd558d18SJames Chapman } 1129fd558d18SJames Chapman 1130fd558d18SJames Chapman sock_put(sk); 1131fd558d18SJames Chapman 1132fd558d18SJames Chapman return err; 1133fd558d18SJames Chapman } 1134fd558d18SJames Chapman 1135fd558d18SJames Chapman /* Tunnel ioctl helper. 1136fd558d18SJames Chapman * 1137fd558d18SJames Chapman * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data 1138fd558d18SJames Chapman * specifies a session_id, the session ioctl handler is called. This allows an 1139fd558d18SJames Chapman * application to retrieve session stats via a tunnel socket. 1140fd558d18SJames Chapman */ 1141fd558d18SJames Chapman static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel, 1142fd558d18SJames Chapman unsigned int cmd, unsigned long arg) 1143fd558d18SJames Chapman { 1144fd558d18SJames Chapman int err = 0; 1145fd558d18SJames Chapman struct sock *sk; 1146fd558d18SJames Chapman struct pppol2tp_ioc_stats stats; 1147fd558d18SJames Chapman 1148a4ca44faSJoe Perches l2tp_dbg(tunnel, PPPOL2TP_MSG_CONTROL, 1149fd558d18SJames Chapman "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", 1150fd558d18SJames Chapman tunnel->name, cmd, arg); 1151fd558d18SJames Chapman 1152fd558d18SJames Chapman sk = tunnel->sock; 1153fd558d18SJames Chapman sock_hold(sk); 1154fd558d18SJames Chapman 1155fd558d18SJames Chapman switch (cmd) { 1156fd558d18SJames Chapman case PPPIOCGL2TPSTATS: 1157fd558d18SJames Chapman err = -ENXIO; 1158fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1159fd558d18SJames Chapman break; 1160fd558d18SJames Chapman 1161fd558d18SJames Chapman if (copy_from_user(&stats, (void __user *) arg, 1162fd558d18SJames Chapman sizeof(stats))) { 1163fd558d18SJames Chapman err = -EFAULT; 1164fd558d18SJames Chapman break; 1165fd558d18SJames Chapman } 1166fd558d18SJames Chapman if (stats.session_id != 0) { 1167fd558d18SJames Chapman /* resend to session ioctl handler */ 1168fd558d18SJames Chapman struct l2tp_session *session = 1169f7faffa3SJames Chapman l2tp_session_find(sock_net(sk), tunnel, stats.session_id); 1170fd558d18SJames Chapman if (session != NULL) 1171fd558d18SJames Chapman err = pppol2tp_session_ioctl(session, cmd, arg); 1172fd558d18SJames Chapman else 1173fd558d18SJames Chapman err = -EBADR; 1174fd558d18SJames Chapman break; 1175fd558d18SJames Chapman } 1176fd558d18SJames Chapman #ifdef CONFIG_XFRM 1177fd558d18SJames Chapman stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0; 1178fd558d18SJames Chapman #endif 1179fd558d18SJames Chapman pppol2tp_copy_stats(&stats, &tunnel->stats); 1180fd558d18SJames Chapman if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) { 1181fd558d18SJames Chapman err = -EFAULT; 1182fd558d18SJames Chapman break; 1183fd558d18SJames Chapman } 1184a4ca44faSJoe Perches l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n", 1185a4ca44faSJoe Perches tunnel->name); 1186fd558d18SJames Chapman err = 0; 1187fd558d18SJames Chapman break; 1188fd558d18SJames Chapman 1189fd558d18SJames Chapman default: 1190fd558d18SJames Chapman err = -ENOSYS; 1191fd558d18SJames Chapman break; 1192fd558d18SJames Chapman } 1193fd558d18SJames Chapman 1194fd558d18SJames Chapman sock_put(sk); 1195fd558d18SJames Chapman 1196fd558d18SJames Chapman return err; 1197fd558d18SJames Chapman } 1198fd558d18SJames Chapman 1199fd558d18SJames Chapman /* Main ioctl() handler. 1200fd558d18SJames Chapman * Dispatch to tunnel or session helpers depending on the socket. 1201fd558d18SJames Chapman */ 1202fd558d18SJames Chapman static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd, 1203fd558d18SJames Chapman unsigned long arg) 1204fd558d18SJames Chapman { 1205fd558d18SJames Chapman struct sock *sk = sock->sk; 1206fd558d18SJames Chapman struct l2tp_session *session; 1207fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1208fd558d18SJames Chapman struct pppol2tp_session *ps; 1209fd558d18SJames Chapman int err; 1210fd558d18SJames Chapman 1211fd558d18SJames Chapman if (!sk) 1212fd558d18SJames Chapman return 0; 1213fd558d18SJames Chapman 1214fd558d18SJames Chapman err = -EBADF; 1215fd558d18SJames Chapman if (sock_flag(sk, SOCK_DEAD) != 0) 1216fd558d18SJames Chapman goto end; 1217fd558d18SJames Chapman 1218fd558d18SJames Chapman err = -ENOTCONN; 1219fd558d18SJames Chapman if ((sk->sk_user_data == NULL) || 1220fd558d18SJames Chapman (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)))) 1221fd558d18SJames Chapman goto end; 1222fd558d18SJames Chapman 1223fd558d18SJames Chapman /* Get session context from the socket */ 1224fd558d18SJames Chapman err = -EBADF; 1225fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 1226fd558d18SJames Chapman if (session == NULL) 1227fd558d18SJames Chapman goto end; 1228fd558d18SJames Chapman 1229fd558d18SJames Chapman /* Special case: if session's session_id is zero, treat ioctl as a 1230fd558d18SJames Chapman * tunnel ioctl 1231fd558d18SJames Chapman */ 1232fd558d18SJames Chapman ps = l2tp_session_priv(session); 1233fd558d18SJames Chapman if ((session->session_id == 0) && 1234fd558d18SJames Chapman (session->peer_session_id == 0)) { 1235fd558d18SJames Chapman err = -EBADF; 1236fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock); 1237fd558d18SJames Chapman if (tunnel == NULL) 1238fd558d18SJames Chapman goto end_put_sess; 1239fd558d18SJames Chapman 1240fd558d18SJames Chapman err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg); 1241fd558d18SJames Chapman sock_put(ps->tunnel_sock); 1242fd558d18SJames Chapman goto end_put_sess; 1243fd558d18SJames Chapman } 1244fd558d18SJames Chapman 1245fd558d18SJames Chapman err = pppol2tp_session_ioctl(session, cmd, arg); 1246fd558d18SJames Chapman 1247fd558d18SJames Chapman end_put_sess: 1248fd558d18SJames Chapman sock_put(sk); 1249fd558d18SJames Chapman end: 1250fd558d18SJames Chapman return err; 1251fd558d18SJames Chapman } 1252fd558d18SJames Chapman 1253fd558d18SJames Chapman /***************************************************************************** 1254fd558d18SJames Chapman * setsockopt() / getsockopt() support. 1255fd558d18SJames Chapman * 1256fd558d18SJames Chapman * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP 1257fd558d18SJames Chapman * sockets. In order to control kernel tunnel features, we allow userspace to 1258fd558d18SJames Chapman * create a special "tunnel" PPPoX socket which is used for control only. 1259fd558d18SJames Chapman * Tunnel PPPoX sockets have session_id == 0 and simply allow the user 1260fd558d18SJames Chapman * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls. 1261fd558d18SJames Chapman *****************************************************************************/ 1262fd558d18SJames Chapman 1263fd558d18SJames Chapman /* Tunnel setsockopt() helper. 1264fd558d18SJames Chapman */ 1265fd558d18SJames Chapman static int pppol2tp_tunnel_setsockopt(struct sock *sk, 1266fd558d18SJames Chapman struct l2tp_tunnel *tunnel, 1267fd558d18SJames Chapman int optname, int val) 1268fd558d18SJames Chapman { 1269fd558d18SJames Chapman int err = 0; 1270fd558d18SJames Chapman 1271fd558d18SJames Chapman switch (optname) { 1272fd558d18SJames Chapman case PPPOL2TP_SO_DEBUG: 1273fd558d18SJames Chapman tunnel->debug = val; 1274a4ca44faSJoe Perches l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n", 1275a4ca44faSJoe Perches tunnel->name, tunnel->debug); 1276fd558d18SJames Chapman break; 1277fd558d18SJames Chapman 1278fd558d18SJames Chapman default: 1279fd558d18SJames Chapman err = -ENOPROTOOPT; 1280fd558d18SJames Chapman break; 1281fd558d18SJames Chapman } 1282fd558d18SJames Chapman 1283fd558d18SJames Chapman return err; 1284fd558d18SJames Chapman } 1285fd558d18SJames Chapman 1286fd558d18SJames Chapman /* Session setsockopt helper. 1287fd558d18SJames Chapman */ 1288fd558d18SJames Chapman static int pppol2tp_session_setsockopt(struct sock *sk, 1289fd558d18SJames Chapman struct l2tp_session *session, 1290fd558d18SJames Chapman int optname, int val) 1291fd558d18SJames Chapman { 1292fd558d18SJames Chapman int err = 0; 1293fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 1294fd558d18SJames Chapman 1295fd558d18SJames Chapman switch (optname) { 1296fd558d18SJames Chapman case PPPOL2TP_SO_RECVSEQ: 1297fd558d18SJames Chapman if ((val != 0) && (val != 1)) { 1298fd558d18SJames Chapman err = -EINVAL; 1299fd558d18SJames Chapman break; 1300fd558d18SJames Chapman } 1301fd558d18SJames Chapman session->recv_seq = val ? -1 : 0; 1302a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, 1303a4ca44faSJoe Perches "%s: set recv_seq=%d\n", 1304a4ca44faSJoe Perches session->name, session->recv_seq); 1305fd558d18SJames Chapman break; 1306fd558d18SJames Chapman 1307fd558d18SJames Chapman case PPPOL2TP_SO_SENDSEQ: 1308fd558d18SJames Chapman if ((val != 0) && (val != 1)) { 1309fd558d18SJames Chapman err = -EINVAL; 1310fd558d18SJames Chapman break; 1311fd558d18SJames Chapman } 1312fd558d18SJames Chapman session->send_seq = val ? -1 : 0; 1313fd558d18SJames Chapman { 1314fd558d18SJames Chapman struct sock *ssk = ps->sock; 1315fd558d18SJames Chapman struct pppox_sock *po = pppox_sk(ssk); 1316fd558d18SJames Chapman po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ : 1317fd558d18SJames Chapman PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; 1318fd558d18SJames Chapman } 1319a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, 1320a4ca44faSJoe Perches "%s: set send_seq=%d\n", 1321a4ca44faSJoe Perches session->name, session->send_seq); 1322fd558d18SJames Chapman break; 1323fd558d18SJames Chapman 1324fd558d18SJames Chapman case PPPOL2TP_SO_LNSMODE: 1325fd558d18SJames Chapman if ((val != 0) && (val != 1)) { 1326fd558d18SJames Chapman err = -EINVAL; 1327fd558d18SJames Chapman break; 1328fd558d18SJames Chapman } 1329fd558d18SJames Chapman session->lns_mode = val ? -1 : 0; 1330a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, 1331a4ca44faSJoe Perches "%s: set lns_mode=%d\n", 1332a4ca44faSJoe Perches session->name, session->lns_mode); 1333fd558d18SJames Chapman break; 1334fd558d18SJames Chapman 1335fd558d18SJames Chapman case PPPOL2TP_SO_DEBUG: 1336fd558d18SJames Chapman session->debug = val; 1337a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n", 1338a4ca44faSJoe Perches session->name, session->debug); 1339fd558d18SJames Chapman break; 1340fd558d18SJames Chapman 1341fd558d18SJames Chapman case PPPOL2TP_SO_REORDERTO: 1342fd558d18SJames Chapman session->reorder_timeout = msecs_to_jiffies(val); 1343a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, 1344a4ca44faSJoe Perches "%s: set reorder_timeout=%d\n", 1345a4ca44faSJoe Perches session->name, session->reorder_timeout); 1346fd558d18SJames Chapman break; 1347fd558d18SJames Chapman 1348fd558d18SJames Chapman default: 1349fd558d18SJames Chapman err = -ENOPROTOOPT; 1350fd558d18SJames Chapman break; 1351fd558d18SJames Chapman } 1352fd558d18SJames Chapman 1353fd558d18SJames Chapman return err; 1354fd558d18SJames Chapman } 1355fd558d18SJames Chapman 1356fd558d18SJames Chapman /* Main setsockopt() entry point. 1357fd558d18SJames Chapman * Does API checks, then calls either the tunnel or session setsockopt 1358fd558d18SJames Chapman * handler, according to whether the PPPoL2TP socket is a for a regular 1359fd558d18SJames Chapman * session or the special tunnel type. 1360fd558d18SJames Chapman */ 1361fd558d18SJames Chapman static int pppol2tp_setsockopt(struct socket *sock, int level, int optname, 1362fd558d18SJames Chapman char __user *optval, unsigned int optlen) 1363fd558d18SJames Chapman { 1364fd558d18SJames Chapman struct sock *sk = sock->sk; 1365fd558d18SJames Chapman struct l2tp_session *session; 1366fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1367fd558d18SJames Chapman struct pppol2tp_session *ps; 1368fd558d18SJames Chapman int val; 1369fd558d18SJames Chapman int err; 1370fd558d18SJames Chapman 1371fd558d18SJames Chapman if (level != SOL_PPPOL2TP) 1372fd558d18SJames Chapman return udp_prot.setsockopt(sk, level, optname, optval, optlen); 1373fd558d18SJames Chapman 1374fd558d18SJames Chapman if (optlen < sizeof(int)) 1375fd558d18SJames Chapman return -EINVAL; 1376fd558d18SJames Chapman 1377fd558d18SJames Chapman if (get_user(val, (int __user *)optval)) 1378fd558d18SJames Chapman return -EFAULT; 1379fd558d18SJames Chapman 1380fd558d18SJames Chapman err = -ENOTCONN; 1381fd558d18SJames Chapman if (sk->sk_user_data == NULL) 1382fd558d18SJames Chapman goto end; 1383fd558d18SJames Chapman 1384fd558d18SJames Chapman /* Get session context from the socket */ 1385fd558d18SJames Chapman err = -EBADF; 1386fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 1387fd558d18SJames Chapman if (session == NULL) 1388fd558d18SJames Chapman goto end; 1389fd558d18SJames Chapman 1390fd558d18SJames Chapman /* Special case: if session_id == 0x0000, treat as operation on tunnel 1391fd558d18SJames Chapman */ 1392fd558d18SJames Chapman ps = l2tp_session_priv(session); 1393fd558d18SJames Chapman if ((session->session_id == 0) && 1394fd558d18SJames Chapman (session->peer_session_id == 0)) { 1395fd558d18SJames Chapman err = -EBADF; 1396fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock); 1397fd558d18SJames Chapman if (tunnel == NULL) 1398fd558d18SJames Chapman goto end_put_sess; 1399fd558d18SJames Chapman 1400fd558d18SJames Chapman err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val); 1401fd558d18SJames Chapman sock_put(ps->tunnel_sock); 1402fd558d18SJames Chapman } else 1403fd558d18SJames Chapman err = pppol2tp_session_setsockopt(sk, session, optname, val); 1404fd558d18SJames Chapman 1405fd558d18SJames Chapman err = 0; 1406fd558d18SJames Chapman 1407fd558d18SJames Chapman end_put_sess: 1408fd558d18SJames Chapman sock_put(sk); 1409fd558d18SJames Chapman end: 1410fd558d18SJames Chapman return err; 1411fd558d18SJames Chapman } 1412fd558d18SJames Chapman 1413fd558d18SJames Chapman /* Tunnel getsockopt helper. Called with sock locked. 1414fd558d18SJames Chapman */ 1415fd558d18SJames Chapman static int pppol2tp_tunnel_getsockopt(struct sock *sk, 1416fd558d18SJames Chapman struct l2tp_tunnel *tunnel, 1417fd558d18SJames Chapman int optname, int *val) 1418fd558d18SJames Chapman { 1419fd558d18SJames Chapman int err = 0; 1420fd558d18SJames Chapman 1421fd558d18SJames Chapman switch (optname) { 1422fd558d18SJames Chapman case PPPOL2TP_SO_DEBUG: 1423fd558d18SJames Chapman *val = tunnel->debug; 1424a4ca44faSJoe Perches l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get debug=%x\n", 1425a4ca44faSJoe Perches tunnel->name, tunnel->debug); 1426fd558d18SJames Chapman break; 1427fd558d18SJames Chapman 1428fd558d18SJames Chapman default: 1429fd558d18SJames Chapman err = -ENOPROTOOPT; 1430fd558d18SJames Chapman break; 1431fd558d18SJames Chapman } 1432fd558d18SJames Chapman 1433fd558d18SJames Chapman return err; 1434fd558d18SJames Chapman } 1435fd558d18SJames Chapman 1436fd558d18SJames Chapman /* Session getsockopt helper. Called with sock locked. 1437fd558d18SJames Chapman */ 1438fd558d18SJames Chapman static int pppol2tp_session_getsockopt(struct sock *sk, 1439fd558d18SJames Chapman struct l2tp_session *session, 1440fd558d18SJames Chapman int optname, int *val) 1441fd558d18SJames Chapman { 1442fd558d18SJames Chapman int err = 0; 1443fd558d18SJames Chapman 1444fd558d18SJames Chapman switch (optname) { 1445fd558d18SJames Chapman case PPPOL2TP_SO_RECVSEQ: 1446fd558d18SJames Chapman *val = session->recv_seq; 1447a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, 1448fd558d18SJames Chapman "%s: get recv_seq=%d\n", session->name, *val); 1449fd558d18SJames Chapman break; 1450fd558d18SJames Chapman 1451fd558d18SJames Chapman case PPPOL2TP_SO_SENDSEQ: 1452fd558d18SJames Chapman *val = session->send_seq; 1453a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, 1454fd558d18SJames Chapman "%s: get send_seq=%d\n", session->name, *val); 1455fd558d18SJames Chapman break; 1456fd558d18SJames Chapman 1457fd558d18SJames Chapman case PPPOL2TP_SO_LNSMODE: 1458fd558d18SJames Chapman *val = session->lns_mode; 1459a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, 1460fd558d18SJames Chapman "%s: get lns_mode=%d\n", session->name, *val); 1461fd558d18SJames Chapman break; 1462fd558d18SJames Chapman 1463fd558d18SJames Chapman case PPPOL2TP_SO_DEBUG: 1464fd558d18SJames Chapman *val = session->debug; 1465a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get debug=%d\n", 1466a4ca44faSJoe Perches session->name, *val); 1467fd558d18SJames Chapman break; 1468fd558d18SJames Chapman 1469fd558d18SJames Chapman case PPPOL2TP_SO_REORDERTO: 1470fd558d18SJames Chapman *val = (int) jiffies_to_msecs(session->reorder_timeout); 1471a4ca44faSJoe Perches l2tp_info(session, PPPOL2TP_MSG_CONTROL, 1472fd558d18SJames Chapman "%s: get reorder_timeout=%d\n", session->name, *val); 1473fd558d18SJames Chapman break; 1474fd558d18SJames Chapman 1475fd558d18SJames Chapman default: 1476fd558d18SJames Chapman err = -ENOPROTOOPT; 1477fd558d18SJames Chapman } 1478fd558d18SJames Chapman 1479fd558d18SJames Chapman return err; 1480fd558d18SJames Chapman } 1481fd558d18SJames Chapman 1482fd558d18SJames Chapman /* Main getsockopt() entry point. 1483fd558d18SJames Chapman * Does API checks, then calls either the tunnel or session getsockopt 1484fd558d18SJames Chapman * handler, according to whether the PPPoX socket is a for a regular session 1485fd558d18SJames Chapman * or the special tunnel type. 1486fd558d18SJames Chapman */ 1487e3192690SJoe Perches static int pppol2tp_getsockopt(struct socket *sock, int level, int optname, 1488e3192690SJoe Perches char __user *optval, int __user *optlen) 1489fd558d18SJames Chapman { 1490fd558d18SJames Chapman struct sock *sk = sock->sk; 1491fd558d18SJames Chapman struct l2tp_session *session; 1492fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1493fd558d18SJames Chapman int val, len; 1494fd558d18SJames Chapman int err; 1495fd558d18SJames Chapman struct pppol2tp_session *ps; 1496fd558d18SJames Chapman 1497fd558d18SJames Chapman if (level != SOL_PPPOL2TP) 1498fd558d18SJames Chapman return udp_prot.getsockopt(sk, level, optname, optval, optlen); 1499fd558d18SJames Chapman 1500e3192690SJoe Perches if (get_user(len, optlen)) 1501fd558d18SJames Chapman return -EFAULT; 1502fd558d18SJames Chapman 1503fd558d18SJames Chapman len = min_t(unsigned int, len, sizeof(int)); 1504fd558d18SJames Chapman 1505fd558d18SJames Chapman if (len < 0) 1506fd558d18SJames Chapman return -EINVAL; 1507fd558d18SJames Chapman 1508fd558d18SJames Chapman err = -ENOTCONN; 1509fd558d18SJames Chapman if (sk->sk_user_data == NULL) 1510fd558d18SJames Chapman goto end; 1511fd558d18SJames Chapman 1512fd558d18SJames Chapman /* Get the session context */ 1513fd558d18SJames Chapman err = -EBADF; 1514fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 1515fd558d18SJames Chapman if (session == NULL) 1516fd558d18SJames Chapman goto end; 1517fd558d18SJames Chapman 1518fd558d18SJames Chapman /* Special case: if session_id == 0x0000, treat as operation on tunnel */ 1519fd558d18SJames Chapman ps = l2tp_session_priv(session); 1520fd558d18SJames Chapman if ((session->session_id == 0) && 1521fd558d18SJames Chapman (session->peer_session_id == 0)) { 1522fd558d18SJames Chapman err = -EBADF; 1523fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock); 1524fd558d18SJames Chapman if (tunnel == NULL) 1525fd558d18SJames Chapman goto end_put_sess; 1526fd558d18SJames Chapman 1527fd558d18SJames Chapman err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val); 1528fd558d18SJames Chapman sock_put(ps->tunnel_sock); 1529fd558d18SJames Chapman } else 1530fd558d18SJames Chapman err = pppol2tp_session_getsockopt(sk, session, optname, &val); 1531fd558d18SJames Chapman 1532fd558d18SJames Chapman err = -EFAULT; 1533e3192690SJoe Perches if (put_user(len, optlen)) 1534fd558d18SJames Chapman goto end_put_sess; 1535fd558d18SJames Chapman 1536fd558d18SJames Chapman if (copy_to_user((void __user *) optval, &val, len)) 1537fd558d18SJames Chapman goto end_put_sess; 1538fd558d18SJames Chapman 1539fd558d18SJames Chapman err = 0; 1540fd558d18SJames Chapman 1541fd558d18SJames Chapman end_put_sess: 1542fd558d18SJames Chapman sock_put(sk); 1543fd558d18SJames Chapman end: 1544fd558d18SJames Chapman return err; 1545fd558d18SJames Chapman } 1546fd558d18SJames Chapman 1547fd558d18SJames Chapman /***************************************************************************** 1548fd558d18SJames Chapman * /proc filesystem for debug 1549f7faffa3SJames Chapman * Since the original pppol2tp driver provided /proc/net/pppol2tp for 1550f7faffa3SJames Chapman * L2TPv2, we dump only L2TPv2 tunnels and sessions here. 1551fd558d18SJames Chapman *****************************************************************************/ 1552fd558d18SJames Chapman 1553fd558d18SJames Chapman static unsigned int pppol2tp_net_id; 1554fd558d18SJames Chapman 1555fd558d18SJames Chapman #ifdef CONFIG_PROC_FS 1556fd558d18SJames Chapman 1557fd558d18SJames Chapman struct pppol2tp_seq_data { 1558fd558d18SJames Chapman struct seq_net_private p; 1559fd558d18SJames Chapman int tunnel_idx; /* current tunnel */ 1560fd558d18SJames Chapman int session_idx; /* index of session within current tunnel */ 1561fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1562fd558d18SJames Chapman struct l2tp_session *session; /* NULL means get next tunnel */ 1563fd558d18SJames Chapman }; 1564fd558d18SJames Chapman 1565fd558d18SJames Chapman static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd) 1566fd558d18SJames Chapman { 1567f7faffa3SJames Chapman for (;;) { 1568fd558d18SJames Chapman pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx); 1569fd558d18SJames Chapman pd->tunnel_idx++; 1570f7faffa3SJames Chapman 1571f7faffa3SJames Chapman if (pd->tunnel == NULL) 1572f7faffa3SJames Chapman break; 1573f7faffa3SJames Chapman 1574f7faffa3SJames Chapman /* Ignore L2TPv3 tunnels */ 1575f7faffa3SJames Chapman if (pd->tunnel->version < 3) 1576f7faffa3SJames Chapman break; 1577f7faffa3SJames Chapman } 1578fd558d18SJames Chapman } 1579fd558d18SJames Chapman 1580fd558d18SJames Chapman static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd) 1581fd558d18SJames Chapman { 1582fd558d18SJames Chapman pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx); 1583fd558d18SJames Chapman pd->session_idx++; 1584f7faffa3SJames Chapman 1585fd558d18SJames Chapman if (pd->session == NULL) { 1586fd558d18SJames Chapman pd->session_idx = 0; 1587fd558d18SJames Chapman pppol2tp_next_tunnel(net, pd); 1588fd558d18SJames Chapman } 1589fd558d18SJames Chapman } 1590fd558d18SJames Chapman 1591fd558d18SJames Chapman static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs) 1592fd558d18SJames Chapman { 1593fd558d18SJames Chapman struct pppol2tp_seq_data *pd = SEQ_START_TOKEN; 1594fd558d18SJames Chapman loff_t pos = *offs; 1595fd558d18SJames Chapman struct net *net; 1596fd558d18SJames Chapman 1597fd558d18SJames Chapman if (!pos) 1598fd558d18SJames Chapman goto out; 1599fd558d18SJames Chapman 1600fd558d18SJames Chapman BUG_ON(m->private == NULL); 1601fd558d18SJames Chapman pd = m->private; 1602fd558d18SJames Chapman net = seq_file_net(m); 1603fd558d18SJames Chapman 1604fd558d18SJames Chapman if (pd->tunnel == NULL) 1605fd558d18SJames Chapman pppol2tp_next_tunnel(net, pd); 1606fd558d18SJames Chapman else 1607fd558d18SJames Chapman pppol2tp_next_session(net, pd); 1608fd558d18SJames Chapman 1609fd558d18SJames Chapman /* NULL tunnel and session indicates end of list */ 1610fd558d18SJames Chapman if ((pd->tunnel == NULL) && (pd->session == NULL)) 1611fd558d18SJames Chapman pd = NULL; 1612fd558d18SJames Chapman 1613fd558d18SJames Chapman out: 1614fd558d18SJames Chapman return pd; 1615fd558d18SJames Chapman } 1616fd558d18SJames Chapman 1617fd558d18SJames Chapman static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos) 1618fd558d18SJames Chapman { 1619fd558d18SJames Chapman (*pos)++; 1620fd558d18SJames Chapman return NULL; 1621fd558d18SJames Chapman } 1622fd558d18SJames Chapman 1623fd558d18SJames Chapman static void pppol2tp_seq_stop(struct seq_file *p, void *v) 1624fd558d18SJames Chapman { 1625fd558d18SJames Chapman /* nothing to do */ 1626fd558d18SJames Chapman } 1627fd558d18SJames Chapman 1628fd558d18SJames Chapman static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v) 1629fd558d18SJames Chapman { 1630fd558d18SJames Chapman struct l2tp_tunnel *tunnel = v; 1631fd558d18SJames Chapman 1632fd558d18SJames Chapman seq_printf(m, "\nTUNNEL '%s', %c %d\n", 1633fd558d18SJames Chapman tunnel->name, 1634fd558d18SJames Chapman (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N', 1635fd558d18SJames Chapman atomic_read(&tunnel->ref_count) - 1); 1636fd558d18SJames Chapman seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n", 1637fd558d18SJames Chapman tunnel->debug, 1638fd558d18SJames Chapman (unsigned long long)tunnel->stats.tx_packets, 1639fd558d18SJames Chapman (unsigned long long)tunnel->stats.tx_bytes, 1640fd558d18SJames Chapman (unsigned long long)tunnel->stats.tx_errors, 1641fd558d18SJames Chapman (unsigned long long)tunnel->stats.rx_packets, 1642fd558d18SJames Chapman (unsigned long long)tunnel->stats.rx_bytes, 1643fd558d18SJames Chapman (unsigned long long)tunnel->stats.rx_errors); 1644fd558d18SJames Chapman } 1645fd558d18SJames Chapman 1646fd558d18SJames Chapman static void pppol2tp_seq_session_show(struct seq_file *m, void *v) 1647fd558d18SJames Chapman { 1648fd558d18SJames Chapman struct l2tp_session *session = v; 1649fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1650fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 16519345471bSJames Chapman struct pppox_sock *po = pppox_sk(ps->sock); 1652fd558d18SJames Chapman u32 ip = 0; 1653fd558d18SJames Chapman u16 port = 0; 1654fd558d18SJames Chapman 1655fd558d18SJames Chapman if (tunnel->sock) { 1656fd558d18SJames Chapman struct inet_sock *inet = inet_sk(tunnel->sock); 1657fd558d18SJames Chapman ip = ntohl(inet->inet_saddr); 1658fd558d18SJames Chapman port = ntohs(inet->inet_sport); 1659fd558d18SJames Chapman } 1660fd558d18SJames Chapman 1661fd558d18SJames Chapman seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> " 1662fd558d18SJames Chapman "%04X/%04X %d %c\n", 1663fd558d18SJames Chapman session->name, ip, port, 1664fd558d18SJames Chapman tunnel->tunnel_id, 1665fd558d18SJames Chapman session->session_id, 1666fd558d18SJames Chapman tunnel->peer_tunnel_id, 1667fd558d18SJames Chapman session->peer_session_id, 1668fd558d18SJames Chapman ps->sock->sk_state, 1669fd558d18SJames Chapman (session == ps->sock->sk_user_data) ? 1670fd558d18SJames Chapman 'Y' : 'N'); 1671fd558d18SJames Chapman seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n", 1672fd558d18SJames Chapman session->mtu, session->mru, 1673fd558d18SJames Chapman session->recv_seq ? 'R' : '-', 1674fd558d18SJames Chapman session->send_seq ? 'S' : '-', 1675fd558d18SJames Chapman session->lns_mode ? "LNS" : "LAC", 1676fd558d18SJames Chapman session->debug, 1677fd558d18SJames Chapman jiffies_to_msecs(session->reorder_timeout)); 1678fd558d18SJames Chapman seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n", 1679fd558d18SJames Chapman session->nr, session->ns, 1680fd558d18SJames Chapman (unsigned long long)session->stats.tx_packets, 1681fd558d18SJames Chapman (unsigned long long)session->stats.tx_bytes, 1682fd558d18SJames Chapman (unsigned long long)session->stats.tx_errors, 1683fd558d18SJames Chapman (unsigned long long)session->stats.rx_packets, 1684fd558d18SJames Chapman (unsigned long long)session->stats.rx_bytes, 1685fd558d18SJames Chapman (unsigned long long)session->stats.rx_errors); 16869345471bSJames Chapman 16879345471bSJames Chapman if (po) 16889345471bSJames Chapman seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan)); 1689fd558d18SJames Chapman } 1690fd558d18SJames Chapman 1691fd558d18SJames Chapman static int pppol2tp_seq_show(struct seq_file *m, void *v) 1692fd558d18SJames Chapman { 1693fd558d18SJames Chapman struct pppol2tp_seq_data *pd = v; 1694fd558d18SJames Chapman 1695fd558d18SJames Chapman /* display header on line 1 */ 1696fd558d18SJames Chapman if (v == SEQ_START_TOKEN) { 1697fd558d18SJames Chapman seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n"); 1698fd558d18SJames Chapman seq_puts(m, "TUNNEL name, user-data-ok session-count\n"); 1699fd558d18SJames Chapman seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); 1700fd558d18SJames Chapman seq_puts(m, " SESSION name, addr/port src-tid/sid " 1701fd558d18SJames Chapman "dest-tid/sid state user-data-ok\n"); 1702fd558d18SJames Chapman seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n"); 1703fd558d18SJames Chapman seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); 1704fd558d18SJames Chapman goto out; 1705fd558d18SJames Chapman } 1706fd558d18SJames Chapman 1707fd558d18SJames Chapman /* Show the tunnel or session context. 1708fd558d18SJames Chapman */ 1709fd558d18SJames Chapman if (pd->session == NULL) 1710fd558d18SJames Chapman pppol2tp_seq_tunnel_show(m, pd->tunnel); 1711fd558d18SJames Chapman else 1712fd558d18SJames Chapman pppol2tp_seq_session_show(m, pd->session); 1713fd558d18SJames Chapman 1714fd558d18SJames Chapman out: 1715fd558d18SJames Chapman return 0; 1716fd558d18SJames Chapman } 1717fd558d18SJames Chapman 1718fd558d18SJames Chapman static const struct seq_operations pppol2tp_seq_ops = { 1719fd558d18SJames Chapman .start = pppol2tp_seq_start, 1720fd558d18SJames Chapman .next = pppol2tp_seq_next, 1721fd558d18SJames Chapman .stop = pppol2tp_seq_stop, 1722fd558d18SJames Chapman .show = pppol2tp_seq_show, 1723fd558d18SJames Chapman }; 1724fd558d18SJames Chapman 1725fd558d18SJames Chapman /* Called when our /proc file is opened. We allocate data for use when 1726fd558d18SJames Chapman * iterating our tunnel / session contexts and store it in the private 1727fd558d18SJames Chapman * data of the seq_file. 1728fd558d18SJames Chapman */ 1729fd558d18SJames Chapman static int pppol2tp_proc_open(struct inode *inode, struct file *file) 1730fd558d18SJames Chapman { 1731fd558d18SJames Chapman return seq_open_net(inode, file, &pppol2tp_seq_ops, 1732fd558d18SJames Chapman sizeof(struct pppol2tp_seq_data)); 1733fd558d18SJames Chapman } 1734fd558d18SJames Chapman 1735fd558d18SJames Chapman static const struct file_operations pppol2tp_proc_fops = { 1736fd558d18SJames Chapman .owner = THIS_MODULE, 1737fd558d18SJames Chapman .open = pppol2tp_proc_open, 1738fd558d18SJames Chapman .read = seq_read, 1739fd558d18SJames Chapman .llseek = seq_lseek, 1740fd558d18SJames Chapman .release = seq_release_net, 1741fd558d18SJames Chapman }; 1742fd558d18SJames Chapman 1743fd558d18SJames Chapman #endif /* CONFIG_PROC_FS */ 1744fd558d18SJames Chapman 1745fd558d18SJames Chapman /***************************************************************************** 1746fd558d18SJames Chapman * Network namespace 1747fd558d18SJames Chapman *****************************************************************************/ 1748fd558d18SJames Chapman 1749fd558d18SJames Chapman static __net_init int pppol2tp_init_net(struct net *net) 1750fd558d18SJames Chapman { 1751fd558d18SJames Chapman struct proc_dir_entry *pde; 1752fd558d18SJames Chapman int err = 0; 1753fd558d18SJames Chapman 1754d4beaa66SGao feng pde = proc_create("pppol2tp", S_IRUGO, net->proc_net, 1755d4beaa66SGao feng &pppol2tp_proc_fops); 1756fd558d18SJames Chapman if (!pde) { 1757fd558d18SJames Chapman err = -ENOMEM; 1758fd558d18SJames Chapman goto out; 1759fd558d18SJames Chapman } 1760fd558d18SJames Chapman 1761fd558d18SJames Chapman out: 1762fd558d18SJames Chapman return err; 1763fd558d18SJames Chapman } 1764fd558d18SJames Chapman 1765fd558d18SJames Chapman static __net_exit void pppol2tp_exit_net(struct net *net) 1766fd558d18SJames Chapman { 1767ece31ffdSGao feng remove_proc_entry("pppol2tp", net->proc_net); 1768fd558d18SJames Chapman } 1769fd558d18SJames Chapman 1770fd558d18SJames Chapman static struct pernet_operations pppol2tp_net_ops = { 1771fd558d18SJames Chapman .init = pppol2tp_init_net, 1772fd558d18SJames Chapman .exit = pppol2tp_exit_net, 1773fd558d18SJames Chapman .id = &pppol2tp_net_id, 1774fd558d18SJames Chapman }; 1775fd558d18SJames Chapman 1776fd558d18SJames Chapman /***************************************************************************** 1777fd558d18SJames Chapman * Init and cleanup 1778fd558d18SJames Chapman *****************************************************************************/ 1779fd558d18SJames Chapman 1780fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops = { 1781fd558d18SJames Chapman .family = AF_PPPOX, 1782fd558d18SJames Chapman .owner = THIS_MODULE, 1783fd558d18SJames Chapman .release = pppol2tp_release, 1784fd558d18SJames Chapman .bind = sock_no_bind, 1785fd558d18SJames Chapman .connect = pppol2tp_connect, 1786fd558d18SJames Chapman .socketpair = sock_no_socketpair, 1787fd558d18SJames Chapman .accept = sock_no_accept, 1788fd558d18SJames Chapman .getname = pppol2tp_getname, 1789fd558d18SJames Chapman .poll = datagram_poll, 1790fd558d18SJames Chapman .listen = sock_no_listen, 1791fd558d18SJames Chapman .shutdown = sock_no_shutdown, 1792fd558d18SJames Chapman .setsockopt = pppol2tp_setsockopt, 1793fd558d18SJames Chapman .getsockopt = pppol2tp_getsockopt, 1794fd558d18SJames Chapman .sendmsg = pppol2tp_sendmsg, 1795fd558d18SJames Chapman .recvmsg = pppol2tp_recvmsg, 1796fd558d18SJames Chapman .mmap = sock_no_mmap, 1797fd558d18SJames Chapman .ioctl = pppox_ioctl, 1798fd558d18SJames Chapman }; 1799fd558d18SJames Chapman 1800756e64a0SEric Dumazet static const struct pppox_proto pppol2tp_proto = { 1801fd558d18SJames Chapman .create = pppol2tp_create, 1802fd558d18SJames Chapman .ioctl = pppol2tp_ioctl 1803fd558d18SJames Chapman }; 1804fd558d18SJames Chapman 1805309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 1806309795f4SJames Chapman 1807309795f4SJames Chapman static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = { 1808309795f4SJames Chapman .session_create = pppol2tp_session_create, 1809*cf2f5c88STom Parkin .session_delete = l2tp_session_delete, 1810309795f4SJames Chapman }; 1811309795f4SJames Chapman 1812309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */ 1813309795f4SJames Chapman 1814fd558d18SJames Chapman static int __init pppol2tp_init(void) 1815fd558d18SJames Chapman { 1816fd558d18SJames Chapman int err; 1817fd558d18SJames Chapman 1818fd558d18SJames Chapman err = register_pernet_device(&pppol2tp_net_ops); 1819fd558d18SJames Chapman if (err) 1820fd558d18SJames Chapman goto out; 1821fd558d18SJames Chapman 1822fd558d18SJames Chapman err = proto_register(&pppol2tp_sk_proto, 0); 1823fd558d18SJames Chapman if (err) 1824fd558d18SJames Chapman goto out_unregister_pppol2tp_pernet; 1825fd558d18SJames Chapman 1826fd558d18SJames Chapman err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto); 1827fd558d18SJames Chapman if (err) 1828fd558d18SJames Chapman goto out_unregister_pppol2tp_proto; 1829fd558d18SJames Chapman 1830309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 1831309795f4SJames Chapman err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops); 1832309795f4SJames Chapman if (err) 1833309795f4SJames Chapman goto out_unregister_pppox; 1834309795f4SJames Chapman #endif 1835309795f4SJames Chapman 1836a4ca44faSJoe Perches pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION); 1837fd558d18SJames Chapman 1838fd558d18SJames Chapman out: 1839fd558d18SJames Chapman return err; 1840309795f4SJames Chapman 1841309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 1842309795f4SJames Chapman out_unregister_pppox: 1843309795f4SJames Chapman unregister_pppox_proto(PX_PROTO_OL2TP); 1844309795f4SJames Chapman #endif 1845fd558d18SJames Chapman out_unregister_pppol2tp_proto: 1846fd558d18SJames Chapman proto_unregister(&pppol2tp_sk_proto); 1847fd558d18SJames Chapman out_unregister_pppol2tp_pernet: 1848fd558d18SJames Chapman unregister_pernet_device(&pppol2tp_net_ops); 1849fd558d18SJames Chapman goto out; 1850fd558d18SJames Chapman } 1851fd558d18SJames Chapman 1852fd558d18SJames Chapman static void __exit pppol2tp_exit(void) 1853fd558d18SJames Chapman { 1854309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 1855309795f4SJames Chapman l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP); 1856309795f4SJames Chapman #endif 1857fd558d18SJames Chapman unregister_pppox_proto(PX_PROTO_OL2TP); 1858fd558d18SJames Chapman proto_unregister(&pppol2tp_sk_proto); 1859fd558d18SJames Chapman unregister_pernet_device(&pppol2tp_net_ops); 1860fd558d18SJames Chapman } 1861fd558d18SJames Chapman 1862fd558d18SJames Chapman module_init(pppol2tp_init); 1863fd558d18SJames Chapman module_exit(pppol2tp_exit); 1864fd558d18SJames Chapman 1865fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1866fd558d18SJames Chapman MODULE_DESCRIPTION("PPP over L2TP over UDP"); 1867fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1868fd558d18SJames Chapman MODULE_VERSION(PPPOL2TP_DRV_VERSION); 18699395a09dSBenjamin LaHaise MODULE_ALIAS("pppox-proto-" __stringify(PX_PROTO_OL2TP)); 1870