1fd558d186df2c13a22455373858bae634a4795afJames Chapman/*
2fd558d186df2c13a22455373858bae634a4795afJames Chapman * L2TP core.
3fd558d186df2c13a22455373858bae634a4795afJames Chapman *
4fd558d186df2c13a22455373858bae634a4795afJames Chapman * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5fd558d186df2c13a22455373858bae634a4795afJames Chapman *
6fd558d186df2c13a22455373858bae634a4795afJames Chapman * This file contains some code of the original L2TPv2 pppol2tp
7fd558d186df2c13a22455373858bae634a4795afJames Chapman * driver, which has the following copyright:
8fd558d186df2c13a22455373858bae634a4795afJames Chapman *
9fd558d186df2c13a22455373858bae634a4795afJames Chapman * Authors:	Martijn van Oosterhout <kleptog@svana.org>
10fd558d186df2c13a22455373858bae634a4795afJames Chapman *		James Chapman (jchapman@katalix.com)
11fd558d186df2c13a22455373858bae634a4795afJames Chapman * Contributors:
12fd558d186df2c13a22455373858bae634a4795afJames Chapman *		Michal Ostrowski <mostrows@speakeasy.net>
13fd558d186df2c13a22455373858bae634a4795afJames Chapman *		Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14fd558d186df2c13a22455373858bae634a4795afJames Chapman *		David S. Miller (davem@redhat.com)
15fd558d186df2c13a22455373858bae634a4795afJames Chapman *
16fd558d186df2c13a22455373858bae634a4795afJames Chapman * This program is free software; you can redistribute it and/or modify
17fd558d186df2c13a22455373858bae634a4795afJames Chapman * it under the terms of the GNU General Public License version 2 as
18fd558d186df2c13a22455373858bae634a4795afJames Chapman * published by the Free Software Foundation.
19fd558d186df2c13a22455373858bae634a4795afJames Chapman */
20fd558d186df2c13a22455373858bae634a4795afJames Chapman
21a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches
23fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/module.h>
24fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/string.h>
25fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/list.h>
26e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman#include <linux/rculist.h>
27fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/uaccess.h>
28fd558d186df2c13a22455373858bae634a4795afJames Chapman
29fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/kernel.h>
30fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/spinlock.h>
31fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/kthread.h>
32fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/sched.h>
33fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/slab.h>
34fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/errno.h>
35fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/jiffies.h>
36fd558d186df2c13a22455373858bae634a4795afJames Chapman
37fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/netdevice.h>
38fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/net.h>
39fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/inetdevice.h>
40fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/skbuff.h>
41fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/init.h>
420d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman#include <linux/in.h>
43fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/ip.h>
44fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/udp.h>
450d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman#include <linux/l2tp.h>
46fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/hash.h>
47fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/sort.h>
48fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/file.h>
49fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <linux/nsproxy.h>
50fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <net/net_namespace.h>
51fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <net/netns/generic.h>
52fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <net/dst.h>
53fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <net/ip.h>
54fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <net/udp.h>
55309795f4bec2d69cd507a631f82065c2198a0825James Chapman#include <net/inet_common.h>
56fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <net/xfrm.h>
570d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman#include <net/protocol.h>
58d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#include <net/inet6_connection_sock.h>
59d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#include <net/inet_ecn.h>
60d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#include <net/ip6_route.h>
61d499bd2ee979cd0e1b5e3f6379d753582c67ec8cDavid S. Miller#include <net/ip6_checksum.h>
62fd558d186df2c13a22455373858bae634a4795afJames Chapman
63fd558d186df2c13a22455373858bae634a4795afJames Chapman#include <asm/byteorder.h>
6460063497a95e716c9a689af3be2687d261f115b4Arun Sharma#include <linux/atomic.h>
65fd558d186df2c13a22455373858bae634a4795afJames Chapman
66fd558d186df2c13a22455373858bae634a4795afJames Chapman#include "l2tp_core.h"
67fd558d186df2c13a22455373858bae634a4795afJames Chapman
68fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_DRV_VERSION	"V2.0"
69fd558d186df2c13a22455373858bae634a4795afJames Chapman
70fd558d186df2c13a22455373858bae634a4795afJames Chapman/* L2TP header constants */
71fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDRFLAG_T	   0x8000
72fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDRFLAG_L	   0x4000
73fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDRFLAG_S	   0x0800
74fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDRFLAG_O	   0x0200
75fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDRFLAG_P	   0x0100
76fd558d186df2c13a22455373858bae634a4795afJames Chapman
77fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDR_VER_MASK  0x000F
78fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDR_VER_2	   0x0002
79f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman#define L2TP_HDR_VER_3	   0x0003
80fd558d186df2c13a22455373858bae634a4795afJames Chapman
81fd558d186df2c13a22455373858bae634a4795afJames Chapman/* L2TPv3 default L2-specific sublayer */
82fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_SLFLAG_S	   0x40000000
83fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_SL_SEQ_MASK   0x00ffffff
84fd558d186df2c13a22455373858bae634a4795afJames Chapman
85fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDR_SIZE_SEQ		10
86fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_HDR_SIZE_NOSEQ		6
87fd558d186df2c13a22455373858bae634a4795afJames Chapman
88fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Default trace flags */
89fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_DEFAULT_DEBUG_FLAGS	0
90fd558d186df2c13a22455373858bae634a4795afJames Chapman
91fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Private data stored for received packets in the skb.
92fd558d186df2c13a22455373858bae634a4795afJames Chapman */
93fd558d186df2c13a22455373858bae634a4795afJames Chapmanstruct l2tp_skb_cb {
94f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	u32			ns;
95fd558d186df2c13a22455373858bae634a4795afJames Chapman	u16			has_seq;
96fd558d186df2c13a22455373858bae634a4795afJames Chapman	u16			length;
97fd558d186df2c13a22455373858bae634a4795afJames Chapman	unsigned long		expires;
98fd558d186df2c13a22455373858bae634a4795afJames Chapman};
99fd558d186df2c13a22455373858bae634a4795afJames Chapman
100fd558d186df2c13a22455373858bae634a4795afJames Chapman#define L2TP_SKB_CB(skb)	((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101fd558d186df2c13a22455373858bae634a4795afJames Chapman
102fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic atomic_t l2tp_tunnel_count;
103fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic atomic_t l2tp_session_count;
104f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkinstatic struct workqueue_struct *l2tp_wq;
105fd558d186df2c13a22455373858bae634a4795afJames Chapman
106fd558d186df2c13a22455373858bae634a4795afJames Chapman/* per-net private data for this module */
107fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic unsigned int l2tp_net_id;
108fd558d186df2c13a22455373858bae634a4795afJames Chapmanstruct l2tp_net {
109fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct list_head l2tp_tunnel_list;
110e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	spinlock_t l2tp_tunnel_list_lock;
111f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
112e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	spinlock_t l2tp_session_hlist_lock;
113fd558d186df2c13a22455373858bae634a4795afJames Chapman};
114fd558d186df2c13a22455373858bae634a4795afJames Chapman
115fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic void l2tp_session_set_header_len(struct l2tp_session *session, int version);
116fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
117fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger
118fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic inline struct l2tp_net *l2tp_pernet(struct net *net)
119fd558d186df2c13a22455373858bae634a4795afJames Chapman{
120fd558d186df2c13a22455373858bae634a4795afJames Chapman	BUG_ON(!net);
121fd558d186df2c13a22455373858bae634a4795afJames Chapman
122fd558d186df2c13a22455373858bae634a4795afJames Chapman	return net_generic(net, l2tp_net_id);
123fd558d186df2c13a22455373858bae634a4795afJames Chapman}
124fd558d186df2c13a22455373858bae634a4795afJames Chapman
125fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger/* Tunnel reference counts. Incremented per session that is added to
126fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger * the tunnel.
127fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger */
128fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
129fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger{
130fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger	atomic_inc(&tunnel->ref_count);
131fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger}
132fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger
133fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
134fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger{
135fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger	if (atomic_dec_and_test(&tunnel->ref_count))
136fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger		l2tp_tunnel_free(tunnel);
137fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger}
138fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger#ifdef L2TP_REFCNT_DEBUG
139a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches#define l2tp_tunnel_inc_refcount(_t)					\
140a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perchesdo {									\
141a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n",	\
142a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		 __func__, __LINE__, (_t)->name,			\
143a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		 atomic_read(&_t->ref_count));				\
144a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	l2tp_tunnel_inc_refcount_1(_t);					\
145a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches} while (0)
146a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches#define l2tp_tunnel_dec_refcount(_t)
147a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perchesdo {									\
148a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n",	\
149a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		 __func__, __LINE__, (_t)->name,			\
150a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		 atomic_read(&_t->ref_count));				\
151a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	l2tp_tunnel_dec_refcount_1(_t);					\
152a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches} while (0)
153fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger#else
154fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
155fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
156fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger#endif
157fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger
158f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman/* Session hash global list for L2TPv3.
159f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * The session_id SHOULD be random according to RFC3931, but several
160f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * L2TP implementations use incrementing session_ids.  So we do a real
161f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * hash on the session_id, rather than a simple bitmask.
162f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman */
163f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapmanstatic inline struct hlist_head *
164f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapmanl2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
165f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman{
166f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
167f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
168f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman}
169f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
17080d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin/* Lookup the tunnel socket, possibly involving the fs code if the socket is
17180d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin * owned by userspace.  A struct sock returned from this function must be
17280d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin * released using l2tp_tunnel_sock_put once you're done with it.
17380d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin */
17480d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkinstruct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
17580d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin{
17680d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	int err = 0;
17780d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	struct socket *sock = NULL;
17880d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	struct sock *sk = NULL;
17980d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin
18080d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	if (!tunnel)
18180d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		goto out;
18280d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin
18380d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	if (tunnel->fd >= 0) {
18480d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		/* Socket is owned by userspace, who might be in the process
18580d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		 * of closing it.  Look the socket up using the fd to ensure
18680d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		 * consistency.
18780d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		 */
18880d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		sock = sockfd_lookup(tunnel->fd, &err);
18980d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		if (sock)
19080d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin			sk = sock->sk;
19180d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	} else {
19280d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		/* Socket is owned by kernelspace */
19380d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		sk = tunnel->sock;
1948abbbe8ff572fd84d1b98eb9acf30611a97cf72eTom Parkin		sock_hold(sk);
19580d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	}
19680d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin
19780d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkinout:
19880d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	return sk;
19980d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin}
20080d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom ParkinEXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup);
20180d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin
20280d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
20380d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkinvoid l2tp_tunnel_sock_put(struct sock *sk)
20480d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin{
20580d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
20680d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	if (tunnel) {
20780d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		if (tunnel->fd >= 0) {
20880d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin			/* Socket is owned by userspace */
20980d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin			sockfd_put(sk->sk_socket);
21080d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		}
21180d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin		sock_put(sk);
21280d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	}
2138abbbe8ff572fd84d1b98eb9acf30611a97cf72eTom Parkin	sock_put(sk);
21480d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin}
21580d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom ParkinEXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put);
21680d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin
217f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman/* Lookup a session by id in the global session list
218f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman */
219f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapmanstatic struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
220f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman{
221f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	struct l2tp_net *pn = l2tp_pernet(net);
222f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	struct hlist_head *session_list =
223f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		l2tp_session_id_hash_2(pn, session_id);
224f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	struct l2tp_session *session;
225f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
226e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	rcu_read_lock_bh();
227b67bfe0d42cac56c512dd5da4b1b347a23f4b70aSasha Levin	hlist_for_each_entry_rcu(session, session_list, global_hlist) {
228f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (session->session_id == session_id) {
229e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			rcu_read_unlock_bh();
230f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			return session;
231f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		}
232f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
233e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	rcu_read_unlock_bh();
234f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
235f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	return NULL;
236f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman}
237f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
238fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Session hash list.
239fd558d186df2c13a22455373858bae634a4795afJames Chapman * The session_id SHOULD be random according to RFC2661, but several
240fd558d186df2c13a22455373858bae634a4795afJames Chapman * L2TP implementations (Cisco and Microsoft) use incrementing
241fd558d186df2c13a22455373858bae634a4795afJames Chapman * session_ids.  So we do a real hash on the session_id, rather than a
242fd558d186df2c13a22455373858bae634a4795afJames Chapman * simple bitmask.
243fd558d186df2c13a22455373858bae634a4795afJames Chapman */
244fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic inline struct hlist_head *
245fd558d186df2c13a22455373858bae634a4795afJames Chapmanl2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
246fd558d186df2c13a22455373858bae634a4795afJames Chapman{
247fd558d186df2c13a22455373858bae634a4795afJames Chapman	return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
248fd558d186df2c13a22455373858bae634a4795afJames Chapman}
249fd558d186df2c13a22455373858bae634a4795afJames Chapman
250fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Lookup a session by id
251fd558d186df2c13a22455373858bae634a4795afJames Chapman */
252f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapmanstruct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
253fd558d186df2c13a22455373858bae634a4795afJames Chapman{
254f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	struct hlist_head *session_list;
255fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_session *session;
256fd558d186df2c13a22455373858bae634a4795afJames Chapman
257f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* In L2TPv3, session_ids are unique over all tunnels and we
258f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	 * sometimes need to look them up before we know the
259f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	 * tunnel.
260f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	 */
261f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (tunnel == NULL)
262f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		return l2tp_session_find_2(net, session_id);
263f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
264f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	session_list = l2tp_session_id_hash(tunnel, session_id);
265fd558d186df2c13a22455373858bae634a4795afJames Chapman	read_lock_bh(&tunnel->hlist_lock);
266b67bfe0d42cac56c512dd5da4b1b347a23f4b70aSasha Levin	hlist_for_each_entry(session, session_list, hlist) {
267fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (session->session_id == session_id) {
268fd558d186df2c13a22455373858bae634a4795afJames Chapman			read_unlock_bh(&tunnel->hlist_lock);
269fd558d186df2c13a22455373858bae634a4795afJames Chapman			return session;
270fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
271fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
272fd558d186df2c13a22455373858bae634a4795afJames Chapman	read_unlock_bh(&tunnel->hlist_lock);
273fd558d186df2c13a22455373858bae634a4795afJames Chapman
274fd558d186df2c13a22455373858bae634a4795afJames Chapman	return NULL;
275fd558d186df2c13a22455373858bae634a4795afJames Chapman}
276fd558d186df2c13a22455373858bae634a4795afJames ChapmanEXPORT_SYMBOL_GPL(l2tp_session_find);
277fd558d186df2c13a22455373858bae634a4795afJames Chapman
278fd558d186df2c13a22455373858bae634a4795afJames Chapmanstruct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
279fd558d186df2c13a22455373858bae634a4795afJames Chapman{
280fd558d186df2c13a22455373858bae634a4795afJames Chapman	int hash;
281fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_session *session;
282fd558d186df2c13a22455373858bae634a4795afJames Chapman	int count = 0;
283fd558d186df2c13a22455373858bae634a4795afJames Chapman
284fd558d186df2c13a22455373858bae634a4795afJames Chapman	read_lock_bh(&tunnel->hlist_lock);
285fd558d186df2c13a22455373858bae634a4795afJames Chapman	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
286b67bfe0d42cac56c512dd5da4b1b347a23f4b70aSasha Levin		hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
287fd558d186df2c13a22455373858bae634a4795afJames Chapman			if (++count > nth) {
288fd558d186df2c13a22455373858bae634a4795afJames Chapman				read_unlock_bh(&tunnel->hlist_lock);
289fd558d186df2c13a22455373858bae634a4795afJames Chapman				return session;
290fd558d186df2c13a22455373858bae634a4795afJames Chapman			}
291fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
292fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
293fd558d186df2c13a22455373858bae634a4795afJames Chapman
294fd558d186df2c13a22455373858bae634a4795afJames Chapman	read_unlock_bh(&tunnel->hlist_lock);
295fd558d186df2c13a22455373858bae634a4795afJames Chapman
296fd558d186df2c13a22455373858bae634a4795afJames Chapman	return NULL;
297fd558d186df2c13a22455373858bae634a4795afJames Chapman}
298fd558d186df2c13a22455373858bae634a4795afJames ChapmanEXPORT_SYMBOL_GPL(l2tp_session_find_nth);
299fd558d186df2c13a22455373858bae634a4795afJames Chapman
300309795f4bec2d69cd507a631f82065c2198a0825James Chapman/* Lookup a session by interface name.
301309795f4bec2d69cd507a631f82065c2198a0825James Chapman * This is very inefficient but is only used by management interfaces.
302309795f4bec2d69cd507a631f82065c2198a0825James Chapman */
303309795f4bec2d69cd507a631f82065c2198a0825James Chapmanstruct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
304309795f4bec2d69cd507a631f82065c2198a0825James Chapman{
305309795f4bec2d69cd507a631f82065c2198a0825James Chapman	struct l2tp_net *pn = l2tp_pernet(net);
306309795f4bec2d69cd507a631f82065c2198a0825James Chapman	int hash;
307309795f4bec2d69cd507a631f82065c2198a0825James Chapman	struct l2tp_session *session;
308309795f4bec2d69cd507a631f82065c2198a0825James Chapman
309e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	rcu_read_lock_bh();
310309795f4bec2d69cd507a631f82065c2198a0825James Chapman	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
311b67bfe0d42cac56c512dd5da4b1b347a23f4b70aSasha Levin		hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
312309795f4bec2d69cd507a631f82065c2198a0825James Chapman			if (!strcmp(session->ifname, ifname)) {
313e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman				rcu_read_unlock_bh();
314309795f4bec2d69cd507a631f82065c2198a0825James Chapman				return session;
315309795f4bec2d69cd507a631f82065c2198a0825James Chapman			}
316309795f4bec2d69cd507a631f82065c2198a0825James Chapman		}
317309795f4bec2d69cd507a631f82065c2198a0825James Chapman	}
318309795f4bec2d69cd507a631f82065c2198a0825James Chapman
319e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	rcu_read_unlock_bh();
320309795f4bec2d69cd507a631f82065c2198a0825James Chapman
321309795f4bec2d69cd507a631f82065c2198a0825James Chapman	return NULL;
322309795f4bec2d69cd507a631f82065c2198a0825James Chapman}
323309795f4bec2d69cd507a631f82065c2198a0825James ChapmanEXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
324309795f4bec2d69cd507a631f82065c2198a0825James Chapman
325fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Lookup a tunnel by id
326fd558d186df2c13a22455373858bae634a4795afJames Chapman */
327fd558d186df2c13a22455373858bae634a4795afJames Chapmanstruct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
328fd558d186df2c13a22455373858bae634a4795afJames Chapman{
329fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_tunnel *tunnel;
330fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_net *pn = l2tp_pernet(net);
331fd558d186df2c13a22455373858bae634a4795afJames Chapman
332e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	rcu_read_lock_bh();
333e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
334fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (tunnel->tunnel_id == tunnel_id) {
335e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			rcu_read_unlock_bh();
336fd558d186df2c13a22455373858bae634a4795afJames Chapman			return tunnel;
337fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
338fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
339e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	rcu_read_unlock_bh();
340fd558d186df2c13a22455373858bae634a4795afJames Chapman
341fd558d186df2c13a22455373858bae634a4795afJames Chapman	return NULL;
342fd558d186df2c13a22455373858bae634a4795afJames Chapman}
343fd558d186df2c13a22455373858bae634a4795afJames ChapmanEXPORT_SYMBOL_GPL(l2tp_tunnel_find);
344fd558d186df2c13a22455373858bae634a4795afJames Chapman
345fd558d186df2c13a22455373858bae634a4795afJames Chapmanstruct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
346fd558d186df2c13a22455373858bae634a4795afJames Chapman{
347fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_net *pn = l2tp_pernet(net);
348fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_tunnel *tunnel;
349fd558d186df2c13a22455373858bae634a4795afJames Chapman	int count = 0;
350fd558d186df2c13a22455373858bae634a4795afJames Chapman
351e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	rcu_read_lock_bh();
352e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
353fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (++count > nth) {
354e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			rcu_read_unlock_bh();
355fd558d186df2c13a22455373858bae634a4795afJames Chapman			return tunnel;
356fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
357fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
358fd558d186df2c13a22455373858bae634a4795afJames Chapman
359e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	rcu_read_unlock_bh();
360fd558d186df2c13a22455373858bae634a4795afJames Chapman
361fd558d186df2c13a22455373858bae634a4795afJames Chapman	return NULL;
362fd558d186df2c13a22455373858bae634a4795afJames Chapman}
363fd558d186df2c13a22455373858bae634a4795afJames ChapmanEXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
364fd558d186df2c13a22455373858bae634a4795afJames Chapman
365fd558d186df2c13a22455373858bae634a4795afJames Chapman/*****************************************************************************
366fd558d186df2c13a22455373858bae634a4795afJames Chapman * Receive data handling
367fd558d186df2c13a22455373858bae634a4795afJames Chapman *****************************************************************************/
368fd558d186df2c13a22455373858bae634a4795afJames Chapman
369fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Queue a skb in order. We come here only if the skb has an L2TP sequence
370fd558d186df2c13a22455373858bae634a4795afJames Chapman * number.
371fd558d186df2c13a22455373858bae634a4795afJames Chapman */
372fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
373fd558d186df2c13a22455373858bae634a4795afJames Chapman{
374fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct sk_buff *skbp;
375fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct sk_buff *tmp;
376f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	u32 ns = L2TP_SKB_CB(skb)->ns;
377fd558d186df2c13a22455373858bae634a4795afJames Chapman
378fd558d186df2c13a22455373858bae634a4795afJames Chapman	spin_lock_bh(&session->reorder_q.lock);
379fd558d186df2c13a22455373858bae634a4795afJames Chapman	skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
380fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (L2TP_SKB_CB(skbp)->ns > ns) {
381fd558d186df2c13a22455373858bae634a4795afJames Chapman			__skb_queue_before(&session->reorder_q, skbp, skb);
382a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_dbg(session, L2TP_MSG_SEQ,
383a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
384a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 session->name, ns, L2TP_SKB_CB(skbp)->ns,
385a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 skb_queue_len(&session->reorder_q));
3867b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin			atomic_long_inc(&session->stats.rx_oos_packets);
387fd558d186df2c13a22455373858bae634a4795afJames Chapman			goto out;
388fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
389fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
390fd558d186df2c13a22455373858bae634a4795afJames Chapman
391fd558d186df2c13a22455373858bae634a4795afJames Chapman	__skb_queue_tail(&session->reorder_q, skb);
392fd558d186df2c13a22455373858bae634a4795afJames Chapman
393fd558d186df2c13a22455373858bae634a4795afJames Chapmanout:
394fd558d186df2c13a22455373858bae634a4795afJames Chapman	spin_unlock_bh(&session->reorder_q.lock);
395fd558d186df2c13a22455373858bae634a4795afJames Chapman}
396fd558d186df2c13a22455373858bae634a4795afJames Chapman
397fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Dequeue a single skb.
398fd558d186df2c13a22455373858bae634a4795afJames Chapman */
399fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
400fd558d186df2c13a22455373858bae634a4795afJames Chapman{
401fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_tunnel *tunnel = session->tunnel;
402fd558d186df2c13a22455373858bae634a4795afJames Chapman	int length = L2TP_SKB_CB(skb)->length;
403fd558d186df2c13a22455373858bae634a4795afJames Chapman
404fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* We're about to requeue the skb, so return resources
405fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * to its current owner (a socket receive buffer).
406fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
407fd558d186df2c13a22455373858bae634a4795afJames Chapman	skb_orphan(skb);
408fd558d186df2c13a22455373858bae634a4795afJames Chapman
4097b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin	atomic_long_inc(&tunnel->stats.rx_packets);
4107b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin	atomic_long_add(length, &tunnel->stats.rx_bytes);
4117b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin	atomic_long_inc(&session->stats.rx_packets);
4127b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin	atomic_long_add(length, &session->stats.rx_bytes);
413fd558d186df2c13a22455373858bae634a4795afJames Chapman
414fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (L2TP_SKB_CB(skb)->has_seq) {
415fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* Bump our Nr */
416fd558d186df2c13a22455373858bae634a4795afJames Chapman		session->nr++;
417f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (tunnel->version == L2TP_HDR_VER_2)
418f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->nr &= 0xffff;
419f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		else
420f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->nr &= 0xffffff;
421f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
422a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
423a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			 session->name, session->nr);
424fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
425fd558d186df2c13a22455373858bae634a4795afJames Chapman
426fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* call private receive handler */
427fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session->recv_skb != NULL)
428fd558d186df2c13a22455373858bae634a4795afJames Chapman		(*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
429fd558d186df2c13a22455373858bae634a4795afJames Chapman	else
430fd558d186df2c13a22455373858bae634a4795afJames Chapman		kfree_skb(skb);
431fd558d186df2c13a22455373858bae634a4795afJames Chapman
432fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session->deref)
433fd558d186df2c13a22455373858bae634a4795afJames Chapman		(*session->deref)(session);
434fd558d186df2c13a22455373858bae634a4795afJames Chapman}
435fd558d186df2c13a22455373858bae634a4795afJames Chapman
436fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Dequeue skbs from the session's reorder_q, subject to packet order.
437fd558d186df2c13a22455373858bae634a4795afJames Chapman * Skbs that have been in the queue for too long are simply discarded.
438fd558d186df2c13a22455373858bae634a4795afJames Chapman */
439fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic void l2tp_recv_dequeue(struct l2tp_session *session)
440fd558d186df2c13a22455373858bae634a4795afJames Chapman{
441fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct sk_buff *skb;
442fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct sk_buff *tmp;
443fd558d186df2c13a22455373858bae634a4795afJames Chapman
444fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* If the pkt at the head of the queue has the nr that we
445fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * expect to send up next, dequeue it and any other
446fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * in-sequence packets behind it.
447fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
448e2e210c0238eb7073e07af503ae743fa53977120Eric Dumazetstart:
449fd558d186df2c13a22455373858bae634a4795afJames Chapman	spin_lock_bh(&session->reorder_q.lock);
450fd558d186df2c13a22455373858bae634a4795afJames Chapman	skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
451fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
4527b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin			atomic_long_inc(&session->stats.rx_seq_discards);
4537b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin			atomic_long_inc(&session->stats.rx_errors);
454a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_dbg(session, L2TP_MSG_SEQ,
455a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
456a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 session->name, L2TP_SKB_CB(skb)->ns,
457a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 L2TP_SKB_CB(skb)->length, session->nr,
458a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 skb_queue_len(&session->reorder_q));
45938d40b3f4e223336422b7e87cb483e758ef87e3aJames Chapman			session->reorder_skip = 1;
460fd558d186df2c13a22455373858bae634a4795afJames Chapman			__skb_unlink(skb, &session->reorder_q);
461fd558d186df2c13a22455373858bae634a4795afJames Chapman			kfree_skb(skb);
462fd558d186df2c13a22455373858bae634a4795afJames Chapman			if (session->deref)
463fd558d186df2c13a22455373858bae634a4795afJames Chapman				(*session->deref)(session);
464fd558d186df2c13a22455373858bae634a4795afJames Chapman			continue;
465fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
466fd558d186df2c13a22455373858bae634a4795afJames Chapman
467fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (L2TP_SKB_CB(skb)->has_seq) {
46838d40b3f4e223336422b7e87cb483e758ef87e3aJames Chapman			if (session->reorder_skip) {
469a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				l2tp_dbg(session, L2TP_MSG_SEQ,
470a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 "%s: advancing nr to next pkt: %u -> %u",
471a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 session->name, session->nr,
472a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 L2TP_SKB_CB(skb)->ns);
47338d40b3f4e223336422b7e87cb483e758ef87e3aJames Chapman				session->reorder_skip = 0;
47438d40b3f4e223336422b7e87cb483e758ef87e3aJames Chapman				session->nr = L2TP_SKB_CB(skb)->ns;
47538d40b3f4e223336422b7e87cb483e758ef87e3aJames Chapman			}
476fd558d186df2c13a22455373858bae634a4795afJames Chapman			if (L2TP_SKB_CB(skb)->ns != session->nr) {
477a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				l2tp_dbg(session, L2TP_MSG_SEQ,
478a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
479a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 session->name, L2TP_SKB_CB(skb)->ns,
480a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 L2TP_SKB_CB(skb)->length, session->nr,
481a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 skb_queue_len(&session->reorder_q));
482fd558d186df2c13a22455373858bae634a4795afJames Chapman				goto out;
483fd558d186df2c13a22455373858bae634a4795afJames Chapman			}
484fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
485fd558d186df2c13a22455373858bae634a4795afJames Chapman		__skb_unlink(skb, &session->reorder_q);
486fd558d186df2c13a22455373858bae634a4795afJames Chapman
487fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* Process the skb. We release the queue lock while we
488fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * do so to let other contexts process the queue.
489fd558d186df2c13a22455373858bae634a4795afJames Chapman		 */
490fd558d186df2c13a22455373858bae634a4795afJames Chapman		spin_unlock_bh(&session->reorder_q.lock);
491fd558d186df2c13a22455373858bae634a4795afJames Chapman		l2tp_recv_dequeue_skb(session, skb);
492e2e210c0238eb7073e07af503ae743fa53977120Eric Dumazet		goto start;
493fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
494fd558d186df2c13a22455373858bae634a4795afJames Chapman
495fd558d186df2c13a22455373858bae634a4795afJames Chapmanout:
496fd558d186df2c13a22455373858bae634a4795afJames Chapman	spin_unlock_bh(&session->reorder_q.lock);
497fd558d186df2c13a22455373858bae634a4795afJames Chapman}
498fd558d186df2c13a22455373858bae634a4795afJames Chapman
499fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic inline int l2tp_verify_udp_checksum(struct sock *sk,
500fd558d186df2c13a22455373858bae634a4795afJames Chapman					   struct sk_buff *skb)
501fd558d186df2c13a22455373858bae634a4795afJames Chapman{
502fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct udphdr *uh = udp_hdr(skb);
503fd558d186df2c13a22455373858bae634a4795afJames Chapman	u16 ulen = ntohs(uh->len);
504fd558d186df2c13a22455373858bae634a4795afJames Chapman	__wsum psum;
505fd558d186df2c13a22455373858bae634a4795afJames Chapman
506d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	if (sk->sk_no_check || skb_csum_unnecessary(skb))
507fd558d186df2c13a22455373858bae634a4795afJames Chapman		return 0;
508fd558d186df2c13a22455373858bae634a4795afJames Chapman
509d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#if IS_ENABLED(CONFIG_IPV6)
510d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	if (sk->sk_family == PF_INET6) {
511d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		if (!uh->check) {
512d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
513d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			return 1;
514d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		}
515d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
516d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		    !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
517d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise				     &ipv6_hdr(skb)->daddr, ulen,
518d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise				     IPPROTO_UDP, skb->csum)) {
519d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			skb->ip_summed = CHECKSUM_UNNECESSARY;
520d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			return 0;
521d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		}
522d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
523d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise							 &ipv6_hdr(skb)->daddr,
524d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise							 skb->len, IPPROTO_UDP,
525d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise							 0));
526d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	} else
527d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#endif
528d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	{
529d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		struct inet_sock *inet;
530d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		if (!uh->check)
531d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			return 0;
532d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		inet = inet_sk(sk);
533d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
534d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise					  ulen, IPPROTO_UDP, 0);
535d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise
536d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
537d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		    !csum_fold(csum_add(psum, skb->csum)))
538d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			return 0;
539d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		skb->csum = psum;
540d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	}
541fd558d186df2c13a22455373858bae634a4795afJames Chapman
542fd558d186df2c13a22455373858bae634a4795afJames Chapman	return __skb_checksum_complete(skb);
543fd558d186df2c13a22455373858bae634a4795afJames Chapman}
544fd558d186df2c13a22455373858bae634a4795afJames Chapman
545f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman/* Do receive processing of L2TP data frames. We handle both L2TPv2
546f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * and L2TPv3 data frames here.
547f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
548f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * L2TPv2 Data Message Header
549f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
550f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *  0                   1                   2                   3
551f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames 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
552f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
553f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |T|L|x|x|S|x|O|P|x|x|x|x|  Ver  |          Length (opt)         |
554f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
555f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |           Tunnel ID           |           Session ID          |
556f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
557f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |             Ns (opt)          |             Nr (opt)          |
558f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
559f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |      Offset Size (opt)        |    Offset pad... (opt)
560f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
561f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
562f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * Data frames are marked by T=0. All other fields are the same as
563f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * those in L2TP control frames.
564f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
565f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * L2TPv3 Data Message Header
566f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
567f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
568f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |                      L2TP Session Header                      |
569f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |                      L2-Specific Sublayer                     |
571f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
572f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |                        Tunnel Payload                      ...
573f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
574f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
575f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * L2TPv3 Session Header Over IP
576f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
577f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *  0                   1                   2                   3
578f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames 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
579f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
580f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |                           Session ID                          |
581f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
582f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |               Cookie (optional, maximum 64 bits)...
583f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
584f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *                                                                 |
585f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
587f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * L2TPv3 L2-Specific Sublayer Format
588f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
589f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *  0                   1                   2                   3
590f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames 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
591f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * |x|S|x|x|x|x|x|x|              Sequence Number                  |
593f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
594f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
595f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * Cookie value, sublayer format and offset (pad) are negotiated with
596f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * the peer when the session is set up. Unlike L2TPv2, we do not need
597f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * to parse the packet header to determine if optional fields are
598f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * present.
599f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman *
600f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * Caller must already have parsed the frame and determined that it is
601f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * a data (not control) frame before coming here. Fields up to the
602f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * session-id have already been parsed and ptr points to the data
603f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * after the session-id.
604fd558d186df2c13a22455373858bae634a4795afJames Chapman */
605f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapmanvoid l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
606f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
607f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		      int length, int (*payload_hook)(struct sk_buff *skb))
608fd558d186df2c13a22455373858bae634a4795afJames Chapman{
609f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	struct l2tp_tunnel *tunnel = session->tunnel;
610fd558d186df2c13a22455373858bae634a4795afJames Chapman	int offset;
611f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	u32 ns, nr;
612fd558d186df2c13a22455373858bae634a4795afJames Chapman
613fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* The ref count is increased since we now hold a pointer to
614fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * the session. Take care to decrement the refcnt when exiting
615fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * this function from now on...
616fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
617fd558d186df2c13a22455373858bae634a4795afJames Chapman	l2tp_session_inc_refcount(session);
618fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session->ref)
619fd558d186df2c13a22455373858bae634a4795afJames Chapman		(*session->ref)(session);
620fd558d186df2c13a22455373858bae634a4795afJames Chapman
621f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Parse and check optional cookie */
622f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (session->peer_cookie_len > 0) {
623f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
624a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_info(tunnel, L2TP_MSG_DATA,
625a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  "%s: cookie mismatch (%u/%u). Discarding.\n",
626a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  tunnel->name, tunnel->tunnel_id,
627a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  session->session_id);
6287b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin			atomic_long_inc(&session->stats.rx_cookie_discards);
629f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			goto discard;
630f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		}
631f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		ptr += session->peer_cookie_len;
632f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
633f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
634fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Handle the optional sequence numbers. Sequence numbers are
635fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * in different places for L2TPv2 and L2TPv3.
636fd558d186df2c13a22455373858bae634a4795afJames Chapman	 *
637fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * If we are the LAC, enable/disable sequence numbers under
638fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * the control of the LNS.  If no sequence numbers present but
639fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * we were expecting them, discard frame.
640fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
641fd558d186df2c13a22455373858bae634a4795afJames Chapman	ns = nr = 0;
642fd558d186df2c13a22455373858bae634a4795afJames Chapman	L2TP_SKB_CB(skb)->has_seq = 0;
643f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (tunnel->version == L2TP_HDR_VER_2) {
644f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (hdrflags & L2TP_HDRFLAG_S) {
645f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			ns = ntohs(*(__be16 *) ptr);
646f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			ptr += 2;
647f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			nr = ntohs(*(__be16 *) ptr);
648f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			ptr += 2;
649fd558d186df2c13a22455373858bae634a4795afJames Chapman
650f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			/* Store L2TP info in the skb */
651f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			L2TP_SKB_CB(skb)->ns = ns;
652f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			L2TP_SKB_CB(skb)->has_seq = 1;
653fd558d186df2c13a22455373858bae634a4795afJames Chapman
654a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_dbg(session, L2TP_MSG_SEQ,
655a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
656a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 session->name, ns, nr, session->nr);
657f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		}
658f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	} else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
659f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		u32 l2h = ntohl(*(__be32 *) ptr);
660f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
661f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (l2h & 0x40000000) {
662f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			ns = l2h & 0x00ffffff;
663f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
664f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			/* Store L2TP info in the skb */
665f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			L2TP_SKB_CB(skb)->ns = ns;
666f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			L2TP_SKB_CB(skb)->has_seq = 1;
667f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
668a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_dbg(session, L2TP_MSG_SEQ,
669a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 "%s: recv data ns=%u, session nr=%u\n",
670a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				 session->name, ns, session->nr);
671f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		}
672fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
673fd558d186df2c13a22455373858bae634a4795afJames Chapman
674f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Advance past L2-specific header, if present */
675f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	ptr += session->l2specific_len;
676f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
677fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (L2TP_SKB_CB(skb)->has_seq) {
678fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* Received a packet with sequence numbers. If we're the LNS,
679fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * check if we sre sending sequence numbers and if not,
680fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * configure it so.
681fd558d186df2c13a22455373858bae634a4795afJames Chapman		 */
682fd558d186df2c13a22455373858bae634a4795afJames Chapman		if ((!session->lns_mode) && (!session->send_seq)) {
683a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_info(session, L2TP_MSG_SEQ,
684a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  "%s: requested to enable seq numbers by LNS\n",
685a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  session->name);
686fd558d186df2c13a22455373858bae634a4795afJames Chapman			session->send_seq = -1;
687f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			l2tp_session_set_header_len(session, tunnel->version);
688fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
689fd558d186df2c13a22455373858bae634a4795afJames Chapman	} else {
690fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* No sequence numbers.
691fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * If user has configured mandatory sequence numbers, discard.
692fd558d186df2c13a22455373858bae634a4795afJames Chapman		 */
693fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (session->recv_seq) {
694a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_warn(session, L2TP_MSG_SEQ,
695a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  "%s: recv data has no seq numbers when required. Discarding.\n",
696a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  session->name);
6977b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin			atomic_long_inc(&session->stats.rx_seq_discards);
698fd558d186df2c13a22455373858bae634a4795afJames Chapman			goto discard;
699fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
700fd558d186df2c13a22455373858bae634a4795afJames Chapman
701fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* If we're the LAC and we're sending sequence numbers, the
702fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * LNS has requested that we no longer send sequence numbers.
703fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * If we're the LNS and we're sending sequence numbers, the
704fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * LAC is broken. Discard the frame.
705fd558d186df2c13a22455373858bae634a4795afJames Chapman		 */
706fd558d186df2c13a22455373858bae634a4795afJames Chapman		if ((!session->lns_mode) && (session->send_seq)) {
707a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_info(session, L2TP_MSG_SEQ,
708a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  "%s: requested to disable seq numbers by LNS\n",
709a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  session->name);
710fd558d186df2c13a22455373858bae634a4795afJames Chapman			session->send_seq = 0;
711f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			l2tp_session_set_header_len(session, tunnel->version);
712fd558d186df2c13a22455373858bae634a4795afJames Chapman		} else if (session->send_seq) {
713a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_warn(session, L2TP_MSG_SEQ,
714a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  "%s: recv data has no seq numbers when required. Discarding.\n",
715a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  session->name);
7167b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin			atomic_long_inc(&session->stats.rx_seq_discards);
717fd558d186df2c13a22455373858bae634a4795afJames Chapman			goto discard;
718fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
719fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
720fd558d186df2c13a22455373858bae634a4795afJames Chapman
721f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Session data offset is handled differently for L2TPv2 and
722f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
723f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	 * the header. For L2TPv3, the offset is negotiated using AVPs
724f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	 * in the session setup control protocol.
725f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	 */
726f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (tunnel->version == L2TP_HDR_VER_2) {
727f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		/* If offset bit set, skip it. */
728f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (hdrflags & L2TP_HDRFLAG_O) {
729f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			offset = ntohs(*(__be16 *)ptr);
730f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			ptr += 2 + offset;
731f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		}
732f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	} else
733f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		ptr += session->offset;
734fd558d186df2c13a22455373858bae634a4795afJames Chapman
735fd558d186df2c13a22455373858bae634a4795afJames Chapman	offset = ptr - optr;
736fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (!pskb_may_pull(skb, offset))
737fd558d186df2c13a22455373858bae634a4795afJames Chapman		goto discard;
738fd558d186df2c13a22455373858bae634a4795afJames Chapman
739fd558d186df2c13a22455373858bae634a4795afJames Chapman	__skb_pull(skb, offset);
740fd558d186df2c13a22455373858bae634a4795afJames Chapman
741fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* If caller wants to process the payload before we queue the
742fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * packet, do so now.
743fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
744fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (payload_hook)
745fd558d186df2c13a22455373858bae634a4795afJames Chapman		if ((*payload_hook)(skb))
746fd558d186df2c13a22455373858bae634a4795afJames Chapman			goto discard;
747fd558d186df2c13a22455373858bae634a4795afJames Chapman
748fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Prepare skb for adding to the session's reorder_q.  Hold
749fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * packets for max reorder_timeout or 1 second if not
750fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * reordering.
751fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
752fd558d186df2c13a22455373858bae634a4795afJames Chapman	L2TP_SKB_CB(skb)->length = length;
753fd558d186df2c13a22455373858bae634a4795afJames Chapman	L2TP_SKB_CB(skb)->expires = jiffies +
754fd558d186df2c13a22455373858bae634a4795afJames Chapman		(session->reorder_timeout ? session->reorder_timeout : HZ);
755fd558d186df2c13a22455373858bae634a4795afJames Chapman
756fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Add packet to the session's receive queue. Reordering is done here, if
757fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * enabled. Saved L2TP protocol info is stored in skb->sb[].
758fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
759fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (L2TP_SKB_CB(skb)->has_seq) {
760fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (session->reorder_timeout != 0) {
761fd558d186df2c13a22455373858bae634a4795afJames Chapman			/* Packet reordering enabled. Add skb to session's
762fd558d186df2c13a22455373858bae634a4795afJames Chapman			 * reorder queue, in order of ns.
763fd558d186df2c13a22455373858bae634a4795afJames Chapman			 */
764fd558d186df2c13a22455373858bae634a4795afJames Chapman			l2tp_recv_queue_skb(session, skb);
765fd558d186df2c13a22455373858bae634a4795afJames Chapman		} else {
766fd558d186df2c13a22455373858bae634a4795afJames Chapman			/* Packet reordering disabled. Discard out-of-sequence
767fd558d186df2c13a22455373858bae634a4795afJames Chapman			 * packets
768fd558d186df2c13a22455373858bae634a4795afJames Chapman			 */
769fd558d186df2c13a22455373858bae634a4795afJames Chapman			if (L2TP_SKB_CB(skb)->ns != session->nr) {
7707b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin				atomic_long_inc(&session->stats.rx_seq_discards);
771a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				l2tp_dbg(session, L2TP_MSG_SEQ,
772a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
773a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 session->name, L2TP_SKB_CB(skb)->ns,
774a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 L2TP_SKB_CB(skb)->length, session->nr,
775a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 skb_queue_len(&session->reorder_q));
776fd558d186df2c13a22455373858bae634a4795afJames Chapman				goto discard;
777fd558d186df2c13a22455373858bae634a4795afJames Chapman			}
778fd558d186df2c13a22455373858bae634a4795afJames Chapman			skb_queue_tail(&session->reorder_q, skb);
779fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
780fd558d186df2c13a22455373858bae634a4795afJames Chapman	} else {
781fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* No sequence numbers. Add the skb to the tail of the
782fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * reorder queue. This ensures that it will be
783fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * delivered after all previous sequenced skbs.
784fd558d186df2c13a22455373858bae634a4795afJames Chapman		 */
785fd558d186df2c13a22455373858bae634a4795afJames Chapman		skb_queue_tail(&session->reorder_q, skb);
786fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
787fd558d186df2c13a22455373858bae634a4795afJames Chapman
788fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Try to dequeue as many skbs from reorder_q as we can. */
789fd558d186df2c13a22455373858bae634a4795afJames Chapman	l2tp_recv_dequeue(session);
790fd558d186df2c13a22455373858bae634a4795afJames Chapman
791fd558d186df2c13a22455373858bae634a4795afJames Chapman	l2tp_session_dec_refcount(session);
792fd558d186df2c13a22455373858bae634a4795afJames Chapman
793f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	return;
794fd558d186df2c13a22455373858bae634a4795afJames Chapman
795fd558d186df2c13a22455373858bae634a4795afJames Chapmandiscard:
7967b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin	atomic_long_inc(&session->stats.rx_errors);
797fd558d186df2c13a22455373858bae634a4795afJames Chapman	kfree_skb(skb);
798fd558d186df2c13a22455373858bae634a4795afJames Chapman
799fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session->deref)
800fd558d186df2c13a22455373858bae634a4795afJames Chapman		(*session->deref)(session);
801fd558d186df2c13a22455373858bae634a4795afJames Chapman
802fd558d186df2c13a22455373858bae634a4795afJames Chapman	l2tp_session_dec_refcount(session);
803f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman}
804f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames ChapmanEXPORT_SYMBOL(l2tp_recv_common);
805f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
80648f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin/* Drop skbs from the session's reorder_q
80748f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin */
80848f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkinint l2tp_session_queue_purge(struct l2tp_session *session)
80948f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin{
81048f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin	struct sk_buff *skb = NULL;
81148f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin	BUG_ON(!session);
81248f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin	BUG_ON(session->magic != L2TP_SESSION_MAGIC);
81348f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin	while ((skb = skb_dequeue(&session->reorder_q))) {
81448f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin		atomic_long_inc(&session->stats.rx_errors);
81548f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin		kfree_skb(skb);
81648f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin		if (session->deref)
81748f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin			(*session->deref)(session);
81848f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin	}
81948f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin	return 0;
82048f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin}
82148f72f92b31431c40279b0fba6c5588e07e67d95Tom ParkinEXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
82248f72f92b31431c40279b0fba6c5588e07e67d95Tom Parkin
823f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
824f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * here. The skb is not on a list when we get here.
825f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * Returns 0 if the packet was a data packet and was successfully passed on.
826f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * Returns 1 if the packet was not a good data packet and could not be
827f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * forwarded.  All such packets are passed up to userspace to deal with.
828f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman */
829fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
830fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemminger			      int (*payload_hook)(struct sk_buff *skb))
831f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman{
832f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	struct l2tp_session *session = NULL;
833f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	unsigned char *ptr, *optr;
834f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	u16 hdrflags;
835f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	u32 tunnel_id, session_id;
836f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	u16 version;
837f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	int length;
838f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
839f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
840f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		goto discard_bad_csum;
841f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
842f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* UDP always verifies the packet length. */
843f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	__skb_pull(skb, sizeof(struct udphdr));
844f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
845f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Short packet? */
846f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
847a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		l2tp_info(tunnel, L2TP_MSG_DATA,
848a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			  "%s: recv short packet (len=%d)\n",
849a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			  tunnel->name, skb->len);
850f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		goto error;
851f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
852f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
853f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Trace packet contents, if enabled */
854f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (tunnel->debug & L2TP_MSG_DATA) {
855f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		length = min(32u, skb->len);
856f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (!pskb_may_pull(skb, length))
857f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			goto error;
858f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
859a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		pr_debug("%s: recv\n", tunnel->name);
860a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
861f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
862f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
863e50e705ca71db82a78a3e13fb7abf6b2171ac9c7Eric Dumazet	/* Point to L2TP header */
864e50e705ca71db82a78a3e13fb7abf6b2171ac9c7Eric Dumazet	optr = ptr = skb->data;
865e50e705ca71db82a78a3e13fb7abf6b2171ac9c7Eric Dumazet
866f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Get L2TP header flags */
867f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	hdrflags = ntohs(*(__be16 *) ptr);
868f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
869f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Check protocol version */
870f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	version = hdrflags & L2TP_HDR_VER_MASK;
871f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (version != tunnel->version) {
872a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		l2tp_info(tunnel, L2TP_MSG_DATA,
873a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			  "%s: recv protocol version mismatch: got %d expected %d\n",
874a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			  tunnel->name, version, tunnel->version);
875f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		goto error;
876f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
877f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
878f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Get length of L2TP packet */
879f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	length = skb->len;
880f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
881f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* If type is control packet, it is handled by userspace. */
882f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (hdrflags & L2TP_HDRFLAG_T) {
883a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		l2tp_dbg(tunnel, L2TP_MSG_DATA,
884a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			 "%s: recv control packet, len=%d\n",
885a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			 tunnel->name, length);
886f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		goto error;
887f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
888f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
889f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Skip flags */
890f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	ptr += 2;
891f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
892f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (tunnel->version == L2TP_HDR_VER_2) {
893f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		/* If length is present, skip it */
894f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (hdrflags & L2TP_HDRFLAG_L)
895f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			ptr += 2;
896f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
897f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		/* Extract tunnel and session ID */
898f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		tunnel_id = ntohs(*(__be16 *) ptr);
899f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		ptr += 2;
900f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		session_id = ntohs(*(__be16 *) ptr);
901f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		ptr += 2;
902f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	} else {
903f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		ptr += 2;	/* skip reserved bits */
904f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		tunnel_id = tunnel->tunnel_id;
905f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		session_id = ntohl(*(__be32 *) ptr);
906f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		ptr += 4;
907f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
908f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
909f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	/* Find the session context */
910f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
911309795f4bec2d69cd507a631f82065c2198a0825James Chapman	if (!session || !session->recv_skb) {
912f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		/* Not found? Pass to userspace to deal with */
913a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		l2tp_info(tunnel, L2TP_MSG_DATA,
914a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			  "%s: no session found (%u/%u). Passing up.\n",
915a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			  tunnel->name, tunnel_id, session_id);
916f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		goto error;
917f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
918f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
919f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
920fd558d186df2c13a22455373858bae634a4795afJames Chapman
921fd558d186df2c13a22455373858bae634a4795afJames Chapman	return 0;
922fd558d186df2c13a22455373858bae634a4795afJames Chapman
923fd558d186df2c13a22455373858bae634a4795afJames Chapmandiscard_bad_csum:
924fd558d186df2c13a22455373858bae634a4795afJames Chapman	LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
925fd558d186df2c13a22455373858bae634a4795afJames Chapman	UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
9267b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin	atomic_long_inc(&tunnel->stats.rx_errors);
927fd558d186df2c13a22455373858bae634a4795afJames Chapman	kfree_skb(skb);
928fd558d186df2c13a22455373858bae634a4795afJames Chapman
929fd558d186df2c13a22455373858bae634a4795afJames Chapman	return 0;
930fd558d186df2c13a22455373858bae634a4795afJames Chapman
931fd558d186df2c13a22455373858bae634a4795afJames Chapmanerror:
932fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Put UDP header back */
933fd558d186df2c13a22455373858bae634a4795afJames Chapman	__skb_push(skb, sizeof(struct udphdr));
934fd558d186df2c13a22455373858bae634a4795afJames Chapman
935fd558d186df2c13a22455373858bae634a4795afJames Chapman	return 1;
936fd558d186df2c13a22455373858bae634a4795afJames Chapman}
937fd558d186df2c13a22455373858bae634a4795afJames Chapman
938fd558d186df2c13a22455373858bae634a4795afJames Chapman/* UDP encapsulation receive handler. See net/ipv4/udp.c.
939fd558d186df2c13a22455373858bae634a4795afJames Chapman * Return codes:
940fd558d186df2c13a22455373858bae634a4795afJames Chapman * 0 : success.
941fd558d186df2c13a22455373858bae634a4795afJames Chapman * <0: error
942fd558d186df2c13a22455373858bae634a4795afJames Chapman * >0: skb should be passed up to userspace as UDP.
943fd558d186df2c13a22455373858bae634a4795afJames Chapman */
944fd558d186df2c13a22455373858bae634a4795afJames Chapmanint l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
945fd558d186df2c13a22455373858bae634a4795afJames Chapman{
946fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_tunnel *tunnel;
947fd558d186df2c13a22455373858bae634a4795afJames Chapman
948fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel = l2tp_sock_to_tunnel(sk);
949fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (tunnel == NULL)
950fd558d186df2c13a22455373858bae634a4795afJames Chapman		goto pass_up;
951fd558d186df2c13a22455373858bae634a4795afJames Chapman
952a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
953a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		 tunnel->name, skb->len);
954fd558d186df2c13a22455373858bae634a4795afJames Chapman
955fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
956fd558d186df2c13a22455373858bae634a4795afJames Chapman		goto pass_up_put;
957fd558d186df2c13a22455373858bae634a4795afJames Chapman
958fd558d186df2c13a22455373858bae634a4795afJames Chapman	sock_put(sk);
959fd558d186df2c13a22455373858bae634a4795afJames Chapman	return 0;
960fd558d186df2c13a22455373858bae634a4795afJames Chapman
961fd558d186df2c13a22455373858bae634a4795afJames Chapmanpass_up_put:
962fd558d186df2c13a22455373858bae634a4795afJames Chapman	sock_put(sk);
963fd558d186df2c13a22455373858bae634a4795afJames Chapmanpass_up:
964fd558d186df2c13a22455373858bae634a4795afJames Chapman	return 1;
965fd558d186df2c13a22455373858bae634a4795afJames Chapman}
966fd558d186df2c13a22455373858bae634a4795afJames ChapmanEXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
967fd558d186df2c13a22455373858bae634a4795afJames Chapman
968fd558d186df2c13a22455373858bae634a4795afJames Chapman/************************************************************************
969fd558d186df2c13a22455373858bae634a4795afJames Chapman * Transmit handling
970fd558d186df2c13a22455373858bae634a4795afJames Chapman ***********************************************************************/
971fd558d186df2c13a22455373858bae634a4795afJames Chapman
972fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Build an L2TP header for the session into the buffer provided.
973fd558d186df2c13a22455373858bae634a4795afJames Chapman */
974f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapmanstatic int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
975fd558d186df2c13a22455373858bae634a4795afJames Chapman{
976f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	struct l2tp_tunnel *tunnel = session->tunnel;
977fd558d186df2c13a22455373858bae634a4795afJames Chapman	__be16 *bufp = buf;
978f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	__be16 *optr = buf;
979fd558d186df2c13a22455373858bae634a4795afJames Chapman	u16 flags = L2TP_HDR_VER_2;
980fd558d186df2c13a22455373858bae634a4795afJames Chapman	u32 tunnel_id = tunnel->peer_tunnel_id;
981fd558d186df2c13a22455373858bae634a4795afJames Chapman	u32 session_id = session->peer_session_id;
982fd558d186df2c13a22455373858bae634a4795afJames Chapman
983fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session->send_seq)
984fd558d186df2c13a22455373858bae634a4795afJames Chapman		flags |= L2TP_HDRFLAG_S;
985fd558d186df2c13a22455373858bae634a4795afJames Chapman
986fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Setup L2TP header. */
987fd558d186df2c13a22455373858bae634a4795afJames Chapman	*bufp++ = htons(flags);
988fd558d186df2c13a22455373858bae634a4795afJames Chapman	*bufp++ = htons(tunnel_id);
989fd558d186df2c13a22455373858bae634a4795afJames Chapman	*bufp++ = htons(session_id);
990fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session->send_seq) {
991fd558d186df2c13a22455373858bae634a4795afJames Chapman		*bufp++ = htons(session->ns);
992fd558d186df2c13a22455373858bae634a4795afJames Chapman		*bufp++ = 0;
993fd558d186df2c13a22455373858bae634a4795afJames Chapman		session->ns++;
994f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		session->ns &= 0xffff;
995a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
996a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			 session->name, session->ns);
997fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
998f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
999f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	return bufp - optr;
1000fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1001fd558d186df2c13a22455373858bae634a4795afJames Chapman
1002f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapmanstatic int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1003fd558d186df2c13a22455373858bae634a4795afJames Chapman{
10040d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	struct l2tp_tunnel *tunnel = session->tunnel;
1005f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	char *bufp = buf;
1006f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	char *optr = bufp;
1007f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
10080d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	/* Setup L2TP header. The header differs slightly for UDP and
10090d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	 * IP encapsulations. For UDP, there is 4 bytes of flags.
10100d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	 */
10110d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
10120d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		u16 flags = L2TP_HDR_VER_3;
10130d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		*((__be16 *) bufp) = htons(flags);
10140d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		bufp += 2;
10150d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		*((__be16 *) bufp) = 0;
10160d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		bufp += 2;
10170d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	}
10180d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman
1019f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	*((__be32 *) bufp) = htonl(session->peer_session_id);
1020f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	bufp += 4;
1021f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (session->cookie_len) {
1022f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		memcpy(bufp, &session->cookie[0], session->cookie_len);
1023f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		bufp += session->cookie_len;
1024f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
1025f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (session->l2specific_len) {
1026f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1027f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			u32 l2h = 0;
1028f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			if (session->send_seq) {
1029f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman				l2h = 0x40000000 | session->ns;
1030f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman				session->ns++;
1031f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman				session->ns &= 0xffffff;
1032a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				l2tp_dbg(session, L2TP_MSG_SEQ,
1033a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 "%s: updated ns to %u\n",
1034a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches					 session->name, session->ns);
1035f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			}
1036f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1037f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			*((__be32 *) bufp) = htonl(l2h);
1038f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		}
1039f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		bufp += session->l2specific_len;
1040f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
1041f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (session->offset)
1042f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		bufp += session->offset;
1043fd558d186df2c13a22455373858bae634a4795afJames Chapman
1044f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	return bufp - optr;
1045fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1046fd558d186df2c13a22455373858bae634a4795afJames Chapman
1047fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1048d9d8da805dcb503ef8ee49918a94d49085060f23David S. Miller			  struct flowi *fl, size_t data_len)
1049fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1050fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_tunnel *tunnel = session->tunnel;
1051fd558d186df2c13a22455373858bae634a4795afJames Chapman	unsigned int len = skb->len;
1052fd558d186df2c13a22455373858bae634a4795afJames Chapman	int error;
1053fd558d186df2c13a22455373858bae634a4795afJames Chapman
1054fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Debug */
1055fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session->send_seq)
1056a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1057a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			 session->name, data_len, session->ns - 1);
1058fd558d186df2c13a22455373858bae634a4795afJames Chapman	else
1059a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1060a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			 session->name, data_len);
1061fd558d186df2c13a22455373858bae634a4795afJames Chapman
1062fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session->debug & L2TP_MSG_DATA) {
10630d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
10640d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		unsigned char *datap = skb->data + uhlen;
1065fd558d186df2c13a22455373858bae634a4795afJames Chapman
1066a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		pr_debug("%s: xmit\n", session->name);
1067a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1068a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				     datap, min_t(size_t, 32, len - uhlen));
1069fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1070fd558d186df2c13a22455373858bae634a4795afJames Chapman
1071fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Queue the packet to IP for output */
10724e15ed4d930297c127d280ca1d0c785be870def4Shan Wei	skb->local_df = 1;
1073d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#if IS_ENABLED(CONFIG_IPV6)
1074d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	if (skb->sk->sk_family == PF_INET6)
1075d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		error = inet6_csk_xmit(skb, NULL);
1076d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	else
1077d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#endif
1078d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		error = ip_queue_xmit(skb, fl);
1079fd558d186df2c13a22455373858bae634a4795afJames Chapman
1080fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Update stats */
1081fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (error >= 0) {
10827b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin		atomic_long_inc(&tunnel->stats.tx_packets);
10837b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin		atomic_long_add(len, &tunnel->stats.tx_bytes);
10847b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin		atomic_long_inc(&session->stats.tx_packets);
10857b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin		atomic_long_add(len, &session->stats.tx_bytes);
1086fd558d186df2c13a22455373858bae634a4795afJames Chapman	} else {
10877b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin		atomic_long_inc(&tunnel->stats.tx_errors);
10887b7c0719cd7afee725b920d75ec6a500b76107e6Tom Parkin		atomic_long_inc(&session->stats.tx_errors);
1089fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1090fd558d186df2c13a22455373858bae634a4795afJames Chapman
1091fd558d186df2c13a22455373858bae634a4795afJames Chapman	return 0;
1092fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1093fd558d186df2c13a22455373858bae634a4795afJames Chapman
1094fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Automatically called when the skb is freed.
1095fd558d186df2c13a22455373858bae634a4795afJames Chapman */
1096fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic void l2tp_sock_wfree(struct sk_buff *skb)
1097fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1098fd558d186df2c13a22455373858bae634a4795afJames Chapman	sock_put(skb->sk);
1099fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1100fd558d186df2c13a22455373858bae634a4795afJames Chapman
1101fd558d186df2c13a22455373858bae634a4795afJames Chapman/* For data skbs that we transmit, we associate with the tunnel socket
1102fd558d186df2c13a22455373858bae634a4795afJames Chapman * but don't do accounting.
1103fd558d186df2c13a22455373858bae634a4795afJames Chapman */
1104fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1105fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1106fd558d186df2c13a22455373858bae634a4795afJames Chapman	sock_hold(sk);
1107fd558d186df2c13a22455373858bae634a4795afJames Chapman	skb->sk = sk;
1108fd558d186df2c13a22455373858bae634a4795afJames Chapman	skb->destructor = l2tp_sock_wfree;
1109fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1110fd558d186df2c13a22455373858bae634a4795afJames Chapman
1111d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#if IS_ENABLED(CONFIG_IPV6)
1112d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaisestatic void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1113d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise				int udp_len)
1114d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise{
1115d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	struct ipv6_pinfo *np = inet6_sk(sk);
1116d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	struct udphdr *uh = udp_hdr(skb);
1117d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise
1118d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1119d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	    !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1120d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		__wsum csum = skb_checksum(skb, 0, udp_len, 0);
1121d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		skb->ip_summed = CHECKSUM_UNNECESSARY;
1122d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1123d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise					    IPPROTO_UDP, csum);
1124d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		if (uh->check == 0)
1125d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			uh->check = CSUM_MANGLED_0;
1126d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	} else {
1127d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		skb->ip_summed = CHECKSUM_PARTIAL;
1128d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		skb->csum_start = skb_transport_header(skb) - skb->head;
1129d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		skb->csum_offset = offsetof(struct udphdr, check);
1130d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1131d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise					     udp_len, IPPROTO_UDP, 0);
1132d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise	}
1133d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise}
1134d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#endif
1135d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise
1136fd558d186df2c13a22455373858bae634a4795afJames Chapman/* If caller requires the skb to have a ppp header, the header must be
1137fd558d186df2c13a22455373858bae634a4795afJames Chapman * inserted in the skb data before calling this function.
1138fd558d186df2c13a22455373858bae634a4795afJames Chapman */
1139fd558d186df2c13a22455373858bae634a4795afJames Chapmanint l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1140fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1141fd558d186df2c13a22455373858bae634a4795afJames Chapman	int data_len = skb->len;
11420d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	struct l2tp_tunnel *tunnel = session->tunnel;
11430d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	struct sock *sk = tunnel->sock;
1144d9d8da805dcb503ef8ee49918a94d49085060f23David S. Miller	struct flowi *fl;
1145fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct udphdr *uh;
1146fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct inet_sock *inet;
1147fd558d186df2c13a22455373858bae634a4795afJames Chapman	__wsum csum;
1148fd558d186df2c13a22455373858bae634a4795afJames Chapman	int headroom;
11490d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
11500d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	int udp_len;
1151b8c8430726e5bd552e01dacc5a44f3f83f7446caEric Dumazet	int ret = NET_XMIT_SUCCESS;
1152fd558d186df2c13a22455373858bae634a4795afJames Chapman
1153fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Check that there's enough headroom in the skb to insert IP,
1154fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * UDP and L2TP headers. If not enough, expand it to
1155fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * make room. Adjust truesize.
1156fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
1157fd558d186df2c13a22455373858bae634a4795afJames Chapman	headroom = NET_SKB_PAD + sizeof(struct iphdr) +
11580d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		uhlen + hdr_len;
1159835acf5da239b91edb9f7ebe36516999e156e6eeEric Dumazet	if (skb_cow_head(skb, headroom)) {
1160b8c8430726e5bd552e01dacc5a44f3f83f7446caEric Dumazet		kfree_skb(skb);
1161b8c8430726e5bd552e01dacc5a44f3f83f7446caEric Dumazet		return NET_XMIT_DROP;
1162835acf5da239b91edb9f7ebe36516999e156e6eeEric Dumazet	}
1163fd558d186df2c13a22455373858bae634a4795afJames Chapman
1164fd558d186df2c13a22455373858bae634a4795afJames Chapman	skb_orphan(skb);
1165fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Setup L2TP header */
1166f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	session->build_header(session, __skb_push(skb, hdr_len));
1167fd558d186df2c13a22455373858bae634a4795afJames Chapman
11680d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	/* Reset skb netfilter state */
1169fd558d186df2c13a22455373858bae634a4795afJames Chapman	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1170fd558d186df2c13a22455373858bae634a4795afJames Chapman	IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1171fd558d186df2c13a22455373858bae634a4795afJames Chapman			      IPSKB_REROUTED);
1172fd558d186df2c13a22455373858bae634a4795afJames Chapman	nf_reset(skb);
1173fd558d186df2c13a22455373858bae634a4795afJames Chapman
11746af88da14ee284aaad6e4326da09a89191ab6165David S. Miller	bh_lock_sock(sk);
11756af88da14ee284aaad6e4326da09a89191ab6165David S. Miller	if (sock_owned_by_user(sk)) {
1176b8c8430726e5bd552e01dacc5a44f3f83f7446caEric Dumazet		kfree_skb(skb);
1177b8c8430726e5bd552e01dacc5a44f3f83f7446caEric Dumazet		ret = NET_XMIT_DROP;
11786af88da14ee284aaad6e4326da09a89191ab6165David S. Miller		goto out_unlock;
11796af88da14ee284aaad6e4326da09a89191ab6165David S. Miller	}
11806af88da14ee284aaad6e4326da09a89191ab6165David S. Miller
1181fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Get routing info from the tunnel socket */
1182fd558d186df2c13a22455373858bae634a4795afJames Chapman	skb_dst_drop(skb);
118371b1391a41289735676be02e35239e5aa9fe6ba6Florian Westphal	skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
1184fd558d186df2c13a22455373858bae634a4795afJames Chapman
1185d9d8da805dcb503ef8ee49918a94d49085060f23David S. Miller	inet = inet_sk(sk);
1186d9d8da805dcb503ef8ee49918a94d49085060f23David S. Miller	fl = &inet->cork.fl;
11870d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	switch (tunnel->encap) {
11880d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	case L2TP_ENCAPTYPE_UDP:
11890d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		/* Setup UDP header */
11900d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		__skb_push(skb, sizeof(*uh));
11910d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		skb_reset_transport_header(skb);
11920d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		uh = udp_hdr(skb);
11930d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		uh->source = inet->inet_sport;
11940d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		uh->dest = inet->inet_dport;
11950d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		udp_len = uhlen + hdr_len + data_len;
11960d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		uh->len = htons(udp_len);
11970d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		uh->check = 0;
11980d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman
11990d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		/* Calculate UDP checksum if configured to do so */
1200d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#if IS_ENABLED(CONFIG_IPV6)
1201d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		if (sk->sk_family == PF_INET6)
1202d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1203d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		else
1204d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#endif
12050d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		if (sk->sk_no_check == UDP_CSUM_NOXMIT)
12060d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			skb->ip_summed = CHECKSUM_NONE;
12070d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
12080d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
12090d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			skb->ip_summed = CHECKSUM_COMPLETE;
12100d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			csum = skb_checksum(skb, 0, udp_len, 0);
12110d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			uh->check = csum_tcpudp_magic(inet->inet_saddr,
12120d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman						      inet->inet_daddr,
12130d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman						      udp_len, IPPROTO_UDP, csum);
12140d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			if (uh->check == 0)
12150d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman				uh->check = CSUM_MANGLED_0;
12160d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		} else {
12170d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			skb->ip_summed = CHECKSUM_PARTIAL;
12180d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			skb->csum_start = skb_transport_header(skb) - skb->head;
12190d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			skb->csum_offset = offsetof(struct udphdr, check);
12200d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
12210d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman						       inet->inet_daddr,
12220d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman						       udp_len, IPPROTO_UDP, 0);
12230d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		}
12240d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		break;
12250d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman
12260d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	case L2TP_ENCAPTYPE_IP:
12270d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		break;
1228fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1229fd558d186df2c13a22455373858bae634a4795afJames Chapman
12300d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	l2tp_skb_set_owner_w(skb, sk);
12310d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman
1232d9d8da805dcb503ef8ee49918a94d49085060f23David S. Miller	l2tp_xmit_core(session, skb, fl, data_len);
12336af88da14ee284aaad6e4326da09a89191ab6165David S. Millerout_unlock:
12346af88da14ee284aaad6e4326da09a89191ab6165David S. Miller	bh_unlock_sock(sk);
1235fd558d186df2c13a22455373858bae634a4795afJames Chapman
1236b8c8430726e5bd552e01dacc5a44f3f83f7446caEric Dumazet	return ret;
1237fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1238fd558d186df2c13a22455373858bae634a4795afJames ChapmanEXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1239fd558d186df2c13a22455373858bae634a4795afJames Chapman
1240fd558d186df2c13a22455373858bae634a4795afJames Chapman/*****************************************************************************
1241fd558d186df2c13a22455373858bae634a4795afJames Chapman * Tinnel and session create/destroy.
1242fd558d186df2c13a22455373858bae634a4795afJames Chapman *****************************************************************************/
1243fd558d186df2c13a22455373858bae634a4795afJames Chapman
1244fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Tunnel socket destruct hook.
1245fd558d186df2c13a22455373858bae634a4795afJames Chapman * The tunnel context is deleted only when all session sockets have been
1246fd558d186df2c13a22455373858bae634a4795afJames Chapman * closed.
1247fd558d186df2c13a22455373858bae634a4795afJames Chapman */
1248fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic void l2tp_tunnel_destruct(struct sock *sk)
1249fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1250fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_tunnel *tunnel;
1251f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	struct l2tp_net *pn;
1252fd558d186df2c13a22455373858bae634a4795afJames Chapman
1253fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel = sk->sk_user_data;
1254fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (tunnel == NULL)
1255fd558d186df2c13a22455373858bae634a4795afJames Chapman		goto end;
1256fd558d186df2c13a22455373858bae634a4795afJames Chapman
1257a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1258fd558d186df2c13a22455373858bae634a4795afJames Chapman
1259fd558d186df2c13a22455373858bae634a4795afJames Chapman
1260f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	/* Disable udp encapsulation */
12610d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	switch (tunnel->encap) {
12620d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	case L2TP_ENCAPTYPE_UDP:
12630d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		/* No longer an encapsulation socket. See net/ipv4/udp.c */
12640d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		(udp_sk(sk))->encap_type = 0;
12650d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		(udp_sk(sk))->encap_rcv = NULL;
12669980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin		(udp_sk(sk))->encap_destroy = NULL;
12670d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		break;
12680d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	case L2TP_ENCAPTYPE_IP:
12690d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		break;
12700d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	}
1271fd558d186df2c13a22455373858bae634a4795afJames Chapman
1272fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Remove hooks into tunnel socket */
1273fd558d186df2c13a22455373858bae634a4795afJames Chapman	sk->sk_destruct = tunnel->old_sk_destruct;
1274fd558d186df2c13a22455373858bae634a4795afJames Chapman	sk->sk_user_data = NULL;
1275f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	tunnel->sock = NULL;
1276fd558d186df2c13a22455373858bae634a4795afJames Chapman
1277f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	/* Remove the tunnel struct from the tunnel list */
1278f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	pn = l2tp_pernet(tunnel->l2tp_net);
1279f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1280f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	list_del_rcu(&tunnel->list);
1281f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1282f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	atomic_dec(&l2tp_tunnel_count);
1283fd558d186df2c13a22455373858bae634a4795afJames Chapman
1284f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	l2tp_tunnel_closeall(tunnel);
1285fd558d186df2c13a22455373858bae634a4795afJames Chapman	l2tp_tunnel_dec_refcount(tunnel);
1286fd558d186df2c13a22455373858bae634a4795afJames Chapman
1287f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	/* Call the original destructor */
1288f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	if (sk->sk_destruct)
1289f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin		(*sk->sk_destruct)(sk);
1290fd558d186df2c13a22455373858bae634a4795afJames Chapmanend:
1291fd558d186df2c13a22455373858bae634a4795afJames Chapman	return;
1292fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1293fd558d186df2c13a22455373858bae634a4795afJames Chapman
1294fd558d186df2c13a22455373858bae634a4795afJames Chapman/* When the tunnel is closed, all the attached sessions need to go too.
1295fd558d186df2c13a22455373858bae634a4795afJames Chapman */
1296e34f4c7050e5471b6d4fb25380713937fc837514Tom Parkinvoid l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1297fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1298fd558d186df2c13a22455373858bae634a4795afJames Chapman	int hash;
1299fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct hlist_node *walk;
1300fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct hlist_node *tmp;
1301fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_session *session;
1302fd558d186df2c13a22455373858bae634a4795afJames Chapman
1303fd558d186df2c13a22455373858bae634a4795afJames Chapman	BUG_ON(tunnel == NULL);
1304fd558d186df2c13a22455373858bae634a4795afJames Chapman
1305a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1306a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches		  tunnel->name);
1307fd558d186df2c13a22455373858bae634a4795afJames Chapman
1308fd558d186df2c13a22455373858bae634a4795afJames Chapman	write_lock_bh(&tunnel->hlist_lock);
1309fd558d186df2c13a22455373858bae634a4795afJames Chapman	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1310fd558d186df2c13a22455373858bae634a4795afJames Chapmanagain:
1311fd558d186df2c13a22455373858bae634a4795afJames Chapman		hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1312fd558d186df2c13a22455373858bae634a4795afJames Chapman			session = hlist_entry(walk, struct l2tp_session, hlist);
1313fd558d186df2c13a22455373858bae634a4795afJames Chapman
1314a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			l2tp_info(session, L2TP_MSG_CONTROL,
1315a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches				  "%s: closing session\n", session->name);
1316fd558d186df2c13a22455373858bae634a4795afJames Chapman
1317fd558d186df2c13a22455373858bae634a4795afJames Chapman			hlist_del_init(&session->hlist);
1318fd558d186df2c13a22455373858bae634a4795afJames Chapman
1319fd558d186df2c13a22455373858bae634a4795afJames Chapman			if (session->ref != NULL)
1320fd558d186df2c13a22455373858bae634a4795afJames Chapman				(*session->ref)(session);
1321fd558d186df2c13a22455373858bae634a4795afJames Chapman
1322fd558d186df2c13a22455373858bae634a4795afJames Chapman			write_unlock_bh(&tunnel->hlist_lock);
1323fd558d186df2c13a22455373858bae634a4795afJames Chapman
1324f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin			__l2tp_session_unhash(session);
13254c6e2fd35460208596fa099ee0750a4b0438aa5cTom Parkin			l2tp_session_queue_purge(session);
13264c6e2fd35460208596fa099ee0750a4b0438aa5cTom Parkin
1327fd558d186df2c13a22455373858bae634a4795afJames Chapman			if (session->session_close != NULL)
1328fd558d186df2c13a22455373858bae634a4795afJames Chapman				(*session->session_close)(session);
1329fd558d186df2c13a22455373858bae634a4795afJames Chapman
1330fd558d186df2c13a22455373858bae634a4795afJames Chapman			if (session->deref != NULL)
1331fd558d186df2c13a22455373858bae634a4795afJames Chapman				(*session->deref)(session);
1332fd558d186df2c13a22455373858bae634a4795afJames Chapman
13339980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin			l2tp_session_dec_refcount(session);
13349980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin
1335fd558d186df2c13a22455373858bae634a4795afJames Chapman			write_lock_bh(&tunnel->hlist_lock);
1336fd558d186df2c13a22455373858bae634a4795afJames Chapman
1337fd558d186df2c13a22455373858bae634a4795afJames Chapman			/* Now restart from the beginning of this hash
1338fd558d186df2c13a22455373858bae634a4795afJames Chapman			 * chain.  We always remove a session from the
1339fd558d186df2c13a22455373858bae634a4795afJames Chapman			 * list so we are guaranteed to make forward
1340fd558d186df2c13a22455373858bae634a4795afJames Chapman			 * progress.
1341fd558d186df2c13a22455373858bae634a4795afJames Chapman			 */
1342fd558d186df2c13a22455373858bae634a4795afJames Chapman			goto again;
1343fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
1344fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1345fd558d186df2c13a22455373858bae634a4795afJames Chapman	write_unlock_bh(&tunnel->hlist_lock);
1346fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1347e34f4c7050e5471b6d4fb25380713937fc837514Tom ParkinEXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1348fd558d186df2c13a22455373858bae634a4795afJames Chapman
13499980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin/* Tunnel socket destroy hook for UDP encapsulation */
13509980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkinstatic void l2tp_udp_encap_destroy(struct sock *sk)
13519980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin{
13529980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin	struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
13539980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin	if (tunnel) {
13549980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin		l2tp_tunnel_closeall(tunnel);
13559980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin		sock_put(sk);
13569980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin	}
13579980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin}
13589980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin
1359fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Really kill the tunnel.
1360fd558d186df2c13a22455373858bae634a4795afJames Chapman * Come here only when all sessions have been cleared from the tunnel.
1361fd558d186df2c13a22455373858bae634a4795afJames Chapman */
1362fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1363fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1364fd558d186df2c13a22455373858bae634a4795afJames Chapman	BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1365fd558d186df2c13a22455373858bae634a4795afJames Chapman	BUG_ON(tunnel->sock != NULL);
1366a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
136799469c32f79a32d8481f87be0d3c66dad286f4ecxeb@mail.ru	kfree_rcu(tunnel, rcu);
1368f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin}
1369fd558d186df2c13a22455373858bae634a4795afJames Chapman
1370f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin/* Workqueue tunnel deletion function */
1371f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkinstatic void l2tp_tunnel_del_work(struct work_struct *work)
1372f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin{
1373f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	struct l2tp_tunnel *tunnel = NULL;
1374f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	struct socket *sock = NULL;
1375f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	struct sock *sk = NULL;
1376f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin
1377f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	tunnel = container_of(work, struct l2tp_tunnel, del_work);
1378f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	sk = l2tp_tunnel_sock_lookup(tunnel);
1379f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	if (!sk)
1380f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin		return;
1381f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin
1382f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	sock = sk->sk_socket;
1383f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin
138402d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin	/* If the tunnel socket was created by userspace, then go through the
138502d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin	 * inet layer to shut the socket down, and let userspace close it.
138602d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin	 * Otherwise, if we created the socket directly within the kernel, use
138702d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin	 * the sk API to release it here.
1388167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	 * In either case the tunnel resources are freed in the socket
1389167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	 * destructor when the tunnel socket goes away.
1390f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	 */
139102d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin	if (tunnel->fd >= 0) {
139202d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin		if (sock)
139302d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin			inet_shutdown(sock, 2);
1394167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	} else {
139502d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin		if (sock)
139602d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin			kernel_sock_shutdown(sock, SHUT_RDWR);
139702d13ed5f94af38c37d1abd53462fe48d78bcc9dTom Parkin		sk_release_kernel(sk);
1398167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	}
1399f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin
1400f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	l2tp_tunnel_sock_put(sk);
1401fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1402fd558d186df2c13a22455373858bae634a4795afJames Chapman
1403789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman/* Create a socket for the tunnel, if one isn't set up by
1404789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman * userspace. This is used for static tunnels where there is no
1405789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman * managing L2TP daemon.
1406167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin *
1407167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin * Since we don't want these sockets to keep a namespace alive by
1408167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin * themselves, we drop the socket's namespace refcount after creation.
1409167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin * These sockets are freed when the namespace exits using the pernet
1410167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin * exit hook.
1411789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman */
1412167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkinstatic int l2tp_tunnel_sock_create(struct net *net,
1413167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin				u32 tunnel_id,
1414167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin				u32 peer_tunnel_id,
1415167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin				struct l2tp_tunnel_cfg *cfg,
1416167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin				struct socket **sockp)
1417789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman{
1418789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	int err = -EINVAL;
1419167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	struct socket *sock = NULL;
1420167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	struct sockaddr_in udp_addr = {0};
1421167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	struct sockaddr_l2tpip ip_addr = {0};
1422f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston#if IS_ENABLED(CONFIG_IPV6)
1423167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	struct sockaddr_in6 udp6_addr = {0};
1424167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	struct sockaddr_l2tpip6 ip6_addr = {0};
1425f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston#endif
1426789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1427789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	switch (cfg->encap) {
1428789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	case L2TP_ENCAPTYPE_UDP:
1429f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston#if IS_ENABLED(CONFIG_IPV6)
1430f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston		if (cfg->local_ip6 && cfg->peer_ip6) {
1431167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin			err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
1432f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			if (err < 0)
1433f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston				goto out;
1434789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1435167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin			sk_change_net(sock->sk, net);
1436789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1437f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp6_addr.sin6_family = AF_INET6;
1438f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1439f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			       sizeof(udp6_addr.sin6_addr));
1440f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp6_addr.sin6_port = htons(cfg->local_udp_port);
1441f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1442f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston					  sizeof(udp6_addr));
1443f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			if (err < 0)
1444f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston				goto out;
1445789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1446f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp6_addr.sin6_family = AF_INET6;
1447f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1448f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			       sizeof(udp6_addr.sin6_addr));
1449f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1450f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			err = kernel_connect(sock,
1451f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston					     (struct sockaddr *) &udp6_addr,
1452f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston					     sizeof(udp6_addr), 0);
1453f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			if (err < 0)
1454f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston				goto out;
1455f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston		} else
1456f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston#endif
1457f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston		{
1458167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin			err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
1459f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			if (err < 0)
1460f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston				goto out;
1461f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston
1462167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin			sk_change_net(sock->sk, net);
1463f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston
1464f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp_addr.sin_family = AF_INET;
1465f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp_addr.sin_addr = cfg->local_ip;
1466f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp_addr.sin_port = htons(cfg->local_udp_port);
1467f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1468f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston					  sizeof(udp_addr));
1469f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			if (err < 0)
1470f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston				goto out;
1471f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston
1472f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp_addr.sin_family = AF_INET;
1473f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp_addr.sin_addr = cfg->peer_ip;
1474f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			udp_addr.sin_port = htons(cfg->peer_udp_port);
1475f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			err = kernel_connect(sock,
1476f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston					     (struct sockaddr *) &udp_addr,
1477f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston					     sizeof(udp_addr), 0);
1478f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston			if (err < 0)
1479f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston				goto out;
1480f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston		}
1481789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1482789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		if (!cfg->use_udp_checksums)
1483789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman			sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1484789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1485789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		break;
1486789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1487789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	case L2TP_ENCAPTYPE_IP:
1488f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston#if IS_ENABLED(CONFIG_IPV6)
1489f9bac8df908d7c0a36960265c92f3445623b19d1Chris Elston		if (cfg->local_ip6 && cfg->peer_ip6) {
1490167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin			err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1491167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin					  IPPROTO_L2TP, &sock);
14925dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			if (err < 0)
14935dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman				goto out;
1494789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1495167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin			sk_change_net(sock->sk, net);
1496789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
14975dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip6_addr.l2tp_family = AF_INET6;
14985dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
14995dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			       sizeof(ip6_addr.l2tp_addr));
15005dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip6_addr.l2tp_conn_id = tunnel_id;
15015dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
15025dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman					  sizeof(ip6_addr));
15035dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			if (err < 0)
15045dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman				goto out;
1505789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
15065dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip6_addr.l2tp_family = AF_INET6;
15075dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
15085dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			       sizeof(ip6_addr.l2tp_addr));
15095dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip6_addr.l2tp_conn_id = peer_tunnel_id;
15105dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			err = kernel_connect(sock,
15115dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman					     (struct sockaddr *) &ip6_addr,
15125dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman					     sizeof(ip6_addr), 0);
15135dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			if (err < 0)
15145dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman				goto out;
15155dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman		} else
15165dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman#endif
15175dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman		{
1518167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin			err = sock_create_kern(AF_INET, SOCK_DGRAM,
1519167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin					  IPPROTO_L2TP, &sock);
15205dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			if (err < 0)
15215dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman				goto out;
1522789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1523167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin			sk_change_net(sock->sk, net);
15245dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman
15255dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip_addr.l2tp_family = AF_INET;
15265dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip_addr.l2tp_addr = cfg->local_ip;
15275dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip_addr.l2tp_conn_id = tunnel_id;
15285dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
15295dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman					  sizeof(ip_addr));
15305dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			if (err < 0)
15315dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman				goto out;
15325dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman
15335dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip_addr.l2tp_family = AF_INET;
15345dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip_addr.l2tp_addr = cfg->peer_ip;
15355dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			ip_addr.l2tp_conn_id = peer_tunnel_id;
15365dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
15375dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman					     sizeof(ip_addr), 0);
15385dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman			if (err < 0)
15395dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman				goto out;
15405dac94e109263e75ab7fe4e66ef88e9b49f500bfJames Chapman		}
1541789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		break;
1542789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1543789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	default:
1544789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		goto out;
1545789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	}
1546789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1547789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapmanout:
1548167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	*sockp = sock;
1549789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	if ((err < 0) && sock) {
1550167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin		kernel_sock_shutdown(sock, SHUT_RDWR);
1551167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin		sk_release_kernel(sock->sk);
1552789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		*sockp = NULL;
1553789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	}
1554789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
1555789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	return err;
1556789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman}
1557789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman
155837159ef2c1ae1e696b24b260b241209a19f92c60Eric Dumazetstatic struct lock_class_key l2tp_socket_class;
155937159ef2c1ae1e696b24b260b241209a19f92c60Eric Dumazet
1560fd558d186df2c13a22455373858bae634a4795afJames Chapmanint 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)
1561fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1562fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_tunnel *tunnel = NULL;
1563fd558d186df2c13a22455373858bae634a4795afJames Chapman	int err;
1564fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct socket *sock = NULL;
1565fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct sock *sk = NULL;
1566fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_net *pn;
15670d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1568fd558d186df2c13a22455373858bae634a4795afJames Chapman
1569fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Get the tunnel socket from the fd, which was opened by
1570789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	 * the userspace L2TP daemon. If not specified, create a
1571789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	 * kernel socket.
1572fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
1573789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	if (fd < 0) {
1574167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin		err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1575167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin				cfg, &sock);
1576789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		if (err < 0)
1577789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman			goto err;
1578789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	} else {
1579789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		sock = sockfd_lookup(fd, &err);
1580789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		if (!sock) {
1581cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin			pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1582789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman			       tunnel_id, fd, err);
1583cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin			err = -EBADF;
1584cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin			goto err;
1585cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin		}
1586cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin
1587cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin		/* Reject namespace mismatches */
1588cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin		if (!net_eq(sock_net(sock->sk), net)) {
1589cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin			pr_err("tunl %u: netns mismatch\n", tunnel_id);
1590cbb95e0ca92869cc94a1c5e5ac58395afbbda26eTom Parkin			err = -EINVAL;
1591789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman			goto err;
1592789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman		}
1593fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1594fd558d186df2c13a22455373858bae634a4795afJames Chapman
1595fd558d186df2c13a22455373858bae634a4795afJames Chapman	sk = sock->sk;
1596fd558d186df2c13a22455373858bae634a4795afJames Chapman
15970d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	if (cfg != NULL)
15980d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		encap = cfg->encap;
15990d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman
1600fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Quick sanity checks */
16010d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	switch (encap) {
16020d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	case L2TP_ENCAPTYPE_UDP:
16030d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		err = -EPROTONOSUPPORT;
16040d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		if (sk->sk_protocol != IPPROTO_UDP) {
1605a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
16060d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			       tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
16070d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			goto err;
16080d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		}
16090d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		break;
16100d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	case L2TP_ENCAPTYPE_IP:
16110d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		err = -EPROTONOSUPPORT;
16120d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		if (sk->sk_protocol != IPPROTO_L2TP) {
1613a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches			pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
16140d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			       tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
16150d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			goto err;
16160d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		}
16170d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		break;
1618fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1619fd558d186df2c13a22455373858bae634a4795afJames Chapman
1620fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Check if this socket has already been prepped */
1621fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1622fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (tunnel != NULL) {
1623fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* This socket has already been prepped */
1624fd558d186df2c13a22455373858bae634a4795afJames Chapman		err = -EBUSY;
1625fd558d186df2c13a22455373858bae634a4795afJames Chapman		goto err;
1626fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1627fd558d186df2c13a22455373858bae634a4795afJames Chapman
1628fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1629fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (tunnel == NULL) {
1630fd558d186df2c13a22455373858bae634a4795afJames Chapman		err = -ENOMEM;
1631fd558d186df2c13a22455373858bae634a4795afJames Chapman		goto err;
1632fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1633fd558d186df2c13a22455373858bae634a4795afJames Chapman
1634fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel->version = version;
1635fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel->tunnel_id = tunnel_id;
1636fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel->peer_tunnel_id = peer_tunnel_id;
1637fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1638fd558d186df2c13a22455373858bae634a4795afJames Chapman
1639fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel->magic = L2TP_TUNNEL_MAGIC;
1640fd558d186df2c13a22455373858bae634a4795afJames Chapman	sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1641fd558d186df2c13a22455373858bae634a4795afJames Chapman	rwlock_init(&tunnel->hlist_lock);
1642fd558d186df2c13a22455373858bae634a4795afJames Chapman
1643fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* The net we belong to */
1644fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel->l2tp_net = net;
1645fd558d186df2c13a22455373858bae634a4795afJames Chapman	pn = l2tp_pernet(net);
1646fd558d186df2c13a22455373858bae634a4795afJames Chapman
16470d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	if (cfg != NULL)
1648fd558d186df2c13a22455373858bae634a4795afJames Chapman		tunnel->debug = cfg->debug;
1649fd558d186df2c13a22455373858bae634a4795afJames Chapman
1650fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
16510d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	tunnel->encap = encap;
16520d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	if (encap == L2TP_ENCAPTYPE_UDP) {
16530d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		/* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
16540d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
16550d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
16569980d001cec86c3c75f3a6008ddb73c397ea3b3eTom Parkin		udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
1657d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#if IS_ENABLED(CONFIG_IPV6)
1658d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		if (sk->sk_family == PF_INET6)
1659d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise			udpv6_encap_enable();
1660d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise		else
1661d2cf3361677e5bb5d01d45052212b7050a9aa8c4Benjamin LaHaise#endif
1662447167bf565a474ff0cfb0f41d54936937479e97Eric Dumazet		udp_encap_enable();
16630d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman	}
1664fd558d186df2c13a22455373858bae634a4795afJames Chapman
1665fd558d186df2c13a22455373858bae634a4795afJames Chapman	sk->sk_user_data = tunnel;
1666fd558d186df2c13a22455373858bae634a4795afJames Chapman
1667fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Hook on the tunnel socket destructor so that we can cleanup
1668fd558d186df2c13a22455373858bae634a4795afJames Chapman	 * if the tunnel socket goes away.
1669fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
1670fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel->old_sk_destruct = sk->sk_destruct;
1671fd558d186df2c13a22455373858bae634a4795afJames Chapman	sk->sk_destruct = &l2tp_tunnel_destruct;
1672fd558d186df2c13a22455373858bae634a4795afJames Chapman	tunnel->sock = sk;
167380d84ef3ff1ddc7a829c58980a9dd566a8af5203Tom Parkin	tunnel->fd = fd;
167437159ef2c1ae1e696b24b260b241209a19f92c60Eric Dumazet	lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
167537159ef2c1ae1e696b24b260b241209a19f92c60Eric Dumazet
1676fd558d186df2c13a22455373858bae634a4795afJames Chapman	sk->sk_allocation = GFP_ATOMIC;
1677fd558d186df2c13a22455373858bae634a4795afJames Chapman
1678f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	/* Init delete workqueue struct */
1679f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1680f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin
1681fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Add tunnel to our list */
1682fd558d186df2c13a22455373858bae634a4795afJames Chapman	INIT_LIST_HEAD(&tunnel->list);
1683fd558d186df2c13a22455373858bae634a4795afJames Chapman	atomic_inc(&l2tp_tunnel_count);
1684fd558d186df2c13a22455373858bae634a4795afJames Chapman
1685fd558d186df2c13a22455373858bae634a4795afJames Chapman	/* Bump the reference count. The tunnel context is deleted
16861769192a3c50778e03352a3d95faec830d47ba55Eric Dumazet	 * only when this drops to zero. Must be done before list insertion
1687fd558d186df2c13a22455373858bae634a4795afJames Chapman	 */
1688fd558d186df2c13a22455373858bae634a4795afJames Chapman	l2tp_tunnel_inc_refcount(tunnel);
16891769192a3c50778e03352a3d95faec830d47ba55Eric Dumazet	spin_lock_bh(&pn->l2tp_tunnel_list_lock);
16901769192a3c50778e03352a3d95faec830d47ba55Eric Dumazet	list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
16911769192a3c50778e03352a3d95faec830d47ba55Eric Dumazet	spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1692fd558d186df2c13a22455373858bae634a4795afJames Chapman
1693fd558d186df2c13a22455373858bae634a4795afJames Chapman	err = 0;
1694fd558d186df2c13a22455373858bae634a4795afJames Chapmanerr:
1695fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (tunnelp)
1696fd558d186df2c13a22455373858bae634a4795afJames Chapman		*tunnelp = tunnel;
1697fd558d186df2c13a22455373858bae634a4795afJames Chapman
1698789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	/* If tunnel's socket was created by the kernel, it doesn't
1699789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	 *  have a file.
1700789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	 */
1701789a4a2c61d843df67988d69e7c3f3a4bca97e8eJames Chapman	if (sock && sock->file)
1702fd558d186df2c13a22455373858bae634a4795afJames Chapman		sockfd_put(sock);
1703fd558d186df2c13a22455373858bae634a4795afJames Chapman
1704fd558d186df2c13a22455373858bae634a4795afJames Chapman	return err;
1705fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1706fd558d186df2c13a22455373858bae634a4795afJames ChapmanEXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1707fd558d186df2c13a22455373858bae634a4795afJames Chapman
1708309795f4bec2d69cd507a631f82065c2198a0825James Chapman/* This function is used by the netlink TUNNEL_DELETE command.
1709309795f4bec2d69cd507a631f82065c2198a0825James Chapman */
1710309795f4bec2d69cd507a631f82065c2198a0825James Chapmanint l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1711309795f4bec2d69cd507a631f82065c2198a0825James Chapman{
17122b551c6e7d5bca2c78c216b15ef675653d4f459aTom Parkin	l2tp_tunnel_closeall(tunnel);
1713f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	return (false == queue_work(l2tp_wq, &tunnel->del_work));
1714309795f4bec2d69cd507a631f82065c2198a0825James Chapman}
1715309795f4bec2d69cd507a631f82065c2198a0825James ChapmanEXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1716309795f4bec2d69cd507a631f82065c2198a0825James Chapman
1717fd558d186df2c13a22455373858bae634a4795afJames Chapman/* Really kill the session.
1718fd558d186df2c13a22455373858bae634a4795afJames Chapman */
1719fd558d186df2c13a22455373858bae634a4795afJames Chapmanvoid l2tp_session_free(struct l2tp_session *session)
1720fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1721f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	struct l2tp_tunnel *tunnel = session->tunnel;
1722fd558d186df2c13a22455373858bae634a4795afJames Chapman
1723fd558d186df2c13a22455373858bae634a4795afJames Chapman	BUG_ON(atomic_read(&session->ref_count) != 0);
1724fd558d186df2c13a22455373858bae634a4795afJames Chapman
1725f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	if (tunnel) {
1726fd558d186df2c13a22455373858bae634a4795afJames Chapman		BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1727f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin		if (session->session_id != 0)
1728f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin			atomic_dec(&l2tp_session_count);
1729f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin		sock_put(tunnel->sock);
1730f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin		session->tunnel = NULL;
1731f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin		l2tp_tunnel_dec_refcount(tunnel);
1732f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	}
1733f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin
1734f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	kfree(session);
1735f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin
1736f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	return;
1737f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin}
1738f6e16b299bacaa71c6604a784f2d088a966f8c23Tom ParkinEXPORT_SYMBOL_GPL(l2tp_session_free);
1739f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin
1740f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin/* Remove an l2tp session from l2tp_core's hash lists.
1741f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin * Provides a tidyup interface for pseudowire code which can't just route all
1742f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1743f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin * callback.
1744f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin */
1745f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkinvoid __l2tp_session_unhash(struct l2tp_session *session)
1746f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin{
1747f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	struct l2tp_tunnel *tunnel = session->tunnel;
1748fd558d186df2c13a22455373858bae634a4795afJames Chapman
1749f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	/* Remove the session from core hashes */
1750f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	if (tunnel) {
1751f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin		/* Remove from the per-tunnel hash */
1752fd558d186df2c13a22455373858bae634a4795afJames Chapman		write_lock_bh(&tunnel->hlist_lock);
1753fd558d186df2c13a22455373858bae634a4795afJames Chapman		hlist_del_init(&session->hlist);
1754fd558d186df2c13a22455373858bae634a4795afJames Chapman		write_unlock_bh(&tunnel->hlist_lock);
1755fd558d186df2c13a22455373858bae634a4795afJames Chapman
1756f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin		/* For L2TPv3 we have a per-net hash: remove from there, too */
1757f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (tunnel->version != L2TP_HDR_VER_2) {
1758f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1759e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1760e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			hlist_del_init_rcu(&session->global_hlist);
1761e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1762e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			synchronize_rcu();
1763f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		}
1764fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1765fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1766f6e16b299bacaa71c6604a784f2d088a966f8c23Tom ParkinEXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1767fd558d186df2c13a22455373858bae634a4795afJames Chapman
1768309795f4bec2d69cd507a631f82065c2198a0825James Chapman/* This function is used by the netlink SESSION_DELETE command and by
1769309795f4bec2d69cd507a631f82065c2198a0825James Chapman   pseudowire modules.
1770309795f4bec2d69cd507a631f82065c2198a0825James Chapman */
1771309795f4bec2d69cd507a631f82065c2198a0825James Chapmanint l2tp_session_delete(struct l2tp_session *session)
1772309795f4bec2d69cd507a631f82065c2198a0825James Chapman{
1773f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	if (session->ref)
1774f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin		(*session->ref)(session);
1775f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	__l2tp_session_unhash(session);
17764c6e2fd35460208596fa099ee0750a4b0438aa5cTom Parkin	l2tp_session_queue_purge(session);
1777309795f4bec2d69cd507a631f82065c2198a0825James Chapman	if (session->session_close != NULL)
1778309795f4bec2d69cd507a631f82065c2198a0825James Chapman		(*session->session_close)(session);
1779f6e16b299bacaa71c6604a784f2d088a966f8c23Tom Parkin	if (session->deref)
17801b7c92b90514aaec0daea4319d519084da373aebDan Carpenter		(*session->deref)(session);
1781309795f4bec2d69cd507a631f82065c2198a0825James Chapman	l2tp_session_dec_refcount(session);
1782309795f4bec2d69cd507a631f82065c2198a0825James Chapman	return 0;
1783309795f4bec2d69cd507a631f82065c2198a0825James Chapman}
1784309795f4bec2d69cd507a631f82065c2198a0825James ChapmanEXPORT_SYMBOL_GPL(l2tp_session_delete);
1785309795f4bec2d69cd507a631f82065c2198a0825James Chapman
1786f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman/* We come here whenever a session's send_seq, cookie_len or
1787f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman * l2specific_len parameters are set.
1788f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman */
1789fc130840d75d42c5a360fd1d8b72489eec09cad3stephen hemmingerstatic void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1790f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman{
1791f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	if (version == L2TP_HDR_VER_2) {
1792f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		session->hdr_len = 6;
1793f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (session->send_seq)
1794f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->hdr_len += 4;
1795f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	} else {
17960d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
17970d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman		if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
17980d76751fad7739014485ba5bd388d4f1b4fd4143James Chapman			session->hdr_len += 4;
1799f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	}
1800f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1801f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman}
1802f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1803fd558d186df2c13a22455373858bae634a4795afJames Chapmanstruct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1804fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1805fd558d186df2c13a22455373858bae634a4795afJames Chapman	struct l2tp_session *session;
1806fd558d186df2c13a22455373858bae634a4795afJames Chapman
1807fd558d186df2c13a22455373858bae634a4795afJames Chapman	session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1808fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (session != NULL) {
1809fd558d186df2c13a22455373858bae634a4795afJames Chapman		session->magic = L2TP_SESSION_MAGIC;
1810fd558d186df2c13a22455373858bae634a4795afJames Chapman		session->tunnel = tunnel;
1811fd558d186df2c13a22455373858bae634a4795afJames Chapman
1812fd558d186df2c13a22455373858bae634a4795afJames Chapman		session->session_id = session_id;
1813fd558d186df2c13a22455373858bae634a4795afJames Chapman		session->peer_session_id = peer_session_id;
1814d301e3256866bfd3ae3093aeb43d3ca9570d758eJames Chapman		session->nr = 0;
1815fd558d186df2c13a22455373858bae634a4795afJames Chapman
1816fd558d186df2c13a22455373858bae634a4795afJames Chapman		sprintf(&session->name[0], "sess %u/%u",
1817fd558d186df2c13a22455373858bae634a4795afJames Chapman			tunnel->tunnel_id, session->session_id);
1818fd558d186df2c13a22455373858bae634a4795afJames Chapman
1819fd558d186df2c13a22455373858bae634a4795afJames Chapman		skb_queue_head_init(&session->reorder_q);
1820fd558d186df2c13a22455373858bae634a4795afJames Chapman
1821fd558d186df2c13a22455373858bae634a4795afJames Chapman		INIT_HLIST_NODE(&session->hlist);
1822f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		INIT_HLIST_NODE(&session->global_hlist);
1823fd558d186df2c13a22455373858bae634a4795afJames Chapman
1824fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* Inherit debug options from tunnel */
1825fd558d186df2c13a22455373858bae634a4795afJames Chapman		session->debug = tunnel->debug;
1826fd558d186df2c13a22455373858bae634a4795afJames Chapman
1827fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (cfg) {
1828f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->pwtype = cfg->pw_type;
1829fd558d186df2c13a22455373858bae634a4795afJames Chapman			session->debug = cfg->debug;
1830fd558d186df2c13a22455373858bae634a4795afJames Chapman			session->mtu = cfg->mtu;
1831fd558d186df2c13a22455373858bae634a4795afJames Chapman			session->mru = cfg->mru;
1832fd558d186df2c13a22455373858bae634a4795afJames Chapman			session->send_seq = cfg->send_seq;
1833fd558d186df2c13a22455373858bae634a4795afJames Chapman			session->recv_seq = cfg->recv_seq;
1834fd558d186df2c13a22455373858bae634a4795afJames Chapman			session->lns_mode = cfg->lns_mode;
1835f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->reorder_timeout = cfg->reorder_timeout;
1836f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->offset = cfg->offset;
1837f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->l2specific_type = cfg->l2specific_type;
1838f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->l2specific_len = cfg->l2specific_len;
1839f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->cookie_len = cfg->cookie_len;
1840f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1841f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->peer_cookie_len = cfg->peer_cookie_len;
1842f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1843fd558d186df2c13a22455373858bae634a4795afJames Chapman		}
1844fd558d186df2c13a22455373858bae634a4795afJames Chapman
1845f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (tunnel->version == L2TP_HDR_VER_2)
1846f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->build_header = l2tp_build_l2tpv2_header;
1847f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		else
1848f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			session->build_header = l2tp_build_l2tpv3_header;
1849f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1850f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		l2tp_session_set_header_len(session, tunnel->version);
1851f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1852fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* Bump the reference count. The session context is deleted
1853fd558d186df2c13a22455373858bae634a4795afJames Chapman		 * only when this drops to zero.
1854fd558d186df2c13a22455373858bae634a4795afJames Chapman		 */
1855fd558d186df2c13a22455373858bae634a4795afJames Chapman		l2tp_session_inc_refcount(session);
1856fd558d186df2c13a22455373858bae634a4795afJames Chapman		l2tp_tunnel_inc_refcount(tunnel);
1857fd558d186df2c13a22455373858bae634a4795afJames Chapman
1858fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* Ensure tunnel socket isn't deleted */
1859fd558d186df2c13a22455373858bae634a4795afJames Chapman		sock_hold(tunnel->sock);
1860fd558d186df2c13a22455373858bae634a4795afJames Chapman
1861fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* Add session to the tunnel's hash list */
1862fd558d186df2c13a22455373858bae634a4795afJames Chapman		write_lock_bh(&tunnel->hlist_lock);
1863fd558d186df2c13a22455373858bae634a4795afJames Chapman		hlist_add_head(&session->hlist,
1864fd558d186df2c13a22455373858bae634a4795afJames Chapman			       l2tp_session_id_hash(tunnel, session_id));
1865fd558d186df2c13a22455373858bae634a4795afJames Chapman		write_unlock_bh(&tunnel->hlist_lock);
1866fd558d186df2c13a22455373858bae634a4795afJames Chapman
1867f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		/* And to the global session list if L2TPv3 */
1868f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		if (tunnel->version != L2TP_HDR_VER_2) {
1869f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman			struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1870f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1871e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			spin_lock_bh(&pn->l2tp_session_hlist_lock);
1872e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			hlist_add_head_rcu(&session->global_hlist,
1873e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman					   l2tp_session_id_hash_2(pn, session_id));
1874e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman			spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1875f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		}
1876f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1877fd558d186df2c13a22455373858bae634a4795afJames Chapman		/* Ignore management session in session count value */
1878fd558d186df2c13a22455373858bae634a4795afJames Chapman		if (session->session_id != 0)
1879fd558d186df2c13a22455373858bae634a4795afJames Chapman			atomic_inc(&l2tp_session_count);
1880fd558d186df2c13a22455373858bae634a4795afJames Chapman	}
1881fd558d186df2c13a22455373858bae634a4795afJames Chapman
1882fd558d186df2c13a22455373858bae634a4795afJames Chapman	return session;
1883fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1884fd558d186df2c13a22455373858bae634a4795afJames ChapmanEXPORT_SYMBOL_GPL(l2tp_session_create);
1885fd558d186df2c13a22455373858bae634a4795afJames Chapman
1886fd558d186df2c13a22455373858bae634a4795afJames Chapman/*****************************************************************************
1887fd558d186df2c13a22455373858bae634a4795afJames Chapman * Init and cleanup
1888fd558d186df2c13a22455373858bae634a4795afJames Chapman *****************************************************************************/
1889fd558d186df2c13a22455373858bae634a4795afJames Chapman
1890fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic __net_init int l2tp_init_net(struct net *net)
1891fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1892e773aaff8295e7f3428d9cf6f8a476a33de00716Jiri Pirko	struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1893f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	int hash;
1894fd558d186df2c13a22455373858bae634a4795afJames Chapman
1895fd558d186df2c13a22455373858bae634a4795afJames Chapman	INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1896e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	spin_lock_init(&pn->l2tp_tunnel_list_lock);
1897fd558d186df2c13a22455373858bae634a4795afJames Chapman
1898f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1899f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman		INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1900f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1901e02d494d2c60746ee6583132904ac1791f5bc9a6James Chapman	spin_lock_init(&pn->l2tp_session_hlist_lock);
1902f7faffa3ff8ef6ae712ef16312b8a2aa7a1c95feJames Chapman
1903fd558d186df2c13a22455373858bae634a4795afJames Chapman	return 0;
1904fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1905fd558d186df2c13a22455373858bae634a4795afJames Chapman
1906167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkinstatic __net_exit void l2tp_exit_net(struct net *net)
1907167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin{
1908167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	struct l2tp_net *pn = l2tp_pernet(net);
1909167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	struct l2tp_tunnel *tunnel = NULL;
1910167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin
1911167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	rcu_read_lock_bh();
1912167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1913167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin		(void)l2tp_tunnel_delete(tunnel);
1914167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	}
1915167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	rcu_read_unlock_bh();
1916167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin}
1917167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin
1918fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic struct pernet_operations l2tp_net_ops = {
1919fd558d186df2c13a22455373858bae634a4795afJames Chapman	.init = l2tp_init_net,
1920167eb17e0b178549f5e19036b16b6d6e35856b67Tom Parkin	.exit = l2tp_exit_net,
1921fd558d186df2c13a22455373858bae634a4795afJames Chapman	.id   = &l2tp_net_id,
1922fd558d186df2c13a22455373858bae634a4795afJames Chapman	.size = sizeof(struct l2tp_net),
1923fd558d186df2c13a22455373858bae634a4795afJames Chapman};
1924fd558d186df2c13a22455373858bae634a4795afJames Chapman
1925fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic int __init l2tp_init(void)
1926fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1927fd558d186df2c13a22455373858bae634a4795afJames Chapman	int rc = 0;
1928fd558d186df2c13a22455373858bae634a4795afJames Chapman
1929fd558d186df2c13a22455373858bae634a4795afJames Chapman	rc = register_pernet_device(&l2tp_net_ops);
1930fd558d186df2c13a22455373858bae634a4795afJames Chapman	if (rc)
1931fd558d186df2c13a22455373858bae634a4795afJames Chapman		goto out;
1932fd558d186df2c13a22455373858bae634a4795afJames Chapman
1933f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
1934f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	if (!l2tp_wq) {
1935f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin		pr_err("alloc_workqueue failed\n");
1936f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin		rc = -ENOMEM;
1937f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin		goto out;
1938f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	}
1939f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin
1940a4ca44fa578c7c7fd123b7fba3c2c98d4ba4e53dJoe Perches	pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1941fd558d186df2c13a22455373858bae634a4795afJames Chapman
1942fd558d186df2c13a22455373858bae634a4795afJames Chapmanout:
1943fd558d186df2c13a22455373858bae634a4795afJames Chapman	return rc;
1944fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1945fd558d186df2c13a22455373858bae634a4795afJames Chapman
1946fd558d186df2c13a22455373858bae634a4795afJames Chapmanstatic void __exit l2tp_exit(void)
1947fd558d186df2c13a22455373858bae634a4795afJames Chapman{
1948fd558d186df2c13a22455373858bae634a4795afJames Chapman	unregister_pernet_device(&l2tp_net_ops);
1949f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	if (l2tp_wq) {
1950f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin		destroy_workqueue(l2tp_wq);
1951f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin		l2tp_wq = NULL;
1952f8ccac0e44934ff9414b31cc3167a5c828afec73Tom Parkin	}
1953fd558d186df2c13a22455373858bae634a4795afJames Chapman}
1954fd558d186df2c13a22455373858bae634a4795afJames Chapman
1955fd558d186df2c13a22455373858bae634a4795afJames Chapmanmodule_init(l2tp_init);
1956fd558d186df2c13a22455373858bae634a4795afJames Chapmanmodule_exit(l2tp_exit);
1957fd558d186df2c13a22455373858bae634a4795afJames Chapman
1958fd558d186df2c13a22455373858bae634a4795afJames ChapmanMODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1959fd558d186df2c13a22455373858bae634a4795afJames ChapmanMODULE_DESCRIPTION("L2TP core");
1960fd558d186df2c13a22455373858bae634a4795afJames ChapmanMODULE_LICENSE("GPL");
1961fd558d186df2c13a22455373858bae634a4795afJames ChapmanMODULE_VERSION(L2TP_DRV_VERSION);
1962fd558d186df2c13a22455373858bae634a4795afJames Chapman
1963