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