xref: /openbmc/linux/net/l2tp/l2tp_core.c (revision d2cf3361)
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 
21fd558d18SJames Chapman #include <linux/module.h>
22fd558d18SJames Chapman #include <linux/string.h>
23fd558d18SJames Chapman #include <linux/list.h>
24e02d494dSJames Chapman #include <linux/rculist.h>
25fd558d18SJames Chapman #include <linux/uaccess.h>
26fd558d18SJames Chapman 
27fd558d18SJames Chapman #include <linux/kernel.h>
28fd558d18SJames Chapman #include <linux/spinlock.h>
29fd558d18SJames Chapman #include <linux/kthread.h>
30fd558d18SJames Chapman #include <linux/sched.h>
31fd558d18SJames Chapman #include <linux/slab.h>
32fd558d18SJames Chapman #include <linux/errno.h>
33fd558d18SJames Chapman #include <linux/jiffies.h>
34fd558d18SJames Chapman 
35fd558d18SJames Chapman #include <linux/netdevice.h>
36fd558d18SJames Chapman #include <linux/net.h>
37fd558d18SJames Chapman #include <linux/inetdevice.h>
38fd558d18SJames Chapman #include <linux/skbuff.h>
39fd558d18SJames Chapman #include <linux/init.h>
400d76751fSJames Chapman #include <linux/in.h>
41fd558d18SJames Chapman #include <linux/ip.h>
42fd558d18SJames Chapman #include <linux/udp.h>
430d76751fSJames Chapman #include <linux/l2tp.h>
44fd558d18SJames Chapman #include <linux/hash.h>
45fd558d18SJames Chapman #include <linux/sort.h>
46fd558d18SJames Chapman #include <linux/file.h>
47fd558d18SJames Chapman #include <linux/nsproxy.h>
48fd558d18SJames Chapman #include <net/net_namespace.h>
49fd558d18SJames Chapman #include <net/netns/generic.h>
50fd558d18SJames Chapman #include <net/dst.h>
51fd558d18SJames Chapman #include <net/ip.h>
52fd558d18SJames Chapman #include <net/udp.h>
53309795f4SJames Chapman #include <net/inet_common.h>
54fd558d18SJames Chapman #include <net/xfrm.h>
550d76751fSJames Chapman #include <net/protocol.h>
56d2cf3361SBenjamin LaHaise #include <net/inet6_connection_sock.h>
57d2cf3361SBenjamin LaHaise #include <net/inet_ecn.h>
58d2cf3361SBenjamin LaHaise #include <net/ip6_route.h>
59fd558d18SJames Chapman 
60fd558d18SJames Chapman #include <asm/byteorder.h>
6160063497SArun Sharma #include <linux/atomic.h>
62fd558d18SJames Chapman 
63fd558d18SJames Chapman #include "l2tp_core.h"
64fd558d18SJames Chapman 
65fd558d18SJames Chapman #define L2TP_DRV_VERSION	"V2.0"
66fd558d18SJames Chapman 
67fd558d18SJames Chapman /* L2TP header constants */
68fd558d18SJames Chapman #define L2TP_HDRFLAG_T	   0x8000
69fd558d18SJames Chapman #define L2TP_HDRFLAG_L	   0x4000
70fd558d18SJames Chapman #define L2TP_HDRFLAG_S	   0x0800
71fd558d18SJames Chapman #define L2TP_HDRFLAG_O	   0x0200
72fd558d18SJames Chapman #define L2TP_HDRFLAG_P	   0x0100
73fd558d18SJames Chapman 
74fd558d18SJames Chapman #define L2TP_HDR_VER_MASK  0x000F
75fd558d18SJames Chapman #define L2TP_HDR_VER_2	   0x0002
76f7faffa3SJames Chapman #define L2TP_HDR_VER_3	   0x0003
77fd558d18SJames Chapman 
78fd558d18SJames Chapman /* L2TPv3 default L2-specific sublayer */
79fd558d18SJames Chapman #define L2TP_SLFLAG_S	   0x40000000
80fd558d18SJames Chapman #define L2TP_SL_SEQ_MASK   0x00ffffff
81fd558d18SJames Chapman 
82fd558d18SJames Chapman #define L2TP_HDR_SIZE_SEQ		10
83fd558d18SJames Chapman #define L2TP_HDR_SIZE_NOSEQ		6
84fd558d18SJames Chapman 
85fd558d18SJames Chapman /* Default trace flags */
86fd558d18SJames Chapman #define L2TP_DEFAULT_DEBUG_FLAGS	0
87fd558d18SJames Chapman 
88fd558d18SJames Chapman #define PRINTK(_mask, _type, _lvl, _fmt, args...)			\
89fd558d18SJames Chapman 	do {								\
90fd558d18SJames Chapman 		if ((_mask) & (_type))					\
91fd558d18SJames Chapman 			printk(_lvl "L2TP: " _fmt, ##args);		\
92fd558d18SJames Chapman 	} while (0)
93fd558d18SJames Chapman 
94fd558d18SJames Chapman /* Private data stored for received packets in the skb.
95fd558d18SJames Chapman  */
96fd558d18SJames Chapman struct l2tp_skb_cb {
97f7faffa3SJames Chapman 	u32			ns;
98fd558d18SJames Chapman 	u16			has_seq;
99fd558d18SJames Chapman 	u16			length;
100fd558d18SJames Chapman 	unsigned long		expires;
101fd558d18SJames Chapman };
102fd558d18SJames Chapman 
103fd558d18SJames Chapman #define L2TP_SKB_CB(skb)	((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
104fd558d18SJames Chapman 
105fd558d18SJames Chapman static atomic_t l2tp_tunnel_count;
106fd558d18SJames Chapman static atomic_t l2tp_session_count;
107fd558d18SJames Chapman 
108fd558d18SJames Chapman /* per-net private data for this module */
109fd558d18SJames Chapman static unsigned int l2tp_net_id;
110fd558d18SJames Chapman struct l2tp_net {
111fd558d18SJames Chapman 	struct list_head l2tp_tunnel_list;
112e02d494dSJames Chapman 	spinlock_t l2tp_tunnel_list_lock;
113f7faffa3SJames Chapman 	struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
114e02d494dSJames Chapman 	spinlock_t l2tp_session_hlist_lock;
115fd558d18SJames Chapman };
116fd558d18SJames Chapman 
117fc130840Sstephen hemminger static void l2tp_session_set_header_len(struct l2tp_session *session, int version);
118fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
119fc130840Sstephen hemminger static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
120fc130840Sstephen hemminger 
121fd558d18SJames Chapman static inline struct l2tp_net *l2tp_pernet(struct net *net)
122fd558d18SJames Chapman {
123fd558d18SJames Chapman 	BUG_ON(!net);
124fd558d18SJames Chapman 
125fd558d18SJames Chapman 	return net_generic(net, l2tp_net_id);
126fd558d18SJames Chapman }
127fd558d18SJames Chapman 
128fc130840Sstephen hemminger 
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
143fc130840Sstephen hemminger #define l2tp_tunnel_inc_refcount(_t) do { \
144fc130840Sstephen hemminger 		printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
145fc130840Sstephen hemminger 		l2tp_tunnel_inc_refcount_1(_t);				\
146fc130840Sstephen hemminger 	} while (0)
147fc130840Sstephen hemminger #define l2tp_tunnel_dec_refcount(_t) do { \
148fc130840Sstephen hemminger 		printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
149fc130840Sstephen hemminger 		l2tp_tunnel_dec_refcount_1(_t);				\
150fc130840Sstephen hemminger 	} while (0)
151fc130840Sstephen hemminger #else
152fc130840Sstephen hemminger #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
153fc130840Sstephen hemminger #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
154fc130840Sstephen hemminger #endif
155fc130840Sstephen hemminger 
156f7faffa3SJames Chapman /* Session hash global list for L2TPv3.
157f7faffa3SJames Chapman  * The session_id SHOULD be random according to RFC3931, but several
158f7faffa3SJames Chapman  * L2TP implementations use incrementing session_ids.  So we do a real
159f7faffa3SJames Chapman  * hash on the session_id, rather than a simple bitmask.
160f7faffa3SJames Chapman  */
161f7faffa3SJames Chapman static inline struct hlist_head *
162f7faffa3SJames Chapman l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
163f7faffa3SJames Chapman {
164f7faffa3SJames Chapman 	return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
165f7faffa3SJames Chapman 
166f7faffa3SJames Chapman }
167f7faffa3SJames Chapman 
168f7faffa3SJames Chapman /* Lookup a session by id in the global session list
169f7faffa3SJames Chapman  */
170f7faffa3SJames Chapman static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
171f7faffa3SJames Chapman {
172f7faffa3SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
173f7faffa3SJames Chapman 	struct hlist_head *session_list =
174f7faffa3SJames Chapman 		l2tp_session_id_hash_2(pn, session_id);
175f7faffa3SJames Chapman 	struct l2tp_session *session;
176f7faffa3SJames Chapman 	struct hlist_node *walk;
177f7faffa3SJames Chapman 
178e02d494dSJames Chapman 	rcu_read_lock_bh();
179e02d494dSJames Chapman 	hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) {
180f7faffa3SJames Chapman 		if (session->session_id == session_id) {
181e02d494dSJames Chapman 			rcu_read_unlock_bh();
182f7faffa3SJames Chapman 			return session;
183f7faffa3SJames Chapman 		}
184f7faffa3SJames Chapman 	}
185e02d494dSJames Chapman 	rcu_read_unlock_bh();
186f7faffa3SJames Chapman 
187f7faffa3SJames Chapman 	return NULL;
188f7faffa3SJames Chapman }
189f7faffa3SJames Chapman 
190fd558d18SJames Chapman /* Session hash list.
191fd558d18SJames Chapman  * The session_id SHOULD be random according to RFC2661, but several
192fd558d18SJames Chapman  * L2TP implementations (Cisco and Microsoft) use incrementing
193fd558d18SJames Chapman  * session_ids.  So we do a real hash on the session_id, rather than a
194fd558d18SJames Chapman  * simple bitmask.
195fd558d18SJames Chapman  */
196fd558d18SJames Chapman static inline struct hlist_head *
197fd558d18SJames Chapman l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
198fd558d18SJames Chapman {
199fd558d18SJames Chapman 	return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
200fd558d18SJames Chapman }
201fd558d18SJames Chapman 
202fd558d18SJames Chapman /* Lookup a session by id
203fd558d18SJames Chapman  */
204f7faffa3SJames Chapman struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
205fd558d18SJames Chapman {
206f7faffa3SJames Chapman 	struct hlist_head *session_list;
207fd558d18SJames Chapman 	struct l2tp_session *session;
208fd558d18SJames Chapman 	struct hlist_node *walk;
209fd558d18SJames Chapman 
210f7faffa3SJames Chapman 	/* In L2TPv3, session_ids are unique over all tunnels and we
211f7faffa3SJames Chapman 	 * sometimes need to look them up before we know the
212f7faffa3SJames Chapman 	 * tunnel.
213f7faffa3SJames Chapman 	 */
214f7faffa3SJames Chapman 	if (tunnel == NULL)
215f7faffa3SJames Chapman 		return l2tp_session_find_2(net, session_id);
216f7faffa3SJames Chapman 
217f7faffa3SJames Chapman 	session_list = l2tp_session_id_hash(tunnel, session_id);
218fd558d18SJames Chapman 	read_lock_bh(&tunnel->hlist_lock);
219fd558d18SJames Chapman 	hlist_for_each_entry(session, walk, session_list, hlist) {
220fd558d18SJames Chapman 		if (session->session_id == session_id) {
221fd558d18SJames Chapman 			read_unlock_bh(&tunnel->hlist_lock);
222fd558d18SJames Chapman 			return session;
223fd558d18SJames Chapman 		}
224fd558d18SJames Chapman 	}
225fd558d18SJames Chapman 	read_unlock_bh(&tunnel->hlist_lock);
226fd558d18SJames Chapman 
227fd558d18SJames Chapman 	return NULL;
228fd558d18SJames Chapman }
229fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find);
230fd558d18SJames Chapman 
231fd558d18SJames Chapman struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
232fd558d18SJames Chapman {
233fd558d18SJames Chapman 	int hash;
234fd558d18SJames Chapman 	struct hlist_node *walk;
235fd558d18SJames Chapman 	struct l2tp_session *session;
236fd558d18SJames Chapman 	int count = 0;
237fd558d18SJames Chapman 
238fd558d18SJames Chapman 	read_lock_bh(&tunnel->hlist_lock);
239fd558d18SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
240fd558d18SJames Chapman 		hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
241fd558d18SJames Chapman 			if (++count > nth) {
242fd558d18SJames Chapman 				read_unlock_bh(&tunnel->hlist_lock);
243fd558d18SJames Chapman 				return session;
244fd558d18SJames Chapman 			}
245fd558d18SJames Chapman 		}
246fd558d18SJames Chapman 	}
247fd558d18SJames Chapman 
248fd558d18SJames Chapman 	read_unlock_bh(&tunnel->hlist_lock);
249fd558d18SJames Chapman 
250fd558d18SJames Chapman 	return NULL;
251fd558d18SJames Chapman }
252fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
253fd558d18SJames Chapman 
254309795f4SJames Chapman /* Lookup a session by interface name.
255309795f4SJames Chapman  * This is very inefficient but is only used by management interfaces.
256309795f4SJames Chapman  */
257309795f4SJames Chapman struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
258309795f4SJames Chapman {
259309795f4SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
260309795f4SJames Chapman 	int hash;
261309795f4SJames Chapman 	struct hlist_node *walk;
262309795f4SJames Chapman 	struct l2tp_session *session;
263309795f4SJames Chapman 
264e02d494dSJames Chapman 	rcu_read_lock_bh();
265309795f4SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
266e02d494dSJames Chapman 		hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) {
267309795f4SJames Chapman 			if (!strcmp(session->ifname, ifname)) {
268e02d494dSJames Chapman 				rcu_read_unlock_bh();
269309795f4SJames Chapman 				return session;
270309795f4SJames Chapman 			}
271309795f4SJames Chapman 		}
272309795f4SJames Chapman 	}
273309795f4SJames Chapman 
274e02d494dSJames Chapman 	rcu_read_unlock_bh();
275309795f4SJames Chapman 
276309795f4SJames Chapman 	return NULL;
277309795f4SJames Chapman }
278309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
279309795f4SJames Chapman 
280fd558d18SJames Chapman /* Lookup a tunnel by id
281fd558d18SJames Chapman  */
282fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
283fd558d18SJames Chapman {
284fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
285fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
286fd558d18SJames Chapman 
287e02d494dSJames Chapman 	rcu_read_lock_bh();
288e02d494dSJames Chapman 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
289fd558d18SJames Chapman 		if (tunnel->tunnel_id == tunnel_id) {
290e02d494dSJames Chapman 			rcu_read_unlock_bh();
291fd558d18SJames Chapman 			return tunnel;
292fd558d18SJames Chapman 		}
293fd558d18SJames Chapman 	}
294e02d494dSJames Chapman 	rcu_read_unlock_bh();
295fd558d18SJames Chapman 
296fd558d18SJames Chapman 	return NULL;
297fd558d18SJames Chapman }
298fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
299fd558d18SJames Chapman 
300fd558d18SJames Chapman struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
301fd558d18SJames Chapman {
302fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(net);
303fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
304fd558d18SJames Chapman 	int count = 0;
305fd558d18SJames Chapman 
306e02d494dSJames Chapman 	rcu_read_lock_bh();
307e02d494dSJames Chapman 	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
308fd558d18SJames Chapman 		if (++count > nth) {
309e02d494dSJames Chapman 			rcu_read_unlock_bh();
310fd558d18SJames Chapman 			return tunnel;
311fd558d18SJames Chapman 		}
312fd558d18SJames Chapman 	}
313fd558d18SJames Chapman 
314e02d494dSJames Chapman 	rcu_read_unlock_bh();
315fd558d18SJames Chapman 
316fd558d18SJames Chapman 	return NULL;
317fd558d18SJames Chapman }
318fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
319fd558d18SJames Chapman 
320fd558d18SJames Chapman /*****************************************************************************
321fd558d18SJames Chapman  * Receive data handling
322fd558d18SJames Chapman  *****************************************************************************/
323fd558d18SJames Chapman 
324fd558d18SJames Chapman /* Queue a skb in order. We come here only if the skb has an L2TP sequence
325fd558d18SJames Chapman  * number.
326fd558d18SJames Chapman  */
327fd558d18SJames Chapman static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
328fd558d18SJames Chapman {
329fd558d18SJames Chapman 	struct sk_buff *skbp;
330fd558d18SJames Chapman 	struct sk_buff *tmp;
331f7faffa3SJames Chapman 	u32 ns = L2TP_SKB_CB(skb)->ns;
332fd558d18SJames Chapman 
333fd558d18SJames Chapman 	spin_lock_bh(&session->reorder_q.lock);
334fd558d18SJames Chapman 	skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
335fd558d18SJames Chapman 		if (L2TP_SKB_CB(skbp)->ns > ns) {
336fd558d18SJames Chapman 			__skb_queue_before(&session->reorder_q, skbp, skb);
337fd558d18SJames Chapman 			PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
338fd558d18SJames Chapman 			       "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
339fd558d18SJames Chapman 			       session->name, ns, L2TP_SKB_CB(skbp)->ns,
340fd558d18SJames Chapman 			       skb_queue_len(&session->reorder_q));
341fd558d18SJames Chapman 			session->stats.rx_oos_packets++;
342fd558d18SJames Chapman 			goto out;
343fd558d18SJames Chapman 		}
344fd558d18SJames Chapman 	}
345fd558d18SJames Chapman 
346fd558d18SJames Chapman 	__skb_queue_tail(&session->reorder_q, skb);
347fd558d18SJames Chapman 
348fd558d18SJames Chapman out:
349fd558d18SJames Chapman 	spin_unlock_bh(&session->reorder_q.lock);
350fd558d18SJames Chapman }
351fd558d18SJames Chapman 
352fd558d18SJames Chapman /* Dequeue a single skb.
353fd558d18SJames Chapman  */
354fd558d18SJames Chapman static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
355fd558d18SJames Chapman {
356fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
357fd558d18SJames Chapman 	int length = L2TP_SKB_CB(skb)->length;
358fd558d18SJames Chapman 
359fd558d18SJames Chapman 	/* We're about to requeue the skb, so return resources
360fd558d18SJames Chapman 	 * to its current owner (a socket receive buffer).
361fd558d18SJames Chapman 	 */
362fd558d18SJames Chapman 	skb_orphan(skb);
363fd558d18SJames Chapman 
364fd558d18SJames Chapman 	tunnel->stats.rx_packets++;
365fd558d18SJames Chapman 	tunnel->stats.rx_bytes += length;
366fd558d18SJames Chapman 	session->stats.rx_packets++;
367fd558d18SJames Chapman 	session->stats.rx_bytes += length;
368fd558d18SJames Chapman 
369fd558d18SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
370fd558d18SJames Chapman 		/* Bump our Nr */
371fd558d18SJames Chapman 		session->nr++;
372f7faffa3SJames Chapman 		if (tunnel->version == L2TP_HDR_VER_2)
373f7faffa3SJames Chapman 			session->nr &= 0xffff;
374f7faffa3SJames Chapman 		else
375f7faffa3SJames Chapman 			session->nr &= 0xffffff;
376f7faffa3SJames Chapman 
377fd558d18SJames Chapman 		PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
378fd558d18SJames Chapman 		       "%s: updated nr to %hu\n", session->name, session->nr);
379fd558d18SJames Chapman 	}
380fd558d18SJames Chapman 
381fd558d18SJames Chapman 	/* call private receive handler */
382fd558d18SJames Chapman 	if (session->recv_skb != NULL)
383fd558d18SJames Chapman 		(*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
384fd558d18SJames Chapman 	else
385fd558d18SJames Chapman 		kfree_skb(skb);
386fd558d18SJames Chapman 
387fd558d18SJames Chapman 	if (session->deref)
388fd558d18SJames Chapman 		(*session->deref)(session);
389fd558d18SJames Chapman }
390fd558d18SJames Chapman 
391fd558d18SJames Chapman /* Dequeue skbs from the session's reorder_q, subject to packet order.
392fd558d18SJames Chapman  * Skbs that have been in the queue for too long are simply discarded.
393fd558d18SJames Chapman  */
394fd558d18SJames Chapman static void l2tp_recv_dequeue(struct l2tp_session *session)
395fd558d18SJames Chapman {
396fd558d18SJames Chapman 	struct sk_buff *skb;
397fd558d18SJames Chapman 	struct sk_buff *tmp;
398fd558d18SJames Chapman 
399fd558d18SJames Chapman 	/* If the pkt at the head of the queue has the nr that we
400fd558d18SJames Chapman 	 * expect to send up next, dequeue it and any other
401fd558d18SJames Chapman 	 * in-sequence packets behind it.
402fd558d18SJames Chapman 	 */
403e2e210c0SEric Dumazet start:
404fd558d18SJames Chapman 	spin_lock_bh(&session->reorder_q.lock);
405fd558d18SJames Chapman 	skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
406fd558d18SJames Chapman 		if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
407fd558d18SJames Chapman 			session->stats.rx_seq_discards++;
408fd558d18SJames Chapman 			session->stats.rx_errors++;
409fd558d18SJames Chapman 			PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
410f7faffa3SJames Chapman 			       "%s: oos pkt %u len %d discarded (too old), "
411f7faffa3SJames Chapman 			       "waiting for %u, reorder_q_len=%d\n",
412fd558d18SJames Chapman 			       session->name, L2TP_SKB_CB(skb)->ns,
413fd558d18SJames Chapman 			       L2TP_SKB_CB(skb)->length, session->nr,
414fd558d18SJames Chapman 			       skb_queue_len(&session->reorder_q));
415fd558d18SJames Chapman 			__skb_unlink(skb, &session->reorder_q);
416fd558d18SJames Chapman 			kfree_skb(skb);
417fd558d18SJames Chapman 			if (session->deref)
418fd558d18SJames Chapman 				(*session->deref)(session);
419fd558d18SJames Chapman 			continue;
420fd558d18SJames Chapman 		}
421fd558d18SJames Chapman 
422fd558d18SJames Chapman 		if (L2TP_SKB_CB(skb)->has_seq) {
423fd558d18SJames Chapman 			if (L2TP_SKB_CB(skb)->ns != session->nr) {
424fd558d18SJames Chapman 				PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
425f7faffa3SJames Chapman 				       "%s: holding oos pkt %u len %d, "
426f7faffa3SJames Chapman 				       "waiting for %u, reorder_q_len=%d\n",
427fd558d18SJames Chapman 				       session->name, L2TP_SKB_CB(skb)->ns,
428fd558d18SJames Chapman 				       L2TP_SKB_CB(skb)->length, session->nr,
429fd558d18SJames Chapman 				       skb_queue_len(&session->reorder_q));
430fd558d18SJames Chapman 				goto out;
431fd558d18SJames Chapman 			}
432fd558d18SJames Chapman 		}
433fd558d18SJames Chapman 		__skb_unlink(skb, &session->reorder_q);
434fd558d18SJames Chapman 
435fd558d18SJames Chapman 		/* Process the skb. We release the queue lock while we
436fd558d18SJames Chapman 		 * do so to let other contexts process the queue.
437fd558d18SJames Chapman 		 */
438fd558d18SJames Chapman 		spin_unlock_bh(&session->reorder_q.lock);
439fd558d18SJames Chapman 		l2tp_recv_dequeue_skb(session, skb);
440e2e210c0SEric Dumazet 		goto start;
441fd558d18SJames Chapman 	}
442fd558d18SJames Chapman 
443fd558d18SJames Chapman out:
444fd558d18SJames Chapman 	spin_unlock_bh(&session->reorder_q.lock);
445fd558d18SJames Chapman }
446fd558d18SJames Chapman 
447fd558d18SJames Chapman static inline int l2tp_verify_udp_checksum(struct sock *sk,
448fd558d18SJames Chapman 					   struct sk_buff *skb)
449fd558d18SJames Chapman {
450fd558d18SJames Chapman 	struct udphdr *uh = udp_hdr(skb);
451fd558d18SJames Chapman 	u16 ulen = ntohs(uh->len);
452fd558d18SJames Chapman 	__wsum psum;
453fd558d18SJames Chapman 
454d2cf3361SBenjamin LaHaise 	if (sk->sk_no_check || skb_csum_unnecessary(skb))
455fd558d18SJames Chapman 		return 0;
456fd558d18SJames Chapman 
457d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
458d2cf3361SBenjamin LaHaise 	if (sk->sk_family == PF_INET6) {
459d2cf3361SBenjamin LaHaise 		if (!uh->check) {
460d2cf3361SBenjamin LaHaise 			LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
461d2cf3361SBenjamin LaHaise 			return 1;
462d2cf3361SBenjamin LaHaise 		}
463d2cf3361SBenjamin LaHaise 		if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
464d2cf3361SBenjamin LaHaise 		    !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
465d2cf3361SBenjamin LaHaise 				     &ipv6_hdr(skb)->daddr, ulen,
466d2cf3361SBenjamin LaHaise 				     IPPROTO_UDP, skb->csum)) {
467d2cf3361SBenjamin LaHaise 			skb->ip_summed = CHECKSUM_UNNECESSARY;
468d2cf3361SBenjamin LaHaise 			return 0;
469d2cf3361SBenjamin LaHaise 		}
470d2cf3361SBenjamin LaHaise 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
471d2cf3361SBenjamin LaHaise 							 &ipv6_hdr(skb)->daddr,
472d2cf3361SBenjamin LaHaise 							 skb->len, IPPROTO_UDP,
473d2cf3361SBenjamin LaHaise 							 0));
474d2cf3361SBenjamin LaHaise 	} else
475d2cf3361SBenjamin LaHaise #endif
476d2cf3361SBenjamin LaHaise 	{
477d2cf3361SBenjamin LaHaise 		struct inet_sock *inet;
478d2cf3361SBenjamin LaHaise 		if (!uh->check)
479d2cf3361SBenjamin LaHaise 			return 0;
480fd558d18SJames Chapman 		inet = inet_sk(sk);
481d2cf3361SBenjamin LaHaise 		psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
482d2cf3361SBenjamin LaHaise 					  ulen, IPPROTO_UDP, 0);
483fd558d18SJames Chapman 
484fd558d18SJames Chapman 		if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
485fd558d18SJames Chapman 		    !csum_fold(csum_add(psum, skb->csum)))
486fd558d18SJames Chapman 			return 0;
487fd558d18SJames Chapman 		skb->csum = psum;
488d2cf3361SBenjamin LaHaise 	}
489fd558d18SJames Chapman 
490fd558d18SJames Chapman 	return __skb_checksum_complete(skb);
491fd558d18SJames Chapman }
492fd558d18SJames Chapman 
493f7faffa3SJames Chapman /* Do receive processing of L2TP data frames. We handle both L2TPv2
494f7faffa3SJames Chapman  * and L2TPv3 data frames here.
495f7faffa3SJames Chapman  *
496f7faffa3SJames Chapman  * L2TPv2 Data Message Header
497f7faffa3SJames Chapman  *
498f7faffa3SJames Chapman  *  0                   1                   2                   3
499f7faffa3SJames 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
500f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
501f7faffa3SJames Chapman  * |T|L|x|x|S|x|O|P|x|x|x|x|  Ver  |          Length (opt)         |
502f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
503f7faffa3SJames Chapman  * |           Tunnel ID           |           Session ID          |
504f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
505f7faffa3SJames Chapman  * |             Ns (opt)          |             Nr (opt)          |
506f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
507f7faffa3SJames Chapman  * |      Offset Size (opt)        |    Offset pad... (opt)
508f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
509f7faffa3SJames Chapman  *
510f7faffa3SJames Chapman  * Data frames are marked by T=0. All other fields are the same as
511f7faffa3SJames Chapman  * those in L2TP control frames.
512f7faffa3SJames Chapman  *
513f7faffa3SJames Chapman  * L2TPv3 Data Message Header
514f7faffa3SJames Chapman  *
515f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
516f7faffa3SJames Chapman  * |                      L2TP Session Header                      |
517f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
518f7faffa3SJames Chapman  * |                      L2-Specific Sublayer                     |
519f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
520f7faffa3SJames Chapman  * |                        Tunnel Payload                      ...
521f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
522f7faffa3SJames Chapman  *
523f7faffa3SJames Chapman  * L2TPv3 Session Header Over IP
524f7faffa3SJames Chapman  *
525f7faffa3SJames Chapman  *  0                   1                   2                   3
526f7faffa3SJames 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
527f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
528f7faffa3SJames Chapman  * |                           Session ID                          |
529f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
530f7faffa3SJames Chapman  * |               Cookie (optional, maximum 64 bits)...
531f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
532f7faffa3SJames Chapman  *                                                                 |
533f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534f7faffa3SJames Chapman  *
535f7faffa3SJames Chapman  * L2TPv3 L2-Specific Sublayer Format
536f7faffa3SJames Chapman  *
537f7faffa3SJames Chapman  *  0                   1                   2                   3
538f7faffa3SJames 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
539f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
540f7faffa3SJames Chapman  * |x|S|x|x|x|x|x|x|              Sequence Number                  |
541f7faffa3SJames Chapman  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
542f7faffa3SJames Chapman  *
543f7faffa3SJames Chapman  * Cookie value, sublayer format and offset (pad) are negotiated with
544f7faffa3SJames Chapman  * the peer when the session is set up. Unlike L2TPv2, we do not need
545f7faffa3SJames Chapman  * to parse the packet header to determine if optional fields are
546f7faffa3SJames Chapman  * present.
547f7faffa3SJames Chapman  *
548f7faffa3SJames Chapman  * Caller must already have parsed the frame and determined that it is
549f7faffa3SJames Chapman  * a data (not control) frame before coming here. Fields up to the
550f7faffa3SJames Chapman  * session-id have already been parsed and ptr points to the data
551f7faffa3SJames Chapman  * after the session-id.
552f7faffa3SJames Chapman  */
553f7faffa3SJames Chapman void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
554f7faffa3SJames Chapman 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
555f7faffa3SJames Chapman 		      int length, int (*payload_hook)(struct sk_buff *skb))
556f7faffa3SJames Chapman {
557f7faffa3SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
558f7faffa3SJames Chapman 	int offset;
559f7faffa3SJames Chapman 	u32 ns, nr;
560f7faffa3SJames Chapman 
561f7faffa3SJames Chapman 	/* The ref count is increased since we now hold a pointer to
562f7faffa3SJames Chapman 	 * the session. Take care to decrement the refcnt when exiting
563f7faffa3SJames Chapman 	 * this function from now on...
564f7faffa3SJames Chapman 	 */
565f7faffa3SJames Chapman 	l2tp_session_inc_refcount(session);
566f7faffa3SJames Chapman 	if (session->ref)
567f7faffa3SJames Chapman 		(*session->ref)(session);
568f7faffa3SJames Chapman 
569f7faffa3SJames Chapman 	/* Parse and check optional cookie */
570f7faffa3SJames Chapman 	if (session->peer_cookie_len > 0) {
571f7faffa3SJames Chapman 		if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
572f7faffa3SJames Chapman 			PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
573f7faffa3SJames Chapman 			       "%s: cookie mismatch (%u/%u). Discarding.\n",
574f7faffa3SJames Chapman 			       tunnel->name, tunnel->tunnel_id, session->session_id);
575f7faffa3SJames Chapman 			session->stats.rx_cookie_discards++;
576f7faffa3SJames Chapman 			goto discard;
577f7faffa3SJames Chapman 		}
578f7faffa3SJames Chapman 		ptr += session->peer_cookie_len;
579f7faffa3SJames Chapman 	}
580f7faffa3SJames Chapman 
581f7faffa3SJames Chapman 	/* Handle the optional sequence numbers. Sequence numbers are
582f7faffa3SJames Chapman 	 * in different places for L2TPv2 and L2TPv3.
583f7faffa3SJames Chapman 	 *
584f7faffa3SJames Chapman 	 * If we are the LAC, enable/disable sequence numbers under
585f7faffa3SJames Chapman 	 * the control of the LNS.  If no sequence numbers present but
586f7faffa3SJames Chapman 	 * we were expecting them, discard frame.
587f7faffa3SJames Chapman 	 */
588f7faffa3SJames Chapman 	ns = nr = 0;
589f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->has_seq = 0;
590f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
591f7faffa3SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_S) {
592f7faffa3SJames Chapman 			ns = ntohs(*(__be16 *) ptr);
593f7faffa3SJames Chapman 			ptr += 2;
594f7faffa3SJames Chapman 			nr = ntohs(*(__be16 *) ptr);
595f7faffa3SJames Chapman 			ptr += 2;
596f7faffa3SJames Chapman 
597f7faffa3SJames Chapman 			/* Store L2TP info in the skb */
598f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->ns = ns;
599f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->has_seq = 1;
600f7faffa3SJames Chapman 
601f7faffa3SJames Chapman 			PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
602f7faffa3SJames Chapman 			       "%s: recv data ns=%u, nr=%u, session nr=%u\n",
603f7faffa3SJames Chapman 			       session->name, ns, nr, session->nr);
604f7faffa3SJames Chapman 		}
605f7faffa3SJames Chapman 	} else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
606f7faffa3SJames Chapman 		u32 l2h = ntohl(*(__be32 *) ptr);
607f7faffa3SJames Chapman 
608f7faffa3SJames Chapman 		if (l2h & 0x40000000) {
609f7faffa3SJames Chapman 			ns = l2h & 0x00ffffff;
610f7faffa3SJames Chapman 
611f7faffa3SJames Chapman 			/* Store L2TP info in the skb */
612f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->ns = ns;
613f7faffa3SJames Chapman 			L2TP_SKB_CB(skb)->has_seq = 1;
614f7faffa3SJames Chapman 
615f7faffa3SJames Chapman 			PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
616f7faffa3SJames Chapman 			       "%s: recv data ns=%u, session nr=%u\n",
617f7faffa3SJames Chapman 			       session->name, ns, session->nr);
618f7faffa3SJames Chapman 		}
619f7faffa3SJames Chapman 	}
620f7faffa3SJames Chapman 
621f7faffa3SJames Chapman 	/* Advance past L2-specific header, if present */
622f7faffa3SJames Chapman 	ptr += session->l2specific_len;
623f7faffa3SJames Chapman 
624f7faffa3SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
625f7faffa3SJames Chapman 		/* Received a packet with sequence numbers. If we're the LNS,
626f7faffa3SJames Chapman 		 * check if we sre sending sequence numbers and if not,
627f7faffa3SJames Chapman 		 * configure it so.
628f7faffa3SJames Chapman 		 */
629f7faffa3SJames Chapman 		if ((!session->lns_mode) && (!session->send_seq)) {
630f7faffa3SJames Chapman 			PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
631f7faffa3SJames Chapman 			       "%s: requested to enable seq numbers by LNS\n",
632f7faffa3SJames Chapman 			       session->name);
633f7faffa3SJames Chapman 			session->send_seq = -1;
634f7faffa3SJames Chapman 			l2tp_session_set_header_len(session, tunnel->version);
635f7faffa3SJames Chapman 		}
636f7faffa3SJames Chapman 	} else {
637f7faffa3SJames Chapman 		/* No sequence numbers.
638f7faffa3SJames Chapman 		 * If user has configured mandatory sequence numbers, discard.
639f7faffa3SJames Chapman 		 */
640f7faffa3SJames Chapman 		if (session->recv_seq) {
641f7faffa3SJames Chapman 			PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
642f7faffa3SJames Chapman 			       "%s: recv data has no seq numbers when required. "
643f7faffa3SJames Chapman 			       "Discarding\n", session->name);
644f7faffa3SJames Chapman 			session->stats.rx_seq_discards++;
645f7faffa3SJames Chapman 			goto discard;
646f7faffa3SJames Chapman 		}
647f7faffa3SJames Chapman 
648f7faffa3SJames Chapman 		/* If we're the LAC and we're sending sequence numbers, the
649f7faffa3SJames Chapman 		 * LNS has requested that we no longer send sequence numbers.
650f7faffa3SJames Chapman 		 * If we're the LNS and we're sending sequence numbers, the
651f7faffa3SJames Chapman 		 * LAC is broken. Discard the frame.
652f7faffa3SJames Chapman 		 */
653f7faffa3SJames Chapman 		if ((!session->lns_mode) && (session->send_seq)) {
654f7faffa3SJames Chapman 			PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
655f7faffa3SJames Chapman 			       "%s: requested to disable seq numbers by LNS\n",
656f7faffa3SJames Chapman 			       session->name);
657f7faffa3SJames Chapman 			session->send_seq = 0;
658f7faffa3SJames Chapman 			l2tp_session_set_header_len(session, tunnel->version);
659f7faffa3SJames Chapman 		} else if (session->send_seq) {
660f7faffa3SJames Chapman 			PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
661f7faffa3SJames Chapman 			       "%s: recv data has no seq numbers when required. "
662f7faffa3SJames Chapman 			       "Discarding\n", session->name);
663f7faffa3SJames Chapman 			session->stats.rx_seq_discards++;
664f7faffa3SJames Chapman 			goto discard;
665f7faffa3SJames Chapman 		}
666f7faffa3SJames Chapman 	}
667f7faffa3SJames Chapman 
668f7faffa3SJames Chapman 	/* Session data offset is handled differently for L2TPv2 and
669f7faffa3SJames Chapman 	 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
670f7faffa3SJames Chapman 	 * the header. For L2TPv3, the offset is negotiated using AVPs
671f7faffa3SJames Chapman 	 * in the session setup control protocol.
672f7faffa3SJames Chapman 	 */
673f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
674f7faffa3SJames Chapman 		/* If offset bit set, skip it. */
675f7faffa3SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_O) {
676f7faffa3SJames Chapman 			offset = ntohs(*(__be16 *)ptr);
677f7faffa3SJames Chapman 			ptr += 2 + offset;
678f7faffa3SJames Chapman 		}
679f7faffa3SJames Chapman 	} else
680f7faffa3SJames Chapman 		ptr += session->offset;
681f7faffa3SJames Chapman 
682f7faffa3SJames Chapman 	offset = ptr - optr;
683f7faffa3SJames Chapman 	if (!pskb_may_pull(skb, offset))
684f7faffa3SJames Chapman 		goto discard;
685f7faffa3SJames Chapman 
686f7faffa3SJames Chapman 	__skb_pull(skb, offset);
687f7faffa3SJames Chapman 
688f7faffa3SJames Chapman 	/* If caller wants to process the payload before we queue the
689f7faffa3SJames Chapman 	 * packet, do so now.
690f7faffa3SJames Chapman 	 */
691f7faffa3SJames Chapman 	if (payload_hook)
692f7faffa3SJames Chapman 		if ((*payload_hook)(skb))
693f7faffa3SJames Chapman 			goto discard;
694f7faffa3SJames Chapman 
695f7faffa3SJames Chapman 	/* Prepare skb for adding to the session's reorder_q.  Hold
696f7faffa3SJames Chapman 	 * packets for max reorder_timeout or 1 second if not
697f7faffa3SJames Chapman 	 * reordering.
698f7faffa3SJames Chapman 	 */
699f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->length = length;
700f7faffa3SJames Chapman 	L2TP_SKB_CB(skb)->expires = jiffies +
701f7faffa3SJames Chapman 		(session->reorder_timeout ? session->reorder_timeout : HZ);
702f7faffa3SJames Chapman 
703f7faffa3SJames Chapman 	/* Add packet to the session's receive queue. Reordering is done here, if
704f7faffa3SJames Chapman 	 * enabled. Saved L2TP protocol info is stored in skb->sb[].
705f7faffa3SJames Chapman 	 */
706f7faffa3SJames Chapman 	if (L2TP_SKB_CB(skb)->has_seq) {
707f7faffa3SJames Chapman 		if (session->reorder_timeout != 0) {
708f7faffa3SJames Chapman 			/* Packet reordering enabled. Add skb to session's
709f7faffa3SJames Chapman 			 * reorder queue, in order of ns.
710f7faffa3SJames Chapman 			 */
711f7faffa3SJames Chapman 			l2tp_recv_queue_skb(session, skb);
712f7faffa3SJames Chapman 		} else {
713f7faffa3SJames Chapman 			/* Packet reordering disabled. Discard out-of-sequence
714f7faffa3SJames Chapman 			 * packets
715f7faffa3SJames Chapman 			 */
716f7faffa3SJames Chapman 			if (L2TP_SKB_CB(skb)->ns != session->nr) {
717f7faffa3SJames Chapman 				session->stats.rx_seq_discards++;
718f7faffa3SJames Chapman 				PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
719f7faffa3SJames Chapman 				       "%s: oos pkt %u len %d discarded, "
720f7faffa3SJames Chapman 				       "waiting for %u, reorder_q_len=%d\n",
721f7faffa3SJames Chapman 				       session->name, L2TP_SKB_CB(skb)->ns,
722f7faffa3SJames Chapman 				       L2TP_SKB_CB(skb)->length, session->nr,
723f7faffa3SJames Chapman 				       skb_queue_len(&session->reorder_q));
724f7faffa3SJames Chapman 				goto discard;
725f7faffa3SJames Chapman 			}
726f7faffa3SJames Chapman 			skb_queue_tail(&session->reorder_q, skb);
727f7faffa3SJames Chapman 		}
728f7faffa3SJames Chapman 	} else {
729f7faffa3SJames Chapman 		/* No sequence numbers. Add the skb to the tail of the
730f7faffa3SJames Chapman 		 * reorder queue. This ensures that it will be
731f7faffa3SJames Chapman 		 * delivered after all previous sequenced skbs.
732f7faffa3SJames Chapman 		 */
733f7faffa3SJames Chapman 		skb_queue_tail(&session->reorder_q, skb);
734f7faffa3SJames Chapman 	}
735f7faffa3SJames Chapman 
736f7faffa3SJames Chapman 	/* Try to dequeue as many skbs from reorder_q as we can. */
737f7faffa3SJames Chapman 	l2tp_recv_dequeue(session);
738f7faffa3SJames Chapman 
739f7faffa3SJames Chapman 	l2tp_session_dec_refcount(session);
740f7faffa3SJames Chapman 
741f7faffa3SJames Chapman 	return;
742f7faffa3SJames Chapman 
743f7faffa3SJames Chapman discard:
744f7faffa3SJames Chapman 	session->stats.rx_errors++;
745f7faffa3SJames Chapman 	kfree_skb(skb);
746f7faffa3SJames Chapman 
747f7faffa3SJames Chapman 	if (session->deref)
748f7faffa3SJames Chapman 		(*session->deref)(session);
749f7faffa3SJames Chapman 
750f7faffa3SJames Chapman 	l2tp_session_dec_refcount(session);
751f7faffa3SJames Chapman }
752f7faffa3SJames Chapman EXPORT_SYMBOL(l2tp_recv_common);
753f7faffa3SJames Chapman 
754fd558d18SJames Chapman /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
755fd558d18SJames Chapman  * here. The skb is not on a list when we get here.
756fd558d18SJames Chapman  * Returns 0 if the packet was a data packet and was successfully passed on.
757fd558d18SJames Chapman  * Returns 1 if the packet was not a good data packet and could not be
758fd558d18SJames Chapman  * forwarded.  All such packets are passed up to userspace to deal with.
759fd558d18SJames Chapman  */
760fc130840Sstephen hemminger static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
761fd558d18SJames Chapman 			      int (*payload_hook)(struct sk_buff *skb))
762fd558d18SJames Chapman {
763fd558d18SJames Chapman 	struct l2tp_session *session = NULL;
764fd558d18SJames Chapman 	unsigned char *ptr, *optr;
765fd558d18SJames Chapman 	u16 hdrflags;
766fd558d18SJames Chapman 	u32 tunnel_id, session_id;
767fd558d18SJames Chapman 	int offset;
768fd558d18SJames Chapman 	u16 version;
769f7faffa3SJames Chapman 	int length;
770fd558d18SJames Chapman 
771fd558d18SJames Chapman 	if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
772fd558d18SJames Chapman 		goto discard_bad_csum;
773fd558d18SJames Chapman 
774fd558d18SJames Chapman 	/* UDP always verifies the packet length. */
775fd558d18SJames Chapman 	__skb_pull(skb, sizeof(struct udphdr));
776fd558d18SJames Chapman 
777fd558d18SJames Chapman 	/* Short packet? */
778fd558d18SJames Chapman 	if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
779fd558d18SJames Chapman 		PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
780fd558d18SJames Chapman 		       "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
781fd558d18SJames Chapman 		goto error;
782fd558d18SJames Chapman 	}
783fd558d18SJames Chapman 
784fd558d18SJames Chapman 	/* Trace packet contents, if enabled */
785fd558d18SJames Chapman 	if (tunnel->debug & L2TP_MSG_DATA) {
786fd558d18SJames Chapman 		length = min(32u, skb->len);
787fd558d18SJames Chapman 		if (!pskb_may_pull(skb, length))
788fd558d18SJames Chapman 			goto error;
789fd558d18SJames Chapman 
790fd558d18SJames Chapman 		printk(KERN_DEBUG "%s: recv: ", tunnel->name);
791fd558d18SJames Chapman 
792fd558d18SJames Chapman 		offset = 0;
793fd558d18SJames Chapman 		do {
794e50e705cSEric Dumazet 			printk(" %02X", skb->data[offset]);
795fd558d18SJames Chapman 		} while (++offset < length);
796fd558d18SJames Chapman 
797fd558d18SJames Chapman 		printk("\n");
798fd558d18SJames Chapman 	}
799fd558d18SJames Chapman 
800e50e705cSEric Dumazet 	/* Point to L2TP header */
801e50e705cSEric Dumazet 	optr = ptr = skb->data;
802e50e705cSEric Dumazet 
803fd558d18SJames Chapman 	/* Get L2TP header flags */
804fd558d18SJames Chapman 	hdrflags = ntohs(*(__be16 *) ptr);
805fd558d18SJames Chapman 
806fd558d18SJames Chapman 	/* Check protocol version */
807fd558d18SJames Chapman 	version = hdrflags & L2TP_HDR_VER_MASK;
808fd558d18SJames Chapman 	if (version != tunnel->version) {
809fd558d18SJames Chapman 		PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
810fd558d18SJames Chapman 		       "%s: recv protocol version mismatch: got %d expected %d\n",
811fd558d18SJames Chapman 		       tunnel->name, version, tunnel->version);
812fd558d18SJames Chapman 		goto error;
813fd558d18SJames Chapman 	}
814fd558d18SJames Chapman 
815fd558d18SJames Chapman 	/* Get length of L2TP packet */
816fd558d18SJames Chapman 	length = skb->len;
817fd558d18SJames Chapman 
818fd558d18SJames Chapman 	/* If type is control packet, it is handled by userspace. */
819fd558d18SJames Chapman 	if (hdrflags & L2TP_HDRFLAG_T) {
820fd558d18SJames Chapman 		PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
821fd558d18SJames Chapman 		       "%s: recv control packet, len=%d\n", tunnel->name, length);
822fd558d18SJames Chapman 		goto error;
823fd558d18SJames Chapman 	}
824fd558d18SJames Chapman 
825fd558d18SJames Chapman 	/* Skip flags */
826fd558d18SJames Chapman 	ptr += 2;
827fd558d18SJames Chapman 
828f7faffa3SJames Chapman 	if (tunnel->version == L2TP_HDR_VER_2) {
829fd558d18SJames Chapman 		/* If length is present, skip it */
830fd558d18SJames Chapman 		if (hdrflags & L2TP_HDRFLAG_L)
831fd558d18SJames Chapman 			ptr += 2;
832fd558d18SJames Chapman 
833fd558d18SJames Chapman 		/* Extract tunnel and session ID */
834fd558d18SJames Chapman 		tunnel_id = ntohs(*(__be16 *) ptr);
835fd558d18SJames Chapman 		ptr += 2;
836fd558d18SJames Chapman 		session_id = ntohs(*(__be16 *) ptr);
837fd558d18SJames Chapman 		ptr += 2;
838f7faffa3SJames Chapman 	} else {
839f7faffa3SJames Chapman 		ptr += 2;	/* skip reserved bits */
840f7faffa3SJames Chapman 		tunnel_id = tunnel->tunnel_id;
841f7faffa3SJames Chapman 		session_id = ntohl(*(__be32 *) ptr);
842f7faffa3SJames Chapman 		ptr += 4;
843f7faffa3SJames Chapman 	}
844fd558d18SJames Chapman 
845fd558d18SJames Chapman 	/* Find the session context */
846f7faffa3SJames Chapman 	session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
847309795f4SJames Chapman 	if (!session || !session->recv_skb) {
848fd558d18SJames Chapman 		/* Not found? Pass to userspace to deal with */
849fd558d18SJames Chapman 		PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
850f7faffa3SJames Chapman 		       "%s: no session found (%u/%u). Passing up.\n",
851fd558d18SJames Chapman 		       tunnel->name, tunnel_id, session_id);
852fd558d18SJames Chapman 		goto error;
853fd558d18SJames Chapman 	}
854fd558d18SJames Chapman 
855f7faffa3SJames Chapman 	l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
856fd558d18SJames Chapman 
857fd558d18SJames Chapman 	return 0;
858fd558d18SJames Chapman 
859fd558d18SJames Chapman discard_bad_csum:
860fd558d18SJames Chapman 	LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
861fd558d18SJames Chapman 	UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
862fd558d18SJames Chapman 	tunnel->stats.rx_errors++;
863fd558d18SJames Chapman 	kfree_skb(skb);
864fd558d18SJames Chapman 
865fd558d18SJames Chapman 	return 0;
866fd558d18SJames Chapman 
867fd558d18SJames Chapman error:
868fd558d18SJames Chapman 	/* Put UDP header back */
869fd558d18SJames Chapman 	__skb_push(skb, sizeof(struct udphdr));
870fd558d18SJames Chapman 
871fd558d18SJames Chapman 	return 1;
872fd558d18SJames Chapman }
873fd558d18SJames Chapman 
874fd558d18SJames Chapman /* UDP encapsulation receive handler. See net/ipv4/udp.c.
875fd558d18SJames Chapman  * Return codes:
876fd558d18SJames Chapman  * 0 : success.
877fd558d18SJames Chapman  * <0: error
878fd558d18SJames Chapman  * >0: skb should be passed up to userspace as UDP.
879fd558d18SJames Chapman  */
880fd558d18SJames Chapman int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
881fd558d18SJames Chapman {
882fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
883fd558d18SJames Chapman 
884fd558d18SJames Chapman 	tunnel = l2tp_sock_to_tunnel(sk);
885fd558d18SJames Chapman 	if (tunnel == NULL)
886fd558d18SJames Chapman 		goto pass_up;
887fd558d18SJames Chapman 
888fd558d18SJames Chapman 	PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
889fd558d18SJames Chapman 	       "%s: received %d bytes\n", tunnel->name, skb->len);
890fd558d18SJames Chapman 
891fd558d18SJames Chapman 	if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
892fd558d18SJames Chapman 		goto pass_up_put;
893fd558d18SJames Chapman 
894fd558d18SJames Chapman 	sock_put(sk);
895fd558d18SJames Chapman 	return 0;
896fd558d18SJames Chapman 
897fd558d18SJames Chapman pass_up_put:
898fd558d18SJames Chapman 	sock_put(sk);
899fd558d18SJames Chapman pass_up:
900fd558d18SJames Chapman 	return 1;
901fd558d18SJames Chapman }
902fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
903fd558d18SJames Chapman 
904fd558d18SJames Chapman /************************************************************************
905fd558d18SJames Chapman  * Transmit handling
906fd558d18SJames Chapman  ***********************************************************************/
907fd558d18SJames Chapman 
908fd558d18SJames Chapman /* Build an L2TP header for the session into the buffer provided.
909fd558d18SJames Chapman  */
910f7faffa3SJames Chapman static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
911fd558d18SJames Chapman {
912f7faffa3SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
913fd558d18SJames Chapman 	__be16 *bufp = buf;
914f7faffa3SJames Chapman 	__be16 *optr = buf;
915fd558d18SJames Chapman 	u16 flags = L2TP_HDR_VER_2;
916fd558d18SJames Chapman 	u32 tunnel_id = tunnel->peer_tunnel_id;
917fd558d18SJames Chapman 	u32 session_id = session->peer_session_id;
918fd558d18SJames Chapman 
919fd558d18SJames Chapman 	if (session->send_seq)
920fd558d18SJames Chapman 		flags |= L2TP_HDRFLAG_S;
921fd558d18SJames Chapman 
922fd558d18SJames Chapman 	/* Setup L2TP header. */
923fd558d18SJames Chapman 	*bufp++ = htons(flags);
924fd558d18SJames Chapman 	*bufp++ = htons(tunnel_id);
925fd558d18SJames Chapman 	*bufp++ = htons(session_id);
926fd558d18SJames Chapman 	if (session->send_seq) {
927fd558d18SJames Chapman 		*bufp++ = htons(session->ns);
928fd558d18SJames Chapman 		*bufp++ = 0;
929fd558d18SJames Chapman 		session->ns++;
930f7faffa3SJames Chapman 		session->ns &= 0xffff;
931fd558d18SJames Chapman 		PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
932f7faffa3SJames Chapman 		       "%s: updated ns to %u\n", session->name, session->ns);
933fd558d18SJames Chapman 	}
934fd558d18SJames Chapman 
935f7faffa3SJames Chapman 	return bufp - optr;
936f7faffa3SJames Chapman }
937f7faffa3SJames Chapman 
938f7faffa3SJames Chapman static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
939fd558d18SJames Chapman {
9400d76751fSJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
941f7faffa3SJames Chapman 	char *bufp = buf;
942f7faffa3SJames Chapman 	char *optr = bufp;
943fd558d18SJames Chapman 
9440d76751fSJames Chapman 	/* Setup L2TP header. The header differs slightly for UDP and
9450d76751fSJames Chapman 	 * IP encapsulations. For UDP, there is 4 bytes of flags.
9460d76751fSJames Chapman 	 */
9470d76751fSJames Chapman 	if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
9480d76751fSJames Chapman 		u16 flags = L2TP_HDR_VER_3;
949f7faffa3SJames Chapman 		*((__be16 *) bufp) = htons(flags);
950f7faffa3SJames Chapman 		bufp += 2;
951f7faffa3SJames Chapman 		*((__be16 *) bufp) = 0;
952f7faffa3SJames Chapman 		bufp += 2;
9530d76751fSJames Chapman 	}
9540d76751fSJames Chapman 
955f7faffa3SJames Chapman 	*((__be32 *) bufp) = htonl(session->peer_session_id);
956f7faffa3SJames Chapman 	bufp += 4;
957f7faffa3SJames Chapman 	if (session->cookie_len) {
958f7faffa3SJames Chapman 		memcpy(bufp, &session->cookie[0], session->cookie_len);
959f7faffa3SJames Chapman 		bufp += session->cookie_len;
960fd558d18SJames Chapman 	}
961f7faffa3SJames Chapman 	if (session->l2specific_len) {
962f7faffa3SJames Chapman 		if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
963f7faffa3SJames Chapman 			u32 l2h = 0;
964f7faffa3SJames Chapman 			if (session->send_seq) {
965f7faffa3SJames Chapman 				l2h = 0x40000000 | session->ns;
966f7faffa3SJames Chapman 				session->ns++;
967f7faffa3SJames Chapman 				session->ns &= 0xffffff;
968f7faffa3SJames Chapman 				PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
969f7faffa3SJames Chapman 				       "%s: updated ns to %u\n", session->name, session->ns);
970f7faffa3SJames Chapman 			}
971f7faffa3SJames Chapman 
972f7faffa3SJames Chapman 			*((__be32 *) bufp) = htonl(l2h);
973f7faffa3SJames Chapman 		}
974f7faffa3SJames Chapman 		bufp += session->l2specific_len;
975f7faffa3SJames Chapman 	}
976f7faffa3SJames Chapman 	if (session->offset)
977f7faffa3SJames Chapman 		bufp += session->offset;
978f7faffa3SJames Chapman 
979f7faffa3SJames Chapman 	return bufp - optr;
980f7faffa3SJames Chapman }
981fd558d18SJames Chapman 
982fc130840Sstephen hemminger static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
983d9d8da80SDavid S. Miller 			  struct flowi *fl, size_t data_len)
984fd558d18SJames Chapman {
985fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
986fd558d18SJames Chapman 	unsigned int len = skb->len;
987fd558d18SJames Chapman 	int error;
988fd558d18SJames Chapman 
989fd558d18SJames Chapman 	/* Debug */
990fd558d18SJames Chapman 	if (session->send_seq)
991fd558d18SJames Chapman 		PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
992f7faffa3SJames Chapman 		       "%s: send %Zd bytes, ns=%u\n", session->name,
993fd558d18SJames Chapman 		       data_len, session->ns - 1);
994fd558d18SJames Chapman 	else
995fd558d18SJames Chapman 		PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
996fd558d18SJames Chapman 		       "%s: send %Zd bytes\n", session->name, data_len);
997fd558d18SJames Chapman 
998fd558d18SJames Chapman 	if (session->debug & L2TP_MSG_DATA) {
999fd558d18SJames Chapman 		int i;
10000d76751fSJames Chapman 		int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
10010d76751fSJames Chapman 		unsigned char *datap = skb->data + uhlen;
1002fd558d18SJames Chapman 
1003fd558d18SJames Chapman 		printk(KERN_DEBUG "%s: xmit:", session->name);
10040d76751fSJames Chapman 		for (i = 0; i < (len - uhlen); i++) {
1005fd558d18SJames Chapman 			printk(" %02X", *datap++);
1006fd558d18SJames Chapman 			if (i == 31) {
1007fd558d18SJames Chapman 				printk(" ...");
1008fd558d18SJames Chapman 				break;
1009fd558d18SJames Chapman 			}
1010fd558d18SJames Chapman 		}
1011fd558d18SJames Chapman 		printk("\n");
1012fd558d18SJames Chapman 	}
1013fd558d18SJames Chapman 
1014fd558d18SJames Chapman 	/* Queue the packet to IP for output */
10154e15ed4dSShan Wei 	skb->local_df = 1;
1016d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1017d2cf3361SBenjamin LaHaise 	if (skb->sk->sk_family == PF_INET6)
1018d2cf3361SBenjamin LaHaise 		error = inet6_csk_xmit(skb, NULL);
1019d2cf3361SBenjamin LaHaise 	else
1020d2cf3361SBenjamin LaHaise #endif
1021d9d8da80SDavid S. Miller 		error = ip_queue_xmit(skb, fl);
1022fd558d18SJames Chapman 
1023fd558d18SJames Chapman 	/* Update stats */
1024fd558d18SJames Chapman 	if (error >= 0) {
1025fd558d18SJames Chapman 		tunnel->stats.tx_packets++;
1026fd558d18SJames Chapman 		tunnel->stats.tx_bytes += len;
1027fd558d18SJames Chapman 		session->stats.tx_packets++;
1028fd558d18SJames Chapman 		session->stats.tx_bytes += len;
1029fd558d18SJames Chapman 	} else {
1030fd558d18SJames Chapman 		tunnel->stats.tx_errors++;
1031fd558d18SJames Chapman 		session->stats.tx_errors++;
1032fd558d18SJames Chapman 	}
1033fd558d18SJames Chapman 
1034fd558d18SJames Chapman 	return 0;
1035fd558d18SJames Chapman }
1036fd558d18SJames Chapman 
1037fd558d18SJames Chapman /* Automatically called when the skb is freed.
1038fd558d18SJames Chapman  */
1039fd558d18SJames Chapman static void l2tp_sock_wfree(struct sk_buff *skb)
1040fd558d18SJames Chapman {
1041fd558d18SJames Chapman 	sock_put(skb->sk);
1042fd558d18SJames Chapman }
1043fd558d18SJames Chapman 
1044fd558d18SJames Chapman /* For data skbs that we transmit, we associate with the tunnel socket
1045fd558d18SJames Chapman  * but don't do accounting.
1046fd558d18SJames Chapman  */
1047fd558d18SJames Chapman static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1048fd558d18SJames Chapman {
1049fd558d18SJames Chapman 	sock_hold(sk);
1050fd558d18SJames Chapman 	skb->sk = sk;
1051fd558d18SJames Chapman 	skb->destructor = l2tp_sock_wfree;
1052fd558d18SJames Chapman }
1053fd558d18SJames Chapman 
1054d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1055d2cf3361SBenjamin LaHaise static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1056d2cf3361SBenjamin LaHaise 				int udp_len)
1057d2cf3361SBenjamin LaHaise {
1058d2cf3361SBenjamin LaHaise 	struct ipv6_pinfo *np = inet6_sk(sk);
1059d2cf3361SBenjamin LaHaise 	struct udphdr *uh = udp_hdr(skb);
1060d2cf3361SBenjamin LaHaise 
1061d2cf3361SBenjamin LaHaise 	if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1062d2cf3361SBenjamin LaHaise 	    !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1063d2cf3361SBenjamin LaHaise 		__wsum csum = skb_checksum(skb, 0, udp_len, 0);
1064d2cf3361SBenjamin LaHaise 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1065d2cf3361SBenjamin LaHaise 		uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1066d2cf3361SBenjamin LaHaise 					    IPPROTO_UDP, csum);
1067d2cf3361SBenjamin LaHaise 		if (uh->check == 0)
1068d2cf3361SBenjamin LaHaise 			uh->check = CSUM_MANGLED_0;
1069d2cf3361SBenjamin LaHaise 	} else {
1070d2cf3361SBenjamin LaHaise 		skb->ip_summed = CHECKSUM_PARTIAL;
1071d2cf3361SBenjamin LaHaise 		skb->csum_start = skb_transport_header(skb) - skb->head;
1072d2cf3361SBenjamin LaHaise 		skb->csum_offset = offsetof(struct udphdr, check);
1073d2cf3361SBenjamin LaHaise 		uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1074d2cf3361SBenjamin LaHaise 					     udp_len, IPPROTO_UDP, 0);
1075d2cf3361SBenjamin LaHaise 	}
1076d2cf3361SBenjamin LaHaise }
1077d2cf3361SBenjamin LaHaise #endif
1078d2cf3361SBenjamin LaHaise 
1079fd558d18SJames Chapman /* If caller requires the skb to have a ppp header, the header must be
1080fd558d18SJames Chapman  * inserted in the skb data before calling this function.
1081fd558d18SJames Chapman  */
1082fd558d18SJames Chapman int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1083fd558d18SJames Chapman {
1084fd558d18SJames Chapman 	int data_len = skb->len;
10850d76751fSJames Chapman 	struct l2tp_tunnel *tunnel = session->tunnel;
10860d76751fSJames Chapman 	struct sock *sk = tunnel->sock;
1087d9d8da80SDavid S. Miller 	struct flowi *fl;
1088fd558d18SJames Chapman 	struct udphdr *uh;
1089fd558d18SJames Chapman 	struct inet_sock *inet;
1090fd558d18SJames Chapman 	__wsum csum;
1091fd558d18SJames Chapman 	int old_headroom;
1092fd558d18SJames Chapman 	int new_headroom;
1093fd558d18SJames Chapman 	int headroom;
10940d76751fSJames Chapman 	int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
10950d76751fSJames Chapman 	int udp_len;
1096fd558d18SJames Chapman 
1097fd558d18SJames Chapman 	/* Check that there's enough headroom in the skb to insert IP,
1098fd558d18SJames Chapman 	 * UDP and L2TP headers. If not enough, expand it to
1099fd558d18SJames Chapman 	 * make room. Adjust truesize.
1100fd558d18SJames Chapman 	 */
1101fd558d18SJames Chapman 	headroom = NET_SKB_PAD + sizeof(struct iphdr) +
11020d76751fSJames Chapman 		uhlen + hdr_len;
1103fd558d18SJames Chapman 	old_headroom = skb_headroom(skb);
1104835acf5dSEric Dumazet 	if (skb_cow_head(skb, headroom)) {
1105835acf5dSEric Dumazet 		dev_kfree_skb(skb);
1106fd558d18SJames Chapman 		goto abort;
1107835acf5dSEric Dumazet 	}
1108fd558d18SJames Chapman 
1109fd558d18SJames Chapman 	new_headroom = skb_headroom(skb);
1110fd558d18SJames Chapman 	skb_orphan(skb);
1111fd558d18SJames Chapman 	skb->truesize += new_headroom - old_headroom;
1112fd558d18SJames Chapman 
1113fd558d18SJames Chapman 	/* Setup L2TP header */
1114f7faffa3SJames Chapman 	session->build_header(session, __skb_push(skb, hdr_len));
1115fd558d18SJames Chapman 
11160d76751fSJames Chapman 	/* Reset skb netfilter state */
1117fd558d18SJames Chapman 	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1118fd558d18SJames Chapman 	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1119fd558d18SJames Chapman 			      IPSKB_REROUTED);
1120fd558d18SJames Chapman 	nf_reset(skb);
1121fd558d18SJames Chapman 
11226af88da1SDavid S. Miller 	bh_lock_sock(sk);
11236af88da1SDavid S. Miller 	if (sock_owned_by_user(sk)) {
11246af88da1SDavid S. Miller 		dev_kfree_skb(skb);
11256af88da1SDavid S. Miller 		goto out_unlock;
11266af88da1SDavid S. Miller 	}
11276af88da1SDavid S. Miller 
1128fd558d18SJames Chapman 	/* Get routing info from the tunnel socket */
1129fd558d18SJames Chapman 	skb_dst_drop(skb);
113071b1391aSFlorian Westphal 	skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
11310d76751fSJames Chapman 
1132d9d8da80SDavid S. Miller 	inet = inet_sk(sk);
1133d9d8da80SDavid S. Miller 	fl = &inet->cork.fl;
11340d76751fSJames Chapman 	switch (tunnel->encap) {
11350d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
11360d76751fSJames Chapman 		/* Setup UDP header */
11370d76751fSJames Chapman 		__skb_push(skb, sizeof(*uh));
11380d76751fSJames Chapman 		skb_reset_transport_header(skb);
11390d76751fSJames Chapman 		uh = udp_hdr(skb);
11400d76751fSJames Chapman 		uh->source = inet->inet_sport;
11410d76751fSJames Chapman 		uh->dest = inet->inet_dport;
11420d76751fSJames Chapman 		udp_len = uhlen + hdr_len + data_len;
11430d76751fSJames Chapman 		uh->len = htons(udp_len);
11440d76751fSJames Chapman 		uh->check = 0;
1145fd558d18SJames Chapman 
1146fd558d18SJames Chapman 		/* Calculate UDP checksum if configured to do so */
1147d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1148d2cf3361SBenjamin LaHaise 		if (sk->sk_family == PF_INET6)
1149d2cf3361SBenjamin LaHaise 			l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1150d2cf3361SBenjamin LaHaise 		else
1151d2cf3361SBenjamin LaHaise #endif
1152fd558d18SJames Chapman 		if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1153fd558d18SJames Chapman 			skb->ip_summed = CHECKSUM_NONE;
1154fd558d18SJames Chapman 		else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1155fd558d18SJames Chapman 			 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1156fd558d18SJames Chapman 			skb->ip_summed = CHECKSUM_COMPLETE;
1157fd558d18SJames Chapman 			csum = skb_checksum(skb, 0, udp_len, 0);
1158fd558d18SJames Chapman 			uh->check = csum_tcpudp_magic(inet->inet_saddr,
1159fd558d18SJames Chapman 						      inet->inet_daddr,
1160fd558d18SJames Chapman 						      udp_len, IPPROTO_UDP, csum);
1161fd558d18SJames Chapman 			if (uh->check == 0)
1162fd558d18SJames Chapman 				uh->check = CSUM_MANGLED_0;
1163fd558d18SJames Chapman 		} else {
1164fd558d18SJames Chapman 			skb->ip_summed = CHECKSUM_PARTIAL;
1165fd558d18SJames Chapman 			skb->csum_start = skb_transport_header(skb) - skb->head;
1166fd558d18SJames Chapman 			skb->csum_offset = offsetof(struct udphdr, check);
1167fd558d18SJames Chapman 			uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1168fd558d18SJames Chapman 						       inet->inet_daddr,
1169fd558d18SJames Chapman 						       udp_len, IPPROTO_UDP, 0);
1170fd558d18SJames Chapman 		}
11710d76751fSJames Chapman 		break;
11720d76751fSJames Chapman 
11730d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
11740d76751fSJames Chapman 		break;
11750d76751fSJames Chapman 	}
11760d76751fSJames Chapman 
11770d76751fSJames Chapman 	l2tp_skb_set_owner_w(skb, sk);
1178fd558d18SJames Chapman 
1179d9d8da80SDavid S. Miller 	l2tp_xmit_core(session, skb, fl, data_len);
11806af88da1SDavid S. Miller out_unlock:
11816af88da1SDavid S. Miller 	bh_unlock_sock(sk);
1182fd558d18SJames Chapman 
1183fd558d18SJames Chapman abort:
1184fd558d18SJames Chapman 	return 0;
1185fd558d18SJames Chapman }
1186fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1187fd558d18SJames Chapman 
1188fd558d18SJames Chapman /*****************************************************************************
1189fd558d18SJames Chapman  * Tinnel and session create/destroy.
1190fd558d18SJames Chapman  *****************************************************************************/
1191fd558d18SJames Chapman 
1192fd558d18SJames Chapman /* Tunnel socket destruct hook.
1193fd558d18SJames Chapman  * The tunnel context is deleted only when all session sockets have been
1194fd558d18SJames Chapman  * closed.
1195fd558d18SJames Chapman  */
1196fc130840Sstephen hemminger static void l2tp_tunnel_destruct(struct sock *sk)
1197fd558d18SJames Chapman {
1198fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1199fd558d18SJames Chapman 
1200fd558d18SJames Chapman 	tunnel = sk->sk_user_data;
1201fd558d18SJames Chapman 	if (tunnel == NULL)
1202fd558d18SJames Chapman 		goto end;
1203fd558d18SJames Chapman 
1204fd558d18SJames Chapman 	PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1205fd558d18SJames Chapman 	       "%s: closing...\n", tunnel->name);
1206fd558d18SJames Chapman 
1207fd558d18SJames Chapman 	/* Close all sessions */
1208fd558d18SJames Chapman 	l2tp_tunnel_closeall(tunnel);
1209fd558d18SJames Chapman 
12100d76751fSJames Chapman 	switch (tunnel->encap) {
12110d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1212fd558d18SJames Chapman 		/* No longer an encapsulation socket. See net/ipv4/udp.c */
1213fd558d18SJames Chapman 		(udp_sk(sk))->encap_type = 0;
1214fd558d18SJames Chapman 		(udp_sk(sk))->encap_rcv = NULL;
12150d76751fSJames Chapman 		break;
12160d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
12170d76751fSJames Chapman 		break;
12180d76751fSJames Chapman 	}
1219fd558d18SJames Chapman 
1220fd558d18SJames Chapman 	/* Remove hooks into tunnel socket */
1221fd558d18SJames Chapman 	tunnel->sock = NULL;
1222fd558d18SJames Chapman 	sk->sk_destruct = tunnel->old_sk_destruct;
1223fd558d18SJames Chapman 	sk->sk_user_data = NULL;
1224fd558d18SJames Chapman 
1225fd558d18SJames Chapman 	/* Call the original destructor */
1226fd558d18SJames Chapman 	if (sk->sk_destruct)
1227fd558d18SJames Chapman 		(*sk->sk_destruct)(sk);
1228fd558d18SJames Chapman 
1229fd558d18SJames Chapman 	/* We're finished with the socket */
1230fd558d18SJames Chapman 	l2tp_tunnel_dec_refcount(tunnel);
1231fd558d18SJames Chapman 
1232fd558d18SJames Chapman end:
1233fd558d18SJames Chapman 	return;
1234fd558d18SJames Chapman }
1235fd558d18SJames Chapman 
1236fd558d18SJames Chapman /* When the tunnel is closed, all the attached sessions need to go too.
1237fd558d18SJames Chapman  */
1238fc130840Sstephen hemminger static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1239fd558d18SJames Chapman {
1240fd558d18SJames Chapman 	int hash;
1241fd558d18SJames Chapman 	struct hlist_node *walk;
1242fd558d18SJames Chapman 	struct hlist_node *tmp;
1243fd558d18SJames Chapman 	struct l2tp_session *session;
1244fd558d18SJames Chapman 
1245fd558d18SJames Chapman 	BUG_ON(tunnel == NULL);
1246fd558d18SJames Chapman 
1247fd558d18SJames Chapman 	PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1248fd558d18SJames Chapman 	       "%s: closing all sessions...\n", tunnel->name);
1249fd558d18SJames Chapman 
1250fd558d18SJames Chapman 	write_lock_bh(&tunnel->hlist_lock);
1251fd558d18SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1252fd558d18SJames Chapman again:
1253fd558d18SJames Chapman 		hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1254fd558d18SJames Chapman 			session = hlist_entry(walk, struct l2tp_session, hlist);
1255fd558d18SJames Chapman 
1256fd558d18SJames Chapman 			PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1257fd558d18SJames Chapman 			       "%s: closing session\n", session->name);
1258fd558d18SJames Chapman 
1259fd558d18SJames Chapman 			hlist_del_init(&session->hlist);
1260fd558d18SJames Chapman 
1261fd558d18SJames Chapman 			/* Since we should hold the sock lock while
1262fd558d18SJames Chapman 			 * doing any unbinding, we need to release the
1263fd558d18SJames Chapman 			 * lock we're holding before taking that lock.
1264fd558d18SJames Chapman 			 * Hold a reference to the sock so it doesn't
1265fd558d18SJames Chapman 			 * disappear as we're jumping between locks.
1266fd558d18SJames Chapman 			 */
1267fd558d18SJames Chapman 			if (session->ref != NULL)
1268fd558d18SJames Chapman 				(*session->ref)(session);
1269fd558d18SJames Chapman 
1270fd558d18SJames Chapman 			write_unlock_bh(&tunnel->hlist_lock);
1271fd558d18SJames Chapman 
1272f7faffa3SJames Chapman 			if (tunnel->version != L2TP_HDR_VER_2) {
1273f7faffa3SJames Chapman 				struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1274f7faffa3SJames Chapman 
1275e02d494dSJames Chapman 				spin_lock_bh(&pn->l2tp_session_hlist_lock);
1276e02d494dSJames Chapman 				hlist_del_init_rcu(&session->global_hlist);
1277e02d494dSJames Chapman 				spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1278e02d494dSJames Chapman 				synchronize_rcu();
1279f7faffa3SJames Chapman 			}
1280f7faffa3SJames Chapman 
1281fd558d18SJames Chapman 			if (session->session_close != NULL)
1282fd558d18SJames Chapman 				(*session->session_close)(session);
1283fd558d18SJames Chapman 
1284fd558d18SJames Chapman 			if (session->deref != NULL)
1285fd558d18SJames Chapman 				(*session->deref)(session);
1286fd558d18SJames Chapman 
1287fd558d18SJames Chapman 			write_lock_bh(&tunnel->hlist_lock);
1288fd558d18SJames Chapman 
1289fd558d18SJames Chapman 			/* Now restart from the beginning of this hash
1290fd558d18SJames Chapman 			 * chain.  We always remove a session from the
1291fd558d18SJames Chapman 			 * list so we are guaranteed to make forward
1292fd558d18SJames Chapman 			 * progress.
1293fd558d18SJames Chapman 			 */
1294fd558d18SJames Chapman 			goto again;
1295fd558d18SJames Chapman 		}
1296fd558d18SJames Chapman 	}
1297fd558d18SJames Chapman 	write_unlock_bh(&tunnel->hlist_lock);
1298fd558d18SJames Chapman }
1299fd558d18SJames Chapman 
1300fd558d18SJames Chapman /* Really kill the tunnel.
1301fd558d18SJames Chapman  * Come here only when all sessions have been cleared from the tunnel.
1302fd558d18SJames Chapman  */
1303fc130840Sstephen hemminger static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1304fd558d18SJames Chapman {
1305fd558d18SJames Chapman 	struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1306fd558d18SJames Chapman 
1307fd558d18SJames Chapman 	BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1308fd558d18SJames Chapman 	BUG_ON(tunnel->sock != NULL);
1309fd558d18SJames Chapman 
1310fd558d18SJames Chapman 	PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1311fd558d18SJames Chapman 	       "%s: free...\n", tunnel->name);
1312fd558d18SJames Chapman 
1313fd558d18SJames Chapman 	/* Remove from tunnel list */
1314e02d494dSJames Chapman 	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1315e02d494dSJames Chapman 	list_del_rcu(&tunnel->list);
1316e02d494dSJames Chapman 	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1317e02d494dSJames Chapman 	synchronize_rcu();
1318fd558d18SJames Chapman 
1319fd558d18SJames Chapman 	atomic_dec(&l2tp_tunnel_count);
1320fd558d18SJames Chapman 	kfree(tunnel);
1321fd558d18SJames Chapman }
1322fd558d18SJames Chapman 
1323789a4a2cSJames Chapman /* Create a socket for the tunnel, if one isn't set up by
1324789a4a2cSJames Chapman  * userspace. This is used for static tunnels where there is no
1325789a4a2cSJames Chapman  * managing L2TP daemon.
1326789a4a2cSJames Chapman  */
1327789a4a2cSJames Chapman static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
1328789a4a2cSJames Chapman {
1329789a4a2cSJames Chapman 	int err = -EINVAL;
1330789a4a2cSJames Chapman 	struct sockaddr_in udp_addr;
1331789a4a2cSJames Chapman 	struct sockaddr_l2tpip ip_addr;
13327bddd0dbSEric Dumazet 	struct socket *sock = NULL;
1333789a4a2cSJames Chapman 
1334789a4a2cSJames Chapman 	switch (cfg->encap) {
1335789a4a2cSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1336789a4a2cSJames Chapman 		err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1337789a4a2cSJames Chapman 		if (err < 0)
1338789a4a2cSJames Chapman 			goto out;
1339789a4a2cSJames Chapman 
1340789a4a2cSJames Chapman 		sock = *sockp;
1341789a4a2cSJames Chapman 
1342789a4a2cSJames Chapman 		memset(&udp_addr, 0, sizeof(udp_addr));
1343789a4a2cSJames Chapman 		udp_addr.sin_family = AF_INET;
1344789a4a2cSJames Chapman 		udp_addr.sin_addr = cfg->local_ip;
1345789a4a2cSJames Chapman 		udp_addr.sin_port = htons(cfg->local_udp_port);
1346789a4a2cSJames Chapman 		err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
1347789a4a2cSJames Chapman 		if (err < 0)
1348789a4a2cSJames Chapman 			goto out;
1349789a4a2cSJames Chapman 
1350789a4a2cSJames Chapman 		udp_addr.sin_family = AF_INET;
1351789a4a2cSJames Chapman 		udp_addr.sin_addr = cfg->peer_ip;
1352789a4a2cSJames Chapman 		udp_addr.sin_port = htons(cfg->peer_udp_port);
1353789a4a2cSJames Chapman 		err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0);
1354789a4a2cSJames Chapman 		if (err < 0)
1355789a4a2cSJames Chapman 			goto out;
1356789a4a2cSJames Chapman 
1357789a4a2cSJames Chapman 		if (!cfg->use_udp_checksums)
1358789a4a2cSJames Chapman 			sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1359789a4a2cSJames Chapman 
1360789a4a2cSJames Chapman 		break;
1361789a4a2cSJames Chapman 
1362789a4a2cSJames Chapman 	case L2TP_ENCAPTYPE_IP:
1363789a4a2cSJames Chapman 		err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
1364789a4a2cSJames Chapman 		if (err < 0)
1365789a4a2cSJames Chapman 			goto out;
1366789a4a2cSJames Chapman 
1367789a4a2cSJames Chapman 		sock = *sockp;
1368789a4a2cSJames Chapman 
1369789a4a2cSJames Chapman 		memset(&ip_addr, 0, sizeof(ip_addr));
1370789a4a2cSJames Chapman 		ip_addr.l2tp_family = AF_INET;
1371789a4a2cSJames Chapman 		ip_addr.l2tp_addr = cfg->local_ip;
1372789a4a2cSJames Chapman 		ip_addr.l2tp_conn_id = tunnel_id;
1373789a4a2cSJames Chapman 		err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
1374789a4a2cSJames Chapman 		if (err < 0)
1375789a4a2cSJames Chapman 			goto out;
1376789a4a2cSJames Chapman 
1377789a4a2cSJames Chapman 		ip_addr.l2tp_family = AF_INET;
1378789a4a2cSJames Chapman 		ip_addr.l2tp_addr = cfg->peer_ip;
1379789a4a2cSJames Chapman 		ip_addr.l2tp_conn_id = peer_tunnel_id;
1380789a4a2cSJames Chapman 		err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
1381789a4a2cSJames Chapman 		if (err < 0)
1382789a4a2cSJames Chapman 			goto out;
1383789a4a2cSJames Chapman 
1384789a4a2cSJames Chapman 		break;
1385789a4a2cSJames Chapman 
1386789a4a2cSJames Chapman 	default:
1387789a4a2cSJames Chapman 		goto out;
1388789a4a2cSJames Chapman 	}
1389789a4a2cSJames Chapman 
1390789a4a2cSJames Chapman out:
1391789a4a2cSJames Chapman 	if ((err < 0) && sock) {
1392789a4a2cSJames Chapman 		sock_release(sock);
1393789a4a2cSJames Chapman 		*sockp = NULL;
1394789a4a2cSJames Chapman 	}
1395789a4a2cSJames Chapman 
1396789a4a2cSJames Chapman 	return err;
1397789a4a2cSJames Chapman }
1398789a4a2cSJames Chapman 
1399fd558d18SJames 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)
1400fd558d18SJames Chapman {
1401fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel = NULL;
1402fd558d18SJames Chapman 	int err;
1403fd558d18SJames Chapman 	struct socket *sock = NULL;
1404fd558d18SJames Chapman 	struct sock *sk = NULL;
1405fd558d18SJames Chapman 	struct l2tp_net *pn;
14060d76751fSJames Chapman 	enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1407fd558d18SJames Chapman 
1408fd558d18SJames Chapman 	/* Get the tunnel socket from the fd, which was opened by
1409789a4a2cSJames Chapman 	 * the userspace L2TP daemon. If not specified, create a
1410789a4a2cSJames Chapman 	 * kernel socket.
1411fd558d18SJames Chapman 	 */
1412789a4a2cSJames Chapman 	if (fd < 0) {
1413789a4a2cSJames Chapman 		err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1414789a4a2cSJames Chapman 		if (err < 0)
1415789a4a2cSJames Chapman 			goto err;
1416789a4a2cSJames Chapman 	} else {
1417fd558d18SJames Chapman 		err = -EBADF;
1418fd558d18SJames Chapman 		sock = sockfd_lookup(fd, &err);
1419fd558d18SJames Chapman 		if (!sock) {
1420fd558d18SJames Chapman 			printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1421fd558d18SJames Chapman 			       tunnel_id, fd, err);
1422fd558d18SJames Chapman 			goto err;
1423fd558d18SJames Chapman 		}
1424789a4a2cSJames Chapman 	}
1425fd558d18SJames Chapman 
1426fd558d18SJames Chapman 	sk = sock->sk;
1427fd558d18SJames Chapman 
14280d76751fSJames Chapman 	if (cfg != NULL)
14290d76751fSJames Chapman 		encap = cfg->encap;
14300d76751fSJames Chapman 
1431fd558d18SJames Chapman 	/* Quick sanity checks */
14320d76751fSJames Chapman 	switch (encap) {
14330d76751fSJames Chapman 	case L2TP_ENCAPTYPE_UDP:
1434fd558d18SJames Chapman 		err = -EPROTONOSUPPORT;
1435fd558d18SJames Chapman 		if (sk->sk_protocol != IPPROTO_UDP) {
1436fd558d18SJames Chapman 			printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1437fd558d18SJames Chapman 			       tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1438fd558d18SJames Chapman 			goto err;
1439fd558d18SJames Chapman 		}
14400d76751fSJames Chapman 		break;
14410d76751fSJames Chapman 	case L2TP_ENCAPTYPE_IP:
14420d76751fSJames Chapman 		err = -EPROTONOSUPPORT;
14430d76751fSJames Chapman 		if (sk->sk_protocol != IPPROTO_L2TP) {
14440d76751fSJames Chapman 			printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
14450d76751fSJames Chapman 			       tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1446fd558d18SJames Chapman 			goto err;
1447fd558d18SJames Chapman 		}
14480d76751fSJames Chapman 		break;
14490d76751fSJames Chapman 	}
1450fd558d18SJames Chapman 
1451fd558d18SJames Chapman 	/* Check if this socket has already been prepped */
1452fd558d18SJames Chapman 	tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1453fd558d18SJames Chapman 	if (tunnel != NULL) {
1454fd558d18SJames Chapman 		/* This socket has already been prepped */
1455fd558d18SJames Chapman 		err = -EBUSY;
1456fd558d18SJames Chapman 		goto err;
1457fd558d18SJames Chapman 	}
1458fd558d18SJames Chapman 
1459fd558d18SJames Chapman 	tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1460fd558d18SJames Chapman 	if (tunnel == NULL) {
1461fd558d18SJames Chapman 		err = -ENOMEM;
1462fd558d18SJames Chapman 		goto err;
1463fd558d18SJames Chapman 	}
1464fd558d18SJames Chapman 
1465fd558d18SJames Chapman 	tunnel->version = version;
1466fd558d18SJames Chapman 	tunnel->tunnel_id = tunnel_id;
1467fd558d18SJames Chapman 	tunnel->peer_tunnel_id = peer_tunnel_id;
1468fd558d18SJames Chapman 	tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1469fd558d18SJames Chapman 
1470fd558d18SJames Chapman 	tunnel->magic = L2TP_TUNNEL_MAGIC;
1471fd558d18SJames Chapman 	sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1472fd558d18SJames Chapman 	rwlock_init(&tunnel->hlist_lock);
1473fd558d18SJames Chapman 
1474fd558d18SJames Chapman 	/* The net we belong to */
1475fd558d18SJames Chapman 	tunnel->l2tp_net = net;
1476fd558d18SJames Chapman 	pn = l2tp_pernet(net);
1477fd558d18SJames Chapman 
14780d76751fSJames Chapman 	if (cfg != NULL)
1479fd558d18SJames Chapman 		tunnel->debug = cfg->debug;
1480fd558d18SJames Chapman 
1481fd558d18SJames Chapman 	/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
14820d76751fSJames Chapman 	tunnel->encap = encap;
14830d76751fSJames Chapman 	if (encap == L2TP_ENCAPTYPE_UDP) {
14840d76751fSJames Chapman 		/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1485fd558d18SJames Chapman 		udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1486fd558d18SJames Chapman 		udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1487d2cf3361SBenjamin LaHaise #if IS_ENABLED(CONFIG_IPV6)
1488d2cf3361SBenjamin LaHaise 		if (sk->sk_family == PF_INET6)
1489d2cf3361SBenjamin LaHaise 			udpv6_encap_enable();
1490d2cf3361SBenjamin LaHaise 		else
1491d2cf3361SBenjamin LaHaise #endif
1492447167bfSEric Dumazet 		udp_encap_enable();
14930d76751fSJames Chapman 	}
1494fd558d18SJames Chapman 
1495fd558d18SJames Chapman 	sk->sk_user_data = tunnel;
1496fd558d18SJames Chapman 
1497fd558d18SJames Chapman 	/* Hook on the tunnel socket destructor so that we can cleanup
1498fd558d18SJames Chapman 	 * if the tunnel socket goes away.
1499fd558d18SJames Chapman 	 */
1500fd558d18SJames Chapman 	tunnel->old_sk_destruct = sk->sk_destruct;
1501fd558d18SJames Chapman 	sk->sk_destruct = &l2tp_tunnel_destruct;
1502fd558d18SJames Chapman 	tunnel->sock = sk;
1503fd558d18SJames Chapman 	sk->sk_allocation = GFP_ATOMIC;
1504fd558d18SJames Chapman 
1505fd558d18SJames Chapman 	/* Add tunnel to our list */
1506fd558d18SJames Chapman 	INIT_LIST_HEAD(&tunnel->list);
1507fd558d18SJames Chapman 	atomic_inc(&l2tp_tunnel_count);
1508fd558d18SJames Chapman 
1509fd558d18SJames Chapman 	/* Bump the reference count. The tunnel context is deleted
15101769192aSEric Dumazet 	 * only when this drops to zero. Must be done before list insertion
1511fd558d18SJames Chapman 	 */
1512fd558d18SJames Chapman 	l2tp_tunnel_inc_refcount(tunnel);
15131769192aSEric Dumazet 	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
15141769192aSEric Dumazet 	list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
15151769192aSEric Dumazet 	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1516fd558d18SJames Chapman 
1517fd558d18SJames Chapman 	err = 0;
1518fd558d18SJames Chapman err:
1519fd558d18SJames Chapman 	if (tunnelp)
1520fd558d18SJames Chapman 		*tunnelp = tunnel;
1521fd558d18SJames Chapman 
1522789a4a2cSJames Chapman 	/* If tunnel's socket was created by the kernel, it doesn't
1523789a4a2cSJames Chapman 	 *  have a file.
1524789a4a2cSJames Chapman 	 */
1525789a4a2cSJames Chapman 	if (sock && sock->file)
1526fd558d18SJames Chapman 		sockfd_put(sock);
1527fd558d18SJames Chapman 
1528fd558d18SJames Chapman 	return err;
1529fd558d18SJames Chapman }
1530fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1531fd558d18SJames Chapman 
1532309795f4SJames Chapman /* This function is used by the netlink TUNNEL_DELETE command.
1533309795f4SJames Chapman  */
1534309795f4SJames Chapman int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1535309795f4SJames Chapman {
1536309795f4SJames Chapman 	int err = 0;
1537789a4a2cSJames Chapman 	struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
1538309795f4SJames Chapman 
1539309795f4SJames Chapman 	/* Force the tunnel socket to close. This will eventually
1540309795f4SJames Chapman 	 * cause the tunnel to be deleted via the normal socket close
1541309795f4SJames Chapman 	 * mechanisms when userspace closes the tunnel socket.
1542309795f4SJames Chapman 	 */
1543789a4a2cSJames Chapman 	if (sock != NULL) {
1544789a4a2cSJames Chapman 		err = inet_shutdown(sock, 2);
1545789a4a2cSJames Chapman 
1546789a4a2cSJames Chapman 		/* If the tunnel's socket was created by the kernel,
1547789a4a2cSJames Chapman 		 * close the socket here since the socket was not
1548789a4a2cSJames Chapman 		 * created by userspace.
1549789a4a2cSJames Chapman 		 */
1550789a4a2cSJames Chapman 		if (sock->file == NULL)
1551789a4a2cSJames Chapman 			err = inet_release(sock);
1552789a4a2cSJames Chapman 	}
1553309795f4SJames Chapman 
1554309795f4SJames Chapman 	return err;
1555309795f4SJames Chapman }
1556309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1557309795f4SJames Chapman 
1558fd558d18SJames Chapman /* Really kill the session.
1559fd558d18SJames Chapman  */
1560fd558d18SJames Chapman void l2tp_session_free(struct l2tp_session *session)
1561fd558d18SJames Chapman {
1562fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
1563fd558d18SJames Chapman 
1564fd558d18SJames Chapman 	BUG_ON(atomic_read(&session->ref_count) != 0);
1565fd558d18SJames Chapman 
1566fd558d18SJames Chapman 	tunnel = session->tunnel;
1567fd558d18SJames Chapman 	if (tunnel != NULL) {
1568fd558d18SJames Chapman 		BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1569fd558d18SJames Chapman 
1570fd558d18SJames Chapman 		/* Delete the session from the hash */
1571fd558d18SJames Chapman 		write_lock_bh(&tunnel->hlist_lock);
1572fd558d18SJames Chapman 		hlist_del_init(&session->hlist);
1573fd558d18SJames Chapman 		write_unlock_bh(&tunnel->hlist_lock);
1574fd558d18SJames Chapman 
1575f7faffa3SJames Chapman 		/* Unlink from the global hash if not L2TPv2 */
1576f7faffa3SJames Chapman 		if (tunnel->version != L2TP_HDR_VER_2) {
1577f7faffa3SJames Chapman 			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1578f7faffa3SJames Chapman 
1579e02d494dSJames Chapman 			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1580e02d494dSJames Chapman 			hlist_del_init_rcu(&session->global_hlist);
1581e02d494dSJames Chapman 			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1582e02d494dSJames Chapman 			synchronize_rcu();
1583f7faffa3SJames Chapman 		}
1584f7faffa3SJames Chapman 
1585fd558d18SJames Chapman 		if (session->session_id != 0)
1586fd558d18SJames Chapman 			atomic_dec(&l2tp_session_count);
1587fd558d18SJames Chapman 
1588fd558d18SJames Chapman 		sock_put(tunnel->sock);
1589fd558d18SJames Chapman 
1590fd558d18SJames Chapman 		/* This will delete the tunnel context if this
1591fd558d18SJames Chapman 		 * is the last session on the tunnel.
1592fd558d18SJames Chapman 		 */
1593fd558d18SJames Chapman 		session->tunnel = NULL;
1594fd558d18SJames Chapman 		l2tp_tunnel_dec_refcount(tunnel);
1595fd558d18SJames Chapman 	}
1596fd558d18SJames Chapman 
1597fd558d18SJames Chapman 	kfree(session);
1598fd558d18SJames Chapman 
1599fd558d18SJames Chapman 	return;
1600fd558d18SJames Chapman }
1601fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_free);
1602fd558d18SJames Chapman 
1603309795f4SJames Chapman /* This function is used by the netlink SESSION_DELETE command and by
1604309795f4SJames Chapman    pseudowire modules.
1605309795f4SJames Chapman  */
1606309795f4SJames Chapman int l2tp_session_delete(struct l2tp_session *session)
1607309795f4SJames Chapman {
1608309795f4SJames Chapman 	if (session->session_close != NULL)
1609309795f4SJames Chapman 		(*session->session_close)(session);
1610309795f4SJames Chapman 
1611309795f4SJames Chapman 	l2tp_session_dec_refcount(session);
1612309795f4SJames Chapman 
1613309795f4SJames Chapman 	return 0;
1614309795f4SJames Chapman }
1615309795f4SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_delete);
1616309795f4SJames Chapman 
1617309795f4SJames Chapman 
1618f7faffa3SJames Chapman /* We come here whenever a session's send_seq, cookie_len or
1619f7faffa3SJames Chapman  * l2specific_len parameters are set.
1620f7faffa3SJames Chapman  */
1621fc130840Sstephen hemminger static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1622f7faffa3SJames Chapman {
1623f7faffa3SJames Chapman 	if (version == L2TP_HDR_VER_2) {
1624f7faffa3SJames Chapman 		session->hdr_len = 6;
1625f7faffa3SJames Chapman 		if (session->send_seq)
1626f7faffa3SJames Chapman 			session->hdr_len += 4;
1627f7faffa3SJames Chapman 	} else {
16280d76751fSJames Chapman 		session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
16290d76751fSJames Chapman 		if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
16300d76751fSJames Chapman 			session->hdr_len += 4;
1631f7faffa3SJames Chapman 	}
1632f7faffa3SJames Chapman 
1633f7faffa3SJames Chapman }
1634f7faffa3SJames Chapman 
1635fd558d18SJames 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)
1636fd558d18SJames Chapman {
1637fd558d18SJames Chapman 	struct l2tp_session *session;
1638fd558d18SJames Chapman 
1639fd558d18SJames Chapman 	session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1640fd558d18SJames Chapman 	if (session != NULL) {
1641fd558d18SJames Chapman 		session->magic = L2TP_SESSION_MAGIC;
1642fd558d18SJames Chapman 		session->tunnel = tunnel;
1643fd558d18SJames Chapman 
1644fd558d18SJames Chapman 		session->session_id = session_id;
1645fd558d18SJames Chapman 		session->peer_session_id = peer_session_id;
1646f7faffa3SJames Chapman 		session->nr = 1;
1647fd558d18SJames Chapman 
1648fd558d18SJames Chapman 		sprintf(&session->name[0], "sess %u/%u",
1649fd558d18SJames Chapman 			tunnel->tunnel_id, session->session_id);
1650fd558d18SJames Chapman 
1651fd558d18SJames Chapman 		skb_queue_head_init(&session->reorder_q);
1652fd558d18SJames Chapman 
1653fd558d18SJames Chapman 		INIT_HLIST_NODE(&session->hlist);
1654f7faffa3SJames Chapman 		INIT_HLIST_NODE(&session->global_hlist);
1655fd558d18SJames Chapman 
1656fd558d18SJames Chapman 		/* Inherit debug options from tunnel */
1657fd558d18SJames Chapman 		session->debug = tunnel->debug;
1658fd558d18SJames Chapman 
1659fd558d18SJames Chapman 		if (cfg) {
1660f7faffa3SJames Chapman 			session->pwtype = cfg->pw_type;
1661fd558d18SJames Chapman 			session->debug = cfg->debug;
1662fd558d18SJames Chapman 			session->mtu = cfg->mtu;
1663fd558d18SJames Chapman 			session->mru = cfg->mru;
1664fd558d18SJames Chapman 			session->send_seq = cfg->send_seq;
1665fd558d18SJames Chapman 			session->recv_seq = cfg->recv_seq;
1666fd558d18SJames Chapman 			session->lns_mode = cfg->lns_mode;
1667f7faffa3SJames Chapman 			session->reorder_timeout = cfg->reorder_timeout;
1668f7faffa3SJames Chapman 			session->offset = cfg->offset;
1669f7faffa3SJames Chapman 			session->l2specific_type = cfg->l2specific_type;
1670f7faffa3SJames Chapman 			session->l2specific_len = cfg->l2specific_len;
1671f7faffa3SJames Chapman 			session->cookie_len = cfg->cookie_len;
1672f7faffa3SJames Chapman 			memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1673f7faffa3SJames Chapman 			session->peer_cookie_len = cfg->peer_cookie_len;
1674f7faffa3SJames Chapman 			memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1675fd558d18SJames Chapman 		}
1676fd558d18SJames Chapman 
1677f7faffa3SJames Chapman 		if (tunnel->version == L2TP_HDR_VER_2)
1678f7faffa3SJames Chapman 			session->build_header = l2tp_build_l2tpv2_header;
1679f7faffa3SJames Chapman 		else
1680f7faffa3SJames Chapman 			session->build_header = l2tp_build_l2tpv3_header;
1681f7faffa3SJames Chapman 
1682f7faffa3SJames Chapman 		l2tp_session_set_header_len(session, tunnel->version);
1683f7faffa3SJames Chapman 
1684fd558d18SJames Chapman 		/* Bump the reference count. The session context is deleted
1685fd558d18SJames Chapman 		 * only when this drops to zero.
1686fd558d18SJames Chapman 		 */
1687fd558d18SJames Chapman 		l2tp_session_inc_refcount(session);
1688fd558d18SJames Chapman 		l2tp_tunnel_inc_refcount(tunnel);
1689fd558d18SJames Chapman 
1690fd558d18SJames Chapman 		/* Ensure tunnel socket isn't deleted */
1691fd558d18SJames Chapman 		sock_hold(tunnel->sock);
1692fd558d18SJames Chapman 
1693fd558d18SJames Chapman 		/* Add session to the tunnel's hash list */
1694fd558d18SJames Chapman 		write_lock_bh(&tunnel->hlist_lock);
1695fd558d18SJames Chapman 		hlist_add_head(&session->hlist,
1696fd558d18SJames Chapman 			       l2tp_session_id_hash(tunnel, session_id));
1697fd558d18SJames Chapman 		write_unlock_bh(&tunnel->hlist_lock);
1698fd558d18SJames Chapman 
1699f7faffa3SJames Chapman 		/* And to the global session list if L2TPv3 */
1700f7faffa3SJames Chapman 		if (tunnel->version != L2TP_HDR_VER_2) {
1701f7faffa3SJames Chapman 			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1702f7faffa3SJames Chapman 
1703e02d494dSJames Chapman 			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1704e02d494dSJames Chapman 			hlist_add_head_rcu(&session->global_hlist,
1705f7faffa3SJames Chapman 					   l2tp_session_id_hash_2(pn, session_id));
1706e02d494dSJames Chapman 			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1707f7faffa3SJames Chapman 		}
1708f7faffa3SJames Chapman 
1709fd558d18SJames Chapman 		/* Ignore management session in session count value */
1710fd558d18SJames Chapman 		if (session->session_id != 0)
1711fd558d18SJames Chapman 			atomic_inc(&l2tp_session_count);
1712fd558d18SJames Chapman 	}
1713fd558d18SJames Chapman 
1714fd558d18SJames Chapman 	return session;
1715fd558d18SJames Chapman }
1716fd558d18SJames Chapman EXPORT_SYMBOL_GPL(l2tp_session_create);
1717fd558d18SJames Chapman 
1718fd558d18SJames Chapman /*****************************************************************************
1719fd558d18SJames Chapman  * Init and cleanup
1720fd558d18SJames Chapman  *****************************************************************************/
1721fd558d18SJames Chapman 
1722fd558d18SJames Chapman static __net_init int l2tp_init_net(struct net *net)
1723fd558d18SJames Chapman {
1724e773aaffSJiri Pirko 	struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1725f7faffa3SJames Chapman 	int hash;
1726fd558d18SJames Chapman 
1727fd558d18SJames Chapman 	INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1728e02d494dSJames Chapman 	spin_lock_init(&pn->l2tp_tunnel_list_lock);
1729fd558d18SJames Chapman 
1730f7faffa3SJames Chapman 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1731f7faffa3SJames Chapman 		INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1732f7faffa3SJames Chapman 
1733e02d494dSJames Chapman 	spin_lock_init(&pn->l2tp_session_hlist_lock);
1734f7faffa3SJames Chapman 
1735fd558d18SJames Chapman 	return 0;
1736fd558d18SJames Chapman }
1737fd558d18SJames Chapman 
1738fd558d18SJames Chapman static struct pernet_operations l2tp_net_ops = {
1739fd558d18SJames Chapman 	.init = l2tp_init_net,
1740fd558d18SJames Chapman 	.id   = &l2tp_net_id,
1741fd558d18SJames Chapman 	.size = sizeof(struct l2tp_net),
1742fd558d18SJames Chapman };
1743fd558d18SJames Chapman 
1744fd558d18SJames Chapman static int __init l2tp_init(void)
1745fd558d18SJames Chapman {
1746fd558d18SJames Chapman 	int rc = 0;
1747fd558d18SJames Chapman 
1748fd558d18SJames Chapman 	rc = register_pernet_device(&l2tp_net_ops);
1749fd558d18SJames Chapman 	if (rc)
1750fd558d18SJames Chapman 		goto out;
1751fd558d18SJames Chapman 
1752fd558d18SJames Chapman 	printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1753fd558d18SJames Chapman 
1754fd558d18SJames Chapman out:
1755fd558d18SJames Chapman 	return rc;
1756fd558d18SJames Chapman }
1757fd558d18SJames Chapman 
1758fd558d18SJames Chapman static void __exit l2tp_exit(void)
1759fd558d18SJames Chapman {
1760fd558d18SJames Chapman 	unregister_pernet_device(&l2tp_net_ops);
1761fd558d18SJames Chapman }
1762fd558d18SJames Chapman 
1763fd558d18SJames Chapman module_init(l2tp_init);
1764fd558d18SJames Chapman module_exit(l2tp_exit);
1765fd558d18SJames Chapman 
1766fd558d18SJames Chapman MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1767fd558d18SJames Chapman MODULE_DESCRIPTION("L2TP core");
1768fd558d18SJames Chapman MODULE_LICENSE("GPL");
1769fd558d18SJames Chapman MODULE_VERSION(L2TP_DRV_VERSION);
1770fd558d18SJames Chapman 
1771