4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
6 * This file contains some code of the original L2TPv2 pppol2tp
7 * driver, which has the following copyright:
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/list.h>
26 #include <linux/rculist.h>
27 #include <linux/uaccess.h>
29 #include <linux/kernel.h>
30 #include <linux/spinlock.h>
31 #include <linux/kthread.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/errno.h>
35 #include <linux/jiffies.h>
37 #include <linux/netdevice.h>
38 #include <linux/net.h>
39 #include <linux/inetdevice.h>
40 #include <linux/skbuff.h>
41 #include <linux/init.h>
44 #include <linux/udp.h>
45 #include <linux/l2tp.h>
46 #include <linux/hash.h>
47 #include <linux/sort.h>
48 #include <linux/file.h>
49 #include <linux/nsproxy.h>
50 #include <net/net_namespace.h>
51 #include <net/netns/generic.h>
55 #include <net/inet_common.h>
57 #include <net/protocol.h>
58 #include <net/inet6_connection_sock.h>
59 #include <net/inet_ecn.h>
60 #include <net/ip6_route.h>
61 #include <net/ip6_checksum.h>
63 #include <asm/byteorder.h>
64 #include <linux/atomic.h>
66 #include "l2tp_core.h"
68 #define L2TP_DRV_VERSION "V2.0"
70 /* L2TP header constants */
71 #define L2TP_HDRFLAG_T 0x8000
72 #define L2TP_HDRFLAG_L 0x4000
73 #define L2TP_HDRFLAG_S 0x0800
74 #define L2TP_HDRFLAG_O 0x0200
75 #define L2TP_HDRFLAG_P 0x0100
77 #define L2TP_HDR_VER_MASK 0x000F
78 #define L2TP_HDR_VER_2 0x0002
79 #define L2TP_HDR_VER_3 0x0003
81 /* L2TPv3 default L2-specific sublayer */
82 #define L2TP_SLFLAG_S 0x40000000
83 #define L2TP_SL_SEQ_MASK 0x00ffffff
85 #define L2TP_HDR_SIZE_SEQ 10
86 #define L2TP_HDR_SIZE_NOSEQ 6
88 /* Default trace flags */
89 #define L2TP_DEFAULT_DEBUG_FLAGS 0
91 /* Private data stored for received packets in the skb.
97 unsigned long expires
;
100 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
102 static atomic_t l2tp_tunnel_count
;
103 static atomic_t l2tp_session_count
;
104 static struct workqueue_struct
*l2tp_wq
;
106 /* per-net private data for this module */
107 static unsigned int l2tp_net_id
;
109 struct list_head l2tp_tunnel_list
;
110 spinlock_t l2tp_tunnel_list_lock
;
111 struct hlist_head l2tp_session_hlist
[L2TP_HASH_SIZE_2
];
112 spinlock_t l2tp_session_hlist_lock
;
115 static void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
);
116 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
);
118 static inline struct l2tp_net
*l2tp_pernet(struct net
*net
)
122 return net_generic(net
, l2tp_net_id
);
125 /* Tunnel reference counts. Incremented per session that is added to
128 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel
*tunnel
)
130 atomic_inc(&tunnel
->ref_count
);
133 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel
*tunnel
)
135 if (atomic_dec_and_test(&tunnel
->ref_count
))
136 l2tp_tunnel_free(tunnel
);
138 #ifdef L2TP_REFCNT_DEBUG
139 #define l2tp_tunnel_inc_refcount(_t) \
141 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
142 __func__, __LINE__, (_t)->name, \
143 atomic_read(&_t->ref_count)); \
144 l2tp_tunnel_inc_refcount_1(_t); \
146 #define l2tp_tunnel_dec_refcount(_t)
148 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
149 __func__
, __LINE__
, (_t
)->name
, \
150 atomic_read(&_t
->ref_count
)); \
151 l2tp_tunnel_dec_refcount_1(_t
); \
154 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
155 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
158 /* Session hash global list for L2TPv3.
159 * The session_id SHOULD be random according to RFC3931, but several
160 * L2TP implementations use incrementing session_ids. So we do a real
161 * hash on the session_id, rather than a simple bitmask.
163 static inline struct hlist_head
*
164 l2tp_session_id_hash_2(struct l2tp_net
*pn
, u32 session_id
)
166 return &pn
->l2tp_session_hlist
[hash_32(session_id
, L2TP_HASH_BITS_2
)];
170 /* Lookup the tunnel socket, possibly involving the fs code if the socket is
171 * owned by userspace. A struct sock returned from this function must be
172 * released using l2tp_tunnel_sock_put once you're done with it.
174 struct sock
*l2tp_tunnel_sock_lookup(struct l2tp_tunnel
*tunnel
)
177 struct socket
*sock
= NULL
;
178 struct sock
*sk
= NULL
;
183 if (tunnel
->fd
>= 0) {
184 /* Socket is owned by userspace, who might be in the process
185 * of closing it. Look the socket up using the fd to ensure
188 sock
= sockfd_lookup(tunnel
->fd
, &err
);
192 /* Socket is owned by kernelspace */
200 EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup
);
202 /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
203 void l2tp_tunnel_sock_put(struct sock
*sk
)
205 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
207 if (tunnel
->fd
>= 0) {
208 /* Socket is owned by userspace */
209 sockfd_put(sk
->sk_socket
);
215 EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put
);
217 /* Lookup a session by id in the global session list
219 static struct l2tp_session
*l2tp_session_find_2(struct net
*net
, u32 session_id
)
221 struct l2tp_net
*pn
= l2tp_pernet(net
);
222 struct hlist_head
*session_list
=
223 l2tp_session_id_hash_2(pn
, session_id
);
224 struct l2tp_session
*session
;
227 hlist_for_each_entry_rcu(session
, session_list
, global_hlist
) {
228 if (session
->session_id
== session_id
) {
229 rcu_read_unlock_bh();
233 rcu_read_unlock_bh();
238 /* Session hash list.
239 * The session_id SHOULD be random according to RFC2661, but several
240 * L2TP implementations (Cisco and Microsoft) use incrementing
241 * session_ids. So we do a real hash on the session_id, rather than a
244 static inline struct hlist_head
*
245 l2tp_session_id_hash(struct l2tp_tunnel
*tunnel
, u32 session_id
)
247 return &tunnel
->session_hlist
[hash_32(session_id
, L2TP_HASH_BITS
)];
250 /* Lookup a session by id
252 struct l2tp_session
*l2tp_session_find(struct net
*net
, struct l2tp_tunnel
*tunnel
, u32 session_id
)
254 struct hlist_head
*session_list
;
255 struct l2tp_session
*session
;
257 /* In L2TPv3, session_ids are unique over all tunnels and we
258 * sometimes need to look them up before we know the
262 return l2tp_session_find_2(net
, session_id
);
264 session_list
= l2tp_session_id_hash(tunnel
, session_id
);
265 read_lock_bh(&tunnel
->hlist_lock
);
266 hlist_for_each_entry(session
, session_list
, hlist
) {
267 if (session
->session_id
== session_id
) {
268 read_unlock_bh(&tunnel
->hlist_lock
);
272 read_unlock_bh(&tunnel
->hlist_lock
);
276 EXPORT_SYMBOL_GPL(l2tp_session_find
);
278 struct l2tp_session
*l2tp_session_find_nth(struct l2tp_tunnel
*tunnel
, int nth
)
281 struct l2tp_session
*session
;
284 read_lock_bh(&tunnel
->hlist_lock
);
285 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
286 hlist_for_each_entry(session
, &tunnel
->session_hlist
[hash
], hlist
) {
288 read_unlock_bh(&tunnel
->hlist_lock
);
294 read_unlock_bh(&tunnel
->hlist_lock
);
298 EXPORT_SYMBOL_GPL(l2tp_session_find_nth
);
300 /* Lookup a session by interface name.
301 * This is very inefficient but is only used by management interfaces.
303 struct l2tp_session
*l2tp_session_find_by_ifname(struct net
*net
, char *ifname
)
305 struct l2tp_net
*pn
= l2tp_pernet(net
);
307 struct l2tp_session
*session
;
310 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++) {
311 hlist_for_each_entry_rcu(session
, &pn
->l2tp_session_hlist
[hash
], global_hlist
) {
312 if (!strcmp(session
->ifname
, ifname
)) {
313 rcu_read_unlock_bh();
319 rcu_read_unlock_bh();
323 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname
);
325 /* Lookup a tunnel by id
327 struct l2tp_tunnel
*l2tp_tunnel_find(struct net
*net
, u32 tunnel_id
)
329 struct l2tp_tunnel
*tunnel
;
330 struct l2tp_net
*pn
= l2tp_pernet(net
);
333 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
334 if (tunnel
->tunnel_id
== tunnel_id
) {
335 rcu_read_unlock_bh();
339 rcu_read_unlock_bh();
343 EXPORT_SYMBOL_GPL(l2tp_tunnel_find
);
345 struct l2tp_tunnel
*l2tp_tunnel_find_nth(struct net
*net
, int nth
)
347 struct l2tp_net
*pn
= l2tp_pernet(net
);
348 struct l2tp_tunnel
*tunnel
;
352 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
354 rcu_read_unlock_bh();
359 rcu_read_unlock_bh();
363 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth
);
365 /*****************************************************************************
366 * Receive data handling
367 *****************************************************************************/
369 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
372 static void l2tp_recv_queue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
374 struct sk_buff
*skbp
;
376 u32 ns
= L2TP_SKB_CB(skb
)->ns
;
378 spin_lock_bh(&session
->reorder_q
.lock
);
379 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
380 if (L2TP_SKB_CB(skbp
)->ns
> ns
) {
381 __skb_queue_before(&session
->reorder_q
, skbp
, skb
);
382 l2tp_dbg(session
, L2TP_MSG_SEQ
,
383 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
384 session
->name
, ns
, L2TP_SKB_CB(skbp
)->ns
,
385 skb_queue_len(&session
->reorder_q
));
386 atomic_long_inc(&session
->stats
.rx_oos_packets
);
391 __skb_queue_tail(&session
->reorder_q
, skb
);
394 spin_unlock_bh(&session
->reorder_q
.lock
);
397 /* Dequeue a single skb.
399 static void l2tp_recv_dequeue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
401 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
402 int length
= L2TP_SKB_CB(skb
)->length
;
404 /* We're about to requeue the skb, so return resources
405 * to its current owner (a socket receive buffer).
409 atomic_long_inc(&tunnel
->stats
.rx_packets
);
410 atomic_long_add(length
, &tunnel
->stats
.rx_bytes
);
411 atomic_long_inc(&session
->stats
.rx_packets
);
412 atomic_long_add(length
, &session
->stats
.rx_bytes
);
414 if (L2TP_SKB_CB(skb
)->has_seq
) {
417 session
->nr
&= session
->nr_max
;
419 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated nr to %hu\n",
420 session
->name
, session
->nr
);
423 /* call private receive handler */
424 if (session
->recv_skb
!= NULL
)
425 (*session
->recv_skb
)(session
, skb
, L2TP_SKB_CB(skb
)->length
);
430 (*session
->deref
)(session
);
433 /* Dequeue skbs from the session's reorder_q, subject to packet order.
434 * Skbs that have been in the queue for too long are simply discarded.
436 static void l2tp_recv_dequeue(struct l2tp_session
*session
)
441 /* If the pkt at the head of the queue has the nr that we
442 * expect to send up next, dequeue it and any other
443 * in-sequence packets behind it.
446 spin_lock_bh(&session
->reorder_q
.lock
);
447 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
448 if (time_after(jiffies
, L2TP_SKB_CB(skb
)->expires
)) {
449 atomic_long_inc(&session
->stats
.rx_seq_discards
);
450 atomic_long_inc(&session
->stats
.rx_errors
);
451 l2tp_dbg(session
, L2TP_MSG_SEQ
,
452 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
453 session
->name
, L2TP_SKB_CB(skb
)->ns
,
454 L2TP_SKB_CB(skb
)->length
, session
->nr
,
455 skb_queue_len(&session
->reorder_q
));
456 session
->reorder_skip
= 1;
457 __skb_unlink(skb
, &session
->reorder_q
);
460 (*session
->deref
)(session
);
464 if (L2TP_SKB_CB(skb
)->has_seq
) {
465 if (session
->reorder_skip
) {
466 l2tp_dbg(session
, L2TP_MSG_SEQ
,
467 "%s: advancing nr to next pkt: %u -> %u",
468 session
->name
, session
->nr
,
469 L2TP_SKB_CB(skb
)->ns
);
470 session
->reorder_skip
= 0;
471 session
->nr
= L2TP_SKB_CB(skb
)->ns
;
473 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
474 l2tp_dbg(session
, L2TP_MSG_SEQ
,
475 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
476 session
->name
, L2TP_SKB_CB(skb
)->ns
,
477 L2TP_SKB_CB(skb
)->length
, session
->nr
,
478 skb_queue_len(&session
->reorder_q
));
482 __skb_unlink(skb
, &session
->reorder_q
);
484 /* Process the skb. We release the queue lock while we
485 * do so to let other contexts process the queue.
487 spin_unlock_bh(&session
->reorder_q
.lock
);
488 l2tp_recv_dequeue_skb(session
, skb
);
493 spin_unlock_bh(&session
->reorder_q
.lock
);
496 static inline int l2tp_verify_udp_checksum(struct sock
*sk
,
499 struct udphdr
*uh
= udp_hdr(skb
);
500 u16 ulen
= ntohs(uh
->len
);
503 if (sk
->sk_no_check
|| skb_csum_unnecessary(skb
))
506 #if IS_ENABLED(CONFIG_IPV6)
507 if (sk
->sk_family
== PF_INET6
) {
509 LIMIT_NETDEBUG(KERN_INFO
"L2TP: IPv6: checksum is 0\n");
512 if ((skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
513 !csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
514 &ipv6_hdr(skb
)->daddr
, ulen
,
515 IPPROTO_UDP
, skb
->csum
)) {
516 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
519 skb
->csum
= ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
520 &ipv6_hdr(skb
)->daddr
,
521 skb
->len
, IPPROTO_UDP
,
526 struct inet_sock
*inet
;
530 psum
= csum_tcpudp_nofold(inet
->inet_saddr
, inet
->inet_daddr
,
531 ulen
, IPPROTO_UDP
, 0);
533 if ((skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
534 !csum_fold(csum_add(psum
, skb
->csum
)))
539 return __skb_checksum_complete(skb
);
542 static int l2tp_seq_check_rx_window(struct l2tp_session
*session
, u32 nr
)
546 if (nr
>= session
->nr
)
547 nws
= nr
- session
->nr
;
549 nws
= (session
->nr_max
+ 1) - (session
->nr
- nr
);
551 return nws
< session
->nr_window_size
;
554 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
555 * acceptable, else non-zero.
557 static int l2tp_recv_data_seq(struct l2tp_session
*session
, struct sk_buff
*skb
)
559 if (!l2tp_seq_check_rx_window(session
, L2TP_SKB_CB(skb
)->ns
)) {
560 /* Packet sequence number is outside allowed window.
563 l2tp_dbg(session
, L2TP_MSG_SEQ
,
564 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
565 session
->name
, L2TP_SKB_CB(skb
)->ns
,
566 L2TP_SKB_CB(skb
)->length
, session
->nr
);
570 if (session
->reorder_timeout
!= 0) {
571 /* Packet reordering enabled. Add skb to session's
572 * reorder queue, in order of ns.
574 l2tp_recv_queue_skb(session
, skb
);
578 /* Packet reordering disabled. Discard out-of-sequence packets, while
579 * tracking the number if in-sequence packets after the first OOS packet
580 * is seen. After nr_oos_count_max in-sequence packets, reset the
581 * sequence number to re-enable packet reception.
583 if (L2TP_SKB_CB(skb
)->ns
== session
->nr
) {
584 skb_queue_tail(&session
->reorder_q
, skb
);
586 u32 nr_oos
= L2TP_SKB_CB(skb
)->ns
;
587 u32 nr_next
= (session
->nr_oos
+ 1) & session
->nr_max
;
589 if (nr_oos
== nr_next
)
590 session
->nr_oos_count
++;
592 session
->nr_oos_count
= 0;
594 session
->nr_oos
= nr_oos
;
595 if (session
->nr_oos_count
> session
->nr_oos_count_max
) {
596 session
->reorder_skip
= 1;
597 l2tp_dbg(session
, L2TP_MSG_SEQ
,
598 "%s: %d oos packets received. Resetting sequence numbers\n",
599 session
->name
, session
->nr_oos_count
);
601 if (!session
->reorder_skip
) {
602 atomic_long_inc(&session
->stats
.rx_seq_discards
);
603 l2tp_dbg(session
, L2TP_MSG_SEQ
,
604 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
605 session
->name
, L2TP_SKB_CB(skb
)->ns
,
606 L2TP_SKB_CB(skb
)->length
, session
->nr
,
607 skb_queue_len(&session
->reorder_q
));
610 skb_queue_tail(&session
->reorder_q
, skb
);
620 /* Do receive processing of L2TP data frames. We handle both L2TPv2
621 * and L2TPv3 data frames here.
623 * L2TPv2 Data Message Header
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 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630 * | Tunnel ID | Session ID |
631 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 * | Ns (opt) | Nr (opt) |
633 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 * | Offset Size (opt) | Offset pad... (opt)
635 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
637 * Data frames are marked by T=0. All other fields are the same as
638 * those in L2TP control frames.
640 * L2TPv3 Data Message Header
642 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
643 * | L2TP Session Header |
644 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
645 * | L2-Specific Sublayer |
646 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
647 * | Tunnel Payload ...
648 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
650 * L2TPv3 Session Header Over IP
653 * 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
654 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657 * | Cookie (optional, maximum 64 bits)...
658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
662 * L2TPv3 L2-Specific Sublayer Format
665 * 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
666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
667 * |x|S|x|x|x|x|x|x| Sequence Number |
668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
670 * Cookie value, sublayer format and offset (pad) are negotiated with
671 * the peer when the session is set up. Unlike L2TPv2, we do not need
672 * to parse the packet header to determine if optional fields are
675 * Caller must already have parsed the frame and determined that it is
676 * a data (not control) frame before coming here. Fields up to the
677 * session-id have already been parsed and ptr points to the data
678 * after the session-id.
680 void l2tp_recv_common(struct l2tp_session
*session
, struct sk_buff
*skb
,
681 unsigned char *ptr
, unsigned char *optr
, u16 hdrflags
,
682 int length
, int (*payload_hook
)(struct sk_buff
*skb
))
684 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
688 /* The ref count is increased since we now hold a pointer to
689 * the session. Take care to decrement the refcnt when exiting
690 * this function from now on...
692 l2tp_session_inc_refcount(session
);
694 (*session
->ref
)(session
);
696 /* Parse and check optional cookie */
697 if (session
->peer_cookie_len
> 0) {
698 if (memcmp(ptr
, &session
->peer_cookie
[0], session
->peer_cookie_len
)) {
699 l2tp_info(tunnel
, L2TP_MSG_DATA
,
700 "%s: cookie mismatch (%u/%u). Discarding.\n",
701 tunnel
->name
, tunnel
->tunnel_id
,
702 session
->session_id
);
703 atomic_long_inc(&session
->stats
.rx_cookie_discards
);
706 ptr
+= session
->peer_cookie_len
;
709 /* Handle the optional sequence numbers. Sequence numbers are
710 * in different places for L2TPv2 and L2TPv3.
712 * If we are the LAC, enable/disable sequence numbers under
713 * the control of the LNS. If no sequence numbers present but
714 * we were expecting them, discard frame.
717 L2TP_SKB_CB(skb
)->has_seq
= 0;
718 if (tunnel
->version
== L2TP_HDR_VER_2
) {
719 if (hdrflags
& L2TP_HDRFLAG_S
) {
720 ns
= ntohs(*(__be16
*) ptr
);
722 nr
= ntohs(*(__be16
*) ptr
);
725 /* Store L2TP info in the skb */
726 L2TP_SKB_CB(skb
)->ns
= ns
;
727 L2TP_SKB_CB(skb
)->has_seq
= 1;
729 l2tp_dbg(session
, L2TP_MSG_SEQ
,
730 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
731 session
->name
, ns
, nr
, session
->nr
);
733 } else if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
734 u32 l2h
= ntohl(*(__be32
*) ptr
);
736 if (l2h
& 0x40000000) {
737 ns
= l2h
& 0x00ffffff;
739 /* Store L2TP info in the skb */
740 L2TP_SKB_CB(skb
)->ns
= ns
;
741 L2TP_SKB_CB(skb
)->has_seq
= 1;
743 l2tp_dbg(session
, L2TP_MSG_SEQ
,
744 "%s: recv data ns=%u, session nr=%u\n",
745 session
->name
, ns
, session
->nr
);
749 /* Advance past L2-specific header, if present */
750 ptr
+= session
->l2specific_len
;
752 if (L2TP_SKB_CB(skb
)->has_seq
) {
753 /* Received a packet with sequence numbers. If we're the LNS,
754 * check if we sre sending sequence numbers and if not,
757 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
758 l2tp_info(session
, L2TP_MSG_SEQ
,
759 "%s: requested to enable seq numbers by LNS\n",
761 session
->send_seq
= -1;
762 l2tp_session_set_header_len(session
, tunnel
->version
);
765 /* No sequence numbers.
766 * If user has configured mandatory sequence numbers, discard.
768 if (session
->recv_seq
) {
769 l2tp_warn(session
, L2TP_MSG_SEQ
,
770 "%s: recv data has no seq numbers when required. Discarding.\n",
772 atomic_long_inc(&session
->stats
.rx_seq_discards
);
776 /* If we're the LAC and we're sending sequence numbers, the
777 * LNS has requested that we no longer send sequence numbers.
778 * If we're the LNS and we're sending sequence numbers, the
779 * LAC is broken. Discard the frame.
781 if ((!session
->lns_mode
) && (session
->send_seq
)) {
782 l2tp_info(session
, L2TP_MSG_SEQ
,
783 "%s: requested to disable seq numbers by LNS\n",
785 session
->send_seq
= 0;
786 l2tp_session_set_header_len(session
, tunnel
->version
);
787 } else if (session
->send_seq
) {
788 l2tp_warn(session
, L2TP_MSG_SEQ
,
789 "%s: recv data has no seq numbers when required. Discarding.\n",
791 atomic_long_inc(&session
->stats
.rx_seq_discards
);
796 /* Session data offset is handled differently for L2TPv2 and
797 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
798 * the header. For L2TPv3, the offset is negotiated using AVPs
799 * in the session setup control protocol.
801 if (tunnel
->version
== L2TP_HDR_VER_2
) {
802 /* If offset bit set, skip it. */
803 if (hdrflags
& L2TP_HDRFLAG_O
) {
804 offset
= ntohs(*(__be16
*)ptr
);
808 ptr
+= session
->offset
;
811 if (!pskb_may_pull(skb
, offset
))
814 __skb_pull(skb
, offset
);
816 /* If caller wants to process the payload before we queue the
820 if ((*payload_hook
)(skb
))
823 /* Prepare skb for adding to the session's reorder_q. Hold
824 * packets for max reorder_timeout or 1 second if not
827 L2TP_SKB_CB(skb
)->length
= length
;
828 L2TP_SKB_CB(skb
)->expires
= jiffies
+
829 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
831 /* Add packet to the session's receive queue. Reordering is done here, if
832 * enabled. Saved L2TP protocol info is stored in skb->sb[].
834 if (L2TP_SKB_CB(skb
)->has_seq
) {
835 if (l2tp_recv_data_seq(session
, skb
))
838 /* No sequence numbers. Add the skb to the tail of the
839 * reorder queue. This ensures that it will be
840 * delivered after all previous sequenced skbs.
842 skb_queue_tail(&session
->reorder_q
, skb
);
845 /* Try to dequeue as many skbs from reorder_q as we can. */
846 l2tp_recv_dequeue(session
);
848 l2tp_session_dec_refcount(session
);
853 atomic_long_inc(&session
->stats
.rx_errors
);
857 (*session
->deref
)(session
);
859 l2tp_session_dec_refcount(session
);
861 EXPORT_SYMBOL(l2tp_recv_common
);
863 /* Drop skbs from the session's reorder_q
865 int l2tp_session_queue_purge(struct l2tp_session
*session
)
867 struct sk_buff
*skb
= NULL
;
869 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
870 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
871 atomic_long_inc(&session
->stats
.rx_errors
);
874 (*session
->deref
)(session
);
878 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge
);
880 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
881 * here. The skb is not on a list when we get here.
882 * Returns 0 if the packet was a data packet and was successfully passed on.
883 * Returns 1 if the packet was not a good data packet and could not be
884 * forwarded. All such packets are passed up to userspace to deal with.
886 static int l2tp_udp_recv_core(struct l2tp_tunnel
*tunnel
, struct sk_buff
*skb
,
887 int (*payload_hook
)(struct sk_buff
*skb
))
889 struct l2tp_session
*session
= NULL
;
890 unsigned char *ptr
, *optr
;
892 u32 tunnel_id
, session_id
;
896 if (tunnel
->sock
&& l2tp_verify_udp_checksum(tunnel
->sock
, skb
))
897 goto discard_bad_csum
;
899 /* UDP always verifies the packet length. */
900 __skb_pull(skb
, sizeof(struct udphdr
));
903 if (!pskb_may_pull(skb
, L2TP_HDR_SIZE_SEQ
)) {
904 l2tp_info(tunnel
, L2TP_MSG_DATA
,
905 "%s: recv short packet (len=%d)\n",
906 tunnel
->name
, skb
->len
);
910 /* Trace packet contents, if enabled */
911 if (tunnel
->debug
& L2TP_MSG_DATA
) {
912 length
= min(32u, skb
->len
);
913 if (!pskb_may_pull(skb
, length
))
916 pr_debug("%s: recv\n", tunnel
->name
);
917 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
920 /* Point to L2TP header */
921 optr
= ptr
= skb
->data
;
923 /* Get L2TP header flags */
924 hdrflags
= ntohs(*(__be16
*) ptr
);
926 /* Check protocol version */
927 version
= hdrflags
& L2TP_HDR_VER_MASK
;
928 if (version
!= tunnel
->version
) {
929 l2tp_info(tunnel
, L2TP_MSG_DATA
,
930 "%s: recv protocol version mismatch: got %d expected %d\n",
931 tunnel
->name
, version
, tunnel
->version
);
935 /* Get length of L2TP packet */
938 /* If type is control packet, it is handled by userspace. */
939 if (hdrflags
& L2TP_HDRFLAG_T
) {
940 l2tp_dbg(tunnel
, L2TP_MSG_DATA
,
941 "%s: recv control packet, len=%d\n",
942 tunnel
->name
, length
);
949 if (tunnel
->version
== L2TP_HDR_VER_2
) {
950 /* If length is present, skip it */
951 if (hdrflags
& L2TP_HDRFLAG_L
)
954 /* Extract tunnel and session ID */
955 tunnel_id
= ntohs(*(__be16
*) ptr
);
957 session_id
= ntohs(*(__be16
*) ptr
);
960 ptr
+= 2; /* skip reserved bits */
961 tunnel_id
= tunnel
->tunnel_id
;
962 session_id
= ntohl(*(__be32
*) ptr
);
966 /* Find the session context */
967 session
= l2tp_session_find(tunnel
->l2tp_net
, tunnel
, session_id
);
968 if (!session
|| !session
->recv_skb
) {
969 /* Not found? Pass to userspace to deal with */
970 l2tp_info(tunnel
, L2TP_MSG_DATA
,
971 "%s: no session found (%u/%u). Passing up.\n",
972 tunnel
->name
, tunnel_id
, session_id
);
976 l2tp_recv_common(session
, skb
, ptr
, optr
, hdrflags
, length
, payload_hook
);
981 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel
->name
);
982 UDP_INC_STATS_USER(tunnel
->l2tp_net
, UDP_MIB_INERRORS
, 0);
983 atomic_long_inc(&tunnel
->stats
.rx_errors
);
989 /* Put UDP header back */
990 __skb_push(skb
, sizeof(struct udphdr
));
995 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
999 * >0: skb should be passed up to userspace as UDP.
1001 int l2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
1003 struct l2tp_tunnel
*tunnel
;
1005 tunnel
= l2tp_sock_to_tunnel(sk
);
1009 l2tp_dbg(tunnel
, L2TP_MSG_DATA
, "%s: received %d bytes\n",
1010 tunnel
->name
, skb
->len
);
1012 if (l2tp_udp_recv_core(tunnel
, skb
, tunnel
->recv_payload_hook
))
1023 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv
);
1025 /************************************************************************
1027 ***********************************************************************/
1029 /* Build an L2TP header for the session into the buffer provided.
1031 static int l2tp_build_l2tpv2_header(struct l2tp_session
*session
, void *buf
)
1033 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1036 u16 flags
= L2TP_HDR_VER_2
;
1037 u32 tunnel_id
= tunnel
->peer_tunnel_id
;
1038 u32 session_id
= session
->peer_session_id
;
1040 if (session
->send_seq
)
1041 flags
|= L2TP_HDRFLAG_S
;
1043 /* Setup L2TP header. */
1044 *bufp
++ = htons(flags
);
1045 *bufp
++ = htons(tunnel_id
);
1046 *bufp
++ = htons(session_id
);
1047 if (session
->send_seq
) {
1048 *bufp
++ = htons(session
->ns
);
1051 session
->ns
&= 0xffff;
1052 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated ns to %u\n",
1053 session
->name
, session
->ns
);
1059 static int l2tp_build_l2tpv3_header(struct l2tp_session
*session
, void *buf
)
1061 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1065 /* Setup L2TP header. The header differs slightly for UDP and
1066 * IP encapsulations. For UDP, there is 4 bytes of flags.
1068 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
1069 u16 flags
= L2TP_HDR_VER_3
;
1070 *((__be16
*) bufp
) = htons(flags
);
1072 *((__be16
*) bufp
) = 0;
1076 *((__be32
*) bufp
) = htonl(session
->peer_session_id
);
1078 if (session
->cookie_len
) {
1079 memcpy(bufp
, &session
->cookie
[0], session
->cookie_len
);
1080 bufp
+= session
->cookie_len
;
1082 if (session
->l2specific_len
) {
1083 if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
1085 if (session
->send_seq
) {
1086 l2h
= 0x40000000 | session
->ns
;
1088 session
->ns
&= 0xffffff;
1089 l2tp_dbg(session
, L2TP_MSG_SEQ
,
1090 "%s: updated ns to %u\n",
1091 session
->name
, session
->ns
);
1094 *((__be32
*) bufp
) = htonl(l2h
);
1096 bufp
+= session
->l2specific_len
;
1098 if (session
->offset
)
1099 bufp
+= session
->offset
;
1104 static int l2tp_xmit_core(struct l2tp_session
*session
, struct sk_buff
*skb
,
1105 struct flowi
*fl
, size_t data_len
)
1107 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1108 unsigned int len
= skb
->len
;
1112 if (session
->send_seq
)
1113 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %Zd bytes, ns=%u\n",
1114 session
->name
, data_len
, session
->ns
- 1);
1116 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %Zd bytes\n",
1117 session
->name
, data_len
);
1119 if (session
->debug
& L2TP_MSG_DATA
) {
1120 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1121 unsigned char *datap
= skb
->data
+ uhlen
;
1123 pr_debug("%s: xmit\n", session
->name
);
1124 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1125 datap
, min_t(size_t, 32, len
- uhlen
));
1128 /* Queue the packet to IP for output */
1130 #if IS_ENABLED(CONFIG_IPV6)
1131 if (skb
->sk
->sk_family
== PF_INET6
)
1132 error
= inet6_csk_xmit(skb
, NULL
);
1135 error
= ip_queue_xmit(skb
, fl
);
1139 atomic_long_inc(&tunnel
->stats
.tx_packets
);
1140 atomic_long_add(len
, &tunnel
->stats
.tx_bytes
);
1141 atomic_long_inc(&session
->stats
.tx_packets
);
1142 atomic_long_add(len
, &session
->stats
.tx_bytes
);
1144 atomic_long_inc(&tunnel
->stats
.tx_errors
);
1145 atomic_long_inc(&session
->stats
.tx_errors
);
1151 /* Automatically called when the skb is freed.
1153 static void l2tp_sock_wfree(struct sk_buff
*skb
)
1158 /* For data skbs that we transmit, we associate with the tunnel socket
1159 * but don't do accounting.
1161 static inline void l2tp_skb_set_owner_w(struct sk_buff
*skb
, struct sock
*sk
)
1165 skb
->destructor
= l2tp_sock_wfree
;
1168 #if IS_ENABLED(CONFIG_IPV6)
1169 static void l2tp_xmit_ipv6_csum(struct sock
*sk
, struct sk_buff
*skb
,
1172 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1173 struct udphdr
*uh
= udp_hdr(skb
);
1175 if (!skb_dst(skb
) || !skb_dst(skb
)->dev
||
1176 !(skb_dst(skb
)->dev
->features
& NETIF_F_IPV6_CSUM
)) {
1177 __wsum csum
= skb_checksum(skb
, 0, udp_len
, 0);
1178 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1179 uh
->check
= csum_ipv6_magic(&np
->saddr
, &np
->daddr
, udp_len
,
1182 uh
->check
= CSUM_MANGLED_0
;
1184 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1185 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
1186 skb
->csum_offset
= offsetof(struct udphdr
, check
);
1187 uh
->check
= ~csum_ipv6_magic(&np
->saddr
, &np
->daddr
,
1188 udp_len
, IPPROTO_UDP
, 0);
1193 /* If caller requires the skb to have a ppp header, the header must be
1194 * inserted in the skb data before calling this function.
1196 int l2tp_xmit_skb(struct l2tp_session
*session
, struct sk_buff
*skb
, int hdr_len
)
1198 int data_len
= skb
->len
;
1199 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1200 struct sock
*sk
= tunnel
->sock
;
1203 struct inet_sock
*inet
;
1206 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1208 int ret
= NET_XMIT_SUCCESS
;
1210 /* Check that there's enough headroom in the skb to insert IP,
1211 * UDP and L2TP headers. If not enough, expand it to
1212 * make room. Adjust truesize.
1214 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1216 if (skb_cow_head(skb
, headroom
)) {
1218 return NET_XMIT_DROP
;
1222 /* Setup L2TP header */
1223 session
->build_header(session
, __skb_push(skb
, hdr_len
));
1225 /* Reset skb netfilter state */
1226 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1227 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1232 if (sock_owned_by_user(sk
)) {
1234 ret
= NET_XMIT_DROP
;
1238 /* Get routing info from the tunnel socket */
1240 skb_dst_set(skb
, dst_clone(__sk_dst_check(sk
, 0)));
1243 fl
= &inet
->cork
.fl
;
1244 switch (tunnel
->encap
) {
1245 case L2TP_ENCAPTYPE_UDP
:
1246 /* Setup UDP header */
1247 __skb_push(skb
, sizeof(*uh
));
1248 skb_reset_transport_header(skb
);
1250 uh
->source
= inet
->inet_sport
;
1251 uh
->dest
= inet
->inet_dport
;
1252 udp_len
= uhlen
+ hdr_len
+ data_len
;
1253 uh
->len
= htons(udp_len
);
1256 /* Calculate UDP checksum if configured to do so */
1257 #if IS_ENABLED(CONFIG_IPV6)
1258 if (sk
->sk_family
== PF_INET6
)
1259 l2tp_xmit_ipv6_csum(sk
, skb
, udp_len
);
1262 if (sk
->sk_no_check
== UDP_CSUM_NOXMIT
)
1263 skb
->ip_summed
= CHECKSUM_NONE
;
1264 else if ((skb_dst(skb
) && skb_dst(skb
)->dev
) &&
1265 (!(skb_dst(skb
)->dev
->features
& NETIF_F_V4_CSUM
))) {
1266 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1267 csum
= skb_checksum(skb
, 0, udp_len
, 0);
1268 uh
->check
= csum_tcpudp_magic(inet
->inet_saddr
,
1270 udp_len
, IPPROTO_UDP
, csum
);
1272 uh
->check
= CSUM_MANGLED_0
;
1274 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1275 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
1276 skb
->csum_offset
= offsetof(struct udphdr
, check
);
1277 uh
->check
= ~csum_tcpudp_magic(inet
->inet_saddr
,
1279 udp_len
, IPPROTO_UDP
, 0);
1283 case L2TP_ENCAPTYPE_IP
:
1287 l2tp_skb_set_owner_w(skb
, sk
);
1289 l2tp_xmit_core(session
, skb
, fl
, data_len
);
1295 EXPORT_SYMBOL_GPL(l2tp_xmit_skb
);
1297 /*****************************************************************************
1298 * Tinnel and session create/destroy.
1299 *****************************************************************************/
1301 /* Tunnel socket destruct hook.
1302 * The tunnel context is deleted only when all session sockets have been
1305 static void l2tp_tunnel_destruct(struct sock
*sk
)
1307 struct l2tp_tunnel
*tunnel
;
1308 struct l2tp_net
*pn
;
1310 tunnel
= sk
->sk_user_data
;
1314 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing...\n", tunnel
->name
);
1317 /* Disable udp encapsulation */
1318 switch (tunnel
->encap
) {
1319 case L2TP_ENCAPTYPE_UDP
:
1320 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1321 (udp_sk(sk
))->encap_type
= 0;
1322 (udp_sk(sk
))->encap_rcv
= NULL
;
1323 (udp_sk(sk
))->encap_destroy
= NULL
;
1325 case L2TP_ENCAPTYPE_IP
:
1329 /* Remove hooks into tunnel socket */
1330 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1331 sk
->sk_user_data
= NULL
;
1332 tunnel
->sock
= NULL
;
1334 /* Remove the tunnel struct from the tunnel list */
1335 pn
= l2tp_pernet(tunnel
->l2tp_net
);
1336 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1337 list_del_rcu(&tunnel
->list
);
1338 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1339 atomic_dec(&l2tp_tunnel_count
);
1341 l2tp_tunnel_closeall(tunnel
);
1342 l2tp_tunnel_dec_refcount(tunnel
);
1344 /* Call the original destructor */
1345 if (sk
->sk_destruct
)
1346 (*sk
->sk_destruct
)(sk
);
1351 /* When the tunnel is closed, all the attached sessions need to go too.
1353 void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
)
1356 struct hlist_node
*walk
;
1357 struct hlist_node
*tmp
;
1358 struct l2tp_session
*session
;
1360 BUG_ON(tunnel
== NULL
);
1362 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing all sessions...\n",
1365 write_lock_bh(&tunnel
->hlist_lock
);
1366 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
1368 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1369 session
= hlist_entry(walk
, struct l2tp_session
, hlist
);
1371 l2tp_info(session
, L2TP_MSG_CONTROL
,
1372 "%s: closing session\n", session
->name
);
1374 hlist_del_init(&session
->hlist
);
1376 if (session
->ref
!= NULL
)
1377 (*session
->ref
)(session
);
1379 write_unlock_bh(&tunnel
->hlist_lock
);
1381 __l2tp_session_unhash(session
);
1382 l2tp_session_queue_purge(session
);
1384 if (session
->session_close
!= NULL
)
1385 (*session
->session_close
)(session
);
1387 if (session
->deref
!= NULL
)
1388 (*session
->deref
)(session
);
1390 l2tp_session_dec_refcount(session
);
1392 write_lock_bh(&tunnel
->hlist_lock
);
1394 /* Now restart from the beginning of this hash
1395 * chain. We always remove a session from the
1396 * list so we are guaranteed to make forward
1402 write_unlock_bh(&tunnel
->hlist_lock
);
1404 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall
);
1406 /* Tunnel socket destroy hook for UDP encapsulation */
1407 static void l2tp_udp_encap_destroy(struct sock
*sk
)
1409 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
1411 l2tp_tunnel_closeall(tunnel
);
1416 /* Really kill the tunnel.
1417 * Come here only when all sessions have been cleared from the tunnel.
1419 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
)
1421 BUG_ON(atomic_read(&tunnel
->ref_count
) != 0);
1422 BUG_ON(tunnel
->sock
!= NULL
);
1423 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: free...\n", tunnel
->name
);
1424 kfree_rcu(tunnel
, rcu
);
1427 /* Workqueue tunnel deletion function */
1428 static void l2tp_tunnel_del_work(struct work_struct
*work
)
1430 struct l2tp_tunnel
*tunnel
= NULL
;
1431 struct socket
*sock
= NULL
;
1432 struct sock
*sk
= NULL
;
1434 tunnel
= container_of(work
, struct l2tp_tunnel
, del_work
);
1435 sk
= l2tp_tunnel_sock_lookup(tunnel
);
1439 sock
= sk
->sk_socket
;
1441 /* If the tunnel socket was created by userspace, then go through the
1442 * inet layer to shut the socket down, and let userspace close it.
1443 * Otherwise, if we created the socket directly within the kernel, use
1444 * the sk API to release it here.
1445 * In either case the tunnel resources are freed in the socket
1446 * destructor when the tunnel socket goes away.
1448 if (tunnel
->fd
>= 0) {
1450 inet_shutdown(sock
, 2);
1453 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1454 sk_release_kernel(sk
);
1457 l2tp_tunnel_sock_put(sk
);
1460 /* Create a socket for the tunnel, if one isn't set up by
1461 * userspace. This is used for static tunnels where there is no
1462 * managing L2TP daemon.
1464 * Since we don't want these sockets to keep a namespace alive by
1465 * themselves, we drop the socket's namespace refcount after creation.
1466 * These sockets are freed when the namespace exits using the pernet
1469 static int l2tp_tunnel_sock_create(struct net
*net
,
1472 struct l2tp_tunnel_cfg
*cfg
,
1473 struct socket
**sockp
)
1476 struct socket
*sock
= NULL
;
1477 struct sockaddr_in udp_addr
= {0};
1478 struct sockaddr_l2tpip ip_addr
= {0};
1479 #if IS_ENABLED(CONFIG_IPV6)
1480 struct sockaddr_in6 udp6_addr
= {0};
1481 struct sockaddr_l2tpip6 ip6_addr
= {0};
1484 switch (cfg
->encap
) {
1485 case L2TP_ENCAPTYPE_UDP
:
1486 #if IS_ENABLED(CONFIG_IPV6)
1487 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1488 err
= sock_create_kern(AF_INET6
, SOCK_DGRAM
, 0, &sock
);
1492 sk_change_net(sock
->sk
, net
);
1494 udp6_addr
.sin6_family
= AF_INET6
;
1495 memcpy(&udp6_addr
.sin6_addr
, cfg
->local_ip6
,
1496 sizeof(udp6_addr
.sin6_addr
));
1497 udp6_addr
.sin6_port
= htons(cfg
->local_udp_port
);
1498 err
= kernel_bind(sock
, (struct sockaddr
*) &udp6_addr
,
1503 udp6_addr
.sin6_family
= AF_INET6
;
1504 memcpy(&udp6_addr
.sin6_addr
, cfg
->peer_ip6
,
1505 sizeof(udp6_addr
.sin6_addr
));
1506 udp6_addr
.sin6_port
= htons(cfg
->peer_udp_port
);
1507 err
= kernel_connect(sock
,
1508 (struct sockaddr
*) &udp6_addr
,
1509 sizeof(udp6_addr
), 0);
1515 err
= sock_create_kern(AF_INET
, SOCK_DGRAM
, 0, &sock
);
1519 sk_change_net(sock
->sk
, net
);
1521 udp_addr
.sin_family
= AF_INET
;
1522 udp_addr
.sin_addr
= cfg
->local_ip
;
1523 udp_addr
.sin_port
= htons(cfg
->local_udp_port
);
1524 err
= kernel_bind(sock
, (struct sockaddr
*) &udp_addr
,
1529 udp_addr
.sin_family
= AF_INET
;
1530 udp_addr
.sin_addr
= cfg
->peer_ip
;
1531 udp_addr
.sin_port
= htons(cfg
->peer_udp_port
);
1532 err
= kernel_connect(sock
,
1533 (struct sockaddr
*) &udp_addr
,
1534 sizeof(udp_addr
), 0);
1539 if (!cfg
->use_udp_checksums
)
1540 sock
->sk
->sk_no_check
= UDP_CSUM_NOXMIT
;
1544 case L2TP_ENCAPTYPE_IP
:
1545 #if IS_ENABLED(CONFIG_IPV6)
1546 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1547 err
= sock_create_kern(AF_INET6
, SOCK_DGRAM
,
1548 IPPROTO_L2TP
, &sock
);
1552 sk_change_net(sock
->sk
, net
);
1554 ip6_addr
.l2tp_family
= AF_INET6
;
1555 memcpy(&ip6_addr
.l2tp_addr
, cfg
->local_ip6
,
1556 sizeof(ip6_addr
.l2tp_addr
));
1557 ip6_addr
.l2tp_conn_id
= tunnel_id
;
1558 err
= kernel_bind(sock
, (struct sockaddr
*) &ip6_addr
,
1563 ip6_addr
.l2tp_family
= AF_INET6
;
1564 memcpy(&ip6_addr
.l2tp_addr
, cfg
->peer_ip6
,
1565 sizeof(ip6_addr
.l2tp_addr
));
1566 ip6_addr
.l2tp_conn_id
= peer_tunnel_id
;
1567 err
= kernel_connect(sock
,
1568 (struct sockaddr
*) &ip6_addr
,
1569 sizeof(ip6_addr
), 0);
1575 err
= sock_create_kern(AF_INET
, SOCK_DGRAM
,
1576 IPPROTO_L2TP
, &sock
);
1580 sk_change_net(sock
->sk
, net
);
1582 ip_addr
.l2tp_family
= AF_INET
;
1583 ip_addr
.l2tp_addr
= cfg
->local_ip
;
1584 ip_addr
.l2tp_conn_id
= tunnel_id
;
1585 err
= kernel_bind(sock
, (struct sockaddr
*) &ip_addr
,
1590 ip_addr
.l2tp_family
= AF_INET
;
1591 ip_addr
.l2tp_addr
= cfg
->peer_ip
;
1592 ip_addr
.l2tp_conn_id
= peer_tunnel_id
;
1593 err
= kernel_connect(sock
, (struct sockaddr
*) &ip_addr
,
1594 sizeof(ip_addr
), 0);
1606 if ((err
< 0) && sock
) {
1607 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1608 sk_release_kernel(sock
->sk
);
1615 static struct lock_class_key l2tp_socket_class
;
1617 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
)
1619 struct l2tp_tunnel
*tunnel
= NULL
;
1621 struct socket
*sock
= NULL
;
1622 struct sock
*sk
= NULL
;
1623 struct l2tp_net
*pn
;
1624 enum l2tp_encap_type encap
= L2TP_ENCAPTYPE_UDP
;
1626 /* Get the tunnel socket from the fd, which was opened by
1627 * the userspace L2TP daemon. If not specified, create a
1631 err
= l2tp_tunnel_sock_create(net
, tunnel_id
, peer_tunnel_id
,
1636 sock
= sockfd_lookup(fd
, &err
);
1638 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1639 tunnel_id
, fd
, err
);
1644 /* Reject namespace mismatches */
1645 if (!net_eq(sock_net(sock
->sk
), net
)) {
1646 pr_err("tunl %u: netns mismatch\n", tunnel_id
);
1657 /* Quick sanity checks */
1659 case L2TP_ENCAPTYPE_UDP
:
1660 err
= -EPROTONOSUPPORT
;
1661 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1662 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1663 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1667 case L2TP_ENCAPTYPE_IP
:
1668 err
= -EPROTONOSUPPORT
;
1669 if (sk
->sk_protocol
!= IPPROTO_L2TP
) {
1670 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1671 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_L2TP
);
1677 /* Check if this socket has already been prepped */
1678 tunnel
= (struct l2tp_tunnel
*)sk
->sk_user_data
;
1679 if (tunnel
!= NULL
) {
1680 /* This socket has already been prepped */
1685 tunnel
= kzalloc(sizeof(struct l2tp_tunnel
), GFP_KERNEL
);
1686 if (tunnel
== NULL
) {
1691 tunnel
->version
= version
;
1692 tunnel
->tunnel_id
= tunnel_id
;
1693 tunnel
->peer_tunnel_id
= peer_tunnel_id
;
1694 tunnel
->debug
= L2TP_DEFAULT_DEBUG_FLAGS
;
1696 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1697 sprintf(&tunnel
->name
[0], "tunl %u", tunnel_id
);
1698 rwlock_init(&tunnel
->hlist_lock
);
1700 /* The net we belong to */
1701 tunnel
->l2tp_net
= net
;
1702 pn
= l2tp_pernet(net
);
1705 tunnel
->debug
= cfg
->debug
;
1707 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1708 tunnel
->encap
= encap
;
1709 if (encap
== L2TP_ENCAPTYPE_UDP
) {
1710 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1711 udp_sk(sk
)->encap_type
= UDP_ENCAP_L2TPINUDP
;
1712 udp_sk(sk
)->encap_rcv
= l2tp_udp_encap_recv
;
1713 udp_sk(sk
)->encap_destroy
= l2tp_udp_encap_destroy
;
1714 #if IS_ENABLED(CONFIG_IPV6)
1715 if (sk
->sk_family
== PF_INET6
)
1716 udpv6_encap_enable();
1722 sk
->sk_user_data
= tunnel
;
1724 /* Hook on the tunnel socket destructor so that we can cleanup
1725 * if the tunnel socket goes away.
1727 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1728 sk
->sk_destruct
= &l2tp_tunnel_destruct
;
1731 lockdep_set_class_and_name(&sk
->sk_lock
.slock
, &l2tp_socket_class
, "l2tp_sock");
1733 sk
->sk_allocation
= GFP_ATOMIC
;
1735 /* Init delete workqueue struct */
1736 INIT_WORK(&tunnel
->del_work
, l2tp_tunnel_del_work
);
1738 /* Add tunnel to our list */
1739 INIT_LIST_HEAD(&tunnel
->list
);
1740 atomic_inc(&l2tp_tunnel_count
);
1742 /* Bump the reference count. The tunnel context is deleted
1743 * only when this drops to zero. Must be done before list insertion
1745 l2tp_tunnel_inc_refcount(tunnel
);
1746 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1747 list_add_rcu(&tunnel
->list
, &pn
->l2tp_tunnel_list
);
1748 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1755 /* If tunnel's socket was created by the kernel, it doesn't
1758 if (sock
&& sock
->file
)
1763 EXPORT_SYMBOL_GPL(l2tp_tunnel_create
);
1765 /* This function is used by the netlink TUNNEL_DELETE command.
1767 int l2tp_tunnel_delete(struct l2tp_tunnel
*tunnel
)
1769 l2tp_tunnel_closeall(tunnel
);
1770 return (false == queue_work(l2tp_wq
, &tunnel
->del_work
));
1772 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete
);
1774 /* Really kill the session.
1776 void l2tp_session_free(struct l2tp_session
*session
)
1778 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1780 BUG_ON(atomic_read(&session
->ref_count
) != 0);
1783 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1784 if (session
->session_id
!= 0)
1785 atomic_dec(&l2tp_session_count
);
1786 sock_put(tunnel
->sock
);
1787 session
->tunnel
= NULL
;
1788 l2tp_tunnel_dec_refcount(tunnel
);
1795 EXPORT_SYMBOL_GPL(l2tp_session_free
);
1797 /* Remove an l2tp session from l2tp_core's hash lists.
1798 * Provides a tidyup interface for pseudowire code which can't just route all
1799 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1802 void __l2tp_session_unhash(struct l2tp_session
*session
)
1804 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1806 /* Remove the session from core hashes */
1808 /* Remove from the per-tunnel hash */
1809 write_lock_bh(&tunnel
->hlist_lock
);
1810 hlist_del_init(&session
->hlist
);
1811 write_unlock_bh(&tunnel
->hlist_lock
);
1813 /* For L2TPv3 we have a per-net hash: remove from there, too */
1814 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1815 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1816 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1817 hlist_del_init_rcu(&session
->global_hlist
);
1818 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1823 EXPORT_SYMBOL_GPL(__l2tp_session_unhash
);
1825 /* This function is used by the netlink SESSION_DELETE command and by
1828 int l2tp_session_delete(struct l2tp_session
*session
)
1831 (*session
->ref
)(session
);
1832 __l2tp_session_unhash(session
);
1833 l2tp_session_queue_purge(session
);
1834 if (session
->session_close
!= NULL
)
1835 (*session
->session_close
)(session
);
1837 (*session
->deref
)(session
);
1838 l2tp_session_dec_refcount(session
);
1841 EXPORT_SYMBOL_GPL(l2tp_session_delete
);
1843 /* We come here whenever a session's send_seq, cookie_len or
1844 * l2specific_len parameters are set.
1846 static void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
)
1848 if (version
== L2TP_HDR_VER_2
) {
1849 session
->hdr_len
= 6;
1850 if (session
->send_seq
)
1851 session
->hdr_len
+= 4;
1853 session
->hdr_len
= 4 + session
->cookie_len
+ session
->l2specific_len
+ session
->offset
;
1854 if (session
->tunnel
->encap
== L2TP_ENCAPTYPE_UDP
)
1855 session
->hdr_len
+= 4;
1860 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
)
1862 struct l2tp_session
*session
;
1864 session
= kzalloc(sizeof(struct l2tp_session
) + priv_size
, GFP_KERNEL
);
1865 if (session
!= NULL
) {
1866 session
->magic
= L2TP_SESSION_MAGIC
;
1867 session
->tunnel
= tunnel
;
1869 session
->session_id
= session_id
;
1870 session
->peer_session_id
= peer_session_id
;
1872 if (tunnel
->version
== L2TP_HDR_VER_2
)
1873 session
->nr_max
= 0xffff;
1875 session
->nr_max
= 0xffffff;
1876 session
->nr_window_size
= session
->nr_max
/ 2;
1877 session
->nr_oos_count_max
= 4;
1879 /* Use NR of first received packet */
1880 session
->reorder_skip
= 1;
1882 sprintf(&session
->name
[0], "sess %u/%u",
1883 tunnel
->tunnel_id
, session
->session_id
);
1885 skb_queue_head_init(&session
->reorder_q
);
1887 INIT_HLIST_NODE(&session
->hlist
);
1888 INIT_HLIST_NODE(&session
->global_hlist
);
1890 /* Inherit debug options from tunnel */
1891 session
->debug
= tunnel
->debug
;
1894 session
->pwtype
= cfg
->pw_type
;
1895 session
->debug
= cfg
->debug
;
1896 session
->mtu
= cfg
->mtu
;
1897 session
->mru
= cfg
->mru
;
1898 session
->send_seq
= cfg
->send_seq
;
1899 session
->recv_seq
= cfg
->recv_seq
;
1900 session
->lns_mode
= cfg
->lns_mode
;
1901 session
->reorder_timeout
= cfg
->reorder_timeout
;
1902 session
->offset
= cfg
->offset
;
1903 session
->l2specific_type
= cfg
->l2specific_type
;
1904 session
->l2specific_len
= cfg
->l2specific_len
;
1905 session
->cookie_len
= cfg
->cookie_len
;
1906 memcpy(&session
->cookie
[0], &cfg
->cookie
[0], cfg
->cookie_len
);
1907 session
->peer_cookie_len
= cfg
->peer_cookie_len
;
1908 memcpy(&session
->peer_cookie
[0], &cfg
->peer_cookie
[0], cfg
->peer_cookie_len
);
1911 if (tunnel
->version
== L2TP_HDR_VER_2
)
1912 session
->build_header
= l2tp_build_l2tpv2_header
;
1914 session
->build_header
= l2tp_build_l2tpv3_header
;
1916 l2tp_session_set_header_len(session
, tunnel
->version
);
1918 /* Bump the reference count. The session context is deleted
1919 * only when this drops to zero.
1921 l2tp_session_inc_refcount(session
);
1922 l2tp_tunnel_inc_refcount(tunnel
);
1924 /* Ensure tunnel socket isn't deleted */
1925 sock_hold(tunnel
->sock
);
1927 /* Add session to the tunnel's hash list */
1928 write_lock_bh(&tunnel
->hlist_lock
);
1929 hlist_add_head(&session
->hlist
,
1930 l2tp_session_id_hash(tunnel
, session_id
));
1931 write_unlock_bh(&tunnel
->hlist_lock
);
1933 /* And to the global session list if L2TPv3 */
1934 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1935 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1937 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1938 hlist_add_head_rcu(&session
->global_hlist
,
1939 l2tp_session_id_hash_2(pn
, session_id
));
1940 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1943 /* Ignore management session in session count value */
1944 if (session
->session_id
!= 0)
1945 atomic_inc(&l2tp_session_count
);
1950 EXPORT_SYMBOL_GPL(l2tp_session_create
);
1952 /*****************************************************************************
1954 *****************************************************************************/
1956 static __net_init
int l2tp_init_net(struct net
*net
)
1958 struct l2tp_net
*pn
= net_generic(net
, l2tp_net_id
);
1961 INIT_LIST_HEAD(&pn
->l2tp_tunnel_list
);
1962 spin_lock_init(&pn
->l2tp_tunnel_list_lock
);
1964 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++)
1965 INIT_HLIST_HEAD(&pn
->l2tp_session_hlist
[hash
]);
1967 spin_lock_init(&pn
->l2tp_session_hlist_lock
);
1972 static __net_exit
void l2tp_exit_net(struct net
*net
)
1974 struct l2tp_net
*pn
= l2tp_pernet(net
);
1975 struct l2tp_tunnel
*tunnel
= NULL
;
1978 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
1979 (void)l2tp_tunnel_delete(tunnel
);
1981 rcu_read_unlock_bh();
1984 static struct pernet_operations l2tp_net_ops
= {
1985 .init
= l2tp_init_net
,
1986 .exit
= l2tp_exit_net
,
1988 .size
= sizeof(struct l2tp_net
),
1991 static int __init
l2tp_init(void)
1995 rc
= register_pernet_device(&l2tp_net_ops
);
1999 l2tp_wq
= alloc_workqueue("l2tp", WQ_NON_REENTRANT
| WQ_UNBOUND
, 0);
2001 pr_err("alloc_workqueue failed\n");
2006 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION
);
2012 static void __exit
l2tp_exit(void)
2014 unregister_pernet_device(&l2tp_net_ops
);
2016 destroy_workqueue(l2tp_wq
);
2021 module_init(l2tp_init
);
2022 module_exit(l2tp_exit
);
2024 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
2025 MODULE_DESCRIPTION("L2TP core");
2026 MODULE_LICENSE("GPL");
2027 MODULE_VERSION(L2TP_DRV_VERSION
);