1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
10 * 251003 : Copied from pppoe.c version 0.6.9.
12 * Authors: Martijn van Oosterhout <kleptog@svana.org>
13 * James Chapman (jchapman@katalix.com)
15 * Michal Ostrowski <mostrows@speakeasy.net>
16 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
17 * David S. Miller (davem@redhat.com)
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version
23 * 2 of the License, or (at your option) any later version.
27 /* This driver handles only L2TP data frames; control frames are handled by a
28 * userspace application.
30 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
31 * attaches it to a bound UDP socket with local tunnel_id / session_id and
32 * peer tunnel_id / session_id set. Data can then be sent or received using
33 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
34 * can be read or modified using ioctl() or [gs]etsockopt() calls.
36 * When a PPPoL2TP socket is connected with local and peer session_id values
37 * zero, the socket is treated as a special tunnel management socket.
39 * Here's example userspace code to create a socket for sending/receiving data
40 * over an L2TP session:-
42 * struct sockaddr_pppol2tp sax;
46 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
48 * sax.sa_family = AF_PPPOX;
49 * sax.sa_protocol = PX_PROTO_OL2TP;
50 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
51 * sax.pppol2tp.pid = 0; // current pid owns UDP socket
52 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
53 * sax.pppol2tp.addr.sin_port = addr->sin_port;
54 * sax.pppol2tp.addr.sin_family = AF_INET;
55 * sax.pppol2tp.s_tunnel = tunnel_id;
56 * sax.pppol2tp.s_session = session_id;
57 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
58 * sax.pppol2tp.d_session = peer_session_id;
60 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
64 #include <linux/module.h>
65 #include <linux/version.h>
66 #include <linux/string.h>
67 #include <linux/list.h>
68 #include <asm/uaccess.h>
70 #include <linux/kernel.h>
71 #include <linux/spinlock.h>
72 #include <linux/kthread.h>
73 #include <linux/sched.h>
74 #include <linux/slab.h>
75 #include <linux/errno.h>
77 #include <linux/netdevice.h>
78 #include <linux/net.h>
79 #include <linux/inetdevice.h>
80 #include <linux/skbuff.h>
81 #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/proc_fs.h>
97 #include <asm/byteorder.h>
98 #include <asm/atomic.h>
101 #define PPPOL2TP_DRV_VERSION "V0.17"
103 /* Developer debug code. */
105 #define DEBUG /* Define to compile in very verbose developer debug */
108 /* Timeouts are specified in milliseconds to/from userspace */
109 #define JIFFIES_TO_MS(t) ((t) * 1000 / HZ)
110 #define MS_TO_JIFFIES(j) ((j * HZ) / 1000)
112 /* L2TP header constants */
113 #define L2TP_HDRFLAG_T 0x8000
114 #define L2TP_HDRFLAG_L 0x4000
115 #define L2TP_HDRFLAG_S 0x0800
116 #define L2TP_HDRFLAG_O 0x0200
117 #define L2TP_HDRFLAG_P 0x0100
119 #define L2TP_HDR_VER_MASK 0x000F
120 #define L2TP_HDR_VER 0x0002
122 /* Space for UDP, L2TP and PPP headers */
123 #define PPPOL2TP_HEADER_OVERHEAD 40
125 /* Just some random numbers */
126 #define L2TP_TUNNEL_MAGIC 0x42114DDA
127 #define L2TP_SESSION_MAGIC 0x0C04EB7D
129 #define PPPOL2TP_HASH_BITS 4
130 #define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
132 /* Default trace flags */
134 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS -1
136 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
140 /* Debug kernel message control.
141 * Verbose debug messages (L2TP_MSG_DEBUG flag) are optionally compiled in.
144 #define DPRINTK(_mask, _fmt, args...) \
146 if ((_mask) & PPPOL2TP_MSG_DEBUG) \
147 printk(KERN_DEBUG "PPPOL2TP %s: " _fmt, \
148 __FUNCTION__, ##args); \
151 #define DPRINTK(_mask, _fmt, args...) do { } while(0)
154 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
156 if ((_mask) & (_type)) \
157 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
160 /* Extra driver debug. Should only be enabled by developers working on
164 #define ENTER_FUNCTION printk(KERN_DEBUG "PPPOL2TP: --> %s\n", __FUNCTION__)
165 #define EXIT_FUNCTION printk(KERN_DEBUG "PPPOL2TP: <-- %s\n", __FUNCTION__)
167 #define ENTER_FUNCTION do { } while(0)
168 #define EXIT_FUNCTION do { } while(0)
171 /* Number of bytes to build transmit L2TP headers.
172 * Unfortunately the size is different depending on whether sequence numbers
175 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
176 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
178 struct pppol2tp_send
{
179 struct pppol2tp_session
*session
;
180 struct pppol2tp_tunnel
*tunnel
;
183 struct sock
*tunnel_sock
;
185 struct work_struct send_task
;
188 struct sock_iocb siocb
;
189 u8 hdr
[PPPOL2TP_L2TP_HDR_SIZE_SEQ
];
192 struct pppol2tp_tunnel
;
194 /* Describes a session. It is the sk_user_data field in the PPPoL2TP
195 * socket. Contains information to determine incoming packets and transmit
198 struct pppol2tp_session
200 int magic
; /* should be
201 * L2TP_SESSION_MAGIC */
202 int owner
; /* pid that opened the socket */
204 struct sock
*sock
; /* Pointer to the session
206 struct sock
*tunnel_sock
; /* Pointer to the tunnel UDP
209 struct pppol2tp_addr tunnel_addr
; /* Description of tunnel */
211 struct pppol2tp_tunnel
*tunnel
; /* back pointer to tunnel
214 char name
[20]; /* "sess xxxxx/yyyyy", where
215 * x=tunnel_id, y=session_id */
218 int flags
; /* accessed by PPPIOCGFLAGS.
220 int recv_seq
:1; /* expect receive packets with
221 * sequence numbers? */
222 int send_seq
:1; /* send packets with sequence
224 int lns_mode
:1; /* behave as LNS? LAC enables
225 * sequence numbers under
227 int debug
; /* bitmask of debug message
229 int reorder_timeout
; /* configured reorder timeout
231 u16 nr
; /* session NR state (receive) */
232 u16 ns
; /* session NR state (send) */
233 struct sk_buff_head reorder_q
; /* receive reorder queue */
234 struct pppol2tp_ioc_stats stats
;
235 struct hlist_node hlist
; /* Hash list node */
238 /* The sk_user_data field of the tunnel's UDP socket. It contains info to track
239 * all the associated sessions so incoming packets can be sorted out
241 struct pppol2tp_tunnel
243 int magic
; /* Should be L2TP_TUNNEL_MAGIC */
245 struct workqueue_struct
*wq
; /* Per-tunnel work queue */
247 struct proto
*old_proto
; /* original proto */
248 struct proto l2tp_proto
; /* L2TP proto */
249 rwlock_t hlist_lock
; /* protect session_hlist */
250 struct hlist_head session_hlist
[PPPOL2TP_HASH_SIZE
];
251 /* hashed list of sessions,
253 int debug
; /* bitmask of debug message
255 char name
[12]; /* "tunl xxxxx" */
256 struct pppol2tp_ioc_stats stats
;
258 #ifndef UDP_ENCAP_L2TPINUDP
259 void (*old_data_ready
)(struct sock
*, int);
261 void (*old_sk_destruct
)(struct sock
*);
263 struct sock
*sock
; /* Parent socket */
264 struct list_head list
; /* Keep a list of all open
265 * prepared sockets */
267 atomic_t session_count
;
270 /* Private data stored for received packets in the skb.
272 struct pppol2tp_skb_cb
{
277 unsigned long expires
;
280 #define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
282 static int pppol2tp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
);
284 static struct ppp_channel_ops pppol2tp_chan_ops
= { pppol2tp_xmit
, NULL
};
285 static struct proto_ops pppol2tp_ops
;
286 static LIST_HEAD(pppol2tp_tunnel_list
);
288 /* Macros to derive session/tunnel context pointers from a socket. */
289 #define SOCK_2_SESSION(sock, session, err, errval, label, quiet) \
290 session = (struct pppol2tp_session *)((sock)->sk_user_data); \
291 if (!session || session->magic != L2TP_SESSION_MAGIC) { \
293 printk(KERN_ERR "%s: %s:%d: BAD SESSION MAGIC " \
294 "(" #sock "=%p) session=%p magic=%x\n", \
295 __FUNCTION__, __FILE__, __LINE__, sock, \
296 session, session ? session->magic : 0); \
301 #define SOCK_2_TUNNEL(sock, tunnel, err, errval, label, quiet) \
302 tunnel = (struct pppol2tp_tunnel *)((sock)->sk_user_data); \
303 if (!tunnel || tunnel->magic != L2TP_TUNNEL_MAGIC) { \
305 printk(KERN_ERR "%s: %s:%d: BAD TUNNEL MAGIC " \
306 "(" #sock "=%p) tunnel=%p magic=%x\n", \
307 __FUNCTION__, __FILE__, __LINE__, sock, \
308 tunnel, tunnel ? tunnel->magic : 0); \
313 /* Session hash list.
314 * The session_id SHOULD be random according to RFC2661, but several
315 * L2TP implementations (Cisco and Microsoft) use incrementing
316 * session_ids. So we do a real hash on the session_id, rather than a
319 static inline struct hlist_head
*
320 pppol2tp_session_id_hash(struct pppol2tp_tunnel
*tunnel
, u16 session_id
)
322 unsigned long hash_val
= (unsigned long) session_id
;
323 return &tunnel
->session_hlist
[hash_long(hash_val
, PPPOL2TP_HASH_BITS
)];
326 /* Lookup a session by id
328 static struct pppol2tp_session
*
329 pppol2tp_session_find(struct pppol2tp_tunnel
*tunnel
, u16 session_id
)
331 struct hlist_head
*session_list
=
332 pppol2tp_session_id_hash(tunnel
, session_id
);
333 struct hlist_node
*tmp
;
334 struct hlist_node
*walk
;
335 struct pppol2tp_session
*session
;
337 hlist_for_each_safe(walk
, tmp
, session_list
) {
338 session
= hlist_entry(walk
, struct pppol2tp_session
, hlist
);
339 if (session
->tunnel_addr
.s_session
== session_id
) {
347 /*****************************************************************************
348 * Receive data handling
349 *****************************************************************************/
351 /* Queue a skb in order. If the skb has no sequence number, queue it
354 static void pppol2tp_recv_queue_skb(struct pppol2tp_session
*session
, struct sk_buff
*skb
)
356 struct sk_buff
*next
;
357 struct sk_buff
*prev
;
358 u16 ns
= PPPOL2TP_SKB_CB(skb
)->ns
;
362 spin_lock(&session
->reorder_q
.lock
);
364 prev
= (struct sk_buff
*) &session
->reorder_q
;
366 while (next
!= prev
) {
367 if (PPPOL2TP_SKB_CB(next
)->ns
> ns
) {
368 __skb_insert(skb
, next
->prev
, next
, &session
->reorder_q
);
369 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
370 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
371 session
->name
, ns
, PPPOL2TP_SKB_CB(next
)->ns
,
372 skb_queue_len(&session
->reorder_q
));
373 session
->stats
.rx_oos_packets
++;
379 __skb_queue_tail(&session
->reorder_q
, skb
);
382 spin_unlock(&session
->reorder_q
.lock
);
386 /* Dequeue a single skb, passing it either to ppp or to userspace.
388 static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session
*session
, struct sk_buff
*skb
)
390 struct pppol2tp_tunnel
*tunnel
= session
->tunnel
;
391 int length
= PPPOL2TP_SKB_CB(skb
)->length
;
392 struct sock
*session_sock
= NULL
;
396 /* We're about to requeue the skb, so unlink it and return resources
397 * to its current owner (a socket receive buffer).
399 skb_unlink(skb
, &session
->reorder_q
);
402 tunnel
->stats
.rx_packets
++;
403 tunnel
->stats
.rx_bytes
+= length
;
404 session
->stats
.rx_packets
++;
405 session
->stats
.rx_bytes
+= length
;
407 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
410 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
411 "%s: updated nr to %hu\n", session
->name
, session
->nr
);
414 /* If the socket is bound, send it in to PPP's input queue. Otherwise
415 * queue it on the session socket.
417 session_sock
= session
->sock
;
418 if (session_sock
->sk_state
& PPPOX_BOUND
) {
419 struct pppox_sock
*po
;
420 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
421 "%s: recv %d byte data frame, passing to ppp\n",
422 session
->name
, length
);
424 /* We need to forget all info related to the L2TP packet
425 * gathered in the skb as we are going to reuse the same
426 * skb for the inner packet.
428 * - reset xfrm (IPSec) information as it applies to
429 * the outer L2TP packet and not to the inner one
430 * - release the dst to force a route lookup on the inner
431 * IP packet since skb->dst currently points to the dst
433 * - reset netfilter information as it doesn't apply
434 * to the inner packet either
437 dst_release(skb
->dst
);
441 po
= pppox_sk(session_sock
);
442 ppp_input(&po
->chan
, skb
);
444 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
445 "%s: socket not bound\n", session
->name
);
446 /* Not bound. Queue it now */
447 if (sock_queue_rcv_skb(session_sock
, skb
) < 0) {
448 session
->stats
.rx_errors
++;
450 if (!sock_flag(session_sock
, SOCK_DEAD
))
451 session_sock
->sk_data_ready(session_sock
, 0);
455 DPRINTK(session
->debug
, "calling sock_put; refcnt=%d\n",
456 session
->sock
->sk_refcnt
.counter
);
457 sock_put(session
->sock
);
461 /* Dequeue skbs from the session's reorder_q, subject to packet order.
462 * Skbs that have been in the queue for too long are simply discarded.
464 static void pppol2tp_recv_dequeue(struct pppol2tp_session
*session
)
466 struct sk_buff
*next
;
467 struct sk_buff
*prev
;
471 prev
= (struct sk_buff
*) &session
->reorder_q
;
472 spin_lock(&session
->reorder_q
.lock
);
475 /* If the pkt at the head of the queue has the nr that we
476 * expect to send up next, dequeue it and any other
477 * in-sequence packets behind it.
479 while (next
!= prev
) {
480 struct sk_buff
*skb
= next
;
482 spin_unlock(&session
->reorder_q
.lock
);
484 if (time_after(jiffies
, PPPOL2TP_SKB_CB(skb
)->expires
)) {
485 session
->stats
.rx_seq_discards
++;
486 session
->stats
.rx_errors
++;
487 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
488 "%s: oos pkt %hu len %d discarded (too old), waiting for %hu, reorder_q_len=%d\n",
489 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
490 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
491 skb_queue_len(&session
->reorder_q
));
492 skb_unlink(skb
, &session
->reorder_q
);
494 sock_put(session
->sock
);
498 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
499 if (PPPOL2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
500 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
501 "%s: holding oos pkt %hu len %d, waiting for %hu, reorder_q_len=%d\n",
502 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
503 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
504 skb_queue_len(&session
->reorder_q
));
508 pppol2tp_recv_dequeue_skb(session
, skb
);
510 spin_lock(&session
->reorder_q
.lock
);
513 spin_unlock(&session
->reorder_q
.lock
);
518 /* Internal receive frame. Do the real work of receiving an L2TP data frame
519 * here. The skb is not on a list when we get here.
520 * Returns 0 if the packet was a data packet and was successfully passed on.
521 * Returns 1 if the packet was not a good data packet and could not be
522 * forwarded. All such packets are passed up to userspace to deal with.
524 static int pppol2tp_recv_core(struct sock
*sock
, struct sk_buff
*skb
)
526 struct pppol2tp_session
*session
= NULL
;
528 struct pppol2tp_tunnel
*tunnel
;
531 u16 tunnel_id
, session_id
;
537 SOCK_2_TUNNEL(sock
, tunnel
, error
, 1, end
, 0);
540 if (skb
->len
< sizeof(struct udphdr
)) {
541 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
542 "%s: recv short packet (len=%d)\n", tunnel
->name
, skb
->len
);
546 /* Get length of L2TP packet */
547 uh
= (struct udphdr
*) skb_transport_header(skb
);
548 length
= ntohs(uh
->len
) - sizeof(struct udphdr
);
550 /* Point to L2TP header */
551 ptr
= skb
->data
+ sizeof(struct udphdr
);
553 /* Trace packet contents, if enabled */
554 if (tunnel
->debug
& PPPOL2TP_MSG_DATA
) {
555 printk(KERN_DEBUG
"%s: recv: ", tunnel
->name
);
557 for (i
= 0; i
< length
&& i
< 16; i
++)
558 printk(" %02X", ptr
[i
]);
564 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
565 "%s: recv short L2TP packet (len=%d)\n", tunnel
->name
, length
);
569 /* Get L2TP header flags */
570 hdrflags
= ntohs(*(u16
*)ptr
);
572 /* If type is control packet, it is handled by userspace. */
573 if (hdrflags
& L2TP_HDRFLAG_T
) {
574 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
575 "%s: recv control packet, len=%d\n", tunnel
->name
, length
);
582 /* If length is present, skip it */
583 if (hdrflags
& L2TP_HDRFLAG_L
)
586 /* Extract tunnel and session ID */
587 tunnel_id
= ntohs(*(u16
*) ptr
);
589 session_id
= ntohs(*(u16
*) ptr
);
592 /* Find the session context */
593 session
= pppol2tp_session_find(tunnel
, session_id
);
595 /* Not found? Pass to userspace to deal with */
596 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
597 "%s: no socket found (%hu/%hu). Passing up.\n",
598 tunnel
->name
, tunnel_id
, session_id
);
601 sock_hold(session
->sock
);
603 DPRINTK(session
->debug
, "%s: socket rcvbuf alloc=%d\n",
604 session
->name
, atomic_read(&sock
->sk_rmem_alloc
));
606 /* The ref count on the socket was increased by the above call since
607 * we now hold a pointer to the session. Take care to do sock_put()
608 * when exiting this function from now on...
611 /* Handle the optional sequence numbers. If we are the LAC,
612 * enable/disable sequence numbers under the control of the LNS. If
613 * no sequence numbers present but we were expecting them, discard
616 if (hdrflags
& L2TP_HDRFLAG_S
) {
618 ns
= ntohs(*(u16
*) ptr
);
620 nr
= ntohs(*(u16
*) ptr
);
623 /* Received a packet with sequence numbers. If we're the LNS,
624 * check if we sre sending sequence numbers and if not,
627 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
628 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_INFO
,
629 "%s: requested to enable seq numbers by LNS\n",
631 session
->send_seq
= -1;
634 /* Store L2TP info in the skb */
635 PPPOL2TP_SKB_CB(skb
)->ns
= ns
;
636 PPPOL2TP_SKB_CB(skb
)->nr
= nr
;
637 PPPOL2TP_SKB_CB(skb
)->has_seq
= 1;
639 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
640 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
641 session
->name
, ns
, nr
, session
->nr
);
643 /* No sequence numbers.
644 * If user has configured mandatory sequence numbers, discard.
646 if (session
->recv_seq
) {
647 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_WARNING
,
648 "%s: recv data has no seq numbers when required. "
649 "Discarding\n", session
->name
);
650 session
->stats
.rx_seq_discards
++;
651 session
->stats
.rx_errors
++;
655 /* If we're the LAC and we're sending sequence numbers, the
656 * LNS has requested that we no longer send sequence numbers.
657 * If we're the LNS and we're sending sequence numbers, the
658 * LAC is broken. Discard the frame.
660 if ((!session
->lns_mode
) && (session
->send_seq
)) {
661 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_INFO
,
662 "%s: requested to disable seq numbers by LNS\n",
664 session
->send_seq
= 0;
665 } else if (session
->send_seq
) {
666 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_WARNING
,
667 "%s: recv data has no seq numbers when required. "
668 "Discarding\n", session
->name
);
669 session
->stats
.rx_seq_discards
++;
670 session
->stats
.rx_errors
++;
674 /* Store L2TP info in the skb */
675 PPPOL2TP_SKB_CB(skb
)->has_seq
= 0;
678 /* If offset bit set, skip it. */
679 if (hdrflags
& L2TP_HDRFLAG_O
)
680 ptr
+= 2 + ntohs(*(u16
*) ptr
);
682 skb_pull(skb
, ptr
- skb
->data
);
684 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
685 * don't send the PPP header (PPP header compression enabled), but
686 * other clients can include the header. So we cope with both cases
687 * here. The PPP header is always FF03 when using L2TP.
689 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
690 * the field may be unaligned.
692 if ((skb
->data
[0] == 0xff) && (skb
->data
[1] == 0x03))
695 /* Prepare skb for adding to the session's reorder_q. Hold
696 * packets for max reorder_timeout or 1 second if not
699 PPPOL2TP_SKB_CB(skb
)->length
= length
;
700 PPPOL2TP_SKB_CB(skb
)->expires
= jiffies
+
701 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
703 /* Add packet to the session's receive queue. Reordering is done here, if
704 * enabled. Saved L2TP protocol info is stored in skb->sb[].
706 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
707 if (session
->reorder_timeout
!= 0) {
708 /* Packet reordering enabled. Add skb to session's
709 * reorder queue, in order of ns.
711 pppol2tp_recv_queue_skb(session
, skb
);
713 /* Packet reordering disabled. Discard out-of-sequence
716 if (PPPOL2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
717 session
->stats
.rx_seq_discards
++;
718 session
->stats
.rx_errors
++;
719 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
720 "%s: oos pkt %hu len %d discarded, waiting for %hu, reorder_q_len=%d\n",
721 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
722 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
723 skb_queue_len(&session
->reorder_q
));
726 skb_queue_tail(&session
->reorder_q
, skb
);
729 /* No sequence numbers. Add the skb to the tail of the
730 * reorder queue. This ensures that it will be
731 * delivered after all previous sequenced skbs.
733 skb_queue_tail(&session
->reorder_q
, skb
);
736 /* Try to dequeue as many skbs from reorder_q as we can. */
737 pppol2tp_recv_dequeue(session
);
743 DPRINTK(session
->debug
, "discarding skb, len=%d\n", skb
->len
);
746 DPRINTK(session
->debug
, "calling sock_put; refcnt=%d\n",
747 session
->sock
->sk_refcnt
.counter
);
748 sock_put(session
->sock
);
757 #ifndef UDP_ENCAP_L2TPINUDP
758 /* The data_ready hook on the UDP socket. Scan the incoming packet list for
759 * packets to process. Only control or bad data packets are delivered to
762 static void pppol2tp_data_ready(struct sock
*sk
, int len
)
765 struct pppol2tp_tunnel
*tunnel
;
769 SOCK_2_TUNNEL(sk
, tunnel
, err
, -EBADF
, end
, 0);
771 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
772 "%s: received %d bytes\n", tunnel
->name
, len
);
774 skb
= skb_dequeue(&sk
->sk_receive_queue
);
776 if (pppol2tp_recv_core(sk
, skb
)) {
777 DPRINTK(tunnel
->debug
, "%s: packet passing to userspace\n",
779 skb_queue_head(&sk
->sk_receive_queue
, skb
);
780 tunnel
->old_data_ready(sk
, len
);
782 DPRINTK(tunnel
->debug
, "%s: data packet received\n",
791 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
795 * >0: skb should be passed up to userspace as UDP.
797 static int pppol2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
800 struct pppol2tp_tunnel
*tunnel
;
803 SOCK_2_TUNNEL(sk
, tunnel
, err
, -EBADF
, pass_up
, 0);
805 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
806 "%s: received %d bytes\n", tunnel
->name
, skb
->len
);
808 if (pppol2tp_recv_core(sk
, skb
))
820 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
822 static int pppol2tp_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
823 struct msghdr
*msg
, size_t len
,
827 struct sk_buff
*skb
= NULL
;
828 struct sock
*sk
= sock
->sk
;
833 if (sock
->state
& PPPOX_BOUND
)
836 msg
->msg_namelen
= 0;
838 skb
=skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
839 flags
& MSG_DONTWAIT
, &err
);
845 else if (len
< skb
->len
)
846 msg
->msg_flags
|= MSG_TRUNC
;
848 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, len
);
849 if (likely(err
== 0))
859 /************************************************************************
861 ***********************************************************************/
863 /* Internal UDP socket transmission
865 static int pppol2tp_udp_sock_send(struct kiocb
*iocb
,
866 struct pppol2tp_session
*session
,
867 struct pppol2tp_tunnel
*tunnel
,
868 struct msghdr
*msg
, int total_len
)
875 DPRINTK(session
->debug
, "%s: udp_sendmsg call...\n", session
->name
);
877 /* Catch bad socket parameter errors */
879 struct sockaddr_in
* usin
= (struct sockaddr_in
*)msg
->msg_name
;
880 if (msg
->msg_namelen
< sizeof(*usin
)) {
881 printk(KERN_ERR
"msg->msg_namelen wrong, %d\n", msg
->msg_namelen
);
884 if (usin
->sin_family
!= AF_INET
) {
885 if (usin
->sin_family
!= AF_UNSPEC
) {
886 printk(KERN_ERR
"addr family wrong: %d\n", usin
->sin_family
);
890 if ((usin
->sin_addr
.s_addr
== 0) || (usin
->sin_port
== 0)) {
891 printk(KERN_ERR
"udp addr=%x/%hu\n", usin
->sin_addr
.s_addr
, usin
->sin_port
);
897 /* Set to userspace data segment while we do a sendmsg() call. We're
898 * actually calling a userspace API from the kernel here...
903 /* The actual sendmsg() call... */
904 error
= tunnel
->old_proto
->sendmsg(iocb
, session
->tunnel_sock
, msg
, total_len
);
905 if (error
== -EIOCBQUEUED
)
906 error
= wait_on_sync_kiocb(iocb
);
908 /* Back to kernel space */
912 tunnel
->stats
.tx_packets
++;
913 tunnel
->stats
.tx_bytes
+= error
;
914 session
->stats
.tx_packets
++;
915 session
->stats
.tx_bytes
+= error
;
917 tunnel
->stats
.tx_errors
++;
918 session
->stats
.tx_errors
++;
921 DPRINTK(session
->debug
, "%s: %s: returning result %d\n", __FUNCTION__
,
922 session
->name
, error
);
930 /* Build an L2TP header for the session into the buffer provided.
932 static int pppol2tp_build_l2tp_header(struct pppol2tp_session
*session
,
936 u16 flags
= L2TP_HDR_VER
;
938 if (session
->send_seq
) {
939 flags
|= L2TP_HDRFLAG_S
;
942 /* Setup L2TP header.
943 * FIXME: Can this ever be unaligned? Is direct dereferencing of
944 * 16-bit header fields safe here for all architectures?
946 *bufp
++ = htons(flags
);
947 *bufp
++ = htons(session
->tunnel_addr
.d_tunnel
);
948 *bufp
++ = htons(session
->tunnel_addr
.d_session
);
949 if (session
->send_seq
) {
950 *bufp
++ = htons(session
->ns
);
953 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
954 "%s: updated ns to %hu\n", session
->name
, session
->ns
);
956 /* This is the PPP header really */
957 *bufp
= htons(0xff03);
959 return ((void *) bufp
) - buf
;
962 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
963 * when a user application does a sendmsg() on the session socket. L2TP and
964 * PPP headers must be inserted into the user's data.
966 static int pppol2tp_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*m
,
969 static unsigned char ppph
[2] = { 0xff, 0x03 };
970 struct sock
*sk
= sock
->sk
;
972 u8 hdr
[PPPOL2TP_L2TP_HDR_SIZE_SEQ
];
975 struct pppol2tp_session
*session
;
976 struct pppol2tp_tunnel
*tunnel
;
980 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
)) {
985 /* Get session and tunnel contexts */
986 SOCK_2_SESSION(sk
, session
, error
, -EBADF
, end
, 0);
987 SOCK_2_TUNNEL(session
->tunnel_sock
, tunnel
, error
, -EBADF
, end
, 0);
989 /* Setup L2TP header */
990 hdr_len
= pppol2tp_build_l2tp_header(session
, &hdr
);
992 if (session
->send_seq
)
993 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
994 "%s: send %d bytes, ns=%hu\n", session
->name
,
995 total_len
, session
->ns
- 1);
997 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
998 "%s: send %d bytes\n", session
->name
, total_len
);
1000 if (session
->debug
& PPPOL2TP_MSG_DATA
) {
1003 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
1005 for (i
= 0; i
< m
->msg_iovlen
; i
++) {
1006 for (j
= 0; j
< m
->msg_iov
[i
].iov_len
; j
++) {
1007 printk(" %02X", ((unsigned char *) m
->msg_iov
[i
].iov_base
)[j
]);
1018 /* Unfortunately, there is no direct way for us to pass an skb to the
1019 * UDP layer, we have to pretend to be sending ordinary data and use
1022 * We add the L2TP and PPP headers here. To do so, we create a new
1023 * struct msghdr and insert the headers as the first iovecs.
1025 msg
= kmalloc(sizeof(struct msghdr
), GFP_ATOMIC
);
1028 tunnel
->stats
.tx_errors
++;
1029 session
->stats
.tx_errors
++;
1033 msg
->msg_iov
= kmalloc((m
->msg_iovlen
+ 2) * sizeof(struct iovec
),
1035 if (msg
->msg_iov
== NULL
) {
1037 tunnel
->stats
.tx_errors
++;
1038 session
->stats
.tx_errors
++;
1043 msg
->msg_iov
[0].iov_base
= &hdr
;
1044 msg
->msg_iov
[0].iov_len
= hdr_len
;
1045 msg
->msg_iov
[1].iov_base
= &ppph
;
1046 msg
->msg_iov
[1].iov_len
= sizeof(ppph
);
1047 memcpy(&msg
->msg_iov
[2], &m
->msg_iov
[0],
1048 m
->msg_iovlen
* sizeof(struct iovec
));
1049 msg
->msg_iovlen
= m
->msg_iovlen
+ 2;
1051 /* If the user calls sendto() that's just too bad */
1052 msg
->msg_name
= &session
->tunnel_addr
.addr
;
1053 msg
->msg_namelen
= sizeof(session
->tunnel_addr
.addr
);
1055 msg
->msg_control
= m
->msg_control
;
1056 msg
->msg_controllen
= m
->msg_controllen
;
1057 msg
->msg_flags
= m
->msg_flags
;
1059 /* Do the real work. This always frees msg, regardless of whether
1060 * there was an error
1062 error
= pppol2tp_udp_sock_send(iocb
, session
, tunnel
, msg
,
1063 total_len
+ hdr_len
+ sizeof(ppph
));
1070 /* Work queue handler for pppol2tp_xmit().
1072 static void pppol2tp_wq_send(struct work_struct
*work
)
1074 struct pppol2tp_send
*send
= container_of(work
, struct pppol2tp_send
, send_task
);
1081 error
= send
->tunnel
->old_proto
->sendmsg(&send
->iocb
,
1082 send
->session
->tunnel_sock
,
1083 send
->msg
, send
->total_len
);
1084 if (error
== -EIOCBQUEUED
)
1085 error
= wait_on_sync_kiocb(&send
->iocb
);
1090 send
->tunnel
->stats
.tx_packets
++;
1091 send
->tunnel
->stats
.tx_bytes
+= error
;
1092 send
->session
->stats
.tx_packets
++;
1093 send
->session
->stats
.tx_bytes
+= error
;
1095 send
->tunnel
->stats
.tx_errors
++;
1096 send
->session
->stats
.tx_errors
++;
1101 kfree_skb(send
->skb
);
1105 /* Transmit function called by generic PPP driver. Sends PPP frame over
1108 * This is almost the same as pppol2tp_sendmsg(), but rather than being called
1109 * with a msghdr from userspace, it is called with a skb from the kernel.
1110 * Since this function cannot block, we must queue up the actual socket send
1113 static int pppol2tp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1115 static unsigned char ppph
[2] = { 0xff, 0x03 };
1116 struct sock
*sk
= (struct sock
*) chan
->private;
1119 struct msghdr
*msg
= NULL
;
1120 struct pppol2tp_session
*session
;
1121 struct pppol2tp_tunnel
*tunnel
;
1122 struct pppol2tp_send
*send
= NULL
;
1126 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
)) {
1127 DPRINTK(-1, "dead=%d state=%x\n", sock_flag(sk
, SOCK_DEAD
), sk
->sk_state
);
1132 /* Get session and tunnel contexts from the socket */
1133 SOCK_2_SESSION(sk
, session
, error
, -EBADF
, end
, 0);
1134 SOCK_2_TUNNEL(session
->tunnel_sock
, tunnel
, error
, -EBADF
, end
, 0);
1136 send
= kmalloc(sizeof(struct pppol2tp_send
), GFP_ATOMIC
);
1139 tunnel
->stats
.tx_errors
++;
1140 session
->stats
.tx_errors
++;
1144 /* Setup L2TP header */
1145 hdr_len
= pppol2tp_build_l2tp_header(session
, &send
->hdr
);
1147 if (session
->send_seq
)
1148 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1149 "%s: send %d bytes, ns=%hu\n",
1150 session
->name
, skb
->len
, session
->ns
- 1);
1152 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1153 "%s: send %d bytes\n", session
->name
, skb
->len
);
1155 if (session
->debug
& PPPOL2TP_MSG_DATA
) {
1158 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
1159 for (i
= 0; i
< skb
->len
; i
++) {
1160 printk(" %02X", skb
->data
[i
]);
1169 /* Unfortunately there doesn't appear to be a way for us to pass an skb
1170 * to the UDP layer, we have to pretend to be sending ordinary data
1173 msg
= kmalloc(sizeof(struct msghdr
), GFP_ATOMIC
);
1176 tunnel
->stats
.tx_errors
++;
1177 session
->stats
.tx_errors
++;
1181 msg
->msg_iov
= kmalloc(3 * sizeof(struct iovec
), GFP_ATOMIC
);
1182 if (msg
->msg_iov
== NULL
) {
1184 tunnel
->stats
.tx_errors
++;
1185 session
->stats
.tx_errors
++;
1188 msg
->msg_iov
[0].iov_base
= &send
->hdr
;
1189 msg
->msg_iov
[0].iov_len
= hdr_len
;
1190 /* FIXME: do we need to handle skb fragments here? */
1191 msg
->msg_iov
[1].iov_base
= &ppph
;
1192 msg
->msg_iov
[1].iov_len
= sizeof(ppph
);
1193 msg
->msg_iov
[2].iov_base
= skb
->data
;
1194 msg
->msg_iov
[2].iov_len
= skb
->len
;
1195 msg
->msg_iovlen
= 3;
1197 /* If the user calls sendto() that's just too bad */
1198 msg
->msg_name
= &session
->tunnel_addr
.addr
;
1199 msg
->msg_namelen
= sizeof(session
->tunnel_addr
.addr
);
1201 msg
->msg_control
= NULL
;
1202 msg
->msg_controllen
= 0;
1203 msg
->msg_flags
= MSG_DONTWAIT
; /* Need this to prevent blocking */
1205 send
->session
= session
;
1206 send
->tunnel
= tunnel
;
1209 send
->tunnel_sock
= session
->tunnel_sock
;
1210 send
->iov
= msg
->msg_iov
;
1211 send
->total_len
= skb
->len
+ hdr_len
+ sizeof(ppph
);
1212 init_sync_kiocb(&send
->iocb
, NULL
);
1213 send
->iocb
.private = &send
->siocb
;
1215 INIT_WORK(&send
->send_task
, pppol2tp_wq_send
);
1216 queue_work(tunnel
->wq
, &send
->send_task
);
1229 /*****************************************************************************
1230 * Session (and tunnel control) socket create/destroy.
1231 *****************************************************************************/
1233 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1234 * too. This handles that.
1236 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel
*tunnel
)
1239 struct hlist_node
*walk
;
1240 struct hlist_node
*tmp
;
1241 struct pppol2tp_session
*session
;
1249 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1250 "%s: closing all sessions...\n", tunnel
->name
);
1252 for (hash
= 0; hash
< PPPOL2TP_HASH_SIZE
; hash
++) {
1253 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1254 struct sk_buff
*skb
;
1256 session
= hlist_entry(walk
, struct pppol2tp_session
, hlist
);
1260 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1261 "%s: closing session\n", session
->name
);
1263 write_lock_bh(&tunnel
->hlist_lock
);
1264 hlist_del_init(&session
->hlist
);
1265 write_unlock_bh(&tunnel
->hlist_lock
);
1271 if (sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
)) {
1272 pppox_unbind_sock(sk
);
1273 sk
->sk_state
= PPPOX_DEAD
;
1274 sk
->sk_state_change(sk
);
1277 /* Purge any queued data */
1278 skb_queue_purge(&sk
->sk_receive_queue
);
1279 skb_queue_purge(&sk
->sk_write_queue
);
1280 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
1287 DPRINTK(session
->debug
, "calling sock_put; refcnt=%d\n",
1288 sk
->sk_refcnt
.counter
);
1296 /* Really kill the tunnel.
1297 * Come here only when all sessions have been cleared from the tunnel.
1299 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel
*tunnel
)
1301 struct sock
*sk
= tunnel
->sock
;
1305 /* Remove from socket list */
1306 list_del_init(&tunnel
->list
);
1308 sk
->sk_prot
= tunnel
->old_proto
;
1309 #ifndef UDP_ENCAP_L2TPINUDP
1310 sk
->sk_data_ready
= tunnel
->old_data_ready
;
1312 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1313 (udp_sk(sk
))->encap_type
= 0;
1314 (udp_sk(sk
))->encap_rcv
= NULL
;
1316 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1317 sk
->sk_user_data
= NULL
;
1319 flush_workqueue(tunnel
->wq
);
1320 destroy_workqueue(tunnel
->wq
);
1322 DPRINTK(tunnel
->debug
, "%s: MOD_DEC_USE_COUNT\n", tunnel
->name
);
1328 /* Tunnel UDP socket destruct hook.
1329 * The tunnel context is deleted only when all session sockets have been
1332 static void pppol2tp_tunnel_destruct(struct sock
*sk
)
1334 struct pppol2tp_tunnel
*tunnel
;
1338 SOCK_2_TUNNEL(sk
, tunnel
, error
, -EBADF
, end
, 0);
1340 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1341 "%s: closing...\n", tunnel
->name
);
1343 pppol2tp_tunnel_closeall(tunnel
);
1350 /* Really kill the socket. (Called from sock_put if refcnt == 0.)
1352 static void pppol2tp_session_destruct(struct sock
*sk
)
1354 struct pppol2tp_session
*session
= NULL
;
1359 if (sk
->sk_user_data
!= NULL
) {
1360 struct pppol2tp_tunnel
*tunnel
;
1362 SOCK_2_SESSION(sk
, session
, error
, -EBADF
, out
, 0);
1363 skb_queue_purge(&session
->reorder_q
);
1365 /* Don't use SOCK_2_TUNNEL() here to get the tunnel context
1366 * because the tunnel socket might have already been closed
1367 * (its sk->sk_user_data will be NULL) so use the session's
1368 * private tunnel ptr instead.
1370 tunnel
= session
->tunnel
;
1371 if (tunnel
!= NULL
) {
1372 if (tunnel
->magic
!= L2TP_TUNNEL_MAGIC
) {
1373 printk(KERN_ERR
"%s: %s:%d: BAD TUNNEL MAGIC "
1374 "( tunnel=%p magic=%x )\n",
1375 __FUNCTION__
, __FILE__
, __LINE__
,
1376 tunnel
, tunnel
->magic
);
1381 /* Delete tunnel context if this was the last session on the
1382 * tunnel. This was allocated when the first session was
1383 * created on the tunnel. See
1384 * pppol2tp_prepare_tunnel_socket().
1386 DPRINTK(tunnel
->debug
, "%s: session_count=%d\n",
1387 tunnel
->name
, atomic_read(&tunnel
->session_count
));
1388 if (atomic_dec_and_test(&tunnel
->session_count
)) {
1389 pppol2tp_tunnel_free(tunnel
);
1393 if (session
!= NULL
)
1400 /* Called when the PPPoX socket (session) is closed.
1402 static int pppol2tp_release(struct socket
*sock
)
1404 struct sock
*sk
= sock
->sk
;
1405 struct pppol2tp_session
*session
= NULL
;
1406 struct pppol2tp_tunnel
*tunnel
;
1413 if (sock_flag(sk
, SOCK_DEAD
) != 0)
1416 if (sk
->sk_user_data
) { /* Was this socket actually connected? */
1417 SOCK_2_SESSION(sk
, session
, error
, -EBADF
, end
, 0);
1419 /* Don't use SOCK_2_TUNNEL() here to get the tunnel context
1420 * because the tunnel socket might have already been closed
1421 * (its sk->sk_user_data will be NULL) so use the session's
1422 * private tunnel ptr instead.
1424 tunnel
= session
->tunnel
;
1425 if (tunnel
!= NULL
) {
1426 if (tunnel
->magic
== L2TP_TUNNEL_MAGIC
) {
1427 /* Delete the session socket from the hash */
1428 write_lock_bh(&tunnel
->hlist_lock
);
1429 hlist_del_init(&session
->hlist
);
1430 write_unlock_bh(&tunnel
->hlist_lock
);
1432 printk(KERN_ERR
"%s: %s:%d: BAD TUNNEL MAGIC "
1433 "( tunnel=%p magic=%x )\n",
1434 __FUNCTION__
, __FILE__
, __LINE__
,
1435 tunnel
, tunnel
->magic
);
1443 pppox_unbind_sock(sk
);
1445 /* Signal the death of the socket. */
1446 sk
->sk_state
= PPPOX_DEAD
;
1450 /* Purge any queued data */
1451 skb_queue_purge(&sk
->sk_receive_queue
);
1452 skb_queue_purge(&sk
->sk_write_queue
);
1453 if (session
!= NULL
) {
1454 struct sk_buff
*skb
;
1455 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
1464 if (session
!= NULL
)
1465 DPRINTK(session
->debug
, "calling sock_put; refcnt=%d\n",
1466 session
->sock
->sk_refcnt
.counter
);
1474 /* Copied from fget() in fs/file_table.c.
1475 * Allows caller to specify the pid that owns the fd.
1477 static struct file
*pppol2tp_fget(pid_t pid
, unsigned int fd
)
1480 struct files_struct
*files
= current
->files
;
1483 struct task_struct
*tsk
= find_task_by_pid(pid
);
1489 spin_lock(&files
->file_lock
);
1490 file
= fcheck_files(files
, fd
);
1493 spin_unlock(&files
->file_lock
);
1497 /* Copied from sockfd_lookup() in net/socket.c.
1498 * Allows caller to specify the pid that owns the fd.
1500 static struct socket
*pppol2tp_sockfd_lookup(pid_t pid
, int fd
, int *err
)
1503 struct inode
*inode
;
1504 struct socket
*sock
;
1506 if (!(file
= pppol2tp_fget(pid
, fd
))) {
1511 inode
= file
->f_dentry
->d_inode
;
1512 if (!(sock
= SOCKET_I(inode
))) {
1518 if (sock
->file
!= file
) {
1519 printk(KERN_ERR
"socki_lookup: socket file changed!\n");
1525 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX sockets
1528 static struct sock
*pppol2tp_prepare_tunnel_socket(pid_t pid
, int fd
,
1529 u16 tunnel_id
, int *error
)
1532 struct socket
*sock
= NULL
;
1534 struct pppol2tp_tunnel
*tunnel
;
1535 struct sock
*ret
= NULL
;
1539 /* Get the socket from the fd */
1541 sock
= pppol2tp_sockfd_lookup(pid
, fd
, &err
);
1543 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1544 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1545 tunnel_id
, fd
, err
);
1549 /* Quick sanity checks */
1550 err
= -ESOCKTNOSUPPORT
;
1551 if (sock
->type
!= SOCK_DGRAM
) {
1552 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1553 "tunl %hu: fd %d wrong type, got %d, expected %d\n",
1554 tunnel_id
, fd
, sock
->type
, SOCK_DGRAM
);
1557 err
= -EAFNOSUPPORT
;
1558 if (sock
->ops
->family
!=AF_INET
) {
1559 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1560 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1561 tunnel_id
, fd
, sock
->ops
->family
, AF_INET
);
1568 /* Check if this socket has already been prepped */
1569 tunnel
= (struct pppol2tp_tunnel
*)sk
->sk_user_data
;
1570 if (tunnel
!= NULL
) {
1571 /* User-data field already set */
1573 if (tunnel
->magic
!= L2TP_TUNNEL_MAGIC
) {
1574 printk(KERN_ERR
"%s: %s:%d: BAD TUNNEL MAGIC "
1575 "( tunnel=%p magic=%x )\n",
1576 __FUNCTION__
, __FILE__
, __LINE__
,
1577 tunnel
, tunnel
->magic
);
1581 /* This socket has already been prepped */
1586 /* This socket is available and needs prepping. Create a new tunnel
1587 * context and init it.
1589 sk
->sk_user_data
= tunnel
= kmalloc(sizeof(struct pppol2tp_tunnel
), GFP_KERNEL
);
1590 if (sk
->sk_user_data
== NULL
) {
1595 memset(tunnel
, 0, sizeof(struct pppol2tp_tunnel
));
1597 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1598 sprintf(&tunnel
->name
[0], "tunl %hu", tunnel_id
);
1600 tunnel
->stats
.tunnel_id
= tunnel_id
;
1602 tunnel
->debug
= PPPOL2TP_DEFAULT_DEBUG_FLAGS
;
1604 DPRINTK(tunnel
->debug
, "tunl %hu: allocated tunnel=%p, sk=%p, sock=%p\n",
1605 tunnel_id
, tunnel
, sk
, sock
);
1607 tunnel
->wq
= create_workqueue("kl2tpd");
1610 goto err_free_tunnel
;
1613 /* Setup the new protocol stuff */
1614 tunnel
->old_proto
= sk
->sk_prot
;
1615 tunnel
->l2tp_proto
= *sk
->sk_prot
;
1617 sk
->sk_prot
= &tunnel
->l2tp_proto
;
1619 #ifndef UDP_ENCAP_L2TPINUDP
1620 tunnel
->old_data_ready
= sk
->sk_data_ready
;
1621 sk
->sk_data_ready
= &pppol2tp_data_ready
;
1623 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1624 (udp_sk(sk
))->encap_type
= UDP_ENCAP_L2TPINUDP
;
1625 (udp_sk(sk
))->encap_rcv
= pppol2tp_udp_encap_recv
;
1628 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1629 sk
->sk_destruct
= &pppol2tp_tunnel_destruct
;
1632 sk
->sk_allocation
= GFP_ATOMIC
;
1634 rwlock_init(&tunnel
->hlist_lock
);
1636 /* Add tunnel to our list */
1637 INIT_LIST_HEAD(&tunnel
->list
);
1638 list_add(&tunnel
->list
, &pppol2tp_tunnel_list
);
1657 static struct proto pppol2tp_sk_proto
= {
1659 .owner
= THIS_MODULE
,
1660 .obj_size
= sizeof(struct pppox_sock
),
1663 /* socket() handler. Initialize a new struct sock.
1665 static int pppol2tp_create(struct socket
*sock
)
1667 int error
= -ENOMEM
;
1671 DPRINTK(-1, "sock=%p\n", sock
);
1673 try_module_get(THIS_MODULE
);
1675 sk
= sk_alloc(PF_PPPOX
, GFP_KERNEL
, &pppol2tp_sk_proto
, 1);
1679 sock_init_data(sock
, sk
);
1681 sock
->state
= SS_UNCONNECTED
;
1682 sock
->ops
= &pppol2tp_ops
;
1684 sk
->sk_backlog_rcv
= pppol2tp_recv_core
;
1685 sk
->sk_protocol
= PX_PROTO_OL2TP
;
1686 sk
->sk_family
= PF_PPPOX
;
1687 sk
->sk_state
= PPPOX_NONE
;
1688 sk
->sk_type
= SOCK_STREAM
;
1689 sk
->sk_destruct
= pppol2tp_session_destruct
;
1694 module_put(THIS_MODULE
);
1700 /* connect() handler.. Attach a PPPoX socket to a tunnel UDP socket
1702 int pppol2tp_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
1703 int sockaddr_len
, int flags
)
1705 struct sock
*sk
= sock
->sk
;
1706 struct sockaddr_pppol2tp
*sp
= (struct sockaddr_pppol2tp
*) uservaddr
;
1707 struct pppox_sock
*po
= pppox_sk(sk
);
1708 struct sock
*tunnel_sock
= NULL
;
1709 struct pppol2tp_session
*session
= NULL
;
1710 struct pppol2tp_tunnel
*tunnel
;
1711 struct dst_entry
*dst
;
1716 DPRINTK(-1, "sock=%p, uservaddr=%p, sockaddr_len=%d, flags=%d, addr=%x/%hu\n",
1717 sock
, uservaddr
, sockaddr_len
, flags
,
1718 ntohl(sp
->pppol2tp
.addr
.sin_addr
.s_addr
), ntohs(sp
->pppol2tp
.addr
.sin_port
));
1722 if (sp
->sa_protocol
!= PX_PROTO_OL2TP
)
1725 /* Check for already bound sockets */
1727 if (sk
->sk_state
& PPPOX_CONNECTED
)
1730 /* We don't supporting rebinding anyway */
1731 if (sk
->sk_user_data
)
1732 goto end
; /* socket is already attached */
1734 /* Don't bind if s_tunnel is 0 */
1736 if (sp
->pppol2tp
.s_tunnel
== 0)
1739 /* Look up the tunnel socket and configure it if necessary */
1740 tunnel_sock
= pppol2tp_prepare_tunnel_socket(sp
->pppol2tp
.pid
,
1742 sp
->pppol2tp
.s_tunnel
,
1744 if (tunnel_sock
== NULL
)
1746 tunnel
= tunnel_sock
->sk_user_data
;
1748 /* Allocate and initialize a new session context.
1750 session
= kzalloc(sizeof(struct pppol2tp_session
), GFP_KERNEL
);
1751 if (session
== NULL
) {
1756 skb_queue_head_init(&session
->reorder_q
);
1758 session
->magic
= L2TP_SESSION_MAGIC
;
1759 session
->owner
= current
->pid
;
1761 session
->tunnel
= tunnel
;
1762 session
->tunnel_sock
= tunnel_sock
;
1763 session
->tunnel_addr
= sp
->pppol2tp
;
1764 sprintf(&session
->name
[0], "sess %hu/%hu",
1765 session
->tunnel_addr
.s_tunnel
,
1766 session
->tunnel_addr
.s_session
);
1768 session
->stats
.tunnel_id
= session
->tunnel_addr
.s_tunnel
;
1769 session
->stats
.session_id
= session
->tunnel_addr
.s_session
;
1771 INIT_HLIST_NODE(&session
->hlist
);
1773 session
->debug
= PPPOL2TP_DEFAULT_DEBUG_FLAGS
;
1775 /* Default MTU must allow space for UDP/L2TP/PPP
1776 * headers. Leave some slack.
1778 session
->mtu
= session
->mru
= 1500 - PPPOL2TP_HEADER_OVERHEAD
;
1780 /* If PMTU discovery was enabled, use the MTU that was discovered */
1781 dst
= sk_dst_get(sk
);
1783 u32 pmtu
= dst_mtu(__sk_dst_get(sk
));
1785 session
->mtu
= session
->mru
= pmtu
-
1786 PPPOL2TP_HEADER_OVERHEAD
;
1787 DPRINTK(session
->debug
,
1788 "%s: MTU set by Path MTU discovery: mtu=%d\n",
1789 session
->name
, session
->mtu
);
1794 /* Special case: if source & dest session_id == 0x0000, this socket is
1795 * being created to manage the tunnel. Don't add the session to the
1796 * session hash list, just set up the internal context for use by
1797 * ioctl() and sockopt() handlers.
1799 if ((session
->tunnel_addr
.s_session
== 0) &&
1800 (session
->tunnel_addr
.d_session
== 0)) {
1802 DPRINTK(session
->debug
,
1803 "tunl %hu: socket created for tunnel mgmt ops\n",
1804 session
->tunnel_addr
.s_tunnel
);
1805 sk
->sk_user_data
= session
;
1809 DPRINTK(session
->debug
, "%s: allocated session=%p, sock=%p, owner=%d\n",
1810 session
->name
, session
, sk
, session
->owner
);
1812 /* Add session to the tunnel's hash list */
1813 SOCK_2_TUNNEL(tunnel_sock
, tunnel
, error
, -EBADF
, end
, 0);
1814 write_lock_bh(&tunnel
->hlist_lock
);
1815 hlist_add_head(&session
->hlist
,
1816 pppol2tp_session_id_hash(tunnel
,
1817 session
->tunnel_addr
.s_session
));
1818 write_unlock_bh(&tunnel
->hlist_lock
);
1820 /* This is how we get the session context from the socket. */
1821 sk
->sk_user_data
= session
;
1823 /* Right now, because we don't have a way to push the incoming skb's
1824 * straight through the UDP layer, the only header we need to worry
1825 * about is the L2TP header. This size is different depending on
1826 * whether sequence numbers are enabled for the data channel.
1828 po
->chan
.hdrlen
= PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
1830 po
->chan
.private = sk
;
1831 po
->chan
.ops
= &pppol2tp_chan_ops
;
1832 po
->chan
.mtu
= session
->mtu
;
1834 error
= ppp_register_channel(&po
->chan
);
1839 atomic_inc(&tunnel
->session_count
);
1840 sk
->sk_state
= PPPOX_CONNECTED
;
1841 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1842 "%s: created\n", session
->name
);
1848 PRINTK(session
? session
->debug
: -1, PPPOL2TP_MSG_CONTROL
, KERN_WARNING
,
1849 "%s: connect failed: %d\n", session
->name
, error
);
1856 /* getname() support.
1858 static int pppol2tp_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1859 int *usockaddr_len
, int peer
)
1861 int len
= sizeof(struct sockaddr_pppol2tp
);
1862 struct sockaddr_pppol2tp sp
;
1864 struct pppol2tp_session
*session
;
1869 if (sock
->sk
->sk_state
!= PPPOX_CONNECTED
)
1872 SOCK_2_SESSION(sock
->sk
, session
, error
, -EBADF
, end
, 0);
1874 sp
.sa_family
= AF_PPPOX
;
1875 sp
.sa_protocol
= PX_PROTO_OL2TP
;
1876 memcpy(&sp
.pppol2tp
, &session
->tunnel_addr
,
1877 sizeof(struct pppol2tp_addr
));
1879 memcpy(uaddr
, &sp
, len
);
1881 *usockaddr_len
= len
;
1889 /****************************************************************************
1892 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1893 * sockets. However, in order to control kernel tunnel features, we allow
1894 * userspace to create a special "tunnel" PPPoX socket which is used for
1895 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1896 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1898 ****************************************************************************/
1900 /* Session ioctl helper.
1902 static int pppol2tp_session_ioctl(struct pppol2tp_session
*session
,
1903 unsigned int cmd
, unsigned long arg
)
1907 struct sock
*sk
= session
->sock
;
1908 int val
= (int) arg
;
1912 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_DEBUG
,
1913 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1914 session
->name
, cmd
, arg
);
1919 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1923 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1925 ifr
.ifr_mtu
= session
->mtu
;
1926 if (copy_to_user((void __user
*) arg
, &ifr
, sizeof(struct ifreq
)))
1929 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1930 "%s: get mtu=%d\n", session
->name
, session
->mtu
);
1936 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1940 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1943 session
->mtu
= ifr
.ifr_mtu
;
1945 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1946 "%s: set mtu=%d\n", session
->name
, session
->mtu
);
1952 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1956 if (put_user(session
->mru
, (int __user
*) arg
))
1959 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1960 "%s: get mru=%d\n", session
->name
, session
->mru
);
1966 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1970 if (get_user(val
,(int __user
*) arg
))
1974 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1975 "%s: set mru=%d\n", session
->name
, session
->mru
);
1981 if (put_user(session
->flags
, (int __user
*) arg
))
1984 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1985 "%s: get flags=%d\n", session
->name
, session
->flags
);
1991 if (get_user(val
, (int __user
*) arg
))
1993 session
->flags
= val
;
1994 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1995 "%s: set flags=%d\n", session
->name
, session
->flags
);
1999 case PPPIOCGL2TPSTATS
:
2002 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
2005 if (copy_to_user((void __user
*) arg
, &session
->stats
,
2006 sizeof(session
->stats
)))
2008 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2009 "%s: get L2TP stats\n", session
->name
);
2023 /* Tunnel ioctl helper.
2025 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
2026 * specifies a session_id, the session ioctl handler is called. This allows an
2027 * application to retrieve session stats via a tunnel socket.
2029 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel
*tunnel
,
2030 unsigned int cmd
, unsigned long arg
)
2033 struct sock
*sk
= tunnel
->sock
;
2034 struct pppol2tp_ioc_stats stats_req
;
2038 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_DEBUG
,
2039 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel
->name
,
2043 case PPPIOCGL2TPSTATS
:
2046 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
2049 if (copy_from_user(&stats_req
, (void __user
*) arg
,
2050 sizeof(stats_req
))) {
2054 if (stats_req
.session_id
!= 0) {
2055 /* resend to session ioctl handler */
2056 struct pppol2tp_session
*session
=
2057 pppol2tp_session_find(tunnel
, stats_req
.session_id
);
2058 if (session
!= NULL
)
2059 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
2065 tunnel
->stats
.using_ipsec
= (sk
->sk_policy
[0] || sk
->sk_policy
[1]) ? 1 : 0;
2067 if (copy_to_user((void __user
*) arg
, &tunnel
->stats
,
2068 sizeof(tunnel
->stats
))) {
2072 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2073 "%s: get L2TP stats\n", tunnel
->name
);
2087 /* Main ioctl() handler.
2088 * Dispatch to tunnel or session helpers depending on the socket.
2090 static int pppol2tp_ioctl(struct socket
*sock
, unsigned int cmd
,
2093 struct sock
*sk
= sock
->sk
;
2094 struct pppol2tp_session
*session
;
2095 struct pppol2tp_tunnel
*tunnel
;
2103 if (sock_flag(sk
, SOCK_DEAD
) != 0)
2106 if ((sk
->sk_user_data
== NULL
) ||
2107 (!(sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
)))) {
2109 DPRINTK(-1, "ioctl: socket %p not connected.\n", sk
);
2113 SOCK_2_SESSION(sk
, session
, err
, -EBADF
, end
, 0);
2114 SOCK_2_TUNNEL(session
->tunnel_sock
, tunnel
, err
, -EBADF
, end
, 1);
2116 /* Special case: if session's session_id is zero, treat ioctl as a
2119 if ((session
->tunnel_addr
.s_session
== 0) &&
2120 (session
->tunnel_addr
.d_session
== 0)) {
2121 err
= pppol2tp_tunnel_ioctl(tunnel
, cmd
, arg
);
2125 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
2132 /*****************************************************************************
2133 * setsockopt() / getsockopt() support.
2135 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
2136 * sockets. In order to control kernel tunnel features, we allow userspace to
2137 * create a special "tunnel" PPPoX socket which is used for control only.
2138 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
2139 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
2140 *****************************************************************************/
2142 /* Tunnel setsockopt() helper.
2144 static int pppol2tp_tunnel_setsockopt(struct sock
*sk
,
2145 struct pppol2tp_tunnel
*tunnel
,
2146 int optname
, int val
)
2151 case PPPOL2TP_SO_DEBUG
:
2152 tunnel
->debug
= val
;
2153 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2154 "%s: set debug=%x\n", tunnel
->name
, tunnel
->debug
);
2165 /* Session setsockopt helper.
2167 static int pppol2tp_session_setsockopt(struct sock
*sk
,
2168 struct pppol2tp_session
*session
,
2169 int optname
, int val
)
2174 case PPPOL2TP_SO_RECVSEQ
:
2175 if ((val
!= 0) && (val
!= 1)) {
2179 session
->recv_seq
= val
? -1 : 0;
2180 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2181 "%s: set recv_seq=%d\n", session
->name
,
2185 case PPPOL2TP_SO_SENDSEQ
:
2186 if ((val
!= 0) && (val
!= 1)) {
2190 session
->send_seq
= val
? -1 : 0;
2192 /* FIXME: is it safe to change the ppp channel's
2193 * hdrlen on the fly?
2195 struct sock
*sk
= session
->sock
;
2196 struct pppox_sock
*po
= pppox_sk(sk
);
2197 po
->chan
.hdrlen
= val
? PPPOL2TP_L2TP_HDR_SIZE_SEQ
:
2198 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
2200 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2201 "%s: set send_seq=%d\n", session
->name
, session
->send_seq
);
2204 case PPPOL2TP_SO_LNSMODE
:
2205 if ((val
!= 0) && (val
!= 1)) {
2209 session
->lns_mode
= val
? -1 : 0;
2210 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2211 "%s: set lns_mode=%d\n", session
->name
,
2215 case PPPOL2TP_SO_DEBUG
:
2216 session
->debug
= val
;
2217 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2218 "%s: set debug=%x\n", session
->name
, session
->debug
);
2221 case PPPOL2TP_SO_REORDERTO
:
2222 session
->reorder_timeout
= MS_TO_JIFFIES(val
);
2223 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2224 "%s: set reorder_timeout=%d\n", session
->name
,
2225 session
->reorder_timeout
);
2236 /* Main setsockopt() entry point.
2237 * Does API checks, then calls either the tunnel or session setsockopt
2238 * handler, according to whether the PPPoL2TP socket is a for a regular
2239 * session or the special tunnel type.
2241 static int pppol2tp_setsockopt(struct socket
*sock
, int level
, int optname
,
2242 char *optval
, int optlen
)
2244 struct sock
*sk
= sock
->sk
;
2245 struct pppol2tp_session
*session
= sk
->sk_user_data
;
2246 struct pppol2tp_tunnel
*tunnel
;
2250 if (level
!= SOL_PPPOL2TP
)
2251 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
2253 if (optlen
<sizeof(int))
2256 if (get_user(val
, (int __user
*)optval
))
2259 if (sk
->sk_user_data
== NULL
) {
2261 DPRINTK(-1, "setsockopt: socket %p not connected.\n", sk
);
2265 SOCK_2_SESSION(sk
, session
, err
, -EBADF
, end
, 0);
2266 SOCK_2_TUNNEL(session
->tunnel_sock
, tunnel
, err
, -EBADF
, end
, 1);
2270 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2272 if ((session
->tunnel_addr
.s_session
== 0) &&
2273 (session
->tunnel_addr
.d_session
== 0))
2274 err
= pppol2tp_tunnel_setsockopt(sk
, tunnel
, optname
, val
);
2276 err
= pppol2tp_session_setsockopt(sk
, session
, optname
, val
);
2283 /* Tunnel getsockopt helper.
2285 static int pppol2tp_tunnel_getsockopt(struct sock
*sk
,
2286 struct pppol2tp_tunnel
*tunnel
,
2287 int optname
, int *val
)
2292 case PPPOL2TP_SO_DEBUG
:
2293 *val
= tunnel
->debug
;
2294 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2295 "%s: get debug=%x\n", tunnel
->name
, tunnel
->debug
);
2306 /* Session getsockopt helper.
2308 static int pppol2tp_session_getsockopt(struct sock
*sk
,
2309 struct pppol2tp_session
*session
,
2310 int optname
, int *val
)
2315 case PPPOL2TP_SO_RECVSEQ
:
2316 *val
= session
->recv_seq
;
2317 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2318 "%s: get recv_seq=%d\n", session
->name
, *val
);
2321 case PPPOL2TP_SO_SENDSEQ
:
2322 *val
= session
->send_seq
;
2323 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2324 "%s: get send_seq=%d\n", session
->name
, *val
);
2327 case PPPOL2TP_SO_LNSMODE
:
2328 *val
= session
->lns_mode
;
2329 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2330 "%s: get lns_mode=%d\n", session
->name
, *val
);
2333 case PPPOL2TP_SO_DEBUG
:
2334 *val
= session
->debug
;
2335 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2336 "%s: get debug=%d\n", session
->name
, *val
);
2339 case PPPOL2TP_SO_REORDERTO
:
2340 *val
= JIFFIES_TO_MS(session
->reorder_timeout
);
2341 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2342 "%s: get reorder_timeout=%d\n", session
->name
, *val
);
2352 /* Main getsockopt() entry point.
2353 * Does API checks, then calls either the tunnel or session getsockopt
2354 * handler, according to whether the PPPoX socket is a for a regular session
2355 * or the special tunnel type.
2357 static int pppol2tp_getsockopt(struct socket
*sock
, int level
,
2358 int optname
, char *optval
, int *optlen
)
2360 struct sock
*sk
= sock
->sk
;
2361 struct pppol2tp_session
*session
= sk
->sk_user_data
;
2362 struct pppol2tp_tunnel
*tunnel
;
2366 if (level
!= SOL_PPPOL2TP
)
2367 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
2369 if (get_user(len
, (int __user
*) optlen
))
2372 len
= min_t(unsigned int, len
, sizeof(int));
2377 if (sk
->sk_user_data
== NULL
) {
2379 DPRINTK(-1, "getsockopt: socket %p not connected.\n", sk
);
2383 /* Get the session and tunnel contexts */
2384 SOCK_2_SESSION(sk
, session
, err
, -EBADF
, end
, 0);
2385 SOCK_2_TUNNEL(session
->tunnel_sock
, tunnel
, err
, -EBADF
, end
, 1);
2387 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2388 if ((session
->tunnel_addr
.s_session
== 0) &&
2389 (session
->tunnel_addr
.d_session
== 0))
2390 err
= pppol2tp_tunnel_getsockopt(sk
, tunnel
, optname
, &val
);
2392 err
= pppol2tp_session_getsockopt(sk
, session
, optname
, &val
);
2395 if (put_user(len
, (int __user
*) optlen
))
2398 if (copy_to_user((void __user
*) optval
, &val
, len
))
2405 /*****************************************************************************
2406 * /proc filesystem for debug
2407 *****************************************************************************/
2409 #ifdef CONFIG_PROC_FS
2411 #include <linux/seq_file.h>
2413 static int pppol2tp_proc_open(struct inode
*inode
, struct file
*file
);
2414 static void *pppol2tp_proc_start(struct seq_file
*m
, loff_t
*_pos
);
2415 static void *pppol2tp_proc_next(struct seq_file
*p
, void *v
, loff_t
*pos
);
2416 static void pppol2tp_proc_stop(struct seq_file
*p
, void *v
);
2417 static int pppol2tp_proc_show(struct seq_file
*m
, void *v
);
2419 static struct proc_dir_entry
*pppol2tp_proc
;
2421 static struct seq_operations pppol2tp_proc_ops
= {
2422 .start
= pppol2tp_proc_start
,
2423 .next
= pppol2tp_proc_next
,
2424 .stop
= pppol2tp_proc_stop
,
2425 .show
= pppol2tp_proc_show
,
2428 static struct file_operations pppol2tp_proc_fops
= {
2429 .owner
= THIS_MODULE
,
2430 .open
= pppol2tp_proc_open
,
2432 .llseek
= seq_lseek
,
2433 .release
= seq_release
,
2436 static int pppol2tp_proc_open(struct inode
*inode
, struct file
*file
)
2442 ret
= seq_open(file
, &pppol2tp_proc_ops
);
2446 m
= file
->private_data
;
2447 m
->private = PDE(inode
)->data
;
2454 static void *pppol2tp_proc_start(struct seq_file
*m
, loff_t
*_pos
)
2456 struct pppol2tp_tunnel
*tunnel
= NULL
;
2458 struct list_head
*walk
;
2459 struct list_head
*tmp
;
2463 /* allow for the header line */
2470 /* find the n'th element in the list */
2471 list_for_each_safe(walk
, tmp
, &pppol2tp_tunnel_list
) {
2472 tunnel
= list_entry(walk
, struct pppol2tp_tunnel
, list
);
2474 sock_hold(tunnel
->sock
);
2486 static void *pppol2tp_proc_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
2488 struct pppol2tp_tunnel
*tunnel
= v
;
2489 struct list_head
*tmp
;
2490 struct list_head
*list
;
2497 list
= &pppol2tp_tunnel_list
;
2499 list
= &tunnel
->list
;
2502 if (tmp
== &pppol2tp_tunnel_list
)
2505 tunnel
= list_entry(tmp
, struct pppol2tp_tunnel
, list
);
2512 static void pppol2tp_proc_stop(struct seq_file
*p
, void *v
)
2514 struct pppol2tp_tunnel
*tunnel
= v
;
2519 sock_put(tunnel
->sock
);
2524 static int pppol2tp_proc_show(struct seq_file
*m
, void *v
)
2526 struct pppol2tp_tunnel
*tunnel
= v
;
2527 struct pppol2tp_session
*session
;
2528 struct hlist_node
*walk
;
2529 struct hlist_node
*tmp
;
2534 /* display header on line 1 */
2535 if (v
== (void *)1) {
2536 seq_puts(m
, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION
"\n");
2537 seq_puts(m
, "TUNNEL name, user-data-ok "
2538 "session-count magic-ok\n");
2539 seq_puts(m
, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2540 seq_puts(m
, " SESSION name, addr/port src-tid/sid "
2541 "dest-tid/sid state user-data-ok magic-ok\n");
2542 seq_puts(m
, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2543 seq_puts(m
, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2547 seq_printf(m
, "TUNNEL '%s', %c %d MAGIC %s\n",
2549 (tunnel
== tunnel
->sock
->sk_user_data
) ? 'Y':'N',
2550 atomic_read(&tunnel
->session_count
),
2551 (tunnel
->magic
== L2TP_TUNNEL_MAGIC
) ? "OK" : "BAD");
2552 seq_printf(m
, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2554 tunnel
->stats
.tx_packets
, tunnel
->stats
.tx_bytes
,
2555 tunnel
->stats
.tx_errors
,
2556 tunnel
->stats
.rx_packets
, tunnel
->stats
.rx_bytes
,
2557 tunnel
->stats
.rx_errors
);
2559 if (tunnel
->magic
!= L2TP_TUNNEL_MAGIC
) {
2560 seq_puts(m
, "*** Aborting ***\n");
2564 for (i
= 0; i
< PPPOL2TP_HASH_SIZE
; i
++) {
2565 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[i
]) {
2566 session
= hlist_entry(walk
, struct pppol2tp_session
, hlist
);
2567 seq_printf(m
, " SESSION '%s' %08X/%d %04X/%04X -> "
2568 "%04X/%04X %d %c MAGIC %s\n",
2570 htonl(session
->tunnel_addr
.addr
.sin_addr
.s_addr
),
2571 htons(session
->tunnel_addr
.addr
.sin_port
),
2572 session
->tunnel_addr
.s_tunnel
,
2573 session
->tunnel_addr
.s_session
,
2574 session
->tunnel_addr
.d_tunnel
,
2575 session
->tunnel_addr
.d_session
,
2576 session
->sock
->sk_state
,
2577 (session
== session
->sock
->sk_user_data
) ?
2579 (session
->magic
== L2TP_SESSION_MAGIC
) ?
2582 seq_printf(m
, " %d/%d/%c/%c/%s %08x %d\n",
2583 session
->mtu
, session
->mru
,
2584 session
->recv_seq
? 'R' : '-',
2585 session
->send_seq
? 'S' : '-',
2586 session
->lns_mode
? "LNS" : "LAC",
2588 JIFFIES_TO_MS(session
->reorder_timeout
));
2589 seq_printf(m
, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2590 session
->nr
, session
->ns
,
2591 session
->stats
.tx_packets
,
2592 session
->stats
.tx_bytes
,
2593 session
->stats
.tx_errors
,
2594 session
->stats
.rx_packets
,
2595 session
->stats
.rx_bytes
,
2596 session
->stats
.rx_errors
);
2598 if (session
->magic
!= L2TP_SESSION_MAGIC
) {
2599 seq_puts(m
, "*** Aborting ***\n");
2612 #endif /* CONFIG_PROC_FS */
2614 /*****************************************************************************
2616 *****************************************************************************/
2618 static struct proto_ops pppol2tp_ops
= {
2620 .owner
= THIS_MODULE
,
2621 .release
= pppol2tp_release
,
2622 .bind
= sock_no_bind
,
2623 .connect
= pppol2tp_connect
,
2624 .socketpair
= sock_no_socketpair
,
2625 .accept
= sock_no_accept
,
2626 .getname
= pppol2tp_getname
,
2627 .poll
= datagram_poll
,
2628 .listen
= sock_no_listen
,
2629 .shutdown
= sock_no_shutdown
,
2630 .setsockopt
= pppol2tp_setsockopt
,
2631 .getsockopt
= pppol2tp_getsockopt
,
2632 .sendmsg
= pppol2tp_sendmsg
,
2633 .recvmsg
= pppol2tp_recvmsg
,
2634 .mmap
= sock_no_mmap
,
2635 .ioctl
= pppox_ioctl
,
2638 struct pppox_proto pppol2tp_proto
= {
2639 .create
= pppol2tp_create
,
2640 .ioctl
= pppol2tp_ioctl
2643 int __init
pppol2tp_init(void)
2647 err
= proto_register(&pppol2tp_sk_proto
, 0);
2650 err
= register_pppox_proto(PX_PROTO_OL2TP
, &pppol2tp_proto
);
2652 goto out_unregister_pppol2tp_proto
;
2654 #ifdef CONFIG_PROC_FS
2655 pppol2tp_proc
= create_proc_entry("pppol2tp", 0, proc_net
);
2656 if (!pppol2tp_proc
) {
2658 goto out_unregister_pppox_proto
;
2660 pppol2tp_proc
->proc_fops
= &pppol2tp_proc_fops
;
2661 #endif /* CONFIG_PROC_FS */
2662 printk(KERN_INFO
"PPPoL2TP kernel driver, %s\n",
2663 PPPOL2TP_DRV_VERSION
);
2668 out_unregister_pppox_proto
:
2669 unregister_pppox_proto(PX_PROTO_OL2TP
);
2670 out_unregister_pppol2tp_proto
:
2671 proto_unregister(&pppol2tp_sk_proto
);
2675 void __exit
pppol2tp_exit(void)
2677 unregister_pppox_proto(PX_PROTO_OL2TP
);
2679 #ifdef CONFIG_PROC_FS
2680 remove_proc_entry("pppol2tp", proc_net
);
2682 proto_unregister(&pppol2tp_sk_proto
);
2685 module_init(pppol2tp_init
);
2686 module_exit(pppol2tp_exit
);
2688 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>");
2689 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2690 MODULE_LICENSE("GPL");
2691 MODULE_VERSION(PPPOL2TP_DRV_VERSION
);