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/uaccess.h>
26 #include <linux/kernel.h>
27 #include <linux/spinlock.h>
28 #include <linux/kthread.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/jiffies.h>
34 #include <linux/netdevice.h>
35 #include <linux/net.h>
36 #include <linux/inetdevice.h>
37 #include <linux/skbuff.h>
38 #include <linux/init.h>
41 #include <linux/udp.h>
42 #include <linux/l2tp.h>
43 #include <linux/hash.h>
44 #include <linux/sort.h>
45 #include <linux/file.h>
46 #include <linux/nsproxy.h>
47 #include <net/net_namespace.h>
48 #include <net/netns/generic.h>
52 #include <net/inet_common.h>
54 #include <net/protocol.h>
56 #include <asm/byteorder.h>
57 #include <asm/atomic.h>
59 #include "l2tp_core.h"
61 #define L2TP_DRV_VERSION "V2.0"
63 /* L2TP header constants */
64 #define L2TP_HDRFLAG_T 0x8000
65 #define L2TP_HDRFLAG_L 0x4000
66 #define L2TP_HDRFLAG_S 0x0800
67 #define L2TP_HDRFLAG_O 0x0200
68 #define L2TP_HDRFLAG_P 0x0100
70 #define L2TP_HDR_VER_MASK 0x000F
71 #define L2TP_HDR_VER_2 0x0002
72 #define L2TP_HDR_VER_3 0x0003
74 /* L2TPv3 default L2-specific sublayer */
75 #define L2TP_SLFLAG_S 0x40000000
76 #define L2TP_SL_SEQ_MASK 0x00ffffff
78 #define L2TP_HDR_SIZE_SEQ 10
79 #define L2TP_HDR_SIZE_NOSEQ 6
81 /* Default trace flags */
82 #define L2TP_DEFAULT_DEBUG_FLAGS 0
84 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
86 if ((_mask) & (_type)) \
87 printk(_lvl "L2TP: " _fmt, ##args); \
90 /* Private data stored for received packets in the skb.
96 unsigned long expires;
99 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
101 static atomic_t l2tp_tunnel_count;
102 static atomic_t l2tp_session_count;
104 /* per-net private data for this module */
105 static unsigned int l2tp_net_id;
107 struct list_head l2tp_tunnel_list;
108 rwlock_t l2tp_tunnel_list_lock;
109 struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
110 rwlock_t l2tp_session_hlist_lock;
113 static inline struct l2tp_net *l2tp_pernet(struct net *net)
117 return net_generic(net, l2tp_net_id);
120 /* Session hash global list for L2TPv3.
121 * The session_id SHOULD be random according to RFC3931, but several
122 * L2TP implementations use incrementing session_ids. So we do a real
123 * hash on the session_id, rather than a simple bitmask.
125 static inline struct hlist_head *
126 l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
128 return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
132 /* Lookup a session by id in the global session list
134 static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
136 struct l2tp_net *pn = l2tp_pernet(net);
137 struct hlist_head *session_list =
138 l2tp_session_id_hash_2(pn, session_id);
139 struct l2tp_session *session;
140 struct hlist_node *walk;
142 read_lock_bh(&pn->l2tp_session_hlist_lock);
143 hlist_for_each_entry(session, walk, session_list, global_hlist) {
144 if (session->session_id == session_id) {
145 read_unlock_bh(&pn->l2tp_session_hlist_lock);
149 read_unlock_bh(&pn->l2tp_session_hlist_lock);
154 /* Session hash list.
155 * The session_id SHOULD be random according to RFC2661, but several
156 * L2TP implementations (Cisco and Microsoft) use incrementing
157 * session_ids. So we do a real hash on the session_id, rather than a
160 static inline struct hlist_head *
161 l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
163 return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
166 /* Lookup a session by id
168 struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
170 struct hlist_head *session_list;
171 struct l2tp_session *session;
172 struct hlist_node *walk;
174 /* In L2TPv3, session_ids are unique over all tunnels and we
175 * sometimes need to look them up before we know the
179 return l2tp_session_find_2(net, session_id);
181 session_list = l2tp_session_id_hash(tunnel, session_id);
182 read_lock_bh(&tunnel->hlist_lock);
183 hlist_for_each_entry(session, walk, session_list, hlist) {
184 if (session->session_id == session_id) {
185 read_unlock_bh(&tunnel->hlist_lock);
189 read_unlock_bh(&tunnel->hlist_lock);
193 EXPORT_SYMBOL_GPL(l2tp_session_find);
195 struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
198 struct hlist_node *walk;
199 struct l2tp_session *session;
202 read_lock_bh(&tunnel->hlist_lock);
203 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
204 hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
206 read_unlock_bh(&tunnel->hlist_lock);
212 read_unlock_bh(&tunnel->hlist_lock);
216 EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
218 /* Lookup a session by interface name.
219 * This is very inefficient but is only used by management interfaces.
221 struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
223 struct l2tp_net *pn = l2tp_pernet(net);
225 struct hlist_node *walk;
226 struct l2tp_session *session;
228 read_lock_bh(&pn->l2tp_session_hlist_lock);
229 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
230 hlist_for_each_entry(session, walk, &pn->l2tp_session_hlist[hash], global_hlist) {
231 if (!strcmp(session->ifname, ifname)) {
232 read_unlock_bh(&pn->l2tp_session_hlist_lock);
238 read_unlock_bh(&pn->l2tp_session_hlist_lock);
242 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
244 /* Lookup a tunnel by id
246 struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
248 struct l2tp_tunnel *tunnel;
249 struct l2tp_net *pn = l2tp_pernet(net);
251 read_lock_bh(&pn->l2tp_tunnel_list_lock);
252 list_for_each_entry(tunnel, &pn->l2tp_tunnel_list, list) {
253 if (tunnel->tunnel_id == tunnel_id) {
254 read_unlock_bh(&pn->l2tp_tunnel_list_lock);
258 read_unlock_bh(&pn->l2tp_tunnel_list_lock);
262 EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
264 struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
266 struct l2tp_net *pn = l2tp_pernet(net);
267 struct l2tp_tunnel *tunnel;
270 read_lock_bh(&pn->l2tp_tunnel_list_lock);
271 list_for_each_entry(tunnel, &pn->l2tp_tunnel_list, list) {
273 read_unlock_bh(&pn->l2tp_tunnel_list_lock);
278 read_unlock_bh(&pn->l2tp_tunnel_list_lock);
282 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
284 /*****************************************************************************
285 * Receive data handling
286 *****************************************************************************/
288 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
291 static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
293 struct sk_buff *skbp;
295 u32 ns = L2TP_SKB_CB(skb)->ns;
297 spin_lock_bh(&session->reorder_q.lock);
298 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
299 if (L2TP_SKB_CB(skbp)->ns > ns) {
300 __skb_queue_before(&session->reorder_q, skbp, skb);
301 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
302 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
303 session->name, ns, L2TP_SKB_CB(skbp)->ns,
304 skb_queue_len(&session->reorder_q));
305 session->stats.rx_oos_packets++;
310 __skb_queue_tail(&session->reorder_q, skb);
313 spin_unlock_bh(&session->reorder_q.lock);
316 /* Dequeue a single skb.
318 static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
320 struct l2tp_tunnel *tunnel = session->tunnel;
321 int length = L2TP_SKB_CB(skb)->length;
323 /* We're about to requeue the skb, so return resources
324 * to its current owner (a socket receive buffer).
328 tunnel->stats.rx_packets++;
329 tunnel->stats.rx_bytes += length;
330 session->stats.rx_packets++;
331 session->stats.rx_bytes += length;
333 if (L2TP_SKB_CB(skb)->has_seq) {
336 if (tunnel->version == L2TP_HDR_VER_2)
337 session->nr &= 0xffff;
339 session->nr &= 0xffffff;
341 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
342 "%s: updated nr to %hu\n", session->name, session->nr);
345 /* call private receive handler */
346 if (session->recv_skb != NULL)
347 (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
352 (*session->deref)(session);
355 /* Dequeue skbs from the session's reorder_q, subject to packet order.
356 * Skbs that have been in the queue for too long are simply discarded.
358 static void l2tp_recv_dequeue(struct l2tp_session *session)
363 /* If the pkt at the head of the queue has the nr that we
364 * expect to send up next, dequeue it and any other
365 * in-sequence packets behind it.
367 spin_lock_bh(&session->reorder_q.lock);
368 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
369 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
370 session->stats.rx_seq_discards++;
371 session->stats.rx_errors++;
372 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
373 "%s: oos pkt %u len %d discarded (too old), "
374 "waiting for %u, reorder_q_len=%d\n",
375 session->name, L2TP_SKB_CB(skb)->ns,
376 L2TP_SKB_CB(skb)->length, session->nr,
377 skb_queue_len(&session->reorder_q));
378 __skb_unlink(skb, &session->reorder_q);
381 (*session->deref)(session);
385 if (L2TP_SKB_CB(skb)->has_seq) {
386 if (L2TP_SKB_CB(skb)->ns != session->nr) {
387 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
388 "%s: holding oos pkt %u len %d, "
389 "waiting for %u, reorder_q_len=%d\n",
390 session->name, L2TP_SKB_CB(skb)->ns,
391 L2TP_SKB_CB(skb)->length, session->nr,
392 skb_queue_len(&session->reorder_q));
396 __skb_unlink(skb, &session->reorder_q);
398 /* Process the skb. We release the queue lock while we
399 * do so to let other contexts process the queue.
401 spin_unlock_bh(&session->reorder_q.lock);
402 l2tp_recv_dequeue_skb(session, skb);
403 spin_lock_bh(&session->reorder_q.lock);
407 spin_unlock_bh(&session->reorder_q.lock);
410 static inline int l2tp_verify_udp_checksum(struct sock *sk,
413 struct udphdr *uh = udp_hdr(skb);
414 u16 ulen = ntohs(uh->len);
415 struct inet_sock *inet;
418 if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check)
422 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, ulen,
425 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
426 !csum_fold(csum_add(psum, skb->csum)))
431 return __skb_checksum_complete(skb);
434 /* Do receive processing of L2TP data frames. We handle both L2TPv2
435 * and L2TPv3 data frames here.
437 * L2TPv2 Data Message Header
440 * 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
441 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
442 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
443 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
444 * | Tunnel ID | Session ID |
445 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
446 * | Ns (opt) | Nr (opt) |
447 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
448 * | Offset Size (opt) | Offset pad... (opt)
449 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
451 * Data frames are marked by T=0. All other fields are the same as
452 * those in L2TP control frames.
454 * L2TPv3 Data Message Header
456 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
457 * | L2TP Session Header |
458 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
459 * | L2-Specific Sublayer |
460 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
461 * | Tunnel Payload ...
462 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
464 * L2TPv3 Session Header Over IP
467 * 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
468 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
470 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
471 * | Cookie (optional, maximum 64 bits)...
472 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
474 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
476 * L2TPv3 L2-Specific Sublayer Format
479 * 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
480 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
481 * |x|S|x|x|x|x|x|x| Sequence Number |
482 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484 * Cookie value, sublayer format and offset (pad) are negotiated with
485 * the peer when the session is set up. Unlike L2TPv2, we do not need
486 * to parse the packet header to determine if optional fields are
489 * Caller must already have parsed the frame and determined that it is
490 * a data (not control) frame before coming here. Fields up to the
491 * session-id have already been parsed and ptr points to the data
492 * after the session-id.
494 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
495 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
496 int length, int (*payload_hook)(struct sk_buff *skb))
498 struct l2tp_tunnel *tunnel = session->tunnel;
502 /* The ref count is increased since we now hold a pointer to
503 * the session. Take care to decrement the refcnt when exiting
504 * this function from now on...
506 l2tp_session_inc_refcount(session);
508 (*session->ref)(session);
510 /* Parse and check optional cookie */
511 if (session->peer_cookie_len > 0) {
512 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
513 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
514 "%s: cookie mismatch (%u/%u). Discarding.\n",
515 tunnel->name, tunnel->tunnel_id, session->session_id);
516 session->stats.rx_cookie_discards++;
519 ptr += session->peer_cookie_len;
522 /* Handle the optional sequence numbers. Sequence numbers are
523 * in different places for L2TPv2 and L2TPv3.
525 * If we are the LAC, enable/disable sequence numbers under
526 * the control of the LNS. If no sequence numbers present but
527 * we were expecting them, discard frame.
530 L2TP_SKB_CB(skb)->has_seq = 0;
531 if (tunnel->version == L2TP_HDR_VER_2) {
532 if (hdrflags & L2TP_HDRFLAG_S) {
533 ns = ntohs(*(__be16 *) ptr);
535 nr = ntohs(*(__be16 *) ptr);
538 /* Store L2TP info in the skb */
539 L2TP_SKB_CB(skb)->ns = ns;
540 L2TP_SKB_CB(skb)->has_seq = 1;
542 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
543 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
544 session->name, ns, nr, session->nr);
546 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
547 u32 l2h = ntohl(*(__be32 *) ptr);
549 if (l2h & 0x40000000) {
550 ns = l2h & 0x00ffffff;
552 /* Store L2TP info in the skb */
553 L2TP_SKB_CB(skb)->ns = ns;
554 L2TP_SKB_CB(skb)->has_seq = 1;
556 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
557 "%s: recv data ns=%u, session nr=%u\n",
558 session->name, ns, session->nr);
562 /* Advance past L2-specific header, if present */
563 ptr += session->l2specific_len;
565 if (L2TP_SKB_CB(skb)->has_seq) {
566 /* Received a packet with sequence numbers. If we're the LNS,
567 * check if we sre sending sequence numbers and if not,
570 if ((!session->lns_mode) && (!session->send_seq)) {
571 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
572 "%s: requested to enable seq numbers by LNS\n",
574 session->send_seq = -1;
575 l2tp_session_set_header_len(session, tunnel->version);
578 /* No sequence numbers.
579 * If user has configured mandatory sequence numbers, discard.
581 if (session->recv_seq) {
582 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
583 "%s: recv data has no seq numbers when required. "
584 "Discarding\n", session->name);
585 session->stats.rx_seq_discards++;
589 /* If we're the LAC and we're sending sequence numbers, the
590 * LNS has requested that we no longer send sequence numbers.
591 * If we're the LNS and we're sending sequence numbers, the
592 * LAC is broken. Discard the frame.
594 if ((!session->lns_mode) && (session->send_seq)) {
595 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
596 "%s: requested to disable seq numbers by LNS\n",
598 session->send_seq = 0;
599 l2tp_session_set_header_len(session, tunnel->version);
600 } else if (session->send_seq) {
601 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
602 "%s: recv data has no seq numbers when required. "
603 "Discarding\n", session->name);
604 session->stats.rx_seq_discards++;
609 /* Session data offset is handled differently for L2TPv2 and
610 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
611 * the header. For L2TPv3, the offset is negotiated using AVPs
612 * in the session setup control protocol.
614 if (tunnel->version == L2TP_HDR_VER_2) {
615 /* If offset bit set, skip it. */
616 if (hdrflags & L2TP_HDRFLAG_O) {
617 offset = ntohs(*(__be16 *)ptr);
621 ptr += session->offset;
624 if (!pskb_may_pull(skb, offset))
627 __skb_pull(skb, offset);
629 /* If caller wants to process the payload before we queue the
633 if ((*payload_hook)(skb))
636 /* Prepare skb for adding to the session's reorder_q. Hold
637 * packets for max reorder_timeout or 1 second if not
640 L2TP_SKB_CB(skb)->length = length;
641 L2TP_SKB_CB(skb)->expires = jiffies +
642 (session->reorder_timeout ? session->reorder_timeout : HZ);
644 /* Add packet to the session's receive queue. Reordering is done here, if
645 * enabled. Saved L2TP protocol info is stored in skb->sb[].
647 if (L2TP_SKB_CB(skb)->has_seq) {
648 if (session->reorder_timeout != 0) {
649 /* Packet reordering enabled. Add skb to session's
650 * reorder queue, in order of ns.
652 l2tp_recv_queue_skb(session, skb);
654 /* Packet reordering disabled. Discard out-of-sequence
657 if (L2TP_SKB_CB(skb)->ns != session->nr) {
658 session->stats.rx_seq_discards++;
659 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
660 "%s: oos pkt %u len %d discarded, "
661 "waiting for %u, reorder_q_len=%d\n",
662 session->name, L2TP_SKB_CB(skb)->ns,
663 L2TP_SKB_CB(skb)->length, session->nr,
664 skb_queue_len(&session->reorder_q));
667 skb_queue_tail(&session->reorder_q, skb);
670 /* No sequence numbers. Add the skb to the tail of the
671 * reorder queue. This ensures that it will be
672 * delivered after all previous sequenced skbs.
674 skb_queue_tail(&session->reorder_q, skb);
677 /* Try to dequeue as many skbs from reorder_q as we can. */
678 l2tp_recv_dequeue(session);
680 l2tp_session_dec_refcount(session);
685 session->stats.rx_errors++;
689 (*session->deref)(session);
691 l2tp_session_dec_refcount(session);
693 EXPORT_SYMBOL(l2tp_recv_common);
695 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
696 * here. The skb is not on a list when we get here.
697 * Returns 0 if the packet was a data packet and was successfully passed on.
698 * Returns 1 if the packet was not a good data packet and could not be
699 * forwarded. All such packets are passed up to userspace to deal with.
701 int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
702 int (*payload_hook)(struct sk_buff *skb))
704 struct l2tp_session *session = NULL;
705 unsigned char *ptr, *optr;
707 u32 tunnel_id, session_id;
712 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
713 goto discard_bad_csum;
715 /* UDP always verifies the packet length. */
716 __skb_pull(skb, sizeof(struct udphdr));
719 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
720 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
721 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
725 /* Point to L2TP header */
726 optr = ptr = skb->data;
728 /* Trace packet contents, if enabled */
729 if (tunnel->debug & L2TP_MSG_DATA) {
730 length = min(32u, skb->len);
731 if (!pskb_may_pull(skb, length))
734 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
738 printk(" %02X", ptr[offset]);
739 } while (++offset < length);
744 /* Get L2TP header flags */
745 hdrflags = ntohs(*(__be16 *) ptr);
747 /* Check protocol version */
748 version = hdrflags & L2TP_HDR_VER_MASK;
749 if (version != tunnel->version) {
750 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
751 "%s: recv protocol version mismatch: got %d expected %d\n",
752 tunnel->name, version, tunnel->version);
756 /* Get length of L2TP packet */
759 /* If type is control packet, it is handled by userspace. */
760 if (hdrflags & L2TP_HDRFLAG_T) {
761 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
762 "%s: recv control packet, len=%d\n", tunnel->name, length);
769 if (tunnel->version == L2TP_HDR_VER_2) {
770 /* If length is present, skip it */
771 if (hdrflags & L2TP_HDRFLAG_L)
774 /* Extract tunnel and session ID */
775 tunnel_id = ntohs(*(__be16 *) ptr);
777 session_id = ntohs(*(__be16 *) ptr);
780 ptr += 2; /* skip reserved bits */
781 tunnel_id = tunnel->tunnel_id;
782 session_id = ntohl(*(__be32 *) ptr);
786 /* Find the session context */
787 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
788 if (!session || !session->recv_skb) {
789 /* Not found? Pass to userspace to deal with */
790 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
791 "%s: no session found (%u/%u). Passing up.\n",
792 tunnel->name, tunnel_id, session_id);
796 l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
801 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
802 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
803 tunnel->stats.rx_errors++;
809 /* Put UDP header back */
810 __skb_push(skb, sizeof(struct udphdr));
814 EXPORT_SYMBOL_GPL(l2tp_udp_recv_core);
816 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
820 * >0: skb should be passed up to userspace as UDP.
822 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
824 struct l2tp_tunnel *tunnel;
826 tunnel = l2tp_sock_to_tunnel(sk);
830 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
831 "%s: received %d bytes\n", tunnel->name, skb->len);
833 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
844 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
846 /************************************************************************
848 ***********************************************************************/
850 /* Build an L2TP header for the session into the buffer provided.
852 static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
854 struct l2tp_tunnel *tunnel = session->tunnel;
857 u16 flags = L2TP_HDR_VER_2;
858 u32 tunnel_id = tunnel->peer_tunnel_id;
859 u32 session_id = session->peer_session_id;
861 if (session->send_seq)
862 flags |= L2TP_HDRFLAG_S;
864 /* Setup L2TP header. */
865 *bufp++ = htons(flags);
866 *bufp++ = htons(tunnel_id);
867 *bufp++ = htons(session_id);
868 if (session->send_seq) {
869 *bufp++ = htons(session->ns);
872 session->ns &= 0xffff;
873 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
874 "%s: updated ns to %u\n", session->name, session->ns);
880 static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
882 struct l2tp_tunnel *tunnel = session->tunnel;
886 /* Setup L2TP header. The header differs slightly for UDP and
887 * IP encapsulations. For UDP, there is 4 bytes of flags.
889 if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
890 u16 flags = L2TP_HDR_VER_3;
891 *((__be16 *) bufp) = htons(flags);
893 *((__be16 *) bufp) = 0;
897 *((__be32 *) bufp) = htonl(session->peer_session_id);
899 if (session->cookie_len) {
900 memcpy(bufp, &session->cookie[0], session->cookie_len);
901 bufp += session->cookie_len;
903 if (session->l2specific_len) {
904 if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
906 if (session->send_seq) {
907 l2h = 0x40000000 | session->ns;
909 session->ns &= 0xffffff;
910 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
911 "%s: updated ns to %u\n", session->name, session->ns);
914 *((__be32 *) bufp) = htonl(l2h);
916 bufp += session->l2specific_len;
919 bufp += session->offset;
924 int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, size_t data_len)
926 struct l2tp_tunnel *tunnel = session->tunnel;
927 unsigned int len = skb->len;
931 if (session->send_seq)
932 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
933 "%s: send %Zd bytes, ns=%u\n", session->name,
934 data_len, session->ns - 1);
936 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
937 "%s: send %Zd bytes\n", session->name, data_len);
939 if (session->debug & L2TP_MSG_DATA) {
941 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
942 unsigned char *datap = skb->data + uhlen;
944 printk(KERN_DEBUG "%s: xmit:", session->name);
945 for (i = 0; i < (len - uhlen); i++) {
946 printk(" %02X", *datap++);
955 /* Queue the packet to IP for output */
956 error = ip_queue_xmit(skb, 1);
960 tunnel->stats.tx_packets++;
961 tunnel->stats.tx_bytes += len;
962 session->stats.tx_packets++;
963 session->stats.tx_bytes += len;
965 tunnel->stats.tx_errors++;
966 session->stats.tx_errors++;
971 EXPORT_SYMBOL_GPL(l2tp_xmit_core);
973 /* Automatically called when the skb is freed.
975 static void l2tp_sock_wfree(struct sk_buff *skb)
980 /* For data skbs that we transmit, we associate with the tunnel socket
981 * but don't do accounting.
983 static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
987 skb->destructor = l2tp_sock_wfree;
990 /* If caller requires the skb to have a ppp header, the header must be
991 * inserted in the skb data before calling this function.
993 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
995 int data_len = skb->len;
996 struct l2tp_tunnel *tunnel = session->tunnel;
997 struct sock *sk = tunnel->sock;
999 struct inet_sock *inet;
1004 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1007 /* Check that there's enough headroom in the skb to insert IP,
1008 * UDP and L2TP headers. If not enough, expand it to
1009 * make room. Adjust truesize.
1011 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
1013 old_headroom = skb_headroom(skb);
1014 if (skb_cow_head(skb, headroom))
1017 new_headroom = skb_headroom(skb);
1019 skb->truesize += new_headroom - old_headroom;
1021 /* Setup L2TP header */
1022 session->build_header(session, __skb_push(skb, hdr_len));
1024 /* Reset skb netfilter state */
1025 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1026 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1030 /* Get routing info from the tunnel socket */
1032 skb_dst_set(skb, dst_clone(__sk_dst_get(sk)));
1034 switch (tunnel->encap) {
1035 case L2TP_ENCAPTYPE_UDP:
1036 /* Setup UDP header */
1038 __skb_push(skb, sizeof(*uh));
1039 skb_reset_transport_header(skb);
1041 uh->source = inet->inet_sport;
1042 uh->dest = inet->inet_dport;
1043 udp_len = uhlen + hdr_len + data_len;
1044 uh->len = htons(udp_len);
1047 /* Calculate UDP checksum if configured to do so */
1048 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1049 skb->ip_summed = CHECKSUM_NONE;
1050 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
1051 (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
1052 skb->ip_summed = CHECKSUM_COMPLETE;
1053 csum = skb_checksum(skb, 0, udp_len, 0);
1054 uh->check = csum_tcpudp_magic(inet->inet_saddr,
1056 udp_len, IPPROTO_UDP, csum);
1058 uh->check = CSUM_MANGLED_0;
1060 skb->ip_summed = CHECKSUM_PARTIAL;
1061 skb->csum_start = skb_transport_header(skb) - skb->head;
1062 skb->csum_offset = offsetof(struct udphdr, check);
1063 uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
1065 udp_len, IPPROTO_UDP, 0);
1069 case L2TP_ENCAPTYPE_IP:
1073 l2tp_skb_set_owner_w(skb, sk);
1075 l2tp_xmit_core(session, skb, data_len);
1080 EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1082 /*****************************************************************************
1083 * Tinnel and session create/destroy.
1084 *****************************************************************************/
1086 /* Tunnel socket destruct hook.
1087 * The tunnel context is deleted only when all session sockets have been
1090 void l2tp_tunnel_destruct(struct sock *sk)
1092 struct l2tp_tunnel *tunnel;
1094 tunnel = sk->sk_user_data;
1098 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1099 "%s: closing...\n", tunnel->name);
1101 /* Close all sessions */
1102 l2tp_tunnel_closeall(tunnel);
1104 switch (tunnel->encap) {
1105 case L2TP_ENCAPTYPE_UDP:
1106 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1107 (udp_sk(sk))->encap_type = 0;
1108 (udp_sk(sk))->encap_rcv = NULL;
1110 case L2TP_ENCAPTYPE_IP:
1114 /* Remove hooks into tunnel socket */
1115 tunnel->sock = NULL;
1116 sk->sk_destruct = tunnel->old_sk_destruct;
1117 sk->sk_user_data = NULL;
1119 /* Call the original destructor */
1120 if (sk->sk_destruct)
1121 (*sk->sk_destruct)(sk);
1123 /* We're finished with the socket */
1124 l2tp_tunnel_dec_refcount(tunnel);
1129 EXPORT_SYMBOL(l2tp_tunnel_destruct);
1131 /* When the tunnel is closed, all the attached sessions need to go too.
1133 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1136 struct hlist_node *walk;
1137 struct hlist_node *tmp;
1138 struct l2tp_session *session;
1140 BUG_ON(tunnel == NULL);
1142 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1143 "%s: closing all sessions...\n", tunnel->name);
1145 write_lock_bh(&tunnel->hlist_lock);
1146 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
1148 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1149 session = hlist_entry(walk, struct l2tp_session, hlist);
1151 PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1152 "%s: closing session\n", session->name);
1154 hlist_del_init(&session->hlist);
1156 /* Since we should hold the sock lock while
1157 * doing any unbinding, we need to release the
1158 * lock we're holding before taking that lock.
1159 * Hold a reference to the sock so it doesn't
1160 * disappear as we're jumping between locks.
1162 if (session->ref != NULL)
1163 (*session->ref)(session);
1165 write_unlock_bh(&tunnel->hlist_lock);
1167 if (tunnel->version != L2TP_HDR_VER_2) {
1168 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1170 write_lock_bh(&pn->l2tp_session_hlist_lock);
1171 hlist_del_init(&session->global_hlist);
1172 write_unlock_bh(&pn->l2tp_session_hlist_lock);
1175 if (session->session_close != NULL)
1176 (*session->session_close)(session);
1178 if (session->deref != NULL)
1179 (*session->deref)(session);
1181 write_lock_bh(&tunnel->hlist_lock);
1183 /* Now restart from the beginning of this hash
1184 * chain. We always remove a session from the
1185 * list so we are guaranteed to make forward
1191 write_unlock_bh(&tunnel->hlist_lock);
1193 EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
1195 /* Really kill the tunnel.
1196 * Come here only when all sessions have been cleared from the tunnel.
1198 void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1200 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1202 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1203 BUG_ON(tunnel->sock != NULL);
1205 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1206 "%s: free...\n", tunnel->name);
1208 /* Remove from tunnel list */
1209 write_lock_bh(&pn->l2tp_tunnel_list_lock);
1210 list_del_init(&tunnel->list);
1211 write_unlock_bh(&pn->l2tp_tunnel_list_lock);
1213 atomic_dec(&l2tp_tunnel_count);
1216 EXPORT_SYMBOL_GPL(l2tp_tunnel_free);
1218 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)
1220 struct l2tp_tunnel *tunnel = NULL;
1222 struct socket *sock = NULL;
1223 struct sock *sk = NULL;
1224 struct l2tp_net *pn;
1225 enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
1227 /* Get the tunnel socket from the fd, which was opened by
1228 * the userspace L2TP daemon.
1231 sock = sockfd_lookup(fd, &err);
1233 printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1234 tunnel_id, fd, err);
1243 /* Quick sanity checks */
1245 case L2TP_ENCAPTYPE_UDP:
1246 err = -EPROTONOSUPPORT;
1247 if (sk->sk_protocol != IPPROTO_UDP) {
1248 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1249 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1253 case L2TP_ENCAPTYPE_IP:
1254 err = -EPROTONOSUPPORT;
1255 if (sk->sk_protocol != IPPROTO_L2TP) {
1256 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1257 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1263 /* Check if this socket has already been prepped */
1264 tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
1265 if (tunnel != NULL) {
1266 /* This socket has already been prepped */
1271 tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
1272 if (tunnel == NULL) {
1277 tunnel->version = version;
1278 tunnel->tunnel_id = tunnel_id;
1279 tunnel->peer_tunnel_id = peer_tunnel_id;
1280 tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
1282 tunnel->magic = L2TP_TUNNEL_MAGIC;
1283 sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
1284 rwlock_init(&tunnel->hlist_lock);
1286 /* The net we belong to */
1287 tunnel->l2tp_net = net;
1288 pn = l2tp_pernet(net);
1291 tunnel->debug = cfg->debug;
1293 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1294 tunnel->encap = encap;
1295 if (encap == L2TP_ENCAPTYPE_UDP) {
1296 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1297 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1298 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1301 sk->sk_user_data = tunnel;
1303 /* Hook on the tunnel socket destructor so that we can cleanup
1304 * if the tunnel socket goes away.
1306 tunnel->old_sk_destruct = sk->sk_destruct;
1307 sk->sk_destruct = &l2tp_tunnel_destruct;
1309 sk->sk_allocation = GFP_ATOMIC;
1311 /* Add tunnel to our list */
1312 INIT_LIST_HEAD(&tunnel->list);
1313 write_lock_bh(&pn->l2tp_tunnel_list_lock);
1314 list_add(&tunnel->list, &pn->l2tp_tunnel_list);
1315 write_unlock_bh(&pn->l2tp_tunnel_list_lock);
1316 atomic_inc(&l2tp_tunnel_count);
1318 /* Bump the reference count. The tunnel context is deleted
1319 * only when this drops to zero.
1321 l2tp_tunnel_inc_refcount(tunnel);
1333 EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
1335 /* This function is used by the netlink TUNNEL_DELETE command.
1337 int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
1341 /* Force the tunnel socket to close. This will eventually
1342 * cause the tunnel to be deleted via the normal socket close
1343 * mechanisms when userspace closes the tunnel socket.
1345 if ((tunnel->sock != NULL) && (tunnel->sock->sk_socket != NULL))
1346 err = inet_shutdown(tunnel->sock->sk_socket, 2);
1350 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
1352 /* Really kill the session.
1354 void l2tp_session_free(struct l2tp_session *session)
1356 struct l2tp_tunnel *tunnel;
1358 BUG_ON(atomic_read(&session->ref_count) != 0);
1360 tunnel = session->tunnel;
1361 if (tunnel != NULL) {
1362 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1364 /* Delete the session from the hash */
1365 write_lock_bh(&tunnel->hlist_lock);
1366 hlist_del_init(&session->hlist);
1367 write_unlock_bh(&tunnel->hlist_lock);
1369 /* Unlink from the global hash if not L2TPv2 */
1370 if (tunnel->version != L2TP_HDR_VER_2) {
1371 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1373 write_lock_bh(&pn->l2tp_session_hlist_lock);
1374 hlist_del_init(&session->global_hlist);
1375 write_unlock_bh(&pn->l2tp_session_hlist_lock);
1378 if (session->session_id != 0)
1379 atomic_dec(&l2tp_session_count);
1381 sock_put(tunnel->sock);
1383 /* This will delete the tunnel context if this
1384 * is the last session on the tunnel.
1386 session->tunnel = NULL;
1387 l2tp_tunnel_dec_refcount(tunnel);
1394 EXPORT_SYMBOL_GPL(l2tp_session_free);
1396 /* This function is used by the netlink SESSION_DELETE command and by
1399 int l2tp_session_delete(struct l2tp_session *session)
1401 if (session->session_close != NULL)
1402 (*session->session_close)(session);
1404 l2tp_session_dec_refcount(session);
1408 EXPORT_SYMBOL_GPL(l2tp_session_delete);
1411 /* We come here whenever a session's send_seq, cookie_len or
1412 * l2specific_len parameters are set.
1414 void l2tp_session_set_header_len(struct l2tp_session *session, int version)
1416 if (version == L2TP_HDR_VER_2) {
1417 session->hdr_len = 6;
1418 if (session->send_seq)
1419 session->hdr_len += 4;
1421 session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
1422 if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
1423 session->hdr_len += 4;
1427 EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
1429 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)
1431 struct l2tp_session *session;
1433 session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
1434 if (session != NULL) {
1435 session->magic = L2TP_SESSION_MAGIC;
1436 session->tunnel = tunnel;
1438 session->session_id = session_id;
1439 session->peer_session_id = peer_session_id;
1442 sprintf(&session->name[0], "sess %u/%u",
1443 tunnel->tunnel_id, session->session_id);
1445 skb_queue_head_init(&session->reorder_q);
1447 INIT_HLIST_NODE(&session->hlist);
1448 INIT_HLIST_NODE(&session->global_hlist);
1450 /* Inherit debug options from tunnel */
1451 session->debug = tunnel->debug;
1454 session->pwtype = cfg->pw_type;
1455 session->debug = cfg->debug;
1456 session->mtu = cfg->mtu;
1457 session->mru = cfg->mru;
1458 session->send_seq = cfg->send_seq;
1459 session->recv_seq = cfg->recv_seq;
1460 session->lns_mode = cfg->lns_mode;
1461 session->reorder_timeout = cfg->reorder_timeout;
1462 session->offset = cfg->offset;
1463 session->l2specific_type = cfg->l2specific_type;
1464 session->l2specific_len = cfg->l2specific_len;
1465 session->cookie_len = cfg->cookie_len;
1466 memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
1467 session->peer_cookie_len = cfg->peer_cookie_len;
1468 memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
1471 if (tunnel->version == L2TP_HDR_VER_2)
1472 session->build_header = l2tp_build_l2tpv2_header;
1474 session->build_header = l2tp_build_l2tpv3_header;
1476 l2tp_session_set_header_len(session, tunnel->version);
1478 /* Bump the reference count. The session context is deleted
1479 * only when this drops to zero.
1481 l2tp_session_inc_refcount(session);
1482 l2tp_tunnel_inc_refcount(tunnel);
1484 /* Ensure tunnel socket isn't deleted */
1485 sock_hold(tunnel->sock);
1487 /* Add session to the tunnel's hash list */
1488 write_lock_bh(&tunnel->hlist_lock);
1489 hlist_add_head(&session->hlist,
1490 l2tp_session_id_hash(tunnel, session_id));
1491 write_unlock_bh(&tunnel->hlist_lock);
1493 /* And to the global session list if L2TPv3 */
1494 if (tunnel->version != L2TP_HDR_VER_2) {
1495 struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1497 write_lock_bh(&pn->l2tp_session_hlist_lock);
1498 hlist_add_head(&session->global_hlist,
1499 l2tp_session_id_hash_2(pn, session_id));
1500 write_unlock_bh(&pn->l2tp_session_hlist_lock);
1503 /* Ignore management session in session count value */
1504 if (session->session_id != 0)
1505 atomic_inc(&l2tp_session_count);
1510 EXPORT_SYMBOL_GPL(l2tp_session_create);
1512 /*****************************************************************************
1514 *****************************************************************************/
1516 static __net_init int l2tp_init_net(struct net *net)
1518 struct l2tp_net *pn;
1522 pn = kzalloc(sizeof(*pn), GFP_KERNEL);
1526 INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
1527 rwlock_init(&pn->l2tp_tunnel_list_lock);
1529 for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
1530 INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
1532 rwlock_init(&pn->l2tp_session_hlist_lock);
1534 err = net_assign_generic(net, l2tp_net_id, pn);
1545 static __net_exit void l2tp_exit_net(struct net *net)
1547 struct l2tp_net *pn;
1549 pn = net_generic(net, l2tp_net_id);
1551 * if someone has cached our net then
1552 * further net_generic call will return NULL
1554 net_assign_generic(net, l2tp_net_id, NULL);
1558 static struct pernet_operations l2tp_net_ops = {
1559 .init = l2tp_init_net,
1560 .exit = l2tp_exit_net,
1562 .size = sizeof(struct l2tp_net),
1565 static int __init l2tp_init(void)
1569 rc = register_pernet_device(&l2tp_net_ops);
1573 printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1579 static void __exit l2tp_exit(void)
1581 unregister_pernet_device(&l2tp_net_ops);
1584 module_init(l2tp_init);
1585 module_exit(l2tp_exit);
1587 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1588 MODULE_DESCRIPTION("L2TP core");
1589 MODULE_LICENSE("GPL");
1590 MODULE_VERSION(L2TP_DRV_VERSION);