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> 100cf2f5c88STom 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 18054c151d9SGao Feng if ((skb->data[0] == PPP_ALLSTATIONS) && (skb->data[1] == PPP_UI)) 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 */ 1881b784140SYing Xue static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg, 1891b784140SYing Xue size_t len, int flags) 190fd558d18SJames Chapman { 191fd558d18SJames Chapman int err; 192fd558d18SJames Chapman struct sk_buff *skb; 193fd558d18SJames Chapman struct sock *sk = sock->sk; 194fd558d18SJames Chapman 195fd558d18SJames Chapman err = -EIO; 196fd558d18SJames Chapman if (sk->sk_state & PPPOX_BOUND) 197fd558d18SJames Chapman goto end; 198fd558d18SJames Chapman 199fd558d18SJames Chapman err = 0; 200fd558d18SJames Chapman skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, 201fd558d18SJames Chapman flags & MSG_DONTWAIT, &err); 202fd558d18SJames Chapman if (!skb) 203fd558d18SJames Chapman goto end; 204fd558d18SJames Chapman 205fd558d18SJames Chapman if (len > skb->len) 206fd558d18SJames Chapman len = skb->len; 207fd558d18SJames Chapman else if (len < skb->len) 208fd558d18SJames Chapman msg->msg_flags |= MSG_TRUNC; 209fd558d18SJames Chapman 21051f3d02bSDavid S. Miller err = skb_copy_datagram_msg(skb, 0, msg, len); 211fd558d18SJames Chapman if (likely(err == 0)) 212fd558d18SJames Chapman err = len; 213fd558d18SJames Chapman 214fd558d18SJames Chapman kfree_skb(skb); 215fd558d18SJames Chapman end: 216fd558d18SJames Chapman return err; 217fd558d18SJames Chapman } 218fd558d18SJames Chapman 219fd558d18SJames Chapman static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len) 220fd558d18SJames Chapman { 221fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 222fd558d18SJames Chapman struct sock *sk = NULL; 223fd558d18SJames Chapman 224fd558d18SJames Chapman /* If the socket is bound, send it in to PPP's input queue. Otherwise 225fd558d18SJames Chapman * queue it on the session socket. 226fd558d18SJames Chapman */ 227fd558d18SJames Chapman sk = ps->sock; 228fd558d18SJames Chapman if (sk == NULL) 229fd558d18SJames Chapman goto no_sock; 230fd558d18SJames Chapman 231fd558d18SJames Chapman if (sk->sk_state & PPPOX_BOUND) { 232fd558d18SJames Chapman struct pppox_sock *po; 23398f40b3eSGuillaume Nault 234fba40c63SAsbjørn Sloth Tønnesen l2tp_dbg(session, L2TP_MSG_DATA, 235fd558d18SJames Chapman "%s: recv %d byte data frame, passing to ppp\n", 236fd558d18SJames Chapman session->name, data_len); 237fd558d18SJames Chapman 238fd558d18SJames Chapman po = pppox_sk(sk); 239fd558d18SJames Chapman ppp_input(&po->chan, skb); 240fd558d18SJames Chapman } else { 241fba40c63SAsbjørn Sloth Tønnesen l2tp_dbg(session, L2TP_MSG_DATA, 2429e9cb622SGuillaume Nault "%s: recv %d byte data frame, passing to L2TP socket\n", 2439e9cb622SGuillaume Nault session->name, data_len); 244fd558d18SJames Chapman 2459e9cb622SGuillaume Nault if (sock_queue_rcv_skb(sk, skb) < 0) { 2467b7c0719STom Parkin atomic_long_inc(&session->stats.rx_errors); 247fd558d18SJames Chapman kfree_skb(skb); 248fd558d18SJames Chapman } 2499e9cb622SGuillaume Nault } 250fd558d18SJames Chapman 251fd558d18SJames Chapman return; 252fd558d18SJames Chapman 253fd558d18SJames Chapman no_sock: 254fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name); 255fd558d18SJames Chapman kfree_skb(skb); 256fd558d18SJames Chapman } 257fd558d18SJames Chapman 258fd558d18SJames Chapman static void pppol2tp_session_sock_hold(struct l2tp_session *session) 259fd558d18SJames Chapman { 260fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 261fd558d18SJames Chapman 262fd558d18SJames Chapman if (ps->sock) 263fd558d18SJames Chapman sock_hold(ps->sock); 264fd558d18SJames Chapman } 265fd558d18SJames Chapman 266fd558d18SJames Chapman static void pppol2tp_session_sock_put(struct l2tp_session *session) 267fd558d18SJames Chapman { 268fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 269fd558d18SJames Chapman 270fd558d18SJames Chapman if (ps->sock) 271fd558d18SJames Chapman sock_put(ps->sock); 272fd558d18SJames Chapman } 273fd558d18SJames Chapman 274fd558d18SJames Chapman /************************************************************************ 275fd558d18SJames Chapman * Transmit handling 276fd558d18SJames Chapman ***********************************************************************/ 277fd558d18SJames Chapman 278fd558d18SJames Chapman /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here 279fd558d18SJames Chapman * when a user application does a sendmsg() on the session socket. L2TP and 280fd558d18SJames Chapman * PPP headers must be inserted into the user's data. 281fd558d18SJames Chapman */ 2821b784140SYing Xue static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m, 283fd558d18SJames Chapman size_t total_len) 284fd558d18SJames Chapman { 285fd558d18SJames Chapman struct sock *sk = sock->sk; 286fd558d18SJames Chapman struct sk_buff *skb; 287fd558d18SJames Chapman int error; 288fd558d18SJames Chapman struct l2tp_session *session; 289fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 290fd558d18SJames Chapman struct pppol2tp_session *ps; 2910d76751fSJames Chapman int uhlen; 292fd558d18SJames Chapman 293fd558d18SJames Chapman error = -ENOTCONN; 294fd558d18SJames Chapman if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) 295fd558d18SJames Chapman goto error; 296fd558d18SJames Chapman 297fd558d18SJames Chapman /* Get session and tunnel contexts */ 298fd558d18SJames Chapman error = -EBADF; 299fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 300fd558d18SJames Chapman if (session == NULL) 301fd558d18SJames Chapman goto error; 302fd558d18SJames Chapman 303fd558d18SJames Chapman ps = l2tp_session_priv(session); 304fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock); 305fd558d18SJames Chapman if (tunnel == NULL) 306fd558d18SJames Chapman goto error_put_sess; 307fd558d18SJames Chapman 3080d76751fSJames Chapman uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 3090d76751fSJames Chapman 310fd558d18SJames Chapman /* Allocate a socket buffer */ 311fd558d18SJames Chapman error = -ENOMEM; 312fd558d18SJames Chapman skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) + 3130d76751fSJames Chapman uhlen + session->hdr_len + 31454c151d9SGao Feng 2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */ 315fd558d18SJames Chapman 0, GFP_KERNEL); 316fd558d18SJames Chapman if (!skb) 317fd558d18SJames Chapman goto error_put_sess_tun; 318fd558d18SJames Chapman 319fd558d18SJames Chapman /* Reserve space for headers. */ 320fd558d18SJames Chapman skb_reserve(skb, NET_SKB_PAD); 321fd558d18SJames Chapman skb_reset_network_header(skb); 322fd558d18SJames Chapman skb_reserve(skb, sizeof(struct iphdr)); 323fd558d18SJames Chapman skb_reset_transport_header(skb); 3240d76751fSJames Chapman skb_reserve(skb, uhlen); 325fd558d18SJames Chapman 326fd558d18SJames Chapman /* Add PPP header */ 32754c151d9SGao Feng skb->data[0] = PPP_ALLSTATIONS; 32854c151d9SGao Feng skb->data[1] = PPP_UI; 329fd558d18SJames Chapman skb_put(skb, 2); 330fd558d18SJames Chapman 331fd558d18SJames Chapman /* Copy user data into skb */ 3326ce8e9ceSAl Viro error = memcpy_from_msg(skb_put(skb, total_len), m, total_len); 333fd558d18SJames Chapman if (error < 0) { 334fd558d18SJames Chapman kfree_skb(skb); 335fd558d18SJames Chapman goto error_put_sess_tun; 336fd558d18SJames Chapman } 337fd558d18SJames Chapman 338455cc32bSEric Dumazet local_bh_disable(); 339fd558d18SJames Chapman l2tp_xmit_skb(session, skb, session->hdr_len); 340455cc32bSEric Dumazet local_bh_enable(); 341fd558d18SJames Chapman 342fd558d18SJames Chapman sock_put(ps->tunnel_sock); 3438b82547eSGuillaume Nault sock_put(sk); 344fd558d18SJames Chapman 345a6f79d0fSGuillaume Nault return total_len; 346fd558d18SJames Chapman 347fd558d18SJames Chapman error_put_sess_tun: 348fd558d18SJames Chapman sock_put(ps->tunnel_sock); 349fd558d18SJames Chapman error_put_sess: 350fd558d18SJames Chapman sock_put(sk); 351fd558d18SJames Chapman error: 352fd558d18SJames Chapman return error; 353fd558d18SJames Chapman } 354fd558d18SJames Chapman 355fd558d18SJames Chapman /* Transmit function called by generic PPP driver. Sends PPP frame 356fd558d18SJames Chapman * over PPPoL2TP socket. 357fd558d18SJames Chapman * 358fd558d18SJames Chapman * This is almost the same as pppol2tp_sendmsg(), but rather than 359fd558d18SJames Chapman * being called with a msghdr from userspace, it is called with a skb 360fd558d18SJames Chapman * from the kernel. 361fd558d18SJames Chapman * 362fd558d18SJames Chapman * The supplied skb from ppp doesn't have enough headroom for the 363fd558d18SJames Chapman * insertion of L2TP, UDP and IP headers so we need to allocate more 364fd558d18SJames Chapman * headroom in the skb. This will create a cloned skb. But we must be 365fd558d18SJames Chapman * careful in the error case because the caller will expect to free 366fd558d18SJames Chapman * the skb it supplied, not our cloned skb. So we take care to always 367fd558d18SJames Chapman * leave the original skb unfreed if we return an error. 368fd558d18SJames Chapman */ 369fd558d18SJames Chapman static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb) 370fd558d18SJames Chapman { 371fd558d18SJames Chapman struct sock *sk = (struct sock *) chan->private; 372fd558d18SJames Chapman struct sock *sk_tun; 373fd558d18SJames Chapman struct l2tp_session *session; 374fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 375fd558d18SJames Chapman struct pppol2tp_session *ps; 37609df57caSEric Dumazet int uhlen, headroom; 377fd558d18SJames Chapman 378fd558d18SJames Chapman if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) 379fd558d18SJames Chapman goto abort; 380fd558d18SJames Chapman 381fd558d18SJames Chapman /* Get session and tunnel contexts from the socket */ 382fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 383fd558d18SJames Chapman if (session == NULL) 384fd558d18SJames Chapman goto abort; 385fd558d18SJames Chapman 386fd558d18SJames Chapman ps = l2tp_session_priv(session); 387fd558d18SJames Chapman sk_tun = ps->tunnel_sock; 388fd558d18SJames Chapman if (sk_tun == NULL) 389fd558d18SJames Chapman goto abort_put_sess; 390fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(sk_tun); 391fd558d18SJames Chapman if (tunnel == NULL) 392fd558d18SJames Chapman goto abort_put_sess; 393fd558d18SJames Chapman 39409df57caSEric Dumazet uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 39509df57caSEric Dumazet headroom = NET_SKB_PAD + 39609df57caSEric Dumazet sizeof(struct iphdr) + /* IP header */ 39709df57caSEric Dumazet uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */ 39809df57caSEric Dumazet session->hdr_len + /* L2TP header */ 39954c151d9SGao Feng 2; /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */ 40009df57caSEric Dumazet if (skb_cow_head(skb, headroom)) 401fd558d18SJames Chapman goto abort_put_sess_tun; 402fd558d18SJames Chapman 403fd558d18SJames Chapman /* Setup PPP header */ 40454c151d9SGao Feng __skb_push(skb, 2); 40554c151d9SGao Feng skb->data[0] = PPP_ALLSTATIONS; 40654c151d9SGao Feng skb->data[1] = PPP_UI; 407fd558d18SJames Chapman 408455cc32bSEric Dumazet local_bh_disable(); 409e0d4435fSJames Chapman l2tp_xmit_skb(session, skb, session->hdr_len); 410455cc32bSEric Dumazet local_bh_enable(); 411fd558d18SJames Chapman 412fd558d18SJames Chapman sock_put(sk_tun); 413fd558d18SJames Chapman sock_put(sk); 414fd558d18SJames Chapman return 1; 415fd558d18SJames Chapman 416fd558d18SJames Chapman abort_put_sess_tun: 417fd558d18SJames Chapman sock_put(sk_tun); 418fd558d18SJames Chapman abort_put_sess: 419fd558d18SJames Chapman sock_put(sk); 420fd558d18SJames Chapman abort: 421fd558d18SJames Chapman /* Free the original skb */ 422fd558d18SJames Chapman kfree_skb(skb); 423fd558d18SJames Chapman return 1; 424fd558d18SJames Chapman } 425fd558d18SJames Chapman 426fd558d18SJames Chapman /***************************************************************************** 427fd558d18SJames Chapman * Session (and tunnel control) socket create/destroy. 428fd558d18SJames Chapman *****************************************************************************/ 429fd558d18SJames Chapman 430fd558d18SJames Chapman /* Called by l2tp_core when a session socket is being closed. 431fd558d18SJames Chapman */ 432fd558d18SJames Chapman static void pppol2tp_session_close(struct l2tp_session *session) 433fd558d18SJames Chapman { 434fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 435fd558d18SJames Chapman struct sock *sk = ps->sock; 436cf2f5c88STom Parkin struct socket *sock = sk->sk_socket; 437fd558d18SJames Chapman 438fd558d18SJames Chapman BUG_ON(session->magic != L2TP_SESSION_MAGIC); 439fd558d18SJames Chapman 440cf2f5c88STom Parkin if (sock) { 44154c151d9SGao Feng inet_shutdown(sock, SEND_SHUTDOWN); 442cf2f5c88STom Parkin /* Don't let the session go away before our socket does */ 443cf2f5c88STom Parkin l2tp_session_inc_refcount(session); 444fd558d18SJames Chapman } 445fd558d18SJames Chapman } 446fd558d18SJames Chapman 447fd558d18SJames Chapman /* Really kill the session socket. (Called from sock_put() if 448fd558d18SJames Chapman * refcnt == 0.) 449fd558d18SJames Chapman */ 450fd558d18SJames Chapman static void pppol2tp_session_destruct(struct sock *sk) 451fd558d18SJames Chapman { 452f6e16b29STom Parkin struct l2tp_session *session = sk->sk_user_data; 453*e91793bbSGuillaume Nault 454*e91793bbSGuillaume Nault skb_queue_purge(&sk->sk_receive_queue); 455*e91793bbSGuillaume Nault skb_queue_purge(&sk->sk_write_queue); 456*e91793bbSGuillaume Nault 457f6e16b29STom Parkin if (session) { 458fd558d18SJames Chapman sk->sk_user_data = NULL; 459fd558d18SJames Chapman BUG_ON(session->magic != L2TP_SESSION_MAGIC); 460fd558d18SJames Chapman l2tp_session_dec_refcount(session); 461fd558d18SJames Chapman } 462fd558d18SJames Chapman } 463fd558d18SJames Chapman 464fd558d18SJames Chapman /* Called when the PPPoX socket (session) is closed. 465fd558d18SJames Chapman */ 466fd558d18SJames Chapman static int pppol2tp_release(struct socket *sock) 467fd558d18SJames Chapman { 468fd558d18SJames Chapman struct sock *sk = sock->sk; 469fd558d18SJames Chapman struct l2tp_session *session; 470fd558d18SJames Chapman int error; 471fd558d18SJames Chapman 472fd558d18SJames Chapman if (!sk) 473fd558d18SJames Chapman return 0; 474fd558d18SJames Chapman 475fd558d18SJames Chapman error = -EBADF; 476fd558d18SJames Chapman lock_sock(sk); 477fd558d18SJames Chapman if (sock_flag(sk, SOCK_DEAD) != 0) 478fd558d18SJames Chapman goto error; 479fd558d18SJames Chapman 480fd558d18SJames Chapman pppox_unbind_sock(sk); 481fd558d18SJames Chapman 482fd558d18SJames Chapman /* Signal the death of the socket. */ 483fd558d18SJames Chapman sk->sk_state = PPPOX_DEAD; 484fd558d18SJames Chapman sock_orphan(sk); 485fd558d18SJames Chapman sock->sk = NULL; 486fd558d18SJames Chapman 487fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 488fd558d18SJames Chapman 489fd558d18SJames Chapman /* Purge any queued data */ 490cf2f5c88STom Parkin if (session != NULL) { 491f6e16b29STom Parkin __l2tp_session_unhash(session); 492cf2f5c88STom Parkin l2tp_session_queue_purge(session); 493cf2f5c88STom Parkin sock_put(sk); 494cf2f5c88STom Parkin } 495fd558d18SJames Chapman release_sock(sk); 496fd558d18SJames Chapman 497fd558d18SJames Chapman /* This will delete the session context via 498fd558d18SJames Chapman * pppol2tp_session_destruct() if the socket's refcnt drops to 499fd558d18SJames Chapman * zero. 500fd558d18SJames Chapman */ 501fd558d18SJames Chapman sock_put(sk); 502fd558d18SJames Chapman 503fd558d18SJames Chapman return 0; 504fd558d18SJames Chapman 505fd558d18SJames Chapman error: 506fd558d18SJames Chapman release_sock(sk); 507fd558d18SJames Chapman return error; 508fd558d18SJames Chapman } 509fd558d18SJames Chapman 510fd558d18SJames Chapman static struct proto pppol2tp_sk_proto = { 511fd558d18SJames Chapman .name = "PPPOL2TP", 512fd558d18SJames Chapman .owner = THIS_MODULE, 513fd558d18SJames Chapman .obj_size = sizeof(struct pppox_sock), 514fd558d18SJames Chapman }; 515fd558d18SJames Chapman 516fd558d18SJames Chapman static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb) 517fd558d18SJames Chapman { 518fd558d18SJames Chapman int rc; 519fd558d18SJames Chapman 520fd558d18SJames Chapman rc = l2tp_udp_encap_recv(sk, skb); 521fd558d18SJames Chapman if (rc) 522fd558d18SJames Chapman kfree_skb(skb); 523fd558d18SJames Chapman 524fd558d18SJames Chapman return NET_RX_SUCCESS; 525fd558d18SJames Chapman } 526fd558d18SJames Chapman 527fd558d18SJames Chapman /* socket() handler. Initialize a new struct sock. 528fd558d18SJames Chapman */ 52911aa9c28SEric W. Biederman static int pppol2tp_create(struct net *net, struct socket *sock, int kern) 530fd558d18SJames Chapman { 531fd558d18SJames Chapman int error = -ENOMEM; 532fd558d18SJames Chapman struct sock *sk; 533fd558d18SJames Chapman 53411aa9c28SEric W. Biederman sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern); 535fd558d18SJames Chapman if (!sk) 536fd558d18SJames Chapman goto out; 537fd558d18SJames Chapman 538fd558d18SJames Chapman sock_init_data(sock, sk); 539fd558d18SJames Chapman 540fd558d18SJames Chapman sock->state = SS_UNCONNECTED; 541fd558d18SJames Chapman sock->ops = &pppol2tp_ops; 542fd558d18SJames Chapman 543fd558d18SJames Chapman sk->sk_backlog_rcv = pppol2tp_backlog_recv; 544fd558d18SJames Chapman sk->sk_protocol = PX_PROTO_OL2TP; 545fd558d18SJames Chapman sk->sk_family = PF_PPPOX; 546fd558d18SJames Chapman sk->sk_state = PPPOX_NONE; 547fd558d18SJames Chapman sk->sk_type = SOCK_STREAM; 548fd558d18SJames Chapman sk->sk_destruct = pppol2tp_session_destruct; 549fd558d18SJames Chapman 550fd558d18SJames Chapman error = 0; 551fd558d18SJames Chapman 552fd558d18SJames Chapman out: 553fd558d18SJames Chapman return error; 554fd558d18SJames Chapman } 555fd558d18SJames Chapman 5569dd79945SJavier Martinez Canillas #if IS_ENABLED(CONFIG_L2TP_DEBUGFS) 5570ad66140SJames Chapman static void pppol2tp_show(struct seq_file *m, void *arg) 5580ad66140SJames Chapman { 5590ad66140SJames Chapman struct l2tp_session *session = arg; 5600ad66140SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 5610ad66140SJames Chapman 5620ad66140SJames Chapman if (ps) { 5630ad66140SJames Chapman struct pppox_sock *po = pppox_sk(ps->sock); 5640ad66140SJames Chapman if (po) 5650ad66140SJames Chapman seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan)); 5660ad66140SJames Chapman } 5670ad66140SJames Chapman } 5680ad66140SJames Chapman #endif 5690ad66140SJames Chapman 570fd558d18SJames Chapman /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket 571fd558d18SJames Chapman */ 572fd558d18SJames Chapman static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, 573fd558d18SJames Chapman int sockaddr_len, int flags) 574fd558d18SJames Chapman { 575fd558d18SJames Chapman struct sock *sk = sock->sk; 576fd558d18SJames Chapman struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr; 577fd558d18SJames Chapman struct pppox_sock *po = pppox_sk(sk); 578fd558d18SJames Chapman struct l2tp_session *session = NULL; 579fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 580fd558d18SJames Chapman struct pppol2tp_session *ps; 581fd558d18SJames Chapman struct dst_entry *dst; 582fd558d18SJames Chapman struct l2tp_session_cfg cfg = { 0, }; 583fd558d18SJames Chapman int error = 0; 584e0d4435fSJames Chapman u32 tunnel_id, peer_tunnel_id; 585e0d4435fSJames Chapman u32 session_id, peer_session_id; 586e0d4435fSJames Chapman int ver = 2; 587e0d4435fSJames Chapman int fd; 588fd558d18SJames Chapman 589fd558d18SJames Chapman lock_sock(sk); 590fd558d18SJames Chapman 591fd558d18SJames Chapman error = -EINVAL; 592fd558d18SJames Chapman if (sp->sa_protocol != PX_PROTO_OL2TP) 593fd558d18SJames Chapman goto end; 594fd558d18SJames Chapman 595fd558d18SJames Chapman /* Check for already bound sockets */ 596fd558d18SJames Chapman error = -EBUSY; 597fd558d18SJames Chapman if (sk->sk_state & PPPOX_CONNECTED) 598fd558d18SJames Chapman goto end; 599fd558d18SJames Chapman 600fd558d18SJames Chapman /* We don't supporting rebinding anyway */ 601fd558d18SJames Chapman error = -EALREADY; 602fd558d18SJames Chapman if (sk->sk_user_data) 603fd558d18SJames Chapman goto end; /* socket is already attached */ 604fd558d18SJames Chapman 605b79585f5SJames Chapman /* Get params from socket address. Handle L2TPv2 and L2TPv3. 606b79585f5SJames Chapman * This is nasty because there are different sockaddr_pppol2tp 607b79585f5SJames Chapman * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use 608b79585f5SJames Chapman * the sockaddr size to determine which structure the caller 609b79585f5SJames Chapman * is using. 610b79585f5SJames Chapman */ 611b79585f5SJames Chapman peer_tunnel_id = 0; 612e0d4435fSJames Chapman if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) { 613e0d4435fSJames Chapman fd = sp->pppol2tp.fd; 614e0d4435fSJames Chapman tunnel_id = sp->pppol2tp.s_tunnel; 615e0d4435fSJames Chapman peer_tunnel_id = sp->pppol2tp.d_tunnel; 616e0d4435fSJames Chapman session_id = sp->pppol2tp.s_session; 617e0d4435fSJames Chapman peer_session_id = sp->pppol2tp.d_session; 618e0d4435fSJames Chapman } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) { 619b79585f5SJames Chapman struct sockaddr_pppol2tpv3 *sp3 = 620b79585f5SJames Chapman (struct sockaddr_pppol2tpv3 *) sp; 621e0d4435fSJames Chapman ver = 3; 622e0d4435fSJames Chapman fd = sp3->pppol2tp.fd; 623e0d4435fSJames Chapman tunnel_id = sp3->pppol2tp.s_tunnel; 624e0d4435fSJames Chapman peer_tunnel_id = sp3->pppol2tp.d_tunnel; 625e0d4435fSJames Chapman session_id = sp3->pppol2tp.s_session; 626e0d4435fSJames Chapman peer_session_id = sp3->pppol2tp.d_session; 627b79585f5SJames Chapman } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) { 628b79585f5SJames Chapman struct sockaddr_pppol2tpin6 *sp6 = 629b79585f5SJames Chapman (struct sockaddr_pppol2tpin6 *) sp; 630b79585f5SJames Chapman fd = sp6->pppol2tp.fd; 631b79585f5SJames Chapman tunnel_id = sp6->pppol2tp.s_tunnel; 632b79585f5SJames Chapman peer_tunnel_id = sp6->pppol2tp.d_tunnel; 633b79585f5SJames Chapman session_id = sp6->pppol2tp.s_session; 634b79585f5SJames Chapman peer_session_id = sp6->pppol2tp.d_session; 635b79585f5SJames Chapman } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) { 636b79585f5SJames Chapman struct sockaddr_pppol2tpv3in6 *sp6 = 637b79585f5SJames Chapman (struct sockaddr_pppol2tpv3in6 *) sp; 638b79585f5SJames Chapman ver = 3; 639b79585f5SJames Chapman fd = sp6->pppol2tp.fd; 640b79585f5SJames Chapman tunnel_id = sp6->pppol2tp.s_tunnel; 641b79585f5SJames Chapman peer_tunnel_id = sp6->pppol2tp.d_tunnel; 642b79585f5SJames Chapman session_id = sp6->pppol2tp.s_session; 643b79585f5SJames Chapman peer_session_id = sp6->pppol2tp.d_session; 644e0d4435fSJames Chapman } else { 645fd558d18SJames Chapman error = -EINVAL; 646e0d4435fSJames Chapman goto end; /* bad socket address */ 647e0d4435fSJames Chapman } 648e0d4435fSJames Chapman 649e0d4435fSJames Chapman /* Don't bind if tunnel_id is 0 */ 650e0d4435fSJames Chapman error = -EINVAL; 651e0d4435fSJames Chapman if (tunnel_id == 0) 652fd558d18SJames Chapman goto end; 653fd558d18SJames Chapman 654309795f4SJames Chapman tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id); 655309795f4SJames Chapman 656e0d4435fSJames Chapman /* Special case: create tunnel context if session_id and 657e0d4435fSJames Chapman * peer_session_id is 0. Otherwise look up tunnel using supplied 658fd558d18SJames Chapman * tunnel id. 659fd558d18SJames Chapman */ 660e0d4435fSJames Chapman if ((session_id == 0) && (peer_session_id == 0)) { 661309795f4SJames Chapman if (tunnel == NULL) { 662309795f4SJames Chapman struct l2tp_tunnel_cfg tcfg = { 663309795f4SJames Chapman .encap = L2TP_ENCAPTYPE_UDP, 664309795f4SJames Chapman .debug = 0, 665309795f4SJames Chapman }; 666309795f4SJames Chapman error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel); 667fd558d18SJames Chapman if (error < 0) 668fd558d18SJames Chapman goto end; 669309795f4SJames Chapman } 670fd558d18SJames Chapman } else { 671fd558d18SJames Chapman /* Error if we can't find the tunnel */ 672fd558d18SJames Chapman error = -ENOENT; 673fd558d18SJames Chapman if (tunnel == NULL) 674fd558d18SJames Chapman goto end; 675fd558d18SJames Chapman 676fd558d18SJames Chapman /* Error if socket is not prepped */ 677fd558d18SJames Chapman if (tunnel->sock == NULL) 678fd558d18SJames Chapman goto end; 679fd558d18SJames Chapman } 680fd558d18SJames Chapman 681fd558d18SJames Chapman if (tunnel->recv_payload_hook == NULL) 682fd558d18SJames Chapman tunnel->recv_payload_hook = pppol2tp_recv_payload_hook; 683fd558d18SJames Chapman 684b79585f5SJames Chapman if (tunnel->peer_tunnel_id == 0) 685b79585f5SJames Chapman tunnel->peer_tunnel_id = peer_tunnel_id; 686fd558d18SJames Chapman 687309795f4SJames Chapman /* Create session if it doesn't already exist. We handle the 688309795f4SJames Chapman * case where a session was previously created by the netlink 689309795f4SJames Chapman * interface by checking that the session doesn't already have 690309795f4SJames Chapman * a socket and its tunnel socket are what we expect. If any 691309795f4SJames Chapman * of those checks fail, return EEXIST to the caller. 692309795f4SJames Chapman */ 693309795f4SJames Chapman session = l2tp_session_find(sock_net(sk), tunnel, session_id); 694309795f4SJames Chapman if (session == NULL) { 695309795f4SJames Chapman /* Default MTU must allow space for UDP/L2TP/PPP 696309795f4SJames Chapman * headers. 697309795f4SJames Chapman */ 698309795f4SJames Chapman cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD; 699fd558d18SJames Chapman 700fd558d18SJames Chapman /* Allocate and initialize a new session context. */ 701fd558d18SJames Chapman session = l2tp_session_create(sizeof(struct pppol2tp_session), 702e0d4435fSJames Chapman tunnel, session_id, 703e0d4435fSJames Chapman peer_session_id, &cfg); 704fd558d18SJames Chapman if (session == NULL) { 705fd558d18SJames Chapman error = -ENOMEM; 706fd558d18SJames Chapman goto end; 707fd558d18SJames Chapman } 708309795f4SJames Chapman } else { 709309795f4SJames Chapman ps = l2tp_session_priv(session); 710309795f4SJames Chapman error = -EEXIST; 711309795f4SJames Chapman if (ps->sock != NULL) 712309795f4SJames Chapman goto end; 713fd558d18SJames Chapman 714309795f4SJames Chapman /* consistency checks */ 715309795f4SJames Chapman if (ps->tunnel_sock != tunnel->sock) 716309795f4SJames Chapman goto end; 717309795f4SJames Chapman } 718309795f4SJames Chapman 719309795f4SJames Chapman /* Associate session with its PPPoL2TP socket */ 720fd558d18SJames Chapman ps = l2tp_session_priv(session); 721fd558d18SJames Chapman ps->owner = current->pid; 722fd558d18SJames Chapman ps->sock = sk; 723fd558d18SJames Chapman ps->tunnel_sock = tunnel->sock; 724fd558d18SJames Chapman 725fd558d18SJames Chapman session->recv_skb = pppol2tp_recv; 726fd558d18SJames Chapman session->session_close = pppol2tp_session_close; 7279dd79945SJavier Martinez Canillas #if IS_ENABLED(CONFIG_L2TP_DEBUGFS) 7280ad66140SJames Chapman session->show = pppol2tp_show; 7290ad66140SJames Chapman #endif 730fd558d18SJames Chapman 731fd558d18SJames Chapman /* We need to know each time a skb is dropped from the reorder 732fd558d18SJames Chapman * queue. 733fd558d18SJames Chapman */ 734fd558d18SJames Chapman session->ref = pppol2tp_session_sock_hold; 735fd558d18SJames Chapman session->deref = pppol2tp_session_sock_put; 736fd558d18SJames Chapman 737fd558d18SJames Chapman /* If PMTU discovery was enabled, use the MTU that was discovered */ 738f34c4a35SDmitry Petukhov dst = sk_dst_get(tunnel->sock); 739fd558d18SJames Chapman if (dst != NULL) { 740eed4d839SGuillaume Nault u32 pmtu = dst_mtu(dst); 741eed4d839SGuillaume Nault 742fd558d18SJames Chapman if (pmtu != 0) 743fd558d18SJames Chapman session->mtu = session->mru = pmtu - 744fd558d18SJames Chapman PPPOL2TP_HEADER_OVERHEAD; 745fd558d18SJames Chapman dst_release(dst); 746fd558d18SJames Chapman } 747fd558d18SJames Chapman 748fd558d18SJames Chapman /* Special case: if source & dest session_id == 0x0000, this 749fd558d18SJames Chapman * socket is being created to manage the tunnel. Just set up 750fd558d18SJames Chapman * the internal context for use by ioctl() and sockopt() 751fd558d18SJames Chapman * handlers. 752fd558d18SJames Chapman */ 753fd558d18SJames Chapman if ((session->session_id == 0) && 754fd558d18SJames Chapman (session->peer_session_id == 0)) { 755fd558d18SJames Chapman error = 0; 756fd558d18SJames Chapman goto out_no_ppp; 757fd558d18SJames Chapman } 758fd558d18SJames Chapman 759fd558d18SJames Chapman /* The only header we need to worry about is the L2TP 760fd558d18SJames Chapman * header. This size is different depending on whether 761fd558d18SJames Chapman * sequence numbers are enabled for the data channel. 762fd558d18SJames Chapman */ 763fd558d18SJames Chapman po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; 764fd558d18SJames Chapman 765fd558d18SJames Chapman po->chan.private = sk; 766fd558d18SJames Chapman po->chan.ops = &pppol2tp_chan_ops; 767fd558d18SJames Chapman po->chan.mtu = session->mtu; 768fd558d18SJames Chapman 769fd558d18SJames Chapman error = ppp_register_net_channel(sock_net(sk), &po->chan); 770fd558d18SJames Chapman if (error) 771fd558d18SJames Chapman goto end; 772fd558d18SJames Chapman 773fd558d18SJames Chapman out_no_ppp: 774fd558d18SJames Chapman /* This is how we get the session context from the socket. */ 775fd558d18SJames Chapman sk->sk_user_data = session; 776fd558d18SJames Chapman sk->sk_state = PPPOX_CONNECTED; 777fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n", 778a4ca44faSJoe Perches session->name); 779fd558d18SJames Chapman 780fd558d18SJames Chapman end: 781fd558d18SJames Chapman release_sock(sk); 782fd558d18SJames Chapman 783fd558d18SJames Chapman return error; 784fd558d18SJames Chapman } 785fd558d18SJames Chapman 786309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 787309795f4SJames Chapman 788309795f4SJames Chapman /* Called when creating sessions via the netlink interface. 789309795f4SJames Chapman */ 790309795f4SJames Chapman static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) 791309795f4SJames Chapman { 792309795f4SJames Chapman int error; 793309795f4SJames Chapman struct l2tp_tunnel *tunnel; 794309795f4SJames Chapman struct l2tp_session *session; 795309795f4SJames Chapman struct pppol2tp_session *ps; 796309795f4SJames Chapman 797309795f4SJames Chapman tunnel = l2tp_tunnel_find(net, tunnel_id); 798309795f4SJames Chapman 799309795f4SJames Chapman /* Error if we can't find the tunnel */ 800309795f4SJames Chapman error = -ENOENT; 801309795f4SJames Chapman if (tunnel == NULL) 802309795f4SJames Chapman goto out; 803309795f4SJames Chapman 804309795f4SJames Chapman /* Error if tunnel socket is not prepped */ 805309795f4SJames Chapman if (tunnel->sock == NULL) 806309795f4SJames Chapman goto out; 807309795f4SJames Chapman 808309795f4SJames Chapman /* Check that this session doesn't already exist */ 809309795f4SJames Chapman error = -EEXIST; 810309795f4SJames Chapman session = l2tp_session_find(net, tunnel, session_id); 811309795f4SJames Chapman if (session != NULL) 812309795f4SJames Chapman goto out; 813309795f4SJames Chapman 814309795f4SJames Chapman /* Default MTU values. */ 815309795f4SJames Chapman if (cfg->mtu == 0) 816309795f4SJames Chapman cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD; 817309795f4SJames Chapman if (cfg->mru == 0) 818309795f4SJames Chapman cfg->mru = cfg->mtu; 819309795f4SJames Chapman 820309795f4SJames Chapman /* Allocate and initialize a new session context. */ 821309795f4SJames Chapman error = -ENOMEM; 822309795f4SJames Chapman session = l2tp_session_create(sizeof(struct pppol2tp_session), 823309795f4SJames Chapman tunnel, session_id, 824309795f4SJames Chapman peer_session_id, cfg); 825309795f4SJames Chapman if (session == NULL) 826309795f4SJames Chapman goto out; 827309795f4SJames Chapman 828309795f4SJames Chapman ps = l2tp_session_priv(session); 829309795f4SJames Chapman ps->tunnel_sock = tunnel->sock; 830309795f4SJames Chapman 831fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n", 832a4ca44faSJoe Perches session->name); 833309795f4SJames Chapman 834309795f4SJames Chapman error = 0; 835309795f4SJames Chapman 836309795f4SJames Chapman out: 837309795f4SJames Chapman return error; 838309795f4SJames Chapman } 839309795f4SJames Chapman 840309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */ 841309795f4SJames Chapman 842fd558d18SJames Chapman /* getname() support. 843fd558d18SJames Chapman */ 844fd558d18SJames Chapman static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr, 845fd558d18SJames Chapman int *usockaddr_len, int peer) 846fd558d18SJames Chapman { 847e0d4435fSJames Chapman int len = 0; 848fd558d18SJames Chapman int error = 0; 849fd558d18SJames Chapman struct l2tp_session *session; 850fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 851fd558d18SJames Chapman struct sock *sk = sock->sk; 852fd558d18SJames Chapman struct inet_sock *inet; 853fd558d18SJames Chapman struct pppol2tp_session *pls; 854fd558d18SJames Chapman 855fd558d18SJames Chapman error = -ENOTCONN; 856fd558d18SJames Chapman if (sk == NULL) 857fd558d18SJames Chapman goto end; 85856cff471SGao Feng if (!(sk->sk_state & PPPOX_CONNECTED)) 859fd558d18SJames Chapman goto end; 860fd558d18SJames Chapman 861fd558d18SJames Chapman error = -EBADF; 862fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 863fd558d18SJames Chapman if (session == NULL) 864fd558d18SJames Chapman goto end; 865fd558d18SJames Chapman 866fd558d18SJames Chapman pls = l2tp_session_priv(session); 867fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock); 8684ac36a4aSphil.turnbull@oracle.com if (tunnel == NULL) 869fd558d18SJames Chapman goto end_put_sess; 870fd558d18SJames Chapman 871bbdb32cbSBenjamin LaHaise inet = inet_sk(tunnel->sock); 872d2cf3361SBenjamin LaHaise if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) { 873e0d4435fSJames Chapman struct sockaddr_pppol2tp sp; 874e0d4435fSJames Chapman len = sizeof(sp); 875fd558d18SJames Chapman memset(&sp, 0, len); 876fd558d18SJames Chapman sp.sa_family = AF_PPPOX; 877fd558d18SJames Chapman sp.sa_protocol = PX_PROTO_OL2TP; 878fd558d18SJames Chapman sp.pppol2tp.fd = tunnel->fd; 879fd558d18SJames Chapman sp.pppol2tp.pid = pls->owner; 880fd558d18SJames Chapman sp.pppol2tp.s_tunnel = tunnel->tunnel_id; 881fd558d18SJames Chapman sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; 882fd558d18SJames Chapman sp.pppol2tp.s_session = session->session_id; 883fd558d18SJames Chapman sp.pppol2tp.d_session = session->peer_session_id; 884fd558d18SJames Chapman sp.pppol2tp.addr.sin_family = AF_INET; 885fd558d18SJames Chapman sp.pppol2tp.addr.sin_port = inet->inet_dport; 886fd558d18SJames Chapman sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr; 887fd558d18SJames Chapman memcpy(uaddr, &sp, len); 888d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6) 889d2cf3361SBenjamin LaHaise } else if ((tunnel->version == 2) && 890d2cf3361SBenjamin LaHaise (tunnel->sock->sk_family == AF_INET6)) { 891d2cf3361SBenjamin LaHaise struct sockaddr_pppol2tpin6 sp; 892efe4208fSEric Dumazet 893d2cf3361SBenjamin LaHaise len = sizeof(sp); 894d2cf3361SBenjamin LaHaise memset(&sp, 0, len); 895d2cf3361SBenjamin LaHaise sp.sa_family = AF_PPPOX; 896d2cf3361SBenjamin LaHaise sp.sa_protocol = PX_PROTO_OL2TP; 897d2cf3361SBenjamin LaHaise sp.pppol2tp.fd = tunnel->fd; 898d2cf3361SBenjamin LaHaise sp.pppol2tp.pid = pls->owner; 899d2cf3361SBenjamin LaHaise sp.pppol2tp.s_tunnel = tunnel->tunnel_id; 900d2cf3361SBenjamin LaHaise sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; 901d2cf3361SBenjamin LaHaise sp.pppol2tp.s_session = session->session_id; 902d2cf3361SBenjamin LaHaise sp.pppol2tp.d_session = session->peer_session_id; 903d2cf3361SBenjamin LaHaise sp.pppol2tp.addr.sin6_family = AF_INET6; 904d2cf3361SBenjamin LaHaise sp.pppol2tp.addr.sin6_port = inet->inet_dport; 905efe4208fSEric Dumazet memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr, 906efe4208fSEric Dumazet sizeof(tunnel->sock->sk_v6_daddr)); 907d2cf3361SBenjamin LaHaise memcpy(uaddr, &sp, len); 908d2cf3361SBenjamin LaHaise } else if ((tunnel->version == 3) && 909d2cf3361SBenjamin LaHaise (tunnel->sock->sk_family == AF_INET6)) { 910d2cf3361SBenjamin LaHaise struct sockaddr_pppol2tpv3in6 sp; 911efe4208fSEric Dumazet 912d2cf3361SBenjamin LaHaise len = sizeof(sp); 913d2cf3361SBenjamin LaHaise memset(&sp, 0, len); 914d2cf3361SBenjamin LaHaise sp.sa_family = AF_PPPOX; 915d2cf3361SBenjamin LaHaise sp.sa_protocol = PX_PROTO_OL2TP; 916d2cf3361SBenjamin LaHaise sp.pppol2tp.fd = tunnel->fd; 917d2cf3361SBenjamin LaHaise sp.pppol2tp.pid = pls->owner; 918d2cf3361SBenjamin LaHaise sp.pppol2tp.s_tunnel = tunnel->tunnel_id; 919d2cf3361SBenjamin LaHaise sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; 920d2cf3361SBenjamin LaHaise sp.pppol2tp.s_session = session->session_id; 921d2cf3361SBenjamin LaHaise sp.pppol2tp.d_session = session->peer_session_id; 922d2cf3361SBenjamin LaHaise sp.pppol2tp.addr.sin6_family = AF_INET6; 923d2cf3361SBenjamin LaHaise sp.pppol2tp.addr.sin6_port = inet->inet_dport; 924efe4208fSEric Dumazet memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr, 925efe4208fSEric Dumazet sizeof(tunnel->sock->sk_v6_daddr)); 926d2cf3361SBenjamin LaHaise memcpy(uaddr, &sp, len); 927d2cf3361SBenjamin LaHaise #endif 928e0d4435fSJames Chapman } else if (tunnel->version == 3) { 929e0d4435fSJames Chapman struct sockaddr_pppol2tpv3 sp; 930e0d4435fSJames Chapman len = sizeof(sp); 931e0d4435fSJames Chapman memset(&sp, 0, len); 932e0d4435fSJames Chapman sp.sa_family = AF_PPPOX; 933e0d4435fSJames Chapman sp.sa_protocol = PX_PROTO_OL2TP; 934e0d4435fSJames Chapman sp.pppol2tp.fd = tunnel->fd; 935e0d4435fSJames Chapman sp.pppol2tp.pid = pls->owner; 936e0d4435fSJames Chapman sp.pppol2tp.s_tunnel = tunnel->tunnel_id; 937e0d4435fSJames Chapman sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; 938e0d4435fSJames Chapman sp.pppol2tp.s_session = session->session_id; 939e0d4435fSJames Chapman sp.pppol2tp.d_session = session->peer_session_id; 940e0d4435fSJames Chapman sp.pppol2tp.addr.sin_family = AF_INET; 941e0d4435fSJames Chapman sp.pppol2tp.addr.sin_port = inet->inet_dport; 942e0d4435fSJames Chapman sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr; 943e0d4435fSJames Chapman memcpy(uaddr, &sp, len); 944e0d4435fSJames Chapman } 945fd558d18SJames Chapman 946fd558d18SJames Chapman *usockaddr_len = len; 9474ac36a4aSphil.turnbull@oracle.com error = 0; 948fd558d18SJames Chapman 949fd558d18SJames Chapman sock_put(pls->tunnel_sock); 950fd558d18SJames Chapman end_put_sess: 951fd558d18SJames Chapman sock_put(sk); 952fd558d18SJames Chapman end: 953fd558d18SJames Chapman return error; 954fd558d18SJames Chapman } 955fd558d18SJames Chapman 956fd558d18SJames Chapman /**************************************************************************** 957fd558d18SJames Chapman * ioctl() handlers. 958fd558d18SJames Chapman * 959fd558d18SJames Chapman * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP 960fd558d18SJames Chapman * sockets. However, in order to control kernel tunnel features, we allow 961fd558d18SJames Chapman * userspace to create a special "tunnel" PPPoX socket which is used for 962fd558d18SJames Chapman * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow 963fd558d18SJames Chapman * the user application to issue L2TP setsockopt(), getsockopt() and ioctl() 964fd558d18SJames Chapman * calls. 965fd558d18SJames Chapman ****************************************************************************/ 966fd558d18SJames Chapman 967fd558d18SJames Chapman static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest, 968fd558d18SJames Chapman struct l2tp_stats *stats) 969fd558d18SJames Chapman { 9707b7c0719STom Parkin dest->tx_packets = atomic_long_read(&stats->tx_packets); 9717b7c0719STom Parkin dest->tx_bytes = atomic_long_read(&stats->tx_bytes); 9727b7c0719STom Parkin dest->tx_errors = atomic_long_read(&stats->tx_errors); 9737b7c0719STom Parkin dest->rx_packets = atomic_long_read(&stats->rx_packets); 9747b7c0719STom Parkin dest->rx_bytes = atomic_long_read(&stats->rx_bytes); 9757b7c0719STom Parkin dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards); 9767b7c0719STom Parkin dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets); 9777b7c0719STom Parkin dest->rx_errors = atomic_long_read(&stats->rx_errors); 978fd558d18SJames Chapman } 979fd558d18SJames Chapman 980fd558d18SJames Chapman /* Session ioctl helper. 981fd558d18SJames Chapman */ 982fd558d18SJames Chapman static int pppol2tp_session_ioctl(struct l2tp_session *session, 983fd558d18SJames Chapman unsigned int cmd, unsigned long arg) 984fd558d18SJames Chapman { 985fd558d18SJames Chapman struct ifreq ifr; 986fd558d18SJames Chapman int err = 0; 987fd558d18SJames Chapman struct sock *sk; 988fd558d18SJames Chapman int val = (int) arg; 989fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 990fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 991fd558d18SJames Chapman struct pppol2tp_ioc_stats stats; 992fd558d18SJames Chapman 993fba40c63SAsbjørn Sloth Tønnesen l2tp_dbg(session, L2TP_MSG_CONTROL, 994fd558d18SJames Chapman "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n", 995fd558d18SJames Chapman session->name, cmd, arg); 996fd558d18SJames Chapman 997fd558d18SJames Chapman sk = ps->sock; 998fd558d18SJames Chapman sock_hold(sk); 999fd558d18SJames Chapman 1000fd558d18SJames Chapman switch (cmd) { 1001fd558d18SJames Chapman case SIOCGIFMTU: 1002fd558d18SJames Chapman err = -ENXIO; 1003fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1004fd558d18SJames Chapman break; 1005fd558d18SJames Chapman 1006fd558d18SJames Chapman err = -EFAULT; 1007fd558d18SJames Chapman if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq))) 1008fd558d18SJames Chapman break; 1009fd558d18SJames Chapman ifr.ifr_mtu = session->mtu; 1010fd558d18SJames Chapman if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq))) 1011fd558d18SJames Chapman break; 1012fd558d18SJames Chapman 1013fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mtu=%d\n", 1014a4ca44faSJoe Perches session->name, session->mtu); 1015fd558d18SJames Chapman err = 0; 1016fd558d18SJames Chapman break; 1017fd558d18SJames Chapman 1018fd558d18SJames Chapman case SIOCSIFMTU: 1019fd558d18SJames Chapman err = -ENXIO; 1020fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1021fd558d18SJames Chapman break; 1022fd558d18SJames Chapman 1023fd558d18SJames Chapman err = -EFAULT; 1024fd558d18SJames Chapman if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq))) 1025fd558d18SJames Chapman break; 1026fd558d18SJames Chapman 1027fd558d18SJames Chapman session->mtu = ifr.ifr_mtu; 1028fd558d18SJames Chapman 1029fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mtu=%d\n", 1030a4ca44faSJoe Perches session->name, session->mtu); 1031fd558d18SJames Chapman err = 0; 1032fd558d18SJames Chapman break; 1033fd558d18SJames Chapman 1034fd558d18SJames Chapman case PPPIOCGMRU: 1035fd558d18SJames Chapman err = -ENXIO; 1036fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1037fd558d18SJames Chapman break; 1038fd558d18SJames Chapman 1039fd558d18SJames Chapman err = -EFAULT; 1040fd558d18SJames Chapman if (put_user(session->mru, (int __user *) arg)) 1041fd558d18SJames Chapman break; 1042fd558d18SJames Chapman 1043fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: get mru=%d\n", 1044a4ca44faSJoe Perches session->name, session->mru); 1045fd558d18SJames Chapman err = 0; 1046fd558d18SJames Chapman break; 1047fd558d18SJames Chapman 1048fd558d18SJames Chapman case PPPIOCSMRU: 1049fd558d18SJames Chapman err = -ENXIO; 1050fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1051fd558d18SJames Chapman break; 1052fd558d18SJames Chapman 1053fd558d18SJames Chapman err = -EFAULT; 1054fd558d18SJames Chapman if (get_user(val, (int __user *) arg)) 1055fd558d18SJames Chapman break; 1056fd558d18SJames Chapman 1057fd558d18SJames Chapman session->mru = val; 1058fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: set mru=%d\n", 1059a4ca44faSJoe Perches session->name, session->mru); 1060fd558d18SJames Chapman err = 0; 1061fd558d18SJames Chapman break; 1062fd558d18SJames Chapman 1063fd558d18SJames Chapman case PPPIOCGFLAGS: 1064fd558d18SJames Chapman err = -EFAULT; 1065fd558d18SJames Chapman if (put_user(ps->flags, (int __user *) arg)) 1066fd558d18SJames Chapman break; 1067fd558d18SJames Chapman 1068fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: get flags=%d\n", 1069a4ca44faSJoe Perches session->name, ps->flags); 1070fd558d18SJames Chapman err = 0; 1071fd558d18SJames Chapman break; 1072fd558d18SJames Chapman 1073fd558d18SJames Chapman case PPPIOCSFLAGS: 1074fd558d18SJames Chapman err = -EFAULT; 1075fd558d18SJames Chapman if (get_user(val, (int __user *) arg)) 1076fd558d18SJames Chapman break; 1077fd558d18SJames Chapman ps->flags = val; 1078fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: set flags=%d\n", 1079a4ca44faSJoe Perches session->name, ps->flags); 1080fd558d18SJames Chapman err = 0; 1081fd558d18SJames Chapman break; 1082fd558d18SJames Chapman 1083fd558d18SJames Chapman case PPPIOCGL2TPSTATS: 1084fd558d18SJames Chapman err = -ENXIO; 1085fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1086fd558d18SJames Chapman break; 1087fd558d18SJames Chapman 1088fd558d18SJames Chapman memset(&stats, 0, sizeof(stats)); 1089fd558d18SJames Chapman stats.tunnel_id = tunnel->tunnel_id; 1090fd558d18SJames Chapman stats.session_id = session->session_id; 1091fd558d18SJames Chapman pppol2tp_copy_stats(&stats, &session->stats); 1092fd558d18SJames Chapman if (copy_to_user((void __user *) arg, &stats, 1093fd558d18SJames Chapman sizeof(stats))) 1094fd558d18SJames Chapman break; 1095fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: get L2TP stats\n", 1096a4ca44faSJoe Perches session->name); 1097fd558d18SJames Chapman err = 0; 1098fd558d18SJames Chapman break; 1099fd558d18SJames Chapman 1100fd558d18SJames Chapman default: 1101fd558d18SJames Chapman err = -ENOSYS; 1102fd558d18SJames Chapman break; 1103fd558d18SJames Chapman } 1104fd558d18SJames Chapman 1105fd558d18SJames Chapman sock_put(sk); 1106fd558d18SJames Chapman 1107fd558d18SJames Chapman return err; 1108fd558d18SJames Chapman } 1109fd558d18SJames Chapman 1110fd558d18SJames Chapman /* Tunnel ioctl helper. 1111fd558d18SJames Chapman * 1112fd558d18SJames Chapman * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data 1113fd558d18SJames Chapman * specifies a session_id, the session ioctl handler is called. This allows an 1114fd558d18SJames Chapman * application to retrieve session stats via a tunnel socket. 1115fd558d18SJames Chapman */ 1116fd558d18SJames Chapman static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel, 1117fd558d18SJames Chapman unsigned int cmd, unsigned long arg) 1118fd558d18SJames Chapman { 1119fd558d18SJames Chapman int err = 0; 1120fd558d18SJames Chapman struct sock *sk; 1121fd558d18SJames Chapman struct pppol2tp_ioc_stats stats; 1122fd558d18SJames Chapman 1123fba40c63SAsbjørn Sloth Tønnesen l2tp_dbg(tunnel, L2TP_MSG_CONTROL, 1124fd558d18SJames Chapman "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", 1125fd558d18SJames Chapman tunnel->name, cmd, arg); 1126fd558d18SJames Chapman 1127fd558d18SJames Chapman sk = tunnel->sock; 1128fd558d18SJames Chapman sock_hold(sk); 1129fd558d18SJames Chapman 1130fd558d18SJames Chapman switch (cmd) { 1131fd558d18SJames Chapman case PPPIOCGL2TPSTATS: 1132fd558d18SJames Chapman err = -ENXIO; 1133fd558d18SJames Chapman if (!(sk->sk_state & PPPOX_CONNECTED)) 1134fd558d18SJames Chapman break; 1135fd558d18SJames Chapman 1136fd558d18SJames Chapman if (copy_from_user(&stats, (void __user *) arg, 1137fd558d18SJames Chapman sizeof(stats))) { 1138fd558d18SJames Chapman err = -EFAULT; 1139fd558d18SJames Chapman break; 1140fd558d18SJames Chapman } 1141fd558d18SJames Chapman if (stats.session_id != 0) { 1142fd558d18SJames Chapman /* resend to session ioctl handler */ 1143fd558d18SJames Chapman struct l2tp_session *session = 1144f7faffa3SJames Chapman l2tp_session_find(sock_net(sk), tunnel, stats.session_id); 1145fd558d18SJames Chapman if (session != NULL) 1146fd558d18SJames Chapman err = pppol2tp_session_ioctl(session, cmd, arg); 1147fd558d18SJames Chapman else 1148fd558d18SJames Chapman err = -EBADR; 1149fd558d18SJames Chapman break; 1150fd558d18SJames Chapman } 1151fd558d18SJames Chapman #ifdef CONFIG_XFRM 1152fd558d18SJames Chapman stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0; 1153fd558d18SJames Chapman #endif 1154fd558d18SJames Chapman pppol2tp_copy_stats(&stats, &tunnel->stats); 1155fd558d18SJames Chapman if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) { 1156fd558d18SJames Chapman err = -EFAULT; 1157fd558d18SJames Chapman break; 1158fd558d18SJames Chapman } 1159fba40c63SAsbjørn Sloth Tønnesen l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get L2TP stats\n", 1160a4ca44faSJoe Perches tunnel->name); 1161fd558d18SJames Chapman err = 0; 1162fd558d18SJames Chapman break; 1163fd558d18SJames Chapman 1164fd558d18SJames Chapman default: 1165fd558d18SJames Chapman err = -ENOSYS; 1166fd558d18SJames Chapman break; 1167fd558d18SJames Chapman } 1168fd558d18SJames Chapman 1169fd558d18SJames Chapman sock_put(sk); 1170fd558d18SJames Chapman 1171fd558d18SJames Chapman return err; 1172fd558d18SJames Chapman } 1173fd558d18SJames Chapman 1174fd558d18SJames Chapman /* Main ioctl() handler. 1175fd558d18SJames Chapman * Dispatch to tunnel or session helpers depending on the socket. 1176fd558d18SJames Chapman */ 1177fd558d18SJames Chapman static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd, 1178fd558d18SJames Chapman unsigned long arg) 1179fd558d18SJames Chapman { 1180fd558d18SJames Chapman struct sock *sk = sock->sk; 1181fd558d18SJames Chapman struct l2tp_session *session; 1182fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1183fd558d18SJames Chapman struct pppol2tp_session *ps; 1184fd558d18SJames Chapman int err; 1185fd558d18SJames Chapman 1186fd558d18SJames Chapman if (!sk) 1187fd558d18SJames Chapman return 0; 1188fd558d18SJames Chapman 1189fd558d18SJames Chapman err = -EBADF; 1190fd558d18SJames Chapman if (sock_flag(sk, SOCK_DEAD) != 0) 1191fd558d18SJames Chapman goto end; 1192fd558d18SJames Chapman 1193fd558d18SJames Chapman err = -ENOTCONN; 1194fd558d18SJames Chapman if ((sk->sk_user_data == NULL) || 1195fd558d18SJames Chapman (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)))) 1196fd558d18SJames Chapman goto end; 1197fd558d18SJames Chapman 1198fd558d18SJames Chapman /* Get session context from the socket */ 1199fd558d18SJames Chapman err = -EBADF; 1200fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 1201fd558d18SJames Chapman if (session == NULL) 1202fd558d18SJames Chapman goto end; 1203fd558d18SJames Chapman 1204fd558d18SJames Chapman /* Special case: if session's session_id is zero, treat ioctl as a 1205fd558d18SJames Chapman * tunnel ioctl 1206fd558d18SJames Chapman */ 1207fd558d18SJames Chapman ps = l2tp_session_priv(session); 1208fd558d18SJames Chapman if ((session->session_id == 0) && 1209fd558d18SJames Chapman (session->peer_session_id == 0)) { 1210fd558d18SJames Chapman err = -EBADF; 1211fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock); 1212fd558d18SJames Chapman if (tunnel == NULL) 1213fd558d18SJames Chapman goto end_put_sess; 1214fd558d18SJames Chapman 1215fd558d18SJames Chapman err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg); 1216fd558d18SJames Chapman sock_put(ps->tunnel_sock); 1217fd558d18SJames Chapman goto end_put_sess; 1218fd558d18SJames Chapman } 1219fd558d18SJames Chapman 1220fd558d18SJames Chapman err = pppol2tp_session_ioctl(session, cmd, arg); 1221fd558d18SJames Chapman 1222fd558d18SJames Chapman end_put_sess: 1223fd558d18SJames Chapman sock_put(sk); 1224fd558d18SJames Chapman end: 1225fd558d18SJames Chapman return err; 1226fd558d18SJames Chapman } 1227fd558d18SJames Chapman 1228fd558d18SJames Chapman /***************************************************************************** 1229fd558d18SJames Chapman * setsockopt() / getsockopt() support. 1230fd558d18SJames Chapman * 1231fd558d18SJames Chapman * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP 1232fd558d18SJames Chapman * sockets. In order to control kernel tunnel features, we allow userspace to 1233fd558d18SJames Chapman * create a special "tunnel" PPPoX socket which is used for control only. 1234fd558d18SJames Chapman * Tunnel PPPoX sockets have session_id == 0 and simply allow the user 1235fd558d18SJames Chapman * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls. 1236fd558d18SJames Chapman *****************************************************************************/ 1237fd558d18SJames Chapman 1238fd558d18SJames Chapman /* Tunnel setsockopt() helper. 1239fd558d18SJames Chapman */ 1240fd558d18SJames Chapman static int pppol2tp_tunnel_setsockopt(struct sock *sk, 1241fd558d18SJames Chapman struct l2tp_tunnel *tunnel, 1242fd558d18SJames Chapman int optname, int val) 1243fd558d18SJames Chapman { 1244fd558d18SJames Chapman int err = 0; 1245fd558d18SJames Chapman 1246fd558d18SJames Chapman switch (optname) { 1247fd558d18SJames Chapman case PPPOL2TP_SO_DEBUG: 1248fd558d18SJames Chapman tunnel->debug = val; 1249fba40c63SAsbjørn Sloth Tønnesen l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n", 1250a4ca44faSJoe Perches tunnel->name, tunnel->debug); 1251fd558d18SJames Chapman break; 1252fd558d18SJames Chapman 1253fd558d18SJames Chapman default: 1254fd558d18SJames Chapman err = -ENOPROTOOPT; 1255fd558d18SJames Chapman break; 1256fd558d18SJames Chapman } 1257fd558d18SJames Chapman 1258fd558d18SJames Chapman return err; 1259fd558d18SJames Chapman } 1260fd558d18SJames Chapman 1261fd558d18SJames Chapman /* Session setsockopt helper. 1262fd558d18SJames Chapman */ 1263fd558d18SJames Chapman static int pppol2tp_session_setsockopt(struct sock *sk, 1264fd558d18SJames Chapman struct l2tp_session *session, 1265fd558d18SJames Chapman int optname, int val) 1266fd558d18SJames Chapman { 1267fd558d18SJames Chapman int err = 0; 1268fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 1269fd558d18SJames Chapman 1270fd558d18SJames Chapman switch (optname) { 1271fd558d18SJames Chapman case PPPOL2TP_SO_RECVSEQ: 1272fd558d18SJames Chapman if ((val != 0) && (val != 1)) { 1273fd558d18SJames Chapman err = -EINVAL; 1274fd558d18SJames Chapman break; 1275fd558d18SJames Chapman } 12763f9b9770SAsbjørn Sloth Tønnesen session->recv_seq = !!val; 1277fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, 1278a4ca44faSJoe Perches "%s: set recv_seq=%d\n", 1279a4ca44faSJoe Perches session->name, session->recv_seq); 1280fd558d18SJames Chapman break; 1281fd558d18SJames Chapman 1282fd558d18SJames Chapman case PPPOL2TP_SO_SENDSEQ: 1283fd558d18SJames Chapman if ((val != 0) && (val != 1)) { 1284fd558d18SJames Chapman err = -EINVAL; 1285fd558d18SJames Chapman break; 1286fd558d18SJames Chapman } 12873f9b9770SAsbjørn Sloth Tønnesen session->send_seq = !!val; 1288fd558d18SJames Chapman { 1289fd558d18SJames Chapman struct sock *ssk = ps->sock; 1290fd558d18SJames Chapman struct pppox_sock *po = pppox_sk(ssk); 1291fd558d18SJames Chapman po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ : 1292fd558d18SJames Chapman PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; 1293fd558d18SJames Chapman } 1294bb5016eaSGuillaume Nault l2tp_session_set_header_len(session, session->tunnel->version); 1295fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, 1296a4ca44faSJoe Perches "%s: set send_seq=%d\n", 1297a4ca44faSJoe Perches session->name, session->send_seq); 1298fd558d18SJames Chapman break; 1299fd558d18SJames Chapman 1300fd558d18SJames Chapman case PPPOL2TP_SO_LNSMODE: 1301fd558d18SJames Chapman if ((val != 0) && (val != 1)) { 1302fd558d18SJames Chapman err = -EINVAL; 1303fd558d18SJames Chapman break; 1304fd558d18SJames Chapman } 13053f9b9770SAsbjørn Sloth Tønnesen session->lns_mode = !!val; 1306fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, 1307a4ca44faSJoe Perches "%s: set lns_mode=%d\n", 1308a4ca44faSJoe Perches session->name, session->lns_mode); 1309fd558d18SJames Chapman break; 1310fd558d18SJames Chapman 1311fd558d18SJames Chapman case PPPOL2TP_SO_DEBUG: 1312fd558d18SJames Chapman session->debug = val; 1313fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n", 1314a4ca44faSJoe Perches session->name, session->debug); 1315fd558d18SJames Chapman break; 1316fd558d18SJames Chapman 1317fd558d18SJames Chapman case PPPOL2TP_SO_REORDERTO: 1318fd558d18SJames Chapman session->reorder_timeout = msecs_to_jiffies(val); 1319fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, 1320a4ca44faSJoe Perches "%s: set reorder_timeout=%d\n", 1321a4ca44faSJoe Perches session->name, session->reorder_timeout); 1322fd558d18SJames Chapman break; 1323fd558d18SJames Chapman 1324fd558d18SJames Chapman default: 1325fd558d18SJames Chapman err = -ENOPROTOOPT; 1326fd558d18SJames Chapman break; 1327fd558d18SJames Chapman } 1328fd558d18SJames Chapman 1329fd558d18SJames Chapman return err; 1330fd558d18SJames Chapman } 1331fd558d18SJames Chapman 1332fd558d18SJames Chapman /* Main setsockopt() entry point. 1333fd558d18SJames Chapman * Does API checks, then calls either the tunnel or session setsockopt 1334fd558d18SJames Chapman * handler, according to whether the PPPoL2TP socket is a for a regular 1335fd558d18SJames Chapman * session or the special tunnel type. 1336fd558d18SJames Chapman */ 1337fd558d18SJames Chapman static int pppol2tp_setsockopt(struct socket *sock, int level, int optname, 1338fd558d18SJames Chapman char __user *optval, unsigned int optlen) 1339fd558d18SJames Chapman { 1340fd558d18SJames Chapman struct sock *sk = sock->sk; 1341fd558d18SJames Chapman struct l2tp_session *session; 1342fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1343fd558d18SJames Chapman struct pppol2tp_session *ps; 1344fd558d18SJames Chapman int val; 1345fd558d18SJames Chapman int err; 1346fd558d18SJames Chapman 1347fd558d18SJames Chapman if (level != SOL_PPPOL2TP) 13483cf521f7SSasha Levin return -EINVAL; 1349fd558d18SJames Chapman 1350fd558d18SJames Chapman if (optlen < sizeof(int)) 1351fd558d18SJames Chapman return -EINVAL; 1352fd558d18SJames Chapman 1353fd558d18SJames Chapman if (get_user(val, (int __user *)optval)) 1354fd558d18SJames Chapman return -EFAULT; 1355fd558d18SJames Chapman 1356fd558d18SJames Chapman err = -ENOTCONN; 1357fd558d18SJames Chapman if (sk->sk_user_data == NULL) 1358fd558d18SJames Chapman goto end; 1359fd558d18SJames Chapman 1360fd558d18SJames Chapman /* Get session context from the socket */ 1361fd558d18SJames Chapman err = -EBADF; 1362fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 1363fd558d18SJames Chapman if (session == NULL) 1364fd558d18SJames Chapman goto end; 1365fd558d18SJames Chapman 1366fd558d18SJames Chapman /* Special case: if session_id == 0x0000, treat as operation on tunnel 1367fd558d18SJames Chapman */ 1368fd558d18SJames Chapman ps = l2tp_session_priv(session); 1369fd558d18SJames Chapman if ((session->session_id == 0) && 1370fd558d18SJames Chapman (session->peer_session_id == 0)) { 1371fd558d18SJames Chapman err = -EBADF; 1372fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock); 1373fd558d18SJames Chapman if (tunnel == NULL) 1374fd558d18SJames Chapman goto end_put_sess; 1375fd558d18SJames Chapman 1376fd558d18SJames Chapman err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val); 1377fd558d18SJames Chapman sock_put(ps->tunnel_sock); 1378fd558d18SJames Chapman } else 1379fd558d18SJames Chapman err = pppol2tp_session_setsockopt(sk, session, optname, val); 1380fd558d18SJames Chapman 1381fd558d18SJames Chapman err = 0; 1382fd558d18SJames Chapman 1383fd558d18SJames Chapman end_put_sess: 1384fd558d18SJames Chapman sock_put(sk); 1385fd558d18SJames Chapman end: 1386fd558d18SJames Chapman return err; 1387fd558d18SJames Chapman } 1388fd558d18SJames Chapman 1389fd558d18SJames Chapman /* Tunnel getsockopt helper. Called with sock locked. 1390fd558d18SJames Chapman */ 1391fd558d18SJames Chapman static int pppol2tp_tunnel_getsockopt(struct sock *sk, 1392fd558d18SJames Chapman struct l2tp_tunnel *tunnel, 1393fd558d18SJames Chapman int optname, int *val) 1394fd558d18SJames Chapman { 1395fd558d18SJames Chapman int err = 0; 1396fd558d18SJames Chapman 1397fd558d18SJames Chapman switch (optname) { 1398fd558d18SJames Chapman case PPPOL2TP_SO_DEBUG: 1399fd558d18SJames Chapman *val = tunnel->debug; 1400fba40c63SAsbjørn Sloth Tønnesen l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n", 1401a4ca44faSJoe Perches tunnel->name, tunnel->debug); 1402fd558d18SJames Chapman break; 1403fd558d18SJames Chapman 1404fd558d18SJames Chapman default: 1405fd558d18SJames Chapman err = -ENOPROTOOPT; 1406fd558d18SJames Chapman break; 1407fd558d18SJames Chapman } 1408fd558d18SJames Chapman 1409fd558d18SJames Chapman return err; 1410fd558d18SJames Chapman } 1411fd558d18SJames Chapman 1412fd558d18SJames Chapman /* Session getsockopt helper. Called with sock locked. 1413fd558d18SJames Chapman */ 1414fd558d18SJames Chapman static int pppol2tp_session_getsockopt(struct sock *sk, 1415fd558d18SJames Chapman struct l2tp_session *session, 1416fd558d18SJames Chapman int optname, int *val) 1417fd558d18SJames Chapman { 1418fd558d18SJames Chapman int err = 0; 1419fd558d18SJames Chapman 1420fd558d18SJames Chapman switch (optname) { 1421fd558d18SJames Chapman case PPPOL2TP_SO_RECVSEQ: 1422fd558d18SJames Chapman *val = session->recv_seq; 1423fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, 1424fd558d18SJames Chapman "%s: get recv_seq=%d\n", session->name, *val); 1425fd558d18SJames Chapman break; 1426fd558d18SJames Chapman 1427fd558d18SJames Chapman case PPPOL2TP_SO_SENDSEQ: 1428fd558d18SJames Chapman *val = session->send_seq; 1429fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, 1430fd558d18SJames Chapman "%s: get send_seq=%d\n", session->name, *val); 1431fd558d18SJames Chapman break; 1432fd558d18SJames Chapman 1433fd558d18SJames Chapman case PPPOL2TP_SO_LNSMODE: 1434fd558d18SJames Chapman *val = session->lns_mode; 1435fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, 1436fd558d18SJames Chapman "%s: get lns_mode=%d\n", session->name, *val); 1437fd558d18SJames Chapman break; 1438fd558d18SJames Chapman 1439fd558d18SJames Chapman case PPPOL2TP_SO_DEBUG: 1440fd558d18SJames Chapman *val = session->debug; 1441fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n", 1442a4ca44faSJoe Perches session->name, *val); 1443fd558d18SJames Chapman break; 1444fd558d18SJames Chapman 1445fd558d18SJames Chapman case PPPOL2TP_SO_REORDERTO: 1446fd558d18SJames Chapman *val = (int) jiffies_to_msecs(session->reorder_timeout); 1447fba40c63SAsbjørn Sloth Tønnesen l2tp_info(session, L2TP_MSG_CONTROL, 1448fd558d18SJames Chapman "%s: get reorder_timeout=%d\n", session->name, *val); 1449fd558d18SJames Chapman break; 1450fd558d18SJames Chapman 1451fd558d18SJames Chapman default: 1452fd558d18SJames Chapman err = -ENOPROTOOPT; 1453fd558d18SJames Chapman } 1454fd558d18SJames Chapman 1455fd558d18SJames Chapman return err; 1456fd558d18SJames Chapman } 1457fd558d18SJames Chapman 1458fd558d18SJames Chapman /* Main getsockopt() entry point. 1459fd558d18SJames Chapman * Does API checks, then calls either the tunnel or session getsockopt 1460fd558d18SJames Chapman * handler, according to whether the PPPoX socket is a for a regular session 1461fd558d18SJames Chapman * or the special tunnel type. 1462fd558d18SJames Chapman */ 1463e3192690SJoe Perches static int pppol2tp_getsockopt(struct socket *sock, int level, int optname, 1464e3192690SJoe Perches char __user *optval, int __user *optlen) 1465fd558d18SJames Chapman { 1466fd558d18SJames Chapman struct sock *sk = sock->sk; 1467fd558d18SJames Chapman struct l2tp_session *session; 1468fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1469fd558d18SJames Chapman int val, len; 1470fd558d18SJames Chapman int err; 1471fd558d18SJames Chapman struct pppol2tp_session *ps; 1472fd558d18SJames Chapman 1473fd558d18SJames Chapman if (level != SOL_PPPOL2TP) 14743cf521f7SSasha Levin return -EINVAL; 1475fd558d18SJames Chapman 1476e3192690SJoe Perches if (get_user(len, optlen)) 1477fd558d18SJames Chapman return -EFAULT; 1478fd558d18SJames Chapman 1479fd558d18SJames Chapman len = min_t(unsigned int, len, sizeof(int)); 1480fd558d18SJames Chapman 1481fd558d18SJames Chapman if (len < 0) 1482fd558d18SJames Chapman return -EINVAL; 1483fd558d18SJames Chapman 1484fd558d18SJames Chapman err = -ENOTCONN; 1485fd558d18SJames Chapman if (sk->sk_user_data == NULL) 1486fd558d18SJames Chapman goto end; 1487fd558d18SJames Chapman 1488fd558d18SJames Chapman /* Get the session context */ 1489fd558d18SJames Chapman err = -EBADF; 1490fd558d18SJames Chapman session = pppol2tp_sock_to_session(sk); 1491fd558d18SJames Chapman if (session == NULL) 1492fd558d18SJames Chapman goto end; 1493fd558d18SJames Chapman 1494fd558d18SJames Chapman /* Special case: if session_id == 0x0000, treat as operation on tunnel */ 1495fd558d18SJames Chapman ps = l2tp_session_priv(session); 1496fd558d18SJames Chapman if ((session->session_id == 0) && 1497fd558d18SJames Chapman (session->peer_session_id == 0)) { 1498fd558d18SJames Chapman err = -EBADF; 1499fd558d18SJames Chapman tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock); 1500fd558d18SJames Chapman if (tunnel == NULL) 1501fd558d18SJames Chapman goto end_put_sess; 1502fd558d18SJames Chapman 1503fd558d18SJames Chapman err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val); 1504fd558d18SJames Chapman sock_put(ps->tunnel_sock); 1505fd558d18SJames Chapman } else 1506fd558d18SJames Chapman err = pppol2tp_session_getsockopt(sk, session, optname, &val); 1507fd558d18SJames Chapman 1508fd558d18SJames Chapman err = -EFAULT; 1509e3192690SJoe Perches if (put_user(len, optlen)) 1510fd558d18SJames Chapman goto end_put_sess; 1511fd558d18SJames Chapman 1512fd558d18SJames Chapman if (copy_to_user((void __user *) optval, &val, len)) 1513fd558d18SJames Chapman goto end_put_sess; 1514fd558d18SJames Chapman 1515fd558d18SJames Chapman err = 0; 1516fd558d18SJames Chapman 1517fd558d18SJames Chapman end_put_sess: 1518fd558d18SJames Chapman sock_put(sk); 1519fd558d18SJames Chapman end: 1520fd558d18SJames Chapman return err; 1521fd558d18SJames Chapman } 1522fd558d18SJames Chapman 1523fd558d18SJames Chapman /***************************************************************************** 1524fd558d18SJames Chapman * /proc filesystem for debug 1525f7faffa3SJames Chapman * Since the original pppol2tp driver provided /proc/net/pppol2tp for 1526f7faffa3SJames Chapman * L2TPv2, we dump only L2TPv2 tunnels and sessions here. 1527fd558d18SJames Chapman *****************************************************************************/ 1528fd558d18SJames Chapman 1529fd558d18SJames Chapman static unsigned int pppol2tp_net_id; 1530fd558d18SJames Chapman 1531fd558d18SJames Chapman #ifdef CONFIG_PROC_FS 1532fd558d18SJames Chapman 1533fd558d18SJames Chapman struct pppol2tp_seq_data { 1534fd558d18SJames Chapman struct seq_net_private p; 1535fd558d18SJames Chapman int tunnel_idx; /* current tunnel */ 1536fd558d18SJames Chapman int session_idx; /* index of session within current tunnel */ 1537fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 1538fd558d18SJames Chapman struct l2tp_session *session; /* NULL means get next tunnel */ 1539fd558d18SJames Chapman }; 1540fd558d18SJames Chapman 1541fd558d18SJames Chapman static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd) 1542fd558d18SJames Chapman { 1543f7faffa3SJames Chapman for (;;) { 1544fd558d18SJames Chapman pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx); 1545fd558d18SJames Chapman pd->tunnel_idx++; 1546f7faffa3SJames Chapman 1547f7faffa3SJames Chapman if (pd->tunnel == NULL) 1548f7faffa3SJames Chapman break; 1549f7faffa3SJames Chapman 1550f7faffa3SJames Chapman /* Ignore L2TPv3 tunnels */ 1551f7faffa3SJames Chapman if (pd->tunnel->version < 3) 1552f7faffa3SJames Chapman break; 1553f7faffa3SJames Chapman } 1554fd558d18SJames Chapman } 1555fd558d18SJames Chapman 1556fd558d18SJames Chapman static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd) 1557fd558d18SJames Chapman { 1558fd558d18SJames Chapman pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx); 1559fd558d18SJames Chapman pd->session_idx++; 1560f7faffa3SJames Chapman 1561fd558d18SJames Chapman if (pd->session == NULL) { 1562fd558d18SJames Chapman pd->session_idx = 0; 1563fd558d18SJames Chapman pppol2tp_next_tunnel(net, pd); 1564fd558d18SJames Chapman } 1565fd558d18SJames Chapman } 1566fd558d18SJames Chapman 1567fd558d18SJames Chapman static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs) 1568fd558d18SJames Chapman { 1569fd558d18SJames Chapman struct pppol2tp_seq_data *pd = SEQ_START_TOKEN; 1570fd558d18SJames Chapman loff_t pos = *offs; 1571fd558d18SJames Chapman struct net *net; 1572fd558d18SJames Chapman 1573fd558d18SJames Chapman if (!pos) 1574fd558d18SJames Chapman goto out; 1575fd558d18SJames Chapman 1576fd558d18SJames Chapman BUG_ON(m->private == NULL); 1577fd558d18SJames Chapman pd = m->private; 1578fd558d18SJames Chapman net = seq_file_net(m); 1579fd558d18SJames Chapman 1580fd558d18SJames Chapman if (pd->tunnel == NULL) 1581fd558d18SJames Chapman pppol2tp_next_tunnel(net, pd); 1582fd558d18SJames Chapman else 1583fd558d18SJames Chapman pppol2tp_next_session(net, pd); 1584fd558d18SJames Chapman 1585fd558d18SJames Chapman /* NULL tunnel and session indicates end of list */ 1586fd558d18SJames Chapman if ((pd->tunnel == NULL) && (pd->session == NULL)) 1587fd558d18SJames Chapman pd = NULL; 1588fd558d18SJames Chapman 1589fd558d18SJames Chapman out: 1590fd558d18SJames Chapman return pd; 1591fd558d18SJames Chapman } 1592fd558d18SJames Chapman 1593fd558d18SJames Chapman static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos) 1594fd558d18SJames Chapman { 1595fd558d18SJames Chapman (*pos)++; 1596fd558d18SJames Chapman return NULL; 1597fd558d18SJames Chapman } 1598fd558d18SJames Chapman 1599fd558d18SJames Chapman static void pppol2tp_seq_stop(struct seq_file *p, void *v) 1600fd558d18SJames Chapman { 1601fd558d18SJames Chapman /* nothing to do */ 1602fd558d18SJames Chapman } 1603fd558d18SJames Chapman 1604fd558d18SJames Chapman static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v) 1605fd558d18SJames Chapman { 1606fd558d18SJames Chapman struct l2tp_tunnel *tunnel = v; 1607fd558d18SJames Chapman 1608fd558d18SJames Chapman seq_printf(m, "\nTUNNEL '%s', %c %d\n", 1609fd558d18SJames Chapman tunnel->name, 1610fd558d18SJames Chapman (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N', 1611fd558d18SJames Chapman atomic_read(&tunnel->ref_count) - 1); 16127b7c0719STom Parkin seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n", 1613fd558d18SJames Chapman tunnel->debug, 16147b7c0719STom Parkin atomic_long_read(&tunnel->stats.tx_packets), 16157b7c0719STom Parkin atomic_long_read(&tunnel->stats.tx_bytes), 16167b7c0719STom Parkin atomic_long_read(&tunnel->stats.tx_errors), 16177b7c0719STom Parkin atomic_long_read(&tunnel->stats.rx_packets), 16187b7c0719STom Parkin atomic_long_read(&tunnel->stats.rx_bytes), 16197b7c0719STom Parkin atomic_long_read(&tunnel->stats.rx_errors)); 1620fd558d18SJames Chapman } 1621fd558d18SJames Chapman 1622fd558d18SJames Chapman static void pppol2tp_seq_session_show(struct seq_file *m, void *v) 1623fd558d18SJames Chapman { 1624fd558d18SJames Chapman struct l2tp_session *session = v; 1625fd558d18SJames Chapman struct l2tp_tunnel *tunnel = session->tunnel; 1626fd558d18SJames Chapman struct pppol2tp_session *ps = l2tp_session_priv(session); 16279345471bSJames Chapman struct pppox_sock *po = pppox_sk(ps->sock); 1628fd558d18SJames Chapman u32 ip = 0; 1629fd558d18SJames Chapman u16 port = 0; 1630fd558d18SJames Chapman 1631fd558d18SJames Chapman if (tunnel->sock) { 1632fd558d18SJames Chapman struct inet_sock *inet = inet_sk(tunnel->sock); 1633fd558d18SJames Chapman ip = ntohl(inet->inet_saddr); 1634fd558d18SJames Chapman port = ntohs(inet->inet_sport); 1635fd558d18SJames Chapman } 1636fd558d18SJames Chapman 1637fd558d18SJames Chapman seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> " 1638fd558d18SJames Chapman "%04X/%04X %d %c\n", 1639fd558d18SJames Chapman session->name, ip, port, 1640fd558d18SJames Chapman tunnel->tunnel_id, 1641fd558d18SJames Chapman session->session_id, 1642fd558d18SJames Chapman tunnel->peer_tunnel_id, 1643fd558d18SJames Chapman session->peer_session_id, 1644fd558d18SJames Chapman ps->sock->sk_state, 1645fd558d18SJames Chapman (session == ps->sock->sk_user_data) ? 1646fd558d18SJames Chapman 'Y' : 'N'); 1647fd558d18SJames Chapman seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n", 1648fd558d18SJames Chapman session->mtu, session->mru, 1649fd558d18SJames Chapman session->recv_seq ? 'R' : '-', 1650fd558d18SJames Chapman session->send_seq ? 'S' : '-', 1651fd558d18SJames Chapman session->lns_mode ? "LNS" : "LAC", 1652fd558d18SJames Chapman session->debug, 1653fd558d18SJames Chapman jiffies_to_msecs(session->reorder_timeout)); 16547b7c0719STom Parkin seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n", 1655fd558d18SJames Chapman session->nr, session->ns, 16567b7c0719STom Parkin atomic_long_read(&session->stats.tx_packets), 16577b7c0719STom Parkin atomic_long_read(&session->stats.tx_bytes), 16587b7c0719STom Parkin atomic_long_read(&session->stats.tx_errors), 16597b7c0719STom Parkin atomic_long_read(&session->stats.rx_packets), 16607b7c0719STom Parkin atomic_long_read(&session->stats.rx_bytes), 16617b7c0719STom Parkin atomic_long_read(&session->stats.rx_errors)); 16629345471bSJames Chapman 16639345471bSJames Chapman if (po) 16649345471bSJames Chapman seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan)); 1665fd558d18SJames Chapman } 1666fd558d18SJames Chapman 1667fd558d18SJames Chapman static int pppol2tp_seq_show(struct seq_file *m, void *v) 1668fd558d18SJames Chapman { 1669fd558d18SJames Chapman struct pppol2tp_seq_data *pd = v; 1670fd558d18SJames Chapman 1671fd558d18SJames Chapman /* display header on line 1 */ 1672fd558d18SJames Chapman if (v == SEQ_START_TOKEN) { 1673fd558d18SJames Chapman seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n"); 1674fd558d18SJames Chapman seq_puts(m, "TUNNEL name, user-data-ok session-count\n"); 1675fd558d18SJames Chapman seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); 1676fd558d18SJames Chapman seq_puts(m, " SESSION name, addr/port src-tid/sid " 1677fd558d18SJames Chapman "dest-tid/sid state user-data-ok\n"); 1678fd558d18SJames Chapman seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n"); 1679fd558d18SJames Chapman seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); 1680fd558d18SJames Chapman goto out; 1681fd558d18SJames Chapman } 1682fd558d18SJames Chapman 1683fd558d18SJames Chapman /* Show the tunnel or session context. 1684fd558d18SJames Chapman */ 1685fd558d18SJames Chapman if (pd->session == NULL) 1686fd558d18SJames Chapman pppol2tp_seq_tunnel_show(m, pd->tunnel); 1687fd558d18SJames Chapman else 1688fd558d18SJames Chapman pppol2tp_seq_session_show(m, pd->session); 1689fd558d18SJames Chapman 1690fd558d18SJames Chapman out: 1691fd558d18SJames Chapman return 0; 1692fd558d18SJames Chapman } 1693fd558d18SJames Chapman 1694fd558d18SJames Chapman static const struct seq_operations pppol2tp_seq_ops = { 1695fd558d18SJames Chapman .start = pppol2tp_seq_start, 1696fd558d18SJames Chapman .next = pppol2tp_seq_next, 1697fd558d18SJames Chapman .stop = pppol2tp_seq_stop, 1698fd558d18SJames Chapman .show = pppol2tp_seq_show, 1699fd558d18SJames Chapman }; 1700fd558d18SJames Chapman 1701fd558d18SJames Chapman /* Called when our /proc file is opened. We allocate data for use when 1702fd558d18SJames Chapman * iterating our tunnel / session contexts and store it in the private 1703fd558d18SJames Chapman * data of the seq_file. 1704fd558d18SJames Chapman */ 1705fd558d18SJames Chapman static int pppol2tp_proc_open(struct inode *inode, struct file *file) 1706fd558d18SJames Chapman { 1707fd558d18SJames Chapman return seq_open_net(inode, file, &pppol2tp_seq_ops, 1708fd558d18SJames Chapman sizeof(struct pppol2tp_seq_data)); 1709fd558d18SJames Chapman } 1710fd558d18SJames Chapman 1711fd558d18SJames Chapman static const struct file_operations pppol2tp_proc_fops = { 1712fd558d18SJames Chapman .owner = THIS_MODULE, 1713fd558d18SJames Chapman .open = pppol2tp_proc_open, 1714fd558d18SJames Chapman .read = seq_read, 1715fd558d18SJames Chapman .llseek = seq_lseek, 1716fd558d18SJames Chapman .release = seq_release_net, 1717fd558d18SJames Chapman }; 1718fd558d18SJames Chapman 1719fd558d18SJames Chapman #endif /* CONFIG_PROC_FS */ 1720fd558d18SJames Chapman 1721fd558d18SJames Chapman /***************************************************************************** 1722fd558d18SJames Chapman * Network namespace 1723fd558d18SJames Chapman *****************************************************************************/ 1724fd558d18SJames Chapman 1725fd558d18SJames Chapman static __net_init int pppol2tp_init_net(struct net *net) 1726fd558d18SJames Chapman { 1727fd558d18SJames Chapman struct proc_dir_entry *pde; 1728fd558d18SJames Chapman int err = 0; 1729fd558d18SJames Chapman 1730d4beaa66SGao feng pde = proc_create("pppol2tp", S_IRUGO, net->proc_net, 1731d4beaa66SGao feng &pppol2tp_proc_fops); 1732fd558d18SJames Chapman if (!pde) { 1733fd558d18SJames Chapman err = -ENOMEM; 1734fd558d18SJames Chapman goto out; 1735fd558d18SJames Chapman } 1736fd558d18SJames Chapman 1737fd558d18SJames Chapman out: 1738fd558d18SJames Chapman return err; 1739fd558d18SJames Chapman } 1740fd558d18SJames Chapman 1741fd558d18SJames Chapman static __net_exit void pppol2tp_exit_net(struct net *net) 1742fd558d18SJames Chapman { 1743ece31ffdSGao feng remove_proc_entry("pppol2tp", net->proc_net); 1744fd558d18SJames Chapman } 1745fd558d18SJames Chapman 1746fd558d18SJames Chapman static struct pernet_operations pppol2tp_net_ops = { 1747fd558d18SJames Chapman .init = pppol2tp_init_net, 1748fd558d18SJames Chapman .exit = pppol2tp_exit_net, 1749fd558d18SJames Chapman .id = &pppol2tp_net_id, 1750fd558d18SJames Chapman }; 1751fd558d18SJames Chapman 1752fd558d18SJames Chapman /***************************************************************************** 1753fd558d18SJames Chapman * Init and cleanup 1754fd558d18SJames Chapman *****************************************************************************/ 1755fd558d18SJames Chapman 1756fd558d18SJames Chapman static const struct proto_ops pppol2tp_ops = { 1757fd558d18SJames Chapman .family = AF_PPPOX, 1758fd558d18SJames Chapman .owner = THIS_MODULE, 1759fd558d18SJames Chapman .release = pppol2tp_release, 1760fd558d18SJames Chapman .bind = sock_no_bind, 1761fd558d18SJames Chapman .connect = pppol2tp_connect, 1762fd558d18SJames Chapman .socketpair = sock_no_socketpair, 1763fd558d18SJames Chapman .accept = sock_no_accept, 1764fd558d18SJames Chapman .getname = pppol2tp_getname, 1765fd558d18SJames Chapman .poll = datagram_poll, 1766fd558d18SJames Chapman .listen = sock_no_listen, 1767fd558d18SJames Chapman .shutdown = sock_no_shutdown, 1768fd558d18SJames Chapman .setsockopt = pppol2tp_setsockopt, 1769fd558d18SJames Chapman .getsockopt = pppol2tp_getsockopt, 1770fd558d18SJames Chapman .sendmsg = pppol2tp_sendmsg, 1771fd558d18SJames Chapman .recvmsg = pppol2tp_recvmsg, 1772fd558d18SJames Chapman .mmap = sock_no_mmap, 1773fd558d18SJames Chapman .ioctl = pppox_ioctl, 1774fd558d18SJames Chapman }; 1775fd558d18SJames Chapman 1776756e64a0SEric Dumazet static const struct pppox_proto pppol2tp_proto = { 1777fd558d18SJames Chapman .create = pppol2tp_create, 1778e1558a93SWei Yongjun .ioctl = pppol2tp_ioctl, 1779e1558a93SWei Yongjun .owner = THIS_MODULE, 1780fd558d18SJames Chapman }; 1781fd558d18SJames Chapman 1782309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 1783309795f4SJames Chapman 1784309795f4SJames Chapman static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = { 1785309795f4SJames Chapman .session_create = pppol2tp_session_create, 1786cf2f5c88STom Parkin .session_delete = l2tp_session_delete, 1787309795f4SJames Chapman }; 1788309795f4SJames Chapman 1789309795f4SJames Chapman #endif /* CONFIG_L2TP_V3 */ 1790309795f4SJames Chapman 1791fd558d18SJames Chapman static int __init pppol2tp_init(void) 1792fd558d18SJames Chapman { 1793fd558d18SJames Chapman int err; 1794fd558d18SJames Chapman 1795fd558d18SJames Chapman err = register_pernet_device(&pppol2tp_net_ops); 1796fd558d18SJames Chapman if (err) 1797fd558d18SJames Chapman goto out; 1798fd558d18SJames Chapman 1799fd558d18SJames Chapman err = proto_register(&pppol2tp_sk_proto, 0); 1800fd558d18SJames Chapman if (err) 1801fd558d18SJames Chapman goto out_unregister_pppol2tp_pernet; 1802fd558d18SJames Chapman 1803fd558d18SJames Chapman err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto); 1804fd558d18SJames Chapman if (err) 1805fd558d18SJames Chapman goto out_unregister_pppol2tp_proto; 1806fd558d18SJames Chapman 1807309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 1808309795f4SJames Chapman err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops); 1809309795f4SJames Chapman if (err) 1810309795f4SJames Chapman goto out_unregister_pppox; 1811309795f4SJames Chapman #endif 1812309795f4SJames Chapman 1813a4ca44faSJoe Perches pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION); 1814fd558d18SJames Chapman 1815fd558d18SJames Chapman out: 1816fd558d18SJames Chapman return err; 1817309795f4SJames Chapman 1818309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 1819309795f4SJames Chapman out_unregister_pppox: 1820309795f4SJames Chapman unregister_pppox_proto(PX_PROTO_OL2TP); 1821309795f4SJames Chapman #endif 1822fd558d18SJames Chapman out_unregister_pppol2tp_proto: 1823fd558d18SJames Chapman proto_unregister(&pppol2tp_sk_proto); 1824fd558d18SJames Chapman out_unregister_pppol2tp_pernet: 1825fd558d18SJames Chapman unregister_pernet_device(&pppol2tp_net_ops); 1826fd558d18SJames Chapman goto out; 1827fd558d18SJames Chapman } 1828fd558d18SJames Chapman 1829fd558d18SJames Chapman static void __exit pppol2tp_exit(void) 1830fd558d18SJames Chapman { 1831309795f4SJames Chapman #ifdef CONFIG_L2TP_V3 1832309795f4SJames Chapman l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP); 1833309795f4SJames Chapman #endif 1834fd558d18SJames Chapman unregister_pppox_proto(PX_PROTO_OL2TP); 1835fd558d18SJames Chapman proto_unregister(&pppol2tp_sk_proto); 1836fd558d18SJames Chapman unregister_pernet_device(&pppol2tp_net_ops); 1837fd558d18SJames Chapman } 1838fd558d18SJames Chapman 1839fd558d18SJames Chapman module_init(pppol2tp_init); 1840fd558d18SJames Chapman module_exit(pppol2tp_exit); 1841fd558d18SJames Chapman 1842fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1843fd558d18SJames Chapman MODULE_DESCRIPTION("PPP over L2TP over UDP"); 1844fd558d18SJames Chapman MODULE_LICENSE("GPL"); 1845fd558d18SJames Chapman MODULE_VERSION(PPPOL2TP_DRV_VERSION); 1846681b4d88SGuillaume Nault MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP); 1847f1f39f91Sstephen hemminger MODULE_ALIAS_L2TP_PWTYPE(11); 1848