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