blob: 70e61c9e3614232bd4467c42e69cfdd27b71ab60 [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 Wend1ab05e2019-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
102static atomic_t l2tp_tunnel_count;
103static atomic_t l2tp_session_count;
Tom Parkinf8ccac02013-01-31 23:43:00 +0000104static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000105
106/* per-net private data for this module */
107static unsigned int l2tp_net_id;
108struct l2tp_net {
109 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000110 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000111 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000112 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000113};
114
stephen hemmingerfc130842010-10-21 07:50:46 +0000115static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
stephen hemmingerfc130842010-10-21 07:50:46 +0000116
David S. Miller8d8a51e2013-10-08 15:44:26 -0400117static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
118{
119 return sk->sk_user_data;
120}
121
James Chapmanfd558d12010-04-02 06:18:33 +0000122static inline struct l2tp_net *l2tp_pernet(struct net *net)
123{
124 BUG_ON(!net);
125
126 return net_generic(net, l2tp_net_id);
127}
128
stephen hemmingerfc130842010-10-21 07:50:46 +0000129/* Tunnel reference counts. Incremented per session that is added to
130 * the tunnel.
131 */
132static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
133{
134 atomic_inc(&tunnel->ref_count);
135}
136
137static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
138{
139 if (atomic_dec_and_test(&tunnel->ref_count))
140 l2tp_tunnel_free(tunnel);
141}
142#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000143#define l2tp_tunnel_inc_refcount(_t) \
144do { \
145 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
146 __func__, __LINE__, (_t)->name, \
147 atomic_read(&_t->ref_count)); \
148 l2tp_tunnel_inc_refcount_1(_t); \
149} while (0)
Andy Zhou29abe2f2014-09-03 13:16:54 -0700150#define l2tp_tunnel_dec_refcount(_t) \
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000151do { \
152 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
153 __func__, __LINE__, (_t)->name, \
154 atomic_read(&_t->ref_count)); \
155 l2tp_tunnel_dec_refcount_1(_t); \
156} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000157#else
158#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
159#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
160#endif
161
James Chapmanf7faffa2010-04-02 06:18:49 +0000162/* Session hash global list for L2TPv3.
163 * The session_id SHOULD be random according to RFC3931, but several
164 * L2TP implementations use incrementing session_ids. So we do a real
165 * hash on the session_id, rather than a simple bitmask.
166 */
167static inline struct hlist_head *
168l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
169{
170 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
171
172}
173
Tom Parkin80d84ef2013-01-22 05:13:48 +0000174/* Lookup the tunnel socket, possibly involving the fs code if the socket is
175 * owned by userspace. A struct sock returned from this function must be
176 * released using l2tp_tunnel_sock_put once you're done with it.
177 */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800178static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000179{
180 int err = 0;
181 struct socket *sock = NULL;
182 struct sock *sk = NULL;
183
184 if (!tunnel)
185 goto out;
186
187 if (tunnel->fd >= 0) {
188 /* Socket is owned by userspace, who might be in the process
189 * of closing it. Look the socket up using the fd to ensure
190 * consistency.
191 */
192 sock = sockfd_lookup(tunnel->fd, &err);
193 if (sock)
194 sk = sock->sk;
195 } else {
196 /* Socket is owned by kernelspace */
197 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000198 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000199 }
200
201out:
202 return sk;
203}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000204
205/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800206static void l2tp_tunnel_sock_put(struct sock *sk)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000207{
208 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
209 if (tunnel) {
210 if (tunnel->fd >= 0) {
211 /* Socket is owned by userspace */
212 sockfd_put(sk->sk_socket);
213 }
214 sock_put(sk);
215 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000216 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000217}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000218
James Chapmanf7faffa2010-04-02 06:18:49 +0000219/* Lookup a session by id in the global session list
220 */
221static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
222{
223 struct l2tp_net *pn = l2tp_pernet(net);
224 struct hlist_head *session_list =
225 l2tp_session_id_hash_2(pn, session_id);
226 struct l2tp_session *session;
James Chapmanf7faffa2010-04-02 06:18:49 +0000227
James Chapmane02d4942010-04-02 06:19:16 +0000228 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800229 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000230 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000231 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000232 return session;
233 }
234 }
James Chapmane02d4942010-04-02 06:19:16 +0000235 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000236
237 return NULL;
238}
239
James Chapmanfd558d12010-04-02 06:18:33 +0000240/* Session hash list.
241 * The session_id SHOULD be random according to RFC2661, but several
242 * L2TP implementations (Cisco and Microsoft) use incrementing
243 * session_ids. So we do a real hash on the session_id, rather than a
244 * simple bitmask.
245 */
246static inline struct hlist_head *
247l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
248{
249 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
250}
251
252/* Lookup a session by id
253 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000254struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000255{
James Chapmanf7faffa2010-04-02 06:18:49 +0000256 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000257 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000258
James Chapmanf7faffa2010-04-02 06:18:49 +0000259 /* In L2TPv3, session_ids are unique over all tunnels and we
260 * sometimes need to look them up before we know the
261 * tunnel.
262 */
263 if (tunnel == NULL)
264 return l2tp_session_find_2(net, session_id);
265
266 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000267 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800268 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000269 if (session->session_id == session_id) {
270 read_unlock_bh(&tunnel->hlist_lock);
271 return session;
272 }
273 }
274 read_unlock_bh(&tunnel->hlist_lock);
275
276 return NULL;
277}
278EXPORT_SYMBOL_GPL(l2tp_session_find);
279
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200280/* Like l2tp_session_find() but takes a reference on the returned session.
281 * Optionally calls session->ref() too if do_ref is true.
282 */
283struct l2tp_session *l2tp_session_get(struct net *net,
284 struct l2tp_tunnel *tunnel,
285 u32 session_id, bool do_ref)
286{
287 struct hlist_head *session_list;
288 struct l2tp_session *session;
289
290 if (!tunnel) {
291 struct l2tp_net *pn = l2tp_pernet(net);
292
293 session_list = l2tp_session_id_hash_2(pn, session_id);
294
295 rcu_read_lock_bh();
296 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
297 if (session->session_id == session_id) {
298 l2tp_session_inc_refcount(session);
299 if (do_ref && session->ref)
300 session->ref(session);
301 rcu_read_unlock_bh();
302
303 return session;
304 }
305 }
306 rcu_read_unlock_bh();
307
308 return NULL;
309 }
310
311 session_list = l2tp_session_id_hash(tunnel, session_id);
312 read_lock_bh(&tunnel->hlist_lock);
313 hlist_for_each_entry(session, session_list, hlist) {
314 if (session->session_id == session_id) {
315 l2tp_session_inc_refcount(session);
316 if (do_ref && session->ref)
317 session->ref(session);
318 read_unlock_bh(&tunnel->hlist_lock);
319
320 return session;
321 }
322 }
323 read_unlock_bh(&tunnel->hlist_lock);
324
325 return NULL;
326}
327EXPORT_SYMBOL_GPL(l2tp_session_get);
328
Guillaume Naultb7902602017-04-03 12:03:13 +0200329struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
330 bool do_ref)
James Chapmanfd558d12010-04-02 06:18:33 +0000331{
332 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000333 struct l2tp_session *session;
334 int count = 0;
335
336 read_lock_bh(&tunnel->hlist_lock);
337 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800338 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000339 if (++count > nth) {
Guillaume Naultb7902602017-04-03 12:03:13 +0200340 l2tp_session_inc_refcount(session);
341 if (do_ref && session->ref)
342 session->ref(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000343 read_unlock_bh(&tunnel->hlist_lock);
344 return session;
345 }
346 }
347 }
348
349 read_unlock_bh(&tunnel->hlist_lock);
350
351 return NULL;
352}
Guillaume Naultb7902602017-04-03 12:03:13 +0200353EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000354
James Chapman309795f2010-04-02 06:19:10 +0000355/* Lookup a session by interface name.
356 * This is very inefficient but is only used by management interfaces.
357 */
Guillaume Nault08cb8e52017-03-31 13:02:30 +0200358struct l2tp_session *l2tp_session_get_by_ifname(struct net *net, char *ifname,
359 bool do_ref)
James Chapman309795f2010-04-02 06:19:10 +0000360{
361 struct l2tp_net *pn = l2tp_pernet(net);
362 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000363 struct l2tp_session *session;
364
James Chapmane02d4942010-04-02 06:19:16 +0000365 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000366 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800367 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000368 if (!strcmp(session->ifname, ifname)) {
Guillaume Nault08cb8e52017-03-31 13:02:30 +0200369 l2tp_session_inc_refcount(session);
370 if (do_ref && session->ref)
371 session->ref(session);
James Chapmane02d4942010-04-02 06:19:16 +0000372 rcu_read_unlock_bh();
Guillaume Nault08cb8e52017-03-31 13:02:30 +0200373
James Chapman309795f2010-04-02 06:19:10 +0000374 return session;
375 }
376 }
377 }
378
James Chapmane02d4942010-04-02 06:19:16 +0000379 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000380
381 return NULL;
382}
Guillaume Nault08cb8e52017-03-31 13:02:30 +0200383EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
James Chapman309795f2010-04-02 06:19:10 +0000384
Guillaume Naultd9face62017-03-31 13:02:27 +0200385static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel,
386 struct l2tp_session *session)
387{
388 struct l2tp_session *session_walk;
389 struct hlist_head *g_head;
390 struct hlist_head *head;
391 struct l2tp_net *pn;
392
393 head = l2tp_session_id_hash(tunnel, session->session_id);
394
395 write_lock_bh(&tunnel->hlist_lock);
396 hlist_for_each_entry(session_walk, head, hlist)
397 if (session_walk->session_id == session->session_id)
398 goto exist;
399
400 if (tunnel->version == L2TP_HDR_VER_3) {
401 pn = l2tp_pernet(tunnel->l2tp_net);
402 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
403 session->session_id);
404
405 spin_lock_bh(&pn->l2tp_session_hlist_lock);
406 hlist_for_each_entry(session_walk, g_head, global_hlist)
407 if (session_walk->session_id == session->session_id)
408 goto exist_glob;
409
410 hlist_add_head_rcu(&session->global_hlist, g_head);
411 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
412 }
413
414 hlist_add_head(&session->hlist, head);
415 write_unlock_bh(&tunnel->hlist_lock);
416
417 return 0;
418
419exist_glob:
420 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
421exist:
422 write_unlock_bh(&tunnel->hlist_lock);
423
424 return -EEXIST;
425}
426
James Chapmanfd558d12010-04-02 06:18:33 +0000427/* Lookup a tunnel by id
428 */
429struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
430{
431 struct l2tp_tunnel *tunnel;
432 struct l2tp_net *pn = l2tp_pernet(net);
433
James Chapmane02d4942010-04-02 06:19:16 +0000434 rcu_read_lock_bh();
435 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000436 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000437 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000438 return tunnel;
439 }
440 }
James Chapmane02d4942010-04-02 06:19:16 +0000441 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000442
443 return NULL;
444}
445EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
446
447struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
448{
449 struct l2tp_net *pn = l2tp_pernet(net);
450 struct l2tp_tunnel *tunnel;
451 int count = 0;
452
James Chapmane02d4942010-04-02 06:19:16 +0000453 rcu_read_lock_bh();
454 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000455 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000456 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000457 return tunnel;
458 }
459 }
460
James Chapmane02d4942010-04-02 06:19:16 +0000461 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000462
463 return NULL;
464}
465EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
466
467/*****************************************************************************
468 * Receive data handling
469 *****************************************************************************/
470
471/* Queue a skb in order. We come here only if the skb has an L2TP sequence
472 * number.
473 */
474static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
475{
476 struct sk_buff *skbp;
477 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000478 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000479
480 spin_lock_bh(&session->reorder_q.lock);
481 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
482 if (L2TP_SKB_CB(skbp)->ns > ns) {
483 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000484 l2tp_dbg(session, L2TP_MSG_SEQ,
485 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
486 session->name, ns, L2TP_SKB_CB(skbp)->ns,
487 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000488 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000489 goto out;
490 }
491 }
492
493 __skb_queue_tail(&session->reorder_q, skb);
494
495out:
496 spin_unlock_bh(&session->reorder_q.lock);
497}
498
499/* Dequeue a single skb.
500 */
501static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
502{
503 struct l2tp_tunnel *tunnel = session->tunnel;
504 int length = L2TP_SKB_CB(skb)->length;
505
506 /* We're about to requeue the skb, so return resources
507 * to its current owner (a socket receive buffer).
508 */
509 skb_orphan(skb);
510
Tom Parkin7b7c0712013-03-19 06:11:22 +0000511 atomic_long_inc(&tunnel->stats.rx_packets);
512 atomic_long_add(length, &tunnel->stats.rx_bytes);
513 atomic_long_inc(&session->stats.rx_packets);
514 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000515
516 if (L2TP_SKB_CB(skb)->has_seq) {
517 /* Bump our Nr */
518 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100519 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000520
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000521 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
522 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000523 }
524
525 /* call private receive handler */
526 if (session->recv_skb != NULL)
527 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
528 else
529 kfree_skb(skb);
530
531 if (session->deref)
532 (*session->deref)(session);
533}
534
535/* Dequeue skbs from the session's reorder_q, subject to packet order.
536 * Skbs that have been in the queue for too long are simply discarded.
537 */
538static void l2tp_recv_dequeue(struct l2tp_session *session)
539{
540 struct sk_buff *skb;
541 struct sk_buff *tmp;
542
543 /* If the pkt at the head of the queue has the nr that we
544 * expect to send up next, dequeue it and any other
545 * in-sequence packets behind it.
546 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000547start:
James Chapmanfd558d12010-04-02 06:18:33 +0000548 spin_lock_bh(&session->reorder_q.lock);
549 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
550 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000551 atomic_long_inc(&session->stats.rx_seq_discards);
552 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000553 l2tp_dbg(session, L2TP_MSG_SEQ,
554 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
555 session->name, L2TP_SKB_CB(skb)->ns,
556 L2TP_SKB_CB(skb)->length, session->nr,
557 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000558 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000559 __skb_unlink(skb, &session->reorder_q);
560 kfree_skb(skb);
561 if (session->deref)
562 (*session->deref)(session);
563 continue;
564 }
565
566 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000567 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000568 l2tp_dbg(session, L2TP_MSG_SEQ,
569 "%s: advancing nr to next pkt: %u -> %u",
570 session->name, session->nr,
571 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000572 session->reorder_skip = 0;
573 session->nr = L2TP_SKB_CB(skb)->ns;
574 }
James Chapmanfd558d12010-04-02 06:18:33 +0000575 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000576 l2tp_dbg(session, L2TP_MSG_SEQ,
577 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
578 session->name, L2TP_SKB_CB(skb)->ns,
579 L2TP_SKB_CB(skb)->length, session->nr,
580 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000581 goto out;
582 }
583 }
584 __skb_unlink(skb, &session->reorder_q);
585
586 /* Process the skb. We release the queue lock while we
587 * do so to let other contexts process the queue.
588 */
589 spin_unlock_bh(&session->reorder_q.lock);
590 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000591 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000592 }
593
594out:
595 spin_unlock_bh(&session->reorder_q.lock);
596}
597
James Chapman8a1631d2013-07-02 20:28:59 +0100598static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
599{
600 u32 nws;
601
602 if (nr >= session->nr)
603 nws = nr - session->nr;
604 else
605 nws = (session->nr_max + 1) - (session->nr - nr);
606
607 return nws < session->nr_window_size;
608}
609
James Chapmanb6dc01a2013-07-02 20:28:58 +0100610/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
611 * acceptable, else non-zero.
612 */
613static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
614{
James Chapman8a1631d2013-07-02 20:28:59 +0100615 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
616 /* Packet sequence number is outside allowed window.
617 * Discard it.
618 */
619 l2tp_dbg(session, L2TP_MSG_SEQ,
620 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
621 session->name, L2TP_SKB_CB(skb)->ns,
622 L2TP_SKB_CB(skb)->length, session->nr);
623 goto discard;
624 }
625
James Chapmanb6dc01a2013-07-02 20:28:58 +0100626 if (session->reorder_timeout != 0) {
627 /* Packet reordering enabled. Add skb to session's
628 * reorder queue, in order of ns.
629 */
630 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100631 goto out;
632 }
633
634 /* Packet reordering disabled. Discard out-of-sequence packets, while
635 * tracking the number if in-sequence packets after the first OOS packet
636 * is seen. After nr_oos_count_max in-sequence packets, reset the
637 * sequence number to re-enable packet reception.
638 */
639 if (L2TP_SKB_CB(skb)->ns == session->nr) {
640 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100641 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100642 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
643 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
644
645 if (nr_oos == nr_next)
646 session->nr_oos_count++;
647 else
648 session->nr_oos_count = 0;
649
650 session->nr_oos = nr_oos;
651 if (session->nr_oos_count > session->nr_oos_count_max) {
652 session->reorder_skip = 1;
653 l2tp_dbg(session, L2TP_MSG_SEQ,
654 "%s: %d oos packets received. Resetting sequence numbers\n",
655 session->name, session->nr_oos_count);
656 }
657 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100658 atomic_long_inc(&session->stats.rx_seq_discards);
659 l2tp_dbg(session, L2TP_MSG_SEQ,
660 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
661 session->name, L2TP_SKB_CB(skb)->ns,
662 L2TP_SKB_CB(skb)->length, session->nr,
663 skb_queue_len(&session->reorder_q));
664 goto discard;
665 }
666 skb_queue_tail(&session->reorder_q, skb);
667 }
668
James Chapmana0dbd822013-07-02 20:29:00 +0100669out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100670 return 0;
671
672discard:
673 return 1;
674}
675
James Chapmanf7faffa2010-04-02 06:18:49 +0000676/* Do receive processing of L2TP data frames. We handle both L2TPv2
677 * and L2TPv3 data frames here.
678 *
679 * L2TPv2 Data Message Header
680 *
681 * 0 1 2 3
682 * 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
683 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
684 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
685 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 * | Tunnel ID | Session ID |
687 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
688 * | Ns (opt) | Nr (opt) |
689 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
690 * | Offset Size (opt) | Offset pad... (opt)
691 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
692 *
693 * Data frames are marked by T=0. All other fields are the same as
694 * those in L2TP control frames.
695 *
696 * L2TPv3 Data Message Header
697 *
698 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
699 * | L2TP Session Header |
700 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
701 * | L2-Specific Sublayer |
702 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
703 * | Tunnel Payload ...
704 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
705 *
706 * L2TPv3 Session Header Over IP
707 *
708 * 0 1 2 3
709 * 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
710 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
711 * | Session ID |
712 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
713 * | Cookie (optional, maximum 64 bits)...
714 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
715 * |
716 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
717 *
718 * L2TPv3 L2-Specific Sublayer Format
719 *
720 * 0 1 2 3
721 * 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
722 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
723 * |x|S|x|x|x|x|x|x| Sequence Number |
724 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
725 *
726 * Cookie value, sublayer format and offset (pad) are negotiated with
727 * the peer when the session is set up. Unlike L2TPv2, we do not need
728 * to parse the packet header to determine if optional fields are
729 * present.
730 *
731 * Caller must already have parsed the frame and determined that it is
732 * a data (not control) frame before coming here. Fields up to the
733 * session-id have already been parsed and ptr points to the data
734 * after the session-id.
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200735 *
736 * session->ref() must have been called prior to l2tp_recv_common().
737 * session->deref() will be called automatically after skb is processed.
James Chapmanfd558d12010-04-02 06:18:33 +0000738 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000739void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
740 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
741 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000742{
James Chapmanf7faffa2010-04-02 06:18:49 +0000743 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000744 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000745 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000746
James Chapmanf7faffa2010-04-02 06:18:49 +0000747 /* Parse and check optional cookie */
748 if (session->peer_cookie_len > 0) {
749 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000750 l2tp_info(tunnel, L2TP_MSG_DATA,
751 "%s: cookie mismatch (%u/%u). Discarding.\n",
752 tunnel->name, tunnel->tunnel_id,
753 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000754 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000755 goto discard;
756 }
757 ptr += session->peer_cookie_len;
758 }
759
James Chapmanfd558d12010-04-02 06:18:33 +0000760 /* Handle the optional sequence numbers. Sequence numbers are
761 * in different places for L2TPv2 and L2TPv3.
762 *
763 * If we are the LAC, enable/disable sequence numbers under
764 * the control of the LNS. If no sequence numbers present but
765 * we were expecting them, discard frame.
766 */
767 ns = nr = 0;
768 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000769 if (tunnel->version == L2TP_HDR_VER_2) {
770 if (hdrflags & L2TP_HDRFLAG_S) {
771 ns = ntohs(*(__be16 *) ptr);
772 ptr += 2;
773 nr = ntohs(*(__be16 *) ptr);
774 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000775
James Chapmanf7faffa2010-04-02 06:18:49 +0000776 /* Store L2TP info in the skb */
777 L2TP_SKB_CB(skb)->ns = ns;
778 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000779
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000780 l2tp_dbg(session, L2TP_MSG_SEQ,
781 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
782 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000783 }
784 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
785 u32 l2h = ntohl(*(__be32 *) ptr);
786
787 if (l2h & 0x40000000) {
788 ns = l2h & 0x00ffffff;
789
790 /* Store L2TP info in the skb */
791 L2TP_SKB_CB(skb)->ns = ns;
792 L2TP_SKB_CB(skb)->has_seq = 1;
793
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000794 l2tp_dbg(session, L2TP_MSG_SEQ,
795 "%s: recv data ns=%u, session nr=%u\n",
796 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000797 }
Lorenzo Bianconi592bde82018-01-16 23:01:55 +0100798 ptr += 4;
James Chapmanfd558d12010-04-02 06:18:33 +0000799 }
800
801 if (L2TP_SKB_CB(skb)->has_seq) {
802 /* Received a packet with sequence numbers. If we're the LNS,
803 * check if we sre sending sequence numbers and if not,
804 * configure it so.
805 */
806 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000807 l2tp_info(session, L2TP_MSG_SEQ,
808 "%s: requested to enable seq numbers by LNS\n",
809 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000810 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000811 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000812 }
813 } else {
814 /* No sequence numbers.
815 * If user has configured mandatory sequence numbers, discard.
816 */
817 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000818 l2tp_warn(session, L2TP_MSG_SEQ,
819 "%s: recv data has no seq numbers when required. Discarding.\n",
820 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000821 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000822 goto discard;
823 }
824
825 /* If we're the LAC and we're sending sequence numbers, the
826 * LNS has requested that we no longer send sequence numbers.
827 * If we're the LNS and we're sending sequence numbers, the
828 * LAC is broken. Discard the frame.
829 */
830 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000831 l2tp_info(session, L2TP_MSG_SEQ,
832 "%s: requested to disable seq numbers by LNS\n",
833 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000834 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000835 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000836 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000837 l2tp_warn(session, L2TP_MSG_SEQ,
838 "%s: recv data has no seq numbers when required. Discarding.\n",
839 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000840 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000841 goto discard;
842 }
843 }
844
James Chapman65b05d02018-01-03 22:48:06 +0000845 /* Session data offset is defined only for L2TPv2 and is
846 * indicated by an optional 16-bit value in the header.
James Chapmanf7faffa2010-04-02 06:18:49 +0000847 */
848 if (tunnel->version == L2TP_HDR_VER_2) {
849 /* If offset bit set, skip it. */
850 if (hdrflags & L2TP_HDRFLAG_O) {
851 offset = ntohs(*(__be16 *)ptr);
852 ptr += 2 + offset;
853 }
James Chapman65b05d02018-01-03 22:48:06 +0000854 }
James Chapmanfd558d12010-04-02 06:18:33 +0000855
856 offset = ptr - optr;
857 if (!pskb_may_pull(skb, offset))
858 goto discard;
859
860 __skb_pull(skb, offset);
861
862 /* If caller wants to process the payload before we queue the
863 * packet, do so now.
864 */
865 if (payload_hook)
866 if ((*payload_hook)(skb))
867 goto discard;
868
869 /* Prepare skb for adding to the session's reorder_q. Hold
870 * packets for max reorder_timeout or 1 second if not
871 * reordering.
872 */
873 L2TP_SKB_CB(skb)->length = length;
874 L2TP_SKB_CB(skb)->expires = jiffies +
875 (session->reorder_timeout ? session->reorder_timeout : HZ);
876
877 /* Add packet to the session's receive queue. Reordering is done here, if
878 * enabled. Saved L2TP protocol info is stored in skb->sb[].
879 */
880 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100881 if (l2tp_recv_data_seq(session, skb))
882 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000883 } else {
884 /* No sequence numbers. Add the skb to the tail of the
885 * reorder queue. This ensures that it will be
886 * delivered after all previous sequenced skbs.
887 */
888 skb_queue_tail(&session->reorder_q, skb);
889 }
890
891 /* Try to dequeue as many skbs from reorder_q as we can. */
892 l2tp_recv_dequeue(session);
893
James Chapmanf7faffa2010-04-02 06:18:49 +0000894 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000895
896discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000897 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000898 kfree_skb(skb);
899
900 if (session->deref)
901 (*session->deref)(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000902}
903EXPORT_SYMBOL(l2tp_recv_common);
904
Tom Parkin48f72f92013-03-19 06:11:19 +0000905/* Drop skbs from the session's reorder_q
906 */
907int l2tp_session_queue_purge(struct l2tp_session *session)
908{
909 struct sk_buff *skb = NULL;
910 BUG_ON(!session);
911 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
912 while ((skb = skb_dequeue(&session->reorder_q))) {
913 atomic_long_inc(&session->stats.rx_errors);
914 kfree_skb(skb);
915 if (session->deref)
916 (*session->deref)(session);
917 }
918 return 0;
919}
920EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
921
James Chapmanf7faffa2010-04-02 06:18:49 +0000922/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
923 * here. The skb is not on a list when we get here.
924 * Returns 0 if the packet was a data packet and was successfully passed on.
925 * Returns 1 if the packet was not a good data packet and could not be
926 * forwarded. All such packets are passed up to userspace to deal with.
927 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000928static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
929 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000930{
931 struct l2tp_session *session = NULL;
932 unsigned char *ptr, *optr;
933 u16 hdrflags;
934 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000935 u16 version;
936 int length;
937
Tom Herbert58d60852014-05-07 16:52:48 -0700938 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000939
940 /* UDP always verifies the packet length. */
941 __skb_pull(skb, sizeof(struct udphdr));
942
943 /* Short packet? */
Jacob Wend1ab05e2019-01-31 15:18:56 +0800944 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000945 l2tp_info(tunnel, L2TP_MSG_DATA,
946 "%s: recv short packet (len=%d)\n",
947 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000948 goto error;
949 }
950
James Chapmanf7faffa2010-04-02 06:18:49 +0000951 /* Trace packet contents, if enabled */
952 if (tunnel->debug & L2TP_MSG_DATA) {
953 length = min(32u, skb->len);
954 if (!pskb_may_pull(skb, length))
955 goto error;
956
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000957 pr_debug("%s: recv\n", tunnel->name);
958 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000959 }
960
Eric Dumazete50e7052011-11-08 13:59:44 -0500961 /* Point to L2TP header */
962 optr = ptr = skb->data;
963
James Chapmanf7faffa2010-04-02 06:18:49 +0000964 /* Get L2TP header flags */
965 hdrflags = ntohs(*(__be16 *) ptr);
966
967 /* Check protocol version */
968 version = hdrflags & L2TP_HDR_VER_MASK;
969 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000970 l2tp_info(tunnel, L2TP_MSG_DATA,
971 "%s: recv protocol version mismatch: got %d expected %d\n",
972 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000973 goto error;
974 }
975
976 /* Get length of L2TP packet */
977 length = skb->len;
978
979 /* If type is control packet, it is handled by userspace. */
980 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000981 l2tp_dbg(tunnel, L2TP_MSG_DATA,
982 "%s: recv control packet, len=%d\n",
983 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000984 goto error;
985 }
986
987 /* Skip flags */
988 ptr += 2;
989
990 if (tunnel->version == L2TP_HDR_VER_2) {
991 /* If length is present, skip it */
992 if (hdrflags & L2TP_HDRFLAG_L)
993 ptr += 2;
994
995 /* Extract tunnel and session ID */
996 tunnel_id = ntohs(*(__be16 *) ptr);
997 ptr += 2;
998 session_id = ntohs(*(__be16 *) ptr);
999 ptr += 2;
1000 } else {
1001 ptr += 2; /* skip reserved bits */
1002 tunnel_id = tunnel->tunnel_id;
1003 session_id = ntohl(*(__be32 *) ptr);
1004 ptr += 4;
1005 }
1006
1007 /* Find the session context */
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001008 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
James Chapman309795f2010-04-02 06:19:10 +00001009 if (!session || !session->recv_skb) {
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001010 if (session) {
1011 if (session->deref)
1012 session->deref(session);
1013 l2tp_session_dec_refcount(session);
1014 }
1015
James Chapmanf7faffa2010-04-02 06:18:49 +00001016 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001017 l2tp_info(tunnel, L2TP_MSG_DATA,
1018 "%s: no session found (%u/%u). Passing up.\n",
1019 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001020 goto error;
1021 }
1022
1023 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001024 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001025
1026 return 0;
1027
James Chapmanfd558d12010-04-02 06:18:33 +00001028error:
1029 /* Put UDP header back */
1030 __skb_push(skb, sizeof(struct udphdr));
1031
1032 return 1;
1033}
James Chapmanfd558d12010-04-02 06:18:33 +00001034
1035/* UDP encapsulation receive handler. See net/ipv4/udp.c.
1036 * Return codes:
1037 * 0 : success.
1038 * <0: error
1039 * >0: skb should be passed up to userspace as UDP.
1040 */
1041int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1042{
1043 struct l2tp_tunnel *tunnel;
1044
1045 tunnel = l2tp_sock_to_tunnel(sk);
1046 if (tunnel == NULL)
1047 goto pass_up;
1048
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001049 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1050 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001051
1052 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1053 goto pass_up_put;
1054
1055 sock_put(sk);
1056 return 0;
1057
1058pass_up_put:
1059 sock_put(sk);
1060pass_up:
1061 return 1;
1062}
1063EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1064
1065/************************************************************************
1066 * Transmit handling
1067 ***********************************************************************/
1068
1069/* Build an L2TP header for the session into the buffer provided.
1070 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001071static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001072{
James Chapmanf7faffa2010-04-02 06:18:49 +00001073 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001074 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001075 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001076 u16 flags = L2TP_HDR_VER_2;
1077 u32 tunnel_id = tunnel->peer_tunnel_id;
1078 u32 session_id = session->peer_session_id;
1079
1080 if (session->send_seq)
1081 flags |= L2TP_HDRFLAG_S;
1082
1083 /* Setup L2TP header. */
1084 *bufp++ = htons(flags);
1085 *bufp++ = htons(tunnel_id);
1086 *bufp++ = htons(session_id);
1087 if (session->send_seq) {
1088 *bufp++ = htons(session->ns);
1089 *bufp++ = 0;
1090 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001091 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001092 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1093 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001094 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001095
1096 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001097}
1098
James Chapmanf7faffa2010-04-02 06:18:49 +00001099static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001100{
James Chapman0d767512010-04-02 06:19:00 +00001101 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001102 char *bufp = buf;
1103 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001104
James Chapman0d767512010-04-02 06:19:00 +00001105 /* Setup L2TP header. The header differs slightly for UDP and
1106 * IP encapsulations. For UDP, there is 4 bytes of flags.
1107 */
1108 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1109 u16 flags = L2TP_HDR_VER_3;
1110 *((__be16 *) bufp) = htons(flags);
1111 bufp += 2;
1112 *((__be16 *) bufp) = 0;
1113 bufp += 2;
1114 }
1115
James Chapmanf7faffa2010-04-02 06:18:49 +00001116 *((__be32 *) bufp) = htonl(session->peer_session_id);
1117 bufp += 4;
1118 if (session->cookie_len) {
1119 memcpy(bufp, &session->cookie[0], session->cookie_len);
1120 bufp += session->cookie_len;
1121 }
Lorenzo Bianconi592bde82018-01-16 23:01:55 +01001122 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1123 u32 l2h = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +00001124
Lorenzo Bianconi592bde82018-01-16 23:01:55 +01001125 if (session->send_seq) {
1126 l2h = 0x40000000 | session->ns;
1127 session->ns++;
1128 session->ns &= 0xffffff;
1129 l2tp_dbg(session, L2TP_MSG_SEQ,
1130 "%s: updated ns to %u\n",
1131 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001132 }
Lorenzo Bianconi592bde82018-01-16 23:01:55 +01001133
1134 *((__be32 *)bufp) = htonl(l2h);
1135 bufp += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001136 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001137
1138 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001139}
James Chapmanfd558d12010-04-02 06:18:33 +00001140
stephen hemmingerfc130842010-10-21 07:50:46 +00001141static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001142 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001143{
1144 struct l2tp_tunnel *tunnel = session->tunnel;
1145 unsigned int len = skb->len;
1146 int error;
1147
1148 /* Debug */
1149 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001150 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1151 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001152 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001153 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1154 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001155
1156 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001157 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1158 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001159
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001160 pr_debug("%s: xmit\n", session->name);
1161 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1162 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001163 }
1164
1165 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001166 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001167#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001168 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001169 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001170 else
1171#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001172 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001173
1174 /* Update stats */
1175 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001176 atomic_long_inc(&tunnel->stats.tx_packets);
1177 atomic_long_add(len, &tunnel->stats.tx_bytes);
1178 atomic_long_inc(&session->stats.tx_packets);
1179 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001180 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001181 atomic_long_inc(&tunnel->stats.tx_errors);
1182 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001183 }
1184
1185 return 0;
1186}
James Chapmanfd558d12010-04-02 06:18:33 +00001187
James Chapmanfd558d12010-04-02 06:18:33 +00001188/* If caller requires the skb to have a ppp header, the header must be
1189 * inserted in the skb data before calling this function.
1190 */
1191int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1192{
1193 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001194 struct l2tp_tunnel *tunnel = session->tunnel;
1195 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001196 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001197 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001198 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001199 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001200 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1201 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001202 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001203
1204 /* Check that there's enough headroom in the skb to insert IP,
1205 * UDP and L2TP headers. If not enough, expand it to
1206 * make room. Adjust truesize.
1207 */
1208 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001209 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001210 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001211 kfree_skb(skb);
1212 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001213 }
James Chapmanfd558d12010-04-02 06:18:33 +00001214
James Chapmanfd558d12010-04-02 06:18:33 +00001215 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001216 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001217
James Chapman0d767512010-04-02 06:19:00 +00001218 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001219 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1220 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1221 IPSKB_REROUTED);
1222 nf_reset(skb);
1223
David S. Miller6af88da2011-05-08 13:45:20 -07001224 bh_lock_sock(sk);
1225 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001226 kfree_skb(skb);
1227 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001228 goto out_unlock;
1229 }
1230
James Chapmanfd558d12010-04-02 06:18:33 +00001231 /* Get routing info from the tunnel socket */
1232 skb_dst_drop(skb);
Wei Wangae7d5062018-08-10 11:14:56 -07001233 skb_dst_set(skb, sk_dst_check(sk, 0));
James Chapmanfd558d12010-04-02 06:18:33 +00001234
David S. Millerd9d8da82011-05-06 22:23:20 -07001235 inet = inet_sk(sk);
1236 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001237 switch (tunnel->encap) {
1238 case L2TP_ENCAPTYPE_UDP:
1239 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001240 __skb_push(skb, sizeof(*uh));
1241 skb_reset_transport_header(skb);
1242 uh = udp_hdr(skb);
1243 uh->source = inet->inet_sport;
1244 uh->dest = inet->inet_dport;
1245 udp_len = uhlen + hdr_len + data_len;
1246 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001247
1248 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001249#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001250 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Tom Herbert77157e12014-06-04 17:19:56 -07001251 udp6_set_csum(udp_get_no_check6_tx(sk),
1252 skb, &inet6_sk(sk)->saddr,
1253 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001254 else
1255#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001256 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1257 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001258 break;
1259
1260 case L2TP_ENCAPTYPE_IP:
1261 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001262 }
1263
David S. Millerd9d8da82011-05-06 22:23:20 -07001264 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001265out_unlock:
1266 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001267
Eric Dumazetb8c84302012-06-28 20:15:13 +00001268 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001269}
1270EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1271
1272/*****************************************************************************
1273 * Tinnel and session create/destroy.
1274 *****************************************************************************/
1275
1276/* Tunnel socket destruct hook.
1277 * The tunnel context is deleted only when all session sockets have been
1278 * closed.
1279 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001280static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001281{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001282 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001283 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001284
James Chapmanfd558d12010-04-02 06:18:33 +00001285 if (tunnel == NULL)
1286 goto end;
1287
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001288 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001289
James Chapmanfd558d12010-04-02 06:18:33 +00001290
Tom Parkinf8ccac02013-01-31 23:43:00 +00001291 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001292 switch (tunnel->encap) {
1293 case L2TP_ENCAPTYPE_UDP:
1294 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1295 (udp_sk(sk))->encap_type = 0;
1296 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001297 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001298 break;
1299 case L2TP_ENCAPTYPE_IP:
1300 break;
1301 }
James Chapmanfd558d12010-04-02 06:18:33 +00001302
1303 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001304 sk->sk_destruct = tunnel->old_sk_destruct;
1305 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001306 tunnel->sock = NULL;
1307
1308 /* Remove the tunnel struct from the tunnel list */
1309 pn = l2tp_pernet(tunnel->l2tp_net);
1310 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1311 list_del_rcu(&tunnel->list);
1312 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1313 atomic_dec(&l2tp_tunnel_count);
1314
1315 l2tp_tunnel_closeall(tunnel);
1316 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001317
1318 /* Call the original destructor */
1319 if (sk->sk_destruct)
1320 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001321end:
1322 return;
1323}
James Chapmanfd558d12010-04-02 06:18:33 +00001324
1325/* When the tunnel is closed, all the attached sessions need to go too.
1326 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001327void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001328{
1329 int hash;
1330 struct hlist_node *walk;
1331 struct hlist_node *tmp;
1332 struct l2tp_session *session;
1333
1334 BUG_ON(tunnel == NULL);
1335
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001336 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1337 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001338
1339 write_lock_bh(&tunnel->hlist_lock);
1340 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1341again:
1342 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1343 session = hlist_entry(walk, struct l2tp_session, hlist);
1344
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001345 l2tp_info(session, L2TP_MSG_CONTROL,
1346 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001347
1348 hlist_del_init(&session->hlist);
1349
James Chapmanfd558d12010-04-02 06:18:33 +00001350 if (session->ref != NULL)
1351 (*session->ref)(session);
1352
1353 write_unlock_bh(&tunnel->hlist_lock);
1354
Tom Parkinf6e16b22013-03-19 06:11:23 +00001355 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001356 l2tp_session_queue_purge(session);
1357
James Chapmanfd558d12010-04-02 06:18:33 +00001358 if (session->session_close != NULL)
1359 (*session->session_close)(session);
1360
1361 if (session->deref != NULL)
1362 (*session->deref)(session);
1363
Tom Parkin9980d002013-03-19 06:11:13 +00001364 l2tp_session_dec_refcount(session);
1365
James Chapmanfd558d12010-04-02 06:18:33 +00001366 write_lock_bh(&tunnel->hlist_lock);
1367
1368 /* Now restart from the beginning of this hash
1369 * chain. We always remove a session from the
1370 * list so we are guaranteed to make forward
1371 * progress.
1372 */
1373 goto again;
1374 }
1375 }
1376 write_unlock_bh(&tunnel->hlist_lock);
1377}
Tom Parkine34f4c72013-03-19 06:11:14 +00001378EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001379
Tom Parkin9980d002013-03-19 06:11:13 +00001380/* Tunnel socket destroy hook for UDP encapsulation */
1381static void l2tp_udp_encap_destroy(struct sock *sk)
1382{
1383 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1384 if (tunnel) {
1385 l2tp_tunnel_closeall(tunnel);
1386 sock_put(sk);
1387 }
1388}
1389
James Chapmanfd558d12010-04-02 06:18:33 +00001390/* Really kill the tunnel.
1391 * Come here only when all sessions have been cleared from the tunnel.
1392 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001393static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001394{
James Chapmanfd558d12010-04-02 06:18:33 +00001395 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1396 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001397 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001398 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001399}
James Chapmanfd558d12010-04-02 06:18:33 +00001400
Tom Parkinf8ccac02013-01-31 23:43:00 +00001401/* Workqueue tunnel deletion function */
1402static void l2tp_tunnel_del_work(struct work_struct *work)
1403{
1404 struct l2tp_tunnel *tunnel = NULL;
1405 struct socket *sock = NULL;
1406 struct sock *sk = NULL;
1407
1408 tunnel = container_of(work, struct l2tp_tunnel, del_work);
Ridge Kennedye5941132017-02-22 14:59:49 +13001409
1410 l2tp_tunnel_closeall(tunnel);
1411
Tom Parkinf8ccac02013-01-31 23:43:00 +00001412 sk = l2tp_tunnel_sock_lookup(tunnel);
1413 if (!sk)
Alexander Couzens06a15f52015-09-28 11:32:42 +02001414 goto out;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001415
1416 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001417
Tom Parkin02d13ed2013-03-19 06:11:18 +00001418 /* If the tunnel socket was created by userspace, then go through the
1419 * inet layer to shut the socket down, and let userspace close it.
1420 * Otherwise, if we created the socket directly within the kernel, use
1421 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001422 * In either case the tunnel resources are freed in the socket
1423 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001424 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001425 if (tunnel->fd >= 0) {
1426 if (sock)
1427 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001428 } else {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001429 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001430 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001431 sock_release(sock);
1432 }
Tom Parkin167eb172013-01-31 23:43:03 +00001433 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001434
1435 l2tp_tunnel_sock_put(sk);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001436out:
1437 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001438}
James Chapmanfd558d12010-04-02 06:18:33 +00001439
James Chapman789a4a22010-04-02 06:19:40 +00001440/* Create a socket for the tunnel, if one isn't set up by
1441 * userspace. This is used for static tunnels where there is no
1442 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001443 *
1444 * Since we don't want these sockets to keep a namespace alive by
1445 * themselves, we drop the socket's namespace refcount after creation.
1446 * These sockets are freed when the namespace exits using the pernet
1447 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001448 */
Tom Parkin167eb172013-01-31 23:43:03 +00001449static int l2tp_tunnel_sock_create(struct net *net,
1450 u32 tunnel_id,
1451 u32 peer_tunnel_id,
1452 struct l2tp_tunnel_cfg *cfg,
1453 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001454{
1455 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001456 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001457 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001458
1459 switch (cfg->encap) {
1460 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001461 memset(&udp_conf, 0, sizeof(udp_conf));
1462
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001463#if IS_ENABLED(CONFIG_IPV6)
1464 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001465 udp_conf.family = AF_INET6;
1466 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1467 sizeof(udp_conf.local_ip6));
1468 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1469 sizeof(udp_conf.peer_ip6));
1470 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001471 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001472 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001473 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001474 } else
1475#endif
1476 {
Tom Herbert85644b42014-07-13 19:49:48 -07001477 udp_conf.family = AF_INET;
1478 udp_conf.local_ip = cfg->local_ip;
1479 udp_conf.peer_ip = cfg->peer_ip;
1480 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001481 }
James Chapman789a4a22010-04-02 06:19:40 +00001482
Tom Herbert85644b42014-07-13 19:49:48 -07001483 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1484 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1485
1486 err = udp_sock_create(net, &udp_conf, &sock);
1487 if (err < 0)
1488 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001489
1490 break;
1491
1492 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001493#if IS_ENABLED(CONFIG_IPV6)
1494 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001495 struct sockaddr_l2tpip6 ip6_addr = {0};
1496
Eric W. Biederman26abe142015-05-08 21:10:31 -05001497 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001498 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001499 if (err < 0)
1500 goto out;
1501
James Chapman5dac94e2012-04-29 21:48:55 +00001502 ip6_addr.l2tp_family = AF_INET6;
1503 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1504 sizeof(ip6_addr.l2tp_addr));
1505 ip6_addr.l2tp_conn_id = tunnel_id;
1506 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1507 sizeof(ip6_addr));
1508 if (err < 0)
1509 goto out;
1510
1511 ip6_addr.l2tp_family = AF_INET6;
1512 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1513 sizeof(ip6_addr.l2tp_addr));
1514 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1515 err = kernel_connect(sock,
1516 (struct sockaddr *) &ip6_addr,
1517 sizeof(ip6_addr), 0);
1518 if (err < 0)
1519 goto out;
1520 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001521#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001522 {
Tom Herbert85644b42014-07-13 19:49:48 -07001523 struct sockaddr_l2tpip ip_addr = {0};
1524
Eric W. Biederman26abe142015-05-08 21:10:31 -05001525 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001526 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001527 if (err < 0)
1528 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001529
James Chapman5dac94e2012-04-29 21:48:55 +00001530 ip_addr.l2tp_family = AF_INET;
1531 ip_addr.l2tp_addr = cfg->local_ip;
1532 ip_addr.l2tp_conn_id = tunnel_id;
1533 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1534 sizeof(ip_addr));
1535 if (err < 0)
1536 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001537
James Chapman5dac94e2012-04-29 21:48:55 +00001538 ip_addr.l2tp_family = AF_INET;
1539 ip_addr.l2tp_addr = cfg->peer_ip;
1540 ip_addr.l2tp_conn_id = peer_tunnel_id;
1541 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1542 sizeof(ip_addr), 0);
1543 if (err < 0)
1544 goto out;
1545 }
James Chapman789a4a22010-04-02 06:19:40 +00001546 break;
1547
1548 default:
1549 goto out;
1550 }
1551
1552out:
Tom Parkin167eb172013-01-31 23:43:03 +00001553 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001554 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001555 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001556 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001557 *sockp = NULL;
1558 }
1559
1560 return err;
1561}
1562
Eric Dumazet37159ef2012-09-04 07:18:57 +00001563static struct lock_class_key l2tp_socket_class;
1564
James Chapmanfd558d12010-04-02 06:18:33 +00001565int 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)
1566{
1567 struct l2tp_tunnel *tunnel = NULL;
1568 int err;
1569 struct socket *sock = NULL;
1570 struct sock *sk = NULL;
1571 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001572 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001573
1574 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001575 * the userspace L2TP daemon. If not specified, create a
1576 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001577 */
James Chapman789a4a22010-04-02 06:19:40 +00001578 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001579 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1580 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001581 if (err < 0)
1582 goto err;
1583 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001584 sock = sockfd_lookup(fd, &err);
1585 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001586 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001587 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001588 err = -EBADF;
1589 goto err;
1590 }
1591
1592 /* Reject namespace mismatches */
1593 if (!net_eq(sock_net(sock->sk), net)) {
1594 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1595 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001596 goto err;
1597 }
James Chapmanfd558d12010-04-02 06:18:33 +00001598 }
1599
1600 sk = sock->sk;
1601
James Chapman0d767512010-04-02 06:19:00 +00001602 if (cfg != NULL)
1603 encap = cfg->encap;
1604
James Chapmanfd558d12010-04-02 06:18:33 +00001605 /* Quick sanity checks */
Eric Dumazet84fc2d72018-03-06 07:54:53 -08001606 err = -EPROTONOSUPPORT;
1607 if (sk->sk_type != SOCK_DGRAM) {
1608 pr_debug("tunl %hu: fd %d wrong socket type\n",
1609 tunnel_id, fd);
1610 goto err;
1611 }
James Chapman0d767512010-04-02 06:19:00 +00001612 switch (encap) {
1613 case L2TP_ENCAPTYPE_UDP:
James Chapman0d767512010-04-02 06:19:00 +00001614 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001615 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001616 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1617 goto err;
1618 }
1619 break;
1620 case L2TP_ENCAPTYPE_IP:
James Chapman0d767512010-04-02 06:19:00 +00001621 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001622 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001623 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1624 goto err;
1625 }
1626 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001627 }
1628
1629 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001630 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001631 if (tunnel != NULL) {
1632 /* This socket has already been prepped */
1633 err = -EBUSY;
1634 goto err;
1635 }
1636
James Chapmanfd558d12010-04-02 06:18:33 +00001637 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1638 if (tunnel == NULL) {
1639 err = -ENOMEM;
1640 goto err;
1641 }
1642
1643 tunnel->version = version;
1644 tunnel->tunnel_id = tunnel_id;
1645 tunnel->peer_tunnel_id = peer_tunnel_id;
1646 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1647
1648 tunnel->magic = L2TP_TUNNEL_MAGIC;
1649 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1650 rwlock_init(&tunnel->hlist_lock);
1651
1652 /* The net we belong to */
1653 tunnel->l2tp_net = net;
1654 pn = l2tp_pernet(net);
1655
James Chapman0d767512010-04-02 06:19:00 +00001656 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001657 tunnel->debug = cfg->debug;
1658
François Cachereule18503f2013-10-02 10:16:02 +02001659#if IS_ENABLED(CONFIG_IPV6)
1660 if (sk->sk_family == PF_INET6) {
1661 struct ipv6_pinfo *np = inet6_sk(sk);
1662
1663 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001664 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001665 struct inet_sock *inet = inet_sk(sk);
1666
1667 tunnel->v4mapped = true;
1668 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001669 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1670 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001671 } else {
1672 tunnel->v4mapped = false;
1673 }
1674 }
1675#endif
1676
James Chapmanfd558d12010-04-02 06:18:33 +00001677 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001678 tunnel->encap = encap;
1679 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001680 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001681
Andy Zhouc8fffce2014-09-16 17:31:19 -07001682 udp_cfg.sk_user_data = tunnel;
1683 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1684 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1685 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1686
1687 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1688 } else {
1689 sk->sk_user_data = tunnel;
1690 }
James Chapmanfd558d12010-04-02 06:18:33 +00001691
1692 /* Hook on the tunnel socket destructor so that we can cleanup
1693 * if the tunnel socket goes away.
1694 */
1695 tunnel->old_sk_destruct = sk->sk_destruct;
1696 sk->sk_destruct = &l2tp_tunnel_destruct;
1697 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001698 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001699 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1700
James Chapmanfd558d12010-04-02 06:18:33 +00001701 sk->sk_allocation = GFP_ATOMIC;
1702
Tom Parkinf8ccac02013-01-31 23:43:00 +00001703 /* Init delete workqueue struct */
1704 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1705
James Chapmanfd558d12010-04-02 06:18:33 +00001706 /* Add tunnel to our list */
1707 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001708 atomic_inc(&l2tp_tunnel_count);
1709
1710 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001711 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001712 */
1713 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001714 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1715 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1716 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001717
1718 err = 0;
1719err:
1720 if (tunnelp)
1721 *tunnelp = tunnel;
1722
James Chapman789a4a22010-04-02 06:19:40 +00001723 /* If tunnel's socket was created by the kernel, it doesn't
1724 * have a file.
1725 */
1726 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001727 sockfd_put(sock);
1728
1729 return err;
1730}
1731EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1732
James Chapman309795f2010-04-02 06:19:10 +00001733/* This function is used by the netlink TUNNEL_DELETE command.
1734 */
Sabrina Dubrocab4a9b122017-09-26 16:16:43 +02001735void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
James Chapman309795f2010-04-02 06:19:10 +00001736{
Sabrina Dubrocab4a9b122017-09-26 16:16:43 +02001737 if (!test_and_set_bit(0, &tunnel->dead)) {
1738 l2tp_tunnel_inc_refcount(tunnel);
1739 queue_work(l2tp_wq, &tunnel->del_work);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001740 }
James Chapman309795f2010-04-02 06:19:10 +00001741}
1742EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1743
James Chapmanfd558d12010-04-02 06:18:33 +00001744/* Really kill the session.
1745 */
1746void l2tp_session_free(struct l2tp_session *session)
1747{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001748 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001749
1750 BUG_ON(atomic_read(&session->ref_count) != 0);
1751
Tom Parkinf6e16b22013-03-19 06:11:23 +00001752 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001753 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001754 if (session->session_id != 0)
1755 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001756 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001757 session->tunnel = NULL;
1758 l2tp_tunnel_dec_refcount(tunnel);
1759 }
1760
1761 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001762}
1763EXPORT_SYMBOL_GPL(l2tp_session_free);
1764
Tom Parkinf6e16b22013-03-19 06:11:23 +00001765/* Remove an l2tp session from l2tp_core's hash lists.
1766 * Provides a tidyup interface for pseudowire code which can't just route all
1767 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1768 * callback.
1769 */
1770void __l2tp_session_unhash(struct l2tp_session *session)
1771{
1772 struct l2tp_tunnel *tunnel = session->tunnel;
1773
1774 /* Remove the session from core hashes */
1775 if (tunnel) {
1776 /* Remove from the per-tunnel hash */
1777 write_lock_bh(&tunnel->hlist_lock);
1778 hlist_del_init(&session->hlist);
1779 write_unlock_bh(&tunnel->hlist_lock);
1780
1781 /* For L2TPv3 we have a per-net hash: remove from there, too */
1782 if (tunnel->version != L2TP_HDR_VER_2) {
1783 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1784 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1785 hlist_del_init_rcu(&session->global_hlist);
1786 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1787 synchronize_rcu();
1788 }
1789 }
1790}
1791EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1792
James Chapman309795f2010-04-02 06:19:10 +00001793/* This function is used by the netlink SESSION_DELETE command and by
1794 pseudowire modules.
1795 */
1796int l2tp_session_delete(struct l2tp_session *session)
1797{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001798 if (session->ref)
1799 (*session->ref)(session);
1800 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001801 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001802 if (session->session_close != NULL)
1803 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001804 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001805 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001806 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001807 return 0;
1808}
1809EXPORT_SYMBOL_GPL(l2tp_session_delete);
1810
James Chapmanf7faffa2010-04-02 06:18:49 +00001811/* We come here whenever a session's send_seq, cookie_len or
Lorenzo Bianconi592bde82018-01-16 23:01:55 +01001812 * l2specific_type parameters are set.
James Chapmanf7faffa2010-04-02 06:18:49 +00001813 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001814void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001815{
1816 if (version == L2TP_HDR_VER_2) {
1817 session->hdr_len = 6;
1818 if (session->send_seq)
1819 session->hdr_len += 4;
1820 } else {
Lorenzo Bianconi592bde82018-01-16 23:01:55 +01001821 session->hdr_len = 4 + session->cookie_len;
1822 session->hdr_len += l2tp_get_l2specific_len(session);
James Chapman0d767512010-04-02 06:19:00 +00001823 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1824 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001825 }
1826
1827}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001828EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001829
James Chapmanfd558d12010-04-02 06:18:33 +00001830struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1831{
1832 struct l2tp_session *session;
Guillaume Naultd9face62017-03-31 13:02:27 +02001833 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001834
1835 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1836 if (session != NULL) {
1837 session->magic = L2TP_SESSION_MAGIC;
1838 session->tunnel = tunnel;
1839
1840 session->session_id = session_id;
1841 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001842 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001843 if (tunnel->version == L2TP_HDR_VER_2)
1844 session->nr_max = 0xffff;
1845 else
1846 session->nr_max = 0xffffff;
1847 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001848 session->nr_oos_count_max = 4;
1849
1850 /* Use NR of first received packet */
1851 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001852
1853 sprintf(&session->name[0], "sess %u/%u",
1854 tunnel->tunnel_id, session->session_id);
1855
1856 skb_queue_head_init(&session->reorder_q);
1857
1858 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001859 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001860
1861 /* Inherit debug options from tunnel */
1862 session->debug = tunnel->debug;
1863
1864 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001865 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001866 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001867 session->mtu = cfg->mtu;
1868 session->mru = cfg->mru;
1869 session->send_seq = cfg->send_seq;
1870 session->recv_seq = cfg->recv_seq;
1871 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001872 session->reorder_timeout = cfg->reorder_timeout;
James Chapmanf7faffa2010-04-02 06:18:49 +00001873 session->l2specific_type = cfg->l2specific_type;
1874 session->l2specific_len = cfg->l2specific_len;
1875 session->cookie_len = cfg->cookie_len;
1876 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1877 session->peer_cookie_len = cfg->peer_cookie_len;
1878 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001879 }
1880
James Chapmanf7faffa2010-04-02 06:18:49 +00001881 if (tunnel->version == L2TP_HDR_VER_2)
1882 session->build_header = l2tp_build_l2tpv2_header;
1883 else
1884 session->build_header = l2tp_build_l2tpv3_header;
1885
1886 l2tp_session_set_header_len(session, tunnel->version);
1887
Guillaume Naultd9face62017-03-31 13:02:27 +02001888 err = l2tp_session_add_to_tunnel(tunnel, session);
1889 if (err) {
1890 kfree(session);
1891
1892 return ERR_PTR(err);
1893 }
1894
James Chapmanfd558d12010-04-02 06:18:33 +00001895 /* Bump the reference count. The session context is deleted
1896 * only when this drops to zero.
1897 */
1898 l2tp_session_inc_refcount(session);
1899 l2tp_tunnel_inc_refcount(tunnel);
1900
1901 /* Ensure tunnel socket isn't deleted */
1902 sock_hold(tunnel->sock);
1903
James Chapmanfd558d12010-04-02 06:18:33 +00001904 /* Ignore management session in session count value */
1905 if (session->session_id != 0)
1906 atomic_inc(&l2tp_session_count);
Guillaume Naultd9face62017-03-31 13:02:27 +02001907
1908 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001909 }
1910
Guillaume Naultd9face62017-03-31 13:02:27 +02001911 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001912}
1913EXPORT_SYMBOL_GPL(l2tp_session_create);
1914
1915/*****************************************************************************
1916 * Init and cleanup
1917 *****************************************************************************/
1918
1919static __net_init int l2tp_init_net(struct net *net)
1920{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001921 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001922 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001923
James Chapmanfd558d12010-04-02 06:18:33 +00001924 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001925 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001926
James Chapmanf7faffa2010-04-02 06:18:49 +00001927 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1928 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1929
James Chapmane02d4942010-04-02 06:19:16 +00001930 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001931
James Chapmanfd558d12010-04-02 06:18:33 +00001932 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001933}
1934
Tom Parkin167eb172013-01-31 23:43:03 +00001935static __net_exit void l2tp_exit_net(struct net *net)
1936{
1937 struct l2tp_net *pn = l2tp_pernet(net);
1938 struct l2tp_tunnel *tunnel = NULL;
1939
1940 rcu_read_lock_bh();
1941 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Jiri Slabyfc4177e2017-10-25 15:57:55 +02001942 l2tp_tunnel_delete(tunnel);
Tom Parkin167eb172013-01-31 23:43:03 +00001943 }
1944 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001945
1946 flush_workqueue(l2tp_wq);
1947 rcu_barrier();
Tom Parkin167eb172013-01-31 23:43:03 +00001948}
1949
James Chapmanfd558d12010-04-02 06:18:33 +00001950static struct pernet_operations l2tp_net_ops = {
1951 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001952 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001953 .id = &l2tp_net_id,
1954 .size = sizeof(struct l2tp_net),
1955};
1956
1957static int __init l2tp_init(void)
1958{
1959 int rc = 0;
1960
1961 rc = register_pernet_device(&l2tp_net_ops);
1962 if (rc)
1963 goto out;
1964
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001965 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001966 if (!l2tp_wq) {
1967 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001968 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001969 rc = -ENOMEM;
1970 goto out;
1971 }
1972
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001973 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001974
1975out:
1976 return rc;
1977}
1978
1979static void __exit l2tp_exit(void)
1980{
1981 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001982 if (l2tp_wq) {
1983 destroy_workqueue(l2tp_wq);
1984 l2tp_wq = NULL;
1985 }
James Chapmanfd558d12010-04-02 06:18:33 +00001986}
1987
1988module_init(l2tp_init);
1989module_exit(l2tp_exit);
1990
1991MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1992MODULE_DESCRIPTION("L2TP core");
1993MODULE_LICENSE("GPL");
1994MODULE_VERSION(L2TP_DRV_VERSION);
1995