xref: /openbmc/linux/net/l2tp/l2tp_core.c (revision 54652eb1)
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 
1178d8a51e2SDavid S. Miller static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
1188d8a51e2SDavid S. Miller {
1198d8a51e2SDavid S. Miller 	return sk->sk_user_data;
1208d8a51e2SDavid S. Miller }
1218d8a51e2SDavid S. Miller 
1229aaef50cSGuillaume Nault static inline struct l2tp_net *l2tp_pernet(const struct net *net)
123fd558d18SJames Chapman {
124fd558d18SJames Chapman 	BUG_ON(!net);
125fd558d18SJames Chapman 
126fd558d18SJames Chapman 	return net_generic(net, l2tp_net_id);
127fd558d18SJames Chapman }
128fd558d18SJames Chapman 
129f7faffa3SJames Chapman /* Session hash global list for L2TPv3.
130f7faffa3SJames Chapman  * The session_id SHOULD be random according to RFC3931, but several
131f7faffa3SJames Chapman  * L2TP implementations use incrementing session_ids.  So we do a real
132f7faffa3SJames Chapman  * hash on the session_id, rather than a simple bitmask.
133f7faffa3SJames Chapman  */
134f7faffa3SJames Chapman static inline struct hlist_head *
135f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
136f7faffa3SJames Chapman {
137f7faffa3SJames Chapman 	return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
138f7faffa3SJames Chapman 
139f7faffa3SJames Chapman }
140f7faffa3SJames Chapman 
14180d84ef3STom Parkin /* Lookup the tunnel socket, possibly involving the fs code if the socket is
14280d84ef3STom Parkin  * owned by userspace.  A struct sock returned from this function must be
14380d84ef3STom Parkin  * released using l2tp_tunnel_sock_put once you're done with it.
14480d84ef3STom Parkin  */
145b5d2b285Sstephen hemminger static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
14680d84ef3STom Parkin {
14780d84ef3STom Parkin 	int err = 0;
14880d84ef3STom Parkin 	struct socket *sock = NULL;
14980d84ef3STom Parkin 	struct sock *sk = NULL;
15080d84ef3STom Parkin 
15180d84ef3STom Parkin 	if (!tunnel)
15280d84ef3STom Parkin 		goto out;
15380d84ef3STom Parkin 
15480d84ef3STom Parkin 	if (tunnel->fd >= 0) {
15580d84ef3STom Parkin 		/* Socket is owned by userspace, who might be in the process
15680d84ef3STom Parkin 		 * of closing it.  Look the socket up using the fd to ensure
15780d84ef3STom Parkin 		 * consistency.
15880d84ef3STom Parkin 		 */
15980d84ef3STom Parkin 		sock = sockfd_lookup(tunnel->fd, &err);
16080d84ef3STom Parkin 		if (sock)
16180d84ef3STom Parkin 			sk = sock->sk;
16280d84ef3STom Parkin 	} else {
16380d84ef3STom Parkin 		/* Socket is owned by kernelspace */
16480d84ef3STom Parkin 		sk = tunnel->sock;
1658abbbe8fSTom Parkin 		sock_hold(sk);
16680d84ef3STom Parkin 	}
16780d84ef3STom Parkin 
16880d84ef3STom Parkin out:
16980d84ef3STom Parkin 	return sk;
17080d84ef3STom Parkin }
17180d84ef3STom Parkin 
17280d84ef3STom Parkin /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
173b5d2b285Sstephen hemminger static void l2tp_tunnel_sock_put(struct sock *sk)
17480d84ef3STom Parkin {
17580d84ef3STom Parkin 	struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
17680d84ef3STom Parkin 	if (tunnel) {
17780d84ef3STom Parkin 		if (tunnel->fd >= 0) {
17880d84ef3STom Parkin 			/* Socket is owned by userspace */
17980d84ef3STom Parkin 			sockfd_put(sk->sk_socket);
18080d84ef3STom Parkin 		}
18180d84ef3STom Parkin 		sock_put(sk);
18280d84ef3STom Parkin 	}
1838abbbe8fSTom Parkin 	sock_put(sk);
18480d84ef3STom Parkin }
18580d84ef3STom Parkin 
186fd558d18SJames Chapman /* Session hash list.
187fd558d18SJames Chapman  * The session_id SHOULD be random according to RFC2661, but several
188fd558d18SJames Chapman  * L2TP implementations (Cisco and Microsoft) use incrementing
189fd558d18SJames Chapman  * session_ids.  So we do a real hash on the session_id, rather than a
190fd558d18SJames Chapman  * simple bitmask.
191fd558d18SJames Chapman  */
192fd558d18SJames Chapman static inline struct hlist_head *
193fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
194fd558d18SJames Chapman {
195fd558d18SJames Chapman 	return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
196fd558d18SJames Chapman }
197fd558d18SJames Chapman 
19854652eb1SGuillaume Nault /* Lookup a tunnel. A new reference is held on the returned tunnel. */
19954652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
20054652eb1SGuillaume Nault {
20154652eb1SGuillaume Nault 	const struct l2tp_net *pn = l2tp_pernet(net);
20254652eb1SGuillaume Nault 	struct l2tp_tunnel *tunnel;
20354652eb1SGuillaume Nault 
20454652eb1SGuillaume Nault 	rcu_read_lock_bh();
20554652eb1SGuillaume Nault 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
20654652eb1SGuillaume Nault 		if (tunnel->tunnel_id == tunnel_id) {
20754652eb1SGuillaume Nault 			l2tp_tunnel_inc_refcount(tunnel);
20854652eb1SGuillaume Nault 			rcu_read_unlock_bh();
20954652eb1SGuillaume Nault 
21054652eb1SGuillaume Nault 			return tunnel;
21154652eb1SGuillaume Nault 		}
21254652eb1SGuillaume Nault 	}
21354652eb1SGuillaume Nault 	rcu_read_unlock_bh();
21454652eb1SGuillaume Nault 
21554652eb1SGuillaume Nault 	return NULL;
21654652eb1SGuillaume Nault }
21754652eb1SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
21854652eb1SGuillaume Nault 
21955a3ce3bSGuillaume Nault /* Lookup a session. A new reference is held on the returned session.
22061b9a047SGuillaume Nault  * Optionally calls session->ref() too if do_ref is true.
22161b9a047SGuillaume Nault  */
2229aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net,
22361b9a047SGuillaume Nault 				      struct l2tp_tunnel *tunnel,
22461b9a047SGuillaume Nault 				      u32 session_id, bool do_ref)
22561b9a047SGuillaume Nault {
22661b9a047SGuillaume Nault 	struct hlist_head *session_list;
22761b9a047SGuillaume Nault 	struct l2tp_session *session;
22861b9a047SGuillaume Nault 
22961b9a047SGuillaume Nault 	if (!tunnel) {
23061b9a047SGuillaume Nault 		struct l2tp_net *pn = l2tp_pernet(net);
23161b9a047SGuillaume Nault 
23261b9a047SGuillaume Nault 		session_list = l2tp_session_id_hash_2(pn, session_id);
23361b9a047SGuillaume Nault 
23461b9a047SGuillaume Nault 		rcu_read_lock_bh();
23561b9a047SGuillaume Nault 		hlist_for_each_entry_rcu(session, session_list, global_hlist) {
23661b9a047SGuillaume Nault 			if (session->session_id == session_id) {
23761b9a047SGuillaume Nault 				l2tp_session_inc_refcount(session);
23861b9a047SGuillaume Nault 				if (do_ref && session->ref)
23961b9a047SGuillaume Nault 					session->ref(session);
24061b9a047SGuillaume Nault 				rcu_read_unlock_bh();
24161b9a047SGuillaume Nault 
24261b9a047SGuillaume Nault 				return session;
24361b9a047SGuillaume Nault 			}
24461b9a047SGuillaume Nault 		}
24561b9a047SGuillaume Nault 		rcu_read_unlock_bh();
24661b9a047SGuillaume Nault 
24761b9a047SGuillaume Nault 		return NULL;
24861b9a047SGuillaume Nault 	}
24961b9a047SGuillaume Nault 
25061b9a047SGuillaume Nault 	session_list = l2tp_session_id_hash(tunnel, session_id);
25161b9a047SGuillaume Nault 	read_lock_bh(&tunnel->hlist_lock);
25261b9a047SGuillaume Nault 	hlist_for_each_entry(session, session_list, hlist) {
25361b9a047SGuillaume Nault 		if (session->session_id == session_id) {
25461b9a047SGuillaume Nault 			l2tp_session_inc_refcount(session);
25561b9a047SGuillaume Nault 			if (do_ref && session->ref)
25661b9a047SGuillaume Nault 				session->ref(session);
25761b9a047SGuillaume Nault 			read_unlock_bh(&tunnel->hlist_lock);
25861b9a047SGuillaume Nault 
25961b9a047SGuillaume Nault 			return session;
26061b9a047SGuillaume Nault 		}
26161b9a047SGuillaume Nault 	}
26261b9a047SGuillaume Nault 	read_unlock_bh(&tunnel->hlist_lock);
26361b9a047SGuillaume Nault 
26461b9a047SGuillaume Nault 	return NULL;
26561b9a047SGuillaume Nault }
26661b9a047SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get);
26761b9a047SGuillaume Nault 
268e08293a4SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
269e08293a4SGuillaume Nault 					  bool do_ref)
270fd558d18SJames Chapman {
271fd558d18SJames Chapman 	int hash;
272fd558d18SJames Chapman 	struct l2tp_session *session;
273fd558d18SJames Chapman 	int count = 0;
274fd558d18SJames Chapman 
275fd558d18SJames Chapman 	read_lock_bh(&tunnel->hlist_lock);
276fd558d18SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
277b67bfe0dSSasha Levin 		hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
278fd558d18SJames Chapman 			if (++count > nth) {
279e08293a4SGuillaume Nault 				l2tp_session_inc_refcount(session);
280e08293a4SGuillaume Nault 				if (do_ref && session->ref)
281e08293a4SGuillaume Nault 					session->ref(session);
282fd558d18SJames Chapman 				read_unlock_bh(&tunnel->hlist_lock);
283fd558d18SJames Chapman 				return session;
284fd558d18SJames Chapman 			}
285fd558d18SJames Chapman 		}
286fd558d18SJames Chapman 	}
287fd558d18SJames Chapman 
288fd558d18SJames Chapman 	read_unlock_bh(&tunnel->hlist_lock);
289fd558d18SJames Chapman 
290fd558d18SJames Chapman 	return NULL;
291fd558d18SJames Chapman }
292e08293a4SGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
293fd558d18SJames Chapman 
294309795f4SJames Chapman /* Lookup a session by interface name.
295309795f4SJames Chapman  * This is very inefficient but is only used by management interfaces.
296309795f4SJames Chapman  */
2979aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
2989aaef50cSGuillaume Nault 						const char *ifname,
2992777e2abSGuillaume Nault 						bool do_ref)
300309795f4SJames Chapman {
301309795f4SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
302309795f4SJames Chapman 	int hash;
303309795f4SJames Chapman 	struct l2tp_session *session;
304309795f4SJames Chapman 
305e02d494dSJames Chapman 	rcu_read_lock_bh();
306309795f4SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
307b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
308309795f4SJames Chapman 			if (!strcmp(session->ifname, ifname)) {
3092777e2abSGuillaume Nault 				l2tp_session_inc_refcount(session);
3102777e2abSGuillaume Nault 				if (do_ref && session->ref)
3112777e2abSGuillaume Nault 					session->ref(session);
312e02d494dSJames Chapman 				rcu_read_unlock_bh();
3132777e2abSGuillaume Nault 
314309795f4SJames Chapman 				return session;
315309795f4SJames Chapman 			}
316309795f4SJames Chapman 		}
317309795f4SJames Chapman 	}
318309795f4SJames Chapman 
319e02d494dSJames Chapman 	rcu_read_unlock_bh();
320309795f4SJames Chapman 
321309795f4SJames Chapman 	return NULL;
322309795f4SJames Chapman }
3232777e2abSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
324309795f4SJames Chapman 
325dbdbc73bSGuillaume Nault static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel,
326dbdbc73bSGuillaume Nault 				      struct l2tp_session *session)
327dbdbc73bSGuillaume Nault {
328dbdbc73bSGuillaume Nault 	struct l2tp_session *session_walk;
329dbdbc73bSGuillaume Nault 	struct hlist_head *g_head;
330dbdbc73bSGuillaume Nault 	struct hlist_head *head;
331dbdbc73bSGuillaume Nault 	struct l2tp_net *pn;
332dbdbc73bSGuillaume Nault 
333dbdbc73bSGuillaume Nault 	head = l2tp_session_id_hash(tunnel, session->session_id);
334dbdbc73bSGuillaume Nault 
335dbdbc73bSGuillaume Nault 	write_lock_bh(&tunnel->hlist_lock);
336dbdbc73bSGuillaume Nault 	hlist_for_each_entry(session_walk, head, hlist)
337dbdbc73bSGuillaume Nault 		if (session_walk->session_id == session->session_id)
338dbdbc73bSGuillaume Nault 			goto exist;
339dbdbc73bSGuillaume Nault 
340dbdbc73bSGuillaume Nault 	if (tunnel->version == L2TP_HDR_VER_3) {
341dbdbc73bSGuillaume Nault 		pn = l2tp_pernet(tunnel->l2tp_net);
342dbdbc73bSGuillaume Nault 		g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
343dbdbc73bSGuillaume Nault 						session->session_id);
344dbdbc73bSGuillaume Nault 
345dbdbc73bSGuillaume Nault 		spin_lock_bh(&pn->l2tp_session_hlist_lock);
346dbdbc73bSGuillaume Nault 		hlist_for_each_entry(session_walk, g_head, global_hlist)
347dbdbc73bSGuillaume Nault 			if (session_walk->session_id == session->session_id)
348dbdbc73bSGuillaume Nault 				goto exist_glob;
349dbdbc73bSGuillaume Nault 
350dbdbc73bSGuillaume Nault 		hlist_add_head_rcu(&session->global_hlist, g_head);
351dbdbc73bSGuillaume Nault 		spin_unlock_bh(&pn->l2tp_session_hlist_lock);
352dbdbc73bSGuillaume Nault 	}
353dbdbc73bSGuillaume Nault 
354dbdbc73bSGuillaume Nault 	hlist_add_head(&session->hlist, head);
355dbdbc73bSGuillaume Nault 	write_unlock_bh(&tunnel->hlist_lock);
356dbdbc73bSGuillaume Nault 
357dbdbc73bSGuillaume Nault 	return 0;
358dbdbc73bSGuillaume Nault 
359dbdbc73bSGuillaume Nault exist_glob:
360dbdbc73bSGuillaume Nault 	spin_unlock_bh(&pn->l2tp_session_hlist_lock);
361dbdbc73bSGuillaume Nault exist:
362dbdbc73bSGuillaume Nault 	write_unlock_bh(&tunnel->hlist_lock);
363dbdbc73bSGuillaume Nault 
364dbdbc73bSGuillaume Nault 	return -EEXIST;
365dbdbc73bSGuillaume Nault }
366dbdbc73bSGuillaume Nault 
367fd558d18SJames Chapman /* Lookup a tunnel by id
368fd558d18SJames Chapman  */
3692f858b92SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id)
370fd558d18SJames Chapman {
371fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
372fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
373fd558d18SJames Chapman 
374e02d494dSJames Chapman 	rcu_read_lock_bh();
375e02d494dSJames Chapman 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
376fd558d18SJames Chapman 		if (tunnel->tunnel_id == tunnel_id) {
377e02d494dSJames Chapman 			rcu_read_unlock_bh();
378fd558d18SJames Chapman 			return tunnel;
379fd558d18SJames Chapman 		}
380fd558d18SJames Chapman 	}
381e02d494dSJames Chapman 	rcu_read_unlock_bh();
382fd558d18SJames Chapman 
383fd558d18SJames Chapman 	return NULL;
384fd558d18SJames Chapman }
385fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
386fd558d18SJames Chapman 
3872f858b92SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth)
388fd558d18SJames Chapman {
389fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
390fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
391fd558d18SJames Chapman 	int count = 0;
392fd558d18SJames Chapman 
393e02d494dSJames Chapman 	rcu_read_lock_bh();
394e02d494dSJames Chapman 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
395fd558d18SJames Chapman 		if (++count > nth) {
396e02d494dSJames Chapman 			rcu_read_unlock_bh();
397fd558d18SJames Chapman 			return tunnel;
398fd558d18SJames Chapman 		}
399fd558d18SJames Chapman 	}
400fd558d18SJames Chapman 
401e02d494dSJames Chapman 	rcu_read_unlock_bh();
402fd558d18SJames Chapman 
403fd558d18SJames Chapman 	return NULL;
404fd558d18SJames Chapman }
405fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
406fd558d18SJames Chapman 
407fd558d18SJames Chapman /*****************************************************************************
408fd558d18SJames Chapman  * Receive data handling
409fd558d18SJames Chapman  *****************************************************************************/
410fd558d18SJames Chapman 
411fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence
412fd558d18SJames Chapman  * number.
413fd558d18SJames Chapman  */
414fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
415fd558d18SJames Chapman {
416fd558d18SJames Chapman 	struct sk_buff *skbp;
417fd558d18SJames Chapman 	struct sk_buff *tmp;
418f7faffa3SJames Chapman 	u32 ns = L2TP_SKB_CB(skb)->ns;
419fd558d18SJames Chapman 
420fd558d18SJames Chapman 	spin_lock_bh(&session->reorder_q.lock);
421fd558d18SJames Chapman 	skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
422fd558d18SJames Chapman 		if (L2TP_SKB_CB(skbp)->ns > ns) {
423fd558d18SJames Chapman 			__skb_queue_before(&session->reorder_q, skbp, skb);
424a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
425fd558d18SJames Chapman 				 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
426fd558d18SJames Chapman 				 session->name, ns, L2TP_SKB_CB(skbp)->ns,
427fd558d18SJames Chapman 				 skb_queue_len(&session->reorder_q));
4287b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_oos_packets);
429fd558d18SJames Chapman 			goto out;
430fd558d18SJames Chapman 		}
431fd558d18SJames Chapman 	}
432fd558d18SJames Chapman 
433fd558d18SJames Chapman 	__skb_queue_tail(&session->reorder_q, skb);
434fd558d18SJames Chapman 
435fd558d18SJames Chapman out:
436fd558d18SJames Chapman 	spin_unlock_bh(&session->reorder_q.lock);
437fd558d18SJames Chapman }
438fd558d18SJames Chapman 
439fd558d18SJames Chapman /* Dequeue a single skb.
440fd558d18SJames Chapman  */
441fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
442fd558d18SJames Chapman {
443fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
444fd558d18SJames Chapman 	int length = L2TP_SKB_CB(skb)->length;
445fd558d18SJames Chapman 
446fd558d18SJames Chapman 	/* We're about to requeue the skb, so return resources
447fd558d18SJames Chapman 	 * to its current owner (a socket receive buffer).
448fd558d18SJames Chapman 	 */
449fd558d18SJames Chapman 	skb_orphan(skb);
450fd558d18SJames Chapman 
4517b7c0719STom Parkin 	atomic_long_inc(&tunnel->stats.rx_packets);
4527b7c0719STom Parkin 	atomic_long_add(length, &tunnel->stats.rx_bytes);
4537b7c0719STom Parkin 	atomic_long_inc(&session->stats.rx_packets);
4547b7c0719STom Parkin 	atomic_long_add(length, &session->stats.rx_bytes);
455fd558d18SJames Chapman 
456fd558d18SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
457fd558d18SJames Chapman 		/* Bump our Nr */
458fd558d18SJames Chapman 		session->nr++;
4598a1631d5SJames Chapman 		session->nr &= session->nr_max;
460f7faffa3SJames Chapman 
461a4ca44faSJoe Perches 		l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
462a4ca44faSJoe Perches 			 session->name, session->nr);
463fd558d18SJames Chapman 	}
464fd558d18SJames Chapman 
465fd558d18SJames Chapman 	/* call private receive handler */
466fd558d18SJames Chapman 	if (session->recv_skb != NULL)
467fd558d18SJames Chapman 		(*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
468fd558d18SJames Chapman 	else
469fd558d18SJames Chapman 		kfree_skb(skb);
470fd558d18SJames Chapman 
471fd558d18SJames Chapman 	if (session->deref)
472fd558d18SJames Chapman 		(*session->deref)(session);
473fd558d18SJames Chapman }
474fd558d18SJames Chapman 
475fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order.
476fd558d18SJames Chapman  * Skbs that have been in the queue for too long are simply discarded.
477fd558d18SJames Chapman  */
478fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session)
479fd558d18SJames Chapman {
480fd558d18SJames Chapman 	struct sk_buff *skb;
481fd558d18SJames Chapman 	struct sk_buff *tmp;
482fd558d18SJames Chapman 
483fd558d18SJames Chapman 	/* If the pkt at the head of the queue has the nr that we
484fd558d18SJames Chapman 	 * expect to send up next, dequeue it and any other
485fd558d18SJames Chapman 	 * in-sequence packets behind it.
486fd558d18SJames Chapman 	 */
487e2e210c0SEric Dumazet start:
488fd558d18SJames Chapman 	spin_lock_bh(&session->reorder_q.lock);
489fd558d18SJames Chapman 	skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
490fd558d18SJames Chapman 		if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
4917b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
4927b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_errors);
493a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
494a4ca44faSJoe Perches 				 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
495fd558d18SJames Chapman 				 session->name, L2TP_SKB_CB(skb)->ns,
496fd558d18SJames Chapman 				 L2TP_SKB_CB(skb)->length, session->nr,
497fd558d18SJames Chapman 				 skb_queue_len(&session->reorder_q));
49838d40b3fSJames Chapman 			session->reorder_skip = 1;
499fd558d18SJames Chapman 			__skb_unlink(skb, &session->reorder_q);
500fd558d18SJames Chapman 			kfree_skb(skb);
501fd558d18SJames Chapman 			if (session->deref)
502fd558d18SJames Chapman 				(*session->deref)(session);
503fd558d18SJames Chapman 			continue;
504fd558d18SJames Chapman 		}
505fd558d18SJames Chapman 
506fd558d18SJames Chapman 		if (L2TP_SKB_CB(skb)->has_seq) {
50738d40b3fSJames Chapman 			if (session->reorder_skip) {
508a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
50938d40b3fSJames Chapman 					 "%s: advancing nr to next pkt: %u -> %u",
51038d40b3fSJames Chapman 					 session->name, session->nr,
51138d40b3fSJames Chapman 					 L2TP_SKB_CB(skb)->ns);
51238d40b3fSJames Chapman 				session->reorder_skip = 0;
51338d40b3fSJames Chapman 				session->nr = L2TP_SKB_CB(skb)->ns;
51438d40b3fSJames Chapman 			}
515fd558d18SJames Chapman 			if (L2TP_SKB_CB(skb)->ns != session->nr) {
516a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
517a4ca44faSJoe Perches 					 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
518fd558d18SJames Chapman 					 session->name, L2TP_SKB_CB(skb)->ns,
519fd558d18SJames Chapman 					 L2TP_SKB_CB(skb)->length, session->nr,
520fd558d18SJames Chapman 					 skb_queue_len(&session->reorder_q));
521fd558d18SJames Chapman 				goto out;
522fd558d18SJames Chapman 			}
523fd558d18SJames Chapman 		}
524fd558d18SJames Chapman 		__skb_unlink(skb, &session->reorder_q);
525fd558d18SJames Chapman 
526fd558d18SJames Chapman 		/* Process the skb. We release the queue lock while we
527fd558d18SJames Chapman 		 * do so to let other contexts process the queue.
528fd558d18SJames Chapman 		 */
529fd558d18SJames Chapman 		spin_unlock_bh(&session->reorder_q.lock);
530fd558d18SJames Chapman 		l2tp_recv_dequeue_skb(session, skb);
531e2e210c0SEric Dumazet 		goto start;
532fd558d18SJames Chapman 	}
533fd558d18SJames Chapman 
534fd558d18SJames Chapman out:
535fd558d18SJames Chapman 	spin_unlock_bh(&session->reorder_q.lock);
536fd558d18SJames Chapman }
537fd558d18SJames Chapman 
5388a1631d5SJames Chapman static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
5398a1631d5SJames Chapman {
5408a1631d5SJames Chapman 	u32 nws;
5418a1631d5SJames Chapman 
5428a1631d5SJames Chapman 	if (nr >= session->nr)
5438a1631d5SJames Chapman 		nws = nr - session->nr;
5448a1631d5SJames Chapman 	else
5458a1631d5SJames Chapman 		nws = (session->nr_max + 1) - (session->nr - nr);
5468a1631d5SJames Chapman 
5478a1631d5SJames Chapman 	return nws < session->nr_window_size;
5488a1631d5SJames Chapman }
5498a1631d5SJames Chapman 
550b6dc01a4SJames Chapman /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
551b6dc01a4SJames Chapman  * acceptable, else non-zero.
552b6dc01a4SJames Chapman  */
553b6dc01a4SJames Chapman static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
554b6dc01a4SJames Chapman {
5558a1631d5SJames Chapman 	if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
5568a1631d5SJames Chapman 		/* Packet sequence number is outside allowed window.
5578a1631d5SJames Chapman 		 * Discard it.
5588a1631d5SJames Chapman 		 */
5598a1631d5SJames Chapman 		l2tp_dbg(session, L2TP_MSG_SEQ,
5608a1631d5SJames Chapman 			 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
5618a1631d5SJames Chapman 			 session->name, L2TP_SKB_CB(skb)->ns,
5628a1631d5SJames Chapman 			 L2TP_SKB_CB(skb)->length, session->nr);
5638a1631d5SJames Chapman 		goto discard;
5648a1631d5SJames Chapman 	}
5658a1631d5SJames Chapman 
566b6dc01a4SJames Chapman 	if (session->reorder_timeout != 0) {
567b6dc01a4SJames Chapman 		/* Packet reordering enabled. Add skb to session's
568b6dc01a4SJames Chapman 		 * reorder queue, in order of ns.
569b6dc01a4SJames Chapman 		 */
570b6dc01a4SJames Chapman 		l2tp_recv_queue_skb(session, skb);
571a0dbd822SJames Chapman 		goto out;
572a0dbd822SJames Chapman 	}
573a0dbd822SJames Chapman 
574a0dbd822SJames Chapman 	/* Packet reordering disabled. Discard out-of-sequence packets, while
575a0dbd822SJames Chapman 	 * tracking the number if in-sequence packets after the first OOS packet
576a0dbd822SJames Chapman 	 * is seen. After nr_oos_count_max in-sequence packets, reset the
577a0dbd822SJames Chapman 	 * sequence number to re-enable packet reception.
578b6dc01a4SJames Chapman 	 */
579a0dbd822SJames Chapman 	if (L2TP_SKB_CB(skb)->ns == session->nr) {
580a0dbd822SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
581a0dbd822SJames Chapman 	} else {
582a0dbd822SJames Chapman 		u32 nr_oos = L2TP_SKB_CB(skb)->ns;
583a0dbd822SJames Chapman 		u32 nr_next = (session->nr_oos + 1) & session->nr_max;
584a0dbd822SJames Chapman 
585a0dbd822SJames Chapman 		if (nr_oos == nr_next)
586a0dbd822SJames Chapman 			session->nr_oos_count++;
587a0dbd822SJames Chapman 		else
588a0dbd822SJames Chapman 			session->nr_oos_count = 0;
589a0dbd822SJames Chapman 
590a0dbd822SJames Chapman 		session->nr_oos = nr_oos;
591a0dbd822SJames Chapman 		if (session->nr_oos_count > session->nr_oos_count_max) {
592a0dbd822SJames Chapman 			session->reorder_skip = 1;
593a0dbd822SJames Chapman 			l2tp_dbg(session, L2TP_MSG_SEQ,
594a0dbd822SJames Chapman 				 "%s: %d oos packets received. Resetting sequence numbers\n",
595a0dbd822SJames Chapman 				 session->name, session->nr_oos_count);
596a0dbd822SJames Chapman 		}
597a0dbd822SJames Chapman 		if (!session->reorder_skip) {
598b6dc01a4SJames Chapman 			atomic_long_inc(&session->stats.rx_seq_discards);
599b6dc01a4SJames Chapman 			l2tp_dbg(session, L2TP_MSG_SEQ,
600b6dc01a4SJames Chapman 				 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
601b6dc01a4SJames Chapman 				 session->name, L2TP_SKB_CB(skb)->ns,
602b6dc01a4SJames Chapman 				 L2TP_SKB_CB(skb)->length, session->nr,
603b6dc01a4SJames Chapman 				 skb_queue_len(&session->reorder_q));
604b6dc01a4SJames Chapman 			goto discard;
605b6dc01a4SJames Chapman 		}
606b6dc01a4SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
607b6dc01a4SJames Chapman 	}
608b6dc01a4SJames Chapman 
609a0dbd822SJames Chapman out:
610b6dc01a4SJames Chapman 	return 0;
611b6dc01a4SJames Chapman 
612b6dc01a4SJames Chapman discard:
613b6dc01a4SJames Chapman 	return 1;
614b6dc01a4SJames Chapman }
615b6dc01a4SJames Chapman 
616f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2
617f7faffa3SJames Chapman  * and L2TPv3 data frames here.
618f7faffa3SJames Chapman  *
619f7faffa3SJames Chapman  * L2TPv2 Data Message Header
620f7faffa3SJames Chapman  *
621f7faffa3SJames Chapman  *  0                   1                   2                   3
622f7faffa3SJames 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
623f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
624f7faffa3SJames Chapman  * |T|L|x|x|S|x|O|P|x|x|x|x|  Ver  |          Length (opt)         |
625f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
626f7faffa3SJames Chapman  * |           Tunnel ID           |           Session ID          |
627f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628f7faffa3SJames Chapman  * |             Ns (opt)          |             Nr (opt)          |
629f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630f7faffa3SJames Chapman  * |      Offset Size (opt)        |    Offset pad... (opt)
631f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632f7faffa3SJames Chapman  *
633f7faffa3SJames Chapman  * Data frames are marked by T=0. All other fields are the same as
634f7faffa3SJames Chapman  * those in L2TP control frames.
635f7faffa3SJames Chapman  *
636f7faffa3SJames Chapman  * L2TPv3 Data Message Header
637f7faffa3SJames Chapman  *
638f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
639f7faffa3SJames Chapman  * |                      L2TP Session Header                      |
640f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
641f7faffa3SJames Chapman  * |                      L2-Specific Sublayer                     |
642f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
643f7faffa3SJames Chapman  * |                        Tunnel Payload                      ...
644f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
645f7faffa3SJames Chapman  *
646f7faffa3SJames Chapman  * L2TPv3 Session Header Over IP
647f7faffa3SJames Chapman  *
648f7faffa3SJames Chapman  *  0                   1                   2                   3
649f7faffa3SJames 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
650f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
651f7faffa3SJames Chapman  * |                           Session ID                          |
652f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
653f7faffa3SJames Chapman  * |               Cookie (optional, maximum 64 bits)...
654f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
655f7faffa3SJames Chapman  *                                                                 |
656f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657f7faffa3SJames Chapman  *
658f7faffa3SJames Chapman  * L2TPv3 L2-Specific Sublayer Format
659f7faffa3SJames Chapman  *
660f7faffa3SJames Chapman  *  0                   1                   2                   3
661f7faffa3SJames 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
662f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
663f7faffa3SJames Chapman  * |x|S|x|x|x|x|x|x|              Sequence Number                  |
664f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
665f7faffa3SJames Chapman  *
666f7faffa3SJames Chapman  * Cookie value, sublayer format and offset (pad) are negotiated with
667f7faffa3SJames Chapman  * the peer when the session is set up. Unlike L2TPv2, we do not need
668f7faffa3SJames Chapman  * to parse the packet header to determine if optional fields are
669f7faffa3SJames Chapman  * present.
670f7faffa3SJames Chapman  *
671f7faffa3SJames Chapman  * Caller must already have parsed the frame and determined that it is
672f7faffa3SJames Chapman  * a data (not control) frame before coming here. Fields up to the
673f7faffa3SJames Chapman  * session-id have already been parsed and ptr points to the data
674f7faffa3SJames Chapman  * after the session-id.
67561b9a047SGuillaume Nault  *
67661b9a047SGuillaume Nault  * session->ref() must have been called prior to l2tp_recv_common().
67761b9a047SGuillaume Nault  * session->deref() will be called automatically after skb is processed.
678f7faffa3SJames Chapman  */
679f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
680f7faffa3SJames Chapman 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
681f7faffa3SJames Chapman 		      int length, int (*payload_hook)(struct sk_buff *skb))
682f7faffa3SJames Chapman {
683f7faffa3SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
684f7faffa3SJames Chapman 	int offset;
685f7faffa3SJames Chapman 	u32 ns, nr;
686f7faffa3SJames Chapman 
687f7faffa3SJames Chapman 	/* Parse and check optional cookie */
688f7faffa3SJames Chapman 	if (session->peer_cookie_len > 0) {
689f7faffa3SJames Chapman 		if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
690a4ca44faSJoe Perches 			l2tp_info(tunnel, L2TP_MSG_DATA,
691f7faffa3SJames Chapman 				  "%s: cookie mismatch (%u/%u). Discarding.\n",
692a4ca44faSJoe Perches 				  tunnel->name, tunnel->tunnel_id,
693a4ca44faSJoe Perches 				  session->session_id);
6947b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_cookie_discards);
695f7faffa3SJames Chapman 			goto discard;
696f7faffa3SJames Chapman 		}
697f7faffa3SJames Chapman 		ptr += session->peer_cookie_len;
698f7faffa3SJames Chapman 	}
699f7faffa3SJames Chapman 
700f7faffa3SJames Chapman 	/* Handle the optional sequence numbers. Sequence numbers are
701f7faffa3SJames Chapman 	 * in different places for L2TPv2 and L2TPv3.
702f7faffa3SJames Chapman 	 *
703f7faffa3SJames Chapman 	 * If we are the LAC, enable/disable sequence numbers under
704f7faffa3SJames Chapman 	 * the control of the LNS.  If no sequence numbers present but
705f7faffa3SJames Chapman 	 * we were expecting them, discard frame.
706f7faffa3SJames Chapman 	 */
707f7faffa3SJames Chapman 	ns = nr = 0;
708f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->has_seq = 0;
709f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
710f7faffa3SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_S) {
711f7faffa3SJames Chapman 			ns = ntohs(*(__be16 *) ptr);
712f7faffa3SJames Chapman 			ptr += 2;
713f7faffa3SJames Chapman 			nr = ntohs(*(__be16 *) ptr);
714f7faffa3SJames Chapman 			ptr += 2;
715f7faffa3SJames Chapman 
716f7faffa3SJames Chapman 			/* Store L2TP info in the skb */
717f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->ns = ns;
718f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->has_seq = 1;
719f7faffa3SJames Chapman 
720a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
721f7faffa3SJames Chapman 				 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
722f7faffa3SJames Chapman 				 session->name, ns, nr, session->nr);
723f7faffa3SJames Chapman 		}
724f7faffa3SJames Chapman 	} else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
725f7faffa3SJames Chapman 		u32 l2h = ntohl(*(__be32 *) ptr);
726f7faffa3SJames Chapman 
727f7faffa3SJames Chapman 		if (l2h & 0x40000000) {
728f7faffa3SJames Chapman 			ns = l2h & 0x00ffffff;
729f7faffa3SJames Chapman 
730f7faffa3SJames Chapman 			/* Store L2TP info in the skb */
731f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->ns = ns;
732f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->has_seq = 1;
733f7faffa3SJames Chapman 
734a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
735f7faffa3SJames Chapman 				 "%s: recv data ns=%u, session nr=%u\n",
736f7faffa3SJames Chapman 				 session->name, ns, session->nr);
737f7faffa3SJames Chapman 		}
738f7faffa3SJames Chapman 	}
739f7faffa3SJames Chapman 
740f7faffa3SJames Chapman 	/* Advance past L2-specific header, if present */
741f7faffa3SJames Chapman 	ptr += session->l2specific_len;
742f7faffa3SJames Chapman 
743f7faffa3SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
744f7faffa3SJames Chapman 		/* Received a packet with sequence numbers. If we're the LNS,
745f7faffa3SJames Chapman 		 * check if we sre sending sequence numbers and if not,
746f7faffa3SJames Chapman 		 * configure it so.
747f7faffa3SJames Chapman 		 */
748f7faffa3SJames Chapman 		if ((!session->lns_mode) && (!session->send_seq)) {
749a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_SEQ,
750f7faffa3SJames Chapman 				  "%s: requested to enable seq numbers by LNS\n",
751f7faffa3SJames Chapman 				  session->name);
7523f9b9770SAsbjørn Sloth Tønnesen 			session->send_seq = 1;
753f7faffa3SJames Chapman 			l2tp_session_set_header_len(session, tunnel->version);
754f7faffa3SJames Chapman 		}
755f7faffa3SJames Chapman 	} else {
756f7faffa3SJames Chapman 		/* No sequence numbers.
757f7faffa3SJames Chapman 		 * If user has configured mandatory sequence numbers, discard.
758f7faffa3SJames Chapman 		 */
759f7faffa3SJames Chapman 		if (session->recv_seq) {
760a4ca44faSJoe Perches 			l2tp_warn(session, L2TP_MSG_SEQ,
761a4ca44faSJoe Perches 				  "%s: recv data has no seq numbers when required. Discarding.\n",
762a4ca44faSJoe Perches 				  session->name);
7637b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
764f7faffa3SJames Chapman 			goto discard;
765f7faffa3SJames Chapman 		}
766f7faffa3SJames Chapman 
767f7faffa3SJames Chapman 		/* If we're the LAC and we're sending sequence numbers, the
768f7faffa3SJames Chapman 		 * LNS has requested that we no longer send sequence numbers.
769f7faffa3SJames Chapman 		 * If we're the LNS and we're sending sequence numbers, the
770f7faffa3SJames Chapman 		 * LAC is broken. Discard the frame.
771f7faffa3SJames Chapman 		 */
772f7faffa3SJames Chapman 		if ((!session->lns_mode) && (session->send_seq)) {
773a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_SEQ,
774f7faffa3SJames Chapman 				  "%s: requested to disable seq numbers by LNS\n",
775f7faffa3SJames Chapman 				  session->name);
776f7faffa3SJames Chapman 			session->send_seq = 0;
777f7faffa3SJames Chapman 			l2tp_session_set_header_len(session, tunnel->version);
778f7faffa3SJames Chapman 		} else if (session->send_seq) {
779a4ca44faSJoe Perches 			l2tp_warn(session, L2TP_MSG_SEQ,
780a4ca44faSJoe Perches 				  "%s: recv data has no seq numbers when required. Discarding.\n",
781a4ca44faSJoe Perches 				  session->name);
7827b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
783f7faffa3SJames Chapman 			goto discard;
784f7faffa3SJames Chapman 		}
785f7faffa3SJames Chapman 	}
786f7faffa3SJames Chapman 
787f7faffa3SJames Chapman 	/* Session data offset is handled differently for L2TPv2 and
788f7faffa3SJames Chapman 	 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
789f7faffa3SJames Chapman 	 * the header. For L2TPv3, the offset is negotiated using AVPs
790f7faffa3SJames Chapman 	 * in the session setup control protocol.
791f7faffa3SJames Chapman 	 */
792f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
793f7faffa3SJames Chapman 		/* If offset bit set, skip it. */
794f7faffa3SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_O) {
795f7faffa3SJames Chapman 			offset = ntohs(*(__be16 *)ptr);
796f7faffa3SJames Chapman 			ptr += 2 + offset;
797f7faffa3SJames Chapman 		}
798f7faffa3SJames Chapman 	} else
799f7faffa3SJames Chapman 		ptr += session->offset;
800f7faffa3SJames Chapman 
801f7faffa3SJames Chapman 	offset = ptr - optr;
802f7faffa3SJames Chapman 	if (!pskb_may_pull(skb, offset))
803f7faffa3SJames Chapman 		goto discard;
804f7faffa3SJames Chapman 
805f7faffa3SJames Chapman 	__skb_pull(skb, offset);
806f7faffa3SJames Chapman 
807f7faffa3SJames Chapman 	/* If caller wants to process the payload before we queue the
808f7faffa3SJames Chapman 	 * packet, do so now.
809f7faffa3SJames Chapman 	 */
810f7faffa3SJames Chapman 	if (payload_hook)
811f7faffa3SJames Chapman 		if ((*payload_hook)(skb))
812f7faffa3SJames Chapman 			goto discard;
813f7faffa3SJames Chapman 
814f7faffa3SJames Chapman 	/* Prepare skb for adding to the session's reorder_q.  Hold
815f7faffa3SJames Chapman 	 * packets for max reorder_timeout or 1 second if not
816f7faffa3SJames Chapman 	 * reordering.
817f7faffa3SJames Chapman 	 */
818f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->length = length;
819f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->expires = jiffies +
820f7faffa3SJames Chapman 		(session->reorder_timeout ? session->reorder_timeout : HZ);
821f7faffa3SJames Chapman 
822f7faffa3SJames Chapman 	/* Add packet to the session's receive queue. Reordering is done here, if
823f7faffa3SJames Chapman 	 * enabled. Saved L2TP protocol info is stored in skb->sb[].
824f7faffa3SJames Chapman 	 */
825f7faffa3SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
826b6dc01a4SJames Chapman 		if (l2tp_recv_data_seq(session, skb))
827f7faffa3SJames Chapman 			goto discard;
828f7faffa3SJames Chapman 	} else {
829f7faffa3SJames Chapman 		/* No sequence numbers. Add the skb to the tail of the
830f7faffa3SJames Chapman 		 * reorder queue. This ensures that it will be
831f7faffa3SJames Chapman 		 * delivered after all previous sequenced skbs.
832f7faffa3SJames Chapman 		 */
833f7faffa3SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
834f7faffa3SJames Chapman 	}
835f7faffa3SJames Chapman 
836f7faffa3SJames Chapman 	/* Try to dequeue as many skbs from reorder_q as we can. */
837f7faffa3SJames Chapman 	l2tp_recv_dequeue(session);
838f7faffa3SJames Chapman 
839f7faffa3SJames Chapman 	return;
840f7faffa3SJames Chapman 
841f7faffa3SJames Chapman discard:
8427b7c0719STom Parkin 	atomic_long_inc(&session->stats.rx_errors);
843f7faffa3SJames Chapman 	kfree_skb(skb);
844f7faffa3SJames Chapman 
845f7faffa3SJames Chapman 	if (session->deref)
846f7faffa3SJames Chapman 		(*session->deref)(session);
847f7faffa3SJames Chapman }
848f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common);
849f7faffa3SJames Chapman 
85048f72f92STom Parkin /* Drop skbs from the session's reorder_q
85148f72f92STom Parkin  */
85248f72f92STom Parkin int l2tp_session_queue_purge(struct l2tp_session *session)
85348f72f92STom Parkin {
85448f72f92STom Parkin 	struct sk_buff *skb = NULL;
85548f72f92STom Parkin 	BUG_ON(!session);
85648f72f92STom Parkin 	BUG_ON(session->magic != L2TP_SESSION_MAGIC);
85748f72f92STom Parkin 	while ((skb = skb_dequeue(&session->reorder_q))) {
85848f72f92STom Parkin 		atomic_long_inc(&session->stats.rx_errors);
85948f72f92STom Parkin 		kfree_skb(skb);
86048f72f92STom Parkin 		if (session->deref)
86148f72f92STom Parkin 			(*session->deref)(session);
86248f72f92STom Parkin 	}
86348f72f92STom Parkin 	return 0;
86448f72f92STom Parkin }
86548f72f92STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
86648f72f92STom Parkin 
867fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
868fd558d18SJames Chapman  * here. The skb is not on a list when we get here.
869fd558d18SJames Chapman  * Returns 0 if the packet was a data packet and was successfully passed on.
870fd558d18SJames Chapman  * Returns 1 if the packet was not a good data packet and could not be
871fd558d18SJames Chapman  * forwarded.  All such packets are passed up to userspace to deal with.
872fd558d18SJames Chapman  */
873fc130840Sstephen hemminger static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
874fd558d18SJames Chapman 			      int (*payload_hook)(struct sk_buff *skb))
875fd558d18SJames Chapman {
876fd558d18SJames Chapman 	struct l2tp_session *session = NULL;
877fd558d18SJames Chapman 	unsigned char *ptr, *optr;
878fd558d18SJames Chapman 	u16 hdrflags;
879fd558d18SJames Chapman 	u32 tunnel_id, session_id;
880fd558d18SJames Chapman 	u16 version;
881f7faffa3SJames Chapman 	int length;
882fd558d18SJames Chapman 
88358d6085cSTom Herbert 	/* UDP has verifed checksum */
884fd558d18SJames Chapman 
885fd558d18SJames Chapman 	/* UDP always verifies the packet length. */
886fd558d18SJames Chapman 	__skb_pull(skb, sizeof(struct udphdr));
887fd558d18SJames Chapman 
888fd558d18SJames Chapman 	/* Short packet? */
889fd558d18SJames Chapman 	if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
890a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
891a4ca44faSJoe Perches 			  "%s: recv short packet (len=%d)\n",
892a4ca44faSJoe Perches 			  tunnel->name, skb->len);
893fd558d18SJames Chapman 		goto error;
894fd558d18SJames Chapman 	}
895fd558d18SJames Chapman 
896fd558d18SJames Chapman 	/* Trace packet contents, if enabled */
897fd558d18SJames Chapman 	if (tunnel->debug & L2TP_MSG_DATA) {
898fd558d18SJames Chapman 		length = min(32u, skb->len);
899fd558d18SJames Chapman 		if (!pskb_may_pull(skb, length))
900fd558d18SJames Chapman 			goto error;
901fd558d18SJames Chapman 
902a4ca44faSJoe Perches 		pr_debug("%s: recv\n", tunnel->name);
903a4ca44faSJoe Perches 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
904fd558d18SJames Chapman 	}
905fd558d18SJames Chapman 
906e50e705cSEric Dumazet 	/* Point to L2TP header */
907e50e705cSEric Dumazet 	optr = ptr = skb->data;
908e50e705cSEric Dumazet 
909fd558d18SJames Chapman 	/* Get L2TP header flags */
910fd558d18SJames Chapman 	hdrflags = ntohs(*(__be16 *) ptr);
911fd558d18SJames Chapman 
912fd558d18SJames Chapman 	/* Check protocol version */
913fd558d18SJames Chapman 	version = hdrflags & L2TP_HDR_VER_MASK;
914fd558d18SJames Chapman 	if (version != tunnel->version) {
915a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
916fd558d18SJames Chapman 			  "%s: recv protocol version mismatch: got %d expected %d\n",
917fd558d18SJames Chapman 			  tunnel->name, version, tunnel->version);
918fd558d18SJames Chapman 		goto error;
919fd558d18SJames Chapman 	}
920fd558d18SJames Chapman 
921fd558d18SJames Chapman 	/* Get length of L2TP packet */
922fd558d18SJames Chapman 	length = skb->len;
923fd558d18SJames Chapman 
924fd558d18SJames Chapman 	/* If type is control packet, it is handled by userspace. */
925fd558d18SJames Chapman 	if (hdrflags & L2TP_HDRFLAG_T) {
926a4ca44faSJoe Perches 		l2tp_dbg(tunnel, L2TP_MSG_DATA,
927a4ca44faSJoe Perches 			 "%s: recv control packet, len=%d\n",
928a4ca44faSJoe Perches 			 tunnel->name, length);
929fd558d18SJames Chapman 		goto error;
930fd558d18SJames Chapman 	}
931fd558d18SJames Chapman 
932fd558d18SJames Chapman 	/* Skip flags */
933fd558d18SJames Chapman 	ptr += 2;
934fd558d18SJames Chapman 
935f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
936fd558d18SJames Chapman 		/* If length is present, skip it */
937fd558d18SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_L)
938fd558d18SJames Chapman 			ptr += 2;
939fd558d18SJames Chapman 
940fd558d18SJames Chapman 		/* Extract tunnel and session ID */
941fd558d18SJames Chapman 		tunnel_id = ntohs(*(__be16 *) ptr);
942fd558d18SJames Chapman 		ptr += 2;
943fd558d18SJames Chapman 		session_id = ntohs(*(__be16 *) ptr);
944fd558d18SJames Chapman 		ptr += 2;
945f7faffa3SJames Chapman 	} else {
946f7faffa3SJames Chapman 		ptr += 2;	/* skip reserved bits */
947f7faffa3SJames Chapman 		tunnel_id = tunnel->tunnel_id;
948f7faffa3SJames Chapman 		session_id = ntohl(*(__be32 *) ptr);
949f7faffa3SJames Chapman 		ptr += 4;
950f7faffa3SJames Chapman 	}
951fd558d18SJames Chapman 
952fd558d18SJames Chapman 	/* Find the session context */
95361b9a047SGuillaume Nault 	session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
954309795f4SJames Chapman 	if (!session || !session->recv_skb) {
95561b9a047SGuillaume Nault 		if (session) {
95661b9a047SGuillaume Nault 			if (session->deref)
95761b9a047SGuillaume Nault 				session->deref(session);
95861b9a047SGuillaume Nault 			l2tp_session_dec_refcount(session);
95961b9a047SGuillaume Nault 		}
96061b9a047SGuillaume Nault 
961fd558d18SJames Chapman 		/* Not found? Pass to userspace to deal with */
962a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
963f7faffa3SJames Chapman 			  "%s: no session found (%u/%u). Passing up.\n",
964fd558d18SJames Chapman 			  tunnel->name, tunnel_id, session_id);
965fd558d18SJames Chapman 		goto error;
966fd558d18SJames Chapman 	}
967fd558d18SJames Chapman 
968f7faffa3SJames Chapman 	l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
96961b9a047SGuillaume Nault 	l2tp_session_dec_refcount(session);
970fd558d18SJames Chapman 
971fd558d18SJames Chapman 	return 0;
972fd558d18SJames Chapman 
973fd558d18SJames Chapman error:
974fd558d18SJames Chapman 	/* Put UDP header back */
975fd558d18SJames Chapman 	__skb_push(skb, sizeof(struct udphdr));
976fd558d18SJames Chapman 
977fd558d18SJames Chapman 	return 1;
978fd558d18SJames Chapman }
979fd558d18SJames Chapman 
980fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c.
981fd558d18SJames Chapman  * Return codes:
982fd558d18SJames Chapman  * 0 : success.
983fd558d18SJames Chapman  * <0: error
984fd558d18SJames Chapman  * >0: skb should be passed up to userspace as UDP.
985fd558d18SJames Chapman  */
986fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
987fd558d18SJames Chapman {
988fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
989fd558d18SJames Chapman 
990fd558d18SJames Chapman 	tunnel = l2tp_sock_to_tunnel(sk);
991fd558d18SJames Chapman 	if (tunnel == NULL)
992fd558d18SJames Chapman 		goto pass_up;
993fd558d18SJames Chapman 
994a4ca44faSJoe Perches 	l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
995a4ca44faSJoe Perches 		 tunnel->name, skb->len);
996fd558d18SJames Chapman 
997fd558d18SJames Chapman 	if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
998fd558d18SJames Chapman 		goto pass_up_put;
999fd558d18SJames Chapman 
1000fd558d18SJames Chapman 	sock_put(sk);
1001fd558d18SJames Chapman 	return 0;
1002fd558d18SJames Chapman 
1003fd558d18SJames Chapman pass_up_put:
1004fd558d18SJames Chapman 	sock_put(sk);
1005fd558d18SJames Chapman pass_up:
1006fd558d18SJames Chapman 	return 1;
1007fd558d18SJames Chapman }
1008fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1009fd558d18SJames Chapman 
1010fd558d18SJames Chapman /************************************************************************
1011fd558d18SJames Chapman  * Transmit handling
1012fd558d18SJames Chapman  ***********************************************************************/
1013fd558d18SJames Chapman 
1014fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided.
1015fd558d18SJames Chapman  */
1016f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
1017fd558d18SJames Chapman {
1018f7faffa3SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1019fd558d18SJames Chapman 	__be16 *bufp = buf;
1020f7faffa3SJames Chapman 	__be16 *optr = buf;
1021fd558d18SJames Chapman 	u16 flags = L2TP_HDR_VER_2;
1022fd558d18SJames Chapman 	u32 tunnel_id = tunnel->peer_tunnel_id;
1023fd558d18SJames Chapman 	u32 session_id = session->peer_session_id;
1024fd558d18SJames Chapman 
1025fd558d18SJames Chapman 	if (session->send_seq)
1026fd558d18SJames Chapman 		flags |= L2TP_HDRFLAG_S;
1027fd558d18SJames Chapman 
1028fd558d18SJames Chapman 	/* Setup L2TP header. */
1029fd558d18SJames Chapman 	*bufp++ = htons(flags);
1030fd558d18SJames Chapman 	*bufp++ = htons(tunnel_id);
1031fd558d18SJames Chapman 	*bufp++ = htons(session_id);
1032fd558d18SJames Chapman 	if (session->send_seq) {
1033fd558d18SJames Chapman 		*bufp++ = htons(session->ns);
1034fd558d18SJames Chapman 		*bufp++ = 0;
1035fd558d18SJames Chapman 		session->ns++;
1036f7faffa3SJames Chapman 		session->ns &= 0xffff;
1037a4ca44faSJoe Perches 		l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1038a4ca44faSJoe Perches 			 session->name, session->ns);
1039fd558d18SJames Chapman 	}
1040fd558d18SJames Chapman 
1041f7faffa3SJames Chapman 	return bufp - optr;
1042f7faffa3SJames Chapman }
1043f7faffa3SJames Chapman 
1044f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1045fd558d18SJames Chapman {
10460d76751fSJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1047f7faffa3SJames Chapman 	char *bufp = buf;
1048f7faffa3SJames Chapman 	char *optr = bufp;
1049fd558d18SJames Chapman 
10500d76751fSJames Chapman 	/* Setup L2TP header. The header differs slightly for UDP and
10510d76751fSJames Chapman 	 * IP encapsulations. For UDP, there is 4 bytes of flags.
10520d76751fSJames Chapman 	 */
10530d76751fSJames Chapman 	if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
10540d76751fSJames Chapman 		u16 flags = L2TP_HDR_VER_3;
1055f7faffa3SJames Chapman 		*((__be16 *) bufp) = htons(flags);
1056f7faffa3SJames Chapman 		bufp += 2;
1057f7faffa3SJames Chapman 		*((__be16 *) bufp) = 0;
1058f7faffa3SJames Chapman 		bufp += 2;
10590d76751fSJames Chapman 	}
10600d76751fSJames Chapman 
1061f7faffa3SJames Chapman 	*((__be32 *) bufp) = htonl(session->peer_session_id);
1062f7faffa3SJames Chapman 	bufp += 4;
1063f7faffa3SJames Chapman 	if (session->cookie_len) {
1064f7faffa3SJames Chapman 		memcpy(bufp, &session->cookie[0], session->cookie_len);
1065f7faffa3SJames Chapman 		bufp += session->cookie_len;
1066fd558d18SJames Chapman 	}
1067f7faffa3SJames Chapman 	if (session->l2specific_len) {
1068f7faffa3SJames Chapman 		if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1069f7faffa3SJames Chapman 			u32 l2h = 0;
1070f7faffa3SJames Chapman 			if (session->send_seq) {
1071f7faffa3SJames Chapman 				l2h = 0x40000000 | session->ns;
1072f7faffa3SJames Chapman 				session->ns++;
1073f7faffa3SJames Chapman 				session->ns &= 0xffffff;
1074a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
1075a4ca44faSJoe Perches 					 "%s: updated ns to %u\n",
1076a4ca44faSJoe Perches 					 session->name, session->ns);
1077f7faffa3SJames Chapman 			}
1078f7faffa3SJames Chapman 
1079f7faffa3SJames Chapman 			*((__be32 *) bufp) = htonl(l2h);
1080f7faffa3SJames Chapman 		}
1081f7faffa3SJames Chapman 		bufp += session->l2specific_len;
1082f7faffa3SJames Chapman 	}
1083f7faffa3SJames Chapman 	if (session->offset)
1084f7faffa3SJames Chapman 		bufp += session->offset;
1085f7faffa3SJames Chapman 
1086f7faffa3SJames Chapman 	return bufp - optr;
1087f7faffa3SJames Chapman }
1088fd558d18SJames Chapman 
1089fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1090d9d8da80SDavid S. Miller 			  struct flowi *fl, size_t data_len)
1091fd558d18SJames Chapman {
1092fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1093fd558d18SJames Chapman 	unsigned int len = skb->len;
1094fd558d18SJames Chapman 	int error;
1095fd558d18SJames Chapman 
1096fd558d18SJames Chapman 	/* Debug */
1097fd558d18SJames Chapman 	if (session->send_seq)
10985b5e0928SAlexey Dobriyan 		l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
1099a4ca44faSJoe Perches 			 session->name, data_len, session->ns - 1);
1100fd558d18SJames Chapman 	else
11015b5e0928SAlexey Dobriyan 		l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
1102a4ca44faSJoe Perches 			 session->name, data_len);
1103fd558d18SJames Chapman 
1104fd558d18SJames Chapman 	if (session->debug & L2TP_MSG_DATA) {
11050d76751fSJames Chapman 		int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
11060d76751fSJames Chapman 		unsigned char *datap = skb->data + uhlen;
1107fd558d18SJames Chapman 
1108a4ca44faSJoe Perches 		pr_debug("%s: xmit\n", session->name);
1109a4ca44faSJoe Perches 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1110a4ca44faSJoe Perches 				     datap, min_t(size_t, 32, len - uhlen));
1111fd558d18SJames Chapman 	}
1112fd558d18SJames Chapman 
1113fd558d18SJames Chapman 	/* Queue the packet to IP for output */
111460ff7467SWANG Cong 	skb->ignore_df = 1;
1115d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1116746e3499SEric Dumazet 	if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
1117b0270e91SEric Dumazet 		error = inet6_csk_xmit(tunnel->sock, skb, NULL);
1118d2cf3361SBenjamin LaHaise 	else
1119d2cf3361SBenjamin LaHaise #endif
1120b0270e91SEric Dumazet 		error = ip_queue_xmit(tunnel->sock, skb, fl);
1121fd558d18SJames Chapman 
1122fd558d18SJames Chapman 	/* Update stats */
1123fd558d18SJames Chapman 	if (error >= 0) {
11247b7c0719STom Parkin 		atomic_long_inc(&tunnel->stats.tx_packets);
11257b7c0719STom Parkin 		atomic_long_add(len, &tunnel->stats.tx_bytes);
11267b7c0719STom Parkin 		atomic_long_inc(&session->stats.tx_packets);
11277b7c0719STom Parkin 		atomic_long_add(len, &session->stats.tx_bytes);
1128fd558d18SJames Chapman 	} else {
11297b7c0719STom Parkin 		atomic_long_inc(&tunnel->stats.tx_errors);
11307b7c0719STom Parkin 		atomic_long_inc(&session->stats.tx_errors);
1131fd558d18SJames Chapman 	}
1132fd558d18SJames Chapman 
1133fd558d18SJames Chapman 	return 0;
1134fd558d18SJames Chapman }
1135fd558d18SJames Chapman 
1136fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be
1137fd558d18SJames Chapman  * inserted in the skb data before calling this function.
1138fd558d18SJames Chapman  */
1139fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1140fd558d18SJames Chapman {
1141fd558d18SJames Chapman 	int data_len = skb->len;
11420d76751fSJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
11430d76751fSJames Chapman 	struct sock *sk = tunnel->sock;
1144d9d8da80SDavid S. Miller 	struct flowi *fl;
1145fd558d18SJames Chapman 	struct udphdr *uh;
1146fd558d18SJames Chapman 	struct inet_sock *inet;
1147fd558d18SJames Chapman 	int headroom;
11480d76751fSJames Chapman 	int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
11490d76751fSJames Chapman 	int udp_len;
1150b8c84307SEric Dumazet 	int ret = NET_XMIT_SUCCESS;
1151fd558d18SJames Chapman 
1152fd558d18SJames Chapman 	/* Check that there's enough headroom in the skb to insert IP,
1153fd558d18SJames Chapman 	 * UDP and L2TP headers. If not enough, expand it to
1154fd558d18SJames Chapman 	 * make room. Adjust truesize.
1155fd558d18SJames Chapman 	 */
1156fd558d18SJames Chapman 	headroom = NET_SKB_PAD + sizeof(struct iphdr) +
11570d76751fSJames Chapman 		uhlen + hdr_len;
1158835acf5dSEric Dumazet 	if (skb_cow_head(skb, headroom)) {
1159b8c84307SEric Dumazet 		kfree_skb(skb);
1160b8c84307SEric Dumazet 		return NET_XMIT_DROP;
1161835acf5dSEric Dumazet 	}
1162fd558d18SJames Chapman 
1163fd558d18SJames Chapman 	/* Setup L2TP header */
1164f7faffa3SJames Chapman 	session->build_header(session, __skb_push(skb, hdr_len));
1165fd558d18SJames Chapman 
11660d76751fSJames Chapman 	/* Reset skb netfilter state */
1167fd558d18SJames Chapman 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1168fd558d18SJames Chapman 	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1169fd558d18SJames Chapman 			      IPSKB_REROUTED);
1170fd558d18SJames Chapman 	nf_reset(skb);
1171fd558d18SJames Chapman 
11726af88da1SDavid S. Miller 	bh_lock_sock(sk);
11736af88da1SDavid S. Miller 	if (sock_owned_by_user(sk)) {
1174b8c84307SEric Dumazet 		kfree_skb(skb);
1175b8c84307SEric Dumazet 		ret = NET_XMIT_DROP;
11766af88da1SDavid S. Miller 		goto out_unlock;
11776af88da1SDavid S. Miller 	}
11786af88da1SDavid S. Miller 
1179fd558d18SJames Chapman 	/* Get routing info from the tunnel socket */
1180fd558d18SJames Chapman 	skb_dst_drop(skb);
118171b1391aSFlorian Westphal 	skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
11820d76751fSJames Chapman 
1183d9d8da80SDavid S. Miller 	inet = inet_sk(sk);
1184d9d8da80SDavid S. Miller 	fl = &inet->cork.fl;
11850d76751fSJames Chapman 	switch (tunnel->encap) {
11860d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
11870d76751fSJames Chapman 		/* Setup UDP header */
11880d76751fSJames Chapman 		__skb_push(skb, sizeof(*uh));
11890d76751fSJames Chapman 		skb_reset_transport_header(skb);
11900d76751fSJames Chapman 		uh = udp_hdr(skb);
11910d76751fSJames Chapman 		uh->source = inet->inet_sport;
11920d76751fSJames Chapman 		uh->dest = inet->inet_dport;
11930d76751fSJames Chapman 		udp_len = uhlen + hdr_len + data_len;
11940d76751fSJames Chapman 		uh->len = htons(udp_len);
1195fd558d18SJames Chapman 
1196fd558d18SJames Chapman 		/* Calculate UDP checksum if configured to do so */
1197d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1198e18503f4SFrançois Cachereul 		if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
119977157e19STom Herbert 			udp6_set_csum(udp_get_no_check6_tx(sk),
120077157e19STom Herbert 				      skb, &inet6_sk(sk)->saddr,
120177157e19STom Herbert 				      &sk->sk_v6_daddr, udp_len);
1202d2cf3361SBenjamin LaHaise 		else
1203d2cf3361SBenjamin LaHaise #endif
120477157e19STom Herbert 		udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
120577157e19STom Herbert 			     inet->inet_daddr, udp_len);
12060d76751fSJames Chapman 		break;
12070d76751fSJames Chapman 
12080d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
12090d76751fSJames Chapman 		break;
12100d76751fSJames Chapman 	}
12110d76751fSJames Chapman 
1212d9d8da80SDavid S. Miller 	l2tp_xmit_core(session, skb, fl, data_len);
12136af88da1SDavid S. Miller out_unlock:
12146af88da1SDavid S. Miller 	bh_unlock_sock(sk);
1215fd558d18SJames Chapman 
1216b8c84307SEric Dumazet 	return ret;
1217fd558d18SJames Chapman }
1218fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1219fd558d18SJames Chapman 
1220fd558d18SJames Chapman /*****************************************************************************
1221fd558d18SJames Chapman  * Tinnel and session create/destroy.
1222fd558d18SJames Chapman  *****************************************************************************/
1223fd558d18SJames Chapman 
1224fd558d18SJames Chapman /* Tunnel socket destruct hook.
1225fd558d18SJames Chapman  * The tunnel context is deleted only when all session sockets have been
1226fd558d18SJames Chapman  * closed.
1227fd558d18SJames Chapman  */
1228fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk)
1229fd558d18SJames Chapman {
12308d8a51e2SDavid S. Miller 	struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1231f8ccac0eSTom Parkin 	struct l2tp_net *pn;
1232fd558d18SJames Chapman 
1233fd558d18SJames Chapman 	if (tunnel == NULL)
1234fd558d18SJames Chapman 		goto end;
1235fd558d18SJames Chapman 
1236a4ca44faSJoe Perches 	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1237fd558d18SJames Chapman 
1238fd558d18SJames Chapman 
1239f8ccac0eSTom Parkin 	/* Disable udp encapsulation */
12400d76751fSJames Chapman 	switch (tunnel->encap) {
12410d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1242fd558d18SJames Chapman 		/* No longer an encapsulation socket. See net/ipv4/udp.c */
1243fd558d18SJames Chapman 		(udp_sk(sk))->encap_type = 0;
1244fd558d18SJames Chapman 		(udp_sk(sk))->encap_rcv = NULL;
12459980d001STom Parkin 		(udp_sk(sk))->encap_destroy = NULL;
12460d76751fSJames Chapman 		break;
12470d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
12480d76751fSJames Chapman 		break;
12490d76751fSJames Chapman 	}
1250fd558d18SJames Chapman 
1251fd558d18SJames Chapman 	/* Remove hooks into tunnel socket */
1252fd558d18SJames Chapman 	sk->sk_destruct = tunnel->old_sk_destruct;
1253fd558d18SJames Chapman 	sk->sk_user_data = NULL;
1254f8ccac0eSTom Parkin 	tunnel->sock = NULL;
1255f8ccac0eSTom Parkin 
1256f8ccac0eSTom Parkin 	/* Remove the tunnel struct from the tunnel list */
1257f8ccac0eSTom Parkin 	pn = l2tp_pernet(tunnel->l2tp_net);
1258f8ccac0eSTom Parkin 	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1259f8ccac0eSTom Parkin 	list_del_rcu(&tunnel->list);
1260f8ccac0eSTom Parkin 	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1261f8ccac0eSTom Parkin 	atomic_dec(&l2tp_tunnel_count);
1262f8ccac0eSTom Parkin 
1263f8ccac0eSTom Parkin 	l2tp_tunnel_closeall(tunnel);
1264f8ccac0eSTom Parkin 	l2tp_tunnel_dec_refcount(tunnel);
1265fd558d18SJames Chapman 
1266fd558d18SJames Chapman 	/* Call the original destructor */
1267fd558d18SJames Chapman 	if (sk->sk_destruct)
1268fd558d18SJames Chapman 		(*sk->sk_destruct)(sk);
1269fd558d18SJames Chapman end:
1270fd558d18SJames Chapman 	return;
1271fd558d18SJames Chapman }
1272fd558d18SJames Chapman 
1273fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too.
1274fd558d18SJames Chapman  */
1275e34f4c70STom Parkin void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1276fd558d18SJames Chapman {
1277fd558d18SJames Chapman 	int hash;
1278fd558d18SJames Chapman 	struct hlist_node *walk;
1279fd558d18SJames Chapman 	struct hlist_node *tmp;
1280fd558d18SJames Chapman 	struct l2tp_session *session;
1281fd558d18SJames Chapman 
1282fd558d18SJames Chapman 	BUG_ON(tunnel == NULL);
1283fd558d18SJames Chapman 
1284a4ca44faSJoe Perches 	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1285a4ca44faSJoe Perches 		  tunnel->name);
1286fd558d18SJames Chapman 
1287fd558d18SJames Chapman 	write_lock_bh(&tunnel->hlist_lock);
1288fd558d18SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1289fd558d18SJames Chapman again:
1290fd558d18SJames Chapman 		hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1291fd558d18SJames Chapman 			session = hlist_entry(walk, struct l2tp_session, hlist);
1292fd558d18SJames Chapman 
1293a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_CONTROL,
1294fd558d18SJames Chapman 				  "%s: closing session\n", session->name);
1295fd558d18SJames Chapman 
1296fd558d18SJames Chapman 			hlist_del_init(&session->hlist);
1297fd558d18SJames Chapman 
1298fd558d18SJames Chapman 			if (session->ref != NULL)
1299fd558d18SJames Chapman 				(*session->ref)(session);
1300fd558d18SJames Chapman 
1301fd558d18SJames Chapman 			write_unlock_bh(&tunnel->hlist_lock);
1302fd558d18SJames Chapman 
1303f6e16b29STom Parkin 			__l2tp_session_unhash(session);
13044c6e2fd3STom Parkin 			l2tp_session_queue_purge(session);
13054c6e2fd3STom Parkin 
1306fd558d18SJames Chapman 			if (session->session_close != NULL)
1307fd558d18SJames Chapman 				(*session->session_close)(session);
1308fd558d18SJames Chapman 
1309fd558d18SJames Chapman 			if (session->deref != NULL)
1310fd558d18SJames Chapman 				(*session->deref)(session);
1311fd558d18SJames Chapman 
13129980d001STom Parkin 			l2tp_session_dec_refcount(session);
13139980d001STom Parkin 
1314fd558d18SJames Chapman 			write_lock_bh(&tunnel->hlist_lock);
1315fd558d18SJames Chapman 
1316fd558d18SJames Chapman 			/* Now restart from the beginning of this hash
1317fd558d18SJames Chapman 			 * chain.  We always remove a session from the
1318fd558d18SJames Chapman 			 * list so we are guaranteed to make forward
1319fd558d18SJames Chapman 			 * progress.
1320fd558d18SJames Chapman 			 */
1321fd558d18SJames Chapman 			goto again;
1322fd558d18SJames Chapman 		}
1323fd558d18SJames Chapman 	}
1324fd558d18SJames Chapman 	write_unlock_bh(&tunnel->hlist_lock);
1325fd558d18SJames Chapman }
1326e34f4c70STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1327fd558d18SJames Chapman 
13289980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */
13299980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk)
13309980d001STom Parkin {
13319980d001STom Parkin 	struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
13329980d001STom Parkin 	if (tunnel) {
13339980d001STom Parkin 		l2tp_tunnel_closeall(tunnel);
13349980d001STom Parkin 		sock_put(sk);
13359980d001STom Parkin 	}
13369980d001STom Parkin }
13379980d001STom Parkin 
1338f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */
1339f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work)
1340f8ccac0eSTom Parkin {
1341f8ccac0eSTom Parkin 	struct l2tp_tunnel *tunnel = NULL;
1342f8ccac0eSTom Parkin 	struct socket *sock = NULL;
1343f8ccac0eSTom Parkin 	struct sock *sk = NULL;
1344f8ccac0eSTom Parkin 
1345f8ccac0eSTom Parkin 	tunnel = container_of(work, struct l2tp_tunnel, del_work);
134612d656afSRidge Kennedy 
134712d656afSRidge Kennedy 	l2tp_tunnel_closeall(tunnel);
134812d656afSRidge Kennedy 
1349f8ccac0eSTom Parkin 	sk = l2tp_tunnel_sock_lookup(tunnel);
1350f8ccac0eSTom Parkin 	if (!sk)
135106a15f51SAlexander Couzens 		goto out;
1352f8ccac0eSTom Parkin 
1353f8ccac0eSTom Parkin 	sock = sk->sk_socket;
1354f8ccac0eSTom Parkin 
135502d13ed5STom Parkin 	/* If the tunnel socket was created by userspace, then go through the
135602d13ed5STom Parkin 	 * inet layer to shut the socket down, and let userspace close it.
135702d13ed5STom Parkin 	 * Otherwise, if we created the socket directly within the kernel, use
135802d13ed5STom Parkin 	 * the sk API to release it here.
1359167eb17eSTom Parkin 	 * In either case the tunnel resources are freed in the socket
1360167eb17eSTom Parkin 	 * destructor when the tunnel socket goes away.
1361f8ccac0eSTom Parkin 	 */
136202d13ed5STom Parkin 	if (tunnel->fd >= 0) {
136302d13ed5STom Parkin 		if (sock)
136402d13ed5STom Parkin 			inet_shutdown(sock, 2);
136502d13ed5STom Parkin 	} else {
136626abe143SEric W. Biederman 		if (sock) {
1367167eb17eSTom Parkin 			kernel_sock_shutdown(sock, SHUT_RDWR);
136826abe143SEric W. Biederman 			sock_release(sock);
136926abe143SEric W. Biederman 		}
1370167eb17eSTom Parkin 	}
1371f8ccac0eSTom Parkin 
1372f8ccac0eSTom Parkin 	l2tp_tunnel_sock_put(sk);
137306a15f51SAlexander Couzens out:
137406a15f51SAlexander Couzens 	l2tp_tunnel_dec_refcount(tunnel);
1375fd558d18SJames Chapman }
1376fd558d18SJames Chapman 
1377789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by
1378789a4a2cSJames Chapman  * userspace. This is used for static tunnels where there is no
1379789a4a2cSJames Chapman  * managing L2TP daemon.
1380167eb17eSTom Parkin  *
1381167eb17eSTom Parkin  * Since we don't want these sockets to keep a namespace alive by
1382167eb17eSTom Parkin  * themselves, we drop the socket's namespace refcount after creation.
1383167eb17eSTom Parkin  * These sockets are freed when the namespace exits using the pernet
1384167eb17eSTom Parkin  * exit hook.
1385789a4a2cSJames Chapman  */
1386167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net,
1387167eb17eSTom Parkin 				u32 tunnel_id,
1388167eb17eSTom Parkin 				u32 peer_tunnel_id,
1389167eb17eSTom Parkin 				struct l2tp_tunnel_cfg *cfg,
1390167eb17eSTom Parkin 				struct socket **sockp)
1391789a4a2cSJames Chapman {
1392789a4a2cSJames Chapman 	int err = -EINVAL;
13937bddd0dbSEric Dumazet 	struct socket *sock = NULL;
139485644b4dSTom Herbert 	struct udp_port_cfg udp_conf;
1395789a4a2cSJames Chapman 
1396789a4a2cSJames Chapman 	switch (cfg->encap) {
1397789a4a2cSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
139885644b4dSTom Herbert 		memset(&udp_conf, 0, sizeof(udp_conf));
139985644b4dSTom Herbert 
1400f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
1401f9bac8dfSChris Elston 		if (cfg->local_ip6 && cfg->peer_ip6) {
140285644b4dSTom Herbert 			udp_conf.family = AF_INET6;
140385644b4dSTom Herbert 			memcpy(&udp_conf.local_ip6, cfg->local_ip6,
140485644b4dSTom Herbert 			       sizeof(udp_conf.local_ip6));
140585644b4dSTom Herbert 			memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
140685644b4dSTom Herbert 			       sizeof(udp_conf.peer_ip6));
140785644b4dSTom Herbert 			udp_conf.use_udp6_tx_checksums =
1408018f8258SWang Shanker 			  ! cfg->udp6_zero_tx_checksums;
140985644b4dSTom Herbert 			udp_conf.use_udp6_rx_checksums =
1410018f8258SWang Shanker 			  ! cfg->udp6_zero_rx_checksums;
1411f9bac8dfSChris Elston 		} else
1412f9bac8dfSChris Elston #endif
1413f9bac8dfSChris Elston 		{
141485644b4dSTom Herbert 			udp_conf.family = AF_INET;
141585644b4dSTom Herbert 			udp_conf.local_ip = cfg->local_ip;
141685644b4dSTom Herbert 			udp_conf.peer_ip = cfg->peer_ip;
141785644b4dSTom Herbert 			udp_conf.use_udp_checksums = cfg->use_udp_checksums;
1418f9bac8dfSChris Elston 		}
1419789a4a2cSJames Chapman 
142085644b4dSTom Herbert 		udp_conf.local_udp_port = htons(cfg->local_udp_port);
142185644b4dSTom Herbert 		udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
142285644b4dSTom Herbert 
142385644b4dSTom Herbert 		err = udp_sock_create(net, &udp_conf, &sock);
142485644b4dSTom Herbert 		if (err < 0)
142585644b4dSTom Herbert 			goto out;
1426789a4a2cSJames Chapman 
1427789a4a2cSJames Chapman 		break;
1428789a4a2cSJames Chapman 
1429789a4a2cSJames Chapman 	case L2TP_ENCAPTYPE_IP:
1430f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
1431f9bac8dfSChris Elston 		if (cfg->local_ip6 && cfg->peer_ip6) {
143285644b4dSTom Herbert 			struct sockaddr_l2tpip6 ip6_addr = {0};
143385644b4dSTom Herbert 
143426abe143SEric W. Biederman 			err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
1435167eb17eSTom Parkin 					  IPPROTO_L2TP, &sock);
14365dac94e1SJames Chapman 			if (err < 0)
1437f9bac8dfSChris Elston 				goto out;
14385dac94e1SJames Chapman 
14395dac94e1SJames Chapman 			ip6_addr.l2tp_family = AF_INET6;
14405dac94e1SJames Chapman 			memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
14415dac94e1SJames Chapman 			       sizeof(ip6_addr.l2tp_addr));
14425dac94e1SJames Chapman 			ip6_addr.l2tp_conn_id = tunnel_id;
14435dac94e1SJames Chapman 			err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
14445dac94e1SJames Chapman 					  sizeof(ip6_addr));
14455dac94e1SJames Chapman 			if (err < 0)
14465dac94e1SJames Chapman 				goto out;
14475dac94e1SJames Chapman 
14485dac94e1SJames Chapman 			ip6_addr.l2tp_family = AF_INET6;
14495dac94e1SJames Chapman 			memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
14505dac94e1SJames Chapman 			       sizeof(ip6_addr.l2tp_addr));
14515dac94e1SJames Chapman 			ip6_addr.l2tp_conn_id = peer_tunnel_id;
14525dac94e1SJames Chapman 			err = kernel_connect(sock,
14535dac94e1SJames Chapman 					     (struct sockaddr *) &ip6_addr,
14545dac94e1SJames Chapman 					     sizeof(ip6_addr), 0);
14555dac94e1SJames Chapman 			if (err < 0)
14565dac94e1SJames Chapman 				goto out;
14575dac94e1SJames Chapman 		} else
1458f9bac8dfSChris Elston #endif
14595dac94e1SJames Chapman 		{
146085644b4dSTom Herbert 			struct sockaddr_l2tpip ip_addr = {0};
146185644b4dSTom Herbert 
146226abe143SEric W. Biederman 			err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
1463167eb17eSTom Parkin 					  IPPROTO_L2TP, &sock);
1464789a4a2cSJames Chapman 			if (err < 0)
1465789a4a2cSJames Chapman 				goto out;
1466789a4a2cSJames Chapman 
1467789a4a2cSJames Chapman 			ip_addr.l2tp_family = AF_INET;
1468789a4a2cSJames Chapman 			ip_addr.l2tp_addr = cfg->local_ip;
1469789a4a2cSJames Chapman 			ip_addr.l2tp_conn_id = tunnel_id;
14705dac94e1SJames Chapman 			err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
14715dac94e1SJames Chapman 					  sizeof(ip_addr));
1472789a4a2cSJames Chapman 			if (err < 0)
1473789a4a2cSJames Chapman 				goto out;
1474789a4a2cSJames Chapman 
1475789a4a2cSJames Chapman 			ip_addr.l2tp_family = AF_INET;
1476789a4a2cSJames Chapman 			ip_addr.l2tp_addr = cfg->peer_ip;
1477789a4a2cSJames Chapman 			ip_addr.l2tp_conn_id = peer_tunnel_id;
14785dac94e1SJames Chapman 			err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
14795dac94e1SJames Chapman 					     sizeof(ip_addr), 0);
1480789a4a2cSJames Chapman 			if (err < 0)
1481789a4a2cSJames Chapman 				goto out;
14825dac94e1SJames Chapman 		}
1483789a4a2cSJames Chapman 		break;
1484789a4a2cSJames Chapman 
1485789a4a2cSJames Chapman 	default:
1486789a4a2cSJames Chapman 		goto out;
1487789a4a2cSJames Chapman 	}
1488789a4a2cSJames Chapman 
1489789a4a2cSJames Chapman out:
1490167eb17eSTom Parkin 	*sockp = sock;
1491789a4a2cSJames Chapman 	if ((err < 0) && sock) {
1492167eb17eSTom Parkin 		kernel_sock_shutdown(sock, SHUT_RDWR);
149326abe143SEric W. Biederman 		sock_release(sock);
1494789a4a2cSJames Chapman 		*sockp = NULL;
1495789a4a2cSJames Chapman 	}
1496789a4a2cSJames Chapman 
1497789a4a2cSJames Chapman 	return err;
1498789a4a2cSJames Chapman }
1499789a4a2cSJames Chapman 
150037159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class;
150137159ef2SEric Dumazet 
1502fd558d18SJames 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)
1503fd558d18SJames Chapman {
1504fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = NULL;
1505fd558d18SJames Chapman 	int err;
1506fd558d18SJames Chapman 	struct socket *sock = NULL;
1507fd558d18SJames Chapman 	struct sock *sk = NULL;
1508fd558d18SJames Chapman 	struct l2tp_net *pn;
15090d76751fSJames Chapman 	enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1510fd558d18SJames Chapman 
1511fd558d18SJames Chapman 	/* Get the tunnel socket from the fd, which was opened by
1512789a4a2cSJames Chapman 	 * the userspace L2TP daemon. If not specified, create a
1513789a4a2cSJames Chapman 	 * kernel socket.
1514fd558d18SJames Chapman 	 */
1515789a4a2cSJames Chapman 	if (fd < 0) {
1516167eb17eSTom Parkin 		err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1517167eb17eSTom Parkin 				cfg, &sock);
1518789a4a2cSJames Chapman 		if (err < 0)
1519789a4a2cSJames Chapman 			goto err;
1520789a4a2cSJames Chapman 	} else {
1521fd558d18SJames Chapman 		sock = sockfd_lookup(fd, &err);
1522fd558d18SJames Chapman 		if (!sock) {
1523cbb95e0cSTom Parkin 			pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1524fd558d18SJames Chapman 			       tunnel_id, fd, err);
1525cbb95e0cSTom Parkin 			err = -EBADF;
1526cbb95e0cSTom Parkin 			goto err;
1527cbb95e0cSTom Parkin 		}
1528cbb95e0cSTom Parkin 
1529cbb95e0cSTom Parkin 		/* Reject namespace mismatches */
1530cbb95e0cSTom Parkin 		if (!net_eq(sock_net(sock->sk), net)) {
1531cbb95e0cSTom Parkin 			pr_err("tunl %u: netns mismatch\n", tunnel_id);
1532cbb95e0cSTom Parkin 			err = -EINVAL;
1533fd558d18SJames Chapman 			goto err;
1534fd558d18SJames Chapman 		}
1535789a4a2cSJames Chapman 	}
1536fd558d18SJames Chapman 
1537fd558d18SJames Chapman 	sk = sock->sk;
1538fd558d18SJames Chapman 
15390d76751fSJames Chapman 	if (cfg != NULL)
15400d76751fSJames Chapman 		encap = cfg->encap;
15410d76751fSJames Chapman 
1542fd558d18SJames Chapman 	/* Quick sanity checks */
15430d76751fSJames Chapman 	switch (encap) {
15440d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1545fd558d18SJames Chapman 		err = -EPROTONOSUPPORT;
1546fd558d18SJames Chapman 		if (sk->sk_protocol != IPPROTO_UDP) {
1547a4ca44faSJoe Perches 			pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1548fd558d18SJames Chapman 			       tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1549fd558d18SJames Chapman 			goto err;
1550fd558d18SJames Chapman 		}
15510d76751fSJames Chapman 		break;
15520d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
15530d76751fSJames Chapman 		err = -EPROTONOSUPPORT;
15540d76751fSJames Chapman 		if (sk->sk_protocol != IPPROTO_L2TP) {
1555a4ca44faSJoe Perches 			pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
15560d76751fSJames Chapman 			       tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1557fd558d18SJames Chapman 			goto err;
1558fd558d18SJames Chapman 		}
15590d76751fSJames Chapman 		break;
15600d76751fSJames Chapman 	}
1561fd558d18SJames Chapman 
1562fd558d18SJames Chapman 	/* Check if this socket has already been prepped */
15638d8a51e2SDavid S. Miller 	tunnel = l2tp_tunnel(sk);
1564fd558d18SJames Chapman 	if (tunnel != NULL) {
1565fd558d18SJames Chapman 		/* This socket has already been prepped */
1566fd558d18SJames Chapman 		err = -EBUSY;
1567fd558d18SJames Chapman 		goto err;
1568fd558d18SJames Chapman 	}
1569fd558d18SJames Chapman 
1570fd558d18SJames Chapman 	tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1571fd558d18SJames Chapman 	if (tunnel == NULL) {
1572fd558d18SJames Chapman 		err = -ENOMEM;
1573fd558d18SJames Chapman 		goto err;
1574fd558d18SJames Chapman 	}
1575fd558d18SJames Chapman 
1576fd558d18SJames Chapman 	tunnel->version = version;
1577fd558d18SJames Chapman 	tunnel->tunnel_id = tunnel_id;
1578fd558d18SJames Chapman 	tunnel->peer_tunnel_id = peer_tunnel_id;
1579fd558d18SJames Chapman 	tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1580fd558d18SJames Chapman 
1581fd558d18SJames Chapman 	tunnel->magic = L2TP_TUNNEL_MAGIC;
1582fd558d18SJames Chapman 	sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1583fd558d18SJames Chapman 	rwlock_init(&tunnel->hlist_lock);
1584fd558d18SJames Chapman 
1585fd558d18SJames Chapman 	/* The net we belong to */
1586fd558d18SJames Chapman 	tunnel->l2tp_net = net;
1587fd558d18SJames Chapman 	pn = l2tp_pernet(net);
1588fd558d18SJames Chapman 
15890d76751fSJames Chapman 	if (cfg != NULL)
1590fd558d18SJames Chapman 		tunnel->debug = cfg->debug;
1591fd558d18SJames Chapman 
1592e18503f4SFrançois Cachereul #if IS_ENABLED(CONFIG_IPV6)
1593e18503f4SFrançois Cachereul 	if (sk->sk_family == PF_INET6) {
1594e18503f4SFrançois Cachereul 		struct ipv6_pinfo *np = inet6_sk(sk);
1595e18503f4SFrançois Cachereul 
1596e18503f4SFrançois Cachereul 		if (ipv6_addr_v4mapped(&np->saddr) &&
1597efe4208fSEric Dumazet 		    ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
1598e18503f4SFrançois Cachereul 			struct inet_sock *inet = inet_sk(sk);
1599e18503f4SFrançois Cachereul 
1600e18503f4SFrançois Cachereul 			tunnel->v4mapped = true;
1601e18503f4SFrançois Cachereul 			inet->inet_saddr = np->saddr.s6_addr32[3];
1602efe4208fSEric Dumazet 			inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1603efe4208fSEric Dumazet 			inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
1604e18503f4SFrançois Cachereul 		} else {
1605e18503f4SFrançois Cachereul 			tunnel->v4mapped = false;
1606e18503f4SFrançois Cachereul 		}
1607e18503f4SFrançois Cachereul 	}
1608e18503f4SFrançois Cachereul #endif
1609e18503f4SFrançois Cachereul 
1610fd558d18SJames Chapman 	/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
16110d76751fSJames Chapman 	tunnel->encap = encap;
16120d76751fSJames Chapman 	if (encap == L2TP_ENCAPTYPE_UDP) {
1613a5c5e2daSGuillaume Nault 		struct udp_tunnel_sock_cfg udp_cfg = { };
1614fd558d18SJames Chapman 
1615c8fffceaSAndy Zhou 		udp_cfg.sk_user_data = tunnel;
1616c8fffceaSAndy Zhou 		udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1617c8fffceaSAndy Zhou 		udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1618c8fffceaSAndy Zhou 		udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1619c8fffceaSAndy Zhou 
1620c8fffceaSAndy Zhou 		setup_udp_tunnel_sock(net, sock, &udp_cfg);
1621c8fffceaSAndy Zhou 	} else {
1622fd558d18SJames Chapman 		sk->sk_user_data = tunnel;
1623c8fffceaSAndy Zhou 	}
1624fd558d18SJames Chapman 
1625fd558d18SJames Chapman 	/* Hook on the tunnel socket destructor so that we can cleanup
1626fd558d18SJames Chapman 	 * if the tunnel socket goes away.
1627fd558d18SJames Chapman 	 */
1628fd558d18SJames Chapman 	tunnel->old_sk_destruct = sk->sk_destruct;
1629fd558d18SJames Chapman 	sk->sk_destruct = &l2tp_tunnel_destruct;
1630fd558d18SJames Chapman 	tunnel->sock = sk;
163180d84ef3STom Parkin 	tunnel->fd = fd;
163237159ef2SEric Dumazet 	lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
163337159ef2SEric Dumazet 
1634fd558d18SJames Chapman 	sk->sk_allocation = GFP_ATOMIC;
1635fd558d18SJames Chapman 
1636f8ccac0eSTom Parkin 	/* Init delete workqueue struct */
1637f8ccac0eSTom Parkin 	INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1638f8ccac0eSTom Parkin 
1639fd558d18SJames Chapman 	/* Add tunnel to our list */
1640fd558d18SJames Chapman 	INIT_LIST_HEAD(&tunnel->list);
1641fd558d18SJames Chapman 	atomic_inc(&l2tp_tunnel_count);
1642fd558d18SJames Chapman 
1643fd558d18SJames Chapman 	/* Bump the reference count. The tunnel context is deleted
16441769192aSEric Dumazet 	 * only when this drops to zero. Must be done before list insertion
1645fd558d18SJames Chapman 	 */
1646fbea9e07SReshetova, Elena 	refcount_set(&tunnel->ref_count, 1);
16471769192aSEric Dumazet 	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
16481769192aSEric Dumazet 	list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
16491769192aSEric Dumazet 	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1650fd558d18SJames Chapman 
1651fd558d18SJames Chapman 	err = 0;
1652fd558d18SJames Chapman err:
1653fd558d18SJames Chapman 	if (tunnelp)
1654fd558d18SJames Chapman 		*tunnelp = tunnel;
1655fd558d18SJames Chapman 
1656789a4a2cSJames Chapman 	/* If tunnel's socket was created by the kernel, it doesn't
1657789a4a2cSJames Chapman 	 *  have a file.
1658789a4a2cSJames Chapman 	 */
1659789a4a2cSJames Chapman 	if (sock && sock->file)
1660fd558d18SJames Chapman 		sockfd_put(sock);
1661fd558d18SJames Chapman 
1662fd558d18SJames Chapman 	return err;
1663fd558d18SJames Chapman }
1664fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1665fd558d18SJames Chapman 
1666309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command.
1667309795f4SJames Chapman  */
1668309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1669309795f4SJames Chapman {
167006a15f51SAlexander Couzens 	l2tp_tunnel_inc_refcount(tunnel);
167106a15f51SAlexander Couzens 	if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
167206a15f51SAlexander Couzens 		l2tp_tunnel_dec_refcount(tunnel);
167306a15f51SAlexander Couzens 		return 1;
167406a15f51SAlexander Couzens 	}
167506a15f51SAlexander Couzens 	return 0;
1676309795f4SJames Chapman }
1677309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1678309795f4SJames Chapman 
1679fd558d18SJames Chapman /* Really kill the session.
1680fd558d18SJames Chapman  */
1681fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session)
1682fd558d18SJames Chapman {
1683f6e16b29STom Parkin 	struct l2tp_tunnel *tunnel = session->tunnel;
1684fd558d18SJames Chapman 
1685fbea9e07SReshetova, Elena 	BUG_ON(refcount_read(&session->ref_count) != 0);
1686fd558d18SJames Chapman 
1687f6e16b29STom Parkin 	if (tunnel) {
1688fd558d18SJames Chapman 		BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1689fd558d18SJames Chapman 		if (session->session_id != 0)
1690fd558d18SJames Chapman 			atomic_dec(&l2tp_session_count);
1691fd558d18SJames Chapman 		sock_put(tunnel->sock);
1692fd558d18SJames Chapman 		session->tunnel = NULL;
1693fd558d18SJames Chapman 		l2tp_tunnel_dec_refcount(tunnel);
1694fd558d18SJames Chapman 	}
1695fd558d18SJames Chapman 
1696fd558d18SJames Chapman 	kfree(session);
1697fd558d18SJames Chapman }
1698fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free);
1699fd558d18SJames Chapman 
1700f6e16b29STom Parkin /* Remove an l2tp session from l2tp_core's hash lists.
1701f6e16b29STom Parkin  * Provides a tidyup interface for pseudowire code which can't just route all
1702f6e16b29STom Parkin  * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1703f6e16b29STom Parkin  * callback.
1704f6e16b29STom Parkin  */
1705f6e16b29STom Parkin void __l2tp_session_unhash(struct l2tp_session *session)
1706f6e16b29STom Parkin {
1707f6e16b29STom Parkin 	struct l2tp_tunnel *tunnel = session->tunnel;
1708f6e16b29STom Parkin 
1709f6e16b29STom Parkin 	/* Remove the session from core hashes */
1710f6e16b29STom Parkin 	if (tunnel) {
1711f6e16b29STom Parkin 		/* Remove from the per-tunnel hash */
1712f6e16b29STom Parkin 		write_lock_bh(&tunnel->hlist_lock);
1713f6e16b29STom Parkin 		hlist_del_init(&session->hlist);
1714f6e16b29STom Parkin 		write_unlock_bh(&tunnel->hlist_lock);
1715f6e16b29STom Parkin 
1716f6e16b29STom Parkin 		/* For L2TPv3 we have a per-net hash: remove from there, too */
1717f6e16b29STom Parkin 		if (tunnel->version != L2TP_HDR_VER_2) {
1718f6e16b29STom Parkin 			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1719f6e16b29STom Parkin 			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1720f6e16b29STom Parkin 			hlist_del_init_rcu(&session->global_hlist);
1721f6e16b29STom Parkin 			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1722f6e16b29STom Parkin 			synchronize_rcu();
1723f6e16b29STom Parkin 		}
1724f6e16b29STom Parkin 	}
1725f6e16b29STom Parkin }
1726f6e16b29STom Parkin EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1727f6e16b29STom Parkin 
1728309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by
1729309795f4SJames Chapman    pseudowire modules.
1730309795f4SJames Chapman  */
1731309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session)
1732309795f4SJames Chapman {
1733f6e16b29STom Parkin 	if (session->ref)
1734f6e16b29STom Parkin 		(*session->ref)(session);
1735f6e16b29STom Parkin 	__l2tp_session_unhash(session);
17364c6e2fd3STom Parkin 	l2tp_session_queue_purge(session);
1737309795f4SJames Chapman 	if (session->session_close != NULL)
1738309795f4SJames Chapman 		(*session->session_close)(session);
1739f6e16b29STom Parkin 	if (session->deref)
17401b7c92b9SDan Carpenter 		(*session->deref)(session);
1741309795f4SJames Chapman 	l2tp_session_dec_refcount(session);
1742309795f4SJames Chapman 	return 0;
1743309795f4SJames Chapman }
1744309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete);
1745309795f4SJames Chapman 
1746f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or
1747f7faffa3SJames Chapman  * l2specific_len parameters are set.
1748f7faffa3SJames Chapman  */
1749bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1750f7faffa3SJames Chapman {
1751f7faffa3SJames Chapman 	if (version == L2TP_HDR_VER_2) {
1752f7faffa3SJames Chapman 		session->hdr_len = 6;
1753f7faffa3SJames Chapman 		if (session->send_seq)
1754f7faffa3SJames Chapman 			session->hdr_len += 4;
1755f7faffa3SJames Chapman 	} else {
17560d76751fSJames Chapman 		session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
17570d76751fSJames Chapman 		if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
17580d76751fSJames Chapman 			session->hdr_len += 4;
1759f7faffa3SJames Chapman 	}
1760f7faffa3SJames Chapman 
1761f7faffa3SJames Chapman }
1762bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1763f7faffa3SJames Chapman 
1764fd558d18SJames 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)
1765fd558d18SJames Chapman {
1766fd558d18SJames Chapman 	struct l2tp_session *session;
1767dbdbc73bSGuillaume Nault 	int err;
1768fd558d18SJames Chapman 
1769fd558d18SJames Chapman 	session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1770fd558d18SJames Chapman 	if (session != NULL) {
1771fd558d18SJames Chapman 		session->magic = L2TP_SESSION_MAGIC;
1772fd558d18SJames Chapman 		session->tunnel = tunnel;
1773fd558d18SJames Chapman 
1774fd558d18SJames Chapman 		session->session_id = session_id;
1775fd558d18SJames Chapman 		session->peer_session_id = peer_session_id;
1776d301e325SJames Chapman 		session->nr = 0;
17778a1631d5SJames Chapman 		if (tunnel->version == L2TP_HDR_VER_2)
17788a1631d5SJames Chapman 			session->nr_max = 0xffff;
17798a1631d5SJames Chapman 		else
17808a1631d5SJames Chapman 			session->nr_max = 0xffffff;
17818a1631d5SJames Chapman 		session->nr_window_size = session->nr_max / 2;
1782a0dbd822SJames Chapman 		session->nr_oos_count_max = 4;
1783a0dbd822SJames Chapman 
1784a0dbd822SJames Chapman 		/* Use NR of first received packet */
1785a0dbd822SJames Chapman 		session->reorder_skip = 1;
1786fd558d18SJames Chapman 
1787fd558d18SJames Chapman 		sprintf(&session->name[0], "sess %u/%u",
1788fd558d18SJames Chapman 			tunnel->tunnel_id, session->session_id);
1789fd558d18SJames Chapman 
1790fd558d18SJames Chapman 		skb_queue_head_init(&session->reorder_q);
1791fd558d18SJames Chapman 
1792fd558d18SJames Chapman 		INIT_HLIST_NODE(&session->hlist);
1793f7faffa3SJames Chapman 		INIT_HLIST_NODE(&session->global_hlist);
1794fd558d18SJames Chapman 
1795fd558d18SJames Chapman 		/* Inherit debug options from tunnel */
1796fd558d18SJames Chapman 		session->debug = tunnel->debug;
1797fd558d18SJames Chapman 
1798fd558d18SJames Chapman 		if (cfg) {
1799f7faffa3SJames Chapman 			session->pwtype = cfg->pw_type;
1800fd558d18SJames Chapman 			session->debug = cfg->debug;
1801fd558d18SJames Chapman 			session->mtu = cfg->mtu;
1802fd558d18SJames Chapman 			session->mru = cfg->mru;
1803fd558d18SJames Chapman 			session->send_seq = cfg->send_seq;
1804fd558d18SJames Chapman 			session->recv_seq = cfg->recv_seq;
1805fd558d18SJames Chapman 			session->lns_mode = cfg->lns_mode;
1806f7faffa3SJames Chapman 			session->reorder_timeout = cfg->reorder_timeout;
1807f7faffa3SJames Chapman 			session->offset = cfg->offset;
1808f7faffa3SJames Chapman 			session->l2specific_type = cfg->l2specific_type;
1809f7faffa3SJames Chapman 			session->l2specific_len = cfg->l2specific_len;
1810f7faffa3SJames Chapman 			session->cookie_len = cfg->cookie_len;
1811f7faffa3SJames Chapman 			memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1812f7faffa3SJames Chapman 			session->peer_cookie_len = cfg->peer_cookie_len;
1813f7faffa3SJames Chapman 			memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1814fd558d18SJames Chapman 		}
1815fd558d18SJames Chapman 
1816f7faffa3SJames Chapman 		if (tunnel->version == L2TP_HDR_VER_2)
1817f7faffa3SJames Chapman 			session->build_header = l2tp_build_l2tpv2_header;
1818f7faffa3SJames Chapman 		else
1819f7faffa3SJames Chapman 			session->build_header = l2tp_build_l2tpv3_header;
1820f7faffa3SJames Chapman 
1821f7faffa3SJames Chapman 		l2tp_session_set_header_len(session, tunnel->version);
1822f7faffa3SJames Chapman 
18239ee369a4SGuillaume Nault 		refcount_set(&session->ref_count, 1);
18249ee369a4SGuillaume Nault 
1825dbdbc73bSGuillaume Nault 		err = l2tp_session_add_to_tunnel(tunnel, session);
1826dbdbc73bSGuillaume Nault 		if (err) {
1827dbdbc73bSGuillaume Nault 			kfree(session);
1828dbdbc73bSGuillaume Nault 
1829dbdbc73bSGuillaume Nault 			return ERR_PTR(err);
1830dbdbc73bSGuillaume Nault 		}
1831dbdbc73bSGuillaume Nault 
1832fd558d18SJames Chapman 		l2tp_tunnel_inc_refcount(tunnel);
1833fd558d18SJames Chapman 
1834fd558d18SJames Chapman 		/* Ensure tunnel socket isn't deleted */
1835fd558d18SJames Chapman 		sock_hold(tunnel->sock);
1836fd558d18SJames Chapman 
1837fd558d18SJames Chapman 		/* Ignore management session in session count value */
1838fd558d18SJames Chapman 		if (session->session_id != 0)
1839fd558d18SJames Chapman 			atomic_inc(&l2tp_session_count);
1840fd558d18SJames Chapman 
1841fd558d18SJames Chapman 		return session;
1842fd558d18SJames Chapman 	}
1843dbdbc73bSGuillaume Nault 
1844dbdbc73bSGuillaume Nault 	return ERR_PTR(-ENOMEM);
1845dbdbc73bSGuillaume Nault }
1846fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create);
1847fd558d18SJames Chapman 
1848fd558d18SJames Chapman /*****************************************************************************
1849fd558d18SJames Chapman  * Init and cleanup
1850fd558d18SJames Chapman  *****************************************************************************/
1851fd558d18SJames Chapman 
1852fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net)
1853fd558d18SJames Chapman {
1854e773aaffSJiri Pirko 	struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1855f7faffa3SJames Chapman 	int hash;
1856fd558d18SJames Chapman 
1857fd558d18SJames Chapman 	INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1858e02d494dSJames Chapman 	spin_lock_init(&pn->l2tp_tunnel_list_lock);
1859fd558d18SJames Chapman 
1860f7faffa3SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1861f7faffa3SJames Chapman 		INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1862f7faffa3SJames Chapman 
1863e02d494dSJames Chapman 	spin_lock_init(&pn->l2tp_session_hlist_lock);
1864f7faffa3SJames Chapman 
1865fd558d18SJames Chapman 	return 0;
1866fd558d18SJames Chapman }
1867fd558d18SJames Chapman 
1868167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net)
1869167eb17eSTom Parkin {
1870167eb17eSTom Parkin 	struct l2tp_net *pn = l2tp_pernet(net);
1871167eb17eSTom Parkin 	struct l2tp_tunnel *tunnel = NULL;
1872167eb17eSTom Parkin 
1873167eb17eSTom Parkin 	rcu_read_lock_bh();
1874167eb17eSTom Parkin 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1875167eb17eSTom Parkin 		(void)l2tp_tunnel_delete(tunnel);
1876167eb17eSTom Parkin 	}
1877167eb17eSTom Parkin 	rcu_read_unlock_bh();
18782f86953eSSabrina Dubroca 
18792f86953eSSabrina Dubroca 	flush_workqueue(l2tp_wq);
18802f86953eSSabrina Dubroca 	rcu_barrier();
1881167eb17eSTom Parkin }
1882167eb17eSTom Parkin 
1883fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = {
1884fd558d18SJames Chapman 	.init = l2tp_init_net,
1885167eb17eSTom Parkin 	.exit = l2tp_exit_net,
1886fd558d18SJames Chapman 	.id   = &l2tp_net_id,
1887fd558d18SJames Chapman 	.size = sizeof(struct l2tp_net),
1888fd558d18SJames Chapman };
1889fd558d18SJames Chapman 
1890fd558d18SJames Chapman static int __init l2tp_init(void)
1891fd558d18SJames Chapman {
1892fd558d18SJames Chapman 	int rc = 0;
1893fd558d18SJames Chapman 
1894fd558d18SJames Chapman 	rc = register_pernet_device(&l2tp_net_ops);
1895fd558d18SJames Chapman 	if (rc)
1896fd558d18SJames Chapman 		goto out;
1897fd558d18SJames Chapman 
189859ff3eb6SZhangZhen 	l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1899f8ccac0eSTom Parkin 	if (!l2tp_wq) {
1900f8ccac0eSTom Parkin 		pr_err("alloc_workqueue failed\n");
190167e04c29SWANG Cong 		unregister_pernet_device(&l2tp_net_ops);
1902f8ccac0eSTom Parkin 		rc = -ENOMEM;
1903f8ccac0eSTom Parkin 		goto out;
1904f8ccac0eSTom Parkin 	}
1905f8ccac0eSTom Parkin 
1906a4ca44faSJoe Perches 	pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1907fd558d18SJames Chapman 
1908fd558d18SJames Chapman out:
1909fd558d18SJames Chapman 	return rc;
1910fd558d18SJames Chapman }
1911fd558d18SJames Chapman 
1912fd558d18SJames Chapman static void __exit l2tp_exit(void)
1913fd558d18SJames Chapman {
1914fd558d18SJames Chapman 	unregister_pernet_device(&l2tp_net_ops);
1915f8ccac0eSTom Parkin 	if (l2tp_wq) {
1916f8ccac0eSTom Parkin 		destroy_workqueue(l2tp_wq);
1917f8ccac0eSTom Parkin 		l2tp_wq = NULL;
1918f8ccac0eSTom Parkin 	}
1919fd558d18SJames Chapman }
1920fd558d18SJames Chapman 
1921fd558d18SJames Chapman module_init(l2tp_init);
1922fd558d18SJames Chapman module_exit(l2tp_exit);
1923fd558d18SJames Chapman 
1924fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1925fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core");
1926fd558d18SJames Chapman MODULE_LICENSE("GPL");
1927fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION);
1928fd558d18SJames Chapman 
1929