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