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