1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
10 * 230411 : Added ASUS backports from 2.6.3x.
11 * 251003 : Copied from pppoe.c version 0.6.9.
13 * Authors: Martijn van Oosterhout <kleptog@svana.org>
14 * James Chapman (jchapman@katalix.com)
16 * Michal Ostrowski <mostrows@speakeasy.net>
17 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
18 * David S. Miller (davem@redhat.com)
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License
23 * as published by the Free Software Foundation; either version
24 * 2 of the License, or (at your option) any later version.
28 /* This driver handles only L2TP data frames; control frames are handled by a
29 * userspace application.
31 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
32 * attaches it to a bound UDP socket with local tunnel_id / session_id and
33 * peer tunnel_id / session_id set. Data can then be sent or received using
34 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
35 * can be read or modified using ioctl() or [gs]etsockopt() calls.
37 * When a PPPoL2TP socket is connected with local and peer session_id values
38 * zero, the socket is treated as a special tunnel management socket.
40 * Here's example userspace code to create a socket for sending/receiving data
41 * over an L2TP session:-
43 * struct sockaddr_pppol2tp sax;
47 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
49 * sax.sa_family = AF_PPPOX;
50 * sax.sa_protocol = PX_PROTO_OL2TP;
51 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
52 * sax.pppol2tp.pid = 0; // current pid owns UDP socket
53 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
54 * sax.pppol2tp.addr.sin_port = addr->sin_port;
55 * sax.pppol2tp.addr.sin_family = AF_INET;
56 * sax.pppol2tp.s_tunnel = tunnel_id;
57 * sax.pppol2tp.s_session = session_id;
58 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
59 * sax.pppol2tp.d_session = peer_session_id;
61 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
65 #include <linux/module.h>
66 #include <linux/version.h>
67 #include <linux/string.h>
68 #include <linux/list.h>
69 #include <asm/uaccess.h>
71 #include <linux/kernel.h>
72 #include <linux/spinlock.h>
73 #include <linux/kthread.h>
74 #include <linux/sched.h>
75 #include <linux/slab.h>
76 #include <linux/errno.h>
78 #include <linux/netdevice.h>
79 #include <linux/net.h>
80 #include <linux/inetdevice.h>
81 #include <linux/skbuff.h>
82 #include <linux/init.h>
83 #include <linux/udp.h>
84 #include <linux/if_pppox.h>
85 #include <linux/if_pppol2tp.h>
87 #include <linux/ppp_channel.h>
88 #include <linux/ppp_defs.h>
89 #include <linux/if_ppp.h>
90 #include <linux/file.h>
91 #include <linux/hash.h>
92 #include <linux/proc_fs.h>
98 #include <asm/byteorder.h>
99 #include <asm/atomic.h>
101 #define PPPOL2TP_DRV_VERSION "V0.18.3"
103 /* Developer debug code. */
105 #define DEBUG /* Define to compile in very verbose developer debug */
108 /* Old L2TP daemons semi-compatibility */
109 //#define PPPOL2TP_UDP_CONNECT
111 /* Pre 2.6.22 kernels compatibility */
112 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
113 #define udp_hdr(skb) (struct udphdr *) (skb)->h.raw
114 #define skb_network_header(skb) skb->nh.raw
115 #define skb_transport_header(skb) skb->h.raw
116 #define skb_reset_network_header(skb) (skb)->nh.raw = (skb)->data
117 #define skb_reset_transport_header(skb) (skb)->h.raw = (skb)->data
120 /* Timeouts are specified in milliseconds to/from userspace */
121 #define JIFFIES_TO_MS(t) ((t) * 1000 / HZ)
122 #define MS_TO_JIFFIES(j) ((j * HZ) / 1000)
124 /* L2TP header constants */
125 #define L2TP_HDRFLAG_T 0x8000
126 #define L2TP_HDRFLAG_L 0x4000
127 #define L2TP_HDRFLAG_S 0x0800
128 #define L2TP_HDRFLAG_O 0x0200
129 #define L2TP_HDRFLAG_P 0x0100
131 #define L2TP_HDR_VER_MASK 0x000F
132 #define L2TP_HDR_VER 0x0002
134 /* Space for IP, UDP, L2TP and PPP headers */
135 #define PPPOL2TP_HEADER_OVERHEAD 40
137 /* Just some random numbers */
138 #define L2TP_TUNNEL_MAGIC 0x42114DDA
139 #define L2TP_SESSION_MAGIC 0x0C04EB7D
141 #define PPPOL2TP_HASH_BITS 4
142 #define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
144 /* Default trace flags */
146 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS -1
148 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
152 /* Debug kernel message control.
153 * Verbose debug messages (L2TP_MSG_DEBUG flag) are optionally compiled in.
156 #define DPRINTK(_mask, _fmt, args...) \
158 if ((_mask) & PPPOL2TP_MSG_DEBUG) \
159 printk(KERN_DEBUG "PPPOL2TP %s: " _fmt, \
160 __FUNCTION__, ##args); \
163 #define DPRINTK(_mask, _fmt, args...) do { } while(0)
166 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
168 if ((_mask) & (_type)) \
169 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
172 /* Extra driver debug. Should only be enabled by developers working on
176 #define ENTER_FUNCTION printk(KERN_DEBUG "PPPOL2TP: --> %s\n", __FUNCTION__)
177 #define EXIT_FUNCTION printk(KERN_DEBUG "PPPOL2TP: <-- %s\n", __FUNCTION__)
179 #define ENTER_FUNCTION do { } while(0)
180 #define EXIT_FUNCTION do { } while(0)
183 /* Number of bytes to build transmit L2TP headers.
184 * Unfortunately the size is different depending on whether sequence numbers
187 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 12
188 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 8
190 struct pppol2tp_tunnel
;
192 /* Describes a session. It is the sk_user_data field in the PPPoL2TP
193 * socket. Contains information to determine incoming packets and transmit
196 struct pppol2tp_session
198 int magic
; /* should be
199 * L2TP_SESSION_MAGIC */
200 int owner
; /* pid that opened the socket */
202 struct sock
*sock
; /* Pointer to the session
204 struct sock
*tunnel_sock
; /* Pointer to the tunnel UDP
207 struct pppol2tp_addr tunnel_addr
; /* Description of tunnel */
209 struct pppol2tp_tunnel
*tunnel
; /* back pointer to tunnel
212 char name
[20]; /* "sess xxxxx/yyyyy", where
213 * x=tunnel_id, y=session_id */
216 int flags
; /* accessed by PPPIOCGFLAGS.
218 unsigned recv_seq
:1; /* expect receive packets with
219 * sequence numbers? */
220 unsigned send_seq
:1; /* send packets with sequence
222 unsigned lns_mode
:1; /* behave as LNS? LAC enables
223 * sequence numbers under
225 int debug
; /* bitmask of debug message
227 int reorder_timeout
;/* configured reorder timeout
229 int reorder_skip
; /* set if skip to next nr */
230 u16 nr
; /* session NR state (receive) */
231 u16 ns
; /* session NR state (send) */
232 struct sk_buff_head reorder_q
; /* receive reorder queue */
233 struct pppol2tp_ioc_stats stats
;
234 struct hlist_node hlist
; /* Hash list node */
237 /* The sk_user_data field of the tunnel's UDP socket. It contains info to track
238 * all the associated sessions so incoming packets can be sorted out
240 struct pppol2tp_tunnel
242 int magic
; /* Should be L2TP_TUNNEL_MAGIC */
244 rwlock_t hlist_lock
; /* protect session_hlist */
245 struct hlist_head session_hlist
[PPPOL2TP_HASH_SIZE
];
246 /* hashed list of sessions,
248 int debug
; /* bitmask of debug message
250 char name
[12]; /* "tunl xxxxx" */
251 struct pppol2tp_ioc_stats stats
;
253 #ifndef UDP_ENCAP_L2TPINUDP
254 void (*old_data_ready
)(struct sock
*, int);
256 void (*old_sk_destruct
)(struct sock
*);
258 struct sock
*sock
; /* Parent socket */
259 struct list_head list
; /* Keep a list of all open
260 * prepared sockets */
262 atomic_t session_count
;
265 /* Private data stored for received packets in the skb.
267 struct pppol2tp_skb_cb
{
272 unsigned long expires
;
275 #define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
277 static int pppol2tp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
);
279 static struct ppp_channel_ops pppol2tp_chan_ops
= { pppol2tp_xmit
, NULL
};
280 static struct proto_ops pppol2tp_ops
;
281 static LIST_HEAD(pppol2tp_tunnel_list
);
283 /* Macros to derive session/tunnel context pointers from a socket. */
284 #define SOCK_2_SESSION(sock, session, err, errval, label, quiet) \
285 session = (struct pppol2tp_session *)((sock)->sk_user_data); \
286 if (!session || session->magic != L2TP_SESSION_MAGIC) { \
288 printk(KERN_ERR "%s: %s:%d: BAD SESSION MAGIC " \
289 "(" #sock "=%p) session=%p magic=%x\n", \
290 __FUNCTION__, __FILE__, __LINE__, sock, \
291 session, session ? session->magic : 0); \
296 #define SOCK_2_TUNNEL(sock, tunnel, err, errval, label, quiet) \
297 tunnel = (struct pppol2tp_tunnel *)((sock)->sk_user_data); \
298 if (!tunnel || tunnel->magic != L2TP_TUNNEL_MAGIC) { \
300 printk(KERN_ERR "%s: %s:%d: BAD TUNNEL MAGIC " \
301 "(" #sock "=%p) tunnel=%p magic=%x\n", \
302 __FUNCTION__, __FILE__, __LINE__, sock, \
303 tunnel, tunnel ? tunnel->magic : 0); \
308 /* Session hash list.
309 * The session_id SHOULD be random according to RFC2661, but several
310 * L2TP implementations (Cisco and Microsoft) use incrementing
311 * session_ids. So we do a real hash on the session_id, rather than a
314 static inline struct hlist_head
*
315 pppol2tp_session_id_hash(struct pppol2tp_tunnel
*tunnel
, u16 session_id
)
317 unsigned long hash_val
= (unsigned long) session_id
;
318 return &tunnel
->session_hlist
[hash_long(hash_val
, PPPOL2TP_HASH_BITS
)];
321 /* Lookup a session by id
323 static struct pppol2tp_session
*
324 pppol2tp_session_find(struct pppol2tp_tunnel
*tunnel
, u16 session_id
)
326 struct hlist_head
*session_list
=
327 pppol2tp_session_id_hash(tunnel
, session_id
);
328 struct hlist_node
*tmp
;
329 struct hlist_node
*walk
;
330 struct pppol2tp_session
*session
;
332 //TODO: Switch to RCU
333 read_lock_bh(&tunnel
->hlist_lock
);
334 hlist_for_each_safe(walk
, tmp
, session_list
) {
335 session
= hlist_entry(walk
, struct pppol2tp_session
, hlist
);
336 if (session
->tunnel_addr
.s_session
== session_id
) {
337 read_unlock_bh(&tunnel
->hlist_lock
);
341 read_unlock_bh(&tunnel
->hlist_lock
);
346 /*****************************************************************************
347 * Receive data handling
348 *****************************************************************************/
350 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
353 static void pppol2tp_recv_queue_skb(struct pppol2tp_session
*session
, struct sk_buff
*skb
)
355 struct sk_buff
*skbp
;
357 u16 ns
= PPPOL2TP_SKB_CB(skb
)->ns
;
361 spin_lock(&session
->reorder_q
.lock
);
362 skb_queue_walk_safe(&session
->reorder_q
, skbp
, tmp
) {
363 if (PPPOL2TP_SKB_CB(skbp
)->ns
> ns
) {
364 __skb_insert(skb
, skbp
->prev
, skbp
, &session
->reorder_q
);
365 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
366 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
367 session
->name
, ns
, PPPOL2TP_SKB_CB(skbp
)->ns
,
368 skb_queue_len(&session
->reorder_q
));
369 session
->stats
.rx_oos_packets
++;
374 __skb_queue_tail(&session
->reorder_q
, skb
);
377 spin_unlock(&session
->reorder_q
.lock
);
381 /* Dequeue a single skb, passing it either to ppp or to userspace.
383 static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session
*session
, struct sk_buff
*skb
)
385 struct pppol2tp_tunnel
*tunnel
= session
->tunnel
;
386 int length
= PPPOL2TP_SKB_CB(skb
)->length
;
387 struct sock
*session_sock
= NULL
;
391 /* We're about to requeue the skb, so return resources
392 * to its current owner (a socket receive buffer).
396 tunnel
->stats
.rx_packets
++;
397 tunnel
->stats
.rx_bytes
+= length
;
398 session
->stats
.rx_packets
++;
399 session
->stats
.rx_bytes
+= length
;
401 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
404 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
405 "%s: updated nr to %hu\n", session
->name
, session
->nr
);
408 /* If the socket is bound, send it in to PPP's input queue. Otherwise
409 * queue it on the session socket.
411 session_sock
= session
->sock
;
412 if (session_sock
->sk_state
& PPPOX_BOUND
) {
413 struct pppox_sock
*po
;
414 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
415 "%s: recv %d byte data frame, passing to ppp\n",
416 session
->name
, length
);
418 /* We need to forget all info related to the L2TP packet
419 * gathered in the skb as we are going to reuse the same
420 * skb for the inner packet.
422 * - reset xfrm (IPSec) information as it applies to
423 * the outer L2TP packet and not to the inner one
424 * - release the dst to force a route lookup on the inner
425 * IP packet since skb->dst currently points to the dst
427 * - reset netfilter information as it doesn't apply
428 * to the inner packet either
431 dst_release(skb
->dst
);
435 po
= pppox_sk(session_sock
);
436 ppp_input(&po
->chan
, skb
);
438 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
439 "%s: socket not bound\n", session
->name
);
440 #ifndef UDP_ENCAP_L2TPINUDP
441 /* Not bound. Queue it now */
442 if (sock_queue_rcv_skb(session_sock
, skb
) < 0) {
443 session
->stats
.rx_errors
++;
445 if (!sock_flag(session_sock
, SOCK_DEAD
))
446 session_sock
->sk_data_ready(session_sock
, 0);
449 /* Not bound. Nothing we can do, so discard. */
450 session
->stats
.rx_errors
++;
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
)
471 /* If the pkt at the head of the queue has the nr that we
472 * expect to send up next, dequeue it and any other
473 * in-sequence packets behind it.
475 spin_lock(&session
->reorder_q
.lock
);
476 skb_queue_walk_safe(&session
->reorder_q
, skb
, tmp
) {
477 if (time_after(jiffies
, PPPOL2TP_SKB_CB(skb
)->expires
)) {
478 session
->stats
.rx_seq_discards
++;
479 session
->stats
.rx_errors
++;
480 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
481 "%s: oos pkt %hu len %d discarded (too old), "
482 "waiting for %hu, reorder_q_len=%d\n",
483 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
484 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
485 skb_queue_len(&session
->reorder_q
));
486 session
->reorder_skip
= 1;
487 __skb_unlink(skb
, &session
->reorder_q
);
489 sock_put(session
->sock
);
493 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
494 if (session
->reorder_skip
) {
495 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
496 "%s: advancing nr to next pkt: %hu -> %hu\n",
497 session
->name
, session
->nr
,
498 PPPOL2TP_SKB_CB(skb
)->ns
);
499 session
->reorder_skip
= 0;
500 session
->nr
= PPPOL2TP_SKB_CB(skb
)->ns
;
502 if (PPPOL2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
503 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
504 "%s: holding oos pkt %hu len %d, "
505 "waiting for %hu, reorder_q_len=%d\n",
506 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
507 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
508 skb_queue_len(&session
->reorder_q
));
512 __skb_unlink(skb
, &session
->reorder_q
);
514 /* Process the skb. We release the queue lock while we
515 * do so to let other contexts process the queue.
517 spin_unlock(&session
->reorder_q
.lock
);
518 pppol2tp_recv_dequeue_skb(session
, skb
);
519 spin_lock(&session
->reorder_q
.lock
);
523 spin_unlock(&session
->reorder_q
.lock
);
527 /* Internal receive frame. Do the real work of receiving an L2TP data frame
528 * here. The skb is not on a list when we get here.
529 * Returns 0 if the packet was a data packet and was successfully passed on.
530 * Returns 1 if the packet was not a good data packet and could not be
531 * forwarded. All such packets are passed up to userspace to deal with.
533 static int pppol2tp_recv_core(struct sock
*sock
, struct sk_buff
*skb
)
535 struct pppol2tp_session
*session
= NULL
;
536 struct pppol2tp_tunnel
*tunnel
;
538 unsigned char *ptr
, *optr
;
540 u16 tunnel_id
, session_id
;
546 SOCK_2_TUNNEL(sock
, tunnel
, error
, 1, error
, 0);
548 if (skb
->pkt_type
!= PACKET_HOST
)
551 /* UDP always verifies the packet length. */
552 __skb_pull(skb
, sizeof(struct udphdr
));
555 if (!pskb_may_pull(skb
, 12)) {
556 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
557 "%s: recv short packet (len=%d)\n", tunnel
->name
, skb
->len
);
561 /* Point to L2TP header */
562 optr
= ptr
= skb
->data
;
564 /* Get length of L2TP packet */
567 /* Trace packet contents, if enabled */
568 if (tunnel
->debug
& PPPOL2TP_MSG_DATA
) {
570 unsigned char *datap
= skb
->data
;
572 printk(KERN_DEBUG
"%s: recv:", tunnel
->name
);
573 for (i
= 0; i
< length
; i
++) {
574 printk(" %02X", *datap
++);
583 /* Get L2TP header flags */
584 hdrflags
= ntohs(*(u16
*)ptr
);
586 /* If type is control packet, it is handled by userspace. */
587 if (hdrflags
& L2TP_HDRFLAG_T
) {
588 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
589 "%s: recv control packet, len=%d\n", tunnel
->name
, length
);
596 /* If length is present, skip it */
597 if (hdrflags
& L2TP_HDRFLAG_L
)
600 /* Extract tunnel and session ID */
601 tunnel_id
= ntohs(*(u16
*) ptr
);
603 session_id
= ntohs(*(u16
*) ptr
);
606 /* Find the session context */
607 session
= pppol2tp_session_find(tunnel
, session_id
);
609 /* Not found? Pass to userspace to deal with */
610 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_INFO
,
611 "%s: no socket found (%hu/%hu). Passing up.\n",
612 tunnel
->name
, tunnel_id
, session_id
);
615 sock_hold(session
->sock
);
617 DPRINTK(session
->debug
, "%s: socket rcvbuf alloc=%d\n",
618 session
->name
, atomic_read(&sock
->sk_rmem_alloc
));
620 /* The ref count on the socket was increased by the above call since
621 * we now hold a pointer to the session. Take care to do sock_put()
622 * when exiting this function from now on...
625 /* Handle the optional sequence numbers. If we are the LAC,
626 * enable/disable sequence numbers under the control of the LNS. If
627 * no sequence numbers present but we were expecting them, discard
630 if (hdrflags
& L2TP_HDRFLAG_S
) {
632 ns
= ntohs(*(u16
*) ptr
);
634 nr
= ntohs(*(u16
*) ptr
);
637 /* Received a packet with sequence numbers. If we're the LNS,
638 * check if we sre sending sequence numbers and if not,
641 if ((!session
->lns_mode
) && (!session
->send_seq
)) {
642 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_INFO
,
643 "%s: requested to enable seq numbers by LNS\n",
645 session
->send_seq
= -1;
648 /* Store L2TP info in the skb */
649 PPPOL2TP_SKB_CB(skb
)->ns
= ns
;
650 PPPOL2TP_SKB_CB(skb
)->nr
= nr
;
651 PPPOL2TP_SKB_CB(skb
)->has_seq
= 1;
653 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
654 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
655 session
->name
, ns
, nr
, session
->nr
);
657 /* No sequence numbers.
658 * If user has configured mandatory sequence numbers, discard.
660 if (session
->recv_seq
) {
661 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_WARNING
,
662 "%s: recv data has no seq numbers when required. "
663 "Discarding\n", session
->name
);
664 session
->stats
.rx_seq_discards
++;
668 /* If we're the LAC and we're sending sequence numbers, the
669 * LNS has requested that we no longer send sequence numbers.
670 * If we're the LNS and we're sending sequence numbers, the
671 * LAC is broken. Discard the frame.
673 if ((!session
->lns_mode
) && (session
->send_seq
)) {
674 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_INFO
,
675 "%s: requested to disable seq numbers by LNS\n",
677 session
->send_seq
= 0;
678 } else if (session
->send_seq
) {
679 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_WARNING
,
680 "%s: recv data has no seq numbers when required. "
681 "Discarding\n", session
->name
);
682 session
->stats
.rx_seq_discards
++;
686 /* Store L2TP info in the skb */
687 PPPOL2TP_SKB_CB(skb
)->has_seq
= 0;
690 /* If offset bit set, skip it. */
691 if (hdrflags
& L2TP_HDRFLAG_O
)
692 ptr
+= 2 + ntohs(*(u16
*) ptr
);
695 if (!pskb_may_pull(skb
, offset
))
698 __skb_pull(skb
, offset
);
700 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
701 * don't send the PPP header (PPP header compression enabled), but
702 * other clients can include the header. So we cope with both cases
703 * here. The PPP header is always FF03 when using L2TP.
705 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
706 * the field may be unaligned.
708 if (!pskb_may_pull(skb
, 2))
711 if ((skb
->data
[0] == PPP_ALLSTATIONS
) && (skb
->data
[1] == PPP_UI
))
714 /* Prepare skb for adding to the session's reorder_q. Hold
715 * packets for max reorder_timeout or 1 second if not
718 PPPOL2TP_SKB_CB(skb
)->length
= length
;
719 PPPOL2TP_SKB_CB(skb
)->expires
= jiffies
+
720 (session
->reorder_timeout
? session
->reorder_timeout
: HZ
);
722 /* Add packet to the session's receive queue. Reordering is done here, if
723 * enabled. Saved L2TP protocol info is stored in skb->sb[].
725 if (PPPOL2TP_SKB_CB(skb
)->has_seq
) {
726 if (session
->reorder_timeout
!= 0) {
727 /* Packet reordering enabled. Add skb to session's
728 * reorder queue, in order of ns.
730 pppol2tp_recv_queue_skb(session
, skb
);
732 /* Packet reordering disabled. Discard out-of-sequence
735 if (PPPOL2TP_SKB_CB(skb
)->ns
!= session
->nr
) {
736 session
->stats
.rx_seq_discards
++;
737 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
738 "%s: oos pkt %hu len %d discarded, "
739 "waiting for %hu, reorder_q_len=%d\n",
740 session
->name
, PPPOL2TP_SKB_CB(skb
)->ns
,
741 PPPOL2TP_SKB_CB(skb
)->length
, session
->nr
,
742 skb_queue_len(&session
->reorder_q
));
745 skb_queue_tail(&session
->reorder_q
, skb
);
748 /* No sequence numbers. Add the skb to the tail of the
749 * reorder queue. This ensures that it will be
750 * delivered after all previous sequenced skbs.
752 skb_queue_tail(&session
->reorder_q
, skb
);
755 /* Try to dequeue as many skbs from reorder_q as we can. */
756 pppol2tp_recv_dequeue(session
);
762 DPRINTK(session
->debug
, "discarding skb, len=%d\n", skb
->len
);
763 session
->stats
.rx_errors
++;
766 DPRINTK(session
->debug
, "calling sock_put; refcnt=%d\n",
767 session
->sock
->sk_refcnt
.counter
);
768 sock_put(session
->sock
);
774 /* Put UDP header back */
775 __skb_push(skb
, sizeof(struct udphdr
));
782 #ifndef UDP_ENCAP_L2TPINUDP
783 /* The data_ready hook on the UDP socket. Scan the incoming packet list for
784 * packets to process. Only control or bad data packets are delivered to
787 static void pppol2tp_data_ready(struct sock
*sk
, int len
)
790 struct pppol2tp_tunnel
*tunnel
;
794 SOCK_2_TUNNEL(sk
, tunnel
, err
, -EBADF
, end
, 0);
796 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
797 "%s: received %d bytes\n", tunnel
->name
, len
);
799 skb
= skb_dequeue(&sk
->sk_receive_queue
);
801 if (pppol2tp_recv_core(sk
, skb
)) {
802 DPRINTK(tunnel
->debug
, "%s: packet passing to userspace\n",
804 skb_queue_head(&sk
->sk_receive_queue
, skb
);
805 tunnel
->old_data_ready(sk
, len
);
807 DPRINTK(tunnel
->debug
, "%s: data packet received\n",
816 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
820 * >0: skb should be passed up to userspace as UDP.
822 static int pppol2tp_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
825 struct pppol2tp_tunnel
*tunnel
;
828 SOCK_2_TUNNEL(sk
, tunnel
, err
, -EBADF
, pass_up
, 0);
830 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
831 "%s: received %d bytes\n", tunnel
->name
, skb
->len
);
833 if (pppol2tp_recv_core(sk
, skb
))
845 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
847 static int pppol2tp_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
848 struct msghdr
*msg
, size_t len
,
853 struct sock
*sk
= sock
->sk
;
858 if (sk
->sk_state
& PPPOX_BOUND
)
861 msg
->msg_namelen
= 0;
864 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
865 flags
& MSG_DONTWAIT
, &err
);
871 else if (len
< skb
->len
)
872 msg
->msg_flags
|= MSG_TRUNC
;
874 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, len
);
875 if (likely(err
== 0))
886 /************************************************************************
888 ***********************************************************************/
890 /* Tell how big L2TP headers are for a particular session. This
891 * depends on whether sequence numbers are being used.
893 static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session
*session
)
895 if (session
->send_seq
)
896 return PPPOL2TP_L2TP_HDR_SIZE_SEQ
;
898 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
901 /* Build an L2TP header for the session into the buffer provided.
903 static inline void pppol2tp_build_l2tp_header(struct pppol2tp_session
*session
,
907 u16 flags
= L2TP_HDR_VER
| L2TP_HDRFLAG_O
;
909 if (session
->send_seq
)
910 flags
|= L2TP_HDRFLAG_S
;
912 /* Setup L2TP header.
913 * FIXME: Can this ever be unaligned? Is direct dereferencing of
914 * 16-bit header fields safe here for all architectures?
916 *bufp
++ = htons(flags
);
917 *bufp
++ = htons(session
->tunnel_addr
.d_tunnel
);
918 *bufp
++ = htons(session
->tunnel_addr
.d_session
);
919 if (session
->send_seq
) {
920 *bufp
++ = htons(session
->ns
);
923 PRINTK(session
->debug
, PPPOL2TP_MSG_SEQ
, KERN_DEBUG
,
924 "%s: updated ns to %hu\n", session
->name
, session
->ns
);
929 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
930 * when a user application does a sendmsg() on the session socket. L2TP and
931 * PPP headers must be inserted into the user's data.
933 static int pppol2tp_sendmsg(struct kiocb
*iocb
, struct socket
*sock
, struct msghdr
*m
,
936 struct pppol2tp_session
*session
;
937 struct pppol2tp_tunnel
*tunnel
;
938 struct sock
*sk
= sock
->sk
;
940 struct dst_entry
*dst
;
941 struct inet_sock
*inet
;
951 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
)) {
956 /* Get session and tunnel contexts */
957 SOCK_2_SESSION(sk
, session
, error
, -EBADF
, end
, 0);
958 sk_tun
= session
->tunnel_sock
;
959 SOCK_2_TUNNEL(sk_tun
, tunnel
, error
, -EBADF
, end
, 0);
961 /* What header length is configured for this session? */
962 hdr_len
= pppol2tp_l2tp_header_len(session
);
964 /* Allocate a socket buffer */
965 dst
= __sk_dst_get(sk_tun
);
966 skb
= sock_wmalloc(sk_tun
,
967 (dst
? LL_RESERVED_SPACE(dst
->dev
) : NET_SKB_PAD
) +
968 sizeof(struct iphdr
) +
969 sizeof(struct udphdr
) + hdr_len
+ 2 + total_len
,
976 /* Reserve space for headers. */
977 skb_reserve(skb
, dst
? LL_RESERVED_SPACE(dst
->dev
) : NET_SKB_PAD
);
978 skb_reset_network_header(skb
);
979 skb_reserve(skb
, sizeof(struct iphdr
));
980 skb_reset_transport_header(skb
);
982 /* Build UDP header */
983 inet
= inet_sk(sk_tun
);
984 udp_len
= sizeof(struct udphdr
) + hdr_len
+ 2 + total_len
;
985 uh
= (struct udphdr
*) skb
->data
;
986 uh
->source
= inet
->sport
;
987 uh
->dest
= inet
->dport
;
988 uh
->len
= htons(udp_len
);
990 skb_put(skb
, sizeof(struct udphdr
));
992 /* Build L2TP header */
993 pppol2tp_build_l2tp_header(session
, skb
->data
);
994 skb_put(skb
, hdr_len
);
997 skb
->data
[0] = PPP_ALLSTATIONS
;
998 skb
->data
[1] = PPP_UI
;
1001 /* Copy user data into skb */
1002 error
= memcpy_fromiovec(skb
->data
, m
->msg_iov
, total_len
);
1007 skb_put(skb
, total_len
);
1009 /* Calculate UDP checksum if configured to do so */
1010 skb
->ip_summed
= CHECKSUM_NONE
;
1012 if (session
->send_seq
)
1013 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1014 "%s: send %d bytes, ns=%hu\n", session
->name
,
1015 total_len
, session
->ns
- 1);
1017 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1018 "%s: send %d bytes\n", session
->name
, total_len
);
1020 if (session
->debug
& PPPOL2TP_MSG_DATA
) {
1022 unsigned char *datap
= skb
->data
- hdr_len
- 2;
1024 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
1025 for (i
= 0; i
< total_len
; i
++) {
1026 printk(" %02X", *datap
++);
1035 /* Get routing info from the tunnel socket */
1036 skb
->dst
= dst_clone(dst
);
1038 /* Queue the packet to IP for output */
1040 error
= ip_queue_xmit(skb
, 1);
1041 error
= net_xmit_eval(error
);
1045 tunnel
->stats
.tx_packets
++;
1046 tunnel
->stats
.tx_bytes
+= len
;
1047 session
->stats
.tx_packets
++;
1048 session
->stats
.tx_bytes
+= len
;
1050 tunnel
->stats
.tx_errors
++;
1051 session
->stats
.tx_errors
++;
1059 /* Automatically called when the skb is freed.
1061 static void pppol2tp_sock_wfree(struct sk_buff
*skb
)
1066 /* For data skbs that we transmit, we associate with the tunnel socket
1067 * but don't do accounting.
1069 static inline void pppol2tp_skb_set_owner_w(struct sk_buff
*skb
, struct sock
*sk
)
1073 skb
->destructor
= pppol2tp_sock_wfree
;
1076 /* Transmit function called by generic PPP driver. Sends PPP frame
1077 * over PPPoL2TP socket.
1079 * This is almost the same as pppol2tp_sendmsg(), but rather than
1080 * being called with a msghdr from userspace, it is called with a skb
1083 * The supplied skb from ppp doesn't have enough headroom for the
1084 * insertion of L2TP, UDP and IP headers so we need to allocate more
1085 * headroom in the skb. This will create a cloned skb. But we must be
1086 * careful in the error case because the caller will expect to free
1087 * the skb it supplied, not our cloned skb. So we take care to always
1088 * leave the original skb unfreed if we return an error.
1090 static int pppol2tp_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
1092 struct pppol2tp_session
*session
;
1093 struct pppol2tp_tunnel
*tunnel
;
1094 struct sock
*sk
= (struct sock
*) chan
->private;
1095 struct sock
*sk_tun
;
1096 struct dst_entry
*dst
;
1097 struct inet_sock
*inet
;
1099 unsigned int data_len
= skb
->len
;
1100 unsigned int headroom
;
1108 if (sock_flag(sk
, SOCK_DEAD
) || !(sk
->sk_state
& PPPOX_CONNECTED
)) {
1109 DPRINTK(-1, "dead=%d state=%x\n", sock_flag(sk
, SOCK_DEAD
), sk
->sk_state
);
1114 /* Get session and tunnel contexts from the socket */
1115 SOCK_2_SESSION(sk
, session
, error
, -EBADF
, end
, 0);
1116 sk_tun
= session
->tunnel_sock
;
1117 SOCK_2_TUNNEL(sk_tun
, tunnel
, error
, -EBADF
, end
, 0);
1119 /* What header length is configured for this session? */
1120 hdr_len
= pppol2tp_l2tp_header_len(session
);
1122 /* Check that there's enough headroom in the skb to insert IP,
1123 * UDP and L2TP and PPP headers. If not enough, expand it to
1124 * make room. Adjust truesize.
1126 dst
= __sk_dst_get(sk_tun
);
1127 len
= skb_headroom(skb
);
1128 headroom
= (dst
? LL_RESERVED_SPACE(dst
->dev
) : NET_SKB_PAD
) +
1129 sizeof(struct iphdr
) +
1130 sizeof(struct udphdr
) + hdr_len
+ 2;
1131 if (skb_cow_head(skb
, headroom
)) {
1135 headroom
= skb_headroom(skb
);
1137 skb
->truesize
+= headroom
- len
;
1139 /* Setup PPP header */
1141 skb
->data
[0] = PPP_ALLSTATIONS
;
1142 skb
->data
[1] = PPP_UI
;
1144 /* Setup L2TP header */
1145 pppol2tp_build_l2tp_header(session
, __skb_push(skb
, hdr_len
));
1147 /* Build UDP header */
1148 inet
= inet_sk(sk_tun
);
1149 udp_len
= sizeof(struct udphdr
) + hdr_len
+ 2 + data_len
;
1150 __skb_push(skb
, sizeof(struct udphdr
));
1151 skb_reset_transport_header(skb
);
1153 uh
->source
= inet
->sport
;
1154 uh
->dest
= inet
->dport
;
1155 uh
->len
= htons(udp_len
);
1158 /* Calculate UDP checksum if configured to do so */
1159 skb
->ip_summed
= CHECKSUM_NONE
;
1161 if (session
->send_seq
)
1162 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1163 "%s: send %d bytes, ns=%hu\n",
1164 session
->name
, data_len
, session
->ns
- 1);
1166 PRINTK(session
->debug
, PPPOL2TP_MSG_DATA
, KERN_DEBUG
,
1167 "%s: send %d bytes\n", session
->name
, data_len
);
1169 if (session
->debug
& PPPOL2TP_MSG_DATA
) {
1171 unsigned char *datap
= skb
->data
+ sizeof(struct udphdr
);
1173 printk(KERN_DEBUG
"%s: xmit:", session
->name
);
1174 for (i
= 0; i
< data_len
; i
++) {
1175 printk(" %02X", *datap
++);
1185 /* Reset skb netfilter state */
1186 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1187 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
|
1191 /* Get routing info from the tunnel socket */
1192 dst_release(skb
->dst
);
1193 skb
->dst
= dst_clone(dst
);
1194 pppol2tp_skb_set_owner_w(skb
, sk_tun
);
1196 /* Queue the packet to IP for output */
1198 error
= ip_queue_xmit(skb
, 1);
1199 error
= net_xmit_eval(error
);
1203 tunnel
->stats
.tx_packets
++;
1204 tunnel
->stats
.tx_bytes
+= len
;
1205 session
->stats
.tx_packets
++;
1206 session
->stats
.tx_bytes
+= len
;
1208 tunnel
->stats
.tx_errors
++;
1209 session
->stats
.tx_errors
++;
1216 /* Free the original skb */
1223 /*****************************************************************************
1224 * Session (and tunnel control) socket create/destroy.
1225 *****************************************************************************/
1227 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1228 * too. This handles that.
1230 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel
*tunnel
)
1233 struct hlist_node
*walk
;
1234 struct hlist_node
*tmp
;
1235 struct pppol2tp_session
*session
;
1243 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1244 "%s: closing all sessions...\n", tunnel
->name
);
1246 write_lock_bh(&tunnel
->hlist_lock
);
1247 for (hash
= 0; hash
< PPPOL2TP_HASH_SIZE
; hash
++) {
1249 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[hash
]) {
1250 struct sk_buff
*skb
;
1252 session
= hlist_entry(walk
, struct pppol2tp_session
, hlist
);
1255 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1256 "%s: closing session\n", session
->name
);
1258 hlist_del_init(&session
->hlist
);
1260 /* Since we should hold the sock lock while
1261 * doing any unbinding, we need to release the
1262 * lock we're holding before taking that lock.
1263 * Hold a reference to the sock so it doesn't
1264 * disappear as we're jumping between locks.
1267 write_unlock_bh(&tunnel
->hlist_lock
);
1270 if (sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
)) {
1271 pppox_unbind_sock(sk
);
1272 sk
->sk_state
= PPPOX_DEAD
;
1273 sk
->sk_state_change(sk
);
1276 /* Purge any queued data */
1277 skb_queue_purge(&sk
->sk_receive_queue
);
1278 skb_queue_purge(&sk
->sk_write_queue
);
1279 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
1286 DPRINTK(session
->debug
, "calling sock_put; refcnt=%d\n",
1287 sk
->sk_refcnt
.counter
);
1290 /* Now restart from the beginning of this hash
1291 * chain. We always remove a session from the
1292 * list so we are guaranteed to make forward
1295 write_lock_bh(&tunnel
->hlist_lock
);
1299 write_unlock_bh(&tunnel
->hlist_lock
);
1304 /* Really kill the tunnel.
1305 * Come here only when all sessions have been cleared from the tunnel.
1307 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel
*tunnel
)
1309 struct sock
*sk
= tunnel
->sock
;
1310 #ifdef PPPOL2TP_UDP_CONNECT
1311 struct sockaddr any
= { .sa_family
= AF_UNSPEC
};
1316 #ifdef PPPOL2TP_UDP_CONNECT
1317 /* Disconnect the tunnel socket */
1318 kernel_connect(sk
->sk_socket
, &any
, sizeof(any
), 0);
1321 /* Remove from socket list */
1322 list_del_init(&tunnel
->list
);
1324 #ifndef UDP_ENCAP_L2TPINUDP
1325 sk
->sk_data_ready
= tunnel
->old_data_ready
;
1327 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1328 (udp_sk(sk
))->encap_type
= 0;
1329 (udp_sk(sk
))->encap_rcv
= NULL
;
1331 sk
->sk_destruct
= tunnel
->old_sk_destruct
;
1332 sk
->sk_user_data
= NULL
;
1334 DPRINTK(tunnel
->debug
, "%s: MOD_DEC_USE_COUNT\n", tunnel
->name
);
1340 /* Tunnel UDP socket destruct hook.
1341 * The tunnel context is deleted only when all session sockets have been
1344 static void pppol2tp_tunnel_destruct(struct sock
*sk
)
1346 struct pppol2tp_tunnel
*tunnel
;
1350 SOCK_2_TUNNEL(sk
, tunnel
, error
, -EBADF
, end
, 0);
1352 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1353 "%s: closing...\n", tunnel
->name
);
1355 pppol2tp_tunnel_closeall(tunnel
);
1362 /* Really kill the socket. (Called from sock_put if refcnt == 0.)
1364 static void pppol2tp_session_destruct(struct sock
*sk
)
1366 struct pppol2tp_session
*session
= NULL
;
1371 if (sk
->sk_user_data
!= NULL
) {
1372 struct pppol2tp_tunnel
*tunnel
;
1374 SOCK_2_SESSION(sk
, session
, error
, -EBADF
, out
, 0);
1375 skb_queue_purge(&session
->reorder_q
);
1377 /* Don't use SOCK_2_TUNNEL() here to get the tunnel context
1378 * because the tunnel socket might have already been closed
1379 * (its sk->sk_user_data will be NULL) so use the session's
1380 * private tunnel ptr instead.
1382 tunnel
= session
->tunnel
;
1383 if (tunnel
!= NULL
) {
1384 if (tunnel
->magic
!= L2TP_TUNNEL_MAGIC
) {
1385 printk(KERN_ERR
"%s: %s:%d: BAD TUNNEL MAGIC "
1386 "( tunnel=%p magic=%x )\n",
1387 __FUNCTION__
, __FILE__
, __LINE__
,
1388 tunnel
, tunnel
->magic
);
1393 /* Delete tunnel context if this was the last session on the
1394 * tunnel. This was allocated when the first session was
1395 * created on the tunnel. See
1396 * pppol2tp_prepare_tunnel_socket().
1398 DPRINTK(tunnel
->debug
, "%s: session_count=%d\n",
1399 tunnel
->name
, atomic_read(&tunnel
->session_count
));
1400 if (atomic_dec_and_test(&tunnel
->session_count
)) {
1401 pppol2tp_tunnel_free(tunnel
);
1405 if (session
!= NULL
)
1412 /* Called when the PPPoX socket (session) is closed.
1414 static int pppol2tp_release(struct socket
*sock
)
1416 struct sock
*sk
= sock
->sk
;
1417 struct pppol2tp_session
*session
= NULL
;
1418 struct pppol2tp_tunnel
*tunnel
;
1425 if (sock_flag(sk
, SOCK_DEAD
) != 0)
1428 if (sk
->sk_user_data
) { /* Was this socket actually connected? */
1429 SOCK_2_SESSION(sk
, session
, error
, -EBADF
, end
, 0);
1431 /* Don't use SOCK_2_TUNNEL() here to get the tunnel context
1432 * because the tunnel socket might have already been closed
1433 * (its sk->sk_user_data will be NULL) so use the session's
1434 * private tunnel ptr instead.
1436 tunnel
= session
->tunnel
;
1437 if (tunnel
!= NULL
) {
1438 if (tunnel
->magic
== L2TP_TUNNEL_MAGIC
) {
1439 /* Delete the session socket from the hash */
1440 write_lock_bh(&tunnel
->hlist_lock
);
1441 hlist_del_init(&session
->hlist
);
1442 write_unlock_bh(&tunnel
->hlist_lock
);
1444 printk(KERN_ERR
"%s: %s:%d: BAD TUNNEL MAGIC "
1445 "( tunnel=%p magic=%x )\n",
1446 __FUNCTION__
, __FILE__
, __LINE__
,
1447 tunnel
, tunnel
->magic
);
1455 pppox_unbind_sock(sk
);
1457 /* Signal the death of the socket. */
1458 sk
->sk_state
= PPPOX_DEAD
;
1462 /* Purge any queued data */
1463 skb_queue_purge(&sk
->sk_receive_queue
);
1464 skb_queue_purge(&sk
->sk_write_queue
);
1465 if (session
!= NULL
) {
1466 struct sk_buff
*skb
;
1467 while ((skb
= skb_dequeue(&session
->reorder_q
))) {
1476 if (session
!= NULL
)
1477 DPRINTK(session
->debug
, "calling sock_put; refcnt=%d\n",
1478 session
->sock
->sk_refcnt
.counter
);
1486 /* Copied from fget() in fs/file_table.c.
1487 * Allows caller to specify the pid that owns the fd.
1489 static struct file
*pppol2tp_fget(pid_t pid
, unsigned int fd
)
1492 struct files_struct
*files
= current
->files
;
1495 struct task_struct
*tsk
= find_task_by_pid(pid
);
1501 spin_lock(&files
->file_lock
);
1502 file
= fcheck_files(files
, fd
);
1505 spin_unlock(&files
->file_lock
);
1509 /* Copied from sockfd_lookup() in net/socket.c.
1510 * Allows caller to specify the pid that owns the fd.
1512 static struct socket
*pppol2tp_sockfd_lookup(pid_t pid
, int fd
, int *err
)
1515 struct inode
*inode
;
1516 struct socket
*sock
;
1518 if (!(file
= pppol2tp_fget(pid
, fd
))) {
1523 inode
= file
->f_dentry
->d_inode
;
1524 if (!(sock
= SOCKET_I(inode
))) {
1530 if (sock
->file
!= file
) {
1531 printk(KERN_ERR
"socki_lookup: socket file changed!\n");
1537 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX sockets
1540 static struct sock
*pppol2tp_prepare_tunnel_socket(pid_t pid
, int fd
,
1541 #ifdef PPPOL2TP_UDP_CONNECT
1542 struct sockaddr_in
*addr
,
1544 u16 tunnel_id
, int *error
)
1547 struct socket
*sock
= NULL
;
1549 struct pppol2tp_tunnel
*tunnel
;
1550 struct sock
*ret
= NULL
;
1554 /* Get the socket from the fd */
1556 sock
= pppol2tp_sockfd_lookup(pid
, fd
, &err
);
1558 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1559 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1560 tunnel_id
, fd
, err
);
1566 /* Quick sanity checks */
1567 err
= -EPROTONOSUPPORT
;
1568 if (sk
->sk_protocol
!= IPPROTO_UDP
) {
1569 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1570 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1571 tunnel_id
, fd
, sk
->sk_protocol
, IPPROTO_UDP
);
1574 err
= -EAFNOSUPPORT
;
1575 if (sock
->ops
->family
!= AF_INET
) {
1576 PRINTK(-1, PPPOL2TP_MSG_CONTROL
, KERN_ERR
,
1577 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1578 tunnel_id
, fd
, sock
->ops
->family
, AF_INET
);
1584 /* Check if this socket has already been prepped */
1585 tunnel
= (struct pppol2tp_tunnel
*)sk
->sk_user_data
;
1586 if (tunnel
!= NULL
) {
1587 /* User-data field already set */
1589 if (tunnel
->magic
!= L2TP_TUNNEL_MAGIC
) {
1590 printk(KERN_ERR
"%s: %s:%d: BAD TUNNEL MAGIC "
1591 "( tunnel=%p magic=%x )\n",
1592 __FUNCTION__
, __FILE__
, __LINE__
,
1593 tunnel
, tunnel
->magic
);
1597 /* This socket has already been prepped */
1599 #ifdef PPPOL2TP_UDP_CONNECT
1602 /* (Re)connect tunnel socket to the peer */
1603 err
= kernel_connect(sock
, (struct sockaddr
*) addr
, sizeof(*addr
), 0);
1604 DPRINTK(-1, "tunl %hu: fd %d connect to %x/%hu returns %d\n",
1606 ntohl(addr
->sin_addr
.s_addr
), ntohs(addr
->sin_port
), err
);
1607 if (err
< 0 && err
!= -EALREADY
)
1610 /* This socket has already been prepped */
1611 if (tunnel
!= NULL
) {
1616 /* This socket is available and needs prepping. Create a new tunnel
1617 * context and init it.
1619 sk
->sk_user_data
= tunnel
= kzalloc(sizeof(struct pppol2tp_tunnel
), GFP_KERNEL
);
1620 if (sk
->sk_user_data
== NULL
) {
1625 tunnel
->magic
= L2TP_TUNNEL_MAGIC
;
1626 sprintf(&tunnel
->name
[0], "tunl %hu", tunnel_id
);
1628 tunnel
->stats
.tunnel_id
= tunnel_id
;
1630 tunnel
->debug
= PPPOL2TP_DEFAULT_DEBUG_FLAGS
;
1632 DPRINTK(tunnel
->debug
, "tunl %hu: allocated tunnel=%p, sk=%p, sock=%p\n",
1633 tunnel_id
, tunnel
, sk
, sock
);
1635 /* Setup the new protocol stuff */
1636 #ifndef UDP_ENCAP_L2TPINUDP
1637 tunnel
->old_data_ready
= sk
->sk_data_ready
;
1638 sk
->sk_data_ready
= pppol2tp_data_ready
;
1640 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1641 (udp_sk(sk
))->encap_type
= UDP_ENCAP_L2TPINUDP
;
1642 (udp_sk(sk
))->encap_rcv
= pppol2tp_udp_encap_recv
;
1645 tunnel
->old_sk_destruct
= sk
->sk_destruct
;
1646 sk
->sk_destruct
= pppol2tp_tunnel_destruct
;
1649 sk
->sk_allocation
= GFP_ATOMIC
;
1651 rwlock_init(&tunnel
->hlist_lock
);
1653 /* Add tunnel to our list */
1654 INIT_LIST_HEAD(&tunnel
->list
);
1655 list_add(&tunnel
->list
, &pppol2tp_tunnel_list
);
1672 static struct proto pppol2tp_sk_proto
= {
1674 .owner
= THIS_MODULE
,
1675 .obj_size
= sizeof(struct pppox_sock
),
1678 /* socket() handler. Initialize a new struct sock.
1680 static int pppol2tp_create(struct socket
*sock
)
1682 int error
= -ENOMEM
;
1686 DPRINTK(-1, "sock=%p\n", sock
);
1688 try_module_get(THIS_MODULE
);
1690 sk
= sk_alloc(PF_PPPOX
, GFP_KERNEL
, &pppol2tp_sk_proto
, 1);
1694 sock_init_data(sock
, sk
);
1696 sock
->state
= SS_UNCONNECTED
;
1697 sock
->ops
= &pppol2tp_ops
;
1699 sk
->sk_backlog_rcv
= pppol2tp_recv_core
;
1700 sk
->sk_protocol
= PX_PROTO_OL2TP
;
1701 sk
->sk_family
= PF_PPPOX
;
1702 sk
->sk_state
= PPPOX_NONE
;
1703 sk
->sk_type
= SOCK_STREAM
;
1704 sk
->sk_destruct
= pppol2tp_session_destruct
;
1709 module_put(THIS_MODULE
);
1715 /* connect() handler.. Attach a PPPoX socket to a tunnel UDP socket
1717 int pppol2tp_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
1718 int sockaddr_len
, int flags
)
1720 struct sock
*sk
= sock
->sk
;
1721 struct sockaddr_pppol2tp
*sp
= (struct sockaddr_pppol2tp
*) uservaddr
;
1722 struct pppox_sock
*po
= pppox_sk(sk
);
1723 struct sock
*tunnel_sock
= NULL
;
1724 struct pppol2tp_session
*session
= NULL
;
1725 struct pppol2tp_tunnel
*tunnel
;
1726 struct dst_entry
*dst
;
1731 DPRINTK(-1, "sock=%p, uservaddr=%p, sockaddr_len=%d, flags=%d, addr=%x/%hu\n",
1732 sock
, uservaddr
, sockaddr_len
, flags
,
1733 ntohl(sp
->pppol2tp
.addr
.sin_addr
.s_addr
), ntohs(sp
->pppol2tp
.addr
.sin_port
));
1737 if (sp
->sa_protocol
!= PX_PROTO_OL2TP
)
1740 /* Check for already bound sockets */
1742 if (sk
->sk_state
& PPPOX_CONNECTED
)
1745 /* We don't supporting rebinding anyway */
1746 if (sk
->sk_user_data
)
1747 goto end
; /* socket is already attached */
1749 /* Don't bind if s_tunnel is 0 */
1751 if (sp
->pppol2tp
.s_tunnel
== 0)
1754 /* Look up the tunnel socket and configure it if necessary */
1755 tunnel_sock
= pppol2tp_prepare_tunnel_socket(sp
->pppol2tp
.pid
,
1757 #ifdef PPPOL2TP_UDP_CONNECT
1760 sp
->pppol2tp
.s_tunnel
,
1762 if (tunnel_sock
== NULL
)
1765 tunnel
= tunnel_sock
->sk_user_data
;
1767 /* Allocate and initialize a new session context.
1769 session
= kzalloc(sizeof(struct pppol2tp_session
), GFP_KERNEL
);
1770 if (session
== NULL
) {
1775 skb_queue_head_init(&session
->reorder_q
);
1777 session
->magic
= L2TP_SESSION_MAGIC
;
1778 session
->owner
= current
->pid
;
1780 session
->tunnel
= tunnel
;
1781 session
->tunnel_sock
= tunnel_sock
;
1782 session
->tunnel_addr
= sp
->pppol2tp
;
1783 sprintf(&session
->name
[0], "sess %hu/%hu",
1784 session
->tunnel_addr
.s_tunnel
,
1785 session
->tunnel_addr
.s_session
);
1787 session
->stats
.tunnel_id
= session
->tunnel_addr
.s_tunnel
;
1788 session
->stats
.session_id
= session
->tunnel_addr
.s_session
;
1790 INIT_HLIST_NODE(&session
->hlist
);
1792 session
->debug
= PPPOL2TP_DEFAULT_DEBUG_FLAGS
;
1794 /* Default MTU must allow space for UDP/L2TP/PPP
1795 * headers. Leave some slack.
1797 session
->mtu
= session
->mru
= 1500 - PPPOL2TP_HEADER_OVERHEAD
;
1799 /* If PMTU discovery was enabled, use the MTU that was discovered */
1800 dst
= sk_dst_get(tunnel_sock
);
1802 u32 pmtu
= dst_mtu(__sk_dst_get(tunnel_sock
));
1804 session
->mtu
= session
->mru
= pmtu
-
1805 PPPOL2TP_HEADER_OVERHEAD
;
1806 DPRINTK(session
->debug
,
1807 "%s: MTU set by Path MTU discovery: mtu=%d\n",
1808 session
->name
, session
->mtu
);
1813 /* Special case: if source & dest session_id == 0x0000, this socket is
1814 * being created to manage the tunnel. Don't add the session to the
1815 * session hash list, just set up the internal context for use by
1816 * ioctl() and sockopt() handlers.
1818 if ((session
->tunnel_addr
.s_session
== 0) &&
1819 (session
->tunnel_addr
.d_session
== 0)) {
1821 DPRINTK(session
->debug
,
1822 "tunl %hu: socket created for tunnel mgmt ops\n",
1823 session
->tunnel_addr
.s_tunnel
);
1824 sk
->sk_user_data
= session
;
1828 DPRINTK(session
->debug
, "%s: allocated session=%p, sock=%p, owner=%d\n",
1829 session
->name
, session
, sk
, session
->owner
);
1831 /* Add session to the tunnel's hash list */
1832 SOCK_2_TUNNEL(tunnel_sock
, tunnel
, error
, -EBADF
, end
, 0);
1833 write_lock_bh(&tunnel
->hlist_lock
);
1834 hlist_add_head(&session
->hlist
,
1835 pppol2tp_session_id_hash(tunnel
,
1836 session
->tunnel_addr
.s_session
));
1837 write_unlock_bh(&tunnel
->hlist_lock
);
1839 /* This is how we get the session context from the socket. */
1840 sk
->sk_user_data
= session
;
1842 /* Right now, because we don't have a way to push the incoming skb's
1843 * straight through the UDP layer, the only header we need to worry
1844 * about is the L2TP header. This size is different depending on
1845 * whether sequence numbers are enabled for the data channel.
1847 po
->chan
.hdrlen
= PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
1848 po
->chan
.hdrlen
+= LL_MAX_HEADER
+ sizeof(struct iphdr
) +
1849 sizeof(struct udphdr
) + 2;
1851 po
->chan
.private = sk
;
1852 po
->chan
.ops
= &pppol2tp_chan_ops
;
1853 po
->chan
.mtu
= session
->mtu
;
1855 error
= ppp_register_channel(&po
->chan
);
1860 atomic_inc(&tunnel
->session_count
);
1861 sk
->sk_state
= PPPOX_CONNECTED
;
1862 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1863 "%s: created\n", session
->name
);
1869 PRINTK(session
? session
->debug
: -1, PPPOL2TP_MSG_CONTROL
, KERN_WARNING
,
1870 "%s: connect failed: %d\n", session
->name
, error
);
1877 /* getname() support.
1879 static int pppol2tp_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
1880 int *usockaddr_len
, int peer
)
1882 int len
= sizeof(struct sockaddr_pppol2tp
);
1883 struct sockaddr_pppol2tp sp
;
1885 struct pppol2tp_session
*session
;
1890 if (sock
->sk
->sk_state
!= PPPOX_CONNECTED
)
1893 SOCK_2_SESSION(sock
->sk
, session
, error
, -EBADF
, end
, 0);
1895 sp
.sa_family
= AF_PPPOX
;
1896 sp
.sa_protocol
= PX_PROTO_OL2TP
;
1897 memcpy(&sp
.pppol2tp
, &session
->tunnel_addr
,
1898 sizeof(struct pppol2tp_addr
));
1900 memcpy(uaddr
, &sp
, len
);
1902 *usockaddr_len
= len
;
1910 /****************************************************************************
1913 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1914 * sockets. However, in order to control kernel tunnel features, we allow
1915 * userspace to create a special "tunnel" PPPoX socket which is used for
1916 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1917 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1919 ****************************************************************************/
1921 /* Session ioctl helper.
1923 static int pppol2tp_session_ioctl(struct pppol2tp_session
*session
,
1924 unsigned int cmd
, unsigned long arg
)
1928 struct sock
*sk
= session
->sock
;
1929 int val
= (int) arg
;
1933 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_DEBUG
,
1934 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1935 session
->name
, cmd
, arg
);
1940 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1944 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1946 ifr
.ifr_mtu
= session
->mtu
;
1947 if (copy_to_user((void __user
*) arg
, &ifr
, sizeof(struct ifreq
)))
1950 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1951 "%s: get mtu=%d\n", session
->name
, session
->mtu
);
1957 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1961 if (copy_from_user(&ifr
, (void __user
*) arg
, sizeof(struct ifreq
)))
1964 session
->mtu
= ifr
.ifr_mtu
;
1966 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1967 "%s: set mtu=%d\n", session
->name
, session
->mtu
);
1973 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1977 if (put_user(session
->mru
, (int __user
*) arg
))
1980 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1981 "%s: get mru=%d\n", session
->name
, session
->mru
);
1987 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
1991 if (get_user(val
,(int __user
*) arg
))
1995 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
1996 "%s: set mru=%d\n", session
->name
, session
->mru
);
2002 if (put_user(session
->flags
, (int __user
*) arg
))
2005 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2006 "%s: get flags=%d\n", session
->name
, session
->flags
);
2012 if (get_user(val
, (int __user
*) arg
))
2014 session
->flags
= val
;
2015 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2016 "%s: set flags=%d\n", session
->name
, session
->flags
);
2020 case PPPIOCGL2TPSTATS
:
2023 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
2026 if (copy_to_user((void __user
*) arg
, &session
->stats
,
2027 sizeof(session
->stats
)))
2029 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2030 "%s: get L2TP stats\n", session
->name
);
2044 /* Tunnel ioctl helper.
2046 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
2047 * specifies a session_id, the session ioctl handler is called. This allows an
2048 * application to retrieve session stats via a tunnel socket.
2050 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel
*tunnel
,
2051 unsigned int cmd
, unsigned long arg
)
2054 struct sock
*sk
= tunnel
->sock
;
2055 struct pppol2tp_ioc_stats stats_req
;
2059 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_DEBUG
,
2060 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel
->name
,
2064 case PPPIOCGL2TPSTATS
:
2067 if (!(sk
->sk_state
& PPPOX_CONNECTED
))
2070 if (copy_from_user(&stats_req
, (void __user
*) arg
,
2071 sizeof(stats_req
))) {
2075 if (stats_req
.session_id
!= 0) {
2076 /* resend to session ioctl handler */
2077 struct pppol2tp_session
*session
=
2078 pppol2tp_session_find(tunnel
, stats_req
.session_id
);
2079 if (session
!= NULL
)
2080 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
2086 tunnel
->stats
.using_ipsec
= (sk
->sk_policy
[0] || sk
->sk_policy
[1]) ? 1 : 0;
2088 if (copy_to_user((void __user
*) arg
, &tunnel
->stats
,
2089 sizeof(tunnel
->stats
))) {
2093 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2094 "%s: get L2TP stats\n", tunnel
->name
);
2108 /* Main ioctl() handler.
2109 * Dispatch to tunnel or session helpers depending on the socket.
2111 static int pppol2tp_ioctl(struct socket
*sock
, unsigned int cmd
,
2114 struct sock
*sk
= sock
->sk
;
2115 struct pppol2tp_session
*session
;
2116 struct pppol2tp_tunnel
*tunnel
;
2124 if (sock_flag(sk
, SOCK_DEAD
) != 0)
2127 if ((sk
->sk_user_data
== NULL
) ||
2128 (!(sk
->sk_state
& (PPPOX_CONNECTED
| PPPOX_BOUND
)))) {
2130 DPRINTK(-1, "ioctl: socket %p not connected.\n", sk
);
2134 SOCK_2_SESSION(sk
, session
, err
, -EBADF
, end
, 0);
2135 SOCK_2_TUNNEL(session
->tunnel_sock
, tunnel
, err
, -EBADF
, end
, 1);
2137 /* Special case: if session's session_id is zero, treat ioctl as a
2140 if ((session
->tunnel_addr
.s_session
== 0) &&
2141 (session
->tunnel_addr
.d_session
== 0)) {
2142 err
= pppol2tp_tunnel_ioctl(tunnel
, cmd
, arg
);
2146 err
= pppol2tp_session_ioctl(session
, cmd
, arg
);
2153 /*****************************************************************************
2154 * setsockopt() / getsockopt() support.
2156 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
2157 * sockets. In order to control kernel tunnel features, we allow userspace to
2158 * create a special "tunnel" PPPoX socket which is used for control only.
2159 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
2160 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
2161 *****************************************************************************/
2163 /* Tunnel setsockopt() helper.
2165 static int pppol2tp_tunnel_setsockopt(struct sock
*sk
,
2166 struct pppol2tp_tunnel
*tunnel
,
2167 int optname
, int val
)
2172 case PPPOL2TP_SO_DEBUG
:
2173 tunnel
->debug
= val
;
2174 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2175 "%s: set debug=%x\n", tunnel
->name
, tunnel
->debug
);
2186 /* Session setsockopt helper.
2188 static int pppol2tp_session_setsockopt(struct sock
*sk
,
2189 struct pppol2tp_session
*session
,
2190 int optname
, int val
)
2195 case PPPOL2TP_SO_RECVSEQ
:
2196 if ((val
!= 0) && (val
!= 1)) {
2200 session
->recv_seq
= val
? -1 : 0;
2201 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2202 "%s: set recv_seq=%d\n", session
->name
,
2206 case PPPOL2TP_SO_SENDSEQ
:
2207 if ((val
!= 0) && (val
!= 1)) {
2211 session
->send_seq
= val
? -1 : 0;
2213 /* FIXME: is it safe to change the ppp channel's
2214 * hdrlen on the fly?
2216 struct sock
*sk
= session
->sock
;
2217 struct pppox_sock
*po
= pppox_sk(sk
);
2218 po
->chan
.hdrlen
= val
? PPPOL2TP_L2TP_HDR_SIZE_SEQ
:
2219 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ
;
2220 po
->chan
.hdrlen
+= LL_MAX_HEADER
+ sizeof(struct iphdr
) +
2221 sizeof(struct udphdr
) + 2;
2223 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2224 "%s: set send_seq=%d\n", session
->name
, session
->send_seq
);
2227 case PPPOL2TP_SO_LNSMODE
:
2228 if ((val
!= 0) && (val
!= 1)) {
2232 session
->lns_mode
= val
? -1 : 0;
2233 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2234 "%s: set lns_mode=%d\n", session
->name
,
2238 case PPPOL2TP_SO_DEBUG
:
2239 session
->debug
= val
;
2240 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2241 "%s: set debug=%x\n", session
->name
, session
->debug
);
2244 case PPPOL2TP_SO_REORDERTO
:
2245 session
->reorder_timeout
= MS_TO_JIFFIES(val
);
2246 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2247 "%s: set reorder_timeout=%d\n", session
->name
,
2248 session
->reorder_timeout
);
2259 /* Main setsockopt() entry point.
2260 * Does API checks, then calls either the tunnel or session setsockopt
2261 * handler, according to whether the PPPoL2TP socket is a for a regular
2262 * session or the special tunnel type.
2264 static int pppol2tp_setsockopt(struct socket
*sock
, int level
, int optname
,
2265 char *optval
, int optlen
)
2267 struct sock
*sk
= sock
->sk
;
2268 struct pppol2tp_session
*session
= sk
->sk_user_data
;
2269 struct pppol2tp_tunnel
*tunnel
;
2273 if (level
!= SOL_PPPOL2TP
)
2274 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
2276 if (optlen
<sizeof(int))
2279 if (get_user(val
, (int __user
*)optval
))
2282 if (sk
->sk_user_data
== NULL
) {
2284 DPRINTK(-1, "setsockopt: socket %p not connected.\n", sk
);
2288 SOCK_2_SESSION(sk
, session
, err
, -EBADF
, end
, 0);
2289 SOCK_2_TUNNEL(session
->tunnel_sock
, tunnel
, err
, -EBADF
, end
, 1);
2293 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2295 if ((session
->tunnel_addr
.s_session
== 0) &&
2296 (session
->tunnel_addr
.d_session
== 0))
2297 err
= pppol2tp_tunnel_setsockopt(sk
, tunnel
, optname
, val
);
2299 err
= pppol2tp_session_setsockopt(sk
, session
, optname
, val
);
2306 /* Tunnel getsockopt helper.
2308 static int pppol2tp_tunnel_getsockopt(struct sock
*sk
,
2309 struct pppol2tp_tunnel
*tunnel
,
2310 int optname
, int *val
)
2315 case PPPOL2TP_SO_DEBUG
:
2316 *val
= tunnel
->debug
;
2317 PRINTK(tunnel
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2318 "%s: get debug=%x\n", tunnel
->name
, tunnel
->debug
);
2329 /* Session getsockopt helper.
2331 static int pppol2tp_session_getsockopt(struct sock
*sk
,
2332 struct pppol2tp_session
*session
,
2333 int optname
, int *val
)
2338 case PPPOL2TP_SO_RECVSEQ
:
2339 *val
= session
->recv_seq
;
2340 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2341 "%s: get recv_seq=%d\n", session
->name
, *val
);
2344 case PPPOL2TP_SO_SENDSEQ
:
2345 *val
= session
->send_seq
;
2346 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2347 "%s: get send_seq=%d\n", session
->name
, *val
);
2350 case PPPOL2TP_SO_LNSMODE
:
2351 *val
= session
->lns_mode
;
2352 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2353 "%s: get lns_mode=%d\n", session
->name
, *val
);
2356 case PPPOL2TP_SO_DEBUG
:
2357 *val
= session
->debug
;
2358 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2359 "%s: get debug=%d\n", session
->name
, *val
);
2362 case PPPOL2TP_SO_REORDERTO
:
2363 *val
= JIFFIES_TO_MS(session
->reorder_timeout
);
2364 PRINTK(session
->debug
, PPPOL2TP_MSG_CONTROL
, KERN_INFO
,
2365 "%s: get reorder_timeout=%d\n", session
->name
, *val
);
2375 /* Main getsockopt() entry point.
2376 * Does API checks, then calls either the tunnel or session getsockopt
2377 * handler, according to whether the PPPoX socket is a for a regular session
2378 * or the special tunnel type.
2380 static int pppol2tp_getsockopt(struct socket
*sock
, int level
,
2381 int optname
, char *optval
, int *optlen
)
2383 struct sock
*sk
= sock
->sk
;
2384 struct pppol2tp_session
*session
= sk
->sk_user_data
;
2385 struct pppol2tp_tunnel
*tunnel
;
2389 if (level
!= SOL_PPPOL2TP
)
2390 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
2392 if (get_user(len
, (int __user
*) optlen
))
2395 len
= min_t(unsigned int, len
, sizeof(int));
2400 if (sk
->sk_user_data
== NULL
) {
2402 DPRINTK(-1, "getsockopt: socket %p not connected.\n", sk
);
2406 /* Get the session and tunnel contexts */
2407 SOCK_2_SESSION(sk
, session
, err
, -EBADF
, end
, 0);
2408 SOCK_2_TUNNEL(session
->tunnel_sock
, tunnel
, err
, -EBADF
, end
, 1);
2410 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2411 if ((session
->tunnel_addr
.s_session
== 0) &&
2412 (session
->tunnel_addr
.d_session
== 0))
2413 err
= pppol2tp_tunnel_getsockopt(sk
, tunnel
, optname
, &val
);
2415 err
= pppol2tp_session_getsockopt(sk
, session
, optname
, &val
);
2418 if (put_user(len
, (int __user
*) optlen
))
2421 if (copy_to_user((void __user
*) optval
, &val
, len
))
2428 /*****************************************************************************
2429 * /proc filesystem for debug
2430 *****************************************************************************/
2432 #ifdef CONFIG_PROC_FS
2434 #include <linux/seq_file.h>
2436 static int pppol2tp_proc_open(struct inode
*inode
, struct file
*file
);
2437 static void *pppol2tp_proc_start(struct seq_file
*m
, loff_t
*_pos
);
2438 static void *pppol2tp_proc_next(struct seq_file
*p
, void *v
, loff_t
*pos
);
2439 static void pppol2tp_proc_stop(struct seq_file
*p
, void *v
);
2440 static int pppol2tp_proc_show(struct seq_file
*m
, void *v
);
2442 static struct proc_dir_entry
*pppol2tp_proc
;
2444 static struct seq_operations pppol2tp_proc_ops
= {
2445 .start
= pppol2tp_proc_start
,
2446 .next
= pppol2tp_proc_next
,
2447 .stop
= pppol2tp_proc_stop
,
2448 .show
= pppol2tp_proc_show
,
2451 static struct file_operations pppol2tp_proc_fops
= {
2452 .owner
= THIS_MODULE
,
2453 .open
= pppol2tp_proc_open
,
2455 .llseek
= seq_lseek
,
2456 .release
= seq_release
,
2459 static int pppol2tp_proc_open(struct inode
*inode
, struct file
*file
)
2465 ret
= seq_open(file
, &pppol2tp_proc_ops
);
2469 m
= file
->private_data
;
2470 m
->private = PDE(inode
)->data
;
2477 static void *pppol2tp_proc_start(struct seq_file
*m
, loff_t
*_pos
)
2479 struct pppol2tp_tunnel
*tunnel
= NULL
;
2481 struct list_head
*walk
;
2482 struct list_head
*tmp
;
2486 /* allow for the header line */
2493 /* find the n'th element in the list */
2494 list_for_each_safe(walk
, tmp
, &pppol2tp_tunnel_list
) {
2495 tunnel
= list_entry(walk
, struct pppol2tp_tunnel
, list
);
2497 sock_hold(tunnel
->sock
);
2509 static void *pppol2tp_proc_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
2511 struct pppol2tp_tunnel
*tunnel
= v
;
2512 struct list_head
*tmp
;
2513 struct list_head
*list
;
2520 list
= &pppol2tp_tunnel_list
;
2522 list
= &tunnel
->list
;
2525 if (tmp
== &pppol2tp_tunnel_list
)
2528 tunnel
= list_entry(tmp
, struct pppol2tp_tunnel
, list
);
2535 static void pppol2tp_proc_stop(struct seq_file
*p
, void *v
)
2537 struct pppol2tp_tunnel
*tunnel
= v
;
2542 sock_put(tunnel
->sock
);
2547 static int pppol2tp_proc_show(struct seq_file
*m
, void *v
)
2549 struct pppol2tp_tunnel
*tunnel
= v
;
2550 struct pppol2tp_session
*session
;
2551 struct hlist_node
*walk
;
2552 struct hlist_node
*tmp
;
2557 /* display header on line 1 */
2558 if (v
== (void *)1) {
2559 seq_puts(m
, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION
"\n");
2560 seq_puts(m
, "TUNNEL name, user-data-ok "
2561 "session-count magic-ok\n");
2562 seq_puts(m
, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2563 seq_puts(m
, " SESSION name, addr/port src-tid/sid "
2564 "dest-tid/sid state user-data-ok magic-ok\n");
2565 seq_puts(m
, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2566 seq_puts(m
, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2570 seq_printf(m
, "TUNNEL '%s', %c %d MAGIC %s\n",
2572 (tunnel
== tunnel
->sock
->sk_user_data
) ? 'Y':'N',
2573 atomic_read(&tunnel
->session_count
),
2574 (tunnel
->magic
== L2TP_TUNNEL_MAGIC
) ? "OK" : "BAD");
2575 seq_printf(m
, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2577 tunnel
->stats
.tx_packets
, tunnel
->stats
.tx_bytes
,
2578 tunnel
->stats
.tx_errors
,
2579 tunnel
->stats
.rx_packets
, tunnel
->stats
.rx_bytes
,
2580 tunnel
->stats
.rx_errors
);
2582 if (tunnel
->magic
!= L2TP_TUNNEL_MAGIC
) {
2583 seq_puts(m
, "*** Aborting ***\n");
2587 for (i
= 0; i
< PPPOL2TP_HASH_SIZE
; i
++) {
2588 hlist_for_each_safe(walk
, tmp
, &tunnel
->session_hlist
[i
]) {
2589 session
= hlist_entry(walk
, struct pppol2tp_session
, hlist
);
2590 seq_printf(m
, " SESSION '%s' %08X/%d %04X/%04X -> "
2591 "%04X/%04X %d %c MAGIC %s\n",
2593 htonl(session
->tunnel_addr
.addr
.sin_addr
.s_addr
),
2594 htons(session
->tunnel_addr
.addr
.sin_port
),
2595 session
->tunnel_addr
.s_tunnel
,
2596 session
->tunnel_addr
.s_session
,
2597 session
->tunnel_addr
.d_tunnel
,
2598 session
->tunnel_addr
.d_session
,
2599 session
->sock
->sk_state
,
2600 (session
== session
->sock
->sk_user_data
) ?
2602 (session
->magic
== L2TP_SESSION_MAGIC
) ?
2605 seq_printf(m
, " %d/%d/%c/%c/%s %08x %d\n",
2606 session
->mtu
, session
->mru
,
2607 session
->recv_seq
? 'R' : '-',
2608 session
->send_seq
? 'S' : '-',
2609 session
->lns_mode
? "LNS" : "LAC",
2611 JIFFIES_TO_MS(session
->reorder_timeout
));
2612 seq_printf(m
, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2613 session
->nr
, session
->ns
,
2614 session
->stats
.tx_packets
,
2615 session
->stats
.tx_bytes
,
2616 session
->stats
.tx_errors
,
2617 session
->stats
.rx_packets
,
2618 session
->stats
.rx_bytes
,
2619 session
->stats
.rx_errors
);
2621 if (session
->magic
!= L2TP_SESSION_MAGIC
) {
2622 seq_puts(m
, "*** Aborting ***\n");
2635 #endif /* CONFIG_PROC_FS */
2637 /*****************************************************************************
2639 *****************************************************************************/
2641 static struct proto_ops pppol2tp_ops
= {
2643 .owner
= THIS_MODULE
,
2644 .release
= pppol2tp_release
,
2645 .bind
= sock_no_bind
,
2646 .connect
= pppol2tp_connect
,
2647 .socketpair
= sock_no_socketpair
,
2648 .accept
= sock_no_accept
,
2649 .getname
= pppol2tp_getname
,
2650 .poll
= datagram_poll
,
2651 .listen
= sock_no_listen
,
2652 .shutdown
= sock_no_shutdown
,
2653 .setsockopt
= pppol2tp_setsockopt
,
2654 .getsockopt
= pppol2tp_getsockopt
,
2655 .sendmsg
= pppol2tp_sendmsg
,
2656 .recvmsg
= pppol2tp_recvmsg
,
2657 .mmap
= sock_no_mmap
,
2658 .ioctl
= pppox_ioctl
,
2661 static struct pppox_proto pppol2tp_proto
= {
2662 .create
= pppol2tp_create
,
2663 .ioctl
= pppol2tp_ioctl
2666 static int __init
pppol2tp_init(void)
2670 err
= proto_register(&pppol2tp_sk_proto
, 0);
2673 err
= register_pppox_proto(PX_PROTO_OL2TP
, &pppol2tp_proto
);
2675 goto out_unregister_pppol2tp_proto
;
2677 #ifdef CONFIG_PROC_FS
2678 pppol2tp_proc
= create_proc_entry("pppol2tp", 0, proc_net
);
2679 if (!pppol2tp_proc
) {
2681 goto out_unregister_pppox_proto
;
2683 pppol2tp_proc
->proc_fops
= &pppol2tp_proc_fops
;
2684 #endif /* CONFIG_PROC_FS */
2685 printk(KERN_INFO
"PPPoL2TP kernel driver, %s\n",
2686 PPPOL2TP_DRV_VERSION
);
2691 #ifdef CONFIG_PROC_FS
2692 out_unregister_pppox_proto
:
2693 unregister_pppox_proto(PX_PROTO_OL2TP
);
2694 #endif /* CONFIG_PROC_FS */
2696 out_unregister_pppol2tp_proto
:
2697 proto_unregister(&pppol2tp_sk_proto
);
2701 static void __exit
pppol2tp_exit(void)
2703 unregister_pppox_proto(PX_PROTO_OL2TP
);
2705 #ifdef CONFIG_PROC_FS
2706 remove_proc_entry("pppol2tp", proc_net
);
2707 #endif /* CONFIG_PROC_FS */
2708 proto_unregister(&pppol2tp_sk_proto
);
2711 module_init(pppol2tp_init
);
2712 module_exit(pppol2tp_exit
);
2714 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>, "
2715 "James Chapman <jchapman@katalix.com>");
2716 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2717 MODULE_LICENSE("GPL");
2718 MODULE_VERSION(PPPOL2TP_DRV_VERSION
);