1 /*****************************************************************************
2 * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoL2TP --- PPP over L2TP (RFC 2661)
9 * Authors: Martijn van Oosterhout <kleptog@svana.org>
10 * James Chapman (jchapman@katalix.com)
12 * Michal Ostrowski <mostrows@speakeasy.net>
13 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
14 * David S. Miller (davem@redhat.com)
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
24 /* This driver handles only L2TP data frames; control frames are handled by a
25 * userspace application.
27 * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
28 * attaches it to a bound UDP socket with local tunnel_id / session_id and
29 * peer tunnel_id / session_id set. Data can then be sent or received using
30 * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
31 * can be read or modified using ioctl() or [gs]etsockopt() calls.
33 * When a PPPoL2TP socket is connected with local and peer session_id values
34 * zero, the socket is treated as a special tunnel management socket.
36 * Here's example userspace code to create a socket for sending/receiving data
37 * over an L2TP session:-
39 * struct sockaddr_pppol2tp sax;
43 * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
45 * sax.sa_family = AF_PPPOX;
46 * sax.sa_protocol = PX_PROTO_OL2TP;
47 * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
48 * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
49 * sax.pppol2tp.addr.sin_port = addr->sin_port;
50 * sax.pppol2tp.addr.sin_family = AF_INET;
51 * sax.pppol2tp.s_tunnel = tunnel_id;
52 * sax.pppol2tp.s_session = session_id;
53 * sax.pppol2tp.d_tunnel = peer_tunnel_id;
54 * sax.pppol2tp.d_session = peer_session_id;
56 * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
58 * A pppd plugin that allows PPP traffic to be carried over L2TP using
59 * this driver is available from the OpenL2TP project at
60 * http://openl2tp.sourceforge.net.
63 #include <linux/module.h>
64 #include <linux/version.h>
65 #include <linux/string.h>
66 #include <linux/list.h>
67 #include <asm/uaccess.h>
69 #include <linux/kernel.h>
70 #include <linux/spinlock.h>
71 #include <linux/kthread.h>
72 #include <linux/sched.h>
73 #include <linux/slab.h>
74 #include <linux/errno.h>
75 #include <linux/jiffies.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>
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/sort.h>
93 #include <linux/proc_fs.h>
99 #include <asm/byteorder.h>
100 #include <asm/atomic.h>
103 #define PPPOL2TP_DRV_VERSION "V1.0"
105 /* L2TP header constants */
106 #define L2TP_HDRFLAG_T 0x8000
107 #define L2TP_HDRFLAG_L 0x4000
108 #define L2TP_HDRFLAG_S 0x0800
109 #define L2TP_HDRFLAG_O 0x0200
110 #define L2TP_HDRFLAG_P 0x0100
112 #define L2TP_HDR_VER_MASK 0x000F
113 #define L2TP_HDR_VER 0x0002
115 /* Space for UDP, L2TP and PPP headers */
116 #define PPPOL2TP_HEADER_OVERHEAD 40
118 /* Just some random numbers */
119 #define L2TP_TUNNEL_MAGIC 0x42114DDA
120 #define L2TP_SESSION_MAGIC 0x0C04EB7D
122 #define PPPOL2TP_HASH_BITS 4
123 #define PPPOL2TP_HASH_SIZE (1 << PPPOL2TP_HASH_BITS)
125 /* Default trace flags */
126 #define PPPOL2TP_DEFAULT_DEBUG_FLAGS 0
128 #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
130 if ((_mask) & (_type)) \
131 printk(_lvl "PPPOL2TP: " _fmt, ##args); \
134 /* Number of bytes to build transmit L2TP headers.
135 * Unfortunately the size is different depending on whether sequence numbers
138 #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
139 #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
141 struct pppol2tp_tunnel;
143 /* Describes a session. It is the sk_user_data field in the PPPoL2TP
144 * socket. Contains information to determine incoming packets and transmit
147 struct pppol2tp_session
149 int magic; /* should be
150 * L2TP_SESSION_MAGIC */
151 int owner; /* pid that opened the socket */
153 struct sock *sock; /* Pointer to the session
155 struct sock *tunnel_sock; /* Pointer to the tunnel UDP
158 struct pppol2tp_addr tunnel_addr; /* Description of tunnel */
160 struct pppol2tp_tunnel *tunnel; /* back pointer to tunnel
163 char name[20]; /* "sess xxxxx/yyyyy", where
164 * x=tunnel_id, y=session_id */
167 int flags; /* accessed by PPPIOCGFLAGS.
169 unsigned recv_seq:1; /* expect receive packets with
170 * sequence numbers? */
171 unsigned send_seq:1; /* send packets with sequence
173 unsigned lns_mode:1; /* behave as LNS? LAC enables
174 * sequence numbers under
176 int debug; /* bitmask of debug message
178 int reorder_timeout; /* configured reorder timeout
180 u16 nr; /* session NR state (receive) */
181 u16 ns; /* session NR state (send) */
182 struct sk_buff_head reorder_q; /* receive reorder queue */
183 struct pppol2tp_ioc_stats stats;
184 struct hlist_node hlist; /* Hash list node */
187 /* The sk_user_data field of the tunnel's UDP socket. It contains info to track
188 * all the associated sessions so incoming packets can be sorted out
190 struct pppol2tp_tunnel
192 int magic; /* Should be L2TP_TUNNEL_MAGIC */
193 rwlock_t hlist_lock; /* protect session_hlist */
194 struct hlist_head session_hlist[PPPOL2TP_HASH_SIZE];
195 /* hashed list of sessions,
197 int debug; /* bitmask of debug message
199 char name[12]; /* "tunl xxxxx" */
200 struct pppol2tp_ioc_stats stats;
202 void (*old_sk_destruct)(struct sock *);
204 struct sock *sock; /* Parent socket */
205 struct list_head list; /* Keep a list of all open
206 * prepared sockets */
211 /* Private data stored for received packets in the skb.
213 struct pppol2tp_skb_cb {
218 unsigned long expires;
221 #define PPPOL2TP_SKB_CB(skb) ((struct pppol2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
223 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
224 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel);
226 static atomic_t pppol2tp_tunnel_count;
227 static atomic_t pppol2tp_session_count;
228 static struct ppp_channel_ops pppol2tp_chan_ops = { pppol2tp_xmit , NULL };
229 static struct proto_ops pppol2tp_ops;
230 static LIST_HEAD(pppol2tp_tunnel_list);
231 static DEFINE_RWLOCK(pppol2tp_tunnel_list_lock);
233 /* Helpers to obtain tunnel/session contexts from sockets.
235 static inline struct pppol2tp_session *pppol2tp_sock_to_session(struct sock *sk)
237 struct pppol2tp_session *session;
242 session = (struct pppol2tp_session *)(sk->sk_user_data);
246 BUG_ON(session->magic != L2TP_SESSION_MAGIC);
251 static inline struct pppol2tp_tunnel *pppol2tp_sock_to_tunnel(struct sock *sk)
253 struct pppol2tp_tunnel *tunnel;
258 tunnel = (struct pppol2tp_tunnel *)(sk->sk_user_data);
262 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
267 /* Tunnel reference counts. Incremented per session that is added to
270 static inline void pppol2tp_tunnel_inc_refcount(struct pppol2tp_tunnel *tunnel)
272 atomic_inc(&tunnel->ref_count);
275 static inline void pppol2tp_tunnel_dec_refcount(struct pppol2tp_tunnel *tunnel)
277 if (atomic_dec_and_test(&tunnel->ref_count))
278 pppol2tp_tunnel_free(tunnel);
281 /* Session hash list.
282 * The session_id SHOULD be random according to RFC2661, but several
283 * L2TP implementations (Cisco and Microsoft) use incrementing
284 * session_ids. So we do a real hash on the session_id, rather than a
287 static inline struct hlist_head *
288 pppol2tp_session_id_hash(struct pppol2tp_tunnel *tunnel, u16 session_id)
290 unsigned long hash_val = (unsigned long) session_id;
291 return &tunnel->session_hlist[hash_long(hash_val, PPPOL2TP_HASH_BITS)];
294 /* Lookup a session by id
296 static struct pppol2tp_session *
297 pppol2tp_session_find(struct pppol2tp_tunnel *tunnel, u16 session_id)
299 struct hlist_head *session_list =
300 pppol2tp_session_id_hash(tunnel, session_id);
301 struct pppol2tp_session *session;
302 struct hlist_node *walk;
304 read_lock(&tunnel->hlist_lock);
305 hlist_for_each_entry(session, walk, session_list, hlist) {
306 if (session->tunnel_addr.s_session == session_id) {
307 read_unlock(&tunnel->hlist_lock);
311 read_unlock(&tunnel->hlist_lock);
316 /* Lookup a tunnel by id
318 static struct pppol2tp_tunnel *pppol2tp_tunnel_find(u16 tunnel_id)
320 struct pppol2tp_tunnel *tunnel = NULL;
322 read_lock(&pppol2tp_tunnel_list_lock);
323 list_for_each_entry(tunnel, &pppol2tp_tunnel_list, list) {
324 if (tunnel->stats.tunnel_id == tunnel_id) {
325 read_unlock(&pppol2tp_tunnel_list_lock);
329 read_unlock(&pppol2tp_tunnel_list_lock);
334 /*****************************************************************************
335 * Receive data handling
336 *****************************************************************************/
338 /* Queue a skb in order. We come here only if the skb has an L2TP sequence
341 static void pppol2tp_recv_queue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
343 struct sk_buff *skbp;
344 u16 ns = PPPOL2TP_SKB_CB(skb)->ns;
346 spin_lock(&session->reorder_q.lock);
347 skb_queue_walk(&session->reorder_q, skbp) {
348 if (PPPOL2TP_SKB_CB(skbp)->ns > ns) {
349 __skb_insert(skb, skbp->prev, skbp, &session->reorder_q);
350 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
351 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
352 session->name, ns, PPPOL2TP_SKB_CB(skbp)->ns,
353 skb_queue_len(&session->reorder_q));
354 session->stats.rx_oos_packets++;
359 __skb_queue_tail(&session->reorder_q, skb);
362 spin_unlock(&session->reorder_q.lock);
365 /* Dequeue a single skb.
367 static void pppol2tp_recv_dequeue_skb(struct pppol2tp_session *session, struct sk_buff *skb)
369 struct pppol2tp_tunnel *tunnel = session->tunnel;
370 int length = PPPOL2TP_SKB_CB(skb)->length;
371 struct sock *session_sock = NULL;
373 /* We're about to requeue the skb, so unlink it and return resources
374 * to its current owner (a socket receive buffer).
376 skb_unlink(skb, &session->reorder_q);
379 tunnel->stats.rx_packets++;
380 tunnel->stats.rx_bytes += length;
381 session->stats.rx_packets++;
382 session->stats.rx_bytes += length;
384 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
387 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
388 "%s: updated nr to %hu\n", session->name, session->nr);
391 /* If the socket is bound, send it in to PPP's input queue. Otherwise
392 * queue it on the session socket.
394 session_sock = session->sock;
395 if (session_sock->sk_state & PPPOX_BOUND) {
396 struct pppox_sock *po;
397 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
398 "%s: recv %d byte data frame, passing to ppp\n",
399 session->name, length);
401 /* We need to forget all info related to the L2TP packet
402 * gathered in the skb as we are going to reuse the same
403 * skb for the inner packet.
405 * - reset xfrm (IPSec) information as it applies to
406 * the outer L2TP packet and not to the inner one
407 * - release the dst to force a route lookup on the inner
408 * IP packet since skb->dst currently points to the dst
410 * - reset netfilter information as it doesn't apply
411 * to the inner packet either
414 dst_release(skb->dst);
418 po = pppox_sk(session_sock);
419 ppp_input(&po->chan, skb);
421 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
422 "%s: socket not bound\n", session->name);
424 /* Not bound. Nothing we can do, so discard. */
425 session->stats.rx_errors++;
429 sock_put(session->sock);
432 /* Dequeue skbs from the session's reorder_q, subject to packet order.
433 * Skbs that have been in the queue for too long are simply discarded.
435 static void pppol2tp_recv_dequeue(struct pppol2tp_session *session)
440 /* If the pkt at the head of the queue has the nr that we
441 * expect to send up next, dequeue it and any other
442 * in-sequence packets behind it.
444 spin_lock(&session->reorder_q.lock);
445 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
446 if (time_after(jiffies, PPPOL2TP_SKB_CB(skb)->expires)) {
447 session->stats.rx_seq_discards++;
448 session->stats.rx_errors++;
449 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
450 "%s: oos pkt %hu len %d discarded (too old), "
451 "waiting for %hu, reorder_q_len=%d\n",
452 session->name, PPPOL2TP_SKB_CB(skb)->ns,
453 PPPOL2TP_SKB_CB(skb)->length, session->nr,
454 skb_queue_len(&session->reorder_q));
455 __skb_unlink(skb, &session->reorder_q);
460 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
461 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
462 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
463 "%s: holding oos pkt %hu len %d, "
464 "waiting for %hu, reorder_q_len=%d\n",
465 session->name, PPPOL2TP_SKB_CB(skb)->ns,
466 PPPOL2TP_SKB_CB(skb)->length, session->nr,
467 skb_queue_len(&session->reorder_q));
471 spin_unlock(&session->reorder_q.lock);
472 pppol2tp_recv_dequeue_skb(session, skb);
473 spin_lock(&session->reorder_q.lock);
477 spin_unlock(&session->reorder_q.lock);
480 /* Internal receive frame. Do the real work of receiving an L2TP data frame
481 * here. The skb is not on a list when we get here.
482 * Returns 0 if the packet was a data packet and was successfully passed on.
483 * Returns 1 if the packet was not a good data packet and could not be
484 * forwarded. All such packets are passed up to userspace to deal with.
486 static int pppol2tp_recv_core(struct sock *sock, struct sk_buff *skb)
488 struct pppol2tp_session *session = NULL;
489 struct pppol2tp_tunnel *tunnel;
492 u16 tunnel_id, session_id;
496 tunnel = pppol2tp_sock_to_tunnel(sock);
500 /* UDP always verifies the packet length. */
501 __skb_pull(skb, sizeof(struct udphdr));
504 if (!pskb_may_pull(skb, 12)) {
505 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
506 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
510 /* Point to L2TP header */
513 /* Get L2TP header flags */
514 hdrflags = ntohs(*(__be16*)ptr);
516 /* Trace packet contents, if enabled */
517 if (tunnel->debug & PPPOL2TP_MSG_DATA) {
518 length = min(16u, skb->len);
519 if (!pskb_may_pull(skb, length))
522 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
526 printk(" %02X", ptr[offset]);
527 } while (++offset < length);
532 /* Get length of L2TP packet */
535 /* If type is control packet, it is handled by userspace. */
536 if (hdrflags & L2TP_HDRFLAG_T) {
537 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
538 "%s: recv control packet, len=%d\n", tunnel->name, length);
545 /* If length is present, skip it */
546 if (hdrflags & L2TP_HDRFLAG_L)
549 /* Extract tunnel and session ID */
550 tunnel_id = ntohs(*(__be16 *) ptr);
552 session_id = ntohs(*(__be16 *) ptr);
555 /* Find the session context */
556 session = pppol2tp_session_find(tunnel, session_id);
558 /* Not found? Pass to userspace to deal with */
559 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_INFO,
560 "%s: no socket found (%hu/%hu). Passing up.\n",
561 tunnel->name, tunnel_id, session_id);
564 sock_hold(session->sock);
566 /* The ref count on the socket was increased by the above call since
567 * we now hold a pointer to the session. Take care to do sock_put()
568 * when exiting this function from now on...
571 /* Handle the optional sequence numbers. If we are the LAC,
572 * enable/disable sequence numbers under the control of the LNS. If
573 * no sequence numbers present but we were expecting them, discard
576 if (hdrflags & L2TP_HDRFLAG_S) {
578 ns = ntohs(*(__be16 *) ptr);
580 nr = ntohs(*(__be16 *) ptr);
583 /* Received a packet with sequence numbers. If we're the LNS,
584 * check if we sre sending sequence numbers and if not,
587 if ((!session->lns_mode) && (!session->send_seq)) {
588 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
589 "%s: requested to enable seq numbers by LNS\n",
591 session->send_seq = -1;
594 /* Store L2TP info in the skb */
595 PPPOL2TP_SKB_CB(skb)->ns = ns;
596 PPPOL2TP_SKB_CB(skb)->nr = nr;
597 PPPOL2TP_SKB_CB(skb)->has_seq = 1;
599 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
600 "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
601 session->name, ns, nr, session->nr);
603 /* No sequence numbers.
604 * If user has configured mandatory sequence numbers, discard.
606 if (session->recv_seq) {
607 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
608 "%s: recv data has no seq numbers when required. "
609 "Discarding\n", session->name);
610 session->stats.rx_seq_discards++;
614 /* If we're the LAC and we're sending sequence numbers, the
615 * LNS has requested that we no longer send sequence numbers.
616 * If we're the LNS and we're sending sequence numbers, the
617 * LAC is broken. Discard the frame.
619 if ((!session->lns_mode) && (session->send_seq)) {
620 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_INFO,
621 "%s: requested to disable seq numbers by LNS\n",
623 session->send_seq = 0;
624 } else if (session->send_seq) {
625 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_WARNING,
626 "%s: recv data has no seq numbers when required. "
627 "Discarding\n", session->name);
628 session->stats.rx_seq_discards++;
632 /* Store L2TP info in the skb */
633 PPPOL2TP_SKB_CB(skb)->has_seq = 0;
636 /* If offset bit set, skip it. */
637 if (hdrflags & L2TP_HDRFLAG_O) {
638 offset = ntohs(*(__be16 *)ptr);
639 skb->transport_header += 2 + offset;
640 if (!pskb_may_pull(skb, skb_transport_offset(skb) + 2))
644 __skb_pull(skb, skb_transport_offset(skb));
646 /* Skip PPP header, if present. In testing, Microsoft L2TP clients
647 * don't send the PPP header (PPP header compression enabled), but
648 * other clients can include the header. So we cope with both cases
649 * here. The PPP header is always FF03 when using L2TP.
651 * Note that skb->data[] isn't dereferenced from a u16 ptr here since
652 * the field may be unaligned.
654 if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
657 /* Prepare skb for adding to the session's reorder_q. Hold
658 * packets for max reorder_timeout or 1 second if not
661 PPPOL2TP_SKB_CB(skb)->length = length;
662 PPPOL2TP_SKB_CB(skb)->expires = jiffies +
663 (session->reorder_timeout ? session->reorder_timeout : HZ);
665 /* Add packet to the session's receive queue. Reordering is done here, if
666 * enabled. Saved L2TP protocol info is stored in skb->sb[].
668 if (PPPOL2TP_SKB_CB(skb)->has_seq) {
669 if (session->reorder_timeout != 0) {
670 /* Packet reordering enabled. Add skb to session's
671 * reorder queue, in order of ns.
673 pppol2tp_recv_queue_skb(session, skb);
675 /* Packet reordering disabled. Discard out-of-sequence
678 if (PPPOL2TP_SKB_CB(skb)->ns != session->nr) {
679 session->stats.rx_seq_discards++;
680 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
681 "%s: oos pkt %hu len %d discarded, "
682 "waiting for %hu, reorder_q_len=%d\n",
683 session->name, PPPOL2TP_SKB_CB(skb)->ns,
684 PPPOL2TP_SKB_CB(skb)->length, session->nr,
685 skb_queue_len(&session->reorder_q));
688 skb_queue_tail(&session->reorder_q, skb);
691 /* No sequence numbers. Add the skb to the tail of the
692 * reorder queue. This ensures that it will be
693 * delivered after all previous sequenced skbs.
695 skb_queue_tail(&session->reorder_q, skb);
698 /* Try to dequeue as many skbs from reorder_q as we can. */
699 pppol2tp_recv_dequeue(session);
704 session->stats.rx_errors++;
706 sock_put(session->sock);
714 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
718 * >0: skb should be passed up to userspace as UDP.
720 static int pppol2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
722 struct pppol2tp_tunnel *tunnel;
724 tunnel = pppol2tp_sock_to_tunnel(sk);
728 PRINTK(tunnel->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
729 "%s: received %d bytes\n", tunnel->name, skb->len);
731 if (pppol2tp_recv_core(sk, skb))
740 /* Receive message. This is the recvmsg for the PPPoL2TP socket.
742 static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
743 struct msghdr *msg, size_t len,
748 struct sock *sk = sock->sk;
751 if (sk->sk_state & PPPOX_BOUND)
754 msg->msg_namelen = 0;
757 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
758 flags & MSG_DONTWAIT, &err);
760 err = memcpy_toiovec(msg->msg_iov, (unsigned char *) skb->data,
772 /************************************************************************
774 ***********************************************************************/
776 /* Tell how big L2TP headers are for a particular session. This
777 * depends on whether sequence numbers are being used.
779 static inline int pppol2tp_l2tp_header_len(struct pppol2tp_session *session)
781 if (session->send_seq)
782 return PPPOL2TP_L2TP_HDR_SIZE_SEQ;
784 return PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
787 /* Build an L2TP header for the session into the buffer provided.
789 static void pppol2tp_build_l2tp_header(struct pppol2tp_session *session,
793 u16 flags = L2TP_HDR_VER;
795 if (session->send_seq)
796 flags |= L2TP_HDRFLAG_S;
798 /* Setup L2TP header.
799 * FIXME: Can this ever be unaligned? Is direct dereferencing of
800 * 16-bit header fields safe here for all architectures?
802 *bufp++ = htons(flags);
803 *bufp++ = htons(session->tunnel_addr.d_tunnel);
804 *bufp++ = htons(session->tunnel_addr.d_session);
805 if (session->send_seq) {
806 *bufp++ = htons(session->ns);
809 PRINTK(session->debug, PPPOL2TP_MSG_SEQ, KERN_DEBUG,
810 "%s: updated ns to %hu\n", session->name, session->ns);
814 /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
815 * when a user application does a sendmsg() on the session socket. L2TP and
816 * PPP headers must be inserted into the user's data.
818 static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
821 static const unsigned char ppph[2] = { 0xff, 0x03 };
822 struct sock *sk = sock->sk;
823 struct inet_sock *inet;
828 struct pppol2tp_session *session;
829 struct pppol2tp_tunnel *tunnel;
834 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
837 /* Get session and tunnel contexts */
839 session = pppol2tp_sock_to_session(sk);
843 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
847 /* What header length is configured for this session? */
848 hdr_len = pppol2tp_l2tp_header_len(session);
850 /* Allocate a socket buffer */
852 skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
853 sizeof(struct udphdr) + hdr_len +
854 sizeof(ppph) + total_len,
859 /* Reserve space for headers. */
860 skb_reserve(skb, NET_SKB_PAD);
861 skb_reset_network_header(skb);
862 skb_reserve(skb, sizeof(struct iphdr));
863 skb_reset_transport_header(skb);
865 /* Build UDP header */
866 inet = inet_sk(session->tunnel_sock);
867 uh = (struct udphdr *) skb->data;
868 uh->source = inet->sport;
869 uh->dest = inet->dport;
870 uh->len = htons(hdr_len + sizeof(ppph) + total_len);
872 skb_put(skb, sizeof(struct udphdr));
874 /* Build L2TP header */
875 pppol2tp_build_l2tp_header(session, skb->data);
876 skb_put(skb, hdr_len);
879 skb->data[0] = ppph[0];
880 skb->data[1] = ppph[1];
883 /* Copy user data into skb */
884 error = memcpy_fromiovec(skb->data, m->msg_iov, total_len);
889 skb_put(skb, total_len);
891 /* Calculate UDP checksum if configured to do so */
892 if (session->tunnel_sock->sk_no_check != UDP_CSUM_NOXMIT)
893 csum = udp_csum_outgoing(sk, skb);
896 if (session->send_seq)
897 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
898 "%s: send %Zd bytes, ns=%hu\n", session->name,
899 total_len, session->ns - 1);
901 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
902 "%s: send %Zd bytes\n", session->name, total_len);
904 if (session->debug & PPPOL2TP_MSG_DATA) {
906 unsigned char *datap = skb->data;
908 printk(KERN_DEBUG "%s: xmit:", session->name);
909 for (i = 0; i < total_len; i++) {
910 printk(" %02X", *datap++);
919 /* Queue the packet to IP for output */
921 error = ip_queue_xmit(skb, 1);
925 tunnel->stats.tx_packets++;
926 tunnel->stats.tx_bytes += len;
927 session->stats.tx_packets++;
928 session->stats.tx_bytes += len;
930 tunnel->stats.tx_errors++;
931 session->stats.tx_errors++;
938 /* Transmit function called by generic PPP driver. Sends PPP frame
939 * over PPPoL2TP socket.
941 * This is almost the same as pppol2tp_sendmsg(), but rather than
942 * being called with a msghdr from userspace, it is called with a skb
945 * The supplied skb from ppp doesn't have enough headroom for the
946 * insertion of L2TP, UDP and IP headers so we need to allocate more
947 * headroom in the skb. This will create a cloned skb. But we must be
948 * careful in the error case because the caller will expect to free
949 * the skb it supplied, not our cloned skb. So we take care to always
950 * leave the original skb unfreed if we return an error.
952 static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
954 static const u8 ppph[2] = { 0xff, 0x03 };
955 struct sock *sk = (struct sock *) chan->private;
958 struct pppol2tp_session *session;
959 struct pppol2tp_tunnel *tunnel;
962 int data_len = skb->len;
963 struct inet_sock *inet;
968 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
971 /* Get session and tunnel contexts from the socket */
972 session = pppol2tp_sock_to_session(sk);
976 sk_tun = session->tunnel_sock;
979 tunnel = pppol2tp_sock_to_tunnel(sk_tun);
983 /* What header length is configured for this session? */
984 hdr_len = pppol2tp_l2tp_header_len(session);
986 /* Check that there's enough headroom in the skb to insert IP,
987 * UDP and L2TP and PPP headers. If not enough, expand it to
988 * make room. Note that a new skb (or a clone) is
989 * allocated. If we return an error from this point on, make
990 * sure we free the new skb but do not free the original skb
991 * since that is done by the caller for the error case.
993 headroom = NET_SKB_PAD + sizeof(struct iphdr) +
994 sizeof(struct udphdr) + hdr_len + sizeof(ppph);
995 if (skb_cow_head(skb, headroom))
998 /* Setup PPP header */
999 __skb_push(skb, sizeof(ppph));
1000 skb->data[0] = ppph[0];
1001 skb->data[1] = ppph[1];
1003 /* Setup L2TP header */
1004 pppol2tp_build_l2tp_header(session, __skb_push(skb, hdr_len));
1006 /* Setup UDP header */
1007 inet = inet_sk(sk_tun);
1008 __skb_push(skb, sizeof(*uh));
1009 skb_reset_transport_header(skb);
1011 uh->source = inet->sport;
1012 uh->dest = inet->dport;
1013 uh->len = htons(sizeof(struct udphdr) + hdr_len + sizeof(ppph) + data_len);
1016 /* *BROKEN* Calculate UDP checksum if configured to do so */
1017 if (sk_tun->sk_no_check != UDP_CSUM_NOXMIT)
1018 csum = udp_csum_outgoing(sk_tun, skb);
1021 if (session->send_seq)
1022 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1023 "%s: send %d bytes, ns=%hu\n", session->name,
1024 data_len, session->ns - 1);
1026 PRINTK(session->debug, PPPOL2TP_MSG_DATA, KERN_DEBUG,
1027 "%s: send %d bytes\n", session->name, data_len);
1029 if (session->debug & PPPOL2TP_MSG_DATA) {
1031 unsigned char *datap = skb->data;
1033 printk(KERN_DEBUG "%s: xmit:", session->name);
1034 for (i = 0; i < data_len; i++) {
1035 printk(" %02X", *datap++);
1044 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1045 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1049 /* Get routing info from the tunnel socket */
1050 dst_release(skb->dst);
1051 skb->dst = sk_dst_get(sk_tun);
1053 /* Queue the packet to IP for output */
1055 rc = ip_queue_xmit(skb, 1);
1059 tunnel->stats.tx_packets++;
1060 tunnel->stats.tx_bytes += len;
1061 session->stats.tx_packets++;
1062 session->stats.tx_bytes += len;
1064 tunnel->stats.tx_errors++;
1065 session->stats.tx_errors++;
1071 /* Free the original skb */
1076 /*****************************************************************************
1077 * Session (and tunnel control) socket create/destroy.
1078 *****************************************************************************/
1080 /* When the tunnel UDP socket is closed, all the attached sockets need to go
1083 static void pppol2tp_tunnel_closeall(struct pppol2tp_tunnel *tunnel)
1086 struct hlist_node *walk;
1087 struct hlist_node *tmp;
1088 struct pppol2tp_session *session;
1094 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1095 "%s: closing all sessions...\n", tunnel->name);
1097 write_lock(&tunnel->hlist_lock);
1098 for (hash = 0; hash < PPPOL2TP_HASH_SIZE; hash++) {
1100 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1101 session = hlist_entry(walk, struct pppol2tp_session, hlist);
1105 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1106 "%s: closing session\n", session->name);
1108 hlist_del_init(&session->hlist);
1110 /* Since we should hold the sock lock while
1111 * doing any unbinding, we need to release the
1112 * lock we're holding before taking that lock.
1113 * Hold a reference to the sock so it doesn't
1114 * disappear as we're jumping between locks.
1117 write_unlock(&tunnel->hlist_lock);
1120 if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
1121 pppox_unbind_sock(sk);
1122 sk->sk_state = PPPOX_DEAD;
1123 sk->sk_state_change(sk);
1126 /* Purge any queued data */
1127 skb_queue_purge(&sk->sk_receive_queue);
1128 skb_queue_purge(&sk->sk_write_queue);
1129 skb_queue_purge(&session->reorder_q);
1134 /* Now restart from the beginning of this hash
1135 * chain. We always remove a session from the
1136 * list so we are guaranteed to make forward
1139 write_lock(&tunnel->hlist_lock);
1143 write_unlock(&tunnel->hlist_lock);
1146 /* Really kill the tunnel.
1147 * Come here only when all sessions have been cleared from the tunnel.
1149 static void pppol2tp_tunnel_free(struct pppol2tp_tunnel *tunnel)
1151 /* Remove from socket list */
1152 write_lock(&pppol2tp_tunnel_list_lock);
1153 list_del_init(&tunnel->list);
1154 write_unlock(&pppol2tp_tunnel_list_lock);
1156 atomic_dec(&pppol2tp_tunnel_count);
1160 /* Tunnel UDP socket destruct hook.
1161 * The tunnel context is deleted only when all session sockets have been
1164 static void pppol2tp_tunnel_destruct(struct sock *sk)
1166 struct pppol2tp_tunnel *tunnel;
1168 tunnel = pppol2tp_sock_to_tunnel(sk);
1172 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1173 "%s: closing...\n", tunnel->name);
1175 /* Close all sessions */
1176 pppol2tp_tunnel_closeall(tunnel);
1178 /* No longer an encapsulation socket. See net/ipv4/udp.c */
1179 (udp_sk(sk))->encap_type = 0;
1180 (udp_sk(sk))->encap_rcv = NULL;
1182 /* Remove hooks into tunnel socket */
1183 tunnel->sock = NULL;
1184 sk->sk_destruct = tunnel->old_sk_destruct;
1185 sk->sk_user_data = NULL;
1187 /* Call original (UDP) socket descructor */
1188 if (sk->sk_destruct != NULL)
1189 (*sk->sk_destruct)(sk);
1191 pppol2tp_tunnel_dec_refcount(tunnel);
1197 /* Really kill the session socket. (Called from sock_put() if
1200 static void pppol2tp_session_destruct(struct sock *sk)
1202 struct pppol2tp_session *session = NULL;
1204 if (sk->sk_user_data != NULL) {
1205 struct pppol2tp_tunnel *tunnel;
1207 session = pppol2tp_sock_to_session(sk);
1208 if (session == NULL)
1211 /* Don't use pppol2tp_sock_to_tunnel() here to
1212 * get the tunnel context because the tunnel
1213 * socket might have already been closed (its
1214 * sk->sk_user_data will be NULL) so use the
1215 * session's private tunnel ptr instead.
1217 tunnel = session->tunnel;
1218 if (tunnel != NULL) {
1219 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1221 /* If session_id is zero, this is a null
1222 * session context, which was created for a
1223 * socket that is being used only to manage
1226 if (session->tunnel_addr.s_session != 0) {
1227 /* Delete the session socket from the
1230 write_lock(&tunnel->hlist_lock);
1231 hlist_del_init(&session->hlist);
1232 write_unlock(&tunnel->hlist_lock);
1234 atomic_dec(&pppol2tp_session_count);
1237 /* This will delete the tunnel context if this
1238 * is the last session on the tunnel.
1240 session->tunnel = NULL;
1241 session->tunnel_sock = NULL;
1242 pppol2tp_tunnel_dec_refcount(tunnel);
1251 /* Called when the PPPoX socket (session) is closed.
1253 static int pppol2tp_release(struct socket *sock)
1255 struct sock *sk = sock->sk;
1263 if (sock_flag(sk, SOCK_DEAD) != 0)
1266 pppox_unbind_sock(sk);
1268 /* Signal the death of the socket. */
1269 sk->sk_state = PPPOX_DEAD;
1273 /* Purge any queued data */
1274 skb_queue_purge(&sk->sk_receive_queue);
1275 skb_queue_purge(&sk->sk_write_queue);
1279 /* This will delete the session context via
1280 * pppol2tp_session_destruct() if the socket's refcnt drops to
1292 /* Internal function to prepare a tunnel (UDP) socket to have PPPoX
1293 * sockets attached to it.
1295 static struct sock *pppol2tp_prepare_tunnel_socket(int fd, u16 tunnel_id,
1299 struct socket *sock = NULL;
1301 struct pppol2tp_tunnel *tunnel;
1302 struct sock *ret = NULL;
1304 /* Get the tunnel UDP socket from the fd, which was opened by
1305 * the userspace L2TP daemon.
1308 sock = sockfd_lookup(fd, &err);
1310 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1311 "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1312 tunnel_id, fd, err);
1318 /* Quick sanity checks */
1319 err = -EPROTONOSUPPORT;
1320 if (sk->sk_protocol != IPPROTO_UDP) {
1321 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1322 "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1323 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1326 err = -EAFNOSUPPORT;
1327 if (sock->ops->family != AF_INET) {
1328 PRINTK(-1, PPPOL2TP_MSG_CONTROL, KERN_ERR,
1329 "tunl %hu: fd %d wrong family, got %d, expected %d\n",
1330 tunnel_id, fd, sock->ops->family, AF_INET);
1336 /* Check if this socket has already been prepped */
1337 tunnel = (struct pppol2tp_tunnel *)sk->sk_user_data;
1338 if (tunnel != NULL) {
1339 /* User-data field already set */
1341 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
1343 /* This socket has already been prepped */
1348 /* This socket is available and needs prepping. Create a new tunnel
1349 * context and init it.
1351 sk->sk_user_data = tunnel = kzalloc(sizeof(struct pppol2tp_tunnel), GFP_KERNEL);
1352 if (sk->sk_user_data == NULL) {
1357 tunnel->magic = L2TP_TUNNEL_MAGIC;
1358 sprintf(&tunnel->name[0], "tunl %hu", tunnel_id);
1360 tunnel->stats.tunnel_id = tunnel_id;
1361 tunnel->debug = PPPOL2TP_DEFAULT_DEBUG_FLAGS;
1363 /* Hook on the tunnel socket destructor so that we can cleanup
1364 * if the tunnel socket goes away.
1366 tunnel->old_sk_destruct = sk->sk_destruct;
1367 sk->sk_destruct = &pppol2tp_tunnel_destruct;
1370 sk->sk_allocation = GFP_ATOMIC;
1373 rwlock_init(&tunnel->hlist_lock);
1375 /* Add tunnel to our list */
1376 INIT_LIST_HEAD(&tunnel->list);
1377 write_lock(&pppol2tp_tunnel_list_lock);
1378 list_add(&tunnel->list, &pppol2tp_tunnel_list);
1379 write_unlock(&pppol2tp_tunnel_list_lock);
1380 atomic_inc(&pppol2tp_tunnel_count);
1382 /* Bump the reference count. The tunnel context is deleted
1383 * only when this drops to zero.
1385 pppol2tp_tunnel_inc_refcount(tunnel);
1387 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1388 (udp_sk(sk))->encap_type = UDP_ENCAP_L2TPINUDP;
1389 (udp_sk(sk))->encap_rcv = pppol2tp_udp_encap_recv;
1405 static struct proto pppol2tp_sk_proto = {
1407 .owner = THIS_MODULE,
1408 .obj_size = sizeof(struct pppox_sock),
1411 /* socket() handler. Initialize a new struct sock.
1413 static int pppol2tp_create(struct socket *sock)
1415 int error = -ENOMEM;
1418 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, 1);
1422 sock_init_data(sock, sk);
1424 sock->state = SS_UNCONNECTED;
1425 sock->ops = &pppol2tp_ops;
1427 sk->sk_backlog_rcv = pppol2tp_recv_core;
1428 sk->sk_protocol = PX_PROTO_OL2TP;
1429 sk->sk_family = PF_PPPOX;
1430 sk->sk_state = PPPOX_NONE;
1431 sk->sk_type = SOCK_STREAM;
1432 sk->sk_destruct = pppol2tp_session_destruct;
1440 /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
1442 static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
1443 int sockaddr_len, int flags)
1445 struct sock *sk = sock->sk;
1446 struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
1447 struct pppox_sock *po = pppox_sk(sk);
1448 struct sock *tunnel_sock = NULL;
1449 struct pppol2tp_session *session = NULL;
1450 struct pppol2tp_tunnel *tunnel;
1451 struct dst_entry *dst;
1457 if (sp->sa_protocol != PX_PROTO_OL2TP)
1460 /* Check for already bound sockets */
1462 if (sk->sk_state & PPPOX_CONNECTED)
1465 /* We don't supporting rebinding anyway */
1467 if (sk->sk_user_data)
1468 goto end; /* socket is already attached */
1470 /* Don't bind if s_tunnel is 0 */
1472 if (sp->pppol2tp.s_tunnel == 0)
1475 /* Special case: prepare tunnel socket if s_session and
1476 * d_session is 0. Otherwise look up tunnel using supplied
1479 if ((sp->pppol2tp.s_session == 0) && (sp->pppol2tp.d_session == 0)) {
1480 tunnel_sock = pppol2tp_prepare_tunnel_socket(sp->pppol2tp.fd,
1481 sp->pppol2tp.s_tunnel,
1483 if (tunnel_sock == NULL)
1486 tunnel = tunnel_sock->sk_user_data;
1488 tunnel = pppol2tp_tunnel_find(sp->pppol2tp.s_tunnel);
1490 /* Error if we can't find the tunnel */
1495 tunnel_sock = tunnel->sock;
1498 /* Check that this session doesn't already exist */
1500 session = pppol2tp_session_find(tunnel, sp->pppol2tp.s_session);
1501 if (session != NULL)
1504 /* Allocate and initialize a new session context. */
1505 session = kzalloc(sizeof(struct pppol2tp_session), GFP_KERNEL);
1506 if (session == NULL) {
1511 skb_queue_head_init(&session->reorder_q);
1513 session->magic = L2TP_SESSION_MAGIC;
1514 session->owner = current->pid;
1516 session->tunnel = tunnel;
1517 session->tunnel_sock = tunnel_sock;
1518 session->tunnel_addr = sp->pppol2tp;
1519 sprintf(&session->name[0], "sess %hu/%hu",
1520 session->tunnel_addr.s_tunnel,
1521 session->tunnel_addr.s_session);
1523 session->stats.tunnel_id = session->tunnel_addr.s_tunnel;
1524 session->stats.session_id = session->tunnel_addr.s_session;
1526 INIT_HLIST_NODE(&session->hlist);
1528 /* Inherit debug options from tunnel */
1529 session->debug = tunnel->debug;
1531 /* Default MTU must allow space for UDP/L2TP/PPP
1534 session->mtu = session->mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
1536 /* If PMTU discovery was enabled, use the MTU that was discovered */
1537 dst = sk_dst_get(sk);
1539 u32 pmtu = dst_mtu(__sk_dst_get(sk));
1541 session->mtu = session->mru = pmtu -
1542 PPPOL2TP_HEADER_OVERHEAD;
1546 /* Special case: if source & dest session_id == 0x0000, this socket is
1547 * being created to manage the tunnel. Don't add the session to the
1548 * session hash list, just set up the internal context for use by
1549 * ioctl() and sockopt() handlers.
1551 if ((session->tunnel_addr.s_session == 0) &&
1552 (session->tunnel_addr.d_session == 0)) {
1554 sk->sk_user_data = session;
1558 /* Get tunnel context from the tunnel socket */
1559 tunnel = pppol2tp_sock_to_tunnel(tunnel_sock);
1560 if (tunnel == NULL) {
1565 /* Right now, because we don't have a way to push the incoming skb's
1566 * straight through the UDP layer, the only header we need to worry
1567 * about is the L2TP header. This size is different depending on
1568 * whether sequence numbers are enabled for the data channel.
1570 po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1572 po->chan.private = sk;
1573 po->chan.ops = &pppol2tp_chan_ops;
1574 po->chan.mtu = session->mtu;
1576 error = ppp_register_channel(&po->chan);
1580 /* This is how we get the session context from the socket. */
1581 sk->sk_user_data = session;
1583 /* Add session to the tunnel's hash list */
1584 write_lock(&tunnel->hlist_lock);
1585 hlist_add_head(&session->hlist,
1586 pppol2tp_session_id_hash(tunnel,
1587 session->tunnel_addr.s_session));
1588 write_unlock(&tunnel->hlist_lock);
1590 atomic_inc(&pppol2tp_session_count);
1593 pppol2tp_tunnel_inc_refcount(tunnel);
1594 sk->sk_state = PPPOX_CONNECTED;
1595 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1596 "%s: created\n", session->name);
1602 PRINTK(session ? session->debug : -1, PPPOL2TP_MSG_CONTROL, KERN_WARNING,
1603 "%s: connect failed: %d\n", session->name, error);
1608 /* getname() support.
1610 static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
1611 int *usockaddr_len, int peer)
1613 int len = sizeof(struct sockaddr_pppol2tp);
1614 struct sockaddr_pppol2tp sp;
1616 struct pppol2tp_session *session;
1619 if (sock->sk->sk_state != PPPOX_CONNECTED)
1622 session = pppol2tp_sock_to_session(sock->sk);
1623 if (session == NULL) {
1628 sp.sa_family = AF_PPPOX;
1629 sp.sa_protocol = PX_PROTO_OL2TP;
1630 memcpy(&sp.pppol2tp, &session->tunnel_addr,
1631 sizeof(struct pppol2tp_addr));
1633 memcpy(uaddr, &sp, len);
1635 *usockaddr_len = len;
1643 /****************************************************************************
1646 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1647 * sockets. However, in order to control kernel tunnel features, we allow
1648 * userspace to create a special "tunnel" PPPoX socket which is used for
1649 * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
1650 * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
1652 ****************************************************************************/
1654 /* Session ioctl helper.
1656 static int pppol2tp_session_ioctl(struct pppol2tp_session *session,
1657 unsigned int cmd, unsigned long arg)
1661 struct sock *sk = session->sock;
1662 int val = (int) arg;
1664 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1665 "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
1666 session->name, cmd, arg);
1673 if (!(sk->sk_state & PPPOX_CONNECTED))
1677 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1679 ifr.ifr_mtu = session->mtu;
1680 if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
1683 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1684 "%s: get mtu=%d\n", session->name, session->mtu);
1690 if (!(sk->sk_state & PPPOX_CONNECTED))
1694 if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
1697 session->mtu = ifr.ifr_mtu;
1699 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1700 "%s: set mtu=%d\n", session->name, session->mtu);
1706 if (!(sk->sk_state & PPPOX_CONNECTED))
1710 if (put_user(session->mru, (int __user *) arg))
1713 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1714 "%s: get mru=%d\n", session->name, session->mru);
1720 if (!(sk->sk_state & PPPOX_CONNECTED))
1724 if (get_user(val,(int __user *) arg))
1728 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1729 "%s: set mru=%d\n", session->name, session->mru);
1735 if (put_user(session->flags, (int __user *) arg))
1738 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1739 "%s: get flags=%d\n", session->name, session->flags);
1745 if (get_user(val, (int __user *) arg))
1747 session->flags = val;
1748 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1749 "%s: set flags=%d\n", session->name, session->flags);
1753 case PPPIOCGL2TPSTATS:
1755 if (!(sk->sk_state & PPPOX_CONNECTED))
1758 if (copy_to_user((void __user *) arg, &session->stats,
1759 sizeof(session->stats)))
1761 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1762 "%s: get L2TP stats\n", session->name);
1776 /* Tunnel ioctl helper.
1778 * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
1779 * specifies a session_id, the session ioctl handler is called. This allows an
1780 * application to retrieve session stats via a tunnel socket.
1782 static int pppol2tp_tunnel_ioctl(struct pppol2tp_tunnel *tunnel,
1783 unsigned int cmd, unsigned long arg)
1786 struct sock *sk = tunnel->sock;
1787 struct pppol2tp_ioc_stats stats_req;
1789 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_DEBUG,
1790 "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n", tunnel->name,
1796 case PPPIOCGL2TPSTATS:
1798 if (!(sk->sk_state & PPPOX_CONNECTED))
1801 if (copy_from_user(&stats_req, (void __user *) arg,
1802 sizeof(stats_req))) {
1806 if (stats_req.session_id != 0) {
1807 /* resend to session ioctl handler */
1808 struct pppol2tp_session *session =
1809 pppol2tp_session_find(tunnel, stats_req.session_id);
1810 if (session != NULL)
1811 err = pppol2tp_session_ioctl(session, cmd, arg);
1817 tunnel->stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
1819 if (copy_to_user((void __user *) arg, &tunnel->stats,
1820 sizeof(tunnel->stats))) {
1824 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1825 "%s: get L2TP stats\n", tunnel->name);
1839 /* Main ioctl() handler.
1840 * Dispatch to tunnel or session helpers depending on the socket.
1842 static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
1845 struct sock *sk = sock->sk;
1846 struct pppol2tp_session *session;
1847 struct pppol2tp_tunnel *tunnel;
1854 if (sock_flag(sk, SOCK_DEAD) != 0)
1858 if ((sk->sk_user_data == NULL) ||
1859 (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
1862 /* Get session context from the socket */
1864 session = pppol2tp_sock_to_session(sk);
1865 if (session == NULL)
1868 /* Special case: if session's session_id is zero, treat ioctl as a
1871 if ((session->tunnel_addr.s_session == 0) &&
1872 (session->tunnel_addr.d_session == 0)) {
1874 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
1878 err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
1882 err = pppol2tp_session_ioctl(session, cmd, arg);
1888 /*****************************************************************************
1889 * setsockopt() / getsockopt() support.
1891 * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
1892 * sockets. In order to control kernel tunnel features, we allow userspace to
1893 * create a special "tunnel" PPPoX socket which is used for control only.
1894 * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
1895 * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
1896 *****************************************************************************/
1898 /* Tunnel setsockopt() helper.
1900 static int pppol2tp_tunnel_setsockopt(struct sock *sk,
1901 struct pppol2tp_tunnel *tunnel,
1902 int optname, int val)
1907 case PPPOL2TP_SO_DEBUG:
1908 tunnel->debug = val;
1909 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1910 "%s: set debug=%x\n", tunnel->name, tunnel->debug);
1921 /* Session setsockopt helper.
1923 static int pppol2tp_session_setsockopt(struct sock *sk,
1924 struct pppol2tp_session *session,
1925 int optname, int val)
1930 case PPPOL2TP_SO_RECVSEQ:
1931 if ((val != 0) && (val != 1)) {
1935 session->recv_seq = val ? -1 : 0;
1936 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1937 "%s: set recv_seq=%d\n", session->name,
1941 case PPPOL2TP_SO_SENDSEQ:
1942 if ((val != 0) && (val != 1)) {
1946 session->send_seq = val ? -1 : 0;
1948 struct sock *ssk = session->sock;
1949 struct pppox_sock *po = pppox_sk(ssk);
1950 po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
1951 PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
1953 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1954 "%s: set send_seq=%d\n", session->name, session->send_seq);
1957 case PPPOL2TP_SO_LNSMODE:
1958 if ((val != 0) && (val != 1)) {
1962 session->lns_mode = val ? -1 : 0;
1963 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1964 "%s: set lns_mode=%d\n", session->name,
1968 case PPPOL2TP_SO_DEBUG:
1969 session->debug = val;
1970 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1971 "%s: set debug=%x\n", session->name, session->debug);
1974 case PPPOL2TP_SO_REORDERTO:
1975 session->reorder_timeout = msecs_to_jiffies(val);
1976 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
1977 "%s: set reorder_timeout=%d\n", session->name,
1978 session->reorder_timeout);
1989 /* Main setsockopt() entry point.
1990 * Does API checks, then calls either the tunnel or session setsockopt
1991 * handler, according to whether the PPPoL2TP socket is a for a regular
1992 * session or the special tunnel type.
1994 static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
1995 char __user *optval, int optlen)
1997 struct sock *sk = sock->sk;
1998 struct pppol2tp_session *session = sk->sk_user_data;
1999 struct pppol2tp_tunnel *tunnel;
2003 if (level != SOL_PPPOL2TP)
2004 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
2006 if (optlen < sizeof(int))
2009 if (get_user(val, (int __user *)optval))
2013 if (sk->sk_user_data == NULL)
2016 /* Get session context from the socket */
2018 session = pppol2tp_sock_to_session(sk);
2019 if (session == NULL)
2022 /* Special case: if session_id == 0x0000, treat as operation on tunnel
2024 if ((session->tunnel_addr.s_session == 0) &&
2025 (session->tunnel_addr.d_session == 0)) {
2027 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2031 err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
2033 err = pppol2tp_session_setsockopt(sk, session, optname, val);
2041 /* Tunnel getsockopt helper. Called with sock locked.
2043 static int pppol2tp_tunnel_getsockopt(struct sock *sk,
2044 struct pppol2tp_tunnel *tunnel,
2045 int optname, int *val)
2050 case PPPOL2TP_SO_DEBUG:
2051 *val = tunnel->debug;
2052 PRINTK(tunnel->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2053 "%s: get debug=%x\n", tunnel->name, tunnel->debug);
2064 /* Session getsockopt helper. Called with sock locked.
2066 static int pppol2tp_session_getsockopt(struct sock *sk,
2067 struct pppol2tp_session *session,
2068 int optname, int *val)
2073 case PPPOL2TP_SO_RECVSEQ:
2074 *val = session->recv_seq;
2075 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2076 "%s: get recv_seq=%d\n", session->name, *val);
2079 case PPPOL2TP_SO_SENDSEQ:
2080 *val = session->send_seq;
2081 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2082 "%s: get send_seq=%d\n", session->name, *val);
2085 case PPPOL2TP_SO_LNSMODE:
2086 *val = session->lns_mode;
2087 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2088 "%s: get lns_mode=%d\n", session->name, *val);
2091 case PPPOL2TP_SO_DEBUG:
2092 *val = session->debug;
2093 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2094 "%s: get debug=%d\n", session->name, *val);
2097 case PPPOL2TP_SO_REORDERTO:
2098 *val = (int) jiffies_to_msecs(session->reorder_timeout);
2099 PRINTK(session->debug, PPPOL2TP_MSG_CONTROL, KERN_INFO,
2100 "%s: get reorder_timeout=%d\n", session->name, *val);
2110 /* Main getsockopt() entry point.
2111 * Does API checks, then calls either the tunnel or session getsockopt
2112 * handler, according to whether the PPPoX socket is a for a regular session
2113 * or the special tunnel type.
2115 static int pppol2tp_getsockopt(struct socket *sock, int level,
2116 int optname, char __user *optval, int __user *optlen)
2118 struct sock *sk = sock->sk;
2119 struct pppol2tp_session *session = sk->sk_user_data;
2120 struct pppol2tp_tunnel *tunnel;
2124 if (level != SOL_PPPOL2TP)
2125 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
2127 if (get_user(len, (int __user *) optlen))
2130 len = min_t(unsigned int, len, sizeof(int));
2136 if (sk->sk_user_data == NULL)
2139 /* Get the session context */
2141 session = pppol2tp_sock_to_session(sk);
2142 if (session == NULL)
2145 /* Special case: if session_id == 0x0000, treat as operation on tunnel */
2146 if ((session->tunnel_addr.s_session == 0) &&
2147 (session->tunnel_addr.d_session == 0)) {
2149 tunnel = pppol2tp_sock_to_tunnel(session->tunnel_sock);
2153 err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
2155 err = pppol2tp_session_getsockopt(sk, session, optname, &val);
2158 if (put_user(len, (int __user *) optlen))
2161 if (copy_to_user((void __user *) optval, &val, len))
2169 /*****************************************************************************
2170 * /proc filesystem for debug
2171 *****************************************************************************/
2173 #ifdef CONFIG_PROC_FS
2175 #include <linux/seq_file.h>
2177 struct pppol2tp_seq_data {
2178 struct pppol2tp_tunnel *tunnel; /* current tunnel */
2179 struct pppol2tp_session *session; /* NULL means get first session in tunnel */
2182 static struct pppol2tp_session *next_session(struct pppol2tp_tunnel *tunnel, struct pppol2tp_session *curr)
2184 struct pppol2tp_session *session = NULL;
2185 struct hlist_node *walk;
2190 read_lock(&tunnel->hlist_lock);
2191 for (i = 0; i < PPPOL2TP_HASH_SIZE; i++) {
2192 hlist_for_each_entry(session, walk, &tunnel->session_hlist[i], hlist) {
2197 if (session == curr) {
2208 read_unlock(&tunnel->hlist_lock);
2215 static struct pppol2tp_tunnel *next_tunnel(struct pppol2tp_tunnel *curr)
2217 struct pppol2tp_tunnel *tunnel = NULL;
2219 read_lock(&pppol2tp_tunnel_list_lock);
2220 if (list_is_last(&curr->list, &pppol2tp_tunnel_list)) {
2223 tunnel = list_entry(curr->list.next, struct pppol2tp_tunnel, list);
2225 read_unlock(&pppol2tp_tunnel_list_lock);
2230 static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
2232 struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
2238 BUG_ON(m->private == NULL);
2241 if (pd->tunnel == NULL) {
2242 if (!list_empty(&pppol2tp_tunnel_list))
2243 pd->tunnel = list_entry(pppol2tp_tunnel_list.next, struct pppol2tp_tunnel, list);
2245 pd->session = next_session(pd->tunnel, pd->session);
2246 if (pd->session == NULL) {
2247 pd->tunnel = next_tunnel(pd->tunnel);
2251 /* NULL tunnel and session indicates end of list */
2252 if ((pd->tunnel == NULL) && (pd->session == NULL))
2259 static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
2265 static void pppol2tp_seq_stop(struct seq_file *p, void *v)
2270 static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
2272 struct pppol2tp_tunnel *tunnel = v;
2274 seq_printf(m, "\nTUNNEL '%s', %c %d\n",
2276 (tunnel == tunnel->sock->sk_user_data) ? 'Y':'N',
2277 atomic_read(&tunnel->ref_count) - 1);
2278 seq_printf(m, " %08x %llu/%llu/%llu %llu/%llu/%llu\n",
2280 tunnel->stats.tx_packets, tunnel->stats.tx_bytes,
2281 tunnel->stats.tx_errors,
2282 tunnel->stats.rx_packets, tunnel->stats.rx_bytes,
2283 tunnel->stats.rx_errors);
2286 static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
2288 struct pppol2tp_session *session = v;
2290 seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
2291 "%04X/%04X %d %c\n",
2293 ntohl(session->tunnel_addr.addr.sin_addr.s_addr),
2294 ntohs(session->tunnel_addr.addr.sin_port),
2295 session->tunnel_addr.s_tunnel,
2296 session->tunnel_addr.s_session,
2297 session->tunnel_addr.d_tunnel,
2298 session->tunnel_addr.d_session,
2299 session->sock->sk_state,
2300 (session == session->sock->sk_user_data) ?
2302 seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
2303 session->mtu, session->mru,
2304 session->recv_seq ? 'R' : '-',
2305 session->send_seq ? 'S' : '-',
2306 session->lns_mode ? "LNS" : "LAC",
2308 jiffies_to_msecs(session->reorder_timeout));
2309 seq_printf(m, " %hu/%hu %llu/%llu/%llu %llu/%llu/%llu\n",
2310 session->nr, session->ns,
2311 session->stats.tx_packets,
2312 session->stats.tx_bytes,
2313 session->stats.tx_errors,
2314 session->stats.rx_packets,
2315 session->stats.rx_bytes,
2316 session->stats.rx_errors);
2319 static int pppol2tp_seq_show(struct seq_file *m, void *v)
2321 struct pppol2tp_seq_data *pd = v;
2323 /* display header on line 1 */
2324 if (v == SEQ_START_TOKEN) {
2325 seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
2326 seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
2327 seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2328 seq_puts(m, " SESSION name, addr/port src-tid/sid "
2329 "dest-tid/sid state user-data-ok\n");
2330 seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
2331 seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
2335 /* Show the tunnel or session context.
2337 if (pd->session == NULL)
2338 pppol2tp_seq_tunnel_show(m, pd->tunnel);
2340 pppol2tp_seq_session_show(m, pd->session);
2346 static struct seq_operations pppol2tp_seq_ops = {
2347 .start = pppol2tp_seq_start,
2348 .next = pppol2tp_seq_next,
2349 .stop = pppol2tp_seq_stop,
2350 .show = pppol2tp_seq_show,
2353 /* Called when our /proc file is opened. We allocate data for use when
2354 * iterating our tunnel / session contexts and store it in the private
2355 * data of the seq_file.
2357 static int pppol2tp_proc_open(struct inode *inode, struct file *file)
2360 struct pppol2tp_seq_data *pd;
2363 ret = seq_open(file, &pppol2tp_seq_ops);
2367 m = file->private_data;
2369 /* Allocate and fill our proc_data for access later */
2371 m->private = kzalloc(sizeof(struct pppol2tp_seq_data), GFP_KERNEL);
2372 if (m->private == NULL)
2382 /* Called when /proc file access completes.
2384 static int pppol2tp_proc_release(struct inode *inode, struct file *file)
2386 struct seq_file *m = (struct seq_file *)file->private_data;
2391 return seq_release(inode, file);
2394 static struct file_operations pppol2tp_proc_fops = {
2395 .owner = THIS_MODULE,
2396 .open = pppol2tp_proc_open,
2398 .llseek = seq_lseek,
2399 .release = pppol2tp_proc_release,
2402 static struct proc_dir_entry *pppol2tp_proc;
2404 #endif /* CONFIG_PROC_FS */
2406 /*****************************************************************************
2408 *****************************************************************************/
2410 static struct proto_ops pppol2tp_ops = {
2412 .owner = THIS_MODULE,
2413 .release = pppol2tp_release,
2414 .bind = sock_no_bind,
2415 .connect = pppol2tp_connect,
2416 .socketpair = sock_no_socketpair,
2417 .accept = sock_no_accept,
2418 .getname = pppol2tp_getname,
2419 .poll = datagram_poll,
2420 .listen = sock_no_listen,
2421 .shutdown = sock_no_shutdown,
2422 .setsockopt = pppol2tp_setsockopt,
2423 .getsockopt = pppol2tp_getsockopt,
2424 .sendmsg = pppol2tp_sendmsg,
2425 .recvmsg = pppol2tp_recvmsg,
2426 .mmap = sock_no_mmap,
2427 .ioctl = pppox_ioctl,
2430 static struct pppox_proto pppol2tp_proto = {
2431 .create = pppol2tp_create,
2432 .ioctl = pppol2tp_ioctl
2435 static int __init pppol2tp_init(void)
2439 err = proto_register(&pppol2tp_sk_proto, 0);
2442 err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
2444 goto out_unregister_pppol2tp_proto;
2446 #ifdef CONFIG_PROC_FS
2447 pppol2tp_proc = create_proc_entry("pppol2tp", 0, proc_net);
2448 if (!pppol2tp_proc) {
2450 goto out_unregister_pppox_proto;
2452 pppol2tp_proc->proc_fops = &pppol2tp_proc_fops;
2453 #endif /* CONFIG_PROC_FS */
2454 printk(KERN_INFO "PPPoL2TP kernel driver, %s\n",
2455 PPPOL2TP_DRV_VERSION);
2460 out_unregister_pppox_proto:
2461 unregister_pppox_proto(PX_PROTO_OL2TP);
2462 out_unregister_pppol2tp_proto:
2463 proto_unregister(&pppol2tp_sk_proto);
2467 static void __exit pppol2tp_exit(void)
2469 unregister_pppox_proto(PX_PROTO_OL2TP);
2471 #ifdef CONFIG_PROC_FS
2472 remove_proc_entry("pppol2tp", proc_net);
2474 proto_unregister(&pppol2tp_sk_proto);
2477 module_init(pppol2tp_init);
2478 module_exit(pppol2tp_exit);
2480 MODULE_AUTHOR("Martijn van Oosterhout <kleptog@svana.org>,"
2481 "James Chapman <jchapman@katalix.com>");
2482 MODULE_DESCRIPTION("PPP over L2TP over UDP");
2483 MODULE_LICENSE("GPL");
2484 MODULE_VERSION(PPPOL2TP_DRV_VERSION);