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/udp_tunnel.h>
56 #include <net/inet_common.h>
58 #include <net/protocol.h>
59 #include <net/inet6_connection_sock.h>
60 #include <net/inet_ecn.h>
61 #include <net/ip6_route.h>
62 #include <net/ip6_checksum.h>
64 #include <asm/byteorder.h>
65 #include <linux/atomic.h>
67 #include "l2tp_core.h"
69 #define L2TP_DRV_VERSION "V2.0"
71 /* L2TP header constants */
72 #define L2TP_HDRFLAG_T 0x8000
73 #define L2TP_HDRFLAG_L 0x4000
74 #define L2TP_HDRFLAG_S 0x0800
75 #define L2TP_HDRFLAG_O 0x0200
76 #define L2TP_HDRFLAG_P 0x0100
78 #define L2TP_HDR_VER_MASK 0x000F
79 #define L2TP_HDR_VER_2 0x0002
80 #define L2TP_HDR_VER_3 0x0003
82 /* L2TPv3 default L2-specific sublayer */
83 #define L2TP_SLFLAG_S 0x40000000
84 #define L2TP_SL_SEQ_MASK 0x00ffffff
86 #define L2TP_HDR_SIZE_SEQ 10
87 #define L2TP_HDR_SIZE_NOSEQ 6
89 /* Default trace flags */
90 #define L2TP_DEFAULT_DEBUG_FLAGS 0
92 /* Private data stored for received packets in the skb.
98 unsigned long expires
;
101 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
103 static atomic_t l2tp_tunnel_count
;
104 static atomic_t l2tp_session_count
;
105 static struct workqueue_struct
*l2tp_wq
;
107 /* per-net private data for this module */
108 static unsigned int l2tp_net_id
;
110 struct list_head l2tp_tunnel_list
;
111 spinlock_t l2tp_tunnel_list_lock
;
112 struct hlist_head l2tp_session_hlist
[L2TP_HASH_SIZE_2
];
113 spinlock_t l2tp_session_hlist_lock
;
116 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
);
118 static inline struct l2tp_tunnel
*l2tp_tunnel(struct sock
*sk
)
120 return sk
->sk_user_data
;
123 static inline struct l2tp_net
*l2tp_pernet(struct net
*net
)
127 return net_generic(net
, l2tp_net_id
);
130 /* Tunnel reference counts. Incremented per session that is added to
133 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel
*tunnel
)
135 atomic_inc(&tunnel
->ref_count
);
138 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel
*tunnel
)
140 if (atomic_dec_and_test(&tunnel
->ref_count
))
141 l2tp_tunnel_free(tunnel
);
143 #ifdef L2TP_REFCNT_DEBUG
144 #define l2tp_tunnel_inc_refcount(_t) \
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); \
151 #define l2tp_tunnel_dec_refcount(_t) \
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); \
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)
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.
168 static inline struct hlist_head
*
169 l2tp_session_id_hash_2(struct l2tp_net
*pn
, u32 session_id
)
171 return &pn
->l2tp_session_hlist
[hash_32(session_id
, L2TP_HASH_BITS_2
)];
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.
179 static struct sock
*l2tp_tunnel_sock_lookup(struct l2tp_tunnel
*tunnel
)
182 struct socket
*sock
= NULL
;
183 struct sock
*sk
= NULL
;
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
193 sock
= sockfd_lookup(tunnel
->fd
, &err
);
197 /* Socket is owned by kernelspace */
206 /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
207 static void l2tp_tunnel_sock_put(struct sock
*sk
)
209 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
211 if (tunnel
->fd
>= 0) {
212 /* Socket is owned by userspace */
213 sockfd_put(sk
->sk_socket
);
220 /* Lookup a session by id in the global session list
222 static struct l2tp_session
*l2tp_session_find_2(struct net
*net
, u32 session_id
)
224 struct l2tp_net
*pn
= l2tp_pernet(net
);
225 struct hlist_head
*session_list
=
226 l2tp_session_id_hash_2(pn
, session_id
);
227 struct l2tp_session
*session
;
230 hlist_for_each_entry_rcu(session
, session_list
, global_hlist
) {
231 if (session
->session_id
== session_id
) {
232 rcu_read_unlock_bh();
236 rcu_read_unlock_bh();
241 /* Session hash list.
242 * The session_id SHOULD be random according to RFC2661, but several
243 * L2TP implementations (Cisco and Microsoft) use incrementing
244 * session_ids. So we do a real hash on the session_id, rather than a
247 static inline struct hlist_head
*
248 l2tp_session_id_hash(struct l2tp_tunnel
*tunnel
, u32 session_id
)
250 return &tunnel
->session_hlist
[hash_32(session_id
, L2TP_HASH_BITS
)];
253 /* Lookup a session by id
255 struct l2tp_session
*l2tp_session_find(struct net
*net
, struct l2tp_tunnel
*tunnel
, u32 session_id
)
257 struct hlist_head
*session_list
;
258 struct l2tp_session
*session
;
260 /* In L2TPv3, session_ids are unique over all tunnels and we
261 * sometimes need to look them up before we know the
265 return l2tp_session_find_2(net
, session_id
);
267 session_list
= l2tp_session_id_hash(tunnel
, session_id
);
268 read_lock_bh(&tunnel
->hlist_lock
);
269 hlist_for_each_entry(session
, session_list
, hlist
) {
270 if (session
->session_id
== session_id
) {
271 read_unlock_bh(&tunnel
->hlist_lock
);
275 read_unlock_bh(&tunnel
->hlist_lock
);
279 EXPORT_SYMBOL_GPL(l2tp_session_find
);
281 /* Like l2tp_session_find() but takes a reference on the returned session.
282 * Optionally calls session->ref() too if do_ref is true.
284 struct l2tp_session
*l2tp_session_get(struct net
*net
,
285 struct l2tp_tunnel
*tunnel
,
286 u32 session_id
, bool do_ref
)
288 struct hlist_head
*session_list
;
289 struct l2tp_session
*session
;
292 struct l2tp_net
*pn
= l2tp_pernet(net
);
294 session_list
= l2tp_session_id_hash_2(pn
, session_id
);
297 hlist_for_each_entry_rcu(session
, session_list
, global_hlist
) {
298 if (session
->session_id
== session_id
) {
299 l2tp_session_inc_refcount(session
);
300 if (do_ref
&& session
->ref
)
301 session
->ref(session
);
302 rcu_read_unlock_bh();
307 rcu_read_unlock_bh();
312 session_list
= l2tp_session_id_hash(tunnel
, session_id
);
313 read_lock_bh(&tunnel
->hlist_lock
);
314 hlist_for_each_entry(session
, session_list
, hlist
) {
315 if (session
->session_id
== session_id
) {
316 l2tp_session_inc_refcount(session
);
317 if (do_ref
&& session
->ref
)
318 session
->ref(session
);
319 read_unlock_bh(&tunnel
->hlist_lock
);
324 read_unlock_bh(&tunnel
->hlist_lock
);
328 EXPORT_SYMBOL_GPL(l2tp_session_get
);
330 struct l2tp_session
*l2tp_session_find_nth(struct l2tp_tunnel
*tunnel
, int nth
)
333 struct l2tp_session
*session
;
336 read_lock_bh(&tunnel
->hlist_lock
);
337 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
338 hlist_for_each_entry(session
, &tunnel
->session_hlist
[hash
], hlist
) {
340 read_unlock_bh(&tunnel
->hlist_lock
);
346 read_unlock_bh(&tunnel
->hlist_lock
);
350 EXPORT_SYMBOL_GPL(l2tp_session_find_nth
);
352 /* Lookup a session by interface name.
353 * This is very inefficient but is only used by management interfaces.
355 struct l2tp_session
*l2tp_session_get_by_ifname(struct net
*net
, char *ifname
,
358 struct l2tp_net
*pn
= l2tp_pernet(net
);
360 struct l2tp_session
*session
;
363 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++) {
364 hlist_for_each_entry_rcu(session
, &pn
->l2tp_session_hlist
[hash
], global_hlist
) {
365 if (!strcmp(session
->ifname
, ifname
)) {
366 l2tp_session_inc_refcount(session
);
367 if (do_ref
&& session
->ref
)
368 session
->ref(session
);
369 rcu_read_unlock_bh();
376 rcu_read_unlock_bh();
380 EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname
);
382 static int l2tp_session_add_to_tunnel(struct l2tp_tunnel
*tunnel
,
383 struct l2tp_session
*session
)
385 struct l2tp_session
*session_walk
;
386 struct hlist_head
*g_head
;
387 struct hlist_head
*head
;
390 head
= l2tp_session_id_hash(tunnel
, session
->session_id
);
392 write_lock_bh(&tunnel
->hlist_lock
);
393 hlist_for_each_entry(session_walk
, head
, hlist
)
394 if (session_walk
->session_id
== session
->session_id
)
397 if (tunnel
->version
== L2TP_HDR_VER_3
) {
398 pn
= l2tp_pernet(tunnel
->l2tp_net
);
399 g_head
= l2tp_session_id_hash_2(l2tp_pernet(tunnel
->l2tp_net
),
400 session
->session_id
);
402 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
403 hlist_for_each_entry(session_walk
, g_head
, global_hlist
)
404 if (session_walk
->session_id
== session
->session_id
)
407 hlist_add_head_rcu(&session
->global_hlist
, g_head
);
408 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
411 hlist_add_head(&session
->hlist
, head
);
412 write_unlock_bh(&tunnel
->hlist_lock
);
417 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
419 write_unlock_bh(&tunnel
->hlist_lock
);
424 /* Lookup a tunnel by id
426 struct l2tp_tunnel
*l2tp_tunnel_find(struct net
*net
, u32 tunnel_id
)
428 struct l2tp_tunnel
*tunnel
;
429 struct l2tp_net
*pn
= l2tp_pernet(net
);
432 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
433 if (tunnel
->tunnel_id
== tunnel_id
) {
434 rcu_read_unlock_bh();
438 rcu_read_unlock_bh();
442 EXPORT_SYMBOL_GPL(l2tp_tunnel_find
);
444 struct l2tp_tunnel
*l2tp_tunnel_find_nth(struct net
*net
, int nth
)
446 struct l2tp_net
*pn
= l2tp_pernet(net
);
447 struct l2tp_tunnel
*tunnel
;
451 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
453 rcu_read_unlock_bh();
458 rcu_read_unlock_bh();
462 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth
);
464 /*****************************************************************************
465 * Receive data handling
466 *****************************************************************************/
468 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
471 static void l2tp_recv_queue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
473 struct sk_buff
*skbp
;
475 u32 ns
= L2TP_SKB_CB(skb
)->ns
;
477 spin_lock_bh(&session
->reorder_q
.lock
);
478 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
479 if (L2TP_SKB_CB(skbp
)->ns
> ns
) {
480 __skb_queue_before(&session
->reorder_q
, skbp
, skb
);
481 l2tp_dbg(session
, L2TP_MSG_SEQ
,
482 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
483 session
->name
, ns
, L2TP_SKB_CB(skbp
)->ns
,
484 skb_queue_len(&session
->reorder_q
));
485 atomic_long_inc(&session
->stats
.rx_oos_packets
);
490 __skb_queue_tail(&session
->reorder_q
, skb
);
493 spin_unlock_bh(&session
->reorder_q
.lock
);
496 /* Dequeue a single skb.
498 static void l2tp_recv_dequeue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
500 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
501 int length
= L2TP_SKB_CB(skb
)->length
;
503 /* We're about to requeue the skb, so return resources
504 * to its current owner (a socket receive buffer).
508 atomic_long_inc(&tunnel
->stats
.rx_packets
);
509 atomic_long_add(length
, &tunnel
->stats
.rx_bytes
);
510 atomic_long_inc(&session
->stats
.rx_packets
);
511 atomic_long_add(length
, &session
->stats
.rx_bytes
);
513 if (L2TP_SKB_CB(skb
)->has_seq
) {
516 session
->nr
&= session
->nr_max
;
518 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated nr to %hu\n",
519 session
->name
, session
->nr
);
522 /* call private receive handler */
523 if (session
->recv_skb
!= NULL
)
524 (*session
->recv_skb
)(session
, skb
, L2TP_SKB_CB(skb
)->length
);
529 (*session
->deref
)(session
);
532 /* Dequeue skbs from the session's reorder_q, subject to packet order.
533 * Skbs that have been in the queue for too long are simply discarded.
535 static void l2tp_recv_dequeue(struct l2tp_session
*session
)
540 /* If the pkt at the head of the queue has the nr that we
541 * expect to send up next, dequeue it and any other
542 * in-sequence packets behind it.
545 spin_lock_bh(&session
->reorder_q
.lock
);
546 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
547 if (time_after(jiffies
, L2TP_SKB_CB(skb
)->expires
)) {
548 atomic_long_inc(&session
->stats
.rx_seq_discards
);
549 atomic_long_inc(&session
->stats
.rx_errors
);
550 l2tp_dbg(session
, L2TP_MSG_SEQ
,
551 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
552 session
->name
, L2TP_SKB_CB(skb
)->ns
,
553 L2TP_SKB_CB(skb
)->length
, session
->nr
,
554 skb_queue_len(&session
->reorder_q
));
555 session
->reorder_skip
= 1;
556 __skb_unlink(skb
, &session
->reorder_q
);
559 (*session
->deref
)(session
);
563 if (L2TP_SKB_CB(skb
)->has_seq
) {
564 if (session
->reorder_skip
) {
565 l2tp_dbg(session
, L2TP_MSG_SEQ
,
566 "%s: advancing nr to next pkt: %u -> %u",
567 session
->name
, session
->nr
,
568 L2TP_SKB_CB(skb
)->ns
);
569 session
->reorder_skip
= 0;
570 session
->nr
= L2TP_SKB_CB(skb
)->ns
;
572 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
573 l2tp_dbg(session
, L2TP_MSG_SEQ
,
574 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
575 session
->name
, L2TP_SKB_CB(skb
)->ns
,
576 L2TP_SKB_CB(skb
)->length
, session
->nr
,
577 skb_queue_len(&session
->reorder_q
));
581 __skb_unlink(skb
, &session
->reorder_q
);
583 /* Process the skb. We release the queue lock while we
584 * do so to let other contexts process the queue.
586 spin_unlock_bh(&session
->reorder_q
.lock
);
587 l2tp_recv_dequeue_skb(session
, skb
);
592 spin_unlock_bh(&session
->reorder_q
.lock
);
595 static int l2tp_seq_check_rx_window(struct l2tp_session
*session
, u32 nr
)
599 if (nr
>= session
->nr
)
600 nws
= nr
- session
->nr
;
602 nws
= (session
->nr_max
+ 1) - (session
->nr
- nr
);
604 return nws
< session
->nr_window_size
;
607 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
608 * acceptable, else non-zero.
610 static int l2tp_recv_data_seq(struct l2tp_session
*session
, struct sk_buff
*skb
)
612 if (!l2tp_seq_check_rx_window(session
, L2TP_SKB_CB(skb
)->ns
)) {
613 /* Packet sequence number is outside allowed window.
616 l2tp_dbg(session
, L2TP_MSG_SEQ
,
617 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
618 session
->name
, L2TP_SKB_CB(skb
)->ns
,
619 L2TP_SKB_CB(skb
)->length
, session
->nr
);
623 if (session
->reorder_timeout
!= 0) {
624 /* Packet reordering enabled. Add skb to session's
625 * reorder queue, in order of ns.
627 l2tp_recv_queue_skb(session
, skb
);
631 /* Packet reordering disabled. Discard out-of-sequence packets, while
632 * tracking the number if in-sequence packets after the first OOS packet
633 * is seen. After nr_oos_count_max in-sequence packets, reset the
634 * sequence number to re-enable packet reception.
636 if (L2TP_SKB_CB(skb
)->ns
== session
->nr
) {
637 skb_queue_tail(&session
->reorder_q
, skb
);
639 u32 nr_oos
= L2TP_SKB_CB(skb
)->ns
;
640 u32 nr_next
= (session
->nr_oos
+ 1) & session
->nr_max
;
642 if (nr_oos
== nr_next
)
643 session
->nr_oos_count
++;
645 session
->nr_oos_count
= 0;
647 session
->nr_oos
= nr_oos
;
648 if (session
->nr_oos_count
> session
->nr_oos_count_max
) {
649 session
->reorder_skip
= 1;
650 l2tp_dbg(session
, L2TP_MSG_SEQ
,
651 "%s: %d oos packets received. Resetting sequence numbers\n",
652 session
->name
, session
->nr_oos_count
);
654 if (!session
->reorder_skip
) {
655 atomic_long_inc(&session
->stats
.rx_seq_discards
);
656 l2tp_dbg(session
, L2TP_MSG_SEQ
,
657 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
658 session
->name
, L2TP_SKB_CB(skb
)->ns
,
659 L2TP_SKB_CB(skb
)->length
, session
->nr
,
660 skb_queue_len(&session
->reorder_q
));
663 skb_queue_tail(&session
->reorder_q
, skb
);
673 /* Do receive processing of L2TP data frames. We handle both L2TPv2
674 * and L2TPv3 data frames here.
676 * L2TPv2 Data Message Header
679 * 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
680 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
681 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
682 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
683 * | Tunnel ID | Session ID |
684 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
685 * | Ns (opt) | Nr (opt) |
686 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
687 * | Offset Size (opt) | Offset pad... (opt)
688 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
690 * Data frames are marked by T=0. All other fields are the same as
691 * those in L2TP control frames.
693 * L2TPv3 Data Message Header
695 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
696 * | L2TP Session Header |
697 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
698 * | L2-Specific Sublayer |
699 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
700 * | Tunnel Payload ...
701 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
703 * L2TPv3 Session Header Over IP
706 * 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
707 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
709 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
710 * | Cookie (optional, maximum 64 bits)...
711 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
713 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
715 * L2TPv3 L2-Specific Sublayer Format
718 * 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
719 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
720 * |x|S|x|x|x|x|x|x| Sequence Number |
721 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
723 * Cookie value, sublayer format and offset (pad) are negotiated with
724 * the peer when the session is set up. Unlike L2TPv2, we do not need
725 * to parse the packet header to determine if optional fields are
728 * Caller must already have parsed the frame and determined that it is
729 * a data (not control) frame before coming here. Fields up to the
730 * session-id have already been parsed and ptr points to the data
731 * after the session-id.
733 * session->ref() must have been called prior to l2tp_recv_common().
734 * session->deref() will be called automatically after skb is processed.
736 void l2tp_recv_common(struct l2tp_session
*session
, struct sk_buff
*skb
,
737 unsigned char *ptr
, unsigned char *optr
, u16 hdrflags
,
738 int length
, int (*payload_hook
)(struct sk_buff
*skb
))
740 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
744 /* Parse and check optional cookie */
745 if (session
->peer_cookie_len
> 0) {
746 if (memcmp(ptr
, &session
->peer_cookie
[0], session
->peer_cookie_len
)) {
747 l2tp_info(tunnel
, L2TP_MSG_DATA
,
748 "%s: cookie mismatch (%u/%u). Discarding.\n",
749 tunnel
->name
, tunnel
->tunnel_id
,
750 session
->session_id
);
751 atomic_long_inc(&session
->stats
.rx_cookie_discards
);
754 ptr
+= session
->peer_cookie_len
;
757 /* Handle the optional sequence numbers. Sequence numbers are
758 * in different places for L2TPv2 and L2TPv3.
760 * If we are the LAC, enable/disable sequence numbers under
761 * the control of the LNS. If no sequence numbers present but
762 * we were expecting them, discard frame.
765 L2TP_SKB_CB(skb
)->has_seq
= 0;
766 if (tunnel
->version
== L2TP_HDR_VER_2
) {
767 if (hdrflags
& L2TP_HDRFLAG_S
) {
768 ns
= ntohs(*(__be16
*) ptr
);
770 nr
= ntohs(*(__be16
*) ptr
);
773 /* Store L2TP info in the skb */
774 L2TP_SKB_CB(skb
)->ns
= ns
;
775 L2TP_SKB_CB(skb
)->has_seq
= 1;
777 l2tp_dbg(session
, L2TP_MSG_SEQ
,
778 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
779 session
->name
, ns
, nr
, session
->nr
);
781 } else if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
782 u32 l2h
= ntohl(*(__be32
*) ptr
);
784 if (l2h
& 0x40000000) {
785 ns
= l2h
& 0x00ffffff;
787 /* Store L2TP info in the skb */
788 L2TP_SKB_CB(skb
)->ns
= ns
;
789 L2TP_SKB_CB(skb
)->has_seq
= 1;
791 l2tp_dbg(session
, L2TP_MSG_SEQ
,
792 "%s: recv data ns=%u, session nr=%u\n",
793 session
->name
, ns
, session
->nr
);
797 /* Advance past L2-specific header, if present */
798 ptr
+= session
->l2specific_len
;
800 if (L2TP_SKB_CB(skb
)->has_seq
) {
801 /* Received a packet with sequence numbers. If we're the LNS,
802 * check if we sre sending sequence numbers and if not,
805 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
806 l2tp_info(session
, L2TP_MSG_SEQ
,
807 "%s: requested to enable seq numbers by LNS\n",
809 session
->send_seq
= 1;
810 l2tp_session_set_header_len(session
, tunnel
->version
);
813 /* No sequence numbers.
814 * If user has configured mandatory sequence numbers, discard.
816 if (session
->recv_seq
) {
817 l2tp_warn(session
, L2TP_MSG_SEQ
,
818 "%s: recv data has no seq numbers when required. Discarding.\n",
820 atomic_long_inc(&session
->stats
.rx_seq_discards
);
824 /* If we're the LAC and we're sending sequence numbers, the
825 * LNS has requested that we no longer send sequence numbers.
826 * If we're the LNS and we're sending sequence numbers, the
827 * LAC is broken. Discard the frame.
829 if ((!session
->lns_mode
) && (session
->send_seq
)) {
830 l2tp_info(session
, L2TP_MSG_SEQ
,
831 "%s: requested to disable seq numbers by LNS\n",
833 session
->send_seq
= 0;
834 l2tp_session_set_header_len(session
, tunnel
->version
);
835 } else if (session
->send_seq
) {
836 l2tp_warn(session
, L2TP_MSG_SEQ
,
837 "%s: recv data has no seq numbers when required. Discarding.\n",
839 atomic_long_inc(&session
->stats
.rx_seq_discards
);
844 /* Session data offset is handled differently for L2TPv2 and
845 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
846 * the header. For L2TPv3, the offset is negotiated using AVPs
847 * in the session setup control protocol.
849 if (tunnel
->version
== L2TP_HDR_VER_2
) {
850 /* If offset bit set, skip it. */
851 if (hdrflags
& L2TP_HDRFLAG_O
) {
852 offset
= ntohs(*(__be16
*)ptr
);
856 ptr
+= session
->offset
;
859 if (!pskb_may_pull(skb
, offset
))
862 __skb_pull(skb
, offset
);
864 /* If caller wants to process the payload before we queue the
868 if ((*payload_hook
)(skb
))
871 /* Prepare skb for adding to the session's reorder_q. Hold
872 * packets for max reorder_timeout or 1 second if not
875 L2TP_SKB_CB(skb
)->length
= length
;
876 L2TP_SKB_CB(skb
)->expires
= jiffies
+
877 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
879 /* Add packet to the session's receive queue. Reordering is done here, if
880 * enabled. Saved L2TP protocol info is stored in skb->sb[].
882 if (L2TP_SKB_CB(skb
)->has_seq
) {
883 if (l2tp_recv_data_seq(session
, skb
))
886 /* No sequence numbers. Add the skb to the tail of the
887 * reorder queue. This ensures that it will be
888 * delivered after all previous sequenced skbs.
890 skb_queue_tail(&session
->reorder_q
, skb
);
893 /* Try to dequeue as many skbs from reorder_q as we can. */
894 l2tp_recv_dequeue(session
);
899 atomic_long_inc(&session
->stats
.rx_errors
);
903 (*session
->deref
)(session
);
905 EXPORT_SYMBOL(l2tp_recv_common
);
907 /* Drop skbs from the session's reorder_q
909 int l2tp_session_queue_purge(struct l2tp_session
*session
)
911 struct sk_buff
*skb
= NULL
;
913 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
914 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
915 atomic_long_inc(&session
->stats
.rx_errors
);
918 (*session
->deref
)(session
);
922 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge
);
924 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
925 * here. The skb is not on a list when we get here.
926 * Returns 0 if the packet was a data packet and was successfully passed on.
927 * Returns 1 if the packet was not a good data packet and could not be
928 * forwarded. All such packets are passed up to userspace to deal with.
930 static int l2tp_udp_recv_core(struct l2tp_tunnel
*tunnel
, struct sk_buff
*skb
,
931 int (*payload_hook
)(struct sk_buff
*skb
))
933 struct l2tp_session
*session
= NULL
;
934 unsigned char *ptr
, *optr
;
936 u32 tunnel_id
, session_id
;
940 /* UDP has verifed checksum */
942 /* UDP always verifies the packet length. */
943 __skb_pull(skb
, sizeof(struct udphdr
));
946 if (!pskb_may_pull(skb
, L2TP_HDR_SIZE_SEQ
)) {
947 l2tp_info(tunnel
, L2TP_MSG_DATA
,
948 "%s: recv short packet (len=%d)\n",
949 tunnel
->name
, skb
->len
);
953 /* Trace packet contents, if enabled */
954 if (tunnel
->debug
& L2TP_MSG_DATA
) {
955 length
= min(32u, skb
->len
);
956 if (!pskb_may_pull(skb
, length
))
959 pr_debug("%s: recv\n", tunnel
->name
);
960 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
963 /* Point to L2TP header */
964 optr
= ptr
= skb
->data
;
966 /* Get L2TP header flags */
967 hdrflags
= ntohs(*(__be16
*) ptr
);
969 /* Check protocol version */
970 version
= hdrflags
& L2TP_HDR_VER_MASK
;
971 if (version
!= tunnel
->version
) {
972 l2tp_info(tunnel
, L2TP_MSG_DATA
,
973 "%s: recv protocol version mismatch: got %d expected %d\n",
974 tunnel
->name
, version
, tunnel
->version
);
978 /* Get length of L2TP packet */
981 /* If type is control packet, it is handled by userspace. */
982 if (hdrflags
& L2TP_HDRFLAG_T
) {
983 l2tp_dbg(tunnel
, L2TP_MSG_DATA
,
984 "%s: recv control packet, len=%d\n",
985 tunnel
->name
, length
);
992 if (tunnel
->version
== L2TP_HDR_VER_2
) {
993 /* If length is present, skip it */
994 if (hdrflags
& L2TP_HDRFLAG_L
)
997 /* Extract tunnel and session ID */
998 tunnel_id
= ntohs(*(__be16
*) ptr
);
1000 session_id
= ntohs(*(__be16
*) ptr
);
1003 ptr
+= 2; /* skip reserved bits */
1004 tunnel_id
= tunnel
->tunnel_id
;
1005 session_id
= ntohl(*(__be32
*) ptr
);
1009 /* Find the session context */
1010 session
= l2tp_session_get(tunnel
->l2tp_net
, tunnel
, session_id
, true);
1011 if (!session
|| !session
->recv_skb
) {
1014 session
->deref(session
);
1015 l2tp_session_dec_refcount(session
);
1018 /* Not found? Pass to userspace to deal with */
1019 l2tp_info(tunnel
, L2TP_MSG_DATA
,
1020 "%s: no session found (%u/%u). Passing up.\n",
1021 tunnel
->name
, tunnel_id
, session_id
);
1025 l2tp_recv_common(session
, skb
, ptr
, optr
, hdrflags
, length
, payload_hook
);
1026 l2tp_session_dec_refcount(session
);
1031 /* Put UDP header back */
1032 __skb_push(skb
, sizeof(struct udphdr
));
1037 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
1041 * >0: skb should be passed up to userspace as UDP.
1043 int l2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
1045 struct l2tp_tunnel
*tunnel
;
1047 tunnel
= l2tp_sock_to_tunnel(sk
);
1051 l2tp_dbg(tunnel
, L2TP_MSG_DATA
, "%s: received %d bytes\n",
1052 tunnel
->name
, skb
->len
);
1054 if (l2tp_udp_recv_core(tunnel
, skb
, tunnel
->recv_payload_hook
))
1065 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv
);
1067 /************************************************************************
1069 ***********************************************************************/
1071 /* Build an L2TP header for the session into the buffer provided.
1073 static int l2tp_build_l2tpv2_header(struct l2tp_session
*session
, void *buf
)
1075 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1078 u16 flags
= L2TP_HDR_VER_2
;
1079 u32 tunnel_id
= tunnel
->peer_tunnel_id
;
1080 u32 session_id
= session
->peer_session_id
;
1082 if (session
->send_seq
)
1083 flags
|= L2TP_HDRFLAG_S
;
1085 /* Setup L2TP header. */
1086 *bufp
++ = htons(flags
);
1087 *bufp
++ = htons(tunnel_id
);
1088 *bufp
++ = htons(session_id
);
1089 if (session
->send_seq
) {
1090 *bufp
++ = htons(session
->ns
);
1093 session
->ns
&= 0xffff;
1094 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated ns to %u\n",
1095 session
->name
, session
->ns
);
1101 static int l2tp_build_l2tpv3_header(struct l2tp_session
*session
, void *buf
)
1103 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1107 /* Setup L2TP header. The header differs slightly for UDP and
1108 * IP encapsulations. For UDP, there is 4 bytes of flags.
1110 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
1111 u16 flags
= L2TP_HDR_VER_3
;
1112 *((__be16
*) bufp
) = htons(flags
);
1114 *((__be16
*) bufp
) = 0;
1118 *((__be32
*) bufp
) = htonl(session
->peer_session_id
);
1120 if (session
->cookie_len
) {
1121 memcpy(bufp
, &session
->cookie
[0], session
->cookie_len
);
1122 bufp
+= session
->cookie_len
;
1124 if (session
->l2specific_len
) {
1125 if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
1127 if (session
->send_seq
) {
1128 l2h
= 0x40000000 | session
->ns
;
1130 session
->ns
&= 0xffffff;
1131 l2tp_dbg(session
, L2TP_MSG_SEQ
,
1132 "%s: updated ns to %u\n",
1133 session
->name
, session
->ns
);
1136 *((__be32
*) bufp
) = htonl(l2h
);
1138 bufp
+= session
->l2specific_len
;
1140 if (session
->offset
)
1141 bufp
+= session
->offset
;
1146 static int l2tp_xmit_core(struct l2tp_session
*session
, struct sk_buff
*skb
,
1147 struct flowi
*fl
, size_t data_len
)
1149 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1150 unsigned int len
= skb
->len
;
1154 if (session
->send_seq
)
1155 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %zd bytes, ns=%u\n",
1156 session
->name
, data_len
, session
->ns
- 1);
1158 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %zd bytes\n",
1159 session
->name
, data_len
);
1161 if (session
->debug
& L2TP_MSG_DATA
) {
1162 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1163 unsigned char *datap
= skb
->data
+ uhlen
;
1165 pr_debug("%s: xmit\n", session
->name
);
1166 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1167 datap
, min_t(size_t, 32, len
- uhlen
));
1170 /* Queue the packet to IP for output */
1172 #if IS_ENABLED(CONFIG_IPV6)
1173 if (tunnel
->sock
->sk_family
== PF_INET6
&& !tunnel
->v4mapped
)
1174 error
= inet6_csk_xmit(tunnel
->sock
, skb
, NULL
);
1177 error
= ip_queue_xmit(tunnel
->sock
, skb
, fl
);
1181 atomic_long_inc(&tunnel
->stats
.tx_packets
);
1182 atomic_long_add(len
, &tunnel
->stats
.tx_bytes
);
1183 atomic_long_inc(&session
->stats
.tx_packets
);
1184 atomic_long_add(len
, &session
->stats
.tx_bytes
);
1186 atomic_long_inc(&tunnel
->stats
.tx_errors
);
1187 atomic_long_inc(&session
->stats
.tx_errors
);
1193 /* If caller requires the skb to have a ppp header, the header must be
1194 * inserted in the skb data before calling this function.
1196 int l2tp_xmit_skb(struct l2tp_session
*session
, struct sk_buff
*skb
, int hdr_len
)
1198 int data_len
= skb
->len
;
1199 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1200 struct sock
*sk
= tunnel
->sock
;
1203 struct inet_sock
*inet
;
1205 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1207 int ret
= NET_XMIT_SUCCESS
;
1209 /* Check that there's enough headroom in the skb to insert IP,
1210 * UDP and L2TP headers. If not enough, expand it to
1211 * make room. Adjust truesize.
1213 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1215 if (skb_cow_head(skb
, headroom
)) {
1217 return NET_XMIT_DROP
;
1220 /* Setup L2TP header */
1221 session
->build_header(session
, __skb_push(skb
, hdr_len
));
1223 /* Reset skb netfilter state */
1224 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1225 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1230 if (sock_owned_by_user(sk
)) {
1232 ret
= NET_XMIT_DROP
;
1236 /* Get routing info from the tunnel socket */
1238 skb_dst_set(skb
, dst_clone(__sk_dst_check(sk
, 0)));
1241 fl
= &inet
->cork
.fl
;
1242 switch (tunnel
->encap
) {
1243 case L2TP_ENCAPTYPE_UDP
:
1244 /* Setup UDP header */
1245 __skb_push(skb
, sizeof(*uh
));
1246 skb_reset_transport_header(skb
);
1248 uh
->source
= inet
->inet_sport
;
1249 uh
->dest
= inet
->inet_dport
;
1250 udp_len
= uhlen
+ hdr_len
+ data_len
;
1251 uh
->len
= htons(udp_len
);
1253 /* Calculate UDP checksum if configured to do so */
1254 #if IS_ENABLED(CONFIG_IPV6)
1255 if (sk
->sk_family
== PF_INET6
&& !tunnel
->v4mapped
)
1256 udp6_set_csum(udp_get_no_check6_tx(sk
),
1257 skb
, &inet6_sk(sk
)->saddr
,
1258 &sk
->sk_v6_daddr
, udp_len
);
1261 udp_set_csum(sk
->sk_no_check_tx
, skb
, inet
->inet_saddr
,
1262 inet
->inet_daddr
, udp_len
);
1265 case L2TP_ENCAPTYPE_IP
:
1269 l2tp_xmit_core(session
, skb
, fl
, data_len
);
1275 EXPORT_SYMBOL_GPL(l2tp_xmit_skb
);
1277 /*****************************************************************************
1278 * Tinnel and session create/destroy.
1279 *****************************************************************************/
1281 /* Tunnel socket destruct hook.
1282 * The tunnel context is deleted only when all session sockets have been
1285 static void l2tp_tunnel_destruct(struct sock
*sk
)
1287 struct l2tp_tunnel
*tunnel
= l2tp_tunnel(sk
);
1288 struct l2tp_net
*pn
;
1293 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing...\n", tunnel
->name
);
1296 /* Disable udp encapsulation */
1297 switch (tunnel
->encap
) {
1298 case L2TP_ENCAPTYPE_UDP
:
1299 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1300 (udp_sk(sk
))->encap_type
= 0;
1301 (udp_sk(sk
))->encap_rcv
= NULL
;
1302 (udp_sk(sk
))->encap_destroy
= NULL
;
1304 case L2TP_ENCAPTYPE_IP
:
1308 /* Remove hooks into tunnel socket */
1309 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1310 sk
->sk_user_data
= NULL
;
1311 tunnel
->sock
= NULL
;
1313 /* Remove the tunnel struct from the tunnel list */
1314 pn
= l2tp_pernet(tunnel
->l2tp_net
);
1315 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1316 list_del_rcu(&tunnel
->list
);
1317 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1318 atomic_dec(&l2tp_tunnel_count
);
1320 l2tp_tunnel_closeall(tunnel
);
1321 l2tp_tunnel_dec_refcount(tunnel
);
1323 /* Call the original destructor */
1324 if (sk
->sk_destruct
)
1325 (*sk
->sk_destruct
)(sk
);
1330 /* When the tunnel is closed, all the attached sessions need to go too.
1332 void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
)
1335 struct hlist_node
*walk
;
1336 struct hlist_node
*tmp
;
1337 struct l2tp_session
*session
;
1339 BUG_ON(tunnel
== NULL
);
1341 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing all sessions...\n",
1344 write_lock_bh(&tunnel
->hlist_lock
);
1345 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
1347 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1348 session
= hlist_entry(walk
, struct l2tp_session
, hlist
);
1350 l2tp_info(session
, L2TP_MSG_CONTROL
,
1351 "%s: closing session\n", session
->name
);
1353 hlist_del_init(&session
->hlist
);
1355 if (session
->ref
!= NULL
)
1356 (*session
->ref
)(session
);
1358 write_unlock_bh(&tunnel
->hlist_lock
);
1360 __l2tp_session_unhash(session
);
1361 l2tp_session_queue_purge(session
);
1363 if (session
->session_close
!= NULL
)
1364 (*session
->session_close
)(session
);
1366 if (session
->deref
!= NULL
)
1367 (*session
->deref
)(session
);
1369 l2tp_session_dec_refcount(session
);
1371 write_lock_bh(&tunnel
->hlist_lock
);
1373 /* Now restart from the beginning of this hash
1374 * chain. We always remove a session from the
1375 * list so we are guaranteed to make forward
1381 write_unlock_bh(&tunnel
->hlist_lock
);
1383 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall
);
1385 /* Tunnel socket destroy hook for UDP encapsulation */
1386 static void l2tp_udp_encap_destroy(struct sock
*sk
)
1388 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
1390 l2tp_tunnel_closeall(tunnel
);
1395 /* Really kill the tunnel.
1396 * Come here only when all sessions have been cleared from the tunnel.
1398 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
)
1400 BUG_ON(atomic_read(&tunnel
->ref_count
) != 0);
1401 BUG_ON(tunnel
->sock
!= NULL
);
1402 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: free...\n", tunnel
->name
);
1403 kfree_rcu(tunnel
, rcu
);
1406 /* Workqueue tunnel deletion function */
1407 static void l2tp_tunnel_del_work(struct work_struct
*work
)
1409 struct l2tp_tunnel
*tunnel
= NULL
;
1410 struct socket
*sock
= NULL
;
1411 struct sock
*sk
= NULL
;
1413 tunnel
= container_of(work
, struct l2tp_tunnel
, del_work
);
1415 l2tp_tunnel_closeall(tunnel
);
1417 sk
= l2tp_tunnel_sock_lookup(tunnel
);
1421 sock
= sk
->sk_socket
;
1423 /* If the tunnel socket was created by userspace, then go through the
1424 * inet layer to shut the socket down, and let userspace close it.
1425 * Otherwise, if we created the socket directly within the kernel, use
1426 * the sk API to release it here.
1427 * In either case the tunnel resources are freed in the socket
1428 * destructor when the tunnel socket goes away.
1430 if (tunnel
->fd
>= 0) {
1432 inet_shutdown(sock
, 2);
1435 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1440 l2tp_tunnel_sock_put(sk
);
1442 l2tp_tunnel_dec_refcount(tunnel
);
1445 /* Create a socket for the tunnel, if one isn't set up by
1446 * userspace. This is used for static tunnels where there is no
1447 * managing L2TP daemon.
1449 * Since we don't want these sockets to keep a namespace alive by
1450 * themselves, we drop the socket's namespace refcount after creation.
1451 * These sockets are freed when the namespace exits using the pernet
1454 static int l2tp_tunnel_sock_create(struct net
*net
,
1457 struct l2tp_tunnel_cfg
*cfg
,
1458 struct socket
**sockp
)
1461 struct socket
*sock
= NULL
;
1462 struct udp_port_cfg udp_conf
;
1464 switch (cfg
->encap
) {
1465 case L2TP_ENCAPTYPE_UDP
:
1466 memset(&udp_conf
, 0, sizeof(udp_conf
));
1468 #if IS_ENABLED(CONFIG_IPV6)
1469 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1470 udp_conf
.family
= AF_INET6
;
1471 memcpy(&udp_conf
.local_ip6
, cfg
->local_ip6
,
1472 sizeof(udp_conf
.local_ip6
));
1473 memcpy(&udp_conf
.peer_ip6
, cfg
->peer_ip6
,
1474 sizeof(udp_conf
.peer_ip6
));
1475 udp_conf
.use_udp6_tx_checksums
=
1476 ! cfg
->udp6_zero_tx_checksums
;
1477 udp_conf
.use_udp6_rx_checksums
=
1478 ! cfg
->udp6_zero_rx_checksums
;
1482 udp_conf
.family
= AF_INET
;
1483 udp_conf
.local_ip
= cfg
->local_ip
;
1484 udp_conf
.peer_ip
= cfg
->peer_ip
;
1485 udp_conf
.use_udp_checksums
= cfg
->use_udp_checksums
;
1488 udp_conf
.local_udp_port
= htons(cfg
->local_udp_port
);
1489 udp_conf
.peer_udp_port
= htons(cfg
->peer_udp_port
);
1491 err
= udp_sock_create(net
, &udp_conf
, &sock
);
1497 case L2TP_ENCAPTYPE_IP
:
1498 #if IS_ENABLED(CONFIG_IPV6)
1499 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1500 struct sockaddr_l2tpip6 ip6_addr
= {0};
1502 err
= sock_create_kern(net
, AF_INET6
, SOCK_DGRAM
,
1503 IPPROTO_L2TP
, &sock
);
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
,
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);
1528 struct sockaddr_l2tpip ip_addr
= {0};
1530 err
= sock_create_kern(net
, AF_INET
, SOCK_DGRAM
,
1531 IPPROTO_L2TP
, &sock
);
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
,
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);
1559 if ((err
< 0) && sock
) {
1560 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1568 static struct lock_class_key l2tp_socket_class
;
1570 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
)
1572 struct l2tp_tunnel
*tunnel
= NULL
;
1574 struct socket
*sock
= NULL
;
1575 struct sock
*sk
= NULL
;
1576 struct l2tp_net
*pn
;
1577 enum l2tp_encap_type encap
= L2TP_ENCAPTYPE_UDP
;
1579 /* Get the tunnel socket from the fd, which was opened by
1580 * the userspace L2TP daemon. If not specified, create a
1584 err
= l2tp_tunnel_sock_create(net
, tunnel_id
, peer_tunnel_id
,
1589 sock
= sockfd_lookup(fd
, &err
);
1591 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1592 tunnel_id
, fd
, err
);
1597 /* Reject namespace mismatches */
1598 if (!net_eq(sock_net(sock
->sk
), net
)) {
1599 pr_err("tunl %u: netns mismatch\n", tunnel_id
);
1610 /* Quick sanity checks */
1612 case L2TP_ENCAPTYPE_UDP
:
1613 err
= -EPROTONOSUPPORT
;
1614 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1615 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1616 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1620 case L2TP_ENCAPTYPE_IP
:
1621 err
= -EPROTONOSUPPORT
;
1622 if (sk
->sk_protocol
!= IPPROTO_L2TP
) {
1623 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1624 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_L2TP
);
1630 /* Check if this socket has already been prepped */
1631 tunnel
= l2tp_tunnel(sk
);
1632 if (tunnel
!= NULL
) {
1633 /* This socket has already been prepped */
1638 tunnel
= kzalloc(sizeof(struct l2tp_tunnel
), GFP_KERNEL
);
1639 if (tunnel
== NULL
) {
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
;
1649 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1650 sprintf(&tunnel
->name
[0], "tunl %u", tunnel_id
);
1651 rwlock_init(&tunnel
->hlist_lock
);
1653 /* The net we belong to */
1654 tunnel
->l2tp_net
= net
;
1655 pn
= l2tp_pernet(net
);
1658 tunnel
->debug
= cfg
->debug
;
1660 #if IS_ENABLED(CONFIG_IPV6)
1661 if (sk
->sk_family
== PF_INET6
) {
1662 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1664 if (ipv6_addr_v4mapped(&np
->saddr
) &&
1665 ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
1666 struct inet_sock
*inet
= inet_sk(sk
);
1668 tunnel
->v4mapped
= true;
1669 inet
->inet_saddr
= np
->saddr
.s6_addr32
[3];
1670 inet
->inet_rcv_saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
[3];
1671 inet
->inet_daddr
= sk
->sk_v6_daddr
.s6_addr32
[3];
1673 tunnel
->v4mapped
= false;
1678 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1679 tunnel
->encap
= encap
;
1680 if (encap
== L2TP_ENCAPTYPE_UDP
) {
1681 struct udp_tunnel_sock_cfg udp_cfg
= { };
1683 udp_cfg
.sk_user_data
= tunnel
;
1684 udp_cfg
.encap_type
= UDP_ENCAP_L2TPINUDP
;
1685 udp_cfg
.encap_rcv
= l2tp_udp_encap_recv
;
1686 udp_cfg
.encap_destroy
= l2tp_udp_encap_destroy
;
1688 setup_udp_tunnel_sock(net
, sock
, &udp_cfg
);
1690 sk
->sk_user_data
= tunnel
;
1693 /* Hook on the tunnel socket destructor so that we can cleanup
1694 * if the tunnel socket goes away.
1696 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1697 sk
->sk_destruct
= &l2tp_tunnel_destruct
;
1700 lockdep_set_class_and_name(&sk
->sk_lock
.slock
, &l2tp_socket_class
, "l2tp_sock");
1702 sk
->sk_allocation
= GFP_ATOMIC
;
1704 /* Init delete workqueue struct */
1705 INIT_WORK(&tunnel
->del_work
, l2tp_tunnel_del_work
);
1707 /* Add tunnel to our list */
1708 INIT_LIST_HEAD(&tunnel
->list
);
1709 atomic_inc(&l2tp_tunnel_count
);
1711 /* Bump the reference count. The tunnel context is deleted
1712 * only when this drops to zero. Must be done before list insertion
1714 l2tp_tunnel_inc_refcount(tunnel
);
1715 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1716 list_add_rcu(&tunnel
->list
, &pn
->l2tp_tunnel_list
);
1717 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1724 /* If tunnel's socket was created by the kernel, it doesn't
1727 if (sock
&& sock
->file
)
1732 EXPORT_SYMBOL_GPL(l2tp_tunnel_create
);
1734 /* This function is used by the netlink TUNNEL_DELETE command.
1736 int l2tp_tunnel_delete(struct l2tp_tunnel
*tunnel
)
1738 l2tp_tunnel_inc_refcount(tunnel
);
1739 if (false == queue_work(l2tp_wq
, &tunnel
->del_work
)) {
1740 l2tp_tunnel_dec_refcount(tunnel
);
1745 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete
);
1747 /* Really kill the session.
1749 void l2tp_session_free(struct l2tp_session
*session
)
1751 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1753 BUG_ON(atomic_read(&session
->ref_count
) != 0);
1756 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1757 if (session
->session_id
!= 0)
1758 atomic_dec(&l2tp_session_count
);
1759 sock_put(tunnel
->sock
);
1760 session
->tunnel
= NULL
;
1761 l2tp_tunnel_dec_refcount(tunnel
);
1766 EXPORT_SYMBOL_GPL(l2tp_session_free
);
1768 /* Remove an l2tp session from l2tp_core's hash lists.
1769 * Provides a tidyup interface for pseudowire code which can't just route all
1770 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1773 void __l2tp_session_unhash(struct l2tp_session
*session
)
1775 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1777 /* Remove the session from core hashes */
1779 /* Remove from the per-tunnel hash */
1780 write_lock_bh(&tunnel
->hlist_lock
);
1781 hlist_del_init(&session
->hlist
);
1782 write_unlock_bh(&tunnel
->hlist_lock
);
1784 /* For L2TPv3 we have a per-net hash: remove from there, too */
1785 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1786 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1787 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1788 hlist_del_init_rcu(&session
->global_hlist
);
1789 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1794 EXPORT_SYMBOL_GPL(__l2tp_session_unhash
);
1796 /* This function is used by the netlink SESSION_DELETE command and by
1799 int l2tp_session_delete(struct l2tp_session
*session
)
1802 (*session
->ref
)(session
);
1803 __l2tp_session_unhash(session
);
1804 l2tp_session_queue_purge(session
);
1805 if (session
->session_close
!= NULL
)
1806 (*session
->session_close
)(session
);
1808 (*session
->deref
)(session
);
1809 l2tp_session_dec_refcount(session
);
1812 EXPORT_SYMBOL_GPL(l2tp_session_delete
);
1814 /* We come here whenever a session's send_seq, cookie_len or
1815 * l2specific_len parameters are set.
1817 void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
)
1819 if (version
== L2TP_HDR_VER_2
) {
1820 session
->hdr_len
= 6;
1821 if (session
->send_seq
)
1822 session
->hdr_len
+= 4;
1824 session
->hdr_len
= 4 + session
->cookie_len
+ session
->l2specific_len
+ session
->offset
;
1825 if (session
->tunnel
->encap
== L2TP_ENCAPTYPE_UDP
)
1826 session
->hdr_len
+= 4;
1830 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len
);
1832 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
)
1834 struct l2tp_session
*session
;
1837 session
= kzalloc(sizeof(struct l2tp_session
) + priv_size
, GFP_KERNEL
);
1838 if (session
!= NULL
) {
1839 session
->magic
= L2TP_SESSION_MAGIC
;
1840 session
->tunnel
= tunnel
;
1842 session
->session_id
= session_id
;
1843 session
->peer_session_id
= peer_session_id
;
1845 if (tunnel
->version
== L2TP_HDR_VER_2
)
1846 session
->nr_max
= 0xffff;
1848 session
->nr_max
= 0xffffff;
1849 session
->nr_window_size
= session
->nr_max
/ 2;
1850 session
->nr_oos_count_max
= 4;
1852 /* Use NR of first received packet */
1853 session
->reorder_skip
= 1;
1855 sprintf(&session
->name
[0], "sess %u/%u",
1856 tunnel
->tunnel_id
, session
->session_id
);
1858 skb_queue_head_init(&session
->reorder_q
);
1860 INIT_HLIST_NODE(&session
->hlist
);
1861 INIT_HLIST_NODE(&session
->global_hlist
);
1863 /* Inherit debug options from tunnel */
1864 session
->debug
= tunnel
->debug
;
1867 session
->pwtype
= cfg
->pw_type
;
1868 session
->debug
= cfg
->debug
;
1869 session
->mtu
= cfg
->mtu
;
1870 session
->mru
= cfg
->mru
;
1871 session
->send_seq
= cfg
->send_seq
;
1872 session
->recv_seq
= cfg
->recv_seq
;
1873 session
->lns_mode
= cfg
->lns_mode
;
1874 session
->reorder_timeout
= cfg
->reorder_timeout
;
1875 session
->offset
= cfg
->offset
;
1876 session
->l2specific_type
= cfg
->l2specific_type
;
1877 session
->l2specific_len
= cfg
->l2specific_len
;
1878 session
->cookie_len
= cfg
->cookie_len
;
1879 memcpy(&session
->cookie
[0], &cfg
->cookie
[0], cfg
->cookie_len
);
1880 session
->peer_cookie_len
= cfg
->peer_cookie_len
;
1881 memcpy(&session
->peer_cookie
[0], &cfg
->peer_cookie
[0], cfg
->peer_cookie_len
);
1884 if (tunnel
->version
== L2TP_HDR_VER_2
)
1885 session
->build_header
= l2tp_build_l2tpv2_header
;
1887 session
->build_header
= l2tp_build_l2tpv3_header
;
1889 l2tp_session_set_header_len(session
, tunnel
->version
);
1891 err
= l2tp_session_add_to_tunnel(tunnel
, session
);
1895 return ERR_PTR(err
);
1898 /* Bump the reference count. The session context is deleted
1899 * only when this drops to zero.
1901 l2tp_session_inc_refcount(session
);
1902 l2tp_tunnel_inc_refcount(tunnel
);
1904 /* Ensure tunnel socket isn't deleted */
1905 sock_hold(tunnel
->sock
);
1907 /* Ignore management session in session count value */
1908 if (session
->session_id
!= 0)
1909 atomic_inc(&l2tp_session_count
);
1914 return ERR_PTR(-ENOMEM
);
1916 EXPORT_SYMBOL_GPL(l2tp_session_create
);
1918 /*****************************************************************************
1920 *****************************************************************************/
1922 static __net_init
int l2tp_init_net(struct net
*net
)
1924 struct l2tp_net
*pn
= net_generic(net
, l2tp_net_id
);
1927 INIT_LIST_HEAD(&pn
->l2tp_tunnel_list
);
1928 spin_lock_init(&pn
->l2tp_tunnel_list_lock
);
1930 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++)
1931 INIT_HLIST_HEAD(&pn
->l2tp_session_hlist
[hash
]);
1933 spin_lock_init(&pn
->l2tp_session_hlist_lock
);
1938 static __net_exit
void l2tp_exit_net(struct net
*net
)
1940 struct l2tp_net
*pn
= l2tp_pernet(net
);
1941 struct l2tp_tunnel
*tunnel
= NULL
;
1944 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
1945 (void)l2tp_tunnel_delete(tunnel
);
1947 rcu_read_unlock_bh();
1949 flush_workqueue(l2tp_wq
);
1953 static struct pernet_operations l2tp_net_ops
= {
1954 .init
= l2tp_init_net
,
1955 .exit
= l2tp_exit_net
,
1957 .size
= sizeof(struct l2tp_net
),
1960 static int __init
l2tp_init(void)
1964 rc
= register_pernet_device(&l2tp_net_ops
);
1968 l2tp_wq
= alloc_workqueue("l2tp", WQ_UNBOUND
, 0);
1970 pr_err("alloc_workqueue failed\n");
1971 unregister_pernet_device(&l2tp_net_ops
);
1976 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION
);
1982 static void __exit
l2tp_exit(void)
1984 unregister_pernet_device(&l2tp_net_ops
);
1986 destroy_workqueue(l2tp_wq
);
1991 module_init(l2tp_init
);
1992 module_exit(l2tp_exit
);
1994 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1995 MODULE_DESCRIPTION("L2TP core");
1996 MODULE_LICENSE("GPL");
1997 MODULE_VERSION(L2TP_DRV_VERSION
);