blob: f29911ab3b803552fa1d626679bfae0203d84308 [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 */
359struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
360{
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)) {
James Chapmane02d4942010-04-02 06:19:16 +0000369 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000370 return session;
371 }
372 }
373 }
374
James Chapmane02d4942010-04-02 06:19:16 +0000375 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000376
377 return NULL;
378}
379EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
380
Guillaume Naultd9face62017-03-31 13:02:27 +0200381static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel,
382 struct l2tp_session *session)
383{
384 struct l2tp_session *session_walk;
385 struct hlist_head *g_head;
386 struct hlist_head *head;
387 struct l2tp_net *pn;
388
389 head = l2tp_session_id_hash(tunnel, session->session_id);
390
391 write_lock_bh(&tunnel->hlist_lock);
392 hlist_for_each_entry(session_walk, head, hlist)
393 if (session_walk->session_id == session->session_id)
394 goto exist;
395
396 if (tunnel->version == L2TP_HDR_VER_3) {
397 pn = l2tp_pernet(tunnel->l2tp_net);
398 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
399 session->session_id);
400
401 spin_lock_bh(&pn->l2tp_session_hlist_lock);
402 hlist_for_each_entry(session_walk, g_head, global_hlist)
403 if (session_walk->session_id == session->session_id)
404 goto exist_glob;
405
406 hlist_add_head_rcu(&session->global_hlist, g_head);
407 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
408 }
409
410 hlist_add_head(&session->hlist, head);
411 write_unlock_bh(&tunnel->hlist_lock);
412
413 return 0;
414
415exist_glob:
416 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
417exist:
418 write_unlock_bh(&tunnel->hlist_lock);
419
420 return -EEXIST;
421}
422
James Chapmanfd558d12010-04-02 06:18:33 +0000423/* Lookup a tunnel by id
424 */
425struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
426{
427 struct l2tp_tunnel *tunnel;
428 struct l2tp_net *pn = l2tp_pernet(net);
429
James Chapmane02d4942010-04-02 06:19:16 +0000430 rcu_read_lock_bh();
431 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000432 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000433 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000434 return tunnel;
435 }
436 }
James Chapmane02d4942010-04-02 06:19:16 +0000437 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000438
439 return NULL;
440}
441EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
442
443struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
444{
445 struct l2tp_net *pn = l2tp_pernet(net);
446 struct l2tp_tunnel *tunnel;
447 int count = 0;
448
James Chapmane02d4942010-04-02 06:19:16 +0000449 rcu_read_lock_bh();
450 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000451 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000452 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000453 return tunnel;
454 }
455 }
456
James Chapmane02d4942010-04-02 06:19:16 +0000457 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000458
459 return NULL;
460}
461EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
462
463/*****************************************************************************
464 * Receive data handling
465 *****************************************************************************/
466
467/* Queue a skb in order. We come here only if the skb has an L2TP sequence
468 * number.
469 */
470static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
471{
472 struct sk_buff *skbp;
473 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000474 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000475
476 spin_lock_bh(&session->reorder_q.lock);
477 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
478 if (L2TP_SKB_CB(skbp)->ns > ns) {
479 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000480 l2tp_dbg(session, L2TP_MSG_SEQ,
481 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
482 session->name, ns, L2TP_SKB_CB(skbp)->ns,
483 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000484 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000485 goto out;
486 }
487 }
488
489 __skb_queue_tail(&session->reorder_q, skb);
490
491out:
492 spin_unlock_bh(&session->reorder_q.lock);
493}
494
495/* Dequeue a single skb.
496 */
497static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
498{
499 struct l2tp_tunnel *tunnel = session->tunnel;
500 int length = L2TP_SKB_CB(skb)->length;
501
502 /* We're about to requeue the skb, so return resources
503 * to its current owner (a socket receive buffer).
504 */
505 skb_orphan(skb);
506
Tom Parkin7b7c0712013-03-19 06:11:22 +0000507 atomic_long_inc(&tunnel->stats.rx_packets);
508 atomic_long_add(length, &tunnel->stats.rx_bytes);
509 atomic_long_inc(&session->stats.rx_packets);
510 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000511
512 if (L2TP_SKB_CB(skb)->has_seq) {
513 /* Bump our Nr */
514 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100515 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000516
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000517 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
518 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000519 }
520
521 /* call private receive handler */
522 if (session->recv_skb != NULL)
523 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
524 else
525 kfree_skb(skb);
526
527 if (session->deref)
528 (*session->deref)(session);
529}
530
531/* Dequeue skbs from the session's reorder_q, subject to packet order.
532 * Skbs that have been in the queue for too long are simply discarded.
533 */
534static void l2tp_recv_dequeue(struct l2tp_session *session)
535{
536 struct sk_buff *skb;
537 struct sk_buff *tmp;
538
539 /* If the pkt at the head of the queue has the nr that we
540 * expect to send up next, dequeue it and any other
541 * in-sequence packets behind it.
542 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000543start:
James Chapmanfd558d12010-04-02 06:18:33 +0000544 spin_lock_bh(&session->reorder_q.lock);
545 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
546 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000547 atomic_long_inc(&session->stats.rx_seq_discards);
548 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000549 l2tp_dbg(session, L2TP_MSG_SEQ,
550 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
551 session->name, L2TP_SKB_CB(skb)->ns,
552 L2TP_SKB_CB(skb)->length, session->nr,
553 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000554 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000555 __skb_unlink(skb, &session->reorder_q);
556 kfree_skb(skb);
557 if (session->deref)
558 (*session->deref)(session);
559 continue;
560 }
561
562 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000563 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000564 l2tp_dbg(session, L2TP_MSG_SEQ,
565 "%s: advancing nr to next pkt: %u -> %u",
566 session->name, session->nr,
567 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000568 session->reorder_skip = 0;
569 session->nr = L2TP_SKB_CB(skb)->ns;
570 }
James Chapmanfd558d12010-04-02 06:18:33 +0000571 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000572 l2tp_dbg(session, L2TP_MSG_SEQ,
573 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
574 session->name, L2TP_SKB_CB(skb)->ns,
575 L2TP_SKB_CB(skb)->length, session->nr,
576 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000577 goto out;
578 }
579 }
580 __skb_unlink(skb, &session->reorder_q);
581
582 /* Process the skb. We release the queue lock while we
583 * do so to let other contexts process the queue.
584 */
585 spin_unlock_bh(&session->reorder_q.lock);
586 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000587 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000588 }
589
590out:
591 spin_unlock_bh(&session->reorder_q.lock);
592}
593
James Chapman8a1631d2013-07-02 20:28:59 +0100594static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
595{
596 u32 nws;
597
598 if (nr >= session->nr)
599 nws = nr - session->nr;
600 else
601 nws = (session->nr_max + 1) - (session->nr - nr);
602
603 return nws < session->nr_window_size;
604}
605
James Chapmanb6dc01a2013-07-02 20:28:58 +0100606/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
607 * acceptable, else non-zero.
608 */
609static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
610{
James Chapman8a1631d2013-07-02 20:28:59 +0100611 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
612 /* Packet sequence number is outside allowed window.
613 * Discard it.
614 */
615 l2tp_dbg(session, L2TP_MSG_SEQ,
616 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
617 session->name, L2TP_SKB_CB(skb)->ns,
618 L2TP_SKB_CB(skb)->length, session->nr);
619 goto discard;
620 }
621
James Chapmanb6dc01a2013-07-02 20:28:58 +0100622 if (session->reorder_timeout != 0) {
623 /* Packet reordering enabled. Add skb to session's
624 * reorder queue, in order of ns.
625 */
626 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100627 goto out;
628 }
629
630 /* Packet reordering disabled. Discard out-of-sequence packets, while
631 * tracking the number if in-sequence packets after the first OOS packet
632 * is seen. After nr_oos_count_max in-sequence packets, reset the
633 * sequence number to re-enable packet reception.
634 */
635 if (L2TP_SKB_CB(skb)->ns == session->nr) {
636 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100637 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100638 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
639 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
640
641 if (nr_oos == nr_next)
642 session->nr_oos_count++;
643 else
644 session->nr_oos_count = 0;
645
646 session->nr_oos = nr_oos;
647 if (session->nr_oos_count > session->nr_oos_count_max) {
648 session->reorder_skip = 1;
649 l2tp_dbg(session, L2TP_MSG_SEQ,
650 "%s: %d oos packets received. Resetting sequence numbers\n",
651 session->name, session->nr_oos_count);
652 }
653 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100654 atomic_long_inc(&session->stats.rx_seq_discards);
655 l2tp_dbg(session, L2TP_MSG_SEQ,
656 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
657 session->name, L2TP_SKB_CB(skb)->ns,
658 L2TP_SKB_CB(skb)->length, session->nr,
659 skb_queue_len(&session->reorder_q));
660 goto discard;
661 }
662 skb_queue_tail(&session->reorder_q, skb);
663 }
664
James Chapmana0dbd822013-07-02 20:29:00 +0100665out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100666 return 0;
667
668discard:
669 return 1;
670}
671
James Chapmanf7faffa2010-04-02 06:18:49 +0000672/* Do receive processing of L2TP data frames. We handle both L2TPv2
673 * and L2TPv3 data frames here.
674 *
675 * L2TPv2 Data Message Header
676 *
677 * 0 1 2 3
678 * 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
679 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
681 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
682 * | Tunnel ID | Session ID |
683 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
684 * | Ns (opt) | Nr (opt) |
685 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
686 * | Offset Size (opt) | Offset pad... (opt)
687 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
688 *
689 * Data frames are marked by T=0. All other fields are the same as
690 * those in L2TP control frames.
691 *
692 * L2TPv3 Data Message Header
693 *
694 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * | L2TP Session Header |
696 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
697 * | L2-Specific Sublayer |
698 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
699 * | Tunnel Payload ...
700 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
701 *
702 * L2TPv3 Session Header Over IP
703 *
704 * 0 1 2 3
705 * 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
706 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
707 * | Session ID |
708 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
709 * | Cookie (optional, maximum 64 bits)...
710 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
711 * |
712 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
713 *
714 * L2TPv3 L2-Specific Sublayer Format
715 *
716 * 0 1 2 3
717 * 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
718 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
719 * |x|S|x|x|x|x|x|x| Sequence Number |
720 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
721 *
722 * Cookie value, sublayer format and offset (pad) are negotiated with
723 * the peer when the session is set up. Unlike L2TPv2, we do not need
724 * to parse the packet header to determine if optional fields are
725 * present.
726 *
727 * Caller must already have parsed the frame and determined that it is
728 * a data (not control) frame before coming here. Fields up to the
729 * session-id have already been parsed and ptr points to the data
730 * after the session-id.
Guillaume Nault6539c4f2017-03-31 13:02:25 +0200731 *
732 * session->ref() must have been called prior to l2tp_recv_common().
733 * session->deref() will be called automatically after skb is processed.
James Chapmanfd558d12010-04-02 06:18:33 +0000734 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000735void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
736 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
737 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000738{
James Chapmanf7faffa2010-04-02 06:18:49 +0000739 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000740 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000741 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000742
James Chapmanf7faffa2010-04-02 06:18:49 +0000743 /* Parse and check optional cookie */
744 if (session->peer_cookie_len > 0) {
745 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000746 l2tp_info(tunnel, L2TP_MSG_DATA,
747 "%s: cookie mismatch (%u/%u). Discarding.\n",
748 tunnel->name, tunnel->tunnel_id,
749 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000750 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000751 goto discard;
752 }
753 ptr += session->peer_cookie_len;
754 }
755
James Chapmanfd558d12010-04-02 06:18:33 +0000756 /* Handle the optional sequence numbers. Sequence numbers are
757 * in different places for L2TPv2 and L2TPv3.
758 *
759 * If we are the LAC, enable/disable sequence numbers under
760 * the control of the LNS. If no sequence numbers present but
761 * we were expecting them, discard frame.
762 */
763 ns = nr = 0;
764 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000765 if (tunnel->version == L2TP_HDR_VER_2) {
766 if (hdrflags & L2TP_HDRFLAG_S) {
767 ns = ntohs(*(__be16 *) ptr);
768 ptr += 2;
769 nr = ntohs(*(__be16 *) ptr);
770 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000771
James Chapmanf7faffa2010-04-02 06:18:49 +0000772 /* Store L2TP info in the skb */
773 L2TP_SKB_CB(skb)->ns = ns;
774 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000775
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000776 l2tp_dbg(session, L2TP_MSG_SEQ,
777 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
778 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000779 }
780 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
781 u32 l2h = ntohl(*(__be32 *) ptr);
782
783 if (l2h & 0x40000000) {
784 ns = l2h & 0x00ffffff;
785
786 /* Store L2TP info in the skb */
787 L2TP_SKB_CB(skb)->ns = ns;
788 L2TP_SKB_CB(skb)->has_seq = 1;
789
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000790 l2tp_dbg(session, L2TP_MSG_SEQ,
791 "%s: recv data ns=%u, session nr=%u\n",
792 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000793 }
James Chapmanfd558d12010-04-02 06:18:33 +0000794 }
795
James Chapmanf7faffa2010-04-02 06:18:49 +0000796 /* Advance past L2-specific header, if present */
797 ptr += session->l2specific_len;
798
James Chapmanfd558d12010-04-02 06:18:33 +0000799 if (L2TP_SKB_CB(skb)->has_seq) {
800 /* Received a packet with sequence numbers. If we're the LNS,
801 * check if we sre sending sequence numbers and if not,
802 * configure it so.
803 */
804 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000805 l2tp_info(session, L2TP_MSG_SEQ,
806 "%s: requested to enable seq numbers by LNS\n",
807 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000808 session->send_seq = -1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000809 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000810 }
811 } else {
812 /* No sequence numbers.
813 * If user has configured mandatory sequence numbers, discard.
814 */
815 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000816 l2tp_warn(session, L2TP_MSG_SEQ,
817 "%s: recv data has no seq numbers when required. Discarding.\n",
818 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000819 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000820 goto discard;
821 }
822
823 /* If we're the LAC and we're sending sequence numbers, the
824 * LNS has requested that we no longer send sequence numbers.
825 * If we're the LNS and we're sending sequence numbers, the
826 * LAC is broken. Discard the frame.
827 */
828 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000829 l2tp_info(session, L2TP_MSG_SEQ,
830 "%s: requested to disable seq numbers by LNS\n",
831 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000832 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000833 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000834 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000835 l2tp_warn(session, L2TP_MSG_SEQ,
836 "%s: recv data has no seq numbers when required. Discarding.\n",
837 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000838 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000839 goto discard;
840 }
841 }
842
James Chapmanf7faffa2010-04-02 06:18:49 +0000843 /* Session data offset is handled differently for L2TPv2 and
844 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
845 * the header. For L2TPv3, the offset is negotiated using AVPs
846 * in the session setup control protocol.
847 */
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 }
854 } else
855 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000856
857 offset = ptr - optr;
858 if (!pskb_may_pull(skb, offset))
859 goto discard;
860
861 __skb_pull(skb, offset);
862
863 /* If caller wants to process the payload before we queue the
864 * packet, do so now.
865 */
866 if (payload_hook)
867 if ((*payload_hook)(skb))
868 goto discard;
869
870 /* Prepare skb for adding to the session's reorder_q. Hold
871 * packets for max reorder_timeout or 1 second if not
872 * reordering.
873 */
874 L2TP_SKB_CB(skb)->length = length;
875 L2TP_SKB_CB(skb)->expires = jiffies +
876 (session->reorder_timeout ? session->reorder_timeout : HZ);
877
878 /* Add packet to the session's receive queue. Reordering is done here, if
879 * enabled. Saved L2TP protocol info is stored in skb->sb[].
880 */
881 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100882 if (l2tp_recv_data_seq(session, skb))
883 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000884 } else {
885 /* No sequence numbers. Add the skb to the tail of the
886 * reorder queue. This ensures that it will be
887 * delivered after all previous sequenced skbs.
888 */
889 skb_queue_tail(&session->reorder_q, skb);
890 }
891
892 /* Try to dequeue as many skbs from reorder_q as we can. */
893 l2tp_recv_dequeue(session);
894
James Chapmanf7faffa2010-04-02 06:18:49 +0000895 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000896
897discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000898 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000899 kfree_skb(skb);
900
901 if (session->deref)
902 (*session->deref)(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000903}
904EXPORT_SYMBOL(l2tp_recv_common);
905
Tom Parkin48f72f92013-03-19 06:11:19 +0000906/* Drop skbs from the session's reorder_q
907 */
908int l2tp_session_queue_purge(struct l2tp_session *session)
909{
910 struct sk_buff *skb = NULL;
911 BUG_ON(!session);
912 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
913 while ((skb = skb_dequeue(&session->reorder_q))) {
914 atomic_long_inc(&session->stats.rx_errors);
915 kfree_skb(skb);
916 if (session->deref)
917 (*session->deref)(session);
918 }
919 return 0;
920}
921EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
922
James Chapmanf7faffa2010-04-02 06:18:49 +0000923/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
924 * here. The skb is not on a list when we get here.
925 * Returns 0 if the packet was a data packet and was successfully passed on.
926 * Returns 1 if the packet was not a good data packet and could not be
927 * forwarded. All such packets are passed up to userspace to deal with.
928 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000929static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
930 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000931{
932 struct l2tp_session *session = NULL;
933 unsigned char *ptr, *optr;
934 u16 hdrflags;
935 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000936 u16 version;
937 int length;
938
Tom Herbert58d60852014-05-07 16:52:48 -0700939 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000940
941 /* UDP always verifies the packet length. */
942 __skb_pull(skb, sizeof(struct udphdr));
943
944 /* Short packet? */
945 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000946 l2tp_info(tunnel, L2TP_MSG_DATA,
947 "%s: recv short packet (len=%d)\n",
948 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000949 goto error;
950 }
951
James Chapmanf7faffa2010-04-02 06:18:49 +0000952 /* Trace packet contents, if enabled */
953 if (tunnel->debug & L2TP_MSG_DATA) {
954 length = min(32u, skb->len);
955 if (!pskb_may_pull(skb, length))
956 goto error;
957
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000958 pr_debug("%s: recv\n", tunnel->name);
959 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000960 }
961
Eric Dumazete50e7052011-11-08 13:59:44 -0500962 /* Point to L2TP header */
963 optr = ptr = skb->data;
964
James Chapmanf7faffa2010-04-02 06:18:49 +0000965 /* Get L2TP header flags */
966 hdrflags = ntohs(*(__be16 *) ptr);
967
968 /* Check protocol version */
969 version = hdrflags & L2TP_HDR_VER_MASK;
970 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000971 l2tp_info(tunnel, L2TP_MSG_DATA,
972 "%s: recv protocol version mismatch: got %d expected %d\n",
973 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000974 goto error;
975 }
976
977 /* Get length of L2TP packet */
978 length = skb->len;
979
980 /* If type is control packet, it is handled by userspace. */
981 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000982 l2tp_dbg(tunnel, L2TP_MSG_DATA,
983 "%s: recv control packet, len=%d\n",
984 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000985 goto error;
986 }
987
988 /* Skip flags */
989 ptr += 2;
990
991 if (tunnel->version == L2TP_HDR_VER_2) {
992 /* If length is present, skip it */
993 if (hdrflags & L2TP_HDRFLAG_L)
994 ptr += 2;
995
996 /* Extract tunnel and session ID */
997 tunnel_id = ntohs(*(__be16 *) ptr);
998 ptr += 2;
999 session_id = ntohs(*(__be16 *) ptr);
1000 ptr += 2;
1001 } else {
1002 ptr += 2; /* skip reserved bits */
1003 tunnel_id = tunnel->tunnel_id;
1004 session_id = ntohl(*(__be32 *) ptr);
1005 ptr += 4;
1006 }
1007
1008 /* Find the session context */
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001009 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
James Chapman309795f2010-04-02 06:19:10 +00001010 if (!session || !session->recv_skb) {
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001011 if (session) {
1012 if (session->deref)
1013 session->deref(session);
1014 l2tp_session_dec_refcount(session);
1015 }
1016
James Chapmanf7faffa2010-04-02 06:18:49 +00001017 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001018 l2tp_info(tunnel, L2TP_MSG_DATA,
1019 "%s: no session found (%u/%u). Passing up.\n",
1020 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001021 goto error;
1022 }
1023
1024 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault6539c4f2017-03-31 13:02:25 +02001025 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001026
1027 return 0;
1028
James Chapmanfd558d12010-04-02 06:18:33 +00001029error:
1030 /* Put UDP header back */
1031 __skb_push(skb, sizeof(struct udphdr));
1032
1033 return 1;
1034}
James Chapmanfd558d12010-04-02 06:18:33 +00001035
1036/* UDP encapsulation receive handler. See net/ipv4/udp.c.
1037 * Return codes:
1038 * 0 : success.
1039 * <0: error
1040 * >0: skb should be passed up to userspace as UDP.
1041 */
1042int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1043{
1044 struct l2tp_tunnel *tunnel;
1045
1046 tunnel = l2tp_sock_to_tunnel(sk);
1047 if (tunnel == NULL)
1048 goto pass_up;
1049
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001050 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1051 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001052
1053 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1054 goto pass_up_put;
1055
1056 sock_put(sk);
1057 return 0;
1058
1059pass_up_put:
1060 sock_put(sk);
1061pass_up:
1062 return 1;
1063}
1064EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1065
1066/************************************************************************
1067 * Transmit handling
1068 ***********************************************************************/
1069
1070/* Build an L2TP header for the session into the buffer provided.
1071 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001072static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001073{
James Chapmanf7faffa2010-04-02 06:18:49 +00001074 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001075 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001076 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001077 u16 flags = L2TP_HDR_VER_2;
1078 u32 tunnel_id = tunnel->peer_tunnel_id;
1079 u32 session_id = session->peer_session_id;
1080
1081 if (session->send_seq)
1082 flags |= L2TP_HDRFLAG_S;
1083
1084 /* Setup L2TP header. */
1085 *bufp++ = htons(flags);
1086 *bufp++ = htons(tunnel_id);
1087 *bufp++ = htons(session_id);
1088 if (session->send_seq) {
1089 *bufp++ = htons(session->ns);
1090 *bufp++ = 0;
1091 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001092 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001093 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1094 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001095 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001096
1097 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001098}
1099
James Chapmanf7faffa2010-04-02 06:18:49 +00001100static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001101{
James Chapman0d767512010-04-02 06:19:00 +00001102 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001103 char *bufp = buf;
1104 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001105
James Chapman0d767512010-04-02 06:19:00 +00001106 /* Setup L2TP header. The header differs slightly for UDP and
1107 * IP encapsulations. For UDP, there is 4 bytes of flags.
1108 */
1109 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1110 u16 flags = L2TP_HDR_VER_3;
1111 *((__be16 *) bufp) = htons(flags);
1112 bufp += 2;
1113 *((__be16 *) bufp) = 0;
1114 bufp += 2;
1115 }
1116
James Chapmanf7faffa2010-04-02 06:18:49 +00001117 *((__be32 *) bufp) = htonl(session->peer_session_id);
1118 bufp += 4;
1119 if (session->cookie_len) {
1120 memcpy(bufp, &session->cookie[0], session->cookie_len);
1121 bufp += session->cookie_len;
1122 }
1123 if (session->l2specific_len) {
1124 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1125 u32 l2h = 0;
1126 if (session->send_seq) {
1127 l2h = 0x40000000 | session->ns;
1128 session->ns++;
1129 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001130 l2tp_dbg(session, L2TP_MSG_SEQ,
1131 "%s: updated ns to %u\n",
1132 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001133 }
1134
1135 *((__be32 *) bufp) = htonl(l2h);
1136 }
1137 bufp += session->l2specific_len;
1138 }
1139 if (session->offset)
1140 bufp += session->offset;
1141
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);
Florian Westphal71b13912011-11-25 06:47:16 +00001237 skb_dst_set(skb, dst_clone(__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);
1413 sk = l2tp_tunnel_sock_lookup(tunnel);
1414 if (!sk)
Alexander Couzens06a15f52015-09-28 11:32:42 +02001415 goto out;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001416
1417 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001418
Tom Parkin02d13ed2013-03-19 06:11:18 +00001419 /* If the tunnel socket was created by userspace, then go through the
1420 * inet layer to shut the socket down, and let userspace close it.
1421 * Otherwise, if we created the socket directly within the kernel, use
1422 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001423 * In either case the tunnel resources are freed in the socket
1424 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001425 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001426 if (tunnel->fd >= 0) {
1427 if (sock)
1428 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001429 } else {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001430 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001431 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001432 sock_release(sock);
1433 }
Tom Parkin167eb172013-01-31 23:43:03 +00001434 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001435
1436 l2tp_tunnel_sock_put(sk);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001437out:
1438 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001439}
James Chapmanfd558d12010-04-02 06:18:33 +00001440
James Chapman789a4a22010-04-02 06:19:40 +00001441/* Create a socket for the tunnel, if one isn't set up by
1442 * userspace. This is used for static tunnels where there is no
1443 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001444 *
1445 * Since we don't want these sockets to keep a namespace alive by
1446 * themselves, we drop the socket's namespace refcount after creation.
1447 * These sockets are freed when the namespace exits using the pernet
1448 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001449 */
Tom Parkin167eb172013-01-31 23:43:03 +00001450static int l2tp_tunnel_sock_create(struct net *net,
1451 u32 tunnel_id,
1452 u32 peer_tunnel_id,
1453 struct l2tp_tunnel_cfg *cfg,
1454 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001455{
1456 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001457 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001458 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001459
1460 switch (cfg->encap) {
1461 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001462 memset(&udp_conf, 0, sizeof(udp_conf));
1463
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001464#if IS_ENABLED(CONFIG_IPV6)
1465 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001466 udp_conf.family = AF_INET6;
1467 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1468 sizeof(udp_conf.local_ip6));
1469 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1470 sizeof(udp_conf.peer_ip6));
1471 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001472 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001473 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001474 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001475 } else
1476#endif
1477 {
Tom Herbert85644b42014-07-13 19:49:48 -07001478 udp_conf.family = AF_INET;
1479 udp_conf.local_ip = cfg->local_ip;
1480 udp_conf.peer_ip = cfg->peer_ip;
1481 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001482 }
James Chapman789a4a22010-04-02 06:19:40 +00001483
Tom Herbert85644b42014-07-13 19:49:48 -07001484 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1485 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1486
1487 err = udp_sock_create(net, &udp_conf, &sock);
1488 if (err < 0)
1489 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001490
1491 break;
1492
1493 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001494#if IS_ENABLED(CONFIG_IPV6)
1495 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001496 struct sockaddr_l2tpip6 ip6_addr = {0};
1497
Eric W. Biederman26abe142015-05-08 21:10:31 -05001498 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001499 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001500 if (err < 0)
1501 goto out;
1502
James Chapman5dac94e2012-04-29 21:48:55 +00001503 ip6_addr.l2tp_family = AF_INET6;
1504 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1505 sizeof(ip6_addr.l2tp_addr));
1506 ip6_addr.l2tp_conn_id = tunnel_id;
1507 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1508 sizeof(ip6_addr));
1509 if (err < 0)
1510 goto out;
1511
1512 ip6_addr.l2tp_family = AF_INET6;
1513 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1514 sizeof(ip6_addr.l2tp_addr));
1515 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1516 err = kernel_connect(sock,
1517 (struct sockaddr *) &ip6_addr,
1518 sizeof(ip6_addr), 0);
1519 if (err < 0)
1520 goto out;
1521 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001522#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001523 {
Tom Herbert85644b42014-07-13 19:49:48 -07001524 struct sockaddr_l2tpip ip_addr = {0};
1525
Eric W. Biederman26abe142015-05-08 21:10:31 -05001526 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001527 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001528 if (err < 0)
1529 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001530
James Chapman5dac94e2012-04-29 21:48:55 +00001531 ip_addr.l2tp_family = AF_INET;
1532 ip_addr.l2tp_addr = cfg->local_ip;
1533 ip_addr.l2tp_conn_id = tunnel_id;
1534 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1535 sizeof(ip_addr));
1536 if (err < 0)
1537 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001538
James Chapman5dac94e2012-04-29 21:48:55 +00001539 ip_addr.l2tp_family = AF_INET;
1540 ip_addr.l2tp_addr = cfg->peer_ip;
1541 ip_addr.l2tp_conn_id = peer_tunnel_id;
1542 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1543 sizeof(ip_addr), 0);
1544 if (err < 0)
1545 goto out;
1546 }
James Chapman789a4a22010-04-02 06:19:40 +00001547 break;
1548
1549 default:
1550 goto out;
1551 }
1552
1553out:
Tom Parkin167eb172013-01-31 23:43:03 +00001554 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001555 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001556 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001557 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001558 *sockp = NULL;
1559 }
1560
1561 return err;
1562}
1563
Eric Dumazet37159ef2012-09-04 07:18:57 +00001564static struct lock_class_key l2tp_socket_class;
1565
James Chapmanfd558d12010-04-02 06:18:33 +00001566int 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)
1567{
1568 struct l2tp_tunnel *tunnel = NULL;
1569 int err;
1570 struct socket *sock = NULL;
1571 struct sock *sk = NULL;
1572 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001573 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001574
1575 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001576 * the userspace L2TP daemon. If not specified, create a
1577 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001578 */
James Chapman789a4a22010-04-02 06:19:40 +00001579 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001580 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1581 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001582 if (err < 0)
1583 goto err;
1584 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001585 sock = sockfd_lookup(fd, &err);
1586 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001587 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001588 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001589 err = -EBADF;
1590 goto err;
1591 }
1592
1593 /* Reject namespace mismatches */
1594 if (!net_eq(sock_net(sock->sk), net)) {
1595 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1596 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001597 goto err;
1598 }
James Chapmanfd558d12010-04-02 06:18:33 +00001599 }
1600
1601 sk = sock->sk;
1602
James Chapman0d767512010-04-02 06:19:00 +00001603 if (cfg != NULL)
1604 encap = cfg->encap;
1605
James Chapmanfd558d12010-04-02 06:18:33 +00001606 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001607 switch (encap) {
1608 case L2TP_ENCAPTYPE_UDP:
1609 err = -EPROTONOSUPPORT;
1610 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001611 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001612 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1613 goto err;
1614 }
1615 break;
1616 case L2TP_ENCAPTYPE_IP:
1617 err = -EPROTONOSUPPORT;
1618 if (sk->sk_protocol != IPPROTO_L2TP) {
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_L2TP);
1621 goto err;
1622 }
1623 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001624 }
1625
1626 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001627 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001628 if (tunnel != NULL) {
1629 /* This socket has already been prepped */
1630 err = -EBUSY;
1631 goto err;
1632 }
1633
James Chapmanfd558d12010-04-02 06:18:33 +00001634 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1635 if (tunnel == NULL) {
1636 err = -ENOMEM;
1637 goto err;
1638 }
1639
1640 tunnel->version = version;
1641 tunnel->tunnel_id = tunnel_id;
1642 tunnel->peer_tunnel_id = peer_tunnel_id;
1643 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1644
1645 tunnel->magic = L2TP_TUNNEL_MAGIC;
1646 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1647 rwlock_init(&tunnel->hlist_lock);
1648
1649 /* The net we belong to */
1650 tunnel->l2tp_net = net;
1651 pn = l2tp_pernet(net);
1652
James Chapman0d767512010-04-02 06:19:00 +00001653 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001654 tunnel->debug = cfg->debug;
1655
François Cachereule18503f2013-10-02 10:16:02 +02001656#if IS_ENABLED(CONFIG_IPV6)
1657 if (sk->sk_family == PF_INET6) {
1658 struct ipv6_pinfo *np = inet6_sk(sk);
1659
1660 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001661 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001662 struct inet_sock *inet = inet_sk(sk);
1663
1664 tunnel->v4mapped = true;
1665 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001666 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1667 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001668 } else {
1669 tunnel->v4mapped = false;
1670 }
1671 }
1672#endif
1673
James Chapmanfd558d12010-04-02 06:18:33 +00001674 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001675 tunnel->encap = encap;
1676 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001677 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001678
Andy Zhouc8fffce2014-09-16 17:31:19 -07001679 udp_cfg.sk_user_data = tunnel;
1680 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1681 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1682 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1683
1684 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1685 } else {
1686 sk->sk_user_data = tunnel;
1687 }
James Chapmanfd558d12010-04-02 06:18:33 +00001688
1689 /* Hook on the tunnel socket destructor so that we can cleanup
1690 * if the tunnel socket goes away.
1691 */
1692 tunnel->old_sk_destruct = sk->sk_destruct;
1693 sk->sk_destruct = &l2tp_tunnel_destruct;
1694 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001695 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001696 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1697
James Chapmanfd558d12010-04-02 06:18:33 +00001698 sk->sk_allocation = GFP_ATOMIC;
1699
Tom Parkinf8ccac02013-01-31 23:43:00 +00001700 /* Init delete workqueue struct */
1701 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1702
James Chapmanfd558d12010-04-02 06:18:33 +00001703 /* Add tunnel to our list */
1704 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001705 atomic_inc(&l2tp_tunnel_count);
1706
1707 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001708 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001709 */
1710 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001711 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1712 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1713 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001714
1715 err = 0;
1716err:
1717 if (tunnelp)
1718 *tunnelp = tunnel;
1719
James Chapman789a4a22010-04-02 06:19:40 +00001720 /* If tunnel's socket was created by the kernel, it doesn't
1721 * have a file.
1722 */
1723 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001724 sockfd_put(sock);
1725
1726 return err;
1727}
1728EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1729
James Chapman309795f2010-04-02 06:19:10 +00001730/* This function is used by the netlink TUNNEL_DELETE command.
1731 */
1732int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1733{
Alexander Couzens06a15f52015-09-28 11:32:42 +02001734 l2tp_tunnel_inc_refcount(tunnel);
Tom Parkin2b551c62013-03-19 06:11:16 +00001735 l2tp_tunnel_closeall(tunnel);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001736 if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
1737 l2tp_tunnel_dec_refcount(tunnel);
1738 return 1;
1739 }
1740 return 0;
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
1812 * l2specific_len parameters are set.
1813 */
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 {
James Chapman0d767512010-04-02 06:19:00 +00001821 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1822 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1823 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001824 }
1825
1826}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001827EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001828
James Chapmanfd558d12010-04-02 06:18:33 +00001829struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1830{
1831 struct l2tp_session *session;
Guillaume Naultd9face62017-03-31 13:02:27 +02001832 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001833
1834 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1835 if (session != NULL) {
1836 session->magic = L2TP_SESSION_MAGIC;
1837 session->tunnel = tunnel;
1838
1839 session->session_id = session_id;
1840 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001841 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001842 if (tunnel->version == L2TP_HDR_VER_2)
1843 session->nr_max = 0xffff;
1844 else
1845 session->nr_max = 0xffffff;
1846 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001847 session->nr_oos_count_max = 4;
1848
1849 /* Use NR of first received packet */
1850 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001851
1852 sprintf(&session->name[0], "sess %u/%u",
1853 tunnel->tunnel_id, session->session_id);
1854
1855 skb_queue_head_init(&session->reorder_q);
1856
1857 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001858 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001859
1860 /* Inherit debug options from tunnel */
1861 session->debug = tunnel->debug;
1862
1863 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001864 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001865 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001866 session->mtu = cfg->mtu;
1867 session->mru = cfg->mru;
1868 session->send_seq = cfg->send_seq;
1869 session->recv_seq = cfg->recv_seq;
1870 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001871 session->reorder_timeout = cfg->reorder_timeout;
1872 session->offset = cfg->offset;
1873 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) {
1942 (void)l2tp_tunnel_delete(tunnel);
1943 }
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