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>
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_SEQ 10
83 #define L2TP_HDR_SIZE_NOSEQ 6
85 /* Default trace flags */
86 #define L2TP_DEFAULT_DEBUG_FLAGS 0
88 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
90 if ((_mask) & (_type)) \
91 printk(_lvl "L2TP: " _fmt, ##args); \
94 /* Private data stored for received packets in the skb.
100 unsigned long expires;
103 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
105 static atomic_t l2tp_tunnel_count;
106 static atomic_t l2tp_session_count;
108 /* per-net private data for this module */
109 static unsigned int l2tp_net_id;
111 struct list_head l2tp_tunnel_list;
112 spinlock_t l2tp_tunnel_list_lock;
113 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
114 spinlock_t l2tp_session_hlist_lock;
117 static void l2tp_session_set_header_len(struct l2tp_session *session, int version);
118 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
119 static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
121 static inline struct l2tp_net *l2tp_pernet(struct net *net)
125 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) do { \
144 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
145 l2tp_tunnel_inc_refcount_1(_t); \
147 #define l2tp_tunnel_dec_refcount(_t) do { \
148 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
149 l2tp_tunnel_dec_refcount_1(_t); \
152 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
153 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
156 /* Session hash global list for L2TPv3.
157 * The session_id SHOULD be random according to RFC3931, but several
158 * L2TP implementations use incrementing session_ids. So we do a real
159 * hash on the session_id, rather than a simple bitmask.
161 static inline struct hlist_head *
162 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
164 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
168 /* Lookup a session by id in the global session list
170 static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
172 struct l2tp_net *pn = l2tp_pernet(net);
173 struct hlist_head *session_list =
174 l2tp_session_id_hash_2(pn, session_id);
175 struct l2tp_session *session;
176 struct hlist_node *walk;
179 hlist_for_each_entry_rcu(session, walk, session_list, global_hlist) {
180 if (session->session_id == session_id) {
181 rcu_read_unlock_bh();
185 rcu_read_unlock_bh();
190 /* Session hash list.
191 * The session_id SHOULD be random according to RFC2661, but several
192 * L2TP implementations (Cisco and Microsoft) use incrementing
193 * session_ids. So we do a real hash on the session_id, rather than a
196 static inline struct hlist_head *
197 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
199 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
202 /* Lookup a session by id
204 struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
206 struct hlist_head *session_list;
207 struct l2tp_session *session;
208 struct hlist_node *walk;
210 /* In L2TPv3, session_ids are unique over all tunnels and we
211 * sometimes need to look them up before we know the
215 return l2tp_session_find_2(net, session_id);
217 session_list = l2tp_session_id_hash(tunnel, session_id);
218 read_lock_bh(&tunnel->hlist_lock);
219 hlist_for_each_entry(session, walk, session_list, hlist) {
220 if (session->session_id == session_id) {
221 read_unlock_bh(&tunnel->hlist_lock);
225 read_unlock_bh(&tunnel->hlist_lock);
229 EXPORT_SYMBOL_GPL(l2tp_session_find);
231 struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
234 struct hlist_node *walk;
235 struct l2tp_session *session;
238 read_lock_bh(&tunnel->hlist_lock);
239 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
240 hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
242 read_unlock_bh(&tunnel->hlist_lock);
248 read_unlock_bh(&tunnel->hlist_lock);
252 EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
254 /* Lookup a session by interface name.
255 * This is very inefficient but is only used by management interfaces.
257 struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
259 struct l2tp_net *pn = l2tp_pernet(net);
261 struct hlist_node *walk;
262 struct l2tp_session *session;
265 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
266 hlist_for_each_entry_rcu(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) {
267 if (!strcmp(session->ifname, ifname)) {
268 rcu_read_unlock_bh();
274 rcu_read_unlock_bh();
278 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
280 /* Lookup a tunnel by id
282 struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
284 struct l2tp_tunnel *tunnel;
285 struct l2tp_net *pn = l2tp_pernet(net);
288 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
289 if (tunnel->tunnel_id == tunnel_id) {
290 rcu_read_unlock_bh();
294 rcu_read_unlock_bh();
298 EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
300 struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
302 struct l2tp_net *pn = l2tp_pernet(net);
303 struct l2tp_tunnel *tunnel;
307 list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
309 rcu_read_unlock_bh();
314 rcu_read_unlock_bh();
318 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
320 /*****************************************************************************
321 * Receive data handling
322 *****************************************************************************/
324 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
327 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
329 struct sk_buff *skbp;
331 u32 ns = L2TP_SKB_CB(skb)->ns;
333 spin_lock_bh(&session->reorder_q.lock);
334 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
335 if (L2TP_SKB_CB(skbp)->ns > ns) {
336 __skb_queue_before(&session->reorder_q, skbp, skb);
337 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
338 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
339 session->name, ns, L2TP_SKB_CB(skbp)->ns,
340 skb_queue_len(&session->reorder_q));
341 session->stats.rx_oos_packets++;
346 __skb_queue_tail(&session->reorder_q, skb);
349 spin_unlock_bh(&session->reorder_q.lock);
352 /* Dequeue a single skb.
354 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
356 struct l2tp_tunnel *tunnel = session->tunnel;
357 int length = L2TP_SKB_CB(skb)->length;
359 /* We're about to requeue the skb, so return resources
360 * to its current owner (a socket receive buffer).
364 tunnel->stats.rx_packets++;
365 tunnel->stats.rx_bytes += length;
366 session->stats.rx_packets++;
367 session->stats.rx_bytes += length;
369 if (L2TP_SKB_CB(skb)->has_seq) {
372 if (tunnel->version == L2TP_HDR_VER_2)
373 session->nr &= 0xffff;
375 session->nr &= 0xffffff;
377 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
378 "%s: updated nr to %hu\n", session->name, session->nr);
381 /* call private receive handler */
382 if (session->recv_skb != NULL)
383 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
388 (*session->deref)(session);
391 /* Dequeue skbs from the session's reorder_q, subject to packet order.
392 * Skbs that have been in the queue for too long are simply discarded.
394 static void l2tp_recv_dequeue(struct l2tp_session *session)
399 /* If the pkt at the head of the queue has the nr that we
400 * expect to send up next, dequeue it and any other
401 * in-sequence packets behind it.
404 spin_lock_bh(&session->reorder_q.lock);
405 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
406 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
407 session->stats.rx_seq_discards++;
408 session->stats.rx_errors++;
409 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
410 "%s: oos pkt %u len %d discarded (too old), "
411 "waiting for %u, reorder_q_len=%d\n",
412 session->name, L2TP_SKB_CB(skb)->ns,
413 L2TP_SKB_CB(skb)->length, session->nr,
414 skb_queue_len(&session->reorder_q));
415 __skb_unlink(skb, &session->reorder_q);
418 (*session->deref)(session);
422 if (L2TP_SKB_CB(skb)->has_seq) {
423 if (L2TP_SKB_CB(skb)->ns != session->nr) {
424 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
425 "%s: holding oos pkt %u len %d, "
426 "waiting for %u, reorder_q_len=%d\n",
427 session->name, L2TP_SKB_CB(skb)->ns,
428 L2TP_SKB_CB(skb)->length, session->nr,
429 skb_queue_len(&session->reorder_q));
433 __skb_unlink(skb, &session->reorder_q);
435 /* Process the skb. We release the queue lock while we
436 * do so to let other contexts process the queue.
438 spin_unlock_bh(&session->reorder_q.lock);
439 l2tp_recv_dequeue_skb(session, skb);
444 spin_unlock_bh(&session->reorder_q.lock);
447 static inline int l2tp_verify_udp_checksum(struct sock *sk,
450 struct udphdr *uh = udp_hdr(skb);
451 u16 ulen = ntohs(uh->len);
454 if (sk->sk_no_check || skb_csum_unnecessary(skb))
457 #if IS_ENABLED(CONFIG_IPV6)
458 if (sk->sk_family == PF_INET6) {
460 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
463 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
464 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
465 &ipv6_hdr(skb)->daddr, ulen,
466 IPPROTO_UDP, skb->csum)) {
467 skb->ip_summed = CHECKSUM_UNNECESSARY;
470 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
471 &ipv6_hdr(skb)->daddr,
472 skb->len, IPPROTO_UDP,
477 struct inet_sock *inet;
481 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
482 ulen, IPPROTO_UDP, 0);
484 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
485 !csum_fold(csum_add(psum, skb->csum)))
490 return __skb_checksum_complete(skb);
493 /* Do receive processing of L2TP data frames. We handle both L2TPv2
494 * and L2TPv3 data frames here.
496 * L2TPv2 Data Message Header
499 * 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
500 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
501 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
502 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
503 * | Tunnel ID | Session ID |
504 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
505 * | Ns (opt) | Nr (opt) |
506 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
507 * | Offset Size (opt) | Offset pad... (opt)
508 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
510 * Data frames are marked by T=0. All other fields are the same as
511 * those in L2TP control frames.
513 * L2TPv3 Data Message Header
515 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
516 * | L2TP Session Header |
517 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
518 * | L2-Specific Sublayer |
519 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
520 * | Tunnel Payload ...
521 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
523 * L2TPv3 Session Header Over IP
526 * 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
527 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
529 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
530 * | Cookie (optional, maximum 64 bits)...
531 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
533 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
535 * L2TPv3 L2-Specific Sublayer Format
538 * 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
539 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
540 * |x|S|x|x|x|x|x|x| Sequence Number |
541 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
543 * Cookie value, sublayer format and offset (pad) are negotiated with
544 * the peer when the session is set up. Unlike L2TPv2, we do not need
545 * to parse the packet header to determine if optional fields are
548 * Caller must already have parsed the frame and determined that it is
549 * a data (not control) frame before coming here. Fields up to the
550 * session-id have already been parsed and ptr points to the data
551 * after the session-id.
553 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
554 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
555 int length, int (*payload_hook)(struct sk_buff *skb))
557 struct l2tp_tunnel *tunnel = session->tunnel;
561 /* The ref count is increased since we now hold a pointer to
562 * the session. Take care to decrement the refcnt when exiting
563 * this function from now on...
565 l2tp_session_inc_refcount(session);
567 (*session->ref)(session);
569 /* Parse and check optional cookie */
570 if (session->peer_cookie_len > 0) {
571 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
572 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
573 "%s: cookie mismatch (%u/%u). Discarding.\n",
574 tunnel->name, tunnel->tunnel_id, session->session_id);
575 session->stats.rx_cookie_discards++;
578 ptr += session->peer_cookie_len;
581 /* Handle the optional sequence numbers. Sequence numbers are
582 * in different places for L2TPv2 and L2TPv3.
584 * If we are the LAC, enable/disable sequence numbers under
585 * the control of the LNS. If no sequence numbers present but
586 * we were expecting them, discard frame.
589 L2TP_SKB_CB(skb)->has_seq = 0;
590 if (tunnel->version == L2TP_HDR_VER_2) {
591 if (hdrflags & L2TP_HDRFLAG_S) {
592 ns = ntohs(*(__be16 *) ptr);
594 nr = ntohs(*(__be16 *) ptr);
597 /* Store L2TP info in the skb */
598 L2TP_SKB_CB(skb)->ns = ns;
599 L2TP_SKB_CB(skb)->has_seq = 1;
601 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
602 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
603 session->name, ns, nr, session->nr);
605 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
606 u32 l2h = ntohl(*(__be32 *) ptr);
608 if (l2h & 0x40000000) {
609 ns = l2h & 0x00ffffff;
611 /* Store L2TP info in the skb */
612 L2TP_SKB_CB(skb)->ns = ns;
613 L2TP_SKB_CB(skb)->has_seq = 1;
615 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
616 "%s: recv data ns=%u, session nr=%u\n",
617 session->name, ns, session->nr);
621 /* Advance past L2-specific header, if present */
622 ptr += session->l2specific_len;
624 if (L2TP_SKB_CB(skb)->has_seq) {
625 /* Received a packet with sequence numbers. If we're the LNS,
626 * check if we sre sending sequence numbers and if not,
629 if ((!session->lns_mode) && (!session->send_seq)) {
630 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
631 "%s: requested to enable seq numbers by LNS\n",
633 session->send_seq = -1;
634 l2tp_session_set_header_len(session, tunnel->version);
637 /* No sequence numbers.
638 * If user has configured mandatory sequence numbers, discard.
640 if (session->recv_seq) {
641 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
642 "%s: recv data has no seq numbers when required. "
643 "Discarding\n", session->name);
644 session->stats.rx_seq_discards++;
648 /* If we're the LAC and we're sending sequence numbers, the
649 * LNS has requested that we no longer send sequence numbers.
650 * If we're the LNS and we're sending sequence numbers, the
651 * LAC is broken. Discard the frame.
653 if ((!session->lns_mode) && (session->send_seq)) {
654 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
655 "%s: requested to disable seq numbers by LNS\n",
657 session->send_seq = 0;
658 l2tp_session_set_header_len(session, tunnel->version);
659 } else if (session->send_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 session->stats.rx_seq_discards++;
668 /* Session data offset is handled differently for L2TPv2 and
669 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
670 * the header. For L2TPv3, the offset is negotiated using AVPs
671 * in the session setup control protocol.
673 if (tunnel->version == L2TP_HDR_VER_2) {
674 /* If offset bit set, skip it. */
675 if (hdrflags & L2TP_HDRFLAG_O) {
676 offset = ntohs(*(__be16 *)ptr);
680 ptr += session->offset;
683 if (!pskb_may_pull(skb, offset))
686 __skb_pull(skb, offset);
688 /* If caller wants to process the payload before we queue the
692 if ((*payload_hook)(skb))
695 /* Prepare skb for adding to the session's reorder_q. Hold
696 * packets for max reorder_timeout or 1 second if not
699 L2TP_SKB_CB(skb)->length = length;
700 L2TP_SKB_CB(skb)->expires = jiffies +
701 (session->reorder_timeout ? session->reorder_timeout : HZ);
703 /* Add packet to the session's receive queue. Reordering is done here, if
704 * enabled. Saved L2TP protocol info is stored in skb->sb[].
706 if (L2TP_SKB_CB(skb)->has_seq) {
707 if (session->reorder_timeout != 0) {
708 /* Packet reordering enabled. Add skb to session's
709 * reorder queue, in order of ns.
711 l2tp_recv_queue_skb(session, skb);
713 /* Packet reordering disabled. Discard out-of-sequence
716 if (L2TP_SKB_CB(skb)->ns != session->nr) {
717 session->stats.rx_seq_discards++;
718 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
719 "%s: oos pkt %u len %d discarded, "
720 "waiting for %u, reorder_q_len=%d\n",
721 session->name, L2TP_SKB_CB(skb)->ns,
722 L2TP_SKB_CB(skb)->length, session->nr,
723 skb_queue_len(&session->reorder_q));
726 skb_queue_tail(&session->reorder_q, skb);
729 /* No sequence numbers. Add the skb to the tail of the
730 * reorder queue. This ensures that it will be
731 * delivered after all previous sequenced skbs.
733 skb_queue_tail(&session->reorder_q, skb);
736 /* Try to dequeue as many skbs from reorder_q as we can. */
737 l2tp_recv_dequeue(session);
739 l2tp_session_dec_refcount(session);
744 session->stats.rx_errors++;
748 (*session->deref)(session);
750 l2tp_session_dec_refcount(session);
752 EXPORT_SYMBOL(l2tp_recv_common);
754 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
755 * here. The skb is not on a list when we get here.
756 * Returns 0 if the packet was a data packet and was successfully passed on.
757 * Returns 1 if the packet was not a good data packet and could not be
758 * forwarded. All such packets are passed up to userspace to deal with.
760 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
761 int (*payload_hook)(struct sk_buff *skb))
763 struct l2tp_session *session = NULL;
764 unsigned char *ptr, *optr;
766 u32 tunnel_id, session_id;
771 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
772 goto discard_bad_csum;
774 /* UDP always verifies the packet length. */
775 __skb_pull(skb, sizeof(struct udphdr));
778 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
779 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
780 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
784 /* Trace packet contents, if enabled */
785 if (tunnel->debug & L2TP_MSG_DATA) {
786 length = min(32u, skb->len);
787 if (!pskb_may_pull(skb, length))
790 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
794 printk(" %02X", skb->data[offset]);
795 } while (++offset < length);
800 /* Point to L2TP header */
801 optr = ptr = skb->data;
803 /* Get L2TP header flags */
804 hdrflags = ntohs(*(__be16 *) ptr);
806 /* Check protocol version */
807 version = hdrflags & L2TP_HDR_VER_MASK;
808 if (version != tunnel->version) {
809 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
810 "%s: recv protocol version mismatch: got %d expected %d\n",
811 tunnel->name, version, tunnel->version);
815 /* Get length of L2TP packet */
818 /* If type is control packet, it is handled by userspace. */
819 if (hdrflags & L2TP_HDRFLAG_T) {
820 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
821 "%s: recv control packet, len=%d\n", tunnel->name, length);
828 if (tunnel->version == L2TP_HDR_VER_2) {
829 /* If length is present, skip it */
830 if (hdrflags & L2TP_HDRFLAG_L)
833 /* Extract tunnel and session ID */
834 tunnel_id = ntohs(*(__be16 *) ptr);
836 session_id = ntohs(*(__be16 *) ptr);
839 ptr += 2; /* skip reserved bits */
840 tunnel_id = tunnel->tunnel_id;
841 session_id = ntohl(*(__be32 *) ptr);
845 /* Find the session context */
846 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
847 if (!session || !session->recv_skb) {
848 /* Not found? Pass to userspace to deal with */
849 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
850 "%s: no session found (%u/%u). Passing up.\n",
851 tunnel->name, tunnel_id, session_id);
855 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
860 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
861 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
862 tunnel->stats.rx_errors++;
868 /* Put UDP header back */
869 __skb_push(skb, sizeof(struct udphdr));
874 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
878 * >0: skb should be passed up to userspace as UDP.
880 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
882 struct l2tp_tunnel *tunnel;
884 tunnel = l2tp_sock_to_tunnel(sk);
888 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
889 "%s: received %d bytes\n", tunnel->name, skb->len);
891 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
902 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
904 /************************************************************************
906 ***********************************************************************/
908 /* Build an L2TP header for the session into the buffer provided.
910 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
912 struct l2tp_tunnel *tunnel = session->tunnel;
915 u16 flags = L2TP_HDR_VER_2;
916 u32 tunnel_id = tunnel->peer_tunnel_id;
917 u32 session_id = session->peer_session_id;
919 if (session->send_seq)
920 flags |= L2TP_HDRFLAG_S;
922 /* Setup L2TP header. */
923 *bufp++ = htons(flags);
924 *bufp++ = htons(tunnel_id);
925 *bufp++ = htons(session_id);
926 if (session->send_seq) {
927 *bufp++ = htons(session->ns);
930 session->ns &= 0xffff;
931 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
932 "%s: updated ns to %u\n", session->name, session->ns);
938 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
940 struct l2tp_tunnel *tunnel = session->tunnel;
944 /* Setup L2TP header. The header differs slightly for UDP and
945 * IP encapsulations. For UDP, there is 4 bytes of flags.
947 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
948 u16 flags = L2TP_HDR_VER_3;
949 *((__be16 *) bufp) = htons(flags);
951 *((__be16 *) bufp) = 0;
955 *((__be32 *) bufp) = htonl(session->peer_session_id);
957 if (session->cookie_len) {
958 memcpy(bufp, &session->cookie[0], session->cookie_len);
959 bufp += session->cookie_len;
961 if (session->l2specific_len) {
962 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
964 if (session->send_seq) {
965 l2h = 0x40000000 | session->ns;
967 session->ns &= 0xffffff;
968 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
969 "%s: updated ns to %u\n", session->name, session->ns);
972 *((__be32 *) bufp) = htonl(l2h);
974 bufp += session->l2specific_len;
977 bufp += session->offset;
982 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
983 struct flowi *fl, size_t data_len)
985 struct l2tp_tunnel *tunnel = session->tunnel;
986 unsigned int len = skb->len;
990 if (session->send_seq)
991 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
992 "%s: send %Zd bytes, ns=%u\n", session->name,
993 data_len, session->ns - 1);
995 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
996 "%s: send %Zd bytes\n", session->name, data_len);
998 if (session->debug & L2TP_MSG_DATA) {
1000 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1001 unsigned char *datap = skb->data + uhlen;
1003 printk(KERN_DEBUG "%s: xmit:", session->name);
1004 for (i = 0; i < (len - uhlen); i++) {
1005 printk(" %02X", *datap++);
1014 /* Queue the packet to IP for output */
1016 #if IS_ENABLED(CONFIG_IPV6)
1017 if (skb->sk->sk_family == PF_INET6)
1018 error = inet6_csk_xmit(skb, NULL);
1021 error = ip_queue_xmit(skb, fl);
1025 tunnel->stats.tx_packets++;
1026 tunnel->stats.tx_bytes += len;
1027 session->stats.tx_packets++;
1028 session->stats.tx_bytes += len;
1030 tunnel->stats.tx_errors++;
1031 session->stats.tx_errors++;
1037 /* Automatically called when the skb is freed.
1039 static void l2tp_sock_wfree(struct sk_buff *skb)
1044 /* For data skbs that we transmit, we associate with the tunnel socket
1045 * but don't do accounting.
1047 static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1051 skb->destructor = l2tp_sock_wfree;
1054 #if IS_ENABLED(CONFIG_IPV6)
1055 static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1058 struct ipv6_pinfo *np = inet6_sk(sk);
1059 struct udphdr *uh = udp_hdr(skb);
1061 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1062 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1063 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1064 skb->ip_summed = CHECKSUM_UNNECESSARY;
1065 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1068 uh->check = CSUM_MANGLED_0;
1070 skb->ip_summed = CHECKSUM_PARTIAL;
1071 skb->csum_start = skb_transport_header(skb) - skb->head;
1072 skb->csum_offset = offsetof(struct udphdr, check);
1073 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1074 udp_len, IPPROTO_UDP, 0);
1079 /* If caller requires the skb to have a ppp header, the header must be
1080 * inserted in the skb data before calling this function.
1082 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1084 int data_len = skb->len;
1085 struct l2tp_tunnel *tunnel = session->tunnel;
1086 struct sock *sk = tunnel->sock;
1089 struct inet_sock *inet;
1094 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1097 /* Check that there's enough headroom in the skb to insert IP,
1098 * UDP and L2TP headers. If not enough, expand it to
1099 * make room. Adjust truesize.
1101 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1103 old_headroom = skb_headroom(skb);
1104 if (skb_cow_head(skb, headroom)) {
1109 new_headroom = skb_headroom(skb);
1111 skb->truesize += new_headroom - old_headroom;
1113 /* Setup L2TP header */
1114 session->build_header(session, __skb_push(skb, hdr_len));
1116 /* Reset skb netfilter state */
1117 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1118 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1123 if (sock_owned_by_user(sk)) {
1128 /* Get routing info from the tunnel socket */
1130 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
1133 fl = &inet->cork.fl;
1134 switch (tunnel->encap) {
1135 case L2TP_ENCAPTYPE_UDP:
1136 /* Setup UDP header */
1137 __skb_push(skb, sizeof(*uh));
1138 skb_reset_transport_header(skb);
1140 uh->source = inet->inet_sport;
1141 uh->dest = inet->inet_dport;
1142 udp_len = uhlen + hdr_len + data_len;
1143 uh->len = htons(udp_len);
1146 /* Calculate UDP checksum if configured to do so */
1147 #if IS_ENABLED(CONFIG_IPV6)
1148 if (sk->sk_family == PF_INET6)
1149 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1152 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1153 skb->ip_summed = CHECKSUM_NONE;
1154 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1155 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1156 skb->ip_summed = CHECKSUM_COMPLETE;
1157 csum = skb_checksum(skb, 0, udp_len, 0);
1158 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1160 udp_len, IPPROTO_UDP, csum);
1162 uh->check = CSUM_MANGLED_0;
1164 skb->ip_summed = CHECKSUM_PARTIAL;
1165 skb->csum_start = skb_transport_header(skb) - skb->head;
1166 skb->csum_offset = offsetof(struct udphdr, check);
1167 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1169 udp_len, IPPROTO_UDP, 0);
1173 case L2TP_ENCAPTYPE_IP:
1177 l2tp_skb_set_owner_w(skb, sk);
1179 l2tp_xmit_core(session, skb, fl, data_len);
1186 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1188 /*****************************************************************************
1189 * Tinnel and session create/destroy.
1190 *****************************************************************************/
1192 /* Tunnel socket destruct hook.
1193 * The tunnel context is deleted only when all session sockets have been
1196 static void l2tp_tunnel_destruct(struct sock *sk)
1198 struct l2tp_tunnel *tunnel;
1200 tunnel = sk->sk_user_data;
1204 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1205 "%s: closing...\n", tunnel->name);
1207 /* Close all sessions */
1208 l2tp_tunnel_closeall(tunnel);
1210 switch (tunnel->encap) {
1211 case L2TP_ENCAPTYPE_UDP:
1212 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1213 (udp_sk(sk))->encap_type = 0;
1214 (udp_sk(sk))->encap_rcv = NULL;
1216 case L2TP_ENCAPTYPE_IP:
1220 /* Remove hooks into tunnel socket */
1221 tunnel->sock = NULL;
1222 sk->sk_destruct = tunnel->old_sk_destruct;
1223 sk->sk_user_data = NULL;
1225 /* Call the original destructor */
1226 if (sk->sk_destruct)
1227 (*sk->sk_destruct)(sk);
1229 /* We're finished with the socket */
1230 l2tp_tunnel_dec_refcount(tunnel);
1236 /* When the tunnel is closed, all the attached sessions need to go too.
1238 static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1241 struct hlist_node *walk;
1242 struct hlist_node *tmp;
1243 struct l2tp_session *session;
1245 BUG_ON(tunnel == NULL);
1247 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1248 "%s: closing all sessions...\n", tunnel->name);
1250 write_lock_bh(&tunnel->hlist_lock);
1251 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1253 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1254 session = hlist_entry(walk, struct l2tp_session, hlist);
1256 PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1257 "%s: closing session\n", session->name);
1259 hlist_del_init(&session->hlist);
1261 /* Since we should hold the sock lock while
1262 * doing any unbinding, we need to release the
1263 * lock we're holding before taking that lock.
1264 * Hold a reference to the sock so it doesn't
1265 * disappear as we're jumping between locks.
1267 if (session->ref != NULL)
1268 (*session->ref)(session);
1270 write_unlock_bh(&tunnel->hlist_lock);
1272 if (tunnel->version != L2TP_HDR_VER_2) {
1273 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1275 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1276 hlist_del_init_rcu(&session->global_hlist);
1277 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1281 if (session->session_close != NULL)
1282 (*session->session_close)(session);
1284 if (session->deref != NULL)
1285 (*session->deref)(session);
1287 write_lock_bh(&tunnel->hlist_lock);
1289 /* Now restart from the beginning of this hash
1290 * chain. We always remove a session from the
1291 * list so we are guaranteed to make forward
1297 write_unlock_bh(&tunnel->hlist_lock);
1300 /* Really kill the tunnel.
1301 * Come here only when all sessions have been cleared from the tunnel.
1303 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1305 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1307 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1308 BUG_ON(tunnel->sock != NULL);
1310 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1311 "%s: free...\n", tunnel->name);
1313 /* Remove from tunnel list */
1314 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1315 list_del_rcu(&tunnel->list);
1316 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1319 atomic_dec(&l2tp_tunnel_count);
1323 /* Create a socket for the tunnel, if one isn't set up by
1324 * userspace. This is used for static tunnels where there is no
1325 * managing L2TP daemon.
1327 static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
1330 struct sockaddr_in udp_addr;
1331 struct sockaddr_l2tpip ip_addr;
1332 struct socket *sock = NULL;
1334 switch (cfg->encap) {
1335 case L2TP_ENCAPTYPE_UDP:
1336 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1342 memset(&udp_addr, 0, sizeof(udp_addr));
1343 udp_addr.sin_family = AF_INET;
1344 udp_addr.sin_addr = cfg->local_ip;
1345 udp_addr.sin_port = htons(cfg->local_udp_port);
1346 err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
1350 udp_addr.sin_family = AF_INET;
1351 udp_addr.sin_addr = cfg->peer_ip;
1352 udp_addr.sin_port = htons(cfg->peer_udp_port);
1353 err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0);
1357 if (!cfg->use_udp_checksums)
1358 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1362 case L2TP_ENCAPTYPE_IP:
1363 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
1369 memset(&ip_addr, 0, sizeof(ip_addr));
1370 ip_addr.l2tp_family = AF_INET;
1371 ip_addr.l2tp_addr = cfg->local_ip;
1372 ip_addr.l2tp_conn_id = tunnel_id;
1373 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
1377 ip_addr.l2tp_family = AF_INET;
1378 ip_addr.l2tp_addr = cfg->peer_ip;
1379 ip_addr.l2tp_conn_id = peer_tunnel_id;
1380 err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
1391 if ((err < 0) && sock) {
1399 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)
1401 struct l2tp_tunnel *tunnel = NULL;
1403 struct socket *sock = NULL;
1404 struct sock *sk = NULL;
1405 struct l2tp_net *pn;
1406 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1408 /* Get the tunnel socket from the fd, which was opened by
1409 * the userspace L2TP daemon. If not specified, create a
1413 err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1418 sock = sockfd_lookup(fd, &err);
1420 printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1421 tunnel_id, fd, err);
1431 /* Quick sanity checks */
1433 case L2TP_ENCAPTYPE_UDP:
1434 err = -EPROTONOSUPPORT;
1435 if (sk->sk_protocol != IPPROTO_UDP) {
1436 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1437 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1441 case L2TP_ENCAPTYPE_IP:
1442 err = -EPROTONOSUPPORT;
1443 if (sk->sk_protocol != IPPROTO_L2TP) {
1444 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1445 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1451 /* Check if this socket has already been prepped */
1452 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1453 if (tunnel != NULL) {
1454 /* This socket has already been prepped */
1459 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1460 if (tunnel == NULL) {
1465 tunnel->version = version;
1466 tunnel->tunnel_id = tunnel_id;
1467 tunnel->peer_tunnel_id = peer_tunnel_id;
1468 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1470 tunnel->magic = L2TP_TUNNEL_MAGIC;
1471 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1472 rwlock_init(&tunnel->hlist_lock);
1474 /* The net we belong to */
1475 tunnel->l2tp_net = net;
1476 pn = l2tp_pernet(net);
1479 tunnel->debug = cfg->debug;
1481 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1482 tunnel->encap = encap;
1483 if (encap == L2TP_ENCAPTYPE_UDP) {
1484 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1485 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1486 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1487 #if IS_ENABLED(CONFIG_IPV6)
1488 if (sk->sk_family == PF_INET6)
1489 udpv6_encap_enable();
1495 sk->sk_user_data = tunnel;
1497 /* Hook on the tunnel socket destructor so that we can cleanup
1498 * if the tunnel socket goes away.
1500 tunnel->old_sk_destruct = sk->sk_destruct;
1501 sk->sk_destruct = &l2tp_tunnel_destruct;
1503 sk->sk_allocation = GFP_ATOMIC;
1505 /* Add tunnel to our list */
1506 INIT_LIST_HEAD(&tunnel->list);
1507 atomic_inc(&l2tp_tunnel_count);
1509 /* Bump the reference count. The tunnel context is deleted
1510 * only when this drops to zero. Must be done before list insertion
1512 l2tp_tunnel_inc_refcount(tunnel);
1513 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1514 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1515 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1522 /* If tunnel's socket was created by the kernel, it doesn't
1525 if (sock && sock->file)
1530 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1532 /* This function is used by the netlink TUNNEL_DELETE command.
1534 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1537 struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
1539 /* Force the tunnel socket to close. This will eventually
1540 * cause the tunnel to be deleted via the normal socket close
1541 * mechanisms when userspace closes the tunnel socket.
1544 err = inet_shutdown(sock, 2);
1546 /* If the tunnel's socket was created by the kernel,
1547 * close the socket here since the socket was not
1548 * created by userspace.
1550 if (sock->file == NULL)
1551 err = inet_release(sock);
1556 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1558 /* Really kill the session.
1560 void l2tp_session_free(struct l2tp_session *session)
1562 struct l2tp_tunnel *tunnel;
1564 BUG_ON(atomic_read(&session->ref_count) != 0);
1566 tunnel = session->tunnel;
1567 if (tunnel != NULL) {
1568 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1570 /* Delete the session from the hash */
1571 write_lock_bh(&tunnel->hlist_lock);
1572 hlist_del_init(&session->hlist);
1573 write_unlock_bh(&tunnel->hlist_lock);
1575 /* Unlink from the global hash if not L2TPv2 */
1576 if (tunnel->version != L2TP_HDR_VER_2) {
1577 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1579 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1580 hlist_del_init_rcu(&session->global_hlist);
1581 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1585 if (session->session_id != 0)
1586 atomic_dec(&l2tp_session_count);
1588 sock_put(tunnel->sock);
1590 /* This will delete the tunnel context if this
1591 * is the last session on the tunnel.
1593 session->tunnel = NULL;
1594 l2tp_tunnel_dec_refcount(tunnel);
1601 EXPORT_SYMBOL_GPL(l2tp_session_free);
1603 /* This function is used by the netlink SESSION_DELETE command and by
1606 int l2tp_session_delete(struct l2tp_session *session)
1608 if (session->session_close != NULL)
1609 (*session->session_close)(session);
1611 l2tp_session_dec_refcount(session);
1615 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1618 /* We come here whenever a session's send_seq, cookie_len or
1619 * l2specific_len parameters are set.
1621 static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1623 if (version == L2TP_HDR_VER_2) {
1624 session->hdr_len = 6;
1625 if (session->send_seq)
1626 session->hdr_len += 4;
1628 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1629 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1630 session->hdr_len += 4;
1635 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)
1637 struct l2tp_session *session;
1639 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1640 if (session != NULL) {
1641 session->magic = L2TP_SESSION_MAGIC;
1642 session->tunnel = tunnel;
1644 session->session_id = session_id;
1645 session->peer_session_id = peer_session_id;
1648 sprintf(&session->name[0], "sess %u/%u",
1649 tunnel->tunnel_id, session->session_id);
1651 skb_queue_head_init(&session->reorder_q);
1653 INIT_HLIST_NODE(&session->hlist);
1654 INIT_HLIST_NODE(&session->global_hlist);
1656 /* Inherit debug options from tunnel */
1657 session->debug = tunnel->debug;
1660 session->pwtype = cfg->pw_type;
1661 session->debug = cfg->debug;
1662 session->mtu = cfg->mtu;
1663 session->mru = cfg->mru;
1664 session->send_seq = cfg->send_seq;
1665 session->recv_seq = cfg->recv_seq;
1666 session->lns_mode = cfg->lns_mode;
1667 session->reorder_timeout = cfg->reorder_timeout;
1668 session->offset = cfg->offset;
1669 session->l2specific_type = cfg->l2specific_type;
1670 session->l2specific_len = cfg->l2specific_len;
1671 session->cookie_len = cfg->cookie_len;
1672 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1673 session->peer_cookie_len = cfg->peer_cookie_len;
1674 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1677 if (tunnel->version == L2TP_HDR_VER_2)
1678 session->build_header = l2tp_build_l2tpv2_header;
1680 session->build_header = l2tp_build_l2tpv3_header;
1682 l2tp_session_set_header_len(session, tunnel->version);
1684 /* Bump the reference count. The session context is deleted
1685 * only when this drops to zero.
1687 l2tp_session_inc_refcount(session);
1688 l2tp_tunnel_inc_refcount(tunnel);
1690 /* Ensure tunnel socket isn't deleted */
1691 sock_hold(tunnel->sock);
1693 /* Add session to the tunnel's hash list */
1694 write_lock_bh(&tunnel->hlist_lock);
1695 hlist_add_head(&session->hlist,
1696 l2tp_session_id_hash(tunnel, session_id));
1697 write_unlock_bh(&tunnel->hlist_lock);
1699 /* And to the global session list if L2TPv3 */
1700 if (tunnel->version != L2TP_HDR_VER_2) {
1701 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1703 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1704 hlist_add_head_rcu(&session->global_hlist,
1705 l2tp_session_id_hash_2(pn, session_id));
1706 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1709 /* Ignore management session in session count value */
1710 if (session->session_id != 0)
1711 atomic_inc(&l2tp_session_count);
1716 EXPORT_SYMBOL_GPL(l2tp_session_create);
1718 /*****************************************************************************
1720 *****************************************************************************/
1722 static __net_init int l2tp_init_net(struct net *net)
1724 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1727 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1728 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1730 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1731 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1733 spin_lock_init(&pn->l2tp_session_hlist_lock);
1738 static struct pernet_operations l2tp_net_ops = {
1739 .init = l2tp_init_net,
1741 .size = sizeof(struct l2tp_net),
1744 static int __init l2tp_init(void)
1748 rc = register_pernet_device(&l2tp_net_ops);
1752 printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1758 static void __exit l2tp_exit(void)
1760 unregister_pernet_device(&l2tp_net_ops);
1763 module_init(l2tp_init);
1764 module_exit(l2tp_exit);
1766 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1767 MODULE_DESCRIPTION("L2TP core");
1768 MODULE_LICENSE("GPL");
1769 MODULE_VERSION(L2TP_DRV_VERSION);