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