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_tunnel_free(struct l2tp_tunnel *tunnel);
117 static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
119 return sk->sk_user_data;
122 static inline struct l2tp_net *l2tp_pernet(struct net *net)
126 return net_generic(net, l2tp_net_id);
129 /* Tunnel reference counts. Incremented per session that is added to
132 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
134 atomic_inc(&tunnel->ref_count);
137 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
139 if (atomic_dec_and_test(&tunnel->ref_count))
140 l2tp_tunnel_free(tunnel);
142 #ifdef L2TP_REFCNT_DEBUG
143 #define l2tp_tunnel_inc_refcount(_t) \
145 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
146 __func__, __LINE__, (_t)->name, \
147 atomic_read(&_t->ref_count)); \
148 l2tp_tunnel_inc_refcount_1(_t); \
150 #define l2tp_tunnel_dec_refcount(_t)
152 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
153 __func__, __LINE__, (_t)->name, \
154 atomic_read(&_t->ref_count)); \
155 l2tp_tunnel_dec_refcount_1(_t); \
158 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
159 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
162 /* Session hash global list for L2TPv3.
163 * The session_id SHOULD be random according to RFC3931, but several
164 * L2TP implementations use incrementing session_ids. So we do a real
165 * hash on the session_id, rather than a simple bitmask.
167 static inline struct hlist_head *
168 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
170 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
174 /* Lookup the tunnel socket, possibly involving the fs code if the socket is
175 * owned by userspace. A struct sock returned from this function must be
176 * released using l2tp_tunnel_sock_put once you're done with it.
178 static struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
181 struct socket *sock = NULL;
182 struct sock *sk = NULL;
187 if (tunnel->fd >= 0) {
188 /* Socket is owned by userspace, who might be in the process
189 * of closing it. Look the socket up using the fd to ensure
192 sock = sockfd_lookup(tunnel->fd, &err);
196 /* Socket is owned by kernelspace */
205 /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
206 static void l2tp_tunnel_sock_put(struct sock *sk)
208 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
210 if (tunnel->fd >= 0) {
211 /* Socket is owned by userspace */
212 sockfd_put(sk->sk_socket);
219 /* Lookup a session by id in the global session list
221 static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
223 struct l2tp_net *pn = l2tp_pernet(net);
224 struct hlist_head *session_list =
225 l2tp_session_id_hash_2(pn, session_id);
226 struct l2tp_session *session;
229 hlist_for_each_entry_rcu(session, session_list, global_hlist) {
230 if (session->session_id == session_id) {
231 rcu_read_unlock_bh();
235 rcu_read_unlock_bh();
240 /* Session hash list.
241 * The session_id SHOULD be random according to RFC2661, but several
242 * L2TP implementations (Cisco and Microsoft) use incrementing
243 * session_ids. So we do a real hash on the session_id, rather than a
246 static inline struct hlist_head *
247 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
249 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
252 /* Lookup a session by id
254 struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
256 struct hlist_head *session_list;
257 struct l2tp_session *session;
259 /* In L2TPv3, session_ids are unique over all tunnels and we
260 * sometimes need to look them up before we know the
264 return l2tp_session_find_2(net, session_id);
266 session_list = l2tp_session_id_hash(tunnel, session_id);
267 read_lock_bh(&tunnel->hlist_lock);
268 hlist_for_each_entry(session, session_list, hlist) {
269 if (session->session_id == session_id) {
270 read_unlock_bh(&tunnel->hlist_lock);
274 read_unlock_bh(&tunnel->hlist_lock);
278 EXPORT_SYMBOL_GPL(l2tp_session_find);
280 struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
283 struct l2tp_session *session;
286 read_lock_bh(&tunnel->hlist_lock);
287 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
288 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
290 read_unlock_bh(&tunnel->hlist_lock);
296 read_unlock_bh(&tunnel->hlist_lock);
300 EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
302 /* Lookup a session by interface name.
303 * This is very inefficient but is only used by management interfaces.
305 struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
307 struct l2tp_net *pn = l2tp_pernet(net);
309 struct l2tp_session *session;
312 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
313 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
314 if (!strcmp(session->ifname, ifname)) {
315 rcu_read_unlock_bh();
321 rcu_read_unlock_bh();
325 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
327 /* Lookup a tunnel by id
329 struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
331 struct l2tp_tunnel *tunnel;
332 struct l2tp_net *pn = l2tp_pernet(net);
335 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
336 if (tunnel->tunnel_id == tunnel_id) {
337 rcu_read_unlock_bh();
341 rcu_read_unlock_bh();
345 EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
347 struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
349 struct l2tp_net *pn = l2tp_pernet(net);
350 struct l2tp_tunnel *tunnel;
354 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
356 rcu_read_unlock_bh();
361 rcu_read_unlock_bh();
365 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
367 /*****************************************************************************
368 * Receive data handling
369 *****************************************************************************/
371 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
374 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
376 struct sk_buff *skbp;
378 u32 ns = L2TP_SKB_CB(skb)->ns;
380 spin_lock_bh(&session->reorder_q.lock);
381 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
382 if (L2TP_SKB_CB(skbp)->ns > ns) {
383 __skb_queue_before(&session->reorder_q, skbp, skb);
384 l2tp_dbg(session, L2TP_MSG_SEQ,
385 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
386 session->name, ns, L2TP_SKB_CB(skbp)->ns,
387 skb_queue_len(&session->reorder_q));
388 atomic_long_inc(&session->stats.rx_oos_packets);
393 __skb_queue_tail(&session->reorder_q, skb);
396 spin_unlock_bh(&session->reorder_q.lock);
399 /* Dequeue a single skb.
401 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
403 struct l2tp_tunnel *tunnel = session->tunnel;
404 int length = L2TP_SKB_CB(skb)->length;
406 /* We're about to requeue the skb, so return resources
407 * to its current owner (a socket receive buffer).
411 atomic_long_inc(&tunnel->stats.rx_packets);
412 atomic_long_add(length, &tunnel->stats.rx_bytes);
413 atomic_long_inc(&session->stats.rx_packets);
414 atomic_long_add(length, &session->stats.rx_bytes);
416 if (L2TP_SKB_CB(skb)->has_seq) {
419 session->nr &= session->nr_max;
421 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
422 session->name, session->nr);
425 /* call private receive handler */
426 if (session->recv_skb != NULL)
427 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
432 (*session->deref)(session);
435 /* Dequeue skbs from the session's reorder_q, subject to packet order.
436 * Skbs that have been in the queue for too long are simply discarded.
438 static void l2tp_recv_dequeue(struct l2tp_session *session)
443 /* If the pkt at the head of the queue has the nr that we
444 * expect to send up next, dequeue it and any other
445 * in-sequence packets behind it.
448 spin_lock_bh(&session->reorder_q.lock);
449 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
450 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
451 atomic_long_inc(&session->stats.rx_seq_discards);
452 atomic_long_inc(&session->stats.rx_errors);
453 l2tp_dbg(session, L2TP_MSG_SEQ,
454 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
455 session->name, L2TP_SKB_CB(skb)->ns,
456 L2TP_SKB_CB(skb)->length, session->nr,
457 skb_queue_len(&session->reorder_q));
458 session->reorder_skip = 1;
459 __skb_unlink(skb, &session->reorder_q);
462 (*session->deref)(session);
466 if (L2TP_SKB_CB(skb)->has_seq) {
467 if (session->reorder_skip) {
468 l2tp_dbg(session, L2TP_MSG_SEQ,
469 "%s: advancing nr to next pkt: %u -> %u",
470 session->name, session->nr,
471 L2TP_SKB_CB(skb)->ns);
472 session->reorder_skip = 0;
473 session->nr = L2TP_SKB_CB(skb)->ns;
475 if (L2TP_SKB_CB(skb)->ns != session->nr) {
476 l2tp_dbg(session, L2TP_MSG_SEQ,
477 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
478 session->name, L2TP_SKB_CB(skb)->ns,
479 L2TP_SKB_CB(skb)->length, session->nr,
480 skb_queue_len(&session->reorder_q));
484 __skb_unlink(skb, &session->reorder_q);
486 /* Process the skb. We release the queue lock while we
487 * do so to let other contexts process the queue.
489 spin_unlock_bh(&session->reorder_q.lock);
490 l2tp_recv_dequeue_skb(session, skb);
495 spin_unlock_bh(&session->reorder_q.lock);
498 static inline int l2tp_verify_udp_checksum(struct sock *sk,
501 struct udphdr *uh = udp_hdr(skb);
502 u16 ulen = ntohs(uh->len);
505 if (sk->sk_no_check || skb_csum_unnecessary(skb))
508 #if IS_ENABLED(CONFIG_IPV6)
509 if (sk->sk_family == PF_INET6 && !l2tp_tunnel(sk)->v4mapped) {
511 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
514 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
515 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
516 &ipv6_hdr(skb)->daddr, ulen,
517 IPPROTO_UDP, skb->csum)) {
518 skb->ip_summed = CHECKSUM_UNNECESSARY;
521 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
522 &ipv6_hdr(skb)->daddr,
523 skb->len, IPPROTO_UDP,
528 struct inet_sock *inet;
532 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
533 ulen, IPPROTO_UDP, 0);
535 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
536 !csum_fold(csum_add(psum, skb->csum)))
541 return __skb_checksum_complete(skb);
544 static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
548 if (nr >= session->nr)
549 nws = nr - session->nr;
551 nws = (session->nr_max + 1) - (session->nr - nr);
553 return nws < session->nr_window_size;
556 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
557 * acceptable, else non-zero.
559 static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
561 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
562 /* Packet sequence number is outside allowed window.
565 l2tp_dbg(session, L2TP_MSG_SEQ,
566 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
567 session->name, L2TP_SKB_CB(skb)->ns,
568 L2TP_SKB_CB(skb)->length, session->nr);
572 if (session->reorder_timeout != 0) {
573 /* Packet reordering enabled. Add skb to session's
574 * reorder queue, in order of ns.
576 l2tp_recv_queue_skb(session, skb);
580 /* Packet reordering disabled. Discard out-of-sequence packets, while
581 * tracking the number if in-sequence packets after the first OOS packet
582 * is seen. After nr_oos_count_max in-sequence packets, reset the
583 * sequence number to re-enable packet reception.
585 if (L2TP_SKB_CB(skb)->ns == session->nr) {
586 skb_queue_tail(&session->reorder_q, skb);
588 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
589 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
591 if (nr_oos == nr_next)
592 session->nr_oos_count++;
594 session->nr_oos_count = 0;
596 session->nr_oos = nr_oos;
597 if (session->nr_oos_count > session->nr_oos_count_max) {
598 session->reorder_skip = 1;
599 l2tp_dbg(session, L2TP_MSG_SEQ,
600 "%s: %d oos packets received. Resetting sequence numbers\n",
601 session->name, session->nr_oos_count);
603 if (!session->reorder_skip) {
604 atomic_long_inc(&session->stats.rx_seq_discards);
605 l2tp_dbg(session, L2TP_MSG_SEQ,
606 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
607 session->name, L2TP_SKB_CB(skb)->ns,
608 L2TP_SKB_CB(skb)->length, session->nr,
609 skb_queue_len(&session->reorder_q));
612 skb_queue_tail(&session->reorder_q, skb);
622 /* Do receive processing of L2TP data frames. We handle both L2TPv2
623 * and L2TPv3 data frames here.
625 * L2TPv2 Data Message Header
628 * 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
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
631 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 * | Tunnel ID | Session ID |
633 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 * | Ns (opt) | Nr (opt) |
635 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636 * | Offset Size (opt) | Offset pad... (opt)
637 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
639 * Data frames are marked by T=0. All other fields are the same as
640 * those in L2TP control frames.
642 * L2TPv3 Data Message Header
644 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
645 * | L2TP Session Header |
646 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
647 * | L2-Specific Sublayer |
648 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
649 * | Tunnel Payload ...
650 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
652 * L2TPv3 Session Header Over IP
655 * 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
656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659 * | Cookie (optional, maximum 64 bits)...
660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
662 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
664 * L2TPv3 L2-Specific Sublayer Format
667 * 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
668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
669 * |x|S|x|x|x|x|x|x| Sequence Number |
670 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
672 * Cookie value, sublayer format and offset (pad) are negotiated with
673 * the peer when the session is set up. Unlike L2TPv2, we do not need
674 * to parse the packet header to determine if optional fields are
677 * Caller must already have parsed the frame and determined that it is
678 * a data (not control) frame before coming here. Fields up to the
679 * session-id have already been parsed and ptr points to the data
680 * after the session-id.
682 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
683 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
684 int length, int (*payload_hook)(struct sk_buff *skb))
686 struct l2tp_tunnel *tunnel = session->tunnel;
690 /* The ref count is increased since we now hold a pointer to
691 * the session. Take care to decrement the refcnt when exiting
692 * this function from now on...
694 l2tp_session_inc_refcount(session);
696 (*session->ref)(session);
698 /* Parse and check optional cookie */
699 if (session->peer_cookie_len > 0) {
700 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
701 l2tp_info(tunnel, L2TP_MSG_DATA,
702 "%s: cookie mismatch (%u/%u). Discarding.\n",
703 tunnel->name, tunnel->tunnel_id,
704 session->session_id);
705 atomic_long_inc(&session->stats.rx_cookie_discards);
708 ptr += session->peer_cookie_len;
711 /* Handle the optional sequence numbers. Sequence numbers are
712 * in different places for L2TPv2 and L2TPv3.
714 * If we are the LAC, enable/disable sequence numbers under
715 * the control of the LNS. If no sequence numbers present but
716 * we were expecting them, discard frame.
719 L2TP_SKB_CB(skb)->has_seq = 0;
720 if (tunnel->version == L2TP_HDR_VER_2) {
721 if (hdrflags & L2TP_HDRFLAG_S) {
722 ns = ntohs(*(__be16 *) ptr);
724 nr = ntohs(*(__be16 *) ptr);
727 /* Store L2TP info in the skb */
728 L2TP_SKB_CB(skb)->ns = ns;
729 L2TP_SKB_CB(skb)->has_seq = 1;
731 l2tp_dbg(session, L2TP_MSG_SEQ,
732 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
733 session->name, ns, nr, session->nr);
735 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
736 u32 l2h = ntohl(*(__be32 *) ptr);
738 if (l2h & 0x40000000) {
739 ns = l2h & 0x00ffffff;
741 /* Store L2TP info in the skb */
742 L2TP_SKB_CB(skb)->ns = ns;
743 L2TP_SKB_CB(skb)->has_seq = 1;
745 l2tp_dbg(session, L2TP_MSG_SEQ,
746 "%s: recv data ns=%u, session nr=%u\n",
747 session->name, ns, session->nr);
751 /* Advance past L2-specific header, if present */
752 ptr += session->l2specific_len;
754 if (L2TP_SKB_CB(skb)->has_seq) {
755 /* Received a packet with sequence numbers. If we're the LNS,
756 * check if we sre sending sequence numbers and if not,
759 if ((!session->lns_mode) && (!session->send_seq)) {
760 l2tp_info(session, L2TP_MSG_SEQ,
761 "%s: requested to enable seq numbers by LNS\n",
763 session->send_seq = -1;
764 l2tp_session_set_header_len(session, tunnel->version);
767 /* No sequence numbers.
768 * If user has configured mandatory sequence numbers, discard.
770 if (session->recv_seq) {
771 l2tp_warn(session, L2TP_MSG_SEQ,
772 "%s: recv data has no seq numbers when required. Discarding.\n",
774 atomic_long_inc(&session->stats.rx_seq_discards);
778 /* If we're the LAC and we're sending sequence numbers, the
779 * LNS has requested that we no longer send sequence numbers.
780 * If we're the LNS and we're sending sequence numbers, the
781 * LAC is broken. Discard the frame.
783 if ((!session->lns_mode) && (session->send_seq)) {
784 l2tp_info(session, L2TP_MSG_SEQ,
785 "%s: requested to disable seq numbers by LNS\n",
787 session->send_seq = 0;
788 l2tp_session_set_header_len(session, tunnel->version);
789 } else if (session->send_seq) {
790 l2tp_warn(session, L2TP_MSG_SEQ,
791 "%s: recv data has no seq numbers when required. Discarding.\n",
793 atomic_long_inc(&session->stats.rx_seq_discards);
798 /* Session data offset is handled differently for L2TPv2 and
799 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
800 * the header. For L2TPv3, the offset is negotiated using AVPs
801 * in the session setup control protocol.
803 if (tunnel->version == L2TP_HDR_VER_2) {
804 /* If offset bit set, skip it. */
805 if (hdrflags & L2TP_HDRFLAG_O) {
806 offset = ntohs(*(__be16 *)ptr);
810 ptr += session->offset;
813 if (!pskb_may_pull(skb, offset))
816 __skb_pull(skb, offset);
818 /* If caller wants to process the payload before we queue the
822 if ((*payload_hook)(skb))
825 /* Prepare skb for adding to the session's reorder_q. Hold
826 * packets for max reorder_timeout or 1 second if not
829 L2TP_SKB_CB(skb)->length = length;
830 L2TP_SKB_CB(skb)->expires = jiffies +
831 (session->reorder_timeout ? session->reorder_timeout : HZ);
833 /* Add packet to the session's receive queue. Reordering is done here, if
834 * enabled. Saved L2TP protocol info is stored in skb->sb[].
836 if (L2TP_SKB_CB(skb)->has_seq) {
837 if (l2tp_recv_data_seq(session, skb))
840 /* No sequence numbers. Add the skb to the tail of the
841 * reorder queue. This ensures that it will be
842 * delivered after all previous sequenced skbs.
844 skb_queue_tail(&session->reorder_q, skb);
847 /* Try to dequeue as many skbs from reorder_q as we can. */
848 l2tp_recv_dequeue(session);
850 l2tp_session_dec_refcount(session);
855 atomic_long_inc(&session->stats.rx_errors);
859 (*session->deref)(session);
861 l2tp_session_dec_refcount(session);
863 EXPORT_SYMBOL(l2tp_recv_common);
865 /* Drop skbs from the session's reorder_q
867 int l2tp_session_queue_purge(struct l2tp_session *session)
869 struct sk_buff *skb = NULL;
871 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
872 while ((skb = skb_dequeue(&session->reorder_q))) {
873 atomic_long_inc(&session->stats.rx_errors);
876 (*session->deref)(session);
880 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
882 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
883 * here. The skb is not on a list when we get here.
884 * Returns 0 if the packet was a data packet and was successfully passed on.
885 * Returns 1 if the packet was not a good data packet and could not be
886 * forwarded. All such packets are passed up to userspace to deal with.
888 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
889 int (*payload_hook)(struct sk_buff *skb))
891 struct l2tp_session *session = NULL;
892 unsigned char *ptr, *optr;
894 u32 tunnel_id, session_id;
898 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
899 goto discard_bad_csum;
901 /* UDP always verifies the packet length. */
902 __skb_pull(skb, sizeof(struct udphdr));
905 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
906 l2tp_info(tunnel, L2TP_MSG_DATA,
907 "%s: recv short packet (len=%d)\n",
908 tunnel->name, skb->len);
912 /* Trace packet contents, if enabled */
913 if (tunnel->debug & L2TP_MSG_DATA) {
914 length = min(32u, skb->len);
915 if (!pskb_may_pull(skb, length))
918 pr_debug("%s: recv\n", tunnel->name);
919 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
922 /* Point to L2TP header */
923 optr = ptr = skb->data;
925 /* Get L2TP header flags */
926 hdrflags = ntohs(*(__be16 *) ptr);
928 /* Check protocol version */
929 version = hdrflags & L2TP_HDR_VER_MASK;
930 if (version != tunnel->version) {
931 l2tp_info(tunnel, L2TP_MSG_DATA,
932 "%s: recv protocol version mismatch: got %d expected %d\n",
933 tunnel->name, version, tunnel->version);
937 /* Get length of L2TP packet */
940 /* If type is control packet, it is handled by userspace. */
941 if (hdrflags & L2TP_HDRFLAG_T) {
942 l2tp_dbg(tunnel, L2TP_MSG_DATA,
943 "%s: recv control packet, len=%d\n",
944 tunnel->name, length);
951 if (tunnel->version == L2TP_HDR_VER_2) {
952 /* If length is present, skip it */
953 if (hdrflags & L2TP_HDRFLAG_L)
956 /* Extract tunnel and session ID */
957 tunnel_id = ntohs(*(__be16 *) ptr);
959 session_id = ntohs(*(__be16 *) ptr);
962 ptr += 2; /* skip reserved bits */
963 tunnel_id = tunnel->tunnel_id;
964 session_id = ntohl(*(__be32 *) ptr);
968 /* Find the session context */
969 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
970 if (!session || !session->recv_skb) {
971 /* Not found? Pass to userspace to deal with */
972 l2tp_info(tunnel, L2TP_MSG_DATA,
973 "%s: no session found (%u/%u). Passing up.\n",
974 tunnel->name, tunnel_id, session_id);
978 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
983 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
984 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
985 atomic_long_inc(&tunnel->stats.rx_errors);
991 /* Put UDP header back */
992 __skb_push(skb, sizeof(struct udphdr));
997 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
1001 * >0: skb should be passed up to userspace as UDP.
1003 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1005 struct l2tp_tunnel *tunnel;
1007 tunnel = l2tp_sock_to_tunnel(sk);
1011 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
1012 tunnel->name, skb->len);
1014 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
1025 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
1027 /************************************************************************
1029 ***********************************************************************/
1031 /* Build an L2TP header for the session into the buffer provided.
1033 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
1035 struct l2tp_tunnel *tunnel = session->tunnel;
1038 u16 flags = L2TP_HDR_VER_2;
1039 u32 tunnel_id = tunnel->peer_tunnel_id;
1040 u32 session_id = session->peer_session_id;
1042 if (session->send_seq)
1043 flags |= L2TP_HDRFLAG_S;
1045 /* Setup L2TP header. */
1046 *bufp++ = htons(flags);
1047 *bufp++ = htons(tunnel_id);
1048 *bufp++ = htons(session_id);
1049 if (session->send_seq) {
1050 *bufp++ = htons(session->ns);
1053 session->ns &= 0xffff;
1054 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1055 session->name, session->ns);
1061 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1063 struct l2tp_tunnel *tunnel = session->tunnel;
1067 /* Setup L2TP header. The header differs slightly for UDP and
1068 * IP encapsulations. For UDP, there is 4 bytes of flags.
1070 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1071 u16 flags = L2TP_HDR_VER_3;
1072 *((__be16 *) bufp) = htons(flags);
1074 *((__be16 *) bufp) = 0;
1078 *((__be32 *) bufp) = htonl(session->peer_session_id);
1080 if (session->cookie_len) {
1081 memcpy(bufp, &session->cookie[0], session->cookie_len);
1082 bufp += session->cookie_len;
1084 if (session->l2specific_len) {
1085 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1087 if (session->send_seq) {
1088 l2h = 0x40000000 | session->ns;
1090 session->ns &= 0xffffff;
1091 l2tp_dbg(session, L2TP_MSG_SEQ,
1092 "%s: updated ns to %u\n",
1093 session->name, session->ns);
1096 *((__be32 *) bufp) = htonl(l2h);
1098 bufp += session->l2specific_len;
1100 if (session->offset)
1101 bufp += session->offset;
1106 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1107 struct flowi *fl, size_t data_len)
1109 struct l2tp_tunnel *tunnel = session->tunnel;
1110 unsigned int len = skb->len;
1114 if (session->send_seq)
1115 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1116 session->name, data_len, session->ns - 1);
1118 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1119 session->name, data_len);
1121 if (session->debug & L2TP_MSG_DATA) {
1122 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1123 unsigned char *datap = skb->data + uhlen;
1125 pr_debug("%s: xmit\n", session->name);
1126 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1127 datap, min_t(size_t, 32, len - uhlen));
1130 /* Queue the packet to IP for output */
1132 #if IS_ENABLED(CONFIG_IPV6)
1133 if (skb->sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1134 error = inet6_csk_xmit(skb, NULL);
1137 error = ip_queue_xmit(skb, fl);
1141 atomic_long_inc(&tunnel->stats.tx_packets);
1142 atomic_long_add(len, &tunnel->stats.tx_bytes);
1143 atomic_long_inc(&session->stats.tx_packets);
1144 atomic_long_add(len, &session->stats.tx_bytes);
1146 atomic_long_inc(&tunnel->stats.tx_errors);
1147 atomic_long_inc(&session->stats.tx_errors);
1153 /* Automatically called when the skb is freed.
1155 static void l2tp_sock_wfree(struct sk_buff *skb)
1160 /* For data skbs that we transmit, we associate with the tunnel socket
1161 * but don't do accounting.
1163 static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1167 skb->destructor = l2tp_sock_wfree;
1170 #if IS_ENABLED(CONFIG_IPV6)
1171 static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1174 struct ipv6_pinfo *np = inet6_sk(sk);
1175 struct udphdr *uh = udp_hdr(skb);
1177 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1178 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1179 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1180 skb->ip_summed = CHECKSUM_UNNECESSARY;
1181 uh->check = csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr, udp_len,
1184 uh->check = CSUM_MANGLED_0;
1186 skb->ip_summed = CHECKSUM_PARTIAL;
1187 skb->csum_start = skb_transport_header(skb) - skb->head;
1188 skb->csum_offset = offsetof(struct udphdr, check);
1189 uh->check = ~csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr,
1190 udp_len, IPPROTO_UDP, 0);
1195 /* If caller requires the skb to have a ppp header, the header must be
1196 * inserted in the skb data before calling this function.
1198 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1200 int data_len = skb->len;
1201 struct l2tp_tunnel *tunnel = session->tunnel;
1202 struct sock *sk = tunnel->sock;
1205 struct inet_sock *inet;
1208 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1210 int ret = NET_XMIT_SUCCESS;
1212 /* Check that there's enough headroom in the skb to insert IP,
1213 * UDP and L2TP headers. If not enough, expand it to
1214 * make room. Adjust truesize.
1216 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1218 if (skb_cow_head(skb, headroom)) {
1220 return NET_XMIT_DROP;
1224 /* Setup L2TP header */
1225 session->build_header(session, __skb_push(skb, hdr_len));
1227 /* Reset skb netfilter state */
1228 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1229 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1234 if (sock_owned_by_user(sk)) {
1236 ret = NET_XMIT_DROP;
1240 /* Get routing info from the tunnel socket */
1242 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
1245 fl = &inet->cork.fl;
1246 switch (tunnel->encap) {
1247 case L2TP_ENCAPTYPE_UDP:
1248 /* Setup UDP header */
1249 __skb_push(skb, sizeof(*uh));
1250 skb_reset_transport_header(skb);
1252 uh->source = inet->inet_sport;
1253 uh->dest = inet->inet_dport;
1254 udp_len = uhlen + hdr_len + data_len;
1255 uh->len = htons(udp_len);
1258 /* Calculate UDP checksum if configured to do so */
1259 #if IS_ENABLED(CONFIG_IPV6)
1260 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1261 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1264 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1265 skb->ip_summed = CHECKSUM_NONE;
1266 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1267 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1268 skb->ip_summed = CHECKSUM_COMPLETE;
1269 csum = skb_checksum(skb, 0, udp_len, 0);
1270 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1272 udp_len, IPPROTO_UDP, csum);
1274 uh->check = CSUM_MANGLED_0;
1276 skb->ip_summed = CHECKSUM_PARTIAL;
1277 skb->csum_start = skb_transport_header(skb) - skb->head;
1278 skb->csum_offset = offsetof(struct udphdr, check);
1279 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1281 udp_len, IPPROTO_UDP, 0);
1285 case L2TP_ENCAPTYPE_IP:
1289 l2tp_skb_set_owner_w(skb, sk);
1291 l2tp_xmit_core(session, skb, fl, data_len);
1297 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1299 /*****************************************************************************
1300 * Tinnel and session create/destroy.
1301 *****************************************************************************/
1303 /* Tunnel socket destruct hook.
1304 * The tunnel context is deleted only when all session sockets have been
1307 static void l2tp_tunnel_destruct(struct sock *sk)
1309 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1310 struct l2tp_net *pn;
1315 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1318 /* Disable udp encapsulation */
1319 switch (tunnel->encap) {
1320 case L2TP_ENCAPTYPE_UDP:
1321 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1322 (udp_sk(sk))->encap_type = 0;
1323 (udp_sk(sk))->encap_rcv = NULL;
1324 (udp_sk(sk))->encap_destroy = NULL;
1326 case L2TP_ENCAPTYPE_IP:
1330 /* Remove hooks into tunnel socket */
1331 sk->sk_destruct = tunnel->old_sk_destruct;
1332 sk->sk_user_data = NULL;
1333 tunnel->sock = NULL;
1335 /* Remove the tunnel struct from the tunnel list */
1336 pn = l2tp_pernet(tunnel->l2tp_net);
1337 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1338 list_del_rcu(&tunnel->list);
1339 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1340 atomic_dec(&l2tp_tunnel_count);
1342 l2tp_tunnel_closeall(tunnel);
1343 l2tp_tunnel_dec_refcount(tunnel);
1345 /* Call the original destructor */
1346 if (sk->sk_destruct)
1347 (*sk->sk_destruct)(sk);
1352 /* When the tunnel is closed, all the attached sessions need to go too.
1354 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1357 struct hlist_node *walk;
1358 struct hlist_node *tmp;
1359 struct l2tp_session *session;
1361 BUG_ON(tunnel == NULL);
1363 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1366 write_lock_bh(&tunnel->hlist_lock);
1367 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1369 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1370 session = hlist_entry(walk, struct l2tp_session, hlist);
1372 l2tp_info(session, L2TP_MSG_CONTROL,
1373 "%s: closing session\n", session->name);
1375 hlist_del_init(&session->hlist);
1377 if (session->ref != NULL)
1378 (*session->ref)(session);
1380 write_unlock_bh(&tunnel->hlist_lock);
1382 __l2tp_session_unhash(session);
1383 l2tp_session_queue_purge(session);
1385 if (session->session_close != NULL)
1386 (*session->session_close)(session);
1388 if (session->deref != NULL)
1389 (*session->deref)(session);
1391 l2tp_session_dec_refcount(session);
1393 write_lock_bh(&tunnel->hlist_lock);
1395 /* Now restart from the beginning of this hash
1396 * chain. We always remove a session from the
1397 * list so we are guaranteed to make forward
1403 write_unlock_bh(&tunnel->hlist_lock);
1405 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1407 /* Tunnel socket destroy hook for UDP encapsulation */
1408 static void l2tp_udp_encap_destroy(struct sock *sk)
1410 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1412 l2tp_tunnel_closeall(tunnel);
1417 /* Really kill the tunnel.
1418 * Come here only when all sessions have been cleared from the tunnel.
1420 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1422 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1423 BUG_ON(tunnel->sock != NULL);
1424 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
1425 kfree_rcu(tunnel, rcu);
1428 /* Workqueue tunnel deletion function */
1429 static void l2tp_tunnel_del_work(struct work_struct *work)
1431 struct l2tp_tunnel *tunnel = NULL;
1432 struct socket *sock = NULL;
1433 struct sock *sk = NULL;
1435 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1436 sk = l2tp_tunnel_sock_lookup(tunnel);
1440 sock = sk->sk_socket;
1442 /* If the tunnel socket was created by userspace, then go through the
1443 * inet layer to shut the socket down, and let userspace close it.
1444 * Otherwise, if we created the socket directly within the kernel, use
1445 * the sk API to release it here.
1446 * In either case the tunnel resources are freed in the socket
1447 * destructor when the tunnel socket goes away.
1449 if (tunnel->fd >= 0) {
1451 inet_shutdown(sock, 2);
1454 kernel_sock_shutdown(sock, SHUT_RDWR);
1455 sk_release_kernel(sk);
1458 l2tp_tunnel_sock_put(sk);
1461 /* Create a socket for the tunnel, if one isn't set up by
1462 * userspace. This is used for static tunnels where there is no
1463 * managing L2TP daemon.
1465 * Since we don't want these sockets to keep a namespace alive by
1466 * themselves, we drop the socket's namespace refcount after creation.
1467 * These sockets are freed when the namespace exits using the pernet
1470 static int l2tp_tunnel_sock_create(struct net *net,
1473 struct l2tp_tunnel_cfg *cfg,
1474 struct socket **sockp)
1477 struct socket *sock = NULL;
1478 struct sockaddr_in udp_addr = {0};
1479 struct sockaddr_l2tpip ip_addr = {0};
1480 #if IS_ENABLED(CONFIG_IPV6)
1481 struct sockaddr_in6 udp6_addr = {0};
1482 struct sockaddr_l2tpip6 ip6_addr = {0};
1485 switch (cfg->encap) {
1486 case L2TP_ENCAPTYPE_UDP:
1487 #if IS_ENABLED(CONFIG_IPV6)
1488 if (cfg->local_ip6 && cfg->peer_ip6) {
1489 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
1493 sk_change_net(sock->sk, net);
1495 udp6_addr.sin6_family = AF_INET6;
1496 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1497 sizeof(udp6_addr.sin6_addr));
1498 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1499 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1504 udp6_addr.sin6_family = AF_INET6;
1505 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1506 sizeof(udp6_addr.sin6_addr));
1507 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1508 err = kernel_connect(sock,
1509 (struct sockaddr *) &udp6_addr,
1510 sizeof(udp6_addr), 0);
1516 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
1520 sk_change_net(sock->sk, net);
1522 udp_addr.sin_family = AF_INET;
1523 udp_addr.sin_addr = cfg->local_ip;
1524 udp_addr.sin_port = htons(cfg->local_udp_port);
1525 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1530 udp_addr.sin_family = AF_INET;
1531 udp_addr.sin_addr = cfg->peer_ip;
1532 udp_addr.sin_port = htons(cfg->peer_udp_port);
1533 err = kernel_connect(sock,
1534 (struct sockaddr *) &udp_addr,
1535 sizeof(udp_addr), 0);
1540 if (!cfg->use_udp_checksums)
1541 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1545 case L2TP_ENCAPTYPE_IP:
1546 #if IS_ENABLED(CONFIG_IPV6)
1547 if (cfg->local_ip6 && cfg->peer_ip6) {
1548 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1549 IPPROTO_L2TP, &sock);
1553 sk_change_net(sock->sk, net);
1555 ip6_addr.l2tp_family = AF_INET6;
1556 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1557 sizeof(ip6_addr.l2tp_addr));
1558 ip6_addr.l2tp_conn_id = tunnel_id;
1559 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1564 ip6_addr.l2tp_family = AF_INET6;
1565 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1566 sizeof(ip6_addr.l2tp_addr));
1567 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1568 err = kernel_connect(sock,
1569 (struct sockaddr *) &ip6_addr,
1570 sizeof(ip6_addr), 0);
1576 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1577 IPPROTO_L2TP, &sock);
1581 sk_change_net(sock->sk, net);
1583 ip_addr.l2tp_family = AF_INET;
1584 ip_addr.l2tp_addr = cfg->local_ip;
1585 ip_addr.l2tp_conn_id = tunnel_id;
1586 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1591 ip_addr.l2tp_family = AF_INET;
1592 ip_addr.l2tp_addr = cfg->peer_ip;
1593 ip_addr.l2tp_conn_id = peer_tunnel_id;
1594 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1595 sizeof(ip_addr), 0);
1607 if ((err < 0) && sock) {
1608 kernel_sock_shutdown(sock, SHUT_RDWR);
1609 sk_release_kernel(sock->sk);
1616 static struct lock_class_key l2tp_socket_class;
1618 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)
1620 struct l2tp_tunnel *tunnel = NULL;
1622 struct socket *sock = NULL;
1623 struct sock *sk = NULL;
1624 struct l2tp_net *pn;
1625 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1627 /* Get the tunnel socket from the fd, which was opened by
1628 * the userspace L2TP daemon. If not specified, create a
1632 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1637 sock = sockfd_lookup(fd, &err);
1639 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1640 tunnel_id, fd, err);
1645 /* Reject namespace mismatches */
1646 if (!net_eq(sock_net(sock->sk), net)) {
1647 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1658 /* Quick sanity checks */
1660 case L2TP_ENCAPTYPE_UDP:
1661 err = -EPROTONOSUPPORT;
1662 if (sk->sk_protocol != IPPROTO_UDP) {
1663 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1664 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1668 case L2TP_ENCAPTYPE_IP:
1669 err = -EPROTONOSUPPORT;
1670 if (sk->sk_protocol != IPPROTO_L2TP) {
1671 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1672 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1678 /* Check if this socket has already been prepped */
1679 tunnel = l2tp_tunnel(sk);
1680 if (tunnel != NULL) {
1681 /* This socket has already been prepped */
1686 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1687 if (tunnel == NULL) {
1692 tunnel->version = version;
1693 tunnel->tunnel_id = tunnel_id;
1694 tunnel->peer_tunnel_id = peer_tunnel_id;
1695 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1697 tunnel->magic = L2TP_TUNNEL_MAGIC;
1698 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1699 rwlock_init(&tunnel->hlist_lock);
1701 /* The net we belong to */
1702 tunnel->l2tp_net = net;
1703 pn = l2tp_pernet(net);
1706 tunnel->debug = cfg->debug;
1708 #if IS_ENABLED(CONFIG_IPV6)
1709 if (sk->sk_family == PF_INET6) {
1710 struct ipv6_pinfo *np = inet6_sk(sk);
1712 if (ipv6_addr_v4mapped(&np->saddr) &&
1713 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
1714 struct inet_sock *inet = inet_sk(sk);
1716 tunnel->v4mapped = true;
1717 inet->inet_saddr = np->saddr.s6_addr32[3];
1718 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1719 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
1721 tunnel->v4mapped = false;
1726 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1727 tunnel->encap = encap;
1728 if (encap == L2TP_ENCAPTYPE_UDP) {
1729 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1730 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1731 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1732 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
1733 #if IS_ENABLED(CONFIG_IPV6)
1734 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1735 udpv6_encap_enable();
1741 sk->sk_user_data = tunnel;
1743 /* Hook on the tunnel socket destructor so that we can cleanup
1744 * if the tunnel socket goes away.
1746 tunnel->old_sk_destruct = sk->sk_destruct;
1747 sk->sk_destruct = &l2tp_tunnel_destruct;
1750 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1752 sk->sk_allocation = GFP_ATOMIC;
1754 /* Init delete workqueue struct */
1755 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1757 /* Add tunnel to our list */
1758 INIT_LIST_HEAD(&tunnel->list);
1759 atomic_inc(&l2tp_tunnel_count);
1761 /* Bump the reference count. The tunnel context is deleted
1762 * only when this drops to zero. Must be done before list insertion
1764 l2tp_tunnel_inc_refcount(tunnel);
1765 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1766 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1767 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1774 /* If tunnel's socket was created by the kernel, it doesn't
1777 if (sock && sock->file)
1782 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1784 /* This function is used by the netlink TUNNEL_DELETE command.
1786 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1788 l2tp_tunnel_closeall(tunnel);
1789 return (false == queue_work(l2tp_wq, &tunnel->del_work));
1791 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1793 /* Really kill the session.
1795 void l2tp_session_free(struct l2tp_session *session)
1797 struct l2tp_tunnel *tunnel = session->tunnel;
1799 BUG_ON(atomic_read(&session->ref_count) != 0);
1802 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1803 if (session->session_id != 0)
1804 atomic_dec(&l2tp_session_count);
1805 sock_put(tunnel->sock);
1806 session->tunnel = NULL;
1807 l2tp_tunnel_dec_refcount(tunnel);
1814 EXPORT_SYMBOL_GPL(l2tp_session_free);
1816 /* Remove an l2tp session from l2tp_core's hash lists.
1817 * Provides a tidyup interface for pseudowire code which can't just route all
1818 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1821 void __l2tp_session_unhash(struct l2tp_session *session)
1823 struct l2tp_tunnel *tunnel = session->tunnel;
1825 /* Remove the session from core hashes */
1827 /* Remove from the per-tunnel hash */
1828 write_lock_bh(&tunnel->hlist_lock);
1829 hlist_del_init(&session->hlist);
1830 write_unlock_bh(&tunnel->hlist_lock);
1832 /* For L2TPv3 we have a per-net hash: remove from there, too */
1833 if (tunnel->version != L2TP_HDR_VER_2) {
1834 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1835 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1836 hlist_del_init_rcu(&session->global_hlist);
1837 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1842 EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1844 /* This function is used by the netlink SESSION_DELETE command and by
1847 int l2tp_session_delete(struct l2tp_session *session)
1850 (*session->ref)(session);
1851 __l2tp_session_unhash(session);
1852 l2tp_session_queue_purge(session);
1853 if (session->session_close != NULL)
1854 (*session->session_close)(session);
1856 (*session->deref)(session);
1857 l2tp_session_dec_refcount(session);
1860 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1862 /* We come here whenever a session's send_seq, cookie_len or
1863 * l2specific_len parameters are set.
1865 void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1867 if (version == L2TP_HDR_VER_2) {
1868 session->hdr_len = 6;
1869 if (session->send_seq)
1870 session->hdr_len += 4;
1872 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1873 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1874 session->hdr_len += 4;
1878 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1880 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)
1882 struct l2tp_session *session;
1884 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1885 if (session != NULL) {
1886 session->magic = L2TP_SESSION_MAGIC;
1887 session->tunnel = tunnel;
1889 session->session_id = session_id;
1890 session->peer_session_id = peer_session_id;
1892 if (tunnel->version == L2TP_HDR_VER_2)
1893 session->nr_max = 0xffff;
1895 session->nr_max = 0xffffff;
1896 session->nr_window_size = session->nr_max / 2;
1897 session->nr_oos_count_max = 4;
1899 /* Use NR of first received packet */
1900 session->reorder_skip = 1;
1902 sprintf(&session->name[0], "sess %u/%u",
1903 tunnel->tunnel_id, session->session_id);
1905 skb_queue_head_init(&session->reorder_q);
1907 INIT_HLIST_NODE(&session->hlist);
1908 INIT_HLIST_NODE(&session->global_hlist);
1910 /* Inherit debug options from tunnel */
1911 session->debug = tunnel->debug;
1914 session->pwtype = cfg->pw_type;
1915 session->debug = cfg->debug;
1916 session->mtu = cfg->mtu;
1917 session->mru = cfg->mru;
1918 session->send_seq = cfg->send_seq;
1919 session->recv_seq = cfg->recv_seq;
1920 session->lns_mode = cfg->lns_mode;
1921 session->reorder_timeout = cfg->reorder_timeout;
1922 session->offset = cfg->offset;
1923 session->l2specific_type = cfg->l2specific_type;
1924 session->l2specific_len = cfg->l2specific_len;
1925 session->cookie_len = cfg->cookie_len;
1926 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1927 session->peer_cookie_len = cfg->peer_cookie_len;
1928 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1931 if (tunnel->version == L2TP_HDR_VER_2)
1932 session->build_header = l2tp_build_l2tpv2_header;
1934 session->build_header = l2tp_build_l2tpv3_header;
1936 l2tp_session_set_header_len(session, tunnel->version);
1938 /* Bump the reference count. The session context is deleted
1939 * only when this drops to zero.
1941 l2tp_session_inc_refcount(session);
1942 l2tp_tunnel_inc_refcount(tunnel);
1944 /* Ensure tunnel socket isn't deleted */
1945 sock_hold(tunnel->sock);
1947 /* Add session to the tunnel's hash list */
1948 write_lock_bh(&tunnel->hlist_lock);
1949 hlist_add_head(&session->hlist,
1950 l2tp_session_id_hash(tunnel, session_id));
1951 write_unlock_bh(&tunnel->hlist_lock);
1953 /* And to the global session list if L2TPv3 */
1954 if (tunnel->version != L2TP_HDR_VER_2) {
1955 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1957 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1958 hlist_add_head_rcu(&session->global_hlist,
1959 l2tp_session_id_hash_2(pn, session_id));
1960 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1963 /* Ignore management session in session count value */
1964 if (session->session_id != 0)
1965 atomic_inc(&l2tp_session_count);
1970 EXPORT_SYMBOL_GPL(l2tp_session_create);
1972 /*****************************************************************************
1974 *****************************************************************************/
1976 static __net_init int l2tp_init_net(struct net *net)
1978 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1981 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1982 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1984 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1985 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1987 spin_lock_init(&pn->l2tp_session_hlist_lock);
1992 static __net_exit void l2tp_exit_net(struct net *net)
1994 struct l2tp_net *pn = l2tp_pernet(net);
1995 struct l2tp_tunnel *tunnel = NULL;
1998 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1999 (void)l2tp_tunnel_delete(tunnel);
2001 rcu_read_unlock_bh();
2004 static struct pernet_operations l2tp_net_ops = {
2005 .init = l2tp_init_net,
2006 .exit = l2tp_exit_net,
2008 .size = sizeof(struct l2tp_net),
2011 static int __init l2tp_init(void)
2015 rc = register_pernet_device(&l2tp_net_ops);
2019 l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
2021 pr_err("alloc_workqueue failed\n");
2026 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
2032 static void __exit l2tp_exit(void)
2034 unregister_pernet_device(&l2tp_net_ops);
2036 destroy_workqueue(l2tp_wq);
2041 module_init(l2tp_init);
2042 module_exit(l2tp_exit);
2044 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
2045 MODULE_DESCRIPTION("L2TP core");
2046 MODULE_LICENSE("GPL");
2047 MODULE_VERSION(L2TP_DRV_VERSION);