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