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