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 struct workqueue_struct
*l2tp_wq
;
105 /* per-net private data for this module */
106 static unsigned int l2tp_net_id
;
108 struct list_head l2tp_tunnel_list
;
109 spinlock_t l2tp_tunnel_list_lock
;
110 struct hlist_head l2tp_session_hlist
[L2TP_HASH_SIZE_2
];
111 spinlock_t l2tp_session_hlist_lock
;
115 static inline struct l2tp_tunnel
*l2tp_tunnel(struct sock
*sk
)
117 return sk
->sk_user_data
;
120 static inline struct l2tp_net
*l2tp_pernet(const struct net
*net
)
124 return net_generic(net
, l2tp_net_id
);
127 /* Session hash global list for L2TPv3.
128 * The session_id SHOULD be random according to RFC3931, but several
129 * L2TP implementations use incrementing session_ids. So we do a real
130 * hash on the session_id, rather than a simple bitmask.
132 static inline struct hlist_head
*
133 l2tp_session_id_hash_2(struct l2tp_net
*pn
, u32 session_id
)
135 return &pn
->l2tp_session_hlist
[hash_32(session_id
, L2TP_HASH_BITS_2
)];
139 /* Lookup the tunnel socket, possibly involving the fs code if the socket is
140 * owned by userspace. A struct sock returned from this function must be
141 * released using l2tp_tunnel_sock_put once you're done with it.
143 static struct sock
*l2tp_tunnel_sock_lookup(struct l2tp_tunnel
*tunnel
)
146 struct socket
*sock
= NULL
;
147 struct sock
*sk
= NULL
;
152 if (tunnel
->fd
>= 0) {
153 /* Socket is owned by userspace, who might be in the process
154 * of closing it. Look the socket up using the fd to ensure
157 sock
= sockfd_lookup(tunnel
->fd
, &err
);
161 /* Socket is owned by kernelspace */
170 /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
171 static void l2tp_tunnel_sock_put(struct sock
*sk
)
173 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
175 if (tunnel
->fd
>= 0) {
176 /* Socket is owned by userspace */
177 sockfd_put(sk
->sk_socket
);
184 /* Session hash list.
185 * The session_id SHOULD be random according to RFC2661, but several
186 * L2TP implementations (Cisco and Microsoft) use incrementing
187 * session_ids. So we do a real hash on the session_id, rather than a
190 static inline struct hlist_head
*
191 l2tp_session_id_hash(struct l2tp_tunnel
*tunnel
, u32 session_id
)
193 return &tunnel
->session_hlist
[hash_32(session_id
, L2TP_HASH_BITS
)];
196 /* Lookup a tunnel. A new reference is held on the returned tunnel. */
197 struct l2tp_tunnel
*l2tp_tunnel_get(const struct net
*net
, u32 tunnel_id
)
199 const struct l2tp_net
*pn
= l2tp_pernet(net
);
200 struct l2tp_tunnel
*tunnel
;
203 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
204 if (tunnel
->tunnel_id
== tunnel_id
) {
205 l2tp_tunnel_inc_refcount(tunnel
);
206 rcu_read_unlock_bh();
211 rcu_read_unlock_bh();
215 EXPORT_SYMBOL_GPL(l2tp_tunnel_get
);
217 /* Lookup a session. A new reference is held on the returned session. */
218 struct l2tp_session
*l2tp_session_get(const struct net
*net
,
219 struct l2tp_tunnel
*tunnel
,
222 struct hlist_head
*session_list
;
223 struct l2tp_session
*session
;
226 struct l2tp_net
*pn
= l2tp_pernet(net
);
228 session_list
= l2tp_session_id_hash_2(pn
, session_id
);
231 hlist_for_each_entry_rcu(session
, session_list
, global_hlist
) {
232 if (session
->session_id
== session_id
) {
233 l2tp_session_inc_refcount(session
);
234 rcu_read_unlock_bh();
239 rcu_read_unlock_bh();
244 session_list
= l2tp_session_id_hash(tunnel
, session_id
);
245 read_lock_bh(&tunnel
->hlist_lock
);
246 hlist_for_each_entry(session
, session_list
, hlist
) {
247 if (session
->session_id
== session_id
) {
248 l2tp_session_inc_refcount(session
);
249 read_unlock_bh(&tunnel
->hlist_lock
);
254 read_unlock_bh(&tunnel
->hlist_lock
);
258 EXPORT_SYMBOL_GPL(l2tp_session_get
);
260 struct l2tp_session
*l2tp_session_get_nth(struct l2tp_tunnel
*tunnel
, int nth
)
263 struct l2tp_session
*session
;
266 read_lock_bh(&tunnel
->hlist_lock
);
267 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
268 hlist_for_each_entry(session
, &tunnel
->session_hlist
[hash
], hlist
) {
270 l2tp_session_inc_refcount(session
);
271 read_unlock_bh(&tunnel
->hlist_lock
);
277 read_unlock_bh(&tunnel
->hlist_lock
);
281 EXPORT_SYMBOL_GPL(l2tp_session_get_nth
);
283 /* Lookup a session by interface name.
284 * This is very inefficient but is only used by management interfaces.
286 struct l2tp_session
*l2tp_session_get_by_ifname(const struct net
*net
,
289 struct l2tp_net
*pn
= l2tp_pernet(net
);
291 struct l2tp_session
*session
;
294 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++) {
295 hlist_for_each_entry_rcu(session
, &pn
->l2tp_session_hlist
[hash
], global_hlist
) {
296 if (!strcmp(session
->ifname
, ifname
)) {
297 l2tp_session_inc_refcount(session
);
298 rcu_read_unlock_bh();
305 rcu_read_unlock_bh();
309 EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname
);
311 int l2tp_session_register(struct l2tp_session
*session
,
312 struct l2tp_tunnel
*tunnel
)
314 struct l2tp_session
*session_walk
;
315 struct hlist_head
*g_head
;
316 struct hlist_head
*head
;
320 head
= l2tp_session_id_hash(tunnel
, session
->session_id
);
322 write_lock_bh(&tunnel
->hlist_lock
);
323 if (!tunnel
->acpt_newsess
) {
328 hlist_for_each_entry(session_walk
, head
, hlist
)
329 if (session_walk
->session_id
== session
->session_id
) {
334 if (tunnel
->version
== L2TP_HDR_VER_3
) {
335 pn
= l2tp_pernet(tunnel
->l2tp_net
);
336 g_head
= l2tp_session_id_hash_2(l2tp_pernet(tunnel
->l2tp_net
),
337 session
->session_id
);
339 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
341 hlist_for_each_entry(session_walk
, g_head
, global_hlist
)
342 if (session_walk
->session_id
== session
->session_id
) {
344 goto err_tlock_pnlock
;
347 l2tp_tunnel_inc_refcount(tunnel
);
348 sock_hold(tunnel
->sock
);
349 hlist_add_head_rcu(&session
->global_hlist
, g_head
);
351 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
353 l2tp_tunnel_inc_refcount(tunnel
);
354 sock_hold(tunnel
->sock
);
357 hlist_add_head(&session
->hlist
, head
);
358 write_unlock_bh(&tunnel
->hlist_lock
);
363 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
365 write_unlock_bh(&tunnel
->hlist_lock
);
369 EXPORT_SYMBOL_GPL(l2tp_session_register
);
371 /* Lookup a tunnel by id
373 struct l2tp_tunnel
*l2tp_tunnel_find(const struct net
*net
, u32 tunnel_id
)
375 struct l2tp_tunnel
*tunnel
;
376 struct l2tp_net
*pn
= l2tp_pernet(net
);
379 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
380 if (tunnel
->tunnel_id
== tunnel_id
) {
381 rcu_read_unlock_bh();
385 rcu_read_unlock_bh();
389 EXPORT_SYMBOL_GPL(l2tp_tunnel_find
);
391 struct l2tp_tunnel
*l2tp_tunnel_find_nth(const struct net
*net
, int nth
)
393 struct l2tp_net
*pn
= l2tp_pernet(net
);
394 struct l2tp_tunnel
*tunnel
;
398 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
400 rcu_read_unlock_bh();
405 rcu_read_unlock_bh();
409 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth
);
411 /*****************************************************************************
412 * Receive data handling
413 *****************************************************************************/
415 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
418 static void l2tp_recv_queue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
420 struct sk_buff
*skbp
;
422 u32 ns
= L2TP_SKB_CB(skb
)->ns
;
424 spin_lock_bh(&session
->reorder_q
.lock
);
425 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
426 if (L2TP_SKB_CB(skbp
)->ns
> ns
) {
427 __skb_queue_before(&session
->reorder_q
, skbp
, skb
);
428 l2tp_dbg(session
, L2TP_MSG_SEQ
,
429 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
430 session
->name
, ns
, L2TP_SKB_CB(skbp
)->ns
,
431 skb_queue_len(&session
->reorder_q
));
432 atomic_long_inc(&session
->stats
.rx_oos_packets
);
437 __skb_queue_tail(&session
->reorder_q
, skb
);
440 spin_unlock_bh(&session
->reorder_q
.lock
);
443 /* Dequeue a single skb.
445 static void l2tp_recv_dequeue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
447 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
448 int length
= L2TP_SKB_CB(skb
)->length
;
450 /* We're about to requeue the skb, so return resources
451 * to its current owner (a socket receive buffer).
455 atomic_long_inc(&tunnel
->stats
.rx_packets
);
456 atomic_long_add(length
, &tunnel
->stats
.rx_bytes
);
457 atomic_long_inc(&session
->stats
.rx_packets
);
458 atomic_long_add(length
, &session
->stats
.rx_bytes
);
460 if (L2TP_SKB_CB(skb
)->has_seq
) {
463 session
->nr
&= session
->nr_max
;
465 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated nr to %hu\n",
466 session
->name
, session
->nr
);
469 /* call private receive handler */
470 if (session
->recv_skb
!= NULL
)
471 (*session
->recv_skb
)(session
, skb
, L2TP_SKB_CB(skb
)->length
);
476 /* Dequeue skbs from the session's reorder_q, subject to packet order.
477 * Skbs that have been in the queue for too long are simply discarded.
479 static void l2tp_recv_dequeue(struct l2tp_session
*session
)
484 /* If the pkt at the head of the queue has the nr that we
485 * expect to send up next, dequeue it and any other
486 * in-sequence packets behind it.
489 spin_lock_bh(&session
->reorder_q
.lock
);
490 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
491 if (time_after(jiffies
, L2TP_SKB_CB(skb
)->expires
)) {
492 atomic_long_inc(&session
->stats
.rx_seq_discards
);
493 atomic_long_inc(&session
->stats
.rx_errors
);
494 l2tp_dbg(session
, L2TP_MSG_SEQ
,
495 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
496 session
->name
, L2TP_SKB_CB(skb
)->ns
,
497 L2TP_SKB_CB(skb
)->length
, session
->nr
,
498 skb_queue_len(&session
->reorder_q
));
499 session
->reorder_skip
= 1;
500 __skb_unlink(skb
, &session
->reorder_q
);
505 if (L2TP_SKB_CB(skb
)->has_seq
) {
506 if (session
->reorder_skip
) {
507 l2tp_dbg(session
, L2TP_MSG_SEQ
,
508 "%s: advancing nr to next pkt: %u -> %u",
509 session
->name
, session
->nr
,
510 L2TP_SKB_CB(skb
)->ns
);
511 session
->reorder_skip
= 0;
512 session
->nr
= L2TP_SKB_CB(skb
)->ns
;
514 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
515 l2tp_dbg(session
, L2TP_MSG_SEQ
,
516 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
517 session
->name
, L2TP_SKB_CB(skb
)->ns
,
518 L2TP_SKB_CB(skb
)->length
, session
->nr
,
519 skb_queue_len(&session
->reorder_q
));
523 __skb_unlink(skb
, &session
->reorder_q
);
525 /* Process the skb. We release the queue lock while we
526 * do so to let other contexts process the queue.
528 spin_unlock_bh(&session
->reorder_q
.lock
);
529 l2tp_recv_dequeue_skb(session
, skb
);
534 spin_unlock_bh(&session
->reorder_q
.lock
);
537 static int l2tp_seq_check_rx_window(struct l2tp_session
*session
, u32 nr
)
541 if (nr
>= session
->nr
)
542 nws
= nr
- session
->nr
;
544 nws
= (session
->nr_max
+ 1) - (session
->nr
- nr
);
546 return nws
< session
->nr_window_size
;
549 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
550 * acceptable, else non-zero.
552 static int l2tp_recv_data_seq(struct l2tp_session
*session
, struct sk_buff
*skb
)
554 if (!l2tp_seq_check_rx_window(session
, L2TP_SKB_CB(skb
)->ns
)) {
555 /* Packet sequence number is outside allowed window.
558 l2tp_dbg(session
, L2TP_MSG_SEQ
,
559 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
560 session
->name
, L2TP_SKB_CB(skb
)->ns
,
561 L2TP_SKB_CB(skb
)->length
, session
->nr
);
565 if (session
->reorder_timeout
!= 0) {
566 /* Packet reordering enabled. Add skb to session's
567 * reorder queue, in order of ns.
569 l2tp_recv_queue_skb(session
, skb
);
573 /* Packet reordering disabled. Discard out-of-sequence packets, while
574 * tracking the number if in-sequence packets after the first OOS packet
575 * is seen. After nr_oos_count_max in-sequence packets, reset the
576 * sequence number to re-enable packet reception.
578 if (L2TP_SKB_CB(skb
)->ns
== session
->nr
) {
579 skb_queue_tail(&session
->reorder_q
, skb
);
581 u32 nr_oos
= L2TP_SKB_CB(skb
)->ns
;
582 u32 nr_next
= (session
->nr_oos
+ 1) & session
->nr_max
;
584 if (nr_oos
== nr_next
)
585 session
->nr_oos_count
++;
587 session
->nr_oos_count
= 0;
589 session
->nr_oos
= nr_oos
;
590 if (session
->nr_oos_count
> session
->nr_oos_count_max
) {
591 session
->reorder_skip
= 1;
592 l2tp_dbg(session
, L2TP_MSG_SEQ
,
593 "%s: %d oos packets received. Resetting sequence numbers\n",
594 session
->name
, session
->nr_oos_count
);
596 if (!session
->reorder_skip
) {
597 atomic_long_inc(&session
->stats
.rx_seq_discards
);
598 l2tp_dbg(session
, L2TP_MSG_SEQ
,
599 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
600 session
->name
, L2TP_SKB_CB(skb
)->ns
,
601 L2TP_SKB_CB(skb
)->length
, session
->nr
,
602 skb_queue_len(&session
->reorder_q
));
605 skb_queue_tail(&session
->reorder_q
, skb
);
615 /* Do receive processing of L2TP data frames. We handle both L2TPv2
616 * and L2TPv3 data frames here.
618 * L2TPv2 Data Message Header
621 * 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
622 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
624 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
625 * | Tunnel ID | Session ID |
626 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
627 * | Ns (opt) | Nr (opt) |
628 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
629 * | Offset Size (opt) | Offset pad... (opt)
630 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 * Data frames are marked by T=0. All other fields are the same as
633 * those in L2TP control frames.
635 * L2TPv3 Data Message Header
637 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
638 * | L2TP Session Header |
639 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
640 * | L2-Specific Sublayer |
641 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
642 * | Tunnel Payload ...
643 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
645 * L2TPv3 Session Header Over IP
648 * 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
649 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
651 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
652 * | Cookie (optional, maximum 64 bits)...
653 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657 * L2TPv3 L2-Specific Sublayer Format
660 * 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
661 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
662 * |x|S|x|x|x|x|x|x| Sequence Number |
663 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
665 * Cookie value and sublayer format are negotiated with the peer when
666 * the session is set up. Unlike L2TPv2, we do not need to parse the
667 * packet header to determine if optional fields are present.
669 * Caller must already have parsed the frame and determined that it is
670 * a data (not control) frame before coming here. Fields up to the
671 * session-id have already been parsed and ptr points to the data
672 * after the session-id.
674 void l2tp_recv_common(struct l2tp_session
*session
, struct sk_buff
*skb
,
675 unsigned char *ptr
, unsigned char *optr
, u16 hdrflags
,
676 int length
, int (*payload_hook
)(struct sk_buff
*skb
))
678 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
682 /* Parse and check optional cookie */
683 if (session
->peer_cookie_len
> 0) {
684 if (memcmp(ptr
, &session
->peer_cookie
[0], session
->peer_cookie_len
)) {
685 l2tp_info(tunnel
, L2TP_MSG_DATA
,
686 "%s: cookie mismatch (%u/%u). Discarding.\n",
687 tunnel
->name
, tunnel
->tunnel_id
,
688 session
->session_id
);
689 atomic_long_inc(&session
->stats
.rx_cookie_discards
);
692 ptr
+= session
->peer_cookie_len
;
695 /* Handle the optional sequence numbers. Sequence numbers are
696 * in different places for L2TPv2 and L2TPv3.
698 * If we are the LAC, enable/disable sequence numbers under
699 * the control of the LNS. If no sequence numbers present but
700 * we were expecting them, discard frame.
703 L2TP_SKB_CB(skb
)->has_seq
= 0;
704 if (tunnel
->version
== L2TP_HDR_VER_2
) {
705 if (hdrflags
& L2TP_HDRFLAG_S
) {
706 ns
= ntohs(*(__be16
*) ptr
);
708 nr
= ntohs(*(__be16
*) ptr
);
711 /* Store L2TP info in the skb */
712 L2TP_SKB_CB(skb
)->ns
= ns
;
713 L2TP_SKB_CB(skb
)->has_seq
= 1;
715 l2tp_dbg(session
, L2TP_MSG_SEQ
,
716 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
717 session
->name
, ns
, nr
, session
->nr
);
719 } else if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
720 u32 l2h
= ntohl(*(__be32
*) ptr
);
722 if (l2h
& 0x40000000) {
723 ns
= l2h
& 0x00ffffff;
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, session nr=%u\n",
731 session
->name
, ns
, session
->nr
);
736 if (L2TP_SKB_CB(skb
)->has_seq
) {
737 /* Received a packet with sequence numbers. If we're the LNS,
738 * check if we sre sending sequence numbers and if not,
741 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
742 l2tp_info(session
, L2TP_MSG_SEQ
,
743 "%s: requested to enable seq numbers by LNS\n",
745 session
->send_seq
= 1;
746 l2tp_session_set_header_len(session
, tunnel
->version
);
749 /* No sequence numbers.
750 * If user has configured mandatory sequence numbers, discard.
752 if (session
->recv_seq
) {
753 l2tp_warn(session
, L2TP_MSG_SEQ
,
754 "%s: recv data has no seq numbers when required. Discarding.\n",
756 atomic_long_inc(&session
->stats
.rx_seq_discards
);
760 /* If we're the LAC and we're sending sequence numbers, the
761 * LNS has requested that we no longer send sequence numbers.
762 * If we're the LNS and we're sending sequence numbers, the
763 * LAC is broken. Discard the frame.
765 if ((!session
->lns_mode
) && (session
->send_seq
)) {
766 l2tp_info(session
, L2TP_MSG_SEQ
,
767 "%s: requested to disable seq numbers by LNS\n",
769 session
->send_seq
= 0;
770 l2tp_session_set_header_len(session
, tunnel
->version
);
771 } else if (session
->send_seq
) {
772 l2tp_warn(session
, L2TP_MSG_SEQ
,
773 "%s: recv data has no seq numbers when required. Discarding.\n",
775 atomic_long_inc(&session
->stats
.rx_seq_discards
);
780 /* Session data offset is defined only for L2TPv2 and is
781 * indicated by an optional 16-bit value in the header.
783 if (tunnel
->version
== L2TP_HDR_VER_2
) {
784 /* If offset bit set, skip it. */
785 if (hdrflags
& L2TP_HDRFLAG_O
) {
786 offset
= ntohs(*(__be16
*)ptr
);
792 if (!pskb_may_pull(skb
, offset
))
795 __skb_pull(skb
, offset
);
797 /* If caller wants to process the payload before we queue the
801 if ((*payload_hook
)(skb
))
804 /* Prepare skb for adding to the session's reorder_q. Hold
805 * packets for max reorder_timeout or 1 second if not
808 L2TP_SKB_CB(skb
)->length
= length
;
809 L2TP_SKB_CB(skb
)->expires
= jiffies
+
810 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
812 /* Add packet to the session's receive queue. Reordering is done here, if
813 * enabled. Saved L2TP protocol info is stored in skb->sb[].
815 if (L2TP_SKB_CB(skb
)->has_seq
) {
816 if (l2tp_recv_data_seq(session
, skb
))
819 /* No sequence numbers. Add the skb to the tail of the
820 * reorder queue. This ensures that it will be
821 * delivered after all previous sequenced skbs.
823 skb_queue_tail(&session
->reorder_q
, skb
);
826 /* Try to dequeue as many skbs from reorder_q as we can. */
827 l2tp_recv_dequeue(session
);
832 atomic_long_inc(&session
->stats
.rx_errors
);
835 EXPORT_SYMBOL(l2tp_recv_common
);
837 /* Drop skbs from the session's reorder_q
839 int l2tp_session_queue_purge(struct l2tp_session
*session
)
841 struct sk_buff
*skb
= NULL
;
843 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
844 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
845 atomic_long_inc(&session
->stats
.rx_errors
);
850 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge
);
852 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
853 * here. The skb is not on a list when we get here.
854 * Returns 0 if the packet was a data packet and was successfully passed on.
855 * Returns 1 if the packet was not a good data packet and could not be
856 * forwarded. All such packets are passed up to userspace to deal with.
858 static int l2tp_udp_recv_core(struct l2tp_tunnel
*tunnel
, struct sk_buff
*skb
,
859 int (*payload_hook
)(struct sk_buff
*skb
))
861 struct l2tp_session
*session
= NULL
;
862 unsigned char *ptr
, *optr
;
864 u32 tunnel_id
, session_id
;
868 /* UDP has verifed checksum */
870 /* UDP always verifies the packet length. */
871 __skb_pull(skb
, sizeof(struct udphdr
));
874 if (!pskb_may_pull(skb
, L2TP_HDR_SIZE_SEQ
)) {
875 l2tp_info(tunnel
, L2TP_MSG_DATA
,
876 "%s: recv short packet (len=%d)\n",
877 tunnel
->name
, skb
->len
);
881 /* Trace packet contents, if enabled */
882 if (tunnel
->debug
& L2TP_MSG_DATA
) {
883 length
= min(32u, skb
->len
);
884 if (!pskb_may_pull(skb
, length
))
887 pr_debug("%s: recv\n", tunnel
->name
);
888 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, skb
->data
, length
);
891 /* Point to L2TP header */
892 optr
= ptr
= skb
->data
;
894 /* Get L2TP header flags */
895 hdrflags
= ntohs(*(__be16
*) ptr
);
897 /* Check protocol version */
898 version
= hdrflags
& L2TP_HDR_VER_MASK
;
899 if (version
!= tunnel
->version
) {
900 l2tp_info(tunnel
, L2TP_MSG_DATA
,
901 "%s: recv protocol version mismatch: got %d expected %d\n",
902 tunnel
->name
, version
, tunnel
->version
);
906 /* Get length of L2TP packet */
909 /* If type is control packet, it is handled by userspace. */
910 if (hdrflags
& L2TP_HDRFLAG_T
) {
911 l2tp_dbg(tunnel
, L2TP_MSG_DATA
,
912 "%s: recv control packet, len=%d\n",
913 tunnel
->name
, length
);
920 if (tunnel
->version
== L2TP_HDR_VER_2
) {
921 /* If length is present, skip it */
922 if (hdrflags
& L2TP_HDRFLAG_L
)
925 /* Extract tunnel and session ID */
926 tunnel_id
= ntohs(*(__be16
*) ptr
);
928 session_id
= ntohs(*(__be16
*) ptr
);
931 ptr
+= 2; /* skip reserved bits */
932 tunnel_id
= tunnel
->tunnel_id
;
933 session_id
= ntohl(*(__be32
*) ptr
);
937 /* Find the session context */
938 session
= l2tp_session_get(tunnel
->l2tp_net
, tunnel
, session_id
);
939 if (!session
|| !session
->recv_skb
) {
941 l2tp_session_dec_refcount(session
);
943 /* Not found? Pass to userspace to deal with */
944 l2tp_info(tunnel
, L2TP_MSG_DATA
,
945 "%s: no session found (%u/%u). Passing up.\n",
946 tunnel
->name
, tunnel_id
, session_id
);
950 l2tp_recv_common(session
, skb
, ptr
, optr
, hdrflags
, length
, payload_hook
);
951 l2tp_session_dec_refcount(session
);
956 /* Put UDP header back */
957 __skb_push(skb
, sizeof(struct udphdr
));
962 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
966 * >0: skb should be passed up to userspace as UDP.
968 int l2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
970 struct l2tp_tunnel
*tunnel
;
972 tunnel
= l2tp_sock_to_tunnel(sk
);
976 l2tp_dbg(tunnel
, L2TP_MSG_DATA
, "%s: received %d bytes\n",
977 tunnel
->name
, skb
->len
);
979 if (l2tp_udp_recv_core(tunnel
, skb
, tunnel
->recv_payload_hook
))
990 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv
);
992 /************************************************************************
994 ***********************************************************************/
996 /* Build an L2TP header for the session into the buffer provided.
998 static int l2tp_build_l2tpv2_header(struct l2tp_session
*session
, void *buf
)
1000 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1003 u16 flags
= L2TP_HDR_VER_2
;
1004 u32 tunnel_id
= tunnel
->peer_tunnel_id
;
1005 u32 session_id
= session
->peer_session_id
;
1007 if (session
->send_seq
)
1008 flags
|= L2TP_HDRFLAG_S
;
1010 /* Setup L2TP header. */
1011 *bufp
++ = htons(flags
);
1012 *bufp
++ = htons(tunnel_id
);
1013 *bufp
++ = htons(session_id
);
1014 if (session
->send_seq
) {
1015 *bufp
++ = htons(session
->ns
);
1018 session
->ns
&= 0xffff;
1019 l2tp_dbg(session
, L2TP_MSG_SEQ
, "%s: updated ns to %u\n",
1020 session
->name
, session
->ns
);
1026 static int l2tp_build_l2tpv3_header(struct l2tp_session
*session
, void *buf
)
1028 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1032 /* Setup L2TP header. The header differs slightly for UDP and
1033 * IP encapsulations. For UDP, there is 4 bytes of flags.
1035 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
1036 u16 flags
= L2TP_HDR_VER_3
;
1037 *((__be16
*) bufp
) = htons(flags
);
1039 *((__be16
*) bufp
) = 0;
1043 *((__be32
*) bufp
) = htonl(session
->peer_session_id
);
1045 if (session
->cookie_len
) {
1046 memcpy(bufp
, &session
->cookie
[0], session
->cookie_len
);
1047 bufp
+= session
->cookie_len
;
1049 if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
1052 if (session
->send_seq
) {
1053 l2h
= 0x40000000 | session
->ns
;
1055 session
->ns
&= 0xffffff;
1056 l2tp_dbg(session
, L2TP_MSG_SEQ
,
1057 "%s: updated ns to %u\n",
1058 session
->name
, session
->ns
);
1061 *((__be32
*)bufp
) = htonl(l2h
);
1068 static int l2tp_xmit_core(struct l2tp_session
*session
, struct sk_buff
*skb
,
1069 struct flowi
*fl
, size_t data_len
)
1071 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1072 unsigned int len
= skb
->len
;
1076 if (session
->send_seq
)
1077 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %zd bytes, ns=%u\n",
1078 session
->name
, data_len
, session
->ns
- 1);
1080 l2tp_dbg(session
, L2TP_MSG_DATA
, "%s: send %zd bytes\n",
1081 session
->name
, data_len
);
1083 if (session
->debug
& L2TP_MSG_DATA
) {
1084 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1085 unsigned char *datap
= skb
->data
+ uhlen
;
1087 pr_debug("%s: xmit\n", session
->name
);
1088 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1089 datap
, min_t(size_t, 32, len
- uhlen
));
1092 /* Queue the packet to IP for output */
1094 #if IS_ENABLED(CONFIG_IPV6)
1095 if (tunnel
->sock
->sk_family
== PF_INET6
&& !tunnel
->v4mapped
)
1096 error
= inet6_csk_xmit(tunnel
->sock
, skb
, NULL
);
1099 error
= ip_queue_xmit(tunnel
->sock
, skb
, fl
);
1103 atomic_long_inc(&tunnel
->stats
.tx_packets
);
1104 atomic_long_add(len
, &tunnel
->stats
.tx_bytes
);
1105 atomic_long_inc(&session
->stats
.tx_packets
);
1106 atomic_long_add(len
, &session
->stats
.tx_bytes
);
1108 atomic_long_inc(&tunnel
->stats
.tx_errors
);
1109 atomic_long_inc(&session
->stats
.tx_errors
);
1115 /* If caller requires the skb to have a ppp header, the header must be
1116 * inserted in the skb data before calling this function.
1118 int l2tp_xmit_skb(struct l2tp_session
*session
, struct sk_buff
*skb
, int hdr_len
)
1120 int data_len
= skb
->len
;
1121 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1122 struct sock
*sk
= tunnel
->sock
;
1125 struct inet_sock
*inet
;
1127 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1129 int ret
= NET_XMIT_SUCCESS
;
1131 /* Check that there's enough headroom in the skb to insert IP,
1132 * UDP and L2TP headers. If not enough, expand it to
1133 * make room. Adjust truesize.
1135 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1137 if (skb_cow_head(skb
, headroom
)) {
1139 return NET_XMIT_DROP
;
1142 /* Setup L2TP header */
1143 session
->build_header(session
, __skb_push(skb
, hdr_len
));
1145 /* Reset skb netfilter state */
1146 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1147 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1152 if (sock_owned_by_user(sk
)) {
1154 ret
= NET_XMIT_DROP
;
1158 /* Get routing info from the tunnel socket */
1160 skb_dst_set(skb
, dst_clone(__sk_dst_check(sk
, 0)));
1163 fl
= &inet
->cork
.fl
;
1164 switch (tunnel
->encap
) {
1165 case L2TP_ENCAPTYPE_UDP
:
1166 /* Setup UDP header */
1167 __skb_push(skb
, sizeof(*uh
));
1168 skb_reset_transport_header(skb
);
1170 uh
->source
= inet
->inet_sport
;
1171 uh
->dest
= inet
->inet_dport
;
1172 udp_len
= uhlen
+ hdr_len
+ data_len
;
1173 uh
->len
= htons(udp_len
);
1175 /* Calculate UDP checksum if configured to do so */
1176 #if IS_ENABLED(CONFIG_IPV6)
1177 if (sk
->sk_family
== PF_INET6
&& !tunnel
->v4mapped
)
1178 udp6_set_csum(udp_get_no_check6_tx(sk
),
1179 skb
, &inet6_sk(sk
)->saddr
,
1180 &sk
->sk_v6_daddr
, udp_len
);
1183 udp_set_csum(sk
->sk_no_check_tx
, skb
, inet
->inet_saddr
,
1184 inet
->inet_daddr
, udp_len
);
1187 case L2TP_ENCAPTYPE_IP
:
1191 l2tp_xmit_core(session
, skb
, fl
, data_len
);
1197 EXPORT_SYMBOL_GPL(l2tp_xmit_skb
);
1199 /*****************************************************************************
1200 * Tinnel and session create/destroy.
1201 *****************************************************************************/
1203 /* Tunnel socket destruct hook.
1204 * The tunnel context is deleted only when all session sockets have been
1207 static void l2tp_tunnel_destruct(struct sock
*sk
)
1209 struct l2tp_tunnel
*tunnel
= l2tp_tunnel(sk
);
1210 struct l2tp_net
*pn
;
1215 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing...\n", tunnel
->name
);
1218 /* Disable udp encapsulation */
1219 switch (tunnel
->encap
) {
1220 case L2TP_ENCAPTYPE_UDP
:
1221 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1222 (udp_sk(sk
))->encap_type
= 0;
1223 (udp_sk(sk
))->encap_rcv
= NULL
;
1224 (udp_sk(sk
))->encap_destroy
= NULL
;
1226 case L2TP_ENCAPTYPE_IP
:
1230 /* Remove hooks into tunnel socket */
1231 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1232 sk
->sk_user_data
= NULL
;
1234 /* Remove the tunnel struct from the tunnel list */
1235 pn
= l2tp_pernet(tunnel
->l2tp_net
);
1236 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1237 list_del_rcu(&tunnel
->list
);
1238 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1240 tunnel
->sock
= NULL
;
1241 l2tp_tunnel_dec_refcount(tunnel
);
1243 /* Call the original destructor */
1244 if (sk
->sk_destruct
)
1245 (*sk
->sk_destruct
)(sk
);
1250 /* When the tunnel is closed, all the attached sessions need to go too.
1252 void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
)
1255 struct hlist_node
*walk
;
1256 struct hlist_node
*tmp
;
1257 struct l2tp_session
*session
;
1259 BUG_ON(tunnel
== NULL
);
1261 l2tp_info(tunnel
, L2TP_MSG_CONTROL
, "%s: closing all sessions...\n",
1264 write_lock_bh(&tunnel
->hlist_lock
);
1265 tunnel
->acpt_newsess
= false;
1266 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
1268 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1269 session
= hlist_entry(walk
, struct l2tp_session
, hlist
);
1271 l2tp_info(session
, L2TP_MSG_CONTROL
,
1272 "%s: closing session\n", session
->name
);
1274 hlist_del_init(&session
->hlist
);
1276 if (test_and_set_bit(0, &session
->dead
))
1279 write_unlock_bh(&tunnel
->hlist_lock
);
1281 __l2tp_session_unhash(session
);
1282 l2tp_session_queue_purge(session
);
1284 if (session
->session_close
!= NULL
)
1285 (*session
->session_close
)(session
);
1287 l2tp_session_dec_refcount(session
);
1289 write_lock_bh(&tunnel
->hlist_lock
);
1291 /* Now restart from the beginning of this hash
1292 * chain. We always remove a session from the
1293 * list so we are guaranteed to make forward
1299 write_unlock_bh(&tunnel
->hlist_lock
);
1301 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall
);
1303 /* Tunnel socket destroy hook for UDP encapsulation */
1304 static void l2tp_udp_encap_destroy(struct sock
*sk
)
1306 struct l2tp_tunnel
*tunnel
= l2tp_sock_to_tunnel(sk
);
1308 l2tp_tunnel_closeall(tunnel
);
1313 /* Workqueue tunnel deletion function */
1314 static void l2tp_tunnel_del_work(struct work_struct
*work
)
1316 struct l2tp_tunnel
*tunnel
= NULL
;
1317 struct socket
*sock
= NULL
;
1318 struct sock
*sk
= NULL
;
1320 tunnel
= container_of(work
, struct l2tp_tunnel
, del_work
);
1322 l2tp_tunnel_closeall(tunnel
);
1324 sk
= l2tp_tunnel_sock_lookup(tunnel
);
1328 sock
= sk
->sk_socket
;
1330 /* If the tunnel socket was created by userspace, then go through the
1331 * inet layer to shut the socket down, and let userspace close it.
1332 * Otherwise, if we created the socket directly within the kernel, use
1333 * the sk API to release it here.
1334 * In either case the tunnel resources are freed in the socket
1335 * destructor when the tunnel socket goes away.
1337 if (tunnel
->fd
>= 0) {
1339 inet_shutdown(sock
, 2);
1342 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1347 l2tp_tunnel_sock_put(sk
);
1349 l2tp_tunnel_dec_refcount(tunnel
);
1352 /* Create a socket for the tunnel, if one isn't set up by
1353 * userspace. This is used for static tunnels where there is no
1354 * managing L2TP daemon.
1356 * Since we don't want these sockets to keep a namespace alive by
1357 * themselves, we drop the socket's namespace refcount after creation.
1358 * These sockets are freed when the namespace exits using the pernet
1361 static int l2tp_tunnel_sock_create(struct net
*net
,
1364 struct l2tp_tunnel_cfg
*cfg
,
1365 struct socket
**sockp
)
1368 struct socket
*sock
= NULL
;
1369 struct udp_port_cfg udp_conf
;
1371 switch (cfg
->encap
) {
1372 case L2TP_ENCAPTYPE_UDP
:
1373 memset(&udp_conf
, 0, sizeof(udp_conf
));
1375 #if IS_ENABLED(CONFIG_IPV6)
1376 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1377 udp_conf
.family
= AF_INET6
;
1378 memcpy(&udp_conf
.local_ip6
, cfg
->local_ip6
,
1379 sizeof(udp_conf
.local_ip6
));
1380 memcpy(&udp_conf
.peer_ip6
, cfg
->peer_ip6
,
1381 sizeof(udp_conf
.peer_ip6
));
1382 udp_conf
.use_udp6_tx_checksums
=
1383 ! cfg
->udp6_zero_tx_checksums
;
1384 udp_conf
.use_udp6_rx_checksums
=
1385 ! cfg
->udp6_zero_rx_checksums
;
1389 udp_conf
.family
= AF_INET
;
1390 udp_conf
.local_ip
= cfg
->local_ip
;
1391 udp_conf
.peer_ip
= cfg
->peer_ip
;
1392 udp_conf
.use_udp_checksums
= cfg
->use_udp_checksums
;
1395 udp_conf
.local_udp_port
= htons(cfg
->local_udp_port
);
1396 udp_conf
.peer_udp_port
= htons(cfg
->peer_udp_port
);
1398 err
= udp_sock_create(net
, &udp_conf
, &sock
);
1404 case L2TP_ENCAPTYPE_IP
:
1405 #if IS_ENABLED(CONFIG_IPV6)
1406 if (cfg
->local_ip6
&& cfg
->peer_ip6
) {
1407 struct sockaddr_l2tpip6 ip6_addr
= {0};
1409 err
= sock_create_kern(net
, AF_INET6
, SOCK_DGRAM
,
1410 IPPROTO_L2TP
, &sock
);
1414 ip6_addr
.l2tp_family
= AF_INET6
;
1415 memcpy(&ip6_addr
.l2tp_addr
, cfg
->local_ip6
,
1416 sizeof(ip6_addr
.l2tp_addr
));
1417 ip6_addr
.l2tp_conn_id
= tunnel_id
;
1418 err
= kernel_bind(sock
, (struct sockaddr
*) &ip6_addr
,
1423 ip6_addr
.l2tp_family
= AF_INET6
;
1424 memcpy(&ip6_addr
.l2tp_addr
, cfg
->peer_ip6
,
1425 sizeof(ip6_addr
.l2tp_addr
));
1426 ip6_addr
.l2tp_conn_id
= peer_tunnel_id
;
1427 err
= kernel_connect(sock
,
1428 (struct sockaddr
*) &ip6_addr
,
1429 sizeof(ip6_addr
), 0);
1435 struct sockaddr_l2tpip ip_addr
= {0};
1437 err
= sock_create_kern(net
, AF_INET
, SOCK_DGRAM
,
1438 IPPROTO_L2TP
, &sock
);
1442 ip_addr
.l2tp_family
= AF_INET
;
1443 ip_addr
.l2tp_addr
= cfg
->local_ip
;
1444 ip_addr
.l2tp_conn_id
= tunnel_id
;
1445 err
= kernel_bind(sock
, (struct sockaddr
*) &ip_addr
,
1450 ip_addr
.l2tp_family
= AF_INET
;
1451 ip_addr
.l2tp_addr
= cfg
->peer_ip
;
1452 ip_addr
.l2tp_conn_id
= peer_tunnel_id
;
1453 err
= kernel_connect(sock
, (struct sockaddr
*) &ip_addr
,
1454 sizeof(ip_addr
), 0);
1466 if ((err
< 0) && sock
) {
1467 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1475 static struct lock_class_key l2tp_socket_class
;
1477 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
)
1479 struct l2tp_tunnel
*tunnel
= NULL
;
1481 struct socket
*sock
= NULL
;
1482 struct sock
*sk
= NULL
;
1483 struct l2tp_net
*pn
;
1484 enum l2tp_encap_type encap
= L2TP_ENCAPTYPE_UDP
;
1486 /* Get the tunnel socket from the fd, which was opened by
1487 * the userspace L2TP daemon. If not specified, create a
1491 err
= l2tp_tunnel_sock_create(net
, tunnel_id
, peer_tunnel_id
,
1496 sock
= sockfd_lookup(fd
, &err
);
1498 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1499 tunnel_id
, fd
, err
);
1504 /* Reject namespace mismatches */
1505 if (!net_eq(sock_net(sock
->sk
), net
)) {
1506 pr_err("tunl %u: netns mismatch\n", tunnel_id
);
1517 /* Quick sanity checks */
1519 case L2TP_ENCAPTYPE_UDP
:
1520 err
= -EPROTONOSUPPORT
;
1521 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1522 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1523 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1527 case L2TP_ENCAPTYPE_IP
:
1528 err
= -EPROTONOSUPPORT
;
1529 if (sk
->sk_protocol
!= IPPROTO_L2TP
) {
1530 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1531 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_L2TP
);
1537 /* Check if this socket has already been prepped */
1538 tunnel
= l2tp_tunnel(sk
);
1539 if (tunnel
!= NULL
) {
1540 /* This socket has already been prepped */
1545 tunnel
= kzalloc(sizeof(struct l2tp_tunnel
), GFP_KERNEL
);
1546 if (tunnel
== NULL
) {
1551 tunnel
->version
= version
;
1552 tunnel
->tunnel_id
= tunnel_id
;
1553 tunnel
->peer_tunnel_id
= peer_tunnel_id
;
1554 tunnel
->debug
= L2TP_DEFAULT_DEBUG_FLAGS
;
1556 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1557 sprintf(&tunnel
->name
[0], "tunl %u", tunnel_id
);
1558 rwlock_init(&tunnel
->hlist_lock
);
1559 tunnel
->acpt_newsess
= true;
1561 /* The net we belong to */
1562 tunnel
->l2tp_net
= net
;
1563 pn
= l2tp_pernet(net
);
1566 tunnel
->debug
= cfg
->debug
;
1568 #if IS_ENABLED(CONFIG_IPV6)
1569 if (sk
->sk_family
== PF_INET6
) {
1570 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1572 if (ipv6_addr_v4mapped(&np
->saddr
) &&
1573 ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
1574 struct inet_sock
*inet
= inet_sk(sk
);
1576 tunnel
->v4mapped
= true;
1577 inet
->inet_saddr
= np
->saddr
.s6_addr32
[3];
1578 inet
->inet_rcv_saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
[3];
1579 inet
->inet_daddr
= sk
->sk_v6_daddr
.s6_addr32
[3];
1581 tunnel
->v4mapped
= false;
1586 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1587 tunnel
->encap
= encap
;
1588 if (encap
== L2TP_ENCAPTYPE_UDP
) {
1589 struct udp_tunnel_sock_cfg udp_cfg
= { };
1591 udp_cfg
.sk_user_data
= tunnel
;
1592 udp_cfg
.encap_type
= UDP_ENCAP_L2TPINUDP
;
1593 udp_cfg
.encap_rcv
= l2tp_udp_encap_recv
;
1594 udp_cfg
.encap_destroy
= l2tp_udp_encap_destroy
;
1596 setup_udp_tunnel_sock(net
, sock
, &udp_cfg
);
1598 sk
->sk_user_data
= tunnel
;
1601 /* Hook on the tunnel socket destructor so that we can cleanup
1602 * if the tunnel socket goes away.
1604 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1605 sk
->sk_destruct
= &l2tp_tunnel_destruct
;
1608 lockdep_set_class_and_name(&sk
->sk_lock
.slock
, &l2tp_socket_class
, "l2tp_sock");
1610 sk
->sk_allocation
= GFP_ATOMIC
;
1612 /* Init delete workqueue struct */
1613 INIT_WORK(&tunnel
->del_work
, l2tp_tunnel_del_work
);
1615 /* Add tunnel to our list */
1616 INIT_LIST_HEAD(&tunnel
->list
);
1618 /* Bump the reference count. The tunnel context is deleted
1619 * only when this drops to zero. Must be done before list insertion
1621 refcount_set(&tunnel
->ref_count
, 1);
1622 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1623 list_add_rcu(&tunnel
->list
, &pn
->l2tp_tunnel_list
);
1624 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1631 /* If tunnel's socket was created by the kernel, it doesn't
1634 if (sock
&& sock
->file
)
1639 EXPORT_SYMBOL_GPL(l2tp_tunnel_create
);
1641 /* This function is used by the netlink TUNNEL_DELETE command.
1643 void l2tp_tunnel_delete(struct l2tp_tunnel
*tunnel
)
1645 if (!test_and_set_bit(0, &tunnel
->dead
)) {
1646 l2tp_tunnel_inc_refcount(tunnel
);
1647 queue_work(l2tp_wq
, &tunnel
->del_work
);
1650 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete
);
1652 /* Really kill the session.
1654 void l2tp_session_free(struct l2tp_session
*session
)
1656 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1658 BUG_ON(refcount_read(&session
->ref_count
) != 0);
1661 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1662 sock_put(tunnel
->sock
);
1663 session
->tunnel
= NULL
;
1664 l2tp_tunnel_dec_refcount(tunnel
);
1669 EXPORT_SYMBOL_GPL(l2tp_session_free
);
1671 /* Remove an l2tp session from l2tp_core's hash lists.
1672 * Provides a tidyup interface for pseudowire code which can't just route all
1673 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1676 void __l2tp_session_unhash(struct l2tp_session
*session
)
1678 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1680 /* Remove the session from core hashes */
1682 /* Remove from the per-tunnel hash */
1683 write_lock_bh(&tunnel
->hlist_lock
);
1684 hlist_del_init(&session
->hlist
);
1685 write_unlock_bh(&tunnel
->hlist_lock
);
1687 /* For L2TPv3 we have a per-net hash: remove from there, too */
1688 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1689 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1690 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1691 hlist_del_init_rcu(&session
->global_hlist
);
1692 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1697 EXPORT_SYMBOL_GPL(__l2tp_session_unhash
);
1699 /* This function is used by the netlink SESSION_DELETE command and by
1702 int l2tp_session_delete(struct l2tp_session
*session
)
1704 if (test_and_set_bit(0, &session
->dead
))
1707 __l2tp_session_unhash(session
);
1708 l2tp_session_queue_purge(session
);
1709 if (session
->session_close
!= NULL
)
1710 (*session
->session_close
)(session
);
1712 l2tp_session_dec_refcount(session
);
1716 EXPORT_SYMBOL_GPL(l2tp_session_delete
);
1718 /* We come here whenever a session's send_seq, cookie_len or
1719 * l2specific_type parameters are set.
1721 void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
)
1723 if (version
== L2TP_HDR_VER_2
) {
1724 session
->hdr_len
= 6;
1725 if (session
->send_seq
)
1726 session
->hdr_len
+= 4;
1728 session
->hdr_len
= 4 + session
->cookie_len
;
1729 session
->hdr_len
+= l2tp_get_l2specific_len(session
);
1730 if (session
->tunnel
->encap
== L2TP_ENCAPTYPE_UDP
)
1731 session
->hdr_len
+= 4;
1735 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len
);
1737 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
)
1739 struct l2tp_session
*session
;
1741 session
= kzalloc(sizeof(struct l2tp_session
) + priv_size
, GFP_KERNEL
);
1742 if (session
!= NULL
) {
1743 session
->magic
= L2TP_SESSION_MAGIC
;
1744 session
->tunnel
= tunnel
;
1746 session
->session_id
= session_id
;
1747 session
->peer_session_id
= peer_session_id
;
1749 if (tunnel
->version
== L2TP_HDR_VER_2
)
1750 session
->nr_max
= 0xffff;
1752 session
->nr_max
= 0xffffff;
1753 session
->nr_window_size
= session
->nr_max
/ 2;
1754 session
->nr_oos_count_max
= 4;
1756 /* Use NR of first received packet */
1757 session
->reorder_skip
= 1;
1759 sprintf(&session
->name
[0], "sess %u/%u",
1760 tunnel
->tunnel_id
, session
->session_id
);
1762 skb_queue_head_init(&session
->reorder_q
);
1764 INIT_HLIST_NODE(&session
->hlist
);
1765 INIT_HLIST_NODE(&session
->global_hlist
);
1767 /* Inherit debug options from tunnel */
1768 session
->debug
= tunnel
->debug
;
1771 session
->pwtype
= cfg
->pw_type
;
1772 session
->debug
= cfg
->debug
;
1773 session
->mtu
= cfg
->mtu
;
1774 session
->mru
= cfg
->mru
;
1775 session
->send_seq
= cfg
->send_seq
;
1776 session
->recv_seq
= cfg
->recv_seq
;
1777 session
->lns_mode
= cfg
->lns_mode
;
1778 session
->reorder_timeout
= cfg
->reorder_timeout
;
1779 session
->l2specific_type
= cfg
->l2specific_type
;
1780 session
->cookie_len
= cfg
->cookie_len
;
1781 memcpy(&session
->cookie
[0], &cfg
->cookie
[0], cfg
->cookie_len
);
1782 session
->peer_cookie_len
= cfg
->peer_cookie_len
;
1783 memcpy(&session
->peer_cookie
[0], &cfg
->peer_cookie
[0], cfg
->peer_cookie_len
);
1786 if (tunnel
->version
== L2TP_HDR_VER_2
)
1787 session
->build_header
= l2tp_build_l2tpv2_header
;
1789 session
->build_header
= l2tp_build_l2tpv3_header
;
1791 l2tp_session_set_header_len(session
, tunnel
->version
);
1793 refcount_set(&session
->ref_count
, 1);
1798 return ERR_PTR(-ENOMEM
);
1800 EXPORT_SYMBOL_GPL(l2tp_session_create
);
1802 /*****************************************************************************
1804 *****************************************************************************/
1806 static __net_init
int l2tp_init_net(struct net
*net
)
1808 struct l2tp_net
*pn
= net_generic(net
, l2tp_net_id
);
1811 INIT_LIST_HEAD(&pn
->l2tp_tunnel_list
);
1812 spin_lock_init(&pn
->l2tp_tunnel_list_lock
);
1814 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++)
1815 INIT_HLIST_HEAD(&pn
->l2tp_session_hlist
[hash
]);
1817 spin_lock_init(&pn
->l2tp_session_hlist_lock
);
1822 static __net_exit
void l2tp_exit_net(struct net
*net
)
1824 struct l2tp_net
*pn
= l2tp_pernet(net
);
1825 struct l2tp_tunnel
*tunnel
= NULL
;
1829 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
1830 l2tp_tunnel_delete(tunnel
);
1832 rcu_read_unlock_bh();
1834 flush_workqueue(l2tp_wq
);
1837 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++)
1838 WARN_ON_ONCE(!hlist_empty(&pn
->l2tp_session_hlist
[hash
]));
1841 static struct pernet_operations l2tp_net_ops
= {
1842 .init
= l2tp_init_net
,
1843 .exit
= l2tp_exit_net
,
1845 .size
= sizeof(struct l2tp_net
),
1848 static int __init
l2tp_init(void)
1852 rc
= register_pernet_device(&l2tp_net_ops
);
1856 l2tp_wq
= alloc_workqueue("l2tp", WQ_UNBOUND
, 0);
1858 pr_err("alloc_workqueue failed\n");
1859 unregister_pernet_device(&l2tp_net_ops
);
1864 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION
);
1870 static void __exit
l2tp_exit(void)
1872 unregister_pernet_device(&l2tp_net_ops
);
1874 destroy_workqueue(l2tp_wq
);
1879 module_init(l2tp_init
);
1880 module_exit(l2tp_exit
);
1882 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1883 MODULE_DESCRIPTION("L2TP core");
1884 MODULE_LICENSE("GPL");
1885 MODULE_VERSION(L2TP_DRV_VERSION
);