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 struct l2tp_session
*l2tp_session_get_nth(struct l2tp_tunnel
*tunnel
, int nth
,
285 struct l2tp_session
*session
;
288 read_lock_bh(&tunnel
->hlist_lock
);
289 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
290 hlist_for_each_entry(session
, &tunnel
->session_hlist
[hash
], hlist
) {
292 l2tp_session_inc_refcount(session
);
293 if (do_ref
&& session
->ref
)
294 session
->ref(session
);
295 read_unlock_bh(&tunnel
->hlist_lock
);
301 read_unlock_bh(&tunnel
->hlist_lock
);
305 EXPORT_SYMBOL_GPL(l2tp_session_get_nth
);
307 /* Lookup a session by interface name.
308 * This is very inefficient but is only used by management interfaces.
310 struct l2tp_session
*l2tp_session_find_by_ifname(struct net
*net
, char *ifname
)
312 struct l2tp_net
*pn
= l2tp_pernet(net
);
314 struct l2tp_session
*session
;
317 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++) {
318 hlist_for_each_entry_rcu(session
, &pn
->l2tp_session_hlist
[hash
], global_hlist
) {
319 if (!strcmp(session
->ifname
, ifname
)) {
320 rcu_read_unlock_bh();
326 rcu_read_unlock_bh();
330 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname
);
332 /* Lookup a tunnel by id
334 struct l2tp_tunnel
*l2tp_tunnel_find(struct net
*net
, u32 tunnel_id
)
336 struct l2tp_tunnel
*tunnel
;
337 struct l2tp_net
*pn
= l2tp_pernet(net
);
340 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
341 if (tunnel
->tunnel_id
== tunnel_id
) {
342 rcu_read_unlock_bh();
346 rcu_read_unlock_bh();
350 EXPORT_SYMBOL_GPL(l2tp_tunnel_find
);
352 struct l2tp_tunnel
*l2tp_tunnel_find_nth(struct net
*net
, int nth
)
354 struct l2tp_net
*pn
= l2tp_pernet(net
);
355 struct l2tp_tunnel
*tunnel
;
359 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
361 rcu_read_unlock_bh();
366 rcu_read_unlock_bh();
370 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth
);
372 /*****************************************************************************
373 * Receive data handling
374 *****************************************************************************/
376 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
379 static void l2tp_recv_queue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
381 struct sk_buff
*skbp
;
383 u32 ns
= L2TP_SKB_CB(skb
)->ns
;
385 spin_lock_bh(&session
->reorder_q
.lock
);
386 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
387 if (L2TP_SKB_CB(skbp
)->ns
> ns
) {
388 __skb_queue_before(&session
->reorder_q
, skbp
, skb
);
389 l2tp_dbg(session
, L2TP_MSG_SEQ
,
390 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
391 session
->name
, ns
, L2TP_SKB_CB(skbp
)->ns
,
392 skb_queue_len(&session
->reorder_q
));
393 atomic_long_inc(&session
->stats
.rx_oos_packets
);
398 __skb_queue_tail(&session
->reorder_q
, skb
);
401 spin_unlock_bh(&session
->reorder_q
.lock
);
404 /* Dequeue a single skb.
406 static void l2tp_recv_dequeue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
408 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
409 int length
= L2TP_SKB_CB(skb
)->length
;
411 /* We're about to requeue the skb, so return resources
412 * to its current owner (a socket receive buffer).
416 atomic_long_inc(&tunnel
->stats
.rx_packets
);
417 atomic_long_add(length
, &tunnel
->stats
.rx_bytes
);
418 atomic_long_inc(&session
->stats
.rx_packets
);
419 atomic_long_add(length
, &session
->stats
.rx_bytes
);
421 if (L2TP_SKB_CB(skb
)->has_seq
) {
424 session
->nr
&= session
->nr_max
;
426 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated nr to %hu\n",
427 session
->name
, session
->nr
);
430 /* call private receive handler */
431 if (session
->recv_skb
!= NULL
)
432 (*session
->recv_skb
)(session
, skb
, L2TP_SKB_CB(skb
)->length
);
437 (*session
->deref
)(session
);
440 /* Dequeue skbs from the session's reorder_q, subject to packet order.
441 * Skbs that have been in the queue for too long are simply discarded.
443 static void l2tp_recv_dequeue(struct l2tp_session
*session
)
448 /* If the pkt at the head of the queue has the nr that we
449 * expect to send up next, dequeue it and any other
450 * in-sequence packets behind it.
453 spin_lock_bh(&session
->reorder_q
.lock
);
454 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
455 if (time_after(jiffies
, L2TP_SKB_CB(skb
)->expires
)) {
456 atomic_long_inc(&session
->stats
.rx_seq_discards
);
457 atomic_long_inc(&session
->stats
.rx_errors
);
458 l2tp_dbg(session
, L2TP_MSG_SEQ
,
459 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
460 session
->name
, L2TP_SKB_CB(skb
)->ns
,
461 L2TP_SKB_CB(skb
)->length
, session
->nr
,
462 skb_queue_len(&session
->reorder_q
));
463 session
->reorder_skip
= 1;
464 __skb_unlink(skb
, &session
->reorder_q
);
467 (*session
->deref
)(session
);
471 if (L2TP_SKB_CB(skb
)->has_seq
) {
472 if (session
->reorder_skip
) {
473 l2tp_dbg(session
, L2TP_MSG_SEQ
,
474 "%s: advancing nr to next pkt: %u -> %u",
475 session
->name
, session
->nr
,
476 L2TP_SKB_CB(skb
)->ns
);
477 session
->reorder_skip
= 0;
478 session
->nr
= L2TP_SKB_CB(skb
)->ns
;
480 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
481 l2tp_dbg(session
, L2TP_MSG_SEQ
,
482 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
483 session
->name
, L2TP_SKB_CB(skb
)->ns
,
484 L2TP_SKB_CB(skb
)->length
, session
->nr
,
485 skb_queue_len(&session
->reorder_q
));
489 __skb_unlink(skb
, &session
->reorder_q
);
491 /* Process the skb. We release the queue lock while we
492 * do so to let other contexts process the queue.
494 spin_unlock_bh(&session
->reorder_q
.lock
);
495 l2tp_recv_dequeue_skb(session
, skb
);
500 spin_unlock_bh(&session
->reorder_q
.lock
);
503 static int l2tp_seq_check_rx_window(struct l2tp_session
*session
, u32 nr
)
507 if (nr
>= session
->nr
)
508 nws
= nr
- session
->nr
;
510 nws
= (session
->nr_max
+ 1) - (session
->nr
- nr
);
512 return nws
< session
->nr_window_size
;
515 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
516 * acceptable, else non-zero.
518 static int l2tp_recv_data_seq(struct l2tp_session
*session
, struct sk_buff
*skb
)
520 if (!l2tp_seq_check_rx_window(session
, L2TP_SKB_CB(skb
)->ns
)) {
521 /* Packet sequence number is outside allowed window.
524 l2tp_dbg(session
, L2TP_MSG_SEQ
,
525 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
526 session
->name
, L2TP_SKB_CB(skb
)->ns
,
527 L2TP_SKB_CB(skb
)->length
, session
->nr
);
531 if (session
->reorder_timeout
!= 0) {
532 /* Packet reordering enabled. Add skb to session's
533 * reorder queue, in order of ns.
535 l2tp_recv_queue_skb(session
, skb
);
539 /* Packet reordering disabled. Discard out-of-sequence packets, while
540 * tracking the number if in-sequence packets after the first OOS packet
541 * is seen. After nr_oos_count_max in-sequence packets, reset the
542 * sequence number to re-enable packet reception.
544 if (L2TP_SKB_CB(skb
)->ns
== session
->nr
) {
545 skb_queue_tail(&session
->reorder_q
, skb
);
547 u32 nr_oos
= L2TP_SKB_CB(skb
)->ns
;
548 u32 nr_next
= (session
->nr_oos
+ 1) & session
->nr_max
;
550 if (nr_oos
== nr_next
)
551 session
->nr_oos_count
++;
553 session
->nr_oos_count
= 0;
555 session
->nr_oos
= nr_oos
;
556 if (session
->nr_oos_count
> session
->nr_oos_count_max
) {
557 session
->reorder_skip
= 1;
558 l2tp_dbg(session
, L2TP_MSG_SEQ
,
559 "%s: %d oos packets received. Resetting sequence numbers\n",
560 session
->name
, session
->nr_oos_count
);
562 if (!session
->reorder_skip
) {
563 atomic_long_inc(&session
->stats
.rx_seq_discards
);
564 l2tp_dbg(session
, L2TP_MSG_SEQ
,
565 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
566 session
->name
, L2TP_SKB_CB(skb
)->ns
,
567 L2TP_SKB_CB(skb
)->length
, session
->nr
,
568 skb_queue_len(&session
->reorder_q
));
571 skb_queue_tail(&session
->reorder_q
, skb
);
581 /* Do receive processing of L2TP data frames. We handle both L2TPv2
582 * and L2TPv3 data frames here.
584 * L2TPv2 Data Message Header
587 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
589 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
591 * | Tunnel ID | Session ID |
592 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
593 * | Ns (opt) | Nr (opt) |
594 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
595 * | Offset Size (opt) | Offset pad... (opt)
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * Data frames are marked by T=0. All other fields are the same as
599 * those in L2TP control frames.
601 * L2TPv3 Data Message Header
603 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
604 * | L2TP Session Header |
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * | L2-Specific Sublayer |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * | Tunnel Payload ...
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 * L2TPv3 Session Header Over IP
614 * 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
615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
617 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
618 * | Cookie (optional, maximum 64 bits)...
619 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623 * L2TPv3 L2-Specific Sublayer Format
626 * 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
627 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628 * |x|S|x|x|x|x|x|x| Sequence Number |
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
631 * Cookie value, sublayer format and offset (pad) are negotiated with
632 * the peer when the session is set up. Unlike L2TPv2, we do not need
633 * to parse the packet header to determine if optional fields are
636 * Caller must already have parsed the frame and determined that it is
637 * a data (not control) frame before coming here. Fields up to the
638 * session-id have already been parsed and ptr points to the data
639 * after the session-id.
641 void l2tp_recv_common(struct l2tp_session
*session
, struct sk_buff
*skb
,
642 unsigned char *ptr
, unsigned char *optr
, u16 hdrflags
,
643 int length
, int (*payload_hook
)(struct sk_buff
*skb
))
645 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
649 /* The ref count is increased since we now hold a pointer to
650 * the session. Take care to decrement the refcnt when exiting
651 * this function from now on...
653 l2tp_session_inc_refcount(session
);
655 (*session
->ref
)(session
);
657 /* Parse and check optional cookie */
658 if (session
->peer_cookie_len
> 0) {
659 if (memcmp(ptr
, &session
->peer_cookie
[0], session
->peer_cookie_len
)) {
660 l2tp_info(tunnel
, L2TP_MSG_DATA
,
661 "%s: cookie mismatch (%u/%u). Discarding.\n",
662 tunnel
->name
, tunnel
->tunnel_id
,
663 session
->session_id
);
664 atomic_long_inc(&session
->stats
.rx_cookie_discards
);
667 ptr
+= session
->peer_cookie_len
;
670 /* Handle the optional sequence numbers. Sequence numbers are
671 * in different places for L2TPv2 and L2TPv3.
673 * If we are the LAC, enable/disable sequence numbers under
674 * the control of the LNS. If no sequence numbers present but
675 * we were expecting them, discard frame.
678 L2TP_SKB_CB(skb
)->has_seq
= 0;
679 if (tunnel
->version
== L2TP_HDR_VER_2
) {
680 if (hdrflags
& L2TP_HDRFLAG_S
) {
681 ns
= ntohs(*(__be16
*) ptr
);
683 nr
= ntohs(*(__be16
*) ptr
);
686 /* Store L2TP info in the skb */
687 L2TP_SKB_CB(skb
)->ns
= ns
;
688 L2TP_SKB_CB(skb
)->has_seq
= 1;
690 l2tp_dbg(session
, L2TP_MSG_SEQ
,
691 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
692 session
->name
, ns
, nr
, session
->nr
);
694 } else if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
695 u32 l2h
= ntohl(*(__be32
*) ptr
);
697 if (l2h
& 0x40000000) {
698 ns
= l2h
& 0x00ffffff;
700 /* Store L2TP info in the skb */
701 L2TP_SKB_CB(skb
)->ns
= ns
;
702 L2TP_SKB_CB(skb
)->has_seq
= 1;
704 l2tp_dbg(session
, L2TP_MSG_SEQ
,
705 "%s: recv data ns=%u, session nr=%u\n",
706 session
->name
, ns
, session
->nr
);
710 /* Advance past L2-specific header, if present */
711 ptr
+= session
->l2specific_len
;
713 if (L2TP_SKB_CB(skb
)->has_seq
) {
714 /* Received a packet with sequence numbers. If we're the LNS,
715 * check if we sre sending sequence numbers and if not,
718 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
719 l2tp_info(session
, L2TP_MSG_SEQ
,
720 "%s: requested to enable seq numbers by LNS\n",
722 session
->send_seq
= -1;
723 l2tp_session_set_header_len(session
, tunnel
->version
);
726 /* No sequence numbers.
727 * If user has configured mandatory sequence numbers, discard.
729 if (session
->recv_seq
) {
730 l2tp_warn(session
, L2TP_MSG_SEQ
,
731 "%s: recv data has no seq numbers when required. Discarding.\n",
733 atomic_long_inc(&session
->stats
.rx_seq_discards
);
737 /* If we're the LAC and we're sending sequence numbers, the
738 * LNS has requested that we no longer send sequence numbers.
739 * If we're the LNS and we're sending sequence numbers, the
740 * LAC is broken. Discard the frame.
742 if ((!session
->lns_mode
) && (session
->send_seq
)) {
743 l2tp_info(session
, L2TP_MSG_SEQ
,
744 "%s: requested to disable seq numbers by LNS\n",
746 session
->send_seq
= 0;
747 l2tp_session_set_header_len(session
, tunnel
->version
);
748 } else if (session
->send_seq
) {
749 l2tp_warn(session
, L2TP_MSG_SEQ
,
750 "%s: recv data has no seq numbers when required. Discarding.\n",
752 atomic_long_inc(&session
->stats
.rx_seq_discards
);
757 /* Session data offset is handled differently for L2TPv2 and
758 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
759 * the header. For L2TPv3, the offset is negotiated using AVPs
760 * in the session setup control protocol.
762 if (tunnel
->version
== L2TP_HDR_VER_2
) {
763 /* If offset bit set, skip it. */
764 if (hdrflags
& L2TP_HDRFLAG_O
) {
765 offset
= ntohs(*(__be16
*)ptr
);
769 ptr
+= session
->offset
;
772 if (!pskb_may_pull(skb
, offset
))
775 __skb_pull(skb
, offset
);
777 /* If caller wants to process the payload before we queue the
781 if ((*payload_hook
)(skb
))
784 /* Prepare skb for adding to the session's reorder_q. Hold
785 * packets for max reorder_timeout or 1 second if not
788 L2TP_SKB_CB(skb
)->length
= length
;
789 L2TP_SKB_CB(skb
)->expires
= jiffies
+
790 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
792 /* Add packet to the session's receive queue. Reordering is done here, if
793 * enabled. Saved L2TP protocol info is stored in skb->sb[].
795 if (L2TP_SKB_CB(skb
)->has_seq
) {
796 if (l2tp_recv_data_seq(session
, skb
))
799 /* No sequence numbers. Add the skb to the tail of the
800 * reorder queue. This ensures that it will be
801 * delivered after all previous sequenced skbs.
803 skb_queue_tail(&session
->reorder_q
, skb
);
806 /* Try to dequeue as many skbs from reorder_q as we can. */
807 l2tp_recv_dequeue(session
);
809 l2tp_session_dec_refcount(session
);
814 atomic_long_inc(&session
->stats
.rx_errors
);
818 (*session
->deref
)(session
);
820 l2tp_session_dec_refcount(session
);
822 EXPORT_SYMBOL(l2tp_recv_common
);
824 /* Drop skbs from the session's reorder_q
826 int l2tp_session_queue_purge(struct l2tp_session
*session
)
828 struct sk_buff
*skb
= NULL
;
830 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
831 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
832 atomic_long_inc(&session
->stats
.rx_errors
);
835 (*session
->deref
)(session
);
839 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge
);
841 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
842 * here. The skb is not on a list when we get here.
843 * Returns 0 if the packet was a data packet and was successfully passed on.
844 * Returns 1 if the packet was not a good data packet and could not be
845 * forwarded. All such packets are passed up to userspace to deal with.
847 static int l2tp_udp_recv_core(struct l2tp_tunnel
*tunnel
, struct sk_buff
*skb
,
848 int (*payload_hook
)(struct sk_buff
*skb
))
850 struct l2tp_session
*session
= NULL
;
851 unsigned char *ptr
, *optr
;
853 u32 tunnel_id
, session_id
;
857 /* UDP has verifed checksum */
859 /* UDP always verifies the packet length. */
860 __skb_pull(skb
, sizeof(struct udphdr
));
863 if (!pskb_may_pull(skb
, L2TP_HDR_SIZE_SEQ
)) {
864 l2tp_info(tunnel
, L2TP_MSG_DATA
,
865 "%s: recv short packet (len=%d)\n",
866 tunnel
->name
, skb
->len
);
870 /* Trace packet contents, if enabled */
871 if (tunnel
->debug
& L2TP_MSG_DATA
) {
872 length
= min(32u, skb
->len
);
873 if (!pskb_may_pull(skb
, length
))
876 pr_debug("%s: recv\n", tunnel
->name
);
877 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
880 /* Point to L2TP header */
881 optr
= ptr
= skb
->data
;
883 /* Get L2TP header flags */
884 hdrflags
= ntohs(*(__be16
*) ptr
);
886 /* Check protocol version */
887 version
= hdrflags
& L2TP_HDR_VER_MASK
;
888 if (version
!= tunnel
->version
) {
889 l2tp_info(tunnel
, L2TP_MSG_DATA
,
890 "%s: recv protocol version mismatch: got %d expected %d\n",
891 tunnel
->name
, version
, tunnel
->version
);
895 /* Get length of L2TP packet */
898 /* If type is control packet, it is handled by userspace. */
899 if (hdrflags
& L2TP_HDRFLAG_T
) {
900 l2tp_dbg(tunnel
, L2TP_MSG_DATA
,
901 "%s: recv control packet, len=%d\n",
902 tunnel
->name
, length
);
909 if (tunnel
->version
== L2TP_HDR_VER_2
) {
910 /* If length is present, skip it */
911 if (hdrflags
& L2TP_HDRFLAG_L
)
914 /* Extract tunnel and session ID */
915 tunnel_id
= ntohs(*(__be16
*) ptr
);
917 session_id
= ntohs(*(__be16
*) ptr
);
920 ptr
+= 2; /* skip reserved bits */
921 tunnel_id
= tunnel
->tunnel_id
;
922 session_id
= ntohl(*(__be32
*) ptr
);
926 /* Find the session context */
927 session
= l2tp_session_find(tunnel
->l2tp_net
, tunnel
, session_id
);
928 if (!session
|| !session
->recv_skb
) {
929 /* Not found? Pass to userspace to deal with */
930 l2tp_info(tunnel
, L2TP_MSG_DATA
,
931 "%s: no session found (%u/%u). Passing up.\n",
932 tunnel
->name
, tunnel_id
, session_id
);
936 l2tp_recv_common(session
, skb
, ptr
, optr
, hdrflags
, length
, payload_hook
);
941 /* Put UDP header back */
942 __skb_push(skb
, sizeof(struct udphdr
));
947 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
951 * >0: skb should be passed up to userspace as UDP.
953 int l2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
955 struct l2tp_tunnel
*tunnel
;
957 tunnel
= l2tp_sock_to_tunnel(sk
);
961 l2tp_dbg(tunnel
, L2TP_MSG_DATA
, "%s: received %d bytes\n",
962 tunnel
->name
, skb
->len
);
964 if (l2tp_udp_recv_core(tunnel
, skb
, tunnel
->recv_payload_hook
))
975 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv
);
977 /************************************************************************
979 ***********************************************************************/
981 /* Build an L2TP header for the session into the buffer provided.
983 static int l2tp_build_l2tpv2_header(struct l2tp_session
*session
, void *buf
)
985 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
988 u16 flags
= L2TP_HDR_VER_2
;
989 u32 tunnel_id
= tunnel
->peer_tunnel_id
;
990 u32 session_id
= session
->peer_session_id
;
992 if (session
->send_seq
)
993 flags
|= L2TP_HDRFLAG_S
;
995 /* Setup L2TP header. */
996 *bufp
++ = htons(flags
);
997 *bufp
++ = htons(tunnel_id
);
998 *bufp
++ = htons(session_id
);
999 if (session
->send_seq
) {
1000 *bufp
++ = htons(session
->ns
);
1003 session
->ns
&= 0xffff;
1004 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated ns to %u\n",
1005 session
->name
, session
->ns
);
1011 static int l2tp_build_l2tpv3_header(struct l2tp_session
*session
, void *buf
)
1013 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1017 /* Setup L2TP header. The header differs slightly for UDP and
1018 * IP encapsulations. For UDP, there is 4 bytes of flags.
1020 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
1021 u16 flags
= L2TP_HDR_VER_3
;
1022 *((__be16
*) bufp
) = htons(flags
);
1024 *((__be16
*) bufp
) = 0;
1028 *((__be32
*) bufp
) = htonl(session
->peer_session_id
);
1030 if (session
->cookie_len
) {
1031 memcpy(bufp
, &session
->cookie
[0], session
->cookie_len
);
1032 bufp
+= session
->cookie_len
;
1034 if (session
->l2specific_len
) {
1035 if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
1037 if (session
->send_seq
) {
1038 l2h
= 0x40000000 | session
->ns
;
1040 session
->ns
&= 0xffffff;
1041 l2tp_dbg(session
, L2TP_MSG_SEQ
,
1042 "%s: updated ns to %u\n",
1043 session
->name
, session
->ns
);
1046 *((__be32
*) bufp
) = htonl(l2h
);
1048 bufp
+= session
->l2specific_len
;
1050 if (session
->offset
)
1051 bufp
+= session
->offset
;
1056 static int l2tp_xmit_core(struct l2tp_session
*session
, struct sk_buff
*skb
,
1057 struct flowi
*fl
, size_t data_len
)
1059 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1060 unsigned int len
= skb
->len
;
1064 if (session
->send_seq
)
1065 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %Zd bytes, ns=%u\n",
1066 session
->name
, data_len
, session
->ns
- 1);
1068 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %Zd bytes\n",
1069 session
->name
, data_len
);
1071 if (session
->debug
& L2TP_MSG_DATA
) {
1072 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1073 unsigned char *datap
= skb
->data
+ uhlen
;
1075 pr_debug("%s: xmit\n", session
->name
);
1076 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1077 datap
, min_t(size_t, 32, len
- uhlen
));
1080 /* Queue the packet to IP for output */
1082 #if IS_ENABLED(CONFIG_IPV6)
1083 if (tunnel
->sock
->sk_family
== PF_INET6
&& !tunnel
->v4mapped
)
1084 error
= inet6_csk_xmit(tunnel
->sock
, skb
, NULL
);
1087 error
= ip_queue_xmit(tunnel
->sock
, skb
, fl
);
1091 atomic_long_inc(&tunnel
->stats
.tx_packets
);
1092 atomic_long_add(len
, &tunnel
->stats
.tx_bytes
);
1093 atomic_long_inc(&session
->stats
.tx_packets
);
1094 atomic_long_add(len
, &session
->stats
.tx_bytes
);
1096 atomic_long_inc(&tunnel
->stats
.tx_errors
);
1097 atomic_long_inc(&session
->stats
.tx_errors
);
1103 /* If caller requires the skb to have a ppp header, the header must be
1104 * inserted in the skb data before calling this function.
1106 int l2tp_xmit_skb(struct l2tp_session
*session
, struct sk_buff
*skb
, int hdr_len
)
1108 int data_len
= skb
->len
;
1109 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1110 struct sock
*sk
= tunnel
->sock
;
1113 struct inet_sock
*inet
;
1115 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1117 int ret
= NET_XMIT_SUCCESS
;
1119 /* Check that there's enough headroom in the skb to insert IP,
1120 * UDP and L2TP headers. If not enough, expand it to
1121 * make room. Adjust truesize.
1123 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1125 if (skb_cow_head(skb
, headroom
)) {
1127 return NET_XMIT_DROP
;
1130 /* Setup L2TP header */
1131 session
->build_header(session
, __skb_push(skb
, hdr_len
));
1133 /* Reset skb netfilter state */
1134 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1135 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1140 if (sock_owned_by_user(sk
)) {
1142 ret
= NET_XMIT_DROP
;
1146 /* Get routing info from the tunnel socket */
1148 skb_dst_set(skb
, sk_dst_check(sk
, 0));
1151 fl
= &inet
->cork
.fl
;
1152 switch (tunnel
->encap
) {
1153 case L2TP_ENCAPTYPE_UDP
:
1154 /* Setup UDP header */
1155 __skb_push(skb
, sizeof(*uh
));
1156 skb_reset_transport_header(skb
);
1158 uh
->source
= inet
->inet_sport
;
1159 uh
->dest
= inet
->inet_dport
;
1160 udp_len
= uhlen
+ hdr_len
+ data_len
;
1161 uh
->len
= htons(udp_len
);
1163 /* Calculate UDP checksum if configured to do so */
1164 #if IS_ENABLED(CONFIG_IPV6)
1165 if (sk
->sk_family
== PF_INET6
&& !tunnel
->v4mapped
)
1166 udp6_set_csum(udp_get_no_check6_tx(sk
),
1167 skb
, &inet6_sk(sk
)->saddr
,
1168 &sk
->sk_v6_daddr
, udp_len
);
1171 udp_set_csum(sk
->sk_no_check_tx
, skb
, inet
->inet_saddr
,
1172 inet
->inet_daddr
, udp_len
);
1175 case L2TP_ENCAPTYPE_IP
:
1179 l2tp_xmit_core(session
, skb
, fl
, data_len
);
1185 EXPORT_SYMBOL_GPL(l2tp_xmit_skb
);
1187 /*****************************************************************************
1188 * Tinnel and session create/destroy.
1189 *****************************************************************************/
1191 /* Tunnel socket destruct hook.
1192 * The tunnel context is deleted only when all session sockets have been
1195 static void l2tp_tunnel_destruct(struct sock
*sk
)
1197 struct l2tp_tunnel
*tunnel
= l2tp_tunnel(sk
);
1198 struct l2tp_net
*pn
;
1203 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing...\n", tunnel
->name
);
1206 /* Disable udp encapsulation */
1207 switch (tunnel
->encap
) {
1208 case L2TP_ENCAPTYPE_UDP
:
1209 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1210 (udp_sk(sk
))->encap_type
= 0;
1211 (udp_sk(sk
))->encap_rcv
= NULL
;
1212 (udp_sk(sk
))->encap_destroy
= NULL
;
1214 case L2TP_ENCAPTYPE_IP
:
1218 /* Remove hooks into tunnel socket */
1219 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1220 sk
->sk_user_data
= NULL
;
1221 tunnel
->sock
= NULL
;
1223 /* Remove the tunnel struct from the tunnel list */
1224 pn
= l2tp_pernet(tunnel
->l2tp_net
);
1225 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1226 list_del_rcu(&tunnel
->list
);
1227 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1228 atomic_dec(&l2tp_tunnel_count
);
1230 l2tp_tunnel_closeall(tunnel
);
1231 l2tp_tunnel_dec_refcount(tunnel
);
1233 /* Call the original destructor */
1234 if (sk
->sk_destruct
)
1235 (*sk
->sk_destruct
)(sk
);
1240 /* When the tunnel is closed, all the attached sessions need to go too.
1242 void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
)
1245 struct hlist_node
*walk
;
1246 struct hlist_node
*tmp
;
1247 struct l2tp_session
*session
;
1249 BUG_ON(tunnel
== NULL
);
1251 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing all sessions...\n",
1254 write_lock_bh(&tunnel
->hlist_lock
);
1255 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
1257 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1258 session
= hlist_entry(walk
, struct l2tp_session
, hlist
);
1260 l2tp_info(session
, L2TP_MSG_CONTROL
,
1261 "%s: closing session\n", session
->name
);
1263 hlist_del_init(&session
->hlist
);
1265 if (session
->ref
!= NULL
)
1266 (*session
->ref
)(session
);
1268 write_unlock_bh(&tunnel
->hlist_lock
);
1270 __l2tp_session_unhash(session
);
1271 l2tp_session_queue_purge(session
);
1273 if (session
->session_close
!= NULL
)
1274 (*session
->session_close
)(session
);
1276 if (session
->deref
!= NULL
)
1277 (*session
->deref
)(session
);
1279 l2tp_session_dec_refcount(session
);
1281 write_lock_bh(&tunnel
->hlist_lock
);
1283 /* Now restart from the beginning of this hash
1284 * chain. We always remove a session from the
1285 * list so we are guaranteed to make forward
1291 write_unlock_bh(&tunnel
->hlist_lock
);
1293 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall
);
1295 /* Tunnel socket destroy hook for UDP encapsulation */
1296 static void l2tp_udp_encap_destroy(struct sock
*sk
)
1298 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
1300 l2tp_tunnel_closeall(tunnel
);
1305 /* Really kill the tunnel.
1306 * Come here only when all sessions have been cleared from the tunnel.
1308 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
)
1310 BUG_ON(atomic_read(&tunnel
->ref_count
) != 0);
1311 BUG_ON(tunnel
->sock
!= NULL
);
1312 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: free...\n", tunnel
->name
);
1313 kfree_rcu(tunnel
, rcu
);
1316 /* Workqueue tunnel deletion function */
1317 static void l2tp_tunnel_del_work(struct work_struct
*work
)
1319 struct l2tp_tunnel
*tunnel
= NULL
;
1320 struct socket
*sock
= NULL
;
1321 struct sock
*sk
= NULL
;
1323 tunnel
= container_of(work
, struct l2tp_tunnel
, del_work
);
1325 l2tp_tunnel_closeall(tunnel
);
1327 sk
= l2tp_tunnel_sock_lookup(tunnel
);
1331 sock
= sk
->sk_socket
;
1333 /* If the tunnel socket was created by userspace, then go through the
1334 * inet layer to shut the socket down, and let userspace close it.
1335 * Otherwise, if we created the socket directly within the kernel, use
1336 * the sk API to release it here.
1337 * In either case the tunnel resources are freed in the socket
1338 * destructor when the tunnel socket goes away.
1340 if (tunnel
->fd
>= 0) {
1342 inet_shutdown(sock
, 2);
1345 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1350 l2tp_tunnel_sock_put(sk
);
1352 l2tp_tunnel_dec_refcount(tunnel
);
1355 /* Create a socket for the tunnel, if one isn't set up by
1356 * userspace. This is used for static tunnels where there is no
1357 * managing L2TP daemon.
1359 * Since we don't want these sockets to keep a namespace alive by
1360 * themselves, we drop the socket's namespace refcount after creation.
1361 * These sockets are freed when the namespace exits using the pernet
1364 static int l2tp_tunnel_sock_create(struct net
*net
,
1367 struct l2tp_tunnel_cfg
*cfg
,
1368 struct socket
**sockp
)
1371 struct socket
*sock
= NULL
;
1372 struct udp_port_cfg udp_conf
;
1374 switch (cfg
->encap
) {
1375 case L2TP_ENCAPTYPE_UDP
:
1376 memset(&udp_conf
, 0, sizeof(udp_conf
));
1378 #if IS_ENABLED(CONFIG_IPV6)
1379 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1380 udp_conf
.family
= AF_INET6
;
1381 memcpy(&udp_conf
.local_ip6
, cfg
->local_ip6
,
1382 sizeof(udp_conf
.local_ip6
));
1383 memcpy(&udp_conf
.peer_ip6
, cfg
->peer_ip6
,
1384 sizeof(udp_conf
.peer_ip6
));
1385 udp_conf
.use_udp6_tx_checksums
=
1386 cfg
->udp6_zero_tx_checksums
;
1387 udp_conf
.use_udp6_rx_checksums
=
1388 cfg
->udp6_zero_rx_checksums
;
1392 udp_conf
.family
= AF_INET
;
1393 udp_conf
.local_ip
= cfg
->local_ip
;
1394 udp_conf
.peer_ip
= cfg
->peer_ip
;
1395 udp_conf
.use_udp_checksums
= cfg
->use_udp_checksums
;
1398 udp_conf
.local_udp_port
= htons(cfg
->local_udp_port
);
1399 udp_conf
.peer_udp_port
= htons(cfg
->peer_udp_port
);
1401 err
= udp_sock_create(net
, &udp_conf
, &sock
);
1407 case L2TP_ENCAPTYPE_IP
:
1408 #if IS_ENABLED(CONFIG_IPV6)
1409 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1410 struct sockaddr_l2tpip6 ip6_addr
= {0};
1412 err
= sock_create_kern(net
, AF_INET6
, SOCK_DGRAM
,
1413 IPPROTO_L2TP
, &sock
);
1417 ip6_addr
.l2tp_family
= AF_INET6
;
1418 memcpy(&ip6_addr
.l2tp_addr
, cfg
->local_ip6
,
1419 sizeof(ip6_addr
.l2tp_addr
));
1420 ip6_addr
.l2tp_conn_id
= tunnel_id
;
1421 err
= kernel_bind(sock
, (struct sockaddr
*) &ip6_addr
,
1426 ip6_addr
.l2tp_family
= AF_INET6
;
1427 memcpy(&ip6_addr
.l2tp_addr
, cfg
->peer_ip6
,
1428 sizeof(ip6_addr
.l2tp_addr
));
1429 ip6_addr
.l2tp_conn_id
= peer_tunnel_id
;
1430 err
= kernel_connect(sock
,
1431 (struct sockaddr
*) &ip6_addr
,
1432 sizeof(ip6_addr
), 0);
1438 struct sockaddr_l2tpip ip_addr
= {0};
1440 err
= sock_create_kern(net
, AF_INET
, SOCK_DGRAM
,
1441 IPPROTO_L2TP
, &sock
);
1445 ip_addr
.l2tp_family
= AF_INET
;
1446 ip_addr
.l2tp_addr
= cfg
->local_ip
;
1447 ip_addr
.l2tp_conn_id
= tunnel_id
;
1448 err
= kernel_bind(sock
, (struct sockaddr
*) &ip_addr
,
1453 ip_addr
.l2tp_family
= AF_INET
;
1454 ip_addr
.l2tp_addr
= cfg
->peer_ip
;
1455 ip_addr
.l2tp_conn_id
= peer_tunnel_id
;
1456 err
= kernel_connect(sock
, (struct sockaddr
*) &ip_addr
,
1457 sizeof(ip_addr
), 0);
1469 if ((err
< 0) && sock
) {
1470 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1478 static struct lock_class_key l2tp_socket_class
;
1480 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
)
1482 struct l2tp_tunnel
*tunnel
= NULL
;
1484 struct socket
*sock
= NULL
;
1485 struct sock
*sk
= NULL
;
1486 struct l2tp_net
*pn
;
1487 enum l2tp_encap_type encap
= L2TP_ENCAPTYPE_UDP
;
1489 /* Get the tunnel socket from the fd, which was opened by
1490 * the userspace L2TP daemon. If not specified, create a
1494 err
= l2tp_tunnel_sock_create(net
, tunnel_id
, peer_tunnel_id
,
1499 sock
= sockfd_lookup(fd
, &err
);
1501 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1502 tunnel_id
, fd
, err
);
1507 /* Reject namespace mismatches */
1508 if (!net_eq(sock_net(sock
->sk
), net
)) {
1509 pr_err("tunl %u: netns mismatch\n", tunnel_id
);
1520 /* Quick sanity checks */
1521 err
= -EPROTONOSUPPORT
;
1522 if (sk
->sk_type
!= SOCK_DGRAM
) {
1523 pr_debug("tunl %hu: fd %d wrong socket type\n",
1528 case L2TP_ENCAPTYPE_UDP
:
1529 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1530 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1531 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1535 case L2TP_ENCAPTYPE_IP
:
1536 if (sk
->sk_protocol
!= IPPROTO_L2TP
) {
1537 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1538 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_L2TP
);
1544 /* Check if this socket has already been prepped */
1545 tunnel
= l2tp_tunnel(sk
);
1546 if (tunnel
!= NULL
) {
1547 /* This socket has already been prepped */
1552 tunnel
= kzalloc(sizeof(struct l2tp_tunnel
), GFP_KERNEL
);
1553 if (tunnel
== NULL
) {
1558 tunnel
->version
= version
;
1559 tunnel
->tunnel_id
= tunnel_id
;
1560 tunnel
->peer_tunnel_id
= peer_tunnel_id
;
1561 tunnel
->debug
= L2TP_DEFAULT_DEBUG_FLAGS
;
1563 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1564 sprintf(&tunnel
->name
[0], "tunl %u", tunnel_id
);
1565 rwlock_init(&tunnel
->hlist_lock
);
1567 /* The net we belong to */
1568 tunnel
->l2tp_net
= net
;
1569 pn
= l2tp_pernet(net
);
1572 tunnel
->debug
= cfg
->debug
;
1574 #if IS_ENABLED(CONFIG_IPV6)
1575 if (sk
->sk_family
== PF_INET6
) {
1576 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1578 if (ipv6_addr_v4mapped(&np
->saddr
) &&
1579 ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
1580 struct inet_sock
*inet
= inet_sk(sk
);
1582 tunnel
->v4mapped
= true;
1583 inet
->inet_saddr
= np
->saddr
.s6_addr32
[3];
1584 inet
->inet_rcv_saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
[3];
1585 inet
->inet_daddr
= sk
->sk_v6_daddr
.s6_addr32
[3];
1587 tunnel
->v4mapped
= false;
1592 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1593 tunnel
->encap
= encap
;
1594 if (encap
== L2TP_ENCAPTYPE_UDP
) {
1595 struct udp_tunnel_sock_cfg udp_cfg
= { };
1597 udp_cfg
.sk_user_data
= tunnel
;
1598 udp_cfg
.encap_type
= UDP_ENCAP_L2TPINUDP
;
1599 udp_cfg
.encap_rcv
= l2tp_udp_encap_recv
;
1600 udp_cfg
.encap_destroy
= l2tp_udp_encap_destroy
;
1602 setup_udp_tunnel_sock(net
, sock
, &udp_cfg
);
1604 sk
->sk_user_data
= tunnel
;
1607 /* Hook on the tunnel socket destructor so that we can cleanup
1608 * if the tunnel socket goes away.
1610 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1611 sk
->sk_destruct
= &l2tp_tunnel_destruct
;
1614 lockdep_set_class_and_name(&sk
->sk_lock
.slock
, &l2tp_socket_class
, "l2tp_sock");
1616 sk
->sk_allocation
= GFP_ATOMIC
;
1618 /* Init delete workqueue struct */
1619 INIT_WORK(&tunnel
->del_work
, l2tp_tunnel_del_work
);
1621 /* Add tunnel to our list */
1622 INIT_LIST_HEAD(&tunnel
->list
);
1623 atomic_inc(&l2tp_tunnel_count
);
1625 /* Bump the reference count. The tunnel context is deleted
1626 * only when this drops to zero. Must be done before list insertion
1628 l2tp_tunnel_inc_refcount(tunnel
);
1629 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1630 list_add_rcu(&tunnel
->list
, &pn
->l2tp_tunnel_list
);
1631 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1638 /* If tunnel's socket was created by the kernel, it doesn't
1641 if (sock
&& sock
->file
)
1646 EXPORT_SYMBOL_GPL(l2tp_tunnel_create
);
1648 /* This function is used by the netlink TUNNEL_DELETE command.
1650 void l2tp_tunnel_delete(struct l2tp_tunnel
*tunnel
)
1652 if (!test_and_set_bit(0, &tunnel
->dead
)) {
1653 l2tp_tunnel_inc_refcount(tunnel
);
1654 queue_work(l2tp_wq
, &tunnel
->del_work
);
1657 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete
);
1659 /* Really kill the session.
1661 void l2tp_session_free(struct l2tp_session
*session
)
1663 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1665 BUG_ON(atomic_read(&session
->ref_count
) != 0);
1668 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1669 if (session
->session_id
!= 0)
1670 atomic_dec(&l2tp_session_count
);
1671 sock_put(tunnel
->sock
);
1672 session
->tunnel
= NULL
;
1673 l2tp_tunnel_dec_refcount(tunnel
);
1678 EXPORT_SYMBOL_GPL(l2tp_session_free
);
1680 /* Remove an l2tp session from l2tp_core's hash lists.
1681 * Provides a tidyup interface for pseudowire code which can't just route all
1682 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1685 void __l2tp_session_unhash(struct l2tp_session
*session
)
1687 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1689 /* Remove the session from core hashes */
1691 /* Remove from the per-tunnel hash */
1692 write_lock_bh(&tunnel
->hlist_lock
);
1693 hlist_del_init(&session
->hlist
);
1694 write_unlock_bh(&tunnel
->hlist_lock
);
1696 /* For L2TPv3 we have a per-net hash: remove from there, too */
1697 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1698 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1699 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1700 hlist_del_init_rcu(&session
->global_hlist
);
1701 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1706 EXPORT_SYMBOL_GPL(__l2tp_session_unhash
);
1708 /* This function is used by the netlink SESSION_DELETE command and by
1711 int l2tp_session_delete(struct l2tp_session
*session
)
1714 (*session
->ref
)(session
);
1715 __l2tp_session_unhash(session
);
1716 l2tp_session_queue_purge(session
);
1717 if (session
->session_close
!= NULL
)
1718 (*session
->session_close
)(session
);
1720 (*session
->deref
)(session
);
1721 l2tp_session_dec_refcount(session
);
1724 EXPORT_SYMBOL_GPL(l2tp_session_delete
);
1726 /* We come here whenever a session's send_seq, cookie_len or
1727 * l2specific_len parameters are set.
1729 void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
)
1731 if (version
== L2TP_HDR_VER_2
) {
1732 session
->hdr_len
= 6;
1733 if (session
->send_seq
)
1734 session
->hdr_len
+= 4;
1736 session
->hdr_len
= 4 + session
->cookie_len
+ session
->l2specific_len
+ session
->offset
;
1737 if (session
->tunnel
->encap
== L2TP_ENCAPTYPE_UDP
)
1738 session
->hdr_len
+= 4;
1742 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len
);
1744 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
)
1746 struct l2tp_session
*session
;
1748 session
= kzalloc(sizeof(struct l2tp_session
) + priv_size
, GFP_KERNEL
);
1749 if (session
!= NULL
) {
1750 session
->magic
= L2TP_SESSION_MAGIC
;
1751 session
->tunnel
= tunnel
;
1753 session
->session_id
= session_id
;
1754 session
->peer_session_id
= peer_session_id
;
1756 if (tunnel
->version
== L2TP_HDR_VER_2
)
1757 session
->nr_max
= 0xffff;
1759 session
->nr_max
= 0xffffff;
1760 session
->nr_window_size
= session
->nr_max
/ 2;
1761 session
->nr_oos_count_max
= 4;
1763 /* Use NR of first received packet */
1764 session
->reorder_skip
= 1;
1766 sprintf(&session
->name
[0], "sess %u/%u",
1767 tunnel
->tunnel_id
, session
->session_id
);
1769 skb_queue_head_init(&session
->reorder_q
);
1771 INIT_HLIST_NODE(&session
->hlist
);
1772 INIT_HLIST_NODE(&session
->global_hlist
);
1774 /* Inherit debug options from tunnel */
1775 session
->debug
= tunnel
->debug
;
1778 session
->pwtype
= cfg
->pw_type
;
1779 session
->debug
= cfg
->debug
;
1780 session
->mtu
= cfg
->mtu
;
1781 session
->mru
= cfg
->mru
;
1782 session
->send_seq
= cfg
->send_seq
;
1783 session
->recv_seq
= cfg
->recv_seq
;
1784 session
->lns_mode
= cfg
->lns_mode
;
1785 session
->reorder_timeout
= cfg
->reorder_timeout
;
1786 session
->offset
= cfg
->offset
;
1787 session
->l2specific_type
= cfg
->l2specific_type
;
1788 session
->l2specific_len
= cfg
->l2specific_len
;
1789 session
->cookie_len
= cfg
->cookie_len
;
1790 memcpy(&session
->cookie
[0], &cfg
->cookie
[0], cfg
->cookie_len
);
1791 session
->peer_cookie_len
= cfg
->peer_cookie_len
;
1792 memcpy(&session
->peer_cookie
[0], &cfg
->peer_cookie
[0], cfg
->peer_cookie_len
);
1795 if (tunnel
->version
== L2TP_HDR_VER_2
)
1796 session
->build_header
= l2tp_build_l2tpv2_header
;
1798 session
->build_header
= l2tp_build_l2tpv3_header
;
1800 l2tp_session_set_header_len(session
, tunnel
->version
);
1802 /* Bump the reference count. The session context is deleted
1803 * only when this drops to zero.
1805 l2tp_session_inc_refcount(session
);
1806 l2tp_tunnel_inc_refcount(tunnel
);
1808 /* Ensure tunnel socket isn't deleted */
1809 sock_hold(tunnel
->sock
);
1811 /* Add session to the tunnel's hash list */
1812 write_lock_bh(&tunnel
->hlist_lock
);
1813 hlist_add_head(&session
->hlist
,
1814 l2tp_session_id_hash(tunnel
, session_id
));
1815 write_unlock_bh(&tunnel
->hlist_lock
);
1817 /* And to the global session list if L2TPv3 */
1818 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1819 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1821 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1822 hlist_add_head_rcu(&session
->global_hlist
,
1823 l2tp_session_id_hash_2(pn
, session_id
));
1824 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1827 /* Ignore management session in session count value */
1828 if (session
->session_id
!= 0)
1829 atomic_inc(&l2tp_session_count
);
1834 EXPORT_SYMBOL_GPL(l2tp_session_create
);
1836 /*****************************************************************************
1838 *****************************************************************************/
1840 static __net_init
int l2tp_init_net(struct net
*net
)
1842 struct l2tp_net
*pn
= net_generic(net
, l2tp_net_id
);
1845 INIT_LIST_HEAD(&pn
->l2tp_tunnel_list
);
1846 spin_lock_init(&pn
->l2tp_tunnel_list_lock
);
1848 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++)
1849 INIT_HLIST_HEAD(&pn
->l2tp_session_hlist
[hash
]);
1851 spin_lock_init(&pn
->l2tp_session_hlist_lock
);
1856 static __net_exit
void l2tp_exit_net(struct net
*net
)
1858 struct l2tp_net
*pn
= l2tp_pernet(net
);
1859 struct l2tp_tunnel
*tunnel
= NULL
;
1862 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
1863 l2tp_tunnel_delete(tunnel
);
1865 rcu_read_unlock_bh();
1868 static struct pernet_operations l2tp_net_ops
= {
1869 .init
= l2tp_init_net
,
1870 .exit
= l2tp_exit_net
,
1872 .size
= sizeof(struct l2tp_net
),
1875 static int __init
l2tp_init(void)
1879 rc
= register_pernet_device(&l2tp_net_ops
);
1883 l2tp_wq
= alloc_workqueue("l2tp", WQ_UNBOUND
, 0);
1885 pr_err("alloc_workqueue failed\n");
1886 unregister_pernet_device(&l2tp_net_ops
);
1891 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION
);
1897 static void __exit
l2tp_exit(void)
1899 unregister_pernet_device(&l2tp_net_ops
);
1901 destroy_workqueue(l2tp_wq
);
1906 module_init(l2tp_init
);
1907 module_exit(l2tp_exit
);
1909 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1910 MODULE_DESCRIPTION("L2TP core");
1911 MODULE_LICENSE("GPL");
1912 MODULE_VERSION(L2TP_DRV_VERSION
);