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