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