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