l2tp: Support several sockets with same IP/port quadruple
[linux-block.git] / net / l2tp / l2tp_core.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
20dcb110 2/* L2TP core.
fd558d18
JC
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)
fd558d18
JC
15 */
16
a4ca44fa
JP
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
fd558d18
JC
19#include <linux/module.h>
20#include <linux/string.h>
21#include <linux/list.h>
e02d494d 22#include <linux/rculist.h>
fd558d18
JC
23#include <linux/uaccess.h>
24
25#include <linux/kernel.h>
26#include <linux/spinlock.h>
27#include <linux/kthread.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
30#include <linux/errno.h>
31#include <linux/jiffies.h>
32
33#include <linux/netdevice.h>
34#include <linux/net.h>
35#include <linux/inetdevice.h>
36#include <linux/skbuff.h>
37#include <linux/init.h>
0d76751f 38#include <linux/in.h>
fd558d18
JC
39#include <linux/ip.h>
40#include <linux/udp.h>
0d76751f 41#include <linux/l2tp.h>
fd558d18
JC
42#include <linux/hash.h>
43#include <linux/sort.h>
44#include <linux/file.h>
45#include <linux/nsproxy.h>
46#include <net/net_namespace.h>
47#include <net/netns/generic.h>
48#include <net/dst.h>
49#include <net/ip.h>
50#include <net/udp.h>
85644b4d 51#include <net/udp_tunnel.h>
309795f4 52#include <net/inet_common.h>
fd558d18 53#include <net/xfrm.h>
0d76751f 54#include <net/protocol.h>
d2cf3361
BL
55#include <net/inet6_connection_sock.h>
56#include <net/inet_ecn.h>
57#include <net/ip6_route.h>
d499bd2e 58#include <net/ip6_checksum.h>
fd558d18
JC
59
60#include <asm/byteorder.h>
60063497 61#include <linux/atomic.h>
fd558d18
JC
62
63#include "l2tp_core.h"
6b7bdcd7 64#include "trace.h"
fd558d18 65
3f117d6f
TP
66#define CREATE_TRACE_POINTS
67#include "trace.h"
68
fd558d18
JC
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
f7faffa3 80#define L2TP_HDR_VER_3 0x0003
fd558d18
JC
81
82/* L2TPv3 default L2-specific sublayer */
83#define L2TP_SLFLAG_S 0x40000000
84#define L2TP_SL_SEQ_MASK 0x00ffffff
85
91c52470 86#define L2TP_HDR_SIZE_MAX 14
fd558d18
JC
87
88/* Default trace flags */
89#define L2TP_DEFAULT_DEBUG_FLAGS 0
90
fd558d18
JC
91/* Private data stored for received packets in the skb.
92 */
93struct l2tp_skb_cb {
f7faffa3 94 u32 ns;
fd558d18
JC
95 u16 has_seq;
96 u16 length;
97 unsigned long expires;
98};
99
efcd8c85 100#define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *)&(skb)->cb[sizeof(struct inet_skb_parm)])
fd558d18 101
f8ccac0e 102static struct workqueue_struct *l2tp_wq;
fd558d18
JC
103
104/* per-net private data for this module */
105static unsigned int l2tp_net_id;
106struct l2tp_net {
c4d48a58
CW
107 /* Lock for write access to l2tp_tunnel_idr */
108 spinlock_t l2tp_tunnel_idr_lock;
109 struct idr l2tp_tunnel_idr;
f7faffa3 110 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
20dcb110 111 /* Lock for write access to l2tp_session_hlist */
e02d494d 112 spinlock_t l2tp_session_hlist_lock;
fd558d18
JC
113};
114
b954f940
PA
115#if IS_ENABLED(CONFIG_IPV6)
116static bool l2tp_sk_is_v6(struct sock *sk)
117{
118 return sk->sk_family == PF_INET6 &&
119 !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
120}
121#endif
fc130840 122
9aaef50c 123static inline struct l2tp_net *l2tp_pernet(const struct net *net)
fd558d18 124{
fd558d18
JC
125 return net_generic(net, l2tp_net_id);
126}
127
f7faffa3
JC
128/* Session hash global list for L2TPv3.
129 * The session_id SHOULD be random according to RFC3931, but several
130 * L2TP implementations use incrementing session_ids. So we do a real
131 * hash on the session_id, rather than a simple bitmask.
132 */
133static inline struct hlist_head *
134l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
135{
136 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
f7faffa3
JC
137}
138
fd558d18
JC
139/* Session hash list.
140 * The session_id SHOULD be random according to RFC2661, but several
141 * L2TP implementations (Cisco and Microsoft) use incrementing
142 * session_ids. So we do a real hash on the session_id, rather than a
143 * simple bitmask.
144 */
145static inline struct hlist_head *
146l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
147{
148 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
149}
150
52016e25 151static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
d00fa9ad 152{
6b7bdcd7 153 trace_free_tunnel(tunnel);
d00fa9ad
JC
154 sock_put(tunnel->sock);
155 /* the tunnel is freed in the socket destructor */
156}
52016e25
TP
157
158static void l2tp_session_free(struct l2tp_session *session)
159{
6b7bdcd7 160 trace_free_session(session);
45faeff1
TP
161 if (session->tunnel)
162 l2tp_tunnel_dec_refcount(session->tunnel);
163 kfree(session);
164}
6b7bdcd7 165
45faeff1
TP
166struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk)
167{
168 struct l2tp_tunnel *tunnel = sk->sk_user_data;
169
170 if (tunnel)
52016e25 171 if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
45faeff1 172 return NULL;
52016e25 173
45faeff1 174 return tunnel;
52016e25 175}
45faeff1 176EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel);
52016e25
TP
177
178void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
179{
180 refcount_inc(&tunnel->ref_count);
181}
182EXPORT_SYMBOL_GPL(l2tp_tunnel_inc_refcount);
183
184void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
185{
186 if (refcount_dec_and_test(&tunnel->ref_count))
187 l2tp_tunnel_free(tunnel);
188}
189EXPORT_SYMBOL_GPL(l2tp_tunnel_dec_refcount);
190
191void l2tp_session_inc_refcount(struct l2tp_session *session)
192{
193 refcount_inc(&session->ref_count);
194}
195EXPORT_SYMBOL_GPL(l2tp_session_inc_refcount);
196
197void l2tp_session_dec_refcount(struct l2tp_session *session)
198{
199 if (refcount_dec_and_test(&session->ref_count))
200 l2tp_session_free(session);
201}
202EXPORT_SYMBOL_GPL(l2tp_session_dec_refcount);
d00fa9ad 203
54652eb1
GN
204/* Lookup a tunnel. A new reference is held on the returned tunnel. */
205struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
206{
207 const struct l2tp_net *pn = l2tp_pernet(net);
208 struct l2tp_tunnel *tunnel;
209
210 rcu_read_lock_bh();
c4d48a58
CW
211 tunnel = idr_find(&pn->l2tp_tunnel_idr, tunnel_id);
212 if (tunnel && refcount_inc_not_zero(&tunnel->ref_count)) {
213 rcu_read_unlock_bh();
214 return tunnel;
54652eb1
GN
215 }
216 rcu_read_unlock_bh();
217
218 return NULL;
219}
220EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
221
5846c131
GN
222struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
223{
c4d48a58
CW
224 struct l2tp_net *pn = l2tp_pernet(net);
225 unsigned long tunnel_id, tmp;
5846c131
GN
226 struct l2tp_tunnel *tunnel;
227 int count = 0;
228
229 rcu_read_lock_bh();
c4d48a58
CW
230 idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
231 if (tunnel && ++count > nth &&
a622b400 232 refcount_inc_not_zero(&tunnel->ref_count)) {
5846c131
GN
233 rcu_read_unlock_bh();
234 return tunnel;
235 }
236 }
237 rcu_read_unlock_bh();
238
239 return NULL;
240}
241EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
242
01e28b92
GN
243struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
244 u32 session_id)
61b9a047
GN
245{
246 struct hlist_head *session_list;
247 struct l2tp_session *session;
248
01e28b92 249 session_list = l2tp_session_id_hash(tunnel, session_id);
61b9a047 250
07b8ca37
TP
251 rcu_read_lock_bh();
252 hlist_for_each_entry_rcu(session, session_list, hlist)
01e28b92
GN
253 if (session->session_id == session_id) {
254 l2tp_session_inc_refcount(session);
07b8ca37 255 rcu_read_unlock_bh();
61b9a047 256
01e28b92 257 return session;
61b9a047 258 }
07b8ca37 259 rcu_read_unlock_bh();
61b9a047 260
01e28b92
GN
261 return NULL;
262}
263EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session);
61b9a047 264
01e28b92
GN
265struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
266{
267 struct hlist_head *session_list;
268 struct l2tp_session *session;
269
270 session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id);
271
272 rcu_read_lock_bh();
273 hlist_for_each_entry_rcu(session, session_list, global_hlist)
61b9a047
GN
274 if (session->session_id == session_id) {
275 l2tp_session_inc_refcount(session);
01e28b92 276 rcu_read_unlock_bh();
61b9a047
GN
277
278 return session;
279 }
01e28b92 280 rcu_read_unlock_bh();
61b9a047
GN
281
282 return NULL;
283}
284EXPORT_SYMBOL_GPL(l2tp_session_get);
285
a4346210 286struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
fd558d18
JC
287{
288 int hash;
fd558d18
JC
289 struct l2tp_session *session;
290 int count = 0;
291
07b8ca37 292 rcu_read_lock_bh();
fd558d18 293 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
07b8ca37 294 hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) {
fd558d18 295 if (++count > nth) {
e08293a4 296 l2tp_session_inc_refcount(session);
07b8ca37 297 rcu_read_unlock_bh();
fd558d18
JC
298 return session;
299 }
300 }
301 }
302
07b8ca37 303 rcu_read_unlock_bh();
fd558d18
JC
304
305 return NULL;
306}
e08293a4 307EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
fd558d18 308
309795f4
JC
309/* Lookup a session by interface name.
310 * This is very inefficient but is only used by management interfaces.
311 */
9aaef50c 312struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
a4346210 313 const char *ifname)
309795f4
JC
314{
315 struct l2tp_net *pn = l2tp_pernet(net);
316 int hash;
309795f4
JC
317 struct l2tp_session *session;
318
e02d494d 319 rcu_read_lock_bh();
309795f4 320 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
b67bfe0d 321 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
309795f4 322 if (!strcmp(session->ifname, ifname)) {
2777e2ab 323 l2tp_session_inc_refcount(session);
e02d494d 324 rcu_read_unlock_bh();
2777e2ab 325
309795f4
JC
326 return session;
327 }
328 }
329 }
330
e02d494d 331 rcu_read_unlock_bh();
309795f4
JC
332
333 return NULL;
334}
2777e2ab 335EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
309795f4 336
3953ae7b
GN
337int l2tp_session_register(struct l2tp_session *session,
338 struct l2tp_tunnel *tunnel)
dbdbc73b
GN
339{
340 struct l2tp_session *session_walk;
341 struct hlist_head *g_head;
342 struct hlist_head *head;
343 struct l2tp_net *pn;
f3c66d4e 344 int err;
dbdbc73b
GN
345
346 head = l2tp_session_id_hash(tunnel, session->session_id);
347
07b8ca37 348 spin_lock_bh(&tunnel->hlist_lock);
f3c66d4e
GN
349 if (!tunnel->acpt_newsess) {
350 err = -ENODEV;
351 goto err_tlock;
352 }
353
dbdbc73b 354 hlist_for_each_entry(session_walk, head, hlist)
f3c66d4e
GN
355 if (session_walk->session_id == session->session_id) {
356 err = -EEXIST;
357 goto err_tlock;
358 }
dbdbc73b
GN
359
360 if (tunnel->version == L2TP_HDR_VER_3) {
361 pn = l2tp_pernet(tunnel->l2tp_net);
363a341d 362 g_head = l2tp_session_id_hash_2(pn, session->session_id);
dbdbc73b
GN
363
364 spin_lock_bh(&pn->l2tp_session_hlist_lock);
f3c66d4e 365
0d0d9a38
RK
366 /* IP encap expects session IDs to be globally unique, while
367 * UDP encap doesn't.
368 */
dbdbc73b 369 hlist_for_each_entry(session_walk, g_head, global_hlist)
0d0d9a38
RK
370 if (session_walk->session_id == session->session_id &&
371 (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP ||
372 tunnel->encap == L2TP_ENCAPTYPE_IP)) {
f3c66d4e
GN
373 err = -EEXIST;
374 goto err_tlock_pnlock;
375 }
dbdbc73b 376
f3c66d4e 377 l2tp_tunnel_inc_refcount(tunnel);
dbdbc73b 378 hlist_add_head_rcu(&session->global_hlist, g_head);
f3c66d4e 379
dbdbc73b 380 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
f3c66d4e
GN
381 } else {
382 l2tp_tunnel_inc_refcount(tunnel);
dbdbc73b
GN
383 }
384
07b8ca37
TP
385 hlist_add_head_rcu(&session->hlist, head);
386 spin_unlock_bh(&tunnel->hlist_lock);
dbdbc73b 387
6b7bdcd7
TP
388 trace_register_session(session);
389
dbdbc73b
GN
390 return 0;
391
f3c66d4e 392err_tlock_pnlock:
dbdbc73b 393 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
f3c66d4e 394err_tlock:
07b8ca37 395 spin_unlock_bh(&tunnel->hlist_lock);
dbdbc73b 396
f3c66d4e 397 return err;
dbdbc73b 398}
3953ae7b 399EXPORT_SYMBOL_GPL(l2tp_session_register);
dbdbc73b 400
fd558d18
JC
401/*****************************************************************************
402 * Receive data handling
403 *****************************************************************************/
404
405/* Queue a skb in order. We come here only if the skb has an L2TP sequence
406 * number.
407 */
408static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
409{
410 struct sk_buff *skbp;
411 struct sk_buff *tmp;
f7faffa3 412 u32 ns = L2TP_SKB_CB(skb)->ns;
fd558d18
JC
413
414 spin_lock_bh(&session->reorder_q.lock);
415 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
416 if (L2TP_SKB_CB(skbp)->ns > ns) {
417 __skb_queue_before(&session->reorder_q, skbp, skb);
7b7c0719 418 atomic_long_inc(&session->stats.rx_oos_packets);
fd558d18
JC
419 goto out;
420 }
421 }
422
423 __skb_queue_tail(&session->reorder_q, skb);
424
425out:
426 spin_unlock_bh(&session->reorder_q.lock);
427}
428
429/* Dequeue a single skb.
430 */
431static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
432{
433 struct l2tp_tunnel *tunnel = session->tunnel;
434 int length = L2TP_SKB_CB(skb)->length;
435
436 /* We're about to requeue the skb, so return resources
437 * to its current owner (a socket receive buffer).
438 */
439 skb_orphan(skb);
440
7b7c0719
TP
441 atomic_long_inc(&tunnel->stats.rx_packets);
442 atomic_long_add(length, &tunnel->stats.rx_bytes);
443 atomic_long_inc(&session->stats.rx_packets);
444 atomic_long_add(length, &session->stats.rx_bytes);
fd558d18
JC
445
446 if (L2TP_SKB_CB(skb)->has_seq) {
447 /* Bump our Nr */
448 session->nr++;
8a1631d5 449 session->nr &= session->nr_max;
6b7bdcd7 450 trace_session_seqnum_update(session);
fd558d18
JC
451 }
452
453 /* call private receive handler */
0febc7b3 454 if (session->recv_skb)
fd558d18
JC
455 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
456 else
457 kfree_skb(skb);
fd558d18
JC
458}
459
460/* Dequeue skbs from the session's reorder_q, subject to packet order.
461 * Skbs that have been in the queue for too long are simply discarded.
462 */
463static void l2tp_recv_dequeue(struct l2tp_session *session)
464{
465 struct sk_buff *skb;
466 struct sk_buff *tmp;
467
468 /* If the pkt at the head of the queue has the nr that we
469 * expect to send up next, dequeue it and any other
470 * in-sequence packets behind it.
471 */
e2e210c0 472start:
fd558d18
JC
473 spin_lock_bh(&session->reorder_q.lock);
474 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
6b7bdcd7
TP
475 struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
476
477 /* If the packet has been pending on the queue for too long, discard it */
478 if (time_after(jiffies, cb->expires)) {
7b7c0719
TP
479 atomic_long_inc(&session->stats.rx_seq_discards);
480 atomic_long_inc(&session->stats.rx_errors);
6b7bdcd7 481 trace_session_pkt_expired(session, cb->ns);
38d40b3f 482 session->reorder_skip = 1;
fd558d18
JC
483 __skb_unlink(skb, &session->reorder_q);
484 kfree_skb(skb);
fd558d18
JC
485 continue;
486 }
487
6b7bdcd7 488 if (cb->has_seq) {
38d40b3f 489 if (session->reorder_skip) {
38d40b3f 490 session->reorder_skip = 0;
6b7bdcd7
TP
491 session->nr = cb->ns;
492 trace_session_seqnum_reset(session);
38d40b3f 493 }
6b7bdcd7 494 if (cb->ns != session->nr)
fd558d18 495 goto out;
fd558d18
JC
496 }
497 __skb_unlink(skb, &session->reorder_q);
498
499 /* Process the skb. We release the queue lock while we
500 * do so to let other contexts process the queue.
501 */
502 spin_unlock_bh(&session->reorder_q.lock);
503 l2tp_recv_dequeue_skb(session, skb);
e2e210c0 504 goto start;
fd558d18
JC
505 }
506
507out:
508 spin_unlock_bh(&session->reorder_q.lock);
509}
510
8a1631d5
JC
511static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
512{
513 u32 nws;
514
515 if (nr >= session->nr)
516 nws = nr - session->nr;
517 else
518 nws = (session->nr_max + 1) - (session->nr - nr);
519
520 return nws < session->nr_window_size;
521}
522
b6dc01a4
JC
523/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
524 * acceptable, else non-zero.
525 */
526static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
527{
6b7bdcd7
TP
528 struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
529
530 if (!l2tp_seq_check_rx_window(session, cb->ns)) {
8a1631d5
JC
531 /* Packet sequence number is outside allowed window.
532 * Discard it.
533 */
6b7bdcd7 534 trace_session_pkt_outside_rx_window(session, cb->ns);
8a1631d5
JC
535 goto discard;
536 }
537
b6dc01a4
JC
538 if (session->reorder_timeout != 0) {
539 /* Packet reordering enabled. Add skb to session's
540 * reorder queue, in order of ns.
541 */
542 l2tp_recv_queue_skb(session, skb);
a0dbd822
JC
543 goto out;
544 }
545
546 /* Packet reordering disabled. Discard out-of-sequence packets, while
547 * tracking the number if in-sequence packets after the first OOS packet
548 * is seen. After nr_oos_count_max in-sequence packets, reset the
549 * sequence number to re-enable packet reception.
550 */
6b7bdcd7 551 if (cb->ns == session->nr) {
a0dbd822 552 skb_queue_tail(&session->reorder_q, skb);
b6dc01a4 553 } else {
6b7bdcd7 554 u32 nr_oos = cb->ns;
a0dbd822
JC
555 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
556
557 if (nr_oos == nr_next)
558 session->nr_oos_count++;
559 else
560 session->nr_oos_count = 0;
561
562 session->nr_oos = nr_oos;
563 if (session->nr_oos_count > session->nr_oos_count_max) {
564 session->reorder_skip = 1;
a0dbd822
JC
565 }
566 if (!session->reorder_skip) {
b6dc01a4 567 atomic_long_inc(&session->stats.rx_seq_discards);
6b7bdcd7 568 trace_session_pkt_oos(session, cb->ns);
b6dc01a4
JC
569 goto discard;
570 }
571 skb_queue_tail(&session->reorder_q, skb);
572 }
573
a0dbd822 574out:
b6dc01a4
JC
575 return 0;
576
577discard:
578 return 1;
579}
580
f7faffa3
JC
581/* Do receive processing of L2TP data frames. We handle both L2TPv2
582 * and L2TPv3 data frames here.
583 *
584 * L2TPv2 Data Message Header
585 *
586 * 0 1 2 3
587 * 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
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
591 * | Tunnel ID | Session ID |
592 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
593 * | Ns (opt) | Nr (opt) |
594 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
595 * | Offset Size (opt) | Offset pad... (opt)
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 *
598 * Data frames are marked by T=0. All other fields are the same as
599 * those in L2TP control frames.
600 *
601 * L2TPv3 Data Message Header
602 *
603 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
604 * | L2TP Session Header |
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * | L2-Specific Sublayer |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * | Tunnel Payload ...
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 *
611 * L2TPv3 Session Header Over IP
612 *
613 * 0 1 2 3
614 * 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
615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
616 * | Session ID |
617 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
618 * | Cookie (optional, maximum 64 bits)...
619 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
620 * |
621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
622 *
623 * L2TPv3 L2-Specific Sublayer Format
624 *
625 * 0 1 2 3
626 * 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
627 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628 * |x|S|x|x|x|x|x|x| Sequence Number |
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630 *
23fe846f
GN
631 * Cookie value and sublayer format are negotiated with the peer when
632 * the session is set up. Unlike L2TPv2, we do not need to parse the
633 * packet header to determine if optional fields are present.
f7faffa3
JC
634 *
635 * Caller must already have parsed the frame and determined that it is
636 * a data (not control) frame before coming here. Fields up to the
637 * session-id have already been parsed and ptr points to the data
638 * after the session-id.
fd558d18 639 */
f7faffa3
JC
640void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
641 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
2b139e6b 642 int length)
fd558d18 643{
f7faffa3 644 struct l2tp_tunnel *tunnel = session->tunnel;
fd558d18 645 int offset;
fd558d18 646
f7faffa3
JC
647 /* Parse and check optional cookie */
648 if (session->peer_cookie_len > 0) {
649 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
3e59e885
MS
650 pr_debug_ratelimited("%s: cookie mismatch (%u/%u). Discarding.\n",
651 tunnel->name, tunnel->tunnel_id,
652 session->session_id);
7b7c0719 653 atomic_long_inc(&session->stats.rx_cookie_discards);
f7faffa3
JC
654 goto discard;
655 }
656 ptr += session->peer_cookie_len;
657 }
658
fd558d18
JC
659 /* Handle the optional sequence numbers. Sequence numbers are
660 * in different places for L2TPv2 and L2TPv3.
661 *
662 * If we are the LAC, enable/disable sequence numbers under
663 * the control of the LNS. If no sequence numbers present but
664 * we were expecting them, discard frame.
665 */
fd558d18 666 L2TP_SKB_CB(skb)->has_seq = 0;
f7faffa3
JC
667 if (tunnel->version == L2TP_HDR_VER_2) {
668 if (hdrflags & L2TP_HDRFLAG_S) {
f7faffa3 669 /* Store L2TP info in the skb */
12923365 670 L2TP_SKB_CB(skb)->ns = ntohs(*(__be16 *)ptr);
f7faffa3 671 L2TP_SKB_CB(skb)->has_seq = 1;
12923365
TP
672 ptr += 2;
673 /* Skip past nr in the header */
674 ptr += 2;
fd558d18 675
f7faffa3
JC
676 }
677 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
b71a61cc 678 u32 l2h = ntohl(*(__be32 *)ptr);
f7faffa3
JC
679
680 if (l2h & 0x40000000) {
f7faffa3 681 /* Store L2TP info in the skb */
12923365 682 L2TP_SKB_CB(skb)->ns = l2h & 0x00ffffff;
f7faffa3 683 L2TP_SKB_CB(skb)->has_seq = 1;
f7faffa3 684 }
62e7b6a5 685 ptr += 4;
fd558d18
JC
686 }
687
688 if (L2TP_SKB_CB(skb)->has_seq) {
20dcb110 689 /* Received a packet with sequence numbers. If we're the LAC,
fd558d18
JC
690 * check if we sre sending sequence numbers and if not,
691 * configure it so.
692 */
6c0ec37b 693 if (!session->lns_mode && !session->send_seq) {
6b7bdcd7 694 trace_session_seqnum_lns_enable(session);
3f9b9770 695 session->send_seq = 1;
f7faffa3 696 l2tp_session_set_header_len(session, tunnel->version);
fd558d18
JC
697 }
698 } else {
699 /* No sequence numbers.
700 * If user has configured mandatory sequence numbers, discard.
701 */
702 if (session->recv_seq) {
3e59e885
MS
703 pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
704 session->name);
7b7c0719 705 atomic_long_inc(&session->stats.rx_seq_discards);
fd558d18
JC
706 goto discard;
707 }
708
709 /* If we're the LAC and we're sending sequence numbers, the
710 * LNS has requested that we no longer send sequence numbers.
711 * If we're the LNS and we're sending sequence numbers, the
712 * LAC is broken. Discard the frame.
713 */
6c0ec37b 714 if (!session->lns_mode && session->send_seq) {
6b7bdcd7 715 trace_session_seqnum_lns_disable(session);
fd558d18 716 session->send_seq = 0;
f7faffa3 717 l2tp_session_set_header_len(session, tunnel->version);
fd558d18 718 } else if (session->send_seq) {
3e59e885
MS
719 pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
720 session->name);
7b7c0719 721 atomic_long_inc(&session->stats.rx_seq_discards);
fd558d18
JC
722 goto discard;
723 }
724 }
725
900631ee
JC
726 /* Session data offset is defined only for L2TPv2 and is
727 * indicated by an optional 16-bit value in the header.
f7faffa3
JC
728 */
729 if (tunnel->version == L2TP_HDR_VER_2) {
730 /* If offset bit set, skip it. */
731 if (hdrflags & L2TP_HDRFLAG_O) {
732 offset = ntohs(*(__be16 *)ptr);
733 ptr += 2 + offset;
734 }
900631ee 735 }
fd558d18
JC
736
737 offset = ptr - optr;
738 if (!pskb_may_pull(skb, offset))
739 goto discard;
740
741 __skb_pull(skb, offset);
742
fd558d18
JC
743 /* Prepare skb for adding to the session's reorder_q. Hold
744 * packets for max reorder_timeout or 1 second if not
745 * reordering.
746 */
747 L2TP_SKB_CB(skb)->length = length;
748 L2TP_SKB_CB(skb)->expires = jiffies +
749 (session->reorder_timeout ? session->reorder_timeout : HZ);
750
751 /* Add packet to the session's receive queue. Reordering is done here, if
752 * enabled. Saved L2TP protocol info is stored in skb->sb[].
753 */
754 if (L2TP_SKB_CB(skb)->has_seq) {
b6dc01a4
JC
755 if (l2tp_recv_data_seq(session, skb))
756 goto discard;
fd558d18
JC
757 } else {
758 /* No sequence numbers. Add the skb to the tail of the
759 * reorder queue. This ensures that it will be
760 * delivered after all previous sequenced skbs.
761 */
762 skb_queue_tail(&session->reorder_q, skb);
763 }
764
765 /* Try to dequeue as many skbs from reorder_q as we can. */
766 l2tp_recv_dequeue(session);
767
f7faffa3 768 return;
fd558d18
JC
769
770discard:
7b7c0719 771 atomic_long_inc(&session->stats.rx_errors);
fd558d18 772 kfree_skb(skb);
f7faffa3 773}
ca7885db 774EXPORT_SYMBOL_GPL(l2tp_recv_common);
f7faffa3 775
48f72f92
TP
776/* Drop skbs from the session's reorder_q
777 */
493048f5 778static void l2tp_session_queue_purge(struct l2tp_session *session)
48f72f92
TP
779{
780 struct sk_buff *skb = NULL;
b71a61cc 781
48f72f92
TP
782 while ((skb = skb_dequeue(&session->reorder_q))) {
783 atomic_long_inc(&session->stats.rx_errors);
784 kfree_skb(skb);
48f72f92 785 }
48f72f92 786}
48f72f92 787
f7faffa3
JC
788/* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
789 * here. The skb is not on a list when we get here.
790 * Returns 0 if the packet was a data packet and was successfully passed on.
791 * Returns 1 if the packet was not a good data packet and could not be
792 * forwarded. All such packets are passed up to userspace to deal with.
793 */
2b139e6b 794static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
f7faffa3
JC
795{
796 struct l2tp_session *session = NULL;
628bc3e5 797 struct l2tp_tunnel *orig_tunnel = tunnel;
f7faffa3
JC
798 unsigned char *ptr, *optr;
799 u16 hdrflags;
800 u32 tunnel_id, session_id;
f7faffa3
JC
801 u16 version;
802 int length;
803
aa785f93 804 /* UDP has verified checksum */
f7faffa3
JC
805
806 /* UDP always verifies the packet length. */
807 __skb_pull(skb, sizeof(struct udphdr));
808
809 /* Short packet? */
91c52470 810 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) {
3e59e885
MS
811 pr_debug_ratelimited("%s: recv short packet (len=%d)\n",
812 tunnel->name, skb->len);
813 goto invalid;
f7faffa3
JC
814 }
815
e50e705c 816 /* Point to L2TP header */
95075150
TP
817 optr = skb->data;
818 ptr = skb->data;
e50e705c 819
f7faffa3 820 /* Get L2TP header flags */
b71a61cc 821 hdrflags = ntohs(*(__be16 *)ptr);
f7faffa3
JC
822
823 /* Check protocol version */
824 version = hdrflags & L2TP_HDR_VER_MASK;
825 if (version != tunnel->version) {
3e59e885
MS
826 pr_debug_ratelimited("%s: recv protocol version mismatch: got %d expected %d\n",
827 tunnel->name, version, tunnel->version);
828 goto invalid;
f7faffa3
JC
829 }
830
831 /* Get length of L2TP packet */
832 length = skb->len;
833
834 /* If type is control packet, it is handled by userspace. */
12923365 835 if (hdrflags & L2TP_HDRFLAG_T)
3e59e885 836 goto pass;
f7faffa3
JC
837
838 /* Skip flags */
839 ptr += 2;
840
841 if (tunnel->version == L2TP_HDR_VER_2) {
842 /* If length is present, skip it */
843 if (hdrflags & L2TP_HDRFLAG_L)
844 ptr += 2;
845
846 /* Extract tunnel and session ID */
b71a61cc 847 tunnel_id = ntohs(*(__be16 *)ptr);
f7faffa3 848 ptr += 2;
628bc3e5
ST
849
850 if (tunnel_id != tunnel->tunnel_id) {
851 /* We are receiving trafic for another tunnel, probably
852 * because we have several tunnels between the same
853 * IP/port quadruple, look it up.
854 */
855 struct l2tp_tunnel *alt_tunnel;
856
857 alt_tunnel = l2tp_tunnel_get(tunnel->l2tp_net, tunnel_id);
858 if (!alt_tunnel || alt_tunnel->version != L2TP_HDR_VER_2)
859 goto pass;
860 tunnel = alt_tunnel;
861 }
862
b71a61cc 863 session_id = ntohs(*(__be16 *)ptr);
f7faffa3
JC
864 ptr += 2;
865 } else {
866 ptr += 2; /* skip reserved bits */
867 tunnel_id = tunnel->tunnel_id;
b71a61cc 868 session_id = ntohl(*(__be32 *)ptr);
f7faffa3
JC
869 ptr += 4;
870 }
871
872 /* Find the session context */
01e28b92 873 session = l2tp_tunnel_get_session(tunnel, session_id);
309795f4 874 if (!session || !session->recv_skb) {
a4346210 875 if (session)
61b9a047 876 l2tp_session_dec_refcount(session);
61b9a047 877
f7faffa3 878 /* Not found? Pass to userspace to deal with */
3e59e885
MS
879 pr_debug_ratelimited("%s: no session found (%u/%u). Passing up.\n",
880 tunnel->name, tunnel_id, session_id);
881 goto pass;
f7faffa3
JC
882 }
883
4522a70d 884 if (tunnel->version == L2TP_HDR_VER_3 &&
9b6ff7eb
XY
885 l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
886 l2tp_session_dec_refcount(session);
3e59e885 887 goto invalid;
9b6ff7eb 888 }
4522a70d 889
2b139e6b 890 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
61b9a047 891 l2tp_session_dec_refcount(session);
fd558d18 892
628bc3e5
ST
893 if (tunnel != orig_tunnel)
894 l2tp_tunnel_dec_refcount(tunnel);
895
fd558d18
JC
896 return 0;
897
3e59e885
MS
898invalid:
899 atomic_long_inc(&tunnel->stats.rx_invalid);
900
901pass:
fd558d18
JC
902 /* Put UDP header back */
903 __skb_push(skb, sizeof(struct udphdr));
904
628bc3e5
ST
905 if (tunnel != orig_tunnel)
906 l2tp_tunnel_dec_refcount(tunnel);
907
fd558d18
JC
908 return 1;
909}
fd558d18
JC
910
911/* UDP encapsulation receive handler. See net/ipv4/udp.c.
912 * Return codes:
913 * 0 : success.
914 * <0: error
915 * >0: skb should be passed up to userspace as UDP.
916 */
917int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
918{
919 struct l2tp_tunnel *tunnel;
920
45faeff1
TP
921 /* Note that this is called from the encap_rcv hook inside an
922 * RCU-protected region, but without the socket being locked.
923 * Hence we use rcu_dereference_sk_user_data to access the
924 * tunnel data structure rather the usual l2tp_sk_to_tunnel
925 * accessor function.
926 */
c1c47721 927 tunnel = rcu_dereference_sk_user_data(sk);
0febc7b3 928 if (!tunnel)
fd558d18 929 goto pass_up;
45faeff1
TP
930 if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
931 goto pass_up;
fd558d18 932
2b139e6b 933 if (l2tp_udp_recv_core(tunnel, skb))
d00fa9ad 934 goto pass_up;
fd558d18 935
fd558d18
JC
936 return 0;
937
fd558d18
JC
938pass_up:
939 return 1;
940}
941EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
942
943/************************************************************************
944 * Transmit handling
945 ***********************************************************************/
946
947/* Build an L2TP header for the session into the buffer provided.
948 */
f7faffa3 949static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
fd558d18 950{
f7faffa3 951 struct l2tp_tunnel *tunnel = session->tunnel;
fd558d18 952 __be16 *bufp = buf;
f7faffa3 953 __be16 *optr = buf;
fd558d18
JC
954 u16 flags = L2TP_HDR_VER_2;
955 u32 tunnel_id = tunnel->peer_tunnel_id;
956 u32 session_id = session->peer_session_id;
957
958 if (session->send_seq)
959 flags |= L2TP_HDRFLAG_S;
960
961 /* Setup L2TP header. */
962 *bufp++ = htons(flags);
963 *bufp++ = htons(tunnel_id);
964 *bufp++ = htons(session_id);
965 if (session->send_seq) {
966 *bufp++ = htons(session->ns);
967 *bufp++ = 0;
968 session->ns++;
f7faffa3 969 session->ns &= 0xffff;
6b7bdcd7 970 trace_session_seqnum_update(session);
fd558d18 971 }
f7faffa3
JC
972
973 return bufp - optr;
fd558d18
JC
974}
975
f7faffa3 976static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
fd558d18 977{
0d76751f 978 struct l2tp_tunnel *tunnel = session->tunnel;
f7faffa3
JC
979 char *bufp = buf;
980 char *optr = bufp;
f7faffa3 981
0d76751f
JC
982 /* Setup L2TP header. The header differs slightly for UDP and
983 * IP encapsulations. For UDP, there is 4 bytes of flags.
984 */
985 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
986 u16 flags = L2TP_HDR_VER_3;
b71a61cc 987 *((__be16 *)bufp) = htons(flags);
0d76751f 988 bufp += 2;
b71a61cc 989 *((__be16 *)bufp) = 0;
0d76751f
JC
990 bufp += 2;
991 }
992
b71a61cc 993 *((__be32 *)bufp) = htonl(session->peer_session_id);
f7faffa3
JC
994 bufp += 4;
995 if (session->cookie_len) {
996 memcpy(bufp, &session->cookie[0], session->cookie_len);
997 bufp += session->cookie_len;
998 }
62e7b6a5
LB
999 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1000 u32 l2h = 0;
f7faffa3 1001
62e7b6a5
LB
1002 if (session->send_seq) {
1003 l2h = 0x40000000 | session->ns;
1004 session->ns++;
1005 session->ns &= 0xffffff;
6b7bdcd7 1006 trace_session_seqnum_update(session);
f7faffa3 1007 }
62e7b6a5
LB
1008
1009 *((__be32 *)bufp) = htonl(l2h);
1010 bufp += 4;
f7faffa3 1011 }
fd558d18 1012
f7faffa3 1013 return bufp - optr;
fd558d18 1014}
fd558d18 1015
de68b039
TP
1016/* Queue the packet to IP for output: tunnel socket lock must be held */
1017static int l2tp_xmit_queue(struct l2tp_tunnel *tunnel, struct sk_buff *skb, struct flowi *fl)
fd558d18 1018{
de68b039 1019 int err;
fd558d18 1020
60ff7467 1021 skb->ignore_df = 1;
27d53323 1022 skb_dst_drop(skb);
d2cf3361 1023#if IS_ENABLED(CONFIG_IPV6)
b954f940 1024 if (l2tp_sk_is_v6(tunnel->sock))
de68b039 1025 err = inet6_csk_xmit(tunnel->sock, skb, NULL);
d2cf3361
BL
1026 else
1027#endif
de68b039 1028 err = ip_queue_xmit(tunnel->sock, skb, fl);
fd558d18 1029
de68b039 1030 return err >= 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
fd558d18 1031}
fd558d18 1032
f52e4b27 1033static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, unsigned int *len)
fd558d18 1034{
0d76751f 1035 struct l2tp_tunnel *tunnel = session->tunnel;
de68b039 1036 unsigned int data_len = skb->len;
0d76751f 1037 struct sock *sk = tunnel->sock;
de68b039 1038 int headroom, uhlen, udp_len;
b8c84307 1039 int ret = NET_XMIT_SUCCESS;
de68b039
TP
1040 struct inet_sock *inet;
1041 struct udphdr *uh;
fd558d18
JC
1042
1043 /* Check that there's enough headroom in the skb to insert IP,
1044 * UDP and L2TP headers. If not enough, expand it to
1045 * make room. Adjust truesize.
1046 */
de68b039
TP
1047 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(*uh) : 0;
1048 headroom = NET_SKB_PAD + sizeof(struct iphdr) + uhlen + session->hdr_len;
835acf5d 1049 if (skb_cow_head(skb, headroom)) {
b8c84307
ED
1050 kfree_skb(skb);
1051 return NET_XMIT_DROP;
835acf5d 1052 }
fd558d18 1053
fd558d18 1054 /* Setup L2TP header */
2dedab6f 1055 if (tunnel->version == L2TP_HDR_VER_2)
efe05278 1056 l2tp_build_l2tpv2_header(session, __skb_push(skb, session->hdr_len));
2dedab6f 1057 else
efe05278 1058 l2tp_build_l2tpv3_header(session, __skb_push(skb, session->hdr_len));
fd558d18 1059
0d76751f 1060 /* Reset skb netfilter state */
fd558d18 1061 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
de68b039 1062 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
895b5c9f 1063 nf_reset_ct(skb);
fd558d18 1064
0b2c5972 1065 bh_lock_sock_nested(sk);
6af88da1 1066 if (sock_owned_by_user(sk)) {
b8c84307
ED
1067 kfree_skb(skb);
1068 ret = NET_XMIT_DROP;
6af88da1
DM
1069 goto out_unlock;
1070 }
1071
b954f940
PA
1072 /* The user-space may change the connection status for the user-space
1073 * provided socket at run time: we must check it under the socket lock
1074 */
1075 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1076 kfree_skb(skb);
1077 ret = NET_XMIT_DROP;
1078 goto out_unlock;
1079 }
1080
f52e4b27
TP
1081 /* Report transmitted length before we add encap header, which keeps
1082 * statistics consistent for both UDP and IP encap tx/rx paths.
1083 */
1084 *len = skb->len;
1085
d9d8da80 1086 inet = inet_sk(sk);
0d76751f
JC
1087 switch (tunnel->encap) {
1088 case L2TP_ENCAPTYPE_UDP:
1089 /* Setup UDP header */
0d76751f
JC
1090 __skb_push(skb, sizeof(*uh));
1091 skb_reset_transport_header(skb);
1092 uh = udp_hdr(skb);
1093 uh->source = inet->inet_sport;
1094 uh->dest = inet->inet_dport;
efe05278 1095 udp_len = uhlen + session->hdr_len + data_len;
0d76751f 1096 uh->len = htons(udp_len);
0d76751f
JC
1097
1098 /* Calculate UDP checksum if configured to do so */
d2cf3361 1099#if IS_ENABLED(CONFIG_IPV6)
b954f940 1100 if (l2tp_sk_is_v6(sk))
77157e19
TH
1101 udp6_set_csum(udp_get_no_check6_tx(sk),
1102 skb, &inet6_sk(sk)->saddr,
1103 &sk->sk_v6_daddr, udp_len);
d2cf3361
BL
1104 else
1105#endif
0864e331
TP
1106 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1107 inet->inet_daddr, udp_len);
0d76751f
JC
1108 break;
1109
1110 case L2TP_ENCAPTYPE_IP:
1111 break;
fd558d18
JC
1112 }
1113
de68b039
TP
1114 ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl);
1115
6af88da1
DM
1116out_unlock:
1117 bh_unlock_sock(sk);
fd558d18 1118
b8c84307 1119 return ret;
fd558d18 1120}
de68b039
TP
1121
1122/* If caller requires the skb to have a ppp header, the header must be
1123 * inserted in the skb data before calling this function.
1124 */
1125int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb)
1126{
f52e4b27 1127 unsigned int len = 0;
de68b039
TP
1128 int ret;
1129
f52e4b27 1130 ret = l2tp_xmit_core(session, skb, &len);
de68b039
TP
1131 if (ret == NET_XMIT_SUCCESS) {
1132 atomic_long_inc(&session->tunnel->stats.tx_packets);
1133 atomic_long_add(len, &session->tunnel->stats.tx_bytes);
1134 atomic_long_inc(&session->stats.tx_packets);
1135 atomic_long_add(len, &session->stats.tx_bytes);
1136 } else {
1137 atomic_long_inc(&session->tunnel->stats.tx_errors);
1138 atomic_long_inc(&session->stats.tx_errors);
1139 }
1140 return ret;
1141}
fd558d18
JC
1142EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1143
1144/*****************************************************************************
1145 * Tinnel and session create/destroy.
1146 *****************************************************************************/
1147
1148/* Tunnel socket destruct hook.
1149 * The tunnel context is deleted only when all session sockets have been
1150 * closed.
1151 */
fc130840 1152static void l2tp_tunnel_destruct(struct sock *sk)
fd558d18 1153{
45faeff1 1154 struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
fd558d18 1155
0febc7b3 1156 if (!tunnel)
fd558d18
JC
1157 goto end;
1158
f8ccac0e 1159 /* Disable udp encapsulation */
0d76751f
JC
1160 switch (tunnel->encap) {
1161 case L2TP_ENCAPTYPE_UDP:
1162 /* No longer an encapsulation socket. See net/ipv4/udp.c */
70a36f57
ED
1163 WRITE_ONCE(udp_sk(sk)->encap_type, 0);
1164 udp_sk(sk)->encap_rcv = NULL;
1165 udp_sk(sk)->encap_destroy = NULL;
0d76751f
JC
1166 break;
1167 case L2TP_ENCAPTYPE_IP:
1168 break;
1169 }
fd558d18
JC
1170
1171 /* Remove hooks into tunnel socket */
b68777d5 1172 write_lock_bh(&sk->sk_callback_lock);
fd558d18
JC
1173 sk->sk_destruct = tunnel->old_sk_destruct;
1174 sk->sk_user_data = NULL;
b68777d5 1175 write_unlock_bh(&sk->sk_callback_lock);
fd558d18 1176
f8ccac0e
TP
1177 /* Call the original destructor */
1178 if (sk->sk_destruct)
1179 (*sk->sk_destruct)(sk);
d00fa9ad
JC
1180
1181 kfree_rcu(tunnel, rcu);
fd558d18
JC
1182end:
1183 return;
1184}
fd558d18 1185
b2aecfe8
TP
1186/* Remove an l2tp session from l2tp_core's hash lists. */
1187static void l2tp_session_unhash(struct l2tp_session *session)
1188{
1189 struct l2tp_tunnel *tunnel = session->tunnel;
1190
1191 /* Remove the session from core hashes */
1192 if (tunnel) {
1193 /* Remove from the per-tunnel hash */
07b8ca37
TP
1194 spin_lock_bh(&tunnel->hlist_lock);
1195 hlist_del_init_rcu(&session->hlist);
1196 spin_unlock_bh(&tunnel->hlist_lock);
b2aecfe8
TP
1197
1198 /* For L2TPv3 we have a per-net hash: remove from there, too */
1199 if (tunnel->version != L2TP_HDR_VER_2) {
1200 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1201
1202 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1203 hlist_del_init_rcu(&session->global_hlist);
1204 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
b2aecfe8 1205 }
07b8ca37
TP
1206
1207 synchronize_rcu();
b2aecfe8
TP
1208 }
1209}
1210
fd558d18
JC
1211/* When the tunnel is closed, all the attached sessions need to go too.
1212 */
d08532bb 1213static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
fd558d18 1214{
fd558d18 1215 struct l2tp_session *session;
07b8ca37 1216 int hash;
fd558d18 1217
07b8ca37 1218 spin_lock_bh(&tunnel->hlist_lock);
f3c66d4e 1219 tunnel->acpt_newsess = false;
fd558d18
JC
1220 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1221again:
07b8ca37
TP
1222 hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) {
1223 hlist_del_init_rcu(&session->hlist);
fd558d18 1224
07b8ca37 1225 spin_unlock_bh(&tunnel->hlist_lock);
9d319a8e 1226 l2tp_session_delete(session);
07b8ca37 1227 spin_lock_bh(&tunnel->hlist_lock);
fd558d18
JC
1228
1229 /* Now restart from the beginning of this hash
1230 * chain. We always remove a session from the
1231 * list so we are guaranteed to make forward
1232 * progress.
1233 */
1234 goto again;
1235 }
1236 }
07b8ca37 1237 spin_unlock_bh(&tunnel->hlist_lock);
fd558d18 1238}
fd558d18 1239
9980d001
TP
1240/* Tunnel socket destroy hook for UDP encapsulation */
1241static void l2tp_udp_encap_destroy(struct sock *sk)
1242{
45faeff1 1243 struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
d00fa9ad
JC
1244
1245 if (tunnel)
1246 l2tp_tunnel_delete(tunnel);
9980d001
TP
1247}
1248
c4d48a58
CW
1249static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel)
1250{
1251 struct l2tp_net *pn = l2tp_pernet(net);
1252
1253 spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1254 idr_remove(&pn->l2tp_tunnel_idr, tunnel->tunnel_id);
1255 spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
1256}
1257
f8ccac0e
TP
1258/* Workqueue tunnel deletion function */
1259static void l2tp_tunnel_del_work(struct work_struct *work)
1260{
d00fa9ad
JC
1261 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1262 del_work);
1263 struct sock *sk = tunnel->sock;
1264 struct socket *sock = sk->sk_socket;
12d656af
RK
1265
1266 l2tp_tunnel_closeall(tunnel);
1267
76a6abdb 1268 /* If the tunnel socket was created within the kernel, use
02d13ed5 1269 * the sk API to release it here.
f8ccac0e 1270 */
76a6abdb 1271 if (tunnel->fd < 0) {
26abe143 1272 if (sock) {
02d13ed5 1273 kernel_sock_shutdown(sock, SHUT_RDWR);
26abe143
EB
1274 sock_release(sock);
1275 }
167eb17e 1276 }
f8ccac0e 1277
c4d48a58 1278 l2tp_tunnel_remove(tunnel->l2tp_net, tunnel);
d00fa9ad
JC
1279 /* drop initial ref */
1280 l2tp_tunnel_dec_refcount(tunnel);
1281
1282 /* drop workqueue ref */
06a15f51 1283 l2tp_tunnel_dec_refcount(tunnel);
fd558d18 1284}
fd558d18 1285
789a4a2c
JC
1286/* Create a socket for the tunnel, if one isn't set up by
1287 * userspace. This is used for static tunnels where there is no
1288 * managing L2TP daemon.
167eb17e
TP
1289 *
1290 * Since we don't want these sockets to keep a namespace alive by
1291 * themselves, we drop the socket's namespace refcount after creation.
1292 * These sockets are freed when the namespace exits using the pernet
1293 * exit hook.
789a4a2c 1294 */
167eb17e 1295static int l2tp_tunnel_sock_create(struct net *net,
8ce9825a
TP
1296 u32 tunnel_id,
1297 u32 peer_tunnel_id,
1298 struct l2tp_tunnel_cfg *cfg,
1299 struct socket **sockp)
789a4a2c
JC
1300{
1301 int err = -EINVAL;
167eb17e 1302 struct socket *sock = NULL;
85644b4d 1303 struct udp_port_cfg udp_conf;
789a4a2c
JC
1304
1305 switch (cfg->encap) {
1306 case L2TP_ENCAPTYPE_UDP:
85644b4d
TH
1307 memset(&udp_conf, 0, sizeof(udp_conf));
1308
f9bac8df
CE
1309#if IS_ENABLED(CONFIG_IPV6)
1310 if (cfg->local_ip6 && cfg->peer_ip6) {
85644b4d
TH
1311 udp_conf.family = AF_INET6;
1312 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1313 sizeof(udp_conf.local_ip6));
1314 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1315 sizeof(udp_conf.peer_ip6));
1316 udp_conf.use_udp6_tx_checksums =
b71a61cc 1317 !cfg->udp6_zero_tx_checksums;
85644b4d 1318 udp_conf.use_udp6_rx_checksums =
b71a61cc 1319 !cfg->udp6_zero_rx_checksums;
f9bac8df
CE
1320 } else
1321#endif
1322 {
85644b4d
TH
1323 udp_conf.family = AF_INET;
1324 udp_conf.local_ip = cfg->local_ip;
1325 udp_conf.peer_ip = cfg->peer_ip;
1326 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
f9bac8df 1327 }
789a4a2c 1328
85644b4d
TH
1329 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1330 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1331
1332 err = udp_sock_create(net, &udp_conf, &sock);
1333 if (err < 0)
1334 goto out;
789a4a2c
JC
1335
1336 break;
1337
1338 case L2TP_ENCAPTYPE_IP:
f9bac8df
CE
1339#if IS_ENABLED(CONFIG_IPV6)
1340 if (cfg->local_ip6 && cfg->peer_ip6) {
85644b4d
TH
1341 struct sockaddr_l2tpip6 ip6_addr = {0};
1342
26abe143 1343 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
8ce9825a 1344 IPPROTO_L2TP, &sock);
5dac94e1
JC
1345 if (err < 0)
1346 goto out;
789a4a2c 1347
5dac94e1
JC
1348 ip6_addr.l2tp_family = AF_INET6;
1349 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1350 sizeof(ip6_addr.l2tp_addr));
1351 ip6_addr.l2tp_conn_id = tunnel_id;
b71a61cc 1352 err = kernel_bind(sock, (struct sockaddr *)&ip6_addr,
5dac94e1
JC
1353 sizeof(ip6_addr));
1354 if (err < 0)
1355 goto out;
789a4a2c 1356
5dac94e1
JC
1357 ip6_addr.l2tp_family = AF_INET6;
1358 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1359 sizeof(ip6_addr.l2tp_addr));
1360 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1361 err = kernel_connect(sock,
b71a61cc 1362 (struct sockaddr *)&ip6_addr,
5dac94e1
JC
1363 sizeof(ip6_addr), 0);
1364 if (err < 0)
1365 goto out;
1366 } else
1367#endif
1368 {
85644b4d
TH
1369 struct sockaddr_l2tpip ip_addr = {0};
1370
26abe143 1371 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
8ce9825a 1372 IPPROTO_L2TP, &sock);
5dac94e1
JC
1373 if (err < 0)
1374 goto out;
789a4a2c 1375
5dac94e1
JC
1376 ip_addr.l2tp_family = AF_INET;
1377 ip_addr.l2tp_addr = cfg->local_ip;
1378 ip_addr.l2tp_conn_id = tunnel_id;
b71a61cc 1379 err = kernel_bind(sock, (struct sockaddr *)&ip_addr,
5dac94e1
JC
1380 sizeof(ip_addr));
1381 if (err < 0)
1382 goto out;
1383
1384 ip_addr.l2tp_family = AF_INET;
1385 ip_addr.l2tp_addr = cfg->peer_ip;
1386 ip_addr.l2tp_conn_id = peer_tunnel_id;
b71a61cc 1387 err = kernel_connect(sock, (struct sockaddr *)&ip_addr,
5dac94e1
JC
1388 sizeof(ip_addr), 0);
1389 if (err < 0)
1390 goto out;
1391 }
789a4a2c
JC
1392 break;
1393
1394 default:
1395 goto out;
1396 }
1397
1398out:
167eb17e 1399 *sockp = sock;
6c0ec37b 1400 if (err < 0 && sock) {
167eb17e 1401 kernel_sock_shutdown(sock, SHUT_RDWR);
26abe143 1402 sock_release(sock);
789a4a2c
JC
1403 *sockp = NULL;
1404 }
1405
1406 return err;
1407}
1408
c9ccd4c6 1409int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id,
c0235fb3 1410 struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
fd558d18
JC
1411{
1412 struct l2tp_tunnel *tunnel = NULL;
1413 int err;
0d76751f 1414 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
fd558d18 1415
0febc7b3 1416 if (cfg)
0d76751f
JC
1417 encap = cfg->encap;
1418
70c05bfa 1419 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
0febc7b3 1420 if (!tunnel) {
fd558d18
JC
1421 err = -ENOMEM;
1422 goto err;
1423 }
1424
1425 tunnel->version = version;
1426 tunnel->tunnel_id = tunnel_id;
1427 tunnel->peer_tunnel_id = peer_tunnel_id;
fd558d18
JC
1428
1429 tunnel->magic = L2TP_TUNNEL_MAGIC;
1430 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
07b8ca37 1431 spin_lock_init(&tunnel->hlist_lock);
f3c66d4e 1432 tunnel->acpt_newsess = true;
fd558d18 1433
0d76751f 1434 tunnel->encap = encap;
fd558d18 1435
d00fa9ad 1436 refcount_set(&tunnel->ref_count, 1);
d00fa9ad
JC
1437 tunnel->fd = fd;
1438
f8ccac0e
TP
1439 /* Init delete workqueue struct */
1440 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1441
fd558d18 1442 INIT_LIST_HEAD(&tunnel->list);
fd558d18
JC
1443
1444 err = 0;
1445err:
1446 if (tunnelp)
1447 *tunnelp = tunnel;
1448
fd558d18
JC
1449 return err;
1450}
1451EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1452
6b9f3423
GN
1453static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1454 enum l2tp_encap_type encap)
1455{
1456 if (!net_eq(sock_net(sk), net))
1457 return -EINVAL;
1458
1459 if (sk->sk_type != SOCK_DGRAM)
1460 return -EPROTONOSUPPORT;
1461
d9a81a22
ED
1462 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
1463 return -EPROTONOSUPPORT;
1464
6b9f3423
GN
1465 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1466 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1467 return -EPROTONOSUPPORT;
1468
1469 if (sk->sk_user_data)
1470 return -EBUSY;
1471
1472 return 0;
1473}
1474
1475int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1476 struct l2tp_tunnel_cfg *cfg)
1477{
c4d48a58
CW
1478 struct l2tp_net *pn = l2tp_pernet(net);
1479 u32 tunnel_id = tunnel->tunnel_id;
6b9f3423
GN
1480 struct socket *sock;
1481 struct sock *sk;
1482 int ret;
1483
c4d48a58
CW
1484 spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1485 ret = idr_alloc_u32(&pn->l2tp_tunnel_idr, NULL, &tunnel_id, tunnel_id,
1486 GFP_ATOMIC);
1487 spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
1488 if (ret)
1489 return ret == -ENOSPC ? -EEXIST : ret;
1490
6b9f3423
GN
1491 if (tunnel->fd < 0) {
1492 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1493 tunnel->peer_tunnel_id, cfg,
1494 &sock);
1495 if (ret < 0)
1496 goto err;
1497 } else {
1498 sock = sockfd_lookup(tunnel->fd, &ret);
1499 if (!sock)
1500 goto err;
6b9f3423
GN
1501 }
1502
b68777d5 1503 sk = sock->sk;
0b2c5972 1504 lock_sock(sk);
af295e85 1505 write_lock_bh(&sk->sk_callback_lock);
b68777d5 1506 ret = l2tp_validate_socket(sk, net, tunnel->encap);
b9fb10d1 1507 if (ret < 0)
af295e85
JS
1508 goto err_inval_sock;
1509 rcu_assign_sk_user_data(sk, tunnel);
1510 write_unlock_bh(&sk->sk_callback_lock);
b68777d5 1511
6b9f3423
GN
1512 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1513 struct udp_tunnel_sock_cfg udp_cfg = {
1514 .sk_user_data = tunnel,
1515 .encap_type = UDP_ENCAP_L2TPINUDP,
1516 .encap_rcv = l2tp_udp_encap_recv,
1517 .encap_destroy = l2tp_udp_encap_destroy,
1518 };
1519
1520 setup_udp_tunnel_sock(net, sock, &udp_cfg);
6b9f3423
GN
1521 }
1522
1523 tunnel->old_sk_destruct = sk->sk_destruct;
1524 sk->sk_destruct = &l2tp_tunnel_destruct;
6b9f3423 1525 sk->sk_allocation = GFP_ATOMIC;
0b2c5972
CW
1526 release_sock(sk);
1527
1528 sock_hold(sk);
1529 tunnel->sock = sk;
1530 tunnel->l2tp_net = net;
1531
1532 spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1533 idr_replace(&pn->l2tp_tunnel_idr, tunnel, tunnel->tunnel_id);
1534 spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
6b9f3423 1535
6b7bdcd7
TP
1536 trace_register_tunnel(tunnel);
1537
6b9f3423
GN
1538 if (tunnel->fd >= 0)
1539 sockfd_put(sock);
1540
1541 return 0;
1542
af295e85
JS
1543err_inval_sock:
1544 write_unlock_bh(&sk->sk_callback_lock);
b9fb10d1 1545 release_sock(sk);
af295e85 1546
f6cd651b
GN
1547 if (tunnel->fd < 0)
1548 sock_release(sock);
1549 else
1550 sockfd_put(sock);
6b9f3423 1551err:
c4d48a58 1552 l2tp_tunnel_remove(net, tunnel);
6b9f3423
GN
1553 return ret;
1554}
1555EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1556
309795f4
JC
1557/* This function is used by the netlink TUNNEL_DELETE command.
1558 */
62b982ee 1559void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
309795f4 1560{
62b982ee 1561 if (!test_and_set_bit(0, &tunnel->dead)) {
6b7bdcd7 1562 trace_delete_tunnel(tunnel);
62b982ee
SD
1563 l2tp_tunnel_inc_refcount(tunnel);
1564 queue_work(l2tp_wq, &tunnel->del_work);
06a15f51 1565 }
309795f4
JC
1566}
1567EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1568
628703f5 1569void l2tp_session_delete(struct l2tp_session *session)
309795f4 1570{
b228a940 1571 if (test_and_set_bit(0, &session->dead))
628703f5 1572 return;
b228a940 1573
6b7bdcd7 1574 trace_delete_session(session);
b2aecfe8 1575 l2tp_session_unhash(session);
4c6e2fd3 1576 l2tp_session_queue_purge(session);
0febc7b3 1577 if (session->session_close)
309795f4 1578 (*session->session_close)(session);
a4346210 1579
309795f4 1580 l2tp_session_dec_refcount(session);
309795f4
JC
1581}
1582EXPORT_SYMBOL_GPL(l2tp_session_delete);
1583
f7faffa3 1584/* We come here whenever a session's send_seq, cookie_len or
62e7b6a5 1585 * l2specific_type parameters are set.
f7faffa3 1586 */
bb5016ea 1587void l2tp_session_set_header_len(struct l2tp_session *session, int version)
f7faffa3
JC
1588{
1589 if (version == L2TP_HDR_VER_2) {
1590 session->hdr_len = 6;
1591 if (session->send_seq)
1592 session->hdr_len += 4;
1593 } else {
62e7b6a5
LB
1594 session->hdr_len = 4 + session->cookie_len;
1595 session->hdr_len += l2tp_get_l2specific_len(session);
0d76751f
JC
1596 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1597 session->hdr_len += 4;
f7faffa3 1598 }
f7faffa3 1599}
bb5016ea 1600EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
f7faffa3 1601
c0235fb3
TP
1602struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id,
1603 u32 peer_session_id, struct l2tp_session_cfg *cfg)
fd558d18
JC
1604{
1605 struct l2tp_session *session;
1606
70c05bfa 1607 session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL);
0febc7b3 1608 if (session) {
fd558d18
JC
1609 session->magic = L2TP_SESSION_MAGIC;
1610 session->tunnel = tunnel;
1611
1612 session->session_id = session_id;
1613 session->peer_session_id = peer_session_id;
d301e325 1614 session->nr = 0;
8a1631d5
JC
1615 if (tunnel->version == L2TP_HDR_VER_2)
1616 session->nr_max = 0xffff;
1617 else
1618 session->nr_max = 0xffffff;
1619 session->nr_window_size = session->nr_max / 2;
a0dbd822
JC
1620 session->nr_oos_count_max = 4;
1621
1622 /* Use NR of first received packet */
1623 session->reorder_skip = 1;
fd558d18
JC
1624
1625 sprintf(&session->name[0], "sess %u/%u",
1626 tunnel->tunnel_id, session->session_id);
1627
1628 skb_queue_head_init(&session->reorder_q);
1629
1630 INIT_HLIST_NODE(&session->hlist);
f7faffa3 1631 INIT_HLIST_NODE(&session->global_hlist);
fd558d18 1632
fd558d18 1633 if (cfg) {
f7faffa3 1634 session->pwtype = cfg->pw_type;
fd558d18
JC
1635 session->send_seq = cfg->send_seq;
1636 session->recv_seq = cfg->recv_seq;
1637 session->lns_mode = cfg->lns_mode;
f7faffa3 1638 session->reorder_timeout = cfg->reorder_timeout;
f7faffa3 1639 session->l2specific_type = cfg->l2specific_type;
f7faffa3
JC
1640 session->cookie_len = cfg->cookie_len;
1641 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1642 session->peer_cookie_len = cfg->peer_cookie_len;
1643 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
fd558d18
JC
1644 }
1645
f7faffa3
JC
1646 l2tp_session_set_header_len(session, tunnel->version);
1647
9ee369a4
GN
1648 refcount_set(&session->ref_count, 1);
1649
dbdbc73b 1650 return session;
fd558d18
JC
1651 }
1652
dbdbc73b 1653 return ERR_PTR(-ENOMEM);
fd558d18
JC
1654}
1655EXPORT_SYMBOL_GPL(l2tp_session_create);
1656
1657/*****************************************************************************
1658 * Init and cleanup
1659 *****************************************************************************/
1660
1661static __net_init int l2tp_init_net(struct net *net)
1662{
e773aaff 1663 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
f7faffa3 1664 int hash;
fd558d18 1665
c4d48a58
CW
1666 idr_init(&pn->l2tp_tunnel_idr);
1667 spin_lock_init(&pn->l2tp_tunnel_idr_lock);
fd558d18 1668
f7faffa3
JC
1669 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1670 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1671
e02d494d 1672 spin_lock_init(&pn->l2tp_session_hlist_lock);
f7faffa3 1673
fd558d18 1674 return 0;
fd558d18
JC
1675}
1676
167eb17e
TP
1677static __net_exit void l2tp_exit_net(struct net *net)
1678{
1679 struct l2tp_net *pn = l2tp_pernet(net);
1680 struct l2tp_tunnel *tunnel = NULL;
c4d48a58 1681 unsigned long tunnel_id, tmp;
1e7af3b2 1682 int hash;
167eb17e
TP
1683
1684 rcu_read_lock_bh();
c4d48a58
CW
1685 idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
1686 if (tunnel)
1687 l2tp_tunnel_delete(tunnel);
167eb17e
TP
1688 }
1689 rcu_read_unlock_bh();
2f86953e 1690
638a3a1e
Y
1691 if (l2tp_wq)
1692 flush_workqueue(l2tp_wq);
2f86953e 1693 rcu_barrier();
1e7af3b2
VA
1694
1695 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1696 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
c4d48a58 1697 idr_destroy(&pn->l2tp_tunnel_idr);
167eb17e
TP
1698}
1699
fd558d18
JC
1700static struct pernet_operations l2tp_net_ops = {
1701 .init = l2tp_init_net,
167eb17e 1702 .exit = l2tp_exit_net,
fd558d18
JC
1703 .id = &l2tp_net_id,
1704 .size = sizeof(struct l2tp_net),
1705};
1706
1707static int __init l2tp_init(void)
1708{
1709 int rc = 0;
1710
1711 rc = register_pernet_device(&l2tp_net_ops);
1712 if (rc)
1713 goto out;
1714
59ff3eb6 1715 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
f8ccac0e
TP
1716 if (!l2tp_wq) {
1717 pr_err("alloc_workqueue failed\n");
67e04c29 1718 unregister_pernet_device(&l2tp_net_ops);
f8ccac0e
TP
1719 rc = -ENOMEM;
1720 goto out;
1721 }
1722
a4ca44fa 1723 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
fd558d18
JC
1724
1725out:
1726 return rc;
1727}
1728
1729static void __exit l2tp_exit(void)
1730{
1731 unregister_pernet_device(&l2tp_net_ops);
f8ccac0e
TP
1732 if (l2tp_wq) {
1733 destroy_workqueue(l2tp_wq);
1734 l2tp_wq = NULL;
1735 }
fd558d18
JC
1736}
1737
1738module_init(l2tp_init);
1739module_exit(l2tp_exit);
1740
1741MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1742MODULE_DESCRIPTION("L2TP core");
1743MODULE_LICENSE("GPL");
1744MODULE_VERSION(L2TP_DRV_VERSION);