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