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_tunnel *l2tp_tunnel(struct sock *sk)
120 return sk->sk_user_data;
123 static inline struct l2tp_net *l2tp_pernet(struct net *net)
127 return net_generic(net, l2tp_net_id);
130 /* Tunnel reference counts. Incremented per session that is added to
133 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
135 atomic_inc(&tunnel->ref_count);
138 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
140 if (atomic_dec_and_test(&tunnel->ref_count))
141 l2tp_tunnel_free(tunnel);
143 #ifdef L2TP_REFCNT_DEBUG
144 #define l2tp_tunnel_inc_refcount(_t) \
146 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
147 __func__, __LINE__, (_t)->name, \
148 atomic_read(&_t->ref_count)); \
149 l2tp_tunnel_inc_refcount_1(_t); \
151 #define l2tp_tunnel_dec_refcount(_t)
153 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
154 __func__, __LINE__, (_t)->name, \
155 atomic_read(&_t->ref_count)); \
156 l2tp_tunnel_dec_refcount_1(_t); \
159 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
160 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
163 /* Session hash global list for L2TPv3.
164 * The session_id SHOULD be random according to RFC3931, but several
165 * L2TP implementations use incrementing session_ids. So we do a real
166 * hash on the session_id, rather than a simple bitmask.
168 static inline struct hlist_head *
169 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
171 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
175 /* Lookup the tunnel socket, possibly involving the fs code if the socket is
176 * owned by userspace. A struct sock returned from this function must be
177 * released using l2tp_tunnel_sock_put once you're done with it.
179 struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
182 struct socket *sock = NULL;
183 struct sock *sk = NULL;
188 if (tunnel->fd >= 0) {
189 /* Socket is owned by userspace, who might be in the process
190 * of closing it. Look the socket up using the fd to ensure
193 sock = sockfd_lookup(tunnel->fd, &err);
197 /* Socket is owned by kernelspace */
205 EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup);
207 /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
208 void l2tp_tunnel_sock_put(struct sock *sk)
210 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
212 if (tunnel->fd >= 0) {
213 /* Socket is owned by userspace */
214 sockfd_put(sk->sk_socket);
220 EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put);
222 /* Lookup a session by id in the global session list
224 static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
226 struct l2tp_net *pn = l2tp_pernet(net);
227 struct hlist_head *session_list =
228 l2tp_session_id_hash_2(pn, session_id);
229 struct l2tp_session *session;
232 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
233 if (session->session_id == session_id) {
234 rcu_read_unlock_bh();
238 rcu_read_unlock_bh();
243 /* Session hash list.
244 * The session_id SHOULD be random according to RFC2661, but several
245 * L2TP implementations (Cisco and Microsoft) use incrementing
246 * session_ids. So we do a real hash on the session_id, rather than a
249 static inline struct hlist_head *
250 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
252 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
255 /* Lookup a session by id
257 struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
259 struct hlist_head *session_list;
260 struct l2tp_session *session;
262 /* In L2TPv3, session_ids are unique over all tunnels and we
263 * sometimes need to look them up before we know the
267 return l2tp_session_find_2(net, session_id);
269 session_list = l2tp_session_id_hash(tunnel, session_id);
270 read_lock_bh(&tunnel->hlist_lock);
271 hlist_for_each_entry(session, session_list, hlist) {
272 if (session->session_id == session_id) {
273 read_unlock_bh(&tunnel->hlist_lock);
277 read_unlock_bh(&tunnel->hlist_lock);
281 EXPORT_SYMBOL_GPL(l2tp_session_find);
283 struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
286 struct l2tp_session *session;
289 read_lock_bh(&tunnel->hlist_lock);
290 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
291 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
293 read_unlock_bh(&tunnel->hlist_lock);
299 read_unlock_bh(&tunnel->hlist_lock);
303 EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
305 /* Lookup a session by interface name.
306 * This is very inefficient but is only used by management interfaces.
308 struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
310 struct l2tp_net *pn = l2tp_pernet(net);
312 struct l2tp_session *session;
315 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
316 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
317 if (!strcmp(session->ifname, ifname)) {
318 rcu_read_unlock_bh();
324 rcu_read_unlock_bh();
328 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
330 /* Lookup a tunnel by id
332 struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
334 struct l2tp_tunnel *tunnel;
335 struct l2tp_net *pn = l2tp_pernet(net);
338 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
339 if (tunnel->tunnel_id == tunnel_id) {
340 rcu_read_unlock_bh();
344 rcu_read_unlock_bh();
348 EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
350 struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
352 struct l2tp_net *pn = l2tp_pernet(net);
353 struct l2tp_tunnel *tunnel;
357 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
359 rcu_read_unlock_bh();
364 rcu_read_unlock_bh();
368 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
370 /*****************************************************************************
371 * Receive data handling
372 *****************************************************************************/
374 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
377 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
379 struct sk_buff *skbp;
381 u32 ns = L2TP_SKB_CB(skb)->ns;
383 spin_lock_bh(&session->reorder_q.lock);
384 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
385 if (L2TP_SKB_CB(skbp)->ns > ns) {
386 __skb_queue_before(&session->reorder_q, skbp, skb);
387 l2tp_dbg(session, L2TP_MSG_SEQ,
388 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
389 session->name, ns, L2TP_SKB_CB(skbp)->ns,
390 skb_queue_len(&session->reorder_q));
391 atomic_long_inc(&session->stats.rx_oos_packets);
396 __skb_queue_tail(&session->reorder_q, skb);
399 spin_unlock_bh(&session->reorder_q.lock);
402 /* Dequeue a single skb.
404 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
406 struct l2tp_tunnel *tunnel = session->tunnel;
407 int length = L2TP_SKB_CB(skb)->length;
409 /* We're about to requeue the skb, so return resources
410 * to its current owner (a socket receive buffer).
414 atomic_long_inc(&tunnel->stats.rx_packets);
415 atomic_long_add(length, &tunnel->stats.rx_bytes);
416 atomic_long_inc(&session->stats.rx_packets);
417 atomic_long_add(length, &session->stats.rx_bytes);
419 if (L2TP_SKB_CB(skb)->has_seq) {
422 if (tunnel->version == L2TP_HDR_VER_2)
423 session->nr &= 0xffff;
425 session->nr &= 0xffffff;
427 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
428 session->name, session->nr);
431 /* call private receive handler */
432 if (session->recv_skb != NULL)
433 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
438 (*session->deref)(session);
441 /* Dequeue skbs from the session's reorder_q, subject to packet order.
442 * Skbs that have been in the queue for too long are simply discarded.
444 static void l2tp_recv_dequeue(struct l2tp_session *session)
449 /* If the pkt at the head of the queue has the nr that we
450 * expect to send up next, dequeue it and any other
451 * in-sequence packets behind it.
454 spin_lock_bh(&session->reorder_q.lock);
455 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
456 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
457 atomic_long_inc(&session->stats.rx_seq_discards);
458 atomic_long_inc(&session->stats.rx_errors);
459 l2tp_dbg(session, L2TP_MSG_SEQ,
460 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
461 session->name, L2TP_SKB_CB(skb)->ns,
462 L2TP_SKB_CB(skb)->length, session->nr,
463 skb_queue_len(&session->reorder_q));
464 session->reorder_skip = 1;
465 __skb_unlink(skb, &session->reorder_q);
468 (*session->deref)(session);
472 if (L2TP_SKB_CB(skb)->has_seq) {
473 if (session->reorder_skip) {
474 l2tp_dbg(session, L2TP_MSG_SEQ,
475 "%s: advancing nr to next pkt: %u -> %u",
476 session->name, session->nr,
477 L2TP_SKB_CB(skb)->ns);
478 session->reorder_skip = 0;
479 session->nr = L2TP_SKB_CB(skb)->ns;
481 if (L2TP_SKB_CB(skb)->ns != session->nr) {
482 l2tp_dbg(session, L2TP_MSG_SEQ,
483 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
484 session->name, L2TP_SKB_CB(skb)->ns,
485 L2TP_SKB_CB(skb)->length, session->nr,
486 skb_queue_len(&session->reorder_q));
490 __skb_unlink(skb, &session->reorder_q);
492 /* Process the skb. We release the queue lock while we
493 * do so to let other contexts process the queue.
495 spin_unlock_bh(&session->reorder_q.lock);
496 l2tp_recv_dequeue_skb(session, skb);
501 spin_unlock_bh(&session->reorder_q.lock);
504 static inline int l2tp_verify_udp_checksum(struct sock *sk,
507 struct udphdr *uh = udp_hdr(skb);
508 u16 ulen = ntohs(uh->len);
511 if (sk->sk_no_check || skb_csum_unnecessary(skb))
514 #if IS_ENABLED(CONFIG_IPV6)
515 if (sk->sk_family == PF_INET6 && !l2tp_tunnel(sk)->v4mapped) {
517 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
520 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
521 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
522 &ipv6_hdr(skb)->daddr, ulen,
523 IPPROTO_UDP, skb->csum)) {
524 skb->ip_summed = CHECKSUM_UNNECESSARY;
527 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
528 &ipv6_hdr(skb)->daddr,
529 skb->len, IPPROTO_UDP,
534 struct inet_sock *inet;
538 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
539 ulen, IPPROTO_UDP, 0);
541 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
542 !csum_fold(csum_add(psum, skb->csum)))
547 return __skb_checksum_complete(skb);
550 /* Do receive processing of L2TP data frames. We handle both L2TPv2
551 * and L2TPv3 data frames here.
553 * L2TPv2 Data Message Header
556 * 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
557 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
558 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
559 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
560 * | Tunnel ID | Session ID |
561 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
562 * | Ns (opt) | Nr (opt) |
563 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
564 * | Offset Size (opt) | Offset pad... (opt)
565 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
567 * Data frames are marked by T=0. All other fields are the same as
568 * those in L2TP control frames.
570 * L2TPv3 Data Message Header
572 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
573 * | L2TP Session Header |
574 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
575 * | L2-Specific Sublayer |
576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
577 * | Tunnel Payload ...
578 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
580 * L2TPv3 Session Header Over IP
583 * 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
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
587 * | Cookie (optional, maximum 64 bits)...
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 * L2TPv3 L2-Specific Sublayer Format
595 * 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
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * |x|S|x|x|x|x|x|x| Sequence Number |
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * Cookie value, sublayer format and offset (pad) are negotiated with
601 * the peer when the session is set up. Unlike L2TPv2, we do not need
602 * to parse the packet header to determine if optional fields are
605 * Caller must already have parsed the frame and determined that it is
606 * a data (not control) frame before coming here. Fields up to the
607 * session-id have already been parsed and ptr points to the data
608 * after the session-id.
610 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
611 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
612 int length, int (*payload_hook)(struct sk_buff *skb))
614 struct l2tp_tunnel *tunnel = session->tunnel;
618 /* The ref count is increased since we now hold a pointer to
619 * the session. Take care to decrement the refcnt when exiting
620 * this function from now on...
622 l2tp_session_inc_refcount(session);
624 (*session->ref)(session);
626 /* Parse and check optional cookie */
627 if (session->peer_cookie_len > 0) {
628 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
629 l2tp_info(tunnel, L2TP_MSG_DATA,
630 "%s: cookie mismatch (%u/%u). Discarding.\n",
631 tunnel->name, tunnel->tunnel_id,
632 session->session_id);
633 atomic_long_inc(&session->stats.rx_cookie_discards);
636 ptr += session->peer_cookie_len;
639 /* Handle the optional sequence numbers. Sequence numbers are
640 * in different places for L2TPv2 and L2TPv3.
642 * If we are the LAC, enable/disable sequence numbers under
643 * the control of the LNS. If no sequence numbers present but
644 * we were expecting them, discard frame.
647 L2TP_SKB_CB(skb)->has_seq = 0;
648 if (tunnel->version == L2TP_HDR_VER_2) {
649 if (hdrflags & L2TP_HDRFLAG_S) {
650 ns = ntohs(*(__be16 *) ptr);
652 nr = ntohs(*(__be16 *) ptr);
655 /* Store L2TP info in the skb */
656 L2TP_SKB_CB(skb)->ns = ns;
657 L2TP_SKB_CB(skb)->has_seq = 1;
659 l2tp_dbg(session, L2TP_MSG_SEQ,
660 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
661 session->name, ns, nr, session->nr);
663 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
664 u32 l2h = ntohl(*(__be32 *) ptr);
666 if (l2h & 0x40000000) {
667 ns = l2h & 0x00ffffff;
669 /* Store L2TP info in the skb */
670 L2TP_SKB_CB(skb)->ns = ns;
671 L2TP_SKB_CB(skb)->has_seq = 1;
673 l2tp_dbg(session, L2TP_MSG_SEQ,
674 "%s: recv data ns=%u, session nr=%u\n",
675 session->name, ns, session->nr);
679 /* Advance past L2-specific header, if present */
680 ptr += session->l2specific_len;
682 if (L2TP_SKB_CB(skb)->has_seq) {
683 /* Received a packet with sequence numbers. If we're the LNS,
684 * check if we sre sending sequence numbers and if not,
687 if ((!session->lns_mode) && (!session->send_seq)) {
688 l2tp_info(session, L2TP_MSG_SEQ,
689 "%s: requested to enable seq numbers by LNS\n",
691 session->send_seq = -1;
692 l2tp_session_set_header_len(session, tunnel->version);
695 /* No sequence numbers.
696 * If user has configured mandatory sequence numbers, discard.
698 if (session->recv_seq) {
699 l2tp_warn(session, L2TP_MSG_SEQ,
700 "%s: recv data has no seq numbers when required. Discarding.\n",
702 atomic_long_inc(&session->stats.rx_seq_discards);
706 /* If we're the LAC and we're sending sequence numbers, the
707 * LNS has requested that we no longer send sequence numbers.
708 * If we're the LNS and we're sending sequence numbers, the
709 * LAC is broken. Discard the frame.
711 if ((!session->lns_mode) && (session->send_seq)) {
712 l2tp_info(session, L2TP_MSG_SEQ,
713 "%s: requested to disable seq numbers by LNS\n",
715 session->send_seq = 0;
716 l2tp_session_set_header_len(session, tunnel->version);
717 } else if (session->send_seq) {
718 l2tp_warn(session, L2TP_MSG_SEQ,
719 "%s: recv data has no seq numbers when required. Discarding.\n",
721 atomic_long_inc(&session->stats.rx_seq_discards);
726 /* Session data offset is handled differently for L2TPv2 and
727 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
728 * the header. For L2TPv3, the offset is negotiated using AVPs
729 * in the session setup control protocol.
731 if (tunnel->version == L2TP_HDR_VER_2) {
732 /* If offset bit set, skip it. */
733 if (hdrflags & L2TP_HDRFLAG_O) {
734 offset = ntohs(*(__be16 *)ptr);
738 ptr += session->offset;
741 if (!pskb_may_pull(skb, offset))
744 __skb_pull(skb, offset);
746 /* If caller wants to process the payload before we queue the
750 if ((*payload_hook)(skb))
753 /* Prepare skb for adding to the session's reorder_q. Hold
754 * packets for max reorder_timeout or 1 second if not
757 L2TP_SKB_CB(skb)->length = length;
758 L2TP_SKB_CB(skb)->expires = jiffies +
759 (session->reorder_timeout ? session->reorder_timeout : HZ);
761 /* Add packet to the session's receive queue. Reordering is done here, if
762 * enabled. Saved L2TP protocol info is stored in skb->sb[].
764 if (L2TP_SKB_CB(skb)->has_seq) {
765 if (session->reorder_timeout != 0) {
766 /* Packet reordering enabled. Add skb to session's
767 * reorder queue, in order of ns.
769 l2tp_recv_queue_skb(session, skb);
771 /* Packet reordering disabled. Discard out-of-sequence
774 if (L2TP_SKB_CB(skb)->ns != session->nr) {
775 atomic_long_inc(&session->stats.rx_seq_discards);
776 l2tp_dbg(session, L2TP_MSG_SEQ,
777 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
778 session->name, L2TP_SKB_CB(skb)->ns,
779 L2TP_SKB_CB(skb)->length, session->nr,
780 skb_queue_len(&session->reorder_q));
783 skb_queue_tail(&session->reorder_q, skb);
786 /* No sequence numbers. Add the skb to the tail of the
787 * reorder queue. This ensures that it will be
788 * delivered after all previous sequenced skbs.
790 skb_queue_tail(&session->reorder_q, skb);
793 /* Try to dequeue as many skbs from reorder_q as we can. */
794 l2tp_recv_dequeue(session);
796 l2tp_session_dec_refcount(session);
801 atomic_long_inc(&session->stats.rx_errors);
805 (*session->deref)(session);
807 l2tp_session_dec_refcount(session);
809 EXPORT_SYMBOL(l2tp_recv_common);
811 /* Drop skbs from the session's reorder_q
813 int l2tp_session_queue_purge(struct l2tp_session *session)
815 struct sk_buff *skb = NULL;
817 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
818 while ((skb = skb_dequeue(&session->reorder_q))) {
819 atomic_long_inc(&session->stats.rx_errors);
822 (*session->deref)(session);
826 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
828 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
829 * here. The skb is not on a list when we get here.
830 * Returns 0 if the packet was a data packet and was successfully passed on.
831 * Returns 1 if the packet was not a good data packet and could not be
832 * forwarded. All such packets are passed up to userspace to deal with.
834 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
835 int (*payload_hook)(struct sk_buff *skb))
837 struct l2tp_session *session = NULL;
838 unsigned char *ptr, *optr;
840 u32 tunnel_id, session_id;
844 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
845 goto discard_bad_csum;
847 /* UDP always verifies the packet length. */
848 __skb_pull(skb, sizeof(struct udphdr));
851 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
852 l2tp_info(tunnel, L2TP_MSG_DATA,
853 "%s: recv short packet (len=%d)\n",
854 tunnel->name, skb->len);
858 /* Trace packet contents, if enabled */
859 if (tunnel->debug & L2TP_MSG_DATA) {
860 length = min(32u, skb->len);
861 if (!pskb_may_pull(skb, length))
864 pr_debug("%s: recv\n", tunnel->name);
865 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
868 /* Point to L2TP header */
869 optr = ptr = skb->data;
871 /* Get L2TP header flags */
872 hdrflags = ntohs(*(__be16 *) ptr);
874 /* Check protocol version */
875 version = hdrflags & L2TP_HDR_VER_MASK;
876 if (version != tunnel->version) {
877 l2tp_info(tunnel, L2TP_MSG_DATA,
878 "%s: recv protocol version mismatch: got %d expected %d\n",
879 tunnel->name, version, tunnel->version);
883 /* Get length of L2TP packet */
886 /* If type is control packet, it is handled by userspace. */
887 if (hdrflags & L2TP_HDRFLAG_T) {
888 l2tp_dbg(tunnel, L2TP_MSG_DATA,
889 "%s: recv control packet, len=%d\n",
890 tunnel->name, length);
897 if (tunnel->version == L2TP_HDR_VER_2) {
898 /* If length is present, skip it */
899 if (hdrflags & L2TP_HDRFLAG_L)
902 /* Extract tunnel and session ID */
903 tunnel_id = ntohs(*(__be16 *) ptr);
905 session_id = ntohs(*(__be16 *) ptr);
908 ptr += 2; /* skip reserved bits */
909 tunnel_id = tunnel->tunnel_id;
910 session_id = ntohl(*(__be32 *) ptr);
914 /* Find the session context */
915 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
916 if (!session || !session->recv_skb) {
917 /* Not found? Pass to userspace to deal with */
918 l2tp_info(tunnel, L2TP_MSG_DATA,
919 "%s: no session found (%u/%u). Passing up.\n",
920 tunnel->name, tunnel_id, session_id);
924 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
929 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
930 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
931 atomic_long_inc(&tunnel->stats.rx_errors);
937 /* Put UDP header back */
938 __skb_push(skb, sizeof(struct udphdr));
943 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
947 * >0: skb should be passed up to userspace as UDP.
949 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
951 struct l2tp_tunnel *tunnel;
953 tunnel = l2tp_sock_to_tunnel(sk);
957 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
958 tunnel->name, skb->len);
960 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
971 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
973 /************************************************************************
975 ***********************************************************************/
977 /* Build an L2TP header for the session into the buffer provided.
979 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
981 struct l2tp_tunnel *tunnel = session->tunnel;
984 u16 flags = L2TP_HDR_VER_2;
985 u32 tunnel_id = tunnel->peer_tunnel_id;
986 u32 session_id = session->peer_session_id;
988 if (session->send_seq)
989 flags |= L2TP_HDRFLAG_S;
991 /* Setup L2TP header. */
992 *bufp++ = htons(flags);
993 *bufp++ = htons(tunnel_id);
994 *bufp++ = htons(session_id);
995 if (session->send_seq) {
996 *bufp++ = htons(session->ns);
999 session->ns &= 0xffff;
1000 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1001 session->name, session->ns);
1007 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1009 struct l2tp_tunnel *tunnel = session->tunnel;
1013 /* Setup L2TP header. The header differs slightly for UDP and
1014 * IP encapsulations. For UDP, there is 4 bytes of flags.
1016 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1017 u16 flags = L2TP_HDR_VER_3;
1018 *((__be16 *) bufp) = htons(flags);
1020 *((__be16 *) bufp) = 0;
1024 *((__be32 *) bufp) = htonl(session->peer_session_id);
1026 if (session->cookie_len) {
1027 memcpy(bufp, &session->cookie[0], session->cookie_len);
1028 bufp += session->cookie_len;
1030 if (session->l2specific_len) {
1031 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1033 if (session->send_seq) {
1034 l2h = 0x40000000 | session->ns;
1036 session->ns &= 0xffffff;
1037 l2tp_dbg(session, L2TP_MSG_SEQ,
1038 "%s: updated ns to %u\n",
1039 session->name, session->ns);
1042 *((__be32 *) bufp) = htonl(l2h);
1044 bufp += session->l2specific_len;
1046 if (session->offset)
1047 bufp += session->offset;
1052 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1053 struct flowi *fl, size_t data_len)
1055 struct l2tp_tunnel *tunnel = session->tunnel;
1056 unsigned int len = skb->len;
1060 if (session->send_seq)
1061 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1062 session->name, data_len, session->ns - 1);
1064 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1065 session->name, data_len);
1067 if (session->debug & L2TP_MSG_DATA) {
1068 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1069 unsigned char *datap = skb->data + uhlen;
1071 pr_debug("%s: xmit\n", session->name);
1072 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1073 datap, min_t(size_t, 32, len - uhlen));
1076 /* Queue the packet to IP for output */
1078 #if IS_ENABLED(CONFIG_IPV6)
1079 if (skb->sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1080 error = inet6_csk_xmit(skb, NULL);
1083 error = ip_queue_xmit(skb, fl);
1087 atomic_long_inc(&tunnel->stats.tx_packets);
1088 atomic_long_add(len, &tunnel->stats.tx_bytes);
1089 atomic_long_inc(&session->stats.tx_packets);
1090 atomic_long_add(len, &session->stats.tx_bytes);
1092 atomic_long_inc(&tunnel->stats.tx_errors);
1093 atomic_long_inc(&session->stats.tx_errors);
1099 /* Automatically called when the skb is freed.
1101 static void l2tp_sock_wfree(struct sk_buff *skb)
1106 /* For data skbs that we transmit, we associate with the tunnel socket
1107 * but don't do accounting.
1109 static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1113 skb->destructor = l2tp_sock_wfree;
1116 #if IS_ENABLED(CONFIG_IPV6)
1117 static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1120 struct ipv6_pinfo *np = inet6_sk(sk);
1121 struct udphdr *uh = udp_hdr(skb);
1123 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1124 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1125 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1126 skb->ip_summed = CHECKSUM_UNNECESSARY;
1127 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1130 uh->check = CSUM_MANGLED_0;
1132 skb->ip_summed = CHECKSUM_PARTIAL;
1133 skb->csum_start = skb_transport_header(skb) - skb->head;
1134 skb->csum_offset = offsetof(struct udphdr, check);
1135 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1136 udp_len, IPPROTO_UDP, 0);
1141 /* If caller requires the skb to have a ppp header, the header must be
1142 * inserted in the skb data before calling this function.
1144 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1146 int data_len = skb->len;
1147 struct l2tp_tunnel *tunnel = session->tunnel;
1148 struct sock *sk = tunnel->sock;
1151 struct inet_sock *inet;
1154 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1156 int ret = NET_XMIT_SUCCESS;
1158 /* Check that there's enough headroom in the skb to insert IP,
1159 * UDP and L2TP headers. If not enough, expand it to
1160 * make room. Adjust truesize.
1162 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1164 if (skb_cow_head(skb, headroom)) {
1166 return NET_XMIT_DROP;
1170 /* Setup L2TP header */
1171 session->build_header(session, __skb_push(skb, hdr_len));
1173 /* Reset skb netfilter state */
1174 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1175 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1180 if (sock_owned_by_user(sk)) {
1182 ret = NET_XMIT_DROP;
1186 /* Get routing info from the tunnel socket */
1188 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
1191 fl = &inet->cork.fl;
1192 switch (tunnel->encap) {
1193 case L2TP_ENCAPTYPE_UDP:
1194 /* Setup UDP header */
1195 __skb_push(skb, sizeof(*uh));
1196 skb_reset_transport_header(skb);
1198 uh->source = inet->inet_sport;
1199 uh->dest = inet->inet_dport;
1200 udp_len = uhlen + hdr_len + data_len;
1201 uh->len = htons(udp_len);
1204 /* Calculate UDP checksum if configured to do so */
1205 #if IS_ENABLED(CONFIG_IPV6)
1206 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1207 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1210 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1211 skb->ip_summed = CHECKSUM_NONE;
1212 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1213 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1214 skb->ip_summed = CHECKSUM_COMPLETE;
1215 csum = skb_checksum(skb, 0, udp_len, 0);
1216 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1218 udp_len, IPPROTO_UDP, csum);
1220 uh->check = CSUM_MANGLED_0;
1222 skb->ip_summed = CHECKSUM_PARTIAL;
1223 skb->csum_start = skb_transport_header(skb) - skb->head;
1224 skb->csum_offset = offsetof(struct udphdr, check);
1225 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1227 udp_len, IPPROTO_UDP, 0);
1231 case L2TP_ENCAPTYPE_IP:
1235 l2tp_skb_set_owner_w(skb, sk);
1237 l2tp_xmit_core(session, skb, fl, data_len);
1243 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1245 /*****************************************************************************
1246 * Tinnel and session create/destroy.
1247 *****************************************************************************/
1249 /* Tunnel socket destruct hook.
1250 * The tunnel context is deleted only when all session sockets have been
1253 static void l2tp_tunnel_destruct(struct sock *sk)
1255 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1256 struct l2tp_net *pn;
1261 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1264 /* Disable udp encapsulation */
1265 switch (tunnel->encap) {
1266 case L2TP_ENCAPTYPE_UDP:
1267 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1268 (udp_sk(sk))->encap_type = 0;
1269 (udp_sk(sk))->encap_rcv = NULL;
1270 (udp_sk(sk))->encap_destroy = NULL;
1272 case L2TP_ENCAPTYPE_IP:
1276 /* Remove hooks into tunnel socket */
1277 sk->sk_destruct = tunnel->old_sk_destruct;
1278 sk->sk_user_data = NULL;
1279 tunnel->sock = NULL;
1281 /* Remove the tunnel struct from the tunnel list */
1282 pn = l2tp_pernet(tunnel->l2tp_net);
1283 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1284 list_del_rcu(&tunnel->list);
1285 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1286 atomic_dec(&l2tp_tunnel_count);
1288 l2tp_tunnel_closeall(tunnel);
1289 l2tp_tunnel_dec_refcount(tunnel);
1291 /* Call the original destructor */
1292 if (sk->sk_destruct)
1293 (*sk->sk_destruct)(sk);
1298 /* When the tunnel is closed, all the attached sessions need to go too.
1300 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1303 struct hlist_node *walk;
1304 struct hlist_node *tmp;
1305 struct l2tp_session *session;
1307 BUG_ON(tunnel == NULL);
1309 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1312 write_lock_bh(&tunnel->hlist_lock);
1313 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1315 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1316 session = hlist_entry(walk, struct l2tp_session, hlist);
1318 l2tp_info(session, L2TP_MSG_CONTROL,
1319 "%s: closing session\n", session->name);
1321 hlist_del_init(&session->hlist);
1323 if (session->ref != NULL)
1324 (*session->ref)(session);
1326 write_unlock_bh(&tunnel->hlist_lock);
1328 __l2tp_session_unhash(session);
1329 l2tp_session_queue_purge(session);
1331 if (session->session_close != NULL)
1332 (*session->session_close)(session);
1334 if (session->deref != NULL)
1335 (*session->deref)(session);
1337 l2tp_session_dec_refcount(session);
1339 write_lock_bh(&tunnel->hlist_lock);
1341 /* Now restart from the beginning of this hash
1342 * chain. We always remove a session from the
1343 * list so we are guaranteed to make forward
1349 write_unlock_bh(&tunnel->hlist_lock);
1351 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1353 /* Tunnel socket destroy hook for UDP encapsulation */
1354 static void l2tp_udp_encap_destroy(struct sock *sk)
1356 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1358 l2tp_tunnel_closeall(tunnel);
1363 /* Really kill the tunnel.
1364 * Come here only when all sessions have been cleared from the tunnel.
1366 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1368 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1369 BUG_ON(tunnel->sock != NULL);
1370 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
1371 kfree_rcu(tunnel, rcu);
1374 /* Workqueue tunnel deletion function */
1375 static void l2tp_tunnel_del_work(struct work_struct *work)
1377 struct l2tp_tunnel *tunnel = NULL;
1378 struct socket *sock = NULL;
1379 struct sock *sk = NULL;
1381 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1382 sk = l2tp_tunnel_sock_lookup(tunnel);
1386 sock = sk->sk_socket;
1388 /* If the tunnel socket was created by userspace, then go through the
1389 * inet layer to shut the socket down, and let userspace close it.
1390 * Otherwise, if we created the socket directly within the kernel, use
1391 * the sk API to release it here.
1392 * In either case the tunnel resources are freed in the socket
1393 * destructor when the tunnel socket goes away.
1395 if (tunnel->fd >= 0) {
1397 inet_shutdown(sock, 2);
1400 kernel_sock_shutdown(sock, SHUT_RDWR);
1401 sk_release_kernel(sk);
1404 l2tp_tunnel_sock_put(sk);
1407 /* Create a socket for the tunnel, if one isn't set up by
1408 * userspace. This is used for static tunnels where there is no
1409 * managing L2TP daemon.
1411 * Since we don't want these sockets to keep a namespace alive by
1412 * themselves, we drop the socket's namespace refcount after creation.
1413 * These sockets are freed when the namespace exits using the pernet
1416 static int l2tp_tunnel_sock_create(struct net *net,
1419 struct l2tp_tunnel_cfg *cfg,
1420 struct socket **sockp)
1423 struct socket *sock = NULL;
1424 struct sockaddr_in udp_addr = {0};
1425 struct sockaddr_l2tpip ip_addr = {0};
1426 #if IS_ENABLED(CONFIG_IPV6)
1427 struct sockaddr_in6 udp6_addr = {0};
1428 struct sockaddr_l2tpip6 ip6_addr = {0};
1431 switch (cfg->encap) {
1432 case L2TP_ENCAPTYPE_UDP:
1433 #if IS_ENABLED(CONFIG_IPV6)
1434 if (cfg->local_ip6 && cfg->peer_ip6) {
1435 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
1439 sk_change_net(sock->sk, net);
1441 udp6_addr.sin6_family = AF_INET6;
1442 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1443 sizeof(udp6_addr.sin6_addr));
1444 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1445 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1450 udp6_addr.sin6_family = AF_INET6;
1451 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1452 sizeof(udp6_addr.sin6_addr));
1453 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1454 err = kernel_connect(sock,
1455 (struct sockaddr *) &udp6_addr,
1456 sizeof(udp6_addr), 0);
1462 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
1466 sk_change_net(sock->sk, net);
1468 udp_addr.sin_family = AF_INET;
1469 udp_addr.sin_addr = cfg->local_ip;
1470 udp_addr.sin_port = htons(cfg->local_udp_port);
1471 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1476 udp_addr.sin_family = AF_INET;
1477 udp_addr.sin_addr = cfg->peer_ip;
1478 udp_addr.sin_port = htons(cfg->peer_udp_port);
1479 err = kernel_connect(sock,
1480 (struct sockaddr *) &udp_addr,
1481 sizeof(udp_addr), 0);
1486 if (!cfg->use_udp_checksums)
1487 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1491 case L2TP_ENCAPTYPE_IP:
1492 #if IS_ENABLED(CONFIG_IPV6)
1493 if (cfg->local_ip6 && cfg->peer_ip6) {
1494 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1495 IPPROTO_L2TP, &sock);
1499 sk_change_net(sock->sk, net);
1501 ip6_addr.l2tp_family = AF_INET6;
1502 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1503 sizeof(ip6_addr.l2tp_addr));
1504 ip6_addr.l2tp_conn_id = tunnel_id;
1505 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1510 ip6_addr.l2tp_family = AF_INET6;
1511 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1512 sizeof(ip6_addr.l2tp_addr));
1513 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1514 err = kernel_connect(sock,
1515 (struct sockaddr *) &ip6_addr,
1516 sizeof(ip6_addr), 0);
1522 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1523 IPPROTO_L2TP, &sock);
1527 sk_change_net(sock->sk, net);
1529 ip_addr.l2tp_family = AF_INET;
1530 ip_addr.l2tp_addr = cfg->local_ip;
1531 ip_addr.l2tp_conn_id = tunnel_id;
1532 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1537 ip_addr.l2tp_family = AF_INET;
1538 ip_addr.l2tp_addr = cfg->peer_ip;
1539 ip_addr.l2tp_conn_id = peer_tunnel_id;
1540 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1541 sizeof(ip_addr), 0);
1553 if ((err < 0) && sock) {
1554 kernel_sock_shutdown(sock, SHUT_RDWR);
1555 sk_release_kernel(sock->sk);
1562 static struct lock_class_key l2tp_socket_class;
1564 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)
1566 struct l2tp_tunnel *tunnel = NULL;
1568 struct socket *sock = NULL;
1569 struct sock *sk = NULL;
1570 struct l2tp_net *pn;
1571 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1573 /* Get the tunnel socket from the fd, which was opened by
1574 * the userspace L2TP daemon. If not specified, create a
1578 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1583 sock = sockfd_lookup(fd, &err);
1585 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1586 tunnel_id, fd, err);
1591 /* Reject namespace mismatches */
1592 if (!net_eq(sock_net(sock->sk), net)) {
1593 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1604 /* Quick sanity checks */
1606 case L2TP_ENCAPTYPE_UDP:
1607 err = -EPROTONOSUPPORT;
1608 if (sk->sk_protocol != IPPROTO_UDP) {
1609 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1610 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1614 case L2TP_ENCAPTYPE_IP:
1615 err = -EPROTONOSUPPORT;
1616 if (sk->sk_protocol != IPPROTO_L2TP) {
1617 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1618 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1624 /* Check if this socket has already been prepped */
1625 tunnel = l2tp_tunnel(sk);
1626 if (tunnel != NULL) {
1627 /* This socket has already been prepped */
1632 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1633 if (tunnel == NULL) {
1638 tunnel->version = version;
1639 tunnel->tunnel_id = tunnel_id;
1640 tunnel->peer_tunnel_id = peer_tunnel_id;
1641 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1643 tunnel->magic = L2TP_TUNNEL_MAGIC;
1644 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1645 rwlock_init(&tunnel->hlist_lock);
1647 /* The net we belong to */
1648 tunnel->l2tp_net = net;
1649 pn = l2tp_pernet(net);
1652 tunnel->debug = cfg->debug;
1654 #if IS_ENABLED(CONFIG_IPV6)
1655 if (sk->sk_family == PF_INET6) {
1656 struct ipv6_pinfo *np = inet6_sk(sk);
1658 if (ipv6_addr_v4mapped(&np->saddr) &&
1659 ipv6_addr_v4mapped(&np->daddr)) {
1660 struct inet_sock *inet = inet_sk(sk);
1662 tunnel->v4mapped = true;
1663 inet->inet_saddr = np->saddr.s6_addr32[3];
1664 inet->inet_rcv_saddr = np->rcv_saddr.s6_addr32[3];
1665 inet->inet_daddr = np->daddr.s6_addr32[3];
1667 tunnel->v4mapped = false;
1672 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1673 tunnel->encap = encap;
1674 if (encap == L2TP_ENCAPTYPE_UDP) {
1675 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1676 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1677 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1678 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
1679 #if IS_ENABLED(CONFIG_IPV6)
1680 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1681 udpv6_encap_enable();
1687 sk->sk_user_data = tunnel;
1689 /* Hook on the tunnel socket destructor so that we can cleanup
1690 * if the tunnel socket goes away.
1692 tunnel->old_sk_destruct = sk->sk_destruct;
1693 sk->sk_destruct = &l2tp_tunnel_destruct;
1696 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1698 sk->sk_allocation = GFP_ATOMIC;
1700 /* Init delete workqueue struct */
1701 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1703 /* Add tunnel to our list */
1704 INIT_LIST_HEAD(&tunnel->list);
1705 atomic_inc(&l2tp_tunnel_count);
1707 /* Bump the reference count. The tunnel context is deleted
1708 * only when this drops to zero. Must be done before list insertion
1710 l2tp_tunnel_inc_refcount(tunnel);
1711 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1712 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1713 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1720 /* If tunnel's socket was created by the kernel, it doesn't
1723 if (sock && sock->file)
1728 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1730 /* This function is used by the netlink TUNNEL_DELETE command.
1732 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1734 l2tp_tunnel_closeall(tunnel);
1735 return (false == queue_work(l2tp_wq, &tunnel->del_work));
1737 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1739 /* Really kill the session.
1741 void l2tp_session_free(struct l2tp_session *session)
1743 struct l2tp_tunnel *tunnel = session->tunnel;
1745 BUG_ON(atomic_read(&session->ref_count) != 0);
1748 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1749 if (session->session_id != 0)
1750 atomic_dec(&l2tp_session_count);
1751 sock_put(tunnel->sock);
1752 session->tunnel = NULL;
1753 l2tp_tunnel_dec_refcount(tunnel);
1760 EXPORT_SYMBOL_GPL(l2tp_session_free);
1762 /* Remove an l2tp session from l2tp_core's hash lists.
1763 * Provides a tidyup interface for pseudowire code which can't just route all
1764 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1767 void __l2tp_session_unhash(struct l2tp_session *session)
1769 struct l2tp_tunnel *tunnel = session->tunnel;
1771 /* Remove the session from core hashes */
1773 /* Remove from the per-tunnel hash */
1774 write_lock_bh(&tunnel->hlist_lock);
1775 hlist_del_init(&session->hlist);
1776 write_unlock_bh(&tunnel->hlist_lock);
1778 /* For L2TPv3 we have a per-net hash: remove from there, too */
1779 if (tunnel->version != L2TP_HDR_VER_2) {
1780 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1781 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1782 hlist_del_init_rcu(&session->global_hlist);
1783 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1788 EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1790 /* This function is used by the netlink SESSION_DELETE command and by
1793 int l2tp_session_delete(struct l2tp_session *session)
1796 (*session->ref)(session);
1797 __l2tp_session_unhash(session);
1798 l2tp_session_queue_purge(session);
1799 if (session->session_close != NULL)
1800 (*session->session_close)(session);
1802 (*session->deref)(session);
1803 l2tp_session_dec_refcount(session);
1806 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1808 /* We come here whenever a session's send_seq, cookie_len or
1809 * l2specific_len parameters are set.
1811 static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1813 if (version == L2TP_HDR_VER_2) {
1814 session->hdr_len = 6;
1815 if (session->send_seq)
1816 session->hdr_len += 4;
1818 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1819 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1820 session->hdr_len += 4;
1825 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)
1827 struct l2tp_session *session;
1829 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1830 if (session != NULL) {
1831 session->magic = L2TP_SESSION_MAGIC;
1832 session->tunnel = tunnel;
1834 session->session_id = session_id;
1835 session->peer_session_id = peer_session_id;
1838 sprintf(&session->name[0], "sess %u/%u",
1839 tunnel->tunnel_id, session->session_id);
1841 skb_queue_head_init(&session->reorder_q);
1843 INIT_HLIST_NODE(&session->hlist);
1844 INIT_HLIST_NODE(&session->global_hlist);
1846 /* Inherit debug options from tunnel */
1847 session->debug = tunnel->debug;
1850 session->pwtype = cfg->pw_type;
1851 session->debug = cfg->debug;
1852 session->mtu = cfg->mtu;
1853 session->mru = cfg->mru;
1854 session->send_seq = cfg->send_seq;
1855 session->recv_seq = cfg->recv_seq;
1856 session->lns_mode = cfg->lns_mode;
1857 session->reorder_timeout = cfg->reorder_timeout;
1858 session->offset = cfg->offset;
1859 session->l2specific_type = cfg->l2specific_type;
1860 session->l2specific_len = cfg->l2specific_len;
1861 session->cookie_len = cfg->cookie_len;
1862 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1863 session->peer_cookie_len = cfg->peer_cookie_len;
1864 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1867 if (tunnel->version == L2TP_HDR_VER_2)
1868 session->build_header = l2tp_build_l2tpv2_header;
1870 session->build_header = l2tp_build_l2tpv3_header;
1872 l2tp_session_set_header_len(session, tunnel->version);
1874 /* Bump the reference count. The session context is deleted
1875 * only when this drops to zero.
1877 l2tp_session_inc_refcount(session);
1878 l2tp_tunnel_inc_refcount(tunnel);
1880 /* Ensure tunnel socket isn't deleted */
1881 sock_hold(tunnel->sock);
1883 /* Add session to the tunnel's hash list */
1884 write_lock_bh(&tunnel->hlist_lock);
1885 hlist_add_head(&session->hlist,
1886 l2tp_session_id_hash(tunnel, session_id));
1887 write_unlock_bh(&tunnel->hlist_lock);
1889 /* And to the global session list if L2TPv3 */
1890 if (tunnel->version != L2TP_HDR_VER_2) {
1891 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1893 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1894 hlist_add_head_rcu(&session->global_hlist,
1895 l2tp_session_id_hash_2(pn, session_id));
1896 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1899 /* Ignore management session in session count value */
1900 if (session->session_id != 0)
1901 atomic_inc(&l2tp_session_count);
1906 EXPORT_SYMBOL_GPL(l2tp_session_create);
1908 /*****************************************************************************
1910 *****************************************************************************/
1912 static __net_init int l2tp_init_net(struct net *net)
1914 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1917 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1918 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1920 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1921 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1923 spin_lock_init(&pn->l2tp_session_hlist_lock);
1928 static __net_exit void l2tp_exit_net(struct net *net)
1930 struct l2tp_net *pn = l2tp_pernet(net);
1931 struct l2tp_tunnel *tunnel = NULL;
1934 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1935 (void)l2tp_tunnel_delete(tunnel);
1937 rcu_read_unlock_bh();
1940 static struct pernet_operations l2tp_net_ops = {
1941 .init = l2tp_init_net,
1942 .exit = l2tp_exit_net,
1944 .size = sizeof(struct l2tp_net),
1947 static int __init l2tp_init(void)
1951 rc = register_pernet_device(&l2tp_net_ops);
1955 l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
1957 pr_err("alloc_workqueue failed\n");
1962 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1968 static void __exit l2tp_exit(void)
1970 unregister_pernet_device(&l2tp_net_ops);
1972 destroy_workqueue(l2tp_wq);
1977 module_init(l2tp_init);
1978 module_exit(l2tp_exit);
1980 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1981 MODULE_DESCRIPTION("L2TP core");
1982 MODULE_LICENSE("GPL");
1983 MODULE_VERSION(L2TP_DRV_VERSION);