1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
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)
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
24 /* This driver handles only L2TP data frames; control frames are handled by a
25 * userspace application.
27 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
28 * attaches it to a bound UDP socket with local tunnel_id / session_id and
29 * peer tunnel_id / session_id set. Data can then be sent or received using
30 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
31 * can be read or modified using ioctl() or [gs]etsockopt() calls.
33 * When a PPPoL2TP socket is connected with local and peer session_id values
34 * zero, the socket is treated as a special tunnel management socket.
36 * Here's example userspace code to create a socket for sending/receiving data
37 * over an L2TP session:-
39 * struct sockaddr_pppol2tp sax;
43 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
45 * sax.sa_family = AF_PPPOX;
46 * sax.sa_protocol = PX_PROTO_OL2TP;
47 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
48 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
49 * sax.pppol2tp.addr.sin_port = addr->sin_port;
50 * sax.pppol2tp.addr.sin_family = AF_INET;
51 * sax.pppol2tp.s_tunnel = tunnel_id;
52 * sax.pppol2tp.s_session = session_id;
53 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
54 * sax.pppol2tp.d_session = peer_session_id;
56 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
58 * A pppd plugin that allows PPP traffic to be carried over L2TP using
59 * this driver is available from the OpenL2TP project at
60 * http://openl2tp.sourceforge.net.
63 #include <linux/module.h>
64 #include <linux/string.h>
65 #include <linux/list.h>
66 #include <asm/uaccess.h>
68 #include <linux/kernel.h>
69 #include <linux/spinlock.h>
70 #include <linux/kthread.h>
71 #include <linux/sched.h>
72 #include <linux/slab.h>
73 #include <linux/errno.h>
74 #include <linux/jiffies.h>
76 #include <linux/netdevice.h>
77 #include <linux/net.h>
78 #include <linux/inetdevice.h>
79 #include <linux/skbuff.h>
80 #include <linux/init.h>
82 #include <linux/udp.h>
83 #include <linux/if_pppox.h>
84 #include <linux/if_pppol2tp.h>
86 #include <linux/ppp_channel.h>
87 #include <linux/ppp_defs.h>
88 #include <linux/if_ppp.h>
89 #include <linux/file.h>
90 #include <linux/hash.h>
91 #include <linux/sort.h>
92 #include <linux/proc_fs.h>
93 #include <net/net_namespace.h>
99 #include <asm/byteorder.h>
100 #include <asm/atomic.h>
103 #define PPPOL2TP_DRV_VERSION "V1.0"
105 /* L2TP header constants */
106 #define L2TP_HDRFLAG_T 0x8000
107 #define L2TP_HDRFLAG_L 0x4000
108 #define L2TP_HDRFLAG_S 0x0800
109 #define L2TP_HDRFLAG_O 0x0200
110 #define L2TP_HDRFLAG_P 0x0100
112 #define L2TP_HDR_VER_MASK 0x000F
113 #define L2TP_HDR_VER 0x0002
115 /* Space for UDP, L2TP and PPP headers */
116 #define PPPOL2TP_HEADER_OVERHEAD 40
118 /* Just some random numbers */
119 #define L2TP_TUNNEL_MAGIC 0x42114DDA
120 #define L2TP_SESSION_MAGIC 0x0C04EB7D
122 #define PPPOL2TP_HASH_BITS 4
123 #define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
125 /* Default trace flags */
126 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
128 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
130 if ((_mask) & (_type)) \
131 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
134 /* Number of bytes to build transmit L2TP headers.
135 * Unfortunately the size is different depending on whether sequence numbers
138 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
139 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
141 struct pppol2tp_tunnel
;
143 /* Describes a session. It is the sk_user_data field in the PPPoL2TP
144 * socket. Contains information to determine incoming packets and transmit
147 struct pppol2tp_session
149 int magic
; /* should be
150 * L2TP_SESSION_MAGIC */
151 int owner
; /* pid that opened the socket */
153 struct sock
*sock
; /* Pointer to the session
155 struct sock
*tunnel_sock
; /* Pointer to the tunnel UDP
158 struct pppol2tp_addr tunnel_addr
; /* Description of tunnel */
160 struct pppol2tp_tunnel
*tunnel
; /* back pointer to tunnel
163 char name
[20]; /* "sess xxxxx/yyyyy", where
164 * x=tunnel_id, y=session_id */
167 int flags
; /* accessed by PPPIOCGFLAGS.
169 unsigned recv_seq
:1; /* expect receive packets with
170 * sequence numbers? */
171 unsigned send_seq
:1; /* send packets with sequence
173 unsigned lns_mode
:1; /* behave as LNS? LAC enables
174 * sequence numbers under
176 int debug
; /* bitmask of debug message
178 int reorder_timeout
; /* configured reorder timeout
180 u16 nr
; /* session NR state (receive) */
181 u16 ns
; /* session NR state (send) */
182 struct sk_buff_head reorder_q
; /* receive reorder queue */
183 struct pppol2tp_ioc_stats stats
;
184 struct hlist_node hlist
; /* Hash list node */
187 /* The sk_user_data field of the tunnel's UDP socket. It contains info to track
188 * all the associated sessions so incoming packets can be sorted out
190 struct pppol2tp_tunnel
192 int magic
; /* Should be L2TP_TUNNEL_MAGIC */
193 rwlock_t hlist_lock
; /* protect session_hlist */
194 struct hlist_head session_hlist
[PPPOL2TP_HASH_SIZE
];
195 /* hashed list of sessions,
197 int debug
; /* bitmask of debug message
199 char name
[12]; /* "tunl xxxxx" */
200 struct pppol2tp_ioc_stats stats
;
202 void (*old_sk_destruct
)(struct sock
*);
204 struct sock
*sock
; /* Parent socket */
205 struct list_head list
; /* Keep a list of all open
206 * prepared sockets */
211 /* Private data stored for received packets in the skb.
213 struct pppol2tp_skb_cb
{
218 unsigned long expires
;
221 #define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
223 static int pppol2tp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
);
224 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel
*tunnel
);
226 static atomic_t pppol2tp_tunnel_count
;
227 static atomic_t pppol2tp_session_count
;
228 static struct ppp_channel_ops pppol2tp_chan_ops
= { pppol2tp_xmit
, NULL
};
229 static struct proto_ops pppol2tp_ops
;
230 static LIST_HEAD(pppol2tp_tunnel_list
);
231 static DEFINE_RWLOCK(pppol2tp_tunnel_list_lock
);
233 /* Helpers to obtain tunnel/session contexts from sockets.
235 static inline struct pppol2tp_session
*pppol2tp_sock_to_session(struct sock
*sk
)
237 struct pppol2tp_session
*session
;
243 session
= (struct pppol2tp_session
*)(sk
->sk_user_data
);
244 if (session
== NULL
) {
249 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
254 static inline struct pppol2tp_tunnel
*pppol2tp_sock_to_tunnel(struct sock
*sk
)
256 struct pppol2tp_tunnel
*tunnel
;
262 tunnel
= (struct pppol2tp_tunnel
*)(sk
->sk_user_data
);
263 if (tunnel
== NULL
) {
268 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
273 /* Tunnel reference counts. Incremented per session that is added to
276 static inline void pppol2tp_tunnel_inc_refcount(struct pppol2tp_tunnel
*tunnel
)
278 atomic_inc(&tunnel
->ref_count
);
281 static inline void pppol2tp_tunnel_dec_refcount(struct pppol2tp_tunnel
*tunnel
)
283 if (atomic_dec_and_test(&tunnel
->ref_count
))
284 pppol2tp_tunnel_free(tunnel
);
287 /* Session hash list.
288 * The session_id SHOULD be random according to RFC2661, but several
289 * L2TP implementations (Cisco and Microsoft) use incrementing
290 * session_ids. So we do a real hash on the session_id, rather than a
293 static inline struct hlist_head
*
294 pppol2tp_session_id_hash(struct pppol2tp_tunnel
*tunnel
, u16 session_id
)
296 unsigned long hash_val
= (unsigned long) session_id
;
297 return &tunnel
->session_hlist
[hash_long(hash_val
, PPPOL2TP_HASH_BITS
)];
300 /* Lookup a session by id
302 static struct pppol2tp_session
*
303 pppol2tp_session_find(struct pppol2tp_tunnel
*tunnel
, u16 session_id
)
305 struct hlist_head
*session_list
=
306 pppol2tp_session_id_hash(tunnel
, session_id
);
307 struct pppol2tp_session
*session
;
308 struct hlist_node
*walk
;
310 read_lock_bh(&tunnel
->hlist_lock
);
311 hlist_for_each_entry(session
, walk
, session_list
, hlist
) {
312 if (session
->tunnel_addr
.s_session
== session_id
) {
313 read_unlock_bh(&tunnel
->hlist_lock
);
317 read_unlock_bh(&tunnel
->hlist_lock
);
322 /* Lookup a tunnel by id
324 static struct pppol2tp_tunnel
*pppol2tp_tunnel_find(u16 tunnel_id
)
326 struct pppol2tp_tunnel
*tunnel
= NULL
;
328 read_lock_bh(&pppol2tp_tunnel_list_lock
);
329 list_for_each_entry(tunnel
, &pppol2tp_tunnel_list
, list
) {
330 if (tunnel
->stats
.tunnel_id
== tunnel_id
) {
331 read_unlock_bh(&pppol2tp_tunnel_list_lock
);
335 read_unlock_bh(&pppol2tp_tunnel_list_lock
);
340 /*****************************************************************************
341 * Receive data handling
342 *****************************************************************************/
344 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
347 static void pppol2tp_recv_queue_skb(struct pppol2tp_session
*session
, struct sk_buff
*skb
)
349 struct sk_buff
*skbp
;
351 u16 ns
= PPPOL2TP_SKB_CB(skb
)->ns
;
353 spin_lock_bh(&session
->reorder_q
.lock
);
354 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
355 if (PPPOL2TP_SKB_CB(skbp
)->ns
> ns
) {
356 __skb_insert(skb
, skbp
->prev
, skbp
, &session
->reorder_q
);
357 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
358 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
359 session
->name
, ns
, PPPOL2TP_SKB_CB(skbp
)->ns
,
360 skb_queue_len(&session
->reorder_q
));
361 session
->stats
.rx_oos_packets
++;
366 __skb_queue_tail(&session
->reorder_q
, skb
);
369 spin_unlock_bh(&session
->reorder_q
.lock
);
372 /* Dequeue a single skb.
374 static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session
*session
, struct sk_buff
*skb
)
376 struct pppol2tp_tunnel
*tunnel
= session
->tunnel
;
377 int length
= PPPOL2TP_SKB_CB(skb
)->length
;
378 struct sock
*session_sock
= NULL
;
380 /* We're about to requeue the skb, so return resources
381 * to its current owner (a socket receive buffer).
385 tunnel
->stats
.rx_packets
++;
386 tunnel
->stats
.rx_bytes
+= length
;
387 session
->stats
.rx_packets
++;
388 session
->stats
.rx_bytes
+= length
;
390 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
393 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
394 "%s: updated nr to %hu\n", session
->name
, session
->nr
);
397 /* If the socket is bound, send it in to PPP's input queue. Otherwise
398 * queue it on the session socket.
400 session_sock
= session
->sock
;
401 if (session_sock
->sk_state
& PPPOX_BOUND
) {
402 struct pppox_sock
*po
;
403 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
404 "%s: recv %d byte data frame, passing to ppp\n",
405 session
->name
, length
);
407 /* We need to forget all info related to the L2TP packet
408 * gathered in the skb as we are going to reuse the same
409 * skb for the inner packet.
411 * - reset xfrm (IPSec) information as it applies to
412 * the outer L2TP packet and not to the inner one
413 * - release the dst to force a route lookup on the inner
414 * IP packet since skb->dst currently points to the dst
416 * - reset netfilter information as it doesn't apply
417 * to the inner packet either
420 dst_release(skb
->dst
);
424 po
= pppox_sk(session_sock
);
425 ppp_input(&po
->chan
, skb
);
427 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
428 "%s: socket not bound\n", session
->name
);
430 /* Not bound. Nothing we can do, so discard. */
431 session
->stats
.rx_errors
++;
435 sock_put(session
->sock
);
438 /* Dequeue skbs from the session's reorder_q, subject to packet order.
439 * Skbs that have been in the queue for too long are simply discarded.
441 static void pppol2tp_recv_dequeue(struct pppol2tp_session
*session
)
446 /* If the pkt at the head of the queue has the nr that we
447 * expect to send up next, dequeue it and any other
448 * in-sequence packets behind it.
450 spin_lock_bh(&session
->reorder_q
.lock
);
451 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
452 if (time_after(jiffies
, PPPOL2TP_SKB_CB(skb
)->expires
)) {
453 session
->stats
.rx_seq_discards
++;
454 session
->stats
.rx_errors
++;
455 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
456 "%s: oos pkt %hu len %d discarded (too old), "
457 "waiting for %hu, reorder_q_len=%d\n",
458 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
459 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
460 skb_queue_len(&session
->reorder_q
));
461 __skb_unlink(skb
, &session
->reorder_q
);
463 sock_put(session
->sock
);
467 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
468 if (PPPOL2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
469 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
470 "%s: holding oos pkt %hu len %d, "
471 "waiting for %hu, reorder_q_len=%d\n",
472 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
473 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
474 skb_queue_len(&session
->reorder_q
));
478 __skb_unlink(skb
, &session
->reorder_q
);
480 /* Process the skb. We release the queue lock while we
481 * do so to let other contexts process the queue.
483 spin_unlock_bh(&session
->reorder_q
.lock
);
484 pppol2tp_recv_dequeue_skb(session
, skb
);
485 spin_lock_bh(&session
->reorder_q
.lock
);
489 spin_unlock_bh(&session
->reorder_q
.lock
);
492 /* Internal receive frame. Do the real work of receiving an L2TP data frame
493 * here. The skb is not on a list when we get here.
494 * Returns 0 if the packet was a data packet and was successfully passed on.
495 * Returns 1 if the packet was not a good data packet and could not be
496 * forwarded. All such packets are passed up to userspace to deal with.
498 static int pppol2tp_recv_core(struct sock
*sock
, struct sk_buff
*skb
)
500 struct pppol2tp_session
*session
= NULL
;
501 struct pppol2tp_tunnel
*tunnel
;
502 unsigned char *ptr
, *optr
;
504 u16 tunnel_id
, session_id
;
508 tunnel
= pppol2tp_sock_to_tunnel(sock
);
512 /* UDP always verifies the packet length. */
513 __skb_pull(skb
, sizeof(struct udphdr
));
516 if (!pskb_may_pull(skb
, 12)) {
517 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
518 "%s: recv short packet (len=%d)\n", tunnel
->name
, skb
->len
);
522 /* Point to L2TP header */
523 optr
= ptr
= skb
->data
;
525 /* Get L2TP header flags */
526 hdrflags
= ntohs(*(__be16
*)ptr
);
528 /* Trace packet contents, if enabled */
529 if (tunnel
->debug
& PPPOL2TP_MSG_DATA
) {
530 length
= min(16u, skb
->len
);
531 if (!pskb_may_pull(skb
, length
))
534 printk(KERN_DEBUG
"%s: recv: ", tunnel
->name
);
538 printk(" %02X", ptr
[offset
]);
539 } while (++offset
< length
);
544 /* Get length of L2TP packet */
547 /* If type is control packet, it is handled by userspace. */
548 if (hdrflags
& L2TP_HDRFLAG_T
) {
549 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
550 "%s: recv control packet, len=%d\n", tunnel
->name
, length
);
557 /* If length is present, skip it */
558 if (hdrflags
& L2TP_HDRFLAG_L
)
561 /* Extract tunnel and session ID */
562 tunnel_id
= ntohs(*(__be16
*) ptr
);
564 session_id
= ntohs(*(__be16
*) ptr
);
567 /* Find the session context */
568 session
= pppol2tp_session_find(tunnel
, session_id
);
570 /* Not found? Pass to userspace to deal with */
571 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
572 "%s: no socket found (%hu/%hu). Passing up.\n",
573 tunnel
->name
, tunnel_id
, session_id
);
576 sock_hold(session
->sock
);
578 /* The ref count on the socket was increased by the above call since
579 * we now hold a pointer to the session. Take care to do sock_put()
580 * when exiting this function from now on...
583 /* Handle the optional sequence numbers. If we are the LAC,
584 * enable/disable sequence numbers under the control of the LNS. If
585 * no sequence numbers present but we were expecting them, discard
588 if (hdrflags
& L2TP_HDRFLAG_S
) {
590 ns
= ntohs(*(__be16
*) ptr
);
592 nr
= ntohs(*(__be16
*) ptr
);
595 /* Received a packet with sequence numbers. If we're the LNS,
596 * check if we sre sending sequence numbers and if not,
599 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
600 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_INFO
,
601 "%s: requested to enable seq numbers by LNS\n",
603 session
->send_seq
= -1;
606 /* Store L2TP info in the skb */
607 PPPOL2TP_SKB_CB(skb
)->ns
= ns
;
608 PPPOL2TP_SKB_CB(skb
)->nr
= nr
;
609 PPPOL2TP_SKB_CB(skb
)->has_seq
= 1;
611 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
612 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
613 session
->name
, ns
, nr
, session
->nr
);
615 /* No sequence numbers.
616 * If user has configured mandatory sequence numbers, discard.
618 if (session
->recv_seq
) {
619 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_WARNING
,
620 "%s: recv data has no seq numbers when required. "
621 "Discarding\n", session
->name
);
622 session
->stats
.rx_seq_discards
++;
626 /* If we're the LAC and we're sending sequence numbers, the
627 * LNS has requested that we no longer send sequence numbers.
628 * If we're the LNS and we're sending sequence numbers, the
629 * LAC is broken. Discard the frame.
631 if ((!session
->lns_mode
) && (session
->send_seq
)) {
632 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_INFO
,
633 "%s: requested to disable seq numbers by LNS\n",
635 session
->send_seq
= 0;
636 } else if (session
->send_seq
) {
637 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_WARNING
,
638 "%s: recv data has no seq numbers when required. "
639 "Discarding\n", session
->name
);
640 session
->stats
.rx_seq_discards
++;
644 /* Store L2TP info in the skb */
645 PPPOL2TP_SKB_CB(skb
)->has_seq
= 0;
648 /* If offset bit set, skip it. */
649 if (hdrflags
& L2TP_HDRFLAG_O
) {
650 offset
= ntohs(*(__be16
*)ptr
);
655 if (!pskb_may_pull(skb
, offset
))
658 __skb_pull(skb
, offset
);
660 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
661 * don't send the PPP header (PPP header compression enabled), but
662 * other clients can include the header. So we cope with both cases
663 * here. The PPP header is always FF03 when using L2TP.
665 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
666 * the field may be unaligned.
668 if (!pskb_may_pull(skb
, 2))
671 if ((skb
->data
[0] == 0xff) && (skb
->data
[1] == 0x03))
674 /* Prepare skb for adding to the session's reorder_q. Hold
675 * packets for max reorder_timeout or 1 second if not
678 PPPOL2TP_SKB_CB(skb
)->length
= length
;
679 PPPOL2TP_SKB_CB(skb
)->expires
= jiffies
+
680 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
682 /* Add packet to the session's receive queue. Reordering is done here, if
683 * enabled. Saved L2TP protocol info is stored in skb->sb[].
685 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
686 if (session
->reorder_timeout
!= 0) {
687 /* Packet reordering enabled. Add skb to session's
688 * reorder queue, in order of ns.
690 pppol2tp_recv_queue_skb(session
, skb
);
692 /* Packet reordering disabled. Discard out-of-sequence
695 if (PPPOL2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
696 session
->stats
.rx_seq_discards
++;
697 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
698 "%s: oos pkt %hu len %d discarded, "
699 "waiting for %hu, reorder_q_len=%d\n",
700 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
701 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
702 skb_queue_len(&session
->reorder_q
));
705 skb_queue_tail(&session
->reorder_q
, skb
);
708 /* No sequence numbers. Add the skb to the tail of the
709 * reorder queue. This ensures that it will be
710 * delivered after all previous sequenced skbs.
712 skb_queue_tail(&session
->reorder_q
, skb
);
715 /* Try to dequeue as many skbs from reorder_q as we can. */
716 pppol2tp_recv_dequeue(session
);
721 session
->stats
.rx_errors
++;
723 sock_put(session
->sock
);
729 /* Put UDP header back */
730 __skb_push(skb
, sizeof(struct udphdr
));
737 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
741 * >0: skb should be passed up to userspace as UDP.
743 static int pppol2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
745 struct pppol2tp_tunnel
*tunnel
;
747 tunnel
= pppol2tp_sock_to_tunnel(sk
);
751 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
752 "%s: received %d bytes\n", tunnel
->name
, skb
->len
);
754 if (pppol2tp_recv_core(sk
, skb
))
766 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
768 static int pppol2tp_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
769 struct msghdr
*msg
, size_t len
,
774 struct sock
*sk
= sock
->sk
;
777 if (sk
->sk_state
& PPPOX_BOUND
)
780 msg
->msg_namelen
= 0;
783 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
784 flags
& MSG_DONTWAIT
, &err
);
790 else if (len
< skb
->len
)
791 msg
->msg_flags
|= MSG_TRUNC
;
793 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, len
);
794 if (likely(err
== 0))
802 /************************************************************************
804 ***********************************************************************/
806 /* Tell how big L2TP headers are for a particular session. This
807 * depends on whether sequence numbers are being used.
809 static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session
*session
)
811 if (session
->send_seq
)
812 return PPPOL2TP_L2TP_HDR_SIZE_SEQ
;
814 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
817 /* Build an L2TP header for the session into the buffer provided.
819 static void pppol2tp_build_l2tp_header(struct pppol2tp_session
*session
,
823 u16 flags
= L2TP_HDR_VER
;
825 if (session
->send_seq
)
826 flags
|= L2TP_HDRFLAG_S
;
828 /* Setup L2TP header.
829 * FIXME: Can this ever be unaligned? Is direct dereferencing of
830 * 16-bit header fields safe here for all architectures?
832 *bufp
++ = htons(flags
);
833 *bufp
++ = htons(session
->tunnel_addr
.d_tunnel
);
834 *bufp
++ = htons(session
->tunnel_addr
.d_session
);
835 if (session
->send_seq
) {
836 *bufp
++ = htons(session
->ns
);
839 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
840 "%s: updated ns to %hu\n", session
->name
, session
->ns
);
844 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
845 * when a user application does a sendmsg() on the session socket. L2TP and
846 * PPP headers must be inserted into the user's data.
848 static int pppol2tp_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*m
,
851 static const unsigned char ppph
[2] = { 0xff, 0x03 };
852 struct sock
*sk
= sock
->sk
;
853 struct inet_sock
*inet
;
858 struct pppol2tp_session
*session
;
859 struct pppol2tp_tunnel
*tunnel
;
864 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
))
867 /* Get session and tunnel contexts */
869 session
= pppol2tp_sock_to_session(sk
);
873 tunnel
= pppol2tp_sock_to_tunnel(session
->tunnel_sock
);
877 /* What header length is configured for this session? */
878 hdr_len
= pppol2tp_l2tp_header_len(session
);
880 /* Allocate a socket buffer */
882 skb
= sock_wmalloc(sk
, NET_SKB_PAD
+ sizeof(struct iphdr
) +
883 sizeof(struct udphdr
) + hdr_len
+
884 sizeof(ppph
) + total_len
,
887 goto error_put_sess_tun
;
889 /* Reserve space for headers. */
890 skb_reserve(skb
, NET_SKB_PAD
);
891 skb_reset_network_header(skb
);
892 skb_reserve(skb
, sizeof(struct iphdr
));
893 skb_reset_transport_header(skb
);
895 /* Build UDP header */
896 inet
= inet_sk(session
->tunnel_sock
);
897 uh
= (struct udphdr
*) skb
->data
;
898 uh
->source
= inet
->sport
;
899 uh
->dest
= inet
->dport
;
900 uh
->len
= htons(hdr_len
+ sizeof(ppph
) + total_len
);
902 skb_put(skb
, sizeof(struct udphdr
));
904 /* Build L2TP header */
905 pppol2tp_build_l2tp_header(session
, skb
->data
);
906 skb_put(skb
, hdr_len
);
909 skb
->data
[0] = ppph
[0];
910 skb
->data
[1] = ppph
[1];
913 /* Copy user data into skb */
914 error
= memcpy_fromiovec(skb
->data
, m
->msg_iov
, total_len
);
917 goto error_put_sess_tun
;
919 skb_put(skb
, total_len
);
921 /* Calculate UDP checksum if configured to do so */
922 if (session
->tunnel_sock
->sk_no_check
!= UDP_CSUM_NOXMIT
)
923 csum
= udp_csum_outgoing(sk
, skb
);
926 if (session
->send_seq
)
927 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
928 "%s: send %Zd bytes, ns=%hu\n", session
->name
,
929 total_len
, session
->ns
- 1);
931 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
932 "%s: send %Zd bytes\n", session
->name
, total_len
);
934 if (session
->debug
& PPPOL2TP_MSG_DATA
) {
936 unsigned char *datap
= skb
->data
;
938 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
939 for (i
= 0; i
< total_len
; i
++) {
940 printk(" %02X", *datap
++);
949 /* Queue the packet to IP for output */
951 error
= ip_queue_xmit(skb
, 1);
955 tunnel
->stats
.tx_packets
++;
956 tunnel
->stats
.tx_bytes
+= len
;
957 session
->stats
.tx_packets
++;
958 session
->stats
.tx_bytes
+= len
;
960 tunnel
->stats
.tx_errors
++;
961 session
->stats
.tx_errors
++;
967 sock_put(session
->tunnel_sock
);
974 /* Automatically called when the skb is freed.
976 static void pppol2tp_sock_wfree(struct sk_buff
*skb
)
981 /* For data skbs that we transmit, we associate with the tunnel socket
982 * but don't do accounting.
984 static inline void pppol2tp_skb_set_owner_w(struct sk_buff
*skb
, struct sock
*sk
)
988 skb
->destructor
= pppol2tp_sock_wfree
;
991 /* Transmit function called by generic PPP driver. Sends PPP frame
992 * over PPPoL2TP socket.
994 * This is almost the same as pppol2tp_sendmsg(), but rather than
995 * being called with a msghdr from userspace, it is called with a skb
998 * The supplied skb from ppp doesn't have enough headroom for the
999 * insertion of L2TP, UDP and IP headers so we need to allocate more
1000 * headroom in the skb. This will create a cloned skb. But we must be
1001 * careful in the error case because the caller will expect to free
1002 * the skb it supplied, not our cloned skb. So we take care to always
1003 * leave the original skb unfreed if we return an error.
1005 static int pppol2tp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1007 static const u8 ppph
[2] = { 0xff, 0x03 };
1008 struct sock
*sk
= (struct sock
*) chan
->private;
1009 struct sock
*sk_tun
;
1011 struct pppol2tp_session
*session
;
1012 struct pppol2tp_tunnel
*tunnel
;
1015 int data_len
= skb
->len
;
1016 struct inet_sock
*inet
;
1023 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
))
1026 /* Get session and tunnel contexts from the socket */
1027 session
= pppol2tp_sock_to_session(sk
);
1028 if (session
== NULL
)
1031 sk_tun
= session
->tunnel_sock
;
1033 goto abort_put_sess
;
1034 tunnel
= pppol2tp_sock_to_tunnel(sk_tun
);
1036 goto abort_put_sess
;
1038 /* What header length is configured for this session? */
1039 hdr_len
= pppol2tp_l2tp_header_len(session
);
1041 /* Check that there's enough headroom in the skb to insert IP,
1042 * UDP and L2TP and PPP headers. If not enough, expand it to
1043 * make room. Adjust truesize.
1045 headroom
= NET_SKB_PAD
+ sizeof(struct iphdr
) +
1046 sizeof(struct udphdr
) + hdr_len
+ sizeof(ppph
);
1047 old_headroom
= skb_headroom(skb
);
1048 if (skb_cow_head(skb
, headroom
))
1049 goto abort_put_sess_tun
;
1051 new_headroom
= skb_headroom(skb
);
1053 skb
->truesize
+= new_headroom
- old_headroom
;
1055 /* Setup PPP header */
1056 __skb_push(skb
, sizeof(ppph
));
1057 skb
->data
[0] = ppph
[0];
1058 skb
->data
[1] = ppph
[1];
1060 /* Setup L2TP header */
1061 pppol2tp_build_l2tp_header(session
, __skb_push(skb
, hdr_len
));
1063 /* Setup UDP header */
1064 inet
= inet_sk(sk_tun
);
1065 __skb_push(skb
, sizeof(*uh
));
1066 skb_reset_transport_header(skb
);
1068 uh
->source
= inet
->sport
;
1069 uh
->dest
= inet
->dport
;
1070 uh
->len
= htons(sizeof(struct udphdr
) + hdr_len
+ sizeof(ppph
) + data_len
);
1073 /* *BROKEN* Calculate UDP checksum if configured to do so */
1074 if (sk_tun
->sk_no_check
!= UDP_CSUM_NOXMIT
)
1075 csum
= udp_csum_outgoing(sk_tun
, skb
);
1078 if (session
->send_seq
)
1079 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1080 "%s: send %d bytes, ns=%hu\n", session
->name
,
1081 data_len
, session
->ns
- 1);
1083 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1084 "%s: send %d bytes\n", session
->name
, data_len
);
1086 if (session
->debug
& PPPOL2TP_MSG_DATA
) {
1088 unsigned char *datap
= skb
->data
;
1090 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
1091 for (i
= 0; i
< data_len
; i
++) {
1092 printk(" %02X", *datap
++);
1101 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1102 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1106 /* Get routing info from the tunnel socket */
1107 dst_release(skb
->dst
);
1108 skb
->dst
= dst_clone(__sk_dst_get(sk_tun
));
1109 pppol2tp_skb_set_owner_w(skb
, sk_tun
);
1111 /* Queue the packet to IP for output */
1113 rc
= ip_queue_xmit(skb
, 1);
1117 tunnel
->stats
.tx_packets
++;
1118 tunnel
->stats
.tx_bytes
+= len
;
1119 session
->stats
.tx_packets
++;
1120 session
->stats
.tx_bytes
+= len
;
1122 tunnel
->stats
.tx_errors
++;
1123 session
->stats
.tx_errors
++;
1135 /* Free the original skb */
1140 /*****************************************************************************
1141 * Session (and tunnel control) socket create/destroy.
1142 *****************************************************************************/
1144 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1147 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel
*tunnel
)
1150 struct hlist_node
*walk
;
1151 struct hlist_node
*tmp
;
1152 struct pppol2tp_session
*session
;
1158 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1159 "%s: closing all sessions...\n", tunnel
->name
);
1161 write_lock_bh(&tunnel
->hlist_lock
);
1162 for (hash
= 0; hash
< PPPOL2TP_HASH_SIZE
; hash
++) {
1164 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1165 struct sk_buff
*skb
;
1167 session
= hlist_entry(walk
, struct pppol2tp_session
, hlist
);
1171 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1172 "%s: closing session\n", session
->name
);
1174 hlist_del_init(&session
->hlist
);
1176 /* Since we should hold the sock lock while
1177 * doing any unbinding, we need to release the
1178 * lock we're holding before taking that lock.
1179 * Hold a reference to the sock so it doesn't
1180 * disappear as we're jumping between locks.
1183 write_unlock_bh(&tunnel
->hlist_lock
);
1186 if (sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
)) {
1187 pppox_unbind_sock(sk
);
1188 sk
->sk_state
= PPPOX_DEAD
;
1189 sk
->sk_state_change(sk
);
1192 /* Purge any queued data */
1193 skb_queue_purge(&sk
->sk_receive_queue
);
1194 skb_queue_purge(&sk
->sk_write_queue
);
1195 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
1203 /* Now restart from the beginning of this hash
1204 * chain. We always remove a session from the
1205 * list so we are guaranteed to make forward
1208 write_lock_bh(&tunnel
->hlist_lock
);
1212 write_unlock_bh(&tunnel
->hlist_lock
);
1215 /* Really kill the tunnel.
1216 * Come here only when all sessions have been cleared from the tunnel.
1218 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel
*tunnel
)
1220 /* Remove from socket list */
1221 write_lock_bh(&pppol2tp_tunnel_list_lock
);
1222 list_del_init(&tunnel
->list
);
1223 write_unlock_bh(&pppol2tp_tunnel_list_lock
);
1225 atomic_dec(&pppol2tp_tunnel_count
);
1229 /* Tunnel UDP socket destruct hook.
1230 * The tunnel context is deleted only when all session sockets have been
1233 static void pppol2tp_tunnel_destruct(struct sock
*sk
)
1235 struct pppol2tp_tunnel
*tunnel
;
1237 tunnel
= sk
->sk_user_data
;
1241 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1242 "%s: closing...\n", tunnel
->name
);
1244 /* Close all sessions */
1245 pppol2tp_tunnel_closeall(tunnel
);
1247 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1248 (udp_sk(sk
))->encap_type
= 0;
1249 (udp_sk(sk
))->encap_rcv
= NULL
;
1251 /* Remove hooks into tunnel socket */
1252 tunnel
->sock
= NULL
;
1253 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1254 sk
->sk_user_data
= NULL
;
1256 /* Call original (UDP) socket descructor */
1257 if (sk
->sk_destruct
!= NULL
)
1258 (*sk
->sk_destruct
)(sk
);
1260 pppol2tp_tunnel_dec_refcount(tunnel
);
1266 /* Really kill the session socket. (Called from sock_put() if
1269 static void pppol2tp_session_destruct(struct sock
*sk
)
1271 struct pppol2tp_session
*session
= NULL
;
1273 if (sk
->sk_user_data
!= NULL
) {
1274 struct pppol2tp_tunnel
*tunnel
;
1276 session
= sk
->sk_user_data
;
1277 if (session
== NULL
)
1280 BUG_ON(session
->magic
!= L2TP_SESSION_MAGIC
);
1282 /* Don't use pppol2tp_sock_to_tunnel() here to
1283 * get the tunnel context because the tunnel
1284 * socket might have already been closed (its
1285 * sk->sk_user_data will be NULL) so use the
1286 * session's private tunnel ptr instead.
1288 tunnel
= session
->tunnel
;
1289 if (tunnel
!= NULL
) {
1290 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1292 /* If session_id is zero, this is a null
1293 * session context, which was created for a
1294 * socket that is being used only to manage
1297 if (session
->tunnel_addr
.s_session
!= 0) {
1298 /* Delete the session socket from the
1301 write_lock_bh(&tunnel
->hlist_lock
);
1302 hlist_del_init(&session
->hlist
);
1303 write_unlock_bh(&tunnel
->hlist_lock
);
1305 atomic_dec(&pppol2tp_session_count
);
1308 /* This will delete the tunnel context if this
1309 * is the last session on the tunnel.
1311 session
->tunnel
= NULL
;
1312 session
->tunnel_sock
= NULL
;
1313 pppol2tp_tunnel_dec_refcount(tunnel
);
1322 /* Called when the PPPoX socket (session) is closed.
1324 static int pppol2tp_release(struct socket
*sock
)
1326 struct sock
*sk
= sock
->sk
;
1327 struct pppol2tp_session
*session
;
1335 if (sock_flag(sk
, SOCK_DEAD
) != 0)
1338 pppox_unbind_sock(sk
);
1340 /* Signal the death of the socket. */
1341 sk
->sk_state
= PPPOX_DEAD
;
1345 session
= pppol2tp_sock_to_session(sk
);
1347 /* Purge any queued data */
1348 skb_queue_purge(&sk
->sk_receive_queue
);
1349 skb_queue_purge(&sk
->sk_write_queue
);
1350 if (session
!= NULL
) {
1351 struct sk_buff
*skb
;
1352 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
1361 /* This will delete the session context via
1362 * pppol2tp_session_destruct() if the socket's refcnt drops to
1374 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1375 * sockets attached to it.
1377 static struct sock
*pppol2tp_prepare_tunnel_socket(int fd
, u16 tunnel_id
,
1381 struct socket
*sock
= NULL
;
1383 struct pppol2tp_tunnel
*tunnel
;
1384 struct sock
*ret
= NULL
;
1386 /* Get the tunnel UDP socket from the fd, which was opened by
1387 * the userspace L2TP daemon.
1390 sock
= sockfd_lookup(fd
, &err
);
1392 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1393 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1394 tunnel_id
, fd
, err
);
1400 /* Quick sanity checks */
1401 err
= -EPROTONOSUPPORT
;
1402 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1403 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1404 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1405 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1408 err
= -EAFNOSUPPORT
;
1409 if (sock
->ops
->family
!= AF_INET
) {
1410 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1411 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1412 tunnel_id
, fd
, sock
->ops
->family
, AF_INET
);
1418 /* Check if this socket has already been prepped */
1419 tunnel
= (struct pppol2tp_tunnel
*)sk
->sk_user_data
;
1420 if (tunnel
!= NULL
) {
1421 /* User-data field already set */
1423 BUG_ON(tunnel
->magic
!= L2TP_TUNNEL_MAGIC
);
1425 /* This socket has already been prepped */
1430 /* This socket is available and needs prepping. Create a new tunnel
1431 * context and init it.
1433 sk
->sk_user_data
= tunnel
= kzalloc(sizeof(struct pppol2tp_tunnel
), GFP_KERNEL
);
1434 if (sk
->sk_user_data
== NULL
) {
1439 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1440 sprintf(&tunnel
->name
[0], "tunl %hu", tunnel_id
);
1442 tunnel
->stats
.tunnel_id
= tunnel_id
;
1443 tunnel
->debug
= PPPOL2TP_DEFAULT_DEBUG_FLAGS
;
1445 /* Hook on the tunnel socket destructor so that we can cleanup
1446 * if the tunnel socket goes away.
1448 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1449 sk
->sk_destruct
= &pppol2tp_tunnel_destruct
;
1452 sk
->sk_allocation
= GFP_ATOMIC
;
1455 rwlock_init(&tunnel
->hlist_lock
);
1457 /* Add tunnel to our list */
1458 INIT_LIST_HEAD(&tunnel
->list
);
1459 write_lock_bh(&pppol2tp_tunnel_list_lock
);
1460 list_add(&tunnel
->list
, &pppol2tp_tunnel_list
);
1461 write_unlock_bh(&pppol2tp_tunnel_list_lock
);
1462 atomic_inc(&pppol2tp_tunnel_count
);
1464 /* Bump the reference count. The tunnel context is deleted
1465 * only when this drops to zero.
1467 pppol2tp_tunnel_inc_refcount(tunnel
);
1469 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1470 (udp_sk(sk
))->encap_type
= UDP_ENCAP_L2TPINUDP
;
1471 (udp_sk(sk
))->encap_rcv
= pppol2tp_udp_encap_recv
;
1487 static struct proto pppol2tp_sk_proto
= {
1489 .owner
= THIS_MODULE
,
1490 .obj_size
= sizeof(struct pppox_sock
),
1493 /* socket() handler. Initialize a new struct sock.
1495 static int pppol2tp_create(struct net
*net
, struct socket
*sock
)
1497 int error
= -ENOMEM
;
1500 sk
= sk_alloc(net
, PF_PPPOX
, GFP_KERNEL
, &pppol2tp_sk_proto
);
1504 sock_init_data(sock
, sk
);
1506 sock
->state
= SS_UNCONNECTED
;
1507 sock
->ops
= &pppol2tp_ops
;
1509 sk
->sk_backlog_rcv
= pppol2tp_recv_core
;
1510 sk
->sk_protocol
= PX_PROTO_OL2TP
;
1511 sk
->sk_family
= PF_PPPOX
;
1512 sk
->sk_state
= PPPOX_NONE
;
1513 sk
->sk_type
= SOCK_STREAM
;
1514 sk
->sk_destruct
= pppol2tp_session_destruct
;
1522 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1524 static int pppol2tp_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
1525 int sockaddr_len
, int flags
)
1527 struct sock
*sk
= sock
->sk
;
1528 struct sockaddr_pppol2tp
*sp
= (struct sockaddr_pppol2tp
*) uservaddr
;
1529 struct pppox_sock
*po
= pppox_sk(sk
);
1530 struct sock
*tunnel_sock
= NULL
;
1531 struct pppol2tp_session
*session
= NULL
;
1532 struct pppol2tp_tunnel
*tunnel
;
1533 struct dst_entry
*dst
;
1539 if (sp
->sa_protocol
!= PX_PROTO_OL2TP
)
1542 /* Check for already bound sockets */
1544 if (sk
->sk_state
& PPPOX_CONNECTED
)
1547 /* We don't supporting rebinding anyway */
1549 if (sk
->sk_user_data
)
1550 goto end
; /* socket is already attached */
1552 /* Don't bind if s_tunnel is 0 */
1554 if (sp
->pppol2tp
.s_tunnel
== 0)
1557 /* Special case: prepare tunnel socket if s_session and
1558 * d_session is 0. Otherwise look up tunnel using supplied
1561 if ((sp
->pppol2tp
.s_session
== 0) && (sp
->pppol2tp
.d_session
== 0)) {
1562 tunnel_sock
= pppol2tp_prepare_tunnel_socket(sp
->pppol2tp
.fd
,
1563 sp
->pppol2tp
.s_tunnel
,
1565 if (tunnel_sock
== NULL
)
1568 tunnel
= tunnel_sock
->sk_user_data
;
1570 tunnel
= pppol2tp_tunnel_find(sp
->pppol2tp
.s_tunnel
);
1572 /* Error if we can't find the tunnel */
1577 tunnel_sock
= tunnel
->sock
;
1580 /* Check that this session doesn't already exist */
1582 session
= pppol2tp_session_find(tunnel
, sp
->pppol2tp
.s_session
);
1583 if (session
!= NULL
)
1586 /* Allocate and initialize a new session context. */
1587 session
= kzalloc(sizeof(struct pppol2tp_session
), GFP_KERNEL
);
1588 if (session
== NULL
) {
1593 skb_queue_head_init(&session
->reorder_q
);
1595 session
->magic
= L2TP_SESSION_MAGIC
;
1596 session
->owner
= current
->pid
;
1598 session
->tunnel
= tunnel
;
1599 session
->tunnel_sock
= tunnel_sock
;
1600 session
->tunnel_addr
= sp
->pppol2tp
;
1601 sprintf(&session
->name
[0], "sess %hu/%hu",
1602 session
->tunnel_addr
.s_tunnel
,
1603 session
->tunnel_addr
.s_session
);
1605 session
->stats
.tunnel_id
= session
->tunnel_addr
.s_tunnel
;
1606 session
->stats
.session_id
= session
->tunnel_addr
.s_session
;
1608 INIT_HLIST_NODE(&session
->hlist
);
1610 /* Inherit debug options from tunnel */
1611 session
->debug
= tunnel
->debug
;
1613 /* Default MTU must allow space for UDP/L2TP/PPP
1616 session
->mtu
= session
->mru
= 1500 - PPPOL2TP_HEADER_OVERHEAD
;
1618 /* If PMTU discovery was enabled, use the MTU that was discovered */
1619 dst
= sk_dst_get(sk
);
1621 u32 pmtu
= dst_mtu(__sk_dst_get(sk
));
1623 session
->mtu
= session
->mru
= pmtu
-
1624 PPPOL2TP_HEADER_OVERHEAD
;
1628 /* Special case: if source & dest session_id == 0x0000, this socket is
1629 * being created to manage the tunnel. Don't add the session to the
1630 * session hash list, just set up the internal context for use by
1631 * ioctl() and sockopt() handlers.
1633 if ((session
->tunnel_addr
.s_session
== 0) &&
1634 (session
->tunnel_addr
.d_session
== 0)) {
1636 sk
->sk_user_data
= session
;
1640 /* Get tunnel context from the tunnel socket */
1641 tunnel
= pppol2tp_sock_to_tunnel(tunnel_sock
);
1642 if (tunnel
== NULL
) {
1647 /* Right now, because we don't have a way to push the incoming skb's
1648 * straight through the UDP layer, the only header we need to worry
1649 * about is the L2TP header. This size is different depending on
1650 * whether sequence numbers are enabled for the data channel.
1652 po
->chan
.hdrlen
= PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
1654 po
->chan
.private = sk
;
1655 po
->chan
.ops
= &pppol2tp_chan_ops
;
1656 po
->chan
.mtu
= session
->mtu
;
1658 error
= ppp_register_channel(&po
->chan
);
1662 /* This is how we get the session context from the socket. */
1663 sk
->sk_user_data
= session
;
1665 /* Add session to the tunnel's hash list */
1666 write_lock_bh(&tunnel
->hlist_lock
);
1667 hlist_add_head(&session
->hlist
,
1668 pppol2tp_session_id_hash(tunnel
,
1669 session
->tunnel_addr
.s_session
));
1670 write_unlock_bh(&tunnel
->hlist_lock
);
1672 atomic_inc(&pppol2tp_session_count
);
1675 pppol2tp_tunnel_inc_refcount(tunnel
);
1676 sk
->sk_state
= PPPOX_CONNECTED
;
1677 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1678 "%s: created\n", session
->name
);
1681 sock_put(tunnel_sock
);
1687 PRINTK(session
->debug
,
1688 PPPOL2TP_MSG_CONTROL
, KERN_WARNING
,
1689 "%s: connect failed: %d\n",
1690 session
->name
, error
);
1692 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_WARNING
,
1693 "connect failed: %d\n", error
);
1699 /* getname() support.
1701 static int pppol2tp_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1702 int *usockaddr_len
, int peer
)
1704 int len
= sizeof(struct sockaddr_pppol2tp
);
1705 struct sockaddr_pppol2tp sp
;
1707 struct pppol2tp_session
*session
;
1710 if (sock
->sk
->sk_state
!= PPPOX_CONNECTED
)
1713 session
= pppol2tp_sock_to_session(sock
->sk
);
1714 if (session
== NULL
) {
1719 sp
.sa_family
= AF_PPPOX
;
1720 sp
.sa_protocol
= PX_PROTO_OL2TP
;
1721 memcpy(&sp
.pppol2tp
, &session
->tunnel_addr
,
1722 sizeof(struct pppol2tp_addr
));
1724 memcpy(uaddr
, &sp
, len
);
1726 *usockaddr_len
= len
;
1735 /****************************************************************************
1738 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1739 * sockets. However, in order to control kernel tunnel features, we allow
1740 * userspace to create a special "tunnel" PPPoX socket which is used for
1741 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1742 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1744 ****************************************************************************/
1746 /* Session ioctl helper.
1748 static int pppol2tp_session_ioctl(struct pppol2tp_session
*session
,
1749 unsigned int cmd
, unsigned long arg
)
1753 struct sock
*sk
= session
->sock
;
1754 int val
= (int) arg
;
1756 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_DEBUG
,
1757 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1758 session
->name
, cmd
, arg
);
1765 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1769 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1771 ifr
.ifr_mtu
= session
->mtu
;
1772 if (copy_to_user((void __user
*) arg
, &ifr
, sizeof(struct ifreq
)))
1775 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1776 "%s: get mtu=%d\n", session
->name
, session
->mtu
);
1782 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1786 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1789 session
->mtu
= ifr
.ifr_mtu
;
1791 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1792 "%s: set mtu=%d\n", session
->name
, session
->mtu
);
1798 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1802 if (put_user(session
->mru
, (int __user
*) arg
))
1805 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1806 "%s: get mru=%d\n", session
->name
, session
->mru
);
1812 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1816 if (get_user(val
,(int __user
*) arg
))
1820 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1821 "%s: set mru=%d\n", session
->name
, session
->mru
);
1827 if (put_user(session
->flags
, (int __user
*) arg
))
1830 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1831 "%s: get flags=%d\n", session
->name
, session
->flags
);
1837 if (get_user(val
, (int __user
*) arg
))
1839 session
->flags
= val
;
1840 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1841 "%s: set flags=%d\n", session
->name
, session
->flags
);
1845 case PPPIOCGL2TPSTATS
:
1847 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1850 if (copy_to_user((void __user
*) arg
, &session
->stats
,
1851 sizeof(session
->stats
)))
1853 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1854 "%s: get L2TP stats\n", session
->name
);
1868 /* Tunnel ioctl helper.
1870 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1871 * specifies a session_id, the session ioctl handler is called. This allows an
1872 * application to retrieve session stats via a tunnel socket.
1874 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel
*tunnel
,
1875 unsigned int cmd
, unsigned long arg
)
1878 struct sock
*sk
= tunnel
->sock
;
1879 struct pppol2tp_ioc_stats stats_req
;
1881 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_DEBUG
,
1882 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel
->name
,
1888 case PPPIOCGL2TPSTATS
:
1890 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1893 if (copy_from_user(&stats_req
, (void __user
*) arg
,
1894 sizeof(stats_req
))) {
1898 if (stats_req
.session_id
!= 0) {
1899 /* resend to session ioctl handler */
1900 struct pppol2tp_session
*session
=
1901 pppol2tp_session_find(tunnel
, stats_req
.session_id
);
1902 if (session
!= NULL
)
1903 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
1909 tunnel
->stats
.using_ipsec
= (sk
->sk_policy
[0] || sk
->sk_policy
[1]) ? 1 : 0;
1911 if (copy_to_user((void __user
*) arg
, &tunnel
->stats
,
1912 sizeof(tunnel
->stats
))) {
1916 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1917 "%s: get L2TP stats\n", tunnel
->name
);
1931 /* Main ioctl() handler.
1932 * Dispatch to tunnel or session helpers depending on the socket.
1934 static int pppol2tp_ioctl(struct socket
*sock
, unsigned int cmd
,
1937 struct sock
*sk
= sock
->sk
;
1938 struct pppol2tp_session
*session
;
1939 struct pppol2tp_tunnel
*tunnel
;
1946 if (sock_flag(sk
, SOCK_DEAD
) != 0)
1950 if ((sk
->sk_user_data
== NULL
) ||
1951 (!(sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
))))
1954 /* Get session context from the socket */
1956 session
= pppol2tp_sock_to_session(sk
);
1957 if (session
== NULL
)
1960 /* Special case: if session's session_id is zero, treat ioctl as a
1963 if ((session
->tunnel_addr
.s_session
== 0) &&
1964 (session
->tunnel_addr
.d_session
== 0)) {
1966 tunnel
= pppol2tp_sock_to_tunnel(session
->tunnel_sock
);
1970 err
= pppol2tp_tunnel_ioctl(tunnel
, cmd
, arg
);
1971 sock_put(session
->tunnel_sock
);
1975 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
1983 /*****************************************************************************
1984 * setsockopt() / getsockopt() support.
1986 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1987 * sockets. In order to control kernel tunnel features, we allow userspace to
1988 * create a special "tunnel" PPPoX socket which is used for control only.
1989 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1990 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1991 *****************************************************************************/
1993 /* Tunnel setsockopt() helper.
1995 static int pppol2tp_tunnel_setsockopt(struct sock
*sk
,
1996 struct pppol2tp_tunnel
*tunnel
,
1997 int optname
, int val
)
2002 case PPPOL2TP_SO_DEBUG
:
2003 tunnel
->debug
= val
;
2004 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2005 "%s: set debug=%x\n", tunnel
->name
, tunnel
->debug
);
2016 /* Session setsockopt helper.
2018 static int pppol2tp_session_setsockopt(struct sock
*sk
,
2019 struct pppol2tp_session
*session
,
2020 int optname
, int val
)
2025 case PPPOL2TP_SO_RECVSEQ
:
2026 if ((val
!= 0) && (val
!= 1)) {
2030 session
->recv_seq
= val
? -1 : 0;
2031 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2032 "%s: set recv_seq=%d\n", session
->name
,
2036 case PPPOL2TP_SO_SENDSEQ
:
2037 if ((val
!= 0) && (val
!= 1)) {
2041 session
->send_seq
= val
? -1 : 0;
2043 struct sock
*ssk
= session
->sock
;
2044 struct pppox_sock
*po
= pppox_sk(ssk
);
2045 po
->chan
.hdrlen
= val
? PPPOL2TP_L2TP_HDR_SIZE_SEQ
:
2046 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
2048 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2049 "%s: set send_seq=%d\n", session
->name
, session
->send_seq
);
2052 case PPPOL2TP_SO_LNSMODE
:
2053 if ((val
!= 0) && (val
!= 1)) {
2057 session
->lns_mode
= val
? -1 : 0;
2058 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2059 "%s: set lns_mode=%d\n", session
->name
,
2063 case PPPOL2TP_SO_DEBUG
:
2064 session
->debug
= val
;
2065 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2066 "%s: set debug=%x\n", session
->name
, session
->debug
);
2069 case PPPOL2TP_SO_REORDERTO
:
2070 session
->reorder_timeout
= msecs_to_jiffies(val
);
2071 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2072 "%s: set reorder_timeout=%d\n", session
->name
,
2073 session
->reorder_timeout
);
2084 /* Main setsockopt() entry point.
2085 * Does API checks, then calls either the tunnel or session setsockopt
2086 * handler, according to whether the PPPoL2TP socket is a for a regular
2087 * session or the special tunnel type.
2089 static int pppol2tp_setsockopt(struct socket
*sock
, int level
, int optname
,
2090 char __user
*optval
, int optlen
)
2092 struct sock
*sk
= sock
->sk
;
2093 struct pppol2tp_session
*session
= sk
->sk_user_data
;
2094 struct pppol2tp_tunnel
*tunnel
;
2098 if (level
!= SOL_PPPOL2TP
)
2099 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
2101 if (optlen
< sizeof(int))
2104 if (get_user(val
, (int __user
*)optval
))
2108 if (sk
->sk_user_data
== NULL
)
2111 /* Get session context from the socket */
2113 session
= pppol2tp_sock_to_session(sk
);
2114 if (session
== NULL
)
2117 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2119 if ((session
->tunnel_addr
.s_session
== 0) &&
2120 (session
->tunnel_addr
.d_session
== 0)) {
2122 tunnel
= pppol2tp_sock_to_tunnel(session
->tunnel_sock
);
2126 err
= pppol2tp_tunnel_setsockopt(sk
, tunnel
, optname
, val
);
2127 sock_put(session
->tunnel_sock
);
2129 err
= pppol2tp_session_setsockopt(sk
, session
, optname
, val
);
2139 /* Tunnel getsockopt helper. Called with sock locked.
2141 static int pppol2tp_tunnel_getsockopt(struct sock
*sk
,
2142 struct pppol2tp_tunnel
*tunnel
,
2143 int optname
, int *val
)
2148 case PPPOL2TP_SO_DEBUG
:
2149 *val
= tunnel
->debug
;
2150 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2151 "%s: get debug=%x\n", tunnel
->name
, tunnel
->debug
);
2162 /* Session getsockopt helper. Called with sock locked.
2164 static int pppol2tp_session_getsockopt(struct sock
*sk
,
2165 struct pppol2tp_session
*session
,
2166 int optname
, int *val
)
2171 case PPPOL2TP_SO_RECVSEQ
:
2172 *val
= session
->recv_seq
;
2173 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2174 "%s: get recv_seq=%d\n", session
->name
, *val
);
2177 case PPPOL2TP_SO_SENDSEQ
:
2178 *val
= session
->send_seq
;
2179 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2180 "%s: get send_seq=%d\n", session
->name
, *val
);
2183 case PPPOL2TP_SO_LNSMODE
:
2184 *val
= session
->lns_mode
;
2185 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2186 "%s: get lns_mode=%d\n", session
->name
, *val
);
2189 case PPPOL2TP_SO_DEBUG
:
2190 *val
= session
->debug
;
2191 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2192 "%s: get debug=%d\n", session
->name
, *val
);
2195 case PPPOL2TP_SO_REORDERTO
:
2196 *val
= (int) jiffies_to_msecs(session
->reorder_timeout
);
2197 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2198 "%s: get reorder_timeout=%d\n", session
->name
, *val
);
2208 /* Main getsockopt() entry point.
2209 * Does API checks, then calls either the tunnel or session getsockopt
2210 * handler, according to whether the PPPoX socket is a for a regular session
2211 * or the special tunnel type.
2213 static int pppol2tp_getsockopt(struct socket
*sock
, int level
,
2214 int optname
, char __user
*optval
, int __user
*optlen
)
2216 struct sock
*sk
= sock
->sk
;
2217 struct pppol2tp_session
*session
= sk
->sk_user_data
;
2218 struct pppol2tp_tunnel
*tunnel
;
2222 if (level
!= SOL_PPPOL2TP
)
2223 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
2225 if (get_user(len
, (int __user
*) optlen
))
2228 len
= min_t(unsigned int, len
, sizeof(int));
2234 if (sk
->sk_user_data
== NULL
)
2237 /* Get the session context */
2239 session
= pppol2tp_sock_to_session(sk
);
2240 if (session
== NULL
)
2243 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2244 if ((session
->tunnel_addr
.s_session
== 0) &&
2245 (session
->tunnel_addr
.d_session
== 0)) {
2247 tunnel
= pppol2tp_sock_to_tunnel(session
->tunnel_sock
);
2251 err
= pppol2tp_tunnel_getsockopt(sk
, tunnel
, optname
, &val
);
2252 sock_put(session
->tunnel_sock
);
2254 err
= pppol2tp_session_getsockopt(sk
, session
, optname
, &val
);
2257 if (put_user(len
, (int __user
*) optlen
))
2260 if (copy_to_user((void __user
*) optval
, &val
, len
))
2271 /*****************************************************************************
2272 * /proc filesystem for debug
2273 *****************************************************************************/
2275 #ifdef CONFIG_PROC_FS
2277 #include <linux/seq_file.h>
2279 struct pppol2tp_seq_data
{
2280 struct pppol2tp_tunnel
*tunnel
; /* current tunnel */
2281 struct pppol2tp_session
*session
; /* NULL means get first session in tunnel */
2284 static struct pppol2tp_session
*next_session(struct pppol2tp_tunnel
*tunnel
, struct pppol2tp_session
*curr
)
2286 struct pppol2tp_session
*session
= NULL
;
2287 struct hlist_node
*walk
;
2292 read_lock_bh(&tunnel
->hlist_lock
);
2293 for (i
= 0; i
< PPPOL2TP_HASH_SIZE
; i
++) {
2294 hlist_for_each_entry(session
, walk
, &tunnel
->session_hlist
[i
], hlist
) {
2299 if (session
== curr
) {
2310 read_unlock_bh(&tunnel
->hlist_lock
);
2317 static struct pppol2tp_tunnel
*next_tunnel(struct pppol2tp_tunnel
*curr
)
2319 struct pppol2tp_tunnel
*tunnel
= NULL
;
2321 read_lock_bh(&pppol2tp_tunnel_list_lock
);
2322 if (list_is_last(&curr
->list
, &pppol2tp_tunnel_list
)) {
2325 tunnel
= list_entry(curr
->list
.next
, struct pppol2tp_tunnel
, list
);
2327 read_unlock_bh(&pppol2tp_tunnel_list_lock
);
2332 static void *pppol2tp_seq_start(struct seq_file
*m
, loff_t
*offs
)
2334 struct pppol2tp_seq_data
*pd
= SEQ_START_TOKEN
;
2340 BUG_ON(m
->private == NULL
);
2343 if (pd
->tunnel
== NULL
) {
2344 if (!list_empty(&pppol2tp_tunnel_list
))
2345 pd
->tunnel
= list_entry(pppol2tp_tunnel_list
.next
, struct pppol2tp_tunnel
, list
);
2347 pd
->session
= next_session(pd
->tunnel
, pd
->session
);
2348 if (pd
->session
== NULL
) {
2349 pd
->tunnel
= next_tunnel(pd
->tunnel
);
2353 /* NULL tunnel and session indicates end of list */
2354 if ((pd
->tunnel
== NULL
) && (pd
->session
== NULL
))
2361 static void *pppol2tp_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2367 static void pppol2tp_seq_stop(struct seq_file
*p
, void *v
)
2372 static void pppol2tp_seq_tunnel_show(struct seq_file
*m
, void *v
)
2374 struct pppol2tp_tunnel
*tunnel
= v
;
2376 seq_printf(m
, "\nTUNNEL '%s', %c %d\n",
2378 (tunnel
== tunnel
->sock
->sk_user_data
) ? 'Y':'N',
2379 atomic_read(&tunnel
->ref_count
) - 1);
2380 seq_printf(m
, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2382 (unsigned long long)tunnel
->stats
.tx_packets
,
2383 (unsigned long long)tunnel
->stats
.tx_bytes
,
2384 (unsigned long long)tunnel
->stats
.tx_errors
,
2385 (unsigned long long)tunnel
->stats
.rx_packets
,
2386 (unsigned long long)tunnel
->stats
.rx_bytes
,
2387 (unsigned long long)tunnel
->stats
.rx_errors
);
2390 static void pppol2tp_seq_session_show(struct seq_file
*m
, void *v
)
2392 struct pppol2tp_session
*session
= v
;
2394 seq_printf(m
, " SESSION '%s' %08X/%d %04X/%04X -> "
2395 "%04X/%04X %d %c\n",
2397 ntohl(session
->tunnel_addr
.addr
.sin_addr
.s_addr
),
2398 ntohs(session
->tunnel_addr
.addr
.sin_port
),
2399 session
->tunnel_addr
.s_tunnel
,
2400 session
->tunnel_addr
.s_session
,
2401 session
->tunnel_addr
.d_tunnel
,
2402 session
->tunnel_addr
.d_session
,
2403 session
->sock
->sk_state
,
2404 (session
== session
->sock
->sk_user_data
) ?
2406 seq_printf(m
, " %d/%d/%c/%c/%s %08x %u\n",
2407 session
->mtu
, session
->mru
,
2408 session
->recv_seq
? 'R' : '-',
2409 session
->send_seq
? 'S' : '-',
2410 session
->lns_mode
? "LNS" : "LAC",
2412 jiffies_to_msecs(session
->reorder_timeout
));
2413 seq_printf(m
, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2414 session
->nr
, session
->ns
,
2415 (unsigned long long)session
->stats
.tx_packets
,
2416 (unsigned long long)session
->stats
.tx_bytes
,
2417 (unsigned long long)session
->stats
.tx_errors
,
2418 (unsigned long long)session
->stats
.rx_packets
,
2419 (unsigned long long)session
->stats
.rx_bytes
,
2420 (unsigned long long)session
->stats
.rx_errors
);
2423 static int pppol2tp_seq_show(struct seq_file
*m
, void *v
)
2425 struct pppol2tp_seq_data
*pd
= v
;
2427 /* display header on line 1 */
2428 if (v
== SEQ_START_TOKEN
) {
2429 seq_puts(m
, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION
"\n");
2430 seq_puts(m
, "TUNNEL name, user-data-ok session-count\n");
2431 seq_puts(m
, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2432 seq_puts(m
, " SESSION name, addr/port src-tid/sid "
2433 "dest-tid/sid state user-data-ok\n");
2434 seq_puts(m
, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2435 seq_puts(m
, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2439 /* Show the tunnel or session context.
2441 if (pd
->session
== NULL
)
2442 pppol2tp_seq_tunnel_show(m
, pd
->tunnel
);
2444 pppol2tp_seq_session_show(m
, pd
->session
);
2450 static struct seq_operations pppol2tp_seq_ops
= {
2451 .start
= pppol2tp_seq_start
,
2452 .next
= pppol2tp_seq_next
,
2453 .stop
= pppol2tp_seq_stop
,
2454 .show
= pppol2tp_seq_show
,
2457 /* Called when our /proc file is opened. We allocate data for use when
2458 * iterating our tunnel / session contexts and store it in the private
2459 * data of the seq_file.
2461 static int pppol2tp_proc_open(struct inode
*inode
, struct file
*file
)
2464 struct pppol2tp_seq_data
*pd
;
2467 ret
= seq_open(file
, &pppol2tp_seq_ops
);
2471 m
= file
->private_data
;
2473 /* Allocate and fill our proc_data for access later */
2475 m
->private = kzalloc(sizeof(struct pppol2tp_seq_data
), GFP_KERNEL
);
2476 if (m
->private == NULL
)
2486 /* Called when /proc file access completes.
2488 static int pppol2tp_proc_release(struct inode
*inode
, struct file
*file
)
2490 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2495 return seq_release(inode
, file
);
2498 static struct file_operations pppol2tp_proc_fops
= {
2499 .owner
= THIS_MODULE
,
2500 .open
= pppol2tp_proc_open
,
2502 .llseek
= seq_lseek
,
2503 .release
= pppol2tp_proc_release
,
2506 static struct proc_dir_entry
*pppol2tp_proc
;
2508 #endif /* CONFIG_PROC_FS */
2510 /*****************************************************************************
2512 *****************************************************************************/
2514 static struct proto_ops pppol2tp_ops
= {
2516 .owner
= THIS_MODULE
,
2517 .release
= pppol2tp_release
,
2518 .bind
= sock_no_bind
,
2519 .connect
= pppol2tp_connect
,
2520 .socketpair
= sock_no_socketpair
,
2521 .accept
= sock_no_accept
,
2522 .getname
= pppol2tp_getname
,
2523 .poll
= datagram_poll
,
2524 .listen
= sock_no_listen
,
2525 .shutdown
= sock_no_shutdown
,
2526 .setsockopt
= pppol2tp_setsockopt
,
2527 .getsockopt
= pppol2tp_getsockopt
,
2528 .sendmsg
= pppol2tp_sendmsg
,
2529 .recvmsg
= pppol2tp_recvmsg
,
2530 .mmap
= sock_no_mmap
,
2531 .ioctl
= pppox_ioctl
,
2534 static struct pppox_proto pppol2tp_proto
= {
2535 .create
= pppol2tp_create
,
2536 .ioctl
= pppol2tp_ioctl
2539 static int __init
pppol2tp_init(void)
2543 err
= proto_register(&pppol2tp_sk_proto
, 0);
2546 err
= register_pppox_proto(PX_PROTO_OL2TP
, &pppol2tp_proto
);
2548 goto out_unregister_pppol2tp_proto
;
2550 #ifdef CONFIG_PROC_FS
2551 pppol2tp_proc
= proc_net_fops_create(&init_net
, "pppol2tp", 0,
2552 &pppol2tp_proc_fops
);
2553 if (!pppol2tp_proc
) {
2555 goto out_unregister_pppox_proto
;
2557 #endif /* CONFIG_PROC_FS */
2558 printk(KERN_INFO
"PPPoL2TP kernel driver, %s\n",
2559 PPPOL2TP_DRV_VERSION
);
2563 #ifdef CONFIG_PROC_FS
2564 out_unregister_pppox_proto
:
2565 unregister_pppox_proto(PX_PROTO_OL2TP
);
2567 out_unregister_pppol2tp_proto
:
2568 proto_unregister(&pppol2tp_sk_proto
);
2572 static void __exit
pppol2tp_exit(void)
2574 unregister_pppox_proto(PX_PROTO_OL2TP
);
2576 #ifdef CONFIG_PROC_FS
2577 remove_proc_entry("pppol2tp", init_net
.proc_net
);
2579 proto_unregister(&pppol2tp_sk_proto
);
2582 module_init(pppol2tp_init
);
2583 module_exit(pppol2tp_exit
);
2585 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>, "
2586 "James Chapman <jchapman@katalix.com>");
2587 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2588 MODULE_LICENSE("GPL");
2589 MODULE_VERSION(PPPOL2TP_DRV_VERSION
);