blob: d0a295cd71ef184ed638d8a7591b1dc0dc461853 [file] [log] [blame]
James Chapmanfd558d12010-04-02 06:18:33 +00001/*
2 * L2TP core.
3 *
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5 *
6 * This file contains some code of the original L2TPv2 pppol2tp
7 * driver, which has the following copyright:
8 *
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
11 * Contributors:
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 */
20
Joe Perchesa4ca44f2012-05-16 09:55:56 +000021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
James Chapmanfd558d12010-04-02 06:18:33 +000023#include <linux/module.h>
24#include <linux/string.h>
25#include <linux/list.h>
James Chapmane02d4942010-04-02 06:19:16 +000026#include <linux/rculist.h>
James Chapmanfd558d12010-04-02 06:18:33 +000027#include <linux/uaccess.h>
28
29#include <linux/kernel.h>
30#include <linux/spinlock.h>
31#include <linux/kthread.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/errno.h>
35#include <linux/jiffies.h>
36
37#include <linux/netdevice.h>
38#include <linux/net.h>
39#include <linux/inetdevice.h>
40#include <linux/skbuff.h>
41#include <linux/init.h>
James Chapman0d767512010-04-02 06:19:00 +000042#include <linux/in.h>
James Chapmanfd558d12010-04-02 06:18:33 +000043#include <linux/ip.h>
44#include <linux/udp.h>
James Chapman0d767512010-04-02 06:19:00 +000045#include <linux/l2tp.h>
James Chapmanfd558d12010-04-02 06:18:33 +000046#include <linux/hash.h>
47#include <linux/sort.h>
48#include <linux/file.h>
49#include <linux/nsproxy.h>
50#include <net/net_namespace.h>
51#include <net/netns/generic.h>
52#include <net/dst.h>
53#include <net/ip.h>
54#include <net/udp.h>
Tom Herbert85644b42014-07-13 19:49:48 -070055#include <net/udp_tunnel.h>
James Chapman309795f2010-04-02 06:19:10 +000056#include <net/inet_common.h>
James Chapmanfd558d12010-04-02 06:18:33 +000057#include <net/xfrm.h>
James Chapman0d767512010-04-02 06:19:00 +000058#include <net/protocol.h>
Benjamin LaHaised2cf3362012-04-27 08:24:18 +000059#include <net/inet6_connection_sock.h>
60#include <net/inet_ecn.h>
61#include <net/ip6_route.h>
David S. Millerd499bd22012-04-30 13:21:28 -040062#include <net/ip6_checksum.h>
James Chapmanfd558d12010-04-02 06:18:33 +000063
64#include <asm/byteorder.h>
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
James Chapmanfd558d12010-04-02 06:18:33 +000066
67#include "l2tp_core.h"
68
69#define L2TP_DRV_VERSION "V2.0"
70
71/* L2TP header constants */
72#define L2TP_HDRFLAG_T 0x8000
73#define L2TP_HDRFLAG_L 0x4000
74#define L2TP_HDRFLAG_S 0x0800
75#define L2TP_HDRFLAG_O 0x0200
76#define L2TP_HDRFLAG_P 0x0100
77
78#define L2TP_HDR_VER_MASK 0x000F
79#define L2TP_HDR_VER_2 0x0002
James Chapmanf7faffa2010-04-02 06:18:49 +000080#define L2TP_HDR_VER_3 0x0003
James Chapmanfd558d12010-04-02 06:18:33 +000081
82/* L2TPv3 default L2-specific sublayer */
83#define L2TP_SLFLAG_S 0x40000000
84#define L2TP_SL_SEQ_MASK 0x00ffffff
85
Jacob Wen3d418a22019-01-31 15:18:56 +080086#define L2TP_HDR_SIZE_MAX 14
James Chapmanfd558d12010-04-02 06:18:33 +000087
88/* Default trace flags */
89#define L2TP_DEFAULT_DEBUG_FLAGS 0
90
James Chapmanfd558d12010-04-02 06:18:33 +000091/* Private data stored for received packets in the skb.
92 */
93struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000094 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000095 u16 has_seq;
96 u16 length;
97 unsigned long expires;
98};
99
100#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101
Tom Parkinf8ccac02013-01-31 23:43:00 +0000102static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000103
104/* per-net private data for this module */
105static unsigned int l2tp_net_id;
106struct l2tp_net {
107 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000108 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000109 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000110 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000111};
112
Paolo Abenib954f942018-03-12 14:54:24 +0100113#if IS_ENABLED(CONFIG_IPV6)
114static bool l2tp_sk_is_v6(struct sock *sk)
115{
116 return sk->sk_family == PF_INET6 &&
117 !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
118}
119#endif
stephen hemmingerfc130842010-10-21 07:50:46 +0000120
David S. Miller8d8a51e2013-10-08 15:44:26 -0400121static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
122{
123 return sk->sk_user_data;
124}
125
Guillaume Nault9aaef502017-04-12 10:05:29 +0200126static inline struct l2tp_net *l2tp_pernet(const struct net *net)
James Chapmanfd558d12010-04-02 06:18:33 +0000127{
128 BUG_ON(!net);
129
130 return net_generic(net, l2tp_net_id);
131}
132
James Chapmanf7faffa2010-04-02 06:18:49 +0000133/* Session hash global list for L2TPv3.
134 * The session_id SHOULD be random according to RFC3931, but several
135 * L2TP implementations use incrementing session_ids. So we do a real
136 * hash on the session_id, rather than a simple bitmask.
137 */
138static inline struct hlist_head *
139l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
140{
141 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
142
143}
144
James Chapmanfd558d12010-04-02 06:18:33 +0000145/* Session hash list.
146 * The session_id SHOULD be random according to RFC2661, but several
147 * L2TP implementations (Cisco and Microsoft) use incrementing
148 * session_ids. So we do a real hash on the session_id, rather than a
149 * simple bitmask.
150 */
151static inline struct hlist_head *
152l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
153{
154 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
155}
156
James Chapmand00fa9a2018-02-23 17:45:45 +0000157void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
158{
159 sock_put(tunnel->sock);
160 /* the tunnel is freed in the socket destructor */
161}
162EXPORT_SYMBOL(l2tp_tunnel_free);
163
Guillaume Nault54652eb2017-08-25 16:51:40 +0200164/* Lookup a tunnel. A new reference is held on the returned tunnel. */
165struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
166{
167 const struct l2tp_net *pn = l2tp_pernet(net);
168 struct l2tp_tunnel *tunnel;
169
170 rcu_read_lock_bh();
171 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Eric Dumazete571a332019-04-30 06:27:58 -0700172 if (tunnel->tunnel_id == tunnel_id &&
173 refcount_inc_not_zero(&tunnel->ref_count)) {
Guillaume Nault54652eb2017-08-25 16:51:40 +0200174 rcu_read_unlock_bh();
175
176 return tunnel;
177 }
178 }
179 rcu_read_unlock_bh();
180
181 return NULL;
182}
183EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
184
Guillaume Nault5846c132018-04-12 20:50:33 +0200185struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
186{
187 const struct l2tp_net *pn = l2tp_pernet(net);
188 struct l2tp_tunnel *tunnel;
189 int count = 0;
190
191 rcu_read_lock_bh();
192 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Eric Dumazete571a332019-04-30 06:27:58 -0700193 if (++count > nth &&
194 refcount_inc_not_zero(&tunnel->ref_count)) {
Guillaume Nault5846c132018-04-12 20:50:33 +0200195 rcu_read_unlock_bh();
196 return tunnel;
197 }
198 }
199 rcu_read_unlock_bh();
200
201 return NULL;
202}
203EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
204
Guillaume Nault01e28b92018-08-10 13:21:57 +0200205struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
206 u32 session_id)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200207{
208 struct hlist_head *session_list;
209 struct l2tp_session *session;
210
Guillaume Nault61b9a042017-03-31 13:02:25 +0200211 session_list = l2tp_session_id_hash(tunnel, session_id);
Guillaume Nault01e28b92018-08-10 13:21:57 +0200212
Guillaume Nault61b9a042017-03-31 13:02:25 +0200213 read_lock_bh(&tunnel->hlist_lock);
Guillaume Nault01e28b92018-08-10 13:21:57 +0200214 hlist_for_each_entry(session, session_list, hlist)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200215 if (session->session_id == session_id) {
216 l2tp_session_inc_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200217 read_unlock_bh(&tunnel->hlist_lock);
218
219 return session;
220 }
Guillaume Nault61b9a042017-03-31 13:02:25 +0200221 read_unlock_bh(&tunnel->hlist_lock);
222
223 return NULL;
224}
Guillaume Nault01e28b92018-08-10 13:21:57 +0200225EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session);
226
227struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
228{
229 struct hlist_head *session_list;
230 struct l2tp_session *session;
231
232 session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id);
233
234 rcu_read_lock_bh();
235 hlist_for_each_entry_rcu(session, session_list, global_hlist)
236 if (session->session_id == session_id) {
237 l2tp_session_inc_refcount(session);
238 rcu_read_unlock_bh();
239
240 return session;
241 }
242 rcu_read_unlock_bh();
243
244 return NULL;
245}
Guillaume Nault61b9a042017-03-31 13:02:25 +0200246EXPORT_SYMBOL_GPL(l2tp_session_get);
247
Guillaume Naulta4346212017-10-31 17:36:42 +0100248struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
James Chapmanfd558d12010-04-02 06:18:33 +0000249{
250 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000251 struct l2tp_session *session;
252 int count = 0;
253
254 read_lock_bh(&tunnel->hlist_lock);
255 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800256 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000257 if (++count > nth) {
Guillaume Naulte08293a2017-04-03 12:03:13 +0200258 l2tp_session_inc_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000259 read_unlock_bh(&tunnel->hlist_lock);
260 return session;
261 }
262 }
263 }
264
265 read_unlock_bh(&tunnel->hlist_lock);
266
267 return NULL;
268}
Guillaume Naulte08293a2017-04-03 12:03:13 +0200269EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000270
James Chapman309795f2010-04-02 06:19:10 +0000271/* Lookup a session by interface name.
272 * This is very inefficient but is only used by management interfaces.
273 */
Guillaume Nault9aaef502017-04-12 10:05:29 +0200274struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
Guillaume Naulta4346212017-10-31 17:36:42 +0100275 const char *ifname)
James Chapman309795f2010-04-02 06:19:10 +0000276{
277 struct l2tp_net *pn = l2tp_pernet(net);
278 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000279 struct l2tp_session *session;
280
James Chapmane02d4942010-04-02 06:19:16 +0000281 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000282 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800283 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000284 if (!strcmp(session->ifname, ifname)) {
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200285 l2tp_session_inc_refcount(session);
James Chapmane02d4942010-04-02 06:19:16 +0000286 rcu_read_unlock_bh();
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200287
James Chapman309795f2010-04-02 06:19:10 +0000288 return session;
289 }
290 }
291 }
292
James Chapmane02d4942010-04-02 06:19:16 +0000293 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000294
295 return NULL;
296}
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200297EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
James Chapman309795f2010-04-02 06:19:10 +0000298
Guillaume Nault3953ae72017-10-27 16:51:50 +0200299int l2tp_session_register(struct l2tp_session *session,
300 struct l2tp_tunnel *tunnel)
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200301{
302 struct l2tp_session *session_walk;
303 struct hlist_head *g_head;
304 struct hlist_head *head;
305 struct l2tp_net *pn;
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200306 int err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200307
308 head = l2tp_session_id_hash(tunnel, session->session_id);
309
310 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200311 if (!tunnel->acpt_newsess) {
312 err = -ENODEV;
313 goto err_tlock;
314 }
315
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200316 hlist_for_each_entry(session_walk, head, hlist)
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200317 if (session_walk->session_id == session->session_id) {
318 err = -EEXIST;
319 goto err_tlock;
320 }
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200321
322 if (tunnel->version == L2TP_HDR_VER_3) {
323 pn = l2tp_pernet(tunnel->l2tp_net);
Guillaume Nault363a3412018-06-25 16:07:24 +0200324 g_head = l2tp_session_id_hash_2(pn, session->session_id);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200325
326 spin_lock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200327
Ridge Kennedyf0af9cd82020-02-04 12:24:00 +1300328 /* IP encap expects session IDs to be globally unique, while
329 * UDP encap doesn't.
330 */
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200331 hlist_for_each_entry(session_walk, g_head, global_hlist)
Ridge Kennedyf0af9cd82020-02-04 12:24:00 +1300332 if (session_walk->session_id == session->session_id &&
333 (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP ||
334 tunnel->encap == L2TP_ENCAPTYPE_IP)) {
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200335 err = -EEXIST;
336 goto err_tlock_pnlock;
337 }
338
339 l2tp_tunnel_inc_refcount(tunnel);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200340 hlist_add_head_rcu(&session->global_hlist, g_head);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200341
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200342 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200343 } else {
344 l2tp_tunnel_inc_refcount(tunnel);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200345 }
346
347 hlist_add_head(&session->hlist, head);
348 write_unlock_bh(&tunnel->hlist_lock);
349
350 return 0;
351
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200352err_tlock_pnlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200353 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200354err_tlock:
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200355 write_unlock_bh(&tunnel->hlist_lock);
356
Guillaume Naultf3c66d42017-09-01 17:58:48 +0200357 return err;
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200358}
Guillaume Nault3953ae72017-10-27 16:51:50 +0200359EXPORT_SYMBOL_GPL(l2tp_session_register);
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200360
James Chapmanfd558d12010-04-02 06:18:33 +0000361/*****************************************************************************
362 * Receive data handling
363 *****************************************************************************/
364
365/* Queue a skb in order. We come here only if the skb has an L2TP sequence
366 * number.
367 */
368static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
369{
370 struct sk_buff *skbp;
371 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000372 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000373
374 spin_lock_bh(&session->reorder_q.lock);
375 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
376 if (L2TP_SKB_CB(skbp)->ns > ns) {
377 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000378 l2tp_dbg(session, L2TP_MSG_SEQ,
379 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
380 session->name, ns, L2TP_SKB_CB(skbp)->ns,
381 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000382 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000383 goto out;
384 }
385 }
386
387 __skb_queue_tail(&session->reorder_q, skb);
388
389out:
390 spin_unlock_bh(&session->reorder_q.lock);
391}
392
393/* Dequeue a single skb.
394 */
395static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
396{
397 struct l2tp_tunnel *tunnel = session->tunnel;
398 int length = L2TP_SKB_CB(skb)->length;
399
400 /* We're about to requeue the skb, so return resources
401 * to its current owner (a socket receive buffer).
402 */
403 skb_orphan(skb);
404
Tom Parkin7b7c0712013-03-19 06:11:22 +0000405 atomic_long_inc(&tunnel->stats.rx_packets);
406 atomic_long_add(length, &tunnel->stats.rx_bytes);
407 atomic_long_inc(&session->stats.rx_packets);
408 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000409
410 if (L2TP_SKB_CB(skb)->has_seq) {
411 /* Bump our Nr */
412 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100413 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000414
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000415 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
416 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000417 }
418
419 /* call private receive handler */
420 if (session->recv_skb != NULL)
421 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
422 else
423 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000424}
425
426/* Dequeue skbs from the session's reorder_q, subject to packet order.
427 * Skbs that have been in the queue for too long are simply discarded.
428 */
429static void l2tp_recv_dequeue(struct l2tp_session *session)
430{
431 struct sk_buff *skb;
432 struct sk_buff *tmp;
433
434 /* If the pkt at the head of the queue has the nr that we
435 * expect to send up next, dequeue it and any other
436 * in-sequence packets behind it.
437 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000438start:
James Chapmanfd558d12010-04-02 06:18:33 +0000439 spin_lock_bh(&session->reorder_q.lock);
440 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
441 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000442 atomic_long_inc(&session->stats.rx_seq_discards);
443 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000444 l2tp_dbg(session, L2TP_MSG_SEQ,
445 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
446 session->name, L2TP_SKB_CB(skb)->ns,
447 L2TP_SKB_CB(skb)->length, session->nr,
448 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000449 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000450 __skb_unlink(skb, &session->reorder_q);
451 kfree_skb(skb);
James Chapmanfd558d12010-04-02 06:18:33 +0000452 continue;
453 }
454
455 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000456 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000457 l2tp_dbg(session, L2TP_MSG_SEQ,
458 "%s: advancing nr to next pkt: %u -> %u",
459 session->name, session->nr,
460 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000461 session->reorder_skip = 0;
462 session->nr = L2TP_SKB_CB(skb)->ns;
463 }
James Chapmanfd558d12010-04-02 06:18:33 +0000464 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000465 l2tp_dbg(session, L2TP_MSG_SEQ,
466 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
467 session->name, L2TP_SKB_CB(skb)->ns,
468 L2TP_SKB_CB(skb)->length, session->nr,
469 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000470 goto out;
471 }
472 }
473 __skb_unlink(skb, &session->reorder_q);
474
475 /* Process the skb. We release the queue lock while we
476 * do so to let other contexts process the queue.
477 */
478 spin_unlock_bh(&session->reorder_q.lock);
479 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000480 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000481 }
482
483out:
484 spin_unlock_bh(&session->reorder_q.lock);
485}
486
James Chapman8a1631d2013-07-02 20:28:59 +0100487static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
488{
489 u32 nws;
490
491 if (nr >= session->nr)
492 nws = nr - session->nr;
493 else
494 nws = (session->nr_max + 1) - (session->nr - nr);
495
496 return nws < session->nr_window_size;
497}
498
James Chapmanb6dc01a2013-07-02 20:28:58 +0100499/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
500 * acceptable, else non-zero.
501 */
502static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
503{
James Chapman8a1631d2013-07-02 20:28:59 +0100504 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
505 /* Packet sequence number is outside allowed window.
506 * Discard it.
507 */
508 l2tp_dbg(session, L2TP_MSG_SEQ,
509 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
510 session->name, L2TP_SKB_CB(skb)->ns,
511 L2TP_SKB_CB(skb)->length, session->nr);
512 goto discard;
513 }
514
James Chapmanb6dc01a2013-07-02 20:28:58 +0100515 if (session->reorder_timeout != 0) {
516 /* Packet reordering enabled. Add skb to session's
517 * reorder queue, in order of ns.
518 */
519 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100520 goto out;
521 }
522
523 /* Packet reordering disabled. Discard out-of-sequence packets, while
524 * tracking the number if in-sequence packets after the first OOS packet
525 * is seen. After nr_oos_count_max in-sequence packets, reset the
526 * sequence number to re-enable packet reception.
527 */
528 if (L2TP_SKB_CB(skb)->ns == session->nr) {
529 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100530 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100531 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
532 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
533
534 if (nr_oos == nr_next)
535 session->nr_oos_count++;
536 else
537 session->nr_oos_count = 0;
538
539 session->nr_oos = nr_oos;
540 if (session->nr_oos_count > session->nr_oos_count_max) {
541 session->reorder_skip = 1;
542 l2tp_dbg(session, L2TP_MSG_SEQ,
543 "%s: %d oos packets received. Resetting sequence numbers\n",
544 session->name, session->nr_oos_count);
545 }
546 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100547 atomic_long_inc(&session->stats.rx_seq_discards);
548 l2tp_dbg(session, L2TP_MSG_SEQ,
549 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
550 session->name, L2TP_SKB_CB(skb)->ns,
551 L2TP_SKB_CB(skb)->length, session->nr,
552 skb_queue_len(&session->reorder_q));
553 goto discard;
554 }
555 skb_queue_tail(&session->reorder_q, skb);
556 }
557
James Chapmana0dbd822013-07-02 20:29:00 +0100558out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100559 return 0;
560
561discard:
562 return 1;
563}
564
James Chapmanf7faffa2010-04-02 06:18:49 +0000565/* Do receive processing of L2TP data frames. We handle both L2TPv2
566 * and L2TPv3 data frames here.
567 *
568 * L2TPv2 Data Message Header
569 *
570 * 0 1 2 3
571 * 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
572 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
573 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
574 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
575 * | Tunnel ID | Session ID |
576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
577 * | Ns (opt) | Nr (opt) |
578 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
579 * | Offset Size (opt) | Offset pad... (opt)
580 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
581 *
582 * Data frames are marked by T=0. All other fields are the same as
583 * those in L2TP control frames.
584 *
585 * L2TPv3 Data Message Header
586 *
587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 * | L2TP Session Header |
589 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * | L2-Specific Sublayer |
591 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 * | Tunnel Payload ...
593 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
594 *
595 * L2TPv3 Session Header Over IP
596 *
597 * 0 1 2 3
598 * 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
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * | Session ID |
601 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 * | Cookie (optional, maximum 64 bits)...
603 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
604 * |
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 *
607 * L2TPv3 L2-Specific Sublayer Format
608 *
609 * 0 1 2 3
610 * 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
611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 * |x|S|x|x|x|x|x|x| Sequence Number |
613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
614 *
Guillaume Nault23fe8462018-01-05 19:47:14 +0100615 * Cookie value and sublayer format are negotiated with the peer when
616 * the session is set up. Unlike L2TPv2, we do not need to parse the
617 * packet header to determine if optional fields are present.
James Chapmanf7faffa2010-04-02 06:18:49 +0000618 *
619 * Caller must already have parsed the frame and determined that it is
620 * a data (not control) frame before coming here. Fields up to the
621 * session-id have already been parsed and ptr points to the data
622 * after the session-id.
James Chapmanfd558d12010-04-02 06:18:33 +0000623 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000624void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
625 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
Guillaume Nault2b139e62018-07-25 14:53:33 +0200626 int length)
James Chapmanfd558d12010-04-02 06:18:33 +0000627{
James Chapmanf7faffa2010-04-02 06:18:49 +0000628 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000629 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000630 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000631
James Chapmanf7faffa2010-04-02 06:18:49 +0000632 /* Parse and check optional cookie */
633 if (session->peer_cookie_len > 0) {
634 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000635 l2tp_info(tunnel, L2TP_MSG_DATA,
636 "%s: cookie mismatch (%u/%u). Discarding.\n",
637 tunnel->name, tunnel->tunnel_id,
638 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000639 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000640 goto discard;
641 }
642 ptr += session->peer_cookie_len;
643 }
644
James Chapmanfd558d12010-04-02 06:18:33 +0000645 /* Handle the optional sequence numbers. Sequence numbers are
646 * in different places for L2TPv2 and L2TPv3.
647 *
648 * If we are the LAC, enable/disable sequence numbers under
649 * the control of the LNS. If no sequence numbers present but
650 * we were expecting them, discard frame.
651 */
652 ns = nr = 0;
653 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000654 if (tunnel->version == L2TP_HDR_VER_2) {
655 if (hdrflags & L2TP_HDRFLAG_S) {
656 ns = ntohs(*(__be16 *) ptr);
657 ptr += 2;
658 nr = ntohs(*(__be16 *) ptr);
659 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000660
James Chapmanf7faffa2010-04-02 06:18:49 +0000661 /* Store L2TP info in the skb */
662 L2TP_SKB_CB(skb)->ns = ns;
663 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000664
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000665 l2tp_dbg(session, L2TP_MSG_SEQ,
666 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
667 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000668 }
669 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
670 u32 l2h = ntohl(*(__be32 *) ptr);
671
672 if (l2h & 0x40000000) {
673 ns = l2h & 0x00ffffff;
674
675 /* Store L2TP info in the skb */
676 L2TP_SKB_CB(skb)->ns = ns;
677 L2TP_SKB_CB(skb)->has_seq = 1;
678
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000679 l2tp_dbg(session, L2TP_MSG_SEQ,
680 "%s: recv data ns=%u, session nr=%u\n",
681 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000682 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100683 ptr += 4;
James Chapmanfd558d12010-04-02 06:18:33 +0000684 }
685
686 if (L2TP_SKB_CB(skb)->has_seq) {
687 /* Received a packet with sequence numbers. If we're the LNS,
688 * check if we sre sending sequence numbers and if not,
689 * configure it so.
690 */
691 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000692 l2tp_info(session, L2TP_MSG_SEQ,
693 "%s: requested to enable seq numbers by LNS\n",
694 session->name);
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +0000695 session->send_seq = 1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000696 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000697 }
698 } else {
699 /* No sequence numbers.
700 * If user has configured mandatory sequence numbers, discard.
701 */
702 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000703 l2tp_warn(session, L2TP_MSG_SEQ,
704 "%s: recv data has no seq numbers when required. Discarding.\n",
705 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000706 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000707 goto discard;
708 }
709
710 /* If we're the LAC and we're sending sequence numbers, the
711 * LNS has requested that we no longer send sequence numbers.
712 * If we're the LNS and we're sending sequence numbers, the
713 * LAC is broken. Discard the frame.
714 */
715 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000716 l2tp_info(session, L2TP_MSG_SEQ,
717 "%s: requested to disable seq numbers by LNS\n",
718 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000719 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000720 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000721 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000722 l2tp_warn(session, L2TP_MSG_SEQ,
723 "%s: recv data has no seq numbers when required. Discarding.\n",
724 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000725 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000726 goto discard;
727 }
728 }
729
James Chapman900631e2018-01-03 22:48:06 +0000730 /* Session data offset is defined only for L2TPv2 and is
731 * indicated by an optional 16-bit value in the header.
James Chapmanf7faffa2010-04-02 06:18:49 +0000732 */
733 if (tunnel->version == L2TP_HDR_VER_2) {
734 /* If offset bit set, skip it. */
735 if (hdrflags & L2TP_HDRFLAG_O) {
736 offset = ntohs(*(__be16 *)ptr);
737 ptr += 2 + offset;
738 }
James Chapman900631e2018-01-03 22:48:06 +0000739 }
James Chapmanfd558d12010-04-02 06:18:33 +0000740
741 offset = ptr - optr;
742 if (!pskb_may_pull(skb, offset))
743 goto discard;
744
745 __skb_pull(skb, offset);
746
James Chapmanfd558d12010-04-02 06:18:33 +0000747 /* Prepare skb for adding to the session's reorder_q. Hold
748 * packets for max reorder_timeout or 1 second if not
749 * reordering.
750 */
751 L2TP_SKB_CB(skb)->length = length;
752 L2TP_SKB_CB(skb)->expires = jiffies +
753 (session->reorder_timeout ? session->reorder_timeout : HZ);
754
755 /* Add packet to the session's receive queue. Reordering is done here, if
756 * enabled. Saved L2TP protocol info is stored in skb->sb[].
757 */
758 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100759 if (l2tp_recv_data_seq(session, skb))
760 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000761 } else {
762 /* No sequence numbers. Add the skb to the tail of the
763 * reorder queue. This ensures that it will be
764 * delivered after all previous sequenced skbs.
765 */
766 skb_queue_tail(&session->reorder_q, skb);
767 }
768
769 /* Try to dequeue as many skbs from reorder_q as we can. */
770 l2tp_recv_dequeue(session);
771
James Chapmanf7faffa2010-04-02 06:18:49 +0000772 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000773
774discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000775 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000776 kfree_skb(skb);
James Chapmanf7faffa2010-04-02 06:18:49 +0000777}
778EXPORT_SYMBOL(l2tp_recv_common);
779
Tom Parkin48f72f92013-03-19 06:11:19 +0000780/* Drop skbs from the session's reorder_q
781 */
Guillaume Nault2e675602018-06-25 16:07:22 +0200782static int l2tp_session_queue_purge(struct l2tp_session *session)
Tom Parkin48f72f92013-03-19 06:11:19 +0000783{
784 struct sk_buff *skb = NULL;
785 BUG_ON(!session);
786 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
787 while ((skb = skb_dequeue(&session->reorder_q))) {
788 atomic_long_inc(&session->stats.rx_errors);
789 kfree_skb(skb);
Tom Parkin48f72f92013-03-19 06:11:19 +0000790 }
791 return 0;
792}
Tom Parkin48f72f92013-03-19 06:11:19 +0000793
James Chapmanf7faffa2010-04-02 06:18:49 +0000794/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
795 * here. The skb is not on a list when we get here.
796 * Returns 0 if the packet was a data packet and was successfully passed on.
797 * Returns 1 if the packet was not a good data packet and could not be
798 * forwarded. All such packets are passed up to userspace to deal with.
799 */
Guillaume Nault2b139e62018-07-25 14:53:33 +0200800static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
James Chapmanf7faffa2010-04-02 06:18:49 +0000801{
802 struct l2tp_session *session = NULL;
803 unsigned char *ptr, *optr;
804 u16 hdrflags;
805 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000806 u16 version;
807 int length;
808
Tom Herbert58d60852014-05-07 16:52:48 -0700809 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000810
811 /* UDP always verifies the packet length. */
812 __skb_pull(skb, sizeof(struct udphdr));
813
814 /* Short packet? */
Jacob Wen3d418a22019-01-31 15:18:56 +0800815 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000816 l2tp_info(tunnel, L2TP_MSG_DATA,
817 "%s: recv short packet (len=%d)\n",
818 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000819 goto error;
820 }
821
James Chapmanf7faffa2010-04-02 06:18:49 +0000822 /* Trace packet contents, if enabled */
823 if (tunnel->debug & L2TP_MSG_DATA) {
824 length = min(32u, skb->len);
825 if (!pskb_may_pull(skb, length))
826 goto error;
827
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000828 pr_debug("%s: recv\n", tunnel->name);
829 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000830 }
831
Eric Dumazete50e7052011-11-08 13:59:44 -0500832 /* Point to L2TP header */
833 optr = ptr = skb->data;
834
James Chapmanf7faffa2010-04-02 06:18:49 +0000835 /* Get L2TP header flags */
836 hdrflags = ntohs(*(__be16 *) ptr);
837
838 /* Check protocol version */
839 version = hdrflags & L2TP_HDR_VER_MASK;
840 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000841 l2tp_info(tunnel, L2TP_MSG_DATA,
842 "%s: recv protocol version mismatch: got %d expected %d\n",
843 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000844 goto error;
845 }
846
847 /* Get length of L2TP packet */
848 length = skb->len;
849
850 /* If type is control packet, it is handled by userspace. */
851 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000852 l2tp_dbg(tunnel, L2TP_MSG_DATA,
853 "%s: recv control packet, len=%d\n",
854 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000855 goto error;
856 }
857
858 /* Skip flags */
859 ptr += 2;
860
861 if (tunnel->version == L2TP_HDR_VER_2) {
862 /* If length is present, skip it */
863 if (hdrflags & L2TP_HDRFLAG_L)
864 ptr += 2;
865
866 /* Extract tunnel and session ID */
867 tunnel_id = ntohs(*(__be16 *) ptr);
868 ptr += 2;
869 session_id = ntohs(*(__be16 *) ptr);
870 ptr += 2;
871 } else {
872 ptr += 2; /* skip reserved bits */
873 tunnel_id = tunnel->tunnel_id;
874 session_id = ntohl(*(__be32 *) ptr);
875 ptr += 4;
876 }
877
878 /* Find the session context */
Guillaume Nault01e28b92018-08-10 13:21:57 +0200879 session = l2tp_tunnel_get_session(tunnel, session_id);
James Chapman309795f2010-04-02 06:19:10 +0000880 if (!session || !session->recv_skb) {
Guillaume Naulta4346212017-10-31 17:36:42 +0100881 if (session)
Guillaume Nault61b9a042017-03-31 13:02:25 +0200882 l2tp_session_dec_refcount(session);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200883
James Chapmanf7faffa2010-04-02 06:18:49 +0000884 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000885 l2tp_info(tunnel, L2TP_MSG_DATA,
886 "%s: no session found (%u/%u). Passing up.\n",
887 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000888 goto error;
889 }
890
Jacob Wen8de67662019-01-30 14:55:14 +0800891 if (tunnel->version == L2TP_HDR_VER_3 &&
892 l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
893 goto error;
894
Guillaume Nault2b139e62018-07-25 14:53:33 +0200895 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200896 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000897
898 return 0;
899
James Chapmanfd558d12010-04-02 06:18:33 +0000900error:
901 /* Put UDP header back */
902 __skb_push(skb, sizeof(struct udphdr));
903
904 return 1;
905}
James Chapmanfd558d12010-04-02 06:18:33 +0000906
907/* UDP encapsulation receive handler. See net/ipv4/udp.c.
908 * Return codes:
909 * 0 : success.
910 * <0: error
911 * >0: skb should be passed up to userspace as UDP.
912 */
913int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
914{
915 struct l2tp_tunnel *tunnel;
916
Eric Dumazet485f3822019-04-23 09:43:26 -0700917 tunnel = rcu_dereference_sk_user_data(sk);
James Chapmanfd558d12010-04-02 06:18:33 +0000918 if (tunnel == NULL)
919 goto pass_up;
920
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000921 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
922 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +0000923
Guillaume Nault2b139e62018-07-25 14:53:33 +0200924 if (l2tp_udp_recv_core(tunnel, skb))
James Chapmand00fa9a2018-02-23 17:45:45 +0000925 goto pass_up;
James Chapmanfd558d12010-04-02 06:18:33 +0000926
James Chapmanfd558d12010-04-02 06:18:33 +0000927 return 0;
928
James Chapmanfd558d12010-04-02 06:18:33 +0000929pass_up:
930 return 1;
931}
932EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
933
934/************************************************************************
935 * Transmit handling
936 ***********************************************************************/
937
938/* Build an L2TP header for the session into the buffer provided.
939 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000940static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000941{
James Chapmanf7faffa2010-04-02 06:18:49 +0000942 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000943 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +0000944 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +0000945 u16 flags = L2TP_HDR_VER_2;
946 u32 tunnel_id = tunnel->peer_tunnel_id;
947 u32 session_id = session->peer_session_id;
948
949 if (session->send_seq)
950 flags |= L2TP_HDRFLAG_S;
951
952 /* Setup L2TP header. */
953 *bufp++ = htons(flags);
954 *bufp++ = htons(tunnel_id);
955 *bufp++ = htons(session_id);
956 if (session->send_seq) {
957 *bufp++ = htons(session->ns);
958 *bufp++ = 0;
959 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +0000960 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000961 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
962 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +0000963 }
James Chapmanf7faffa2010-04-02 06:18:49 +0000964
965 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +0000966}
967
James Chapmanf7faffa2010-04-02 06:18:49 +0000968static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +0000969{
James Chapman0d767512010-04-02 06:19:00 +0000970 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +0000971 char *bufp = buf;
972 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +0000973
James Chapman0d767512010-04-02 06:19:00 +0000974 /* Setup L2TP header. The header differs slightly for UDP and
975 * IP encapsulations. For UDP, there is 4 bytes of flags.
976 */
977 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
978 u16 flags = L2TP_HDR_VER_3;
979 *((__be16 *) bufp) = htons(flags);
980 bufp += 2;
981 *((__be16 *) bufp) = 0;
982 bufp += 2;
983 }
984
James Chapmanf7faffa2010-04-02 06:18:49 +0000985 *((__be32 *) bufp) = htonl(session->peer_session_id);
986 bufp += 4;
987 if (session->cookie_len) {
988 memcpy(bufp, &session->cookie[0], session->cookie_len);
989 bufp += session->cookie_len;
990 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100991 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
992 u32 l2h = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000993
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +0100994 if (session->send_seq) {
995 l2h = 0x40000000 | session->ns;
996 session->ns++;
997 session->ns &= 0xffffff;
998 l2tp_dbg(session, L2TP_MSG_SEQ,
999 "%s: updated ns to %u\n",
1000 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001001 }
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001002
1003 *((__be32 *)bufp) = htonl(l2h);
1004 bufp += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001005 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001006
1007 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001008}
James Chapmanfd558d12010-04-02 06:18:33 +00001009
Guillaume Nault2685fbb2018-06-25 16:07:25 +02001010static void l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1011 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001012{
1013 struct l2tp_tunnel *tunnel = session->tunnel;
1014 unsigned int len = skb->len;
1015 int error;
1016
1017 /* Debug */
1018 if (session->send_seq)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001019 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001020 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001021 else
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001022 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001023 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001024
1025 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001026 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1027 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001028
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001029 pr_debug("%s: xmit\n", session->name);
1030 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1031 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001032 }
1033
1034 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001035 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001036#if IS_ENABLED(CONFIG_IPV6)
Paolo Abenib954f942018-03-12 14:54:24 +01001037 if (l2tp_sk_is_v6(tunnel->sock))
Eric Dumazetb0270e92014-04-15 12:58:34 -04001038 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001039 else
1040#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001041 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001042
1043 /* Update stats */
1044 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001045 atomic_long_inc(&tunnel->stats.tx_packets);
1046 atomic_long_add(len, &tunnel->stats.tx_bytes);
1047 atomic_long_inc(&session->stats.tx_packets);
1048 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001049 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001050 atomic_long_inc(&tunnel->stats.tx_errors);
1051 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001052 }
James Chapmanfd558d12010-04-02 06:18:33 +00001053}
James Chapmanfd558d12010-04-02 06:18:33 +00001054
James Chapmanfd558d12010-04-02 06:18:33 +00001055/* If caller requires the skb to have a ppp header, the header must be
1056 * inserted in the skb data before calling this function.
1057 */
1058int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1059{
1060 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001061 struct l2tp_tunnel *tunnel = session->tunnel;
1062 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001063 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001064 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001065 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001066 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001067 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1068 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001069 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001070
1071 /* Check that there's enough headroom in the skb to insert IP,
1072 * UDP and L2TP headers. If not enough, expand it to
1073 * make room. Adjust truesize.
1074 */
1075 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001076 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001077 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001078 kfree_skb(skb);
1079 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001080 }
James Chapmanfd558d12010-04-02 06:18:33 +00001081
James Chapmanfd558d12010-04-02 06:18:33 +00001082 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001083 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001084
James Chapman0d767512010-04-02 06:19:00 +00001085 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001086 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1087 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1088 IPSKB_REROUTED);
1089 nf_reset(skb);
1090
David S. Miller6af88da2011-05-08 13:45:20 -07001091 bh_lock_sock(sk);
1092 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001093 kfree_skb(skb);
1094 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001095 goto out_unlock;
1096 }
1097
Paolo Abenib954f942018-03-12 14:54:24 +01001098 /* The user-space may change the connection status for the user-space
1099 * provided socket at run time: we must check it under the socket lock
1100 */
1101 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1102 kfree_skb(skb);
1103 ret = NET_XMIT_DROP;
1104 goto out_unlock;
1105 }
1106
James Chapmanfd558d12010-04-02 06:18:33 +00001107 /* Get routing info from the tunnel socket */
1108 skb_dst_drop(skb);
Wei Wang6d37fa42018-08-10 11:14:56 -07001109 skb_dst_set(skb, sk_dst_check(sk, 0));
James Chapmanfd558d12010-04-02 06:18:33 +00001110
David S. Millerd9d8da82011-05-06 22:23:20 -07001111 inet = inet_sk(sk);
1112 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001113 switch (tunnel->encap) {
1114 case L2TP_ENCAPTYPE_UDP:
1115 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001116 __skb_push(skb, sizeof(*uh));
1117 skb_reset_transport_header(skb);
1118 uh = udp_hdr(skb);
1119 uh->source = inet->inet_sport;
1120 uh->dest = inet->inet_dport;
1121 udp_len = uhlen + hdr_len + data_len;
1122 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001123
1124 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001125#if IS_ENABLED(CONFIG_IPV6)
Paolo Abenib954f942018-03-12 14:54:24 +01001126 if (l2tp_sk_is_v6(sk))
Tom Herbert77157e12014-06-04 17:19:56 -07001127 udp6_set_csum(udp_get_no_check6_tx(sk),
1128 skb, &inet6_sk(sk)->saddr,
1129 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001130 else
1131#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001132 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1133 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001134 break;
1135
1136 case L2TP_ENCAPTYPE_IP:
1137 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001138 }
1139
David S. Millerd9d8da82011-05-06 22:23:20 -07001140 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001141out_unlock:
1142 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001143
Eric Dumazetb8c84302012-06-28 20:15:13 +00001144 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001145}
1146EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1147
1148/*****************************************************************************
1149 * Tinnel and session create/destroy.
1150 *****************************************************************************/
1151
1152/* Tunnel socket destruct hook.
1153 * The tunnel context is deleted only when all session sockets have been
1154 * closed.
1155 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001156static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001157{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001158 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001159
James Chapmanfd558d12010-04-02 06:18:33 +00001160 if (tunnel == NULL)
1161 goto end;
1162
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001163 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001164
Tom Parkinf8ccac02013-01-31 23:43:00 +00001165 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001166 switch (tunnel->encap) {
1167 case L2TP_ENCAPTYPE_UDP:
1168 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1169 (udp_sk(sk))->encap_type = 0;
1170 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001171 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001172 break;
1173 case L2TP_ENCAPTYPE_IP:
1174 break;
1175 }
James Chapmanfd558d12010-04-02 06:18:33 +00001176
1177 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001178 sk->sk_destruct = tunnel->old_sk_destruct;
1179 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001180
James Chapmanfd558d12010-04-02 06:18:33 +00001181 /* Call the original destructor */
1182 if (sk->sk_destruct)
1183 (*sk->sk_destruct)(sk);
James Chapmand00fa9a2018-02-23 17:45:45 +00001184
1185 kfree_rcu(tunnel, rcu);
James Chapmanfd558d12010-04-02 06:18:33 +00001186end:
1187 return;
1188}
James Chapmanfd558d12010-04-02 06:18:33 +00001189
1190/* When the tunnel is closed, all the attached sessions need to go too.
1191 */
Guillaume Naultd08532b2018-06-25 16:07:23 +02001192static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001193{
1194 int hash;
1195 struct hlist_node *walk;
1196 struct hlist_node *tmp;
1197 struct l2tp_session *session;
1198
1199 BUG_ON(tunnel == NULL);
1200
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001201 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1202 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001203
1204 write_lock_bh(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001205 tunnel->acpt_newsess = false;
James Chapmanfd558d12010-04-02 06:18:33 +00001206 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1207again:
1208 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1209 session = hlist_entry(walk, struct l2tp_session, hlist);
1210
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001211 l2tp_info(session, L2TP_MSG_CONTROL,
1212 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001213
1214 hlist_del_init(&session->hlist);
1215
Guillaume Naultb228a942017-09-22 15:39:24 +02001216 if (test_and_set_bit(0, &session->dead))
1217 goto again;
1218
James Chapmanfd558d12010-04-02 06:18:33 +00001219 write_unlock_bh(&tunnel->hlist_lock);
1220
Tom Parkinf6e16b22013-03-19 06:11:23 +00001221 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001222 l2tp_session_queue_purge(session);
1223
James Chapmanfd558d12010-04-02 06:18:33 +00001224 if (session->session_close != NULL)
1225 (*session->session_close)(session);
1226
Tom Parkin9980d002013-03-19 06:11:13 +00001227 l2tp_session_dec_refcount(session);
1228
James Chapmanfd558d12010-04-02 06:18:33 +00001229 write_lock_bh(&tunnel->hlist_lock);
1230
1231 /* Now restart from the beginning of this hash
1232 * chain. We always remove a session from the
1233 * list so we are guaranteed to make forward
1234 * progress.
1235 */
1236 goto again;
1237 }
1238 }
1239 write_unlock_bh(&tunnel->hlist_lock);
1240}
James Chapmanfd558d12010-04-02 06:18:33 +00001241
Tom Parkin9980d002013-03-19 06:11:13 +00001242/* Tunnel socket destroy hook for UDP encapsulation */
1243static void l2tp_udp_encap_destroy(struct sock *sk)
1244{
James Chapmand00fa9a2018-02-23 17:45:45 +00001245 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1246
1247 if (tunnel)
1248 l2tp_tunnel_delete(tunnel);
Tom Parkin9980d002013-03-19 06:11:13 +00001249}
1250
Tom Parkinf8ccac02013-01-31 23:43:00 +00001251/* Workqueue tunnel deletion function */
1252static void l2tp_tunnel_del_work(struct work_struct *work)
1253{
James Chapmand00fa9a2018-02-23 17:45:45 +00001254 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1255 del_work);
1256 struct sock *sk = tunnel->sock;
1257 struct socket *sock = sk->sk_socket;
James Chapman28f5bfb2018-02-23 17:45:47 +00001258 struct l2tp_net *pn;
Ridge Kennedy12d656a2017-02-22 14:59:49 +13001259
1260 l2tp_tunnel_closeall(tunnel);
1261
James Chapman76a6abd2018-02-23 17:45:43 +00001262 /* If the tunnel socket was created within the kernel, use
Tom Parkin02d13ed2013-03-19 06:11:18 +00001263 * the sk API to release it here.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001264 */
James Chapman76a6abd2018-02-23 17:45:43 +00001265 if (tunnel->fd < 0) {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001266 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001267 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001268 sock_release(sock);
1269 }
Tom Parkin167eb172013-01-31 23:43:03 +00001270 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001271
James Chapman28f5bfb2018-02-23 17:45:47 +00001272 /* Remove the tunnel struct from the tunnel list */
1273 pn = l2tp_pernet(tunnel->l2tp_net);
1274 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1275 list_del_rcu(&tunnel->list);
1276 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1277
James Chapmand00fa9a2018-02-23 17:45:45 +00001278 /* drop initial ref */
1279 l2tp_tunnel_dec_refcount(tunnel);
1280
1281 /* drop workqueue ref */
Alexander Couzens06a15f52015-09-28 11:32:42 +02001282 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001283}
James Chapmanfd558d12010-04-02 06:18:33 +00001284
James Chapman789a4a22010-04-02 06:19:40 +00001285/* Create a socket for the tunnel, if one isn't set up by
1286 * userspace. This is used for static tunnels where there is no
1287 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001288 *
1289 * Since we don't want these sockets to keep a namespace alive by
1290 * themselves, we drop the socket's namespace refcount after creation.
1291 * These sockets are freed when the namespace exits using the pernet
1292 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001293 */
Tom Parkin167eb172013-01-31 23:43:03 +00001294static int l2tp_tunnel_sock_create(struct net *net,
1295 u32 tunnel_id,
1296 u32 peer_tunnel_id,
1297 struct l2tp_tunnel_cfg *cfg,
1298 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001299{
1300 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001301 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001302 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001303
1304 switch (cfg->encap) {
1305 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001306 memset(&udp_conf, 0, sizeof(udp_conf));
1307
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001308#if IS_ENABLED(CONFIG_IPV6)
1309 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001310 udp_conf.family = AF_INET6;
1311 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1312 sizeof(udp_conf.local_ip6));
1313 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1314 sizeof(udp_conf.peer_ip6));
1315 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001316 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001317 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001318 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001319 } else
1320#endif
1321 {
Tom Herbert85644b42014-07-13 19:49:48 -07001322 udp_conf.family = AF_INET;
1323 udp_conf.local_ip = cfg->local_ip;
1324 udp_conf.peer_ip = cfg->peer_ip;
1325 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001326 }
James Chapman789a4a22010-04-02 06:19:40 +00001327
Tom Herbert85644b42014-07-13 19:49:48 -07001328 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1329 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1330
1331 err = udp_sock_create(net, &udp_conf, &sock);
1332 if (err < 0)
1333 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001334
1335 break;
1336
1337 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001338#if IS_ENABLED(CONFIG_IPV6)
1339 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001340 struct sockaddr_l2tpip6 ip6_addr = {0};
1341
Eric W. Biederman26abe142015-05-08 21:10:31 -05001342 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001343 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001344 if (err < 0)
1345 goto out;
1346
James Chapman5dac94e2012-04-29 21:48:55 +00001347 ip6_addr.l2tp_family = AF_INET6;
1348 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1349 sizeof(ip6_addr.l2tp_addr));
1350 ip6_addr.l2tp_conn_id = tunnel_id;
1351 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1352 sizeof(ip6_addr));
1353 if (err < 0)
1354 goto out;
1355
1356 ip6_addr.l2tp_family = AF_INET6;
1357 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1358 sizeof(ip6_addr.l2tp_addr));
1359 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1360 err = kernel_connect(sock,
1361 (struct sockaddr *) &ip6_addr,
1362 sizeof(ip6_addr), 0);
1363 if (err < 0)
1364 goto out;
1365 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001366#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001367 {
Tom Herbert85644b42014-07-13 19:49:48 -07001368 struct sockaddr_l2tpip ip_addr = {0};
1369
Eric W. Biederman26abe142015-05-08 21:10:31 -05001370 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001371 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001372 if (err < 0)
1373 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001374
James Chapman5dac94e2012-04-29 21:48:55 +00001375 ip_addr.l2tp_family = AF_INET;
1376 ip_addr.l2tp_addr = cfg->local_ip;
1377 ip_addr.l2tp_conn_id = tunnel_id;
1378 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1379 sizeof(ip_addr));
1380 if (err < 0)
1381 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001382
James Chapman5dac94e2012-04-29 21:48:55 +00001383 ip_addr.l2tp_family = AF_INET;
1384 ip_addr.l2tp_addr = cfg->peer_ip;
1385 ip_addr.l2tp_conn_id = peer_tunnel_id;
1386 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1387 sizeof(ip_addr), 0);
1388 if (err < 0)
1389 goto out;
1390 }
James Chapman789a4a22010-04-02 06:19:40 +00001391 break;
1392
1393 default:
1394 goto out;
1395 }
1396
1397out:
Tom Parkin167eb172013-01-31 23:43:03 +00001398 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001399 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001400 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001401 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001402 *sockp = NULL;
1403 }
1404
1405 return err;
1406}
1407
Eric Dumazet37159ef2012-09-04 07:18:57 +00001408static struct lock_class_key l2tp_socket_class;
1409
James Chapmanfd558d12010-04-02 06:18:33 +00001410int 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)
1411{
1412 struct l2tp_tunnel *tunnel = NULL;
1413 int err;
James Chapman0d767512010-04-02 06:19:00 +00001414 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001415
James Chapman0d767512010-04-02 06:19:00 +00001416 if (cfg != NULL)
1417 encap = cfg->encap;
1418
James Chapmanfd558d12010-04-02 06:18:33 +00001419 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1420 if (tunnel == NULL) {
1421 err = -ENOMEM;
1422 goto err;
1423 }
1424
1425 tunnel->version = version;
1426 tunnel->tunnel_id = tunnel_id;
1427 tunnel->peer_tunnel_id = peer_tunnel_id;
1428 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1429
1430 tunnel->magic = L2TP_TUNNEL_MAGIC;
1431 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1432 rwlock_init(&tunnel->hlist_lock);
Guillaume Naultf3c66d42017-09-01 17:58:48 +02001433 tunnel->acpt_newsess = true;
James Chapmanfd558d12010-04-02 06:18:33 +00001434
James Chapman0d767512010-04-02 06:19:00 +00001435 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001436 tunnel->debug = cfg->debug;
1437
James Chapman0d767512010-04-02 06:19:00 +00001438 tunnel->encap = encap;
James Chapmanfd558d12010-04-02 06:18:33 +00001439
James Chapmand00fa9a2018-02-23 17:45:45 +00001440 refcount_set(&tunnel->ref_count, 1);
James Chapmand00fa9a2018-02-23 17:45:45 +00001441 tunnel->fd = fd;
1442
Tom Parkinf8ccac02013-01-31 23:43:00 +00001443 /* Init delete workqueue struct */
1444 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1445
James Chapmanfd558d12010-04-02 06:18:33 +00001446 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001447
1448 err = 0;
1449err:
1450 if (tunnelp)
1451 *tunnelp = tunnel;
1452
James Chapmanfd558d12010-04-02 06:18:33 +00001453 return err;
1454}
1455EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1456
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001457static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1458 enum l2tp_encap_type encap)
1459{
1460 if (!net_eq(sock_net(sk), net))
1461 return -EINVAL;
1462
1463 if (sk->sk_type != SOCK_DGRAM)
1464 return -EPROTONOSUPPORT;
1465
1466 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1467 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1468 return -EPROTONOSUPPORT;
1469
1470 if (sk->sk_user_data)
1471 return -EBUSY;
1472
1473 return 0;
1474}
1475
1476int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1477 struct l2tp_tunnel_cfg *cfg)
1478{
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001479 struct l2tp_tunnel *tunnel_walk;
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001480 struct l2tp_net *pn;
1481 struct socket *sock;
1482 struct sock *sk;
1483 int ret;
1484
1485 if (tunnel->fd < 0) {
1486 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1487 tunnel->peer_tunnel_id, cfg,
1488 &sock);
1489 if (ret < 0)
1490 goto err;
1491 } else {
1492 sock = sockfd_lookup(tunnel->fd, &ret);
1493 if (!sock)
1494 goto err;
1495
1496 ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
1497 if (ret < 0)
1498 goto err_sock;
1499 }
1500
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001501 tunnel->l2tp_net = net;
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001502 pn = l2tp_pernet(net);
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001503
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001504 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001505 list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
1506 if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
1507 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1508
1509 ret = -EEXIST;
1510 goto err_sock;
1511 }
1512 }
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001513 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1514 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1515
Xin Long79272fc2018-11-13 01:08:25 +08001516 sk = sock->sk;
1517 sock_hold(sk);
1518 tunnel->sock = sk;
1519
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001520 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1521 struct udp_tunnel_sock_cfg udp_cfg = {
1522 .sk_user_data = tunnel,
1523 .encap_type = UDP_ENCAP_L2TPINUDP,
1524 .encap_rcv = l2tp_udp_encap_recv,
1525 .encap_destroy = l2tp_udp_encap_destroy,
1526 };
1527
1528 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1529 } else {
1530 sk->sk_user_data = tunnel;
1531 }
1532
1533 tunnel->old_sk_destruct = sk->sk_destruct;
1534 sk->sk_destruct = &l2tp_tunnel_destruct;
1535 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
1536 "l2tp_sock");
1537 sk->sk_allocation = GFP_ATOMIC;
1538
1539 if (tunnel->fd >= 0)
1540 sockfd_put(sock);
1541
1542 return 0;
1543
1544err_sock:
Guillaume Naultf6cd6512018-04-10 21:01:13 +02001545 if (tunnel->fd < 0)
1546 sock_release(sock);
1547 else
1548 sockfd_put(sock);
Guillaume Nault6b9f3422018-04-10 21:01:12 +02001549err:
1550 return ret;
1551}
1552EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1553
James Chapman309795f2010-04-02 06:19:10 +00001554/* This function is used by the netlink TUNNEL_DELETE command.
1555 */
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001556void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
James Chapman309795f2010-04-02 06:19:10 +00001557{
Sabrina Dubroca62b982e2017-09-26 16:16:43 +02001558 if (!test_and_set_bit(0, &tunnel->dead)) {
1559 l2tp_tunnel_inc_refcount(tunnel);
1560 queue_work(l2tp_wq, &tunnel->del_work);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001561 }
James Chapman309795f2010-04-02 06:19:10 +00001562}
1563EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1564
James Chapmanfd558d12010-04-02 06:18:33 +00001565/* Really kill the session.
1566 */
1567void l2tp_session_free(struct l2tp_session *session)
1568{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001569 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001570
Reshetova, Elenafbea9e02017-07-04 15:52:57 +03001571 BUG_ON(refcount_read(&session->ref_count) != 0);
James Chapmanfd558d12010-04-02 06:18:33 +00001572
Tom Parkinf6e16b22013-03-19 06:11:23 +00001573 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001574 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001575 l2tp_tunnel_dec_refcount(tunnel);
1576 }
1577
1578 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001579}
1580EXPORT_SYMBOL_GPL(l2tp_session_free);
1581
Tom Parkinf6e16b22013-03-19 06:11:23 +00001582/* Remove an l2tp session from l2tp_core's hash lists.
1583 * Provides a tidyup interface for pseudowire code which can't just route all
1584 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1585 * callback.
1586 */
1587void __l2tp_session_unhash(struct l2tp_session *session)
1588{
1589 struct l2tp_tunnel *tunnel = session->tunnel;
1590
1591 /* Remove the session from core hashes */
1592 if (tunnel) {
1593 /* Remove from the per-tunnel hash */
1594 write_lock_bh(&tunnel->hlist_lock);
1595 hlist_del_init(&session->hlist);
1596 write_unlock_bh(&tunnel->hlist_lock);
1597
1598 /* For L2TPv3 we have a per-net hash: remove from there, too */
1599 if (tunnel->version != L2TP_HDR_VER_2) {
1600 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1601 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1602 hlist_del_init_rcu(&session->global_hlist);
1603 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1604 synchronize_rcu();
1605 }
1606 }
1607}
1608EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1609
James Chapman309795f2010-04-02 06:19:10 +00001610/* This function is used by the netlink SESSION_DELETE command and by
1611 pseudowire modules.
1612 */
1613int l2tp_session_delete(struct l2tp_session *session)
1614{
Guillaume Naultb228a942017-09-22 15:39:24 +02001615 if (test_and_set_bit(0, &session->dead))
1616 return 0;
1617
Tom Parkinf6e16b22013-03-19 06:11:23 +00001618 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001619 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001620 if (session->session_close != NULL)
1621 (*session->session_close)(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001622
James Chapman309795f2010-04-02 06:19:10 +00001623 l2tp_session_dec_refcount(session);
Guillaume Naulta4346212017-10-31 17:36:42 +01001624
James Chapman309795f2010-04-02 06:19:10 +00001625 return 0;
1626}
1627EXPORT_SYMBOL_GPL(l2tp_session_delete);
1628
James Chapmanf7faffa2010-04-02 06:18:49 +00001629/* We come here whenever a session's send_seq, cookie_len or
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001630 * l2specific_type parameters are set.
James Chapmanf7faffa2010-04-02 06:18:49 +00001631 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001632void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001633{
1634 if (version == L2TP_HDR_VER_2) {
1635 session->hdr_len = 6;
1636 if (session->send_seq)
1637 session->hdr_len += 4;
1638 } else {
Lorenzo Bianconi62e7b6a2018-01-16 23:01:55 +01001639 session->hdr_len = 4 + session->cookie_len;
1640 session->hdr_len += l2tp_get_l2specific_len(session);
James Chapman0d767512010-04-02 06:19:00 +00001641 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1642 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001643 }
1644
1645}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001646EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001647
James Chapmanfd558d12010-04-02 06:18:33 +00001648struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1649{
1650 struct l2tp_session *session;
1651
1652 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1653 if (session != NULL) {
1654 session->magic = L2TP_SESSION_MAGIC;
1655 session->tunnel = tunnel;
1656
1657 session->session_id = session_id;
1658 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001659 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001660 if (tunnel->version == L2TP_HDR_VER_2)
1661 session->nr_max = 0xffff;
1662 else
1663 session->nr_max = 0xffffff;
1664 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001665 session->nr_oos_count_max = 4;
1666
1667 /* Use NR of first received packet */
1668 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001669
1670 sprintf(&session->name[0], "sess %u/%u",
1671 tunnel->tunnel_id, session->session_id);
1672
1673 skb_queue_head_init(&session->reorder_q);
1674
1675 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001676 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001677
1678 /* Inherit debug options from tunnel */
1679 session->debug = tunnel->debug;
1680
1681 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001682 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001683 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001684 session->send_seq = cfg->send_seq;
1685 session->recv_seq = cfg->recv_seq;
1686 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001687 session->reorder_timeout = cfg->reorder_timeout;
James Chapmanf7faffa2010-04-02 06:18:49 +00001688 session->l2specific_type = cfg->l2specific_type;
James Chapmanf7faffa2010-04-02 06:18:49 +00001689 session->cookie_len = cfg->cookie_len;
1690 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1691 session->peer_cookie_len = cfg->peer_cookie_len;
1692 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001693 }
1694
James Chapmanf7faffa2010-04-02 06:18:49 +00001695 if (tunnel->version == L2TP_HDR_VER_2)
1696 session->build_header = l2tp_build_l2tpv2_header;
1697 else
1698 session->build_header = l2tp_build_l2tpv3_header;
1699
1700 l2tp_session_set_header_len(session, tunnel->version);
1701
Guillaume Nault9ee369a2017-08-25 16:22:17 +02001702 refcount_set(&session->ref_count, 1);
1703
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001704 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001705 }
1706
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001707 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001708}
1709EXPORT_SYMBOL_GPL(l2tp_session_create);
1710
1711/*****************************************************************************
1712 * Init and cleanup
1713 *****************************************************************************/
1714
1715static __net_init int l2tp_init_net(struct net *net)
1716{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001717 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001718 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001719
James Chapmanfd558d12010-04-02 06:18:33 +00001720 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001721 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001722
James Chapmanf7faffa2010-04-02 06:18:49 +00001723 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1724 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1725
James Chapmane02d4942010-04-02 06:19:16 +00001726 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001727
James Chapmanfd558d12010-04-02 06:18:33 +00001728 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001729}
1730
Tom Parkin167eb172013-01-31 23:43:03 +00001731static __net_exit void l2tp_exit_net(struct net *net)
1732{
1733 struct l2tp_net *pn = l2tp_pernet(net);
1734 struct l2tp_tunnel *tunnel = NULL;
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001735 int hash;
Tom Parkin167eb172013-01-31 23:43:03 +00001736
1737 rcu_read_lock_bh();
1738 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Jiri Slaby4dc12ff2017-10-25 15:57:55 +02001739 l2tp_tunnel_delete(tunnel);
Tom Parkin167eb172013-01-31 23:43:03 +00001740 }
1741 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001742
YueHaibing2f11aba2019-05-06 22:44:04 +08001743 if (l2tp_wq)
1744 flush_workqueue(l2tp_wq);
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001745 rcu_barrier();
Vasily Averin1e7af3b2017-11-12 22:30:31 +03001746
1747 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1748 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
Tom Parkin167eb172013-01-31 23:43:03 +00001749}
1750
James Chapmanfd558d12010-04-02 06:18:33 +00001751static struct pernet_operations l2tp_net_ops = {
1752 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001753 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001754 .id = &l2tp_net_id,
1755 .size = sizeof(struct l2tp_net),
1756};
1757
1758static int __init l2tp_init(void)
1759{
1760 int rc = 0;
1761
1762 rc = register_pernet_device(&l2tp_net_ops);
1763 if (rc)
1764 goto out;
1765
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001766 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001767 if (!l2tp_wq) {
1768 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001769 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001770 rc = -ENOMEM;
1771 goto out;
1772 }
1773
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001774 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001775
1776out:
1777 return rc;
1778}
1779
1780static void __exit l2tp_exit(void)
1781{
1782 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001783 if (l2tp_wq) {
1784 destroy_workqueue(l2tp_wq);
1785 l2tp_wq = NULL;
1786 }
James Chapmanfd558d12010-04-02 06:18:33 +00001787}
1788
1789module_init(l2tp_init);
1790module_exit(l2tp_exit);
1791
1792MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1793MODULE_DESCRIPTION("L2TP core");
1794MODULE_LICENSE("GPL");
1795MODULE_VERSION(L2TP_DRV_VERSION);