1 // SPDX-License-Identifier: GPL-2.0-only
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)
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/string.h>
21 #include <linux/list.h>
22 #include <linux/rculist.h>
23 #include <linux/uaccess.h>
25 #include <linux/kernel.h>
26 #include <linux/spinlock.h>
27 #include <linux/kthread.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/errno.h>
31 #include <linux/jiffies.h>
33 #include <linux/netdevice.h>
34 #include <linux/net.h>
35 #include <linux/inetdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/init.h>
40 #include <linux/udp.h>
41 #include <linux/l2tp.h>
42 #include <linux/hash.h>
43 #include <linux/sort.h>
44 #include <linux/file.h>
45 #include <linux/nsproxy.h>
46 #include <net/net_namespace.h>
47 #include <net/netns/generic.h>
51 #include <net/udp_tunnel.h>
52 #include <net/inet_common.h>
54 #include <net/protocol.h>
55 #include <net/inet6_connection_sock.h>
56 #include <net/inet_ecn.h>
57 #include <net/ip6_route.h>
58 #include <net/ip6_checksum.h>
60 #include <asm/byteorder.h>
61 #include <linux/atomic.h>
63 #include "l2tp_core.h"
65 #define L2TP_DRV_VERSION "V2.0"
67 /* L2TP header constants */
68 #define L2TP_HDRFLAG_T 0x8000
69 #define L2TP_HDRFLAG_L 0x4000
70 #define L2TP_HDRFLAG_S 0x0800
71 #define L2TP_HDRFLAG_O 0x0200
72 #define L2TP_HDRFLAG_P 0x0100
74 #define L2TP_HDR_VER_MASK 0x000F
75 #define L2TP_HDR_VER_2 0x0002
76 #define L2TP_HDR_VER_3 0x0003
78 /* L2TPv3 default L2-specific sublayer */
79 #define L2TP_SLFLAG_S 0x40000000
80 #define L2TP_SL_SEQ_MASK 0x00ffffff
82 #define L2TP_HDR_SIZE_MAX 14
84 /* Default trace flags */
85 #define L2TP_DEFAULT_DEBUG_FLAGS 0
87 /* Private data stored for received packets in the skb.
93 unsigned long expires;
96 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *)&(skb)->cb[sizeof(struct inet_skb_parm)])
98 static struct workqueue_struct *l2tp_wq;
100 /* per-net private data for this module */
101 static unsigned int l2tp_net_id;
103 struct list_head l2tp_tunnel_list;
104 /* Lock for write access to l2tp_tunnel_list */
105 spinlock_t l2tp_tunnel_list_lock;
106 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
107 /* Lock for write access to l2tp_session_hlist */
108 spinlock_t l2tp_session_hlist_lock;
111 #if IS_ENABLED(CONFIG_IPV6)
112 static bool l2tp_sk_is_v6(struct sock *sk)
114 return sk->sk_family == PF_INET6 &&
115 !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
119 static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
121 return sk->sk_user_data;
124 static inline struct l2tp_net *l2tp_pernet(const struct net *net)
126 return net_generic(net, l2tp_net_id);
129 /* Session hash global list for L2TPv3.
130 * The session_id SHOULD be random according to RFC3931, but several
131 * L2TP implementations use incrementing session_ids. So we do a real
132 * hash on the session_id, rather than a simple bitmask.
134 static inline struct hlist_head *
135 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
137 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
140 /* Session hash list.
141 * The session_id SHOULD be random according to RFC2661, but several
142 * L2TP implementations (Cisco and Microsoft) use incrementing
143 * session_ids. So we do a real hash on the session_id, rather than a
146 static inline struct hlist_head *
147 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
149 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
152 void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
154 sock_put(tunnel->sock);
155 /* the tunnel is freed in the socket destructor */
157 EXPORT_SYMBOL(l2tp_tunnel_free);
159 /* Lookup a tunnel. A new reference is held on the returned tunnel. */
160 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
162 const struct l2tp_net *pn = l2tp_pernet(net);
163 struct l2tp_tunnel *tunnel;
166 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
167 if (tunnel->tunnel_id == tunnel_id &&
168 refcount_inc_not_zero(&tunnel->ref_count)) {
169 rcu_read_unlock_bh();
174 rcu_read_unlock_bh();
178 EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
180 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
182 const struct l2tp_net *pn = l2tp_pernet(net);
183 struct l2tp_tunnel *tunnel;
187 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
189 refcount_inc_not_zero(&tunnel->ref_count)) {
190 rcu_read_unlock_bh();
194 rcu_read_unlock_bh();
198 EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
200 struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
203 struct hlist_head *session_list;
204 struct l2tp_session *session;
206 session_list = l2tp_session_id_hash(tunnel, session_id);
208 read_lock_bh(&tunnel->hlist_lock);
209 hlist_for_each_entry(session, session_list, hlist)
210 if (session->session_id == session_id) {
211 l2tp_session_inc_refcount(session);
212 read_unlock_bh(&tunnel->hlist_lock);
216 read_unlock_bh(&tunnel->hlist_lock);
220 EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session);
222 struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
224 struct hlist_head *session_list;
225 struct l2tp_session *session;
227 session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id);
230 hlist_for_each_entry_rcu(session, session_list, global_hlist)
231 if (session->session_id == session_id) {
232 l2tp_session_inc_refcount(session);
233 rcu_read_unlock_bh();
237 rcu_read_unlock_bh();
241 EXPORT_SYMBOL_GPL(l2tp_session_get);
243 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
246 struct l2tp_session *session;
249 read_lock_bh(&tunnel->hlist_lock);
250 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
251 hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
253 l2tp_session_inc_refcount(session);
254 read_unlock_bh(&tunnel->hlist_lock);
260 read_unlock_bh(&tunnel->hlist_lock);
264 EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
266 /* Lookup a session by interface name.
267 * This is very inefficient but is only used by management interfaces.
269 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
272 struct l2tp_net *pn = l2tp_pernet(net);
274 struct l2tp_session *session;
277 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
278 hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
279 if (!strcmp(session->ifname, ifname)) {
280 l2tp_session_inc_refcount(session);
281 rcu_read_unlock_bh();
288 rcu_read_unlock_bh();
292 EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
294 int l2tp_session_register(struct l2tp_session *session,
295 struct l2tp_tunnel *tunnel)
297 struct l2tp_session *session_walk;
298 struct hlist_head *g_head;
299 struct hlist_head *head;
303 head = l2tp_session_id_hash(tunnel, session->session_id);
305 write_lock_bh(&tunnel->hlist_lock);
306 if (!tunnel->acpt_newsess) {
311 hlist_for_each_entry(session_walk, head, hlist)
312 if (session_walk->session_id == session->session_id) {
317 if (tunnel->version == L2TP_HDR_VER_3) {
318 pn = l2tp_pernet(tunnel->l2tp_net);
319 g_head = l2tp_session_id_hash_2(pn, session->session_id);
321 spin_lock_bh(&pn->l2tp_session_hlist_lock);
323 /* IP encap expects session IDs to be globally unique, while
326 hlist_for_each_entry(session_walk, g_head, global_hlist)
327 if (session_walk->session_id == session->session_id &&
328 (session_walk->tunnel->encap == L2TP_ENCAPTYPE_IP ||
329 tunnel->encap == L2TP_ENCAPTYPE_IP)) {
331 goto err_tlock_pnlock;
334 l2tp_tunnel_inc_refcount(tunnel);
335 hlist_add_head_rcu(&session->global_hlist, g_head);
337 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
339 l2tp_tunnel_inc_refcount(tunnel);
342 hlist_add_head(&session->hlist, head);
343 write_unlock_bh(&tunnel->hlist_lock);
348 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
350 write_unlock_bh(&tunnel->hlist_lock);
354 EXPORT_SYMBOL_GPL(l2tp_session_register);
356 /*****************************************************************************
357 * Receive data handling
358 *****************************************************************************/
360 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
363 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
365 struct sk_buff *skbp;
367 u32 ns = L2TP_SKB_CB(skb)->ns;
369 spin_lock_bh(&session->reorder_q.lock);
370 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
371 if (L2TP_SKB_CB(skbp)->ns > ns) {
372 __skb_queue_before(&session->reorder_q, skbp, skb);
373 l2tp_dbg(session, L2TP_MSG_SEQ,
374 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
375 session->name, ns, L2TP_SKB_CB(skbp)->ns,
376 skb_queue_len(&session->reorder_q));
377 atomic_long_inc(&session->stats.rx_oos_packets);
382 __skb_queue_tail(&session->reorder_q, skb);
385 spin_unlock_bh(&session->reorder_q.lock);
388 /* Dequeue a single skb.
390 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
392 struct l2tp_tunnel *tunnel = session->tunnel;
393 int length = L2TP_SKB_CB(skb)->length;
395 /* We're about to requeue the skb, so return resources
396 * to its current owner (a socket receive buffer).
400 atomic_long_inc(&tunnel->stats.rx_packets);
401 atomic_long_add(length, &tunnel->stats.rx_bytes);
402 atomic_long_inc(&session->stats.rx_packets);
403 atomic_long_add(length, &session->stats.rx_bytes);
405 if (L2TP_SKB_CB(skb)->has_seq) {
408 session->nr &= session->nr_max;
410 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
411 session->name, session->nr);
414 /* call private receive handler */
415 if (session->recv_skb)
416 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
421 /* Dequeue skbs from the session's reorder_q, subject to packet order.
422 * Skbs that have been in the queue for too long are simply discarded.
424 static void l2tp_recv_dequeue(struct l2tp_session *session)
429 /* If the pkt at the head of the queue has the nr that we
430 * expect to send up next, dequeue it and any other
431 * in-sequence packets behind it.
434 spin_lock_bh(&session->reorder_q.lock);
435 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
436 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
437 atomic_long_inc(&session->stats.rx_seq_discards);
438 atomic_long_inc(&session->stats.rx_errors);
439 l2tp_dbg(session, L2TP_MSG_SEQ,
440 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
441 session->name, L2TP_SKB_CB(skb)->ns,
442 L2TP_SKB_CB(skb)->length, session->nr,
443 skb_queue_len(&session->reorder_q));
444 session->reorder_skip = 1;
445 __skb_unlink(skb, &session->reorder_q);
450 if (L2TP_SKB_CB(skb)->has_seq) {
451 if (session->reorder_skip) {
452 l2tp_dbg(session, L2TP_MSG_SEQ,
453 "%s: advancing nr to next pkt: %u -> %u",
454 session->name, session->nr,
455 L2TP_SKB_CB(skb)->ns);
456 session->reorder_skip = 0;
457 session->nr = L2TP_SKB_CB(skb)->ns;
459 if (L2TP_SKB_CB(skb)->ns != session->nr) {
460 l2tp_dbg(session, L2TP_MSG_SEQ,
461 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
462 session->name, L2TP_SKB_CB(skb)->ns,
463 L2TP_SKB_CB(skb)->length, session->nr,
464 skb_queue_len(&session->reorder_q));
468 __skb_unlink(skb, &session->reorder_q);
470 /* Process the skb. We release the queue lock while we
471 * do so to let other contexts process the queue.
473 spin_unlock_bh(&session->reorder_q.lock);
474 l2tp_recv_dequeue_skb(session, skb);
479 spin_unlock_bh(&session->reorder_q.lock);
482 static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
486 if (nr >= session->nr)
487 nws = nr - session->nr;
489 nws = (session->nr_max + 1) - (session->nr - nr);
491 return nws < session->nr_window_size;
494 /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
495 * acceptable, else non-zero.
497 static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
499 if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
500 /* Packet sequence number is outside allowed window.
503 l2tp_dbg(session, L2TP_MSG_SEQ,
504 "%s: pkt %u len %d discarded, outside window, nr=%u\n",
505 session->name, L2TP_SKB_CB(skb)->ns,
506 L2TP_SKB_CB(skb)->length, session->nr);
510 if (session->reorder_timeout != 0) {
511 /* Packet reordering enabled. Add skb to session's
512 * reorder queue, in order of ns.
514 l2tp_recv_queue_skb(session, skb);
518 /* Packet reordering disabled. Discard out-of-sequence packets, while
519 * tracking the number if in-sequence packets after the first OOS packet
520 * is seen. After nr_oos_count_max in-sequence packets, reset the
521 * sequence number to re-enable packet reception.
523 if (L2TP_SKB_CB(skb)->ns == session->nr) {
524 skb_queue_tail(&session->reorder_q, skb);
526 u32 nr_oos = L2TP_SKB_CB(skb)->ns;
527 u32 nr_next = (session->nr_oos + 1) & session->nr_max;
529 if (nr_oos == nr_next)
530 session->nr_oos_count++;
532 session->nr_oos_count = 0;
534 session->nr_oos = nr_oos;
535 if (session->nr_oos_count > session->nr_oos_count_max) {
536 session->reorder_skip = 1;
537 l2tp_dbg(session, L2TP_MSG_SEQ,
538 "%s: %d oos packets received. Resetting sequence numbers\n",
539 session->name, session->nr_oos_count);
541 if (!session->reorder_skip) {
542 atomic_long_inc(&session->stats.rx_seq_discards);
543 l2tp_dbg(session, L2TP_MSG_SEQ,
544 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
545 session->name, L2TP_SKB_CB(skb)->ns,
546 L2TP_SKB_CB(skb)->length, session->nr,
547 skb_queue_len(&session->reorder_q));
550 skb_queue_tail(&session->reorder_q, skb);
560 /* Do receive processing of L2TP data frames. We handle both L2TPv2
561 * and L2TPv3 data frames here.
563 * L2TPv2 Data Message Header
566 * 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
567 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
568 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570 * | Tunnel ID | Session ID |
571 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
572 * | Ns (opt) | Nr (opt) |
573 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
574 * | Offset Size (opt) | Offset pad... (opt)
575 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
577 * Data frames are marked by T=0. All other fields are the same as
578 * those in L2TP control frames.
580 * L2TPv3 Data Message Header
582 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
583 * | L2TP Session Header |
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
585 * | L2-Specific Sublayer |
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
587 * | Tunnel Payload ...
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * L2TPv3 Session Header Over IP
593 * 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
594 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * | Cookie (optional, maximum 64 bits)...
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 * L2TPv3 L2-Specific Sublayer Format
605 * 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
606 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
607 * |x|S|x|x|x|x|x|x| Sequence Number |
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 * Cookie value and sublayer format are negotiated with the peer when
611 * the session is set up. Unlike L2TPv2, we do not need to parse the
612 * packet header to determine if optional fields are present.
614 * Caller must already have parsed the frame and determined that it is
615 * a data (not control) frame before coming here. Fields up to the
616 * session-id have already been parsed and ptr points to the data
617 * after the session-id.
619 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
620 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
623 struct l2tp_tunnel *tunnel = session->tunnel;
627 /* Parse and check optional cookie */
628 if (session->peer_cookie_len > 0) {
629 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
630 l2tp_info(tunnel, L2TP_MSG_DATA,
631 "%s: cookie mismatch (%u/%u). Discarding.\n",
632 tunnel->name, tunnel->tunnel_id,
633 session->session_id);
634 atomic_long_inc(&session->stats.rx_cookie_discards);
637 ptr += session->peer_cookie_len;
640 /* Handle the optional sequence numbers. Sequence numbers are
641 * in different places for L2TPv2 and L2TPv3.
643 * If we are the LAC, enable/disable sequence numbers under
644 * the control of the LNS. If no sequence numbers present but
645 * we were expecting them, discard frame.
647 L2TP_SKB_CB(skb)->has_seq = 0;
648 if (tunnel->version == L2TP_HDR_VER_2) {
649 if (hdrflags & L2TP_HDRFLAG_S) {
650 ns = ntohs(*(__be16 *)ptr);
652 nr = ntohs(*(__be16 *)ptr);
655 /* Store L2TP info in the skb */
656 L2TP_SKB_CB(skb)->ns = ns;
657 L2TP_SKB_CB(skb)->has_seq = 1;
659 l2tp_dbg(session, L2TP_MSG_SEQ,
660 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
661 session->name, ns, nr, session->nr);
663 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
664 u32 l2h = ntohl(*(__be32 *)ptr);
666 if (l2h & 0x40000000) {
667 ns = l2h & 0x00ffffff;
669 /* Store L2TP info in the skb */
670 L2TP_SKB_CB(skb)->ns = ns;
671 L2TP_SKB_CB(skb)->has_seq = 1;
673 l2tp_dbg(session, L2TP_MSG_SEQ,
674 "%s: recv data ns=%u, session nr=%u\n",
675 session->name, ns, session->nr);
680 if (L2TP_SKB_CB(skb)->has_seq) {
681 /* Received a packet with sequence numbers. If we're the LAC,
682 * check if we sre sending sequence numbers and if not,
685 if (!session->lns_mode && !session->send_seq) {
686 l2tp_info(session, L2TP_MSG_SEQ,
687 "%s: requested to enable seq numbers by LNS\n",
689 session->send_seq = 1;
690 l2tp_session_set_header_len(session, tunnel->version);
693 /* No sequence numbers.
694 * If user has configured mandatory sequence numbers, discard.
696 if (session->recv_seq) {
697 l2tp_warn(session, L2TP_MSG_SEQ,
698 "%s: recv data has no seq numbers when required. Discarding.\n",
700 atomic_long_inc(&session->stats.rx_seq_discards);
704 /* If we're the LAC and we're sending sequence numbers, the
705 * LNS has requested that we no longer send sequence numbers.
706 * If we're the LNS and we're sending sequence numbers, the
707 * LAC is broken. Discard the frame.
709 if (!session->lns_mode && session->send_seq) {
710 l2tp_info(session, L2TP_MSG_SEQ,
711 "%s: requested to disable seq numbers by LNS\n",
713 session->send_seq = 0;
714 l2tp_session_set_header_len(session, tunnel->version);
715 } else if (session->send_seq) {
716 l2tp_warn(session, L2TP_MSG_SEQ,
717 "%s: recv data has no seq numbers when required. Discarding.\n",
719 atomic_long_inc(&session->stats.rx_seq_discards);
724 /* Session data offset is defined only for L2TPv2 and is
725 * indicated by an optional 16-bit value in the header.
727 if (tunnel->version == L2TP_HDR_VER_2) {
728 /* If offset bit set, skip it. */
729 if (hdrflags & L2TP_HDRFLAG_O) {
730 offset = ntohs(*(__be16 *)ptr);
736 if (!pskb_may_pull(skb, offset))
739 __skb_pull(skb, offset);
741 /* Prepare skb for adding to the session's reorder_q. Hold
742 * packets for max reorder_timeout or 1 second if not
745 L2TP_SKB_CB(skb)->length = length;
746 L2TP_SKB_CB(skb)->expires = jiffies +
747 (session->reorder_timeout ? session->reorder_timeout : HZ);
749 /* Add packet to the session's receive queue. Reordering is done here, if
750 * enabled. Saved L2TP protocol info is stored in skb->sb[].
752 if (L2TP_SKB_CB(skb)->has_seq) {
753 if (l2tp_recv_data_seq(session, skb))
756 /* No sequence numbers. Add the skb to the tail of the
757 * reorder queue. This ensures that it will be
758 * delivered after all previous sequenced skbs.
760 skb_queue_tail(&session->reorder_q, skb);
763 /* Try to dequeue as many skbs from reorder_q as we can. */
764 l2tp_recv_dequeue(session);
769 atomic_long_inc(&session->stats.rx_errors);
772 EXPORT_SYMBOL(l2tp_recv_common);
774 /* Drop skbs from the session's reorder_q
776 static void l2tp_session_queue_purge(struct l2tp_session *session)
778 struct sk_buff *skb = NULL;
780 if (WARN_ON(session->magic != L2TP_SESSION_MAGIC))
783 while ((skb = skb_dequeue(&session->reorder_q))) {
784 atomic_long_inc(&session->stats.rx_errors);
789 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
790 * here. The skb is not on a list when we get here.
791 * Returns 0 if the packet was a data packet and was successfully passed on.
792 * Returns 1 if the packet was not a good data packet and could not be
793 * forwarded. All such packets are passed up to userspace to deal with.
795 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
797 struct l2tp_session *session = NULL;
798 unsigned char *ptr, *optr;
800 u32 tunnel_id, session_id;
804 /* UDP has verifed checksum */
806 /* UDP always verifies the packet length. */
807 __skb_pull(skb, sizeof(struct udphdr));
810 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX)) {
811 l2tp_info(tunnel, L2TP_MSG_DATA,
812 "%s: recv short packet (len=%d)\n",
813 tunnel->name, skb->len);
817 /* Trace packet contents, if enabled */
818 if (tunnel->debug & L2TP_MSG_DATA) {
819 length = min(32u, skb->len);
820 if (!pskb_may_pull(skb, length))
823 pr_debug("%s: recv\n", tunnel->name);
824 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
827 /* Point to L2TP header */
831 /* Get L2TP header flags */
832 hdrflags = ntohs(*(__be16 *)ptr);
834 /* Check protocol version */
835 version = hdrflags & L2TP_HDR_VER_MASK;
836 if (version != tunnel->version) {
837 l2tp_info(tunnel, L2TP_MSG_DATA,
838 "%s: recv protocol version mismatch: got %d expected %d\n",
839 tunnel->name, version, tunnel->version);
843 /* Get length of L2TP packet */
846 /* If type is control packet, it is handled by userspace. */
847 if (hdrflags & L2TP_HDRFLAG_T) {
848 l2tp_dbg(tunnel, L2TP_MSG_DATA,
849 "%s: recv control packet, len=%d\n",
850 tunnel->name, length);
857 if (tunnel->version == L2TP_HDR_VER_2) {
858 /* If length is present, skip it */
859 if (hdrflags & L2TP_HDRFLAG_L)
862 /* Extract tunnel and session ID */
863 tunnel_id = ntohs(*(__be16 *)ptr);
865 session_id = ntohs(*(__be16 *)ptr);
868 ptr += 2; /* skip reserved bits */
869 tunnel_id = tunnel->tunnel_id;
870 session_id = ntohl(*(__be32 *)ptr);
874 /* Find the session context */
875 session = l2tp_tunnel_get_session(tunnel, session_id);
876 if (!session || !session->recv_skb) {
878 l2tp_session_dec_refcount(session);
880 /* Not found? Pass to userspace to deal with */
881 l2tp_info(tunnel, L2TP_MSG_DATA,
882 "%s: no session found (%u/%u). Passing up.\n",
883 tunnel->name, tunnel_id, session_id);
887 if (tunnel->version == L2TP_HDR_VER_3 &&
888 l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr))
891 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
892 l2tp_session_dec_refcount(session);
897 /* Put UDP header back */
898 __skb_push(skb, sizeof(struct udphdr));
903 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
907 * >0: skb should be passed up to userspace as UDP.
909 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
911 struct l2tp_tunnel *tunnel;
913 tunnel = rcu_dereference_sk_user_data(sk);
917 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
918 tunnel->name, skb->len);
920 if (l2tp_udp_recv_core(tunnel, skb))
928 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
930 /************************************************************************
932 ***********************************************************************/
934 /* Build an L2TP header for the session into the buffer provided.
936 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
938 struct l2tp_tunnel *tunnel = session->tunnel;
941 u16 flags = L2TP_HDR_VER_2;
942 u32 tunnel_id = tunnel->peer_tunnel_id;
943 u32 session_id = session->peer_session_id;
945 if (session->send_seq)
946 flags |= L2TP_HDRFLAG_S;
948 /* Setup L2TP header. */
949 *bufp++ = htons(flags);
950 *bufp++ = htons(tunnel_id);
951 *bufp++ = htons(session_id);
952 if (session->send_seq) {
953 *bufp++ = htons(session->ns);
956 session->ns &= 0xffff;
957 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
958 session->name, session->ns);
964 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
966 struct l2tp_tunnel *tunnel = session->tunnel;
970 /* Setup L2TP header. The header differs slightly for UDP and
971 * IP encapsulations. For UDP, there is 4 bytes of flags.
973 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
974 u16 flags = L2TP_HDR_VER_3;
975 *((__be16 *)bufp) = htons(flags);
977 *((__be16 *)bufp) = 0;
981 *((__be32 *)bufp) = htonl(session->peer_session_id);
983 if (session->cookie_len) {
984 memcpy(bufp, &session->cookie[0], session->cookie_len);
985 bufp += session->cookie_len;
987 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
990 if (session->send_seq) {
991 l2h = 0x40000000 | session->ns;
993 session->ns &= 0xffffff;
994 l2tp_dbg(session, L2TP_MSG_SEQ,
995 "%s: updated ns to %u\n",
996 session->name, session->ns);
999 *((__be32 *)bufp) = htonl(l2h);
1006 static void l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1007 struct flowi *fl, size_t data_len)
1009 struct l2tp_tunnel *tunnel = session->tunnel;
1010 unsigned int len = skb->len;
1014 if (session->send_seq)
1015 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
1016 session->name, data_len, session->ns - 1);
1018 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
1019 session->name, data_len);
1021 if (session->debug & L2TP_MSG_DATA) {
1022 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1023 unsigned char *datap = skb->data + uhlen;
1025 pr_debug("%s: xmit\n", session->name);
1026 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1027 datap, min_t(size_t, 32, len - uhlen));
1030 /* Queue the packet to IP for output */
1033 #if IS_ENABLED(CONFIG_IPV6)
1034 if (l2tp_sk_is_v6(tunnel->sock))
1035 error = inet6_csk_xmit(tunnel->sock, skb, NULL);
1038 error = ip_queue_xmit(tunnel->sock, skb, fl);
1042 atomic_long_inc(&tunnel->stats.tx_packets);
1043 atomic_long_add(len, &tunnel->stats.tx_bytes);
1044 atomic_long_inc(&session->stats.tx_packets);
1045 atomic_long_add(len, &session->stats.tx_bytes);
1047 atomic_long_inc(&tunnel->stats.tx_errors);
1048 atomic_long_inc(&session->stats.tx_errors);
1052 /* If caller requires the skb to have a ppp header, the header must be
1053 * inserted in the skb data before calling this function.
1055 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1057 int data_len = skb->len;
1058 struct l2tp_tunnel *tunnel = session->tunnel;
1059 struct sock *sk = tunnel->sock;
1062 struct inet_sock *inet;
1064 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1066 int ret = NET_XMIT_SUCCESS;
1068 /* Check that there's enough headroom in the skb to insert IP,
1069 * UDP and L2TP headers. If not enough, expand it to
1070 * make room. Adjust truesize.
1072 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1074 if (skb_cow_head(skb, headroom)) {
1076 return NET_XMIT_DROP;
1079 /* Setup L2TP header */
1080 session->build_header(session, __skb_push(skb, hdr_len));
1082 /* Reset skb netfilter state */
1083 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1084 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1089 if (sock_owned_by_user(sk)) {
1091 ret = NET_XMIT_DROP;
1095 /* The user-space may change the connection status for the user-space
1096 * provided socket at run time: we must check it under the socket lock
1098 if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
1100 ret = NET_XMIT_DROP;
1105 fl = &inet->cork.fl;
1106 switch (tunnel->encap) {
1107 case L2TP_ENCAPTYPE_UDP:
1108 /* Setup UDP header */
1109 __skb_push(skb, sizeof(*uh));
1110 skb_reset_transport_header(skb);
1112 uh->source = inet->inet_sport;
1113 uh->dest = inet->inet_dport;
1114 udp_len = uhlen + hdr_len + data_len;
1115 uh->len = htons(udp_len);
1117 /* Calculate UDP checksum if configured to do so */
1118 #if IS_ENABLED(CONFIG_IPV6)
1119 if (l2tp_sk_is_v6(sk))
1120 udp6_set_csum(udp_get_no_check6_tx(sk),
1121 skb, &inet6_sk(sk)->saddr,
1122 &sk->sk_v6_daddr, udp_len);
1125 udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
1126 inet->inet_daddr, udp_len);
1129 case L2TP_ENCAPTYPE_IP:
1133 l2tp_xmit_core(session, skb, fl, data_len);
1139 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1141 /*****************************************************************************
1142 * Tinnel and session create/destroy.
1143 *****************************************************************************/
1145 /* Tunnel socket destruct hook.
1146 * The tunnel context is deleted only when all session sockets have been
1149 static void l2tp_tunnel_destruct(struct sock *sk)
1151 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1156 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
1158 /* Disable udp encapsulation */
1159 switch (tunnel->encap) {
1160 case L2TP_ENCAPTYPE_UDP:
1161 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1162 (udp_sk(sk))->encap_type = 0;
1163 (udp_sk(sk))->encap_rcv = NULL;
1164 (udp_sk(sk))->encap_destroy = NULL;
1166 case L2TP_ENCAPTYPE_IP:
1170 /* Remove hooks into tunnel socket */
1171 sk->sk_destruct = tunnel->old_sk_destruct;
1172 sk->sk_user_data = NULL;
1174 /* Call the original destructor */
1175 if (sk->sk_destruct)
1176 (*sk->sk_destruct)(sk);
1178 kfree_rcu(tunnel, rcu);
1183 /* When the tunnel is closed, all the attached sessions need to go too.
1185 static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1188 struct hlist_node *walk;
1189 struct hlist_node *tmp;
1190 struct l2tp_session *session;
1192 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
1195 write_lock_bh(&tunnel->hlist_lock);
1196 tunnel->acpt_newsess = false;
1197 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1199 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1200 session = hlist_entry(walk, struct l2tp_session, hlist);
1202 l2tp_info(session, L2TP_MSG_CONTROL,
1203 "%s: closing session\n", session->name);
1205 hlist_del_init(&session->hlist);
1207 if (test_and_set_bit(0, &session->dead))
1210 write_unlock_bh(&tunnel->hlist_lock);
1212 __l2tp_session_unhash(session);
1213 l2tp_session_queue_purge(session);
1215 if (session->session_close)
1216 (*session->session_close)(session);
1218 l2tp_session_dec_refcount(session);
1220 write_lock_bh(&tunnel->hlist_lock);
1222 /* Now restart from the beginning of this hash
1223 * chain. We always remove a session from the
1224 * list so we are guaranteed to make forward
1230 write_unlock_bh(&tunnel->hlist_lock);
1233 /* Tunnel socket destroy hook for UDP encapsulation */
1234 static void l2tp_udp_encap_destroy(struct sock *sk)
1236 struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
1239 l2tp_tunnel_delete(tunnel);
1242 /* Workqueue tunnel deletion function */
1243 static void l2tp_tunnel_del_work(struct work_struct *work)
1245 struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
1247 struct sock *sk = tunnel->sock;
1248 struct socket *sock = sk->sk_socket;
1249 struct l2tp_net *pn;
1251 l2tp_tunnel_closeall(tunnel);
1253 /* If the tunnel socket was created within the kernel, use
1254 * the sk API to release it here.
1256 if (tunnel->fd < 0) {
1258 kernel_sock_shutdown(sock, SHUT_RDWR);
1263 /* Remove the tunnel struct from the tunnel list */
1264 pn = l2tp_pernet(tunnel->l2tp_net);
1265 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1266 list_del_rcu(&tunnel->list);
1267 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1269 /* drop initial ref */
1270 l2tp_tunnel_dec_refcount(tunnel);
1272 /* drop workqueue ref */
1273 l2tp_tunnel_dec_refcount(tunnel);
1276 /* Create a socket for the tunnel, if one isn't set up by
1277 * userspace. This is used for static tunnels where there is no
1278 * managing L2TP daemon.
1280 * Since we don't want these sockets to keep a namespace alive by
1281 * themselves, we drop the socket's namespace refcount after creation.
1282 * These sockets are freed when the namespace exits using the pernet
1285 static int l2tp_tunnel_sock_create(struct net *net,
1288 struct l2tp_tunnel_cfg *cfg,
1289 struct socket **sockp)
1292 struct socket *sock = NULL;
1293 struct udp_port_cfg udp_conf;
1295 switch (cfg->encap) {
1296 case L2TP_ENCAPTYPE_UDP:
1297 memset(&udp_conf, 0, sizeof(udp_conf));
1299 #if IS_ENABLED(CONFIG_IPV6)
1300 if (cfg->local_ip6 && cfg->peer_ip6) {
1301 udp_conf.family = AF_INET6;
1302 memcpy(&udp_conf.local_ip6, cfg->local_ip6,
1303 sizeof(udp_conf.local_ip6));
1304 memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
1305 sizeof(udp_conf.peer_ip6));
1306 udp_conf.use_udp6_tx_checksums =
1307 !cfg->udp6_zero_tx_checksums;
1308 udp_conf.use_udp6_rx_checksums =
1309 !cfg->udp6_zero_rx_checksums;
1313 udp_conf.family = AF_INET;
1314 udp_conf.local_ip = cfg->local_ip;
1315 udp_conf.peer_ip = cfg->peer_ip;
1316 udp_conf.use_udp_checksums = cfg->use_udp_checksums;
1319 udp_conf.local_udp_port = htons(cfg->local_udp_port);
1320 udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
1322 err = udp_sock_create(net, &udp_conf, &sock);
1328 case L2TP_ENCAPTYPE_IP:
1329 #if IS_ENABLED(CONFIG_IPV6)
1330 if (cfg->local_ip6 && cfg->peer_ip6) {
1331 struct sockaddr_l2tpip6 ip6_addr = {0};
1333 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
1334 IPPROTO_L2TP, &sock);
1338 ip6_addr.l2tp_family = AF_INET6;
1339 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1340 sizeof(ip6_addr.l2tp_addr));
1341 ip6_addr.l2tp_conn_id = tunnel_id;
1342 err = kernel_bind(sock, (struct sockaddr *)&ip6_addr,
1347 ip6_addr.l2tp_family = AF_INET6;
1348 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1349 sizeof(ip6_addr.l2tp_addr));
1350 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1351 err = kernel_connect(sock,
1352 (struct sockaddr *)&ip6_addr,
1353 sizeof(ip6_addr), 0);
1359 struct sockaddr_l2tpip ip_addr = {0};
1361 err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
1362 IPPROTO_L2TP, &sock);
1366 ip_addr.l2tp_family = AF_INET;
1367 ip_addr.l2tp_addr = cfg->local_ip;
1368 ip_addr.l2tp_conn_id = tunnel_id;
1369 err = kernel_bind(sock, (struct sockaddr *)&ip_addr,
1374 ip_addr.l2tp_family = AF_INET;
1375 ip_addr.l2tp_addr = cfg->peer_ip;
1376 ip_addr.l2tp_conn_id = peer_tunnel_id;
1377 err = kernel_connect(sock, (struct sockaddr *)&ip_addr,
1378 sizeof(ip_addr), 0);
1390 if (err < 0 && sock) {
1391 kernel_sock_shutdown(sock, SHUT_RDWR);
1399 static struct lock_class_key l2tp_socket_class;
1401 int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id,
1402 struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
1404 struct l2tp_tunnel *tunnel = NULL;
1406 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1411 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
1417 tunnel->version = version;
1418 tunnel->tunnel_id = tunnel_id;
1419 tunnel->peer_tunnel_id = peer_tunnel_id;
1420 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1422 tunnel->magic = L2TP_TUNNEL_MAGIC;
1423 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1424 rwlock_init(&tunnel->hlist_lock);
1425 tunnel->acpt_newsess = true;
1428 tunnel->debug = cfg->debug;
1430 tunnel->encap = encap;
1432 refcount_set(&tunnel->ref_count, 1);
1435 /* Init delete workqueue struct */
1436 INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1438 INIT_LIST_HEAD(&tunnel->list);
1447 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1449 static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
1450 enum l2tp_encap_type encap)
1452 if (!net_eq(sock_net(sk), net))
1455 if (sk->sk_type != SOCK_DGRAM)
1456 return -EPROTONOSUPPORT;
1458 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
1459 return -EPROTONOSUPPORT;
1461 if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
1462 (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
1463 return -EPROTONOSUPPORT;
1465 if (sk->sk_user_data)
1471 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1472 struct l2tp_tunnel_cfg *cfg)
1474 struct l2tp_tunnel *tunnel_walk;
1475 struct l2tp_net *pn;
1476 struct socket *sock;
1480 if (tunnel->fd < 0) {
1481 ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
1482 tunnel->peer_tunnel_id, cfg,
1487 sock = sockfd_lookup(tunnel->fd, &ret);
1491 ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
1496 tunnel->l2tp_net = net;
1497 pn = l2tp_pernet(net);
1499 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1500 list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
1501 if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
1502 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1508 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1509 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1515 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
1516 struct udp_tunnel_sock_cfg udp_cfg = {
1517 .sk_user_data = tunnel,
1518 .encap_type = UDP_ENCAP_L2TPINUDP,
1519 .encap_rcv = l2tp_udp_encap_recv,
1520 .encap_destroy = l2tp_udp_encap_destroy,
1523 setup_udp_tunnel_sock(net, sock, &udp_cfg);
1525 sk->sk_user_data = tunnel;
1528 tunnel->old_sk_destruct = sk->sk_destruct;
1529 sk->sk_destruct = &l2tp_tunnel_destruct;
1530 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
1532 sk->sk_allocation = GFP_ATOMIC;
1534 if (tunnel->fd >= 0)
1547 EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
1549 /* This function is used by the netlink TUNNEL_DELETE command.
1551 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1553 if (!test_and_set_bit(0, &tunnel->dead)) {
1554 l2tp_tunnel_inc_refcount(tunnel);
1555 queue_work(l2tp_wq, &tunnel->del_work);
1558 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1560 /* Really kill the session.
1562 void l2tp_session_free(struct l2tp_session *session)
1564 struct l2tp_tunnel *tunnel = session->tunnel;
1567 if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
1569 l2tp_tunnel_dec_refcount(tunnel);
1575 EXPORT_SYMBOL_GPL(l2tp_session_free);
1577 /* Remove an l2tp session from l2tp_core's hash lists.
1578 * Provides a tidyup interface for pseudowire code which can't just route all
1579 * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
1582 void __l2tp_session_unhash(struct l2tp_session *session)
1584 struct l2tp_tunnel *tunnel = session->tunnel;
1586 /* Remove the session from core hashes */
1588 /* Remove from the per-tunnel hash */
1589 write_lock_bh(&tunnel->hlist_lock);
1590 hlist_del_init(&session->hlist);
1591 write_unlock_bh(&tunnel->hlist_lock);
1593 /* For L2TPv3 we have a per-net hash: remove from there, too */
1594 if (tunnel->version != L2TP_HDR_VER_2) {
1595 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1597 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1598 hlist_del_init_rcu(&session->global_hlist);
1599 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1604 EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
1606 /* This function is used by the netlink SESSION_DELETE command and by
1607 * pseudowire modules.
1609 int l2tp_session_delete(struct l2tp_session *session)
1611 if (test_and_set_bit(0, &session->dead))
1614 __l2tp_session_unhash(session);
1615 l2tp_session_queue_purge(session);
1616 if (session->session_close)
1617 (*session->session_close)(session);
1619 l2tp_session_dec_refcount(session);
1623 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1625 /* We come here whenever a session's send_seq, cookie_len or
1626 * l2specific_type parameters are set.
1628 void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1630 if (version == L2TP_HDR_VER_2) {
1631 session->hdr_len = 6;
1632 if (session->send_seq)
1633 session->hdr_len += 4;
1635 session->hdr_len = 4 + session->cookie_len;
1636 session->hdr_len += l2tp_get_l2specific_len(session);
1637 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1638 session->hdr_len += 4;
1641 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1643 struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id,
1644 u32 peer_session_id, struct l2tp_session_cfg *cfg)
1646 struct l2tp_session *session;
1648 session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL);
1650 session->magic = L2TP_SESSION_MAGIC;
1651 session->tunnel = tunnel;
1653 session->session_id = session_id;
1654 session->peer_session_id = peer_session_id;
1656 if (tunnel->version == L2TP_HDR_VER_2)
1657 session->nr_max = 0xffff;
1659 session->nr_max = 0xffffff;
1660 session->nr_window_size = session->nr_max / 2;
1661 session->nr_oos_count_max = 4;
1663 /* Use NR of first received packet */
1664 session->reorder_skip = 1;
1666 sprintf(&session->name[0], "sess %u/%u",
1667 tunnel->tunnel_id, session->session_id);
1669 skb_queue_head_init(&session->reorder_q);
1671 INIT_HLIST_NODE(&session->hlist);
1672 INIT_HLIST_NODE(&session->global_hlist);
1674 /* Inherit debug options from tunnel */
1675 session->debug = tunnel->debug;
1678 session->pwtype = cfg->pw_type;
1679 session->debug = cfg->debug;
1680 session->send_seq = cfg->send_seq;
1681 session->recv_seq = cfg->recv_seq;
1682 session->lns_mode = cfg->lns_mode;
1683 session->reorder_timeout = cfg->reorder_timeout;
1684 session->l2specific_type = cfg->l2specific_type;
1685 session->cookie_len = cfg->cookie_len;
1686 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1687 session->peer_cookie_len = cfg->peer_cookie_len;
1688 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1691 if (tunnel->version == L2TP_HDR_VER_2)
1692 session->build_header = l2tp_build_l2tpv2_header;
1694 session->build_header = l2tp_build_l2tpv3_header;
1696 l2tp_session_set_header_len(session, tunnel->version);
1698 refcount_set(&session->ref_count, 1);
1703 return ERR_PTR(-ENOMEM);
1705 EXPORT_SYMBOL_GPL(l2tp_session_create);
1707 /*****************************************************************************
1709 *****************************************************************************/
1711 static __net_init int l2tp_init_net(struct net *net)
1713 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1716 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1717 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1719 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1720 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1722 spin_lock_init(&pn->l2tp_session_hlist_lock);
1727 static __net_exit void l2tp_exit_net(struct net *net)
1729 struct l2tp_net *pn = l2tp_pernet(net);
1730 struct l2tp_tunnel *tunnel = NULL;
1734 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
1735 l2tp_tunnel_delete(tunnel);
1737 rcu_read_unlock_bh();
1740 flush_workqueue(l2tp_wq);
1743 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1744 WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
1747 static struct pernet_operations l2tp_net_ops = {
1748 .init = l2tp_init_net,
1749 .exit = l2tp_exit_net,
1751 .size = sizeof(struct l2tp_net),
1754 static int __init l2tp_init(void)
1758 rc = register_pernet_device(&l2tp_net_ops);
1762 l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
1764 pr_err("alloc_workqueue failed\n");
1765 unregister_pernet_device(&l2tp_net_ops);
1770 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
1776 static void __exit l2tp_exit(void)
1778 unregister_pernet_device(&l2tp_net_ops);
1780 destroy_workqueue(l2tp_wq);
1785 module_init(l2tp_init);
1786 module_exit(l2tp_exit);
1788 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1789 MODULE_DESCRIPTION("L2TP core");
1790 MODULE_LICENSE("GPL");
1791 MODULE_VERSION(L2TP_DRV_VERSION);