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 int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
502 if (nr >= session->nr)
503 nws = nr - session->nr;
505 nws = (session->nr_max + 1) - (session->nr - nr);
507 return nws < session->nr_window_size;
510 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
511 * acceptable, else non-zero.
513 static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
515 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
516 /* Packet sequence number is outside allowed window.
519 l2tp_dbg(session, L2TP_MSG_SEQ,
520 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
521 session->name, L2TP_SKB_CB(skb)->ns,
522 L2TP_SKB_CB(skb)->length, session->nr);
526 if (session->reorder_timeout != 0) {
527 /* Packet reordering enabled. Add skb to session's
528 * reorder queue, in order of ns.
530 l2tp_recv_queue_skb(session, skb);
534 /* Packet reordering disabled. Discard out-of-sequence packets, while
535 * tracking the number if in-sequence packets after the first OOS packet
536 * is seen. After nr_oos_count_max in-sequence packets, reset the
537 * sequence number to re-enable packet reception.
539 if (L2TP_SKB_CB(skb)->ns == session->nr) {
540 skb_queue_tail(&session->reorder_q, skb);
542 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
543 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
545 if (nr_oos == nr_next)
546 session->nr_oos_count++;
548 session->nr_oos_count = 0;
550 session->nr_oos = nr_oos;
551 if (session->nr_oos_count > session->nr_oos_count_max) {
552 session->reorder_skip = 1;
553 l2tp_dbg(session, L2TP_MSG_SEQ,
554 "%s: %d oos packets received. Resetting sequence numbers\n",
555 session->name, session->nr_oos_count);
557 if (!session->reorder_skip) {
558 atomic_long_inc(&session->stats.rx_seq_discards);
559 l2tp_dbg(session, L2TP_MSG_SEQ,
560 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
561 session->name, L2TP_SKB_CB(skb)->ns,
562 L2TP_SKB_CB(skb)->length, session->nr,
563 skb_queue_len(&session->reorder_q));
566 skb_queue_tail(&session->reorder_q, skb);
576 /* Do receive processing of L2TP data frames. We handle both L2TPv2
577 * and L2TPv3 data frames here.
579 * L2TPv2 Data Message Header
582 * 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
583 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
584 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
585 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * | Tunnel ID | Session ID |
587 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 * | Ns (opt) | Nr (opt) |
589 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * | Offset Size (opt) | Offset pad... (opt)
591 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
593 * Data frames are marked by T=0. All other fields are the same as
594 * those in L2TP control frames.
596 * L2TPv3 Data Message Header
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
599 * | L2TP Session Header |
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 * | L2-Specific Sublayer |
602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603 * | Tunnel Payload ...
604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * L2TPv3 Session Header Over IP
609 * 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
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
613 * | Cookie (optional, maximum 64 bits)...
614 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
616 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
618 * L2TPv3 L2-Specific Sublayer Format
621 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
622 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623 * |x|S|x|x|x|x|x|x| Sequence Number |
624 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
626 * Cookie value, sublayer format and offset (pad) are negotiated with
627 * the peer when the session is set up. Unlike L2TPv2, we do not need
628 * to parse the packet header to determine if optional fields are
631 * Caller must already have parsed the frame and determined that it is
632 * a data (not control) frame before coming here. Fields up to the
633 * session-id have already been parsed and ptr points to the data
634 * after the session-id.
636 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
637 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
638 int length, int (*payload_hook)(struct sk_buff *skb))
640 struct l2tp_tunnel *tunnel = session->tunnel;
644 /* The ref count is increased since we now hold a pointer to
645 * the session. Take care to decrement the refcnt when exiting
646 * this function from now on...
648 l2tp_session_inc_refcount(session);
650 (*session->ref)(session);
652 /* Parse and check optional cookie */
653 if (session->peer_cookie_len > 0) {
654 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
655 l2tp_info(tunnel, L2TP_MSG_DATA,
656 "%s: cookie mismatch (%u/%u). Discarding.\n",
657 tunnel->name, tunnel->tunnel_id,
658 session->session_id);
659 atomic_long_inc(&session->stats.rx_cookie_discards);
662 ptr += session->peer_cookie_len;
665 /* Handle the optional sequence numbers. Sequence numbers are
666 * in different places for L2TPv2 and L2TPv3.
668 * If we are the LAC, enable/disable sequence numbers under
669 * the control of the LNS. If no sequence numbers present but
670 * we were expecting them, discard frame.
673 L2TP_SKB_CB(skb)->has_seq = 0;
674 if (tunnel->version == L2TP_HDR_VER_2) {
675 if (hdrflags & L2TP_HDRFLAG_S) {
676 ns = ntohs(*(__be16 *) ptr);
678 nr = ntohs(*(__be16 *) ptr);
681 /* Store L2TP info in the skb */
682 L2TP_SKB_CB(skb)->ns = ns;
683 L2TP_SKB_CB(skb)->has_seq = 1;
685 l2tp_dbg(session, L2TP_MSG_SEQ,
686 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
687 session->name, ns, nr, session->nr);
689 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
690 u32 l2h = ntohl(*(__be32 *) ptr);
692 if (l2h & 0x40000000) {
693 ns = l2h & 0x00ffffff;
695 /* Store L2TP info in the skb */
696 L2TP_SKB_CB(skb)->ns = ns;
697 L2TP_SKB_CB(skb)->has_seq = 1;
699 l2tp_dbg(session, L2TP_MSG_SEQ,
700 "%s: recv data ns=%u, session nr=%u\n",
701 session->name, ns, session->nr);
705 /* Advance past L2-specific header, if present */
706 ptr += session->l2specific_len;
708 if (L2TP_SKB_CB(skb)->has_seq) {
709 /* Received a packet with sequence numbers. If we're the LNS,
710 * check if we sre sending sequence numbers and if not,
713 if ((!session->lns_mode) && (!session->send_seq)) {
714 l2tp_info(session, L2TP_MSG_SEQ,
715 "%s: requested to enable seq numbers by LNS\n",
717 session->send_seq = -1;
718 l2tp_session_set_header_len(session, tunnel->version);
721 /* No sequence numbers.
722 * If user has configured mandatory sequence numbers, discard.
724 if (session->recv_seq) {
725 l2tp_warn(session, L2TP_MSG_SEQ,
726 "%s: recv data has no seq numbers when required. Discarding.\n",
728 atomic_long_inc(&session->stats.rx_seq_discards);
732 /* If we're the LAC and we're sending sequence numbers, the
733 * LNS has requested that we no longer send sequence numbers.
734 * If we're the LNS and we're sending sequence numbers, the
735 * LAC is broken. Discard the frame.
737 if ((!session->lns_mode) && (session->send_seq)) {
738 l2tp_info(session, L2TP_MSG_SEQ,
739 "%s: requested to disable seq numbers by LNS\n",
741 session->send_seq = 0;
742 l2tp_session_set_header_len(session, tunnel->version);
743 } else if (session->send_seq) {
744 l2tp_warn(session, L2TP_MSG_SEQ,
745 "%s: recv data has no seq numbers when required. Discarding.\n",
747 atomic_long_inc(&session->stats.rx_seq_discards);
752 /* Session data offset is handled differently for L2TPv2 and
753 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
754 * the header. For L2TPv3, the offset is negotiated using AVPs
755 * in the session setup control protocol.
757 if (tunnel->version == L2TP_HDR_VER_2) {
758 /* If offset bit set, skip it. */
759 if (hdrflags & L2TP_HDRFLAG_O) {
760 offset = ntohs(*(__be16 *)ptr);
764 ptr += session->offset;
767 if (!pskb_may_pull(skb, offset))
770 __skb_pull(skb, offset);
772 /* If caller wants to process the payload before we queue the
776 if ((*payload_hook)(skb))
779 /* Prepare skb for adding to the session's reorder_q. Hold
780 * packets for max reorder_timeout or 1 second if not
783 L2TP_SKB_CB(skb)->length = length;
784 L2TP_SKB_CB(skb)->expires = jiffies +
785 (session->reorder_timeout ? session->reorder_timeout : HZ);
787 /* Add packet to the session's receive queue. Reordering is done here, if
788 * enabled. Saved L2TP protocol info is stored in skb->sb[].
790 if (L2TP_SKB_CB(skb)->has_seq) {
791 if (l2tp_recv_data_seq(session, skb))
794 /* No sequence numbers. Add the skb to the tail of the
795 * reorder queue. This ensures that it will be
796 * delivered after all previous sequenced skbs.
798 skb_queue_tail(&session->reorder_q, skb);
801 /* Try to dequeue as many skbs from reorder_q as we can. */
802 l2tp_recv_dequeue(session);
804 l2tp_session_dec_refcount(session);
809 atomic_long_inc(&session->stats.rx_errors);
813 (*session->deref)(session);
815 l2tp_session_dec_refcount(session);
817 EXPORT_SYMBOL(l2tp_recv_common);
819 /* Drop skbs from the session's reorder_q
821 int l2tp_session_queue_purge(struct l2tp_session *session)
823 struct sk_buff *skb = NULL;
825 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
826 while ((skb = skb_dequeue(&session->reorder_q))) {
827 atomic_long_inc(&session->stats.rx_errors);
830 (*session->deref)(session);
834 EXPORT_SYMBOL_GPL(l2tp_session_queue_purge);
836 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
837 * here. The skb is not on a list when we get here.
838 * Returns 0 if the packet was a data packet and was successfully passed on.
839 * Returns 1 if the packet was not a good data packet and could not be
840 * forwarded. All such packets are passed up to userspace to deal with.
842 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
843 int (*payload_hook)(struct sk_buff *skb))
845 struct l2tp_session *session = NULL;
846 unsigned char *ptr, *optr;
848 u32 tunnel_id, session_id;
852 /* UDP has verifed checksum */
854 /* UDP always verifies the packet length. */
855 __skb_pull(skb, sizeof(struct udphdr));
858 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
859 l2tp_info(tunnel, L2TP_MSG_DATA,
860 "%s: recv short packet (len=%d)\n",
861 tunnel->name, skb->len);
865 /* Trace packet contents, if enabled */
866 if (tunnel->debug & L2TP_MSG_DATA) {
867 length = min(32u, skb->len);
868 if (!pskb_may_pull(skb, length))
871 pr_debug("%s: recv\n", tunnel->name);
872 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
875 /* Point to L2TP header */
876 optr = ptr = skb->data;
878 /* Get L2TP header flags */
879 hdrflags = ntohs(*(__be16 *) ptr);
881 /* Check protocol version */
882 version = hdrflags & L2TP_HDR_VER_MASK;
883 if (version != tunnel->version) {
884 l2tp_info(tunnel, L2TP_MSG_DATA,
885 "%s: recv protocol version mismatch: got %d expected %d\n",
886 tunnel->name, version, tunnel->version);
890 /* Get length of L2TP packet */
893 /* If type is control packet, it is handled by userspace. */
894 if (hdrflags & L2TP_HDRFLAG_T) {
895 l2tp_dbg(tunnel, L2TP_MSG_DATA,
896 "%s: recv control packet, len=%d\n",
897 tunnel->name, length);
904 if (tunnel->version == L2TP_HDR_VER_2) {
905 /* If length is present, skip it */
906 if (hdrflags & L2TP_HDRFLAG_L)
909 /* Extract tunnel and session ID */
910 tunnel_id = ntohs(*(__be16 *) ptr);
912 session_id = ntohs(*(__be16 *) ptr);
915 ptr += 2; /* skip reserved bits */
916 tunnel_id = tunnel->tunnel_id;
917 session_id = ntohl(*(__be32 *) ptr);
921 /* Find the session context */
922 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
923 if (!session || !session->recv_skb) {
924 /* Not found? Pass to userspace to deal with */
925 l2tp_info(tunnel, L2TP_MSG_DATA,
926 "%s: no session found (%u/%u). Passing up.\n",
927 tunnel->name, tunnel_id, session_id);
931 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
936 /* Put UDP header back */
937 __skb_push(skb, sizeof(struct udphdr));
942 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
946 * >0: skb should be passed up to userspace as UDP.
948 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
950 struct l2tp_tunnel *tunnel;
952 tunnel = l2tp_sock_to_tunnel(sk);
956 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
957 tunnel->name, skb->len);
959 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
970 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
972 /************************************************************************
974 ***********************************************************************/
976 /* Build an L2TP header for the session into the buffer provided.
978 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
980 struct l2tp_tunnel *tunnel = session->tunnel;
983 u16 flags = L2TP_HDR_VER_2;
984 u32 tunnel_id = tunnel->peer_tunnel_id;
985 u32 session_id = session->peer_session_id;
987 if (session->send_seq)
988 flags |= L2TP_HDRFLAG_S;
990 /* Setup L2TP header. */
991 *bufp++ = htons(flags);
992 *bufp++ = htons(tunnel_id);
993 *bufp++ = htons(session_id);
994 if (session->send_seq) {
995 *bufp++ = htons(session->ns);
998 session->ns &= 0xffff;
999 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
1000 session->name, session->ns);
1006 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1008 struct l2tp_tunnel *tunnel = session->tunnel;
1012 /* Setup L2TP header. The header differs slightly for UDP and
1013 * IP encapsulations. For UDP, there is 4 bytes of flags.
1015 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1016 u16 flags = L2TP_HDR_VER_3;
1017 *((__be16 *) bufp) = htons(flags);
1019 *((__be16 *) bufp) = 0;
1023 *((__be32 *) bufp) = htonl(session->peer_session_id);
1025 if (session->cookie_len) {
1026 memcpy(bufp, &session->cookie[0], session->cookie_len);
1027 bufp += session->cookie_len;
1029 if (session->l2specific_len) {
1030 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
1032 if (session->send_seq) {
1033 l2h = 0x40000000 | session->ns;
1035 session->ns &= 0xffffff;
1036 l2tp_dbg(session, L2TP_MSG_SEQ,
1037 "%s: updated ns to %u\n",
1038 session->name, session->ns);
1041 *((__be32 *) bufp) = htonl(l2h);
1043 bufp += session->l2specific_len;
1045 if (session->offset)
1046 bufp += session->offset;
1051 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1052 struct flowi *fl, size_t data_len)
1054 struct l2tp_tunnel *tunnel = session->tunnel;
1055 unsigned int len = skb->len;
1059 if (session->send_seq)
1060 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
1061 session->name, data_len, session->ns - 1);
1063 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
1064 session->name, data_len);
1066 if (session->debug & L2TP_MSG_DATA) {
1067 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1068 unsigned char *datap = skb->data + uhlen;
1070 pr_debug("%s: xmit\n", session->name);
1071 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1072 datap, min_t(size_t, 32, len - uhlen));
1075 /* Queue the packet to IP for output */
1077 #if IS_ENABLED(CONFIG_IPV6)
1078 if (tunnel->sock->sk_family == PF_INET6 && !tunnel->v4mapped)
1079 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
1082 error = ip_queue_xmit(tunnel->sock, skb, fl);
1086 atomic_long_inc(&tunnel->stats.tx_packets);
1087 atomic_long_add(len, &tunnel->stats.tx_bytes);
1088 atomic_long_inc(&session->stats.tx_packets);
1089 atomic_long_add(len, &session->stats.tx_bytes);
1091 atomic_long_inc(&tunnel->stats.tx_errors);
1092 atomic_long_inc(&session->stats.tx_errors);
1098 #if IS_ENABLED(CONFIG_IPV6)
1099 static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1102 struct ipv6_pinfo *np = inet6_sk(sk);
1103 struct udphdr *uh = udp_hdr(skb);
1105 if (udp_get_no_check6_tx(sk))
1106 skb->ip_summed = CHECKSUM_NONE;
1107 else if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1108 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1109 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1110 skb->ip_summed = CHECKSUM_UNNECESSARY;
1111 uh->check = csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr, udp_len,
1114 uh->check = CSUM_MANGLED_0;
1116 skb->ip_summed = CHECKSUM_PARTIAL;
1117 skb->csum_start = skb_transport_header(skb) - skb->head;
1118 skb->csum_offset = offsetof(struct udphdr, check);
1119 uh->check = ~csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr,
1120 udp_len, IPPROTO_UDP, 0);
1125 /* If caller requires the skb to have a ppp header, the header must be
1126 * inserted in the skb data before calling this function.
1128 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1130 int data_len = skb->len;
1131 struct l2tp_tunnel *tunnel = session->tunnel;
1132 struct sock *sk = tunnel->sock;
1135 struct inet_sock *inet;
1138 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1140 int ret = NET_XMIT_SUCCESS;
1142 /* Check that there's enough headroom in the skb to insert IP,
1143 * UDP and L2TP headers. If not enough, expand it to
1144 * make room. Adjust truesize.
1146 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1148 if (skb_cow_head(skb, headroom)) {
1150 return NET_XMIT_DROP;
1153 /* Setup L2TP header */
1154 session->build_header(session, __skb_push(skb, hdr_len));
1156 /* Reset skb netfilter state */
1157 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1158 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1163 if (sock_owned_by_user(sk)) {
1165 ret = NET_XMIT_DROP;
1169 /* Get routing info from the tunnel socket */
1171 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
1174 fl = &inet->cork.fl;
1175 switch (tunnel->encap) {
1176 case L2TP_ENCAPTYPE_UDP:
1177 /* Setup UDP header */
1178 __skb_push(skb, sizeof(*uh));
1179 skb_reset_transport_header(skb);
1181 uh->source = inet->inet_sport;
1182 uh->dest = inet->inet_dport;
1183 udp_len = uhlen + hdr_len + data_len;
1184 uh->len = htons(udp_len);
1187 /* Calculate UDP checksum if configured to do so */
1188 #if IS_ENABLED(CONFIG_IPV6)
1189 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1190 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1193 if (sk->sk_no_check_tx)
1194 skb->ip_summed = CHECKSUM_NONE;
1195 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1196 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1197 skb->ip_summed = CHECKSUM_COMPLETE;
1198 csum = skb_checksum(skb, 0, udp_len, 0);
1199 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1201 udp_len, IPPROTO_UDP, csum);
1203 uh->check = CSUM_MANGLED_0;
1205 skb->ip_summed = CHECKSUM_PARTIAL;
1206 skb->csum_start = skb_transport_header(skb) - skb->head;
1207 skb->csum_offset = offsetof(struct udphdr, check);
1208 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1210 udp_len, IPPROTO_UDP, 0);
1214 case L2TP_ENCAPTYPE_IP:
1218 l2tp_xmit_core(session, skb, fl, data_len);
1224 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1226 /*****************************************************************************
1227 * Tinnel and session create/destroy.
1228 *****************************************************************************/
1230 /* Tunnel socket destruct hook.
1231 * The tunnel context is deleted only when all session sockets have been
1234 static void l2tp_tunnel_destruct(struct sock *sk)
1236 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1237 struct l2tp_net *pn;
1242 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1245 /* Disable udp encapsulation */
1246 switch (tunnel->encap) {
1247 case L2TP_ENCAPTYPE_UDP:
1248 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1249 (udp_sk(sk))->encap_type = 0;
1250 (udp_sk(sk))->encap_rcv = NULL;
1251 (udp_sk(sk))->encap_destroy = NULL;
1253 case L2TP_ENCAPTYPE_IP:
1257 /* Remove hooks into tunnel socket */
1258 sk->sk_destruct = tunnel->old_sk_destruct;
1259 sk->sk_user_data = NULL;
1260 tunnel->sock = NULL;
1262 /* Remove the tunnel struct from the tunnel list */
1263 pn = l2tp_pernet(tunnel->l2tp_net);
1264 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1265 list_del_rcu(&tunnel->list);
1266 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1267 atomic_dec(&l2tp_tunnel_count);
1269 l2tp_tunnel_closeall(tunnel);
1270 l2tp_tunnel_dec_refcount(tunnel);
1272 /* Call the original destructor */
1273 if (sk->sk_destruct)
1274 (*sk->sk_destruct)(sk);
1279 /* When the tunnel is closed, all the attached sessions need to go too.
1281 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1284 struct hlist_node *walk;
1285 struct hlist_node *tmp;
1286 struct l2tp_session *session;
1288 BUG_ON(tunnel == NULL);
1290 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1293 write_lock_bh(&tunnel->hlist_lock);
1294 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1296 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1297 session = hlist_entry(walk, struct l2tp_session, hlist);
1299 l2tp_info(session, L2TP_MSG_CONTROL,
1300 "%s: closing session\n", session->name);
1302 hlist_del_init(&session->hlist);
1304 if (session->ref != NULL)
1305 (*session->ref)(session);
1307 write_unlock_bh(&tunnel->hlist_lock);
1309 __l2tp_session_unhash(session);
1310 l2tp_session_queue_purge(session);
1312 if (session->session_close != NULL)
1313 (*session->session_close)(session);
1315 if (session->deref != NULL)
1316 (*session->deref)(session);
1318 l2tp_session_dec_refcount(session);
1320 write_lock_bh(&tunnel->hlist_lock);
1322 /* Now restart from the beginning of this hash
1323 * chain. We always remove a session from the
1324 * list so we are guaranteed to make forward
1330 write_unlock_bh(&tunnel->hlist_lock);
1332 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1334 /* Tunnel socket destroy hook for UDP encapsulation */
1335 static void l2tp_udp_encap_destroy(struct sock *sk)
1337 struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
1339 l2tp_tunnel_closeall(tunnel);
1344 /* Really kill the tunnel.
1345 * Come here only when all sessions have been cleared from the tunnel.
1347 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1349 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1350 BUG_ON(tunnel->sock != NULL);
1351 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
1352 kfree_rcu(tunnel, rcu);
1355 /* Workqueue tunnel deletion function */
1356 static void l2tp_tunnel_del_work(struct work_struct *work)
1358 struct l2tp_tunnel *tunnel = NULL;
1359 struct socket *sock = NULL;
1360 struct sock *sk = NULL;
1362 tunnel = container_of(work, struct l2tp_tunnel, del_work);
1363 sk = l2tp_tunnel_sock_lookup(tunnel);
1367 sock = sk->sk_socket;
1369 /* If the tunnel socket was created by userspace, then go through the
1370 * inet layer to shut the socket down, and let userspace close it.
1371 * Otherwise, if we created the socket directly within the kernel, use
1372 * the sk API to release it here.
1373 * In either case the tunnel resources are freed in the socket
1374 * destructor when the tunnel socket goes away.
1376 if (tunnel->fd >= 0) {
1378 inet_shutdown(sock, 2);
1381 kernel_sock_shutdown(sock, SHUT_RDWR);
1382 sk_release_kernel(sk);
1385 l2tp_tunnel_sock_put(sk);
1388 /* Create a socket for the tunnel, if one isn't set up by
1389 * userspace. This is used for static tunnels where there is no
1390 * managing L2TP daemon.
1392 * Since we don't want these sockets to keep a namespace alive by
1393 * themselves, we drop the socket's namespace refcount after creation.
1394 * These sockets are freed when the namespace exits using the pernet
1397 static int l2tp_tunnel_sock_create(struct net *net,
1400 struct l2tp_tunnel_cfg *cfg,
1401 struct socket **sockp)
1404 struct socket *sock = NULL;
1405 struct sockaddr_in udp_addr = {0};
1406 struct sockaddr_l2tpip ip_addr = {0};
1407 #if IS_ENABLED(CONFIG_IPV6)
1408 struct sockaddr_in6 udp6_addr = {0};
1409 struct sockaddr_l2tpip6 ip6_addr = {0};
1412 switch (cfg->encap) {
1413 case L2TP_ENCAPTYPE_UDP:
1414 #if IS_ENABLED(CONFIG_IPV6)
1415 if (cfg->local_ip6 && cfg->peer_ip6) {
1416 err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
1420 sk_change_net(sock->sk, net);
1422 udp6_addr.sin6_family = AF_INET6;
1423 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1424 sizeof(udp6_addr.sin6_addr));
1425 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1426 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1431 udp6_addr.sin6_family = AF_INET6;
1432 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1433 sizeof(udp6_addr.sin6_addr));
1434 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1435 err = kernel_connect(sock,
1436 (struct sockaddr *) &udp6_addr,
1437 sizeof(udp6_addr), 0);
1441 if (cfg->udp6_zero_tx_checksums)
1442 udp_set_no_check6_tx(sock->sk, true);
1443 if (cfg->udp6_zero_rx_checksums)
1444 udp_set_no_check6_rx(sock->sk, true);
1448 err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
1452 sk_change_net(sock->sk, net);
1454 udp_addr.sin_family = AF_INET;
1455 udp_addr.sin_addr = cfg->local_ip;
1456 udp_addr.sin_port = htons(cfg->local_udp_port);
1457 err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
1462 udp_addr.sin_family = AF_INET;
1463 udp_addr.sin_addr = cfg->peer_ip;
1464 udp_addr.sin_port = htons(cfg->peer_udp_port);
1465 err = kernel_connect(sock,
1466 (struct sockaddr *) &udp_addr,
1467 sizeof(udp_addr), 0);
1472 if (!cfg->use_udp_checksums)
1473 sock->sk->sk_no_check_tx = 1;
1477 case L2TP_ENCAPTYPE_IP:
1478 #if IS_ENABLED(CONFIG_IPV6)
1479 if (cfg->local_ip6 && cfg->peer_ip6) {
1480 err = sock_create_kern(AF_INET6, SOCK_DGRAM,
1481 IPPROTO_L2TP, &sock);
1485 sk_change_net(sock->sk, net);
1487 ip6_addr.l2tp_family = AF_INET6;
1488 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1489 sizeof(ip6_addr.l2tp_addr));
1490 ip6_addr.l2tp_conn_id = tunnel_id;
1491 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1496 ip6_addr.l2tp_family = AF_INET6;
1497 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1498 sizeof(ip6_addr.l2tp_addr));
1499 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1500 err = kernel_connect(sock,
1501 (struct sockaddr *) &ip6_addr,
1502 sizeof(ip6_addr), 0);
1508 err = sock_create_kern(AF_INET, SOCK_DGRAM,
1509 IPPROTO_L2TP, &sock);
1513 sk_change_net(sock->sk, net);
1515 ip_addr.l2tp_family = AF_INET;
1516 ip_addr.l2tp_addr = cfg->local_ip;
1517 ip_addr.l2tp_conn_id = tunnel_id;
1518 err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
1523 ip_addr.l2tp_family = AF_INET;
1524 ip_addr.l2tp_addr = cfg->peer_ip;
1525 ip_addr.l2tp_conn_id = peer_tunnel_id;
1526 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1527 sizeof(ip_addr), 0);
1539 if ((err < 0) && sock) {
1540 kernel_sock_shutdown(sock, SHUT_RDWR);
1541 sk_release_kernel(sock->sk);
1548 static struct lock_class_key l2tp_socket_class;
1550 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)
1552 struct l2tp_tunnel *tunnel = NULL;
1554 struct socket *sock = NULL;
1555 struct sock *sk = NULL;
1556 struct l2tp_net *pn;
1557 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1559 /* Get the tunnel socket from the fd, which was opened by
1560 * the userspace L2TP daemon. If not specified, create a
1564 err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
1569 sock = sockfd_lookup(fd, &err);
1571 pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
1572 tunnel_id, fd, err);
1577 /* Reject namespace mismatches */
1578 if (!net_eq(sock_net(sock->sk), net)) {
1579 pr_err("tunl %u: netns mismatch\n", tunnel_id);
1590 /* Quick sanity checks */
1592 case L2TP_ENCAPTYPE_UDP:
1593 err = -EPROTONOSUPPORT;
1594 if (sk->sk_protocol != IPPROTO_UDP) {
1595 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1596 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1600 case L2TP_ENCAPTYPE_IP:
1601 err = -EPROTONOSUPPORT;
1602 if (sk->sk_protocol != IPPROTO_L2TP) {
1603 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1604 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1610 /* Check if this socket has already been prepped */
1611 tunnel = l2tp_tunnel(sk);
1612 if (tunnel != NULL) {
1613 /* This socket has already been prepped */
1618 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1619 if (tunnel == NULL) {
1624 tunnel->version = version;
1625 tunnel->tunnel_id = tunnel_id;
1626 tunnel->peer_tunnel_id = peer_tunnel_id;
1627 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1629 tunnel->magic = L2TP_TUNNEL_MAGIC;
1630 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1631 rwlock_init(&tunnel->hlist_lock);
1633 /* The net we belong to */
1634 tunnel->l2tp_net = net;
1635 pn = l2tp_pernet(net);
1638 tunnel->debug = cfg->debug;
1640 #if IS_ENABLED(CONFIG_IPV6)
1641 if (sk->sk_family == PF_INET6) {
1642 struct ipv6_pinfo *np = inet6_sk(sk);
1644 if (ipv6_addr_v4mapped(&np->saddr) &&
1645 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
1646 struct inet_sock *inet = inet_sk(sk);
1648 tunnel->v4mapped = true;
1649 inet->inet_saddr = np->saddr.s6_addr32[3];
1650 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1651 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
1653 tunnel->v4mapped = false;
1658 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1659 tunnel->encap = encap;
1660 if (encap == L2TP_ENCAPTYPE_UDP) {
1661 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1662 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1663 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1664 udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
1665 #if IS_ENABLED(CONFIG_IPV6)
1666 if (sk->sk_family == PF_INET6 && !tunnel->v4mapped)
1667 udpv6_encap_enable();
1673 sk->sk_user_data = tunnel;
1675 /* Hook on the tunnel socket destructor so that we can cleanup
1676 * if the tunnel socket goes away.
1678 tunnel->old_sk_destruct = sk->sk_destruct;
1679 sk->sk_destruct = &l2tp_tunnel_destruct;
1682 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1684 sk->sk_allocation = GFP_ATOMIC;
1686 /* Init delete workqueue struct */
1687 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1689 /* Add tunnel to our list */
1690 INIT_LIST_HEAD(&tunnel->list);
1691 atomic_inc(&l2tp_tunnel_count);
1693 /* Bump the reference count. The tunnel context is deleted
1694 * only when this drops to zero. Must be done before list insertion
1696 l2tp_tunnel_inc_refcount(tunnel);
1697 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1698 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1699 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1706 /* If tunnel's socket was created by the kernel, it doesn't
1709 if (sock && sock->file)
1714 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1716 /* This function is used by the netlink TUNNEL_DELETE command.
1718 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1720 l2tp_tunnel_closeall(tunnel);
1721 return (false == queue_work(l2tp_wq, &tunnel->del_work));
1723 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1725 /* Really kill the session.
1727 void l2tp_session_free(struct l2tp_session *session)
1729 struct l2tp_tunnel *tunnel = session->tunnel;
1731 BUG_ON(atomic_read(&session->ref_count) != 0);
1734 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1735 if (session->session_id != 0)
1736 atomic_dec(&l2tp_session_count);
1737 sock_put(tunnel->sock);
1738 session->tunnel = NULL;
1739 l2tp_tunnel_dec_refcount(tunnel);
1744 EXPORT_SYMBOL_GPL(l2tp_session_free);
1746 /* Remove an l2tp session from l2tp_core's hash lists.
1747 * Provides a tidyup interface for pseudowire code which can't just route all
1748 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1751 void __l2tp_session_unhash(struct l2tp_session *session)
1753 struct l2tp_tunnel *tunnel = session->tunnel;
1755 /* Remove the session from core hashes */
1757 /* Remove from the per-tunnel hash */
1758 write_lock_bh(&tunnel->hlist_lock);
1759 hlist_del_init(&session->hlist);
1760 write_unlock_bh(&tunnel->hlist_lock);
1762 /* For L2TPv3 we have a per-net hash: remove from there, too */
1763 if (tunnel->version != L2TP_HDR_VER_2) {
1764 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1765 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1766 hlist_del_init_rcu(&session->global_hlist);
1767 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1772 EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1774 /* This function is used by the netlink SESSION_DELETE command and by
1777 int l2tp_session_delete(struct l2tp_session *session)
1780 (*session->ref)(session);
1781 __l2tp_session_unhash(session);
1782 l2tp_session_queue_purge(session);
1783 if (session->session_close != NULL)
1784 (*session->session_close)(session);
1786 (*session->deref)(session);
1787 l2tp_session_dec_refcount(session);
1790 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1792 /* We come here whenever a session's send_seq, cookie_len or
1793 * l2specific_len parameters are set.
1795 void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1797 if (version == L2TP_HDR_VER_2) {
1798 session->hdr_len = 6;
1799 if (session->send_seq)
1800 session->hdr_len += 4;
1802 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1803 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1804 session->hdr_len += 4;
1808 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1810 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)
1812 struct l2tp_session *session;
1814 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1815 if (session != NULL) {
1816 session->magic = L2TP_SESSION_MAGIC;
1817 session->tunnel = tunnel;
1819 session->session_id = session_id;
1820 session->peer_session_id = peer_session_id;
1822 if (tunnel->version == L2TP_HDR_VER_2)
1823 session->nr_max = 0xffff;
1825 session->nr_max = 0xffffff;
1826 session->nr_window_size = session->nr_max / 2;
1827 session->nr_oos_count_max = 4;
1829 /* Use NR of first received packet */
1830 session->reorder_skip = 1;
1832 sprintf(&session->name[0], "sess %u/%u",
1833 tunnel->tunnel_id, session->session_id);
1835 skb_queue_head_init(&session->reorder_q);
1837 INIT_HLIST_NODE(&session->hlist);
1838 INIT_HLIST_NODE(&session->global_hlist);
1840 /* Inherit debug options from tunnel */
1841 session->debug = tunnel->debug;
1844 session->pwtype = cfg->pw_type;
1845 session->debug = cfg->debug;
1846 session->mtu = cfg->mtu;
1847 session->mru = cfg->mru;
1848 session->send_seq = cfg->send_seq;
1849 session->recv_seq = cfg->recv_seq;
1850 session->lns_mode = cfg->lns_mode;
1851 session->reorder_timeout = cfg->reorder_timeout;
1852 session->offset = cfg->offset;
1853 session->l2specific_type = cfg->l2specific_type;
1854 session->l2specific_len = cfg->l2specific_len;
1855 session->cookie_len = cfg->cookie_len;
1856 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1857 session->peer_cookie_len = cfg->peer_cookie_len;
1858 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1861 if (tunnel->version == L2TP_HDR_VER_2)
1862 session->build_header = l2tp_build_l2tpv2_header;
1864 session->build_header = l2tp_build_l2tpv3_header;
1866 l2tp_session_set_header_len(session, tunnel->version);
1868 /* Bump the reference count. The session context is deleted
1869 * only when this drops to zero.
1871 l2tp_session_inc_refcount(session);
1872 l2tp_tunnel_inc_refcount(tunnel);
1874 /* Ensure tunnel socket isn't deleted */
1875 sock_hold(tunnel->sock);
1877 /* Add session to the tunnel's hash list */
1878 write_lock_bh(&tunnel->hlist_lock);
1879 hlist_add_head(&session->hlist,
1880 l2tp_session_id_hash(tunnel, session_id));
1881 write_unlock_bh(&tunnel->hlist_lock);
1883 /* And to the global session list if L2TPv3 */
1884 if (tunnel->version != L2TP_HDR_VER_2) {
1885 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1887 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1888 hlist_add_head_rcu(&session->global_hlist,
1889 l2tp_session_id_hash_2(pn, session_id));
1890 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1893 /* Ignore management session in session count value */
1894 if (session->session_id != 0)
1895 atomic_inc(&l2tp_session_count);
1900 EXPORT_SYMBOL_GPL(l2tp_session_create);
1902 /*****************************************************************************
1904 *****************************************************************************/
1906 static __net_init int l2tp_init_net(struct net *net)
1908 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1911 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1912 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1914 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1915 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1917 spin_lock_init(&pn->l2tp_session_hlist_lock);
1922 static __net_exit void l2tp_exit_net(struct net *net)
1924 struct l2tp_net *pn = l2tp_pernet(net);
1925 struct l2tp_tunnel *tunnel = NULL;
1928 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1929 (void)l2tp_tunnel_delete(tunnel);
1931 rcu_read_unlock_bh();
1934 static struct pernet_operations l2tp_net_ops = {
1935 .init = l2tp_init_net,
1936 .exit = l2tp_exit_net,
1938 .size = sizeof(struct l2tp_net),
1941 static int __init l2tp_init(void)
1945 rc = register_pernet_device(&l2tp_net_ops);
1949 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1951 pr_err("alloc_workqueue failed\n");
1956 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1962 static void __exit l2tp_exit(void)
1964 unregister_pernet_device(&l2tp_net_ops);
1966 destroy_workqueue(l2tp_wq);
1971 module_init(l2tp_init);
1972 module_exit(l2tp_exit);
1974 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1975 MODULE_DESCRIPTION("L2TP core");
1976 MODULE_LICENSE("GPL");
1977 MODULE_VERSION(L2TP_DRV_VERSION);