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