xref: /openbmc/linux/net/l2tp/l2tp_core.c (revision b0270e91)
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>
55309795f4SJames Chapman #include <net/inet_common.h>
56fd558d18SJames Chapman #include <net/xfrm.h>
570d76751fSJames Chapman #include <net/protocol.h>
58d2cf3361SBenjamin LaHaise #include <net/inet6_connection_sock.h>
59d2cf3361SBenjamin LaHaise #include <net/inet_ecn.h>
60d2cf3361SBenjamin LaHaise #include <net/ip6_route.h>
61d499bd2eSDavid S. Miller #include <net/ip6_checksum.h>
62fd558d18SJames Chapman 
63fd558d18SJames Chapman #include <asm/byteorder.h>
6460063497SArun Sharma #include <linux/atomic.h>
65fd558d18SJames Chapman 
66fd558d18SJames Chapman #include "l2tp_core.h"
67fd558d18SJames Chapman 
68fd558d18SJames Chapman #define L2TP_DRV_VERSION	"V2.0"
69fd558d18SJames Chapman 
70fd558d18SJames Chapman /* L2TP header constants */
71fd558d18SJames Chapman #define L2TP_HDRFLAG_T	   0x8000
72fd558d18SJames Chapman #define L2TP_HDRFLAG_L	   0x4000
73fd558d18SJames Chapman #define L2TP_HDRFLAG_S	   0x0800
74fd558d18SJames Chapman #define L2TP_HDRFLAG_O	   0x0200
75fd558d18SJames Chapman #define L2TP_HDRFLAG_P	   0x0100
76fd558d18SJames Chapman 
77fd558d18SJames Chapman #define L2TP_HDR_VER_MASK  0x000F
78fd558d18SJames Chapman #define L2TP_HDR_VER_2	   0x0002
79f7faffa3SJames Chapman #define L2TP_HDR_VER_3	   0x0003
80fd558d18SJames Chapman 
81fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */
82fd558d18SJames Chapman #define L2TP_SLFLAG_S	   0x40000000
83fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK   0x00ffffff
84fd558d18SJames Chapman 
85fd558d18SJames Chapman #define L2TP_HDR_SIZE_SEQ		10
86fd558d18SJames Chapman #define L2TP_HDR_SIZE_NOSEQ		6
87fd558d18SJames Chapman 
88fd558d18SJames Chapman /* Default trace flags */
89fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS	0
90fd558d18SJames Chapman 
91fd558d18SJames Chapman /* Private data stored for received packets in the skb.
92fd558d18SJames Chapman  */
93fd558d18SJames Chapman struct l2tp_skb_cb {
94f7faffa3SJames Chapman 	u32			ns;
95fd558d18SJames Chapman 	u16			has_seq;
96fd558d18SJames Chapman 	u16			length;
97fd558d18SJames Chapman 	unsigned long		expires;
98fd558d18SJames Chapman };
99fd558d18SJames Chapman 
100fd558d18SJames Chapman #define L2TP_SKB_CB(skb)	((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101fd558d18SJames Chapman 
102fd558d18SJames Chapman static atomic_t l2tp_tunnel_count;
103fd558d18SJames Chapman static atomic_t l2tp_session_count;
104f8ccac0eSTom Parkin static struct workqueue_struct *l2tp_wq;
105fd558d18SJames Chapman 
106fd558d18SJames Chapman /* per-net private data for this module */
107fd558d18SJames Chapman static unsigned int l2tp_net_id;
108fd558d18SJames Chapman struct l2tp_net {
109fd558d18SJames Chapman 	struct list_head l2tp_tunnel_list;
110e02d494dSJames Chapman 	spinlock_t l2tp_tunnel_list_lock;
111f7faffa3SJames Chapman 	struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
112e02d494dSJames Chapman 	spinlock_t l2tp_session_hlist_lock;
113fd558d18SJames Chapman };
114fd558d18SJames Chapman 
115fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
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 
122fd558d18SJames Chapman static inline struct l2tp_net *l2tp_pernet(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 
129fc130840Sstephen hemminger /* Tunnel reference counts. Incremented per session that is added to
130fc130840Sstephen hemminger  * the tunnel.
131fc130840Sstephen hemminger  */
132fc130840Sstephen hemminger static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
133fc130840Sstephen hemminger {
134fc130840Sstephen hemminger 	atomic_inc(&tunnel->ref_count);
135fc130840Sstephen hemminger }
136fc130840Sstephen hemminger 
137fc130840Sstephen hemminger static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
138fc130840Sstephen hemminger {
139fc130840Sstephen hemminger 	if (atomic_dec_and_test(&tunnel->ref_count))
140fc130840Sstephen hemminger 		l2tp_tunnel_free(tunnel);
141fc130840Sstephen hemminger }
142fc130840Sstephen hemminger #ifdef L2TP_REFCNT_DEBUG
143a4ca44faSJoe Perches #define l2tp_tunnel_inc_refcount(_t)					\
144a4ca44faSJoe Perches do {									\
145a4ca44faSJoe Perches 	pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n",	\
146a4ca44faSJoe Perches 		 __func__, __LINE__, (_t)->name,			\
147a4ca44faSJoe Perches 		 atomic_read(&_t->ref_count));				\
148fc130840Sstephen hemminger 	l2tp_tunnel_inc_refcount_1(_t);					\
149fc130840Sstephen hemminger } while (0)
150a4ca44faSJoe Perches #define l2tp_tunnel_dec_refcount(_t)
151a4ca44faSJoe Perches do {									\
152a4ca44faSJoe Perches 	pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n",	\
153a4ca44faSJoe Perches 		 __func__, __LINE__, (_t)->name,			\
154a4ca44faSJoe Perches 		 atomic_read(&_t->ref_count));				\
155fc130840Sstephen hemminger 	l2tp_tunnel_dec_refcount_1(_t);					\
156fc130840Sstephen hemminger } while (0)
157fc130840Sstephen hemminger #else
158fc130840Sstephen hemminger #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
159fc130840Sstephen hemminger #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
160fc130840Sstephen hemminger #endif
161fc130840Sstephen hemminger 
162f7faffa3SJames Chapman /* Session hash global list for L2TPv3.
163f7faffa3SJames Chapman  * The session_id SHOULD be random according to RFC3931, but several
164f7faffa3SJames Chapman  * L2TP implementations use incrementing session_ids.  So we do a real
165f7faffa3SJames Chapman  * hash on the session_id, rather than a simple bitmask.
166f7faffa3SJames Chapman  */
167f7faffa3SJames Chapman static inline struct hlist_head *
168f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
169f7faffa3SJames Chapman {
170f7faffa3SJames Chapman 	return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
171f7faffa3SJames Chapman 
172f7faffa3SJames Chapman }
173f7faffa3SJames Chapman 
17480d84ef3STom Parkin /* Lookup the tunnel socket, possibly involving the fs code if the socket is
17580d84ef3STom Parkin  * owned by userspace.  A struct sock returned from this function must be
17680d84ef3STom Parkin  * released using l2tp_tunnel_sock_put once you're done with it.
17780d84ef3STom Parkin  */
178b5d2b285Sstephen hemminger static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
17980d84ef3STom Parkin {
18080d84ef3STom Parkin 	int err = 0;
18180d84ef3STom Parkin 	struct socket *sock = NULL;
18280d84ef3STom Parkin 	struct sock *sk = NULL;
18380d84ef3STom Parkin 
18480d84ef3STom Parkin 	if (!tunnel)
18580d84ef3STom Parkin 		goto out;
18680d84ef3STom Parkin 
18780d84ef3STom Parkin 	if (tunnel->fd >= 0) {
18880d84ef3STom Parkin 		/* Socket is owned by userspace, who might be in the process
18980d84ef3STom Parkin 		 * of closing it.  Look the socket up using the fd to ensure
19080d84ef3STom Parkin 		 * consistency.
19180d84ef3STom Parkin 		 */
19280d84ef3STom Parkin 		sock = sockfd_lookup(tunnel->fd, &err);
19380d84ef3STom Parkin 		if (sock)
19480d84ef3STom Parkin 			sk = sock->sk;
19580d84ef3STom Parkin 	} else {
19680d84ef3STom Parkin 		/* Socket is owned by kernelspace */
19780d84ef3STom Parkin 		sk = tunnel->sock;
1988abbbe8fSTom Parkin 		sock_hold(sk);
19980d84ef3STom Parkin 	}
20080d84ef3STom Parkin 
20180d84ef3STom Parkin out:
20280d84ef3STom Parkin 	return sk;
20380d84ef3STom Parkin }
20480d84ef3STom Parkin 
20580d84ef3STom Parkin /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
206b5d2b285Sstephen hemminger static void l2tp_tunnel_sock_put(struct sock *sk)
20780d84ef3STom Parkin {
20880d84ef3STom Parkin 	struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
20980d84ef3STom Parkin 	if (tunnel) {
21080d84ef3STom Parkin 		if (tunnel->fd >= 0) {
21180d84ef3STom Parkin 			/* Socket is owned by userspace */
21280d84ef3STom Parkin 			sockfd_put(sk->sk_socket);
21380d84ef3STom Parkin 		}
21480d84ef3STom Parkin 		sock_put(sk);
21580d84ef3STom Parkin 	}
2168abbbe8fSTom Parkin 	sock_put(sk);
21780d84ef3STom Parkin }
21880d84ef3STom Parkin 
219f7faffa3SJames Chapman /* Lookup a session by id in the global session list
220f7faffa3SJames Chapman  */
221f7faffa3SJames Chapman static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
222f7faffa3SJames Chapman {
223f7faffa3SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
224f7faffa3SJames Chapman 	struct hlist_head *session_list =
225f7faffa3SJames Chapman 		l2tp_session_id_hash_2(pn, session_id);
226f7faffa3SJames Chapman 	struct l2tp_session *session;
227f7faffa3SJames Chapman 
228e02d494dSJames Chapman 	rcu_read_lock_bh();
229b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(session, session_list, global_hlist) {
230f7faffa3SJames Chapman 		if (session->session_id == session_id) {
231e02d494dSJames Chapman 			rcu_read_unlock_bh();
232f7faffa3SJames Chapman 			return session;
233f7faffa3SJames Chapman 		}
234f7faffa3SJames Chapman 	}
235e02d494dSJames Chapman 	rcu_read_unlock_bh();
236f7faffa3SJames Chapman 
237f7faffa3SJames Chapman 	return NULL;
238f7faffa3SJames Chapman }
239f7faffa3SJames Chapman 
240fd558d18SJames Chapman /* Session hash list.
241fd558d18SJames Chapman  * The session_id SHOULD be random according to RFC2661, but several
242fd558d18SJames Chapman  * L2TP implementations (Cisco and Microsoft) use incrementing
243fd558d18SJames Chapman  * session_ids.  So we do a real hash on the session_id, rather than a
244fd558d18SJames Chapman  * simple bitmask.
245fd558d18SJames Chapman  */
246fd558d18SJames Chapman static inline struct hlist_head *
247fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
248fd558d18SJames Chapman {
249fd558d18SJames Chapman 	return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
250fd558d18SJames Chapman }
251fd558d18SJames Chapman 
252fd558d18SJames Chapman /* Lookup a session by id
253fd558d18SJames Chapman  */
254f7faffa3SJames Chapman struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
255fd558d18SJames Chapman {
256f7faffa3SJames Chapman 	struct hlist_head *session_list;
257fd558d18SJames Chapman 	struct l2tp_session *session;
258fd558d18SJames Chapman 
259f7faffa3SJames Chapman 	/* In L2TPv3, session_ids are unique over all tunnels and we
260f7faffa3SJames Chapman 	 * sometimes need to look them up before we know the
261f7faffa3SJames Chapman 	 * tunnel.
262f7faffa3SJames Chapman 	 */
263f7faffa3SJames Chapman 	if (tunnel == NULL)
264f7faffa3SJames Chapman 		return l2tp_session_find_2(net, session_id);
265f7faffa3SJames Chapman 
266f7faffa3SJames Chapman 	session_list = l2tp_session_id_hash(tunnel, session_id);
267fd558d18SJames Chapman 	read_lock_bh(&tunnel->hlist_lock);
268b67bfe0dSSasha Levin 	hlist_for_each_entry(session, session_list, hlist) {
269fd558d18SJames Chapman 		if (session->session_id == session_id) {
270fd558d18SJames Chapman 			read_unlock_bh(&tunnel->hlist_lock);
271fd558d18SJames Chapman 			return session;
272fd558d18SJames Chapman 		}
273fd558d18SJames Chapman 	}
274fd558d18SJames Chapman 	read_unlock_bh(&tunnel->hlist_lock);
275fd558d18SJames Chapman 
276fd558d18SJames Chapman 	return NULL;
277fd558d18SJames Chapman }
278fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find);
279fd558d18SJames Chapman 
280fd558d18SJames Chapman struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
281fd558d18SJames Chapman {
282fd558d18SJames Chapman 	int hash;
283fd558d18SJames Chapman 	struct l2tp_session *session;
284fd558d18SJames Chapman 	int count = 0;
285fd558d18SJames Chapman 
286fd558d18SJames Chapman 	read_lock_bh(&tunnel->hlist_lock);
287fd558d18SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
288b67bfe0dSSasha Levin 		hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
289fd558d18SJames Chapman 			if (++count > nth) {
290fd558d18SJames Chapman 				read_unlock_bh(&tunnel->hlist_lock);
291fd558d18SJames Chapman 				return session;
292fd558d18SJames Chapman 			}
293fd558d18SJames Chapman 		}
294fd558d18SJames Chapman 	}
295fd558d18SJames Chapman 
296fd558d18SJames Chapman 	read_unlock_bh(&tunnel->hlist_lock);
297fd558d18SJames Chapman 
298fd558d18SJames Chapman 	return NULL;
299fd558d18SJames Chapman }
300fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
301fd558d18SJames Chapman 
302309795f4SJames Chapman /* Lookup a session by interface name.
303309795f4SJames Chapman  * This is very inefficient but is only used by management interfaces.
304309795f4SJames Chapman  */
305309795f4SJames Chapman struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
306309795f4SJames Chapman {
307309795f4SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
308309795f4SJames Chapman 	int hash;
309309795f4SJames Chapman 	struct l2tp_session *session;
310309795f4SJames Chapman 
311e02d494dSJames Chapman 	rcu_read_lock_bh();
312309795f4SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
313b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
314309795f4SJames Chapman 			if (!strcmp(session->ifname, ifname)) {
315e02d494dSJames Chapman 				rcu_read_unlock_bh();
316309795f4SJames Chapman 				return session;
317309795f4SJames Chapman 			}
318309795f4SJames Chapman 		}
319309795f4SJames Chapman 	}
320309795f4SJames Chapman 
321e02d494dSJames Chapman 	rcu_read_unlock_bh();
322309795f4SJames Chapman 
323309795f4SJames Chapman 	return NULL;
324309795f4SJames Chapman }
325309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
326309795f4SJames Chapman 
327fd558d18SJames Chapman /* Lookup a tunnel by id
328fd558d18SJames Chapman  */
329fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
330fd558d18SJames Chapman {
331fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
332fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
333fd558d18SJames Chapman 
334e02d494dSJames Chapman 	rcu_read_lock_bh();
335e02d494dSJames Chapman 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
336fd558d18SJames Chapman 		if (tunnel->tunnel_id == tunnel_id) {
337e02d494dSJames Chapman 			rcu_read_unlock_bh();
338fd558d18SJames Chapman 			return tunnel;
339fd558d18SJames Chapman 		}
340fd558d18SJames Chapman 	}
341e02d494dSJames Chapman 	rcu_read_unlock_bh();
342fd558d18SJames Chapman 
343fd558d18SJames Chapman 	return NULL;
344fd558d18SJames Chapman }
345fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
346fd558d18SJames Chapman 
347fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
348fd558d18SJames Chapman {
349fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
350fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
351fd558d18SJames Chapman 	int count = 0;
352fd558d18SJames Chapman 
353e02d494dSJames Chapman 	rcu_read_lock_bh();
354e02d494dSJames Chapman 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
355fd558d18SJames Chapman 		if (++count > nth) {
356e02d494dSJames Chapman 			rcu_read_unlock_bh();
357fd558d18SJames Chapman 			return tunnel;
358fd558d18SJames Chapman 		}
359fd558d18SJames Chapman 	}
360fd558d18SJames Chapman 
361e02d494dSJames Chapman 	rcu_read_unlock_bh();
362fd558d18SJames Chapman 
363fd558d18SJames Chapman 	return NULL;
364fd558d18SJames Chapman }
365fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
366fd558d18SJames Chapman 
367fd558d18SJames Chapman /*****************************************************************************
368fd558d18SJames Chapman  * Receive data handling
369fd558d18SJames Chapman  *****************************************************************************/
370fd558d18SJames Chapman 
371fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence
372fd558d18SJames Chapman  * number.
373fd558d18SJames Chapman  */
374fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
375fd558d18SJames Chapman {
376fd558d18SJames Chapman 	struct sk_buff *skbp;
377fd558d18SJames Chapman 	struct sk_buff *tmp;
378f7faffa3SJames Chapman 	u32 ns = L2TP_SKB_CB(skb)->ns;
379fd558d18SJames Chapman 
380fd558d18SJames Chapman 	spin_lock_bh(&session->reorder_q.lock);
381fd558d18SJames Chapman 	skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
382fd558d18SJames Chapman 		if (L2TP_SKB_CB(skbp)->ns > ns) {
383fd558d18SJames Chapman 			__skb_queue_before(&session->reorder_q, skbp, skb);
384a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
385fd558d18SJames Chapman 				 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
386fd558d18SJames Chapman 				 session->name, ns, L2TP_SKB_CB(skbp)->ns,
387fd558d18SJames Chapman 				 skb_queue_len(&session->reorder_q));
3887b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_oos_packets);
389fd558d18SJames Chapman 			goto out;
390fd558d18SJames Chapman 		}
391fd558d18SJames Chapman 	}
392fd558d18SJames Chapman 
393fd558d18SJames Chapman 	__skb_queue_tail(&session->reorder_q, skb);
394fd558d18SJames Chapman 
395fd558d18SJames Chapman out:
396fd558d18SJames Chapman 	spin_unlock_bh(&session->reorder_q.lock);
397fd558d18SJames Chapman }
398fd558d18SJames Chapman 
399fd558d18SJames Chapman /* Dequeue a single skb.
400fd558d18SJames Chapman  */
401fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
402fd558d18SJames Chapman {
403fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
404fd558d18SJames Chapman 	int length = L2TP_SKB_CB(skb)->length;
405fd558d18SJames Chapman 
406fd558d18SJames Chapman 	/* We're about to requeue the skb, so return resources
407fd558d18SJames Chapman 	 * to its current owner (a socket receive buffer).
408fd558d18SJames Chapman 	 */
409fd558d18SJames Chapman 	skb_orphan(skb);
410fd558d18SJames Chapman 
4117b7c0719STom Parkin 	atomic_long_inc(&tunnel->stats.rx_packets);
4127b7c0719STom Parkin 	atomic_long_add(length, &tunnel->stats.rx_bytes);
4137b7c0719STom Parkin 	atomic_long_inc(&session->stats.rx_packets);
4147b7c0719STom Parkin 	atomic_long_add(length, &session->stats.rx_bytes);
415fd558d18SJames Chapman 
416fd558d18SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
417fd558d18SJames Chapman 		/* Bump our Nr */
418fd558d18SJames Chapman 		session->nr++;
4198a1631d5SJames Chapman 		session->nr &= session->nr_max;
420f7faffa3SJames Chapman 
421a4ca44faSJoe Perches 		l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
422a4ca44faSJoe Perches 			 session->name, session->nr);
423fd558d18SJames Chapman 	}
424fd558d18SJames Chapman 
425fd558d18SJames Chapman 	/* call private receive handler */
426fd558d18SJames Chapman 	if (session->recv_skb != NULL)
427fd558d18SJames Chapman 		(*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
428fd558d18SJames Chapman 	else
429fd558d18SJames Chapman 		kfree_skb(skb);
430fd558d18SJames Chapman 
431fd558d18SJames Chapman 	if (session->deref)
432fd558d18SJames Chapman 		(*session->deref)(session);
433fd558d18SJames Chapman }
434fd558d18SJames Chapman 
435fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order.
436fd558d18SJames Chapman  * Skbs that have been in the queue for too long are simply discarded.
437fd558d18SJames Chapman  */
438fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session)
439fd558d18SJames Chapman {
440fd558d18SJames Chapman 	struct sk_buff *skb;
441fd558d18SJames Chapman 	struct sk_buff *tmp;
442fd558d18SJames Chapman 
443fd558d18SJames Chapman 	/* If the pkt at the head of the queue has the nr that we
444fd558d18SJames Chapman 	 * expect to send up next, dequeue it and any other
445fd558d18SJames Chapman 	 * in-sequence packets behind it.
446fd558d18SJames Chapman 	 */
447e2e210c0SEric Dumazet start:
448fd558d18SJames Chapman 	spin_lock_bh(&session->reorder_q.lock);
449fd558d18SJames Chapman 	skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
450fd558d18SJames Chapman 		if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
4517b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
4527b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_errors);
453a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
454a4ca44faSJoe Perches 				 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
455fd558d18SJames Chapman 				 session->name, L2TP_SKB_CB(skb)->ns,
456fd558d18SJames Chapman 				 L2TP_SKB_CB(skb)->length, session->nr,
457fd558d18SJames Chapman 				 skb_queue_len(&session->reorder_q));
45838d40b3fSJames Chapman 			session->reorder_skip = 1;
459fd558d18SJames Chapman 			__skb_unlink(skb, &session->reorder_q);
460fd558d18SJames Chapman 			kfree_skb(skb);
461fd558d18SJames Chapman 			if (session->deref)
462fd558d18SJames Chapman 				(*session->deref)(session);
463fd558d18SJames Chapman 			continue;
464fd558d18SJames Chapman 		}
465fd558d18SJames Chapman 
466fd558d18SJames Chapman 		if (L2TP_SKB_CB(skb)->has_seq) {
46738d40b3fSJames Chapman 			if (session->reorder_skip) {
468a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
46938d40b3fSJames Chapman 					 "%s: advancing nr to next pkt: %u -> %u",
47038d40b3fSJames Chapman 					 session->name, session->nr,
47138d40b3fSJames Chapman 					 L2TP_SKB_CB(skb)->ns);
47238d40b3fSJames Chapman 				session->reorder_skip = 0;
47338d40b3fSJames Chapman 				session->nr = L2TP_SKB_CB(skb)->ns;
47438d40b3fSJames Chapman 			}
475fd558d18SJames Chapman 			if (L2TP_SKB_CB(skb)->ns != session->nr) {
476a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
477a4ca44faSJoe Perches 					 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
478fd558d18SJames Chapman 					 session->name, L2TP_SKB_CB(skb)->ns,
479fd558d18SJames Chapman 					 L2TP_SKB_CB(skb)->length, session->nr,
480fd558d18SJames Chapman 					 skb_queue_len(&session->reorder_q));
481fd558d18SJames Chapman 				goto out;
482fd558d18SJames Chapman 			}
483fd558d18SJames Chapman 		}
484fd558d18SJames Chapman 		__skb_unlink(skb, &session->reorder_q);
485fd558d18SJames Chapman 
486fd558d18SJames Chapman 		/* Process the skb. We release the queue lock while we
487fd558d18SJames Chapman 		 * do so to let other contexts process the queue.
488fd558d18SJames Chapman 		 */
489fd558d18SJames Chapman 		spin_unlock_bh(&session->reorder_q.lock);
490fd558d18SJames Chapman 		l2tp_recv_dequeue_skb(session, skb);
491e2e210c0SEric Dumazet 		goto start;
492fd558d18SJames Chapman 	}
493fd558d18SJames Chapman 
494fd558d18SJames Chapman out:
495fd558d18SJames Chapman 	spin_unlock_bh(&session->reorder_q.lock);
496fd558d18SJames Chapman }
497fd558d18SJames Chapman 
498fd558d18SJames Chapman static inline int l2tp_verify_udp_checksum(struct sock *sk,
499fd558d18SJames Chapman 					   struct sk_buff *skb)
500fd558d18SJames Chapman {
501fd558d18SJames Chapman 	struct udphdr *uh = udp_hdr(skb);
502fd558d18SJames Chapman 	u16 ulen = ntohs(uh->len);
503fd558d18SJames Chapman 	__wsum psum;
504fd558d18SJames Chapman 
505d2cf3361SBenjamin LaHaise 	if (sk->sk_no_check || skb_csum_unnecessary(skb))
506fd558d18SJames Chapman 		return 0;
507fd558d18SJames Chapman 
508d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
5098d8a51e2SDavid S. Miller 	if (sk->sk_family == PF_INET6 && !l2tp_tunnel(sk)->v4mapped) {
510d2cf3361SBenjamin LaHaise 		if (!uh->check) {
511d2cf3361SBenjamin LaHaise 			LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
512d2cf3361SBenjamin LaHaise 			return 1;
513d2cf3361SBenjamin LaHaise 		}
514d2cf3361SBenjamin LaHaise 		if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
515d2cf3361SBenjamin LaHaise 		    !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
516d2cf3361SBenjamin LaHaise 				     &ipv6_hdr(skb)->daddr, ulen,
517d2cf3361SBenjamin LaHaise 				     IPPROTO_UDP, skb->csum)) {
518d2cf3361SBenjamin LaHaise 			skb->ip_summed = CHECKSUM_UNNECESSARY;
519d2cf3361SBenjamin LaHaise 			return 0;
520d2cf3361SBenjamin LaHaise 		}
521d2cf3361SBenjamin LaHaise 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
522d2cf3361SBenjamin LaHaise 							 &ipv6_hdr(skb)->daddr,
523d2cf3361SBenjamin LaHaise 							 skb->len, IPPROTO_UDP,
524d2cf3361SBenjamin LaHaise 							 0));
525d2cf3361SBenjamin LaHaise 	} else
526d2cf3361SBenjamin LaHaise #endif
527d2cf3361SBenjamin LaHaise 	{
528d2cf3361SBenjamin LaHaise 		struct inet_sock *inet;
529d2cf3361SBenjamin LaHaise 		if (!uh->check)
530d2cf3361SBenjamin LaHaise 			return 0;
531fd558d18SJames Chapman 		inet = inet_sk(sk);
532d2cf3361SBenjamin LaHaise 		psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
533d2cf3361SBenjamin LaHaise 					  ulen, IPPROTO_UDP, 0);
534fd558d18SJames Chapman 
535fd558d18SJames Chapman 		if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
536fd558d18SJames Chapman 		    !csum_fold(csum_add(psum, skb->csum)))
537fd558d18SJames Chapman 			return 0;
538fd558d18SJames Chapman 		skb->csum = psum;
539d2cf3361SBenjamin LaHaise 	}
540fd558d18SJames Chapman 
541fd558d18SJames Chapman 	return __skb_checksum_complete(skb);
542fd558d18SJames Chapman }
543fd558d18SJames Chapman 
5448a1631d5SJames Chapman static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
5458a1631d5SJames Chapman {
5468a1631d5SJames Chapman 	u32 nws;
5478a1631d5SJames Chapman 
5488a1631d5SJames Chapman 	if (nr >= session->nr)
5498a1631d5SJames Chapman 		nws = nr - session->nr;
5508a1631d5SJames Chapman 	else
5518a1631d5SJames Chapman 		nws = (session->nr_max + 1) - (session->nr - nr);
5528a1631d5SJames Chapman 
5538a1631d5SJames Chapman 	return nws < session->nr_window_size;
5548a1631d5SJames Chapman }
5558a1631d5SJames Chapman 
556b6dc01a4SJames Chapman /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
557b6dc01a4SJames Chapman  * acceptable, else non-zero.
558b6dc01a4SJames Chapman  */
559b6dc01a4SJames Chapman static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
560b6dc01a4SJames Chapman {
5618a1631d5SJames Chapman 	if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
5628a1631d5SJames Chapman 		/* Packet sequence number is outside allowed window.
5638a1631d5SJames Chapman 		 * Discard it.
5648a1631d5SJames Chapman 		 */
5658a1631d5SJames Chapman 		l2tp_dbg(session, L2TP_MSG_SEQ,
5668a1631d5SJames Chapman 			 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
5678a1631d5SJames Chapman 			 session->name, L2TP_SKB_CB(skb)->ns,
5688a1631d5SJames Chapman 			 L2TP_SKB_CB(skb)->length, session->nr);
5698a1631d5SJames Chapman 		goto discard;
5708a1631d5SJames Chapman 	}
5718a1631d5SJames Chapman 
572b6dc01a4SJames Chapman 	if (session->reorder_timeout != 0) {
573b6dc01a4SJames Chapman 		/* Packet reordering enabled. Add skb to session's
574b6dc01a4SJames Chapman 		 * reorder queue, in order of ns.
575b6dc01a4SJames Chapman 		 */
576b6dc01a4SJames Chapman 		l2tp_recv_queue_skb(session, skb);
577a0dbd822SJames Chapman 		goto out;
578a0dbd822SJames Chapman 	}
579a0dbd822SJames Chapman 
580a0dbd822SJames Chapman 	/* Packet reordering disabled. Discard out-of-sequence packets, while
581a0dbd822SJames Chapman 	 * tracking the number if in-sequence packets after the first OOS packet
582a0dbd822SJames Chapman 	 * is seen. After nr_oos_count_max in-sequence packets, reset the
583a0dbd822SJames Chapman 	 * sequence number to re-enable packet reception.
584b6dc01a4SJames Chapman 	 */
585a0dbd822SJames Chapman 	if (L2TP_SKB_CB(skb)->ns == session->nr) {
586a0dbd822SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
587a0dbd822SJames Chapman 	} else {
588a0dbd822SJames Chapman 		u32 nr_oos = L2TP_SKB_CB(skb)->ns;
589a0dbd822SJames Chapman 		u32 nr_next = (session->nr_oos + 1) & session->nr_max;
590a0dbd822SJames Chapman 
591a0dbd822SJames Chapman 		if (nr_oos == nr_next)
592a0dbd822SJames Chapman 			session->nr_oos_count++;
593a0dbd822SJames Chapman 		else
594a0dbd822SJames Chapman 			session->nr_oos_count = 0;
595a0dbd822SJames Chapman 
596a0dbd822SJames Chapman 		session->nr_oos = nr_oos;
597a0dbd822SJames Chapman 		if (session->nr_oos_count > session->nr_oos_count_max) {
598a0dbd822SJames Chapman 			session->reorder_skip = 1;
599a0dbd822SJames Chapman 			l2tp_dbg(session, L2TP_MSG_SEQ,
600a0dbd822SJames Chapman 				 "%s: %d oos packets received. Resetting sequence numbers\n",
601a0dbd822SJames Chapman 				 session->name, session->nr_oos_count);
602a0dbd822SJames Chapman 		}
603a0dbd822SJames Chapman 		if (!session->reorder_skip) {
604b6dc01a4SJames Chapman 			atomic_long_inc(&session->stats.rx_seq_discards);
605b6dc01a4SJames Chapman 			l2tp_dbg(session, L2TP_MSG_SEQ,
606b6dc01a4SJames Chapman 				 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
607b6dc01a4SJames Chapman 				 session->name, L2TP_SKB_CB(skb)->ns,
608b6dc01a4SJames Chapman 				 L2TP_SKB_CB(skb)->length, session->nr,
609b6dc01a4SJames Chapman 				 skb_queue_len(&session->reorder_q));
610b6dc01a4SJames Chapman 			goto discard;
611b6dc01a4SJames Chapman 		}
612b6dc01a4SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
613b6dc01a4SJames Chapman 	}
614b6dc01a4SJames Chapman 
615a0dbd822SJames Chapman out:
616b6dc01a4SJames Chapman 	return 0;
617b6dc01a4SJames Chapman 
618b6dc01a4SJames Chapman discard:
619b6dc01a4SJames Chapman 	return 1;
620b6dc01a4SJames Chapman }
621b6dc01a4SJames Chapman 
622f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2
623f7faffa3SJames Chapman  * and L2TPv3 data frames here.
624f7faffa3SJames Chapman  *
625f7faffa3SJames Chapman  * L2TPv2 Data Message Header
626f7faffa3SJames Chapman  *
627f7faffa3SJames Chapman  *  0                   1                   2                   3
628f7faffa3SJames 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
629f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630f7faffa3SJames Chapman  * |T|L|x|x|S|x|O|P|x|x|x|x|  Ver  |          Length (opt)         |
631f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632f7faffa3SJames Chapman  * |           Tunnel ID           |           Session ID          |
633f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634f7faffa3SJames Chapman  * |             Ns (opt)          |             Nr (opt)          |
635f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636f7faffa3SJames Chapman  * |      Offset Size (opt)        |    Offset pad... (opt)
637f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638f7faffa3SJames Chapman  *
639f7faffa3SJames Chapman  * Data frames are marked by T=0. All other fields are the same as
640f7faffa3SJames Chapman  * those in L2TP control frames.
641f7faffa3SJames Chapman  *
642f7faffa3SJames Chapman  * L2TPv3 Data Message Header
643f7faffa3SJames Chapman  *
644f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
645f7faffa3SJames Chapman  * |                      L2TP Session Header                      |
646f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
647f7faffa3SJames Chapman  * |                      L2-Specific Sublayer                     |
648f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
649f7faffa3SJames Chapman  * |                        Tunnel Payload                      ...
650f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
651f7faffa3SJames Chapman  *
652f7faffa3SJames Chapman  * L2TPv3 Session Header Over IP
653f7faffa3SJames Chapman  *
654f7faffa3SJames Chapman  *  0                   1                   2                   3
655f7faffa3SJames 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
656f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657f7faffa3SJames Chapman  * |                           Session ID                          |
658f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659f7faffa3SJames Chapman  * |               Cookie (optional, maximum 64 bits)...
660f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
661f7faffa3SJames Chapman  *                                                                 |
662f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
663f7faffa3SJames Chapman  *
664f7faffa3SJames Chapman  * L2TPv3 L2-Specific Sublayer Format
665f7faffa3SJames Chapman  *
666f7faffa3SJames Chapman  *  0                   1                   2                   3
667f7faffa3SJames 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
668f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669f7faffa3SJames Chapman  * |x|S|x|x|x|x|x|x|              Sequence Number                  |
670f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
671f7faffa3SJames Chapman  *
672f7faffa3SJames Chapman  * Cookie value, sublayer format and offset (pad) are negotiated with
673f7faffa3SJames Chapman  * the peer when the session is set up. Unlike L2TPv2, we do not need
674f7faffa3SJames Chapman  * to parse the packet header to determine if optional fields are
675f7faffa3SJames Chapman  * present.
676f7faffa3SJames Chapman  *
677f7faffa3SJames Chapman  * Caller must already have parsed the frame and determined that it is
678f7faffa3SJames Chapman  * a data (not control) frame before coming here. Fields up to the
679f7faffa3SJames Chapman  * session-id have already been parsed and ptr points to the data
680f7faffa3SJames Chapman  * after the session-id.
681f7faffa3SJames Chapman  */
682f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
683f7faffa3SJames Chapman 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
684f7faffa3SJames Chapman 		      int length, int (*payload_hook)(struct sk_buff *skb))
685f7faffa3SJames Chapman {
686f7faffa3SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
687f7faffa3SJames Chapman 	int offset;
688f7faffa3SJames Chapman 	u32 ns, nr;
689f7faffa3SJames Chapman 
690f7faffa3SJames Chapman 	/* The ref count is increased since we now hold a pointer to
691f7faffa3SJames Chapman 	 * the session. Take care to decrement the refcnt when exiting
692f7faffa3SJames Chapman 	 * this function from now on...
693f7faffa3SJames Chapman 	 */
694f7faffa3SJames Chapman 	l2tp_session_inc_refcount(session);
695f7faffa3SJames Chapman 	if (session->ref)
696f7faffa3SJames Chapman 		(*session->ref)(session);
697f7faffa3SJames Chapman 
698f7faffa3SJames Chapman 	/* Parse and check optional cookie */
699f7faffa3SJames Chapman 	if (session->peer_cookie_len > 0) {
700f7faffa3SJames Chapman 		if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
701a4ca44faSJoe Perches 			l2tp_info(tunnel, L2TP_MSG_DATA,
702f7faffa3SJames Chapman 				  "%s: cookie mismatch (%u/%u). Discarding.\n",
703a4ca44faSJoe Perches 				  tunnel->name, tunnel->tunnel_id,
704a4ca44faSJoe Perches 				  session->session_id);
7057b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_cookie_discards);
706f7faffa3SJames Chapman 			goto discard;
707f7faffa3SJames Chapman 		}
708f7faffa3SJames Chapman 		ptr += session->peer_cookie_len;
709f7faffa3SJames Chapman 	}
710f7faffa3SJames Chapman 
711f7faffa3SJames Chapman 	/* Handle the optional sequence numbers. Sequence numbers are
712f7faffa3SJames Chapman 	 * in different places for L2TPv2 and L2TPv3.
713f7faffa3SJames Chapman 	 *
714f7faffa3SJames Chapman 	 * If we are the LAC, enable/disable sequence numbers under
715f7faffa3SJames Chapman 	 * the control of the LNS.  If no sequence numbers present but
716f7faffa3SJames Chapman 	 * we were expecting them, discard frame.
717f7faffa3SJames Chapman 	 */
718f7faffa3SJames Chapman 	ns = nr = 0;
719f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->has_seq = 0;
720f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
721f7faffa3SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_S) {
722f7faffa3SJames Chapman 			ns = ntohs(*(__be16 *) ptr);
723f7faffa3SJames Chapman 			ptr += 2;
724f7faffa3SJames Chapman 			nr = ntohs(*(__be16 *) ptr);
725f7faffa3SJames Chapman 			ptr += 2;
726f7faffa3SJames Chapman 
727f7faffa3SJames Chapman 			/* Store L2TP info in the skb */
728f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->ns = ns;
729f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->has_seq = 1;
730f7faffa3SJames Chapman 
731a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
732f7faffa3SJames Chapman 				 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
733f7faffa3SJames Chapman 				 session->name, ns, nr, session->nr);
734f7faffa3SJames Chapman 		}
735f7faffa3SJames Chapman 	} else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
736f7faffa3SJames Chapman 		u32 l2h = ntohl(*(__be32 *) ptr);
737f7faffa3SJames Chapman 
738f7faffa3SJames Chapman 		if (l2h & 0x40000000) {
739f7faffa3SJames Chapman 			ns = l2h & 0x00ffffff;
740f7faffa3SJames Chapman 
741f7faffa3SJames Chapman 			/* Store L2TP info in the skb */
742f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->ns = ns;
743f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->has_seq = 1;
744f7faffa3SJames Chapman 
745a4ca44faSJoe Perches 			l2tp_dbg(session, L2TP_MSG_SEQ,
746f7faffa3SJames Chapman 				 "%s: recv data ns=%u, session nr=%u\n",
747f7faffa3SJames Chapman 				 session->name, ns, session->nr);
748f7faffa3SJames Chapman 		}
749f7faffa3SJames Chapman 	}
750f7faffa3SJames Chapman 
751f7faffa3SJames Chapman 	/* Advance past L2-specific header, if present */
752f7faffa3SJames Chapman 	ptr += session->l2specific_len;
753f7faffa3SJames Chapman 
754f7faffa3SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
755f7faffa3SJames Chapman 		/* Received a packet with sequence numbers. If we're the LNS,
756f7faffa3SJames Chapman 		 * check if we sre sending sequence numbers and if not,
757f7faffa3SJames Chapman 		 * configure it so.
758f7faffa3SJames Chapman 		 */
759f7faffa3SJames Chapman 		if ((!session->lns_mode) && (!session->send_seq)) {
760a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_SEQ,
761f7faffa3SJames Chapman 				  "%s: requested to enable seq numbers by LNS\n",
762f7faffa3SJames Chapman 				  session->name);
763f7faffa3SJames Chapman 			session->send_seq = -1;
764f7faffa3SJames Chapman 			l2tp_session_set_header_len(session, tunnel->version);
765f7faffa3SJames Chapman 		}
766f7faffa3SJames Chapman 	} else {
767f7faffa3SJames Chapman 		/* No sequence numbers.
768f7faffa3SJames Chapman 		 * If user has configured mandatory sequence numbers, discard.
769f7faffa3SJames Chapman 		 */
770f7faffa3SJames Chapman 		if (session->recv_seq) {
771a4ca44faSJoe Perches 			l2tp_warn(session, L2TP_MSG_SEQ,
772a4ca44faSJoe Perches 				  "%s: recv data has no seq numbers when required. Discarding.\n",
773a4ca44faSJoe Perches 				  session->name);
7747b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
775f7faffa3SJames Chapman 			goto discard;
776f7faffa3SJames Chapman 		}
777f7faffa3SJames Chapman 
778f7faffa3SJames Chapman 		/* If we're the LAC and we're sending sequence numbers, the
779f7faffa3SJames Chapman 		 * LNS has requested that we no longer send sequence numbers.
780f7faffa3SJames Chapman 		 * If we're the LNS and we're sending sequence numbers, the
781f7faffa3SJames Chapman 		 * LAC is broken. Discard the frame.
782f7faffa3SJames Chapman 		 */
783f7faffa3SJames Chapman 		if ((!session->lns_mode) && (session->send_seq)) {
784a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_SEQ,
785f7faffa3SJames Chapman 				  "%s: requested to disable seq numbers by LNS\n",
786f7faffa3SJames Chapman 				  session->name);
787f7faffa3SJames Chapman 			session->send_seq = 0;
788f7faffa3SJames Chapman 			l2tp_session_set_header_len(session, tunnel->version);
789f7faffa3SJames Chapman 		} else if (session->send_seq) {
790a4ca44faSJoe Perches 			l2tp_warn(session, L2TP_MSG_SEQ,
791a4ca44faSJoe Perches 				  "%s: recv data has no seq numbers when required. Discarding.\n",
792a4ca44faSJoe Perches 				  session->name);
7937b7c0719STom Parkin 			atomic_long_inc(&session->stats.rx_seq_discards);
794f7faffa3SJames Chapman 			goto discard;
795f7faffa3SJames Chapman 		}
796f7faffa3SJames Chapman 	}
797f7faffa3SJames Chapman 
798f7faffa3SJames Chapman 	/* Session data offset is handled differently for L2TPv2 and
799f7faffa3SJames Chapman 	 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
800f7faffa3SJames Chapman 	 * the header. For L2TPv3, the offset is negotiated using AVPs
801f7faffa3SJames Chapman 	 * in the session setup control protocol.
802f7faffa3SJames Chapman 	 */
803f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
804f7faffa3SJames Chapman 		/* If offset bit set, skip it. */
805f7faffa3SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_O) {
806f7faffa3SJames Chapman 			offset = ntohs(*(__be16 *)ptr);
807f7faffa3SJames Chapman 			ptr += 2 + offset;
808f7faffa3SJames Chapman 		}
809f7faffa3SJames Chapman 	} else
810f7faffa3SJames Chapman 		ptr += session->offset;
811f7faffa3SJames Chapman 
812f7faffa3SJames Chapman 	offset = ptr - optr;
813f7faffa3SJames Chapman 	if (!pskb_may_pull(skb, offset))
814f7faffa3SJames Chapman 		goto discard;
815f7faffa3SJames Chapman 
816f7faffa3SJames Chapman 	__skb_pull(skb, offset);
817f7faffa3SJames Chapman 
818f7faffa3SJames Chapman 	/* If caller wants to process the payload before we queue the
819f7faffa3SJames Chapman 	 * packet, do so now.
820f7faffa3SJames Chapman 	 */
821f7faffa3SJames Chapman 	if (payload_hook)
822f7faffa3SJames Chapman 		if ((*payload_hook)(skb))
823f7faffa3SJames Chapman 			goto discard;
824f7faffa3SJames Chapman 
825f7faffa3SJames Chapman 	/* Prepare skb for adding to the session's reorder_q.  Hold
826f7faffa3SJames Chapman 	 * packets for max reorder_timeout or 1 second if not
827f7faffa3SJames Chapman 	 * reordering.
828f7faffa3SJames Chapman 	 */
829f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->length = length;
830f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->expires = jiffies +
831f7faffa3SJames Chapman 		(session->reorder_timeout ? session->reorder_timeout : HZ);
832f7faffa3SJames Chapman 
833f7faffa3SJames Chapman 	/* Add packet to the session's receive queue. Reordering is done here, if
834f7faffa3SJames Chapman 	 * enabled. Saved L2TP protocol info is stored in skb->sb[].
835f7faffa3SJames Chapman 	 */
836f7faffa3SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
837b6dc01a4SJames Chapman 		if (l2tp_recv_data_seq(session, skb))
838f7faffa3SJames Chapman 			goto discard;
839f7faffa3SJames Chapman 	} else {
840f7faffa3SJames Chapman 		/* No sequence numbers. Add the skb to the tail of the
841f7faffa3SJames Chapman 		 * reorder queue. This ensures that it will be
842f7faffa3SJames Chapman 		 * delivered after all previous sequenced skbs.
843f7faffa3SJames Chapman 		 */
844f7faffa3SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
845f7faffa3SJames Chapman 	}
846f7faffa3SJames Chapman 
847f7faffa3SJames Chapman 	/* Try to dequeue as many skbs from reorder_q as we can. */
848f7faffa3SJames Chapman 	l2tp_recv_dequeue(session);
849f7faffa3SJames Chapman 
850f7faffa3SJames Chapman 	l2tp_session_dec_refcount(session);
851f7faffa3SJames Chapman 
852f7faffa3SJames Chapman 	return;
853f7faffa3SJames Chapman 
854f7faffa3SJames Chapman discard:
8557b7c0719STom Parkin 	atomic_long_inc(&session->stats.rx_errors);
856f7faffa3SJames Chapman 	kfree_skb(skb);
857f7faffa3SJames Chapman 
858f7faffa3SJames Chapman 	if (session->deref)
859f7faffa3SJames Chapman 		(*session->deref)(session);
860f7faffa3SJames Chapman 
861f7faffa3SJames Chapman 	l2tp_session_dec_refcount(session);
862f7faffa3SJames Chapman }
863f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common);
864f7faffa3SJames Chapman 
86548f72f92STom Parkin /* Drop skbs from the session's reorder_q
86648f72f92STom Parkin  */
86748f72f92STom Parkin int l2tp_session_queue_purge(struct l2tp_session *session)
86848f72f92STom Parkin {
86948f72f92STom Parkin 	struct sk_buff *skb = NULL;
87048f72f92STom Parkin 	BUG_ON(!session);
87148f72f92STom Parkin 	BUG_ON(session->magic != L2TP_SESSION_MAGIC);
87248f72f92STom Parkin 	while ((skb = skb_dequeue(&session->reorder_q))) {
87348f72f92STom Parkin 		atomic_long_inc(&session->stats.rx_errors);
87448f72f92STom Parkin 		kfree_skb(skb);
87548f72f92STom Parkin 		if (session->deref)
87648f72f92STom Parkin 			(*session->deref)(session);
87748f72f92STom Parkin 	}
87848f72f92STom Parkin 	return 0;
87948f72f92STom Parkin }
88048f72f92STom Parkin EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
88148f72f92STom Parkin 
882fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
883fd558d18SJames Chapman  * here. The skb is not on a list when we get here.
884fd558d18SJames Chapman  * Returns 0 if the packet was a data packet and was successfully passed on.
885fd558d18SJames Chapman  * Returns 1 if the packet was not a good data packet and could not be
886fd558d18SJames Chapman  * forwarded.  All such packets are passed up to userspace to deal with.
887fd558d18SJames Chapman  */
888fc130840Sstephen hemminger static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
889fd558d18SJames Chapman 			      int (*payload_hook)(struct sk_buff *skb))
890fd558d18SJames Chapman {
891fd558d18SJames Chapman 	struct l2tp_session *session = NULL;
892fd558d18SJames Chapman 	unsigned char *ptr, *optr;
893fd558d18SJames Chapman 	u16 hdrflags;
894fd558d18SJames Chapman 	u32 tunnel_id, session_id;
895fd558d18SJames Chapman 	u16 version;
896f7faffa3SJames Chapman 	int length;
897fd558d18SJames Chapman 
898fd558d18SJames Chapman 	if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
899fd558d18SJames Chapman 		goto discard_bad_csum;
900fd558d18SJames Chapman 
901fd558d18SJames Chapman 	/* UDP always verifies the packet length. */
902fd558d18SJames Chapman 	__skb_pull(skb, sizeof(struct udphdr));
903fd558d18SJames Chapman 
904fd558d18SJames Chapman 	/* Short packet? */
905fd558d18SJames Chapman 	if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
906a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
907a4ca44faSJoe Perches 			  "%s: recv short packet (len=%d)\n",
908a4ca44faSJoe Perches 			  tunnel->name, skb->len);
909fd558d18SJames Chapman 		goto error;
910fd558d18SJames Chapman 	}
911fd558d18SJames Chapman 
912fd558d18SJames Chapman 	/* Trace packet contents, if enabled */
913fd558d18SJames Chapman 	if (tunnel->debug & L2TP_MSG_DATA) {
914fd558d18SJames Chapman 		length = min(32u, skb->len);
915fd558d18SJames Chapman 		if (!pskb_may_pull(skb, length))
916fd558d18SJames Chapman 			goto error;
917fd558d18SJames Chapman 
918a4ca44faSJoe Perches 		pr_debug("%s: recv\n", tunnel->name);
919a4ca44faSJoe Perches 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
920fd558d18SJames Chapman 	}
921fd558d18SJames Chapman 
922e50e705cSEric Dumazet 	/* Point to L2TP header */
923e50e705cSEric Dumazet 	optr = ptr = skb->data;
924e50e705cSEric Dumazet 
925fd558d18SJames Chapman 	/* Get L2TP header flags */
926fd558d18SJames Chapman 	hdrflags = ntohs(*(__be16 *) ptr);
927fd558d18SJames Chapman 
928fd558d18SJames Chapman 	/* Check protocol version */
929fd558d18SJames Chapman 	version = hdrflags & L2TP_HDR_VER_MASK;
930fd558d18SJames Chapman 	if (version != tunnel->version) {
931a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
932fd558d18SJames Chapman 			  "%s: recv protocol version mismatch: got %d expected %d\n",
933fd558d18SJames Chapman 			  tunnel->name, version, tunnel->version);
934fd558d18SJames Chapman 		goto error;
935fd558d18SJames Chapman 	}
936fd558d18SJames Chapman 
937fd558d18SJames Chapman 	/* Get length of L2TP packet */
938fd558d18SJames Chapman 	length = skb->len;
939fd558d18SJames Chapman 
940fd558d18SJames Chapman 	/* If type is control packet, it is handled by userspace. */
941fd558d18SJames Chapman 	if (hdrflags & L2TP_HDRFLAG_T) {
942a4ca44faSJoe Perches 		l2tp_dbg(tunnel, L2TP_MSG_DATA,
943a4ca44faSJoe Perches 			 "%s: recv control packet, len=%d\n",
944a4ca44faSJoe Perches 			 tunnel->name, length);
945fd558d18SJames Chapman 		goto error;
946fd558d18SJames Chapman 	}
947fd558d18SJames Chapman 
948fd558d18SJames Chapman 	/* Skip flags */
949fd558d18SJames Chapman 	ptr += 2;
950fd558d18SJames Chapman 
951f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
952fd558d18SJames Chapman 		/* If length is present, skip it */
953fd558d18SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_L)
954fd558d18SJames Chapman 			ptr += 2;
955fd558d18SJames Chapman 
956fd558d18SJames Chapman 		/* Extract tunnel and session ID */
957fd558d18SJames Chapman 		tunnel_id = ntohs(*(__be16 *) ptr);
958fd558d18SJames Chapman 		ptr += 2;
959fd558d18SJames Chapman 		session_id = ntohs(*(__be16 *) ptr);
960fd558d18SJames Chapman 		ptr += 2;
961f7faffa3SJames Chapman 	} else {
962f7faffa3SJames Chapman 		ptr += 2;	/* skip reserved bits */
963f7faffa3SJames Chapman 		tunnel_id = tunnel->tunnel_id;
964f7faffa3SJames Chapman 		session_id = ntohl(*(__be32 *) ptr);
965f7faffa3SJames Chapman 		ptr += 4;
966f7faffa3SJames Chapman 	}
967fd558d18SJames Chapman 
968fd558d18SJames Chapman 	/* Find the session context */
969f7faffa3SJames Chapman 	session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
970309795f4SJames Chapman 	if (!session || !session->recv_skb) {
971fd558d18SJames Chapman 		/* Not found? Pass to userspace to deal with */
972a4ca44faSJoe Perches 		l2tp_info(tunnel, L2TP_MSG_DATA,
973f7faffa3SJames Chapman 			  "%s: no session found (%u/%u). Passing up.\n",
974fd558d18SJames Chapman 			  tunnel->name, tunnel_id, session_id);
975fd558d18SJames Chapman 		goto error;
976fd558d18SJames Chapman 	}
977fd558d18SJames Chapman 
978f7faffa3SJames Chapman 	l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
979fd558d18SJames Chapman 
980fd558d18SJames Chapman 	return 0;
981fd558d18SJames Chapman 
982fd558d18SJames Chapman discard_bad_csum:
983fd558d18SJames Chapman 	LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
984fd558d18SJames Chapman 	UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
9857b7c0719STom Parkin 	atomic_long_inc(&tunnel->stats.rx_errors);
986fd558d18SJames Chapman 	kfree_skb(skb);
987fd558d18SJames Chapman 
988fd558d18SJames Chapman 	return 0;
989fd558d18SJames Chapman 
990fd558d18SJames Chapman error:
991fd558d18SJames Chapman 	/* Put UDP header back */
992fd558d18SJames Chapman 	__skb_push(skb, sizeof(struct udphdr));
993fd558d18SJames Chapman 
994fd558d18SJames Chapman 	return 1;
995fd558d18SJames Chapman }
996fd558d18SJames Chapman 
997fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c.
998fd558d18SJames Chapman  * Return codes:
999fd558d18SJames Chapman  * 0 : success.
1000fd558d18SJames Chapman  * <0: error
1001fd558d18SJames Chapman  * >0: skb should be passed up to userspace as UDP.
1002fd558d18SJames Chapman  */
1003fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1004fd558d18SJames Chapman {
1005fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1006fd558d18SJames Chapman 
1007fd558d18SJames Chapman 	tunnel = l2tp_sock_to_tunnel(sk);
1008fd558d18SJames Chapman 	if (tunnel == NULL)
1009fd558d18SJames Chapman 		goto pass_up;
1010fd558d18SJames Chapman 
1011a4ca44faSJoe Perches 	l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1012a4ca44faSJoe Perches 		 tunnel->name, skb->len);
1013fd558d18SJames Chapman 
1014fd558d18SJames Chapman 	if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1015fd558d18SJames Chapman 		goto pass_up_put;
1016fd558d18SJames Chapman 
1017fd558d18SJames Chapman 	sock_put(sk);
1018fd558d18SJames Chapman 	return 0;
1019fd558d18SJames Chapman 
1020fd558d18SJames Chapman pass_up_put:
1021fd558d18SJames Chapman 	sock_put(sk);
1022fd558d18SJames Chapman pass_up:
1023fd558d18SJames Chapman 	return 1;
1024fd558d18SJames Chapman }
1025fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1026fd558d18SJames Chapman 
1027fd558d18SJames Chapman /************************************************************************
1028fd558d18SJames Chapman  * Transmit handling
1029fd558d18SJames Chapman  ***********************************************************************/
1030fd558d18SJames Chapman 
1031fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided.
1032fd558d18SJames Chapman  */
1033f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
1034fd558d18SJames Chapman {
1035f7faffa3SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1036fd558d18SJames Chapman 	__be16 *bufp = buf;
1037f7faffa3SJames Chapman 	__be16 *optr = buf;
1038fd558d18SJames Chapman 	u16 flags = L2TP_HDR_VER_2;
1039fd558d18SJames Chapman 	u32 tunnel_id = tunnel->peer_tunnel_id;
1040fd558d18SJames Chapman 	u32 session_id = session->peer_session_id;
1041fd558d18SJames Chapman 
1042fd558d18SJames Chapman 	if (session->send_seq)
1043fd558d18SJames Chapman 		flags |= L2TP_HDRFLAG_S;
1044fd558d18SJames Chapman 
1045fd558d18SJames Chapman 	/* Setup L2TP header. */
1046fd558d18SJames Chapman 	*bufp++ = htons(flags);
1047fd558d18SJames Chapman 	*bufp++ = htons(tunnel_id);
1048fd558d18SJames Chapman 	*bufp++ = htons(session_id);
1049fd558d18SJames Chapman 	if (session->send_seq) {
1050fd558d18SJames Chapman 		*bufp++ = htons(session->ns);
1051fd558d18SJames Chapman 		*bufp++ = 0;
1052fd558d18SJames Chapman 		session->ns++;
1053f7faffa3SJames Chapman 		session->ns &= 0xffff;
1054a4ca44faSJoe Perches 		l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1055a4ca44faSJoe Perches 			 session->name, session->ns);
1056fd558d18SJames Chapman 	}
1057fd558d18SJames Chapman 
1058f7faffa3SJames Chapman 	return bufp - optr;
1059f7faffa3SJames Chapman }
1060f7faffa3SJames Chapman 
1061f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1062fd558d18SJames Chapman {
10630d76751fSJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1064f7faffa3SJames Chapman 	char *bufp = buf;
1065f7faffa3SJames Chapman 	char *optr = bufp;
1066fd558d18SJames Chapman 
10670d76751fSJames Chapman 	/* Setup L2TP header. The header differs slightly for UDP and
10680d76751fSJames Chapman 	 * IP encapsulations. For UDP, there is 4 bytes of flags.
10690d76751fSJames Chapman 	 */
10700d76751fSJames Chapman 	if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
10710d76751fSJames Chapman 		u16 flags = L2TP_HDR_VER_3;
1072f7faffa3SJames Chapman 		*((__be16 *) bufp) = htons(flags);
1073f7faffa3SJames Chapman 		bufp += 2;
1074f7faffa3SJames Chapman 		*((__be16 *) bufp) = 0;
1075f7faffa3SJames Chapman 		bufp += 2;
10760d76751fSJames Chapman 	}
10770d76751fSJames Chapman 
1078f7faffa3SJames Chapman 	*((__be32 *) bufp) = htonl(session->peer_session_id);
1079f7faffa3SJames Chapman 	bufp += 4;
1080f7faffa3SJames Chapman 	if (session->cookie_len) {
1081f7faffa3SJames Chapman 		memcpy(bufp, &session->cookie[0], session->cookie_len);
1082f7faffa3SJames Chapman 		bufp += session->cookie_len;
1083fd558d18SJames Chapman 	}
1084f7faffa3SJames Chapman 	if (session->l2specific_len) {
1085f7faffa3SJames Chapman 		if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1086f7faffa3SJames Chapman 			u32 l2h = 0;
1087f7faffa3SJames Chapman 			if (session->send_seq) {
1088f7faffa3SJames Chapman 				l2h = 0x40000000 | session->ns;
1089f7faffa3SJames Chapman 				session->ns++;
1090f7faffa3SJames Chapman 				session->ns &= 0xffffff;
1091a4ca44faSJoe Perches 				l2tp_dbg(session, L2TP_MSG_SEQ,
1092a4ca44faSJoe Perches 					 "%s: updated ns to %u\n",
1093a4ca44faSJoe Perches 					 session->name, session->ns);
1094f7faffa3SJames Chapman 			}
1095f7faffa3SJames Chapman 
1096f7faffa3SJames Chapman 			*((__be32 *) bufp) = htonl(l2h);
1097f7faffa3SJames Chapman 		}
1098f7faffa3SJames Chapman 		bufp += session->l2specific_len;
1099f7faffa3SJames Chapman 	}
1100f7faffa3SJames Chapman 	if (session->offset)
1101f7faffa3SJames Chapman 		bufp += session->offset;
1102f7faffa3SJames Chapman 
1103f7faffa3SJames Chapman 	return bufp - optr;
1104f7faffa3SJames Chapman }
1105fd558d18SJames Chapman 
1106fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1107d9d8da80SDavid S. Miller 			  struct flowi *fl, size_t data_len)
1108fd558d18SJames Chapman {
1109fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
1110fd558d18SJames Chapman 	unsigned int len = skb->len;
1111fd558d18SJames Chapman 	int error;
1112fd558d18SJames Chapman 
1113fd558d18SJames Chapman 	/* Debug */
1114fd558d18SJames Chapman 	if (session->send_seq)
1115a4ca44faSJoe Perches 		l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1116a4ca44faSJoe Perches 			 session->name, data_len, session->ns - 1);
1117fd558d18SJames Chapman 	else
1118a4ca44faSJoe Perches 		l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1119a4ca44faSJoe Perches 			 session->name, data_len);
1120fd558d18SJames Chapman 
1121fd558d18SJames Chapman 	if (session->debug & L2TP_MSG_DATA) {
11220d76751fSJames Chapman 		int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
11230d76751fSJames Chapman 		unsigned char *datap = skb->data + uhlen;
1124fd558d18SJames Chapman 
1125a4ca44faSJoe Perches 		pr_debug("%s: xmit\n", session->name);
1126a4ca44faSJoe Perches 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1127a4ca44faSJoe Perches 				     datap, min_t(size_t, 32, len - uhlen));
1128fd558d18SJames Chapman 	}
1129fd558d18SJames Chapman 
1130fd558d18SJames Chapman 	/* Queue the packet to IP for output */
11314e15ed4dSShan Wei 	skb->local_df = 1;
1132d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1133746e3499SEric Dumazet 	if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
1134b0270e91SEric Dumazet 		error = inet6_csk_xmit(tunnel->sock, skb, NULL);
1135d2cf3361SBenjamin LaHaise 	else
1136d2cf3361SBenjamin LaHaise #endif
1137b0270e91SEric Dumazet 		error = ip_queue_xmit(tunnel->sock, skb, fl);
1138fd558d18SJames Chapman 
1139fd558d18SJames Chapman 	/* Update stats */
1140fd558d18SJames Chapman 	if (error >= 0) {
11417b7c0719STom Parkin 		atomic_long_inc(&tunnel->stats.tx_packets);
11427b7c0719STom Parkin 		atomic_long_add(len, &tunnel->stats.tx_bytes);
11437b7c0719STom Parkin 		atomic_long_inc(&session->stats.tx_packets);
11447b7c0719STom Parkin 		atomic_long_add(len, &session->stats.tx_bytes);
1145fd558d18SJames Chapman 	} else {
11467b7c0719STom Parkin 		atomic_long_inc(&tunnel->stats.tx_errors);
11477b7c0719STom Parkin 		atomic_long_inc(&session->stats.tx_errors);
1148fd558d18SJames Chapman 	}
1149fd558d18SJames Chapman 
1150fd558d18SJames Chapman 	return 0;
1151fd558d18SJames Chapman }
1152fd558d18SJames Chapman 
1153d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1154d2cf3361SBenjamin LaHaise static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1155d2cf3361SBenjamin LaHaise 				int udp_len)
1156d2cf3361SBenjamin LaHaise {
1157d2cf3361SBenjamin LaHaise 	struct ipv6_pinfo *np = inet6_sk(sk);
1158d2cf3361SBenjamin LaHaise 	struct udphdr *uh = udp_hdr(skb);
1159d2cf3361SBenjamin LaHaise 
1160d2cf3361SBenjamin LaHaise 	if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1161d2cf3361SBenjamin LaHaise 	    !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1162d2cf3361SBenjamin LaHaise 		__wsum csum = skb_checksum(skb, 0, udp_len, 0);
1163d2cf3361SBenjamin LaHaise 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1164efe4208fSEric Dumazet 		uh->check = csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr, udp_len,
1165d2cf3361SBenjamin LaHaise 					    IPPROTO_UDP, csum);
1166d2cf3361SBenjamin LaHaise 		if (uh->check == 0)
1167d2cf3361SBenjamin LaHaise 			uh->check = CSUM_MANGLED_0;
1168d2cf3361SBenjamin LaHaise 	} else {
1169d2cf3361SBenjamin LaHaise 		skb->ip_summed = CHECKSUM_PARTIAL;
1170d2cf3361SBenjamin LaHaise 		skb->csum_start = skb_transport_header(skb) - skb->head;
1171d2cf3361SBenjamin LaHaise 		skb->csum_offset = offsetof(struct udphdr, check);
1172efe4208fSEric Dumazet 		uh->check = ~csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr,
1173d2cf3361SBenjamin LaHaise 					     udp_len, IPPROTO_UDP, 0);
1174d2cf3361SBenjamin LaHaise 	}
1175d2cf3361SBenjamin LaHaise }
1176d2cf3361SBenjamin LaHaise #endif
1177d2cf3361SBenjamin LaHaise 
1178fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be
1179fd558d18SJames Chapman  * inserted in the skb data before calling this function.
1180fd558d18SJames Chapman  */
1181fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1182fd558d18SJames Chapman {
1183fd558d18SJames Chapman 	int data_len = skb->len;
11840d76751fSJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
11850d76751fSJames Chapman 	struct sock *sk = tunnel->sock;
1186d9d8da80SDavid S. Miller 	struct flowi *fl;
1187fd558d18SJames Chapman 	struct udphdr *uh;
1188fd558d18SJames Chapman 	struct inet_sock *inet;
1189fd558d18SJames Chapman 	__wsum csum;
1190fd558d18SJames Chapman 	int headroom;
11910d76751fSJames Chapman 	int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
11920d76751fSJames Chapman 	int udp_len;
1193b8c84307SEric Dumazet 	int ret = NET_XMIT_SUCCESS;
1194fd558d18SJames Chapman 
1195fd558d18SJames Chapman 	/* Check that there's enough headroom in the skb to insert IP,
1196fd558d18SJames Chapman 	 * UDP and L2TP headers. If not enough, expand it to
1197fd558d18SJames Chapman 	 * make room. Adjust truesize.
1198fd558d18SJames Chapman 	 */
1199fd558d18SJames Chapman 	headroom = NET_SKB_PAD + sizeof(struct iphdr) +
12000d76751fSJames Chapman 		uhlen + hdr_len;
1201835acf5dSEric Dumazet 	if (skb_cow_head(skb, headroom)) {
1202b8c84307SEric Dumazet 		kfree_skb(skb);
1203b8c84307SEric Dumazet 		return NET_XMIT_DROP;
1204835acf5dSEric Dumazet 	}
1205fd558d18SJames Chapman 
1206fd558d18SJames Chapman 	/* Setup L2TP header */
1207f7faffa3SJames Chapman 	session->build_header(session, __skb_push(skb, hdr_len));
1208fd558d18SJames Chapman 
12090d76751fSJames Chapman 	/* Reset skb netfilter state */
1210fd558d18SJames Chapman 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1211fd558d18SJames Chapman 	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1212fd558d18SJames Chapman 			      IPSKB_REROUTED);
1213fd558d18SJames Chapman 	nf_reset(skb);
1214fd558d18SJames Chapman 
12156af88da1SDavid S. Miller 	bh_lock_sock(sk);
12166af88da1SDavid S. Miller 	if (sock_owned_by_user(sk)) {
1217b8c84307SEric Dumazet 		kfree_skb(skb);
1218b8c84307SEric Dumazet 		ret = NET_XMIT_DROP;
12196af88da1SDavid S. Miller 		goto out_unlock;
12206af88da1SDavid S. Miller 	}
12216af88da1SDavid S. Miller 
1222fd558d18SJames Chapman 	/* Get routing info from the tunnel socket */
1223fd558d18SJames Chapman 	skb_dst_drop(skb);
122471b1391aSFlorian Westphal 	skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
12250d76751fSJames Chapman 
1226d9d8da80SDavid S. Miller 	inet = inet_sk(sk);
1227d9d8da80SDavid S. Miller 	fl = &inet->cork.fl;
12280d76751fSJames Chapman 	switch (tunnel->encap) {
12290d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
12300d76751fSJames Chapman 		/* Setup UDP header */
12310d76751fSJames Chapman 		__skb_push(skb, sizeof(*uh));
12320d76751fSJames Chapman 		skb_reset_transport_header(skb);
12330d76751fSJames Chapman 		uh = udp_hdr(skb);
12340d76751fSJames Chapman 		uh->source = inet->inet_sport;
12350d76751fSJames Chapman 		uh->dest = inet->inet_dport;
12360d76751fSJames Chapman 		udp_len = uhlen + hdr_len + data_len;
12370d76751fSJames Chapman 		uh->len = htons(udp_len);
12380d76751fSJames Chapman 		uh->check = 0;
1239fd558d18SJames Chapman 
1240fd558d18SJames Chapman 		/* Calculate UDP checksum if configured to do so */
1241d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1242e18503f4SFrançois Cachereul 		if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1243d2cf3361SBenjamin LaHaise 			l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1244d2cf3361SBenjamin LaHaise 		else
1245d2cf3361SBenjamin LaHaise #endif
1246fd558d18SJames Chapman 		if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1247fd558d18SJames Chapman 			skb->ip_summed = CHECKSUM_NONE;
1248fd558d18SJames Chapman 		else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1249fd558d18SJames Chapman 			 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1250fd558d18SJames Chapman 			skb->ip_summed = CHECKSUM_COMPLETE;
1251fd558d18SJames Chapman 			csum = skb_checksum(skb, 0, udp_len, 0);
1252fd558d18SJames Chapman 			uh->check = csum_tcpudp_magic(inet->inet_saddr,
1253fd558d18SJames Chapman 						      inet->inet_daddr,
1254fd558d18SJames Chapman 						      udp_len, IPPROTO_UDP, csum);
1255fd558d18SJames Chapman 			if (uh->check == 0)
1256fd558d18SJames Chapman 				uh->check = CSUM_MANGLED_0;
1257fd558d18SJames Chapman 		} else {
1258fd558d18SJames Chapman 			skb->ip_summed = CHECKSUM_PARTIAL;
1259fd558d18SJames Chapman 			skb->csum_start = skb_transport_header(skb) - skb->head;
1260fd558d18SJames Chapman 			skb->csum_offset = offsetof(struct udphdr, check);
1261fd558d18SJames Chapman 			uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1262fd558d18SJames Chapman 						       inet->inet_daddr,
1263fd558d18SJames Chapman 						       udp_len, IPPROTO_UDP, 0);
1264fd558d18SJames Chapman 		}
12650d76751fSJames Chapman 		break;
12660d76751fSJames Chapman 
12670d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
12680d76751fSJames Chapman 		break;
12690d76751fSJames Chapman 	}
12700d76751fSJames Chapman 
1271d9d8da80SDavid S. Miller 	l2tp_xmit_core(session, skb, fl, data_len);
12726af88da1SDavid S. Miller out_unlock:
12736af88da1SDavid S. Miller 	bh_unlock_sock(sk);
1274fd558d18SJames Chapman 
1275b8c84307SEric Dumazet 	return ret;
1276fd558d18SJames Chapman }
1277fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1278fd558d18SJames Chapman 
1279fd558d18SJames Chapman /*****************************************************************************
1280fd558d18SJames Chapman  * Tinnel and session create/destroy.
1281fd558d18SJames Chapman  *****************************************************************************/
1282fd558d18SJames Chapman 
1283fd558d18SJames Chapman /* Tunnel socket destruct hook.
1284fd558d18SJames Chapman  * The tunnel context is deleted only when all session sockets have been
1285fd558d18SJames Chapman  * closed.
1286fd558d18SJames Chapman  */
1287fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk)
1288fd558d18SJames Chapman {
12898d8a51e2SDavid S. Miller 	struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1290f8ccac0eSTom Parkin 	struct l2tp_net *pn;
1291fd558d18SJames Chapman 
1292fd558d18SJames Chapman 	if (tunnel == NULL)
1293fd558d18SJames Chapman 		goto end;
1294fd558d18SJames Chapman 
1295a4ca44faSJoe Perches 	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1296fd558d18SJames Chapman 
1297fd558d18SJames Chapman 
1298f8ccac0eSTom Parkin 	/* Disable udp encapsulation */
12990d76751fSJames Chapman 	switch (tunnel->encap) {
13000d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1301fd558d18SJames Chapman 		/* No longer an encapsulation socket. See net/ipv4/udp.c */
1302fd558d18SJames Chapman 		(udp_sk(sk))->encap_type = 0;
1303fd558d18SJames Chapman 		(udp_sk(sk))->encap_rcv = NULL;
13049980d001STom Parkin 		(udp_sk(sk))->encap_destroy = NULL;
13050d76751fSJames Chapman 		break;
13060d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
13070d76751fSJames Chapman 		break;
13080d76751fSJames Chapman 	}
1309fd558d18SJames Chapman 
1310fd558d18SJames Chapman 	/* Remove hooks into tunnel socket */
1311fd558d18SJames Chapman 	sk->sk_destruct = tunnel->old_sk_destruct;
1312fd558d18SJames Chapman 	sk->sk_user_data = NULL;
1313f8ccac0eSTom Parkin 	tunnel->sock = NULL;
1314f8ccac0eSTom Parkin 
1315f8ccac0eSTom Parkin 	/* Remove the tunnel struct from the tunnel list */
1316f8ccac0eSTom Parkin 	pn = l2tp_pernet(tunnel->l2tp_net);
1317f8ccac0eSTom Parkin 	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1318f8ccac0eSTom Parkin 	list_del_rcu(&tunnel->list);
1319f8ccac0eSTom Parkin 	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1320f8ccac0eSTom Parkin 	atomic_dec(&l2tp_tunnel_count);
1321f8ccac0eSTom Parkin 
1322f8ccac0eSTom Parkin 	l2tp_tunnel_closeall(tunnel);
1323f8ccac0eSTom Parkin 	l2tp_tunnel_dec_refcount(tunnel);
1324fd558d18SJames Chapman 
1325fd558d18SJames Chapman 	/* Call the original destructor */
1326fd558d18SJames Chapman 	if (sk->sk_destruct)
1327fd558d18SJames Chapman 		(*sk->sk_destruct)(sk);
1328fd558d18SJames Chapman end:
1329fd558d18SJames Chapman 	return;
1330fd558d18SJames Chapman }
1331fd558d18SJames Chapman 
1332fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too.
1333fd558d18SJames Chapman  */
1334e34f4c70STom Parkin void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1335fd558d18SJames Chapman {
1336fd558d18SJames Chapman 	int hash;
1337fd558d18SJames Chapman 	struct hlist_node *walk;
1338fd558d18SJames Chapman 	struct hlist_node *tmp;
1339fd558d18SJames Chapman 	struct l2tp_session *session;
1340fd558d18SJames Chapman 
1341fd558d18SJames Chapman 	BUG_ON(tunnel == NULL);
1342fd558d18SJames Chapman 
1343a4ca44faSJoe Perches 	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1344a4ca44faSJoe Perches 		  tunnel->name);
1345fd558d18SJames Chapman 
1346fd558d18SJames Chapman 	write_lock_bh(&tunnel->hlist_lock);
1347fd558d18SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1348fd558d18SJames Chapman again:
1349fd558d18SJames Chapman 		hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1350fd558d18SJames Chapman 			session = hlist_entry(walk, struct l2tp_session, hlist);
1351fd558d18SJames Chapman 
1352a4ca44faSJoe Perches 			l2tp_info(session, L2TP_MSG_CONTROL,
1353fd558d18SJames Chapman 				  "%s: closing session\n", session->name);
1354fd558d18SJames Chapman 
1355fd558d18SJames Chapman 			hlist_del_init(&session->hlist);
1356fd558d18SJames Chapman 
1357fd558d18SJames Chapman 			if (session->ref != NULL)
1358fd558d18SJames Chapman 				(*session->ref)(session);
1359fd558d18SJames Chapman 
1360fd558d18SJames Chapman 			write_unlock_bh(&tunnel->hlist_lock);
1361fd558d18SJames Chapman 
1362f6e16b29STom Parkin 			__l2tp_session_unhash(session);
13634c6e2fd3STom Parkin 			l2tp_session_queue_purge(session);
13644c6e2fd3STom Parkin 
1365fd558d18SJames Chapman 			if (session->session_close != NULL)
1366fd558d18SJames Chapman 				(*session->session_close)(session);
1367fd558d18SJames Chapman 
1368fd558d18SJames Chapman 			if (session->deref != NULL)
1369fd558d18SJames Chapman 				(*session->deref)(session);
1370fd558d18SJames Chapman 
13719980d001STom Parkin 			l2tp_session_dec_refcount(session);
13729980d001STom Parkin 
1373fd558d18SJames Chapman 			write_lock_bh(&tunnel->hlist_lock);
1374fd558d18SJames Chapman 
1375fd558d18SJames Chapman 			/* Now restart from the beginning of this hash
1376fd558d18SJames Chapman 			 * chain.  We always remove a session from the
1377fd558d18SJames Chapman 			 * list so we are guaranteed to make forward
1378fd558d18SJames Chapman 			 * progress.
1379fd558d18SJames Chapman 			 */
1380fd558d18SJames Chapman 			goto again;
1381fd558d18SJames Chapman 		}
1382fd558d18SJames Chapman 	}
1383fd558d18SJames Chapman 	write_unlock_bh(&tunnel->hlist_lock);
1384fd558d18SJames Chapman }
1385e34f4c70STom Parkin EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1386fd558d18SJames Chapman 
13879980d001STom Parkin /* Tunnel socket destroy hook for UDP encapsulation */
13889980d001STom Parkin static void l2tp_udp_encap_destroy(struct sock *sk)
13899980d001STom Parkin {
13909980d001STom Parkin 	struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
13919980d001STom Parkin 	if (tunnel) {
13929980d001STom Parkin 		l2tp_tunnel_closeall(tunnel);
13939980d001STom Parkin 		sock_put(sk);
13949980d001STom Parkin 	}
13959980d001STom Parkin }
13969980d001STom Parkin 
1397fd558d18SJames Chapman /* Really kill the tunnel.
1398fd558d18SJames Chapman  * Come here only when all sessions have been cleared from the tunnel.
1399fd558d18SJames Chapman  */
1400fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1401fd558d18SJames Chapman {
1402fd558d18SJames Chapman 	BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1403fd558d18SJames Chapman 	BUG_ON(tunnel->sock != NULL);
1404a4ca44faSJoe Perches 	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
140599469c32Sxeb@mail.ru 	kfree_rcu(tunnel, rcu);
1406f8ccac0eSTom Parkin }
1407fd558d18SJames Chapman 
1408f8ccac0eSTom Parkin /* Workqueue tunnel deletion function */
1409f8ccac0eSTom Parkin static void l2tp_tunnel_del_work(struct work_struct *work)
1410f8ccac0eSTom Parkin {
1411f8ccac0eSTom Parkin 	struct l2tp_tunnel *tunnel = NULL;
1412f8ccac0eSTom Parkin 	struct socket *sock = NULL;
1413f8ccac0eSTom Parkin 	struct sock *sk = NULL;
1414f8ccac0eSTom Parkin 
1415f8ccac0eSTom Parkin 	tunnel = container_of(work, struct l2tp_tunnel, del_work);
1416f8ccac0eSTom Parkin 	sk = l2tp_tunnel_sock_lookup(tunnel);
1417f8ccac0eSTom Parkin 	if (!sk)
1418f8ccac0eSTom Parkin 		return;
1419f8ccac0eSTom Parkin 
1420f8ccac0eSTom Parkin 	sock = sk->sk_socket;
1421f8ccac0eSTom Parkin 
142202d13ed5STom Parkin 	/* If the tunnel socket was created by userspace, then go through the
142302d13ed5STom Parkin 	 * inet layer to shut the socket down, and let userspace close it.
142402d13ed5STom Parkin 	 * Otherwise, if we created the socket directly within the kernel, use
142502d13ed5STom Parkin 	 * the sk API to release it here.
1426167eb17eSTom Parkin 	 * In either case the tunnel resources are freed in the socket
1427167eb17eSTom Parkin 	 * destructor when the tunnel socket goes away.
1428f8ccac0eSTom Parkin 	 */
142902d13ed5STom Parkin 	if (tunnel->fd >= 0) {
143002d13ed5STom Parkin 		if (sock)
143102d13ed5STom Parkin 			inet_shutdown(sock, 2);
143202d13ed5STom Parkin 	} else {
143302d13ed5STom Parkin 		if (sock)
1434167eb17eSTom Parkin 			kernel_sock_shutdown(sock, SHUT_RDWR);
1435167eb17eSTom Parkin 		sk_release_kernel(sk);
1436167eb17eSTom Parkin 	}
1437f8ccac0eSTom Parkin 
1438f8ccac0eSTom Parkin 	l2tp_tunnel_sock_put(sk);
1439fd558d18SJames Chapman }
1440fd558d18SJames Chapman 
1441789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by
1442789a4a2cSJames Chapman  * userspace. This is used for static tunnels where there is no
1443789a4a2cSJames Chapman  * managing L2TP daemon.
1444167eb17eSTom Parkin  *
1445167eb17eSTom Parkin  * Since we don't want these sockets to keep a namespace alive by
1446167eb17eSTom Parkin  * themselves, we drop the socket's namespace refcount after creation.
1447167eb17eSTom Parkin  * These sockets are freed when the namespace exits using the pernet
1448167eb17eSTom Parkin  * exit hook.
1449789a4a2cSJames Chapman  */
1450167eb17eSTom Parkin static int l2tp_tunnel_sock_create(struct net *net,
1451167eb17eSTom Parkin 				u32 tunnel_id,
1452167eb17eSTom Parkin 				u32 peer_tunnel_id,
1453167eb17eSTom Parkin 				struct l2tp_tunnel_cfg *cfg,
1454167eb17eSTom Parkin 				struct socket **sockp)
1455789a4a2cSJames Chapman {
1456789a4a2cSJames Chapman 	int err = -EINVAL;
14577bddd0dbSEric Dumazet 	struct socket *sock = NULL;
1458167eb17eSTom Parkin 	struct sockaddr_in udp_addr = {0};
1459167eb17eSTom Parkin 	struct sockaddr_l2tpip ip_addr = {0};
1460167eb17eSTom Parkin #if IS_ENABLED(CONFIG_IPV6)
1461167eb17eSTom Parkin 	struct sockaddr_in6 udp6_addr = {0};
1462167eb17eSTom Parkin 	struct sockaddr_l2tpip6 ip6_addr = {0};
1463167eb17eSTom Parkin #endif
1464789a4a2cSJames Chapman 
1465789a4a2cSJames Chapman 	switch (cfg->encap) {
1466789a4a2cSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1467f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
1468f9bac8dfSChris Elston 		if (cfg->local_ip6 && cfg->peer_ip6) {
1469167eb17eSTom Parkin 			err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
1470f9bac8dfSChris Elston 			if (err < 0)
1471f9bac8dfSChris Elston 				goto out;
1472f9bac8dfSChris Elston 
1473167eb17eSTom Parkin 			sk_change_net(sock->sk, net);
1474f9bac8dfSChris Elston 
1475f9bac8dfSChris Elston 			udp6_addr.sin6_family = AF_INET6;
1476f9bac8dfSChris Elston 			memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1477f9bac8dfSChris Elston 			       sizeof(udp6_addr.sin6_addr));
1478f9bac8dfSChris Elston 			udp6_addr.sin6_port = htons(cfg->local_udp_port);
1479f9bac8dfSChris Elston 			err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1480f9bac8dfSChris Elston 					  sizeof(udp6_addr));
1481f9bac8dfSChris Elston 			if (err < 0)
1482f9bac8dfSChris Elston 				goto out;
1483f9bac8dfSChris Elston 
1484f9bac8dfSChris Elston 			udp6_addr.sin6_family = AF_INET6;
1485f9bac8dfSChris Elston 			memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1486f9bac8dfSChris Elston 			       sizeof(udp6_addr.sin6_addr));
1487f9bac8dfSChris Elston 			udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1488f9bac8dfSChris Elston 			err = kernel_connect(sock,
1489f9bac8dfSChris Elston 					     (struct sockaddr *) &udp6_addr,
1490f9bac8dfSChris Elston 					     sizeof(udp6_addr), 0);
1491f9bac8dfSChris Elston 			if (err < 0)
1492f9bac8dfSChris Elston 				goto out;
1493f9bac8dfSChris Elston 		} else
1494f9bac8dfSChris Elston #endif
1495f9bac8dfSChris Elston 		{
1496167eb17eSTom Parkin 			err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
1497789a4a2cSJames Chapman 			if (err < 0)
1498789a4a2cSJames Chapman 				goto out;
1499789a4a2cSJames Chapman 
1500167eb17eSTom Parkin 			sk_change_net(sock->sk, net);
1501789a4a2cSJames Chapman 
1502789a4a2cSJames Chapman 			udp_addr.sin_family = AF_INET;
1503789a4a2cSJames Chapman 			udp_addr.sin_addr = cfg->local_ip;
1504789a4a2cSJames Chapman 			udp_addr.sin_port = htons(cfg->local_udp_port);
1505f9bac8dfSChris Elston 			err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1506f9bac8dfSChris Elston 					  sizeof(udp_addr));
1507789a4a2cSJames Chapman 			if (err < 0)
1508789a4a2cSJames Chapman 				goto out;
1509789a4a2cSJames Chapman 
1510789a4a2cSJames Chapman 			udp_addr.sin_family = AF_INET;
1511789a4a2cSJames Chapman 			udp_addr.sin_addr = cfg->peer_ip;
1512789a4a2cSJames Chapman 			udp_addr.sin_port = htons(cfg->peer_udp_port);
1513f9bac8dfSChris Elston 			err = kernel_connect(sock,
1514f9bac8dfSChris Elston 					     (struct sockaddr *) &udp_addr,
1515f9bac8dfSChris Elston 					     sizeof(udp_addr), 0);
1516789a4a2cSJames Chapman 			if (err < 0)
1517789a4a2cSJames Chapman 				goto out;
1518f9bac8dfSChris Elston 		}
1519789a4a2cSJames Chapman 
1520789a4a2cSJames Chapman 		if (!cfg->use_udp_checksums)
1521789a4a2cSJames Chapman 			sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1522789a4a2cSJames Chapman 
1523789a4a2cSJames Chapman 		break;
1524789a4a2cSJames Chapman 
1525789a4a2cSJames Chapman 	case L2TP_ENCAPTYPE_IP:
1526f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
1527f9bac8dfSChris Elston 		if (cfg->local_ip6 && cfg->peer_ip6) {
1528167eb17eSTom Parkin 			err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1529167eb17eSTom Parkin 					  IPPROTO_L2TP, &sock);
15305dac94e1SJames Chapman 			if (err < 0)
1531f9bac8dfSChris Elston 				goto out;
15325dac94e1SJames Chapman 
1533167eb17eSTom Parkin 			sk_change_net(sock->sk, net);
15345dac94e1SJames Chapman 
15355dac94e1SJames Chapman 			ip6_addr.l2tp_family = AF_INET6;
15365dac94e1SJames Chapman 			memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
15375dac94e1SJames Chapman 			       sizeof(ip6_addr.l2tp_addr));
15385dac94e1SJames Chapman 			ip6_addr.l2tp_conn_id = tunnel_id;
15395dac94e1SJames Chapman 			err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
15405dac94e1SJames Chapman 					  sizeof(ip6_addr));
15415dac94e1SJames Chapman 			if (err < 0)
15425dac94e1SJames Chapman 				goto out;
15435dac94e1SJames Chapman 
15445dac94e1SJames Chapman 			ip6_addr.l2tp_family = AF_INET6;
15455dac94e1SJames Chapman 			memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
15465dac94e1SJames Chapman 			       sizeof(ip6_addr.l2tp_addr));
15475dac94e1SJames Chapman 			ip6_addr.l2tp_conn_id = peer_tunnel_id;
15485dac94e1SJames Chapman 			err = kernel_connect(sock,
15495dac94e1SJames Chapman 					     (struct sockaddr *) &ip6_addr,
15505dac94e1SJames Chapman 					     sizeof(ip6_addr), 0);
15515dac94e1SJames Chapman 			if (err < 0)
15525dac94e1SJames Chapman 				goto out;
15535dac94e1SJames Chapman 		} else
1554f9bac8dfSChris Elston #endif
15555dac94e1SJames Chapman 		{
1556167eb17eSTom Parkin 			err = sock_create_kern(AF_INET, SOCK_DGRAM,
1557167eb17eSTom Parkin 					  IPPROTO_L2TP, &sock);
1558789a4a2cSJames Chapman 			if (err < 0)
1559789a4a2cSJames Chapman 				goto out;
1560789a4a2cSJames Chapman 
1561167eb17eSTom Parkin 			sk_change_net(sock->sk, net);
1562789a4a2cSJames Chapman 
1563789a4a2cSJames Chapman 			ip_addr.l2tp_family = AF_INET;
1564789a4a2cSJames Chapman 			ip_addr.l2tp_addr = cfg->local_ip;
1565789a4a2cSJames Chapman 			ip_addr.l2tp_conn_id = tunnel_id;
15665dac94e1SJames Chapman 			err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
15675dac94e1SJames Chapman 					  sizeof(ip_addr));
1568789a4a2cSJames Chapman 			if (err < 0)
1569789a4a2cSJames Chapman 				goto out;
1570789a4a2cSJames Chapman 
1571789a4a2cSJames Chapman 			ip_addr.l2tp_family = AF_INET;
1572789a4a2cSJames Chapman 			ip_addr.l2tp_addr = cfg->peer_ip;
1573789a4a2cSJames Chapman 			ip_addr.l2tp_conn_id = peer_tunnel_id;
15745dac94e1SJames Chapman 			err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
15755dac94e1SJames Chapman 					     sizeof(ip_addr), 0);
1576789a4a2cSJames Chapman 			if (err < 0)
1577789a4a2cSJames Chapman 				goto out;
15785dac94e1SJames Chapman 		}
1579789a4a2cSJames Chapman 		break;
1580789a4a2cSJames Chapman 
1581789a4a2cSJames Chapman 	default:
1582789a4a2cSJames Chapman 		goto out;
1583789a4a2cSJames Chapman 	}
1584789a4a2cSJames Chapman 
1585789a4a2cSJames Chapman out:
1586167eb17eSTom Parkin 	*sockp = sock;
1587789a4a2cSJames Chapman 	if ((err < 0) && sock) {
1588167eb17eSTom Parkin 		kernel_sock_shutdown(sock, SHUT_RDWR);
1589167eb17eSTom Parkin 		sk_release_kernel(sock->sk);
1590789a4a2cSJames Chapman 		*sockp = NULL;
1591789a4a2cSJames Chapman 	}
1592789a4a2cSJames Chapman 
1593789a4a2cSJames Chapman 	return err;
1594789a4a2cSJames Chapman }
1595789a4a2cSJames Chapman 
159637159ef2SEric Dumazet static struct lock_class_key l2tp_socket_class;
159737159ef2SEric Dumazet 
1598fd558d18SJames 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)
1599fd558d18SJames Chapman {
1600fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = NULL;
1601fd558d18SJames Chapman 	int err;
1602fd558d18SJames Chapman 	struct socket *sock = NULL;
1603fd558d18SJames Chapman 	struct sock *sk = NULL;
1604fd558d18SJames Chapman 	struct l2tp_net *pn;
16050d76751fSJames Chapman 	enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1606fd558d18SJames Chapman 
1607fd558d18SJames Chapman 	/* Get the tunnel socket from the fd, which was opened by
1608789a4a2cSJames Chapman 	 * the userspace L2TP daemon. If not specified, create a
1609789a4a2cSJames Chapman 	 * kernel socket.
1610fd558d18SJames Chapman 	 */
1611789a4a2cSJames Chapman 	if (fd < 0) {
1612167eb17eSTom Parkin 		err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1613167eb17eSTom Parkin 				cfg, &sock);
1614789a4a2cSJames Chapman 		if (err < 0)
1615789a4a2cSJames Chapman 			goto err;
1616789a4a2cSJames Chapman 	} else {
1617fd558d18SJames Chapman 		sock = sockfd_lookup(fd, &err);
1618fd558d18SJames Chapman 		if (!sock) {
1619cbb95e0cSTom Parkin 			pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1620fd558d18SJames Chapman 			       tunnel_id, fd, err);
1621cbb95e0cSTom Parkin 			err = -EBADF;
1622cbb95e0cSTom Parkin 			goto err;
1623cbb95e0cSTom Parkin 		}
1624cbb95e0cSTom Parkin 
1625cbb95e0cSTom Parkin 		/* Reject namespace mismatches */
1626cbb95e0cSTom Parkin 		if (!net_eq(sock_net(sock->sk), net)) {
1627cbb95e0cSTom Parkin 			pr_err("tunl %u: netns mismatch\n", tunnel_id);
1628cbb95e0cSTom Parkin 			err = -EINVAL;
1629fd558d18SJames Chapman 			goto err;
1630fd558d18SJames Chapman 		}
1631789a4a2cSJames Chapman 	}
1632fd558d18SJames Chapman 
1633fd558d18SJames Chapman 	sk = sock->sk;
1634fd558d18SJames Chapman 
16350d76751fSJames Chapman 	if (cfg != NULL)
16360d76751fSJames Chapman 		encap = cfg->encap;
16370d76751fSJames Chapman 
1638fd558d18SJames Chapman 	/* Quick sanity checks */
16390d76751fSJames Chapman 	switch (encap) {
16400d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1641fd558d18SJames Chapman 		err = -EPROTONOSUPPORT;
1642fd558d18SJames Chapman 		if (sk->sk_protocol != IPPROTO_UDP) {
1643a4ca44faSJoe Perches 			pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1644fd558d18SJames Chapman 			       tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1645fd558d18SJames Chapman 			goto err;
1646fd558d18SJames Chapman 		}
16470d76751fSJames Chapman 		break;
16480d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
16490d76751fSJames Chapman 		err = -EPROTONOSUPPORT;
16500d76751fSJames Chapman 		if (sk->sk_protocol != IPPROTO_L2TP) {
1651a4ca44faSJoe Perches 			pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
16520d76751fSJames Chapman 			       tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1653fd558d18SJames Chapman 			goto err;
1654fd558d18SJames Chapman 		}
16550d76751fSJames Chapman 		break;
16560d76751fSJames Chapman 	}
1657fd558d18SJames Chapman 
1658fd558d18SJames Chapman 	/* Check if this socket has already been prepped */
16598d8a51e2SDavid S. Miller 	tunnel = l2tp_tunnel(sk);
1660fd558d18SJames Chapman 	if (tunnel != NULL) {
1661fd558d18SJames Chapman 		/* This socket has already been prepped */
1662fd558d18SJames Chapman 		err = -EBUSY;
1663fd558d18SJames Chapman 		goto err;
1664fd558d18SJames Chapman 	}
1665fd558d18SJames Chapman 
1666fd558d18SJames Chapman 	tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1667fd558d18SJames Chapman 	if (tunnel == NULL) {
1668fd558d18SJames Chapman 		err = -ENOMEM;
1669fd558d18SJames Chapman 		goto err;
1670fd558d18SJames Chapman 	}
1671fd558d18SJames Chapman 
1672fd558d18SJames Chapman 	tunnel->version = version;
1673fd558d18SJames Chapman 	tunnel->tunnel_id = tunnel_id;
1674fd558d18SJames Chapman 	tunnel->peer_tunnel_id = peer_tunnel_id;
1675fd558d18SJames Chapman 	tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1676fd558d18SJames Chapman 
1677fd558d18SJames Chapman 	tunnel->magic = L2TP_TUNNEL_MAGIC;
1678fd558d18SJames Chapman 	sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1679fd558d18SJames Chapman 	rwlock_init(&tunnel->hlist_lock);
1680fd558d18SJames Chapman 
1681fd558d18SJames Chapman 	/* The net we belong to */
1682fd558d18SJames Chapman 	tunnel->l2tp_net = net;
1683fd558d18SJames Chapman 	pn = l2tp_pernet(net);
1684fd558d18SJames Chapman 
16850d76751fSJames Chapman 	if (cfg != NULL)
1686fd558d18SJames Chapman 		tunnel->debug = cfg->debug;
1687fd558d18SJames Chapman 
1688e18503f4SFrançois Cachereul #if IS_ENABLED(CONFIG_IPV6)
1689e18503f4SFrançois Cachereul 	if (sk->sk_family == PF_INET6) {
1690e18503f4SFrançois Cachereul 		struct ipv6_pinfo *np = inet6_sk(sk);
1691e18503f4SFrançois Cachereul 
1692e18503f4SFrançois Cachereul 		if (ipv6_addr_v4mapped(&np->saddr) &&
1693efe4208fSEric Dumazet 		    ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
1694e18503f4SFrançois Cachereul 			struct inet_sock *inet = inet_sk(sk);
1695e18503f4SFrançois Cachereul 
1696e18503f4SFrançois Cachereul 			tunnel->v4mapped = true;
1697e18503f4SFrançois Cachereul 			inet->inet_saddr = np->saddr.s6_addr32[3];
1698efe4208fSEric Dumazet 			inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1699efe4208fSEric Dumazet 			inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
1700e18503f4SFrançois Cachereul 		} else {
1701e18503f4SFrançois Cachereul 			tunnel->v4mapped = false;
1702e18503f4SFrançois Cachereul 		}
1703e18503f4SFrançois Cachereul 	}
1704e18503f4SFrançois Cachereul #endif
1705e18503f4SFrançois Cachereul 
1706fd558d18SJames Chapman 	/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
17070d76751fSJames Chapman 	tunnel->encap = encap;
17080d76751fSJames Chapman 	if (encap == L2TP_ENCAPTYPE_UDP) {
17090d76751fSJames Chapman 		/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1710fd558d18SJames Chapman 		udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1711fd558d18SJames Chapman 		udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
17129980d001STom Parkin 		udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
1713d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1714e18503f4SFrançois Cachereul 		if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1715d2cf3361SBenjamin LaHaise 			udpv6_encap_enable();
1716d2cf3361SBenjamin LaHaise 		else
1717d2cf3361SBenjamin LaHaise #endif
1718447167bfSEric Dumazet 		udp_encap_enable();
17190d76751fSJames Chapman 	}
1720fd558d18SJames Chapman 
1721fd558d18SJames Chapman 	sk->sk_user_data = tunnel;
1722fd558d18SJames Chapman 
1723fd558d18SJames Chapman 	/* Hook on the tunnel socket destructor so that we can cleanup
1724fd558d18SJames Chapman 	 * if the tunnel socket goes away.
1725fd558d18SJames Chapman 	 */
1726fd558d18SJames Chapman 	tunnel->old_sk_destruct = sk->sk_destruct;
1727fd558d18SJames Chapman 	sk->sk_destruct = &l2tp_tunnel_destruct;
1728fd558d18SJames Chapman 	tunnel->sock = sk;
172980d84ef3STom Parkin 	tunnel->fd = fd;
173037159ef2SEric Dumazet 	lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
173137159ef2SEric Dumazet 
1732fd558d18SJames Chapman 	sk->sk_allocation = GFP_ATOMIC;
1733fd558d18SJames Chapman 
1734f8ccac0eSTom Parkin 	/* Init delete workqueue struct */
1735f8ccac0eSTom Parkin 	INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1736f8ccac0eSTom Parkin 
1737fd558d18SJames Chapman 	/* Add tunnel to our list */
1738fd558d18SJames Chapman 	INIT_LIST_HEAD(&tunnel->list);
1739fd558d18SJames Chapman 	atomic_inc(&l2tp_tunnel_count);
1740fd558d18SJames Chapman 
1741fd558d18SJames Chapman 	/* Bump the reference count. The tunnel context is deleted
17421769192aSEric Dumazet 	 * only when this drops to zero. Must be done before list insertion
1743fd558d18SJames Chapman 	 */
1744fd558d18SJames Chapman 	l2tp_tunnel_inc_refcount(tunnel);
17451769192aSEric Dumazet 	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
17461769192aSEric Dumazet 	list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
17471769192aSEric Dumazet 	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1748fd558d18SJames Chapman 
1749fd558d18SJames Chapman 	err = 0;
1750fd558d18SJames Chapman err:
1751fd558d18SJames Chapman 	if (tunnelp)
1752fd558d18SJames Chapman 		*tunnelp = tunnel;
1753fd558d18SJames Chapman 
1754789a4a2cSJames Chapman 	/* If tunnel's socket was created by the kernel, it doesn't
1755789a4a2cSJames Chapman 	 *  have a file.
1756789a4a2cSJames Chapman 	 */
1757789a4a2cSJames Chapman 	if (sock && sock->file)
1758fd558d18SJames Chapman 		sockfd_put(sock);
1759fd558d18SJames Chapman 
1760fd558d18SJames Chapman 	return err;
1761fd558d18SJames Chapman }
1762fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1763fd558d18SJames Chapman 
1764309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command.
1765309795f4SJames Chapman  */
1766309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1767309795f4SJames Chapman {
17682b551c6eSTom Parkin 	l2tp_tunnel_closeall(tunnel);
1769f8ccac0eSTom Parkin 	return (false == queue_work(l2tp_wq, &tunnel->del_work));
1770309795f4SJames Chapman }
1771309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1772309795f4SJames Chapman 
1773fd558d18SJames Chapman /* Really kill the session.
1774fd558d18SJames Chapman  */
1775fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session)
1776fd558d18SJames Chapman {
1777f6e16b29STom Parkin 	struct l2tp_tunnel *tunnel = session->tunnel;
1778fd558d18SJames Chapman 
1779fd558d18SJames Chapman 	BUG_ON(atomic_read(&session->ref_count) != 0);
1780fd558d18SJames Chapman 
1781f6e16b29STom Parkin 	if (tunnel) {
1782fd558d18SJames Chapman 		BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1783fd558d18SJames Chapman 		if (session->session_id != 0)
1784fd558d18SJames Chapman 			atomic_dec(&l2tp_session_count);
1785fd558d18SJames Chapman 		sock_put(tunnel->sock);
1786fd558d18SJames Chapman 		session->tunnel = NULL;
1787fd558d18SJames Chapman 		l2tp_tunnel_dec_refcount(tunnel);
1788fd558d18SJames Chapman 	}
1789fd558d18SJames Chapman 
1790fd558d18SJames Chapman 	kfree(session);
1791fd558d18SJames Chapman }
1792fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free);
1793fd558d18SJames Chapman 
1794f6e16b29STom Parkin /* Remove an l2tp session from l2tp_core's hash lists.
1795f6e16b29STom Parkin  * Provides a tidyup interface for pseudowire code which can't just route all
1796f6e16b29STom Parkin  * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1797f6e16b29STom Parkin  * callback.
1798f6e16b29STom Parkin  */
1799f6e16b29STom Parkin void __l2tp_session_unhash(struct l2tp_session *session)
1800f6e16b29STom Parkin {
1801f6e16b29STom Parkin 	struct l2tp_tunnel *tunnel = session->tunnel;
1802f6e16b29STom Parkin 
1803f6e16b29STom Parkin 	/* Remove the session from core hashes */
1804f6e16b29STom Parkin 	if (tunnel) {
1805f6e16b29STom Parkin 		/* Remove from the per-tunnel hash */
1806f6e16b29STom Parkin 		write_lock_bh(&tunnel->hlist_lock);
1807f6e16b29STom Parkin 		hlist_del_init(&session->hlist);
1808f6e16b29STom Parkin 		write_unlock_bh(&tunnel->hlist_lock);
1809f6e16b29STom Parkin 
1810f6e16b29STom Parkin 		/* For L2TPv3 we have a per-net hash: remove from there, too */
1811f6e16b29STom Parkin 		if (tunnel->version != L2TP_HDR_VER_2) {
1812f6e16b29STom Parkin 			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1813f6e16b29STom Parkin 			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1814f6e16b29STom Parkin 			hlist_del_init_rcu(&session->global_hlist);
1815f6e16b29STom Parkin 			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1816f6e16b29STom Parkin 			synchronize_rcu();
1817f6e16b29STom Parkin 		}
1818f6e16b29STom Parkin 	}
1819f6e16b29STom Parkin }
1820f6e16b29STom Parkin EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1821f6e16b29STom Parkin 
1822309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by
1823309795f4SJames Chapman    pseudowire modules.
1824309795f4SJames Chapman  */
1825309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session)
1826309795f4SJames Chapman {
1827f6e16b29STom Parkin 	if (session->ref)
1828f6e16b29STom Parkin 		(*session->ref)(session);
1829f6e16b29STom Parkin 	__l2tp_session_unhash(session);
18304c6e2fd3STom Parkin 	l2tp_session_queue_purge(session);
1831309795f4SJames Chapman 	if (session->session_close != NULL)
1832309795f4SJames Chapman 		(*session->session_close)(session);
1833f6e16b29STom Parkin 	if (session->deref)
18341b7c92b9SDan Carpenter 		(*session->deref)(session);
1835309795f4SJames Chapman 	l2tp_session_dec_refcount(session);
1836309795f4SJames Chapman 	return 0;
1837309795f4SJames Chapman }
1838309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete);
1839309795f4SJames Chapman 
1840f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or
1841f7faffa3SJames Chapman  * l2specific_len parameters are set.
1842f7faffa3SJames Chapman  */
1843bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1844f7faffa3SJames Chapman {
1845f7faffa3SJames Chapman 	if (version == L2TP_HDR_VER_2) {
1846f7faffa3SJames Chapman 		session->hdr_len = 6;
1847f7faffa3SJames Chapman 		if (session->send_seq)
1848f7faffa3SJames Chapman 			session->hdr_len += 4;
1849f7faffa3SJames Chapman 	} else {
18500d76751fSJames Chapman 		session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
18510d76751fSJames Chapman 		if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
18520d76751fSJames Chapman 			session->hdr_len += 4;
1853f7faffa3SJames Chapman 	}
1854f7faffa3SJames Chapman 
1855f7faffa3SJames Chapman }
1856bb5016eaSGuillaume Nault EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1857f7faffa3SJames Chapman 
1858fd558d18SJames 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)
1859fd558d18SJames Chapman {
1860fd558d18SJames Chapman 	struct l2tp_session *session;
1861fd558d18SJames Chapman 
1862fd558d18SJames Chapman 	session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1863fd558d18SJames Chapman 	if (session != NULL) {
1864fd558d18SJames Chapman 		session->magic = L2TP_SESSION_MAGIC;
1865fd558d18SJames Chapman 		session->tunnel = tunnel;
1866fd558d18SJames Chapman 
1867fd558d18SJames Chapman 		session->session_id = session_id;
1868fd558d18SJames Chapman 		session->peer_session_id = peer_session_id;
1869d301e325SJames Chapman 		session->nr = 0;
18708a1631d5SJames Chapman 		if (tunnel->version == L2TP_HDR_VER_2)
18718a1631d5SJames Chapman 			session->nr_max = 0xffff;
18728a1631d5SJames Chapman 		else
18738a1631d5SJames Chapman 			session->nr_max = 0xffffff;
18748a1631d5SJames Chapman 		session->nr_window_size = session->nr_max / 2;
1875a0dbd822SJames Chapman 		session->nr_oos_count_max = 4;
1876a0dbd822SJames Chapman 
1877a0dbd822SJames Chapman 		/* Use NR of first received packet */
1878a0dbd822SJames Chapman 		session->reorder_skip = 1;
1879fd558d18SJames Chapman 
1880fd558d18SJames Chapman 		sprintf(&session->name[0], "sess %u/%u",
1881fd558d18SJames Chapman 			tunnel->tunnel_id, session->session_id);
1882fd558d18SJames Chapman 
1883fd558d18SJames Chapman 		skb_queue_head_init(&session->reorder_q);
1884fd558d18SJames Chapman 
1885fd558d18SJames Chapman 		INIT_HLIST_NODE(&session->hlist);
1886f7faffa3SJames Chapman 		INIT_HLIST_NODE(&session->global_hlist);
1887fd558d18SJames Chapman 
1888fd558d18SJames Chapman 		/* Inherit debug options from tunnel */
1889fd558d18SJames Chapman 		session->debug = tunnel->debug;
1890fd558d18SJames Chapman 
1891fd558d18SJames Chapman 		if (cfg) {
1892f7faffa3SJames Chapman 			session->pwtype = cfg->pw_type;
1893fd558d18SJames Chapman 			session->debug = cfg->debug;
1894fd558d18SJames Chapman 			session->mtu = cfg->mtu;
1895fd558d18SJames Chapman 			session->mru = cfg->mru;
1896fd558d18SJames Chapman 			session->send_seq = cfg->send_seq;
1897fd558d18SJames Chapman 			session->recv_seq = cfg->recv_seq;
1898fd558d18SJames Chapman 			session->lns_mode = cfg->lns_mode;
1899f7faffa3SJames Chapman 			session->reorder_timeout = cfg->reorder_timeout;
1900f7faffa3SJames Chapman 			session->offset = cfg->offset;
1901f7faffa3SJames Chapman 			session->l2specific_type = cfg->l2specific_type;
1902f7faffa3SJames Chapman 			session->l2specific_len = cfg->l2specific_len;
1903f7faffa3SJames Chapman 			session->cookie_len = cfg->cookie_len;
1904f7faffa3SJames Chapman 			memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1905f7faffa3SJames Chapman 			session->peer_cookie_len = cfg->peer_cookie_len;
1906f7faffa3SJames Chapman 			memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1907fd558d18SJames Chapman 		}
1908fd558d18SJames Chapman 
1909f7faffa3SJames Chapman 		if (tunnel->version == L2TP_HDR_VER_2)
1910f7faffa3SJames Chapman 			session->build_header = l2tp_build_l2tpv2_header;
1911f7faffa3SJames Chapman 		else
1912f7faffa3SJames Chapman 			session->build_header = l2tp_build_l2tpv3_header;
1913f7faffa3SJames Chapman 
1914f7faffa3SJames Chapman 		l2tp_session_set_header_len(session, tunnel->version);
1915f7faffa3SJames Chapman 
1916fd558d18SJames Chapman 		/* Bump the reference count. The session context is deleted
1917fd558d18SJames Chapman 		 * only when this drops to zero.
1918fd558d18SJames Chapman 		 */
1919fd558d18SJames Chapman 		l2tp_session_inc_refcount(session);
1920fd558d18SJames Chapman 		l2tp_tunnel_inc_refcount(tunnel);
1921fd558d18SJames Chapman 
1922fd558d18SJames Chapman 		/* Ensure tunnel socket isn't deleted */
1923fd558d18SJames Chapman 		sock_hold(tunnel->sock);
1924fd558d18SJames Chapman 
1925fd558d18SJames Chapman 		/* Add session to the tunnel's hash list */
1926fd558d18SJames Chapman 		write_lock_bh(&tunnel->hlist_lock);
1927fd558d18SJames Chapman 		hlist_add_head(&session->hlist,
1928fd558d18SJames Chapman 			       l2tp_session_id_hash(tunnel, session_id));
1929fd558d18SJames Chapman 		write_unlock_bh(&tunnel->hlist_lock);
1930fd558d18SJames Chapman 
1931f7faffa3SJames Chapman 		/* And to the global session list if L2TPv3 */
1932f7faffa3SJames Chapman 		if (tunnel->version != L2TP_HDR_VER_2) {
1933f7faffa3SJames Chapman 			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1934f7faffa3SJames Chapman 
1935e02d494dSJames Chapman 			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1936e02d494dSJames Chapman 			hlist_add_head_rcu(&session->global_hlist,
1937f7faffa3SJames Chapman 					   l2tp_session_id_hash_2(pn, session_id));
1938e02d494dSJames Chapman 			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1939f7faffa3SJames Chapman 		}
1940f7faffa3SJames Chapman 
1941fd558d18SJames Chapman 		/* Ignore management session in session count value */
1942fd558d18SJames Chapman 		if (session->session_id != 0)
1943fd558d18SJames Chapman 			atomic_inc(&l2tp_session_count);
1944fd558d18SJames Chapman 	}
1945fd558d18SJames Chapman 
1946fd558d18SJames Chapman 	return session;
1947fd558d18SJames Chapman }
1948fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create);
1949fd558d18SJames Chapman 
1950fd558d18SJames Chapman /*****************************************************************************
1951fd558d18SJames Chapman  * Init and cleanup
1952fd558d18SJames Chapman  *****************************************************************************/
1953fd558d18SJames Chapman 
1954fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net)
1955fd558d18SJames Chapman {
1956e773aaffSJiri Pirko 	struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1957f7faffa3SJames Chapman 	int hash;
1958fd558d18SJames Chapman 
1959fd558d18SJames Chapman 	INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1960e02d494dSJames Chapman 	spin_lock_init(&pn->l2tp_tunnel_list_lock);
1961fd558d18SJames Chapman 
1962f7faffa3SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1963f7faffa3SJames Chapman 		INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1964f7faffa3SJames Chapman 
1965e02d494dSJames Chapman 	spin_lock_init(&pn->l2tp_session_hlist_lock);
1966f7faffa3SJames Chapman 
1967fd558d18SJames Chapman 	return 0;
1968fd558d18SJames Chapman }
1969fd558d18SJames Chapman 
1970167eb17eSTom Parkin static __net_exit void l2tp_exit_net(struct net *net)
1971167eb17eSTom Parkin {
1972167eb17eSTom Parkin 	struct l2tp_net *pn = l2tp_pernet(net);
1973167eb17eSTom Parkin 	struct l2tp_tunnel *tunnel = NULL;
1974167eb17eSTom Parkin 
1975167eb17eSTom Parkin 	rcu_read_lock_bh();
1976167eb17eSTom Parkin 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1977167eb17eSTom Parkin 		(void)l2tp_tunnel_delete(tunnel);
1978167eb17eSTom Parkin 	}
1979167eb17eSTom Parkin 	rcu_read_unlock_bh();
1980167eb17eSTom Parkin }
1981167eb17eSTom Parkin 
1982fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = {
1983fd558d18SJames Chapman 	.init = l2tp_init_net,
1984167eb17eSTom Parkin 	.exit = l2tp_exit_net,
1985fd558d18SJames Chapman 	.id   = &l2tp_net_id,
1986fd558d18SJames Chapman 	.size = sizeof(struct l2tp_net),
1987fd558d18SJames Chapman };
1988fd558d18SJames Chapman 
1989fd558d18SJames Chapman static int __init l2tp_init(void)
1990fd558d18SJames Chapman {
1991fd558d18SJames Chapman 	int rc = 0;
1992fd558d18SJames Chapman 
1993fd558d18SJames Chapman 	rc = register_pernet_device(&l2tp_net_ops);
1994fd558d18SJames Chapman 	if (rc)
1995fd558d18SJames Chapman 		goto out;
1996fd558d18SJames Chapman 
199759ff3eb6SZhangZhen 	l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1998f8ccac0eSTom Parkin 	if (!l2tp_wq) {
1999f8ccac0eSTom Parkin 		pr_err("alloc_workqueue failed\n");
2000f8ccac0eSTom Parkin 		rc = -ENOMEM;
2001f8ccac0eSTom Parkin 		goto out;
2002f8ccac0eSTom Parkin 	}
2003f8ccac0eSTom Parkin 
2004a4ca44faSJoe Perches 	pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
2005fd558d18SJames Chapman 
2006fd558d18SJames Chapman out:
2007fd558d18SJames Chapman 	return rc;
2008fd558d18SJames Chapman }
2009fd558d18SJames Chapman 
2010fd558d18SJames Chapman static void __exit l2tp_exit(void)
2011fd558d18SJames Chapman {
2012fd558d18SJames Chapman 	unregister_pernet_device(&l2tp_net_ops);
2013f8ccac0eSTom Parkin 	if (l2tp_wq) {
2014f8ccac0eSTom Parkin 		destroy_workqueue(l2tp_wq);
2015f8ccac0eSTom Parkin 		l2tp_wq = NULL;
2016f8ccac0eSTom Parkin 	}
2017fd558d18SJames Chapman }
2018fd558d18SJames Chapman 
2019fd558d18SJames Chapman module_init(l2tp_init);
2020fd558d18SJames Chapman module_exit(l2tp_exit);
2021fd558d18SJames Chapman 
2022fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
2023fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core");
2024fd558d18SJames Chapman MODULE_LICENSE("GPL");
2025fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION);
2026fd558d18SJames Chapman 
2027