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_find_by_ifname(struct net
*net
, char *ifname
)
357 struct l2tp_net
*pn
= l2tp_pernet(net
);
359 struct l2tp_session
*session
;
362 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++) {
363 hlist_for_each_entry_rcu(session
, &pn
->l2tp_session_hlist
[hash
], global_hlist
) {
364 if (!strcmp(session
->ifname
, ifname
)) {
365 rcu_read_unlock_bh();
371 rcu_read_unlock_bh();
375 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname
);
377 static int l2tp_session_add_to_tunnel(struct l2tp_tunnel
*tunnel
,
378 struct l2tp_session
*session
)
380 struct l2tp_session
*session_walk
;
381 struct hlist_head
*g_head
;
382 struct hlist_head
*head
;
385 head
= l2tp_session_id_hash(tunnel
, session
->session_id
);
387 write_lock_bh(&tunnel
->hlist_lock
);
388 hlist_for_each_entry(session_walk
, head
, hlist
)
389 if (session_walk
->session_id
== session
->session_id
)
392 if (tunnel
->version
== L2TP_HDR_VER_3
) {
393 pn
= l2tp_pernet(tunnel
->l2tp_net
);
394 g_head
= l2tp_session_id_hash_2(l2tp_pernet(tunnel
->l2tp_net
),
395 session
->session_id
);
397 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
398 hlist_for_each_entry(session_walk
, g_head
, global_hlist
)
399 if (session_walk
->session_id
== session
->session_id
)
402 hlist_add_head_rcu(&session
->global_hlist
, g_head
);
403 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
406 hlist_add_head(&session
->hlist
, head
);
407 write_unlock_bh(&tunnel
->hlist_lock
);
412 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
414 write_unlock_bh(&tunnel
->hlist_lock
);
419 /* Lookup a tunnel by id
421 struct l2tp_tunnel
*l2tp_tunnel_find(struct net
*net
, u32 tunnel_id
)
423 struct l2tp_tunnel
*tunnel
;
424 struct l2tp_net
*pn
= l2tp_pernet(net
);
427 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
428 if (tunnel
->tunnel_id
== tunnel_id
) {
429 rcu_read_unlock_bh();
433 rcu_read_unlock_bh();
437 EXPORT_SYMBOL_GPL(l2tp_tunnel_find
);
439 struct l2tp_tunnel
*l2tp_tunnel_find_nth(struct net
*net
, int nth
)
441 struct l2tp_net
*pn
= l2tp_pernet(net
);
442 struct l2tp_tunnel
*tunnel
;
446 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
448 rcu_read_unlock_bh();
453 rcu_read_unlock_bh();
457 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth
);
459 /*****************************************************************************
460 * Receive data handling
461 *****************************************************************************/
463 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
466 static void l2tp_recv_queue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
468 struct sk_buff
*skbp
;
470 u32 ns
= L2TP_SKB_CB(skb
)->ns
;
472 spin_lock_bh(&session
->reorder_q
.lock
);
473 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
474 if (L2TP_SKB_CB(skbp
)->ns
> ns
) {
475 __skb_queue_before(&session
->reorder_q
, skbp
, skb
);
476 l2tp_dbg(session
, L2TP_MSG_SEQ
,
477 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
478 session
->name
, ns
, L2TP_SKB_CB(skbp
)->ns
,
479 skb_queue_len(&session
->reorder_q
));
480 atomic_long_inc(&session
->stats
.rx_oos_packets
);
485 __skb_queue_tail(&session
->reorder_q
, skb
);
488 spin_unlock_bh(&session
->reorder_q
.lock
);
491 /* Dequeue a single skb.
493 static void l2tp_recv_dequeue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
495 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
496 int length
= L2TP_SKB_CB(skb
)->length
;
498 /* We're about to requeue the skb, so return resources
499 * to its current owner (a socket receive buffer).
503 atomic_long_inc(&tunnel
->stats
.rx_packets
);
504 atomic_long_add(length
, &tunnel
->stats
.rx_bytes
);
505 atomic_long_inc(&session
->stats
.rx_packets
);
506 atomic_long_add(length
, &session
->stats
.rx_bytes
);
508 if (L2TP_SKB_CB(skb
)->has_seq
) {
511 session
->nr
&= session
->nr_max
;
513 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated nr to %hu\n",
514 session
->name
, session
->nr
);
517 /* call private receive handler */
518 if (session
->recv_skb
!= NULL
)
519 (*session
->recv_skb
)(session
, skb
, L2TP_SKB_CB(skb
)->length
);
524 (*session
->deref
)(session
);
527 /* Dequeue skbs from the session's reorder_q, subject to packet order.
528 * Skbs that have been in the queue for too long are simply discarded.
530 static void l2tp_recv_dequeue(struct l2tp_session
*session
)
535 /* If the pkt at the head of the queue has the nr that we
536 * expect to send up next, dequeue it and any other
537 * in-sequence packets behind it.
540 spin_lock_bh(&session
->reorder_q
.lock
);
541 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
542 if (time_after(jiffies
, L2TP_SKB_CB(skb
)->expires
)) {
543 atomic_long_inc(&session
->stats
.rx_seq_discards
);
544 atomic_long_inc(&session
->stats
.rx_errors
);
545 l2tp_dbg(session
, L2TP_MSG_SEQ
,
546 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
547 session
->name
, L2TP_SKB_CB(skb
)->ns
,
548 L2TP_SKB_CB(skb
)->length
, session
->nr
,
549 skb_queue_len(&session
->reorder_q
));
550 session
->reorder_skip
= 1;
551 __skb_unlink(skb
, &session
->reorder_q
);
554 (*session
->deref
)(session
);
558 if (L2TP_SKB_CB(skb
)->has_seq
) {
559 if (session
->reorder_skip
) {
560 l2tp_dbg(session
, L2TP_MSG_SEQ
,
561 "%s: advancing nr to next pkt: %u -> %u",
562 session
->name
, session
->nr
,
563 L2TP_SKB_CB(skb
)->ns
);
564 session
->reorder_skip
= 0;
565 session
->nr
= L2TP_SKB_CB(skb
)->ns
;
567 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
568 l2tp_dbg(session
, L2TP_MSG_SEQ
,
569 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
570 session
->name
, L2TP_SKB_CB(skb
)->ns
,
571 L2TP_SKB_CB(skb
)->length
, session
->nr
,
572 skb_queue_len(&session
->reorder_q
));
576 __skb_unlink(skb
, &session
->reorder_q
);
578 /* Process the skb. We release the queue lock while we
579 * do so to let other contexts process the queue.
581 spin_unlock_bh(&session
->reorder_q
.lock
);
582 l2tp_recv_dequeue_skb(session
, skb
);
587 spin_unlock_bh(&session
->reorder_q
.lock
);
590 static int l2tp_seq_check_rx_window(struct l2tp_session
*session
, u32 nr
)
594 if (nr
>= session
->nr
)
595 nws
= nr
- session
->nr
;
597 nws
= (session
->nr_max
+ 1) - (session
->nr
- nr
);
599 return nws
< session
->nr_window_size
;
602 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
603 * acceptable, else non-zero.
605 static int l2tp_recv_data_seq(struct l2tp_session
*session
, struct sk_buff
*skb
)
607 if (!l2tp_seq_check_rx_window(session
, L2TP_SKB_CB(skb
)->ns
)) {
608 /* Packet sequence number is outside allowed window.
611 l2tp_dbg(session
, L2TP_MSG_SEQ
,
612 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
613 session
->name
, L2TP_SKB_CB(skb
)->ns
,
614 L2TP_SKB_CB(skb
)->length
, session
->nr
);
618 if (session
->reorder_timeout
!= 0) {
619 /* Packet reordering enabled. Add skb to session's
620 * reorder queue, in order of ns.
622 l2tp_recv_queue_skb(session
, skb
);
626 /* Packet reordering disabled. Discard out-of-sequence packets, while
627 * tracking the number if in-sequence packets after the first OOS packet
628 * is seen. After nr_oos_count_max in-sequence packets, reset the
629 * sequence number to re-enable packet reception.
631 if (L2TP_SKB_CB(skb
)->ns
== session
->nr
) {
632 skb_queue_tail(&session
->reorder_q
, skb
);
634 u32 nr_oos
= L2TP_SKB_CB(skb
)->ns
;
635 u32 nr_next
= (session
->nr_oos
+ 1) & session
->nr_max
;
637 if (nr_oos
== nr_next
)
638 session
->nr_oos_count
++;
640 session
->nr_oos_count
= 0;
642 session
->nr_oos
= nr_oos
;
643 if (session
->nr_oos_count
> session
->nr_oos_count_max
) {
644 session
->reorder_skip
= 1;
645 l2tp_dbg(session
, L2TP_MSG_SEQ
,
646 "%s: %d oos packets received. Resetting sequence numbers\n",
647 session
->name
, session
->nr_oos_count
);
649 if (!session
->reorder_skip
) {
650 atomic_long_inc(&session
->stats
.rx_seq_discards
);
651 l2tp_dbg(session
, L2TP_MSG_SEQ
,
652 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
653 session
->name
, L2TP_SKB_CB(skb
)->ns
,
654 L2TP_SKB_CB(skb
)->length
, session
->nr
,
655 skb_queue_len(&session
->reorder_q
));
658 skb_queue_tail(&session
->reorder_q
, skb
);
668 /* Do receive processing of L2TP data frames. We handle both L2TPv2
669 * and L2TPv3 data frames here.
671 * L2TPv2 Data Message Header
674 * 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
675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
676 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
677 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
678 * | Tunnel ID | Session ID |
679 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680 * | Ns (opt) | Nr (opt) |
681 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
682 * | Offset Size (opt) | Offset pad... (opt)
683 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
685 * Data frames are marked by T=0. All other fields are the same as
686 * those in L2TP control frames.
688 * L2TPv3 Data Message Header
690 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691 * | L2TP Session Header |
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 * | L2-Specific Sublayer |
694 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * | Tunnel Payload ...
696 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
698 * L2TPv3 Session Header Over IP
701 * 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
702 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
704 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
705 * | Cookie (optional, maximum 64 bits)...
706 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
708 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
710 * L2TPv3 L2-Specific Sublayer Format
713 * 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
714 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
715 * |x|S|x|x|x|x|x|x| Sequence Number |
716 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
718 * Cookie value, sublayer format and offset (pad) are negotiated with
719 * the peer when the session is set up. Unlike L2TPv2, we do not need
720 * to parse the packet header to determine if optional fields are
723 * Caller must already have parsed the frame and determined that it is
724 * a data (not control) frame before coming here. Fields up to the
725 * session-id have already been parsed and ptr points to the data
726 * after the session-id.
728 * session->ref() must have been called prior to l2tp_recv_common().
729 * session->deref() will be called automatically after skb is processed.
731 void l2tp_recv_common(struct l2tp_session
*session
, struct sk_buff
*skb
,
732 unsigned char *ptr
, unsigned char *optr
, u16 hdrflags
,
733 int length
, int (*payload_hook
)(struct sk_buff
*skb
))
735 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
739 /* Parse and check optional cookie */
740 if (session
->peer_cookie_len
> 0) {
741 if (memcmp(ptr
, &session
->peer_cookie
[0], session
->peer_cookie_len
)) {
742 l2tp_info(tunnel
, L2TP_MSG_DATA
,
743 "%s: cookie mismatch (%u/%u). Discarding.\n",
744 tunnel
->name
, tunnel
->tunnel_id
,
745 session
->session_id
);
746 atomic_long_inc(&session
->stats
.rx_cookie_discards
);
749 ptr
+= session
->peer_cookie_len
;
752 /* Handle the optional sequence numbers. Sequence numbers are
753 * in different places for L2TPv2 and L2TPv3.
755 * If we are the LAC, enable/disable sequence numbers under
756 * the control of the LNS. If no sequence numbers present but
757 * we were expecting them, discard frame.
760 L2TP_SKB_CB(skb
)->has_seq
= 0;
761 if (tunnel
->version
== L2TP_HDR_VER_2
) {
762 if (hdrflags
& L2TP_HDRFLAG_S
) {
763 ns
= ntohs(*(__be16
*) ptr
);
765 nr
= ntohs(*(__be16
*) ptr
);
768 /* Store L2TP info in the skb */
769 L2TP_SKB_CB(skb
)->ns
= ns
;
770 L2TP_SKB_CB(skb
)->has_seq
= 1;
772 l2tp_dbg(session
, L2TP_MSG_SEQ
,
773 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
774 session
->name
, ns
, nr
, session
->nr
);
776 } else if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
777 u32 l2h
= ntohl(*(__be32
*) ptr
);
779 if (l2h
& 0x40000000) {
780 ns
= l2h
& 0x00ffffff;
782 /* Store L2TP info in the skb */
783 L2TP_SKB_CB(skb
)->ns
= ns
;
784 L2TP_SKB_CB(skb
)->has_seq
= 1;
786 l2tp_dbg(session
, L2TP_MSG_SEQ
,
787 "%s: recv data ns=%u, session nr=%u\n",
788 session
->name
, ns
, session
->nr
);
792 /* Advance past L2-specific header, if present */
793 ptr
+= session
->l2specific_len
;
795 if (L2TP_SKB_CB(skb
)->has_seq
) {
796 /* Received a packet with sequence numbers. If we're the LNS,
797 * check if we sre sending sequence numbers and if not,
800 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
801 l2tp_info(session
, L2TP_MSG_SEQ
,
802 "%s: requested to enable seq numbers by LNS\n",
804 session
->send_seq
= 1;
805 l2tp_session_set_header_len(session
, tunnel
->version
);
808 /* No sequence numbers.
809 * If user has configured mandatory sequence numbers, discard.
811 if (session
->recv_seq
) {
812 l2tp_warn(session
, L2TP_MSG_SEQ
,
813 "%s: recv data has no seq numbers when required. Discarding.\n",
815 atomic_long_inc(&session
->stats
.rx_seq_discards
);
819 /* If we're the LAC and we're sending sequence numbers, the
820 * LNS has requested that we no longer send sequence numbers.
821 * If we're the LNS and we're sending sequence numbers, the
822 * LAC is broken. Discard the frame.
824 if ((!session
->lns_mode
) && (session
->send_seq
)) {
825 l2tp_info(session
, L2TP_MSG_SEQ
,
826 "%s: requested to disable seq numbers by LNS\n",
828 session
->send_seq
= 0;
829 l2tp_session_set_header_len(session
, tunnel
->version
);
830 } else if (session
->send_seq
) {
831 l2tp_warn(session
, L2TP_MSG_SEQ
,
832 "%s: recv data has no seq numbers when required. Discarding.\n",
834 atomic_long_inc(&session
->stats
.rx_seq_discards
);
839 /* Session data offset is handled differently for L2TPv2 and
840 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
841 * the header. For L2TPv3, the offset is negotiated using AVPs
842 * in the session setup control protocol.
844 if (tunnel
->version
== L2TP_HDR_VER_2
) {
845 /* If offset bit set, skip it. */
846 if (hdrflags
& L2TP_HDRFLAG_O
) {
847 offset
= ntohs(*(__be16
*)ptr
);
851 ptr
+= session
->offset
;
854 if (!pskb_may_pull(skb
, offset
))
857 __skb_pull(skb
, offset
);
859 /* If caller wants to process the payload before we queue the
863 if ((*payload_hook
)(skb
))
866 /* Prepare skb for adding to the session's reorder_q. Hold
867 * packets for max reorder_timeout or 1 second if not
870 L2TP_SKB_CB(skb
)->length
= length
;
871 L2TP_SKB_CB(skb
)->expires
= jiffies
+
872 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
874 /* Add packet to the session's receive queue. Reordering is done here, if
875 * enabled. Saved L2TP protocol info is stored in skb->sb[].
877 if (L2TP_SKB_CB(skb
)->has_seq
) {
878 if (l2tp_recv_data_seq(session
, skb
))
881 /* No sequence numbers. Add the skb to the tail of the
882 * reorder queue. This ensures that it will be
883 * delivered after all previous sequenced skbs.
885 skb_queue_tail(&session
->reorder_q
, skb
);
888 /* Try to dequeue as many skbs from reorder_q as we can. */
889 l2tp_recv_dequeue(session
);
894 atomic_long_inc(&session
->stats
.rx_errors
);
898 (*session
->deref
)(session
);
900 EXPORT_SYMBOL(l2tp_recv_common
);
902 /* Drop skbs from the session's reorder_q
904 int l2tp_session_queue_purge(struct l2tp_session
*session
)
906 struct sk_buff
*skb
= NULL
;
908 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
909 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
910 atomic_long_inc(&session
->stats
.rx_errors
);
913 (*session
->deref
)(session
);
917 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge
);
919 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
920 * here. The skb is not on a list when we get here.
921 * Returns 0 if the packet was a data packet and was successfully passed on.
922 * Returns 1 if the packet was not a good data packet and could not be
923 * forwarded. All such packets are passed up to userspace to deal with.
925 static int l2tp_udp_recv_core(struct l2tp_tunnel
*tunnel
, struct sk_buff
*skb
,
926 int (*payload_hook
)(struct sk_buff
*skb
))
928 struct l2tp_session
*session
= NULL
;
929 unsigned char *ptr
, *optr
;
931 u32 tunnel_id
, session_id
;
935 /* UDP has verifed checksum */
937 /* UDP always verifies the packet length. */
938 __skb_pull(skb
, sizeof(struct udphdr
));
941 if (!pskb_may_pull(skb
, L2TP_HDR_SIZE_SEQ
)) {
942 l2tp_info(tunnel
, L2TP_MSG_DATA
,
943 "%s: recv short packet (len=%d)\n",
944 tunnel
->name
, skb
->len
);
948 /* Trace packet contents, if enabled */
949 if (tunnel
->debug
& L2TP_MSG_DATA
) {
950 length
= min(32u, skb
->len
);
951 if (!pskb_may_pull(skb
, length
))
954 pr_debug("%s: recv\n", tunnel
->name
);
955 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
958 /* Point to L2TP header */
959 optr
= ptr
= skb
->data
;
961 /* Get L2TP header flags */
962 hdrflags
= ntohs(*(__be16
*) ptr
);
964 /* Check protocol version */
965 version
= hdrflags
& L2TP_HDR_VER_MASK
;
966 if (version
!= tunnel
->version
) {
967 l2tp_info(tunnel
, L2TP_MSG_DATA
,
968 "%s: recv protocol version mismatch: got %d expected %d\n",
969 tunnel
->name
, version
, tunnel
->version
);
973 /* Get length of L2TP packet */
976 /* If type is control packet, it is handled by userspace. */
977 if (hdrflags
& L2TP_HDRFLAG_T
) {
978 l2tp_dbg(tunnel
, L2TP_MSG_DATA
,
979 "%s: recv control packet, len=%d\n",
980 tunnel
->name
, length
);
987 if (tunnel
->version
== L2TP_HDR_VER_2
) {
988 /* If length is present, skip it */
989 if (hdrflags
& L2TP_HDRFLAG_L
)
992 /* Extract tunnel and session ID */
993 tunnel_id
= ntohs(*(__be16
*) ptr
);
995 session_id
= ntohs(*(__be16
*) ptr
);
998 ptr
+= 2; /* skip reserved bits */
999 tunnel_id
= tunnel
->tunnel_id
;
1000 session_id
= ntohl(*(__be32
*) ptr
);
1004 /* Find the session context */
1005 session
= l2tp_session_get(tunnel
->l2tp_net
, tunnel
, session_id
, true);
1006 if (!session
|| !session
->recv_skb
) {
1009 session
->deref(session
);
1010 l2tp_session_dec_refcount(session
);
1013 /* Not found? Pass to userspace to deal with */
1014 l2tp_info(tunnel
, L2TP_MSG_DATA
,
1015 "%s: no session found (%u/%u). Passing up.\n",
1016 tunnel
->name
, tunnel_id
, session_id
);
1020 l2tp_recv_common(session
, skb
, ptr
, optr
, hdrflags
, length
, payload_hook
);
1021 l2tp_session_dec_refcount(session
);
1026 /* Put UDP header back */
1027 __skb_push(skb
, sizeof(struct udphdr
));
1032 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
1036 * >0: skb should be passed up to userspace as UDP.
1038 int l2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
1040 struct l2tp_tunnel
*tunnel
;
1042 tunnel
= l2tp_sock_to_tunnel(sk
);
1046 l2tp_dbg(tunnel
, L2TP_MSG_DATA
, "%s: received %d bytes\n",
1047 tunnel
->name
, skb
->len
);
1049 if (l2tp_udp_recv_core(tunnel
, skb
, tunnel
->recv_payload_hook
))
1060 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv
);
1062 /************************************************************************
1064 ***********************************************************************/
1066 /* Build an L2TP header for the session into the buffer provided.
1068 static int l2tp_build_l2tpv2_header(struct l2tp_session
*session
, void *buf
)
1070 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1073 u16 flags
= L2TP_HDR_VER_2
;
1074 u32 tunnel_id
= tunnel
->peer_tunnel_id
;
1075 u32 session_id
= session
->peer_session_id
;
1077 if (session
->send_seq
)
1078 flags
|= L2TP_HDRFLAG_S
;
1080 /* Setup L2TP header. */
1081 *bufp
++ = htons(flags
);
1082 *bufp
++ = htons(tunnel_id
);
1083 *bufp
++ = htons(session_id
);
1084 if (session
->send_seq
) {
1085 *bufp
++ = htons(session
->ns
);
1088 session
->ns
&= 0xffff;
1089 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated ns to %u\n",
1090 session
->name
, session
->ns
);
1096 static int l2tp_build_l2tpv3_header(struct l2tp_session
*session
, void *buf
)
1098 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1102 /* Setup L2TP header. The header differs slightly for UDP and
1103 * IP encapsulations. For UDP, there is 4 bytes of flags.
1105 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
1106 u16 flags
= L2TP_HDR_VER_3
;
1107 *((__be16
*) bufp
) = htons(flags
);
1109 *((__be16
*) bufp
) = 0;
1113 *((__be32
*) bufp
) = htonl(session
->peer_session_id
);
1115 if (session
->cookie_len
) {
1116 memcpy(bufp
, &session
->cookie
[0], session
->cookie_len
);
1117 bufp
+= session
->cookie_len
;
1119 if (session
->l2specific_len
) {
1120 if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
1122 if (session
->send_seq
) {
1123 l2h
= 0x40000000 | session
->ns
;
1125 session
->ns
&= 0xffffff;
1126 l2tp_dbg(session
, L2TP_MSG_SEQ
,
1127 "%s: updated ns to %u\n",
1128 session
->name
, session
->ns
);
1131 *((__be32
*) bufp
) = htonl(l2h
);
1133 bufp
+= session
->l2specific_len
;
1135 if (session
->offset
)
1136 bufp
+= session
->offset
;
1141 static int l2tp_xmit_core(struct l2tp_session
*session
, struct sk_buff
*skb
,
1142 struct flowi
*fl
, size_t data_len
)
1144 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1145 unsigned int len
= skb
->len
;
1149 if (session
->send_seq
)
1150 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %zd bytes, ns=%u\n",
1151 session
->name
, data_len
, session
->ns
- 1);
1153 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %zd bytes\n",
1154 session
->name
, data_len
);
1156 if (session
->debug
& L2TP_MSG_DATA
) {
1157 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1158 unsigned char *datap
= skb
->data
+ uhlen
;
1160 pr_debug("%s: xmit\n", session
->name
);
1161 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1162 datap
, min_t(size_t, 32, len
- uhlen
));
1165 /* Queue the packet to IP for output */
1167 #if IS_ENABLED(CONFIG_IPV6)
1168 if (tunnel
->sock
->sk_family
== PF_INET6
&& !tunnel
->v4mapped
)
1169 error
= inet6_csk_xmit(tunnel
->sock
, skb
, NULL
);
1172 error
= ip_queue_xmit(tunnel
->sock
, skb
, fl
);
1176 atomic_long_inc(&tunnel
->stats
.tx_packets
);
1177 atomic_long_add(len
, &tunnel
->stats
.tx_bytes
);
1178 atomic_long_inc(&session
->stats
.tx_packets
);
1179 atomic_long_add(len
, &session
->stats
.tx_bytes
);
1181 atomic_long_inc(&tunnel
->stats
.tx_errors
);
1182 atomic_long_inc(&session
->stats
.tx_errors
);
1188 /* If caller requires the skb to have a ppp header, the header must be
1189 * inserted in the skb data before calling this function.
1191 int l2tp_xmit_skb(struct l2tp_session
*session
, struct sk_buff
*skb
, int hdr_len
)
1193 int data_len
= skb
->len
;
1194 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1195 struct sock
*sk
= tunnel
->sock
;
1198 struct inet_sock
*inet
;
1200 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1202 int ret
= NET_XMIT_SUCCESS
;
1204 /* Check that there's enough headroom in the skb to insert IP,
1205 * UDP and L2TP headers. If not enough, expand it to
1206 * make room. Adjust truesize.
1208 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1210 if (skb_cow_head(skb
, headroom
)) {
1212 return NET_XMIT_DROP
;
1215 /* Setup L2TP header */
1216 session
->build_header(session
, __skb_push(skb
, hdr_len
));
1218 /* Reset skb netfilter state */
1219 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1220 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1225 if (sock_owned_by_user(sk
)) {
1227 ret
= NET_XMIT_DROP
;
1231 /* Get routing info from the tunnel socket */
1233 skb_dst_set(skb
, dst_clone(__sk_dst_check(sk
, 0)));
1236 fl
= &inet
->cork
.fl
;
1237 switch (tunnel
->encap
) {
1238 case L2TP_ENCAPTYPE_UDP
:
1239 /* Setup UDP header */
1240 __skb_push(skb
, sizeof(*uh
));
1241 skb_reset_transport_header(skb
);
1243 uh
->source
= inet
->inet_sport
;
1244 uh
->dest
= inet
->inet_dport
;
1245 udp_len
= uhlen
+ hdr_len
+ data_len
;
1246 uh
->len
= htons(udp_len
);
1248 /* Calculate UDP checksum if configured to do so */
1249 #if IS_ENABLED(CONFIG_IPV6)
1250 if (sk
->sk_family
== PF_INET6
&& !tunnel
->v4mapped
)
1251 udp6_set_csum(udp_get_no_check6_tx(sk
),
1252 skb
, &inet6_sk(sk
)->saddr
,
1253 &sk
->sk_v6_daddr
, udp_len
);
1256 udp_set_csum(sk
->sk_no_check_tx
, skb
, inet
->inet_saddr
,
1257 inet
->inet_daddr
, udp_len
);
1260 case L2TP_ENCAPTYPE_IP
:
1264 l2tp_xmit_core(session
, skb
, fl
, data_len
);
1270 EXPORT_SYMBOL_GPL(l2tp_xmit_skb
);
1272 /*****************************************************************************
1273 * Tinnel and session create/destroy.
1274 *****************************************************************************/
1276 /* Tunnel socket destruct hook.
1277 * The tunnel context is deleted only when all session sockets have been
1280 static void l2tp_tunnel_destruct(struct sock
*sk
)
1282 struct l2tp_tunnel
*tunnel
= l2tp_tunnel(sk
);
1283 struct l2tp_net
*pn
;
1288 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing...\n", tunnel
->name
);
1291 /* Disable udp encapsulation */
1292 switch (tunnel
->encap
) {
1293 case L2TP_ENCAPTYPE_UDP
:
1294 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1295 (udp_sk(sk
))->encap_type
= 0;
1296 (udp_sk(sk
))->encap_rcv
= NULL
;
1297 (udp_sk(sk
))->encap_destroy
= NULL
;
1299 case L2TP_ENCAPTYPE_IP
:
1303 /* Remove hooks into tunnel socket */
1304 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1305 sk
->sk_user_data
= NULL
;
1306 tunnel
->sock
= NULL
;
1308 /* Remove the tunnel struct from the tunnel list */
1309 pn
= l2tp_pernet(tunnel
->l2tp_net
);
1310 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1311 list_del_rcu(&tunnel
->list
);
1312 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1313 atomic_dec(&l2tp_tunnel_count
);
1315 l2tp_tunnel_closeall(tunnel
);
1316 l2tp_tunnel_dec_refcount(tunnel
);
1318 /* Call the original destructor */
1319 if (sk
->sk_destruct
)
1320 (*sk
->sk_destruct
)(sk
);
1325 /* When the tunnel is closed, all the attached sessions need to go too.
1327 void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
)
1330 struct hlist_node
*walk
;
1331 struct hlist_node
*tmp
;
1332 struct l2tp_session
*session
;
1334 BUG_ON(tunnel
== NULL
);
1336 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing all sessions...\n",
1339 write_lock_bh(&tunnel
->hlist_lock
);
1340 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
1342 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1343 session
= hlist_entry(walk
, struct l2tp_session
, hlist
);
1345 l2tp_info(session
, L2TP_MSG_CONTROL
,
1346 "%s: closing session\n", session
->name
);
1348 hlist_del_init(&session
->hlist
);
1350 if (session
->ref
!= NULL
)
1351 (*session
->ref
)(session
);
1353 write_unlock_bh(&tunnel
->hlist_lock
);
1355 __l2tp_session_unhash(session
);
1356 l2tp_session_queue_purge(session
);
1358 if (session
->session_close
!= NULL
)
1359 (*session
->session_close
)(session
);
1361 if (session
->deref
!= NULL
)
1362 (*session
->deref
)(session
);
1364 l2tp_session_dec_refcount(session
);
1366 write_lock_bh(&tunnel
->hlist_lock
);
1368 /* Now restart from the beginning of this hash
1369 * chain. We always remove a session from the
1370 * list so we are guaranteed to make forward
1376 write_unlock_bh(&tunnel
->hlist_lock
);
1378 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall
);
1380 /* Tunnel socket destroy hook for UDP encapsulation */
1381 static void l2tp_udp_encap_destroy(struct sock
*sk
)
1383 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
1385 l2tp_tunnel_closeall(tunnel
);
1390 /* Really kill the tunnel.
1391 * Come here only when all sessions have been cleared from the tunnel.
1393 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
)
1395 BUG_ON(atomic_read(&tunnel
->ref_count
) != 0);
1396 BUG_ON(tunnel
->sock
!= NULL
);
1397 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: free...\n", tunnel
->name
);
1398 kfree_rcu(tunnel
, rcu
);
1401 /* Workqueue tunnel deletion function */
1402 static void l2tp_tunnel_del_work(struct work_struct
*work
)
1404 struct l2tp_tunnel
*tunnel
= NULL
;
1405 struct socket
*sock
= NULL
;
1406 struct sock
*sk
= NULL
;
1408 tunnel
= container_of(work
, struct l2tp_tunnel
, del_work
);
1410 l2tp_tunnel_closeall(tunnel
);
1412 sk
= l2tp_tunnel_sock_lookup(tunnel
);
1416 sock
= sk
->sk_socket
;
1418 /* If the tunnel socket was created by userspace, then go through the
1419 * inet layer to shut the socket down, and let userspace close it.
1420 * Otherwise, if we created the socket directly within the kernel, use
1421 * the sk API to release it here.
1422 * In either case the tunnel resources are freed in the socket
1423 * destructor when the tunnel socket goes away.
1425 if (tunnel
->fd
>= 0) {
1427 inet_shutdown(sock
, 2);
1430 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1435 l2tp_tunnel_sock_put(sk
);
1437 l2tp_tunnel_dec_refcount(tunnel
);
1440 /* Create a socket for the tunnel, if one isn't set up by
1441 * userspace. This is used for static tunnels where there is no
1442 * managing L2TP daemon.
1444 * Since we don't want these sockets to keep a namespace alive by
1445 * themselves, we drop the socket's namespace refcount after creation.
1446 * These sockets are freed when the namespace exits using the pernet
1449 static int l2tp_tunnel_sock_create(struct net
*net
,
1452 struct l2tp_tunnel_cfg
*cfg
,
1453 struct socket
**sockp
)
1456 struct socket
*sock
= NULL
;
1457 struct udp_port_cfg udp_conf
;
1459 switch (cfg
->encap
) {
1460 case L2TP_ENCAPTYPE_UDP
:
1461 memset(&udp_conf
, 0, sizeof(udp_conf
));
1463 #if IS_ENABLED(CONFIG_IPV6)
1464 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1465 udp_conf
.family
= AF_INET6
;
1466 memcpy(&udp_conf
.local_ip6
, cfg
->local_ip6
,
1467 sizeof(udp_conf
.local_ip6
));
1468 memcpy(&udp_conf
.peer_ip6
, cfg
->peer_ip6
,
1469 sizeof(udp_conf
.peer_ip6
));
1470 udp_conf
.use_udp6_tx_checksums
=
1471 ! cfg
->udp6_zero_tx_checksums
;
1472 udp_conf
.use_udp6_rx_checksums
=
1473 ! cfg
->udp6_zero_rx_checksums
;
1477 udp_conf
.family
= AF_INET
;
1478 udp_conf
.local_ip
= cfg
->local_ip
;
1479 udp_conf
.peer_ip
= cfg
->peer_ip
;
1480 udp_conf
.use_udp_checksums
= cfg
->use_udp_checksums
;
1483 udp_conf
.local_udp_port
= htons(cfg
->local_udp_port
);
1484 udp_conf
.peer_udp_port
= htons(cfg
->peer_udp_port
);
1486 err
= udp_sock_create(net
, &udp_conf
, &sock
);
1492 case L2TP_ENCAPTYPE_IP
:
1493 #if IS_ENABLED(CONFIG_IPV6)
1494 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1495 struct sockaddr_l2tpip6 ip6_addr
= {0};
1497 err
= sock_create_kern(net
, AF_INET6
, SOCK_DGRAM
,
1498 IPPROTO_L2TP
, &sock
);
1502 ip6_addr
.l2tp_family
= AF_INET6
;
1503 memcpy(&ip6_addr
.l2tp_addr
, cfg
->local_ip6
,
1504 sizeof(ip6_addr
.l2tp_addr
));
1505 ip6_addr
.l2tp_conn_id
= tunnel_id
;
1506 err
= kernel_bind(sock
, (struct sockaddr
*) &ip6_addr
,
1511 ip6_addr
.l2tp_family
= AF_INET6
;
1512 memcpy(&ip6_addr
.l2tp_addr
, cfg
->peer_ip6
,
1513 sizeof(ip6_addr
.l2tp_addr
));
1514 ip6_addr
.l2tp_conn_id
= peer_tunnel_id
;
1515 err
= kernel_connect(sock
,
1516 (struct sockaddr
*) &ip6_addr
,
1517 sizeof(ip6_addr
), 0);
1523 struct sockaddr_l2tpip ip_addr
= {0};
1525 err
= sock_create_kern(net
, AF_INET
, SOCK_DGRAM
,
1526 IPPROTO_L2TP
, &sock
);
1530 ip_addr
.l2tp_family
= AF_INET
;
1531 ip_addr
.l2tp_addr
= cfg
->local_ip
;
1532 ip_addr
.l2tp_conn_id
= tunnel_id
;
1533 err
= kernel_bind(sock
, (struct sockaddr
*) &ip_addr
,
1538 ip_addr
.l2tp_family
= AF_INET
;
1539 ip_addr
.l2tp_addr
= cfg
->peer_ip
;
1540 ip_addr
.l2tp_conn_id
= peer_tunnel_id
;
1541 err
= kernel_connect(sock
, (struct sockaddr
*) &ip_addr
,
1542 sizeof(ip_addr
), 0);
1554 if ((err
< 0) && sock
) {
1555 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1563 static struct lock_class_key l2tp_socket_class
;
1565 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
)
1567 struct l2tp_tunnel
*tunnel
= NULL
;
1569 struct socket
*sock
= NULL
;
1570 struct sock
*sk
= NULL
;
1571 struct l2tp_net
*pn
;
1572 enum l2tp_encap_type encap
= L2TP_ENCAPTYPE_UDP
;
1574 /* Get the tunnel socket from the fd, which was opened by
1575 * the userspace L2TP daemon. If not specified, create a
1579 err
= l2tp_tunnel_sock_create(net
, tunnel_id
, peer_tunnel_id
,
1584 sock
= sockfd_lookup(fd
, &err
);
1586 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1587 tunnel_id
, fd
, err
);
1592 /* Reject namespace mismatches */
1593 if (!net_eq(sock_net(sock
->sk
), net
)) {
1594 pr_err("tunl %u: netns mismatch\n", tunnel_id
);
1605 /* Quick sanity checks */
1607 case L2TP_ENCAPTYPE_UDP
:
1608 err
= -EPROTONOSUPPORT
;
1609 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1610 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1611 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1615 case L2TP_ENCAPTYPE_IP
:
1616 err
= -EPROTONOSUPPORT
;
1617 if (sk
->sk_protocol
!= IPPROTO_L2TP
) {
1618 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1619 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_L2TP
);
1625 /* Check if this socket has already been prepped */
1626 tunnel
= l2tp_tunnel(sk
);
1627 if (tunnel
!= NULL
) {
1628 /* This socket has already been prepped */
1633 tunnel
= kzalloc(sizeof(struct l2tp_tunnel
), GFP_KERNEL
);
1634 if (tunnel
== NULL
) {
1639 tunnel
->version
= version
;
1640 tunnel
->tunnel_id
= tunnel_id
;
1641 tunnel
->peer_tunnel_id
= peer_tunnel_id
;
1642 tunnel
->debug
= L2TP_DEFAULT_DEBUG_FLAGS
;
1644 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1645 sprintf(&tunnel
->name
[0], "tunl %u", tunnel_id
);
1646 rwlock_init(&tunnel
->hlist_lock
);
1648 /* The net we belong to */
1649 tunnel
->l2tp_net
= net
;
1650 pn
= l2tp_pernet(net
);
1653 tunnel
->debug
= cfg
->debug
;
1655 #if IS_ENABLED(CONFIG_IPV6)
1656 if (sk
->sk_family
== PF_INET6
) {
1657 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1659 if (ipv6_addr_v4mapped(&np
->saddr
) &&
1660 ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
1661 struct inet_sock
*inet
= inet_sk(sk
);
1663 tunnel
->v4mapped
= true;
1664 inet
->inet_saddr
= np
->saddr
.s6_addr32
[3];
1665 inet
->inet_rcv_saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
[3];
1666 inet
->inet_daddr
= sk
->sk_v6_daddr
.s6_addr32
[3];
1668 tunnel
->v4mapped
= false;
1673 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1674 tunnel
->encap
= encap
;
1675 if (encap
== L2TP_ENCAPTYPE_UDP
) {
1676 struct udp_tunnel_sock_cfg udp_cfg
= { };
1678 udp_cfg
.sk_user_data
= tunnel
;
1679 udp_cfg
.encap_type
= UDP_ENCAP_L2TPINUDP
;
1680 udp_cfg
.encap_rcv
= l2tp_udp_encap_recv
;
1681 udp_cfg
.encap_destroy
= l2tp_udp_encap_destroy
;
1683 setup_udp_tunnel_sock(net
, sock
, &udp_cfg
);
1685 sk
->sk_user_data
= tunnel
;
1688 /* Hook on the tunnel socket destructor so that we can cleanup
1689 * if the tunnel socket goes away.
1691 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1692 sk
->sk_destruct
= &l2tp_tunnel_destruct
;
1695 lockdep_set_class_and_name(&sk
->sk_lock
.slock
, &l2tp_socket_class
, "l2tp_sock");
1697 sk
->sk_allocation
= GFP_ATOMIC
;
1699 /* Init delete workqueue struct */
1700 INIT_WORK(&tunnel
->del_work
, l2tp_tunnel_del_work
);
1702 /* Add tunnel to our list */
1703 INIT_LIST_HEAD(&tunnel
->list
);
1704 atomic_inc(&l2tp_tunnel_count
);
1706 /* Bump the reference count. The tunnel context is deleted
1707 * only when this drops to zero. Must be done before list insertion
1709 l2tp_tunnel_inc_refcount(tunnel
);
1710 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1711 list_add_rcu(&tunnel
->list
, &pn
->l2tp_tunnel_list
);
1712 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1719 /* If tunnel's socket was created by the kernel, it doesn't
1722 if (sock
&& sock
->file
)
1727 EXPORT_SYMBOL_GPL(l2tp_tunnel_create
);
1729 /* This function is used by the netlink TUNNEL_DELETE command.
1731 int l2tp_tunnel_delete(struct l2tp_tunnel
*tunnel
)
1733 l2tp_tunnel_inc_refcount(tunnel
);
1734 if (false == queue_work(l2tp_wq
, &tunnel
->del_work
)) {
1735 l2tp_tunnel_dec_refcount(tunnel
);
1740 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete
);
1742 /* Really kill the session.
1744 void l2tp_session_free(struct l2tp_session
*session
)
1746 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1748 BUG_ON(atomic_read(&session
->ref_count
) != 0);
1751 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1752 if (session
->session_id
!= 0)
1753 atomic_dec(&l2tp_session_count
);
1754 sock_put(tunnel
->sock
);
1755 session
->tunnel
= NULL
;
1756 l2tp_tunnel_dec_refcount(tunnel
);
1761 EXPORT_SYMBOL_GPL(l2tp_session_free
);
1763 /* Remove an l2tp session from l2tp_core's hash lists.
1764 * Provides a tidyup interface for pseudowire code which can't just route all
1765 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1768 void __l2tp_session_unhash(struct l2tp_session
*session
)
1770 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1772 /* Remove the session from core hashes */
1774 /* Remove from the per-tunnel hash */
1775 write_lock_bh(&tunnel
->hlist_lock
);
1776 hlist_del_init(&session
->hlist
);
1777 write_unlock_bh(&tunnel
->hlist_lock
);
1779 /* For L2TPv3 we have a per-net hash: remove from there, too */
1780 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1781 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1782 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1783 hlist_del_init_rcu(&session
->global_hlist
);
1784 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1789 EXPORT_SYMBOL_GPL(__l2tp_session_unhash
);
1791 /* This function is used by the netlink SESSION_DELETE command and by
1794 int l2tp_session_delete(struct l2tp_session
*session
)
1797 (*session
->ref
)(session
);
1798 __l2tp_session_unhash(session
);
1799 l2tp_session_queue_purge(session
);
1800 if (session
->session_close
!= NULL
)
1801 (*session
->session_close
)(session
);
1803 (*session
->deref
)(session
);
1804 l2tp_session_dec_refcount(session
);
1807 EXPORT_SYMBOL_GPL(l2tp_session_delete
);
1809 /* We come here whenever a session's send_seq, cookie_len or
1810 * l2specific_len parameters are set.
1812 void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
)
1814 if (version
== L2TP_HDR_VER_2
) {
1815 session
->hdr_len
= 6;
1816 if (session
->send_seq
)
1817 session
->hdr_len
+= 4;
1819 session
->hdr_len
= 4 + session
->cookie_len
+ session
->l2specific_len
+ session
->offset
;
1820 if (session
->tunnel
->encap
== L2TP_ENCAPTYPE_UDP
)
1821 session
->hdr_len
+= 4;
1825 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len
);
1827 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
)
1829 struct l2tp_session
*session
;
1832 session
= kzalloc(sizeof(struct l2tp_session
) + priv_size
, GFP_KERNEL
);
1833 if (session
!= NULL
) {
1834 session
->magic
= L2TP_SESSION_MAGIC
;
1835 session
->tunnel
= tunnel
;
1837 session
->session_id
= session_id
;
1838 session
->peer_session_id
= peer_session_id
;
1840 if (tunnel
->version
== L2TP_HDR_VER_2
)
1841 session
->nr_max
= 0xffff;
1843 session
->nr_max
= 0xffffff;
1844 session
->nr_window_size
= session
->nr_max
/ 2;
1845 session
->nr_oos_count_max
= 4;
1847 /* Use NR of first received packet */
1848 session
->reorder_skip
= 1;
1850 sprintf(&session
->name
[0], "sess %u/%u",
1851 tunnel
->tunnel_id
, session
->session_id
);
1853 skb_queue_head_init(&session
->reorder_q
);
1855 INIT_HLIST_NODE(&session
->hlist
);
1856 INIT_HLIST_NODE(&session
->global_hlist
);
1858 /* Inherit debug options from tunnel */
1859 session
->debug
= tunnel
->debug
;
1862 session
->pwtype
= cfg
->pw_type
;
1863 session
->debug
= cfg
->debug
;
1864 session
->mtu
= cfg
->mtu
;
1865 session
->mru
= cfg
->mru
;
1866 session
->send_seq
= cfg
->send_seq
;
1867 session
->recv_seq
= cfg
->recv_seq
;
1868 session
->lns_mode
= cfg
->lns_mode
;
1869 session
->reorder_timeout
= cfg
->reorder_timeout
;
1870 session
->offset
= cfg
->offset
;
1871 session
->l2specific_type
= cfg
->l2specific_type
;
1872 session
->l2specific_len
= cfg
->l2specific_len
;
1873 session
->cookie_len
= cfg
->cookie_len
;
1874 memcpy(&session
->cookie
[0], &cfg
->cookie
[0], cfg
->cookie_len
);
1875 session
->peer_cookie_len
= cfg
->peer_cookie_len
;
1876 memcpy(&session
->peer_cookie
[0], &cfg
->peer_cookie
[0], cfg
->peer_cookie_len
);
1879 if (tunnel
->version
== L2TP_HDR_VER_2
)
1880 session
->build_header
= l2tp_build_l2tpv2_header
;
1882 session
->build_header
= l2tp_build_l2tpv3_header
;
1884 l2tp_session_set_header_len(session
, tunnel
->version
);
1886 err
= l2tp_session_add_to_tunnel(tunnel
, session
);
1890 return ERR_PTR(err
);
1893 /* Bump the reference count. The session context is deleted
1894 * only when this drops to zero.
1896 l2tp_session_inc_refcount(session
);
1897 l2tp_tunnel_inc_refcount(tunnel
);
1899 /* Ensure tunnel socket isn't deleted */
1900 sock_hold(tunnel
->sock
);
1902 /* Ignore management session in session count value */
1903 if (session
->session_id
!= 0)
1904 atomic_inc(&l2tp_session_count
);
1909 return ERR_PTR(-ENOMEM
);
1911 EXPORT_SYMBOL_GPL(l2tp_session_create
);
1913 /*****************************************************************************
1915 *****************************************************************************/
1917 static __net_init
int l2tp_init_net(struct net
*net
)
1919 struct l2tp_net
*pn
= net_generic(net
, l2tp_net_id
);
1922 INIT_LIST_HEAD(&pn
->l2tp_tunnel_list
);
1923 spin_lock_init(&pn
->l2tp_tunnel_list_lock
);
1925 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++)
1926 INIT_HLIST_HEAD(&pn
->l2tp_session_hlist
[hash
]);
1928 spin_lock_init(&pn
->l2tp_session_hlist_lock
);
1933 static __net_exit
void l2tp_exit_net(struct net
*net
)
1935 struct l2tp_net
*pn
= l2tp_pernet(net
);
1936 struct l2tp_tunnel
*tunnel
= NULL
;
1939 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
1940 (void)l2tp_tunnel_delete(tunnel
);
1942 rcu_read_unlock_bh();
1944 flush_workqueue(l2tp_wq
);
1948 static struct pernet_operations l2tp_net_ops
= {
1949 .init
= l2tp_init_net
,
1950 .exit
= l2tp_exit_net
,
1952 .size
= sizeof(struct l2tp_net
),
1955 static int __init
l2tp_init(void)
1959 rc
= register_pernet_device(&l2tp_net_ops
);
1963 l2tp_wq
= alloc_workqueue("l2tp", WQ_UNBOUND
, 0);
1965 pr_err("alloc_workqueue failed\n");
1966 unregister_pernet_device(&l2tp_net_ops
);
1971 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION
);
1977 static void __exit
l2tp_exit(void)
1979 unregister_pernet_device(&l2tp_net_ops
);
1981 destroy_workqueue(l2tp_wq
);
1986 module_init(l2tp_init
);
1987 module_exit(l2tp_exit
);
1989 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1990 MODULE_DESCRIPTION("L2TP core");
1991 MODULE_LICENSE("GPL");
1992 MODULE_VERSION(L2TP_DRV_VERSION
);