Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v2.6.24 2496 lines 66 kB view raw
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: 1.0.0 8 * 9 * Authors: Martijn van Oosterhout <kleptog@svana.org> 10 * James Chapman (jchapman@katalix.com) 11 * Contributors: 12 * Michal Ostrowski <mostrows@speakeasy.net> 13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br> 14 * David S. Miller (davem@redhat.com) 15 * 16 * License: 17 * This program is free software; you can redistribute it and/or 18 * modify it under the terms of the GNU General Public License 19 * as published by the Free Software Foundation; either version 20 * 2 of the License, or (at your option) any later version. 21 * 22 */ 23 24/* This driver handles only L2TP data frames; control frames are handled by a 25 * userspace application. 26 * 27 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and 28 * attaches it to a bound UDP socket with local tunnel_id / session_id and 29 * peer tunnel_id / session_id set. Data can then be sent or received using 30 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket 31 * can be read or modified using ioctl() or [gs]etsockopt() calls. 32 * 33 * When a PPPoL2TP socket is connected with local and peer session_id values 34 * zero, the socket is treated as a special tunnel management socket. 35 * 36 * Here's example userspace code to create a socket for sending/receiving data 37 * over an L2TP session:- 38 * 39 * struct sockaddr_pppol2tp sax; 40 * int fd; 41 * int session_fd; 42 * 43 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); 44 * 45 * sax.sa_family = AF_PPPOX; 46 * sax.sa_protocol = PX_PROTO_OL2TP; 47 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket 48 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr; 49 * sax.pppol2tp.addr.sin_port = addr->sin_port; 50 * sax.pppol2tp.addr.sin_family = AF_INET; 51 * sax.pppol2tp.s_tunnel = tunnel_id; 52 * sax.pppol2tp.s_session = session_id; 53 * sax.pppol2tp.d_tunnel = peer_tunnel_id; 54 * sax.pppol2tp.d_session = peer_session_id; 55 * 56 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax)); 57 * 58 * A pppd plugin that allows PPP traffic to be carried over L2TP using 59 * this driver is available from the OpenL2TP project at 60 * http://openl2tp.sourceforge.net. 61 */ 62 63#include <linux/module.h> 64#include <linux/version.h> 65#include <linux/string.h> 66#include <linux/list.h> 67#include <asm/uaccess.h> 68 69#include <linux/kernel.h> 70#include <linux/spinlock.h> 71#include <linux/kthread.h> 72#include <linux/sched.h> 73#include <linux/slab.h> 74#include <linux/errno.h> 75#include <linux/jiffies.h> 76 77#include <linux/netdevice.h> 78#include <linux/net.h> 79#include <linux/inetdevice.h> 80#include <linux/skbuff.h> 81#include <linux/init.h> 82#include <linux/ip.h> 83#include <linux/udp.h> 84#include <linux/if_pppox.h> 85#include <linux/if_pppol2tp.h> 86#include <net/sock.h> 87#include <linux/ppp_channel.h> 88#include <linux/ppp_defs.h> 89#include <linux/if_ppp.h> 90#include <linux/file.h> 91#include <linux/hash.h> 92#include <linux/sort.h> 93#include <linux/proc_fs.h> 94#include <net/net_namespace.h> 95#include <net/dst.h> 96#include <net/ip.h> 97#include <net/udp.h> 98#include <net/xfrm.h> 99 100#include <asm/byteorder.h> 101#include <asm/atomic.h> 102 103 104#define PPPOL2TP_DRV_VERSION "V1.0" 105 106/* L2TP header constants */ 107#define L2TP_HDRFLAG_T 0x8000 108#define L2TP_HDRFLAG_L 0x4000 109#define L2TP_HDRFLAG_S 0x0800 110#define L2TP_HDRFLAG_O 0x0200 111#define L2TP_HDRFLAG_P 0x0100 112 113#define L2TP_HDR_VER_MASK 0x000F 114#define L2TP_HDR_VER 0x0002 115 116/* Space for UDP, L2TP and PPP headers */ 117#define PPPOL2TP_HEADER_OVERHEAD 40 118 119/* Just some random numbers */ 120#define L2TP_TUNNEL_MAGIC 0x42114DDA 121#define L2TP_SESSION_MAGIC 0x0C04EB7D 122 123#define PPPOL2TP_HASH_BITS 4 124#define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS) 125 126/* Default trace flags */ 127#define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0 128 129#define PRINTK(_mask, _type, _lvl, _fmt, args...) \ 130 do { \ 131 if ((_mask) & (_type)) \ 132 printk(_lvl "PPPOL2TP: " _fmt, ##args); \ 133 } while(0) 134 135/* Number of bytes to build transmit L2TP headers. 136 * Unfortunately the size is different depending on whether sequence numbers 137 * are enabled. 138 */ 139#define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10 140#define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6 141 142struct pppol2tp_tunnel; 143 144/* Describes a session. It is the sk_user_data field in the PPPoL2TP 145 * socket. Contains information to determine incoming packets and transmit 146 * outgoing ones. 147 */ 148struct pppol2tp_session 149{ 150 int magic; /* should be 151 * L2TP_SESSION_MAGIC */ 152 int owner; /* pid that opened the socket */ 153 154 struct sock *sock; /* Pointer to the session 155 * PPPoX socket */ 156 struct sock *tunnel_sock; /* Pointer to the tunnel UDP 157 * socket */ 158 159 struct pppol2tp_addr tunnel_addr; /* Description of tunnel */ 160 161 struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel 162 * context */ 163 164 char name[20]; /* "sess xxxxx/yyyyy", where 165 * x=tunnel_id, y=session_id */ 166 int mtu; 167 int mru; 168 int flags; /* accessed by PPPIOCGFLAGS. 169 * Unused. */ 170 unsigned recv_seq:1; /* expect receive packets with 171 * sequence numbers? */ 172 unsigned send_seq:1; /* send packets with sequence 173 * numbers? */ 174 unsigned lns_mode:1; /* behave as LNS? LAC enables 175 * sequence numbers under 176 * control of LNS. */ 177 int debug; /* bitmask of debug message 178 * categories */ 179 int reorder_timeout; /* configured reorder timeout 180 * (in jiffies) */ 181 u16 nr; /* session NR state (receive) */ 182 u16 ns; /* session NR state (send) */ 183 struct sk_buff_head reorder_q; /* receive reorder queue */ 184 struct pppol2tp_ioc_stats stats; 185 struct hlist_node hlist; /* Hash list node */ 186}; 187 188/* The sk_user_data field of the tunnel's UDP socket. It contains info to track 189 * all the associated sessions so incoming packets can be sorted out 190 */ 191struct pppol2tp_tunnel 192{ 193 int magic; /* Should be L2TP_TUNNEL_MAGIC */ 194 rwlock_t hlist_lock; /* protect session_hlist */ 195 struct hlist_head session_hlist[PPPOL2TP_HASH_SIZE]; 196 /* hashed list of sessions, 197 * hashed by id */ 198 int debug; /* bitmask of debug message 199 * categories */ 200 char name[12]; /* "tunl xxxxx" */ 201 struct pppol2tp_ioc_stats stats; 202 203 void (*old_sk_destruct)(struct sock *); 204 205 struct sock *sock; /* Parent socket */ 206 struct list_head list; /* Keep a list of all open 207 * prepared sockets */ 208 209 atomic_t ref_count; 210}; 211 212/* Private data stored for received packets in the skb. 213 */ 214struct pppol2tp_skb_cb { 215 u16 ns; 216 u16 nr; 217 u16 has_seq; 218 u16 length; 219 unsigned long expires; 220}; 221 222#define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) 223 224static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb); 225static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel); 226 227static atomic_t pppol2tp_tunnel_count; 228static atomic_t pppol2tp_session_count; 229static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL }; 230static struct proto_ops pppol2tp_ops; 231static LIST_HEAD(pppol2tp_tunnel_list); 232static DEFINE_RWLOCK(pppol2tp_tunnel_list_lock); 233 234/* Helpers to obtain tunnel/session contexts from sockets. 235 */ 236static inline struct pppol2tp_session *pppol2tp_sock_to_session(struct sock *sk) 237{ 238 struct pppol2tp_session *session; 239 240 if (sk == NULL) 241 return NULL; 242 243 session = (struct pppol2tp_session *)(sk->sk_user_data); 244 if (session == NULL) 245 return NULL; 246 247 BUG_ON(session->magic != L2TP_SESSION_MAGIC); 248 249 return session; 250} 251 252static inline struct pppol2tp_tunnel *pppol2tp_sock_to_tunnel(struct sock *sk) 253{ 254 struct pppol2tp_tunnel *tunnel; 255 256 if (sk == NULL) 257 return NULL; 258 259 tunnel = (struct pppol2tp_tunnel *)(sk->sk_user_data); 260 if (tunnel == NULL) 261 return NULL; 262 263 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 264 265 return tunnel; 266} 267 268/* Tunnel reference counts. Incremented per session that is added to 269 * the tunnel. 270 */ 271static inline void pppol2tp_tunnel_inc_refcount(struct pppol2tp_tunnel *tunnel) 272{ 273 atomic_inc(&tunnel->ref_count); 274} 275 276static inline void pppol2tp_tunnel_dec_refcount(struct pppol2tp_tunnel *tunnel) 277{ 278 if (atomic_dec_and_test(&tunnel->ref_count)) 279 pppol2tp_tunnel_free(tunnel); 280} 281 282/* Session hash list. 283 * The session_id SHOULD be random according to RFC2661, but several 284 * L2TP implementations (Cisco and Microsoft) use incrementing 285 * session_ids. So we do a real hash on the session_id, rather than a 286 * simple bitmask. 287 */ 288static inline struct hlist_head * 289pppol2tp_session_id_hash(struct pppol2tp_tunnel *tunnel, u16 session_id) 290{ 291 unsigned long hash_val = (unsigned long) session_id; 292 return &tunnel->session_hlist[hash_long(hash_val, PPPOL2TP_HASH_BITS)]; 293} 294 295/* Lookup a session by id 296 */ 297static struct pppol2tp_session * 298pppol2tp_session_find(struct pppol2tp_tunnel *tunnel, u16 session_id) 299{ 300 struct hlist_head *session_list = 301 pppol2tp_session_id_hash(tunnel, session_id); 302 struct pppol2tp_session *session; 303 struct hlist_node *walk; 304 305 read_lock(&tunnel->hlist_lock); 306 hlist_for_each_entry(session, walk, session_list, hlist) { 307 if (session->tunnel_addr.s_session == session_id) { 308 read_unlock(&tunnel->hlist_lock); 309 return session; 310 } 311 } 312 read_unlock(&tunnel->hlist_lock); 313 314 return NULL; 315} 316 317/* Lookup a tunnel by id 318 */ 319static struct pppol2tp_tunnel *pppol2tp_tunnel_find(u16 tunnel_id) 320{ 321 struct pppol2tp_tunnel *tunnel = NULL; 322 323 read_lock(&pppol2tp_tunnel_list_lock); 324 list_for_each_entry(tunnel, &pppol2tp_tunnel_list, list) { 325 if (tunnel->stats.tunnel_id == tunnel_id) { 326 read_unlock(&pppol2tp_tunnel_list_lock); 327 return tunnel; 328 } 329 } 330 read_unlock(&pppol2tp_tunnel_list_lock); 331 332 return NULL; 333} 334 335/***************************************************************************** 336 * Receive data handling 337 *****************************************************************************/ 338 339/* Queue a skb in order. We come here only if the skb has an L2TP sequence 340 * number. 341 */ 342static void pppol2tp_recv_queue_skb(struct pppol2tp_session *session, struct sk_buff *skb) 343{ 344 struct sk_buff *skbp; 345 u16 ns = PPPOL2TP_SKB_CB(skb)->ns; 346 347 spin_lock(&session->reorder_q.lock); 348 skb_queue_walk(&session->reorder_q, skbp) { 349 if (PPPOL2TP_SKB_CB(skbp)->ns > ns) { 350 __skb_insert(skb, skbp->prev, skbp, &session->reorder_q); 351 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, 352 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 353 session->name, ns, PPPOL2TP_SKB_CB(skbp)->ns, 354 skb_queue_len(&session->reorder_q)); 355 session->stats.rx_oos_packets++; 356 goto out; 357 } 358 } 359 360 __skb_queue_tail(&session->reorder_q, skb); 361 362out: 363 spin_unlock(&session->reorder_q.lock); 364} 365 366/* Dequeue a single skb. 367 */ 368static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session *session, struct sk_buff *skb) 369{ 370 struct pppol2tp_tunnel *tunnel = session->tunnel; 371 int length = PPPOL2TP_SKB_CB(skb)->length; 372 struct sock *session_sock = NULL; 373 374 /* We're about to requeue the skb, so unlink it and return resources 375 * to its current owner (a socket receive buffer). 376 */ 377 skb_unlink(skb, &session->reorder_q); 378 skb_orphan(skb); 379 380 tunnel->stats.rx_packets++; 381 tunnel->stats.rx_bytes += length; 382 session->stats.rx_packets++; 383 session->stats.rx_bytes += length; 384 385 if (PPPOL2TP_SKB_CB(skb)->has_seq) { 386 /* Bump our Nr */ 387 session->nr++; 388 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, 389 "%s: updated nr to %hu\n", session->name, session->nr); 390 } 391 392 /* If the socket is bound, send it in to PPP's input queue. Otherwise 393 * queue it on the session socket. 394 */ 395 session_sock = session->sock; 396 if (session_sock->sk_state & PPPOX_BOUND) { 397 struct pppox_sock *po; 398 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, 399 "%s: recv %d byte data frame, passing to ppp\n", 400 session->name, length); 401 402 /* We need to forget all info related to the L2TP packet 403 * gathered in the skb as we are going to reuse the same 404 * skb for the inner packet. 405 * Namely we need to: 406 * - reset xfrm (IPSec) information as it applies to 407 * the outer L2TP packet and not to the inner one 408 * - release the dst to force a route lookup on the inner 409 * IP packet since skb->dst currently points to the dst 410 * of the UDP tunnel 411 * - reset netfilter information as it doesn't apply 412 * to the inner packet either 413 */ 414 secpath_reset(skb); 415 dst_release(skb->dst); 416 skb->dst = NULL; 417 nf_reset(skb); 418 419 po = pppox_sk(session_sock); 420 ppp_input(&po->chan, skb); 421 } else { 422 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO, 423 "%s: socket not bound\n", session->name); 424 425 /* Not bound. Nothing we can do, so discard. */ 426 session->stats.rx_errors++; 427 kfree_skb(skb); 428 } 429 430 sock_put(session->sock); 431} 432 433/* Dequeue skbs from the session's reorder_q, subject to packet order. 434 * Skbs that have been in the queue for too long are simply discarded. 435 */ 436static void pppol2tp_recv_dequeue(struct pppol2tp_session *session) 437{ 438 struct sk_buff *skb; 439 struct sk_buff *tmp; 440 441 /* If the pkt at the head of the queue has the nr that we 442 * expect to send up next, dequeue it and any other 443 * in-sequence packets behind it. 444 */ 445 spin_lock(&session->reorder_q.lock); 446 skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 447 if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) { 448 session->stats.rx_seq_discards++; 449 session->stats.rx_errors++; 450 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, 451 "%s: oos pkt %hu len %d discarded (too old), " 452 "waiting for %hu, reorder_q_len=%d\n", 453 session->name, PPPOL2TP_SKB_CB(skb)->ns, 454 PPPOL2TP_SKB_CB(skb)->length, session->nr, 455 skb_queue_len(&session->reorder_q)); 456 __skb_unlink(skb, &session->reorder_q); 457 kfree_skb(skb); 458 continue; 459 } 460 461 if (PPPOL2TP_SKB_CB(skb)->has_seq) { 462 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) { 463 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, 464 "%s: holding oos pkt %hu len %d, " 465 "waiting for %hu, reorder_q_len=%d\n", 466 session->name, PPPOL2TP_SKB_CB(skb)->ns, 467 PPPOL2TP_SKB_CB(skb)->length, session->nr, 468 skb_queue_len(&session->reorder_q)); 469 goto out; 470 } 471 } 472 spin_unlock(&session->reorder_q.lock); 473 pppol2tp_recv_dequeue_skb(session, skb); 474 spin_lock(&session->reorder_q.lock); 475 } 476 477out: 478 spin_unlock(&session->reorder_q.lock); 479} 480 481/* Internal receive frame. Do the real work of receiving an L2TP data frame 482 * here. The skb is not on a list when we get here. 483 * Returns 0 if the packet was a data packet and was successfully passed on. 484 * Returns 1 if the packet was not a good data packet and could not be 485 * forwarded. All such packets are passed up to userspace to deal with. 486 */ 487static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb) 488{ 489 struct pppol2tp_session *session = NULL; 490 struct pppol2tp_tunnel *tunnel; 491 unsigned char *ptr, *optr; 492 u16 hdrflags; 493 u16 tunnel_id, session_id; 494 int length; 495 int offset; 496 497 tunnel = pppol2tp_sock_to_tunnel(sock); 498 if (tunnel == NULL) 499 goto no_tunnel; 500 501 /* UDP always verifies the packet length. */ 502 __skb_pull(skb, sizeof(struct udphdr)); 503 504 /* Short packet? */ 505 if (!pskb_may_pull(skb, 12)) { 506 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO, 507 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len); 508 goto error; 509 } 510 511 /* Point to L2TP header */ 512 optr = ptr = skb->data; 513 514 /* Get L2TP header flags */ 515 hdrflags = ntohs(*(__be16*)ptr); 516 517 /* Trace packet contents, if enabled */ 518 if (tunnel->debug & PPPOL2TP_MSG_DATA) { 519 length = min(16u, skb->len); 520 if (!pskb_may_pull(skb, length)) 521 goto error; 522 523 printk(KERN_DEBUG "%s: recv: ", tunnel->name); 524 525 offset = 0; 526 do { 527 printk(" %02X", ptr[offset]); 528 } while (++offset < length); 529 530 printk("\n"); 531 } 532 533 /* Get length of L2TP packet */ 534 length = skb->len; 535 536 /* If type is control packet, it is handled by userspace. */ 537 if (hdrflags & L2TP_HDRFLAG_T) { 538 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, 539 "%s: recv control packet, len=%d\n", tunnel->name, length); 540 goto error; 541 } 542 543 /* Skip flags */ 544 ptr += 2; 545 546 /* If length is present, skip it */ 547 if (hdrflags & L2TP_HDRFLAG_L) 548 ptr += 2; 549 550 /* Extract tunnel and session ID */ 551 tunnel_id = ntohs(*(__be16 *) ptr); 552 ptr += 2; 553 session_id = ntohs(*(__be16 *) ptr); 554 ptr += 2; 555 556 /* Find the session context */ 557 session = pppol2tp_session_find(tunnel, session_id); 558 if (!session) { 559 /* Not found? Pass to userspace to deal with */ 560 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO, 561 "%s: no socket found (%hu/%hu). Passing up.\n", 562 tunnel->name, tunnel_id, session_id); 563 goto error; 564 } 565 sock_hold(session->sock); 566 567 /* The ref count on the socket was increased by the above call since 568 * we now hold a pointer to the session. Take care to do sock_put() 569 * when exiting this function from now on... 570 */ 571 572 /* Handle the optional sequence numbers. If we are the LAC, 573 * enable/disable sequence numbers under the control of the LNS. If 574 * no sequence numbers present but we were expecting them, discard 575 * frame. 576 */ 577 if (hdrflags & L2TP_HDRFLAG_S) { 578 u16 ns, nr; 579 ns = ntohs(*(__be16 *) ptr); 580 ptr += 2; 581 nr = ntohs(*(__be16 *) ptr); 582 ptr += 2; 583 584 /* Received a packet with sequence numbers. If we're the LNS, 585 * check if we sre sending sequence numbers and if not, 586 * configure it so. 587 */ 588 if ((!session->lns_mode) && (!session->send_seq)) { 589 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO, 590 "%s: requested to enable seq numbers by LNS\n", 591 session->name); 592 session->send_seq = -1; 593 } 594 595 /* Store L2TP info in the skb */ 596 PPPOL2TP_SKB_CB(skb)->ns = ns; 597 PPPOL2TP_SKB_CB(skb)->nr = nr; 598 PPPOL2TP_SKB_CB(skb)->has_seq = 1; 599 600 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, 601 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n", 602 session->name, ns, nr, session->nr); 603 } else { 604 /* No sequence numbers. 605 * If user has configured mandatory sequence numbers, discard. 606 */ 607 if (session->recv_seq) { 608 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING, 609 "%s: recv data has no seq numbers when required. " 610 "Discarding\n", session->name); 611 session->stats.rx_seq_discards++; 612 goto discard; 613 } 614 615 /* If we're the LAC and we're sending sequence numbers, the 616 * LNS has requested that we no longer send sequence numbers. 617 * If we're the LNS and we're sending sequence numbers, the 618 * LAC is broken. Discard the frame. 619 */ 620 if ((!session->lns_mode) && (session->send_seq)) { 621 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO, 622 "%s: requested to disable seq numbers by LNS\n", 623 session->name); 624 session->send_seq = 0; 625 } else if (session->send_seq) { 626 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING, 627 "%s: recv data has no seq numbers when required. " 628 "Discarding\n", session->name); 629 session->stats.rx_seq_discards++; 630 goto discard; 631 } 632 633 /* Store L2TP info in the skb */ 634 PPPOL2TP_SKB_CB(skb)->has_seq = 0; 635 } 636 637 /* If offset bit set, skip it. */ 638 if (hdrflags & L2TP_HDRFLAG_O) { 639 offset = ntohs(*(__be16 *)ptr); 640 ptr += 2 + offset; 641 } 642 643 offset = ptr - optr; 644 if (!pskb_may_pull(skb, offset)) 645 goto discard; 646 647 __skb_pull(skb, offset); 648 649 /* Skip PPP header, if present. In testing, Microsoft L2TP clients 650 * don't send the PPP header (PPP header compression enabled), but 651 * other clients can include the header. So we cope with both cases 652 * here. The PPP header is always FF03 when using L2TP. 653 * 654 * Note that skb->data[] isn't dereferenced from a u16 ptr here since 655 * the field may be unaligned. 656 */ 657 if (!pskb_may_pull(skb, 2)) 658 goto discard; 659 660 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03)) 661 skb_pull(skb, 2); 662 663 /* Prepare skb for adding to the session's reorder_q. Hold 664 * packets for max reorder_timeout or 1 second if not 665 * reordering. 666 */ 667 PPPOL2TP_SKB_CB(skb)->length = length; 668 PPPOL2TP_SKB_CB(skb)->expires = jiffies + 669 (session->reorder_timeout ? session->reorder_timeout : HZ); 670 671 /* Add packet to the session's receive queue. Reordering is done here, if 672 * enabled. Saved L2TP protocol info is stored in skb->sb[]. 673 */ 674 if (PPPOL2TP_SKB_CB(skb)->has_seq) { 675 if (session->reorder_timeout != 0) { 676 /* Packet reordering enabled. Add skb to session's 677 * reorder queue, in order of ns. 678 */ 679 pppol2tp_recv_queue_skb(session, skb); 680 } else { 681 /* Packet reordering disabled. Discard out-of-sequence 682 * packets 683 */ 684 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) { 685 session->stats.rx_seq_discards++; 686 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, 687 "%s: oos pkt %hu len %d discarded, " 688 "waiting for %hu, reorder_q_len=%d\n", 689 session->name, PPPOL2TP_SKB_CB(skb)->ns, 690 PPPOL2TP_SKB_CB(skb)->length, session->nr, 691 skb_queue_len(&session->reorder_q)); 692 goto discard; 693 } 694 skb_queue_tail(&session->reorder_q, skb); 695 } 696 } else { 697 /* No sequence numbers. Add the skb to the tail of the 698 * reorder queue. This ensures that it will be 699 * delivered after all previous sequenced skbs. 700 */ 701 skb_queue_tail(&session->reorder_q, skb); 702 } 703 704 /* Try to dequeue as many skbs from reorder_q as we can. */ 705 pppol2tp_recv_dequeue(session); 706 707 return 0; 708 709discard: 710 session->stats.rx_errors++; 711 kfree_skb(skb); 712 sock_put(session->sock); 713 714 return 0; 715 716error: 717 /* Put UDP header back */ 718 __skb_push(skb, sizeof(struct udphdr)); 719 720no_tunnel: 721 return 1; 722} 723 724/* UDP encapsulation receive handler. See net/ipv4/udp.c. 725 * Return codes: 726 * 0 : success. 727 * <0: error 728 * >0: skb should be passed up to userspace as UDP. 729 */ 730static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 731{ 732 struct pppol2tp_tunnel *tunnel; 733 734 tunnel = pppol2tp_sock_to_tunnel(sk); 735 if (tunnel == NULL) 736 goto pass_up; 737 738 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, 739 "%s: received %d bytes\n", tunnel->name, skb->len); 740 741 if (pppol2tp_recv_core(sk, skb)) 742 goto pass_up; 743 744 return 0; 745 746pass_up: 747 return 1; 748} 749 750/* Receive message. This is the recvmsg for the PPPoL2TP socket. 751 */ 752static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock, 753 struct msghdr *msg, size_t len, 754 int flags) 755{ 756 int err; 757 struct sk_buff *skb; 758 struct sock *sk = sock->sk; 759 760 err = -EIO; 761 if (sk->sk_state & PPPOX_BOUND) 762 goto end; 763 764 msg->msg_namelen = 0; 765 766 err = 0; 767 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, 768 flags & MSG_DONTWAIT, &err); 769 if (skb) { 770 err = memcpy_toiovec(msg->msg_iov, (unsigned char *) skb->data, 771 skb->len); 772 if (err < 0) 773 goto do_skb_free; 774 err = skb->len; 775 } 776do_skb_free: 777 kfree_skb(skb); 778end: 779 return err; 780} 781 782/************************************************************************ 783 * Transmit handling 784 ***********************************************************************/ 785 786/* Tell how big L2TP headers are for a particular session. This 787 * depends on whether sequence numbers are being used. 788 */ 789static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session) 790{ 791 if (session->send_seq) 792 return PPPOL2TP_L2TP_HDR_SIZE_SEQ; 793 794 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; 795} 796 797/* Build an L2TP header for the session into the buffer provided. 798 */ 799static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session, 800 void *buf) 801{ 802 __be16 *bufp = buf; 803 u16 flags = L2TP_HDR_VER; 804 805 if (session->send_seq) 806 flags |= L2TP_HDRFLAG_S; 807 808 /* Setup L2TP header. 809 * FIXME: Can this ever be unaligned? Is direct dereferencing of 810 * 16-bit header fields safe here for all architectures? 811 */ 812 *bufp++ = htons(flags); 813 *bufp++ = htons(session->tunnel_addr.d_tunnel); 814 *bufp++ = htons(session->tunnel_addr.d_session); 815 if (session->send_seq) { 816 *bufp++ = htons(session->ns); 817 *bufp++ = 0; 818 session->ns++; 819 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG, 820 "%s: updated ns to %hu\n", session->name, session->ns); 821 } 822} 823 824/* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here 825 * when a user application does a sendmsg() on the session socket. L2TP and 826 * PPP headers must be inserted into the user's data. 827 */ 828static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, 829 size_t total_len) 830{ 831 static const unsigned char ppph[2] = { 0xff, 0x03 }; 832 struct sock *sk = sock->sk; 833 struct inet_sock *inet; 834 __wsum csum = 0; 835 struct sk_buff *skb; 836 int error; 837 int hdr_len; 838 struct pppol2tp_session *session; 839 struct pppol2tp_tunnel *tunnel; 840 struct udphdr *uh; 841 unsigned int len; 842 843 error = -ENOTCONN; 844 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) 845 goto error; 846 847 /* Get session and tunnel contexts */ 848 error = -EBADF; 849 session = pppol2tp_sock_to_session(sk); 850 if (session == NULL) 851 goto error; 852 853 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock); 854 if (tunnel == NULL) 855 goto error; 856 857 /* What header length is configured for this session? */ 858 hdr_len = pppol2tp_l2tp_header_len(session); 859 860 /* Allocate a socket buffer */ 861 error = -ENOMEM; 862 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) + 863 sizeof(struct udphdr) + hdr_len + 864 sizeof(ppph) + total_len, 865 0, GFP_KERNEL); 866 if (!skb) 867 goto error; 868 869 /* Reserve space for headers. */ 870 skb_reserve(skb, NET_SKB_PAD); 871 skb_reset_network_header(skb); 872 skb_reserve(skb, sizeof(struct iphdr)); 873 skb_reset_transport_header(skb); 874 875 /* Build UDP header */ 876 inet = inet_sk(session->tunnel_sock); 877 uh = (struct udphdr *) skb->data; 878 uh->source = inet->sport; 879 uh->dest = inet->dport; 880 uh->len = htons(hdr_len + sizeof(ppph) + total_len); 881 uh->check = 0; 882 skb_put(skb, sizeof(struct udphdr)); 883 884 /* Build L2TP header */ 885 pppol2tp_build_l2tp_header(session, skb->data); 886 skb_put(skb, hdr_len); 887 888 /* Add PPP header */ 889 skb->data[0] = ppph[0]; 890 skb->data[1] = ppph[1]; 891 skb_put(skb, 2); 892 893 /* Copy user data into skb */ 894 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len); 895 if (error < 0) { 896 kfree_skb(skb); 897 goto error; 898 } 899 skb_put(skb, total_len); 900 901 /* Calculate UDP checksum if configured to do so */ 902 if (session->tunnel_sock->sk_no_check != UDP_CSUM_NOXMIT) 903 csum = udp_csum_outgoing(sk, skb); 904 905 /* Debug */ 906 if (session->send_seq) 907 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, 908 "%s: send %Zd bytes, ns=%hu\n", session->name, 909 total_len, session->ns - 1); 910 else 911 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, 912 "%s: send %Zd bytes\n", session->name, total_len); 913 914 if (session->debug & PPPOL2TP_MSG_DATA) { 915 int i; 916 unsigned char *datap = skb->data; 917 918 printk(KERN_DEBUG "%s: xmit:", session->name); 919 for (i = 0; i < total_len; i++) { 920 printk(" %02X", *datap++); 921 if (i == 15) { 922 printk(" ..."); 923 break; 924 } 925 } 926 printk("\n"); 927 } 928 929 /* Queue the packet to IP for output */ 930 len = skb->len; 931 error = ip_queue_xmit(skb, 1); 932 933 /* Update stats */ 934 if (error >= 0) { 935 tunnel->stats.tx_packets++; 936 tunnel->stats.tx_bytes += len; 937 session->stats.tx_packets++; 938 session->stats.tx_bytes += len; 939 } else { 940 tunnel->stats.tx_errors++; 941 session->stats.tx_errors++; 942 } 943 944error: 945 return error; 946} 947 948/* Transmit function called by generic PPP driver. Sends PPP frame 949 * over PPPoL2TP socket. 950 * 951 * This is almost the same as pppol2tp_sendmsg(), but rather than 952 * being called with a msghdr from userspace, it is called with a skb 953 * from the kernel. 954 * 955 * The supplied skb from ppp doesn't have enough headroom for the 956 * insertion of L2TP, UDP and IP headers so we need to allocate more 957 * headroom in the skb. This will create a cloned skb. But we must be 958 * careful in the error case because the caller will expect to free 959 * the skb it supplied, not our cloned skb. So we take care to always 960 * leave the original skb unfreed if we return an error. 961 */ 962static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb) 963{ 964 static const u8 ppph[2] = { 0xff, 0x03 }; 965 struct sock *sk = (struct sock *) chan->private; 966 struct sock *sk_tun; 967 int hdr_len; 968 struct pppol2tp_session *session; 969 struct pppol2tp_tunnel *tunnel; 970 int rc; 971 int headroom; 972 int data_len = skb->len; 973 struct inet_sock *inet; 974 __wsum csum = 0; 975 struct udphdr *uh; 976 unsigned int len; 977 978 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) 979 goto abort; 980 981 /* Get session and tunnel contexts from the socket */ 982 session = pppol2tp_sock_to_session(sk); 983 if (session == NULL) 984 goto abort; 985 986 sk_tun = session->tunnel_sock; 987 if (sk_tun == NULL) 988 goto abort; 989 tunnel = pppol2tp_sock_to_tunnel(sk_tun); 990 if (tunnel == NULL) 991 goto abort; 992 993 /* What header length is configured for this session? */ 994 hdr_len = pppol2tp_l2tp_header_len(session); 995 996 /* Check that there's enough headroom in the skb to insert IP, 997 * UDP and L2TP and PPP headers. If not enough, expand it to 998 * make room. Note that a new skb (or a clone) is 999 * allocated. If we return an error from this point on, make 1000 * sure we free the new skb but do not free the original skb 1001 * since that is done by the caller for the error case. 1002 */ 1003 headroom = NET_SKB_PAD + sizeof(struct iphdr) + 1004 sizeof(struct udphdr) + hdr_len + sizeof(ppph); 1005 if (skb_cow_head(skb, headroom)) 1006 goto abort; 1007 1008 /* Setup PPP header */ 1009 __skb_push(skb, sizeof(ppph)); 1010 skb->data[0] = ppph[0]; 1011 skb->data[1] = ppph[1]; 1012 1013 /* Setup L2TP header */ 1014 pppol2tp_build_l2tp_header(session, __skb_push(skb, hdr_len)); 1015 1016 /* Setup UDP header */ 1017 inet = inet_sk(sk_tun); 1018 __skb_push(skb, sizeof(*uh)); 1019 skb_reset_transport_header(skb); 1020 uh = udp_hdr(skb); 1021 uh->source = inet->sport; 1022 uh->dest = inet->dport; 1023 uh->len = htons(sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len); 1024 uh->check = 0; 1025 1026 /* *BROKEN* Calculate UDP checksum if configured to do so */ 1027 if (sk_tun->sk_no_check != UDP_CSUM_NOXMIT) 1028 csum = udp_csum_outgoing(sk_tun, skb); 1029 1030 /* Debug */ 1031 if (session->send_seq) 1032 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, 1033 "%s: send %d bytes, ns=%hu\n", session->name, 1034 data_len, session->ns - 1); 1035 else 1036 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG, 1037 "%s: send %d bytes\n", session->name, data_len); 1038 1039 if (session->debug & PPPOL2TP_MSG_DATA) { 1040 int i; 1041 unsigned char *datap = skb->data; 1042 1043 printk(KERN_DEBUG "%s: xmit:", session->name); 1044 for (i = 0; i < data_len; i++) { 1045 printk(" %02X", *datap++); 1046 if (i == 31) { 1047 printk(" ..."); 1048 break; 1049 } 1050 } 1051 printk("\n"); 1052 } 1053 1054 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1055 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1056 IPSKB_REROUTED); 1057 nf_reset(skb); 1058 1059 /* Get routing info from the tunnel socket */ 1060 dst_release(skb->dst); 1061 skb->dst = sk_dst_get(sk_tun); 1062 skb_orphan(skb); 1063 skb->sk = sk_tun; 1064 1065 /* Queue the packet to IP for output */ 1066 len = skb->len; 1067 rc = ip_queue_xmit(skb, 1); 1068 1069 /* Update stats */ 1070 if (rc >= 0) { 1071 tunnel->stats.tx_packets++; 1072 tunnel->stats.tx_bytes += len; 1073 session->stats.tx_packets++; 1074 session->stats.tx_bytes += len; 1075 } else { 1076 tunnel->stats.tx_errors++; 1077 session->stats.tx_errors++; 1078 } 1079 1080 return 1; 1081 1082abort: 1083 /* Free the original skb */ 1084 kfree_skb(skb); 1085 return 1; 1086} 1087 1088/***************************************************************************** 1089 * Session (and tunnel control) socket create/destroy. 1090 *****************************************************************************/ 1091 1092/* When the tunnel UDP socket is closed, all the attached sockets need to go 1093 * too. 1094 */ 1095static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel) 1096{ 1097 int hash; 1098 struct hlist_node *walk; 1099 struct hlist_node *tmp; 1100 struct pppol2tp_session *session; 1101 struct sock *sk; 1102 1103 if (tunnel == NULL) 1104 BUG(); 1105 1106 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1107 "%s: closing all sessions...\n", tunnel->name); 1108 1109 write_lock(&tunnel->hlist_lock); 1110 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) { 1111again: 1112 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1113 session = hlist_entry(walk, struct pppol2tp_session, hlist); 1114 1115 sk = session->sock; 1116 1117 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1118 "%s: closing session\n", session->name); 1119 1120 hlist_del_init(&session->hlist); 1121 1122 /* Since we should hold the sock lock while 1123 * doing any unbinding, we need to release the 1124 * lock we're holding before taking that lock. 1125 * Hold a reference to the sock so it doesn't 1126 * disappear as we're jumping between locks. 1127 */ 1128 sock_hold(sk); 1129 write_unlock(&tunnel->hlist_lock); 1130 lock_sock(sk); 1131 1132 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) { 1133 pppox_unbind_sock(sk); 1134 sk->sk_state = PPPOX_DEAD; 1135 sk->sk_state_change(sk); 1136 } 1137 1138 /* Purge any queued data */ 1139 skb_queue_purge(&sk->sk_receive_queue); 1140 skb_queue_purge(&sk->sk_write_queue); 1141 skb_queue_purge(&session->reorder_q); 1142 1143 release_sock(sk); 1144 sock_put(sk); 1145 1146 /* Now restart from the beginning of this hash 1147 * chain. We always remove a session from the 1148 * list so we are guaranteed to make forward 1149 * progress. 1150 */ 1151 write_lock(&tunnel->hlist_lock); 1152 goto again; 1153 } 1154 } 1155 write_unlock(&tunnel->hlist_lock); 1156} 1157 1158/* Really kill the tunnel. 1159 * Come here only when all sessions have been cleared from the tunnel. 1160 */ 1161static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel) 1162{ 1163 /* Remove from socket list */ 1164 write_lock(&pppol2tp_tunnel_list_lock); 1165 list_del_init(&tunnel->list); 1166 write_unlock(&pppol2tp_tunnel_list_lock); 1167 1168 atomic_dec(&pppol2tp_tunnel_count); 1169 kfree(tunnel); 1170} 1171 1172/* Tunnel UDP socket destruct hook. 1173 * The tunnel context is deleted only when all session sockets have been 1174 * closed. 1175 */ 1176static void pppol2tp_tunnel_destruct(struct sock *sk) 1177{ 1178 struct pppol2tp_tunnel *tunnel; 1179 1180 tunnel = pppol2tp_sock_to_tunnel(sk); 1181 if (tunnel == NULL) 1182 goto end; 1183 1184 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1185 "%s: closing...\n", tunnel->name); 1186 1187 /* Close all sessions */ 1188 pppol2tp_tunnel_closeall(tunnel); 1189 1190 /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1191 (udp_sk(sk))->encap_type = 0; 1192 (udp_sk(sk))->encap_rcv = NULL; 1193 1194 /* Remove hooks into tunnel socket */ 1195 tunnel->sock = NULL; 1196 sk->sk_destruct = tunnel->old_sk_destruct; 1197 sk->sk_user_data = NULL; 1198 1199 /* Call original (UDP) socket descructor */ 1200 if (sk->sk_destruct != NULL) 1201 (*sk->sk_destruct)(sk); 1202 1203 pppol2tp_tunnel_dec_refcount(tunnel); 1204 1205end: 1206 return; 1207} 1208 1209/* Really kill the session socket. (Called from sock_put() if 1210 * refcnt == 0.) 1211 */ 1212static void pppol2tp_session_destruct(struct sock *sk) 1213{ 1214 struct pppol2tp_session *session = NULL; 1215 1216 if (sk->sk_user_data != NULL) { 1217 struct pppol2tp_tunnel *tunnel; 1218 1219 session = pppol2tp_sock_to_session(sk); 1220 if (session == NULL) 1221 goto out; 1222 1223 /* Don't use pppol2tp_sock_to_tunnel() here to 1224 * get the tunnel context because the tunnel 1225 * socket might have already been closed (its 1226 * sk->sk_user_data will be NULL) so use the 1227 * session's private tunnel ptr instead. 1228 */ 1229 tunnel = session->tunnel; 1230 if (tunnel != NULL) { 1231 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1232 1233 /* If session_id is zero, this is a null 1234 * session context, which was created for a 1235 * socket that is being used only to manage 1236 * tunnels. 1237 */ 1238 if (session->tunnel_addr.s_session != 0) { 1239 /* Delete the session socket from the 1240 * hash 1241 */ 1242 write_lock(&tunnel->hlist_lock); 1243 hlist_del_init(&session->hlist); 1244 write_unlock(&tunnel->hlist_lock); 1245 1246 atomic_dec(&pppol2tp_session_count); 1247 } 1248 1249 /* This will delete the tunnel context if this 1250 * is the last session on the tunnel. 1251 */ 1252 session->tunnel = NULL; 1253 session->tunnel_sock = NULL; 1254 pppol2tp_tunnel_dec_refcount(tunnel); 1255 } 1256 } 1257 1258 kfree(session); 1259out: 1260 return; 1261} 1262 1263/* Called when the PPPoX socket (session) is closed. 1264 */ 1265static int pppol2tp_release(struct socket *sock) 1266{ 1267 struct sock *sk = sock->sk; 1268 int error; 1269 1270 if (!sk) 1271 return 0; 1272 1273 error = -EBADF; 1274 lock_sock(sk); 1275 if (sock_flag(sk, SOCK_DEAD) != 0) 1276 goto error; 1277 1278 pppox_unbind_sock(sk); 1279 1280 /* Signal the death of the socket. */ 1281 sk->sk_state = PPPOX_DEAD; 1282 sock_orphan(sk); 1283 sock->sk = NULL; 1284 1285 /* Purge any queued data */ 1286 skb_queue_purge(&sk->sk_receive_queue); 1287 skb_queue_purge(&sk->sk_write_queue); 1288 1289 release_sock(sk); 1290 1291 /* This will delete the session context via 1292 * pppol2tp_session_destruct() if the socket's refcnt drops to 1293 * zero. 1294 */ 1295 sock_put(sk); 1296 1297 return 0; 1298 1299error: 1300 release_sock(sk); 1301 return error; 1302} 1303 1304/* Internal function to prepare a tunnel (UDP) socket to have PPPoX 1305 * sockets attached to it. 1306 */ 1307static struct sock *pppol2tp_prepare_tunnel_socket(int fd, u16 tunnel_id, 1308 int *error) 1309{ 1310 int err; 1311 struct socket *sock = NULL; 1312 struct sock *sk; 1313 struct pppol2tp_tunnel *tunnel; 1314 struct sock *ret = NULL; 1315 1316 /* Get the tunnel UDP socket from the fd, which was opened by 1317 * the userspace L2TP daemon. 1318 */ 1319 err = -EBADF; 1320 sock = sockfd_lookup(fd, &err); 1321 if (!sock) { 1322 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR, 1323 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n", 1324 tunnel_id, fd, err); 1325 goto err; 1326 } 1327 1328 sk = sock->sk; 1329 1330 /* Quick sanity checks */ 1331 err = -EPROTONOSUPPORT; 1332 if (sk->sk_protocol != IPPROTO_UDP) { 1333 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR, 1334 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1335 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1336 goto err; 1337 } 1338 err = -EAFNOSUPPORT; 1339 if (sock->ops->family != AF_INET) { 1340 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR, 1341 "tunl %hu: fd %d wrong family, got %d, expected %d\n", 1342 tunnel_id, fd, sock->ops->family, AF_INET); 1343 goto err; 1344 } 1345 1346 err = -ENOTCONN; 1347 1348 /* Check if this socket has already been prepped */ 1349 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data; 1350 if (tunnel != NULL) { 1351 /* User-data field already set */ 1352 err = -EBUSY; 1353 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1354 1355 /* This socket has already been prepped */ 1356 ret = tunnel->sock; 1357 goto out; 1358 } 1359 1360 /* This socket is available and needs prepping. Create a new tunnel 1361 * context and init it. 1362 */ 1363 sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL); 1364 if (sk->sk_user_data == NULL) { 1365 err = -ENOMEM; 1366 goto err; 1367 } 1368 1369 tunnel->magic = L2TP_TUNNEL_MAGIC; 1370 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id); 1371 1372 tunnel->stats.tunnel_id = tunnel_id; 1373 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS; 1374 1375 /* Hook on the tunnel socket destructor so that we can cleanup 1376 * if the tunnel socket goes away. 1377 */ 1378 tunnel->old_sk_destruct = sk->sk_destruct; 1379 sk->sk_destruct = &pppol2tp_tunnel_destruct; 1380 1381 tunnel->sock = sk; 1382 sk->sk_allocation = GFP_ATOMIC; 1383 1384 /* Misc init */ 1385 rwlock_init(&tunnel->hlist_lock); 1386 1387 /* Add tunnel to our list */ 1388 INIT_LIST_HEAD(&tunnel->list); 1389 write_lock(&pppol2tp_tunnel_list_lock); 1390 list_add(&tunnel->list, &pppol2tp_tunnel_list); 1391 write_unlock(&pppol2tp_tunnel_list_lock); 1392 atomic_inc(&pppol2tp_tunnel_count); 1393 1394 /* Bump the reference count. The tunnel context is deleted 1395 * only when this drops to zero. 1396 */ 1397 pppol2tp_tunnel_inc_refcount(tunnel); 1398 1399 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 1400 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP; 1401 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv; 1402 1403 ret = tunnel->sock; 1404 1405 *error = 0; 1406out: 1407 if (sock) 1408 sockfd_put(sock); 1409 1410 return ret; 1411 1412err: 1413 *error = err; 1414 goto out; 1415} 1416 1417static struct proto pppol2tp_sk_proto = { 1418 .name = "PPPOL2TP", 1419 .owner = THIS_MODULE, 1420 .obj_size = sizeof(struct pppox_sock), 1421}; 1422 1423/* socket() handler. Initialize a new struct sock. 1424 */ 1425static int pppol2tp_create(struct net *net, struct socket *sock) 1426{ 1427 int error = -ENOMEM; 1428 struct sock *sk; 1429 1430 sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto); 1431 if (!sk) 1432 goto out; 1433 1434 sock_init_data(sock, sk); 1435 1436 sock->state = SS_UNCONNECTED; 1437 sock->ops = &pppol2tp_ops; 1438 1439 sk->sk_backlog_rcv = pppol2tp_recv_core; 1440 sk->sk_protocol = PX_PROTO_OL2TP; 1441 sk->sk_family = PF_PPPOX; 1442 sk->sk_state = PPPOX_NONE; 1443 sk->sk_type = SOCK_STREAM; 1444 sk->sk_destruct = pppol2tp_session_destruct; 1445 1446 error = 0; 1447 1448out: 1449 return error; 1450} 1451 1452/* connect() handler. Attach a PPPoX socket to a tunnel UDP socket 1453 */ 1454static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr, 1455 int sockaddr_len, int flags) 1456{ 1457 struct sock *sk = sock->sk; 1458 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr; 1459 struct pppox_sock *po = pppox_sk(sk); 1460 struct sock *tunnel_sock = NULL; 1461 struct pppol2tp_session *session = NULL; 1462 struct pppol2tp_tunnel *tunnel; 1463 struct dst_entry *dst; 1464 int error = 0; 1465 1466 lock_sock(sk); 1467 1468 error = -EINVAL; 1469 if (sp->sa_protocol != PX_PROTO_OL2TP) 1470 goto end; 1471 1472 /* Check for already bound sockets */ 1473 error = -EBUSY; 1474 if (sk->sk_state & PPPOX_CONNECTED) 1475 goto end; 1476 1477 /* We don't supporting rebinding anyway */ 1478 error = -EALREADY; 1479 if (sk->sk_user_data) 1480 goto end; /* socket is already attached */ 1481 1482 /* Don't bind if s_tunnel is 0 */ 1483 error = -EINVAL; 1484 if (sp->pppol2tp.s_tunnel == 0) 1485 goto end; 1486 1487 /* Special case: prepare tunnel socket if s_session and 1488 * d_session is 0. Otherwise look up tunnel using supplied 1489 * tunnel id. 1490 */ 1491 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) { 1492 tunnel_sock = pppol2tp_prepare_tunnel_socket(sp->pppol2tp.fd, 1493 sp->pppol2tp.s_tunnel, 1494 &error); 1495 if (tunnel_sock == NULL) 1496 goto end; 1497 1498 tunnel = tunnel_sock->sk_user_data; 1499 } else { 1500 tunnel = pppol2tp_tunnel_find(sp->pppol2tp.s_tunnel); 1501 1502 /* Error if we can't find the tunnel */ 1503 error = -ENOENT; 1504 if (tunnel == NULL) 1505 goto end; 1506 1507 tunnel_sock = tunnel->sock; 1508 } 1509 1510 /* Check that this session doesn't already exist */ 1511 error = -EEXIST; 1512 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session); 1513 if (session != NULL) 1514 goto end; 1515 1516 /* Allocate and initialize a new session context. */ 1517 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL); 1518 if (session == NULL) { 1519 error = -ENOMEM; 1520 goto end; 1521 } 1522 1523 skb_queue_head_init(&session->reorder_q); 1524 1525 session->magic = L2TP_SESSION_MAGIC; 1526 session->owner = current->pid; 1527 session->sock = sk; 1528 session->tunnel = tunnel; 1529 session->tunnel_sock = tunnel_sock; 1530 session->tunnel_addr = sp->pppol2tp; 1531 sprintf(&session->name[0], "sess %hu/%hu", 1532 session->tunnel_addr.s_tunnel, 1533 session->tunnel_addr.s_session); 1534 1535 session->stats.tunnel_id = session->tunnel_addr.s_tunnel; 1536 session->stats.session_id = session->tunnel_addr.s_session; 1537 1538 INIT_HLIST_NODE(&session->hlist); 1539 1540 /* Inherit debug options from tunnel */ 1541 session->debug = tunnel->debug; 1542 1543 /* Default MTU must allow space for UDP/L2TP/PPP 1544 * headers. 1545 */ 1546 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD; 1547 1548 /* If PMTU discovery was enabled, use the MTU that was discovered */ 1549 dst = sk_dst_get(sk); 1550 if (dst != NULL) { 1551 u32 pmtu = dst_mtu(__sk_dst_get(sk)); 1552 if (pmtu != 0) 1553 session->mtu = session->mru = pmtu - 1554 PPPOL2TP_HEADER_OVERHEAD; 1555 dst_release(dst); 1556 } 1557 1558 /* Special case: if source & dest session_id == 0x0000, this socket is 1559 * being created to manage the tunnel. Don't add the session to the 1560 * session hash list, just set up the internal context for use by 1561 * ioctl() and sockopt() handlers. 1562 */ 1563 if ((session->tunnel_addr.s_session == 0) && 1564 (session->tunnel_addr.d_session == 0)) { 1565 error = 0; 1566 sk->sk_user_data = session; 1567 goto out_no_ppp; 1568 } 1569 1570 /* Get tunnel context from the tunnel socket */ 1571 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock); 1572 if (tunnel == NULL) { 1573 error = -EBADF; 1574 goto end; 1575 } 1576 1577 /* Right now, because we don't have a way to push the incoming skb's 1578 * straight through the UDP layer, the only header we need to worry 1579 * about is the L2TP header. This size is different depending on 1580 * whether sequence numbers are enabled for the data channel. 1581 */ 1582 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; 1583 1584 po->chan.private = sk; 1585 po->chan.ops = &pppol2tp_chan_ops; 1586 po->chan.mtu = session->mtu; 1587 1588 error = ppp_register_channel(&po->chan); 1589 if (error) 1590 goto end; 1591 1592 /* This is how we get the session context from the socket. */ 1593 sk->sk_user_data = session; 1594 1595 /* Add session to the tunnel's hash list */ 1596 write_lock(&tunnel->hlist_lock); 1597 hlist_add_head(&session->hlist, 1598 pppol2tp_session_id_hash(tunnel, 1599 session->tunnel_addr.s_session)); 1600 write_unlock(&tunnel->hlist_lock); 1601 1602 atomic_inc(&pppol2tp_session_count); 1603 1604out_no_ppp: 1605 pppol2tp_tunnel_inc_refcount(tunnel); 1606 sk->sk_state = PPPOX_CONNECTED; 1607 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1608 "%s: created\n", session->name); 1609 1610end: 1611 release_sock(sk); 1612 1613 if (error != 0) 1614 PRINTK(session ? session->debug : -1, PPPOL2TP_MSG_CONTROL, KERN_WARNING, 1615 "%s: connect failed: %d\n", session->name, error); 1616 1617 return error; 1618} 1619 1620/* getname() support. 1621 */ 1622static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr, 1623 int *usockaddr_len, int peer) 1624{ 1625 int len = sizeof(struct sockaddr_pppol2tp); 1626 struct sockaddr_pppol2tp sp; 1627 int error = 0; 1628 struct pppol2tp_session *session; 1629 1630 error = -ENOTCONN; 1631 if (sock->sk->sk_state != PPPOX_CONNECTED) 1632 goto end; 1633 1634 session = pppol2tp_sock_to_session(sock->sk); 1635 if (session == NULL) { 1636 error = -EBADF; 1637 goto end; 1638 } 1639 1640 sp.sa_family = AF_PPPOX; 1641 sp.sa_protocol = PX_PROTO_OL2TP; 1642 memcpy(&sp.pppol2tp, &session->tunnel_addr, 1643 sizeof(struct pppol2tp_addr)); 1644 1645 memcpy(uaddr, &sp, len); 1646 1647 *usockaddr_len = len; 1648 1649 error = 0; 1650 1651end: 1652 return error; 1653} 1654 1655/**************************************************************************** 1656 * ioctl() handlers. 1657 * 1658 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP 1659 * sockets. However, in order to control kernel tunnel features, we allow 1660 * userspace to create a special "tunnel" PPPoX socket which is used for 1661 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow 1662 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl() 1663 * calls. 1664 ****************************************************************************/ 1665 1666/* Session ioctl helper. 1667 */ 1668static int pppol2tp_session_ioctl(struct pppol2tp_session *session, 1669 unsigned int cmd, unsigned long arg) 1670{ 1671 struct ifreq ifr; 1672 int err = 0; 1673 struct sock *sk = session->sock; 1674 int val = (int) arg; 1675 1676 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG, 1677 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n", 1678 session->name, cmd, arg); 1679 1680 sock_hold(sk); 1681 1682 switch (cmd) { 1683 case SIOCGIFMTU: 1684 err = -ENXIO; 1685 if (!(sk->sk_state & PPPOX_CONNECTED)) 1686 break; 1687 1688 err = -EFAULT; 1689 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq))) 1690 break; 1691 ifr.ifr_mtu = session->mtu; 1692 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq))) 1693 break; 1694 1695 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1696 "%s: get mtu=%d\n", session->name, session->mtu); 1697 err = 0; 1698 break; 1699 1700 case SIOCSIFMTU: 1701 err = -ENXIO; 1702 if (!(sk->sk_state & PPPOX_CONNECTED)) 1703 break; 1704 1705 err = -EFAULT; 1706 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq))) 1707 break; 1708 1709 session->mtu = ifr.ifr_mtu; 1710 1711 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1712 "%s: set mtu=%d\n", session->name, session->mtu); 1713 err = 0; 1714 break; 1715 1716 case PPPIOCGMRU: 1717 err = -ENXIO; 1718 if (!(sk->sk_state & PPPOX_CONNECTED)) 1719 break; 1720 1721 err = -EFAULT; 1722 if (put_user(session->mru, (int __user *) arg)) 1723 break; 1724 1725 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1726 "%s: get mru=%d\n", session->name, session->mru); 1727 err = 0; 1728 break; 1729 1730 case PPPIOCSMRU: 1731 err = -ENXIO; 1732 if (!(sk->sk_state & PPPOX_CONNECTED)) 1733 break; 1734 1735 err = -EFAULT; 1736 if (get_user(val,(int __user *) arg)) 1737 break; 1738 1739 session->mru = val; 1740 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1741 "%s: set mru=%d\n", session->name, session->mru); 1742 err = 0; 1743 break; 1744 1745 case PPPIOCGFLAGS: 1746 err = -EFAULT; 1747 if (put_user(session->flags, (int __user *) arg)) 1748 break; 1749 1750 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1751 "%s: get flags=%d\n", session->name, session->flags); 1752 err = 0; 1753 break; 1754 1755 case PPPIOCSFLAGS: 1756 err = -EFAULT; 1757 if (get_user(val, (int __user *) arg)) 1758 break; 1759 session->flags = val; 1760 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1761 "%s: set flags=%d\n", session->name, session->flags); 1762 err = 0; 1763 break; 1764 1765 case PPPIOCGL2TPSTATS: 1766 err = -ENXIO; 1767 if (!(sk->sk_state & PPPOX_CONNECTED)) 1768 break; 1769 1770 if (copy_to_user((void __user *) arg, &session->stats, 1771 sizeof(session->stats))) 1772 break; 1773 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1774 "%s: get L2TP stats\n", session->name); 1775 err = 0; 1776 break; 1777 1778 default: 1779 err = -ENOSYS; 1780 break; 1781 } 1782 1783 sock_put(sk); 1784 1785 return err; 1786} 1787 1788/* Tunnel ioctl helper. 1789 * 1790 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data 1791 * specifies a session_id, the session ioctl handler is called. This allows an 1792 * application to retrieve session stats via a tunnel socket. 1793 */ 1794static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel, 1795 unsigned int cmd, unsigned long arg) 1796{ 1797 int err = 0; 1798 struct sock *sk = tunnel->sock; 1799 struct pppol2tp_ioc_stats stats_req; 1800 1801 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG, 1802 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name, 1803 cmd, arg); 1804 1805 sock_hold(sk); 1806 1807 switch (cmd) { 1808 case PPPIOCGL2TPSTATS: 1809 err = -ENXIO; 1810 if (!(sk->sk_state & PPPOX_CONNECTED)) 1811 break; 1812 1813 if (copy_from_user(&stats_req, (void __user *) arg, 1814 sizeof(stats_req))) { 1815 err = -EFAULT; 1816 break; 1817 } 1818 if (stats_req.session_id != 0) { 1819 /* resend to session ioctl handler */ 1820 struct pppol2tp_session *session = 1821 pppol2tp_session_find(tunnel, stats_req.session_id); 1822 if (session != NULL) 1823 err = pppol2tp_session_ioctl(session, cmd, arg); 1824 else 1825 err = -EBADR; 1826 break; 1827 } 1828#ifdef CONFIG_XFRM 1829 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0; 1830#endif 1831 if (copy_to_user((void __user *) arg, &tunnel->stats, 1832 sizeof(tunnel->stats))) { 1833 err = -EFAULT; 1834 break; 1835 } 1836 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1837 "%s: get L2TP stats\n", tunnel->name); 1838 err = 0; 1839 break; 1840 1841 default: 1842 err = -ENOSYS; 1843 break; 1844 } 1845 1846 sock_put(sk); 1847 1848 return err; 1849} 1850 1851/* Main ioctl() handler. 1852 * Dispatch to tunnel or session helpers depending on the socket. 1853 */ 1854static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd, 1855 unsigned long arg) 1856{ 1857 struct sock *sk = sock->sk; 1858 struct pppol2tp_session *session; 1859 struct pppol2tp_tunnel *tunnel; 1860 int err; 1861 1862 if (!sk) 1863 return 0; 1864 1865 err = -EBADF; 1866 if (sock_flag(sk, SOCK_DEAD) != 0) 1867 goto end; 1868 1869 err = -ENOTCONN; 1870 if ((sk->sk_user_data == NULL) || 1871 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)))) 1872 goto end; 1873 1874 /* Get session context from the socket */ 1875 err = -EBADF; 1876 session = pppol2tp_sock_to_session(sk); 1877 if (session == NULL) 1878 goto end; 1879 1880 /* Special case: if session's session_id is zero, treat ioctl as a 1881 * tunnel ioctl 1882 */ 1883 if ((session->tunnel_addr.s_session == 0) && 1884 (session->tunnel_addr.d_session == 0)) { 1885 err = -EBADF; 1886 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock); 1887 if (tunnel == NULL) 1888 goto end; 1889 1890 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg); 1891 goto end; 1892 } 1893 1894 err = pppol2tp_session_ioctl(session, cmd, arg); 1895 1896end: 1897 return err; 1898} 1899 1900/***************************************************************************** 1901 * setsockopt() / getsockopt() support. 1902 * 1903 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP 1904 * sockets. In order to control kernel tunnel features, we allow userspace to 1905 * create a special "tunnel" PPPoX socket which is used for control only. 1906 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user 1907 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls. 1908 *****************************************************************************/ 1909 1910/* Tunnel setsockopt() helper. 1911 */ 1912static int pppol2tp_tunnel_setsockopt(struct sock *sk, 1913 struct pppol2tp_tunnel *tunnel, 1914 int optname, int val) 1915{ 1916 int err = 0; 1917 1918 switch (optname) { 1919 case PPPOL2TP_SO_DEBUG: 1920 tunnel->debug = val; 1921 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1922 "%s: set debug=%x\n", tunnel->name, tunnel->debug); 1923 break; 1924 1925 default: 1926 err = -ENOPROTOOPT; 1927 break; 1928 } 1929 1930 return err; 1931} 1932 1933/* Session setsockopt helper. 1934 */ 1935static int pppol2tp_session_setsockopt(struct sock *sk, 1936 struct pppol2tp_session *session, 1937 int optname, int val) 1938{ 1939 int err = 0; 1940 1941 switch (optname) { 1942 case PPPOL2TP_SO_RECVSEQ: 1943 if ((val != 0) && (val != 1)) { 1944 err = -EINVAL; 1945 break; 1946 } 1947 session->recv_seq = val ? -1 : 0; 1948 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1949 "%s: set recv_seq=%d\n", session->name, 1950 session->recv_seq); 1951 break; 1952 1953 case PPPOL2TP_SO_SENDSEQ: 1954 if ((val != 0) && (val != 1)) { 1955 err = -EINVAL; 1956 break; 1957 } 1958 session->send_seq = val ? -1 : 0; 1959 { 1960 struct sock *ssk = session->sock; 1961 struct pppox_sock *po = pppox_sk(ssk); 1962 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ : 1963 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ; 1964 } 1965 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1966 "%s: set send_seq=%d\n", session->name, session->send_seq); 1967 break; 1968 1969 case PPPOL2TP_SO_LNSMODE: 1970 if ((val != 0) && (val != 1)) { 1971 err = -EINVAL; 1972 break; 1973 } 1974 session->lns_mode = val ? -1 : 0; 1975 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1976 "%s: set lns_mode=%d\n", session->name, 1977 session->lns_mode); 1978 break; 1979 1980 case PPPOL2TP_SO_DEBUG: 1981 session->debug = val; 1982 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1983 "%s: set debug=%x\n", session->name, session->debug); 1984 break; 1985 1986 case PPPOL2TP_SO_REORDERTO: 1987 session->reorder_timeout = msecs_to_jiffies(val); 1988 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 1989 "%s: set reorder_timeout=%d\n", session->name, 1990 session->reorder_timeout); 1991 break; 1992 1993 default: 1994 err = -ENOPROTOOPT; 1995 break; 1996 } 1997 1998 return err; 1999} 2000 2001/* Main setsockopt() entry point. 2002 * Does API checks, then calls either the tunnel or session setsockopt 2003 * handler, according to whether the PPPoL2TP socket is a for a regular 2004 * session or the special tunnel type. 2005 */ 2006static int pppol2tp_setsockopt(struct socket *sock, int level, int optname, 2007 char __user *optval, int optlen) 2008{ 2009 struct sock *sk = sock->sk; 2010 struct pppol2tp_session *session = sk->sk_user_data; 2011 struct pppol2tp_tunnel *tunnel; 2012 int val; 2013 int err; 2014 2015 if (level != SOL_PPPOL2TP) 2016 return udp_prot.setsockopt(sk, level, optname, optval, optlen); 2017 2018 if (optlen < sizeof(int)) 2019 return -EINVAL; 2020 2021 if (get_user(val, (int __user *)optval)) 2022 return -EFAULT; 2023 2024 err = -ENOTCONN; 2025 if (sk->sk_user_data == NULL) 2026 goto end; 2027 2028 /* Get session context from the socket */ 2029 err = -EBADF; 2030 session = pppol2tp_sock_to_session(sk); 2031 if (session == NULL) 2032 goto end; 2033 2034 /* Special case: if session_id == 0x0000, treat as operation on tunnel 2035 */ 2036 if ((session->tunnel_addr.s_session == 0) && 2037 (session->tunnel_addr.d_session == 0)) { 2038 err = -EBADF; 2039 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock); 2040 if (tunnel == NULL) 2041 goto end; 2042 2043 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val); 2044 } else 2045 err = pppol2tp_session_setsockopt(sk, session, optname, val); 2046 2047 err = 0; 2048 2049end: 2050 return err; 2051} 2052 2053/* Tunnel getsockopt helper. Called with sock locked. 2054 */ 2055static int pppol2tp_tunnel_getsockopt(struct sock *sk, 2056 struct pppol2tp_tunnel *tunnel, 2057 int optname, int *val) 2058{ 2059 int err = 0; 2060 2061 switch (optname) { 2062 case PPPOL2TP_SO_DEBUG: 2063 *val = tunnel->debug; 2064 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 2065 "%s: get debug=%x\n", tunnel->name, tunnel->debug); 2066 break; 2067 2068 default: 2069 err = -ENOPROTOOPT; 2070 break; 2071 } 2072 2073 return err; 2074} 2075 2076/* Session getsockopt helper. Called with sock locked. 2077 */ 2078static int pppol2tp_session_getsockopt(struct sock *sk, 2079 struct pppol2tp_session *session, 2080 int optname, int *val) 2081{ 2082 int err = 0; 2083 2084 switch (optname) { 2085 case PPPOL2TP_SO_RECVSEQ: 2086 *val = session->recv_seq; 2087 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 2088 "%s: get recv_seq=%d\n", session->name, *val); 2089 break; 2090 2091 case PPPOL2TP_SO_SENDSEQ: 2092 *val = session->send_seq; 2093 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 2094 "%s: get send_seq=%d\n", session->name, *val); 2095 break; 2096 2097 case PPPOL2TP_SO_LNSMODE: 2098 *val = session->lns_mode; 2099 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 2100 "%s: get lns_mode=%d\n", session->name, *val); 2101 break; 2102 2103 case PPPOL2TP_SO_DEBUG: 2104 *val = session->debug; 2105 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 2106 "%s: get debug=%d\n", session->name, *val); 2107 break; 2108 2109 case PPPOL2TP_SO_REORDERTO: 2110 *val = (int) jiffies_to_msecs(session->reorder_timeout); 2111 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO, 2112 "%s: get reorder_timeout=%d\n", session->name, *val); 2113 break; 2114 2115 default: 2116 err = -ENOPROTOOPT; 2117 } 2118 2119 return err; 2120} 2121 2122/* Main getsockopt() entry point. 2123 * Does API checks, then calls either the tunnel or session getsockopt 2124 * handler, according to whether the PPPoX socket is a for a regular session 2125 * or the special tunnel type. 2126 */ 2127static int pppol2tp_getsockopt(struct socket *sock, int level, 2128 int optname, char __user *optval, int __user *optlen) 2129{ 2130 struct sock *sk = sock->sk; 2131 struct pppol2tp_session *session = sk->sk_user_data; 2132 struct pppol2tp_tunnel *tunnel; 2133 int val, len; 2134 int err; 2135 2136 if (level != SOL_PPPOL2TP) 2137 return udp_prot.getsockopt(sk, level, optname, optval, optlen); 2138 2139 if (get_user(len, (int __user *) optlen)) 2140 return -EFAULT; 2141 2142 len = min_t(unsigned int, len, sizeof(int)); 2143 2144 if (len < 0) 2145 return -EINVAL; 2146 2147 err = -ENOTCONN; 2148 if (sk->sk_user_data == NULL) 2149 goto end; 2150 2151 /* Get the session context */ 2152 err = -EBADF; 2153 session = pppol2tp_sock_to_session(sk); 2154 if (session == NULL) 2155 goto end; 2156 2157 /* Special case: if session_id == 0x0000, treat as operation on tunnel */ 2158 if ((session->tunnel_addr.s_session == 0) && 2159 (session->tunnel_addr.d_session == 0)) { 2160 err = -EBADF; 2161 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock); 2162 if (tunnel == NULL) 2163 goto end; 2164 2165 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val); 2166 } else 2167 err = pppol2tp_session_getsockopt(sk, session, optname, &val); 2168 2169 err = -EFAULT; 2170 if (put_user(len, (int __user *) optlen)) 2171 goto end; 2172 2173 if (copy_to_user((void __user *) optval, &val, len)) 2174 goto end; 2175 2176 err = 0; 2177end: 2178 return err; 2179} 2180 2181/***************************************************************************** 2182 * /proc filesystem for debug 2183 *****************************************************************************/ 2184 2185#ifdef CONFIG_PROC_FS 2186 2187#include <linux/seq_file.h> 2188 2189struct pppol2tp_seq_data { 2190 struct pppol2tp_tunnel *tunnel; /* current tunnel */ 2191 struct pppol2tp_session *session; /* NULL means get first session in tunnel */ 2192}; 2193 2194static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr) 2195{ 2196 struct pppol2tp_session *session = NULL; 2197 struct hlist_node *walk; 2198 int found = 0; 2199 int next = 0; 2200 int i; 2201 2202 read_lock(&tunnel->hlist_lock); 2203 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) { 2204 hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) { 2205 if (curr == NULL) { 2206 found = 1; 2207 goto out; 2208 } 2209 if (session == curr) { 2210 next = 1; 2211 continue; 2212 } 2213 if (next) { 2214 found = 1; 2215 goto out; 2216 } 2217 } 2218 } 2219out: 2220 read_unlock(&tunnel->hlist_lock); 2221 if (!found) 2222 session = NULL; 2223 2224 return session; 2225} 2226 2227static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_tunnel *curr) 2228{ 2229 struct pppol2tp_tunnel *tunnel = NULL; 2230 2231 read_lock(&pppol2tp_tunnel_list_lock); 2232 if (list_is_last(&curr->list, &pppol2tp_tunnel_list)) { 2233 goto out; 2234 } 2235 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list); 2236out: 2237 read_unlock(&pppol2tp_tunnel_list_lock); 2238 2239 return tunnel; 2240} 2241 2242static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs) 2243{ 2244 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN; 2245 loff_t pos = *offs; 2246 2247 if (!pos) 2248 goto out; 2249 2250 BUG_ON(m->private == NULL); 2251 pd = m->private; 2252 2253 if (pd->tunnel == NULL) { 2254 if (!list_empty(&pppol2tp_tunnel_list)) 2255 pd->tunnel = list_entry(pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list); 2256 } else { 2257 pd->session = next_session(pd->tunnel, pd->session); 2258 if (pd->session == NULL) { 2259 pd->tunnel = next_tunnel(pd->tunnel); 2260 } 2261 } 2262 2263 /* NULL tunnel and session indicates end of list */ 2264 if ((pd->tunnel == NULL) && (pd->session == NULL)) 2265 pd = NULL; 2266 2267out: 2268 return pd; 2269} 2270 2271static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos) 2272{ 2273 (*pos)++; 2274 return NULL; 2275} 2276 2277static void pppol2tp_seq_stop(struct seq_file *p, void *v) 2278{ 2279 /* nothing to do */ 2280} 2281 2282static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v) 2283{ 2284 struct pppol2tp_tunnel *tunnel = v; 2285 2286 seq_printf(m, "\nTUNNEL '%s', %c %d\n", 2287 tunnel->name, 2288 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N', 2289 atomic_read(&tunnel->ref_count) - 1); 2290 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n", 2291 tunnel->debug, 2292 tunnel->stats.tx_packets, tunnel->stats.tx_bytes, 2293 tunnel->stats.tx_errors, 2294 tunnel->stats.rx_packets, tunnel->stats.rx_bytes, 2295 tunnel->stats.rx_errors); 2296} 2297 2298static void pppol2tp_seq_session_show(struct seq_file *m, void *v) 2299{ 2300 struct pppol2tp_session *session = v; 2301 2302 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> " 2303 "%04X/%04X %d %c\n", 2304 session->name, 2305 ntohl(session->tunnel_addr.addr.sin_addr.s_addr), 2306 ntohs(session->tunnel_addr.addr.sin_port), 2307 session->tunnel_addr.s_tunnel, 2308 session->tunnel_addr.s_session, 2309 session->tunnel_addr.d_tunnel, 2310 session->tunnel_addr.d_session, 2311 session->sock->sk_state, 2312 (session == session->sock->sk_user_data) ? 2313 'Y' : 'N'); 2314 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n", 2315 session->mtu, session->mru, 2316 session->recv_seq ? 'R' : '-', 2317 session->send_seq ? 'S' : '-', 2318 session->lns_mode ? "LNS" : "LAC", 2319 session->debug, 2320 jiffies_to_msecs(session->reorder_timeout)); 2321 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n", 2322 session->nr, session->ns, 2323 session->stats.tx_packets, 2324 session->stats.tx_bytes, 2325 session->stats.tx_errors, 2326 session->stats.rx_packets, 2327 session->stats.rx_bytes, 2328 session->stats.rx_errors); 2329} 2330 2331static int pppol2tp_seq_show(struct seq_file *m, void *v) 2332{ 2333 struct pppol2tp_seq_data *pd = v; 2334 2335 /* display header on line 1 */ 2336 if (v == SEQ_START_TOKEN) { 2337 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n"); 2338 seq_puts(m, "TUNNEL name, user-data-ok session-count\n"); 2339 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); 2340 seq_puts(m, " SESSION name, addr/port src-tid/sid " 2341 "dest-tid/sid state user-data-ok\n"); 2342 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n"); 2343 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n"); 2344 goto out; 2345 } 2346 2347 /* Show the tunnel or session context. 2348 */ 2349 if (pd->session == NULL) 2350 pppol2tp_seq_tunnel_show(m, pd->tunnel); 2351 else 2352 pppol2tp_seq_session_show(m, pd->session); 2353 2354out: 2355 return 0; 2356} 2357 2358static struct seq_operations pppol2tp_seq_ops = { 2359 .start = pppol2tp_seq_start, 2360 .next = pppol2tp_seq_next, 2361 .stop = pppol2tp_seq_stop, 2362 .show = pppol2tp_seq_show, 2363}; 2364 2365/* Called when our /proc file is opened. We allocate data for use when 2366 * iterating our tunnel / session contexts and store it in the private 2367 * data of the seq_file. 2368 */ 2369static int pppol2tp_proc_open(struct inode *inode, struct file *file) 2370{ 2371 struct seq_file *m; 2372 struct pppol2tp_seq_data *pd; 2373 int ret = 0; 2374 2375 ret = seq_open(file, &pppol2tp_seq_ops); 2376 if (ret < 0) 2377 goto out; 2378 2379 m = file->private_data; 2380 2381 /* Allocate and fill our proc_data for access later */ 2382 ret = -ENOMEM; 2383 m->private = kzalloc(sizeof(struct pppol2tp_seq_data), GFP_KERNEL); 2384 if (m->private == NULL) 2385 goto out; 2386 2387 pd = m->private; 2388 ret = 0; 2389 2390out: 2391 return ret; 2392} 2393 2394/* Called when /proc file access completes. 2395 */ 2396static int pppol2tp_proc_release(struct inode *inode, struct file *file) 2397{ 2398 struct seq_file *m = (struct seq_file *)file->private_data; 2399 2400 kfree(m->private); 2401 m->private = NULL; 2402 2403 return seq_release(inode, file); 2404} 2405 2406static struct file_operations pppol2tp_proc_fops = { 2407 .owner = THIS_MODULE, 2408 .open = pppol2tp_proc_open, 2409 .read = seq_read, 2410 .llseek = seq_lseek, 2411 .release = pppol2tp_proc_release, 2412}; 2413 2414static struct proc_dir_entry *pppol2tp_proc; 2415 2416#endif /* CONFIG_PROC_FS */ 2417 2418/***************************************************************************** 2419 * Init and cleanup 2420 *****************************************************************************/ 2421 2422static struct proto_ops pppol2tp_ops = { 2423 .family = AF_PPPOX, 2424 .owner = THIS_MODULE, 2425 .release = pppol2tp_release, 2426 .bind = sock_no_bind, 2427 .connect = pppol2tp_connect, 2428 .socketpair = sock_no_socketpair, 2429 .accept = sock_no_accept, 2430 .getname = pppol2tp_getname, 2431 .poll = datagram_poll, 2432 .listen = sock_no_listen, 2433 .shutdown = sock_no_shutdown, 2434 .setsockopt = pppol2tp_setsockopt, 2435 .getsockopt = pppol2tp_getsockopt, 2436 .sendmsg = pppol2tp_sendmsg, 2437 .recvmsg = pppol2tp_recvmsg, 2438 .mmap = sock_no_mmap, 2439 .ioctl = pppox_ioctl, 2440}; 2441 2442static struct pppox_proto pppol2tp_proto = { 2443 .create = pppol2tp_create, 2444 .ioctl = pppol2tp_ioctl 2445}; 2446 2447static int __init pppol2tp_init(void) 2448{ 2449 int err; 2450 2451 err = proto_register(&pppol2tp_sk_proto, 0); 2452 if (err) 2453 goto out; 2454 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto); 2455 if (err) 2456 goto out_unregister_pppol2tp_proto; 2457 2458#ifdef CONFIG_PROC_FS 2459 pppol2tp_proc = create_proc_entry("pppol2tp", 0, init_net.proc_net); 2460 if (!pppol2tp_proc) { 2461 err = -ENOMEM; 2462 goto out_unregister_pppox_proto; 2463 } 2464 pppol2tp_proc->proc_fops = &pppol2tp_proc_fops; 2465#endif /* CONFIG_PROC_FS */ 2466 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n", 2467 PPPOL2TP_DRV_VERSION); 2468 2469out: 2470 return err; 2471 2472out_unregister_pppox_proto: 2473 unregister_pppox_proto(PX_PROTO_OL2TP); 2474out_unregister_pppol2tp_proto: 2475 proto_unregister(&pppol2tp_sk_proto); 2476 goto out; 2477} 2478 2479static void __exit pppol2tp_exit(void) 2480{ 2481 unregister_pppox_proto(PX_PROTO_OL2TP); 2482 2483#ifdef CONFIG_PROC_FS 2484 remove_proc_entry("pppol2tp", init_net.proc_net); 2485#endif 2486 proto_unregister(&pppol2tp_sk_proto); 2487} 2488 2489module_init(pppol2tp_init); 2490module_exit(pppol2tp_exit); 2491 2492MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>," 2493 "James Chapman <jchapman@katalix.com>"); 2494MODULE_DESCRIPTION("PPP over L2TP over UDP"); 2495MODULE_LICENSE("GPL"); 2496MODULE_VERSION(PPPOL2TP_DRV_VERSION);