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