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;
334 spin_lock_bh(&session->reorder_q.lock);
335 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
336 if (L2TP_SKB_CB(skbp)->ns > ns) {
337 __skb_queue_before(&session->reorder_q, skbp, skb);
338 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
339 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
340 session->name, ns, L2TP_SKB_CB(skbp)->ns,
341 skb_queue_len(&session->reorder_q));
342 session->stats.rx_oos_packets++;
347 __skb_queue_tail(&session->reorder_q, skb);
350 spin_unlock_bh(&session->reorder_q.lock);
353 /* Dequeue a single skb.
355 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
357 struct l2tp_tunnel *tunnel = session->tunnel;
358 int length = L2TP_SKB_CB(skb)->length;
360 /* We're about to requeue the skb, so return resources
361 * to its current owner (a socket receive buffer).
365 tunnel->stats.rx_packets++;
366 tunnel->stats.rx_bytes += length;
367 session->stats.rx_packets++;
368 session->stats.rx_bytes += length;
370 if (L2TP_SKB_CB(skb)->has_seq) {
373 if (tunnel->version == L2TP_HDR_VER_2)
374 session->nr &= 0xffff;
376 session->nr &= 0xffffff;
378 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
379 "%s: updated nr to %hu\n", session->name, session->nr);
382 /* call private receive handler */
383 if (session->recv_skb != NULL)
384 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
389 (*session->deref)(session);
392 /* Dequeue skbs from the session's reorder_q, subject to packet order.
393 * Skbs that have been in the queue for too long are simply discarded.
395 static void l2tp_recv_dequeue(struct l2tp_session *session)
400 /* If the pkt at the head of the queue has the nr that we
401 * expect to send up next, dequeue it and any other
402 * in-sequence packets behind it.
405 spin_lock_bh(&session->reorder_q.lock);
406 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
407 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
408 session->stats.rx_seq_discards++;
409 session->stats.rx_errors++;
410 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
411 "%s: oos pkt %u len %d discarded (too old), "
412 "waiting for %u, reorder_q_len=%d\n",
413 session->name, L2TP_SKB_CB(skb)->ns,
414 L2TP_SKB_CB(skb)->length, session->nr,
415 skb_queue_len(&session->reorder_q));
416 __skb_unlink(skb, &session->reorder_q);
419 (*session->deref)(session);
423 if (L2TP_SKB_CB(skb)->has_seq) {
424 if (L2TP_SKB_CB(skb)->ns != session->nr) {
425 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
426 "%s: holding oos pkt %u len %d, "
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));
434 __skb_unlink(skb, &session->reorder_q);
436 /* Process the skb. We release the queue lock while we
437 * do so to let other contexts process the queue.
439 spin_unlock_bh(&session->reorder_q.lock);
440 l2tp_recv_dequeue_skb(session, skb);
445 spin_unlock_bh(&session->reorder_q.lock);
448 static inline int l2tp_verify_udp_checksum(struct sock *sk,
451 struct udphdr *uh = udp_hdr(skb);
452 u16 ulen = ntohs(uh->len);
455 if (sk->sk_no_check || skb_csum_unnecessary(skb))
458 #if IS_ENABLED(CONFIG_IPV6)
459 if (sk->sk_family == PF_INET6) {
461 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
464 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
465 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
466 &ipv6_hdr(skb)->daddr, ulen,
467 IPPROTO_UDP, skb->csum)) {
468 skb->ip_summed = CHECKSUM_UNNECESSARY;
471 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
472 &ipv6_hdr(skb)->daddr,
473 skb->len, IPPROTO_UDP,
478 struct inet_sock *inet;
482 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
483 ulen, IPPROTO_UDP, 0);
485 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
486 !csum_fold(csum_add(psum, skb->csum)))
491 return __skb_checksum_complete(skb);
494 /* Do receive processing of L2TP data frames. We handle both L2TPv2
495 * and L2TPv3 data frames here.
497 * L2TPv2 Data Message Header
500 * 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
501 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
502 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
503 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
504 * | Tunnel ID | Session ID |
505 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
506 * | Ns (opt) | Nr (opt) |
507 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
508 * | Offset Size (opt) | Offset pad... (opt)
509 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
511 * Data frames are marked by T=0. All other fields are the same as
512 * those in L2TP control frames.
514 * L2TPv3 Data Message Header
516 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
517 * | L2TP Session Header |
518 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
519 * | L2-Specific Sublayer |
520 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
521 * | Tunnel Payload ...
522 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
524 * L2TPv3 Session Header Over IP
527 * 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
528 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
530 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
531 * | Cookie (optional, maximum 64 bits)...
532 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 * L2TPv3 L2-Specific Sublayer Format
539 * 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
540 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
541 * |x|S|x|x|x|x|x|x| Sequence Number |
542 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
544 * Cookie value, sublayer format and offset (pad) are negotiated with
545 * the peer when the session is set up. Unlike L2TPv2, we do not need
546 * to parse the packet header to determine if optional fields are
549 * Caller must already have parsed the frame and determined that it is
550 * a data (not control) frame before coming here. Fields up to the
551 * session-id have already been parsed and ptr points to the data
552 * after the session-id.
554 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
555 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
556 int length, int (*payload_hook)(struct sk_buff *skb))
558 struct l2tp_tunnel *tunnel = session->tunnel;
562 /* The ref count is increased since we now hold a pointer to
563 * the session. Take care to decrement the refcnt when exiting
564 * this function from now on...
566 l2tp_session_inc_refcount(session);
568 (*session->ref)(session);
570 /* Parse and check optional cookie */
571 if (session->peer_cookie_len > 0) {
572 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
573 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
574 "%s: cookie mismatch (%u/%u). Discarding.\n",
575 tunnel->name, tunnel->tunnel_id, session->session_id);
576 session->stats.rx_cookie_discards++;
579 ptr += session->peer_cookie_len;
582 /* Handle the optional sequence numbers. Sequence numbers are
583 * in different places for L2TPv2 and L2TPv3.
585 * If we are the LAC, enable/disable sequence numbers under
586 * the control of the LNS. If no sequence numbers present but
587 * we were expecting them, discard frame.
590 L2TP_SKB_CB(skb)->has_seq = 0;
591 if (tunnel->version == L2TP_HDR_VER_2) {
592 if (hdrflags & L2TP_HDRFLAG_S) {
593 ns = ntohs(*(__be16 *) ptr);
595 nr = ntohs(*(__be16 *) ptr);
598 /* Store L2TP info in the skb */
599 L2TP_SKB_CB(skb)->ns = ns;
600 L2TP_SKB_CB(skb)->has_seq = 1;
602 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
603 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
604 session->name, ns, nr, session->nr);
606 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
607 u32 l2h = ntohl(*(__be32 *) ptr);
609 if (l2h & 0x40000000) {
610 ns = l2h & 0x00ffffff;
612 /* Store L2TP info in the skb */
613 L2TP_SKB_CB(skb)->ns = ns;
614 L2TP_SKB_CB(skb)->has_seq = 1;
616 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
617 "%s: recv data ns=%u, session nr=%u\n",
618 session->name, ns, session->nr);
622 /* Advance past L2-specific header, if present */
623 ptr += session->l2specific_len;
625 if (L2TP_SKB_CB(skb)->has_seq) {
626 /* Received a packet with sequence numbers. If we're the LNS,
627 * check if we sre sending sequence numbers and if not,
630 if ((!session->lns_mode) && (!session->send_seq)) {
631 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
632 "%s: requested to enable seq numbers by LNS\n",
634 session->send_seq = -1;
635 l2tp_session_set_header_len(session, tunnel->version);
638 /* No sequence numbers.
639 * If user has configured mandatory sequence numbers, discard.
641 if (session->recv_seq) {
642 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
643 "%s: recv data has no seq numbers when required. "
644 "Discarding\n", session->name);
645 session->stats.rx_seq_discards++;
649 /* If we're the LAC and we're sending sequence numbers, the
650 * LNS has requested that we no longer send sequence numbers.
651 * If we're the LNS and we're sending sequence numbers, the
652 * LAC is broken. Discard the frame.
654 if ((!session->lns_mode) && (session->send_seq)) {
655 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
656 "%s: requested to disable seq numbers by LNS\n",
658 session->send_seq = 0;
659 l2tp_session_set_header_len(session, tunnel->version);
660 } else if (session->send_seq) {
661 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
662 "%s: recv data has no seq numbers when required. "
663 "Discarding\n", session->name);
664 session->stats.rx_seq_discards++;
669 /* Session data offset is handled differently for L2TPv2 and
670 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
671 * the header. For L2TPv3, the offset is negotiated using AVPs
672 * in the session setup control protocol.
674 if (tunnel->version == L2TP_HDR_VER_2) {
675 /* If offset bit set, skip it. */
676 if (hdrflags & L2TP_HDRFLAG_O) {
677 offset = ntohs(*(__be16 *)ptr);
681 ptr += session->offset;
684 if (!pskb_may_pull(skb, offset))
687 __skb_pull(skb, offset);
689 /* If caller wants to process the payload before we queue the
693 if ((*payload_hook)(skb))
696 /* Prepare skb for adding to the session's reorder_q. Hold
697 * packets for max reorder_timeout or 1 second if not
700 L2TP_SKB_CB(skb)->length = length;
701 L2TP_SKB_CB(skb)->expires = jiffies +
702 (session->reorder_timeout ? session->reorder_timeout : HZ);
704 /* Add packet to the session's receive queue. Reordering is done here, if
705 * enabled. Saved L2TP protocol info is stored in skb->sb[].
707 if (L2TP_SKB_CB(skb)->has_seq) {
708 if (session->reorder_timeout != 0) {
709 /* Packet reordering enabled. Add skb to session's
710 * reorder queue, in order of ns.
712 l2tp_recv_queue_skb(session, skb);
714 /* Packet reordering disabled. Discard out-of-sequence
717 if (L2TP_SKB_CB(skb)->ns != session->nr) {
718 session->stats.rx_seq_discards++;
719 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
720 "%s: oos pkt %u len %d discarded, "
721 "waiting for %u, reorder_q_len=%d\n",
722 session->name, L2TP_SKB_CB(skb)->ns,
723 L2TP_SKB_CB(skb)->length, session->nr,
724 skb_queue_len(&session->reorder_q));
727 skb_queue_tail(&session->reorder_q, skb);
730 /* No sequence numbers. Add the skb to the tail of the
731 * reorder queue. This ensures that it will be
732 * delivered after all previous sequenced skbs.
734 skb_queue_tail(&session->reorder_q, skb);
737 /* Try to dequeue as many skbs from reorder_q as we can. */
738 l2tp_recv_dequeue(session);
740 l2tp_session_dec_refcount(session);
745 session->stats.rx_errors++;
749 (*session->deref)(session);
751 l2tp_session_dec_refcount(session);
753 EXPORT_SYMBOL(l2tp_recv_common);
755 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
756 * here. The skb is not on a list when we get here.
757 * Returns 0 if the packet was a data packet and was successfully passed on.
758 * Returns 1 if the packet was not a good data packet and could not be
759 * forwarded. All such packets are passed up to userspace to deal with.
761 static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
762 int (*payload_hook)(struct sk_buff *skb))
764 struct l2tp_session *session = NULL;
765 unsigned char *ptr, *optr;
767 u32 tunnel_id, session_id;
772 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
773 goto discard_bad_csum;
775 /* UDP always verifies the packet length. */
776 __skb_pull(skb, sizeof(struct udphdr));
779 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
780 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
781 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
785 /* Trace packet contents, if enabled */
786 if (tunnel->debug & L2TP_MSG_DATA) {
787 length = min(32u, skb->len);
788 if (!pskb_may_pull(skb, length))
791 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
795 printk(" %02X", skb->data[offset]);
796 } while (++offset < length);
801 /* Point to L2TP header */
802 optr = ptr = skb->data;
804 /* Get L2TP header flags */
805 hdrflags = ntohs(*(__be16 *) ptr);
807 /* Check protocol version */
808 version = hdrflags & L2TP_HDR_VER_MASK;
809 if (version != tunnel->version) {
810 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
811 "%s: recv protocol version mismatch: got %d expected %d\n",
812 tunnel->name, version, tunnel->version);
816 /* Get length of L2TP packet */
819 /* If type is control packet, it is handled by userspace. */
820 if (hdrflags & L2TP_HDRFLAG_T) {
821 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
822 "%s: recv control packet, len=%d\n", tunnel->name, length);
829 if (tunnel->version == L2TP_HDR_VER_2) {
830 /* If length is present, skip it */
831 if (hdrflags & L2TP_HDRFLAG_L)
834 /* Extract tunnel and session ID */
835 tunnel_id = ntohs(*(__be16 *) ptr);
837 session_id = ntohs(*(__be16 *) ptr);
840 ptr += 2; /* skip reserved bits */
841 tunnel_id = tunnel->tunnel_id;
842 session_id = ntohl(*(__be32 *) ptr);
846 /* Find the session context */
847 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
848 if (!session || !session->recv_skb) {
849 /* Not found? Pass to userspace to deal with */
850 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
851 "%s: no session found (%u/%u). Passing up.\n",
852 tunnel->name, tunnel_id, session_id);
856 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
861 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
862 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
863 tunnel->stats.rx_errors++;
869 /* Put UDP header back */
870 __skb_push(skb, sizeof(struct udphdr));
875 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
879 * >0: skb should be passed up to userspace as UDP.
881 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
883 struct l2tp_tunnel *tunnel;
885 tunnel = l2tp_sock_to_tunnel(sk);
889 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
890 "%s: received %d bytes\n", tunnel->name, skb->len);
892 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
903 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
905 /************************************************************************
907 ***********************************************************************/
909 /* Build an L2TP header for the session into the buffer provided.
911 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
913 struct l2tp_tunnel *tunnel = session->tunnel;
916 u16 flags = L2TP_HDR_VER_2;
917 u32 tunnel_id = tunnel->peer_tunnel_id;
918 u32 session_id = session->peer_session_id;
920 if (session->send_seq)
921 flags |= L2TP_HDRFLAG_S;
923 /* Setup L2TP header. */
924 *bufp++ = htons(flags);
925 *bufp++ = htons(tunnel_id);
926 *bufp++ = htons(session_id);
927 if (session->send_seq) {
928 *bufp++ = htons(session->ns);
931 session->ns &= 0xffff;
932 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
933 "%s: updated ns to %u\n", session->name, session->ns);
939 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
941 struct l2tp_tunnel *tunnel = session->tunnel;
945 /* Setup L2TP header. The header differs slightly for UDP and
946 * IP encapsulations. For UDP, there is 4 bytes of flags.
948 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
949 u16 flags = L2TP_HDR_VER_3;
950 *((__be16 *) bufp) = htons(flags);
952 *((__be16 *) bufp) = 0;
956 *((__be32 *) bufp) = htonl(session->peer_session_id);
958 if (session->cookie_len) {
959 memcpy(bufp, &session->cookie[0], session->cookie_len);
960 bufp += session->cookie_len;
962 if (session->l2specific_len) {
963 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
965 if (session->send_seq) {
966 l2h = 0x40000000 | session->ns;
968 session->ns &= 0xffffff;
969 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
970 "%s: updated ns to %u\n", session->name, session->ns);
973 *((__be32 *) bufp) = htonl(l2h);
975 bufp += session->l2specific_len;
978 bufp += session->offset;
983 static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
984 struct flowi *fl, size_t data_len)
986 struct l2tp_tunnel *tunnel = session->tunnel;
987 unsigned int len = skb->len;
991 if (session->send_seq)
992 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
993 "%s: send %Zd bytes, ns=%u\n", session->name,
994 data_len, session->ns - 1);
996 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
997 "%s: send %Zd bytes\n", session->name, data_len);
999 if (session->debug & L2TP_MSG_DATA) {
1001 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1002 unsigned char *datap = skb->data + uhlen;
1004 printk(KERN_DEBUG "%s: xmit:", session->name);
1005 for (i = 0; i < (len - uhlen); i++) {
1006 printk(" %02X", *datap++);
1015 /* Queue the packet to IP for output */
1017 #if IS_ENABLED(CONFIG_IPV6)
1018 if (skb->sk->sk_family == PF_INET6)
1019 error = inet6_csk_xmit(skb, NULL);
1022 error = ip_queue_xmit(skb, fl);
1026 tunnel->stats.tx_packets++;
1027 tunnel->stats.tx_bytes += len;
1028 session->stats.tx_packets++;
1029 session->stats.tx_bytes += len;
1031 tunnel->stats.tx_errors++;
1032 session->stats.tx_errors++;
1038 /* Automatically called when the skb is freed.
1040 static void l2tp_sock_wfree(struct sk_buff *skb)
1045 /* For data skbs that we transmit, we associate with the tunnel socket
1046 * but don't do accounting.
1048 static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1052 skb->destructor = l2tp_sock_wfree;
1055 #if IS_ENABLED(CONFIG_IPV6)
1056 static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1059 struct ipv6_pinfo *np = inet6_sk(sk);
1060 struct udphdr *uh = udp_hdr(skb);
1062 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1063 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1064 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1065 skb->ip_summed = CHECKSUM_UNNECESSARY;
1066 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1069 uh->check = CSUM_MANGLED_0;
1071 skb->ip_summed = CHECKSUM_PARTIAL;
1072 skb->csum_start = skb_transport_header(skb) - skb->head;
1073 skb->csum_offset = offsetof(struct udphdr, check);
1074 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1075 udp_len, IPPROTO_UDP, 0);
1080 /* If caller requires the skb to have a ppp header, the header must be
1081 * inserted in the skb data before calling this function.
1083 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
1085 int data_len = skb->len;
1086 struct l2tp_tunnel *tunnel = session->tunnel;
1087 struct sock *sk = tunnel->sock;
1090 struct inet_sock *inet;
1095 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1098 /* Check that there's enough headroom in the skb to insert IP,
1099 * UDP and L2TP headers. If not enough, expand it to
1100 * make room. Adjust truesize.
1102 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1104 old_headroom = skb_headroom(skb);
1105 if (skb_cow_head(skb, headroom)) {
1110 new_headroom = skb_headroom(skb);
1112 skb->truesize += new_headroom - old_headroom;
1114 /* Setup L2TP header */
1115 session->build_header(session, __skb_push(skb, hdr_len));
1117 /* Reset skb netfilter state */
1118 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1119 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1124 if (sock_owned_by_user(sk)) {
1129 /* Get routing info from the tunnel socket */
1131 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
1134 fl = &inet->cork.fl;
1135 switch (tunnel->encap) {
1136 case L2TP_ENCAPTYPE_UDP:
1137 /* Setup UDP header */
1138 __skb_push(skb, sizeof(*uh));
1139 skb_reset_transport_header(skb);
1141 uh->source = inet->inet_sport;
1142 uh->dest = inet->inet_dport;
1143 udp_len = uhlen + hdr_len + data_len;
1144 uh->len = htons(udp_len);
1147 /* Calculate UDP checksum if configured to do so */
1148 #if IS_ENABLED(CONFIG_IPV6)
1149 if (sk->sk_family == PF_INET6)
1150 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1153 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1154 skb->ip_summed = CHECKSUM_NONE;
1155 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1156 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1157 skb->ip_summed = CHECKSUM_COMPLETE;
1158 csum = skb_checksum(skb, 0, udp_len, 0);
1159 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1161 udp_len, IPPROTO_UDP, csum);
1163 uh->check = CSUM_MANGLED_0;
1165 skb->ip_summed = CHECKSUM_PARTIAL;
1166 skb->csum_start = skb_transport_header(skb) - skb->head;
1167 skb->csum_offset = offsetof(struct udphdr, check);
1168 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1170 udp_len, IPPROTO_UDP, 0);
1174 case L2TP_ENCAPTYPE_IP:
1178 l2tp_skb_set_owner_w(skb, sk);
1180 l2tp_xmit_core(session, skb, fl, data_len);
1187 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1189 /*****************************************************************************
1190 * Tinnel and session create/destroy.
1191 *****************************************************************************/
1193 /* Tunnel socket destruct hook.
1194 * The tunnel context is deleted only when all session sockets have been
1197 static void l2tp_tunnel_destruct(struct sock *sk)
1199 struct l2tp_tunnel *tunnel;
1201 tunnel = sk->sk_user_data;
1205 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1206 "%s: closing...\n", tunnel->name);
1208 /* Close all sessions */
1209 l2tp_tunnel_closeall(tunnel);
1211 switch (tunnel->encap) {
1212 case L2TP_ENCAPTYPE_UDP:
1213 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1214 (udp_sk(sk))->encap_type = 0;
1215 (udp_sk(sk))->encap_rcv = NULL;
1217 case L2TP_ENCAPTYPE_IP:
1221 /* Remove hooks into tunnel socket */
1222 tunnel->sock = NULL;
1223 sk->sk_destruct = tunnel->old_sk_destruct;
1224 sk->sk_user_data = NULL;
1226 /* Call the original destructor */
1227 if (sk->sk_destruct)
1228 (*sk->sk_destruct)(sk);
1230 /* We're finished with the socket */
1231 l2tp_tunnel_dec_refcount(tunnel);
1237 /* When the tunnel is closed, all the attached sessions need to go too.
1239 static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1242 struct hlist_node *walk;
1243 struct hlist_node *tmp;
1244 struct l2tp_session *session;
1246 BUG_ON(tunnel == NULL);
1248 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1249 "%s: closing all sessions...\n", tunnel->name);
1251 write_lock_bh(&tunnel->hlist_lock);
1252 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1254 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1255 session = hlist_entry(walk, struct l2tp_session, hlist);
1257 PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1258 "%s: closing session\n", session->name);
1260 hlist_del_init(&session->hlist);
1262 /* Since we should hold the sock lock while
1263 * doing any unbinding, we need to release the
1264 * lock we're holding before taking that lock.
1265 * Hold a reference to the sock so it doesn't
1266 * disappear as we're jumping between locks.
1268 if (session->ref != NULL)
1269 (*session->ref)(session);
1271 write_unlock_bh(&tunnel->hlist_lock);
1273 if (tunnel->version != L2TP_HDR_VER_2) {
1274 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1276 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1277 hlist_del_init_rcu(&session->global_hlist);
1278 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1282 if (session->session_close != NULL)
1283 (*session->session_close)(session);
1285 if (session->deref != NULL)
1286 (*session->deref)(session);
1288 write_lock_bh(&tunnel->hlist_lock);
1290 /* Now restart from the beginning of this hash
1291 * chain. We always remove a session from the
1292 * list so we are guaranteed to make forward
1298 write_unlock_bh(&tunnel->hlist_lock);
1301 /* Really kill the tunnel.
1302 * Come here only when all sessions have been cleared from the tunnel.
1304 static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1306 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1308 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1309 BUG_ON(tunnel->sock != NULL);
1311 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1312 "%s: free...\n", tunnel->name);
1314 /* Remove from tunnel list */
1315 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1316 list_del_rcu(&tunnel->list);
1317 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1320 atomic_dec(&l2tp_tunnel_count);
1324 /* Create a socket for the tunnel, if one isn't set up by
1325 * userspace. This is used for static tunnels where there is no
1326 * managing L2TP daemon.
1328 static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct socket **sockp)
1331 struct sockaddr_in udp_addr;
1332 struct sockaddr_l2tpip ip_addr;
1333 struct socket *sock = NULL;
1335 switch (cfg->encap) {
1336 case L2TP_ENCAPTYPE_UDP:
1337 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1343 memset(&udp_addr, 0, sizeof(udp_addr));
1344 udp_addr.sin_family = AF_INET;
1345 udp_addr.sin_addr = cfg->local_ip;
1346 udp_addr.sin_port = htons(cfg->local_udp_port);
1347 err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
1351 udp_addr.sin_family = AF_INET;
1352 udp_addr.sin_addr = cfg->peer_ip;
1353 udp_addr.sin_port = htons(cfg->peer_udp_port);
1354 err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0);
1358 if (!cfg->use_udp_checksums)
1359 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
1363 case L2TP_ENCAPTYPE_IP:
1364 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
1370 memset(&ip_addr, 0, sizeof(ip_addr));
1371 ip_addr.l2tp_family = AF_INET;
1372 ip_addr.l2tp_addr = cfg->local_ip;
1373 ip_addr.l2tp_conn_id = tunnel_id;
1374 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
1378 ip_addr.l2tp_family = AF_INET;
1379 ip_addr.l2tp_addr = cfg->peer_ip;
1380 ip_addr.l2tp_conn_id = peer_tunnel_id;
1381 err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
1392 if ((err < 0) && sock) {
1400 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)
1402 struct l2tp_tunnel *tunnel = NULL;
1404 struct socket *sock = NULL;
1405 struct sock *sk = NULL;
1406 struct l2tp_net *pn;
1407 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1409 /* Get the tunnel socket from the fd, which was opened by
1410 * the userspace L2TP daemon. If not specified, create a
1414 err = l2tp_tunnel_sock_create(tunnel_id, peer_tunnel_id, cfg, &sock);
1419 sock = sockfd_lookup(fd, &err);
1421 printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1422 tunnel_id, fd, err);
1432 /* Quick sanity checks */
1434 case L2TP_ENCAPTYPE_UDP:
1435 err = -EPROTONOSUPPORT;
1436 if (sk->sk_protocol != IPPROTO_UDP) {
1437 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1438 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1442 case L2TP_ENCAPTYPE_IP:
1443 err = -EPROTONOSUPPORT;
1444 if (sk->sk_protocol != IPPROTO_L2TP) {
1445 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1446 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1452 /* Check if this socket has already been prepped */
1453 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1454 if (tunnel != NULL) {
1455 /* This socket has already been prepped */
1460 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1461 if (tunnel == NULL) {
1466 tunnel->version = version;
1467 tunnel->tunnel_id = tunnel_id;
1468 tunnel->peer_tunnel_id = peer_tunnel_id;
1469 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1471 tunnel->magic = L2TP_TUNNEL_MAGIC;
1472 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1473 rwlock_init(&tunnel->hlist_lock);
1475 /* The net we belong to */
1476 tunnel->l2tp_net = net;
1477 pn = l2tp_pernet(net);
1480 tunnel->debug = cfg->debug;
1482 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1483 tunnel->encap = encap;
1484 if (encap == L2TP_ENCAPTYPE_UDP) {
1485 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1486 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1487 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1488 #if IS_ENABLED(CONFIG_IPV6)
1489 if (sk->sk_family == PF_INET6)
1490 udpv6_encap_enable();
1496 sk->sk_user_data = tunnel;
1498 /* Hook on the tunnel socket destructor so that we can cleanup
1499 * if the tunnel socket goes away.
1501 tunnel->old_sk_destruct = sk->sk_destruct;
1502 sk->sk_destruct = &l2tp_tunnel_destruct;
1504 sk->sk_allocation = GFP_ATOMIC;
1506 /* Add tunnel to our list */
1507 INIT_LIST_HEAD(&tunnel->list);
1508 atomic_inc(&l2tp_tunnel_count);
1510 /* Bump the reference count. The tunnel context is deleted
1511 * only when this drops to zero. Must be done before list insertion
1513 l2tp_tunnel_inc_refcount(tunnel);
1514 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1515 list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
1516 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1523 /* If tunnel's socket was created by the kernel, it doesn't
1526 if (sock && sock->file)
1531 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1533 /* This function is used by the netlink TUNNEL_DELETE command.
1535 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1538 struct socket *sock = tunnel->sock ? tunnel->sock->sk_socket : NULL;
1540 /* Force the tunnel socket to close. This will eventually
1541 * cause the tunnel to be deleted via the normal socket close
1542 * mechanisms when userspace closes the tunnel socket.
1545 err = inet_shutdown(sock, 2);
1547 /* If the tunnel's socket was created by the kernel,
1548 * close the socket here since the socket was not
1549 * created by userspace.
1551 if (sock->file == NULL)
1552 err = inet_release(sock);
1557 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1559 /* Really kill the session.
1561 void l2tp_session_free(struct l2tp_session *session)
1563 struct l2tp_tunnel *tunnel;
1565 BUG_ON(atomic_read(&session->ref_count) != 0);
1567 tunnel = session->tunnel;
1568 if (tunnel != NULL) {
1569 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1571 /* Delete the session from the hash */
1572 write_lock_bh(&tunnel->hlist_lock);
1573 hlist_del_init(&session->hlist);
1574 write_unlock_bh(&tunnel->hlist_lock);
1576 /* Unlink from the global hash if not L2TPv2 */
1577 if (tunnel->version != L2TP_HDR_VER_2) {
1578 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1580 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1581 hlist_del_init_rcu(&session->global_hlist);
1582 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1586 if (session->session_id != 0)
1587 atomic_dec(&l2tp_session_count);
1589 sock_put(tunnel->sock);
1591 /* This will delete the tunnel context if this
1592 * is the last session on the tunnel.
1594 session->tunnel = NULL;
1595 l2tp_tunnel_dec_refcount(tunnel);
1602 EXPORT_SYMBOL_GPL(l2tp_session_free);
1604 /* This function is used by the netlink SESSION_DELETE command and by
1607 int l2tp_session_delete(struct l2tp_session *session)
1609 if (session->session_close != NULL)
1610 (*session->session_close)(session);
1612 l2tp_session_dec_refcount(session);
1616 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1619 /* We come here whenever a session's send_seq, cookie_len or
1620 * l2specific_len parameters are set.
1622 static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1624 if (version == L2TP_HDR_VER_2) {
1625 session->hdr_len = 6;
1626 if (session->send_seq)
1627 session->hdr_len += 4;
1629 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1630 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1631 session->hdr_len += 4;
1636 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)
1638 struct l2tp_session *session;
1640 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1641 if (session != NULL) {
1642 session->magic = L2TP_SESSION_MAGIC;
1643 session->tunnel = tunnel;
1645 session->session_id = session_id;
1646 session->peer_session_id = peer_session_id;
1649 sprintf(&session->name[0], "sess %u/%u",
1650 tunnel->tunnel_id, session->session_id);
1652 skb_queue_head_init(&session->reorder_q);
1654 INIT_HLIST_NODE(&session->hlist);
1655 INIT_HLIST_NODE(&session->global_hlist);
1657 /* Inherit debug options from tunnel */
1658 session->debug = tunnel->debug;
1661 session->pwtype = cfg->pw_type;
1662 session->debug = cfg->debug;
1663 session->mtu = cfg->mtu;
1664 session->mru = cfg->mru;
1665 session->send_seq = cfg->send_seq;
1666 session->recv_seq = cfg->recv_seq;
1667 session->lns_mode = cfg->lns_mode;
1668 session->reorder_timeout = cfg->reorder_timeout;
1669 session->offset = cfg->offset;
1670 session->l2specific_type = cfg->l2specific_type;
1671 session->l2specific_len = cfg->l2specific_len;
1672 session->cookie_len = cfg->cookie_len;
1673 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1674 session->peer_cookie_len = cfg->peer_cookie_len;
1675 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1678 if (tunnel->version == L2TP_HDR_VER_2)
1679 session->build_header = l2tp_build_l2tpv2_header;
1681 session->build_header = l2tp_build_l2tpv3_header;
1683 l2tp_session_set_header_len(session, tunnel->version);
1685 /* Bump the reference count. The session context is deleted
1686 * only when this drops to zero.
1688 l2tp_session_inc_refcount(session);
1689 l2tp_tunnel_inc_refcount(tunnel);
1691 /* Ensure tunnel socket isn't deleted */
1692 sock_hold(tunnel->sock);
1694 /* Add session to the tunnel's hash list */
1695 write_lock_bh(&tunnel->hlist_lock);
1696 hlist_add_head(&session->hlist,
1697 l2tp_session_id_hash(tunnel, session_id));
1698 write_unlock_bh(&tunnel->hlist_lock);
1700 /* And to the global session list if L2TPv3 */
1701 if (tunnel->version != L2TP_HDR_VER_2) {
1702 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1704 spin_lock_bh(&pn->l2tp_session_hlist_lock);
1705 hlist_add_head_rcu(&session->global_hlist,
1706 l2tp_session_id_hash_2(pn, session_id));
1707 spin_unlock_bh(&pn->l2tp_session_hlist_lock);
1710 /* Ignore management session in session count value */
1711 if (session->session_id != 0)
1712 atomic_inc(&l2tp_session_count);
1717 EXPORT_SYMBOL_GPL(l2tp_session_create);
1719 /*****************************************************************************
1721 *****************************************************************************/
1723 static __net_init int l2tp_init_net(struct net *net)
1725 struct l2tp_net *pn = net_generic(net, l2tp_net_id);
1728 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1729 spin_lock_init(&pn->l2tp_tunnel_list_lock);
1731 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1732 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1734 spin_lock_init(&pn->l2tp_session_hlist_lock);
1739 static struct pernet_operations l2tp_net_ops = {
1740 .init = l2tp_init_net,
1742 .size = sizeof(struct l2tp_net),
1745 static int __init l2tp_init(void)
1749 rc = register_pernet_device(&l2tp_net_ops);
1753 printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1759 static void __exit l2tp_exit(void)
1761 unregister_pernet_device(&l2tp_net_ops);
1764 module_init(l2tp_init);
1765 module_exit(l2tp_exit);
1767 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1768 MODULE_DESCRIPTION("L2TP core");
1769 MODULE_LICENSE("GPL");
1770 MODULE_VERSION(L2TP_DRV_VERSION);