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