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 #include <linux/module.h>
22 #include <linux/string.h>
23 #include <linux/list.h>
24 #include <linux/rculist.h>
25 #include <linux/uaccess.h>
27 #include <linux/kernel.h>
28 #include <linux/spinlock.h>
29 #include <linux/kthread.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/jiffies.h>
35 #include <linux/netdevice.h>
36 #include <linux/net.h>
37 #include <linux/inetdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/init.h>
42 #include <linux/udp.h>
43 #include <linux/l2tp.h>
44 #include <linux/hash.h>
45 #include <linux/sort.h>
46 #include <linux/file.h>
47 #include <linux/nsproxy.h>
48 #include <net/net_namespace.h>
49 #include <net/netns/generic.h>
53 #include <net/inet_common.h>
55 #include <net/protocol.h>
56 #include <net/inet6_connection_sock.h>
57 #include <net/inet_ecn.h>
58 #include <net/ip6_route.h>
59 #include <net/ip6_checksum.h>
61 #include <asm/byteorder.h>
62 #include <linux/atomic.h>
64 #include "l2tp_core.h"
66 #define L2TP_DRV_VERSION "V2.0"
68 /* L2TP header constants */
69 #define L2TP_HDRFLAG_T 0x8000
70 #define L2TP_HDRFLAG_L 0x4000
71 #define L2TP_HDRFLAG_S 0x0800
72 #define L2TP_HDRFLAG_O 0x0200
73 #define L2TP_HDRFLAG_P 0x0100
75 #define L2TP_HDR_VER_MASK 0x000F
76 #define L2TP_HDR_VER_2 0x0002
77 #define L2TP_HDR_VER_3 0x0003
79 /* L2TPv3 default L2-specific sublayer */
80 #define L2TP_SLFLAG_S 0x40000000
81 #define L2TP_SL_SEQ_MASK 0x00ffffff
83 #define L2TP_HDR_SIZE_SEQ 10
84 #define L2TP_HDR_SIZE_NOSEQ 6
86 /* Default trace flags */
87 #define L2TP_DEFAULT_DEBUG_FLAGS 0
89 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
91 if ((_mask) & (_type)) \
92 printk(_lvl "L2TP: " _fmt, ##args); \
95 /* Private data stored for received packets in the skb.
101 unsigned long expires;
104 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
106 static atomic_t l2tp_tunnel_count;
107 static atomic_t l2tp_session_count;
109 /* per-net private data for this module */
110 static unsigned int l2tp_net_id;
112 struct list_head l2tp_tunnel_list;
113 spinlock_t l2tp_tunnel_list_lock;
114 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
115 spinlock_t l2tp_session_hlist_lock;
118 static void l2tp_session_set_header_len(struct l2tp_session *session, int version);
119 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
120 static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
122 static inline struct l2tp_net *l2tp_pernet(struct net *net)
126 return net_generic(net, l2tp_net_id);
130 /* Tunnel reference counts. Incremented per session that is added to
133 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
135 atomic_inc(&tunnel->ref_count);
138 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
140 if (atomic_dec_and_test(&tunnel->ref_count))
141 l2tp_tunnel_free(tunnel);
143 #ifdef L2TP_REFCNT_DEBUG
144 #define l2tp_tunnel_inc_refcount(_t) do { \
145 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
146 l2tp_tunnel_inc_refcount_1(_t); \
148 #define l2tp_tunnel_dec_refcount(_t) do { \
149 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
150 l2tp_tunnel_dec_refcount_1(_t); \
153 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
154 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
157 /* Session hash global list for L2TPv3.
158 * The session_id SHOULD be random according to RFC3931, but several
159 * L2TP implementations use incrementing session_ids. So we do a real
160 * hash on the session_id, rather than a simple bitmask.
162 static inline struct hlist_head *
163 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
165 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
169 /* Lookup a session by id in the global session list
171 static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
173 struct l2tp_net *pn = l2tp_pernet(net);
174 struct hlist_head *session_list =
175 l2tp_session_id_hash_2(pn, session_id);
176 struct l2tp_session *session;
177 struct hlist_node *walk;
180 hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) {
181 if (session->session_id == session_id) {
182 rcu_read_unlock_bh();
186 rcu_read_unlock_bh();
191 /* Session hash list.
192 * The session_id SHOULD be random according to RFC2661, but several
193 * L2TP implementations (Cisco and Microsoft) use incrementing
194 * session_ids. So we do a real hash on the session_id, rather than a
197 static inline struct hlist_head *
198 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
200 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
203 /* Lookup a session by id
205 struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
207 struct hlist_head *session_list;
208 struct l2tp_session *session;
209 struct hlist_node *walk;
211 /* In L2TPv3, session_ids are unique over all tunnels and we
212 * sometimes need to look them up before we know the
216 return l2tp_session_find_2(net, session_id);
218 session_list = l2tp_session_id_hash(tunnel, session_id);
219 read_lock_bh(&tunnel->hlist_lock);
220 hlist_for_each_entry(session, walk, session_list, hlist) {
221 if (session->session_id == session_id) {
222 read_unlock_bh(&tunnel->hlist_lock);
226 read_unlock_bh(&tunnel->hlist_lock);
230 EXPORT_SYMBOL_GPL(l2tp_session_find);
232 struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
235 struct hlist_node *walk;
236 struct l2tp_session *session;
239 read_lock_bh(&tunnel->hlist_lock);
240 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
241 hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
243 read_unlock_bh(&tunnel->hlist_lock);
249 read_unlock_bh(&tunnel->hlist_lock);
253 EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
255 /* Lookup a session by interface name.
256 * This is very inefficient but is only used by management interfaces.
258 struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
260 struct l2tp_net *pn = l2tp_pernet(net);
262 struct hlist_node *walk;
263 struct l2tp_session *session;
266 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
267 hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) {
268 if (!strcmp(session->ifname, ifname)) {
269 rcu_read_unlock_bh();
275 rcu_read_unlock_bh();
279 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
281 /* Lookup a tunnel by id
283 struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
285 struct l2tp_tunnel *tunnel;
286 struct l2tp_net *pn = l2tp_pernet(net);
289 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
290 if (tunnel->tunnel_id == tunnel_id) {
291 rcu_read_unlock_bh();
295 rcu_read_unlock_bh();
299 EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
301 struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
303 struct l2tp_net *pn = l2tp_pernet(net);
304 struct l2tp_tunnel *tunnel;
308 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
310 rcu_read_unlock_bh();
315 rcu_read_unlock_bh();
319 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
321 /*****************************************************************************
322 * Receive data handling
323 *****************************************************************************/
325 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
328 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
330 struct sk_buff *skbp;
332 u32 ns = L2TP_SKB_CB(skb)->ns;
333 struct l2tp_stats *sstats;
335 spin_lock_bh(&session->reorder_q.lock);
336 sstats = &session->stats;
337 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
338 if (L2TP_SKB_CB(skbp)->ns > ns) {
339 __skb_queue_before(&session->reorder_q, skbp, skb);
340 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
341 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
342 session->name, ns, L2TP_SKB_CB(skbp)->ns,
343 skb_queue_len(&session->reorder_q));
344 u64_stats_update_begin(&sstats->syncp);
345 sstats->rx_oos_packets++;
346 u64_stats_update_end(&sstats->syncp);
351 __skb_queue_tail(&session->reorder_q, skb);
354 spin_unlock_bh(&session->reorder_q.lock);
357 /* Dequeue a single skb.
359 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
361 struct l2tp_tunnel *tunnel = session->tunnel;
362 int length = L2TP_SKB_CB(skb)->length;
363 struct l2tp_stats *tstats, *sstats;
365 /* We're about to requeue the skb, so return resources
366 * to its current owner (a socket receive buffer).
370 tstats = &tunnel->stats;
371 u64_stats_update_begin(&tstats->syncp);
372 sstats = &session->stats;
373 u64_stats_update_begin(&sstats->syncp);
374 tstats->rx_packets++;
375 tstats->rx_bytes += length;
376 sstats->rx_packets++;
377 sstats->rx_bytes += length;
378 u64_stats_update_end(&tstats->syncp);
379 u64_stats_update_end(&sstats->syncp);
381 if (L2TP_SKB_CB(skb)->has_seq) {
384 if (tunnel->version == L2TP_HDR_VER_2)
385 session->nr &= 0xffff;
387 session->nr &= 0xffffff;
389 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
390 "%s: updated nr to %hu\n", session->name, session->nr);
393 /* call private receive handler */
394 if (session->recv_skb != NULL)
395 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
400 (*session->deref)(session);
403 /* Dequeue skbs from the session's reorder_q, subject to packet order.
404 * Skbs that have been in the queue for too long are simply discarded.
406 static void l2tp_recv_dequeue(struct l2tp_session *session)
410 struct l2tp_stats *sstats;
412 /* If the pkt at the head of the queue has the nr that we
413 * expect to send up next, dequeue it and any other
414 * in-sequence packets behind it.
417 spin_lock_bh(&session->reorder_q.lock);
418 sstats = &session->stats;
419 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
420 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
421 u64_stats_update_begin(&sstats->syncp);
422 sstats->rx_seq_discards++;
424 u64_stats_update_end(&sstats->syncp);
425 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
426 "%s: oos pkt %u len %d discarded (too old), "
427 "waiting for %u, reorder_q_len=%d\n",
428 session->name, L2TP_SKB_CB(skb)->ns,
429 L2TP_SKB_CB(skb)->length, session->nr,
430 skb_queue_len(&session->reorder_q));
431 __skb_unlink(skb, &session->reorder_q);
434 (*session->deref)(session);
438 if (L2TP_SKB_CB(skb)->has_seq) {
439 if (L2TP_SKB_CB(skb)->ns != session->nr) {
440 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
441 "%s: holding oos pkt %u len %d, "
442 "waiting for %u, reorder_q_len=%d\n",
443 session->name, L2TP_SKB_CB(skb)->ns,
444 L2TP_SKB_CB(skb)->length, session->nr,
445 skb_queue_len(&session->reorder_q));
449 __skb_unlink(skb, &session->reorder_q);
451 /* Process the skb. We release the queue lock while we
452 * do so to let other contexts process the queue.
454 spin_unlock_bh(&session->reorder_q.lock);
455 l2tp_recv_dequeue_skb(session, skb);
460 spin_unlock_bh(&session->reorder_q.lock);
463 static inline int l2tp_verify_udp_checksum(struct sock *sk,
466 struct udphdr *uh = udp_hdr(skb);
467 u16 ulen = ntohs(uh->len);
470 if (sk->sk_no_check || skb_csum_unnecessary(skb))
473 #if IS_ENABLED(CONFIG_IPV6)
474 if (sk->sk_family == PF_INET6) {
476 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
479 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
480 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
481 &ipv6_hdr(skb)->daddr, ulen,
482 IPPROTO_UDP, skb->csum)) {
483 skb->ip_summed = CHECKSUM_UNNECESSARY;
486 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
487 &ipv6_hdr(skb)->daddr,
488 skb->len, IPPROTO_UDP,
493 struct inet_sock *inet;
497 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
498 ulen, IPPROTO_UDP, 0);
500 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
501 !csum_fold(csum_add(psum, skb->csum)))
506 return __skb_checksum_complete(skb);
509 /* Do receive processing of L2TP data frames. We handle both L2TPv2
510 * and L2TPv3 data frames here.
512 * L2TPv2 Data Message Header
515 * 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
516 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
517 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
518 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
519 * | Tunnel ID | Session ID |
520 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
521 * | Ns (opt) | Nr (opt) |
522 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
523 * | Offset Size (opt) | Offset pad... (opt)
524 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
526 * Data frames are marked by T=0. All other fields are the same as
527 * those in L2TP control frames.
529 * L2TPv3 Data Message Header
531 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
532 * | L2TP Session Header |
533 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 * | L2-Specific Sublayer |
535 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 * | Tunnel Payload ...
537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
539 * L2TPv3 Session Header Over IP
542 * 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
543 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
545 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
546 * | Cookie (optional, maximum 64 bits)...
547 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
549 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
551 * L2TPv3 L2-Specific Sublayer Format
554 * 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
555 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
556 * |x|S|x|x|x|x|x|x| Sequence Number |
557 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
559 * Cookie value, sublayer format and offset (pad) are negotiated with
560 * the peer when the session is set up. Unlike L2TPv2, we do not need
561 * to parse the packet header to determine if optional fields are
564 * Caller must already have parsed the frame and determined that it is
565 * a data (not control) frame before coming here. Fields up to the
566 * session-id have already been parsed and ptr points to the data
567 * after the session-id.
569 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
570 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
571 int length, int (*payload_hook)(struct sk_buff *skb))
573 struct l2tp_tunnel *tunnel = session->tunnel;
576 struct l2tp_stats *sstats = &session->stats;
578 /* The ref count is increased since we now hold a pointer to
579 * the session. Take care to decrement the refcnt when exiting
580 * this function from now on...
582 l2tp_session_inc_refcount(session);
584 (*session->ref)(session);
586 /* Parse and check optional cookie */
587 if (session->peer_cookie_len > 0) {
588 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
589 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
590 "%s: cookie mismatch (%u/%u). Discarding.\n",
591 tunnel->name, tunnel->tunnel_id, session->session_id);
592 u64_stats_update_begin(&sstats->syncp);
593 sstats->rx_cookie_discards++;
594 u64_stats_update_end(&sstats->syncp);
597 ptr += session->peer_cookie_len;
600 /* Handle the optional sequence numbers. Sequence numbers are
601 * in different places for L2TPv2 and L2TPv3.
603 * If we are the LAC, enable/disable sequence numbers under
604 * the control of the LNS. If no sequence numbers present but
605 * we were expecting them, discard frame.
608 L2TP_SKB_CB(skb)->has_seq = 0;
609 if (tunnel->version == L2TP_HDR_VER_2) {
610 if (hdrflags & L2TP_HDRFLAG_S) {
611 ns = ntohs(*(__be16 *) ptr);
613 nr = ntohs(*(__be16 *) ptr);
616 /* Store L2TP info in the skb */
617 L2TP_SKB_CB(skb)->ns = ns;
618 L2TP_SKB_CB(skb)->has_seq = 1;
620 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
621 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
622 session->name, ns, nr, session->nr);
624 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
625 u32 l2h = ntohl(*(__be32 *) ptr);
627 if (l2h & 0x40000000) {
628 ns = l2h & 0x00ffffff;
630 /* Store L2TP info in the skb */
631 L2TP_SKB_CB(skb)->ns = ns;
632 L2TP_SKB_CB(skb)->has_seq = 1;
634 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
635 "%s: recv data ns=%u, session nr=%u\n",
636 session->name, ns, session->nr);
640 /* Advance past L2-specific header, if present */
641 ptr += session->l2specific_len;
643 if (L2TP_SKB_CB(skb)->has_seq) {
644 /* Received a packet with sequence numbers. If we're the LNS,
645 * check if we sre sending sequence numbers and if not,
648 if ((!session->lns_mode) && (!session->send_seq)) {
649 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
650 "%s: requested to enable seq numbers by LNS\n",
652 session->send_seq = -1;
653 l2tp_session_set_header_len(session, tunnel->version);
656 /* No sequence numbers.
657 * If user has configured mandatory sequence numbers, discard.
659 if (session->recv_seq) {
660 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
661 "%s: recv data has no seq numbers when required. "
662 "Discarding\n", session->name);
663 u64_stats_update_begin(&sstats->syncp);
664 sstats->rx_seq_discards++;
665 u64_stats_update_end(&sstats->syncp);
669 /* If we're the LAC and we're sending sequence numbers, the
670 * LNS has requested that we no longer send sequence numbers.
671 * If we're the LNS and we're sending sequence numbers, the
672 * LAC is broken. Discard the frame.
674 if ((!session->lns_mode) && (session->send_seq)) {
675 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
676 "%s: requested to disable seq numbers by LNS\n",
678 session->send_seq = 0;
679 l2tp_session_set_header_len(session, tunnel->version);
680 } else if (session->send_seq) {
681 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
682 "%s: recv data has no seq numbers when required. "
683 "Discarding\n", session->name);
684 u64_stats_update_begin(&sstats->syncp);
685 sstats->rx_seq_discards++;
686 u64_stats_update_end(&sstats->syncp);
691 /* Session data offset is handled differently for L2TPv2 and
692 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
693 * the header. For L2TPv3, the offset is negotiated using AVPs
694 * in the session setup control protocol.
696 if (tunnel->version == L2TP_HDR_VER_2) {
697 /* If offset bit set, skip it. */
698 if (hdrflags & L2TP_HDRFLAG_O) {
699 offset = ntohs(*(__be16 *)ptr);
703 ptr += session->offset;
706 if (!pskb_may_pull(skb, offset))
709 __skb_pull(skb, offset);
711 /* If caller wants to process the payload before we queue the
715 if ((*payload_hook)(skb))
718 /* Prepare skb for adding to the session's reorder_q. Hold
719 * packets for max reorder_timeout or 1 second if not
722 L2TP_SKB_CB(skb)->length = length;
723 L2TP_SKB_CB(skb)->expires = jiffies +
724 (session->reorder_timeout ? session->reorder_timeout : HZ);
726 /* Add packet to the session's receive queue. Reordering is done here, if
727 * enabled. Saved L2TP protocol info is stored in skb->sb[].
729 if (L2TP_SKB_CB(skb)->has_seq) {
730 if (session->reorder_timeout != 0) {
731 /* Packet reordering enabled. Add skb to session's
732 * reorder queue, in order of ns.
734 l2tp_recv_queue_skb(session, skb);
736 /* Packet reordering disabled. Discard out-of-sequence
739 if (L2TP_SKB_CB(skb)->ns != session->nr) {
740 u64_stats_update_begin(&sstats->syncp);
741 sstats->rx_seq_discards++;
742 u64_stats_update_end(&sstats->syncp);
743 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
744 "%s: oos pkt %u len %d discarded, "
745 "waiting for %u, reorder_q_len=%d\n",
746 session->name, L2TP_SKB_CB(skb)->ns,
747 L2TP_SKB_CB(skb)->length, session->nr,
748 skb_queue_len(&session->reorder_q));
751 skb_queue_tail(&session->reorder_q, skb);
754 /* No sequence numbers. Add the skb to the tail of the
755 * reorder queue. This ensures that it will be
756 * delivered after all previous sequenced skbs.
758 skb_queue_tail(&session->reorder_q, skb);
761 /* Try to dequeue as many skbs from reorder_q as we can. */
762 l2tp_recv_dequeue(session);
764 l2tp_session_dec_refcount(session);
769 u64_stats_update_begin(&sstats->syncp);
771 u64_stats_update_end(&sstats->syncp);
775 (*session->deref)(session);
777 l2tp_session_dec_refcount(session);
779 EXPORT_SYMBOL(l2tp_recv_common);
781 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
782 * here. The skb is not on a list when we get here.
783 * Returns 0 if the packet was a data packet and was successfully passed on.
784 * Returns 1 if the packet was not a good data packet and could not be
785 * forwarded. All such packets are passed up to userspace to deal with.
787 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
788 int (*payload_hook)(struct sk_buff *skb))
790 struct l2tp_session *session = NULL;
791 unsigned char *ptr, *optr;
793 u32 tunnel_id, session_id;
797 struct l2tp_stats *tstats;
799 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
800 goto discard_bad_csum;
802 /* UDP always verifies the packet length. */
803 __skb_pull(skb, sizeof(struct udphdr));
806 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
807 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
808 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
812 /* Trace packet contents, if enabled */
813 if (tunnel->debug & L2TP_MSG_DATA) {
814 length = min(32u, skb->len);
815 if (!pskb_may_pull(skb, length))
818 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
822 printk(" %02X", skb->data[offset]);
823 } while (++offset < length);
828 /* Point to L2TP header */
829 optr = ptr = skb->data;
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 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
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 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
849 "%s: recv control packet, len=%d\n", tunnel->name, length);
856 if (tunnel->version == L2TP_HDR_VER_2) {
857 /* If length is present, skip it */
858 if (hdrflags & L2TP_HDRFLAG_L)
861 /* Extract tunnel and session ID */
862 tunnel_id = ntohs(*(__be16 *) ptr);
864 session_id = ntohs(*(__be16 *) ptr);
867 ptr += 2; /* skip reserved bits */
868 tunnel_id = tunnel->tunnel_id;
869 session_id = ntohl(*(__be32 *) ptr);
873 /* Find the session context */
874 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
875 if (!session || !session->recv_skb) {
876 /* Not found? Pass to userspace to deal with */
877 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
878 "%s: no session found (%u/%u). Passing up.\n",
879 tunnel->name, tunnel_id, session_id);
883 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
888 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
889 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
890 tstats = &tunnel->stats;
891 u64_stats_update_begin(&tstats->syncp);
893 u64_stats_update_end(&tstats->syncp);
899 /* Put UDP header back */
900 __skb_push(skb, sizeof(struct udphdr));
905 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
909 * >0: skb should be passed up to userspace as UDP.
911 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
913 struct l2tp_tunnel *tunnel;
915 tunnel = l2tp_sock_to_tunnel(sk);
919 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
920 "%s: received %d bytes\n", tunnel->name, skb->len);
922 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
933 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
935 /************************************************************************
937 ***********************************************************************/
939 /* Build an L2TP header for the session into the buffer provided.
941 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
943 struct l2tp_tunnel *tunnel = session->tunnel;
946 u16 flags = L2TP_HDR_VER_2;
947 u32 tunnel_id = tunnel->peer_tunnel_id;
948 u32 session_id = session->peer_session_id;
950 if (session->send_seq)
951 flags |= L2TP_HDRFLAG_S;
953 /* Setup L2TP header. */
954 *bufp++ = htons(flags);
955 *bufp++ = htons(tunnel_id);
956 *bufp++ = htons(session_id);
957 if (session->send_seq) {
958 *bufp++ = htons(session->ns);
961 session->ns &= 0xffff;
962 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
963 "%s: updated ns to %u\n", session->name, session->ns);
969 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
971 struct l2tp_tunnel *tunnel = session->tunnel;
975 /* Setup L2TP header. The header differs slightly for UDP and
976 * IP encapsulations. For UDP, there is 4 bytes of flags.
978 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
979 u16 flags = L2TP_HDR_VER_3;
980 *((__be16 *) bufp) = htons(flags);
982 *((__be16 *) bufp) = 0;
986 *((__be32 *) bufp) = htonl(session->peer_session_id);
988 if (session->cookie_len) {
989 memcpy(bufp, &session->cookie[0], session->cookie_len);
990 bufp += session->cookie_len;
992 if (session->l2specific_len) {
993 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
995 if (session->send_seq) {
996 l2h = 0x40000000 | session->ns;
998 session->ns &= 0xffffff;
999 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
1000 "%s: updated ns to %u\n", session->name, session->ns);
1003 *((__be32 *) bufp) = htonl(l2h);
1005 bufp += session->l2specific_len;
1007 if (session->offset)
1008 bufp += session->offset;
1013 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1014 struct flowi *fl, size_t data_len)
1016 struct l2tp_tunnel *tunnel = session->tunnel;
1017 unsigned int len = skb->len;
1019 struct l2tp_stats *tstats, *sstats;
1022 if (session->send_seq)
1023 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
1024 "%s: send %Zd bytes, ns=%u\n", session->name,
1025 data_len, session->ns - 1);
1027 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
1028 "%s: send %Zd bytes\n", session->name, data_len);
1030 if (session->debug & L2TP_MSG_DATA) {
1032 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1033 unsigned char *datap = skb->data + uhlen;
1035 printk(KERN_DEBUG "%s: xmit:", session->name);
1036 for (i = 0; i < (len - uhlen); i++) {
1037 printk(" %02X", *datap++);
1046 /* Queue the packet to IP for output */
1048 #if IS_ENABLED(CONFIG_IPV6)
1049 if (skb->sk->sk_family == PF_INET6)
1050 error = inet6_csk_xmit(skb, NULL);
1053 error = ip_queue_xmit(skb, fl);
1056 tstats = &tunnel->stats;
1057 u64_stats_update_begin(&tstats->syncp);
1058 sstats = &session->stats;
1059 u64_stats_update_begin(&sstats->syncp);
1061 tstats->tx_packets++;
1062 tstats->tx_bytes += len;
1063 sstats->tx_packets++;
1064 sstats->tx_bytes += len;
1066 tstats->tx_errors++;
1067 sstats->tx_errors++;
1069 u64_stats_update_end(&tstats->syncp);
1070 u64_stats_update_end(&sstats->syncp);
1075 /* Automatically called when the skb is freed.
1077 static void l2tp_sock_wfree(struct sk_buff *skb)
1082 /* For data skbs that we transmit, we associate with the tunnel socket
1083 * but don't do accounting.
1085 static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1089 skb->destructor = l2tp_sock_wfree;
1092 #if IS_ENABLED(CONFIG_IPV6)
1093 static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1096 struct ipv6_pinfo *np = inet6_sk(sk);
1097 struct udphdr *uh = udp_hdr(skb);
1099 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1100 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1101 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1102 skb->ip_summed = CHECKSUM_UNNECESSARY;
1103 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1106 uh->check = CSUM_MANGLED_0;
1108 skb->ip_summed = CHECKSUM_PARTIAL;
1109 skb->csum_start = skb_transport_header(skb) - skb->head;
1110 skb->csum_offset = offsetof(struct udphdr, check);
1111 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1112 udp_len, IPPROTO_UDP, 0);
1117 /* If caller requires the skb to have a ppp header, the header must be
1118 * inserted in the skb data before calling this function.
1120 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1122 int data_len = skb->len;
1123 struct l2tp_tunnel *tunnel = session->tunnel;
1124 struct sock *sk = tunnel->sock;
1127 struct inet_sock *inet;
1132 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1135 /* Check that there's enough headroom in the skb to insert IP,
1136 * UDP and L2TP headers. If not enough, expand it to
1137 * make room. Adjust truesize.
1139 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1141 old_headroom = skb_headroom(skb);
1142 if (skb_cow_head(skb, headroom)) {
1147 new_headroom = skb_headroom(skb);
1149 skb->truesize += new_headroom - old_headroom;
1151 /* Setup L2TP header */
1152 session->build_header(session, __skb_push(skb, hdr_len));
1154 /* Reset skb netfilter state */
1155 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1156 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1161 if (sock_owned_by_user(sk)) {
1166 /* Get routing info from the tunnel socket */
1168 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
1171 fl = &inet->cork.fl;
1172 switch (tunnel->encap) {
1173 case L2TP_ENCAPTYPE_UDP:
1174 /* Setup UDP header */
1175 __skb_push(skb, sizeof(*uh));
1176 skb_reset_transport_header(skb);
1178 uh->source = inet->inet_sport;
1179 uh->dest = inet->inet_dport;
1180 udp_len = uhlen + hdr_len + data_len;
1181 uh->len = htons(udp_len);
1184 /* Calculate UDP checksum if configured to do so */
1185 #if IS_ENABLED(CONFIG_IPV6)
1186 if (sk->sk_family == PF_INET6)
1187 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1190 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1191 skb->ip_summed = CHECKSUM_NONE;
1192 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1193 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1194 skb->ip_summed = CHECKSUM_COMPLETE;
1195 csum = skb_checksum(skb, 0, udp_len, 0);
1196 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1198 udp_len, IPPROTO_UDP, csum);
1200 uh->check = CSUM_MANGLED_0;
1202 skb->ip_summed = CHECKSUM_PARTIAL;
1203 skb->csum_start = skb_transport_header(skb) - skb->head;
1204 skb->csum_offset = offsetof(struct udphdr, check);
1205 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1207 udp_len, IPPROTO_UDP, 0);
1211 case L2TP_ENCAPTYPE_IP:
1215 l2tp_skb_set_owner_w(skb, sk);
1217 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;
1238 tunnel = sk->sk_user_data;
1242 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1243 "%s: closing...\n", tunnel->name);
1245 /* Close all sessions */
1246 l2tp_tunnel_closeall(tunnel);
1248 switch (tunnel->encap) {
1249 case L2TP_ENCAPTYPE_UDP:
1250 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1251 (udp_sk(sk))->encap_type = 0;
1252 (udp_sk(sk))->encap_rcv = NULL;
1254 case L2TP_ENCAPTYPE_IP:
1258 /* Remove hooks into tunnel socket */
1259 tunnel->sock = NULL;
1260 sk->sk_destruct = tunnel->old_sk_destruct;
1261 sk->sk_user_data = NULL;
1263 /* Call the original destructor */
1264 if (sk->sk_destruct)
1265 (*sk->sk_destruct)(sk);
1267 /* We're finished with the socket */
1268 l2tp_tunnel_dec_refcount(tunnel);
1274 /* When the tunnel is closed, all the attached sessions need to go too.
1276 static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1279 struct hlist_node *walk;
1280 struct hlist_node *tmp;
1281 struct l2tp_session *session;
1283 BUG_ON(tunnel == NULL);
1285 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1286 "%s: closing all sessions...\n", tunnel->name);
1288 write_lock_bh(&tunnel->hlist_lock);
1289 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1291 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1292 session = hlist_entry(walk, struct l2tp_session, hlist);
1294 PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1295 "%s: closing session\n", session->name);
1297 hlist_del_init(&session->hlist);
1299 /* Since we should hold the sock lock while
1300 * doing any unbinding, we need to release the
1301 * lock we're holding before taking that lock.
1302 * Hold a reference to the sock so it doesn't
1303 * disappear as we're jumping between locks.
1305 if (session->ref != NULL)
1306 (*session->ref)(session);
1308 write_unlock_bh(&tunnel->hlist_lock);
1310 if (tunnel->version != L2TP_HDR_VER_2) {
1311 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1313 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1314 hlist_del_init_rcu(&session->global_hlist);
1315 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1319 if (session->session_close != NULL)
1320 (*session->session_close)(session);
1322 if (session->deref != NULL)
1323 (*session->deref)(session);
1325 write_lock_bh(&tunnel->hlist_lock);
1327 /* Now restart from the beginning of this hash
1328 * chain. We always remove a session from the
1329 * list so we are guaranteed to make forward
1335 write_unlock_bh(&tunnel->hlist_lock);
1338 /* Really kill the tunnel.
1339 * Come here only when all sessions have been cleared from the tunnel.
1341 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1343 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1345 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1346 BUG_ON(tunnel->sock != NULL);
1348 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1349 "%s: free...\n", tunnel->name);
1351 /* Remove from tunnel list */
1352 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1353 list_del_rcu(&tunnel->list);
1354 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1357 atomic_dec(&l2tp_tunnel_count);
1361 /* Create a socket for the tunnel, if one isn't set up by
1362 * userspace. This is used for static tunnels where there is no
1363 * managing L2TP daemon.
1365 static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
1368 struct sockaddr_in udp_addr;
1369 struct sockaddr_l2tpip ip_addr;
1370 struct socket *sock = NULL;
1372 switch (cfg->encap) {
1373 case L2TP_ENCAPTYPE_UDP:
1374 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1380 memset(&udp_addr, 0, sizeof(udp_addr));
1381 udp_addr.sin_family = AF_INET;
1382 udp_addr.sin_addr = cfg->local_ip;
1383 udp_addr.sin_port = htons(cfg->local_udp_port);
1384 err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
1388 udp_addr.sin_family = AF_INET;
1389 udp_addr.sin_addr = cfg->peer_ip;
1390 udp_addr.sin_port = htons(cfg->peer_udp_port);
1391 err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0);
1395 if (!cfg->use_udp_checksums)
1396 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1400 case L2TP_ENCAPTYPE_IP:
1401 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
1407 memset(&ip_addr, 0, sizeof(ip_addr));
1408 ip_addr.l2tp_family = AF_INET;
1409 ip_addr.l2tp_addr = cfg->local_ip;
1410 ip_addr.l2tp_conn_id = tunnel_id;
1411 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
1415 ip_addr.l2tp_family = AF_INET;
1416 ip_addr.l2tp_addr = cfg->peer_ip;
1417 ip_addr.l2tp_conn_id = peer_tunnel_id;
1418 err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
1429 if ((err < 0) && sock) {
1437 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)
1439 struct l2tp_tunnel *tunnel = NULL;
1441 struct socket *sock = NULL;
1442 struct sock *sk = NULL;
1443 struct l2tp_net *pn;
1444 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1446 /* Get the tunnel socket from the fd, which was opened by
1447 * the userspace L2TP daemon. If not specified, create a
1451 err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1456 sock = sockfd_lookup(fd, &err);
1458 printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1459 tunnel_id, fd, err);
1469 /* Quick sanity checks */
1471 case L2TP_ENCAPTYPE_UDP:
1472 err = -EPROTONOSUPPORT;
1473 if (sk->sk_protocol != IPPROTO_UDP) {
1474 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1475 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1479 case L2TP_ENCAPTYPE_IP:
1480 err = -EPROTONOSUPPORT;
1481 if (sk->sk_protocol != IPPROTO_L2TP) {
1482 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1483 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1489 /* Check if this socket has already been prepped */
1490 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1491 if (tunnel != NULL) {
1492 /* This socket has already been prepped */
1497 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1498 if (tunnel == NULL) {
1503 tunnel->version = version;
1504 tunnel->tunnel_id = tunnel_id;
1505 tunnel->peer_tunnel_id = peer_tunnel_id;
1506 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1508 tunnel->magic = L2TP_TUNNEL_MAGIC;
1509 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1510 rwlock_init(&tunnel->hlist_lock);
1512 /* The net we belong to */
1513 tunnel->l2tp_net = net;
1514 pn = l2tp_pernet(net);
1517 tunnel->debug = cfg->debug;
1519 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1520 tunnel->encap = encap;
1521 if (encap == L2TP_ENCAPTYPE_UDP) {
1522 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1523 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1524 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1525 #if IS_ENABLED(CONFIG_IPV6)
1526 if (sk->sk_family == PF_INET6)
1527 udpv6_encap_enable();
1533 sk->sk_user_data = tunnel;
1535 /* Hook on the tunnel socket destructor so that we can cleanup
1536 * if the tunnel socket goes away.
1538 tunnel->old_sk_destruct = sk->sk_destruct;
1539 sk->sk_destruct = &l2tp_tunnel_destruct;
1541 sk->sk_allocation = GFP_ATOMIC;
1543 /* Add tunnel to our list */
1544 INIT_LIST_HEAD(&tunnel->list);
1545 atomic_inc(&l2tp_tunnel_count);
1547 /* Bump the reference count. The tunnel context is deleted
1548 * only when this drops to zero. Must be done before list insertion
1550 l2tp_tunnel_inc_refcount(tunnel);
1551 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1552 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1553 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1560 /* If tunnel's socket was created by the kernel, it doesn't
1563 if (sock && sock->file)
1568 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1570 /* This function is used by the netlink TUNNEL_DELETE command.
1572 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1575 struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
1577 /* Force the tunnel socket to close. This will eventually
1578 * cause the tunnel to be deleted via the normal socket close
1579 * mechanisms when userspace closes the tunnel socket.
1582 err = inet_shutdown(sock, 2);
1584 /* If the tunnel's socket was created by the kernel,
1585 * close the socket here since the socket was not
1586 * created by userspace.
1588 if (sock->file == NULL)
1589 err = inet_release(sock);
1594 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1596 /* Really kill the session.
1598 void l2tp_session_free(struct l2tp_session *session)
1600 struct l2tp_tunnel *tunnel;
1602 BUG_ON(atomic_read(&session->ref_count) != 0);
1604 tunnel = session->tunnel;
1605 if (tunnel != NULL) {
1606 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1608 /* Delete the session from the hash */
1609 write_lock_bh(&tunnel->hlist_lock);
1610 hlist_del_init(&session->hlist);
1611 write_unlock_bh(&tunnel->hlist_lock);
1613 /* Unlink from the global hash if not L2TPv2 */
1614 if (tunnel->version != L2TP_HDR_VER_2) {
1615 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1617 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1618 hlist_del_init_rcu(&session->global_hlist);
1619 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1623 if (session->session_id != 0)
1624 atomic_dec(&l2tp_session_count);
1626 sock_put(tunnel->sock);
1628 /* This will delete the tunnel context if this
1629 * is the last session on the tunnel.
1631 session->tunnel = NULL;
1632 l2tp_tunnel_dec_refcount(tunnel);
1639 EXPORT_SYMBOL_GPL(l2tp_session_free);
1641 /* This function is used by the netlink SESSION_DELETE command and by
1644 int l2tp_session_delete(struct l2tp_session *session)
1646 if (session->session_close != NULL)
1647 (*session->session_close)(session);
1649 l2tp_session_dec_refcount(session);
1653 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1656 /* We come here whenever a session's send_seq, cookie_len or
1657 * l2specific_len parameters are set.
1659 static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1661 if (version == L2TP_HDR_VER_2) {
1662 session->hdr_len = 6;
1663 if (session->send_seq)
1664 session->hdr_len += 4;
1666 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1667 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1668 session->hdr_len += 4;
1673 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)
1675 struct l2tp_session *session;
1677 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1678 if (session != NULL) {
1679 session->magic = L2TP_SESSION_MAGIC;
1680 session->tunnel = tunnel;
1682 session->session_id = session_id;
1683 session->peer_session_id = peer_session_id;
1686 sprintf(&session->name[0], "sess %u/%u",
1687 tunnel->tunnel_id, session->session_id);
1689 skb_queue_head_init(&session->reorder_q);
1691 INIT_HLIST_NODE(&session->hlist);
1692 INIT_HLIST_NODE(&session->global_hlist);
1694 /* Inherit debug options from tunnel */
1695 session->debug = tunnel->debug;
1698 session->pwtype = cfg->pw_type;
1699 session->debug = cfg->debug;
1700 session->mtu = cfg->mtu;
1701 session->mru = cfg->mru;
1702 session->send_seq = cfg->send_seq;
1703 session->recv_seq = cfg->recv_seq;
1704 session->lns_mode = cfg->lns_mode;
1705 session->reorder_timeout = cfg->reorder_timeout;
1706 session->offset = cfg->offset;
1707 session->l2specific_type = cfg->l2specific_type;
1708 session->l2specific_len = cfg->l2specific_len;
1709 session->cookie_len = cfg->cookie_len;
1710 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1711 session->peer_cookie_len = cfg->peer_cookie_len;
1712 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1715 if (tunnel->version == L2TP_HDR_VER_2)
1716 session->build_header = l2tp_build_l2tpv2_header;
1718 session->build_header = l2tp_build_l2tpv3_header;
1720 l2tp_session_set_header_len(session, tunnel->version);
1722 /* Bump the reference count. The session context is deleted
1723 * only when this drops to zero.
1725 l2tp_session_inc_refcount(session);
1726 l2tp_tunnel_inc_refcount(tunnel);
1728 /* Ensure tunnel socket isn't deleted */
1729 sock_hold(tunnel->sock);
1731 /* Add session to the tunnel's hash list */
1732 write_lock_bh(&tunnel->hlist_lock);
1733 hlist_add_head(&session->hlist,
1734 l2tp_session_id_hash(tunnel, session_id));
1735 write_unlock_bh(&tunnel->hlist_lock);
1737 /* And to the global session list if L2TPv3 */
1738 if (tunnel->version != L2TP_HDR_VER_2) {
1739 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1741 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1742 hlist_add_head_rcu(&session->global_hlist,
1743 l2tp_session_id_hash_2(pn, session_id));
1744 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1747 /* Ignore management session in session count value */
1748 if (session->session_id != 0)
1749 atomic_inc(&l2tp_session_count);
1754 EXPORT_SYMBOL_GPL(l2tp_session_create);
1756 /*****************************************************************************
1758 *****************************************************************************/
1760 static __net_init int l2tp_init_net(struct net *net)
1762 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1765 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1766 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1768 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1769 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1771 spin_lock_init(&pn->l2tp_session_hlist_lock);
1776 static struct pernet_operations l2tp_net_ops = {
1777 .init = l2tp_init_net,
1779 .size = sizeof(struct l2tp_net),
1782 static int __init l2tp_init(void)
1786 rc = register_pernet_device(&l2tp_net_ops);
1790 printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1796 static void __exit l2tp_exit(void)
1798 unregister_pernet_device(&l2tp_net_ops);
1801 module_init(l2tp_init);
1802 module_exit(l2tp_exit);
1804 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1805 MODULE_DESCRIPTION("L2TP core");
1806 MODULE_LICENSE("GPL");
1807 MODULE_VERSION(L2TP_DRV_VERSION);