f2fs: Provide a splice-read wrapper
[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;
797 unsigned char *ptr, *optr;
798 u16 hdrflags;
799 u32 tunnel_id, session_id;
f7faffa3
JC
800 u16 version;
801 int length;
802
aa785f93 803 /* UDP has verified checksum */
f7faffa3
JC
804
805 /* UDP always verifies the packet length. */
806 __skb_pull(skb, sizeof(struct udphdr));
807
808 /* Short packet? */
91c52470 809 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) {
3e59e885
MS
810 pr_debug_ratelimited("%s: recv short packet (len=%d)\n",
811 tunnel->name, skb->len);
812 goto invalid;
f7faffa3
JC
813 }
814
e50e705c 815 /* Point to L2TP header */
95075150
TP
816 optr = skb->data;
817 ptr = skb->data;
e50e705c 818
f7faffa3 819 /* Get L2TP header flags */
b71a61cc 820 hdrflags = ntohs(*(__be16 *)ptr);
f7faffa3
JC
821
822 /* Check protocol version */
823 version = hdrflags & L2TP_HDR_VER_MASK;
824 if (version != tunnel->version) {
3e59e885
MS
825 pr_debug_ratelimited("%s: recv protocol version mismatch: got %d expected %d\n",
826 tunnel->name, version, tunnel->version);
827 goto invalid;
f7faffa3
JC
828 }
829
830 /* Get length of L2TP packet */
831 length = skb->len;
832
833 /* If type is control packet, it is handled by userspace. */
12923365 834 if (hdrflags & L2TP_HDRFLAG_T)
3e59e885 835 goto pass;
f7faffa3
JC
836
837 /* Skip flags */
838 ptr += 2;
839
840 if (tunnel->version == L2TP_HDR_VER_2) {
841 /* If length is present, skip it */
842 if (hdrflags & L2TP_HDRFLAG_L)
843 ptr += 2;
844
845 /* Extract tunnel and session ID */
b71a61cc 846 tunnel_id = ntohs(*(__be16 *)ptr);
f7faffa3 847 ptr += 2;
b71a61cc 848 session_id = ntohs(*(__be16 *)ptr);
f7faffa3
JC
849 ptr += 2;
850 } else {
851 ptr += 2; /* skip reserved bits */
852 tunnel_id = tunnel->tunnel_id;
b71a61cc 853 session_id = ntohl(*(__be32 *)ptr);
f7faffa3
JC
854 ptr += 4;
855 }
856
857 /* Find the session context */
01e28b92 858 session = l2tp_tunnel_get_session(tunnel, session_id);
309795f4 859 if (!session || !session->recv_skb) {
a4346210 860 if (session)
61b9a047 861 l2tp_session_dec_refcount(session);
61b9a047 862
f7faffa3 863 /* Not found? Pass to userspace to deal with */
3e59e885
MS
864 pr_debug_ratelimited("%s: no session found (%u/%u). Passing up.\n",
865 tunnel->name, tunnel_id, session_id);
866 goto pass;
f7faffa3
JC
867 }
868
4522a70d 869 if (tunnel->version == L2TP_HDR_VER_3 &&
9b6ff7eb
XY
870 l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
871 l2tp_session_dec_refcount(session);
3e59e885 872 goto invalid;
9b6ff7eb 873 }
4522a70d 874
2b139e6b 875 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
61b9a047 876 l2tp_session_dec_refcount(session);
fd558d18
JC
877
878 return 0;
879
3e59e885
MS
880invalid:
881 atomic_long_inc(&tunnel->stats.rx_invalid);
882
883pass:
fd558d18
JC
884 /* Put UDP header back */
885 __skb_push(skb, sizeof(struct udphdr));
886
887 return 1;
888}
fd558d18
JC
889
890/* UDP encapsulation receive handler. See net/ipv4/udp.c.
891 * Return codes:
892 * 0 : success.
893 * <0: error
894 * >0: skb should be passed up to userspace as UDP.
895 */
896int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
897{
898 struct l2tp_tunnel *tunnel;
899
45faeff1
TP
900 /* Note that this is called from the encap_rcv hook inside an
901 * RCU-protected region, but without the socket being locked.
902 * Hence we use rcu_dereference_sk_user_data to access the
903 * tunnel data structure rather the usual l2tp_sk_to_tunnel
904 * accessor function.
905 */
c1c47721 906 tunnel = rcu_dereference_sk_user_data(sk);
0febc7b3 907 if (!tunnel)
fd558d18 908 goto pass_up;
45faeff1
TP
909 if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
910 goto pass_up;
fd558d18 911
2b139e6b 912 if (l2tp_udp_recv_core(tunnel, skb))
d00fa9ad 913 goto pass_up;
fd558d18 914
fd558d18
JC
915 return 0;
916
fd558d18
JC
917pass_up:
918 return 1;
919}
920EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
921
922/************************************************************************
923 * Transmit handling
924 ***********************************************************************/
925
926/* Build an L2TP header for the session into the buffer provided.
927 */
f7faffa3 928static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
fd558d18 929{
f7faffa3 930 struct l2tp_tunnel *tunnel = session->tunnel;
fd558d18 931 __be16 *bufp = buf;
f7faffa3 932 __be16 *optr = buf;
fd558d18
JC
933 u16 flags = L2TP_HDR_VER_2;
934 u32 tunnel_id = tunnel->peer_tunnel_id;
935 u32 session_id = session->peer_session_id;
936
937 if (session->send_seq)
938 flags |= L2TP_HDRFLAG_S;
939
940 /* Setup L2TP header. */
941 *bufp++ = htons(flags);
942 *bufp++ = htons(tunnel_id);
943 *bufp++ = htons(session_id);
944 if (session->send_seq) {
945 *bufp++ = htons(session->ns);
946 *bufp++ = 0;
947 session->ns++;
f7faffa3 948 session->ns &= 0xffff;
6b7bdcd7 949 trace_session_seqnum_update(session);
fd558d18 950 }
f7faffa3
JC
951
952 return bufp - optr;
fd558d18
JC
953}
954
f7faffa3 955static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
fd558d18 956{
0d76751f 957 struct l2tp_tunnel *tunnel = session->tunnel;
f7faffa3
JC
958 char *bufp = buf;
959 char *optr = bufp;
f7faffa3 960
0d76751f
JC
961 /* Setup L2TP header. The header differs slightly for UDP and
962 * IP encapsulations. For UDP, there is 4 bytes of flags.
963 */
964 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
965 u16 flags = L2TP_HDR_VER_3;
b71a61cc 966 *((__be16 *)bufp) = htons(flags);
0d76751f 967 bufp += 2;
b71a61cc 968 *((__be16 *)bufp) = 0;
0d76751f
JC
969 bufp += 2;
970 }
971
b71a61cc 972 *((__be32 *)bufp) = htonl(session->peer_session_id);
f7faffa3
JC
973 bufp += 4;
974 if (session->cookie_len) {
975 memcpy(bufp, &session->cookie[0], session->cookie_len);
976 bufp += session->cookie_len;
977 }
62e7b6a5
LB
978 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
979 u32 l2h = 0;
f7faffa3 980
62e7b6a5
LB
981 if (session->send_seq) {
982 l2h = 0x40000000 | session->ns;
983 session->ns++;
984 session->ns &= 0xffffff;
6b7bdcd7 985 trace_session_seqnum_update(session);
f7faffa3 986 }
62e7b6a5
LB
987
988 *((__be32 *)bufp) = htonl(l2h);
989 bufp += 4;
f7faffa3 990 }
fd558d18 991
f7faffa3 992 return bufp - optr;
fd558d18 993}
fd558d18 994
de68b039
TP
995/* Queue the packet to IP for output: tunnel socket lock must be held */
996static int l2tp_xmit_queue(struct l2tp_tunnel *tunnel, struct sk_buff *skb, struct flowi *fl)
fd558d18 997{
de68b039 998 int err;
fd558d18 999
60ff7467 1000 skb->ignore_df = 1;
27d53323 1001 skb_dst_drop(skb);
d2cf3361 1002#if IS_ENABLED(CONFIG_IPV6)
b954f940 1003 if (l2tp_sk_is_v6(tunnel->sock))
de68b039 1004 err = inet6_csk_xmit(tunnel->sock, skb, NULL);
d2cf3361
BL
1005 else
1006#endif
de68b039 1007 err = ip_queue_xmit(tunnel->sock, skb, fl);
fd558d18 1008
de68b039 1009 return err >= 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
fd558d18 1010}
fd558d18 1011
f52e4b27 1012static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, unsigned int *len)
fd558d18 1013{
0d76751f 1014 struct l2tp_tunnel *tunnel = session->tunnel;
de68b039 1015 unsigned int data_len = skb->len;
0d76751f 1016 struct sock *sk = tunnel->sock;
de68b039 1017 int headroom, uhlen, udp_len;
b8c84307 1018 int ret = NET_XMIT_SUCCESS;
de68b039
TP
1019 struct inet_sock *inet;
1020 struct udphdr *uh;
fd558d18
JC
1021
1022 /* Check that there's enough headroom in the skb to insert IP,
1023 * UDP and L2TP headers. If not enough, expand it to
1024 * make room. Adjust truesize.
1025 */
de68b039
TP
1026 uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(*uh) : 0;
1027 headroom = NET_SKB_PAD + sizeof(struct iphdr) + uhlen + session->hdr_len;
835acf5d 1028 if (skb_cow_head(skb, headroom)) {
b8c84307
ED
1029 kfree_skb(skb);
1030 return NET_XMIT_DROP;
835acf5d 1031 }
fd558d18 1032
fd558d18 1033 /* Setup L2TP header */
2dedab6f 1034 if (tunnel->version == L2TP_HDR_VER_2)
efe05278 1035 l2tp_build_l2tpv2_header(session, __skb_push(skb, session->hdr_len));
2dedab6f 1036 else
efe05278 1037 l2tp_build_l2tpv3_header(session, __skb_push(skb, session->hdr_len));
fd558d18 1038
0d76751f 1039 /* Reset skb netfilter state */
fd558d18 1040 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
de68b039 1041 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
895b5c9f 1042 nf_reset_ct(skb);
fd558d18 1043
0b2c5972 1044 bh_lock_sock_nested(sk);
6af88da1 1045 if (sock_owned_by_user(sk)) {
b8c84307
ED
1046 kfree_skb(skb);
1047 ret = NET_XMIT_DROP;
6af88da1
DM
1048 goto out_unlock;
1049 }
1050
b954f940
PA
1051 /* The user-space may change the connection status for the user-space
1052 * provided socket at run time: we must check it under the socket lock
1053 */
1054 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1055 kfree_skb(skb);
1056 ret = NET_XMIT_DROP;
1057 goto out_unlock;
1058 }
1059
f52e4b27
TP
1060 /* Report transmitted length before we add encap header, which keeps
1061 * statistics consistent for both UDP and IP encap tx/rx paths.
1062 */
1063 *len = skb->len;
1064
d9d8da80 1065 inet = inet_sk(sk);
0d76751f
JC
1066 switch (tunnel->encap) {
1067 case L2TP_ENCAPTYPE_UDP:
1068 /* Setup UDP header */
0d76751f
JC
1069 __skb_push(skb, sizeof(*uh));
1070 skb_reset_transport_header(skb);
1071 uh = udp_hdr(skb);
1072 uh->source = inet->inet_sport;
1073 uh->dest = inet->inet_dport;
efe05278 1074 udp_len = uhlen + session->hdr_len + data_len;
0d76751f 1075 uh->len = htons(udp_len);
0d76751f
JC
1076
1077 /* Calculate UDP checksum if configured to do so */
d2cf3361 1078#if IS_ENABLED(CONFIG_IPV6)
b954f940 1079 if (l2tp_sk_is_v6(sk))
77157e19
TH
1080 udp6_set_csum(udp_get_no_check6_tx(sk),
1081 skb, &inet6_sk(sk)->saddr,
1082 &sk->sk_v6_daddr, udp_len);
d2cf3361
BL
1083 else
1084#endif
0864e331
TP
1085 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1086 inet->inet_daddr, udp_len);
0d76751f
JC
1087 break;
1088
1089 case L2TP_ENCAPTYPE_IP:
1090 break;
fd558d18
JC
1091 }
1092
de68b039
TP
1093 ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl);
1094
6af88da1
DM
1095out_unlock:
1096 bh_unlock_sock(sk);
fd558d18 1097
b8c84307 1098 return ret;
fd558d18 1099}
de68b039
TP
1100
1101/* If caller requires the skb to have a ppp header, the header must be
1102 * inserted in the skb data before calling this function.
1103 */
1104int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb)
1105{
f52e4b27 1106 unsigned int len = 0;
de68b039
TP
1107 int ret;
1108
f52e4b27 1109 ret = l2tp_xmit_core(session, skb, &len);
de68b039
TP
1110 if (ret == NET_XMIT_SUCCESS) {
1111 atomic_long_inc(&session->tunnel->stats.tx_packets);
1112 atomic_long_add(len, &session->tunnel->stats.tx_bytes);
1113 atomic_long_inc(&session->stats.tx_packets);
1114 atomic_long_add(len, &session->stats.tx_bytes);
1115 } else {
1116 atomic_long_inc(&session->tunnel->stats.tx_errors);
1117 atomic_long_inc(&session->stats.tx_errors);
1118 }
1119 return ret;
1120}
fd558d18
JC
1121EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1122
1123/*****************************************************************************
1124 * Tinnel and session create/destroy.
1125 *****************************************************************************/
1126
1127/* Tunnel socket destruct hook.
1128 * The tunnel context is deleted only when all session sockets have been
1129 * closed.
1130 */
fc130840 1131static void l2tp_tunnel_destruct(struct sock *sk)
fd558d18 1132{
45faeff1 1133 struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
fd558d18 1134
0febc7b3 1135 if (!tunnel)
fd558d18
JC
1136 goto end;
1137
f8ccac0e 1138 /* Disable udp encapsulation */
0d76751f
JC
1139 switch (tunnel->encap) {
1140 case L2TP_ENCAPTYPE_UDP:
1141 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1142 (udp_sk(sk))->encap_type = 0;
1143 (udp_sk(sk))->encap_rcv = NULL;
9980d001 1144 (udp_sk(sk))->encap_destroy = NULL;
0d76751f
JC
1145 break;
1146 case L2TP_ENCAPTYPE_IP:
1147 break;
1148 }
fd558d18
JC
1149
1150 /* Remove hooks into tunnel socket */
b68777d5 1151 write_lock_bh(&sk->sk_callback_lock);
fd558d18
JC
1152 sk->sk_destruct = tunnel->old_sk_destruct;
1153 sk->sk_user_data = NULL;
b68777d5 1154 write_unlock_bh(&sk->sk_callback_lock);
fd558d18 1155
f8ccac0e
TP
1156 /* Call the original destructor */
1157 if (sk->sk_destruct)
1158 (*sk->sk_destruct)(sk);
d00fa9ad
JC
1159
1160 kfree_rcu(tunnel, rcu);
fd558d18
JC
1161end:
1162 return;
1163}
fd558d18 1164
b2aecfe8
TP
1165/* Remove an l2tp session from l2tp_core's hash lists. */
1166static void l2tp_session_unhash(struct l2tp_session *session)
1167{
1168 struct l2tp_tunnel *tunnel = session->tunnel;
1169
1170 /* Remove the session from core hashes */
1171 if (tunnel) {
1172 /* Remove from the per-tunnel hash */
07b8ca37
TP
1173 spin_lock_bh(&tunnel->hlist_lock);
1174 hlist_del_init_rcu(&session->hlist);
1175 spin_unlock_bh(&tunnel->hlist_lock);
b2aecfe8
TP
1176
1177 /* For L2TPv3 we have a per-net hash: remove from there, too */
1178 if (tunnel->version != L2TP_HDR_VER_2) {
1179 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1180
1181 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1182 hlist_del_init_rcu(&session->global_hlist);
1183 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
b2aecfe8 1184 }
07b8ca37
TP
1185
1186 synchronize_rcu();
b2aecfe8
TP
1187 }
1188}
1189
fd558d18
JC
1190/* When the tunnel is closed, all the attached sessions need to go too.
1191 */
d08532bb 1192static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
fd558d18 1193{
fd558d18 1194 struct l2tp_session *session;
07b8ca37 1195 int hash;
fd558d18 1196
07b8ca37 1197 spin_lock_bh(&tunnel->hlist_lock);
f3c66d4e 1198 tunnel->acpt_newsess = false;
fd558d18
JC
1199 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1200again:
07b8ca37
TP
1201 hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) {
1202 hlist_del_init_rcu(&session->hlist);
fd558d18 1203
07b8ca37 1204 spin_unlock_bh(&tunnel->hlist_lock);
9d319a8e 1205 l2tp_session_delete(session);
07b8ca37 1206 spin_lock_bh(&tunnel->hlist_lock);
fd558d18
JC
1207
1208 /* Now restart from the beginning of this hash
1209 * chain. We always remove a session from the
1210 * list so we are guaranteed to make forward
1211 * progress.
1212 */
1213 goto again;
1214 }
1215 }
07b8ca37 1216 spin_unlock_bh(&tunnel->hlist_lock);
fd558d18 1217}
fd558d18 1218
9980d001
TP
1219/* Tunnel socket destroy hook for UDP encapsulation */
1220static void l2tp_udp_encap_destroy(struct sock *sk)
1221{
45faeff1 1222 struct l2tp_tunnel *tunnel = l2tp_sk_to_tunnel(sk);
d00fa9ad
JC
1223
1224 if (tunnel)
1225 l2tp_tunnel_delete(tunnel);
9980d001
TP
1226}
1227
c4d48a58
CW
1228static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel)
1229{
1230 struct l2tp_net *pn = l2tp_pernet(net);
1231
1232 spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1233 idr_remove(&pn->l2tp_tunnel_idr, tunnel->tunnel_id);
1234 spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
1235}
1236
f8ccac0e
TP
1237/* Workqueue tunnel deletion function */
1238static void l2tp_tunnel_del_work(struct work_struct *work)
1239{
d00fa9ad
JC
1240 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1241 del_work);
1242 struct sock *sk = tunnel->sock;
1243 struct socket *sock = sk->sk_socket;
12d656af
RK
1244
1245 l2tp_tunnel_closeall(tunnel);
1246
76a6abdb 1247 /* If the tunnel socket was created within the kernel, use
02d13ed5 1248 * the sk API to release it here.
f8ccac0e 1249 */
76a6abdb 1250 if (tunnel->fd < 0) {
26abe143 1251 if (sock) {
02d13ed5 1252 kernel_sock_shutdown(sock, SHUT_RDWR);
26abe143
EB
1253 sock_release(sock);
1254 }
167eb17e 1255 }
f8ccac0e 1256
c4d48a58 1257 l2tp_tunnel_remove(tunnel->l2tp_net, tunnel);
d00fa9ad
JC
1258 /* drop initial ref */
1259 l2tp_tunnel_dec_refcount(tunnel);
1260
1261 /* drop workqueue ref */
06a15f51 1262 l2tp_tunnel_dec_refcount(tunnel);
fd558d18 1263}
fd558d18 1264
789a4a2c
JC
1265/* Create a socket for the tunnel, if one isn't set up by
1266 * userspace. This is used for static tunnels where there is no
1267 * managing L2TP daemon.
167eb17e
TP
1268 *
1269 * Since we don't want these sockets to keep a namespace alive by
1270 * themselves, we drop the socket's namespace refcount after creation.
1271 * These sockets are freed when the namespace exits using the pernet
1272 * exit hook.
789a4a2c 1273 */
167eb17e 1274static int l2tp_tunnel_sock_create(struct net *net,
8ce9825a
TP
1275 u32 tunnel_id,
1276 u32 peer_tunnel_id,
1277 struct l2tp_tunnel_cfg *cfg,
1278 struct socket **sockp)
789a4a2c
JC
1279{
1280 int err = -EINVAL;
167eb17e 1281 struct socket *sock = NULL;
85644b4d 1282 struct udp_port_cfg udp_conf;
789a4a2c
JC
1283
1284 switch (cfg->encap) {
1285 case L2TP_ENCAPTYPE_UDP:
85644b4d
TH
1286 memset(&udp_conf, 0, sizeof(udp_conf));
1287
f9bac8df
CE
1288#if IS_ENABLED(CONFIG_IPV6)
1289 if (cfg->local_ip6 && cfg->peer_ip6) {
85644b4d
TH
1290 udp_conf.family = AF_INET6;
1291 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1292 sizeof(udp_conf.local_ip6));
1293 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1294 sizeof(udp_conf.peer_ip6));
1295 udp_conf.use_udp6_tx_checksums =
b71a61cc 1296 !cfg->udp6_zero_tx_checksums;
85644b4d 1297 udp_conf.use_udp6_rx_checksums =
b71a61cc 1298 !cfg->udp6_zero_rx_checksums;
f9bac8df
CE
1299 } else
1300#endif
1301 {
85644b4d
TH
1302 udp_conf.family = AF_INET;
1303 udp_conf.local_ip = cfg->local_ip;
1304 udp_conf.peer_ip = cfg->peer_ip;
1305 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
f9bac8df 1306 }
789a4a2c 1307
85644b4d
TH
1308 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1309 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1310
1311 err = udp_sock_create(net, &udp_conf, &sock);
1312 if (err < 0)
1313 goto out;
789a4a2c
JC
1314
1315 break;
1316
1317 case L2TP_ENCAPTYPE_IP:
f9bac8df
CE
1318#if IS_ENABLED(CONFIG_IPV6)
1319 if (cfg->local_ip6 && cfg->peer_ip6) {
85644b4d
TH
1320 struct sockaddr_l2tpip6 ip6_addr = {0};
1321
26abe143 1322 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
8ce9825a 1323 IPPROTO_L2TP, &sock);
5dac94e1
JC
1324 if (err < 0)
1325 goto out;
789a4a2c 1326
5dac94e1
JC
1327 ip6_addr.l2tp_family = AF_INET6;
1328 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1329 sizeof(ip6_addr.l2tp_addr));
1330 ip6_addr.l2tp_conn_id = tunnel_id;
b71a61cc 1331 err = kernel_bind(sock, (struct sockaddr *)&ip6_addr,
5dac94e1
JC
1332 sizeof(ip6_addr));
1333 if (err < 0)
1334 goto out;
789a4a2c 1335
5dac94e1
JC
1336 ip6_addr.l2tp_family = AF_INET6;
1337 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1338 sizeof(ip6_addr.l2tp_addr));
1339 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1340 err = kernel_connect(sock,
b71a61cc 1341 (struct sockaddr *)&ip6_addr,
5dac94e1
JC
1342 sizeof(ip6_addr), 0);
1343 if (err < 0)
1344 goto out;
1345 } else
1346#endif
1347 {
85644b4d
TH
1348 struct sockaddr_l2tpip ip_addr = {0};
1349
26abe143 1350 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
8ce9825a 1351 IPPROTO_L2TP, &sock);
5dac94e1
JC
1352 if (err < 0)
1353 goto out;
789a4a2c 1354
5dac94e1
JC
1355 ip_addr.l2tp_family = AF_INET;
1356 ip_addr.l2tp_addr = cfg->local_ip;
1357 ip_addr.l2tp_conn_id = tunnel_id;
b71a61cc 1358 err = kernel_bind(sock, (struct sockaddr *)&ip_addr,
5dac94e1
JC
1359 sizeof(ip_addr));
1360 if (err < 0)
1361 goto out;
1362
1363 ip_addr.l2tp_family = AF_INET;
1364 ip_addr.l2tp_addr = cfg->peer_ip;
1365 ip_addr.l2tp_conn_id = peer_tunnel_id;
b71a61cc 1366 err = kernel_connect(sock, (struct sockaddr *)&ip_addr,
5dac94e1
JC
1367 sizeof(ip_addr), 0);
1368 if (err < 0)
1369 goto out;
1370 }
789a4a2c
JC
1371 break;
1372
1373 default:
1374 goto out;
1375 }
1376
1377out:
167eb17e 1378 *sockp = sock;
6c0ec37b 1379 if (err < 0 && sock) {
167eb17e 1380 kernel_sock_shutdown(sock, SHUT_RDWR);
26abe143 1381 sock_release(sock);
789a4a2c
JC
1382 *sockp = NULL;
1383 }
1384
1385 return err;
1386}
1387
c9ccd4c6 1388int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id,
c0235fb3 1389 struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
fd558d18
JC
1390{
1391 struct l2tp_tunnel *tunnel = NULL;
1392 int err;
0d76751f 1393 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
fd558d18 1394
0febc7b3 1395 if (cfg)
0d76751f
JC
1396 encap = cfg->encap;
1397
70c05bfa 1398 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
0febc7b3 1399 if (!tunnel) {
fd558d18
JC
1400 err = -ENOMEM;
1401 goto err;
1402 }
1403
1404 tunnel->version = version;
1405 tunnel->tunnel_id = tunnel_id;
1406 tunnel->peer_tunnel_id = peer_tunnel_id;
fd558d18
JC
1407
1408 tunnel->magic = L2TP_TUNNEL_MAGIC;
1409 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
07b8ca37 1410 spin_lock_init(&tunnel->hlist_lock);
f3c66d4e 1411 tunnel->acpt_newsess = true;
fd558d18 1412
0d76751f 1413 tunnel->encap = encap;
fd558d18 1414
d00fa9ad 1415 refcount_set(&tunnel->ref_count, 1);
d00fa9ad
JC
1416 tunnel->fd = fd;
1417
f8ccac0e
TP
1418 /* Init delete workqueue struct */
1419 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1420
fd558d18 1421 INIT_LIST_HEAD(&tunnel->list);
fd558d18
JC
1422
1423 err = 0;
1424err:
1425 if (tunnelp)
1426 *tunnelp = tunnel;
1427
fd558d18
JC
1428 return err;
1429}
1430EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1431
6b9f3423
GN
1432static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1433 enum l2tp_encap_type encap)
1434{
1435 if (!net_eq(sock_net(sk), net))
1436 return -EINVAL;
1437
1438 if (sk->sk_type != SOCK_DGRAM)
1439 return -EPROTONOSUPPORT;
1440
d9a81a22
ED
1441 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
1442 return -EPROTONOSUPPORT;
1443
6b9f3423
GN
1444 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1445 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1446 return -EPROTONOSUPPORT;
1447
1448 if (sk->sk_user_data)
1449 return -EBUSY;
1450
1451 return 0;
1452}
1453
1454int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1455 struct l2tp_tunnel_cfg *cfg)
1456{
c4d48a58
CW
1457 struct l2tp_net *pn = l2tp_pernet(net);
1458 u32 tunnel_id = tunnel->tunnel_id;
6b9f3423
GN
1459 struct socket *sock;
1460 struct sock *sk;
1461 int ret;
1462
c4d48a58
CW
1463 spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1464 ret = idr_alloc_u32(&pn->l2tp_tunnel_idr, NULL, &tunnel_id, tunnel_id,
1465 GFP_ATOMIC);
1466 spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
1467 if (ret)
1468 return ret == -ENOSPC ? -EEXIST : ret;
1469
6b9f3423
GN
1470 if (tunnel->fd < 0) {
1471 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1472 tunnel->peer_tunnel_id, cfg,
1473 &sock);
1474 if (ret < 0)
1475 goto err;
1476 } else {
1477 sock = sockfd_lookup(tunnel->fd, &ret);
1478 if (!sock)
1479 goto err;
6b9f3423
GN
1480 }
1481
b68777d5 1482 sk = sock->sk;
0b2c5972 1483 lock_sock(sk);
af295e85 1484 write_lock_bh(&sk->sk_callback_lock);
b68777d5 1485 ret = l2tp_validate_socket(sk, net, tunnel->encap);
b9fb10d1 1486 if (ret < 0)
af295e85
JS
1487 goto err_inval_sock;
1488 rcu_assign_sk_user_data(sk, tunnel);
1489 write_unlock_bh(&sk->sk_callback_lock);
b68777d5 1490
6b9f3423
GN
1491 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1492 struct udp_tunnel_sock_cfg udp_cfg = {
1493 .sk_user_data = tunnel,
1494 .encap_type = UDP_ENCAP_L2TPINUDP,
1495 .encap_rcv = l2tp_udp_encap_recv,
1496 .encap_destroy = l2tp_udp_encap_destroy,
1497 };
1498
1499 setup_udp_tunnel_sock(net, sock, &udp_cfg);
6b9f3423
GN
1500 }
1501
1502 tunnel->old_sk_destruct = sk->sk_destruct;
1503 sk->sk_destruct = &l2tp_tunnel_destruct;
6b9f3423 1504 sk->sk_allocation = GFP_ATOMIC;
0b2c5972
CW
1505 release_sock(sk);
1506
1507 sock_hold(sk);
1508 tunnel->sock = sk;
1509 tunnel->l2tp_net = net;
1510
1511 spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
1512 idr_replace(&pn->l2tp_tunnel_idr, tunnel, tunnel->tunnel_id);
1513 spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
6b9f3423 1514
6b7bdcd7
TP
1515 trace_register_tunnel(tunnel);
1516
6b9f3423
GN
1517 if (tunnel->fd >= 0)
1518 sockfd_put(sock);
1519
1520 return 0;
1521
af295e85
JS
1522err_inval_sock:
1523 write_unlock_bh(&sk->sk_callback_lock);
b9fb10d1 1524 release_sock(sk);
af295e85 1525
f6cd651b
GN
1526 if (tunnel->fd < 0)
1527 sock_release(sock);
1528 else
1529 sockfd_put(sock);
6b9f3423 1530err:
c4d48a58 1531 l2tp_tunnel_remove(net, tunnel);
6b9f3423
GN
1532 return ret;
1533}
1534EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1535
309795f4
JC
1536/* This function is used by the netlink TUNNEL_DELETE command.
1537 */
62b982ee 1538void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
309795f4 1539{
62b982ee 1540 if (!test_and_set_bit(0, &tunnel->dead)) {
6b7bdcd7 1541 trace_delete_tunnel(tunnel);
62b982ee
SD
1542 l2tp_tunnel_inc_refcount(tunnel);
1543 queue_work(l2tp_wq, &tunnel->del_work);
06a15f51 1544 }
309795f4
JC
1545}
1546EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1547
628703f5 1548void l2tp_session_delete(struct l2tp_session *session)
309795f4 1549{
b228a940 1550 if (test_and_set_bit(0, &session->dead))
628703f5 1551 return;
b228a940 1552
6b7bdcd7 1553 trace_delete_session(session);
b2aecfe8 1554 l2tp_session_unhash(session);
4c6e2fd3 1555 l2tp_session_queue_purge(session);
0febc7b3 1556 if (session->session_close)
309795f4 1557 (*session->session_close)(session);
a4346210 1558
309795f4 1559 l2tp_session_dec_refcount(session);
309795f4
JC
1560}
1561EXPORT_SYMBOL_GPL(l2tp_session_delete);
1562
f7faffa3 1563/* We come here whenever a session's send_seq, cookie_len or
62e7b6a5 1564 * l2specific_type parameters are set.
f7faffa3 1565 */
bb5016ea 1566void l2tp_session_set_header_len(struct l2tp_session *session, int version)
f7faffa3
JC
1567{
1568 if (version == L2TP_HDR_VER_2) {
1569 session->hdr_len = 6;
1570 if (session->send_seq)
1571 session->hdr_len += 4;
1572 } else {
62e7b6a5
LB
1573 session->hdr_len = 4 + session->cookie_len;
1574 session->hdr_len += l2tp_get_l2specific_len(session);
0d76751f
JC
1575 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1576 session->hdr_len += 4;
f7faffa3 1577 }
f7faffa3 1578}
bb5016ea 1579EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
f7faffa3 1580
c0235fb3
TP
1581struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id,
1582 u32 peer_session_id, struct l2tp_session_cfg *cfg)
fd558d18
JC
1583{
1584 struct l2tp_session *session;
1585
70c05bfa 1586 session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL);
0febc7b3 1587 if (session) {
fd558d18
JC
1588 session->magic = L2TP_SESSION_MAGIC;
1589 session->tunnel = tunnel;
1590
1591 session->session_id = session_id;
1592 session->peer_session_id = peer_session_id;
d301e325 1593 session->nr = 0;
8a1631d5
JC
1594 if (tunnel->version == L2TP_HDR_VER_2)
1595 session->nr_max = 0xffff;
1596 else
1597 session->nr_max = 0xffffff;
1598 session->nr_window_size = session->nr_max / 2;
a0dbd822
JC
1599 session->nr_oos_count_max = 4;
1600
1601 /* Use NR of first received packet */
1602 session->reorder_skip = 1;
fd558d18
JC
1603
1604 sprintf(&session->name[0], "sess %u/%u",
1605 tunnel->tunnel_id, session->session_id);
1606
1607 skb_queue_head_init(&session->reorder_q);
1608
1609 INIT_HLIST_NODE(&session->hlist);
f7faffa3 1610 INIT_HLIST_NODE(&session->global_hlist);
fd558d18 1611
fd558d18 1612 if (cfg) {
f7faffa3 1613 session->pwtype = cfg->pw_type;
fd558d18
JC
1614 session->send_seq = cfg->send_seq;
1615 session->recv_seq = cfg->recv_seq;
1616 session->lns_mode = cfg->lns_mode;
f7faffa3 1617 session->reorder_timeout = cfg->reorder_timeout;
f7faffa3 1618 session->l2specific_type = cfg->l2specific_type;
f7faffa3
JC
1619 session->cookie_len = cfg->cookie_len;
1620 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1621 session->peer_cookie_len = cfg->peer_cookie_len;
1622 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
fd558d18
JC
1623 }
1624
f7faffa3
JC
1625 l2tp_session_set_header_len(session, tunnel->version);
1626
9ee369a4
GN
1627 refcount_set(&session->ref_count, 1);
1628
dbdbc73b 1629 return session;
fd558d18
JC
1630 }
1631
dbdbc73b 1632 return ERR_PTR(-ENOMEM);
fd558d18
JC
1633}
1634EXPORT_SYMBOL_GPL(l2tp_session_create);
1635
1636/*****************************************************************************
1637 * Init and cleanup
1638 *****************************************************************************/
1639
1640static __net_init int l2tp_init_net(struct net *net)
1641{
e773aaff 1642 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
f7faffa3 1643 int hash;
fd558d18 1644
c4d48a58
CW
1645 idr_init(&pn->l2tp_tunnel_idr);
1646 spin_lock_init(&pn->l2tp_tunnel_idr_lock);
fd558d18 1647
f7faffa3
JC
1648 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1649 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1650
e02d494d 1651 spin_lock_init(&pn->l2tp_session_hlist_lock);
f7faffa3 1652
fd558d18 1653 return 0;
fd558d18
JC
1654}
1655
167eb17e
TP
1656static __net_exit void l2tp_exit_net(struct net *net)
1657{
1658 struct l2tp_net *pn = l2tp_pernet(net);
1659 struct l2tp_tunnel *tunnel = NULL;
c4d48a58 1660 unsigned long tunnel_id, tmp;
1e7af3b2 1661 int hash;
167eb17e
TP
1662
1663 rcu_read_lock_bh();
c4d48a58
CW
1664 idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
1665 if (tunnel)
1666 l2tp_tunnel_delete(tunnel);
167eb17e
TP
1667 }
1668 rcu_read_unlock_bh();
2f86953e 1669
638a3a1e
Y
1670 if (l2tp_wq)
1671 flush_workqueue(l2tp_wq);
2f86953e 1672 rcu_barrier();
1e7af3b2
VA
1673
1674 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1675 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
c4d48a58 1676 idr_destroy(&pn->l2tp_tunnel_idr);
167eb17e
TP
1677}
1678
fd558d18
JC
1679static struct pernet_operations l2tp_net_ops = {
1680 .init = l2tp_init_net,
167eb17e 1681 .exit = l2tp_exit_net,
fd558d18
JC
1682 .id = &l2tp_net_id,
1683 .size = sizeof(struct l2tp_net),
1684};
1685
1686static int __init l2tp_init(void)
1687{
1688 int rc = 0;
1689
1690 rc = register_pernet_device(&l2tp_net_ops);
1691 if (rc)
1692 goto out;
1693
59ff3eb6 1694 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
f8ccac0e
TP
1695 if (!l2tp_wq) {
1696 pr_err("alloc_workqueue failed\n");
67e04c29 1697 unregister_pernet_device(&l2tp_net_ops);
f8ccac0e
TP
1698 rc = -ENOMEM;
1699 goto out;
1700 }
1701
a4ca44fa 1702 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
fd558d18
JC
1703
1704out:
1705 return rc;
1706}
1707
1708static void __exit l2tp_exit(void)
1709{
1710 unregister_pernet_device(&l2tp_net_ops);
f8ccac0e
TP
1711 if (l2tp_wq) {
1712 destroy_workqueue(l2tp_wq);
1713 l2tp_wq = NULL;
1714 }
fd558d18
JC
1715}
1716
1717module_init(l2tp_init);
1718module_exit(l2tp_exit);
1719
1720MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1721MODULE_DESCRIPTION("L2TP core");
1722MODULE_LICENSE("GPL");
1723MODULE_VERSION(L2TP_DRV_VERSION);