Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 2 | /***************************************************************************** |
| 3 | * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets |
| 4 | * |
| 5 | * PPPoX --- Generic PPP encapsulation socket family |
| 6 | * PPPoL2TP --- PPP over L2TP (RFC 2661) |
| 7 | * |
| 8 | * Version: 2.0.0 |
| 9 | * |
| 10 | * Authors: James Chapman (jchapman@katalix.com) |
| 11 | * |
| 12 | * Based on original work by Martijn van Oosterhout <kleptog@svana.org> |
| 13 | * |
| 14 | * License: |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* This driver handles only L2TP data frames; control frames are handled by a |
| 18 | * userspace application. |
| 19 | * |
| 20 | * To send data in an L2TP session, userspace opens a PPPoL2TP socket and |
| 21 | * attaches it to a bound UDP socket with local tunnel_id / session_id and |
| 22 | * peer tunnel_id / session_id set. Data can then be sent or received using |
| 23 | * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket |
| 24 | * can be read or modified using ioctl() or [gs]etsockopt() calls. |
| 25 | * |
| 26 | * When a PPPoL2TP socket is connected with local and peer session_id values |
| 27 | * zero, the socket is treated as a special tunnel management socket. |
| 28 | * |
| 29 | * Here's example userspace code to create a socket for sending/receiving data |
| 30 | * over an L2TP session:- |
| 31 | * |
| 32 | * struct sockaddr_pppol2tp sax; |
| 33 | * int fd; |
| 34 | * int session_fd; |
| 35 | * |
| 36 | * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); |
| 37 | * |
| 38 | * sax.sa_family = AF_PPPOX; |
| 39 | * sax.sa_protocol = PX_PROTO_OL2TP; |
| 40 | * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket |
| 41 | * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr; |
| 42 | * sax.pppol2tp.addr.sin_port = addr->sin_port; |
| 43 | * sax.pppol2tp.addr.sin_family = AF_INET; |
| 44 | * sax.pppol2tp.s_tunnel = tunnel_id; |
| 45 | * sax.pppol2tp.s_session = session_id; |
| 46 | * sax.pppol2tp.d_tunnel = peer_tunnel_id; |
| 47 | * sax.pppol2tp.d_session = peer_session_id; |
| 48 | * |
| 49 | * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax)); |
| 50 | * |
| 51 | * A pppd plugin that allows PPP traffic to be carried over L2TP using |
| 52 | * this driver is available from the OpenL2TP project at |
| 53 | * http://openl2tp.sourceforge.net. |
| 54 | */ |
| 55 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 56 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 57 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 58 | #include <linux/module.h> |
| 59 | #include <linux/string.h> |
| 60 | #include <linux/list.h> |
| 61 | #include <linux/uaccess.h> |
| 62 | |
| 63 | #include <linux/kernel.h> |
| 64 | #include <linux/spinlock.h> |
| 65 | #include <linux/kthread.h> |
| 66 | #include <linux/sched.h> |
| 67 | #include <linux/slab.h> |
| 68 | #include <linux/errno.h> |
| 69 | #include <linux/jiffies.h> |
| 70 | |
| 71 | #include <linux/netdevice.h> |
| 72 | #include <linux/net.h> |
| 73 | #include <linux/inetdevice.h> |
| 74 | #include <linux/skbuff.h> |
| 75 | #include <linux/init.h> |
| 76 | #include <linux/ip.h> |
| 77 | #include <linux/udp.h> |
| 78 | #include <linux/if_pppox.h> |
| 79 | #include <linux/if_pppol2tp.h> |
| 80 | #include <net/sock.h> |
| 81 | #include <linux/ppp_channel.h> |
| 82 | #include <linux/ppp_defs.h> |
Paul Mackerras | 4b32da2b | 2012-03-04 12:56:55 +0000 | [diff] [blame] | 83 | #include <linux/ppp-ioctl.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 84 | #include <linux/file.h> |
| 85 | #include <linux/hash.h> |
| 86 | #include <linux/sort.h> |
| 87 | #include <linux/proc_fs.h> |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 88 | #include <linux/l2tp.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 89 | #include <linux/nsproxy.h> |
| 90 | #include <net/net_namespace.h> |
| 91 | #include <net/netns/generic.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 92 | #include <net/ip.h> |
| 93 | #include <net/udp.h> |
Tom Parkin | cf2f5c8 | 2013-03-19 06:11:21 +0000 | [diff] [blame] | 94 | #include <net/inet_common.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 95 | |
| 96 | #include <asm/byteorder.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 97 | #include <linux/atomic.h> |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 98 | |
| 99 | #include "l2tp_core.h" |
| 100 | |
| 101 | #define PPPOL2TP_DRV_VERSION "V2.0" |
| 102 | |
| 103 | /* Space for UDP, L2TP and PPP headers */ |
| 104 | #define PPPOL2TP_HEADER_OVERHEAD 40 |
| 105 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 106 | /* Number of bytes to build transmit L2TP headers. |
| 107 | * Unfortunately the size is different depending on whether sequence numbers |
| 108 | * are enabled. |
| 109 | */ |
| 110 | #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10 |
| 111 | #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6 |
| 112 | |
| 113 | /* Private data of each session. This data lives at the end of struct |
| 114 | * l2tp_session, referenced via session->priv[]. |
| 115 | */ |
| 116 | struct pppol2tp_session { |
| 117 | int owner; /* pid that opened the socket */ |
| 118 | |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 119 | struct mutex sk_lock; /* Protects .sk */ |
Tom Parkin | 20dcb11 | 2020-07-22 17:32:06 +0100 | [diff] [blame] | 120 | struct sock __rcu *sk; /* Pointer to the session PPPoX socket */ |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 121 | struct sock *__sk; /* Copy of .sk, for cleanup */ |
| 122 | struct rcu_head rcu; /* For asynchronous release */ |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb); |
| 126 | |
stephen hemminger | d7100da | 2010-08-04 07:34:36 +0000 | [diff] [blame] | 127 | static const struct ppp_channel_ops pppol2tp_chan_ops = { |
| 128 | .start_xmit = pppol2tp_xmit, |
| 129 | }; |
| 130 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 131 | static const struct proto_ops pppol2tp_ops; |
| 132 | |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 133 | /* Retrieves the pppol2tp socket associated to a session. |
| 134 | * A reference is held on the returned socket, so this function must be paired |
| 135 | * with sock_put(). |
| 136 | */ |
| 137 | static struct sock *pppol2tp_session_get_sock(struct l2tp_session *session) |
| 138 | { |
| 139 | struct pppol2tp_session *ps = l2tp_session_priv(session); |
| 140 | struct sock *sk; |
| 141 | |
| 142 | rcu_read_lock(); |
| 143 | sk = rcu_dereference(ps->sk); |
| 144 | if (sk) |
| 145 | sock_hold(sk); |
| 146 | rcu_read_unlock(); |
| 147 | |
| 148 | return sk; |
| 149 | } |
| 150 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 151 | /* Helpers to obtain tunnel/session contexts from sockets. |
| 152 | */ |
| 153 | static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk) |
| 154 | { |
| 155 | struct l2tp_session *session; |
| 156 | |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 157 | if (!sk) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | |
| 160 | sock_hold(sk); |
| 161 | session = (struct l2tp_session *)(sk->sk_user_data); |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 162 | if (!session) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 163 | sock_put(sk); |
| 164 | goto out; |
| 165 | } |
| 166 | |
| 167 | BUG_ON(session->magic != L2TP_SESSION_MAGIC); |
| 168 | |
| 169 | out: |
| 170 | return session; |
| 171 | } |
| 172 | |
| 173 | /***************************************************************************** |
| 174 | * Receive data handling |
| 175 | *****************************************************************************/ |
| 176 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 177 | /* Receive message. This is the recvmsg for the PPPoL2TP socket. |
| 178 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 179 | static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg, |
| 180 | size_t len, int flags) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 181 | { |
| 182 | int err; |
| 183 | struct sk_buff *skb; |
| 184 | struct sock *sk = sock->sk; |
| 185 | |
| 186 | err = -EIO; |
| 187 | if (sk->sk_state & PPPOX_BOUND) |
| 188 | goto end; |
| 189 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 190 | err = 0; |
| 191 | skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, |
| 192 | flags & MSG_DONTWAIT, &err); |
| 193 | if (!skb) |
| 194 | goto end; |
| 195 | |
| 196 | if (len > skb->len) |
| 197 | len = skb->len; |
| 198 | else if (len < skb->len) |
| 199 | msg->msg_flags |= MSG_TRUNC; |
| 200 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 201 | err = skb_copy_datagram_msg(skb, 0, msg, len); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 202 | if (likely(err == 0)) |
| 203 | err = len; |
| 204 | |
| 205 | kfree_skb(skb); |
| 206 | end: |
| 207 | return err; |
| 208 | } |
| 209 | |
| 210 | static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len) |
| 211 | { |
| 212 | struct pppol2tp_session *ps = l2tp_session_priv(session); |
| 213 | struct sock *sk = NULL; |
| 214 | |
| 215 | /* If the socket is bound, send it in to PPP's input queue. Otherwise |
| 216 | * queue it on the session socket. |
| 217 | */ |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 218 | rcu_read_lock(); |
| 219 | sk = rcu_dereference(ps->sk); |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 220 | if (!sk) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 221 | goto no_sock; |
| 222 | |
Guillaume Nault | 2b139e6 | 2018-07-25 14:53:33 +0200 | [diff] [blame] | 223 | /* If the first two bytes are 0xFF03, consider that it is the PPP's |
| 224 | * Address and Control fields and skip them. The L2TP module has always |
| 225 | * worked this way, although, in theory, the use of these fields should |
| 226 | * be negociated and handled at the PPP layer. These fields are |
| 227 | * constant: 0xFF is the All-Stations Address and 0x03 the Unnumbered |
| 228 | * Information command with Poll/Final bit set to zero (RFC 1662). |
| 229 | */ |
| 230 | if (pskb_may_pull(skb, 2) && skb->data[0] == PPP_ALLSTATIONS && |
| 231 | skb->data[1] == PPP_UI) |
| 232 | skb_pull(skb, 2); |
| 233 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 234 | if (sk->sk_state & PPPOX_BOUND) { |
| 235 | struct pppox_sock *po; |
Guillaume Nault | 98f40b3 | 2015-12-29 13:06:59 +0100 | [diff] [blame] | 236 | |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 237 | l2tp_dbg(session, L2TP_MSG_DATA, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 238 | "%s: recv %d byte data frame, passing to ppp\n", |
| 239 | session->name, data_len); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 240 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 241 | po = pppox_sk(sk); |
| 242 | ppp_input(&po->chan, skb); |
| 243 | } else { |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 244 | l2tp_dbg(session, L2TP_MSG_DATA, |
Guillaume Nault | 9e9cb62 | 2014-03-06 11:15:10 +0100 | [diff] [blame] | 245 | "%s: recv %d byte data frame, passing to L2TP socket\n", |
| 246 | session->name, data_len); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 247 | |
Guillaume Nault | 9e9cb62 | 2014-03-06 11:15:10 +0100 | [diff] [blame] | 248 | if (sock_queue_rcv_skb(sk, skb) < 0) { |
| 249 | atomic_long_inc(&session->stats.rx_errors); |
| 250 | kfree_skb(skb); |
| 251 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 252 | } |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 253 | rcu_read_unlock(); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 254 | |
| 255 | return; |
| 256 | |
| 257 | no_sock: |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 258 | rcu_read_unlock(); |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 259 | l2tp_info(session, L2TP_MSG_DATA, "%s: no socket\n", session->name); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 260 | kfree_skb(skb); |
| 261 | } |
| 262 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 263 | /************************************************************************ |
| 264 | * Transmit handling |
| 265 | ***********************************************************************/ |
| 266 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 267 | /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here |
| 268 | * when a user application does a sendmsg() on the session socket. L2TP and |
| 269 | * PPP headers must be inserted into the user's data. |
| 270 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 271 | static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m, |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 272 | size_t total_len) |
| 273 | { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 274 | struct sock *sk = sock->sk; |
| 275 | struct sk_buff *skb; |
| 276 | int error; |
| 277 | struct l2tp_session *session; |
| 278 | struct l2tp_tunnel *tunnel; |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 279 | int uhlen; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 280 | |
| 281 | error = -ENOTCONN; |
| 282 | if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) |
| 283 | goto error; |
| 284 | |
| 285 | /* Get session and tunnel contexts */ |
| 286 | error = -EBADF; |
| 287 | session = pppol2tp_sock_to_session(sk); |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 288 | if (!session) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 289 | goto error; |
| 290 | |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 291 | tunnel = session->tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 292 | |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 293 | uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; |
| 294 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 295 | /* Allocate a socket buffer */ |
| 296 | error = -ENOMEM; |
| 297 | skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) + |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 298 | uhlen + session->hdr_len + |
Gao Feng | 54c151d | 2016-08-22 22:50:02 +0800 | [diff] [blame] | 299 | 2 + total_len, /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */ |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 300 | 0, GFP_KERNEL); |
| 301 | if (!skb) |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 302 | goto error_put_sess; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 303 | |
| 304 | /* Reserve space for headers. */ |
| 305 | skb_reserve(skb, NET_SKB_PAD); |
| 306 | skb_reset_network_header(skb); |
| 307 | skb_reserve(skb, sizeof(struct iphdr)); |
| 308 | skb_reset_transport_header(skb); |
James Chapman | 0d76751 | 2010-04-02 06:19:00 +0000 | [diff] [blame] | 309 | skb_reserve(skb, uhlen); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 310 | |
| 311 | /* Add PPP header */ |
Gao Feng | 54c151d | 2016-08-22 22:50:02 +0800 | [diff] [blame] | 312 | skb->data[0] = PPP_ALLSTATIONS; |
| 313 | skb->data[1] = PPP_UI; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 314 | skb_put(skb, 2); |
| 315 | |
| 316 | /* Copy user data into skb */ |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 317 | error = memcpy_from_msg(skb_put(skb, total_len), m, total_len); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 318 | if (error < 0) { |
| 319 | kfree_skb(skb); |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 320 | goto error_put_sess; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 321 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 322 | |
Eric Dumazet | 455cc32 | 2013-10-10 06:30:09 -0700 | [diff] [blame] | 323 | local_bh_disable(); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 324 | l2tp_xmit_skb(session, skb, session->hdr_len); |
Eric Dumazet | 455cc32 | 2013-10-10 06:30:09 -0700 | [diff] [blame] | 325 | local_bh_enable(); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 326 | |
Guillaume Nault | 8b82547e33e | 2013-03-01 05:02:02 +0000 | [diff] [blame] | 327 | sock_put(sk); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 328 | |
Guillaume Nault | a6f79d0 | 2013-06-12 16:07:36 +0200 | [diff] [blame] | 329 | return total_len; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 330 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 331 | error_put_sess: |
| 332 | sock_put(sk); |
| 333 | error: |
| 334 | return error; |
| 335 | } |
| 336 | |
| 337 | /* Transmit function called by generic PPP driver. Sends PPP frame |
| 338 | * over PPPoL2TP socket. |
| 339 | * |
| 340 | * This is almost the same as pppol2tp_sendmsg(), but rather than |
| 341 | * being called with a msghdr from userspace, it is called with a skb |
| 342 | * from the kernel. |
| 343 | * |
| 344 | * The supplied skb from ppp doesn't have enough headroom for the |
| 345 | * insertion of L2TP, UDP and IP headers so we need to allocate more |
| 346 | * headroom in the skb. This will create a cloned skb. But we must be |
| 347 | * careful in the error case because the caller will expect to free |
| 348 | * the skb it supplied, not our cloned skb. So we take care to always |
| 349 | * leave the original skb unfreed if we return an error. |
| 350 | */ |
| 351 | static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb) |
| 352 | { |
Tom Parkin | b71a61c | 2020-07-22 17:32:05 +0100 | [diff] [blame] | 353 | struct sock *sk = (struct sock *)chan->private; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 354 | struct l2tp_session *session; |
| 355 | struct l2tp_tunnel *tunnel; |
Eric Dumazet | 09df57c | 2011-10-07 05:45:57 +0000 | [diff] [blame] | 356 | int uhlen, headroom; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 357 | |
| 358 | if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) |
| 359 | goto abort; |
| 360 | |
| 361 | /* Get session and tunnel contexts from the socket */ |
| 362 | session = pppol2tp_sock_to_session(sk); |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 363 | if (!session) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 364 | goto abort; |
| 365 | |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 366 | tunnel = session->tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 367 | |
Eric Dumazet | 09df57c | 2011-10-07 05:45:57 +0000 | [diff] [blame] | 368 | uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; |
| 369 | headroom = NET_SKB_PAD + |
| 370 | sizeof(struct iphdr) + /* IP header */ |
| 371 | uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */ |
| 372 | session->hdr_len + /* L2TP header */ |
Gao Feng | 54c151d | 2016-08-22 22:50:02 +0800 | [diff] [blame] | 373 | 2; /* 2 bytes for PPP_ALLSTATIONS & PPP_UI */ |
Eric Dumazet | 09df57c | 2011-10-07 05:45:57 +0000 | [diff] [blame] | 374 | if (skb_cow_head(skb, headroom)) |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 375 | goto abort_put_sess; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 376 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 377 | /* Setup PPP header */ |
Gao Feng | 54c151d | 2016-08-22 22:50:02 +0800 | [diff] [blame] | 378 | __skb_push(skb, 2); |
| 379 | skb->data[0] = PPP_ALLSTATIONS; |
| 380 | skb->data[1] = PPP_UI; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 381 | |
Eric Dumazet | 455cc32 | 2013-10-10 06:30:09 -0700 | [diff] [blame] | 382 | local_bh_disable(); |
James Chapman | e0d4435 | 2010-04-02 06:18:54 +0000 | [diff] [blame] | 383 | l2tp_xmit_skb(session, skb, session->hdr_len); |
Eric Dumazet | 455cc32 | 2013-10-10 06:30:09 -0700 | [diff] [blame] | 384 | local_bh_enable(); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 385 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 386 | sock_put(sk); |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 387 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 388 | return 1; |
| 389 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 390 | abort_put_sess: |
| 391 | sock_put(sk); |
| 392 | abort: |
| 393 | /* Free the original skb */ |
| 394 | kfree_skb(skb); |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | /***************************************************************************** |
| 399 | * Session (and tunnel control) socket create/destroy. |
| 400 | *****************************************************************************/ |
| 401 | |
James Chapman | d02ba2a | 2018-02-23 17:45:46 +0000 | [diff] [blame] | 402 | static void pppol2tp_put_sk(struct rcu_head *head) |
| 403 | { |
| 404 | struct pppol2tp_session *ps; |
| 405 | |
| 406 | ps = container_of(head, typeof(*ps), rcu); |
| 407 | sock_put(ps->__sk); |
| 408 | } |
| 409 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 410 | /* Really kill the session socket. (Called from sock_put() if |
| 411 | * refcnt == 0.) |
| 412 | */ |
| 413 | static void pppol2tp_session_destruct(struct sock *sk) |
| 414 | { |
Tom Parkin | f6e16b2 | 2013-03-19 06:11:23 +0000 | [diff] [blame] | 415 | struct l2tp_session *session = sk->sk_user_data; |
Guillaume Nault | e91793b | 2017-03-29 08:45:29 +0200 | [diff] [blame] | 416 | |
| 417 | skb_queue_purge(&sk->sk_receive_queue); |
| 418 | skb_queue_purge(&sk->sk_write_queue); |
| 419 | |
Tom Parkin | f6e16b2 | 2013-03-19 06:11:23 +0000 | [diff] [blame] | 420 | if (session) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 421 | sk->sk_user_data = NULL; |
| 422 | BUG_ON(session->magic != L2TP_SESSION_MAGIC); |
| 423 | l2tp_session_dec_refcount(session); |
| 424 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* Called when the PPPoX socket (session) is closed. |
| 428 | */ |
| 429 | static int pppol2tp_release(struct socket *sock) |
| 430 | { |
| 431 | struct sock *sk = sock->sk; |
| 432 | struct l2tp_session *session; |
| 433 | int error; |
| 434 | |
| 435 | if (!sk) |
| 436 | return 0; |
| 437 | |
| 438 | error = -EBADF; |
| 439 | lock_sock(sk); |
| 440 | if (sock_flag(sk, SOCK_DEAD) != 0) |
| 441 | goto error; |
| 442 | |
| 443 | pppox_unbind_sock(sk); |
| 444 | |
| 445 | /* Signal the death of the socket. */ |
| 446 | sk->sk_state = PPPOX_DEAD; |
| 447 | sock_orphan(sk); |
| 448 | sock->sk = NULL; |
| 449 | |
| 450 | session = pppol2tp_sock_to_session(sk); |
James Chapman | d02ba2a | 2018-02-23 17:45:46 +0000 | [diff] [blame] | 451 | if (session) { |
Guillaume Nault | 3d60934 | 2018-06-04 18:52:19 +0200 | [diff] [blame] | 452 | struct pppol2tp_session *ps; |
| 453 | |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 454 | l2tp_session_delete(session); |
Guillaume Nault | 3d60934 | 2018-06-04 18:52:19 +0200 | [diff] [blame] | 455 | |
| 456 | ps = l2tp_session_priv(session); |
| 457 | mutex_lock(&ps->sk_lock); |
| 458 | ps->__sk = rcu_dereference_protected(ps->sk, |
| 459 | lockdep_is_held(&ps->sk_lock)); |
| 460 | RCU_INIT_POINTER(ps->sk, NULL); |
| 461 | mutex_unlock(&ps->sk_lock); |
| 462 | call_rcu(&ps->rcu, pppol2tp_put_sk); |
| 463 | |
| 464 | /* Rely on the sock_put() call at the end of the function for |
| 465 | * dropping the reference held by pppol2tp_sock_to_session(). |
| 466 | * The last reference will be dropped by pppol2tp_put_sk(). |
| 467 | */ |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 468 | } |
James Chapman | d02ba2a | 2018-02-23 17:45:46 +0000 | [diff] [blame] | 469 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 470 | release_sock(sk); |
| 471 | |
| 472 | /* This will delete the session context via |
| 473 | * pppol2tp_session_destruct() if the socket's refcnt drops to |
| 474 | * zero. |
| 475 | */ |
| 476 | sock_put(sk); |
| 477 | |
| 478 | return 0; |
| 479 | |
| 480 | error: |
| 481 | release_sock(sk); |
| 482 | return error; |
| 483 | } |
| 484 | |
| 485 | static struct proto pppol2tp_sk_proto = { |
| 486 | .name = "PPPOL2TP", |
| 487 | .owner = THIS_MODULE, |
| 488 | .obj_size = sizeof(struct pppox_sock), |
| 489 | }; |
| 490 | |
| 491 | static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb) |
| 492 | { |
| 493 | int rc; |
| 494 | |
| 495 | rc = l2tp_udp_encap_recv(sk, skb); |
| 496 | if (rc) |
| 497 | kfree_skb(skb); |
| 498 | |
| 499 | return NET_RX_SUCCESS; |
| 500 | } |
| 501 | |
| 502 | /* socket() handler. Initialize a new struct sock. |
| 503 | */ |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 504 | static int pppol2tp_create(struct net *net, struct socket *sock, int kern) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 505 | { |
| 506 | int error = -ENOMEM; |
| 507 | struct sock *sk; |
| 508 | |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 509 | sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 510 | if (!sk) |
| 511 | goto out; |
| 512 | |
| 513 | sock_init_data(sock, sk); |
| 514 | |
| 515 | sock->state = SS_UNCONNECTED; |
| 516 | sock->ops = &pppol2tp_ops; |
| 517 | |
| 518 | sk->sk_backlog_rcv = pppol2tp_backlog_recv; |
| 519 | sk->sk_protocol = PX_PROTO_OL2TP; |
| 520 | sk->sk_family = PF_PPPOX; |
| 521 | sk->sk_state = PPPOX_NONE; |
| 522 | sk->sk_type = SOCK_STREAM; |
| 523 | sk->sk_destruct = pppol2tp_session_destruct; |
| 524 | |
| 525 | error = 0; |
| 526 | |
| 527 | out: |
| 528 | return error; |
| 529 | } |
| 530 | |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 531 | static void pppol2tp_show(struct seq_file *m, void *arg) |
| 532 | { |
| 533 | struct l2tp_session *session = arg; |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 534 | struct sock *sk; |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 535 | |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 536 | sk = pppol2tp_session_get_sock(session); |
| 537 | if (sk) { |
| 538 | struct pppox_sock *po = pppox_sk(sk); |
| 539 | |
| 540 | seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan)); |
| 541 | sock_put(sk); |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 544 | |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 545 | static void pppol2tp_session_init(struct l2tp_session *session) |
| 546 | { |
| 547 | struct pppol2tp_session *ps; |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 548 | |
| 549 | session->recv_skb = pppol2tp_recv; |
Arnd Bergmann | c2ebc25 | 2018-08-13 23:43:05 +0200 | [diff] [blame] | 550 | if (IS_ENABLED(CONFIG_L2TP_DEBUGFS)) |
| 551 | session->show = pppol2tp_show; |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 552 | |
| 553 | ps = l2tp_session_priv(session); |
| 554 | mutex_init(&ps->sk_lock); |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 555 | ps->owner = current->pid; |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 556 | } |
| 557 | |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 558 | struct l2tp_connect_info { |
| 559 | u8 version; |
| 560 | int fd; |
| 561 | u32 tunnel_id; |
| 562 | u32 peer_tunnel_id; |
| 563 | u32 session_id; |
| 564 | u32 peer_session_id; |
| 565 | }; |
| 566 | |
| 567 | static int pppol2tp_sockaddr_get_info(const void *sa, int sa_len, |
| 568 | struct l2tp_connect_info *info) |
| 569 | { |
| 570 | switch (sa_len) { |
| 571 | case sizeof(struct sockaddr_pppol2tp): |
| 572 | { |
| 573 | const struct sockaddr_pppol2tp *sa_v2in4 = sa; |
| 574 | |
| 575 | if (sa_v2in4->sa_protocol != PX_PROTO_OL2TP) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | info->version = 2; |
| 579 | info->fd = sa_v2in4->pppol2tp.fd; |
| 580 | info->tunnel_id = sa_v2in4->pppol2tp.s_tunnel; |
| 581 | info->peer_tunnel_id = sa_v2in4->pppol2tp.d_tunnel; |
| 582 | info->session_id = sa_v2in4->pppol2tp.s_session; |
| 583 | info->peer_session_id = sa_v2in4->pppol2tp.d_session; |
| 584 | |
| 585 | break; |
| 586 | } |
| 587 | case sizeof(struct sockaddr_pppol2tpv3): |
| 588 | { |
| 589 | const struct sockaddr_pppol2tpv3 *sa_v3in4 = sa; |
| 590 | |
| 591 | if (sa_v3in4->sa_protocol != PX_PROTO_OL2TP) |
| 592 | return -EINVAL; |
| 593 | |
| 594 | info->version = 3; |
| 595 | info->fd = sa_v3in4->pppol2tp.fd; |
| 596 | info->tunnel_id = sa_v3in4->pppol2tp.s_tunnel; |
| 597 | info->peer_tunnel_id = sa_v3in4->pppol2tp.d_tunnel; |
| 598 | info->session_id = sa_v3in4->pppol2tp.s_session; |
| 599 | info->peer_session_id = sa_v3in4->pppol2tp.d_session; |
| 600 | |
| 601 | break; |
| 602 | } |
| 603 | case sizeof(struct sockaddr_pppol2tpin6): |
| 604 | { |
| 605 | const struct sockaddr_pppol2tpin6 *sa_v2in6 = sa; |
| 606 | |
| 607 | if (sa_v2in6->sa_protocol != PX_PROTO_OL2TP) |
| 608 | return -EINVAL; |
| 609 | |
| 610 | info->version = 2; |
| 611 | info->fd = sa_v2in6->pppol2tp.fd; |
| 612 | info->tunnel_id = sa_v2in6->pppol2tp.s_tunnel; |
| 613 | info->peer_tunnel_id = sa_v2in6->pppol2tp.d_tunnel; |
| 614 | info->session_id = sa_v2in6->pppol2tp.s_session; |
| 615 | info->peer_session_id = sa_v2in6->pppol2tp.d_session; |
| 616 | |
| 617 | break; |
| 618 | } |
| 619 | case sizeof(struct sockaddr_pppol2tpv3in6): |
| 620 | { |
| 621 | const struct sockaddr_pppol2tpv3in6 *sa_v3in6 = sa; |
| 622 | |
| 623 | if (sa_v3in6->sa_protocol != PX_PROTO_OL2TP) |
| 624 | return -EINVAL; |
| 625 | |
| 626 | info->version = 3; |
| 627 | info->fd = sa_v3in6->pppol2tp.fd; |
| 628 | info->tunnel_id = sa_v3in6->pppol2tp.s_tunnel; |
| 629 | info->peer_tunnel_id = sa_v3in6->pppol2tp.d_tunnel; |
| 630 | info->session_id = sa_v3in6->pppol2tp.s_session; |
| 631 | info->peer_session_id = sa_v3in6->pppol2tp.d_session; |
| 632 | |
| 633 | break; |
| 634 | } |
| 635 | default: |
| 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
Guillaume Nault | 789141b | 2018-08-03 12:38:37 +0200 | [diff] [blame] | 642 | /* Rough estimation of the maximum payload size a tunnel can transmit without |
| 643 | * fragmenting at the lower IP layer. Assumes L2TPv2 with sequence |
| 644 | * numbers and no IP option. Not quite accurate, but the result is mostly |
| 645 | * unused anyway. |
| 646 | */ |
| 647 | static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel) |
| 648 | { |
| 649 | int mtu; |
| 650 | |
| 651 | mtu = l2tp_tunnel_dst_mtu(tunnel); |
| 652 | if (mtu <= PPPOL2TP_HEADER_OVERHEAD) |
| 653 | return 1500 - PPPOL2TP_HEADER_OVERHEAD; |
| 654 | |
| 655 | return mtu - PPPOL2TP_HEADER_OVERHEAD; |
| 656 | } |
| 657 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 658 | /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket |
| 659 | */ |
| 660 | static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, |
| 661 | int sockaddr_len, int flags) |
| 662 | { |
| 663 | struct sock *sk = sock->sk; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 664 | struct pppox_sock *po = pppox_sk(sk); |
| 665 | struct l2tp_session *session = NULL; |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 666 | struct l2tp_connect_info info; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 667 | struct l2tp_tunnel *tunnel; |
| 668 | struct pppol2tp_session *ps; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 669 | struct l2tp_session_cfg cfg = { 0, }; |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 670 | bool drop_refcnt = false; |
Guillaume Nault | f9e56ba | 2017-10-30 17:58:58 +0100 | [diff] [blame] | 671 | bool drop_tunnel = false; |
Guillaume Nault | bda06be | 2018-06-13 15:09:21 +0200 | [diff] [blame] | 672 | bool new_session = false; |
| 673 | bool new_tunnel = false; |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 674 | int error; |
| 675 | |
| 676 | error = pppol2tp_sockaddr_get_info(uservaddr, sockaddr_len, &info); |
| 677 | if (error < 0) |
| 678 | return error; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 679 | |
| 680 | lock_sock(sk); |
| 681 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 682 | /* Check for already bound sockets */ |
| 683 | error = -EBUSY; |
| 684 | if (sk->sk_state & PPPOX_CONNECTED) |
| 685 | goto end; |
| 686 | |
| 687 | /* We don't supporting rebinding anyway */ |
| 688 | error = -EALREADY; |
| 689 | if (sk->sk_user_data) |
| 690 | goto end; /* socket is already attached */ |
| 691 | |
James Chapman | e0d4435 | 2010-04-02 06:18:54 +0000 | [diff] [blame] | 692 | /* Don't bind if tunnel_id is 0 */ |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 693 | error = -EINVAL; |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 694 | if (!info.tunnel_id) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 695 | goto end; |
| 696 | |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 697 | tunnel = l2tp_tunnel_get(sock_net(sk), info.tunnel_id); |
Guillaume Nault | f9e56ba | 2017-10-30 17:58:58 +0100 | [diff] [blame] | 698 | if (tunnel) |
| 699 | drop_tunnel = true; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 700 | |
James Chapman | e0d4435 | 2010-04-02 06:18:54 +0000 | [diff] [blame] | 701 | /* Special case: create tunnel context if session_id and |
| 702 | * peer_session_id is 0. Otherwise look up tunnel using supplied |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 703 | * tunnel id. |
| 704 | */ |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 705 | if (!info.session_id && !info.peer_session_id) { |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 706 | if (!tunnel) { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 707 | struct l2tp_tunnel_cfg tcfg = { |
| 708 | .encap = L2TP_ENCAPTYPE_UDP, |
| 709 | .debug = 0, |
| 710 | }; |
Guillaume Nault | 3e1bc8b | 2018-06-13 15:09:20 +0200 | [diff] [blame] | 711 | |
| 712 | /* Prevent l2tp_tunnel_register() from trying to set up |
| 713 | * a kernel socket. |
| 714 | */ |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 715 | if (info.fd < 0) { |
Guillaume Nault | 3e1bc8b | 2018-06-13 15:09:20 +0200 | [diff] [blame] | 716 | error = -EBADF; |
| 717 | goto end; |
| 718 | } |
| 719 | |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 720 | error = l2tp_tunnel_create(sock_net(sk), info.fd, |
| 721 | info.version, |
| 722 | info.tunnel_id, |
| 723 | info.peer_tunnel_id, &tcfg, |
| 724 | &tunnel); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 725 | if (error < 0) |
| 726 | goto end; |
Guillaume Nault | 6b9f342 | 2018-04-10 21:01:12 +0200 | [diff] [blame] | 727 | |
| 728 | l2tp_tunnel_inc_refcount(tunnel); |
| 729 | error = l2tp_tunnel_register(tunnel, sock_net(sk), |
| 730 | &tcfg); |
| 731 | if (error < 0) { |
| 732 | kfree(tunnel); |
| 733 | goto end; |
| 734 | } |
| 735 | drop_tunnel = true; |
Guillaume Nault | bda06be | 2018-06-13 15:09:21 +0200 | [diff] [blame] | 736 | new_tunnel = true; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 737 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 738 | } else { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 739 | /* Error if we can't find the tunnel */ |
| 740 | error = -ENOENT; |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 741 | if (!tunnel) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 742 | goto end; |
| 743 | |
| 744 | /* Error if socket is not prepped */ |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 745 | if (!tunnel->sock) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 746 | goto end; |
| 747 | } |
| 748 | |
James Chapman | b79585f | 2012-04-29 21:48:50 +0000 | [diff] [blame] | 749 | if (tunnel->peer_tunnel_id == 0) |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 750 | tunnel->peer_tunnel_id = info.peer_tunnel_id; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 751 | |
Guillaume Nault | 01e28b9 | 2018-08-10 13:21:57 +0200 | [diff] [blame] | 752 | session = l2tp_tunnel_get_session(tunnel, info.session_id); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 753 | if (session) { |
| 754 | drop_refcnt = true; |
Guillaume Nault | 7ac6ab1 | 2018-06-13 15:09:19 +0200 | [diff] [blame] | 755 | |
| 756 | if (session->pwtype != L2TP_PWTYPE_PPP) { |
| 757 | error = -EPROTOTYPE; |
| 758 | goto end; |
| 759 | } |
| 760 | |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 761 | ps = l2tp_session_priv(session); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 762 | |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 763 | /* Using a pre-existing session is fine as long as it hasn't |
| 764 | * been connected yet. |
| 765 | */ |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 766 | mutex_lock(&ps->sk_lock); |
| 767 | if (rcu_dereference_protected(ps->sk, |
Guillaume Nault | 3d60934 | 2018-06-04 18:52:19 +0200 | [diff] [blame] | 768 | lockdep_is_held(&ps->sk_lock)) || |
| 769 | ps->__sk) { |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 770 | mutex_unlock(&ps->sk_lock); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 771 | error = -EEXIST; |
| 772 | goto end; |
| 773 | } |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 774 | } else { |
Guillaume Nault | 90904ff | 2018-06-13 15:09:18 +0200 | [diff] [blame] | 775 | cfg.pw_type = L2TP_PWTYPE_PPP; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 776 | |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 777 | session = l2tp_session_create(sizeof(struct pppol2tp_session), |
Guillaume Nault | a408194 | 2018-06-26 18:41:36 +0200 | [diff] [blame] | 778 | tunnel, info.session_id, |
| 779 | info.peer_session_id, &cfg); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 780 | if (IS_ERR(session)) { |
| 781 | error = PTR_ERR(session); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 782 | goto end; |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 783 | } |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 784 | |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 785 | pppol2tp_session_init(session); |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 786 | ps = l2tp_session_priv(session); |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 787 | l2tp_session_inc_refcount(session); |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 788 | |
| 789 | mutex_lock(&ps->sk_lock); |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 790 | error = l2tp_session_register(session, tunnel); |
| 791 | if (error < 0) { |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 792 | mutex_unlock(&ps->sk_lock); |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 793 | kfree(session); |
| 794 | goto end; |
| 795 | } |
| 796 | drop_refcnt = true; |
Guillaume Nault | bda06be | 2018-06-13 15:09:21 +0200 | [diff] [blame] | 797 | new_session = true; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 798 | } |
| 799 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 800 | /* Special case: if source & dest session_id == 0x0000, this |
| 801 | * socket is being created to manage the tunnel. Just set up |
| 802 | * the internal context for use by ioctl() and sockopt() |
| 803 | * handlers. |
| 804 | */ |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 805 | if (session->session_id == 0 && session->peer_session_id == 0) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 806 | error = 0; |
| 807 | goto out_no_ppp; |
| 808 | } |
| 809 | |
| 810 | /* The only header we need to worry about is the L2TP |
| 811 | * header. This size is different depending on whether |
| 812 | * sequence numbers are enabled for the data channel. |
| 813 | */ |
| 814 | po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; |
| 815 | |
| 816 | po->chan.private = sk; |
| 817 | po->chan.ops = &pppol2tp_chan_ops; |
Guillaume Nault | 789141b | 2018-08-03 12:38:37 +0200 | [diff] [blame] | 818 | po->chan.mtu = pppol2tp_tunnel_mtu(tunnel); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 819 | |
| 820 | error = ppp_register_net_channel(sock_net(sk), &po->chan); |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 821 | if (error) { |
| 822 | mutex_unlock(&ps->sk_lock); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 823 | goto end; |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 824 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 825 | |
| 826 | out_no_ppp: |
| 827 | /* This is how we get the session context from the socket. */ |
| 828 | sk->sk_user_data = session; |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 829 | rcu_assign_pointer(ps->sk, sk); |
| 830 | mutex_unlock(&ps->sk_lock); |
| 831 | |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 832 | /* Keep the reference we've grabbed on the session: sk doesn't expect |
| 833 | * the session to disappear. pppol2tp_session_destruct() is responsible |
| 834 | * for dropping it. |
| 835 | */ |
| 836 | drop_refcnt = false; |
| 837 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 838 | sk->sk_state = PPPOX_CONNECTED; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 839 | l2tp_info(session, L2TP_MSG_CONTROL, "%s: created\n", |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 840 | session->name); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 841 | |
| 842 | end: |
Guillaume Nault | bda06be | 2018-06-13 15:09:21 +0200 | [diff] [blame] | 843 | if (error) { |
| 844 | if (new_session) |
| 845 | l2tp_session_delete(session); |
| 846 | if (new_tunnel) |
| 847 | l2tp_tunnel_delete(tunnel); |
| 848 | } |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 849 | if (drop_refcnt) |
| 850 | l2tp_session_dec_refcount(session); |
Guillaume Nault | f9e56ba | 2017-10-30 17:58:58 +0100 | [diff] [blame] | 851 | if (drop_tunnel) |
| 852 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 853 | release_sock(sk); |
| 854 | |
| 855 | return error; |
| 856 | } |
| 857 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 858 | #ifdef CONFIG_L2TP_V3 |
| 859 | |
Guillaume Nault | f026bc2 | 2017-09-01 17:58:51 +0200 | [diff] [blame] | 860 | /* Called when creating sessions via the netlink interface. */ |
| 861 | static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel, |
| 862 | u32 session_id, u32 peer_session_id, |
| 863 | struct l2tp_session_cfg *cfg) |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 864 | { |
| 865 | int error; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 866 | struct l2tp_session *session; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 867 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 868 | /* Error if tunnel socket is not prepped */ |
Guillaume Nault | f026bc2 | 2017-09-01 17:58:51 +0200 | [diff] [blame] | 869 | if (!tunnel->sock) { |
| 870 | error = -ENOENT; |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 871 | goto err; |
Guillaume Nault | f026bc2 | 2017-09-01 17:58:51 +0200 | [diff] [blame] | 872 | } |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 873 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 874 | /* Allocate and initialize a new session context. */ |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 875 | session = l2tp_session_create(sizeof(struct pppol2tp_session), |
| 876 | tunnel, session_id, |
| 877 | peer_session_id, cfg); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 878 | if (IS_ERR(session)) { |
| 879 | error = PTR_ERR(session); |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 880 | goto err; |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 881 | } |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 882 | |
Guillaume Nault | f98be6c | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 883 | pppol2tp_session_init(session); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 884 | |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 885 | error = l2tp_session_register(session, tunnel); |
| 886 | if (error < 0) |
| 887 | goto err_sess; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 888 | |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 889 | return 0; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 890 | |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 891 | err_sess: |
| 892 | kfree(session); |
| 893 | err: |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 894 | return error; |
| 895 | } |
| 896 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 897 | #endif /* CONFIG_L2TP_V3 */ |
| 898 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 899 | /* getname() support. |
| 900 | */ |
| 901 | static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 902 | int peer) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 903 | { |
James Chapman | e0d4435 | 2010-04-02 06:18:54 +0000 | [diff] [blame] | 904 | int len = 0; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 905 | int error = 0; |
| 906 | struct l2tp_session *session; |
| 907 | struct l2tp_tunnel *tunnel; |
| 908 | struct sock *sk = sock->sk; |
| 909 | struct inet_sock *inet; |
| 910 | struct pppol2tp_session *pls; |
| 911 | |
| 912 | error = -ENOTCONN; |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 913 | if (!sk) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 914 | goto end; |
Gao Feng | 56cff47 | 2016-08-19 13:36:23 +0800 | [diff] [blame] | 915 | if (!(sk->sk_state & PPPOX_CONNECTED)) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 916 | goto end; |
| 917 | |
| 918 | error = -EBADF; |
| 919 | session = pppol2tp_sock_to_session(sk); |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 920 | if (!session) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 921 | goto end; |
| 922 | |
| 923 | pls = l2tp_session_priv(session); |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 924 | tunnel = session->tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 925 | |
Benjamin LaHaise | bbdb32c | 2012-03-20 03:57:54 +0000 | [diff] [blame] | 926 | inet = inet_sk(tunnel->sock); |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 927 | if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET) { |
James Chapman | e0d4435 | 2010-04-02 06:18:54 +0000 | [diff] [blame] | 928 | struct sockaddr_pppol2tp sp; |
Tom Parkin | b71a61c | 2020-07-22 17:32:05 +0100 | [diff] [blame] | 929 | |
James Chapman | e0d4435 | 2010-04-02 06:18:54 +0000 | [diff] [blame] | 930 | len = sizeof(sp); |
| 931 | memset(&sp, 0, len); |
| 932 | sp.sa_family = AF_PPPOX; |
| 933 | sp.sa_protocol = PX_PROTO_OL2TP; |
| 934 | sp.pppol2tp.fd = tunnel->fd; |
| 935 | sp.pppol2tp.pid = pls->owner; |
| 936 | sp.pppol2tp.s_tunnel = tunnel->tunnel_id; |
| 937 | sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; |
| 938 | sp.pppol2tp.s_session = session->session_id; |
| 939 | sp.pppol2tp.d_session = session->peer_session_id; |
| 940 | sp.pppol2tp.addr.sin_family = AF_INET; |
| 941 | sp.pppol2tp.addr.sin_port = inet->inet_dport; |
| 942 | sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr; |
| 943 | memcpy(uaddr, &sp, len); |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 944 | #if IS_ENABLED(CONFIG_IPV6) |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 945 | } else if (tunnel->version == 2 && tunnel->sock->sk_family == AF_INET6) { |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 946 | struct sockaddr_pppol2tpin6 sp; |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 947 | |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 948 | len = sizeof(sp); |
| 949 | memset(&sp, 0, len); |
| 950 | sp.sa_family = AF_PPPOX; |
| 951 | sp.sa_protocol = PX_PROTO_OL2TP; |
| 952 | sp.pppol2tp.fd = tunnel->fd; |
| 953 | sp.pppol2tp.pid = pls->owner; |
| 954 | sp.pppol2tp.s_tunnel = tunnel->tunnel_id; |
| 955 | sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; |
| 956 | sp.pppol2tp.s_session = session->session_id; |
| 957 | sp.pppol2tp.d_session = session->peer_session_id; |
| 958 | sp.pppol2tp.addr.sin6_family = AF_INET6; |
| 959 | sp.pppol2tp.addr.sin6_port = inet->inet_dport; |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 960 | memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr, |
| 961 | sizeof(tunnel->sock->sk_v6_daddr)); |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 962 | memcpy(uaddr, &sp, len); |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 963 | } else if (tunnel->version == 3 && tunnel->sock->sk_family == AF_INET6) { |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 964 | struct sockaddr_pppol2tpv3in6 sp; |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 965 | |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 966 | len = sizeof(sp); |
| 967 | memset(&sp, 0, len); |
| 968 | sp.sa_family = AF_PPPOX; |
| 969 | sp.sa_protocol = PX_PROTO_OL2TP; |
| 970 | sp.pppol2tp.fd = tunnel->fd; |
| 971 | sp.pppol2tp.pid = pls->owner; |
| 972 | sp.pppol2tp.s_tunnel = tunnel->tunnel_id; |
| 973 | sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; |
| 974 | sp.pppol2tp.s_session = session->session_id; |
| 975 | sp.pppol2tp.d_session = session->peer_session_id; |
| 976 | sp.pppol2tp.addr.sin6_family = AF_INET6; |
| 977 | sp.pppol2tp.addr.sin6_port = inet->inet_dport; |
Eric Dumazet | efe4208 | 2013-10-03 15:42:29 -0700 | [diff] [blame] | 978 | memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr, |
| 979 | sizeof(tunnel->sock->sk_v6_daddr)); |
Benjamin LaHaise | d2cf336 | 2012-04-27 08:24:18 +0000 | [diff] [blame] | 980 | memcpy(uaddr, &sp, len); |
| 981 | #endif |
James Chapman | e0d4435 | 2010-04-02 06:18:54 +0000 | [diff] [blame] | 982 | } else if (tunnel->version == 3) { |
| 983 | struct sockaddr_pppol2tpv3 sp; |
Tom Parkin | b71a61c | 2020-07-22 17:32:05 +0100 | [diff] [blame] | 984 | |
James Chapman | e0d4435 | 2010-04-02 06:18:54 +0000 | [diff] [blame] | 985 | len = sizeof(sp); |
| 986 | memset(&sp, 0, len); |
| 987 | sp.sa_family = AF_PPPOX; |
| 988 | sp.sa_protocol = PX_PROTO_OL2TP; |
| 989 | sp.pppol2tp.fd = tunnel->fd; |
| 990 | sp.pppol2tp.pid = pls->owner; |
| 991 | sp.pppol2tp.s_tunnel = tunnel->tunnel_id; |
| 992 | sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id; |
| 993 | sp.pppol2tp.s_session = session->session_id; |
| 994 | sp.pppol2tp.d_session = session->peer_session_id; |
| 995 | sp.pppol2tp.addr.sin_family = AF_INET; |
| 996 | sp.pppol2tp.addr.sin_port = inet->inet_dport; |
| 997 | sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr; |
| 998 | memcpy(uaddr, &sp, len); |
| 999 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1000 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1001 | error = len; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1002 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1003 | sock_put(sk); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1004 | end: |
| 1005 | return error; |
| 1006 | } |
| 1007 | |
| 1008 | /**************************************************************************** |
| 1009 | * ioctl() handlers. |
| 1010 | * |
| 1011 | * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP |
| 1012 | * sockets. However, in order to control kernel tunnel features, we allow |
| 1013 | * userspace to create a special "tunnel" PPPoX socket which is used for |
| 1014 | * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow |
| 1015 | * the user application to issue L2TP setsockopt(), getsockopt() and ioctl() |
| 1016 | * calls. |
| 1017 | ****************************************************************************/ |
| 1018 | |
| 1019 | static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest, |
Guillaume Nault | 7390ed8 | 2018-08-10 13:22:02 +0200 | [diff] [blame] | 1020 | const struct l2tp_stats *stats) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1021 | { |
Guillaume Nault | 7390ed8 | 2018-08-10 13:22:02 +0200 | [diff] [blame] | 1022 | memset(dest, 0, sizeof(*dest)); |
| 1023 | |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 1024 | dest->tx_packets = atomic_long_read(&stats->tx_packets); |
| 1025 | dest->tx_bytes = atomic_long_read(&stats->tx_bytes); |
| 1026 | dest->tx_errors = atomic_long_read(&stats->tx_errors); |
| 1027 | dest->rx_packets = atomic_long_read(&stats->rx_packets); |
| 1028 | dest->rx_bytes = atomic_long_read(&stats->rx_bytes); |
| 1029 | dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards); |
| 1030 | dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets); |
| 1031 | dest->rx_errors = atomic_long_read(&stats->rx_errors); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Guillaume Nault | 528534f | 2018-08-10 13:22:00 +0200 | [diff] [blame] | 1034 | static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats, |
| 1035 | struct l2tp_tunnel *tunnel) |
| 1036 | { |
| 1037 | struct l2tp_session *session; |
| 1038 | |
| 1039 | if (!stats->session_id) { |
Guillaume Nault | 528534f | 2018-08-10 13:22:00 +0200 | [diff] [blame] | 1040 | pppol2tp_copy_stats(stats, &tunnel->stats); |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | /* If session_id is set, search the corresponding session in the |
| 1045 | * context of this tunnel and record the session's statistics. |
| 1046 | */ |
| 1047 | session = l2tp_tunnel_get_session(tunnel, stats->session_id); |
| 1048 | if (!session) |
| 1049 | return -EBADR; |
| 1050 | |
| 1051 | if (session->pwtype != L2TP_PWTYPE_PPP) { |
| 1052 | l2tp_session_dec_refcount(session); |
| 1053 | return -EBADR; |
| 1054 | } |
| 1055 | |
Guillaume Nault | 528534f | 2018-08-10 13:22:00 +0200 | [diff] [blame] | 1056 | pppol2tp_copy_stats(stats, &session->stats); |
| 1057 | l2tp_session_dec_refcount(session); |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1062 | static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd, |
| 1063 | unsigned long arg) |
| 1064 | { |
Guillaume Nault | 528534f | 2018-08-10 13:22:00 +0200 | [diff] [blame] | 1065 | struct pppol2tp_ioc_stats stats; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1066 | struct l2tp_session *session; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1067 | |
Guillaume Nault | 79e6760 | 2018-08-10 13:21:58 +0200 | [diff] [blame] | 1068 | switch (cmd) { |
| 1069 | case PPPIOCGMRU: |
| 1070 | case PPPIOCGFLAGS: |
| 1071 | session = sock->sk->sk_user_data; |
| 1072 | if (!session) |
| 1073 | return -ENOTCONN; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1074 | |
Guillaume Nault | 79e6760 | 2018-08-10 13:21:58 +0200 | [diff] [blame] | 1075 | /* Not defined for tunnels */ |
| 1076 | if (!session->session_id && !session->peer_session_id) |
| 1077 | return -ENOSYS; |
Guillaume Nault | bdd0292 | 2018-08-10 13:21:58 +0200 | [diff] [blame] | 1078 | |
Guillaume Nault | 79e6760 | 2018-08-10 13:21:58 +0200 | [diff] [blame] | 1079 | if (put_user(0, (int __user *)arg)) |
| 1080 | return -EFAULT; |
| 1081 | break; |
| 1082 | |
| 1083 | case PPPIOCSMRU: |
| 1084 | case PPPIOCSFLAGS: |
| 1085 | session = sock->sk->sk_user_data; |
| 1086 | if (!session) |
| 1087 | return -ENOTCONN; |
| 1088 | |
| 1089 | /* Not defined for tunnels */ |
| 1090 | if (!session->session_id && !session->peer_session_id) |
| 1091 | return -ENOSYS; |
| 1092 | |
Jakub Kicinski | 503c018 | 2019-04-17 13:51:55 -0700 | [diff] [blame] | 1093 | if (!access_ok((int __user *)arg, sizeof(int))) |
Guillaume Nault | 79e6760 | 2018-08-10 13:21:58 +0200 | [diff] [blame] | 1094 | return -EFAULT; |
| 1095 | break; |
| 1096 | |
| 1097 | case PPPIOCGL2TPSTATS: |
| 1098 | session = sock->sk->sk_user_data; |
| 1099 | if (!session) |
| 1100 | return -ENOTCONN; |
| 1101 | |
| 1102 | /* Session 0 represents the parent tunnel */ |
Guillaume Nault | 528534f | 2018-08-10 13:22:00 +0200 | [diff] [blame] | 1103 | if (!session->session_id && !session->peer_session_id) { |
| 1104 | u32 session_id; |
| 1105 | int err; |
| 1106 | |
| 1107 | if (copy_from_user(&stats, (void __user *)arg, |
| 1108 | sizeof(stats))) |
| 1109 | return -EFAULT; |
| 1110 | |
| 1111 | session_id = stats.session_id; |
| 1112 | err = pppol2tp_tunnel_copy_stats(&stats, |
| 1113 | session->tunnel); |
| 1114 | if (err < 0) |
| 1115 | return err; |
| 1116 | |
| 1117 | stats.session_id = session_id; |
| 1118 | } else { |
Guillaume Nault | b0e2906 | 2018-08-10 13:22:01 +0200 | [diff] [blame] | 1119 | pppol2tp_copy_stats(&stats, &session->stats); |
| 1120 | stats.session_id = session->session_id; |
Guillaume Nault | 528534f | 2018-08-10 13:22:00 +0200 | [diff] [blame] | 1121 | } |
| 1122 | stats.tunnel_id = session->tunnel->tunnel_id; |
| 1123 | stats.using_ipsec = l2tp_tunnel_uses_xfrm(session->tunnel); |
| 1124 | |
| 1125 | if (copy_to_user((void __user *)arg, &stats, sizeof(stats))) |
| 1126 | return -EFAULT; |
Guillaume Nault | 79e6760 | 2018-08-10 13:21:58 +0200 | [diff] [blame] | 1127 | break; |
| 1128 | |
| 1129 | default: |
Guillaume Nault | 4f5f85e | 2018-08-10 13:22:03 +0200 | [diff] [blame] | 1130 | return -ENOIOCTLCMD; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Guillaume Nault | 79e6760 | 2018-08-10 13:21:58 +0200 | [diff] [blame] | 1133 | return 0; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /***************************************************************************** |
| 1137 | * setsockopt() / getsockopt() support. |
| 1138 | * |
| 1139 | * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP |
| 1140 | * sockets. In order to control kernel tunnel features, we allow userspace to |
| 1141 | * create a special "tunnel" PPPoX socket which is used for control only. |
| 1142 | * Tunnel PPPoX sockets have session_id == 0 and simply allow the user |
| 1143 | * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls. |
| 1144 | *****************************************************************************/ |
| 1145 | |
| 1146 | /* Tunnel setsockopt() helper. |
| 1147 | */ |
| 1148 | static int pppol2tp_tunnel_setsockopt(struct sock *sk, |
| 1149 | struct l2tp_tunnel *tunnel, |
| 1150 | int optname, int val) |
| 1151 | { |
| 1152 | int err = 0; |
| 1153 | |
| 1154 | switch (optname) { |
| 1155 | case PPPOL2TP_SO_DEBUG: |
| 1156 | tunnel->debug = val; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1157 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: set debug=%x\n", |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1158 | tunnel->name, tunnel->debug); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1159 | break; |
| 1160 | |
| 1161 | default: |
| 1162 | err = -ENOPROTOOPT; |
| 1163 | break; |
| 1164 | } |
| 1165 | |
| 1166 | return err; |
| 1167 | } |
| 1168 | |
| 1169 | /* Session setsockopt helper. |
| 1170 | */ |
| 1171 | static int pppol2tp_session_setsockopt(struct sock *sk, |
| 1172 | struct l2tp_session *session, |
| 1173 | int optname, int val) |
| 1174 | { |
| 1175 | int err = 0; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1176 | |
| 1177 | switch (optname) { |
| 1178 | case PPPOL2TP_SO_RECVSEQ: |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 1179 | if (val != 0 && val != 1) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1180 | err = -EINVAL; |
| 1181 | break; |
| 1182 | } |
Asbjørn Sloth Tønnesen | 3f9b977 | 2016-11-07 20:39:28 +0000 | [diff] [blame] | 1183 | session->recv_seq = !!val; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1184 | l2tp_info(session, L2TP_MSG_CONTROL, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1185 | "%s: set recv_seq=%d\n", |
| 1186 | session->name, session->recv_seq); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1187 | break; |
| 1188 | |
| 1189 | case PPPOL2TP_SO_SENDSEQ: |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 1190 | if (val != 0 && val != 1) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1191 | err = -EINVAL; |
| 1192 | break; |
| 1193 | } |
Asbjørn Sloth Tønnesen | 3f9b977 | 2016-11-07 20:39:28 +0000 | [diff] [blame] | 1194 | session->send_seq = !!val; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1195 | { |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 1196 | struct pppox_sock *po = pppox_sk(sk); |
| 1197 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1198 | po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ : |
| 1199 | PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; |
| 1200 | } |
Guillaume Nault | bb5016e | 2014-03-06 11:14:30 +0100 | [diff] [blame] | 1201 | l2tp_session_set_header_len(session, session->tunnel->version); |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1202 | l2tp_info(session, L2TP_MSG_CONTROL, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1203 | "%s: set send_seq=%d\n", |
| 1204 | session->name, session->send_seq); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1205 | break; |
| 1206 | |
| 1207 | case PPPOL2TP_SO_LNSMODE: |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 1208 | if (val != 0 && val != 1) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1209 | err = -EINVAL; |
| 1210 | break; |
| 1211 | } |
Asbjørn Sloth Tønnesen | 3f9b977 | 2016-11-07 20:39:28 +0000 | [diff] [blame] | 1212 | session->lns_mode = !!val; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1213 | l2tp_info(session, L2TP_MSG_CONTROL, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1214 | "%s: set lns_mode=%d\n", |
| 1215 | session->name, session->lns_mode); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | |
| 1218 | case PPPOL2TP_SO_DEBUG: |
| 1219 | session->debug = val; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1220 | l2tp_info(session, L2TP_MSG_CONTROL, "%s: set debug=%x\n", |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1221 | session->name, session->debug); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1222 | break; |
| 1223 | |
| 1224 | case PPPOL2TP_SO_REORDERTO: |
| 1225 | session->reorder_timeout = msecs_to_jiffies(val); |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1226 | l2tp_info(session, L2TP_MSG_CONTROL, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1227 | "%s: set reorder_timeout=%d\n", |
| 1228 | session->name, session->reorder_timeout); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1229 | break; |
| 1230 | |
| 1231 | default: |
| 1232 | err = -ENOPROTOOPT; |
| 1233 | break; |
| 1234 | } |
| 1235 | |
| 1236 | return err; |
| 1237 | } |
| 1238 | |
| 1239 | /* Main setsockopt() entry point. |
| 1240 | * Does API checks, then calls either the tunnel or session setsockopt |
| 1241 | * handler, according to whether the PPPoL2TP socket is a for a regular |
| 1242 | * session or the special tunnel type. |
| 1243 | */ |
| 1244 | static int pppol2tp_setsockopt(struct socket *sock, int level, int optname, |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame^] | 1245 | sockptr_t optval, unsigned int optlen) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1246 | { |
| 1247 | struct sock *sk = sock->sk; |
| 1248 | struct l2tp_session *session; |
| 1249 | struct l2tp_tunnel *tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1250 | int val; |
| 1251 | int err; |
| 1252 | |
| 1253 | if (level != SOL_PPPOL2TP) |
Sasha Levin | 3cf521f | 2014-07-14 17:02:31 -0700 | [diff] [blame] | 1254 | return -EINVAL; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1255 | |
| 1256 | if (optlen < sizeof(int)) |
| 1257 | return -EINVAL; |
| 1258 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame^] | 1259 | if (copy_from_sockptr(&val, optval, sizeof(int))) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1260 | return -EFAULT; |
| 1261 | |
| 1262 | err = -ENOTCONN; |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 1263 | if (!sk->sk_user_data) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1264 | goto end; |
| 1265 | |
| 1266 | /* Get session context from the socket */ |
| 1267 | err = -EBADF; |
| 1268 | session = pppol2tp_sock_to_session(sk); |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 1269 | if (!session) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1270 | goto end; |
| 1271 | |
| 1272 | /* Special case: if session_id == 0x0000, treat as operation on tunnel |
| 1273 | */ |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 1274 | if (session->session_id == 0 && session->peer_session_id == 0) { |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 1275 | tunnel = session->tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1276 | err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val); |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 1277 | } else { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1278 | err = pppol2tp_session_setsockopt(sk, session, optname, val); |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 1279 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1280 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1281 | sock_put(sk); |
| 1282 | end: |
| 1283 | return err; |
| 1284 | } |
| 1285 | |
| 1286 | /* Tunnel getsockopt helper. Called with sock locked. |
| 1287 | */ |
| 1288 | static int pppol2tp_tunnel_getsockopt(struct sock *sk, |
| 1289 | struct l2tp_tunnel *tunnel, |
| 1290 | int optname, int *val) |
| 1291 | { |
| 1292 | int err = 0; |
| 1293 | |
| 1294 | switch (optname) { |
| 1295 | case PPPOL2TP_SO_DEBUG: |
| 1296 | *val = tunnel->debug; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1297 | l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: get debug=%x\n", |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1298 | tunnel->name, tunnel->debug); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1299 | break; |
| 1300 | |
| 1301 | default: |
| 1302 | err = -ENOPROTOOPT; |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | return err; |
| 1307 | } |
| 1308 | |
| 1309 | /* Session getsockopt helper. Called with sock locked. |
| 1310 | */ |
| 1311 | static int pppol2tp_session_getsockopt(struct sock *sk, |
| 1312 | struct l2tp_session *session, |
| 1313 | int optname, int *val) |
| 1314 | { |
| 1315 | int err = 0; |
| 1316 | |
| 1317 | switch (optname) { |
| 1318 | case PPPOL2TP_SO_RECVSEQ: |
| 1319 | *val = session->recv_seq; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1320 | l2tp_info(session, L2TP_MSG_CONTROL, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1321 | "%s: get recv_seq=%d\n", session->name, *val); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1322 | break; |
| 1323 | |
| 1324 | case PPPOL2TP_SO_SENDSEQ: |
| 1325 | *val = session->send_seq; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1326 | l2tp_info(session, L2TP_MSG_CONTROL, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1327 | "%s: get send_seq=%d\n", session->name, *val); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1328 | break; |
| 1329 | |
| 1330 | case PPPOL2TP_SO_LNSMODE: |
| 1331 | *val = session->lns_mode; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1332 | l2tp_info(session, L2TP_MSG_CONTROL, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1333 | "%s: get lns_mode=%d\n", session->name, *val); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | |
| 1336 | case PPPOL2TP_SO_DEBUG: |
| 1337 | *val = session->debug; |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1338 | l2tp_info(session, L2TP_MSG_CONTROL, "%s: get debug=%d\n", |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1339 | session->name, *val); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | |
| 1342 | case PPPOL2TP_SO_REORDERTO: |
Tom Parkin | b71a61c | 2020-07-22 17:32:05 +0100 | [diff] [blame] | 1343 | *val = (int)jiffies_to_msecs(session->reorder_timeout); |
Asbjørn Sloth Tønnesen | fba40c6 | 2016-12-11 00:18:59 +0000 | [diff] [blame] | 1344 | l2tp_info(session, L2TP_MSG_CONTROL, |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1345 | "%s: get reorder_timeout=%d\n", session->name, *val); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1346 | break; |
| 1347 | |
| 1348 | default: |
| 1349 | err = -ENOPROTOOPT; |
| 1350 | } |
| 1351 | |
| 1352 | return err; |
| 1353 | } |
| 1354 | |
| 1355 | /* Main getsockopt() entry point. |
| 1356 | * Does API checks, then calls either the tunnel or session getsockopt |
| 1357 | * handler, according to whether the PPPoX socket is a for a regular session |
| 1358 | * or the special tunnel type. |
| 1359 | */ |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 1360 | static int pppol2tp_getsockopt(struct socket *sock, int level, int optname, |
| 1361 | char __user *optval, int __user *optlen) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1362 | { |
| 1363 | struct sock *sk = sock->sk; |
| 1364 | struct l2tp_session *session; |
| 1365 | struct l2tp_tunnel *tunnel; |
| 1366 | int val, len; |
| 1367 | int err; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1368 | |
| 1369 | if (level != SOL_PPPOL2TP) |
Sasha Levin | 3cf521f | 2014-07-14 17:02:31 -0700 | [diff] [blame] | 1370 | return -EINVAL; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1371 | |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 1372 | if (get_user(len, optlen)) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1373 | return -EFAULT; |
| 1374 | |
| 1375 | len = min_t(unsigned int, len, sizeof(int)); |
| 1376 | |
| 1377 | if (len < 0) |
| 1378 | return -EINVAL; |
| 1379 | |
| 1380 | err = -ENOTCONN; |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 1381 | if (!sk->sk_user_data) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1382 | goto end; |
| 1383 | |
| 1384 | /* Get the session context */ |
| 1385 | err = -EBADF; |
| 1386 | session = pppol2tp_sock_to_session(sk); |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 1387 | if (!session) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1388 | goto end; |
| 1389 | |
| 1390 | /* Special case: if session_id == 0x0000, treat as operation on tunnel */ |
Tom Parkin | 6c0ec37 | 2020-07-23 12:29:51 +0100 | [diff] [blame] | 1391 | if (session->session_id == 0 && session->peer_session_id == 0) { |
Guillaume Nault | 7198c77 | 2017-11-11 06:06:31 +0900 | [diff] [blame] | 1392 | tunnel = session->tunnel; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1393 | err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val); |
Guillaume Nault | 321a52a | 2017-04-06 18:31:21 +0200 | [diff] [blame] | 1394 | if (err) |
| 1395 | goto end_put_sess; |
| 1396 | } else { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1397 | err = pppol2tp_session_getsockopt(sk, session, optname, &val); |
Guillaume Nault | 321a52a | 2017-04-06 18:31:21 +0200 | [diff] [blame] | 1398 | if (err) |
| 1399 | goto end_put_sess; |
| 1400 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1401 | |
| 1402 | err = -EFAULT; |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 1403 | if (put_user(len, optlen)) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1404 | goto end_put_sess; |
| 1405 | |
Tom Parkin | b71a61c | 2020-07-22 17:32:05 +0100 | [diff] [blame] | 1406 | if (copy_to_user((void __user *)optval, &val, len)) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1407 | goto end_put_sess; |
| 1408 | |
| 1409 | err = 0; |
| 1410 | |
| 1411 | end_put_sess: |
| 1412 | sock_put(sk); |
| 1413 | end: |
| 1414 | return err; |
| 1415 | } |
| 1416 | |
| 1417 | /***************************************************************************** |
| 1418 | * /proc filesystem for debug |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1419 | * Since the original pppol2tp driver provided /proc/net/pppol2tp for |
| 1420 | * L2TPv2, we dump only L2TPv2 tunnels and sessions here. |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1421 | *****************************************************************************/ |
| 1422 | |
| 1423 | static unsigned int pppol2tp_net_id; |
| 1424 | |
| 1425 | #ifdef CONFIG_PROC_FS |
| 1426 | |
| 1427 | struct pppol2tp_seq_data { |
| 1428 | struct seq_net_private p; |
| 1429 | int tunnel_idx; /* current tunnel */ |
| 1430 | int session_idx; /* index of session within current tunnel */ |
| 1431 | struct l2tp_tunnel *tunnel; |
| 1432 | struct l2tp_session *session; /* NULL means get next tunnel */ |
| 1433 | }; |
| 1434 | |
| 1435 | static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd) |
| 1436 | { |
Guillaume Nault | 0e0c3fe | 2018-04-12 20:50:34 +0200 | [diff] [blame] | 1437 | /* Drop reference taken during previous invocation */ |
| 1438 | if (pd->tunnel) |
| 1439 | l2tp_tunnel_dec_refcount(pd->tunnel); |
| 1440 | |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1441 | for (;;) { |
Guillaume Nault | 0e0c3fe | 2018-04-12 20:50:34 +0200 | [diff] [blame] | 1442 | pd->tunnel = l2tp_tunnel_get_nth(net, pd->tunnel_idx); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1443 | pd->tunnel_idx++; |
| 1444 | |
Guillaume Nault | 0e0c3fe | 2018-04-12 20:50:34 +0200 | [diff] [blame] | 1445 | /* Only accept L2TPv2 tunnels */ |
| 1446 | if (!pd->tunnel || pd->tunnel->version == 2) |
| 1447 | return; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1448 | |
Guillaume Nault | 0e0c3fe | 2018-04-12 20:50:34 +0200 | [diff] [blame] | 1449 | l2tp_tunnel_dec_refcount(pd->tunnel); |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1450 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd) |
| 1454 | { |
Guillaume Nault | 8349440 | 2018-04-25 19:54:14 +0200 | [diff] [blame] | 1455 | /* Drop reference taken during previous invocation */ |
| 1456 | if (pd->session) |
| 1457 | l2tp_session_dec_refcount(pd->session); |
| 1458 | |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 1459 | pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1460 | pd->session_idx++; |
James Chapman | f7faffa | 2010-04-02 06:18:49 +0000 | [diff] [blame] | 1461 | |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 1462 | if (!pd->session) { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1463 | pd->session_idx = 0; |
| 1464 | pppol2tp_next_tunnel(net, pd); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs) |
| 1469 | { |
| 1470 | struct pppol2tp_seq_data *pd = SEQ_START_TOKEN; |
| 1471 | loff_t pos = *offs; |
| 1472 | struct net *net; |
| 1473 | |
| 1474 | if (!pos) |
| 1475 | goto out; |
| 1476 | |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 1477 | BUG_ON(!m->private); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1478 | pd = m->private; |
| 1479 | net = seq_file_net(m); |
| 1480 | |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 1481 | if (!pd->tunnel) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1482 | pppol2tp_next_tunnel(net, pd); |
| 1483 | else |
| 1484 | pppol2tp_next_session(net, pd); |
| 1485 | |
| 1486 | /* NULL tunnel and session indicates end of list */ |
Tom Parkin | 0febc7b | 2020-07-23 12:29:50 +0100 | [diff] [blame] | 1487 | if (!pd->tunnel && !pd->session) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1488 | pd = NULL; |
| 1489 | |
| 1490 | out: |
| 1491 | return pd; |
| 1492 | } |
| 1493 | |
| 1494 | static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 1495 | { |
| 1496 | (*pos)++; |
| 1497 | return NULL; |
| 1498 | } |
| 1499 | |
| 1500 | static void pppol2tp_seq_stop(struct seq_file *p, void *v) |
| 1501 | { |
Guillaume Nault | 0e0c3fe | 2018-04-12 20:50:34 +0200 | [diff] [blame] | 1502 | struct pppol2tp_seq_data *pd = v; |
| 1503 | |
| 1504 | if (!pd || pd == SEQ_START_TOKEN) |
| 1505 | return; |
| 1506 | |
Guillaume Nault | 8349440 | 2018-04-25 19:54:14 +0200 | [diff] [blame] | 1507 | /* Drop reference taken by last invocation of pppol2tp_next_session() |
| 1508 | * or pppol2tp_next_tunnel(). |
| 1509 | */ |
| 1510 | if (pd->session) { |
| 1511 | l2tp_session_dec_refcount(pd->session); |
| 1512 | pd->session = NULL; |
| 1513 | } |
Guillaume Nault | 5411b618 | 2018-04-19 16:20:48 +0200 | [diff] [blame] | 1514 | if (pd->tunnel) { |
Guillaume Nault | 0e0c3fe | 2018-04-12 20:50:34 +0200 | [diff] [blame] | 1515 | l2tp_tunnel_dec_refcount(pd->tunnel); |
Guillaume Nault | 5411b618 | 2018-04-19 16:20:48 +0200 | [diff] [blame] | 1516 | pd->tunnel = NULL; |
Guillaume Nault | 5411b618 | 2018-04-19 16:20:48 +0200 | [diff] [blame] | 1517 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v) |
| 1521 | { |
| 1522 | struct l2tp_tunnel *tunnel = v; |
| 1523 | |
| 1524 | seq_printf(m, "\nTUNNEL '%s', %c %d\n", |
| 1525 | tunnel->name, |
| 1526 | (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N', |
Reshetova, Elena | fbea9e0 | 2017-07-04 15:52:57 +0300 | [diff] [blame] | 1527 | refcount_read(&tunnel->ref_count) - 1); |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 1528 | seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n", |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1529 | tunnel->debug, |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 1530 | atomic_long_read(&tunnel->stats.tx_packets), |
| 1531 | atomic_long_read(&tunnel->stats.tx_bytes), |
| 1532 | atomic_long_read(&tunnel->stats.tx_errors), |
| 1533 | atomic_long_read(&tunnel->stats.rx_packets), |
| 1534 | atomic_long_read(&tunnel->stats.rx_bytes), |
| 1535 | atomic_long_read(&tunnel->stats.rx_errors)); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | static void pppol2tp_seq_session_show(struct seq_file *m, void *v) |
| 1539 | { |
| 1540 | struct l2tp_session *session = v; |
| 1541 | struct l2tp_tunnel *tunnel = session->tunnel; |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 1542 | unsigned char state; |
| 1543 | char user_data_ok; |
| 1544 | struct sock *sk; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1545 | u32 ip = 0; |
| 1546 | u16 port = 0; |
| 1547 | |
| 1548 | if (tunnel->sock) { |
| 1549 | struct inet_sock *inet = inet_sk(tunnel->sock); |
Tom Parkin | b71a61c | 2020-07-22 17:32:05 +0100 | [diff] [blame] | 1550 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1551 | ip = ntohl(inet->inet_saddr); |
| 1552 | port = ntohs(inet->inet_sport); |
| 1553 | } |
| 1554 | |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 1555 | sk = pppol2tp_session_get_sock(session); |
| 1556 | if (sk) { |
| 1557 | state = sk->sk_state; |
| 1558 | user_data_ok = (session == sk->sk_user_data) ? 'Y' : 'N'; |
| 1559 | } else { |
| 1560 | state = 0; |
| 1561 | user_data_ok = 'N'; |
| 1562 | } |
| 1563 | |
Tom Parkin | 9f7da9a | 2020-07-22 17:32:07 +0100 | [diff] [blame] | 1564 | seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> %04X/%04X %d %c\n", |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1565 | session->name, ip, port, |
| 1566 | tunnel->tunnel_id, |
| 1567 | session->session_id, |
| 1568 | tunnel->peer_tunnel_id, |
| 1569 | session->peer_session_id, |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 1570 | state, user_data_ok); |
Guillaume Nault | 789141b | 2018-08-03 12:38:37 +0200 | [diff] [blame] | 1571 | seq_printf(m, " 0/0/%c/%c/%s %08x %u\n", |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1572 | session->recv_seq ? 'R' : '-', |
| 1573 | session->send_seq ? 'S' : '-', |
| 1574 | session->lns_mode ? "LNS" : "LAC", |
| 1575 | session->debug, |
| 1576 | jiffies_to_msecs(session->reorder_timeout)); |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 1577 | seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n", |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1578 | session->nr, session->ns, |
Tom Parkin | 7b7c071 | 2013-03-19 06:11:22 +0000 | [diff] [blame] | 1579 | atomic_long_read(&session->stats.tx_packets), |
| 1580 | atomic_long_read(&session->stats.tx_bytes), |
| 1581 | atomic_long_read(&session->stats.tx_errors), |
| 1582 | atomic_long_read(&session->stats.rx_packets), |
| 1583 | atomic_long_read(&session->stats.rx_bytes), |
| 1584 | atomic_long_read(&session->stats.rx_errors)); |
James Chapman | 9345471 | 2010-04-02 06:18:44 +0000 | [diff] [blame] | 1585 | |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 1586 | if (sk) { |
| 1587 | struct pppox_sock *po = pppox_sk(sk); |
| 1588 | |
James Chapman | 9345471 | 2010-04-02 06:18:44 +0000 | [diff] [blame] | 1589 | seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan)); |
Guillaume Nault | ee40fb2e | 2017-10-27 16:51:52 +0200 | [diff] [blame] | 1590 | sock_put(sk); |
| 1591 | } |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | static int pppol2tp_seq_show(struct seq_file *m, void *v) |
| 1595 | { |
| 1596 | struct pppol2tp_seq_data *pd = v; |
| 1597 | |
| 1598 | /* display header on line 1 */ |
| 1599 | if (v == SEQ_START_TOKEN) { |
| 1600 | seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n"); |
| 1601 | seq_puts(m, "TUNNEL name, user-data-ok session-count\n"); |
| 1602 | seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); |
Tom Parkin | 9f7da9a | 2020-07-22 17:32:07 +0100 | [diff] [blame] | 1603 | seq_puts(m, " SESSION name, addr/port src-tid/sid dest-tid/sid state user-data-ok\n"); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1604 | seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n"); |
| 1605 | seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); |
| 1606 | goto out; |
| 1607 | } |
| 1608 | |
Guillaume Nault | 8349440 | 2018-04-25 19:54:14 +0200 | [diff] [blame] | 1609 | if (!pd->session) |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1610 | pppol2tp_seq_tunnel_show(m, pd->tunnel); |
Guillaume Nault | 8349440 | 2018-04-25 19:54:14 +0200 | [diff] [blame] | 1611 | else |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1612 | pppol2tp_seq_session_show(m, pd->session); |
| 1613 | |
| 1614 | out: |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | static const struct seq_operations pppol2tp_seq_ops = { |
| 1619 | .start = pppol2tp_seq_start, |
| 1620 | .next = pppol2tp_seq_next, |
| 1621 | .stop = pppol2tp_seq_stop, |
| 1622 | .show = pppol2tp_seq_show, |
| 1623 | }; |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1624 | #endif /* CONFIG_PROC_FS */ |
| 1625 | |
| 1626 | /***************************************************************************** |
| 1627 | * Network namespace |
| 1628 | *****************************************************************************/ |
| 1629 | |
| 1630 | static __net_init int pppol2tp_init_net(struct net *net) |
| 1631 | { |
| 1632 | struct proc_dir_entry *pde; |
| 1633 | int err = 0; |
| 1634 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 1635 | pde = proc_create_net("pppol2tp", 0444, net->proc_net, |
Tom Parkin | 8ce9825a | 2020-07-22 17:32:08 +0100 | [diff] [blame] | 1636 | &pppol2tp_seq_ops, sizeof(struct pppol2tp_seq_data)); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1637 | if (!pde) { |
| 1638 | err = -ENOMEM; |
| 1639 | goto out; |
| 1640 | } |
| 1641 | |
| 1642 | out: |
| 1643 | return err; |
| 1644 | } |
| 1645 | |
| 1646 | static __net_exit void pppol2tp_exit_net(struct net *net) |
| 1647 | { |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1648 | remove_proc_entry("pppol2tp", net->proc_net); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | static struct pernet_operations pppol2tp_net_ops = { |
| 1652 | .init = pppol2tp_init_net, |
| 1653 | .exit = pppol2tp_exit_net, |
| 1654 | .id = &pppol2tp_net_id, |
| 1655 | }; |
| 1656 | |
| 1657 | /***************************************************************************** |
| 1658 | * Init and cleanup |
| 1659 | *****************************************************************************/ |
| 1660 | |
| 1661 | static const struct proto_ops pppol2tp_ops = { |
| 1662 | .family = AF_PPPOX, |
| 1663 | .owner = THIS_MODULE, |
| 1664 | .release = pppol2tp_release, |
| 1665 | .bind = sock_no_bind, |
| 1666 | .connect = pppol2tp_connect, |
| 1667 | .socketpair = sock_no_socketpair, |
| 1668 | .accept = sock_no_accept, |
| 1669 | .getname = pppol2tp_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1670 | .poll = datagram_poll, |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1671 | .listen = sock_no_listen, |
| 1672 | .shutdown = sock_no_shutdown, |
| 1673 | .setsockopt = pppol2tp_setsockopt, |
| 1674 | .getsockopt = pppol2tp_getsockopt, |
| 1675 | .sendmsg = pppol2tp_sendmsg, |
| 1676 | .recvmsg = pppol2tp_recvmsg, |
| 1677 | .mmap = sock_no_mmap, |
| 1678 | .ioctl = pppox_ioctl, |
Arnd Bergmann | 055d882 | 2019-07-30 21:25:20 +0200 | [diff] [blame] | 1679 | #ifdef CONFIG_COMPAT |
| 1680 | .compat_ioctl = pppox_compat_ioctl, |
| 1681 | #endif |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1682 | }; |
| 1683 | |
Eric Dumazet | 756e64a | 2010-09-21 06:43:54 +0000 | [diff] [blame] | 1684 | static const struct pppox_proto pppol2tp_proto = { |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1685 | .create = pppol2tp_create, |
Wei Yongjun | e1558a9 | 2013-07-02 09:02:07 +0800 | [diff] [blame] | 1686 | .ioctl = pppol2tp_ioctl, |
| 1687 | .owner = THIS_MODULE, |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1688 | }; |
| 1689 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1690 | #ifdef CONFIG_L2TP_V3 |
| 1691 | |
| 1692 | static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = { |
| 1693 | .session_create = pppol2tp_session_create, |
Tom Parkin | cf2f5c8 | 2013-03-19 06:11:21 +0000 | [diff] [blame] | 1694 | .session_delete = l2tp_session_delete, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1695 | }; |
| 1696 | |
| 1697 | #endif /* CONFIG_L2TP_V3 */ |
| 1698 | |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1699 | static int __init pppol2tp_init(void) |
| 1700 | { |
| 1701 | int err; |
| 1702 | |
| 1703 | err = register_pernet_device(&pppol2tp_net_ops); |
| 1704 | if (err) |
| 1705 | goto out; |
| 1706 | |
| 1707 | err = proto_register(&pppol2tp_sk_proto, 0); |
| 1708 | if (err) |
| 1709 | goto out_unregister_pppol2tp_pernet; |
| 1710 | |
| 1711 | err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto); |
| 1712 | if (err) |
| 1713 | goto out_unregister_pppol2tp_proto; |
| 1714 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1715 | #ifdef CONFIG_L2TP_V3 |
| 1716 | err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops); |
| 1717 | if (err) |
| 1718 | goto out_unregister_pppox; |
| 1719 | #endif |
| 1720 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1721 | pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION); |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1722 | |
| 1723 | out: |
| 1724 | return err; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1725 | |
| 1726 | #ifdef CONFIG_L2TP_V3 |
| 1727 | out_unregister_pppox: |
| 1728 | unregister_pppox_proto(PX_PROTO_OL2TP); |
| 1729 | #endif |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1730 | out_unregister_pppol2tp_proto: |
| 1731 | proto_unregister(&pppol2tp_sk_proto); |
| 1732 | out_unregister_pppol2tp_pernet: |
| 1733 | unregister_pernet_device(&pppol2tp_net_ops); |
| 1734 | goto out; |
| 1735 | } |
| 1736 | |
| 1737 | static void __exit pppol2tp_exit(void) |
| 1738 | { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1739 | #ifdef CONFIG_L2TP_V3 |
| 1740 | l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP); |
| 1741 | #endif |
James Chapman | fd558d1 | 2010-04-02 06:18:33 +0000 | [diff] [blame] | 1742 | unregister_pppox_proto(PX_PROTO_OL2TP); |
| 1743 | proto_unregister(&pppol2tp_sk_proto); |
| 1744 | unregister_pernet_device(&pppol2tp_net_ops); |
| 1745 | } |
| 1746 | |
| 1747 | module_init(pppol2tp_init); |
| 1748 | module_exit(pppol2tp_exit); |
| 1749 | |
| 1750 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 1751 | MODULE_DESCRIPTION("PPP over L2TP over UDP"); |
| 1752 | MODULE_LICENSE("GPL"); |
| 1753 | MODULE_VERSION(PPPOL2TP_DRV_VERSION); |
Guillaume Nault | 681b4d8 | 2015-12-02 16:27:39 +0100 | [diff] [blame] | 1754 | MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OL2TP); |
Guillaume Nault | 249ee81 | 2017-04-03 13:23:15 +0200 | [diff] [blame] | 1755 | MODULE_ALIAS_L2TP_PWTYPE(7); |