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