xref: /openbmc/linux/net/l2tp/l2tp_core.c (revision 61b9a047)
1fd558d18SJames Chapman /*
2fd558d18SJames Chapman  * L2TP core.
3fd558d18SJames Chapman  *
4fd558d18SJames Chapman  * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5fd558d18SJames Chapman  *
6fd558d18SJames Chapman  * This file contains some code of the original L2TPv2 pppol2tp
7fd558d18SJames Chapman  * driver, which has the following copyright:
8fd558d18SJames Chapman  *
9fd558d18SJames Chapman  * Authors:	Martijn van Oosterhout <kleptog@svana.org>
10fd558d18SJames Chapman  *		James Chapman (jchapman@katalix.com)
11fd558d18SJames Chapman  * Contributors:
12fd558d18SJames Chapman  *		Michal Ostrowski <mostrows@speakeasy.net>
13fd558d18SJames Chapman  *		Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14fd558d18SJames Chapman  *		David S. Miller (davem@redhat.com)
15fd558d18SJames Chapman  *
16fd558d18SJames Chapman  * This program is free software; you can redistribute it and/or modify
17fd558d18SJames Chapman  * it under the terms of the GNU General Public License version 2 as
18fd558d18SJames Chapman  * published by the Free Software Foundation.
19fd558d18SJames Chapman  */
20fd558d18SJames Chapman 
21a4ca44faSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22a4ca44faSJoe Perches 
23fd558d18SJames Chapman #include <linux/module.h>
24fd558d18SJames Chapman #include <linux/string.h>
25fd558d18SJames Chapman #include <linux/list.h>
26e02d494dSJames Chapman #include <linux/rculist.h>
27fd558d18SJames Chapman #include <linux/uaccess.h>
28fd558d18SJames Chapman 
29fd558d18SJames Chapman #include <linux/kernel.h>
30fd558d18SJames Chapman #include <linux/spinlock.h>
31fd558d18SJames Chapman #include <linux/kthread.h>
32fd558d18SJames Chapman #include <linux/sched.h>
33fd558d18SJames Chapman #include <linux/slab.h>
34fd558d18SJames Chapman #include <linux/errno.h>
35fd558d18SJames Chapman #include <linux/jiffies.h>
36fd558d18SJames Chapman 
37fd558d18SJames Chapman #include <linux/netdevice.h>
38fd558d18SJames Chapman #include <linux/net.h>
39fd558d18SJames Chapman #include <linux/inetdevice.h>
40fd558d18SJames Chapman #include <linux/skbuff.h>
41fd558d18SJames Chapman #include <linux/init.h>
420d76751fSJames Chapman #include <linux/in.h>
43fd558d18SJames Chapman #include <linux/ip.h>
44fd558d18SJames Chapman #include <linux/udp.h>
450d76751fSJames Chapman #include <linux/l2tp.h>
46fd558d18SJames Chapman #include <linux/hash.h>
47fd558d18SJames Chapman #include <linux/sort.h>
48fd558d18SJames Chapman #include <linux/file.h>
49fd558d18SJames Chapman #include <linux/nsproxy.h>
50fd558d18SJames Chapman #include <net/net_namespace.h>
51fd558d18SJames Chapman #include <net/netns/generic.h>
52fd558d18SJames Chapman #include <net/dst.h>
53fd558d18SJames Chapman #include <net/ip.h>
54fd558d18SJames Chapman #include <net/udp.h>
5585644b4dSTom Herbert #include <net/udp_tunnel.h>
56309795f4SJames Chapman #include <net/inet_common.h>
57fd558d18SJames Chapman #include <net/xfrm.h>
580d76751fSJames Chapman #include <net/protocol.h>
59d2cf3361SBenjamin LaHaise #include <net/inet6_connection_sock.h>
60d2cf3361SBenjamin LaHaise #include <net/inet_ecn.h>
61d2cf3361SBenjamin LaHaise #include <net/ip6_route.h>
62d499bd2eSDavid S. Miller #include <net/ip6_checksum.h>
63fd558d18SJames Chapman 
64fd558d18SJames Chapman #include <asm/byteorder.h>
6560063497SArun Sharma #include <linux/atomic.h>
66fd558d18SJames Chapman 
67fd558d18SJames Chapman #include "l2tp_core.h"
68fd558d18SJames Chapman 
69fd558d18SJames Chapman #define L2TP_DRV_VERSION	"V2.0"
70fd558d18SJames Chapman 
71fd558d18SJames Chapman /* L2TP header constants */
72fd558d18SJames Chapman #define L2TP_HDRFLAG_T	   0x8000
73fd558d18SJames Chapman #define L2TP_HDRFLAG_L	   0x4000
74fd558d18SJames Chapman #define L2TP_HDRFLAG_S	   0x0800
75fd558d18SJames Chapman #define L2TP_HDRFLAG_O	   0x0200
76fd558d18SJames Chapman #define L2TP_HDRFLAG_P	   0x0100
77fd558d18SJames Chapman 
78fd558d18SJames Chapman #define L2TP_HDR_VER_MASK  0x000F
79fd558d18SJames Chapman #define L2TP_HDR_VER_2	   0x0002
80f7faffa3SJames Chapman #define L2TP_HDR_VER_3	   0x0003
81fd558d18SJames Chapman 
82fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */
83fd558d18SJames Chapman #define L2TP_SLFLAG_S	   0x40000000
84fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK   0x00ffffff
85fd558d18SJames Chapman 
86fd558d18SJames Chapman #define L2TP_HDR_SIZE_SEQ		10
87fd558d18SJames Chapman #define L2TP_HDR_SIZE_NOSEQ		6
88fd558d18SJames Chapman 
89fd558d18SJames Chapman /* Default trace flags */
90fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS	0
91fd558d18SJames Chapman 
92fd558d18SJames Chapman /* Private data stored for received packets in the skb.
93fd558d18SJames Chapman  */
94fd558d18SJames Chapman struct l2tp_skb_cb {
95f7faffa3SJames Chapman 	u32			ns;
96fd558d18SJames Chapman 	u16			has_seq;
97fd558d18SJames Chapman 	u16			length;
98fd558d18SJames Chapman 	unsigned long		expires;
99fd558d18SJames Chapman };
100fd558d18SJames Chapman 
101fd558d18SJames Chapman #define L2TP_SKB_CB(skb)	((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
102fd558d18SJames Chapman 
103fd558d18SJames Chapman static atomic_t l2tp_tunnel_count;
104fd558d18SJames Chapman static atomic_t l2tp_session_count;
105f8ccac0eSTom Parkin static struct workqueue_struct *l2tp_wq;
106fd558d18SJames Chapman 
107fd558d18SJames Chapman /* per-net private data for this module */
108fd558d18SJames Chapman static unsigned int l2tp_net_id;
109fd558d18SJames Chapman struct l2tp_net {
110fd558d18SJames Chapman 	struct list_head l2tp_tunnel_list;
111e02d494dSJames Chapman 	spinlock_t l2tp_tunnel_list_lock;
112f7faffa3SJames Chapman 	struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
113e02d494dSJames Chapman 	spinlock_t l2tp_session_hlist_lock;
114fd558d18SJames Chapman };
115fd558d18SJames Chapman 
116fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
117fc130840Sstephen hemminger 
1188d8a51e2SDavid S. Miller static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
1198d8a51e2SDavid S. Miller {
1208d8a51e2SDavid S. Miller 	return sk->sk_user_data;
1218d8a51e2SDavid S. Miller }
1228d8a51e2SDavid S. Miller 
123fd558d18SJames Chapman static inline struct l2tp_net *l2tp_pernet(struct net *net)
124fd558d18SJames Chapman {
125fd558d18SJames Chapman 	BUG_ON(!net);
126fd558d18SJames Chapman 
127fd558d18SJames Chapman 	return net_generic(net, l2tp_net_id);
128fd558d18SJames Chapman }
129fd558d18SJames Chapman 
130fc130840Sstephen hemminger /* Tunnel reference counts. Incremented per session that is added to
131fc130840Sstephen hemminger  * the tunnel.
132fc130840Sstephen hemminger  */
133fc130840Sstephen hemminger static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
134fc130840Sstephen hemminger {
135fc130840Sstephen hemminger 	atomic_inc(&tunnel->ref_count);
136fc130840Sstephen hemminger }
137fc130840Sstephen hemminger 
138fc130840Sstephen hemminger static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
139fc130840Sstephen hemminger {
140fc130840Sstephen hemminger 	if (atomic_dec_and_test(&tunnel->ref_count))
141fc130840Sstephen hemminger 		l2tp_tunnel_free(tunnel);
142fc130840Sstephen hemminger }
143fc130840Sstephen hemminger #ifdef L2TP_REFCNT_DEBUG
144a4ca44faSJoe Perches #define l2tp_tunnel_inc_refcount(_t)					\
145a4ca44faSJoe Perches do {									\
146a4ca44faSJoe Perches 	pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n",	\
147a4ca44faSJoe Perches 		 __func__, __LINE__, (_t)->name,			\
148a4ca44faSJoe Perches 		 atomic_read(&_t->ref_count));				\
149fc130840Sstephen hemminger 	l2tp_tunnel_inc_refcount_1(_t);					\
150fc130840Sstephen hemminger } while (0)
15129abe2fdSAndy Zhou #define l2tp_tunnel_dec_refcount(_t)					\
152a4ca44faSJoe Perches do {									\
153a4ca44faSJoe Perches 	pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n",	\
154a4ca44faSJoe Perches 		 __func__, __LINE__, (_t)->name,			\
155a4ca44faSJoe Perches 		 atomic_read(&_t->ref_count));				\
156fc130840Sstephen hemminger 	l2tp_tunnel_dec_refcount_1(_t);					\
157fc130840Sstephen hemminger } while (0)
158fc130840Sstephen hemminger #else
159fc130840Sstephen hemminger #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
160fc130840Sstephen hemminger #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
161fc130840Sstephen hemminger #endif
162fc130840Sstephen hemminger 
163f7faffa3SJames Chapman /* Session hash global list for L2TPv3.
164f7faffa3SJames Chapman  * The session_id SHOULD be random according to RFC3931, but several
165f7faffa3SJames Chapman  * L2TP implementations use incrementing session_ids.  So we do a real
166f7faffa3SJames Chapman  * hash on the session_id, rather than a simple bitmask.
167f7faffa3SJames Chapman  */
168f7faffa3SJames Chapman static inline struct hlist_head *
169f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
170f7faffa3SJames Chapman {
171f7faffa3SJames Chapman 	return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
172f7faffa3SJames Chapman 
173f7faffa3SJames Chapman }
174f7faffa3SJames Chapman 
17580d84ef3STom Parkin /* Lookup the tunnel socket, possibly involving the fs code if the socket is
17680d84ef3STom Parkin  * owned by userspace.  A struct sock returned from this function must be
17780d84ef3STom Parkin  * released using l2tp_tunnel_sock_put once you're done with it.
17880d84ef3STom Parkin  */
179b5d2b285Sstephen hemminger static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
18080d84ef3STom Parkin {
18180d84ef3STom Parkin 	int err = 0;
18280d84ef3STom Parkin 	struct socket *sock = NULL;
18380d84ef3STom Parkin 	struct sock *sk = NULL;
18480d84ef3STom Parkin 
18580d84ef3STom Parkin 	if (!tunnel)
18680d84ef3STom Parkin 		goto out;
18780d84ef3STom Parkin 
18880d84ef3STom Parkin 	if (tunnel->fd >= 0) {
18980d84ef3STom Parkin 		/* Socket is owned by userspace, who might be in the process
19080d84ef3STom Parkin 		 * of closing it.  Look the socket up using the fd to ensure
19180d84ef3STom Parkin 		 * consistency.
19280d84ef3STom Parkin 		 */
19380d84ef3STom Parkin 		sock = sockfd_lookup(tunnel->fd, &err);
19480d84ef3STom Parkin 		if (sock)
19580d84ef3STom Parkin 			sk = sock->sk;
19680d84ef3STom Parkin 	} else {
19780d84ef3STom Parkin 		/* Socket is owned by kernelspace */
19880d84ef3STom Parkin 		sk = tunnel->sock;
1998abbbe8fSTom Parkin 		sock_hold(sk);
20080d84ef3STom Parkin 	}
20180d84ef3STom Parkin 
20280d84ef3STom Parkin out:
20380d84ef3STom Parkin 	return sk;
20480d84ef3STom Parkin }
20580d84ef3STom Parkin 
20680d84ef3STom Parkin /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
207b5d2b285Sstephen hemminger static void l2tp_tunnel_sock_put(struct sock *sk)
20880d84ef3STom Parkin {
20980d84ef3STom Parkin 	struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
21080d84ef3STom Parkin 	if (tunnel) {
21180d84ef3STom Parkin 		if (tunnel->fd >= 0) {
21280d84ef3STom Parkin 			/* Socket is owned by userspace */
21380d84ef3STom Parkin 			sockfd_put(sk->sk_socket);
21480d84ef3STom Parkin 		}
21580d84ef3STom Parkin 		sock_put(sk);
21680d84ef3STom Parkin 	}
2178abbbe8fSTom Parkin 	sock_put(sk);
21880d84ef3STom Parkin }
21980d84ef3STom Parkin 
220f7faffa3SJames Chapman /* Lookup a session by id in the global session list
221f7faffa3SJames Chapman  */
222f7faffa3SJames Chapman static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
223f7faffa3SJames Chapman {
224f7faffa3SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
225f7faffa3SJames Chapman 	struct hlist_head *session_list =
226f7faffa3SJames Chapman 		l2tp_session_id_hash_2(pn, session_id);
227f7faffa3SJames Chapman 	struct l2tp_session *session;
228f7faffa3SJames Chapman 
229e02d494dSJames Chapman 	rcu_read_lock_bh();
230b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(session, session_list, global_hlist) {
231f7faffa3SJames Chapman 		if (session->session_id == session_id) {
232e02d494dSJames Chapman 			rcu_read_unlock_bh();
233f7faffa3SJames Chapman 			return session;
234f7faffa3SJames Chapman 		}
235f7faffa3SJames Chapman 	}
236e02d494dSJames Chapman 	rcu_read_unlock_bh();
237f7faffa3SJames Chapman 
238f7faffa3SJames Chapman 	return NULL;
239f7faffa3SJames Chapman }
240f7faffa3SJames Chapman 
241fd558d18SJames Chapman /* Session hash list.
242fd558d18SJames Chapman  * The session_id SHOULD be random according to RFC2661, but several
243fd558d18SJames Chapman  * L2TP implementations (Cisco and Microsoft) use incrementing
244fd558d18SJames Chapman  * session_ids.  So we do a real hash on the session_id, rather than a
245fd558d18SJames Chapman  * simple bitmask.
246fd558d18SJames Chapman  */
247fd558d18SJames Chapman static inline struct hlist_head *
248fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
249fd558d18SJames Chapman {
250fd558d18SJames Chapman 	return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
251fd558d18SJames Chapman }
252fd558d18SJames Chapman 
253fd558d18SJames Chapman /* Lookup a session by id
254fd558d18SJames Chapman  */
255f7faffa3SJames Chapman struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
256fd558d18SJames Chapman {
257f7faffa3SJames Chapman 	struct hlist_head *session_list;
258fd558d18SJames Chapman 	struct l2tp_session *session;
259fd558d18SJames Chapman 
260f7faffa3SJames Chapman 	/* In L2TPv3, session_ids are unique over all tunnels and we
261f7faffa3SJames Chapman 	 * sometimes need to look them up before we know the
262f7faffa3SJames Chapman 	 * tunnel.
263f7faffa3SJames Chapman 	 */
264f7faffa3SJames Chapman 	if (tunnel == NULL)
265f7faffa3SJames Chapman 		return l2tp_session_find_2(net, session_id);
266f7faffa3SJames Chapman 
267f7faffa3SJames Chapman 	session_list = l2tp_session_id_hash(tunnel, session_id);
268fd558d18SJames Chapman 	read_lock_bh(&tunnel->hlist_lock);
269b67bfe0dSSasha Levin 	hlist_for_each_entry(session, session_list, hlist) {
270fd558d18SJames Chapman 		if (session->session_id == session_id) {
271fd558d18SJames Chapman 			read_unlock_bh(&tunnel->hlist_lock);
272fd558d18SJames Chapman 			return session;
273fd558d18SJames Chapman 		}
274fd558d18SJames Chapman 	}
275fd558d18SJames Chapman 	read_unlock_bh(&tunnel->hlist_lock);
276fd558d18SJames Chapman 
277fd558d18SJames Chapman 	return NULL;
278fd558d18SJames Chapman }
279fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find);
280fd558d18SJames Chapman 
28161b9a047SGuillaume Nault /* Like l2tp_session_find() but takes a reference on the returned session.
28261b9a047SGuillaume Nault  * Optionally calls session->ref() too if do_ref is true.
28361b9a047SGuillaume Nault  */
28461b9a047SGuillaume Nault struct l2tp_session *l2tp_session_get(struct net *net,
28561b9a047SGuillaume Nault 				      struct l2tp_tunnel *tunnel,
28661b9a047SGuillaume Nault 				      u32 session_id, bool do_ref)
28761b9a047SGuillaume Nault {
28861b9a047SGuillaume Nault 	struct hlist_head *session_list;
28961b9a047SGuillaume Nault 	struct l2tp_session *session;
29061b9a047SGuillaume Nault 
29161b9a047SGuillaume Nault 	if (!tunnel) {
29261b9a047SGuillaume Nault 		struct l2tp_net *pn = l2tp_pernet(net);
29361b9a047SGuillaume Nault 
29461b9a047SGuillaume Nault 		session_list = l2tp_session_id_hash_2(pn, session_id);
29561b9a047SGuillaume Nault 
29661b9a047SGuillaume Nault 		rcu_read_lock_bh();
29761b9a047SGuillaume Nault 		hlist_for_each_entry_rcu(session, session_list, global_hlist) {
29861b9a047SGuillaume Nault 			if (session->session_id == session_id) {
29961b9a047SGuillaume Nault 				l2tp_session_inc_refcount(session);
30061b9a047SGuillaume Nault 				if (do_ref && session->ref)
30161b9a047SGuillaume Nault 					session->ref(session);
30261b9a047SGuillaume Nault 				rcu_read_unlock_bh();
30361b9a047SGuillaume Nault 
30461b9a047SGuillaume Nault 				return session;
30561b9a047SGuillaume Nault 			}
30661b9a047SGuillaume Nault 		}
30761b9a047SGuillaume Nault 		rcu_read_unlock_bh();
30861b9a047SGuillaume Nault 
30961b9a047SGuillaume Nault 		return NULL;
31061b9a047SGuillaume Nault 	}
31161b9a047SGuillaume Nault 
31261b9a047SGuillaume Nault 	session_list = l2tp_session_id_hash(tunnel, session_id);
31361b9a047SGuillaume Nault 	read_lock_bh(&tunnel->hlist_lock);
31461b9a047SGuillaume Nault 	hlist_for_each_entry(session, session_list, hlist) {
31561b9a047SGuillaume Nault 		if (session->session_id == session_id) {
31661b9a047SGuillaume Nault 			l2tp_session_inc_refcount(session);
31761b9a047SGuillaume Nault 			if (do_ref && session->ref)
31861b9a047SGuillaume Nault 				session->ref(session);
31961b9a047SGuillaume Nault 			read_unlock_bh(&tunnel->hlist_lock);
32061b9a047SGuillaume Nault 
32161b9a047SGuillaume Nault 			return session;
32261b9a047SGuillaume Nault 		}
32361b9a047SGuillaume Nault 	}
32461b9a047SGuillaume Nault 	read_unlock_bh(&tunnel->hlist_lock);
32561b9a047SGuillaume Nault 
32661b9a047SGuillaume Nault 	return NULL;
32761b9a047SGuillaume Nault }
32861b9a047SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get);
32961b9a047SGuillaume Nault 
330fd558d18SJames Chapman struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
331fd558d18SJames Chapman {
332fd558d18SJames Chapman 	int hash;
333fd558d18SJames Chapman 	struct l2tp_session *session;
334fd558d18SJames Chapman 	int count = 0;
335fd558d18SJames Chapman 
336fd558d18SJames Chapman 	read_lock_bh(&tunnel->hlist_lock);
337fd558d18SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
338b67bfe0dSSasha Levin 		hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
339fd558d18SJames Chapman 			if (++count > nth) {
340fd558d18SJames Chapman 				read_unlock_bh(&tunnel->hlist_lock);
341fd558d18SJames Chapman 				return session;
342fd558d18SJames Chapman 			}
343fd558d18SJames Chapman 		}
344fd558d18SJames Chapman 	}
345fd558d18SJames Chapman 
346fd558d18SJames Chapman 	read_unlock_bh(&tunnel->hlist_lock);
347fd558d18SJames Chapman 
348fd558d18SJames Chapman 	return NULL;
349fd558d18SJames Chapman }
350fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
351fd558d18SJames Chapman 
352309795f4SJames Chapman /* Lookup a session by interface name.
353309795f4SJames Chapman  * This is very inefficient but is only used by management interfaces.
354309795f4SJames Chapman  */
355309795f4SJames Chapman struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
356309795f4SJames Chapman {
357309795f4SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
358309795f4SJames Chapman 	int hash;
359309795f4SJames Chapman 	struct l2tp_session *session;
360309795f4SJames Chapman 
361e02d494dSJames Chapman 	rcu_read_lock_bh();
362309795f4SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
363b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
364309795f4SJames Chapman 			if (!strcmp(session->ifname, ifname)) {
365e02d494dSJames Chapman 				rcu_read_unlock_bh();
366309795f4SJames Chapman 				return session;
367309795f4SJames Chapman 			}
368309795f4SJames Chapman 		}
369309795f4SJames Chapman 	}
370309795f4SJames Chapman 
371e02d494dSJames Chapman 	rcu_read_unlock_bh();
372309795f4SJames Chapman 
373309795f4SJames Chapman 	return NULL;
374309795f4SJames Chapman }
375309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
376309795f4SJames Chapman 
377fd558d18SJames Chapman /* Lookup a tunnel by id
378fd558d18SJames Chapman  */
379fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
380fd558d18SJames Chapman {
381fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
382fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
383fd558d18SJames Chapman 
384e02d494dSJames Chapman 	rcu_read_lock_bh();
385e02d494dSJames Chapman 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
386fd558d18SJames Chapman 		if (tunnel->tunnel_id == tunnel_id) {
387e02d494dSJames Chapman 			rcu_read_unlock_bh();
388fd558d18SJames Chapman 			return tunnel;
389fd558d18SJames Chapman 		}
390fd558d18SJames Chapman 	}
391e02d494dSJames Chapman 	rcu_read_unlock_bh();
392fd558d18SJames Chapman 
393fd558d18SJames Chapman 	return NULL;
394fd558d18SJames Chapman }
395fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
396fd558d18SJames Chapman 
397fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
398fd558d18SJames Chapman {
399fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
400fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
401fd558d18SJames Chapman 	int count = 0;
402fd558d18SJames Chapman 
403e02d494dSJames Chapman 	rcu_read_lock_bh();
404e02d494dSJames Chapman 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
405fd558d18SJames Chapman 		if (++count > nth) {
406e02d494dSJames Chapman 			rcu_read_unlock_bh();
407fd558d18SJames Chapman 			return tunnel;
408fd558d18SJames Chapman 		}
409fd558d18SJames Chapman 	}
410fd558d18SJames Chapman 
411e02d494dSJames Chapman 	rcu_read_unlock_bh();
412fd558d18SJames Chapman 
413fd558d18SJames Chapman 	return NULL;
414fd558d18SJames Chapman }
415fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
416fd558d18SJames Chapman 
417fd558d18SJames Chapman /*****************************************************************************
418fd558d18SJames Chapman  * Receive data handling
419fd558d18SJames Chapman  *****************************************************************************/
420fd558d18SJames Chapman 
421fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence
422fd558d18SJames Chapman  * number.
423fd558d18SJames Chapman  */
424fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
425fd558d18SJames Chapman {
426fd558d18SJames Chapman 	struct sk_buff *skbp;
427fd558d18SJames Chapman 	struct sk_buff *tmp;
428f7faffa3SJames Chapman 	u32 ns = L2TP_SKB_CB(skb)->ns;
429fd558d18SJames Chapman 
430fd558d18SJames Chapman 	spin_lock_bh(&session->reorder_q.lock);
431fd558d18SJames Chapman 	skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
432fd558d18SJames Chapman 		if (L2TP_SKB_CB(skbp)->ns > ns) {
433fd558d18SJames Chapman 			__skb_queue_before(&session->reorder_q, skbp, skb);
434a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
435fd558d18SJames Chapman 				 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
436fd558d18SJames Chapman 				 session->name, ns, L2TP_SKB_CB(skbp)->ns,
437fd558d18SJames Chapman 				 skb_queue_len(&session->reorder_q));
4387b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_oos_packets);
439fd558d18SJames Chapman 			goto out;
440fd558d18SJames Chapman 		}
441fd558d18SJames Chapman 	}
442fd558d18SJames Chapman 
443fd558d18SJames Chapman 	__skb_queue_tail(&session->reorder_q, skb);
444fd558d18SJames Chapman 
445fd558d18SJames Chapman out:
446fd558d18SJames Chapman 	spin_unlock_bh(&session->reorder_q.lock);
447fd558d18SJames Chapman }
448fd558d18SJames Chapman 
449fd558d18SJames Chapman /* Dequeue a single skb.
450fd558d18SJames Chapman  */
451fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
452fd558d18SJames Chapman {
453fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
454fd558d18SJames Chapman 	int length = L2TP_SKB_CB(skb)->length;
455fd558d18SJames Chapman 
456fd558d18SJames Chapman 	/* We're about to requeue the skb, so return resources
457fd558d18SJames Chapman 	 * to its current owner (a socket receive buffer).
458fd558d18SJames Chapman 	 */
459fd558d18SJames Chapman 	skb_orphan(skb);
460fd558d18SJames Chapman 
4617b7c0719STom Parkin 	atomic_long_inc(&tunnel->stats.rx_packets);
4627b7c0719STom Parkin 	atomic_long_add(length, &tunnel->stats.rx_bytes);
4637b7c0719STom Parkin 	atomic_long_inc(&session->stats.rx_packets);
4647b7c0719STom Parkin 	atomic_long_add(length, &session->stats.rx_bytes);
465fd558d18SJames Chapman 
466fd558d18SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
467fd558d18SJames Chapman 		/* Bump our Nr */
468fd558d18SJames Chapman 		session->nr++;
4698a1631d5SJames Chapman 		session->nr &= session->nr_max;
470f7faffa3SJames Chapman 
471a4ca44faSJoe Perches 		l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
472a4ca44faSJoe Perches 			 session->name, session->nr);
473fd558d18SJames Chapman 	}
474fd558d18SJames Chapman 
475fd558d18SJames Chapman 	/* call private receive handler */
476fd558d18SJames Chapman 	if (session->recv_skb != NULL)
477fd558d18SJames Chapman 		(*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
478fd558d18SJames Chapman 	else
479fd558d18SJames Chapman 		kfree_skb(skb);
480fd558d18SJames Chapman 
481fd558d18SJames Chapman 	if (session->deref)
482fd558d18SJames Chapman 		(*session->deref)(session);
483fd558d18SJames Chapman }
484fd558d18SJames Chapman 
485fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order.
486fd558d18SJames Chapman  * Skbs that have been in the queue for too long are simply discarded.
487fd558d18SJames Chapman  */
488fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session)
489fd558d18SJames Chapman {
490fd558d18SJames Chapman 	struct sk_buff *skb;
491fd558d18SJames Chapman 	struct sk_buff *tmp;
492fd558d18SJames Chapman 
493fd558d18SJames Chapman 	/* If the pkt at the head of the queue has the nr that we
494fd558d18SJames Chapman 	 * expect to send up next, dequeue it and any other
495fd558d18SJames Chapman 	 * in-sequence packets behind it.
496fd558d18SJames Chapman 	 */
497e2e210c0SEric Dumazet start:
498fd558d18SJames Chapman 	spin_lock_bh(&session->reorder_q.lock);
499fd558d18SJames Chapman 	skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
500fd558d18SJames Chapman 		if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
5017b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
5027b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_errors);
503a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
504a4ca44faSJoe Perches 				 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
505fd558d18SJames Chapman 				 session->name, L2TP_SKB_CB(skb)->ns,
506fd558d18SJames Chapman 				 L2TP_SKB_CB(skb)->length, session->nr,
507fd558d18SJames Chapman 				 skb_queue_len(&session->reorder_q));
50838d40b3fSJames Chapman 			session->reorder_skip = 1;
509fd558d18SJames Chapman 			__skb_unlink(skb, &session->reorder_q);
510fd558d18SJames Chapman 			kfree_skb(skb);
511fd558d18SJames Chapman 			if (session->deref)
512fd558d18SJames Chapman 				(*session->deref)(session);
513fd558d18SJames Chapman 			continue;
514fd558d18SJames Chapman 		}
515fd558d18SJames Chapman 
516fd558d18SJames Chapman 		if (L2TP_SKB_CB(skb)->has_seq) {
51738d40b3fSJames Chapman 			if (session->reorder_skip) {
518a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
51938d40b3fSJames Chapman 					 "%s: advancing nr to next pkt: %u -> %u",
52038d40b3fSJames Chapman 					 session->name, session->nr,
52138d40b3fSJames Chapman 					 L2TP_SKB_CB(skb)->ns);
52238d40b3fSJames Chapman 				session->reorder_skip = 0;
52338d40b3fSJames Chapman 				session->nr = L2TP_SKB_CB(skb)->ns;
52438d40b3fSJames Chapman 			}
525fd558d18SJames Chapman 			if (L2TP_SKB_CB(skb)->ns != session->nr) {
526a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
527a4ca44faSJoe Perches 					 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
528fd558d18SJames Chapman 					 session->name, L2TP_SKB_CB(skb)->ns,
529fd558d18SJames Chapman 					 L2TP_SKB_CB(skb)->length, session->nr,
530fd558d18SJames Chapman 					 skb_queue_len(&session->reorder_q));
531fd558d18SJames Chapman 				goto out;
532fd558d18SJames Chapman 			}
533fd558d18SJames Chapman 		}
534fd558d18SJames Chapman 		__skb_unlink(skb, &session->reorder_q);
535fd558d18SJames Chapman 
536fd558d18SJames Chapman 		/* Process the skb. We release the queue lock while we
537fd558d18SJames Chapman 		 * do so to let other contexts process the queue.
538fd558d18SJames Chapman 		 */
539fd558d18SJames Chapman 		spin_unlock_bh(&session->reorder_q.lock);
540fd558d18SJames Chapman 		l2tp_recv_dequeue_skb(session, skb);
541e2e210c0SEric Dumazet 		goto start;
542fd558d18SJames Chapman 	}
543fd558d18SJames Chapman 
544fd558d18SJames Chapman out:
545fd558d18SJames Chapman 	spin_unlock_bh(&session->reorder_q.lock);
546fd558d18SJames Chapman }
547fd558d18SJames Chapman 
5488a1631d5SJames Chapman static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
5498a1631d5SJames Chapman {
5508a1631d5SJames Chapman 	u32 nws;
5518a1631d5SJames Chapman 
5528a1631d5SJames Chapman 	if (nr >= session->nr)
5538a1631d5SJames Chapman 		nws = nr - session->nr;
5548a1631d5SJames Chapman 	else
5558a1631d5SJames Chapman 		nws = (session->nr_max + 1) - (session->nr - nr);
5568a1631d5SJames Chapman 
5578a1631d5SJames Chapman 	return nws < session->nr_window_size;
5588a1631d5SJames Chapman }
5598a1631d5SJames Chapman 
560b6dc01a4SJames Chapman /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
561b6dc01a4SJames Chapman  * acceptable, else non-zero.
562b6dc01a4SJames Chapman  */
563b6dc01a4SJames Chapman static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
564b6dc01a4SJames Chapman {
5658a1631d5SJames Chapman 	if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
5668a1631d5SJames Chapman 		/* Packet sequence number is outside allowed window.
5678a1631d5SJames Chapman 		 * Discard it.
5688a1631d5SJames Chapman 		 */
5698a1631d5SJames Chapman 		l2tp_dbg(session, L2TP_MSG_SEQ,
5708a1631d5SJames Chapman 			 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
5718a1631d5SJames Chapman 			 session->name, L2TP_SKB_CB(skb)->ns,
5728a1631d5SJames Chapman 			 L2TP_SKB_CB(skb)->length, session->nr);
5738a1631d5SJames Chapman 		goto discard;
5748a1631d5SJames Chapman 	}
5758a1631d5SJames Chapman 
576b6dc01a4SJames Chapman 	if (session->reorder_timeout != 0) {
577b6dc01a4SJames Chapman 		/* Packet reordering enabled. Add skb to session's
578b6dc01a4SJames Chapman 		 * reorder queue, in order of ns.
579b6dc01a4SJames Chapman 		 */
580b6dc01a4SJames Chapman 		l2tp_recv_queue_skb(session, skb);
581a0dbd822SJames Chapman 		goto out;
582a0dbd822SJames Chapman 	}
583a0dbd822SJames Chapman 
584a0dbd822SJames Chapman 	/* Packet reordering disabled. Discard out-of-sequence packets, while
585a0dbd822SJames Chapman 	 * tracking the number if in-sequence packets after the first OOS packet
586a0dbd822SJames Chapman 	 * is seen. After nr_oos_count_max in-sequence packets, reset the
587a0dbd822SJames Chapman 	 * sequence number to re-enable packet reception.
588b6dc01a4SJames Chapman 	 */
589a0dbd822SJames Chapman 	if (L2TP_SKB_CB(skb)->ns == session->nr) {
590a0dbd822SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
591a0dbd822SJames Chapman 	} else {
592a0dbd822SJames Chapman 		u32 nr_oos = L2TP_SKB_CB(skb)->ns;
593a0dbd822SJames Chapman 		u32 nr_next = (session->nr_oos + 1) & session->nr_max;
594a0dbd822SJames Chapman 
595a0dbd822SJames Chapman 		if (nr_oos == nr_next)
596a0dbd822SJames Chapman 			session->nr_oos_count++;
597a0dbd822SJames Chapman 		else
598a0dbd822SJames Chapman 			session->nr_oos_count = 0;
599a0dbd822SJames Chapman 
600a0dbd822SJames Chapman 		session->nr_oos = nr_oos;
601a0dbd822SJames Chapman 		if (session->nr_oos_count > session->nr_oos_count_max) {
602a0dbd822SJames Chapman 			session->reorder_skip = 1;
603a0dbd822SJames Chapman 			l2tp_dbg(session, L2TP_MSG_SEQ,
604a0dbd822SJames Chapman 				 "%s: %d oos packets received. Resetting sequence numbers\n",
605a0dbd822SJames Chapman 				 session->name, session->nr_oos_count);
606a0dbd822SJames Chapman 		}
607a0dbd822SJames Chapman 		if (!session->reorder_skip) {
608b6dc01a4SJames Chapman 			atomic_long_inc(&session->stats.rx_seq_discards);
609b6dc01a4SJames Chapman 			l2tp_dbg(session, L2TP_MSG_SEQ,
610b6dc01a4SJames Chapman 				 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
611b6dc01a4SJames Chapman 				 session->name, L2TP_SKB_CB(skb)->ns,
612b6dc01a4SJames Chapman 				 L2TP_SKB_CB(skb)->length, session->nr,
613b6dc01a4SJames Chapman 				 skb_queue_len(&session->reorder_q));
614b6dc01a4SJames Chapman 			goto discard;
615b6dc01a4SJames Chapman 		}
616b6dc01a4SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
617b6dc01a4SJames Chapman 	}
618b6dc01a4SJames Chapman 
619a0dbd822SJames Chapman out:
620b6dc01a4SJames Chapman 	return 0;
621b6dc01a4SJames Chapman 
622b6dc01a4SJames Chapman discard:
623b6dc01a4SJames Chapman 	return 1;
624b6dc01a4SJames Chapman }
625b6dc01a4SJames Chapman 
626f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2
627f7faffa3SJames Chapman  * and L2TPv3 data frames here.
628f7faffa3SJames Chapman  *
629f7faffa3SJames Chapman  * L2TPv2 Data Message Header
630f7faffa3SJames Chapman  *
631f7faffa3SJames Chapman  *  0                   1                   2                   3
632f7faffa3SJames Chapman  *  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
633f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634f7faffa3SJames Chapman  * |T|L|x|x|S|x|O|P|x|x|x|x|  Ver  |          Length (opt)         |
635f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636f7faffa3SJames Chapman  * |           Tunnel ID           |           Session ID          |
637f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638f7faffa3SJames Chapman  * |             Ns (opt)          |             Nr (opt)          |
639f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
640f7faffa3SJames Chapman  * |      Offset Size (opt)        |    Offset pad... (opt)
641f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
642f7faffa3SJames Chapman  *
643f7faffa3SJames Chapman  * Data frames are marked by T=0. All other fields are the same as
644f7faffa3SJames Chapman  * those in L2TP control frames.
645f7faffa3SJames Chapman  *
646f7faffa3SJames Chapman  * L2TPv3 Data Message Header
647f7faffa3SJames Chapman  *
648f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
649f7faffa3SJames Chapman  * |                      L2TP Session Header                      |
650f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
651f7faffa3SJames Chapman  * |                      L2-Specific Sublayer                     |
652f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
653f7faffa3SJames Chapman  * |                        Tunnel Payload                      ...
654f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
655f7faffa3SJames Chapman  *
656f7faffa3SJames Chapman  * L2TPv3 Session Header Over IP
657f7faffa3SJames Chapman  *
658f7faffa3SJames Chapman  *  0                   1                   2                   3
659f7faffa3SJames Chapman  *  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
660f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
661f7faffa3SJames Chapman  * |                           Session ID                          |
662f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
663f7faffa3SJames Chapman  * |               Cookie (optional, maximum 64 bits)...
664f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
665f7faffa3SJames Chapman  *                                                                 |
666f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
667f7faffa3SJames Chapman  *
668f7faffa3SJames Chapman  * L2TPv3 L2-Specific Sublayer Format
669f7faffa3SJames Chapman  *
670f7faffa3SJames Chapman  *  0                   1                   2                   3
671f7faffa3SJames Chapman  *  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
672f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
673f7faffa3SJames Chapman  * |x|S|x|x|x|x|x|x|              Sequence Number                  |
674f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675f7faffa3SJames Chapman  *
676f7faffa3SJames Chapman  * Cookie value, sublayer format and offset (pad) are negotiated with
677f7faffa3SJames Chapman  * the peer when the session is set up. Unlike L2TPv2, we do not need
678f7faffa3SJames Chapman  * to parse the packet header to determine if optional fields are
679f7faffa3SJames Chapman  * present.
680f7faffa3SJames Chapman  *
681f7faffa3SJames Chapman  * Caller must already have parsed the frame and determined that it is
682f7faffa3SJames Chapman  * a data (not control) frame before coming here. Fields up to the
683f7faffa3SJames Chapman  * session-id have already been parsed and ptr points to the data
684f7faffa3SJames Chapman  * after the session-id.
68561b9a047SGuillaume Nault  *
68661b9a047SGuillaume Nault  * session->ref() must have been called prior to l2tp_recv_common().
68761b9a047SGuillaume Nault  * session->deref() will be called automatically after skb is processed.
688f7faffa3SJames Chapman  */
689f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
690f7faffa3SJames Chapman 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
691f7faffa3SJames Chapman 		      int length, int (*payload_hook)(struct sk_buff *skb))
692f7faffa3SJames Chapman {
693f7faffa3SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
694f7faffa3SJames Chapman 	int offset;
695f7faffa3SJames Chapman 	u32 ns, nr;
696f7faffa3SJames Chapman 
697f7faffa3SJames Chapman 	/* Parse and check optional cookie */
698f7faffa3SJames Chapman 	if (session->peer_cookie_len > 0) {
699f7faffa3SJames Chapman 		if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
700a4ca44faSJoe Perches 			l2tp_info(tunnel, L2TP_MSG_DATA,
701f7faffa3SJames Chapman 				  "%s: cookie mismatch (%u/%u). Discarding.\n",
702a4ca44faSJoe Perches 				  tunnel->name, tunnel->tunnel_id,
703a4ca44faSJoe Perches 				  session->session_id);
7047b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_cookie_discards);
705f7faffa3SJames Chapman 			goto discard;
706f7faffa3SJames Chapman 		}
707f7faffa3SJames Chapman 		ptr += session->peer_cookie_len;
708f7faffa3SJames Chapman 	}
709f7faffa3SJames Chapman 
710f7faffa3SJames Chapman 	/* Handle the optional sequence numbers. Sequence numbers are
711f7faffa3SJames Chapman 	 * in different places for L2TPv2 and L2TPv3.
712f7faffa3SJames Chapman 	 *
713f7faffa3SJames Chapman 	 * If we are the LAC, enable/disable sequence numbers under
714f7faffa3SJames Chapman 	 * the control of the LNS.  If no sequence numbers present but
715f7faffa3SJames Chapman 	 * we were expecting them, discard frame.
716f7faffa3SJames Chapman 	 */
717f7faffa3SJames Chapman 	ns = nr = 0;
718f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->has_seq = 0;
719f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
720f7faffa3SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_S) {
721f7faffa3SJames Chapman 			ns = ntohs(*(__be16 *) ptr);
722f7faffa3SJames Chapman 			ptr += 2;
723f7faffa3SJames Chapman 			nr = ntohs(*(__be16 *) ptr);
724f7faffa3SJames Chapman 			ptr += 2;
725f7faffa3SJames Chapman 
726f7faffa3SJames Chapman 			/* Store L2TP info in the skb */
727f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->ns = ns;
728f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->has_seq = 1;
729f7faffa3SJames Chapman 
730a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
731f7faffa3SJames Chapman 				 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
732f7faffa3SJames Chapman 				 session->name, ns, nr, session->nr);
733f7faffa3SJames Chapman 		}
734f7faffa3SJames Chapman 	} else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
735f7faffa3SJames Chapman 		u32 l2h = ntohl(*(__be32 *) ptr);
736f7faffa3SJames Chapman 
737f7faffa3SJames Chapman 		if (l2h & 0x40000000) {
738f7faffa3SJames Chapman 			ns = l2h & 0x00ffffff;
739f7faffa3SJames Chapman 
740f7faffa3SJames Chapman 			/* Store L2TP info in the skb */
741f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->ns = ns;
742f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->has_seq = 1;
743f7faffa3SJames Chapman 
744a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
745f7faffa3SJames Chapman 				 "%s: recv data ns=%u, session nr=%u\n",
746f7faffa3SJames Chapman 				 session->name, ns, session->nr);
747f7faffa3SJames Chapman 		}
748f7faffa3SJames Chapman 	}
749f7faffa3SJames Chapman 
750f7faffa3SJames Chapman 	/* Advance past L2-specific header, if present */
751f7faffa3SJames Chapman 	ptr += session->l2specific_len;
752f7faffa3SJames Chapman 
753f7faffa3SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
754f7faffa3SJames Chapman 		/* Received a packet with sequence numbers. If we're the LNS,
755f7faffa3SJames Chapman 		 * check if we sre sending sequence numbers and if not,
756f7faffa3SJames Chapman 		 * configure it so.
757f7faffa3SJames Chapman 		 */
758f7faffa3SJames Chapman 		if ((!session->lns_mode) && (!session->send_seq)) {
759a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_SEQ,
760f7faffa3SJames Chapman 				  "%s: requested to enable seq numbers by LNS\n",
761f7faffa3SJames Chapman 				  session->name);
7623f9b9770SAsbjørn Sloth Tønnesen 			session->send_seq = 1;
763f7faffa3SJames Chapman 			l2tp_session_set_header_len(session, tunnel->version);
764f7faffa3SJames Chapman 		}
765f7faffa3SJames Chapman 	} else {
766f7faffa3SJames Chapman 		/* No sequence numbers.
767f7faffa3SJames Chapman 		 * If user has configured mandatory sequence numbers, discard.
768f7faffa3SJames Chapman 		 */
769f7faffa3SJames Chapman 		if (session->recv_seq) {
770a4ca44faSJoe Perches 			l2tp_warn(session, L2TP_MSG_SEQ,
771a4ca44faSJoe Perches 				  "%s: recv data has no seq numbers when required. Discarding.\n",
772a4ca44faSJoe Perches 				  session->name);
7737b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
774f7faffa3SJames Chapman 			goto discard;
775f7faffa3SJames Chapman 		}
776f7faffa3SJames Chapman 
777f7faffa3SJames Chapman 		/* If we're the LAC and we're sending sequence numbers, the
778f7faffa3SJames Chapman 		 * LNS has requested that we no longer send sequence numbers.
779f7faffa3SJames Chapman 		 * If we're the LNS and we're sending sequence numbers, the
780f7faffa3SJames Chapman 		 * LAC is broken. Discard the frame.
781f7faffa3SJames Chapman 		 */
782f7faffa3SJames Chapman 		if ((!session->lns_mode) && (session->send_seq)) {
783a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_SEQ,
784f7faffa3SJames Chapman 				  "%s: requested to disable seq numbers by LNS\n",
785f7faffa3SJames Chapman 				  session->name);
786f7faffa3SJames Chapman 			session->send_seq = 0;
787f7faffa3SJames Chapman 			l2tp_session_set_header_len(session, tunnel->version);
788f7faffa3SJames Chapman 		} else if (session->send_seq) {
789a4ca44faSJoe Perches 			l2tp_warn(session, L2TP_MSG_SEQ,
790a4ca44faSJoe Perches 				  "%s: recv data has no seq numbers when required. Discarding.\n",
791a4ca44faSJoe Perches 				  session->name);
7927b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
793f7faffa3SJames Chapman 			goto discard;
794f7faffa3SJames Chapman 		}
795f7faffa3SJames Chapman 	}
796f7faffa3SJames Chapman 
797f7faffa3SJames Chapman 	/* Session data offset is handled differently for L2TPv2 and
798f7faffa3SJames Chapman 	 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
799f7faffa3SJames Chapman 	 * the header. For L2TPv3, the offset is negotiated using AVPs
800f7faffa3SJames Chapman 	 * in the session setup control protocol.
801f7faffa3SJames Chapman 	 */
802f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
803f7faffa3SJames Chapman 		/* If offset bit set, skip it. */
804f7faffa3SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_O) {
805f7faffa3SJames Chapman 			offset = ntohs(*(__be16 *)ptr);
806f7faffa3SJames Chapman 			ptr += 2 + offset;
807f7faffa3SJames Chapman 		}
808f7faffa3SJames Chapman 	} else
809f7faffa3SJames Chapman 		ptr += session->offset;
810f7faffa3SJames Chapman 
811f7faffa3SJames Chapman 	offset = ptr - optr;
812f7faffa3SJames Chapman 	if (!pskb_may_pull(skb, offset))
813f7faffa3SJames Chapman 		goto discard;
814f7faffa3SJames Chapman 
815f7faffa3SJames Chapman 	__skb_pull(skb, offset);
816f7faffa3SJames Chapman 
817f7faffa3SJames Chapman 	/* If caller wants to process the payload before we queue the
818f7faffa3SJames Chapman 	 * packet, do so now.
819f7faffa3SJames Chapman 	 */
820f7faffa3SJames Chapman 	if (payload_hook)
821f7faffa3SJames Chapman 		if ((*payload_hook)(skb))
822f7faffa3SJames Chapman 			goto discard;
823f7faffa3SJames Chapman 
824f7faffa3SJames Chapman 	/* Prepare skb for adding to the session's reorder_q.  Hold
825f7faffa3SJames Chapman 	 * packets for max reorder_timeout or 1 second if not
826f7faffa3SJames Chapman 	 * reordering.
827f7faffa3SJames Chapman 	 */
828f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->length = length;
829f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->expires = jiffies +
830f7faffa3SJames Chapman 		(session->reorder_timeout ? session->reorder_timeout : HZ);
831f7faffa3SJames Chapman 
832f7faffa3SJames Chapman 	/* Add packet to the session's receive queue. Reordering is done here, if
833f7faffa3SJames Chapman 	 * enabled. Saved L2TP protocol info is stored in skb->sb[].
834f7faffa3SJames Chapman 	 */
835f7faffa3SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
836b6dc01a4SJames Chapman 		if (l2tp_recv_data_seq(session, skb))
837f7faffa3SJames Chapman 			goto discard;
838f7faffa3SJames Chapman 	} else {
839f7faffa3SJames Chapman 		/* No sequence numbers. Add the skb to the tail of the
840f7faffa3SJames Chapman 		 * reorder queue. This ensures that it will be
841f7faffa3SJames Chapman 		 * delivered after all previous sequenced skbs.
842f7faffa3SJames Chapman 		 */
843f7faffa3SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
844f7faffa3SJames Chapman 	}
845f7faffa3SJames Chapman 
846f7faffa3SJames Chapman 	/* Try to dequeue as many skbs from reorder_q as we can. */
847f7faffa3SJames Chapman 	l2tp_recv_dequeue(session);
848f7faffa3SJames Chapman 
849f7faffa3SJames Chapman 	return;
850f7faffa3SJames Chapman 
851f7faffa3SJames Chapman discard:
8527b7c0719STom Parkin 	atomic_long_inc(&session->stats.rx_errors);
853f7faffa3SJames Chapman 	kfree_skb(skb);
854f7faffa3SJames Chapman 
855f7faffa3SJames Chapman 	if (session->deref)
856f7faffa3SJames Chapman 		(*session->deref)(session);
857f7faffa3SJames Chapman }
858f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common);
859f7faffa3SJames Chapman 
86048f72f92STom Parkin /* Drop skbs from the session's reorder_q
86148f72f92STom Parkin  */
86248f72f92STom Parkin int l2tp_session_queue_purge(struct l2tp_session *session)
86348f72f92STom Parkin {
86448f72f92STom Parkin 	struct sk_buff *skb = NULL;
86548f72f92STom Parkin 	BUG_ON(!session);
86648f72f92STom Parkin 	BUG_ON(session->magic != L2TP_SESSION_MAGIC);
86748f72f92STom Parkin 	while ((skb = skb_dequeue(&session->reorder_q))) {
86848f72f92STom Parkin 		atomic_long_inc(&session->stats.rx_errors);
86948f72f92STom Parkin 		kfree_skb(skb);
87048f72f92STom Parkin 		if (session->deref)
87148f72f92STom Parkin 			(*session->deref)(session);
87248f72f92STom Parkin 	}
87348f72f92STom Parkin 	return 0;
87448f72f92STom Parkin }
87548f72f92STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
87648f72f92STom Parkin 
877fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
878fd558d18SJames Chapman  * here. The skb is not on a list when we get here.
879fd558d18SJames Chapman  * Returns 0 if the packet was a data packet and was successfully passed on.
880fd558d18SJames Chapman  * Returns 1 if the packet was not a good data packet and could not be
881fd558d18SJames Chapman  * forwarded.  All such packets are passed up to userspace to deal with.
882fd558d18SJames Chapman  */
883fc130840Sstephen hemminger static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
884fd558d18SJames Chapman 			      int (*payload_hook)(struct sk_buff *skb))
885fd558d18SJames Chapman {
886fd558d18SJames Chapman 	struct l2tp_session *session = NULL;
887fd558d18SJames Chapman 	unsigned char *ptr, *optr;
888fd558d18SJames Chapman 	u16 hdrflags;
889fd558d18SJames Chapman 	u32 tunnel_id, session_id;
890fd558d18SJames Chapman 	u16 version;
891f7faffa3SJames Chapman 	int length;
892fd558d18SJames Chapman 
89358d6085cSTom Herbert 	/* UDP has verifed checksum */
894fd558d18SJames Chapman 
895fd558d18SJames Chapman 	/* UDP always verifies the packet length. */
896fd558d18SJames Chapman 	__skb_pull(skb, sizeof(struct udphdr));
897fd558d18SJames Chapman 
898fd558d18SJames Chapman 	/* Short packet? */
899fd558d18SJames Chapman 	if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
900a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
901a4ca44faSJoe Perches 			  "%s: recv short packet (len=%d)\n",
902a4ca44faSJoe Perches 			  tunnel->name, skb->len);
903fd558d18SJames Chapman 		goto error;
904fd558d18SJames Chapman 	}
905fd558d18SJames Chapman 
906fd558d18SJames Chapman 	/* Trace packet contents, if enabled */
907fd558d18SJames Chapman 	if (tunnel->debug & L2TP_MSG_DATA) {
908fd558d18SJames Chapman 		length = min(32u, skb->len);
909fd558d18SJames Chapman 		if (!pskb_may_pull(skb, length))
910fd558d18SJames Chapman 			goto error;
911fd558d18SJames Chapman 
912a4ca44faSJoe Perches 		pr_debug("%s: recv\n", tunnel->name);
913a4ca44faSJoe Perches 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
914fd558d18SJames Chapman 	}
915fd558d18SJames Chapman 
916e50e705cSEric Dumazet 	/* Point to L2TP header */
917e50e705cSEric Dumazet 	optr = ptr = skb->data;
918e50e705cSEric Dumazet 
919fd558d18SJames Chapman 	/* Get L2TP header flags */
920fd558d18SJames Chapman 	hdrflags = ntohs(*(__be16 *) ptr);
921fd558d18SJames Chapman 
922fd558d18SJames Chapman 	/* Check protocol version */
923fd558d18SJames Chapman 	version = hdrflags & L2TP_HDR_VER_MASK;
924fd558d18SJames Chapman 	if (version != tunnel->version) {
925a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
926fd558d18SJames Chapman 			  "%s: recv protocol version mismatch: got %d expected %d\n",
927fd558d18SJames Chapman 			  tunnel->name, version, tunnel->version);
928fd558d18SJames Chapman 		goto error;
929fd558d18SJames Chapman 	}
930fd558d18SJames Chapman 
931fd558d18SJames Chapman 	/* Get length of L2TP packet */
932fd558d18SJames Chapman 	length = skb->len;
933fd558d18SJames Chapman 
934fd558d18SJames Chapman 	/* If type is control packet, it is handled by userspace. */
935fd558d18SJames Chapman 	if (hdrflags & L2TP_HDRFLAG_T) {
936a4ca44faSJoe Perches 		l2tp_dbg(tunnel, L2TP_MSG_DATA,
937a4ca44faSJoe Perches 			 "%s: recv control packet, len=%d\n",
938a4ca44faSJoe Perches 			 tunnel->name, length);
939fd558d18SJames Chapman 		goto error;
940fd558d18SJames Chapman 	}
941fd558d18SJames Chapman 
942fd558d18SJames Chapman 	/* Skip flags */
943fd558d18SJames Chapman 	ptr += 2;
944fd558d18SJames Chapman 
945f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
946fd558d18SJames Chapman 		/* If length is present, skip it */
947fd558d18SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_L)
948fd558d18SJames Chapman 			ptr += 2;
949fd558d18SJames Chapman 
950fd558d18SJames Chapman 		/* Extract tunnel and session ID */
951fd558d18SJames Chapman 		tunnel_id = ntohs(*(__be16 *) ptr);
952fd558d18SJames Chapman 		ptr += 2;
953fd558d18SJames Chapman 		session_id = ntohs(*(__be16 *) ptr);
954fd558d18SJames Chapman 		ptr += 2;
955f7faffa3SJames Chapman 	} else {
956f7faffa3SJames Chapman 		ptr += 2;	/* skip reserved bits */
957f7faffa3SJames Chapman 		tunnel_id = tunnel->tunnel_id;
958f7faffa3SJames Chapman 		session_id = ntohl(*(__be32 *) ptr);
959f7faffa3SJames Chapman 		ptr += 4;
960f7faffa3SJames Chapman 	}
961fd558d18SJames Chapman 
962fd558d18SJames Chapman 	/* Find the session context */
96361b9a047SGuillaume Nault 	session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
964309795f4SJames Chapman 	if (!session || !session->recv_skb) {
96561b9a047SGuillaume Nault 		if (session) {
96661b9a047SGuillaume Nault 			if (session->deref)
96761b9a047SGuillaume Nault 				session->deref(session);
96861b9a047SGuillaume Nault 			l2tp_session_dec_refcount(session);
96961b9a047SGuillaume Nault 		}
97061b9a047SGuillaume Nault 
971fd558d18SJames Chapman 		/* Not found? Pass to userspace to deal with */
972a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
973f7faffa3SJames Chapman 			  "%s: no session found (%u/%u). Passing up.\n",
974fd558d18SJames Chapman 			  tunnel->name, tunnel_id, session_id);
975fd558d18SJames Chapman 		goto error;
976fd558d18SJames Chapman 	}
977fd558d18SJames Chapman 
978f7faffa3SJames Chapman 	l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
97961b9a047SGuillaume Nault 	l2tp_session_dec_refcount(session);
980fd558d18SJames Chapman 
981fd558d18SJames Chapman 	return 0;
982fd558d18SJames Chapman 
983fd558d18SJames Chapman error:
984fd558d18SJames Chapman 	/* Put UDP header back */
985fd558d18SJames Chapman 	__skb_push(skb, sizeof(struct udphdr));
986fd558d18SJames Chapman 
987fd558d18SJames Chapman 	return 1;
988fd558d18SJames Chapman }
989fd558d18SJames Chapman 
990fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c.
991fd558d18SJames Chapman  * Return codes:
992fd558d18SJames Chapman  * 0 : success.
993fd558d18SJames Chapman  * <0: error
994fd558d18SJames Chapman  * >0: skb should be passed up to userspace as UDP.
995fd558d18SJames Chapman  */
996fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
997fd558d18SJames Chapman {
998fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
999fd558d18SJames Chapman 
1000fd558d18SJames Chapman 	tunnel = l2tp_sock_to_tunnel(sk);
1001fd558d18SJames Chapman 	if (tunnel == NULL)
1002fd558d18SJames Chapman 		goto pass_up;
1003fd558d18SJames Chapman 
1004a4ca44faSJoe Perches 	l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1005a4ca44faSJoe Perches 		 tunnel->name, skb->len);
1006fd558d18SJames Chapman 
1007fd558d18SJames Chapman 	if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1008fd558d18SJames Chapman 		goto pass_up_put;
1009fd558d18SJames Chapman 
1010fd558d18SJames Chapman 	sock_put(sk);
1011fd558d18SJames Chapman 	return 0;
1012fd558d18SJames Chapman 
1013fd558d18SJames Chapman pass_up_put:
1014fd558d18SJames Chapman 	sock_put(sk);
1015fd558d18SJames Chapman pass_up:
1016fd558d18SJames Chapman 	return 1;
1017fd558d18SJames Chapman }
1018fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1019fd558d18SJames Chapman 
1020fd558d18SJames Chapman /************************************************************************
1021fd558d18SJames Chapman  * Transmit handling
1022fd558d18SJames Chapman  ***********************************************************************/
1023fd558d18SJames Chapman 
1024fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided.
1025fd558d18SJames Chapman  */
1026f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
1027fd558d18SJames Chapman {
1028f7faffa3SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1029fd558d18SJames Chapman 	__be16 *bufp = buf;
1030f7faffa3SJames Chapman 	__be16 *optr = buf;
1031fd558d18SJames Chapman 	u16 flags = L2TP_HDR_VER_2;
1032fd558d18SJames Chapman 	u32 tunnel_id = tunnel->peer_tunnel_id;
1033fd558d18SJames Chapman 	u32 session_id = session->peer_session_id;
1034fd558d18SJames Chapman 
1035fd558d18SJames Chapman 	if (session->send_seq)
1036fd558d18SJames Chapman 		flags |= L2TP_HDRFLAG_S;
1037fd558d18SJames Chapman 
1038fd558d18SJames Chapman 	/* Setup L2TP header. */
1039fd558d18SJames Chapman 	*bufp++ = htons(flags);
1040fd558d18SJames Chapman 	*bufp++ = htons(tunnel_id);
1041fd558d18SJames Chapman 	*bufp++ = htons(session_id);
1042fd558d18SJames Chapman 	if (session->send_seq) {
1043fd558d18SJames Chapman 		*bufp++ = htons(session->ns);
1044fd558d18SJames Chapman 		*bufp++ = 0;
1045fd558d18SJames Chapman 		session->ns++;
1046f7faffa3SJames Chapman 		session->ns &= 0xffff;
1047a4ca44faSJoe Perches 		l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1048a4ca44faSJoe Perches 			 session->name, session->ns);
1049fd558d18SJames Chapman 	}
1050fd558d18SJames Chapman 
1051f7faffa3SJames Chapman 	return bufp - optr;
1052f7faffa3SJames Chapman }
1053f7faffa3SJames Chapman 
1054f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1055fd558d18SJames Chapman {
10560d76751fSJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1057f7faffa3SJames Chapman 	char *bufp = buf;
1058f7faffa3SJames Chapman 	char *optr = bufp;
1059fd558d18SJames Chapman 
10600d76751fSJames Chapman 	/* Setup L2TP header. The header differs slightly for UDP and
10610d76751fSJames Chapman 	 * IP encapsulations. For UDP, there is 4 bytes of flags.
10620d76751fSJames Chapman 	 */
10630d76751fSJames Chapman 	if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
10640d76751fSJames Chapman 		u16 flags = L2TP_HDR_VER_3;
1065f7faffa3SJames Chapman 		*((__be16 *) bufp) = htons(flags);
1066f7faffa3SJames Chapman 		bufp += 2;
1067f7faffa3SJames Chapman 		*((__be16 *) bufp) = 0;
1068f7faffa3SJames Chapman 		bufp += 2;
10690d76751fSJames Chapman 	}
10700d76751fSJames Chapman 
1071f7faffa3SJames Chapman 	*((__be32 *) bufp) = htonl(session->peer_session_id);
1072f7faffa3SJames Chapman 	bufp += 4;
1073f7faffa3SJames Chapman 	if (session->cookie_len) {
1074f7faffa3SJames Chapman 		memcpy(bufp, &session->cookie[0], session->cookie_len);
1075f7faffa3SJames Chapman 		bufp += session->cookie_len;
1076fd558d18SJames Chapman 	}
1077f7faffa3SJames Chapman 	if (session->l2specific_len) {
1078f7faffa3SJames Chapman 		if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1079f7faffa3SJames Chapman 			u32 l2h = 0;
1080f7faffa3SJames Chapman 			if (session->send_seq) {
1081f7faffa3SJames Chapman 				l2h = 0x40000000 | session->ns;
1082f7faffa3SJames Chapman 				session->ns++;
1083f7faffa3SJames Chapman 				session->ns &= 0xffffff;
1084a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
1085a4ca44faSJoe Perches 					 "%s: updated ns to %u\n",
1086a4ca44faSJoe Perches 					 session->name, session->ns);
1087f7faffa3SJames Chapman 			}
1088f7faffa3SJames Chapman 
1089f7faffa3SJames Chapman 			*((__be32 *) bufp) = htonl(l2h);
1090f7faffa3SJames Chapman 		}
1091f7faffa3SJames Chapman 		bufp += session->l2specific_len;
1092f7faffa3SJames Chapman 	}
1093f7faffa3SJames Chapman 	if (session->offset)
1094f7faffa3SJames Chapman 		bufp += session->offset;
1095f7faffa3SJames Chapman 
1096f7faffa3SJames Chapman 	return bufp - optr;
1097f7faffa3SJames Chapman }
1098fd558d18SJames Chapman 
1099fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1100d9d8da80SDavid S. Miller 			  struct flowi *fl, size_t data_len)
1101fd558d18SJames Chapman {
1102fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1103fd558d18SJames Chapman 	unsigned int len = skb->len;
1104fd558d18SJames Chapman 	int error;
1105fd558d18SJames Chapman 
1106fd558d18SJames Chapman 	/* Debug */
1107fd558d18SJames Chapman 	if (session->send_seq)
11085b5e0928SAlexey Dobriyan 		l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
1109a4ca44faSJoe Perches 			 session->name, data_len, session->ns - 1);
1110fd558d18SJames Chapman 	else
11115b5e0928SAlexey Dobriyan 		l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
1112a4ca44faSJoe Perches 			 session->name, data_len);
1113fd558d18SJames Chapman 
1114fd558d18SJames Chapman 	if (session->debug & L2TP_MSG_DATA) {
11150d76751fSJames Chapman 		int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
11160d76751fSJames Chapman 		unsigned char *datap = skb->data + uhlen;
1117fd558d18SJames Chapman 
1118a4ca44faSJoe Perches 		pr_debug("%s: xmit\n", session->name);
1119a4ca44faSJoe Perches 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1120a4ca44faSJoe Perches 				     datap, min_t(size_t, 32, len - uhlen));
1121fd558d18SJames Chapman 	}
1122fd558d18SJames Chapman 
1123fd558d18SJames Chapman 	/* Queue the packet to IP for output */
112460ff7467SWANG Cong 	skb->ignore_df = 1;
1125d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1126746e3499SEric Dumazet 	if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
1127b0270e91SEric Dumazet 		error = inet6_csk_xmit(tunnel->sock, skb, NULL);
1128d2cf3361SBenjamin LaHaise 	else
1129d2cf3361SBenjamin LaHaise #endif
1130b0270e91SEric Dumazet 		error = ip_queue_xmit(tunnel->sock, skb, fl);
1131fd558d18SJames Chapman 
1132fd558d18SJames Chapman 	/* Update stats */
1133fd558d18SJames Chapman 	if (error >= 0) {
11347b7c0719STom Parkin 		atomic_long_inc(&tunnel->stats.tx_packets);
11357b7c0719STom Parkin 		atomic_long_add(len, &tunnel->stats.tx_bytes);
11367b7c0719STom Parkin 		atomic_long_inc(&session->stats.tx_packets);
11377b7c0719STom Parkin 		atomic_long_add(len, &session->stats.tx_bytes);
1138fd558d18SJames Chapman 	} else {
11397b7c0719STom Parkin 		atomic_long_inc(&tunnel->stats.tx_errors);
11407b7c0719STom Parkin 		atomic_long_inc(&session->stats.tx_errors);
1141fd558d18SJames Chapman 	}
1142fd558d18SJames Chapman 
1143fd558d18SJames Chapman 	return 0;
1144fd558d18SJames Chapman }
1145fd558d18SJames Chapman 
1146fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be
1147fd558d18SJames Chapman  * inserted in the skb data before calling this function.
1148fd558d18SJames Chapman  */
1149fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1150fd558d18SJames Chapman {
1151fd558d18SJames Chapman 	int data_len = skb->len;
11520d76751fSJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
11530d76751fSJames Chapman 	struct sock *sk = tunnel->sock;
1154d9d8da80SDavid S. Miller 	struct flowi *fl;
1155fd558d18SJames Chapman 	struct udphdr *uh;
1156fd558d18SJames Chapman 	struct inet_sock *inet;
1157fd558d18SJames Chapman 	int headroom;
11580d76751fSJames Chapman 	int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
11590d76751fSJames Chapman 	int udp_len;
1160b8c84307SEric Dumazet 	int ret = NET_XMIT_SUCCESS;
1161fd558d18SJames Chapman 
1162fd558d18SJames Chapman 	/* Check that there's enough headroom in the skb to insert IP,
1163fd558d18SJames Chapman 	 * UDP and L2TP headers. If not enough, expand it to
1164fd558d18SJames Chapman 	 * make room. Adjust truesize.
1165fd558d18SJames Chapman 	 */
1166fd558d18SJames Chapman 	headroom = NET_SKB_PAD + sizeof(struct iphdr) +
11670d76751fSJames Chapman 		uhlen + hdr_len;
1168835acf5dSEric Dumazet 	if (skb_cow_head(skb, headroom)) {
1169b8c84307SEric Dumazet 		kfree_skb(skb);
1170b8c84307SEric Dumazet 		return NET_XMIT_DROP;
1171835acf5dSEric Dumazet 	}
1172fd558d18SJames Chapman 
1173fd558d18SJames Chapman 	/* Setup L2TP header */
1174f7faffa3SJames Chapman 	session->build_header(session, __skb_push(skb, hdr_len));
1175fd558d18SJames Chapman 
11760d76751fSJames Chapman 	/* Reset skb netfilter state */
1177fd558d18SJames Chapman 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1178fd558d18SJames Chapman 	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1179fd558d18SJames Chapman 			      IPSKB_REROUTED);
1180fd558d18SJames Chapman 	nf_reset(skb);
1181fd558d18SJames Chapman 
11826af88da1SDavid S. Miller 	bh_lock_sock(sk);
11836af88da1SDavid S. Miller 	if (sock_owned_by_user(sk)) {
1184b8c84307SEric Dumazet 		kfree_skb(skb);
1185b8c84307SEric Dumazet 		ret = NET_XMIT_DROP;
11866af88da1SDavid S. Miller 		goto out_unlock;
11876af88da1SDavid S. Miller 	}
11886af88da1SDavid S. Miller 
1189fd558d18SJames Chapman 	/* Get routing info from the tunnel socket */
1190fd558d18SJames Chapman 	skb_dst_drop(skb);
119171b1391aSFlorian Westphal 	skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
11920d76751fSJames Chapman 
1193d9d8da80SDavid S. Miller 	inet = inet_sk(sk);
1194d9d8da80SDavid S. Miller 	fl = &inet->cork.fl;
11950d76751fSJames Chapman 	switch (tunnel->encap) {
11960d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
11970d76751fSJames Chapman 		/* Setup UDP header */
11980d76751fSJames Chapman 		__skb_push(skb, sizeof(*uh));
11990d76751fSJames Chapman 		skb_reset_transport_header(skb);
12000d76751fSJames Chapman 		uh = udp_hdr(skb);
12010d76751fSJames Chapman 		uh->source = inet->inet_sport;
12020d76751fSJames Chapman 		uh->dest = inet->inet_dport;
12030d76751fSJames Chapman 		udp_len = uhlen + hdr_len + data_len;
12040d76751fSJames Chapman 		uh->len = htons(udp_len);
1205fd558d18SJames Chapman 
1206fd558d18SJames Chapman 		/* Calculate UDP checksum if configured to do so */
1207d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1208e18503f4SFrançois Cachereul 		if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
120977157e19STom Herbert 			udp6_set_csum(udp_get_no_check6_tx(sk),
121077157e19STom Herbert 				      skb, &inet6_sk(sk)->saddr,
121177157e19STom Herbert 				      &sk->sk_v6_daddr, udp_len);
1212d2cf3361SBenjamin LaHaise 		else
1213d2cf3361SBenjamin LaHaise #endif
121477157e19STom Herbert 		udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
121577157e19STom Herbert 			     inet->inet_daddr, udp_len);
12160d76751fSJames Chapman 		break;
12170d76751fSJames Chapman 
12180d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
12190d76751fSJames Chapman 		break;
12200d76751fSJames Chapman 	}
12210d76751fSJames Chapman 
1222d9d8da80SDavid S. Miller 	l2tp_xmit_core(session, skb, fl, data_len);
12236af88da1SDavid S. Miller out_unlock:
12246af88da1SDavid S. Miller 	bh_unlock_sock(sk);
1225fd558d18SJames Chapman 
1226b8c84307SEric Dumazet 	return ret;
1227fd558d18SJames Chapman }
1228fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1229fd558d18SJames Chapman 
1230fd558d18SJames Chapman /*****************************************************************************
1231fd558d18SJames Chapman  * Tinnel and session create/destroy.
1232fd558d18SJames Chapman  *****************************************************************************/
1233fd558d18SJames Chapman 
1234fd558d18SJames Chapman /* Tunnel socket destruct hook.
1235fd558d18SJames Chapman  * The tunnel context is deleted only when all session sockets have been
1236fd558d18SJames Chapman  * closed.
1237fd558d18SJames Chapman  */
1238fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk)
1239fd558d18SJames Chapman {
12408d8a51e2SDavid S. Miller 	struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1241f8ccac0eSTom Parkin 	struct l2tp_net *pn;
1242fd558d18SJames Chapman 
1243fd558d18SJames Chapman 	if (tunnel == NULL)
1244fd558d18SJames Chapman 		goto end;
1245fd558d18SJames Chapman 
1246a4ca44faSJoe Perches 	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1247fd558d18SJames Chapman 
1248fd558d18SJames Chapman 
1249f8ccac0eSTom Parkin 	/* Disable udp encapsulation */
12500d76751fSJames Chapman 	switch (tunnel->encap) {
12510d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1252fd558d18SJames Chapman 		/* No longer an encapsulation socket. See net/ipv4/udp.c */
1253fd558d18SJames Chapman 		(udp_sk(sk))->encap_type = 0;
1254fd558d18SJames Chapman 		(udp_sk(sk))->encap_rcv = NULL;
12559980d001STom Parkin 		(udp_sk(sk))->encap_destroy = NULL;
12560d76751fSJames Chapman 		break;
12570d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
12580d76751fSJames Chapman 		break;
12590d76751fSJames Chapman 	}
1260fd558d18SJames Chapman 
1261fd558d18SJames Chapman 	/* Remove hooks into tunnel socket */
1262fd558d18SJames Chapman 	sk->sk_destruct = tunnel->old_sk_destruct;
1263fd558d18SJames Chapman 	sk->sk_user_data = NULL;
1264f8ccac0eSTom Parkin 	tunnel->sock = NULL;
1265f8ccac0eSTom Parkin 
1266f8ccac0eSTom Parkin 	/* Remove the tunnel struct from the tunnel list */
1267f8ccac0eSTom Parkin 	pn = l2tp_pernet(tunnel->l2tp_net);
1268f8ccac0eSTom Parkin 	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1269f8ccac0eSTom Parkin 	list_del_rcu(&tunnel->list);
1270f8ccac0eSTom Parkin 	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1271f8ccac0eSTom Parkin 	atomic_dec(&l2tp_tunnel_count);
1272f8ccac0eSTom Parkin 
1273f8ccac0eSTom Parkin 	l2tp_tunnel_closeall(tunnel);
1274f8ccac0eSTom Parkin 	l2tp_tunnel_dec_refcount(tunnel);
1275fd558d18SJames Chapman 
1276fd558d18SJames Chapman 	/* Call the original destructor */
1277fd558d18SJames Chapman 	if (sk->sk_destruct)
1278fd558d18SJames Chapman 		(*sk->sk_destruct)(sk);
1279fd558d18SJames Chapman end:
1280fd558d18SJames Chapman 	return;
1281fd558d18SJames Chapman }
1282fd558d18SJames Chapman 
1283fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too.
1284fd558d18SJames Chapman  */
1285e34f4c70STom Parkin void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1286fd558d18SJames Chapman {
1287fd558d18SJames Chapman 	int hash;
1288fd558d18SJames Chapman 	struct hlist_node *walk;
1289fd558d18SJames Chapman 	struct hlist_node *tmp;
1290fd558d18SJames Chapman 	struct l2tp_session *session;
1291fd558d18SJames Chapman 
1292fd558d18SJames Chapman 	BUG_ON(tunnel == NULL);
1293fd558d18SJames Chapman 
1294a4ca44faSJoe Perches 	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1295a4ca44faSJoe Perches 		  tunnel->name);
1296fd558d18SJames Chapman 
1297fd558d18SJames Chapman 	write_lock_bh(&tunnel->hlist_lock);
1298fd558d18SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1299fd558d18SJames Chapman again:
1300fd558d18SJames Chapman 		hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1301fd558d18SJames Chapman 			session = hlist_entry(walk, struct l2tp_session, hlist);
1302fd558d18SJames Chapman 
1303a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_CONTROL,
1304fd558d18SJames Chapman 				  "%s: closing session\n", session->name);
1305fd558d18SJames Chapman 
1306fd558d18SJames Chapman 			hlist_del_init(&session->hlist);
1307fd558d18SJames Chapman 
1308fd558d18SJames Chapman 			if (session->ref != NULL)
1309fd558d18SJames Chapman 				(*session->ref)(session);
1310fd558d18SJames Chapman 
1311fd558d18SJames Chapman 			write_unlock_bh(&tunnel->hlist_lock);
1312fd558d18SJames Chapman 
1313f6e16b29STom Parkin 			__l2tp_session_unhash(session);
13144c6e2fd3STom Parkin 			l2tp_session_queue_purge(session);
13154c6e2fd3STom Parkin 
1316fd558d18SJames Chapman 			if (session->session_close != NULL)
1317fd558d18SJames Chapman 				(*session->session_close)(session);
1318fd558d18SJames Chapman 
1319fd558d18SJames Chapman 			if (session->deref != NULL)
1320fd558d18SJames Chapman 				(*session->deref)(session);
1321fd558d18SJames Chapman 
13229980d001STom Parkin 			l2tp_session_dec_refcount(session);
13239980d001STom Parkin 
1324fd558d18SJames Chapman 			write_lock_bh(&tunnel->hlist_lock);
1325fd558d18SJames Chapman 
1326fd558d18SJames Chapman 			/* Now restart from the beginning of this hash
1327fd558d18SJames Chapman 			 * chain.  We always remove a session from the
1328fd558d18SJames Chapman 			 * list so we are guaranteed to make forward
1329fd558d18SJames Chapman 			 * progress.
1330fd558d18SJames Chapman 			 */
1331fd558d18SJames Chapman 			goto again;
1332fd558d18SJames Chapman 		}
1333fd558d18SJames Chapman 	}
1334fd558d18SJames Chapman 	write_unlock_bh(&tunnel->hlist_lock);
1335fd558d18SJames Chapman }
1336e34f4c70STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1337fd558d18SJames Chapman 
13389980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */
13399980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk)
13409980d001STom Parkin {
13419980d001STom Parkin 	struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
13429980d001STom Parkin 	if (tunnel) {
13439980d001STom Parkin 		l2tp_tunnel_closeall(tunnel);
13449980d001STom Parkin 		sock_put(sk);
13459980d001STom Parkin 	}
13469980d001STom Parkin }
13479980d001STom Parkin 
1348fd558d18SJames Chapman /* Really kill the tunnel.
1349fd558d18SJames Chapman  * Come here only when all sessions have been cleared from the tunnel.
1350fd558d18SJames Chapman  */
1351fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1352fd558d18SJames Chapman {
1353fd558d18SJames Chapman 	BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1354fd558d18SJames Chapman 	BUG_ON(tunnel->sock != NULL);
1355a4ca44faSJoe Perches 	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
135699469c32Sxeb@mail.ru 	kfree_rcu(tunnel, rcu);
1357f8ccac0eSTom Parkin }
1358fd558d18SJames Chapman 
1359f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */
1360f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work)
1361f8ccac0eSTom Parkin {
1362f8ccac0eSTom Parkin 	struct l2tp_tunnel *tunnel = NULL;
1363f8ccac0eSTom Parkin 	struct socket *sock = NULL;
1364f8ccac0eSTom Parkin 	struct sock *sk = NULL;
1365f8ccac0eSTom Parkin 
1366f8ccac0eSTom Parkin 	tunnel = container_of(work, struct l2tp_tunnel, del_work);
136712d656afSRidge Kennedy 
136812d656afSRidge Kennedy 	l2tp_tunnel_closeall(tunnel);
136912d656afSRidge Kennedy 
1370f8ccac0eSTom Parkin 	sk = l2tp_tunnel_sock_lookup(tunnel);
1371f8ccac0eSTom Parkin 	if (!sk)
137206a15f51SAlexander Couzens 		goto out;
1373f8ccac0eSTom Parkin 
1374f8ccac0eSTom Parkin 	sock = sk->sk_socket;
1375f8ccac0eSTom Parkin 
137602d13ed5STom Parkin 	/* If the tunnel socket was created by userspace, then go through the
137702d13ed5STom Parkin 	 * inet layer to shut the socket down, and let userspace close it.
137802d13ed5STom Parkin 	 * Otherwise, if we created the socket directly within the kernel, use
137902d13ed5STom Parkin 	 * the sk API to release it here.
1380167eb17eSTom Parkin 	 * In either case the tunnel resources are freed in the socket
1381167eb17eSTom Parkin 	 * destructor when the tunnel socket goes away.
1382f8ccac0eSTom Parkin 	 */
138302d13ed5STom Parkin 	if (tunnel->fd >= 0) {
138402d13ed5STom Parkin 		if (sock)
138502d13ed5STom Parkin 			inet_shutdown(sock, 2);
138602d13ed5STom Parkin 	} else {
138726abe143SEric W. Biederman 		if (sock) {
1388167eb17eSTom Parkin 			kernel_sock_shutdown(sock, SHUT_RDWR);
138926abe143SEric W. Biederman 			sock_release(sock);
139026abe143SEric W. Biederman 		}
1391167eb17eSTom Parkin 	}
1392f8ccac0eSTom Parkin 
1393f8ccac0eSTom Parkin 	l2tp_tunnel_sock_put(sk);
139406a15f51SAlexander Couzens out:
139506a15f51SAlexander Couzens 	l2tp_tunnel_dec_refcount(tunnel);
1396fd558d18SJames Chapman }
1397fd558d18SJames Chapman 
1398789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by
1399789a4a2cSJames Chapman  * userspace. This is used for static tunnels where there is no
1400789a4a2cSJames Chapman  * managing L2TP daemon.
1401167eb17eSTom Parkin  *
1402167eb17eSTom Parkin  * Since we don't want these sockets to keep a namespace alive by
1403167eb17eSTom Parkin  * themselves, we drop the socket's namespace refcount after creation.
1404167eb17eSTom Parkin  * These sockets are freed when the namespace exits using the pernet
1405167eb17eSTom Parkin  * exit hook.
1406789a4a2cSJames Chapman  */
1407167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net,
1408167eb17eSTom Parkin 				u32 tunnel_id,
1409167eb17eSTom Parkin 				u32 peer_tunnel_id,
1410167eb17eSTom Parkin 				struct l2tp_tunnel_cfg *cfg,
1411167eb17eSTom Parkin 				struct socket **sockp)
1412789a4a2cSJames Chapman {
1413789a4a2cSJames Chapman 	int err = -EINVAL;
14147bddd0dbSEric Dumazet 	struct socket *sock = NULL;
141585644b4dSTom Herbert 	struct udp_port_cfg udp_conf;
1416789a4a2cSJames Chapman 
1417789a4a2cSJames Chapman 	switch (cfg->encap) {
1418789a4a2cSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
141985644b4dSTom Herbert 		memset(&udp_conf, 0, sizeof(udp_conf));
142085644b4dSTom Herbert 
1421f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
1422f9bac8dfSChris Elston 		if (cfg->local_ip6 && cfg->peer_ip6) {
142385644b4dSTom Herbert 			udp_conf.family = AF_INET6;
142485644b4dSTom Herbert 			memcpy(&udp_conf.local_ip6, cfg->local_ip6,
142585644b4dSTom Herbert 			       sizeof(udp_conf.local_ip6));
142685644b4dSTom Herbert 			memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
142785644b4dSTom Herbert 			       sizeof(udp_conf.peer_ip6));
142885644b4dSTom Herbert 			udp_conf.use_udp6_tx_checksums =
1429018f8258SWang Shanker 			  ! cfg->udp6_zero_tx_checksums;
143085644b4dSTom Herbert 			udp_conf.use_udp6_rx_checksums =
1431018f8258SWang Shanker 			  ! cfg->udp6_zero_rx_checksums;
1432f9bac8dfSChris Elston 		} else
1433f9bac8dfSChris Elston #endif
1434f9bac8dfSChris Elston 		{
143585644b4dSTom Herbert 			udp_conf.family = AF_INET;
143685644b4dSTom Herbert 			udp_conf.local_ip = cfg->local_ip;
143785644b4dSTom Herbert 			udp_conf.peer_ip = cfg->peer_ip;
143885644b4dSTom Herbert 			udp_conf.use_udp_checksums = cfg->use_udp_checksums;
1439f9bac8dfSChris Elston 		}
1440789a4a2cSJames Chapman 
144185644b4dSTom Herbert 		udp_conf.local_udp_port = htons(cfg->local_udp_port);
144285644b4dSTom Herbert 		udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
144385644b4dSTom Herbert 
144485644b4dSTom Herbert 		err = udp_sock_create(net, &udp_conf, &sock);
144585644b4dSTom Herbert 		if (err < 0)
144685644b4dSTom Herbert 			goto out;
1447789a4a2cSJames Chapman 
1448789a4a2cSJames Chapman 		break;
1449789a4a2cSJames Chapman 
1450789a4a2cSJames Chapman 	case L2TP_ENCAPTYPE_IP:
1451f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
1452f9bac8dfSChris Elston 		if (cfg->local_ip6 && cfg->peer_ip6) {
145385644b4dSTom Herbert 			struct sockaddr_l2tpip6 ip6_addr = {0};
145485644b4dSTom Herbert 
145526abe143SEric W. Biederman 			err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
1456167eb17eSTom Parkin 					  IPPROTO_L2TP, &sock);
14575dac94e1SJames Chapman 			if (err < 0)
1458f9bac8dfSChris Elston 				goto out;
14595dac94e1SJames Chapman 
14605dac94e1SJames Chapman 			ip6_addr.l2tp_family = AF_INET6;
14615dac94e1SJames Chapman 			memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
14625dac94e1SJames Chapman 			       sizeof(ip6_addr.l2tp_addr));
14635dac94e1SJames Chapman 			ip6_addr.l2tp_conn_id = tunnel_id;
14645dac94e1SJames Chapman 			err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
14655dac94e1SJames Chapman 					  sizeof(ip6_addr));
14665dac94e1SJames Chapman 			if (err < 0)
14675dac94e1SJames Chapman 				goto out;
14685dac94e1SJames Chapman 
14695dac94e1SJames Chapman 			ip6_addr.l2tp_family = AF_INET6;
14705dac94e1SJames Chapman 			memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
14715dac94e1SJames Chapman 			       sizeof(ip6_addr.l2tp_addr));
14725dac94e1SJames Chapman 			ip6_addr.l2tp_conn_id = peer_tunnel_id;
14735dac94e1SJames Chapman 			err = kernel_connect(sock,
14745dac94e1SJames Chapman 					     (struct sockaddr *) &ip6_addr,
14755dac94e1SJames Chapman 					     sizeof(ip6_addr), 0);
14765dac94e1SJames Chapman 			if (err < 0)
14775dac94e1SJames Chapman 				goto out;
14785dac94e1SJames Chapman 		} else
1479f9bac8dfSChris Elston #endif
14805dac94e1SJames Chapman 		{
148185644b4dSTom Herbert 			struct sockaddr_l2tpip ip_addr = {0};
148285644b4dSTom Herbert 
148326abe143SEric W. Biederman 			err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
1484167eb17eSTom Parkin 					  IPPROTO_L2TP, &sock);
1485789a4a2cSJames Chapman 			if (err < 0)
1486789a4a2cSJames Chapman 				goto out;
1487789a4a2cSJames Chapman 
1488789a4a2cSJames Chapman 			ip_addr.l2tp_family = AF_INET;
1489789a4a2cSJames Chapman 			ip_addr.l2tp_addr = cfg->local_ip;
1490789a4a2cSJames Chapman 			ip_addr.l2tp_conn_id = tunnel_id;
14915dac94e1SJames Chapman 			err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
14925dac94e1SJames Chapman 					  sizeof(ip_addr));
1493789a4a2cSJames Chapman 			if (err < 0)
1494789a4a2cSJames Chapman 				goto out;
1495789a4a2cSJames Chapman 
1496789a4a2cSJames Chapman 			ip_addr.l2tp_family = AF_INET;
1497789a4a2cSJames Chapman 			ip_addr.l2tp_addr = cfg->peer_ip;
1498789a4a2cSJames Chapman 			ip_addr.l2tp_conn_id = peer_tunnel_id;
14995dac94e1SJames Chapman 			err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
15005dac94e1SJames Chapman 					     sizeof(ip_addr), 0);
1501789a4a2cSJames Chapman 			if (err < 0)
1502789a4a2cSJames Chapman 				goto out;
15035dac94e1SJames Chapman 		}
1504789a4a2cSJames Chapman 		break;
1505789a4a2cSJames Chapman 
1506789a4a2cSJames Chapman 	default:
1507789a4a2cSJames Chapman 		goto out;
1508789a4a2cSJames Chapman 	}
1509789a4a2cSJames Chapman 
1510789a4a2cSJames Chapman out:
1511167eb17eSTom Parkin 	*sockp = sock;
1512789a4a2cSJames Chapman 	if ((err < 0) && sock) {
1513167eb17eSTom Parkin 		kernel_sock_shutdown(sock, SHUT_RDWR);
151426abe143SEric W. Biederman 		sock_release(sock);
1515789a4a2cSJames Chapman 		*sockp = NULL;
1516789a4a2cSJames Chapman 	}
1517789a4a2cSJames Chapman 
1518789a4a2cSJames Chapman 	return err;
1519789a4a2cSJames Chapman }
1520789a4a2cSJames Chapman 
152137159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class;
152237159ef2SEric Dumazet 
1523fd558d18SJames Chapman int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
1524fd558d18SJames Chapman {
1525fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = NULL;
1526fd558d18SJames Chapman 	int err;
1527fd558d18SJames Chapman 	struct socket *sock = NULL;
1528fd558d18SJames Chapman 	struct sock *sk = NULL;
1529fd558d18SJames Chapman 	struct l2tp_net *pn;
15300d76751fSJames Chapman 	enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1531fd558d18SJames Chapman 
1532fd558d18SJames Chapman 	/* Get the tunnel socket from the fd, which was opened by
1533789a4a2cSJames Chapman 	 * the userspace L2TP daemon. If not specified, create a
1534789a4a2cSJames Chapman 	 * kernel socket.
1535fd558d18SJames Chapman 	 */
1536789a4a2cSJames Chapman 	if (fd < 0) {
1537167eb17eSTom Parkin 		err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1538167eb17eSTom Parkin 				cfg, &sock);
1539789a4a2cSJames Chapman 		if (err < 0)
1540789a4a2cSJames Chapman 			goto err;
1541789a4a2cSJames Chapman 	} else {
1542fd558d18SJames Chapman 		sock = sockfd_lookup(fd, &err);
1543fd558d18SJames Chapman 		if (!sock) {
1544cbb95e0cSTom Parkin 			pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1545fd558d18SJames Chapman 			       tunnel_id, fd, err);
1546cbb95e0cSTom Parkin 			err = -EBADF;
1547cbb95e0cSTom Parkin 			goto err;
1548cbb95e0cSTom Parkin 		}
1549cbb95e0cSTom Parkin 
1550cbb95e0cSTom Parkin 		/* Reject namespace mismatches */
1551cbb95e0cSTom Parkin 		if (!net_eq(sock_net(sock->sk), net)) {
1552cbb95e0cSTom Parkin 			pr_err("tunl %u: netns mismatch\n", tunnel_id);
1553cbb95e0cSTom Parkin 			err = -EINVAL;
1554fd558d18SJames Chapman 			goto err;
1555fd558d18SJames Chapman 		}
1556789a4a2cSJames Chapman 	}
1557fd558d18SJames Chapman 
1558fd558d18SJames Chapman 	sk = sock->sk;
1559fd558d18SJames Chapman 
15600d76751fSJames Chapman 	if (cfg != NULL)
15610d76751fSJames Chapman 		encap = cfg->encap;
15620d76751fSJames Chapman 
1563fd558d18SJames Chapman 	/* Quick sanity checks */
15640d76751fSJames Chapman 	switch (encap) {
15650d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1566fd558d18SJames Chapman 		err = -EPROTONOSUPPORT;
1567fd558d18SJames Chapman 		if (sk->sk_protocol != IPPROTO_UDP) {
1568a4ca44faSJoe Perches 			pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1569fd558d18SJames Chapman 			       tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1570fd558d18SJames Chapman 			goto err;
1571fd558d18SJames Chapman 		}
15720d76751fSJames Chapman 		break;
15730d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
15740d76751fSJames Chapman 		err = -EPROTONOSUPPORT;
15750d76751fSJames Chapman 		if (sk->sk_protocol != IPPROTO_L2TP) {
1576a4ca44faSJoe Perches 			pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
15770d76751fSJames Chapman 			       tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1578fd558d18SJames Chapman 			goto err;
1579fd558d18SJames Chapman 		}
15800d76751fSJames Chapman 		break;
15810d76751fSJames Chapman 	}
1582fd558d18SJames Chapman 
1583fd558d18SJames Chapman 	/* Check if this socket has already been prepped */
15848d8a51e2SDavid S. Miller 	tunnel = l2tp_tunnel(sk);
1585fd558d18SJames Chapman 	if (tunnel != NULL) {
1586fd558d18SJames Chapman 		/* This socket has already been prepped */
1587fd558d18SJames Chapman 		err = -EBUSY;
1588fd558d18SJames Chapman 		goto err;
1589fd558d18SJames Chapman 	}
1590fd558d18SJames Chapman 
1591fd558d18SJames Chapman 	tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1592fd558d18SJames Chapman 	if (tunnel == NULL) {
1593fd558d18SJames Chapman 		err = -ENOMEM;
1594fd558d18SJames Chapman 		goto err;
1595fd558d18SJames Chapman 	}
1596fd558d18SJames Chapman 
1597fd558d18SJames Chapman 	tunnel->version = version;
1598fd558d18SJames Chapman 	tunnel->tunnel_id = tunnel_id;
1599fd558d18SJames Chapman 	tunnel->peer_tunnel_id = peer_tunnel_id;
1600fd558d18SJames Chapman 	tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1601fd558d18SJames Chapman 
1602fd558d18SJames Chapman 	tunnel->magic = L2TP_TUNNEL_MAGIC;
1603fd558d18SJames Chapman 	sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1604fd558d18SJames Chapman 	rwlock_init(&tunnel->hlist_lock);
1605fd558d18SJames Chapman 
1606fd558d18SJames Chapman 	/* The net we belong to */
1607fd558d18SJames Chapman 	tunnel->l2tp_net = net;
1608fd558d18SJames Chapman 	pn = l2tp_pernet(net);
1609fd558d18SJames Chapman 
16100d76751fSJames Chapman 	if (cfg != NULL)
1611fd558d18SJames Chapman 		tunnel->debug = cfg->debug;
1612fd558d18SJames Chapman 
1613e18503f4SFrançois Cachereul #if IS_ENABLED(CONFIG_IPV6)
1614e18503f4SFrançois Cachereul 	if (sk->sk_family == PF_INET6) {
1615e18503f4SFrançois Cachereul 		struct ipv6_pinfo *np = inet6_sk(sk);
1616e18503f4SFrançois Cachereul 
1617e18503f4SFrançois Cachereul 		if (ipv6_addr_v4mapped(&np->saddr) &&
1618efe4208fSEric Dumazet 		    ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
1619e18503f4SFrançois Cachereul 			struct inet_sock *inet = inet_sk(sk);
1620e18503f4SFrançois Cachereul 
1621e18503f4SFrançois Cachereul 			tunnel->v4mapped = true;
1622e18503f4SFrançois Cachereul 			inet->inet_saddr = np->saddr.s6_addr32[3];
1623efe4208fSEric Dumazet 			inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1624efe4208fSEric Dumazet 			inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
1625e18503f4SFrançois Cachereul 		} else {
1626e18503f4SFrançois Cachereul 			tunnel->v4mapped = false;
1627e18503f4SFrançois Cachereul 		}
1628e18503f4SFrançois Cachereul 	}
1629e18503f4SFrançois Cachereul #endif
1630e18503f4SFrançois Cachereul 
1631fd558d18SJames Chapman 	/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
16320d76751fSJames Chapman 	tunnel->encap = encap;
16330d76751fSJames Chapman 	if (encap == L2TP_ENCAPTYPE_UDP) {
1634a5c5e2daSGuillaume Nault 		struct udp_tunnel_sock_cfg udp_cfg = { };
1635fd558d18SJames Chapman 
1636c8fffceaSAndy Zhou 		udp_cfg.sk_user_data = tunnel;
1637c8fffceaSAndy Zhou 		udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1638c8fffceaSAndy Zhou 		udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1639c8fffceaSAndy Zhou 		udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1640c8fffceaSAndy Zhou 
1641c8fffceaSAndy Zhou 		setup_udp_tunnel_sock(net, sock, &udp_cfg);
1642c8fffceaSAndy Zhou 	} else {
1643fd558d18SJames Chapman 		sk->sk_user_data = tunnel;
1644c8fffceaSAndy Zhou 	}
1645fd558d18SJames Chapman 
1646fd558d18SJames Chapman 	/* Hook on the tunnel socket destructor so that we can cleanup
1647fd558d18SJames Chapman 	 * if the tunnel socket goes away.
1648fd558d18SJames Chapman 	 */
1649fd558d18SJames Chapman 	tunnel->old_sk_destruct = sk->sk_destruct;
1650fd558d18SJames Chapman 	sk->sk_destruct = &l2tp_tunnel_destruct;
1651fd558d18SJames Chapman 	tunnel->sock = sk;
165280d84ef3STom Parkin 	tunnel->fd = fd;
165337159ef2SEric Dumazet 	lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
165437159ef2SEric Dumazet 
1655fd558d18SJames Chapman 	sk->sk_allocation = GFP_ATOMIC;
1656fd558d18SJames Chapman 
1657f8ccac0eSTom Parkin 	/* Init delete workqueue struct */
1658f8ccac0eSTom Parkin 	INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1659f8ccac0eSTom Parkin 
1660fd558d18SJames Chapman 	/* Add tunnel to our list */
1661fd558d18SJames Chapman 	INIT_LIST_HEAD(&tunnel->list);
1662fd558d18SJames Chapman 	atomic_inc(&l2tp_tunnel_count);
1663fd558d18SJames Chapman 
1664fd558d18SJames Chapman 	/* Bump the reference count. The tunnel context is deleted
16651769192aSEric Dumazet 	 * only when this drops to zero. Must be done before list insertion
1666fd558d18SJames Chapman 	 */
1667fd558d18SJames Chapman 	l2tp_tunnel_inc_refcount(tunnel);
16681769192aSEric Dumazet 	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
16691769192aSEric Dumazet 	list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
16701769192aSEric Dumazet 	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1671fd558d18SJames Chapman 
1672fd558d18SJames Chapman 	err = 0;
1673fd558d18SJames Chapman err:
1674fd558d18SJames Chapman 	if (tunnelp)
1675fd558d18SJames Chapman 		*tunnelp = tunnel;
1676fd558d18SJames Chapman 
1677789a4a2cSJames Chapman 	/* If tunnel's socket was created by the kernel, it doesn't
1678789a4a2cSJames Chapman 	 *  have a file.
1679789a4a2cSJames Chapman 	 */
1680789a4a2cSJames Chapman 	if (sock && sock->file)
1681fd558d18SJames Chapman 		sockfd_put(sock);
1682fd558d18SJames Chapman 
1683fd558d18SJames Chapman 	return err;
1684fd558d18SJames Chapman }
1685fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1686fd558d18SJames Chapman 
1687309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command.
1688309795f4SJames Chapman  */
1689309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1690309795f4SJames Chapman {
169106a15f51SAlexander Couzens 	l2tp_tunnel_inc_refcount(tunnel);
169206a15f51SAlexander Couzens 	if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
169306a15f51SAlexander Couzens 		l2tp_tunnel_dec_refcount(tunnel);
169406a15f51SAlexander Couzens 		return 1;
169506a15f51SAlexander Couzens 	}
169606a15f51SAlexander Couzens 	return 0;
1697309795f4SJames Chapman }
1698309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1699309795f4SJames Chapman 
1700fd558d18SJames Chapman /* Really kill the session.
1701fd558d18SJames Chapman  */
1702fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session)
1703fd558d18SJames Chapman {
1704f6e16b29STom Parkin 	struct l2tp_tunnel *tunnel = session->tunnel;
1705fd558d18SJames Chapman 
1706fd558d18SJames Chapman 	BUG_ON(atomic_read(&session->ref_count) != 0);
1707fd558d18SJames Chapman 
1708f6e16b29STom Parkin 	if (tunnel) {
1709fd558d18SJames Chapman 		BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1710fd558d18SJames Chapman 		if (session->session_id != 0)
1711fd558d18SJames Chapman 			atomic_dec(&l2tp_session_count);
1712fd558d18SJames Chapman 		sock_put(tunnel->sock);
1713fd558d18SJames Chapman 		session->tunnel = NULL;
1714fd558d18SJames Chapman 		l2tp_tunnel_dec_refcount(tunnel);
1715fd558d18SJames Chapman 	}
1716fd558d18SJames Chapman 
1717fd558d18SJames Chapman 	kfree(session);
1718fd558d18SJames Chapman }
1719fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free);
1720fd558d18SJames Chapman 
1721f6e16b29STom Parkin /* Remove an l2tp session from l2tp_core's hash lists.
1722f6e16b29STom Parkin  * Provides a tidyup interface for pseudowire code which can't just route all
1723f6e16b29STom Parkin  * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1724f6e16b29STom Parkin  * callback.
1725f6e16b29STom Parkin  */
1726f6e16b29STom Parkin void __l2tp_session_unhash(struct l2tp_session *session)
1727f6e16b29STom Parkin {
1728f6e16b29STom Parkin 	struct l2tp_tunnel *tunnel = session->tunnel;
1729f6e16b29STom Parkin 
1730f6e16b29STom Parkin 	/* Remove the session from core hashes */
1731f6e16b29STom Parkin 	if (tunnel) {
1732f6e16b29STom Parkin 		/* Remove from the per-tunnel hash */
1733f6e16b29STom Parkin 		write_lock_bh(&tunnel->hlist_lock);
1734f6e16b29STom Parkin 		hlist_del_init(&session->hlist);
1735f6e16b29STom Parkin 		write_unlock_bh(&tunnel->hlist_lock);
1736f6e16b29STom Parkin 
1737f6e16b29STom Parkin 		/* For L2TPv3 we have a per-net hash: remove from there, too */
1738f6e16b29STom Parkin 		if (tunnel->version != L2TP_HDR_VER_2) {
1739f6e16b29STom Parkin 			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1740f6e16b29STom Parkin 			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1741f6e16b29STom Parkin 			hlist_del_init_rcu(&session->global_hlist);
1742f6e16b29STom Parkin 			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1743f6e16b29STom Parkin 			synchronize_rcu();
1744f6e16b29STom Parkin 		}
1745f6e16b29STom Parkin 	}
1746f6e16b29STom Parkin }
1747f6e16b29STom Parkin EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1748f6e16b29STom Parkin 
1749309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by
1750309795f4SJames Chapman    pseudowire modules.
1751309795f4SJames Chapman  */
1752309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session)
1753309795f4SJames Chapman {
1754f6e16b29STom Parkin 	if (session->ref)
1755f6e16b29STom Parkin 		(*session->ref)(session);
1756f6e16b29STom Parkin 	__l2tp_session_unhash(session);
17574c6e2fd3STom Parkin 	l2tp_session_queue_purge(session);
1758309795f4SJames Chapman 	if (session->session_close != NULL)
1759309795f4SJames Chapman 		(*session->session_close)(session);
1760f6e16b29STom Parkin 	if (session->deref)
17611b7c92b9SDan Carpenter 		(*session->deref)(session);
1762309795f4SJames Chapman 	l2tp_session_dec_refcount(session);
1763309795f4SJames Chapman 	return 0;
1764309795f4SJames Chapman }
1765309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete);
1766309795f4SJames Chapman 
1767f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or
1768f7faffa3SJames Chapman  * l2specific_len parameters are set.
1769f7faffa3SJames Chapman  */
1770bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1771f7faffa3SJames Chapman {
1772f7faffa3SJames Chapman 	if (version == L2TP_HDR_VER_2) {
1773f7faffa3SJames Chapman 		session->hdr_len = 6;
1774f7faffa3SJames Chapman 		if (session->send_seq)
1775f7faffa3SJames Chapman 			session->hdr_len += 4;
1776f7faffa3SJames Chapman 	} else {
17770d76751fSJames Chapman 		session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
17780d76751fSJames Chapman 		if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
17790d76751fSJames Chapman 			session->hdr_len += 4;
1780f7faffa3SJames Chapman 	}
1781f7faffa3SJames Chapman 
1782f7faffa3SJames Chapman }
1783bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1784f7faffa3SJames Chapman 
1785fd558d18SJames Chapman struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1786fd558d18SJames Chapman {
1787fd558d18SJames Chapman 	struct l2tp_session *session;
1788fd558d18SJames Chapman 
1789fd558d18SJames Chapman 	session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1790fd558d18SJames Chapman 	if (session != NULL) {
1791fd558d18SJames Chapman 		session->magic = L2TP_SESSION_MAGIC;
1792fd558d18SJames Chapman 		session->tunnel = tunnel;
1793fd558d18SJames Chapman 
1794fd558d18SJames Chapman 		session->session_id = session_id;
1795fd558d18SJames Chapman 		session->peer_session_id = peer_session_id;
1796d301e325SJames Chapman 		session->nr = 0;
17978a1631d5SJames Chapman 		if (tunnel->version == L2TP_HDR_VER_2)
17988a1631d5SJames Chapman 			session->nr_max = 0xffff;
17998a1631d5SJames Chapman 		else
18008a1631d5SJames Chapman 			session->nr_max = 0xffffff;
18018a1631d5SJames Chapman 		session->nr_window_size = session->nr_max / 2;
1802a0dbd822SJames Chapman 		session->nr_oos_count_max = 4;
1803a0dbd822SJames Chapman 
1804a0dbd822SJames Chapman 		/* Use NR of first received packet */
1805a0dbd822SJames Chapman 		session->reorder_skip = 1;
1806fd558d18SJames Chapman 
1807fd558d18SJames Chapman 		sprintf(&session->name[0], "sess %u/%u",
1808fd558d18SJames Chapman 			tunnel->tunnel_id, session->session_id);
1809fd558d18SJames Chapman 
1810fd558d18SJames Chapman 		skb_queue_head_init(&session->reorder_q);
1811fd558d18SJames Chapman 
1812fd558d18SJames Chapman 		INIT_HLIST_NODE(&session->hlist);
1813f7faffa3SJames Chapman 		INIT_HLIST_NODE(&session->global_hlist);
1814fd558d18SJames Chapman 
1815fd558d18SJames Chapman 		/* Inherit debug options from tunnel */
1816fd558d18SJames Chapman 		session->debug = tunnel->debug;
1817fd558d18SJames Chapman 
1818fd558d18SJames Chapman 		if (cfg) {
1819f7faffa3SJames Chapman 			session->pwtype = cfg->pw_type;
1820fd558d18SJames Chapman 			session->debug = cfg->debug;
1821fd558d18SJames Chapman 			session->mtu = cfg->mtu;
1822fd558d18SJames Chapman 			session->mru = cfg->mru;
1823fd558d18SJames Chapman 			session->send_seq = cfg->send_seq;
1824fd558d18SJames Chapman 			session->recv_seq = cfg->recv_seq;
1825fd558d18SJames Chapman 			session->lns_mode = cfg->lns_mode;
1826f7faffa3SJames Chapman 			session->reorder_timeout = cfg->reorder_timeout;
1827f7faffa3SJames Chapman 			session->offset = cfg->offset;
1828f7faffa3SJames Chapman 			session->l2specific_type = cfg->l2specific_type;
1829f7faffa3SJames Chapman 			session->l2specific_len = cfg->l2specific_len;
1830f7faffa3SJames Chapman 			session->cookie_len = cfg->cookie_len;
1831f7faffa3SJames Chapman 			memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1832f7faffa3SJames Chapman 			session->peer_cookie_len = cfg->peer_cookie_len;
1833f7faffa3SJames Chapman 			memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1834fd558d18SJames Chapman 		}
1835fd558d18SJames Chapman 
1836f7faffa3SJames Chapman 		if (tunnel->version == L2TP_HDR_VER_2)
1837f7faffa3SJames Chapman 			session->build_header = l2tp_build_l2tpv2_header;
1838f7faffa3SJames Chapman 		else
1839f7faffa3SJames Chapman 			session->build_header = l2tp_build_l2tpv3_header;
1840f7faffa3SJames Chapman 
1841f7faffa3SJames Chapman 		l2tp_session_set_header_len(session, tunnel->version);
1842f7faffa3SJames Chapman 
1843fd558d18SJames Chapman 		/* Bump the reference count. The session context is deleted
1844fd558d18SJames Chapman 		 * only when this drops to zero.
1845fd558d18SJames Chapman 		 */
1846fd558d18SJames Chapman 		l2tp_session_inc_refcount(session);
1847fd558d18SJames Chapman 		l2tp_tunnel_inc_refcount(tunnel);
1848fd558d18SJames Chapman 
1849fd558d18SJames Chapman 		/* Ensure tunnel socket isn't deleted */
1850fd558d18SJames Chapman 		sock_hold(tunnel->sock);
1851fd558d18SJames Chapman 
1852fd558d18SJames Chapman 		/* Add session to the tunnel's hash list */
1853fd558d18SJames Chapman 		write_lock_bh(&tunnel->hlist_lock);
1854fd558d18SJames Chapman 		hlist_add_head(&session->hlist,
1855fd558d18SJames Chapman 			       l2tp_session_id_hash(tunnel, session_id));
1856fd558d18SJames Chapman 		write_unlock_bh(&tunnel->hlist_lock);
1857fd558d18SJames Chapman 
1858f7faffa3SJames Chapman 		/* And to the global session list if L2TPv3 */
1859f7faffa3SJames Chapman 		if (tunnel->version != L2TP_HDR_VER_2) {
1860f7faffa3SJames Chapman 			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1861f7faffa3SJames Chapman 
1862e02d494dSJames Chapman 			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1863e02d494dSJames Chapman 			hlist_add_head_rcu(&session->global_hlist,
1864f7faffa3SJames Chapman 					   l2tp_session_id_hash_2(pn, session_id));
1865e02d494dSJames Chapman 			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1866f7faffa3SJames Chapman 		}
1867f7faffa3SJames Chapman 
1868fd558d18SJames Chapman 		/* Ignore management session in session count value */
1869fd558d18SJames Chapman 		if (session->session_id != 0)
1870fd558d18SJames Chapman 			atomic_inc(&l2tp_session_count);
1871fd558d18SJames Chapman 	}
1872fd558d18SJames Chapman 
1873fd558d18SJames Chapman 	return session;
1874fd558d18SJames Chapman }
1875fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create);
1876fd558d18SJames Chapman 
1877fd558d18SJames Chapman /*****************************************************************************
1878fd558d18SJames Chapman  * Init and cleanup
1879fd558d18SJames Chapman  *****************************************************************************/
1880fd558d18SJames Chapman 
1881fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net)
1882fd558d18SJames Chapman {
1883e773aaffSJiri Pirko 	struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1884f7faffa3SJames Chapman 	int hash;
1885fd558d18SJames Chapman 
1886fd558d18SJames Chapman 	INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1887e02d494dSJames Chapman 	spin_lock_init(&pn->l2tp_tunnel_list_lock);
1888fd558d18SJames Chapman 
1889f7faffa3SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1890f7faffa3SJames Chapman 		INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1891f7faffa3SJames Chapman 
1892e02d494dSJames Chapman 	spin_lock_init(&pn->l2tp_session_hlist_lock);
1893f7faffa3SJames Chapman 
1894fd558d18SJames Chapman 	return 0;
1895fd558d18SJames Chapman }
1896fd558d18SJames Chapman 
1897167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net)
1898167eb17eSTom Parkin {
1899167eb17eSTom Parkin 	struct l2tp_net *pn = l2tp_pernet(net);
1900167eb17eSTom Parkin 	struct l2tp_tunnel *tunnel = NULL;
1901167eb17eSTom Parkin 
1902167eb17eSTom Parkin 	rcu_read_lock_bh();
1903167eb17eSTom Parkin 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1904167eb17eSTom Parkin 		(void)l2tp_tunnel_delete(tunnel);
1905167eb17eSTom Parkin 	}
1906167eb17eSTom Parkin 	rcu_read_unlock_bh();
19072f86953eSSabrina Dubroca 
19082f86953eSSabrina Dubroca 	flush_workqueue(l2tp_wq);
19092f86953eSSabrina Dubroca 	rcu_barrier();
1910167eb17eSTom Parkin }
1911167eb17eSTom Parkin 
1912fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = {
1913fd558d18SJames Chapman 	.init = l2tp_init_net,
1914167eb17eSTom Parkin 	.exit = l2tp_exit_net,
1915fd558d18SJames Chapman 	.id   = &l2tp_net_id,
1916fd558d18SJames Chapman 	.size = sizeof(struct l2tp_net),
1917fd558d18SJames Chapman };
1918fd558d18SJames Chapman 
1919fd558d18SJames Chapman static int __init l2tp_init(void)
1920fd558d18SJames Chapman {
1921fd558d18SJames Chapman 	int rc = 0;
1922fd558d18SJames Chapman 
1923fd558d18SJames Chapman 	rc = register_pernet_device(&l2tp_net_ops);
1924fd558d18SJames Chapman 	if (rc)
1925fd558d18SJames Chapman 		goto out;
1926fd558d18SJames Chapman 
192759ff3eb6SZhangZhen 	l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1928f8ccac0eSTom Parkin 	if (!l2tp_wq) {
1929f8ccac0eSTom Parkin 		pr_err("alloc_workqueue failed\n");
193067e04c29SWANG Cong 		unregister_pernet_device(&l2tp_net_ops);
1931f8ccac0eSTom Parkin 		rc = -ENOMEM;
1932f8ccac0eSTom Parkin 		goto out;
1933f8ccac0eSTom Parkin 	}
1934f8ccac0eSTom Parkin 
1935a4ca44faSJoe Perches 	pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1936fd558d18SJames Chapman 
1937fd558d18SJames Chapman out:
1938fd558d18SJames Chapman 	return rc;
1939fd558d18SJames Chapman }
1940fd558d18SJames Chapman 
1941fd558d18SJames Chapman static void __exit l2tp_exit(void)
1942fd558d18SJames Chapman {
1943fd558d18SJames Chapman 	unregister_pernet_device(&l2tp_net_ops);
1944f8ccac0eSTom Parkin 	if (l2tp_wq) {
1945f8ccac0eSTom Parkin 		destroy_workqueue(l2tp_wq);
1946f8ccac0eSTom Parkin 		l2tp_wq = NULL;
1947f8ccac0eSTom Parkin 	}
1948fd558d18SJames Chapman }
1949fd558d18SJames Chapman 
1950fd558d18SJames Chapman module_init(l2tp_init);
1951fd558d18SJames Chapman module_exit(l2tp_exit);
1952fd558d18SJames Chapman 
1953fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1954fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core");
1955fd558d18SJames Chapman MODULE_LICENSE("GPL");
1956fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION);
1957fd558d18SJames Chapman 
1958