blob: 154974be1eed1f12ecbd4ce5d089dd596101509d [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 Chapmanfd558d12010-04-02 06:18:33 +0000220/* Session hash list.
221 * The session_id SHOULD be random according to RFC2661, but several
222 * L2TP implementations (Cisco and Microsoft) use incrementing
223 * session_ids. So we do a real hash on the session_id, rather than a
224 * simple bitmask.
225 */
226static inline struct hlist_head *
227l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
228{
229 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
230}
231
Guillaume Nault55a3ce32017-04-11 13:12:21 +0200232/* Lookup a session. A new reference is held on the returned session.
Guillaume Nault61b9a042017-03-31 13:02:25 +0200233 * Optionally calls session->ref() too if do_ref is true.
234 */
235struct l2tp_session *l2tp_session_get(struct net *net,
236 struct l2tp_tunnel *tunnel,
237 u32 session_id, bool do_ref)
238{
239 struct hlist_head *session_list;
240 struct l2tp_session *session;
241
242 if (!tunnel) {
243 struct l2tp_net *pn = l2tp_pernet(net);
244
245 session_list = l2tp_session_id_hash_2(pn, session_id);
246
247 rcu_read_lock_bh();
248 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
249 if (session->session_id == session_id) {
250 l2tp_session_inc_refcount(session);
251 if (do_ref && session->ref)
252 session->ref(session);
253 rcu_read_unlock_bh();
254
255 return session;
256 }
257 }
258 rcu_read_unlock_bh();
259
260 return NULL;
261 }
262
263 session_list = l2tp_session_id_hash(tunnel, session_id);
264 read_lock_bh(&tunnel->hlist_lock);
265 hlist_for_each_entry(session, session_list, hlist) {
266 if (session->session_id == session_id) {
267 l2tp_session_inc_refcount(session);
268 if (do_ref && session->ref)
269 session->ref(session);
270 read_unlock_bh(&tunnel->hlist_lock);
271
272 return session;
273 }
274 }
275 read_unlock_bh(&tunnel->hlist_lock);
276
277 return NULL;
278}
279EXPORT_SYMBOL_GPL(l2tp_session_get);
280
Guillaume Naulte08293a2017-04-03 12:03:13 +0200281struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
282 bool do_ref)
James Chapmanfd558d12010-04-02 06:18:33 +0000283{
284 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +0000285 struct l2tp_session *session;
286 int count = 0;
287
288 read_lock_bh(&tunnel->hlist_lock);
289 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800290 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
James Chapmanfd558d12010-04-02 06:18:33 +0000291 if (++count > nth) {
Guillaume Naulte08293a2017-04-03 12:03:13 +0200292 l2tp_session_inc_refcount(session);
293 if (do_ref && session->ref)
294 session->ref(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000295 read_unlock_bh(&tunnel->hlist_lock);
296 return session;
297 }
298 }
299 }
300
301 read_unlock_bh(&tunnel->hlist_lock);
302
303 return NULL;
304}
Guillaume Naulte08293a2017-04-03 12:03:13 +0200305EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
James Chapmanfd558d12010-04-02 06:18:33 +0000306
James Chapman309795f2010-04-02 06:19:10 +0000307/* Lookup a session by interface name.
308 * This is very inefficient but is only used by management interfaces.
309 */
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200310struct l2tp_session *l2tp_session_get_by_ifname(struct net *net, char *ifname,
311 bool do_ref)
James Chapman309795f2010-04-02 06:19:10 +0000312{
313 struct l2tp_net *pn = l2tp_pernet(net);
314 int hash;
James Chapman309795f2010-04-02 06:19:10 +0000315 struct l2tp_session *session;
316
James Chapmane02d4942010-04-02 06:19:16 +0000317 rcu_read_lock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000318 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800319 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
James Chapman309795f2010-04-02 06:19:10 +0000320 if (!strcmp(session->ifname, ifname)) {
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200321 l2tp_session_inc_refcount(session);
322 if (do_ref && session->ref)
323 session->ref(session);
James Chapmane02d4942010-04-02 06:19:16 +0000324 rcu_read_unlock_bh();
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200325
James Chapman309795f2010-04-02 06:19:10 +0000326 return session;
327 }
328 }
329 }
330
James Chapmane02d4942010-04-02 06:19:16 +0000331 rcu_read_unlock_bh();
James Chapman309795f2010-04-02 06:19:10 +0000332
333 return NULL;
334}
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200335EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
James Chapman309795f2010-04-02 06:19:10 +0000336
Guillaume Naultdbdbc732017-03-31 13:02:27 +0200337static int l2tp_session_add_to_tunnel(struct l2tp_tunnel *tunnel,
338 struct l2tp_session *session)
339{
340 struct l2tp_session *session_walk;
341 struct hlist_head *g_head;
342 struct hlist_head *head;
343 struct l2tp_net *pn;
344
345 head = l2tp_session_id_hash(tunnel, session->session_id);
346
347 write_lock_bh(&tunnel->hlist_lock);
348 hlist_for_each_entry(session_walk, head, hlist)
349 if (session_walk->session_id == session->session_id)
350 goto exist;
351
352 if (tunnel->version == L2TP_HDR_VER_3) {
353 pn = l2tp_pernet(tunnel->l2tp_net);
354 g_head = l2tp_session_id_hash_2(l2tp_pernet(tunnel->l2tp_net),
355 session->session_id);
356
357 spin_lock_bh(&pn->l2tp_session_hlist_lock);
358 hlist_for_each_entry(session_walk, g_head, global_hlist)
359 if (session_walk->session_id == session->session_id)
360 goto exist_glob;
361
362 hlist_add_head_rcu(&session->global_hlist, g_head);
363 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
364 }
365
366 hlist_add_head(&session->hlist, head);
367 write_unlock_bh(&tunnel->hlist_lock);
368
369 return 0;
370
371exist_glob:
372 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
373exist:
374 write_unlock_bh(&tunnel->hlist_lock);
375
376 return -EEXIST;
377}
378
James Chapmanfd558d12010-04-02 06:18:33 +0000379/* Lookup a tunnel by id
380 */
381struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
382{
383 struct l2tp_tunnel *tunnel;
384 struct l2tp_net *pn = l2tp_pernet(net);
385
James Chapmane02d4942010-04-02 06:19:16 +0000386 rcu_read_lock_bh();
387 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000388 if (tunnel->tunnel_id == tunnel_id) {
James Chapmane02d4942010-04-02 06:19:16 +0000389 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000390 return tunnel;
391 }
392 }
James Chapmane02d4942010-04-02 06:19:16 +0000393 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000394
395 return NULL;
396}
397EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
398
399struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
400{
401 struct l2tp_net *pn = l2tp_pernet(net);
402 struct l2tp_tunnel *tunnel;
403 int count = 0;
404
James Chapmane02d4942010-04-02 06:19:16 +0000405 rcu_read_lock_bh();
406 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
James Chapmanfd558d12010-04-02 06:18:33 +0000407 if (++count > nth) {
James Chapmane02d4942010-04-02 06:19:16 +0000408 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000409 return tunnel;
410 }
411 }
412
James Chapmane02d4942010-04-02 06:19:16 +0000413 rcu_read_unlock_bh();
James Chapmanfd558d12010-04-02 06:18:33 +0000414
415 return NULL;
416}
417EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
418
419/*****************************************************************************
420 * Receive data handling
421 *****************************************************************************/
422
423/* Queue a skb in order. We come here only if the skb has an L2TP sequence
424 * number.
425 */
426static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
427{
428 struct sk_buff *skbp;
429 struct sk_buff *tmp;
James Chapmanf7faffa2010-04-02 06:18:49 +0000430 u32 ns = L2TP_SKB_CB(skb)->ns;
James Chapmanfd558d12010-04-02 06:18:33 +0000431
432 spin_lock_bh(&session->reorder_q.lock);
433 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
434 if (L2TP_SKB_CB(skbp)->ns > ns) {
435 __skb_queue_before(&session->reorder_q, skbp, skb);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000436 l2tp_dbg(session, L2TP_MSG_SEQ,
437 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
438 session->name, ns, L2TP_SKB_CB(skbp)->ns,
439 skb_queue_len(&session->reorder_q));
Tom Parkin7b7c0712013-03-19 06:11:22 +0000440 atomic_long_inc(&session->stats.rx_oos_packets);
James Chapmanfd558d12010-04-02 06:18:33 +0000441 goto out;
442 }
443 }
444
445 __skb_queue_tail(&session->reorder_q, skb);
446
447out:
448 spin_unlock_bh(&session->reorder_q.lock);
449}
450
451/* Dequeue a single skb.
452 */
453static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
454{
455 struct l2tp_tunnel *tunnel = session->tunnel;
456 int length = L2TP_SKB_CB(skb)->length;
457
458 /* We're about to requeue the skb, so return resources
459 * to its current owner (a socket receive buffer).
460 */
461 skb_orphan(skb);
462
Tom Parkin7b7c0712013-03-19 06:11:22 +0000463 atomic_long_inc(&tunnel->stats.rx_packets);
464 atomic_long_add(length, &tunnel->stats.rx_bytes);
465 atomic_long_inc(&session->stats.rx_packets);
466 atomic_long_add(length, &session->stats.rx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +0000467
468 if (L2TP_SKB_CB(skb)->has_seq) {
469 /* Bump our Nr */
470 session->nr++;
James Chapman8a1631d2013-07-02 20:28:59 +0100471 session->nr &= session->nr_max;
James Chapmanf7faffa2010-04-02 06:18:49 +0000472
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000473 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
474 session->name, session->nr);
James Chapmanfd558d12010-04-02 06:18:33 +0000475 }
476
477 /* call private receive handler */
478 if (session->recv_skb != NULL)
479 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
480 else
481 kfree_skb(skb);
482
483 if (session->deref)
484 (*session->deref)(session);
485}
486
487/* Dequeue skbs from the session's reorder_q, subject to packet order.
488 * Skbs that have been in the queue for too long are simply discarded.
489 */
490static void l2tp_recv_dequeue(struct l2tp_session *session)
491{
492 struct sk_buff *skb;
493 struct sk_buff *tmp;
494
495 /* If the pkt at the head of the queue has the nr that we
496 * expect to send up next, dequeue it and any other
497 * in-sequence packets behind it.
498 */
Eric Dumazete2e210c2011-11-02 22:47:44 +0000499start:
James Chapmanfd558d12010-04-02 06:18:33 +0000500 spin_lock_bh(&session->reorder_q.lock);
501 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
502 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
Tom Parkin7b7c0712013-03-19 06:11:22 +0000503 atomic_long_inc(&session->stats.rx_seq_discards);
504 atomic_long_inc(&session->stats.rx_errors);
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000505 l2tp_dbg(session, L2TP_MSG_SEQ,
506 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
507 session->name, L2TP_SKB_CB(skb)->ns,
508 L2TP_SKB_CB(skb)->length, session->nr,
509 skb_queue_len(&session->reorder_q));
James Chapman38d40b32012-05-09 23:43:08 +0000510 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000511 __skb_unlink(skb, &session->reorder_q);
512 kfree_skb(skb);
513 if (session->deref)
514 (*session->deref)(session);
515 continue;
516 }
517
518 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapman38d40b32012-05-09 23:43:08 +0000519 if (session->reorder_skip) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000520 l2tp_dbg(session, L2TP_MSG_SEQ,
521 "%s: advancing nr to next pkt: %u -> %u",
522 session->name, session->nr,
523 L2TP_SKB_CB(skb)->ns);
James Chapman38d40b32012-05-09 23:43:08 +0000524 session->reorder_skip = 0;
525 session->nr = L2TP_SKB_CB(skb)->ns;
526 }
James Chapmanfd558d12010-04-02 06:18:33 +0000527 if (L2TP_SKB_CB(skb)->ns != session->nr) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000528 l2tp_dbg(session, L2TP_MSG_SEQ,
529 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
530 session->name, L2TP_SKB_CB(skb)->ns,
531 L2TP_SKB_CB(skb)->length, session->nr,
532 skb_queue_len(&session->reorder_q));
James Chapmanfd558d12010-04-02 06:18:33 +0000533 goto out;
534 }
535 }
536 __skb_unlink(skb, &session->reorder_q);
537
538 /* Process the skb. We release the queue lock while we
539 * do so to let other contexts process the queue.
540 */
541 spin_unlock_bh(&session->reorder_q.lock);
542 l2tp_recv_dequeue_skb(session, skb);
Eric Dumazete2e210c2011-11-02 22:47:44 +0000543 goto start;
James Chapmanfd558d12010-04-02 06:18:33 +0000544 }
545
546out:
547 spin_unlock_bh(&session->reorder_q.lock);
548}
549
James Chapman8a1631d2013-07-02 20:28:59 +0100550static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
551{
552 u32 nws;
553
554 if (nr >= session->nr)
555 nws = nr - session->nr;
556 else
557 nws = (session->nr_max + 1) - (session->nr - nr);
558
559 return nws < session->nr_window_size;
560}
561
James Chapmanb6dc01a2013-07-02 20:28:58 +0100562/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
563 * acceptable, else non-zero.
564 */
565static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
566{
James Chapman8a1631d2013-07-02 20:28:59 +0100567 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
568 /* Packet sequence number is outside allowed window.
569 * Discard it.
570 */
571 l2tp_dbg(session, L2TP_MSG_SEQ,
572 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
573 session->name, L2TP_SKB_CB(skb)->ns,
574 L2TP_SKB_CB(skb)->length, session->nr);
575 goto discard;
576 }
577
James Chapmanb6dc01a2013-07-02 20:28:58 +0100578 if (session->reorder_timeout != 0) {
579 /* Packet reordering enabled. Add skb to session's
580 * reorder queue, in order of ns.
581 */
582 l2tp_recv_queue_skb(session, skb);
James Chapmana0dbd822013-07-02 20:29:00 +0100583 goto out;
584 }
585
586 /* Packet reordering disabled. Discard out-of-sequence packets, while
587 * tracking the number if in-sequence packets after the first OOS packet
588 * is seen. After nr_oos_count_max in-sequence packets, reset the
589 * sequence number to re-enable packet reception.
590 */
591 if (L2TP_SKB_CB(skb)->ns == session->nr) {
592 skb_queue_tail(&session->reorder_q, skb);
James Chapmanb6dc01a2013-07-02 20:28:58 +0100593 } else {
James Chapmana0dbd822013-07-02 20:29:00 +0100594 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
595 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
596
597 if (nr_oos == nr_next)
598 session->nr_oos_count++;
599 else
600 session->nr_oos_count = 0;
601
602 session->nr_oos = nr_oos;
603 if (session->nr_oos_count > session->nr_oos_count_max) {
604 session->reorder_skip = 1;
605 l2tp_dbg(session, L2TP_MSG_SEQ,
606 "%s: %d oos packets received. Resetting sequence numbers\n",
607 session->name, session->nr_oos_count);
608 }
609 if (!session->reorder_skip) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100610 atomic_long_inc(&session->stats.rx_seq_discards);
611 l2tp_dbg(session, L2TP_MSG_SEQ,
612 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
613 session->name, L2TP_SKB_CB(skb)->ns,
614 L2TP_SKB_CB(skb)->length, session->nr,
615 skb_queue_len(&session->reorder_q));
616 goto discard;
617 }
618 skb_queue_tail(&session->reorder_q, skb);
619 }
620
James Chapmana0dbd822013-07-02 20:29:00 +0100621out:
James Chapmanb6dc01a2013-07-02 20:28:58 +0100622 return 0;
623
624discard:
625 return 1;
626}
627
James Chapmanf7faffa2010-04-02 06:18:49 +0000628/* Do receive processing of L2TP data frames. We handle both L2TPv2
629 * and L2TPv3 data frames here.
630 *
631 * L2TPv2 Data Message Header
632 *
633 * 0 1 2 3
634 * 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
635 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
637 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638 * | Tunnel ID | Session ID |
639 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
640 * | Ns (opt) | Nr (opt) |
641 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
642 * | Offset Size (opt) | Offset pad... (opt)
643 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
644 *
645 * Data frames are marked by T=0. All other fields are the same as
646 * those in L2TP control frames.
647 *
648 * L2TPv3 Data Message Header
649 *
650 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
651 * | L2TP Session Header |
652 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
653 * | L2-Specific Sublayer |
654 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
655 * | Tunnel Payload ...
656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657 *
658 * L2TPv3 Session Header Over IP
659 *
660 * 0 1 2 3
661 * 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
662 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
663 * | Session ID |
664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
665 * | Cookie (optional, maximum 64 bits)...
666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
667 * |
668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669 *
670 * L2TPv3 L2-Specific Sublayer Format
671 *
672 * 0 1 2 3
673 * 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
674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
675 * |x|S|x|x|x|x|x|x| Sequence Number |
676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
677 *
678 * Cookie value, sublayer format and offset (pad) are negotiated with
679 * the peer when the session is set up. Unlike L2TPv2, we do not need
680 * to parse the packet header to determine if optional fields are
681 * present.
682 *
683 * Caller must already have parsed the frame and determined that it is
684 * a data (not control) frame before coming here. Fields up to the
685 * session-id have already been parsed and ptr points to the data
686 * after the session-id.
Guillaume Nault61b9a042017-03-31 13:02:25 +0200687 *
688 * session->ref() must have been called prior to l2tp_recv_common().
689 * session->deref() will be called automatically after skb is processed.
James Chapmanfd558d12010-04-02 06:18:33 +0000690 */
James Chapmanf7faffa2010-04-02 06:18:49 +0000691void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
692 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
693 int length, int (*payload_hook)(struct sk_buff *skb))
James Chapmanfd558d12010-04-02 06:18:33 +0000694{
James Chapmanf7faffa2010-04-02 06:18:49 +0000695 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +0000696 int offset;
James Chapmanf7faffa2010-04-02 06:18:49 +0000697 u32 ns, nr;
James Chapmanfd558d12010-04-02 06:18:33 +0000698
James Chapmanf7faffa2010-04-02 06:18:49 +0000699 /* Parse and check optional cookie */
700 if (session->peer_cookie_len > 0) {
701 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000702 l2tp_info(tunnel, L2TP_MSG_DATA,
703 "%s: cookie mismatch (%u/%u). Discarding.\n",
704 tunnel->name, tunnel->tunnel_id,
705 session->session_id);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000706 atomic_long_inc(&session->stats.rx_cookie_discards);
James Chapmanf7faffa2010-04-02 06:18:49 +0000707 goto discard;
708 }
709 ptr += session->peer_cookie_len;
710 }
711
James Chapmanfd558d12010-04-02 06:18:33 +0000712 /* Handle the optional sequence numbers. Sequence numbers are
713 * in different places for L2TPv2 and L2TPv3.
714 *
715 * If we are the LAC, enable/disable sequence numbers under
716 * the control of the LNS. If no sequence numbers present but
717 * we were expecting them, discard frame.
718 */
719 ns = nr = 0;
720 L2TP_SKB_CB(skb)->has_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000721 if (tunnel->version == L2TP_HDR_VER_2) {
722 if (hdrflags & L2TP_HDRFLAG_S) {
723 ns = ntohs(*(__be16 *) ptr);
724 ptr += 2;
725 nr = ntohs(*(__be16 *) ptr);
726 ptr += 2;
James Chapmanfd558d12010-04-02 06:18:33 +0000727
James Chapmanf7faffa2010-04-02 06:18:49 +0000728 /* Store L2TP info in the skb */
729 L2TP_SKB_CB(skb)->ns = ns;
730 L2TP_SKB_CB(skb)->has_seq = 1;
James Chapmanfd558d12010-04-02 06:18:33 +0000731
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000732 l2tp_dbg(session, L2TP_MSG_SEQ,
733 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
734 session->name, ns, nr, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000735 }
736 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
737 u32 l2h = ntohl(*(__be32 *) ptr);
738
739 if (l2h & 0x40000000) {
740 ns = l2h & 0x00ffffff;
741
742 /* Store L2TP info in the skb */
743 L2TP_SKB_CB(skb)->ns = ns;
744 L2TP_SKB_CB(skb)->has_seq = 1;
745
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000746 l2tp_dbg(session, L2TP_MSG_SEQ,
747 "%s: recv data ns=%u, session nr=%u\n",
748 session->name, ns, session->nr);
James Chapmanf7faffa2010-04-02 06:18:49 +0000749 }
James Chapmanfd558d12010-04-02 06:18:33 +0000750 }
751
James Chapmanf7faffa2010-04-02 06:18:49 +0000752 /* Advance past L2-specific header, if present */
753 ptr += session->l2specific_len;
754
James Chapmanfd558d12010-04-02 06:18:33 +0000755 if (L2TP_SKB_CB(skb)->has_seq) {
756 /* Received a packet with sequence numbers. If we're the LNS,
757 * check if we sre sending sequence numbers and if not,
758 * configure it so.
759 */
760 if ((!session->lns_mode) && (!session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000761 l2tp_info(session, L2TP_MSG_SEQ,
762 "%s: requested to enable seq numbers by LNS\n",
763 session->name);
Asbjørn Sloth Tønnesen3f9b9772016-11-07 20:39:28 +0000764 session->send_seq = 1;
James Chapmanf7faffa2010-04-02 06:18:49 +0000765 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000766 }
767 } else {
768 /* No sequence numbers.
769 * If user has configured mandatory sequence numbers, discard.
770 */
771 if (session->recv_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000772 l2tp_warn(session, L2TP_MSG_SEQ,
773 "%s: recv data has no seq numbers when required. Discarding.\n",
774 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000775 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000776 goto discard;
777 }
778
779 /* If we're the LAC and we're sending sequence numbers, the
780 * LNS has requested that we no longer send sequence numbers.
781 * If we're the LNS and we're sending sequence numbers, the
782 * LAC is broken. Discard the frame.
783 */
784 if ((!session->lns_mode) && (session->send_seq)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000785 l2tp_info(session, L2TP_MSG_SEQ,
786 "%s: requested to disable seq numbers by LNS\n",
787 session->name);
James Chapmanfd558d12010-04-02 06:18:33 +0000788 session->send_seq = 0;
James Chapmanf7faffa2010-04-02 06:18:49 +0000789 l2tp_session_set_header_len(session, tunnel->version);
James Chapmanfd558d12010-04-02 06:18:33 +0000790 } else if (session->send_seq) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000791 l2tp_warn(session, L2TP_MSG_SEQ,
792 "%s: recv data has no seq numbers when required. Discarding.\n",
793 session->name);
Tom Parkin7b7c0712013-03-19 06:11:22 +0000794 atomic_long_inc(&session->stats.rx_seq_discards);
James Chapmanfd558d12010-04-02 06:18:33 +0000795 goto discard;
796 }
797 }
798
James Chapmanf7faffa2010-04-02 06:18:49 +0000799 /* Session data offset is handled differently for L2TPv2 and
800 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
801 * the header. For L2TPv3, the offset is negotiated using AVPs
802 * in the session setup control protocol.
803 */
804 if (tunnel->version == L2TP_HDR_VER_2) {
805 /* If offset bit set, skip it. */
806 if (hdrflags & L2TP_HDRFLAG_O) {
807 offset = ntohs(*(__be16 *)ptr);
808 ptr += 2 + offset;
809 }
810 } else
811 ptr += session->offset;
James Chapmanfd558d12010-04-02 06:18:33 +0000812
813 offset = ptr - optr;
814 if (!pskb_may_pull(skb, offset))
815 goto discard;
816
817 __skb_pull(skb, offset);
818
819 /* If caller wants to process the payload before we queue the
820 * packet, do so now.
821 */
822 if (payload_hook)
823 if ((*payload_hook)(skb))
824 goto discard;
825
826 /* Prepare skb for adding to the session's reorder_q. Hold
827 * packets for max reorder_timeout or 1 second if not
828 * reordering.
829 */
830 L2TP_SKB_CB(skb)->length = length;
831 L2TP_SKB_CB(skb)->expires = jiffies +
832 (session->reorder_timeout ? session->reorder_timeout : HZ);
833
834 /* Add packet to the session's receive queue. Reordering is done here, if
835 * enabled. Saved L2TP protocol info is stored in skb->sb[].
836 */
837 if (L2TP_SKB_CB(skb)->has_seq) {
James Chapmanb6dc01a2013-07-02 20:28:58 +0100838 if (l2tp_recv_data_seq(session, skb))
839 goto discard;
James Chapmanfd558d12010-04-02 06:18:33 +0000840 } else {
841 /* No sequence numbers. Add the skb to the tail of the
842 * reorder queue. This ensures that it will be
843 * delivered after all previous sequenced skbs.
844 */
845 skb_queue_tail(&session->reorder_q, skb);
846 }
847
848 /* Try to dequeue as many skbs from reorder_q as we can. */
849 l2tp_recv_dequeue(session);
850
James Chapmanf7faffa2010-04-02 06:18:49 +0000851 return;
James Chapmanfd558d12010-04-02 06:18:33 +0000852
853discard:
Tom Parkin7b7c0712013-03-19 06:11:22 +0000854 atomic_long_inc(&session->stats.rx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +0000855 kfree_skb(skb);
856
857 if (session->deref)
858 (*session->deref)(session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000859}
860EXPORT_SYMBOL(l2tp_recv_common);
861
Tom Parkin48f72f92013-03-19 06:11:19 +0000862/* Drop skbs from the session's reorder_q
863 */
864int l2tp_session_queue_purge(struct l2tp_session *session)
865{
866 struct sk_buff *skb = NULL;
867 BUG_ON(!session);
868 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
869 while ((skb = skb_dequeue(&session->reorder_q))) {
870 atomic_long_inc(&session->stats.rx_errors);
871 kfree_skb(skb);
872 if (session->deref)
873 (*session->deref)(session);
874 }
875 return 0;
876}
877EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
878
James Chapmanf7faffa2010-04-02 06:18:49 +0000879/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
880 * here. The skb is not on a list when we get here.
881 * Returns 0 if the packet was a data packet and was successfully passed on.
882 * Returns 1 if the packet was not a good data packet and could not be
883 * forwarded. All such packets are passed up to userspace to deal with.
884 */
stephen hemmingerfc130842010-10-21 07:50:46 +0000885static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
886 int (*payload_hook)(struct sk_buff *skb))
James Chapmanf7faffa2010-04-02 06:18:49 +0000887{
888 struct l2tp_session *session = NULL;
889 unsigned char *ptr, *optr;
890 u16 hdrflags;
891 u32 tunnel_id, session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000892 u16 version;
893 int length;
894
Tom Herbert58d60852014-05-07 16:52:48 -0700895 /* UDP has verifed checksum */
James Chapmanf7faffa2010-04-02 06:18:49 +0000896
897 /* UDP always verifies the packet length. */
898 __skb_pull(skb, sizeof(struct udphdr));
899
900 /* Short packet? */
901 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000902 l2tp_info(tunnel, L2TP_MSG_DATA,
903 "%s: recv short packet (len=%d)\n",
904 tunnel->name, skb->len);
James Chapmanf7faffa2010-04-02 06:18:49 +0000905 goto error;
906 }
907
James Chapmanf7faffa2010-04-02 06:18:49 +0000908 /* Trace packet contents, if enabled */
909 if (tunnel->debug & L2TP_MSG_DATA) {
910 length = min(32u, skb->len);
911 if (!pskb_may_pull(skb, length))
912 goto error;
913
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000914 pr_debug("%s: recv\n", tunnel->name);
915 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000916 }
917
Eric Dumazete50e7052011-11-08 13:59:44 -0500918 /* Point to L2TP header */
919 optr = ptr = skb->data;
920
James Chapmanf7faffa2010-04-02 06:18:49 +0000921 /* Get L2TP header flags */
922 hdrflags = ntohs(*(__be16 *) ptr);
923
924 /* Check protocol version */
925 version = hdrflags & L2TP_HDR_VER_MASK;
926 if (version != tunnel->version) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000927 l2tp_info(tunnel, L2TP_MSG_DATA,
928 "%s: recv protocol version mismatch: got %d expected %d\n",
929 tunnel->name, version, tunnel->version);
James Chapmanf7faffa2010-04-02 06:18:49 +0000930 goto error;
931 }
932
933 /* Get length of L2TP packet */
934 length = skb->len;
935
936 /* If type is control packet, it is handled by userspace. */
937 if (hdrflags & L2TP_HDRFLAG_T) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000938 l2tp_dbg(tunnel, L2TP_MSG_DATA,
939 "%s: recv control packet, len=%d\n",
940 tunnel->name, length);
James Chapmanf7faffa2010-04-02 06:18:49 +0000941 goto error;
942 }
943
944 /* Skip flags */
945 ptr += 2;
946
947 if (tunnel->version == L2TP_HDR_VER_2) {
948 /* If length is present, skip it */
949 if (hdrflags & L2TP_HDRFLAG_L)
950 ptr += 2;
951
952 /* Extract tunnel and session ID */
953 tunnel_id = ntohs(*(__be16 *) ptr);
954 ptr += 2;
955 session_id = ntohs(*(__be16 *) ptr);
956 ptr += 2;
957 } else {
958 ptr += 2; /* skip reserved bits */
959 tunnel_id = tunnel->tunnel_id;
960 session_id = ntohl(*(__be32 *) ptr);
961 ptr += 4;
962 }
963
964 /* Find the session context */
Guillaume Nault61b9a042017-03-31 13:02:25 +0200965 session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id, true);
James Chapman309795f2010-04-02 06:19:10 +0000966 if (!session || !session->recv_skb) {
Guillaume Nault61b9a042017-03-31 13:02:25 +0200967 if (session) {
968 if (session->deref)
969 session->deref(session);
970 l2tp_session_dec_refcount(session);
971 }
972
James Chapmanf7faffa2010-04-02 06:18:49 +0000973 /* Not found? Pass to userspace to deal with */
Joe Perchesa4ca44f2012-05-16 09:55:56 +0000974 l2tp_info(tunnel, L2TP_MSG_DATA,
975 "%s: no session found (%u/%u). Passing up.\n",
976 tunnel->name, tunnel_id, session_id);
James Chapmanf7faffa2010-04-02 06:18:49 +0000977 goto error;
978 }
979
980 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
Guillaume Nault61b9a042017-03-31 13:02:25 +0200981 l2tp_session_dec_refcount(session);
James Chapmanfd558d12010-04-02 06:18:33 +0000982
983 return 0;
984
James Chapmanfd558d12010-04-02 06:18:33 +0000985error:
986 /* Put UDP header back */
987 __skb_push(skb, sizeof(struct udphdr));
988
989 return 1;
990}
James Chapmanfd558d12010-04-02 06:18:33 +0000991
992/* UDP encapsulation receive handler. See net/ipv4/udp.c.
993 * Return codes:
994 * 0 : success.
995 * <0: error
996 * >0: skb should be passed up to userspace as UDP.
997 */
998int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
999{
1000 struct l2tp_tunnel *tunnel;
1001
1002 tunnel = l2tp_sock_to_tunnel(sk);
1003 if (tunnel == NULL)
1004 goto pass_up;
1005
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001006 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1007 tunnel->name, skb->len);
James Chapmanfd558d12010-04-02 06:18:33 +00001008
1009 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1010 goto pass_up_put;
1011
1012 sock_put(sk);
1013 return 0;
1014
1015pass_up_put:
1016 sock_put(sk);
1017pass_up:
1018 return 1;
1019}
1020EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1021
1022/************************************************************************
1023 * Transmit handling
1024 ***********************************************************************/
1025
1026/* Build an L2TP header for the session into the buffer provided.
1027 */
James Chapmanf7faffa2010-04-02 06:18:49 +00001028static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001029{
James Chapmanf7faffa2010-04-02 06:18:49 +00001030 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001031 __be16 *bufp = buf;
James Chapmanf7faffa2010-04-02 06:18:49 +00001032 __be16 *optr = buf;
James Chapmanfd558d12010-04-02 06:18:33 +00001033 u16 flags = L2TP_HDR_VER_2;
1034 u32 tunnel_id = tunnel->peer_tunnel_id;
1035 u32 session_id = session->peer_session_id;
1036
1037 if (session->send_seq)
1038 flags |= L2TP_HDRFLAG_S;
1039
1040 /* Setup L2TP header. */
1041 *bufp++ = htons(flags);
1042 *bufp++ = htons(tunnel_id);
1043 *bufp++ = htons(session_id);
1044 if (session->send_seq) {
1045 *bufp++ = htons(session->ns);
1046 *bufp++ = 0;
1047 session->ns++;
James Chapmanf7faffa2010-04-02 06:18:49 +00001048 session->ns &= 0xffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001049 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1050 session->name, session->ns);
James Chapmanfd558d12010-04-02 06:18:33 +00001051 }
James Chapmanf7faffa2010-04-02 06:18:49 +00001052
1053 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001054}
1055
James Chapmanf7faffa2010-04-02 06:18:49 +00001056static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
James Chapmanfd558d12010-04-02 06:18:33 +00001057{
James Chapman0d767512010-04-02 06:19:00 +00001058 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanf7faffa2010-04-02 06:18:49 +00001059 char *bufp = buf;
1060 char *optr = bufp;
James Chapmanfd558d12010-04-02 06:18:33 +00001061
James Chapman0d767512010-04-02 06:19:00 +00001062 /* Setup L2TP header. The header differs slightly for UDP and
1063 * IP encapsulations. For UDP, there is 4 bytes of flags.
1064 */
1065 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1066 u16 flags = L2TP_HDR_VER_3;
1067 *((__be16 *) bufp) = htons(flags);
1068 bufp += 2;
1069 *((__be16 *) bufp) = 0;
1070 bufp += 2;
1071 }
1072
James Chapmanf7faffa2010-04-02 06:18:49 +00001073 *((__be32 *) bufp) = htonl(session->peer_session_id);
1074 bufp += 4;
1075 if (session->cookie_len) {
1076 memcpy(bufp, &session->cookie[0], session->cookie_len);
1077 bufp += session->cookie_len;
1078 }
1079 if (session->l2specific_len) {
1080 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1081 u32 l2h = 0;
1082 if (session->send_seq) {
1083 l2h = 0x40000000 | session->ns;
1084 session->ns++;
1085 session->ns &= 0xffffff;
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001086 l2tp_dbg(session, L2TP_MSG_SEQ,
1087 "%s: updated ns to %u\n",
1088 session->name, session->ns);
James Chapmanf7faffa2010-04-02 06:18:49 +00001089 }
1090
1091 *((__be32 *) bufp) = htonl(l2h);
1092 }
1093 bufp += session->l2specific_len;
1094 }
1095 if (session->offset)
1096 bufp += session->offset;
1097
1098 return bufp - optr;
James Chapmanfd558d12010-04-02 06:18:33 +00001099}
James Chapmanfd558d12010-04-02 06:18:33 +00001100
stephen hemmingerfc130842010-10-21 07:50:46 +00001101static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
David S. Millerd9d8da82011-05-06 22:23:20 -07001102 struct flowi *fl, size_t data_len)
James Chapmanfd558d12010-04-02 06:18:33 +00001103{
1104 struct l2tp_tunnel *tunnel = session->tunnel;
1105 unsigned int len = skb->len;
1106 int error;
1107
1108 /* Debug */
1109 if (session->send_seq)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001110 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001111 session->name, data_len, session->ns - 1);
James Chapmanfd558d12010-04-02 06:18:33 +00001112 else
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001113 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001114 session->name, data_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001115
1116 if (session->debug & L2TP_MSG_DATA) {
James Chapman0d767512010-04-02 06:19:00 +00001117 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1118 unsigned char *datap = skb->data + uhlen;
James Chapmanfd558d12010-04-02 06:18:33 +00001119
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001120 pr_debug("%s: xmit\n", session->name);
1121 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1122 datap, min_t(size_t, 32, len - uhlen));
James Chapmanfd558d12010-04-02 06:18:33 +00001123 }
1124
1125 /* Queue the packet to IP for output */
WANG Cong60ff7462014-05-04 16:39:18 -07001126 skb->ignore_df = 1;
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001127#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet746e3492014-03-07 14:57:43 -08001128 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
Eric Dumazetb0270e92014-04-15 12:58:34 -04001129 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001130 else
1131#endif
Eric Dumazetb0270e92014-04-15 12:58:34 -04001132 error = ip_queue_xmit(tunnel->sock, skb, fl);
James Chapmanfd558d12010-04-02 06:18:33 +00001133
1134 /* Update stats */
1135 if (error >= 0) {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001136 atomic_long_inc(&tunnel->stats.tx_packets);
1137 atomic_long_add(len, &tunnel->stats.tx_bytes);
1138 atomic_long_inc(&session->stats.tx_packets);
1139 atomic_long_add(len, &session->stats.tx_bytes);
James Chapmanfd558d12010-04-02 06:18:33 +00001140 } else {
Tom Parkin7b7c0712013-03-19 06:11:22 +00001141 atomic_long_inc(&tunnel->stats.tx_errors);
1142 atomic_long_inc(&session->stats.tx_errors);
James Chapmanfd558d12010-04-02 06:18:33 +00001143 }
1144
1145 return 0;
1146}
James Chapmanfd558d12010-04-02 06:18:33 +00001147
James Chapmanfd558d12010-04-02 06:18:33 +00001148/* If caller requires the skb to have a ppp header, the header must be
1149 * inserted in the skb data before calling this function.
1150 */
1151int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1152{
1153 int data_len = skb->len;
James Chapman0d767512010-04-02 06:19:00 +00001154 struct l2tp_tunnel *tunnel = session->tunnel;
1155 struct sock *sk = tunnel->sock;
David S. Millerd9d8da82011-05-06 22:23:20 -07001156 struct flowi *fl;
James Chapmanfd558d12010-04-02 06:18:33 +00001157 struct udphdr *uh;
James Chapmanfd558d12010-04-02 06:18:33 +00001158 struct inet_sock *inet;
James Chapmanfd558d12010-04-02 06:18:33 +00001159 int headroom;
James Chapman0d767512010-04-02 06:19:00 +00001160 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1161 int udp_len;
Eric Dumazetb8c84302012-06-28 20:15:13 +00001162 int ret = NET_XMIT_SUCCESS;
James Chapmanfd558d12010-04-02 06:18:33 +00001163
1164 /* Check that there's enough headroom in the skb to insert IP,
1165 * UDP and L2TP headers. If not enough, expand it to
1166 * make room. Adjust truesize.
1167 */
1168 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
James Chapman0d767512010-04-02 06:19:00 +00001169 uhlen + hdr_len;
Eric Dumazet835acf52011-10-07 05:35:46 +00001170 if (skb_cow_head(skb, headroom)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001171 kfree_skb(skb);
1172 return NET_XMIT_DROP;
Eric Dumazet835acf52011-10-07 05:35:46 +00001173 }
James Chapmanfd558d12010-04-02 06:18:33 +00001174
James Chapmanfd558d12010-04-02 06:18:33 +00001175 /* Setup L2TP header */
James Chapmanf7faffa2010-04-02 06:18:49 +00001176 session->build_header(session, __skb_push(skb, hdr_len));
James Chapmanfd558d12010-04-02 06:18:33 +00001177
James Chapman0d767512010-04-02 06:19:00 +00001178 /* Reset skb netfilter state */
James Chapmanfd558d12010-04-02 06:18:33 +00001179 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1180 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1181 IPSKB_REROUTED);
1182 nf_reset(skb);
1183
David S. Miller6af88da2011-05-08 13:45:20 -07001184 bh_lock_sock(sk);
1185 if (sock_owned_by_user(sk)) {
Eric Dumazetb8c84302012-06-28 20:15:13 +00001186 kfree_skb(skb);
1187 ret = NET_XMIT_DROP;
David S. Miller6af88da2011-05-08 13:45:20 -07001188 goto out_unlock;
1189 }
1190
James Chapmanfd558d12010-04-02 06:18:33 +00001191 /* Get routing info from the tunnel socket */
1192 skb_dst_drop(skb);
Florian Westphal71b13912011-11-25 06:47:16 +00001193 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
James Chapmanfd558d12010-04-02 06:18:33 +00001194
David S. Millerd9d8da82011-05-06 22:23:20 -07001195 inet = inet_sk(sk);
1196 fl = &inet->cork.fl;
James Chapman0d767512010-04-02 06:19:00 +00001197 switch (tunnel->encap) {
1198 case L2TP_ENCAPTYPE_UDP:
1199 /* Setup UDP header */
James Chapman0d767512010-04-02 06:19:00 +00001200 __skb_push(skb, sizeof(*uh));
1201 skb_reset_transport_header(skb);
1202 uh = udp_hdr(skb);
1203 uh->source = inet->inet_sport;
1204 uh->dest = inet->inet_dport;
1205 udp_len = uhlen + hdr_len + data_len;
1206 uh->len = htons(udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001207
1208 /* Calculate UDP checksum if configured to do so */
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001209#if IS_ENABLED(CONFIG_IPV6)
François Cachereule18503f2013-10-02 10:16:02 +02001210 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
Tom Herbert77157e12014-06-04 17:19:56 -07001211 udp6_set_csum(udp_get_no_check6_tx(sk),
1212 skb, &inet6_sk(sk)->saddr,
1213 &sk->sk_v6_daddr, udp_len);
Benjamin LaHaised2cf3362012-04-27 08:24:18 +00001214 else
1215#endif
Tom Herbert77157e12014-06-04 17:19:56 -07001216 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1217 inet->inet_daddr, udp_len);
James Chapman0d767512010-04-02 06:19:00 +00001218 break;
1219
1220 case L2TP_ENCAPTYPE_IP:
1221 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001222 }
1223
David S. Millerd9d8da82011-05-06 22:23:20 -07001224 l2tp_xmit_core(session, skb, fl, data_len);
David S. Miller6af88da2011-05-08 13:45:20 -07001225out_unlock:
1226 bh_unlock_sock(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001227
Eric Dumazetb8c84302012-06-28 20:15:13 +00001228 return ret;
James Chapmanfd558d12010-04-02 06:18:33 +00001229}
1230EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1231
1232/*****************************************************************************
1233 * Tinnel and session create/destroy.
1234 *****************************************************************************/
1235
1236/* Tunnel socket destruct hook.
1237 * The tunnel context is deleted only when all session sockets have been
1238 * closed.
1239 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001240static void l2tp_tunnel_destruct(struct sock *sk)
James Chapmanfd558d12010-04-02 06:18:33 +00001241{
David S. Miller8d8a51e2013-10-08 15:44:26 -04001242 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001243 struct l2tp_net *pn;
James Chapmanfd558d12010-04-02 06:18:33 +00001244
James Chapmanfd558d12010-04-02 06:18:33 +00001245 if (tunnel == NULL)
1246 goto end;
1247
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001248 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001249
James Chapmanfd558d12010-04-02 06:18:33 +00001250
Tom Parkinf8ccac02013-01-31 23:43:00 +00001251 /* Disable udp encapsulation */
James Chapman0d767512010-04-02 06:19:00 +00001252 switch (tunnel->encap) {
1253 case L2TP_ENCAPTYPE_UDP:
1254 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1255 (udp_sk(sk))->encap_type = 0;
1256 (udp_sk(sk))->encap_rcv = NULL;
Tom Parkin9980d002013-03-19 06:11:13 +00001257 (udp_sk(sk))->encap_destroy = NULL;
James Chapman0d767512010-04-02 06:19:00 +00001258 break;
1259 case L2TP_ENCAPTYPE_IP:
1260 break;
1261 }
James Chapmanfd558d12010-04-02 06:18:33 +00001262
1263 /* Remove hooks into tunnel socket */
James Chapmanfd558d12010-04-02 06:18:33 +00001264 sk->sk_destruct = tunnel->old_sk_destruct;
1265 sk->sk_user_data = NULL;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001266 tunnel->sock = NULL;
1267
1268 /* Remove the tunnel struct from the tunnel list */
1269 pn = l2tp_pernet(tunnel->l2tp_net);
1270 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1271 list_del_rcu(&tunnel->list);
1272 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1273 atomic_dec(&l2tp_tunnel_count);
1274
1275 l2tp_tunnel_closeall(tunnel);
1276 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001277
1278 /* Call the original destructor */
1279 if (sk->sk_destruct)
1280 (*sk->sk_destruct)(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001281end:
1282 return;
1283}
James Chapmanfd558d12010-04-02 06:18:33 +00001284
1285/* When the tunnel is closed, all the attached sessions need to go too.
1286 */
Tom Parkine34f4c72013-03-19 06:11:14 +00001287void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001288{
1289 int hash;
1290 struct hlist_node *walk;
1291 struct hlist_node *tmp;
1292 struct l2tp_session *session;
1293
1294 BUG_ON(tunnel == NULL);
1295
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001296 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1297 tunnel->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001298
1299 write_lock_bh(&tunnel->hlist_lock);
1300 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1301again:
1302 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1303 session = hlist_entry(walk, struct l2tp_session, hlist);
1304
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001305 l2tp_info(session, L2TP_MSG_CONTROL,
1306 "%s: closing session\n", session->name);
James Chapmanfd558d12010-04-02 06:18:33 +00001307
1308 hlist_del_init(&session->hlist);
1309
James Chapmanfd558d12010-04-02 06:18:33 +00001310 if (session->ref != NULL)
1311 (*session->ref)(session);
1312
1313 write_unlock_bh(&tunnel->hlist_lock);
1314
Tom Parkinf6e16b22013-03-19 06:11:23 +00001315 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001316 l2tp_session_queue_purge(session);
1317
James Chapmanfd558d12010-04-02 06:18:33 +00001318 if (session->session_close != NULL)
1319 (*session->session_close)(session);
1320
1321 if (session->deref != NULL)
1322 (*session->deref)(session);
1323
Tom Parkin9980d002013-03-19 06:11:13 +00001324 l2tp_session_dec_refcount(session);
1325
James Chapmanfd558d12010-04-02 06:18:33 +00001326 write_lock_bh(&tunnel->hlist_lock);
1327
1328 /* Now restart from the beginning of this hash
1329 * chain. We always remove a session from the
1330 * list so we are guaranteed to make forward
1331 * progress.
1332 */
1333 goto again;
1334 }
1335 }
1336 write_unlock_bh(&tunnel->hlist_lock);
1337}
Tom Parkine34f4c72013-03-19 06:11:14 +00001338EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
James Chapmanfd558d12010-04-02 06:18:33 +00001339
Tom Parkin9980d002013-03-19 06:11:13 +00001340/* Tunnel socket destroy hook for UDP encapsulation */
1341static void l2tp_udp_encap_destroy(struct sock *sk)
1342{
1343 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1344 if (tunnel) {
1345 l2tp_tunnel_closeall(tunnel);
1346 sock_put(sk);
1347 }
1348}
1349
James Chapmanfd558d12010-04-02 06:18:33 +00001350/* Really kill the tunnel.
1351 * Come here only when all sessions have been cleared from the tunnel.
1352 */
stephen hemmingerfc130842010-10-21 07:50:46 +00001353static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
James Chapmanfd558d12010-04-02 06:18:33 +00001354{
James Chapmanfd558d12010-04-02 06:18:33 +00001355 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1356 BUG_ON(tunnel->sock != NULL);
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001357 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
xeb@mail.ru99469c32012-08-24 01:07:38 +00001358 kfree_rcu(tunnel, rcu);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001359}
James Chapmanfd558d12010-04-02 06:18:33 +00001360
Tom Parkinf8ccac02013-01-31 23:43:00 +00001361/* Workqueue tunnel deletion function */
1362static void l2tp_tunnel_del_work(struct work_struct *work)
1363{
1364 struct l2tp_tunnel *tunnel = NULL;
1365 struct socket *sock = NULL;
1366 struct sock *sk = NULL;
1367
1368 tunnel = container_of(work, struct l2tp_tunnel, del_work);
Ridge Kennedy12d656a2017-02-22 14:59:49 +13001369
1370 l2tp_tunnel_closeall(tunnel);
1371
Tom Parkinf8ccac02013-01-31 23:43:00 +00001372 sk = l2tp_tunnel_sock_lookup(tunnel);
1373 if (!sk)
Alexander Couzens06a15f52015-09-28 11:32:42 +02001374 goto out;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001375
1376 sock = sk->sk_socket;
Tom Parkinf8ccac02013-01-31 23:43:00 +00001377
Tom Parkin02d13ed2013-03-19 06:11:18 +00001378 /* If the tunnel socket was created by userspace, then go through the
1379 * inet layer to shut the socket down, and let userspace close it.
1380 * Otherwise, if we created the socket directly within the kernel, use
1381 * the sk API to release it here.
Tom Parkin167eb172013-01-31 23:43:03 +00001382 * In either case the tunnel resources are freed in the socket
1383 * destructor when the tunnel socket goes away.
Tom Parkinf8ccac02013-01-31 23:43:00 +00001384 */
Tom Parkin02d13ed2013-03-19 06:11:18 +00001385 if (tunnel->fd >= 0) {
1386 if (sock)
1387 inet_shutdown(sock, 2);
Tom Parkin167eb172013-01-31 23:43:03 +00001388 } else {
Eric W. Biederman26abe142015-05-08 21:10:31 -05001389 if (sock) {
Tom Parkin02d13ed2013-03-19 06:11:18 +00001390 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001391 sock_release(sock);
1392 }
Tom Parkin167eb172013-01-31 23:43:03 +00001393 }
Tom Parkinf8ccac02013-01-31 23:43:00 +00001394
1395 l2tp_tunnel_sock_put(sk);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001396out:
1397 l2tp_tunnel_dec_refcount(tunnel);
James Chapmanfd558d12010-04-02 06:18:33 +00001398}
James Chapmanfd558d12010-04-02 06:18:33 +00001399
James Chapman789a4a22010-04-02 06:19:40 +00001400/* Create a socket for the tunnel, if one isn't set up by
1401 * userspace. This is used for static tunnels where there is no
1402 * managing L2TP daemon.
Tom Parkin167eb172013-01-31 23:43:03 +00001403 *
1404 * Since we don't want these sockets to keep a namespace alive by
1405 * themselves, we drop the socket's namespace refcount after creation.
1406 * These sockets are freed when the namespace exits using the pernet
1407 * exit hook.
James Chapman789a4a22010-04-02 06:19:40 +00001408 */
Tom Parkin167eb172013-01-31 23:43:03 +00001409static int l2tp_tunnel_sock_create(struct net *net,
1410 u32 tunnel_id,
1411 u32 peer_tunnel_id,
1412 struct l2tp_tunnel_cfg *cfg,
1413 struct socket **sockp)
James Chapman789a4a22010-04-02 06:19:40 +00001414{
1415 int err = -EINVAL;
Eric Dumazet7bddd0d2010-04-04 01:02:46 -07001416 struct socket *sock = NULL;
Tom Herbert85644b42014-07-13 19:49:48 -07001417 struct udp_port_cfg udp_conf;
James Chapman789a4a22010-04-02 06:19:40 +00001418
1419 switch (cfg->encap) {
1420 case L2TP_ENCAPTYPE_UDP:
Tom Herbert85644b42014-07-13 19:49:48 -07001421 memset(&udp_conf, 0, sizeof(udp_conf));
1422
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001423#if IS_ENABLED(CONFIG_IPV6)
1424 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001425 udp_conf.family = AF_INET6;
1426 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1427 sizeof(udp_conf.local_ip6));
1428 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1429 sizeof(udp_conf.peer_ip6));
1430 udp_conf.use_udp6_tx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001431 ! cfg->udp6_zero_tx_checksums;
Tom Herbert85644b42014-07-13 19:49:48 -07001432 udp_conf.use_udp6_rx_checksums =
Wang Shanker018f8252016-04-29 01:29:43 +08001433 ! cfg->udp6_zero_rx_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001434 } else
1435#endif
1436 {
Tom Herbert85644b42014-07-13 19:49:48 -07001437 udp_conf.family = AF_INET;
1438 udp_conf.local_ip = cfg->local_ip;
1439 udp_conf.peer_ip = cfg->peer_ip;
1440 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001441 }
James Chapman789a4a22010-04-02 06:19:40 +00001442
Tom Herbert85644b42014-07-13 19:49:48 -07001443 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1444 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1445
1446 err = udp_sock_create(net, &udp_conf, &sock);
1447 if (err < 0)
1448 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001449
1450 break;
1451
1452 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001453#if IS_ENABLED(CONFIG_IPV6)
1454 if (cfg->local_ip6 && cfg->peer_ip6) {
Tom Herbert85644b42014-07-13 19:49:48 -07001455 struct sockaddr_l2tpip6 ip6_addr = {0};
1456
Eric W. Biederman26abe142015-05-08 21:10:31 -05001457 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001458 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001459 if (err < 0)
1460 goto out;
1461
James Chapman5dac94e2012-04-29 21:48:55 +00001462 ip6_addr.l2tp_family = AF_INET6;
1463 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1464 sizeof(ip6_addr.l2tp_addr));
1465 ip6_addr.l2tp_conn_id = tunnel_id;
1466 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1467 sizeof(ip6_addr));
1468 if (err < 0)
1469 goto out;
1470
1471 ip6_addr.l2tp_family = AF_INET6;
1472 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1473 sizeof(ip6_addr.l2tp_addr));
1474 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1475 err = kernel_connect(sock,
1476 (struct sockaddr *) &ip6_addr,
1477 sizeof(ip6_addr), 0);
1478 if (err < 0)
1479 goto out;
1480 } else
Chris Elstonf9bac8d2012-04-29 21:48:52 +00001481#endif
James Chapman5dac94e2012-04-29 21:48:55 +00001482 {
Tom Herbert85644b42014-07-13 19:49:48 -07001483 struct sockaddr_l2tpip ip_addr = {0};
1484
Eric W. Biederman26abe142015-05-08 21:10:31 -05001485 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
Tom Parkin167eb172013-01-31 23:43:03 +00001486 IPPROTO_L2TP, &sock);
James Chapman5dac94e2012-04-29 21:48:55 +00001487 if (err < 0)
1488 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001489
James Chapman5dac94e2012-04-29 21:48:55 +00001490 ip_addr.l2tp_family = AF_INET;
1491 ip_addr.l2tp_addr = cfg->local_ip;
1492 ip_addr.l2tp_conn_id = tunnel_id;
1493 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1494 sizeof(ip_addr));
1495 if (err < 0)
1496 goto out;
James Chapman789a4a22010-04-02 06:19:40 +00001497
James Chapman5dac94e2012-04-29 21:48:55 +00001498 ip_addr.l2tp_family = AF_INET;
1499 ip_addr.l2tp_addr = cfg->peer_ip;
1500 ip_addr.l2tp_conn_id = peer_tunnel_id;
1501 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1502 sizeof(ip_addr), 0);
1503 if (err < 0)
1504 goto out;
1505 }
James Chapman789a4a22010-04-02 06:19:40 +00001506 break;
1507
1508 default:
1509 goto out;
1510 }
1511
1512out:
Tom Parkin167eb172013-01-31 23:43:03 +00001513 *sockp = sock;
James Chapman789a4a22010-04-02 06:19:40 +00001514 if ((err < 0) && sock) {
Tom Parkin167eb172013-01-31 23:43:03 +00001515 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -05001516 sock_release(sock);
James Chapman789a4a22010-04-02 06:19:40 +00001517 *sockp = NULL;
1518 }
1519
1520 return err;
1521}
1522
Eric Dumazet37159ef2012-09-04 07:18:57 +00001523static struct lock_class_key l2tp_socket_class;
1524
James Chapmanfd558d12010-04-02 06:18:33 +00001525int 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)
1526{
1527 struct l2tp_tunnel *tunnel = NULL;
1528 int err;
1529 struct socket *sock = NULL;
1530 struct sock *sk = NULL;
1531 struct l2tp_net *pn;
James Chapman0d767512010-04-02 06:19:00 +00001532 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
James Chapmanfd558d12010-04-02 06:18:33 +00001533
1534 /* Get the tunnel socket from the fd, which was opened by
James Chapman789a4a22010-04-02 06:19:40 +00001535 * the userspace L2TP daemon. If not specified, create a
1536 * kernel socket.
James Chapmanfd558d12010-04-02 06:18:33 +00001537 */
James Chapman789a4a22010-04-02 06:19:40 +00001538 if (fd < 0) {
Tom Parkin167eb172013-01-31 23:43:03 +00001539 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1540 cfg, &sock);
James Chapman789a4a22010-04-02 06:19:40 +00001541 if (err < 0)
1542 goto err;
1543 } else {
James Chapman789a4a22010-04-02 06:19:40 +00001544 sock = sockfd_lookup(fd, &err);
1545 if (!sock) {
Tom Parkincbb95e02013-01-31 23:43:02 +00001546 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
James Chapman789a4a22010-04-02 06:19:40 +00001547 tunnel_id, fd, err);
Tom Parkincbb95e02013-01-31 23:43:02 +00001548 err = -EBADF;
1549 goto err;
1550 }
1551
1552 /* Reject namespace mismatches */
1553 if (!net_eq(sock_net(sock->sk), net)) {
1554 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1555 err = -EINVAL;
James Chapman789a4a22010-04-02 06:19:40 +00001556 goto err;
1557 }
James Chapmanfd558d12010-04-02 06:18:33 +00001558 }
1559
1560 sk = sock->sk;
1561
James Chapman0d767512010-04-02 06:19:00 +00001562 if (cfg != NULL)
1563 encap = cfg->encap;
1564
James Chapmanfd558d12010-04-02 06:18:33 +00001565 /* Quick sanity checks */
James Chapman0d767512010-04-02 06:19:00 +00001566 switch (encap) {
1567 case L2TP_ENCAPTYPE_UDP:
1568 err = -EPROTONOSUPPORT;
1569 if (sk->sk_protocol != IPPROTO_UDP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001570 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001571 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1572 goto err;
1573 }
1574 break;
1575 case L2TP_ENCAPTYPE_IP:
1576 err = -EPROTONOSUPPORT;
1577 if (sk->sk_protocol != IPPROTO_L2TP) {
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001578 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
James Chapman0d767512010-04-02 06:19:00 +00001579 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1580 goto err;
1581 }
1582 break;
James Chapmanfd558d12010-04-02 06:18:33 +00001583 }
1584
1585 /* Check if this socket has already been prepped */
David S. Miller8d8a51e2013-10-08 15:44:26 -04001586 tunnel = l2tp_tunnel(sk);
James Chapmanfd558d12010-04-02 06:18:33 +00001587 if (tunnel != NULL) {
1588 /* This socket has already been prepped */
1589 err = -EBUSY;
1590 goto err;
1591 }
1592
James Chapmanfd558d12010-04-02 06:18:33 +00001593 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1594 if (tunnel == NULL) {
1595 err = -ENOMEM;
1596 goto err;
1597 }
1598
1599 tunnel->version = version;
1600 tunnel->tunnel_id = tunnel_id;
1601 tunnel->peer_tunnel_id = peer_tunnel_id;
1602 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1603
1604 tunnel->magic = L2TP_TUNNEL_MAGIC;
1605 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1606 rwlock_init(&tunnel->hlist_lock);
1607
1608 /* The net we belong to */
1609 tunnel->l2tp_net = net;
1610 pn = l2tp_pernet(net);
1611
James Chapman0d767512010-04-02 06:19:00 +00001612 if (cfg != NULL)
James Chapmanfd558d12010-04-02 06:18:33 +00001613 tunnel->debug = cfg->debug;
1614
François Cachereule18503f2013-10-02 10:16:02 +02001615#if IS_ENABLED(CONFIG_IPV6)
1616 if (sk->sk_family == PF_INET6) {
1617 struct ipv6_pinfo *np = inet6_sk(sk);
1618
1619 if (ipv6_addr_v4mapped(&np->saddr) &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001620 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
François Cachereule18503f2013-10-02 10:16:02 +02001621 struct inet_sock *inet = inet_sk(sk);
1622
1623 tunnel->v4mapped = true;
1624 inet->inet_saddr = np->saddr.s6_addr32[3];
Eric Dumazetefe42082013-10-03 15:42:29 -07001625 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1626 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
François Cachereule18503f2013-10-02 10:16:02 +02001627 } else {
1628 tunnel->v4mapped = false;
1629 }
1630 }
1631#endif
1632
James Chapmanfd558d12010-04-02 06:18:33 +00001633 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
James Chapman0d767512010-04-02 06:19:00 +00001634 tunnel->encap = encap;
1635 if (encap == L2TP_ENCAPTYPE_UDP) {
Guillaume Naulta5c5e2d2016-06-08 12:59:17 +02001636 struct udp_tunnel_sock_cfg udp_cfg = { };
James Chapmanfd558d12010-04-02 06:18:33 +00001637
Andy Zhouc8fffce2014-09-16 17:31:19 -07001638 udp_cfg.sk_user_data = tunnel;
1639 udp_cfg.encap_type = UDP_ENCAP_L2TPINUDP;
1640 udp_cfg.encap_rcv = l2tp_udp_encap_recv;
1641 udp_cfg.encap_destroy = l2tp_udp_encap_destroy;
1642
1643 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1644 } else {
1645 sk->sk_user_data = tunnel;
1646 }
James Chapmanfd558d12010-04-02 06:18:33 +00001647
1648 /* Hook on the tunnel socket destructor so that we can cleanup
1649 * if the tunnel socket goes away.
1650 */
1651 tunnel->old_sk_destruct = sk->sk_destruct;
1652 sk->sk_destruct = &l2tp_tunnel_destruct;
1653 tunnel->sock = sk;
Tom Parkin80d84ef2013-01-22 05:13:48 +00001654 tunnel->fd = fd;
Eric Dumazet37159ef2012-09-04 07:18:57 +00001655 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1656
James Chapmanfd558d12010-04-02 06:18:33 +00001657 sk->sk_allocation = GFP_ATOMIC;
1658
Tom Parkinf8ccac02013-01-31 23:43:00 +00001659 /* Init delete workqueue struct */
1660 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1661
James Chapmanfd558d12010-04-02 06:18:33 +00001662 /* Add tunnel to our list */
1663 INIT_LIST_HEAD(&tunnel->list);
James Chapmanfd558d12010-04-02 06:18:33 +00001664 atomic_inc(&l2tp_tunnel_count);
1665
1666 /* Bump the reference count. The tunnel context is deleted
Eric Dumazet17691922011-05-11 18:22:36 +00001667 * only when this drops to zero. Must be done before list insertion
James Chapmanfd558d12010-04-02 06:18:33 +00001668 */
1669 l2tp_tunnel_inc_refcount(tunnel);
Eric Dumazet17691922011-05-11 18:22:36 +00001670 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1671 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1672 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001673
1674 err = 0;
1675err:
1676 if (tunnelp)
1677 *tunnelp = tunnel;
1678
James Chapman789a4a22010-04-02 06:19:40 +00001679 /* If tunnel's socket was created by the kernel, it doesn't
1680 * have a file.
1681 */
1682 if (sock && sock->file)
James Chapmanfd558d12010-04-02 06:18:33 +00001683 sockfd_put(sock);
1684
1685 return err;
1686}
1687EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1688
James Chapman309795f2010-04-02 06:19:10 +00001689/* This function is used by the netlink TUNNEL_DELETE command.
1690 */
1691int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1692{
Alexander Couzens06a15f52015-09-28 11:32:42 +02001693 l2tp_tunnel_inc_refcount(tunnel);
Alexander Couzens06a15f52015-09-28 11:32:42 +02001694 if (false == queue_work(l2tp_wq, &tunnel->del_work)) {
1695 l2tp_tunnel_dec_refcount(tunnel);
1696 return 1;
1697 }
1698 return 0;
James Chapman309795f2010-04-02 06:19:10 +00001699}
1700EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1701
James Chapmanfd558d12010-04-02 06:18:33 +00001702/* Really kill the session.
1703 */
1704void l2tp_session_free(struct l2tp_session *session)
1705{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001706 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapmanfd558d12010-04-02 06:18:33 +00001707
1708 BUG_ON(atomic_read(&session->ref_count) != 0);
1709
Tom Parkinf6e16b22013-03-19 06:11:23 +00001710 if (tunnel) {
James Chapmanfd558d12010-04-02 06:18:33 +00001711 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
James Chapmanfd558d12010-04-02 06:18:33 +00001712 if (session->session_id != 0)
1713 atomic_dec(&l2tp_session_count);
James Chapmanfd558d12010-04-02 06:18:33 +00001714 sock_put(tunnel->sock);
James Chapmanfd558d12010-04-02 06:18:33 +00001715 session->tunnel = NULL;
1716 l2tp_tunnel_dec_refcount(tunnel);
1717 }
1718
1719 kfree(session);
James Chapmanfd558d12010-04-02 06:18:33 +00001720}
1721EXPORT_SYMBOL_GPL(l2tp_session_free);
1722
Tom Parkinf6e16b22013-03-19 06:11:23 +00001723/* Remove an l2tp session from l2tp_core's hash lists.
1724 * Provides a tidyup interface for pseudowire code which can't just route all
1725 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1726 * callback.
1727 */
1728void __l2tp_session_unhash(struct l2tp_session *session)
1729{
1730 struct l2tp_tunnel *tunnel = session->tunnel;
1731
1732 /* Remove the session from core hashes */
1733 if (tunnel) {
1734 /* Remove from the per-tunnel hash */
1735 write_lock_bh(&tunnel->hlist_lock);
1736 hlist_del_init(&session->hlist);
1737 write_unlock_bh(&tunnel->hlist_lock);
1738
1739 /* For L2TPv3 we have a per-net hash: remove from there, too */
1740 if (tunnel->version != L2TP_HDR_VER_2) {
1741 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1742 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1743 hlist_del_init_rcu(&session->global_hlist);
1744 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1745 synchronize_rcu();
1746 }
1747 }
1748}
1749EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1750
James Chapman309795f2010-04-02 06:19:10 +00001751/* This function is used by the netlink SESSION_DELETE command and by
1752 pseudowire modules.
1753 */
1754int l2tp_session_delete(struct l2tp_session *session)
1755{
Tom Parkinf6e16b22013-03-19 06:11:23 +00001756 if (session->ref)
1757 (*session->ref)(session);
1758 __l2tp_session_unhash(session);
Tom Parkin4c6e2fd2013-03-19 06:11:20 +00001759 l2tp_session_queue_purge(session);
James Chapman309795f2010-04-02 06:19:10 +00001760 if (session->session_close != NULL)
1761 (*session->session_close)(session);
Tom Parkinf6e16b22013-03-19 06:11:23 +00001762 if (session->deref)
Dan Carpenter1b7c92b2013-03-22 21:33:15 +03001763 (*session->deref)(session);
James Chapman309795f2010-04-02 06:19:10 +00001764 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +00001765 return 0;
1766}
1767EXPORT_SYMBOL_GPL(l2tp_session_delete);
1768
James Chapmanf7faffa2010-04-02 06:18:49 +00001769/* We come here whenever a session's send_seq, cookie_len or
1770 * l2specific_len parameters are set.
1771 */
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001772void l2tp_session_set_header_len(struct l2tp_session *session, int version)
James Chapmanf7faffa2010-04-02 06:18:49 +00001773{
1774 if (version == L2TP_HDR_VER_2) {
1775 session->hdr_len = 6;
1776 if (session->send_seq)
1777 session->hdr_len += 4;
1778 } else {
James Chapman0d767512010-04-02 06:19:00 +00001779 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1780 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1781 session->hdr_len += 4;
James Chapmanf7faffa2010-04-02 06:18:49 +00001782 }
1783
1784}
Guillaume Naultbb5016e2014-03-06 11:14:30 +01001785EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
James Chapmanf7faffa2010-04-02 06:18:49 +00001786
James Chapmanfd558d12010-04-02 06:18:33 +00001787struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
1788{
1789 struct l2tp_session *session;
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001790 int err;
James Chapmanfd558d12010-04-02 06:18:33 +00001791
1792 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1793 if (session != NULL) {
1794 session->magic = L2TP_SESSION_MAGIC;
1795 session->tunnel = tunnel;
1796
1797 session->session_id = session_id;
1798 session->peer_session_id = peer_session_id;
James Chapmand301e322012-05-09 23:43:09 +00001799 session->nr = 0;
James Chapman8a1631d2013-07-02 20:28:59 +01001800 if (tunnel->version == L2TP_HDR_VER_2)
1801 session->nr_max = 0xffff;
1802 else
1803 session->nr_max = 0xffffff;
1804 session->nr_window_size = session->nr_max / 2;
James Chapmana0dbd822013-07-02 20:29:00 +01001805 session->nr_oos_count_max = 4;
1806
1807 /* Use NR of first received packet */
1808 session->reorder_skip = 1;
James Chapmanfd558d12010-04-02 06:18:33 +00001809
1810 sprintf(&session->name[0], "sess %u/%u",
1811 tunnel->tunnel_id, session->session_id);
1812
1813 skb_queue_head_init(&session->reorder_q);
1814
1815 INIT_HLIST_NODE(&session->hlist);
James Chapmanf7faffa2010-04-02 06:18:49 +00001816 INIT_HLIST_NODE(&session->global_hlist);
James Chapmanfd558d12010-04-02 06:18:33 +00001817
1818 /* Inherit debug options from tunnel */
1819 session->debug = tunnel->debug;
1820
1821 if (cfg) {
James Chapmanf7faffa2010-04-02 06:18:49 +00001822 session->pwtype = cfg->pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +00001823 session->debug = cfg->debug;
James Chapmanfd558d12010-04-02 06:18:33 +00001824 session->mtu = cfg->mtu;
1825 session->mru = cfg->mru;
1826 session->send_seq = cfg->send_seq;
1827 session->recv_seq = cfg->recv_seq;
1828 session->lns_mode = cfg->lns_mode;
James Chapmanf7faffa2010-04-02 06:18:49 +00001829 session->reorder_timeout = cfg->reorder_timeout;
1830 session->offset = cfg->offset;
1831 session->l2specific_type = cfg->l2specific_type;
1832 session->l2specific_len = cfg->l2specific_len;
1833 session->cookie_len = cfg->cookie_len;
1834 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1835 session->peer_cookie_len = cfg->peer_cookie_len;
1836 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
James Chapmanfd558d12010-04-02 06:18:33 +00001837 }
1838
James Chapmanf7faffa2010-04-02 06:18:49 +00001839 if (tunnel->version == L2TP_HDR_VER_2)
1840 session->build_header = l2tp_build_l2tpv2_header;
1841 else
1842 session->build_header = l2tp_build_l2tpv3_header;
1843
1844 l2tp_session_set_header_len(session, tunnel->version);
1845
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001846 err = l2tp_session_add_to_tunnel(tunnel, session);
1847 if (err) {
1848 kfree(session);
1849
1850 return ERR_PTR(err);
1851 }
1852
James Chapmanfd558d12010-04-02 06:18:33 +00001853 /* Bump the reference count. The session context is deleted
1854 * only when this drops to zero.
1855 */
1856 l2tp_session_inc_refcount(session);
1857 l2tp_tunnel_inc_refcount(tunnel);
1858
1859 /* Ensure tunnel socket isn't deleted */
1860 sock_hold(tunnel->sock);
1861
James Chapmanfd558d12010-04-02 06:18:33 +00001862 /* Ignore management session in session count value */
1863 if (session->session_id != 0)
1864 atomic_inc(&l2tp_session_count);
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001865
1866 return session;
James Chapmanfd558d12010-04-02 06:18:33 +00001867 }
1868
Guillaume Naultdbdbc732017-03-31 13:02:27 +02001869 return ERR_PTR(-ENOMEM);
James Chapmanfd558d12010-04-02 06:18:33 +00001870}
1871EXPORT_SYMBOL_GPL(l2tp_session_create);
1872
1873/*****************************************************************************
1874 * Init and cleanup
1875 *****************************************************************************/
1876
1877static __net_init int l2tp_init_net(struct net *net)
1878{
Jiri Pirkoe773aaf2010-04-23 00:53:39 +00001879 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
James Chapmanf7faffa2010-04-02 06:18:49 +00001880 int hash;
James Chapmanfd558d12010-04-02 06:18:33 +00001881
James Chapmanfd558d12010-04-02 06:18:33 +00001882 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
James Chapmane02d4942010-04-02 06:19:16 +00001883 spin_lock_init(&pn->l2tp_tunnel_list_lock);
James Chapmanfd558d12010-04-02 06:18:33 +00001884
James Chapmanf7faffa2010-04-02 06:18:49 +00001885 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1886 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1887
James Chapmane02d4942010-04-02 06:19:16 +00001888 spin_lock_init(&pn->l2tp_session_hlist_lock);
James Chapmanf7faffa2010-04-02 06:18:49 +00001889
James Chapmanfd558d12010-04-02 06:18:33 +00001890 return 0;
James Chapmanfd558d12010-04-02 06:18:33 +00001891}
1892
Tom Parkin167eb172013-01-31 23:43:03 +00001893static __net_exit void l2tp_exit_net(struct net *net)
1894{
1895 struct l2tp_net *pn = l2tp_pernet(net);
1896 struct l2tp_tunnel *tunnel = NULL;
1897
1898 rcu_read_lock_bh();
1899 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1900 (void)l2tp_tunnel_delete(tunnel);
1901 }
1902 rcu_read_unlock_bh();
Sabrina Dubroca2f869532016-09-02 10:22:54 +02001903
1904 flush_workqueue(l2tp_wq);
1905 rcu_barrier();
Tom Parkin167eb172013-01-31 23:43:03 +00001906}
1907
James Chapmanfd558d12010-04-02 06:18:33 +00001908static struct pernet_operations l2tp_net_ops = {
1909 .init = l2tp_init_net,
Tom Parkin167eb172013-01-31 23:43:03 +00001910 .exit = l2tp_exit_net,
James Chapmanfd558d12010-04-02 06:18:33 +00001911 .id = &l2tp_net_id,
1912 .size = sizeof(struct l2tp_net),
1913};
1914
1915static int __init l2tp_init(void)
1916{
1917 int rc = 0;
1918
1919 rc = register_pernet_device(&l2tp_net_ops);
1920 if (rc)
1921 goto out;
1922
ZhangZhen59ff3eb2014-03-27 09:41:47 +08001923 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001924 if (!l2tp_wq) {
1925 pr_err("alloc_workqueue failed\n");
WANG Cong67e04c22015-04-03 13:46:09 -07001926 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001927 rc = -ENOMEM;
1928 goto out;
1929 }
1930
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001931 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
James Chapmanfd558d12010-04-02 06:18:33 +00001932
1933out:
1934 return rc;
1935}
1936
1937static void __exit l2tp_exit(void)
1938{
1939 unregister_pernet_device(&l2tp_net_ops);
Tom Parkinf8ccac02013-01-31 23:43:00 +00001940 if (l2tp_wq) {
1941 destroy_workqueue(l2tp_wq);
1942 l2tp_wq = NULL;
1943 }
James Chapmanfd558d12010-04-02 06:18:33 +00001944}
1945
1946module_init(l2tp_init);
1947module_exit(l2tp_exit);
1948
1949MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1950MODULE_DESCRIPTION("L2TP core");
1951MODULE_LICENSE("GPL");
1952MODULE_VERSION(L2TP_DRV_VERSION);
1953