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