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>
57 #include <asm/byteorder.h>
58 #include <linux/atomic.h>
60 #include "l2tp_core.h"
62 #define L2TP_DRV_VERSION "V2.0"
64 /* L2TP header constants */
65 #define L2TP_HDRFLAG_T 0x8000
66 #define L2TP_HDRFLAG_L 0x4000
67 #define L2TP_HDRFLAG_S 0x0800
68 #define L2TP_HDRFLAG_O 0x0200
69 #define L2TP_HDRFLAG_P 0x0100
71 #define L2TP_HDR_VER_MASK 0x000F
72 #define L2TP_HDR_VER_2 0x0002
73 #define L2TP_HDR_VER_3 0x0003
75 /* L2TPv3 default L2-specific sublayer */
76 #define L2TP_SLFLAG_S 0x40000000
77 #define L2TP_SL_SEQ_MASK 0x00ffffff
79 #define L2TP_HDR_SIZE_SEQ 10
80 #define L2TP_HDR_SIZE_NOSEQ 6
82 /* Default trace flags */
83 #define L2TP_DEFAULT_DEBUG_FLAGS 0
85 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
87 if ((_mask) & (_type)) \
88 printk(_lvl "L2TP: " _fmt, ##args); \
91 /* Private data stored for received packets in the skb.
97 unsigned long expires
;
100 #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
102 static atomic_t l2tp_tunnel_count
;
103 static atomic_t l2tp_session_count
;
105 /* per-net private data for this module */
106 static unsigned int l2tp_net_id
;
108 struct list_head l2tp_tunnel_list
;
109 spinlock_t l2tp_tunnel_list_lock
;
110 struct hlist_head l2tp_session_hlist
[L2TP_HASH_SIZE_2
];
111 spinlock_t l2tp_session_hlist_lock
;
114 static void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
);
115 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
);
116 static void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
);
118 static inline struct l2tp_net
*l2tp_pernet(struct net
*net
)
122 return net_generic(net
, l2tp_net_id
);
126 /* Tunnel reference counts. Incremented per session that is added to
129 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel
*tunnel
)
131 atomic_inc(&tunnel
->ref_count
);
134 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel
*tunnel
)
136 if (atomic_dec_and_test(&tunnel
->ref_count
))
137 l2tp_tunnel_free(tunnel
);
139 #ifdef L2TP_REFCNT_DEBUG
140 #define l2tp_tunnel_inc_refcount(_t) do { \
141 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
142 l2tp_tunnel_inc_refcount_1(_t); \
144 #define l2tp_tunnel_dec_refcount(_t) do { \
145 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
146 l2tp_tunnel_dec_refcount_1(_t); \
149 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
150 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
153 /* Session hash global list for L2TPv3.
154 * The session_id SHOULD be random according to RFC3931, but several
155 * L2TP implementations use incrementing session_ids. So we do a real
156 * hash on the session_id, rather than a simple bitmask.
158 static inline struct hlist_head
*
159 l2tp_session_id_hash_2(struct l2tp_net
*pn
, u32 session_id
)
161 return &pn
->l2tp_session_hlist
[hash_32(session_id
, L2TP_HASH_BITS_2
)];
165 /* Lookup a session by id in the global session list
167 static struct l2tp_session
*l2tp_session_find_2(struct net
*net
, u32 session_id
)
169 struct l2tp_net
*pn
= l2tp_pernet(net
);
170 struct hlist_head
*session_list
=
171 l2tp_session_id_hash_2(pn
, session_id
);
172 struct l2tp_session
*session
;
173 struct hlist_node
*walk
;
176 hlist_for_each_entry_rcu(session
, walk
, session_list
, global_hlist
) {
177 if (session
->session_id
== session_id
) {
178 rcu_read_unlock_bh();
182 rcu_read_unlock_bh();
187 /* Session hash list.
188 * The session_id SHOULD be random according to RFC2661, but several
189 * L2TP implementations (Cisco and Microsoft) use incrementing
190 * session_ids. So we do a real hash on the session_id, rather than a
193 static inline struct hlist_head
*
194 l2tp_session_id_hash(struct l2tp_tunnel
*tunnel
, u32 session_id
)
196 return &tunnel
->session_hlist
[hash_32(session_id
, L2TP_HASH_BITS
)];
199 /* Lookup a session by id
201 struct l2tp_session
*l2tp_session_find(struct net
*net
, struct l2tp_tunnel
*tunnel
, u32 session_id
)
203 struct hlist_head
*session_list
;
204 struct l2tp_session
*session
;
205 struct hlist_node
*walk
;
207 /* In L2TPv3, session_ids are unique over all tunnels and we
208 * sometimes need to look them up before we know the
212 return l2tp_session_find_2(net
, session_id
);
214 session_list
= l2tp_session_id_hash(tunnel
, session_id
);
215 read_lock_bh(&tunnel
->hlist_lock
);
216 hlist_for_each_entry(session
, walk
, session_list
, hlist
) {
217 if (session
->session_id
== session_id
) {
218 read_unlock_bh(&tunnel
->hlist_lock
);
222 read_unlock_bh(&tunnel
->hlist_lock
);
226 EXPORT_SYMBOL_GPL(l2tp_session_find
);
228 struct l2tp_session
*l2tp_session_find_nth(struct l2tp_tunnel
*tunnel
, int nth
)
231 struct hlist_node
*walk
;
232 struct l2tp_session
*session
;
235 read_lock_bh(&tunnel
->hlist_lock
);
236 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
237 hlist_for_each_entry(session
, walk
, &tunnel
->session_hlist
[hash
], hlist
) {
239 read_unlock_bh(&tunnel
->hlist_lock
);
245 read_unlock_bh(&tunnel
->hlist_lock
);
249 EXPORT_SYMBOL_GPL(l2tp_session_find_nth
);
251 /* Lookup a session by interface name.
252 * This is very inefficient but is only used by management interfaces.
254 struct l2tp_session
*l2tp_session_find_by_ifname(struct net
*net
, char *ifname
)
256 struct l2tp_net
*pn
= l2tp_pernet(net
);
258 struct hlist_node
*walk
;
259 struct l2tp_session
*session
;
262 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++) {
263 hlist_for_each_entry_rcu(session
, walk
, &pn
->l2tp_session_hlist
[hash
], global_hlist
) {
264 if (!strcmp(session
->ifname
, ifname
)) {
265 rcu_read_unlock_bh();
271 rcu_read_unlock_bh();
275 EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname
);
277 /* Lookup a tunnel by id
279 struct l2tp_tunnel
*l2tp_tunnel_find(struct net
*net
, u32 tunnel_id
)
281 struct l2tp_tunnel
*tunnel
;
282 struct l2tp_net
*pn
= l2tp_pernet(net
);
285 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
286 if (tunnel
->tunnel_id
== tunnel_id
) {
287 rcu_read_unlock_bh();
291 rcu_read_unlock_bh();
295 EXPORT_SYMBOL_GPL(l2tp_tunnel_find
);
297 struct l2tp_tunnel
*l2tp_tunnel_find_nth(struct net
*net
, int nth
)
299 struct l2tp_net
*pn
= l2tp_pernet(net
);
300 struct l2tp_tunnel
*tunnel
;
304 list_for_each_entry_rcu(tunnel
, &pn
->l2tp_tunnel_list
, list
) {
306 rcu_read_unlock_bh();
311 rcu_read_unlock_bh();
315 EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth
);
317 /*****************************************************************************
318 * Receive data handling
319 *****************************************************************************/
321 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
324 static void l2tp_recv_queue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
326 struct sk_buff
*skbp
;
328 u32 ns
= L2TP_SKB_CB(skb
)->ns
;
330 spin_lock_bh(&session
->reorder_q
.lock
);
331 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
332 if (L2TP_SKB_CB(skbp
)->ns
> ns
) {
333 __skb_queue_before(&session
->reorder_q
, skbp
, skb
);
334 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
335 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
336 session
->name
, ns
, L2TP_SKB_CB(skbp
)->ns
,
337 skb_queue_len(&session
->reorder_q
));
338 session
->stats
.rx_oos_packets
++;
343 __skb_queue_tail(&session
->reorder_q
, skb
);
346 spin_unlock_bh(&session
->reorder_q
.lock
);
349 /* Dequeue a single skb.
351 static void l2tp_recv_dequeue_skb(struct l2tp_session
*session
, struct sk_buff
*skb
)
353 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
354 int length
= L2TP_SKB_CB(skb
)->length
;
356 /* We're about to requeue the skb, so return resources
357 * to its current owner (a socket receive buffer).
361 tunnel
->stats
.rx_packets
++;
362 tunnel
->stats
.rx_bytes
+= length
;
363 session
->stats
.rx_packets
++;
364 session
->stats
.rx_bytes
+= length
;
366 if (L2TP_SKB_CB(skb
)->has_seq
) {
369 if (tunnel
->version
== L2TP_HDR_VER_2
)
370 session
->nr
&= 0xffff;
372 session
->nr
&= 0xffffff;
374 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
375 "%s: updated nr to %hu\n", session
->name
, session
->nr
);
378 /* call private receive handler */
379 if (session
->recv_skb
!= NULL
)
380 (*session
->recv_skb
)(session
, skb
, L2TP_SKB_CB(skb
)->length
);
385 (*session
->deref
)(session
);
388 /* Dequeue skbs from the session's reorder_q, subject to packet order.
389 * Skbs that have been in the queue for too long are simply discarded.
391 static void l2tp_recv_dequeue(struct l2tp_session
*session
)
396 /* If the pkt at the head of the queue has the nr that we
397 * expect to send up next, dequeue it and any other
398 * in-sequence packets behind it.
400 spin_lock_bh(&session
->reorder_q
.lock
);
401 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
402 if (time_after(jiffies
, L2TP_SKB_CB(skb
)->expires
)) {
403 session
->stats
.rx_seq_discards
++;
404 session
->stats
.rx_errors
++;
405 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
406 "%s: oos pkt %u len %d discarded (too old), "
407 "waiting for %u, reorder_q_len=%d\n",
408 session
->name
, L2TP_SKB_CB(skb
)->ns
,
409 L2TP_SKB_CB(skb
)->length
, session
->nr
,
410 skb_queue_len(&session
->reorder_q
));
411 __skb_unlink(skb
, &session
->reorder_q
);
414 (*session
->deref
)(session
);
418 if (L2TP_SKB_CB(skb
)->has_seq
) {
419 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
420 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
421 "%s: holding oos pkt %u len %d, "
422 "waiting for %u, reorder_q_len=%d\n",
423 session
->name
, L2TP_SKB_CB(skb
)->ns
,
424 L2TP_SKB_CB(skb
)->length
, session
->nr
,
425 skb_queue_len(&session
->reorder_q
));
429 __skb_unlink(skb
, &session
->reorder_q
);
431 /* Process the skb. We release the queue lock while we
432 * do so to let other contexts process the queue.
434 spin_unlock_bh(&session
->reorder_q
.lock
);
435 l2tp_recv_dequeue_skb(session
, skb
);
436 spin_lock_bh(&session
->reorder_q
.lock
);
440 spin_unlock_bh(&session
->reorder_q
.lock
);
443 static inline int l2tp_verify_udp_checksum(struct sock
*sk
,
446 struct udphdr
*uh
= udp_hdr(skb
);
447 u16 ulen
= ntohs(uh
->len
);
448 struct inet_sock
*inet
;
451 if (sk
->sk_no_check
|| skb_csum_unnecessary(skb
) || !uh
->check
)
455 psum
= csum_tcpudp_nofold(inet
->inet_saddr
, inet
->inet_daddr
, ulen
,
458 if ((skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
459 !csum_fold(csum_add(psum
, skb
->csum
)))
464 return __skb_checksum_complete(skb
);
467 /* Do receive processing of L2TP data frames. We handle both L2TPv2
468 * and L2TPv3 data frames here.
470 * L2TPv2 Data Message Header
473 * 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
474 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
475 * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
476 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
477 * | Tunnel ID | Session ID |
478 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
479 * | Ns (opt) | Nr (opt) |
480 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
481 * | Offset Size (opt) | Offset pad... (opt)
482 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484 * Data frames are marked by T=0. All other fields are the same as
485 * those in L2TP control frames.
487 * L2TPv3 Data Message Header
489 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
490 * | L2TP Session Header |
491 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
492 * | L2-Specific Sublayer |
493 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
494 * | Tunnel Payload ...
495 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
497 * L2TPv3 Session Header Over IP
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
503 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
504 * | Cookie (optional, maximum 64 bits)...
505 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
507 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
509 * L2TPv3 L2-Specific Sublayer Format
512 * 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
513 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
514 * |x|S|x|x|x|x|x|x| Sequence Number |
515 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
517 * Cookie value, sublayer format and offset (pad) are negotiated with
518 * the peer when the session is set up. Unlike L2TPv2, we do not need
519 * to parse the packet header to determine if optional fields are
522 * Caller must already have parsed the frame and determined that it is
523 * a data (not control) frame before coming here. Fields up to the
524 * session-id have already been parsed and ptr points to the data
525 * after the session-id.
527 void l2tp_recv_common(struct l2tp_session
*session
, struct sk_buff
*skb
,
528 unsigned char *ptr
, unsigned char *optr
, u16 hdrflags
,
529 int length
, int (*payload_hook
)(struct sk_buff
*skb
))
531 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
535 /* The ref count is increased since we now hold a pointer to
536 * the session. Take care to decrement the refcnt when exiting
537 * this function from now on...
539 l2tp_session_inc_refcount(session
);
541 (*session
->ref
)(session
);
543 /* Parse and check optional cookie */
544 if (session
->peer_cookie_len
> 0) {
545 if (memcmp(ptr
, &session
->peer_cookie
[0], session
->peer_cookie_len
)) {
546 PRINTK(tunnel
->debug
, L2TP_MSG_DATA
, KERN_INFO
,
547 "%s: cookie mismatch (%u/%u). Discarding.\n",
548 tunnel
->name
, tunnel
->tunnel_id
, session
->session_id
);
549 session
->stats
.rx_cookie_discards
++;
552 ptr
+= session
->peer_cookie_len
;
555 /* Handle the optional sequence numbers. Sequence numbers are
556 * in different places for L2TPv2 and L2TPv3.
558 * If we are the LAC, enable/disable sequence numbers under
559 * the control of the LNS. If no sequence numbers present but
560 * we were expecting them, discard frame.
563 L2TP_SKB_CB(skb
)->has_seq
= 0;
564 if (tunnel
->version
== L2TP_HDR_VER_2
) {
565 if (hdrflags
& L2TP_HDRFLAG_S
) {
566 ns
= ntohs(*(__be16
*) ptr
);
568 nr
= ntohs(*(__be16
*) ptr
);
571 /* Store L2TP info in the skb */
572 L2TP_SKB_CB(skb
)->ns
= ns
;
573 L2TP_SKB_CB(skb
)->has_seq
= 1;
575 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
576 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
577 session
->name
, ns
, nr
, session
->nr
);
579 } else if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
580 u32 l2h
= ntohl(*(__be32
*) ptr
);
582 if (l2h
& 0x40000000) {
583 ns
= l2h
& 0x00ffffff;
585 /* Store L2TP info in the skb */
586 L2TP_SKB_CB(skb
)->ns
= ns
;
587 L2TP_SKB_CB(skb
)->has_seq
= 1;
589 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
590 "%s: recv data ns=%u, session nr=%u\n",
591 session
->name
, ns
, session
->nr
);
595 /* Advance past L2-specific header, if present */
596 ptr
+= session
->l2specific_len
;
598 if (L2TP_SKB_CB(skb
)->has_seq
) {
599 /* Received a packet with sequence numbers. If we're the LNS,
600 * check if we sre sending sequence numbers and if not,
603 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
604 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_INFO
,
605 "%s: requested to enable seq numbers by LNS\n",
607 session
->send_seq
= -1;
608 l2tp_session_set_header_len(session
, tunnel
->version
);
611 /* No sequence numbers.
612 * If user has configured mandatory sequence numbers, discard.
614 if (session
->recv_seq
) {
615 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_WARNING
,
616 "%s: recv data has no seq numbers when required. "
617 "Discarding\n", session
->name
);
618 session
->stats
.rx_seq_discards
++;
622 /* If we're the LAC and we're sending sequence numbers, the
623 * LNS has requested that we no longer send sequence numbers.
624 * If we're the LNS and we're sending sequence numbers, the
625 * LAC is broken. Discard the frame.
627 if ((!session
->lns_mode
) && (session
->send_seq
)) {
628 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_INFO
,
629 "%s: requested to disable seq numbers by LNS\n",
631 session
->send_seq
= 0;
632 l2tp_session_set_header_len(session
, tunnel
->version
);
633 } else if (session
->send_seq
) {
634 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_WARNING
,
635 "%s: recv data has no seq numbers when required. "
636 "Discarding\n", session
->name
);
637 session
->stats
.rx_seq_discards
++;
642 /* Session data offset is handled differently for L2TPv2 and
643 * L2TPv3. For L2TPv2, there is an optional 16-bit value in
644 * the header. For L2TPv3, the offset is negotiated using AVPs
645 * in the session setup control protocol.
647 if (tunnel
->version
== L2TP_HDR_VER_2
) {
648 /* If offset bit set, skip it. */
649 if (hdrflags
& L2TP_HDRFLAG_O
) {
650 offset
= ntohs(*(__be16
*)ptr
);
654 ptr
+= session
->offset
;
657 if (!pskb_may_pull(skb
, offset
))
660 __skb_pull(skb
, offset
);
662 /* If caller wants to process the payload before we queue the
666 if ((*payload_hook
)(skb
))
669 /* Prepare skb for adding to the session's reorder_q. Hold
670 * packets for max reorder_timeout or 1 second if not
673 L2TP_SKB_CB(skb
)->length
= length
;
674 L2TP_SKB_CB(skb
)->expires
= jiffies
+
675 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
677 /* Add packet to the session's receive queue. Reordering is done here, if
678 * enabled. Saved L2TP protocol info is stored in skb->sb[].
680 if (L2TP_SKB_CB(skb
)->has_seq
) {
681 if (session
->reorder_timeout
!= 0) {
682 /* Packet reordering enabled. Add skb to session's
683 * reorder queue, in order of ns.
685 l2tp_recv_queue_skb(session
, skb
);
687 /* Packet reordering disabled. Discard out-of-sequence
690 if (L2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
691 session
->stats
.rx_seq_discards
++;
692 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
693 "%s: oos pkt %u len %d discarded, "
694 "waiting for %u, reorder_q_len=%d\n",
695 session
->name
, L2TP_SKB_CB(skb
)->ns
,
696 L2TP_SKB_CB(skb
)->length
, session
->nr
,
697 skb_queue_len(&session
->reorder_q
));
700 skb_queue_tail(&session
->reorder_q
, skb
);
703 /* No sequence numbers. Add the skb to the tail of the
704 * reorder queue. This ensures that it will be
705 * delivered after all previous sequenced skbs.
707 skb_queue_tail(&session
->reorder_q
, skb
);
710 /* Try to dequeue as many skbs from reorder_q as we can. */
711 l2tp_recv_dequeue(session
);
713 l2tp_session_dec_refcount(session
);
718 session
->stats
.rx_errors
++;
722 (*session
->deref
)(session
);
724 l2tp_session_dec_refcount(session
);
726 EXPORT_SYMBOL(l2tp_recv_common
);
728 /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
729 * here. The skb is not on a list when we get here.
730 * Returns 0 if the packet was a data packet and was successfully passed on.
731 * Returns 1 if the packet was not a good data packet and could not be
732 * forwarded. All such packets are passed up to userspace to deal with.
734 static int l2tp_udp_recv_core(struct l2tp_tunnel
*tunnel
, struct sk_buff
*skb
,
735 int (*payload_hook
)(struct sk_buff
*skb
))
737 struct l2tp_session
*session
= NULL
;
738 unsigned char *ptr
, *optr
;
740 u32 tunnel_id
, session_id
;
745 if (tunnel
->sock
&& l2tp_verify_udp_checksum(tunnel
->sock
, skb
))
746 goto discard_bad_csum
;
748 /* UDP always verifies the packet length. */
749 __skb_pull(skb
, sizeof(struct udphdr
));
752 if (!pskb_may_pull(skb
, L2TP_HDR_SIZE_SEQ
)) {
753 PRINTK(tunnel
->debug
, L2TP_MSG_DATA
, KERN_INFO
,
754 "%s: recv short packet (len=%d)\n", tunnel
->name
, skb
->len
);
758 /* Point to L2TP header */
759 optr
= ptr
= skb
->data
;
761 /* Trace packet contents, if enabled */
762 if (tunnel
->debug
& L2TP_MSG_DATA
) {
763 length
= min(32u, skb
->len
);
764 if (!pskb_may_pull(skb
, length
))
767 printk(KERN_DEBUG
"%s: recv: ", tunnel
->name
);
771 printk(" %02X", ptr
[offset
]);
772 } while (++offset
< length
);
777 /* Get L2TP header flags */
778 hdrflags
= ntohs(*(__be16
*) ptr
);
780 /* Check protocol version */
781 version
= hdrflags
& L2TP_HDR_VER_MASK
;
782 if (version
!= tunnel
->version
) {
783 PRINTK(tunnel
->debug
, L2TP_MSG_DATA
, KERN_INFO
,
784 "%s: recv protocol version mismatch: got %d expected %d\n",
785 tunnel
->name
, version
, tunnel
->version
);
789 /* Get length of L2TP packet */
792 /* If type is control packet, it is handled by userspace. */
793 if (hdrflags
& L2TP_HDRFLAG_T
) {
794 PRINTK(tunnel
->debug
, L2TP_MSG_DATA
, KERN_DEBUG
,
795 "%s: recv control packet, len=%d\n", tunnel
->name
, length
);
802 if (tunnel
->version
== L2TP_HDR_VER_2
) {
803 /* If length is present, skip it */
804 if (hdrflags
& L2TP_HDRFLAG_L
)
807 /* Extract tunnel and session ID */
808 tunnel_id
= ntohs(*(__be16
*) ptr
);
810 session_id
= ntohs(*(__be16
*) ptr
);
813 ptr
+= 2; /* skip reserved bits */
814 tunnel_id
= tunnel
->tunnel_id
;
815 session_id
= ntohl(*(__be32
*) ptr
);
819 /* Find the session context */
820 session
= l2tp_session_find(tunnel
->l2tp_net
, tunnel
, session_id
);
821 if (!session
|| !session
->recv_skb
) {
822 /* Not found? Pass to userspace to deal with */
823 PRINTK(tunnel
->debug
, L2TP_MSG_DATA
, KERN_INFO
,
824 "%s: no session found (%u/%u). Passing up.\n",
825 tunnel
->name
, tunnel_id
, session_id
);
829 l2tp_recv_common(session
, skb
, ptr
, optr
, hdrflags
, length
, payload_hook
);
834 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel
->name
);
835 UDP_INC_STATS_USER(tunnel
->l2tp_net
, UDP_MIB_INERRORS
, 0);
836 tunnel
->stats
.rx_errors
++;
842 /* Put UDP header back */
843 __skb_push(skb
, sizeof(struct udphdr
));
848 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
852 * >0: skb should be passed up to userspace as UDP.
854 int l2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
856 struct l2tp_tunnel
*tunnel
;
858 tunnel
= l2tp_sock_to_tunnel(sk
);
862 PRINTK(tunnel
->debug
, L2TP_MSG_DATA
, KERN_DEBUG
,
863 "%s: received %d bytes\n", tunnel
->name
, skb
->len
);
865 if (l2tp_udp_recv_core(tunnel
, skb
, tunnel
->recv_payload_hook
))
876 EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv
);
878 /************************************************************************
880 ***********************************************************************/
882 /* Build an L2TP header for the session into the buffer provided.
884 static int l2tp_build_l2tpv2_header(struct l2tp_session
*session
, void *buf
)
886 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
889 u16 flags
= L2TP_HDR_VER_2
;
890 u32 tunnel_id
= tunnel
->peer_tunnel_id
;
891 u32 session_id
= session
->peer_session_id
;
893 if (session
->send_seq
)
894 flags
|= L2TP_HDRFLAG_S
;
896 /* Setup L2TP header. */
897 *bufp
++ = htons(flags
);
898 *bufp
++ = htons(tunnel_id
);
899 *bufp
++ = htons(session_id
);
900 if (session
->send_seq
) {
901 *bufp
++ = htons(session
->ns
);
904 session
->ns
&= 0xffff;
905 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
906 "%s: updated ns to %u\n", session
->name
, session
->ns
);
912 static int l2tp_build_l2tpv3_header(struct l2tp_session
*session
, void *buf
)
914 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
918 /* Setup L2TP header. The header differs slightly for UDP and
919 * IP encapsulations. For UDP, there is 4 bytes of flags.
921 if (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) {
922 u16 flags
= L2TP_HDR_VER_3
;
923 *((__be16
*) bufp
) = htons(flags
);
925 *((__be16
*) bufp
) = 0;
929 *((__be32
*) bufp
) = htonl(session
->peer_session_id
);
931 if (session
->cookie_len
) {
932 memcpy(bufp
, &session
->cookie
[0], session
->cookie_len
);
933 bufp
+= session
->cookie_len
;
935 if (session
->l2specific_len
) {
936 if (session
->l2specific_type
== L2TP_L2SPECTYPE_DEFAULT
) {
938 if (session
->send_seq
) {
939 l2h
= 0x40000000 | session
->ns
;
941 session
->ns
&= 0xffffff;
942 PRINTK(session
->debug
, L2TP_MSG_SEQ
, KERN_DEBUG
,
943 "%s: updated ns to %u\n", session
->name
, session
->ns
);
946 *((__be32
*) bufp
) = htonl(l2h
);
948 bufp
+= session
->l2specific_len
;
951 bufp
+= session
->offset
;
956 static int l2tp_xmit_core(struct l2tp_session
*session
, struct sk_buff
*skb
,
957 struct flowi
*fl
, size_t data_len
)
959 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
960 unsigned int len
= skb
->len
;
964 if (session
->send_seq
)
965 PRINTK(session
->debug
, L2TP_MSG_DATA
, KERN_DEBUG
,
966 "%s: send %Zd bytes, ns=%u\n", session
->name
,
967 data_len
, session
->ns
- 1);
969 PRINTK(session
->debug
, L2TP_MSG_DATA
, KERN_DEBUG
,
970 "%s: send %Zd bytes\n", session
->name
, data_len
);
972 if (session
->debug
& L2TP_MSG_DATA
) {
974 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
975 unsigned char *datap
= skb
->data
+ uhlen
;
977 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
978 for (i
= 0; i
< (len
- uhlen
); i
++) {
979 printk(" %02X", *datap
++);
988 /* Queue the packet to IP for output */
990 error
= ip_queue_xmit(skb
, fl
);
994 tunnel
->stats
.tx_packets
++;
995 tunnel
->stats
.tx_bytes
+= len
;
996 session
->stats
.tx_packets
++;
997 session
->stats
.tx_bytes
+= len
;
999 tunnel
->stats
.tx_errors
++;
1000 session
->stats
.tx_errors
++;
1006 /* Automatically called when the skb is freed.
1008 static void l2tp_sock_wfree(struct sk_buff
*skb
)
1013 /* For data skbs that we transmit, we associate with the tunnel socket
1014 * but don't do accounting.
1016 static inline void l2tp_skb_set_owner_w(struct sk_buff
*skb
, struct sock
*sk
)
1020 skb
->destructor
= l2tp_sock_wfree
;
1023 /* If caller requires the skb to have a ppp header, the header must be
1024 * inserted in the skb data before calling this function.
1026 int l2tp_xmit_skb(struct l2tp_session
*session
, struct sk_buff
*skb
, int hdr_len
)
1028 int data_len
= skb
->len
;
1029 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
1030 struct sock
*sk
= tunnel
->sock
;
1033 struct inet_sock
*inet
;
1038 int uhlen
= (tunnel
->encap
== L2TP_ENCAPTYPE_UDP
) ? sizeof(struct udphdr
) : 0;
1041 /* Check that there's enough headroom in the skb to insert IP,
1042 * UDP and L2TP headers. If not enough, expand it to
1043 * make room. Adjust truesize.
1045 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1047 old_headroom
= skb_headroom(skb
);
1048 if (skb_cow_head(skb
, headroom
)) {
1053 new_headroom
= skb_headroom(skb
);
1055 skb
->truesize
+= new_headroom
- old_headroom
;
1057 /* Setup L2TP header */
1058 session
->build_header(session
, __skb_push(skb
, hdr_len
));
1060 /* Reset skb netfilter state */
1061 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1062 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1067 if (sock_owned_by_user(sk
)) {
1072 /* Get routing info from the tunnel socket */
1074 skb_dst_set(skb
, dst_clone(__sk_dst_get(sk
)));
1077 fl
= &inet
->cork
.fl
;
1078 switch (tunnel
->encap
) {
1079 case L2TP_ENCAPTYPE_UDP
:
1080 /* Setup UDP header */
1081 __skb_push(skb
, sizeof(*uh
));
1082 skb_reset_transport_header(skb
);
1084 uh
->source
= inet
->inet_sport
;
1085 uh
->dest
= inet
->inet_dport
;
1086 udp_len
= uhlen
+ hdr_len
+ data_len
;
1087 uh
->len
= htons(udp_len
);
1090 /* Calculate UDP checksum if configured to do so */
1091 if (sk
->sk_no_check
== UDP_CSUM_NOXMIT
)
1092 skb
->ip_summed
= CHECKSUM_NONE
;
1093 else if ((skb_dst(skb
) && skb_dst(skb
)->dev
) &&
1094 (!(skb_dst(skb
)->dev
->features
& NETIF_F_V4_CSUM
))) {
1095 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1096 csum
= skb_checksum(skb
, 0, udp_len
, 0);
1097 uh
->check
= csum_tcpudp_magic(inet
->inet_saddr
,
1099 udp_len
, IPPROTO_UDP
, csum
);
1101 uh
->check
= CSUM_MANGLED_0
;
1103 skb
->ip_summed
= CHECKSUM_PARTIAL
;
1104 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
1105 skb
->csum_offset
= offsetof(struct udphdr
, check
);
1106 uh
->check
= ~csum_tcpudp_magic(inet
->inet_saddr
,
1108 udp_len
, IPPROTO_UDP
, 0);
1112 case L2TP_ENCAPTYPE_IP
:
1116 l2tp_skb_set_owner_w(skb
, sk
);
1118 l2tp_xmit_core(session
, skb
, fl
, data_len
);
1125 EXPORT_SYMBOL_GPL(l2tp_xmit_skb
);
1127 /*****************************************************************************
1128 * Tinnel and session create/destroy.
1129 *****************************************************************************/
1131 /* Tunnel socket destruct hook.
1132 * The tunnel context is deleted only when all session sockets have been
1135 static void l2tp_tunnel_destruct(struct sock
*sk
)
1137 struct l2tp_tunnel
*tunnel
;
1139 tunnel
= sk
->sk_user_data
;
1143 PRINTK(tunnel
->debug
, L2TP_MSG_CONTROL
, KERN_INFO
,
1144 "%s: closing...\n", tunnel
->name
);
1146 /* Close all sessions */
1147 l2tp_tunnel_closeall(tunnel
);
1149 switch (tunnel
->encap
) {
1150 case L2TP_ENCAPTYPE_UDP
:
1151 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1152 (udp_sk(sk
))->encap_type
= 0;
1153 (udp_sk(sk
))->encap_rcv
= NULL
;
1155 case L2TP_ENCAPTYPE_IP
:
1159 /* Remove hooks into tunnel socket */
1160 tunnel
->sock
= NULL
;
1161 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1162 sk
->sk_user_data
= NULL
;
1164 /* Call the original destructor */
1165 if (sk
->sk_destruct
)
1166 (*sk
->sk_destruct
)(sk
);
1168 /* We're finished with the socket */
1169 l2tp_tunnel_dec_refcount(tunnel
);
1175 /* When the tunnel is closed, all the attached sessions need to go too.
1177 static void l2tp_tunnel_closeall(struct l2tp_tunnel
*tunnel
)
1180 struct hlist_node
*walk
;
1181 struct hlist_node
*tmp
;
1182 struct l2tp_session
*session
;
1184 BUG_ON(tunnel
== NULL
);
1186 PRINTK(tunnel
->debug
, L2TP_MSG_CONTROL
, KERN_INFO
,
1187 "%s: closing all sessions...\n", tunnel
->name
);
1189 write_lock_bh(&tunnel
->hlist_lock
);
1190 for (hash
= 0; hash
< L2TP_HASH_SIZE
; hash
++) {
1192 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1193 session
= hlist_entry(walk
, struct l2tp_session
, hlist
);
1195 PRINTK(session
->debug
, L2TP_MSG_CONTROL
, KERN_INFO
,
1196 "%s: closing session\n", session
->name
);
1198 hlist_del_init(&session
->hlist
);
1200 /* Since we should hold the sock lock while
1201 * doing any unbinding, we need to release the
1202 * lock we're holding before taking that lock.
1203 * Hold a reference to the sock so it doesn't
1204 * disappear as we're jumping between locks.
1206 if (session
->ref
!= NULL
)
1207 (*session
->ref
)(session
);
1209 write_unlock_bh(&tunnel
->hlist_lock
);
1211 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1212 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1214 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1215 hlist_del_init_rcu(&session
->global_hlist
);
1216 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1220 if (session
->session_close
!= NULL
)
1221 (*session
->session_close
)(session
);
1223 if (session
->deref
!= NULL
)
1224 (*session
->deref
)(session
);
1226 write_lock_bh(&tunnel
->hlist_lock
);
1228 /* Now restart from the beginning of this hash
1229 * chain. We always remove a session from the
1230 * list so we are guaranteed to make forward
1236 write_unlock_bh(&tunnel
->hlist_lock
);
1239 /* Really kill the tunnel.
1240 * Come here only when all sessions have been cleared from the tunnel.
1242 static void l2tp_tunnel_free(struct l2tp_tunnel
*tunnel
)
1244 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1246 BUG_ON(atomic_read(&tunnel
->ref_count
) != 0);
1247 BUG_ON(tunnel
->sock
!= NULL
);
1249 PRINTK(tunnel
->debug
, L2TP_MSG_CONTROL
, KERN_INFO
,
1250 "%s: free...\n", tunnel
->name
);
1252 /* Remove from tunnel list */
1253 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1254 list_del_rcu(&tunnel
->list
);
1255 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1258 atomic_dec(&l2tp_tunnel_count
);
1262 /* Create a socket for the tunnel, if one isn't set up by
1263 * userspace. This is used for static tunnels where there is no
1264 * managing L2TP daemon.
1266 static int l2tp_tunnel_sock_create(u32 tunnel_id
, u32 peer_tunnel_id
, struct l2tp_tunnel_cfg
*cfg
, struct socket
**sockp
)
1269 struct sockaddr_in udp_addr
;
1270 struct sockaddr_l2tpip ip_addr
;
1271 struct socket
*sock
= NULL
;
1273 switch (cfg
->encap
) {
1274 case L2TP_ENCAPTYPE_UDP
:
1275 err
= sock_create(AF_INET
, SOCK_DGRAM
, 0, sockp
);
1281 memset(&udp_addr
, 0, sizeof(udp_addr
));
1282 udp_addr
.sin_family
= AF_INET
;
1283 udp_addr
.sin_addr
= cfg
->local_ip
;
1284 udp_addr
.sin_port
= htons(cfg
->local_udp_port
);
1285 err
= kernel_bind(sock
, (struct sockaddr
*) &udp_addr
, sizeof(udp_addr
));
1289 udp_addr
.sin_family
= AF_INET
;
1290 udp_addr
.sin_addr
= cfg
->peer_ip
;
1291 udp_addr
.sin_port
= htons(cfg
->peer_udp_port
);
1292 err
= kernel_connect(sock
, (struct sockaddr
*) &udp_addr
, sizeof(udp_addr
), 0);
1296 if (!cfg
->use_udp_checksums
)
1297 sock
->sk
->sk_no_check
= UDP_CSUM_NOXMIT
;
1301 case L2TP_ENCAPTYPE_IP
:
1302 err
= sock_create(AF_INET
, SOCK_DGRAM
, IPPROTO_L2TP
, sockp
);
1308 memset(&ip_addr
, 0, sizeof(ip_addr
));
1309 ip_addr
.l2tp_family
= AF_INET
;
1310 ip_addr
.l2tp_addr
= cfg
->local_ip
;
1311 ip_addr
.l2tp_conn_id
= tunnel_id
;
1312 err
= kernel_bind(sock
, (struct sockaddr
*) &ip_addr
, sizeof(ip_addr
));
1316 ip_addr
.l2tp_family
= AF_INET
;
1317 ip_addr
.l2tp_addr
= cfg
->peer_ip
;
1318 ip_addr
.l2tp_conn_id
= peer_tunnel_id
;
1319 err
= kernel_connect(sock
, (struct sockaddr
*) &ip_addr
, sizeof(ip_addr
), 0);
1330 if ((err
< 0) && sock
) {
1338 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
)
1340 struct l2tp_tunnel
*tunnel
= NULL
;
1342 struct socket
*sock
= NULL
;
1343 struct sock
*sk
= NULL
;
1344 struct l2tp_net
*pn
;
1345 enum l2tp_encap_type encap
= L2TP_ENCAPTYPE_UDP
;
1347 /* Get the tunnel socket from the fd, which was opened by
1348 * the userspace L2TP daemon. If not specified, create a
1352 err
= l2tp_tunnel_sock_create(tunnel_id
, peer_tunnel_id
, cfg
, &sock
);
1357 sock
= sockfd_lookup(fd
, &err
);
1359 printk(KERN_ERR
"tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1360 tunnel_id
, fd
, err
);
1370 /* Quick sanity checks */
1372 case L2TP_ENCAPTYPE_UDP
:
1373 err
= -EPROTONOSUPPORT
;
1374 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1375 printk(KERN_ERR
"tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1376 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1380 case L2TP_ENCAPTYPE_IP
:
1381 err
= -EPROTONOSUPPORT
;
1382 if (sk
->sk_protocol
!= IPPROTO_L2TP
) {
1383 printk(KERN_ERR
"tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1384 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_L2TP
);
1390 /* Check if this socket has already been prepped */
1391 tunnel
= (struct l2tp_tunnel
*)sk
->sk_user_data
;
1392 if (tunnel
!= NULL
) {
1393 /* This socket has already been prepped */
1398 tunnel
= kzalloc(sizeof(struct l2tp_tunnel
), GFP_KERNEL
);
1399 if (tunnel
== NULL
) {
1404 tunnel
->version
= version
;
1405 tunnel
->tunnel_id
= tunnel_id
;
1406 tunnel
->peer_tunnel_id
= peer_tunnel_id
;
1407 tunnel
->debug
= L2TP_DEFAULT_DEBUG_FLAGS
;
1409 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1410 sprintf(&tunnel
->name
[0], "tunl %u", tunnel_id
);
1411 rwlock_init(&tunnel
->hlist_lock
);
1413 /* The net we belong to */
1414 tunnel
->l2tp_net
= net
;
1415 pn
= l2tp_pernet(net
);
1418 tunnel
->debug
= cfg
->debug
;
1420 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1421 tunnel
->encap
= encap
;
1422 if (encap
== L2TP_ENCAPTYPE_UDP
) {
1423 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1424 udp_sk(sk
)->encap_type
= UDP_ENCAP_L2TPINUDP
;
1425 udp_sk(sk
)->encap_rcv
= l2tp_udp_encap_recv
;
1428 sk
->sk_user_data
= tunnel
;
1430 /* Hook on the tunnel socket destructor so that we can cleanup
1431 * if the tunnel socket goes away.
1433 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1434 sk
->sk_destruct
= &l2tp_tunnel_destruct
;
1436 sk
->sk_allocation
= GFP_ATOMIC
;
1438 /* Add tunnel to our list */
1439 INIT_LIST_HEAD(&tunnel
->list
);
1440 atomic_inc(&l2tp_tunnel_count
);
1442 /* Bump the reference count. The tunnel context is deleted
1443 * only when this drops to zero. Must be done before list insertion
1445 l2tp_tunnel_inc_refcount(tunnel
);
1446 spin_lock_bh(&pn
->l2tp_tunnel_list_lock
);
1447 list_add_rcu(&tunnel
->list
, &pn
->l2tp_tunnel_list
);
1448 spin_unlock_bh(&pn
->l2tp_tunnel_list_lock
);
1455 /* If tunnel's socket was created by the kernel, it doesn't
1458 if (sock
&& sock
->file
)
1463 EXPORT_SYMBOL_GPL(l2tp_tunnel_create
);
1465 /* This function is used by the netlink TUNNEL_DELETE command.
1467 int l2tp_tunnel_delete(struct l2tp_tunnel
*tunnel
)
1470 struct socket
*sock
= tunnel
->sock
? tunnel
->sock
->sk_socket
: NULL
;
1472 /* Force the tunnel socket to close. This will eventually
1473 * cause the tunnel to be deleted via the normal socket close
1474 * mechanisms when userspace closes the tunnel socket.
1477 err
= inet_shutdown(sock
, 2);
1479 /* If the tunnel's socket was created by the kernel,
1480 * close the socket here since the socket was not
1481 * created by userspace.
1483 if (sock
->file
== NULL
)
1484 err
= inet_release(sock
);
1489 EXPORT_SYMBOL_GPL(l2tp_tunnel_delete
);
1491 /* Really kill the session.
1493 void l2tp_session_free(struct l2tp_session
*session
)
1495 struct l2tp_tunnel
*tunnel
;
1497 BUG_ON(atomic_read(&session
->ref_count
) != 0);
1499 tunnel
= session
->tunnel
;
1500 if (tunnel
!= NULL
) {
1501 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1503 /* Delete the session from the hash */
1504 write_lock_bh(&tunnel
->hlist_lock
);
1505 hlist_del_init(&session
->hlist
);
1506 write_unlock_bh(&tunnel
->hlist_lock
);
1508 /* Unlink from the global hash if not L2TPv2 */
1509 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1510 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1512 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1513 hlist_del_init_rcu(&session
->global_hlist
);
1514 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1518 if (session
->session_id
!= 0)
1519 atomic_dec(&l2tp_session_count
);
1521 sock_put(tunnel
->sock
);
1523 /* This will delete the tunnel context if this
1524 * is the last session on the tunnel.
1526 session
->tunnel
= NULL
;
1527 l2tp_tunnel_dec_refcount(tunnel
);
1534 EXPORT_SYMBOL_GPL(l2tp_session_free
);
1536 /* This function is used by the netlink SESSION_DELETE command and by
1539 int l2tp_session_delete(struct l2tp_session
*session
)
1541 if (session
->session_close
!= NULL
)
1542 (*session
->session_close
)(session
);
1544 l2tp_session_dec_refcount(session
);
1548 EXPORT_SYMBOL_GPL(l2tp_session_delete
);
1551 /* We come here whenever a session's send_seq, cookie_len or
1552 * l2specific_len parameters are set.
1554 static void l2tp_session_set_header_len(struct l2tp_session
*session
, int version
)
1556 if (version
== L2TP_HDR_VER_2
) {
1557 session
->hdr_len
= 6;
1558 if (session
->send_seq
)
1559 session
->hdr_len
+= 4;
1561 session
->hdr_len
= 4 + session
->cookie_len
+ session
->l2specific_len
+ session
->offset
;
1562 if (session
->tunnel
->encap
== L2TP_ENCAPTYPE_UDP
)
1563 session
->hdr_len
+= 4;
1568 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
)
1570 struct l2tp_session
*session
;
1572 session
= kzalloc(sizeof(struct l2tp_session
) + priv_size
, GFP_KERNEL
);
1573 if (session
!= NULL
) {
1574 session
->magic
= L2TP_SESSION_MAGIC
;
1575 session
->tunnel
= tunnel
;
1577 session
->session_id
= session_id
;
1578 session
->peer_session_id
= peer_session_id
;
1581 sprintf(&session
->name
[0], "sess %u/%u",
1582 tunnel
->tunnel_id
, session
->session_id
);
1584 skb_queue_head_init(&session
->reorder_q
);
1586 INIT_HLIST_NODE(&session
->hlist
);
1587 INIT_HLIST_NODE(&session
->global_hlist
);
1589 /* Inherit debug options from tunnel */
1590 session
->debug
= tunnel
->debug
;
1593 session
->pwtype
= cfg
->pw_type
;
1594 session
->debug
= cfg
->debug
;
1595 session
->mtu
= cfg
->mtu
;
1596 session
->mru
= cfg
->mru
;
1597 session
->send_seq
= cfg
->send_seq
;
1598 session
->recv_seq
= cfg
->recv_seq
;
1599 session
->lns_mode
= cfg
->lns_mode
;
1600 session
->reorder_timeout
= cfg
->reorder_timeout
;
1601 session
->offset
= cfg
->offset
;
1602 session
->l2specific_type
= cfg
->l2specific_type
;
1603 session
->l2specific_len
= cfg
->l2specific_len
;
1604 session
->cookie_len
= cfg
->cookie_len
;
1605 memcpy(&session
->cookie
[0], &cfg
->cookie
[0], cfg
->cookie_len
);
1606 session
->peer_cookie_len
= cfg
->peer_cookie_len
;
1607 memcpy(&session
->peer_cookie
[0], &cfg
->peer_cookie
[0], cfg
->peer_cookie_len
);
1610 if (tunnel
->version
== L2TP_HDR_VER_2
)
1611 session
->build_header
= l2tp_build_l2tpv2_header
;
1613 session
->build_header
= l2tp_build_l2tpv3_header
;
1615 l2tp_session_set_header_len(session
, tunnel
->version
);
1617 /* Bump the reference count. The session context is deleted
1618 * only when this drops to zero.
1620 l2tp_session_inc_refcount(session
);
1621 l2tp_tunnel_inc_refcount(tunnel
);
1623 /* Ensure tunnel socket isn't deleted */
1624 sock_hold(tunnel
->sock
);
1626 /* Add session to the tunnel's hash list */
1627 write_lock_bh(&tunnel
->hlist_lock
);
1628 hlist_add_head(&session
->hlist
,
1629 l2tp_session_id_hash(tunnel
, session_id
));
1630 write_unlock_bh(&tunnel
->hlist_lock
);
1632 /* And to the global session list if L2TPv3 */
1633 if (tunnel
->version
!= L2TP_HDR_VER_2
) {
1634 struct l2tp_net
*pn
= l2tp_pernet(tunnel
->l2tp_net
);
1636 spin_lock_bh(&pn
->l2tp_session_hlist_lock
);
1637 hlist_add_head_rcu(&session
->global_hlist
,
1638 l2tp_session_id_hash_2(pn
, session_id
));
1639 spin_unlock_bh(&pn
->l2tp_session_hlist_lock
);
1642 /* Ignore management session in session count value */
1643 if (session
->session_id
!= 0)
1644 atomic_inc(&l2tp_session_count
);
1649 EXPORT_SYMBOL_GPL(l2tp_session_create
);
1651 /*****************************************************************************
1653 *****************************************************************************/
1655 static __net_init
int l2tp_init_net(struct net
*net
)
1657 struct l2tp_net
*pn
= net_generic(net
, l2tp_net_id
);
1660 INIT_LIST_HEAD(&pn
->l2tp_tunnel_list
);
1661 spin_lock_init(&pn
->l2tp_tunnel_list_lock
);
1663 for (hash
= 0; hash
< L2TP_HASH_SIZE_2
; hash
++)
1664 INIT_HLIST_HEAD(&pn
->l2tp_session_hlist
[hash
]);
1666 spin_lock_init(&pn
->l2tp_session_hlist_lock
);
1671 static struct pernet_operations l2tp_net_ops
= {
1672 .init
= l2tp_init_net
,
1674 .size
= sizeof(struct l2tp_net
),
1677 static int __init
l2tp_init(void)
1681 rc
= register_pernet_device(&l2tp_net_ops
);
1685 printk(KERN_INFO
"L2TP core driver, %s\n", L2TP_DRV_VERSION
);
1691 static void __exit
l2tp_exit(void)
1693 unregister_pernet_device(&l2tp_net_ops
);
1696 module_init(l2tp_init
);
1697 module_exit(l2tp_exit
);
1699 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1700 MODULE_DESCRIPTION("L2TP core");
1701 MODULE_LICENSE("GPL");
1702 MODULE_VERSION(L2TP_DRV_VERSION
);