blob: b96dbe38ecad5b8e94c27ca892985bfbc038710c [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
86#define L2TP_HDR_SIZE_SEQ 10
87#define L2TP_HDR_SIZE_NOSEQ 6
88
89/* Default trace flags */
90#define L2TP_DEFAULT_DEBUG_FLAGS 0
91
James Chapmanfd558d12010-04-02 06:18:33 +000092/* Private data stored for received packets in the skb.
93 */
94struct l2tp_skb_cb {
James Chapmanf7faffa2010-04-02 06:18:49 +000095 u32 ns;
James Chapmanfd558d12010-04-02 06:18:33 +000096 u16 has_seq;
97 u16 length;
98 unsigned long expires;
99};
100
101#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
102
103static atomic_t l2tp_tunnel_count;
104static atomic_t l2tp_session_count;
Tom Parkinf8ccac02013-01-31 23:43:00 +0000105static struct workqueue_struct *l2tp_wq;
James Chapmanfd558d12010-04-02 06:18:33 +0000106
107/* per-net private data for this module */
108static unsigned int l2tp_net_id;
109struct l2tp_net {
110 struct list_head l2tp_tunnel_list;
James Chapmane02d4942010-04-02 06:19:16 +0000111 spinlock_t l2tp_tunnel_list_lock;
James Chapmanf7faffa2010-04-02 06:18:49 +0000112 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
James Chapmane02d4942010-04-02 06:19:16 +0000113 spinlock_t l2tp_session_hlist_lock;
James Chapmanfd558d12010-04-02 06:18:33 +0000114};
115
stephen hemmingerfc130842010-10-21 07:50:46 +0000116static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
stephen hemmingerfc130842010-10-21 07:50:46 +0000117
David S. Miller8d8a51e2013-10-08 15:44:26 -0400118static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
119{
120 return sk->sk_user_data;
121}
122
James Chapmanfd558d12010-04-02 06:18:33 +0000123static inline struct l2tp_net *l2tp_pernet(struct net *net)
124{
125 BUG_ON(!net);
126
127 return net_generic(net, l2tp_net_id);
128}
129
stephen hemmingerfc130842010-10-21 07:50:46 +0000130/* Tunnel reference counts. Incremented per session that is added to
131 * the tunnel.
132 */
133static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
134{
135 atomic_inc(&tunnel->ref_count);
136}
137
138static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
139{
140 if (atomic_dec_and_test(&tunnel->ref_count))
141 l2tp_tunnel_free(tunnel);
142}
143#ifdef L2TP_REFCNT_DEBUG
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000144#define l2tp_tunnel_inc_refcount(_t) \
145do { \
146 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
147 __func__, __LINE__, (_t)->name, \
148 atomic_read(&_t->ref_count)); \
149 l2tp_tunnel_inc_refcount_1(_t); \
150} while (0)
Andy Zhou29abe2f2014-09-03 13:16:54 -0700151#define l2tp_tunnel_dec_refcount(_t) \
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000152do { \
153 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
154 __func__, __LINE__, (_t)->name, \
155 atomic_read(&_t->ref_count)); \
156 l2tp_tunnel_dec_refcount_1(_t); \
157} while (0)
stephen hemmingerfc130842010-10-21 07:50:46 +0000158#else
159#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
160#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
161#endif
162
James Chapmanf7faffa2010-04-02 06:18:49 +0000163/* Session hash global list for L2TPv3.
164 * The session_id SHOULD be random according to RFC3931, but several
165 * L2TP implementations use incrementing session_ids. So we do a real
166 * hash on the session_id, rather than a simple bitmask.
167 */
168static inline struct hlist_head *
169l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
170{
171 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
172
173}
174
Tom Parkin80d84ef2013-01-22 05:13:48 +0000175/* Lookup the tunnel socket, possibly involving the fs code if the socket is
176 * owned by userspace. A struct sock returned from this function must be
177 * released using l2tp_tunnel_sock_put once you're done with it.
178 */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800179static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000180{
181 int err = 0;
182 struct socket *sock = NULL;
183 struct sock *sk = NULL;
184
185 if (!tunnel)
186 goto out;
187
188 if (tunnel->fd >= 0) {
189 /* Socket is owned by userspace, who might be in the process
190 * of closing it. Look the socket up using the fd to ensure
191 * consistency.
192 */
193 sock = sockfd_lookup(tunnel->fd, &err);
194 if (sock)
195 sk = sock->sk;
196 } else {
197 /* Socket is owned by kernelspace */
198 sk = tunnel->sock;
Tom Parkin8abbbe82013-03-19 06:11:17 +0000199 sock_hold(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000200 }
201
202out:
203 return sk;
204}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000205
206/* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
stephen hemmingerb5d2b282014-01-09 22:22:27 -0800207static void l2tp_tunnel_sock_put(struct sock *sk)
Tom Parkin80d84ef2013-01-22 05:13:48 +0000208{
209 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
210 if (tunnel) {
211 if (tunnel->fd >= 0) {
212 /* Socket is owned by userspace */
213 sockfd_put(sk->sk_socket);
214 }
215 sock_put(sk);
216 }
Tom Parkin8abbbe82013-03-19 06:11:17 +0000217 sock_put(sk);
Tom Parkin80d84ef2013-01-22 05:13:48 +0000218}
Tom Parkin80d84ef2013-01-22 05:13:48 +0000219
James Chapmanf7faffa2010-04-02 06:18:49 +0000220/* Lookup a session by id in the global session list
221 */
222static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
223{
224 struct l2tp_net *pn = l2tp_pernet(net);
225 struct hlist_head *session_list =
226 l2tp_session_id_hash_2(pn, session_id);
227 struct l2tp_session *session;
James Chapmanf7faffa2010-04-02 06:18:49 +0000228
James Chapmane02d4942010-04-02 06:19:16 +0000229 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800230 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
James Chapmanf7faffa2010-04-02 06:18:49 +0000231 if (session->session_id == session_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000232 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000233 return session;
234 }
235 }
James Chapmane02d4942010-04-02 06:19:16 +0000236 rcu_read_unlock_bh();
James Chapmanf7faffa2010-04-02 06:18:49 +0000237
238 return NULL;
239}
240
James Chapmanfd558d12010-04-02 06:18:33 +0000241/* Session hash list.
242 * The session_id SHOULD be random according to RFC2661, but several
243 * L2TP implementations (Cisco and Microsoft) use incrementing
244 * session_ids. So we do a real hash on the session_id, rather than a
245 * simple bitmask.
246 */
247static inline struct hlist_head *
248l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
249{
250 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
251}
252
253/* Lookup a session by id
254 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000255struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
James Chapmanfd558d12010-04-02 06:18:33 +0000256{
James Chapmanf7faffa2010-04-02 06:18:49 +0000257 struct hlist_head *session_list;
James Chapmanfd558d12010-04-02 06:18:33 +0000258 struct l2tp_session *session;
James Chapmanfd558d12010-04-02 06:18:33 +0000259
James Chapmanf7faffa2010-04-02 06:18:49 +0000260 /* In L2TPv3, session_ids are unique over all tunnels and we
261 * sometimes need to look them up before we know the
262 * tunnel.
263 */
264 if (tunnel == NULL)
265 return l2tp_session_find_2(net, session_id);
266
267 session_list = l2tp_session_id_hash(tunnel, session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000268 read_lock_bh(&tunnel->hlist_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800269 hlist_for_each_entry(session, session_list, hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000270 if (session->session_id == session_id) {
271 read_unlock_bh(&tunnel->hlist_lock);
272 return session;
273 }
274 }
275 read_unlock_bh(&tunnel->hlist_lock);
276
277 return NULL;
278}
279EXPORT_SYMBOL_GPL(l2tp_session_find);
280
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200281/* Like l2tp_session_find() but takes a reference on the returned session.
282 * Optionally calls session->ref() too if do_ref is true.
283 */
284struct l2tp_session *l2tp_session_get(struct net *net,
285 struct l2tp_tunnel *tunnel,
286 u32 session_id, bool do_ref)
287{
288 struct hlist_head *session_list;
289 struct l2tp_session *session;
290
291 if (!tunnel) {
292 struct l2tp_net *pn = l2tp_pernet(net);
293
294 session_list = l2tp_session_id_hash_2(pn, session_id);
295
296 rcu_read_lock_bh();
297 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
298 if (session->session_id == session_id) {
299 l2tp_session_inc_refcount(session);
300 if (do_ref && session->ref)
301 session->ref(session);
302 rcu_read_unlock_bh();
303
304 return session;
305 }
306 }
307 rcu_read_unlock_bh();
308
309 return NULL;
310 }
311
312 session_list = l2tp_session_id_hash(tunnel, session_id);
313 read_lock_bh(&tunnel->hlist_lock);
314 hlist_for_each_entry(session, session_list, hlist) {
315 if (session->session_id == session_id) {
316 l2tp_session_inc_refcount(session);
317 if (do_ref && session->ref)
318 session->ref(session);
319 read_unlock_bh(&tunnel->hlist_lock);
320
321 return session;
322 }
323 }
324 read_unlock_bh(&tunnel->hlist_lock);
325
326 return NULL;
327}
328EXPORT_SYMBOL_GPL(l2tp_session_get);
329
Guillaume Naultb7902602017-04-03 12:03:13 +0200330struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
331 bool do_ref)
James Chapmanfd558d12010-04-02 06:18:33 +0000332{
333 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000334 struct l2tp_session *session;
335 int count = 0;
336
337 read_lock_bh(&tunnel->hlist_lock);
338 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800339 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000340 if (++count > nth) {
Guillaume Naultb7902602017-04-03 12:03:13 +0200341 l2tp_session_inc_refcount(session);
342 if (do_ref && session->ref)
343 session->ref(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000344 read_unlock_bh(&tunnel->hlist_lock);
345 return session;
346 }
347 }
348 }
349
350 read_unlock_bh(&tunnel->hlist_lock);
351
352 return NULL;
353}
Guillaume Naultb7902602017-04-03 12:03:13 +0200354EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000355
James Chapman309795f2010-04-02 06:19:10 +0000356/* Lookup a session by interface name.
357 * This is very inefficient but is only used by management interfaces.
358 */
Guillaume Nault08cb8e52017-03-31 13:02:30 +0200359struct l2tp_session *l2tp_session_get_by_ifname(struct net *net, char *ifname,
360 bool do_ref)
James Chapman309795f2010-04-02 06:19:10 +0000361{
362 struct l2tp_net *pn = l2tp_pernet(net);
363 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000364 struct l2tp_session *session;
365
James Chapmane02d4942010-04-02 06:19:16 +0000366 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000367 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800368 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000369 if (!strcmp(session->ifname, ifname)) {
Guillaume Nault08cb8e52017-03-31 13:02:30 +0200370 l2tp_session_inc_refcount(session);
371 if (do_ref && session->ref)
372 session->ref(session);
James Chapmane02d4942010-04-02 06:19:16 +0000373 rcu_read_unlock_bh();
Guillaume Nault08cb8e52017-03-31 13:02:30 +0200374
James Chapman309795f2010-04-02 06:19:10 +0000375 return session;
376 }
377 }
378 }
379
James Chapmane02d4942010-04-02 06:19:16 +0000380 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000381
382 return NULL;
383}
Guillaume Nault08cb8e52017-03-31 13:02:30 +0200384EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
James Chapman309795f2010-04-02 06:19:10 +0000385
Guillaume Naultd9face62017-03-31 13:02:27 +0200386static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel,
387 struct l2tp_session *session)
388{
389 struct l2tp_session *session_walk;
390 struct hlist_head *g_head;
391 struct hlist_head *head;
392 struct l2tp_net *pn;
393
394 head = l2tp_session_id_hash(tunnel, session->session_id);
395
396 write_lock_bh(&tunnel->hlist_lock);
397 hlist_for_each_entry(session_walk, head, hlist)
398 if (session_walk->session_id == session->session_id)
399 goto exist;
400
401 if (tunnel->version == L2TP_HDR_VER_3) {
402 pn = l2tp_pernet(tunnel->l2tp_net);
403 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
404 session->session_id);
405
406 spin_lock_bh(&pn->l2tp_session_hlist_lock);
407 hlist_for_each_entry(session_walk, g_head, global_hlist)
408 if (session_walk->session_id == session->session_id)
409 goto exist_glob;
410
411 hlist_add_head_rcu(&session->global_hlist, g_head);
412 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
413 }
414
415 hlist_add_head(&session->hlist, head);
416 write_unlock_bh(&tunnel->hlist_lock);
417
418 return 0;
419
420exist_glob:
421 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
422exist:
423 write_unlock_bh(&tunnel->hlist_lock);
424
425 return -EEXIST;
426}
427
James Chapmanfd558d12010-04-02 06:18:33 +0000428/* Lookup a tunnel by id
429 */
430struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
431{
432 struct l2tp_tunnel *tunnel;
433 struct l2tp_net *pn = l2tp_pernet(net);
434
James Chapmane02d4942010-04-02 06:19:16 +0000435 rcu_read_lock_bh();
436 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000437 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000438 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000439 return tunnel;
440 }
441 }
James Chapmane02d4942010-04-02 06:19:16 +0000442 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000443
444 return NULL;
445}
446EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
447
448struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
449{
450 struct l2tp_net *pn = l2tp_pernet(net);
451 struct l2tp_tunnel *tunnel;
452 int count = 0;
453
James Chapmane02d4942010-04-02 06:19:16 +0000454 rcu_read_lock_bh();
455 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000456 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000457 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000458 return tunnel;
459 }
460 }
461
James Chapmane02d4942010-04-02 06:19:16 +0000462 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000463
464 return NULL;
465}
466EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
467
468/*****************************************************************************
469 * Receive data handling
470 *****************************************************************************/
471
472/* Queue a skb in order. We come here only if the skb has an L2TP sequence
473 * number.
474 */
475static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
476{
477 struct sk_buff *skbp;
478 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000479 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000480
481 spin_lock_bh(&session->reorder_q.lock);
482 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
483 if (L2TP_SKB_CB(skbp)->ns > ns) {
484 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000485 l2tp_dbg(session, L2TP_MSG_SEQ,
486 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
487 session->name, ns, L2TP_SKB_CB(skbp)->ns,
488 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000489 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000490 goto out;
491 }
492 }
493
494 __skb_queue_tail(&session->reorder_q, skb);
495
496out:
497 spin_unlock_bh(&session->reorder_q.lock);
498}
499
500/* Dequeue a single skb.
501 */
502static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
503{
504 struct l2tp_tunnel *tunnel = session->tunnel;
505 int length = L2TP_SKB_CB(skb)->length;
506
507 /* We're about to requeue the skb, so return resources
508 * to its current owner (a socket receive buffer).
509 */
510 skb_orphan(skb);
511
Tom Parkin7b7c0712013-03-19 06:11:22 +0000512 atomic_long_inc(&tunnel->stats.rx_packets);
513 atomic_long_add(length, &tunnel->stats.rx_bytes);
514 atomic_long_inc(&session->stats.rx_packets);
515 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000516
517 if (L2TP_SKB_CB(skb)->has_seq) {
518 /* Bump our Nr */
519 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100520 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000521
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000522 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
523 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000524 }
525
526 /* call private receive handler */
527 if (session->recv_skb != NULL)
528 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
529 else
530 kfree_skb(skb);
531
532 if (session->deref)
533 (*session->deref)(session);
534}
535
536/* Dequeue skbs from the session's reorder_q, subject to packet order.
537 * Skbs that have been in the queue for too long are simply discarded.
538 */
539static void l2tp_recv_dequeue(struct l2tp_session *session)
540{
541 struct sk_buff *skb;
542 struct sk_buff *tmp;
543
544 /* If the pkt at the head of the queue has the nr that we
545 * expect to send up next, dequeue it and any other
546 * in-sequence packets behind it.
547 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000548start:
James Chapmanfd558d12010-04-02 06:18:33 +0000549 spin_lock_bh(&session->reorder_q.lock);
550 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
551 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000552 atomic_long_inc(&session->stats.rx_seq_discards);
553 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000554 l2tp_dbg(session, L2TP_MSG_SEQ,
555 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
556 session->name, L2TP_SKB_CB(skb)->ns,
557 L2TP_SKB_CB(skb)->length, session->nr,
558 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000559 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000560 __skb_unlink(skb, &session->reorder_q);
561 kfree_skb(skb);
562 if (session->deref)
563 (*session->deref)(session);
564 continue;
565 }
566
567 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000568 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000569 l2tp_dbg(session, L2TP_MSG_SEQ,
570 "%s: advancing nr to next pkt: %u -> %u",
571 session->name, session->nr,
572 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000573 session->reorder_skip = 0;
574 session->nr = L2TP_SKB_CB(skb)->ns;
575 }
James Chapmanfd558d12010-04-02 06:18:33 +0000576 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000577 l2tp_dbg(session, L2TP_MSG_SEQ,
578 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
579 session->name, L2TP_SKB_CB(skb)->ns,
580 L2TP_SKB_CB(skb)->length, session->nr,
581 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000582 goto out;
583 }
584 }
585 __skb_unlink(skb, &session->reorder_q);
586
587 /* Process the skb. We release the queue lock while we
588 * do so to let other contexts process the queue.
589 */
590 spin_unlock_bh(&session->reorder_q.lock);
591 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000592 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000593 }
594
595out:
596 spin_unlock_bh(&session->reorder_q.lock);
597}
598
James Chapman8a1631d2013-07-02 20:28:59 +0100599static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
600{
601 u32 nws;
602
603 if (nr >= session->nr)
604 nws = nr - session->nr;
605 else
606 nws = (session->nr_max + 1) - (session->nr - nr);
607
608 return nws < session->nr_window_size;
609}
610
James Chapmanb6dc01a2013-07-02 20:28:58 +0100611/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
612 * acceptable, else non-zero.
613 */
614static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
615{
James Chapman8a1631d2013-07-02 20:28:59 +0100616 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
617 /* Packet sequence number is outside allowed window.
618 * Discard it.
619 */
620 l2tp_dbg(session, L2TP_MSG_SEQ,
621 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
622 session->name, L2TP_SKB_CB(skb)->ns,
623 L2TP_SKB_CB(skb)->length, session->nr);
624 goto discard;
625 }
626
James Chapmanb6dc01a2013-07-02 20:28:58 +0100627 if (session->reorder_timeout != 0) {
628 /* Packet reordering enabled. Add skb to session's
629 * reorder queue, in order of ns.
630 */
631 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100632 goto out;
633 }
634
635 /* Packet reordering disabled. Discard out-of-sequence packets, while
636 * tracking the number if in-sequence packets after the first OOS packet
637 * is seen. After nr_oos_count_max in-sequence packets, reset the
638 * sequence number to re-enable packet reception.
639 */
640 if (L2TP_SKB_CB(skb)->ns == session->nr) {
641 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100642 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100643 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
644 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
645
646 if (nr_oos == nr_next)
647 session->nr_oos_count++;
648 else
649 session->nr_oos_count = 0;
650
651 session->nr_oos = nr_oos;
652 if (session->nr_oos_count > session->nr_oos_count_max) {
653 session->reorder_skip = 1;
654 l2tp_dbg(session, L2TP_MSG_SEQ,
655 "%s: %d oos packets received. Resetting sequence numbers\n",
656 session->name, session->nr_oos_count);
657 }
658 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100659 atomic_long_inc(&session->stats.rx_seq_discards);
660 l2tp_dbg(session, L2TP_MSG_SEQ,
661 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
662 session->name, L2TP_SKB_CB(skb)->ns,
663 L2TP_SKB_CB(skb)->length, session->nr,
664 skb_queue_len(&session->reorder_q));
665 goto discard;
666 }
667 skb_queue_tail(&session->reorder_q, skb);
668 }
669
James Chapmana0dbd822013-07-02 20:29:00 +0100670out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100671 return 0;
672
673discard:
674 return 1;
675}
676
James Chapmanf7faffa2010-04-02 06:18:49 +0000677/* Do receive processing of L2TP data frames. We handle both L2TPv2
678 * and L2TPv3 data frames here.
679 *
680 * L2TPv2 Data Message Header
681 *
682 * 0 1 2 3
683 * 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
684 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
685 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
686 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
687 * | Tunnel ID | Session ID |
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689 * | Ns (opt) | Nr (opt) |
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * | Offset Size (opt) | Offset pad... (opt)
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 *
694 * Data frames are marked by T=0. All other fields are the same as
695 * those in L2TP control frames.
696 *
697 * L2TPv3 Data Message Header
698 *
699 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
700 * | L2TP Session Header |
701 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
702 * | L2-Specific Sublayer |
703 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
704 * | Tunnel Payload ...
705 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
706 *
707 * L2TPv3 Session Header Over IP
708 *
709 * 0 1 2 3
710 * 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
711 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
712 * | Session ID |
713 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
714 * | Cookie (optional, maximum 64 bits)...
715 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
716 * |
717 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
718 *
719 * L2TPv3 L2-Specific Sublayer Format
720 *
721 * 0 1 2 3
722 * 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
723 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
724 * |x|S|x|x|x|x|x|x| Sequence Number |
725 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
726 *
727 * Cookie value, sublayer format and offset (pad) are negotiated with
728 * the peer when the session is set up. Unlike L2TPv2, we do not need
729 * to parse the packet header to determine if optional fields are
730 * present.
731 *
732 * Caller must already have parsed the frame and determined that it is
733 * a data (not control) frame before coming here. Fields up to the
734 * session-id have already been parsed and ptr points to the data
735 * after the session-id.
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200736 *
737 * session->ref() must have been called prior to l2tp_recv_common().
738 * session->deref() will be called automatically after skb is processed.
James Chapmanfd558d12010-04-02 06:18:33 +0000739 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000740void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
741 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
742 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000743{
James Chapmanf7faffa2010-04-02 06:18:49 +0000744 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000745 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000746 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000747
James Chapmanf7faffa2010-04-02 06:18:49 +0000748 /* Parse and check optional cookie */
749 if (session->peer_cookie_len > 0) {
750 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000751 l2tp_info(tunnel, L2TP_MSG_DATA,
752 "%s: cookie mismatch (%u/%u). Discarding.\n",
753 tunnel->name, tunnel->tunnel_id,
754 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000755 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000756 goto discard;
757 }
758 ptr += session->peer_cookie_len;
759 }
760
James Chapmanfd558d12010-04-02 06:18:33 +0000761 /* Handle the optional sequence numbers. Sequence numbers are
762 * in different places for L2TPv2 and L2TPv3.
763 *
764 * If we are the LAC, enable/disable sequence numbers under
765 * the control of the LNS. If no sequence numbers present but
766 * we were expecting them, discard frame.
767 */
768 ns = nr = 0;
769 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000770 if (tunnel->version == L2TP_HDR_VER_2) {
771 if (hdrflags & L2TP_HDRFLAG_S) {
772 ns = ntohs(*(__be16 *) ptr);
773 ptr += 2;
774 nr = ntohs(*(__be16 *) ptr);
775 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000776
James Chapmanf7faffa2010-04-02 06:18:49 +0000777 /* Store L2TP info in the skb */
778 L2TP_SKB_CB(skb)->ns = ns;
779 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000780
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000781 l2tp_dbg(session, L2TP_MSG_SEQ,
782 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
783 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000784 }
785 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
786 u32 l2h = ntohl(*(__be32 *) ptr);
787
788 if (l2h & 0x40000000) {
789 ns = l2h & 0x00ffffff;
790
791 /* Store L2TP info in the skb */
792 L2TP_SKB_CB(skb)->ns = ns;
793 L2TP_SKB_CB(skb)->has_seq = 1;
794
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000795 l2tp_dbg(session, L2TP_MSG_SEQ,
796 "%s: recv data ns=%u, session nr=%u\n",
797 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000798 }
James Chapmanfd558d12010-04-02 06:18:33 +0000799 }
800
James Chapmanf7faffa2010-04-02 06:18:49 +0000801 /* Advance past L2-specific header, if present */
802 ptr += session->l2specific_len;
803
James Chapmanfd558d12010-04-02 06:18:33 +0000804 if (L2TP_SKB_CB(skb)->has_seq) {
805 /* Received a packet with sequence numbers. If we're the LNS,
806 * check if we sre sending sequence numbers and if not,
807 * configure it so.
808 */
809 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000810 l2tp_info(session, L2TP_MSG_SEQ,
811 "%s: requested to enable seq numbers by LNS\n",
812 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000813 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000814 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000815 }
816 } else {
817 /* No sequence numbers.
818 * If user has configured mandatory sequence numbers, discard.
819 */
820 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000821 l2tp_warn(session, L2TP_MSG_SEQ,
822 "%s: recv data has no seq numbers when required. Discarding.\n",
823 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000824 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000825 goto discard;
826 }
827
828 /* If we're the LAC and we're sending sequence numbers, the
829 * LNS has requested that we no longer send sequence numbers.
830 * If we're the LNS and we're sending sequence numbers, the
831 * LAC is broken. Discard the frame.
832 */
833 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000834 l2tp_info(session, L2TP_MSG_SEQ,
835 "%s: requested to disable seq numbers by LNS\n",
836 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000837 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000838 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000839 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000840 l2tp_warn(session, L2TP_MSG_SEQ,
841 "%s: recv data has no seq numbers when required. Discarding.\n",
842 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000843 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000844 goto discard;
845 }
846 }
847
James Chapman65b05d02018-01-03 22:48:06 +0000848 /* Session data offset is defined only for L2TPv2 and is
849 * indicated by an optional 16-bit value in the header.
James Chapmanf7faffa2010-04-02 06:18:49 +0000850 */
851 if (tunnel->version == L2TP_HDR_VER_2) {
852 /* If offset bit set, skip it. */
853 if (hdrflags & L2TP_HDRFLAG_O) {
854 offset = ntohs(*(__be16 *)ptr);
855 ptr += 2 + offset;
856 }
James Chapman65b05d02018-01-03 22:48:06 +0000857 }
James Chapmanfd558d12010-04-02 06:18:33 +0000858
859 offset = ptr - optr;
860 if (!pskb_may_pull(skb, offset))
861 goto discard;
862
863 __skb_pull(skb, offset);
864
865 /* If caller wants to process the payload before we queue the
866 * packet, do so now.
867 */
868 if (payload_hook)
869 if ((*payload_hook)(skb))
870 goto discard;
871
872 /* Prepare skb for adding to the session's reorder_q. Hold
873 * packets for max reorder_timeout or 1 second if not
874 * reordering.
875 */
876 L2TP_SKB_CB(skb)->length = length;
877 L2TP_SKB_CB(skb)->expires = jiffies +
878 (session->reorder_timeout ? session->reorder_timeout : HZ);
879
880 /* Add packet to the session's receive queue. Reordering is done here, if
881 * enabled. Saved L2TP protocol info is stored in skb->sb[].
882 */
883 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100884 if (l2tp_recv_data_seq(session, skb))
885 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000886 } else {
887 /* No sequence numbers. Add the skb to the tail of the
888 * reorder queue. This ensures that it will be
889 * delivered after all previous sequenced skbs.
890 */
891 skb_queue_tail(&session->reorder_q, skb);
892 }
893
894 /* Try to dequeue as many skbs from reorder_q as we can. */
895 l2tp_recv_dequeue(session);
896
James Chapmanf7faffa2010-04-02 06:18:49 +0000897 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000898
899discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000900 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000901 kfree_skb(skb);
902
903 if (session->deref)
904 (*session->deref)(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000905}
906EXPORT_SYMBOL(l2tp_recv_common);
907
Tom Parkin48f72f92013-03-19 06:11:19 +0000908/* Drop skbs from the session's reorder_q
909 */
910int l2tp_session_queue_purge(struct l2tp_session *session)
911{
912 struct sk_buff *skb = NULL;
913 BUG_ON(!session);
914 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
915 while ((skb = skb_dequeue(&session->reorder_q))) {
916 atomic_long_inc(&session->stats.rx_errors);
917 kfree_skb(skb);
918 if (session->deref)
919 (*session->deref)(session);
920 }
921 return 0;
922}
923EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
924
James Chapmanf7faffa2010-04-02 06:18:49 +0000925/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
926 * here. The skb is not on a list when we get here.
927 * Returns 0 if the packet was a data packet and was successfully passed on.
928 * Returns 1 if the packet was not a good data packet and could not be
929 * forwarded. All such packets are passed up to userspace to deal with.
930 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000931static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
932 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000933{
934 struct l2tp_session *session = NULL;
935 unsigned char *ptr, *optr;
936 u16 hdrflags;
937 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000938 u16 version;
939 int length;
940
Tom Herbert58d60852014-05-07 16:52:48 -0700941 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000942
943 /* UDP always verifies the packet length. */
944 __skb_pull(skb, sizeof(struct udphdr));
945
946 /* Short packet? */
947 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000948 l2tp_info(tunnel, L2TP_MSG_DATA,
949 "%s: recv short packet (len=%d)\n",
950 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000951 goto error;
952 }
953
James Chapmanf7faffa2010-04-02 06:18:49 +0000954 /* Trace packet contents, if enabled */
955 if (tunnel->debug & L2TP_MSG_DATA) {
956 length = min(32u, skb->len);
957 if (!pskb_may_pull(skb, length))
958 goto error;
959
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000960 pr_debug("%s: recv\n", tunnel->name);
961 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000962 }
963
Eric Dumazete50e7052011-11-08 13:59:44 -0500964 /* Point to L2TP header */
965 optr = ptr = skb->data;
966
James Chapmanf7faffa2010-04-02 06:18:49 +0000967 /* Get L2TP header flags */
968 hdrflags = ntohs(*(__be16 *) ptr);
969
970 /* Check protocol version */
971 version = hdrflags & L2TP_HDR_VER_MASK;
972 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000973 l2tp_info(tunnel, L2TP_MSG_DATA,
974 "%s: recv protocol version mismatch: got %d expected %d\n",
975 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000976 goto error;
977 }
978
979 /* Get length of L2TP packet */
980 length = skb->len;
981
982 /* If type is control packet, it is handled by userspace. */
983 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000984 l2tp_dbg(tunnel, L2TP_MSG_DATA,
985 "%s: recv control packet, len=%d\n",
986 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000987 goto error;
988 }
989
990 /* Skip flags */
991 ptr += 2;
992
993 if (tunnel->version == L2TP_HDR_VER_2) {
994 /* If length is present, skip it */
995 if (hdrflags & L2TP_HDRFLAG_L)
996 ptr += 2;
997
998 /* Extract tunnel and session ID */
999 tunnel_id = ntohs(*(__be16 *) ptr);
1000 ptr += 2;
1001 session_id = ntohs(*(__be16 *) ptr);
1002 ptr += 2;
1003 } else {
1004 ptr += 2; /* skip reserved bits */
1005 tunnel_id = tunnel->tunnel_id;
1006 session_id = ntohl(*(__be32 *) ptr);
1007 ptr += 4;
1008 }
1009
1010 /* Find the session context */
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001011 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
James Chapman309795f2010-04-02 06:19:10 +00001012 if (!session || !session->recv_skb) {
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001013 if (session) {
1014 if (session->deref)
1015 session->deref(session);
1016 l2tp_session_dec_refcount(session);
1017 }
1018
James Chapmanf7faffa2010-04-02 06:18:49 +00001019 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001020 l2tp_info(tunnel, L2TP_MSG_DATA,
1021 "%s: no session found (%u/%u). Passing up.\n",
1022 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001023 goto error;
1024 }
1025
1026 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001027 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001028
1029 return 0;
1030
James Chapmanfd558d12010-04-02 06:18:33 +00001031error:
1032 /* Put UDP header back */
1033 __skb_push(skb, sizeof(struct udphdr));
1034
1035 return 1;
1036}
James Chapmanfd558d12010-04-02 06:18:33 +00001037
1038/* UDP encapsulation receive handler. See net/ipv4/udp.c.
1039 * Return codes:
1040 * 0 : success.
1041 * <0: error
1042 * >0: skb should be passed up to userspace as UDP.
1043 */
1044int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1045{
1046 struct l2tp_tunnel *tunnel;
1047
1048 tunnel = l2tp_sock_to_tunnel(sk);
1049 if (tunnel == NULL)
1050 goto pass_up;
1051
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001052 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1053 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001054
1055 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1056 goto pass_up_put;
1057
1058 sock_put(sk);
1059 return 0;
1060
1061pass_up_put:
1062 sock_put(sk);
1063pass_up:
1064 return 1;
1065}
1066EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1067
1068/************************************************************************
1069 * Transmit handling
1070 ***********************************************************************/
1071
1072/* Build an L2TP header for the session into the buffer provided.
1073 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001074static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001075{
James Chapmanf7faffa2010-04-02 06:18:49 +00001076 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001077 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001078 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001079 u16 flags = L2TP_HDR_VER_2;
1080 u32 tunnel_id = tunnel->peer_tunnel_id;
1081 u32 session_id = session->peer_session_id;
1082
1083 if (session->send_seq)
1084 flags |= L2TP_HDRFLAG_S;
1085
1086 /* Setup L2TP header. */
1087 *bufp++ = htons(flags);
1088 *bufp++ = htons(tunnel_id);
1089 *bufp++ = htons(session_id);
1090 if (session->send_seq) {
1091 *bufp++ = htons(session->ns);
1092 *bufp++ = 0;
1093 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001094 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001095 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1096 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001097 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001098
1099 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001100}
1101
James Chapmanf7faffa2010-04-02 06:18:49 +00001102static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001103{
James Chapman0d767512010-04-02 06:19:00 +00001104 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001105 char *bufp = buf;
1106 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001107
James Chapman0d767512010-04-02 06:19:00 +00001108 /* Setup L2TP header. The header differs slightly for UDP and
1109 * IP encapsulations. For UDP, there is 4 bytes of flags.
1110 */
1111 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1112 u16 flags = L2TP_HDR_VER_3;
1113 *((__be16 *) bufp) = htons(flags);
1114 bufp += 2;
1115 *((__be16 *) bufp) = 0;
1116 bufp += 2;
1117 }
1118
James Chapmanf7faffa2010-04-02 06:18:49 +00001119 *((__be32 *) bufp) = htonl(session->peer_session_id);
1120 bufp += 4;
1121 if (session->cookie_len) {
1122 memcpy(bufp, &session->cookie[0], session->cookie_len);
1123 bufp += session->cookie_len;
1124 }
1125 if (session->l2specific_len) {
1126 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1127 u32 l2h = 0;
1128 if (session->send_seq) {
1129 l2h = 0x40000000 | session->ns;
1130 session->ns++;
1131 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001132 l2tp_dbg(session, L2TP_MSG_SEQ,
1133 "%s: updated ns to %u\n",
1134 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001135 }
1136
1137 *((__be32 *) bufp) = htonl(l2h);
1138 }
1139 bufp += session->l2specific_len;
1140 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001141
1142 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001143}
James Chapmanfd558d12010-04-02 06:18:33 +00001144
stephen hemmingerfc130842010-10-21 07:50:46 +00001145static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001146 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001147{
1148 struct l2tp_tunnel *tunnel = session->tunnel;
1149 unsigned int len = skb->len;
1150 int error;
1151
1152 /* Debug */
1153 if (session->send_seq)
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001154 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1155 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001156 else
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001157 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1158 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001159
1160 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001161 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1162 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001163
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001164 pr_debug("%s: xmit\n", session->name);
1165 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1166 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001167 }
1168
1169 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001170 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001171#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001172 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001173 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001174 else
1175#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001176 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001177
1178 /* Update stats */
1179 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001180 atomic_long_inc(&tunnel->stats.tx_packets);
1181 atomic_long_add(len, &tunnel->stats.tx_bytes);
1182 atomic_long_inc(&session->stats.tx_packets);
1183 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001184 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001185 atomic_long_inc(&tunnel->stats.tx_errors);
1186 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001187 }
1188
1189 return 0;
1190}
James Chapmanfd558d12010-04-02 06:18:33 +00001191
James Chapmanfd558d12010-04-02 06:18:33 +00001192/* If caller requires the skb to have a ppp header, the header must be
1193 * inserted in the skb data before calling this function.
1194 */
1195int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1196{
1197 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001198 struct l2tp_tunnel *tunnel = session->tunnel;
1199 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001200 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001201 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001202 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001203 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001204 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1205 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001206 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001207
1208 /* Check that there's enough headroom in the skb to insert IP,
1209 * UDP and L2TP headers. If not enough, expand it to
1210 * make room. Adjust truesize.
1211 */
1212 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001213 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001214 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001215 kfree_skb(skb);
1216 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001217 }
James Chapmanfd558d12010-04-02 06:18:33 +00001218
James Chapmanfd558d12010-04-02 06:18:33 +00001219 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001220 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001221
James Chapman0d767512010-04-02 06:19:00 +00001222 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001223 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1224 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1225 IPSKB_REROUTED);
1226 nf_reset(skb);
1227
David S. Miller6af88da2011-05-08 13:45:20 -07001228 bh_lock_sock(sk);
1229 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001230 kfree_skb(skb);
1231 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001232 goto out_unlock;
1233 }
1234
James Chapmanfd558d12010-04-02 06:18:33 +00001235 /* Get routing info from the tunnel socket */
1236 skb_dst_drop(skb);
Wei Wangae7d5062018-08-10 11:14:56 -07001237 skb_dst_set(skb, sk_dst_check(sk, 0));
James Chapmanfd558d12010-04-02 06:18:33 +00001238
David S. Millerd9d8da82011-05-06 22:23:20 -07001239 inet = inet_sk(sk);
1240 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001241 switch (tunnel->encap) {
1242 case L2TP_ENCAPTYPE_UDP:
1243 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001244 __skb_push(skb, sizeof(*uh));
1245 skb_reset_transport_header(skb);
1246 uh = udp_hdr(skb);
1247 uh->source = inet->inet_sport;
1248 uh->dest = inet->inet_dport;
1249 udp_len = uhlen + hdr_len + data_len;
1250 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001251
1252 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001253#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001254 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Tom Herbert77157e12014-06-04 17:19:56 -07001255 udp6_set_csum(udp_get_no_check6_tx(sk),
1256 skb, &inet6_sk(sk)->saddr,
1257 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001258 else
1259#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001260 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1261 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001262 break;
1263
1264 case L2TP_ENCAPTYPE_IP:
1265 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001266 }
1267
David S. Millerd9d8da82011-05-06 22:23:20 -07001268 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001269out_unlock:
1270 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001271
Eric Dumazetb8c84302012-06-28 20:15:13 +00001272 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001273}
1274EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1275
1276/*****************************************************************************
1277 * Tinnel and session create/destroy.
1278 *****************************************************************************/
1279
1280/* Tunnel socket destruct hook.
1281 * The tunnel context is deleted only when all session sockets have been
1282 * closed.
1283 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001284static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001285{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001286 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001287 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001288
James Chapmanfd558d12010-04-02 06:18:33 +00001289 if (tunnel == NULL)
1290 goto end;
1291
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001292 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001293
James Chapmanfd558d12010-04-02 06:18:33 +00001294
Tom Parkinf8ccac02013-01-31 23:43:00 +00001295 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001296 switch (tunnel->encap) {
1297 case L2TP_ENCAPTYPE_UDP:
1298 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1299 (udp_sk(sk))->encap_type = 0;
1300 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001301 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001302 break;
1303 case L2TP_ENCAPTYPE_IP:
1304 break;
1305 }
James Chapmanfd558d12010-04-02 06:18:33 +00001306
1307 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001308 sk->sk_destruct = tunnel->old_sk_destruct;
1309 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001310 tunnel->sock = NULL;
1311
1312 /* Remove the tunnel struct from the tunnel list */
1313 pn = l2tp_pernet(tunnel->l2tp_net);
1314 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1315 list_del_rcu(&tunnel->list);
1316 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1317 atomic_dec(&l2tp_tunnel_count);
1318
1319 l2tp_tunnel_closeall(tunnel);
1320 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001321
1322 /* Call the original destructor */
1323 if (sk->sk_destruct)
1324 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001325end:
1326 return;
1327}
James Chapmanfd558d12010-04-02 06:18:33 +00001328
1329/* When the tunnel is closed, all the attached sessions need to go too.
1330 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001331void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001332{
1333 int hash;
1334 struct hlist_node *walk;
1335 struct hlist_node *tmp;
1336 struct l2tp_session *session;
1337
1338 BUG_ON(tunnel == NULL);
1339
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001340 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1341 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001342
1343 write_lock_bh(&tunnel->hlist_lock);
1344 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1345again:
1346 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1347 session = hlist_entry(walk, struct l2tp_session, hlist);
1348
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001349 l2tp_info(session, L2TP_MSG_CONTROL,
1350 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001351
1352 hlist_del_init(&session->hlist);
1353
James Chapmanfd558d12010-04-02 06:18:33 +00001354 if (session->ref != NULL)
1355 (*session->ref)(session);
1356
1357 write_unlock_bh(&tunnel->hlist_lock);
1358
Tom Parkinf6e16b22013-03-19 06:11:23 +00001359 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001360 l2tp_session_queue_purge(session);
1361
James Chapmanfd558d12010-04-02 06:18:33 +00001362 if (session->session_close != NULL)
1363 (*session->session_close)(session);
1364
1365 if (session->deref != NULL)
1366 (*session->deref)(session);
1367
Tom Parkin9980d002013-03-19 06:11:13 +00001368 l2tp_session_dec_refcount(session);
1369
James Chapmanfd558d12010-04-02 06:18:33 +00001370 write_lock_bh(&tunnel->hlist_lock);
1371
1372 /* Now restart from the beginning of this hash
1373 * chain. We always remove a session from the
1374 * list so we are guaranteed to make forward
1375 * progress.
1376 */
1377 goto again;
1378 }
1379 }
1380 write_unlock_bh(&tunnel->hlist_lock);
1381}
Tom Parkine34f4c72013-03-19 06:11:14 +00001382EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001383
Tom Parkin9980d002013-03-19 06:11:13 +00001384/* Tunnel socket destroy hook for UDP encapsulation */
1385static void l2tp_udp_encap_destroy(struct sock *sk)
1386{
1387 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1388 if (tunnel) {
1389 l2tp_tunnel_closeall(tunnel);
1390 sock_put(sk);
1391 }
1392}
1393
James Chapmanfd558d12010-04-02 06:18:33 +00001394/* Really kill the tunnel.
1395 * Come here only when all sessions have been cleared from the tunnel.
1396 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001397static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001398{
James Chapmanfd558d12010-04-02 06:18:33 +00001399 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1400 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001401 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001402 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001403}
James Chapmanfd558d12010-04-02 06:18:33 +00001404
Tom Parkinf8ccac02013-01-31 23:43:00 +00001405/* Workqueue tunnel deletion function */
1406static void l2tp_tunnel_del_work(struct work_struct *work)
1407{
1408 struct l2tp_tunnel *tunnel = NULL;
1409 struct socket *sock = NULL;
1410 struct sock *sk = NULL;
1411
1412 tunnel = container_of(work, struct l2tp_tunnel, del_work);
Ridge Kennedye5941132017-02-22 14:59:49 +13001413
1414 l2tp_tunnel_closeall(tunnel);
1415
Tom Parkinf8ccac02013-01-31 23:43:00 +00001416 sk = l2tp_tunnel_sock_lookup(tunnel);
1417 if (!sk)
Alexander Couzens06a15f52015-09-28 11:32:42 +02001418 goto out;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001419
1420 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001421
Tom Parkin02d13ed2013-03-19 06:11:18 +00001422 /* If the tunnel socket was created by userspace, then go through the
1423 * inet layer to shut the socket down, and let userspace close it.
1424 * Otherwise, if we created the socket directly within the kernel, use
1425 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001426 * In either case the tunnel resources are freed in the socket
1427 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001428 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001429 if (tunnel->fd >= 0) {
1430 if (sock)
1431 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001432 } else {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001433 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001434 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001435 sock_release(sock);
1436 }
Tom Parkin167eb172013-01-31 23:43:03 +00001437 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001438
1439 l2tp_tunnel_sock_put(sk);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001440out:
1441 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001442}
James Chapmanfd558d12010-04-02 06:18:33 +00001443
James Chapman789a4a22010-04-02 06:19:40 +00001444/* Create a socket for the tunnel, if one isn't set up by
1445 * userspace. This is used for static tunnels where there is no
1446 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001447 *
1448 * Since we don't want these sockets to keep a namespace alive by
1449 * themselves, we drop the socket's namespace refcount after creation.
1450 * These sockets are freed when the namespace exits using the pernet
1451 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001452 */
Tom Parkin167eb172013-01-31 23:43:03 +00001453static int l2tp_tunnel_sock_create(struct net *net,
1454 u32 tunnel_id,
1455 u32 peer_tunnel_id,
1456 struct l2tp_tunnel_cfg *cfg,
1457 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001458{
1459 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001460 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001461 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001462
1463 switch (cfg->encap) {
1464 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001465 memset(&udp_conf, 0, sizeof(udp_conf));
1466
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001467#if IS_ENABLED(CONFIG_IPV6)
1468 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001469 udp_conf.family = AF_INET6;
1470 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1471 sizeof(udp_conf.local_ip6));
1472 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1473 sizeof(udp_conf.peer_ip6));
1474 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001475 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001476 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001477 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001478 } else
1479#endif
1480 {
Tom Herbert85644b42014-07-13 19:49:48 -07001481 udp_conf.family = AF_INET;
1482 udp_conf.local_ip = cfg->local_ip;
1483 udp_conf.peer_ip = cfg->peer_ip;
1484 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001485 }
James Chapman789a4a22010-04-02 06:19:40 +00001486
Tom Herbert85644b42014-07-13 19:49:48 -07001487 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1488 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1489
1490 err = udp_sock_create(net, &udp_conf, &sock);
1491 if (err < 0)
1492 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001493
1494 break;
1495
1496 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001497#if IS_ENABLED(CONFIG_IPV6)
1498 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001499 struct sockaddr_l2tpip6 ip6_addr = {0};
1500
Eric W. Biederman26abe142015-05-08 21:10:31 -05001501 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001502 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001503 if (err < 0)
1504 goto out;
1505
James Chapman5dac94e2012-04-29 21:48:55 +00001506 ip6_addr.l2tp_family = AF_INET6;
1507 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1508 sizeof(ip6_addr.l2tp_addr));
1509 ip6_addr.l2tp_conn_id = tunnel_id;
1510 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1511 sizeof(ip6_addr));
1512 if (err < 0)
1513 goto out;
1514
1515 ip6_addr.l2tp_family = AF_INET6;
1516 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1517 sizeof(ip6_addr.l2tp_addr));
1518 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1519 err = kernel_connect(sock,
1520 (struct sockaddr *) &ip6_addr,
1521 sizeof(ip6_addr), 0);
1522 if (err < 0)
1523 goto out;
1524 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001525#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001526 {
Tom Herbert85644b42014-07-13 19:49:48 -07001527 struct sockaddr_l2tpip ip_addr = {0};
1528
Eric W. Biederman26abe142015-05-08 21:10:31 -05001529 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001530 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001531 if (err < 0)
1532 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001533
James Chapman5dac94e2012-04-29 21:48:55 +00001534 ip_addr.l2tp_family = AF_INET;
1535 ip_addr.l2tp_addr = cfg->local_ip;
1536 ip_addr.l2tp_conn_id = tunnel_id;
1537 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1538 sizeof(ip_addr));
1539 if (err < 0)
1540 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001541
James Chapman5dac94e2012-04-29 21:48:55 +00001542 ip_addr.l2tp_family = AF_INET;
1543 ip_addr.l2tp_addr = cfg->peer_ip;
1544 ip_addr.l2tp_conn_id = peer_tunnel_id;
1545 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1546 sizeof(ip_addr), 0);
1547 if (err < 0)
1548 goto out;
1549 }
James Chapman789a4a22010-04-02 06:19:40 +00001550 break;
1551
1552 default:
1553 goto out;
1554 }
1555
1556out:
Tom Parkin167eb172013-01-31 23:43:03 +00001557 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001558 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001559 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001560 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001561 *sockp = NULL;
1562 }
1563
1564 return err;
1565}
1566
Eric Dumazet37159ef2012-09-04 07:18:57 +00001567static struct lock_class_key l2tp_socket_class;
1568
James Chapmanfd558d12010-04-02 06:18:33 +00001569int 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)
1570{
1571 struct l2tp_tunnel *tunnel = NULL;
1572 int err;
1573 struct socket *sock = NULL;
1574 struct sock *sk = NULL;
1575 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001576 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001577
1578 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001579 * the userspace L2TP daemon. If not specified, create a
1580 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001581 */
James Chapman789a4a22010-04-02 06:19:40 +00001582 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001583 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1584 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001585 if (err < 0)
1586 goto err;
1587 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001588 sock = sockfd_lookup(fd, &err);
1589 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001590 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001591 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001592 err = -EBADF;
1593 goto err;
1594 }
1595
1596 /* Reject namespace mismatches */
1597 if (!net_eq(sock_net(sock->sk), net)) {
1598 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1599 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001600 goto err;
1601 }
James Chapmanfd558d12010-04-02 06:18:33 +00001602 }
1603
1604 sk = sock->sk;
1605
James Chapman0d767512010-04-02 06:19:00 +00001606 if (cfg != NULL)
1607 encap = cfg->encap;
1608
James Chapmanfd558d12010-04-02 06:18:33 +00001609 /* Quick sanity checks */
Eric Dumazet84fc2d72018-03-06 07:54:53 -08001610 err = -EPROTONOSUPPORT;
1611 if (sk->sk_type != SOCK_DGRAM) {
1612 pr_debug("tunl %hu: fd %d wrong socket type\n",
1613 tunnel_id, fd);
1614 goto err;
1615 }
James Chapman0d767512010-04-02 06:19:00 +00001616 switch (encap) {
1617 case L2TP_ENCAPTYPE_UDP:
James Chapman0d767512010-04-02 06:19:00 +00001618 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001619 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001620 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1621 goto err;
1622 }
1623 break;
1624 case L2TP_ENCAPTYPE_IP:
James Chapman0d767512010-04-02 06:19:00 +00001625 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001626 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001627 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1628 goto err;
1629 }
1630 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001631 }
1632
1633 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001634 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001635 if (tunnel != NULL) {
1636 /* This socket has already been prepped */
1637 err = -EBUSY;
1638 goto err;
1639 }
1640
James Chapmanfd558d12010-04-02 06:18:33 +00001641 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1642 if (tunnel == NULL) {
1643 err = -ENOMEM;
1644 goto err;
1645 }
1646
1647 tunnel->version = version;
1648 tunnel->tunnel_id = tunnel_id;
1649 tunnel->peer_tunnel_id = peer_tunnel_id;
1650 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1651
1652 tunnel->magic = L2TP_TUNNEL_MAGIC;
1653 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1654 rwlock_init(&tunnel->hlist_lock);
1655
1656 /* The net we belong to */
1657 tunnel->l2tp_net = net;
1658 pn = l2tp_pernet(net);
1659
James Chapman0d767512010-04-02 06:19:00 +00001660 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001661 tunnel->debug = cfg->debug;
1662
François Cachereule18503f2013-10-02 10:16:02 +02001663#if IS_ENABLED(CONFIG_IPV6)
1664 if (sk->sk_family == PF_INET6) {
1665 struct ipv6_pinfo *np = inet6_sk(sk);
1666
1667 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001668 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001669 struct inet_sock *inet = inet_sk(sk);
1670
1671 tunnel->v4mapped = true;
1672 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001673 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1674 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001675 } else {
1676 tunnel->v4mapped = false;
1677 }
1678 }
1679#endif
1680
James Chapmanfd558d12010-04-02 06:18:33 +00001681 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001682 tunnel->encap = encap;
1683 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001684 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001685
Andy Zhouc8fffce2014-09-16 17:31:19 -07001686 udp_cfg.sk_user_data = tunnel;
1687 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1688 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1689 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1690
1691 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1692 } else {
1693 sk->sk_user_data = tunnel;
1694 }
James Chapmanfd558d12010-04-02 06:18:33 +00001695
1696 /* Hook on the tunnel socket destructor so that we can cleanup
1697 * if the tunnel socket goes away.
1698 */
1699 tunnel->old_sk_destruct = sk->sk_destruct;
1700 sk->sk_destruct = &l2tp_tunnel_destruct;
1701 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001702 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001703 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1704
James Chapmanfd558d12010-04-02 06:18:33 +00001705 sk->sk_allocation = GFP_ATOMIC;
1706
Tom Parkinf8ccac02013-01-31 23:43:00 +00001707 /* Init delete workqueue struct */
1708 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1709
James Chapmanfd558d12010-04-02 06:18:33 +00001710 /* Add tunnel to our list */
1711 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001712 atomic_inc(&l2tp_tunnel_count);
1713
1714 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001715 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001716 */
1717 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001718 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1719 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1720 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001721
1722 err = 0;
1723err:
1724 if (tunnelp)
1725 *tunnelp = tunnel;
1726
James Chapman789a4a22010-04-02 06:19:40 +00001727 /* If tunnel's socket was created by the kernel, it doesn't
1728 * have a file.
1729 */
1730 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001731 sockfd_put(sock);
1732
1733 return err;
1734}
1735EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1736
James Chapman309795f2010-04-02 06:19:10 +00001737/* This function is used by the netlink TUNNEL_DELETE command.
1738 */
Sabrina Dubrocab4a9b122017-09-26 16:16:43 +02001739void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
James Chapman309795f2010-04-02 06:19:10 +00001740{
Sabrina Dubrocab4a9b122017-09-26 16:16:43 +02001741 if (!test_and_set_bit(0, &tunnel->dead)) {
1742 l2tp_tunnel_inc_refcount(tunnel);
1743 queue_work(l2tp_wq, &tunnel->del_work);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001744 }
James Chapman309795f2010-04-02 06:19:10 +00001745}
1746EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1747
James Chapmanfd558d12010-04-02 06:18:33 +00001748/* Really kill the session.
1749 */
1750void l2tp_session_free(struct l2tp_session *session)
1751{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001752 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001753
1754 BUG_ON(atomic_read(&session->ref_count) != 0);
1755
Tom Parkinf6e16b22013-03-19 06:11:23 +00001756 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001757 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001758 if (session->session_id != 0)
1759 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001760 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001761 session->tunnel = NULL;
1762 l2tp_tunnel_dec_refcount(tunnel);
1763 }
1764
1765 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001766}
1767EXPORT_SYMBOL_GPL(l2tp_session_free);
1768
Tom Parkinf6e16b22013-03-19 06:11:23 +00001769/* Remove an l2tp session from l2tp_core's hash lists.
1770 * Provides a tidyup interface for pseudowire code which can't just route all
1771 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1772 * callback.
1773 */
1774void __l2tp_session_unhash(struct l2tp_session *session)
1775{
1776 struct l2tp_tunnel *tunnel = session->tunnel;
1777
1778 /* Remove the session from core hashes */
1779 if (tunnel) {
1780 /* Remove from the per-tunnel hash */
1781 write_lock_bh(&tunnel->hlist_lock);
1782 hlist_del_init(&session->hlist);
1783 write_unlock_bh(&tunnel->hlist_lock);
1784
1785 /* For L2TPv3 we have a per-net hash: remove from there, too */
1786 if (tunnel->version != L2TP_HDR_VER_2) {
1787 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1788 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1789 hlist_del_init_rcu(&session->global_hlist);
1790 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1791 synchronize_rcu();
1792 }
1793 }
1794}
1795EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1796
James Chapman309795f2010-04-02 06:19:10 +00001797/* This function is used by the netlink SESSION_DELETE command and by
1798 pseudowire modules.
1799 */
1800int l2tp_session_delete(struct l2tp_session *session)
1801{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001802 if (session->ref)
1803 (*session->ref)(session);
1804 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001805 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001806 if (session->session_close != NULL)
1807 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001808 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001809 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001810 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001811 return 0;
1812}
1813EXPORT_SYMBOL_GPL(l2tp_session_delete);
1814
James Chapmanf7faffa2010-04-02 06:18:49 +00001815/* We come here whenever a session's send_seq, cookie_len or
1816 * l2specific_len parameters are set.
1817 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001818void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001819{
1820 if (version == L2TP_HDR_VER_2) {
1821 session->hdr_len = 6;
1822 if (session->send_seq)
1823 session->hdr_len += 4;
1824 } else {
James Chapman65b05d02018-01-03 22:48:06 +00001825 session->hdr_len = 4 + session->cookie_len + session->l2specific_len;
James Chapman0d767512010-04-02 06:19:00 +00001826 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1827 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001828 }
1829
1830}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001831EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001832
James Chapmanfd558d12010-04-02 06:18:33 +00001833struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1834{
1835 struct l2tp_session *session;
Guillaume Naultd9face62017-03-31 13:02:27 +02001836 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001837
1838 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1839 if (session != NULL) {
1840 session->magic = L2TP_SESSION_MAGIC;
1841 session->tunnel = tunnel;
1842
1843 session->session_id = session_id;
1844 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001845 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001846 if (tunnel->version == L2TP_HDR_VER_2)
1847 session->nr_max = 0xffff;
1848 else
1849 session->nr_max = 0xffffff;
1850 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001851 session->nr_oos_count_max = 4;
1852
1853 /* Use NR of first received packet */
1854 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001855
1856 sprintf(&session->name[0], "sess %u/%u",
1857 tunnel->tunnel_id, session->session_id);
1858
1859 skb_queue_head_init(&session->reorder_q);
1860
1861 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001862 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001863
1864 /* Inherit debug options from tunnel */
1865 session->debug = tunnel->debug;
1866
1867 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001868 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001869 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001870 session->mtu = cfg->mtu;
1871 session->mru = cfg->mru;
1872 session->send_seq = cfg->send_seq;
1873 session->recv_seq = cfg->recv_seq;
1874 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001875 session->reorder_timeout = cfg->reorder_timeout;
James Chapmanf7faffa2010-04-02 06:18:49 +00001876 session->l2specific_type = cfg->l2specific_type;
1877 session->l2specific_len = cfg->l2specific_len;
1878 session->cookie_len = cfg->cookie_len;
1879 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1880 session->peer_cookie_len = cfg->peer_cookie_len;
1881 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001882 }
1883
James Chapmanf7faffa2010-04-02 06:18:49 +00001884 if (tunnel->version == L2TP_HDR_VER_2)
1885 session->build_header = l2tp_build_l2tpv2_header;
1886 else
1887 session->build_header = l2tp_build_l2tpv3_header;
1888
1889 l2tp_session_set_header_len(session, tunnel->version);
1890
Guillaume Naultd9face62017-03-31 13:02:27 +02001891 err = l2tp_session_add_to_tunnel(tunnel, session);
1892 if (err) {
1893 kfree(session);
1894
1895 return ERR_PTR(err);
1896 }
1897
James Chapmanfd558d12010-04-02 06:18:33 +00001898 /* Bump the reference count. The session context is deleted
1899 * only when this drops to zero.
1900 */
1901 l2tp_session_inc_refcount(session);
1902 l2tp_tunnel_inc_refcount(tunnel);
1903
1904 /* Ensure tunnel socket isn't deleted */
1905 sock_hold(tunnel->sock);
1906
James Chapmanfd558d12010-04-02 06:18:33 +00001907 /* Ignore management session in session count value */
1908 if (session->session_id != 0)
1909 atomic_inc(&l2tp_session_count);
Guillaume Naultd9face62017-03-31 13:02:27 +02001910
1911 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001912 }
1913
Guillaume Naultd9face62017-03-31 13:02:27 +02001914 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001915}
1916EXPORT_SYMBOL_GPL(l2tp_session_create);
1917
1918/*****************************************************************************
1919 * Init and cleanup
1920 *****************************************************************************/
1921
1922static __net_init int l2tp_init_net(struct net *net)
1923{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001924 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001925 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001926
James Chapmanfd558d12010-04-02 06:18:33 +00001927 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001928 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001929
James Chapmanf7faffa2010-04-02 06:18:49 +00001930 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1931 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1932
James Chapmane02d4942010-04-02 06:19:16 +00001933 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001934
James Chapmanfd558d12010-04-02 06:18:33 +00001935 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001936}
1937
Tom Parkin167eb172013-01-31 23:43:03 +00001938static __net_exit void l2tp_exit_net(struct net *net)
1939{
1940 struct l2tp_net *pn = l2tp_pernet(net);
1941 struct l2tp_tunnel *tunnel = NULL;
1942
1943 rcu_read_lock_bh();
1944 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
Jiri Slabyfc4177e2017-10-25 15:57:55 +02001945 l2tp_tunnel_delete(tunnel);
Tom Parkin167eb172013-01-31 23:43:03 +00001946 }
1947 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001948
1949 flush_workqueue(l2tp_wq);
1950 rcu_barrier();
Tom Parkin167eb172013-01-31 23:43:03 +00001951}
1952
James Chapmanfd558d12010-04-02 06:18:33 +00001953static struct pernet_operations l2tp_net_ops = {
1954 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001955 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001956 .id = &l2tp_net_id,
1957 .size = sizeof(struct l2tp_net),
1958};
1959
1960static int __init l2tp_init(void)
1961{
1962 int rc = 0;
1963
1964 rc = register_pernet_device(&l2tp_net_ops);
1965 if (rc)
1966 goto out;
1967
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001968 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001969 if (!l2tp_wq) {
1970 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001971 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001972 rc = -ENOMEM;
1973 goto out;
1974 }
1975
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001976 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001977
1978out:
1979 return rc;
1980}
1981
1982static void __exit l2tp_exit(void)
1983{
1984 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001985 if (l2tp_wq) {
1986 destroy_workqueue(l2tp_wq);
1987 l2tp_wq = NULL;
1988 }
James Chapmanfd558d12010-04-02 06:18:33 +00001989}
1990
1991module_init(l2tp_init);
1992module_exit(l2tp_exit);
1993
1994MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1995MODULE_DESCRIPTION("L2TP core");
1996MODULE_LICENSE("GPL");
1997MODULE_VERSION(L2TP_DRV_VERSION);
1998