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