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