4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
6 * This file contains some code of the original L2TPv2 pppol2tp
7 * driver, which has the following copyright:
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
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.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
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>
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>
55 #include <net/udp_tunnel.h>
56 #include <net/inet_common.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>
64 #include <asm/byteorder.h>
65 #include <linux/atomic.h>
67 #include "l2tp_core.h"
69 #define L2TP_DRV_VERSION "V2.0"
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
78 #define L2TP_HDR_VER_MASK 0x000F
79 #define L2TP_HDR_VER_2 0x0002
80 #define L2TP_HDR_VER_3 0x0003
82 /* L2TPv3 default L2-specific sublayer */
83 #define L2TP_SLFLAG_S 0x40000000
84 #define L2TP_SL_SEQ_MASK 0x00ffffff
86 #define L2TP_HDR_SIZE_MAX 14
88 /* Default trace flags */
89 #define L2TP_DEFAULT_DEBUG_FLAGS 0
91 /* Private data stored for received packets in the skb.
97 unsigned long expires;
100 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
102 static atomic_t l2tp_tunnel_count;
103 static atomic_t l2tp_session_count;
104 static struct workqueue_struct *l2tp_wq;
106 /* per-net private data for this module */
107 static unsigned int l2tp_net_id;
109 struct list_head l2tp_tunnel_list;
110 spinlock_t l2tp_tunnel_list_lock;
111 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
112 spinlock_t l2tp_session_hlist_lock;
115 #if IS_ENABLED(CONFIG_IPV6)
116 static bool l2tp_sk_is_v6(struct sock *sk)
118 return sk->sk_family == PF_INET6 &&
119 !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
123 static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
125 return sk->sk_user_data;
128 static inline struct l2tp_net *l2tp_pernet(const struct net *net)
132 return net_generic(net, l2tp_net_id);
135 /* Session hash global list for L2TPv3.
136 * The session_id SHOULD be random according to RFC3931, but several
137 * L2TP implementations use incrementing session_ids. So we do a real
138 * hash on the session_id, rather than a simple bitmask.
140 static inline struct hlist_head *
141 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
143 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
147 /* Lookup the tunnel socket, possibly involving the fs code if the socket is
148 * owned by userspace. A struct sock returned from this function must be
149 * released using l2tp_tunnel_sock_put once you're done with it.
151 static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
154 struct socket *sock = NULL;
155 struct sock *sk = NULL;
160 if (tunnel->fd >= 0) {
161 /* Socket is owned by userspace, who might be in the process
162 * of closing it. Look the socket up using the fd to ensure
165 sock = sockfd_lookup(tunnel->fd, &err);
169 /* Socket is owned by kernelspace */
178 /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
179 static void l2tp_tunnel_sock_put(struct sock *sk)
181 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
183 if (tunnel->fd >= 0) {
184 /* Socket is owned by userspace */
185 sockfd_put(sk->sk_socket);
192 /* Session hash list.
193 * The session_id SHOULD be random according to RFC2661, but several
194 * L2TP implementations (Cisco and Microsoft) use incrementing
195 * session_ids. So we do a real hash on the session_id, rather than a
198 static inline struct hlist_head *
199 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
201 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
204 /* Lookup a tunnel. A new reference is held on the returned tunnel. */
205 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
207 const struct l2tp_net *pn = l2tp_pernet(net);
208 struct l2tp_tunnel *tunnel;
211 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
212 if (tunnel->tunnel_id == tunnel_id) {
213 l2tp_tunnel_inc_refcount(tunnel);
214 rcu_read_unlock_bh();
219 rcu_read_unlock_bh();
223 EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
225 /* Lookup a session. A new reference is held on the returned session.
226 * Optionally calls session->ref() too if do_ref is true.
228 struct l2tp_session *l2tp_session_get(const struct net *net,
229 struct l2tp_tunnel *tunnel,
230 u32 session_id, bool do_ref)
232 struct hlist_head *session_list;
233 struct l2tp_session *session;
236 struct l2tp_net *pn = l2tp_pernet(net);
238 session_list = l2tp_session_id_hash_2(pn, session_id);
241 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
242 if (session->session_id == session_id) {
243 l2tp_session_inc_refcount(session);
244 if (do_ref && session->ref)
245 session->ref(session);
246 rcu_read_unlock_bh();
251 rcu_read_unlock_bh();
256 session_list = l2tp_session_id_hash(tunnel, session_id);
257 read_lock_bh(&tunnel->hlist_lock);
258 hlist_for_each_entry(session, session_list, hlist) {
259 if (session->session_id == session_id) {
260 l2tp_session_inc_refcount(session);
261 if (do_ref && session->ref)
262 session->ref(session);
263 read_unlock_bh(&tunnel->hlist_lock);
268 read_unlock_bh(&tunnel->hlist_lock);
272 EXPORT_SYMBOL_GPL(l2tp_session_get);
274 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
278 struct l2tp_session *session;
281 read_lock_bh(&tunnel->hlist_lock);
282 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
283 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
285 l2tp_session_inc_refcount(session);
286 if (do_ref && session->ref)
287 session->ref(session);
288 read_unlock_bh(&tunnel->hlist_lock);
294 read_unlock_bh(&tunnel->hlist_lock);
298 EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
300 /* Lookup a session by interface name.
301 * This is very inefficient but is only used by management interfaces.
303 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
307 struct l2tp_net *pn = l2tp_pernet(net);
309 struct l2tp_session *session;
312 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
313 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
314 if (!strcmp(session->ifname, ifname)) {
315 l2tp_session_inc_refcount(session);
316 if (do_ref && session->ref)
317 session->ref(session);
318 rcu_read_unlock_bh();
325 rcu_read_unlock_bh();
329 EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
331 int l2tp_session_register(struct l2tp_session *session,
332 struct l2tp_tunnel *tunnel)
334 struct l2tp_session *session_walk;
335 struct hlist_head *g_head;
336 struct hlist_head *head;
340 head = l2tp_session_id_hash(tunnel, session->session_id);
342 write_lock_bh(&tunnel->hlist_lock);
343 if (!tunnel->acpt_newsess) {
348 hlist_for_each_entry(session_walk, head, hlist)
349 if (session_walk->session_id == session->session_id) {
354 if (tunnel->version == L2TP_HDR_VER_3) {
355 pn = l2tp_pernet(tunnel->l2tp_net);
356 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
357 session->session_id);
359 spin_lock_bh(&pn->l2tp_session_hlist_lock);
361 /* IP encap expects session IDs to be globally unique, while
364 hlist_for_each_entry(session_walk, g_head, global_hlist)
365 if (session_walk->session_id == session->session_id &&
366 (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP ||
367 tunnel->encap == L2TP_ENCAPTYPE_IP)) {
369 goto err_tlock_pnlock;
372 l2tp_tunnel_inc_refcount(tunnel);
373 sock_hold(tunnel->sock);
374 hlist_add_head_rcu(&session->global_hlist, g_head);
376 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
378 l2tp_tunnel_inc_refcount(tunnel);
379 sock_hold(tunnel->sock);
382 hlist_add_head(&session->hlist, head);
383 write_unlock_bh(&tunnel->hlist_lock);
385 /* Ignore management session in session count value */
386 if (session->session_id != 0)
387 atomic_inc(&l2tp_session_count);
392 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
394 write_unlock_bh(&tunnel->hlist_lock);
398 EXPORT_SYMBOL_GPL(l2tp_session_register);
400 /* Lookup a tunnel by id
402 struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id)
404 struct l2tp_tunnel *tunnel;
405 struct l2tp_net *pn = l2tp_pernet(net);
408 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
409 if (tunnel->tunnel_id == tunnel_id) {
410 rcu_read_unlock_bh();
414 rcu_read_unlock_bh();
418 EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
420 struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth)
422 struct l2tp_net *pn = l2tp_pernet(net);
423 struct l2tp_tunnel *tunnel;
427 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
429 rcu_read_unlock_bh();
434 rcu_read_unlock_bh();
438 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
440 /*****************************************************************************
441 * Receive data handling
442 *****************************************************************************/
444 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
447 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
449 struct sk_buff *skbp;
451 u32 ns = L2TP_SKB_CB(skb)->ns;
453 spin_lock_bh(&session->reorder_q.lock);
454 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
455 if (L2TP_SKB_CB(skbp)->ns > ns) {
456 __skb_queue_before(&session->reorder_q, skbp, skb);
457 l2tp_dbg(session, L2TP_MSG_SEQ,
458 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
459 session->name, ns, L2TP_SKB_CB(skbp)->ns,
460 skb_queue_len(&session->reorder_q));
461 atomic_long_inc(&session->stats.rx_oos_packets);
466 __skb_queue_tail(&session->reorder_q, skb);
469 spin_unlock_bh(&session->reorder_q.lock);
472 /* Dequeue a single skb.
474 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
476 struct l2tp_tunnel *tunnel = session->tunnel;
477 int length = L2TP_SKB_CB(skb)->length;
479 /* We're about to requeue the skb, so return resources
480 * to its current owner (a socket receive buffer).
484 atomic_long_inc(&tunnel->stats.rx_packets);
485 atomic_long_add(length, &tunnel->stats.rx_bytes);
486 atomic_long_inc(&session->stats.rx_packets);
487 atomic_long_add(length, &session->stats.rx_bytes);
489 if (L2TP_SKB_CB(skb)->has_seq) {
492 session->nr &= session->nr_max;
494 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
495 session->name, session->nr);
498 /* call private receive handler */
499 if (session->recv_skb != NULL)
500 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
505 (*session->deref)(session);
508 /* Dequeue skbs from the session's reorder_q, subject to packet order.
509 * Skbs that have been in the queue for too long are simply discarded.
511 static void l2tp_recv_dequeue(struct l2tp_session *session)
516 /* If the pkt at the head of the queue has the nr that we
517 * expect to send up next, dequeue it and any other
518 * in-sequence packets behind it.
521 spin_lock_bh(&session->reorder_q.lock);
522 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
523 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
524 atomic_long_inc(&session->stats.rx_seq_discards);
525 atomic_long_inc(&session->stats.rx_errors);
526 l2tp_dbg(session, L2TP_MSG_SEQ,
527 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
528 session->name, L2TP_SKB_CB(skb)->ns,
529 L2TP_SKB_CB(skb)->length, session->nr,
530 skb_queue_len(&session->reorder_q));
531 session->reorder_skip = 1;
532 __skb_unlink(skb, &session->reorder_q);
535 (*session->deref)(session);
539 if (L2TP_SKB_CB(skb)->has_seq) {
540 if (session->reorder_skip) {
541 l2tp_dbg(session, L2TP_MSG_SEQ,
542 "%s: advancing nr to next pkt: %u -> %u",
543 session->name, session->nr,
544 L2TP_SKB_CB(skb)->ns);
545 session->reorder_skip = 0;
546 session->nr = L2TP_SKB_CB(skb)->ns;
548 if (L2TP_SKB_CB(skb)->ns != session->nr) {
549 l2tp_dbg(session, L2TP_MSG_SEQ,
550 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
551 session->name, L2TP_SKB_CB(skb)->ns,
552 L2TP_SKB_CB(skb)->length, session->nr,
553 skb_queue_len(&session->reorder_q));
557 __skb_unlink(skb, &session->reorder_q);
559 /* Process the skb. We release the queue lock while we
560 * do so to let other contexts process the queue.
562 spin_unlock_bh(&session->reorder_q.lock);
563 l2tp_recv_dequeue_skb(session, skb);
568 spin_unlock_bh(&session->reorder_q.lock);
571 static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
575 if (nr >= session->nr)
576 nws = nr - session->nr;
578 nws = (session->nr_max + 1) - (session->nr - nr);
580 return nws < session->nr_window_size;
583 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
584 * acceptable, else non-zero.
586 static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
588 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
589 /* Packet sequence number is outside allowed window.
592 l2tp_dbg(session, L2TP_MSG_SEQ,
593 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
594 session->name, L2TP_SKB_CB(skb)->ns,
595 L2TP_SKB_CB(skb)->length, session->nr);
599 if (session->reorder_timeout != 0) {
600 /* Packet reordering enabled. Add skb to session's
601 * reorder queue, in order of ns.
603 l2tp_recv_queue_skb(session, skb);
607 /* Packet reordering disabled. Discard out-of-sequence packets, while
608 * tracking the number if in-sequence packets after the first OOS packet
609 * is seen. After nr_oos_count_max in-sequence packets, reset the
610 * sequence number to re-enable packet reception.
612 if (L2TP_SKB_CB(skb)->ns == session->nr) {
613 skb_queue_tail(&session->reorder_q, skb);
615 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
616 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
618 if (nr_oos == nr_next)
619 session->nr_oos_count++;
621 session->nr_oos_count = 0;
623 session->nr_oos = nr_oos;
624 if (session->nr_oos_count > session->nr_oos_count_max) {
625 session->reorder_skip = 1;
626 l2tp_dbg(session, L2TP_MSG_SEQ,
627 "%s: %d oos packets received. Resetting sequence numbers\n",
628 session->name, session->nr_oos_count);
630 if (!session->reorder_skip) {
631 atomic_long_inc(&session->stats.rx_seq_discards);
632 l2tp_dbg(session, L2TP_MSG_SEQ,
633 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
634 session->name, L2TP_SKB_CB(skb)->ns,
635 L2TP_SKB_CB(skb)->length, session->nr,
636 skb_queue_len(&session->reorder_q));
639 skb_queue_tail(&session->reorder_q, skb);
649 /* Do receive processing of L2TP data frames. We handle both L2TPv2
650 * and L2TPv3 data frames here.
652 * L2TPv2 Data Message Header
655 * 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
656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659 * | Tunnel ID | Session ID |
660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
661 * | Ns (opt) | Nr (opt) |
662 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
663 * | Offset Size (opt) | Offset pad... (opt)
664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
666 * Data frames are marked by T=0. All other fields are the same as
667 * those in L2TP control frames.
669 * L2TPv3 Data Message Header
671 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
672 * | L2TP Session Header |
673 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
674 * | L2-Specific Sublayer |
675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
676 * | Tunnel Payload ...
677 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
679 * L2TPv3 Session Header Over IP
682 * 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
683 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
685 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 * | Cookie (optional, maximum 64 bits)...
687 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * L2TPv3 L2-Specific Sublayer Format
694 * 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
695 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
696 * |x|S|x|x|x|x|x|x| Sequence Number |
697 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
699 * Cookie value, sublayer format and offset (pad) are negotiated with
700 * the peer when the session is set up. Unlike L2TPv2, we do not need
701 * to parse the packet header to determine if optional fields are
704 * Caller must already have parsed the frame and determined that it is
705 * a data (not control) frame before coming here. Fields up to the
706 * session-id have already been parsed and ptr points to the data
707 * after the session-id.
709 * session->ref() must have been called prior to l2tp_recv_common().
710 * session->deref() will be called automatically after skb is processed.
712 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
713 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
714 int length, int (*payload_hook)(struct sk_buff *skb))
716 struct l2tp_tunnel *tunnel = session->tunnel;
720 /* Parse and check optional cookie */
721 if (session->peer_cookie_len > 0) {
722 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
723 l2tp_info(tunnel, L2TP_MSG_DATA,
724 "%s: cookie mismatch (%u/%u). Discarding.\n",
725 tunnel->name, tunnel->tunnel_id,
726 session->session_id);
727 atomic_long_inc(&session->stats.rx_cookie_discards);
730 ptr += session->peer_cookie_len;
733 /* Handle the optional sequence numbers. Sequence numbers are
734 * in different places for L2TPv2 and L2TPv3.
736 * If we are the LAC, enable/disable sequence numbers under
737 * the control of the LNS. If no sequence numbers present but
738 * we were expecting them, discard frame.
741 L2TP_SKB_CB(skb)->has_seq = 0;
742 if (tunnel->version == L2TP_HDR_VER_2) {
743 if (hdrflags & L2TP_HDRFLAG_S) {
744 ns = ntohs(*(__be16 *) ptr);
746 nr = ntohs(*(__be16 *) ptr);
749 /* Store L2TP info in the skb */
750 L2TP_SKB_CB(skb)->ns = ns;
751 L2TP_SKB_CB(skb)->has_seq = 1;
753 l2tp_dbg(session, L2TP_MSG_SEQ,
754 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
755 session->name, ns, nr, session->nr);
757 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
758 u32 l2h = ntohl(*(__be32 *) ptr);
760 if (l2h & 0x40000000) {
761 ns = l2h & 0x00ffffff;
763 /* Store L2TP info in the skb */
764 L2TP_SKB_CB(skb)->ns = ns;
765 L2TP_SKB_CB(skb)->has_seq = 1;
767 l2tp_dbg(session, L2TP_MSG_SEQ,
768 "%s: recv data ns=%u, session nr=%u\n",
769 session->name, ns, session->nr);
774 if (L2TP_SKB_CB(skb)->has_seq) {
775 /* Received a packet with sequence numbers. If we're the LNS,
776 * check if we sre sending sequence numbers and if not,
779 if ((!session->lns_mode) && (!session->send_seq)) {
780 l2tp_info(session, L2TP_MSG_SEQ,
781 "%s: requested to enable seq numbers by LNS\n",
783 session->send_seq = 1;
784 l2tp_session_set_header_len(session, tunnel->version);
787 /* No sequence numbers.
788 * If user has configured mandatory sequence numbers, discard.
790 if (session->recv_seq) {
791 l2tp_warn(session, L2TP_MSG_SEQ,
792 "%s: recv data has no seq numbers when required. Discarding.\n",
794 atomic_long_inc(&session->stats.rx_seq_discards);
798 /* If we're the LAC and we're sending sequence numbers, the
799 * LNS has requested that we no longer send sequence numbers.
800 * If we're the LNS and we're sending sequence numbers, the
801 * LAC is broken. Discard the frame.
803 if ((!session->lns_mode) && (session->send_seq)) {
804 l2tp_info(session, L2TP_MSG_SEQ,
805 "%s: requested to disable seq numbers by LNS\n",
807 session->send_seq = 0;
808 l2tp_session_set_header_len(session, tunnel->version);
809 } else if (session->send_seq) {
810 l2tp_warn(session, L2TP_MSG_SEQ,
811 "%s: recv data has no seq numbers when required. Discarding.\n",
813 atomic_long_inc(&session->stats.rx_seq_discards);
818 /* Session data offset is defined only for L2TPv2 and is
819 * indicated by an optional 16-bit value in the header.
821 if (tunnel->version == L2TP_HDR_VER_2) {
822 /* If offset bit set, skip it. */
823 if (hdrflags & L2TP_HDRFLAG_O) {
824 offset = ntohs(*(__be16 *)ptr);
830 if (!pskb_may_pull(skb, offset))
833 __skb_pull(skb, offset);
835 /* If caller wants to process the payload before we queue the
839 if ((*payload_hook)(skb))
842 /* Prepare skb for adding to the session's reorder_q. Hold
843 * packets for max reorder_timeout or 1 second if not
846 L2TP_SKB_CB(skb)->length = length;
847 L2TP_SKB_CB(skb)->expires = jiffies +
848 (session->reorder_timeout ? session->reorder_timeout : HZ);
850 /* Add packet to the session's receive queue. Reordering is done here, if
851 * enabled. Saved L2TP protocol info is stored in skb->sb[].
853 if (L2TP_SKB_CB(skb)->has_seq) {
854 if (l2tp_recv_data_seq(session, skb))
857 /* No sequence numbers. Add the skb to the tail of the
858 * reorder queue. This ensures that it will be
859 * delivered after all previous sequenced skbs.
861 skb_queue_tail(&session->reorder_q, skb);
864 /* Try to dequeue as many skbs from reorder_q as we can. */
865 l2tp_recv_dequeue(session);
870 atomic_long_inc(&session->stats.rx_errors);
874 (*session->deref)(session);
876 EXPORT_SYMBOL(l2tp_recv_common);
878 /* Drop skbs from the session's reorder_q
880 int l2tp_session_queue_purge(struct l2tp_session *session)
882 struct sk_buff *skb = NULL;
884 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
885 while ((skb = skb_dequeue(&session->reorder_q))) {
886 atomic_long_inc(&session->stats.rx_errors);
889 (*session->deref)(session);
893 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
895 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
896 * here. The skb is not on a list when we get here.
897 * Returns 0 if the packet was a data packet and was successfully passed on.
898 * Returns 1 if the packet was not a good data packet and could not be
899 * forwarded. All such packets are passed up to userspace to deal with.
901 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
902 int (*payload_hook)(struct sk_buff *skb))
904 struct l2tp_session *session = NULL;
905 unsigned char *ptr, *optr;
907 u32 tunnel_id, session_id;
911 /* UDP has verifed checksum */
913 /* UDP always verifies the packet length. */
914 __skb_pull(skb, sizeof(struct udphdr));
917 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) {
918 l2tp_info(tunnel, L2TP_MSG_DATA,
919 "%s: recv short packet (len=%d)\n",
920 tunnel->name, skb->len);
924 /* Trace packet contents, if enabled */
925 if (tunnel->debug & L2TP_MSG_DATA) {
926 length = min(32u, skb->len);
927 if (!pskb_may_pull(skb, length))
930 pr_debug("%s: recv\n", tunnel->name);
931 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
934 /* Point to L2TP header */
935 optr = ptr = skb->data;
937 /* Get L2TP header flags */
938 hdrflags = ntohs(*(__be16 *) ptr);
940 /* Check protocol version */
941 version = hdrflags & L2TP_HDR_VER_MASK;
942 if (version != tunnel->version) {
943 l2tp_info(tunnel, L2TP_MSG_DATA,
944 "%s: recv protocol version mismatch: got %d expected %d\n",
945 tunnel->name, version, tunnel->version);
949 /* Get length of L2TP packet */
952 /* If type is control packet, it is handled by userspace. */
953 if (hdrflags & L2TP_HDRFLAG_T) {
954 l2tp_dbg(tunnel, L2TP_MSG_DATA,
955 "%s: recv control packet, len=%d\n",
956 tunnel->name, length);
963 if (tunnel->version == L2TP_HDR_VER_2) {
964 /* If length is present, skip it */
965 if (hdrflags & L2TP_HDRFLAG_L)
968 /* Extract tunnel and session ID */
969 tunnel_id = ntohs(*(__be16 *) ptr);
971 session_id = ntohs(*(__be16 *) ptr);
974 ptr += 2; /* skip reserved bits */
975 tunnel_id = tunnel->tunnel_id;
976 session_id = ntohl(*(__be32 *) ptr);
980 /* Find the session context */
981 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
982 if (!session || !session->recv_skb) {
985 session->deref(session);
986 l2tp_session_dec_refcount(session);
989 /* Not found? Pass to userspace to deal with */
990 l2tp_info(tunnel, L2TP_MSG_DATA,
991 "%s: no session found (%u/%u). Passing up.\n",
992 tunnel->name, tunnel_id, session_id);
996 if (tunnel->version == L2TP_HDR_VER_3 &&
997 l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
998 l2tp_session_dec_refcount(session);
1002 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
1003 l2tp_session_dec_refcount(session);
1008 /* Put UDP header back */
1009 __skb_push(skb, sizeof(struct udphdr));
1014 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
1018 * >0: skb should be passed up to userspace as UDP.
1020 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1022 struct l2tp_tunnel *tunnel;
1024 tunnel = l2tp_sock_to_tunnel(sk);
1028 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1029 tunnel->name, skb->len);
1031 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1042 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1044 /************************************************************************
1046 ***********************************************************************/
1048 /* Build an L2TP header for the session into the buffer provided.
1050 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
1052 struct l2tp_tunnel *tunnel = session->tunnel;
1055 u16 flags = L2TP_HDR_VER_2;
1056 u32 tunnel_id = tunnel->peer_tunnel_id;
1057 u32 session_id = session->peer_session_id;
1059 if (session->send_seq)
1060 flags |= L2TP_HDRFLAG_S;
1062 /* Setup L2TP header. */
1063 *bufp++ = htons(flags);
1064 *bufp++ = htons(tunnel_id);
1065 *bufp++ = htons(session_id);
1066 if (session->send_seq) {
1067 *bufp++ = htons(session->ns);
1070 session->ns &= 0xffff;
1071 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1072 session->name, session->ns);
1078 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1080 struct l2tp_tunnel *tunnel = session->tunnel;
1084 /* Setup L2TP header. The header differs slightly for UDP and
1085 * IP encapsulations. For UDP, there is 4 bytes of flags.
1087 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1088 u16 flags = L2TP_HDR_VER_3;
1089 *((__be16 *) bufp) = htons(flags);
1091 *((__be16 *) bufp) = 0;
1095 *((__be32 *) bufp) = htonl(session->peer_session_id);
1097 if (session->cookie_len) {
1098 memcpy(bufp, &session->cookie[0], session->cookie_len);
1099 bufp += session->cookie_len;
1101 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1104 if (session->send_seq) {
1105 l2h = 0x40000000 | session->ns;
1107 session->ns &= 0xffffff;
1108 l2tp_dbg(session, L2TP_MSG_SEQ,
1109 "%s: updated ns to %u\n",
1110 session->name, session->ns);
1113 *((__be32 *)bufp) = htonl(l2h);
1120 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1121 struct flowi *fl, size_t data_len)
1123 struct l2tp_tunnel *tunnel = session->tunnel;
1124 unsigned int len = skb->len;
1128 if (session->send_seq)
1129 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
1130 session->name, data_len, session->ns - 1);
1132 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
1133 session->name, data_len);
1135 if (session->debug & L2TP_MSG_DATA) {
1136 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1137 unsigned char *datap = skb->data + uhlen;
1139 pr_debug("%s: xmit\n", session->name);
1140 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1141 datap, min_t(size_t, 32, len - uhlen));
1144 /* Queue the packet to IP for output */
1147 #if IS_ENABLED(CONFIG_IPV6)
1148 if (l2tp_sk_is_v6(tunnel->sock))
1149 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
1152 error = ip_queue_xmit(tunnel->sock, skb, fl);
1156 atomic_long_inc(&tunnel->stats.tx_packets);
1157 atomic_long_add(len, &tunnel->stats.tx_bytes);
1158 atomic_long_inc(&session->stats.tx_packets);
1159 atomic_long_add(len, &session->stats.tx_bytes);
1161 atomic_long_inc(&tunnel->stats.tx_errors);
1162 atomic_long_inc(&session->stats.tx_errors);
1168 /* If caller requires the skb to have a ppp header, the header must be
1169 * inserted in the skb data before calling this function.
1171 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1173 int data_len = skb->len;
1174 struct l2tp_tunnel *tunnel = session->tunnel;
1175 struct sock *sk = tunnel->sock;
1178 struct inet_sock *inet;
1180 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1182 int ret = NET_XMIT_SUCCESS;
1184 /* Check that there's enough headroom in the skb to insert IP,
1185 * UDP and L2TP headers. If not enough, expand it to
1186 * make room. Adjust truesize.
1188 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1190 if (skb_cow_head(skb, headroom)) {
1192 return NET_XMIT_DROP;
1195 /* Setup L2TP header */
1196 session->build_header(session, __skb_push(skb, hdr_len));
1198 /* Reset skb netfilter state */
1199 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1200 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1205 if (sock_owned_by_user(sk)) {
1207 ret = NET_XMIT_DROP;
1211 /* The user-space may change the connection status for the user-space
1212 * provided socket at run time: we must check it under the socket lock
1214 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1216 ret = NET_XMIT_DROP;
1221 fl = &inet->cork.fl;
1222 switch (tunnel->encap) {
1223 case L2TP_ENCAPTYPE_UDP:
1224 /* Setup UDP header */
1225 __skb_push(skb, sizeof(*uh));
1226 skb_reset_transport_header(skb);
1228 uh->source = inet->inet_sport;
1229 uh->dest = inet->inet_dport;
1230 udp_len = uhlen + hdr_len + data_len;
1231 uh->len = htons(udp_len);
1233 /* Calculate UDP checksum if configured to do so */
1234 #if IS_ENABLED(CONFIG_IPV6)
1235 if (l2tp_sk_is_v6(sk))
1236 udp6_set_csum(udp_get_no_check6_tx(sk),
1237 skb, &inet6_sk(sk)->saddr,
1238 &sk->sk_v6_daddr, udp_len);
1241 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1242 inet->inet_daddr, udp_len);
1245 case L2TP_ENCAPTYPE_IP:
1249 l2tp_xmit_core(session, skb, fl, data_len);
1255 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1257 /*****************************************************************************
1258 * Tinnel and session create/destroy.
1259 *****************************************************************************/
1261 /* Tunnel socket destruct hook.
1262 * The tunnel context is deleted only when all session sockets have been
1265 static void l2tp_tunnel_destruct(struct sock *sk)
1267 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1268 struct l2tp_net *pn;
1273 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1276 /* Disable udp encapsulation */
1277 switch (tunnel->encap) {
1278 case L2TP_ENCAPTYPE_UDP:
1279 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1280 (udp_sk(sk))->encap_type = 0;
1281 (udp_sk(sk))->encap_rcv = NULL;
1282 (udp_sk(sk))->encap_destroy = NULL;
1284 case L2TP_ENCAPTYPE_IP:
1288 /* Remove hooks into tunnel socket */
1289 sk->sk_destruct = tunnel->old_sk_destruct;
1290 sk->sk_user_data = NULL;
1292 /* Remove the tunnel struct from the tunnel list */
1293 pn = l2tp_pernet(tunnel->l2tp_net);
1294 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1295 list_del_rcu(&tunnel->list);
1296 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1297 atomic_dec(&l2tp_tunnel_count);
1299 l2tp_tunnel_closeall(tunnel);
1301 tunnel->sock = NULL;
1302 l2tp_tunnel_dec_refcount(tunnel);
1304 /* Call the original destructor */
1305 if (sk->sk_destruct)
1306 (*sk->sk_destruct)(sk);
1311 /* When the tunnel is closed, all the attached sessions need to go too.
1313 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1316 struct hlist_node *walk;
1317 struct hlist_node *tmp;
1318 struct l2tp_session *session;
1320 BUG_ON(tunnel == NULL);
1322 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1325 write_lock_bh(&tunnel->hlist_lock);
1326 tunnel->acpt_newsess = false;
1327 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1329 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1330 session = hlist_entry(walk, struct l2tp_session, hlist);
1332 l2tp_info(session, L2TP_MSG_CONTROL,
1333 "%s: closing session\n", session->name);
1335 hlist_del_init(&session->hlist);
1337 if (test_and_set_bit(0, &session->dead))
1340 if (session->ref != NULL)
1341 (*session->ref)(session);
1343 write_unlock_bh(&tunnel->hlist_lock);
1345 __l2tp_session_unhash(session);
1346 l2tp_session_queue_purge(session);
1348 if (session->session_close != NULL)
1349 (*session->session_close)(session);
1351 if (session->deref != NULL)
1352 (*session->deref)(session);
1354 l2tp_session_dec_refcount(session);
1356 write_lock_bh(&tunnel->hlist_lock);
1358 /* Now restart from the beginning of this hash
1359 * chain. We always remove a session from the
1360 * list so we are guaranteed to make forward
1366 write_unlock_bh(&tunnel->hlist_lock);
1368 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1370 /* Tunnel socket destroy hook for UDP encapsulation */
1371 static void l2tp_udp_encap_destroy(struct sock *sk)
1373 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1375 l2tp_tunnel_closeall(tunnel);
1380 /* Workqueue tunnel deletion function */
1381 static void l2tp_tunnel_del_work(struct work_struct *work)
1383 struct l2tp_tunnel *tunnel = NULL;
1384 struct socket *sock = NULL;
1385 struct sock *sk = NULL;
1387 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1389 l2tp_tunnel_closeall(tunnel);
1391 sk = l2tp_tunnel_sock_lookup(tunnel);
1395 sock = sk->sk_socket;
1397 /* If the tunnel socket was created by userspace, then go through the
1398 * inet layer to shut the socket down, and let userspace close it.
1399 * Otherwise, if we created the socket directly within the kernel, use
1400 * the sk API to release it here.
1401 * In either case the tunnel resources are freed in the socket
1402 * destructor when the tunnel socket goes away.
1404 if (tunnel->fd >= 0) {
1406 inet_shutdown(sock, 2);
1409 kernel_sock_shutdown(sock, SHUT_RDWR);
1414 l2tp_tunnel_sock_put(sk);
1416 l2tp_tunnel_dec_refcount(tunnel);
1419 /* Create a socket for the tunnel, if one isn't set up by
1420 * userspace. This is used for static tunnels where there is no
1421 * managing L2TP daemon.
1423 * Since we don't want these sockets to keep a namespace alive by
1424 * themselves, we drop the socket's namespace refcount after creation.
1425 * These sockets are freed when the namespace exits using the pernet
1428 static int l2tp_tunnel_sock_create(struct net *net,
1431 struct l2tp_tunnel_cfg *cfg,
1432 struct socket **sockp)
1435 struct socket *sock = NULL;
1436 struct udp_port_cfg udp_conf;
1438 switch (cfg->encap) {
1439 case L2TP_ENCAPTYPE_UDP:
1440 memset(&udp_conf, 0, sizeof(udp_conf));
1442 #if IS_ENABLED(CONFIG_IPV6)
1443 if (cfg->local_ip6 && cfg->peer_ip6) {
1444 udp_conf.family = AF_INET6;
1445 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1446 sizeof(udp_conf.local_ip6));
1447 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1448 sizeof(udp_conf.peer_ip6));
1449 udp_conf.use_udp6_tx_checksums =
1450 ! cfg->udp6_zero_tx_checksums;
1451 udp_conf.use_udp6_rx_checksums =
1452 ! cfg->udp6_zero_rx_checksums;
1456 udp_conf.family = AF_INET;
1457 udp_conf.local_ip = cfg->local_ip;
1458 udp_conf.peer_ip = cfg->peer_ip;
1459 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
1462 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1463 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1465 err = udp_sock_create(net, &udp_conf, &sock);
1471 case L2TP_ENCAPTYPE_IP:
1472 #if IS_ENABLED(CONFIG_IPV6)
1473 if (cfg->local_ip6 && cfg->peer_ip6) {
1474 struct sockaddr_l2tpip6 ip6_addr = {0};
1476 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
1477 IPPROTO_L2TP, &sock);
1481 ip6_addr.l2tp_family = AF_INET6;
1482 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1483 sizeof(ip6_addr.l2tp_addr));
1484 ip6_addr.l2tp_conn_id = tunnel_id;
1485 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1490 ip6_addr.l2tp_family = AF_INET6;
1491 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1492 sizeof(ip6_addr.l2tp_addr));
1493 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1494 err = kernel_connect(sock,
1495 (struct sockaddr *) &ip6_addr,
1496 sizeof(ip6_addr), 0);
1502 struct sockaddr_l2tpip ip_addr = {0};
1504 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
1505 IPPROTO_L2TP, &sock);
1509 ip_addr.l2tp_family = AF_INET;
1510 ip_addr.l2tp_addr = cfg->local_ip;
1511 ip_addr.l2tp_conn_id = tunnel_id;
1512 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1517 ip_addr.l2tp_family = AF_INET;
1518 ip_addr.l2tp_addr = cfg->peer_ip;
1519 ip_addr.l2tp_conn_id = peer_tunnel_id;
1520 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1521 sizeof(ip_addr), 0);
1533 if ((err < 0) && sock) {
1534 kernel_sock_shutdown(sock, SHUT_RDWR);
1542 static struct lock_class_key l2tp_socket_class;
1544 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)
1546 struct l2tp_tunnel *tunnel = NULL;
1548 struct socket *sock = NULL;
1549 struct sock *sk = NULL;
1550 struct l2tp_net *pn;
1551 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1553 /* Get the tunnel socket from the fd, which was opened by
1554 * the userspace L2TP daemon. If not specified, create a
1558 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1563 sock = sockfd_lookup(fd, &err);
1565 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1566 tunnel_id, fd, err);
1571 /* Reject namespace mismatches */
1572 if (!net_eq(sock_net(sock->sk), net)) {
1573 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1584 /* Quick sanity checks */
1585 err = -EPROTONOSUPPORT;
1586 if (sk->sk_type != SOCK_DGRAM) {
1587 pr_debug("tunl %hu: fd %d wrong socket type\n",
1591 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
1594 case L2TP_ENCAPTYPE_UDP:
1595 if (sk->sk_protocol != IPPROTO_UDP) {
1596 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1597 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1601 case L2TP_ENCAPTYPE_IP:
1602 if (sk->sk_protocol != IPPROTO_L2TP) {
1603 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1604 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1610 /* Check if this socket has already been prepped */
1611 tunnel = l2tp_tunnel(sk);
1612 if (tunnel != NULL) {
1613 /* This socket has already been prepped */
1618 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1619 if (tunnel == NULL) {
1624 tunnel->version = version;
1625 tunnel->tunnel_id = tunnel_id;
1626 tunnel->peer_tunnel_id = peer_tunnel_id;
1627 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1629 tunnel->magic = L2TP_TUNNEL_MAGIC;
1630 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1631 rwlock_init(&tunnel->hlist_lock);
1632 tunnel->acpt_newsess = true;
1634 /* The net we belong to */
1635 tunnel->l2tp_net = net;
1636 pn = l2tp_pernet(net);
1639 tunnel->debug = cfg->debug;
1641 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1642 tunnel->encap = encap;
1643 if (encap == L2TP_ENCAPTYPE_UDP) {
1644 struct udp_tunnel_sock_cfg udp_cfg = { };
1646 udp_cfg.sk_user_data = tunnel;
1647 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1648 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1649 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1651 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1653 sk->sk_user_data = tunnel;
1656 /* Hook on the tunnel socket destructor so that we can cleanup
1657 * if the tunnel socket goes away.
1659 tunnel->old_sk_destruct = sk->sk_destruct;
1660 sk->sk_destruct = &l2tp_tunnel_destruct;
1663 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1665 sk->sk_allocation = GFP_ATOMIC;
1667 /* Init delete workqueue struct */
1668 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1670 /* Add tunnel to our list */
1671 INIT_LIST_HEAD(&tunnel->list);
1672 atomic_inc(&l2tp_tunnel_count);
1674 /* Bump the reference count. The tunnel context is deleted
1675 * only when this drops to zero. Must be done before list insertion
1677 refcount_set(&tunnel->ref_count, 1);
1678 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1679 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1680 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1687 /* If tunnel's socket was created by the kernel, it doesn't
1690 if (sock && sock->file)
1695 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1697 /* This function is used by the netlink TUNNEL_DELETE command.
1699 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1701 if (!test_and_set_bit(0, &tunnel->dead)) {
1702 l2tp_tunnel_inc_refcount(tunnel);
1703 queue_work(l2tp_wq, &tunnel->del_work);
1706 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1708 /* Really kill the session.
1710 void l2tp_session_free(struct l2tp_session *session)
1712 struct l2tp_tunnel *tunnel = session->tunnel;
1714 BUG_ON(refcount_read(&session->ref_count) != 0);
1717 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1718 if (session->session_id != 0)
1719 atomic_dec(&l2tp_session_count);
1720 sock_put(tunnel->sock);
1721 session->tunnel = NULL;
1722 l2tp_tunnel_dec_refcount(tunnel);
1727 EXPORT_SYMBOL_GPL(l2tp_session_free);
1729 /* Remove an l2tp session from l2tp_core's hash lists.
1730 * Provides a tidyup interface for pseudowire code which can't just route all
1731 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1734 void __l2tp_session_unhash(struct l2tp_session *session)
1736 struct l2tp_tunnel *tunnel = session->tunnel;
1738 /* Remove the session from core hashes */
1740 /* Remove from the per-tunnel hash */
1741 write_lock_bh(&tunnel->hlist_lock);
1742 hlist_del_init(&session->hlist);
1743 write_unlock_bh(&tunnel->hlist_lock);
1745 /* For L2TPv3 we have a per-net hash: remove from there, too */
1746 if (tunnel->version != L2TP_HDR_VER_2) {
1747 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1748 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1749 hlist_del_init_rcu(&session->global_hlist);
1750 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1755 EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1757 /* This function is used by the netlink SESSION_DELETE command and by
1760 int l2tp_session_delete(struct l2tp_session *session)
1762 if (test_and_set_bit(0, &session->dead))
1766 (*session->ref)(session);
1767 __l2tp_session_unhash(session);
1768 l2tp_session_queue_purge(session);
1769 if (session->session_close != NULL)
1770 (*session->session_close)(session);
1772 (*session->deref)(session);
1773 l2tp_session_dec_refcount(session);
1776 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1778 /* We come here whenever a session's send_seq, cookie_len or
1779 * l2specific_type parameters are set.
1781 void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1783 if (version == L2TP_HDR_VER_2) {
1784 session->hdr_len = 6;
1785 if (session->send_seq)
1786 session->hdr_len += 4;
1788 session->hdr_len = 4 + session->cookie_len;
1789 session->hdr_len += l2tp_get_l2specific_len(session);
1790 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1791 session->hdr_len += 4;
1795 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1797 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)
1799 struct l2tp_session *session;
1801 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1802 if (session != NULL) {
1803 session->magic = L2TP_SESSION_MAGIC;
1804 session->tunnel = tunnel;
1806 session->session_id = session_id;
1807 session->peer_session_id = peer_session_id;
1809 if (tunnel->version == L2TP_HDR_VER_2)
1810 session->nr_max = 0xffff;
1812 session->nr_max = 0xffffff;
1813 session->nr_window_size = session->nr_max / 2;
1814 session->nr_oos_count_max = 4;
1816 /* Use NR of first received packet */
1817 session->reorder_skip = 1;
1819 sprintf(&session->name[0], "sess %u/%u",
1820 tunnel->tunnel_id, session->session_id);
1822 skb_queue_head_init(&session->reorder_q);
1824 INIT_HLIST_NODE(&session->hlist);
1825 INIT_HLIST_NODE(&session->global_hlist);
1827 /* Inherit debug options from tunnel */
1828 session->debug = tunnel->debug;
1831 session->pwtype = cfg->pw_type;
1832 session->debug = cfg->debug;
1833 session->mtu = cfg->mtu;
1834 session->mru = cfg->mru;
1835 session->send_seq = cfg->send_seq;
1836 session->recv_seq = cfg->recv_seq;
1837 session->lns_mode = cfg->lns_mode;
1838 session->reorder_timeout = cfg->reorder_timeout;
1839 session->l2specific_type = cfg->l2specific_type;
1840 session->l2specific_len = cfg->l2specific_len;
1841 session->cookie_len = cfg->cookie_len;
1842 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1843 session->peer_cookie_len = cfg->peer_cookie_len;
1844 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1847 if (tunnel->version == L2TP_HDR_VER_2)
1848 session->build_header = l2tp_build_l2tpv2_header;
1850 session->build_header = l2tp_build_l2tpv3_header;
1852 l2tp_session_set_header_len(session, tunnel->version);
1854 refcount_set(&session->ref_count, 1);
1859 return ERR_PTR(-ENOMEM);
1861 EXPORT_SYMBOL_GPL(l2tp_session_create);
1863 /*****************************************************************************
1865 *****************************************************************************/
1867 static __net_init int l2tp_init_net(struct net *net)
1869 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1872 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1873 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1875 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1876 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1878 spin_lock_init(&pn->l2tp_session_hlist_lock);
1883 static __net_exit void l2tp_exit_net(struct net *net)
1885 struct l2tp_net *pn = l2tp_pernet(net);
1886 struct l2tp_tunnel *tunnel = NULL;
1889 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1890 l2tp_tunnel_delete(tunnel);
1892 rcu_read_unlock_bh();
1895 flush_workqueue(l2tp_wq);
1899 static struct pernet_operations l2tp_net_ops = {
1900 .init = l2tp_init_net,
1901 .exit = l2tp_exit_net,
1903 .size = sizeof(struct l2tp_net),
1906 static int __init l2tp_init(void)
1910 rc = register_pernet_device(&l2tp_net_ops);
1914 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1916 pr_err("alloc_workqueue failed\n");
1917 unregister_pernet_device(&l2tp_net_ops);
1922 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1928 static void __exit l2tp_exit(void)
1930 unregister_pernet_device(&l2tp_net_ops);
1932 destroy_workqueue(l2tp_wq);
1937 module_init(l2tp_init);
1938 module_exit(l2tp_exit);
1940 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1941 MODULE_DESCRIPTION("L2TP core");
1942 MODULE_LICENSE("GPL");
1943 MODULE_VERSION(L2TP_DRV_VERSION);