1 /* 2 * L2TP core. 3 * 4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd 5 * 6 * This file contains some code of the original L2TPv2 pppol2tp 7 * driver, which has the following copyright: 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 * This program is free software; you can redistribute it and/or modify 17 * it under the terms of the GNU General Public License version 2 as 18 * published by the Free Software Foundation. 19 */ 20 21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22 23 #include <linux/module.h> 24 #include <linux/string.h> 25 #include <linux/list.h> 26 #include <linux/rculist.h> 27 #include <linux/uaccess.h> 28 29 #include <linux/kernel.h> 30 #include <linux/spinlock.h> 31 #include <linux/kthread.h> 32 #include <linux/sched.h> 33 #include <linux/slab.h> 34 #include <linux/errno.h> 35 #include <linux/jiffies.h> 36 37 #include <linux/netdevice.h> 38 #include <linux/net.h> 39 #include <linux/inetdevice.h> 40 #include <linux/skbuff.h> 41 #include <linux/init.h> 42 #include <linux/in.h> 43 #include <linux/ip.h> 44 #include <linux/udp.h> 45 #include <linux/l2tp.h> 46 #include <linux/hash.h> 47 #include <linux/sort.h> 48 #include <linux/file.h> 49 #include <linux/nsproxy.h> 50 #include <net/net_namespace.h> 51 #include <net/netns/generic.h> 52 #include <net/dst.h> 53 #include <net/ip.h> 54 #include <net/udp.h> 55 #include <net/udp_tunnel.h> 56 #include <net/inet_common.h> 57 #include <net/xfrm.h> 58 #include <net/protocol.h> 59 #include <net/inet6_connection_sock.h> 60 #include <net/inet_ecn.h> 61 #include <net/ip6_route.h> 62 #include <net/ip6_checksum.h> 63 64 #include <asm/byteorder.h> 65 #include <linux/atomic.h> 66 67 #include "l2tp_core.h" 68 69 #define L2TP_DRV_VERSION "V2.0" 70 71 /* L2TP header constants */ 72 #define L2TP_HDRFLAG_T 0x8000 73 #define L2TP_HDRFLAG_L 0x4000 74 #define L2TP_HDRFLAG_S 0x0800 75 #define L2TP_HDRFLAG_O 0x0200 76 #define L2TP_HDRFLAG_P 0x0100 77 78 #define L2TP_HDR_VER_MASK 0x000F 79 #define L2TP_HDR_VER_2 0x0002 80 #define L2TP_HDR_VER_3 0x0003 81 82 /* L2TPv3 default L2-specific sublayer */ 83 #define L2TP_SLFLAG_S 0x40000000 84 #define L2TP_SL_SEQ_MASK 0x00ffffff 85 86 #define L2TP_HDR_SIZE_SEQ 10 87 #define L2TP_HDR_SIZE_NOSEQ 6 88 89 /* Default trace flags */ 90 #define L2TP_DEFAULT_DEBUG_FLAGS 0 91 92 /* Private data stored for received packets in the skb. 93 */ 94 struct l2tp_skb_cb { 95 u32 ns; 96 u16 has_seq; 97 u16 length; 98 unsigned long expires; 99 }; 100 101 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)]) 102 103 static atomic_t l2tp_tunnel_count; 104 static atomic_t l2tp_session_count; 105 static struct workqueue_struct *l2tp_wq; 106 107 /* per-net private data for this module */ 108 static unsigned int l2tp_net_id; 109 struct l2tp_net { 110 struct list_head l2tp_tunnel_list; 111 spinlock_t l2tp_tunnel_list_lock; 112 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2]; 113 spinlock_t l2tp_session_hlist_lock; 114 }; 115 116 117 static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk) 118 { 119 return sk->sk_user_data; 120 } 121 122 static inline struct l2tp_net *l2tp_pernet(const struct net *net) 123 { 124 BUG_ON(!net); 125 126 return net_generic(net, l2tp_net_id); 127 } 128 129 /* Session hash global list for L2TPv3. 130 * The session_id SHOULD be random according to RFC3931, but several 131 * L2TP implementations use incrementing session_ids. So we do a real 132 * hash on the session_id, rather than a simple bitmask. 133 */ 134 static inline struct hlist_head * 135 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id) 136 { 137 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)]; 138 139 } 140 141 /* Lookup the tunnel socket, possibly involving the fs code if the socket is 142 * owned by userspace. A struct sock returned from this function must be 143 * released using l2tp_tunnel_sock_put once you're done with it. 144 */ 145 static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel) 146 { 147 int err = 0; 148 struct socket *sock = NULL; 149 struct sock *sk = NULL; 150 151 if (!tunnel) 152 goto out; 153 154 if (tunnel->fd >= 0) { 155 /* Socket is owned by userspace, who might be in the process 156 * of closing it. Look the socket up using the fd to ensure 157 * consistency. 158 */ 159 sock = sockfd_lookup(tunnel->fd, &err); 160 if (sock) 161 sk = sock->sk; 162 } else { 163 /* Socket is owned by kernelspace */ 164 sk = tunnel->sock; 165 sock_hold(sk); 166 } 167 168 out: 169 return sk; 170 } 171 172 /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */ 173 static void l2tp_tunnel_sock_put(struct sock *sk) 174 { 175 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); 176 if (tunnel) { 177 if (tunnel->fd >= 0) { 178 /* Socket is owned by userspace */ 179 sockfd_put(sk->sk_socket); 180 } 181 sock_put(sk); 182 } 183 sock_put(sk); 184 } 185 186 /* Session hash list. 187 * The session_id SHOULD be random according to RFC2661, but several 188 * L2TP implementations (Cisco and Microsoft) use incrementing 189 * session_ids. So we do a real hash on the session_id, rather than a 190 * simple bitmask. 191 */ 192 static inline struct hlist_head * 193 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id) 194 { 195 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)]; 196 } 197 198 /* Lookup a tunnel. A new reference is held on the returned tunnel. */ 199 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id) 200 { 201 const struct l2tp_net *pn = l2tp_pernet(net); 202 struct l2tp_tunnel *tunnel; 203 204 rcu_read_lock_bh(); 205 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 206 if (tunnel->tunnel_id == tunnel_id) { 207 l2tp_tunnel_inc_refcount(tunnel); 208 rcu_read_unlock_bh(); 209 210 return tunnel; 211 } 212 } 213 rcu_read_unlock_bh(); 214 215 return NULL; 216 } 217 EXPORT_SYMBOL_GPL(l2tp_tunnel_get); 218 219 /* Lookup a session. A new reference is held on the returned session. 220 * Optionally calls session->ref() too if do_ref is true. 221 */ 222 struct l2tp_session *l2tp_session_get(const struct net *net, 223 struct l2tp_tunnel *tunnel, 224 u32 session_id, bool do_ref) 225 { 226 struct hlist_head *session_list; 227 struct l2tp_session *session; 228 229 if (!tunnel) { 230 struct l2tp_net *pn = l2tp_pernet(net); 231 232 session_list = l2tp_session_id_hash_2(pn, session_id); 233 234 rcu_read_lock_bh(); 235 hlist_for_each_entry_rcu(session, session_list, global_hlist) { 236 if (session->session_id == session_id) { 237 l2tp_session_inc_refcount(session); 238 if (do_ref && session->ref) 239 session->ref(session); 240 rcu_read_unlock_bh(); 241 242 return session; 243 } 244 } 245 rcu_read_unlock_bh(); 246 247 return NULL; 248 } 249 250 session_list = l2tp_session_id_hash(tunnel, session_id); 251 read_lock_bh(&tunnel->hlist_lock); 252 hlist_for_each_entry(session, session_list, hlist) { 253 if (session->session_id == session_id) { 254 l2tp_session_inc_refcount(session); 255 if (do_ref && session->ref) 256 session->ref(session); 257 read_unlock_bh(&tunnel->hlist_lock); 258 259 return session; 260 } 261 } 262 read_unlock_bh(&tunnel->hlist_lock); 263 264 return NULL; 265 } 266 EXPORT_SYMBOL_GPL(l2tp_session_get); 267 268 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth, 269 bool do_ref) 270 { 271 int hash; 272 struct l2tp_session *session; 273 int count = 0; 274 275 read_lock_bh(&tunnel->hlist_lock); 276 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 277 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) { 278 if (++count > nth) { 279 l2tp_session_inc_refcount(session); 280 if (do_ref && session->ref) 281 session->ref(session); 282 read_unlock_bh(&tunnel->hlist_lock); 283 return session; 284 } 285 } 286 } 287 288 read_unlock_bh(&tunnel->hlist_lock); 289 290 return NULL; 291 } 292 EXPORT_SYMBOL_GPL(l2tp_session_get_nth); 293 294 /* Lookup a session by interface name. 295 * This is very inefficient but is only used by management interfaces. 296 */ 297 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, 298 const char *ifname, 299 bool do_ref) 300 { 301 struct l2tp_net *pn = l2tp_pernet(net); 302 int hash; 303 struct l2tp_session *session; 304 305 rcu_read_lock_bh(); 306 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) { 307 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) { 308 if (!strcmp(session->ifname, ifname)) { 309 l2tp_session_inc_refcount(session); 310 if (do_ref && session->ref) 311 session->ref(session); 312 rcu_read_unlock_bh(); 313 314 return session; 315 } 316 } 317 } 318 319 rcu_read_unlock_bh(); 320 321 return NULL; 322 } 323 EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname); 324 325 int l2tp_session_register(struct l2tp_session *session, 326 struct l2tp_tunnel *tunnel) 327 { 328 struct l2tp_session *session_walk; 329 struct hlist_head *g_head; 330 struct hlist_head *head; 331 struct l2tp_net *pn; 332 int err; 333 334 head = l2tp_session_id_hash(tunnel, session->session_id); 335 336 write_lock_bh(&tunnel->hlist_lock); 337 if (!tunnel->acpt_newsess) { 338 err = -ENODEV; 339 goto err_tlock; 340 } 341 342 hlist_for_each_entry(session_walk, head, hlist) 343 if (session_walk->session_id == session->session_id) { 344 err = -EEXIST; 345 goto err_tlock; 346 } 347 348 if (tunnel->version == L2TP_HDR_VER_3) { 349 pn = l2tp_pernet(tunnel->l2tp_net); 350 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net), 351 session->session_id); 352 353 spin_lock_bh(&pn->l2tp_session_hlist_lock); 354 355 hlist_for_each_entry(session_walk, g_head, global_hlist) 356 if (session_walk->session_id == session->session_id) { 357 err = -EEXIST; 358 goto err_tlock_pnlock; 359 } 360 361 l2tp_tunnel_inc_refcount(tunnel); 362 sock_hold(tunnel->sock); 363 hlist_add_head_rcu(&session->global_hlist, g_head); 364 365 spin_unlock_bh(&pn->l2tp_session_hlist_lock); 366 } else { 367 l2tp_tunnel_inc_refcount(tunnel); 368 sock_hold(tunnel->sock); 369 } 370 371 hlist_add_head(&session->hlist, head); 372 write_unlock_bh(&tunnel->hlist_lock); 373 374 /* Ignore management session in session count value */ 375 if (session->session_id != 0) 376 atomic_inc(&l2tp_session_count); 377 378 return 0; 379 380 err_tlock_pnlock: 381 spin_unlock_bh(&pn->l2tp_session_hlist_lock); 382 err_tlock: 383 write_unlock_bh(&tunnel->hlist_lock); 384 385 return err; 386 } 387 EXPORT_SYMBOL_GPL(l2tp_session_register); 388 389 /* Lookup a tunnel by id 390 */ 391 struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id) 392 { 393 struct l2tp_tunnel *tunnel; 394 struct l2tp_net *pn = l2tp_pernet(net); 395 396 rcu_read_lock_bh(); 397 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 398 if (tunnel->tunnel_id == tunnel_id) { 399 rcu_read_unlock_bh(); 400 return tunnel; 401 } 402 } 403 rcu_read_unlock_bh(); 404 405 return NULL; 406 } 407 EXPORT_SYMBOL_GPL(l2tp_tunnel_find); 408 409 struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth) 410 { 411 struct l2tp_net *pn = l2tp_pernet(net); 412 struct l2tp_tunnel *tunnel; 413 int count = 0; 414 415 rcu_read_lock_bh(); 416 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 417 if (++count > nth) { 418 rcu_read_unlock_bh(); 419 return tunnel; 420 } 421 } 422 423 rcu_read_unlock_bh(); 424 425 return NULL; 426 } 427 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth); 428 429 /***************************************************************************** 430 * Receive data handling 431 *****************************************************************************/ 432 433 /* Queue a skb in order. We come here only if the skb has an L2TP sequence 434 * number. 435 */ 436 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb) 437 { 438 struct sk_buff *skbp; 439 struct sk_buff *tmp; 440 u32 ns = L2TP_SKB_CB(skb)->ns; 441 442 spin_lock_bh(&session->reorder_q.lock); 443 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 444 if (L2TP_SKB_CB(skbp)->ns > ns) { 445 __skb_queue_before(&session->reorder_q, skbp, skb); 446 l2tp_dbg(session, L2TP_MSG_SEQ, 447 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 448 session->name, ns, L2TP_SKB_CB(skbp)->ns, 449 skb_queue_len(&session->reorder_q)); 450 atomic_long_inc(&session->stats.rx_oos_packets); 451 goto out; 452 } 453 } 454 455 __skb_queue_tail(&session->reorder_q, skb); 456 457 out: 458 spin_unlock_bh(&session->reorder_q.lock); 459 } 460 461 /* Dequeue a single skb. 462 */ 463 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb) 464 { 465 struct l2tp_tunnel *tunnel = session->tunnel; 466 int length = L2TP_SKB_CB(skb)->length; 467 468 /* We're about to requeue the skb, so return resources 469 * to its current owner (a socket receive buffer). 470 */ 471 skb_orphan(skb); 472 473 atomic_long_inc(&tunnel->stats.rx_packets); 474 atomic_long_add(length, &tunnel->stats.rx_bytes); 475 atomic_long_inc(&session->stats.rx_packets); 476 atomic_long_add(length, &session->stats.rx_bytes); 477 478 if (L2TP_SKB_CB(skb)->has_seq) { 479 /* Bump our Nr */ 480 session->nr++; 481 session->nr &= session->nr_max; 482 483 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", 484 session->name, session->nr); 485 } 486 487 /* call private receive handler */ 488 if (session->recv_skb != NULL) 489 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length); 490 else 491 kfree_skb(skb); 492 493 if (session->deref) 494 (*session->deref)(session); 495 } 496 497 /* Dequeue skbs from the session's reorder_q, subject to packet order. 498 * Skbs that have been in the queue for too long are simply discarded. 499 */ 500 static void l2tp_recv_dequeue(struct l2tp_session *session) 501 { 502 struct sk_buff *skb; 503 struct sk_buff *tmp; 504 505 /* If the pkt at the head of the queue has the nr that we 506 * expect to send up next, dequeue it and any other 507 * in-sequence packets behind it. 508 */ 509 start: 510 spin_lock_bh(&session->reorder_q.lock); 511 skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 512 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 513 atomic_long_inc(&session->stats.rx_seq_discards); 514 atomic_long_inc(&session->stats.rx_errors); 515 l2tp_dbg(session, L2TP_MSG_SEQ, 516 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", 517 session->name, L2TP_SKB_CB(skb)->ns, 518 L2TP_SKB_CB(skb)->length, session->nr, 519 skb_queue_len(&session->reorder_q)); 520 session->reorder_skip = 1; 521 __skb_unlink(skb, &session->reorder_q); 522 kfree_skb(skb); 523 if (session->deref) 524 (*session->deref)(session); 525 continue; 526 } 527 528 if (L2TP_SKB_CB(skb)->has_seq) { 529 if (session->reorder_skip) { 530 l2tp_dbg(session, L2TP_MSG_SEQ, 531 "%s: advancing nr to next pkt: %u -> %u", 532 session->name, session->nr, 533 L2TP_SKB_CB(skb)->ns); 534 session->reorder_skip = 0; 535 session->nr = L2TP_SKB_CB(skb)->ns; 536 } 537 if (L2TP_SKB_CB(skb)->ns != session->nr) { 538 l2tp_dbg(session, L2TP_MSG_SEQ, 539 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", 540 session->name, L2TP_SKB_CB(skb)->ns, 541 L2TP_SKB_CB(skb)->length, session->nr, 542 skb_queue_len(&session->reorder_q)); 543 goto out; 544 } 545 } 546 __skb_unlink(skb, &session->reorder_q); 547 548 /* Process the skb. We release the queue lock while we 549 * do so to let other contexts process the queue. 550 */ 551 spin_unlock_bh(&session->reorder_q.lock); 552 l2tp_recv_dequeue_skb(session, skb); 553 goto start; 554 } 555 556 out: 557 spin_unlock_bh(&session->reorder_q.lock); 558 } 559 560 static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr) 561 { 562 u32 nws; 563 564 if (nr >= session->nr) 565 nws = nr - session->nr; 566 else 567 nws = (session->nr_max + 1) - (session->nr - nr); 568 569 return nws < session->nr_window_size; 570 } 571 572 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if 573 * acceptable, else non-zero. 574 */ 575 static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb) 576 { 577 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) { 578 /* Packet sequence number is outside allowed window. 579 * Discard it. 580 */ 581 l2tp_dbg(session, L2TP_MSG_SEQ, 582 "%s: pkt %u len %d discarded, outside window, nr=%u\n", 583 session->name, L2TP_SKB_CB(skb)->ns, 584 L2TP_SKB_CB(skb)->length, session->nr); 585 goto discard; 586 } 587 588 if (session->reorder_timeout != 0) { 589 /* Packet reordering enabled. Add skb to session's 590 * reorder queue, in order of ns. 591 */ 592 l2tp_recv_queue_skb(session, skb); 593 goto out; 594 } 595 596 /* Packet reordering disabled. Discard out-of-sequence packets, while 597 * tracking the number if in-sequence packets after the first OOS packet 598 * is seen. After nr_oos_count_max in-sequence packets, reset the 599 * sequence number to re-enable packet reception. 600 */ 601 if (L2TP_SKB_CB(skb)->ns == session->nr) { 602 skb_queue_tail(&session->reorder_q, skb); 603 } else { 604 u32 nr_oos = L2TP_SKB_CB(skb)->ns; 605 u32 nr_next = (session->nr_oos + 1) & session->nr_max; 606 607 if (nr_oos == nr_next) 608 session->nr_oos_count++; 609 else 610 session->nr_oos_count = 0; 611 612 session->nr_oos = nr_oos; 613 if (session->nr_oos_count > session->nr_oos_count_max) { 614 session->reorder_skip = 1; 615 l2tp_dbg(session, L2TP_MSG_SEQ, 616 "%s: %d oos packets received. Resetting sequence numbers\n", 617 session->name, session->nr_oos_count); 618 } 619 if (!session->reorder_skip) { 620 atomic_long_inc(&session->stats.rx_seq_discards); 621 l2tp_dbg(session, L2TP_MSG_SEQ, 622 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", 623 session->name, L2TP_SKB_CB(skb)->ns, 624 L2TP_SKB_CB(skb)->length, session->nr, 625 skb_queue_len(&session->reorder_q)); 626 goto discard; 627 } 628 skb_queue_tail(&session->reorder_q, skb); 629 } 630 631 out: 632 return 0; 633 634 discard: 635 return 1; 636 } 637 638 /* Do receive processing of L2TP data frames. We handle both L2TPv2 639 * and L2TPv3 data frames here. 640 * 641 * L2TPv2 Data Message Header 642 * 643 * 0 1 2 3 644 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 645 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 646 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) | 647 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 648 * | Tunnel ID | Session ID | 649 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 650 * | Ns (opt) | Nr (opt) | 651 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 652 * | Offset Size (opt) | Offset pad... (opt) 653 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 654 * 655 * Data frames are marked by T=0. All other fields are the same as 656 * those in L2TP control frames. 657 * 658 * L2TPv3 Data Message Header 659 * 660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 661 * | L2TP Session Header | 662 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 663 * | L2-Specific Sublayer | 664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 665 * | Tunnel Payload ... 666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 667 * 668 * L2TPv3 Session Header Over IP 669 * 670 * 0 1 2 3 671 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 672 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 673 * | Session ID | 674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 675 * | Cookie (optional, maximum 64 bits)... 676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 677 * | 678 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 679 * 680 * L2TPv3 L2-Specific Sublayer Format 681 * 682 * 0 1 2 3 683 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 684 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 685 * |x|S|x|x|x|x|x|x| Sequence Number | 686 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 687 * 688 * Cookie value, sublayer format and offset (pad) are negotiated with 689 * the peer when the session is set up. Unlike L2TPv2, we do not need 690 * to parse the packet header to determine if optional fields are 691 * present. 692 * 693 * Caller must already have parsed the frame and determined that it is 694 * a data (not control) frame before coming here. Fields up to the 695 * session-id have already been parsed and ptr points to the data 696 * after the session-id. 697 * 698 * session->ref() must have been called prior to l2tp_recv_common(). 699 * session->deref() will be called automatically after skb is processed. 700 */ 701 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, 702 unsigned char *ptr, unsigned char *optr, u16 hdrflags, 703 int length, int (*payload_hook)(struct sk_buff *skb)) 704 { 705 struct l2tp_tunnel *tunnel = session->tunnel; 706 int offset; 707 u32 ns, nr; 708 709 /* Parse and check optional cookie */ 710 if (session->peer_cookie_len > 0) { 711 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 712 l2tp_info(tunnel, L2TP_MSG_DATA, 713 "%s: cookie mismatch (%u/%u). Discarding.\n", 714 tunnel->name, tunnel->tunnel_id, 715 session->session_id); 716 atomic_long_inc(&session->stats.rx_cookie_discards); 717 goto discard; 718 } 719 ptr += session->peer_cookie_len; 720 } 721 722 /* Handle the optional sequence numbers. Sequence numbers are 723 * in different places for L2TPv2 and L2TPv3. 724 * 725 * If we are the LAC, enable/disable sequence numbers under 726 * the control of the LNS. If no sequence numbers present but 727 * we were expecting them, discard frame. 728 */ 729 ns = nr = 0; 730 L2TP_SKB_CB(skb)->has_seq = 0; 731 if (tunnel->version == L2TP_HDR_VER_2) { 732 if (hdrflags & L2TP_HDRFLAG_S) { 733 ns = ntohs(*(__be16 *) ptr); 734 ptr += 2; 735 nr = ntohs(*(__be16 *) ptr); 736 ptr += 2; 737 738 /* Store L2TP info in the skb */ 739 L2TP_SKB_CB(skb)->ns = ns; 740 L2TP_SKB_CB(skb)->has_seq = 1; 741 742 l2tp_dbg(session, L2TP_MSG_SEQ, 743 "%s: recv data ns=%u, nr=%u, session nr=%u\n", 744 session->name, ns, nr, session->nr); 745 } 746 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 747 u32 l2h = ntohl(*(__be32 *) ptr); 748 749 if (l2h & 0x40000000) { 750 ns = l2h & 0x00ffffff; 751 752 /* Store L2TP info in the skb */ 753 L2TP_SKB_CB(skb)->ns = ns; 754 L2TP_SKB_CB(skb)->has_seq = 1; 755 756 l2tp_dbg(session, L2TP_MSG_SEQ, 757 "%s: recv data ns=%u, session nr=%u\n", 758 session->name, ns, session->nr); 759 } 760 } 761 762 /* Advance past L2-specific header, if present */ 763 ptr += session->l2specific_len; 764 765 if (L2TP_SKB_CB(skb)->has_seq) { 766 /* Received a packet with sequence numbers. If we're the LNS, 767 * check if we sre sending sequence numbers and if not, 768 * configure it so. 769 */ 770 if ((!session->lns_mode) && (!session->send_seq)) { 771 l2tp_info(session, L2TP_MSG_SEQ, 772 "%s: requested to enable seq numbers by LNS\n", 773 session->name); 774 session->send_seq = 1; 775 l2tp_session_set_header_len(session, tunnel->version); 776 } 777 } else { 778 /* No sequence numbers. 779 * If user has configured mandatory sequence numbers, discard. 780 */ 781 if (session->recv_seq) { 782 l2tp_warn(session, L2TP_MSG_SEQ, 783 "%s: recv data has no seq numbers when required. Discarding.\n", 784 session->name); 785 atomic_long_inc(&session->stats.rx_seq_discards); 786 goto discard; 787 } 788 789 /* If we're the LAC and we're sending sequence numbers, the 790 * LNS has requested that we no longer send sequence numbers. 791 * If we're the LNS and we're sending sequence numbers, the 792 * LAC is broken. Discard the frame. 793 */ 794 if ((!session->lns_mode) && (session->send_seq)) { 795 l2tp_info(session, L2TP_MSG_SEQ, 796 "%s: requested to disable seq numbers by LNS\n", 797 session->name); 798 session->send_seq = 0; 799 l2tp_session_set_header_len(session, tunnel->version); 800 } else if (session->send_seq) { 801 l2tp_warn(session, L2TP_MSG_SEQ, 802 "%s: recv data has no seq numbers when required. Discarding.\n", 803 session->name); 804 atomic_long_inc(&session->stats.rx_seq_discards); 805 goto discard; 806 } 807 } 808 809 /* Session data offset is handled differently for L2TPv2 and 810 * L2TPv3. For L2TPv2, there is an optional 16-bit value in 811 * the header. For L2TPv3, the offset is negotiated using AVPs 812 * in the session setup control protocol. 813 */ 814 if (tunnel->version == L2TP_HDR_VER_2) { 815 /* If offset bit set, skip it. */ 816 if (hdrflags & L2TP_HDRFLAG_O) { 817 offset = ntohs(*(__be16 *)ptr); 818 ptr += 2 + offset; 819 } 820 } else 821 ptr += session->offset; 822 823 offset = ptr - optr; 824 if (!pskb_may_pull(skb, offset)) 825 goto discard; 826 827 __skb_pull(skb, offset); 828 829 /* If caller wants to process the payload before we queue the 830 * packet, do so now. 831 */ 832 if (payload_hook) 833 if ((*payload_hook)(skb)) 834 goto discard; 835 836 /* Prepare skb for adding to the session's reorder_q. Hold 837 * packets for max reorder_timeout or 1 second if not 838 * reordering. 839 */ 840 L2TP_SKB_CB(skb)->length = length; 841 L2TP_SKB_CB(skb)->expires = jiffies + 842 (session->reorder_timeout ? session->reorder_timeout : HZ); 843 844 /* Add packet to the session's receive queue. Reordering is done here, if 845 * enabled. Saved L2TP protocol info is stored in skb->sb[]. 846 */ 847 if (L2TP_SKB_CB(skb)->has_seq) { 848 if (l2tp_recv_data_seq(session, skb)) 849 goto discard; 850 } else { 851 /* No sequence numbers. Add the skb to the tail of the 852 * reorder queue. This ensures that it will be 853 * delivered after all previous sequenced skbs. 854 */ 855 skb_queue_tail(&session->reorder_q, skb); 856 } 857 858 /* Try to dequeue as many skbs from reorder_q as we can. */ 859 l2tp_recv_dequeue(session); 860 861 return; 862 863 discard: 864 atomic_long_inc(&session->stats.rx_errors); 865 kfree_skb(skb); 866 867 if (session->deref) 868 (*session->deref)(session); 869 } 870 EXPORT_SYMBOL(l2tp_recv_common); 871 872 /* Drop skbs from the session's reorder_q 873 */ 874 int l2tp_session_queue_purge(struct l2tp_session *session) 875 { 876 struct sk_buff *skb = NULL; 877 BUG_ON(!session); 878 BUG_ON(session->magic != L2TP_SESSION_MAGIC); 879 while ((skb = skb_dequeue(&session->reorder_q))) { 880 atomic_long_inc(&session->stats.rx_errors); 881 kfree_skb(skb); 882 if (session->deref) 883 (*session->deref)(session); 884 } 885 return 0; 886 } 887 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge); 888 889 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame 890 * here. The skb is not on a list when we get here. 891 * Returns 0 if the packet was a data packet and was successfully passed on. 892 * Returns 1 if the packet was not a good data packet and could not be 893 * forwarded. All such packets are passed up to userspace to deal with. 894 */ 895 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, 896 int (*payload_hook)(struct sk_buff *skb)) 897 { 898 struct l2tp_session *session = NULL; 899 unsigned char *ptr, *optr; 900 u16 hdrflags; 901 u32 tunnel_id, session_id; 902 u16 version; 903 int length; 904 905 /* UDP has verifed checksum */ 906 907 /* UDP always verifies the packet length. */ 908 __skb_pull(skb, sizeof(struct udphdr)); 909 910 /* Short packet? */ 911 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { 912 l2tp_info(tunnel, L2TP_MSG_DATA, 913 "%s: recv short packet (len=%d)\n", 914 tunnel->name, skb->len); 915 goto error; 916 } 917 918 /* Trace packet contents, if enabled */ 919 if (tunnel->debug & L2TP_MSG_DATA) { 920 length = min(32u, skb->len); 921 if (!pskb_may_pull(skb, length)) 922 goto error; 923 924 pr_debug("%s: recv\n", tunnel->name); 925 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); 926 } 927 928 /* Point to L2TP header */ 929 optr = ptr = skb->data; 930 931 /* Get L2TP header flags */ 932 hdrflags = ntohs(*(__be16 *) ptr); 933 934 /* Check protocol version */ 935 version = hdrflags & L2TP_HDR_VER_MASK; 936 if (version != tunnel->version) { 937 l2tp_info(tunnel, L2TP_MSG_DATA, 938 "%s: recv protocol version mismatch: got %d expected %d\n", 939 tunnel->name, version, tunnel->version); 940 goto error; 941 } 942 943 /* Get length of L2TP packet */ 944 length = skb->len; 945 946 /* If type is control packet, it is handled by userspace. */ 947 if (hdrflags & L2TP_HDRFLAG_T) { 948 l2tp_dbg(tunnel, L2TP_MSG_DATA, 949 "%s: recv control packet, len=%d\n", 950 tunnel->name, length); 951 goto error; 952 } 953 954 /* Skip flags */ 955 ptr += 2; 956 957 if (tunnel->version == L2TP_HDR_VER_2) { 958 /* If length is present, skip it */ 959 if (hdrflags & L2TP_HDRFLAG_L) 960 ptr += 2; 961 962 /* Extract tunnel and session ID */ 963 tunnel_id = ntohs(*(__be16 *) ptr); 964 ptr += 2; 965 session_id = ntohs(*(__be16 *) ptr); 966 ptr += 2; 967 } else { 968 ptr += 2; /* skip reserved bits */ 969 tunnel_id = tunnel->tunnel_id; 970 session_id = ntohl(*(__be32 *) ptr); 971 ptr += 4; 972 } 973 974 /* Find the session context */ 975 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true); 976 if (!session || !session->recv_skb) { 977 if (session) { 978 if (session->deref) 979 session->deref(session); 980 l2tp_session_dec_refcount(session); 981 } 982 983 /* Not found? Pass to userspace to deal with */ 984 l2tp_info(tunnel, L2TP_MSG_DATA, 985 "%s: no session found (%u/%u). Passing up.\n", 986 tunnel->name, tunnel_id, session_id); 987 goto error; 988 } 989 990 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook); 991 l2tp_session_dec_refcount(session); 992 993 return 0; 994 995 error: 996 /* Put UDP header back */ 997 __skb_push(skb, sizeof(struct udphdr)); 998 999 return 1; 1000 } 1001 1002 /* UDP encapsulation receive handler. See net/ipv4/udp.c. 1003 * Return codes: 1004 * 0 : success. 1005 * <0: error 1006 * >0: skb should be passed up to userspace as UDP. 1007 */ 1008 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 1009 { 1010 struct l2tp_tunnel *tunnel; 1011 1012 tunnel = l2tp_sock_to_tunnel(sk); 1013 if (tunnel == NULL) 1014 goto pass_up; 1015 1016 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", 1017 tunnel->name, skb->len); 1018 1019 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) 1020 goto pass_up_put; 1021 1022 sock_put(sk); 1023 return 0; 1024 1025 pass_up_put: 1026 sock_put(sk); 1027 pass_up: 1028 return 1; 1029 } 1030 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv); 1031 1032 /************************************************************************ 1033 * Transmit handling 1034 ***********************************************************************/ 1035 1036 /* Build an L2TP header for the session into the buffer provided. 1037 */ 1038 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf) 1039 { 1040 struct l2tp_tunnel *tunnel = session->tunnel; 1041 __be16 *bufp = buf; 1042 __be16 *optr = buf; 1043 u16 flags = L2TP_HDR_VER_2; 1044 u32 tunnel_id = tunnel->peer_tunnel_id; 1045 u32 session_id = session->peer_session_id; 1046 1047 if (session->send_seq) 1048 flags |= L2TP_HDRFLAG_S; 1049 1050 /* Setup L2TP header. */ 1051 *bufp++ = htons(flags); 1052 *bufp++ = htons(tunnel_id); 1053 *bufp++ = htons(session_id); 1054 if (session->send_seq) { 1055 *bufp++ = htons(session->ns); 1056 *bufp++ = 0; 1057 session->ns++; 1058 session->ns &= 0xffff; 1059 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", 1060 session->name, session->ns); 1061 } 1062 1063 return bufp - optr; 1064 } 1065 1066 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf) 1067 { 1068 struct l2tp_tunnel *tunnel = session->tunnel; 1069 char *bufp = buf; 1070 char *optr = bufp; 1071 1072 /* Setup L2TP header. The header differs slightly for UDP and 1073 * IP encapsulations. For UDP, there is 4 bytes of flags. 1074 */ 1075 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { 1076 u16 flags = L2TP_HDR_VER_3; 1077 *((__be16 *) bufp) = htons(flags); 1078 bufp += 2; 1079 *((__be16 *) bufp) = 0; 1080 bufp += 2; 1081 } 1082 1083 *((__be32 *) bufp) = htonl(session->peer_session_id); 1084 bufp += 4; 1085 if (session->cookie_len) { 1086 memcpy(bufp, &session->cookie[0], session->cookie_len); 1087 bufp += session->cookie_len; 1088 } 1089 if (session->l2specific_len) { 1090 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 1091 u32 l2h = 0; 1092 if (session->send_seq) { 1093 l2h = 0x40000000 | session->ns; 1094 session->ns++; 1095 session->ns &= 0xffffff; 1096 l2tp_dbg(session, L2TP_MSG_SEQ, 1097 "%s: updated ns to %u\n", 1098 session->name, session->ns); 1099 } 1100 1101 *((__be32 *) bufp) = htonl(l2h); 1102 } 1103 bufp += session->l2specific_len; 1104 } 1105 if (session->offset) 1106 bufp += session->offset; 1107 1108 return bufp - optr; 1109 } 1110 1111 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, 1112 struct flowi *fl, size_t data_len) 1113 { 1114 struct l2tp_tunnel *tunnel = session->tunnel; 1115 unsigned int len = skb->len; 1116 int error; 1117 1118 /* Debug */ 1119 if (session->send_seq) 1120 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n", 1121 session->name, data_len, session->ns - 1); 1122 else 1123 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n", 1124 session->name, data_len); 1125 1126 if (session->debug & L2TP_MSG_DATA) { 1127 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 1128 unsigned char *datap = skb->data + uhlen; 1129 1130 pr_debug("%s: xmit\n", session->name); 1131 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, 1132 datap, min_t(size_t, 32, len - uhlen)); 1133 } 1134 1135 /* Queue the packet to IP for output */ 1136 skb->ignore_df = 1; 1137 #if IS_ENABLED(CONFIG_IPV6) 1138 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped) 1139 error = inet6_csk_xmit(tunnel->sock, skb, NULL); 1140 else 1141 #endif 1142 error = ip_queue_xmit(tunnel->sock, skb, fl); 1143 1144 /* Update stats */ 1145 if (error >= 0) { 1146 atomic_long_inc(&tunnel->stats.tx_packets); 1147 atomic_long_add(len, &tunnel->stats.tx_bytes); 1148 atomic_long_inc(&session->stats.tx_packets); 1149 atomic_long_add(len, &session->stats.tx_bytes); 1150 } else { 1151 atomic_long_inc(&tunnel->stats.tx_errors); 1152 atomic_long_inc(&session->stats.tx_errors); 1153 } 1154 1155 return 0; 1156 } 1157 1158 /* If caller requires the skb to have a ppp header, the header must be 1159 * inserted in the skb data before calling this function. 1160 */ 1161 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len) 1162 { 1163 int data_len = skb->len; 1164 struct l2tp_tunnel *tunnel = session->tunnel; 1165 struct sock *sk = tunnel->sock; 1166 struct flowi *fl; 1167 struct udphdr *uh; 1168 struct inet_sock *inet; 1169 int headroom; 1170 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 1171 int udp_len; 1172 int ret = NET_XMIT_SUCCESS; 1173 1174 /* Check that there's enough headroom in the skb to insert IP, 1175 * UDP and L2TP headers. If not enough, expand it to 1176 * make room. Adjust truesize. 1177 */ 1178 headroom = NET_SKB_PAD + sizeof(struct iphdr) + 1179 uhlen + hdr_len; 1180 if (skb_cow_head(skb, headroom)) { 1181 kfree_skb(skb); 1182 return NET_XMIT_DROP; 1183 } 1184 1185 /* Setup L2TP header */ 1186 session->build_header(session, __skb_push(skb, hdr_len)); 1187 1188 /* Reset skb netfilter state */ 1189 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1190 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | 1191 IPSKB_REROUTED); 1192 nf_reset(skb); 1193 1194 bh_lock_sock(sk); 1195 if (sock_owned_by_user(sk)) { 1196 kfree_skb(skb); 1197 ret = NET_XMIT_DROP; 1198 goto out_unlock; 1199 } 1200 1201 /* Get routing info from the tunnel socket */ 1202 skb_dst_drop(skb); 1203 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); 1204 1205 inet = inet_sk(sk); 1206 fl = &inet->cork.fl; 1207 switch (tunnel->encap) { 1208 case L2TP_ENCAPTYPE_UDP: 1209 /* Setup UDP header */ 1210 __skb_push(skb, sizeof(*uh)); 1211 skb_reset_transport_header(skb); 1212 uh = udp_hdr(skb); 1213 uh->source = inet->inet_sport; 1214 uh->dest = inet->inet_dport; 1215 udp_len = uhlen + hdr_len + data_len; 1216 uh->len = htons(udp_len); 1217 1218 /* Calculate UDP checksum if configured to do so */ 1219 #if IS_ENABLED(CONFIG_IPV6) 1220 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped) 1221 udp6_set_csum(udp_get_no_check6_tx(sk), 1222 skb, &inet6_sk(sk)->saddr, 1223 &sk->sk_v6_daddr, udp_len); 1224 else 1225 #endif 1226 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr, 1227 inet->inet_daddr, udp_len); 1228 break; 1229 1230 case L2TP_ENCAPTYPE_IP: 1231 break; 1232 } 1233 1234 l2tp_xmit_core(session, skb, fl, data_len); 1235 out_unlock: 1236 bh_unlock_sock(sk); 1237 1238 return ret; 1239 } 1240 EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1241 1242 /***************************************************************************** 1243 * Tinnel and session create/destroy. 1244 *****************************************************************************/ 1245 1246 /* Tunnel socket destruct hook. 1247 * The tunnel context is deleted only when all session sockets have been 1248 * closed. 1249 */ 1250 static void l2tp_tunnel_destruct(struct sock *sk) 1251 { 1252 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk); 1253 struct l2tp_net *pn; 1254 1255 if (tunnel == NULL) 1256 goto end; 1257 1258 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); 1259 1260 1261 /* Disable udp encapsulation */ 1262 switch (tunnel->encap) { 1263 case L2TP_ENCAPTYPE_UDP: 1264 /* No longer an encapsulation socket. See net/ipv4/udp.c */ 1265 (udp_sk(sk))->encap_type = 0; 1266 (udp_sk(sk))->encap_rcv = NULL; 1267 (udp_sk(sk))->encap_destroy = NULL; 1268 break; 1269 case L2TP_ENCAPTYPE_IP: 1270 break; 1271 } 1272 1273 /* Remove hooks into tunnel socket */ 1274 sk->sk_destruct = tunnel->old_sk_destruct; 1275 sk->sk_user_data = NULL; 1276 1277 /* Remove the tunnel struct from the tunnel list */ 1278 pn = l2tp_pernet(tunnel->l2tp_net); 1279 spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1280 list_del_rcu(&tunnel->list); 1281 spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1282 atomic_dec(&l2tp_tunnel_count); 1283 1284 l2tp_tunnel_closeall(tunnel); 1285 1286 tunnel->sock = NULL; 1287 l2tp_tunnel_dec_refcount(tunnel); 1288 1289 /* Call the original destructor */ 1290 if (sk->sk_destruct) 1291 (*sk->sk_destruct)(sk); 1292 end: 1293 return; 1294 } 1295 1296 /* When the tunnel is closed, all the attached sessions need to go too. 1297 */ 1298 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel) 1299 { 1300 int hash; 1301 struct hlist_node *walk; 1302 struct hlist_node *tmp; 1303 struct l2tp_session *session; 1304 1305 BUG_ON(tunnel == NULL); 1306 1307 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", 1308 tunnel->name); 1309 1310 write_lock_bh(&tunnel->hlist_lock); 1311 tunnel->acpt_newsess = false; 1312 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1313 again: 1314 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1315 session = hlist_entry(walk, struct l2tp_session, hlist); 1316 1317 l2tp_info(session, L2TP_MSG_CONTROL, 1318 "%s: closing session\n", session->name); 1319 1320 hlist_del_init(&session->hlist); 1321 1322 if (test_and_set_bit(0, &session->dead)) 1323 goto again; 1324 1325 if (session->ref != NULL) 1326 (*session->ref)(session); 1327 1328 write_unlock_bh(&tunnel->hlist_lock); 1329 1330 __l2tp_session_unhash(session); 1331 l2tp_session_queue_purge(session); 1332 1333 if (session->session_close != NULL) 1334 (*session->session_close)(session); 1335 1336 if (session->deref != NULL) 1337 (*session->deref)(session); 1338 1339 l2tp_session_dec_refcount(session); 1340 1341 write_lock_bh(&tunnel->hlist_lock); 1342 1343 /* Now restart from the beginning of this hash 1344 * chain. We always remove a session from the 1345 * list so we are guaranteed to make forward 1346 * progress. 1347 */ 1348 goto again; 1349 } 1350 } 1351 write_unlock_bh(&tunnel->hlist_lock); 1352 } 1353 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall); 1354 1355 /* Tunnel socket destroy hook for UDP encapsulation */ 1356 static void l2tp_udp_encap_destroy(struct sock *sk) 1357 { 1358 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk); 1359 if (tunnel) { 1360 l2tp_tunnel_closeall(tunnel); 1361 sock_put(sk); 1362 } 1363 } 1364 1365 /* Workqueue tunnel deletion function */ 1366 static void l2tp_tunnel_del_work(struct work_struct *work) 1367 { 1368 struct l2tp_tunnel *tunnel = NULL; 1369 struct socket *sock = NULL; 1370 struct sock *sk = NULL; 1371 1372 tunnel = container_of(work, struct l2tp_tunnel, del_work); 1373 1374 l2tp_tunnel_closeall(tunnel); 1375 1376 sk = l2tp_tunnel_sock_lookup(tunnel); 1377 if (!sk) 1378 goto out; 1379 1380 sock = sk->sk_socket; 1381 1382 /* If the tunnel socket was created by userspace, then go through the 1383 * inet layer to shut the socket down, and let userspace close it. 1384 * Otherwise, if we created the socket directly within the kernel, use 1385 * the sk API to release it here. 1386 * In either case the tunnel resources are freed in the socket 1387 * destructor when the tunnel socket goes away. 1388 */ 1389 if (tunnel->fd >= 0) { 1390 if (sock) 1391 inet_shutdown(sock, 2); 1392 } else { 1393 if (sock) { 1394 kernel_sock_shutdown(sock, SHUT_RDWR); 1395 sock_release(sock); 1396 } 1397 } 1398 1399 l2tp_tunnel_sock_put(sk); 1400 out: 1401 l2tp_tunnel_dec_refcount(tunnel); 1402 } 1403 1404 /* Create a socket for the tunnel, if one isn't set up by 1405 * userspace. This is used for static tunnels where there is no 1406 * managing L2TP daemon. 1407 * 1408 * Since we don't want these sockets to keep a namespace alive by 1409 * themselves, we drop the socket's namespace refcount after creation. 1410 * These sockets are freed when the namespace exits using the pernet 1411 * exit hook. 1412 */ 1413 static int l2tp_tunnel_sock_create(struct net *net, 1414 u32 tunnel_id, 1415 u32 peer_tunnel_id, 1416 struct l2tp_tunnel_cfg *cfg, 1417 struct socket **sockp) 1418 { 1419 int err = -EINVAL; 1420 struct socket *sock = NULL; 1421 struct udp_port_cfg udp_conf; 1422 1423 switch (cfg->encap) { 1424 case L2TP_ENCAPTYPE_UDP: 1425 memset(&udp_conf, 0, sizeof(udp_conf)); 1426 1427 #if IS_ENABLED(CONFIG_IPV6) 1428 if (cfg->local_ip6 && cfg->peer_ip6) { 1429 udp_conf.family = AF_INET6; 1430 memcpy(&udp_conf.local_ip6, cfg->local_ip6, 1431 sizeof(udp_conf.local_ip6)); 1432 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6, 1433 sizeof(udp_conf.peer_ip6)); 1434 udp_conf.use_udp6_tx_checksums = 1435 ! cfg->udp6_zero_tx_checksums; 1436 udp_conf.use_udp6_rx_checksums = 1437 ! cfg->udp6_zero_rx_checksums; 1438 } else 1439 #endif 1440 { 1441 udp_conf.family = AF_INET; 1442 udp_conf.local_ip = cfg->local_ip; 1443 udp_conf.peer_ip = cfg->peer_ip; 1444 udp_conf.use_udp_checksums = cfg->use_udp_checksums; 1445 } 1446 1447 udp_conf.local_udp_port = htons(cfg->local_udp_port); 1448 udp_conf.peer_udp_port = htons(cfg->peer_udp_port); 1449 1450 err = udp_sock_create(net, &udp_conf, &sock); 1451 if (err < 0) 1452 goto out; 1453 1454 break; 1455 1456 case L2TP_ENCAPTYPE_IP: 1457 #if IS_ENABLED(CONFIG_IPV6) 1458 if (cfg->local_ip6 && cfg->peer_ip6) { 1459 struct sockaddr_l2tpip6 ip6_addr = {0}; 1460 1461 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 1462 IPPROTO_L2TP, &sock); 1463 if (err < 0) 1464 goto out; 1465 1466 ip6_addr.l2tp_family = AF_INET6; 1467 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6, 1468 sizeof(ip6_addr.l2tp_addr)); 1469 ip6_addr.l2tp_conn_id = tunnel_id; 1470 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr, 1471 sizeof(ip6_addr)); 1472 if (err < 0) 1473 goto out; 1474 1475 ip6_addr.l2tp_family = AF_INET6; 1476 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6, 1477 sizeof(ip6_addr.l2tp_addr)); 1478 ip6_addr.l2tp_conn_id = peer_tunnel_id; 1479 err = kernel_connect(sock, 1480 (struct sockaddr *) &ip6_addr, 1481 sizeof(ip6_addr), 0); 1482 if (err < 0) 1483 goto out; 1484 } else 1485 #endif 1486 { 1487 struct sockaddr_l2tpip ip_addr = {0}; 1488 1489 err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 1490 IPPROTO_L2TP, &sock); 1491 if (err < 0) 1492 goto out; 1493 1494 ip_addr.l2tp_family = AF_INET; 1495 ip_addr.l2tp_addr = cfg->local_ip; 1496 ip_addr.l2tp_conn_id = tunnel_id; 1497 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, 1498 sizeof(ip_addr)); 1499 if (err < 0) 1500 goto out; 1501 1502 ip_addr.l2tp_family = AF_INET; 1503 ip_addr.l2tp_addr = cfg->peer_ip; 1504 ip_addr.l2tp_conn_id = peer_tunnel_id; 1505 err = kernel_connect(sock, (struct sockaddr *) &ip_addr, 1506 sizeof(ip_addr), 0); 1507 if (err < 0) 1508 goto out; 1509 } 1510 break; 1511 1512 default: 1513 goto out; 1514 } 1515 1516 out: 1517 *sockp = sock; 1518 if ((err < 0) && sock) { 1519 kernel_sock_shutdown(sock, SHUT_RDWR); 1520 sock_release(sock); 1521 *sockp = NULL; 1522 } 1523 1524 return err; 1525 } 1526 1527 static struct lock_class_key l2tp_socket_class; 1528 1529 int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp) 1530 { 1531 struct l2tp_tunnel *tunnel = NULL; 1532 int err; 1533 struct socket *sock = NULL; 1534 struct sock *sk = NULL; 1535 struct l2tp_net *pn; 1536 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP; 1537 1538 /* Get the tunnel socket from the fd, which was opened by 1539 * the userspace L2TP daemon. If not specified, create a 1540 * kernel socket. 1541 */ 1542 if (fd < 0) { 1543 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id, 1544 cfg, &sock); 1545 if (err < 0) 1546 goto err; 1547 } else { 1548 sock = sockfd_lookup(fd, &err); 1549 if (!sock) { 1550 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n", 1551 tunnel_id, fd, err); 1552 err = -EBADF; 1553 goto err; 1554 } 1555 1556 /* Reject namespace mismatches */ 1557 if (!net_eq(sock_net(sock->sk), net)) { 1558 pr_err("tunl %u: netns mismatch\n", tunnel_id); 1559 err = -EINVAL; 1560 goto err; 1561 } 1562 } 1563 1564 sk = sock->sk; 1565 1566 if (cfg != NULL) 1567 encap = cfg->encap; 1568 1569 /* Quick sanity checks */ 1570 switch (encap) { 1571 case L2TP_ENCAPTYPE_UDP: 1572 err = -EPROTONOSUPPORT; 1573 if (sk->sk_protocol != IPPROTO_UDP) { 1574 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1575 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1576 goto err; 1577 } 1578 break; 1579 case L2TP_ENCAPTYPE_IP: 1580 err = -EPROTONOSUPPORT; 1581 if (sk->sk_protocol != IPPROTO_L2TP) { 1582 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1583 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); 1584 goto err; 1585 } 1586 break; 1587 } 1588 1589 /* Check if this socket has already been prepped */ 1590 tunnel = l2tp_tunnel(sk); 1591 if (tunnel != NULL) { 1592 /* This socket has already been prepped */ 1593 err = -EBUSY; 1594 goto err; 1595 } 1596 1597 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL); 1598 if (tunnel == NULL) { 1599 err = -ENOMEM; 1600 goto err; 1601 } 1602 1603 tunnel->version = version; 1604 tunnel->tunnel_id = tunnel_id; 1605 tunnel->peer_tunnel_id = peer_tunnel_id; 1606 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS; 1607 1608 tunnel->magic = L2TP_TUNNEL_MAGIC; 1609 sprintf(&tunnel->name[0], "tunl %u", tunnel_id); 1610 rwlock_init(&tunnel->hlist_lock); 1611 tunnel->acpt_newsess = true; 1612 1613 /* The net we belong to */ 1614 tunnel->l2tp_net = net; 1615 pn = l2tp_pernet(net); 1616 1617 if (cfg != NULL) 1618 tunnel->debug = cfg->debug; 1619 1620 #if IS_ENABLED(CONFIG_IPV6) 1621 if (sk->sk_family == PF_INET6) { 1622 struct ipv6_pinfo *np = inet6_sk(sk); 1623 1624 if (ipv6_addr_v4mapped(&np->saddr) && 1625 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { 1626 struct inet_sock *inet = inet_sk(sk); 1627 1628 tunnel->v4mapped = true; 1629 inet->inet_saddr = np->saddr.s6_addr32[3]; 1630 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3]; 1631 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3]; 1632 } else { 1633 tunnel->v4mapped = false; 1634 } 1635 } 1636 #endif 1637 1638 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 1639 tunnel->encap = encap; 1640 if (encap == L2TP_ENCAPTYPE_UDP) { 1641 struct udp_tunnel_sock_cfg udp_cfg = { }; 1642 1643 udp_cfg.sk_user_data = tunnel; 1644 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP; 1645 udp_cfg.encap_rcv = l2tp_udp_encap_recv; 1646 udp_cfg.encap_destroy = l2tp_udp_encap_destroy; 1647 1648 setup_udp_tunnel_sock(net, sock, &udp_cfg); 1649 } else { 1650 sk->sk_user_data = tunnel; 1651 } 1652 1653 /* Hook on the tunnel socket destructor so that we can cleanup 1654 * if the tunnel socket goes away. 1655 */ 1656 tunnel->old_sk_destruct = sk->sk_destruct; 1657 sk->sk_destruct = &l2tp_tunnel_destruct; 1658 tunnel->sock = sk; 1659 tunnel->fd = fd; 1660 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock"); 1661 1662 sk->sk_allocation = GFP_ATOMIC; 1663 1664 /* Init delete workqueue struct */ 1665 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work); 1666 1667 /* Add tunnel to our list */ 1668 INIT_LIST_HEAD(&tunnel->list); 1669 atomic_inc(&l2tp_tunnel_count); 1670 1671 /* Bump the reference count. The tunnel context is deleted 1672 * only when this drops to zero. Must be done before list insertion 1673 */ 1674 refcount_set(&tunnel->ref_count, 1); 1675 spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1676 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list); 1677 spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1678 1679 err = 0; 1680 err: 1681 if (tunnelp) 1682 *tunnelp = tunnel; 1683 1684 /* If tunnel's socket was created by the kernel, it doesn't 1685 * have a file. 1686 */ 1687 if (sock && sock->file) 1688 sockfd_put(sock); 1689 1690 return err; 1691 } 1692 EXPORT_SYMBOL_GPL(l2tp_tunnel_create); 1693 1694 /* This function is used by the netlink TUNNEL_DELETE command. 1695 */ 1696 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel) 1697 { 1698 if (!test_and_set_bit(0, &tunnel->dead)) { 1699 l2tp_tunnel_inc_refcount(tunnel); 1700 queue_work(l2tp_wq, &tunnel->del_work); 1701 } 1702 } 1703 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete); 1704 1705 /* Really kill the session. 1706 */ 1707 void l2tp_session_free(struct l2tp_session *session) 1708 { 1709 struct l2tp_tunnel *tunnel = session->tunnel; 1710 1711 BUG_ON(refcount_read(&session->ref_count) != 0); 1712 1713 if (tunnel) { 1714 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 1715 if (session->session_id != 0) 1716 atomic_dec(&l2tp_session_count); 1717 sock_put(tunnel->sock); 1718 session->tunnel = NULL; 1719 l2tp_tunnel_dec_refcount(tunnel); 1720 } 1721 1722 kfree(session); 1723 } 1724 EXPORT_SYMBOL_GPL(l2tp_session_free); 1725 1726 /* Remove an l2tp session from l2tp_core's hash lists. 1727 * Provides a tidyup interface for pseudowire code which can't just route all 1728 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close 1729 * callback. 1730 */ 1731 void __l2tp_session_unhash(struct l2tp_session *session) 1732 { 1733 struct l2tp_tunnel *tunnel = session->tunnel; 1734 1735 /* Remove the session from core hashes */ 1736 if (tunnel) { 1737 /* Remove from the per-tunnel hash */ 1738 write_lock_bh(&tunnel->hlist_lock); 1739 hlist_del_init(&session->hlist); 1740 write_unlock_bh(&tunnel->hlist_lock); 1741 1742 /* For L2TPv3 we have a per-net hash: remove from there, too */ 1743 if (tunnel->version != L2TP_HDR_VER_2) { 1744 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net); 1745 spin_lock_bh(&pn->l2tp_session_hlist_lock); 1746 hlist_del_init_rcu(&session->global_hlist); 1747 spin_unlock_bh(&pn->l2tp_session_hlist_lock); 1748 synchronize_rcu(); 1749 } 1750 } 1751 } 1752 EXPORT_SYMBOL_GPL(__l2tp_session_unhash); 1753 1754 /* This function is used by the netlink SESSION_DELETE command and by 1755 pseudowire modules. 1756 */ 1757 int l2tp_session_delete(struct l2tp_session *session) 1758 { 1759 if (test_and_set_bit(0, &session->dead)) 1760 return 0; 1761 1762 if (session->ref) 1763 (*session->ref)(session); 1764 __l2tp_session_unhash(session); 1765 l2tp_session_queue_purge(session); 1766 if (session->session_close != NULL) 1767 (*session->session_close)(session); 1768 if (session->deref) 1769 (*session->deref)(session); 1770 l2tp_session_dec_refcount(session); 1771 return 0; 1772 } 1773 EXPORT_SYMBOL_GPL(l2tp_session_delete); 1774 1775 /* We come here whenever a session's send_seq, cookie_len or 1776 * l2specific_len parameters are set. 1777 */ 1778 void l2tp_session_set_header_len(struct l2tp_session *session, int version) 1779 { 1780 if (version == L2TP_HDR_VER_2) { 1781 session->hdr_len = 6; 1782 if (session->send_seq) 1783 session->hdr_len += 4; 1784 } else { 1785 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset; 1786 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP) 1787 session->hdr_len += 4; 1788 } 1789 1790 } 1791 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len); 1792 1793 struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) 1794 { 1795 struct l2tp_session *session; 1796 1797 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL); 1798 if (session != NULL) { 1799 session->magic = L2TP_SESSION_MAGIC; 1800 session->tunnel = tunnel; 1801 1802 session->session_id = session_id; 1803 session->peer_session_id = peer_session_id; 1804 session->nr = 0; 1805 if (tunnel->version == L2TP_HDR_VER_2) 1806 session->nr_max = 0xffff; 1807 else 1808 session->nr_max = 0xffffff; 1809 session->nr_window_size = session->nr_max / 2; 1810 session->nr_oos_count_max = 4; 1811 1812 /* Use NR of first received packet */ 1813 session->reorder_skip = 1; 1814 1815 sprintf(&session->name[0], "sess %u/%u", 1816 tunnel->tunnel_id, session->session_id); 1817 1818 skb_queue_head_init(&session->reorder_q); 1819 1820 INIT_HLIST_NODE(&session->hlist); 1821 INIT_HLIST_NODE(&session->global_hlist); 1822 1823 /* Inherit debug options from tunnel */ 1824 session->debug = tunnel->debug; 1825 1826 if (cfg) { 1827 session->pwtype = cfg->pw_type; 1828 session->debug = cfg->debug; 1829 session->mtu = cfg->mtu; 1830 session->mru = cfg->mru; 1831 session->send_seq = cfg->send_seq; 1832 session->recv_seq = cfg->recv_seq; 1833 session->lns_mode = cfg->lns_mode; 1834 session->reorder_timeout = cfg->reorder_timeout; 1835 session->offset = cfg->offset; 1836 session->l2specific_type = cfg->l2specific_type; 1837 session->l2specific_len = cfg->l2specific_len; 1838 session->cookie_len = cfg->cookie_len; 1839 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len); 1840 session->peer_cookie_len = cfg->peer_cookie_len; 1841 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len); 1842 } 1843 1844 if (tunnel->version == L2TP_HDR_VER_2) 1845 session->build_header = l2tp_build_l2tpv2_header; 1846 else 1847 session->build_header = l2tp_build_l2tpv3_header; 1848 1849 l2tp_session_set_header_len(session, tunnel->version); 1850 1851 refcount_set(&session->ref_count, 1); 1852 1853 return session; 1854 } 1855 1856 return ERR_PTR(-ENOMEM); 1857 } 1858 EXPORT_SYMBOL_GPL(l2tp_session_create); 1859 1860 /***************************************************************************** 1861 * Init and cleanup 1862 *****************************************************************************/ 1863 1864 static __net_init int l2tp_init_net(struct net *net) 1865 { 1866 struct l2tp_net *pn = net_generic(net, l2tp_net_id); 1867 int hash; 1868 1869 INIT_LIST_HEAD(&pn->l2tp_tunnel_list); 1870 spin_lock_init(&pn->l2tp_tunnel_list_lock); 1871 1872 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) 1873 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]); 1874 1875 spin_lock_init(&pn->l2tp_session_hlist_lock); 1876 1877 return 0; 1878 } 1879 1880 static __net_exit void l2tp_exit_net(struct net *net) 1881 { 1882 struct l2tp_net *pn = l2tp_pernet(net); 1883 struct l2tp_tunnel *tunnel = NULL; 1884 1885 rcu_read_lock_bh(); 1886 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) { 1887 l2tp_tunnel_delete(tunnel); 1888 } 1889 rcu_read_unlock_bh(); 1890 1891 flush_workqueue(l2tp_wq); 1892 rcu_barrier(); 1893 } 1894 1895 static struct pernet_operations l2tp_net_ops = { 1896 .init = l2tp_init_net, 1897 .exit = l2tp_exit_net, 1898 .id = &l2tp_net_id, 1899 .size = sizeof(struct l2tp_net), 1900 }; 1901 1902 static int __init l2tp_init(void) 1903 { 1904 int rc = 0; 1905 1906 rc = register_pernet_device(&l2tp_net_ops); 1907 if (rc) 1908 goto out; 1909 1910 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0); 1911 if (!l2tp_wq) { 1912 pr_err("alloc_workqueue failed\n"); 1913 unregister_pernet_device(&l2tp_net_ops); 1914 rc = -ENOMEM; 1915 goto out; 1916 } 1917 1918 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1919 1920 out: 1921 return rc; 1922 } 1923 1924 static void __exit l2tp_exit(void) 1925 { 1926 unregister_pernet_device(&l2tp_net_ops); 1927 if (l2tp_wq) { 1928 destroy_workqueue(l2tp_wq); 1929 l2tp_wq = NULL; 1930 } 1931 } 1932 1933 module_init(l2tp_init); 1934 module_exit(l2tp_exit); 1935 1936 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); 1937 MODULE_DESCRIPTION("L2TP core"); 1938 MODULE_LICENSE("GPL"); 1939 MODULE_VERSION(L2TP_DRV_VERSION); 1940 1941