1 // SPDX-License-Identifier: GPL-2.0-only
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)
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/string.h>
21 #include <linux/list.h>
22 #include <linux/rculist.h>
23 #include <linux/uaccess.h>
25 #include <linux/kernel.h>
26 #include <linux/spinlock.h>
27 #include <linux/kthread.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/errno.h>
31 #include <linux/jiffies.h>
33 #include <linux/netdevice.h>
34 #include <linux/net.h>
35 #include <linux/inetdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/init.h>
40 #include <linux/udp.h>
41 #include <linux/l2tp.h>
42 #include <linux/hash.h>
43 #include <linux/sort.h>
44 #include <linux/file.h>
45 #include <linux/nsproxy.h>
46 #include <net/net_namespace.h>
47 #include <net/netns/generic.h>
51 #include <net/udp_tunnel.h>
52 #include <net/inet_common.h>
54 #include <net/protocol.h>
55 #include <net/inet6_connection_sock.h>
56 #include <net/inet_ecn.h>
57 #include <net/ip6_route.h>
58 #include <net/ip6_checksum.h>
60 #include <asm/byteorder.h>
61 #include <linux/atomic.h>
63 #include "l2tp_core.h"
66 #define CREATE_TRACE_POINTS
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 struct workqueue_struct *l2tp_wq;
104 /* per-net private data for this module */
105 static unsigned int l2tp_net_id;
107 struct list_head l2tp_tunnel_list;
108 /* Lock for write access to l2tp_tunnel_list */
109 spinlock_t l2tp_tunnel_list_lock;
110 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
111 /* Lock for write access to l2tp_session_hlist */
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_net *l2tp_pernet(const struct net *net)
125 return net_generic(net, l2tp_net_id);
128 /* Session hash global list for L2TPv3.
129 * The session_id SHOULD be random according to RFC3931, but several
130 * L2TP implementations use incrementing session_ids. So we do a real
131 * hash on the session_id, rather than a simple bitmask.
133 static inline struct hlist_head *
134 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
136 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
139 /* Session hash list.
140 * The session_id SHOULD be random according to RFC2661, but several
141 * L2TP implementations (Cisco and Microsoft) use incrementing
142 * session_ids. So we do a real hash on the session_id, rather than a
145 static inline struct hlist_head *
146 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
148 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
151 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
153 trace_free_tunnel(tunnel);
154 sock_put(tunnel->sock);
155 /* the tunnel is freed in the socket destructor */
158 static void l2tp_session_free(struct l2tp_session *session)
160 trace_free_session(session);
162 l2tp_tunnel_dec_refcount(session->tunnel);
166 struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk)
168 struct l2tp_tunnel *tunnel = sk->sk_user_data;
171 if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
176 EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel);
178 void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
180 refcount_inc(&tunnel->ref_count);
182 EXPORT_SYMBOL_GPL(l2tp_tunnel_inc_refcount);
184 void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
186 if (refcount_dec_and_test(&tunnel->ref_count))
187 l2tp_tunnel_free(tunnel);
189 EXPORT_SYMBOL_GPL(l2tp_tunnel_dec_refcount);
191 void l2tp_session_inc_refcount(struct l2tp_session *session)
193 refcount_inc(&session->ref_count);
195 EXPORT_SYMBOL_GPL(l2tp_session_inc_refcount);
197 void l2tp_session_dec_refcount(struct l2tp_session *session)
199 if (refcount_dec_and_test(&session->ref_count))
200 l2tp_session_free(session);
202 EXPORT_SYMBOL_GPL(l2tp_session_dec_refcount);
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 refcount_inc_not_zero(&tunnel->ref_count)) {
214 rcu_read_unlock_bh();
219 rcu_read_unlock_bh();
223 EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
225 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
227 const struct l2tp_net *pn = l2tp_pernet(net);
228 struct l2tp_tunnel *tunnel;
232 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
234 refcount_inc_not_zero(&tunnel->ref_count)) {
235 rcu_read_unlock_bh();
239 rcu_read_unlock_bh();
243 EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
245 struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
248 struct hlist_head *session_list;
249 struct l2tp_session *session;
251 session_list = l2tp_session_id_hash(tunnel, session_id);
254 hlist_for_each_entry_rcu(session, session_list, hlist)
255 if (session->session_id == session_id) {
256 l2tp_session_inc_refcount(session);
257 rcu_read_unlock_bh();
261 rcu_read_unlock_bh();
265 EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session);
267 struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
269 struct hlist_head *session_list;
270 struct l2tp_session *session;
272 session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id);
275 hlist_for_each_entry_rcu(session, session_list, global_hlist)
276 if (session->session_id == session_id) {
277 l2tp_session_inc_refcount(session);
278 rcu_read_unlock_bh();
282 rcu_read_unlock_bh();
286 EXPORT_SYMBOL_GPL(l2tp_session_get);
288 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
291 struct l2tp_session *session;
295 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
296 hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) {
298 l2tp_session_inc_refcount(session);
299 rcu_read_unlock_bh();
305 rcu_read_unlock_bh();
309 EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
311 /* Lookup a session by interface name.
312 * This is very inefficient but is only used by management interfaces.
314 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
317 struct l2tp_net *pn = l2tp_pernet(net);
319 struct l2tp_session *session;
322 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
323 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
324 if (!strcmp(session->ifname, ifname)) {
325 l2tp_session_inc_refcount(session);
326 rcu_read_unlock_bh();
333 rcu_read_unlock_bh();
337 EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
339 int l2tp_session_register(struct l2tp_session *session,
340 struct l2tp_tunnel *tunnel)
342 struct l2tp_session *session_walk;
343 struct hlist_head *g_head;
344 struct hlist_head *head;
348 head = l2tp_session_id_hash(tunnel, session->session_id);
350 spin_lock_bh(&tunnel->hlist_lock);
351 if (!tunnel->acpt_newsess) {
356 hlist_for_each_entry(session_walk, head, hlist)
357 if (session_walk->session_id == session->session_id) {
362 if (tunnel->version == L2TP_HDR_VER_3) {
363 pn = l2tp_pernet(tunnel->l2tp_net);
364 g_head = l2tp_session_id_hash_2(pn, session->session_id);
366 spin_lock_bh(&pn->l2tp_session_hlist_lock);
368 /* IP encap expects session IDs to be globally unique, while
371 hlist_for_each_entry(session_walk, g_head, global_hlist)
372 if (session_walk->session_id == session->session_id &&
373 (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP ||
374 tunnel->encap == L2TP_ENCAPTYPE_IP)) {
376 goto err_tlock_pnlock;
379 l2tp_tunnel_inc_refcount(tunnel);
380 hlist_add_head_rcu(&session->global_hlist, g_head);
382 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
384 l2tp_tunnel_inc_refcount(tunnel);
387 hlist_add_head_rcu(&session->hlist, head);
388 spin_unlock_bh(&tunnel->hlist_lock);
390 trace_register_session(session);
395 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
397 spin_unlock_bh(&tunnel->hlist_lock);
401 EXPORT_SYMBOL_GPL(l2tp_session_register);
403 /*****************************************************************************
404 * Receive data handling
405 *****************************************************************************/
407 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
410 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
412 struct sk_buff *skbp;
414 u32 ns = L2TP_SKB_CB(skb)->ns;
416 spin_lock_bh(&session->reorder_q.lock);
417 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
418 if (L2TP_SKB_CB(skbp)->ns > ns) {
419 __skb_queue_before(&session->reorder_q, skbp, skb);
420 atomic_long_inc(&session->stats.rx_oos_packets);
425 __skb_queue_tail(&session->reorder_q, skb);
428 spin_unlock_bh(&session->reorder_q.lock);
431 /* Dequeue a single skb.
433 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
435 struct l2tp_tunnel *tunnel = session->tunnel;
436 int length = L2TP_SKB_CB(skb)->length;
438 /* We're about to requeue the skb, so return resources
439 * to its current owner (a socket receive buffer).
443 atomic_long_inc(&tunnel->stats.rx_packets);
444 atomic_long_add(length, &tunnel->stats.rx_bytes);
445 atomic_long_inc(&session->stats.rx_packets);
446 atomic_long_add(length, &session->stats.rx_bytes);
448 if (L2TP_SKB_CB(skb)->has_seq) {
451 session->nr &= session->nr_max;
452 trace_session_seqnum_update(session);
455 /* call private receive handler */
456 if (session->recv_skb)
457 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
462 /* Dequeue skbs from the session's reorder_q, subject to packet order.
463 * Skbs that have been in the queue for too long are simply discarded.
465 static void l2tp_recv_dequeue(struct l2tp_session *session)
470 /* If the pkt at the head of the queue has the nr that we
471 * expect to send up next, dequeue it and any other
472 * in-sequence packets behind it.
475 spin_lock_bh(&session->reorder_q.lock);
476 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
477 struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
479 /* If the packet has been pending on the queue for too long, discard it */
480 if (time_after(jiffies, cb->expires)) {
481 atomic_long_inc(&session->stats.rx_seq_discards);
482 atomic_long_inc(&session->stats.rx_errors);
483 trace_session_pkt_expired(session, cb->ns);
484 session->reorder_skip = 1;
485 __skb_unlink(skb, &session->reorder_q);
491 if (session->reorder_skip) {
492 session->reorder_skip = 0;
493 session->nr = cb->ns;
494 trace_session_seqnum_reset(session);
496 if (cb->ns != session->nr)
499 __skb_unlink(skb, &session->reorder_q);
501 /* Process the skb. We release the queue lock while we
502 * do so to let other contexts process the queue.
504 spin_unlock_bh(&session->reorder_q.lock);
505 l2tp_recv_dequeue_skb(session, skb);
510 spin_unlock_bh(&session->reorder_q.lock);
513 static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
517 if (nr >= session->nr)
518 nws = nr - session->nr;
520 nws = (session->nr_max + 1) - (session->nr - nr);
522 return nws < session->nr_window_size;
525 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
526 * acceptable, else non-zero.
528 static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
530 struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
532 if (!l2tp_seq_check_rx_window(session, cb->ns)) {
533 /* Packet sequence number is outside allowed window.
536 trace_session_pkt_outside_rx_window(session, cb->ns);
540 if (session->reorder_timeout != 0) {
541 /* Packet reordering enabled. Add skb to session's
542 * reorder queue, in order of ns.
544 l2tp_recv_queue_skb(session, skb);
548 /* Packet reordering disabled. Discard out-of-sequence packets, while
549 * tracking the number if in-sequence packets after the first OOS packet
550 * is seen. After nr_oos_count_max in-sequence packets, reset the
551 * sequence number to re-enable packet reception.
553 if (cb->ns == session->nr) {
554 skb_queue_tail(&session->reorder_q, skb);
557 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
559 if (nr_oos == nr_next)
560 session->nr_oos_count++;
562 session->nr_oos_count = 0;
564 session->nr_oos = nr_oos;
565 if (session->nr_oos_count > session->nr_oos_count_max) {
566 session->reorder_skip = 1;
568 if (!session->reorder_skip) {
569 atomic_long_inc(&session->stats.rx_seq_discards);
570 trace_session_pkt_oos(session, cb->ns);
573 skb_queue_tail(&session->reorder_q, skb);
583 /* Do receive processing of L2TP data frames. We handle both L2TPv2
584 * and L2TPv3 data frames here.
586 * L2TPv2 Data Message Header
589 * 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
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
591 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
592 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
593 * | Tunnel ID | Session ID |
594 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
595 * | Ns (opt) | Nr (opt) |
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * | Offset Size (opt) | Offset pad... (opt)
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * Data frames are marked by T=0. All other fields are the same as
601 * those in L2TP control frames.
603 * L2TPv3 Data Message Header
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * | L2TP Session Header |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * | L2-Specific Sublayer |
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 * | Tunnel Payload ...
611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
613 * L2TPv3 Session Header Over IP
616 * 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
617 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
619 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
620 * | Cookie (optional, maximum 64 bits)...
621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
625 * L2TPv3 L2-Specific Sublayer Format
628 * 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
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630 * |x|S|x|x|x|x|x|x| Sequence Number |
631 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
633 * Cookie value and sublayer format are negotiated with the peer when
634 * the session is set up. Unlike L2TPv2, we do not need to parse the
635 * packet header to determine if optional fields are present.
637 * Caller must already have parsed the frame and determined that it is
638 * a data (not control) frame before coming here. Fields up to the
639 * session-id have already been parsed and ptr points to the data
640 * after the session-id.
642 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
643 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
646 struct l2tp_tunnel *tunnel = session->tunnel;
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 pr_debug_ratelimited("%s: cookie mismatch (%u/%u). Discarding.\n",
653 tunnel->name, tunnel->tunnel_id,
654 session->session_id);
655 atomic_long_inc(&session->stats.rx_cookie_discards);
658 ptr += session->peer_cookie_len;
661 /* Handle the optional sequence numbers. Sequence numbers are
662 * in different places for L2TPv2 and L2TPv3.
664 * If we are the LAC, enable/disable sequence numbers under
665 * the control of the LNS. If no sequence numbers present but
666 * we were expecting them, discard frame.
668 L2TP_SKB_CB(skb)->has_seq = 0;
669 if (tunnel->version == L2TP_HDR_VER_2) {
670 if (hdrflags & L2TP_HDRFLAG_S) {
671 /* Store L2TP info in the skb */
672 L2TP_SKB_CB(skb)->ns = ntohs(*(__be16 *)ptr);
673 L2TP_SKB_CB(skb)->has_seq = 1;
675 /* Skip past nr in the header */
679 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
680 u32 l2h = ntohl(*(__be32 *)ptr);
682 if (l2h & 0x40000000) {
683 /* Store L2TP info in the skb */
684 L2TP_SKB_CB(skb)->ns = l2h & 0x00ffffff;
685 L2TP_SKB_CB(skb)->has_seq = 1;
690 if (L2TP_SKB_CB(skb)->has_seq) {
691 /* Received a packet with sequence numbers. If we're the LAC,
692 * check if we sre sending sequence numbers and if not,
695 if (!session->lns_mode && !session->send_seq) {
696 trace_session_seqnum_lns_enable(session);
697 session->send_seq = 1;
698 l2tp_session_set_header_len(session, tunnel->version);
701 /* No sequence numbers.
702 * If user has configured mandatory sequence numbers, discard.
704 if (session->recv_seq) {
705 pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
707 atomic_long_inc(&session->stats.rx_seq_discards);
711 /* If we're the LAC and we're sending sequence numbers, the
712 * LNS has requested that we no longer send sequence numbers.
713 * If we're the LNS and we're sending sequence numbers, the
714 * LAC is broken. Discard the frame.
716 if (!session->lns_mode && session->send_seq) {
717 trace_session_seqnum_lns_disable(session);
718 session->send_seq = 0;
719 l2tp_session_set_header_len(session, tunnel->version);
720 } else if (session->send_seq) {
721 pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
723 atomic_long_inc(&session->stats.rx_seq_discards);
728 /* Session data offset is defined only for L2TPv2 and is
729 * indicated by an optional 16-bit value in the header.
731 if (tunnel->version == L2TP_HDR_VER_2) {
732 /* If offset bit set, skip it. */
733 if (hdrflags & L2TP_HDRFLAG_O) {
734 offset = ntohs(*(__be16 *)ptr);
740 if (!pskb_may_pull(skb, offset))
743 __skb_pull(skb, offset);
745 /* Prepare skb for adding to the session's reorder_q. Hold
746 * packets for max reorder_timeout or 1 second if not
749 L2TP_SKB_CB(skb)->length = length;
750 L2TP_SKB_CB(skb)->expires = jiffies +
751 (session->reorder_timeout ? session->reorder_timeout : HZ);
753 /* Add packet to the session's receive queue. Reordering is done here, if
754 * enabled. Saved L2TP protocol info is stored in skb->sb[].
756 if (L2TP_SKB_CB(skb)->has_seq) {
757 if (l2tp_recv_data_seq(session, skb))
760 /* No sequence numbers. Add the skb to the tail of the
761 * reorder queue. This ensures that it will be
762 * delivered after all previous sequenced skbs.
764 skb_queue_tail(&session->reorder_q, skb);
767 /* Try to dequeue as many skbs from reorder_q as we can. */
768 l2tp_recv_dequeue(session);
773 atomic_long_inc(&session->stats.rx_errors);
776 EXPORT_SYMBOL_GPL(l2tp_recv_common);
778 /* Drop skbs from the session's reorder_q
780 static void l2tp_session_queue_purge(struct l2tp_session *session)
782 struct sk_buff *skb = NULL;
784 while ((skb = skb_dequeue(&session->reorder_q))) {
785 atomic_long_inc(&session->stats.rx_errors);
790 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
791 * here. The skb is not on a list when we get here.
792 * Returns 0 if the packet was a data packet and was successfully passed on.
793 * Returns 1 if the packet was not a good data packet and could not be
794 * forwarded. All such packets are passed up to userspace to deal with.
796 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
798 struct l2tp_session *session = NULL;
799 unsigned char *ptr, *optr;
801 u32 tunnel_id, session_id;
805 /* UDP has verified checksum */
807 /* UDP always verifies the packet length. */
808 __skb_pull(skb, sizeof(struct udphdr));
811 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) {
812 pr_debug_ratelimited("%s: recv short packet (len=%d)\n",
813 tunnel->name, skb->len);
817 /* Point to L2TP header */
821 /* Get L2TP header flags */
822 hdrflags = ntohs(*(__be16 *)ptr);
824 /* Check protocol version */
825 version = hdrflags & L2TP_HDR_VER_MASK;
826 if (version != tunnel->version) {
827 pr_debug_ratelimited("%s: recv protocol version mismatch: got %d expected %d\n",
828 tunnel->name, version, tunnel->version);
832 /* Get length of L2TP packet */
835 /* If type is control packet, it is handled by userspace. */
836 if (hdrflags & L2TP_HDRFLAG_T)
842 if (tunnel->version == L2TP_HDR_VER_2) {
843 /* If length is present, skip it */
844 if (hdrflags & L2TP_HDRFLAG_L)
847 /* Extract tunnel and session ID */
848 tunnel_id = ntohs(*(__be16 *)ptr);
850 session_id = ntohs(*(__be16 *)ptr);
853 ptr += 2; /* skip reserved bits */
854 tunnel_id = tunnel->tunnel_id;
855 session_id = ntohl(*(__be32 *)ptr);
859 /* Find the session context */
860 session = l2tp_tunnel_get_session(tunnel, session_id);
861 if (!session || !session->recv_skb) {
863 l2tp_session_dec_refcount(session);
865 /* Not found? Pass to userspace to deal with */
866 pr_debug_ratelimited("%s: no session found (%u/%u). Passing up.\n",
867 tunnel->name, tunnel_id, session_id);
871 if (tunnel->version == L2TP_HDR_VER_3 &&
872 l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
873 l2tp_session_dec_refcount(session);
877 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
878 l2tp_session_dec_refcount(session);
883 atomic_long_inc(&tunnel->stats.rx_invalid);
886 /* Put UDP header back */
887 __skb_push(skb, sizeof(struct udphdr));
892 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
896 * >0: skb should be passed up to userspace as UDP.
898 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
900 struct l2tp_tunnel *tunnel;
902 /* Note that this is called from the encap_rcv hook inside an
903 * RCU-protected region, but without the socket being locked.
904 * Hence we use rcu_dereference_sk_user_data to access the
905 * tunnel data structure rather the usual l2tp_sk_to_tunnel
908 tunnel = rcu_dereference_sk_user_data(sk);
911 if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
914 if (l2tp_udp_recv_core(tunnel, skb))
922 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
924 /************************************************************************
926 ***********************************************************************/
928 /* Build an L2TP header for the session into the buffer provided.
930 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
932 struct l2tp_tunnel *tunnel = session->tunnel;
935 u16 flags = L2TP_HDR_VER_2;
936 u32 tunnel_id = tunnel->peer_tunnel_id;
937 u32 session_id = session->peer_session_id;
939 if (session->send_seq)
940 flags |= L2TP_HDRFLAG_S;
942 /* Setup L2TP header. */
943 *bufp++ = htons(flags);
944 *bufp++ = htons(tunnel_id);
945 *bufp++ = htons(session_id);
946 if (session->send_seq) {
947 *bufp++ = htons(session->ns);
950 session->ns &= 0xffff;
951 trace_session_seqnum_update(session);
957 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
959 struct l2tp_tunnel *tunnel = session->tunnel;
963 /* Setup L2TP header. The header differs slightly for UDP and
964 * IP encapsulations. For UDP, there is 4 bytes of flags.
966 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
967 u16 flags = L2TP_HDR_VER_3;
968 *((__be16 *)bufp) = htons(flags);
970 *((__be16 *)bufp) = 0;
974 *((__be32 *)bufp) = htonl(session->peer_session_id);
976 if (session->cookie_len) {
977 memcpy(bufp, &session->cookie[0], session->cookie_len);
978 bufp += session->cookie_len;
980 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
983 if (session->send_seq) {
984 l2h = 0x40000000 | session->ns;
986 session->ns &= 0xffffff;
987 trace_session_seqnum_update(session);
990 *((__be32 *)bufp) = htonl(l2h);
997 /* Queue the packet to IP for output: tunnel socket lock must be held */
998 static int l2tp_xmit_queue(struct l2tp_tunnel *tunnel, struct sk_buff *skb, struct flowi *fl)
1004 #if IS_ENABLED(CONFIG_IPV6)
1005 if (l2tp_sk_is_v6(tunnel->sock))
1006 err = inet6_csk_xmit(tunnel->sock, skb, NULL);
1009 err = ip_queue_xmit(tunnel->sock, skb, fl);
1011 return err >= 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
1014 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, unsigned int *len)
1016 struct l2tp_tunnel *tunnel = session->tunnel;
1017 unsigned int data_len = skb->len;
1018 struct sock *sk = tunnel->sock;
1019 int headroom, uhlen, udp_len;
1020 int ret = NET_XMIT_SUCCESS;
1021 struct inet_sock *inet;
1024 /* Check that there's enough headroom in the skb to insert IP,
1025 * UDP and L2TP headers. If not enough, expand it to
1026 * make room. Adjust truesize.
1028 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(*uh) : 0;
1029 headroom = NET_SKB_PAD + sizeof(struct iphdr) + uhlen + session->hdr_len;
1030 if (skb_cow_head(skb, headroom)) {
1032 return NET_XMIT_DROP;
1035 /* Setup L2TP header */
1036 if (tunnel->version == L2TP_HDR_VER_2)
1037 l2tp_build_l2tpv2_header(session, __skb_push(skb, session->hdr_len));
1039 l2tp_build_l2tpv3_header(session, __skb_push(skb, session->hdr_len));
1041 /* Reset skb netfilter state */
1042 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1043 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
1047 if (sock_owned_by_user(sk)) {
1049 ret = NET_XMIT_DROP;
1053 /* The user-space may change the connection status for the user-space
1054 * provided socket at run time: we must check it under the socket lock
1056 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1058 ret = NET_XMIT_DROP;
1062 /* Report transmitted length before we add encap header, which keeps
1063 * statistics consistent for both UDP and IP encap tx/rx paths.
1068 switch (tunnel->encap) {
1069 case L2TP_ENCAPTYPE_UDP:
1070 /* Setup UDP header */
1071 __skb_push(skb, sizeof(*uh));
1072 skb_reset_transport_header(skb);
1074 uh->source = inet->inet_sport;
1075 uh->dest = inet->inet_dport;
1076 udp_len = uhlen + session->hdr_len + data_len;
1077 uh->len = htons(udp_len);
1079 /* Calculate UDP checksum if configured to do so */
1080 #if IS_ENABLED(CONFIG_IPV6)
1081 if (l2tp_sk_is_v6(sk))
1082 udp6_set_csum(udp_get_no_check6_tx(sk),
1083 skb, &inet6_sk(sk)->saddr,
1084 &sk->sk_v6_daddr, udp_len);
1087 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1088 inet->inet_daddr, udp_len);
1091 case L2TP_ENCAPTYPE_IP:
1095 ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl);
1103 /* If caller requires the skb to have a ppp header, the header must be
1104 * inserted in the skb data before calling this function.
1106 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb)
1108 unsigned int len = 0;
1111 ret = l2tp_xmit_core(session, skb, &len);
1112 if (ret == NET_XMIT_SUCCESS) {
1113 atomic_long_inc(&session->tunnel->stats.tx_packets);
1114 atomic_long_add(len, &session->tunnel->stats.tx_bytes);
1115 atomic_long_inc(&session->stats.tx_packets);
1116 atomic_long_add(len, &session->stats.tx_bytes);
1118 atomic_long_inc(&session->tunnel->stats.tx_errors);
1119 atomic_long_inc(&session->stats.tx_errors);
1123 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1125 /*****************************************************************************
1126 * Tinnel and session create/destroy.
1127 *****************************************************************************/
1129 /* Tunnel socket destruct hook.
1130 * The tunnel context is deleted only when all session sockets have been
1133 static void l2tp_tunnel_destruct(struct sock *sk)
1135 struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
1140 /* Disable udp encapsulation */
1141 switch (tunnel->encap) {
1142 case L2TP_ENCAPTYPE_UDP:
1143 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1144 (udp_sk(sk))->encap_type = 0;
1145 (udp_sk(sk))->encap_rcv = NULL;
1146 (udp_sk(sk))->encap_destroy = NULL;
1148 case L2TP_ENCAPTYPE_IP:
1152 /* Remove hooks into tunnel socket */
1153 sk->sk_destruct = tunnel->old_sk_destruct;
1154 sk->sk_user_data = NULL;
1156 /* Call the original destructor */
1157 if (sk->sk_destruct)
1158 (*sk->sk_destruct)(sk);
1160 kfree_rcu(tunnel, rcu);
1165 /* Remove an l2tp session from l2tp_core's hash lists. */
1166 static void l2tp_session_unhash(struct l2tp_session *session)
1168 struct l2tp_tunnel *tunnel = session->tunnel;
1170 /* Remove the session from core hashes */
1172 /* Remove from the per-tunnel hash */
1173 spin_lock_bh(&tunnel->hlist_lock);
1174 hlist_del_init_rcu(&session->hlist);
1175 spin_unlock_bh(&tunnel->hlist_lock);
1177 /* For L2TPv3 we have a per-net hash: remove from there, too */
1178 if (tunnel->version != L2TP_HDR_VER_2) {
1179 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1181 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1182 hlist_del_init_rcu(&session->global_hlist);
1183 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1190 /* When the tunnel is closed, all the attached sessions need to go too.
1192 static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1194 struct l2tp_session *session;
1197 spin_lock_bh(&tunnel->hlist_lock);
1198 tunnel->acpt_newsess = false;
1199 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1201 hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) {
1202 hlist_del_init_rcu(&session->hlist);
1204 spin_unlock_bh(&tunnel->hlist_lock);
1205 l2tp_session_delete(session);
1206 spin_lock_bh(&tunnel->hlist_lock);
1208 /* Now restart from the beginning of this hash
1209 * chain. We always remove a session from the
1210 * list so we are guaranteed to make forward
1216 spin_unlock_bh(&tunnel->hlist_lock);
1219 /* Tunnel socket destroy hook for UDP encapsulation */
1220 static void l2tp_udp_encap_destroy(struct sock *sk)
1222 struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
1225 l2tp_tunnel_delete(tunnel);
1228 /* Workqueue tunnel deletion function */
1229 static void l2tp_tunnel_del_work(struct work_struct *work)
1231 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1233 struct sock *sk = tunnel->sock;
1234 struct socket *sock = sk->sk_socket;
1235 struct l2tp_net *pn;
1237 l2tp_tunnel_closeall(tunnel);
1239 /* If the tunnel socket was created within the kernel, use
1240 * the sk API to release it here.
1242 if (tunnel->fd < 0) {
1244 kernel_sock_shutdown(sock, SHUT_RDWR);
1249 /* Remove the tunnel struct from the tunnel list */
1250 pn = l2tp_pernet(tunnel->l2tp_net);
1251 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1252 list_del_rcu(&tunnel->list);
1253 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1255 /* drop initial ref */
1256 l2tp_tunnel_dec_refcount(tunnel);
1258 /* drop workqueue ref */
1259 l2tp_tunnel_dec_refcount(tunnel);
1262 /* Create a socket for the tunnel, if one isn't set up by
1263 * userspace. This is used for static tunnels where there is no
1264 * managing L2TP daemon.
1266 * Since we don't want these sockets to keep a namespace alive by
1267 * themselves, we drop the socket's namespace refcount after creation.
1268 * These sockets are freed when the namespace exits using the pernet
1271 static int l2tp_tunnel_sock_create(struct net *net,
1274 struct l2tp_tunnel_cfg *cfg,
1275 struct socket **sockp)
1278 struct socket *sock = NULL;
1279 struct udp_port_cfg udp_conf;
1281 switch (cfg->encap) {
1282 case L2TP_ENCAPTYPE_UDP:
1283 memset(&udp_conf, 0, sizeof(udp_conf));
1285 #if IS_ENABLED(CONFIG_IPV6)
1286 if (cfg->local_ip6 && cfg->peer_ip6) {
1287 udp_conf.family = AF_INET6;
1288 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1289 sizeof(udp_conf.local_ip6));
1290 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1291 sizeof(udp_conf.peer_ip6));
1292 udp_conf.use_udp6_tx_checksums =
1293 !cfg->udp6_zero_tx_checksums;
1294 udp_conf.use_udp6_rx_checksums =
1295 !cfg->udp6_zero_rx_checksums;
1299 udp_conf.family = AF_INET;
1300 udp_conf.local_ip = cfg->local_ip;
1301 udp_conf.peer_ip = cfg->peer_ip;
1302 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
1305 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1306 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1308 err = udp_sock_create(net, &udp_conf, &sock);
1314 case L2TP_ENCAPTYPE_IP:
1315 #if IS_ENABLED(CONFIG_IPV6)
1316 if (cfg->local_ip6 && cfg->peer_ip6) {
1317 struct sockaddr_l2tpip6 ip6_addr = {0};
1319 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
1320 IPPROTO_L2TP, &sock);
1324 ip6_addr.l2tp_family = AF_INET6;
1325 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1326 sizeof(ip6_addr.l2tp_addr));
1327 ip6_addr.l2tp_conn_id = tunnel_id;
1328 err = kernel_bind(sock, (struct sockaddr *)&ip6_addr,
1333 ip6_addr.l2tp_family = AF_INET6;
1334 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1335 sizeof(ip6_addr.l2tp_addr));
1336 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1337 err = kernel_connect(sock,
1338 (struct sockaddr *)&ip6_addr,
1339 sizeof(ip6_addr), 0);
1345 struct sockaddr_l2tpip ip_addr = {0};
1347 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
1348 IPPROTO_L2TP, &sock);
1352 ip_addr.l2tp_family = AF_INET;
1353 ip_addr.l2tp_addr = cfg->local_ip;
1354 ip_addr.l2tp_conn_id = tunnel_id;
1355 err = kernel_bind(sock, (struct sockaddr *)&ip_addr,
1360 ip_addr.l2tp_family = AF_INET;
1361 ip_addr.l2tp_addr = cfg->peer_ip;
1362 ip_addr.l2tp_conn_id = peer_tunnel_id;
1363 err = kernel_connect(sock, (struct sockaddr *)&ip_addr,
1364 sizeof(ip_addr), 0);
1376 if (err < 0 && sock) {
1377 kernel_sock_shutdown(sock, SHUT_RDWR);
1385 static struct lock_class_key l2tp_socket_class;
1387 int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id,
1388 struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
1390 struct l2tp_tunnel *tunnel = NULL;
1392 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1397 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
1403 tunnel->version = version;
1404 tunnel->tunnel_id = tunnel_id;
1405 tunnel->peer_tunnel_id = peer_tunnel_id;
1407 tunnel->magic = L2TP_TUNNEL_MAGIC;
1408 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1409 spin_lock_init(&tunnel->hlist_lock);
1410 tunnel->acpt_newsess = true;
1412 tunnel->encap = encap;
1414 refcount_set(&tunnel->ref_count, 1);
1417 /* Init delete workqueue struct */
1418 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1420 INIT_LIST_HEAD(&tunnel->list);
1429 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1431 static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1432 enum l2tp_encap_type encap)
1434 if (!net_eq(sock_net(sk), net))
1437 if (sk->sk_type != SOCK_DGRAM)
1438 return -EPROTONOSUPPORT;
1440 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
1441 return -EPROTONOSUPPORT;
1443 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1444 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1445 return -EPROTONOSUPPORT;
1447 if (sk->sk_user_data)
1453 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1454 struct l2tp_tunnel_cfg *cfg)
1456 struct l2tp_tunnel *tunnel_walk;
1457 struct l2tp_net *pn;
1458 struct socket *sock;
1462 if (tunnel->fd < 0) {
1463 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1464 tunnel->peer_tunnel_id, cfg,
1469 sock = sockfd_lookup(tunnel->fd, &ret);
1473 ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
1478 tunnel->l2tp_net = net;
1479 pn = l2tp_pernet(net);
1485 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1486 list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
1487 if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
1488 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1494 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1495 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1497 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1498 struct udp_tunnel_sock_cfg udp_cfg = {
1499 .sk_user_data = tunnel,
1500 .encap_type = UDP_ENCAP_L2TPINUDP,
1501 .encap_rcv = l2tp_udp_encap_recv,
1502 .encap_destroy = l2tp_udp_encap_destroy,
1505 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1507 sk->sk_user_data = tunnel;
1510 tunnel->old_sk_destruct = sk->sk_destruct;
1511 sk->sk_destruct = &l2tp_tunnel_destruct;
1512 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
1514 sk->sk_allocation = GFP_ATOMIC;
1516 trace_register_tunnel(tunnel);
1518 if (tunnel->fd >= 0)
1531 EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1533 /* This function is used by the netlink TUNNEL_DELETE command.
1535 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1537 if (!test_and_set_bit(0, &tunnel->dead)) {
1538 trace_delete_tunnel(tunnel);
1539 l2tp_tunnel_inc_refcount(tunnel);
1540 queue_work(l2tp_wq, &tunnel->del_work);
1543 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1545 void l2tp_session_delete(struct l2tp_session *session)
1547 if (test_and_set_bit(0, &session->dead))
1550 trace_delete_session(session);
1551 l2tp_session_unhash(session);
1552 l2tp_session_queue_purge(session);
1553 if (session->session_close)
1554 (*session->session_close)(session);
1556 l2tp_session_dec_refcount(session);
1558 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1560 /* We come here whenever a session's send_seq, cookie_len or
1561 * l2specific_type parameters are set.
1563 void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1565 if (version == L2TP_HDR_VER_2) {
1566 session->hdr_len = 6;
1567 if (session->send_seq)
1568 session->hdr_len += 4;
1570 session->hdr_len = 4 + session->cookie_len;
1571 session->hdr_len += l2tp_get_l2specific_len(session);
1572 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1573 session->hdr_len += 4;
1576 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1578 struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id,
1579 u32 peer_session_id, struct l2tp_session_cfg *cfg)
1581 struct l2tp_session *session;
1583 session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL);
1585 session->magic = L2TP_SESSION_MAGIC;
1586 session->tunnel = tunnel;
1588 session->session_id = session_id;
1589 session->peer_session_id = peer_session_id;
1591 if (tunnel->version == L2TP_HDR_VER_2)
1592 session->nr_max = 0xffff;
1594 session->nr_max = 0xffffff;
1595 session->nr_window_size = session->nr_max / 2;
1596 session->nr_oos_count_max = 4;
1598 /* Use NR of first received packet */
1599 session->reorder_skip = 1;
1601 sprintf(&session->name[0], "sess %u/%u",
1602 tunnel->tunnel_id, session->session_id);
1604 skb_queue_head_init(&session->reorder_q);
1606 INIT_HLIST_NODE(&session->hlist);
1607 INIT_HLIST_NODE(&session->global_hlist);
1610 session->pwtype = cfg->pw_type;
1611 session->send_seq = cfg->send_seq;
1612 session->recv_seq = cfg->recv_seq;
1613 session->lns_mode = cfg->lns_mode;
1614 session->reorder_timeout = cfg->reorder_timeout;
1615 session->l2specific_type = cfg->l2specific_type;
1616 session->cookie_len = cfg->cookie_len;
1617 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1618 session->peer_cookie_len = cfg->peer_cookie_len;
1619 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1622 l2tp_session_set_header_len(session, tunnel->version);
1624 refcount_set(&session->ref_count, 1);
1629 return ERR_PTR(-ENOMEM);
1631 EXPORT_SYMBOL_GPL(l2tp_session_create);
1633 /*****************************************************************************
1635 *****************************************************************************/
1637 static __net_init int l2tp_init_net(struct net *net)
1639 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1642 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1643 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1645 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1646 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1648 spin_lock_init(&pn->l2tp_session_hlist_lock);
1653 static __net_exit void l2tp_exit_net(struct net *net)
1655 struct l2tp_net *pn = l2tp_pernet(net);
1656 struct l2tp_tunnel *tunnel = NULL;
1660 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1661 l2tp_tunnel_delete(tunnel);
1663 rcu_read_unlock_bh();
1666 flush_workqueue(l2tp_wq);
1669 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1670 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
1673 static struct pernet_operations l2tp_net_ops = {
1674 .init = l2tp_init_net,
1675 .exit = l2tp_exit_net,
1677 .size = sizeof(struct l2tp_net),
1680 static int __init l2tp_init(void)
1684 rc = register_pernet_device(&l2tp_net_ops);
1688 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1690 pr_err("alloc_workqueue failed\n");
1691 unregister_pernet_device(&l2tp_net_ops);
1696 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1702 static void __exit l2tp_exit(void)
1704 unregister_pernet_device(&l2tp_net_ops);
1706 destroy_workqueue(l2tp_wq);
1711 module_init(l2tp_init);
1712 module_exit(l2tp_exit);
1714 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1715 MODULE_DESCRIPTION("L2TP core");
1716 MODULE_LICENSE("GPL");
1717 MODULE_VERSION(L2TP_DRV_VERSION);