4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
6 * This file contains some code of the original L2TPv2 pppol2tp
7 * driver, which has the following copyright:
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
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.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/list.h>
26 #include <linux/rculist.h>
27 #include <linux/uaccess.h>
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>
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>
44 #include <linux/udp.h>
45 #include <linux/l2tp.h>
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>
55 #include <net/inet_common.h>
57 #include <net/protocol.h>
58 #include <net/inet6_connection_sock.h>
59 #include <net/inet_ecn.h>
60 #include <net/ip6_route.h>
61 #include <net/ip6_checksum.h>
63 #include <asm/byteorder.h>
64 #include <linux/atomic.h>
66 #include "l2tp_core.h"
68 #define L2TP_DRV_VERSION "V2.0"
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
77 #define L2TP_HDR_VER_MASK 0x000F
78 #define L2TP_HDR_VER_2 0x0002
79 #define L2TP_HDR_VER_3 0x0003
81 /* L2TPv3 default L2-specific sublayer */
82 #define L2TP_SLFLAG_S 0x40000000
83 #define L2TP_SL_SEQ_MASK 0x00ffffff
85 #define L2TP_HDR_SIZE_SEQ 10
86 #define L2TP_HDR_SIZE_NOSEQ 6
88 /* Default trace flags */
89 #define L2TP_DEFAULT_DEBUG_FLAGS 0
91 /* Private data stored for received packets in the skb.
97 unsigned long expires
;
100 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
102 static atomic_t l2tp_tunnel_count
;
103 static atomic_t l2tp_session_count
;
104 static struct workqueue_struct
*l2tp_wq
;
106 /* per-net private data for this module */
107 static unsigned int l2tp_net_id
;
109 struct list_head l2tp_tunnel_list
;
110 spinlock_t l2tp_tunnel_list_lock
;
111 struct hlist_head l2tp_session_hlist
[L2TP_HASH_SIZE_2
];
112 spinlock_t l2tp_session_hlist_lock
;
115 static void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
);
116 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
);
117 static void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
);
119 static inline struct l2tp_net
*l2tp_pernet(struct net
*net
)
123 return net_generic(net
, l2tp_net_id
);
126 /* Tunnel reference counts. Incremented per session that is added to
129 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel
*tunnel
)
131 atomic_inc(&tunnel
->ref_count
);
134 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel
*tunnel
)
136 if (atomic_dec_and_test(&tunnel
->ref_count
))
137 l2tp_tunnel_free(tunnel
);
139 #ifdef L2TP_REFCNT_DEBUG
140 #define l2tp_tunnel_inc_refcount(_t) \
142 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
143 __func__, __LINE__, (_t)->name, \
144 atomic_read(&_t->ref_count)); \
145 l2tp_tunnel_inc_refcount_1(_t); \
147 #define l2tp_tunnel_dec_refcount(_t)
149 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
150 __func__
, __LINE__
, (_t
)->name
, \
151 atomic_read(&_t
->ref_count
)); \
152 l2tp_tunnel_dec_refcount_1(_t
); \
155 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
156 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
159 /* Session hash global list for L2TPv3.
160 * The session_id SHOULD be random according to RFC3931, but several
161 * L2TP implementations use incrementing session_ids. So we do a real
162 * hash on the session_id, rather than a simple bitmask.
164 static inline struct hlist_head
*
165 l2tp_session_id_hash_2(struct l2tp_net
*pn
, u32 session_id
)
167 return &pn
->l2tp_session_hlist
[hash_32(session_id
, L2TP_HASH_BITS_2
)];
171 /* Lookup the tunnel socket, possibly involving the fs code if the socket is
172 * owned by userspace. A struct sock returned from this function must be
173 * released using l2tp_tunnel_sock_put once you're done with it.
175 struct sock
*l2tp_tunnel_sock_lookup(struct l2tp_tunnel
*tunnel
)
178 struct socket
*sock
= NULL
;
179 struct sock
*sk
= NULL
;
184 if (tunnel
->fd
>= 0) {
185 /* Socket is owned by userspace, who might be in the process
186 * of closing it. Look the socket up using the fd to ensure
189 sock
= sockfd_lookup(tunnel
->fd
, &err
);
193 /* Socket is owned by kernelspace */
200 EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup
);
202 /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
203 void l2tp_tunnel_sock_put(struct sock
*sk
)
205 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
207 if (tunnel
->fd
>= 0) {
208 /* Socket is owned by userspace */
209 sockfd_put(sk
->sk_socket
);
214 EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put
);
216 /* Lookup a session by id in the global session list
218 static struct l2tp_session
*l2tp_session_find_2(struct net
*net
, u32 session_id
)
220 struct l2tp_net
*pn
= l2tp_pernet(net
);
221 struct hlist_head
*session_list
=
222 l2tp_session_id_hash_2(pn
, session_id
);
223 struct l2tp_session
*session
;
226 hlist_for_each_entry_rcu(session
, session_list
, global_hlist
) {
227 if (session
->session_id
== session_id
) {
228 rcu_read_unlock_bh();
232 rcu_read_unlock_bh();
237 /* Session hash list.
238 * The session_id SHOULD be random according to RFC2661, but several
239 * L2TP implementations (Cisco and Microsoft) use incrementing
240 * session_ids. So we do a real hash on the session_id, rather than a
243 static inline struct hlist_head
*
244 l2tp_session_id_hash(struct l2tp_tunnel
*tunnel
, u32 session_id
)
246 return &tunnel
->session_hlist
[hash_32(session_id
, L2TP_HASH_BITS
)];
249 /* Lookup a session by id
251 struct l2tp_session
*l2tp_session_find(struct net
*net
, struct l2tp_tunnel
*tunnel
, u32 session_id
)
253 struct hlist_head
*session_list
;
254 struct l2tp_session
*session
;
256 /* In L2TPv3, session_ids are unique over all tunnels and we
257 * sometimes need to look them up before we know the
261 return l2tp_session_find_2(net
, session_id
);
263 session_list
= l2tp_session_id_hash(tunnel
, session_id
);
264 read_lock_bh(&tunnel
->hlist_lock
);
265 hlist_for_each_entry(session
, session_list
, hlist
) {
266 if (session
->session_id
== session_id
) {
267 read_unlock_bh(&tunnel
->hlist_lock
);
271 read_unlock_bh(&tunnel
->hlist_lock
);
275 EXPORT_SYMBOL_GPL(l2tp_session_find
);
277 struct l2tp_session
*l2tp_session_find_nth(struct l2tp_tunnel
*tunnel
, int nth
)
280 struct l2tp_session
*session
;
283 read_lock_bh(&tunnel
->hlist_lock
);
284 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
285 hlist_for_each_entry(session
, &tunnel
->session_hlist
[hash
], hlist
) {
287 read_unlock_bh(&tunnel
->hlist_lock
);
293 read_unlock_bh(&tunnel
->hlist_lock
);
297 EXPORT_SYMBOL_GPL(l2tp_session_find_nth
);
299 /* Lookup a session by interface name.
300 * This is very inefficient but is only used by management interfaces.
302 struct l2tp_session
*l2tp_session_find_by_ifname(struct net
*net
, char *ifname
)
304 struct l2tp_net
*pn
= l2tp_pernet(net
);
306 struct l2tp_session
*session
;
309 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++) {
310 hlist_for_each_entry_rcu(session
, &pn
->l2tp_session_hlist
[hash
], global_hlist
) {
311 if (!strcmp(session
->ifname
, ifname
)) {
312 rcu_read_unlock_bh();
318 rcu_read_unlock_bh();
322 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname
);
324 /* Lookup a tunnel by id
326 struct l2tp_tunnel
*l2tp_tunnel_find(struct net
*net
, u32 tunnel_id
)
328 struct l2tp_tunnel
*tunnel
;
329 struct l2tp_net
*pn
= l2tp_pernet(net
);
332 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
333 if (tunnel
->tunnel_id
== tunnel_id
) {
334 rcu_read_unlock_bh();
338 rcu_read_unlock_bh();
342 EXPORT_SYMBOL_GPL(l2tp_tunnel_find
);
344 struct l2tp_tunnel
*l2tp_tunnel_find_nth(struct net
*net
, int nth
)
346 struct l2tp_net
*pn
= l2tp_pernet(net
);
347 struct l2tp_tunnel
*tunnel
;
351 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
353 rcu_read_unlock_bh();
358 rcu_read_unlock_bh();
362 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth
);
364 /*****************************************************************************
365 * Receive data handling
366 *****************************************************************************/
368 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
371 static void l2tp_recv_queue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
373 struct sk_buff
*skbp
;
375 u32 ns
= L2TP_SKB_CB(skb
)->ns
;
376 struct l2tp_stats
*sstats
;
378 spin_lock_bh(&session
->reorder_q
.lock
);
379 sstats
= &session
->stats
;
380 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
381 if (L2TP_SKB_CB(skbp
)->ns
> ns
) {
382 __skb_queue_before(&session
->reorder_q
, skbp
, skb
);
383 l2tp_dbg(session
, L2TP_MSG_SEQ
,
384 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
385 session
->name
, ns
, L2TP_SKB_CB(skbp
)->ns
,
386 skb_queue_len(&session
->reorder_q
));
387 u64_stats_update_begin(&sstats
->syncp
);
388 sstats
->rx_oos_packets
++;
389 u64_stats_update_end(&sstats
->syncp
);
394 __skb_queue_tail(&session
->reorder_q
, skb
);
397 spin_unlock_bh(&session
->reorder_q
.lock
);
400 /* Dequeue a single skb.
402 static void l2tp_recv_dequeue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
404 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
405 int length
= L2TP_SKB_CB(skb
)->length
;
406 struct l2tp_stats
*tstats
, *sstats
;
408 /* We're about to requeue the skb, so return resources
409 * to its current owner (a socket receive buffer).
413 tstats
= &tunnel
->stats
;
414 u64_stats_update_begin(&tstats
->syncp
);
415 sstats
= &session
->stats
;
416 u64_stats_update_begin(&sstats
->syncp
);
417 tstats
->rx_packets
++;
418 tstats
->rx_bytes
+= length
;
419 sstats
->rx_packets
++;
420 sstats
->rx_bytes
+= length
;
421 u64_stats_update_end(&tstats
->syncp
);
422 u64_stats_update_end(&sstats
->syncp
);
424 if (L2TP_SKB_CB(skb
)->has_seq
) {
427 if (tunnel
->version
== L2TP_HDR_VER_2
)
428 session
->nr
&= 0xffff;
430 session
->nr
&= 0xffffff;
432 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated nr to %hu\n",
433 session
->name
, session
->nr
);
436 /* call private receive handler */
437 if (session
->recv_skb
!= NULL
)
438 (*session
->recv_skb
)(session
, skb
, L2TP_SKB_CB(skb
)->length
);
443 (*session
->deref
)(session
);
446 /* Dequeue skbs from the session's reorder_q, subject to packet order.
447 * Skbs that have been in the queue for too long are simply discarded.
449 static void l2tp_recv_dequeue(struct l2tp_session
*session
)
453 struct l2tp_stats
*sstats
;
455 /* If the pkt at the head of the queue has the nr that we
456 * expect to send up next, dequeue it and any other
457 * in-sequence packets behind it.
460 spin_lock_bh(&session
->reorder_q
.lock
);
461 sstats
= &session
->stats
;
462 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
463 if (time_after(jiffies
, L2TP_SKB_CB(skb
)->expires
)) {
464 u64_stats_update_begin(&sstats
->syncp
);
465 sstats
->rx_seq_discards
++;
467 u64_stats_update_end(&sstats
->syncp
);
468 l2tp_dbg(session
, L2TP_MSG_SEQ
,
469 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
470 session
->name
, L2TP_SKB_CB(skb
)->ns
,
471 L2TP_SKB_CB(skb
)->length
, session
->nr
,
472 skb_queue_len(&session
->reorder_q
));
473 session
->reorder_skip
= 1;
474 __skb_unlink(skb
, &session
->reorder_q
);
477 (*session
->deref
)(session
);
481 if (L2TP_SKB_CB(skb
)->has_seq
) {
482 if (session
->reorder_skip
) {
483 l2tp_dbg(session
, L2TP_MSG_SEQ
,
484 "%s: advancing nr to next pkt: %u -> %u",
485 session
->name
, session
->nr
,
486 L2TP_SKB_CB(skb
)->ns
);
487 session
->reorder_skip
= 0;
488 session
->nr
= L2TP_SKB_CB(skb
)->ns
;
490 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
491 l2tp_dbg(session
, L2TP_MSG_SEQ
,
492 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
493 session
->name
, L2TP_SKB_CB(skb
)->ns
,
494 L2TP_SKB_CB(skb
)->length
, session
->nr
,
495 skb_queue_len(&session
->reorder_q
));
499 __skb_unlink(skb
, &session
->reorder_q
);
501 /* Process the skb. We release the queue lock while we
502 * do so to let other contexts process the queue.
504 spin_unlock_bh(&session
->reorder_q
.lock
);
505 l2tp_recv_dequeue_skb(session
, skb
);
510 spin_unlock_bh(&session
->reorder_q
.lock
);
513 static inline int l2tp_verify_udp_checksum(struct sock
*sk
,
516 struct udphdr
*uh
= udp_hdr(skb
);
517 u16 ulen
= ntohs(uh
->len
);
520 if (sk
->sk_no_check
|| skb_csum_unnecessary(skb
))
523 #if IS_ENABLED(CONFIG_IPV6)
524 if (sk
->sk_family
== PF_INET6
) {
526 LIMIT_NETDEBUG(KERN_INFO
"L2TP: IPv6: checksum is 0\n");
529 if ((skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
530 !csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
531 &ipv6_hdr(skb
)->daddr
, ulen
,
532 IPPROTO_UDP
, skb
->csum
)) {
533 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
536 skb
->csum
= ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
537 &ipv6_hdr(skb
)->daddr
,
538 skb
->len
, IPPROTO_UDP
,
543 struct inet_sock
*inet
;
547 psum
= csum_tcpudp_nofold(inet
->inet_saddr
, inet
->inet_daddr
,
548 ulen
, IPPROTO_UDP
, 0);
550 if ((skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
551 !csum_fold(csum_add(psum
, skb
->csum
)))
556 return __skb_checksum_complete(skb
);
559 /* Do receive processing of L2TP data frames. We handle both L2TPv2
560 * and L2TPv3 data frames here.
562 * L2TPv2 Data Message Header
565 * 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
566 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
567 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
568 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
569 * | Tunnel ID | Session ID |
570 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
571 * | Ns (opt) | Nr (opt) |
572 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
573 * | Offset Size (opt) | Offset pad... (opt)
574 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
576 * Data frames are marked by T=0. All other fields are the same as
577 * those in L2TP control frames.
579 * L2TPv3 Data Message Header
581 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
582 * | L2TP Session Header |
583 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
584 * | L2-Specific Sublayer |
585 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * | Tunnel Payload ...
587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 * L2TPv3 Session Header Over IP
592 * 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
593 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * | Cookie (optional, maximum 64 bits)...
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 * L2TPv3 L2-Specific Sublayer Format
604 * 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
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * |x|S|x|x|x|x|x|x| Sequence Number |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 * Cookie value, sublayer format and offset (pad) are negotiated with
610 * the peer when the session is set up. Unlike L2TPv2, we do not need
611 * to parse the packet header to determine if optional fields are
614 * Caller must already have parsed the frame and determined that it is
615 * a data (not control) frame before coming here. Fields up to the
616 * session-id have already been parsed and ptr points to the data
617 * after the session-id.
619 void l2tp_recv_common(struct l2tp_session
*session
, struct sk_buff
*skb
,
620 unsigned char *ptr
, unsigned char *optr
, u16 hdrflags
,
621 int length
, int (*payload_hook
)(struct sk_buff
*skb
))
623 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
626 struct l2tp_stats
*sstats
= &session
->stats
;
628 /* The ref count is increased since we now hold a pointer to
629 * the session. Take care to decrement the refcnt when exiting
630 * this function from now on...
632 l2tp_session_inc_refcount(session
);
634 (*session
->ref
)(session
);
636 /* Parse and check optional cookie */
637 if (session
->peer_cookie_len
> 0) {
638 if (memcmp(ptr
, &session
->peer_cookie
[0], session
->peer_cookie_len
)) {
639 l2tp_info(tunnel
, L2TP_MSG_DATA
,
640 "%s: cookie mismatch (%u/%u). Discarding.\n",
641 tunnel
->name
, tunnel
->tunnel_id
,
642 session
->session_id
);
643 u64_stats_update_begin(&sstats
->syncp
);
644 sstats
->rx_cookie_discards
++;
645 u64_stats_update_end(&sstats
->syncp
);
648 ptr
+= session
->peer_cookie_len
;
651 /* Handle the optional sequence numbers. Sequence numbers are
652 * in different places for L2TPv2 and L2TPv3.
654 * If we are the LAC, enable/disable sequence numbers under
655 * the control of the LNS. If no sequence numbers present but
656 * we were expecting them, discard frame.
659 L2TP_SKB_CB(skb
)->has_seq
= 0;
660 if (tunnel
->version
== L2TP_HDR_VER_2
) {
661 if (hdrflags
& L2TP_HDRFLAG_S
) {
662 ns
= ntohs(*(__be16
*) ptr
);
664 nr
= ntohs(*(__be16
*) ptr
);
667 /* Store L2TP info in the skb */
668 L2TP_SKB_CB(skb
)->ns
= ns
;
669 L2TP_SKB_CB(skb
)->has_seq
= 1;
671 l2tp_dbg(session
, L2TP_MSG_SEQ
,
672 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
673 session
->name
, ns
, nr
, session
->nr
);
675 } else if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
676 u32 l2h
= ntohl(*(__be32
*) ptr
);
678 if (l2h
& 0x40000000) {
679 ns
= l2h
& 0x00ffffff;
681 /* Store L2TP info in the skb */
682 L2TP_SKB_CB(skb
)->ns
= ns
;
683 L2TP_SKB_CB(skb
)->has_seq
= 1;
685 l2tp_dbg(session
, L2TP_MSG_SEQ
,
686 "%s: recv data ns=%u, session nr=%u\n",
687 session
->name
, ns
, session
->nr
);
691 /* Advance past L2-specific header, if present */
692 ptr
+= session
->l2specific_len
;
694 if (L2TP_SKB_CB(skb
)->has_seq
) {
695 /* Received a packet with sequence numbers. If we're the LNS,
696 * check if we sre sending sequence numbers and if not,
699 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
700 l2tp_info(session
, L2TP_MSG_SEQ
,
701 "%s: requested to enable seq numbers by LNS\n",
703 session
->send_seq
= -1;
704 l2tp_session_set_header_len(session
, tunnel
->version
);
707 /* No sequence numbers.
708 * If user has configured mandatory sequence numbers, discard.
710 if (session
->recv_seq
) {
711 l2tp_warn(session
, L2TP_MSG_SEQ
,
712 "%s: recv data has no seq numbers when required. Discarding.\n",
714 u64_stats_update_begin(&sstats
->syncp
);
715 sstats
->rx_seq_discards
++;
716 u64_stats_update_end(&sstats
->syncp
);
720 /* If we're the LAC and we're sending sequence numbers, the
721 * LNS has requested that we no longer send sequence numbers.
722 * If we're the LNS and we're sending sequence numbers, the
723 * LAC is broken. Discard the frame.
725 if ((!session
->lns_mode
) && (session
->send_seq
)) {
726 l2tp_info(session
, L2TP_MSG_SEQ
,
727 "%s: requested to disable seq numbers by LNS\n",
729 session
->send_seq
= 0;
730 l2tp_session_set_header_len(session
, tunnel
->version
);
731 } else if (session
->send_seq
) {
732 l2tp_warn(session
, L2TP_MSG_SEQ
,
733 "%s: recv data has no seq numbers when required. Discarding.\n",
735 u64_stats_update_begin(&sstats
->syncp
);
736 sstats
->rx_seq_discards
++;
737 u64_stats_update_end(&sstats
->syncp
);
742 /* Session data offset is handled differently for L2TPv2 and
743 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
744 * the header. For L2TPv3, the offset is negotiated using AVPs
745 * in the session setup control protocol.
747 if (tunnel
->version
== L2TP_HDR_VER_2
) {
748 /* If offset bit set, skip it. */
749 if (hdrflags
& L2TP_HDRFLAG_O
) {
750 offset
= ntohs(*(__be16
*)ptr
);
754 ptr
+= session
->offset
;
757 if (!pskb_may_pull(skb
, offset
))
760 __skb_pull(skb
, offset
);
762 /* If caller wants to process the payload before we queue the
766 if ((*payload_hook
)(skb
))
769 /* Prepare skb for adding to the session's reorder_q. Hold
770 * packets for max reorder_timeout or 1 second if not
773 L2TP_SKB_CB(skb
)->length
= length
;
774 L2TP_SKB_CB(skb
)->expires
= jiffies
+
775 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
777 /* Add packet to the session's receive queue. Reordering is done here, if
778 * enabled. Saved L2TP protocol info is stored in skb->sb[].
780 if (L2TP_SKB_CB(skb
)->has_seq
) {
781 if (session
->reorder_timeout
!= 0) {
782 /* Packet reordering enabled. Add skb to session's
783 * reorder queue, in order of ns.
785 l2tp_recv_queue_skb(session
, skb
);
787 /* Packet reordering disabled. Discard out-of-sequence
790 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
791 u64_stats_update_begin(&sstats
->syncp
);
792 sstats
->rx_seq_discards
++;
793 u64_stats_update_end(&sstats
->syncp
);
794 l2tp_dbg(session
, L2TP_MSG_SEQ
,
795 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
796 session
->name
, L2TP_SKB_CB(skb
)->ns
,
797 L2TP_SKB_CB(skb
)->length
, session
->nr
,
798 skb_queue_len(&session
->reorder_q
));
801 skb_queue_tail(&session
->reorder_q
, skb
);
804 /* No sequence numbers. Add the skb to the tail of the
805 * reorder queue. This ensures that it will be
806 * delivered after all previous sequenced skbs.
808 skb_queue_tail(&session
->reorder_q
, skb
);
811 /* Try to dequeue as many skbs from reorder_q as we can. */
812 l2tp_recv_dequeue(session
);
814 l2tp_session_dec_refcount(session
);
819 u64_stats_update_begin(&sstats
->syncp
);
821 u64_stats_update_end(&sstats
->syncp
);
825 (*session
->deref
)(session
);
827 l2tp_session_dec_refcount(session
);
829 EXPORT_SYMBOL(l2tp_recv_common
);
831 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
832 * here. The skb is not on a list when we get here.
833 * Returns 0 if the packet was a data packet and was successfully passed on.
834 * Returns 1 if the packet was not a good data packet and could not be
835 * forwarded. All such packets are passed up to userspace to deal with.
837 static int l2tp_udp_recv_core(struct l2tp_tunnel
*tunnel
, struct sk_buff
*skb
,
838 int (*payload_hook
)(struct sk_buff
*skb
))
840 struct l2tp_session
*session
= NULL
;
841 unsigned char *ptr
, *optr
;
843 u32 tunnel_id
, session_id
;
846 struct l2tp_stats
*tstats
;
848 if (tunnel
->sock
&& l2tp_verify_udp_checksum(tunnel
->sock
, skb
))
849 goto discard_bad_csum
;
851 /* UDP always verifies the packet length. */
852 __skb_pull(skb
, sizeof(struct udphdr
));
855 if (!pskb_may_pull(skb
, L2TP_HDR_SIZE_SEQ
)) {
856 l2tp_info(tunnel
, L2TP_MSG_DATA
,
857 "%s: recv short packet (len=%d)\n",
858 tunnel
->name
, skb
->len
);
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
))
868 pr_debug("%s: recv\n", tunnel
->name
);
869 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
872 /* Point to L2TP header */
873 optr
= ptr
= skb
->data
;
875 /* Get L2TP header flags */
876 hdrflags
= ntohs(*(__be16
*) ptr
);
878 /* Check protocol version */
879 version
= hdrflags
& L2TP_HDR_VER_MASK
;
880 if (version
!= tunnel
->version
) {
881 l2tp_info(tunnel
, L2TP_MSG_DATA
,
882 "%s: recv protocol version mismatch: got %d expected %d\n",
883 tunnel
->name
, version
, tunnel
->version
);
887 /* Get length of L2TP packet */
890 /* If type is control packet, it is handled by userspace. */
891 if (hdrflags
& L2TP_HDRFLAG_T
) {
892 l2tp_dbg(tunnel
, L2TP_MSG_DATA
,
893 "%s: recv control packet, len=%d\n",
894 tunnel
->name
, length
);
901 if (tunnel
->version
== L2TP_HDR_VER_2
) {
902 /* If length is present, skip it */
903 if (hdrflags
& L2TP_HDRFLAG_L
)
906 /* Extract tunnel and session ID */
907 tunnel_id
= ntohs(*(__be16
*) ptr
);
909 session_id
= ntohs(*(__be16
*) ptr
);
912 ptr
+= 2; /* skip reserved bits */
913 tunnel_id
= tunnel
->tunnel_id
;
914 session_id
= ntohl(*(__be32
*) ptr
);
918 /* Find the session context */
919 session
= l2tp_session_find(tunnel
->l2tp_net
, tunnel
, session_id
);
920 if (!session
|| !session
->recv_skb
) {
921 /* Not found? Pass to userspace to deal with */
922 l2tp_info(tunnel
, L2TP_MSG_DATA
,
923 "%s: no session found (%u/%u). Passing up.\n",
924 tunnel
->name
, tunnel_id
, session_id
);
928 l2tp_recv_common(session
, skb
, ptr
, optr
, hdrflags
, length
, payload_hook
);
933 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel
->name
);
934 UDP_INC_STATS_USER(tunnel
->l2tp_net
, UDP_MIB_INERRORS
, 0);
935 tstats
= &tunnel
->stats
;
936 u64_stats_update_begin(&tstats
->syncp
);
938 u64_stats_update_end(&tstats
->syncp
);
944 /* Put UDP header back */
945 __skb_push(skb
, sizeof(struct udphdr
));
950 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
954 * >0: skb should be passed up to userspace as UDP.
956 int l2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
958 struct l2tp_tunnel
*tunnel
;
960 tunnel
= l2tp_sock_to_tunnel(sk
);
964 l2tp_dbg(tunnel
, L2TP_MSG_DATA
, "%s: received %d bytes\n",
965 tunnel
->name
, skb
->len
);
967 if (l2tp_udp_recv_core(tunnel
, skb
, tunnel
->recv_payload_hook
))
978 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv
);
980 /************************************************************************
982 ***********************************************************************/
984 /* Build an L2TP header for the session into the buffer provided.
986 static int l2tp_build_l2tpv2_header(struct l2tp_session
*session
, void *buf
)
988 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
991 u16 flags
= L2TP_HDR_VER_2
;
992 u32 tunnel_id
= tunnel
->peer_tunnel_id
;
993 u32 session_id
= session
->peer_session_id
;
995 if (session
->send_seq
)
996 flags
|= L2TP_HDRFLAG_S
;
998 /* Setup L2TP header. */
999 *bufp
++ = htons(flags
);
1000 *bufp
++ = htons(tunnel_id
);
1001 *bufp
++ = htons(session_id
);
1002 if (session
->send_seq
) {
1003 *bufp
++ = htons(session
->ns
);
1006 session
->ns
&= 0xffff;
1007 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated ns to %u\n",
1008 session
->name
, session
->ns
);
1014 static int l2tp_build_l2tpv3_header(struct l2tp_session
*session
, void *buf
)
1016 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1020 /* Setup L2TP header. The header differs slightly for UDP and
1021 * IP encapsulations. For UDP, there is 4 bytes of flags.
1023 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
1024 u16 flags
= L2TP_HDR_VER_3
;
1025 *((__be16
*) bufp
) = htons(flags
);
1027 *((__be16
*) bufp
) = 0;
1031 *((__be32
*) bufp
) = htonl(session
->peer_session_id
);
1033 if (session
->cookie_len
) {
1034 memcpy(bufp
, &session
->cookie
[0], session
->cookie_len
);
1035 bufp
+= session
->cookie_len
;
1037 if (session
->l2specific_len
) {
1038 if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
1040 if (session
->send_seq
) {
1041 l2h
= 0x40000000 | session
->ns
;
1043 session
->ns
&= 0xffffff;
1044 l2tp_dbg(session
, L2TP_MSG_SEQ
,
1045 "%s: updated ns to %u\n",
1046 session
->name
, session
->ns
);
1049 *((__be32
*) bufp
) = htonl(l2h
);
1051 bufp
+= session
->l2specific_len
;
1053 if (session
->offset
)
1054 bufp
+= session
->offset
;
1059 static int l2tp_xmit_core(struct l2tp_session
*session
, struct sk_buff
*skb
,
1060 struct flowi
*fl
, size_t data_len
)
1062 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1063 unsigned int len
= skb
->len
;
1065 struct l2tp_stats
*tstats
, *sstats
;
1068 if (session
->send_seq
)
1069 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %Zd bytes, ns=%u\n",
1070 session
->name
, data_len
, session
->ns
- 1);
1072 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %Zd bytes\n",
1073 session
->name
, data_len
);
1075 if (session
->debug
& L2TP_MSG_DATA
) {
1076 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1077 unsigned char *datap
= skb
->data
+ uhlen
;
1079 pr_debug("%s: xmit\n", session
->name
);
1080 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1081 datap
, min_t(size_t, 32, len
- uhlen
));
1084 /* Queue the packet to IP for output */
1086 #if IS_ENABLED(CONFIG_IPV6)
1087 if (skb
->sk
->sk_family
== PF_INET6
)
1088 error
= inet6_csk_xmit(skb
, NULL
);
1091 error
= ip_queue_xmit(skb
, fl
);
1094 tstats
= &tunnel
->stats
;
1095 u64_stats_update_begin(&tstats
->syncp
);
1096 sstats
= &session
->stats
;
1097 u64_stats_update_begin(&sstats
->syncp
);
1099 tstats
->tx_packets
++;
1100 tstats
->tx_bytes
+= len
;
1101 sstats
->tx_packets
++;
1102 sstats
->tx_bytes
+= len
;
1104 tstats
->tx_errors
++;
1105 sstats
->tx_errors
++;
1107 u64_stats_update_end(&tstats
->syncp
);
1108 u64_stats_update_end(&sstats
->syncp
);
1113 /* Automatically called when the skb is freed.
1115 static void l2tp_sock_wfree(struct sk_buff
*skb
)
1120 /* For data skbs that we transmit, we associate with the tunnel socket
1121 * but don't do accounting.
1123 static inline void l2tp_skb_set_owner_w(struct sk_buff
*skb
, struct sock
*sk
)
1127 skb
->destructor
= l2tp_sock_wfree
;
1130 #if IS_ENABLED(CONFIG_IPV6)
1131 static void l2tp_xmit_ipv6_csum(struct sock
*sk
, struct sk_buff
*skb
,
1134 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1135 struct udphdr
*uh
= udp_hdr(skb
);
1137 if (!skb_dst(skb
) || !skb_dst(skb
)->dev
||
1138 !(skb_dst(skb
)->dev
->features
& NETIF_F_IPV6_CSUM
)) {
1139 __wsum csum
= skb_checksum(skb
, 0, udp_len
, 0);
1140 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1141 uh
->check
= csum_ipv6_magic(&np
->saddr
, &np
->daddr
, udp_len
,
1144 uh
->check
= CSUM_MANGLED_0
;
1146 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1147 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
1148 skb
->csum_offset
= offsetof(struct udphdr
, check
);
1149 uh
->check
= ~csum_ipv6_magic(&np
->saddr
, &np
->daddr
,
1150 udp_len
, IPPROTO_UDP
, 0);
1155 /* If caller requires the skb to have a ppp header, the header must be
1156 * inserted in the skb data before calling this function.
1158 int l2tp_xmit_skb(struct l2tp_session
*session
, struct sk_buff
*skb
, int hdr_len
)
1160 int data_len
= skb
->len
;
1161 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1162 struct sock
*sk
= tunnel
->sock
;
1165 struct inet_sock
*inet
;
1168 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1170 int ret
= NET_XMIT_SUCCESS
;
1172 /* Check that there's enough headroom in the skb to insert IP,
1173 * UDP and L2TP headers. If not enough, expand it to
1174 * make room. Adjust truesize.
1176 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1178 if (skb_cow_head(skb
, headroom
)) {
1180 return NET_XMIT_DROP
;
1184 /* Setup L2TP header */
1185 session
->build_header(session
, __skb_push(skb
, hdr_len
));
1187 /* Reset skb netfilter state */
1188 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1189 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1194 if (sock_owned_by_user(sk
)) {
1196 ret
= NET_XMIT_DROP
;
1200 /* Get routing info from the tunnel socket */
1202 skb_dst_set(skb
, dst_clone(__sk_dst_check(sk
, 0)));
1205 fl
= &inet
->cork
.fl
;
1206 switch (tunnel
->encap
) {
1207 case L2TP_ENCAPTYPE_UDP
:
1208 /* Setup UDP header */
1209 __skb_push(skb
, sizeof(*uh
));
1210 skb_reset_transport_header(skb
);
1212 uh
->source
= inet
->inet_sport
;
1213 uh
->dest
= inet
->inet_dport
;
1214 udp_len
= uhlen
+ hdr_len
+ data_len
;
1215 uh
->len
= htons(udp_len
);
1218 /* Calculate UDP checksum if configured to do so */
1219 #if IS_ENABLED(CONFIG_IPV6)
1220 if (sk
->sk_family
== PF_INET6
)
1221 l2tp_xmit_ipv6_csum(sk
, skb
, udp_len
);
1224 if (sk
->sk_no_check
== UDP_CSUM_NOXMIT
)
1225 skb
->ip_summed
= CHECKSUM_NONE
;
1226 else if ((skb_dst(skb
) && skb_dst(skb
)->dev
) &&
1227 (!(skb_dst(skb
)->dev
->features
& NETIF_F_V4_CSUM
))) {
1228 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1229 csum
= skb_checksum(skb
, 0, udp_len
, 0);
1230 uh
->check
= csum_tcpudp_magic(inet
->inet_saddr
,
1232 udp_len
, IPPROTO_UDP
, csum
);
1234 uh
->check
= CSUM_MANGLED_0
;
1236 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1237 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
1238 skb
->csum_offset
= offsetof(struct udphdr
, check
);
1239 uh
->check
= ~csum_tcpudp_magic(inet
->inet_saddr
,
1241 udp_len
, IPPROTO_UDP
, 0);
1245 case L2TP_ENCAPTYPE_IP
:
1249 l2tp_skb_set_owner_w(skb
, sk
);
1251 l2tp_xmit_core(session
, skb
, fl
, data_len
);
1257 EXPORT_SYMBOL_GPL(l2tp_xmit_skb
);
1259 /*****************************************************************************
1260 * Tinnel and session create/destroy.
1261 *****************************************************************************/
1263 /* Tunnel socket destruct hook.
1264 * The tunnel context is deleted only when all session sockets have been
1267 static void l2tp_tunnel_destruct(struct sock
*sk
)
1269 struct l2tp_tunnel
*tunnel
;
1270 struct l2tp_net
*pn
;
1272 tunnel
= sk
->sk_user_data
;
1276 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing...\n", tunnel
->name
);
1279 /* Disable udp encapsulation */
1280 switch (tunnel
->encap
) {
1281 case L2TP_ENCAPTYPE_UDP
:
1282 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1283 (udp_sk(sk
))->encap_type
= 0;
1284 (udp_sk(sk
))->encap_rcv
= NULL
;
1286 case L2TP_ENCAPTYPE_IP
:
1290 /* Remove hooks into tunnel socket */
1291 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1292 sk
->sk_user_data
= NULL
;
1293 tunnel
->sock
= NULL
;
1295 /* Remove the tunnel struct from the tunnel list */
1296 pn
= l2tp_pernet(tunnel
->l2tp_net
);
1297 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1298 list_del_rcu(&tunnel
->list
);
1299 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1300 atomic_dec(&l2tp_tunnel_count
);
1302 l2tp_tunnel_closeall(tunnel
);
1303 l2tp_tunnel_dec_refcount(tunnel
);
1305 /* Call the original destructor */
1306 if (sk
->sk_destruct
)
1307 (*sk
->sk_destruct
)(sk
);
1312 /* When the tunnel is closed, all the attached sessions need to go too.
1314 static void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
)
1317 struct hlist_node
*walk
;
1318 struct hlist_node
*tmp
;
1319 struct l2tp_session
*session
;
1321 BUG_ON(tunnel
== NULL
);
1323 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing all sessions...\n",
1326 write_lock_bh(&tunnel
->hlist_lock
);
1327 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
1329 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1330 session
= hlist_entry(walk
, struct l2tp_session
, hlist
);
1332 l2tp_info(session
, L2TP_MSG_CONTROL
,
1333 "%s: closing session\n", session
->name
);
1335 hlist_del_init(&session
->hlist
);
1337 /* Since we should hold the sock lock while
1338 * doing any unbinding, we need to release the
1339 * lock we're holding before taking that lock.
1340 * Hold a reference to the sock so it doesn't
1341 * disappear as we're jumping between locks.
1343 if (session
->ref
!= NULL
)
1344 (*session
->ref
)(session
);
1346 write_unlock_bh(&tunnel
->hlist_lock
);
1348 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1349 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1351 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1352 hlist_del_init_rcu(&session
->global_hlist
);
1353 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1357 if (session
->session_close
!= NULL
)
1358 (*session
->session_close
)(session
);
1360 if (session
->deref
!= NULL
)
1361 (*session
->deref
)(session
);
1363 write_lock_bh(&tunnel
->hlist_lock
);
1365 /* Now restart from the beginning of this hash
1366 * chain. We always remove a session from the
1367 * list so we are guaranteed to make forward
1373 write_unlock_bh(&tunnel
->hlist_lock
);
1376 /* Really kill the tunnel.
1377 * Come here only when all sessions have been cleared from the tunnel.
1379 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
)
1381 BUG_ON(atomic_read(&tunnel
->ref_count
) != 0);
1382 BUG_ON(tunnel
->sock
!= NULL
);
1383 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: free...\n", tunnel
->name
);
1384 kfree_rcu(tunnel
, rcu
);
1387 /* Workqueue tunnel deletion function */
1388 static void l2tp_tunnel_del_work(struct work_struct
*work
)
1390 struct l2tp_tunnel
*tunnel
= NULL
;
1391 struct socket
*sock
= NULL
;
1392 struct sock
*sk
= NULL
;
1394 tunnel
= container_of(work
, struct l2tp_tunnel
, del_work
);
1395 sk
= l2tp_tunnel_sock_lookup(tunnel
);
1399 sock
= sk
->sk_socket
;
1402 /* If the tunnel socket was created directly by the kernel, use the
1403 * sk_* API to release the socket now. Otherwise go through the
1404 * inet_* layer to shut the socket down, and let userspace close it.
1405 * In either case the tunnel resources are freed in the socket
1406 * destructor when the tunnel socket goes away.
1408 if (sock
->file
== NULL
) {
1409 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1410 sk_release_kernel(sk
);
1412 inet_shutdown(sock
, 2);
1415 l2tp_tunnel_sock_put(sk
);
1418 /* Create a socket for the tunnel, if one isn't set up by
1419 * userspace. This is used for static tunnels where there is no
1420 * managing L2TP daemon.
1422 * Since we don't want these sockets to keep a namespace alive by
1423 * themselves, we drop the socket's namespace refcount after creation.
1424 * These sockets are freed when the namespace exits using the pernet
1427 static int l2tp_tunnel_sock_create(struct net
*net
,
1430 struct l2tp_tunnel_cfg
*cfg
,
1431 struct socket
**sockp
)
1434 struct socket
*sock
= NULL
;
1435 struct sockaddr_in udp_addr
= {0};
1436 struct sockaddr_l2tpip ip_addr
= {0};
1437 #if IS_ENABLED(CONFIG_IPV6)
1438 struct sockaddr_in6 udp6_addr
= {0};
1439 struct sockaddr_l2tpip6 ip6_addr
= {0};
1442 switch (cfg
->encap
) {
1443 case L2TP_ENCAPTYPE_UDP
:
1444 #if IS_ENABLED(CONFIG_IPV6)
1445 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1446 err
= sock_create_kern(AF_INET6
, SOCK_DGRAM
, 0, &sock
);
1450 sk_change_net(sock
->sk
, net
);
1452 udp6_addr
.sin6_family
= AF_INET6
;
1453 memcpy(&udp6_addr
.sin6_addr
, cfg
->local_ip6
,
1454 sizeof(udp6_addr
.sin6_addr
));
1455 udp6_addr
.sin6_port
= htons(cfg
->local_udp_port
);
1456 err
= kernel_bind(sock
, (struct sockaddr
*) &udp6_addr
,
1461 udp6_addr
.sin6_family
= AF_INET6
;
1462 memcpy(&udp6_addr
.sin6_addr
, cfg
->peer_ip6
,
1463 sizeof(udp6_addr
.sin6_addr
));
1464 udp6_addr
.sin6_port
= htons(cfg
->peer_udp_port
);
1465 err
= kernel_connect(sock
,
1466 (struct sockaddr
*) &udp6_addr
,
1467 sizeof(udp6_addr
), 0);
1473 err
= sock_create_kern(AF_INET
, SOCK_DGRAM
, 0, &sock
);
1477 sk_change_net(sock
->sk
, net
);
1479 udp_addr
.sin_family
= AF_INET
;
1480 udp_addr
.sin_addr
= cfg
->local_ip
;
1481 udp_addr
.sin_port
= htons(cfg
->local_udp_port
);
1482 err
= kernel_bind(sock
, (struct sockaddr
*) &udp_addr
,
1487 udp_addr
.sin_family
= AF_INET
;
1488 udp_addr
.sin_addr
= cfg
->peer_ip
;
1489 udp_addr
.sin_port
= htons(cfg
->peer_udp_port
);
1490 err
= kernel_connect(sock
,
1491 (struct sockaddr
*) &udp_addr
,
1492 sizeof(udp_addr
), 0);
1497 if (!cfg
->use_udp_checksums
)
1498 sock
->sk
->sk_no_check
= UDP_CSUM_NOXMIT
;
1502 case L2TP_ENCAPTYPE_IP
:
1503 #if IS_ENABLED(CONFIG_IPV6)
1504 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1505 err
= sock_create_kern(AF_INET6
, SOCK_DGRAM
,
1506 IPPROTO_L2TP
, &sock
);
1510 sk_change_net(sock
->sk
, net
);
1512 ip6_addr
.l2tp_family
= AF_INET6
;
1513 memcpy(&ip6_addr
.l2tp_addr
, cfg
->local_ip6
,
1514 sizeof(ip6_addr
.l2tp_addr
));
1515 ip6_addr
.l2tp_conn_id
= tunnel_id
;
1516 err
= kernel_bind(sock
, (struct sockaddr
*) &ip6_addr
,
1521 ip6_addr
.l2tp_family
= AF_INET6
;
1522 memcpy(&ip6_addr
.l2tp_addr
, cfg
->peer_ip6
,
1523 sizeof(ip6_addr
.l2tp_addr
));
1524 ip6_addr
.l2tp_conn_id
= peer_tunnel_id
;
1525 err
= kernel_connect(sock
,
1526 (struct sockaddr
*) &ip6_addr
,
1527 sizeof(ip6_addr
), 0);
1533 err
= sock_create_kern(AF_INET
, SOCK_DGRAM
,
1534 IPPROTO_L2TP
, &sock
);
1538 sk_change_net(sock
->sk
, net
);
1540 ip_addr
.l2tp_family
= AF_INET
;
1541 ip_addr
.l2tp_addr
= cfg
->local_ip
;
1542 ip_addr
.l2tp_conn_id
= tunnel_id
;
1543 err
= kernel_bind(sock
, (struct sockaddr
*) &ip_addr
,
1548 ip_addr
.l2tp_family
= AF_INET
;
1549 ip_addr
.l2tp_addr
= cfg
->peer_ip
;
1550 ip_addr
.l2tp_conn_id
= peer_tunnel_id
;
1551 err
= kernel_connect(sock
, (struct sockaddr
*) &ip_addr
,
1552 sizeof(ip_addr
), 0);
1564 if ((err
< 0) && sock
) {
1565 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1566 sk_release_kernel(sock
->sk
);
1573 static struct lock_class_key l2tp_socket_class
;
1575 int 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
)
1577 struct l2tp_tunnel
*tunnel
= NULL
;
1579 struct socket
*sock
= NULL
;
1580 struct sock
*sk
= NULL
;
1581 struct l2tp_net
*pn
;
1582 enum l2tp_encap_type encap
= L2TP_ENCAPTYPE_UDP
;
1584 /* Get the tunnel socket from the fd, which was opened by
1585 * the userspace L2TP daemon. If not specified, create a
1589 err
= l2tp_tunnel_sock_create(net
, tunnel_id
, peer_tunnel_id
,
1594 sock
= sockfd_lookup(fd
, &err
);
1596 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1597 tunnel_id
, fd
, err
);
1602 /* Reject namespace mismatches */
1603 if (!net_eq(sock_net(sock
->sk
), net
)) {
1604 pr_err("tunl %u: netns mismatch\n", tunnel_id
);
1615 /* Quick sanity checks */
1617 case L2TP_ENCAPTYPE_UDP
:
1618 err
= -EPROTONOSUPPORT
;
1619 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1620 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1621 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1625 case L2TP_ENCAPTYPE_IP
:
1626 err
= -EPROTONOSUPPORT
;
1627 if (sk
->sk_protocol
!= IPPROTO_L2TP
) {
1628 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1629 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_L2TP
);
1635 /* Check if this socket has already been prepped */
1636 tunnel
= (struct l2tp_tunnel
*)sk
->sk_user_data
;
1637 if (tunnel
!= NULL
) {
1638 /* This socket has already been prepped */
1643 tunnel
= kzalloc(sizeof(struct l2tp_tunnel
), GFP_KERNEL
);
1644 if (tunnel
== NULL
) {
1649 tunnel
->version
= version
;
1650 tunnel
->tunnel_id
= tunnel_id
;
1651 tunnel
->peer_tunnel_id
= peer_tunnel_id
;
1652 tunnel
->debug
= L2TP_DEFAULT_DEBUG_FLAGS
;
1654 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1655 sprintf(&tunnel
->name
[0], "tunl %u", tunnel_id
);
1656 rwlock_init(&tunnel
->hlist_lock
);
1658 /* The net we belong to */
1659 tunnel
->l2tp_net
= net
;
1660 pn
= l2tp_pernet(net
);
1663 tunnel
->debug
= cfg
->debug
;
1665 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1666 tunnel
->encap
= encap
;
1667 if (encap
== L2TP_ENCAPTYPE_UDP
) {
1668 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1669 udp_sk(sk
)->encap_type
= UDP_ENCAP_L2TPINUDP
;
1670 udp_sk(sk
)->encap_rcv
= l2tp_udp_encap_recv
;
1671 #if IS_ENABLED(CONFIG_IPV6)
1672 if (sk
->sk_family
== PF_INET6
)
1673 udpv6_encap_enable();
1679 sk
->sk_user_data
= tunnel
;
1681 /* Hook on the tunnel socket destructor so that we can cleanup
1682 * if the tunnel socket goes away.
1684 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1685 sk
->sk_destruct
= &l2tp_tunnel_destruct
;
1688 lockdep_set_class_and_name(&sk
->sk_lock
.slock
, &l2tp_socket_class
, "l2tp_sock");
1690 sk
->sk_allocation
= GFP_ATOMIC
;
1692 /* Init delete workqueue struct */
1693 INIT_WORK(&tunnel
->del_work
, l2tp_tunnel_del_work
);
1695 /* Add tunnel to our list */
1696 INIT_LIST_HEAD(&tunnel
->list
);
1697 atomic_inc(&l2tp_tunnel_count
);
1699 /* Bump the reference count. The tunnel context is deleted
1700 * only when this drops to zero. Must be done before list insertion
1702 l2tp_tunnel_inc_refcount(tunnel
);
1703 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1704 list_add_rcu(&tunnel
->list
, &pn
->l2tp_tunnel_list
);
1705 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1712 /* If tunnel's socket was created by the kernel, it doesn't
1715 if (sock
&& sock
->file
)
1720 EXPORT_SYMBOL_GPL(l2tp_tunnel_create
);
1722 /* This function is used by the netlink TUNNEL_DELETE command.
1724 int l2tp_tunnel_delete(struct l2tp_tunnel
*tunnel
)
1726 return (false == queue_work(l2tp_wq
, &tunnel
->del_work
));
1728 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete
);
1730 /* Really kill the session.
1732 void l2tp_session_free(struct l2tp_session
*session
)
1734 struct l2tp_tunnel
*tunnel
;
1736 BUG_ON(atomic_read(&session
->ref_count
) != 0);
1738 tunnel
= session
->tunnel
;
1739 if (tunnel
!= NULL
) {
1740 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1742 /* Delete the session from the hash */
1743 write_lock_bh(&tunnel
->hlist_lock
);
1744 hlist_del_init(&session
->hlist
);
1745 write_unlock_bh(&tunnel
->hlist_lock
);
1747 /* Unlink from the global hash if not L2TPv2 */
1748 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1749 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1751 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1752 hlist_del_init_rcu(&session
->global_hlist
);
1753 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1757 if (session
->session_id
!= 0)
1758 atomic_dec(&l2tp_session_count
);
1760 sock_put(tunnel
->sock
);
1762 /* This will delete the tunnel context if this
1763 * is the last session on the tunnel.
1765 session
->tunnel
= NULL
;
1766 l2tp_tunnel_dec_refcount(tunnel
);
1773 EXPORT_SYMBOL_GPL(l2tp_session_free
);
1775 /* This function is used by the netlink SESSION_DELETE command and by
1778 int l2tp_session_delete(struct l2tp_session
*session
)
1780 if (session
->session_close
!= NULL
)
1781 (*session
->session_close
)(session
);
1783 l2tp_session_dec_refcount(session
);
1787 EXPORT_SYMBOL_GPL(l2tp_session_delete
);
1790 /* We come here whenever a session's send_seq, cookie_len or
1791 * l2specific_len parameters are set.
1793 static void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
)
1795 if (version
== L2TP_HDR_VER_2
) {
1796 session
->hdr_len
= 6;
1797 if (session
->send_seq
)
1798 session
->hdr_len
+= 4;
1800 session
->hdr_len
= 4 + session
->cookie_len
+ session
->l2specific_len
+ session
->offset
;
1801 if (session
->tunnel
->encap
== L2TP_ENCAPTYPE_UDP
)
1802 session
->hdr_len
+= 4;
1807 struct l2tp_session
*l2tp_session_create(int priv_size
, struct l2tp_tunnel
*tunnel
, u32 session_id
, u32 peer_session_id
, struct l2tp_session_cfg
*cfg
)
1809 struct l2tp_session
*session
;
1811 session
= kzalloc(sizeof(struct l2tp_session
) + priv_size
, GFP_KERNEL
);
1812 if (session
!= NULL
) {
1813 session
->magic
= L2TP_SESSION_MAGIC
;
1814 session
->tunnel
= tunnel
;
1816 session
->session_id
= session_id
;
1817 session
->peer_session_id
= peer_session_id
;
1820 sprintf(&session
->name
[0], "sess %u/%u",
1821 tunnel
->tunnel_id
, session
->session_id
);
1823 skb_queue_head_init(&session
->reorder_q
);
1825 INIT_HLIST_NODE(&session
->hlist
);
1826 INIT_HLIST_NODE(&session
->global_hlist
);
1828 /* Inherit debug options from tunnel */
1829 session
->debug
= tunnel
->debug
;
1832 session
->pwtype
= cfg
->pw_type
;
1833 session
->debug
= cfg
->debug
;
1834 session
->mtu
= cfg
->mtu
;
1835 session
->mru
= cfg
->mru
;
1836 session
->send_seq
= cfg
->send_seq
;
1837 session
->recv_seq
= cfg
->recv_seq
;
1838 session
->lns_mode
= cfg
->lns_mode
;
1839 session
->reorder_timeout
= cfg
->reorder_timeout
;
1840 session
->offset
= cfg
->offset
;
1841 session
->l2specific_type
= cfg
->l2specific_type
;
1842 session
->l2specific_len
= cfg
->l2specific_len
;
1843 session
->cookie_len
= cfg
->cookie_len
;
1844 memcpy(&session
->cookie
[0], &cfg
->cookie
[0], cfg
->cookie_len
);
1845 session
->peer_cookie_len
= cfg
->peer_cookie_len
;
1846 memcpy(&session
->peer_cookie
[0], &cfg
->peer_cookie
[0], cfg
->peer_cookie_len
);
1849 if (tunnel
->version
== L2TP_HDR_VER_2
)
1850 session
->build_header
= l2tp_build_l2tpv2_header
;
1852 session
->build_header
= l2tp_build_l2tpv3_header
;
1854 l2tp_session_set_header_len(session
, tunnel
->version
);
1856 /* Bump the reference count. The session context is deleted
1857 * only when this drops to zero.
1859 l2tp_session_inc_refcount(session
);
1860 l2tp_tunnel_inc_refcount(tunnel
);
1862 /* Ensure tunnel socket isn't deleted */
1863 sock_hold(tunnel
->sock
);
1865 /* Add session to the tunnel's hash list */
1866 write_lock_bh(&tunnel
->hlist_lock
);
1867 hlist_add_head(&session
->hlist
,
1868 l2tp_session_id_hash(tunnel
, session_id
));
1869 write_unlock_bh(&tunnel
->hlist_lock
);
1871 /* And to the global session list if L2TPv3 */
1872 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1873 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1875 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1876 hlist_add_head_rcu(&session
->global_hlist
,
1877 l2tp_session_id_hash_2(pn
, session_id
));
1878 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1881 /* Ignore management session in session count value */
1882 if (session
->session_id
!= 0)
1883 atomic_inc(&l2tp_session_count
);
1888 EXPORT_SYMBOL_GPL(l2tp_session_create
);
1890 /*****************************************************************************
1892 *****************************************************************************/
1894 static __net_init
int l2tp_init_net(struct net
*net
)
1896 struct l2tp_net
*pn
= net_generic(net
, l2tp_net_id
);
1899 INIT_LIST_HEAD(&pn
->l2tp_tunnel_list
);
1900 spin_lock_init(&pn
->l2tp_tunnel_list_lock
);
1902 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++)
1903 INIT_HLIST_HEAD(&pn
->l2tp_session_hlist
[hash
]);
1905 spin_lock_init(&pn
->l2tp_session_hlist_lock
);
1910 static __net_exit
void l2tp_exit_net(struct net
*net
)
1912 struct l2tp_net
*pn
= l2tp_pernet(net
);
1913 struct l2tp_tunnel
*tunnel
= NULL
;
1916 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
1917 (void)l2tp_tunnel_delete(tunnel
);
1919 rcu_read_unlock_bh();
1922 static struct pernet_operations l2tp_net_ops
= {
1923 .init
= l2tp_init_net
,
1924 .exit
= l2tp_exit_net
,
1926 .size
= sizeof(struct l2tp_net
),
1929 static int __init
l2tp_init(void)
1933 rc
= register_pernet_device(&l2tp_net_ops
);
1937 l2tp_wq
= alloc_workqueue("l2tp", WQ_NON_REENTRANT
| WQ_UNBOUND
, 0);
1939 pr_err("alloc_workqueue failed\n");
1944 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION
);
1950 static void __exit
l2tp_exit(void)
1952 unregister_pernet_device(&l2tp_net_ops
);
1954 destroy_workqueue(l2tp_wq
);
1959 module_init(l2tp_init
);
1960 module_exit(l2tp_exit
);
1962 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1963 MODULE_DESCRIPTION("L2TP core");
1964 MODULE_LICENSE("GPL");
1965 MODULE_VERSION(L2TP_DRV_VERSION
);